aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty')
-rw-r--r--drivers/tty/Kconfig352
-rw-r--r--drivers/tty/Makefile30
-rw-r--r--drivers/tty/amiserial.c2176
-rw-r--r--drivers/tty/bfin_jtag_comm.c368
-rw-r--r--drivers/tty/cyclades.c4196
-rw-r--r--drivers/tty/hvc/Kconfig105
-rw-r--r--drivers/tty/hvc/Makefile13
-rw-r--r--drivers/tty/hvc/hvc_beat.c134
-rw-r--r--drivers/tty/hvc/hvc_bfin_jtag.c105
-rw-r--r--drivers/tty/hvc/hvc_console.c914
-rw-r--r--drivers/tty/hvc/hvc_console.h119
-rw-r--r--drivers/tty/hvc/hvc_dcc.c104
-rw-r--r--drivers/tty/hvc/hvc_irq.c49
-rw-r--r--drivers/tty/hvc/hvc_iseries.c598
-rw-r--r--drivers/tty/hvc/hvc_iucv.c1337
-rw-r--r--drivers/tty/hvc/hvc_rtas.c133
-rw-r--r--drivers/tty/hvc/hvc_tile.c68
-rw-r--r--drivers/tty/hvc/hvc_udbg.c96
-rw-r--r--drivers/tty/hvc/hvc_vio.c173
-rw-r--r--drivers/tty/hvc/hvc_xen.c273
-rw-r--r--drivers/tty/hvc/hvcs.c1616
-rw-r--r--drivers/tty/hvc/hvsi.c1314
-rw-r--r--drivers/tty/ipwireless/Makefile8
-rw-r--r--drivers/tty/ipwireless/hardware.c1764
-rw-r--r--drivers/tty/ipwireless/hardware.h62
-rw-r--r--drivers/tty/ipwireless/main.c347
-rw-r--r--drivers/tty/ipwireless/main.h68
-rw-r--r--drivers/tty/ipwireless/network.c508
-rw-r--r--drivers/tty/ipwireless/network.h53
-rw-r--r--drivers/tty/ipwireless/setup_protocol.h108
-rw-r--r--drivers/tty/ipwireless/tty.c679
-rw-r--r--drivers/tty/ipwireless/tty.h45
-rw-r--r--drivers/tty/isicom.c1736
-rw-r--r--drivers/tty/moxa.c2084
-rw-r--r--drivers/tty/moxa.h304
-rw-r--r--drivers/tty/mxser.c2757
-rw-r--r--drivers/tty/mxser.h150
-rw-r--r--drivers/tty/n_gsm.c2810
-rw-r--r--drivers/tty/n_hdlc.c1006
-rw-r--r--drivers/tty/n_r3964.c1263
-rw-r--r--drivers/tty/n_tracerouter.c243
-rw-r--r--drivers/tty/n_tracesink.c238
-rw-r--r--drivers/tty/n_tracesink.h36
-rw-r--r--drivers/tty/n_tty.c2128
-rw-r--r--drivers/tty/nozomi.c1971
-rw-r--r--drivers/tty/pty.c777
-rw-r--r--drivers/tty/rocket.c3152
-rw-r--r--drivers/tty/rocket.h111
-rw-r--r--drivers/tty/rocket_int.h1214
-rw-r--r--drivers/tty/serial/21285.c511
-rw-r--r--drivers/tty/serial/68328serial.c1448
-rw-r--r--drivers/tty/serial/68328serial.h187
-rw-r--r--drivers/tty/serial/68360serial.c2979
-rw-r--r--drivers/tty/serial/8250.c3424
-rw-r--r--drivers/tty/serial/8250.h79
-rw-r--r--drivers/tty/serial/8250_accent.c45
-rw-r--r--drivers/tty/serial/8250_acorn.c141
-rw-r--r--drivers/tty/serial/8250_boca.c59
-rw-r--r--drivers/tty/serial/8250_early.c287
-rw-r--r--drivers/tty/serial/8250_exar_st16c554.c50
-rw-r--r--drivers/tty/serial/8250_fourport.c51
-rw-r--r--drivers/tty/serial/8250_gsc.c122
-rw-r--r--drivers/tty/serial/8250_hp300.c327
-rw-r--r--drivers/tty/serial/8250_hub6.c56
-rw-r--r--drivers/tty/serial/8250_mca.c61
-rw-r--r--drivers/tty/serial/8250_pci.c3956
-rw-r--r--drivers/tty/serial/8250_pnp.c521
-rw-r--r--drivers/tty/serial/Kconfig1638
-rw-r--r--drivers/tty/serial/Makefile98
-rw-r--r--drivers/tty/serial/altera_jtaguart.c516
-rw-r--r--drivers/tty/serial/altera_uart.c661
-rw-r--r--drivers/tty/serial/amba-pl010.c823
-rw-r--r--drivers/tty/serial/amba-pl011.c2027
-rw-r--r--drivers/tty/serial/apbuart.c696
-rw-r--r--drivers/tty/serial/apbuart.h64
-rw-r--r--drivers/tty/serial/atmel_serial.c1828
-rw-r--r--drivers/tty/serial/bcm63xx_uart.c901
-rw-r--r--drivers/tty/serial/bfin_5xx.c1597
-rw-r--r--drivers/tty/serial/bfin_sport_uart.c934
-rw-r--r--drivers/tty/serial/bfin_sport_uart.h86
-rw-r--r--drivers/tty/serial/clps711x.c577
-rw-r--r--drivers/tty/serial/cpm_uart/Makefile11
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart.h145
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_core.c1440
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c136
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h32
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c172
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h32
-rw-r--r--drivers/tty/serial/crisv10.c4572
-rw-r--r--drivers/tty/serial/crisv10.h147
-rw-r--r--drivers/tty/serial/dz.c955
-rw-r--r--drivers/tty/serial/dz.h129
-rw-r--r--drivers/tty/serial/icom.c1658
-rw-r--r--drivers/tty/serial/icom.h287
-rw-r--r--drivers/tty/serial/ifx6x60.c1414
-rw-r--r--drivers/tty/serial/ifx6x60.h129
-rw-r--r--drivers/tty/serial/imx.c1379
-rw-r--r--drivers/tty/serial/ioc3_serial.c2199
-rw-r--r--drivers/tty/serial/ioc4_serial.c2953
-rw-r--r--drivers/tty/serial/ip22zilog.c1221
-rw-r--r--drivers/tty/serial/ip22zilog.h281
-rw-r--r--drivers/tty/serial/jsm/Makefile8
-rw-r--r--drivers/tty/serial/jsm/jsm.h388
-rw-r--r--drivers/tty/serial/jsm/jsm_driver.c297
-rw-r--r--drivers/tty/serial/jsm/jsm_neo.c1412
-rw-r--r--drivers/tty/serial/jsm/jsm_tty.c910
-rw-r--r--drivers/tty/serial/kgdboc.c328
-rw-r--r--drivers/tty/serial/lantiq.c756
-rw-r--r--drivers/tty/serial/m32r_sio.c1193
-rw-r--r--drivers/tty/serial/m32r_sio.h48
-rw-r--r--drivers/tty/serial/m32r_sio_reg.h152
-rw-r--r--drivers/tty/serial/max3100.c926
-rw-r--r--drivers/tty/serial/max3107-aava.c344
-rw-r--r--drivers/tty/serial/max3107.c1213
-rw-r--r--drivers/tty/serial/max3107.h441
-rw-r--r--drivers/tty/serial/mcf.c662
-rw-r--r--drivers/tty/serial/mfd.c1470
-rw-r--r--drivers/tty/serial/mpc52xx_uart.c1524
-rw-r--r--drivers/tty/serial/mpsc.c2159
-rw-r--r--drivers/tty/serial/mrst_max3110.c920
-rw-r--r--drivers/tty/serial/mrst_max3110.h60
-rw-r--r--drivers/tty/serial/msm_serial.c966
-rw-r--r--drivers/tty/serial/msm_serial.h187
-rw-r--r--drivers/tty/serial/msm_serial_hs.c1880
-rw-r--r--drivers/tty/serial/msm_smd_tty.c235
-rw-r--r--drivers/tty/serial/mux.c633
-rw-r--r--drivers/tty/serial/mxs-auart.c798
-rw-r--r--drivers/tty/serial/netx-serial.c748
-rw-r--r--drivers/tty/serial/nwpserial.c477
-rw-r--r--drivers/tty/serial/of_serial.c206
-rw-r--r--drivers/tty/serial/omap-serial.c1362
-rw-r--r--drivers/tty/serial/pch_uart.c1612
-rw-r--r--drivers/tty/serial/pmac_zilog.c2206
-rw-r--r--drivers/tty/serial/pmac_zilog.h396
-rw-r--r--drivers/tty/serial/pnx8xxx_uart.c854
-rw-r--r--drivers/tty/serial/pxa.c877
-rw-r--r--drivers/tty/serial/s3c2400.c105
-rw-r--r--drivers/tty/serial/s3c2410.c117
-rw-r--r--drivers/tty/serial/s3c2412.c151
-rw-r--r--drivers/tty/serial/s3c2440.c180
-rw-r--r--drivers/tty/serial/s3c24a0.c117
-rw-r--r--drivers/tty/serial/s3c6400.c151
-rw-r--r--drivers/tty/serial/s5pv210.c161
-rw-r--r--drivers/tty/serial/sa1100.c916
-rw-r--r--drivers/tty/serial/samsung.c1486
-rw-r--r--drivers/tty/serial/samsung.h119
-rw-r--r--drivers/tty/serial/sb1250-duart.c974
-rw-r--r--drivers/tty/serial/sc26xx.c757
-rw-r--r--drivers/tty/serial/serial_core.c2504
-rw-r--r--drivers/tty/serial/serial_cs.c870
-rw-r--r--drivers/tty/serial/serial_ks8695.c703
-rw-r--r--drivers/tty/serial/serial_txx9.c1342
-rw-r--r--drivers/tty/serial/sh-sci.c2083
-rw-r--r--drivers/tty/serial/sh-sci.h468
-rw-r--r--drivers/tty/serial/sn_console.c1085
-rw-r--r--drivers/tty/serial/suncore.c247
-rw-r--r--drivers/tty/serial/suncore.h33
-rw-r--r--drivers/tty/serial/sunhv.c661
-rw-r--r--drivers/tty/serial/sunsab.c1152
-rw-r--r--drivers/tty/serial/sunsab.h322
-rw-r--r--drivers/tty/serial/sunsu.c1608
-rw-r--r--drivers/tty/serial/sunzilog.c1655
-rw-r--r--drivers/tty/serial/sunzilog.h289
-rw-r--r--drivers/tty/serial/timbuart.c531
-rw-r--r--drivers/tty/serial/timbuart.h58
-rw-r--r--drivers/tty/serial/uartlite.c654
-rw-r--r--drivers/tty/serial/ucc_uart.c1539
-rw-r--r--drivers/tty/serial/vr41xx_siu.c978
-rw-r--r--drivers/tty/serial/vt8500_serial.c646
-rw-r--r--drivers/tty/serial/xilinx_uartps.c1113
-rw-r--r--drivers/tty/serial/zs.c1304
-rw-r--r--drivers/tty/serial/zs.h284
-rw-r--r--drivers/tty/synclink.c8117
-rw-r--r--drivers/tty/synclink_gt.c5161
-rw-r--r--drivers/tty/synclinkmp.c5600
-rw-r--r--drivers/tty/sysrq.c905
-rw-r--r--drivers/tty/tty_audit.c360
-rw-r--r--drivers/tty/tty_buffer.c522
-rw-r--r--drivers/tty/tty_io.c3334
-rw-r--r--drivers/tty/tty_ioctl.c1181
-rw-r--r--drivers/tty/tty_ldisc.c978
-rw-r--r--drivers/tty/tty_mutex.c44
-rw-r--r--drivers/tty/tty_port.c446
-rw-r--r--drivers/tty/vt/.gitignore2
-rw-r--r--drivers/tty/vt/Makefile34
-rw-r--r--drivers/tty/vt/consolemap.c745
-rw-r--r--drivers/tty/vt/cp437.uni291
-rw-r--r--drivers/tty/vt/defkeymap.c_shipped262
-rw-r--r--drivers/tty/vt/defkeymap.map357
-rw-r--r--drivers/tty/vt/keyboard.c1453
-rw-r--r--drivers/tty/vt/selection.c345
-rw-r--r--drivers/tty/vt/vc_screen.c665
-rw-r--r--drivers/tty/vt/vt.c4226
-rw-r--r--drivers/tty/vt/vt_ioctl.c1800
194 files changed, 178776 insertions, 0 deletions
diff --git a/drivers/tty/Kconfig b/drivers/tty/Kconfig
new file mode 100644
index 000000000000..bd7cc0527999
--- /dev/null
+++ b/drivers/tty/Kconfig
@@ -0,0 +1,352 @@
1config VT
2 bool "Virtual terminal" if EXPERT
3 depends on !S390
4 select INPUT
5 default y
6 ---help---
7 If you say Y here, you will get support for terminal devices with
8 display and keyboard devices. These are called "virtual" because you
9 can run several virtual terminals (also called virtual consoles) on
10 one physical terminal. This is rather useful, for example one
11 virtual terminal can collect system messages and warnings, another
12 one can be used for a text-mode user session, and a third could run
13 an X session, all in parallel. Switching between virtual terminals
14 is done with certain key combinations, usually Alt-<function key>.
15
16 The setterm command ("man setterm") can be used to change the
17 properties (such as colors or beeping) of a virtual terminal. The
18 man page console_codes(4) ("man console_codes") contains the special
19 character sequences that can be used to change those properties
20 directly. The fonts used on virtual terminals can be changed with
21 the setfont ("man setfont") command and the key bindings are defined
22 with the loadkeys ("man loadkeys") command.
23
24 You need at least one virtual terminal device in order to make use
25 of your keyboard and monitor. Therefore, only people configuring an
26 embedded system would want to say N here in order to save some
27 memory; the only way to log into such a system is then via a serial
28 or network connection.
29
30 If unsure, say Y, or else you won't be able to do much with your new
31 shiny Linux system :-)
32
33config CONSOLE_TRANSLATIONS
34 depends on VT
35 default y
36 bool "Enable character translations in console" if EXPERT
37 ---help---
38 This enables support for font mapping and Unicode translation
39 on virtual consoles.
40
41config VT_CONSOLE
42 bool "Support for console on virtual terminal" if EXPERT
43 depends on VT
44 default y
45 ---help---
46 The system console is the device which receives all kernel messages
47 and warnings and which allows logins in single user mode. If you
48 answer Y here, a virtual terminal (the device used to interact with
49 a physical terminal) can be used as system console. This is the most
50 common mode of operations, so you should say Y here unless you want
51 the kernel messages be output only to a serial port (in which case
52 you should say Y to "Console on serial port", below).
53
54 If you do say Y here, by default the currently visible virtual
55 terminal (/dev/tty0) will be used as system console. You can change
56 that with a kernel command line option such as "console=tty3" which
57 would use the third virtual terminal as system console. (Try "man
58 bootparam" or see the documentation of your boot loader (lilo or
59 loadlin) about how to pass options to the kernel at boot time.)
60
61 If unsure, say Y.
62
63config HW_CONSOLE
64 bool
65 depends on VT && !S390 && !UML
66 default y
67
68config VT_HW_CONSOLE_BINDING
69 bool "Support for binding and unbinding console drivers"
70 depends on HW_CONSOLE
71 default n
72 ---help---
73 The virtual terminal is the device that interacts with the physical
74 terminal through console drivers. On these systems, at least one
75 console driver is loaded. In other configurations, additional console
76 drivers may be enabled, such as the framebuffer console. If more than
77 1 console driver is enabled, setting this to 'y' will allow you to
78 select the console driver that will serve as the backend for the
79 virtual terminals.
80
81 See <file:Documentation/console/console.txt> for more
82 information. For framebuffer console users, please refer to
83 <file:Documentation/fb/fbcon.txt>.
84
85config UNIX98_PTYS
86 bool "Unix98 PTY support" if EXPERT
87 default y
88 ---help---
89 A pseudo terminal (PTY) is a software device consisting of two
90 halves: a master and a slave. The slave device behaves identical to
91 a physical terminal; the master device is used by a process to
92 read data from and write data to the slave, thereby emulating a
93 terminal. Typical programs for the master side are telnet servers
94 and xterms.
95
96 Linux has traditionally used the BSD-like names /dev/ptyxx for
97 masters and /dev/ttyxx for slaves of pseudo terminals. This scheme
98 has a number of problems. The GNU C library glibc 2.1 and later,
99 however, supports the Unix98 naming standard: in order to acquire a
100 pseudo terminal, a process opens /dev/ptmx; the number of the pseudo
101 terminal is then made available to the process and the pseudo
102 terminal slave can be accessed as /dev/pts/<number>. What was
103 traditionally /dev/ttyp2 will then be /dev/pts/2, for example.
104
105 All modern Linux systems use the Unix98 ptys. Say Y unless
106 you're on an embedded system and want to conserve memory.
107
108config DEVPTS_MULTIPLE_INSTANCES
109 bool "Support multiple instances of devpts"
110 depends on UNIX98_PTYS
111 default n
112 ---help---
113 Enable support for multiple instances of devpts filesystem.
114 If you want to have isolated PTY namespaces (eg: in containers),
115 say Y here. Otherwise, say N. If enabled, each mount of devpts
116 filesystem with the '-o newinstance' option will create an
117 independent PTY namespace.
118
119config LEGACY_PTYS
120 bool "Legacy (BSD) PTY support"
121 default y
122 ---help---
123 A pseudo terminal (PTY) is a software device consisting of two
124 halves: a master and a slave. The slave device behaves identical to
125 a physical terminal; the master device is used by a process to
126 read data from and write data to the slave, thereby emulating a
127 terminal. Typical programs for the master side are telnet servers
128 and xterms.
129
130 Linux has traditionally used the BSD-like names /dev/ptyxx
131 for masters and /dev/ttyxx for slaves of pseudo
132 terminals. This scheme has a number of problems, including
133 security. This option enables these legacy devices; on most
134 systems, it is safe to say N.
135
136
137config LEGACY_PTY_COUNT
138 int "Maximum number of legacy PTY in use"
139 depends on LEGACY_PTYS
140 range 0 256
141 default "256"
142 ---help---
143 The maximum number of legacy PTYs that can be used at any one time.
144 The default is 256, and should be more than enough. Embedded
145 systems may want to reduce this to save memory.
146
147 When not in use, each legacy PTY occupies 12 bytes on 32-bit
148 architectures and 24 bytes on 64-bit architectures.
149
150config BFIN_JTAG_COMM
151 tristate "Blackfin JTAG Communication"
152 depends on BLACKFIN
153 help
154 Add support for emulating a TTY device over the Blackfin JTAG.
155
156 To compile this driver as a module, choose M here: the
157 module will be called bfin_jtag_comm.
158
159config BFIN_JTAG_COMM_CONSOLE
160 bool "Console on Blackfin JTAG"
161 depends on BFIN_JTAG_COMM=y
162
163config SERIAL_NONSTANDARD
164 bool "Non-standard serial port support"
165 depends on HAS_IOMEM
166 ---help---
167 Say Y here if you have any non-standard serial boards -- boards
168 which aren't supported using the standard "dumb" serial driver.
169 This includes intelligent serial boards such as Cyclades,
170 Digiboards, etc. These are usually used for systems that need many
171 serial ports because they serve many terminals or dial-in
172 connections.
173
174 Note that the answer to this question won't directly affect the
175 kernel: saying N will just cause the configurator to skip all
176 the questions about non-standard serial boards.
177
178 Most people can say N here.
179
180config ROCKETPORT
181 tristate "Comtrol RocketPort support"
182 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
183 help
184 This driver supports Comtrol RocketPort and RocketModem PCI boards.
185 These boards provide 2, 4, 8, 16, or 32 high-speed serial ports or
186 modems. For information about the RocketPort/RocketModem boards
187 and this driver read <file:Documentation/serial/rocket.txt>.
188
189 To compile this driver as a module, choose M here: the
190 module will be called rocket.
191
192 If you want to compile this driver into the kernel, say Y here. If
193 you don't have a Comtrol RocketPort/RocketModem card installed, say N.
194
195config CYCLADES
196 tristate "Cyclades async mux support"
197 depends on SERIAL_NONSTANDARD && (PCI || ISA)
198 select FW_LOADER
199 ---help---
200 This driver supports Cyclades Z and Y multiserial boards.
201 You would need something like this to connect more than two modems to
202 your Linux box, for instance in order to become a dial-in server.
203
204 For information about the Cyclades-Z card, read
205 <file:Documentation/serial/README.cycladesZ>.
206
207 To compile this driver as a module, choose M here: the
208 module will be called cyclades.
209
210 If you haven't heard about it, it's safe to say N.
211
212config CYZ_INTR
213 bool "Cyclades-Z interrupt mode operation (EXPERIMENTAL)"
214 depends on EXPERIMENTAL && CYCLADES
215 help
216 The Cyclades-Z family of multiport cards allows 2 (two) driver op
217 modes: polling and interrupt. In polling mode, the driver will check
218 the status of the Cyclades-Z ports every certain amount of time
219 (which is called polling cycle and is configurable). In interrupt
220 mode, it will use an interrupt line (IRQ) in order to check the
221 status of the Cyclades-Z ports. The default op mode is polling. If
222 unsure, say N.
223
224config MOXA_INTELLIO
225 tristate "Moxa Intellio support"
226 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
227 select FW_LOADER
228 help
229 Say Y here if you have a Moxa Intellio multiport serial card.
230
231 To compile this driver as a module, choose M here: the
232 module will be called moxa.
233
234config MOXA_SMARTIO
235 tristate "Moxa SmartIO support v. 2.0"
236 depends on SERIAL_NONSTANDARD && (PCI || EISA || ISA)
237 help
238 Say Y here if you have a Moxa SmartIO multiport serial card and/or
239 want to help develop a new version of this driver.
240
241 This is upgraded (1.9.1) driver from original Moxa drivers with
242 changes finally resulting in PCI probing.
243
244 This driver can also be built as a module. The module will be called
245 mxser. If you want to do that, say M here.
246
247config SYNCLINK
248 tristate "Microgate SyncLink card support"
249 depends on SERIAL_NONSTANDARD && PCI && ISA_DMA_API
250 help
251 Provides support for the SyncLink ISA and PCI multiprotocol serial
252 adapters. These adapters support asynchronous and HDLC bit
253 synchronous communication up to 10Mbps (PCI adapter).
254
255 This driver can only be built as a module ( = code which can be
256 inserted in and removed from the running kernel whenever you want).
257 The module will be called synclink. If you want to do that, say M
258 here.
259
260config SYNCLINKMP
261 tristate "SyncLink Multiport support"
262 depends on SERIAL_NONSTANDARD && PCI
263 help
264 Enable support for the SyncLink Multiport (2 or 4 ports)
265 serial adapter, running asynchronous and HDLC communications up
266 to 2.048Mbps. Each ports is independently selectable for
267 RS-232, V.35, RS-449, RS-530, and X.21
268
269 This driver may be built as a module ( = code which can be
270 inserted in and removed from the running kernel whenever you want).
271 The module will be called synclinkmp. If you want to do that, say M
272 here.
273
274config SYNCLINK_GT
275 tristate "SyncLink GT/AC support"
276 depends on SERIAL_NONSTANDARD && PCI
277 help
278 Support for SyncLink GT and SyncLink AC families of
279 synchronous and asynchronous serial adapters
280 manufactured by Microgate Systems, Ltd. (www.microgate.com)
281
282config NOZOMI
283 tristate "HSDPA Broadband Wireless Data Card - Globe Trotter"
284 depends on PCI && EXPERIMENTAL
285 help
286 If you have a HSDPA driver Broadband Wireless Data Card -
287 Globe Trotter PCMCIA card, say Y here.
288
289 To compile this driver as a module, choose M here, the module
290 will be called nozomi.
291
292config ISI
293 tristate "Multi-Tech multiport card support (EXPERIMENTAL)"
294 depends on SERIAL_NONSTANDARD && PCI
295 select FW_LOADER
296 help
297 This is a driver for the Multi-Tech cards which provide several
298 serial ports. The driver is experimental and can currently only be
299 built as a module. The module will be called isicom.
300 If you want to do that, choose M here.
301
302config N_HDLC
303 tristate "HDLC line discipline support"
304 depends on SERIAL_NONSTANDARD
305 help
306 Allows synchronous HDLC communications with tty device drivers that
307 support synchronous HDLC such as the Microgate SyncLink adapter.
308
309 This driver can be built as a module ( = code which can be
310 inserted in and removed from the running kernel whenever you want).
311 The module will be called n_hdlc. If you want to do that, say M
312 here.
313
314config N_GSM
315 tristate "GSM MUX line discipline support (EXPERIMENTAL)"
316 depends on EXPERIMENTAL
317 depends on NET
318 help
319 This line discipline provides support for the GSM MUX protocol and
320 presents the mux as a set of 61 individual tty devices.
321
322config TRACE_ROUTER
323 tristate "Trace data router for MIPI P1149.7 cJTAG standard"
324 depends on TRACE_SINK
325 default n
326 help
327 The trace router uses the Linux tty line discipline framework to
328 route trace data coming from a tty port (say UART for example) to
329 the trace sink line discipline driver and to another tty port (say
330 USB). This is part of a solution for the MIPI P1149.7, compact JTAG,
331 standard, which is for debugging mobile devices. The PTI driver in
332 drivers/misc/pti.c defines the majority of this MIPI solution.
333
334 You should select this driver if the target kernel is meant for
335 a mobile device containing a modem. Then you will need to select
336 "Trace data sink for MIPI P1149.7 cJTAG standard" line discipline
337 driver.
338
339config TRACE_SINK
340 tristate "Trace data sink for MIPI P1149.7 cJTAG standard"
341 default n
342 help
343 The trace sink uses the Linux line discipline framework to receive
344 trace data coming from the trace router line discipline driver
345 to a user-defined tty port target, like USB.
346 This is to provide a way to extract modem trace data on
347 devices that do not have a PTI HW module, or just need modem
348 trace data to come out of a different HW output port.
349 This is part of a solution for the P1149.7, compact JTAG, standard.
350
351 If you select this option, you need to select
352 "Trace data router for MIPI P1149.7 cJTAG standard".
diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile
new file mode 100644
index 000000000000..ea89b0bd15fe
--- /dev/null
+++ b/drivers/tty/Makefile
@@ -0,0 +1,30 @@
1obj-y += tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o \
2 tty_buffer.o tty_port.o tty_mutex.o
3obj-$(CONFIG_LEGACY_PTYS) += pty.o
4obj-$(CONFIG_UNIX98_PTYS) += pty.o
5obj-$(CONFIG_AUDIT) += tty_audit.o
6obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o
7obj-$(CONFIG_N_HDLC) += n_hdlc.o
8obj-$(CONFIG_N_GSM) += n_gsm.o
9obj-$(CONFIG_TRACE_ROUTER) += n_tracerouter.o
10obj-$(CONFIG_TRACE_SINK) += n_tracesink.o
11obj-$(CONFIG_R3964) += n_r3964.o
12
13obj-y += vt/
14obj-$(CONFIG_HVC_DRIVER) += hvc/
15obj-y += serial/
16
17# tty drivers
18obj-$(CONFIG_AMIGA_BUILTIN_SERIAL) += amiserial.o
19obj-$(CONFIG_BFIN_JTAG_COMM) += bfin_jtag_comm.o
20obj-$(CONFIG_CYCLADES) += cyclades.o
21obj-$(CONFIG_ISI) += isicom.o
22obj-$(CONFIG_MOXA_INTELLIO) += moxa.o
23obj-$(CONFIG_MOXA_SMARTIO) += mxser.o
24obj-$(CONFIG_NOZOMI) += nozomi.o
25obj-$(CONFIG_ROCKETPORT) += rocket.o
26obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o
27obj-$(CONFIG_SYNCLINKMP) += synclinkmp.o
28obj-$(CONFIG_SYNCLINK) += synclink.o
29
30obj-y += ipwireless/
diff --git a/drivers/tty/amiserial.c b/drivers/tty/amiserial.c
new file mode 100644
index 000000000000..220579592c20
--- /dev/null
+++ b/drivers/tty/amiserial.c
@@ -0,0 +1,2176 @@
1/*
2 * Serial driver for the amiga builtin port.
3 *
4 * This code was created by taking serial.c version 4.30 from kernel
5 * release 2.3.22, replacing all hardware related stuff with the
6 * corresponding amiga hardware actions, and removing all irrelevant
7 * code. As a consequence, it uses many of the constants and names
8 * associated with the registers and bits of 16550 compatible UARTS -
9 * but only to keep track of status, etc in the state variables. It
10 * was done this was to make it easier to keep the code in line with
11 * (non hardware specific) changes to serial.c.
12 *
13 * The port is registered with the tty driver as minor device 64, and
14 * therefore other ports should should only use 65 upwards.
15 *
16 * Richard Lucock 28/12/99
17 *
18 * Copyright (C) 1991, 1992 Linus Torvalds
19 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
20 * 1998, 1999 Theodore Ts'o
21 *
22 */
23
24/*
25 * Serial driver configuration section. Here are the various options:
26 *
27 * SERIAL_PARANOIA_CHECK
28 * Check the magic number for the async_structure where
29 * ever possible.
30 */
31
32#include <linux/delay.h>
33
34#undef SERIAL_PARANOIA_CHECK
35#define SERIAL_DO_RESTART
36
37/* Set of debugging defines */
38
39#undef SERIAL_DEBUG_INTR
40#undef SERIAL_DEBUG_OPEN
41#undef SERIAL_DEBUG_FLOW
42#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
43
44/* Sanity checks */
45
46#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
47#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
48 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
49#else
50#define DBG_CNT(s)
51#endif
52
53/*
54 * End of serial driver configuration section.
55 */
56
57#include <linux/module.h>
58
59#include <linux/types.h>
60#include <linux/serial.h>
61#include <linux/serialP.h>
62#include <linux/serial_reg.h>
63static char *serial_version = "4.30";
64
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/kernel.h>
69#include <linux/timer.h>
70#include <linux/interrupt.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/console.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
80#include <linux/seq_file.h>
81#include <linux/slab.h>
82#include <linux/init.h>
83#include <linux/bitops.h>
84#include <linux/platform_device.h>
85
86#include <asm/setup.h>
87
88#include <asm/system.h>
89
90#include <asm/irq.h>
91
92#include <asm/amigahw.h>
93#include <asm/amigaints.h>
94
95#define custom amiga_custom
96static char *serial_name = "Amiga-builtin serial driver";
97
98static struct tty_driver *serial_driver;
99
100/* number of characters left in xmit buffer before we ask for more */
101#define WAKEUP_CHARS 256
102
103static struct async_struct *IRQ_ports;
104
105static unsigned char current_ctl_bits;
106
107static void change_speed(struct async_struct *info, struct ktermios *old);
108static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
109
110
111static struct serial_state rs_table[1];
112
113#define NR_PORTS ARRAY_SIZE(rs_table)
114
115#include <asm/uaccess.h>
116
117#define serial_isroot() (capable(CAP_SYS_ADMIN))
118
119
120static inline int serial_paranoia_check(struct async_struct *info,
121 char *name, const char *routine)
122{
123#ifdef SERIAL_PARANOIA_CHECK
124 static const char *badmagic =
125 "Warning: bad magic number for serial struct (%s) in %s\n";
126 static const char *badinfo =
127 "Warning: null async_struct for (%s) in %s\n";
128
129 if (!info) {
130 printk(badinfo, name, routine);
131 return 1;
132 }
133 if (info->magic != SERIAL_MAGIC) {
134 printk(badmagic, name, routine);
135 return 1;
136 }
137#endif
138 return 0;
139}
140
141/* some serial hardware definitions */
142#define SDR_OVRUN (1<<15)
143#define SDR_RBF (1<<14)
144#define SDR_TBE (1<<13)
145#define SDR_TSRE (1<<12)
146
147#define SERPER_PARENB (1<<15)
148
149#define AC_SETCLR (1<<15)
150#define AC_UARTBRK (1<<11)
151
152#define SER_DTR (1<<7)
153#define SER_RTS (1<<6)
154#define SER_DCD (1<<5)
155#define SER_CTS (1<<4)
156#define SER_DSR (1<<3)
157
158static __inline__ void rtsdtr_ctrl(int bits)
159{
160 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
161}
162
163/*
164 * ------------------------------------------------------------
165 * rs_stop() and rs_start()
166 *
167 * This routines are called before setting or resetting tty->stopped.
168 * They enable or disable transmitter interrupts, as necessary.
169 * ------------------------------------------------------------
170 */
171static void rs_stop(struct tty_struct *tty)
172{
173 struct async_struct *info = tty->driver_data;
174 unsigned long flags;
175
176 if (serial_paranoia_check(info, tty->name, "rs_stop"))
177 return;
178
179 local_irq_save(flags);
180 if (info->IER & UART_IER_THRI) {
181 info->IER &= ~UART_IER_THRI;
182 /* disable Tx interrupt and remove any pending interrupts */
183 custom.intena = IF_TBE;
184 mb();
185 custom.intreq = IF_TBE;
186 mb();
187 }
188 local_irq_restore(flags);
189}
190
191static void rs_start(struct tty_struct *tty)
192{
193 struct async_struct *info = tty->driver_data;
194 unsigned long flags;
195
196 if (serial_paranoia_check(info, tty->name, "rs_start"))
197 return;
198
199 local_irq_save(flags);
200 if (info->xmit.head != info->xmit.tail
201 && info->xmit.buf
202 && !(info->IER & UART_IER_THRI)) {
203 info->IER |= UART_IER_THRI;
204 custom.intena = IF_SETCLR | IF_TBE;
205 mb();
206 /* set a pending Tx Interrupt, transmitter should restart now */
207 custom.intreq = IF_SETCLR | IF_TBE;
208 mb();
209 }
210 local_irq_restore(flags);
211}
212
213/*
214 * ----------------------------------------------------------------------
215 *
216 * Here starts the interrupt handling routines. All of the following
217 * subroutines are declared as inline and are folded into
218 * rs_interrupt(). They were separated out for readability's sake.
219 *
220 * Note: rs_interrupt() is a "fast" interrupt, which means that it
221 * runs with interrupts turned off. People who may want to modify
222 * rs_interrupt() should try to keep the interrupt handler as fast as
223 * possible. After you are done making modifications, it is not a bad
224 * idea to do:
225 *
226 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
227 *
228 * and look at the resulting assemble code in serial.s.
229 *
230 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
231 * -----------------------------------------------------------------------
232 */
233
234/*
235 * This routine is used by the interrupt handler to schedule
236 * processing in the software interrupt portion of the driver.
237 */
238static void rs_sched_event(struct async_struct *info,
239 int event)
240{
241 info->event |= 1 << event;
242 tasklet_schedule(&info->tlet);
243}
244
245static void receive_chars(struct async_struct *info)
246{
247 int status;
248 int serdatr;
249 struct tty_struct *tty = info->tty;
250 unsigned char ch, flag;
251 struct async_icount *icount;
252 int oe = 0;
253
254 icount = &info->state->icount;
255
256 status = UART_LSR_DR; /* We obviously have a character! */
257 serdatr = custom.serdatr;
258 mb();
259 custom.intreq = IF_RBF;
260 mb();
261
262 if((serdatr & 0x1ff) == 0)
263 status |= UART_LSR_BI;
264 if(serdatr & SDR_OVRUN)
265 status |= UART_LSR_OE;
266
267 ch = serdatr & 0xff;
268 icount->rx++;
269
270#ifdef SERIAL_DEBUG_INTR
271 printk("DR%02x:%02x...", ch, status);
272#endif
273 flag = TTY_NORMAL;
274
275 /*
276 * We don't handle parity or frame errors - but I have left
277 * the code in, since I'm not sure that the errors can't be
278 * detected.
279 */
280
281 if (status & (UART_LSR_BI | UART_LSR_PE |
282 UART_LSR_FE | UART_LSR_OE)) {
283 /*
284 * For statistics only
285 */
286 if (status & UART_LSR_BI) {
287 status &= ~(UART_LSR_FE | UART_LSR_PE);
288 icount->brk++;
289 } else if (status & UART_LSR_PE)
290 icount->parity++;
291 else if (status & UART_LSR_FE)
292 icount->frame++;
293 if (status & UART_LSR_OE)
294 icount->overrun++;
295
296 /*
297 * Now check to see if character should be
298 * ignored, and mask off conditions which
299 * should be ignored.
300 */
301 if (status & info->ignore_status_mask)
302 goto out;
303
304 status &= info->read_status_mask;
305
306 if (status & (UART_LSR_BI)) {
307#ifdef SERIAL_DEBUG_INTR
308 printk("handling break....");
309#endif
310 flag = TTY_BREAK;
311 if (info->flags & ASYNC_SAK)
312 do_SAK(tty);
313 } else if (status & UART_LSR_PE)
314 flag = TTY_PARITY;
315 else if (status & UART_LSR_FE)
316 flag = TTY_FRAME;
317 if (status & UART_LSR_OE) {
318 /*
319 * Overrun is special, since it's
320 * reported immediately, and doesn't
321 * affect the current character
322 */
323 oe = 1;
324 }
325 }
326 tty_insert_flip_char(tty, ch, flag);
327 if (oe == 1)
328 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
329 tty_flip_buffer_push(tty);
330out:
331 return;
332}
333
334static void transmit_chars(struct async_struct *info)
335{
336 custom.intreq = IF_TBE;
337 mb();
338 if (info->x_char) {
339 custom.serdat = info->x_char | 0x100;
340 mb();
341 info->state->icount.tx++;
342 info->x_char = 0;
343 return;
344 }
345 if (info->xmit.head == info->xmit.tail
346 || info->tty->stopped
347 || info->tty->hw_stopped) {
348 info->IER &= ~UART_IER_THRI;
349 custom.intena = IF_TBE;
350 mb();
351 return;
352 }
353
354 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
355 mb();
356 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
357 info->state->icount.tx++;
358
359 if (CIRC_CNT(info->xmit.head,
360 info->xmit.tail,
361 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
362 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
363
364#ifdef SERIAL_DEBUG_INTR
365 printk("THRE...");
366#endif
367 if (info->xmit.head == info->xmit.tail) {
368 custom.intena = IF_TBE;
369 mb();
370 info->IER &= ~UART_IER_THRI;
371 }
372}
373
374static void check_modem_status(struct async_struct *info)
375{
376 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
377 unsigned char dstatus;
378 struct async_icount *icount;
379
380 /* Determine bits that have changed */
381 dstatus = status ^ current_ctl_bits;
382 current_ctl_bits = status;
383
384 if (dstatus) {
385 icount = &info->state->icount;
386 /* update input line counters */
387 if (dstatus & SER_DSR)
388 icount->dsr++;
389 if (dstatus & SER_DCD) {
390 icount->dcd++;
391#ifdef CONFIG_HARD_PPS
392 if ((info->flags & ASYNC_HARDPPS_CD) &&
393 !(status & SER_DCD))
394 hardpps();
395#endif
396 }
397 if (dstatus & SER_CTS)
398 icount->cts++;
399 wake_up_interruptible(&info->delta_msr_wait);
400 }
401
402 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
403#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
404 printk("ttyS%d CD now %s...", info->line,
405 (!(status & SER_DCD)) ? "on" : "off");
406#endif
407 if (!(status & SER_DCD))
408 wake_up_interruptible(&info->open_wait);
409 else {
410#ifdef SERIAL_DEBUG_OPEN
411 printk("doing serial hangup...");
412#endif
413 if (info->tty)
414 tty_hangup(info->tty);
415 }
416 }
417 if (info->flags & ASYNC_CTS_FLOW) {
418 if (info->tty->hw_stopped) {
419 if (!(status & SER_CTS)) {
420#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
421 printk("CTS tx start...");
422#endif
423 info->tty->hw_stopped = 0;
424 info->IER |= UART_IER_THRI;
425 custom.intena = IF_SETCLR | IF_TBE;
426 mb();
427 /* set a pending Tx Interrupt, transmitter should restart now */
428 custom.intreq = IF_SETCLR | IF_TBE;
429 mb();
430 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
431 return;
432 }
433 } else {
434 if ((status & SER_CTS)) {
435#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
436 printk("CTS tx stop...");
437#endif
438 info->tty->hw_stopped = 1;
439 info->IER &= ~UART_IER_THRI;
440 /* disable Tx interrupt and remove any pending interrupts */
441 custom.intena = IF_TBE;
442 mb();
443 custom.intreq = IF_TBE;
444 mb();
445 }
446 }
447 }
448}
449
450static irqreturn_t ser_vbl_int( int irq, void *data)
451{
452 /* vbl is just a periodic interrupt we tie into to update modem status */
453 struct async_struct * info = IRQ_ports;
454 /*
455 * TBD - is it better to unregister from this interrupt or to
456 * ignore it if MSI is clear ?
457 */
458 if(info->IER & UART_IER_MSI)
459 check_modem_status(info);
460 return IRQ_HANDLED;
461}
462
463static irqreturn_t ser_rx_int(int irq, void *dev_id)
464{
465 struct async_struct * info;
466
467#ifdef SERIAL_DEBUG_INTR
468 printk("ser_rx_int...");
469#endif
470
471 info = IRQ_ports;
472 if (!info || !info->tty)
473 return IRQ_NONE;
474
475 receive_chars(info);
476 info->last_active = jiffies;
477#ifdef SERIAL_DEBUG_INTR
478 printk("end.\n");
479#endif
480 return IRQ_HANDLED;
481}
482
483static irqreturn_t ser_tx_int(int irq, void *dev_id)
484{
485 struct async_struct * info;
486
487 if (custom.serdatr & SDR_TBE) {
488#ifdef SERIAL_DEBUG_INTR
489 printk("ser_tx_int...");
490#endif
491
492 info = IRQ_ports;
493 if (!info || !info->tty)
494 return IRQ_NONE;
495
496 transmit_chars(info);
497 info->last_active = jiffies;
498#ifdef SERIAL_DEBUG_INTR
499 printk("end.\n");
500#endif
501 }
502 return IRQ_HANDLED;
503}
504
505/*
506 * -------------------------------------------------------------------
507 * Here ends the serial interrupt routines.
508 * -------------------------------------------------------------------
509 */
510
511/*
512 * This routine is used to handle the "bottom half" processing for the
513 * serial driver, known also the "software interrupt" processing.
514 * This processing is done at the kernel interrupt level, after the
515 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
516 * is where time-consuming activities which can not be done in the
517 * interrupt driver proper are done; the interrupt driver schedules
518 * them using rs_sched_event(), and they get done here.
519 */
520
521static void do_softint(unsigned long private_)
522{
523 struct async_struct *info = (struct async_struct *) private_;
524 struct tty_struct *tty;
525
526 tty = info->tty;
527 if (!tty)
528 return;
529
530 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
531 tty_wakeup(tty);
532}
533
534/*
535 * ---------------------------------------------------------------
536 * Low level utility subroutines for the serial driver: routines to
537 * figure out the appropriate timeout for an interrupt chain, routines
538 * to initialize and startup a serial port, and routines to shutdown a
539 * serial port. Useful stuff like that.
540 * ---------------------------------------------------------------
541 */
542
543static int startup(struct async_struct * info)
544{
545 unsigned long flags;
546 int retval=0;
547 unsigned long page;
548
549 page = get_zeroed_page(GFP_KERNEL);
550 if (!page)
551 return -ENOMEM;
552
553 local_irq_save(flags);
554
555 if (info->flags & ASYNC_INITIALIZED) {
556 free_page(page);
557 goto errout;
558 }
559
560 if (info->xmit.buf)
561 free_page(page);
562 else
563 info->xmit.buf = (unsigned char *) page;
564
565#ifdef SERIAL_DEBUG_OPEN
566 printk("starting up ttys%d ...", info->line);
567#endif
568
569 /* Clear anything in the input buffer */
570
571 custom.intreq = IF_RBF;
572 mb();
573
574 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
575 if (retval) {
576 if (serial_isroot()) {
577 if (info->tty)
578 set_bit(TTY_IO_ERROR,
579 &info->tty->flags);
580 retval = 0;
581 }
582 goto errout;
583 }
584
585 /* enable both Rx and Tx interrupts */
586 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
587 mb();
588 info->IER = UART_IER_MSI;
589
590 /* remember current state of the DCD and CTS bits */
591 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
592
593 IRQ_ports = info;
594
595 info->MCR = 0;
596 if (info->tty->termios->c_cflag & CBAUD)
597 info->MCR = SER_DTR | SER_RTS;
598 rtsdtr_ctrl(info->MCR);
599
600 if (info->tty)
601 clear_bit(TTY_IO_ERROR, &info->tty->flags);
602 info->xmit.head = info->xmit.tail = 0;
603
604 /*
605 * Set up the tty->alt_speed kludge
606 */
607 if (info->tty) {
608 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
609 info->tty->alt_speed = 57600;
610 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
611 info->tty->alt_speed = 115200;
612 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
613 info->tty->alt_speed = 230400;
614 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
615 info->tty->alt_speed = 460800;
616 }
617
618 /*
619 * and set the speed of the serial port
620 */
621 change_speed(info, NULL);
622
623 info->flags |= ASYNC_INITIALIZED;
624 local_irq_restore(flags);
625 return 0;
626
627errout:
628 local_irq_restore(flags);
629 return retval;
630}
631
632/*
633 * This routine will shutdown a serial port; interrupts are disabled, and
634 * DTR is dropped if the hangup on close termio flag is on.
635 */
636static void shutdown(struct async_struct * info)
637{
638 unsigned long flags;
639 struct serial_state *state;
640
641 if (!(info->flags & ASYNC_INITIALIZED))
642 return;
643
644 state = info->state;
645
646#ifdef SERIAL_DEBUG_OPEN
647 printk("Shutting down serial port %d ....\n", info->line);
648#endif
649
650 local_irq_save(flags); /* Disable interrupts */
651
652 /*
653 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
654 * here so the queue might never be waken up
655 */
656 wake_up_interruptible(&info->delta_msr_wait);
657
658 IRQ_ports = NULL;
659
660 /*
661 * Free the IRQ, if necessary
662 */
663 free_irq(IRQ_AMIGA_VERTB, info);
664
665 if (info->xmit.buf) {
666 free_page((unsigned long) info->xmit.buf);
667 info->xmit.buf = NULL;
668 }
669
670 info->IER = 0;
671 custom.intena = IF_RBF | IF_TBE;
672 mb();
673
674 /* disable break condition */
675 custom.adkcon = AC_UARTBRK;
676 mb();
677
678 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
679 info->MCR &= ~(SER_DTR|SER_RTS);
680 rtsdtr_ctrl(info->MCR);
681
682 if (info->tty)
683 set_bit(TTY_IO_ERROR, &info->tty->flags);
684
685 info->flags &= ~ASYNC_INITIALIZED;
686 local_irq_restore(flags);
687}
688
689
690/*
691 * This routine is called to set the UART divisor registers to match
692 * the specified baud rate for a serial port.
693 */
694static void change_speed(struct async_struct *info,
695 struct ktermios *old_termios)
696{
697 int quot = 0, baud_base, baud;
698 unsigned cflag, cval = 0;
699 int bits;
700 unsigned long flags;
701
702 if (!info->tty || !info->tty->termios)
703 return;
704 cflag = info->tty->termios->c_cflag;
705
706 /* Byte size is always 8 bits plus parity bit if requested */
707
708 cval = 3; bits = 10;
709 if (cflag & CSTOPB) {
710 cval |= 0x04;
711 bits++;
712 }
713 if (cflag & PARENB) {
714 cval |= UART_LCR_PARITY;
715 bits++;
716 }
717 if (!(cflag & PARODD))
718 cval |= UART_LCR_EPAR;
719#ifdef CMSPAR
720 if (cflag & CMSPAR)
721 cval |= UART_LCR_SPAR;
722#endif
723
724 /* Determine divisor based on baud rate */
725 baud = tty_get_baud_rate(info->tty);
726 if (!baud)
727 baud = 9600; /* B0 transition handled in rs_set_termios */
728 baud_base = info->state->baud_base;
729 if (baud == 38400 &&
730 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
731 quot = info->state->custom_divisor;
732 else {
733 if (baud == 134)
734 /* Special case since 134 is really 134.5 */
735 quot = (2*baud_base / 269);
736 else if (baud)
737 quot = baud_base / baud;
738 }
739 /* If the quotient is zero refuse the change */
740 if (!quot && old_termios) {
741 /* FIXME: Will need updating for new tty in the end */
742 info->tty->termios->c_cflag &= ~CBAUD;
743 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
744 baud = tty_get_baud_rate(info->tty);
745 if (!baud)
746 baud = 9600;
747 if (baud == 38400 &&
748 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
749 quot = info->state->custom_divisor;
750 else {
751 if (baud == 134)
752 /* Special case since 134 is really 134.5 */
753 quot = (2*baud_base / 269);
754 else if (baud)
755 quot = baud_base / baud;
756 }
757 }
758 /* As a last resort, if the quotient is zero, default to 9600 bps */
759 if (!quot)
760 quot = baud_base / 9600;
761 info->quot = quot;
762 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
763 info->timeout += HZ/50; /* Add .02 seconds of slop */
764
765 /* CTS flow control flag and modem status interrupts */
766 info->IER &= ~UART_IER_MSI;
767 if (info->flags & ASYNC_HARDPPS_CD)
768 info->IER |= UART_IER_MSI;
769 if (cflag & CRTSCTS) {
770 info->flags |= ASYNC_CTS_FLOW;
771 info->IER |= UART_IER_MSI;
772 } else
773 info->flags &= ~ASYNC_CTS_FLOW;
774 if (cflag & CLOCAL)
775 info->flags &= ~ASYNC_CHECK_CD;
776 else {
777 info->flags |= ASYNC_CHECK_CD;
778 info->IER |= UART_IER_MSI;
779 }
780 /* TBD:
781 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
782 */
783
784 /*
785 * Set up parity check flag
786 */
787
788 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
789 if (I_INPCK(info->tty))
790 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
791 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
792 info->read_status_mask |= UART_LSR_BI;
793
794 /*
795 * Characters to ignore
796 */
797 info->ignore_status_mask = 0;
798 if (I_IGNPAR(info->tty))
799 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
800 if (I_IGNBRK(info->tty)) {
801 info->ignore_status_mask |= UART_LSR_BI;
802 /*
803 * If we're ignore parity and break indicators, ignore
804 * overruns too. (For real raw support).
805 */
806 if (I_IGNPAR(info->tty))
807 info->ignore_status_mask |= UART_LSR_OE;
808 }
809 /*
810 * !!! ignore all characters if CREAD is not set
811 */
812 if ((cflag & CREAD) == 0)
813 info->ignore_status_mask |= UART_LSR_DR;
814 local_irq_save(flags);
815
816 {
817 short serper;
818
819 /* Set up the baud rate */
820 serper = quot - 1;
821
822 /* Enable or disable parity bit */
823
824 if(cval & UART_LCR_PARITY)
825 serper |= (SERPER_PARENB);
826
827 custom.serper = serper;
828 mb();
829 }
830
831 info->LCR = cval; /* Save LCR */
832 local_irq_restore(flags);
833}
834
835static int rs_put_char(struct tty_struct *tty, unsigned char ch)
836{
837 struct async_struct *info;
838 unsigned long flags;
839
840 info = tty->driver_data;
841
842 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
843 return 0;
844
845 if (!info->xmit.buf)
846 return 0;
847
848 local_irq_save(flags);
849 if (CIRC_SPACE(info->xmit.head,
850 info->xmit.tail,
851 SERIAL_XMIT_SIZE) == 0) {
852 local_irq_restore(flags);
853 return 0;
854 }
855
856 info->xmit.buf[info->xmit.head++] = ch;
857 info->xmit.head &= SERIAL_XMIT_SIZE-1;
858 local_irq_restore(flags);
859 return 1;
860}
861
862static void rs_flush_chars(struct tty_struct *tty)
863{
864 struct async_struct *info = tty->driver_data;
865 unsigned long flags;
866
867 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
868 return;
869
870 if (info->xmit.head == info->xmit.tail
871 || tty->stopped
872 || tty->hw_stopped
873 || !info->xmit.buf)
874 return;
875
876 local_irq_save(flags);
877 info->IER |= UART_IER_THRI;
878 custom.intena = IF_SETCLR | IF_TBE;
879 mb();
880 /* set a pending Tx Interrupt, transmitter should restart now */
881 custom.intreq = IF_SETCLR | IF_TBE;
882 mb();
883 local_irq_restore(flags);
884}
885
886static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
887{
888 int c, ret = 0;
889 struct async_struct *info;
890 unsigned long flags;
891
892 info = tty->driver_data;
893
894 if (serial_paranoia_check(info, tty->name, "rs_write"))
895 return 0;
896
897 if (!info->xmit.buf)
898 return 0;
899
900 local_irq_save(flags);
901 while (1) {
902 c = CIRC_SPACE_TO_END(info->xmit.head,
903 info->xmit.tail,
904 SERIAL_XMIT_SIZE);
905 if (count < c)
906 c = count;
907 if (c <= 0) {
908 break;
909 }
910 memcpy(info->xmit.buf + info->xmit.head, buf, c);
911 info->xmit.head = ((info->xmit.head + c) &
912 (SERIAL_XMIT_SIZE-1));
913 buf += c;
914 count -= c;
915 ret += c;
916 }
917 local_irq_restore(flags);
918
919 if (info->xmit.head != info->xmit.tail
920 && !tty->stopped
921 && !tty->hw_stopped
922 && !(info->IER & UART_IER_THRI)) {
923 info->IER |= UART_IER_THRI;
924 local_irq_disable();
925 custom.intena = IF_SETCLR | IF_TBE;
926 mb();
927 /* set a pending Tx Interrupt, transmitter should restart now */
928 custom.intreq = IF_SETCLR | IF_TBE;
929 mb();
930 local_irq_restore(flags);
931 }
932 return ret;
933}
934
935static int rs_write_room(struct tty_struct *tty)
936{
937 struct async_struct *info = tty->driver_data;
938
939 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
940 return 0;
941 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
942}
943
944static int rs_chars_in_buffer(struct tty_struct *tty)
945{
946 struct async_struct *info = tty->driver_data;
947
948 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
949 return 0;
950 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
951}
952
953static void rs_flush_buffer(struct tty_struct *tty)
954{
955 struct async_struct *info = tty->driver_data;
956 unsigned long flags;
957
958 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
959 return;
960 local_irq_save(flags);
961 info->xmit.head = info->xmit.tail = 0;
962 local_irq_restore(flags);
963 tty_wakeup(tty);
964}
965
966/*
967 * This function is used to send a high-priority XON/XOFF character to
968 * the device
969 */
970static void rs_send_xchar(struct tty_struct *tty, char ch)
971{
972 struct async_struct *info = tty->driver_data;
973 unsigned long flags;
974
975 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
976 return;
977
978 info->x_char = ch;
979 if (ch) {
980 /* Make sure transmit interrupts are on */
981
982 /* Check this ! */
983 local_irq_save(flags);
984 if(!(custom.intenar & IF_TBE)) {
985 custom.intena = IF_SETCLR | IF_TBE;
986 mb();
987 /* set a pending Tx Interrupt, transmitter should restart now */
988 custom.intreq = IF_SETCLR | IF_TBE;
989 mb();
990 }
991 local_irq_restore(flags);
992
993 info->IER |= UART_IER_THRI;
994 }
995}
996
997/*
998 * ------------------------------------------------------------
999 * rs_throttle()
1000 *
1001 * This routine is called by the upper-layer tty layer to signal that
1002 * incoming characters should be throttled.
1003 * ------------------------------------------------------------
1004 */
1005static void rs_throttle(struct tty_struct * tty)
1006{
1007 struct async_struct *info = tty->driver_data;
1008 unsigned long flags;
1009#ifdef SERIAL_DEBUG_THROTTLE
1010 char buf[64];
1011
1012 printk("throttle %s: %d....\n", tty_name(tty, buf),
1013 tty->ldisc.chars_in_buffer(tty));
1014#endif
1015
1016 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1017 return;
1018
1019 if (I_IXOFF(tty))
1020 rs_send_xchar(tty, STOP_CHAR(tty));
1021
1022 if (tty->termios->c_cflag & CRTSCTS)
1023 info->MCR &= ~SER_RTS;
1024
1025 local_irq_save(flags);
1026 rtsdtr_ctrl(info->MCR);
1027 local_irq_restore(flags);
1028}
1029
1030static void rs_unthrottle(struct tty_struct * tty)
1031{
1032 struct async_struct *info = tty->driver_data;
1033 unsigned long flags;
1034#ifdef SERIAL_DEBUG_THROTTLE
1035 char buf[64];
1036
1037 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1038 tty->ldisc.chars_in_buffer(tty));
1039#endif
1040
1041 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1042 return;
1043
1044 if (I_IXOFF(tty)) {
1045 if (info->x_char)
1046 info->x_char = 0;
1047 else
1048 rs_send_xchar(tty, START_CHAR(tty));
1049 }
1050 if (tty->termios->c_cflag & CRTSCTS)
1051 info->MCR |= SER_RTS;
1052 local_irq_save(flags);
1053 rtsdtr_ctrl(info->MCR);
1054 local_irq_restore(flags);
1055}
1056
1057/*
1058 * ------------------------------------------------------------
1059 * rs_ioctl() and friends
1060 * ------------------------------------------------------------
1061 */
1062
1063static int get_serial_info(struct async_struct * info,
1064 struct serial_struct __user * retinfo)
1065{
1066 struct serial_struct tmp;
1067 struct serial_state *state = info->state;
1068
1069 if (!retinfo)
1070 return -EFAULT;
1071 memset(&tmp, 0, sizeof(tmp));
1072 tty_lock();
1073 tmp.type = state->type;
1074 tmp.line = state->line;
1075 tmp.port = state->port;
1076 tmp.irq = state->irq;
1077 tmp.flags = state->flags;
1078 tmp.xmit_fifo_size = state->xmit_fifo_size;
1079 tmp.baud_base = state->baud_base;
1080 tmp.close_delay = state->close_delay;
1081 tmp.closing_wait = state->closing_wait;
1082 tmp.custom_divisor = state->custom_divisor;
1083 tty_unlock();
1084 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1085 return -EFAULT;
1086 return 0;
1087}
1088
1089static int set_serial_info(struct async_struct * info,
1090 struct serial_struct __user * new_info)
1091{
1092 struct serial_struct new_serial;
1093 struct serial_state old_state, *state;
1094 unsigned int change_irq,change_port;
1095 int retval = 0;
1096
1097 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1098 return -EFAULT;
1099
1100 tty_lock();
1101 state = info->state;
1102 old_state = *state;
1103
1104 change_irq = new_serial.irq != state->irq;
1105 change_port = (new_serial.port != state->port);
1106 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1107 tty_unlock();
1108 return -EINVAL;
1109 }
1110
1111 if (!serial_isroot()) {
1112 if ((new_serial.baud_base != state->baud_base) ||
1113 (new_serial.close_delay != state->close_delay) ||
1114 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1115 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1116 (state->flags & ~ASYNC_USR_MASK)))
1117 return -EPERM;
1118 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1119 (new_serial.flags & ASYNC_USR_MASK));
1120 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1121 (new_serial.flags & ASYNC_USR_MASK));
1122 state->custom_divisor = new_serial.custom_divisor;
1123 goto check_and_exit;
1124 }
1125
1126 if (new_serial.baud_base < 9600) {
1127 tty_unlock();
1128 return -EINVAL;
1129 }
1130
1131 /*
1132 * OK, past this point, all the error checking has been done.
1133 * At this point, we start making changes.....
1134 */
1135
1136 state->baud_base = new_serial.baud_base;
1137 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1138 (new_serial.flags & ASYNC_FLAGS));
1139 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1140 (info->flags & ASYNC_INTERNAL_FLAGS));
1141 state->custom_divisor = new_serial.custom_divisor;
1142 state->close_delay = new_serial.close_delay * HZ/100;
1143 state->closing_wait = new_serial.closing_wait * HZ/100;
1144 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1145
1146check_and_exit:
1147 if (info->flags & ASYNC_INITIALIZED) {
1148 if (((old_state.flags & ASYNC_SPD_MASK) !=
1149 (state->flags & ASYNC_SPD_MASK)) ||
1150 (old_state.custom_divisor != state->custom_divisor)) {
1151 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1152 info->tty->alt_speed = 57600;
1153 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1154 info->tty->alt_speed = 115200;
1155 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1156 info->tty->alt_speed = 230400;
1157 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1158 info->tty->alt_speed = 460800;
1159 change_speed(info, NULL);
1160 }
1161 } else
1162 retval = startup(info);
1163 tty_unlock();
1164 return retval;
1165}
1166
1167
1168/*
1169 * get_lsr_info - get line status register info
1170 *
1171 * Purpose: Let user call ioctl() to get info when the UART physically
1172 * is emptied. On bus types like RS485, the transmitter must
1173 * release the bus after transmitting. This must be done when
1174 * the transmit shift register is empty, not be done when the
1175 * transmit holding register is empty. This functionality
1176 * allows an RS485 driver to be written in user space.
1177 */
1178static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1179{
1180 unsigned char status;
1181 unsigned int result;
1182 unsigned long flags;
1183
1184 local_irq_save(flags);
1185 status = custom.serdatr;
1186 mb();
1187 local_irq_restore(flags);
1188 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1189 if (copy_to_user(value, &result, sizeof(int)))
1190 return -EFAULT;
1191 return 0;
1192}
1193
1194
1195static int rs_tiocmget(struct tty_struct *tty)
1196{
1197 struct async_struct * info = tty->driver_data;
1198 unsigned char control, status;
1199 unsigned long flags;
1200
1201 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1202 return -ENODEV;
1203 if (tty->flags & (1 << TTY_IO_ERROR))
1204 return -EIO;
1205
1206 control = info->MCR;
1207 local_irq_save(flags);
1208 status = ciab.pra;
1209 local_irq_restore(flags);
1210 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1211 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1212 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1213 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1214 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1215}
1216
1217static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
1218 unsigned int clear)
1219{
1220 struct async_struct * info = tty->driver_data;
1221 unsigned long flags;
1222
1223 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1224 return -ENODEV;
1225 if (tty->flags & (1 << TTY_IO_ERROR))
1226 return -EIO;
1227
1228 local_irq_save(flags);
1229 if (set & TIOCM_RTS)
1230 info->MCR |= SER_RTS;
1231 if (set & TIOCM_DTR)
1232 info->MCR |= SER_DTR;
1233 if (clear & TIOCM_RTS)
1234 info->MCR &= ~SER_RTS;
1235 if (clear & TIOCM_DTR)
1236 info->MCR &= ~SER_DTR;
1237 rtsdtr_ctrl(info->MCR);
1238 local_irq_restore(flags);
1239 return 0;
1240}
1241
1242/*
1243 * rs_break() --- routine which turns the break handling on or off
1244 */
1245static int rs_break(struct tty_struct *tty, int break_state)
1246{
1247 struct async_struct * info = tty->driver_data;
1248 unsigned long flags;
1249
1250 if (serial_paranoia_check(info, tty->name, "rs_break"))
1251 return -EINVAL;
1252
1253 local_irq_save(flags);
1254 if (break_state == -1)
1255 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1256 else
1257 custom.adkcon = AC_UARTBRK;
1258 mb();
1259 local_irq_restore(flags);
1260 return 0;
1261}
1262
1263/*
1264 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1265 * Return: write counters to the user passed counter struct
1266 * NB: both 1->0 and 0->1 transitions are counted except for
1267 * RI where only 0->1 is counted.
1268 */
1269static int rs_get_icount(struct tty_struct *tty,
1270 struct serial_icounter_struct *icount)
1271{
1272 struct async_struct *info = tty->driver_data;
1273 struct async_icount cnow;
1274 unsigned long flags;
1275
1276 local_irq_save(flags);
1277 cnow = info->state->icount;
1278 local_irq_restore(flags);
1279 icount->cts = cnow.cts;
1280 icount->dsr = cnow.dsr;
1281 icount->rng = cnow.rng;
1282 icount->dcd = cnow.dcd;
1283 icount->rx = cnow.rx;
1284 icount->tx = cnow.tx;
1285 icount->frame = cnow.frame;
1286 icount->overrun = cnow.overrun;
1287 icount->parity = cnow.parity;
1288 icount->brk = cnow.brk;
1289 icount->buf_overrun = cnow.buf_overrun;
1290
1291 return 0;
1292}
1293
1294static int rs_ioctl(struct tty_struct *tty,
1295 unsigned int cmd, unsigned long arg)
1296{
1297 struct async_struct * info = tty->driver_data;
1298 struct async_icount cprev, cnow; /* kernel counter temps */
1299 void __user *argp = (void __user *)arg;
1300 unsigned long flags;
1301
1302 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1303 return -ENODEV;
1304
1305 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1306 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1307 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1308 if (tty->flags & (1 << TTY_IO_ERROR))
1309 return -EIO;
1310 }
1311
1312 switch (cmd) {
1313 case TIOCGSERIAL:
1314 return get_serial_info(info, argp);
1315 case TIOCSSERIAL:
1316 return set_serial_info(info, argp);
1317 case TIOCSERCONFIG:
1318 return 0;
1319
1320 case TIOCSERGETLSR: /* Get line status register */
1321 return get_lsr_info(info, argp);
1322
1323 case TIOCSERGSTRUCT:
1324 if (copy_to_user(argp,
1325 info, sizeof(struct async_struct)))
1326 return -EFAULT;
1327 return 0;
1328
1329 /*
1330 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1331 * - mask passed in arg for lines of interest
1332 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1333 * Caller should use TIOCGICOUNT to see which one it was
1334 */
1335 case TIOCMIWAIT:
1336 local_irq_save(flags);
1337 /* note the counters on entry */
1338 cprev = info->state->icount;
1339 local_irq_restore(flags);
1340 while (1) {
1341 interruptible_sleep_on(&info->delta_msr_wait);
1342 /* see if a signal did it */
1343 if (signal_pending(current))
1344 return -ERESTARTSYS;
1345 local_irq_save(flags);
1346 cnow = info->state->icount; /* atomic copy */
1347 local_irq_restore(flags);
1348 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1349 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1350 return -EIO; /* no change => error */
1351 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1352 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1353 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1354 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1355 return 0;
1356 }
1357 cprev = cnow;
1358 }
1359 /* NOTREACHED */
1360
1361 case TIOCSERGWILD:
1362 case TIOCSERSWILD:
1363 /* "setserial -W" is called in Debian boot */
1364 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1365 return 0;
1366
1367 default:
1368 return -ENOIOCTLCMD;
1369 }
1370 return 0;
1371}
1372
1373static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1374{
1375 struct async_struct *info = tty->driver_data;
1376 unsigned long flags;
1377 unsigned int cflag = tty->termios->c_cflag;
1378
1379 change_speed(info, old_termios);
1380
1381 /* Handle transition to B0 status */
1382 if ((old_termios->c_cflag & CBAUD) &&
1383 !(cflag & CBAUD)) {
1384 info->MCR &= ~(SER_DTR|SER_RTS);
1385 local_irq_save(flags);
1386 rtsdtr_ctrl(info->MCR);
1387 local_irq_restore(flags);
1388 }
1389
1390 /* Handle transition away from B0 status */
1391 if (!(old_termios->c_cflag & CBAUD) &&
1392 (cflag & CBAUD)) {
1393 info->MCR |= SER_DTR;
1394 if (!(tty->termios->c_cflag & CRTSCTS) ||
1395 !test_bit(TTY_THROTTLED, &tty->flags)) {
1396 info->MCR |= SER_RTS;
1397 }
1398 local_irq_save(flags);
1399 rtsdtr_ctrl(info->MCR);
1400 local_irq_restore(flags);
1401 }
1402
1403 /* Handle turning off CRTSCTS */
1404 if ((old_termios->c_cflag & CRTSCTS) &&
1405 !(tty->termios->c_cflag & CRTSCTS)) {
1406 tty->hw_stopped = 0;
1407 rs_start(tty);
1408 }
1409
1410#if 0
1411 /*
1412 * No need to wake up processes in open wait, since they
1413 * sample the CLOCAL flag once, and don't recheck it.
1414 * XXX It's not clear whether the current behavior is correct
1415 * or not. Hence, this may change.....
1416 */
1417 if (!(old_termios->c_cflag & CLOCAL) &&
1418 (tty->termios->c_cflag & CLOCAL))
1419 wake_up_interruptible(&info->open_wait);
1420#endif
1421}
1422
1423/*
1424 * ------------------------------------------------------------
1425 * rs_close()
1426 *
1427 * This routine is called when the serial port gets closed. First, we
1428 * wait for the last remaining data to be sent. Then, we unlink its
1429 * async structure from the interrupt chain if necessary, and we free
1430 * that IRQ if nothing is left in the chain.
1431 * ------------------------------------------------------------
1432 */
1433static void rs_close(struct tty_struct *tty, struct file * filp)
1434{
1435 struct async_struct * info = tty->driver_data;
1436 struct serial_state *state;
1437 unsigned long flags;
1438
1439 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1440 return;
1441
1442 state = info->state;
1443
1444 local_irq_save(flags);
1445
1446 if (tty_hung_up_p(filp)) {
1447 DBG_CNT("before DEC-hung");
1448 local_irq_restore(flags);
1449 return;
1450 }
1451
1452#ifdef SERIAL_DEBUG_OPEN
1453 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1454#endif
1455 if ((tty->count == 1) && (state->count != 1)) {
1456 /*
1457 * Uh, oh. tty->count is 1, which means that the tty
1458 * structure will be freed. state->count should always
1459 * be one in these conditions. If it's greater than
1460 * one, we've got real problems, since it means the
1461 * serial port won't be shutdown.
1462 */
1463 printk("rs_close: bad serial port count; tty->count is 1, "
1464 "state->count is %d\n", state->count);
1465 state->count = 1;
1466 }
1467 if (--state->count < 0) {
1468 printk("rs_close: bad serial port count for ttys%d: %d\n",
1469 info->line, state->count);
1470 state->count = 0;
1471 }
1472 if (state->count) {
1473 DBG_CNT("before DEC-2");
1474 local_irq_restore(flags);
1475 return;
1476 }
1477 info->flags |= ASYNC_CLOSING;
1478 /*
1479 * Now we wait for the transmit buffer to clear; and we notify
1480 * the line discipline to only process XON/XOFF characters.
1481 */
1482 tty->closing = 1;
1483 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1484 tty_wait_until_sent(tty, info->closing_wait);
1485 /*
1486 * At this point we stop accepting input. To do this, we
1487 * disable the receive line status interrupts, and tell the
1488 * interrupt driver to stop checking the data ready bit in the
1489 * line status register.
1490 */
1491 info->read_status_mask &= ~UART_LSR_DR;
1492 if (info->flags & ASYNC_INITIALIZED) {
1493 /* disable receive interrupts */
1494 custom.intena = IF_RBF;
1495 mb();
1496 /* clear any pending receive interrupt */
1497 custom.intreq = IF_RBF;
1498 mb();
1499
1500 /*
1501 * Before we drop DTR, make sure the UART transmitter
1502 * has completely drained; this is especially
1503 * important if there is a transmit FIFO!
1504 */
1505 rs_wait_until_sent(tty, info->timeout);
1506 }
1507 shutdown(info);
1508 rs_flush_buffer(tty);
1509
1510 tty_ldisc_flush(tty);
1511 tty->closing = 0;
1512 info->event = 0;
1513 info->tty = NULL;
1514 if (info->blocked_open) {
1515 if (info->close_delay) {
1516 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1517 }
1518 wake_up_interruptible(&info->open_wait);
1519 }
1520 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1521 wake_up_interruptible(&info->close_wait);
1522 local_irq_restore(flags);
1523}
1524
1525/*
1526 * rs_wait_until_sent() --- wait until the transmitter is empty
1527 */
1528static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1529{
1530 struct async_struct * info = tty->driver_data;
1531 unsigned long orig_jiffies, char_time;
1532 int tty_was_locked = tty_locked();
1533 int lsr;
1534
1535 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1536 return;
1537
1538 if (info->xmit_fifo_size == 0)
1539 return; /* Just in case.... */
1540
1541 orig_jiffies = jiffies;
1542
1543 /*
1544 * tty_wait_until_sent is called from lots of places,
1545 * with or without the BTM.
1546 */
1547 if (!tty_was_locked)
1548 tty_lock();
1549 /*
1550 * Set the check interval to be 1/5 of the estimated time to
1551 * send a single character, and make it at least 1. The check
1552 * interval should also be less than the timeout.
1553 *
1554 * Note: we have to use pretty tight timings here to satisfy
1555 * the NIST-PCTS.
1556 */
1557 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1558 char_time = char_time / 5;
1559 if (char_time == 0)
1560 char_time = 1;
1561 if (timeout)
1562 char_time = min_t(unsigned long, char_time, timeout);
1563 /*
1564 * If the transmitter hasn't cleared in twice the approximate
1565 * amount of time to send the entire FIFO, it probably won't
1566 * ever clear. This assumes the UART isn't doing flow
1567 * control, which is currently the case. Hence, if it ever
1568 * takes longer than info->timeout, this is probably due to a
1569 * UART bug of some kind. So, we clamp the timeout parameter at
1570 * 2*info->timeout.
1571 */
1572 if (!timeout || timeout > 2*info->timeout)
1573 timeout = 2*info->timeout;
1574#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1575 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1576 printk("jiff=%lu...", jiffies);
1577#endif
1578 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1579#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1580 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1581#endif
1582 msleep_interruptible(jiffies_to_msecs(char_time));
1583 if (signal_pending(current))
1584 break;
1585 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1586 break;
1587 }
1588 __set_current_state(TASK_RUNNING);
1589 if (!tty_was_locked)
1590 tty_unlock();
1591#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1592 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1593#endif
1594}
1595
1596/*
1597 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1598 */
1599static void rs_hangup(struct tty_struct *tty)
1600{
1601 struct async_struct * info = tty->driver_data;
1602 struct serial_state *state = info->state;
1603
1604 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1605 return;
1606
1607 state = info->state;
1608
1609 rs_flush_buffer(tty);
1610 shutdown(info);
1611 info->event = 0;
1612 state->count = 0;
1613 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1614 info->tty = NULL;
1615 wake_up_interruptible(&info->open_wait);
1616}
1617
1618/*
1619 * ------------------------------------------------------------
1620 * rs_open() and friends
1621 * ------------------------------------------------------------
1622 */
1623static int block_til_ready(struct tty_struct *tty, struct file * filp,
1624 struct async_struct *info)
1625{
1626#ifdef DECLARE_WAITQUEUE
1627 DECLARE_WAITQUEUE(wait, current);
1628#else
1629 struct wait_queue wait = { current, NULL };
1630#endif
1631 struct serial_state *state = info->state;
1632 int retval;
1633 int do_clocal = 0, extra_count = 0;
1634 unsigned long flags;
1635
1636 /*
1637 * If the device is in the middle of being closed, then block
1638 * until it's done, and then try again.
1639 */
1640 if (tty_hung_up_p(filp) ||
1641 (info->flags & ASYNC_CLOSING)) {
1642 if (info->flags & ASYNC_CLOSING)
1643 interruptible_sleep_on(&info->close_wait);
1644#ifdef SERIAL_DO_RESTART
1645 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1646 -EAGAIN : -ERESTARTSYS);
1647#else
1648 return -EAGAIN;
1649#endif
1650 }
1651
1652 /*
1653 * If non-blocking mode is set, or the port is not enabled,
1654 * then make the check up front and then exit.
1655 */
1656 if ((filp->f_flags & O_NONBLOCK) ||
1657 (tty->flags & (1 << TTY_IO_ERROR))) {
1658 info->flags |= ASYNC_NORMAL_ACTIVE;
1659 return 0;
1660 }
1661
1662 if (tty->termios->c_cflag & CLOCAL)
1663 do_clocal = 1;
1664
1665 /*
1666 * Block waiting for the carrier detect and the line to become
1667 * free (i.e., not in use by the callout). While we are in
1668 * this loop, state->count is dropped by one, so that
1669 * rs_close() knows when to free things. We restore it upon
1670 * exit, either normal or abnormal.
1671 */
1672 retval = 0;
1673 add_wait_queue(&info->open_wait, &wait);
1674#ifdef SERIAL_DEBUG_OPEN
1675 printk("block_til_ready before block: ttys%d, count = %d\n",
1676 state->line, state->count);
1677#endif
1678 local_irq_save(flags);
1679 if (!tty_hung_up_p(filp)) {
1680 extra_count = 1;
1681 state->count--;
1682 }
1683 local_irq_restore(flags);
1684 info->blocked_open++;
1685 while (1) {
1686 local_irq_save(flags);
1687 if (tty->termios->c_cflag & CBAUD)
1688 rtsdtr_ctrl(SER_DTR|SER_RTS);
1689 local_irq_restore(flags);
1690 set_current_state(TASK_INTERRUPTIBLE);
1691 if (tty_hung_up_p(filp) ||
1692 !(info->flags & ASYNC_INITIALIZED)) {
1693#ifdef SERIAL_DO_RESTART
1694 if (info->flags & ASYNC_HUP_NOTIFY)
1695 retval = -EAGAIN;
1696 else
1697 retval = -ERESTARTSYS;
1698#else
1699 retval = -EAGAIN;
1700#endif
1701 break;
1702 }
1703 if (!(info->flags & ASYNC_CLOSING) &&
1704 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1705 break;
1706 if (signal_pending(current)) {
1707 retval = -ERESTARTSYS;
1708 break;
1709 }
1710#ifdef SERIAL_DEBUG_OPEN
1711 printk("block_til_ready blocking: ttys%d, count = %d\n",
1712 info->line, state->count);
1713#endif
1714 tty_unlock();
1715 schedule();
1716 tty_lock();
1717 }
1718 __set_current_state(TASK_RUNNING);
1719 remove_wait_queue(&info->open_wait, &wait);
1720 if (extra_count)
1721 state->count++;
1722 info->blocked_open--;
1723#ifdef SERIAL_DEBUG_OPEN
1724 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1725 info->line, state->count);
1726#endif
1727 if (retval)
1728 return retval;
1729 info->flags |= ASYNC_NORMAL_ACTIVE;
1730 return 0;
1731}
1732
1733static int get_async_struct(int line, struct async_struct **ret_info)
1734{
1735 struct async_struct *info;
1736 struct serial_state *sstate;
1737
1738 sstate = rs_table + line;
1739 sstate->count++;
1740 if (sstate->info) {
1741 *ret_info = sstate->info;
1742 return 0;
1743 }
1744 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1745 if (!info) {
1746 sstate->count--;
1747 return -ENOMEM;
1748 }
1749#ifdef DECLARE_WAITQUEUE
1750 init_waitqueue_head(&info->open_wait);
1751 init_waitqueue_head(&info->close_wait);
1752 init_waitqueue_head(&info->delta_msr_wait);
1753#endif
1754 info->magic = SERIAL_MAGIC;
1755 info->port = sstate->port;
1756 info->flags = sstate->flags;
1757 info->xmit_fifo_size = sstate->xmit_fifo_size;
1758 info->line = line;
1759 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1760 info->state = sstate;
1761 if (sstate->info) {
1762 kfree(info);
1763 *ret_info = sstate->info;
1764 return 0;
1765 }
1766 *ret_info = sstate->info = info;
1767 return 0;
1768}
1769
1770/*
1771 * This routine is called whenever a serial port is opened. It
1772 * enables interrupts for a serial port, linking in its async structure into
1773 * the IRQ chain. It also performs the serial-specific
1774 * initialization for the tty structure.
1775 */
1776static int rs_open(struct tty_struct *tty, struct file * filp)
1777{
1778 struct async_struct *info;
1779 int retval, line;
1780
1781 line = tty->index;
1782 if ((line < 0) || (line >= NR_PORTS)) {
1783 return -ENODEV;
1784 }
1785 retval = get_async_struct(line, &info);
1786 if (retval) {
1787 return retval;
1788 }
1789 tty->driver_data = info;
1790 info->tty = tty;
1791 if (serial_paranoia_check(info, tty->name, "rs_open"))
1792 return -ENODEV;
1793
1794#ifdef SERIAL_DEBUG_OPEN
1795 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1796#endif
1797 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1798
1799 /*
1800 * If the port is the middle of closing, bail out now
1801 */
1802 if (tty_hung_up_p(filp) ||
1803 (info->flags & ASYNC_CLOSING)) {
1804 if (info->flags & ASYNC_CLOSING)
1805 interruptible_sleep_on(&info->close_wait);
1806#ifdef SERIAL_DO_RESTART
1807 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1808 -EAGAIN : -ERESTARTSYS);
1809#else
1810 return -EAGAIN;
1811#endif
1812 }
1813
1814 /*
1815 * Start up serial port
1816 */
1817 retval = startup(info);
1818 if (retval) {
1819 return retval;
1820 }
1821
1822 retval = block_til_ready(tty, filp, info);
1823 if (retval) {
1824#ifdef SERIAL_DEBUG_OPEN
1825 printk("rs_open returning after block_til_ready with %d\n",
1826 retval);
1827#endif
1828 return retval;
1829 }
1830
1831#ifdef SERIAL_DEBUG_OPEN
1832 printk("rs_open %s successful...", tty->name);
1833#endif
1834 return 0;
1835}
1836
1837/*
1838 * /proc fs routines....
1839 */
1840
1841static inline void line_info(struct seq_file *m, struct serial_state *state)
1842{
1843 struct async_struct *info = state->info, scr_info;
1844 char stat_buf[30], control, status;
1845 unsigned long flags;
1846
1847 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1848
1849 /*
1850 * Figure out the current RS-232 lines
1851 */
1852 if (!info) {
1853 info = &scr_info; /* This is just for serial_{in,out} */
1854
1855 info->magic = SERIAL_MAGIC;
1856 info->flags = state->flags;
1857 info->quot = 0;
1858 info->tty = NULL;
1859 }
1860 local_irq_save(flags);
1861 status = ciab.pra;
1862 control = info ? info->MCR : status;
1863 local_irq_restore(flags);
1864
1865 stat_buf[0] = 0;
1866 stat_buf[1] = 0;
1867 if(!(control & SER_RTS))
1868 strcat(stat_buf, "|RTS");
1869 if(!(status & SER_CTS))
1870 strcat(stat_buf, "|CTS");
1871 if(!(control & SER_DTR))
1872 strcat(stat_buf, "|DTR");
1873 if(!(status & SER_DSR))
1874 strcat(stat_buf, "|DSR");
1875 if(!(status & SER_DCD))
1876 strcat(stat_buf, "|CD");
1877
1878 if (info->quot) {
1879 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1880 }
1881
1882 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1883
1884 if (state->icount.frame)
1885 seq_printf(m, " fe:%d", state->icount.frame);
1886
1887 if (state->icount.parity)
1888 seq_printf(m, " pe:%d", state->icount.parity);
1889
1890 if (state->icount.brk)
1891 seq_printf(m, " brk:%d", state->icount.brk);
1892
1893 if (state->icount.overrun)
1894 seq_printf(m, " oe:%d", state->icount.overrun);
1895
1896 /*
1897 * Last thing is the RS-232 status lines
1898 */
1899 seq_printf(m, " %s\n", stat_buf+1);
1900}
1901
1902static int rs_proc_show(struct seq_file *m, void *v)
1903{
1904 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1905 line_info(m, &rs_table[0]);
1906 return 0;
1907}
1908
1909static int rs_proc_open(struct inode *inode, struct file *file)
1910{
1911 return single_open(file, rs_proc_show, NULL);
1912}
1913
1914static const struct file_operations rs_proc_fops = {
1915 .owner = THIS_MODULE,
1916 .open = rs_proc_open,
1917 .read = seq_read,
1918 .llseek = seq_lseek,
1919 .release = single_release,
1920};
1921
1922/*
1923 * ---------------------------------------------------------------------
1924 * rs_init() and friends
1925 *
1926 * rs_init() is called at boot-time to initialize the serial driver.
1927 * ---------------------------------------------------------------------
1928 */
1929
1930/*
1931 * This routine prints out the appropriate serial driver version
1932 * number, and identifies which options were configured into this
1933 * driver.
1934 */
1935static void show_serial_version(void)
1936{
1937 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1938}
1939
1940
1941static const struct tty_operations serial_ops = {
1942 .open = rs_open,
1943 .close = rs_close,
1944 .write = rs_write,
1945 .put_char = rs_put_char,
1946 .flush_chars = rs_flush_chars,
1947 .write_room = rs_write_room,
1948 .chars_in_buffer = rs_chars_in_buffer,
1949 .flush_buffer = rs_flush_buffer,
1950 .ioctl = rs_ioctl,
1951 .throttle = rs_throttle,
1952 .unthrottle = rs_unthrottle,
1953 .set_termios = rs_set_termios,
1954 .stop = rs_stop,
1955 .start = rs_start,
1956 .hangup = rs_hangup,
1957 .break_ctl = rs_break,
1958 .send_xchar = rs_send_xchar,
1959 .wait_until_sent = rs_wait_until_sent,
1960 .tiocmget = rs_tiocmget,
1961 .tiocmset = rs_tiocmset,
1962 .get_icount = rs_get_icount,
1963 .proc_fops = &rs_proc_fops,
1964};
1965
1966/*
1967 * The serial driver boot-time initialization code!
1968 */
1969static int __init amiga_serial_probe(struct platform_device *pdev)
1970{
1971 unsigned long flags;
1972 struct serial_state * state;
1973 int error;
1974
1975 serial_driver = alloc_tty_driver(1);
1976 if (!serial_driver)
1977 return -ENOMEM;
1978
1979 IRQ_ports = NULL;
1980
1981 show_serial_version();
1982
1983 /* Initialize the tty_driver structure */
1984
1985 serial_driver->owner = THIS_MODULE;
1986 serial_driver->driver_name = "amiserial";
1987 serial_driver->name = "ttyS";
1988 serial_driver->major = TTY_MAJOR;
1989 serial_driver->minor_start = 64;
1990 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1991 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1992 serial_driver->init_termios = tty_std_termios;
1993 serial_driver->init_termios.c_cflag =
1994 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1995 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1996 tty_set_operations(serial_driver, &serial_ops);
1997
1998 error = tty_register_driver(serial_driver);
1999 if (error)
2000 goto fail_put_tty_driver;
2001
2002 state = rs_table;
2003 state->magic = SSTATE_MAGIC;
2004 state->port = (int)&custom.serdatr; /* Just to give it a value */
2005 state->line = 0;
2006 state->custom_divisor = 0;
2007 state->close_delay = 5*HZ/10;
2008 state->closing_wait = 30*HZ;
2009 state->icount.cts = state->icount.dsr =
2010 state->icount.rng = state->icount.dcd = 0;
2011 state->icount.rx = state->icount.tx = 0;
2012 state->icount.frame = state->icount.parity = 0;
2013 state->icount.overrun = state->icount.brk = 0;
2014
2015 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2016 state->line);
2017
2018 /* Hardware set up */
2019
2020 state->baud_base = amiga_colorclock;
2021 state->xmit_fifo_size = 1;
2022
2023 /* set ISRs, and then disable the rx interrupts */
2024 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2025 if (error)
2026 goto fail_unregister;
2027
2028 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED,
2029 "serial RX", state);
2030 if (error)
2031 goto fail_free_irq;
2032
2033 local_irq_save(flags);
2034
2035 /* turn off Rx and Tx interrupts */
2036 custom.intena = IF_RBF | IF_TBE;
2037 mb();
2038
2039 /* clear any pending interrupt */
2040 custom.intreq = IF_RBF | IF_TBE;
2041 mb();
2042
2043 local_irq_restore(flags);
2044
2045 /*
2046 * set the appropriate directions for the modem control flags,
2047 * and clear RTS and DTR
2048 */
2049 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2050 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2051
2052 platform_set_drvdata(pdev, state);
2053
2054 return 0;
2055
2056fail_free_irq:
2057 free_irq(IRQ_AMIGA_TBE, state);
2058fail_unregister:
2059 tty_unregister_driver(serial_driver);
2060fail_put_tty_driver:
2061 put_tty_driver(serial_driver);
2062 return error;
2063}
2064
2065static int __exit amiga_serial_remove(struct platform_device *pdev)
2066{
2067 int error;
2068 struct serial_state *state = platform_get_drvdata(pdev);
2069 struct async_struct *info = state->info;
2070
2071 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2072 tasklet_kill(&info->tlet);
2073 if ((error = tty_unregister_driver(serial_driver)))
2074 printk("SERIAL: failed to unregister serial driver (%d)\n",
2075 error);
2076 put_tty_driver(serial_driver);
2077
2078 rs_table[0].info = NULL;
2079 kfree(info);
2080
2081 free_irq(IRQ_AMIGA_TBE, rs_table);
2082 free_irq(IRQ_AMIGA_RBF, rs_table);
2083
2084 platform_set_drvdata(pdev, NULL);
2085
2086 return error;
2087}
2088
2089static struct platform_driver amiga_serial_driver = {
2090 .remove = __exit_p(amiga_serial_remove),
2091 .driver = {
2092 .name = "amiga-serial",
2093 .owner = THIS_MODULE,
2094 },
2095};
2096
2097static int __init amiga_serial_init(void)
2098{
2099 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2100}
2101
2102module_init(amiga_serial_init);
2103
2104static void __exit amiga_serial_exit(void)
2105{
2106 platform_driver_unregister(&amiga_serial_driver);
2107}
2108
2109module_exit(amiga_serial_exit);
2110
2111
2112#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2113
2114/*
2115 * ------------------------------------------------------------
2116 * Serial console driver
2117 * ------------------------------------------------------------
2118 */
2119
2120static void amiga_serial_putc(char c)
2121{
2122 custom.serdat = (unsigned char)c | 0x100;
2123 while (!(custom.serdatr & 0x2000))
2124 barrier();
2125}
2126
2127/*
2128 * Print a string to the serial port trying not to disturb
2129 * any possible real use of the port...
2130 *
2131 * The console must be locked when we get here.
2132 */
2133static void serial_console_write(struct console *co, const char *s,
2134 unsigned count)
2135{
2136 unsigned short intena = custom.intenar;
2137
2138 custom.intena = IF_TBE;
2139
2140 while (count--) {
2141 if (*s == '\n')
2142 amiga_serial_putc('\r');
2143 amiga_serial_putc(*s++);
2144 }
2145
2146 custom.intena = IF_SETCLR | (intena & IF_TBE);
2147}
2148
2149static struct tty_driver *serial_console_device(struct console *c, int *index)
2150{
2151 *index = 0;
2152 return serial_driver;
2153}
2154
2155static struct console sercons = {
2156 .name = "ttyS",
2157 .write = serial_console_write,
2158 .device = serial_console_device,
2159 .flags = CON_PRINTBUFFER,
2160 .index = -1,
2161};
2162
2163/*
2164 * Register console.
2165 */
2166static int __init amiserial_console_init(void)
2167{
2168 register_console(&sercons);
2169 return 0;
2170}
2171console_initcall(amiserial_console_init);
2172
2173#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2174
2175MODULE_LICENSE("GPL");
2176MODULE_ALIAS("platform:amiga-serial");
diff --git a/drivers/tty/bfin_jtag_comm.c b/drivers/tty/bfin_jtag_comm.c
new file mode 100644
index 000000000000..03c285bb2f18
--- /dev/null
+++ b/drivers/tty/bfin_jtag_comm.c
@@ -0,0 +1,368 @@
1/*
2 * TTY over Blackfin JTAG Communication
3 *
4 * Copyright 2008-2009 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#define DRV_NAME "bfin-jtag-comm"
12#define DEV_NAME "ttyBFJC"
13#define pr_fmt(fmt) DRV_NAME ": " fmt
14
15#include <linux/circ_buf.h>
16#include <linux/console.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/kernel.h>
20#include <linux/kthread.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/sched.h>
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <asm/atomic.h>
29
30#define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
31
32/* See the Debug/Emulation chapter in the HRM */
33#define EMUDOF 0x00000001 /* EMUDAT_OUT full & valid */
34#define EMUDIF 0x00000002 /* EMUDAT_IN full & valid */
35#define EMUDOOVF 0x00000004 /* EMUDAT_OUT overflow */
36#define EMUDIOVF 0x00000008 /* EMUDAT_IN overflow */
37
38static inline uint32_t bfin_write_emudat(uint32_t emudat)
39{
40 __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
41 return emudat;
42}
43
44static inline uint32_t bfin_read_emudat(void)
45{
46 uint32_t emudat;
47 __asm__ __volatile__("%0 = emudat;" : "=d"(emudat));
48 return emudat;
49}
50
51static inline uint32_t bfin_write_emudat_chars(char a, char b, char c, char d)
52{
53 return bfin_write_emudat((a << 0) | (b << 8) | (c << 16) | (d << 24));
54}
55
56#define CIRC_SIZE 2048 /* see comment in tty_io.c:do_tty_write() */
57#define CIRC_MASK (CIRC_SIZE - 1)
58#define circ_empty(circ) ((circ)->head == (circ)->tail)
59#define circ_free(circ) CIRC_SPACE((circ)->head, (circ)->tail, CIRC_SIZE)
60#define circ_cnt(circ) CIRC_CNT((circ)->head, (circ)->tail, CIRC_SIZE)
61#define circ_byte(circ, idx) ((circ)->buf[(idx) & CIRC_MASK])
62
63static struct tty_driver *bfin_jc_driver;
64static struct task_struct *bfin_jc_kthread;
65static struct tty_struct * volatile bfin_jc_tty;
66static unsigned long bfin_jc_count;
67static DEFINE_MUTEX(bfin_jc_tty_mutex);
68static volatile struct circ_buf bfin_jc_write_buf;
69
70static int
71bfin_jc_emudat_manager(void *arg)
72{
73 uint32_t inbound_len = 0, outbound_len = 0;
74
75 while (!kthread_should_stop()) {
76 /* no one left to give data to, so sleep */
77 if (bfin_jc_tty == NULL && circ_empty(&bfin_jc_write_buf)) {
78 pr_debug("waiting for readers\n");
79 __set_current_state(TASK_UNINTERRUPTIBLE);
80 schedule();
81 __set_current_state(TASK_RUNNING);
82 }
83
84 /* no data available, so just chill */
85 if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) {
86 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
87 inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head);
88 if (inbound_len)
89 schedule();
90 else
91 schedule_timeout_interruptible(HZ);
92 continue;
93 }
94
95 /* if incoming data is ready, eat it */
96 if (bfin_read_DBGSTAT() & EMUDIF) {
97 struct tty_struct *tty;
98 mutex_lock(&bfin_jc_tty_mutex);
99 tty = (struct tty_struct *)bfin_jc_tty;
100 if (tty != NULL) {
101 uint32_t emudat = bfin_read_emudat();
102 if (inbound_len == 0) {
103 pr_debug("incoming length: 0x%08x\n", emudat);
104 inbound_len = emudat;
105 } else {
106 size_t num_chars = (4 <= inbound_len ? 4 : inbound_len);
107 pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat, num_chars);
108 inbound_len -= num_chars;
109 tty_insert_flip_string(tty, (unsigned char *)&emudat, num_chars);
110 tty_flip_buffer_push(tty);
111 }
112 }
113 mutex_unlock(&bfin_jc_tty_mutex);
114 }
115
116 /* if outgoing data is ready, post it */
117 if (!(bfin_read_DBGSTAT() & EMUDOF) && !circ_empty(&bfin_jc_write_buf)) {
118 if (outbound_len == 0) {
119 outbound_len = circ_cnt(&bfin_jc_write_buf);
120 bfin_write_emudat(outbound_len);
121 pr_debug("outgoing length: 0x%08x\n", outbound_len);
122 } else {
123 struct tty_struct *tty;
124 int tail = bfin_jc_write_buf.tail;
125 size_t ate = (4 <= outbound_len ? 4 : outbound_len);
126 uint32_t emudat =
127 bfin_write_emudat_chars(
128 circ_byte(&bfin_jc_write_buf, tail + 0),
129 circ_byte(&bfin_jc_write_buf, tail + 1),
130 circ_byte(&bfin_jc_write_buf, tail + 2),
131 circ_byte(&bfin_jc_write_buf, tail + 3)
132 );
133 bfin_jc_write_buf.tail += ate;
134 outbound_len -= ate;
135 mutex_lock(&bfin_jc_tty_mutex);
136 tty = (struct tty_struct *)bfin_jc_tty;
137 if (tty)
138 tty_wakeup(tty);
139 mutex_unlock(&bfin_jc_tty_mutex);
140 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate);
141 }
142 }
143 }
144
145 __set_current_state(TASK_RUNNING);
146 return 0;
147}
148
149static int
150bfin_jc_open(struct tty_struct *tty, struct file *filp)
151{
152 mutex_lock(&bfin_jc_tty_mutex);
153 pr_debug("open %lu\n", bfin_jc_count);
154 ++bfin_jc_count;
155 bfin_jc_tty = tty;
156 wake_up_process(bfin_jc_kthread);
157 mutex_unlock(&bfin_jc_tty_mutex);
158 return 0;
159}
160
161static void
162bfin_jc_close(struct tty_struct *tty, struct file *filp)
163{
164 mutex_lock(&bfin_jc_tty_mutex);
165 pr_debug("close %lu\n", bfin_jc_count);
166 if (--bfin_jc_count == 0)
167 bfin_jc_tty = NULL;
168 wake_up_process(bfin_jc_kthread);
169 mutex_unlock(&bfin_jc_tty_mutex);
170}
171
172/* XXX: we dont handle the put_char() case where we must handle count = 1 */
173static int
174bfin_jc_circ_write(const unsigned char *buf, int count)
175{
176 int i;
177 count = min(count, circ_free(&bfin_jc_write_buf));
178 pr_debug("going to write chunk of %i bytes\n", count);
179 for (i = 0; i < count; ++i)
180 circ_byte(&bfin_jc_write_buf, bfin_jc_write_buf.head + i) = buf[i];
181 bfin_jc_write_buf.head += i;
182 return i;
183}
184
185#ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
186# define console_lock()
187# define console_unlock()
188#endif
189static int
190bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count)
191{
192 int i;
193 console_lock();
194 i = bfin_jc_circ_write(buf, count);
195 console_unlock();
196 wake_up_process(bfin_jc_kthread);
197 return i;
198}
199
200static void
201bfin_jc_flush_chars(struct tty_struct *tty)
202{
203 wake_up_process(bfin_jc_kthread);
204}
205
206static int
207bfin_jc_write_room(struct tty_struct *tty)
208{
209 return circ_free(&bfin_jc_write_buf);
210}
211
212static int
213bfin_jc_chars_in_buffer(struct tty_struct *tty)
214{
215 return circ_cnt(&bfin_jc_write_buf);
216}
217
218static void
219bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout)
220{
221 unsigned long expire = jiffies + timeout;
222 while (!circ_empty(&bfin_jc_write_buf)) {
223 if (signal_pending(current))
224 break;
225 if (time_after(jiffies, expire))
226 break;
227 }
228}
229
230static const struct tty_operations bfin_jc_ops = {
231 .open = bfin_jc_open,
232 .close = bfin_jc_close,
233 .write = bfin_jc_write,
234 /*.put_char = bfin_jc_put_char,*/
235 .flush_chars = bfin_jc_flush_chars,
236 .write_room = bfin_jc_write_room,
237 .chars_in_buffer = bfin_jc_chars_in_buffer,
238 .wait_until_sent = bfin_jc_wait_until_sent,
239};
240
241static int __init bfin_jc_init(void)
242{
243 int ret;
244
245 bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME);
246 if (IS_ERR(bfin_jc_kthread))
247 return PTR_ERR(bfin_jc_kthread);
248
249 ret = -ENOMEM;
250
251 bfin_jc_write_buf.head = bfin_jc_write_buf.tail = 0;
252 bfin_jc_write_buf.buf = kmalloc(CIRC_SIZE, GFP_KERNEL);
253 if (!bfin_jc_write_buf.buf)
254 goto err_buf;
255
256 bfin_jc_driver = alloc_tty_driver(1);
257 if (!bfin_jc_driver)
258 goto err_driver;
259
260 bfin_jc_driver->owner = THIS_MODULE;
261 bfin_jc_driver->driver_name = DRV_NAME;
262 bfin_jc_driver->name = DEV_NAME;
263 bfin_jc_driver->type = TTY_DRIVER_TYPE_SERIAL;
264 bfin_jc_driver->subtype = SERIAL_TYPE_NORMAL;
265 bfin_jc_driver->init_termios = tty_std_termios;
266 tty_set_operations(bfin_jc_driver, &bfin_jc_ops);
267
268 ret = tty_register_driver(bfin_jc_driver);
269 if (ret)
270 goto err;
271
272 pr_init(KERN_INFO DRV_NAME ": initialized\n");
273
274 return 0;
275
276 err:
277 put_tty_driver(bfin_jc_driver);
278 err_driver:
279 kfree(bfin_jc_write_buf.buf);
280 err_buf:
281 kthread_stop(bfin_jc_kthread);
282 return ret;
283}
284module_init(bfin_jc_init);
285
286static void __exit bfin_jc_exit(void)
287{
288 kthread_stop(bfin_jc_kthread);
289 kfree(bfin_jc_write_buf.buf);
290 tty_unregister_driver(bfin_jc_driver);
291 put_tty_driver(bfin_jc_driver);
292}
293module_exit(bfin_jc_exit);
294
295#if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
296static void
297bfin_jc_straight_buffer_write(const char *buf, unsigned count)
298{
299 unsigned ate = 0;
300 while (bfin_read_DBGSTAT() & EMUDOF)
301 continue;
302 bfin_write_emudat(count);
303 while (ate < count) {
304 while (bfin_read_DBGSTAT() & EMUDOF)
305 continue;
306 bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]);
307 ate += 4;
308 }
309}
310#endif
311
312#ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
313static void
314bfin_jc_console_write(struct console *co, const char *buf, unsigned count)
315{
316 if (bfin_jc_kthread == NULL)
317 bfin_jc_straight_buffer_write(buf, count);
318 else
319 bfin_jc_circ_write(buf, count);
320}
321
322static struct tty_driver *
323bfin_jc_console_device(struct console *co, int *index)
324{
325 *index = co->index;
326 return bfin_jc_driver;
327}
328
329static struct console bfin_jc_console = {
330 .name = DEV_NAME,
331 .write = bfin_jc_console_write,
332 .device = bfin_jc_console_device,
333 .flags = CON_ANYTIME | CON_PRINTBUFFER,
334 .index = -1,
335};
336
337static int __init bfin_jc_console_init(void)
338{
339 register_console(&bfin_jc_console);
340 return 0;
341}
342console_initcall(bfin_jc_console_init);
343#endif
344
345#ifdef CONFIG_EARLY_PRINTK
346static void __init
347bfin_jc_early_write(struct console *co, const char *buf, unsigned int count)
348{
349 bfin_jc_straight_buffer_write(buf, count);
350}
351
352static struct __initdata console bfin_jc_early_console = {
353 .name = "early_BFJC",
354 .write = bfin_jc_early_write,
355 .flags = CON_ANYTIME | CON_PRINTBUFFER,
356 .index = -1,
357};
358
359struct console * __init
360bfin_jc_early_init(unsigned int port, unsigned int cflag)
361{
362 return &bfin_jc_early_console;
363}
364#endif
365
366MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
367MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
368MODULE_LICENSE("GPL");
diff --git a/drivers/tty/cyclades.c b/drivers/tty/cyclades.c
new file mode 100644
index 000000000000..c0e8f2eeb886
--- /dev/null
+++ b/drivers/tty/cyclades.c
@@ -0,0 +1,4196 @@
1#undef BLOCKMOVE
2#define Z_WAKE
3#undef Z_EXT_CHARS_IN_BUFFER
4
5/*
6 * This file contains the driver for the Cyclades async multiport
7 * serial boards.
8 *
9 * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
10 * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
11 *
12 * Copyright (C) 2007-2009 Jiri Slaby <jirislaby@gmail.com>
13 *
14 * Much of the design and some of the code came from serial.c
15 * which was copyright (C) 1991, 1992 Linus Torvalds. It was
16 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
17 * and then fixed as suggested by Michael K. Johnson 12/12/92.
18 * Converted to pci probing and cleaned up by Jiri Slaby.
19 *
20 */
21
22#define CY_VERSION "2.6"
23
24/* If you need to install more boards than NR_CARDS, change the constant
25 in the definition below. No other change is necessary to support up to
26 eight boards. Beyond that you'll have to extend cy_isa_addresses. */
27
28#define NR_CARDS 4
29
30/*
31 If the total number of ports is larger than NR_PORTS, change this
32 constant in the definition below. No other change is necessary to
33 support more boards/ports. */
34
35#define NR_PORTS 256
36
37#define ZO_V1 0
38#define ZO_V2 1
39#define ZE_V1 2
40
41#define SERIAL_PARANOIA_CHECK
42#undef CY_DEBUG_OPEN
43#undef CY_DEBUG_THROTTLE
44#undef CY_DEBUG_OTHER
45#undef CY_DEBUG_IO
46#undef CY_DEBUG_COUNT
47#undef CY_DEBUG_DTR
48#undef CY_DEBUG_WAIT_UNTIL_SENT
49#undef CY_DEBUG_INTERRUPTS
50#undef CY_16Y_HACK
51#undef CY_ENABLE_MONITORING
52#undef CY_PCI_DEBUG
53
54/*
55 * Include section
56 */
57#include <linux/module.h>
58#include <linux/errno.h>
59#include <linux/signal.h>
60#include <linux/sched.h>
61#include <linux/timer.h>
62#include <linux/interrupt.h>
63#include <linux/tty.h>
64#include <linux/tty_flip.h>
65#include <linux/serial.h>
66#include <linux/major.h>
67#include <linux/string.h>
68#include <linux/fcntl.h>
69#include <linux/ptrace.h>
70#include <linux/cyclades.h>
71#include <linux/mm.h>
72#include <linux/ioport.h>
73#include <linux/init.h>
74#include <linux/delay.h>
75#include <linux/spinlock.h>
76#include <linux/bitops.h>
77#include <linux/firmware.h>
78#include <linux/device.h>
79#include <linux/slab.h>
80
81#include <linux/io.h>
82#include <linux/uaccess.h>
83
84#include <linux/kernel.h>
85#include <linux/pci.h>
86
87#include <linux/stat.h>
88#include <linux/proc_fs.h>
89#include <linux/seq_file.h>
90
91static void cy_send_xchar(struct tty_struct *tty, char ch);
92
93#ifndef SERIAL_XMIT_SIZE
94#define SERIAL_XMIT_SIZE (min(PAGE_SIZE, 4096))
95#endif
96
97#define STD_COM_FLAGS (0)
98
99/* firmware stuff */
100#define ZL_MAX_BLOCKS 16
101#define DRIVER_VERSION 0x02010203
102#define RAM_SIZE 0x80000
103
104enum zblock_type {
105 ZBLOCK_PRG = 0,
106 ZBLOCK_FPGA = 1
107};
108
109struct zfile_header {
110 char name[64];
111 char date[32];
112 char aux[32];
113 u32 n_config;
114 u32 config_offset;
115 u32 n_blocks;
116 u32 block_offset;
117 u32 reserved[9];
118} __attribute__ ((packed));
119
120struct zfile_config {
121 char name[64];
122 u32 mailbox;
123 u32 function;
124 u32 n_blocks;
125 u32 block_list[ZL_MAX_BLOCKS];
126} __attribute__ ((packed));
127
128struct zfile_block {
129 u32 type;
130 u32 file_offset;
131 u32 ram_offset;
132 u32 size;
133} __attribute__ ((packed));
134
135static struct tty_driver *cy_serial_driver;
136
137#ifdef CONFIG_ISA
138/* This is the address lookup table. The driver will probe for
139 Cyclom-Y/ISA boards at all addresses in here. If you want the
140 driver to probe addresses at a different address, add it to
141 this table. If the driver is probing some other board and
142 causing problems, remove the offending address from this table.
143*/
144
145static unsigned int cy_isa_addresses[] = {
146 0xD0000,
147 0xD2000,
148 0xD4000,
149 0xD6000,
150 0xD8000,
151 0xDA000,
152 0xDC000,
153 0xDE000,
154 0, 0, 0, 0, 0, 0, 0, 0
155};
156
157#define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
158
159static long maddr[NR_CARDS];
160static int irq[NR_CARDS];
161
162module_param_array(maddr, long, NULL, 0);
163module_param_array(irq, int, NULL, 0);
164
165#endif /* CONFIG_ISA */
166
167/* This is the per-card data structure containing address, irq, number of
168 channels, etc. This driver supports a maximum of NR_CARDS cards.
169*/
170static struct cyclades_card cy_card[NR_CARDS];
171
172static int cy_next_channel; /* next minor available */
173
174/*
175 * This is used to look up the divisor speeds and the timeouts
176 * We're normally limited to 15 distinct baud rates. The extra
177 * are accessed via settings in info->port.flags.
178 * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
179 * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
180 * HI VHI
181 * 20
182 */
183static const int baud_table[] = {
184 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
185 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
186 230400, 0
187};
188
189static const char baud_co_25[] = { /* 25 MHz clock option table */
190 /* value => 00 01 02 03 04 */
191 /* divide by 8 32 128 512 2048 */
192 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
193 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
194};
195
196static const char baud_bpr_25[] = { /* 25 MHz baud rate period table */
197 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
198 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
199};
200
201static const char baud_co_60[] = { /* 60 MHz clock option table (CD1400 J) */
202 /* value => 00 01 02 03 04 */
203 /* divide by 8 32 128 512 2048 */
204 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
205 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
206 0x00
207};
208
209static const char baud_bpr_60[] = { /* 60 MHz baud rate period table (CD1400 J) */
210 0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
211 0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
212 0x21
213};
214
215static const char baud_cor3[] = { /* receive threshold */
216 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
217 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
218 0x07
219};
220
221/*
222 * The Cyclades driver implements HW flow control as any serial driver.
223 * The cyclades_port structure member rflow and the vector rflow_thr
224 * allows us to take advantage of a special feature in the CD1400 to avoid
225 * data loss even when the system interrupt latency is too high. These flags
226 * are to be used only with very special applications. Setting these flags
227 * requires the use of a special cable (DTR and RTS reversed). In the new
228 * CD1400-based boards (rev. 6.00 or later), there is no need for special
229 * cables.
230 */
231
232static const char rflow_thr[] = { /* rflow threshold */
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
235 0x0a
236};
237
238/* The Cyclom-Ye has placed the sequential chips in non-sequential
239 * address order. This look-up table overcomes that problem.
240 */
241static const unsigned int cy_chip_offset[] = { 0x0000,
242 0x0400,
243 0x0800,
244 0x0C00,
245 0x0200,
246 0x0600,
247 0x0A00,
248 0x0E00
249};
250
251/* PCI related definitions */
252
253#ifdef CONFIG_PCI
254static const struct pci_device_id cy_pci_dev_id[] = {
255 /* PCI < 1Mb */
256 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },
257 /* PCI > 1Mb */
258 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },
259 /* 4Y PCI < 1Mb */
260 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },
261 /* 4Y PCI > 1Mb */
262 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },
263 /* 8Y PCI < 1Mb */
264 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },
265 /* 8Y PCI > 1Mb */
266 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },
267 /* Z PCI < 1Mb */
268 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },
269 /* Z PCI > 1Mb */
270 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },
271 { } /* end of table */
272};
273MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
274#endif
275
276static void cy_start(struct tty_struct *);
277static void cy_set_line_char(struct cyclades_port *, struct tty_struct *);
278static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
279#ifdef CONFIG_ISA
280static unsigned detect_isa_irq(void __iomem *);
281#endif /* CONFIG_ISA */
282
283#ifndef CONFIG_CYZ_INTR
284static void cyz_poll(unsigned long);
285
286/* The Cyclades-Z polling cycle is defined by this variable */
287static long cyz_polling_cycle = CZ_DEF_POLL;
288
289static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
290
291#else /* CONFIG_CYZ_INTR */
292static void cyz_rx_restart(unsigned long);
293static struct timer_list cyz_rx_full_timer[NR_PORTS];
294#endif /* CONFIG_CYZ_INTR */
295
296static inline void cyy_writeb(struct cyclades_port *port, u32 reg, u8 val)
297{
298 struct cyclades_card *card = port->card;
299
300 cy_writeb(port->u.cyy.base_addr + (reg << card->bus_index), val);
301}
302
303static inline u8 cyy_readb(struct cyclades_port *port, u32 reg)
304{
305 struct cyclades_card *card = port->card;
306
307 return readb(port->u.cyy.base_addr + (reg << card->bus_index));
308}
309
310static inline bool cy_is_Z(struct cyclades_card *card)
311{
312 return card->num_chips == (unsigned int)-1;
313}
314
315static inline bool __cyz_fpga_loaded(struct RUNTIME_9060 __iomem *ctl_addr)
316{
317 return readl(&ctl_addr->init_ctrl) & (1 << 17);
318}
319
320static inline bool cyz_fpga_loaded(struct cyclades_card *card)
321{
322 return __cyz_fpga_loaded(card->ctl_addr.p9060);
323}
324
325static inline bool cyz_is_loaded(struct cyclades_card *card)
326{
327 struct FIRM_ID __iomem *fw_id = card->base_addr + ID_ADDRESS;
328
329 return (card->hw_ver == ZO_V1 || cyz_fpga_loaded(card)) &&
330 readl(&fw_id->signature) == ZFIRM_ID;
331}
332
333static inline int serial_paranoia_check(struct cyclades_port *info,
334 const char *name, const char *routine)
335{
336#ifdef SERIAL_PARANOIA_CHECK
337 if (!info) {
338 printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
339 "in %s\n", name, routine);
340 return 1;
341 }
342
343 if (info->magic != CYCLADES_MAGIC) {
344 printk(KERN_WARNING "cyc Warning: bad magic number for serial "
345 "struct (%s) in %s\n", name, routine);
346 return 1;
347 }
348#endif
349 return 0;
350}
351
352/***********************************************************/
353/********* Start of block of Cyclom-Y specific code ********/
354
355/* This routine waits up to 1000 micro-seconds for the previous
356 command to the Cirrus chip to complete and then issues the
357 new command. An error is returned if the previous command
358 didn't finish within the time limit.
359
360 This function is only called from inside spinlock-protected code.
361 */
362static int __cyy_issue_cmd(void __iomem *base_addr, u8 cmd, int index)
363{
364 void __iomem *ccr = base_addr + (CyCCR << index);
365 unsigned int i;
366
367 /* Check to see that the previous command has completed */
368 for (i = 0; i < 100; i++) {
369 if (readb(ccr) == 0)
370 break;
371 udelay(10L);
372 }
373 /* if the CCR never cleared, the previous command
374 didn't finish within the "reasonable time" */
375 if (i == 100)
376 return -1;
377
378 /* Issue the new command */
379 cy_writeb(ccr, cmd);
380
381 return 0;
382}
383
384static inline int cyy_issue_cmd(struct cyclades_port *port, u8 cmd)
385{
386 return __cyy_issue_cmd(port->u.cyy.base_addr, cmd,
387 port->card->bus_index);
388}
389
390#ifdef CONFIG_ISA
391/* ISA interrupt detection code */
392static unsigned detect_isa_irq(void __iomem *address)
393{
394 int irq;
395 unsigned long irqs, flags;
396 int save_xir, save_car;
397 int index = 0; /* IRQ probing is only for ISA */
398
399 /* forget possible initially masked and pending IRQ */
400 irq = probe_irq_off(probe_irq_on());
401
402 /* Clear interrupts on the board first */
403 cy_writeb(address + (Cy_ClrIntr << index), 0);
404 /* Cy_ClrIntr is 0x1800 */
405
406 irqs = probe_irq_on();
407 /* Wait ... */
408 msleep(5);
409
410 /* Enable the Tx interrupts on the CD1400 */
411 local_irq_save(flags);
412 cy_writeb(address + (CyCAR << index), 0);
413 __cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
414
415 cy_writeb(address + (CyCAR << index), 0);
416 cy_writeb(address + (CySRER << index),
417 readb(address + (CySRER << index)) | CyTxRdy);
418 local_irq_restore(flags);
419
420 /* Wait ... */
421 msleep(5);
422
423 /* Check which interrupt is in use */
424 irq = probe_irq_off(irqs);
425
426 /* Clean up */
427 save_xir = (u_char) readb(address + (CyTIR << index));
428 save_car = readb(address + (CyCAR << index));
429 cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
430 cy_writeb(address + (CySRER << index),
431 readb(address + (CySRER << index)) & ~CyTxRdy);
432 cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
433 cy_writeb(address + (CyCAR << index), (save_car));
434 cy_writeb(address + (Cy_ClrIntr << index), 0);
435 /* Cy_ClrIntr is 0x1800 */
436
437 return (irq > 0) ? irq : 0;
438}
439#endif /* CONFIG_ISA */
440
441static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
442 void __iomem *base_addr)
443{
444 struct cyclades_port *info;
445 struct tty_struct *tty;
446 int len, index = cinfo->bus_index;
447 u8 ivr, save_xir, channel, save_car, data, char_count;
448
449#ifdef CY_DEBUG_INTERRUPTS
450 printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
451#endif
452 /* determine the channel & change to that context */
453 save_xir = readb(base_addr + (CyRIR << index));
454 channel = save_xir & CyIRChannel;
455 info = &cinfo->ports[channel + chip * 4];
456 save_car = cyy_readb(info, CyCAR);
457 cyy_writeb(info, CyCAR, save_xir);
458 ivr = cyy_readb(info, CyRIVR) & CyIVRMask;
459
460 tty = tty_port_tty_get(&info->port);
461 /* if there is nowhere to put the data, discard it */
462 if (tty == NULL) {
463 if (ivr == CyIVRRxEx) { /* exception */
464 data = cyy_readb(info, CyRDSR);
465 } else { /* normal character reception */
466 char_count = cyy_readb(info, CyRDCR);
467 while (char_count--)
468 data = cyy_readb(info, CyRDSR);
469 }
470 goto end;
471 }
472 /* there is an open port for this data */
473 if (ivr == CyIVRRxEx) { /* exception */
474 data = cyy_readb(info, CyRDSR);
475
476 /* For statistics only */
477 if (data & CyBREAK)
478 info->icount.brk++;
479 else if (data & CyFRAME)
480 info->icount.frame++;
481 else if (data & CyPARITY)
482 info->icount.parity++;
483 else if (data & CyOVERRUN)
484 info->icount.overrun++;
485
486 if (data & info->ignore_status_mask) {
487 info->icount.rx++;
488 tty_kref_put(tty);
489 return;
490 }
491 if (tty_buffer_request_room(tty, 1)) {
492 if (data & info->read_status_mask) {
493 if (data & CyBREAK) {
494 tty_insert_flip_char(tty,
495 cyy_readb(info, CyRDSR),
496 TTY_BREAK);
497 info->icount.rx++;
498 if (info->port.flags & ASYNC_SAK)
499 do_SAK(tty);
500 } else if (data & CyFRAME) {
501 tty_insert_flip_char(tty,
502 cyy_readb(info, CyRDSR),
503 TTY_FRAME);
504 info->icount.rx++;
505 info->idle_stats.frame_errs++;
506 } else if (data & CyPARITY) {
507 /* Pieces of seven... */
508 tty_insert_flip_char(tty,
509 cyy_readb(info, CyRDSR),
510 TTY_PARITY);
511 info->icount.rx++;
512 info->idle_stats.parity_errs++;
513 } else if (data & CyOVERRUN) {
514 tty_insert_flip_char(tty, 0,
515 TTY_OVERRUN);
516 info->icount.rx++;
517 /* If the flip buffer itself is
518 overflowing, we still lose
519 the next incoming character.
520 */
521 tty_insert_flip_char(tty,
522 cyy_readb(info, CyRDSR),
523 TTY_FRAME);
524 info->icount.rx++;
525 info->idle_stats.overruns++;
526 /* These two conditions may imply */
527 /* a normal read should be done. */
528 /* } else if(data & CyTIMEOUT) { */
529 /* } else if(data & CySPECHAR) { */
530 } else {
531 tty_insert_flip_char(tty, 0,
532 TTY_NORMAL);
533 info->icount.rx++;
534 }
535 } else {
536 tty_insert_flip_char(tty, 0, TTY_NORMAL);
537 info->icount.rx++;
538 }
539 } else {
540 /* there was a software buffer overrun and nothing
541 * could be done about it!!! */
542 info->icount.buf_overrun++;
543 info->idle_stats.overruns++;
544 }
545 } else { /* normal character reception */
546 /* load # chars available from the chip */
547 char_count = cyy_readb(info, CyRDCR);
548
549#ifdef CY_ENABLE_MONITORING
550 ++info->mon.int_count;
551 info->mon.char_count += char_count;
552 if (char_count > info->mon.char_max)
553 info->mon.char_max = char_count;
554 info->mon.char_last = char_count;
555#endif
556 len = tty_buffer_request_room(tty, char_count);
557 while (len--) {
558 data = cyy_readb(info, CyRDSR);
559 tty_insert_flip_char(tty, data, TTY_NORMAL);
560 info->idle_stats.recv_bytes++;
561 info->icount.rx++;
562#ifdef CY_16Y_HACK
563 udelay(10L);
564#endif
565 }
566 info->idle_stats.recv_idle = jiffies;
567 }
568 tty_schedule_flip(tty);
569 tty_kref_put(tty);
570end:
571 /* end of service */
572 cyy_writeb(info, CyRIR, save_xir & 0x3f);
573 cyy_writeb(info, CyCAR, save_car);
574}
575
576static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
577 void __iomem *base_addr)
578{
579 struct cyclades_port *info;
580 struct tty_struct *tty;
581 int char_count, index = cinfo->bus_index;
582 u8 save_xir, channel, save_car, outch;
583
584 /* Since we only get here when the transmit buffer
585 is empty, we know we can always stuff a dozen
586 characters. */
587#ifdef CY_DEBUG_INTERRUPTS
588 printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
589#endif
590
591 /* determine the channel & change to that context */
592 save_xir = readb(base_addr + (CyTIR << index));
593 channel = save_xir & CyIRChannel;
594 save_car = readb(base_addr + (CyCAR << index));
595 cy_writeb(base_addr + (CyCAR << index), save_xir);
596
597 info = &cinfo->ports[channel + chip * 4];
598 tty = tty_port_tty_get(&info->port);
599 if (tty == NULL) {
600 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
601 goto end;
602 }
603
604 /* load the on-chip space for outbound data */
605 char_count = info->xmit_fifo_size;
606
607 if (info->x_char) { /* send special char */
608 outch = info->x_char;
609 cyy_writeb(info, CyTDR, outch);
610 char_count--;
611 info->icount.tx++;
612 info->x_char = 0;
613 }
614
615 if (info->breakon || info->breakoff) {
616 if (info->breakon) {
617 cyy_writeb(info, CyTDR, 0);
618 cyy_writeb(info, CyTDR, 0x81);
619 info->breakon = 0;
620 char_count -= 2;
621 }
622 if (info->breakoff) {
623 cyy_writeb(info, CyTDR, 0);
624 cyy_writeb(info, CyTDR, 0x83);
625 info->breakoff = 0;
626 char_count -= 2;
627 }
628 }
629
630 while (char_count-- > 0) {
631 if (!info->xmit_cnt) {
632 if (cyy_readb(info, CySRER) & CyTxMpty) {
633 cyy_writeb(info, CySRER,
634 cyy_readb(info, CySRER) & ~CyTxMpty);
635 } else {
636 cyy_writeb(info, CySRER, CyTxMpty |
637 (cyy_readb(info, CySRER) & ~CyTxRdy));
638 }
639 goto done;
640 }
641 if (info->port.xmit_buf == NULL) {
642 cyy_writeb(info, CySRER,
643 cyy_readb(info, CySRER) & ~CyTxRdy);
644 goto done;
645 }
646 if (tty->stopped || tty->hw_stopped) {
647 cyy_writeb(info, CySRER,
648 cyy_readb(info, CySRER) & ~CyTxRdy);
649 goto done;
650 }
651 /* Because the Embedded Transmit Commands have been enabled,
652 * we must check to see if the escape character, NULL, is being
653 * sent. If it is, we must ensure that there is room for it to
654 * be doubled in the output stream. Therefore we no longer
655 * advance the pointer when the character is fetched, but
656 * rather wait until after the check for a NULL output
657 * character. This is necessary because there may not be room
658 * for the two chars needed to send a NULL.)
659 */
660 outch = info->port.xmit_buf[info->xmit_tail];
661 if (outch) {
662 info->xmit_cnt--;
663 info->xmit_tail = (info->xmit_tail + 1) &
664 (SERIAL_XMIT_SIZE - 1);
665 cyy_writeb(info, CyTDR, outch);
666 info->icount.tx++;
667 } else {
668 if (char_count > 1) {
669 info->xmit_cnt--;
670 info->xmit_tail = (info->xmit_tail + 1) &
671 (SERIAL_XMIT_SIZE - 1);
672 cyy_writeb(info, CyTDR, outch);
673 cyy_writeb(info, CyTDR, 0);
674 info->icount.tx++;
675 char_count--;
676 }
677 }
678 }
679
680done:
681 tty_wakeup(tty);
682 tty_kref_put(tty);
683end:
684 /* end of service */
685 cyy_writeb(info, CyTIR, save_xir & 0x3f);
686 cyy_writeb(info, CyCAR, save_car);
687}
688
689static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
690 void __iomem *base_addr)
691{
692 struct cyclades_port *info;
693 struct tty_struct *tty;
694 int index = cinfo->bus_index;
695 u8 save_xir, channel, save_car, mdm_change, mdm_status;
696
697 /* determine the channel & change to that context */
698 save_xir = readb(base_addr + (CyMIR << index));
699 channel = save_xir & CyIRChannel;
700 info = &cinfo->ports[channel + chip * 4];
701 save_car = cyy_readb(info, CyCAR);
702 cyy_writeb(info, CyCAR, save_xir);
703
704 mdm_change = cyy_readb(info, CyMISR);
705 mdm_status = cyy_readb(info, CyMSVR1);
706
707 tty = tty_port_tty_get(&info->port);
708 if (!tty)
709 goto end;
710
711 if (mdm_change & CyANY_DELTA) {
712 /* For statistics only */
713 if (mdm_change & CyDCD)
714 info->icount.dcd++;
715 if (mdm_change & CyCTS)
716 info->icount.cts++;
717 if (mdm_change & CyDSR)
718 info->icount.dsr++;
719 if (mdm_change & CyRI)
720 info->icount.rng++;
721
722 wake_up_interruptible(&info->port.delta_msr_wait);
723 }
724
725 if ((mdm_change & CyDCD) && (info->port.flags & ASYNC_CHECK_CD)) {
726 if (mdm_status & CyDCD)
727 wake_up_interruptible(&info->port.open_wait);
728 else
729 tty_hangup(tty);
730 }
731 if ((mdm_change & CyCTS) && (info->port.flags & ASYNC_CTS_FLOW)) {
732 if (tty->hw_stopped) {
733 if (mdm_status & CyCTS) {
734 /* cy_start isn't used
735 because... !!! */
736 tty->hw_stopped = 0;
737 cyy_writeb(info, CySRER,
738 cyy_readb(info, CySRER) | CyTxRdy);
739 tty_wakeup(tty);
740 }
741 } else {
742 if (!(mdm_status & CyCTS)) {
743 /* cy_stop isn't used
744 because ... !!! */
745 tty->hw_stopped = 1;
746 cyy_writeb(info, CySRER,
747 cyy_readb(info, CySRER) & ~CyTxRdy);
748 }
749 }
750 }
751/* if (mdm_change & CyDSR) {
752 }
753 if (mdm_change & CyRI) {
754 }*/
755 tty_kref_put(tty);
756end:
757 /* end of service */
758 cyy_writeb(info, CyMIR, save_xir & 0x3f);
759 cyy_writeb(info, CyCAR, save_car);
760}
761
762/* The real interrupt service routine is called
763 whenever the card wants its hand held--chars
764 received, out buffer empty, modem change, etc.
765 */
766static irqreturn_t cyy_interrupt(int irq, void *dev_id)
767{
768 int status;
769 struct cyclades_card *cinfo = dev_id;
770 void __iomem *base_addr, *card_base_addr;
771 unsigned int chip, too_many, had_work;
772 int index;
773
774 if (unlikely(cinfo == NULL)) {
775#ifdef CY_DEBUG_INTERRUPTS
776 printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",
777 irq);
778#endif
779 return IRQ_NONE; /* spurious interrupt */
780 }
781
782 card_base_addr = cinfo->base_addr;
783 index = cinfo->bus_index;
784
785 /* card was not initialized yet (e.g. DEBUG_SHIRQ) */
786 if (unlikely(card_base_addr == NULL))
787 return IRQ_HANDLED;
788
789 /* This loop checks all chips in the card. Make a note whenever
790 _any_ chip had some work to do, as this is considered an
791 indication that there will be more to do. Only when no chip
792 has any work does this outermost loop exit.
793 */
794 do {
795 had_work = 0;
796 for (chip = 0; chip < cinfo->num_chips; chip++) {
797 base_addr = cinfo->base_addr +
798 (cy_chip_offset[chip] << index);
799 too_many = 0;
800 while ((status = readb(base_addr +
801 (CySVRR << index))) != 0x00) {
802 had_work++;
803 /* The purpose of the following test is to ensure that
804 no chip can monopolize the driver. This forces the
805 chips to be checked in a round-robin fashion (after
806 draining each of a bunch (1000) of characters).
807 */
808 if (1000 < too_many++)
809 break;
810 spin_lock(&cinfo->card_lock);
811 if (status & CySRReceive) /* rx intr */
812 cyy_chip_rx(cinfo, chip, base_addr);
813 if (status & CySRTransmit) /* tx intr */
814 cyy_chip_tx(cinfo, chip, base_addr);
815 if (status & CySRModem) /* modem intr */
816 cyy_chip_modem(cinfo, chip, base_addr);
817 spin_unlock(&cinfo->card_lock);
818 }
819 }
820 } while (had_work);
821
822 /* clear interrupts */
823 spin_lock(&cinfo->card_lock);
824 cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
825 /* Cy_ClrIntr is 0x1800 */
826 spin_unlock(&cinfo->card_lock);
827 return IRQ_HANDLED;
828} /* cyy_interrupt */
829
830static void cyy_change_rts_dtr(struct cyclades_port *info, unsigned int set,
831 unsigned int clear)
832{
833 struct cyclades_card *card = info->card;
834 int channel = info->line - card->first_line;
835 u32 rts, dtr, msvrr, msvrd;
836
837 channel &= 0x03;
838
839 if (info->rtsdtr_inv) {
840 msvrr = CyMSVR2;
841 msvrd = CyMSVR1;
842 rts = CyDTR;
843 dtr = CyRTS;
844 } else {
845 msvrr = CyMSVR1;
846 msvrd = CyMSVR2;
847 rts = CyRTS;
848 dtr = CyDTR;
849 }
850 if (set & TIOCM_RTS) {
851 cyy_writeb(info, CyCAR, channel);
852 cyy_writeb(info, msvrr, rts);
853 }
854 if (clear & TIOCM_RTS) {
855 cyy_writeb(info, CyCAR, channel);
856 cyy_writeb(info, msvrr, ~rts);
857 }
858 if (set & TIOCM_DTR) {
859 cyy_writeb(info, CyCAR, channel);
860 cyy_writeb(info, msvrd, dtr);
861#ifdef CY_DEBUG_DTR
862 printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
863 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
864 cyy_readb(info, CyMSVR1),
865 cyy_readb(info, CyMSVR2));
866#endif
867 }
868 if (clear & TIOCM_DTR) {
869 cyy_writeb(info, CyCAR, channel);
870 cyy_writeb(info, msvrd, ~dtr);
871#ifdef CY_DEBUG_DTR
872 printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
873 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
874 cyy_readb(info, CyMSVR1),
875 cyy_readb(info, CyMSVR2));
876#endif
877 }
878}
879
880/***********************************************************/
881/********* End of block of Cyclom-Y specific code **********/
882/******** Start of block of Cyclades-Z specific code *******/
883/***********************************************************/
884
885static int
886cyz_fetch_msg(struct cyclades_card *cinfo,
887 __u32 *channel, __u8 *cmd, __u32 *param)
888{
889 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
890 unsigned long loc_doorbell;
891
892 loc_doorbell = readl(&cinfo->ctl_addr.p9060->loc_doorbell);
893 if (loc_doorbell) {
894 *cmd = (char)(0xff & loc_doorbell);
895 *channel = readl(&board_ctrl->fwcmd_channel);
896 *param = (__u32) readl(&board_ctrl->fwcmd_param);
897 cy_writel(&cinfo->ctl_addr.p9060->loc_doorbell, 0xffffffff);
898 return 1;
899 }
900 return 0;
901} /* cyz_fetch_msg */
902
903static int
904cyz_issue_cmd(struct cyclades_card *cinfo,
905 __u32 channel, __u8 cmd, __u32 param)
906{
907 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
908 __u32 __iomem *pci_doorbell;
909 unsigned int index;
910
911 if (!cyz_is_loaded(cinfo))
912 return -1;
913
914 index = 0;
915 pci_doorbell = &cinfo->ctl_addr.p9060->pci_doorbell;
916 while ((readl(pci_doorbell) & 0xff) != 0) {
917 if (index++ == 1000)
918 return (int)(readl(pci_doorbell) & 0xff);
919 udelay(50L);
920 }
921 cy_writel(&board_ctrl->hcmd_channel, channel);
922 cy_writel(&board_ctrl->hcmd_param, param);
923 cy_writel(pci_doorbell, (long)cmd);
924
925 return 0;
926} /* cyz_issue_cmd */
927
928static void cyz_handle_rx(struct cyclades_port *info, struct tty_struct *tty)
929{
930 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
931 struct cyclades_card *cinfo = info->card;
932 unsigned int char_count;
933 int len;
934#ifdef BLOCKMOVE
935 unsigned char *buf;
936#else
937 char data;
938#endif
939 __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
940
941 rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
942 rx_put = readl(&buf_ctrl->rx_put);
943 rx_bufsize = readl(&buf_ctrl->rx_bufsize);
944 rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
945 if (rx_put >= rx_get)
946 char_count = rx_put - rx_get;
947 else
948 char_count = rx_put - rx_get + rx_bufsize;
949
950 if (char_count) {
951#ifdef CY_ENABLE_MONITORING
952 info->mon.int_count++;
953 info->mon.char_count += char_count;
954 if (char_count > info->mon.char_max)
955 info->mon.char_max = char_count;
956 info->mon.char_last = char_count;
957#endif
958 if (tty == NULL) {
959 /* flush received characters */
960 new_rx_get = (new_rx_get + char_count) &
961 (rx_bufsize - 1);
962 info->rflush_count++;
963 } else {
964#ifdef BLOCKMOVE
965 /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
966 for performance, but because of buffer boundaries, there
967 may be several steps to the operation */
968 while (1) {
969 len = tty_prepare_flip_string(tty, &buf,
970 char_count);
971 if (!len)
972 break;
973
974 len = min_t(unsigned int, min(len, char_count),
975 rx_bufsize - new_rx_get);
976
977 memcpy_fromio(buf, cinfo->base_addr +
978 rx_bufaddr + new_rx_get, len);
979
980 new_rx_get = (new_rx_get + len) &
981 (rx_bufsize - 1);
982 char_count -= len;
983 info->icount.rx += len;
984 info->idle_stats.recv_bytes += len;
985 }
986#else
987 len = tty_buffer_request_room(tty, char_count);
988 while (len--) {
989 data = readb(cinfo->base_addr + rx_bufaddr +
990 new_rx_get);
991 new_rx_get = (new_rx_get + 1) &
992 (rx_bufsize - 1);
993 tty_insert_flip_char(tty, data, TTY_NORMAL);
994 info->idle_stats.recv_bytes++;
995 info->icount.rx++;
996 }
997#endif
998#ifdef CONFIG_CYZ_INTR
999 /* Recalculate the number of chars in the RX buffer and issue
1000 a cmd in case it's higher than the RX high water mark */
1001 rx_put = readl(&buf_ctrl->rx_put);
1002 if (rx_put >= rx_get)
1003 char_count = rx_put - rx_get;
1004 else
1005 char_count = rx_put - rx_get + rx_bufsize;
1006 if (char_count >= readl(&buf_ctrl->rx_threshold) &&
1007 !timer_pending(&cyz_rx_full_timer[
1008 info->line]))
1009 mod_timer(&cyz_rx_full_timer[info->line],
1010 jiffies + 1);
1011#endif
1012 info->idle_stats.recv_idle = jiffies;
1013 tty_schedule_flip(tty);
1014 }
1015 /* Update rx_get */
1016 cy_writel(&buf_ctrl->rx_get, new_rx_get);
1017 }
1018}
1019
1020static void cyz_handle_tx(struct cyclades_port *info, struct tty_struct *tty)
1021{
1022 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
1023 struct cyclades_card *cinfo = info->card;
1024 u8 data;
1025 unsigned int char_count;
1026#ifdef BLOCKMOVE
1027 int small_count;
1028#endif
1029 __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
1030
1031 if (info->xmit_cnt <= 0) /* Nothing to transmit */
1032 return;
1033
1034 tx_get = readl(&buf_ctrl->tx_get);
1035 tx_put = readl(&buf_ctrl->tx_put);
1036 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1037 tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
1038 if (tx_put >= tx_get)
1039 char_count = tx_get - tx_put - 1 + tx_bufsize;
1040 else
1041 char_count = tx_get - tx_put - 1;
1042
1043 if (char_count) {
1044
1045 if (tty == NULL)
1046 goto ztxdone;
1047
1048 if (info->x_char) { /* send special char */
1049 data = info->x_char;
1050
1051 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1052 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1053 info->x_char = 0;
1054 char_count--;
1055 info->icount.tx++;
1056 }
1057#ifdef BLOCKMOVE
1058 while (0 < (small_count = min_t(unsigned int,
1059 tx_bufsize - tx_put, min_t(unsigned int,
1060 (SERIAL_XMIT_SIZE - info->xmit_tail),
1061 min_t(unsigned int, info->xmit_cnt,
1062 char_count))))) {
1063
1064 memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1065 tx_put),
1066 &info->port.xmit_buf[info->xmit_tail],
1067 small_count);
1068
1069 tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1070 char_count -= small_count;
1071 info->icount.tx += small_count;
1072 info->xmit_cnt -= small_count;
1073 info->xmit_tail = (info->xmit_tail + small_count) &
1074 (SERIAL_XMIT_SIZE - 1);
1075 }
1076#else
1077 while (info->xmit_cnt && char_count) {
1078 data = info->port.xmit_buf[info->xmit_tail];
1079 info->xmit_cnt--;
1080 info->xmit_tail = (info->xmit_tail + 1) &
1081 (SERIAL_XMIT_SIZE - 1);
1082
1083 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1084 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1085 char_count--;
1086 info->icount.tx++;
1087 }
1088#endif
1089 tty_wakeup(tty);
1090ztxdone:
1091 /* Update tx_put */
1092 cy_writel(&buf_ctrl->tx_put, tx_put);
1093 }
1094}
1095
1096static void cyz_handle_cmd(struct cyclades_card *cinfo)
1097{
1098 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
1099 struct tty_struct *tty;
1100 struct cyclades_port *info;
1101 __u32 channel, param, fw_ver;
1102 __u8 cmd;
1103 int special_count;
1104 int delta_count;
1105
1106 fw_ver = readl(&board_ctrl->fw_version);
1107
1108 while (cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1109 special_count = 0;
1110 delta_count = 0;
1111 info = &cinfo->ports[channel];
1112 tty = tty_port_tty_get(&info->port);
1113 if (tty == NULL)
1114 continue;
1115
1116 switch (cmd) {
1117 case C_CM_PR_ERROR:
1118 tty_insert_flip_char(tty, 0, TTY_PARITY);
1119 info->icount.rx++;
1120 special_count++;
1121 break;
1122 case C_CM_FR_ERROR:
1123 tty_insert_flip_char(tty, 0, TTY_FRAME);
1124 info->icount.rx++;
1125 special_count++;
1126 break;
1127 case C_CM_RXBRK:
1128 tty_insert_flip_char(tty, 0, TTY_BREAK);
1129 info->icount.rx++;
1130 special_count++;
1131 break;
1132 case C_CM_MDCD:
1133 info->icount.dcd++;
1134 delta_count++;
1135 if (info->port.flags & ASYNC_CHECK_CD) {
1136 u32 dcd = fw_ver > 241 ? param :
1137 readl(&info->u.cyz.ch_ctrl->rs_status);
1138 if (dcd & C_RS_DCD)
1139 wake_up_interruptible(&info->port.open_wait);
1140 else
1141 tty_hangup(tty);
1142 }
1143 break;
1144 case C_CM_MCTS:
1145 info->icount.cts++;
1146 delta_count++;
1147 break;
1148 case C_CM_MRI:
1149 info->icount.rng++;
1150 delta_count++;
1151 break;
1152 case C_CM_MDSR:
1153 info->icount.dsr++;
1154 delta_count++;
1155 break;
1156#ifdef Z_WAKE
1157 case C_CM_IOCTLW:
1158 complete(&info->shutdown_wait);
1159 break;
1160#endif
1161#ifdef CONFIG_CYZ_INTR
1162 case C_CM_RXHIWM:
1163 case C_CM_RXNNDT:
1164 case C_CM_INTBACK2:
1165 /* Reception Interrupt */
1166#ifdef CY_DEBUG_INTERRUPTS
1167 printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1168 "port %ld\n", info->card, channel);
1169#endif
1170 cyz_handle_rx(info, tty);
1171 break;
1172 case C_CM_TXBEMPTY:
1173 case C_CM_TXLOWWM:
1174 case C_CM_INTBACK:
1175 /* Transmission Interrupt */
1176#ifdef CY_DEBUG_INTERRUPTS
1177 printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1178 "port %ld\n", info->card, channel);
1179#endif
1180 cyz_handle_tx(info, tty);
1181 break;
1182#endif /* CONFIG_CYZ_INTR */
1183 case C_CM_FATAL:
1184 /* should do something with this !!! */
1185 break;
1186 default:
1187 break;
1188 }
1189 if (delta_count)
1190 wake_up_interruptible(&info->port.delta_msr_wait);
1191 if (special_count)
1192 tty_schedule_flip(tty);
1193 tty_kref_put(tty);
1194 }
1195}
1196
1197#ifdef CONFIG_CYZ_INTR
1198static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1199{
1200 struct cyclades_card *cinfo = dev_id;
1201
1202 if (unlikely(!cyz_is_loaded(cinfo))) {
1203#ifdef CY_DEBUG_INTERRUPTS
1204 printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1205 "(IRQ%d).\n", irq);
1206#endif
1207 return IRQ_NONE;
1208 }
1209
1210 /* Handle the interrupts */
1211 cyz_handle_cmd(cinfo);
1212
1213 return IRQ_HANDLED;
1214} /* cyz_interrupt */
1215
1216static void cyz_rx_restart(unsigned long arg)
1217{
1218 struct cyclades_port *info = (struct cyclades_port *)arg;
1219 struct cyclades_card *card = info->card;
1220 int retval;
1221 __u32 channel = info->line - card->first_line;
1222 unsigned long flags;
1223
1224 spin_lock_irqsave(&card->card_lock, flags);
1225 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1226 if (retval != 0) {
1227 printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1228 info->line, retval);
1229 }
1230 spin_unlock_irqrestore(&card->card_lock, flags);
1231}
1232
1233#else /* CONFIG_CYZ_INTR */
1234
1235static void cyz_poll(unsigned long arg)
1236{
1237 struct cyclades_card *cinfo;
1238 struct cyclades_port *info;
1239 unsigned long expires = jiffies + HZ;
1240 unsigned int port, card;
1241
1242 for (card = 0; card < NR_CARDS; card++) {
1243 cinfo = &cy_card[card];
1244
1245 if (!cy_is_Z(cinfo))
1246 continue;
1247 if (!cyz_is_loaded(cinfo))
1248 continue;
1249
1250 /* Skip first polling cycle to avoid racing conditions with the FW */
1251 if (!cinfo->intr_enabled) {
1252 cinfo->intr_enabled = 1;
1253 continue;
1254 }
1255
1256 cyz_handle_cmd(cinfo);
1257
1258 for (port = 0; port < cinfo->nports; port++) {
1259 struct tty_struct *tty;
1260
1261 info = &cinfo->ports[port];
1262 tty = tty_port_tty_get(&info->port);
1263 /* OK to pass NULL to the handle functions below.
1264 They need to drop the data in that case. */
1265
1266 if (!info->throttle)
1267 cyz_handle_rx(info, tty);
1268 cyz_handle_tx(info, tty);
1269 tty_kref_put(tty);
1270 }
1271 /* poll every 'cyz_polling_cycle' period */
1272 expires = jiffies + cyz_polling_cycle;
1273 }
1274 mod_timer(&cyz_timerlist, expires);
1275} /* cyz_poll */
1276
1277#endif /* CONFIG_CYZ_INTR */
1278
1279/********** End of block of Cyclades-Z specific code *********/
1280/***********************************************************/
1281
1282/* This is called whenever a port becomes active;
1283 interrupts are enabled and DTR & RTS are turned on.
1284 */
1285static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
1286{
1287 struct cyclades_card *card;
1288 unsigned long flags;
1289 int retval = 0;
1290 int channel;
1291 unsigned long page;
1292
1293 card = info->card;
1294 channel = info->line - card->first_line;
1295
1296 page = get_zeroed_page(GFP_KERNEL);
1297 if (!page)
1298 return -ENOMEM;
1299
1300 spin_lock_irqsave(&card->card_lock, flags);
1301
1302 if (info->port.flags & ASYNC_INITIALIZED)
1303 goto errout;
1304
1305 if (!info->type) {
1306 set_bit(TTY_IO_ERROR, &tty->flags);
1307 goto errout;
1308 }
1309
1310 if (info->port.xmit_buf)
1311 free_page(page);
1312 else
1313 info->port.xmit_buf = (unsigned char *)page;
1314
1315 spin_unlock_irqrestore(&card->card_lock, flags);
1316
1317 cy_set_line_char(info, tty);
1318
1319 if (!cy_is_Z(card)) {
1320 channel &= 0x03;
1321
1322 spin_lock_irqsave(&card->card_lock, flags);
1323
1324 cyy_writeb(info, CyCAR, channel);
1325
1326 cyy_writeb(info, CyRTPR,
1327 (info->default_timeout ? info->default_timeout : 0x02));
1328 /* 10ms rx timeout */
1329
1330 cyy_issue_cmd(info, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR);
1331
1332 cyy_change_rts_dtr(info, TIOCM_RTS | TIOCM_DTR, 0);
1333
1334 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyRxData);
1335 } else {
1336 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1337
1338 if (!cyz_is_loaded(card))
1339 return -ENODEV;
1340
1341#ifdef CY_DEBUG_OPEN
1342 printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1343 "base_addr %p\n", card, channel, card->base_addr);
1344#endif
1345 spin_lock_irqsave(&card->card_lock, flags);
1346
1347 cy_writel(&ch_ctrl->op_mode, C_CH_ENABLE);
1348#ifdef Z_WAKE
1349#ifdef CONFIG_CYZ_INTR
1350 cy_writel(&ch_ctrl->intr_enable,
1351 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1352 C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1353#else
1354 cy_writel(&ch_ctrl->intr_enable,
1355 C_IN_IOCTLW | C_IN_MDCD);
1356#endif /* CONFIG_CYZ_INTR */
1357#else
1358#ifdef CONFIG_CYZ_INTR
1359 cy_writel(&ch_ctrl->intr_enable,
1360 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1361 C_IN_RXNNDT | C_IN_MDCD);
1362#else
1363 cy_writel(&ch_ctrl->intr_enable, C_IN_MDCD);
1364#endif /* CONFIG_CYZ_INTR */
1365#endif /* Z_WAKE */
1366
1367 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1368 if (retval != 0) {
1369 printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1370 "%x\n", info->line, retval);
1371 }
1372
1373 /* Flush RX buffers before raising DTR and RTS */
1374 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1375 if (retval != 0) {
1376 printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1377 "%x\n", info->line, retval);
1378 }
1379
1380 /* set timeout !!! */
1381 /* set RTS and DTR !!! */
1382 tty_port_raise_dtr_rts(&info->port);
1383
1384 /* enable send, recv, modem !!! */
1385 }
1386
1387 info->port.flags |= ASYNC_INITIALIZED;
1388
1389 clear_bit(TTY_IO_ERROR, &tty->flags);
1390 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1391 info->breakon = info->breakoff = 0;
1392 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1393 info->idle_stats.in_use =
1394 info->idle_stats.recv_idle =
1395 info->idle_stats.xmit_idle = jiffies;
1396
1397 spin_unlock_irqrestore(&card->card_lock, flags);
1398
1399#ifdef CY_DEBUG_OPEN
1400 printk(KERN_DEBUG "cyc startup done\n");
1401#endif
1402 return 0;
1403
1404errout:
1405 spin_unlock_irqrestore(&card->card_lock, flags);
1406 free_page(page);
1407 return retval;
1408} /* startup */
1409
1410static void start_xmit(struct cyclades_port *info)
1411{
1412 struct cyclades_card *card = info->card;
1413 unsigned long flags;
1414 int channel = info->line - card->first_line;
1415
1416 if (!cy_is_Z(card)) {
1417 spin_lock_irqsave(&card->card_lock, flags);
1418 cyy_writeb(info, CyCAR, channel & 0x03);
1419 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
1420 spin_unlock_irqrestore(&card->card_lock, flags);
1421 } else {
1422#ifdef CONFIG_CYZ_INTR
1423 int retval;
1424
1425 spin_lock_irqsave(&card->card_lock, flags);
1426 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
1427 if (retval != 0) {
1428 printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
1429 "%x\n", info->line, retval);
1430 }
1431 spin_unlock_irqrestore(&card->card_lock, flags);
1432#else /* CONFIG_CYZ_INTR */
1433 /* Don't have to do anything at this time */
1434#endif /* CONFIG_CYZ_INTR */
1435 }
1436} /* start_xmit */
1437
1438/*
1439 * This routine shuts down a serial port; interrupts are disabled,
1440 * and DTR is dropped if the hangup on close termio flag is on.
1441 */
1442static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
1443{
1444 struct cyclades_card *card;
1445 unsigned long flags;
1446
1447 if (!(info->port.flags & ASYNC_INITIALIZED))
1448 return;
1449
1450 card = info->card;
1451 if (!cy_is_Z(card)) {
1452 spin_lock_irqsave(&card->card_lock, flags);
1453
1454 /* Clear delta_msr_wait queue to avoid mem leaks. */
1455 wake_up_interruptible(&info->port.delta_msr_wait);
1456
1457 if (info->port.xmit_buf) {
1458 unsigned char *temp;
1459 temp = info->port.xmit_buf;
1460 info->port.xmit_buf = NULL;
1461 free_page((unsigned long)temp);
1462 }
1463 if (tty->termios->c_cflag & HUPCL)
1464 cyy_change_rts_dtr(info, 0, TIOCM_RTS | TIOCM_DTR);
1465
1466 cyy_issue_cmd(info, CyCHAN_CTL | CyDIS_RCVR);
1467 /* it may be appropriate to clear _XMIT at
1468 some later date (after testing)!!! */
1469
1470 set_bit(TTY_IO_ERROR, &tty->flags);
1471 info->port.flags &= ~ASYNC_INITIALIZED;
1472 spin_unlock_irqrestore(&card->card_lock, flags);
1473 } else {
1474#ifdef CY_DEBUG_OPEN
1475 int channel = info->line - card->first_line;
1476 printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
1477 "base_addr %p\n", card, channel, card->base_addr);
1478#endif
1479
1480 if (!cyz_is_loaded(card))
1481 return;
1482
1483 spin_lock_irqsave(&card->card_lock, flags);
1484
1485 if (info->port.xmit_buf) {
1486 unsigned char *temp;
1487 temp = info->port.xmit_buf;
1488 info->port.xmit_buf = NULL;
1489 free_page((unsigned long)temp);
1490 }
1491
1492 if (tty->termios->c_cflag & HUPCL)
1493 tty_port_lower_dtr_rts(&info->port);
1494
1495 set_bit(TTY_IO_ERROR, &tty->flags);
1496 info->port.flags &= ~ASYNC_INITIALIZED;
1497
1498 spin_unlock_irqrestore(&card->card_lock, flags);
1499 }
1500
1501#ifdef CY_DEBUG_OPEN
1502 printk(KERN_DEBUG "cyc shutdown done\n");
1503#endif
1504} /* shutdown */
1505
1506/*
1507 * ------------------------------------------------------------
1508 * cy_open() and friends
1509 * ------------------------------------------------------------
1510 */
1511
1512/*
1513 * This routine is called whenever a serial port is opened. It
1514 * performs the serial-specific initialization for the tty structure.
1515 */
1516static int cy_open(struct tty_struct *tty, struct file *filp)
1517{
1518 struct cyclades_port *info;
1519 unsigned int i, line;
1520 int retval;
1521
1522 line = tty->index;
1523 if (tty->index < 0 || NR_PORTS <= line)
1524 return -ENODEV;
1525
1526 for (i = 0; i < NR_CARDS; i++)
1527 if (line < cy_card[i].first_line + cy_card[i].nports &&
1528 line >= cy_card[i].first_line)
1529 break;
1530 if (i >= NR_CARDS)
1531 return -ENODEV;
1532 info = &cy_card[i].ports[line - cy_card[i].first_line];
1533 if (info->line < 0)
1534 return -ENODEV;
1535
1536 /* If the card's firmware hasn't been loaded,
1537 treat it as absent from the system. This
1538 will make the user pay attention.
1539 */
1540 if (cy_is_Z(info->card)) {
1541 struct cyclades_card *cinfo = info->card;
1542 struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
1543
1544 if (!cyz_is_loaded(cinfo)) {
1545 if (cinfo->hw_ver == ZE_V1 && cyz_fpga_loaded(cinfo) &&
1546 readl(&firm_id->signature) ==
1547 ZFIRM_HLT) {
1548 printk(KERN_ERR "cyc:Cyclades-Z Error: you "
1549 "need an external power supply for "
1550 "this number of ports.\nFirmware "
1551 "halted.\n");
1552 } else {
1553 printk(KERN_ERR "cyc:Cyclades-Z firmware not "
1554 "yet loaded\n");
1555 }
1556 return -ENODEV;
1557 }
1558#ifdef CONFIG_CYZ_INTR
1559 else {
1560 /* In case this Z board is operating in interrupt mode, its
1561 interrupts should be enabled as soon as the first open
1562 happens to one of its ports. */
1563 if (!cinfo->intr_enabled) {
1564 u16 intr;
1565
1566 /* Enable interrupts on the PLX chip */
1567 intr = readw(&cinfo->ctl_addr.p9060->
1568 intr_ctrl_stat) | 0x0900;
1569 cy_writew(&cinfo->ctl_addr.p9060->
1570 intr_ctrl_stat, intr);
1571 /* Enable interrupts on the FW */
1572 retval = cyz_issue_cmd(cinfo, 0,
1573 C_CM_IRQ_ENBL, 0L);
1574 if (retval != 0) {
1575 printk(KERN_ERR "cyc:IRQ enable retval "
1576 "was %x\n", retval);
1577 }
1578 cinfo->intr_enabled = 1;
1579 }
1580 }
1581#endif /* CONFIG_CYZ_INTR */
1582 /* Make sure this Z port really exists in hardware */
1583 if (info->line > (cinfo->first_line + cinfo->nports - 1))
1584 return -ENODEV;
1585 }
1586#ifdef CY_DEBUG_OTHER
1587 printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
1588#endif
1589 tty->driver_data = info;
1590 if (serial_paranoia_check(info, tty->name, "cy_open"))
1591 return -ENODEV;
1592
1593#ifdef CY_DEBUG_OPEN
1594 printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
1595 info->port.count);
1596#endif
1597 info->port.count++;
1598#ifdef CY_DEBUG_COUNT
1599 printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
1600 current->pid, info->port.count);
1601#endif
1602
1603 /*
1604 * If the port is the middle of closing, bail out now
1605 */
1606 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1607 wait_event_interruptible_tty(info->port.close_wait,
1608 !(info->port.flags & ASYNC_CLOSING));
1609 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1610 }
1611
1612 /*
1613 * Start up serial port
1614 */
1615 retval = cy_startup(info, tty);
1616 if (retval)
1617 return retval;
1618
1619 retval = tty_port_block_til_ready(&info->port, tty, filp);
1620 if (retval) {
1621#ifdef CY_DEBUG_OPEN
1622 printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
1623 "with %d\n", retval);
1624#endif
1625 return retval;
1626 }
1627
1628 info->throttle = 0;
1629 tty_port_tty_set(&info->port, tty);
1630
1631#ifdef CY_DEBUG_OPEN
1632 printk(KERN_DEBUG "cyc:cy_open done\n");
1633#endif
1634 return 0;
1635} /* cy_open */
1636
1637/*
1638 * cy_wait_until_sent() --- wait until the transmitter is empty
1639 */
1640static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1641{
1642 struct cyclades_card *card;
1643 struct cyclades_port *info = tty->driver_data;
1644 unsigned long orig_jiffies;
1645 int char_time;
1646
1647 if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
1648 return;
1649
1650 if (info->xmit_fifo_size == 0)
1651 return; /* Just in case.... */
1652
1653 orig_jiffies = jiffies;
1654 /*
1655 * Set the check interval to be 1/5 of the estimated time to
1656 * send a single character, and make it at least 1. The check
1657 * interval should also be less than the timeout.
1658 *
1659 * Note: we have to use pretty tight timings here to satisfy
1660 * the NIST-PCTS.
1661 */
1662 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1663 char_time = char_time / 5;
1664 if (char_time <= 0)
1665 char_time = 1;
1666 if (timeout < 0)
1667 timeout = 0;
1668 if (timeout)
1669 char_time = min(char_time, timeout);
1670 /*
1671 * If the transmitter hasn't cleared in twice the approximate
1672 * amount of time to send the entire FIFO, it probably won't
1673 * ever clear. This assumes the UART isn't doing flow
1674 * control, which is currently the case. Hence, if it ever
1675 * takes longer than info->timeout, this is probably due to a
1676 * UART bug of some kind. So, we clamp the timeout parameter at
1677 * 2*info->timeout.
1678 */
1679 if (!timeout || timeout > 2 * info->timeout)
1680 timeout = 2 * info->timeout;
1681#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1682 printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
1683 timeout, char_time, jiffies);
1684#endif
1685 card = info->card;
1686 if (!cy_is_Z(card)) {
1687 while (cyy_readb(info, CySRER) & CyTxRdy) {
1688#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1689 printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
1690#endif
1691 if (msleep_interruptible(jiffies_to_msecs(char_time)))
1692 break;
1693 if (timeout && time_after(jiffies, orig_jiffies +
1694 timeout))
1695 break;
1696 }
1697 }
1698 /* Run one more char cycle */
1699 msleep_interruptible(jiffies_to_msecs(char_time * 5));
1700#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1701 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
1702#endif
1703}
1704
1705static void cy_flush_buffer(struct tty_struct *tty)
1706{
1707 struct cyclades_port *info = tty->driver_data;
1708 struct cyclades_card *card;
1709 int channel, retval;
1710 unsigned long flags;
1711
1712#ifdef CY_DEBUG_IO
1713 printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
1714#endif
1715
1716 if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1717 return;
1718
1719 card = info->card;
1720 channel = info->line - card->first_line;
1721
1722 spin_lock_irqsave(&card->card_lock, flags);
1723 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1724 spin_unlock_irqrestore(&card->card_lock, flags);
1725
1726 if (cy_is_Z(card)) { /* If it is a Z card, flush the on-board
1727 buffers as well */
1728 spin_lock_irqsave(&card->card_lock, flags);
1729 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
1730 if (retval != 0) {
1731 printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
1732 "was %x\n", info->line, retval);
1733 }
1734 spin_unlock_irqrestore(&card->card_lock, flags);
1735 }
1736 tty_wakeup(tty);
1737} /* cy_flush_buffer */
1738
1739
1740static void cy_do_close(struct tty_port *port)
1741{
1742 struct cyclades_port *info = container_of(port, struct cyclades_port,
1743 port);
1744 struct cyclades_card *card;
1745 unsigned long flags;
1746 int channel;
1747
1748 card = info->card;
1749 channel = info->line - card->first_line;
1750 spin_lock_irqsave(&card->card_lock, flags);
1751
1752 if (!cy_is_Z(card)) {
1753 /* Stop accepting input */
1754 cyy_writeb(info, CyCAR, channel & 0x03);
1755 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyRxData);
1756 if (info->port.flags & ASYNC_INITIALIZED) {
1757 /* Waiting for on-board buffers to be empty before
1758 closing the port */
1759 spin_unlock_irqrestore(&card->card_lock, flags);
1760 cy_wait_until_sent(port->tty, info->timeout);
1761 spin_lock_irqsave(&card->card_lock, flags);
1762 }
1763 } else {
1764#ifdef Z_WAKE
1765 /* Waiting for on-board buffers to be empty before closing
1766 the port */
1767 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1768 int retval;
1769
1770 if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
1771 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
1772 if (retval != 0) {
1773 printk(KERN_DEBUG "cyc:cy_close retval on "
1774 "ttyC%d was %x\n", info->line, retval);
1775 }
1776 spin_unlock_irqrestore(&card->card_lock, flags);
1777 wait_for_completion_interruptible(&info->shutdown_wait);
1778 spin_lock_irqsave(&card->card_lock, flags);
1779 }
1780#endif
1781 }
1782 spin_unlock_irqrestore(&card->card_lock, flags);
1783 cy_shutdown(info, port->tty);
1784}
1785
1786/*
1787 * This routine is called when a particular tty device is closed.
1788 */
1789static void cy_close(struct tty_struct *tty, struct file *filp)
1790{
1791 struct cyclades_port *info = tty->driver_data;
1792 if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
1793 return;
1794 tty_port_close(&info->port, tty, filp);
1795} /* cy_close */
1796
1797/* This routine gets called when tty_write has put something into
1798 * the write_queue. The characters may come from user space or
1799 * kernel space.
1800 *
1801 * This routine will return the number of characters actually
1802 * accepted for writing.
1803 *
1804 * If the port is not already transmitting stuff, start it off by
1805 * enabling interrupts. The interrupt service routine will then
1806 * ensure that the characters are sent.
1807 * If the port is already active, there is no need to kick it.
1808 *
1809 */
1810static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1811{
1812 struct cyclades_port *info = tty->driver_data;
1813 unsigned long flags;
1814 int c, ret = 0;
1815
1816#ifdef CY_DEBUG_IO
1817 printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
1818#endif
1819
1820 if (serial_paranoia_check(info, tty->name, "cy_write"))
1821 return 0;
1822
1823 if (!info->port.xmit_buf)
1824 return 0;
1825
1826 spin_lock_irqsave(&info->card->card_lock, flags);
1827 while (1) {
1828 c = min(count, (int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1));
1829 c = min(c, (int)(SERIAL_XMIT_SIZE - info->xmit_head));
1830
1831 if (c <= 0)
1832 break;
1833
1834 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1835 info->xmit_head = (info->xmit_head + c) &
1836 (SERIAL_XMIT_SIZE - 1);
1837 info->xmit_cnt += c;
1838 buf += c;
1839 count -= c;
1840 ret += c;
1841 }
1842 spin_unlock_irqrestore(&info->card->card_lock, flags);
1843
1844 info->idle_stats.xmit_bytes += ret;
1845 info->idle_stats.xmit_idle = jiffies;
1846
1847 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1848 start_xmit(info);
1849
1850 return ret;
1851} /* cy_write */
1852
1853/*
1854 * This routine is called by the kernel to write a single
1855 * character to the tty device. If the kernel uses this routine,
1856 * it must call the flush_chars() routine (if defined) when it is
1857 * done stuffing characters into the driver. If there is no room
1858 * in the queue, the character is ignored.
1859 */
1860static int cy_put_char(struct tty_struct *tty, unsigned char ch)
1861{
1862 struct cyclades_port *info = tty->driver_data;
1863 unsigned long flags;
1864
1865#ifdef CY_DEBUG_IO
1866 printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
1867#endif
1868
1869 if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1870 return 0;
1871
1872 if (!info->port.xmit_buf)
1873 return 0;
1874
1875 spin_lock_irqsave(&info->card->card_lock, flags);
1876 if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
1877 spin_unlock_irqrestore(&info->card->card_lock, flags);
1878 return 0;
1879 }
1880
1881 info->port.xmit_buf[info->xmit_head++] = ch;
1882 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1883 info->xmit_cnt++;
1884 info->idle_stats.xmit_bytes++;
1885 info->idle_stats.xmit_idle = jiffies;
1886 spin_unlock_irqrestore(&info->card->card_lock, flags);
1887 return 1;
1888} /* cy_put_char */
1889
1890/*
1891 * This routine is called by the kernel after it has written a
1892 * series of characters to the tty device using put_char().
1893 */
1894static void cy_flush_chars(struct tty_struct *tty)
1895{
1896 struct cyclades_port *info = tty->driver_data;
1897
1898#ifdef CY_DEBUG_IO
1899 printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
1900#endif
1901
1902 if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1903 return;
1904
1905 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1906 !info->port.xmit_buf)
1907 return;
1908
1909 start_xmit(info);
1910} /* cy_flush_chars */
1911
1912/*
1913 * This routine returns the numbers of characters the tty driver
1914 * will accept for queuing to be written. This number is subject
1915 * to change as output buffers get emptied, or if the output flow
1916 * control is activated.
1917 */
1918static int cy_write_room(struct tty_struct *tty)
1919{
1920 struct cyclades_port *info = tty->driver_data;
1921 int ret;
1922
1923#ifdef CY_DEBUG_IO
1924 printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
1925#endif
1926
1927 if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1928 return 0;
1929 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1930 if (ret < 0)
1931 ret = 0;
1932 return ret;
1933} /* cy_write_room */
1934
1935static int cy_chars_in_buffer(struct tty_struct *tty)
1936{
1937 struct cyclades_port *info = tty->driver_data;
1938
1939 if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1940 return 0;
1941
1942#ifdef Z_EXT_CHARS_IN_BUFFER
1943 if (!cy_is_Z(info->card)) {
1944#endif /* Z_EXT_CHARS_IN_BUFFER */
1945#ifdef CY_DEBUG_IO
1946 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
1947 info->line, info->xmit_cnt);
1948#endif
1949 return info->xmit_cnt;
1950#ifdef Z_EXT_CHARS_IN_BUFFER
1951 } else {
1952 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
1953 int char_count;
1954 __u32 tx_put, tx_get, tx_bufsize;
1955
1956 tx_get = readl(&buf_ctrl->tx_get);
1957 tx_put = readl(&buf_ctrl->tx_put);
1958 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1959 if (tx_put >= tx_get)
1960 char_count = tx_put - tx_get;
1961 else
1962 char_count = tx_put - tx_get + tx_bufsize;
1963#ifdef CY_DEBUG_IO
1964 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
1965 info->line, info->xmit_cnt + char_count);
1966#endif
1967 return info->xmit_cnt + char_count;
1968 }
1969#endif /* Z_EXT_CHARS_IN_BUFFER */
1970} /* cy_chars_in_buffer */
1971
1972/*
1973 * ------------------------------------------------------------
1974 * cy_ioctl() and friends
1975 * ------------------------------------------------------------
1976 */
1977
1978static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
1979{
1980 int co, co_val, bpr;
1981 __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
1982 25000000);
1983
1984 if (baud == 0) {
1985 info->tbpr = info->tco = info->rbpr = info->rco = 0;
1986 return;
1987 }
1988
1989 /* determine which prescaler to use */
1990 for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
1991 if (cy_clock / co_val / baud > 63)
1992 break;
1993 }
1994
1995 bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
1996 if (bpr > 255)
1997 bpr = 255;
1998
1999 info->tbpr = info->rbpr = bpr;
2000 info->tco = info->rco = co;
2001}
2002
2003/*
2004 * This routine finds or computes the various line characteristics.
2005 * It used to be called config_setup
2006 */
2007static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
2008{
2009 struct cyclades_card *card;
2010 unsigned long flags;
2011 int channel;
2012 unsigned cflag, iflag;
2013 int baud, baud_rate = 0;
2014 int i;
2015
2016 if (!tty->termios) /* XXX can this happen at all? */
2017 return;
2018
2019 if (info->line == -1)
2020 return;
2021
2022 cflag = tty->termios->c_cflag;
2023 iflag = tty->termios->c_iflag;
2024
2025 /*
2026 * Set up the tty->alt_speed kludge
2027 */
2028 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2029 tty->alt_speed = 57600;
2030 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2031 tty->alt_speed = 115200;
2032 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2033 tty->alt_speed = 230400;
2034 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2035 tty->alt_speed = 460800;
2036
2037 card = info->card;
2038 channel = info->line - card->first_line;
2039
2040 if (!cy_is_Z(card)) {
2041 u32 cflags;
2042
2043 /* baud rate */
2044 baud = tty_get_baud_rate(tty);
2045 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2046 ASYNC_SPD_CUST) {
2047 if (info->custom_divisor)
2048 baud_rate = info->baud / info->custom_divisor;
2049 else
2050 baud_rate = info->baud;
2051 } else if (baud > CD1400_MAX_SPEED) {
2052 baud = CD1400_MAX_SPEED;
2053 }
2054 /* find the baud index */
2055 for (i = 0; i < 20; i++) {
2056 if (baud == baud_table[i])
2057 break;
2058 }
2059 if (i == 20)
2060 i = 19; /* CD1400_MAX_SPEED */
2061
2062 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2063 ASYNC_SPD_CUST) {
2064 cyy_baud_calc(info, baud_rate);
2065 } else {
2066 if (info->chip_rev >= CD1400_REV_J) {
2067 /* It is a CD1400 rev. J or later */
2068 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
2069 info->tco = baud_co_60[i]; /* Tx CO */
2070 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
2071 info->rco = baud_co_60[i]; /* Rx CO */
2072 } else {
2073 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
2074 info->tco = baud_co_25[i]; /* Tx CO */
2075 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
2076 info->rco = baud_co_25[i]; /* Rx CO */
2077 }
2078 }
2079 if (baud_table[i] == 134) {
2080 /* get it right for 134.5 baud */
2081 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2082 2;
2083 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2084 ASYNC_SPD_CUST) {
2085 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2086 baud_rate) + 2;
2087 } else if (baud_table[i]) {
2088 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2089 baud_table[i]) + 2;
2090 /* this needs to be propagated into the card info */
2091 } else {
2092 info->timeout = 0;
2093 }
2094 /* By tradition (is it a standard?) a baud rate of zero
2095 implies the line should be/has been closed. A bit
2096 later in this routine such a test is performed. */
2097
2098 /* byte size and parity */
2099 info->cor5 = 0;
2100 info->cor4 = 0;
2101 /* receive threshold */
2102 info->cor3 = (info->default_threshold ?
2103 info->default_threshold : baud_cor3[i]);
2104 info->cor2 = CyETC;
2105 switch (cflag & CSIZE) {
2106 case CS5:
2107 info->cor1 = Cy_5_BITS;
2108 break;
2109 case CS6:
2110 info->cor1 = Cy_6_BITS;
2111 break;
2112 case CS7:
2113 info->cor1 = Cy_7_BITS;
2114 break;
2115 case CS8:
2116 info->cor1 = Cy_8_BITS;
2117 break;
2118 }
2119 if (cflag & CSTOPB)
2120 info->cor1 |= Cy_2_STOP;
2121
2122 if (cflag & PARENB) {
2123 if (cflag & PARODD)
2124 info->cor1 |= CyPARITY_O;
2125 else
2126 info->cor1 |= CyPARITY_E;
2127 } else
2128 info->cor1 |= CyPARITY_NONE;
2129
2130 /* CTS flow control flag */
2131 if (cflag & CRTSCTS) {
2132 info->port.flags |= ASYNC_CTS_FLOW;
2133 info->cor2 |= CyCtsAE;
2134 } else {
2135 info->port.flags &= ~ASYNC_CTS_FLOW;
2136 info->cor2 &= ~CyCtsAE;
2137 }
2138 if (cflag & CLOCAL)
2139 info->port.flags &= ~ASYNC_CHECK_CD;
2140 else
2141 info->port.flags |= ASYNC_CHECK_CD;
2142
2143 /***********************************************
2144 The hardware option, CyRtsAO, presents RTS when
2145 the chip has characters to send. Since most modems
2146 use RTS as reverse (inbound) flow control, this
2147 option is not used. If inbound flow control is
2148 necessary, DTR can be programmed to provide the
2149 appropriate signals for use with a non-standard
2150 cable. Contact Marcio Saito for details.
2151 ***********************************************/
2152
2153 channel &= 0x03;
2154
2155 spin_lock_irqsave(&card->card_lock, flags);
2156 cyy_writeb(info, CyCAR, channel);
2157
2158 /* tx and rx baud rate */
2159
2160 cyy_writeb(info, CyTCOR, info->tco);
2161 cyy_writeb(info, CyTBPR, info->tbpr);
2162 cyy_writeb(info, CyRCOR, info->rco);
2163 cyy_writeb(info, CyRBPR, info->rbpr);
2164
2165 /* set line characteristics according configuration */
2166
2167 cyy_writeb(info, CySCHR1, START_CHAR(tty));
2168 cyy_writeb(info, CySCHR2, STOP_CHAR(tty));
2169 cyy_writeb(info, CyCOR1, info->cor1);
2170 cyy_writeb(info, CyCOR2, info->cor2);
2171 cyy_writeb(info, CyCOR3, info->cor3);
2172 cyy_writeb(info, CyCOR4, info->cor4);
2173 cyy_writeb(info, CyCOR5, info->cor5);
2174
2175 cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
2176 CyCOR3ch);
2177
2178 /* !!! Is this needed? */
2179 cyy_writeb(info, CyCAR, channel);
2180 cyy_writeb(info, CyRTPR,
2181 (info->default_timeout ? info->default_timeout : 0x02));
2182 /* 10ms rx timeout */
2183
2184 cflags = CyCTS;
2185 if (!C_CLOCAL(tty))
2186 cflags |= CyDSR | CyRI | CyDCD;
2187 /* without modem intr */
2188 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyMdmCh);
2189 /* act on 1->0 modem transitions */
2190 if ((cflag & CRTSCTS) && info->rflow)
2191 cyy_writeb(info, CyMCOR1, cflags | rflow_thr[i]);
2192 else
2193 cyy_writeb(info, CyMCOR1, cflags);
2194 /* act on 0->1 modem transitions */
2195 cyy_writeb(info, CyMCOR2, cflags);
2196
2197 if (i == 0) /* baud rate is zero, turn off line */
2198 cyy_change_rts_dtr(info, 0, TIOCM_DTR);
2199 else
2200 cyy_change_rts_dtr(info, TIOCM_DTR, 0);
2201
2202 clear_bit(TTY_IO_ERROR, &tty->flags);
2203 spin_unlock_irqrestore(&card->card_lock, flags);
2204
2205 } else {
2206 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2207 __u32 sw_flow;
2208 int retval;
2209
2210 if (!cyz_is_loaded(card))
2211 return;
2212
2213 /* baud rate */
2214 baud = tty_get_baud_rate(tty);
2215 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2216 ASYNC_SPD_CUST) {
2217 if (info->custom_divisor)
2218 baud_rate = info->baud / info->custom_divisor;
2219 else
2220 baud_rate = info->baud;
2221 } else if (baud > CYZ_MAX_SPEED) {
2222 baud = CYZ_MAX_SPEED;
2223 }
2224 cy_writel(&ch_ctrl->comm_baud, baud);
2225
2226 if (baud == 134) {
2227 /* get it right for 134.5 baud */
2228 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2229 2;
2230 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2231 ASYNC_SPD_CUST) {
2232 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2233 baud_rate) + 2;
2234 } else if (baud) {
2235 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2236 baud) + 2;
2237 /* this needs to be propagated into the card info */
2238 } else {
2239 info->timeout = 0;
2240 }
2241
2242 /* byte size and parity */
2243 switch (cflag & CSIZE) {
2244 case CS5:
2245 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
2246 break;
2247 case CS6:
2248 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
2249 break;
2250 case CS7:
2251 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
2252 break;
2253 case CS8:
2254 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
2255 break;
2256 }
2257 if (cflag & CSTOPB) {
2258 cy_writel(&ch_ctrl->comm_data_l,
2259 readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
2260 } else {
2261 cy_writel(&ch_ctrl->comm_data_l,
2262 readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
2263 }
2264 if (cflag & PARENB) {
2265 if (cflag & PARODD)
2266 cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
2267 else
2268 cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
2269 } else
2270 cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
2271
2272 /* CTS flow control flag */
2273 if (cflag & CRTSCTS) {
2274 cy_writel(&ch_ctrl->hw_flow,
2275 readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
2276 } else {
2277 cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
2278 ~(C_RS_CTS | C_RS_RTS));
2279 }
2280 /* As the HW flow control is done in firmware, the driver
2281 doesn't need to care about it */
2282 info->port.flags &= ~ASYNC_CTS_FLOW;
2283
2284 /* XON/XOFF/XANY flow control flags */
2285 sw_flow = 0;
2286 if (iflag & IXON) {
2287 sw_flow |= C_FL_OXX;
2288 if (iflag & IXANY)
2289 sw_flow |= C_FL_OIXANY;
2290 }
2291 cy_writel(&ch_ctrl->sw_flow, sw_flow);
2292
2293 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
2294 if (retval != 0) {
2295 printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
2296 "was %x\n", info->line, retval);
2297 }
2298
2299 /* CD sensitivity */
2300 if (cflag & CLOCAL)
2301 info->port.flags &= ~ASYNC_CHECK_CD;
2302 else
2303 info->port.flags |= ASYNC_CHECK_CD;
2304
2305 if (baud == 0) { /* baud rate is zero, turn off line */
2306 cy_writel(&ch_ctrl->rs_control,
2307 readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
2308#ifdef CY_DEBUG_DTR
2309 printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
2310#endif
2311 } else {
2312 cy_writel(&ch_ctrl->rs_control,
2313 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2314#ifdef CY_DEBUG_DTR
2315 printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
2316#endif
2317 }
2318
2319 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2320 if (retval != 0) {
2321 printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
2322 "was %x\n", info->line, retval);
2323 }
2324
2325 clear_bit(TTY_IO_ERROR, &tty->flags);
2326 }
2327} /* set_line_char */
2328
2329static int cy_get_serial_info(struct cyclades_port *info,
2330 struct serial_struct __user *retinfo)
2331{
2332 struct cyclades_card *cinfo = info->card;
2333 struct serial_struct tmp = {
2334 .type = info->type,
2335 .line = info->line,
2336 .port = (info->card - cy_card) * 0x100 + info->line -
2337 cinfo->first_line,
2338 .irq = cinfo->irq,
2339 .flags = info->port.flags,
2340 .close_delay = info->port.close_delay,
2341 .closing_wait = info->port.closing_wait,
2342 .baud_base = info->baud,
2343 .custom_divisor = info->custom_divisor,
2344 .hub6 = 0, /*!!! */
2345 };
2346 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2347}
2348
2349static int
2350cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2351 struct serial_struct __user *new_info)
2352{
2353 struct serial_struct new_serial;
2354 int ret;
2355
2356 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2357 return -EFAULT;
2358
2359 mutex_lock(&info->port.mutex);
2360 if (!capable(CAP_SYS_ADMIN)) {
2361 if (new_serial.close_delay != info->port.close_delay ||
2362 new_serial.baud_base != info->baud ||
2363 (new_serial.flags & ASYNC_FLAGS &
2364 ~ASYNC_USR_MASK) !=
2365 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2366 {
2367 mutex_unlock(&info->port.mutex);
2368 return -EPERM;
2369 }
2370 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
2371 (new_serial.flags & ASYNC_USR_MASK);
2372 info->baud = new_serial.baud_base;
2373 info->custom_divisor = new_serial.custom_divisor;
2374 goto check_and_exit;
2375 }
2376
2377 /*
2378 * OK, past this point, all the error checking has been done.
2379 * At this point, we start making changes.....
2380 */
2381
2382 info->baud = new_serial.baud_base;
2383 info->custom_divisor = new_serial.custom_divisor;
2384 info->port.flags = (info->port.flags & ~ASYNC_FLAGS) |
2385 (new_serial.flags & ASYNC_FLAGS);
2386 info->port.close_delay = new_serial.close_delay * HZ / 100;
2387 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
2388
2389check_and_exit:
2390 if (info->port.flags & ASYNC_INITIALIZED) {
2391 cy_set_line_char(info, tty);
2392 ret = 0;
2393 } else {
2394 ret = cy_startup(info, tty);
2395 }
2396 mutex_unlock(&info->port.mutex);
2397 return ret;
2398} /* set_serial_info */
2399
2400/*
2401 * get_lsr_info - get line status register info
2402 *
2403 * Purpose: Let user call ioctl() to get info when the UART physically
2404 * is emptied. On bus types like RS485, the transmitter must
2405 * release the bus after transmitting. This must be done when
2406 * the transmit shift register is empty, not be done when the
2407 * transmit holding register is empty. This functionality
2408 * allows an RS485 driver to be written in user space.
2409 */
2410static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
2411{
2412 struct cyclades_card *card = info->card;
2413 unsigned int result;
2414 unsigned long flags;
2415 u8 status;
2416
2417 if (!cy_is_Z(card)) {
2418 spin_lock_irqsave(&card->card_lock, flags);
2419 status = cyy_readb(info, CySRER) & (CyTxRdy | CyTxMpty);
2420 spin_unlock_irqrestore(&card->card_lock, flags);
2421 result = (status ? 0 : TIOCSER_TEMT);
2422 } else {
2423 /* Not supported yet */
2424 return -EINVAL;
2425 }
2426 return put_user(result, (unsigned long __user *)value);
2427}
2428
2429static int cy_tiocmget(struct tty_struct *tty)
2430{
2431 struct cyclades_port *info = tty->driver_data;
2432 struct cyclades_card *card;
2433 int result;
2434
2435 if (serial_paranoia_check(info, tty->name, __func__))
2436 return -ENODEV;
2437
2438 card = info->card;
2439
2440 if (!cy_is_Z(card)) {
2441 unsigned long flags;
2442 int channel = info->line - card->first_line;
2443 u8 status;
2444
2445 spin_lock_irqsave(&card->card_lock, flags);
2446 cyy_writeb(info, CyCAR, channel & 0x03);
2447 status = cyy_readb(info, CyMSVR1);
2448 status |= cyy_readb(info, CyMSVR2);
2449 spin_unlock_irqrestore(&card->card_lock, flags);
2450
2451 if (info->rtsdtr_inv) {
2452 result = ((status & CyRTS) ? TIOCM_DTR : 0) |
2453 ((status & CyDTR) ? TIOCM_RTS : 0);
2454 } else {
2455 result = ((status & CyRTS) ? TIOCM_RTS : 0) |
2456 ((status & CyDTR) ? TIOCM_DTR : 0);
2457 }
2458 result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
2459 ((status & CyRI) ? TIOCM_RNG : 0) |
2460 ((status & CyDSR) ? TIOCM_DSR : 0) |
2461 ((status & CyCTS) ? TIOCM_CTS : 0);
2462 } else {
2463 u32 lstatus;
2464
2465 if (!cyz_is_loaded(card)) {
2466 result = -ENODEV;
2467 goto end;
2468 }
2469
2470 lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
2471 result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
2472 ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
2473 ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
2474 ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
2475 ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
2476 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
2477 }
2478end:
2479 return result;
2480} /* cy_tiomget */
2481
2482static int
2483cy_tiocmset(struct tty_struct *tty,
2484 unsigned int set, unsigned int clear)
2485{
2486 struct cyclades_port *info = tty->driver_data;
2487 struct cyclades_card *card;
2488 unsigned long flags;
2489
2490 if (serial_paranoia_check(info, tty->name, __func__))
2491 return -ENODEV;
2492
2493 card = info->card;
2494 if (!cy_is_Z(card)) {
2495 spin_lock_irqsave(&card->card_lock, flags);
2496 cyy_change_rts_dtr(info, set, clear);
2497 spin_unlock_irqrestore(&card->card_lock, flags);
2498 } else {
2499 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2500 int retval, channel = info->line - card->first_line;
2501 u32 rs;
2502
2503 if (!cyz_is_loaded(card))
2504 return -ENODEV;
2505
2506 spin_lock_irqsave(&card->card_lock, flags);
2507 rs = readl(&ch_ctrl->rs_control);
2508 if (set & TIOCM_RTS)
2509 rs |= C_RS_RTS;
2510 if (clear & TIOCM_RTS)
2511 rs &= ~C_RS_RTS;
2512 if (set & TIOCM_DTR) {
2513 rs |= C_RS_DTR;
2514#ifdef CY_DEBUG_DTR
2515 printk(KERN_DEBUG "cyc:set_modem_info raising Z DTR\n");
2516#endif
2517 }
2518 if (clear & TIOCM_DTR) {
2519 rs &= ~C_RS_DTR;
2520#ifdef CY_DEBUG_DTR
2521 printk(KERN_DEBUG "cyc:set_modem_info clearing "
2522 "Z DTR\n");
2523#endif
2524 }
2525 cy_writel(&ch_ctrl->rs_control, rs);
2526 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2527 spin_unlock_irqrestore(&card->card_lock, flags);
2528 if (retval != 0) {
2529 printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
2530 "was %x\n", info->line, retval);
2531 }
2532 }
2533 return 0;
2534}
2535
2536/*
2537 * cy_break() --- routine which turns the break handling on or off
2538 */
2539static int cy_break(struct tty_struct *tty, int break_state)
2540{
2541 struct cyclades_port *info = tty->driver_data;
2542 struct cyclades_card *card;
2543 unsigned long flags;
2544 int retval = 0;
2545
2546 if (serial_paranoia_check(info, tty->name, "cy_break"))
2547 return -EINVAL;
2548
2549 card = info->card;
2550
2551 spin_lock_irqsave(&card->card_lock, flags);
2552 if (!cy_is_Z(card)) {
2553 /* Let the transmit ISR take care of this (since it
2554 requires stuffing characters into the output stream).
2555 */
2556 if (break_state == -1) {
2557 if (!info->breakon) {
2558 info->breakon = 1;
2559 if (!info->xmit_cnt) {
2560 spin_unlock_irqrestore(&card->card_lock, flags);
2561 start_xmit(info);
2562 spin_lock_irqsave(&card->card_lock, flags);
2563 }
2564 }
2565 } else {
2566 if (!info->breakoff) {
2567 info->breakoff = 1;
2568 if (!info->xmit_cnt) {
2569 spin_unlock_irqrestore(&card->card_lock, flags);
2570 start_xmit(info);
2571 spin_lock_irqsave(&card->card_lock, flags);
2572 }
2573 }
2574 }
2575 } else {
2576 if (break_state == -1) {
2577 retval = cyz_issue_cmd(card,
2578 info->line - card->first_line,
2579 C_CM_SET_BREAK, 0L);
2580 if (retval != 0) {
2581 printk(KERN_ERR "cyc:cy_break (set) retval on "
2582 "ttyC%d was %x\n", info->line, retval);
2583 }
2584 } else {
2585 retval = cyz_issue_cmd(card,
2586 info->line - card->first_line,
2587 C_CM_CLR_BREAK, 0L);
2588 if (retval != 0) {
2589 printk(KERN_DEBUG "cyc:cy_break (clr) retval "
2590 "on ttyC%d was %x\n", info->line,
2591 retval);
2592 }
2593 }
2594 }
2595 spin_unlock_irqrestore(&card->card_lock, flags);
2596 return retval;
2597} /* cy_break */
2598
2599static int set_threshold(struct cyclades_port *info, unsigned long value)
2600{
2601 struct cyclades_card *card = info->card;
2602 unsigned long flags;
2603
2604 if (!cy_is_Z(card)) {
2605 info->cor3 &= ~CyREC_FIFO;
2606 info->cor3 |= value & CyREC_FIFO;
2607
2608 spin_lock_irqsave(&card->card_lock, flags);
2609 cyy_writeb(info, CyCOR3, info->cor3);
2610 cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR3ch);
2611 spin_unlock_irqrestore(&card->card_lock, flags);
2612 }
2613 return 0;
2614} /* set_threshold */
2615
2616static int get_threshold(struct cyclades_port *info,
2617 unsigned long __user *value)
2618{
2619 struct cyclades_card *card = info->card;
2620
2621 if (!cy_is_Z(card)) {
2622 u8 tmp = cyy_readb(info, CyCOR3) & CyREC_FIFO;
2623 return put_user(tmp, value);
2624 }
2625 return 0;
2626} /* get_threshold */
2627
2628static int set_timeout(struct cyclades_port *info, unsigned long value)
2629{
2630 struct cyclades_card *card = info->card;
2631 unsigned long flags;
2632
2633 if (!cy_is_Z(card)) {
2634 spin_lock_irqsave(&card->card_lock, flags);
2635 cyy_writeb(info, CyRTPR, value & 0xff);
2636 spin_unlock_irqrestore(&card->card_lock, flags);
2637 }
2638 return 0;
2639} /* set_timeout */
2640
2641static int get_timeout(struct cyclades_port *info,
2642 unsigned long __user *value)
2643{
2644 struct cyclades_card *card = info->card;
2645
2646 if (!cy_is_Z(card)) {
2647 u8 tmp = cyy_readb(info, CyRTPR);
2648 return put_user(tmp, value);
2649 }
2650 return 0;
2651} /* get_timeout */
2652
2653static int cy_cflags_changed(struct cyclades_port *info, unsigned long arg,
2654 struct cyclades_icount *cprev)
2655{
2656 struct cyclades_icount cnow;
2657 unsigned long flags;
2658 int ret;
2659
2660 spin_lock_irqsave(&info->card->card_lock, flags);
2661 cnow = info->icount; /* atomic copy */
2662 spin_unlock_irqrestore(&info->card->card_lock, flags);
2663
2664 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
2665 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
2666 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
2667 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
2668
2669 *cprev = cnow;
2670
2671 return ret;
2672}
2673
2674/*
2675 * This routine allows the tty driver to implement device-
2676 * specific ioctl's. If the ioctl number passed in cmd is
2677 * not recognized by the driver, it should return ENOIOCTLCMD.
2678 */
2679static int
2680cy_ioctl(struct tty_struct *tty,
2681 unsigned int cmd, unsigned long arg)
2682{
2683 struct cyclades_port *info = tty->driver_data;
2684 struct cyclades_icount cnow; /* kernel counter temps */
2685 int ret_val = 0;
2686 unsigned long flags;
2687 void __user *argp = (void __user *)arg;
2688
2689 if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
2690 return -ENODEV;
2691
2692#ifdef CY_DEBUG_OTHER
2693 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
2694 info->line, cmd, arg);
2695#endif
2696
2697 switch (cmd) {
2698 case CYGETMON:
2699 if (copy_to_user(argp, &info->mon, sizeof(info->mon))) {
2700 ret_val = -EFAULT;
2701 break;
2702 }
2703 memset(&info->mon, 0, sizeof(info->mon));
2704 break;
2705 case CYGETTHRESH:
2706 ret_val = get_threshold(info, argp);
2707 break;
2708 case CYSETTHRESH:
2709 ret_val = set_threshold(info, arg);
2710 break;
2711 case CYGETDEFTHRESH:
2712 ret_val = put_user(info->default_threshold,
2713 (unsigned long __user *)argp);
2714 break;
2715 case CYSETDEFTHRESH:
2716 info->default_threshold = arg & 0x0f;
2717 break;
2718 case CYGETTIMEOUT:
2719 ret_val = get_timeout(info, argp);
2720 break;
2721 case CYSETTIMEOUT:
2722 ret_val = set_timeout(info, arg);
2723 break;
2724 case CYGETDEFTIMEOUT:
2725 ret_val = put_user(info->default_timeout,
2726 (unsigned long __user *)argp);
2727 break;
2728 case CYSETDEFTIMEOUT:
2729 info->default_timeout = arg & 0xff;
2730 break;
2731 case CYSETRFLOW:
2732 info->rflow = (int)arg;
2733 break;
2734 case CYGETRFLOW:
2735 ret_val = info->rflow;
2736 break;
2737 case CYSETRTSDTR_INV:
2738 info->rtsdtr_inv = (int)arg;
2739 break;
2740 case CYGETRTSDTR_INV:
2741 ret_val = info->rtsdtr_inv;
2742 break;
2743 case CYGETCD1400VER:
2744 ret_val = info->chip_rev;
2745 break;
2746#ifndef CONFIG_CYZ_INTR
2747 case CYZSETPOLLCYCLE:
2748 cyz_polling_cycle = (arg * HZ) / 1000;
2749 break;
2750 case CYZGETPOLLCYCLE:
2751 ret_val = (cyz_polling_cycle * 1000) / HZ;
2752 break;
2753#endif /* CONFIG_CYZ_INTR */
2754 case CYSETWAIT:
2755 info->port.closing_wait = (unsigned short)arg * HZ / 100;
2756 break;
2757 case CYGETWAIT:
2758 ret_val = info->port.closing_wait / (HZ / 100);
2759 break;
2760 case TIOCGSERIAL:
2761 ret_val = cy_get_serial_info(info, argp);
2762 break;
2763 case TIOCSSERIAL:
2764 ret_val = cy_set_serial_info(info, tty, argp);
2765 break;
2766 case TIOCSERGETLSR: /* Get line status register */
2767 ret_val = get_lsr_info(info, argp);
2768 break;
2769 /*
2770 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2771 * - mask passed in arg for lines of interest
2772 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2773 * Caller should use TIOCGICOUNT to see which one it was
2774 */
2775 case TIOCMIWAIT:
2776 spin_lock_irqsave(&info->card->card_lock, flags);
2777 /* note the counters on entry */
2778 cnow = info->icount;
2779 spin_unlock_irqrestore(&info->card->card_lock, flags);
2780 ret_val = wait_event_interruptible(info->port.delta_msr_wait,
2781 cy_cflags_changed(info, arg, &cnow));
2782 break;
2783
2784 /*
2785 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2786 * Return: write counters to the user passed counter struct
2787 * NB: both 1->0 and 0->1 transitions are counted except for
2788 * RI where only 0->1 is counted.
2789 */
2790 default:
2791 ret_val = -ENOIOCTLCMD;
2792 }
2793
2794#ifdef CY_DEBUG_OTHER
2795 printk(KERN_DEBUG "cyc:cy_ioctl done\n");
2796#endif
2797 return ret_val;
2798} /* cy_ioctl */
2799
2800static int cy_get_icount(struct tty_struct *tty,
2801 struct serial_icounter_struct *sic)
2802{
2803 struct cyclades_port *info = tty->driver_data;
2804 struct cyclades_icount cnow; /* Used to snapshot */
2805 unsigned long flags;
2806
2807 spin_lock_irqsave(&info->card->card_lock, flags);
2808 cnow = info->icount;
2809 spin_unlock_irqrestore(&info->card->card_lock, flags);
2810
2811 sic->cts = cnow.cts;
2812 sic->dsr = cnow.dsr;
2813 sic->rng = cnow.rng;
2814 sic->dcd = cnow.dcd;
2815 sic->rx = cnow.rx;
2816 sic->tx = cnow.tx;
2817 sic->frame = cnow.frame;
2818 sic->overrun = cnow.overrun;
2819 sic->parity = cnow.parity;
2820 sic->brk = cnow.brk;
2821 sic->buf_overrun = cnow.buf_overrun;
2822 return 0;
2823}
2824
2825/*
2826 * This routine allows the tty driver to be notified when
2827 * device's termios settings have changed. Note that a
2828 * well-designed tty driver should be prepared to accept the case
2829 * where old == NULL, and try to do something rational.
2830 */
2831static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2832{
2833 struct cyclades_port *info = tty->driver_data;
2834
2835#ifdef CY_DEBUG_OTHER
2836 printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
2837#endif
2838
2839 cy_set_line_char(info, tty);
2840
2841 if ((old_termios->c_cflag & CRTSCTS) &&
2842 !(tty->termios->c_cflag & CRTSCTS)) {
2843 tty->hw_stopped = 0;
2844 cy_start(tty);
2845 }
2846#if 0
2847 /*
2848 * No need to wake up processes in open wait, since they
2849 * sample the CLOCAL flag once, and don't recheck it.
2850 * XXX It's not clear whether the current behavior is correct
2851 * or not. Hence, this may change.....
2852 */
2853 if (!(old_termios->c_cflag & CLOCAL) &&
2854 (tty->termios->c_cflag & CLOCAL))
2855 wake_up_interruptible(&info->port.open_wait);
2856#endif
2857} /* cy_set_termios */
2858
2859/* This function is used to send a high-priority XON/XOFF character to
2860 the device.
2861*/
2862static void cy_send_xchar(struct tty_struct *tty, char ch)
2863{
2864 struct cyclades_port *info = tty->driver_data;
2865 struct cyclades_card *card;
2866 int channel;
2867
2868 if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
2869 return;
2870
2871 info->x_char = ch;
2872
2873 if (ch)
2874 cy_start(tty);
2875
2876 card = info->card;
2877 channel = info->line - card->first_line;
2878
2879 if (cy_is_Z(card)) {
2880 if (ch == STOP_CHAR(tty))
2881 cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
2882 else if (ch == START_CHAR(tty))
2883 cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
2884 }
2885}
2886
2887/* This routine is called by the upper-layer tty layer to signal
2888 that incoming characters should be throttled because the input
2889 buffers are close to full.
2890 */
2891static void cy_throttle(struct tty_struct *tty)
2892{
2893 struct cyclades_port *info = tty->driver_data;
2894 struct cyclades_card *card;
2895 unsigned long flags;
2896
2897#ifdef CY_DEBUG_THROTTLE
2898 char buf[64];
2899
2900 printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
2901 tty->ldisc.chars_in_buffer(tty), info->line);
2902#endif
2903
2904 if (serial_paranoia_check(info, tty->name, "cy_throttle"))
2905 return;
2906
2907 card = info->card;
2908
2909 if (I_IXOFF(tty)) {
2910 if (!cy_is_Z(card))
2911 cy_send_xchar(tty, STOP_CHAR(tty));
2912 else
2913 info->throttle = 1;
2914 }
2915
2916 if (tty->termios->c_cflag & CRTSCTS) {
2917 if (!cy_is_Z(card)) {
2918 spin_lock_irqsave(&card->card_lock, flags);
2919 cyy_change_rts_dtr(info, 0, TIOCM_RTS);
2920 spin_unlock_irqrestore(&card->card_lock, flags);
2921 } else {
2922 info->throttle = 1;
2923 }
2924 }
2925} /* cy_throttle */
2926
2927/*
2928 * This routine notifies the tty driver that it should signal
2929 * that characters can now be sent to the tty without fear of
2930 * overrunning the input buffers of the line disciplines.
2931 */
2932static void cy_unthrottle(struct tty_struct *tty)
2933{
2934 struct cyclades_port *info = tty->driver_data;
2935 struct cyclades_card *card;
2936 unsigned long flags;
2937
2938#ifdef CY_DEBUG_THROTTLE
2939 char buf[64];
2940
2941 printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
2942 tty_name(tty, buf), tty_chars_in_buffer(tty), info->line);
2943#endif
2944
2945 if (serial_paranoia_check(info, tty->name, "cy_unthrottle"))
2946 return;
2947
2948 if (I_IXOFF(tty)) {
2949 if (info->x_char)
2950 info->x_char = 0;
2951 else
2952 cy_send_xchar(tty, START_CHAR(tty));
2953 }
2954
2955 if (tty->termios->c_cflag & CRTSCTS) {
2956 card = info->card;
2957 if (!cy_is_Z(card)) {
2958 spin_lock_irqsave(&card->card_lock, flags);
2959 cyy_change_rts_dtr(info, TIOCM_RTS, 0);
2960 spin_unlock_irqrestore(&card->card_lock, flags);
2961 } else {
2962 info->throttle = 0;
2963 }
2964 }
2965} /* cy_unthrottle */
2966
2967/* cy_start and cy_stop provide software output flow control as a
2968 function of XON/XOFF, software CTS, and other such stuff.
2969*/
2970static void cy_stop(struct tty_struct *tty)
2971{
2972 struct cyclades_card *cinfo;
2973 struct cyclades_port *info = tty->driver_data;
2974 int channel;
2975 unsigned long flags;
2976
2977#ifdef CY_DEBUG_OTHER
2978 printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
2979#endif
2980
2981 if (serial_paranoia_check(info, tty->name, "cy_stop"))
2982 return;
2983
2984 cinfo = info->card;
2985 channel = info->line - cinfo->first_line;
2986 if (!cy_is_Z(cinfo)) {
2987 spin_lock_irqsave(&cinfo->card_lock, flags);
2988 cyy_writeb(info, CyCAR, channel & 0x03);
2989 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
2990 spin_unlock_irqrestore(&cinfo->card_lock, flags);
2991 }
2992} /* cy_stop */
2993
2994static void cy_start(struct tty_struct *tty)
2995{
2996 struct cyclades_card *cinfo;
2997 struct cyclades_port *info = tty->driver_data;
2998 int channel;
2999 unsigned long flags;
3000
3001#ifdef CY_DEBUG_OTHER
3002 printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
3003#endif
3004
3005 if (serial_paranoia_check(info, tty->name, "cy_start"))
3006 return;
3007
3008 cinfo = info->card;
3009 channel = info->line - cinfo->first_line;
3010 if (!cy_is_Z(cinfo)) {
3011 spin_lock_irqsave(&cinfo->card_lock, flags);
3012 cyy_writeb(info, CyCAR, channel & 0x03);
3013 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
3014 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3015 }
3016} /* cy_start */
3017
3018/*
3019 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
3020 */
3021static void cy_hangup(struct tty_struct *tty)
3022{
3023 struct cyclades_port *info = tty->driver_data;
3024
3025#ifdef CY_DEBUG_OTHER
3026 printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
3027#endif
3028
3029 if (serial_paranoia_check(info, tty->name, "cy_hangup"))
3030 return;
3031
3032 cy_flush_buffer(tty);
3033 cy_shutdown(info, tty);
3034 tty_port_hangup(&info->port);
3035} /* cy_hangup */
3036
3037static int cyy_carrier_raised(struct tty_port *port)
3038{
3039 struct cyclades_port *info = container_of(port, struct cyclades_port,
3040 port);
3041 struct cyclades_card *cinfo = info->card;
3042 unsigned long flags;
3043 int channel = info->line - cinfo->first_line;
3044 u32 cd;
3045
3046 spin_lock_irqsave(&cinfo->card_lock, flags);
3047 cyy_writeb(info, CyCAR, channel & 0x03);
3048 cd = cyy_readb(info, CyMSVR1) & CyDCD;
3049 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3050
3051 return cd;
3052}
3053
3054static void cyy_dtr_rts(struct tty_port *port, int raise)
3055{
3056 struct cyclades_port *info = container_of(port, struct cyclades_port,
3057 port);
3058 struct cyclades_card *cinfo = info->card;
3059 unsigned long flags;
3060
3061 spin_lock_irqsave(&cinfo->card_lock, flags);
3062 cyy_change_rts_dtr(info, raise ? TIOCM_RTS | TIOCM_DTR : 0,
3063 raise ? 0 : TIOCM_RTS | TIOCM_DTR);
3064 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3065}
3066
3067static int cyz_carrier_raised(struct tty_port *port)
3068{
3069 struct cyclades_port *info = container_of(port, struct cyclades_port,
3070 port);
3071
3072 return readl(&info->u.cyz.ch_ctrl->rs_status) & C_RS_DCD;
3073}
3074
3075static void cyz_dtr_rts(struct tty_port *port, int raise)
3076{
3077 struct cyclades_port *info = container_of(port, struct cyclades_port,
3078 port);
3079 struct cyclades_card *cinfo = info->card;
3080 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
3081 int ret, channel = info->line - cinfo->first_line;
3082 u32 rs;
3083
3084 rs = readl(&ch_ctrl->rs_control);
3085 if (raise)
3086 rs |= C_RS_RTS | C_RS_DTR;
3087 else
3088 rs &= ~(C_RS_RTS | C_RS_DTR);
3089 cy_writel(&ch_ctrl->rs_control, rs);
3090 ret = cyz_issue_cmd(cinfo, channel, C_CM_IOCTLM, 0L);
3091 if (ret != 0)
3092 printk(KERN_ERR "%s: retval on ttyC%d was %x\n",
3093 __func__, info->line, ret);
3094#ifdef CY_DEBUG_DTR
3095 printk(KERN_DEBUG "%s: raising Z DTR\n", __func__);
3096#endif
3097}
3098
3099static const struct tty_port_operations cyy_port_ops = {
3100 .carrier_raised = cyy_carrier_raised,
3101 .dtr_rts = cyy_dtr_rts,
3102 .shutdown = cy_do_close,
3103};
3104
3105static const struct tty_port_operations cyz_port_ops = {
3106 .carrier_raised = cyz_carrier_raised,
3107 .dtr_rts = cyz_dtr_rts,
3108 .shutdown = cy_do_close,
3109};
3110
3111/*
3112 * ---------------------------------------------------------------------
3113 * cy_init() and friends
3114 *
3115 * cy_init() is called at boot-time to initialize the serial driver.
3116 * ---------------------------------------------------------------------
3117 */
3118
3119static int __devinit cy_init_card(struct cyclades_card *cinfo)
3120{
3121 struct cyclades_port *info;
3122 unsigned int channel, port;
3123
3124 spin_lock_init(&cinfo->card_lock);
3125 cinfo->intr_enabled = 0;
3126
3127 cinfo->ports = kcalloc(cinfo->nports, sizeof(*cinfo->ports),
3128 GFP_KERNEL);
3129 if (cinfo->ports == NULL) {
3130 printk(KERN_ERR "Cyclades: cannot allocate ports\n");
3131 return -ENOMEM;
3132 }
3133
3134 for (channel = 0, port = cinfo->first_line; channel < cinfo->nports;
3135 channel++, port++) {
3136 info = &cinfo->ports[channel];
3137 tty_port_init(&info->port);
3138 info->magic = CYCLADES_MAGIC;
3139 info->card = cinfo;
3140 info->line = port;
3141
3142 info->port.closing_wait = CLOSING_WAIT_DELAY;
3143 info->port.close_delay = 5 * HZ / 10;
3144 info->port.flags = STD_COM_FLAGS;
3145 init_completion(&info->shutdown_wait);
3146
3147 if (cy_is_Z(cinfo)) {
3148 struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
3149 struct ZFW_CTRL *zfw_ctrl;
3150
3151 info->port.ops = &cyz_port_ops;
3152 info->type = PORT_STARTECH;
3153
3154 zfw_ctrl = cinfo->base_addr +
3155 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3156 info->u.cyz.ch_ctrl = &zfw_ctrl->ch_ctrl[channel];
3157 info->u.cyz.buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3158
3159 if (cinfo->hw_ver == ZO_V1)
3160 info->xmit_fifo_size = CYZ_FIFO_SIZE;
3161 else
3162 info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
3163#ifdef CONFIG_CYZ_INTR
3164 setup_timer(&cyz_rx_full_timer[port],
3165 cyz_rx_restart, (unsigned long)info);
3166#endif
3167 } else {
3168 unsigned short chip_number;
3169 int index = cinfo->bus_index;
3170
3171 info->port.ops = &cyy_port_ops;
3172 info->type = PORT_CIRRUS;
3173 info->xmit_fifo_size = CyMAX_CHAR_FIFO;
3174 info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
3175 info->cor2 = CyETC;
3176 info->cor3 = 0x08; /* _very_ small rcv threshold */
3177
3178 chip_number = channel / CyPORTS_PER_CHIP;
3179 info->u.cyy.base_addr = cinfo->base_addr +
3180 (cy_chip_offset[chip_number] << index);
3181 info->chip_rev = cyy_readb(info, CyGFRCR);
3182
3183 if (info->chip_rev >= CD1400_REV_J) {
3184 /* It is a CD1400 rev. J or later */
3185 info->tbpr = baud_bpr_60[13]; /* Tx BPR */
3186 info->tco = baud_co_60[13]; /* Tx CO */
3187 info->rbpr = baud_bpr_60[13]; /* Rx BPR */
3188 info->rco = baud_co_60[13]; /* Rx CO */
3189 info->rtsdtr_inv = 1;
3190 } else {
3191 info->tbpr = baud_bpr_25[13]; /* Tx BPR */
3192 info->tco = baud_co_25[13]; /* Tx CO */
3193 info->rbpr = baud_bpr_25[13]; /* Rx BPR */
3194 info->rco = baud_co_25[13]; /* Rx CO */
3195 info->rtsdtr_inv = 0;
3196 }
3197 info->read_status_mask = CyTIMEOUT | CySPECHAR |
3198 CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
3199 }
3200
3201 }
3202
3203#ifndef CONFIG_CYZ_INTR
3204 if (cy_is_Z(cinfo) && !timer_pending(&cyz_timerlist)) {
3205 mod_timer(&cyz_timerlist, jiffies + 1);
3206#ifdef CY_PCI_DEBUG
3207 printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
3208#endif
3209 }
3210#endif
3211 return 0;
3212}
3213
3214/* initialize chips on Cyclom-Y card -- return number of valid
3215 chips (which is number of ports/4) */
3216static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
3217 int index)
3218{
3219 unsigned int chip_number;
3220 void __iomem *base_addr;
3221
3222 cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
3223 /* Cy_HwReset is 0x1400 */
3224 cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
3225 /* Cy_ClrIntr is 0x1800 */
3226 udelay(500L);
3227
3228 for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD;
3229 chip_number++) {
3230 base_addr =
3231 true_base_addr + (cy_chip_offset[chip_number] << index);
3232 mdelay(1);
3233 if (readb(base_addr + (CyCCR << index)) != 0x00) {
3234 /*************
3235 printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
3236 chip_number, (unsigned long)base_addr);
3237 *************/
3238 return chip_number;
3239 }
3240
3241 cy_writeb(base_addr + (CyGFRCR << index), 0);
3242 udelay(10L);
3243
3244 /* The Cyclom-16Y does not decode address bit 9 and therefore
3245 cannot distinguish between references to chip 0 and a non-
3246 existent chip 4. If the preceding clearing of the supposed
3247 chip 4 GFRCR register appears at chip 0, there is no chip 4
3248 and this must be a Cyclom-16Y, not a Cyclom-32Ye.
3249 */
3250 if (chip_number == 4 && readb(true_base_addr +
3251 (cy_chip_offset[0] << index) +
3252 (CyGFRCR << index)) == 0) {
3253 return chip_number;
3254 }
3255
3256 cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
3257 mdelay(1);
3258
3259 if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
3260 /*
3261 printk(" chip #%d at %#6lx is not responding ",
3262 chip_number, (unsigned long)base_addr);
3263 printk("(GFRCR stayed 0)\n",
3264 */
3265 return chip_number;
3266 }
3267 if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
3268 0x40) {
3269 /*
3270 printk(" chip #%d at %#6lx is not valid (GFRCR == "
3271 "%#2x)\n",
3272 chip_number, (unsigned long)base_addr,
3273 base_addr[CyGFRCR<<index]);
3274 */
3275 return chip_number;
3276 }
3277 cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
3278 if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
3279 /* It is a CD1400 rev. J or later */
3280 /* Impossible to reach 5ms with this chip.
3281 Changed to 2ms instead (f = 500 Hz). */
3282 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
3283 } else {
3284 /* f = 200 Hz */
3285 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
3286 }
3287
3288 /*
3289 printk(" chip #%d at %#6lx is rev 0x%2x\n",
3290 chip_number, (unsigned long)base_addr,
3291 readb(base_addr+(CyGFRCR<<index)));
3292 */
3293 }
3294 return chip_number;
3295} /* cyy_init_card */
3296
3297/*
3298 * ---------------------------------------------------------------------
3299 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
3300 * sets global variables and return the number of ISA boards found.
3301 * ---------------------------------------------------------------------
3302 */
3303static int __init cy_detect_isa(void)
3304{
3305#ifdef CONFIG_ISA
3306 unsigned short cy_isa_irq, nboard;
3307 void __iomem *cy_isa_address;
3308 unsigned short i, j, cy_isa_nchan;
3309 int isparam = 0;
3310
3311 nboard = 0;
3312
3313 /* Check for module parameters */
3314 for (i = 0; i < NR_CARDS; i++) {
3315 if (maddr[i] || i) {
3316 isparam = 1;
3317 cy_isa_addresses[i] = maddr[i];
3318 }
3319 if (!maddr[i])
3320 break;
3321 }
3322
3323 /* scan the address table probing for Cyclom-Y/ISA boards */
3324 for (i = 0; i < NR_ISA_ADDRS; i++) {
3325 unsigned int isa_address = cy_isa_addresses[i];
3326 if (isa_address == 0x0000)
3327 return nboard;
3328
3329 /* probe for CD1400... */
3330 cy_isa_address = ioremap_nocache(isa_address, CyISA_Ywin);
3331 if (cy_isa_address == NULL) {
3332 printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
3333 "address\n");
3334 continue;
3335 }
3336 cy_isa_nchan = CyPORTS_PER_CHIP *
3337 cyy_init_card(cy_isa_address, 0);
3338 if (cy_isa_nchan == 0) {
3339 iounmap(cy_isa_address);
3340 continue;
3341 }
3342
3343 if (isparam && i < NR_CARDS && irq[i])
3344 cy_isa_irq = irq[i];
3345 else
3346 /* find out the board's irq by probing */
3347 cy_isa_irq = detect_isa_irq(cy_isa_address);
3348 if (cy_isa_irq == 0) {
3349 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
3350 "IRQ could not be detected.\n",
3351 (unsigned long)cy_isa_address);
3352 iounmap(cy_isa_address);
3353 continue;
3354 }
3355
3356 if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
3357 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3358 "more channels are available. Change NR_PORTS "
3359 "in cyclades.c and recompile kernel.\n",
3360 (unsigned long)cy_isa_address);
3361 iounmap(cy_isa_address);
3362 return nboard;
3363 }
3364 /* fill the next cy_card structure available */
3365 for (j = 0; j < NR_CARDS; j++) {
3366 if (cy_card[j].base_addr == NULL)
3367 break;
3368 }
3369 if (j == NR_CARDS) { /* no more cy_cards available */
3370 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3371 "more cards can be used. Change NR_CARDS in "
3372 "cyclades.c and recompile kernel.\n",
3373 (unsigned long)cy_isa_address);
3374 iounmap(cy_isa_address);
3375 return nboard;
3376 }
3377
3378 /* allocate IRQ */
3379 if (request_irq(cy_isa_irq, cyy_interrupt,
3380 IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
3381 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
3382 "could not allocate IRQ#%d.\n",
3383 (unsigned long)cy_isa_address, cy_isa_irq);
3384 iounmap(cy_isa_address);
3385 return nboard;
3386 }
3387
3388 /* set cy_card */
3389 cy_card[j].base_addr = cy_isa_address;
3390 cy_card[j].ctl_addr.p9050 = NULL;
3391 cy_card[j].irq = (int)cy_isa_irq;
3392 cy_card[j].bus_index = 0;
3393 cy_card[j].first_line = cy_next_channel;
3394 cy_card[j].num_chips = cy_isa_nchan / CyPORTS_PER_CHIP;
3395 cy_card[j].nports = cy_isa_nchan;
3396 if (cy_init_card(&cy_card[j])) {
3397 cy_card[j].base_addr = NULL;
3398 free_irq(cy_isa_irq, &cy_card[j]);
3399 iounmap(cy_isa_address);
3400 continue;
3401 }
3402 nboard++;
3403
3404 printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
3405 "%d channels starting from port %d\n",
3406 j + 1, (unsigned long)cy_isa_address,
3407 (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
3408 cy_isa_irq, cy_isa_nchan, cy_next_channel);
3409
3410 for (j = cy_next_channel;
3411 j < cy_next_channel + cy_isa_nchan; j++)
3412 tty_register_device(cy_serial_driver, j, NULL);
3413 cy_next_channel += cy_isa_nchan;
3414 }
3415 return nboard;
3416#else
3417 return 0;
3418#endif /* CONFIG_ISA */
3419} /* cy_detect_isa */
3420
3421#ifdef CONFIG_PCI
3422static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
3423{
3424 unsigned int a;
3425
3426 for (a = 0; a < size && *str; a++, str++)
3427 if (*str & 0x80)
3428 return -EINVAL;
3429
3430 for (; a < size; a++, str++)
3431 if (*str)
3432 return -EINVAL;
3433
3434 return 0;
3435}
3436
3437static inline void __devinit cyz_fpga_copy(void __iomem *fpga, const u8 *data,
3438 unsigned int size)
3439{
3440 for (; size > 0; size--) {
3441 cy_writel(fpga, *data++);
3442 udelay(10);
3443 }
3444}
3445
3446static void __devinit plx_init(struct pci_dev *pdev, int irq,
3447 struct RUNTIME_9060 __iomem *addr)
3448{
3449 /* Reset PLX */
3450 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
3451 udelay(100L);
3452 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
3453
3454 /* Reload Config. Registers from EEPROM */
3455 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
3456 udelay(100L);
3457 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
3458
3459 /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
3460 * the IRQ is lost and, thus, we have to re-write it to the PCI config.
3461 * registers. This will remain here until we find a permanent fix.
3462 */
3463 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
3464}
3465
3466static int __devinit __cyz_load_fw(const struct firmware *fw,
3467 const char *name, const u32 mailbox, void __iomem *base,
3468 void __iomem *fpga)
3469{
3470 const void *ptr = fw->data;
3471 const struct zfile_header *h = ptr;
3472 const struct zfile_config *c, *cs;
3473 const struct zfile_block *b, *bs;
3474 unsigned int a, tmp, len = fw->size;
3475#define BAD_FW KERN_ERR "Bad firmware: "
3476 if (len < sizeof(*h)) {
3477 printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
3478 return -EINVAL;
3479 }
3480
3481 cs = ptr + h->config_offset;
3482 bs = ptr + h->block_offset;
3483
3484 if ((void *)(cs + h->n_config) > ptr + len ||
3485 (void *)(bs + h->n_blocks) > ptr + len) {
3486 printk(BAD_FW "too short");
3487 return -EINVAL;
3488 }
3489
3490 if (cyc_isfwstr(h->name, sizeof(h->name)) ||
3491 cyc_isfwstr(h->date, sizeof(h->date))) {
3492 printk(BAD_FW "bad formatted header string\n");
3493 return -EINVAL;
3494 }
3495
3496 if (strncmp(name, h->name, sizeof(h->name))) {
3497 printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
3498 return -EINVAL;
3499 }
3500
3501 tmp = 0;
3502 for (c = cs; c < cs + h->n_config; c++) {
3503 for (a = 0; a < c->n_blocks; a++)
3504 if (c->block_list[a] > h->n_blocks) {
3505 printk(BAD_FW "bad block ref number in cfgs\n");
3506 return -EINVAL;
3507 }
3508 if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
3509 tmp++;
3510 }
3511 if (!tmp) {
3512 printk(BAD_FW "nothing appropriate\n");
3513 return -EINVAL;
3514 }
3515
3516 for (b = bs; b < bs + h->n_blocks; b++)
3517 if (b->file_offset + b->size > len) {
3518 printk(BAD_FW "bad block data offset\n");
3519 return -EINVAL;
3520 }
3521
3522 /* everything is OK, let's seek'n'load it */
3523 for (c = cs; c < cs + h->n_config; c++)
3524 if (c->mailbox == mailbox && c->function == 0)
3525 break;
3526
3527 for (a = 0; a < c->n_blocks; a++) {
3528 b = &bs[c->block_list[a]];
3529 if (b->type == ZBLOCK_FPGA) {
3530 if (fpga != NULL)
3531 cyz_fpga_copy(fpga, ptr + b->file_offset,
3532 b->size);
3533 } else {
3534 if (base != NULL)
3535 memcpy_toio(base + b->ram_offset,
3536 ptr + b->file_offset, b->size);
3537 }
3538 }
3539#undef BAD_FW
3540 return 0;
3541}
3542
3543static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
3544 struct RUNTIME_9060 __iomem *ctl_addr, int irq)
3545{
3546 const struct firmware *fw;
3547 struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
3548 struct CUSTOM_REG __iomem *cust = base_addr;
3549 struct ZFW_CTRL __iomem *pt_zfwctrl;
3550 void __iomem *tmp;
3551 u32 mailbox, status, nchan;
3552 unsigned int i;
3553 int retval;
3554
3555 retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
3556 if (retval) {
3557 dev_err(&pdev->dev, "can't get firmware\n");
3558 goto err;
3559 }
3560
3561 /* Check whether the firmware is already loaded and running. If
3562 positive, skip this board */
3563 if (__cyz_fpga_loaded(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
3564 u32 cntval = readl(base_addr + 0x190);
3565
3566 udelay(100);
3567 if (cntval != readl(base_addr + 0x190)) {
3568 /* FW counter is working, FW is running */
3569 dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
3570 "Skipping board.\n");
3571 retval = 0;
3572 goto err_rel;
3573 }
3574 }
3575
3576 /* start boot */
3577 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
3578 ~0x00030800UL);
3579
3580 mailbox = readl(&ctl_addr->mail_box_0);
3581
3582 if (mailbox == 0 || __cyz_fpga_loaded(ctl_addr)) {
3583 /* stops CPU and set window to beginning of RAM */
3584 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3585 cy_writel(&cust->cpu_stop, 0);
3586 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3587 udelay(100);
3588 }
3589
3590 plx_init(pdev, irq, ctl_addr);
3591
3592 if (mailbox != 0) {
3593 /* load FPGA */
3594 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
3595 base_addr);
3596 if (retval)
3597 goto err_rel;
3598 if (!__cyz_fpga_loaded(ctl_addr)) {
3599 dev_err(&pdev->dev, "fw upload successful, but fw is "
3600 "not loaded\n");
3601 goto err_rel;
3602 }
3603 }
3604
3605 /* stops CPU and set window to beginning of RAM */
3606 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3607 cy_writel(&cust->cpu_stop, 0);
3608 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3609 udelay(100);
3610
3611 /* clear memory */
3612 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3613 cy_writeb(tmp, 255);
3614 if (mailbox != 0) {
3615 /* set window to last 512K of RAM */
3616 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
3617 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3618 cy_writeb(tmp, 255);
3619 /* set window to beginning of RAM */
3620 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3621 }
3622
3623 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
3624 release_firmware(fw);
3625 if (retval)
3626 goto err;
3627
3628 /* finish boot and start boards */
3629 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3630 cy_writel(&cust->cpu_start, 0);
3631 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3632 i = 0;
3633 while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
3634 msleep(100);
3635 if (status != ZFIRM_ID) {
3636 if (status == ZFIRM_HLT) {
3637 dev_err(&pdev->dev, "you need an external power supply "
3638 "for this number of ports. Firmware halted and "
3639 "board reset.\n");
3640 retval = -EIO;
3641 goto err;
3642 }
3643 dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
3644 "some more time\n", status);
3645 while ((status = readl(&fid->signature)) != ZFIRM_ID &&
3646 i++ < 200)
3647 msleep(100);
3648 if (status != ZFIRM_ID) {
3649 dev_err(&pdev->dev, "Board not started in 20 seconds! "
3650 "Giving up. (fid->signature = 0x%x)\n",
3651 status);
3652 dev_info(&pdev->dev, "*** Warning ***: if you are "
3653 "upgrading the FW, please power cycle the "
3654 "system before loading the new FW to the "
3655 "Cyclades-Z.\n");
3656
3657 if (__cyz_fpga_loaded(ctl_addr))
3658 plx_init(pdev, irq, ctl_addr);
3659
3660 retval = -EIO;
3661 goto err;
3662 }
3663 dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
3664 i / 10);
3665 }
3666 pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
3667
3668 dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
3669 base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
3670 base_addr + readl(&fid->zfwctrl_addr));
3671
3672 nchan = readl(&pt_zfwctrl->board_ctrl.n_channel);
3673 dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
3674 readl(&pt_zfwctrl->board_ctrl.fw_version), nchan);
3675
3676 if (nchan == 0) {
3677 dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
3678 "check the connection between the Z host card and the "
3679 "serial expanders.\n");
3680
3681 if (__cyz_fpga_loaded(ctl_addr))
3682 plx_init(pdev, irq, ctl_addr);
3683
3684 dev_info(&pdev->dev, "Null number of ports detected. Board "
3685 "reset.\n");
3686 retval = 0;
3687 goto err;
3688 }
3689
3690 cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
3691 cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
3692
3693 /*
3694 Early firmware failed to start looking for commands.
3695 This enables firmware interrupts for those commands.
3696 */
3697 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3698 (1 << 17));
3699 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3700 0x00030800UL);
3701
3702 return nchan;
3703err_rel:
3704 release_firmware(fw);
3705err:
3706 return retval;
3707}
3708
3709static int __devinit cy_pci_probe(struct pci_dev *pdev,
3710 const struct pci_device_id *ent)
3711{
3712 void __iomem *addr0 = NULL, *addr2 = NULL;
3713 char *card_name = NULL;
3714 u32 uninitialized_var(mailbox);
3715 unsigned int device_id, nchan = 0, card_no, i;
3716 unsigned char plx_ver;
3717 int retval, irq;
3718
3719 retval = pci_enable_device(pdev);
3720 if (retval) {
3721 dev_err(&pdev->dev, "cannot enable device\n");
3722 goto err;
3723 }
3724
3725 /* read PCI configuration area */
3726 irq = pdev->irq;
3727 device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
3728
3729#if defined(__alpha__)
3730 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
3731 dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
3732 "addresses on Alpha systems.\n");
3733 retval = -EIO;
3734 goto err_dis;
3735 }
3736#endif
3737 if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
3738 dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
3739 "addresses\n");
3740 retval = -EIO;
3741 goto err_dis;
3742 }
3743
3744 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
3745 dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
3746 "it...\n");
3747 pdev->resource[2].flags &= ~IORESOURCE_IO;
3748 }
3749
3750 retval = pci_request_regions(pdev, "cyclades");
3751 if (retval) {
3752 dev_err(&pdev->dev, "failed to reserve resources\n");
3753 goto err_dis;
3754 }
3755
3756 retval = -EIO;
3757 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3758 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3759 card_name = "Cyclom-Y";
3760
3761 addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
3762 CyPCI_Yctl);
3763 if (addr0 == NULL) {
3764 dev_err(&pdev->dev, "can't remap ctl region\n");
3765 goto err_reg;
3766 }
3767 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
3768 CyPCI_Ywin);
3769 if (addr2 == NULL) {
3770 dev_err(&pdev->dev, "can't remap base region\n");
3771 goto err_unmap;
3772 }
3773
3774 nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
3775 if (nchan == 0) {
3776 dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
3777 "Serial-Modules\n");
3778 goto err_unmap;
3779 }
3780 } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
3781 struct RUNTIME_9060 __iomem *ctl_addr;
3782
3783 ctl_addr = addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
3784 CyPCI_Zctl);
3785 if (addr0 == NULL) {
3786 dev_err(&pdev->dev, "can't remap ctl region\n");
3787 goto err_reg;
3788 }
3789
3790 /* Disable interrupts on the PLX before resetting it */
3791 cy_writew(&ctl_addr->intr_ctrl_stat,
3792 readw(&ctl_addr->intr_ctrl_stat) & ~0x0900);
3793
3794 plx_init(pdev, irq, addr0);
3795
3796 mailbox = readl(&ctl_addr->mail_box_0);
3797
3798 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
3799 mailbox == ZE_V1 ? CyPCI_Ze_win : CyPCI_Zwin);
3800 if (addr2 == NULL) {
3801 dev_err(&pdev->dev, "can't remap base region\n");
3802 goto err_unmap;
3803 }
3804
3805 if (mailbox == ZE_V1) {
3806 card_name = "Cyclades-Ze";
3807 } else {
3808 card_name = "Cyclades-8Zo";
3809#ifdef CY_PCI_DEBUG
3810 if (mailbox == ZO_V1) {
3811 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3812 dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
3813 "id %lx, ver %lx\n", (ulong)(0xff &
3814 readl(&((struct CUSTOM_REG *)addr2)->
3815 fpga_id)), (ulong)(0xff &
3816 readl(&((struct CUSTOM_REG *)addr2)->
3817 fpga_version)));
3818 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3819 } else {
3820 dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
3821 "Cyclades-Z board. FPGA not loaded\n");
3822 }
3823#endif
3824 /* The following clears the firmware id word. This
3825 ensures that the driver will not attempt to talk to
3826 the board until it has been properly initialized.
3827 */
3828 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
3829 cy_writel(addr2 + ID_ADDRESS, 0L);
3830 }
3831
3832 retval = cyz_load_fw(pdev, addr2, addr0, irq);
3833 if (retval <= 0)
3834 goto err_unmap;
3835 nchan = retval;
3836 }
3837
3838 if ((cy_next_channel + nchan) > NR_PORTS) {
3839 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
3840 "channels are available. Change NR_PORTS in "
3841 "cyclades.c and recompile kernel.\n");
3842 goto err_unmap;
3843 }
3844 /* fill the next cy_card structure available */
3845 for (card_no = 0; card_no < NR_CARDS; card_no++) {
3846 if (cy_card[card_no].base_addr == NULL)
3847 break;
3848 }
3849 if (card_no == NR_CARDS) { /* no more cy_cards available */
3850 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
3851 "more cards can be used. Change NR_CARDS in "
3852 "cyclades.c and recompile kernel.\n");
3853 goto err_unmap;
3854 }
3855
3856 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3857 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3858 /* allocate IRQ */
3859 retval = request_irq(irq, cyy_interrupt,
3860 IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
3861 if (retval) {
3862 dev_err(&pdev->dev, "could not allocate IRQ\n");
3863 goto err_unmap;
3864 }
3865 cy_card[card_no].num_chips = nchan / CyPORTS_PER_CHIP;
3866 } else {
3867 struct FIRM_ID __iomem *firm_id = addr2 + ID_ADDRESS;
3868 struct ZFW_CTRL __iomem *zfw_ctrl;
3869
3870 zfw_ctrl = addr2 + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3871
3872 cy_card[card_no].hw_ver = mailbox;
3873 cy_card[card_no].num_chips = (unsigned int)-1;
3874 cy_card[card_no].board_ctrl = &zfw_ctrl->board_ctrl;
3875#ifdef CONFIG_CYZ_INTR
3876 /* allocate IRQ only if board has an IRQ */
3877 if (irq != 0 && irq != 255) {
3878 retval = request_irq(irq, cyz_interrupt,
3879 IRQF_SHARED, "Cyclades-Z",
3880 &cy_card[card_no]);
3881 if (retval) {
3882 dev_err(&pdev->dev, "could not allocate IRQ\n");
3883 goto err_unmap;
3884 }
3885 }
3886#endif /* CONFIG_CYZ_INTR */
3887 }
3888
3889 /* set cy_card */
3890 cy_card[card_no].base_addr = addr2;
3891 cy_card[card_no].ctl_addr.p9050 = addr0;
3892 cy_card[card_no].irq = irq;
3893 cy_card[card_no].bus_index = 1;
3894 cy_card[card_no].first_line = cy_next_channel;
3895 cy_card[card_no].nports = nchan;
3896 retval = cy_init_card(&cy_card[card_no]);
3897 if (retval)
3898 goto err_null;
3899
3900 pci_set_drvdata(pdev, &cy_card[card_no]);
3901
3902 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3903 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3904 /* enable interrupts in the PCI interface */
3905 plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
3906 switch (plx_ver) {
3907 case PLX_9050:
3908 cy_writeb(addr0 + 0x4c, 0x43);
3909 break;
3910
3911 case PLX_9060:
3912 case PLX_9080:
3913 default: /* Old boards, use PLX_9060 */
3914 {
3915 struct RUNTIME_9060 __iomem *ctl_addr = addr0;
3916 plx_init(pdev, irq, ctl_addr);
3917 cy_writew(&ctl_addr->intr_ctrl_stat,
3918 readw(&ctl_addr->intr_ctrl_stat) | 0x0900);
3919 break;
3920 }
3921 }
3922 }
3923
3924 dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
3925 "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
3926 for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
3927 tty_register_device(cy_serial_driver, i, &pdev->dev);
3928 cy_next_channel += nchan;
3929
3930 return 0;
3931err_null:
3932 cy_card[card_no].base_addr = NULL;
3933 free_irq(irq, &cy_card[card_no]);
3934err_unmap:
3935 iounmap(addr0);
3936 if (addr2)
3937 iounmap(addr2);
3938err_reg:
3939 pci_release_regions(pdev);
3940err_dis:
3941 pci_disable_device(pdev);
3942err:
3943 return retval;
3944}
3945
3946static void __devexit cy_pci_remove(struct pci_dev *pdev)
3947{
3948 struct cyclades_card *cinfo = pci_get_drvdata(pdev);
3949 unsigned int i;
3950
3951 /* non-Z with old PLX */
3952 if (!cy_is_Z(cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
3953 PLX_9050)
3954 cy_writeb(cinfo->ctl_addr.p9050 + 0x4c, 0);
3955 else
3956#ifndef CONFIG_CYZ_INTR
3957 if (!cy_is_Z(cinfo))
3958#endif
3959 cy_writew(&cinfo->ctl_addr.p9060->intr_ctrl_stat,
3960 readw(&cinfo->ctl_addr.p9060->intr_ctrl_stat) &
3961 ~0x0900);
3962
3963 iounmap(cinfo->base_addr);
3964 if (cinfo->ctl_addr.p9050)
3965 iounmap(cinfo->ctl_addr.p9050);
3966 if (cinfo->irq
3967#ifndef CONFIG_CYZ_INTR
3968 && !cy_is_Z(cinfo)
3969#endif /* CONFIG_CYZ_INTR */
3970 )
3971 free_irq(cinfo->irq, cinfo);
3972 pci_release_regions(pdev);
3973
3974 cinfo->base_addr = NULL;
3975 for (i = cinfo->first_line; i < cinfo->first_line +
3976 cinfo->nports; i++)
3977 tty_unregister_device(cy_serial_driver, i);
3978 cinfo->nports = 0;
3979 kfree(cinfo->ports);
3980}
3981
3982static struct pci_driver cy_pci_driver = {
3983 .name = "cyclades",
3984 .id_table = cy_pci_dev_id,
3985 .probe = cy_pci_probe,
3986 .remove = __devexit_p(cy_pci_remove)
3987};
3988#endif
3989
3990static int cyclades_proc_show(struct seq_file *m, void *v)
3991{
3992 struct cyclades_port *info;
3993 unsigned int i, j;
3994 __u32 cur_jifs = jiffies;
3995
3996 seq_puts(m, "Dev TimeOpen BytesOut IdleOut BytesIn "
3997 "IdleIn Overruns Ldisc\n");
3998
3999 /* Output one line for each known port */
4000 for (i = 0; i < NR_CARDS; i++)
4001 for (j = 0; j < cy_card[i].nports; j++) {
4002 info = &cy_card[i].ports[j];
4003
4004 if (info->port.count) {
4005 /* XXX is the ldisc num worth this? */
4006 struct tty_struct *tty;
4007 struct tty_ldisc *ld;
4008 int num = 0;
4009 tty = tty_port_tty_get(&info->port);
4010 if (tty) {
4011 ld = tty_ldisc_ref(tty);
4012 if (ld) {
4013 num = ld->ops->num;
4014 tty_ldisc_deref(ld);
4015 }
4016 tty_kref_put(tty);
4017 }
4018 seq_printf(m, "%3d %8lu %10lu %8lu "
4019 "%10lu %8lu %9lu %6d\n", info->line,
4020 (cur_jifs - info->idle_stats.in_use) /
4021 HZ, info->idle_stats.xmit_bytes,
4022 (cur_jifs - info->idle_stats.xmit_idle)/
4023 HZ, info->idle_stats.recv_bytes,
4024 (cur_jifs - info->idle_stats.recv_idle)/
4025 HZ, info->idle_stats.overruns,
4026 num);
4027 } else
4028 seq_printf(m, "%3d %8lu %10lu %8lu "
4029 "%10lu %8lu %9lu %6ld\n",
4030 info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
4031 }
4032 return 0;
4033}
4034
4035static int cyclades_proc_open(struct inode *inode, struct file *file)
4036{
4037 return single_open(file, cyclades_proc_show, NULL);
4038}
4039
4040static const struct file_operations cyclades_proc_fops = {
4041 .owner = THIS_MODULE,
4042 .open = cyclades_proc_open,
4043 .read = seq_read,
4044 .llseek = seq_lseek,
4045 .release = single_release,
4046};
4047
4048/* The serial driver boot-time initialization code!
4049 Hardware I/O ports are mapped to character special devices on a
4050 first found, first allocated manner. That is, this code searches
4051 for Cyclom cards in the system. As each is found, it is probed
4052 to discover how many chips (and thus how many ports) are present.
4053 These ports are mapped to the tty ports 32 and upward in monotonic
4054 fashion. If an 8-port card is replaced with a 16-port card, the
4055 port mapping on a following card will shift.
4056
4057 This approach is different from what is used in the other serial
4058 device driver because the Cyclom is more properly a multiplexer,
4059 not just an aggregation of serial ports on one card.
4060
4061 If there are more cards with more ports than have been
4062 statically allocated above, a warning is printed and the
4063 extra ports are ignored.
4064 */
4065
4066static const struct tty_operations cy_ops = {
4067 .open = cy_open,
4068 .close = cy_close,
4069 .write = cy_write,
4070 .put_char = cy_put_char,
4071 .flush_chars = cy_flush_chars,
4072 .write_room = cy_write_room,
4073 .chars_in_buffer = cy_chars_in_buffer,
4074 .flush_buffer = cy_flush_buffer,
4075 .ioctl = cy_ioctl,
4076 .throttle = cy_throttle,
4077 .unthrottle = cy_unthrottle,
4078 .set_termios = cy_set_termios,
4079 .stop = cy_stop,
4080 .start = cy_start,
4081 .hangup = cy_hangup,
4082 .break_ctl = cy_break,
4083 .wait_until_sent = cy_wait_until_sent,
4084 .tiocmget = cy_tiocmget,
4085 .tiocmset = cy_tiocmset,
4086 .get_icount = cy_get_icount,
4087 .proc_fops = &cyclades_proc_fops,
4088};
4089
4090static int __init cy_init(void)
4091{
4092 unsigned int nboards;
4093 int retval = -ENOMEM;
4094
4095 cy_serial_driver = alloc_tty_driver(NR_PORTS);
4096 if (!cy_serial_driver)
4097 goto err;
4098
4099 printk(KERN_INFO "Cyclades driver " CY_VERSION "\n");
4100
4101 /* Initialize the tty_driver structure */
4102
4103 cy_serial_driver->owner = THIS_MODULE;
4104 cy_serial_driver->driver_name = "cyclades";
4105 cy_serial_driver->name = "ttyC";
4106 cy_serial_driver->major = CYCLADES_MAJOR;
4107 cy_serial_driver->minor_start = 0;
4108 cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4109 cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
4110 cy_serial_driver->init_termios = tty_std_termios;
4111 cy_serial_driver->init_termios.c_cflag =
4112 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4113 cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4114 tty_set_operations(cy_serial_driver, &cy_ops);
4115
4116 retval = tty_register_driver(cy_serial_driver);
4117 if (retval) {
4118 printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
4119 goto err_frtty;
4120 }
4121
4122 /* the code below is responsible to find the boards. Each different
4123 type of board has its own detection routine. If a board is found,
4124 the next cy_card structure available is set by the detection
4125 routine. These functions are responsible for checking the
4126 availability of cy_card and cy_port data structures and updating
4127 the cy_next_channel. */
4128
4129 /* look for isa boards */
4130 nboards = cy_detect_isa();
4131
4132#ifdef CONFIG_PCI
4133 /* look for pci boards */
4134 retval = pci_register_driver(&cy_pci_driver);
4135 if (retval && !nboards) {
4136 tty_unregister_driver(cy_serial_driver);
4137 goto err_frtty;
4138 }
4139#endif
4140
4141 return 0;
4142err_frtty:
4143 put_tty_driver(cy_serial_driver);
4144err:
4145 return retval;
4146} /* cy_init */
4147
4148static void __exit cy_cleanup_module(void)
4149{
4150 struct cyclades_card *card;
4151 unsigned int i, e1;
4152
4153#ifndef CONFIG_CYZ_INTR
4154 del_timer_sync(&cyz_timerlist);
4155#endif /* CONFIG_CYZ_INTR */
4156
4157 e1 = tty_unregister_driver(cy_serial_driver);
4158 if (e1)
4159 printk(KERN_ERR "failed to unregister Cyclades serial "
4160 "driver(%d)\n", e1);
4161
4162#ifdef CONFIG_PCI
4163 pci_unregister_driver(&cy_pci_driver);
4164#endif
4165
4166 for (i = 0; i < NR_CARDS; i++) {
4167 card = &cy_card[i];
4168 if (card->base_addr) {
4169 /* clear interrupt */
4170 cy_writeb(card->base_addr + Cy_ClrIntr, 0);
4171 iounmap(card->base_addr);
4172 if (card->ctl_addr.p9050)
4173 iounmap(card->ctl_addr.p9050);
4174 if (card->irq
4175#ifndef CONFIG_CYZ_INTR
4176 && !cy_is_Z(card)
4177#endif /* CONFIG_CYZ_INTR */
4178 )
4179 free_irq(card->irq, card);
4180 for (e1 = card->first_line; e1 < card->first_line +
4181 card->nports; e1++)
4182 tty_unregister_device(cy_serial_driver, e1);
4183 kfree(card->ports);
4184 }
4185 }
4186
4187 put_tty_driver(cy_serial_driver);
4188} /* cy_cleanup_module */
4189
4190module_init(cy_init);
4191module_exit(cy_cleanup_module);
4192
4193MODULE_LICENSE("GPL");
4194MODULE_VERSION(CY_VERSION);
4195MODULE_ALIAS_CHARDEV_MAJOR(CYCLADES_MAJOR);
4196MODULE_FIRMWARE("cyzfirm.bin");
diff --git a/drivers/tty/hvc/Kconfig b/drivers/tty/hvc/Kconfig
new file mode 100644
index 000000000000..6f2c9809f1fb
--- /dev/null
+++ b/drivers/tty/hvc/Kconfig
@@ -0,0 +1,105 @@
1config HVC_DRIVER
2 bool
3 help
4 Generic "hypervisor virtual console" infrastructure for various
5 hypervisors (pSeries, iSeries, Xen, lguest).
6 It will automatically be selected if one of the back-end console drivers
7 is selected.
8
9config HVC_IRQ
10 bool
11
12config HVC_CONSOLE
13 bool "pSeries Hypervisor Virtual Console support"
14 depends on PPC_PSERIES
15 select HVC_DRIVER
16 select HVC_IRQ
17 help
18 pSeries machines when partitioned support a hypervisor virtual
19 console. This driver allows each pSeries partition to have a console
20 which is accessed via the HMC.
21
22config HVC_ISERIES
23 bool "iSeries Hypervisor Virtual Console support"
24 depends on PPC_ISERIES
25 default y
26 select HVC_DRIVER
27 select HVC_IRQ
28 select VIOPATH
29 help
30 iSeries machines support a hypervisor virtual console.
31
32config HVC_RTAS
33 bool "IBM RTAS Console support"
34 depends on PPC_RTAS
35 select HVC_DRIVER
36 help
37 IBM Console device driver which makes use of RTAS
38
39config HVC_BEAT
40 bool "Toshiba's Beat Hypervisor Console support"
41 depends on PPC_CELLEB
42 select HVC_DRIVER
43 help
44 Toshiba's Cell Reference Set Beat Console device driver
45
46config HVC_IUCV
47 bool "z/VM IUCV Hypervisor console support (VM only)"
48 depends on S390
49 select HVC_DRIVER
50 select IUCV
51 default y
52 help
53 This driver provides a Hypervisor console (HVC) back-end to access
54 a Linux (console) terminal via a z/VM IUCV communication path.
55
56config HVC_XEN
57 bool "Xen Hypervisor Console support"
58 depends on XEN
59 select HVC_DRIVER
60 select HVC_IRQ
61 default y
62 help
63 Xen virtual console device driver
64
65config HVC_UDBG
66 bool "udbg based fake hypervisor console"
67 depends on PPC && EXPERIMENTAL
68 select HVC_DRIVER
69 default n
70
71config HVC_DCC
72 bool "ARM JTAG DCC console"
73 depends on ARM
74 select HVC_DRIVER
75 help
76 This console uses the JTAG DCC on ARM to create a console under the HVC
77 driver. This console is used through a JTAG only on ARM. If you don't have
78 a JTAG then you probably don't want this option.
79
80config HVC_BFIN_JTAG
81 bool "Blackfin JTAG console"
82 depends on BLACKFIN
83 select HVC_DRIVER
84 help
85 This console uses the Blackfin JTAG to create a console under the
86 the HVC driver. If you don't have JTAG, then you probably don't
87 want this option.
88
89config HVCS
90 tristate "IBM Hypervisor Virtual Console Server support"
91 depends on PPC_PSERIES && HVC_CONSOLE
92 help
93 Partitionable IBM Power5 ppc64 machines allow hosting of
94 firmware virtual consoles from one Linux partition by
95 another Linux partition. This driver allows console data
96 from Linux partitions to be accessed through TTY device
97 interfaces in the device tree of a Linux partition running
98 this driver.
99
100 To compile this driver as a module, choose M here: the
101 module will be called hvcs. Additionally, this module
102 will depend on arch specific APIs exported from hvcserver.ko
103 which will also be compiled when this driver is built as a
104 module.
105
diff --git a/drivers/tty/hvc/Makefile b/drivers/tty/hvc/Makefile
new file mode 100644
index 000000000000..40a25d93fe52
--- /dev/null
+++ b/drivers/tty/hvc/Makefile
@@ -0,0 +1,13 @@
1obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o
2obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o
3obj-$(CONFIG_HVC_RTAS) += hvc_rtas.o
4obj-$(CONFIG_HVC_TILE) += hvc_tile.o
5obj-$(CONFIG_HVC_DCC) += hvc_dcc.o
6obj-$(CONFIG_HVC_BEAT) += hvc_beat.o
7obj-$(CONFIG_HVC_DRIVER) += hvc_console.o
8obj-$(CONFIG_HVC_IRQ) += hvc_irq.o
9obj-$(CONFIG_HVC_XEN) += hvc_xen.o
10obj-$(CONFIG_HVC_IUCV) += hvc_iucv.o
11obj-$(CONFIG_HVC_UDBG) += hvc_udbg.o
12obj-$(CONFIG_HVC_BFIN_JTAG) += hvc_bfin_jtag.o
13obj-$(CONFIG_HVCS) += hvcs.o
diff --git a/drivers/tty/hvc/hvc_beat.c b/drivers/tty/hvc/hvc_beat.c
new file mode 100644
index 000000000000..5fe4631e2a61
--- /dev/null
+++ b/drivers/tty/hvc/hvc_beat.c
@@ -0,0 +1,134 @@
1/*
2 * Beat hypervisor console driver
3 *
4 * (C) Copyright 2006 TOSHIBA CORPORATION
5 *
6 * This code is based on drivers/char/hvc_rtas.c:
7 * (C) Copyright IBM Corporation 2001-2005
8 * (C) Copyright Red Hat, Inc. 2005
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/string.h>
29#include <linux/console.h>
30#include <asm/prom.h>
31#include <asm/hvconsole.h>
32#include <asm/firmware.h>
33
34#include "hvc_console.h"
35
36extern int64_t beat_get_term_char(uint64_t, uint64_t *, uint64_t *, uint64_t *);
37extern int64_t beat_put_term_char(uint64_t, uint64_t, uint64_t, uint64_t);
38
39struct hvc_struct *hvc_beat_dev = NULL;
40
41/* bug: only one queue is available regardless of vtermno */
42static int hvc_beat_get_chars(uint32_t vtermno, char *buf, int cnt)
43{
44 static unsigned char q[sizeof(unsigned long) * 2]
45 __attribute__((aligned(sizeof(unsigned long))));
46 static int qlen = 0;
47 u64 got;
48
49again:
50 if (qlen) {
51 if (qlen > cnt) {
52 memcpy(buf, q, cnt);
53 qlen -= cnt;
54 memmove(q + cnt, q, qlen);
55 return cnt;
56 } else { /* qlen <= cnt */
57 int r;
58
59 memcpy(buf, q, qlen);
60 r = qlen;
61 qlen = 0;
62 return r;
63 }
64 }
65 if (beat_get_term_char(vtermno, &got,
66 ((u64 *)q), ((u64 *)q) + 1) == 0) {
67 qlen = got;
68 goto again;
69 }
70 return 0;
71}
72
73static int hvc_beat_put_chars(uint32_t vtermno, const char *buf, int cnt)
74{
75 unsigned long kb[2];
76 int rest, nlen;
77
78 for (rest = cnt; rest > 0; rest -= nlen) {
79 nlen = (rest > 16) ? 16 : rest;
80 memcpy(kb, buf, nlen);
81 beat_put_term_char(vtermno, nlen, kb[0], kb[1]);
82 buf += nlen;
83 }
84 return cnt;
85}
86
87static const struct hv_ops hvc_beat_get_put_ops = {
88 .get_chars = hvc_beat_get_chars,
89 .put_chars = hvc_beat_put_chars,
90};
91
92static int hvc_beat_useit = 1;
93
94static int hvc_beat_config(char *p)
95{
96 hvc_beat_useit = simple_strtoul(p, NULL, 0);
97 return 0;
98}
99
100static int __init hvc_beat_console_init(void)
101{
102 if (hvc_beat_useit && of_machine_is_compatible("Beat")) {
103 hvc_instantiate(0, 0, &hvc_beat_get_put_ops);
104 }
105 return 0;
106}
107
108/* temp */
109static int __init hvc_beat_init(void)
110{
111 struct hvc_struct *hp;
112
113 if (!firmware_has_feature(FW_FEATURE_BEAT))
114 return -ENODEV;
115
116 hp = hvc_alloc(0, NO_IRQ, &hvc_beat_get_put_ops, 16);
117 if (IS_ERR(hp))
118 return PTR_ERR(hp);
119 hvc_beat_dev = hp;
120 return 0;
121}
122
123static void __exit hvc_beat_exit(void)
124{
125 if (hvc_beat_dev)
126 hvc_remove(hvc_beat_dev);
127}
128
129module_init(hvc_beat_init);
130module_exit(hvc_beat_exit);
131
132__setup("hvc_beat=", hvc_beat_config);
133
134console_initcall(hvc_beat_console_init);
diff --git a/drivers/tty/hvc/hvc_bfin_jtag.c b/drivers/tty/hvc/hvc_bfin_jtag.c
new file mode 100644
index 000000000000..31d6cc6a77af
--- /dev/null
+++ b/drivers/tty/hvc/hvc_bfin_jtag.c
@@ -0,0 +1,105 @@
1/*
2 * Console via Blackfin JTAG Communication
3 *
4 * Copyright 2008-2011 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#include <linux/console.h>
12#include <linux/delay.h>
13#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/moduleparam.h>
16#include <linux/types.h>
17
18#include "hvc_console.h"
19
20/* See the Debug/Emulation chapter in the HRM */
21#define EMUDOF 0x00000001 /* EMUDAT_OUT full & valid */
22#define EMUDIF 0x00000002 /* EMUDAT_IN full & valid */
23#define EMUDOOVF 0x00000004 /* EMUDAT_OUT overflow */
24#define EMUDIOVF 0x00000008 /* EMUDAT_IN overflow */
25
26/* Helper functions to glue the register API to simple C operations */
27static inline uint32_t bfin_write_emudat(uint32_t emudat)
28{
29 __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
30 return emudat;
31}
32
33static inline uint32_t bfin_read_emudat(void)
34{
35 uint32_t emudat;
36 __asm__ __volatile__("%0 = emudat;" : "=d"(emudat));
37 return emudat;
38}
39
40/* Send data to the host */
41static int hvc_bfin_put_chars(uint32_t vt, const char *buf, int count)
42{
43 static uint32_t outbound_len;
44 uint32_t emudat;
45 int ret;
46
47 if (bfin_read_DBGSTAT() & EMUDOF)
48 return 0;
49
50 if (!outbound_len) {
51 outbound_len = count;
52 bfin_write_emudat(outbound_len);
53 return 0;
54 }
55
56 ret = min(outbound_len, (uint32_t)4);
57 memcpy(&emudat, buf, ret);
58 bfin_write_emudat(emudat);
59 outbound_len -= ret;
60
61 return ret;
62}
63
64/* Receive data from the host */
65static int hvc_bfin_get_chars(uint32_t vt, char *buf, int count)
66{
67 static uint32_t inbound_len;
68 uint32_t emudat;
69 int ret;
70
71 if (!(bfin_read_DBGSTAT() & EMUDIF))
72 return 0;
73 emudat = bfin_read_emudat();
74
75 if (!inbound_len) {
76 inbound_len = emudat;
77 return 0;
78 }
79
80 ret = min(inbound_len, (uint32_t)4);
81 memcpy(buf, &emudat, ret);
82 inbound_len -= ret;
83
84 return ret;
85}
86
87/* Glue the HVC layers to the Blackfin layers */
88static const struct hv_ops hvc_bfin_get_put_ops = {
89 .get_chars = hvc_bfin_get_chars,
90 .put_chars = hvc_bfin_put_chars,
91};
92
93static int __init hvc_bfin_console_init(void)
94{
95 hvc_instantiate(0, 0, &hvc_bfin_get_put_ops);
96 return 0;
97}
98console_initcall(hvc_bfin_console_init);
99
100static int __init hvc_bfin_init(void)
101{
102 hvc_alloc(0, 0, &hvc_bfin_get_put_ops, 128);
103 return 0;
104}
105device_initcall(hvc_bfin_init);
diff --git a/drivers/tty/hvc/hvc_console.c b/drivers/tty/hvc/hvc_console.c
new file mode 100644
index 000000000000..e9cba13ee800
--- /dev/null
+++ b/drivers/tty/hvc/hvc_console.c
@@ -0,0 +1,914 @@
1/*
2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
4 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5 * Copyright (C) 2004 IBM Corporation
6 *
7 * Additional Author(s):
8 * Ryan S. Arnold <rsa@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/console.h>
26#include <linux/cpumask.h>
27#include <linux/init.h>
28#include <linux/kbd_kern.h>
29#include <linux/kernel.h>
30#include <linux/kthread.h>
31#include <linux/list.h>
32#include <linux/module.h>
33#include <linux/major.h>
34#include <linux/sysrq.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/sched.h>
38#include <linux/spinlock.h>
39#include <linux/delay.h>
40#include <linux/freezer.h>
41#include <linux/slab.h>
42
43#include <asm/uaccess.h>
44
45#include "hvc_console.h"
46
47#define HVC_MAJOR 229
48#define HVC_MINOR 0
49
50/*
51 * Wait this long per iteration while trying to push buffered data to the
52 * hypervisor before allowing the tty to complete a close operation.
53 */
54#define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
55
56/*
57 * These sizes are most efficient for vio, because they are the
58 * native transfer size. We could make them selectable in the
59 * future to better deal with backends that want other buffer sizes.
60 */
61#define N_OUTBUF 16
62#define N_INBUF 16
63
64#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
65
66static struct tty_driver *hvc_driver;
67static struct task_struct *hvc_task;
68
69/* Picks up late kicks after list walk but before schedule() */
70static int hvc_kicked;
71
72static int hvc_init(void);
73
74#ifdef CONFIG_MAGIC_SYSRQ
75static int sysrq_pressed;
76#endif
77
78/* dynamic list of hvc_struct instances */
79static LIST_HEAD(hvc_structs);
80
81/*
82 * Protect the list of hvc_struct instances from inserts and removals during
83 * list traversal.
84 */
85static DEFINE_SPINLOCK(hvc_structs_lock);
86
87/*
88 * This value is used to assign a tty->index value to a hvc_struct based
89 * upon order of exposure via hvc_probe(), when we can not match it to
90 * a console candidate registered with hvc_instantiate().
91 */
92static int last_hvc = -1;
93
94/*
95 * Do not call this function with either the hvc_structs_lock or the hvc_struct
96 * lock held. If successful, this function increments the kref reference
97 * count against the target hvc_struct so it should be released when finished.
98 */
99static struct hvc_struct *hvc_get_by_index(int index)
100{
101 struct hvc_struct *hp;
102 unsigned long flags;
103
104 spin_lock(&hvc_structs_lock);
105
106 list_for_each_entry(hp, &hvc_structs, next) {
107 spin_lock_irqsave(&hp->lock, flags);
108 if (hp->index == index) {
109 kref_get(&hp->kref);
110 spin_unlock_irqrestore(&hp->lock, flags);
111 spin_unlock(&hvc_structs_lock);
112 return hp;
113 }
114 spin_unlock_irqrestore(&hp->lock, flags);
115 }
116 hp = NULL;
117
118 spin_unlock(&hvc_structs_lock);
119 return hp;
120}
121
122
123/*
124 * Initial console vtermnos for console API usage prior to full console
125 * initialization. Any vty adapter outside this range will not have usable
126 * console interfaces but can still be used as a tty device. This has to be
127 * static because kmalloc will not work during early console init.
128 */
129static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
130static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
131 {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
132
133/*
134 * Console APIs, NOT TTY. These APIs are available immediately when
135 * hvc_console_setup() finds adapters.
136 */
137
138static void hvc_console_print(struct console *co, const char *b,
139 unsigned count)
140{
141 char c[N_OUTBUF] __ALIGNED__;
142 unsigned i = 0, n = 0;
143 int r, donecr = 0, index = co->index;
144
145 /* Console access attempt outside of acceptable console range. */
146 if (index >= MAX_NR_HVC_CONSOLES)
147 return;
148
149 /* This console adapter was removed so it is not usable. */
150 if (vtermnos[index] == -1)
151 return;
152
153 while (count > 0 || i > 0) {
154 if (count > 0 && i < sizeof(c)) {
155 if (b[n] == '\n' && !donecr) {
156 c[i++] = '\r';
157 donecr = 1;
158 } else {
159 c[i++] = b[n++];
160 donecr = 0;
161 --count;
162 }
163 } else {
164 r = cons_ops[index]->put_chars(vtermnos[index], c, i);
165 if (r <= 0) {
166 /* throw away chars on error */
167 i = 0;
168 } else if (r > 0) {
169 i -= r;
170 if (i > 0)
171 memmove(c, c+r, i);
172 }
173 }
174 }
175}
176
177static struct tty_driver *hvc_console_device(struct console *c, int *index)
178{
179 if (vtermnos[c->index] == -1)
180 return NULL;
181
182 *index = c->index;
183 return hvc_driver;
184}
185
186static int __init hvc_console_setup(struct console *co, char *options)
187{
188 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
189 return -ENODEV;
190
191 if (vtermnos[co->index] == -1)
192 return -ENODEV;
193
194 return 0;
195}
196
197static struct console hvc_console = {
198 .name = "hvc",
199 .write = hvc_console_print,
200 .device = hvc_console_device,
201 .setup = hvc_console_setup,
202 .flags = CON_PRINTBUFFER,
203 .index = -1,
204};
205
206/*
207 * Early console initialization. Precedes driver initialization.
208 *
209 * (1) we are first, and the user specified another driver
210 * -- index will remain -1
211 * (2) we are first and the user specified no driver
212 * -- index will be set to 0, then we will fail setup.
213 * (3) we are first and the user specified our driver
214 * -- index will be set to user specified driver, and we will fail
215 * (4) we are after driver, and this initcall will register us
216 * -- if the user didn't specify a driver then the console will match
217 *
218 * Note that for cases 2 and 3, we will match later when the io driver
219 * calls hvc_instantiate() and call register again.
220 */
221static int __init hvc_console_init(void)
222{
223 register_console(&hvc_console);
224 return 0;
225}
226console_initcall(hvc_console_init);
227
228/* callback when the kboject ref count reaches zero. */
229static void destroy_hvc_struct(struct kref *kref)
230{
231 struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
232 unsigned long flags;
233
234 spin_lock(&hvc_structs_lock);
235
236 spin_lock_irqsave(&hp->lock, flags);
237 list_del(&(hp->next));
238 spin_unlock_irqrestore(&hp->lock, flags);
239
240 spin_unlock(&hvc_structs_lock);
241
242 kfree(hp);
243}
244
245/*
246 * hvc_instantiate() is an early console discovery method which locates
247 * consoles * prior to the vio subsystem discovering them. Hotplugged
248 * vty adapters do NOT get an hvc_instantiate() callback since they
249 * appear after early console init.
250 */
251int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
252{
253 struct hvc_struct *hp;
254
255 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
256 return -1;
257
258 if (vtermnos[index] != -1)
259 return -1;
260
261 /* make sure no no tty has been registered in this index */
262 hp = hvc_get_by_index(index);
263 if (hp) {
264 kref_put(&hp->kref, destroy_hvc_struct);
265 return -1;
266 }
267
268 vtermnos[index] = vtermno;
269 cons_ops[index] = ops;
270
271 /* reserve all indices up to and including this index */
272 if (last_hvc < index)
273 last_hvc = index;
274
275 /* if this index is what the user requested, then register
276 * now (setup won't fail at this point). It's ok to just
277 * call register again if previously .setup failed.
278 */
279 if (index == hvc_console.index)
280 register_console(&hvc_console);
281
282 return 0;
283}
284EXPORT_SYMBOL_GPL(hvc_instantiate);
285
286/* Wake the sleeping khvcd */
287void hvc_kick(void)
288{
289 hvc_kicked = 1;
290 wake_up_process(hvc_task);
291}
292EXPORT_SYMBOL_GPL(hvc_kick);
293
294static void hvc_unthrottle(struct tty_struct *tty)
295{
296 hvc_kick();
297}
298
299/*
300 * The TTY interface won't be used until after the vio layer has exposed the vty
301 * adapter to the kernel.
302 */
303static int hvc_open(struct tty_struct *tty, struct file * filp)
304{
305 struct hvc_struct *hp;
306 unsigned long flags;
307 int rc = 0;
308
309 /* Auto increments kref reference if found. */
310 if (!(hp = hvc_get_by_index(tty->index)))
311 return -ENODEV;
312
313 spin_lock_irqsave(&hp->lock, flags);
314 /* Check and then increment for fast path open. */
315 if (hp->count++ > 0) {
316 tty_kref_get(tty);
317 spin_unlock_irqrestore(&hp->lock, flags);
318 hvc_kick();
319 return 0;
320 } /* else count == 0 */
321
322 tty->driver_data = hp;
323
324 hp->tty = tty_kref_get(tty);
325
326 spin_unlock_irqrestore(&hp->lock, flags);
327
328 if (hp->ops->notifier_add)
329 rc = hp->ops->notifier_add(hp, hp->data);
330
331 /*
332 * If the notifier fails we return an error. The tty layer
333 * will call hvc_close() after a failed open but we don't want to clean
334 * up there so we'll clean up here and clear out the previously set
335 * tty fields and return the kref reference.
336 */
337 if (rc) {
338 spin_lock_irqsave(&hp->lock, flags);
339 hp->tty = NULL;
340 spin_unlock_irqrestore(&hp->lock, flags);
341 tty_kref_put(tty);
342 tty->driver_data = NULL;
343 kref_put(&hp->kref, destroy_hvc_struct);
344 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
345 }
346 /* Force wakeup of the polling thread */
347 hvc_kick();
348
349 return rc;
350}
351
352static void hvc_close(struct tty_struct *tty, struct file * filp)
353{
354 struct hvc_struct *hp;
355 unsigned long flags;
356
357 if (tty_hung_up_p(filp))
358 return;
359
360 /*
361 * No driver_data means that this close was issued after a failed
362 * hvc_open by the tty layer's release_dev() function and we can just
363 * exit cleanly because the kref reference wasn't made.
364 */
365 if (!tty->driver_data)
366 return;
367
368 hp = tty->driver_data;
369
370 spin_lock_irqsave(&hp->lock, flags);
371
372 if (--hp->count == 0) {
373 /* We are done with the tty pointer now. */
374 hp->tty = NULL;
375 spin_unlock_irqrestore(&hp->lock, flags);
376
377 if (hp->ops->notifier_del)
378 hp->ops->notifier_del(hp, hp->data);
379
380 /* cancel pending tty resize work */
381 cancel_work_sync(&hp->tty_resize);
382
383 /*
384 * Chain calls chars_in_buffer() and returns immediately if
385 * there is no buffered data otherwise sleeps on a wait queue
386 * waking periodically to check chars_in_buffer().
387 */
388 tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
389 } else {
390 if (hp->count < 0)
391 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
392 hp->vtermno, hp->count);
393 spin_unlock_irqrestore(&hp->lock, flags);
394 }
395
396 tty_kref_put(tty);
397 kref_put(&hp->kref, destroy_hvc_struct);
398}
399
400static void hvc_hangup(struct tty_struct *tty)
401{
402 struct hvc_struct *hp = tty->driver_data;
403 unsigned long flags;
404 int temp_open_count;
405
406 if (!hp)
407 return;
408
409 /* cancel pending tty resize work */
410 cancel_work_sync(&hp->tty_resize);
411
412 spin_lock_irqsave(&hp->lock, flags);
413
414 /*
415 * The N_TTY line discipline has problems such that in a close vs
416 * open->hangup case this can be called after the final close so prevent
417 * that from happening for now.
418 */
419 if (hp->count <= 0) {
420 spin_unlock_irqrestore(&hp->lock, flags);
421 return;
422 }
423
424 temp_open_count = hp->count;
425 hp->count = 0;
426 hp->n_outbuf = 0;
427 hp->tty = NULL;
428
429 spin_unlock_irqrestore(&hp->lock, flags);
430
431 if (hp->ops->notifier_hangup)
432 hp->ops->notifier_hangup(hp, hp->data);
433
434 while(temp_open_count) {
435 --temp_open_count;
436 tty_kref_put(tty);
437 kref_put(&hp->kref, destroy_hvc_struct);
438 }
439}
440
441/*
442 * Push buffered characters whether they were just recently buffered or waiting
443 * on a blocked hypervisor. Call this function with hp->lock held.
444 */
445static int hvc_push(struct hvc_struct *hp)
446{
447 int n;
448
449 n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
450 if (n <= 0) {
451 if (n == 0) {
452 hp->do_wakeup = 1;
453 return 0;
454 }
455 /* throw away output on error; this happens when
456 there is no session connected to the vterm. */
457 hp->n_outbuf = 0;
458 } else
459 hp->n_outbuf -= n;
460 if (hp->n_outbuf > 0)
461 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
462 else
463 hp->do_wakeup = 1;
464
465 return n;
466}
467
468static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
469{
470 struct hvc_struct *hp = tty->driver_data;
471 unsigned long flags;
472 int rsize, written = 0;
473
474 /* This write was probably executed during a tty close. */
475 if (!hp)
476 return -EPIPE;
477
478 if (hp->count <= 0)
479 return -EIO;
480
481 spin_lock_irqsave(&hp->lock, flags);
482
483 /* Push pending writes */
484 if (hp->n_outbuf > 0)
485 hvc_push(hp);
486
487 while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
488 if (rsize > count)
489 rsize = count;
490 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
491 count -= rsize;
492 buf += rsize;
493 hp->n_outbuf += rsize;
494 written += rsize;
495 hvc_push(hp);
496 }
497 spin_unlock_irqrestore(&hp->lock, flags);
498
499 /*
500 * Racy, but harmless, kick thread if there is still pending data.
501 */
502 if (hp->n_outbuf)
503 hvc_kick();
504
505 return written;
506}
507
508/**
509 * hvc_set_winsz() - Resize the hvc tty terminal window.
510 * @work: work structure.
511 *
512 * The routine shall not be called within an atomic context because it
513 * might sleep.
514 *
515 * Locking: hp->lock
516 */
517static void hvc_set_winsz(struct work_struct *work)
518{
519 struct hvc_struct *hp;
520 unsigned long hvc_flags;
521 struct tty_struct *tty;
522 struct winsize ws;
523
524 hp = container_of(work, struct hvc_struct, tty_resize);
525
526 spin_lock_irqsave(&hp->lock, hvc_flags);
527 if (!hp->tty) {
528 spin_unlock_irqrestore(&hp->lock, hvc_flags);
529 return;
530 }
531 ws = hp->ws;
532 tty = tty_kref_get(hp->tty);
533 spin_unlock_irqrestore(&hp->lock, hvc_flags);
534
535 tty_do_resize(tty, &ws);
536 tty_kref_put(tty);
537}
538
539/*
540 * This is actually a contract between the driver and the tty layer outlining
541 * how much write room the driver can guarantee will be sent OR BUFFERED. This
542 * driver MUST honor the return value.
543 */
544static int hvc_write_room(struct tty_struct *tty)
545{
546 struct hvc_struct *hp = tty->driver_data;
547
548 if (!hp)
549 return -1;
550
551 return hp->outbuf_size - hp->n_outbuf;
552}
553
554static int hvc_chars_in_buffer(struct tty_struct *tty)
555{
556 struct hvc_struct *hp = tty->driver_data;
557
558 if (!hp)
559 return 0;
560 return hp->n_outbuf;
561}
562
563/*
564 * timeout will vary between the MIN and MAX values defined here. By default
565 * and during console activity we will use a default MIN_TIMEOUT of 10. When
566 * the console is idle, we increase the timeout value on each pass through
567 * msleep until we reach the max. This may be noticeable as a brief (average
568 * one second) delay on the console before the console responds to input when
569 * there has been no input for some time.
570 */
571#define MIN_TIMEOUT (10)
572#define MAX_TIMEOUT (2000)
573static u32 timeout = MIN_TIMEOUT;
574
575#define HVC_POLL_READ 0x00000001
576#define HVC_POLL_WRITE 0x00000002
577
578int hvc_poll(struct hvc_struct *hp)
579{
580 struct tty_struct *tty;
581 int i, n, poll_mask = 0;
582 char buf[N_INBUF] __ALIGNED__;
583 unsigned long flags;
584 int read_total = 0;
585 int written_total = 0;
586
587 spin_lock_irqsave(&hp->lock, flags);
588
589 /* Push pending writes */
590 if (hp->n_outbuf > 0)
591 written_total = hvc_push(hp);
592
593 /* Reschedule us if still some write pending */
594 if (hp->n_outbuf > 0) {
595 poll_mask |= HVC_POLL_WRITE;
596 /* If hvc_push() was not able to write, sleep a few msecs */
597 timeout = (written_total) ? 0 : MIN_TIMEOUT;
598 }
599
600 /* No tty attached, just skip */
601 tty = tty_kref_get(hp->tty);
602 if (tty == NULL)
603 goto bail;
604
605 /* Now check if we can get data (are we throttled ?) */
606 if (test_bit(TTY_THROTTLED, &tty->flags))
607 goto throttled;
608
609 /* If we aren't notifier driven and aren't throttled, we always
610 * request a reschedule
611 */
612 if (!hp->irq_requested)
613 poll_mask |= HVC_POLL_READ;
614
615 /* Read data if any */
616 for (;;) {
617 int count = tty_buffer_request_room(tty, N_INBUF);
618
619 /* If flip is full, just reschedule a later read */
620 if (count == 0) {
621 poll_mask |= HVC_POLL_READ;
622 break;
623 }
624
625 n = hp->ops->get_chars(hp->vtermno, buf, count);
626 if (n <= 0) {
627 /* Hangup the tty when disconnected from host */
628 if (n == -EPIPE) {
629 spin_unlock_irqrestore(&hp->lock, flags);
630 tty_hangup(tty);
631 spin_lock_irqsave(&hp->lock, flags);
632 } else if ( n == -EAGAIN ) {
633 /*
634 * Some back-ends can only ensure a certain min
635 * num of bytes read, which may be > 'count'.
636 * Let the tty clear the flip buff to make room.
637 */
638 poll_mask |= HVC_POLL_READ;
639 }
640 break;
641 }
642 for (i = 0; i < n; ++i) {
643#ifdef CONFIG_MAGIC_SYSRQ
644 if (hp->index == hvc_console.index) {
645 /* Handle the SysRq Hack */
646 /* XXX should support a sequence */
647 if (buf[i] == '\x0f') { /* ^O */
648 /* if ^O is pressed again, reset
649 * sysrq_pressed and flip ^O char */
650 sysrq_pressed = !sysrq_pressed;
651 if (sysrq_pressed)
652 continue;
653 } else if (sysrq_pressed) {
654 handle_sysrq(buf[i]);
655 sysrq_pressed = 0;
656 continue;
657 }
658 }
659#endif /* CONFIG_MAGIC_SYSRQ */
660 tty_insert_flip_char(tty, buf[i], 0);
661 }
662
663 read_total += n;
664 }
665 throttled:
666 /* Wakeup write queue if necessary */
667 if (hp->do_wakeup) {
668 hp->do_wakeup = 0;
669 tty_wakeup(tty);
670 }
671 bail:
672 spin_unlock_irqrestore(&hp->lock, flags);
673
674 if (read_total) {
675 /* Activity is occurring, so reset the polling backoff value to
676 a minimum for performance. */
677 timeout = MIN_TIMEOUT;
678
679 tty_flip_buffer_push(tty);
680 }
681 if (tty)
682 tty_kref_put(tty);
683
684 return poll_mask;
685}
686EXPORT_SYMBOL_GPL(hvc_poll);
687
688/**
689 * __hvc_resize() - Update terminal window size information.
690 * @hp: HVC console pointer
691 * @ws: Terminal window size structure
692 *
693 * Stores the specified window size information in the hvc structure of @hp.
694 * The function schedule the tty resize update.
695 *
696 * Locking: Locking free; the function MUST be called holding hp->lock
697 */
698void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
699{
700 hp->ws = ws;
701 schedule_work(&hp->tty_resize);
702}
703EXPORT_SYMBOL_GPL(__hvc_resize);
704
705/*
706 * This kthread is either polling or interrupt driven. This is determined by
707 * calling hvc_poll() who determines whether a console adapter support
708 * interrupts.
709 */
710static int khvcd(void *unused)
711{
712 int poll_mask;
713 struct hvc_struct *hp;
714
715 set_freezable();
716 do {
717 poll_mask = 0;
718 hvc_kicked = 0;
719 try_to_freeze();
720 wmb();
721 if (!cpus_are_in_xmon()) {
722 spin_lock(&hvc_structs_lock);
723 list_for_each_entry(hp, &hvc_structs, next) {
724 poll_mask |= hvc_poll(hp);
725 }
726 spin_unlock(&hvc_structs_lock);
727 } else
728 poll_mask |= HVC_POLL_READ;
729 if (hvc_kicked)
730 continue;
731 set_current_state(TASK_INTERRUPTIBLE);
732 if (!hvc_kicked) {
733 if (poll_mask == 0)
734 schedule();
735 else {
736 if (timeout < MAX_TIMEOUT)
737 timeout += (timeout >> 6) + 1;
738
739 msleep_interruptible(timeout);
740 }
741 }
742 __set_current_state(TASK_RUNNING);
743 } while (!kthread_should_stop());
744
745 return 0;
746}
747
748static const struct tty_operations hvc_ops = {
749 .open = hvc_open,
750 .close = hvc_close,
751 .write = hvc_write,
752 .hangup = hvc_hangup,
753 .unthrottle = hvc_unthrottle,
754 .write_room = hvc_write_room,
755 .chars_in_buffer = hvc_chars_in_buffer,
756};
757
758struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
759 const struct hv_ops *ops,
760 int outbuf_size)
761{
762 struct hvc_struct *hp;
763 int i;
764
765 /* We wait until a driver actually comes along */
766 if (!hvc_driver) {
767 int err = hvc_init();
768 if (err)
769 return ERR_PTR(err);
770 }
771
772 hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
773 GFP_KERNEL);
774 if (!hp)
775 return ERR_PTR(-ENOMEM);
776
777 hp->vtermno = vtermno;
778 hp->data = data;
779 hp->ops = ops;
780 hp->outbuf_size = outbuf_size;
781 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
782
783 kref_init(&hp->kref);
784
785 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
786 spin_lock_init(&hp->lock);
787 spin_lock(&hvc_structs_lock);
788
789 /*
790 * find index to use:
791 * see if this vterm id matches one registered for console.
792 */
793 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
794 if (vtermnos[i] == hp->vtermno &&
795 cons_ops[i] == hp->ops)
796 break;
797
798 /* no matching slot, just use a counter */
799 if (i >= MAX_NR_HVC_CONSOLES)
800 i = ++last_hvc;
801
802 hp->index = i;
803
804 list_add_tail(&(hp->next), &hvc_structs);
805 spin_unlock(&hvc_structs_lock);
806
807 return hp;
808}
809EXPORT_SYMBOL_GPL(hvc_alloc);
810
811int hvc_remove(struct hvc_struct *hp)
812{
813 unsigned long flags;
814 struct tty_struct *tty;
815
816 spin_lock_irqsave(&hp->lock, flags);
817 tty = tty_kref_get(hp->tty);
818
819 if (hp->index < MAX_NR_HVC_CONSOLES)
820 vtermnos[hp->index] = -1;
821
822 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
823
824 spin_unlock_irqrestore(&hp->lock, flags);
825
826 /*
827 * We 'put' the instance that was grabbed when the kref instance
828 * was initialized using kref_init(). Let the last holder of this
829 * kref cause it to be removed, which will probably be the tty_vhangup
830 * below.
831 */
832 kref_put(&hp->kref, destroy_hvc_struct);
833
834 /*
835 * This function call will auto chain call hvc_hangup.
836 */
837 if (tty) {
838 tty_vhangup(tty);
839 tty_kref_put(tty);
840 }
841 return 0;
842}
843EXPORT_SYMBOL_GPL(hvc_remove);
844
845/* Driver initialization: called as soon as someone uses hvc_alloc(). */
846static int hvc_init(void)
847{
848 struct tty_driver *drv;
849 int err;
850
851 /* We need more than hvc_count adapters due to hotplug additions. */
852 drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
853 if (!drv) {
854 err = -ENOMEM;
855 goto out;
856 }
857
858 drv->owner = THIS_MODULE;
859 drv->driver_name = "hvc";
860 drv->name = "hvc";
861 drv->major = HVC_MAJOR;
862 drv->minor_start = HVC_MINOR;
863 drv->type = TTY_DRIVER_TYPE_SYSTEM;
864 drv->init_termios = tty_std_termios;
865 drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
866 tty_set_operations(drv, &hvc_ops);
867
868 /* Always start the kthread because there can be hotplug vty adapters
869 * added later. */
870 hvc_task = kthread_run(khvcd, NULL, "khvcd");
871 if (IS_ERR(hvc_task)) {
872 printk(KERN_ERR "Couldn't create kthread for console.\n");
873 err = PTR_ERR(hvc_task);
874 goto put_tty;
875 }
876
877 err = tty_register_driver(drv);
878 if (err) {
879 printk(KERN_ERR "Couldn't register hvc console driver\n");
880 goto stop_thread;
881 }
882
883 /*
884 * Make sure tty is fully registered before allowing it to be
885 * found by hvc_console_device.
886 */
887 smp_mb();
888 hvc_driver = drv;
889 return 0;
890
891stop_thread:
892 kthread_stop(hvc_task);
893 hvc_task = NULL;
894put_tty:
895 put_tty_driver(drv);
896out:
897 return err;
898}
899
900/* This isn't particularly necessary due to this being a console driver
901 * but it is nice to be thorough.
902 */
903static void __exit hvc_exit(void)
904{
905 if (hvc_driver) {
906 kthread_stop(hvc_task);
907
908 tty_unregister_driver(hvc_driver);
909 /* return tty_struct instances allocated in hvc_init(). */
910 put_tty_driver(hvc_driver);
911 unregister_console(&hvc_console);
912 }
913}
914module_exit(hvc_exit);
diff --git a/drivers/tty/hvc/hvc_console.h b/drivers/tty/hvc/hvc_console.h
new file mode 100644
index 000000000000..54381eba4e4a
--- /dev/null
+++ b/drivers/tty/hvc/hvc_console.h
@@ -0,0 +1,119 @@
1/*
2 * hvc_console.h
3 * Copyright (C) 2005 IBM Corporation
4 *
5 * Author(s):
6 * Ryan S. Arnold <rsa@us.ibm.com>
7 *
8 * hvc_console header information:
9 * moved here from arch/powerpc/include/asm/hvconsole.h
10 * and drivers/char/hvc_console.c
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#ifndef HVC_CONSOLE_H
28#define HVC_CONSOLE_H
29#include <linux/kref.h>
30#include <linux/tty.h>
31#include <linux/spinlock.h>
32
33/*
34 * This is the max number of console adapters that can/will be found as
35 * console devices on first stage console init. Any number beyond this range
36 * can't be used as a console device but is still a valid tty device.
37 */
38#define MAX_NR_HVC_CONSOLES 16
39
40/*
41 * The Linux TTY code does not support dynamic addition of tty derived devices
42 * so we need to know how many tty devices we might need when space is allocated
43 * for the tty device. Since this driver supports hotplug of vty adapters we
44 * need to make sure we have enough allocated.
45 */
46#define HVC_ALLOC_TTY_ADAPTERS 8
47
48struct hvc_struct {
49 spinlock_t lock;
50 int index;
51 struct tty_struct *tty;
52 int count;
53 int do_wakeup;
54 char *outbuf;
55 int outbuf_size;
56 int n_outbuf;
57 uint32_t vtermno;
58 const struct hv_ops *ops;
59 int irq_requested;
60 int data;
61 struct winsize ws;
62 struct work_struct tty_resize;
63 struct list_head next;
64 struct kref kref; /* ref count & hvc_struct lifetime */
65};
66
67/* implemented by a low level driver */
68struct hv_ops {
69 int (*get_chars)(uint32_t vtermno, char *buf, int count);
70 int (*put_chars)(uint32_t vtermno, const char *buf, int count);
71
72 /* Callbacks for notification. Called in open, close and hangup */
73 int (*notifier_add)(struct hvc_struct *hp, int irq);
74 void (*notifier_del)(struct hvc_struct *hp, int irq);
75 void (*notifier_hangup)(struct hvc_struct *hp, int irq);
76};
77
78/* Register a vterm and a slot index for use as a console (console_init) */
79extern int hvc_instantiate(uint32_t vtermno, int index,
80 const struct hv_ops *ops);
81
82/* register a vterm for hvc tty operation (module_init or hotplug add) */
83extern struct hvc_struct * hvc_alloc(uint32_t vtermno, int data,
84 const struct hv_ops *ops, int outbuf_size);
85/* remove a vterm from hvc tty operation (module_exit or hotplug remove) */
86extern int hvc_remove(struct hvc_struct *hp);
87
88/* data available */
89int hvc_poll(struct hvc_struct *hp);
90void hvc_kick(void);
91
92/* Resize hvc tty terminal window */
93extern void __hvc_resize(struct hvc_struct *hp, struct winsize ws);
94
95static inline void hvc_resize(struct hvc_struct *hp, struct winsize ws)
96{
97 unsigned long flags;
98
99 spin_lock_irqsave(&hp->lock, flags);
100 __hvc_resize(hp, ws);
101 spin_unlock_irqrestore(&hp->lock, flags);
102}
103
104/* default notifier for irq based notification */
105extern int notifier_add_irq(struct hvc_struct *hp, int data);
106extern void notifier_del_irq(struct hvc_struct *hp, int data);
107extern void notifier_hangup_irq(struct hvc_struct *hp, int data);
108
109
110#if defined(CONFIG_XMON) && defined(CONFIG_SMP)
111#include <asm/xmon.h>
112#else
113static inline int cpus_are_in_xmon(void)
114{
115 return 0;
116}
117#endif
118
119#endif // HVC_CONSOLE_H
diff --git a/drivers/tty/hvc/hvc_dcc.c b/drivers/tty/hvc/hvc_dcc.c
new file mode 100644
index 000000000000..435f6facbc23
--- /dev/null
+++ b/drivers/tty/hvc/hvc_dcc.c
@@ -0,0 +1,104 @@
1/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include <linux/console.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/moduleparam.h>
23#include <linux/types.h>
24
25#include <asm/processor.h>
26
27#include "hvc_console.h"
28
29/* DCC Status Bits */
30#define DCC_STATUS_RX (1 << 30)
31#define DCC_STATUS_TX (1 << 29)
32
33static inline u32 __dcc_getstatus(void)
34{
35 u32 __ret;
36 asm volatile("mrc p14, 0, %0, c0, c1, 0 @ read comms ctrl reg"
37 : "=r" (__ret) : : "cc");
38
39 return __ret;
40}
41
42
43static inline char __dcc_getchar(void)
44{
45 char __c;
46
47 asm volatile("mrc p14, 0, %0, c0, c5, 0 @ read comms data reg"
48 : "=r" (__c));
49
50 return __c;
51}
52
53static inline void __dcc_putchar(char c)
54{
55 asm volatile("mcr p14, 0, %0, c0, c5, 0 @ write a char"
56 : /* no output register */
57 : "r" (c));
58}
59
60static int hvc_dcc_put_chars(uint32_t vt, const char *buf, int count)
61{
62 int i;
63
64 for (i = 0; i < count; i++) {
65 while (__dcc_getstatus() & DCC_STATUS_TX)
66 cpu_relax();
67
68 __dcc_putchar(buf[i]);
69 }
70
71 return count;
72}
73
74static int hvc_dcc_get_chars(uint32_t vt, char *buf, int count)
75{
76 int i;
77
78 for (i = 0; i < count; ++i)
79 if (__dcc_getstatus() & DCC_STATUS_RX)
80 buf[i] = __dcc_getchar();
81 else
82 break;
83
84 return i;
85}
86
87static const struct hv_ops hvc_dcc_get_put_ops = {
88 .get_chars = hvc_dcc_get_chars,
89 .put_chars = hvc_dcc_put_chars,
90};
91
92static int __init hvc_dcc_console_init(void)
93{
94 hvc_instantiate(0, 0, &hvc_dcc_get_put_ops);
95 return 0;
96}
97console_initcall(hvc_dcc_console_init);
98
99static int __init hvc_dcc_init(void)
100{
101 hvc_alloc(0, 0, &hvc_dcc_get_put_ops, 128);
102 return 0;
103}
104device_initcall(hvc_dcc_init);
diff --git a/drivers/tty/hvc/hvc_irq.c b/drivers/tty/hvc/hvc_irq.c
new file mode 100644
index 000000000000..2623e177e8d6
--- /dev/null
+++ b/drivers/tty/hvc/hvc_irq.c
@@ -0,0 +1,49 @@
1/*
2 * Copyright IBM Corp. 2001,2008
3 *
4 * This file contains the IRQ specific code for hvc_console
5 *
6 */
7
8#include <linux/interrupt.h>
9
10#include "hvc_console.h"
11
12static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance)
13{
14 /* if hvc_poll request a repoll, then kick the hvcd thread */
15 if (hvc_poll(dev_instance))
16 hvc_kick();
17 return IRQ_HANDLED;
18}
19
20/*
21 * For IRQ based systems these callbacks can be used
22 */
23int notifier_add_irq(struct hvc_struct *hp, int irq)
24{
25 int rc;
26
27 if (!irq) {
28 hp->irq_requested = 0;
29 return 0;
30 }
31 rc = request_irq(irq, hvc_handle_interrupt, IRQF_DISABLED,
32 "hvc_console", hp);
33 if (!rc)
34 hp->irq_requested = 1;
35 return rc;
36}
37
38void notifier_del_irq(struct hvc_struct *hp, int irq)
39{
40 if (!hp->irq_requested)
41 return;
42 free_irq(irq, hp);
43 hp->irq_requested = 0;
44}
45
46void notifier_hangup_irq(struct hvc_struct *hp, int irq)
47{
48 notifier_del_irq(hp, irq);
49}
diff --git a/drivers/tty/hvc/hvc_iseries.c b/drivers/tty/hvc/hvc_iseries.c
new file mode 100644
index 000000000000..21c54955084e
--- /dev/null
+++ b/drivers/tty/hvc/hvc_iseries.c
@@ -0,0 +1,598 @@
1/*
2 * iSeries vio driver interface to hvc_console.c
3 *
4 * This code is based heavily on hvc_vio.c and viocons.c
5 *
6 * Copyright (C) 2006 Stephen Rothwell, IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <stdarg.h>
23#include <linux/types.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
27#include <linux/console.h>
28
29#include <asm/hvconsole.h>
30#include <asm/vio.h>
31#include <asm/prom.h>
32#include <asm/firmware.h>
33#include <asm/iseries/vio.h>
34#include <asm/iseries/hv_call.h>
35#include <asm/iseries/hv_lp_config.h>
36#include <asm/iseries/hv_lp_event.h>
37
38#include "hvc_console.h"
39
40#define VTTY_PORTS 10
41
42static DEFINE_SPINLOCK(consolelock);
43static DEFINE_SPINLOCK(consoleloglock);
44
45static const char hvc_driver_name[] = "hvc_console";
46
47#define IN_BUF_SIZE 200
48
49/*
50 * Our port information.
51 */
52static struct port_info {
53 HvLpIndex lp;
54 u64 seq; /* sequence number of last HV send */
55 u64 ack; /* last ack from HV */
56 struct hvc_struct *hp;
57 int in_start;
58 int in_end;
59 unsigned char in_buf[IN_BUF_SIZE];
60} port_info[VTTY_PORTS] = {
61 [ 0 ... VTTY_PORTS - 1 ] = {
62 .lp = HvLpIndexInvalid
63 }
64};
65
66#define viochar_is_console(pi) ((pi) == &port_info[0])
67
68static struct vio_device_id hvc_driver_table[] __devinitdata = {
69 {"serial", "IBM,iSeries-vty"},
70 { "", "" }
71};
72MODULE_DEVICE_TABLE(vio, hvc_driver_table);
73
74static void hvlog(char *fmt, ...)
75{
76 int i;
77 unsigned long flags;
78 va_list args;
79 static char buf[256];
80
81 spin_lock_irqsave(&consoleloglock, flags);
82 va_start(args, fmt);
83 i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
84 va_end(args);
85 buf[i++] = '\r';
86 HvCall_writeLogBuffer(buf, i);
87 spin_unlock_irqrestore(&consoleloglock, flags);
88}
89
90/*
91 * Initialize the common fields in a charLpEvent
92 */
93static void init_data_event(struct viocharlpevent *viochar, HvLpIndex lp)
94{
95 struct HvLpEvent *hev = &viochar->event;
96
97 memset(viochar, 0, sizeof(struct viocharlpevent));
98
99 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
100 HV_LP_EVENT_INT;
101 hev->xType = HvLpEvent_Type_VirtualIo;
102 hev->xSubtype = viomajorsubtype_chario | viochardata;
103 hev->xSourceLp = HvLpConfig_getLpIndex();
104 hev->xTargetLp = lp;
105 hev->xSizeMinus1 = sizeof(struct viocharlpevent);
106 hev->xSourceInstanceId = viopath_sourceinst(lp);
107 hev->xTargetInstanceId = viopath_targetinst(lp);
108}
109
110static int get_chars(uint32_t vtermno, char *buf, int count)
111{
112 struct port_info *pi;
113 int n = 0;
114 unsigned long flags;
115
116 if (vtermno >= VTTY_PORTS)
117 return -EINVAL;
118 if (count == 0)
119 return 0;
120
121 pi = &port_info[vtermno];
122 spin_lock_irqsave(&consolelock, flags);
123
124 if (pi->in_end == 0)
125 goto done;
126
127 n = pi->in_end - pi->in_start;
128 if (n > count)
129 n = count;
130 memcpy(buf, &pi->in_buf[pi->in_start], n);
131 pi->in_start += n;
132 if (pi->in_start == pi->in_end) {
133 pi->in_start = 0;
134 pi->in_end = 0;
135 }
136done:
137 spin_unlock_irqrestore(&consolelock, flags);
138 return n;
139}
140
141static int put_chars(uint32_t vtermno, const char *buf, int count)
142{
143 struct viocharlpevent *viochar;
144 struct port_info *pi;
145 HvLpEvent_Rc hvrc;
146 unsigned long flags;
147 int sent = 0;
148
149 if (vtermno >= VTTY_PORTS)
150 return -EINVAL;
151
152 pi = &port_info[vtermno];
153
154 spin_lock_irqsave(&consolelock, flags);
155
156 if (viochar_is_console(pi) && !viopath_isactive(pi->lp)) {
157 HvCall_writeLogBuffer(buf, count);
158 sent = count;
159 goto done;
160 }
161
162 viochar = vio_get_event_buffer(viomajorsubtype_chario);
163 if (viochar == NULL) {
164 hvlog("\n\rviocons: Can't get viochar buffer.");
165 goto done;
166 }
167
168 while ((count > 0) && ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
169 int len;
170
171 len = (count > VIOCHAR_MAX_DATA) ? VIOCHAR_MAX_DATA : count;
172
173 if (viochar_is_console(pi))
174 HvCall_writeLogBuffer(buf, len);
175
176 init_data_event(viochar, pi->lp);
177
178 viochar->len = len;
179 viochar->event.xCorrelationToken = pi->seq++;
180 viochar->event.xSizeMinus1 =
181 offsetof(struct viocharlpevent, data) + len;
182
183 memcpy(viochar->data, buf, len);
184
185 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
186 if (hvrc)
187 hvlog("\n\rerror sending event! return code %d\n\r",
188 (int)hvrc);
189 sent += len;
190 count -= len;
191 buf += len;
192 }
193
194 vio_free_event_buffer(viomajorsubtype_chario, viochar);
195done:
196 spin_unlock_irqrestore(&consolelock, flags);
197 return sent;
198}
199
200static const struct hv_ops hvc_get_put_ops = {
201 .get_chars = get_chars,
202 .put_chars = put_chars,
203 .notifier_add = notifier_add_irq,
204 .notifier_del = notifier_del_irq,
205 .notifier_hangup = notifier_hangup_irq,
206};
207
208static int __devinit hvc_vio_probe(struct vio_dev *vdev,
209 const struct vio_device_id *id)
210{
211 struct hvc_struct *hp;
212 struct port_info *pi;
213
214 /* probed with invalid parameters. */
215 if (!vdev || !id)
216 return -EPERM;
217
218 if (vdev->unit_address >= VTTY_PORTS)
219 return -ENODEV;
220
221 pi = &port_info[vdev->unit_address];
222
223 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
224 VIOCHAR_MAX_DATA);
225 if (IS_ERR(hp))
226 return PTR_ERR(hp);
227 pi->hp = hp;
228 dev_set_drvdata(&vdev->dev, pi);
229
230 return 0;
231}
232
233static int __devexit hvc_vio_remove(struct vio_dev *vdev)
234{
235 struct port_info *pi = dev_get_drvdata(&vdev->dev);
236 struct hvc_struct *hp = pi->hp;
237
238 return hvc_remove(hp);
239}
240
241static struct vio_driver hvc_vio_driver = {
242 .id_table = hvc_driver_table,
243 .probe = hvc_vio_probe,
244 .remove = __devexit_p(hvc_vio_remove),
245 .driver = {
246 .name = hvc_driver_name,
247 .owner = THIS_MODULE,
248 }
249};
250
251static void hvc_open_event(struct HvLpEvent *event)
252{
253 unsigned long flags;
254 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
255 u8 port = cevent->virtual_device;
256 struct port_info *pi;
257 int reject = 0;
258
259 if (hvlpevent_is_ack(event)) {
260 if (port >= VTTY_PORTS)
261 return;
262
263 spin_lock_irqsave(&consolelock, flags);
264
265 pi = &port_info[port];
266 if (event->xRc == HvLpEvent_Rc_Good) {
267 pi->seq = pi->ack = 0;
268 /*
269 * This line allows connections from the primary
270 * partition but once one is connected from the
271 * primary partition nothing short of a reboot
272 * of linux will allow access from the hosting
273 * partition again without a required iSeries fix.
274 */
275 pi->lp = event->xTargetLp;
276 }
277
278 spin_unlock_irqrestore(&consolelock, flags);
279 if (event->xRc != HvLpEvent_Rc_Good)
280 printk(KERN_WARNING
281 "hvc: handle_open_event: event->xRc == (%d).\n",
282 event->xRc);
283
284 if (event->xCorrelationToken != 0) {
285 atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
286 atomic_set(aptr, 1);
287 } else
288 printk(KERN_WARNING
289 "hvc: weird...got open ack without atomic\n");
290 return;
291 }
292
293 /* This had better require an ack, otherwise complain */
294 if (!hvlpevent_need_ack(event)) {
295 printk(KERN_WARNING "hvc: viocharopen without ack bit!\n");
296 return;
297 }
298
299 spin_lock_irqsave(&consolelock, flags);
300
301 /* Make sure this is a good virtual tty */
302 if (port >= VTTY_PORTS) {
303 event->xRc = HvLpEvent_Rc_SubtypeError;
304 cevent->subtype_result_code = viorc_openRejected;
305 /*
306 * Flag state here since we can't printk while holding
307 * the consolelock spinlock.
308 */
309 reject = 1;
310 } else {
311 pi = &port_info[port];
312 if ((pi->lp != HvLpIndexInvalid) &&
313 (pi->lp != event->xSourceLp)) {
314 /*
315 * If this is tty is already connected to a different
316 * partition, fail.
317 */
318 event->xRc = HvLpEvent_Rc_SubtypeError;
319 cevent->subtype_result_code = viorc_openRejected;
320 reject = 2;
321 } else {
322 pi->lp = event->xSourceLp;
323 event->xRc = HvLpEvent_Rc_Good;
324 cevent->subtype_result_code = viorc_good;
325 pi->seq = pi->ack = 0;
326 }
327 }
328
329 spin_unlock_irqrestore(&consolelock, flags);
330
331 if (reject == 1)
332 printk(KERN_WARNING "hvc: open rejected: bad virtual tty.\n");
333 else if (reject == 2)
334 printk(KERN_WARNING "hvc: open rejected: console in exclusive "
335 "use by another partition.\n");
336
337 /* Return the acknowledgement */
338 HvCallEvent_ackLpEvent(event);
339}
340
341/*
342 * Handle a close charLpEvent. This should ONLY be an Interrupt because the
343 * virtual console should never actually issue a close event to the hypervisor
344 * because the virtual console never goes away. A close event coming from the
345 * hypervisor simply means that there are no client consoles connected to the
346 * virtual console.
347 */
348static void hvc_close_event(struct HvLpEvent *event)
349{
350 unsigned long flags;
351 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
352 u8 port = cevent->virtual_device;
353
354 if (!hvlpevent_is_int(event)) {
355 printk(KERN_WARNING
356 "hvc: got unexpected close acknowledgement\n");
357 return;
358 }
359
360 if (port >= VTTY_PORTS) {
361 printk(KERN_WARNING
362 "hvc: close message from invalid virtual device.\n");
363 return;
364 }
365
366 /* For closes, just mark the console partition invalid */
367 spin_lock_irqsave(&consolelock, flags);
368
369 if (port_info[port].lp == event->xSourceLp)
370 port_info[port].lp = HvLpIndexInvalid;
371
372 spin_unlock_irqrestore(&consolelock, flags);
373}
374
375static void hvc_data_event(struct HvLpEvent *event)
376{
377 unsigned long flags;
378 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
379 struct port_info *pi;
380 int n;
381 u8 port = cevent->virtual_device;
382
383 if (port >= VTTY_PORTS) {
384 printk(KERN_WARNING "hvc: data on invalid virtual device %d\n",
385 port);
386 return;
387 }
388 if (cevent->len == 0)
389 return;
390
391 /*
392 * Change 05/01/2003 - Ryan Arnold: If a partition other than
393 * the current exclusive partition tries to send us data
394 * events then just drop them on the floor because we don't
395 * want his stinking data. He isn't authorized to receive
396 * data because he wasn't the first one to get the console,
397 * therefore he shouldn't be allowed to send data either.
398 * This will work without an iSeries fix.
399 */
400 pi = &port_info[port];
401 if (pi->lp != event->xSourceLp)
402 return;
403
404 spin_lock_irqsave(&consolelock, flags);
405
406 n = IN_BUF_SIZE - pi->in_end;
407 if (n > cevent->len)
408 n = cevent->len;
409 if (n > 0) {
410 memcpy(&pi->in_buf[pi->in_end], cevent->data, n);
411 pi->in_end += n;
412 }
413 spin_unlock_irqrestore(&consolelock, flags);
414 if (n == 0)
415 printk(KERN_WARNING "hvc: input buffer overflow\n");
416}
417
418static void hvc_ack_event(struct HvLpEvent *event)
419{
420 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
421 unsigned long flags;
422 u8 port = cevent->virtual_device;
423
424 if (port >= VTTY_PORTS) {
425 printk(KERN_WARNING "hvc: data on invalid virtual device\n");
426 return;
427 }
428
429 spin_lock_irqsave(&consolelock, flags);
430 port_info[port].ack = event->xCorrelationToken;
431 spin_unlock_irqrestore(&consolelock, flags);
432}
433
434static void hvc_config_event(struct HvLpEvent *event)
435{
436 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
437
438 if (cevent->data[0] == 0x01)
439 printk(KERN_INFO "hvc: window resized to %d: %d: %d: %d\n",
440 cevent->data[1], cevent->data[2],
441 cevent->data[3], cevent->data[4]);
442 else
443 printk(KERN_WARNING "hvc: unknown config event\n");
444}
445
446static void hvc_handle_event(struct HvLpEvent *event)
447{
448 int charminor;
449
450 if (event == NULL)
451 return;
452
453 charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
454 switch (charminor) {
455 case viocharopen:
456 hvc_open_event(event);
457 break;
458 case viocharclose:
459 hvc_close_event(event);
460 break;
461 case viochardata:
462 hvc_data_event(event);
463 break;
464 case viocharack:
465 hvc_ack_event(event);
466 break;
467 case viocharconfig:
468 hvc_config_event(event);
469 break;
470 default:
471 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
472 event->xRc = HvLpEvent_Rc_InvalidSubtype;
473 HvCallEvent_ackLpEvent(event);
474 }
475 }
476}
477
478static int __init send_open(HvLpIndex remoteLp, void *sem)
479{
480 return HvCallEvent_signalLpEventFast(remoteLp,
481 HvLpEvent_Type_VirtualIo,
482 viomajorsubtype_chario | viocharopen,
483 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
484 viopath_sourceinst(remoteLp),
485 viopath_targetinst(remoteLp),
486 (u64)(unsigned long)sem, VIOVERSION << 16,
487 0, 0, 0, 0);
488}
489
490static int __init hvc_vio_init(void)
491{
492 atomic_t wait_flag;
493 int rc;
494
495 if (!firmware_has_feature(FW_FEATURE_ISERIES))
496 return -EIO;
497
498 /* +2 for fudge */
499 rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
500 viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
501 if (rc)
502 printk(KERN_WARNING "hvc: error opening to primary %d\n", rc);
503
504 if (viopath_hostLp == HvLpIndexInvalid)
505 vio_set_hostlp();
506
507 /*
508 * And if the primary is not the same as the hosting LP, open to the
509 * hosting lp
510 */
511 if ((viopath_hostLp != HvLpIndexInvalid) &&
512 (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
513 printk(KERN_INFO "hvc: open path to hosting (%d)\n",
514 viopath_hostLp);
515 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
516 VIOCHAR_WINDOW + 2); /* +2 for fudge */
517 if (rc)
518 printk(KERN_WARNING
519 "error opening to partition %d: %d\n",
520 viopath_hostLp, rc);
521 }
522
523 if (vio_setHandler(viomajorsubtype_chario, hvc_handle_event) < 0)
524 printk(KERN_WARNING
525 "hvc: error seting handler for console events!\n");
526
527 /*
528 * First, try to open the console to the hosting lp.
529 * Wait on a semaphore for the response.
530 */
531 atomic_set(&wait_flag, 0);
532 if ((viopath_isactive(viopath_hostLp)) &&
533 (send_open(viopath_hostLp, &wait_flag) == 0)) {
534 printk(KERN_INFO "hvc: hosting partition %d\n", viopath_hostLp);
535 while (atomic_read(&wait_flag) == 0)
536 mb();
537 atomic_set(&wait_flag, 0);
538 }
539
540 /*
541 * If we don't have an active console, try the primary
542 */
543 if ((!viopath_isactive(port_info[0].lp)) &&
544 (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
545 (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag) == 0)) {
546 printk(KERN_INFO "hvc: opening console to primary partition\n");
547 while (atomic_read(&wait_flag) == 0)
548 mb();
549 }
550
551 /* Register as a vio device to receive callbacks */
552 rc = vio_register_driver(&hvc_vio_driver);
553
554 return rc;
555}
556module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
557
558static void __exit hvc_vio_exit(void)
559{
560 vio_unregister_driver(&hvc_vio_driver);
561}
562module_exit(hvc_vio_exit);
563
564/* the device tree order defines our numbering */
565static int __init hvc_find_vtys(void)
566{
567 struct device_node *vty;
568 int num_found = 0;
569
570 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
571 vty = of_find_node_by_name(vty, "vty")) {
572 const uint32_t *vtermno;
573
574 /* We have statically defined space for only a certain number
575 * of console adapters.
576 */
577 if ((num_found >= MAX_NR_HVC_CONSOLES) ||
578 (num_found >= VTTY_PORTS)) {
579 of_node_put(vty);
580 break;
581 }
582
583 vtermno = of_get_property(vty, "reg", NULL);
584 if (!vtermno)
585 continue;
586
587 if (!of_device_is_compatible(vty, "IBM,iSeries-vty"))
588 continue;
589
590 if (num_found == 0)
591 add_preferred_console("hvc", 0, NULL);
592 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
593 ++num_found;
594 }
595
596 return num_found;
597}
598console_initcall(hvc_find_vtys);
diff --git a/drivers/tty/hvc/hvc_iucv.c b/drivers/tty/hvc/hvc_iucv.c
new file mode 100644
index 000000000000..b6f7d52f7c35
--- /dev/null
+++ b/drivers/tty/hvc/hvc_iucv.c
@@ -0,0 +1,1337 @@
1/*
2 * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
3 *
4 * This HVC device driver provides terminal access using
5 * z/VM IUCV communication paths.
6 *
7 * Copyright IBM Corp. 2008, 2009
8 *
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 */
11#define KMSG_COMPONENT "hvc_iucv"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <asm/ebcdic.h>
17#include <linux/ctype.h>
18#include <linux/delay.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/mempool.h>
22#include <linux/moduleparam.h>
23#include <linux/tty.h>
24#include <linux/wait.h>
25#include <net/iucv/iucv.h>
26
27#include "hvc_console.h"
28
29
30/* General device driver settings */
31#define HVC_IUCV_MAGIC 0xc9e4c3e5
32#define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
33#define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
34
35/* IUCV TTY message */
36#define MSG_VERSION 0x02 /* Message version */
37#define MSG_TYPE_ERROR 0x01 /* Error message */
38#define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
39#define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
40#define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
41#define MSG_TYPE_DATA 0x10 /* Terminal data */
42
43struct iucv_tty_msg {
44 u8 version; /* Message version */
45 u8 type; /* Message type */
46#define MSG_MAX_DATALEN ((u16)(~0))
47 u16 datalen; /* Payload length */
48 u8 data[]; /* Payload buffer */
49} __attribute__((packed));
50#define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
51
52enum iucv_state_t {
53 IUCV_DISCONN = 0,
54 IUCV_CONNECTED = 1,
55 IUCV_SEVERED = 2,
56};
57
58enum tty_state_t {
59 TTY_CLOSED = 0,
60 TTY_OPENED = 1,
61};
62
63struct hvc_iucv_private {
64 struct hvc_struct *hvc; /* HVC struct reference */
65 u8 srv_name[8]; /* IUCV service name (ebcdic) */
66 unsigned char is_console; /* Linux console usage flag */
67 enum iucv_state_t iucv_state; /* IUCV connection status */
68 enum tty_state_t tty_state; /* TTY status */
69 struct iucv_path *path; /* IUCV path pointer */
70 spinlock_t lock; /* hvc_iucv_private lock */
71#define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
72 void *sndbuf; /* send buffer */
73 size_t sndbuf_len; /* length of send buffer */
74#define QUEUE_SNDBUF_DELAY (HZ / 25)
75 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
76 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
77 struct list_head tty_outqueue; /* outgoing IUCV messages */
78 struct list_head tty_inqueue; /* incoming IUCV messages */
79 struct device *dev; /* device structure */
80};
81
82struct iucv_tty_buffer {
83 struct list_head list; /* list pointer */
84 struct iucv_message msg; /* store an IUCV message */
85 size_t offset; /* data buffer offset */
86 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
87};
88
89/* IUCV callback handler */
90static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
91static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
92static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
93static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
94
95
96/* Kernel module parameter: use one terminal device as default */
97static unsigned long hvc_iucv_devices = 1;
98
99/* Array of allocated hvc iucv tty lines... */
100static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
101#define IUCV_HVC_CON_IDX (0)
102/* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
103#define MAX_VMID_FILTER (500)
104static size_t hvc_iucv_filter_size;
105static void *hvc_iucv_filter;
106static const char *hvc_iucv_filter_string;
107static DEFINE_RWLOCK(hvc_iucv_filter_lock);
108
109/* Kmem cache and mempool for iucv_tty_buffer elements */
110static struct kmem_cache *hvc_iucv_buffer_cache;
111static mempool_t *hvc_iucv_mempool;
112
113/* IUCV handler callback functions */
114static struct iucv_handler hvc_iucv_handler = {
115 .path_pending = hvc_iucv_path_pending,
116 .path_severed = hvc_iucv_path_severed,
117 .message_complete = hvc_iucv_msg_complete,
118 .message_pending = hvc_iucv_msg_pending,
119};
120
121
122/**
123 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
124 * @num: The HVC virtual terminal number (vtermno)
125 *
126 * This function returns the struct hvc_iucv_private instance that corresponds
127 * to the HVC virtual terminal number specified as parameter @num.
128 */
129struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
130{
131 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
132 return NULL;
133 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
134}
135
136/**
137 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
138 * @size: Size of the internal buffer used to store data.
139 * @flags: Memory allocation flags passed to mempool.
140 *
141 * This function allocates a new struct iucv_tty_buffer element and, optionally,
142 * allocates an internal data buffer with the specified size @size.
143 * The internal data buffer is always allocated with GFP_DMA which is
144 * required for receiving and sending data with IUCV.
145 * Note: The total message size arises from the internal buffer size and the
146 * members of the iucv_tty_msg structure.
147 * The function returns NULL if memory allocation has failed.
148 */
149static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
150{
151 struct iucv_tty_buffer *bufp;
152
153 bufp = mempool_alloc(hvc_iucv_mempool, flags);
154 if (!bufp)
155 return NULL;
156 memset(bufp, 0, sizeof(*bufp));
157
158 if (size > 0) {
159 bufp->msg.length = MSG_SIZE(size);
160 bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
161 if (!bufp->mbuf) {
162 mempool_free(bufp, hvc_iucv_mempool);
163 return NULL;
164 }
165 bufp->mbuf->version = MSG_VERSION;
166 bufp->mbuf->type = MSG_TYPE_DATA;
167 bufp->mbuf->datalen = (u16) size;
168 }
169 return bufp;
170}
171
172/**
173 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
174 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
175 */
176static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
177{
178 kfree(bufp->mbuf);
179 mempool_free(bufp, hvc_iucv_mempool);
180}
181
182/**
183 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
184 * @list: List containing struct iucv_tty_buffer elements.
185 */
186static void destroy_tty_buffer_list(struct list_head *list)
187{
188 struct iucv_tty_buffer *ent, *next;
189
190 list_for_each_entry_safe(ent, next, list, list) {
191 list_del(&ent->list);
192 destroy_tty_buffer(ent);
193 }
194}
195
196/**
197 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
198 * @priv: Pointer to struct hvc_iucv_private
199 * @buf: HVC buffer for writing received terminal data.
200 * @count: HVC buffer size.
201 * @has_more_data: Pointer to an int variable.
202 *
203 * The function picks up pending messages from the input queue and receives
204 * the message data that is then written to the specified buffer @buf.
205 * If the buffer size @count is less than the data message size, the
206 * message is kept on the input queue and @has_more_data is set to 1.
207 * If all message data has been written, the message is removed from
208 * the input queue.
209 *
210 * The function returns the number of bytes written to the terminal, zero if
211 * there are no pending data messages available or if there is no established
212 * IUCV path.
213 * If the IUCV path has been severed, then -EPIPE is returned to cause a
214 * hang up (that is issued by the HVC layer).
215 */
216static int hvc_iucv_write(struct hvc_iucv_private *priv,
217 char *buf, int count, int *has_more_data)
218{
219 struct iucv_tty_buffer *rb;
220 int written;
221 int rc;
222
223 /* immediately return if there is no IUCV connection */
224 if (priv->iucv_state == IUCV_DISCONN)
225 return 0;
226
227 /* if the IUCV path has been severed, return -EPIPE to inform the
228 * HVC layer to hang up the tty device. */
229 if (priv->iucv_state == IUCV_SEVERED)
230 return -EPIPE;
231
232 /* check if there are pending messages */
233 if (list_empty(&priv->tty_inqueue))
234 return 0;
235
236 /* receive an iucv message and flip data to the tty (ldisc) */
237 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
238
239 written = 0;
240 if (!rb->mbuf) { /* message not yet received ... */
241 /* allocate mem to store msg data; if no memory is available
242 * then leave the buffer on the list and re-try later */
243 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
244 if (!rb->mbuf)
245 return -ENOMEM;
246
247 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
248 rb->mbuf, rb->msg.length, NULL);
249 switch (rc) {
250 case 0: /* Successful */
251 break;
252 case 2: /* No message found */
253 case 9: /* Message purged */
254 break;
255 default:
256 written = -EIO;
257 }
258 /* remove buffer if an error has occurred or received data
259 * is not correct */
260 if (rc || (rb->mbuf->version != MSG_VERSION) ||
261 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
262 goto out_remove_buffer;
263 }
264
265 switch (rb->mbuf->type) {
266 case MSG_TYPE_DATA:
267 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
268 memcpy(buf, rb->mbuf->data + rb->offset, written);
269 if (written < (rb->mbuf->datalen - rb->offset)) {
270 rb->offset += written;
271 *has_more_data = 1;
272 goto out_written;
273 }
274 break;
275
276 case MSG_TYPE_WINSIZE:
277 if (rb->mbuf->datalen != sizeof(struct winsize))
278 break;
279 /* The caller must ensure that the hvc is locked, which
280 * is the case when called from hvc_iucv_get_chars() */
281 __hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
282 break;
283
284 case MSG_TYPE_ERROR: /* ignored ... */
285 case MSG_TYPE_TERMENV: /* ignored ... */
286 case MSG_TYPE_TERMIOS: /* ignored ... */
287 break;
288 }
289
290out_remove_buffer:
291 list_del(&rb->list);
292 destroy_tty_buffer(rb);
293 *has_more_data = !list_empty(&priv->tty_inqueue);
294
295out_written:
296 return written;
297}
298
299/**
300 * hvc_iucv_get_chars() - HVC get_chars operation.
301 * @vtermno: HVC virtual terminal number.
302 * @buf: Pointer to a buffer to store data
303 * @count: Size of buffer available for writing
304 *
305 * The HVC thread calls this method to read characters from the back-end.
306 * If an IUCV communication path has been established, pending IUCV messages
307 * are received and data is copied into buffer @buf up to @count bytes.
308 *
309 * Locking: The routine gets called under an irqsave() spinlock; and
310 * the routine locks the struct hvc_iucv_private->lock to call
311 * helper functions.
312 */
313static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
314{
315 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
316 int written;
317 int has_more_data;
318
319 if (count <= 0)
320 return 0;
321
322 if (!priv)
323 return -ENODEV;
324
325 spin_lock(&priv->lock);
326 has_more_data = 0;
327 written = hvc_iucv_write(priv, buf, count, &has_more_data);
328 spin_unlock(&priv->lock);
329
330 /* if there are still messages on the queue... schedule another run */
331 if (has_more_data)
332 hvc_kick();
333
334 return written;
335}
336
337/**
338 * hvc_iucv_queue() - Buffer terminal data for sending.
339 * @priv: Pointer to struct hvc_iucv_private instance.
340 * @buf: Buffer containing data to send.
341 * @count: Size of buffer and amount of data to send.
342 *
343 * The function queues data for sending. To actually send the buffered data,
344 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
345 * The function returns the number of data bytes that has been buffered.
346 *
347 * If the device is not connected, data is ignored and the function returns
348 * @count.
349 * If the buffer is full, the function returns 0.
350 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
351 * (that can be passed to HVC layer to cause a tty hangup).
352 */
353static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
354 int count)
355{
356 size_t len;
357
358 if (priv->iucv_state == IUCV_DISCONN)
359 return count; /* ignore data */
360
361 if (priv->iucv_state == IUCV_SEVERED)
362 return -EPIPE;
363
364 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
365 if (!len)
366 return 0;
367
368 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
369 priv->sndbuf_len += len;
370
371 if (priv->iucv_state == IUCV_CONNECTED)
372 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
373
374 return len;
375}
376
377/**
378 * hvc_iucv_send() - Send an IUCV message containing terminal data.
379 * @priv: Pointer to struct hvc_iucv_private instance.
380 *
381 * If an IUCV communication path has been established, the buffered output data
382 * is sent via an IUCV message and the number of bytes sent is returned.
383 * Returns 0 if there is no established IUCV communication path or
384 * -EPIPE if an existing IUCV communicaton path has been severed.
385 */
386static int hvc_iucv_send(struct hvc_iucv_private *priv)
387{
388 struct iucv_tty_buffer *sb;
389 int rc, len;
390
391 if (priv->iucv_state == IUCV_SEVERED)
392 return -EPIPE;
393
394 if (priv->iucv_state == IUCV_DISCONN)
395 return -EIO;
396
397 if (!priv->sndbuf_len)
398 return 0;
399
400 /* allocate internal buffer to store msg data and also compute total
401 * message length */
402 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
403 if (!sb)
404 return -ENOMEM;
405
406 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
407 sb->mbuf->datalen = (u16) priv->sndbuf_len;
408 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
409
410 list_add_tail(&sb->list, &priv->tty_outqueue);
411
412 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
413 (void *) sb->mbuf, sb->msg.length);
414 if (rc) {
415 /* drop the message here; however we might want to handle
416 * 0x03 (msg limit reached) by trying again... */
417 list_del(&sb->list);
418 destroy_tty_buffer(sb);
419 }
420 len = priv->sndbuf_len;
421 priv->sndbuf_len = 0;
422
423 return len;
424}
425
426/**
427 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
428 * @work: Work structure.
429 *
430 * This work queue function sends buffered output data over IUCV and,
431 * if not all buffered data could be sent, reschedules itself.
432 */
433static void hvc_iucv_sndbuf_work(struct work_struct *work)
434{
435 struct hvc_iucv_private *priv;
436
437 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
438 if (!priv)
439 return;
440
441 spin_lock_bh(&priv->lock);
442 hvc_iucv_send(priv);
443 spin_unlock_bh(&priv->lock);
444}
445
446/**
447 * hvc_iucv_put_chars() - HVC put_chars operation.
448 * @vtermno: HVC virtual terminal number.
449 * @buf: Pointer to an buffer to read data from
450 * @count: Size of buffer available for reading
451 *
452 * The HVC thread calls this method to write characters to the back-end.
453 * The function calls hvc_iucv_queue() to queue terminal data for sending.
454 *
455 * Locking: The method gets called under an irqsave() spinlock; and
456 * locks struct hvc_iucv_private->lock.
457 */
458static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
459{
460 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
461 int queued;
462
463 if (count <= 0)
464 return 0;
465
466 if (!priv)
467 return -ENODEV;
468
469 spin_lock(&priv->lock);
470 queued = hvc_iucv_queue(priv, buf, count);
471 spin_unlock(&priv->lock);
472
473 return queued;
474}
475
476/**
477 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
478 * @hp: Pointer to the HVC device (struct hvc_struct)
479 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
480 * hvc_iucv_private instance.
481 *
482 * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
483 * instance that is derived from @id. Always returns 0.
484 *
485 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
486 */
487static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
488{
489 struct hvc_iucv_private *priv;
490
491 priv = hvc_iucv_get_private(id);
492 if (!priv)
493 return 0;
494
495 spin_lock_bh(&priv->lock);
496 priv->tty_state = TTY_OPENED;
497 spin_unlock_bh(&priv->lock);
498
499 return 0;
500}
501
502/**
503 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
504 * @priv: Pointer to the struct hvc_iucv_private instance.
505 */
506static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
507{
508 destroy_tty_buffer_list(&priv->tty_outqueue);
509 destroy_tty_buffer_list(&priv->tty_inqueue);
510
511 priv->tty_state = TTY_CLOSED;
512 priv->iucv_state = IUCV_DISCONN;
513
514 priv->sndbuf_len = 0;
515}
516
517/**
518 * tty_outqueue_empty() - Test if the tty outq is empty
519 * @priv: Pointer to struct hvc_iucv_private instance.
520 */
521static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
522{
523 int rc;
524
525 spin_lock_bh(&priv->lock);
526 rc = list_empty(&priv->tty_outqueue);
527 spin_unlock_bh(&priv->lock);
528
529 return rc;
530}
531
532/**
533 * flush_sndbuf_sync() - Flush send buffer and wait for completion
534 * @priv: Pointer to struct hvc_iucv_private instance.
535 *
536 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
537 * to flush any buffered terminal output data and waits for completion.
538 */
539static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
540{
541 int sync_wait;
542
543 cancel_delayed_work_sync(&priv->sndbuf_work);
544
545 spin_lock_bh(&priv->lock);
546 hvc_iucv_send(priv); /* force sending buffered data */
547 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
548 spin_unlock_bh(&priv->lock);
549
550 if (sync_wait)
551 wait_event_timeout(priv->sndbuf_waitq,
552 tty_outqueue_empty(priv), HZ/10);
553}
554
555/**
556 * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
557 * @priv: Pointer to hvc_iucv_private structure
558 *
559 * This routine severs an existing IUCV communication path and hangs
560 * up the underlying HVC terminal device.
561 * The hang-up occurs only if an IUCV communication path is established;
562 * otherwise there is no need to hang up the terminal device.
563 *
564 * The IUCV HVC hang-up is separated into two steps:
565 * 1. After the IUCV path has been severed, the iucv_state is set to
566 * IUCV_SEVERED.
567 * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
568 * IUCV_SEVERED state causes the tty hang-up in the HVC layer.
569 *
570 * If the tty has not yet been opened, clean up the hvc_iucv_private
571 * structure to allow re-connects.
572 * If the tty has been opened, let get_chars() return -EPIPE to signal
573 * the HVC layer to hang up the tty and, if so, wake up the HVC thread
574 * to call get_chars()...
575 *
576 * Special notes on hanging up a HVC terminal instantiated as console:
577 * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
578 * 2. do_tty_hangup() calls tty->ops->close() for console_filp
579 * => no hangup notifier is called by HVC (default)
580 * 2. hvc_close() returns because of tty_hung_up_p(filp)
581 * => no delete notifier is called!
582 * Finally, the back-end is not being notified, thus, the tty session is
583 * kept active (TTY_OPEN) to be ready for re-connects.
584 *
585 * Locking: spin_lock(&priv->lock) w/o disabling bh
586 */
587static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
588{
589 struct iucv_path *path;
590
591 path = NULL;
592 spin_lock(&priv->lock);
593 if (priv->iucv_state == IUCV_CONNECTED) {
594 path = priv->path;
595 priv->path = NULL;
596 priv->iucv_state = IUCV_SEVERED;
597 if (priv->tty_state == TTY_CLOSED)
598 hvc_iucv_cleanup(priv);
599 else
600 /* console is special (see above) */
601 if (priv->is_console) {
602 hvc_iucv_cleanup(priv);
603 priv->tty_state = TTY_OPENED;
604 } else
605 hvc_kick();
606 }
607 spin_unlock(&priv->lock);
608
609 /* finally sever path (outside of priv->lock due to lock ordering) */
610 if (path) {
611 iucv_path_sever(path, NULL);
612 iucv_path_free(path);
613 }
614}
615
616/**
617 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
618 * @hp: Pointer to the HVC device (struct hvc_struct)
619 * @id: Additional data (originally passed to hvc_alloc):
620 * the index of an struct hvc_iucv_private instance.
621 *
622 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
623 * virtual or otherwise) has occurred.
624 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
625 * to keep an existing IUCV communication path established.
626 * (Background: vhangup() is called from user space (by getty or login) to
627 * disable writing to the tty by other applications).
628 * If the tty has been opened and an established IUCV path has been severed
629 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
630 *
631 * Locking: struct hvc_iucv_private->lock
632 */
633static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
634{
635 struct hvc_iucv_private *priv;
636
637 priv = hvc_iucv_get_private(id);
638 if (!priv)
639 return;
640
641 flush_sndbuf_sync(priv);
642
643 spin_lock_bh(&priv->lock);
644 /* NOTE: If the hangup was scheduled by ourself (from the iucv
645 * path_servered callback [IUCV_SEVERED]), we have to clean up
646 * our structure and to set state to TTY_CLOSED.
647 * If the tty was hung up otherwise (e.g. vhangup()), then we
648 * ignore this hangup and keep an established IUCV path open...
649 * (...the reason is that we are not able to connect back to the
650 * client if we disconnect on hang up) */
651 priv->tty_state = TTY_CLOSED;
652
653 if (priv->iucv_state == IUCV_SEVERED)
654 hvc_iucv_cleanup(priv);
655 spin_unlock_bh(&priv->lock);
656}
657
658/**
659 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
660 * @hp: Pointer to the HVC device (struct hvc_struct)
661 * @id: Additional data (originally passed to hvc_alloc):
662 * the index of an struct hvc_iucv_private instance.
663 *
664 * This routine notifies the HVC back-end that the last tty device fd has been
665 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
666 * hvc_iucv_private instance.
667 *
668 * Locking: struct hvc_iucv_private->lock
669 */
670static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
671{
672 struct hvc_iucv_private *priv;
673 struct iucv_path *path;
674
675 priv = hvc_iucv_get_private(id);
676 if (!priv)
677 return;
678
679 flush_sndbuf_sync(priv);
680
681 spin_lock_bh(&priv->lock);
682 path = priv->path; /* save reference to IUCV path */
683 priv->path = NULL;
684 hvc_iucv_cleanup(priv);
685 spin_unlock_bh(&priv->lock);
686
687 /* sever IUCV path outside of priv->lock due to lock ordering of:
688 * priv->lock <--> iucv_table_lock */
689 if (path) {
690 iucv_path_sever(path, NULL);
691 iucv_path_free(path);
692 }
693}
694
695/**
696 * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
697 * @ipvmid: Originating z/VM user ID (right padded with blanks)
698 *
699 * Returns 0 if the z/VM user ID @ipvmid is allowed to connection, otherwise
700 * non-zero.
701 */
702static int hvc_iucv_filter_connreq(u8 ipvmid[8])
703{
704 size_t i;
705
706 /* Note: default policy is ACCEPT if no filter is set */
707 if (!hvc_iucv_filter_size)
708 return 0;
709
710 for (i = 0; i < hvc_iucv_filter_size; i++)
711 if (0 == memcmp(ipvmid, hvc_iucv_filter + (8 * i), 8))
712 return 0;
713 return 1;
714}
715
716/**
717 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
718 * @path: Pending path (struct iucv_path)
719 * @ipvmid: z/VM system identifier of originator
720 * @ipuser: User specified data for this path
721 * (AF_IUCV: port/service name and originator port)
722 *
723 * The function uses the @ipuser data to determine if the pending path belongs
724 * to a terminal managed by this device driver.
725 * If the path belongs to this driver, ensure that the terminal is not accessed
726 * multiple times (only one connection to a terminal is allowed).
727 * If the terminal is not yet connected, the pending path is accepted and is
728 * associated to the appropriate struct hvc_iucv_private instance.
729 *
730 * Returns 0 if @path belongs to a terminal managed by the this device driver;
731 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
732 *
733 * Locking: struct hvc_iucv_private->lock
734 */
735static int hvc_iucv_path_pending(struct iucv_path *path,
736 u8 ipvmid[8], u8 ipuser[16])
737{
738 struct hvc_iucv_private *priv;
739 u8 nuser_data[16];
740 u8 vm_user_id[9];
741 int i, rc;
742
743 priv = NULL;
744 for (i = 0; i < hvc_iucv_devices; i++)
745 if (hvc_iucv_table[i] &&
746 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
747 priv = hvc_iucv_table[i];
748 break;
749 }
750 if (!priv)
751 return -ENODEV;
752
753 /* Enforce that ipvmid is allowed to connect to us */
754 read_lock(&hvc_iucv_filter_lock);
755 rc = hvc_iucv_filter_connreq(ipvmid);
756 read_unlock(&hvc_iucv_filter_lock);
757 if (rc) {
758 iucv_path_sever(path, ipuser);
759 iucv_path_free(path);
760 memcpy(vm_user_id, ipvmid, 8);
761 vm_user_id[8] = 0;
762 pr_info("A connection request from z/VM user ID %s "
763 "was refused\n", vm_user_id);
764 return 0;
765 }
766
767 spin_lock(&priv->lock);
768
769 /* If the terminal is already connected or being severed, then sever
770 * this path to enforce that there is only ONE established communication
771 * path per terminal. */
772 if (priv->iucv_state != IUCV_DISCONN) {
773 iucv_path_sever(path, ipuser);
774 iucv_path_free(path);
775 goto out_path_handled;
776 }
777
778 /* accept path */
779 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
780 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
781 path->msglim = 0xffff; /* IUCV MSGLIMIT */
782 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
783 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
784 if (rc) {
785 iucv_path_sever(path, ipuser);
786 iucv_path_free(path);
787 goto out_path_handled;
788 }
789 priv->path = path;
790 priv->iucv_state = IUCV_CONNECTED;
791
792 /* flush buffered output data... */
793 schedule_delayed_work(&priv->sndbuf_work, 5);
794
795out_path_handled:
796 spin_unlock(&priv->lock);
797 return 0;
798}
799
800/**
801 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
802 * @path: Pending path (struct iucv_path)
803 * @ipuser: User specified data for this path
804 * (AF_IUCV: port/service name and originator port)
805 *
806 * This function calls the hvc_iucv_hangup() function for the
807 * respective IUCV HVC terminal.
808 *
809 * Locking: struct hvc_iucv_private->lock
810 */
811static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
812{
813 struct hvc_iucv_private *priv = path->private;
814
815 hvc_iucv_hangup(priv);
816}
817
818/**
819 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
820 * @path: Pending path (struct iucv_path)
821 * @msg: Pointer to the IUCV message
822 *
823 * The function puts an incoming message on the input queue for later
824 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
825 * If the tty has not yet been opened, the message is rejected.
826 *
827 * Locking: struct hvc_iucv_private->lock
828 */
829static void hvc_iucv_msg_pending(struct iucv_path *path,
830 struct iucv_message *msg)
831{
832 struct hvc_iucv_private *priv = path->private;
833 struct iucv_tty_buffer *rb;
834
835 /* reject messages that exceed max size of iucv_tty_msg->datalen */
836 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
837 iucv_message_reject(path, msg);
838 return;
839 }
840
841 spin_lock(&priv->lock);
842
843 /* reject messages if tty has not yet been opened */
844 if (priv->tty_state == TTY_CLOSED) {
845 iucv_message_reject(path, msg);
846 goto unlock_return;
847 }
848
849 /* allocate tty buffer to save iucv msg only */
850 rb = alloc_tty_buffer(0, GFP_ATOMIC);
851 if (!rb) {
852 iucv_message_reject(path, msg);
853 goto unlock_return; /* -ENOMEM */
854 }
855 rb->msg = *msg;
856
857 list_add_tail(&rb->list, &priv->tty_inqueue);
858
859 hvc_kick(); /* wake up hvc thread */
860
861unlock_return:
862 spin_unlock(&priv->lock);
863}
864
865/**
866 * hvc_iucv_msg_complete() - IUCV handler to process message completion
867 * @path: Pending path (struct iucv_path)
868 * @msg: Pointer to the IUCV message
869 *
870 * The function is called upon completion of message delivery to remove the
871 * message from the outqueue. Additional delivery information can be found
872 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
873 * purged messages (0x010000 (IPADPGNR)).
874 *
875 * Locking: struct hvc_iucv_private->lock
876 */
877static void hvc_iucv_msg_complete(struct iucv_path *path,
878 struct iucv_message *msg)
879{
880 struct hvc_iucv_private *priv = path->private;
881 struct iucv_tty_buffer *ent, *next;
882 LIST_HEAD(list_remove);
883
884 spin_lock(&priv->lock);
885 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
886 if (ent->msg.id == msg->id) {
887 list_move(&ent->list, &list_remove);
888 break;
889 }
890 wake_up(&priv->sndbuf_waitq);
891 spin_unlock(&priv->lock);
892 destroy_tty_buffer_list(&list_remove);
893}
894
895/**
896 * hvc_iucv_pm_freeze() - Freeze PM callback
897 * @dev: IUVC HVC terminal device
898 *
899 * Sever an established IUCV communication path and
900 * trigger a hang-up of the underlying HVC terminal.
901 */
902static int hvc_iucv_pm_freeze(struct device *dev)
903{
904 struct hvc_iucv_private *priv = dev_get_drvdata(dev);
905
906 local_bh_disable();
907 hvc_iucv_hangup(priv);
908 local_bh_enable();
909
910 return 0;
911}
912
913/**
914 * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
915 * @dev: IUVC HVC terminal device
916 *
917 * Wake up the HVC thread to trigger hang-up and respective
918 * HVC back-end notifier invocations.
919 */
920static int hvc_iucv_pm_restore_thaw(struct device *dev)
921{
922 hvc_kick();
923 return 0;
924}
925
926
927/* HVC operations */
928static const struct hv_ops hvc_iucv_ops = {
929 .get_chars = hvc_iucv_get_chars,
930 .put_chars = hvc_iucv_put_chars,
931 .notifier_add = hvc_iucv_notifier_add,
932 .notifier_del = hvc_iucv_notifier_del,
933 .notifier_hangup = hvc_iucv_notifier_hangup,
934};
935
936/* Suspend / resume device operations */
937static const struct dev_pm_ops hvc_iucv_pm_ops = {
938 .freeze = hvc_iucv_pm_freeze,
939 .thaw = hvc_iucv_pm_restore_thaw,
940 .restore = hvc_iucv_pm_restore_thaw,
941};
942
943/* IUCV HVC device driver */
944static struct device_driver hvc_iucv_driver = {
945 .name = KMSG_COMPONENT,
946 .bus = &iucv_bus,
947 .pm = &hvc_iucv_pm_ops,
948};
949
950/**
951 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
952 * @id: hvc_iucv_table index
953 * @is_console: Flag if the instance is used as Linux console
954 *
955 * This function allocates a new hvc_iucv_private structure and stores
956 * the instance in hvc_iucv_table at index @id.
957 * Returns 0 on success; otherwise non-zero.
958 */
959static int __init hvc_iucv_alloc(int id, unsigned int is_console)
960{
961 struct hvc_iucv_private *priv;
962 char name[9];
963 int rc;
964
965 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
966 if (!priv)
967 return -ENOMEM;
968
969 spin_lock_init(&priv->lock);
970 INIT_LIST_HEAD(&priv->tty_outqueue);
971 INIT_LIST_HEAD(&priv->tty_inqueue);
972 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
973 init_waitqueue_head(&priv->sndbuf_waitq);
974
975 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
976 if (!priv->sndbuf) {
977 kfree(priv);
978 return -ENOMEM;
979 }
980
981 /* set console flag */
982 priv->is_console = is_console;
983
984 /* allocate hvc device */
985 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
986 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
987 if (IS_ERR(priv->hvc)) {
988 rc = PTR_ERR(priv->hvc);
989 goto out_error_hvc;
990 }
991
992 /* notify HVC thread instead of using polling */
993 priv->hvc->irq_requested = 1;
994
995 /* setup iucv related information */
996 snprintf(name, 9, "lnxhvc%-2d", id);
997 memcpy(priv->srv_name, name, 8);
998 ASCEBC(priv->srv_name, 8);
999
1000 /* create and setup device */
1001 priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
1002 if (!priv->dev) {
1003 rc = -ENOMEM;
1004 goto out_error_dev;
1005 }
1006 dev_set_name(priv->dev, "hvc_iucv%d", id);
1007 dev_set_drvdata(priv->dev, priv);
1008 priv->dev->bus = &iucv_bus;
1009 priv->dev->parent = iucv_root;
1010 priv->dev->driver = &hvc_iucv_driver;
1011 priv->dev->release = (void (*)(struct device *)) kfree;
1012 rc = device_register(priv->dev);
1013 if (rc) {
1014 put_device(priv->dev);
1015 goto out_error_dev;
1016 }
1017
1018 hvc_iucv_table[id] = priv;
1019 return 0;
1020
1021out_error_dev:
1022 hvc_remove(priv->hvc);
1023out_error_hvc:
1024 free_page((unsigned long) priv->sndbuf);
1025 kfree(priv);
1026
1027 return rc;
1028}
1029
1030/**
1031 * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1032 */
1033static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1034{
1035 hvc_remove(priv->hvc);
1036 device_unregister(priv->dev);
1037 free_page((unsigned long) priv->sndbuf);
1038 kfree(priv);
1039}
1040
1041/**
1042 * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
1043 * @filter: String containing a comma-separated list of z/VM user IDs
1044 */
1045static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1046{
1047 const char *nextdelim, *residual;
1048 size_t len;
1049
1050 nextdelim = strchr(filter, ',');
1051 if (nextdelim) {
1052 len = nextdelim - filter;
1053 residual = nextdelim + 1;
1054 } else {
1055 len = strlen(filter);
1056 residual = filter + len;
1057 }
1058
1059 if (len == 0)
1060 return ERR_PTR(-EINVAL);
1061
1062 /* check for '\n' (if called from sysfs) */
1063 if (filter[len - 1] == '\n')
1064 len--;
1065
1066 if (len > 8)
1067 return ERR_PTR(-EINVAL);
1068
1069 /* pad with blanks and save upper case version of user ID */
1070 memset(dest, ' ', 8);
1071 while (len--)
1072 dest[len] = toupper(filter[len]);
1073 return residual;
1074}
1075
1076/**
1077 * hvc_iucv_setup_filter() - Set up z/VM user ID filter
1078 * @filter: String consisting of a comma-separated list of z/VM user IDs
1079 *
1080 * The function parses the @filter string and creates an array containing
1081 * the list of z/VM user ID filter entries.
1082 * Return code 0 means success, -EINVAL if the filter is syntactically
1083 * incorrect, -ENOMEM if there was not enough memory to allocate the
1084 * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
1085 */
1086static int hvc_iucv_setup_filter(const char *val)
1087{
1088 const char *residual;
1089 int err;
1090 size_t size, count;
1091 void *array, *old_filter;
1092
1093 count = strlen(val);
1094 if (count == 0 || (count == 1 && val[0] == '\n')) {
1095 size = 0;
1096 array = NULL;
1097 goto out_replace_filter; /* clear filter */
1098 }
1099
1100 /* count user IDs in order to allocate sufficient memory */
1101 size = 1;
1102 residual = val;
1103 while ((residual = strchr(residual, ',')) != NULL) {
1104 residual++;
1105 size++;
1106 }
1107
1108 /* check if the specified list exceeds the filter limit */
1109 if (size > MAX_VMID_FILTER)
1110 return -ENOSPC;
1111
1112 array = kzalloc(size * 8, GFP_KERNEL);
1113 if (!array)
1114 return -ENOMEM;
1115
1116 count = size;
1117 residual = val;
1118 while (*residual && count) {
1119 residual = hvc_iucv_parse_filter(residual,
1120 array + ((size - count) * 8));
1121 if (IS_ERR(residual)) {
1122 err = PTR_ERR(residual);
1123 kfree(array);
1124 goto out_err;
1125 }
1126 count--;
1127 }
1128
1129out_replace_filter:
1130 write_lock_bh(&hvc_iucv_filter_lock);
1131 old_filter = hvc_iucv_filter;
1132 hvc_iucv_filter_size = size;
1133 hvc_iucv_filter = array;
1134 write_unlock_bh(&hvc_iucv_filter_lock);
1135 kfree(old_filter);
1136
1137 err = 0;
1138out_err:
1139 return err;
1140}
1141
1142/**
1143 * param_set_vmidfilter() - Set z/VM user ID filter parameter
1144 * @val: String consisting of a comma-separated list of z/VM user IDs
1145 * @kp: Kernel parameter pointing to hvc_iucv_filter array
1146 *
1147 * The function sets up the z/VM user ID filter specified as comma-separated
1148 * list of user IDs in @val.
1149 * Note: If it is called early in the boot process, @val is stored and
1150 * parsed later in hvc_iucv_init().
1151 */
1152static int param_set_vmidfilter(const char *val, const struct kernel_param *kp)
1153{
1154 int rc;
1155
1156 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1157 return -ENODEV;
1158
1159 if (!val)
1160 return -EINVAL;
1161
1162 rc = 0;
1163 if (slab_is_available())
1164 rc = hvc_iucv_setup_filter(val);
1165 else
1166 hvc_iucv_filter_string = val; /* defer... */
1167 return rc;
1168}
1169
1170/**
1171 * param_get_vmidfilter() - Get z/VM user ID filter
1172 * @buffer: Buffer to store z/VM user ID filter,
1173 * (buffer size assumption PAGE_SIZE)
1174 * @kp: Kernel parameter pointing to the hvc_iucv_filter array
1175 *
1176 * The function stores the filter as a comma-separated list of z/VM user IDs
1177 * in @buffer. Typically, sysfs routines call this function for attr show.
1178 */
1179static int param_get_vmidfilter(char *buffer, const struct kernel_param *kp)
1180{
1181 int rc;
1182 size_t index, len;
1183 void *start, *end;
1184
1185 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1186 return -ENODEV;
1187
1188 rc = 0;
1189 read_lock_bh(&hvc_iucv_filter_lock);
1190 for (index = 0; index < hvc_iucv_filter_size; index++) {
1191 start = hvc_iucv_filter + (8 * index);
1192 end = memchr(start, ' ', 8);
1193 len = (end) ? end - start : 8;
1194 memcpy(buffer + rc, start, len);
1195 rc += len;
1196 buffer[rc++] = ',';
1197 }
1198 read_unlock_bh(&hvc_iucv_filter_lock);
1199 if (rc)
1200 buffer[--rc] = '\0'; /* replace last comma and update rc */
1201 return rc;
1202}
1203
1204#define param_check_vmidfilter(name, p) __param_check(name, p, void)
1205
1206static struct kernel_param_ops param_ops_vmidfilter = {
1207 .set = param_set_vmidfilter,
1208 .get = param_get_vmidfilter,
1209};
1210
1211/**
1212 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
1213 */
1214static int __init hvc_iucv_init(void)
1215{
1216 int rc;
1217 unsigned int i;
1218
1219 if (!hvc_iucv_devices)
1220 return -ENODEV;
1221
1222 if (!MACHINE_IS_VM) {
1223 pr_notice("The z/VM IUCV HVC device driver cannot "
1224 "be used without z/VM\n");
1225 rc = -ENODEV;
1226 goto out_error;
1227 }
1228
1229 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
1230 pr_err("%lu is not a valid value for the hvc_iucv= "
1231 "kernel parameter\n", hvc_iucv_devices);
1232 rc = -EINVAL;
1233 goto out_error;
1234 }
1235
1236 /* register IUCV HVC device driver */
1237 rc = driver_register(&hvc_iucv_driver);
1238 if (rc)
1239 goto out_error;
1240
1241 /* parse hvc_iucv_allow string and create z/VM user ID filter list */
1242 if (hvc_iucv_filter_string) {
1243 rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
1244 switch (rc) {
1245 case 0:
1246 break;
1247 case -ENOMEM:
1248 pr_err("Allocating memory failed with "
1249 "reason code=%d\n", 3);
1250 goto out_error;
1251 case -EINVAL:
1252 pr_err("hvc_iucv_allow= does not specify a valid "
1253 "z/VM user ID list\n");
1254 goto out_error;
1255 case -ENOSPC:
1256 pr_err("hvc_iucv_allow= specifies too many "
1257 "z/VM user IDs\n");
1258 goto out_error;
1259 default:
1260 goto out_error;
1261 }
1262 }
1263
1264 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
1265 sizeof(struct iucv_tty_buffer),
1266 0, 0, NULL);
1267 if (!hvc_iucv_buffer_cache) {
1268 pr_err("Allocating memory failed with reason code=%d\n", 1);
1269 rc = -ENOMEM;
1270 goto out_error;
1271 }
1272
1273 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1274 hvc_iucv_buffer_cache);
1275 if (!hvc_iucv_mempool) {
1276 pr_err("Allocating memory failed with reason code=%d\n", 2);
1277 kmem_cache_destroy(hvc_iucv_buffer_cache);
1278 rc = -ENOMEM;
1279 goto out_error;
1280 }
1281
1282 /* register the first terminal device as console
1283 * (must be done before allocating hvc terminal devices) */
1284 rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
1285 if (rc) {
1286 pr_err("Registering HVC terminal device as "
1287 "Linux console failed\n");
1288 goto out_error_memory;
1289 }
1290
1291 /* allocate hvc_iucv_private structs */
1292 for (i = 0; i < hvc_iucv_devices; i++) {
1293 rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
1294 if (rc) {
1295 pr_err("Creating a new HVC terminal device "
1296 "failed with error code=%d\n", rc);
1297 goto out_error_hvc;
1298 }
1299 }
1300
1301 /* register IUCV callback handler */
1302 rc = iucv_register(&hvc_iucv_handler, 0);
1303 if (rc) {
1304 pr_err("Registering IUCV handlers failed with error code=%d\n",
1305 rc);
1306 goto out_error_hvc;
1307 }
1308
1309 return 0;
1310
1311out_error_hvc:
1312 for (i = 0; i < hvc_iucv_devices; i++)
1313 if (hvc_iucv_table[i])
1314 hvc_iucv_destroy(hvc_iucv_table[i]);
1315out_error_memory:
1316 mempool_destroy(hvc_iucv_mempool);
1317 kmem_cache_destroy(hvc_iucv_buffer_cache);
1318out_error:
1319 if (hvc_iucv_filter)
1320 kfree(hvc_iucv_filter);
1321 hvc_iucv_devices = 0; /* ensure that we do not provide any device */
1322 return rc;
1323}
1324
1325/**
1326 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
1327 * @val: Parameter value (numeric)
1328 */
1329static int __init hvc_iucv_config(char *val)
1330{
1331 return strict_strtoul(val, 10, &hvc_iucv_devices);
1332}
1333
1334
1335device_initcall(hvc_iucv_init);
1336__setup("hvc_iucv=", hvc_iucv_config);
1337core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);
diff --git a/drivers/tty/hvc/hvc_rtas.c b/drivers/tty/hvc/hvc_rtas.c
new file mode 100644
index 000000000000..61c4a61558d9
--- /dev/null
+++ b/drivers/tty/hvc/hvc_rtas.c
@@ -0,0 +1,133 @@
1/*
2 * IBM RTAS driver interface to hvc_console.c
3 *
4 * (C) Copyright IBM Corporation 2001-2005
5 * (C) Copyright Red Hat, Inc. 2005
6 *
7 * Author(s): Maximino Augilar <IBM STI Design Center>
8 * : Ryan S. Arnold <rsa@us.ibm.com>
9 * : Utz Bacher <utz.bacher@de.ibm.com>
10 * : David Woodhouse <dwmw2@infradead.org>
11 *
12 * inspired by drivers/char/hvc_console.c
13 * written by Anton Blanchard and Paul Mackerras
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
30#include <linux/console.h>
31#include <linux/delay.h>
32#include <linux/err.h>
33#include <linux/init.h>
34#include <linux/moduleparam.h>
35#include <linux/types.h>
36
37#include <asm/irq.h>
38#include <asm/rtas.h>
39#include "hvc_console.h"
40
41#define hvc_rtas_cookie 0x67781e15
42struct hvc_struct *hvc_rtas_dev;
43
44static int rtascons_put_char_token = RTAS_UNKNOWN_SERVICE;
45static int rtascons_get_char_token = RTAS_UNKNOWN_SERVICE;
46
47static inline int hvc_rtas_write_console(uint32_t vtermno, const char *buf,
48 int count)
49{
50 int i;
51
52 for (i = 0; i < count; i++) {
53 if (rtas_call(rtascons_put_char_token, 1, 1, NULL, buf[i]))
54 break;
55 }
56
57 return i;
58}
59
60static int hvc_rtas_read_console(uint32_t vtermno, char *buf, int count)
61{
62 int i, c;
63
64 for (i = 0; i < count; i++) {
65 if (rtas_call(rtascons_get_char_token, 0, 2, &c))
66 break;
67
68 buf[i] = c;
69 }
70
71 return i;
72}
73
74static const struct hv_ops hvc_rtas_get_put_ops = {
75 .get_chars = hvc_rtas_read_console,
76 .put_chars = hvc_rtas_write_console,
77};
78
79static int __init hvc_rtas_init(void)
80{
81 struct hvc_struct *hp;
82
83 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
84 rtascons_put_char_token = rtas_token("put-term-char");
85 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
86 return -EIO;
87
88 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
89 rtascons_get_char_token = rtas_token("get-term-char");
90 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
91 return -EIO;
92
93 BUG_ON(hvc_rtas_dev);
94
95 /* Allocate an hvc_struct for the console device we instantiated
96 * earlier. Save off hp so that we can return it on exit */
97 hp = hvc_alloc(hvc_rtas_cookie, NO_IRQ, &hvc_rtas_get_put_ops, 16);
98 if (IS_ERR(hp))
99 return PTR_ERR(hp);
100
101 hvc_rtas_dev = hp;
102
103 return 0;
104}
105module_init(hvc_rtas_init);
106
107/* This will tear down the tty portion of the driver */
108static void __exit hvc_rtas_exit(void)
109{
110 /* Really the fun isn't over until the worker thread breaks down and
111 * the tty cleans up */
112 if (hvc_rtas_dev)
113 hvc_remove(hvc_rtas_dev);
114}
115module_exit(hvc_rtas_exit);
116
117/* This will happen prior to module init. There is no tty at this time? */
118static int __init hvc_rtas_console_init(void)
119{
120 rtascons_put_char_token = rtas_token("put-term-char");
121 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
122 return -EIO;
123
124 rtascons_get_char_token = rtas_token("get-term-char");
125 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
126 return -EIO;
127
128 hvc_instantiate(hvc_rtas_cookie, 0, &hvc_rtas_get_put_ops);
129 add_preferred_console("hvc", 0, NULL);
130
131 return 0;
132}
133console_initcall(hvc_rtas_console_init);
diff --git a/drivers/tty/hvc/hvc_tile.c b/drivers/tty/hvc/hvc_tile.c
new file mode 100644
index 000000000000..7a84a0595477
--- /dev/null
+++ b/drivers/tty/hvc/hvc_tile.c
@@ -0,0 +1,68 @@
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * Tilera TILE Processor hypervisor console
15 */
16
17#include <linux/console.h>
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/moduleparam.h>
22#include <linux/types.h>
23
24#include <hv/hypervisor.h>
25
26#include "hvc_console.h"
27
28static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
29{
30 return hv_console_write((HV_VirtAddr)buf, count);
31}
32
33static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
34{
35 int i, c;
36
37 for (i = 0; i < count; ++i) {
38 c = hv_console_read_if_ready();
39 if (c < 0)
40 break;
41 buf[i] = c;
42 }
43
44 return i;
45}
46
47static const struct hv_ops hvc_tile_get_put_ops = {
48 .get_chars = hvc_tile_get_chars,
49 .put_chars = hvc_tile_put_chars,
50};
51
52static int __init hvc_tile_console_init(void)
53{
54 extern void disable_early_printk(void);
55 hvc_instantiate(0, 0, &hvc_tile_get_put_ops);
56 add_preferred_console("hvc", 0, NULL);
57 disable_early_printk();
58 return 0;
59}
60console_initcall(hvc_tile_console_init);
61
62static int __init hvc_tile_init(void)
63{
64 struct hvc_struct *s;
65 s = hvc_alloc(0, 0, &hvc_tile_get_put_ops, 128);
66 return IS_ERR(s) ? PTR_ERR(s) : 0;
67}
68device_initcall(hvc_tile_init);
diff --git a/drivers/tty/hvc/hvc_udbg.c b/drivers/tty/hvc/hvc_udbg.c
new file mode 100644
index 000000000000..b0957e61a7be
--- /dev/null
+++ b/drivers/tty/hvc/hvc_udbg.c
@@ -0,0 +1,96 @@
1/*
2 * udbg interface to hvc_console.c
3 *
4 * (C) Copyright David Gibson, IBM Corporation 2008.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/moduleparam.h>
26#include <linux/types.h>
27#include <linux/irq.h>
28
29#include <asm/udbg.h>
30
31#include "hvc_console.h"
32
33struct hvc_struct *hvc_udbg_dev;
34
35static int hvc_udbg_put(uint32_t vtermno, const char *buf, int count)
36{
37 int i;
38
39 for (i = 0; i < count; i++)
40 udbg_putc(buf[i]);
41
42 return i;
43}
44
45static int hvc_udbg_get(uint32_t vtermno, char *buf, int count)
46{
47 int i, c;
48
49 if (!udbg_getc_poll)
50 return 0;
51
52 for (i = 0; i < count; i++) {
53 if ((c = udbg_getc_poll()) == -1)
54 break;
55 buf[i] = c;
56 }
57
58 return i;
59}
60
61static const struct hv_ops hvc_udbg_ops = {
62 .get_chars = hvc_udbg_get,
63 .put_chars = hvc_udbg_put,
64};
65
66static int __init hvc_udbg_init(void)
67{
68 struct hvc_struct *hp;
69
70 BUG_ON(hvc_udbg_dev);
71
72 hp = hvc_alloc(0, NO_IRQ, &hvc_udbg_ops, 16);
73 if (IS_ERR(hp))
74 return PTR_ERR(hp);
75
76 hvc_udbg_dev = hp;
77
78 return 0;
79}
80module_init(hvc_udbg_init);
81
82static void __exit hvc_udbg_exit(void)
83{
84 if (hvc_udbg_dev)
85 hvc_remove(hvc_udbg_dev);
86}
87module_exit(hvc_udbg_exit);
88
89static int __init hvc_udbg_console_init(void)
90{
91 hvc_instantiate(0, 0, &hvc_udbg_ops);
92 add_preferred_console("hvc", 0, NULL);
93
94 return 0;
95}
96console_initcall(hvc_udbg_console_init);
diff --git a/drivers/tty/hvc/hvc_vio.c b/drivers/tty/hvc/hvc_vio.c
new file mode 100644
index 000000000000..e6eea1485244
--- /dev/null
+++ b/drivers/tty/hvc/hvc_vio.c
@@ -0,0 +1,173 @@
1/*
2 * vio driver interface to hvc_console.c
3 *
4 * This code was moved here to allow the remaining code to be reused as a
5 * generic polling mode with semi-reliable transport driver core to the
6 * console and tty subsystems.
7 *
8 *
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
11 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
12 * Copyright (C) 2004 IBM Corporation
13 *
14 * Additional Author(s):
15 * Ryan S. Arnold <rsa@us.ibm.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32#include <linux/types.h>
33#include <linux/init.h>
34
35#include <asm/hvconsole.h>
36#include <asm/vio.h>
37#include <asm/prom.h>
38#include <asm/firmware.h>
39
40#include "hvc_console.h"
41
42static const char hvc_driver_name[] = "hvc_console";
43
44static struct vio_device_id hvc_driver_table[] __devinitdata = {
45 {"serial", "hvterm1"},
46 { "", "" }
47};
48MODULE_DEVICE_TABLE(vio, hvc_driver_table);
49
50static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
51{
52 unsigned long got;
53 int i;
54
55 /*
56 * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
57 * so we play safe and avoid the situation where got > count which could
58 * overload the flip buffer.
59 */
60 if (count < SIZE_VIO_GET_CHARS)
61 return -EAGAIN;
62
63 got = hvc_get_chars(vtermno, buf, count);
64
65 /*
66 * Work around a HV bug where it gives us a null
67 * after every \r. -- paulus
68 */
69 for (i = 1; i < got; ++i) {
70 if (buf[i] == 0 && buf[i-1] == '\r') {
71 --got;
72 if (i < got)
73 memmove(&buf[i], &buf[i+1],
74 got - i);
75 }
76 }
77 return got;
78}
79
80static const struct hv_ops hvc_get_put_ops = {
81 .get_chars = filtered_get_chars,
82 .put_chars = hvc_put_chars,
83 .notifier_add = notifier_add_irq,
84 .notifier_del = notifier_del_irq,
85 .notifier_hangup = notifier_hangup_irq,
86};
87
88static int __devinit hvc_vio_probe(struct vio_dev *vdev,
89 const struct vio_device_id *id)
90{
91 struct hvc_struct *hp;
92
93 /* probed with invalid parameters. */
94 if (!vdev || !id)
95 return -EPERM;
96
97 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
98 MAX_VIO_PUT_CHARS);
99 if (IS_ERR(hp))
100 return PTR_ERR(hp);
101 dev_set_drvdata(&vdev->dev, hp);
102
103 return 0;
104}
105
106static int __devexit hvc_vio_remove(struct vio_dev *vdev)
107{
108 struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
109
110 return hvc_remove(hp);
111}
112
113static struct vio_driver hvc_vio_driver = {
114 .id_table = hvc_driver_table,
115 .probe = hvc_vio_probe,
116 .remove = __devexit_p(hvc_vio_remove),
117 .driver = {
118 .name = hvc_driver_name,
119 .owner = THIS_MODULE,
120 }
121};
122
123static int __init hvc_vio_init(void)
124{
125 int rc;
126
127 if (firmware_has_feature(FW_FEATURE_ISERIES))
128 return -EIO;
129
130 /* Register as a vio device to receive callbacks */
131 rc = vio_register_driver(&hvc_vio_driver);
132
133 return rc;
134}
135module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
136
137static void __exit hvc_vio_exit(void)
138{
139 vio_unregister_driver(&hvc_vio_driver);
140}
141module_exit(hvc_vio_exit);
142
143/* the device tree order defines our numbering */
144static int hvc_find_vtys(void)
145{
146 struct device_node *vty;
147 int num_found = 0;
148
149 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
150 vty = of_find_node_by_name(vty, "vty")) {
151 const uint32_t *vtermno;
152
153 /* We have statically defined space for only a certain number
154 * of console adapters.
155 */
156 if (num_found >= MAX_NR_HVC_CONSOLES) {
157 of_node_put(vty);
158 break;
159 }
160
161 vtermno = of_get_property(vty, "reg", NULL);
162 if (!vtermno)
163 continue;
164
165 if (of_device_is_compatible(vty, "hvterm1")) {
166 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
167 ++num_found;
168 }
169 }
170
171 return num_found;
172}
173console_initcall(hvc_find_vtys);
diff --git a/drivers/tty/hvc/hvc_xen.c b/drivers/tty/hvc/hvc_xen.c
new file mode 100644
index 000000000000..52fdf60bdbe2
--- /dev/null
+++ b/drivers/tty/hvc/hvc_xen.c
@@ -0,0 +1,273 @@
1/*
2 * xen console driver interface to hvc_console.c
3 *
4 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/types.h>
26
27#include <asm/xen/hypervisor.h>
28
29#include <xen/xen.h>
30#include <xen/page.h>
31#include <xen/events.h>
32#include <xen/interface/io/console.h>
33#include <xen/hvc-console.h>
34
35#include "hvc_console.h"
36
37#define HVC_COOKIE 0x58656e /* "Xen" in hex */
38
39static struct hvc_struct *hvc;
40static int xencons_irq;
41
42/* ------------------------------------------------------------------ */
43
44static unsigned long console_pfn = ~0ul;
45
46static inline struct xencons_interface *xencons_interface(void)
47{
48 if (console_pfn == ~0ul)
49 return mfn_to_virt(xen_start_info->console.domU.mfn);
50 else
51 return __va(console_pfn << PAGE_SHIFT);
52}
53
54static inline void notify_daemon(void)
55{
56 /* Use evtchn: this is called early, before irq is set up. */
57 notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
58}
59
60static int __write_console(const char *data, int len)
61{
62 struct xencons_interface *intf = xencons_interface();
63 XENCONS_RING_IDX cons, prod;
64 int sent = 0;
65
66 cons = intf->out_cons;
67 prod = intf->out_prod;
68 mb(); /* update queue values before going on */
69 BUG_ON((prod - cons) > sizeof(intf->out));
70
71 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
72 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
73
74 wmb(); /* write ring before updating pointer */
75 intf->out_prod = prod;
76
77 if (sent)
78 notify_daemon();
79 return sent;
80}
81
82static int domU_write_console(uint32_t vtermno, const char *data, int len)
83{
84 int ret = len;
85
86 /*
87 * Make sure the whole buffer is emitted, polling if
88 * necessary. We don't ever want to rely on the hvc daemon
89 * because the most interesting console output is when the
90 * kernel is crippled.
91 */
92 while (len) {
93 int sent = __write_console(data, len);
94
95 data += sent;
96 len -= sent;
97
98 if (unlikely(len))
99 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
100 }
101
102 return ret;
103}
104
105static int domU_read_console(uint32_t vtermno, char *buf, int len)
106{
107 struct xencons_interface *intf = xencons_interface();
108 XENCONS_RING_IDX cons, prod;
109 int recv = 0;
110
111 cons = intf->in_cons;
112 prod = intf->in_prod;
113 mb(); /* get pointers before reading ring */
114 BUG_ON((prod - cons) > sizeof(intf->in));
115
116 while (cons != prod && recv < len)
117 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
118
119 mb(); /* read ring before consuming */
120 intf->in_cons = cons;
121
122 notify_daemon();
123 return recv;
124}
125
126static struct hv_ops domU_hvc_ops = {
127 .get_chars = domU_read_console,
128 .put_chars = domU_write_console,
129 .notifier_add = notifier_add_irq,
130 .notifier_del = notifier_del_irq,
131 .notifier_hangup = notifier_hangup_irq,
132};
133
134static int dom0_read_console(uint32_t vtermno, char *buf, int len)
135{
136 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
137}
138
139/*
140 * Either for a dom0 to write to the system console, or a domU with a
141 * debug version of Xen
142 */
143static int dom0_write_console(uint32_t vtermno, const char *str, int len)
144{
145 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
146 if (rc < 0)
147 return 0;
148
149 return len;
150}
151
152static struct hv_ops dom0_hvc_ops = {
153 .get_chars = dom0_read_console,
154 .put_chars = dom0_write_console,
155 .notifier_add = notifier_add_irq,
156 .notifier_del = notifier_del_irq,
157 .notifier_hangup = notifier_hangup_irq,
158};
159
160static int __init xen_hvc_init(void)
161{
162 struct hvc_struct *hp;
163 struct hv_ops *ops;
164
165 if (!xen_pv_domain())
166 return -ENODEV;
167
168 if (xen_initial_domain()) {
169 ops = &dom0_hvc_ops;
170 xencons_irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
171 } else {
172 if (!xen_start_info->console.domU.evtchn)
173 return -ENODEV;
174
175 ops = &domU_hvc_ops;
176 xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
177 }
178 if (xencons_irq < 0)
179 xencons_irq = 0; /* NO_IRQ */
180 else
181 irq_set_noprobe(xencons_irq);
182
183 hp = hvc_alloc(HVC_COOKIE, xencons_irq, ops, 256);
184 if (IS_ERR(hp))
185 return PTR_ERR(hp);
186
187 hvc = hp;
188
189 console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
190
191 return 0;
192}
193
194void xen_console_resume(void)
195{
196 if (xencons_irq)
197 rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
198}
199
200static void __exit xen_hvc_fini(void)
201{
202 if (hvc)
203 hvc_remove(hvc);
204}
205
206static int xen_cons_init(void)
207{
208 struct hv_ops *ops;
209
210 if (!xen_pv_domain())
211 return 0;
212
213 if (xen_initial_domain())
214 ops = &dom0_hvc_ops;
215 else
216 ops = &domU_hvc_ops;
217
218 hvc_instantiate(HVC_COOKIE, 0, ops);
219 return 0;
220}
221
222module_init(xen_hvc_init);
223module_exit(xen_hvc_fini);
224console_initcall(xen_cons_init);
225
226#ifdef CONFIG_EARLY_PRINTK
227static void xenboot_write_console(struct console *console, const char *string,
228 unsigned len)
229{
230 unsigned int linelen, off = 0;
231 const char *pos;
232
233 dom0_write_console(0, string, len);
234
235 if (xen_initial_domain())
236 return;
237
238 domU_write_console(0, "(early) ", 8);
239 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
240 linelen = pos-string+off;
241 if (off + linelen > len)
242 break;
243 domU_write_console(0, string+off, linelen);
244 domU_write_console(0, "\r\n", 2);
245 off += linelen + 1;
246 }
247 if (off < len)
248 domU_write_console(0, string+off, len-off);
249}
250
251struct console xenboot_console = {
252 .name = "xenboot",
253 .write = xenboot_write_console,
254 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
255};
256#endif /* CONFIG_EARLY_PRINTK */
257
258void xen_raw_console_write(const char *str)
259{
260 dom0_write_console(0, str, strlen(str));
261}
262
263void xen_raw_printk(const char *fmt, ...)
264{
265 static char buf[512];
266 va_list ap;
267
268 va_start(ap, fmt);
269 vsnprintf(buf, sizeof(buf), fmt, ap);
270 va_end(ap);
271
272 xen_raw_console_write(buf);
273}
diff --git a/drivers/tty/hvc/hvcs.c b/drivers/tty/hvc/hvcs.c
new file mode 100644
index 000000000000..4c8b66546930
--- /dev/null
+++ b/drivers/tty/hvc/hvcs.c
@@ -0,0 +1,1616 @@
1/*
2 * IBM eServer Hypervisor Virtual Console Server Device Driver
3 * Copyright (C) 2003, 2004 IBM Corp.
4 * Ryan S. Arnold (rsa@us.ibm.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
21 *
22 * This is the device driver for the IBM Hypervisor Virtual Console Server,
23 * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
24 * user space applications access to the system consoles of logically
25 * partitioned operating systems, e.g. Linux, running on the same partitioned
26 * Power5 ppc64 system. Physical hardware consoles per partition are not
27 * practical on this hardware so system consoles are accessed by this driver
28 * using inter-partition firmware interfaces to virtual terminal devices.
29 *
30 * A vty is known to the HMC as a "virtual serial server adapter". It is a
31 * virtual terminal device that is created by firmware upon partition creation
32 * to act as a partitioned OS's console device.
33 *
34 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
35 * Linux system upon their creation by the HMC or their exposure during boot.
36 * The non-user interactive backend of this driver is implemented as a vio
37 * device driver so that it can receive notification of vty-server lifetimes
38 * after it registers with the vio bus to handle vty-server probe and remove
39 * callbacks.
40 *
41 * Many vty-servers can be configured to connect to one vty, but a vty can
42 * only be actively connected to by a single vty-server, in any manner, at one
43 * time. If the HMC is currently hosting the console for a target Linux
44 * partition; attempts to open the tty device to the partition's console using
45 * the hvcs on any partition will return -EBUSY with every open attempt until
46 * the HMC frees the connection between its vty-server and the desired
47 * partition's vty device. Conversely, a vty-server may only be connected to
48 * a single vty at one time even though it may have several configured vty
49 * partner possibilities.
50 *
51 * Firmware does not provide notification of vty partner changes to this
52 * driver. This means that an HMC Super Admin may add or remove partner vtys
53 * from a vty-server's partner list but the changes will not be signaled to
54 * the vty-server. Firmware only notifies the driver when a vty-server is
55 * added or removed from the system. To compensate for this deficiency, this
56 * driver implements a sysfs update attribute which provides a method for
57 * rescanning partner information upon a user's request.
58 *
59 * Each vty-server, prior to being exposed to this driver is reference counted
60 * using the 2.6 Linux kernel kref construct.
61 *
62 * For direction on installation and usage of this driver please reference
63 * Documentation/powerpc/hvcs.txt.
64 */
65
66#include <linux/device.h>
67#include <linux/init.h>
68#include <linux/interrupt.h>
69#include <linux/kernel.h>
70#include <linux/kref.h>
71#include <linux/kthread.h>
72#include <linux/list.h>
73#include <linux/major.h>
74#include <linux/module.h>
75#include <linux/moduleparam.h>
76#include <linux/sched.h>
77#include <linux/slab.h>
78#include <linux/spinlock.h>
79#include <linux/stat.h>
80#include <linux/tty.h>
81#include <linux/tty_flip.h>
82#include <asm/hvconsole.h>
83#include <asm/hvcserver.h>
84#include <asm/uaccess.h>
85#include <asm/vio.h>
86
87/*
88 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
89 * Removed braces around single statements following conditionals. Removed '=
90 * 0' after static int declarations since these default to zero. Removed
91 * list_for_each_safe() and replaced with list_for_each_entry() in
92 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
93 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
94 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
95 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
96 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
97 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
98 * list traversals from a deletion. Removed '= NULL' from pointer declaration
99 * statements since they are initialized NULL by default. Removed wmb()
100 * instances from hvcs_try_write(). They probably aren't needed with locking in
101 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
102 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
103 * the coupling between /dev/hvcs* and a vty-server can be automatically
104 * determined. Moved kobject_put() in hvcs_open outside of the
105 * spin_unlock_irqrestore().
106 *
107 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
108 * align with how the tty layer always assigns the lowest index available. This
109 * change resulted in a list of ints that denotes which indexes are available.
110 * Device additions and removals use the new hvcs_get_index() and
111 * hvcs_return_index() helper functions. The list is created with
112 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
113 * Without these fixes hotplug vty-server adapter support goes crazy with this
114 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
115 * the hvcs_final_close() function in order to get it out of the spinlock.
116 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
117 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
118 * arch/powerepc/include/asm/hvcserver.h
119 *
120 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
121 * prevent possible lockup with realtime scheduling as similarly pointed out by
122 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
123 * to reorder cleanup operations and prevent discarding of pending data during
124 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
125 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
126 */
127
128#define HVCS_DRIVER_VERSION "1.3.3"
129
130MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
131MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
132MODULE_LICENSE("GPL");
133MODULE_VERSION(HVCS_DRIVER_VERSION);
134
135/*
136 * Wait this long per iteration while trying to push buffered data to the
137 * hypervisor before allowing the tty to complete a close operation.
138 */
139#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
140
141/*
142 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
143 * addition of tty derived devices and we shouldn't allocate thousands of
144 * tty_device pointers when the number of vty-server & vty partner connections
145 * will most often be much lower than this, we'll arbitrarily allocate
146 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
147 * register the tty_driver. This can be overridden using an insmod parameter.
148 */
149#define HVCS_DEFAULT_SERVER_ADAPTERS 64
150
151/*
152 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
153 * nodes as a sanity check. Theoretically there can be over 1 Billion
154 * vty-server & vty partner connections.
155 */
156#define HVCS_MAX_SERVER_ADAPTERS 1024
157
158/*
159 * We let Linux assign us a major number and we start the minors at zero. There
160 * is no intuitive mapping between minor number and the target vty-server
161 * adapter except that each new vty-server adapter is always assigned to the
162 * smallest minor number available.
163 */
164#define HVCS_MINOR_START 0
165
166/*
167 * The hcall interface involves putting 8 chars into each of two registers.
168 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
169 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
170 * little (tiny) bit slower because an unaligned load is slower than aligned
171 * load.
172 */
173#define __ALIGNED__ __attribute__((__aligned__(8)))
174
175/*
176 * How much data can firmware send with each hvc_put_chars()? Maybe this
177 * should be moved into an architecture specific area.
178 */
179#define HVCS_BUFF_LEN 16
180
181/*
182 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
183 * once in a chunk as a sanity check.
184 */
185#define HVCS_MAX_FROM_USER 4096
186
187/*
188 * Be careful when adding flags to this line discipline. Don't add anything
189 * that will cause echoing or we'll go into recursive loop echoing chars back
190 * and forth with the console drivers.
191 */
192static struct ktermios hvcs_tty_termios = {
193 .c_iflag = IGNBRK | IGNPAR,
194 .c_oflag = OPOST,
195 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
196 .c_cc = INIT_C_CC,
197 .c_ispeed = 38400,
198 .c_ospeed = 38400
199};
200
201/*
202 * This value is used to take the place of a command line parameter when the
203 * module is inserted. It starts as -1 and stays as such if the user doesn't
204 * specify a module insmod parameter. If they DO specify one then it is set to
205 * the value of the integer passed in.
206 */
207static int hvcs_parm_num_devs = -1;
208module_param(hvcs_parm_num_devs, int, 0);
209
210static const char hvcs_driver_name[] = "hvcs";
211static const char hvcs_device_node[] = "hvcs";
212static const char hvcs_driver_string[]
213 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
214
215/* Status of partner info rescan triggered via sysfs. */
216static int hvcs_rescan_status;
217
218static struct tty_driver *hvcs_tty_driver;
219
220/*
221 * In order to be somewhat sane this driver always associates the hvcs_struct
222 * index element with the numerically equal tty->index. This means that a
223 * hotplugged vty-server adapter will always map to the lowest index valued
224 * device node. If vty-servers were hotplug removed from the system and then
225 * new ones added the new vty-server may have the largest slot number of all
226 * the vty-server adapters in the partition but it may have the lowest dev node
227 * index of all the adapters due to the hole left by the hotplug removed
228 * adapter. There are a set of functions provided to get the lowest index for
229 * a new device as well as return the index to the list. This list is allocated
230 * with a number of elements equal to the number of device nodes requested when
231 * the module was inserted.
232 */
233static int *hvcs_index_list;
234
235/*
236 * How large is the list? This is kept for traversal since the list is
237 * dynamically created.
238 */
239static int hvcs_index_count;
240
241/*
242 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
243 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
244 */
245static int hvcs_kicked;
246
247/*
248 * Use by the kthread construct for task operations like waking the sleeping
249 * thread and stopping the kthread.
250 */
251static struct task_struct *hvcs_task;
252
253/*
254 * We allocate this for the use of all of the hvcs_structs when they fetch
255 * partner info.
256 */
257static unsigned long *hvcs_pi_buff;
258
259/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
260static DEFINE_SPINLOCK(hvcs_pi_lock);
261
262/* One vty-server per hvcs_struct */
263struct hvcs_struct {
264 spinlock_t lock;
265
266 /*
267 * This index identifies this hvcs device as the complement to a
268 * specific tty index.
269 */
270 unsigned int index;
271
272 struct tty_struct *tty;
273 int open_count;
274
275 /*
276 * Used to tell the driver kernel_thread what operations need to take
277 * place upon this hvcs_struct instance.
278 */
279 int todo_mask;
280
281 /*
282 * This buffer is required so that when hvcs_write_room() reports that
283 * it can send HVCS_BUFF_LEN characters that it will buffer the full
284 * HVCS_BUFF_LEN characters if need be. This is essential for opost
285 * writes since they do not do high level buffering and expect to be
286 * able to send what the driver commits to sending buffering
287 * [e.g. tab to space conversions in n_tty.c opost()].
288 */
289 char buffer[HVCS_BUFF_LEN];
290 int chars_in_buffer;
291
292 /*
293 * Any variable below the kref is valid before a tty is connected and
294 * stays valid after the tty is disconnected. These shouldn't be
295 * whacked until the kobject refcount reaches zero though some entries
296 * may be changed via sysfs initiatives.
297 */
298 struct kref kref; /* ref count & hvcs_struct lifetime */
299 int connected; /* is the vty-server currently connected to a vty? */
300 uint32_t p_unit_address; /* partner unit address */
301 uint32_t p_partition_ID; /* partner partition ID */
302 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
303 struct list_head next; /* list management */
304 struct vio_dev *vdev;
305};
306
307/* Required to back map a kref to its containing object */
308#define from_kref(k) container_of(k, struct hvcs_struct, kref)
309
310static LIST_HEAD(hvcs_structs);
311static DEFINE_SPINLOCK(hvcs_structs_lock);
312static DEFINE_MUTEX(hvcs_init_mutex);
313
314static void hvcs_unthrottle(struct tty_struct *tty);
315static void hvcs_throttle(struct tty_struct *tty);
316static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
317
318static int hvcs_write(struct tty_struct *tty,
319 const unsigned char *buf, int count);
320static int hvcs_write_room(struct tty_struct *tty);
321static int hvcs_chars_in_buffer(struct tty_struct *tty);
322
323static int hvcs_has_pi(struct hvcs_struct *hvcsd);
324static void hvcs_set_pi(struct hvcs_partner_info *pi,
325 struct hvcs_struct *hvcsd);
326static int hvcs_get_pi(struct hvcs_struct *hvcsd);
327static int hvcs_rescan_devices_list(void);
328
329static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
330static void hvcs_partner_free(struct hvcs_struct *hvcsd);
331
332static int hvcs_enable_device(struct hvcs_struct *hvcsd,
333 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
334
335static int hvcs_open(struct tty_struct *tty, struct file *filp);
336static void hvcs_close(struct tty_struct *tty, struct file *filp);
337static void hvcs_hangup(struct tty_struct * tty);
338
339static int __devinit hvcs_probe(struct vio_dev *dev,
340 const struct vio_device_id *id);
341static int __devexit hvcs_remove(struct vio_dev *dev);
342static int __init hvcs_module_init(void);
343static void __exit hvcs_module_exit(void);
344static int __devinit hvcs_initialize(void);
345
346#define HVCS_SCHED_READ 0x00000001
347#define HVCS_QUICK_READ 0x00000002
348#define HVCS_TRY_WRITE 0x00000004
349#define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
350
351static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
352{
353 return dev_get_drvdata(&viod->dev);
354}
355/* The sysfs interface for the driver and devices */
356
357static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
358{
359 struct vio_dev *viod = to_vio_dev(dev);
360 struct hvcs_struct *hvcsd = from_vio_dev(viod);
361 unsigned long flags;
362 int retval;
363
364 spin_lock_irqsave(&hvcsd->lock, flags);
365 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
366 spin_unlock_irqrestore(&hvcsd->lock, flags);
367 return retval;
368}
369static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
370
371static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
372{
373 struct vio_dev *viod = to_vio_dev(dev);
374 struct hvcs_struct *hvcsd = from_vio_dev(viod);
375 unsigned long flags;
376 int retval;
377
378 spin_lock_irqsave(&hvcsd->lock, flags);
379 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
380 spin_unlock_irqrestore(&hvcsd->lock, flags);
381 return retval;
382}
383static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
384
385static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
386 size_t count)
387{
388 /*
389 * Don't need this feature at the present time because firmware doesn't
390 * yet support multiple partners.
391 */
392 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
393 return -EPERM;
394}
395
396static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
397{
398 struct vio_dev *viod = to_vio_dev(dev);
399 struct hvcs_struct *hvcsd = from_vio_dev(viod);
400 unsigned long flags;
401 int retval;
402
403 spin_lock_irqsave(&hvcsd->lock, flags);
404 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
405 spin_unlock_irqrestore(&hvcsd->lock, flags);
406 return retval;
407}
408
409static DEVICE_ATTR(current_vty,
410 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
411
412static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
413 size_t count)
414{
415 struct vio_dev *viod = to_vio_dev(dev);
416 struct hvcs_struct *hvcsd = from_vio_dev(viod);
417 unsigned long flags;
418
419 /* writing a '0' to this sysfs entry will result in the disconnect. */
420 if (simple_strtol(buf, NULL, 0) != 0)
421 return -EINVAL;
422
423 spin_lock_irqsave(&hvcsd->lock, flags);
424
425 if (hvcsd->open_count > 0) {
426 spin_unlock_irqrestore(&hvcsd->lock, flags);
427 printk(KERN_INFO "HVCS: vterm state unchanged. "
428 "The hvcs device node is still in use.\n");
429 return -EPERM;
430 }
431
432 if (hvcsd->connected == 0) {
433 spin_unlock_irqrestore(&hvcsd->lock, flags);
434 printk(KERN_INFO "HVCS: vterm state unchanged. The"
435 " vty-server is not connected to a vty.\n");
436 return -EPERM;
437 }
438
439 hvcs_partner_free(hvcsd);
440 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
441 " partner vty@%X:%d connection.\n",
442 hvcsd->vdev->unit_address,
443 hvcsd->p_unit_address,
444 (uint32_t)hvcsd->p_partition_ID);
445
446 spin_unlock_irqrestore(&hvcsd->lock, flags);
447 return count;
448}
449
450static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
451{
452 struct vio_dev *viod = to_vio_dev(dev);
453 struct hvcs_struct *hvcsd = from_vio_dev(viod);
454 unsigned long flags;
455 int retval;
456
457 spin_lock_irqsave(&hvcsd->lock, flags);
458 retval = sprintf(buf, "%d\n", hvcsd->connected);
459 spin_unlock_irqrestore(&hvcsd->lock, flags);
460 return retval;
461}
462static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
463 hvcs_vterm_state_show, hvcs_vterm_state_store);
464
465static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
466{
467 struct vio_dev *viod = to_vio_dev(dev);
468 struct hvcs_struct *hvcsd = from_vio_dev(viod);
469 unsigned long flags;
470 int retval;
471
472 spin_lock_irqsave(&hvcsd->lock, flags);
473 retval = sprintf(buf, "%d\n", hvcsd->index);
474 spin_unlock_irqrestore(&hvcsd->lock, flags);
475 return retval;
476}
477
478static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
479
480static struct attribute *hvcs_attrs[] = {
481 &dev_attr_partner_vtys.attr,
482 &dev_attr_partner_clcs.attr,
483 &dev_attr_current_vty.attr,
484 &dev_attr_vterm_state.attr,
485 &dev_attr_index.attr,
486 NULL,
487};
488
489static struct attribute_group hvcs_attr_group = {
490 .attrs = hvcs_attrs,
491};
492
493static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
494{
495 /* A 1 means it is updating, a 0 means it is done updating */
496 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
497}
498
499static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
500 size_t count)
501{
502 if ((simple_strtol(buf, NULL, 0) != 1)
503 && (hvcs_rescan_status != 0))
504 return -EINVAL;
505
506 hvcs_rescan_status = 1;
507 printk(KERN_INFO "HVCS: rescanning partner info for all"
508 " vty-servers.\n");
509 hvcs_rescan_devices_list();
510 hvcs_rescan_status = 0;
511 return count;
512}
513
514static DRIVER_ATTR(rescan,
515 S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
516
517static void hvcs_kick(void)
518{
519 hvcs_kicked = 1;
520 wmb();
521 wake_up_process(hvcs_task);
522}
523
524static void hvcs_unthrottle(struct tty_struct *tty)
525{
526 struct hvcs_struct *hvcsd = tty->driver_data;
527 unsigned long flags;
528
529 spin_lock_irqsave(&hvcsd->lock, flags);
530 hvcsd->todo_mask |= HVCS_SCHED_READ;
531 spin_unlock_irqrestore(&hvcsd->lock, flags);
532 hvcs_kick();
533}
534
535static void hvcs_throttle(struct tty_struct *tty)
536{
537 struct hvcs_struct *hvcsd = tty->driver_data;
538 unsigned long flags;
539
540 spin_lock_irqsave(&hvcsd->lock, flags);
541 vio_disable_interrupts(hvcsd->vdev);
542 spin_unlock_irqrestore(&hvcsd->lock, flags);
543}
544
545/*
546 * If the device is being removed we don't have to worry about this interrupt
547 * handler taking any further interrupts because they are disabled which means
548 * the hvcs_struct will always be valid in this handler.
549 */
550static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
551{
552 struct hvcs_struct *hvcsd = dev_instance;
553
554 spin_lock(&hvcsd->lock);
555 vio_disable_interrupts(hvcsd->vdev);
556 hvcsd->todo_mask |= HVCS_SCHED_READ;
557 spin_unlock(&hvcsd->lock);
558 hvcs_kick();
559
560 return IRQ_HANDLED;
561}
562
563/* This function must be called with the hvcsd->lock held */
564static void hvcs_try_write(struct hvcs_struct *hvcsd)
565{
566 uint32_t unit_address = hvcsd->vdev->unit_address;
567 struct tty_struct *tty = hvcsd->tty;
568 int sent;
569
570 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
571 /* won't send partial writes */
572 sent = hvc_put_chars(unit_address,
573 &hvcsd->buffer[0],
574 hvcsd->chars_in_buffer );
575 if (sent > 0) {
576 hvcsd->chars_in_buffer = 0;
577 /* wmb(); */
578 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
579 /* wmb(); */
580
581 /*
582 * We are still obligated to deliver the data to the
583 * hypervisor even if the tty has been closed because
584 * we committed to delivering it. But don't try to wake
585 * a non-existent tty.
586 */
587 if (tty) {
588 tty_wakeup(tty);
589 }
590 }
591 }
592}
593
594static int hvcs_io(struct hvcs_struct *hvcsd)
595{
596 uint32_t unit_address;
597 struct tty_struct *tty;
598 char buf[HVCS_BUFF_LEN] __ALIGNED__;
599 unsigned long flags;
600 int got = 0;
601
602 spin_lock_irqsave(&hvcsd->lock, flags);
603
604 unit_address = hvcsd->vdev->unit_address;
605 tty = hvcsd->tty;
606
607 hvcs_try_write(hvcsd);
608
609 if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
610 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
611 goto bail;
612 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
613 goto bail;
614
615 /* remove the read masks */
616 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
617
618 if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
619 got = hvc_get_chars(unit_address,
620 &buf[0],
621 HVCS_BUFF_LEN);
622 tty_insert_flip_string(tty, buf, got);
623 }
624
625 /* Give the TTY time to process the data we just sent. */
626 if (got)
627 hvcsd->todo_mask |= HVCS_QUICK_READ;
628
629 spin_unlock_irqrestore(&hvcsd->lock, flags);
630 /* This is synch because tty->low_latency == 1 */
631 if(got)
632 tty_flip_buffer_push(tty);
633
634 if (!got) {
635 /* Do this _after_ the flip_buffer_push */
636 spin_lock_irqsave(&hvcsd->lock, flags);
637 vio_enable_interrupts(hvcsd->vdev);
638 spin_unlock_irqrestore(&hvcsd->lock, flags);
639 }
640
641 return hvcsd->todo_mask;
642
643 bail:
644 spin_unlock_irqrestore(&hvcsd->lock, flags);
645 return hvcsd->todo_mask;
646}
647
648static int khvcsd(void *unused)
649{
650 struct hvcs_struct *hvcsd;
651 int hvcs_todo_mask;
652
653 __set_current_state(TASK_RUNNING);
654
655 do {
656 hvcs_todo_mask = 0;
657 hvcs_kicked = 0;
658 wmb();
659
660 spin_lock(&hvcs_structs_lock);
661 list_for_each_entry(hvcsd, &hvcs_structs, next) {
662 hvcs_todo_mask |= hvcs_io(hvcsd);
663 }
664 spin_unlock(&hvcs_structs_lock);
665
666 /*
667 * If any of the hvcs adapters want to try a write or quick read
668 * don't schedule(), yield a smidgen then execute the hvcs_io
669 * thread again for those that want the write.
670 */
671 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
672 yield();
673 continue;
674 }
675
676 set_current_state(TASK_INTERRUPTIBLE);
677 if (!hvcs_kicked)
678 schedule();
679 __set_current_state(TASK_RUNNING);
680 } while (!kthread_should_stop());
681
682 return 0;
683}
684
685static struct vio_device_id hvcs_driver_table[] __devinitdata= {
686 {"serial-server", "hvterm2"},
687 { "", "" }
688};
689MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
690
691static void hvcs_return_index(int index)
692{
693 /* Paranoia check */
694 if (!hvcs_index_list)
695 return;
696 if (index < 0 || index >= hvcs_index_count)
697 return;
698 if (hvcs_index_list[index] == -1)
699 return;
700 else
701 hvcs_index_list[index] = -1;
702}
703
704/* callback when the kref ref count reaches zero */
705static void destroy_hvcs_struct(struct kref *kref)
706{
707 struct hvcs_struct *hvcsd = from_kref(kref);
708 struct vio_dev *vdev;
709 unsigned long flags;
710
711 spin_lock(&hvcs_structs_lock);
712 spin_lock_irqsave(&hvcsd->lock, flags);
713
714 /* the list_del poisons the pointers */
715 list_del(&(hvcsd->next));
716
717 if (hvcsd->connected == 1) {
718 hvcs_partner_free(hvcsd);
719 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
720 " partner vty@%X:%d connection.\n",
721 hvcsd->vdev->unit_address,
722 hvcsd->p_unit_address,
723 (uint32_t)hvcsd->p_partition_ID);
724 }
725 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
726 hvcsd->vdev->unit_address);
727
728 vdev = hvcsd->vdev;
729 hvcsd->vdev = NULL;
730
731 hvcsd->p_unit_address = 0;
732 hvcsd->p_partition_ID = 0;
733 hvcs_return_index(hvcsd->index);
734 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
735
736 spin_unlock_irqrestore(&hvcsd->lock, flags);
737 spin_unlock(&hvcs_structs_lock);
738
739 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
740
741 kfree(hvcsd);
742}
743
744static int hvcs_get_index(void)
745{
746 int i;
747 /* Paranoia check */
748 if (!hvcs_index_list) {
749 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
750 return -EFAULT;
751 }
752 /* Find the numerically lowest first free index. */
753 for(i = 0; i < hvcs_index_count; i++) {
754 if (hvcs_index_list[i] == -1) {
755 hvcs_index_list[i] = 0;
756 return i;
757 }
758 }
759 return -1;
760}
761
762static int __devinit hvcs_probe(
763 struct vio_dev *dev,
764 const struct vio_device_id *id)
765{
766 struct hvcs_struct *hvcsd;
767 int index, rc;
768 int retval;
769
770 if (!dev || !id) {
771 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
772 return -EPERM;
773 }
774
775 /* Make sure we are properly initialized */
776 rc = hvcs_initialize();
777 if (rc) {
778 pr_err("HVCS: Failed to initialize core driver.\n");
779 return rc;
780 }
781
782 /* early to avoid cleanup on failure */
783 index = hvcs_get_index();
784 if (index < 0) {
785 return -EFAULT;
786 }
787
788 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
789 if (!hvcsd)
790 return -ENODEV;
791
792
793 spin_lock_init(&hvcsd->lock);
794 /* Automatically incs the refcount the first time */
795 kref_init(&hvcsd->kref);
796
797 hvcsd->vdev = dev;
798 dev_set_drvdata(&dev->dev, hvcsd);
799
800 hvcsd->index = index;
801
802 /* hvcsd->index = ++hvcs_struct_count; */
803 hvcsd->chars_in_buffer = 0;
804 hvcsd->todo_mask = 0;
805 hvcsd->connected = 0;
806
807 /*
808 * This will populate the hvcs_struct's partner info fields for the
809 * first time.
810 */
811 if (hvcs_get_pi(hvcsd)) {
812 printk(KERN_ERR "HVCS: Failed to fetch partner"
813 " info for vty-server@%X on device probe.\n",
814 hvcsd->vdev->unit_address);
815 }
816
817 /*
818 * If a user app opens a tty that corresponds to this vty-server before
819 * the hvcs_struct has been added to the devices list then the user app
820 * will get -ENODEV.
821 */
822 spin_lock(&hvcs_structs_lock);
823 list_add_tail(&(hvcsd->next), &hvcs_structs);
824 spin_unlock(&hvcs_structs_lock);
825
826 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
827 if (retval) {
828 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
829 hvcsd->vdev->unit_address);
830 return retval;
831 }
832
833 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
834
835 /*
836 * DON'T enable interrupts here because there is no user to receive the
837 * data.
838 */
839 return 0;
840}
841
842static int __devexit hvcs_remove(struct vio_dev *dev)
843{
844 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
845 unsigned long flags;
846 struct tty_struct *tty;
847
848 if (!hvcsd)
849 return -ENODEV;
850
851 /* By this time the vty-server won't be getting any more interrupts */
852
853 spin_lock_irqsave(&hvcsd->lock, flags);
854
855 tty = hvcsd->tty;
856
857 spin_unlock_irqrestore(&hvcsd->lock, flags);
858
859 /*
860 * Let the last holder of this object cause it to be removed, which
861 * would probably be tty_hangup below.
862 */
863 kref_put(&hvcsd->kref, destroy_hvcs_struct);
864
865 /*
866 * The hangup is a scheduled function which will auto chain call
867 * hvcs_hangup. The tty should always be valid at this time unless a
868 * simultaneous tty close already cleaned up the hvcs_struct.
869 */
870 if (tty)
871 tty_hangup(tty);
872
873 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
874 " vio bus.\n", dev->unit_address);
875 return 0;
876};
877
878static struct vio_driver hvcs_vio_driver = {
879 .id_table = hvcs_driver_table,
880 .probe = hvcs_probe,
881 .remove = __devexit_p(hvcs_remove),
882 .driver = {
883 .name = hvcs_driver_name,
884 .owner = THIS_MODULE,
885 }
886};
887
888/* Only called from hvcs_get_pi please */
889static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
890{
891 int clclength;
892
893 hvcsd->p_unit_address = pi->unit_address;
894 hvcsd->p_partition_ID = pi->partition_ID;
895 clclength = strlen(&pi->location_code[0]);
896 if (clclength > HVCS_CLC_LENGTH)
897 clclength = HVCS_CLC_LENGTH;
898
899 /* copy the null-term char too */
900 strncpy(&hvcsd->p_location_code[0],
901 &pi->location_code[0], clclength + 1);
902}
903
904/*
905 * Traverse the list and add the partner info that is found to the hvcs_struct
906 * struct entry. NOTE: At this time I know that partner info will return a
907 * single entry but in the future there may be multiple partner info entries per
908 * vty-server and you'll want to zero out that list and reset it. If for some
909 * reason you have an old version of this driver but there IS more than one
910 * partner info then hvcsd->p_* will hold the last partner info data from the
911 * firmware query. A good way to update this code would be to replace the three
912 * partner info fields in hvcs_struct with a list of hvcs_partner_info
913 * instances.
914 *
915 * This function must be called with the hvcsd->lock held.
916 */
917static int hvcs_get_pi(struct hvcs_struct *hvcsd)
918{
919 struct hvcs_partner_info *pi;
920 uint32_t unit_address = hvcsd->vdev->unit_address;
921 struct list_head head;
922 int retval;
923
924 spin_lock(&hvcs_pi_lock);
925 if (!hvcs_pi_buff) {
926 spin_unlock(&hvcs_pi_lock);
927 return -EFAULT;
928 }
929 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
930 spin_unlock(&hvcs_pi_lock);
931 if (retval) {
932 printk(KERN_ERR "HVCS: Failed to fetch partner"
933 " info for vty-server@%x.\n", unit_address);
934 return retval;
935 }
936
937 /* nixes the values if the partner vty went away */
938 hvcsd->p_unit_address = 0;
939 hvcsd->p_partition_ID = 0;
940
941 list_for_each_entry(pi, &head, node)
942 hvcs_set_pi(pi, hvcsd);
943
944 hvcs_free_partner_info(&head);
945 return 0;
946}
947
948/*
949 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
950 * be executed elsewhere, in order to prevent deadlock issues.
951 */
952static int hvcs_rescan_devices_list(void)
953{
954 struct hvcs_struct *hvcsd;
955 unsigned long flags;
956
957 spin_lock(&hvcs_structs_lock);
958
959 list_for_each_entry(hvcsd, &hvcs_structs, next) {
960 spin_lock_irqsave(&hvcsd->lock, flags);
961 hvcs_get_pi(hvcsd);
962 spin_unlock_irqrestore(&hvcsd->lock, flags);
963 }
964
965 spin_unlock(&hvcs_structs_lock);
966
967 return 0;
968}
969
970/*
971 * Farm this off into its own function because it could be more complex once
972 * multiple partners support is added. This function should be called with
973 * the hvcsd->lock held.
974 */
975static int hvcs_has_pi(struct hvcs_struct *hvcsd)
976{
977 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
978 return 0;
979 return 1;
980}
981
982/*
983 * NOTE: It is possible that the super admin removed a partner vty and then
984 * added a different vty as the new partner.
985 *
986 * This function must be called with the hvcsd->lock held.
987 */
988static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
989{
990 int retval;
991 unsigned int unit_address = hvcsd->vdev->unit_address;
992
993 /*
994 * If there wasn't any pi when the device was added it doesn't meant
995 * there isn't any now. This driver isn't notified when a new partner
996 * vty is added to a vty-server so we discover changes on our own.
997 * Please see comments in hvcs_register_connection() for justification
998 * of this bizarre code.
999 */
1000 retval = hvcs_register_connection(unit_address,
1001 hvcsd->p_partition_ID,
1002 hvcsd->p_unit_address);
1003 if (!retval) {
1004 hvcsd->connected = 1;
1005 return 0;
1006 } else if (retval != -EINVAL)
1007 return retval;
1008
1009 /*
1010 * As per the spec re-get the pi and try again if -EINVAL after the
1011 * first connection attempt.
1012 */
1013 if (hvcs_get_pi(hvcsd))
1014 return -ENOMEM;
1015
1016 if (!hvcs_has_pi(hvcsd))
1017 return -ENODEV;
1018
1019 retval = hvcs_register_connection(unit_address,
1020 hvcsd->p_partition_ID,
1021 hvcsd->p_unit_address);
1022 if (retval != -EINVAL) {
1023 hvcsd->connected = 1;
1024 return retval;
1025 }
1026
1027 /*
1028 * EBUSY is the most likely scenario though the vty could have been
1029 * removed or there really could be an hcall error due to the parameter
1030 * data but thanks to ambiguous firmware return codes we can't really
1031 * tell.
1032 */
1033 printk(KERN_INFO "HVCS: vty-server or partner"
1034 " vty is busy. Try again later.\n");
1035 return -EBUSY;
1036}
1037
1038/* This function must be called with the hvcsd->lock held */
1039static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1040{
1041 int retval;
1042 do {
1043 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1044 } while (retval == -EBUSY);
1045 hvcsd->connected = 0;
1046}
1047
1048/* This helper function must be called WITHOUT the hvcsd->lock held */
1049static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1050 unsigned int irq, struct vio_dev *vdev)
1051{
1052 unsigned long flags;
1053 int rc;
1054
1055 /*
1056 * It is possible that the vty-server was removed between the time that
1057 * the conn was registered and now.
1058 */
1059 if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
1060 IRQF_DISABLED, "ibmhvcs", hvcsd))) {
1061 /*
1062 * It is possible the vty-server was removed after the irq was
1063 * requested but before we have time to enable interrupts.
1064 */
1065 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1066 return 0;
1067 else {
1068 printk(KERN_ERR "HVCS: int enable failed for"
1069 " vty-server@%X.\n", unit_address);
1070 free_irq(irq, hvcsd);
1071 }
1072 } else
1073 printk(KERN_ERR "HVCS: irq req failed for"
1074 " vty-server@%X.\n", unit_address);
1075
1076 spin_lock_irqsave(&hvcsd->lock, flags);
1077 hvcs_partner_free(hvcsd);
1078 spin_unlock_irqrestore(&hvcsd->lock, flags);
1079
1080 return rc;
1081
1082}
1083
1084/*
1085 * This always increments the kref ref count if the call is successful.
1086 * Please remember to dec when you are done with the instance.
1087 *
1088 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1089 * calling this function or you will get deadlock.
1090 */
1091static struct hvcs_struct *hvcs_get_by_index(int index)
1092{
1093 struct hvcs_struct *hvcsd = NULL;
1094 unsigned long flags;
1095
1096 spin_lock(&hvcs_structs_lock);
1097 /* We can immediately discard OOB requests */
1098 if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
1099 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1100 spin_lock_irqsave(&hvcsd->lock, flags);
1101 if (hvcsd->index == index) {
1102 kref_get(&hvcsd->kref);
1103 spin_unlock_irqrestore(&hvcsd->lock, flags);
1104 spin_unlock(&hvcs_structs_lock);
1105 return hvcsd;
1106 }
1107 spin_unlock_irqrestore(&hvcsd->lock, flags);
1108 }
1109 hvcsd = NULL;
1110 }
1111
1112 spin_unlock(&hvcs_structs_lock);
1113 return hvcsd;
1114}
1115
1116/*
1117 * This is invoked via the tty_open interface when a user app connects to the
1118 * /dev node.
1119 */
1120static int hvcs_open(struct tty_struct *tty, struct file *filp)
1121{
1122 struct hvcs_struct *hvcsd;
1123 int rc, retval = 0;
1124 unsigned long flags;
1125 unsigned int irq;
1126 struct vio_dev *vdev;
1127 unsigned long unit_address;
1128
1129 if (tty->driver_data)
1130 goto fast_open;
1131
1132 /*
1133 * Is there a vty-server that shares the same index?
1134 * This function increments the kref index.
1135 */
1136 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1137 printk(KERN_WARNING "HVCS: open failed, no device associated"
1138 " with tty->index %d.\n", tty->index);
1139 return -ENODEV;
1140 }
1141
1142 spin_lock_irqsave(&hvcsd->lock, flags);
1143
1144 if (hvcsd->connected == 0)
1145 if ((retval = hvcs_partner_connect(hvcsd)))
1146 goto error_release;
1147
1148 hvcsd->open_count = 1;
1149 hvcsd->tty = tty;
1150 tty->driver_data = hvcsd;
1151
1152 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1153
1154 /*
1155 * Save these in the spinlock for the enable operations that need them
1156 * outside of the spinlock.
1157 */
1158 irq = hvcsd->vdev->irq;
1159 vdev = hvcsd->vdev;
1160 unit_address = hvcsd->vdev->unit_address;
1161
1162 hvcsd->todo_mask |= HVCS_SCHED_READ;
1163 spin_unlock_irqrestore(&hvcsd->lock, flags);
1164
1165 /*
1166 * This must be done outside of the spinlock because it requests irqs
1167 * and will grab the spinlock and free the connection if it fails.
1168 */
1169 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1170 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1171 printk(KERN_WARNING "HVCS: enable device failed.\n");
1172 return rc;
1173 }
1174
1175 goto open_success;
1176
1177fast_open:
1178 hvcsd = tty->driver_data;
1179
1180 spin_lock_irqsave(&hvcsd->lock, flags);
1181 kref_get(&hvcsd->kref);
1182 hvcsd->open_count++;
1183 hvcsd->todo_mask |= HVCS_SCHED_READ;
1184 spin_unlock_irqrestore(&hvcsd->lock, flags);
1185
1186open_success:
1187 hvcs_kick();
1188
1189 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1190 hvcsd->vdev->unit_address );
1191
1192 return 0;
1193
1194error_release:
1195 spin_unlock_irqrestore(&hvcsd->lock, flags);
1196 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1197
1198 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1199 return retval;
1200}
1201
1202static void hvcs_close(struct tty_struct *tty, struct file *filp)
1203{
1204 struct hvcs_struct *hvcsd;
1205 unsigned long flags;
1206 int irq = NO_IRQ;
1207
1208 /*
1209 * Is someone trying to close the file associated with this device after
1210 * we have hung up? If so tty->driver_data wouldn't be valid.
1211 */
1212 if (tty_hung_up_p(filp))
1213 return;
1214
1215 /*
1216 * No driver_data means that this close was probably issued after a
1217 * failed hvcs_open by the tty layer's release_dev() api and we can just
1218 * exit cleanly.
1219 */
1220 if (!tty->driver_data)
1221 return;
1222
1223 hvcsd = tty->driver_data;
1224
1225 spin_lock_irqsave(&hvcsd->lock, flags);
1226 if (--hvcsd->open_count == 0) {
1227
1228 vio_disable_interrupts(hvcsd->vdev);
1229
1230 /*
1231 * NULL this early so that the kernel_thread doesn't try to
1232 * execute any operations on the TTY even though it is obligated
1233 * to deliver any pending I/O to the hypervisor.
1234 */
1235 hvcsd->tty = NULL;
1236
1237 irq = hvcsd->vdev->irq;
1238 spin_unlock_irqrestore(&hvcsd->lock, flags);
1239
1240 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1241
1242 /*
1243 * This line is important because it tells hvcs_open that this
1244 * device needs to be re-configured the next time hvcs_open is
1245 * called.
1246 */
1247 tty->driver_data = NULL;
1248
1249 free_irq(irq, hvcsd);
1250 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1251 return;
1252 } else if (hvcsd->open_count < 0) {
1253 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1254 " is missmanaged.\n",
1255 hvcsd->vdev->unit_address, hvcsd->open_count);
1256 }
1257
1258 spin_unlock_irqrestore(&hvcsd->lock, flags);
1259 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1260}
1261
1262static void hvcs_hangup(struct tty_struct * tty)
1263{
1264 struct hvcs_struct *hvcsd = tty->driver_data;
1265 unsigned long flags;
1266 int temp_open_count;
1267 int irq = NO_IRQ;
1268
1269 spin_lock_irqsave(&hvcsd->lock, flags);
1270 /* Preserve this so that we know how many kref refs to put */
1271 temp_open_count = hvcsd->open_count;
1272
1273 /*
1274 * Don't kref put inside the spinlock because the destruction
1275 * callback may use the spinlock and it may get called before the
1276 * spinlock has been released.
1277 */
1278 vio_disable_interrupts(hvcsd->vdev);
1279
1280 hvcsd->todo_mask = 0;
1281
1282 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1283 hvcsd->tty->driver_data = NULL;
1284 hvcsd->tty = NULL;
1285
1286 hvcsd->open_count = 0;
1287
1288 /* This will drop any buffered data on the floor which is OK in a hangup
1289 * scenario. */
1290 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1291 hvcsd->chars_in_buffer = 0;
1292
1293 irq = hvcsd->vdev->irq;
1294
1295 spin_unlock_irqrestore(&hvcsd->lock, flags);
1296
1297 free_irq(irq, hvcsd);
1298
1299 /*
1300 * We need to kref_put() for every open_count we have since the
1301 * tty_hangup() function doesn't invoke a close per open connection on a
1302 * non-console device.
1303 */
1304 while(temp_open_count) {
1305 --temp_open_count;
1306 /*
1307 * The final put will trigger destruction of the hvcs_struct.
1308 * NOTE: If this hangup was signaled from user space then the
1309 * final put will never happen.
1310 */
1311 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1312 }
1313}
1314
1315/*
1316 * NOTE: This is almost always from_user since user level apps interact with the
1317 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1318 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1319 * tty_hangup will allow hvcs_write time to complete execution before it
1320 * terminates our device.
1321 */
1322static int hvcs_write(struct tty_struct *tty,
1323 const unsigned char *buf, int count)
1324{
1325 struct hvcs_struct *hvcsd = tty->driver_data;
1326 unsigned int unit_address;
1327 const unsigned char *charbuf;
1328 unsigned long flags;
1329 int total_sent = 0;
1330 int tosend = 0;
1331 int result = 0;
1332
1333 /*
1334 * If they don't check the return code off of their open they may
1335 * attempt this even if there is no connected device.
1336 */
1337 if (!hvcsd)
1338 return -ENODEV;
1339
1340 /* Reasonable size to prevent user level flooding */
1341 if (count > HVCS_MAX_FROM_USER) {
1342 printk(KERN_WARNING "HVCS write: count being truncated to"
1343 " HVCS_MAX_FROM_USER.\n");
1344 count = HVCS_MAX_FROM_USER;
1345 }
1346
1347 charbuf = buf;
1348
1349 spin_lock_irqsave(&hvcsd->lock, flags);
1350
1351 /*
1352 * Somehow an open succeeded but the device was removed or the
1353 * connection terminated between the vty-server and partner vty during
1354 * the middle of a write operation? This is a crummy place to do this
1355 * but we want to keep it all in the spinlock.
1356 */
1357 if (hvcsd->open_count <= 0) {
1358 spin_unlock_irqrestore(&hvcsd->lock, flags);
1359 return -ENODEV;
1360 }
1361
1362 unit_address = hvcsd->vdev->unit_address;
1363
1364 while (count > 0) {
1365 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1366 /*
1367 * No more space, this probably means that the last call to
1368 * hvcs_write() didn't succeed and the buffer was filled up.
1369 */
1370 if (!tosend)
1371 break;
1372
1373 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1374 &charbuf[total_sent],
1375 tosend);
1376
1377 hvcsd->chars_in_buffer += tosend;
1378
1379 result = 0;
1380
1381 /*
1382 * If this is true then we don't want to try writing to the
1383 * hypervisor because that is the kernel_threads job now. We'll
1384 * just add to the buffer.
1385 */
1386 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1387 /* won't send partial writes */
1388 result = hvc_put_chars(unit_address,
1389 &hvcsd->buffer[0],
1390 hvcsd->chars_in_buffer);
1391
1392 /*
1393 * Since we know we have enough room in hvcsd->buffer for
1394 * tosend we record that it was sent regardless of whether the
1395 * hypervisor actually took it because we have it buffered.
1396 */
1397 total_sent+=tosend;
1398 count-=tosend;
1399 if (result == 0) {
1400 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1401 hvcs_kick();
1402 break;
1403 }
1404
1405 hvcsd->chars_in_buffer = 0;
1406 /*
1407 * Test after the chars_in_buffer reset otherwise this could
1408 * deadlock our writes if hvc_put_chars fails.
1409 */
1410 if (result < 0)
1411 break;
1412 }
1413
1414 spin_unlock_irqrestore(&hvcsd->lock, flags);
1415
1416 if (result == -1)
1417 return -EIO;
1418 else
1419 return total_sent;
1420}
1421
1422/*
1423 * This is really asking how much can we guarantee that we can send or that we
1424 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1425 * return value, hence the reason for hvcs_struct buffering.
1426 */
1427static int hvcs_write_room(struct tty_struct *tty)
1428{
1429 struct hvcs_struct *hvcsd = tty->driver_data;
1430
1431 if (!hvcsd || hvcsd->open_count <= 0)
1432 return 0;
1433
1434 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1435}
1436
1437static int hvcs_chars_in_buffer(struct tty_struct *tty)
1438{
1439 struct hvcs_struct *hvcsd = tty->driver_data;
1440
1441 return hvcsd->chars_in_buffer;
1442}
1443
1444static const struct tty_operations hvcs_ops = {
1445 .open = hvcs_open,
1446 .close = hvcs_close,
1447 .hangup = hvcs_hangup,
1448 .write = hvcs_write,
1449 .write_room = hvcs_write_room,
1450 .chars_in_buffer = hvcs_chars_in_buffer,
1451 .unthrottle = hvcs_unthrottle,
1452 .throttle = hvcs_throttle,
1453};
1454
1455static int hvcs_alloc_index_list(int n)
1456{
1457 int i;
1458
1459 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1460 if (!hvcs_index_list)
1461 return -ENOMEM;
1462 hvcs_index_count = n;
1463 for (i = 0; i < hvcs_index_count; i++)
1464 hvcs_index_list[i] = -1;
1465 return 0;
1466}
1467
1468static void hvcs_free_index_list(void)
1469{
1470 /* Paranoia check to be thorough. */
1471 kfree(hvcs_index_list);
1472 hvcs_index_list = NULL;
1473 hvcs_index_count = 0;
1474}
1475
1476static int __devinit hvcs_initialize(void)
1477{
1478 int rc, num_ttys_to_alloc;
1479
1480 mutex_lock(&hvcs_init_mutex);
1481 if (hvcs_task) {
1482 mutex_unlock(&hvcs_init_mutex);
1483 return 0;
1484 }
1485
1486 /* Has the user specified an overload with an insmod param? */
1487 if (hvcs_parm_num_devs <= 0 ||
1488 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1489 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1490 } else
1491 num_ttys_to_alloc = hvcs_parm_num_devs;
1492
1493 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1494 if (!hvcs_tty_driver)
1495 return -ENOMEM;
1496
1497 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1498 rc = -ENOMEM;
1499 goto index_fail;
1500 }
1501
1502 hvcs_tty_driver->owner = THIS_MODULE;
1503
1504 hvcs_tty_driver->driver_name = hvcs_driver_name;
1505 hvcs_tty_driver->name = hvcs_device_node;
1506
1507 /*
1508 * We'll let the system assign us a major number, indicated by leaving
1509 * it blank.
1510 */
1511
1512 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1513 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1514
1515 /*
1516 * We role our own so that we DONT ECHO. We can't echo because the
1517 * device we are connecting to already echoes by default and this would
1518 * throw us into a horrible recursive echo-echo-echo loop.
1519 */
1520 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1521 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1522
1523 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1524
1525 /*
1526 * The following call will result in sysfs entries that denote the
1527 * dynamically assigned major and minor numbers for our devices.
1528 */
1529 if (tty_register_driver(hvcs_tty_driver)) {
1530 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1531 rc = -EIO;
1532 goto register_fail;
1533 }
1534
1535 hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
1536 if (!hvcs_pi_buff) {
1537 rc = -ENOMEM;
1538 goto buff_alloc_fail;
1539 }
1540
1541 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1542 if (IS_ERR(hvcs_task)) {
1543 printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1544 rc = -EIO;
1545 goto kthread_fail;
1546 }
1547 mutex_unlock(&hvcs_init_mutex);
1548 return 0;
1549
1550kthread_fail:
1551 kfree(hvcs_pi_buff);
1552buff_alloc_fail:
1553 tty_unregister_driver(hvcs_tty_driver);
1554register_fail:
1555 hvcs_free_index_list();
1556index_fail:
1557 put_tty_driver(hvcs_tty_driver);
1558 hvcs_tty_driver = NULL;
1559 mutex_unlock(&hvcs_init_mutex);
1560 return rc;
1561}
1562
1563static int __init hvcs_module_init(void)
1564{
1565 int rc = vio_register_driver(&hvcs_vio_driver);
1566 if (rc) {
1567 printk(KERN_ERR "HVCS: can't register vio driver\n");
1568 return rc;
1569 }
1570
1571 pr_info("HVCS: Driver registered.\n");
1572
1573 /* This needs to be done AFTER the vio_register_driver() call or else
1574 * the kobjects won't be initialized properly.
1575 */
1576 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1577 if (rc)
1578 pr_warning(KERN_ERR "HVCS: Failed to create rescan file (err %d)\n", rc);
1579
1580 return 0;
1581}
1582
1583static void __exit hvcs_module_exit(void)
1584{
1585 /*
1586 * This driver receives hvcs_remove callbacks for each device upon
1587 * module removal.
1588 */
1589 vio_unregister_driver(&hvcs_vio_driver);
1590 if (!hvcs_task)
1591 return;
1592
1593 /*
1594 * This synchronous operation will wake the khvcsd kthread if it is
1595 * asleep and will return when khvcsd has terminated.
1596 */
1597 kthread_stop(hvcs_task);
1598
1599 spin_lock(&hvcs_pi_lock);
1600 kfree(hvcs_pi_buff);
1601 hvcs_pi_buff = NULL;
1602 spin_unlock(&hvcs_pi_lock);
1603
1604 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1605
1606 tty_unregister_driver(hvcs_tty_driver);
1607
1608 hvcs_free_index_list();
1609
1610 put_tty_driver(hvcs_tty_driver);
1611
1612 printk(KERN_INFO "HVCS: driver module removed.\n");
1613}
1614
1615module_init(hvcs_module_init);
1616module_exit(hvcs_module_exit);
diff --git a/drivers/tty/hvc/hvsi.c b/drivers/tty/hvc/hvsi.c
new file mode 100644
index 000000000000..8a8d6373f164
--- /dev/null
+++ b/drivers/tty/hvc/hvsi.c
@@ -0,0 +1,1314 @@
1/*
2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
3 *
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 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19/* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
20 * and the service processor on IBM pSeries servers. On these servers, there
21 * are no serial ports under the OS's control, and sometimes there is no other
22 * console available either. However, the service processor has two standard
23 * serial ports, so this over-complicated protocol allows the OS to control
24 * those ports by proxy.
25 *
26 * Besides data, the procotol supports the reading/writing of the serial
27 * port's DTR line, and the reading of the CD line. This is to allow the OS to
28 * control a modem attached to the service processor's serial port. Note that
29 * the OS cannot change the speed of the port through this protocol.
30 */
31
32#undef DEBUG
33
34#include <linux/console.h>
35#include <linux/ctype.h>
36#include <linux/delay.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/module.h>
40#include <linux/major.h>
41#include <linux/kernel.h>
42#include <linux/spinlock.h>
43#include <linux/sysrq.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <asm/hvcall.h>
47#include <asm/hvconsole.h>
48#include <asm/prom.h>
49#include <asm/uaccess.h>
50#include <asm/vio.h>
51#include <asm/param.h>
52
53#define HVSI_MAJOR 229
54#define HVSI_MINOR 128
55#define MAX_NR_HVSI_CONSOLES 4
56
57#define HVSI_TIMEOUT (5*HZ)
58#define HVSI_VERSION 1
59#define HVSI_MAX_PACKET 256
60#define HVSI_MAX_READ 16
61#define HVSI_MAX_OUTGOING_DATA 12
62#define N_OUTBUF 12
63
64/*
65 * we pass data via two 8-byte registers, so we would like our char arrays
66 * properly aligned for those loads.
67 */
68#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
69
70struct hvsi_struct {
71 struct delayed_work writer;
72 struct work_struct handshaker;
73 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
74 wait_queue_head_t stateq; /* woken when HVSI state changes */
75 spinlock_t lock;
76 int index;
77 struct tty_struct *tty;
78 int count;
79 uint8_t throttle_buf[128];
80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
81 /* inbuf is for packet reassembly. leave a little room for leftovers. */
82 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
83 uint8_t *inbuf_end;
84 int n_throttle;
85 int n_outbuf;
86 uint32_t vtermno;
87 uint32_t virq;
88 atomic_t seqno; /* HVSI packet sequence number */
89 uint16_t mctrl;
90 uint8_t state; /* HVSI protocol state */
91 uint8_t flags;
92#ifdef CONFIG_MAGIC_SYSRQ
93 uint8_t sysrq;
94#endif /* CONFIG_MAGIC_SYSRQ */
95};
96static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
97
98static struct tty_driver *hvsi_driver;
99static int hvsi_count;
100static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
101
102enum HVSI_PROTOCOL_STATE {
103 HVSI_CLOSED,
104 HVSI_WAIT_FOR_VER_RESPONSE,
105 HVSI_WAIT_FOR_VER_QUERY,
106 HVSI_OPEN,
107 HVSI_WAIT_FOR_MCTRL_RESPONSE,
108 HVSI_FSP_DIED,
109};
110#define HVSI_CONSOLE 0x1
111
112#define VS_DATA_PACKET_HEADER 0xff
113#define VS_CONTROL_PACKET_HEADER 0xfe
114#define VS_QUERY_PACKET_HEADER 0xfd
115#define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
116
117/* control verbs */
118#define VSV_SET_MODEM_CTL 1 /* to service processor only */
119#define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
120#define VSV_CLOSE_PROTOCOL 3
121
122/* query verbs */
123#define VSV_SEND_VERSION_NUMBER 1
124#define VSV_SEND_MODEM_CTL_STATUS 2
125
126/* yes, these masks are not consecutive. */
127#define HVSI_TSDTR 0x01
128#define HVSI_TSCD 0x20
129
130struct hvsi_header {
131 uint8_t type;
132 uint8_t len;
133 uint16_t seqno;
134} __attribute__((packed));
135
136struct hvsi_data {
137 uint8_t type;
138 uint8_t len;
139 uint16_t seqno;
140 uint8_t data[HVSI_MAX_OUTGOING_DATA];
141} __attribute__((packed));
142
143struct hvsi_control {
144 uint8_t type;
145 uint8_t len;
146 uint16_t seqno;
147 uint16_t verb;
148 /* optional depending on verb: */
149 uint32_t word;
150 uint32_t mask;
151} __attribute__((packed));
152
153struct hvsi_query {
154 uint8_t type;
155 uint8_t len;
156 uint16_t seqno;
157 uint16_t verb;
158} __attribute__((packed));
159
160struct hvsi_query_response {
161 uint8_t type;
162 uint8_t len;
163 uint16_t seqno;
164 uint16_t verb;
165 uint16_t query_seqno;
166 union {
167 uint8_t version;
168 uint32_t mctrl_word;
169 } u;
170} __attribute__((packed));
171
172
173
174static inline int is_console(struct hvsi_struct *hp)
175{
176 return hp->flags & HVSI_CONSOLE;
177}
178
179static inline int is_open(struct hvsi_struct *hp)
180{
181 /* if we're waiting for an mctrl then we're already open */
182 return (hp->state == HVSI_OPEN)
183 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
184}
185
186static inline void print_state(struct hvsi_struct *hp)
187{
188#ifdef DEBUG
189 static const char *state_names[] = {
190 "HVSI_CLOSED",
191 "HVSI_WAIT_FOR_VER_RESPONSE",
192 "HVSI_WAIT_FOR_VER_QUERY",
193 "HVSI_OPEN",
194 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
195 "HVSI_FSP_DIED",
196 };
197 const char *name = (hp->state < ARRAY_SIZE(state_names))
198 ? state_names[hp->state] : "UNKNOWN";
199
200 pr_debug("hvsi%i: state = %s\n", hp->index, name);
201#endif /* DEBUG */
202}
203
204static inline void __set_state(struct hvsi_struct *hp, int state)
205{
206 hp->state = state;
207 print_state(hp);
208 wake_up_all(&hp->stateq);
209}
210
211static inline void set_state(struct hvsi_struct *hp, int state)
212{
213 unsigned long flags;
214
215 spin_lock_irqsave(&hp->lock, flags);
216 __set_state(hp, state);
217 spin_unlock_irqrestore(&hp->lock, flags);
218}
219
220static inline int len_packet(const uint8_t *packet)
221{
222 return (int)((struct hvsi_header *)packet)->len;
223}
224
225static inline int is_header(const uint8_t *packet)
226{
227 struct hvsi_header *header = (struct hvsi_header *)packet;
228 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
229}
230
231static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
232{
233 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
234 return 0; /* don't even have the packet header */
235
236 if (hp->inbuf_end < (packet + len_packet(packet)))
237 return 0; /* don't have the rest of the packet */
238
239 return 1;
240}
241
242/* shift remaining bytes in packetbuf down */
243static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
244{
245 int remaining = (int)(hp->inbuf_end - read_to);
246
247 pr_debug("%s: %i chars remain\n", __func__, remaining);
248
249 if (read_to != hp->inbuf)
250 memmove(hp->inbuf, read_to, remaining);
251
252 hp->inbuf_end = hp->inbuf + remaining;
253}
254
255#ifdef DEBUG
256#define dbg_dump_packet(packet) dump_packet(packet)
257#define dbg_dump_hex(data, len) dump_hex(data, len)
258#else
259#define dbg_dump_packet(packet) do { } while (0)
260#define dbg_dump_hex(data, len) do { } while (0)
261#endif
262
263static void dump_hex(const uint8_t *data, int len)
264{
265 int i;
266
267 printk(" ");
268 for (i=0; i < len; i++)
269 printk("%.2x", data[i]);
270
271 printk("\n ");
272 for (i=0; i < len; i++) {
273 if (isprint(data[i]))
274 printk("%c", data[i]);
275 else
276 printk(".");
277 }
278 printk("\n");
279}
280
281static void dump_packet(uint8_t *packet)
282{
283 struct hvsi_header *header = (struct hvsi_header *)packet;
284
285 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
286 header->seqno);
287
288 dump_hex(packet, header->len);
289}
290
291static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
292{
293 unsigned long got;
294
295 got = hvc_get_chars(hp->vtermno, buf, count);
296
297 return got;
298}
299
300static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
301 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
302{
303 struct hvsi_control *header = (struct hvsi_control *)packet;
304
305 switch (header->verb) {
306 case VSV_MODEM_CTL_UPDATE:
307 if ((header->word & HVSI_TSCD) == 0) {
308 /* CD went away; no more connection */
309 pr_debug("hvsi%i: CD dropped\n", hp->index);
310 hp->mctrl &= TIOCM_CD;
311 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
312 if (hp->tty && !(hp->tty->flags & CLOCAL))
313 *to_hangup = hp->tty;
314 }
315 break;
316 case VSV_CLOSE_PROTOCOL:
317 pr_debug("hvsi%i: service processor came back\n", hp->index);
318 if (hp->state != HVSI_CLOSED) {
319 *to_handshake = hp;
320 }
321 break;
322 default:
323 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
324 hp->index);
325 dump_packet(packet);
326 break;
327 }
328}
329
330static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
331{
332 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
333
334 switch (hp->state) {
335 case HVSI_WAIT_FOR_VER_RESPONSE:
336 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
337 break;
338 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
339 hp->mctrl = 0;
340 if (resp->u.mctrl_word & HVSI_TSDTR)
341 hp->mctrl |= TIOCM_DTR;
342 if (resp->u.mctrl_word & HVSI_TSCD)
343 hp->mctrl |= TIOCM_CD;
344 __set_state(hp, HVSI_OPEN);
345 break;
346 default:
347 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
348 dump_packet(packet);
349 break;
350 }
351}
352
353/* respond to service processor's version query */
354static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
355{
356 struct hvsi_query_response packet __ALIGNED__;
357 int wrote;
358
359 packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
360 packet.len = sizeof(struct hvsi_query_response);
361 packet.seqno = atomic_inc_return(&hp->seqno);
362 packet.verb = VSV_SEND_VERSION_NUMBER;
363 packet.u.version = HVSI_VERSION;
364 packet.query_seqno = query_seqno+1;
365
366 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
367 dbg_dump_hex((uint8_t*)&packet, packet.len);
368
369 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
370 if (wrote != packet.len) {
371 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
372 hp->index);
373 return -EIO;
374 }
375
376 return 0;
377}
378
379static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
380{
381 struct hvsi_query *query = (struct hvsi_query *)packet;
382
383 switch (hp->state) {
384 case HVSI_WAIT_FOR_VER_QUERY:
385 hvsi_version_respond(hp, query->seqno);
386 __set_state(hp, HVSI_OPEN);
387 break;
388 default:
389 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
390 dump_packet(packet);
391 break;
392 }
393}
394
395static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
396{
397 int i;
398
399 for (i=0; i < len; i++) {
400 char c = buf[i];
401#ifdef CONFIG_MAGIC_SYSRQ
402 if (c == '\0') {
403 hp->sysrq = 1;
404 continue;
405 } else if (hp->sysrq) {
406 handle_sysrq(c);
407 hp->sysrq = 0;
408 continue;
409 }
410#endif /* CONFIG_MAGIC_SYSRQ */
411 tty_insert_flip_char(hp->tty, c, 0);
412 }
413}
414
415/*
416 * We could get 252 bytes of data at once here. But the tty layer only
417 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
418 * it. Accordingly we won't send more than 128 bytes at a time to the flip
419 * buffer, which will give the tty buffer a chance to throttle us. Should the
420 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
421 * revisited.
422 */
423#define TTY_THRESHOLD_THROTTLE 128
424static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
425 const uint8_t *packet)
426{
427 const struct hvsi_header *header = (const struct hvsi_header *)packet;
428 const uint8_t *data = packet + sizeof(struct hvsi_header);
429 int datalen = header->len - sizeof(struct hvsi_header);
430 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
431
432 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
433
434 if (datalen == 0)
435 return NULL;
436
437 if (overflow > 0) {
438 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
439 datalen = TTY_THRESHOLD_THROTTLE;
440 }
441
442 hvsi_insert_chars(hp, data, datalen);
443
444 if (overflow > 0) {
445 /*
446 * we still have more data to deliver, so we need to save off the
447 * overflow and send it later
448 */
449 pr_debug("%s: deferring overflow\n", __func__);
450 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
451 hp->n_throttle = overflow;
452 }
453
454 return hp->tty;
455}
456
457/*
458 * Returns true/false indicating data successfully read from hypervisor.
459 * Used both to get packets for tty connections and to advance the state
460 * machine during console handshaking (in which case tty = NULL and we ignore
461 * incoming data).
462 */
463static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
464 struct tty_struct **hangup, struct hvsi_struct **handshake)
465{
466 uint8_t *packet = hp->inbuf;
467 int chunklen;
468
469 *flip = NULL;
470 *hangup = NULL;
471 *handshake = NULL;
472
473 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
474 if (chunklen == 0) {
475 pr_debug("%s: 0-length read\n", __func__);
476 return 0;
477 }
478
479 pr_debug("%s: got %i bytes\n", __func__, chunklen);
480 dbg_dump_hex(hp->inbuf_end, chunklen);
481
482 hp->inbuf_end += chunklen;
483
484 /* handle all completed packets */
485 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
486 struct hvsi_header *header = (struct hvsi_header *)packet;
487
488 if (!is_header(packet)) {
489 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
490 /* skip bytes until we find a header or run out of data */
491 while ((packet < hp->inbuf_end) && (!is_header(packet)))
492 packet++;
493 continue;
494 }
495
496 pr_debug("%s: handling %i-byte packet\n", __func__,
497 len_packet(packet));
498 dbg_dump_packet(packet);
499
500 switch (header->type) {
501 case VS_DATA_PACKET_HEADER:
502 if (!is_open(hp))
503 break;
504 if (hp->tty == NULL)
505 break; /* no tty buffer to put data in */
506 *flip = hvsi_recv_data(hp, packet);
507 break;
508 case VS_CONTROL_PACKET_HEADER:
509 hvsi_recv_control(hp, packet, hangup, handshake);
510 break;
511 case VS_QUERY_RESPONSE_PACKET_HEADER:
512 hvsi_recv_response(hp, packet);
513 break;
514 case VS_QUERY_PACKET_HEADER:
515 hvsi_recv_query(hp, packet);
516 break;
517 default:
518 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
519 hp->index, header->type);
520 dump_packet(packet);
521 break;
522 }
523
524 packet += len_packet(packet);
525
526 if (*hangup || *handshake) {
527 pr_debug("%s: hangup or handshake\n", __func__);
528 /*
529 * we need to send the hangup now before receiving any more data.
530 * If we get "data, hangup, data", we can't deliver the second
531 * data before the hangup.
532 */
533 break;
534 }
535 }
536
537 compact_inbuf(hp, packet);
538
539 return 1;
540}
541
542static void hvsi_send_overflow(struct hvsi_struct *hp)
543{
544 pr_debug("%s: delivering %i bytes overflow\n", __func__,
545 hp->n_throttle);
546
547 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
548 hp->n_throttle = 0;
549}
550
551/*
552 * must get all pending data because we only get an irq on empty->non-empty
553 * transition
554 */
555static irqreturn_t hvsi_interrupt(int irq, void *arg)
556{
557 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
558 struct tty_struct *flip;
559 struct tty_struct *hangup;
560 struct hvsi_struct *handshake;
561 unsigned long flags;
562 int again = 1;
563
564 pr_debug("%s\n", __func__);
565
566 while (again) {
567 spin_lock_irqsave(&hp->lock, flags);
568 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
569 spin_unlock_irqrestore(&hp->lock, flags);
570
571 /*
572 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
573 * spinlock. But we also have to keep going until we've read all the
574 * available data.
575 */
576
577 if (flip) {
578 /* there was data put in the tty flip buffer */
579 tty_flip_buffer_push(flip);
580 flip = NULL;
581 }
582
583 if (hangup) {
584 tty_hangup(hangup);
585 }
586
587 if (handshake) {
588 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
589 schedule_work(&handshake->handshaker);
590 }
591 }
592
593 spin_lock_irqsave(&hp->lock, flags);
594 if (hp->tty && hp->n_throttle
595 && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
596 /* we weren't hung up and we weren't throttled, so we can deliver the
597 * rest now */
598 flip = hp->tty;
599 hvsi_send_overflow(hp);
600 }
601 spin_unlock_irqrestore(&hp->lock, flags);
602
603 if (flip) {
604 tty_flip_buffer_push(flip);
605 }
606
607 return IRQ_HANDLED;
608}
609
610/* for boot console, before the irq handler is running */
611static int __init poll_for_state(struct hvsi_struct *hp, int state)
612{
613 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
614
615 for (;;) {
616 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
617
618 if (hp->state == state)
619 return 0;
620
621 mdelay(5);
622 if (time_after(jiffies, end_jiffies))
623 return -EIO;
624 }
625}
626
627/* wait for irq handler to change our state */
628static int wait_for_state(struct hvsi_struct *hp, int state)
629{
630 int ret = 0;
631
632 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
633 ret = -EIO;
634
635 return ret;
636}
637
638static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
639{
640 struct hvsi_query packet __ALIGNED__;
641 int wrote;
642
643 packet.type = VS_QUERY_PACKET_HEADER;
644 packet.len = sizeof(struct hvsi_query);
645 packet.seqno = atomic_inc_return(&hp->seqno);
646 packet.verb = verb;
647
648 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
649 dbg_dump_hex((uint8_t*)&packet, packet.len);
650
651 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
652 if (wrote != packet.len) {
653 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
654 wrote);
655 return -EIO;
656 }
657
658 return 0;
659}
660
661static int hvsi_get_mctrl(struct hvsi_struct *hp)
662{
663 int ret;
664
665 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
666 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
667
668 ret = hvsi_wait(hp, HVSI_OPEN);
669 if (ret < 0) {
670 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
671 set_state(hp, HVSI_OPEN);
672 return ret;
673 }
674
675 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
676
677 return 0;
678}
679
680/* note that we can only set DTR */
681static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
682{
683 struct hvsi_control packet __ALIGNED__;
684 int wrote;
685
686 packet.type = VS_CONTROL_PACKET_HEADER,
687 packet.seqno = atomic_inc_return(&hp->seqno);
688 packet.len = sizeof(struct hvsi_control);
689 packet.verb = VSV_SET_MODEM_CTL;
690 packet.mask = HVSI_TSDTR;
691
692 if (mctrl & TIOCM_DTR)
693 packet.word = HVSI_TSDTR;
694
695 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
696 dbg_dump_hex((uint8_t*)&packet, packet.len);
697
698 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
699 if (wrote != packet.len) {
700 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
701 return -EIO;
702 }
703
704 return 0;
705}
706
707static void hvsi_drain_input(struct hvsi_struct *hp)
708{
709 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
710 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
711
712 while (time_before(end_jiffies, jiffies))
713 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
714 break;
715}
716
717static int hvsi_handshake(struct hvsi_struct *hp)
718{
719 int ret;
720
721 /*
722 * We could have a CLOSE or other data waiting for us before we even try
723 * to open; try to throw it all away so we don't get confused. (CLOSE
724 * is the first message sent up the pipe when the FSP comes online. We
725 * need to distinguish between "it came up a while ago and we're the first
726 * user" and "it was just reset before it saw our handshake packet".)
727 */
728 hvsi_drain_input(hp);
729
730 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
731 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
732 if (ret < 0) {
733 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
734 return ret;
735 }
736
737 ret = hvsi_wait(hp, HVSI_OPEN);
738 if (ret < 0)
739 return ret;
740
741 return 0;
742}
743
744static void hvsi_handshaker(struct work_struct *work)
745{
746 struct hvsi_struct *hp =
747 container_of(work, struct hvsi_struct, handshaker);
748
749 if (hvsi_handshake(hp) >= 0)
750 return;
751
752 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
753 if (is_console(hp)) {
754 /*
755 * ttys will re-attempt the handshake via hvsi_open, but
756 * the console will not.
757 */
758 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
759 }
760}
761
762static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
763{
764 struct hvsi_data packet __ALIGNED__;
765 int ret;
766
767 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
768
769 packet.type = VS_DATA_PACKET_HEADER;
770 packet.seqno = atomic_inc_return(&hp->seqno);
771 packet.len = count + sizeof(struct hvsi_header);
772 memcpy(&packet.data, buf, count);
773
774 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
775 if (ret == packet.len) {
776 /* return the number of chars written, not the packet length */
777 return count;
778 }
779 return ret; /* return any errors */
780}
781
782static void hvsi_close_protocol(struct hvsi_struct *hp)
783{
784 struct hvsi_control packet __ALIGNED__;
785
786 packet.type = VS_CONTROL_PACKET_HEADER;
787 packet.seqno = atomic_inc_return(&hp->seqno);
788 packet.len = 6;
789 packet.verb = VSV_CLOSE_PROTOCOL;
790
791 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
792 dbg_dump_hex((uint8_t*)&packet, packet.len);
793
794 hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
795}
796
797static int hvsi_open(struct tty_struct *tty, struct file *filp)
798{
799 struct hvsi_struct *hp;
800 unsigned long flags;
801 int line = tty->index;
802 int ret;
803
804 pr_debug("%s\n", __func__);
805
806 if (line < 0 || line >= hvsi_count)
807 return -ENODEV;
808 hp = &hvsi_ports[line];
809
810 tty->driver_data = hp;
811
812 mb();
813 if (hp->state == HVSI_FSP_DIED)
814 return -EIO;
815
816 spin_lock_irqsave(&hp->lock, flags);
817 hp->tty = tty;
818 hp->count++;
819 atomic_set(&hp->seqno, 0);
820 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
821 spin_unlock_irqrestore(&hp->lock, flags);
822
823 if (is_console(hp))
824 return 0; /* this has already been handshaked as the console */
825
826 ret = hvsi_handshake(hp);
827 if (ret < 0) {
828 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
829 return ret;
830 }
831
832 ret = hvsi_get_mctrl(hp);
833 if (ret < 0) {
834 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
835 return ret;
836 }
837
838 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
839 if (ret < 0) {
840 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
841 return ret;
842 }
843
844 return 0;
845}
846
847/* wait for hvsi_write_worker to empty hp->outbuf */
848static void hvsi_flush_output(struct hvsi_struct *hp)
849{
850 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
851
852 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
853 cancel_delayed_work_sync(&hp->writer);
854 flush_work_sync(&hp->handshaker);
855
856 /*
857 * it's also possible that our timeout expired and hvsi_write_worker
858 * didn't manage to push outbuf. poof.
859 */
860 hp->n_outbuf = 0;
861}
862
863static void hvsi_close(struct tty_struct *tty, struct file *filp)
864{
865 struct hvsi_struct *hp = tty->driver_data;
866 unsigned long flags;
867
868 pr_debug("%s\n", __func__);
869
870 if (tty_hung_up_p(filp))
871 return;
872
873 spin_lock_irqsave(&hp->lock, flags);
874
875 if (--hp->count == 0) {
876 hp->tty = NULL;
877 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
878
879 /* only close down connection if it is not the console */
880 if (!is_console(hp)) {
881 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
882 __set_state(hp, HVSI_CLOSED);
883 /*
884 * any data delivered to the tty layer after this will be
885 * discarded (except for XON/XOFF)
886 */
887 tty->closing = 1;
888
889 spin_unlock_irqrestore(&hp->lock, flags);
890
891 /* let any existing irq handlers finish. no more will start. */
892 synchronize_irq(hp->virq);
893
894 /* hvsi_write_worker will re-schedule until outbuf is empty. */
895 hvsi_flush_output(hp);
896
897 /* tell FSP to stop sending data */
898 hvsi_close_protocol(hp);
899
900 /*
901 * drain anything FSP is still in the middle of sending, and let
902 * hvsi_handshake drain the rest on the next open.
903 */
904 hvsi_drain_input(hp);
905
906 spin_lock_irqsave(&hp->lock, flags);
907 }
908 } else if (hp->count < 0)
909 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
910 hp - hvsi_ports, hp->count);
911
912 spin_unlock_irqrestore(&hp->lock, flags);
913}
914
915static void hvsi_hangup(struct tty_struct *tty)
916{
917 struct hvsi_struct *hp = tty->driver_data;
918 unsigned long flags;
919
920 pr_debug("%s\n", __func__);
921
922 spin_lock_irqsave(&hp->lock, flags);
923
924 hp->count = 0;
925 hp->n_outbuf = 0;
926 hp->tty = NULL;
927
928 spin_unlock_irqrestore(&hp->lock, flags);
929}
930
931/* called with hp->lock held */
932static void hvsi_push(struct hvsi_struct *hp)
933{
934 int n;
935
936 if (hp->n_outbuf <= 0)
937 return;
938
939 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
940 if (n > 0) {
941 /* success */
942 pr_debug("%s: wrote %i chars\n", __func__, n);
943 hp->n_outbuf = 0;
944 } else if (n == -EIO) {
945 __set_state(hp, HVSI_FSP_DIED);
946 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
947 }
948}
949
950/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
951static void hvsi_write_worker(struct work_struct *work)
952{
953 struct hvsi_struct *hp =
954 container_of(work, struct hvsi_struct, writer.work);
955 unsigned long flags;
956#ifdef DEBUG
957 static long start_j = 0;
958
959 if (start_j == 0)
960 start_j = jiffies;
961#endif /* DEBUG */
962
963 spin_lock_irqsave(&hp->lock, flags);
964
965 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
966
967 if (!is_open(hp)) {
968 /*
969 * We could have a non-open connection if the service processor died
970 * while we were busily scheduling ourselves. In that case, it could
971 * be minutes before the service processor comes back, so only try
972 * again once a second.
973 */
974 schedule_delayed_work(&hp->writer, HZ);
975 goto out;
976 }
977
978 hvsi_push(hp);
979 if (hp->n_outbuf > 0)
980 schedule_delayed_work(&hp->writer, 10);
981 else {
982#ifdef DEBUG
983 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
984 jiffies - start_j);
985 start_j = 0;
986#endif /* DEBUG */
987 wake_up_all(&hp->emptyq);
988 tty_wakeup(hp->tty);
989 }
990
991out:
992 spin_unlock_irqrestore(&hp->lock, flags);
993}
994
995static int hvsi_write_room(struct tty_struct *tty)
996{
997 struct hvsi_struct *hp = tty->driver_data;
998
999 return N_OUTBUF - hp->n_outbuf;
1000}
1001
1002static int hvsi_chars_in_buffer(struct tty_struct *tty)
1003{
1004 struct hvsi_struct *hp = tty->driver_data;
1005
1006 return hp->n_outbuf;
1007}
1008
1009static int hvsi_write(struct tty_struct *tty,
1010 const unsigned char *buf, int count)
1011{
1012 struct hvsi_struct *hp = tty->driver_data;
1013 const char *source = buf;
1014 unsigned long flags;
1015 int total = 0;
1016 int origcount = count;
1017
1018 spin_lock_irqsave(&hp->lock, flags);
1019
1020 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
1021
1022 if (!is_open(hp)) {
1023 /* we're either closing or not yet open; don't accept data */
1024 pr_debug("%s: not open\n", __func__);
1025 goto out;
1026 }
1027
1028 /*
1029 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
1030 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
1031 * will see there is no room in outbuf and return.
1032 */
1033 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
1034 int chunksize = min(count, hvsi_write_room(hp->tty));
1035
1036 BUG_ON(hp->n_outbuf < 0);
1037 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
1038 hp->n_outbuf += chunksize;
1039
1040 total += chunksize;
1041 source += chunksize;
1042 count -= chunksize;
1043 hvsi_push(hp);
1044 }
1045
1046 if (hp->n_outbuf > 0) {
1047 /*
1048 * we weren't able to write it all to the hypervisor.
1049 * schedule another push attempt.
1050 */
1051 schedule_delayed_work(&hp->writer, 10);
1052 }
1053
1054out:
1055 spin_unlock_irqrestore(&hp->lock, flags);
1056
1057 if (total != origcount)
1058 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
1059 total);
1060
1061 return total;
1062}
1063
1064/*
1065 * I have never seen throttle or unthrottle called, so this little throttle
1066 * buffering scheme may or may not work.
1067 */
1068static void hvsi_throttle(struct tty_struct *tty)
1069{
1070 struct hvsi_struct *hp = tty->driver_data;
1071
1072 pr_debug("%s\n", __func__);
1073
1074 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1075}
1076
1077static void hvsi_unthrottle(struct tty_struct *tty)
1078{
1079 struct hvsi_struct *hp = tty->driver_data;
1080 unsigned long flags;
1081 int shouldflip = 0;
1082
1083 pr_debug("%s\n", __func__);
1084
1085 spin_lock_irqsave(&hp->lock, flags);
1086 if (hp->n_throttle) {
1087 hvsi_send_overflow(hp);
1088 shouldflip = 1;
1089 }
1090 spin_unlock_irqrestore(&hp->lock, flags);
1091
1092 if (shouldflip)
1093 tty_flip_buffer_push(hp->tty);
1094
1095 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1096}
1097
1098static int hvsi_tiocmget(struct tty_struct *tty)
1099{
1100 struct hvsi_struct *hp = tty->driver_data;
1101
1102 hvsi_get_mctrl(hp);
1103 return hp->mctrl;
1104}
1105
1106static int hvsi_tiocmset(struct tty_struct *tty,
1107 unsigned int set, unsigned int clear)
1108{
1109 struct hvsi_struct *hp = tty->driver_data;
1110 unsigned long flags;
1111 uint16_t new_mctrl;
1112
1113 /* we can only alter DTR */
1114 clear &= TIOCM_DTR;
1115 set &= TIOCM_DTR;
1116
1117 spin_lock_irqsave(&hp->lock, flags);
1118
1119 new_mctrl = (hp->mctrl & ~clear) | set;
1120
1121 if (hp->mctrl != new_mctrl) {
1122 hvsi_set_mctrl(hp, new_mctrl);
1123 hp->mctrl = new_mctrl;
1124 }
1125 spin_unlock_irqrestore(&hp->lock, flags);
1126
1127 return 0;
1128}
1129
1130
1131static const struct tty_operations hvsi_ops = {
1132 .open = hvsi_open,
1133 .close = hvsi_close,
1134 .write = hvsi_write,
1135 .hangup = hvsi_hangup,
1136 .write_room = hvsi_write_room,
1137 .chars_in_buffer = hvsi_chars_in_buffer,
1138 .throttle = hvsi_throttle,
1139 .unthrottle = hvsi_unthrottle,
1140 .tiocmget = hvsi_tiocmget,
1141 .tiocmset = hvsi_tiocmset,
1142};
1143
1144static int __init hvsi_init(void)
1145{
1146 int i;
1147
1148 hvsi_driver = alloc_tty_driver(hvsi_count);
1149 if (!hvsi_driver)
1150 return -ENOMEM;
1151
1152 hvsi_driver->owner = THIS_MODULE;
1153 hvsi_driver->driver_name = "hvsi";
1154 hvsi_driver->name = "hvsi";
1155 hvsi_driver->major = HVSI_MAJOR;
1156 hvsi_driver->minor_start = HVSI_MINOR;
1157 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1158 hvsi_driver->init_termios = tty_std_termios;
1159 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1160 hvsi_driver->init_termios.c_ispeed = 9600;
1161 hvsi_driver->init_termios.c_ospeed = 9600;
1162 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1163 tty_set_operations(hvsi_driver, &hvsi_ops);
1164
1165 for (i=0; i < hvsi_count; i++) {
1166 struct hvsi_struct *hp = &hvsi_ports[i];
1167 int ret = 1;
1168
1169 ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
1170 if (ret)
1171 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1172 hp->virq, ret);
1173 }
1174 hvsi_wait = wait_for_state; /* irqs active now */
1175
1176 if (tty_register_driver(hvsi_driver))
1177 panic("Couldn't register hvsi console driver\n");
1178
1179 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1180
1181 return 0;
1182}
1183device_initcall(hvsi_init);
1184
1185/***** console (not tty) code: *****/
1186
1187static void hvsi_console_print(struct console *console, const char *buf,
1188 unsigned int count)
1189{
1190 struct hvsi_struct *hp = &hvsi_ports[console->index];
1191 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1192 unsigned int i = 0, n = 0;
1193 int ret, donecr = 0;
1194
1195 mb();
1196 if (!is_open(hp))
1197 return;
1198
1199 /*
1200 * ugh, we have to translate LF -> CRLF ourselves, in place.
1201 * copied from hvc_console.c:
1202 */
1203 while (count > 0 || i > 0) {
1204 if (count > 0 && i < sizeof(c)) {
1205 if (buf[n] == '\n' && !donecr) {
1206 c[i++] = '\r';
1207 donecr = 1;
1208 } else {
1209 c[i++] = buf[n++];
1210 donecr = 0;
1211 --count;
1212 }
1213 } else {
1214 ret = hvsi_put_chars(hp, c, i);
1215 if (ret < 0)
1216 i = 0;
1217 i -= ret;
1218 }
1219 }
1220}
1221
1222static struct tty_driver *hvsi_console_device(struct console *console,
1223 int *index)
1224{
1225 *index = console->index;
1226 return hvsi_driver;
1227}
1228
1229static int __init hvsi_console_setup(struct console *console, char *options)
1230{
1231 struct hvsi_struct *hp;
1232 int ret;
1233
1234 if (console->index < 0 || console->index >= hvsi_count)
1235 return -1;
1236 hp = &hvsi_ports[console->index];
1237
1238 /* give the FSP a chance to change the baud rate when we re-open */
1239 hvsi_close_protocol(hp);
1240
1241 ret = hvsi_handshake(hp);
1242 if (ret < 0)
1243 return ret;
1244
1245 ret = hvsi_get_mctrl(hp);
1246 if (ret < 0)
1247 return ret;
1248
1249 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1250 if (ret < 0)
1251 return ret;
1252
1253 hp->flags |= HVSI_CONSOLE;
1254
1255 return 0;
1256}
1257
1258static struct console hvsi_console = {
1259 .name = "hvsi",
1260 .write = hvsi_console_print,
1261 .device = hvsi_console_device,
1262 .setup = hvsi_console_setup,
1263 .flags = CON_PRINTBUFFER,
1264 .index = -1,
1265};
1266
1267static int __init hvsi_console_init(void)
1268{
1269 struct device_node *vty;
1270
1271 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1272
1273 /* search device tree for vty nodes */
1274 for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1275 vty != NULL;
1276 vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1277 struct hvsi_struct *hp;
1278 const uint32_t *vtermno, *irq;
1279
1280 vtermno = of_get_property(vty, "reg", NULL);
1281 irq = of_get_property(vty, "interrupts", NULL);
1282 if (!vtermno || !irq)
1283 continue;
1284
1285 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1286 of_node_put(vty);
1287 break;
1288 }
1289
1290 hp = &hvsi_ports[hvsi_count];
1291 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1292 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1293 init_waitqueue_head(&hp->emptyq);
1294 init_waitqueue_head(&hp->stateq);
1295 spin_lock_init(&hp->lock);
1296 hp->index = hvsi_count;
1297 hp->inbuf_end = hp->inbuf;
1298 hp->state = HVSI_CLOSED;
1299 hp->vtermno = *vtermno;
1300 hp->virq = irq_create_mapping(NULL, irq[0]);
1301 if (hp->virq == NO_IRQ) {
1302 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1303 __func__, irq[0]);
1304 continue;
1305 }
1306
1307 hvsi_count++;
1308 }
1309
1310 if (hvsi_count)
1311 register_console(&hvsi_console);
1312 return 0;
1313}
1314console_initcall(hvsi_console_init);
diff --git a/drivers/tty/ipwireless/Makefile b/drivers/tty/ipwireless/Makefile
new file mode 100644
index 000000000000..fe2e1730986b
--- /dev/null
+++ b/drivers/tty/ipwireless/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for the IPWireless driver
3#
4
5obj-$(CONFIG_IPWIRELESS) += ipwireless.o
6
7ipwireless-y := hardware.o main.o network.o tty.o
8
diff --git a/drivers/tty/ipwireless/hardware.c b/drivers/tty/ipwireless/hardware.c
new file mode 100644
index 000000000000..0aeb5a38d296
--- /dev/null
+++ b/drivers/tty/ipwireless/hardware.c
@@ -0,0 +1,1764 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23#include <linux/slab.h>
24
25#include "hardware.h"
26#include "setup_protocol.h"
27#include "network.h"
28#include "main.h"
29
30static void ipw_send_setup_packet(struct ipw_hardware *hw);
31static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
32 unsigned int address,
33 const unsigned char *data, int len,
34 int is_last);
35static void ipwireless_setup_timer(unsigned long data);
36static void handle_received_CTRL_packet(struct ipw_hardware *hw,
37 unsigned int channel_idx, const unsigned char *data, int len);
38
39/*#define TIMING_DIAGNOSTICS*/
40
41#ifdef TIMING_DIAGNOSTICS
42
43static struct timing_stats {
44 unsigned long last_report_time;
45 unsigned long read_time;
46 unsigned long write_time;
47 unsigned long read_bytes;
48 unsigned long write_bytes;
49 unsigned long start_time;
50};
51
52static void start_timing(void)
53{
54 timing_stats.start_time = jiffies;
55}
56
57static void end_read_timing(unsigned length)
58{
59 timing_stats.read_time += (jiffies - start_time);
60 timing_stats.read_bytes += length + 2;
61 report_timing();
62}
63
64static void end_write_timing(unsigned length)
65{
66 timing_stats.write_time += (jiffies - start_time);
67 timing_stats.write_bytes += length + 2;
68 report_timing();
69}
70
71static void report_timing(void)
72{
73 unsigned long since = jiffies - timing_stats.last_report_time;
74
75 /* If it's been more than one second... */
76 if (since >= HZ) {
77 int first = (timing_stats.last_report_time == 0);
78
79 timing_stats.last_report_time = jiffies;
80 if (!first)
81 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
82 ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
83 jiffies_to_usecs(since),
84 timing_stats.read_bytes,
85 jiffies_to_usecs(timing_stats.read_time),
86 timing_stats.write_bytes,
87 jiffies_to_usecs(timing_stats.write_time));
88
89 timing_stats.read_time = 0;
90 timing_stats.write_time = 0;
91 timing_stats.read_bytes = 0;
92 timing_stats.write_bytes = 0;
93 }
94}
95#else
96static void start_timing(void) { }
97static void end_read_timing(unsigned length) { }
98static void end_write_timing(unsigned length) { }
99#endif
100
101/* Imported IPW definitions */
102
103#define LL_MTU_V1 318
104#define LL_MTU_V2 250
105#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
106
107#define PRIO_DATA 2
108#define PRIO_CTRL 1
109#define PRIO_SETUP 0
110
111/* Addresses */
112#define ADDR_SETUP_PROT 0
113
114/* Protocol ids */
115enum {
116 /* Identifier for the Com Data protocol */
117 TL_PROTOCOLID_COM_DATA = 0,
118
119 /* Identifier for the Com Control protocol */
120 TL_PROTOCOLID_COM_CTRL = 1,
121
122 /* Identifier for the Setup protocol */
123 TL_PROTOCOLID_SETUP = 2
124};
125
126/* Number of bytes in NL packet header (cannot do
127 * sizeof(nl_packet_header) since it's a bitfield) */
128#define NL_FIRST_PACKET_HEADER_SIZE 3
129
130/* Number of bytes in NL packet header (cannot do
131 * sizeof(nl_packet_header) since it's a bitfield) */
132#define NL_FOLLOWING_PACKET_HEADER_SIZE 1
133
134struct nl_first_packet_header {
135 unsigned char protocol:3;
136 unsigned char address:3;
137 unsigned char packet_rank:2;
138 unsigned char length_lsb;
139 unsigned char length_msb;
140};
141
142struct nl_packet_header {
143 unsigned char protocol:3;
144 unsigned char address:3;
145 unsigned char packet_rank:2;
146};
147
148/* Value of 'packet_rank' above */
149#define NL_INTERMEDIATE_PACKET 0x0
150#define NL_LAST_PACKET 0x1
151#define NL_FIRST_PACKET 0x2
152
153union nl_packet {
154 /* Network packet header of the first packet (a special case) */
155 struct nl_first_packet_header hdr_first;
156 /* Network packet header of the following packets (if any) */
157 struct nl_packet_header hdr;
158 /* Complete network packet (header + data) */
159 unsigned char rawpkt[LL_MTU_MAX];
160} __attribute__ ((__packed__));
161
162#define HW_VERSION_UNKNOWN -1
163#define HW_VERSION_1 1
164#define HW_VERSION_2 2
165
166/* IPW I/O ports */
167#define IOIER 0x00 /* Interrupt Enable Register */
168#define IOIR 0x02 /* Interrupt Source/ACK register */
169#define IODCR 0x04 /* Data Control Register */
170#define IODRR 0x06 /* Data Read Register */
171#define IODWR 0x08 /* Data Write Register */
172#define IOESR 0x0A /* Embedded Driver Status Register */
173#define IORXR 0x0C /* Rx Fifo Register (Host to Embedded) */
174#define IOTXR 0x0E /* Tx Fifo Register (Embedded to Host) */
175
176/* I/O ports and bit definitions for version 1 of the hardware */
177
178/* IER bits*/
179#define IER_RXENABLED 0x1
180#define IER_TXENABLED 0x2
181
182/* ISR bits */
183#define IR_RXINTR 0x1
184#define IR_TXINTR 0x2
185
186/* DCR bits */
187#define DCR_RXDONE 0x1
188#define DCR_TXDONE 0x2
189#define DCR_RXRESET 0x4
190#define DCR_TXRESET 0x8
191
192/* I/O ports and bit definitions for version 2 of the hardware */
193
194struct MEMCCR {
195 unsigned short reg_config_option; /* PCCOR: Configuration Option Register */
196 unsigned short reg_config_and_status; /* PCCSR: Configuration and Status Register */
197 unsigned short reg_pin_replacement; /* PCPRR: Pin Replacemant Register */
198 unsigned short reg_socket_and_copy; /* PCSCR: Socket and Copy Register */
199 unsigned short reg_ext_status; /* PCESR: Extendend Status Register */
200 unsigned short reg_io_base; /* PCIOB: I/O Base Register */
201};
202
203struct MEMINFREG {
204 unsigned short memreg_tx_old; /* TX Register (R/W) */
205 unsigned short pad1;
206 unsigned short memreg_rx_done; /* RXDone Register (R/W) */
207 unsigned short pad2;
208 unsigned short memreg_rx; /* RX Register (R/W) */
209 unsigned short pad3;
210 unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */
211 unsigned short pad4;
212 unsigned long memreg_card_present;/* Mask for Host to check (R) for
213 * CARD_PRESENT_VALUE */
214 unsigned short memreg_tx_new; /* TX2 (new) Register (R/W) */
215};
216
217#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
218
219#define MEMTX_TX 0x0001
220#define MEMRX_RX 0x0001
221#define MEMRX_RX_DONE 0x0001
222#define MEMRX_PCINTACKK 0x0001
223
224#define NL_NUM_OF_PRIORITIES 3
225#define NL_NUM_OF_PROTOCOLS 3
226#define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS
227
228struct ipw_hardware {
229 unsigned int base_port;
230 short hw_version;
231 unsigned short ll_mtu;
232 spinlock_t lock;
233
234 int initializing;
235 int init_loops;
236 struct timer_list setup_timer;
237
238 /* Flag if hw is ready to send next packet */
239 int tx_ready;
240 /* Count of pending packets to be sent */
241 int tx_queued;
242 struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
243
244 int rx_bytes_queued;
245 struct list_head rx_queue;
246 /* Pool of rx_packet structures that are not currently used. */
247 struct list_head rx_pool;
248 int rx_pool_size;
249 /* True if reception of data is blocked while userspace processes it. */
250 int blocking_rx;
251 /* True if there is RX data ready on the hardware. */
252 int rx_ready;
253 unsigned short last_memtx_serial;
254 /*
255 * Newer versions of the V2 card firmware send serial numbers in the
256 * MemTX register. 'serial_number_detected' is set true when we detect
257 * a non-zero serial number (indicating the new firmware). Thereafter,
258 * the driver can safely ignore the Timer Recovery re-sends to avoid
259 * out-of-sync problems.
260 */
261 int serial_number_detected;
262 struct work_struct work_rx;
263
264 /* True if we are to send the set-up data to the hardware. */
265 int to_setup;
266
267 /* Card has been removed */
268 int removed;
269 /* Saved irq value when we disable the interrupt. */
270 int irq;
271 /* True if this driver is shutting down. */
272 int shutting_down;
273 /* Modem control lines */
274 unsigned int control_lines[NL_NUM_OF_ADDRESSES];
275 struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
276
277 struct tasklet_struct tasklet;
278
279 /* The handle for the network layer, for the sending of events to it. */
280 struct ipw_network *network;
281 struct MEMINFREG __iomem *memory_info_regs;
282 struct MEMCCR __iomem *memregs_CCR;
283 void (*reboot_callback) (void *data);
284 void *reboot_callback_data;
285
286 unsigned short __iomem *memreg_tx;
287};
288
289/*
290 * Packet info structure for tx packets.
291 * Note: not all the fields defined here are required for all protocols
292 */
293struct ipw_tx_packet {
294 struct list_head queue;
295 /* channel idx + 1 */
296 unsigned char dest_addr;
297 /* SETUP, CTRL or DATA */
298 unsigned char protocol;
299 /* Length of data block, which starts at the end of this structure */
300 unsigned short length;
301 /* Sending state */
302 /* Offset of where we've sent up to so far */
303 unsigned long offset;
304 /* Count of packet fragments, starting at 0 */
305 int fragment_count;
306
307 /* Called after packet is sent and before is freed */
308 void (*packet_callback) (void *cb_data, unsigned int packet_length);
309 void *callback_data;
310};
311
312/* Signals from DTE */
313#define COMCTRL_RTS 0
314#define COMCTRL_DTR 1
315
316/* Signals from DCE */
317#define COMCTRL_CTS 2
318#define COMCTRL_DCD 3
319#define COMCTRL_DSR 4
320#define COMCTRL_RI 5
321
322struct ipw_control_packet_body {
323 /* DTE signal or DCE signal */
324 unsigned char sig_no;
325 /* 0: set signal, 1: clear signal */
326 unsigned char value;
327} __attribute__ ((__packed__));
328
329struct ipw_control_packet {
330 struct ipw_tx_packet header;
331 struct ipw_control_packet_body body;
332};
333
334struct ipw_rx_packet {
335 struct list_head queue;
336 unsigned int capacity;
337 unsigned int length;
338 unsigned int protocol;
339 unsigned int channel_idx;
340};
341
342static char *data_type(const unsigned char *buf, unsigned length)
343{
344 struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
345
346 if (length == 0)
347 return " ";
348
349 if (hdr->packet_rank & NL_FIRST_PACKET) {
350 switch (hdr->protocol) {
351 case TL_PROTOCOLID_COM_DATA: return "DATA ";
352 case TL_PROTOCOLID_COM_CTRL: return "CTRL ";
353 case TL_PROTOCOLID_SETUP: return "SETUP";
354 default: return "???? ";
355 }
356 } else
357 return " ";
358}
359
360#define DUMP_MAX_BYTES 64
361
362static void dump_data_bytes(const char *type, const unsigned char *data,
363 unsigned length)
364{
365 char prefix[56];
366
367 sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
368 type, data_type(data, length));
369 print_hex_dump_bytes(prefix, 0, (void *)data,
370 length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
371}
372
373static void swap_packet_bitfield_to_le(unsigned char *data)
374{
375#ifdef __BIG_ENDIAN_BITFIELD
376 unsigned char tmp = *data, ret = 0;
377
378 /*
379 * transform bits from aa.bbb.ccc to ccc.bbb.aa
380 */
381 ret |= tmp & 0xc0 >> 6;
382 ret |= tmp & 0x38 >> 1;
383 ret |= tmp & 0x07 << 5;
384 *data = ret & 0xff;
385#endif
386}
387
388static void swap_packet_bitfield_from_le(unsigned char *data)
389{
390#ifdef __BIG_ENDIAN_BITFIELD
391 unsigned char tmp = *data, ret = 0;
392
393 /*
394 * transform bits from ccc.bbb.aa to aa.bbb.ccc
395 */
396 ret |= tmp & 0xe0 >> 5;
397 ret |= tmp & 0x1c << 1;
398 ret |= tmp & 0x03 << 6;
399 *data = ret & 0xff;
400#endif
401}
402
403static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
404 unsigned length)
405{
406 unsigned i;
407 unsigned long flags;
408
409 start_timing();
410 BUG_ON(length > hw->ll_mtu);
411
412 if (ipwireless_debug)
413 dump_data_bytes("send", data, length);
414
415 spin_lock_irqsave(&hw->lock, flags);
416
417 hw->tx_ready = 0;
418 swap_packet_bitfield_to_le(data);
419
420 if (hw->hw_version == HW_VERSION_1) {
421 outw((unsigned short) length, hw->base_port + IODWR);
422
423 for (i = 0; i < length; i += 2) {
424 unsigned short d = data[i];
425 __le16 raw_data;
426
427 if (i + 1 < length)
428 d |= data[i + 1] << 8;
429 raw_data = cpu_to_le16(d);
430 outw(raw_data, hw->base_port + IODWR);
431 }
432
433 outw(DCR_TXDONE, hw->base_port + IODCR);
434 } else if (hw->hw_version == HW_VERSION_2) {
435 outw((unsigned short) length, hw->base_port);
436
437 for (i = 0; i < length; i += 2) {
438 unsigned short d = data[i];
439 __le16 raw_data;
440
441 if (i + 1 < length)
442 d |= data[i + 1] << 8;
443 raw_data = cpu_to_le16(d);
444 outw(raw_data, hw->base_port);
445 }
446 while ((i & 3) != 2) {
447 outw((unsigned short) 0xDEAD, hw->base_port);
448 i += 2;
449 }
450 writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
451 }
452
453 spin_unlock_irqrestore(&hw->lock, flags);
454
455 end_write_timing(length);
456}
457
458static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
459{
460 unsigned short fragment_data_len;
461 unsigned short data_left = packet->length - packet->offset;
462 unsigned short header_size;
463 union nl_packet pkt;
464
465 header_size =
466 (packet->fragment_count == 0)
467 ? NL_FIRST_PACKET_HEADER_SIZE
468 : NL_FOLLOWING_PACKET_HEADER_SIZE;
469 fragment_data_len = hw->ll_mtu - header_size;
470 if (data_left < fragment_data_len)
471 fragment_data_len = data_left;
472
473 /*
474 * hdr_first is now in machine bitfield order, which will be swapped
475 * to le just before it goes to hw
476 */
477 pkt.hdr_first.protocol = packet->protocol;
478 pkt.hdr_first.address = packet->dest_addr;
479 pkt.hdr_first.packet_rank = 0;
480
481 /* First packet? */
482 if (packet->fragment_count == 0) {
483 pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
484 pkt.hdr_first.length_lsb = (unsigned char) packet->length;
485 pkt.hdr_first.length_msb =
486 (unsigned char) (packet->length >> 8);
487 }
488
489 memcpy(pkt.rawpkt + header_size,
490 ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
491 packet->offset, fragment_data_len);
492 packet->offset += fragment_data_len;
493 packet->fragment_count++;
494
495 /* Last packet? (May also be first packet.) */
496 if (packet->offset == packet->length)
497 pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
498 do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
499
500 /* If this packet has unsent data, then re-queue it. */
501 if (packet->offset < packet->length) {
502 /*
503 * Re-queue it at the head of the highest priority queue so
504 * it goes before all other packets
505 */
506 unsigned long flags;
507
508 spin_lock_irqsave(&hw->lock, flags);
509 list_add(&packet->queue, &hw->tx_queue[0]);
510 hw->tx_queued++;
511 spin_unlock_irqrestore(&hw->lock, flags);
512 } else {
513 if (packet->packet_callback)
514 packet->packet_callback(packet->callback_data,
515 packet->length);
516 kfree(packet);
517 }
518}
519
520static void ipw_setup_hardware(struct ipw_hardware *hw)
521{
522 unsigned long flags;
523
524 spin_lock_irqsave(&hw->lock, flags);
525 if (hw->hw_version == HW_VERSION_1) {
526 /* Reset RX FIFO */
527 outw(DCR_RXRESET, hw->base_port + IODCR);
528 /* SB: Reset TX FIFO */
529 outw(DCR_TXRESET, hw->base_port + IODCR);
530
531 /* Enable TX and RX interrupts. */
532 outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
533 } else {
534 /*
535 * Set INTRACK bit (bit 0), which means we must explicitly
536 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
537 */
538 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
539
540 csr |= 1;
541 writew(csr, &hw->memregs_CCR->reg_config_and_status);
542 }
543 spin_unlock_irqrestore(&hw->lock, flags);
544}
545
546/*
547 * If 'packet' is NULL, then this function allocates a new packet, setting its
548 * length to 0 and ensuring it has the specified minimum amount of free space.
549 *
550 * If 'packet' is not NULL, then this function enlarges it if it doesn't
551 * have the specified minimum amount of free space.
552 *
553 */
554static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
555 struct ipw_rx_packet *packet,
556 int minimum_free_space)
557{
558
559 if (!packet) {
560 unsigned long flags;
561
562 spin_lock_irqsave(&hw->lock, flags);
563 if (!list_empty(&hw->rx_pool)) {
564 packet = list_first_entry(&hw->rx_pool,
565 struct ipw_rx_packet, queue);
566 hw->rx_pool_size--;
567 spin_unlock_irqrestore(&hw->lock, flags);
568 list_del(&packet->queue);
569 } else {
570 const int min_capacity =
571 ipwireless_ppp_mru(hw->network) + 2;
572 int new_capacity;
573
574 spin_unlock_irqrestore(&hw->lock, flags);
575 new_capacity =
576 (minimum_free_space > min_capacity
577 ? minimum_free_space
578 : min_capacity);
579 packet = kmalloc(sizeof(struct ipw_rx_packet)
580 + new_capacity, GFP_ATOMIC);
581 if (!packet)
582 return NULL;
583 packet->capacity = new_capacity;
584 }
585 packet->length = 0;
586 }
587
588 if (packet->length + minimum_free_space > packet->capacity) {
589 struct ipw_rx_packet *old_packet = packet;
590
591 packet = kmalloc(sizeof(struct ipw_rx_packet) +
592 old_packet->length + minimum_free_space,
593 GFP_ATOMIC);
594 if (!packet) {
595 kfree(old_packet);
596 return NULL;
597 }
598 memcpy(packet, old_packet,
599 sizeof(struct ipw_rx_packet)
600 + old_packet->length);
601 packet->capacity = old_packet->length + minimum_free_space;
602 kfree(old_packet);
603 }
604
605 return packet;
606}
607
608static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
609{
610 if (hw->rx_pool_size > 6)
611 kfree(packet);
612 else {
613 hw->rx_pool_size++;
614 list_add(&packet->queue, &hw->rx_pool);
615 }
616}
617
618static void queue_received_packet(struct ipw_hardware *hw,
619 unsigned int protocol,
620 unsigned int address,
621 const unsigned char *data, int length,
622 int is_last)
623{
624 unsigned int channel_idx = address - 1;
625 struct ipw_rx_packet *packet = NULL;
626 unsigned long flags;
627
628 /* Discard packet if channel index is out of range. */
629 if (channel_idx >= NL_NUM_OF_ADDRESSES) {
630 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
631 ": data packet has bad address %u\n", address);
632 return;
633 }
634
635 /*
636 * ->packet_assembler is safe to touch unlocked, this is the only place
637 */
638 if (protocol == TL_PROTOCOLID_COM_DATA) {
639 struct ipw_rx_packet **assem =
640 &hw->packet_assembler[channel_idx];
641
642 /*
643 * Create a new packet, or assembler already contains one
644 * enlarge it by 'length' bytes.
645 */
646 (*assem) = pool_allocate(hw, *assem, length);
647 if (!(*assem)) {
648 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
649 ": no memory for incomming data packet, dropped!\n");
650 return;
651 }
652 (*assem)->protocol = protocol;
653 (*assem)->channel_idx = channel_idx;
654
655 /* Append this packet data onto existing data. */
656 memcpy((unsigned char *)(*assem) +
657 sizeof(struct ipw_rx_packet)
658 + (*assem)->length, data, length);
659 (*assem)->length += length;
660 if (is_last) {
661 packet = *assem;
662 *assem = NULL;
663 /* Count queued DATA bytes only */
664 spin_lock_irqsave(&hw->lock, flags);
665 hw->rx_bytes_queued += packet->length;
666 spin_unlock_irqrestore(&hw->lock, flags);
667 }
668 } else {
669 /* If it's a CTRL packet, don't assemble, just queue it. */
670 packet = pool_allocate(hw, NULL, length);
671 if (!packet) {
672 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
673 ": no memory for incomming ctrl packet, dropped!\n");
674 return;
675 }
676 packet->protocol = protocol;
677 packet->channel_idx = channel_idx;
678 memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
679 data, length);
680 packet->length = length;
681 }
682
683 /*
684 * If this is the last packet, then send the assembled packet on to the
685 * network layer.
686 */
687 if (packet) {
688 spin_lock_irqsave(&hw->lock, flags);
689 list_add_tail(&packet->queue, &hw->rx_queue);
690 /* Block reception of incoming packets if queue is full. */
691 hw->blocking_rx =
692 (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
693
694 spin_unlock_irqrestore(&hw->lock, flags);
695 schedule_work(&hw->work_rx);
696 }
697}
698
699/*
700 * Workqueue callback
701 */
702static void ipw_receive_data_work(struct work_struct *work_rx)
703{
704 struct ipw_hardware *hw =
705 container_of(work_rx, struct ipw_hardware, work_rx);
706 unsigned long flags;
707
708 spin_lock_irqsave(&hw->lock, flags);
709 while (!list_empty(&hw->rx_queue)) {
710 struct ipw_rx_packet *packet =
711 list_first_entry(&hw->rx_queue,
712 struct ipw_rx_packet, queue);
713
714 if (hw->shutting_down)
715 break;
716 list_del(&packet->queue);
717
718 /*
719 * Note: ipwireless_network_packet_received must be called in a
720 * process context (i.e. via schedule_work) because the tty
721 * output code can sleep in the tty_flip_buffer_push call.
722 */
723 if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
724 if (hw->network != NULL) {
725 /* If the network hasn't been disconnected. */
726 spin_unlock_irqrestore(&hw->lock, flags);
727 /*
728 * This must run unlocked due to tty processing
729 * and mutex locking
730 */
731 ipwireless_network_packet_received(
732 hw->network,
733 packet->channel_idx,
734 (unsigned char *)packet
735 + sizeof(struct ipw_rx_packet),
736 packet->length);
737 spin_lock_irqsave(&hw->lock, flags);
738 }
739 /* Count queued DATA bytes only */
740 hw->rx_bytes_queued -= packet->length;
741 } else {
742 /*
743 * This is safe to be called locked, callchain does
744 * not block
745 */
746 handle_received_CTRL_packet(hw, packet->channel_idx,
747 (unsigned char *)packet
748 + sizeof(struct ipw_rx_packet),
749 packet->length);
750 }
751 pool_free(hw, packet);
752 /*
753 * Unblock reception of incoming packets if queue is no longer
754 * full.
755 */
756 hw->blocking_rx =
757 hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
758 if (hw->shutting_down)
759 break;
760 }
761 spin_unlock_irqrestore(&hw->lock, flags);
762}
763
764static void handle_received_CTRL_packet(struct ipw_hardware *hw,
765 unsigned int channel_idx,
766 const unsigned char *data, int len)
767{
768 const struct ipw_control_packet_body *body =
769 (const struct ipw_control_packet_body *) data;
770 unsigned int changed_mask;
771
772 if (len != sizeof(struct ipw_control_packet_body)) {
773 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
774 ": control packet was %d bytes - wrong size!\n",
775 len);
776 return;
777 }
778
779 switch (body->sig_no) {
780 case COMCTRL_CTS:
781 changed_mask = IPW_CONTROL_LINE_CTS;
782 break;
783 case COMCTRL_DCD:
784 changed_mask = IPW_CONTROL_LINE_DCD;
785 break;
786 case COMCTRL_DSR:
787 changed_mask = IPW_CONTROL_LINE_DSR;
788 break;
789 case COMCTRL_RI:
790 changed_mask = IPW_CONTROL_LINE_RI;
791 break;
792 default:
793 changed_mask = 0;
794 }
795
796 if (changed_mask != 0) {
797 if (body->value)
798 hw->control_lines[channel_idx] |= changed_mask;
799 else
800 hw->control_lines[channel_idx] &= ~changed_mask;
801 if (hw->network)
802 ipwireless_network_notify_control_line_change(
803 hw->network,
804 channel_idx,
805 hw->control_lines[channel_idx],
806 changed_mask);
807 }
808}
809
810static void handle_received_packet(struct ipw_hardware *hw,
811 const union nl_packet *packet,
812 unsigned short len)
813{
814 unsigned int protocol = packet->hdr.protocol;
815 unsigned int address = packet->hdr.address;
816 unsigned int header_length;
817 const unsigned char *data;
818 unsigned int data_len;
819 int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
820
821 if (packet->hdr.packet_rank & NL_FIRST_PACKET)
822 header_length = NL_FIRST_PACKET_HEADER_SIZE;
823 else
824 header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
825
826 data = packet->rawpkt + header_length;
827 data_len = len - header_length;
828 switch (protocol) {
829 case TL_PROTOCOLID_COM_DATA:
830 case TL_PROTOCOLID_COM_CTRL:
831 queue_received_packet(hw, protocol, address, data, data_len,
832 is_last);
833 break;
834 case TL_PROTOCOLID_SETUP:
835 handle_received_SETUP_packet(hw, address, data, data_len,
836 is_last);
837 break;
838 }
839}
840
841static void acknowledge_data_read(struct ipw_hardware *hw)
842{
843 if (hw->hw_version == HW_VERSION_1)
844 outw(DCR_RXDONE, hw->base_port + IODCR);
845 else
846 writew(MEMRX_PCINTACKK,
847 &hw->memory_info_regs->memreg_pc_interrupt_ack);
848}
849
850/*
851 * Retrieve a packet from the IPW hardware.
852 */
853static void do_receive_packet(struct ipw_hardware *hw)
854{
855 unsigned len;
856 unsigned i;
857 unsigned char pkt[LL_MTU_MAX];
858
859 start_timing();
860
861 if (hw->hw_version == HW_VERSION_1) {
862 len = inw(hw->base_port + IODRR);
863 if (len > hw->ll_mtu) {
864 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
865 ": received a packet of %u bytes - longer than the MTU!\n", len);
866 outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
867 return;
868 }
869
870 for (i = 0; i < len; i += 2) {
871 __le16 raw_data = inw(hw->base_port + IODRR);
872 unsigned short data = le16_to_cpu(raw_data);
873
874 pkt[i] = (unsigned char) data;
875 pkt[i + 1] = (unsigned char) (data >> 8);
876 }
877 } else {
878 len = inw(hw->base_port);
879 if (len > hw->ll_mtu) {
880 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
881 ": received a packet of %u bytes - longer than the MTU!\n", len);
882 writew(MEMRX_PCINTACKK,
883 &hw->memory_info_regs->memreg_pc_interrupt_ack);
884 return;
885 }
886
887 for (i = 0; i < len; i += 2) {
888 __le16 raw_data = inw(hw->base_port);
889 unsigned short data = le16_to_cpu(raw_data);
890
891 pkt[i] = (unsigned char) data;
892 pkt[i + 1] = (unsigned char) (data >> 8);
893 }
894
895 while ((i & 3) != 2) {
896 inw(hw->base_port);
897 i += 2;
898 }
899 }
900
901 acknowledge_data_read(hw);
902
903 swap_packet_bitfield_from_le(pkt);
904
905 if (ipwireless_debug)
906 dump_data_bytes("recv", pkt, len);
907
908 handle_received_packet(hw, (union nl_packet *) pkt, len);
909
910 end_read_timing(len);
911}
912
913static int get_current_packet_priority(struct ipw_hardware *hw)
914{
915 /*
916 * If we're initializing, don't send anything of higher priority than
917 * PRIO_SETUP. The network layer therefore need not care about
918 * hardware initialization - any of its stuff will simply be queued
919 * until setup is complete.
920 */
921 return (hw->to_setup || hw->initializing
922 ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
923}
924
925/*
926 * return 1 if something has been received from hw
927 */
928static int get_packets_from_hw(struct ipw_hardware *hw)
929{
930 int received = 0;
931 unsigned long flags;
932
933 spin_lock_irqsave(&hw->lock, flags);
934 while (hw->rx_ready && !hw->blocking_rx) {
935 received = 1;
936 hw->rx_ready--;
937 spin_unlock_irqrestore(&hw->lock, flags);
938
939 do_receive_packet(hw);
940
941 spin_lock_irqsave(&hw->lock, flags);
942 }
943 spin_unlock_irqrestore(&hw->lock, flags);
944
945 return received;
946}
947
948/*
949 * Send pending packet up to given priority, prioritize SETUP data until
950 * hardware is fully setup.
951 *
952 * return 1 if more packets can be sent
953 */
954static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
955{
956 int more_to_send = 0;
957 unsigned long flags;
958
959 spin_lock_irqsave(&hw->lock, flags);
960 if (hw->tx_queued && hw->tx_ready) {
961 int priority;
962 struct ipw_tx_packet *packet = NULL;
963
964 /* Pick a packet */
965 for (priority = 0; priority < priority_limit; priority++) {
966 if (!list_empty(&hw->tx_queue[priority])) {
967 packet = list_first_entry(
968 &hw->tx_queue[priority],
969 struct ipw_tx_packet,
970 queue);
971
972 hw->tx_queued--;
973 list_del(&packet->queue);
974
975 break;
976 }
977 }
978 if (!packet) {
979 hw->tx_queued = 0;
980 spin_unlock_irqrestore(&hw->lock, flags);
981 return 0;
982 }
983
984 spin_unlock_irqrestore(&hw->lock, flags);
985
986 /* Send */
987 do_send_packet(hw, packet);
988
989 /* Check if more to send */
990 spin_lock_irqsave(&hw->lock, flags);
991 for (priority = 0; priority < priority_limit; priority++)
992 if (!list_empty(&hw->tx_queue[priority])) {
993 more_to_send = 1;
994 break;
995 }
996
997 if (!more_to_send)
998 hw->tx_queued = 0;
999 }
1000 spin_unlock_irqrestore(&hw->lock, flags);
1001
1002 return more_to_send;
1003}
1004
1005/*
1006 * Send and receive all queued packets.
1007 */
1008static void ipwireless_do_tasklet(unsigned long hw_)
1009{
1010 struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1011 unsigned long flags;
1012
1013 spin_lock_irqsave(&hw->lock, flags);
1014 if (hw->shutting_down) {
1015 spin_unlock_irqrestore(&hw->lock, flags);
1016 return;
1017 }
1018
1019 if (hw->to_setup == 1) {
1020 /*
1021 * Initial setup data sent to hardware
1022 */
1023 hw->to_setup = 2;
1024 spin_unlock_irqrestore(&hw->lock, flags);
1025
1026 ipw_setup_hardware(hw);
1027 ipw_send_setup_packet(hw);
1028
1029 send_pending_packet(hw, PRIO_SETUP + 1);
1030 get_packets_from_hw(hw);
1031 } else {
1032 int priority_limit = get_current_packet_priority(hw);
1033 int again;
1034
1035 spin_unlock_irqrestore(&hw->lock, flags);
1036
1037 do {
1038 again = send_pending_packet(hw, priority_limit);
1039 again |= get_packets_from_hw(hw);
1040 } while (again);
1041 }
1042}
1043
1044/*
1045 * return true if the card is physically present.
1046 */
1047static int is_card_present(struct ipw_hardware *hw)
1048{
1049 if (hw->hw_version == HW_VERSION_1)
1050 return inw(hw->base_port + IOIR) != 0xFFFF;
1051 else
1052 return readl(&hw->memory_info_regs->memreg_card_present) ==
1053 CARD_PRESENT_VALUE;
1054}
1055
1056static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1057 struct ipw_hardware *hw)
1058{
1059 unsigned short irqn;
1060
1061 irqn = inw(hw->base_port + IOIR);
1062
1063 /* Check if card is present */
1064 if (irqn == 0xFFFF)
1065 return IRQ_NONE;
1066 else if (irqn != 0) {
1067 unsigned short ack = 0;
1068 unsigned long flags;
1069
1070 /* Transmit complete. */
1071 if (irqn & IR_TXINTR) {
1072 ack |= IR_TXINTR;
1073 spin_lock_irqsave(&hw->lock, flags);
1074 hw->tx_ready = 1;
1075 spin_unlock_irqrestore(&hw->lock, flags);
1076 }
1077 /* Received data */
1078 if (irqn & IR_RXINTR) {
1079 ack |= IR_RXINTR;
1080 spin_lock_irqsave(&hw->lock, flags);
1081 hw->rx_ready++;
1082 spin_unlock_irqrestore(&hw->lock, flags);
1083 }
1084 if (ack != 0) {
1085 outw(ack, hw->base_port + IOIR);
1086 tasklet_schedule(&hw->tasklet);
1087 }
1088 return IRQ_HANDLED;
1089 }
1090 return IRQ_NONE;
1091}
1092
1093static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1094{
1095 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1096
1097 csr &= 0xfffd;
1098 writew(csr, &hw->memregs_CCR->reg_config_and_status);
1099}
1100
1101static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1102 struct ipw_hardware *hw)
1103{
1104 int tx = 0;
1105 int rx = 0;
1106 int rx_repeat = 0;
1107 int try_mem_tx_old;
1108 unsigned long flags;
1109
1110 do {
1111
1112 unsigned short memtx = readw(hw->memreg_tx);
1113 unsigned short memtx_serial;
1114 unsigned short memrxdone =
1115 readw(&hw->memory_info_regs->memreg_rx_done);
1116
1117 try_mem_tx_old = 0;
1118
1119 /* check whether the interrupt was generated by ipwireless card */
1120 if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1121
1122 /* check if the card uses memreg_tx_old register */
1123 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1124 memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1125 if (memtx & MEMTX_TX) {
1126 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1127 ": Using memreg_tx_old\n");
1128 hw->memreg_tx =
1129 &hw->memory_info_regs->memreg_tx_old;
1130 } else {
1131 return IRQ_NONE;
1132 }
1133 } else
1134 return IRQ_NONE;
1135 }
1136
1137 /*
1138 * See if the card is physically present. Note that while it is
1139 * powering up, it appears not to be present.
1140 */
1141 if (!is_card_present(hw)) {
1142 acknowledge_pcmcia_interrupt(hw);
1143 return IRQ_HANDLED;
1144 }
1145
1146 memtx_serial = memtx & (unsigned short) 0xff00;
1147 if (memtx & MEMTX_TX) {
1148 writew(memtx_serial, hw->memreg_tx);
1149
1150 if (hw->serial_number_detected) {
1151 if (memtx_serial != hw->last_memtx_serial) {
1152 hw->last_memtx_serial = memtx_serial;
1153 spin_lock_irqsave(&hw->lock, flags);
1154 hw->rx_ready++;
1155 spin_unlock_irqrestore(&hw->lock, flags);
1156 rx = 1;
1157 } else
1158 /* Ignore 'Timer Recovery' duplicates. */
1159 rx_repeat = 1;
1160 } else {
1161 /*
1162 * If a non-zero serial number is seen, then enable
1163 * serial number checking.
1164 */
1165 if (memtx_serial != 0) {
1166 hw->serial_number_detected = 1;
1167 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1168 ": memreg_tx serial num detected\n");
1169
1170 spin_lock_irqsave(&hw->lock, flags);
1171 hw->rx_ready++;
1172 spin_unlock_irqrestore(&hw->lock, flags);
1173 }
1174 rx = 1;
1175 }
1176 }
1177 if (memrxdone & MEMRX_RX_DONE) {
1178 writew(0, &hw->memory_info_regs->memreg_rx_done);
1179 spin_lock_irqsave(&hw->lock, flags);
1180 hw->tx_ready = 1;
1181 spin_unlock_irqrestore(&hw->lock, flags);
1182 tx = 1;
1183 }
1184 if (tx)
1185 writew(MEMRX_PCINTACKK,
1186 &hw->memory_info_regs->memreg_pc_interrupt_ack);
1187
1188 acknowledge_pcmcia_interrupt(hw);
1189
1190 if (tx || rx)
1191 tasklet_schedule(&hw->tasklet);
1192 else if (!rx_repeat) {
1193 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1194 if (hw->serial_number_detected)
1195 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1196 ": spurious interrupt - new_tx mode\n");
1197 else {
1198 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1199 ": no valid memreg_tx value - switching to the old memreg_tx\n");
1200 hw->memreg_tx =
1201 &hw->memory_info_regs->memreg_tx_old;
1202 try_mem_tx_old = 1;
1203 }
1204 } else
1205 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1206 ": spurious interrupt - old_tx mode\n");
1207 }
1208
1209 } while (try_mem_tx_old == 1);
1210
1211 return IRQ_HANDLED;
1212}
1213
1214irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1215{
1216 struct ipw_dev *ipw = dev_id;
1217
1218 if (ipw->hardware->hw_version == HW_VERSION_1)
1219 return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
1220 else
1221 return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
1222}
1223
1224static void flush_packets_to_hw(struct ipw_hardware *hw)
1225{
1226 int priority_limit;
1227 unsigned long flags;
1228
1229 spin_lock_irqsave(&hw->lock, flags);
1230 priority_limit = get_current_packet_priority(hw);
1231 spin_unlock_irqrestore(&hw->lock, flags);
1232
1233 while (send_pending_packet(hw, priority_limit));
1234}
1235
1236static void send_packet(struct ipw_hardware *hw, int priority,
1237 struct ipw_tx_packet *packet)
1238{
1239 unsigned long flags;
1240
1241 spin_lock_irqsave(&hw->lock, flags);
1242 list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1243 hw->tx_queued++;
1244 spin_unlock_irqrestore(&hw->lock, flags);
1245
1246 flush_packets_to_hw(hw);
1247}
1248
1249/* Create data packet, non-atomic allocation */
1250static void *alloc_data_packet(int data_size,
1251 unsigned char dest_addr,
1252 unsigned char protocol)
1253{
1254 struct ipw_tx_packet *packet = kzalloc(
1255 sizeof(struct ipw_tx_packet) + data_size,
1256 GFP_ATOMIC);
1257
1258 if (!packet)
1259 return NULL;
1260
1261 INIT_LIST_HEAD(&packet->queue);
1262 packet->dest_addr = dest_addr;
1263 packet->protocol = protocol;
1264 packet->length = data_size;
1265
1266 return packet;
1267}
1268
1269static void *alloc_ctrl_packet(int header_size,
1270 unsigned char dest_addr,
1271 unsigned char protocol,
1272 unsigned char sig_no)
1273{
1274 /*
1275 * sig_no is located right after ipw_tx_packet struct in every
1276 * CTRL or SETUP packets, we can use ipw_control_packet as a
1277 * common struct
1278 */
1279 struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1280
1281 if (!packet)
1282 return NULL;
1283
1284 INIT_LIST_HEAD(&packet->header.queue);
1285 packet->header.dest_addr = dest_addr;
1286 packet->header.protocol = protocol;
1287 packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1288 packet->body.sig_no = sig_no;
1289
1290 return packet;
1291}
1292
1293int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1294 const unsigned char *data, unsigned int length,
1295 void (*callback) (void *cb, unsigned int length),
1296 void *callback_data)
1297{
1298 struct ipw_tx_packet *packet;
1299
1300 packet = alloc_data_packet(length, (channel_idx + 1),
1301 TL_PROTOCOLID_COM_DATA);
1302 if (!packet)
1303 return -ENOMEM;
1304 packet->packet_callback = callback;
1305 packet->callback_data = callback_data;
1306 memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1307 length);
1308
1309 send_packet(hw, PRIO_DATA, packet);
1310 return 0;
1311}
1312
1313static int set_control_line(struct ipw_hardware *hw, int prio,
1314 unsigned int channel_idx, int line, int state)
1315{
1316 struct ipw_control_packet *packet;
1317 int protocolid = TL_PROTOCOLID_COM_CTRL;
1318
1319 if (prio == PRIO_SETUP)
1320 protocolid = TL_PROTOCOLID_SETUP;
1321
1322 packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1323 (channel_idx + 1), protocolid, line);
1324 if (!packet)
1325 return -ENOMEM;
1326 packet->header.length = sizeof(struct ipw_control_packet_body);
1327 packet->body.value = (state == 0 ? 0 : 1);
1328 send_packet(hw, prio, &packet->header);
1329 return 0;
1330}
1331
1332
1333static int set_DTR(struct ipw_hardware *hw, int priority,
1334 unsigned int channel_idx, int state)
1335{
1336 if (state != 0)
1337 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1338 else
1339 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1340
1341 return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1342}
1343
1344static int set_RTS(struct ipw_hardware *hw, int priority,
1345 unsigned int channel_idx, int state)
1346{
1347 if (state != 0)
1348 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1349 else
1350 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1351
1352 return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1353}
1354
1355int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1356 int state)
1357{
1358 return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1359}
1360
1361int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1362 int state)
1363{
1364 return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1365}
1366
1367struct ipw_setup_get_version_query_packet {
1368 struct ipw_tx_packet header;
1369 struct tl_setup_get_version_qry body;
1370};
1371
1372struct ipw_setup_config_packet {
1373 struct ipw_tx_packet header;
1374 struct tl_setup_config_msg body;
1375};
1376
1377struct ipw_setup_config_done_packet {
1378 struct ipw_tx_packet header;
1379 struct tl_setup_config_done_msg body;
1380};
1381
1382struct ipw_setup_open_packet {
1383 struct ipw_tx_packet header;
1384 struct tl_setup_open_msg body;
1385};
1386
1387struct ipw_setup_info_packet {
1388 struct ipw_tx_packet header;
1389 struct tl_setup_info_msg body;
1390};
1391
1392struct ipw_setup_reboot_msg_ack {
1393 struct ipw_tx_packet header;
1394 struct TlSetupRebootMsgAck body;
1395};
1396
1397/* This handles the actual initialization of the card */
1398static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1399{
1400 struct ipw_setup_config_packet *config_packet;
1401 struct ipw_setup_config_done_packet *config_done_packet;
1402 struct ipw_setup_open_packet *open_packet;
1403 struct ipw_setup_info_packet *info_packet;
1404 int port;
1405 unsigned int channel_idx;
1406
1407 /* generate config packet */
1408 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1409 config_packet = alloc_ctrl_packet(
1410 sizeof(struct ipw_setup_config_packet),
1411 ADDR_SETUP_PROT,
1412 TL_PROTOCOLID_SETUP,
1413 TL_SETUP_SIGNO_CONFIG_MSG);
1414 if (!config_packet)
1415 goto exit_nomem;
1416 config_packet->header.length = sizeof(struct tl_setup_config_msg);
1417 config_packet->body.port_no = port;
1418 config_packet->body.prio_data = PRIO_DATA;
1419 config_packet->body.prio_ctrl = PRIO_CTRL;
1420 send_packet(hw, PRIO_SETUP, &config_packet->header);
1421 }
1422 config_done_packet = alloc_ctrl_packet(
1423 sizeof(struct ipw_setup_config_done_packet),
1424 ADDR_SETUP_PROT,
1425 TL_PROTOCOLID_SETUP,
1426 TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1427 if (!config_done_packet)
1428 goto exit_nomem;
1429 config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1430 send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1431
1432 /* generate open packet */
1433 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1434 open_packet = alloc_ctrl_packet(
1435 sizeof(struct ipw_setup_open_packet),
1436 ADDR_SETUP_PROT,
1437 TL_PROTOCOLID_SETUP,
1438 TL_SETUP_SIGNO_OPEN_MSG);
1439 if (!open_packet)
1440 goto exit_nomem;
1441 open_packet->header.length = sizeof(struct tl_setup_open_msg);
1442 open_packet->body.port_no = port;
1443 send_packet(hw, PRIO_SETUP, &open_packet->header);
1444 }
1445 for (channel_idx = 0;
1446 channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1447 int ret;
1448
1449 ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1450 (hw->control_lines[channel_idx] &
1451 IPW_CONTROL_LINE_DTR) != 0);
1452 if (ret) {
1453 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1454 ": error setting DTR (%d)\n", ret);
1455 return;
1456 }
1457
1458 set_RTS(hw, PRIO_SETUP, channel_idx,
1459 (hw->control_lines [channel_idx] &
1460 IPW_CONTROL_LINE_RTS) != 0);
1461 if (ret) {
1462 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1463 ": error setting RTS (%d)\n", ret);
1464 return;
1465 }
1466 }
1467 /*
1468 * For NDIS we assume that we are using sync PPP frames, for COM async.
1469 * This driver uses NDIS mode too. We don't bother with translation
1470 * from async -> sync PPP.
1471 */
1472 info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1473 ADDR_SETUP_PROT,
1474 TL_PROTOCOLID_SETUP,
1475 TL_SETUP_SIGNO_INFO_MSG);
1476 if (!info_packet)
1477 goto exit_nomem;
1478 info_packet->header.length = sizeof(struct tl_setup_info_msg);
1479 info_packet->body.driver_type = NDISWAN_DRIVER;
1480 info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1481 info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1482 send_packet(hw, PRIO_SETUP, &info_packet->header);
1483
1484 /* Initialization is now complete, so we clear the 'to_setup' flag */
1485 hw->to_setup = 0;
1486
1487 return;
1488
1489exit_nomem:
1490 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1491 ": not enough memory to alloc control packet\n");
1492 hw->to_setup = -1;
1493}
1494
1495static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1496 unsigned char vers_no)
1497{
1498 del_timer(&hw->setup_timer);
1499 hw->initializing = 0;
1500 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1501
1502 if (vers_no == TL_SETUP_VERSION)
1503 __handle_setup_get_version_rsp(hw);
1504 else
1505 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1506 ": invalid hardware version no %u\n",
1507 (unsigned int) vers_no);
1508}
1509
1510static void ipw_send_setup_packet(struct ipw_hardware *hw)
1511{
1512 struct ipw_setup_get_version_query_packet *ver_packet;
1513
1514 ver_packet = alloc_ctrl_packet(
1515 sizeof(struct ipw_setup_get_version_query_packet),
1516 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1517 TL_SETUP_SIGNO_GET_VERSION_QRY);
1518 ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1519
1520 /*
1521 * Response is handled in handle_received_SETUP_packet
1522 */
1523 send_packet(hw, PRIO_SETUP, &ver_packet->header);
1524}
1525
1526static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1527 unsigned int address,
1528 const unsigned char *data, int len,
1529 int is_last)
1530{
1531 const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
1532
1533 if (address != ADDR_SETUP_PROT) {
1534 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1535 ": setup packet has bad address %d\n", address);
1536 return;
1537 }
1538
1539 switch (rx_msg->sig_no) {
1540 case TL_SETUP_SIGNO_GET_VERSION_RSP:
1541 if (hw->to_setup)
1542 handle_setup_get_version_rsp(hw,
1543 rx_msg->version_rsp_msg.version);
1544 break;
1545
1546 case TL_SETUP_SIGNO_OPEN_MSG:
1547 if (ipwireless_debug) {
1548 unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1549
1550 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1551 ": OPEN_MSG [channel %u] reply received\n",
1552 channel_idx);
1553 }
1554 break;
1555
1556 case TL_SETUP_SIGNO_INFO_MSG_ACK:
1557 if (ipwireless_debug)
1558 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1559 ": card successfully configured as NDISWAN\n");
1560 break;
1561
1562 case TL_SETUP_SIGNO_REBOOT_MSG:
1563 if (hw->to_setup)
1564 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1565 ": Setup not completed - ignoring reboot msg\n");
1566 else {
1567 struct ipw_setup_reboot_msg_ack *packet;
1568
1569 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1570 ": Acknowledging REBOOT message\n");
1571 packet = alloc_ctrl_packet(
1572 sizeof(struct ipw_setup_reboot_msg_ack),
1573 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1574 TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1575 packet->header.length =
1576 sizeof(struct TlSetupRebootMsgAck);
1577 send_packet(hw, PRIO_SETUP, &packet->header);
1578 if (hw->reboot_callback)
1579 hw->reboot_callback(hw->reboot_callback_data);
1580 }
1581 break;
1582
1583 default:
1584 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1585 ": unknown setup message %u received\n",
1586 (unsigned int) rx_msg->sig_no);
1587 }
1588}
1589
1590static void do_close_hardware(struct ipw_hardware *hw)
1591{
1592 unsigned int irqn;
1593
1594 if (hw->hw_version == HW_VERSION_1) {
1595 /* Disable TX and RX interrupts. */
1596 outw(0, hw->base_port + IOIER);
1597
1598 /* Acknowledge any outstanding interrupt requests */
1599 irqn = inw(hw->base_port + IOIR);
1600 if (irqn & IR_TXINTR)
1601 outw(IR_TXINTR, hw->base_port + IOIR);
1602 if (irqn & IR_RXINTR)
1603 outw(IR_RXINTR, hw->base_port + IOIR);
1604
1605 synchronize_irq(hw->irq);
1606 }
1607}
1608
1609struct ipw_hardware *ipwireless_hardware_create(void)
1610{
1611 int i;
1612 struct ipw_hardware *hw =
1613 kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1614
1615 if (!hw)
1616 return NULL;
1617
1618 hw->irq = -1;
1619 hw->initializing = 1;
1620 hw->tx_ready = 1;
1621 hw->rx_bytes_queued = 0;
1622 hw->rx_pool_size = 0;
1623 hw->last_memtx_serial = (unsigned short) 0xffff;
1624 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1625 INIT_LIST_HEAD(&hw->tx_queue[i]);
1626
1627 INIT_LIST_HEAD(&hw->rx_queue);
1628 INIT_LIST_HEAD(&hw->rx_pool);
1629 spin_lock_init(&hw->lock);
1630 tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1631 INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1632 setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1633 (unsigned long) hw);
1634
1635 return hw;
1636}
1637
1638void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1639 unsigned int base_port,
1640 void __iomem *attr_memory,
1641 void __iomem *common_memory,
1642 int is_v2_card,
1643 void (*reboot_callback) (void *data),
1644 void *reboot_callback_data)
1645{
1646 if (hw->removed) {
1647 hw->removed = 0;
1648 enable_irq(hw->irq);
1649 }
1650 hw->base_port = base_port;
1651 hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1652 hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1653 hw->memregs_CCR = (struct MEMCCR __iomem *)
1654 ((unsigned short __iomem *) attr_memory + 0x200);
1655 hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1656 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1657 hw->reboot_callback = reboot_callback;
1658 hw->reboot_callback_data = reboot_callback_data;
1659}
1660
1661void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1662{
1663 hw->initializing = 1;
1664 hw->init_loops = 0;
1665 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1666 ": waiting for card to start up...\n");
1667 ipwireless_setup_timer((unsigned long) hw);
1668}
1669
1670static void ipwireless_setup_timer(unsigned long data)
1671{
1672 struct ipw_hardware *hw = (struct ipw_hardware *) data;
1673
1674 hw->init_loops++;
1675
1676 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1677 hw->hw_version == HW_VERSION_2 &&
1678 hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1679 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1680 ": failed to startup using TX2, trying TX\n");
1681
1682 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1683 hw->init_loops = 0;
1684 }
1685 /* Give up after a certain number of retries */
1686 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1687 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1688 ": card failed to start up!\n");
1689 hw->initializing = 0;
1690 } else {
1691 /* Do not attempt to write to the board if it is not present. */
1692 if (is_card_present(hw)) {
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&hw->lock, flags);
1696 hw->to_setup = 1;
1697 hw->tx_ready = 1;
1698 spin_unlock_irqrestore(&hw->lock, flags);
1699 tasklet_schedule(&hw->tasklet);
1700 }
1701
1702 mod_timer(&hw->setup_timer,
1703 jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1704 }
1705}
1706
1707/*
1708 * Stop any interrupts from executing so that, once this function returns,
1709 * other layers of the driver can be sure they won't get any more callbacks.
1710 * Thus must be called on a proper process context.
1711 */
1712void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1713{
1714 if (!hw->shutting_down) {
1715 /* Tell everyone we are going down. */
1716 hw->shutting_down = 1;
1717 del_timer(&hw->setup_timer);
1718
1719 /* Prevent the hardware from sending any more interrupts */
1720 do_close_hardware(hw);
1721 }
1722}
1723
1724void ipwireless_hardware_free(struct ipw_hardware *hw)
1725{
1726 int i;
1727 struct ipw_rx_packet *rp, *rq;
1728 struct ipw_tx_packet *tp, *tq;
1729
1730 ipwireless_stop_interrupts(hw);
1731
1732 flush_work_sync(&hw->work_rx);
1733
1734 for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1735 if (hw->packet_assembler[i] != NULL)
1736 kfree(hw->packet_assembler[i]);
1737
1738 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1739 list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1740 list_del(&tp->queue);
1741 kfree(tp);
1742 }
1743
1744 list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1745 list_del(&rp->queue);
1746 kfree(rp);
1747 }
1748
1749 list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1750 list_del(&rp->queue);
1751 kfree(rp);
1752 }
1753 kfree(hw);
1754}
1755
1756/*
1757 * Associate the specified network with this hardware, so it will receive events
1758 * from it.
1759 */
1760void ipwireless_associate_network(struct ipw_hardware *hw,
1761 struct ipw_network *network)
1762{
1763 hw->network = network;
1764}
diff --git a/drivers/tty/ipwireless/hardware.h b/drivers/tty/ipwireless/hardware.h
new file mode 100644
index 000000000000..90a8590e43b0
--- /dev/null
+++ b/drivers/tty/ipwireless/hardware.h
@@ -0,0 +1,62 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_HARDWARE_H_
19#define _IPWIRELESS_CS_HARDWARE_H_
20
21#include <linux/types.h>
22#include <linux/sched.h>
23#include <linux/interrupt.h>
24
25#define IPW_CONTROL_LINE_CTS 0x0001
26#define IPW_CONTROL_LINE_DCD 0x0002
27#define IPW_CONTROL_LINE_DSR 0x0004
28#define IPW_CONTROL_LINE_RI 0x0008
29#define IPW_CONTROL_LINE_DTR 0x0010
30#define IPW_CONTROL_LINE_RTS 0x0020
31
32struct ipw_hardware;
33struct ipw_network;
34
35struct ipw_hardware *ipwireless_hardware_create(void);
36void ipwireless_hardware_free(struct ipw_hardware *hw);
37irqreturn_t ipwireless_interrupt(int irq, void *dev_id);
38int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
39 int state);
40int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
41 int state);
42int ipwireless_send_packet(struct ipw_hardware *hw,
43 unsigned int channel_idx,
44 const unsigned char *data,
45 unsigned int length,
46 void (*packet_sent_callback) (void *cb,
47 unsigned int length),
48 void *sent_cb_data);
49void ipwireless_associate_network(struct ipw_hardware *hw,
50 struct ipw_network *net);
51void ipwireless_stop_interrupts(struct ipw_hardware *hw);
52void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
53 unsigned int base_port,
54 void __iomem *attr_memory,
55 void __iomem *common_memory,
56 int is_v2_card,
57 void (*reboot_cb) (void *data),
58 void *reboot_cb_data);
59void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw);
60void ipwireless_sleep(unsigned int tenths);
61
62#endif
diff --git a/drivers/tty/ipwireless/main.c b/drivers/tty/ipwireless/main.c
new file mode 100644
index 000000000000..655c7948261c
--- /dev/null
+++ b/drivers/tty/ipwireless/main.c
@@ -0,0 +1,347 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include "hardware.h"
19#include "network.h"
20#include "main.h"
21#include "tty.h"
22
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/io.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30
31#include <pcmcia/cisreg.h>
32#include <pcmcia/device_id.h>
33#include <pcmcia/ss.h>
34#include <pcmcia/ds.h>
35
36static const struct pcmcia_device_id ipw_ids[] = {
37 PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
38 PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
39 PCMCIA_DEVICE_NULL
40};
41MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
42
43static void ipwireless_detach(struct pcmcia_device *link);
44
45/*
46 * Module params
47 */
48/* Debug mode: more verbose, print sent/recv bytes */
49int ipwireless_debug;
50int ipwireless_loopback;
51int ipwireless_out_queue = 10;
52
53module_param_named(debug, ipwireless_debug, int, 0);
54module_param_named(loopback, ipwireless_loopback, int, 0);
55module_param_named(out_queue, ipwireless_out_queue, int, 0);
56MODULE_PARM_DESC(debug, "switch on debug messages [0]");
57MODULE_PARM_DESC(loopback,
58 "debug: enable ras_raw channel [0]");
59MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
60
61/* Executes in process context. */
62static void signalled_reboot_work(struct work_struct *work_reboot)
63{
64 struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
65 work_reboot);
66 struct pcmcia_device *link = ipw->link;
67 pcmcia_reset_card(link->socket);
68}
69
70static void signalled_reboot_callback(void *callback_data)
71{
72 struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
73
74 /* Delegate to process context. */
75 schedule_work(&ipw->work_reboot);
76}
77
78static int ipwireless_probe(struct pcmcia_device *p_dev, void *priv_data)
79{
80 struct ipw_dev *ipw = priv_data;
81 int ret;
82
83 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
84 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
85
86 /* 0x40 causes it to generate level mode interrupts. */
87 /* 0x04 enables IREQ pin. */
88 p_dev->config_index |= 0x44;
89 p_dev->io_lines = 16;
90 ret = pcmcia_request_io(p_dev);
91 if (ret)
92 return ret;
93
94 if (!request_region(p_dev->resource[0]->start,
95 resource_size(p_dev->resource[0]),
96 IPWIRELESS_PCCARD_NAME)) {
97 ret = -EBUSY;
98 goto exit;
99 }
100
101 p_dev->resource[2]->flags |=
102 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
103
104 ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
105 if (ret != 0)
106 goto exit1;
107
108 ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2], p_dev->card_addr);
109 if (ret != 0)
110 goto exit1;
111
112 ipw->is_v2_card = resource_size(p_dev->resource[2]) == 0x100;
113
114 ipw->common_memory = ioremap(p_dev->resource[2]->start,
115 resource_size(p_dev->resource[2]));
116 if (!request_mem_region(p_dev->resource[2]->start,
117 resource_size(p_dev->resource[2]),
118 IPWIRELESS_PCCARD_NAME)) {
119 ret = -EBUSY;
120 goto exit2;
121 }
122
123 p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
124 WIN_ENABLE;
125 p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
126 ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
127 if (ret != 0)
128 goto exit3;
129
130 ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
131 if (ret != 0)
132 goto exit3;
133
134 ipw->attr_memory = ioremap(p_dev->resource[3]->start,
135 resource_size(p_dev->resource[3]));
136 if (!request_mem_region(p_dev->resource[3]->start,
137 resource_size(p_dev->resource[3]),
138 IPWIRELESS_PCCARD_NAME)) {
139 ret = -EBUSY;
140 goto exit4;
141 }
142
143 return 0;
144
145exit4:
146 iounmap(ipw->attr_memory);
147exit3:
148 release_mem_region(p_dev->resource[2]->start,
149 resource_size(p_dev->resource[2]));
150exit2:
151 iounmap(ipw->common_memory);
152exit1:
153 release_region(p_dev->resource[0]->start,
154 resource_size(p_dev->resource[0]));
155exit:
156 pcmcia_disable_device(p_dev);
157 return ret;
158}
159
160static int config_ipwireless(struct ipw_dev *ipw)
161{
162 struct pcmcia_device *link = ipw->link;
163 int ret = 0;
164
165 ipw->is_v2_card = 0;
166 link->config_flags |= CONF_AUTO_SET_IO | CONF_AUTO_SET_IOMEM |
167 CONF_ENABLE_IRQ;
168
169 ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
170 if (ret != 0)
171 return ret;
172
173 INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
174
175 ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
176 ipw->attr_memory, ipw->common_memory,
177 ipw->is_v2_card, signalled_reboot_callback,
178 ipw);
179
180 ret = pcmcia_request_irq(link, ipwireless_interrupt);
181 if (ret != 0)
182 goto exit;
183
184 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
185 ipw->is_v2_card ? "V2/V3" : "V1");
186 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
187 ": I/O ports %pR, irq %d\n", link->resource[0],
188 (unsigned int) link->irq);
189 if (ipw->attr_memory && ipw->common_memory)
190 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
191 ": attr memory %pR, common memory %pR\n",
192 link->resource[3],
193 link->resource[2]);
194
195 ipw->network = ipwireless_network_create(ipw->hardware);
196 if (!ipw->network)
197 goto exit;
198
199 ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
200 if (!ipw->tty)
201 goto exit;
202
203 ipwireless_init_hardware_v2_v3(ipw->hardware);
204
205 /*
206 * Do the RequestConfiguration last, because it enables interrupts.
207 * Then we don't get any interrupts before we're ready for them.
208 */
209 ret = pcmcia_enable_device(link);
210 if (ret != 0)
211 goto exit;
212
213 return 0;
214
215exit:
216 if (ipw->common_memory) {
217 release_mem_region(link->resource[2]->start,
218 resource_size(link->resource[2]));
219 iounmap(ipw->common_memory);
220 }
221 if (ipw->attr_memory) {
222 release_mem_region(link->resource[3]->start,
223 resource_size(link->resource[3]));
224 iounmap(ipw->attr_memory);
225 }
226 pcmcia_disable_device(link);
227 return -1;
228}
229
230static void release_ipwireless(struct ipw_dev *ipw)
231{
232 release_region(ipw->link->resource[0]->start,
233 resource_size(ipw->link->resource[0]));
234 if (ipw->common_memory) {
235 release_mem_region(ipw->link->resource[2]->start,
236 resource_size(ipw->link->resource[2]));
237 iounmap(ipw->common_memory);
238 }
239 if (ipw->attr_memory) {
240 release_mem_region(ipw->link->resource[3]->start,
241 resource_size(ipw->link->resource[3]));
242 iounmap(ipw->attr_memory);
243 }
244 pcmcia_disable_device(ipw->link);
245}
246
247/*
248 * ipwireless_attach() creates an "instance" of the driver, allocating
249 * local data structures for one device (one interface). The device
250 * is registered with Card Services.
251 *
252 * The pcmcia_device structure is initialized, but we don't actually
253 * configure the card at this point -- we wait until we receive a
254 * card insertion event.
255 */
256static int ipwireless_attach(struct pcmcia_device *link)
257{
258 struct ipw_dev *ipw;
259 int ret;
260
261 ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
262 if (!ipw)
263 return -ENOMEM;
264
265 ipw->link = link;
266 link->priv = ipw;
267
268 ipw->hardware = ipwireless_hardware_create();
269 if (!ipw->hardware) {
270 kfree(ipw);
271 return -ENOMEM;
272 }
273 /* RegisterClient will call config_ipwireless */
274
275 ret = config_ipwireless(ipw);
276
277 if (ret != 0) {
278 ipwireless_detach(link);
279 return ret;
280 }
281
282 return 0;
283}
284
285/*
286 * This deletes a driver "instance". The device is de-registered with
287 * Card Services. If it has been released, all local data structures
288 * are freed. Otherwise, the structures will be freed when the device
289 * is released.
290 */
291static void ipwireless_detach(struct pcmcia_device *link)
292{
293 struct ipw_dev *ipw = link->priv;
294
295 release_ipwireless(ipw);
296
297 if (ipw->tty != NULL)
298 ipwireless_tty_free(ipw->tty);
299 if (ipw->network != NULL)
300 ipwireless_network_free(ipw->network);
301 if (ipw->hardware != NULL)
302 ipwireless_hardware_free(ipw->hardware);
303 kfree(ipw);
304}
305
306static struct pcmcia_driver me = {
307 .owner = THIS_MODULE,
308 .probe = ipwireless_attach,
309 .remove = ipwireless_detach,
310 .name = IPWIRELESS_PCCARD_NAME,
311 .id_table = ipw_ids
312};
313
314/*
315 * Module insertion : initialisation of the module.
316 * Register the card with cardmgr...
317 */
318static int __init init_ipwireless(void)
319{
320 int ret;
321
322 ret = ipwireless_tty_init();
323 if (ret != 0)
324 return ret;
325
326 ret = pcmcia_register_driver(&me);
327 if (ret != 0)
328 ipwireless_tty_release();
329
330 return ret;
331}
332
333/*
334 * Module removal
335 */
336static void __exit exit_ipwireless(void)
337{
338 pcmcia_unregister_driver(&me);
339 ipwireless_tty_release();
340}
341
342module_init(init_ipwireless);
343module_exit(exit_ipwireless);
344
345MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
346MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
347MODULE_LICENSE("GPL");
diff --git a/drivers/tty/ipwireless/main.h b/drivers/tty/ipwireless/main.h
new file mode 100644
index 000000000000..f2cbb116bccb
--- /dev/null
+++ b/drivers/tty/ipwireless/main.h
@@ -0,0 +1,68 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_H_
19#define _IPWIRELESS_CS_H_
20
21#include <linux/sched.h>
22#include <linux/types.h>
23
24#include <pcmcia/cistpl.h>
25#include <pcmcia/ds.h>
26
27#include "hardware.h"
28
29#define IPWIRELESS_PCCARD_NAME "ipwireless"
30#define IPWIRELESS_PCMCIA_VERSION "1.1"
31#define IPWIRELESS_PCMCIA_AUTHOR \
32 "Stephen Blackheath, Ben Martel, Jiri Kosina and David Sterba"
33
34#define IPWIRELESS_TX_QUEUE_SIZE 262144
35#define IPWIRELESS_RX_QUEUE_SIZE 262144
36
37#define IPWIRELESS_STATE_DEBUG
38
39struct ipw_hardware;
40struct ipw_network;
41struct ipw_tty;
42
43struct ipw_dev {
44 struct pcmcia_device *link;
45 int is_v2_card;
46
47 void __iomem *attr_memory;
48
49 void __iomem *common_memory;
50
51 /* Reference to attribute memory, containing CIS data */
52 void *attribute_memory;
53
54 /* Hardware context */
55 struct ipw_hardware *hardware;
56 /* Network layer context */
57 struct ipw_network *network;
58 /* TTY device context */
59 struct ipw_tty *tty;
60 struct work_struct work_reboot;
61};
62
63/* Module parametres */
64extern int ipwireless_debug;
65extern int ipwireless_loopback;
66extern int ipwireless_out_queue;
67
68#endif
diff --git a/drivers/tty/ipwireless/network.c b/drivers/tty/ipwireless/network.c
new file mode 100644
index 000000000000..f7daeea598e4
--- /dev/null
+++ b/drivers/tty/ipwireless/network.c
@@ -0,0 +1,508 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/mutex.h>
21#include <linux/netdevice.h>
22#include <linux/ppp_channel.h>
23#include <linux/ppp_defs.h>
24#include <linux/slab.h>
25#include <linux/if_ppp.h>
26#include <linux/skbuff.h>
27
28#include "network.h"
29#include "hardware.h"
30#include "main.h"
31#include "tty.h"
32
33#define MAX_ASSOCIATED_TTYS 2
34
35#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
36
37struct ipw_network {
38 /* Hardware context, used for calls to hardware layer. */
39 struct ipw_hardware *hardware;
40 /* Context for kernel 'generic_ppp' functionality */
41 struct ppp_channel *ppp_channel;
42 /* tty context connected with IPW console */
43 struct ipw_tty *associated_ttys[NO_OF_IPW_CHANNELS][MAX_ASSOCIATED_TTYS];
44 /* True if ppp needs waking up once we're ready to xmit */
45 int ppp_blocked;
46 /* Number of packets queued up in hardware module. */
47 int outgoing_packets_queued;
48 /* Spinlock to avoid interrupts during shutdown */
49 spinlock_t lock;
50 struct mutex close_lock;
51
52 /* PPP ioctl data, not actually used anywere */
53 unsigned int flags;
54 unsigned int rbits;
55 u32 xaccm[8];
56 u32 raccm;
57 int mru;
58
59 int shutting_down;
60 unsigned int ras_control_lines;
61
62 struct work_struct work_go_online;
63 struct work_struct work_go_offline;
64};
65
66static void notify_packet_sent(void *callback_data, unsigned int packet_length)
67{
68 struct ipw_network *network = callback_data;
69 unsigned long flags;
70
71 spin_lock_irqsave(&network->lock, flags);
72 network->outgoing_packets_queued--;
73 if (network->ppp_channel != NULL) {
74 if (network->ppp_blocked) {
75 network->ppp_blocked = 0;
76 spin_unlock_irqrestore(&network->lock, flags);
77 ppp_output_wakeup(network->ppp_channel);
78 if (ipwireless_debug)
79 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
80 ": ppp unblocked\n");
81 } else
82 spin_unlock_irqrestore(&network->lock, flags);
83 } else
84 spin_unlock_irqrestore(&network->lock, flags);
85}
86
87/*
88 * Called by the ppp system when it has a packet to send to the hardware.
89 */
90static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel,
91 struct sk_buff *skb)
92{
93 struct ipw_network *network = ppp_channel->private;
94 unsigned long flags;
95
96 spin_lock_irqsave(&network->lock, flags);
97 if (network->outgoing_packets_queued < ipwireless_out_queue) {
98 unsigned char *buf;
99 static unsigned char header[] = {
100 PPP_ALLSTATIONS, /* 0xff */
101 PPP_UI, /* 0x03 */
102 };
103 int ret;
104
105 network->outgoing_packets_queued++;
106 spin_unlock_irqrestore(&network->lock, flags);
107
108 /*
109 * If we have the requested amount of headroom in the skb we
110 * were handed, then we can add the header efficiently.
111 */
112 if (skb_headroom(skb) >= 2) {
113 memcpy(skb_push(skb, 2), header, 2);
114 ret = ipwireless_send_packet(network->hardware,
115 IPW_CHANNEL_RAS, skb->data,
116 skb->len,
117 notify_packet_sent,
118 network);
119 if (ret == -1) {
120 skb_pull(skb, 2);
121 return 0;
122 }
123 } else {
124 /* Otherwise (rarely) we do it inefficiently. */
125 buf = kmalloc(skb->len + 2, GFP_ATOMIC);
126 if (!buf)
127 return 0;
128 memcpy(buf + 2, skb->data, skb->len);
129 memcpy(buf, header, 2);
130 ret = ipwireless_send_packet(network->hardware,
131 IPW_CHANNEL_RAS, buf,
132 skb->len + 2,
133 notify_packet_sent,
134 network);
135 kfree(buf);
136 if (ret == -1)
137 return 0;
138 }
139 kfree_skb(skb);
140 return 1;
141 } else {
142 /*
143 * Otherwise reject the packet, and flag that the ppp system
144 * needs to be unblocked once we are ready to send.
145 */
146 network->ppp_blocked = 1;
147 spin_unlock_irqrestore(&network->lock, flags);
148 if (ipwireless_debug)
149 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
150 return 0;
151 }
152}
153
154/* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
155static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
156 unsigned int cmd, unsigned long arg)
157{
158 struct ipw_network *network = ppp_channel->private;
159 int err, val;
160 u32 accm[8];
161 int __user *user_arg = (int __user *) arg;
162
163 err = -EFAULT;
164 switch (cmd) {
165 case PPPIOCGFLAGS:
166 val = network->flags | network->rbits;
167 if (put_user(val, user_arg))
168 break;
169 err = 0;
170 break;
171
172 case PPPIOCSFLAGS:
173 if (get_user(val, user_arg))
174 break;
175 network->flags = val & ~SC_RCV_BITS;
176 network->rbits = val & SC_RCV_BITS;
177 err = 0;
178 break;
179
180 case PPPIOCGASYNCMAP:
181 if (put_user(network->xaccm[0], user_arg))
182 break;
183 err = 0;
184 break;
185
186 case PPPIOCSASYNCMAP:
187 if (get_user(network->xaccm[0], user_arg))
188 break;
189 err = 0;
190 break;
191
192 case PPPIOCGRASYNCMAP:
193 if (put_user(network->raccm, user_arg))
194 break;
195 err = 0;
196 break;
197
198 case PPPIOCSRASYNCMAP:
199 if (get_user(network->raccm, user_arg))
200 break;
201 err = 0;
202 break;
203
204 case PPPIOCGXASYNCMAP:
205 if (copy_to_user((void __user *) arg, network->xaccm,
206 sizeof(network->xaccm)))
207 break;
208 err = 0;
209 break;
210
211 case PPPIOCSXASYNCMAP:
212 if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
213 break;
214 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
215 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
216 memcpy(network->xaccm, accm, sizeof(network->xaccm));
217 err = 0;
218 break;
219
220 case PPPIOCGMRU:
221 if (put_user(network->mru, user_arg))
222 break;
223 err = 0;
224 break;
225
226 case PPPIOCSMRU:
227 if (get_user(val, user_arg))
228 break;
229 if (val < PPP_MRU)
230 val = PPP_MRU;
231 network->mru = val;
232 err = 0;
233 break;
234
235 default:
236 err = -ENOTTY;
237 }
238
239 return err;
240}
241
242static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
243 .start_xmit = ipwireless_ppp_start_xmit,
244 .ioctl = ipwireless_ppp_ioctl
245};
246
247static void do_go_online(struct work_struct *work_go_online)
248{
249 struct ipw_network *network =
250 container_of(work_go_online, struct ipw_network,
251 work_go_online);
252 unsigned long flags;
253
254 spin_lock_irqsave(&network->lock, flags);
255 if (!network->ppp_channel) {
256 struct ppp_channel *channel;
257
258 spin_unlock_irqrestore(&network->lock, flags);
259 channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
260 if (!channel) {
261 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
262 ": unable to allocate PPP channel\n");
263 return;
264 }
265 channel->private = network;
266 channel->mtu = 16384; /* Wild guess */
267 channel->hdrlen = 2;
268 channel->ops = &ipwireless_ppp_channel_ops;
269
270 network->flags = 0;
271 network->rbits = 0;
272 network->mru = PPP_MRU;
273 memset(network->xaccm, 0, sizeof(network->xaccm));
274 network->xaccm[0] = ~0U;
275 network->xaccm[3] = 0x60000000U;
276 network->raccm = ~0U;
277 ppp_register_channel(channel);
278 spin_lock_irqsave(&network->lock, flags);
279 network->ppp_channel = channel;
280 }
281 spin_unlock_irqrestore(&network->lock, flags);
282}
283
284static void do_go_offline(struct work_struct *work_go_offline)
285{
286 struct ipw_network *network =
287 container_of(work_go_offline, struct ipw_network,
288 work_go_offline);
289 unsigned long flags;
290
291 mutex_lock(&network->close_lock);
292 spin_lock_irqsave(&network->lock, flags);
293 if (network->ppp_channel != NULL) {
294 struct ppp_channel *channel = network->ppp_channel;
295
296 network->ppp_channel = NULL;
297 spin_unlock_irqrestore(&network->lock, flags);
298 mutex_unlock(&network->close_lock);
299 ppp_unregister_channel(channel);
300 } else {
301 spin_unlock_irqrestore(&network->lock, flags);
302 mutex_unlock(&network->close_lock);
303 }
304}
305
306void ipwireless_network_notify_control_line_change(struct ipw_network *network,
307 unsigned int channel_idx,
308 unsigned int control_lines,
309 unsigned int changed_mask)
310{
311 int i;
312
313 if (channel_idx == IPW_CHANNEL_RAS)
314 network->ras_control_lines = control_lines;
315
316 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
317 struct ipw_tty *tty =
318 network->associated_ttys[channel_idx][i];
319
320 /*
321 * If it's associated with a tty (other than the RAS channel
322 * when we're online), then send the data to that tty. The RAS
323 * channel's data is handled above - it always goes through
324 * ppp_generic.
325 */
326 if (tty)
327 ipwireless_tty_notify_control_line_change(tty,
328 channel_idx,
329 control_lines,
330 changed_mask);
331 }
332}
333
334/*
335 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
336 * bytes, which are required on sent packet, but not always present on received
337 * packets
338 */
339static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
340 unsigned int length)
341{
342 struct sk_buff *skb;
343
344 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
345 length -= 2;
346 data += 2;
347 }
348
349 skb = dev_alloc_skb(length + 4);
350 skb_reserve(skb, 2);
351 memcpy(skb_put(skb, length), data, length);
352
353 return skb;
354}
355
356void ipwireless_network_packet_received(struct ipw_network *network,
357 unsigned int channel_idx,
358 unsigned char *data,
359 unsigned int length)
360{
361 int i;
362 unsigned long flags;
363
364 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
365 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
366
367 if (!tty)
368 continue;
369
370 /*
371 * If it's associated with a tty (other than the RAS channel
372 * when we're online), then send the data to that tty. The RAS
373 * channel's data is handled above - it always goes through
374 * ppp_generic.
375 */
376 if (channel_idx == IPW_CHANNEL_RAS
377 && (network->ras_control_lines &
378 IPW_CONTROL_LINE_DCD) != 0
379 && ipwireless_tty_is_modem(tty)) {
380 /*
381 * If data came in on the RAS channel and this tty is
382 * the modem tty, and we are online, then we send it to
383 * the PPP layer.
384 */
385 mutex_lock(&network->close_lock);
386 spin_lock_irqsave(&network->lock, flags);
387 if (network->ppp_channel != NULL) {
388 struct sk_buff *skb;
389
390 spin_unlock_irqrestore(&network->lock,
391 flags);
392
393 /* Send the data to the ppp_generic module. */
394 skb = ipw_packet_received_skb(data, length);
395 ppp_input(network->ppp_channel, skb);
396 } else
397 spin_unlock_irqrestore(&network->lock,
398 flags);
399 mutex_unlock(&network->close_lock);
400 }
401 /* Otherwise we send it out the tty. */
402 else
403 ipwireless_tty_received(tty, data, length);
404 }
405}
406
407struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
408{
409 struct ipw_network *network =
410 kzalloc(sizeof(struct ipw_network), GFP_ATOMIC);
411
412 if (!network)
413 return NULL;
414
415 spin_lock_init(&network->lock);
416 mutex_init(&network->close_lock);
417
418 network->hardware = hw;
419
420 INIT_WORK(&network->work_go_online, do_go_online);
421 INIT_WORK(&network->work_go_offline, do_go_offline);
422
423 ipwireless_associate_network(hw, network);
424
425 return network;
426}
427
428void ipwireless_network_free(struct ipw_network *network)
429{
430 network->shutting_down = 1;
431
432 ipwireless_ppp_close(network);
433 flush_work_sync(&network->work_go_online);
434 flush_work_sync(&network->work_go_offline);
435
436 ipwireless_stop_interrupts(network->hardware);
437 ipwireless_associate_network(network->hardware, NULL);
438
439 kfree(network);
440}
441
442void ipwireless_associate_network_tty(struct ipw_network *network,
443 unsigned int channel_idx,
444 struct ipw_tty *tty)
445{
446 int i;
447
448 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
449 if (network->associated_ttys[channel_idx][i] == NULL) {
450 network->associated_ttys[channel_idx][i] = tty;
451 break;
452 }
453}
454
455void ipwireless_disassociate_network_ttys(struct ipw_network *network,
456 unsigned int channel_idx)
457{
458 int i;
459
460 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
461 network->associated_ttys[channel_idx][i] = NULL;
462}
463
464void ipwireless_ppp_open(struct ipw_network *network)
465{
466 if (ipwireless_debug)
467 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
468 schedule_work(&network->work_go_online);
469}
470
471void ipwireless_ppp_close(struct ipw_network *network)
472{
473 /* Disconnect from the wireless network. */
474 if (ipwireless_debug)
475 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n");
476 schedule_work(&network->work_go_offline);
477}
478
479int ipwireless_ppp_channel_index(struct ipw_network *network)
480{
481 int ret = -1;
482 unsigned long flags;
483
484 spin_lock_irqsave(&network->lock, flags);
485 if (network->ppp_channel != NULL)
486 ret = ppp_channel_index(network->ppp_channel);
487 spin_unlock_irqrestore(&network->lock, flags);
488
489 return ret;
490}
491
492int ipwireless_ppp_unit_number(struct ipw_network *network)
493{
494 int ret = -1;
495 unsigned long flags;
496
497 spin_lock_irqsave(&network->lock, flags);
498 if (network->ppp_channel != NULL)
499 ret = ppp_unit_number(network->ppp_channel);
500 spin_unlock_irqrestore(&network->lock, flags);
501
502 return ret;
503}
504
505int ipwireless_ppp_mru(const struct ipw_network *network)
506{
507 return network->mru;
508}
diff --git a/drivers/tty/ipwireless/network.h b/drivers/tty/ipwireless/network.h
new file mode 100644
index 000000000000..561f765b3334
--- /dev/null
+++ b/drivers/tty/ipwireless/network.h
@@ -0,0 +1,53 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_NETWORK_H_
19#define _IPWIRELESS_CS_NETWORK_H_
20
21#include <linux/types.h>
22
23struct ipw_network;
24struct ipw_tty;
25struct ipw_hardware;
26
27/* Definitions of the different channels on the PCMCIA UE */
28#define IPW_CHANNEL_RAS 0
29#define IPW_CHANNEL_DIALLER 1
30#define IPW_CHANNEL_CONSOLE 2
31#define NO_OF_IPW_CHANNELS 5
32
33void ipwireless_network_notify_control_line_change(struct ipw_network *net,
34 unsigned int channel_idx, unsigned int control_lines,
35 unsigned int control_mask);
36void ipwireless_network_packet_received(struct ipw_network *net,
37 unsigned int channel_idx, unsigned char *data,
38 unsigned int length);
39struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw);
40void ipwireless_network_free(struct ipw_network *net);
41void ipwireless_associate_network_tty(struct ipw_network *net,
42 unsigned int channel_idx, struct ipw_tty *tty);
43void ipwireless_disassociate_network_ttys(struct ipw_network *net,
44 unsigned int channel_idx);
45
46void ipwireless_ppp_open(struct ipw_network *net);
47
48void ipwireless_ppp_close(struct ipw_network *net);
49int ipwireless_ppp_channel_index(struct ipw_network *net);
50int ipwireless_ppp_unit_number(struct ipw_network *net);
51int ipwireless_ppp_mru(const struct ipw_network *net);
52
53#endif
diff --git a/drivers/tty/ipwireless/setup_protocol.h b/drivers/tty/ipwireless/setup_protocol.h
new file mode 100644
index 000000000000..9d6bcc77c73c
--- /dev/null
+++ b/drivers/tty/ipwireless/setup_protocol.h
@@ -0,0 +1,108 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_SETUP_PROTOCOL_H_
19#define _IPWIRELESS_CS_SETUP_PROTOCOL_H_
20
21/* Version of the setup protocol and transport protocols */
22#define TL_SETUP_VERSION 1
23
24#define TL_SETUP_VERSION_QRY_TMO 1000
25#define TL_SETUP_MAX_VERSION_QRY 30
26
27/* Message numbers 0-9 are obsoleted and must not be reused! */
28#define TL_SETUP_SIGNO_GET_VERSION_QRY 10
29#define TL_SETUP_SIGNO_GET_VERSION_RSP 11
30#define TL_SETUP_SIGNO_CONFIG_MSG 12
31#define TL_SETUP_SIGNO_CONFIG_DONE_MSG 13
32#define TL_SETUP_SIGNO_OPEN_MSG 14
33#define TL_SETUP_SIGNO_CLOSE_MSG 15
34
35#define TL_SETUP_SIGNO_INFO_MSG 20
36#define TL_SETUP_SIGNO_INFO_MSG_ACK 21
37
38#define TL_SETUP_SIGNO_REBOOT_MSG 22
39#define TL_SETUP_SIGNO_REBOOT_MSG_ACK 23
40
41/* Synchronous start-messages */
42struct tl_setup_get_version_qry {
43 unsigned char sig_no; /* TL_SETUP_SIGNO_GET_VERSION_QRY */
44} __attribute__ ((__packed__));
45
46struct tl_setup_get_version_rsp {
47 unsigned char sig_no; /* TL_SETUP_SIGNO_GET_VERSION_RSP */
48 unsigned char version; /* TL_SETUP_VERSION */
49} __attribute__ ((__packed__));
50
51struct tl_setup_config_msg {
52 unsigned char sig_no; /* TL_SETUP_SIGNO_CONFIG_MSG */
53 unsigned char port_no;
54 unsigned char prio_data;
55 unsigned char prio_ctrl;
56} __attribute__ ((__packed__));
57
58struct tl_setup_config_done_msg {
59 unsigned char sig_no; /* TL_SETUP_SIGNO_CONFIG_DONE_MSG */
60} __attribute__ ((__packed__));
61
62/* Asyncronous messages */
63struct tl_setup_open_msg {
64 unsigned char sig_no; /* TL_SETUP_SIGNO_OPEN_MSG */
65 unsigned char port_no;
66} __attribute__ ((__packed__));
67
68struct tl_setup_close_msg {
69 unsigned char sig_no; /* TL_SETUP_SIGNO_CLOSE_MSG */
70 unsigned char port_no;
71} __attribute__ ((__packed__));
72
73/* Driver type - for use in tl_setup_info_msg.driver_type */
74#define COMM_DRIVER 0
75#define NDISWAN_DRIVER 1
76#define NDISWAN_DRIVER_MAJOR_VERSION 2
77#define NDISWAN_DRIVER_MINOR_VERSION 0
78
79/*
80 * It should not matter when this message comes over as we just store the
81 * results and send the ACK.
82 */
83struct tl_setup_info_msg {
84 unsigned char sig_no; /* TL_SETUP_SIGNO_INFO_MSG */
85 unsigned char driver_type;
86 unsigned char major_version;
87 unsigned char minor_version;
88} __attribute__ ((__packed__));
89
90struct tl_setup_info_msgAck {
91 unsigned char sig_no; /* TL_SETUP_SIGNO_INFO_MSG_ACK */
92} __attribute__ ((__packed__));
93
94struct TlSetupRebootMsgAck {
95 unsigned char sig_no; /* TL_SETUP_SIGNO_REBOOT_MSG_ACK */
96} __attribute__ ((__packed__));
97
98/* Define a union of all the msgs that the driver can receive from the card.*/
99union ipw_setup_rx_msg {
100 unsigned char sig_no;
101 struct tl_setup_get_version_rsp version_rsp_msg;
102 struct tl_setup_open_msg open_msg;
103 struct tl_setup_close_msg close_msg;
104 struct tl_setup_info_msg InfoMsg;
105 struct tl_setup_info_msgAck info_msg_ack;
106} __attribute__ ((__packed__));
107
108#endif /* _IPWIRELESS_CS_SETUP_PROTOCOL_H_ */
diff --git a/drivers/tty/ipwireless/tty.c b/drivers/tty/ipwireless/tty.c
new file mode 100644
index 000000000000..ef92869502a7
--- /dev/null
+++ b/drivers/tty/ipwireless/tty.c
@@ -0,0 +1,679 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/ppp_defs.h>
23#include <linux/if.h>
24#include <linux/if_ppp.h>
25#include <linux/sched.h>
26#include <linux/serial.h>
27#include <linux/slab.h>
28#include <linux/tty.h>
29#include <linux/tty_driver.h>
30#include <linux/tty_flip.h>
31#include <linux/uaccess.h>
32
33#include "tty.h"
34#include "network.h"
35#include "hardware.h"
36#include "main.h"
37
38#define IPWIRELESS_PCMCIA_START (0)
39#define IPWIRELESS_PCMCIA_MINORS (24)
40#define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
41
42#define TTYTYPE_MODEM (0)
43#define TTYTYPE_MONITOR (1)
44#define TTYTYPE_RAS_RAW (2)
45
46struct ipw_tty {
47 int index;
48 struct ipw_hardware *hardware;
49 unsigned int channel_idx;
50 unsigned int secondary_channel_idx;
51 int tty_type;
52 struct ipw_network *network;
53 struct tty_struct *linux_tty;
54 int open_count;
55 unsigned int control_lines;
56 struct mutex ipw_tty_mutex;
57 int tx_bytes_queued;
58 int closing;
59};
60
61static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
62
63static struct tty_driver *ipw_tty_driver;
64
65static char *tty_type_name(int tty_type)
66{
67 static char *channel_names[] = {
68 "modem",
69 "monitor",
70 "RAS-raw"
71 };
72
73 return channel_names[tty_type];
74}
75
76static void report_registering(struct ipw_tty *tty)
77{
78 char *iftype = tty_type_name(tty->tty_type);
79
80 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
81 ": registering %s device ttyIPWp%d\n", iftype, tty->index);
82}
83
84static void report_deregistering(struct ipw_tty *tty)
85{
86 char *iftype = tty_type_name(tty->tty_type);
87
88 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
89 ": deregistering %s device ttyIPWp%d\n", iftype,
90 tty->index);
91}
92
93static struct ipw_tty *get_tty(int minor)
94{
95 if (minor < ipw_tty_driver->minor_start
96 || minor >= ipw_tty_driver->minor_start +
97 IPWIRELESS_PCMCIA_MINORS)
98 return NULL;
99 else {
100 int minor_offset = minor - ipw_tty_driver->minor_start;
101
102 /*
103 * The 'ras_raw' channel is only available when 'loopback' mode
104 * is enabled.
105 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
106 */
107 if (!ipwireless_loopback &&
108 minor_offset >=
109 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
110 return NULL;
111
112 return ttys[minor_offset];
113 }
114}
115
116static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
117{
118 int minor = linux_tty->index;
119 struct ipw_tty *tty = get_tty(minor);
120
121 if (!tty)
122 return -ENODEV;
123
124 mutex_lock(&tty->ipw_tty_mutex);
125
126 if (tty->closing) {
127 mutex_unlock(&tty->ipw_tty_mutex);
128 return -ENODEV;
129 }
130 if (tty->open_count == 0)
131 tty->tx_bytes_queued = 0;
132
133 tty->open_count++;
134
135 tty->linux_tty = linux_tty;
136 linux_tty->driver_data = tty;
137 linux_tty->low_latency = 1;
138
139 if (tty->tty_type == TTYTYPE_MODEM)
140 ipwireless_ppp_open(tty->network);
141
142 mutex_unlock(&tty->ipw_tty_mutex);
143
144 return 0;
145}
146
147static void do_ipw_close(struct ipw_tty *tty)
148{
149 tty->open_count--;
150
151 if (tty->open_count == 0) {
152 struct tty_struct *linux_tty = tty->linux_tty;
153
154 if (linux_tty != NULL) {
155 tty->linux_tty = NULL;
156 linux_tty->driver_data = NULL;
157
158 if (tty->tty_type == TTYTYPE_MODEM)
159 ipwireless_ppp_close(tty->network);
160 }
161 }
162}
163
164static void ipw_hangup(struct tty_struct *linux_tty)
165{
166 struct ipw_tty *tty = linux_tty->driver_data;
167
168 if (!tty)
169 return;
170
171 mutex_lock(&tty->ipw_tty_mutex);
172 if (tty->open_count == 0) {
173 mutex_unlock(&tty->ipw_tty_mutex);
174 return;
175 }
176
177 do_ipw_close(tty);
178
179 mutex_unlock(&tty->ipw_tty_mutex);
180}
181
182static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
183{
184 ipw_hangup(linux_tty);
185}
186
187/* Take data received from hardware, and send it out the tty */
188void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
189 unsigned int length)
190{
191 struct tty_struct *linux_tty;
192 int work = 0;
193
194 mutex_lock(&tty->ipw_tty_mutex);
195 linux_tty = tty->linux_tty;
196 if (linux_tty == NULL) {
197 mutex_unlock(&tty->ipw_tty_mutex);
198 return;
199 }
200
201 if (!tty->open_count) {
202 mutex_unlock(&tty->ipw_tty_mutex);
203 return;
204 }
205 mutex_unlock(&tty->ipw_tty_mutex);
206
207 work = tty_insert_flip_string(linux_tty, data, length);
208
209 if (work != length)
210 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
211 ": %d chars not inserted to flip buffer!\n",
212 length - work);
213
214 /*
215 * This may sleep if ->low_latency is set
216 */
217 if (work)
218 tty_flip_buffer_push(linux_tty);
219}
220
221static void ipw_write_packet_sent_callback(void *callback_data,
222 unsigned int packet_length)
223{
224 struct ipw_tty *tty = callback_data;
225
226 /*
227 * Packet has been sent, so we subtract the number of bytes from our
228 * tally of outstanding TX bytes.
229 */
230 tty->tx_bytes_queued -= packet_length;
231}
232
233static int ipw_write(struct tty_struct *linux_tty,
234 const unsigned char *buf, int count)
235{
236 struct ipw_tty *tty = linux_tty->driver_data;
237 int room, ret;
238
239 if (!tty)
240 return -ENODEV;
241
242 mutex_lock(&tty->ipw_tty_mutex);
243 if (!tty->open_count) {
244 mutex_unlock(&tty->ipw_tty_mutex);
245 return -EINVAL;
246 }
247
248 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
249 if (room < 0)
250 room = 0;
251 /* Don't allow caller to write any more than we have room for */
252 if (count > room)
253 count = room;
254
255 if (count == 0) {
256 mutex_unlock(&tty->ipw_tty_mutex);
257 return 0;
258 }
259
260 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
261 buf, count,
262 ipw_write_packet_sent_callback, tty);
263 if (ret == -1) {
264 mutex_unlock(&tty->ipw_tty_mutex);
265 return 0;
266 }
267
268 tty->tx_bytes_queued += count;
269 mutex_unlock(&tty->ipw_tty_mutex);
270
271 return count;
272}
273
274static int ipw_write_room(struct tty_struct *linux_tty)
275{
276 struct ipw_tty *tty = linux_tty->driver_data;
277 int room;
278
279 /* FIXME: Exactly how is the tty object locked here .. */
280 if (!tty)
281 return -ENODEV;
282
283 if (!tty->open_count)
284 return -EINVAL;
285
286 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
287 if (room < 0)
288 room = 0;
289
290 return room;
291}
292
293static int ipwireless_get_serial_info(struct ipw_tty *tty,
294 struct serial_struct __user *retinfo)
295{
296 struct serial_struct tmp;
297
298 if (!retinfo)
299 return (-EFAULT);
300
301 memset(&tmp, 0, sizeof(tmp));
302 tmp.type = PORT_UNKNOWN;
303 tmp.line = tty->index;
304 tmp.port = 0;
305 tmp.irq = 0;
306 tmp.flags = 0;
307 tmp.baud_base = 115200;
308 tmp.close_delay = 0;
309 tmp.closing_wait = 0;
310 tmp.custom_divisor = 0;
311 tmp.hub6 = 0;
312 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
313 return -EFAULT;
314
315 return 0;
316}
317
318static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
319{
320 struct ipw_tty *tty = linux_tty->driver_data;
321
322 if (!tty)
323 return 0;
324
325 if (!tty->open_count)
326 return 0;
327
328 return tty->tx_bytes_queued;
329}
330
331static int get_control_lines(struct ipw_tty *tty)
332{
333 unsigned int my = tty->control_lines;
334 unsigned int out = 0;
335
336 if (my & IPW_CONTROL_LINE_RTS)
337 out |= TIOCM_RTS;
338 if (my & IPW_CONTROL_LINE_DTR)
339 out |= TIOCM_DTR;
340 if (my & IPW_CONTROL_LINE_CTS)
341 out |= TIOCM_CTS;
342 if (my & IPW_CONTROL_LINE_DSR)
343 out |= TIOCM_DSR;
344 if (my & IPW_CONTROL_LINE_DCD)
345 out |= TIOCM_CD;
346
347 return out;
348}
349
350static int set_control_lines(struct ipw_tty *tty, unsigned int set,
351 unsigned int clear)
352{
353 int ret;
354
355 if (set & TIOCM_RTS) {
356 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
357 if (ret)
358 return ret;
359 if (tty->secondary_channel_idx != -1) {
360 ret = ipwireless_set_RTS(tty->hardware,
361 tty->secondary_channel_idx, 1);
362 if (ret)
363 return ret;
364 }
365 }
366 if (set & TIOCM_DTR) {
367 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
368 if (ret)
369 return ret;
370 if (tty->secondary_channel_idx != -1) {
371 ret = ipwireless_set_DTR(tty->hardware,
372 tty->secondary_channel_idx, 1);
373 if (ret)
374 return ret;
375 }
376 }
377 if (clear & TIOCM_RTS) {
378 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
379 if (tty->secondary_channel_idx != -1) {
380 ret = ipwireless_set_RTS(tty->hardware,
381 tty->secondary_channel_idx, 0);
382 if (ret)
383 return ret;
384 }
385 }
386 if (clear & TIOCM_DTR) {
387 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
388 if (tty->secondary_channel_idx != -1) {
389 ret = ipwireless_set_DTR(tty->hardware,
390 tty->secondary_channel_idx, 0);
391 if (ret)
392 return ret;
393 }
394 }
395 return 0;
396}
397
398static int ipw_tiocmget(struct tty_struct *linux_tty)
399{
400 struct ipw_tty *tty = linux_tty->driver_data;
401 /* FIXME: Exactly how is the tty object locked here .. */
402
403 if (!tty)
404 return -ENODEV;
405
406 if (!tty->open_count)
407 return -EINVAL;
408
409 return get_control_lines(tty);
410}
411
412static int
413ipw_tiocmset(struct tty_struct *linux_tty,
414 unsigned int set, unsigned int clear)
415{
416 struct ipw_tty *tty = linux_tty->driver_data;
417 /* FIXME: Exactly how is the tty object locked here .. */
418
419 if (!tty)
420 return -ENODEV;
421
422 if (!tty->open_count)
423 return -EINVAL;
424
425 return set_control_lines(tty, set, clear);
426}
427
428static int ipw_ioctl(struct tty_struct *linux_tty,
429 unsigned int cmd, unsigned long arg)
430{
431 struct ipw_tty *tty = linux_tty->driver_data;
432
433 if (!tty)
434 return -ENODEV;
435
436 if (!tty->open_count)
437 return -EINVAL;
438
439 /* FIXME: Exactly how is the tty object locked here .. */
440
441 switch (cmd) {
442 case TIOCGSERIAL:
443 return ipwireless_get_serial_info(tty, (void __user *) arg);
444
445 case TIOCSSERIAL:
446 return 0; /* Keeps the PCMCIA scripts happy. */
447 }
448
449 if (tty->tty_type == TTYTYPE_MODEM) {
450 switch (cmd) {
451 case PPPIOCGCHAN:
452 {
453 int chan = ipwireless_ppp_channel_index(
454 tty->network);
455
456 if (chan < 0)
457 return -ENODEV;
458 if (put_user(chan, (int __user *) arg))
459 return -EFAULT;
460 }
461 return 0;
462
463 case PPPIOCGUNIT:
464 {
465 int unit = ipwireless_ppp_unit_number(
466 tty->network);
467
468 if (unit < 0)
469 return -ENODEV;
470 if (put_user(unit, (int __user *) arg))
471 return -EFAULT;
472 }
473 return 0;
474
475 case FIONREAD:
476 {
477 int val = 0;
478
479 if (put_user(val, (int __user *) arg))
480 return -EFAULT;
481 }
482 return 0;
483 case TCFLSH:
484 return tty_perform_flush(linux_tty, arg);
485 }
486 }
487 return -ENOIOCTLCMD;
488}
489
490static int add_tty(int j,
491 struct ipw_hardware *hardware,
492 struct ipw_network *network, int channel_idx,
493 int secondary_channel_idx, int tty_type)
494{
495 ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
496 if (!ttys[j])
497 return -ENOMEM;
498 ttys[j]->index = j;
499 ttys[j]->hardware = hardware;
500 ttys[j]->channel_idx = channel_idx;
501 ttys[j]->secondary_channel_idx = secondary_channel_idx;
502 ttys[j]->network = network;
503 ttys[j]->tty_type = tty_type;
504 mutex_init(&ttys[j]->ipw_tty_mutex);
505
506 tty_register_device(ipw_tty_driver, j, NULL);
507 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
508
509 if (secondary_channel_idx != -1)
510 ipwireless_associate_network_tty(network,
511 secondary_channel_idx,
512 ttys[j]);
513 if (get_tty(j + ipw_tty_driver->minor_start) == ttys[j])
514 report_registering(ttys[j]);
515 return 0;
516}
517
518struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
519 struct ipw_network *network)
520{
521 int i, j;
522
523 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
524 int allfree = 1;
525
526 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
527 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
528 if (ttys[j] != NULL) {
529 allfree = 0;
530 break;
531 }
532
533 if (allfree) {
534 j = i;
535
536 if (add_tty(j, hardware, network,
537 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
538 TTYTYPE_MODEM))
539 return NULL;
540
541 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
542 if (add_tty(j, hardware, network,
543 IPW_CHANNEL_DIALLER, -1,
544 TTYTYPE_MONITOR))
545 return NULL;
546
547 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
548 if (add_tty(j, hardware, network,
549 IPW_CHANNEL_RAS, -1,
550 TTYTYPE_RAS_RAW))
551 return NULL;
552
553 return ttys[i];
554 }
555 }
556 return NULL;
557}
558
559/*
560 * Must be called before ipwireless_network_free().
561 */
562void ipwireless_tty_free(struct ipw_tty *tty)
563{
564 int j;
565 struct ipw_network *network = ttys[tty->index]->network;
566
567 for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
568 j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
569 struct ipw_tty *ttyj = ttys[j];
570
571 if (ttyj) {
572 mutex_lock(&ttyj->ipw_tty_mutex);
573 if (get_tty(j + ipw_tty_driver->minor_start) == ttyj)
574 report_deregistering(ttyj);
575 ttyj->closing = 1;
576 if (ttyj->linux_tty != NULL) {
577 mutex_unlock(&ttyj->ipw_tty_mutex);
578 tty_hangup(ttyj->linux_tty);
579 /* Wait till the tty_hangup has completed */
580 flush_work_sync(&ttyj->linux_tty->hangup_work);
581 /* FIXME: Exactly how is the tty object locked here
582 against a parallel ioctl etc */
583 mutex_lock(&ttyj->ipw_tty_mutex);
584 }
585 while (ttyj->open_count)
586 do_ipw_close(ttyj);
587 ipwireless_disassociate_network_ttys(network,
588 ttyj->channel_idx);
589 tty_unregister_device(ipw_tty_driver, j);
590 ttys[j] = NULL;
591 mutex_unlock(&ttyj->ipw_tty_mutex);
592 kfree(ttyj);
593 }
594 }
595}
596
597static const struct tty_operations tty_ops = {
598 .open = ipw_open,
599 .close = ipw_close,
600 .hangup = ipw_hangup,
601 .write = ipw_write,
602 .write_room = ipw_write_room,
603 .ioctl = ipw_ioctl,
604 .chars_in_buffer = ipw_chars_in_buffer,
605 .tiocmget = ipw_tiocmget,
606 .tiocmset = ipw_tiocmset,
607};
608
609int ipwireless_tty_init(void)
610{
611 int result;
612
613 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
614 if (!ipw_tty_driver)
615 return -ENOMEM;
616
617 ipw_tty_driver->owner = THIS_MODULE;
618 ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
619 ipw_tty_driver->name = "ttyIPWp";
620 ipw_tty_driver->major = 0;
621 ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
622 ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
623 ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
624 ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
625 ipw_tty_driver->init_termios = tty_std_termios;
626 ipw_tty_driver->init_termios.c_cflag =
627 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
628 ipw_tty_driver->init_termios.c_ispeed = 9600;
629 ipw_tty_driver->init_termios.c_ospeed = 9600;
630 tty_set_operations(ipw_tty_driver, &tty_ops);
631 result = tty_register_driver(ipw_tty_driver);
632 if (result) {
633 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
634 ": failed to register tty driver\n");
635 put_tty_driver(ipw_tty_driver);
636 return result;
637 }
638
639 return 0;
640}
641
642void ipwireless_tty_release(void)
643{
644 int ret;
645
646 ret = tty_unregister_driver(ipw_tty_driver);
647 put_tty_driver(ipw_tty_driver);
648 if (ret != 0)
649 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
650 ": tty_unregister_driver failed with code %d\n", ret);
651}
652
653int ipwireless_tty_is_modem(struct ipw_tty *tty)
654{
655 return tty->tty_type == TTYTYPE_MODEM;
656}
657
658void
659ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
660 unsigned int channel_idx,
661 unsigned int control_lines,
662 unsigned int changed_mask)
663{
664 unsigned int old_control_lines = tty->control_lines;
665
666 tty->control_lines = (tty->control_lines & ~changed_mask)
667 | (control_lines & changed_mask);
668
669 /*
670 * If DCD is de-asserted, we close the tty so pppd can tell that we
671 * have gone offline.
672 */
673 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
674 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
675 && tty->linux_tty) {
676 tty_hangup(tty->linux_tty);
677 }
678}
679
diff --git a/drivers/tty/ipwireless/tty.h b/drivers/tty/ipwireless/tty.h
new file mode 100644
index 000000000000..747b2d637860
--- /dev/null
+++ b/drivers/tty/ipwireless/tty.h
@@ -0,0 +1,45 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_TTY_H_
19#define _IPWIRELESS_CS_TTY_H_
20
21#include <linux/types.h>
22#include <linux/sched.h>
23
24#include <pcmcia/cistpl.h>
25#include <pcmcia/ds.h>
26
27struct ipw_tty;
28struct ipw_network;
29struct ipw_hardware;
30
31int ipwireless_tty_init(void);
32void ipwireless_tty_release(void);
33
34struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hw,
35 struct ipw_network *net);
36void ipwireless_tty_free(struct ipw_tty *tty);
37void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
38 unsigned int length);
39int ipwireless_tty_is_modem(struct ipw_tty *tty);
40void ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
41 unsigned int channel_idx,
42 unsigned int control_lines,
43 unsigned int changed_mask);
44
45#endif
diff --git a/drivers/tty/isicom.c b/drivers/tty/isicom.c
new file mode 100644
index 000000000000..db1cf9c328d8
--- /dev/null
+++ b/drivers/tty/isicom.c
@@ -0,0 +1,1736 @@
1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Original driver code supplied by Multi-Tech
8 *
9 * Changes
10 * 1/9/98 alan@lxorguk.ukuu.org.uk
11 * Merge to 2.0.x kernel tree
12 * Obtain and use official major/minors
13 * Loader switched to a misc device
14 * (fixed range check bug as a side effect)
15 * Printk clean up
16 * 9/12/98 alan@lxorguk.ukuu.org.uk
17 * Rough port to 2.1.x
18 *
19 * 10/6/99 sameer Merged the ISA and PCI drivers to
20 * a new unified driver.
21 *
22 * 3/9/99 sameer Added support for ISI4616 cards.
23 *
24 * 16/9/99 sameer We do not force RTS low anymore.
25 * This is to prevent the firmware
26 * from getting confused.
27 *
28 * 26/10/99 sameer Cosmetic changes:The driver now
29 * dumps the Port Count information
30 * along with I/O address and IRQ.
31 *
32 * 13/12/99 sameer Fixed the problem with IRQ sharing.
33 *
34 * 10/5/00 sameer Fixed isicom_shutdown_board()
35 * to not lower DTR on all the ports
36 * when the last port on the card is
37 * closed.
38 *
39 * 10/5/00 sameer Signal mask setup command added
40 * to isicom_setup_port and
41 * isicom_shutdown_port.
42 *
43 * 24/5/00 sameer The driver is now SMP aware.
44 *
45 *
46 * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
47 *
48 *
49 * 03/01/01 anil .s Added support for resetting the
50 * internal modems on ISI cards.
51 *
52 * 08/02/01 anil .s Upgraded the driver for kernel
53 * 2.4.x
54 *
55 * 11/04/01 Kevin Fixed firmware load problem with
56 * ISIHP-4X card
57 *
58 * 30/04/01 anil .s Fixed the remote login through
59 * ISI port problem. Now the link
60 * does not go down before password
61 * prompt.
62 *
63 * 03/05/01 anil .s Fixed the problem with IRQ sharing
64 * among ISI-PCI cards.
65 *
66 * 03/05/01 anil .s Added support to display the version
67 * info during insmod as well as module
68 * listing by lsmod.
69 *
70 * 10/05/01 anil .s Done the modifications to the source
71 * file and Install script so that the
72 * same installation can be used for
73 * 2.2.x and 2.4.x kernel.
74 *
75 * 06/06/01 anil .s Now we drop both dtr and rts during
76 * shutdown_port as well as raise them
77 * during isicom_config_port.
78 *
79 * 09/06/01 acme@conectiva.com.br use capable, not suser, do
80 * restore_flags on failure in
81 * isicom_send_break, verify put_user
82 * result
83 *
84 * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
85 * Baud index extended to 21
86 *
87 * 20/03/03 ranjeeth Made to work for Linux Advanced server.
88 * Taken care of license warning.
89 *
90 * 10/12/03 Ravindra Made to work for Fedora Core 1 of
91 * Red Hat Distribution
92 *
93 * 06/01/05 Alan Cox Merged the ISI and base kernel strands
94 * into a single 2.6 driver
95 *
96 * ***********************************************************
97 *
98 * To use this driver you also need the support package. You
99 * can find this in RPM format on
100 * ftp://ftp.linux.org.uk/pub/linux/alan
101 *
102 * You can find the original tools for this direct from Multitech
103 * ftp://ftp.multitech.com/ISI-Cards/
104 *
105 * Having installed the cards the module options (/etc/modprobe.conf)
106 *
107 * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
108 *
109 * Omit those entries for boards you don't have installed.
110 *
111 * TODO
112 * Merge testing
113 * 64-bit verification
114 */
115
116#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
117
118#include <linux/module.h>
119#include <linux/firmware.h>
120#include <linux/kernel.h>
121#include <linux/tty.h>
122#include <linux/tty_flip.h>
123#include <linux/termios.h>
124#include <linux/fs.h>
125#include <linux/sched.h>
126#include <linux/serial.h>
127#include <linux/mm.h>
128#include <linux/interrupt.h>
129#include <linux/timer.h>
130#include <linux/delay.h>
131#include <linux/ioport.h>
132#include <linux/slab.h>
133
134#include <linux/uaccess.h>
135#include <linux/io.h>
136#include <asm/system.h>
137
138#include <linux/pci.h>
139
140#include <linux/isicom.h>
141
142#define InterruptTheCard(base) outw(0, (base) + 0xc)
143#define ClearInterrupt(base) inw((base) + 0x0a)
144
145#ifdef DEBUG
146#define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
147#else
148#define isicom_paranoia_check(a, b, c) 0
149#endif
150
151static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
152static void __devexit isicom_remove(struct pci_dev *);
153
154static struct pci_device_id isicom_pci_tbl[] = {
155 { PCI_DEVICE(VENDOR_ID, 0x2028) },
156 { PCI_DEVICE(VENDOR_ID, 0x2051) },
157 { PCI_DEVICE(VENDOR_ID, 0x2052) },
158 { PCI_DEVICE(VENDOR_ID, 0x2053) },
159 { PCI_DEVICE(VENDOR_ID, 0x2054) },
160 { PCI_DEVICE(VENDOR_ID, 0x2055) },
161 { PCI_DEVICE(VENDOR_ID, 0x2056) },
162 { PCI_DEVICE(VENDOR_ID, 0x2057) },
163 { PCI_DEVICE(VENDOR_ID, 0x2058) },
164 { 0 }
165};
166MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
167
168static struct pci_driver isicom_driver = {
169 .name = "isicom",
170 .id_table = isicom_pci_tbl,
171 .probe = isicom_probe,
172 .remove = __devexit_p(isicom_remove)
173};
174
175static int prev_card = 3; /* start servicing isi_card[0] */
176static struct tty_driver *isicom_normal;
177
178static void isicom_tx(unsigned long _data);
179static void isicom_start(struct tty_struct *tty);
180
181static DEFINE_TIMER(tx, isicom_tx, 0, 0);
182
183/* baud index mappings from linux defns to isi */
184
185static signed char linuxb_to_isib[] = {
186 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
187};
188
189struct isi_board {
190 unsigned long base;
191 int irq;
192 unsigned char port_count;
193 unsigned short status;
194 unsigned short port_status; /* each bit for each port */
195 unsigned short shift_count;
196 struct isi_port *ports;
197 signed char count;
198 spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
199 unsigned long flags;
200 unsigned int index;
201};
202
203struct isi_port {
204 unsigned short magic;
205 struct tty_port port;
206 u16 channel;
207 u16 status;
208 struct isi_board *card;
209 unsigned char *xmit_buf;
210 int xmit_head;
211 int xmit_tail;
212 int xmit_cnt;
213};
214
215static struct isi_board isi_card[BOARD_COUNT];
216static struct isi_port isi_ports[PORT_COUNT];
217
218/*
219 * Locking functions for card level locking. We need to own both
220 * the kernel lock for the card and have the card in a position that
221 * it wants to talk.
222 */
223
224static inline int WaitTillCardIsFree(unsigned long base)
225{
226 unsigned int count = 0;
227 unsigned int a = in_atomic(); /* do we run under spinlock? */
228
229 while (!(inw(base + 0xe) & 0x1) && count++ < 100)
230 if (a)
231 mdelay(1);
232 else
233 msleep(1);
234
235 return !(inw(base + 0xe) & 0x1);
236}
237
238static int lock_card(struct isi_board *card)
239{
240 unsigned long base = card->base;
241 unsigned int retries, a;
242
243 for (retries = 0; retries < 10; retries++) {
244 spin_lock_irqsave(&card->card_lock, card->flags);
245 for (a = 0; a < 10; a++) {
246 if (inw(base + 0xe) & 0x1)
247 return 1;
248 udelay(10);
249 }
250 spin_unlock_irqrestore(&card->card_lock, card->flags);
251 msleep(10);
252 }
253 pr_warning("Failed to lock Card (0x%lx)\n", card->base);
254
255 return 0; /* Failed to acquire the card! */
256}
257
258static void unlock_card(struct isi_board *card)
259{
260 spin_unlock_irqrestore(&card->card_lock, card->flags);
261}
262
263/*
264 * ISI Card specific ops ...
265 */
266
267/* card->lock HAS to be held */
268static void raise_dtr(struct isi_port *port)
269{
270 struct isi_board *card = port->card;
271 unsigned long base = card->base;
272 u16 channel = port->channel;
273
274 if (WaitTillCardIsFree(base))
275 return;
276
277 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
278 outw(0x0504, base);
279 InterruptTheCard(base);
280 port->status |= ISI_DTR;
281}
282
283/* card->lock HAS to be held */
284static inline void drop_dtr(struct isi_port *port)
285{
286 struct isi_board *card = port->card;
287 unsigned long base = card->base;
288 u16 channel = port->channel;
289
290 if (WaitTillCardIsFree(base))
291 return;
292
293 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
294 outw(0x0404, base);
295 InterruptTheCard(base);
296 port->status &= ~ISI_DTR;
297}
298
299/* card->lock HAS to be held */
300static inline void raise_rts(struct isi_port *port)
301{
302 struct isi_board *card = port->card;
303 unsigned long base = card->base;
304 u16 channel = port->channel;
305
306 if (WaitTillCardIsFree(base))
307 return;
308
309 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
310 outw(0x0a04, base);
311 InterruptTheCard(base);
312 port->status |= ISI_RTS;
313}
314
315/* card->lock HAS to be held */
316static inline void drop_rts(struct isi_port *port)
317{
318 struct isi_board *card = port->card;
319 unsigned long base = card->base;
320 u16 channel = port->channel;
321
322 if (WaitTillCardIsFree(base))
323 return;
324
325 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
326 outw(0x0804, base);
327 InterruptTheCard(base);
328 port->status &= ~ISI_RTS;
329}
330
331/* card->lock MUST NOT be held */
332
333static void isicom_dtr_rts(struct tty_port *port, int on)
334{
335 struct isi_port *ip = container_of(port, struct isi_port, port);
336 struct isi_board *card = ip->card;
337 unsigned long base = card->base;
338 u16 channel = ip->channel;
339
340 if (!lock_card(card))
341 return;
342
343 if (on) {
344 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
345 outw(0x0f04, base);
346 InterruptTheCard(base);
347 ip->status |= (ISI_DTR | ISI_RTS);
348 } else {
349 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
350 outw(0x0C04, base);
351 InterruptTheCard(base);
352 ip->status &= ~(ISI_DTR | ISI_RTS);
353 }
354 unlock_card(card);
355}
356
357/* card->lock HAS to be held */
358static void drop_dtr_rts(struct isi_port *port)
359{
360 struct isi_board *card = port->card;
361 unsigned long base = card->base;
362 u16 channel = port->channel;
363
364 if (WaitTillCardIsFree(base))
365 return;
366
367 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
368 outw(0x0c04, base);
369 InterruptTheCard(base);
370 port->status &= ~(ISI_RTS | ISI_DTR);
371}
372
373/*
374 * ISICOM Driver specific routines ...
375 *
376 */
377
378static inline int __isicom_paranoia_check(struct isi_port const *port,
379 char *name, const char *routine)
380{
381 if (!port) {
382 pr_warning("Warning: bad isicom magic for dev %s in %s.\n",
383 name, routine);
384 return 1;
385 }
386 if (port->magic != ISICOM_MAGIC) {
387 pr_warning("Warning: NULL isicom port for dev %s in %s.\n",
388 name, routine);
389 return 1;
390 }
391
392 return 0;
393}
394
395/*
396 * Transmitter.
397 *
398 * We shovel data into the card buffers on a regular basis. The card
399 * will do the rest of the work for us.
400 */
401
402static void isicom_tx(unsigned long _data)
403{
404 unsigned long flags, base;
405 unsigned int retries;
406 short count = (BOARD_COUNT-1), card;
407 short txcount, wrd, residue, word_count, cnt;
408 struct isi_port *port;
409 struct tty_struct *tty;
410
411 /* find next active board */
412 card = (prev_card + 1) & 0x0003;
413 while (count-- > 0) {
414 if (isi_card[card].status & BOARD_ACTIVE)
415 break;
416 card = (card + 1) & 0x0003;
417 }
418 if (!(isi_card[card].status & BOARD_ACTIVE))
419 goto sched_again;
420
421 prev_card = card;
422
423 count = isi_card[card].port_count;
424 port = isi_card[card].ports;
425 base = isi_card[card].base;
426
427 spin_lock_irqsave(&isi_card[card].card_lock, flags);
428 for (retries = 0; retries < 100; retries++) {
429 if (inw(base + 0xe) & 0x1)
430 break;
431 udelay(2);
432 }
433 if (retries >= 100)
434 goto unlock;
435
436 tty = tty_port_tty_get(&port->port);
437 if (tty == NULL)
438 goto put_unlock;
439
440 for (; count > 0; count--, port++) {
441 /* port not active or tx disabled to force flow control */
442 if (!(port->port.flags & ASYNC_INITIALIZED) ||
443 !(port->status & ISI_TXOK))
444 continue;
445
446 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
447 if (txcount <= 0 || tty->stopped || tty->hw_stopped)
448 continue;
449
450 if (!(inw(base + 0x02) & (1 << port->channel)))
451 continue;
452
453 pr_debug("txing %d bytes, port%d.\n",
454 txcount, port->channel + 1);
455 outw((port->channel << isi_card[card].shift_count) | txcount,
456 base);
457 residue = NO;
458 wrd = 0;
459 while (1) {
460 cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
461 - port->xmit_tail));
462 if (residue == YES) {
463 residue = NO;
464 if (cnt > 0) {
465 wrd |= (port->port.xmit_buf[port->xmit_tail]
466 << 8);
467 port->xmit_tail = (port->xmit_tail + 1)
468 & (SERIAL_XMIT_SIZE - 1);
469 port->xmit_cnt--;
470 txcount--;
471 cnt--;
472 outw(wrd, base);
473 } else {
474 outw(wrd, base);
475 break;
476 }
477 }
478 if (cnt <= 0)
479 break;
480 word_count = cnt >> 1;
481 outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
482 port->xmit_tail = (port->xmit_tail
483 + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
484 txcount -= (word_count << 1);
485 port->xmit_cnt -= (word_count << 1);
486 if (cnt & 0x0001) {
487 residue = YES;
488 wrd = port->port.xmit_buf[port->xmit_tail];
489 port->xmit_tail = (port->xmit_tail + 1)
490 & (SERIAL_XMIT_SIZE - 1);
491 port->xmit_cnt--;
492 txcount--;
493 }
494 }
495
496 InterruptTheCard(base);
497 if (port->xmit_cnt <= 0)
498 port->status &= ~ISI_TXOK;
499 if (port->xmit_cnt <= WAKEUP_CHARS)
500 tty_wakeup(tty);
501 }
502
503put_unlock:
504 tty_kref_put(tty);
505unlock:
506 spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
507 /* schedule another tx for hopefully in about 10ms */
508sched_again:
509 mod_timer(&tx, jiffies + msecs_to_jiffies(10));
510}
511
512/*
513 * Main interrupt handler routine
514 */
515
516static irqreturn_t isicom_interrupt(int irq, void *dev_id)
517{
518 struct isi_board *card = dev_id;
519 struct isi_port *port;
520 struct tty_struct *tty;
521 unsigned long base;
522 u16 header, word_count, count, channel;
523 short byte_count;
524 unsigned char *rp;
525
526 if (!card || !(card->status & FIRMWARE_LOADED))
527 return IRQ_NONE;
528
529 base = card->base;
530
531 /* did the card interrupt us? */
532 if (!(inw(base + 0x0e) & 0x02))
533 return IRQ_NONE;
534
535 spin_lock(&card->card_lock);
536
537 /*
538 * disable any interrupts from the PCI card and lower the
539 * interrupt line
540 */
541 outw(0x8000, base+0x04);
542 ClearInterrupt(base);
543
544 inw(base); /* get the dummy word out */
545 header = inw(base);
546 channel = (header & 0x7800) >> card->shift_count;
547 byte_count = header & 0xff;
548
549 if (channel + 1 > card->port_count) {
550 pr_warning("%s(0x%lx): %d(channel) > port_count.\n",
551 __func__, base, channel+1);
552 outw(0x0000, base+0x04); /* enable interrupts */
553 spin_unlock(&card->card_lock);
554 return IRQ_HANDLED;
555 }
556 port = card->ports + channel;
557 if (!(port->port.flags & ASYNC_INITIALIZED)) {
558 outw(0x0000, base+0x04); /* enable interrupts */
559 spin_unlock(&card->card_lock);
560 return IRQ_HANDLED;
561 }
562
563 tty = tty_port_tty_get(&port->port);
564 if (tty == NULL) {
565 word_count = byte_count >> 1;
566 while (byte_count > 1) {
567 inw(base);
568 byte_count -= 2;
569 }
570 if (byte_count & 0x01)
571 inw(base);
572 outw(0x0000, base+0x04); /* enable interrupts */
573 spin_unlock(&card->card_lock);
574 return IRQ_HANDLED;
575 }
576
577 if (header & 0x8000) { /* Status Packet */
578 header = inw(base);
579 switch (header & 0xff) {
580 case 0: /* Change in EIA signals */
581 if (port->port.flags & ASYNC_CHECK_CD) {
582 if (port->status & ISI_DCD) {
583 if (!(header & ISI_DCD)) {
584 /* Carrier has been lost */
585 pr_debug("%s: DCD->low.\n",
586 __func__);
587 port->status &= ~ISI_DCD;
588 tty_hangup(tty);
589 }
590 } else if (header & ISI_DCD) {
591 /* Carrier has been detected */
592 pr_debug("%s: DCD->high.\n",
593 __func__);
594 port->status |= ISI_DCD;
595 wake_up_interruptible(&port->port.open_wait);
596 }
597 } else {
598 if (header & ISI_DCD)
599 port->status |= ISI_DCD;
600 else
601 port->status &= ~ISI_DCD;
602 }
603
604 if (port->port.flags & ASYNC_CTS_FLOW) {
605 if (tty->hw_stopped) {
606 if (header & ISI_CTS) {
607 port->port.tty->hw_stopped = 0;
608 /* start tx ing */
609 port->status |= (ISI_TXOK
610 | ISI_CTS);
611 tty_wakeup(tty);
612 }
613 } else if (!(header & ISI_CTS)) {
614 tty->hw_stopped = 1;
615 /* stop tx ing */
616 port->status &= ~(ISI_TXOK | ISI_CTS);
617 }
618 } else {
619 if (header & ISI_CTS)
620 port->status |= ISI_CTS;
621 else
622 port->status &= ~ISI_CTS;
623 }
624
625 if (header & ISI_DSR)
626 port->status |= ISI_DSR;
627 else
628 port->status &= ~ISI_DSR;
629
630 if (header & ISI_RI)
631 port->status |= ISI_RI;
632 else
633 port->status &= ~ISI_RI;
634
635 break;
636
637 case 1: /* Received Break !!! */
638 tty_insert_flip_char(tty, 0, TTY_BREAK);
639 if (port->port.flags & ASYNC_SAK)
640 do_SAK(tty);
641 tty_flip_buffer_push(tty);
642 break;
643
644 case 2: /* Statistics */
645 pr_debug("%s: stats!!!\n", __func__);
646 break;
647
648 default:
649 pr_debug("%s: Unknown code in status packet.\n",
650 __func__);
651 break;
652 }
653 } else { /* Data Packet */
654
655 count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
656 pr_debug("%s: Can rx %d of %d bytes.\n",
657 __func__, count, byte_count);
658 word_count = count >> 1;
659 insw(base, rp, word_count);
660 byte_count -= (word_count << 1);
661 if (count & 0x0001) {
662 tty_insert_flip_char(tty, inw(base) & 0xff,
663 TTY_NORMAL);
664 byte_count -= 2;
665 }
666 if (byte_count > 0) {
667 pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
668 __func__, base, channel + 1);
669 /* drain out unread xtra data */
670 while (byte_count > 0) {
671 inw(base);
672 byte_count -= 2;
673 }
674 }
675 tty_flip_buffer_push(tty);
676 }
677 outw(0x0000, base+0x04); /* enable interrupts */
678 spin_unlock(&card->card_lock);
679 tty_kref_put(tty);
680
681 return IRQ_HANDLED;
682}
683
684static void isicom_config_port(struct tty_struct *tty)
685{
686 struct isi_port *port = tty->driver_data;
687 struct isi_board *card = port->card;
688 unsigned long baud;
689 unsigned long base = card->base;
690 u16 channel_setup, channel = port->channel,
691 shift_count = card->shift_count;
692 unsigned char flow_ctrl;
693
694 /* FIXME: Switch to new tty baud API */
695 baud = C_BAUD(tty);
696 if (baud & CBAUDEX) {
697 baud &= ~CBAUDEX;
698
699 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
700 * then the card is programmed for 57.6Kbps or 115Kbps
701 * respectively.
702 */
703
704 /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
705 if (baud < 1 || baud > 4)
706 tty->termios->c_cflag &= ~CBAUDEX;
707 else
708 baud += 15;
709 }
710 if (baud == 15) {
711
712 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
713 * by the set_serial_info ioctl ... this is done by
714 * the 'setserial' utility.
715 */
716
717 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
718 baud++; /* 57.6 Kbps */
719 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
720 baud += 2; /* 115 Kbps */
721 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
722 baud += 3; /* 230 kbps*/
723 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
724 baud += 4; /* 460 kbps*/
725 }
726 if (linuxb_to_isib[baud] == -1) {
727 /* hang up */
728 drop_dtr(port);
729 return;
730 } else
731 raise_dtr(port);
732
733 if (WaitTillCardIsFree(base) == 0) {
734 outw(0x8000 | (channel << shift_count) | 0x03, base);
735 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
736 channel_setup = 0;
737 switch (C_CSIZE(tty)) {
738 case CS5:
739 channel_setup |= ISICOM_CS5;
740 break;
741 case CS6:
742 channel_setup |= ISICOM_CS6;
743 break;
744 case CS7:
745 channel_setup |= ISICOM_CS7;
746 break;
747 case CS8:
748 channel_setup |= ISICOM_CS8;
749 break;
750 }
751
752 if (C_CSTOPB(tty))
753 channel_setup |= ISICOM_2SB;
754 if (C_PARENB(tty)) {
755 channel_setup |= ISICOM_EVPAR;
756 if (C_PARODD(tty))
757 channel_setup |= ISICOM_ODPAR;
758 }
759 outw(channel_setup, base);
760 InterruptTheCard(base);
761 }
762 if (C_CLOCAL(tty))
763 port->port.flags &= ~ASYNC_CHECK_CD;
764 else
765 port->port.flags |= ASYNC_CHECK_CD;
766
767 /* flow control settings ...*/
768 flow_ctrl = 0;
769 port->port.flags &= ~ASYNC_CTS_FLOW;
770 if (C_CRTSCTS(tty)) {
771 port->port.flags |= ASYNC_CTS_FLOW;
772 flow_ctrl |= ISICOM_CTSRTS;
773 }
774 if (I_IXON(tty))
775 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
776 if (I_IXOFF(tty))
777 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
778
779 if (WaitTillCardIsFree(base) == 0) {
780 outw(0x8000 | (channel << shift_count) | 0x04, base);
781 outw(flow_ctrl << 8 | 0x05, base);
782 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
783 InterruptTheCard(base);
784 }
785
786 /* rx enabled -> enable port for rx on the card */
787 if (C_CREAD(tty)) {
788 card->port_status |= (1 << channel);
789 outw(card->port_status, base + 0x02);
790 }
791}
792
793/* open et all */
794
795static inline void isicom_setup_board(struct isi_board *bp)
796{
797 int channel;
798 struct isi_port *port;
799
800 bp->count++;
801 if (!(bp->status & BOARD_INIT)) {
802 port = bp->ports;
803 for (channel = 0; channel < bp->port_count; channel++, port++)
804 drop_dtr_rts(port);
805 }
806 bp->status |= BOARD_ACTIVE | BOARD_INIT;
807}
808
809/* Activate and thus setup board are protected from races against shutdown
810 by the tty_port mutex */
811
812static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
813{
814 struct isi_port *port = container_of(tport, struct isi_port, port);
815 struct isi_board *card = port->card;
816 unsigned long flags;
817
818 if (tty_port_alloc_xmit_buf(tport) < 0)
819 return -ENOMEM;
820
821 spin_lock_irqsave(&card->card_lock, flags);
822 isicom_setup_board(card);
823
824 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
825
826 /* discard any residual data */
827 if (WaitTillCardIsFree(card->base) == 0) {
828 outw(0x8000 | (port->channel << card->shift_count) | 0x02,
829 card->base);
830 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
831 InterruptTheCard(card->base);
832 }
833 isicom_config_port(tty);
834 spin_unlock_irqrestore(&card->card_lock, flags);
835
836 return 0;
837}
838
839static int isicom_carrier_raised(struct tty_port *port)
840{
841 struct isi_port *ip = container_of(port, struct isi_port, port);
842 return (ip->status & ISI_DCD)?1 : 0;
843}
844
845static struct tty_port *isicom_find_port(struct tty_struct *tty)
846{
847 struct isi_port *port;
848 struct isi_board *card;
849 unsigned int board;
850 int line = tty->index;
851
852 if (line < 0 || line > PORT_COUNT-1)
853 return NULL;
854 board = BOARD(line);
855 card = &isi_card[board];
856
857 if (!(card->status & FIRMWARE_LOADED))
858 return NULL;
859
860 /* open on a port greater than the port count for the card !!! */
861 if (line > ((board * 16) + card->port_count - 1))
862 return NULL;
863
864 port = &isi_ports[line];
865 if (isicom_paranoia_check(port, tty->name, "isicom_open"))
866 return NULL;
867
868 return &port->port;
869}
870
871static int isicom_open(struct tty_struct *tty, struct file *filp)
872{
873 struct isi_port *port;
874 struct tty_port *tport;
875
876 tport = isicom_find_port(tty);
877 if (tport == NULL)
878 return -ENODEV;
879 port = container_of(tport, struct isi_port, port);
880
881 tty->driver_data = port;
882 return tty_port_open(tport, tty, filp);
883}
884
885/* close et all */
886
887/* card->lock HAS to be held */
888static void isicom_shutdown_port(struct isi_port *port)
889{
890 struct isi_board *card = port->card;
891
892 if (--card->count < 0) {
893 pr_debug("%s: bad board(0x%lx) count %d.\n",
894 __func__, card->base, card->count);
895 card->count = 0;
896 }
897 /* last port was closed, shutdown that board too */
898 if (!card->count)
899 card->status &= BOARD_ACTIVE;
900}
901
902static void isicom_flush_buffer(struct tty_struct *tty)
903{
904 struct isi_port *port = tty->driver_data;
905 struct isi_board *card = port->card;
906 unsigned long flags;
907
908 if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
909 return;
910
911 spin_lock_irqsave(&card->card_lock, flags);
912 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
913 spin_unlock_irqrestore(&card->card_lock, flags);
914
915 tty_wakeup(tty);
916}
917
918static void isicom_shutdown(struct tty_port *port)
919{
920 struct isi_port *ip = container_of(port, struct isi_port, port);
921 struct isi_board *card = ip->card;
922 unsigned long flags;
923
924 /* indicate to the card that no more data can be received
925 on this port */
926 spin_lock_irqsave(&card->card_lock, flags);
927 card->port_status &= ~(1 << ip->channel);
928 outw(card->port_status, card->base + 0x02);
929 isicom_shutdown_port(ip);
930 spin_unlock_irqrestore(&card->card_lock, flags);
931 tty_port_free_xmit_buf(port);
932}
933
934static void isicom_close(struct tty_struct *tty, struct file *filp)
935{
936 struct isi_port *ip = tty->driver_data;
937 struct tty_port *port;
938
939 if (ip == NULL)
940 return;
941
942 port = &ip->port;
943 if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
944 return;
945 tty_port_close(port, tty, filp);
946}
947
948/* write et all */
949static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
950 int count)
951{
952 struct isi_port *port = tty->driver_data;
953 struct isi_board *card = port->card;
954 unsigned long flags;
955 int cnt, total = 0;
956
957 if (isicom_paranoia_check(port, tty->name, "isicom_write"))
958 return 0;
959
960 spin_lock_irqsave(&card->card_lock, flags);
961
962 while (1) {
963 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
964 - 1, SERIAL_XMIT_SIZE - port->xmit_head));
965 if (cnt <= 0)
966 break;
967
968 memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
969 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
970 - 1);
971 port->xmit_cnt += cnt;
972 buf += cnt;
973 count -= cnt;
974 total += cnt;
975 }
976 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
977 port->status |= ISI_TXOK;
978 spin_unlock_irqrestore(&card->card_lock, flags);
979 return total;
980}
981
982/* put_char et all */
983static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
984{
985 struct isi_port *port = tty->driver_data;
986 struct isi_board *card = port->card;
987 unsigned long flags;
988
989 if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
990 return 0;
991
992 spin_lock_irqsave(&card->card_lock, flags);
993 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
994 spin_unlock_irqrestore(&card->card_lock, flags);
995 return 0;
996 }
997
998 port->port.xmit_buf[port->xmit_head++] = ch;
999 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1000 port->xmit_cnt++;
1001 spin_unlock_irqrestore(&card->card_lock, flags);
1002 return 1;
1003}
1004
1005/* flush_chars et all */
1006static void isicom_flush_chars(struct tty_struct *tty)
1007{
1008 struct isi_port *port = tty->driver_data;
1009
1010 if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1011 return;
1012
1013 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1014 !port->port.xmit_buf)
1015 return;
1016
1017 /* this tells the transmitter to consider this port for
1018 data output to the card ... that's the best we can do. */
1019 port->status |= ISI_TXOK;
1020}
1021
1022/* write_room et all */
1023static int isicom_write_room(struct tty_struct *tty)
1024{
1025 struct isi_port *port = tty->driver_data;
1026 int free;
1027
1028 if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1029 return 0;
1030
1031 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1032 if (free < 0)
1033 free = 0;
1034 return free;
1035}
1036
1037/* chars_in_buffer et all */
1038static int isicom_chars_in_buffer(struct tty_struct *tty)
1039{
1040 struct isi_port *port = tty->driver_data;
1041 if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1042 return 0;
1043 return port->xmit_cnt;
1044}
1045
1046/* ioctl et all */
1047static int isicom_send_break(struct tty_struct *tty, int length)
1048{
1049 struct isi_port *port = tty->driver_data;
1050 struct isi_board *card = port->card;
1051 unsigned long base = card->base;
1052
1053 if (length == -1)
1054 return -EOPNOTSUPP;
1055
1056 if (!lock_card(card))
1057 return -EINVAL;
1058
1059 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1060 outw((length & 0xff) << 8 | 0x00, base);
1061 outw((length & 0xff00), base);
1062 InterruptTheCard(base);
1063
1064 unlock_card(card);
1065 return 0;
1066}
1067
1068static int isicom_tiocmget(struct tty_struct *tty)
1069{
1070 struct isi_port *port = tty->driver_data;
1071 /* just send the port status */
1072 u16 status = port->status;
1073
1074 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1075 return -ENODEV;
1076
1077 return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1078 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1079 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1080 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1081 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1082 ((status & ISI_RI ) ? TIOCM_RI : 0);
1083}
1084
1085static int isicom_tiocmset(struct tty_struct *tty,
1086 unsigned int set, unsigned int clear)
1087{
1088 struct isi_port *port = tty->driver_data;
1089 unsigned long flags;
1090
1091 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1092 return -ENODEV;
1093
1094 spin_lock_irqsave(&port->card->card_lock, flags);
1095 if (set & TIOCM_RTS)
1096 raise_rts(port);
1097 if (set & TIOCM_DTR)
1098 raise_dtr(port);
1099
1100 if (clear & TIOCM_RTS)
1101 drop_rts(port);
1102 if (clear & TIOCM_DTR)
1103 drop_dtr(port);
1104 spin_unlock_irqrestore(&port->card->card_lock, flags);
1105
1106 return 0;
1107}
1108
1109static int isicom_set_serial_info(struct tty_struct *tty,
1110 struct serial_struct __user *info)
1111{
1112 struct isi_port *port = tty->driver_data;
1113 struct serial_struct newinfo;
1114 int reconfig_port;
1115
1116 if (copy_from_user(&newinfo, info, sizeof(newinfo)))
1117 return -EFAULT;
1118
1119 mutex_lock(&port->port.mutex);
1120 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
1121 (newinfo.flags & ASYNC_SPD_MASK));
1122
1123 if (!capable(CAP_SYS_ADMIN)) {
1124 if ((newinfo.close_delay != port->port.close_delay) ||
1125 (newinfo.closing_wait != port->port.closing_wait) ||
1126 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1127 (port->port.flags & ~ASYNC_USR_MASK))) {
1128 mutex_unlock(&port->port.mutex);
1129 return -EPERM;
1130 }
1131 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1132 (newinfo.flags & ASYNC_USR_MASK));
1133 } else {
1134 port->port.close_delay = newinfo.close_delay;
1135 port->port.closing_wait = newinfo.closing_wait;
1136 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1137 (newinfo.flags & ASYNC_FLAGS));
1138 }
1139 if (reconfig_port) {
1140 unsigned long flags;
1141 spin_lock_irqsave(&port->card->card_lock, flags);
1142 isicom_config_port(tty);
1143 spin_unlock_irqrestore(&port->card->card_lock, flags);
1144 }
1145 mutex_unlock(&port->port.mutex);
1146 return 0;
1147}
1148
1149static int isicom_get_serial_info(struct isi_port *port,
1150 struct serial_struct __user *info)
1151{
1152 struct serial_struct out_info;
1153
1154 mutex_lock(&port->port.mutex);
1155 memset(&out_info, 0, sizeof(out_info));
1156/* out_info.type = ? */
1157 out_info.line = port - isi_ports;
1158 out_info.port = port->card->base;
1159 out_info.irq = port->card->irq;
1160 out_info.flags = port->port.flags;
1161/* out_info.baud_base = ? */
1162 out_info.close_delay = port->port.close_delay;
1163 out_info.closing_wait = port->port.closing_wait;
1164 mutex_unlock(&port->port.mutex);
1165 if (copy_to_user(info, &out_info, sizeof(out_info)))
1166 return -EFAULT;
1167 return 0;
1168}
1169
1170static int isicom_ioctl(struct tty_struct *tty,
1171 unsigned int cmd, unsigned long arg)
1172{
1173 struct isi_port *port = tty->driver_data;
1174 void __user *argp = (void __user *)arg;
1175
1176 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1177 return -ENODEV;
1178
1179 switch (cmd) {
1180 case TIOCGSERIAL:
1181 return isicom_get_serial_info(port, argp);
1182
1183 case TIOCSSERIAL:
1184 return isicom_set_serial_info(tty, argp);
1185
1186 default:
1187 return -ENOIOCTLCMD;
1188 }
1189 return 0;
1190}
1191
1192/* set_termios et all */
1193static void isicom_set_termios(struct tty_struct *tty,
1194 struct ktermios *old_termios)
1195{
1196 struct isi_port *port = tty->driver_data;
1197 unsigned long flags;
1198
1199 if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1200 return;
1201
1202 if (tty->termios->c_cflag == old_termios->c_cflag &&
1203 tty->termios->c_iflag == old_termios->c_iflag)
1204 return;
1205
1206 spin_lock_irqsave(&port->card->card_lock, flags);
1207 isicom_config_port(tty);
1208 spin_unlock_irqrestore(&port->card->card_lock, flags);
1209
1210 if ((old_termios->c_cflag & CRTSCTS) &&
1211 !(tty->termios->c_cflag & CRTSCTS)) {
1212 tty->hw_stopped = 0;
1213 isicom_start(tty);
1214 }
1215}
1216
1217/* throttle et all */
1218static void isicom_throttle(struct tty_struct *tty)
1219{
1220 struct isi_port *port = tty->driver_data;
1221 struct isi_board *card = port->card;
1222
1223 if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1224 return;
1225
1226 /* tell the card that this port cannot handle any more data for now */
1227 card->port_status &= ~(1 << port->channel);
1228 outw(card->port_status, card->base + 0x02);
1229}
1230
1231/* unthrottle et all */
1232static void isicom_unthrottle(struct tty_struct *tty)
1233{
1234 struct isi_port *port = tty->driver_data;
1235 struct isi_board *card = port->card;
1236
1237 if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1238 return;
1239
1240 /* tell the card that this port is ready to accept more data */
1241 card->port_status |= (1 << port->channel);
1242 outw(card->port_status, card->base + 0x02);
1243}
1244
1245/* stop et all */
1246static void isicom_stop(struct tty_struct *tty)
1247{
1248 struct isi_port *port = tty->driver_data;
1249
1250 if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1251 return;
1252
1253 /* this tells the transmitter not to consider this port for
1254 data output to the card. */
1255 port->status &= ~ISI_TXOK;
1256}
1257
1258/* start et all */
1259static void isicom_start(struct tty_struct *tty)
1260{
1261 struct isi_port *port = tty->driver_data;
1262
1263 if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1264 return;
1265
1266 /* this tells the transmitter to consider this port for
1267 data output to the card. */
1268 port->status |= ISI_TXOK;
1269}
1270
1271static void isicom_hangup(struct tty_struct *tty)
1272{
1273 struct isi_port *port = tty->driver_data;
1274
1275 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1276 return;
1277 tty_port_hangup(&port->port);
1278}
1279
1280
1281/*
1282 * Driver init and deinit functions
1283 */
1284
1285static const struct tty_operations isicom_ops = {
1286 .open = isicom_open,
1287 .close = isicom_close,
1288 .write = isicom_write,
1289 .put_char = isicom_put_char,
1290 .flush_chars = isicom_flush_chars,
1291 .write_room = isicom_write_room,
1292 .chars_in_buffer = isicom_chars_in_buffer,
1293 .ioctl = isicom_ioctl,
1294 .set_termios = isicom_set_termios,
1295 .throttle = isicom_throttle,
1296 .unthrottle = isicom_unthrottle,
1297 .stop = isicom_stop,
1298 .start = isicom_start,
1299 .hangup = isicom_hangup,
1300 .flush_buffer = isicom_flush_buffer,
1301 .tiocmget = isicom_tiocmget,
1302 .tiocmset = isicom_tiocmset,
1303 .break_ctl = isicom_send_break,
1304};
1305
1306static const struct tty_port_operations isicom_port_ops = {
1307 .carrier_raised = isicom_carrier_raised,
1308 .dtr_rts = isicom_dtr_rts,
1309 .activate = isicom_activate,
1310 .shutdown = isicom_shutdown,
1311};
1312
1313static int __devinit reset_card(struct pci_dev *pdev,
1314 const unsigned int card, unsigned int *signature)
1315{
1316 struct isi_board *board = pci_get_drvdata(pdev);
1317 unsigned long base = board->base;
1318 unsigned int sig, portcount = 0;
1319 int retval = 0;
1320
1321 dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
1322 base);
1323
1324 inw(base + 0x8);
1325
1326 msleep(10);
1327
1328 outw(0, base + 0x8); /* Reset */
1329
1330 msleep(1000);
1331
1332 sig = inw(base + 0x4) & 0xff;
1333
1334 if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
1335 sig != 0xee) {
1336 dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
1337 "bad I/O Port Address 0x%lx).\n", card + 1, base);
1338 dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
1339 retval = -EIO;
1340 goto end;
1341 }
1342
1343 msleep(10);
1344
1345 portcount = inw(base + 0x2);
1346 if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
1347 portcount != 8 && portcount != 16)) {
1348 dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
1349 card + 1);
1350 retval = -EIO;
1351 goto end;
1352 }
1353
1354 switch (sig) {
1355 case 0xa5:
1356 case 0xbb:
1357 case 0xdd:
1358 board->port_count = (portcount == 4) ? 4 : 8;
1359 board->shift_count = 12;
1360 break;
1361 case 0xcc:
1362 case 0xee:
1363 board->port_count = 16;
1364 board->shift_count = 11;
1365 break;
1366 }
1367 dev_info(&pdev->dev, "-Done\n");
1368 *signature = sig;
1369
1370end:
1371 return retval;
1372}
1373
1374static int __devinit load_firmware(struct pci_dev *pdev,
1375 const unsigned int index, const unsigned int signature)
1376{
1377 struct isi_board *board = pci_get_drvdata(pdev);
1378 const struct firmware *fw;
1379 unsigned long base = board->base;
1380 unsigned int a;
1381 u16 word_count, status;
1382 int retval = -EIO;
1383 char *name;
1384 u8 *data;
1385
1386 struct stframe {
1387 u16 addr;
1388 u16 count;
1389 u8 data[0];
1390 } *frame;
1391
1392 switch (signature) {
1393 case 0xa5:
1394 name = "isi608.bin";
1395 break;
1396 case 0xbb:
1397 name = "isi608em.bin";
1398 break;
1399 case 0xcc:
1400 name = "isi616em.bin";
1401 break;
1402 case 0xdd:
1403 name = "isi4608.bin";
1404 break;
1405 case 0xee:
1406 name = "isi4616.bin";
1407 break;
1408 default:
1409 dev_err(&pdev->dev, "Unknown signature.\n");
1410 goto end;
1411 }
1412
1413 retval = request_firmware(&fw, name, &pdev->dev);
1414 if (retval)
1415 goto end;
1416
1417 retval = -EIO;
1418
1419 for (frame = (struct stframe *)fw->data;
1420 frame < (struct stframe *)(fw->data + fw->size);
1421 frame = (struct stframe *)((u8 *)(frame + 1) +
1422 frame->count)) {
1423 if (WaitTillCardIsFree(base))
1424 goto errrelfw;
1425
1426 outw(0xf0, base); /* start upload sequence */
1427 outw(0x00, base);
1428 outw(frame->addr, base); /* lsb of address */
1429
1430 word_count = frame->count / 2 + frame->count % 2;
1431 outw(word_count, base);
1432 InterruptTheCard(base);
1433
1434 udelay(100); /* 0x2f */
1435
1436 if (WaitTillCardIsFree(base))
1437 goto errrelfw;
1438
1439 status = inw(base + 0x4);
1440 if (status != 0) {
1441 dev_warn(&pdev->dev, "Card%d rejected load header:\n"
1442 "Address:0x%x\n"
1443 "Count:0x%x\n"
1444 "Status:0x%x\n",
1445 index + 1, frame->addr, frame->count, status);
1446 goto errrelfw;
1447 }
1448 outsw(base, frame->data, word_count);
1449
1450 InterruptTheCard(base);
1451
1452 udelay(50); /* 0x0f */
1453
1454 if (WaitTillCardIsFree(base))
1455 goto errrelfw;
1456
1457 status = inw(base + 0x4);
1458 if (status != 0) {
1459 dev_err(&pdev->dev, "Card%d got out of sync.Card "
1460 "Status:0x%x\n", index + 1, status);
1461 goto errrelfw;
1462 }
1463 }
1464
1465/* XXX: should we test it by reading it back and comparing with original like
1466 * in load firmware package? */
1467 for (frame = (struct stframe *)fw->data;
1468 frame < (struct stframe *)(fw->data + fw->size);
1469 frame = (struct stframe *)((u8 *)(frame + 1) +
1470 frame->count)) {
1471 if (WaitTillCardIsFree(base))
1472 goto errrelfw;
1473
1474 outw(0xf1, base); /* start download sequence */
1475 outw(0x00, base);
1476 outw(frame->addr, base); /* lsb of address */
1477
1478 word_count = (frame->count >> 1) + frame->count % 2;
1479 outw(word_count + 1, base);
1480 InterruptTheCard(base);
1481
1482 udelay(50); /* 0xf */
1483
1484 if (WaitTillCardIsFree(base))
1485 goto errrelfw;
1486
1487 status = inw(base + 0x4);
1488 if (status != 0) {
1489 dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
1490 "Address:0x%x\n"
1491 "Count:0x%x\n"
1492 "Status: 0x%x\n",
1493 index + 1, frame->addr, frame->count, status);
1494 goto errrelfw;
1495 }
1496
1497 data = kmalloc(word_count * 2, GFP_KERNEL);
1498 if (data == NULL) {
1499 dev_err(&pdev->dev, "Card%d, firmware upload "
1500 "failed, not enough memory\n", index + 1);
1501 goto errrelfw;
1502 }
1503 inw(base);
1504 insw(base, data, word_count);
1505 InterruptTheCard(base);
1506
1507 for (a = 0; a < frame->count; a++)
1508 if (data[a] != frame->data[a]) {
1509 kfree(data);
1510 dev_err(&pdev->dev, "Card%d, firmware upload "
1511 "failed\n", index + 1);
1512 goto errrelfw;
1513 }
1514 kfree(data);
1515
1516 udelay(50); /* 0xf */
1517
1518 if (WaitTillCardIsFree(base))
1519 goto errrelfw;
1520
1521 status = inw(base + 0x4);
1522 if (status != 0) {
1523 dev_err(&pdev->dev, "Card%d verify got out of sync. "
1524 "Card Status:0x%x\n", index + 1, status);
1525 goto errrelfw;
1526 }
1527 }
1528
1529 /* xfer ctrl */
1530 if (WaitTillCardIsFree(base))
1531 goto errrelfw;
1532
1533 outw(0xf2, base);
1534 outw(0x800, base);
1535 outw(0x0, base);
1536 outw(0x0, base);
1537 InterruptTheCard(base);
1538 outw(0x0, base + 0x4); /* for ISI4608 cards */
1539
1540 board->status |= FIRMWARE_LOADED;
1541 retval = 0;
1542
1543errrelfw:
1544 release_firmware(fw);
1545end:
1546 return retval;
1547}
1548
1549/*
1550 * Insmod can set static symbols so keep these static
1551 */
1552static unsigned int card_count;
1553
1554static int __devinit isicom_probe(struct pci_dev *pdev,
1555 const struct pci_device_id *ent)
1556{
1557 unsigned int uninitialized_var(signature), index;
1558 int retval = -EPERM;
1559 struct isi_board *board = NULL;
1560
1561 if (card_count >= BOARD_COUNT)
1562 goto err;
1563
1564 retval = pci_enable_device(pdev);
1565 if (retval) {
1566 dev_err(&pdev->dev, "failed to enable\n");
1567 goto err;
1568 }
1569
1570 dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
1571
1572 /* allot the first empty slot in the array */
1573 for (index = 0; index < BOARD_COUNT; index++) {
1574 if (isi_card[index].base == 0) {
1575 board = &isi_card[index];
1576 break;
1577 }
1578 }
1579 if (index == BOARD_COUNT) {
1580 retval = -ENODEV;
1581 goto err_disable;
1582 }
1583
1584 board->index = index;
1585 board->base = pci_resource_start(pdev, 3);
1586 board->irq = pdev->irq;
1587 card_count++;
1588
1589 pci_set_drvdata(pdev, board);
1590
1591 retval = pci_request_region(pdev, 3, ISICOM_NAME);
1592 if (retval) {
1593 dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
1594 "will be disabled.\n", board->base, board->base + 15,
1595 index + 1);
1596 retval = -EBUSY;
1597 goto errdec;
1598 }
1599
1600 retval = request_irq(board->irq, isicom_interrupt,
1601 IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
1602 if (retval < 0) {
1603 dev_err(&pdev->dev, "Could not install handler at Irq %d. "
1604 "Card%d will be disabled.\n", board->irq, index + 1);
1605 goto errunrr;
1606 }
1607
1608 retval = reset_card(pdev, index, &signature);
1609 if (retval < 0)
1610 goto errunri;
1611
1612 retval = load_firmware(pdev, index, signature);
1613 if (retval < 0)
1614 goto errunri;
1615
1616 for (index = 0; index < board->port_count; index++)
1617 tty_register_device(isicom_normal, board->index * 16 + index,
1618 &pdev->dev);
1619
1620 return 0;
1621
1622errunri:
1623 free_irq(board->irq, board);
1624errunrr:
1625 pci_release_region(pdev, 3);
1626errdec:
1627 board->base = 0;
1628 card_count--;
1629err_disable:
1630 pci_disable_device(pdev);
1631err:
1632 return retval;
1633}
1634
1635static void __devexit isicom_remove(struct pci_dev *pdev)
1636{
1637 struct isi_board *board = pci_get_drvdata(pdev);
1638 unsigned int i;
1639
1640 for (i = 0; i < board->port_count; i++)
1641 tty_unregister_device(isicom_normal, board->index * 16 + i);
1642
1643 free_irq(board->irq, board);
1644 pci_release_region(pdev, 3);
1645 board->base = 0;
1646 card_count--;
1647 pci_disable_device(pdev);
1648}
1649
1650static int __init isicom_init(void)
1651{
1652 int retval, idx, channel;
1653 struct isi_port *port;
1654
1655 for (idx = 0; idx < BOARD_COUNT; idx++) {
1656 port = &isi_ports[idx * 16];
1657 isi_card[idx].ports = port;
1658 spin_lock_init(&isi_card[idx].card_lock);
1659 for (channel = 0; channel < 16; channel++, port++) {
1660 tty_port_init(&port->port);
1661 port->port.ops = &isicom_port_ops;
1662 port->magic = ISICOM_MAGIC;
1663 port->card = &isi_card[idx];
1664 port->channel = channel;
1665 port->port.close_delay = 50 * HZ/100;
1666 port->port.closing_wait = 3000 * HZ/100;
1667 port->status = 0;
1668 /* . . . */
1669 }
1670 isi_card[idx].base = 0;
1671 isi_card[idx].irq = 0;
1672 }
1673
1674 /* tty driver structure initialization */
1675 isicom_normal = alloc_tty_driver(PORT_COUNT);
1676 if (!isicom_normal) {
1677 retval = -ENOMEM;
1678 goto error;
1679 }
1680
1681 isicom_normal->owner = THIS_MODULE;
1682 isicom_normal->name = "ttyM";
1683 isicom_normal->major = ISICOM_NMAJOR;
1684 isicom_normal->minor_start = 0;
1685 isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
1686 isicom_normal->subtype = SERIAL_TYPE_NORMAL;
1687 isicom_normal->init_termios = tty_std_termios;
1688 isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1689 CLOCAL;
1690 isicom_normal->flags = TTY_DRIVER_REAL_RAW |
1691 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
1692 tty_set_operations(isicom_normal, &isicom_ops);
1693
1694 retval = tty_register_driver(isicom_normal);
1695 if (retval) {
1696 pr_debug("Couldn't register the dialin driver\n");
1697 goto err_puttty;
1698 }
1699
1700 retval = pci_register_driver(&isicom_driver);
1701 if (retval < 0) {
1702 pr_err("Unable to register pci driver.\n");
1703 goto err_unrtty;
1704 }
1705
1706 mod_timer(&tx, jiffies + 1);
1707
1708 return 0;
1709err_unrtty:
1710 tty_unregister_driver(isicom_normal);
1711err_puttty:
1712 put_tty_driver(isicom_normal);
1713error:
1714 return retval;
1715}
1716
1717static void __exit isicom_exit(void)
1718{
1719 del_timer_sync(&tx);
1720
1721 pci_unregister_driver(&isicom_driver);
1722 tty_unregister_driver(isicom_normal);
1723 put_tty_driver(isicom_normal);
1724}
1725
1726module_init(isicom_init);
1727module_exit(isicom_exit);
1728
1729MODULE_AUTHOR("MultiTech");
1730MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1731MODULE_LICENSE("GPL");
1732MODULE_FIRMWARE("isi608.bin");
1733MODULE_FIRMWARE("isi608em.bin");
1734MODULE_FIRMWARE("isi616em.bin");
1735MODULE_FIRMWARE("isi4608.bin");
1736MODULE_FIRMWARE("isi4616.bin");
diff --git a/drivers/tty/moxa.c b/drivers/tty/moxa.c
new file mode 100644
index 000000000000..ba679ce0a774
--- /dev/null
+++ b/drivers/tty/moxa.c
@@ -0,0 +1,2084 @@
1/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
7 *
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17/*
18 * MOXA Intellio Series Driver
19 * for : LINUX
20 * date : 1999/1/7
21 * version : 5.1
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/ioport.h>
28#include <linux/errno.h>
29#include <linux/firmware.h>
30#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/major.h>
37#include <linux/string.h>
38#include <linux/fcntl.h>
39#include <linux/ptrace.h>
40#include <linux/serial.h>
41#include <linux/tty_driver.h>
42#include <linux/delay.h>
43#include <linux/pci.h>
44#include <linux/init.h>
45#include <linux/bitops.h>
46#include <linux/slab.h>
47
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51
52#include "moxa.h"
53
54#define MOXA_VERSION "6.0k"
55
56#define MOXA_FW_HDRLEN 32
57
58#define MOXAMAJOR 172
59
60#define MAX_BOARDS 4 /* Don't change this value */
61#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63
64#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
66
67/*
68 * Define the Moxa PCI vendor and device IDs.
69 */
70#define MOXA_BUS_TYPE_ISA 0
71#define MOXA_BUS_TYPE_PCI 1
72
73enum {
74 MOXA_BOARD_C218_PCI = 1,
75 MOXA_BOARD_C218_ISA,
76 MOXA_BOARD_C320_PCI,
77 MOXA_BOARD_C320_ISA,
78 MOXA_BOARD_CP204J,
79};
80
81static char *moxa_brdname[] =
82{
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
87 "CP-204J series",
88};
89
90#ifdef CONFIG_PCI
91static struct pci_device_id moxa_pcibrds[] = {
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
98 { 0 }
99};
100MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101#endif /* CONFIG_PCI */
102
103struct moxa_port;
104
105static struct moxa_board_conf {
106 int boardType;
107 int numPorts;
108 int busType;
109
110 unsigned int ready;
111
112 struct moxa_port *ports;
113
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118} moxa_boards[MAX_BOARDS];
119
120struct mxser_mstatus {
121 tcflag_t cflag;
122 int cts;
123 int dsr;
124 int ri;
125 int dcd;
126};
127
128struct moxaq_str {
129 int inq;
130 int outq;
131};
132
133struct moxa_port {
134 struct tty_port port;
135 struct moxa_board_conf *board;
136 void __iomem *tableAddr;
137
138 int type;
139 int cflag;
140 unsigned long statusflags;
141
142 u8 DCDState; /* Protected by the port lock */
143 u8 lineCtrl;
144 u8 lowChkFlag;
145};
146
147struct mon_str {
148 int tick;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
151};
152
153/* statusflags */
154#define TXSTOPPED 1
155#define LOWWAIT 2
156#define EMPTYWAIT 3
157
158#define SERIAL_DO_RESTART
159
160#define WAKEUP_CHARS 256
161
162static int ttymajor = MOXAMAJOR;
163static struct mon_str moxaLog;
164static unsigned int moxaFuncTout = HZ / 2;
165static unsigned int moxaLowWaterChk;
166static DEFINE_MUTEX(moxa_openlock);
167static DEFINE_SPINLOCK(moxa_lock);
168
169static unsigned long baseaddr[MAX_BOARDS];
170static unsigned int type[MAX_BOARDS];
171static unsigned int numports[MAX_BOARDS];
172
173MODULE_AUTHOR("William Chen");
174MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175MODULE_LICENSE("GPL");
176MODULE_FIRMWARE("c218tunx.cod");
177MODULE_FIRMWARE("cp204unx.cod");
178MODULE_FIRMWARE("c320tunx.cod");
179
180module_param_array(type, uint, NULL, 0);
181MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
182module_param_array(baseaddr, ulong, NULL, 0);
183MODULE_PARM_DESC(baseaddr, "base address");
184module_param_array(numports, uint, NULL, 0);
185MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186
187module_param(ttymajor, int, 0);
188
189/*
190 * static functions:
191 */
192static int moxa_open(struct tty_struct *, struct file *);
193static void moxa_close(struct tty_struct *, struct file *);
194static int moxa_write(struct tty_struct *, const unsigned char *, int);
195static int moxa_write_room(struct tty_struct *);
196static void moxa_flush_buffer(struct tty_struct *);
197static int moxa_chars_in_buffer(struct tty_struct *);
198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty);
203static int moxa_tiocmset(struct tty_struct *tty,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
207static void moxa_shutdown(struct tty_port *);
208static int moxa_carrier_raised(struct tty_port *);
209static void moxa_dtr_rts(struct tty_port *, int);
210/*
211 * moxa board interface functions:
212 */
213static void MoxaPortEnable(struct moxa_port *);
214static void MoxaPortDisable(struct moxa_port *);
215static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
216static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
217static void MoxaPortLineCtrl(struct moxa_port *, int, int);
218static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
219static int MoxaPortLineStatus(struct moxa_port *);
220static void MoxaPortFlushData(struct moxa_port *, int);
221static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
222static int MoxaPortReadData(struct moxa_port *);
223static int MoxaPortTxQueue(struct moxa_port *);
224static int MoxaPortRxQueue(struct moxa_port *);
225static int MoxaPortTxFree(struct moxa_port *);
226static void MoxaPortTxDisable(struct moxa_port *);
227static void MoxaPortTxEnable(struct moxa_port *);
228static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
229static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
230static void MoxaSetFifo(struct moxa_port *port, int enable);
231
232/*
233 * I/O functions
234 */
235
236static DEFINE_SPINLOCK(moxafunc_lock);
237
238static void moxa_wait_finish(void __iomem *ofsAddr)
239{
240 unsigned long end = jiffies + moxaFuncTout;
241
242 while (readw(ofsAddr + FuncCode) != 0)
243 if (time_after(jiffies, end))
244 return;
245 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
246 printk(KERN_WARNING "moxa function expired\n");
247}
248
249static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
250{
251 unsigned long flags;
252 spin_lock_irqsave(&moxafunc_lock, flags);
253 writew(arg, ofsAddr + FuncArg);
254 writew(cmd, ofsAddr + FuncCode);
255 moxa_wait_finish(ofsAddr);
256 spin_unlock_irqrestore(&moxafunc_lock, flags);
257}
258
259static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
260{
261 unsigned long flags;
262 u16 ret;
263 spin_lock_irqsave(&moxafunc_lock, flags);
264 writew(arg, ofsAddr + FuncArg);
265 writew(cmd, ofsAddr + FuncCode);
266 moxa_wait_finish(ofsAddr);
267 ret = readw(ofsAddr + FuncArg);
268 spin_unlock_irqrestore(&moxafunc_lock, flags);
269 return ret;
270}
271
272static void moxa_low_water_check(void __iomem *ofsAddr)
273{
274 u16 rptr, wptr, mask, len;
275
276 if (readb(ofsAddr + FlagStat) & Xoff_state) {
277 rptr = readw(ofsAddr + RXrptr);
278 wptr = readw(ofsAddr + RXwptr);
279 mask = readw(ofsAddr + RX_mask);
280 len = (wptr - rptr) & mask;
281 if (len <= Low_water)
282 moxafunc(ofsAddr, FC_SendXon, 0);
283 }
284}
285
286/*
287 * TTY operations
288 */
289
290static int moxa_ioctl(struct tty_struct *tty,
291 unsigned int cmd, unsigned long arg)
292{
293 struct moxa_port *ch = tty->driver_data;
294 void __user *argp = (void __user *)arg;
295 int status, ret = 0;
296
297 if (tty->index == MAX_PORTS) {
298 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
299 cmd != MOXA_GETMSTATUS)
300 return -EINVAL;
301 } else if (!ch)
302 return -ENODEV;
303
304 switch (cmd) {
305 case MOXA_GETDATACOUNT:
306 moxaLog.tick = jiffies;
307 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
308 ret = -EFAULT;
309 break;
310 case MOXA_FLUSH_QUEUE:
311 MoxaPortFlushData(ch, arg);
312 break;
313 case MOXA_GET_IOQUEUE: {
314 struct moxaq_str __user *argm = argp;
315 struct moxaq_str tmp;
316 struct moxa_port *p;
317 unsigned int i, j;
318
319 for (i = 0; i < MAX_BOARDS; i++) {
320 p = moxa_boards[i].ports;
321 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
322 memset(&tmp, 0, sizeof(tmp));
323 spin_lock_bh(&moxa_lock);
324 if (moxa_boards[i].ready) {
325 tmp.inq = MoxaPortRxQueue(p);
326 tmp.outq = MoxaPortTxQueue(p);
327 }
328 spin_unlock_bh(&moxa_lock);
329 if (copy_to_user(argm, &tmp, sizeof(tmp)))
330 return -EFAULT;
331 }
332 }
333 break;
334 } case MOXA_GET_OQUEUE:
335 status = MoxaPortTxQueue(ch);
336 ret = put_user(status, (unsigned long __user *)argp);
337 break;
338 case MOXA_GET_IQUEUE:
339 status = MoxaPortRxQueue(ch);
340 ret = put_user(status, (unsigned long __user *)argp);
341 break;
342 case MOXA_GETMSTATUS: {
343 struct mxser_mstatus __user *argm = argp;
344 struct mxser_mstatus tmp;
345 struct moxa_port *p;
346 unsigned int i, j;
347
348 for (i = 0; i < MAX_BOARDS; i++) {
349 p = moxa_boards[i].ports;
350 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
351 struct tty_struct *ttyp;
352 memset(&tmp, 0, sizeof(tmp));
353 spin_lock_bh(&moxa_lock);
354 if (!moxa_boards[i].ready) {
355 spin_unlock_bh(&moxa_lock);
356 goto copy;
357 }
358
359 status = MoxaPortLineStatus(p);
360 spin_unlock_bh(&moxa_lock);
361
362 if (status & 1)
363 tmp.cts = 1;
364 if (status & 2)
365 tmp.dsr = 1;
366 if (status & 4)
367 tmp.dcd = 1;
368
369 ttyp = tty_port_tty_get(&p->port);
370 if (!ttyp || !ttyp->termios)
371 tmp.cflag = p->cflag;
372 else
373 tmp.cflag = ttyp->termios->c_cflag;
374 tty_kref_put(ttyp);
375copy:
376 if (copy_to_user(argm, &tmp, sizeof(tmp)))
377 return -EFAULT;
378 }
379 }
380 break;
381 }
382 case TIOCGSERIAL:
383 mutex_lock(&ch->port.mutex);
384 ret = moxa_get_serial_info(ch, argp);
385 mutex_unlock(&ch->port.mutex);
386 break;
387 case TIOCSSERIAL:
388 mutex_lock(&ch->port.mutex);
389 ret = moxa_set_serial_info(ch, argp);
390 mutex_unlock(&ch->port.mutex);
391 break;
392 default:
393 ret = -ENOIOCTLCMD;
394 }
395 return ret;
396}
397
398static int moxa_break_ctl(struct tty_struct *tty, int state)
399{
400 struct moxa_port *port = tty->driver_data;
401
402 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
403 Magic_code);
404 return 0;
405}
406
407static const struct tty_operations moxa_ops = {
408 .open = moxa_open,
409 .close = moxa_close,
410 .write = moxa_write,
411 .write_room = moxa_write_room,
412 .flush_buffer = moxa_flush_buffer,
413 .chars_in_buffer = moxa_chars_in_buffer,
414 .ioctl = moxa_ioctl,
415 .set_termios = moxa_set_termios,
416 .stop = moxa_stop,
417 .start = moxa_start,
418 .hangup = moxa_hangup,
419 .break_ctl = moxa_break_ctl,
420 .tiocmget = moxa_tiocmget,
421 .tiocmset = moxa_tiocmset,
422};
423
424static const struct tty_port_operations moxa_port_ops = {
425 .carrier_raised = moxa_carrier_raised,
426 .dtr_rts = moxa_dtr_rts,
427 .shutdown = moxa_shutdown,
428};
429
430static struct tty_driver *moxaDriver;
431static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
432
433/*
434 * HW init
435 */
436
437static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
438{
439 switch (brd->boardType) {
440 case MOXA_BOARD_C218_ISA:
441 case MOXA_BOARD_C218_PCI:
442 if (model != 1)
443 goto err;
444 break;
445 case MOXA_BOARD_CP204J:
446 if (model != 3)
447 goto err;
448 break;
449 default:
450 if (model != 2)
451 goto err;
452 break;
453 }
454 return 0;
455err:
456 return -EINVAL;
457}
458
459static int moxa_check_fw(const void *ptr)
460{
461 const __le16 *lptr = ptr;
462
463 if (*lptr != cpu_to_le16(0x7980))
464 return -EINVAL;
465
466 return 0;
467}
468
469static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
470 size_t len)
471{
472 void __iomem *baseAddr = brd->basemem;
473 u16 tmp;
474
475 writeb(HW_reset, baseAddr + Control_reg); /* reset */
476 msleep(10);
477 memset_io(baseAddr, 0, 4096);
478 memcpy_toio(baseAddr, buf, len); /* download BIOS */
479 writeb(0, baseAddr + Control_reg); /* restart */
480
481 msleep(2000);
482
483 switch (brd->boardType) {
484 case MOXA_BOARD_C218_ISA:
485 case MOXA_BOARD_C218_PCI:
486 tmp = readw(baseAddr + C218_key);
487 if (tmp != C218_KeyCode)
488 goto err;
489 break;
490 case MOXA_BOARD_CP204J:
491 tmp = readw(baseAddr + C218_key);
492 if (tmp != CP204J_KeyCode)
493 goto err;
494 break;
495 default:
496 tmp = readw(baseAddr + C320_key);
497 if (tmp != C320_KeyCode)
498 goto err;
499 tmp = readw(baseAddr + C320_status);
500 if (tmp != STS_init) {
501 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
502 "module not found\n");
503 return -EIO;
504 }
505 break;
506 }
507
508 return 0;
509err:
510 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
511 return -EIO;
512}
513
514static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
515 size_t len)
516{
517 void __iomem *baseAddr = brd->basemem;
518
519 if (len < 7168) {
520 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
521 return -EINVAL;
522 }
523
524 writew(len - 7168 - 2, baseAddr + C320bapi_len);
525 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
526 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
527 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
528 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
529
530 return 0;
531}
532
533static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
534 size_t len)
535{
536 void __iomem *baseAddr = brd->basemem;
537 const __le16 *uptr = ptr;
538 size_t wlen, len2, j;
539 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
540 unsigned int i, retry;
541 u16 usum, keycode;
542
543 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
544 C218_KeyCode;
545
546 switch (brd->boardType) {
547 case MOXA_BOARD_CP204J:
548 case MOXA_BOARD_C218_ISA:
549 case MOXA_BOARD_C218_PCI:
550 key = C218_key;
551 loadbuf = C218_LoadBuf;
552 loadlen = C218DLoad_len;
553 checksum = C218check_sum;
554 checksum_ok = C218chksum_ok;
555 break;
556 default:
557 key = C320_key;
558 keycode = C320_KeyCode;
559 loadbuf = C320_LoadBuf;
560 loadlen = C320DLoad_len;
561 checksum = C320check_sum;
562 checksum_ok = C320chksum_ok;
563 break;
564 }
565
566 usum = 0;
567 wlen = len >> 1;
568 for (i = 0; i < wlen; i++)
569 usum += le16_to_cpu(uptr[i]);
570 retry = 0;
571 do {
572 wlen = len >> 1;
573 j = 0;
574 while (wlen) {
575 len2 = (wlen > 2048) ? 2048 : wlen;
576 wlen -= len2;
577 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
578 j += len2 << 1;
579
580 writew(len2, baseAddr + loadlen);
581 writew(0, baseAddr + key);
582 for (i = 0; i < 100; i++) {
583 if (readw(baseAddr + key) == keycode)
584 break;
585 msleep(10);
586 }
587 if (readw(baseAddr + key) != keycode)
588 return -EIO;
589 }
590 writew(0, baseAddr + loadlen);
591 writew(usum, baseAddr + checksum);
592 writew(0, baseAddr + key);
593 for (i = 0; i < 100; i++) {
594 if (readw(baseAddr + key) == keycode)
595 break;
596 msleep(10);
597 }
598 retry++;
599 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
600 if (readb(baseAddr + checksum_ok) != 1)
601 return -EIO;
602
603 writew(0, baseAddr + key);
604 for (i = 0; i < 600; i++) {
605 if (readw(baseAddr + Magic_no) == Magic_code)
606 break;
607 msleep(10);
608 }
609 if (readw(baseAddr + Magic_no) != Magic_code)
610 return -EIO;
611
612 if (MOXA_IS_320(brd)) {
613 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
614 writew(0x3800, baseAddr + TMS320_PORT1);
615 writew(0x3900, baseAddr + TMS320_PORT2);
616 writew(28499, baseAddr + TMS320_CLOCK);
617 } else {
618 writew(0x3200, baseAddr + TMS320_PORT1);
619 writew(0x3400, baseAddr + TMS320_PORT2);
620 writew(19999, baseAddr + TMS320_CLOCK);
621 }
622 }
623 writew(1, baseAddr + Disable_IRQ);
624 writew(0, baseAddr + Magic_no);
625 for (i = 0; i < 500; i++) {
626 if (readw(baseAddr + Magic_no) == Magic_code)
627 break;
628 msleep(10);
629 }
630 if (readw(baseAddr + Magic_no) != Magic_code)
631 return -EIO;
632
633 if (MOXA_IS_320(brd)) {
634 j = readw(baseAddr + Module_cnt);
635 if (j <= 0)
636 return -EIO;
637 brd->numPorts = j * 8;
638 writew(j, baseAddr + Module_no);
639 writew(0, baseAddr + Magic_no);
640 for (i = 0; i < 600; i++) {
641 if (readw(baseAddr + Magic_no) == Magic_code)
642 break;
643 msleep(10);
644 }
645 if (readw(baseAddr + Magic_no) != Magic_code)
646 return -EIO;
647 }
648 brd->intNdx = baseAddr + IRQindex;
649 brd->intPend = baseAddr + IRQpending;
650 brd->intTable = baseAddr + IRQtable;
651
652 return 0;
653}
654
655static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
656 size_t len)
657{
658 void __iomem *ofsAddr, *baseAddr = brd->basemem;
659 struct moxa_port *port;
660 int retval, i;
661
662 if (len % 2) {
663 printk(KERN_ERR "MOXA: bios length is not even\n");
664 return -EINVAL;
665 }
666
667 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
668 if (retval)
669 return retval;
670
671 switch (brd->boardType) {
672 case MOXA_BOARD_C218_ISA:
673 case MOXA_BOARD_C218_PCI:
674 case MOXA_BOARD_CP204J:
675 port = brd->ports;
676 for (i = 0; i < brd->numPorts; i++, port++) {
677 port->board = brd;
678 port->DCDState = 0;
679 port->tableAddr = baseAddr + Extern_table +
680 Extern_size * i;
681 ofsAddr = port->tableAddr;
682 writew(C218rx_mask, ofsAddr + RX_mask);
683 writew(C218tx_mask, ofsAddr + TX_mask);
684 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
685 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
686
687 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
688 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
689
690 }
691 break;
692 default:
693 port = brd->ports;
694 for (i = 0; i < brd->numPorts; i++, port++) {
695 port->board = brd;
696 port->DCDState = 0;
697 port->tableAddr = baseAddr + Extern_table +
698 Extern_size * i;
699 ofsAddr = port->tableAddr;
700 switch (brd->numPorts) {
701 case 8:
702 writew(C320p8rx_mask, ofsAddr + RX_mask);
703 writew(C320p8tx_mask, ofsAddr + TX_mask);
704 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
705 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
706 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
707 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
708
709 break;
710 case 16:
711 writew(C320p16rx_mask, ofsAddr + RX_mask);
712 writew(C320p16tx_mask, ofsAddr + TX_mask);
713 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
714 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
715 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
716 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
717 break;
718
719 case 24:
720 writew(C320p24rx_mask, ofsAddr + RX_mask);
721 writew(C320p24tx_mask, ofsAddr + TX_mask);
722 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
723 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
724 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
725 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
726 break;
727 case 32:
728 writew(C320p32rx_mask, ofsAddr + RX_mask);
729 writew(C320p32tx_mask, ofsAddr + TX_mask);
730 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
731 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
732 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
733 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
734 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
735 break;
736 }
737 }
738 break;
739 }
740 return 0;
741}
742
743static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
744{
745 const void *ptr = fw->data;
746 char rsn[64];
747 u16 lens[5];
748 size_t len;
749 unsigned int a, lenp, lencnt;
750 int ret = -EINVAL;
751 struct {
752 __le32 magic; /* 0x34303430 */
753 u8 reserved1[2];
754 u8 type; /* UNIX = 3 */
755 u8 model; /* C218T=1, C320T=2, CP204=3 */
756 u8 reserved2[8];
757 __le16 len[5];
758 } const *hdr = ptr;
759
760 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
761
762 if (fw->size < MOXA_FW_HDRLEN) {
763 strcpy(rsn, "too short (even header won't fit)");
764 goto err;
765 }
766 if (hdr->magic != cpu_to_le32(0x30343034)) {
767 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
768 goto err;
769 }
770 if (hdr->type != 3) {
771 sprintf(rsn, "not for linux, type is %u", hdr->type);
772 goto err;
773 }
774 if (moxa_check_fw_model(brd, hdr->model)) {
775 sprintf(rsn, "not for this card, model is %u", hdr->model);
776 goto err;
777 }
778
779 len = MOXA_FW_HDRLEN;
780 lencnt = hdr->model == 2 ? 5 : 3;
781 for (a = 0; a < ARRAY_SIZE(lens); a++) {
782 lens[a] = le16_to_cpu(hdr->len[a]);
783 if (lens[a] && len + lens[a] <= fw->size &&
784 moxa_check_fw(&fw->data[len]))
785 printk(KERN_WARNING "MOXA firmware: unexpected input "
786 "at offset %u, but going on\n", (u32)len);
787 if (!lens[a] && a < lencnt) {
788 sprintf(rsn, "too few entries in fw file");
789 goto err;
790 }
791 len += lens[a];
792 }
793
794 if (len != fw->size) {
795 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
796 (u32)len);
797 goto err;
798 }
799
800 ptr += MOXA_FW_HDRLEN;
801 lenp = 0; /* bios */
802
803 strcpy(rsn, "read above");
804
805 ret = moxa_load_bios(brd, ptr, lens[lenp]);
806 if (ret)
807 goto err;
808
809 /* we skip the tty section (lens[1]), since we don't need it */
810 ptr += lens[lenp] + lens[lenp + 1];
811 lenp += 2; /* comm */
812
813 if (hdr->model == 2) {
814 ret = moxa_load_320b(brd, ptr, lens[lenp]);
815 if (ret)
816 goto err;
817 /* skip another tty */
818 ptr += lens[lenp] + lens[lenp + 1];
819 lenp += 2;
820 }
821
822 ret = moxa_load_code(brd, ptr, lens[lenp]);
823 if (ret)
824 goto err;
825
826 return 0;
827err:
828 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
829 return ret;
830}
831
832static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
833{
834 const struct firmware *fw;
835 const char *file;
836 struct moxa_port *p;
837 unsigned int i;
838 int ret;
839
840 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
841 GFP_KERNEL);
842 if (brd->ports == NULL) {
843 printk(KERN_ERR "cannot allocate memory for ports\n");
844 ret = -ENOMEM;
845 goto err;
846 }
847
848 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
849 tty_port_init(&p->port);
850 p->port.ops = &moxa_port_ops;
851 p->type = PORT_16550A;
852 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
853 }
854
855 switch (brd->boardType) {
856 case MOXA_BOARD_C218_ISA:
857 case MOXA_BOARD_C218_PCI:
858 file = "c218tunx.cod";
859 break;
860 case MOXA_BOARD_CP204J:
861 file = "cp204unx.cod";
862 break;
863 default:
864 file = "c320tunx.cod";
865 break;
866 }
867
868 ret = request_firmware(&fw, file, dev);
869 if (ret) {
870 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
871 "you've placed '%s' file into your firmware "
872 "loader directory (e.g. /lib/firmware)\n",
873 file);
874 goto err_free;
875 }
876
877 ret = moxa_load_fw(brd, fw);
878
879 release_firmware(fw);
880
881 if (ret)
882 goto err_free;
883
884 spin_lock_bh(&moxa_lock);
885 brd->ready = 1;
886 if (!timer_pending(&moxaTimer))
887 mod_timer(&moxaTimer, jiffies + HZ / 50);
888 spin_unlock_bh(&moxa_lock);
889
890 return 0;
891err_free:
892 kfree(brd->ports);
893err:
894 return ret;
895}
896
897static void moxa_board_deinit(struct moxa_board_conf *brd)
898{
899 unsigned int a, opened;
900
901 mutex_lock(&moxa_openlock);
902 spin_lock_bh(&moxa_lock);
903 brd->ready = 0;
904 spin_unlock_bh(&moxa_lock);
905
906 /* pci hot-un-plug support */
907 for (a = 0; a < brd->numPorts; a++)
908 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
909 struct tty_struct *tty = tty_port_tty_get(
910 &brd->ports[a].port);
911 if (tty) {
912 tty_hangup(tty);
913 tty_kref_put(tty);
914 }
915 }
916 while (1) {
917 opened = 0;
918 for (a = 0; a < brd->numPorts; a++)
919 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
920 opened++;
921 mutex_unlock(&moxa_openlock);
922 if (!opened)
923 break;
924 msleep(50);
925 mutex_lock(&moxa_openlock);
926 }
927
928 iounmap(brd->basemem);
929 brd->basemem = NULL;
930 kfree(brd->ports);
931}
932
933#ifdef CONFIG_PCI
934static int __devinit moxa_pci_probe(struct pci_dev *pdev,
935 const struct pci_device_id *ent)
936{
937 struct moxa_board_conf *board;
938 unsigned int i;
939 int board_type = ent->driver_data;
940 int retval;
941
942 retval = pci_enable_device(pdev);
943 if (retval) {
944 dev_err(&pdev->dev, "can't enable pci device\n");
945 goto err;
946 }
947
948 for (i = 0; i < MAX_BOARDS; i++)
949 if (moxa_boards[i].basemem == NULL)
950 break;
951
952 retval = -ENODEV;
953 if (i >= MAX_BOARDS) {
954 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
955 "found. Board is ignored.\n", MAX_BOARDS);
956 goto err;
957 }
958
959 board = &moxa_boards[i];
960
961 retval = pci_request_region(pdev, 2, "moxa-base");
962 if (retval) {
963 dev_err(&pdev->dev, "can't request pci region 2\n");
964 goto err;
965 }
966
967 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
968 if (board->basemem == NULL) {
969 dev_err(&pdev->dev, "can't remap io space 2\n");
970 goto err_reg;
971 }
972
973 board->boardType = board_type;
974 switch (board_type) {
975 case MOXA_BOARD_C218_ISA:
976 case MOXA_BOARD_C218_PCI:
977 board->numPorts = 8;
978 break;
979
980 case MOXA_BOARD_CP204J:
981 board->numPorts = 4;
982 break;
983 default:
984 board->numPorts = 0;
985 break;
986 }
987 board->busType = MOXA_BUS_TYPE_PCI;
988
989 retval = moxa_init_board(board, &pdev->dev);
990 if (retval)
991 goto err_base;
992
993 pci_set_drvdata(pdev, board);
994
995 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
996 moxa_brdname[board_type - 1], board->numPorts);
997
998 return 0;
999err_base:
1000 iounmap(board->basemem);
1001 board->basemem = NULL;
1002err_reg:
1003 pci_release_region(pdev, 2);
1004err:
1005 return retval;
1006}
1007
1008static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1009{
1010 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1011
1012 moxa_board_deinit(brd);
1013
1014 pci_release_region(pdev, 2);
1015}
1016
1017static struct pci_driver moxa_pci_driver = {
1018 .name = "moxa",
1019 .id_table = moxa_pcibrds,
1020 .probe = moxa_pci_probe,
1021 .remove = __devexit_p(moxa_pci_remove)
1022};
1023#endif /* CONFIG_PCI */
1024
1025static int __init moxa_init(void)
1026{
1027 unsigned int isabrds = 0;
1028 int retval = 0;
1029 struct moxa_board_conf *brd = moxa_boards;
1030 unsigned int i;
1031
1032 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1033 MOXA_VERSION);
1034 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1035 if (!moxaDriver)
1036 return -ENOMEM;
1037
1038 moxaDriver->owner = THIS_MODULE;
1039 moxaDriver->name = "ttyMX";
1040 moxaDriver->major = ttymajor;
1041 moxaDriver->minor_start = 0;
1042 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1043 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1044 moxaDriver->init_termios = tty_std_termios;
1045 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1046 moxaDriver->init_termios.c_ispeed = 9600;
1047 moxaDriver->init_termios.c_ospeed = 9600;
1048 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1049 tty_set_operations(moxaDriver, &moxa_ops);
1050
1051 if (tty_register_driver(moxaDriver)) {
1052 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1053 put_tty_driver(moxaDriver);
1054 return -1;
1055 }
1056
1057 /* Find the boards defined from module args. */
1058
1059 for (i = 0; i < MAX_BOARDS; i++) {
1060 if (!baseaddr[i])
1061 break;
1062 if (type[i] == MOXA_BOARD_C218_ISA ||
1063 type[i] == MOXA_BOARD_C320_ISA) {
1064 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1065 isabrds + 1, moxa_brdname[type[i] - 1],
1066 baseaddr[i]);
1067 brd->boardType = type[i];
1068 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1069 numports[i];
1070 brd->busType = MOXA_BUS_TYPE_ISA;
1071 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1072 if (!brd->basemem) {
1073 printk(KERN_ERR "MOXA: can't remap %lx\n",
1074 baseaddr[i]);
1075 continue;
1076 }
1077 if (moxa_init_board(brd, NULL)) {
1078 iounmap(brd->basemem);
1079 brd->basemem = NULL;
1080 continue;
1081 }
1082
1083 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1084 "ready (%u ports, firmware loaded)\n",
1085 baseaddr[i], brd->numPorts);
1086
1087 brd++;
1088 isabrds++;
1089 }
1090 }
1091
1092#ifdef CONFIG_PCI
1093 retval = pci_register_driver(&moxa_pci_driver);
1094 if (retval) {
1095 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1096 if (isabrds)
1097 retval = 0;
1098 }
1099#endif
1100
1101 return retval;
1102}
1103
1104static void __exit moxa_exit(void)
1105{
1106 unsigned int i;
1107
1108#ifdef CONFIG_PCI
1109 pci_unregister_driver(&moxa_pci_driver);
1110#endif
1111
1112 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1113 if (moxa_boards[i].ready)
1114 moxa_board_deinit(&moxa_boards[i]);
1115
1116 del_timer_sync(&moxaTimer);
1117
1118 if (tty_unregister_driver(moxaDriver))
1119 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1120 "serial driver\n");
1121 put_tty_driver(moxaDriver);
1122}
1123
1124module_init(moxa_init);
1125module_exit(moxa_exit);
1126
1127static void moxa_shutdown(struct tty_port *port)
1128{
1129 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1130 MoxaPortDisable(ch);
1131 MoxaPortFlushData(ch, 2);
1132}
1133
1134static int moxa_carrier_raised(struct tty_port *port)
1135{
1136 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1137 int dcd;
1138
1139 spin_lock_irq(&port->lock);
1140 dcd = ch->DCDState;
1141 spin_unlock_irq(&port->lock);
1142 return dcd;
1143}
1144
1145static void moxa_dtr_rts(struct tty_port *port, int onoff)
1146{
1147 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1148 MoxaPortLineCtrl(ch, onoff, onoff);
1149}
1150
1151
1152static int moxa_open(struct tty_struct *tty, struct file *filp)
1153{
1154 struct moxa_board_conf *brd;
1155 struct moxa_port *ch;
1156 int port;
1157
1158 port = tty->index;
1159 if (port == MAX_PORTS) {
1160 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1161 }
1162 if (mutex_lock_interruptible(&moxa_openlock))
1163 return -ERESTARTSYS;
1164 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1165 if (!brd->ready) {
1166 mutex_unlock(&moxa_openlock);
1167 return -ENODEV;
1168 }
1169
1170 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1171 mutex_unlock(&moxa_openlock);
1172 return -ENODEV;
1173 }
1174
1175 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1176 ch->port.count++;
1177 tty->driver_data = ch;
1178 tty_port_tty_set(&ch->port, tty);
1179 mutex_lock(&ch->port.mutex);
1180 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1181 ch->statusflags = 0;
1182 moxa_set_tty_param(tty, tty->termios);
1183 MoxaPortLineCtrl(ch, 1, 1);
1184 MoxaPortEnable(ch);
1185 MoxaSetFifo(ch, ch->type == PORT_16550A);
1186 ch->port.flags |= ASYNC_INITIALIZED;
1187 }
1188 mutex_unlock(&ch->port.mutex);
1189 mutex_unlock(&moxa_openlock);
1190
1191 return tty_port_block_til_ready(&ch->port, tty, filp);
1192}
1193
1194static void moxa_close(struct tty_struct *tty, struct file *filp)
1195{
1196 struct moxa_port *ch = tty->driver_data;
1197 ch->cflag = tty->termios->c_cflag;
1198 tty_port_close(&ch->port, tty, filp);
1199}
1200
1201static int moxa_write(struct tty_struct *tty,
1202 const unsigned char *buf, int count)
1203{
1204 struct moxa_port *ch = tty->driver_data;
1205 unsigned long flags;
1206 int len;
1207
1208 if (ch == NULL)
1209 return 0;
1210
1211 spin_lock_irqsave(&moxa_lock, flags);
1212 len = MoxaPortWriteData(tty, buf, count);
1213 spin_unlock_irqrestore(&moxa_lock, flags);
1214
1215 set_bit(LOWWAIT, &ch->statusflags);
1216 return len;
1217}
1218
1219static int moxa_write_room(struct tty_struct *tty)
1220{
1221 struct moxa_port *ch;
1222
1223 if (tty->stopped)
1224 return 0;
1225 ch = tty->driver_data;
1226 if (ch == NULL)
1227 return 0;
1228 return MoxaPortTxFree(ch);
1229}
1230
1231static void moxa_flush_buffer(struct tty_struct *tty)
1232{
1233 struct moxa_port *ch = tty->driver_data;
1234
1235 if (ch == NULL)
1236 return;
1237 MoxaPortFlushData(ch, 1);
1238 tty_wakeup(tty);
1239}
1240
1241static int moxa_chars_in_buffer(struct tty_struct *tty)
1242{
1243 struct moxa_port *ch = tty->driver_data;
1244 int chars;
1245
1246 chars = MoxaPortTxQueue(ch);
1247 if (chars)
1248 /*
1249 * Make it possible to wakeup anything waiting for output
1250 * in tty_ioctl.c, etc.
1251 */
1252 set_bit(EMPTYWAIT, &ch->statusflags);
1253 return chars;
1254}
1255
1256static int moxa_tiocmget(struct tty_struct *tty)
1257{
1258 struct moxa_port *ch = tty->driver_data;
1259 int flag = 0, dtr, rts;
1260
1261 MoxaPortGetLineOut(ch, &dtr, &rts);
1262 if (dtr)
1263 flag |= TIOCM_DTR;
1264 if (rts)
1265 flag |= TIOCM_RTS;
1266 dtr = MoxaPortLineStatus(ch);
1267 if (dtr & 1)
1268 flag |= TIOCM_CTS;
1269 if (dtr & 2)
1270 flag |= TIOCM_DSR;
1271 if (dtr & 4)
1272 flag |= TIOCM_CD;
1273 return flag;
1274}
1275
1276static int moxa_tiocmset(struct tty_struct *tty,
1277 unsigned int set, unsigned int clear)
1278{
1279 struct moxa_port *ch;
1280 int dtr, rts;
1281
1282 mutex_lock(&moxa_openlock);
1283 ch = tty->driver_data;
1284 if (!ch) {
1285 mutex_unlock(&moxa_openlock);
1286 return -EINVAL;
1287 }
1288
1289 MoxaPortGetLineOut(ch, &dtr, &rts);
1290 if (set & TIOCM_RTS)
1291 rts = 1;
1292 if (set & TIOCM_DTR)
1293 dtr = 1;
1294 if (clear & TIOCM_RTS)
1295 rts = 0;
1296 if (clear & TIOCM_DTR)
1297 dtr = 0;
1298 MoxaPortLineCtrl(ch, dtr, rts);
1299 mutex_unlock(&moxa_openlock);
1300 return 0;
1301}
1302
1303static void moxa_set_termios(struct tty_struct *tty,
1304 struct ktermios *old_termios)
1305{
1306 struct moxa_port *ch = tty->driver_data;
1307
1308 if (ch == NULL)
1309 return;
1310 moxa_set_tty_param(tty, old_termios);
1311 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1312 wake_up_interruptible(&ch->port.open_wait);
1313}
1314
1315static void moxa_stop(struct tty_struct *tty)
1316{
1317 struct moxa_port *ch = tty->driver_data;
1318
1319 if (ch == NULL)
1320 return;
1321 MoxaPortTxDisable(ch);
1322 set_bit(TXSTOPPED, &ch->statusflags);
1323}
1324
1325
1326static void moxa_start(struct tty_struct *tty)
1327{
1328 struct moxa_port *ch = tty->driver_data;
1329
1330 if (ch == NULL)
1331 return;
1332
1333 if (!(ch->statusflags & TXSTOPPED))
1334 return;
1335
1336 MoxaPortTxEnable(ch);
1337 clear_bit(TXSTOPPED, &ch->statusflags);
1338}
1339
1340static void moxa_hangup(struct tty_struct *tty)
1341{
1342 struct moxa_port *ch = tty->driver_data;
1343 tty_port_hangup(&ch->port);
1344}
1345
1346static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1347{
1348 struct tty_struct *tty;
1349 unsigned long flags;
1350 dcd = !!dcd;
1351
1352 spin_lock_irqsave(&p->port.lock, flags);
1353 if (dcd != p->DCDState) {
1354 p->DCDState = dcd;
1355 spin_unlock_irqrestore(&p->port.lock, flags);
1356 tty = tty_port_tty_get(&p->port);
1357 if (tty && C_CLOCAL(tty) && !dcd)
1358 tty_hangup(tty);
1359 tty_kref_put(tty);
1360 }
1361 else
1362 spin_unlock_irqrestore(&p->port.lock, flags);
1363}
1364
1365static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1366 u16 __iomem *ip)
1367{
1368 struct tty_struct *tty = tty_port_tty_get(&p->port);
1369 void __iomem *ofsAddr;
1370 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1371 u16 intr;
1372
1373 if (tty) {
1374 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1375 MoxaPortTxQueue(p) == 0) {
1376 clear_bit(EMPTYWAIT, &p->statusflags);
1377 tty_wakeup(tty);
1378 }
1379 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1380 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1381 clear_bit(LOWWAIT, &p->statusflags);
1382 tty_wakeup(tty);
1383 }
1384
1385 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
1386 MoxaPortRxQueue(p) > 0) { /* RX */
1387 MoxaPortReadData(p);
1388 tty_schedule_flip(tty);
1389 }
1390 } else {
1391 clear_bit(EMPTYWAIT, &p->statusflags);
1392 MoxaPortFlushData(p, 0); /* flush RX */
1393 }
1394
1395 if (!handle) /* nothing else to do */
1396 goto put;
1397
1398 intr = readw(ip); /* port irq status */
1399 if (intr == 0)
1400 goto put;
1401
1402 writew(0, ip); /* ACK port */
1403 ofsAddr = p->tableAddr;
1404 if (intr & IntrTx) /* disable tx intr */
1405 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1406 ofsAddr + HostStat);
1407
1408 if (!inited)
1409 goto put;
1410
1411 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1412 tty_insert_flip_char(tty, 0, TTY_BREAK);
1413 tty_schedule_flip(tty);
1414 }
1415
1416 if (intr & IntrLine)
1417 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1418put:
1419 tty_kref_put(tty);
1420
1421 return 0;
1422}
1423
1424static void moxa_poll(unsigned long ignored)
1425{
1426 struct moxa_board_conf *brd;
1427 u16 __iomem *ip;
1428 unsigned int card, port, served = 0;
1429
1430 spin_lock(&moxa_lock);
1431 for (card = 0; card < MAX_BOARDS; card++) {
1432 brd = &moxa_boards[card];
1433 if (!brd->ready)
1434 continue;
1435
1436 served++;
1437
1438 ip = NULL;
1439 if (readb(brd->intPend) == 0xff)
1440 ip = brd->intTable + readb(brd->intNdx);
1441
1442 for (port = 0; port < brd->numPorts; port++)
1443 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1444
1445 if (ip)
1446 writeb(0, brd->intPend); /* ACK */
1447
1448 if (moxaLowWaterChk) {
1449 struct moxa_port *p = brd->ports;
1450 for (port = 0; port < brd->numPorts; port++, p++)
1451 if (p->lowChkFlag) {
1452 p->lowChkFlag = 0;
1453 moxa_low_water_check(p->tableAddr);
1454 }
1455 }
1456 }
1457 moxaLowWaterChk = 0;
1458
1459 if (served)
1460 mod_timer(&moxaTimer, jiffies + HZ / 50);
1461 spin_unlock(&moxa_lock);
1462}
1463
1464/******************************************************************************/
1465
1466static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1467{
1468 register struct ktermios *ts = tty->termios;
1469 struct moxa_port *ch = tty->driver_data;
1470 int rts, cts, txflow, rxflow, xany, baud;
1471
1472 rts = cts = txflow = rxflow = xany = 0;
1473 if (ts->c_cflag & CRTSCTS)
1474 rts = cts = 1;
1475 if (ts->c_iflag & IXON)
1476 txflow = 1;
1477 if (ts->c_iflag & IXOFF)
1478 rxflow = 1;
1479 if (ts->c_iflag & IXANY)
1480 xany = 1;
1481
1482 /* Clear the features we don't support */
1483 ts->c_cflag &= ~CMSPAR;
1484 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1485 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1486 if (baud == -1)
1487 baud = tty_termios_baud_rate(old_termios);
1488 /* Not put the baud rate into the termios data */
1489 tty_encode_baud_rate(tty, baud, baud);
1490}
1491
1492/*****************************************************************************
1493 * Driver level functions: *
1494 *****************************************************************************/
1495
1496static void MoxaPortFlushData(struct moxa_port *port, int mode)
1497{
1498 void __iomem *ofsAddr;
1499 if (mode < 0 || mode > 2)
1500 return;
1501 ofsAddr = port->tableAddr;
1502 moxafunc(ofsAddr, FC_FlushQueue, mode);
1503 if (mode != 1) {
1504 port->lowChkFlag = 0;
1505 moxa_low_water_check(ofsAddr);
1506 }
1507}
1508
1509/*
1510 * Moxa Port Number Description:
1511 *
1512 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1513 * the port number using in MOXA driver functions will be 0 to 31 for
1514 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1515 * to 127 for fourth. For example, if you setup three MOXA boards,
1516 * first board is C218, second board is C320-16 and third board is
1517 * C320-32. The port number of first board (C218 - 8 ports) is from
1518 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1519 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1520 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1521 * 127 will be invalid.
1522 *
1523 *
1524 * Moxa Functions Description:
1525 *
1526 * Function 1: Driver initialization routine, this routine must be
1527 * called when initialized driver.
1528 * Syntax:
1529 * void MoxaDriverInit();
1530 *
1531 *
1532 * Function 2: Moxa driver private IOCTL command processing.
1533 * Syntax:
1534 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1535 *
1536 * unsigned int cmd : IOCTL command
1537 * unsigned long arg : IOCTL argument
1538 * int port : port number (0 - 127)
1539 *
1540 * return: 0 (OK)
1541 * -EINVAL
1542 * -ENOIOCTLCMD
1543 *
1544 *
1545 * Function 6: Enable this port to start Tx/Rx data.
1546 * Syntax:
1547 * void MoxaPortEnable(int port);
1548 * int port : port number (0 - 127)
1549 *
1550 *
1551 * Function 7: Disable this port
1552 * Syntax:
1553 * void MoxaPortDisable(int port);
1554 * int port : port number (0 - 127)
1555 *
1556 *
1557 * Function 10: Setting baud rate of this port.
1558 * Syntax:
1559 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1560 * int port : port number (0 - 127)
1561 * long baud : baud rate (50 - 115200)
1562 *
1563 * return: 0 : this port is invalid or baud < 50
1564 * 50 - 115200 : the real baud rate set to the port, if
1565 * the argument baud is large than maximun
1566 * available baud rate, the real setting
1567 * baud rate will be the maximun baud rate.
1568 *
1569 *
1570 * Function 12: Configure the port.
1571 * Syntax:
1572 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1573 * int port : port number (0 - 127)
1574 * struct ktermios * termio : termio structure pointer
1575 * speed_t baud : baud rate
1576 *
1577 * return: -1 : this port is invalid or termio == NULL
1578 * 0 : setting O.K.
1579 *
1580 *
1581 * Function 13: Get the DTR/RTS state of this port.
1582 * Syntax:
1583 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1584 * int port : port number (0 - 127)
1585 * int * dtrState : pointer to INT to receive the current DTR
1586 * state. (if NULL, this function will not
1587 * write to this address)
1588 * int * rtsState : pointer to INT to receive the current RTS
1589 * state. (if NULL, this function will not
1590 * write to this address)
1591 *
1592 * return: -1 : this port is invalid
1593 * 0 : O.K.
1594 *
1595 *
1596 * Function 14: Setting the DTR/RTS output state of this port.
1597 * Syntax:
1598 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1599 * int port : port number (0 - 127)
1600 * int dtrState : DTR output state (0: off, 1: on)
1601 * int rtsState : RTS output state (0: off, 1: on)
1602 *
1603 *
1604 * Function 15: Setting the flow control of this port.
1605 * Syntax:
1606 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1607 * int txFlow,int xany);
1608 * int port : port number (0 - 127)
1609 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1610 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1611 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1612 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1613 * int xany : S/W XANY flow control (0: no, 1: yes)
1614 *
1615 *
1616 * Function 16: Get ths line status of this port
1617 * Syntax:
1618 * int MoxaPortLineStatus(int port);
1619 * int port : port number (0 - 127)
1620 *
1621 * return: Bit 0 - CTS state (0: off, 1: on)
1622 * Bit 1 - DSR state (0: off, 1: on)
1623 * Bit 2 - DCD state (0: off, 1: on)
1624 *
1625 *
1626 * Function 19: Flush the Rx/Tx buffer data of this port.
1627 * Syntax:
1628 * void MoxaPortFlushData(int port, int mode);
1629 * int port : port number (0 - 127)
1630 * int mode
1631 * 0 : flush the Rx buffer
1632 * 1 : flush the Tx buffer
1633 * 2 : flush the Rx and Tx buffer
1634 *
1635 *
1636 * Function 20: Write data.
1637 * Syntax:
1638 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1639 * int port : port number (0 - 127)
1640 * unsigned char * buffer : pointer to write data buffer.
1641 * int length : write data length
1642 *
1643 * return: 0 - length : real write data length
1644 *
1645 *
1646 * Function 21: Read data.
1647 * Syntax:
1648 * int MoxaPortReadData(int port, struct tty_struct *tty);
1649 * int port : port number (0 - 127)
1650 * struct tty_struct *tty : tty for data
1651 *
1652 * return: 0 - length : real read data length
1653 *
1654 *
1655 * Function 24: Get the Tx buffer current queued data bytes
1656 * Syntax:
1657 * int MoxaPortTxQueue(int port);
1658 * int port : port number (0 - 127)
1659 *
1660 * return: .. : Tx buffer current queued data bytes
1661 *
1662 *
1663 * Function 25: Get the Tx buffer current free space
1664 * Syntax:
1665 * int MoxaPortTxFree(int port);
1666 * int port : port number (0 - 127)
1667 *
1668 * return: .. : Tx buffer current free space
1669 *
1670 *
1671 * Function 26: Get the Rx buffer current queued data bytes
1672 * Syntax:
1673 * int MoxaPortRxQueue(int port);
1674 * int port : port number (0 - 127)
1675 *
1676 * return: .. : Rx buffer current queued data bytes
1677 *
1678 *
1679 * Function 28: Disable port data transmission.
1680 * Syntax:
1681 * void MoxaPortTxDisable(int port);
1682 * int port : port number (0 - 127)
1683 *
1684 *
1685 * Function 29: Enable port data transmission.
1686 * Syntax:
1687 * void MoxaPortTxEnable(int port);
1688 * int port : port number (0 - 127)
1689 *
1690 *
1691 * Function 31: Get the received BREAK signal count and reset it.
1692 * Syntax:
1693 * int MoxaPortResetBrkCnt(int port);
1694 * int port : port number (0 - 127)
1695 *
1696 * return: 0 - .. : BREAK signal count
1697 *
1698 *
1699 */
1700
1701static void MoxaPortEnable(struct moxa_port *port)
1702{
1703 void __iomem *ofsAddr;
1704 u16 lowwater = 512;
1705
1706 ofsAddr = port->tableAddr;
1707 writew(lowwater, ofsAddr + Low_water);
1708 if (MOXA_IS_320(port->board))
1709 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1710 else
1711 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1712 ofsAddr + HostStat);
1713
1714 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1715 moxafunc(ofsAddr, FC_FlushQueue, 2);
1716
1717 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1718 MoxaPortLineStatus(port);
1719}
1720
1721static void MoxaPortDisable(struct moxa_port *port)
1722{
1723 void __iomem *ofsAddr = port->tableAddr;
1724
1725 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1726 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1727 writew(0, ofsAddr + HostStat);
1728 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1729}
1730
1731static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1732{
1733 void __iomem *ofsAddr = port->tableAddr;
1734 unsigned int clock, val;
1735 speed_t max;
1736
1737 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1738 if (baud < 50)
1739 return 0;
1740 if (baud > max)
1741 baud = max;
1742 clock = 921600;
1743 val = clock / baud;
1744 moxafunc(ofsAddr, FC_SetBaud, val);
1745 baud = clock / val;
1746 return baud;
1747}
1748
1749static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1750 speed_t baud)
1751{
1752 void __iomem *ofsAddr;
1753 tcflag_t mode = 0;
1754
1755 ofsAddr = port->tableAddr;
1756
1757 mode = termio->c_cflag & CSIZE;
1758 if (mode == CS5)
1759 mode = MX_CS5;
1760 else if (mode == CS6)
1761 mode = MX_CS6;
1762 else if (mode == CS7)
1763 mode = MX_CS7;
1764 else if (mode == CS8)
1765 mode = MX_CS8;
1766
1767 if (termio->c_cflag & CSTOPB) {
1768 if (mode == MX_CS5)
1769 mode |= MX_STOP15;
1770 else
1771 mode |= MX_STOP2;
1772 } else
1773 mode |= MX_STOP1;
1774
1775 if (termio->c_cflag & PARENB) {
1776 if (termio->c_cflag & PARODD)
1777 mode |= MX_PARODD;
1778 else
1779 mode |= MX_PAREVEN;
1780 } else
1781 mode |= MX_PARNONE;
1782
1783 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1784
1785 if (MOXA_IS_320(port->board) && baud >= 921600)
1786 return -1;
1787
1788 baud = MoxaPortSetBaud(port, baud);
1789
1790 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1791 spin_lock_irq(&moxafunc_lock);
1792 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1793 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1794 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1795 moxa_wait_finish(ofsAddr);
1796 spin_unlock_irq(&moxafunc_lock);
1797
1798 }
1799 return baud;
1800}
1801
1802static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1803 int *rtsState)
1804{
1805 if (dtrState)
1806 *dtrState = !!(port->lineCtrl & DTR_ON);
1807 if (rtsState)
1808 *rtsState = !!(port->lineCtrl & RTS_ON);
1809
1810 return 0;
1811}
1812
1813static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1814{
1815 u8 mode = 0;
1816
1817 if (dtr)
1818 mode |= DTR_ON;
1819 if (rts)
1820 mode |= RTS_ON;
1821 port->lineCtrl = mode;
1822 moxafunc(port->tableAddr, FC_LineControl, mode);
1823}
1824
1825static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1826 int txflow, int rxflow, int txany)
1827{
1828 int mode = 0;
1829
1830 if (rts)
1831 mode |= RTS_FlowCtl;
1832 if (cts)
1833 mode |= CTS_FlowCtl;
1834 if (txflow)
1835 mode |= Tx_FlowCtl;
1836 if (rxflow)
1837 mode |= Rx_FlowCtl;
1838 if (txany)
1839 mode |= IXM_IXANY;
1840 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1841}
1842
1843static int MoxaPortLineStatus(struct moxa_port *port)
1844{
1845 void __iomem *ofsAddr;
1846 int val;
1847
1848 ofsAddr = port->tableAddr;
1849 if (MOXA_IS_320(port->board))
1850 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1851 else
1852 val = readw(ofsAddr + FlagStat) >> 4;
1853 val &= 0x0B;
1854 if (val & 8)
1855 val |= 4;
1856 moxa_new_dcdstate(port, val & 8);
1857 val &= 7;
1858 return val;
1859}
1860
1861static int MoxaPortWriteData(struct tty_struct *tty,
1862 const unsigned char *buffer, int len)
1863{
1864 struct moxa_port *port = tty->driver_data;
1865 void __iomem *baseAddr, *ofsAddr, *ofs;
1866 unsigned int c, total;
1867 u16 head, tail, tx_mask, spage, epage;
1868 u16 pageno, pageofs, bufhead;
1869
1870 ofsAddr = port->tableAddr;
1871 baseAddr = port->board->basemem;
1872 tx_mask = readw(ofsAddr + TX_mask);
1873 spage = readw(ofsAddr + Page_txb);
1874 epage = readw(ofsAddr + EndPage_txb);
1875 tail = readw(ofsAddr + TXwptr);
1876 head = readw(ofsAddr + TXrptr);
1877 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1878 if (c > len)
1879 c = len;
1880 moxaLog.txcnt[port->port.tty->index] += c;
1881 total = c;
1882 if (spage == epage) {
1883 bufhead = readw(ofsAddr + Ofs_txb);
1884 writew(spage, baseAddr + Control_reg);
1885 while (c > 0) {
1886 if (head > tail)
1887 len = head - tail - 1;
1888 else
1889 len = tx_mask + 1 - tail;
1890 len = (c > len) ? len : c;
1891 ofs = baseAddr + DynPage_addr + bufhead + tail;
1892 memcpy_toio(ofs, buffer, len);
1893 buffer += len;
1894 tail = (tail + len) & tx_mask;
1895 c -= len;
1896 }
1897 } else {
1898 pageno = spage + (tail >> 13);
1899 pageofs = tail & Page_mask;
1900 while (c > 0) {
1901 len = Page_size - pageofs;
1902 if (len > c)
1903 len = c;
1904 writeb(pageno, baseAddr + Control_reg);
1905 ofs = baseAddr + DynPage_addr + pageofs;
1906 memcpy_toio(ofs, buffer, len);
1907 buffer += len;
1908 if (++pageno == epage)
1909 pageno = spage;
1910 pageofs = 0;
1911 c -= len;
1912 }
1913 tail = (tail + total) & tx_mask;
1914 }
1915 writew(tail, ofsAddr + TXwptr);
1916 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1917 return total;
1918}
1919
1920static int MoxaPortReadData(struct moxa_port *port)
1921{
1922 struct tty_struct *tty = port->port.tty;
1923 unsigned char *dst;
1924 void __iomem *baseAddr, *ofsAddr, *ofs;
1925 unsigned int count, len, total;
1926 u16 tail, rx_mask, spage, epage;
1927 u16 pageno, pageofs, bufhead, head;
1928
1929 ofsAddr = port->tableAddr;
1930 baseAddr = port->board->basemem;
1931 head = readw(ofsAddr + RXrptr);
1932 tail = readw(ofsAddr + RXwptr);
1933 rx_mask = readw(ofsAddr + RX_mask);
1934 spage = readw(ofsAddr + Page_rxb);
1935 epage = readw(ofsAddr + EndPage_rxb);
1936 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1937 if (count == 0)
1938 return 0;
1939
1940 total = count;
1941 moxaLog.rxcnt[tty->index] += total;
1942 if (spage == epage) {
1943 bufhead = readw(ofsAddr + Ofs_rxb);
1944 writew(spage, baseAddr + Control_reg);
1945 while (count > 0) {
1946 ofs = baseAddr + DynPage_addr + bufhead + head;
1947 len = (tail >= head) ? (tail - head) :
1948 (rx_mask + 1 - head);
1949 len = tty_prepare_flip_string(tty, &dst,
1950 min(len, count));
1951 memcpy_fromio(dst, ofs, len);
1952 head = (head + len) & rx_mask;
1953 count -= len;
1954 }
1955 } else {
1956 pageno = spage + (head >> 13);
1957 pageofs = head & Page_mask;
1958 while (count > 0) {
1959 writew(pageno, baseAddr + Control_reg);
1960 ofs = baseAddr + DynPage_addr + pageofs;
1961 len = tty_prepare_flip_string(tty, &dst,
1962 min(Page_size - pageofs, count));
1963 memcpy_fromio(dst, ofs, len);
1964
1965 count -= len;
1966 pageofs = (pageofs + len) & Page_mask;
1967 if (pageofs == 0 && ++pageno == epage)
1968 pageno = spage;
1969 }
1970 head = (head + total) & rx_mask;
1971 }
1972 writew(head, ofsAddr + RXrptr);
1973 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1974 moxaLowWaterChk = 1;
1975 port->lowChkFlag = 1;
1976 }
1977 return total;
1978}
1979
1980
1981static int MoxaPortTxQueue(struct moxa_port *port)
1982{
1983 void __iomem *ofsAddr = port->tableAddr;
1984 u16 rptr, wptr, mask;
1985
1986 rptr = readw(ofsAddr + TXrptr);
1987 wptr = readw(ofsAddr + TXwptr);
1988 mask = readw(ofsAddr + TX_mask);
1989 return (wptr - rptr) & mask;
1990}
1991
1992static int MoxaPortTxFree(struct moxa_port *port)
1993{
1994 void __iomem *ofsAddr = port->tableAddr;
1995 u16 rptr, wptr, mask;
1996
1997 rptr = readw(ofsAddr + TXrptr);
1998 wptr = readw(ofsAddr + TXwptr);
1999 mask = readw(ofsAddr + TX_mask);
2000 return mask - ((wptr - rptr) & mask);
2001}
2002
2003static int MoxaPortRxQueue(struct moxa_port *port)
2004{
2005 void __iomem *ofsAddr = port->tableAddr;
2006 u16 rptr, wptr, mask;
2007
2008 rptr = readw(ofsAddr + RXrptr);
2009 wptr = readw(ofsAddr + RXwptr);
2010 mask = readw(ofsAddr + RX_mask);
2011 return (wptr - rptr) & mask;
2012}
2013
2014static void MoxaPortTxDisable(struct moxa_port *port)
2015{
2016 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2017}
2018
2019static void MoxaPortTxEnable(struct moxa_port *port)
2020{
2021 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2022}
2023
2024static int moxa_get_serial_info(struct moxa_port *info,
2025 struct serial_struct __user *retinfo)
2026{
2027 struct serial_struct tmp = {
2028 .type = info->type,
2029 .line = info->port.tty->index,
2030 .flags = info->port.flags,
2031 .baud_base = 921600,
2032 .close_delay = info->port.close_delay
2033 };
2034 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2035}
2036
2037
2038static int moxa_set_serial_info(struct moxa_port *info,
2039 struct serial_struct __user *new_info)
2040{
2041 struct serial_struct new_serial;
2042
2043 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2044 return -EFAULT;
2045
2046 if (new_serial.irq != 0 || new_serial.port != 0 ||
2047 new_serial.custom_divisor != 0 ||
2048 new_serial.baud_base != 921600)
2049 return -EPERM;
2050
2051 if (!capable(CAP_SYS_ADMIN)) {
2052 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2053 (info->port.flags & ~ASYNC_USR_MASK)))
2054 return -EPERM;
2055 } else
2056 info->port.close_delay = new_serial.close_delay * HZ / 100;
2057
2058 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2059 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2060
2061 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2062
2063 info->type = new_serial.type;
2064 return 0;
2065}
2066
2067
2068
2069/*****************************************************************************
2070 * Static local functions: *
2071 *****************************************************************************/
2072
2073static void MoxaSetFifo(struct moxa_port *port, int enable)
2074{
2075 void __iomem *ofsAddr = port->tableAddr;
2076
2077 if (!enable) {
2078 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2079 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2080 } else {
2081 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2082 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2083 }
2084}
diff --git a/drivers/tty/moxa.h b/drivers/tty/moxa.h
new file mode 100644
index 000000000000..87d16ce57be7
--- /dev/null
+++ b/drivers/tty/moxa.h
@@ -0,0 +1,304 @@
1#ifndef MOXA_H_FILE
2#define MOXA_H_FILE
3
4#define MOXA 0x400
5#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
6#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
7#define MOXA_GETDATACOUNT (MOXA + 23)
8#define MOXA_GET_IOQUEUE (MOXA + 27)
9#define MOXA_FLUSH_QUEUE (MOXA + 28)
10#define MOXA_GETMSTATUS (MOXA + 65)
11
12/*
13 * System Configuration
14 */
15
16#define Magic_code 0x404
17
18/*
19 * for C218 BIOS initialization
20 */
21#define C218_ConfBase 0x800
22#define C218_status (C218_ConfBase + 0) /* BIOS running status */
23#define C218_diag (C218_ConfBase + 2) /* diagnostic status */
24#define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
25#define C218DLoad_len (C218_ConfBase + 6) /* WORD */
26#define C218check_sum (C218_ConfBase + 8) /* BYTE */
27#define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
28#define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
29#define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
30#define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
31#define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
32
33#define C218_LoadBuf 0x0F00
34#define C218_KeyCode 0x218
35#define CP204J_KeyCode 0x204
36
37/*
38 * for C320 BIOS initialization
39 */
40#define C320_ConfBase 0x800
41#define C320_LoadBuf 0x0f00
42#define STS_init 0x05 /* for C320_status */
43
44#define C320_status C320_ConfBase + 0 /* BIOS running status */
45#define C320_diag C320_ConfBase + 2 /* diagnostic status */
46#define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
47#define C320DLoad_len C320_ConfBase + 6 /* WORD */
48#define C320check_sum C320_ConfBase + 8 /* WORD */
49#define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
50#define C320bapi_len C320_ConfBase + 0x0c /* WORD */
51#define C320UART_no C320_ConfBase + 0x0e /* WORD */
52
53#define C320_KeyCode 0x320
54
55#define FixPage_addr 0x0000 /* starting addr of static page */
56#define DynPage_addr 0x2000 /* starting addr of dynamic page */
57#define C218_start 0x3000 /* starting addr of C218 BIOS prg */
58#define Control_reg 0x1ff0 /* select page and reset control */
59#define HW_reset 0x80
60
61/*
62 * Function Codes
63 */
64#define FC_CardReset 0x80
65#define FC_ChannelReset 1 /* C320 firmware not supported */
66#define FC_EnableCH 2
67#define FC_DisableCH 3
68#define FC_SetParam 4
69#define FC_SetMode 5
70#define FC_SetRate 6
71#define FC_LineControl 7
72#define FC_LineStatus 8
73#define FC_XmitControl 9
74#define FC_FlushQueue 10
75#define FC_SendBreak 11
76#define FC_StopBreak 12
77#define FC_LoopbackON 13
78#define FC_LoopbackOFF 14
79#define FC_ClrIrqTable 15
80#define FC_SendXon 16
81#define FC_SetTermIrq 17 /* C320 firmware not supported */
82#define FC_SetCntIrq 18 /* C320 firmware not supported */
83#define FC_SetBreakIrq 19
84#define FC_SetLineIrq 20
85#define FC_SetFlowCtl 21
86#define FC_GenIrq 22
87#define FC_InCD180 23
88#define FC_OutCD180 24
89#define FC_InUARTreg 23
90#define FC_OutUARTreg 24
91#define FC_SetXonXoff 25
92#define FC_OutCD180CCR 26
93#define FC_ExtIQueue 27
94#define FC_ExtOQueue 28
95#define FC_ClrLineIrq 29
96#define FC_HWFlowCtl 30
97#define FC_GetClockRate 35
98#define FC_SetBaud 36
99#define FC_SetDataMode 41
100#define FC_GetCCSR 43
101#define FC_GetDataError 45
102#define FC_RxControl 50
103#define FC_ImmSend 51
104#define FC_SetXonState 52
105#define FC_SetXoffState 53
106#define FC_SetRxFIFOTrig 54
107#define FC_SetTxFIFOCnt 55
108#define FC_UnixRate 56
109#define FC_UnixResetTimer 57
110
111#define RxFIFOTrig1 0
112#define RxFIFOTrig4 1
113#define RxFIFOTrig8 2
114#define RxFIFOTrig14 3
115
116/*
117 * Dual-Ported RAM
118 */
119#define DRAM_global 0
120#define INT_data (DRAM_global + 0)
121#define Config_base (DRAM_global + 0x108)
122
123#define IRQindex (INT_data + 0)
124#define IRQpending (INT_data + 4)
125#define IRQtable (INT_data + 8)
126
127/*
128 * Interrupt Status
129 */
130#define IntrRx 0x01 /* receiver data O.K. */
131#define IntrTx 0x02 /* transmit buffer empty */
132#define IntrFunc 0x04 /* function complete */
133#define IntrBreak 0x08 /* received break */
134#define IntrLine 0x10 /* line status change
135 for transmitter */
136#define IntrIntr 0x20 /* received INTR code */
137#define IntrQuit 0x40 /* received QUIT code */
138#define IntrEOF 0x80 /* received EOF code */
139
140#define IntrRxTrigger 0x100 /* rx data count reach tigger value */
141#define IntrTxTrigger 0x200 /* tx data count below trigger value */
142
143#define Magic_no (Config_base + 0)
144#define Card_model_no (Config_base + 2)
145#define Total_ports (Config_base + 4)
146#define Module_cnt (Config_base + 8)
147#define Module_no (Config_base + 10)
148#define Timer_10ms (Config_base + 14)
149#define Disable_IRQ (Config_base + 20)
150#define TMS320_PORT1 (Config_base + 22)
151#define TMS320_PORT2 (Config_base + 24)
152#define TMS320_CLOCK (Config_base + 26)
153
154/*
155 * DATA BUFFER in DRAM
156 */
157#define Extern_table 0x400 /* Base address of the external table
158 (24 words * 64) total 3K bytes
159 (24 words * 128) total 6K bytes */
160#define Extern_size 0x60 /* 96 bytes */
161#define RXrptr 0x00 /* read pointer for RX buffer */
162#define RXwptr 0x02 /* write pointer for RX buffer */
163#define TXrptr 0x04 /* read pointer for TX buffer */
164#define TXwptr 0x06 /* write pointer for TX buffer */
165#define HostStat 0x08 /* IRQ flag and general flag */
166#define FlagStat 0x0A
167#define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
168 /* x x x x | | | | */
169 /* | | | + CTS flow */
170 /* | | +--- RTS flow */
171 /* | +------ TX Xon/Xoff */
172 /* +--------- RX Xon/Xoff */
173#define Break_cnt 0x0E /* received break count */
174#define CD180TXirq 0x10 /* if non-0: enable TX irq */
175#define RX_mask 0x12
176#define TX_mask 0x14
177#define Ofs_rxb 0x16
178#define Ofs_txb 0x18
179#define Page_rxb 0x1A
180#define Page_txb 0x1C
181#define EndPage_rxb 0x1E
182#define EndPage_txb 0x20
183#define Data_error 0x22
184#define RxTrigger 0x28
185#define TxTrigger 0x2a
186
187#define rRXwptr 0x34
188#define Low_water 0x36
189
190#define FuncCode 0x40
191#define FuncArg 0x42
192#define FuncArg1 0x44
193
194#define C218rx_size 0x2000 /* 8K bytes */
195#define C218tx_size 0x8000 /* 32K bytes */
196
197#define C218rx_mask (C218rx_size - 1)
198#define C218tx_mask (C218tx_size - 1)
199
200#define C320p8rx_size 0x2000
201#define C320p8tx_size 0x8000
202#define C320p8rx_mask (C320p8rx_size - 1)
203#define C320p8tx_mask (C320p8tx_size - 1)
204
205#define C320p16rx_size 0x2000
206#define C320p16tx_size 0x4000
207#define C320p16rx_mask (C320p16rx_size - 1)
208#define C320p16tx_mask (C320p16tx_size - 1)
209
210#define C320p24rx_size 0x2000
211#define C320p24tx_size 0x2000
212#define C320p24rx_mask (C320p24rx_size - 1)
213#define C320p24tx_mask (C320p24tx_size - 1)
214
215#define C320p32rx_size 0x1000
216#define C320p32tx_size 0x1000
217#define C320p32rx_mask (C320p32rx_size - 1)
218#define C320p32tx_mask (C320p32tx_size - 1)
219
220#define Page_size 0x2000U
221#define Page_mask (Page_size - 1)
222#define C218rx_spage 3
223#define C218tx_spage 4
224#define C218rx_pageno 1
225#define C218tx_pageno 4
226#define C218buf_pageno 5
227
228#define C320p8rx_spage 3
229#define C320p8tx_spage 4
230#define C320p8rx_pgno 1
231#define C320p8tx_pgno 4
232#define C320p8buf_pgno 5
233
234#define C320p16rx_spage 3
235#define C320p16tx_spage 4
236#define C320p16rx_pgno 1
237#define C320p16tx_pgno 2
238#define C320p16buf_pgno 3
239
240#define C320p24rx_spage 3
241#define C320p24tx_spage 4
242#define C320p24rx_pgno 1
243#define C320p24tx_pgno 1
244#define C320p24buf_pgno 2
245
246#define C320p32rx_spage 3
247#define C320p32tx_ofs C320p32rx_size
248#define C320p32tx_spage 3
249#define C320p32buf_pgno 1
250
251/*
252 * Host Status
253 */
254#define WakeupRx 0x01
255#define WakeupTx 0x02
256#define WakeupBreak 0x08
257#define WakeupLine 0x10
258#define WakeupIntr 0x20
259#define WakeupQuit 0x40
260#define WakeupEOF 0x80 /* used in VTIME control */
261#define WakeupRxTrigger 0x100
262#define WakeupTxTrigger 0x200
263/*
264 * Flag status
265 */
266#define Rx_over 0x01
267#define Xoff_state 0x02
268#define Tx_flowOff 0x04
269#define Tx_enable 0x08
270#define CTS_state 0x10
271#define DSR_state 0x20
272#define DCD_state 0x80
273/*
274 * FlowControl
275 */
276#define CTS_FlowCtl 1
277#define RTS_FlowCtl 2
278#define Tx_FlowCtl 4
279#define Rx_FlowCtl 8
280#define IXM_IXANY 0x10
281
282#define LowWater 128
283
284#define DTR_ON 1
285#define RTS_ON 2
286#define CTS_ON 1
287#define DSR_ON 2
288#define DCD_ON 8
289
290/* mode definition */
291#define MX_CS8 0x03
292#define MX_CS7 0x02
293#define MX_CS6 0x01
294#define MX_CS5 0x00
295
296#define MX_STOP1 0x00
297#define MX_STOP15 0x04
298#define MX_STOP2 0x08
299
300#define MX_PARNONE 0x00
301#define MX_PAREVEN 0x40
302#define MX_PARODD 0xC0
303
304#endif
diff --git a/drivers/tty/mxser.c b/drivers/tty/mxser.c
new file mode 100644
index 000000000000..d188f378684d
--- /dev/null
+++ b/drivers/tty/mxser.c
@@ -0,0 +1,2757 @@
1/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
6 *
7 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
18 * www.moxa.com.
19 * - Fixed x86_64 cleanness
20 */
21
22#include <linux/module.h>
23#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/delay.h>
39#include <linux/pci.h>
40#include <linux/bitops.h>
41#include <linux/slab.h>
42
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/uaccess.h>
47
48#include "mxser.h"
49
50#define MXSER_VERSION "2.0.5" /* 1.14 */
51#define MXSERMAJOR 174
52
53#define MXSER_BOARDS 4 /* Max. boards */
54#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
55#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56#define MXSER_ISR_PASS_LIMIT 100
57
58/*CheckIsMoxaMust return value*/
59#define MOXA_OTHER_UART 0x00
60#define MOXA_MUST_MU150_HWID 0x01
61#define MOXA_MUST_MU860_HWID 0x02
62
63#define WAKEUP_CHARS 256
64
65#define UART_MCR_AFE 0x20
66#define UART_LSR_SPECIAL 0x1E
67
68#define PCI_DEVICE_ID_POS104UL 0x1044
69#define PCI_DEVICE_ID_CB108 0x1080
70#define PCI_DEVICE_ID_CP102UF 0x1023
71#define PCI_DEVICE_ID_CP112UL 0x1120
72#define PCI_DEVICE_ID_CB114 0x1142
73#define PCI_DEVICE_ID_CP114UL 0x1143
74#define PCI_DEVICE_ID_CB134I 0x1341
75#define PCI_DEVICE_ID_CP138U 0x1380
76
77
78#define C168_ASIC_ID 1
79#define C104_ASIC_ID 2
80#define C102_ASIC_ID 0xB
81#define CI132_ASIC_ID 4
82#define CI134_ASIC_ID 3
83#define CI104J_ASIC_ID 5
84
85#define MXSER_HIGHBAUD 1
86#define MXSER_HAS2 2
87
88/* This is only for PCI */
89static const struct {
90 int type;
91 int tx_fifo;
92 int rx_fifo;
93 int xmit_fifo_size;
94 int rx_high_water;
95 int rx_trigger;
96 int rx_low_water;
97 long max_baud;
98} Gpci_uart_info[] = {
99 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
100 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
101 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
102};
103#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
104
105struct mxser_cardinfo {
106 char *name;
107 unsigned int nports;
108 unsigned int flags;
109};
110
111static const struct mxser_cardinfo mxser_cards[] = {
112/* 0*/ { "C168 series", 8, },
113 { "C104 series", 4, },
114 { "CI-104J series", 4, },
115 { "C168H/PCI series", 8, },
116 { "C104H/PCI series", 4, },
117/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
118 { "CI-132 series", 4, MXSER_HAS2 },
119 { "CI-134 series", 4, },
120 { "CP-132 series", 2, },
121 { "CP-114 series", 4, },
122/*10*/ { "CT-114 series", 4, },
123 { "CP-102 series", 2, MXSER_HIGHBAUD },
124 { "CP-104U series", 4, },
125 { "CP-168U series", 8, },
126 { "CP-132U series", 2, },
127/*15*/ { "CP-134U series", 4, },
128 { "CP-104JU series", 4, },
129 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
130 { "CP-118U series", 8, },
131 { "CP-102UL series", 2, },
132/*20*/ { "CP-102U series", 2, },
133 { "CP-118EL series", 8, },
134 { "CP-168EL series", 8, },
135 { "CP-104EL series", 4, },
136 { "CB-108 series", 8, },
137/*25*/ { "CB-114 series", 4, },
138 { "CB-134I series", 4, },
139 { "CP-138U series", 8, },
140 { "POS-104UL series", 4, },
141 { "CP-114UL series", 4, },
142/*30*/ { "CP-102UF series", 2, },
143 { "CP-112UL series", 2, },
144};
145
146/* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148static struct pci_device_id mxser_pcibrds[] = {
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 31 },
175 { }
176};
177MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
178
179static unsigned long ioaddr[MXSER_BOARDS];
180static int ttymajor = MXSERMAJOR;
181
182/* Variables for insmod */
183
184MODULE_AUTHOR("Casper Yang");
185MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
186module_param_array(ioaddr, ulong, NULL, 0);
187MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
188module_param(ttymajor, int, 0);
189MODULE_LICENSE("GPL");
190
191struct mxser_log {
192 int tick;
193 unsigned long rxcnt[MXSER_PORTS];
194 unsigned long txcnt[MXSER_PORTS];
195};
196
197struct mxser_mon {
198 unsigned long rxcnt;
199 unsigned long txcnt;
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
202 int modem_status;
203 unsigned char hold_reason;
204};
205
206struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
212
213 long baudrate[32];
214 int databits[32];
215 int stopbits[32];
216 int parity[32];
217 int flowctrl[32];
218 int fifo[32];
219 int iftype[32];
220};
221
222struct mxser_board;
223
224struct mxser_port {
225 struct tty_port port;
226 struct mxser_board *board;
227
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
230 int max_baud;
231
232 int rx_high_water;
233 int rx_trigger; /* Rx fifo trigger level */
234 int rx_low_water;
235 int baud_base; /* max. speed */
236 int type; /* UART type */
237
238 int x_char; /* xon/xoff character */
239 int IER; /* Interrupt Enable Register */
240 int MCR; /* Modem control register */
241
242 unsigned char stop_rx;
243 unsigned char ldisc_stop_rx;
244
245 int custom_divisor;
246 unsigned char err_shadow;
247
248 struct async_icount icount; /* kernel counters for 4 input interrupts */
249 int timeout;
250
251 int read_status_mask;
252 int ignore_status_mask;
253 int xmit_fifo_size;
254 int xmit_head;
255 int xmit_tail;
256 int xmit_cnt;
257
258 struct ktermios normal_termios;
259
260 struct mxser_mon mon_data;
261
262 spinlock_t slock;
263};
264
265struct mxser_board {
266 unsigned int idx;
267 int irq;
268 const struct mxser_cardinfo *info;
269 unsigned long vector;
270 unsigned long vector_mask;
271
272 int chip_flag;
273 int uart_type;
274
275 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
276};
277
278struct mxser_mstatus {
279 tcflag_t cflag;
280 int cts;
281 int dsr;
282 int ri;
283 int dcd;
284};
285
286static struct mxser_board mxser_boards[MXSER_BOARDS];
287static struct tty_driver *mxvar_sdriver;
288static struct mxser_log mxvar_log;
289static int mxser_set_baud_method[MXSER_PORTS + 1];
290
291static void mxser_enable_must_enchance_mode(unsigned long baseio)
292{
293 u8 oldlcr;
294 u8 efr;
295
296 oldlcr = inb(baseio + UART_LCR);
297 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
298
299 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
300 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
301
302 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
303 outb(oldlcr, baseio + UART_LCR);
304}
305
306#ifdef CONFIG_PCI
307static void mxser_disable_must_enchance_mode(unsigned long baseio)
308{
309 u8 oldlcr;
310 u8 efr;
311
312 oldlcr = inb(baseio + UART_LCR);
313 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
314
315 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
316 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
317
318 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
319 outb(oldlcr, baseio + UART_LCR);
320}
321#endif
322
323static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
324{
325 u8 oldlcr;
326 u8 efr;
327
328 oldlcr = inb(baseio + UART_LCR);
329 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
330
331 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
332 efr &= ~MOXA_MUST_EFR_BANK_MASK;
333 efr |= MOXA_MUST_EFR_BANK0;
334
335 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
336 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
337 outb(oldlcr, baseio + UART_LCR);
338}
339
340static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
341{
342 u8 oldlcr;
343 u8 efr;
344
345 oldlcr = inb(baseio + UART_LCR);
346 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
347
348 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
349 efr &= ~MOXA_MUST_EFR_BANK_MASK;
350 efr |= MOXA_MUST_EFR_BANK0;
351
352 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
353 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
354 outb(oldlcr, baseio + UART_LCR);
355}
356
357static void mxser_set_must_fifo_value(struct mxser_port *info)
358{
359 u8 oldlcr;
360 u8 efr;
361
362 oldlcr = inb(info->ioaddr + UART_LCR);
363 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
364
365 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
366 efr &= ~MOXA_MUST_EFR_BANK_MASK;
367 efr |= MOXA_MUST_EFR_BANK1;
368
369 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
370 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
371 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
372 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
373 outb(oldlcr, info->ioaddr + UART_LCR);
374}
375
376static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
377{
378 u8 oldlcr;
379 u8 efr;
380
381 oldlcr = inb(baseio + UART_LCR);
382 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
383
384 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
385 efr &= ~MOXA_MUST_EFR_BANK_MASK;
386 efr |= MOXA_MUST_EFR_BANK2;
387
388 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
389 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
390 outb(oldlcr, baseio + UART_LCR);
391}
392
393#ifdef CONFIG_PCI
394static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
395{
396 u8 oldlcr;
397 u8 efr;
398
399 oldlcr = inb(baseio + UART_LCR);
400 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
401
402 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
403 efr &= ~MOXA_MUST_EFR_BANK_MASK;
404 efr |= MOXA_MUST_EFR_BANK2;
405
406 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
407 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
408 outb(oldlcr, baseio + UART_LCR);
409}
410#endif
411
412static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
413{
414 u8 oldlcr;
415 u8 efr;
416
417 oldlcr = inb(baseio + UART_LCR);
418 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
419
420 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
421 efr &= ~MOXA_MUST_EFR_SF_MASK;
422
423 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
424 outb(oldlcr, baseio + UART_LCR);
425}
426
427static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
428{
429 u8 oldlcr;
430 u8 efr;
431
432 oldlcr = inb(baseio + UART_LCR);
433 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
434
435 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
436 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
437 efr |= MOXA_MUST_EFR_SF_TX1;
438
439 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
440 outb(oldlcr, baseio + UART_LCR);
441}
442
443static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
444{
445 u8 oldlcr;
446 u8 efr;
447
448 oldlcr = inb(baseio + UART_LCR);
449 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
450
451 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
452 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
453
454 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
455 outb(oldlcr, baseio + UART_LCR);
456}
457
458static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
459{
460 u8 oldlcr;
461 u8 efr;
462
463 oldlcr = inb(baseio + UART_LCR);
464 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
465
466 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
467 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
468 efr |= MOXA_MUST_EFR_SF_RX1;
469
470 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
471 outb(oldlcr, baseio + UART_LCR);
472}
473
474static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
475{
476 u8 oldlcr;
477 u8 efr;
478
479 oldlcr = inb(baseio + UART_LCR);
480 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
481
482 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
483 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
484
485 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
486 outb(oldlcr, baseio + UART_LCR);
487}
488
489#ifdef CONFIG_PCI
490static int __devinit CheckIsMoxaMust(unsigned long io)
491{
492 u8 oldmcr, hwid;
493 int i;
494
495 outb(0, io + UART_LCR);
496 mxser_disable_must_enchance_mode(io);
497 oldmcr = inb(io + UART_MCR);
498 outb(0, io + UART_MCR);
499 mxser_set_must_xon1_value(io, 0x11);
500 if ((hwid = inb(io + UART_MCR)) != 0) {
501 outb(oldmcr, io + UART_MCR);
502 return MOXA_OTHER_UART;
503 }
504
505 mxser_get_must_hardware_id(io, &hwid);
506 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
507 if (hwid == Gpci_uart_info[i].type)
508 return (int)hwid;
509 }
510 return MOXA_OTHER_UART;
511}
512#endif
513
514static void process_txrx_fifo(struct mxser_port *info)
515{
516 int i;
517
518 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
519 info->rx_trigger = 1;
520 info->rx_high_water = 1;
521 info->rx_low_water = 1;
522 info->xmit_fifo_size = 1;
523 } else
524 for (i = 0; i < UART_INFO_NUM; i++)
525 if (info->board->chip_flag == Gpci_uart_info[i].type) {
526 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
527 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
528 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
529 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
530 break;
531 }
532}
533
534static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
535{
536 static unsigned char mxser_msr[MXSER_PORTS + 1];
537 unsigned char status = 0;
538
539 status = inb(baseaddr + UART_MSR);
540
541 mxser_msr[port] &= 0x0F;
542 mxser_msr[port] |= status;
543 status = mxser_msr[port];
544 if (mode)
545 mxser_msr[port] = 0;
546
547 return status;
548}
549
550static int mxser_carrier_raised(struct tty_port *port)
551{
552 struct mxser_port *mp = container_of(port, struct mxser_port, port);
553 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
554}
555
556static void mxser_dtr_rts(struct tty_port *port, int on)
557{
558 struct mxser_port *mp = container_of(port, struct mxser_port, port);
559 unsigned long flags;
560
561 spin_lock_irqsave(&mp->slock, flags);
562 if (on)
563 outb(inb(mp->ioaddr + UART_MCR) |
564 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
565 else
566 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
567 mp->ioaddr + UART_MCR);
568 spin_unlock_irqrestore(&mp->slock, flags);
569}
570
571static int mxser_set_baud(struct tty_struct *tty, long newspd)
572{
573 struct mxser_port *info = tty->driver_data;
574 int quot = 0, baud;
575 unsigned char cval;
576
577 if (!info->ioaddr)
578 return -1;
579
580 if (newspd > info->max_baud)
581 return -1;
582
583 if (newspd == 134) {
584 quot = 2 * info->baud_base / 269;
585 tty_encode_baud_rate(tty, 134, 134);
586 } else if (newspd) {
587 quot = info->baud_base / newspd;
588 if (quot == 0)
589 quot = 1;
590 baud = info->baud_base/quot;
591 tty_encode_baud_rate(tty, baud, baud);
592 } else {
593 quot = 0;
594 }
595
596 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
597 info->timeout += HZ / 50; /* Add .02 seconds of slop */
598
599 if (quot) {
600 info->MCR |= UART_MCR_DTR;
601 outb(info->MCR, info->ioaddr + UART_MCR);
602 } else {
603 info->MCR &= ~UART_MCR_DTR;
604 outb(info->MCR, info->ioaddr + UART_MCR);
605 return 0;
606 }
607
608 cval = inb(info->ioaddr + UART_LCR);
609
610 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
611
612 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
613 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
614 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
615
616#ifdef BOTHER
617 if (C_BAUD(tty) == BOTHER) {
618 quot = info->baud_base % newspd;
619 quot *= 8;
620 if (quot % newspd > newspd / 2) {
621 quot /= newspd;
622 quot++;
623 } else
624 quot /= newspd;
625
626 mxser_set_must_enum_value(info->ioaddr, quot);
627 } else
628#endif
629 mxser_set_must_enum_value(info->ioaddr, 0);
630
631 return 0;
632}
633
634/*
635 * This routine is called to set the UART divisor registers to match
636 * the specified baud rate for a serial port.
637 */
638static int mxser_change_speed(struct tty_struct *tty,
639 struct ktermios *old_termios)
640{
641 struct mxser_port *info = tty->driver_data;
642 unsigned cflag, cval, fcr;
643 int ret = 0;
644 unsigned char status;
645
646 cflag = tty->termios->c_cflag;
647 if (!info->ioaddr)
648 return ret;
649
650 if (mxser_set_baud_method[tty->index] == 0)
651 mxser_set_baud(tty, tty_get_baud_rate(tty));
652
653 /* byte size and parity */
654 switch (cflag & CSIZE) {
655 case CS5:
656 cval = 0x00;
657 break;
658 case CS6:
659 cval = 0x01;
660 break;
661 case CS7:
662 cval = 0x02;
663 break;
664 case CS8:
665 cval = 0x03;
666 break;
667 default:
668 cval = 0x00;
669 break; /* too keep GCC shut... */
670 }
671 if (cflag & CSTOPB)
672 cval |= 0x04;
673 if (cflag & PARENB)
674 cval |= UART_LCR_PARITY;
675 if (!(cflag & PARODD))
676 cval |= UART_LCR_EPAR;
677 if (cflag & CMSPAR)
678 cval |= UART_LCR_SPAR;
679
680 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
681 if (info->board->chip_flag) {
682 fcr = UART_FCR_ENABLE_FIFO;
683 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
684 mxser_set_must_fifo_value(info);
685 } else
686 fcr = 0;
687 } else {
688 fcr = UART_FCR_ENABLE_FIFO;
689 if (info->board->chip_flag) {
690 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
691 mxser_set_must_fifo_value(info);
692 } else {
693 switch (info->rx_trigger) {
694 case 1:
695 fcr |= UART_FCR_TRIGGER_1;
696 break;
697 case 4:
698 fcr |= UART_FCR_TRIGGER_4;
699 break;
700 case 8:
701 fcr |= UART_FCR_TRIGGER_8;
702 break;
703 default:
704 fcr |= UART_FCR_TRIGGER_14;
705 break;
706 }
707 }
708 }
709
710 /* CTS flow control flag and modem status interrupts */
711 info->IER &= ~UART_IER_MSI;
712 info->MCR &= ~UART_MCR_AFE;
713 if (cflag & CRTSCTS) {
714 info->port.flags |= ASYNC_CTS_FLOW;
715 info->IER |= UART_IER_MSI;
716 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
717 info->MCR |= UART_MCR_AFE;
718 } else {
719 status = inb(info->ioaddr + UART_MSR);
720 if (tty->hw_stopped) {
721 if (status & UART_MSR_CTS) {
722 tty->hw_stopped = 0;
723 if (info->type != PORT_16550A &&
724 !info->board->chip_flag) {
725 outb(info->IER & ~UART_IER_THRI,
726 info->ioaddr +
727 UART_IER);
728 info->IER |= UART_IER_THRI;
729 outb(info->IER, info->ioaddr +
730 UART_IER);
731 }
732 tty_wakeup(tty);
733 }
734 } else {
735 if (!(status & UART_MSR_CTS)) {
736 tty->hw_stopped = 1;
737 if ((info->type != PORT_16550A) &&
738 (!info->board->chip_flag)) {
739 info->IER &= ~UART_IER_THRI;
740 outb(info->IER, info->ioaddr +
741 UART_IER);
742 }
743 }
744 }
745 }
746 } else {
747 info->port.flags &= ~ASYNC_CTS_FLOW;
748 }
749 outb(info->MCR, info->ioaddr + UART_MCR);
750 if (cflag & CLOCAL) {
751 info->port.flags &= ~ASYNC_CHECK_CD;
752 } else {
753 info->port.flags |= ASYNC_CHECK_CD;
754 info->IER |= UART_IER_MSI;
755 }
756 outb(info->IER, info->ioaddr + UART_IER);
757
758 /*
759 * Set up parity check flag
760 */
761 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
762 if (I_INPCK(tty))
763 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
764 if (I_BRKINT(tty) || I_PARMRK(tty))
765 info->read_status_mask |= UART_LSR_BI;
766
767 info->ignore_status_mask = 0;
768
769 if (I_IGNBRK(tty)) {
770 info->ignore_status_mask |= UART_LSR_BI;
771 info->read_status_mask |= UART_LSR_BI;
772 /*
773 * If we're ignore parity and break indicators, ignore
774 * overruns too. (For real raw support).
775 */
776 if (I_IGNPAR(tty)) {
777 info->ignore_status_mask |=
778 UART_LSR_OE |
779 UART_LSR_PE |
780 UART_LSR_FE;
781 info->read_status_mask |=
782 UART_LSR_OE |
783 UART_LSR_PE |
784 UART_LSR_FE;
785 }
786 }
787 if (info->board->chip_flag) {
788 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
789 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
790 if (I_IXON(tty)) {
791 mxser_enable_must_rx_software_flow_control(
792 info->ioaddr);
793 } else {
794 mxser_disable_must_rx_software_flow_control(
795 info->ioaddr);
796 }
797 if (I_IXOFF(tty)) {
798 mxser_enable_must_tx_software_flow_control(
799 info->ioaddr);
800 } else {
801 mxser_disable_must_tx_software_flow_control(
802 info->ioaddr);
803 }
804 }
805
806
807 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
808 outb(cval, info->ioaddr + UART_LCR);
809
810 return ret;
811}
812
813static void mxser_check_modem_status(struct tty_struct *tty,
814 struct mxser_port *port, int status)
815{
816 /* update input line counters */
817 if (status & UART_MSR_TERI)
818 port->icount.rng++;
819 if (status & UART_MSR_DDSR)
820 port->icount.dsr++;
821 if (status & UART_MSR_DDCD)
822 port->icount.dcd++;
823 if (status & UART_MSR_DCTS)
824 port->icount.cts++;
825 port->mon_data.modem_status = status;
826 wake_up_interruptible(&port->port.delta_msr_wait);
827
828 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
829 if (status & UART_MSR_DCD)
830 wake_up_interruptible(&port->port.open_wait);
831 }
832
833 if (port->port.flags & ASYNC_CTS_FLOW) {
834 if (tty->hw_stopped) {
835 if (status & UART_MSR_CTS) {
836 tty->hw_stopped = 0;
837
838 if ((port->type != PORT_16550A) &&
839 (!port->board->chip_flag)) {
840 outb(port->IER & ~UART_IER_THRI,
841 port->ioaddr + UART_IER);
842 port->IER |= UART_IER_THRI;
843 outb(port->IER, port->ioaddr +
844 UART_IER);
845 }
846 tty_wakeup(tty);
847 }
848 } else {
849 if (!(status & UART_MSR_CTS)) {
850 tty->hw_stopped = 1;
851 if (port->type != PORT_16550A &&
852 !port->board->chip_flag) {
853 port->IER &= ~UART_IER_THRI;
854 outb(port->IER, port->ioaddr +
855 UART_IER);
856 }
857 }
858 }
859 }
860}
861
862static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
863{
864 struct mxser_port *info = container_of(port, struct mxser_port, port);
865 unsigned long page;
866 unsigned long flags;
867
868 page = __get_free_page(GFP_KERNEL);
869 if (!page)
870 return -ENOMEM;
871
872 spin_lock_irqsave(&info->slock, flags);
873
874 if (!info->ioaddr || !info->type) {
875 set_bit(TTY_IO_ERROR, &tty->flags);
876 free_page(page);
877 spin_unlock_irqrestore(&info->slock, flags);
878 return 0;
879 }
880 info->port.xmit_buf = (unsigned char *) page;
881
882 /*
883 * Clear the FIFO buffers and disable them
884 * (they will be reenabled in mxser_change_speed())
885 */
886 if (info->board->chip_flag)
887 outb((UART_FCR_CLEAR_RCVR |
888 UART_FCR_CLEAR_XMIT |
889 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
890 else
891 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
892 info->ioaddr + UART_FCR);
893
894 /*
895 * At this point there's no way the LSR could still be 0xFF;
896 * if it is, then bail out, because there's likely no UART
897 * here.
898 */
899 if (inb(info->ioaddr + UART_LSR) == 0xff) {
900 spin_unlock_irqrestore(&info->slock, flags);
901 if (capable(CAP_SYS_ADMIN)) {
902 set_bit(TTY_IO_ERROR, &tty->flags);
903 return 0;
904 } else
905 return -ENODEV;
906 }
907
908 /*
909 * Clear the interrupt registers.
910 */
911 (void) inb(info->ioaddr + UART_LSR);
912 (void) inb(info->ioaddr + UART_RX);
913 (void) inb(info->ioaddr + UART_IIR);
914 (void) inb(info->ioaddr + UART_MSR);
915
916 /*
917 * Now, initialize the UART
918 */
919 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
920 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
921 outb(info->MCR, info->ioaddr + UART_MCR);
922
923 /*
924 * Finally, enable interrupts
925 */
926 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
927
928 if (info->board->chip_flag)
929 info->IER |= MOXA_MUST_IER_EGDAI;
930 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
931
932 /*
933 * And clear the interrupt registers again for luck.
934 */
935 (void) inb(info->ioaddr + UART_LSR);
936 (void) inb(info->ioaddr + UART_RX);
937 (void) inb(info->ioaddr + UART_IIR);
938 (void) inb(info->ioaddr + UART_MSR);
939
940 clear_bit(TTY_IO_ERROR, &tty->flags);
941 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
942
943 /*
944 * and set the speed of the serial port
945 */
946 mxser_change_speed(tty, NULL);
947 spin_unlock_irqrestore(&info->slock, flags);
948
949 return 0;
950}
951
952/*
953 * This routine will shutdown a serial port
954 */
955static void mxser_shutdown_port(struct tty_port *port)
956{
957 struct mxser_port *info = container_of(port, struct mxser_port, port);
958 unsigned long flags;
959
960 spin_lock_irqsave(&info->slock, flags);
961
962 /*
963 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
964 * here so the queue might never be waken up
965 */
966 wake_up_interruptible(&info->port.delta_msr_wait);
967
968 /*
969 * Free the xmit buffer, if necessary
970 */
971 if (info->port.xmit_buf) {
972 free_page((unsigned long) info->port.xmit_buf);
973 info->port.xmit_buf = NULL;
974 }
975
976 info->IER = 0;
977 outb(0x00, info->ioaddr + UART_IER);
978
979 /* clear Rx/Tx FIFO's */
980 if (info->board->chip_flag)
981 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
982 MOXA_MUST_FCR_GDA_MODE_ENABLE,
983 info->ioaddr + UART_FCR);
984 else
985 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
986 info->ioaddr + UART_FCR);
987
988 /* read data port to reset things */
989 (void) inb(info->ioaddr + UART_RX);
990
991
992 if (info->board->chip_flag)
993 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
994
995 spin_unlock_irqrestore(&info->slock, flags);
996}
997
998/*
999 * This routine is called whenever a serial port is opened. It
1000 * enables interrupts for a serial port, linking in its async structure into
1001 * the IRQ chain. It also performs the serial-specific
1002 * initialization for the tty structure.
1003 */
1004static int mxser_open(struct tty_struct *tty, struct file *filp)
1005{
1006 struct mxser_port *info;
1007 int line;
1008
1009 line = tty->index;
1010 if (line == MXSER_PORTS)
1011 return 0;
1012 if (line < 0 || line > MXSER_PORTS)
1013 return -ENODEV;
1014 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1015 if (!info->ioaddr)
1016 return -ENODEV;
1017
1018 tty->driver_data = info;
1019 return tty_port_open(&info->port, tty, filp);
1020}
1021
1022static void mxser_flush_buffer(struct tty_struct *tty)
1023{
1024 struct mxser_port *info = tty->driver_data;
1025 char fcr;
1026 unsigned long flags;
1027
1028
1029 spin_lock_irqsave(&info->slock, flags);
1030 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1031
1032 fcr = inb(info->ioaddr + UART_FCR);
1033 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1034 info->ioaddr + UART_FCR);
1035 outb(fcr, info->ioaddr + UART_FCR);
1036
1037 spin_unlock_irqrestore(&info->slock, flags);
1038
1039 tty_wakeup(tty);
1040}
1041
1042
1043static void mxser_close_port(struct tty_port *port)
1044{
1045 struct mxser_port *info = container_of(port, struct mxser_port, port);
1046 unsigned long timeout;
1047 /*
1048 * At this point we stop accepting input. To do this, we
1049 * disable the receive line status interrupts, and tell the
1050 * interrupt driver to stop checking the data ready bit in the
1051 * line status register.
1052 */
1053 info->IER &= ~UART_IER_RLSI;
1054 if (info->board->chip_flag)
1055 info->IER &= ~MOXA_MUST_RECV_ISR;
1056
1057 outb(info->IER, info->ioaddr + UART_IER);
1058 /*
1059 * Before we drop DTR, make sure the UART transmitter
1060 * has completely drained; this is especially
1061 * important if there is a transmit FIFO!
1062 */
1063 timeout = jiffies + HZ;
1064 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1065 schedule_timeout_interruptible(5);
1066 if (time_after(jiffies, timeout))
1067 break;
1068 }
1069}
1070
1071/*
1072 * This routine is called when the serial port gets closed. First, we
1073 * wait for the last remaining data to be sent. Then, we unlink its
1074 * async structure from the interrupt chain if necessary, and we free
1075 * that IRQ if nothing is left in the chain.
1076 */
1077static void mxser_close(struct tty_struct *tty, struct file *filp)
1078{
1079 struct mxser_port *info = tty->driver_data;
1080 struct tty_port *port = &info->port;
1081
1082 if (tty->index == MXSER_PORTS || info == NULL)
1083 return;
1084 if (tty_port_close_start(port, tty, filp) == 0)
1085 return;
1086 mutex_lock(&port->mutex);
1087 mxser_close_port(port);
1088 mxser_flush_buffer(tty);
1089 mxser_shutdown_port(port);
1090 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1091 mutex_unlock(&port->mutex);
1092 /* Right now the tty_port set is done outside of the close_end helper
1093 as we don't yet have everyone using refcounts */
1094 tty_port_close_end(port, tty);
1095 tty_port_tty_set(port, NULL);
1096}
1097
1098static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1099{
1100 int c, total = 0;
1101 struct mxser_port *info = tty->driver_data;
1102 unsigned long flags;
1103
1104 if (!info->port.xmit_buf)
1105 return 0;
1106
1107 while (1) {
1108 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1109 SERIAL_XMIT_SIZE - info->xmit_head));
1110 if (c <= 0)
1111 break;
1112
1113 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1114 spin_lock_irqsave(&info->slock, flags);
1115 info->xmit_head = (info->xmit_head + c) &
1116 (SERIAL_XMIT_SIZE - 1);
1117 info->xmit_cnt += c;
1118 spin_unlock_irqrestore(&info->slock, flags);
1119
1120 buf += c;
1121 count -= c;
1122 total += c;
1123 }
1124
1125 if (info->xmit_cnt && !tty->stopped) {
1126 if (!tty->hw_stopped ||
1127 (info->type == PORT_16550A) ||
1128 (info->board->chip_flag)) {
1129 spin_lock_irqsave(&info->slock, flags);
1130 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1131 UART_IER);
1132 info->IER |= UART_IER_THRI;
1133 outb(info->IER, info->ioaddr + UART_IER);
1134 spin_unlock_irqrestore(&info->slock, flags);
1135 }
1136 }
1137 return total;
1138}
1139
1140static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1141{
1142 struct mxser_port *info = tty->driver_data;
1143 unsigned long flags;
1144
1145 if (!info->port.xmit_buf)
1146 return 0;
1147
1148 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1149 return 0;
1150
1151 spin_lock_irqsave(&info->slock, flags);
1152 info->port.xmit_buf[info->xmit_head++] = ch;
1153 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1154 info->xmit_cnt++;
1155 spin_unlock_irqrestore(&info->slock, flags);
1156 if (!tty->stopped) {
1157 if (!tty->hw_stopped ||
1158 (info->type == PORT_16550A) ||
1159 info->board->chip_flag) {
1160 spin_lock_irqsave(&info->slock, flags);
1161 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1162 info->IER |= UART_IER_THRI;
1163 outb(info->IER, info->ioaddr + UART_IER);
1164 spin_unlock_irqrestore(&info->slock, flags);
1165 }
1166 }
1167 return 1;
1168}
1169
1170
1171static void mxser_flush_chars(struct tty_struct *tty)
1172{
1173 struct mxser_port *info = tty->driver_data;
1174 unsigned long flags;
1175
1176 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1177 (tty->hw_stopped && info->type != PORT_16550A &&
1178 !info->board->chip_flag))
1179 return;
1180
1181 spin_lock_irqsave(&info->slock, flags);
1182
1183 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1184 info->IER |= UART_IER_THRI;
1185 outb(info->IER, info->ioaddr + UART_IER);
1186
1187 spin_unlock_irqrestore(&info->slock, flags);
1188}
1189
1190static int mxser_write_room(struct tty_struct *tty)
1191{
1192 struct mxser_port *info = tty->driver_data;
1193 int ret;
1194
1195 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1196 return ret < 0 ? 0 : ret;
1197}
1198
1199static int mxser_chars_in_buffer(struct tty_struct *tty)
1200{
1201 struct mxser_port *info = tty->driver_data;
1202 return info->xmit_cnt;
1203}
1204
1205/*
1206 * ------------------------------------------------------------
1207 * friends of mxser_ioctl()
1208 * ------------------------------------------------------------
1209 */
1210static int mxser_get_serial_info(struct tty_struct *tty,
1211 struct serial_struct __user *retinfo)
1212{
1213 struct mxser_port *info = tty->driver_data;
1214 struct serial_struct tmp = {
1215 .type = info->type,
1216 .line = tty->index,
1217 .port = info->ioaddr,
1218 .irq = info->board->irq,
1219 .flags = info->port.flags,
1220 .baud_base = info->baud_base,
1221 .close_delay = info->port.close_delay,
1222 .closing_wait = info->port.closing_wait,
1223 .custom_divisor = info->custom_divisor,
1224 .hub6 = 0
1225 };
1226 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1227 return -EFAULT;
1228 return 0;
1229}
1230
1231static int mxser_set_serial_info(struct tty_struct *tty,
1232 struct serial_struct __user *new_info)
1233{
1234 struct mxser_port *info = tty->driver_data;
1235 struct tty_port *port = &info->port;
1236 struct serial_struct new_serial;
1237 speed_t baud;
1238 unsigned long sl_flags;
1239 unsigned int flags;
1240 int retval = 0;
1241
1242 if (!new_info || !info->ioaddr)
1243 return -ENODEV;
1244 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1245 return -EFAULT;
1246
1247 if (new_serial.irq != info->board->irq ||
1248 new_serial.port != info->ioaddr)
1249 return -EINVAL;
1250
1251 flags = port->flags & ASYNC_SPD_MASK;
1252
1253 if (!capable(CAP_SYS_ADMIN)) {
1254 if ((new_serial.baud_base != info->baud_base) ||
1255 (new_serial.close_delay != info->port.close_delay) ||
1256 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
1257 return -EPERM;
1258 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1259 (new_serial.flags & ASYNC_USR_MASK));
1260 } else {
1261 /*
1262 * OK, past this point, all the error checking has been done.
1263 * At this point, we start making changes.....
1264 */
1265 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1266 (new_serial.flags & ASYNC_FLAGS));
1267 port->close_delay = new_serial.close_delay * HZ / 100;
1268 port->closing_wait = new_serial.closing_wait * HZ / 100;
1269 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1270 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1271 (new_serial.baud_base != info->baud_base ||
1272 new_serial.custom_divisor !=
1273 info->custom_divisor)) {
1274 if (new_serial.custom_divisor == 0)
1275 return -EINVAL;
1276 baud = new_serial.baud_base / new_serial.custom_divisor;
1277 tty_encode_baud_rate(tty, baud, baud);
1278 }
1279 }
1280
1281 info->type = new_serial.type;
1282
1283 process_txrx_fifo(info);
1284
1285 if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
1286 if (flags != (port->flags & ASYNC_SPD_MASK)) {
1287 spin_lock_irqsave(&info->slock, sl_flags);
1288 mxser_change_speed(tty, NULL);
1289 spin_unlock_irqrestore(&info->slock, sl_flags);
1290 }
1291 } else {
1292 retval = mxser_activate(port, tty);
1293 if (retval == 0)
1294 set_bit(ASYNCB_INITIALIZED, &port->flags);
1295 }
1296 return retval;
1297}
1298
1299/*
1300 * mxser_get_lsr_info - get line status register info
1301 *
1302 * Purpose: Let user call ioctl() to get info when the UART physically
1303 * is emptied. On bus types like RS485, the transmitter must
1304 * release the bus after transmitting. This must be done when
1305 * the transmit shift register is empty, not be done when the
1306 * transmit holding register is empty. This functionality
1307 * allows an RS485 driver to be written in user space.
1308 */
1309static int mxser_get_lsr_info(struct mxser_port *info,
1310 unsigned int __user *value)
1311{
1312 unsigned char status;
1313 unsigned int result;
1314 unsigned long flags;
1315
1316 spin_lock_irqsave(&info->slock, flags);
1317 status = inb(info->ioaddr + UART_LSR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1319 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1320 return put_user(result, value);
1321}
1322
1323static int mxser_tiocmget(struct tty_struct *tty)
1324{
1325 struct mxser_port *info = tty->driver_data;
1326 unsigned char control, status;
1327 unsigned long flags;
1328
1329
1330 if (tty->index == MXSER_PORTS)
1331 return -ENOIOCTLCMD;
1332 if (test_bit(TTY_IO_ERROR, &tty->flags))
1333 return -EIO;
1334
1335 control = info->MCR;
1336
1337 spin_lock_irqsave(&info->slock, flags);
1338 status = inb(info->ioaddr + UART_MSR);
1339 if (status & UART_MSR_ANY_DELTA)
1340 mxser_check_modem_status(tty, info, status);
1341 spin_unlock_irqrestore(&info->slock, flags);
1342 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1343 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1344 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1345 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1346 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1347 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1348}
1349
1350static int mxser_tiocmset(struct tty_struct *tty,
1351 unsigned int set, unsigned int clear)
1352{
1353 struct mxser_port *info = tty->driver_data;
1354 unsigned long flags;
1355
1356
1357 if (tty->index == MXSER_PORTS)
1358 return -ENOIOCTLCMD;
1359 if (test_bit(TTY_IO_ERROR, &tty->flags))
1360 return -EIO;
1361
1362 spin_lock_irqsave(&info->slock, flags);
1363
1364 if (set & TIOCM_RTS)
1365 info->MCR |= UART_MCR_RTS;
1366 if (set & TIOCM_DTR)
1367 info->MCR |= UART_MCR_DTR;
1368
1369 if (clear & TIOCM_RTS)
1370 info->MCR &= ~UART_MCR_RTS;
1371 if (clear & TIOCM_DTR)
1372 info->MCR &= ~UART_MCR_DTR;
1373
1374 outb(info->MCR, info->ioaddr + UART_MCR);
1375 spin_unlock_irqrestore(&info->slock, flags);
1376 return 0;
1377}
1378
1379static int __init mxser_program_mode(int port)
1380{
1381 int id, i, j, n;
1382
1383 outb(0, port);
1384 outb(0, port);
1385 outb(0, port);
1386 (void)inb(port);
1387 (void)inb(port);
1388 outb(0, port);
1389 (void)inb(port);
1390
1391 id = inb(port + 1) & 0x1F;
1392 if ((id != C168_ASIC_ID) &&
1393 (id != C104_ASIC_ID) &&
1394 (id != C102_ASIC_ID) &&
1395 (id != CI132_ASIC_ID) &&
1396 (id != CI134_ASIC_ID) &&
1397 (id != CI104J_ASIC_ID))
1398 return -1;
1399 for (i = 0, j = 0; i < 4; i++) {
1400 n = inb(port + 2);
1401 if (n == 'M') {
1402 j = 1;
1403 } else if ((j == 1) && (n == 1)) {
1404 j = 2;
1405 break;
1406 } else
1407 j = 0;
1408 }
1409 if (j != 2)
1410 id = -2;
1411 return id;
1412}
1413
1414static void __init mxser_normal_mode(int port)
1415{
1416 int i, n;
1417
1418 outb(0xA5, port + 1);
1419 outb(0x80, port + 3);
1420 outb(12, port + 0); /* 9600 bps */
1421 outb(0, port + 1);
1422 outb(0x03, port + 3); /* 8 data bits */
1423 outb(0x13, port + 4); /* loop back mode */
1424 for (i = 0; i < 16; i++) {
1425 n = inb(port + 5);
1426 if ((n & 0x61) == 0x60)
1427 break;
1428 if ((n & 1) == 1)
1429 (void)inb(port);
1430 }
1431 outb(0x00, port + 4);
1432}
1433
1434#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1435#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1436#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1437#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1438#define EN_CCMD 0x000 /* Chip's command register */
1439#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1440#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1441#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1442#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1443#define EN0_DCFG 0x00E /* Data configuration reg WR */
1444#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1445#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1446#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1447static int __init mxser_read_register(int port, unsigned short *regs)
1448{
1449 int i, k, value, id;
1450 unsigned int j;
1451
1452 id = mxser_program_mode(port);
1453 if (id < 0)
1454 return id;
1455 for (i = 0; i < 14; i++) {
1456 k = (i & 0x3F) | 0x180;
1457 for (j = 0x100; j > 0; j >>= 1) {
1458 outb(CHIP_CS, port);
1459 if (k & j) {
1460 outb(CHIP_CS | CHIP_DO, port);
1461 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1462 } else {
1463 outb(CHIP_CS, port);
1464 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1465 }
1466 }
1467 (void)inb(port);
1468 value = 0;
1469 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1470 outb(CHIP_CS, port);
1471 outb(CHIP_CS | CHIP_SK, port);
1472 if (inb(port) & CHIP_DI)
1473 value |= j;
1474 }
1475 regs[i] = value;
1476 outb(0, port);
1477 }
1478 mxser_normal_mode(port);
1479 return id;
1480}
1481
1482static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1483{
1484 struct mxser_port *ip;
1485 struct tty_port *port;
1486 struct tty_struct *tty;
1487 int result, status;
1488 unsigned int i, j;
1489 int ret = 0;
1490
1491 switch (cmd) {
1492 case MOXA_GET_MAJOR:
1493 if (printk_ratelimit())
1494 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1495 "%x (GET_MAJOR), fix your userspace\n",
1496 current->comm, cmd);
1497 return put_user(ttymajor, (int __user *)argp);
1498
1499 case MOXA_CHKPORTENABLE:
1500 result = 0;
1501 for (i = 0; i < MXSER_BOARDS; i++)
1502 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1503 if (mxser_boards[i].ports[j].ioaddr)
1504 result |= (1 << i);
1505 return put_user(result, (unsigned long __user *)argp);
1506 case MOXA_GETDATACOUNT:
1507 /* The receive side is locked by port->slock but it isn't
1508 clear that an exact snapshot is worth copying here */
1509 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1510 ret = -EFAULT;
1511 return ret;
1512 case MOXA_GETMSTATUS: {
1513 struct mxser_mstatus ms, __user *msu = argp;
1514 for (i = 0; i < MXSER_BOARDS; i++)
1515 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1516 ip = &mxser_boards[i].ports[j];
1517 port = &ip->port;
1518 memset(&ms, 0, sizeof(ms));
1519
1520 mutex_lock(&port->mutex);
1521 if (!ip->ioaddr)
1522 goto copy;
1523
1524 tty = tty_port_tty_get(port);
1525
1526 if (!tty || !tty->termios)
1527 ms.cflag = ip->normal_termios.c_cflag;
1528 else
1529 ms.cflag = tty->termios->c_cflag;
1530 tty_kref_put(tty);
1531 spin_lock_irq(&ip->slock);
1532 status = inb(ip->ioaddr + UART_MSR);
1533 spin_unlock_irq(&ip->slock);
1534 if (status & UART_MSR_DCD)
1535 ms.dcd = 1;
1536 if (status & UART_MSR_DSR)
1537 ms.dsr = 1;
1538 if (status & UART_MSR_CTS)
1539 ms.cts = 1;
1540 copy:
1541 mutex_unlock(&port->mutex);
1542 if (copy_to_user(msu, &ms, sizeof(ms)))
1543 return -EFAULT;
1544 msu++;
1545 }
1546 return 0;
1547 }
1548 case MOXA_ASPP_MON_EXT: {
1549 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1550 unsigned int cflag, iflag, p;
1551 u8 opmode;
1552
1553 me = kzalloc(sizeof(*me), GFP_KERNEL);
1554 if (!me)
1555 return -ENOMEM;
1556
1557 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1558 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1559 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1560 i = MXSER_BOARDS;
1561 break;
1562 }
1563 ip = &mxser_boards[i].ports[j];
1564 port = &ip->port;
1565
1566 mutex_lock(&port->mutex);
1567 if (!ip->ioaddr) {
1568 mutex_unlock(&port->mutex);
1569 continue;
1570 }
1571
1572 spin_lock_irq(&ip->slock);
1573 status = mxser_get_msr(ip->ioaddr, 0, p);
1574
1575 if (status & UART_MSR_TERI)
1576 ip->icount.rng++;
1577 if (status & UART_MSR_DDSR)
1578 ip->icount.dsr++;
1579 if (status & UART_MSR_DDCD)
1580 ip->icount.dcd++;
1581 if (status & UART_MSR_DCTS)
1582 ip->icount.cts++;
1583
1584 ip->mon_data.modem_status = status;
1585 me->rx_cnt[p] = ip->mon_data.rxcnt;
1586 me->tx_cnt[p] = ip->mon_data.txcnt;
1587 me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
1588 me->up_txcnt[p] = ip->mon_data.up_txcnt;
1589 me->modem_status[p] =
1590 ip->mon_data.modem_status;
1591 spin_unlock_irq(&ip->slock);
1592
1593 tty = tty_port_tty_get(&ip->port);
1594
1595 if (!tty || !tty->termios) {
1596 cflag = ip->normal_termios.c_cflag;
1597 iflag = ip->normal_termios.c_iflag;
1598 me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
1599 } else {
1600 cflag = tty->termios->c_cflag;
1601 iflag = tty->termios->c_iflag;
1602 me->baudrate[p] = tty_get_baud_rate(tty);
1603 }
1604 tty_kref_put(tty);
1605
1606 me->databits[p] = cflag & CSIZE;
1607 me->stopbits[p] = cflag & CSTOPB;
1608 me->parity[p] = cflag & (PARENB | PARODD |
1609 CMSPAR);
1610
1611 if (cflag & CRTSCTS)
1612 me->flowctrl[p] |= 0x03;
1613
1614 if (iflag & (IXON | IXOFF))
1615 me->flowctrl[p] |= 0x0C;
1616
1617 if (ip->type == PORT_16550A)
1618 me->fifo[p] = 1;
1619
1620 opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
1621 opmode &= OP_MODE_MASK;
1622 me->iftype[p] = opmode;
1623 mutex_unlock(&port->mutex);
1624 }
1625 }
1626 if (copy_to_user(argp, me, sizeof(*me)))
1627 ret = -EFAULT;
1628 kfree(me);
1629 return ret;
1630 }
1631 default:
1632 return -ENOIOCTLCMD;
1633 }
1634 return 0;
1635}
1636
1637static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1638 struct async_icount *cprev)
1639{
1640 struct async_icount cnow;
1641 unsigned long flags;
1642 int ret;
1643
1644 spin_lock_irqsave(&info->slock, flags);
1645 cnow = info->icount; /* atomic copy */
1646 spin_unlock_irqrestore(&info->slock, flags);
1647
1648 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1649 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1650 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1651 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1652
1653 *cprev = cnow;
1654
1655 return ret;
1656}
1657
1658static int mxser_ioctl(struct tty_struct *tty,
1659 unsigned int cmd, unsigned long arg)
1660{
1661 struct mxser_port *info = tty->driver_data;
1662 struct tty_port *port = &info->port;
1663 struct async_icount cnow;
1664 unsigned long flags;
1665 void __user *argp = (void __user *)arg;
1666 int retval;
1667
1668 if (tty->index == MXSER_PORTS)
1669 return mxser_ioctl_special(cmd, argp);
1670
1671 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1672 int p;
1673 unsigned long opmode;
1674 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1675 int shiftbit;
1676 unsigned char val, mask;
1677
1678 p = tty->index % 4;
1679 if (cmd == MOXA_SET_OP_MODE) {
1680 if (get_user(opmode, (int __user *) argp))
1681 return -EFAULT;
1682 if (opmode != RS232_MODE &&
1683 opmode != RS485_2WIRE_MODE &&
1684 opmode != RS422_MODE &&
1685 opmode != RS485_4WIRE_MODE)
1686 return -EFAULT;
1687 mask = ModeMask[p];
1688 shiftbit = p * 2;
1689 spin_lock_irq(&info->slock);
1690 val = inb(info->opmode_ioaddr);
1691 val &= mask;
1692 val |= (opmode << shiftbit);
1693 outb(val, info->opmode_ioaddr);
1694 spin_unlock_irq(&info->slock);
1695 } else {
1696 shiftbit = p * 2;
1697 spin_lock_irq(&info->slock);
1698 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1699 spin_unlock_irq(&info->slock);
1700 opmode &= OP_MODE_MASK;
1701 if (put_user(opmode, (int __user *)argp))
1702 return -EFAULT;
1703 }
1704 return 0;
1705 }
1706
1707 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT &&
1708 test_bit(TTY_IO_ERROR, &tty->flags))
1709 return -EIO;
1710
1711 switch (cmd) {
1712 case TIOCGSERIAL:
1713 mutex_lock(&port->mutex);
1714 retval = mxser_get_serial_info(tty, argp);
1715 mutex_unlock(&port->mutex);
1716 return retval;
1717 case TIOCSSERIAL:
1718 mutex_lock(&port->mutex);
1719 retval = mxser_set_serial_info(tty, argp);
1720 mutex_unlock(&port->mutex);
1721 return retval;
1722 case TIOCSERGETLSR: /* Get line status register */
1723 return mxser_get_lsr_info(info, argp);
1724 /*
1725 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1726 * - mask passed in arg for lines of interest
1727 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1728 * Caller should use TIOCGICOUNT to see which one it was
1729 */
1730 case TIOCMIWAIT:
1731 spin_lock_irqsave(&info->slock, flags);
1732 cnow = info->icount; /* note the counters on entry */
1733 spin_unlock_irqrestore(&info->slock, flags);
1734
1735 return wait_event_interruptible(info->port.delta_msr_wait,
1736 mxser_cflags_changed(info, arg, &cnow));
1737 case MOXA_HighSpeedOn:
1738 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1739 case MOXA_SDS_RSTICOUNTER:
1740 spin_lock_irq(&info->slock);
1741 info->mon_data.rxcnt = 0;
1742 info->mon_data.txcnt = 0;
1743 spin_unlock_irq(&info->slock);
1744 return 0;
1745
1746 case MOXA_ASPP_OQUEUE:{
1747 int len, lsr;
1748
1749 len = mxser_chars_in_buffer(tty);
1750 spin_lock_irq(&info->slock);
1751 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
1752 spin_unlock_irq(&info->slock);
1753 len += (lsr ? 0 : 1);
1754
1755 return put_user(len, (int __user *)argp);
1756 }
1757 case MOXA_ASPP_MON: {
1758 int mcr, status;
1759
1760 spin_lock_irq(&info->slock);
1761 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1762 mxser_check_modem_status(tty, info, status);
1763
1764 mcr = inb(info->ioaddr + UART_MCR);
1765 spin_unlock_irq(&info->slock);
1766
1767 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1768 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1769 else
1770 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1771
1772 if (mcr & MOXA_MUST_MCR_TX_XON)
1773 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1774 else
1775 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1776
1777 if (tty->hw_stopped)
1778 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1779 else
1780 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1781
1782 if (copy_to_user(argp, &info->mon_data,
1783 sizeof(struct mxser_mon)))
1784 return -EFAULT;
1785
1786 return 0;
1787 }
1788 case MOXA_ASPP_LSTATUS: {
1789 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1790 return -EFAULT;
1791
1792 info->err_shadow = 0;
1793 return 0;
1794 }
1795 case MOXA_SET_BAUD_METHOD: {
1796 int method;
1797
1798 if (get_user(method, (int __user *)argp))
1799 return -EFAULT;
1800 mxser_set_baud_method[tty->index] = method;
1801 return put_user(method, (int __user *)argp);
1802 }
1803 default:
1804 return -ENOIOCTLCMD;
1805 }
1806 return 0;
1807}
1808
1809 /*
1810 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1811 * Return: write counters to the user passed counter struct
1812 * NB: both 1->0 and 0->1 transitions are counted except for
1813 * RI where only 0->1 is counted.
1814 */
1815
1816static int mxser_get_icount(struct tty_struct *tty,
1817 struct serial_icounter_struct *icount)
1818
1819{
1820 struct mxser_port *info = tty->driver_data;
1821 struct async_icount cnow;
1822 unsigned long flags;
1823
1824 spin_lock_irqsave(&info->slock, flags);
1825 cnow = info->icount;
1826 spin_unlock_irqrestore(&info->slock, flags);
1827
1828 icount->frame = cnow.frame;
1829 icount->brk = cnow.brk;
1830 icount->overrun = cnow.overrun;
1831 icount->buf_overrun = cnow.buf_overrun;
1832 icount->parity = cnow.parity;
1833 icount->rx = cnow.rx;
1834 icount->tx = cnow.tx;
1835 icount->cts = cnow.cts;
1836 icount->dsr = cnow.dsr;
1837 icount->rng = cnow.rng;
1838 icount->dcd = cnow.dcd;
1839 return 0;
1840}
1841
1842static void mxser_stoprx(struct tty_struct *tty)
1843{
1844 struct mxser_port *info = tty->driver_data;
1845
1846 info->ldisc_stop_rx = 1;
1847 if (I_IXOFF(tty)) {
1848 if (info->board->chip_flag) {
1849 info->IER &= ~MOXA_MUST_RECV_ISR;
1850 outb(info->IER, info->ioaddr + UART_IER);
1851 } else {
1852 info->x_char = STOP_CHAR(tty);
1853 outb(0, info->ioaddr + UART_IER);
1854 info->IER |= UART_IER_THRI;
1855 outb(info->IER, info->ioaddr + UART_IER);
1856 }
1857 }
1858
1859 if (tty->termios->c_cflag & CRTSCTS) {
1860 info->MCR &= ~UART_MCR_RTS;
1861 outb(info->MCR, info->ioaddr + UART_MCR);
1862 }
1863}
1864
1865/*
1866 * This routine is called by the upper-layer tty layer to signal that
1867 * incoming characters should be throttled.
1868 */
1869static void mxser_throttle(struct tty_struct *tty)
1870{
1871 mxser_stoprx(tty);
1872}
1873
1874static void mxser_unthrottle(struct tty_struct *tty)
1875{
1876 struct mxser_port *info = tty->driver_data;
1877
1878 /* startrx */
1879 info->ldisc_stop_rx = 0;
1880 if (I_IXOFF(tty)) {
1881 if (info->x_char)
1882 info->x_char = 0;
1883 else {
1884 if (info->board->chip_flag) {
1885 info->IER |= MOXA_MUST_RECV_ISR;
1886 outb(info->IER, info->ioaddr + UART_IER);
1887 } else {
1888 info->x_char = START_CHAR(tty);
1889 outb(0, info->ioaddr + UART_IER);
1890 info->IER |= UART_IER_THRI;
1891 outb(info->IER, info->ioaddr + UART_IER);
1892 }
1893 }
1894 }
1895
1896 if (tty->termios->c_cflag & CRTSCTS) {
1897 info->MCR |= UART_MCR_RTS;
1898 outb(info->MCR, info->ioaddr + UART_MCR);
1899 }
1900}
1901
1902/*
1903 * mxser_stop() and mxser_start()
1904 *
1905 * This routines are called before setting or resetting tty->stopped.
1906 * They enable or disable transmitter interrupts, as necessary.
1907 */
1908static void mxser_stop(struct tty_struct *tty)
1909{
1910 struct mxser_port *info = tty->driver_data;
1911 unsigned long flags;
1912
1913 spin_lock_irqsave(&info->slock, flags);
1914 if (info->IER & UART_IER_THRI) {
1915 info->IER &= ~UART_IER_THRI;
1916 outb(info->IER, info->ioaddr + UART_IER);
1917 }
1918 spin_unlock_irqrestore(&info->slock, flags);
1919}
1920
1921static void mxser_start(struct tty_struct *tty)
1922{
1923 struct mxser_port *info = tty->driver_data;
1924 unsigned long flags;
1925
1926 spin_lock_irqsave(&info->slock, flags);
1927 if (info->xmit_cnt && info->port.xmit_buf) {
1928 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1929 info->IER |= UART_IER_THRI;
1930 outb(info->IER, info->ioaddr + UART_IER);
1931 }
1932 spin_unlock_irqrestore(&info->slock, flags);
1933}
1934
1935static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1936{
1937 struct mxser_port *info = tty->driver_data;
1938 unsigned long flags;
1939
1940 spin_lock_irqsave(&info->slock, flags);
1941 mxser_change_speed(tty, old_termios);
1942 spin_unlock_irqrestore(&info->slock, flags);
1943
1944 if ((old_termios->c_cflag & CRTSCTS) &&
1945 !(tty->termios->c_cflag & CRTSCTS)) {
1946 tty->hw_stopped = 0;
1947 mxser_start(tty);
1948 }
1949
1950 /* Handle sw stopped */
1951 if ((old_termios->c_iflag & IXON) &&
1952 !(tty->termios->c_iflag & IXON)) {
1953 tty->stopped = 0;
1954
1955 if (info->board->chip_flag) {
1956 spin_lock_irqsave(&info->slock, flags);
1957 mxser_disable_must_rx_software_flow_control(
1958 info->ioaddr);
1959 spin_unlock_irqrestore(&info->slock, flags);
1960 }
1961
1962 mxser_start(tty);
1963 }
1964}
1965
1966/*
1967 * mxser_wait_until_sent() --- wait until the transmitter is empty
1968 */
1969static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1970{
1971 struct mxser_port *info = tty->driver_data;
1972 unsigned long orig_jiffies, char_time;
1973 unsigned long flags;
1974 int lsr;
1975
1976 if (info->type == PORT_UNKNOWN)
1977 return;
1978
1979 if (info->xmit_fifo_size == 0)
1980 return; /* Just in case.... */
1981
1982 orig_jiffies = jiffies;
1983 /*
1984 * Set the check interval to be 1/5 of the estimated time to
1985 * send a single character, and make it at least 1. The check
1986 * interval should also be less than the timeout.
1987 *
1988 * Note: we have to use pretty tight timings here to satisfy
1989 * the NIST-PCTS.
1990 */
1991 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1992 char_time = char_time / 5;
1993 if (char_time == 0)
1994 char_time = 1;
1995 if (timeout && timeout < char_time)
1996 char_time = timeout;
1997 /*
1998 * If the transmitter hasn't cleared in twice the approximate
1999 * amount of time to send the entire FIFO, it probably won't
2000 * ever clear. This assumes the UART isn't doing flow
2001 * control, which is currently the case. Hence, if it ever
2002 * takes longer than info->timeout, this is probably due to a
2003 * UART bug of some kind. So, we clamp the timeout parameter at
2004 * 2*info->timeout.
2005 */
2006 if (!timeout || timeout > 2 * info->timeout)
2007 timeout = 2 * info->timeout;
2008#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2009 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2010 timeout, char_time);
2011 printk("jiff=%lu...", jiffies);
2012#endif
2013 spin_lock_irqsave(&info->slock, flags);
2014 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2015#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2016 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2017#endif
2018 spin_unlock_irqrestore(&info->slock, flags);
2019 schedule_timeout_interruptible(char_time);
2020 spin_lock_irqsave(&info->slock, flags);
2021 if (signal_pending(current))
2022 break;
2023 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2024 break;
2025 }
2026 spin_unlock_irqrestore(&info->slock, flags);
2027 set_current_state(TASK_RUNNING);
2028
2029#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2030 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2031#endif
2032}
2033
2034/*
2035 * This routine is called by tty_hangup() when a hangup is signaled.
2036 */
2037static void mxser_hangup(struct tty_struct *tty)
2038{
2039 struct mxser_port *info = tty->driver_data;
2040
2041 mxser_flush_buffer(tty);
2042 tty_port_hangup(&info->port);
2043}
2044
2045/*
2046 * mxser_rs_break() --- routine which turns the break handling on or off
2047 */
2048static int mxser_rs_break(struct tty_struct *tty, int break_state)
2049{
2050 struct mxser_port *info = tty->driver_data;
2051 unsigned long flags;
2052
2053 spin_lock_irqsave(&info->slock, flags);
2054 if (break_state == -1)
2055 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2056 info->ioaddr + UART_LCR);
2057 else
2058 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2059 info->ioaddr + UART_LCR);
2060 spin_unlock_irqrestore(&info->slock, flags);
2061 return 0;
2062}
2063
2064static void mxser_receive_chars(struct tty_struct *tty,
2065 struct mxser_port *port, int *status)
2066{
2067 unsigned char ch, gdl;
2068 int ignored = 0;
2069 int cnt = 0;
2070 int recv_room;
2071 int max = 256;
2072
2073 recv_room = tty->receive_room;
2074 if (recv_room == 0 && !port->ldisc_stop_rx)
2075 mxser_stoprx(tty);
2076 if (port->board->chip_flag != MOXA_OTHER_UART) {
2077
2078 if (*status & UART_LSR_SPECIAL)
2079 goto intr_old;
2080 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2081 (*status & MOXA_MUST_LSR_RERR))
2082 goto intr_old;
2083 if (*status & MOXA_MUST_LSR_RERR)
2084 goto intr_old;
2085
2086 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2087
2088 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2089 gdl &= MOXA_MUST_GDL_MASK;
2090 if (gdl >= recv_room) {
2091 if (!port->ldisc_stop_rx)
2092 mxser_stoprx(tty);
2093 }
2094 while (gdl--) {
2095 ch = inb(port->ioaddr + UART_RX);
2096 tty_insert_flip_char(tty, ch, 0);
2097 cnt++;
2098 }
2099 goto end_intr;
2100 }
2101intr_old:
2102
2103 do {
2104 if (max-- < 0)
2105 break;
2106
2107 ch = inb(port->ioaddr + UART_RX);
2108 if (port->board->chip_flag && (*status & UART_LSR_OE))
2109 outb(0x23, port->ioaddr + UART_FCR);
2110 *status &= port->read_status_mask;
2111 if (*status & port->ignore_status_mask) {
2112 if (++ignored > 100)
2113 break;
2114 } else {
2115 char flag = 0;
2116 if (*status & UART_LSR_SPECIAL) {
2117 if (*status & UART_LSR_BI) {
2118 flag = TTY_BREAK;
2119 port->icount.brk++;
2120
2121 if (port->port.flags & ASYNC_SAK)
2122 do_SAK(tty);
2123 } else if (*status & UART_LSR_PE) {
2124 flag = TTY_PARITY;
2125 port->icount.parity++;
2126 } else if (*status & UART_LSR_FE) {
2127 flag = TTY_FRAME;
2128 port->icount.frame++;
2129 } else if (*status & UART_LSR_OE) {
2130 flag = TTY_OVERRUN;
2131 port->icount.overrun++;
2132 } else
2133 flag = TTY_BREAK;
2134 }
2135 tty_insert_flip_char(tty, ch, flag);
2136 cnt++;
2137 if (cnt >= recv_room) {
2138 if (!port->ldisc_stop_rx)
2139 mxser_stoprx(tty);
2140 break;
2141 }
2142
2143 }
2144
2145 if (port->board->chip_flag)
2146 break;
2147
2148 *status = inb(port->ioaddr + UART_LSR);
2149 } while (*status & UART_LSR_DR);
2150
2151end_intr:
2152 mxvar_log.rxcnt[tty->index] += cnt;
2153 port->mon_data.rxcnt += cnt;
2154 port->mon_data.up_rxcnt += cnt;
2155
2156 /*
2157 * We are called from an interrupt context with &port->slock
2158 * being held. Drop it temporarily in order to prevent
2159 * recursive locking.
2160 */
2161 spin_unlock(&port->slock);
2162 tty_flip_buffer_push(tty);
2163 spin_lock(&port->slock);
2164}
2165
2166static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
2167{
2168 int count, cnt;
2169
2170 if (port->x_char) {
2171 outb(port->x_char, port->ioaddr + UART_TX);
2172 port->x_char = 0;
2173 mxvar_log.txcnt[tty->index]++;
2174 port->mon_data.txcnt++;
2175 port->mon_data.up_txcnt++;
2176 port->icount.tx++;
2177 return;
2178 }
2179
2180 if (port->port.xmit_buf == NULL)
2181 return;
2182
2183 if (port->xmit_cnt <= 0 || tty->stopped ||
2184 (tty->hw_stopped &&
2185 (port->type != PORT_16550A) &&
2186 (!port->board->chip_flag))) {
2187 port->IER &= ~UART_IER_THRI;
2188 outb(port->IER, port->ioaddr + UART_IER);
2189 return;
2190 }
2191
2192 cnt = port->xmit_cnt;
2193 count = port->xmit_fifo_size;
2194 do {
2195 outb(port->port.xmit_buf[port->xmit_tail++],
2196 port->ioaddr + UART_TX);
2197 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2198 if (--port->xmit_cnt <= 0)
2199 break;
2200 } while (--count > 0);
2201 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
2202
2203 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2204 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2205 port->icount.tx += (cnt - port->xmit_cnt);
2206
2207 if (port->xmit_cnt < WAKEUP_CHARS)
2208 tty_wakeup(tty);
2209
2210 if (port->xmit_cnt <= 0) {
2211 port->IER &= ~UART_IER_THRI;
2212 outb(port->IER, port->ioaddr + UART_IER);
2213 }
2214}
2215
2216/*
2217 * This is the serial driver's generic interrupt routine
2218 */
2219static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2220{
2221 int status, iir, i;
2222 struct mxser_board *brd = NULL;
2223 struct mxser_port *port;
2224 int max, irqbits, bits, msr;
2225 unsigned int int_cnt, pass_counter = 0;
2226 int handled = IRQ_NONE;
2227 struct tty_struct *tty;
2228
2229 for (i = 0; i < MXSER_BOARDS; i++)
2230 if (dev_id == &mxser_boards[i]) {
2231 brd = dev_id;
2232 break;
2233 }
2234
2235 if (i == MXSER_BOARDS)
2236 goto irq_stop;
2237 if (brd == NULL)
2238 goto irq_stop;
2239 max = brd->info->nports;
2240 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2241 irqbits = inb(brd->vector) & brd->vector_mask;
2242 if (irqbits == brd->vector_mask)
2243 break;
2244
2245 handled = IRQ_HANDLED;
2246 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2247 if (irqbits == brd->vector_mask)
2248 break;
2249 if (bits & irqbits)
2250 continue;
2251 port = &brd->ports[i];
2252
2253 int_cnt = 0;
2254 spin_lock(&port->slock);
2255 do {
2256 iir = inb(port->ioaddr + UART_IIR);
2257 if (iir & UART_IIR_NO_INT)
2258 break;
2259 iir &= MOXA_MUST_IIR_MASK;
2260 tty = tty_port_tty_get(&port->port);
2261 if (!tty ||
2262 (port->port.flags & ASYNC_CLOSING) ||
2263 !(port->port.flags &
2264 ASYNC_INITIALIZED)) {
2265 status = inb(port->ioaddr + UART_LSR);
2266 outb(0x27, port->ioaddr + UART_FCR);
2267 inb(port->ioaddr + UART_MSR);
2268 tty_kref_put(tty);
2269 break;
2270 }
2271
2272 status = inb(port->ioaddr + UART_LSR);
2273
2274 if (status & UART_LSR_PE)
2275 port->err_shadow |= NPPI_NOTIFY_PARITY;
2276 if (status & UART_LSR_FE)
2277 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2278 if (status & UART_LSR_OE)
2279 port->err_shadow |=
2280 NPPI_NOTIFY_HW_OVERRUN;
2281 if (status & UART_LSR_BI)
2282 port->err_shadow |= NPPI_NOTIFY_BREAK;
2283
2284 if (port->board->chip_flag) {
2285 if (iir == MOXA_MUST_IIR_GDA ||
2286 iir == MOXA_MUST_IIR_RDA ||
2287 iir == MOXA_MUST_IIR_RTO ||
2288 iir == MOXA_MUST_IIR_LSR)
2289 mxser_receive_chars(tty, port,
2290 &status);
2291
2292 } else {
2293 status &= port->read_status_mask;
2294 if (status & UART_LSR_DR)
2295 mxser_receive_chars(tty, port,
2296 &status);
2297 }
2298 msr = inb(port->ioaddr + UART_MSR);
2299 if (msr & UART_MSR_ANY_DELTA)
2300 mxser_check_modem_status(tty, port, msr);
2301
2302 if (port->board->chip_flag) {
2303 if (iir == 0x02 && (status &
2304 UART_LSR_THRE))
2305 mxser_transmit_chars(tty, port);
2306 } else {
2307 if (status & UART_LSR_THRE)
2308 mxser_transmit_chars(tty, port);
2309 }
2310 tty_kref_put(tty);
2311 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2312 spin_unlock(&port->slock);
2313 }
2314 }
2315
2316irq_stop:
2317 return handled;
2318}
2319
2320static const struct tty_operations mxser_ops = {
2321 .open = mxser_open,
2322 .close = mxser_close,
2323 .write = mxser_write,
2324 .put_char = mxser_put_char,
2325 .flush_chars = mxser_flush_chars,
2326 .write_room = mxser_write_room,
2327 .chars_in_buffer = mxser_chars_in_buffer,
2328 .flush_buffer = mxser_flush_buffer,
2329 .ioctl = mxser_ioctl,
2330 .throttle = mxser_throttle,
2331 .unthrottle = mxser_unthrottle,
2332 .set_termios = mxser_set_termios,
2333 .stop = mxser_stop,
2334 .start = mxser_start,
2335 .hangup = mxser_hangup,
2336 .break_ctl = mxser_rs_break,
2337 .wait_until_sent = mxser_wait_until_sent,
2338 .tiocmget = mxser_tiocmget,
2339 .tiocmset = mxser_tiocmset,
2340 .get_icount = mxser_get_icount,
2341};
2342
2343struct tty_port_operations mxser_port_ops = {
2344 .carrier_raised = mxser_carrier_raised,
2345 .dtr_rts = mxser_dtr_rts,
2346 .activate = mxser_activate,
2347 .shutdown = mxser_shutdown_port,
2348};
2349
2350/*
2351 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2352 */
2353
2354static void mxser_release_ISA_res(struct mxser_board *brd)
2355{
2356 free_irq(brd->irq, brd);
2357 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2358 release_region(brd->vector, 1);
2359}
2360
2361static int __devinit mxser_initbrd(struct mxser_board *brd,
2362 struct pci_dev *pdev)
2363{
2364 struct mxser_port *info;
2365 unsigned int i;
2366 int retval;
2367
2368 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2369 brd->ports[0].max_baud);
2370
2371 for (i = 0; i < brd->info->nports; i++) {
2372 info = &brd->ports[i];
2373 tty_port_init(&info->port);
2374 info->port.ops = &mxser_port_ops;
2375 info->board = brd;
2376 info->stop_rx = 0;
2377 info->ldisc_stop_rx = 0;
2378
2379 /* Enhance mode enabled here */
2380 if (brd->chip_flag != MOXA_OTHER_UART)
2381 mxser_enable_must_enchance_mode(info->ioaddr);
2382
2383 info->port.flags = ASYNC_SHARE_IRQ;
2384 info->type = brd->uart_type;
2385
2386 process_txrx_fifo(info);
2387
2388 info->custom_divisor = info->baud_base * 16;
2389 info->port.close_delay = 5 * HZ / 10;
2390 info->port.closing_wait = 30 * HZ;
2391 info->normal_termios = mxvar_sdriver->init_termios;
2392 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2393 info->err_shadow = 0;
2394 spin_lock_init(&info->slock);
2395
2396 /* before set INT ISR, disable all int */
2397 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2398 info->ioaddr + UART_IER);
2399 }
2400
2401 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2402 brd);
2403 if (retval)
2404 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2405 "conflict with another device.\n",
2406 brd->info->name, brd->irq);
2407
2408 return retval;
2409}
2410
2411static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2412{
2413 int id, i, bits;
2414 unsigned short regs[16], irq;
2415 unsigned char scratch, scratch2;
2416
2417 brd->chip_flag = MOXA_OTHER_UART;
2418
2419 id = mxser_read_register(cap, regs);
2420 switch (id) {
2421 case C168_ASIC_ID:
2422 brd->info = &mxser_cards[0];
2423 break;
2424 case C104_ASIC_ID:
2425 brd->info = &mxser_cards[1];
2426 break;
2427 case CI104J_ASIC_ID:
2428 brd->info = &mxser_cards[2];
2429 break;
2430 case C102_ASIC_ID:
2431 brd->info = &mxser_cards[5];
2432 break;
2433 case CI132_ASIC_ID:
2434 brd->info = &mxser_cards[6];
2435 break;
2436 case CI134_ASIC_ID:
2437 brd->info = &mxser_cards[7];
2438 break;
2439 default:
2440 return 0;
2441 }
2442
2443 irq = 0;
2444 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2445 Flag-hack checks if configuration should be read as 2-port here. */
2446 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2447 irq = regs[9] & 0xF000;
2448 irq = irq | (irq >> 4);
2449 if (irq != (regs[9] & 0xFF00))
2450 goto err_irqconflict;
2451 } else if (brd->info->nports == 4) {
2452 irq = regs[9] & 0xF000;
2453 irq = irq | (irq >> 4);
2454 irq = irq | (irq >> 8);
2455 if (irq != regs[9])
2456 goto err_irqconflict;
2457 } else if (brd->info->nports == 8) {
2458 irq = regs[9] & 0xF000;
2459 irq = irq | (irq >> 4);
2460 irq = irq | (irq >> 8);
2461 if ((irq != regs[9]) || (irq != regs[10]))
2462 goto err_irqconflict;
2463 }
2464
2465 if (!irq) {
2466 printk(KERN_ERR "mxser: interrupt number unset\n");
2467 return -EIO;
2468 }
2469 brd->irq = ((int)(irq & 0xF000) >> 12);
2470 for (i = 0; i < 8; i++)
2471 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2472 if ((regs[12] & 0x80) == 0) {
2473 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2474 return -EIO;
2475 }
2476 brd->vector = (int)regs[11]; /* interrupt vector */
2477 if (id == 1)
2478 brd->vector_mask = 0x00FF;
2479 else
2480 brd->vector_mask = 0x000F;
2481 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2482 if (regs[12] & bits) {
2483 brd->ports[i].baud_base = 921600;
2484 brd->ports[i].max_baud = 921600;
2485 } else {
2486 brd->ports[i].baud_base = 115200;
2487 brd->ports[i].max_baud = 115200;
2488 }
2489 }
2490 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2491 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2492 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2493 outb(scratch2, cap + UART_LCR);
2494 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2495 scratch = inb(cap + UART_IIR);
2496
2497 if (scratch & 0xC0)
2498 brd->uart_type = PORT_16550A;
2499 else
2500 brd->uart_type = PORT_16450;
2501 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2502 "mxser(IO)")) {
2503 printk(KERN_ERR "mxser: can't request ports I/O region: "
2504 "0x%.8lx-0x%.8lx\n",
2505 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2506 8 * brd->info->nports - 1);
2507 return -EIO;
2508 }
2509 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2510 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2511 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2512 "0x%.8lx-0x%.8lx\n",
2513 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2514 8 * brd->info->nports - 1);
2515 return -EIO;
2516 }
2517 return brd->info->nports;
2518
2519err_irqconflict:
2520 printk(KERN_ERR "mxser: invalid interrupt number\n");
2521 return -EIO;
2522}
2523
2524static int __devinit mxser_probe(struct pci_dev *pdev,
2525 const struct pci_device_id *ent)
2526{
2527#ifdef CONFIG_PCI
2528 struct mxser_board *brd;
2529 unsigned int i, j;
2530 unsigned long ioaddress;
2531 int retval = -EINVAL;
2532
2533 for (i = 0; i < MXSER_BOARDS; i++)
2534 if (mxser_boards[i].info == NULL)
2535 break;
2536
2537 if (i >= MXSER_BOARDS) {
2538 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2539 "not configured\n", MXSER_BOARDS);
2540 goto err;
2541 }
2542
2543 brd = &mxser_boards[i];
2544 brd->idx = i * MXSER_PORTS_PER_BOARD;
2545 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2546 mxser_cards[ent->driver_data].name,
2547 pdev->bus->number, PCI_SLOT(pdev->devfn));
2548
2549 retval = pci_enable_device(pdev);
2550 if (retval) {
2551 dev_err(&pdev->dev, "PCI enable failed\n");
2552 goto err;
2553 }
2554
2555 /* io address */
2556 ioaddress = pci_resource_start(pdev, 2);
2557 retval = pci_request_region(pdev, 2, "mxser(IO)");
2558 if (retval)
2559 goto err_dis;
2560
2561 brd->info = &mxser_cards[ent->driver_data];
2562 for (i = 0; i < brd->info->nports; i++)
2563 brd->ports[i].ioaddr = ioaddress + 8 * i;
2564
2565 /* vector */
2566 ioaddress = pci_resource_start(pdev, 3);
2567 retval = pci_request_region(pdev, 3, "mxser(vector)");
2568 if (retval)
2569 goto err_zero;
2570 brd->vector = ioaddress;
2571
2572 /* irq */
2573 brd->irq = pdev->irq;
2574
2575 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2576 brd->uart_type = PORT_16550A;
2577 brd->vector_mask = 0;
2578
2579 for (i = 0; i < brd->info->nports; i++) {
2580 for (j = 0; j < UART_INFO_NUM; j++) {
2581 if (Gpci_uart_info[j].type == brd->chip_flag) {
2582 brd->ports[i].max_baud =
2583 Gpci_uart_info[j].max_baud;
2584
2585 /* exception....CP-102 */
2586 if (brd->info->flags & MXSER_HIGHBAUD)
2587 brd->ports[i].max_baud = 921600;
2588 break;
2589 }
2590 }
2591 }
2592
2593 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2594 for (i = 0; i < brd->info->nports; i++) {
2595 if (i < 4)
2596 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2597 else
2598 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2599 }
2600 outb(0, ioaddress + 4); /* default set to RS232 mode */
2601 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2602 }
2603
2604 for (i = 0; i < brd->info->nports; i++) {
2605 brd->vector_mask |= (1 << i);
2606 brd->ports[i].baud_base = 921600;
2607 }
2608
2609 /* mxser_initbrd will hook ISR. */
2610 retval = mxser_initbrd(brd, pdev);
2611 if (retval)
2612 goto err_rel3;
2613
2614 for (i = 0; i < brd->info->nports; i++)
2615 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2616
2617 pci_set_drvdata(pdev, brd);
2618
2619 return 0;
2620err_rel3:
2621 pci_release_region(pdev, 3);
2622err_zero:
2623 brd->info = NULL;
2624 pci_release_region(pdev, 2);
2625err_dis:
2626 pci_disable_device(pdev);
2627err:
2628 return retval;
2629#else
2630 return -ENODEV;
2631#endif
2632}
2633
2634static void __devexit mxser_remove(struct pci_dev *pdev)
2635{
2636#ifdef CONFIG_PCI
2637 struct mxser_board *brd = pci_get_drvdata(pdev);
2638 unsigned int i;
2639
2640 for (i = 0; i < brd->info->nports; i++)
2641 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2642
2643 free_irq(pdev->irq, brd);
2644 pci_release_region(pdev, 2);
2645 pci_release_region(pdev, 3);
2646 pci_disable_device(pdev);
2647 brd->info = NULL;
2648#endif
2649}
2650
2651static struct pci_driver mxser_driver = {
2652 .name = "mxser",
2653 .id_table = mxser_pcibrds,
2654 .probe = mxser_probe,
2655 .remove = __devexit_p(mxser_remove)
2656};
2657
2658static int __init mxser_module_init(void)
2659{
2660 struct mxser_board *brd;
2661 unsigned int b, i, m;
2662 int retval;
2663
2664 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2665 if (!mxvar_sdriver)
2666 return -ENOMEM;
2667
2668 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2669 MXSER_VERSION);
2670
2671 /* Initialize the tty_driver structure */
2672 mxvar_sdriver->owner = THIS_MODULE;
2673 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2674 mxvar_sdriver->name = "ttyMI";
2675 mxvar_sdriver->major = ttymajor;
2676 mxvar_sdriver->minor_start = 0;
2677 mxvar_sdriver->num = MXSER_PORTS + 1;
2678 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2679 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2680 mxvar_sdriver->init_termios = tty_std_termios;
2681 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2682 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2683 tty_set_operations(mxvar_sdriver, &mxser_ops);
2684
2685 retval = tty_register_driver(mxvar_sdriver);
2686 if (retval) {
2687 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2688 "tty driver !\n");
2689 goto err_put;
2690 }
2691
2692 /* Start finding ISA boards here */
2693 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2694 if (!ioaddr[b])
2695 continue;
2696
2697 brd = &mxser_boards[m];
2698 retval = mxser_get_ISA_conf(ioaddr[b], brd);
2699 if (retval <= 0) {
2700 brd->info = NULL;
2701 continue;
2702 }
2703
2704 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2705 brd->info->name, ioaddr[b]);
2706
2707 /* mxser_initbrd will hook ISR. */
2708 if (mxser_initbrd(brd, NULL) < 0) {
2709 brd->info = NULL;
2710 continue;
2711 }
2712
2713 brd->idx = m * MXSER_PORTS_PER_BOARD;
2714 for (i = 0; i < brd->info->nports; i++)
2715 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2716
2717 m++;
2718 }
2719
2720 retval = pci_register_driver(&mxser_driver);
2721 if (retval) {
2722 printk(KERN_ERR "mxser: can't register pci driver\n");
2723 if (!m) {
2724 retval = -ENODEV;
2725 goto err_unr;
2726 } /* else: we have some ISA cards under control */
2727 }
2728
2729 return 0;
2730err_unr:
2731 tty_unregister_driver(mxvar_sdriver);
2732err_put:
2733 put_tty_driver(mxvar_sdriver);
2734 return retval;
2735}
2736
2737static void __exit mxser_module_exit(void)
2738{
2739 unsigned int i, j;
2740
2741 pci_unregister_driver(&mxser_driver);
2742
2743 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2744 if (mxser_boards[i].info != NULL)
2745 for (j = 0; j < mxser_boards[i].info->nports; j++)
2746 tty_unregister_device(mxvar_sdriver,
2747 mxser_boards[i].idx + j);
2748 tty_unregister_driver(mxvar_sdriver);
2749 put_tty_driver(mxvar_sdriver);
2750
2751 for (i = 0; i < MXSER_BOARDS; i++)
2752 if (mxser_boards[i].info != NULL)
2753 mxser_release_ISA_res(&mxser_boards[i]);
2754}
2755
2756module_init(mxser_module_init);
2757module_exit(mxser_module_exit);
diff --git a/drivers/tty/mxser.h b/drivers/tty/mxser.h
new file mode 100644
index 000000000000..0bf794313ffd
--- /dev/null
+++ b/drivers/tty/mxser.h
@@ -0,0 +1,150 @@
1#ifndef _MXSER_H
2#define _MXSER_H
3
4/*
5 * Semi-public control interfaces
6 */
7
8/*
9 * MOXA ioctls
10 */
11
12#define MOXA 0x400
13#define MOXA_GETDATACOUNT (MOXA + 23)
14#define MOXA_DIAGNOSE (MOXA + 50)
15#define MOXA_CHKPORTENABLE (MOXA + 60)
16#define MOXA_HighSpeedOn (MOXA + 61)
17#define MOXA_GET_MAJOR (MOXA + 63)
18#define MOXA_GETMSTATUS (MOXA + 65)
19#define MOXA_SET_OP_MODE (MOXA + 66)
20#define MOXA_GET_OP_MODE (MOXA + 67)
21
22#define RS232_MODE 0
23#define RS485_2WIRE_MODE 1
24#define RS422_MODE 2
25#define RS485_4WIRE_MODE 3
26#define OP_MODE_MASK 3
27
28#define MOXA_SDS_RSTICOUNTER (MOXA + 69)
29#define MOXA_ASPP_OQUEUE (MOXA + 70)
30#define MOXA_ASPP_MON (MOXA + 73)
31#define MOXA_ASPP_LSTATUS (MOXA + 74)
32#define MOXA_ASPP_MON_EXT (MOXA + 75)
33#define MOXA_SET_BAUD_METHOD (MOXA + 76)
34
35/* --------------------------------------------------- */
36
37#define NPPI_NOTIFY_PARITY 0x01
38#define NPPI_NOTIFY_FRAMING 0x02
39#define NPPI_NOTIFY_HW_OVERRUN 0x04
40#define NPPI_NOTIFY_SW_OVERRUN 0x08
41#define NPPI_NOTIFY_BREAK 0x10
42
43#define NPPI_NOTIFY_CTSHOLD 0x01 /* Tx hold by CTS low */
44#define NPPI_NOTIFY_DSRHOLD 0x02 /* Tx hold by DSR low */
45#define NPPI_NOTIFY_XOFFHOLD 0x08 /* Tx hold by Xoff received */
46#define NPPI_NOTIFY_XOFFXENT 0x10 /* Xoff Sent */
47
48/* follow just for Moxa Must chip define. */
49/* */
50/* when LCR register (offset 0x03) write following value, */
51/* the Must chip will enter enchance mode. And write value */
52/* on EFR (offset 0x02) bit 6,7 to change bank. */
53#define MOXA_MUST_ENTER_ENCHANCE 0xBF
54
55/* when enhance mode enable, access on general bank register */
56#define MOXA_MUST_GDL_REGISTER 0x07
57#define MOXA_MUST_GDL_MASK 0x7F
58#define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
59
60#define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
61/* enchance register bank select and enchance mode setting register */
62/* when LCR register equal to 0xBF */
63#define MOXA_MUST_EFR_REGISTER 0x02
64/* enchance mode enable */
65#define MOXA_MUST_EFR_EFRB_ENABLE 0x10
66/* enchance reister bank set 0, 1, 2 */
67#define MOXA_MUST_EFR_BANK0 0x00
68#define MOXA_MUST_EFR_BANK1 0x40
69#define MOXA_MUST_EFR_BANK2 0x80
70#define MOXA_MUST_EFR_BANK3 0xC0
71#define MOXA_MUST_EFR_BANK_MASK 0xC0
72
73/* set XON1 value register, when LCR=0xBF and change to bank0 */
74#define MOXA_MUST_XON1_REGISTER 0x04
75
76/* set XON2 value register, when LCR=0xBF and change to bank0 */
77#define MOXA_MUST_XON2_REGISTER 0x05
78
79/* set XOFF1 value register, when LCR=0xBF and change to bank0 */
80#define MOXA_MUST_XOFF1_REGISTER 0x06
81
82/* set XOFF2 value register, when LCR=0xBF and change to bank0 */
83#define MOXA_MUST_XOFF2_REGISTER 0x07
84
85#define MOXA_MUST_RBRTL_REGISTER 0x04
86#define MOXA_MUST_RBRTH_REGISTER 0x05
87#define MOXA_MUST_RBRTI_REGISTER 0x06
88#define MOXA_MUST_THRTL_REGISTER 0x07
89#define MOXA_MUST_ENUM_REGISTER 0x04
90#define MOXA_MUST_HWID_REGISTER 0x05
91#define MOXA_MUST_ECR_REGISTER 0x06
92#define MOXA_MUST_CSR_REGISTER 0x07
93
94/* good data mode enable */
95#define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20
96/* only good data put into RxFIFO */
97#define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10
98
99/* enable CTS interrupt */
100#define MOXA_MUST_IER_ECTSI 0x80
101/* enable RTS interrupt */
102#define MOXA_MUST_IER_ERTSI 0x40
103/* enable Xon/Xoff interrupt */
104#define MOXA_MUST_IER_XINT 0x20
105/* enable GDA interrupt */
106#define MOXA_MUST_IER_EGDAI 0x10
107
108#define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
109
110/* GDA interrupt pending */
111#define MOXA_MUST_IIR_GDA 0x1C
112#define MOXA_MUST_IIR_RDA 0x04
113#define MOXA_MUST_IIR_RTO 0x0C
114#define MOXA_MUST_IIR_LSR 0x06
115
116/* received Xon/Xoff or specical interrupt pending */
117#define MOXA_MUST_IIR_XSC 0x10
118
119/* RTS/CTS change state interrupt pending */
120#define MOXA_MUST_IIR_RTSCTS 0x20
121#define MOXA_MUST_IIR_MASK 0x3E
122
123#define MOXA_MUST_MCR_XON_FLAG 0x40
124#define MOXA_MUST_MCR_XON_ANY 0x80
125#define MOXA_MUST_MCR_TX_XON 0x08
126
127/* software flow control on chip mask value */
128#define MOXA_MUST_EFR_SF_MASK 0x0F
129/* send Xon1/Xoff1 */
130#define MOXA_MUST_EFR_SF_TX1 0x08
131/* send Xon2/Xoff2 */
132#define MOXA_MUST_EFR_SF_TX2 0x04
133/* send Xon1,Xon2/Xoff1,Xoff2 */
134#define MOXA_MUST_EFR_SF_TX12 0x0C
135/* don't send Xon/Xoff */
136#define MOXA_MUST_EFR_SF_TX_NO 0x00
137/* Tx software flow control mask */
138#define MOXA_MUST_EFR_SF_TX_MASK 0x0C
139/* don't receive Xon/Xoff */
140#define MOXA_MUST_EFR_SF_RX_NO 0x00
141/* receive Xon1/Xoff1 */
142#define MOXA_MUST_EFR_SF_RX1 0x02
143/* receive Xon2/Xoff2 */
144#define MOXA_MUST_EFR_SF_RX2 0x01
145/* receive Xon1,Xon2/Xoff1,Xoff2 */
146#define MOXA_MUST_EFR_SF_RX12 0x03
147/* Rx software flow control mask */
148#define MOXA_MUST_EFR_SF_RX_MASK 0x03
149
150#endif
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
new file mode 100644
index 000000000000..19b4ae052af8
--- /dev/null
+++ b/drivers/tty/n_gsm.c
@@ -0,0 +1,2810 @@
1/*
2 * n_gsm.c GSM 0710 tty multiplexor
3 * Copyright (c) 2009/10 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
19 *
20 * TO DO:
21 * Mostly done: ioctls for setting modes/timing
22 * Partly done: hooks so you can pull off frames to non tty devs
23 * Restart DLCI 0 when it closes ?
24 * Test basic encoding
25 * Improve the tx engine
26 * Resolve tx side locking by adding a queue_head and routing
27 * all control traffic via it
28 * General tidy/document
29 * Review the locking/move to refcounts more (mux now moved to an
30 * alloc/free model ready)
31 * Use newest tty open/close port helpers and install hooks
32 * What to do about power functions ?
33 * Termios setting and negotiation
34 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
35 *
36 */
37
38#include <linux/types.h>
39#include <linux/major.h>
40#include <linux/errno.h>
41#include <linux/signal.h>
42#include <linux/fcntl.h>
43#include <linux/sched.h>
44#include <linux/interrupt.h>
45#include <linux/tty.h>
46#include <linux/ctype.h>
47#include <linux/mm.h>
48#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/poll.h>
51#include <linux/bitops.h>
52#include <linux/file.h>
53#include <linux/uaccess.h>
54#include <linux/module.h>
55#include <linux/timer.h>
56#include <linux/tty_flip.h>
57#include <linux/tty_driver.h>
58#include <linux/serial.h>
59#include <linux/kfifo.h>
60#include <linux/skbuff.h>
61#include <linux/gsmmux.h>
62
63static int debug;
64module_param(debug, int, 0600);
65
66#define T1 (HZ/10)
67#define T2 (HZ/3)
68#define N2 3
69
70/* Use long timers for testing at low speed with debug on */
71#ifdef DEBUG_TIMING
72#define T1 HZ
73#define T2 (2 * HZ)
74#endif
75
76/*
77 * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
78 * limits so this is plenty
79 */
80#define MAX_MRU 512
81#define MAX_MTU 512
82
83/*
84 * Each block of data we have queued to go out is in the form of
85 * a gsm_msg which holds everything we need in a link layer independent
86 * format
87 */
88
89struct gsm_msg {
90 struct gsm_msg *next;
91 u8 addr; /* DLCI address + flags */
92 u8 ctrl; /* Control byte + flags */
93 unsigned int len; /* Length of data block (can be zero) */
94 unsigned char *data; /* Points into buffer but not at the start */
95 unsigned char buffer[0];
96};
97
98/*
99 * Each active data link has a gsm_dlci structure associated which ties
100 * the link layer to an optional tty (if the tty side is open). To avoid
101 * complexity right now these are only ever freed up when the mux is
102 * shut down.
103 *
104 * At the moment we don't free DLCI objects until the mux is torn down
105 * this avoid object life time issues but might be worth review later.
106 */
107
108struct gsm_dlci {
109 struct gsm_mux *gsm;
110 int addr;
111 int state;
112#define DLCI_CLOSED 0
113#define DLCI_OPENING 1 /* Sending SABM not seen UA */
114#define DLCI_OPEN 2 /* SABM/UA complete */
115#define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */
116
117 /* Link layer */
118 spinlock_t lock; /* Protects the internal state */
119 struct timer_list t1; /* Retransmit timer for SABM and UA */
120 int retries;
121 /* Uplink tty if active */
122 struct tty_port port; /* The tty bound to this DLCI if there is one */
123 struct kfifo *fifo; /* Queue fifo for the DLCI */
124 struct kfifo _fifo; /* For new fifo API porting only */
125 int adaption; /* Adaption layer in use */
126 u32 modem_rx; /* Our incoming virtual modem lines */
127 u32 modem_tx; /* Our outgoing modem lines */
128 int dead; /* Refuse re-open */
129 /* Flow control */
130 int throttled; /* Private copy of throttle state */
131 int constipated; /* Throttle status for outgoing */
132 /* Packetised I/O */
133 struct sk_buff *skb; /* Frame being sent */
134 struct sk_buff_head skb_list; /* Queued frames */
135 /* Data handling callback */
136 void (*data)(struct gsm_dlci *dlci, u8 *data, int len);
137};
138
139/* DLCI 0, 62/63 are special or reseved see gsmtty_open */
140
141#define NUM_DLCI 64
142
143/*
144 * DLCI 0 is used to pass control blocks out of band of the data
145 * flow (and with a higher link priority). One command can be outstanding
146 * at a time and we use this structure to manage them. They are created
147 * and destroyed by the user context, and updated by the receive paths
148 * and timers
149 */
150
151struct gsm_control {
152 u8 cmd; /* Command we are issuing */
153 u8 *data; /* Data for the command in case we retransmit */
154 int len; /* Length of block for retransmission */
155 int done; /* Done flag */
156 int error; /* Error if any */
157};
158
159/*
160 * Each GSM mux we have is represented by this structure. If we are
161 * operating as an ldisc then we use this structure as our ldisc
162 * state. We need to sort out lifetimes and locking with respect
163 * to the gsm mux array. For now we don't free DLCI objects that
164 * have been instantiated until the mux itself is terminated.
165 *
166 * To consider further: tty open versus mux shutdown.
167 */
168
169struct gsm_mux {
170 struct tty_struct *tty; /* The tty our ldisc is bound to */
171 spinlock_t lock;
172
173 /* Events on the GSM channel */
174 wait_queue_head_t event;
175
176 /* Bits for GSM mode decoding */
177
178 /* Framing Layer */
179 unsigned char *buf;
180 int state;
181#define GSM_SEARCH 0
182#define GSM_START 1
183#define GSM_ADDRESS 2
184#define GSM_CONTROL 3
185#define GSM_LEN 4
186#define GSM_DATA 5
187#define GSM_FCS 6
188#define GSM_OVERRUN 7
189#define GSM_LEN0 8
190#define GSM_LEN1 9
191#define GSM_SSOF 10
192 unsigned int len;
193 unsigned int address;
194 unsigned int count;
195 int escape;
196 int encoding;
197 u8 control;
198 u8 fcs;
199 u8 received_fcs;
200 u8 *txframe; /* TX framing buffer */
201
202 /* Methods for the receiver side */
203 void (*receive)(struct gsm_mux *gsm, u8 ch);
204 void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
205 /* And transmit side */
206 int (*output)(struct gsm_mux *mux, u8 *data, int len);
207
208 /* Link Layer */
209 unsigned int mru;
210 unsigned int mtu;
211 int initiator; /* Did we initiate connection */
212 int dead; /* Has the mux been shut down */
213 struct gsm_dlci *dlci[NUM_DLCI];
214 int constipated; /* Asked by remote to shut up */
215
216 spinlock_t tx_lock;
217 unsigned int tx_bytes; /* TX data outstanding */
218#define TX_THRESH_HI 8192
219#define TX_THRESH_LO 2048
220 struct gsm_msg *tx_head; /* Pending data packets */
221 struct gsm_msg *tx_tail;
222
223 /* Control messages */
224 struct timer_list t2_timer; /* Retransmit timer for commands */
225 int cretries; /* Command retry counter */
226 struct gsm_control *pending_cmd;/* Our current pending command */
227 spinlock_t control_lock; /* Protects the pending command */
228
229 /* Configuration */
230 int adaption; /* 1 or 2 supported */
231 u8 ftype; /* UI or UIH */
232 int t1, t2; /* Timers in 1/100th of a sec */
233 int n2; /* Retry count */
234
235 /* Statistics (not currently exposed) */
236 unsigned long bad_fcs;
237 unsigned long malformed;
238 unsigned long io_error;
239 unsigned long bad_size;
240 unsigned long unsupported;
241};
242
243
244/*
245 * Mux objects - needed so that we can translate a tty index into the
246 * relevant mux and DLCI.
247 */
248
249#define MAX_MUX 4 /* 256 minors */
250static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
251static spinlock_t gsm_mux_lock;
252
253/*
254 * This section of the driver logic implements the GSM encodings
255 * both the basic and the 'advanced'. Reliable transport is not
256 * supported.
257 */
258
259#define CR 0x02
260#define EA 0x01
261#define PF 0x10
262
263/* I is special: the rest are ..*/
264#define RR 0x01
265#define UI 0x03
266#define RNR 0x05
267#define REJ 0x09
268#define DM 0x0F
269#define SABM 0x2F
270#define DISC 0x43
271#define UA 0x63
272#define UIH 0xEF
273
274/* Channel commands */
275#define CMD_NSC 0x09
276#define CMD_TEST 0x11
277#define CMD_PSC 0x21
278#define CMD_RLS 0x29
279#define CMD_FCOFF 0x31
280#define CMD_PN 0x41
281#define CMD_RPN 0x49
282#define CMD_FCON 0x51
283#define CMD_CLD 0x61
284#define CMD_SNC 0x69
285#define CMD_MSC 0x71
286
287/* Virtual modem bits */
288#define MDM_FC 0x01
289#define MDM_RTC 0x02
290#define MDM_RTR 0x04
291#define MDM_IC 0x20
292#define MDM_DV 0x40
293
294#define GSM0_SOF 0xF9
295#define GSM1_SOF 0x7E
296#define GSM1_ESCAPE 0x7D
297#define GSM1_ESCAPE_BITS 0x20
298#define XON 0x11
299#define XOFF 0x13
300
301static const struct tty_port_operations gsm_port_ops;
302
303/*
304 * CRC table for GSM 0710
305 */
306
307static const u8 gsm_fcs8[256] = {
308 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
309 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
310 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
311 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
312 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
313 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
314 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
315 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
316 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
317 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
318 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
319 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
320 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
321 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
322 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
323 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
324 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
325 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
326 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
327 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
328 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
329 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
330 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
331 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
332 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
333 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
334 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
335 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
336 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
337 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
338 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
339 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
340};
341
342#define INIT_FCS 0xFF
343#define GOOD_FCS 0xCF
344
345/**
346 * gsm_fcs_add - update FCS
347 * @fcs: Current FCS
348 * @c: Next data
349 *
350 * Update the FCS to include c. Uses the algorithm in the specification
351 * notes.
352 */
353
354static inline u8 gsm_fcs_add(u8 fcs, u8 c)
355{
356 return gsm_fcs8[fcs ^ c];
357}
358
359/**
360 * gsm_fcs_add_block - update FCS for a block
361 * @fcs: Current FCS
362 * @c: buffer of data
363 * @len: length of buffer
364 *
365 * Update the FCS to include c. Uses the algorithm in the specification
366 * notes.
367 */
368
369static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
370{
371 while (len--)
372 fcs = gsm_fcs8[fcs ^ *c++];
373 return fcs;
374}
375
376/**
377 * gsm_read_ea - read a byte into an EA
378 * @val: variable holding value
379 * c: byte going into the EA
380 *
381 * Processes one byte of an EA. Updates the passed variable
382 * and returns 1 if the EA is now completely read
383 */
384
385static int gsm_read_ea(unsigned int *val, u8 c)
386{
387 /* Add the next 7 bits into the value */
388 *val <<= 7;
389 *val |= c >> 1;
390 /* Was this the last byte of the EA 1 = yes*/
391 return c & EA;
392}
393
394/**
395 * gsm_encode_modem - encode modem data bits
396 * @dlci: DLCI to encode from
397 *
398 * Returns the correct GSM encoded modem status bits (6 bit field) for
399 * the current status of the DLCI and attached tty object
400 */
401
402static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
403{
404 u8 modembits = 0;
405 /* FC is true flow control not modem bits */
406 if (dlci->throttled)
407 modembits |= MDM_FC;
408 if (dlci->modem_tx & TIOCM_DTR)
409 modembits |= MDM_RTC;
410 if (dlci->modem_tx & TIOCM_RTS)
411 modembits |= MDM_RTR;
412 if (dlci->modem_tx & TIOCM_RI)
413 modembits |= MDM_IC;
414 if (dlci->modem_tx & TIOCM_CD)
415 modembits |= MDM_DV;
416 return modembits;
417}
418
419/**
420 * gsm_print_packet - display a frame for debug
421 * @hdr: header to print before decode
422 * @addr: address EA from the frame
423 * @cr: C/R bit from the frame
424 * @control: control including PF bit
425 * @data: following data bytes
426 * @dlen: length of data
427 *
428 * Displays a packet in human readable format for debugging purposes. The
429 * style is based on amateur radio LAP-B dump display.
430 */
431
432static void gsm_print_packet(const char *hdr, int addr, int cr,
433 u8 control, const u8 *data, int dlen)
434{
435 if (!(debug & 1))
436 return;
437
438 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
439
440 switch (control & ~PF) {
441 case SABM:
442 pr_cont("SABM");
443 break;
444 case UA:
445 pr_cont("UA");
446 break;
447 case DISC:
448 pr_cont("DISC");
449 break;
450 case DM:
451 pr_cont("DM");
452 break;
453 case UI:
454 pr_cont("UI");
455 break;
456 case UIH:
457 pr_cont("UIH");
458 break;
459 default:
460 if (!(control & 0x01)) {
461 pr_cont("I N(S)%d N(R)%d",
462 (control & 0x0E) >> 1, (control & 0xE) >> 5);
463 } else switch (control & 0x0F) {
464 case RR:
465 pr_cont("RR(%d)", (control & 0xE0) >> 5);
466 break;
467 case RNR:
468 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
469 break;
470 case REJ:
471 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
472 break;
473 default:
474 pr_cont("[%02X]", control);
475 }
476 }
477
478 if (control & PF)
479 pr_cont("(P)");
480 else
481 pr_cont("(F)");
482
483 if (dlen) {
484 int ct = 0;
485 while (dlen--) {
486 if (ct % 8 == 0) {
487 pr_cont("\n");
488 pr_debug(" ");
489 }
490 pr_cont("%02X ", *data++);
491 ct++;
492 }
493 }
494 pr_cont("\n");
495}
496
497
498/*
499 * Link level transmission side
500 */
501
502/**
503 * gsm_stuff_packet - bytestuff a packet
504 * @ibuf: input
505 * @obuf: output
506 * @len: length of input
507 *
508 * Expand a buffer by bytestuffing it. The worst case size change
509 * is doubling and the caller is responsible for handing out
510 * suitable sized buffers.
511 */
512
513static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
514{
515 int olen = 0;
516 while (len--) {
517 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
518 || *input == XON || *input == XOFF) {
519 *output++ = GSM1_ESCAPE;
520 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
521 olen++;
522 } else
523 *output++ = *input++;
524 olen++;
525 }
526 return olen;
527}
528
529/**
530 * gsm_send - send a control frame
531 * @gsm: our GSM mux
532 * @addr: address for control frame
533 * @cr: command/response bit
534 * @control: control byte including PF bit
535 *
536 * Format up and transmit a control frame. These do not go via the
537 * queueing logic as they should be transmitted ahead of data when
538 * they are needed.
539 *
540 * FIXME: Lock versus data TX path
541 */
542
543static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
544{
545 int len;
546 u8 cbuf[10];
547 u8 ibuf[3];
548
549 switch (gsm->encoding) {
550 case 0:
551 cbuf[0] = GSM0_SOF;
552 cbuf[1] = (addr << 2) | (cr << 1) | EA;
553 cbuf[2] = control;
554 cbuf[3] = EA; /* Length of data = 0 */
555 cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
556 cbuf[5] = GSM0_SOF;
557 len = 6;
558 break;
559 case 1:
560 case 2:
561 /* Control frame + packing (but not frame stuffing) in mode 1 */
562 ibuf[0] = (addr << 2) | (cr << 1) | EA;
563 ibuf[1] = control;
564 ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
565 /* Stuffing may double the size worst case */
566 len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
567 /* Now add the SOF markers */
568 cbuf[0] = GSM1_SOF;
569 cbuf[len + 1] = GSM1_SOF;
570 /* FIXME: we can omit the lead one in many cases */
571 len += 2;
572 break;
573 default:
574 WARN_ON(1);
575 return;
576 }
577 gsm->output(gsm, cbuf, len);
578 gsm_print_packet("-->", addr, cr, control, NULL, 0);
579}
580
581/**
582 * gsm_response - send a control response
583 * @gsm: our GSM mux
584 * @addr: address for control frame
585 * @control: control byte including PF bit
586 *
587 * Format up and transmit a link level response frame.
588 */
589
590static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
591{
592 gsm_send(gsm, addr, 0, control);
593}
594
595/**
596 * gsm_command - send a control command
597 * @gsm: our GSM mux
598 * @addr: address for control frame
599 * @control: control byte including PF bit
600 *
601 * Format up and transmit a link level command frame.
602 */
603
604static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
605{
606 gsm_send(gsm, addr, 1, control);
607}
608
609/* Data transmission */
610
611#define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
612
613/**
614 * gsm_data_alloc - allocate data frame
615 * @gsm: GSM mux
616 * @addr: DLCI address
617 * @len: length excluding header and FCS
618 * @ctrl: control byte
619 *
620 * Allocate a new data buffer for sending frames with data. Space is left
621 * at the front for header bytes but that is treated as an implementation
622 * detail and not for the high level code to use
623 */
624
625static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
626 u8 ctrl)
627{
628 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
629 GFP_ATOMIC);
630 if (m == NULL)
631 return NULL;
632 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
633 m->len = len;
634 m->addr = addr;
635 m->ctrl = ctrl;
636 m->next = NULL;
637 return m;
638}
639
640/**
641 * gsm_data_kick - poke the queue
642 * @gsm: GSM Mux
643 *
644 * The tty device has called us to indicate that room has appeared in
645 * the transmit queue. Ram more data into the pipe if we have any
646 *
647 * FIXME: lock against link layer control transmissions
648 */
649
650static void gsm_data_kick(struct gsm_mux *gsm)
651{
652 struct gsm_msg *msg = gsm->tx_head;
653 int len;
654 int skip_sof = 0;
655
656 /* FIXME: We need to apply this solely to data messages */
657 if (gsm->constipated)
658 return;
659
660 while (gsm->tx_head != NULL) {
661 msg = gsm->tx_head;
662 if (gsm->encoding != 0) {
663 gsm->txframe[0] = GSM1_SOF;
664 len = gsm_stuff_frame(msg->data,
665 gsm->txframe + 1, msg->len);
666 gsm->txframe[len + 1] = GSM1_SOF;
667 len += 2;
668 } else {
669 gsm->txframe[0] = GSM0_SOF;
670 memcpy(gsm->txframe + 1 , msg->data, msg->len);
671 gsm->txframe[msg->len + 1] = GSM0_SOF;
672 len = msg->len + 2;
673 }
674
675 if (debug & 4)
676 print_hex_dump_bytes("gsm_data_kick: ",
677 DUMP_PREFIX_OFFSET,
678 gsm->txframe, len);
679
680 if (gsm->output(gsm, gsm->txframe + skip_sof,
681 len - skip_sof) < 0)
682 break;
683 /* FIXME: Can eliminate one SOF in many more cases */
684 gsm->tx_head = msg->next;
685 if (gsm->tx_head == NULL)
686 gsm->tx_tail = NULL;
687 gsm->tx_bytes -= msg->len;
688 kfree(msg);
689 /* For a burst of frames skip the extra SOF within the
690 burst */
691 skip_sof = 1;
692 }
693}
694
695/**
696 * __gsm_data_queue - queue a UI or UIH frame
697 * @dlci: DLCI sending the data
698 * @msg: message queued
699 *
700 * Add data to the transmit queue and try and get stuff moving
701 * out of the mux tty if not already doing so. The Caller must hold
702 * the gsm tx lock.
703 */
704
705static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
706{
707 struct gsm_mux *gsm = dlci->gsm;
708 u8 *dp = msg->data;
709 u8 *fcs = dp + msg->len;
710
711 /* Fill in the header */
712 if (gsm->encoding == 0) {
713 if (msg->len < 128)
714 *--dp = (msg->len << 1) | EA;
715 else {
716 *--dp = (msg->len >> 7); /* bits 7 - 15 */
717 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
718 }
719 }
720
721 *--dp = msg->ctrl;
722 if (gsm->initiator)
723 *--dp = (msg->addr << 2) | 2 | EA;
724 else
725 *--dp = (msg->addr << 2) | EA;
726 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
727 /* Ugly protocol layering violation */
728 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
729 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
730 *fcs = 0xFF - *fcs;
731
732 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
733 msg->data, msg->len);
734
735 /* Move the header back and adjust the length, also allow for the FCS
736 now tacked on the end */
737 msg->len += (msg->data - dp) + 1;
738 msg->data = dp;
739
740 /* Add to the actual output queue */
741 if (gsm->tx_tail)
742 gsm->tx_tail->next = msg;
743 else
744 gsm->tx_head = msg;
745 gsm->tx_tail = msg;
746 gsm->tx_bytes += msg->len;
747 gsm_data_kick(gsm);
748}
749
750/**
751 * gsm_data_queue - queue a UI or UIH frame
752 * @dlci: DLCI sending the data
753 * @msg: message queued
754 *
755 * Add data to the transmit queue and try and get stuff moving
756 * out of the mux tty if not already doing so. Take the
757 * the gsm tx lock and dlci lock.
758 */
759
760static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
761{
762 unsigned long flags;
763 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
764 __gsm_data_queue(dlci, msg);
765 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
766}
767
768/**
769 * gsm_dlci_data_output - try and push data out of a DLCI
770 * @gsm: mux
771 * @dlci: the DLCI to pull data from
772 *
773 * Pull data from a DLCI and send it into the transmit queue if there
774 * is data. Keep to the MRU of the mux. This path handles the usual tty
775 * interface which is a byte stream with optional modem data.
776 *
777 * Caller must hold the tx_lock of the mux.
778 */
779
780static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
781{
782 struct gsm_msg *msg;
783 u8 *dp;
784 int len, size;
785 int h = dlci->adaption - 1;
786
787 len = kfifo_len(dlci->fifo);
788 if (len == 0)
789 return 0;
790
791 /* MTU/MRU count only the data bits */
792 if (len > gsm->mtu)
793 len = gsm->mtu;
794
795 size = len + h;
796
797 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
798 /* FIXME: need a timer or something to kick this so it can't
799 get stuck with no work outstanding and no buffer free */
800 if (msg == NULL)
801 return -ENOMEM;
802 dp = msg->data;
803 switch (dlci->adaption) {
804 case 1: /* Unstructured */
805 break;
806 case 2: /* Unstructed with modem bits. Always one byte as we never
807 send inline break data */
808 *dp += gsm_encode_modem(dlci);
809 len--;
810 break;
811 }
812 WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len);
813 __gsm_data_queue(dlci, msg);
814 /* Bytes of data we used up */
815 return size;
816}
817
818/**
819 * gsm_dlci_data_output_framed - try and push data out of a DLCI
820 * @gsm: mux
821 * @dlci: the DLCI to pull data from
822 *
823 * Pull data from a DLCI and send it into the transmit queue if there
824 * is data. Keep to the MRU of the mux. This path handles framed data
825 * queued as skbuffs to the DLCI.
826 *
827 * Caller must hold the tx_lock of the mux.
828 */
829
830static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
831 struct gsm_dlci *dlci)
832{
833 struct gsm_msg *msg;
834 u8 *dp;
835 int len, size;
836 int last = 0, first = 0;
837 int overhead = 0;
838
839 /* One byte per frame is used for B/F flags */
840 if (dlci->adaption == 4)
841 overhead = 1;
842
843 /* dlci->skb is locked by tx_lock */
844 if (dlci->skb == NULL) {
845 dlci->skb = skb_dequeue(&dlci->skb_list);
846 if (dlci->skb == NULL)
847 return 0;
848 first = 1;
849 }
850 len = dlci->skb->len + overhead;
851
852 /* MTU/MRU count only the data bits */
853 if (len > gsm->mtu) {
854 if (dlci->adaption == 3) {
855 /* Over long frame, bin it */
856 kfree_skb(dlci->skb);
857 dlci->skb = NULL;
858 return 0;
859 }
860 len = gsm->mtu;
861 } else
862 last = 1;
863
864 size = len + overhead;
865 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
866
867 /* FIXME: need a timer or something to kick this so it can't
868 get stuck with no work outstanding and no buffer free */
869 if (msg == NULL)
870 return -ENOMEM;
871 dp = msg->data;
872
873 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
874 /* Flag byte to carry the start/end info */
875 *dp++ = last << 7 | first << 6 | 1; /* EA */
876 len--;
877 }
878 memcpy(dp, dlci->skb->data, len);
879 skb_pull(dlci->skb, len);
880 __gsm_data_queue(dlci, msg);
881 if (last)
882 dlci->skb = NULL;
883 return size;
884}
885
886/**
887 * gsm_dlci_data_sweep - look for data to send
888 * @gsm: the GSM mux
889 *
890 * Sweep the GSM mux channels in priority order looking for ones with
891 * data to send. We could do with optimising this scan a bit. We aim
892 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
893 * TX_THRESH_LO we get called again
894 *
895 * FIXME: We should round robin between groups and in theory you can
896 * renegotiate DLCI priorities with optional stuff. Needs optimising.
897 */
898
899static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
900{
901 int len;
902 /* Priority ordering: We should do priority with RR of the groups */
903 int i = 1;
904
905 while (i < NUM_DLCI) {
906 struct gsm_dlci *dlci;
907
908 if (gsm->tx_bytes > TX_THRESH_HI)
909 break;
910 dlci = gsm->dlci[i];
911 if (dlci == NULL || dlci->constipated) {
912 i++;
913 continue;
914 }
915 if (dlci->adaption < 3)
916 len = gsm_dlci_data_output(gsm, dlci);
917 else
918 len = gsm_dlci_data_output_framed(gsm, dlci);
919 if (len < 0)
920 break;
921 /* DLCI empty - try the next */
922 if (len == 0)
923 i++;
924 }
925}
926
927/**
928 * gsm_dlci_data_kick - transmit if possible
929 * @dlci: DLCI to kick
930 *
931 * Transmit data from this DLCI if the queue is empty. We can't rely on
932 * a tty wakeup except when we filled the pipe so we need to fire off
933 * new data ourselves in other cases.
934 */
935
936static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
937{
938 unsigned long flags;
939
940 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
941 /* If we have nothing running then we need to fire up */
942 if (dlci->gsm->tx_bytes == 0)
943 gsm_dlci_data_output(dlci->gsm, dlci);
944 else if (dlci->gsm->tx_bytes < TX_THRESH_LO)
945 gsm_dlci_data_sweep(dlci->gsm);
946 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
947}
948
949/*
950 * Control message processing
951 */
952
953
954/**
955 * gsm_control_reply - send a response frame to a control
956 * @gsm: gsm channel
957 * @cmd: the command to use
958 * @data: data to follow encoded info
959 * @dlen: length of data
960 *
961 * Encode up and queue a UI/UIH frame containing our response.
962 */
963
964static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
965 int dlen)
966{
967 struct gsm_msg *msg;
968 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
969 if (msg == NULL)
970 return;
971 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
972 msg->data[1] = (dlen << 1) | EA;
973 memcpy(msg->data + 2, data, dlen);
974 gsm_data_queue(gsm->dlci[0], msg);
975}
976
977/**
978 * gsm_process_modem - process received modem status
979 * @tty: virtual tty bound to the DLCI
980 * @dlci: DLCI to affect
981 * @modem: modem bits (full EA)
982 *
983 * Used when a modem control message or line state inline in adaption
984 * layer 2 is processed. Sort out the local modem state and throttles
985 */
986
987static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
988 u32 modem, int clen)
989{
990 int mlines = 0;
991 u8 brk = 0;
992
993 /* The modem status command can either contain one octet (v.24 signals)
994 or two octets (v.24 signals + break signals). The length field will
995 either be 2 or 3 respectively. This is specified in section
996 5.4.6.3.7 of the 27.010 mux spec. */
997
998 if (clen == 2)
999 modem = modem & 0x7f;
1000 else {
1001 brk = modem & 0x7f;
1002 modem = (modem >> 7) & 0x7f;
1003 };
1004
1005 /* Flow control/ready to communicate */
1006 if (modem & MDM_FC) {
1007 /* Need to throttle our output on this device */
1008 dlci->constipated = 1;
1009 }
1010 if (modem & MDM_RTC) {
1011 mlines |= TIOCM_DSR | TIOCM_DTR;
1012 dlci->constipated = 0;
1013 gsm_dlci_data_kick(dlci);
1014 }
1015 /* Map modem bits */
1016 if (modem & MDM_RTR)
1017 mlines |= TIOCM_RTS | TIOCM_CTS;
1018 if (modem & MDM_IC)
1019 mlines |= TIOCM_RI;
1020 if (modem & MDM_DV)
1021 mlines |= TIOCM_CD;
1022
1023 /* Carrier drop -> hangup */
1024 if (tty) {
1025 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
1026 if (!(tty->termios->c_cflag & CLOCAL))
1027 tty_hangup(tty);
1028 if (brk & 0x01)
1029 tty_insert_flip_char(tty, 0, TTY_BREAK);
1030 }
1031 dlci->modem_rx = mlines;
1032}
1033
1034/**
1035 * gsm_control_modem - modem status received
1036 * @gsm: GSM channel
1037 * @data: data following command
1038 * @clen: command length
1039 *
1040 * We have received a modem status control message. This is used by
1041 * the GSM mux protocol to pass virtual modem line status and optionally
1042 * to indicate break signals. Unpack it, convert to Linux representation
1043 * and if need be stuff a break message down the tty.
1044 */
1045
1046static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1047{
1048 unsigned int addr = 0;
1049 unsigned int modem = 0;
1050 struct gsm_dlci *dlci;
1051 int len = clen;
1052 u8 *dp = data;
1053 struct tty_struct *tty;
1054
1055 while (gsm_read_ea(&addr, *dp++) == 0) {
1056 len--;
1057 if (len == 0)
1058 return;
1059 }
1060 /* Must be at least one byte following the EA */
1061 len--;
1062 if (len <= 0)
1063 return;
1064
1065 addr >>= 1;
1066 /* Closed port, or invalid ? */
1067 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1068 return;
1069 dlci = gsm->dlci[addr];
1070
1071 while (gsm_read_ea(&modem, *dp++) == 0) {
1072 len--;
1073 if (len == 0)
1074 return;
1075 }
1076 tty = tty_port_tty_get(&dlci->port);
1077 gsm_process_modem(tty, dlci, modem, clen);
1078 if (tty) {
1079 tty_wakeup(tty);
1080 tty_kref_put(tty);
1081 }
1082 gsm_control_reply(gsm, CMD_MSC, data, clen);
1083}
1084
1085/**
1086 * gsm_control_rls - remote line status
1087 * @gsm: GSM channel
1088 * @data: data bytes
1089 * @clen: data length
1090 *
1091 * The modem sends us a two byte message on the control channel whenever
1092 * it wishes to send us an error state from the virtual link. Stuff
1093 * this into the uplink tty if present
1094 */
1095
1096static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1097{
1098 struct tty_struct *tty;
1099 unsigned int addr = 0 ;
1100 u8 bits;
1101 int len = clen;
1102 u8 *dp = data;
1103
1104 while (gsm_read_ea(&addr, *dp++) == 0) {
1105 len--;
1106 if (len == 0)
1107 return;
1108 }
1109 /* Must be at least one byte following ea */
1110 len--;
1111 if (len <= 0)
1112 return;
1113 addr >>= 1;
1114 /* Closed port, or invalid ? */
1115 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1116 return;
1117 /* No error ? */
1118 bits = *dp;
1119 if ((bits & 1) == 0)
1120 return;
1121 /* See if we have an uplink tty */
1122 tty = tty_port_tty_get(&gsm->dlci[addr]->port);
1123
1124 if (tty) {
1125 if (bits & 2)
1126 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1127 if (bits & 4)
1128 tty_insert_flip_char(tty, 0, TTY_PARITY);
1129 if (bits & 8)
1130 tty_insert_flip_char(tty, 0, TTY_FRAME);
1131 tty_flip_buffer_push(tty);
1132 tty_kref_put(tty);
1133 }
1134 gsm_control_reply(gsm, CMD_RLS, data, clen);
1135}
1136
1137static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1138
1139/**
1140 * gsm_control_message - DLCI 0 control processing
1141 * @gsm: our GSM mux
1142 * @command: the command EA
1143 * @data: data beyond the command/length EAs
1144 * @clen: length
1145 *
1146 * Input processor for control messages from the other end of the link.
1147 * Processes the incoming request and queues a response frame or an
1148 * NSC response if not supported
1149 */
1150
1151static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1152 u8 *data, int clen)
1153{
1154 u8 buf[1];
1155 switch (command) {
1156 case CMD_CLD: {
1157 struct gsm_dlci *dlci = gsm->dlci[0];
1158 /* Modem wishes to close down */
1159 if (dlci) {
1160 dlci->dead = 1;
1161 gsm->dead = 1;
1162 gsm_dlci_begin_close(dlci);
1163 }
1164 }
1165 break;
1166 case CMD_TEST:
1167 /* Modem wishes to test, reply with the data */
1168 gsm_control_reply(gsm, CMD_TEST, data, clen);
1169 break;
1170 case CMD_FCON:
1171 /* Modem wants us to STFU */
1172 gsm->constipated = 1;
1173 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
1174 break;
1175 case CMD_FCOFF:
1176 /* Modem can accept data again */
1177 gsm->constipated = 0;
1178 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1179 /* Kick the link in case it is idling */
1180 gsm_data_kick(gsm);
1181 break;
1182 case CMD_MSC:
1183 /* Out of band modem line change indicator for a DLCI */
1184 gsm_control_modem(gsm, data, clen);
1185 break;
1186 case CMD_RLS:
1187 /* Out of band error reception for a DLCI */
1188 gsm_control_rls(gsm, data, clen);
1189 break;
1190 case CMD_PSC:
1191 /* Modem wishes to enter power saving state */
1192 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1193 break;
1194 /* Optional unsupported commands */
1195 case CMD_PN: /* Parameter negotiation */
1196 case CMD_RPN: /* Remote port negotiation */
1197 case CMD_SNC: /* Service negotiation command */
1198 default:
1199 /* Reply to bad commands with an NSC */
1200 buf[0] = command;
1201 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1202 break;
1203 }
1204}
1205
1206/**
1207 * gsm_control_response - process a response to our control
1208 * @gsm: our GSM mux
1209 * @command: the command (response) EA
1210 * @data: data beyond the command/length EA
1211 * @clen: length
1212 *
1213 * Process a response to an outstanding command. We only allow a single
1214 * control message in flight so this is fairly easy. All the clean up
1215 * is done by the caller, we just update the fields, flag it as done
1216 * and return
1217 */
1218
1219static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1220 u8 *data, int clen)
1221{
1222 struct gsm_control *ctrl;
1223 unsigned long flags;
1224
1225 spin_lock_irqsave(&gsm->control_lock, flags);
1226
1227 ctrl = gsm->pending_cmd;
1228 /* Does the reply match our command */
1229 command |= 1;
1230 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1231 /* Our command was replied to, kill the retry timer */
1232 del_timer(&gsm->t2_timer);
1233 gsm->pending_cmd = NULL;
1234 /* Rejected by the other end */
1235 if (command == CMD_NSC)
1236 ctrl->error = -EOPNOTSUPP;
1237 ctrl->done = 1;
1238 wake_up(&gsm->event);
1239 }
1240 spin_unlock_irqrestore(&gsm->control_lock, flags);
1241}
1242
1243/**
1244 * gsm_control_transmit - send control packet
1245 * @gsm: gsm mux
1246 * @ctrl: frame to send
1247 *
1248 * Send out a pending control command (called under control lock)
1249 */
1250
1251static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1252{
1253 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
1254 if (msg == NULL)
1255 return;
1256 msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1257 memcpy(msg->data + 1, ctrl->data, ctrl->len);
1258 gsm_data_queue(gsm->dlci[0], msg);
1259}
1260
1261/**
1262 * gsm_control_retransmit - retransmit a control frame
1263 * @data: pointer to our gsm object
1264 *
1265 * Called off the T2 timer expiry in order to retransmit control frames
1266 * that have been lost in the system somewhere. The control_lock protects
1267 * us from colliding with another sender or a receive completion event.
1268 * In that situation the timer may still occur in a small window but
1269 * gsm->pending_cmd will be NULL and we just let the timer expire.
1270 */
1271
1272static void gsm_control_retransmit(unsigned long data)
1273{
1274 struct gsm_mux *gsm = (struct gsm_mux *)data;
1275 struct gsm_control *ctrl;
1276 unsigned long flags;
1277 spin_lock_irqsave(&gsm->control_lock, flags);
1278 ctrl = gsm->pending_cmd;
1279 if (ctrl) {
1280 gsm->cretries--;
1281 if (gsm->cretries == 0) {
1282 gsm->pending_cmd = NULL;
1283 ctrl->error = -ETIMEDOUT;
1284 ctrl->done = 1;
1285 spin_unlock_irqrestore(&gsm->control_lock, flags);
1286 wake_up(&gsm->event);
1287 return;
1288 }
1289 gsm_control_transmit(gsm, ctrl);
1290 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1291 }
1292 spin_unlock_irqrestore(&gsm->control_lock, flags);
1293}
1294
1295/**
1296 * gsm_control_send - send a control frame on DLCI 0
1297 * @gsm: the GSM channel
1298 * @command: command to send including CR bit
1299 * @data: bytes of data (must be kmalloced)
1300 * @len: length of the block to send
1301 *
1302 * Queue and dispatch a control command. Only one command can be
1303 * active at a time. In theory more can be outstanding but the matching
1304 * gets really complicated so for now stick to one outstanding.
1305 */
1306
1307static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1308 unsigned int command, u8 *data, int clen)
1309{
1310 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1311 GFP_KERNEL);
1312 unsigned long flags;
1313 if (ctrl == NULL)
1314 return NULL;
1315retry:
1316 wait_event(gsm->event, gsm->pending_cmd == NULL);
1317 spin_lock_irqsave(&gsm->control_lock, flags);
1318 if (gsm->pending_cmd != NULL) {
1319 spin_unlock_irqrestore(&gsm->control_lock, flags);
1320 goto retry;
1321 }
1322 ctrl->cmd = command;
1323 ctrl->data = data;
1324 ctrl->len = clen;
1325 gsm->pending_cmd = ctrl;
1326 gsm->cretries = gsm->n2;
1327 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1328 gsm_control_transmit(gsm, ctrl);
1329 spin_unlock_irqrestore(&gsm->control_lock, flags);
1330 return ctrl;
1331}
1332
1333/**
1334 * gsm_control_wait - wait for a control to finish
1335 * @gsm: GSM mux
1336 * @control: control we are waiting on
1337 *
1338 * Waits for the control to complete or time out. Frees any used
1339 * resources and returns 0 for success, or an error if the remote
1340 * rejected or ignored the request.
1341 */
1342
1343static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1344{
1345 int err;
1346 wait_event(gsm->event, control->done == 1);
1347 err = control->error;
1348 kfree(control);
1349 return err;
1350}
1351
1352
1353/*
1354 * DLCI level handling: Needs krefs
1355 */
1356
1357/*
1358 * State transitions and timers
1359 */
1360
1361/**
1362 * gsm_dlci_close - a DLCI has closed
1363 * @dlci: DLCI that closed
1364 *
1365 * Perform processing when moving a DLCI into closed state. If there
1366 * is an attached tty this is hung up
1367 */
1368
1369static void gsm_dlci_close(struct gsm_dlci *dlci)
1370{
1371 del_timer(&dlci->t1);
1372 if (debug & 8)
1373 pr_debug("DLCI %d goes closed.\n", dlci->addr);
1374 dlci->state = DLCI_CLOSED;
1375 if (dlci->addr != 0) {
1376 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1377 if (tty) {
1378 tty_hangup(tty);
1379 tty_kref_put(tty);
1380 }
1381 kfifo_reset(dlci->fifo);
1382 } else
1383 dlci->gsm->dead = 1;
1384 wake_up(&dlci->gsm->event);
1385 /* A DLCI 0 close is a MUX termination so we need to kick that
1386 back to userspace somehow */
1387}
1388
1389/**
1390 * gsm_dlci_open - a DLCI has opened
1391 * @dlci: DLCI that opened
1392 *
1393 * Perform processing when moving a DLCI into open state.
1394 */
1395
1396static void gsm_dlci_open(struct gsm_dlci *dlci)
1397{
1398 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
1399 open -> open */
1400 del_timer(&dlci->t1);
1401 /* This will let a tty open continue */
1402 dlci->state = DLCI_OPEN;
1403 if (debug & 8)
1404 pr_debug("DLCI %d goes open.\n", dlci->addr);
1405 wake_up(&dlci->gsm->event);
1406}
1407
1408/**
1409 * gsm_dlci_t1 - T1 timer expiry
1410 * @dlci: DLCI that opened
1411 *
1412 * The T1 timer handles retransmits of control frames (essentially of
1413 * SABM and DISC). We resend the command until the retry count runs out
1414 * in which case an opening port goes back to closed and a closing port
1415 * is simply put into closed state (any further frames from the other
1416 * end will get a DM response)
1417 */
1418
1419static void gsm_dlci_t1(unsigned long data)
1420{
1421 struct gsm_dlci *dlci = (struct gsm_dlci *)data;
1422 struct gsm_mux *gsm = dlci->gsm;
1423
1424 switch (dlci->state) {
1425 case DLCI_OPENING:
1426 dlci->retries--;
1427 if (dlci->retries) {
1428 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1429 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1430 } else
1431 gsm_dlci_close(dlci);
1432 break;
1433 case DLCI_CLOSING:
1434 dlci->retries--;
1435 if (dlci->retries) {
1436 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1437 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1438 } else
1439 gsm_dlci_close(dlci);
1440 break;
1441 }
1442}
1443
1444/**
1445 * gsm_dlci_begin_open - start channel open procedure
1446 * @dlci: DLCI to open
1447 *
1448 * Commence opening a DLCI from the Linux side. We issue SABM messages
1449 * to the modem which should then reply with a UA, at which point we
1450 * will move into open state. Opening is done asynchronously with retry
1451 * running off timers and the responses.
1452 */
1453
1454static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1455{
1456 struct gsm_mux *gsm = dlci->gsm;
1457 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1458 return;
1459 dlci->retries = gsm->n2;
1460 dlci->state = DLCI_OPENING;
1461 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1462 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1463}
1464
1465/**
1466 * gsm_dlci_begin_close - start channel open procedure
1467 * @dlci: DLCI to open
1468 *
1469 * Commence closing a DLCI from the Linux side. We issue DISC messages
1470 * to the modem which should then reply with a UA, at which point we
1471 * will move into closed state. Closing is done asynchronously with retry
1472 * off timers. We may also receive a DM reply from the other end which
1473 * indicates the channel was already closed.
1474 */
1475
1476static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1477{
1478 struct gsm_mux *gsm = dlci->gsm;
1479 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1480 return;
1481 dlci->retries = gsm->n2;
1482 dlci->state = DLCI_CLOSING;
1483 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1484 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1485}
1486
1487/**
1488 * gsm_dlci_data - data arrived
1489 * @dlci: channel
1490 * @data: block of bytes received
1491 * @len: length of received block
1492 *
1493 * A UI or UIH frame has arrived which contains data for a channel
1494 * other than the control channel. If the relevant virtual tty is
1495 * open we shovel the bits down it, if not we drop them.
1496 */
1497
1498static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen)
1499{
1500 /* krefs .. */
1501 struct tty_port *port = &dlci->port;
1502 struct tty_struct *tty = tty_port_tty_get(port);
1503 unsigned int modem = 0;
1504 int len = clen;
1505
1506 if (debug & 16)
1507 pr_debug("%d bytes for tty %p\n", len, tty);
1508 if (tty) {
1509 switch (dlci->adaption) {
1510 /* Unsupported types */
1511 /* Packetised interruptible data */
1512 case 4:
1513 break;
1514 /* Packetised uininterruptible voice/data */
1515 case 3:
1516 break;
1517 /* Asynchronous serial with line state in each frame */
1518 case 2:
1519 while (gsm_read_ea(&modem, *data++) == 0) {
1520 len--;
1521 if (len == 0)
1522 return;
1523 }
1524 gsm_process_modem(tty, dlci, modem, clen);
1525 /* Line state will go via DLCI 0 controls only */
1526 case 1:
1527 default:
1528 tty_insert_flip_string(tty, data, len);
1529 tty_flip_buffer_push(tty);
1530 }
1531 tty_kref_put(tty);
1532 }
1533}
1534
1535/**
1536 * gsm_dlci_control - data arrived on control channel
1537 * @dlci: channel
1538 * @data: block of bytes received
1539 * @len: length of received block
1540 *
1541 * A UI or UIH frame has arrived which contains data for DLCI 0 the
1542 * control channel. This should contain a command EA followed by
1543 * control data bytes. The command EA contains a command/response bit
1544 * and we divide up the work accordingly.
1545 */
1546
1547static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
1548{
1549 /* See what command is involved */
1550 unsigned int command = 0;
1551 while (len-- > 0) {
1552 if (gsm_read_ea(&command, *data++) == 1) {
1553 int clen = *data++;
1554 len--;
1555 /* FIXME: this is properly an EA */
1556 clen >>= 1;
1557 /* Malformed command ? */
1558 if (clen > len)
1559 return;
1560 if (command & 1)
1561 gsm_control_message(dlci->gsm, command,
1562 data, clen);
1563 else
1564 gsm_control_response(dlci->gsm, command,
1565 data, clen);
1566 return;
1567 }
1568 }
1569}
1570
1571/*
1572 * Allocate/Free DLCI channels
1573 */
1574
1575/**
1576 * gsm_dlci_alloc - allocate a DLCI
1577 * @gsm: GSM mux
1578 * @addr: address of the DLCI
1579 *
1580 * Allocate and install a new DLCI object into the GSM mux.
1581 *
1582 * FIXME: review locking races
1583 */
1584
1585static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
1586{
1587 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
1588 if (dlci == NULL)
1589 return NULL;
1590 spin_lock_init(&dlci->lock);
1591 dlci->fifo = &dlci->_fifo;
1592 if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
1593 kfree(dlci);
1594 return NULL;
1595 }
1596
1597 skb_queue_head_init(&dlci->skb_list);
1598 init_timer(&dlci->t1);
1599 dlci->t1.function = gsm_dlci_t1;
1600 dlci->t1.data = (unsigned long)dlci;
1601 tty_port_init(&dlci->port);
1602 dlci->port.ops = &gsm_port_ops;
1603 dlci->gsm = gsm;
1604 dlci->addr = addr;
1605 dlci->adaption = gsm->adaption;
1606 dlci->state = DLCI_CLOSED;
1607 if (addr)
1608 dlci->data = gsm_dlci_data;
1609 else
1610 dlci->data = gsm_dlci_command;
1611 gsm->dlci[addr] = dlci;
1612 return dlci;
1613}
1614
1615/**
1616 * gsm_dlci_free - release DLCI
1617 * @dlci: DLCI to destroy
1618 *
1619 * Free up a DLCI. Currently to keep the lifetime rules sane we only
1620 * clean up DLCI objects when the MUX closes rather than as the port
1621 * is closed down on both the tty and mux levels.
1622 *
1623 * Can sleep.
1624 */
1625static void gsm_dlci_free(struct gsm_dlci *dlci)
1626{
1627 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1628 if (tty) {
1629 tty_vhangup(tty);
1630 tty_kref_put(tty);
1631 }
1632 del_timer_sync(&dlci->t1);
1633 dlci->gsm->dlci[dlci->addr] = NULL;
1634 kfifo_free(dlci->fifo);
1635 kfree(dlci);
1636}
1637
1638/*
1639 * LAPBish link layer logic
1640 */
1641
1642/**
1643 * gsm_queue - a GSM frame is ready to process
1644 * @gsm: pointer to our gsm mux
1645 *
1646 * At this point in time a frame has arrived and been demangled from
1647 * the line encoding. All the differences between the encodings have
1648 * been handled below us and the frame is unpacked into the structures.
1649 * The fcs holds the header FCS but any data FCS must be added here.
1650 */
1651
1652static void gsm_queue(struct gsm_mux *gsm)
1653{
1654 struct gsm_dlci *dlci;
1655 u8 cr;
1656 int address;
1657 /* We have to sneak a look at the packet body to do the FCS.
1658 A somewhat layering violation in the spec */
1659
1660 if ((gsm->control & ~PF) == UI)
1661 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
1662 if (gsm->encoding == 0){
1663 /* WARNING: gsm->received_fcs is used for gsm->encoding = 0 only.
1664 In this case it contain the last piece of data
1665 required to generate final CRC */
1666 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1667 }
1668 if (gsm->fcs != GOOD_FCS) {
1669 gsm->bad_fcs++;
1670 if (debug & 4)
1671 pr_debug("BAD FCS %02x\n", gsm->fcs);
1672 return;
1673 }
1674 address = gsm->address >> 1;
1675 if (address >= NUM_DLCI)
1676 goto invalid;
1677
1678 cr = gsm->address & 1; /* C/R bit */
1679
1680 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
1681
1682 cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
1683 dlci = gsm->dlci[address];
1684
1685 switch (gsm->control) {
1686 case SABM|PF:
1687 if (cr == 0)
1688 goto invalid;
1689 if (dlci == NULL)
1690 dlci = gsm_dlci_alloc(gsm, address);
1691 if (dlci == NULL)
1692 return;
1693 if (dlci->dead)
1694 gsm_response(gsm, address, DM);
1695 else {
1696 gsm_response(gsm, address, UA);
1697 gsm_dlci_open(dlci);
1698 }
1699 break;
1700 case DISC|PF:
1701 if (cr == 0)
1702 goto invalid;
1703 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
1704 gsm_response(gsm, address, DM);
1705 return;
1706 }
1707 /* Real close complete */
1708 gsm_response(gsm, address, UA);
1709 gsm_dlci_close(dlci);
1710 break;
1711 case UA:
1712 case UA|PF:
1713 if (cr == 0 || dlci == NULL)
1714 break;
1715 switch (dlci->state) {
1716 case DLCI_CLOSING:
1717 gsm_dlci_close(dlci);
1718 break;
1719 case DLCI_OPENING:
1720 gsm_dlci_open(dlci);
1721 break;
1722 }
1723 break;
1724 case DM: /* DM can be valid unsolicited */
1725 case DM|PF:
1726 if (cr)
1727 goto invalid;
1728 if (dlci == NULL)
1729 return;
1730 gsm_dlci_close(dlci);
1731 break;
1732 case UI:
1733 case UI|PF:
1734 case UIH:
1735 case UIH|PF:
1736#if 0
1737 if (cr)
1738 goto invalid;
1739#endif
1740 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1741 gsm_command(gsm, address, DM|PF);
1742 return;
1743 }
1744 dlci->data(dlci, gsm->buf, gsm->len);
1745 break;
1746 default:
1747 goto invalid;
1748 }
1749 return;
1750invalid:
1751 gsm->malformed++;
1752 return;
1753}
1754
1755
1756/**
1757 * gsm0_receive - perform processing for non-transparency
1758 * @gsm: gsm data for this ldisc instance
1759 * @c: character
1760 *
1761 * Receive bytes in gsm mode 0
1762 */
1763
1764static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
1765{
1766 unsigned int len;
1767
1768 switch (gsm->state) {
1769 case GSM_SEARCH: /* SOF marker */
1770 if (c == GSM0_SOF) {
1771 gsm->state = GSM_ADDRESS;
1772 gsm->address = 0;
1773 gsm->len = 0;
1774 gsm->fcs = INIT_FCS;
1775 }
1776 break;
1777 case GSM_ADDRESS: /* Address EA */
1778 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1779 if (gsm_read_ea(&gsm->address, c))
1780 gsm->state = GSM_CONTROL;
1781 break;
1782 case GSM_CONTROL: /* Control Byte */
1783 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1784 gsm->control = c;
1785 gsm->state = GSM_LEN0;
1786 break;
1787 case GSM_LEN0: /* Length EA */
1788 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1789 if (gsm_read_ea(&gsm->len, c)) {
1790 if (gsm->len > gsm->mru) {
1791 gsm->bad_size++;
1792 gsm->state = GSM_SEARCH;
1793 break;
1794 }
1795 gsm->count = 0;
1796 if (!gsm->len)
1797 gsm->state = GSM_FCS;
1798 else
1799 gsm->state = GSM_DATA;
1800 break;
1801 }
1802 gsm->state = GSM_LEN1;
1803 break;
1804 case GSM_LEN1:
1805 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1806 len = c;
1807 gsm->len |= len << 7;
1808 if (gsm->len > gsm->mru) {
1809 gsm->bad_size++;
1810 gsm->state = GSM_SEARCH;
1811 break;
1812 }
1813 gsm->count = 0;
1814 if (!gsm->len)
1815 gsm->state = GSM_FCS;
1816 else
1817 gsm->state = GSM_DATA;
1818 break;
1819 case GSM_DATA: /* Data */
1820 gsm->buf[gsm->count++] = c;
1821 if (gsm->count == gsm->len)
1822 gsm->state = GSM_FCS;
1823 break;
1824 case GSM_FCS: /* FCS follows the packet */
1825 gsm->received_fcs = c;
1826 if (c == GSM0_SOF) {
1827 gsm->state = GSM_SEARCH;
1828 break;
1829 }
1830 gsm_queue(gsm);
1831 gsm->state = GSM_SSOF;
1832 break;
1833 case GSM_SSOF:
1834 if (c == GSM0_SOF) {
1835 gsm->state = GSM_SEARCH;
1836 break;
1837 }
1838 break;
1839 }
1840}
1841
1842/**
1843 * gsm1_receive - perform processing for non-transparency
1844 * @gsm: gsm data for this ldisc instance
1845 * @c: character
1846 *
1847 * Receive bytes in mode 1 (Advanced option)
1848 */
1849
1850static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
1851{
1852 if (c == GSM1_SOF) {
1853 /* EOF is only valid in frame if we have got to the data state
1854 and received at least one byte (the FCS) */
1855 if (gsm->state == GSM_DATA && gsm->count) {
1856 /* Extract the FCS */
1857 gsm->count--;
1858 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
1859 gsm->len = gsm->count;
1860 gsm_queue(gsm);
1861 gsm->state = GSM_START;
1862 return;
1863 }
1864 /* Any partial frame was a runt so go back to start */
1865 if (gsm->state != GSM_START) {
1866 gsm->malformed++;
1867 gsm->state = GSM_START;
1868 }
1869 /* A SOF in GSM_START means we are still reading idling or
1870 framing bytes */
1871 return;
1872 }
1873
1874 if (c == GSM1_ESCAPE) {
1875 gsm->escape = 1;
1876 return;
1877 }
1878
1879 /* Only an unescaped SOF gets us out of GSM search */
1880 if (gsm->state == GSM_SEARCH)
1881 return;
1882
1883 if (gsm->escape) {
1884 c ^= GSM1_ESCAPE_BITS;
1885 gsm->escape = 0;
1886 }
1887 switch (gsm->state) {
1888 case GSM_START: /* First byte after SOF */
1889 gsm->address = 0;
1890 gsm->state = GSM_ADDRESS;
1891 gsm->fcs = INIT_FCS;
1892 /* Drop through */
1893 case GSM_ADDRESS: /* Address continuation */
1894 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1895 if (gsm_read_ea(&gsm->address, c))
1896 gsm->state = GSM_CONTROL;
1897 break;
1898 case GSM_CONTROL: /* Control Byte */
1899 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1900 gsm->control = c;
1901 gsm->count = 0;
1902 gsm->state = GSM_DATA;
1903 break;
1904 case GSM_DATA: /* Data */
1905 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
1906 gsm->state = GSM_OVERRUN;
1907 gsm->bad_size++;
1908 } else
1909 gsm->buf[gsm->count++] = c;
1910 break;
1911 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
1912 break;
1913 }
1914}
1915
1916/**
1917 * gsm_error - handle tty error
1918 * @gsm: ldisc data
1919 * @data: byte received (may be invalid)
1920 * @flag: error received
1921 *
1922 * Handle an error in the receipt of data for a frame. Currently we just
1923 * go back to hunting for a SOF.
1924 *
1925 * FIXME: better diagnostics ?
1926 */
1927
1928static void gsm_error(struct gsm_mux *gsm,
1929 unsigned char data, unsigned char flag)
1930{
1931 gsm->state = GSM_SEARCH;
1932 gsm->io_error++;
1933}
1934
1935/**
1936 * gsm_cleanup_mux - generic GSM protocol cleanup
1937 * @gsm: our mux
1938 *
1939 * Clean up the bits of the mux which are the same for all framing
1940 * protocols. Remove the mux from the mux table, stop all the timers
1941 * and then shut down each device hanging up the channels as we go.
1942 */
1943
1944void gsm_cleanup_mux(struct gsm_mux *gsm)
1945{
1946 int i;
1947 struct gsm_dlci *dlci = gsm->dlci[0];
1948 struct gsm_msg *txq;
1949
1950 gsm->dead = 1;
1951
1952 spin_lock(&gsm_mux_lock);
1953 for (i = 0; i < MAX_MUX; i++) {
1954 if (gsm_mux[i] == gsm) {
1955 gsm_mux[i] = NULL;
1956 break;
1957 }
1958 }
1959 spin_unlock(&gsm_mux_lock);
1960 WARN_ON(i == MAX_MUX);
1961
1962 del_timer_sync(&gsm->t2_timer);
1963 /* Now we are sure T2 has stopped */
1964 if (dlci) {
1965 dlci->dead = 1;
1966 gsm_dlci_begin_close(dlci);
1967 wait_event_interruptible(gsm->event,
1968 dlci->state == DLCI_CLOSED);
1969 }
1970 /* Free up any link layer users */
1971 for (i = 0; i < NUM_DLCI; i++)
1972 if (gsm->dlci[i])
1973 gsm_dlci_free(gsm->dlci[i]);
1974 /* Now wipe the queues */
1975 for (txq = gsm->tx_head; txq != NULL; txq = gsm->tx_head) {
1976 gsm->tx_head = txq->next;
1977 kfree(txq);
1978 }
1979 gsm->tx_tail = NULL;
1980}
1981EXPORT_SYMBOL_GPL(gsm_cleanup_mux);
1982
1983/**
1984 * gsm_activate_mux - generic GSM setup
1985 * @gsm: our mux
1986 *
1987 * Set up the bits of the mux which are the same for all framing
1988 * protocols. Add the mux to the mux table so it can be opened and
1989 * finally kick off connecting to DLCI 0 on the modem.
1990 */
1991
1992int gsm_activate_mux(struct gsm_mux *gsm)
1993{
1994 struct gsm_dlci *dlci;
1995 int i = 0;
1996
1997 init_timer(&gsm->t2_timer);
1998 gsm->t2_timer.function = gsm_control_retransmit;
1999 gsm->t2_timer.data = (unsigned long)gsm;
2000 init_waitqueue_head(&gsm->event);
2001 spin_lock_init(&gsm->control_lock);
2002 spin_lock_init(&gsm->tx_lock);
2003
2004 if (gsm->encoding == 0)
2005 gsm->receive = gsm0_receive;
2006 else
2007 gsm->receive = gsm1_receive;
2008 gsm->error = gsm_error;
2009
2010 spin_lock(&gsm_mux_lock);
2011 for (i = 0; i < MAX_MUX; i++) {
2012 if (gsm_mux[i] == NULL) {
2013 gsm_mux[i] = gsm;
2014 break;
2015 }
2016 }
2017 spin_unlock(&gsm_mux_lock);
2018 if (i == MAX_MUX)
2019 return -EBUSY;
2020
2021 dlci = gsm_dlci_alloc(gsm, 0);
2022 if (dlci == NULL)
2023 return -ENOMEM;
2024 gsm->dead = 0; /* Tty opens are now permissible */
2025 return 0;
2026}
2027EXPORT_SYMBOL_GPL(gsm_activate_mux);
2028
2029/**
2030 * gsm_free_mux - free up a mux
2031 * @mux: mux to free
2032 *
2033 * Dispose of allocated resources for a dead mux. No refcounting
2034 * at present so the mux must be truly dead.
2035 */
2036void gsm_free_mux(struct gsm_mux *gsm)
2037{
2038 kfree(gsm->txframe);
2039 kfree(gsm->buf);
2040 kfree(gsm);
2041}
2042EXPORT_SYMBOL_GPL(gsm_free_mux);
2043
2044/**
2045 * gsm_alloc_mux - allocate a mux
2046 *
2047 * Creates a new mux ready for activation.
2048 */
2049
2050struct gsm_mux *gsm_alloc_mux(void)
2051{
2052 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2053 if (gsm == NULL)
2054 return NULL;
2055 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2056 if (gsm->buf == NULL) {
2057 kfree(gsm);
2058 return NULL;
2059 }
2060 gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2061 if (gsm->txframe == NULL) {
2062 kfree(gsm->buf);
2063 kfree(gsm);
2064 return NULL;
2065 }
2066 spin_lock_init(&gsm->lock);
2067
2068 gsm->t1 = T1;
2069 gsm->t2 = T2;
2070 gsm->n2 = N2;
2071 gsm->ftype = UIH;
2072 gsm->initiator = 0;
2073 gsm->adaption = 1;
2074 gsm->encoding = 1;
2075 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
2076 gsm->mtu = 64;
2077 gsm->dead = 1; /* Avoid early tty opens */
2078
2079 return gsm;
2080}
2081EXPORT_SYMBOL_GPL(gsm_alloc_mux);
2082
2083/**
2084 * gsmld_output - write to link
2085 * @gsm: our mux
2086 * @data: bytes to output
2087 * @len: size
2088 *
2089 * Write a block of data from the GSM mux to the data channel. This
2090 * will eventually be serialized from above but at the moment isn't.
2091 */
2092
2093static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2094{
2095 if (tty_write_room(gsm->tty) < len) {
2096 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2097 return -ENOSPC;
2098 }
2099 if (debug & 4)
2100 print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET,
2101 data, len);
2102 gsm->tty->ops->write(gsm->tty, data, len);
2103 return len;
2104}
2105
2106/**
2107 * gsmld_attach_gsm - mode set up
2108 * @tty: our tty structure
2109 * @gsm: our mux
2110 *
2111 * Set up the MUX for basic mode and commence connecting to the
2112 * modem. Currently called from the line discipline set up but
2113 * will need moving to an ioctl path.
2114 */
2115
2116static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2117{
2118 int ret;
2119
2120 gsm->tty = tty_kref_get(tty);
2121 gsm->output = gsmld_output;
2122 ret = gsm_activate_mux(gsm);
2123 if (ret != 0)
2124 tty_kref_put(gsm->tty);
2125 return ret;
2126}
2127
2128
2129/**
2130 * gsmld_detach_gsm - stop doing 0710 mux
2131 * @tty: tty attached to the mux
2132 * @gsm: mux
2133 *
2134 * Shutdown and then clean up the resources used by the line discipline
2135 */
2136
2137static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2138{
2139 WARN_ON(tty != gsm->tty);
2140 gsm_cleanup_mux(gsm);
2141 tty_kref_put(gsm->tty);
2142 gsm->tty = NULL;
2143}
2144
2145static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2146 char *fp, int count)
2147{
2148 struct gsm_mux *gsm = tty->disc_data;
2149 const unsigned char *dp;
2150 char *f;
2151 int i;
2152 char buf[64];
2153 char flags;
2154
2155 if (debug & 4)
2156 print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET,
2157 cp, count);
2158
2159 for (i = count, dp = cp, f = fp; i; i--, dp++) {
2160 flags = *f++;
2161 switch (flags) {
2162 case TTY_NORMAL:
2163 gsm->receive(gsm, *dp);
2164 break;
2165 case TTY_OVERRUN:
2166 case TTY_BREAK:
2167 case TTY_PARITY:
2168 case TTY_FRAME:
2169 gsm->error(gsm, *dp, flags);
2170 break;
2171 default:
2172 WARN_ONCE("%s: unknown flag %d\n",
2173 tty_name(tty, buf), flags);
2174 break;
2175 }
2176 }
2177 /* FASYNC if needed ? */
2178 /* If clogged call tty_throttle(tty); */
2179}
2180
2181/**
2182 * gsmld_chars_in_buffer - report available bytes
2183 * @tty: tty device
2184 *
2185 * Report the number of characters buffered to be delivered to user
2186 * at this instant in time.
2187 *
2188 * Locking: gsm lock
2189 */
2190
2191static ssize_t gsmld_chars_in_buffer(struct tty_struct *tty)
2192{
2193 return 0;
2194}
2195
2196/**
2197 * gsmld_flush_buffer - clean input queue
2198 * @tty: terminal device
2199 *
2200 * Flush the input buffer. Called when the line discipline is
2201 * being closed, when the tty layer wants the buffer flushed (eg
2202 * at hangup).
2203 */
2204
2205static void gsmld_flush_buffer(struct tty_struct *tty)
2206{
2207}
2208
2209/**
2210 * gsmld_close - close the ldisc for this tty
2211 * @tty: device
2212 *
2213 * Called from the terminal layer when this line discipline is
2214 * being shut down, either because of a close or becsuse of a
2215 * discipline change. The function will not be called while other
2216 * ldisc methods are in progress.
2217 */
2218
2219static void gsmld_close(struct tty_struct *tty)
2220{
2221 struct gsm_mux *gsm = tty->disc_data;
2222
2223 gsmld_detach_gsm(tty, gsm);
2224
2225 gsmld_flush_buffer(tty);
2226 /* Do other clean up here */
2227 gsm_free_mux(gsm);
2228}
2229
2230/**
2231 * gsmld_open - open an ldisc
2232 * @tty: terminal to open
2233 *
2234 * Called when this line discipline is being attached to the
2235 * terminal device. Can sleep. Called serialized so that no
2236 * other events will occur in parallel. No further open will occur
2237 * until a close.
2238 */
2239
2240static int gsmld_open(struct tty_struct *tty)
2241{
2242 struct gsm_mux *gsm;
2243
2244 if (tty->ops->write == NULL)
2245 return -EINVAL;
2246
2247 /* Attach our ldisc data */
2248 gsm = gsm_alloc_mux();
2249 if (gsm == NULL)
2250 return -ENOMEM;
2251
2252 tty->disc_data = gsm;
2253 tty->receive_room = 65536;
2254
2255 /* Attach the initial passive connection */
2256 gsm->encoding = 1;
2257 return gsmld_attach_gsm(tty, gsm);
2258}
2259
2260/**
2261 * gsmld_write_wakeup - asynchronous I/O notifier
2262 * @tty: tty device
2263 *
2264 * Required for the ptys, serial driver etc. since processes
2265 * that attach themselves to the master and rely on ASYNC
2266 * IO must be woken up
2267 */
2268
2269static void gsmld_write_wakeup(struct tty_struct *tty)
2270{
2271 struct gsm_mux *gsm = tty->disc_data;
2272 unsigned long flags;
2273
2274 /* Queue poll */
2275 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2276 gsm_data_kick(gsm);
2277 if (gsm->tx_bytes < TX_THRESH_LO) {
2278 spin_lock_irqsave(&gsm->tx_lock, flags);
2279 gsm_dlci_data_sweep(gsm);
2280 spin_unlock_irqrestore(&gsm->tx_lock, flags);
2281 }
2282}
2283
2284/**
2285 * gsmld_read - read function for tty
2286 * @tty: tty device
2287 * @file: file object
2288 * @buf: userspace buffer pointer
2289 * @nr: size of I/O
2290 *
2291 * Perform reads for the line discipline. We are guaranteed that the
2292 * line discipline will not be closed under us but we may get multiple
2293 * parallel readers and must handle this ourselves. We may also get
2294 * a hangup. Always called in user context, may sleep.
2295 *
2296 * This code must be sure never to sleep through a hangup.
2297 */
2298
2299static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2300 unsigned char __user *buf, size_t nr)
2301{
2302 return -EOPNOTSUPP;
2303}
2304
2305/**
2306 * gsmld_write - write function for tty
2307 * @tty: tty device
2308 * @file: file object
2309 * @buf: userspace buffer pointer
2310 * @nr: size of I/O
2311 *
2312 * Called when the owner of the device wants to send a frame
2313 * itself (or some other control data). The data is transferred
2314 * as-is and must be properly framed and checksummed as appropriate
2315 * by userspace. Frames are either sent whole or not at all as this
2316 * avoids pain user side.
2317 */
2318
2319static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
2320 const unsigned char *buf, size_t nr)
2321{
2322 int space = tty_write_room(tty);
2323 if (space >= nr)
2324 return tty->ops->write(tty, buf, nr);
2325 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2326 return -ENOBUFS;
2327}
2328
2329/**
2330 * gsmld_poll - poll method for N_GSM0710
2331 * @tty: terminal device
2332 * @file: file accessing it
2333 * @wait: poll table
2334 *
2335 * Called when the line discipline is asked to poll() for data or
2336 * for special events. This code is not serialized with respect to
2337 * other events save open/close.
2338 *
2339 * This code must be sure never to sleep through a hangup.
2340 * Called without the kernel lock held - fine
2341 */
2342
2343static unsigned int gsmld_poll(struct tty_struct *tty, struct file *file,
2344 poll_table *wait)
2345{
2346 unsigned int mask = 0;
2347 struct gsm_mux *gsm = tty->disc_data;
2348
2349 poll_wait(file, &tty->read_wait, wait);
2350 poll_wait(file, &tty->write_wait, wait);
2351 if (tty_hung_up_p(file))
2352 mask |= POLLHUP;
2353 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
2354 mask |= POLLOUT | POLLWRNORM;
2355 if (gsm->dead)
2356 mask |= POLLHUP;
2357 return mask;
2358}
2359
2360static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
2361 struct gsm_config *c)
2362{
2363 int need_close = 0;
2364 int need_restart = 0;
2365
2366 /* Stuff we don't support yet - UI or I frame transport, windowing */
2367 if ((c->adaption != 1 && c->adaption != 2) || c->k)
2368 return -EOPNOTSUPP;
2369 /* Check the MRU/MTU range looks sane */
2370 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2371 return -EINVAL;
2372 if (c->n2 < 3)
2373 return -EINVAL;
2374 if (c->encapsulation > 1) /* Basic, advanced, no I */
2375 return -EINVAL;
2376 if (c->initiator > 1)
2377 return -EINVAL;
2378 if (c->i == 0 || c->i > 2) /* UIH and UI only */
2379 return -EINVAL;
2380 /*
2381 * See what is needed for reconfiguration
2382 */
2383
2384 /* Timing fields */
2385 if (c->t1 != 0 && c->t1 != gsm->t1)
2386 need_restart = 1;
2387 if (c->t2 != 0 && c->t2 != gsm->t2)
2388 need_restart = 1;
2389 if (c->encapsulation != gsm->encoding)
2390 need_restart = 1;
2391 if (c->adaption != gsm->adaption)
2392 need_restart = 1;
2393 /* Requires care */
2394 if (c->initiator != gsm->initiator)
2395 need_close = 1;
2396 if (c->mru != gsm->mru)
2397 need_restart = 1;
2398 if (c->mtu != gsm->mtu)
2399 need_restart = 1;
2400
2401 /*
2402 * Close down what is needed, restart and initiate the new
2403 * configuration
2404 */
2405
2406 if (need_close || need_restart) {
2407 gsm_dlci_begin_close(gsm->dlci[0]);
2408 /* This will timeout if the link is down due to N2 expiring */
2409 wait_event_interruptible(gsm->event,
2410 gsm->dlci[0]->state == DLCI_CLOSED);
2411 if (signal_pending(current))
2412 return -EINTR;
2413 }
2414 if (need_restart)
2415 gsm_cleanup_mux(gsm);
2416
2417 gsm->initiator = c->initiator;
2418 gsm->mru = c->mru;
2419 gsm->mtu = c->mtu;
2420 gsm->encoding = c->encapsulation;
2421 gsm->adaption = c->adaption;
2422 gsm->n2 = c->n2;
2423
2424 if (c->i == 1)
2425 gsm->ftype = UIH;
2426 else if (c->i == 2)
2427 gsm->ftype = UI;
2428
2429 if (c->t1)
2430 gsm->t1 = c->t1;
2431 if (c->t2)
2432 gsm->t2 = c->t2;
2433
2434 /* FIXME: We need to separate activation/deactivation from adding
2435 and removing from the mux array */
2436 if (need_restart)
2437 gsm_activate_mux(gsm);
2438 if (gsm->initiator && need_close)
2439 gsm_dlci_begin_open(gsm->dlci[0]);
2440 return 0;
2441}
2442
2443static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
2444 unsigned int cmd, unsigned long arg)
2445{
2446 struct gsm_config c;
2447 struct gsm_mux *gsm = tty->disc_data;
2448
2449 switch (cmd) {
2450 case GSMIOC_GETCONF:
2451 memset(&c, 0, sizeof(c));
2452 c.adaption = gsm->adaption;
2453 c.encapsulation = gsm->encoding;
2454 c.initiator = gsm->initiator;
2455 c.t1 = gsm->t1;
2456 c.t2 = gsm->t2;
2457 c.t3 = 0; /* Not supported */
2458 c.n2 = gsm->n2;
2459 if (gsm->ftype == UIH)
2460 c.i = 1;
2461 else
2462 c.i = 2;
2463 pr_debug("Ftype %d i %d\n", gsm->ftype, c.i);
2464 c.mru = gsm->mru;
2465 c.mtu = gsm->mtu;
2466 c.k = 0;
2467 if (copy_to_user((void *)arg, &c, sizeof(c)))
2468 return -EFAULT;
2469 return 0;
2470 case GSMIOC_SETCONF:
2471 if (copy_from_user(&c, (void *)arg, sizeof(c)))
2472 return -EFAULT;
2473 return gsmld_config(tty, gsm, &c);
2474 default:
2475 return n_tty_ioctl_helper(tty, file, cmd, arg);
2476 }
2477}
2478
2479
2480/* Line discipline for real tty */
2481struct tty_ldisc_ops tty_ldisc_packet = {
2482 .owner = THIS_MODULE,
2483 .magic = TTY_LDISC_MAGIC,
2484 .name = "n_gsm",
2485 .open = gsmld_open,
2486 .close = gsmld_close,
2487 .flush_buffer = gsmld_flush_buffer,
2488 .chars_in_buffer = gsmld_chars_in_buffer,
2489 .read = gsmld_read,
2490 .write = gsmld_write,
2491 .ioctl = gsmld_ioctl,
2492 .poll = gsmld_poll,
2493 .receive_buf = gsmld_receive_buf,
2494 .write_wakeup = gsmld_write_wakeup
2495};
2496
2497/*
2498 * Virtual tty side
2499 */
2500
2501#define TX_SIZE 512
2502
2503static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
2504{
2505 u8 modembits[5];
2506 struct gsm_control *ctrl;
2507 int len = 2;
2508
2509 if (brk)
2510 len++;
2511
2512 modembits[0] = len << 1 | EA; /* Data bytes */
2513 modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
2514 modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
2515 if (brk)
2516 modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
2517 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
2518 if (ctrl == NULL)
2519 return -ENOMEM;
2520 return gsm_control_wait(dlci->gsm, ctrl);
2521}
2522
2523static int gsm_carrier_raised(struct tty_port *port)
2524{
2525 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2526 /* Not yet open so no carrier info */
2527 if (dlci->state != DLCI_OPEN)
2528 return 0;
2529 if (debug & 2)
2530 return 1;
2531 return dlci->modem_rx & TIOCM_CD;
2532}
2533
2534static void gsm_dtr_rts(struct tty_port *port, int onoff)
2535{
2536 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2537 unsigned int modem_tx = dlci->modem_tx;
2538 if (onoff)
2539 modem_tx |= TIOCM_DTR | TIOCM_RTS;
2540 else
2541 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
2542 if (modem_tx != dlci->modem_tx) {
2543 dlci->modem_tx = modem_tx;
2544 gsmtty_modem_update(dlci, 0);
2545 }
2546}
2547
2548static const struct tty_port_operations gsm_port_ops = {
2549 .carrier_raised = gsm_carrier_raised,
2550 .dtr_rts = gsm_dtr_rts,
2551};
2552
2553
2554static int gsmtty_open(struct tty_struct *tty, struct file *filp)
2555{
2556 struct gsm_mux *gsm;
2557 struct gsm_dlci *dlci;
2558 struct tty_port *port;
2559 unsigned int line = tty->index;
2560 unsigned int mux = line >> 6;
2561
2562 line = line & 0x3F;
2563
2564 if (mux >= MAX_MUX)
2565 return -ENXIO;
2566 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
2567 if (gsm_mux[mux] == NULL)
2568 return -EUNATCH;
2569 if (line == 0 || line > 61) /* 62/63 reserved */
2570 return -ECHRNG;
2571 gsm = gsm_mux[mux];
2572 if (gsm->dead)
2573 return -EL2HLT;
2574 dlci = gsm->dlci[line];
2575 if (dlci == NULL)
2576 dlci = gsm_dlci_alloc(gsm, line);
2577 if (dlci == NULL)
2578 return -ENOMEM;
2579 port = &dlci->port;
2580 port->count++;
2581 tty->driver_data = dlci;
2582 tty_port_tty_set(port, tty);
2583
2584 dlci->modem_rx = 0;
2585 /* We could in theory open and close before we wait - eg if we get
2586 a DM straight back. This is ok as that will have caused a hangup */
2587 set_bit(ASYNCB_INITIALIZED, &port->flags);
2588 /* Start sending off SABM messages */
2589 gsm_dlci_begin_open(dlci);
2590 /* And wait for virtual carrier */
2591 return tty_port_block_til_ready(port, tty, filp);
2592}
2593
2594static void gsmtty_close(struct tty_struct *tty, struct file *filp)
2595{
2596 struct gsm_dlci *dlci = tty->driver_data;
2597 if (dlci == NULL)
2598 return;
2599 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
2600 return;
2601 gsm_dlci_begin_close(dlci);
2602 tty_port_close_end(&dlci->port, tty);
2603 tty_port_tty_set(&dlci->port, NULL);
2604}
2605
2606static void gsmtty_hangup(struct tty_struct *tty)
2607{
2608 struct gsm_dlci *dlci = tty->driver_data;
2609 tty_port_hangup(&dlci->port);
2610 gsm_dlci_begin_close(dlci);
2611}
2612
2613static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
2614 int len)
2615{
2616 struct gsm_dlci *dlci = tty->driver_data;
2617 /* Stuff the bytes into the fifo queue */
2618 int sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
2619 /* Need to kick the channel */
2620 gsm_dlci_data_kick(dlci);
2621 return sent;
2622}
2623
2624static int gsmtty_write_room(struct tty_struct *tty)
2625{
2626 struct gsm_dlci *dlci = tty->driver_data;
2627 return TX_SIZE - kfifo_len(dlci->fifo);
2628}
2629
2630static int gsmtty_chars_in_buffer(struct tty_struct *tty)
2631{
2632 struct gsm_dlci *dlci = tty->driver_data;
2633 return kfifo_len(dlci->fifo);
2634}
2635
2636static void gsmtty_flush_buffer(struct tty_struct *tty)
2637{
2638 struct gsm_dlci *dlci = tty->driver_data;
2639 /* Caution needed: If we implement reliable transport classes
2640 then the data being transmitted can't simply be junked once
2641 it has first hit the stack. Until then we can just blow it
2642 away */
2643 kfifo_reset(dlci->fifo);
2644 /* Need to unhook this DLCI from the transmit queue logic */
2645}
2646
2647static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
2648{
2649 /* The FIFO handles the queue so the kernel will do the right
2650 thing waiting on chars_in_buffer before calling us. No work
2651 to do here */
2652}
2653
2654static int gsmtty_tiocmget(struct tty_struct *tty)
2655{
2656 struct gsm_dlci *dlci = tty->driver_data;
2657 return dlci->modem_rx;
2658}
2659
2660static int gsmtty_tiocmset(struct tty_struct *tty,
2661 unsigned int set, unsigned int clear)
2662{
2663 struct gsm_dlci *dlci = tty->driver_data;
2664 unsigned int modem_tx = dlci->modem_tx;
2665
2666 modem_tx &= clear;
2667 modem_tx |= set;
2668
2669 if (modem_tx != dlci->modem_tx) {
2670 dlci->modem_tx = modem_tx;
2671 return gsmtty_modem_update(dlci, 0);
2672 }
2673 return 0;
2674}
2675
2676
2677static int gsmtty_ioctl(struct tty_struct *tty,
2678 unsigned int cmd, unsigned long arg)
2679{
2680 return -ENOIOCTLCMD;
2681}
2682
2683static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
2684{
2685 /* For the moment its fixed. In actual fact the speed information
2686 for the virtual channel can be propogated in both directions by
2687 the RPN control message. This however rapidly gets nasty as we
2688 then have to remap modem signals each way according to whether
2689 our virtual cable is null modem etc .. */
2690 tty_termios_copy_hw(tty->termios, old);
2691}
2692
2693static void gsmtty_throttle(struct tty_struct *tty)
2694{
2695 struct gsm_dlci *dlci = tty->driver_data;
2696 if (tty->termios->c_cflag & CRTSCTS)
2697 dlci->modem_tx &= ~TIOCM_DTR;
2698 dlci->throttled = 1;
2699 /* Send an MSC with DTR cleared */
2700 gsmtty_modem_update(dlci, 0);
2701}
2702
2703static void gsmtty_unthrottle(struct tty_struct *tty)
2704{
2705 struct gsm_dlci *dlci = tty->driver_data;
2706 if (tty->termios->c_cflag & CRTSCTS)
2707 dlci->modem_tx |= TIOCM_DTR;
2708 dlci->throttled = 0;
2709 /* Send an MSC with DTR set */
2710 gsmtty_modem_update(dlci, 0);
2711}
2712
2713static int gsmtty_break_ctl(struct tty_struct *tty, int state)
2714{
2715 struct gsm_dlci *dlci = tty->driver_data;
2716 int encode = 0; /* Off */
2717
2718 if (state == -1) /* "On indefinitely" - we can't encode this
2719 properly */
2720 encode = 0x0F;
2721 else if (state > 0) {
2722 encode = state / 200; /* mS to encoding */
2723 if (encode > 0x0F)
2724 encode = 0x0F; /* Best effort */
2725 }
2726 return gsmtty_modem_update(dlci, encode);
2727}
2728
2729static struct tty_driver *gsm_tty_driver;
2730
2731/* Virtual ttys for the demux */
2732static const struct tty_operations gsmtty_ops = {
2733 .open = gsmtty_open,
2734 .close = gsmtty_close,
2735 .write = gsmtty_write,
2736 .write_room = gsmtty_write_room,
2737 .chars_in_buffer = gsmtty_chars_in_buffer,
2738 .flush_buffer = gsmtty_flush_buffer,
2739 .ioctl = gsmtty_ioctl,
2740 .throttle = gsmtty_throttle,
2741 .unthrottle = gsmtty_unthrottle,
2742 .set_termios = gsmtty_set_termios,
2743 .hangup = gsmtty_hangup,
2744 .wait_until_sent = gsmtty_wait_until_sent,
2745 .tiocmget = gsmtty_tiocmget,
2746 .tiocmset = gsmtty_tiocmset,
2747 .break_ctl = gsmtty_break_ctl,
2748};
2749
2750
2751
2752static int __init gsm_init(void)
2753{
2754 /* Fill in our line protocol discipline, and register it */
2755 int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
2756 if (status != 0) {
2757 pr_err("n_gsm: can't register line discipline (err = %d)\n",
2758 status);
2759 return status;
2760 }
2761
2762 gsm_tty_driver = alloc_tty_driver(256);
2763 if (!gsm_tty_driver) {
2764 tty_unregister_ldisc(N_GSM0710);
2765 pr_err("gsm_init: tty allocation failed.\n");
2766 return -EINVAL;
2767 }
2768 gsm_tty_driver->owner = THIS_MODULE;
2769 gsm_tty_driver->driver_name = "gsmtty";
2770 gsm_tty_driver->name = "gsmtty";
2771 gsm_tty_driver->major = 0; /* Dynamic */
2772 gsm_tty_driver->minor_start = 0;
2773 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2774 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2775 gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
2776 | TTY_DRIVER_HARDWARE_BREAK;
2777 gsm_tty_driver->init_termios = tty_std_termios;
2778 /* Fixme */
2779 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
2780 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
2781
2782 spin_lock_init(&gsm_mux_lock);
2783
2784 if (tty_register_driver(gsm_tty_driver)) {
2785 put_tty_driver(gsm_tty_driver);
2786 tty_unregister_ldisc(N_GSM0710);
2787 pr_err("gsm_init: tty registration failed.\n");
2788 return -EBUSY;
2789 }
2790 pr_debug("gsm_init: loaded as %d,%d.\n",
2791 gsm_tty_driver->major, gsm_tty_driver->minor_start);
2792 return 0;
2793}
2794
2795static void __exit gsm_exit(void)
2796{
2797 int status = tty_unregister_ldisc(N_GSM0710);
2798 if (status != 0)
2799 pr_err("n_gsm: can't unregister line discipline (err = %d)\n",
2800 status);
2801 tty_unregister_driver(gsm_tty_driver);
2802 put_tty_driver(gsm_tty_driver);
2803}
2804
2805module_init(gsm_init);
2806module_exit(gsm_exit);
2807
2808
2809MODULE_LICENSE("GPL");
2810MODULE_ALIAS_LDISC(N_GSM0710);
diff --git a/drivers/tty/n_hdlc.c b/drivers/tty/n_hdlc.c
new file mode 100644
index 000000000000..cea56033b34c
--- /dev/null
+++ b/drivers/tty/n_hdlc.c
@@ -0,0 +1,1006 @@
1/* generic HDLC line discipline for Linux
2 *
3 * Written by Paul Fulghum paulkf@microgate.com
4 * for Microgate Corporation
5 *
6 * Microgate and SyncLink are registered trademarks of Microgate Corporation
7 *
8 * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
9 * Al Longyear <longyear@netcom.com>,
10 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
11 *
12 * Original release 01/11/99
13 *
14 * This code is released under the GNU General Public License (GPL)
15 *
16 * This module implements the tty line discipline N_HDLC for use with
17 * tty device drivers that support bit-synchronous HDLC communications.
18 *
19 * All HDLC data is frame oriented which means:
20 *
21 * 1. tty write calls represent one complete transmit frame of data
22 * The device driver should accept the complete frame or none of
23 * the frame (busy) in the write method. Each write call should have
24 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
25 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
26 * should include any crc bytes required. For example, when using
27 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
28 * the application may transmit is limited to 65531 bytes. For CCITT
29 * CRC16, the maximum application frame size would be 65533.
30 *
31 *
32 * 2. receive callbacks from the device driver represents
33 * one received frame. The device driver should bypass
34 * the tty flip buffer and call the line discipline receive
35 * callback directly to avoid fragmenting or concatenating
36 * multiple frames into a single receive callback.
37 *
38 * The HDLC line discipline queues the receive frames in separate
39 * buffers so complete receive frames can be returned by the
40 * tty read calls.
41 *
42 * 3. tty read calls returns an entire frame of data or nothing.
43 *
44 * 4. all send and receive data is considered raw. No processing
45 * or translation is performed by the line discipline, regardless
46 * of the tty flags
47 *
48 * 5. When line discipline is queried for the amount of receive
49 * data available (FIOC), 0 is returned if no data available,
50 * otherwise the count of the next available frame is returned.
51 * (instead of the sum of all received frame counts).
52 *
53 * These conventions allow the standard tty programming interface
54 * to be used for synchronous HDLC applications when used with
55 * this line discipline (or another line discipline that is frame
56 * oriented such as N_PPP).
57 *
58 * The SyncLink driver (synclink.c) implements both asynchronous
59 * (using standard line discipline N_TTY) and synchronous HDLC
60 * (using N_HDLC) communications, with the latter using the above
61 * conventions.
62 *
63 * This implementation is very basic and does not maintain
64 * any statistics. The main point is to enforce the raw data
65 * and frame orientation of HDLC communications.
66 *
67 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
68 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
71 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
72 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
75 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
77 * OF THE POSSIBILITY OF SUCH DAMAGE.
78 */
79
80#define HDLC_MAGIC 0x239e
81
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/kernel.h>
85#include <linux/sched.h>
86#include <linux/types.h>
87#include <linux/fcntl.h>
88#include <linux/interrupt.h>
89#include <linux/ptrace.h>
90
91#undef VERSION
92#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
93
94#include <linux/poll.h>
95#include <linux/in.h>
96#include <linux/ioctl.h>
97#include <linux/slab.h>
98#include <linux/tty.h>
99#include <linux/errno.h>
100#include <linux/string.h> /* used in new tty drivers */
101#include <linux/signal.h> /* used in new tty drivers */
102#include <linux/if.h>
103#include <linux/bitops.h>
104
105#include <asm/system.h>
106#include <asm/termios.h>
107#include <asm/uaccess.h>
108
109/*
110 * Buffers for individual HDLC frames
111 */
112#define MAX_HDLC_FRAME_SIZE 65535
113#define DEFAULT_RX_BUF_COUNT 10
114#define MAX_RX_BUF_COUNT 60
115#define DEFAULT_TX_BUF_COUNT 3
116
117struct n_hdlc_buf {
118 struct n_hdlc_buf *link;
119 int count;
120 char buf[1];
121};
122
123#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
124
125struct n_hdlc_buf_list {
126 struct n_hdlc_buf *head;
127 struct n_hdlc_buf *tail;
128 int count;
129 spinlock_t spinlock;
130};
131
132/**
133 * struct n_hdlc - per device instance data structure
134 * @magic - magic value for structure
135 * @flags - miscellaneous control flags
136 * @tty - ptr to TTY structure
137 * @backup_tty - TTY to use if tty gets closed
138 * @tbusy - reentrancy flag for tx wakeup code
139 * @woke_up - FIXME: describe this field
140 * @tbuf - currently transmitting tx buffer
141 * @tx_buf_list - list of pending transmit frame buffers
142 * @rx_buf_list - list of received frame buffers
143 * @tx_free_buf_list - list unused transmit frame buffers
144 * @rx_free_buf_list - list unused received frame buffers
145 */
146struct n_hdlc {
147 int magic;
148 __u32 flags;
149 struct tty_struct *tty;
150 struct tty_struct *backup_tty;
151 int tbusy;
152 int woke_up;
153 struct n_hdlc_buf *tbuf;
154 struct n_hdlc_buf_list tx_buf_list;
155 struct n_hdlc_buf_list rx_buf_list;
156 struct n_hdlc_buf_list tx_free_buf_list;
157 struct n_hdlc_buf_list rx_free_buf_list;
158};
159
160/*
161 * HDLC buffer list manipulation functions
162 */
163static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list);
164static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
165 struct n_hdlc_buf *buf);
166static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
167
168/* Local functions */
169
170static struct n_hdlc *n_hdlc_alloc (void);
171
172/* debug level can be set by insmod for debugging purposes */
173#define DEBUG_LEVEL_INFO 1
174static int debuglevel;
175
176/* max frame size for memory allocations */
177static int maxframe = 4096;
178
179/* TTY callbacks */
180
181static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
182 __u8 __user *buf, size_t nr);
183static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
184 const unsigned char *buf, size_t nr);
185static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
186 unsigned int cmd, unsigned long arg);
187static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
188 poll_table *wait);
189static int n_hdlc_tty_open(struct tty_struct *tty);
190static void n_hdlc_tty_close(struct tty_struct *tty);
191static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
192 char *fp, int count);
193static void n_hdlc_tty_wakeup(struct tty_struct *tty);
194
195#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
196
197#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
198#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
199
200static void flush_rx_queue(struct tty_struct *tty)
201{
202 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
203 struct n_hdlc_buf *buf;
204
205 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
206 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
207}
208
209static void flush_tx_queue(struct tty_struct *tty)
210{
211 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
212 struct n_hdlc_buf *buf;
213 unsigned long flags;
214
215 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
216 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
217 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
218 if (n_hdlc->tbuf) {
219 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, n_hdlc->tbuf);
220 n_hdlc->tbuf = NULL;
221 }
222 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
223}
224
225static struct tty_ldisc_ops n_hdlc_ldisc = {
226 .owner = THIS_MODULE,
227 .magic = TTY_LDISC_MAGIC,
228 .name = "hdlc",
229 .open = n_hdlc_tty_open,
230 .close = n_hdlc_tty_close,
231 .read = n_hdlc_tty_read,
232 .write = n_hdlc_tty_write,
233 .ioctl = n_hdlc_tty_ioctl,
234 .poll = n_hdlc_tty_poll,
235 .receive_buf = n_hdlc_tty_receive,
236 .write_wakeup = n_hdlc_tty_wakeup,
237 .flush_buffer = flush_rx_queue,
238};
239
240/**
241 * n_hdlc_release - release an n_hdlc per device line discipline info structure
242 * @n_hdlc - per device line discipline info structure
243 */
244static void n_hdlc_release(struct n_hdlc *n_hdlc)
245{
246 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
247 struct n_hdlc_buf *buf;
248
249 if (debuglevel >= DEBUG_LEVEL_INFO)
250 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
251
252 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
253 wake_up_interruptible (&tty->read_wait);
254 wake_up_interruptible (&tty->write_wait);
255
256 if (tty->disc_data == n_hdlc)
257 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
258
259 /* Release transmit and receive buffers */
260 for(;;) {
261 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
262 if (buf) {
263 kfree(buf);
264 } else
265 break;
266 }
267 for(;;) {
268 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
269 if (buf) {
270 kfree(buf);
271 } else
272 break;
273 }
274 for(;;) {
275 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
276 if (buf) {
277 kfree(buf);
278 } else
279 break;
280 }
281 for(;;) {
282 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
283 if (buf) {
284 kfree(buf);
285 } else
286 break;
287 }
288 kfree(n_hdlc->tbuf);
289 kfree(n_hdlc);
290
291} /* end of n_hdlc_release() */
292
293/**
294 * n_hdlc_tty_close - line discipline close
295 * @tty - pointer to tty info structure
296 *
297 * Called when the line discipline is changed to something
298 * else, the tty is closed, or the tty detects a hangup.
299 */
300static void n_hdlc_tty_close(struct tty_struct *tty)
301{
302 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
303
304 if (debuglevel >= DEBUG_LEVEL_INFO)
305 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
306
307 if (n_hdlc != NULL) {
308 if (n_hdlc->magic != HDLC_MAGIC) {
309 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
310 return;
311 }
312#if defined(TTY_NO_WRITE_SPLIT)
313 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
314#endif
315 tty->disc_data = NULL;
316 if (tty == n_hdlc->backup_tty)
317 n_hdlc->backup_tty = NULL;
318 if (tty != n_hdlc->tty)
319 return;
320 if (n_hdlc->backup_tty) {
321 n_hdlc->tty = n_hdlc->backup_tty;
322 } else {
323 n_hdlc_release (n_hdlc);
324 }
325 }
326
327 if (debuglevel >= DEBUG_LEVEL_INFO)
328 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
329
330} /* end of n_hdlc_tty_close() */
331
332/**
333 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
334 * @tty - pointer to tty info structure
335 *
336 * Returns 0 if success, otherwise error code
337 */
338static int n_hdlc_tty_open (struct tty_struct *tty)
339{
340 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
341
342 if (debuglevel >= DEBUG_LEVEL_INFO)
343 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
344 __FILE__,__LINE__,
345 tty->name);
346
347 /* There should not be an existing table for this slot. */
348 if (n_hdlc) {
349 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
350 return -EEXIST;
351 }
352
353 n_hdlc = n_hdlc_alloc();
354 if (!n_hdlc) {
355 printk (KERN_ERR "n_hdlc_alloc failed\n");
356 return -ENFILE;
357 }
358
359 tty->disc_data = n_hdlc;
360 n_hdlc->tty = tty;
361 tty->receive_room = 65536;
362
363#if defined(TTY_NO_WRITE_SPLIT)
364 /* change tty_io write() to not split large writes into 8K chunks */
365 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
366#endif
367
368 /* flush receive data from driver */
369 tty_driver_flush_buffer(tty);
370
371 if (debuglevel >= DEBUG_LEVEL_INFO)
372 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
373
374 return 0;
375
376} /* end of n_tty_hdlc_open() */
377
378/**
379 * n_hdlc_send_frames - send frames on pending send buffer list
380 * @n_hdlc - pointer to ldisc instance data
381 * @tty - pointer to tty instance data
382 *
383 * Send frames on pending send buffer list until the driver does not accept a
384 * frame (busy) this function is called after adding a frame to the send buffer
385 * list and by the tty wakeup callback.
386 */
387static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
388{
389 register int actual;
390 unsigned long flags;
391 struct n_hdlc_buf *tbuf;
392
393 if (debuglevel >= DEBUG_LEVEL_INFO)
394 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
395 check_again:
396
397 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
398 if (n_hdlc->tbusy) {
399 n_hdlc->woke_up = 1;
400 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
401 return;
402 }
403 n_hdlc->tbusy = 1;
404 n_hdlc->woke_up = 0;
405 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
406
407 /* get current transmit buffer or get new transmit */
408 /* buffer from list of pending transmit buffers */
409
410 tbuf = n_hdlc->tbuf;
411 if (!tbuf)
412 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
413
414 while (tbuf) {
415 if (debuglevel >= DEBUG_LEVEL_INFO)
416 printk("%s(%d)sending frame %p, count=%d\n",
417 __FILE__,__LINE__,tbuf,tbuf->count);
418
419 /* Send the next block of data to device */
420 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
421 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
422
423 /* rollback was possible and has been done */
424 if (actual == -ERESTARTSYS) {
425 n_hdlc->tbuf = tbuf;
426 break;
427 }
428 /* if transmit error, throw frame away by */
429 /* pretending it was accepted by driver */
430 if (actual < 0)
431 actual = tbuf->count;
432
433 if (actual == tbuf->count) {
434 if (debuglevel >= DEBUG_LEVEL_INFO)
435 printk("%s(%d)frame %p completed\n",
436 __FILE__,__LINE__,tbuf);
437
438 /* free current transmit buffer */
439 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
440
441 /* this tx buffer is done */
442 n_hdlc->tbuf = NULL;
443
444 /* wait up sleeping writers */
445 wake_up_interruptible(&tty->write_wait);
446
447 /* get next pending transmit buffer */
448 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
449 } else {
450 if (debuglevel >= DEBUG_LEVEL_INFO)
451 printk("%s(%d)frame %p pending\n",
452 __FILE__,__LINE__,tbuf);
453
454 /* buffer not accepted by driver */
455 /* set this buffer as pending buffer */
456 n_hdlc->tbuf = tbuf;
457 break;
458 }
459 }
460
461 if (!tbuf)
462 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
463
464 /* Clear the re-entry flag */
465 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
466 n_hdlc->tbusy = 0;
467 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
468
469 if (n_hdlc->woke_up)
470 goto check_again;
471
472 if (debuglevel >= DEBUG_LEVEL_INFO)
473 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
474
475} /* end of n_hdlc_send_frames() */
476
477/**
478 * n_hdlc_tty_wakeup - Callback for transmit wakeup
479 * @tty - pointer to associated tty instance data
480 *
481 * Called when low level device driver can accept more send data.
482 */
483static void n_hdlc_tty_wakeup(struct tty_struct *tty)
484{
485 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
486
487 if (debuglevel >= DEBUG_LEVEL_INFO)
488 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
489
490 if (!n_hdlc)
491 return;
492
493 if (tty != n_hdlc->tty) {
494 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
495 return;
496 }
497
498 n_hdlc_send_frames (n_hdlc, tty);
499
500} /* end of n_hdlc_tty_wakeup() */
501
502/**
503 * n_hdlc_tty_receive - Called by tty driver when receive data is available
504 * @tty - pointer to tty instance data
505 * @data - pointer to received data
506 * @flags - pointer to flags for data
507 * @count - count of received data in bytes
508 *
509 * Called by tty low level driver when receive data is available. Data is
510 * interpreted as one HDLC frame.
511 */
512static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
513 char *flags, int count)
514{
515 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
516 register struct n_hdlc_buf *buf;
517
518 if (debuglevel >= DEBUG_LEVEL_INFO)
519 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
520 __FILE__,__LINE__, count);
521
522 /* This can happen if stuff comes in on the backup tty */
523 if (!n_hdlc || tty != n_hdlc->tty)
524 return;
525
526 /* verify line is using HDLC discipline */
527 if (n_hdlc->magic != HDLC_MAGIC) {
528 printk("%s(%d) line not using HDLC discipline\n",
529 __FILE__,__LINE__);
530 return;
531 }
532
533 if ( count>maxframe ) {
534 if (debuglevel >= DEBUG_LEVEL_INFO)
535 printk("%s(%d) rx count>maxframesize, data discarded\n",
536 __FILE__,__LINE__);
537 return;
538 }
539
540 /* get a free HDLC buffer */
541 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
542 if (!buf) {
543 /* no buffers in free list, attempt to allocate another rx buffer */
544 /* unless the maximum count has been reached */
545 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
546 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
547 }
548
549 if (!buf) {
550 if (debuglevel >= DEBUG_LEVEL_INFO)
551 printk("%s(%d) no more rx buffers, data discarded\n",
552 __FILE__,__LINE__);
553 return;
554 }
555
556 /* copy received data to HDLC buffer */
557 memcpy(buf->buf,data,count);
558 buf->count=count;
559
560 /* add HDLC buffer to list of received frames */
561 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
562
563 /* wake up any blocked reads and perform async signalling */
564 wake_up_interruptible (&tty->read_wait);
565 if (n_hdlc->tty->fasync != NULL)
566 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
567
568} /* end of n_hdlc_tty_receive() */
569
570/**
571 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
572 * @tty - pointer to tty instance data
573 * @file - pointer to open file object
574 * @buf - pointer to returned data buffer
575 * @nr - size of returned data buffer
576 *
577 * Returns the number of bytes returned or error code.
578 */
579static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
580 __u8 __user *buf, size_t nr)
581{
582 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
583 int ret = 0;
584 struct n_hdlc_buf *rbuf;
585 DECLARE_WAITQUEUE(wait, current);
586
587 if (debuglevel >= DEBUG_LEVEL_INFO)
588 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
589
590 /* Validate the pointers */
591 if (!n_hdlc)
592 return -EIO;
593
594 /* verify user access to buffer */
595 if (!access_ok(VERIFY_WRITE, buf, nr)) {
596 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
597 "buffer\n", __FILE__, __LINE__);
598 return -EFAULT;
599 }
600
601 add_wait_queue(&tty->read_wait, &wait);
602
603 for (;;) {
604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
605 ret = -EIO;
606 break;
607 }
608 if (tty_hung_up_p(file))
609 break;
610
611 set_current_state(TASK_INTERRUPTIBLE);
612
613 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
614 if (rbuf) {
615 if (rbuf->count > nr) {
616 /* too large for caller's buffer */
617 ret = -EOVERFLOW;
618 } else {
619 if (copy_to_user(buf, rbuf->buf, rbuf->count))
620 ret = -EFAULT;
621 else
622 ret = rbuf->count;
623 }
624
625 if (n_hdlc->rx_free_buf_list.count >
626 DEFAULT_RX_BUF_COUNT)
627 kfree(rbuf);
628 else
629 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
630 break;
631 }
632
633 /* no data */
634 if (file->f_flags & O_NONBLOCK) {
635 ret = -EAGAIN;
636 break;
637 }
638
639 schedule();
640
641 if (signal_pending(current)) {
642 ret = -EINTR;
643 break;
644 }
645 }
646
647 remove_wait_queue(&tty->read_wait, &wait);
648 __set_current_state(TASK_RUNNING);
649
650 return ret;
651
652} /* end of n_hdlc_tty_read() */
653
654/**
655 * n_hdlc_tty_write - write a single frame of data to device
656 * @tty - pointer to associated tty device instance data
657 * @file - pointer to file object data
658 * @data - pointer to transmit data (one frame)
659 * @count - size of transmit frame in bytes
660 *
661 * Returns the number of bytes written (or error code).
662 */
663static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
664 const unsigned char *data, size_t count)
665{
666 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
667 int error = 0;
668 DECLARE_WAITQUEUE(wait, current);
669 struct n_hdlc_buf *tbuf;
670
671 if (debuglevel >= DEBUG_LEVEL_INFO)
672 printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
673 __FILE__,__LINE__,count);
674
675 /* Verify pointers */
676 if (!n_hdlc)
677 return -EIO;
678
679 if (n_hdlc->magic != HDLC_MAGIC)
680 return -EIO;
681
682 /* verify frame size */
683 if (count > maxframe ) {
684 if (debuglevel & DEBUG_LEVEL_INFO)
685 printk (KERN_WARNING
686 "n_hdlc_tty_write: truncating user packet "
687 "from %lu to %d\n", (unsigned long) count,
688 maxframe );
689 count = maxframe;
690 }
691
692 add_wait_queue(&tty->write_wait, &wait);
693
694 for (;;) {
695 set_current_state(TASK_INTERRUPTIBLE);
696
697 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
698 if (tbuf)
699 break;
700
701 if (file->f_flags & O_NONBLOCK) {
702 error = -EAGAIN;
703 break;
704 }
705 schedule();
706
707 n_hdlc = tty2n_hdlc (tty);
708 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
709 tty != n_hdlc->tty) {
710 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
711 error = -EIO;
712 break;
713 }
714
715 if (signal_pending(current)) {
716 error = -EINTR;
717 break;
718 }
719 }
720
721 __set_current_state(TASK_RUNNING);
722 remove_wait_queue(&tty->write_wait, &wait);
723
724 if (!error) {
725 /* Retrieve the user's buffer */
726 memcpy(tbuf->buf, data, count);
727
728 /* Send the data */
729 tbuf->count = error = count;
730 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
731 n_hdlc_send_frames(n_hdlc,tty);
732 }
733
734 return error;
735
736} /* end of n_hdlc_tty_write() */
737
738/**
739 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
740 * @tty - pointer to tty instance data
741 * @file - pointer to open file object for device
742 * @cmd - IOCTL command code
743 * @arg - argument for IOCTL call (cmd dependent)
744 *
745 * Returns command dependent result.
746 */
747static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
748 unsigned int cmd, unsigned long arg)
749{
750 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
751 int error = 0;
752 int count;
753 unsigned long flags;
754
755 if (debuglevel >= DEBUG_LEVEL_INFO)
756 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
757 __FILE__,__LINE__,cmd);
758
759 /* Verify the status of the device */
760 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
761 return -EBADF;
762
763 switch (cmd) {
764 case FIONREAD:
765 /* report count of read data available */
766 /* in next available frame (if any) */
767 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
768 if (n_hdlc->rx_buf_list.head)
769 count = n_hdlc->rx_buf_list.head->count;
770 else
771 count = 0;
772 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
773 error = put_user(count, (int __user *)arg);
774 break;
775
776 case TIOCOUTQ:
777 /* get the pending tx byte count in the driver */
778 count = tty_chars_in_buffer(tty);
779 /* add size of next output frame in queue */
780 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
781 if (n_hdlc->tx_buf_list.head)
782 count += n_hdlc->tx_buf_list.head->count;
783 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
784 error = put_user(count, (int __user *)arg);
785 break;
786
787 case TCFLSH:
788 switch (arg) {
789 case TCIOFLUSH:
790 case TCOFLUSH:
791 flush_tx_queue(tty);
792 }
793 /* fall through to default */
794
795 default:
796 error = n_tty_ioctl_helper(tty, file, cmd, arg);
797 break;
798 }
799 return error;
800
801} /* end of n_hdlc_tty_ioctl() */
802
803/**
804 * n_hdlc_tty_poll - TTY callback for poll system call
805 * @tty - pointer to tty instance data
806 * @filp - pointer to open file object for device
807 * @poll_table - wait queue for operations
808 *
809 * Determine which operations (read/write) will not block and return info
810 * to caller.
811 * Returns a bit mask containing info on which ops will not block.
812 */
813static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
814 poll_table *wait)
815{
816 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
817 unsigned int mask = 0;
818
819 if (debuglevel >= DEBUG_LEVEL_INFO)
820 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
821
822 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
823 /* queue current process into any wait queue that */
824 /* may awaken in the future (read and write) */
825
826 poll_wait(filp, &tty->read_wait, wait);
827 poll_wait(filp, &tty->write_wait, wait);
828
829 /* set bits for operations that won't block */
830 if (n_hdlc->rx_buf_list.head)
831 mask |= POLLIN | POLLRDNORM; /* readable */
832 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
833 mask |= POLLHUP;
834 if (tty_hung_up_p(filp))
835 mask |= POLLHUP;
836 if (!tty_is_writelocked(tty) &&
837 n_hdlc->tx_free_buf_list.head)
838 mask |= POLLOUT | POLLWRNORM; /* writable */
839 }
840 return mask;
841} /* end of n_hdlc_tty_poll() */
842
843/**
844 * n_hdlc_alloc - allocate an n_hdlc instance data structure
845 *
846 * Returns a pointer to newly created structure if success, otherwise %NULL
847 */
848static struct n_hdlc *n_hdlc_alloc(void)
849{
850 struct n_hdlc_buf *buf;
851 int i;
852 struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
853
854 if (!n_hdlc)
855 return NULL;
856
857 memset(n_hdlc, 0, sizeof(*n_hdlc));
858
859 n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
860 n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
861 n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
862 n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
863
864 /* allocate free rx buffer list */
865 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
866 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
867 if (buf)
868 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
869 else if (debuglevel >= DEBUG_LEVEL_INFO)
870 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
871 }
872
873 /* allocate free tx buffer list */
874 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
875 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
876 if (buf)
877 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
878 else if (debuglevel >= DEBUG_LEVEL_INFO)
879 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
880 }
881
882 /* Initialize the control block */
883 n_hdlc->magic = HDLC_MAGIC;
884 n_hdlc->flags = 0;
885
886 return n_hdlc;
887
888} /* end of n_hdlc_alloc() */
889
890/**
891 * n_hdlc_buf_list_init - initialize specified HDLC buffer list
892 * @list - pointer to buffer list
893 */
894static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
895{
896 memset(list, 0, sizeof(*list));
897 spin_lock_init(&list->spinlock);
898} /* end of n_hdlc_buf_list_init() */
899
900/**
901 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
902 * @list - pointer to buffer list
903 * @buf - pointer to buffer
904 */
905static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
906 struct n_hdlc_buf *buf)
907{
908 unsigned long flags;
909 spin_lock_irqsave(&list->spinlock,flags);
910
911 buf->link=NULL;
912 if (list->tail)
913 list->tail->link = buf;
914 else
915 list->head = buf;
916 list->tail = buf;
917 (list->count)++;
918
919 spin_unlock_irqrestore(&list->spinlock,flags);
920
921} /* end of n_hdlc_buf_put() */
922
923/**
924 * n_hdlc_buf_get - remove and return an HDLC buffer from list
925 * @list - pointer to HDLC buffer list
926 *
927 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
928 * list.
929 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
930 */
931static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
932{
933 unsigned long flags;
934 struct n_hdlc_buf *buf;
935 spin_lock_irqsave(&list->spinlock,flags);
936
937 buf = list->head;
938 if (buf) {
939 list->head = buf->link;
940 (list->count)--;
941 }
942 if (!list->head)
943 list->tail = NULL;
944
945 spin_unlock_irqrestore(&list->spinlock,flags);
946 return buf;
947
948} /* end of n_hdlc_buf_get() */
949
950static char hdlc_banner[] __initdata =
951 KERN_INFO "HDLC line discipline maxframe=%u\n";
952static char hdlc_register_ok[] __initdata =
953 KERN_INFO "N_HDLC line discipline registered.\n";
954static char hdlc_register_fail[] __initdata =
955 KERN_ERR "error registering line discipline: %d\n";
956static char hdlc_init_fail[] __initdata =
957 KERN_INFO "N_HDLC: init failure %d\n";
958
959static int __init n_hdlc_init(void)
960{
961 int status;
962
963 /* range check maxframe arg */
964 if (maxframe < 4096)
965 maxframe = 4096;
966 else if (maxframe > 65535)
967 maxframe = 65535;
968
969 printk(hdlc_banner, maxframe);
970
971 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
972 if (!status)
973 printk(hdlc_register_ok);
974 else
975 printk(hdlc_register_fail, status);
976
977 if (status)
978 printk(hdlc_init_fail, status);
979 return status;
980
981} /* end of init_module() */
982
983static char hdlc_unregister_ok[] __exitdata =
984 KERN_INFO "N_HDLC: line discipline unregistered\n";
985static char hdlc_unregister_fail[] __exitdata =
986 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
987
988static void __exit n_hdlc_exit(void)
989{
990 /* Release tty registration of line discipline */
991 int status = tty_unregister_ldisc(N_HDLC);
992
993 if (status)
994 printk(hdlc_unregister_fail, status);
995 else
996 printk(hdlc_unregister_ok);
997}
998
999module_init(n_hdlc_init);
1000module_exit(n_hdlc_exit);
1001
1002MODULE_LICENSE("GPL");
1003MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
1004module_param(debuglevel, int, 0);
1005module_param(maxframe, int, 0);
1006MODULE_ALIAS_LDISC(N_HDLC);
diff --git a/drivers/tty/n_r3964.c b/drivers/tty/n_r3964.c
new file mode 100644
index 000000000000..5c6c31459a2f
--- /dev/null
+++ b/drivers/tty/n_r3964.c
@@ -0,0 +1,1263 @@
1/* r3964 linediscipline for linux
2 *
3 * -----------------------------------------------------------
4 * Copyright by
5 * Philips Automation Projects
6 * Kassel (Germany)
7 * -----------------------------------------------------------
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
10 *
11 * Author:
12 * L. Haag
13 *
14 * $Log: n_r3964.c,v $
15 * Revision 1.10 2001/03/18 13:02:24 dwmw2
16 * Fix timer usage, use spinlocks properly.
17 *
18 * Revision 1.9 2001/03/18 12:52:14 dwmw2
19 * Merge changes in 2.4.2
20 *
21 * Revision 1.8 2000/03/23 14:14:54 dwmw2
22 * Fix race in sleeping in r3964_read()
23 *
24 * Revision 1.7 1999/28/08 11:41:50 dwmw2
25 * Port to 2.3 kernel
26 *
27 * Revision 1.6 1998/09/30 00:40:40 dwmw2
28 * Fixed compilation on 2.0.x kernels
29 * Updated to newly registered tty-ldisc number 9
30 *
31 * Revision 1.5 1998/09/04 21:57:36 dwmw2
32 * Signal handling bug fixes, port to 2.1.x.
33 *
34 * Revision 1.4 1998/04/02 20:26:59 lhaag
35 * select, blocking, ...
36 *
37 * Revision 1.3 1998/02/12 18:58:43 root
38 * fixed some memory leaks
39 * calculation of checksum characters
40 *
41 * Revision 1.2 1998/02/07 13:03:34 root
42 * ioctl read_telegram
43 *
44 * Revision 1.1 1998/02/06 19:21:03 root
45 * Initial revision
46 *
47 *
48 */
49
50#include <linux/module.h>
51#include <linux/kernel.h>
52#include <linux/sched.h>
53#include <linux/types.h>
54#include <linux/fcntl.h>
55#include <linux/interrupt.h>
56#include <linux/ptrace.h>
57#include <linux/ioport.h>
58#include <linux/in.h>
59#include <linux/slab.h>
60#include <linux/tty.h>
61#include <linux/errno.h>
62#include <linux/string.h> /* used in new tty drivers */
63#include <linux/signal.h> /* used in new tty drivers */
64#include <linux/ioctl.h>
65#include <linux/n_r3964.h>
66#include <linux/poll.h>
67#include <linux/init.h>
68#include <asm/uaccess.h>
69
70/*#define DEBUG_QUEUE*/
71
72/* Log successful handshake and protocol operations */
73/*#define DEBUG_PROTO_S*/
74
75/* Log handshake and protocol errors: */
76/*#define DEBUG_PROTO_E*/
77
78/* Log Linediscipline operations (open, close, read, write...): */
79/*#define DEBUG_LDISC*/
80
81/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
82/*#define DEBUG_MODUL*/
83
84/* Macro helpers for debug output: */
85#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
86
87#ifdef DEBUG_MODUL
88#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
89#else
90#define TRACE_M(fmt, arg...) do {} while (0)
91#endif
92#ifdef DEBUG_PROTO_S
93#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
94#else
95#define TRACE_PS(fmt, arg...) do {} while (0)
96#endif
97#ifdef DEBUG_PROTO_E
98#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
99#else
100#define TRACE_PE(fmt, arg...) do {} while (0)
101#endif
102#ifdef DEBUG_LDISC
103#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
104#else
105#define TRACE_L(fmt, arg...) do {} while (0)
106#endif
107#ifdef DEBUG_QUEUE
108#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
109#else
110#define TRACE_Q(fmt, arg...) do {} while (0)
111#endif
112static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
113static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
114static void put_char(struct r3964_info *pInfo, unsigned char ch);
115static void trigger_transmit(struct r3964_info *pInfo);
116static void retry_transmit(struct r3964_info *pInfo);
117static void transmit_block(struct r3964_info *pInfo);
118static void receive_char(struct r3964_info *pInfo, const unsigned char c);
119static void receive_error(struct r3964_info *pInfo, const char flag);
120static void on_timeout(unsigned long priv);
121static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
122static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
123 unsigned char __user * buf);
124static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
125 int error_code, struct r3964_block_header *pBlock);
126static struct r3964_message *remove_msg(struct r3964_info *pInfo,
127 struct r3964_client_info *pClient);
128static void remove_client_block(struct r3964_info *pInfo,
129 struct r3964_client_info *pClient);
130
131static int r3964_open(struct tty_struct *tty);
132static void r3964_close(struct tty_struct *tty);
133static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
134 unsigned char __user * buf, size_t nr);
135static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
136 const unsigned char *buf, size_t nr);
137static int r3964_ioctl(struct tty_struct *tty, struct file *file,
138 unsigned int cmd, unsigned long arg);
139static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
140static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
141 struct poll_table_struct *wait);
142static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
143 char *fp, int count);
144
145static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
146 .owner = THIS_MODULE,
147 .magic = TTY_LDISC_MAGIC,
148 .name = "R3964",
149 .open = r3964_open,
150 .close = r3964_close,
151 .read = r3964_read,
152 .write = r3964_write,
153 .ioctl = r3964_ioctl,
154 .set_termios = r3964_set_termios,
155 .poll = r3964_poll,
156 .receive_buf = r3964_receive_buf,
157};
158
159static void dump_block(const unsigned char *block, unsigned int length)
160{
161 unsigned int i, j;
162 char linebuf[16 * 3 + 1];
163
164 for (i = 0; i < length; i += 16) {
165 for (j = 0; (j < 16) && (j + i < length); j++) {
166 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
167 }
168 linebuf[3 * j] = '\0';
169 TRACE_PS("%s", linebuf);
170 }
171}
172
173/*************************************************************
174 * Driver initialisation
175 *************************************************************/
176
177/*************************************************************
178 * Module support routines
179 *************************************************************/
180
181static void __exit r3964_exit(void)
182{
183 int status;
184
185 TRACE_M("cleanup_module()");
186
187 status = tty_unregister_ldisc(N_R3964);
188
189 if (status != 0) {
190 printk(KERN_ERR "r3964: error unregistering linediscipline: "
191 "%d\n", status);
192 } else {
193 TRACE_L("linediscipline successfully unregistered");
194 }
195}
196
197static int __init r3964_init(void)
198{
199 int status;
200
201 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
202
203 /*
204 * Register the tty line discipline
205 */
206
207 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
208 if (status == 0) {
209 TRACE_L("line discipline %d registered", N_R3964);
210 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
211 tty_ldisc_N_R3964.num);
212 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
213 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
214 } else {
215 printk(KERN_ERR "r3964: error registering line discipline: "
216 "%d\n", status);
217 }
218 return status;
219}
220
221module_init(r3964_init);
222module_exit(r3964_exit);
223
224/*************************************************************
225 * Protocol implementation routines
226 *************************************************************/
227
228static void add_tx_queue(struct r3964_info *pInfo,
229 struct r3964_block_header *pHeader)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(&pInfo->lock, flags);
234
235 pHeader->next = NULL;
236
237 if (pInfo->tx_last == NULL) {
238 pInfo->tx_first = pInfo->tx_last = pHeader;
239 } else {
240 pInfo->tx_last->next = pHeader;
241 pInfo->tx_last = pHeader;
242 }
243
244 spin_unlock_irqrestore(&pInfo->lock, flags);
245
246 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
247 pHeader, pHeader->length, pInfo->tx_first);
248}
249
250static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
251{
252 struct r3964_block_header *pHeader;
253 unsigned long flags;
254#ifdef DEBUG_QUEUE
255 struct r3964_block_header *pDump;
256#endif
257
258 pHeader = pInfo->tx_first;
259
260 if (pHeader == NULL)
261 return;
262
263#ifdef DEBUG_QUEUE
264 printk("r3964: remove_from_tx_queue: %p, length %u - ",
265 pHeader, pHeader->length);
266 for (pDump = pHeader; pDump; pDump = pDump->next)
267 printk("%p ", pDump);
268 printk("\n");
269#endif
270
271 if (pHeader->owner) {
272 if (error_code) {
273 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
274 error_code, NULL);
275 } else {
276 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
277 error_code, NULL);
278 }
279 wake_up_interruptible(&pInfo->read_wait);
280 }
281
282 spin_lock_irqsave(&pInfo->lock, flags);
283
284 pInfo->tx_first = pHeader->next;
285 if (pInfo->tx_first == NULL) {
286 pInfo->tx_last = NULL;
287 }
288
289 spin_unlock_irqrestore(&pInfo->lock, flags);
290
291 kfree(pHeader);
292 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
293
294 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
295 pInfo->tx_first, pInfo->tx_last);
296}
297
298static void add_rx_queue(struct r3964_info *pInfo,
299 struct r3964_block_header *pHeader)
300{
301 unsigned long flags;
302
303 spin_lock_irqsave(&pInfo->lock, flags);
304
305 pHeader->next = NULL;
306
307 if (pInfo->rx_last == NULL) {
308 pInfo->rx_first = pInfo->rx_last = pHeader;
309 } else {
310 pInfo->rx_last->next = pHeader;
311 pInfo->rx_last = pHeader;
312 }
313 pInfo->blocks_in_rx_queue++;
314
315 spin_unlock_irqrestore(&pInfo->lock, flags);
316
317 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
318 pHeader, pHeader->length,
319 pInfo->rx_first, pInfo->blocks_in_rx_queue);
320}
321
322static void remove_from_rx_queue(struct r3964_info *pInfo,
323 struct r3964_block_header *pHeader)
324{
325 unsigned long flags;
326 struct r3964_block_header *pFind;
327
328 if (pHeader == NULL)
329 return;
330
331 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
332 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
333 TRACE_Q("remove_from_rx_queue: %p, length %u",
334 pHeader, pHeader->length);
335
336 spin_lock_irqsave(&pInfo->lock, flags);
337
338 if (pInfo->rx_first == pHeader) {
339 /* Remove the first block in the linked list: */
340 pInfo->rx_first = pHeader->next;
341
342 if (pInfo->rx_first == NULL) {
343 pInfo->rx_last = NULL;
344 }
345 pInfo->blocks_in_rx_queue--;
346 } else {
347 /* Find block to remove: */
348 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
349 if (pFind->next == pHeader) {
350 /* Got it. */
351 pFind->next = pHeader->next;
352 pInfo->blocks_in_rx_queue--;
353 if (pFind->next == NULL) {
354 /* Oh, removed the last one! */
355 pInfo->rx_last = pFind;
356 }
357 break;
358 }
359 }
360 }
361
362 spin_unlock_irqrestore(&pInfo->lock, flags);
363
364 kfree(pHeader);
365 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
366
367 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
368 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
369}
370
371static void put_char(struct r3964_info *pInfo, unsigned char ch)
372{
373 struct tty_struct *tty = pInfo->tty;
374 /* FIXME: put_char should not be called from an IRQ */
375 tty_put_char(tty, ch);
376 pInfo->bcc ^= ch;
377}
378
379static void flush(struct r3964_info *pInfo)
380{
381 struct tty_struct *tty = pInfo->tty;
382
383 if (tty == NULL || tty->ops->flush_chars == NULL)
384 return;
385 tty->ops->flush_chars(tty);
386}
387
388static void trigger_transmit(struct r3964_info *pInfo)
389{
390 unsigned long flags;
391
392 spin_lock_irqsave(&pInfo->lock, flags);
393
394 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
395 pInfo->state = R3964_TX_REQUEST;
396 pInfo->nRetry = 0;
397 pInfo->flags &= ~R3964_ERROR;
398 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
399
400 spin_unlock_irqrestore(&pInfo->lock, flags);
401
402 TRACE_PS("trigger_transmit - sent STX");
403
404 put_char(pInfo, STX);
405 flush(pInfo);
406
407 pInfo->bcc = 0;
408 } else {
409 spin_unlock_irqrestore(&pInfo->lock, flags);
410 }
411}
412
413static void retry_transmit(struct r3964_info *pInfo)
414{
415 if (pInfo->nRetry < R3964_MAX_RETRIES) {
416 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
417 pInfo->bcc = 0;
418 put_char(pInfo, STX);
419 flush(pInfo);
420 pInfo->state = R3964_TX_REQUEST;
421 pInfo->nRetry++;
422 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
423 } else {
424 TRACE_PE("transmission failed after %d retries",
425 R3964_MAX_RETRIES);
426
427 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
428
429 put_char(pInfo, NAK);
430 flush(pInfo);
431 pInfo->state = R3964_IDLE;
432
433 trigger_transmit(pInfo);
434 }
435}
436
437static void transmit_block(struct r3964_info *pInfo)
438{
439 struct tty_struct *tty = pInfo->tty;
440 struct r3964_block_header *pBlock = pInfo->tx_first;
441 int room = 0;
442
443 if (tty == NULL || pBlock == NULL) {
444 return;
445 }
446
447 room = tty_write_room(tty);
448
449 TRACE_PS("transmit_block %p, room %d, length %d",
450 pBlock, room, pBlock->length);
451
452 while (pInfo->tx_position < pBlock->length) {
453 if (room < 2)
454 break;
455
456 if (pBlock->data[pInfo->tx_position] == DLE) {
457 /* send additional DLE char: */
458 put_char(pInfo, DLE);
459 }
460 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
461
462 room--;
463 }
464
465 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
466 put_char(pInfo, DLE);
467 put_char(pInfo, ETX);
468 if (pInfo->flags & R3964_BCC) {
469 put_char(pInfo, pInfo->bcc);
470 }
471 pInfo->state = R3964_WAIT_FOR_TX_ACK;
472 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
473 }
474 flush(pInfo);
475}
476
477static void on_receive_block(struct r3964_info *pInfo)
478{
479 unsigned int length;
480 struct r3964_client_info *pClient;
481 struct r3964_block_header *pBlock;
482
483 length = pInfo->rx_position;
484
485 /* compare byte checksum characters: */
486 if (pInfo->flags & R3964_BCC) {
487 if (pInfo->bcc != pInfo->last_rx) {
488 TRACE_PE("checksum error - got %x but expected %x",
489 pInfo->last_rx, pInfo->bcc);
490 pInfo->flags |= R3964_CHECKSUM;
491 }
492 }
493
494 /* check for errors (parity, overrun,...): */
495 if (pInfo->flags & R3964_ERROR) {
496 TRACE_PE("on_receive_block - transmission failed error %x",
497 pInfo->flags & R3964_ERROR);
498
499 put_char(pInfo, NAK);
500 flush(pInfo);
501 if (pInfo->nRetry < R3964_MAX_RETRIES) {
502 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
503 pInfo->nRetry++;
504 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
505 } else {
506 TRACE_PE("on_receive_block - failed after max retries");
507 pInfo->state = R3964_IDLE;
508 }
509 return;
510 }
511
512 /* received block; submit DLE: */
513 put_char(pInfo, DLE);
514 flush(pInfo);
515 del_timer_sync(&pInfo->tmr);
516 TRACE_PS(" rx success: got %d chars", length);
517
518 /* prepare struct r3964_block_header: */
519 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
520 GFP_KERNEL);
521 TRACE_M("on_receive_block - kmalloc %p", pBlock);
522
523 if (pBlock == NULL)
524 return;
525
526 pBlock->length = length;
527 pBlock->data = ((unsigned char *)pBlock) +
528 sizeof(struct r3964_block_header);
529 pBlock->locks = 0;
530 pBlock->next = NULL;
531 pBlock->owner = NULL;
532
533 memcpy(pBlock->data, pInfo->rx_buf, length);
534
535 /* queue block into rx_queue: */
536 add_rx_queue(pInfo, pBlock);
537
538 /* notify attached client processes: */
539 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
540 if (pClient->sig_flags & R3964_SIG_DATA) {
541 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
542 pBlock);
543 }
544 }
545 wake_up_interruptible(&pInfo->read_wait);
546
547 pInfo->state = R3964_IDLE;
548
549 trigger_transmit(pInfo);
550}
551
552static void receive_char(struct r3964_info *pInfo, const unsigned char c)
553{
554 switch (pInfo->state) {
555 case R3964_TX_REQUEST:
556 if (c == DLE) {
557 TRACE_PS("TX_REQUEST - got DLE");
558
559 pInfo->state = R3964_TRANSMITTING;
560 pInfo->tx_position = 0;
561
562 transmit_block(pInfo);
563 } else if (c == STX) {
564 if (pInfo->nRetry == 0) {
565 TRACE_PE("TX_REQUEST - init conflict");
566 if (pInfo->priority == R3964_SLAVE) {
567 goto start_receiving;
568 }
569 } else {
570 TRACE_PE("TX_REQUEST - secondary init "
571 "conflict!? Switching to SLAVE mode "
572 "for next rx.");
573 goto start_receiving;
574 }
575 } else {
576 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
577 retry_transmit(pInfo);
578 }
579 break;
580 case R3964_TRANSMITTING:
581 if (c == NAK) {
582 TRACE_PE("TRANSMITTING - got NAK");
583 retry_transmit(pInfo);
584 } else {
585 TRACE_PE("TRANSMITTING - got invalid char");
586
587 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
588 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
589 }
590 break;
591 case R3964_WAIT_FOR_TX_ACK:
592 if (c == DLE) {
593 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
594 remove_from_tx_queue(pInfo, R3964_OK);
595
596 pInfo->state = R3964_IDLE;
597 trigger_transmit(pInfo);
598 } else {
599 retry_transmit(pInfo);
600 }
601 break;
602 case R3964_WAIT_FOR_RX_REPEAT:
603 /* FALLTHROUGH */
604 case R3964_IDLE:
605 if (c == STX) {
606 /* Prevent rx_queue from overflow: */
607 if (pInfo->blocks_in_rx_queue >=
608 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
609 TRACE_PE("IDLE - got STX but no space in "
610 "rx_queue!");
611 pInfo->state = R3964_WAIT_FOR_RX_BUF;
612 mod_timer(&pInfo->tmr,
613 jiffies + R3964_TO_NO_BUF);
614 break;
615 }
616start_receiving:
617 /* Ok, start receiving: */
618 TRACE_PS("IDLE - got STX");
619 pInfo->rx_position = 0;
620 pInfo->last_rx = 0;
621 pInfo->flags &= ~R3964_ERROR;
622 pInfo->state = R3964_RECEIVING;
623 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
624 pInfo->nRetry = 0;
625 put_char(pInfo, DLE);
626 flush(pInfo);
627 pInfo->bcc = 0;
628 }
629 break;
630 case R3964_RECEIVING:
631 if (pInfo->rx_position < RX_BUF_SIZE) {
632 pInfo->bcc ^= c;
633
634 if (c == DLE) {
635 if (pInfo->last_rx == DLE) {
636 pInfo->last_rx = 0;
637 goto char_to_buf;
638 }
639 pInfo->last_rx = DLE;
640 break;
641 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
642 if (pInfo->flags & R3964_BCC) {
643 pInfo->state = R3964_WAIT_FOR_BCC;
644 mod_timer(&pInfo->tmr,
645 jiffies + R3964_TO_ZVZ);
646 } else {
647 on_receive_block(pInfo);
648 }
649 } else {
650 pInfo->last_rx = c;
651char_to_buf:
652 pInfo->rx_buf[pInfo->rx_position++] = c;
653 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
654 }
655 }
656 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
657 break;
658 case R3964_WAIT_FOR_BCC:
659 pInfo->last_rx = c;
660 on_receive_block(pInfo);
661 break;
662 }
663}
664
665static void receive_error(struct r3964_info *pInfo, const char flag)
666{
667 switch (flag) {
668 case TTY_NORMAL:
669 break;
670 case TTY_BREAK:
671 TRACE_PE("received break");
672 pInfo->flags |= R3964_BREAK;
673 break;
674 case TTY_PARITY:
675 TRACE_PE("parity error");
676 pInfo->flags |= R3964_PARITY;
677 break;
678 case TTY_FRAME:
679 TRACE_PE("frame error");
680 pInfo->flags |= R3964_FRAME;
681 break;
682 case TTY_OVERRUN:
683 TRACE_PE("frame overrun");
684 pInfo->flags |= R3964_OVERRUN;
685 break;
686 default:
687 TRACE_PE("receive_error - unknown flag %d", flag);
688 pInfo->flags |= R3964_UNKNOWN;
689 break;
690 }
691}
692
693static void on_timeout(unsigned long priv)
694{
695 struct r3964_info *pInfo = (void *)priv;
696
697 switch (pInfo->state) {
698 case R3964_TX_REQUEST:
699 TRACE_PE("TX_REQUEST - timeout");
700 retry_transmit(pInfo);
701 break;
702 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
703 put_char(pInfo, NAK);
704 flush(pInfo);
705 retry_transmit(pInfo);
706 break;
707 case R3964_WAIT_FOR_TX_ACK:
708 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
709 retry_transmit(pInfo);
710 break;
711 case R3964_WAIT_FOR_RX_BUF:
712 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
713 put_char(pInfo, NAK);
714 flush(pInfo);
715 pInfo->state = R3964_IDLE;
716 break;
717 case R3964_RECEIVING:
718 TRACE_PE("RECEIVING - timeout after %d chars",
719 pInfo->rx_position);
720 put_char(pInfo, NAK);
721 flush(pInfo);
722 pInfo->state = R3964_IDLE;
723 break;
724 case R3964_WAIT_FOR_RX_REPEAT:
725 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
726 pInfo->state = R3964_IDLE;
727 break;
728 case R3964_WAIT_FOR_BCC:
729 TRACE_PE("WAIT_FOR_BCC - timeout");
730 put_char(pInfo, NAK);
731 flush(pInfo);
732 pInfo->state = R3964_IDLE;
733 break;
734 }
735}
736
737static struct r3964_client_info *findClient(struct r3964_info *pInfo,
738 struct pid *pid)
739{
740 struct r3964_client_info *pClient;
741
742 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
743 if (pClient->pid == pid) {
744 return pClient;
745 }
746 }
747 return NULL;
748}
749
750static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
751{
752 struct r3964_client_info *pClient;
753 struct r3964_client_info **ppClient;
754 struct r3964_message *pMsg;
755
756 if ((arg & R3964_SIG_ALL) == 0) {
757 /* Remove client from client list */
758 for (ppClient = &pInfo->firstClient; *ppClient;
759 ppClient = &(*ppClient)->next) {
760 pClient = *ppClient;
761
762 if (pClient->pid == pid) {
763 TRACE_PS("removing client %d from client list",
764 pid_nr(pid));
765 *ppClient = pClient->next;
766 while (pClient->msg_count) {
767 pMsg = remove_msg(pInfo, pClient);
768 if (pMsg) {
769 kfree(pMsg);
770 TRACE_M("enable_signals - msg "
771 "kfree %p", pMsg);
772 }
773 }
774 put_pid(pClient->pid);
775 kfree(pClient);
776 TRACE_M("enable_signals - kfree %p", pClient);
777 return 0;
778 }
779 }
780 return -EINVAL;
781 } else {
782 pClient = findClient(pInfo, pid);
783 if (pClient) {
784 /* update signal options */
785 pClient->sig_flags = arg;
786 } else {
787 /* add client to client list */
788 pClient = kmalloc(sizeof(struct r3964_client_info),
789 GFP_KERNEL);
790 TRACE_M("enable_signals - kmalloc %p", pClient);
791 if (pClient == NULL)
792 return -ENOMEM;
793
794 TRACE_PS("add client %d to client list", pid_nr(pid));
795 spin_lock_init(&pClient->lock);
796 pClient->sig_flags = arg;
797 pClient->pid = get_pid(pid);
798 pClient->next = pInfo->firstClient;
799 pClient->first_msg = NULL;
800 pClient->last_msg = NULL;
801 pClient->next_block_to_read = NULL;
802 pClient->msg_count = 0;
803 pInfo->firstClient = pClient;
804 }
805 }
806
807 return 0;
808}
809
810static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
811 unsigned char __user * buf)
812{
813 struct r3964_client_info *pClient;
814 struct r3964_block_header *block;
815
816 if (!buf) {
817 return -EINVAL;
818 }
819
820 pClient = findClient(pInfo, pid);
821 if (pClient == NULL) {
822 return -EINVAL;
823 }
824
825 block = pClient->next_block_to_read;
826 if (!block) {
827 return 0;
828 } else {
829 if (copy_to_user(buf, block->data, block->length))
830 return -EFAULT;
831
832 remove_client_block(pInfo, pClient);
833 return block->length;
834 }
835
836 return -EINVAL;
837}
838
839static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
840 int error_code, struct r3964_block_header *pBlock)
841{
842 struct r3964_message *pMsg;
843 unsigned long flags;
844
845 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
846queue_the_message:
847
848 pMsg = kmalloc(sizeof(struct r3964_message),
849 error_code ? GFP_ATOMIC : GFP_KERNEL);
850 TRACE_M("add_msg - kmalloc %p", pMsg);
851 if (pMsg == NULL) {
852 return;
853 }
854
855 spin_lock_irqsave(&pClient->lock, flags);
856
857 pMsg->msg_id = msg_id;
858 pMsg->arg = arg;
859 pMsg->error_code = error_code;
860 pMsg->block = pBlock;
861 pMsg->next = NULL;
862
863 if (pClient->last_msg == NULL) {
864 pClient->first_msg = pClient->last_msg = pMsg;
865 } else {
866 pClient->last_msg->next = pMsg;
867 pClient->last_msg = pMsg;
868 }
869
870 pClient->msg_count++;
871
872 if (pBlock != NULL) {
873 pBlock->locks++;
874 }
875 spin_unlock_irqrestore(&pClient->lock, flags);
876 } else {
877 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
878 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
879 pClient->last_msg->arg++;
880 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
881 } else {
882 msg_id = R3964_MSG_ACK;
883 arg = 0;
884 error_code = R3964_OVERFLOW;
885 pBlock = NULL;
886 TRACE_PE("add_msg - queue OVERFLOW-msg");
887 goto queue_the_message;
888 }
889 }
890 /* Send SIGIO signal to client process: */
891 if (pClient->sig_flags & R3964_USE_SIGIO) {
892 kill_pid(pClient->pid, SIGIO, 1);
893 }
894}
895
896static struct r3964_message *remove_msg(struct r3964_info *pInfo,
897 struct r3964_client_info *pClient)
898{
899 struct r3964_message *pMsg = NULL;
900 unsigned long flags;
901
902 if (pClient->first_msg) {
903 spin_lock_irqsave(&pClient->lock, flags);
904
905 pMsg = pClient->first_msg;
906 pClient->first_msg = pMsg->next;
907 if (pClient->first_msg == NULL) {
908 pClient->last_msg = NULL;
909 }
910
911 pClient->msg_count--;
912 if (pMsg->block) {
913 remove_client_block(pInfo, pClient);
914 pClient->next_block_to_read = pMsg->block;
915 }
916 spin_unlock_irqrestore(&pClient->lock, flags);
917 }
918 return pMsg;
919}
920
921static void remove_client_block(struct r3964_info *pInfo,
922 struct r3964_client_info *pClient)
923{
924 struct r3964_block_header *block;
925
926 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
927
928 block = pClient->next_block_to_read;
929 if (block) {
930 block->locks--;
931 if (block->locks == 0) {
932 remove_from_rx_queue(pInfo, block);
933 }
934 }
935 pClient->next_block_to_read = NULL;
936}
937
938/*************************************************************
939 * Line discipline routines
940 *************************************************************/
941
942static int r3964_open(struct tty_struct *tty)
943{
944 struct r3964_info *pInfo;
945
946 TRACE_L("open");
947 TRACE_L("tty=%p, PID=%d, disc_data=%p",
948 tty, current->pid, tty->disc_data);
949
950 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
951 TRACE_M("r3964_open - info kmalloc %p", pInfo);
952
953 if (!pInfo) {
954 printk(KERN_ERR "r3964: failed to alloc info structure\n");
955 return -ENOMEM;
956 }
957
958 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
959 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
960
961 if (!pInfo->rx_buf) {
962 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
963 kfree(pInfo);
964 TRACE_M("r3964_open - info kfree %p", pInfo);
965 return -ENOMEM;
966 }
967
968 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
969 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
970
971 if (!pInfo->tx_buf) {
972 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
973 kfree(pInfo->rx_buf);
974 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
975 kfree(pInfo);
976 TRACE_M("r3964_open - info kfree %p", pInfo);
977 return -ENOMEM;
978 }
979
980 spin_lock_init(&pInfo->lock);
981 pInfo->tty = tty;
982 init_waitqueue_head(&pInfo->read_wait);
983 pInfo->priority = R3964_MASTER;
984 pInfo->rx_first = pInfo->rx_last = NULL;
985 pInfo->tx_first = pInfo->tx_last = NULL;
986 pInfo->rx_position = 0;
987 pInfo->tx_position = 0;
988 pInfo->last_rx = 0;
989 pInfo->blocks_in_rx_queue = 0;
990 pInfo->firstClient = NULL;
991 pInfo->state = R3964_IDLE;
992 pInfo->flags = R3964_DEBUG;
993 pInfo->nRetry = 0;
994
995 tty->disc_data = pInfo;
996 tty->receive_room = 65536;
997
998 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
999
1000 return 0;
1001}
1002
1003static void r3964_close(struct tty_struct *tty)
1004{
1005 struct r3964_info *pInfo = tty->disc_data;
1006 struct r3964_client_info *pClient, *pNext;
1007 struct r3964_message *pMsg;
1008 struct r3964_block_header *pHeader, *pNextHeader;
1009 unsigned long flags;
1010
1011 TRACE_L("close");
1012
1013 /*
1014 * Make sure that our task queue isn't activated. If it
1015 * is, take it out of the linked list.
1016 */
1017 del_timer_sync(&pInfo->tmr);
1018
1019 /* Remove client-structs and message queues: */
1020 pClient = pInfo->firstClient;
1021 while (pClient) {
1022 pNext = pClient->next;
1023 while (pClient->msg_count) {
1024 pMsg = remove_msg(pInfo, pClient);
1025 if (pMsg) {
1026 kfree(pMsg);
1027 TRACE_M("r3964_close - msg kfree %p", pMsg);
1028 }
1029 }
1030 put_pid(pClient->pid);
1031 kfree(pClient);
1032 TRACE_M("r3964_close - client kfree %p", pClient);
1033 pClient = pNext;
1034 }
1035 /* Remove jobs from tx_queue: */
1036 spin_lock_irqsave(&pInfo->lock, flags);
1037 pHeader = pInfo->tx_first;
1038 pInfo->tx_first = pInfo->tx_last = NULL;
1039 spin_unlock_irqrestore(&pInfo->lock, flags);
1040
1041 while (pHeader) {
1042 pNextHeader = pHeader->next;
1043 kfree(pHeader);
1044 pHeader = pNextHeader;
1045 }
1046
1047 /* Free buffers: */
1048 wake_up_interruptible(&pInfo->read_wait);
1049 kfree(pInfo->rx_buf);
1050 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1051 kfree(pInfo->tx_buf);
1052 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1053 kfree(pInfo);
1054 TRACE_M("r3964_close - info kfree %p", pInfo);
1055}
1056
1057static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1058 unsigned char __user * buf, size_t nr)
1059{
1060 struct r3964_info *pInfo = tty->disc_data;
1061 struct r3964_client_info *pClient;
1062 struct r3964_message *pMsg;
1063 struct r3964_client_message theMsg;
1064 int ret;
1065
1066 TRACE_L("read()");
1067
1068 tty_lock();
1069
1070 pClient = findClient(pInfo, task_pid(current));
1071 if (pClient) {
1072 pMsg = remove_msg(pInfo, pClient);
1073 if (pMsg == NULL) {
1074 /* no messages available. */
1075 if (file->f_flags & O_NONBLOCK) {
1076 ret = -EAGAIN;
1077 goto unlock;
1078 }
1079 /* block until there is a message: */
1080 wait_event_interruptible_tty(pInfo->read_wait,
1081 (pMsg = remove_msg(pInfo, pClient)));
1082 }
1083
1084 /* If we still haven't got a message, we must have been signalled */
1085
1086 if (!pMsg) {
1087 ret = -EINTR;
1088 goto unlock;
1089 }
1090
1091 /* deliver msg to client process: */
1092 theMsg.msg_id = pMsg->msg_id;
1093 theMsg.arg = pMsg->arg;
1094 theMsg.error_code = pMsg->error_code;
1095 ret = sizeof(struct r3964_client_message);
1096
1097 kfree(pMsg);
1098 TRACE_M("r3964_read - msg kfree %p", pMsg);
1099
1100 if (copy_to_user(buf, &theMsg, ret)) {
1101 ret = -EFAULT;
1102 goto unlock;
1103 }
1104
1105 TRACE_PS("read - return %d", ret);
1106 goto unlock;
1107 }
1108 ret = -EPERM;
1109unlock:
1110 tty_unlock();
1111 return ret;
1112}
1113
1114static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1115 const unsigned char *data, size_t count)
1116{
1117 struct r3964_info *pInfo = tty->disc_data;
1118 struct r3964_block_header *pHeader;
1119 struct r3964_client_info *pClient;
1120 unsigned char *new_data;
1121
1122 TRACE_L("write request, %d characters", count);
1123/*
1124 * Verify the pointers
1125 */
1126
1127 if (!pInfo)
1128 return -EIO;
1129
1130/*
1131 * Ensure that the caller does not wish to send too much.
1132 */
1133 if (count > R3964_MTU) {
1134 if (pInfo->flags & R3964_DEBUG) {
1135 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1136 "packet from %u to mtu %d", count, R3964_MTU);
1137 }
1138 count = R3964_MTU;
1139 }
1140/*
1141 * Allocate a buffer for the data and copy it from the buffer with header prepended
1142 */
1143 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1144 GFP_KERNEL);
1145 TRACE_M("r3964_write - kmalloc %p", new_data);
1146 if (new_data == NULL) {
1147 if (pInfo->flags & R3964_DEBUG) {
1148 printk(KERN_ERR "r3964_write: no memory\n");
1149 }
1150 return -ENOSPC;
1151 }
1152
1153 pHeader = (struct r3964_block_header *)new_data;
1154 pHeader->data = new_data + sizeof(struct r3964_block_header);
1155 pHeader->length = count;
1156 pHeader->locks = 0;
1157 pHeader->owner = NULL;
1158
1159 tty_lock();
1160
1161 pClient = findClient(pInfo, task_pid(current));
1162 if (pClient) {
1163 pHeader->owner = pClient;
1164 }
1165
1166 memcpy(pHeader->data, data, count); /* We already verified this */
1167
1168 if (pInfo->flags & R3964_DEBUG) {
1169 dump_block(pHeader->data, count);
1170 }
1171
1172/*
1173 * Add buffer to transmit-queue:
1174 */
1175 add_tx_queue(pInfo, pHeader);
1176 trigger_transmit(pInfo);
1177
1178 tty_unlock();
1179
1180 return 0;
1181}
1182
1183static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1184 unsigned int cmd, unsigned long arg)
1185{
1186 struct r3964_info *pInfo = tty->disc_data;
1187 if (pInfo == NULL)
1188 return -EINVAL;
1189 switch (cmd) {
1190 case R3964_ENABLE_SIGNALS:
1191 return enable_signals(pInfo, task_pid(current), arg);
1192 case R3964_SETPRIORITY:
1193 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1194 return -EINVAL;
1195 pInfo->priority = arg & 0xff;
1196 return 0;
1197 case R3964_USE_BCC:
1198 if (arg)
1199 pInfo->flags |= R3964_BCC;
1200 else
1201 pInfo->flags &= ~R3964_BCC;
1202 return 0;
1203 case R3964_READ_TELEGRAM:
1204 return read_telegram(pInfo, task_pid(current),
1205 (unsigned char __user *)arg);
1206 default:
1207 return -ENOIOCTLCMD;
1208 }
1209}
1210
1211static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1212{
1213 TRACE_L("set_termios");
1214}
1215
1216/* Called without the kernel lock held - fine */
1217static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1218 struct poll_table_struct *wait)
1219{
1220 struct r3964_info *pInfo = tty->disc_data;
1221 struct r3964_client_info *pClient;
1222 struct r3964_message *pMsg = NULL;
1223 unsigned long flags;
1224 int result = POLLOUT;
1225
1226 TRACE_L("POLL");
1227
1228 pClient = findClient(pInfo, task_pid(current));
1229 if (pClient) {
1230 poll_wait(file, &pInfo->read_wait, wait);
1231 spin_lock_irqsave(&pInfo->lock, flags);
1232 pMsg = pClient->first_msg;
1233 spin_unlock_irqrestore(&pInfo->lock, flags);
1234 if (pMsg)
1235 result |= POLLIN | POLLRDNORM;
1236 } else {
1237 result = -EINVAL;
1238 }
1239 return result;
1240}
1241
1242static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1243 char *fp, int count)
1244{
1245 struct r3964_info *pInfo = tty->disc_data;
1246 const unsigned char *p;
1247 char *f, flags = 0;
1248 int i;
1249
1250 for (i = count, p = cp, f = fp; i; i--, p++) {
1251 if (f)
1252 flags = *f++;
1253 if (flags == TTY_NORMAL) {
1254 receive_char(pInfo, *p);
1255 } else {
1256 receive_error(pInfo, flags);
1257 }
1258
1259 }
1260}
1261
1262MODULE_LICENSE("GPL");
1263MODULE_ALIAS_LDISC(N_R3964);
diff --git a/drivers/tty/n_tracerouter.c b/drivers/tty/n_tracerouter.c
new file mode 100644
index 000000000000..1f063d3aa32f
--- /dev/null
+++ b/drivers/tty/n_tracerouter.c
@@ -0,0 +1,243 @@
1/*
2 * n_tracerouter.c - Trace data router through tty space
3 *
4 * Copyright (C) Intel 2011
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 *
19 * This trace router uses the Linux line discipline framework to route
20 * trace data coming from a HW Modem to a PTI (Parallel Trace Module) port.
21 * The solution is not specific to a HW modem and this line disciple can
22 * be used to route any stream of data in kernel space.
23 * This is part of a solution for the P1149.7, compact JTAG, standard.
24 */
25
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/ioctl.h>
31#include <linux/tty.h>
32#include <linux/tty_ldisc.h>
33#include <linux/errno.h>
34#include <linux/string.h>
35#include <linux/mutex.h>
36#include <linux/slab.h>
37#include <asm-generic/bug.h>
38#include "n_tracesink.h"
39
40/*
41 * Other ldisc drivers use 65536 which basically means,
42 * 'I can always accept 64k' and flow control is off.
43 * This number is deemed appropriate for this driver.
44 */
45#define RECEIVE_ROOM 65536
46#define DRIVERNAME "n_tracerouter"
47
48/*
49 * struct to hold private configuration data for this ldisc.
50 * opencalled is used to hold if this ldisc has been opened.
51 * kref_tty holds the tty reference the ldisc sits on top of.
52 */
53struct tracerouter_data {
54 u8 opencalled;
55 struct tty_struct *kref_tty;
56};
57static struct tracerouter_data *tr_data;
58
59/* lock for when tty reference is being used */
60static DEFINE_MUTEX(routelock);
61
62/**
63 * n_tracerouter_open() - Called when a tty is opened by a SW entity.
64 * @tty: terminal device to the ldisc.
65 *
66 * Return:
67 * 0 for success.
68 *
69 * Caveats: This should only be opened one time per SW entity.
70 */
71static int n_tracerouter_open(struct tty_struct *tty)
72{
73 int retval = -EEXIST;
74
75 mutex_lock(&routelock);
76 if (tr_data->opencalled == 0) {
77
78 tr_data->kref_tty = tty_kref_get(tty);
79 if (tr_data->kref_tty == NULL) {
80 retval = -EFAULT;
81 } else {
82 tr_data->opencalled = 1;
83 tty->disc_data = tr_data;
84 tty->receive_room = RECEIVE_ROOM;
85 tty_driver_flush_buffer(tty);
86 retval = 0;
87 }
88 }
89 mutex_unlock(&routelock);
90 return retval;
91}
92
93/**
94 * n_tracerouter_close() - close connection
95 * @tty: terminal device to the ldisc.
96 *
97 * Called when a software entity wants to close a connection.
98 */
99static void n_tracerouter_close(struct tty_struct *tty)
100{
101 struct tracerouter_data *tptr = tty->disc_data;
102
103 mutex_lock(&routelock);
104 WARN_ON(tptr->kref_tty != tr_data->kref_tty);
105 tty_driver_flush_buffer(tty);
106 tty_kref_put(tr_data->kref_tty);
107 tr_data->kref_tty = NULL;
108 tr_data->opencalled = 0;
109 tty->disc_data = NULL;
110 mutex_unlock(&routelock);
111}
112
113/**
114 * n_tracerouter_read() - read request from user space
115 * @tty: terminal device passed into the ldisc.
116 * @file: pointer to open file object.
117 * @buf: pointer to the data buffer that gets eventually returned.
118 * @nr: number of bytes of the data buffer that is returned.
119 *
120 * function that allows read() functionality in userspace. By default if this
121 * is not implemented it returns -EIO. This module is functioning like a
122 * router via n_tracerouter_receivebuf(), and there is no real requirement
123 * to implement this function. However, an error return value other than
124 * -EIO should be used just to show that there was an intent not to have
125 * this function implemented. Return value based on read() man pages.
126 *
127 * Return:
128 * -EINVAL
129 */
130static ssize_t n_tracerouter_read(struct tty_struct *tty, struct file *file,
131 unsigned char __user *buf, size_t nr) {
132 return -EINVAL;
133}
134
135/**
136 * n_tracerouter_write() - Function that allows write() in userspace.
137 * @tty: terminal device passed into the ldisc.
138 * @file: pointer to open file object.
139 * @buf: pointer to the data buffer that gets eventually returned.
140 * @nr: number of bytes of the data buffer that is returned.
141 *
142 * By default if this is not implemented, it returns -EIO.
143 * This should not be implemented, ever, because
144 * 1. this driver is functioning like a router via
145 * n_tracerouter_receivebuf()
146 * 2. No writes to HW will ever go through this line discpline driver.
147 * However, an error return value other than -EIO should be used
148 * just to show that there was an intent not to have this function
149 * implemented. Return value based on write() man pages.
150 *
151 * Return:
152 * -EINVAL
153 */
154static ssize_t n_tracerouter_write(struct tty_struct *tty, struct file *file,
155 const unsigned char *buf, size_t nr) {
156 return -EINVAL;
157}
158
159/**
160 * n_tracerouter_receivebuf() - Routing function for driver.
161 * @tty: terminal device passed into the ldisc. It's assumed
162 * tty will never be NULL.
163 * @cp: buffer, block of characters to be eventually read by
164 * someone, somewhere (user read() call or some kernel function).
165 * @fp: flag buffer.
166 * @count: number of characters (aka, bytes) in cp.
167 *
168 * This function takes the input buffer, cp, and passes it to
169 * an external API function for processing.
170 */
171static void n_tracerouter_receivebuf(struct tty_struct *tty,
172 const unsigned char *cp,
173 char *fp, int count)
174{
175 mutex_lock(&routelock);
176 n_tracesink_datadrain((u8 *) cp, count);
177 mutex_unlock(&routelock);
178}
179
180/*
181 * Flush buffer is not impelemented as the ldisc has no internal buffering
182 * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
183 */
184
185static struct tty_ldisc_ops tty_ptirouter_ldisc = {
186 .owner = THIS_MODULE,
187 .magic = TTY_LDISC_MAGIC,
188 .name = DRIVERNAME,
189 .open = n_tracerouter_open,
190 .close = n_tracerouter_close,
191 .read = n_tracerouter_read,
192 .write = n_tracerouter_write,
193 .receive_buf = n_tracerouter_receivebuf
194};
195
196/**
197 * n_tracerouter_init - module initialisation
198 *
199 * Registers this module as a line discipline driver.
200 *
201 * Return:
202 * 0 for success, any other value error.
203 */
204static int __init n_tracerouter_init(void)
205{
206 int retval;
207
208 tr_data = kzalloc(sizeof(struct tracerouter_data), GFP_KERNEL);
209 if (tr_data == NULL)
210 return -ENOMEM;
211
212
213 /* Note N_TRACEROUTER is defined in linux/tty.h */
214 retval = tty_register_ldisc(N_TRACEROUTER, &tty_ptirouter_ldisc);
215 if (retval < 0) {
216 pr_err("%s: Registration failed: %d\n", __func__, retval);
217 kfree(tr_data);
218 }
219 return retval;
220}
221
222/**
223 * n_tracerouter_exit - module unload
224 *
225 * Removes this module as a line discipline driver.
226 */
227static void __exit n_tracerouter_exit(void)
228{
229 int retval = tty_unregister_ldisc(N_TRACEROUTER);
230
231 if (retval < 0)
232 pr_err("%s: Unregistration failed: %d\n", __func__, retval);
233 else
234 kfree(tr_data);
235}
236
237module_init(n_tracerouter_init);
238module_exit(n_tracerouter_exit);
239
240MODULE_LICENSE("GPL");
241MODULE_AUTHOR("Jay Freyensee");
242MODULE_ALIAS_LDISC(N_TRACEROUTER);
243MODULE_DESCRIPTION("Trace router ldisc driver");
diff --git a/drivers/tty/n_tracesink.c b/drivers/tty/n_tracesink.c
new file mode 100644
index 000000000000..ddce58b973d2
--- /dev/null
+++ b/drivers/tty/n_tracesink.c
@@ -0,0 +1,238 @@
1/*
2 * n_tracesink.c - Trace data router and sink path through tty space.
3 *
4 * Copyright (C) Intel 2011
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 *
19 * The trace sink uses the Linux line discipline framework to receive
20 * trace data coming from the PTI source line discipline driver
21 * to a user-desired tty port, like USB.
22 * This is to provide a way to extract modem trace data on
23 * devices that do not have a PTI HW module, or just need modem
24 * trace data to come out of a different HW output port.
25 * This is part of a solution for the P1149.7, compact JTAG, standard.
26 */
27
28#include <linux/init.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/ioctl.h>
33#include <linux/tty.h>
34#include <linux/tty_ldisc.h>
35#include <linux/errno.h>
36#include <linux/string.h>
37#include <asm-generic/bug.h>
38#include "n_tracesink.h"
39
40/*
41 * Other ldisc drivers use 65536 which basically means,
42 * 'I can always accept 64k' and flow control is off.
43 * This number is deemed appropriate for this driver.
44 */
45#define RECEIVE_ROOM 65536
46#define DRIVERNAME "n_tracesink"
47
48/*
49 * there is a quirk with this ldisc is he can write data
50 * to a tty from anyone calling his kernel API, which
51 * meets customer requirements in the drivers/misc/pti.c
52 * project. So he needs to know when he can and cannot write when
53 * the API is called. In theory, the API can be called
54 * after an init() but before a successful open() which
55 * would crash the system if tty is not checked.
56 */
57static struct tty_struct *this_tty;
58static DEFINE_MUTEX(writelock);
59
60/**
61 * n_tracesink_open() - Called when a tty is opened by a SW entity.
62 * @tty: terminal device to the ldisc.
63 *
64 * Return:
65 * 0 for success,
66 * -EFAULT = couldn't get a tty kref n_tracesink will sit
67 * on top of
68 * -EEXIST = open() called successfully once and it cannot
69 * be called again.
70 *
71 * Caveats: open() should only be successful the first time a
72 * SW entity calls it.
73 */
74static int n_tracesink_open(struct tty_struct *tty)
75{
76 int retval = -EEXIST;
77
78 mutex_lock(&writelock);
79 if (this_tty == NULL) {
80 this_tty = tty_kref_get(tty);
81 if (this_tty == NULL) {
82 retval = -EFAULT;
83 } else {
84 tty->disc_data = this_tty;
85 tty_driver_flush_buffer(tty);
86 retval = 0;
87 }
88 }
89 mutex_unlock(&writelock);
90
91 return retval;
92}
93
94/**
95 * n_tracesink_close() - close connection
96 * @tty: terminal device to the ldisc.
97 *
98 * Called when a software entity wants to close a connection.
99 */
100static void n_tracesink_close(struct tty_struct *tty)
101{
102 mutex_lock(&writelock);
103 tty_driver_flush_buffer(tty);
104 tty_kref_put(this_tty);
105 this_tty = NULL;
106 tty->disc_data = NULL;
107 mutex_unlock(&writelock);
108}
109
110/**
111 * n_tracesink_read() - read request from user space
112 * @tty: terminal device passed into the ldisc.
113 * @file: pointer to open file object.
114 * @buf: pointer to the data buffer that gets eventually returned.
115 * @nr: number of bytes of the data buffer that is returned.
116 *
117 * function that allows read() functionality in userspace. By default if this
118 * is not implemented it returns -EIO. This module is functioning like a
119 * router via n_tracesink_receivebuf(), and there is no real requirement
120 * to implement this function. However, an error return value other than
121 * -EIO should be used just to show that there was an intent not to have
122 * this function implemented. Return value based on read() man pages.
123 *
124 * Return:
125 * -EINVAL
126 */
127static ssize_t n_tracesink_read(struct tty_struct *tty, struct file *file,
128 unsigned char __user *buf, size_t nr) {
129 return -EINVAL;
130}
131
132/**
133 * n_tracesink_write() - Function that allows write() in userspace.
134 * @tty: terminal device passed into the ldisc.
135 * @file: pointer to open file object.
136 * @buf: pointer to the data buffer that gets eventually returned.
137 * @nr: number of bytes of the data buffer that is returned.
138 *
139 * By default if this is not implemented, it returns -EIO.
140 * This should not be implemented, ever, because
141 * 1. this driver is functioning like a router via
142 * n_tracesink_receivebuf()
143 * 2. No writes to HW will ever go through this line discpline driver.
144 * However, an error return value other than -EIO should be used
145 * just to show that there was an intent not to have this function
146 * implemented. Return value based on write() man pages.
147 *
148 * Return:
149 * -EINVAL
150 */
151static ssize_t n_tracesink_write(struct tty_struct *tty, struct file *file,
152 const unsigned char *buf, size_t nr) {
153 return -EINVAL;
154}
155
156/**
157 * n_tracesink_datadrain() - Kernel API function used to route
158 * trace debugging data to user-defined
159 * port like USB.
160 *
161 * @buf: Trace debuging data buffer to write to tty target
162 * port. Null value will return with no write occurring.
163 * @count: Size of buf. Value of 0 or a negative number will
164 * return with no write occuring.
165 *
166 * Caveat: If this line discipline does not set the tty it sits
167 * on top of via an open() call, this API function will not
168 * call the tty's write() call because it will have no pointer
169 * to call the write().
170 */
171void n_tracesink_datadrain(u8 *buf, int count)
172{
173 mutex_lock(&writelock);
174
175 if ((buf != NULL) && (count > 0) && (this_tty != NULL))
176 this_tty->ops->write(this_tty, buf, count);
177
178 mutex_unlock(&writelock);
179}
180EXPORT_SYMBOL_GPL(n_tracesink_datadrain);
181
182/*
183 * Flush buffer is not impelemented as the ldisc has no internal buffering
184 * so the tty_driver_flush_buffer() is sufficient for this driver's needs.
185 */
186
187/*
188 * tty_ldisc function operations for this driver.
189 */
190static struct tty_ldisc_ops tty_n_tracesink = {
191 .owner = THIS_MODULE,
192 .magic = TTY_LDISC_MAGIC,
193 .name = DRIVERNAME,
194 .open = n_tracesink_open,
195 .close = n_tracesink_close,
196 .read = n_tracesink_read,
197 .write = n_tracesink_write
198};
199
200/**
201 * n_tracesink_init- module initialisation
202 *
203 * Registers this module as a line discipline driver.
204 *
205 * Return:
206 * 0 for success, any other value error.
207 */
208static int __init n_tracesink_init(void)
209{
210 /* Note N_TRACESINK is defined in linux/tty.h */
211 int retval = tty_register_ldisc(N_TRACESINK, &tty_n_tracesink);
212
213 if (retval < 0)
214 pr_err("%s: Registration failed: %d\n", __func__, retval);
215
216 return retval;
217}
218
219/**
220 * n_tracesink_exit - module unload
221 *
222 * Removes this module as a line discipline driver.
223 */
224static void __exit n_tracesink_exit(void)
225{
226 int retval = tty_unregister_ldisc(N_TRACESINK);
227
228 if (retval < 0)
229 pr_err("%s: Unregistration failed: %d\n", __func__, retval);
230}
231
232module_init(n_tracesink_init);
233module_exit(n_tracesink_exit);
234
235MODULE_LICENSE("GPL");
236MODULE_AUTHOR("Jay Freyensee");
237MODULE_ALIAS_LDISC(N_TRACESINK);
238MODULE_DESCRIPTION("Trace sink ldisc driver");
diff --git a/drivers/tty/n_tracesink.h b/drivers/tty/n_tracesink.h
new file mode 100644
index 000000000000..a68bb44f1ef5
--- /dev/null
+++ b/drivers/tty/n_tracesink.h
@@ -0,0 +1,36 @@
1/*
2 * n_tracesink.h - Kernel driver API to route trace data in kernel space.
3 *
4 * Copyright (C) Intel 2011
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 *
19 * The PTI (Parallel Trace Interface) driver directs trace data routed from
20 * various parts in the system out through the Intel Penwell PTI port and
21 * out of the mobile device for analysis with a debugging tool
22 * (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7,
23 * compact JTAG, standard.
24 *
25 * This header file is used by n_tracerouter to be able to send the
26 * data of it's tty port to the tty port this module sits. This
27 * mechanism can also be used independent of the PTI module.
28 *
29 */
30
31#ifndef N_TRACESINK_H_
32#define N_TRACESINK_H_
33
34void n_tracesink_datadrain(u8 *buf, int count);
35
36#endif
diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
new file mode 100644
index 000000000000..c3954fbf6ac4
--- /dev/null
+++ b/drivers/tty/n_tty.c
@@ -0,0 +1,2128 @@
1/*
2 * n_tty.c --- implements the N_TTY line discipline.
3 *
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
14 *
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 *
18 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
30 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
48#include <linux/audit.h>
49#include <linux/file.h>
50#include <linux/uaccess.h>
51#include <linux/module.h>
52
53#include <asm/system.h>
54
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64#define TTY_THRESHOLD_UNTHROTTLE 128
65
66/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
77static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
78 unsigned char __user *ptr)
79{
80 tty_audit_add_data(tty, &x, 1);
81 return put_user(x, ptr);
82}
83
84/**
85 * n_tty_set__room - receive space
86 * @tty: terminal
87 *
88 * Called by the driver to find out how much data it is
89 * permitted to feed to the line discipline without any being lost
90 * and thus to manage flow control. Not serialized. Answers for the
91 * "instant".
92 */
93
94static void n_tty_set_room(struct tty_struct *tty)
95{
96 /* tty->read_cnt is not read locked ? */
97 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
98 int old_left;
99
100 /*
101 * If we are doing input canonicalization, and there are no
102 * pending newlines, let characters through without limit, so
103 * that erase characters will be handled. Other excess
104 * characters will be beeped.
105 */
106 if (left <= 0)
107 left = tty->icanon && !tty->canon_data;
108 old_left = tty->receive_room;
109 tty->receive_room = left;
110
111 /* Did this open up the receive buffer? We may need to flip */
112 if (left && !old_left)
113 schedule_work(&tty->buf.work);
114}
115
116static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
117{
118 if (tty->read_cnt < N_TTY_BUF_SIZE) {
119 tty->read_buf[tty->read_head] = c;
120 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
121 tty->read_cnt++;
122 }
123}
124
125/**
126 * put_tty_queue - add character to tty
127 * @c: character
128 * @tty: tty device
129 *
130 * Add a character to the tty read_buf queue. This is done under the
131 * read_lock to serialize character addition and also to protect us
132 * against parallel reads or flushes
133 */
134
135static void put_tty_queue(unsigned char c, struct tty_struct *tty)
136{
137 unsigned long flags;
138 /*
139 * The problem of stomping on the buffers ends here.
140 * Why didn't anyone see this one coming? --AJK
141 */
142 spin_lock_irqsave(&tty->read_lock, flags);
143 put_tty_queue_nolock(c, tty);
144 spin_unlock_irqrestore(&tty->read_lock, flags);
145}
146
147/**
148 * check_unthrottle - allow new receive data
149 * @tty; tty device
150 *
151 * Check whether to call the driver unthrottle functions
152 *
153 * Can sleep, may be called under the atomic_read_lock mutex but
154 * this is not guaranteed.
155 */
156static void check_unthrottle(struct tty_struct *tty)
157{
158 if (tty->count)
159 tty_unthrottle(tty);
160}
161
162/**
163 * reset_buffer_flags - reset buffer state
164 * @tty: terminal to reset
165 *
166 * Reset the read buffer counters, clear the flags,
167 * and make sure the driver is unthrottled. Called
168 * from n_tty_open() and n_tty_flush_buffer().
169 *
170 * Locking: tty_read_lock for read fields.
171 */
172
173static void reset_buffer_flags(struct tty_struct *tty)
174{
175 unsigned long flags;
176
177 spin_lock_irqsave(&tty->read_lock, flags);
178 tty->read_head = tty->read_tail = tty->read_cnt = 0;
179 spin_unlock_irqrestore(&tty->read_lock, flags);
180
181 mutex_lock(&tty->echo_lock);
182 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
183 mutex_unlock(&tty->echo_lock);
184
185 tty->canon_head = tty->canon_data = tty->erasing = 0;
186 memset(&tty->read_flags, 0, sizeof tty->read_flags);
187 n_tty_set_room(tty);
188 check_unthrottle(tty);
189}
190
191/**
192 * n_tty_flush_buffer - clean input queue
193 * @tty: terminal device
194 *
195 * Flush the input buffer. Called when the line discipline is
196 * being closed, when the tty layer wants the buffer flushed (eg
197 * at hangup) or when the N_TTY line discipline internally has to
198 * clean the pending queue (for example some signals).
199 *
200 * Locking: ctrl_lock, read_lock.
201 */
202
203static void n_tty_flush_buffer(struct tty_struct *tty)
204{
205 unsigned long flags;
206 /* clear everything and unthrottle the driver */
207 reset_buffer_flags(tty);
208
209 if (!tty->link)
210 return;
211
212 spin_lock_irqsave(&tty->ctrl_lock, flags);
213 if (tty->link->packet) {
214 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
215 wake_up_interruptible(&tty->link->read_wait);
216 }
217 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
218}
219
220/**
221 * n_tty_chars_in_buffer - report available bytes
222 * @tty: tty device
223 *
224 * Report the number of characters buffered to be delivered to user
225 * at this instant in time.
226 *
227 * Locking: read_lock
228 */
229
230static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
231{
232 unsigned long flags;
233 ssize_t n = 0;
234
235 spin_lock_irqsave(&tty->read_lock, flags);
236 if (!tty->icanon) {
237 n = tty->read_cnt;
238 } else if (tty->canon_data) {
239 n = (tty->canon_head > tty->read_tail) ?
240 tty->canon_head - tty->read_tail :
241 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
242 }
243 spin_unlock_irqrestore(&tty->read_lock, flags);
244 return n;
245}
246
247/**
248 * is_utf8_continuation - utf8 multibyte check
249 * @c: byte to check
250 *
251 * Returns true if the utf8 character 'c' is a multibyte continuation
252 * character. We use this to correctly compute the on screen size
253 * of the character when printing
254 */
255
256static inline int is_utf8_continuation(unsigned char c)
257{
258 return (c & 0xc0) == 0x80;
259}
260
261/**
262 * is_continuation - multibyte check
263 * @c: byte to check
264 *
265 * Returns true if the utf8 character 'c' is a multibyte continuation
266 * character and the terminal is in unicode mode.
267 */
268
269static inline int is_continuation(unsigned char c, struct tty_struct *tty)
270{
271 return I_IUTF8(tty) && is_utf8_continuation(c);
272}
273
274/**
275 * do_output_char - output one character
276 * @c: character (or partial unicode symbol)
277 * @tty: terminal device
278 * @space: space available in tty driver write buffer
279 *
280 * This is a helper function that handles one output character
281 * (including special characters like TAB, CR, LF, etc.),
282 * doing OPOST processing and putting the results in the
283 * tty driver's write buffer.
284 *
285 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
286 * and NLDLY. They simply aren't relevant in the world today.
287 * If you ever need them, add them here.
288 *
289 * Returns the number of bytes of buffer space used or -1 if
290 * no space left.
291 *
292 * Locking: should be called under the output_lock to protect
293 * the column state and space left in the buffer
294 */
295
296static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
297{
298 int spaces;
299
300 if (!space)
301 return -1;
302
303 switch (c) {
304 case '\n':
305 if (O_ONLRET(tty))
306 tty->column = 0;
307 if (O_ONLCR(tty)) {
308 if (space < 2)
309 return -1;
310 tty->canon_column = tty->column = 0;
311 tty->ops->write(tty, "\r\n", 2);
312 return 2;
313 }
314 tty->canon_column = tty->column;
315 break;
316 case '\r':
317 if (O_ONOCR(tty) && tty->column == 0)
318 return 0;
319 if (O_OCRNL(tty)) {
320 c = '\n';
321 if (O_ONLRET(tty))
322 tty->canon_column = tty->column = 0;
323 break;
324 }
325 tty->canon_column = tty->column = 0;
326 break;
327 case '\t':
328 spaces = 8 - (tty->column & 7);
329 if (O_TABDLY(tty) == XTABS) {
330 if (space < spaces)
331 return -1;
332 tty->column += spaces;
333 tty->ops->write(tty, " ", spaces);
334 return spaces;
335 }
336 tty->column += spaces;
337 break;
338 case '\b':
339 if (tty->column > 0)
340 tty->column--;
341 break;
342 default:
343 if (!iscntrl(c)) {
344 if (O_OLCUC(tty))
345 c = toupper(c);
346 if (!is_continuation(c, tty))
347 tty->column++;
348 }
349 break;
350 }
351
352 tty_put_char(tty, c);
353 return 1;
354}
355
356/**
357 * process_output - output post processor
358 * @c: character (or partial unicode symbol)
359 * @tty: terminal device
360 *
361 * Output one character with OPOST processing.
362 * Returns -1 when the output device is full and the character
363 * must be retried.
364 *
365 * Locking: output_lock to protect column state and space left
366 * (also, this is called from n_tty_write under the
367 * tty layer write lock)
368 */
369
370static int process_output(unsigned char c, struct tty_struct *tty)
371{
372 int space, retval;
373
374 mutex_lock(&tty->output_lock);
375
376 space = tty_write_room(tty);
377 retval = do_output_char(c, tty, space);
378
379 mutex_unlock(&tty->output_lock);
380 if (retval < 0)
381 return -1;
382 else
383 return 0;
384}
385
386/**
387 * process_output_block - block post processor
388 * @tty: terminal device
389 * @buf: character buffer
390 * @nr: number of bytes to output
391 *
392 * Output a block of characters with OPOST processing.
393 * Returns the number of characters output.
394 *
395 * This path is used to speed up block console writes, among other
396 * things when processing blocks of output data. It handles only
397 * the simple cases normally found and helps to generate blocks of
398 * symbols for the console driver and thus improve performance.
399 *
400 * Locking: output_lock to protect column state and space left
401 * (also, this is called from n_tty_write under the
402 * tty layer write lock)
403 */
404
405static ssize_t process_output_block(struct tty_struct *tty,
406 const unsigned char *buf, unsigned int nr)
407{
408 int space;
409 int i;
410 const unsigned char *cp;
411
412 mutex_lock(&tty->output_lock);
413
414 space = tty_write_room(tty);
415 if (!space) {
416 mutex_unlock(&tty->output_lock);
417 return 0;
418 }
419 if (nr > space)
420 nr = space;
421
422 for (i = 0, cp = buf; i < nr; i++, cp++) {
423 unsigned char c = *cp;
424
425 switch (c) {
426 case '\n':
427 if (O_ONLRET(tty))
428 tty->column = 0;
429 if (O_ONLCR(tty))
430 goto break_out;
431 tty->canon_column = tty->column;
432 break;
433 case '\r':
434 if (O_ONOCR(tty) && tty->column == 0)
435 goto break_out;
436 if (O_OCRNL(tty))
437 goto break_out;
438 tty->canon_column = tty->column = 0;
439 break;
440 case '\t':
441 goto break_out;
442 case '\b':
443 if (tty->column > 0)
444 tty->column--;
445 break;
446 default:
447 if (!iscntrl(c)) {
448 if (O_OLCUC(tty))
449 goto break_out;
450 if (!is_continuation(c, tty))
451 tty->column++;
452 }
453 break;
454 }
455 }
456break_out:
457 i = tty->ops->write(tty, buf, i);
458
459 mutex_unlock(&tty->output_lock);
460 return i;
461}
462
463/**
464 * process_echoes - write pending echo characters
465 * @tty: terminal device
466 *
467 * Write previously buffered echo (and other ldisc-generated)
468 * characters to the tty.
469 *
470 * Characters generated by the ldisc (including echoes) need to
471 * be buffered because the driver's write buffer can fill during
472 * heavy program output. Echoing straight to the driver will
473 * often fail under these conditions, causing lost characters and
474 * resulting mismatches of ldisc state information.
475 *
476 * Since the ldisc state must represent the characters actually sent
477 * to the driver at the time of the write, operations like certain
478 * changes in column state are also saved in the buffer and executed
479 * here.
480 *
481 * A circular fifo buffer is used so that the most recent characters
482 * are prioritized. Also, when control characters are echoed with a
483 * prefixed "^", the pair is treated atomically and thus not separated.
484 *
485 * Locking: output_lock to protect column state and space left,
486 * echo_lock to protect the echo buffer
487 */
488
489static void process_echoes(struct tty_struct *tty)
490{
491 int space, nr;
492 unsigned char c;
493 unsigned char *cp, *buf_end;
494
495 if (!tty->echo_cnt)
496 return;
497
498 mutex_lock(&tty->output_lock);
499 mutex_lock(&tty->echo_lock);
500
501 space = tty_write_room(tty);
502
503 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
504 cp = tty->echo_buf + tty->echo_pos;
505 nr = tty->echo_cnt;
506 while (nr > 0) {
507 c = *cp;
508 if (c == ECHO_OP_START) {
509 unsigned char op;
510 unsigned char *opp;
511 int no_space_left = 0;
512
513 /*
514 * If the buffer byte is the start of a multi-byte
515 * operation, get the next byte, which is either the
516 * op code or a control character value.
517 */
518 opp = cp + 1;
519 if (opp == buf_end)
520 opp -= N_TTY_BUF_SIZE;
521 op = *opp;
522
523 switch (op) {
524 unsigned int num_chars, num_bs;
525
526 case ECHO_OP_ERASE_TAB:
527 if (++opp == buf_end)
528 opp -= N_TTY_BUF_SIZE;
529 num_chars = *opp;
530
531 /*
532 * Determine how many columns to go back
533 * in order to erase the tab.
534 * This depends on the number of columns
535 * used by other characters within the tab
536 * area. If this (modulo 8) count is from
537 * the start of input rather than from a
538 * previous tab, we offset by canon column.
539 * Otherwise, tab spacing is normal.
540 */
541 if (!(num_chars & 0x80))
542 num_chars += tty->canon_column;
543 num_bs = 8 - (num_chars & 7);
544
545 if (num_bs > space) {
546 no_space_left = 1;
547 break;
548 }
549 space -= num_bs;
550 while (num_bs--) {
551 tty_put_char(tty, '\b');
552 if (tty->column > 0)
553 tty->column--;
554 }
555 cp += 3;
556 nr -= 3;
557 break;
558
559 case ECHO_OP_SET_CANON_COL:
560 tty->canon_column = tty->column;
561 cp += 2;
562 nr -= 2;
563 break;
564
565 case ECHO_OP_MOVE_BACK_COL:
566 if (tty->column > 0)
567 tty->column--;
568 cp += 2;
569 nr -= 2;
570 break;
571
572 case ECHO_OP_START:
573 /* This is an escaped echo op start code */
574 if (!space) {
575 no_space_left = 1;
576 break;
577 }
578 tty_put_char(tty, ECHO_OP_START);
579 tty->column++;
580 space--;
581 cp += 2;
582 nr -= 2;
583 break;
584
585 default:
586 /*
587 * If the op is not a special byte code,
588 * it is a ctrl char tagged to be echoed
589 * as "^X" (where X is the letter
590 * representing the control char).
591 * Note that we must ensure there is
592 * enough space for the whole ctrl pair.
593 *
594 */
595 if (space < 2) {
596 no_space_left = 1;
597 break;
598 }
599 tty_put_char(tty, '^');
600 tty_put_char(tty, op ^ 0100);
601 tty->column += 2;
602 space -= 2;
603 cp += 2;
604 nr -= 2;
605 }
606
607 if (no_space_left)
608 break;
609 } else {
610 if (O_OPOST(tty) &&
611 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
612 int retval = do_output_char(c, tty, space);
613 if (retval < 0)
614 break;
615 space -= retval;
616 } else {
617 if (!space)
618 break;
619 tty_put_char(tty, c);
620 space -= 1;
621 }
622 cp += 1;
623 nr -= 1;
624 }
625
626 /* When end of circular buffer reached, wrap around */
627 if (cp >= buf_end)
628 cp -= N_TTY_BUF_SIZE;
629 }
630
631 if (nr == 0) {
632 tty->echo_pos = 0;
633 tty->echo_cnt = 0;
634 tty->echo_overrun = 0;
635 } else {
636 int num_processed = tty->echo_cnt - nr;
637 tty->echo_pos += num_processed;
638 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
639 tty->echo_cnt = nr;
640 if (num_processed > 0)
641 tty->echo_overrun = 0;
642 }
643
644 mutex_unlock(&tty->echo_lock);
645 mutex_unlock(&tty->output_lock);
646
647 if (tty->ops->flush_chars)
648 tty->ops->flush_chars(tty);
649}
650
651/**
652 * add_echo_byte - add a byte to the echo buffer
653 * @c: unicode byte to echo
654 * @tty: terminal device
655 *
656 * Add a character or operation byte to the echo buffer.
657 *
658 * Should be called under the echo lock to protect the echo buffer.
659 */
660
661static void add_echo_byte(unsigned char c, struct tty_struct *tty)
662{
663 int new_byte_pos;
664
665 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
666 /* Circular buffer is already at capacity */
667 new_byte_pos = tty->echo_pos;
668
669 /*
670 * Since the buffer start position needs to be advanced,
671 * be sure to step by a whole operation byte group.
672 */
673 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
674 if (tty->echo_buf[(tty->echo_pos + 1) &
675 (N_TTY_BUF_SIZE - 1)] ==
676 ECHO_OP_ERASE_TAB) {
677 tty->echo_pos += 3;
678 tty->echo_cnt -= 2;
679 } else {
680 tty->echo_pos += 2;
681 tty->echo_cnt -= 1;
682 }
683 } else {
684 tty->echo_pos++;
685 }
686 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
687
688 tty->echo_overrun = 1;
689 } else {
690 new_byte_pos = tty->echo_pos + tty->echo_cnt;
691 new_byte_pos &= N_TTY_BUF_SIZE - 1;
692 tty->echo_cnt++;
693 }
694
695 tty->echo_buf[new_byte_pos] = c;
696}
697
698/**
699 * echo_move_back_col - add operation to move back a column
700 * @tty: terminal device
701 *
702 * Add an operation to the echo buffer to move back one column.
703 *
704 * Locking: echo_lock to protect the echo buffer
705 */
706
707static void echo_move_back_col(struct tty_struct *tty)
708{
709 mutex_lock(&tty->echo_lock);
710
711 add_echo_byte(ECHO_OP_START, tty);
712 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
713
714 mutex_unlock(&tty->echo_lock);
715}
716
717/**
718 * echo_set_canon_col - add operation to set the canon column
719 * @tty: terminal device
720 *
721 * Add an operation to the echo buffer to set the canon column
722 * to the current column.
723 *
724 * Locking: echo_lock to protect the echo buffer
725 */
726
727static void echo_set_canon_col(struct tty_struct *tty)
728{
729 mutex_lock(&tty->echo_lock);
730
731 add_echo_byte(ECHO_OP_START, tty);
732 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
733
734 mutex_unlock(&tty->echo_lock);
735}
736
737/**
738 * echo_erase_tab - add operation to erase a tab
739 * @num_chars: number of character columns already used
740 * @after_tab: true if num_chars starts after a previous tab
741 * @tty: terminal device
742 *
743 * Add an operation to the echo buffer to erase a tab.
744 *
745 * Called by the eraser function, which knows how many character
746 * columns have been used since either a previous tab or the start
747 * of input. This information will be used later, along with
748 * canon column (if applicable), to go back the correct number
749 * of columns.
750 *
751 * Locking: echo_lock to protect the echo buffer
752 */
753
754static void echo_erase_tab(unsigned int num_chars, int after_tab,
755 struct tty_struct *tty)
756{
757 mutex_lock(&tty->echo_lock);
758
759 add_echo_byte(ECHO_OP_START, tty);
760 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
761
762 /* We only need to know this modulo 8 (tab spacing) */
763 num_chars &= 7;
764
765 /* Set the high bit as a flag if num_chars is after a previous tab */
766 if (after_tab)
767 num_chars |= 0x80;
768
769 add_echo_byte(num_chars, tty);
770
771 mutex_unlock(&tty->echo_lock);
772}
773
774/**
775 * echo_char_raw - echo a character raw
776 * @c: unicode byte to echo
777 * @tty: terminal device
778 *
779 * Echo user input back onto the screen. This must be called only when
780 * L_ECHO(tty) is true. Called from the driver receive_buf path.
781 *
782 * This variant does not treat control characters specially.
783 *
784 * Locking: echo_lock to protect the echo buffer
785 */
786
787static void echo_char_raw(unsigned char c, struct tty_struct *tty)
788{
789 mutex_lock(&tty->echo_lock);
790
791 if (c == ECHO_OP_START) {
792 add_echo_byte(ECHO_OP_START, tty);
793 add_echo_byte(ECHO_OP_START, tty);
794 } else {
795 add_echo_byte(c, tty);
796 }
797
798 mutex_unlock(&tty->echo_lock);
799}
800
801/**
802 * echo_char - echo a character
803 * @c: unicode byte to echo
804 * @tty: terminal device
805 *
806 * Echo user input back onto the screen. This must be called only when
807 * L_ECHO(tty) is true. Called from the driver receive_buf path.
808 *
809 * This variant tags control characters to be echoed as "^X"
810 * (where X is the letter representing the control char).
811 *
812 * Locking: echo_lock to protect the echo buffer
813 */
814
815static void echo_char(unsigned char c, struct tty_struct *tty)
816{
817 mutex_lock(&tty->echo_lock);
818
819 if (c == ECHO_OP_START) {
820 add_echo_byte(ECHO_OP_START, tty);
821 add_echo_byte(ECHO_OP_START, tty);
822 } else {
823 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
824 add_echo_byte(ECHO_OP_START, tty);
825 add_echo_byte(c, tty);
826 }
827
828 mutex_unlock(&tty->echo_lock);
829}
830
831/**
832 * finish_erasing - complete erase
833 * @tty: tty doing the erase
834 */
835
836static inline void finish_erasing(struct tty_struct *tty)
837{
838 if (tty->erasing) {
839 echo_char_raw('/', tty);
840 tty->erasing = 0;
841 }
842}
843
844/**
845 * eraser - handle erase function
846 * @c: character input
847 * @tty: terminal device
848 *
849 * Perform erase and necessary output when an erase character is
850 * present in the stream from the driver layer. Handles the complexities
851 * of UTF-8 multibyte symbols.
852 *
853 * Locking: read_lock for tty buffers
854 */
855
856static void eraser(unsigned char c, struct tty_struct *tty)
857{
858 enum { ERASE, WERASE, KILL } kill_type;
859 int head, seen_alnums, cnt;
860 unsigned long flags;
861
862 /* FIXME: locking needed ? */
863 if (tty->read_head == tty->canon_head) {
864 /* process_output('\a', tty); */ /* what do you think? */
865 return;
866 }
867 if (c == ERASE_CHAR(tty))
868 kill_type = ERASE;
869 else if (c == WERASE_CHAR(tty))
870 kill_type = WERASE;
871 else {
872 if (!L_ECHO(tty)) {
873 spin_lock_irqsave(&tty->read_lock, flags);
874 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
875 (N_TTY_BUF_SIZE - 1));
876 tty->read_head = tty->canon_head;
877 spin_unlock_irqrestore(&tty->read_lock, flags);
878 return;
879 }
880 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
881 spin_lock_irqsave(&tty->read_lock, flags);
882 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
883 (N_TTY_BUF_SIZE - 1));
884 tty->read_head = tty->canon_head;
885 spin_unlock_irqrestore(&tty->read_lock, flags);
886 finish_erasing(tty);
887 echo_char(KILL_CHAR(tty), tty);
888 /* Add a newline if ECHOK is on and ECHOKE is off. */
889 if (L_ECHOK(tty))
890 echo_char_raw('\n', tty);
891 return;
892 }
893 kill_type = KILL;
894 }
895
896 seen_alnums = 0;
897 /* FIXME: Locking ?? */
898 while (tty->read_head != tty->canon_head) {
899 head = tty->read_head;
900
901 /* erase a single possibly multibyte character */
902 do {
903 head = (head - 1) & (N_TTY_BUF_SIZE-1);
904 c = tty->read_buf[head];
905 } while (is_continuation(c, tty) && head != tty->canon_head);
906
907 /* do not partially erase */
908 if (is_continuation(c, tty))
909 break;
910
911 if (kill_type == WERASE) {
912 /* Equivalent to BSD's ALTWERASE. */
913 if (isalnum(c) || c == '_')
914 seen_alnums++;
915 else if (seen_alnums)
916 break;
917 }
918 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
919 spin_lock_irqsave(&tty->read_lock, flags);
920 tty->read_head = head;
921 tty->read_cnt -= cnt;
922 spin_unlock_irqrestore(&tty->read_lock, flags);
923 if (L_ECHO(tty)) {
924 if (L_ECHOPRT(tty)) {
925 if (!tty->erasing) {
926 echo_char_raw('\\', tty);
927 tty->erasing = 1;
928 }
929 /* if cnt > 1, output a multi-byte character */
930 echo_char(c, tty);
931 while (--cnt > 0) {
932 head = (head+1) & (N_TTY_BUF_SIZE-1);
933 echo_char_raw(tty->read_buf[head], tty);
934 echo_move_back_col(tty);
935 }
936 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
937 echo_char(ERASE_CHAR(tty), tty);
938 } else if (c == '\t') {
939 unsigned int num_chars = 0;
940 int after_tab = 0;
941 unsigned long tail = tty->read_head;
942
943 /*
944 * Count the columns used for characters
945 * since the start of input or after a
946 * previous tab.
947 * This info is used to go back the correct
948 * number of columns.
949 */
950 while (tail != tty->canon_head) {
951 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
952 c = tty->read_buf[tail];
953 if (c == '\t') {
954 after_tab = 1;
955 break;
956 } else if (iscntrl(c)) {
957 if (L_ECHOCTL(tty))
958 num_chars += 2;
959 } else if (!is_continuation(c, tty)) {
960 num_chars++;
961 }
962 }
963 echo_erase_tab(num_chars, after_tab, tty);
964 } else {
965 if (iscntrl(c) && L_ECHOCTL(tty)) {
966 echo_char_raw('\b', tty);
967 echo_char_raw(' ', tty);
968 echo_char_raw('\b', tty);
969 }
970 if (!iscntrl(c) || L_ECHOCTL(tty)) {
971 echo_char_raw('\b', tty);
972 echo_char_raw(' ', tty);
973 echo_char_raw('\b', tty);
974 }
975 }
976 }
977 if (kill_type == ERASE)
978 break;
979 }
980 if (tty->read_head == tty->canon_head && L_ECHO(tty))
981 finish_erasing(tty);
982}
983
984/**
985 * isig - handle the ISIG optio
986 * @sig: signal
987 * @tty: terminal
988 * @flush: force flush
989 *
990 * Called when a signal is being sent due to terminal input. This
991 * may caus terminal flushing to take place according to the termios
992 * settings and character used. Called from the driver receive_buf
993 * path so serialized.
994 *
995 * Locking: ctrl_lock, read_lock (both via flush buffer)
996 */
997
998static inline void isig(int sig, struct tty_struct *tty, int flush)
999{
1000 if (tty->pgrp)
1001 kill_pgrp(tty->pgrp, sig, 1);
1002 if (flush || !L_NOFLSH(tty)) {
1003 n_tty_flush_buffer(tty);
1004 tty_driver_flush_buffer(tty);
1005 }
1006}
1007
1008/**
1009 * n_tty_receive_break - handle break
1010 * @tty: terminal
1011 *
1012 * An RS232 break event has been hit in the incoming bitstream. This
1013 * can cause a variety of events depending upon the termios settings.
1014 *
1015 * Called from the receive_buf path so single threaded.
1016 */
1017
1018static inline void n_tty_receive_break(struct tty_struct *tty)
1019{
1020 if (I_IGNBRK(tty))
1021 return;
1022 if (I_BRKINT(tty)) {
1023 isig(SIGINT, tty, 1);
1024 return;
1025 }
1026 if (I_PARMRK(tty)) {
1027 put_tty_queue('\377', tty);
1028 put_tty_queue('\0', tty);
1029 }
1030 put_tty_queue('\0', tty);
1031 wake_up_interruptible(&tty->read_wait);
1032}
1033
1034/**
1035 * n_tty_receive_overrun - handle overrun reporting
1036 * @tty: terminal
1037 *
1038 * Data arrived faster than we could process it. While the tty
1039 * driver has flagged this the bits that were missed are gone
1040 * forever.
1041 *
1042 * Called from the receive_buf path so single threaded. Does not
1043 * need locking as num_overrun and overrun_time are function
1044 * private.
1045 */
1046
1047static inline void n_tty_receive_overrun(struct tty_struct *tty)
1048{
1049 char buf[64];
1050
1051 tty->num_overrun++;
1052 if (time_before(tty->overrun_time, jiffies - HZ) ||
1053 time_after(tty->overrun_time, jiffies)) {
1054 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1055 tty_name(tty, buf),
1056 tty->num_overrun);
1057 tty->overrun_time = jiffies;
1058 tty->num_overrun = 0;
1059 }
1060}
1061
1062/**
1063 * n_tty_receive_parity_error - error notifier
1064 * @tty: terminal device
1065 * @c: character
1066 *
1067 * Process a parity error and queue the right data to indicate
1068 * the error case if necessary. Locking as per n_tty_receive_buf.
1069 */
1070static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1071 unsigned char c)
1072{
1073 if (I_IGNPAR(tty))
1074 return;
1075 if (I_PARMRK(tty)) {
1076 put_tty_queue('\377', tty);
1077 put_tty_queue('\0', tty);
1078 put_tty_queue(c, tty);
1079 } else if (I_INPCK(tty))
1080 put_tty_queue('\0', tty);
1081 else
1082 put_tty_queue(c, tty);
1083 wake_up_interruptible(&tty->read_wait);
1084}
1085
1086/**
1087 * n_tty_receive_char - perform processing
1088 * @tty: terminal device
1089 * @c: character
1090 *
1091 * Process an individual character of input received from the driver.
1092 * This is serialized with respect to itself by the rules for the
1093 * driver above.
1094 */
1095
1096static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1097{
1098 unsigned long flags;
1099 int parmrk;
1100
1101 if (tty->raw) {
1102 put_tty_queue(c, tty);
1103 return;
1104 }
1105
1106 if (I_ISTRIP(tty))
1107 c &= 0x7f;
1108 if (I_IUCLC(tty) && L_IEXTEN(tty))
1109 c = tolower(c);
1110
1111 if (L_EXTPROC(tty)) {
1112 put_tty_queue(c, tty);
1113 return;
1114 }
1115
1116 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1117 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1118 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1119 start_tty(tty);
1120 process_echoes(tty);
1121 }
1122
1123 if (tty->closing) {
1124 if (I_IXON(tty)) {
1125 if (c == START_CHAR(tty)) {
1126 start_tty(tty);
1127 process_echoes(tty);
1128 } else if (c == STOP_CHAR(tty))
1129 stop_tty(tty);
1130 }
1131 return;
1132 }
1133
1134 /*
1135 * If the previous character was LNEXT, or we know that this
1136 * character is not one of the characters that we'll have to
1137 * handle specially, do shortcut processing to speed things
1138 * up.
1139 */
1140 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
1141 tty->lnext = 0;
1142 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1143 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1144 /* beep if no space */
1145 if (L_ECHO(tty))
1146 process_output('\a', tty);
1147 return;
1148 }
1149 if (L_ECHO(tty)) {
1150 finish_erasing(tty);
1151 /* Record the column of first canon char. */
1152 if (tty->canon_head == tty->read_head)
1153 echo_set_canon_col(tty);
1154 echo_char(c, tty);
1155 process_echoes(tty);
1156 }
1157 if (parmrk)
1158 put_tty_queue(c, tty);
1159 put_tty_queue(c, tty);
1160 return;
1161 }
1162
1163 if (I_IXON(tty)) {
1164 if (c == START_CHAR(tty)) {
1165 start_tty(tty);
1166 process_echoes(tty);
1167 return;
1168 }
1169 if (c == STOP_CHAR(tty)) {
1170 stop_tty(tty);
1171 return;
1172 }
1173 }
1174
1175 if (L_ISIG(tty)) {
1176 int signal;
1177 signal = SIGINT;
1178 if (c == INTR_CHAR(tty))
1179 goto send_signal;
1180 signal = SIGQUIT;
1181 if (c == QUIT_CHAR(tty))
1182 goto send_signal;
1183 signal = SIGTSTP;
1184 if (c == SUSP_CHAR(tty)) {
1185send_signal:
1186 /*
1187 * Note that we do not use isig() here because we want
1188 * the order to be:
1189 * 1) flush, 2) echo, 3) signal
1190 */
1191 if (!L_NOFLSH(tty)) {
1192 n_tty_flush_buffer(tty);
1193 tty_driver_flush_buffer(tty);
1194 }
1195 if (I_IXON(tty))
1196 start_tty(tty);
1197 if (L_ECHO(tty)) {
1198 echo_char(c, tty);
1199 process_echoes(tty);
1200 }
1201 if (tty->pgrp)
1202 kill_pgrp(tty->pgrp, signal, 1);
1203 return;
1204 }
1205 }
1206
1207 if (c == '\r') {
1208 if (I_IGNCR(tty))
1209 return;
1210 if (I_ICRNL(tty))
1211 c = '\n';
1212 } else if (c == '\n' && I_INLCR(tty))
1213 c = '\r';
1214
1215 if (tty->icanon) {
1216 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1217 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1218 eraser(c, tty);
1219 process_echoes(tty);
1220 return;
1221 }
1222 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1223 tty->lnext = 1;
1224 if (L_ECHO(tty)) {
1225 finish_erasing(tty);
1226 if (L_ECHOCTL(tty)) {
1227 echo_char_raw('^', tty);
1228 echo_char_raw('\b', tty);
1229 process_echoes(tty);
1230 }
1231 }
1232 return;
1233 }
1234 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1235 L_IEXTEN(tty)) {
1236 unsigned long tail = tty->canon_head;
1237
1238 finish_erasing(tty);
1239 echo_char(c, tty);
1240 echo_char_raw('\n', tty);
1241 while (tail != tty->read_head) {
1242 echo_char(tty->read_buf[tail], tty);
1243 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1244 }
1245 process_echoes(tty);
1246 return;
1247 }
1248 if (c == '\n') {
1249 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
1250 if (L_ECHO(tty))
1251 process_output('\a', tty);
1252 return;
1253 }
1254 if (L_ECHO(tty) || L_ECHONL(tty)) {
1255 echo_char_raw('\n', tty);
1256 process_echoes(tty);
1257 }
1258 goto handle_newline;
1259 }
1260 if (c == EOF_CHAR(tty)) {
1261 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1262 return;
1263 if (tty->canon_head != tty->read_head)
1264 set_bit(TTY_PUSH, &tty->flags);
1265 c = __DISABLED_CHAR;
1266 goto handle_newline;
1267 }
1268 if ((c == EOL_CHAR(tty)) ||
1269 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1270 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1271 ? 1 : 0;
1272 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1273 if (L_ECHO(tty))
1274 process_output('\a', tty);
1275 return;
1276 }
1277 /*
1278 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1279 */
1280 if (L_ECHO(tty)) {
1281 /* Record the column of first canon char. */
1282 if (tty->canon_head == tty->read_head)
1283 echo_set_canon_col(tty);
1284 echo_char(c, tty);
1285 process_echoes(tty);
1286 }
1287 /*
1288 * XXX does PARMRK doubling happen for
1289 * EOL_CHAR and EOL2_CHAR?
1290 */
1291 if (parmrk)
1292 put_tty_queue(c, tty);
1293
1294handle_newline:
1295 spin_lock_irqsave(&tty->read_lock, flags);
1296 set_bit(tty->read_head, tty->read_flags);
1297 put_tty_queue_nolock(c, tty);
1298 tty->canon_head = tty->read_head;
1299 tty->canon_data++;
1300 spin_unlock_irqrestore(&tty->read_lock, flags);
1301 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1302 if (waitqueue_active(&tty->read_wait))
1303 wake_up_interruptible(&tty->read_wait);
1304 return;
1305 }
1306 }
1307
1308 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1309 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1310 /* beep if no space */
1311 if (L_ECHO(tty))
1312 process_output('\a', tty);
1313 return;
1314 }
1315 if (L_ECHO(tty)) {
1316 finish_erasing(tty);
1317 if (c == '\n')
1318 echo_char_raw('\n', tty);
1319 else {
1320 /* Record the column of first canon char. */
1321 if (tty->canon_head == tty->read_head)
1322 echo_set_canon_col(tty);
1323 echo_char(c, tty);
1324 }
1325 process_echoes(tty);
1326 }
1327
1328 if (parmrk)
1329 put_tty_queue(c, tty);
1330
1331 put_tty_queue(c, tty);
1332}
1333
1334
1335/**
1336 * n_tty_write_wakeup - asynchronous I/O notifier
1337 * @tty: tty device
1338 *
1339 * Required for the ptys, serial driver etc. since processes
1340 * that attach themselves to the master and rely on ASYNC
1341 * IO must be woken up
1342 */
1343
1344static void n_tty_write_wakeup(struct tty_struct *tty)
1345{
1346 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1347 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1348}
1349
1350/**
1351 * n_tty_receive_buf - data receive
1352 * @tty: terminal device
1353 * @cp: buffer
1354 * @fp: flag buffer
1355 * @count: characters
1356 *
1357 * Called by the terminal driver when a block of characters has
1358 * been received. This function must be called from soft contexts
1359 * not from interrupt context. The driver is responsible for making
1360 * calls one at a time and in order (or using flush_to_ldisc)
1361 */
1362
1363static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1364 char *fp, int count)
1365{
1366 const unsigned char *p;
1367 char *f, flags = TTY_NORMAL;
1368 int i;
1369 char buf[64];
1370 unsigned long cpuflags;
1371
1372 if (!tty->read_buf)
1373 return;
1374
1375 if (tty->real_raw) {
1376 spin_lock_irqsave(&tty->read_lock, cpuflags);
1377 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1378 N_TTY_BUF_SIZE - tty->read_head);
1379 i = min(count, i);
1380 memcpy(tty->read_buf + tty->read_head, cp, i);
1381 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1382 tty->read_cnt += i;
1383 cp += i;
1384 count -= i;
1385
1386 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1387 N_TTY_BUF_SIZE - tty->read_head);
1388 i = min(count, i);
1389 memcpy(tty->read_buf + tty->read_head, cp, i);
1390 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1391 tty->read_cnt += i;
1392 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1393 } else {
1394 for (i = count, p = cp, f = fp; i; i--, p++) {
1395 if (f)
1396 flags = *f++;
1397 switch (flags) {
1398 case TTY_NORMAL:
1399 n_tty_receive_char(tty, *p);
1400 break;
1401 case TTY_BREAK:
1402 n_tty_receive_break(tty);
1403 break;
1404 case TTY_PARITY:
1405 case TTY_FRAME:
1406 n_tty_receive_parity_error(tty, *p);
1407 break;
1408 case TTY_OVERRUN:
1409 n_tty_receive_overrun(tty);
1410 break;
1411 default:
1412 printk(KERN_ERR "%s: unknown flag %d\n",
1413 tty_name(tty, buf), flags);
1414 break;
1415 }
1416 }
1417 if (tty->ops->flush_chars)
1418 tty->ops->flush_chars(tty);
1419 }
1420
1421 n_tty_set_room(tty);
1422
1423 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1424 L_EXTPROC(tty)) {
1425 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1426 if (waitqueue_active(&tty->read_wait))
1427 wake_up_interruptible(&tty->read_wait);
1428 }
1429
1430 /*
1431 * Check the remaining room for the input canonicalization
1432 * mode. We don't want to throttle the driver if we're in
1433 * canonical mode and don't have a newline yet!
1434 */
1435 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
1436 tty_throttle(tty);
1437}
1438
1439int is_ignored(int sig)
1440{
1441 return (sigismember(&current->blocked, sig) ||
1442 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1443}
1444
1445/**
1446 * n_tty_set_termios - termios data changed
1447 * @tty: terminal
1448 * @old: previous data
1449 *
1450 * Called by the tty layer when the user changes termios flags so
1451 * that the line discipline can plan ahead. This function cannot sleep
1452 * and is protected from re-entry by the tty layer. The user is
1453 * guaranteed that this function will not be re-entered or in progress
1454 * when the ldisc is closed.
1455 *
1456 * Locking: Caller holds tty->termios_mutex
1457 */
1458
1459static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1460{
1461 int canon_change = 1;
1462 BUG_ON(!tty);
1463
1464 if (old)
1465 canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
1466 if (canon_change) {
1467 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1468 tty->canon_head = tty->read_tail;
1469 tty->canon_data = 0;
1470 tty->erasing = 0;
1471 }
1472
1473 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1474 wake_up_interruptible(&tty->read_wait);
1475
1476 tty->icanon = (L_ICANON(tty) != 0);
1477 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1478 tty->raw = 1;
1479 tty->real_raw = 1;
1480 n_tty_set_room(tty);
1481 return;
1482 }
1483 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1484 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1485 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1486 I_PARMRK(tty)) {
1487 memset(tty->process_char_map, 0, 256/8);
1488
1489 if (I_IGNCR(tty) || I_ICRNL(tty))
1490 set_bit('\r', tty->process_char_map);
1491 if (I_INLCR(tty))
1492 set_bit('\n', tty->process_char_map);
1493
1494 if (L_ICANON(tty)) {
1495 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1496 set_bit(KILL_CHAR(tty), tty->process_char_map);
1497 set_bit(EOF_CHAR(tty), tty->process_char_map);
1498 set_bit('\n', tty->process_char_map);
1499 set_bit(EOL_CHAR(tty), tty->process_char_map);
1500 if (L_IEXTEN(tty)) {
1501 set_bit(WERASE_CHAR(tty),
1502 tty->process_char_map);
1503 set_bit(LNEXT_CHAR(tty),
1504 tty->process_char_map);
1505 set_bit(EOL2_CHAR(tty),
1506 tty->process_char_map);
1507 if (L_ECHO(tty))
1508 set_bit(REPRINT_CHAR(tty),
1509 tty->process_char_map);
1510 }
1511 }
1512 if (I_IXON(tty)) {
1513 set_bit(START_CHAR(tty), tty->process_char_map);
1514 set_bit(STOP_CHAR(tty), tty->process_char_map);
1515 }
1516 if (L_ISIG(tty)) {
1517 set_bit(INTR_CHAR(tty), tty->process_char_map);
1518 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1519 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1520 }
1521 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1522 tty->raw = 0;
1523 tty->real_raw = 0;
1524 } else {
1525 tty->raw = 1;
1526 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1527 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1528 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1529 tty->real_raw = 1;
1530 else
1531 tty->real_raw = 0;
1532 }
1533 n_tty_set_room(tty);
1534 /* The termios change make the tty ready for I/O */
1535 wake_up_interruptible(&tty->write_wait);
1536 wake_up_interruptible(&tty->read_wait);
1537}
1538
1539/**
1540 * n_tty_close - close the ldisc for this tty
1541 * @tty: device
1542 *
1543 * Called from the terminal layer when this line discipline is
1544 * being shut down, either because of a close or becsuse of a
1545 * discipline change. The function will not be called while other
1546 * ldisc methods are in progress.
1547 */
1548
1549static void n_tty_close(struct tty_struct *tty)
1550{
1551 n_tty_flush_buffer(tty);
1552 if (tty->read_buf) {
1553 kfree(tty->read_buf);
1554 tty->read_buf = NULL;
1555 }
1556 if (tty->echo_buf) {
1557 kfree(tty->echo_buf);
1558 tty->echo_buf = NULL;
1559 }
1560}
1561
1562/**
1563 * n_tty_open - open an ldisc
1564 * @tty: terminal to open
1565 *
1566 * Called when this line discipline is being attached to the
1567 * terminal device. Can sleep. Called serialized so that no
1568 * other events will occur in parallel. No further open will occur
1569 * until a close.
1570 */
1571
1572static int n_tty_open(struct tty_struct *tty)
1573{
1574 if (!tty)
1575 return -EINVAL;
1576
1577 /* These are ugly. Currently a malloc failure here can panic */
1578 if (!tty->read_buf) {
1579 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1580 if (!tty->read_buf)
1581 return -ENOMEM;
1582 }
1583 if (!tty->echo_buf) {
1584 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1585
1586 if (!tty->echo_buf)
1587 return -ENOMEM;
1588 }
1589 reset_buffer_flags(tty);
1590 tty->column = 0;
1591 n_tty_set_termios(tty, NULL);
1592 tty->minimum_to_wake = 1;
1593 tty->closing = 0;
1594 return 0;
1595}
1596
1597static inline int input_available_p(struct tty_struct *tty, int amt)
1598{
1599 tty_flush_to_ldisc(tty);
1600 if (tty->icanon && !L_EXTPROC(tty)) {
1601 if (tty->canon_data)
1602 return 1;
1603 } else if (tty->read_cnt >= (amt ? amt : 1))
1604 return 1;
1605
1606 return 0;
1607}
1608
1609/**
1610 * copy_from_read_buf - copy read data directly
1611 * @tty: terminal device
1612 * @b: user data
1613 * @nr: size of data
1614 *
1615 * Helper function to speed up n_tty_read. It is only called when
1616 * ICANON is off; it copies characters straight from the tty queue to
1617 * user space directly. It can be profitably called twice; once to
1618 * drain the space from the tail pointer to the (physical) end of the
1619 * buffer, and once to drain the space from the (physical) beginning of
1620 * the buffer to head pointer.
1621 *
1622 * Called under the tty->atomic_read_lock sem
1623 *
1624 */
1625
1626static int copy_from_read_buf(struct tty_struct *tty,
1627 unsigned char __user **b,
1628 size_t *nr)
1629
1630{
1631 int retval;
1632 size_t n;
1633 unsigned long flags;
1634
1635 retval = 0;
1636 spin_lock_irqsave(&tty->read_lock, flags);
1637 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1638 n = min(*nr, n);
1639 spin_unlock_irqrestore(&tty->read_lock, flags);
1640 if (n) {
1641 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1642 n -= retval;
1643 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1644 spin_lock_irqsave(&tty->read_lock, flags);
1645 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1646 tty->read_cnt -= n;
1647 /* Turn single EOF into zero-length read */
1648 if (L_EXTPROC(tty) && tty->icanon && n == 1) {
1649 if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
1650 n--;
1651 }
1652 spin_unlock_irqrestore(&tty->read_lock, flags);
1653 *b += n;
1654 *nr -= n;
1655 }
1656 return retval;
1657}
1658
1659extern ssize_t redirected_tty_write(struct file *, const char __user *,
1660 size_t, loff_t *);
1661
1662/**
1663 * job_control - check job control
1664 * @tty: tty
1665 * @file: file handle
1666 *
1667 * Perform job control management checks on this file/tty descriptor
1668 * and if appropriate send any needed signals and return a negative
1669 * error code if action should be taken.
1670 *
1671 * FIXME:
1672 * Locking: None - redirected write test is safe, testing
1673 * current->signal should possibly lock current->sighand
1674 * pgrp locking ?
1675 */
1676
1677static int job_control(struct tty_struct *tty, struct file *file)
1678{
1679 /* Job control check -- must be done at start and after
1680 every sleep (POSIX.1 7.1.1.4). */
1681 /* NOTE: not yet done after every sleep pending a thorough
1682 check of the logic of this change. -- jlc */
1683 /* don't stop on /dev/console */
1684 if (file->f_op->write != redirected_tty_write &&
1685 current->signal->tty == tty) {
1686 if (!tty->pgrp)
1687 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1688 else if (task_pgrp(current) != tty->pgrp) {
1689 if (is_ignored(SIGTTIN) ||
1690 is_current_pgrp_orphaned())
1691 return -EIO;
1692 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1693 set_thread_flag(TIF_SIGPENDING);
1694 return -ERESTARTSYS;
1695 }
1696 }
1697 return 0;
1698}
1699
1700
1701/**
1702 * n_tty_read - read function for tty
1703 * @tty: tty device
1704 * @file: file object
1705 * @buf: userspace buffer pointer
1706 * @nr: size of I/O
1707 *
1708 * Perform reads for the line discipline. We are guaranteed that the
1709 * line discipline will not be closed under us but we may get multiple
1710 * parallel readers and must handle this ourselves. We may also get
1711 * a hangup. Always called in user context, may sleep.
1712 *
1713 * This code must be sure never to sleep through a hangup.
1714 */
1715
1716static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1717 unsigned char __user *buf, size_t nr)
1718{
1719 unsigned char __user *b = buf;
1720 DECLARE_WAITQUEUE(wait, current);
1721 int c;
1722 int minimum, time;
1723 ssize_t retval = 0;
1724 ssize_t size;
1725 long timeout;
1726 unsigned long flags;
1727 int packet;
1728
1729do_it_again:
1730
1731 BUG_ON(!tty->read_buf);
1732
1733 c = job_control(tty, file);
1734 if (c < 0)
1735 return c;
1736
1737 minimum = time = 0;
1738 timeout = MAX_SCHEDULE_TIMEOUT;
1739 if (!tty->icanon) {
1740 time = (HZ / 10) * TIME_CHAR(tty);
1741 minimum = MIN_CHAR(tty);
1742 if (minimum) {
1743 if (time)
1744 tty->minimum_to_wake = 1;
1745 else if (!waitqueue_active(&tty->read_wait) ||
1746 (tty->minimum_to_wake > minimum))
1747 tty->minimum_to_wake = minimum;
1748 } else {
1749 timeout = 0;
1750 if (time) {
1751 timeout = time;
1752 time = 0;
1753 }
1754 tty->minimum_to_wake = minimum = 1;
1755 }
1756 }
1757
1758 /*
1759 * Internal serialization of reads.
1760 */
1761 if (file->f_flags & O_NONBLOCK) {
1762 if (!mutex_trylock(&tty->atomic_read_lock))
1763 return -EAGAIN;
1764 } else {
1765 if (mutex_lock_interruptible(&tty->atomic_read_lock))
1766 return -ERESTARTSYS;
1767 }
1768 packet = tty->packet;
1769
1770 add_wait_queue(&tty->read_wait, &wait);
1771 while (nr) {
1772 /* First test for status change. */
1773 if (packet && tty->link->ctrl_status) {
1774 unsigned char cs;
1775 if (b != buf)
1776 break;
1777 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1778 cs = tty->link->ctrl_status;
1779 tty->link->ctrl_status = 0;
1780 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1781 if (tty_put_user(tty, cs, b++)) {
1782 retval = -EFAULT;
1783 b--;
1784 break;
1785 }
1786 nr--;
1787 break;
1788 }
1789 /* This statement must be first before checking for input
1790 so that any interrupt will set the state back to
1791 TASK_RUNNING. */
1792 set_current_state(TASK_INTERRUPTIBLE);
1793
1794 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1795 ((minimum - (b - buf)) >= 1))
1796 tty->minimum_to_wake = (minimum - (b - buf));
1797
1798 if (!input_available_p(tty, 0)) {
1799 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1800 retval = -EIO;
1801 break;
1802 }
1803 if (tty_hung_up_p(file))
1804 break;
1805 if (!timeout)
1806 break;
1807 if (file->f_flags & O_NONBLOCK) {
1808 retval = -EAGAIN;
1809 break;
1810 }
1811 if (signal_pending(current)) {
1812 retval = -ERESTARTSYS;
1813 break;
1814 }
1815 /* FIXME: does n_tty_set_room need locking ? */
1816 n_tty_set_room(tty);
1817 timeout = schedule_timeout(timeout);
1818 BUG_ON(!tty->read_buf);
1819 continue;
1820 }
1821 __set_current_state(TASK_RUNNING);
1822
1823 /* Deal with packet mode. */
1824 if (packet && b == buf) {
1825 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1826 retval = -EFAULT;
1827 b--;
1828 break;
1829 }
1830 nr--;
1831 }
1832
1833 if (tty->icanon && !L_EXTPROC(tty)) {
1834 /* N.B. avoid overrun if nr == 0 */
1835 while (nr && tty->read_cnt) {
1836 int eol;
1837
1838 eol = test_and_clear_bit(tty->read_tail,
1839 tty->read_flags);
1840 c = tty->read_buf[tty->read_tail];
1841 spin_lock_irqsave(&tty->read_lock, flags);
1842 tty->read_tail = ((tty->read_tail+1) &
1843 (N_TTY_BUF_SIZE-1));
1844 tty->read_cnt--;
1845 if (eol) {
1846 /* this test should be redundant:
1847 * we shouldn't be reading data if
1848 * canon_data is 0
1849 */
1850 if (--tty->canon_data < 0)
1851 tty->canon_data = 0;
1852 }
1853 spin_unlock_irqrestore(&tty->read_lock, flags);
1854
1855 if (!eol || (c != __DISABLED_CHAR)) {
1856 if (tty_put_user(tty, c, b++)) {
1857 retval = -EFAULT;
1858 b--;
1859 break;
1860 }
1861 nr--;
1862 }
1863 if (eol) {
1864 tty_audit_push(tty);
1865 break;
1866 }
1867 }
1868 if (retval)
1869 break;
1870 } else {
1871 int uncopied;
1872 /* The copy function takes the read lock and handles
1873 locking internally for this case */
1874 uncopied = copy_from_read_buf(tty, &b, &nr);
1875 uncopied += copy_from_read_buf(tty, &b, &nr);
1876 if (uncopied) {
1877 retval = -EFAULT;
1878 break;
1879 }
1880 }
1881
1882 /* If there is enough space in the read buffer now, let the
1883 * low-level driver know. We use n_tty_chars_in_buffer() to
1884 * check the buffer, as it now knows about canonical mode.
1885 * Otherwise, if the driver is throttled and the line is
1886 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1887 * we won't get any more characters.
1888 */
1889 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1890 n_tty_set_room(tty);
1891 check_unthrottle(tty);
1892 }
1893
1894 if (b - buf >= minimum)
1895 break;
1896 if (time)
1897 timeout = time;
1898 }
1899 mutex_unlock(&tty->atomic_read_lock);
1900 remove_wait_queue(&tty->read_wait, &wait);
1901
1902 if (!waitqueue_active(&tty->read_wait))
1903 tty->minimum_to_wake = minimum;
1904
1905 __set_current_state(TASK_RUNNING);
1906 size = b - buf;
1907 if (size) {
1908 retval = size;
1909 if (nr)
1910 clear_bit(TTY_PUSH, &tty->flags);
1911 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1912 goto do_it_again;
1913
1914 n_tty_set_room(tty);
1915 return retval;
1916}
1917
1918/**
1919 * n_tty_write - write function for tty
1920 * @tty: tty device
1921 * @file: file object
1922 * @buf: userspace buffer pointer
1923 * @nr: size of I/O
1924 *
1925 * Write function of the terminal device. This is serialized with
1926 * respect to other write callers but not to termios changes, reads
1927 * and other such events. Since the receive code will echo characters,
1928 * thus calling driver write methods, the output_lock is used in
1929 * the output processing functions called here as well as in the
1930 * echo processing function to protect the column state and space
1931 * left in the buffer.
1932 *
1933 * This code must be sure never to sleep through a hangup.
1934 *
1935 * Locking: output_lock to protect column state and space left
1936 * (note that the process_output*() functions take this
1937 * lock themselves)
1938 */
1939
1940static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
1941 const unsigned char *buf, size_t nr)
1942{
1943 const unsigned char *b = buf;
1944 DECLARE_WAITQUEUE(wait, current);
1945 int c;
1946 ssize_t retval = 0;
1947
1948 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1949 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1950 retval = tty_check_change(tty);
1951 if (retval)
1952 return retval;
1953 }
1954
1955 /* Write out any echoed characters that are still pending */
1956 process_echoes(tty);
1957
1958 add_wait_queue(&tty->write_wait, &wait);
1959 while (1) {
1960 set_current_state(TASK_INTERRUPTIBLE);
1961 if (signal_pending(current)) {
1962 retval = -ERESTARTSYS;
1963 break;
1964 }
1965 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1966 retval = -EIO;
1967 break;
1968 }
1969 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1970 while (nr > 0) {
1971 ssize_t num = process_output_block(tty, b, nr);
1972 if (num < 0) {
1973 if (num == -EAGAIN)
1974 break;
1975 retval = num;
1976 goto break_out;
1977 }
1978 b += num;
1979 nr -= num;
1980 if (nr == 0)
1981 break;
1982 c = *b;
1983 if (process_output(c, tty) < 0)
1984 break;
1985 b++; nr--;
1986 }
1987 if (tty->ops->flush_chars)
1988 tty->ops->flush_chars(tty);
1989 } else {
1990 while (nr > 0) {
1991 c = tty->ops->write(tty, b, nr);
1992 if (c < 0) {
1993 retval = c;
1994 goto break_out;
1995 }
1996 if (!c)
1997 break;
1998 b += c;
1999 nr -= c;
2000 }
2001 }
2002 if (!nr)
2003 break;
2004 if (file->f_flags & O_NONBLOCK) {
2005 retval = -EAGAIN;
2006 break;
2007 }
2008 schedule();
2009 }
2010break_out:
2011 __set_current_state(TASK_RUNNING);
2012 remove_wait_queue(&tty->write_wait, &wait);
2013 if (b - buf != nr && tty->fasync)
2014 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2015 return (b - buf) ? b - buf : retval;
2016}
2017
2018/**
2019 * n_tty_poll - poll method for N_TTY
2020 * @tty: terminal device
2021 * @file: file accessing it
2022 * @wait: poll table
2023 *
2024 * Called when the line discipline is asked to poll() for data or
2025 * for special events. This code is not serialized with respect to
2026 * other events save open/close.
2027 *
2028 * This code must be sure never to sleep through a hangup.
2029 * Called without the kernel lock held - fine
2030 */
2031
2032static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2033 poll_table *wait)
2034{
2035 unsigned int mask = 0;
2036
2037 poll_wait(file, &tty->read_wait, wait);
2038 poll_wait(file, &tty->write_wait, wait);
2039 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2040 mask |= POLLIN | POLLRDNORM;
2041 if (tty->packet && tty->link->ctrl_status)
2042 mask |= POLLPRI | POLLIN | POLLRDNORM;
2043 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2044 mask |= POLLHUP;
2045 if (tty_hung_up_p(file))
2046 mask |= POLLHUP;
2047 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2048 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2049 tty->minimum_to_wake = MIN_CHAR(tty);
2050 else
2051 tty->minimum_to_wake = 1;
2052 }
2053 if (tty->ops->write && !tty_is_writelocked(tty) &&
2054 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2055 tty_write_room(tty) > 0)
2056 mask |= POLLOUT | POLLWRNORM;
2057 return mask;
2058}
2059
2060static unsigned long inq_canon(struct tty_struct *tty)
2061{
2062 int nr, head, tail;
2063
2064 if (!tty->canon_data)
2065 return 0;
2066 head = tty->canon_head;
2067 tail = tty->read_tail;
2068 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2069 /* Skip EOF-chars.. */
2070 while (head != tail) {
2071 if (test_bit(tail, tty->read_flags) &&
2072 tty->read_buf[tail] == __DISABLED_CHAR)
2073 nr--;
2074 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2075 }
2076 return nr;
2077}
2078
2079static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2080 unsigned int cmd, unsigned long arg)
2081{
2082 int retval;
2083
2084 switch (cmd) {
2085 case TIOCOUTQ:
2086 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2087 case TIOCINQ:
2088 /* FIXME: Locking */
2089 retval = tty->read_cnt;
2090 if (L_ICANON(tty))
2091 retval = inq_canon(tty);
2092 return put_user(retval, (unsigned int __user *) arg);
2093 default:
2094 return n_tty_ioctl_helper(tty, file, cmd, arg);
2095 }
2096}
2097
2098struct tty_ldisc_ops tty_ldisc_N_TTY = {
2099 .magic = TTY_LDISC_MAGIC,
2100 .name = "n_tty",
2101 .open = n_tty_open,
2102 .close = n_tty_close,
2103 .flush_buffer = n_tty_flush_buffer,
2104 .chars_in_buffer = n_tty_chars_in_buffer,
2105 .read = n_tty_read,
2106 .write = n_tty_write,
2107 .ioctl = n_tty_ioctl,
2108 .set_termios = n_tty_set_termios,
2109 .poll = n_tty_poll,
2110 .receive_buf = n_tty_receive_buf,
2111 .write_wakeup = n_tty_write_wakeup
2112};
2113
2114/**
2115 * n_tty_inherit_ops - inherit N_TTY methods
2116 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2117 *
2118 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2119 * methods.
2120 */
2121
2122void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2123{
2124 *ops = tty_ldisc_N_TTY;
2125 ops->owner = NULL;
2126 ops->refcount = ops->flags = 0;
2127}
2128EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
diff --git a/drivers/tty/nozomi.c b/drivers/tty/nozomi.c
new file mode 100644
index 000000000000..fd347ff34d07
--- /dev/null
+++ b/drivers/tty/nozomi.c
@@ -0,0 +1,1971 @@
1/*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
5 * Jan Ã…kerfeldt,
6 * Stefan Thomasson,
7 *
8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
9 *
10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
14 *
15 * Source has been ported from an implementation made by Filip Aben @ Option
16 *
17 * --------------------------------------------------------------------------
18 *
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 *
38 * --------------------------------------------------------------------------
39 */
40
41/* Enable this to have a lot of debug printouts */
42#define DEBUG
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/pci.h>
47#include <linux/ioport.h>
48#include <linux/tty.h>
49#include <linux/tty_driver.h>
50#include <linux/tty_flip.h>
51#include <linux/sched.h>
52#include <linux/serial.h>
53#include <linux/interrupt.h>
54#include <linux/kmod.h>
55#include <linux/init.h>
56#include <linux/kfifo.h>
57#include <linux/uaccess.h>
58#include <linux/slab.h>
59#include <asm/byteorder.h>
60
61#include <linux/delay.h>
62
63
64#define VERSION_STRING DRIVER_DESC " 2.1d"
65
66/* Macros definitions */
67
68/* Default debug printout level */
69#define NOZOMI_DEBUG_LEVEL 0x00
70
71#define P_BUF_SIZE 128
72#define NFO(_err_flag_, args...) \
73do { \
74 char tmp[P_BUF_SIZE]; \
75 snprintf(tmp, sizeof(tmp), ##args); \
76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
77 __func__, tmp); \
78} while (0)
79
80#define DBG1(args...) D_(0x01, ##args)
81#define DBG2(args...) D_(0x02, ##args)
82#define DBG3(args...) D_(0x04, ##args)
83#define DBG4(args...) D_(0x08, ##args)
84#define DBG5(args...) D_(0x10, ##args)
85#define DBG6(args...) D_(0x20, ##args)
86#define DBG7(args...) D_(0x40, ##args)
87#define DBG8(args...) D_(0x80, ##args)
88
89#ifdef DEBUG
90/* Do we need this settable at runtime? */
91static int debug = NOZOMI_DEBUG_LEVEL;
92
93#define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
95 while (0)
96#define D_(lvl, args...) D(lvl, ##args)
97
98/* These printouts are always printed */
99
100#else
101static int debug;
102#define D_(lvl, args...)
103#endif
104
105/* TODO: rewrite to optimize macros... */
106
107#define TMP_BUF_MAX 256
108
109#define DUMP(buf__,len__) \
110 do { \
111 char tbuf[TMP_BUF_MAX] = {0};\
112 if (len__ > 1) {\
113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
114 if (tbuf[len__-2] == '\r') {\
115 tbuf[len__-2] = 'r';\
116 } \
117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
118 } else {\
119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
120 } \
121} while (0)
122
123/* Defines */
124#define NOZOMI_NAME "nozomi"
125#define NOZOMI_NAME_TTY "nozomi_tty"
126#define DRIVER_DESC "Nozomi driver"
127
128#define NTTY_TTY_MAXMINORS 256
129#define NTTY_FIFO_BUFFER_SIZE 8192
130
131/* Must be power of 2 */
132#define FIFO_BUFFER_SIZE_UL 8192
133
134/* Size of tmp send buffer to card */
135#define SEND_BUF_MAX 1024
136#define RECEIVE_BUF_MAX 4
137
138
139#define R_IIR 0x0000 /* Interrupt Identity Register */
140#define R_FCR 0x0000 /* Flow Control Register */
141#define R_IER 0x0004 /* Interrupt Enable Register */
142
143#define CONFIG_MAGIC 0xEFEFFEFE
144#define TOGGLE_VALID 0x0000
145
146/* Definition of interrupt tokens */
147#define MDM_DL1 0x0001
148#define MDM_UL1 0x0002
149#define MDM_DL2 0x0004
150#define MDM_UL2 0x0008
151#define DIAG_DL1 0x0010
152#define DIAG_DL2 0x0020
153#define DIAG_UL 0x0040
154#define APP1_DL 0x0080
155#define APP1_UL 0x0100
156#define APP2_DL 0x0200
157#define APP2_UL 0x0400
158#define CTRL_DL 0x0800
159#define CTRL_UL 0x1000
160#define RESET 0x8000
161
162#define MDM_DL (MDM_DL1 | MDM_DL2)
163#define MDM_UL (MDM_UL1 | MDM_UL2)
164#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
165
166/* modem signal definition */
167#define CTRL_DSR 0x0001
168#define CTRL_DCD 0x0002
169#define CTRL_RI 0x0004
170#define CTRL_CTS 0x0008
171
172#define CTRL_DTR 0x0001
173#define CTRL_RTS 0x0002
174
175#define MAX_PORT 4
176#define NOZOMI_MAX_PORTS 5
177#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
178
179/* Type definitions */
180
181/*
182 * There are two types of nozomi cards,
183 * one with 2048 memory and with 8192 memory
184 */
185enum card_type {
186 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
187 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
188};
189
190/* Initialization states a card can be in */
191enum card_state {
192 NOZOMI_STATE_UKNOWN = 0,
193 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
194 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
195 NOZOMI_STATE_READY = 3, /* flowcontrols received */
196};
197
198/* Two different toggle channels exist */
199enum channel_type {
200 CH_A = 0,
201 CH_B = 1,
202};
203
204/* Port definition for the card regarding flow control */
205enum ctrl_port_type {
206 CTRL_CMD = 0,
207 CTRL_MDM = 1,
208 CTRL_DIAG = 2,
209 CTRL_APP1 = 3,
210 CTRL_APP2 = 4,
211 CTRL_ERROR = -1,
212};
213
214/* Ports that the nozomi has */
215enum port_type {
216 PORT_MDM = 0,
217 PORT_DIAG = 1,
218 PORT_APP1 = 2,
219 PORT_APP2 = 3,
220 PORT_CTRL = 4,
221 PORT_ERROR = -1,
222};
223
224#ifdef __BIG_ENDIAN
225/* Big endian */
226
227struct toggles {
228 unsigned int enabled:5; /*
229 * Toggle fields are valid if enabled is 0,
230 * else A-channels must always be used.
231 */
232 unsigned int diag_dl:1;
233 unsigned int mdm_dl:1;
234 unsigned int mdm_ul:1;
235} __attribute__ ((packed));
236
237/* Configuration table to read at startup of card */
238/* Is for now only needed during initialization phase */
239struct config_table {
240 u32 signature;
241 u16 product_information;
242 u16 version;
243 u8 pad3[3];
244 struct toggles toggle;
245 u8 pad1[4];
246 u16 dl_mdm_len1; /*
247 * If this is 64, it can hold
248 * 60 bytes + 4 that is length field
249 */
250 u16 dl_start;
251
252 u16 dl_diag_len1;
253 u16 dl_mdm_len2; /*
254 * If this is 64, it can hold
255 * 60 bytes + 4 that is length field
256 */
257 u16 dl_app1_len;
258
259 u16 dl_diag_len2;
260 u16 dl_ctrl_len;
261 u16 dl_app2_len;
262 u8 pad2[16];
263 u16 ul_mdm_len1;
264 u16 ul_start;
265 u16 ul_diag_len;
266 u16 ul_mdm_len2;
267 u16 ul_app1_len;
268 u16 ul_app2_len;
269 u16 ul_ctrl_len;
270} __attribute__ ((packed));
271
272/* This stores all control downlink flags */
273struct ctrl_dl {
274 u8 port;
275 unsigned int reserved:4;
276 unsigned int CTS:1;
277 unsigned int RI:1;
278 unsigned int DCD:1;
279 unsigned int DSR:1;
280} __attribute__ ((packed));
281
282/* This stores all control uplink flags */
283struct ctrl_ul {
284 u8 port;
285 unsigned int reserved:6;
286 unsigned int RTS:1;
287 unsigned int DTR:1;
288} __attribute__ ((packed));
289
290#else
291/* Little endian */
292
293/* This represents the toggle information */
294struct toggles {
295 unsigned int mdm_ul:1;
296 unsigned int mdm_dl:1;
297 unsigned int diag_dl:1;
298 unsigned int enabled:5; /*
299 * Toggle fields are valid if enabled is 0,
300 * else A-channels must always be used.
301 */
302} __attribute__ ((packed));
303
304/* Configuration table to read at startup of card */
305struct config_table {
306 u32 signature;
307 u16 version;
308 u16 product_information;
309 struct toggles toggle;
310 u8 pad1[7];
311 u16 dl_start;
312 u16 dl_mdm_len1; /*
313 * If this is 64, it can hold
314 * 60 bytes + 4 that is length field
315 */
316 u16 dl_mdm_len2;
317 u16 dl_diag_len1;
318 u16 dl_diag_len2;
319 u16 dl_app1_len;
320 u16 dl_app2_len;
321 u16 dl_ctrl_len;
322 u8 pad2[16];
323 u16 ul_start;
324 u16 ul_mdm_len2;
325 u16 ul_mdm_len1;
326 u16 ul_diag_len;
327 u16 ul_app1_len;
328 u16 ul_app2_len;
329 u16 ul_ctrl_len;
330} __attribute__ ((packed));
331
332/* This stores all control downlink flags */
333struct ctrl_dl {
334 unsigned int DSR:1;
335 unsigned int DCD:1;
336 unsigned int RI:1;
337 unsigned int CTS:1;
338 unsigned int reserverd:4;
339 u8 port;
340} __attribute__ ((packed));
341
342/* This stores all control uplink flags */
343struct ctrl_ul {
344 unsigned int DTR:1;
345 unsigned int RTS:1;
346 unsigned int reserved:6;
347 u8 port;
348} __attribute__ ((packed));
349#endif
350
351/* This holds all information that is needed regarding a port */
352struct port {
353 struct tty_port port;
354 u8 update_flow_control;
355 struct ctrl_ul ctrl_ul;
356 struct ctrl_dl ctrl_dl;
357 struct kfifo fifo_ul;
358 void __iomem *dl_addr[2];
359 u32 dl_size[2];
360 u8 toggle_dl;
361 void __iomem *ul_addr[2];
362 u32 ul_size[2];
363 u8 toggle_ul;
364 u16 token_dl;
365
366 wait_queue_head_t tty_wait;
367 struct async_icount tty_icount;
368
369 struct nozomi *dc;
370};
371
372/* Private data one for each card in the system */
373struct nozomi {
374 void __iomem *base_addr;
375 unsigned long flip;
376
377 /* Pointers to registers */
378 void __iomem *reg_iir;
379 void __iomem *reg_fcr;
380 void __iomem *reg_ier;
381
382 u16 last_ier;
383 enum card_type card_type;
384 struct config_table config_table; /* Configuration table */
385 struct pci_dev *pdev;
386 struct port port[NOZOMI_MAX_PORTS];
387 u8 *send_buf;
388
389 spinlock_t spin_mutex; /* secures access to registers and tty */
390
391 unsigned int index_start;
392 enum card_state state;
393 u32 open_ttys;
394};
395
396/* This is a data packet that is read or written to/from card */
397struct buffer {
398 u32 size; /* size is the length of the data buffer */
399 u8 *data;
400} __attribute__ ((packed));
401
402/* Global variables */
403static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
404 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
405 {},
406};
407
408MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
409
410static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
411static struct tty_driver *ntty_driver;
412
413static const struct tty_port_operations noz_tty_port_ops;
414
415/*
416 * find card by tty_index
417 */
418static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
419{
420 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
421}
422
423static inline struct port *get_port_by_tty(const struct tty_struct *tty)
424{
425 struct nozomi *ndev = get_dc_by_tty(tty);
426 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
427}
428
429/*
430 * TODO:
431 * -Optimize
432 * -Rewrite cleaner
433 */
434
435static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
436 u32 size_bytes)
437{
438 u32 i = 0;
439 const u32 __iomem *ptr = mem_addr_start;
440 u16 *buf16;
441
442 if (unlikely(!ptr || !buf))
443 goto out;
444
445 /* shortcut for extremely often used cases */
446 switch (size_bytes) {
447 case 2: /* 2 bytes */
448 buf16 = (u16 *) buf;
449 *buf16 = __le16_to_cpu(readw(ptr));
450 goto out;
451 break;
452 case 4: /* 4 bytes */
453 *(buf) = __le32_to_cpu(readl(ptr));
454 goto out;
455 break;
456 }
457
458 while (i < size_bytes) {
459 if (size_bytes - i == 2) {
460 /* Handle 2 bytes in the end */
461 buf16 = (u16 *) buf;
462 *(buf16) = __le16_to_cpu(readw(ptr));
463 i += 2;
464 } else {
465 /* Read 4 bytes */
466 *(buf) = __le32_to_cpu(readl(ptr));
467 i += 4;
468 }
469 buf++;
470 ptr++;
471 }
472out:
473 return;
474}
475
476/*
477 * TODO:
478 * -Optimize
479 * -Rewrite cleaner
480 */
481static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
482 u32 size_bytes)
483{
484 u32 i = 0;
485 u32 __iomem *ptr = mem_addr_start;
486 const u16 *buf16;
487
488 if (unlikely(!ptr || !buf))
489 return 0;
490
491 /* shortcut for extremely often used cases */
492 switch (size_bytes) {
493 case 2: /* 2 bytes */
494 buf16 = (const u16 *)buf;
495 writew(__cpu_to_le16(*buf16), ptr);
496 return 2;
497 break;
498 case 1: /*
499 * also needs to write 4 bytes in this case
500 * so falling through..
501 */
502 case 4: /* 4 bytes */
503 writel(__cpu_to_le32(*buf), ptr);
504 return 4;
505 break;
506 }
507
508 while (i < size_bytes) {
509 if (size_bytes - i == 2) {
510 /* 2 bytes */
511 buf16 = (const u16 *)buf;
512 writew(__cpu_to_le16(*buf16), ptr);
513 i += 2;
514 } else {
515 /* 4 bytes */
516 writel(__cpu_to_le32(*buf), ptr);
517 i += 4;
518 }
519 buf++;
520 ptr++;
521 }
522 return i;
523}
524
525/* Setup pointers to different channels and also setup buffer sizes. */
526static void setup_memory(struct nozomi *dc)
527{
528 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
529 /* The length reported is including the length field of 4 bytes,
530 * hence subtract with 4.
531 */
532 const u16 buff_offset = 4;
533
534 /* Modem port dl configuration */
535 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
536 dc->port[PORT_MDM].dl_addr[CH_B] =
537 (offset += dc->config_table.dl_mdm_len1);
538 dc->port[PORT_MDM].dl_size[CH_A] =
539 dc->config_table.dl_mdm_len1 - buff_offset;
540 dc->port[PORT_MDM].dl_size[CH_B] =
541 dc->config_table.dl_mdm_len2 - buff_offset;
542
543 /* Diag port dl configuration */
544 dc->port[PORT_DIAG].dl_addr[CH_A] =
545 (offset += dc->config_table.dl_mdm_len2);
546 dc->port[PORT_DIAG].dl_size[CH_A] =
547 dc->config_table.dl_diag_len1 - buff_offset;
548 dc->port[PORT_DIAG].dl_addr[CH_B] =
549 (offset += dc->config_table.dl_diag_len1);
550 dc->port[PORT_DIAG].dl_size[CH_B] =
551 dc->config_table.dl_diag_len2 - buff_offset;
552
553 /* App1 port dl configuration */
554 dc->port[PORT_APP1].dl_addr[CH_A] =
555 (offset += dc->config_table.dl_diag_len2);
556 dc->port[PORT_APP1].dl_size[CH_A] =
557 dc->config_table.dl_app1_len - buff_offset;
558
559 /* App2 port dl configuration */
560 dc->port[PORT_APP2].dl_addr[CH_A] =
561 (offset += dc->config_table.dl_app1_len);
562 dc->port[PORT_APP2].dl_size[CH_A] =
563 dc->config_table.dl_app2_len - buff_offset;
564
565 /* Ctrl dl configuration */
566 dc->port[PORT_CTRL].dl_addr[CH_A] =
567 (offset += dc->config_table.dl_app2_len);
568 dc->port[PORT_CTRL].dl_size[CH_A] =
569 dc->config_table.dl_ctrl_len - buff_offset;
570
571 offset = dc->base_addr + dc->config_table.ul_start;
572
573 /* Modem Port ul configuration */
574 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
575 dc->port[PORT_MDM].ul_size[CH_A] =
576 dc->config_table.ul_mdm_len1 - buff_offset;
577 dc->port[PORT_MDM].ul_addr[CH_B] =
578 (offset += dc->config_table.ul_mdm_len1);
579 dc->port[PORT_MDM].ul_size[CH_B] =
580 dc->config_table.ul_mdm_len2 - buff_offset;
581
582 /* Diag port ul configuration */
583 dc->port[PORT_DIAG].ul_addr[CH_A] =
584 (offset += dc->config_table.ul_mdm_len2);
585 dc->port[PORT_DIAG].ul_size[CH_A] =
586 dc->config_table.ul_diag_len - buff_offset;
587
588 /* App1 port ul configuration */
589 dc->port[PORT_APP1].ul_addr[CH_A] =
590 (offset += dc->config_table.ul_diag_len);
591 dc->port[PORT_APP1].ul_size[CH_A] =
592 dc->config_table.ul_app1_len - buff_offset;
593
594 /* App2 port ul configuration */
595 dc->port[PORT_APP2].ul_addr[CH_A] =
596 (offset += dc->config_table.ul_app1_len);
597 dc->port[PORT_APP2].ul_size[CH_A] =
598 dc->config_table.ul_app2_len - buff_offset;
599
600 /* Ctrl ul configuration */
601 dc->port[PORT_CTRL].ul_addr[CH_A] =
602 (offset += dc->config_table.ul_app2_len);
603 dc->port[PORT_CTRL].ul_size[CH_A] =
604 dc->config_table.ul_ctrl_len - buff_offset;
605}
606
607/* Dump config table under initalization phase */
608#ifdef DEBUG
609static void dump_table(const struct nozomi *dc)
610{
611 DBG3("signature: 0x%08X", dc->config_table.signature);
612 DBG3("version: 0x%04X", dc->config_table.version);
613 DBG3("product_information: 0x%04X", \
614 dc->config_table.product_information);
615 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
616 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
617 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
618 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
619
620 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
621 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
622 dc->config_table.dl_mdm_len1);
623 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
624 dc->config_table.dl_mdm_len2);
625 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
626 dc->config_table.dl_diag_len1);
627 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
628 dc->config_table.dl_diag_len2);
629 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
630 dc->config_table.dl_app1_len);
631 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
632 dc->config_table.dl_app2_len);
633 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
634 dc->config_table.dl_ctrl_len);
635 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
636 dc->config_table.ul_start);
637 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
638 dc->config_table.ul_mdm_len1);
639 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
640 dc->config_table.ul_mdm_len2);
641 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
642 dc->config_table.ul_diag_len);
643 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
644 dc->config_table.ul_app1_len);
645 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
646 dc->config_table.ul_app2_len);
647 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
648 dc->config_table.ul_ctrl_len);
649}
650#else
651static inline void dump_table(const struct nozomi *dc) { }
652#endif
653
654/*
655 * Read configuration table from card under intalization phase
656 * Returns 1 if ok, else 0
657 */
658static int nozomi_read_config_table(struct nozomi *dc)
659{
660 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
661 sizeof(struct config_table));
662
663 if (dc->config_table.signature != CONFIG_MAGIC) {
664 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
665 dc->config_table.signature, CONFIG_MAGIC);
666 return 0;
667 }
668
669 if ((dc->config_table.version == 0)
670 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
671 int i;
672 DBG1("Second phase, configuring card");
673
674 setup_memory(dc);
675
676 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
677 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
678 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
679 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
680 dc->port[PORT_MDM].toggle_ul,
681 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
682
683 dump_table(dc);
684
685 for (i = PORT_MDM; i < MAX_PORT; i++) {
686 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
687 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
688 }
689
690 /* Enable control channel */
691 dc->last_ier = dc->last_ier | CTRL_DL;
692 writew(dc->last_ier, dc->reg_ier);
693
694 dc->state = NOZOMI_STATE_ALLOCATED;
695 dev_info(&dc->pdev->dev, "Initialization OK!\n");
696 return 1;
697 }
698
699 if ((dc->config_table.version > 0)
700 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
701 u32 offset = 0;
702 DBG1("First phase: pushing upload buffers, clearing download");
703
704 dev_info(&dc->pdev->dev, "Version of card: %d\n",
705 dc->config_table.version);
706
707 /* Here we should disable all I/O over F32. */
708 setup_memory(dc);
709
710 /*
711 * We should send ALL channel pair tokens back along
712 * with reset token
713 */
714
715 /* push upload modem buffers */
716 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
717 (u32 *) &offset, 4);
718 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
719 (u32 *) &offset, 4);
720
721 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
722
723 DBG1("First phase done");
724 }
725
726 return 1;
727}
728
729/* Enable uplink interrupts */
730static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
731{
732 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
733
734 if (port < NOZOMI_MAX_PORTS) {
735 dc->last_ier |= mask[port];
736 writew(dc->last_ier, dc->reg_ier);
737 } else {
738 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
739 }
740}
741
742/* Disable uplink interrupts */
743static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
744{
745 static const u16 mask[] =
746 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
747
748 if (port < NOZOMI_MAX_PORTS) {
749 dc->last_ier &= mask[port];
750 writew(dc->last_ier, dc->reg_ier);
751 } else {
752 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
753 }
754}
755
756/* Enable downlink interrupts */
757static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
758{
759 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
760
761 if (port < NOZOMI_MAX_PORTS) {
762 dc->last_ier |= mask[port];
763 writew(dc->last_ier, dc->reg_ier);
764 } else {
765 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
766 }
767}
768
769/* Disable downlink interrupts */
770static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
771{
772 static const u16 mask[] =
773 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
774
775 if (port < NOZOMI_MAX_PORTS) {
776 dc->last_ier &= mask[port];
777 writew(dc->last_ier, dc->reg_ier);
778 } else {
779 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
780 }
781}
782
783/*
784 * Return 1 - send buffer to card and ack.
785 * Return 0 - don't ack, don't send buffer to card.
786 */
787static int send_data(enum port_type index, struct nozomi *dc)
788{
789 u32 size = 0;
790 struct port *port = &dc->port[index];
791 const u8 toggle = port->toggle_ul;
792 void __iomem *addr = port->ul_addr[toggle];
793 const u32 ul_size = port->ul_size[toggle];
794 struct tty_struct *tty = tty_port_tty_get(&port->port);
795
796 /* Get data from tty and place in buf for now */
797 size = kfifo_out(&port->fifo_ul, dc->send_buf,
798 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
799
800 if (size == 0) {
801 DBG4("No more data to send, disable link:");
802 tty_kref_put(tty);
803 return 0;
804 }
805
806 /* DUMP(buf, size); */
807
808 /* Write length + data */
809 write_mem32(addr, (u32 *) &size, 4);
810 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
811
812 if (tty)
813 tty_wakeup(tty);
814
815 tty_kref_put(tty);
816 return 1;
817}
818
819/* If all data has been read, return 1, else 0 */
820static int receive_data(enum port_type index, struct nozomi *dc)
821{
822 u8 buf[RECEIVE_BUF_MAX] = { 0 };
823 int size;
824 u32 offset = 4;
825 struct port *port = &dc->port[index];
826 void __iomem *addr = port->dl_addr[port->toggle_dl];
827 struct tty_struct *tty = tty_port_tty_get(&port->port);
828 int i, ret;
829
830 if (unlikely(!tty)) {
831 DBG1("tty not open for port: %d?", index);
832 return 1;
833 }
834
835 read_mem32((u32 *) &size, addr, 4);
836 /* DBG1( "%d bytes port: %d", size, index); */
837
838 if (test_bit(TTY_THROTTLED, &tty->flags)) {
839 DBG1("No room in tty, don't read data, don't ack interrupt, "
840 "disable interrupt");
841
842 /* disable interrupt in downlink... */
843 disable_transmit_dl(index, dc);
844 ret = 0;
845 goto put;
846 }
847
848 if (unlikely(size == 0)) {
849 dev_err(&dc->pdev->dev, "size == 0?\n");
850 ret = 1;
851 goto put;
852 }
853
854 while (size > 0) {
855 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
856
857 if (size == 1) {
858 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
859 size = 0;
860 } else if (size < RECEIVE_BUF_MAX) {
861 size -= tty_insert_flip_string(tty, (char *) buf, size);
862 } else {
863 i = tty_insert_flip_string(tty, \
864 (char *) buf, RECEIVE_BUF_MAX);
865 size -= i;
866 offset += i;
867 }
868 }
869
870 set_bit(index, &dc->flip);
871 ret = 1;
872put:
873 tty_kref_put(tty);
874 return ret;
875}
876
877/* Debug for interrupts */
878#ifdef DEBUG
879static char *interrupt2str(u16 interrupt)
880{
881 static char buf[TMP_BUF_MAX];
882 char *p = buf;
883
884 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
885 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
886 "MDM_DL2 ") : NULL;
887
888 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "MDM_UL1 ") : NULL;
890 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
891 "MDM_UL2 ") : NULL;
892
893 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "DIAG_DL1 ") : NULL;
895 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
896 "DIAG_DL2 ") : NULL;
897
898 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_UL ") : NULL;
900
901 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "APP1_DL ") : NULL;
903 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
904 "APP2_DL ") : NULL;
905
906 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP1_UL ") : NULL;
908 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
909 "APP2_UL ") : NULL;
910
911 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "CTRL_DL ") : NULL;
913 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
914 "CTRL_UL ") : NULL;
915
916 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "RESET ") : NULL;
918
919 return buf;
920}
921#endif
922
923/*
924 * Receive flow control
925 * Return 1 - If ok, else 0
926 */
927static int receive_flow_control(struct nozomi *dc)
928{
929 enum port_type port = PORT_MDM;
930 struct ctrl_dl ctrl_dl;
931 struct ctrl_dl old_ctrl;
932 u16 enable_ier = 0;
933
934 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
935
936 switch (ctrl_dl.port) {
937 case CTRL_CMD:
938 DBG1("The Base Band sends this value as a response to a "
939 "request for IMSI detach sent over the control "
940 "channel uplink (see section 7.6.1).");
941 break;
942 case CTRL_MDM:
943 port = PORT_MDM;
944 enable_ier = MDM_DL;
945 break;
946 case CTRL_DIAG:
947 port = PORT_DIAG;
948 enable_ier = DIAG_DL;
949 break;
950 case CTRL_APP1:
951 port = PORT_APP1;
952 enable_ier = APP1_DL;
953 break;
954 case CTRL_APP2:
955 port = PORT_APP2;
956 enable_ier = APP2_DL;
957 if (dc->state == NOZOMI_STATE_ALLOCATED) {
958 /*
959 * After card initialization the flow control
960 * received for APP2 is always the last
961 */
962 dc->state = NOZOMI_STATE_READY;
963 dev_info(&dc->pdev->dev, "Device READY!\n");
964 }
965 break;
966 default:
967 dev_err(&dc->pdev->dev,
968 "ERROR: flow control received for non-existing port\n");
969 return 0;
970 };
971
972 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
973 *((u16 *)&ctrl_dl));
974
975 old_ctrl = dc->port[port].ctrl_dl;
976 dc->port[port].ctrl_dl = ctrl_dl;
977
978 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
979 DBG1("Disable interrupt (0x%04X) on port: %d",
980 enable_ier, port);
981 disable_transmit_ul(port, dc);
982
983 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
984
985 if (kfifo_len(&dc->port[port].fifo_ul)) {
986 DBG1("Enable interrupt (0x%04X) on port: %d",
987 enable_ier, port);
988 DBG1("Data in buffer [%d], enable transmit! ",
989 kfifo_len(&dc->port[port].fifo_ul));
990 enable_transmit_ul(port, dc);
991 } else {
992 DBG1("No data in buffer...");
993 }
994 }
995
996 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
997 DBG1(" No change in mctrl");
998 return 1;
999 }
1000 /* Update statistics */
1001 if (old_ctrl.CTS != ctrl_dl.CTS)
1002 dc->port[port].tty_icount.cts++;
1003 if (old_ctrl.DSR != ctrl_dl.DSR)
1004 dc->port[port].tty_icount.dsr++;
1005 if (old_ctrl.RI != ctrl_dl.RI)
1006 dc->port[port].tty_icount.rng++;
1007 if (old_ctrl.DCD != ctrl_dl.DCD)
1008 dc->port[port].tty_icount.dcd++;
1009
1010 wake_up_interruptible(&dc->port[port].tty_wait);
1011
1012 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1013 port,
1014 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1015 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1016
1017 return 1;
1018}
1019
1020static enum ctrl_port_type port2ctrl(enum port_type port,
1021 const struct nozomi *dc)
1022{
1023 switch (port) {
1024 case PORT_MDM:
1025 return CTRL_MDM;
1026 case PORT_DIAG:
1027 return CTRL_DIAG;
1028 case PORT_APP1:
1029 return CTRL_APP1;
1030 case PORT_APP2:
1031 return CTRL_APP2;
1032 default:
1033 dev_err(&dc->pdev->dev,
1034 "ERROR: send flow control " \
1035 "received for non-existing port\n");
1036 };
1037 return CTRL_ERROR;
1038}
1039
1040/*
1041 * Send flow control, can only update one channel at a time
1042 * Return 0 - If we have updated all flow control
1043 * Return 1 - If we need to update more flow control, ack current enable more
1044 */
1045static int send_flow_control(struct nozomi *dc)
1046{
1047 u32 i, more_flow_control_to_be_updated = 0;
1048 u16 *ctrl;
1049
1050 for (i = PORT_MDM; i < MAX_PORT; i++) {
1051 if (dc->port[i].update_flow_control) {
1052 if (more_flow_control_to_be_updated) {
1053 /* We have more flow control to be updated */
1054 return 1;
1055 }
1056 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1057 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1058 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1059 (u32 *) ctrl, 2);
1060 dc->port[i].update_flow_control = 0;
1061 more_flow_control_to_be_updated = 1;
1062 }
1063 }
1064 return 0;
1065}
1066
1067/*
1068 * Handle downlink data, ports that are handled are modem and diagnostics
1069 * Return 1 - ok
1070 * Return 0 - toggle fields are out of sync
1071 */
1072static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1073 u16 read_iir, u16 mask1, u16 mask2)
1074{
1075 if (*toggle == 0 && read_iir & mask1) {
1076 if (receive_data(port, dc)) {
1077 writew(mask1, dc->reg_fcr);
1078 *toggle = !(*toggle);
1079 }
1080
1081 if (read_iir & mask2) {
1082 if (receive_data(port, dc)) {
1083 writew(mask2, dc->reg_fcr);
1084 *toggle = !(*toggle);
1085 }
1086 }
1087 } else if (*toggle == 1 && read_iir & mask2) {
1088 if (receive_data(port, dc)) {
1089 writew(mask2, dc->reg_fcr);
1090 *toggle = !(*toggle);
1091 }
1092
1093 if (read_iir & mask1) {
1094 if (receive_data(port, dc)) {
1095 writew(mask1, dc->reg_fcr);
1096 *toggle = !(*toggle);
1097 }
1098 }
1099 } else {
1100 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1101 *toggle);
1102 return 0;
1103 }
1104 return 1;
1105}
1106
1107/*
1108 * Handle uplink data, this is currently for the modem port
1109 * Return 1 - ok
1110 * Return 0 - toggle field are out of sync
1111 */
1112static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1113{
1114 u8 *toggle = &(dc->port[port].toggle_ul);
1115
1116 if (*toggle == 0 && read_iir & MDM_UL1) {
1117 dc->last_ier &= ~MDM_UL;
1118 writew(dc->last_ier, dc->reg_ier);
1119 if (send_data(port, dc)) {
1120 writew(MDM_UL1, dc->reg_fcr);
1121 dc->last_ier = dc->last_ier | MDM_UL;
1122 writew(dc->last_ier, dc->reg_ier);
1123 *toggle = !*toggle;
1124 }
1125
1126 if (read_iir & MDM_UL2) {
1127 dc->last_ier &= ~MDM_UL;
1128 writew(dc->last_ier, dc->reg_ier);
1129 if (send_data(port, dc)) {
1130 writew(MDM_UL2, dc->reg_fcr);
1131 dc->last_ier = dc->last_ier | MDM_UL;
1132 writew(dc->last_ier, dc->reg_ier);
1133 *toggle = !*toggle;
1134 }
1135 }
1136
1137 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1138 dc->last_ier &= ~MDM_UL;
1139 writew(dc->last_ier, dc->reg_ier);
1140 if (send_data(port, dc)) {
1141 writew(MDM_UL2, dc->reg_fcr);
1142 dc->last_ier = dc->last_ier | MDM_UL;
1143 writew(dc->last_ier, dc->reg_ier);
1144 *toggle = !*toggle;
1145 }
1146
1147 if (read_iir & MDM_UL1) {
1148 dc->last_ier &= ~MDM_UL;
1149 writew(dc->last_ier, dc->reg_ier);
1150 if (send_data(port, dc)) {
1151 writew(MDM_UL1, dc->reg_fcr);
1152 dc->last_ier = dc->last_ier | MDM_UL;
1153 writew(dc->last_ier, dc->reg_ier);
1154 *toggle = !*toggle;
1155 }
1156 }
1157 } else {
1158 writew(read_iir & MDM_UL, dc->reg_fcr);
1159 dev_err(&dc->pdev->dev, "port out of sync!\n");
1160 return 0;
1161 }
1162 return 1;
1163}
1164
1165static irqreturn_t interrupt_handler(int irq, void *dev_id)
1166{
1167 struct nozomi *dc = dev_id;
1168 unsigned int a;
1169 u16 read_iir;
1170
1171 if (!dc)
1172 return IRQ_NONE;
1173
1174 spin_lock(&dc->spin_mutex);
1175 read_iir = readw(dc->reg_iir);
1176
1177 /* Card removed */
1178 if (read_iir == (u16)-1)
1179 goto none;
1180 /*
1181 * Just handle interrupt enabled in IER
1182 * (by masking with dc->last_ier)
1183 */
1184 read_iir &= dc->last_ier;
1185
1186 if (read_iir == 0)
1187 goto none;
1188
1189
1190 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1191 dc->last_ier);
1192
1193 if (read_iir & RESET) {
1194 if (unlikely(!nozomi_read_config_table(dc))) {
1195 dc->last_ier = 0x0;
1196 writew(dc->last_ier, dc->reg_ier);
1197 dev_err(&dc->pdev->dev, "Could not read status from "
1198 "card, we should disable interface\n");
1199 } else {
1200 writew(RESET, dc->reg_fcr);
1201 }
1202 /* No more useful info if this was the reset interrupt. */
1203 goto exit_handler;
1204 }
1205 if (read_iir & CTRL_UL) {
1206 DBG1("CTRL_UL");
1207 dc->last_ier &= ~CTRL_UL;
1208 writew(dc->last_ier, dc->reg_ier);
1209 if (send_flow_control(dc)) {
1210 writew(CTRL_UL, dc->reg_fcr);
1211 dc->last_ier = dc->last_ier | CTRL_UL;
1212 writew(dc->last_ier, dc->reg_ier);
1213 }
1214 }
1215 if (read_iir & CTRL_DL) {
1216 receive_flow_control(dc);
1217 writew(CTRL_DL, dc->reg_fcr);
1218 }
1219 if (read_iir & MDM_DL) {
1220 if (!handle_data_dl(dc, PORT_MDM,
1221 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1222 MDM_DL1, MDM_DL2)) {
1223 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1224 goto exit_handler;
1225 }
1226 }
1227 if (read_iir & MDM_UL) {
1228 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1229 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1230 goto exit_handler;
1231 }
1232 }
1233 if (read_iir & DIAG_DL) {
1234 if (!handle_data_dl(dc, PORT_DIAG,
1235 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1236 DIAG_DL1, DIAG_DL2)) {
1237 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1238 goto exit_handler;
1239 }
1240 }
1241 if (read_iir & DIAG_UL) {
1242 dc->last_ier &= ~DIAG_UL;
1243 writew(dc->last_ier, dc->reg_ier);
1244 if (send_data(PORT_DIAG, dc)) {
1245 writew(DIAG_UL, dc->reg_fcr);
1246 dc->last_ier = dc->last_ier | DIAG_UL;
1247 writew(dc->last_ier, dc->reg_ier);
1248 }
1249 }
1250 if (read_iir & APP1_DL) {
1251 if (receive_data(PORT_APP1, dc))
1252 writew(APP1_DL, dc->reg_fcr);
1253 }
1254 if (read_iir & APP1_UL) {
1255 dc->last_ier &= ~APP1_UL;
1256 writew(dc->last_ier, dc->reg_ier);
1257 if (send_data(PORT_APP1, dc)) {
1258 writew(APP1_UL, dc->reg_fcr);
1259 dc->last_ier = dc->last_ier | APP1_UL;
1260 writew(dc->last_ier, dc->reg_ier);
1261 }
1262 }
1263 if (read_iir & APP2_DL) {
1264 if (receive_data(PORT_APP2, dc))
1265 writew(APP2_DL, dc->reg_fcr);
1266 }
1267 if (read_iir & APP2_UL) {
1268 dc->last_ier &= ~APP2_UL;
1269 writew(dc->last_ier, dc->reg_ier);
1270 if (send_data(PORT_APP2, dc)) {
1271 writew(APP2_UL, dc->reg_fcr);
1272 dc->last_ier = dc->last_ier | APP2_UL;
1273 writew(dc->last_ier, dc->reg_ier);
1274 }
1275 }
1276
1277exit_handler:
1278 spin_unlock(&dc->spin_mutex);
1279 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1280 struct tty_struct *tty;
1281 if (test_and_clear_bit(a, &dc->flip)) {
1282 tty = tty_port_tty_get(&dc->port[a].port);
1283 if (tty)
1284 tty_flip_buffer_push(tty);
1285 tty_kref_put(tty);
1286 }
1287 }
1288 return IRQ_HANDLED;
1289none:
1290 spin_unlock(&dc->spin_mutex);
1291 return IRQ_NONE;
1292}
1293
1294static void nozomi_get_card_type(struct nozomi *dc)
1295{
1296 int i;
1297 u32 size = 0;
1298
1299 for (i = 0; i < 6; i++)
1300 size += pci_resource_len(dc->pdev, i);
1301
1302 /* Assume card type F32_8 if no match */
1303 dc->card_type = size == 2048 ? F32_2 : F32_8;
1304
1305 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1306}
1307
1308static void nozomi_setup_private_data(struct nozomi *dc)
1309{
1310 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1311 unsigned int i;
1312
1313 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1314 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1315 dc->reg_ier = (void __iomem *)(offset + R_IER);
1316 dc->last_ier = 0;
1317 dc->flip = 0;
1318
1319 dc->port[PORT_MDM].token_dl = MDM_DL;
1320 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1321 dc->port[PORT_APP1].token_dl = APP1_DL;
1322 dc->port[PORT_APP2].token_dl = APP2_DL;
1323
1324 for (i = 0; i < MAX_PORT; i++)
1325 init_waitqueue_head(&dc->port[i].tty_wait);
1326}
1327
1328static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1329 char *buf)
1330{
1331 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1332
1333 return sprintf(buf, "%d\n", dc->card_type);
1334}
1335static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1336
1337static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1338 char *buf)
1339{
1340 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1341
1342 return sprintf(buf, "%u\n", dc->open_ttys);
1343}
1344static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1345
1346static void make_sysfs_files(struct nozomi *dc)
1347{
1348 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1349 dev_err(&dc->pdev->dev,
1350 "Could not create sysfs file for card_type\n");
1351 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1352 dev_err(&dc->pdev->dev,
1353 "Could not create sysfs file for open_ttys\n");
1354}
1355
1356static void remove_sysfs_files(struct nozomi *dc)
1357{
1358 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1359 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1360}
1361
1362/* Allocate memory for one device */
1363static int __devinit nozomi_card_init(struct pci_dev *pdev,
1364 const struct pci_device_id *ent)
1365{
1366 resource_size_t start;
1367 int ret;
1368 struct nozomi *dc = NULL;
1369 int ndev_idx;
1370 int i;
1371
1372 dev_dbg(&pdev->dev, "Init, new card found\n");
1373
1374 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1375 if (!ndevs[ndev_idx])
1376 break;
1377
1378 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1379 dev_err(&pdev->dev, "no free tty range for this card left\n");
1380 ret = -EIO;
1381 goto err;
1382 }
1383
1384 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1385 if (unlikely(!dc)) {
1386 dev_err(&pdev->dev, "Could not allocate memory\n");
1387 ret = -ENOMEM;
1388 goto err_free;
1389 }
1390
1391 dc->pdev = pdev;
1392
1393 ret = pci_enable_device(dc->pdev);
1394 if (ret) {
1395 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1396 goto err_free;
1397 }
1398
1399 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1400 if (ret) {
1401 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1402 (int) /* nozomi_private.io_addr */ 0);
1403 goto err_disable_device;
1404 }
1405
1406 start = pci_resource_start(dc->pdev, 0);
1407 if (start == 0) {
1408 dev_err(&pdev->dev, "No I/O address for card detected\n");
1409 ret = -ENODEV;
1410 goto err_rel_regs;
1411 }
1412
1413 /* Find out what card type it is */
1414 nozomi_get_card_type(dc);
1415
1416 dc->base_addr = ioremap_nocache(start, dc->card_type);
1417 if (!dc->base_addr) {
1418 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1419 ret = -ENODEV;
1420 goto err_rel_regs;
1421 }
1422
1423 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1424 if (!dc->send_buf) {
1425 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1426 ret = -ENOMEM;
1427 goto err_free_sbuf;
1428 }
1429
1430 for (i = PORT_MDM; i < MAX_PORT; i++) {
1431 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
1432 GFP_KERNEL)) {
1433 dev_err(&pdev->dev,
1434 "Could not allocate kfifo buffer\n");
1435 ret = -ENOMEM;
1436 goto err_free_kfifo;
1437 }
1438 }
1439
1440 spin_lock_init(&dc->spin_mutex);
1441
1442 nozomi_setup_private_data(dc);
1443
1444 /* Disable all interrupts */
1445 dc->last_ier = 0;
1446 writew(dc->last_ier, dc->reg_ier);
1447
1448 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1449 NOZOMI_NAME, dc);
1450 if (unlikely(ret)) {
1451 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1452 goto err_free_kfifo;
1453 }
1454
1455 DBG1("base_addr: %p", dc->base_addr);
1456
1457 make_sysfs_files(dc);
1458
1459 dc->index_start = ndev_idx * MAX_PORT;
1460 ndevs[ndev_idx] = dc;
1461
1462 pci_set_drvdata(pdev, dc);
1463
1464 /* Enable RESET interrupt */
1465 dc->last_ier = RESET;
1466 iowrite16(dc->last_ier, dc->reg_ier);
1467
1468 dc->state = NOZOMI_STATE_ENABLED;
1469
1470 for (i = 0; i < MAX_PORT; i++) {
1471 struct device *tty_dev;
1472 struct port *port = &dc->port[i];
1473 port->dc = dc;
1474 tty_port_init(&port->port);
1475 port->port.ops = &noz_tty_port_ops;
1476 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
1477 &pdev->dev);
1478
1479 if (IS_ERR(tty_dev)) {
1480 ret = PTR_ERR(tty_dev);
1481 dev_err(&pdev->dev, "Could not allocate tty?\n");
1482 goto err_free_tty;
1483 }
1484 }
1485
1486 return 0;
1487
1488err_free_tty:
1489 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1490 tty_unregister_device(ntty_driver, i);
1491err_free_kfifo:
1492 for (i = 0; i < MAX_PORT; i++)
1493 kfifo_free(&dc->port[i].fifo_ul);
1494err_free_sbuf:
1495 kfree(dc->send_buf);
1496 iounmap(dc->base_addr);
1497err_rel_regs:
1498 pci_release_regions(pdev);
1499err_disable_device:
1500 pci_disable_device(pdev);
1501err_free:
1502 kfree(dc);
1503err:
1504 return ret;
1505}
1506
1507static void __devexit tty_exit(struct nozomi *dc)
1508{
1509 unsigned int i;
1510
1511 DBG1(" ");
1512
1513 for (i = 0; i < MAX_PORT; ++i) {
1514 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1515 if (tty && list_empty(&tty->hangup_work.entry))
1516 tty_hangup(tty);
1517 tty_kref_put(tty);
1518 }
1519 /* Racy below - surely should wait for scheduled work to be done or
1520 complete off a hangup method ? */
1521 while (dc->open_ttys)
1522 msleep(1);
1523 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1524 tty_unregister_device(ntty_driver, i);
1525}
1526
1527/* Deallocate memory for one device */
1528static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1529{
1530 int i;
1531 struct ctrl_ul ctrl;
1532 struct nozomi *dc = pci_get_drvdata(pdev);
1533
1534 /* Disable all interrupts */
1535 dc->last_ier = 0;
1536 writew(dc->last_ier, dc->reg_ier);
1537
1538 tty_exit(dc);
1539
1540 /* Send 0x0001, command card to resend the reset token. */
1541 /* This is to get the reset when the module is reloaded. */
1542 ctrl.port = 0x00;
1543 ctrl.reserved = 0;
1544 ctrl.RTS = 0;
1545 ctrl.DTR = 1;
1546 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1547
1548 /* Setup dc->reg addresses to we can use defines here */
1549 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1550 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1551
1552 remove_sysfs_files(dc);
1553
1554 free_irq(pdev->irq, dc);
1555
1556 for (i = 0; i < MAX_PORT; i++)
1557 kfifo_free(&dc->port[i].fifo_ul);
1558
1559 kfree(dc->send_buf);
1560
1561 iounmap(dc->base_addr);
1562
1563 pci_release_regions(pdev);
1564
1565 pci_disable_device(pdev);
1566
1567 ndevs[dc->index_start / MAX_PORT] = NULL;
1568
1569 kfree(dc);
1570}
1571
1572static void set_rts(const struct tty_struct *tty, int rts)
1573{
1574 struct port *port = get_port_by_tty(tty);
1575
1576 port->ctrl_ul.RTS = rts;
1577 port->update_flow_control = 1;
1578 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1579}
1580
1581static void set_dtr(const struct tty_struct *tty, int dtr)
1582{
1583 struct port *port = get_port_by_tty(tty);
1584
1585 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1586
1587 port->ctrl_ul.DTR = dtr;
1588 port->update_flow_control = 1;
1589 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1590}
1591
1592/*
1593 * ----------------------------------------------------------------------------
1594 * TTY code
1595 * ----------------------------------------------------------------------------
1596 */
1597
1598static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
1599{
1600 struct port *port = get_port_by_tty(tty);
1601 struct nozomi *dc = get_dc_by_tty(tty);
1602 int ret;
1603 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1604 return -ENODEV;
1605 ret = tty_init_termios(tty);
1606 if (ret == 0) {
1607 tty_driver_kref_get(driver);
1608 tty->count++;
1609 tty->driver_data = port;
1610 driver->ttys[tty->index] = tty;
1611 }
1612 return ret;
1613}
1614
1615static void ntty_cleanup(struct tty_struct *tty)
1616{
1617 tty->driver_data = NULL;
1618}
1619
1620static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1621{
1622 struct port *port = container_of(tport, struct port, port);
1623 struct nozomi *dc = port->dc;
1624 unsigned long flags;
1625
1626 DBG1("open: %d", port->token_dl);
1627 spin_lock_irqsave(&dc->spin_mutex, flags);
1628 dc->last_ier = dc->last_ier | port->token_dl;
1629 writew(dc->last_ier, dc->reg_ier);
1630 dc->open_ttys++;
1631 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1632 printk("noz: activated %d: %p\n", tty->index, tport);
1633 return 0;
1634}
1635
1636static int ntty_open(struct tty_struct *tty, struct file *filp)
1637{
1638 struct port *port = tty->driver_data;
1639 return tty_port_open(&port->port, tty, filp);
1640}
1641
1642static void ntty_shutdown(struct tty_port *tport)
1643{
1644 struct port *port = container_of(tport, struct port, port);
1645 struct nozomi *dc = port->dc;
1646 unsigned long flags;
1647
1648 DBG1("close: %d", port->token_dl);
1649 spin_lock_irqsave(&dc->spin_mutex, flags);
1650 dc->last_ier &= ~(port->token_dl);
1651 writew(dc->last_ier, dc->reg_ier);
1652 dc->open_ttys--;
1653 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1654 printk("noz: shutdown %p\n", tport);
1655}
1656
1657static void ntty_close(struct tty_struct *tty, struct file *filp)
1658{
1659 struct port *port = tty->driver_data;
1660 if (port)
1661 tty_port_close(&port->port, tty, filp);
1662}
1663
1664static void ntty_hangup(struct tty_struct *tty)
1665{
1666 struct port *port = tty->driver_data;
1667 tty_port_hangup(&port->port);
1668}
1669
1670/*
1671 * called when the userspace process writes to the tty (/dev/noz*).
1672 * Data is inserted into a fifo, which is then read and transferred to the modem.
1673 */
1674static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1675 int count)
1676{
1677 int rval = -EINVAL;
1678 struct nozomi *dc = get_dc_by_tty(tty);
1679 struct port *port = tty->driver_data;
1680 unsigned long flags;
1681
1682 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1683
1684 if (!dc || !port)
1685 return -ENODEV;
1686
1687 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
1688
1689 /* notify card */
1690 if (unlikely(dc == NULL)) {
1691 DBG1("No device context?");
1692 goto exit;
1693 }
1694
1695 spin_lock_irqsave(&dc->spin_mutex, flags);
1696 /* CTS is only valid on the modem channel */
1697 if (port == &(dc->port[PORT_MDM])) {
1698 if (port->ctrl_dl.CTS) {
1699 DBG4("Enable interrupt");
1700 enable_transmit_ul(tty->index % MAX_PORT, dc);
1701 } else {
1702 dev_err(&dc->pdev->dev,
1703 "CTS not active on modem port?\n");
1704 }
1705 } else {
1706 enable_transmit_ul(tty->index % MAX_PORT, dc);
1707 }
1708 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1709
1710exit:
1711 return rval;
1712}
1713
1714/*
1715 * Calculate how much is left in device
1716 * This method is called by the upper tty layer.
1717 * #according to sources N_TTY.c it expects a value >= 0 and
1718 * does not check for negative values.
1719 *
1720 * If the port is unplugged report lots of room and let the bits
1721 * dribble away so we don't block anything.
1722 */
1723static int ntty_write_room(struct tty_struct *tty)
1724{
1725 struct port *port = tty->driver_data;
1726 int room = 4096;
1727 const struct nozomi *dc = get_dc_by_tty(tty);
1728
1729 if (dc)
1730 room = kfifo_avail(&port->fifo_ul);
1731
1732 return room;
1733}
1734
1735/* Gets io control parameters */
1736static int ntty_tiocmget(struct tty_struct *tty)
1737{
1738 const struct port *port = tty->driver_data;
1739 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1740 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1741
1742 /* Note: these could change under us but it is not clear this
1743 matters if so */
1744 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1745 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1746 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1747 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1748 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1749 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1750}
1751
1752/* Sets io controls parameters */
1753static int ntty_tiocmset(struct tty_struct *tty,
1754 unsigned int set, unsigned int clear)
1755{
1756 struct nozomi *dc = get_dc_by_tty(tty);
1757 unsigned long flags;
1758
1759 spin_lock_irqsave(&dc->spin_mutex, flags);
1760 if (set & TIOCM_RTS)
1761 set_rts(tty, 1);
1762 else if (clear & TIOCM_RTS)
1763 set_rts(tty, 0);
1764
1765 if (set & TIOCM_DTR)
1766 set_dtr(tty, 1);
1767 else if (clear & TIOCM_DTR)
1768 set_dtr(tty, 0);
1769 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1770
1771 return 0;
1772}
1773
1774static int ntty_cflags_changed(struct port *port, unsigned long flags,
1775 struct async_icount *cprev)
1776{
1777 const struct async_icount cnow = port->tty_icount;
1778 int ret;
1779
1780 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1781 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1782 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1783 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1784
1785 *cprev = cnow;
1786
1787 return ret;
1788}
1789
1790static int ntty_tiocgicount(struct tty_struct *tty,
1791 struct serial_icounter_struct *icount)
1792{
1793 struct port *port = tty->driver_data;
1794 const struct async_icount cnow = port->tty_icount;
1795
1796 icount->cts = cnow.cts;
1797 icount->dsr = cnow.dsr;
1798 icount->rng = cnow.rng;
1799 icount->dcd = cnow.dcd;
1800 icount->rx = cnow.rx;
1801 icount->tx = cnow.tx;
1802 icount->frame = cnow.frame;
1803 icount->overrun = cnow.overrun;
1804 icount->parity = cnow.parity;
1805 icount->brk = cnow.brk;
1806 icount->buf_overrun = cnow.buf_overrun;
1807 return 0;
1808}
1809
1810static int ntty_ioctl(struct tty_struct *tty,
1811 unsigned int cmd, unsigned long arg)
1812{
1813 struct port *port = tty->driver_data;
1814 int rval = -ENOIOCTLCMD;
1815
1816 DBG1("******** IOCTL, cmd: %d", cmd);
1817
1818 switch (cmd) {
1819 case TIOCMIWAIT: {
1820 struct async_icount cprev = port->tty_icount;
1821
1822 rval = wait_event_interruptible(port->tty_wait,
1823 ntty_cflags_changed(port, arg, &cprev));
1824 break;
1825 }
1826 default:
1827 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1828 break;
1829 };
1830
1831 return rval;
1832}
1833
1834/*
1835 * Called by the upper tty layer when tty buffers are ready
1836 * to receive data again after a call to throttle.
1837 */
1838static void ntty_unthrottle(struct tty_struct *tty)
1839{
1840 struct nozomi *dc = get_dc_by_tty(tty);
1841 unsigned long flags;
1842
1843 DBG1("UNTHROTTLE");
1844 spin_lock_irqsave(&dc->spin_mutex, flags);
1845 enable_transmit_dl(tty->index % MAX_PORT, dc);
1846 set_rts(tty, 1);
1847
1848 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1849}
1850
1851/*
1852 * Called by the upper tty layer when the tty buffers are almost full.
1853 * The driver should stop send more data.
1854 */
1855static void ntty_throttle(struct tty_struct *tty)
1856{
1857 struct nozomi *dc = get_dc_by_tty(tty);
1858 unsigned long flags;
1859
1860 DBG1("THROTTLE");
1861 spin_lock_irqsave(&dc->spin_mutex, flags);
1862 set_rts(tty, 0);
1863 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1864}
1865
1866/* Returns number of chars in buffer, called by tty layer */
1867static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1868{
1869 struct port *port = tty->driver_data;
1870 struct nozomi *dc = get_dc_by_tty(tty);
1871 s32 rval = 0;
1872
1873 if (unlikely(!dc || !port)) {
1874 goto exit_in_buffer;
1875 }
1876
1877 rval = kfifo_len(&port->fifo_ul);
1878
1879exit_in_buffer:
1880 return rval;
1881}
1882
1883static const struct tty_port_operations noz_tty_port_ops = {
1884 .activate = ntty_activate,
1885 .shutdown = ntty_shutdown,
1886};
1887
1888static const struct tty_operations tty_ops = {
1889 .ioctl = ntty_ioctl,
1890 .open = ntty_open,
1891 .close = ntty_close,
1892 .hangup = ntty_hangup,
1893 .write = ntty_write,
1894 .write_room = ntty_write_room,
1895 .unthrottle = ntty_unthrottle,
1896 .throttle = ntty_throttle,
1897 .chars_in_buffer = ntty_chars_in_buffer,
1898 .tiocmget = ntty_tiocmget,
1899 .tiocmset = ntty_tiocmset,
1900 .get_icount = ntty_tiocgicount,
1901 .install = ntty_install,
1902 .cleanup = ntty_cleanup,
1903};
1904
1905/* Module initialization */
1906static struct pci_driver nozomi_driver = {
1907 .name = NOZOMI_NAME,
1908 .id_table = nozomi_pci_tbl,
1909 .probe = nozomi_card_init,
1910 .remove = __devexit_p(nozomi_card_exit),
1911};
1912
1913static __init int nozomi_init(void)
1914{
1915 int ret;
1916
1917 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1918
1919 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1920 if (!ntty_driver)
1921 return -ENOMEM;
1922
1923 ntty_driver->owner = THIS_MODULE;
1924 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1925 ntty_driver->name = "noz";
1926 ntty_driver->major = 0;
1927 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1928 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1929 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1930 ntty_driver->init_termios = tty_std_termios;
1931 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1932 HUPCL | CLOCAL;
1933 ntty_driver->init_termios.c_ispeed = 115200;
1934 ntty_driver->init_termios.c_ospeed = 115200;
1935 tty_set_operations(ntty_driver, &tty_ops);
1936
1937 ret = tty_register_driver(ntty_driver);
1938 if (ret) {
1939 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1940 goto free_tty;
1941 }
1942
1943 ret = pci_register_driver(&nozomi_driver);
1944 if (ret) {
1945 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1946 goto unr_tty;
1947 }
1948
1949 return 0;
1950unr_tty:
1951 tty_unregister_driver(ntty_driver);
1952free_tty:
1953 put_tty_driver(ntty_driver);
1954 return ret;
1955}
1956
1957static __exit void nozomi_exit(void)
1958{
1959 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1960 pci_unregister_driver(&nozomi_driver);
1961 tty_unregister_driver(ntty_driver);
1962 put_tty_driver(ntty_driver);
1963}
1964
1965module_init(nozomi_init);
1966module_exit(nozomi_exit);
1967
1968module_param(debug, int, S_IRUGO | S_IWUSR);
1969
1970MODULE_LICENSE("Dual BSD/GPL");
1971MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/tty/pty.c b/drivers/tty/pty.c
new file mode 100644
index 000000000000..98b6e3bdb000
--- /dev/null
+++ b/drivers/tty/pty.c
@@ -0,0 +1,777 @@
1/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 *
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
6 *
7 * When reading this code see also fs/devpts. In particular note that the
8 * driver_data field is used by the devpts side as a binding to the devpts
9 * inode.
10 */
11
12#include <linux/module.h>
13
14#include <linux/errno.h>
15#include <linux/interrupt.h>
16#include <linux/tty.h>
17#include <linux/tty_flip.h>
18#include <linux/fcntl.h>
19#include <linux/sched.h>
20#include <linux/string.h>
21#include <linux/major.h>
22#include <linux/mm.h>
23#include <linux/init.h>
24#include <linux/sysctl.h>
25#include <linux/device.h>
26#include <linux/uaccess.h>
27#include <linux/bitops.h>
28#include <linux/devpts_fs.h>
29#include <linux/slab.h>
30
31#include <asm/system.h>
32
33#ifdef CONFIG_UNIX98_PTYS
34static struct tty_driver *ptm_driver;
35static struct tty_driver *pts_driver;
36#endif
37
38static void pty_close(struct tty_struct *tty, struct file *filp)
39{
40 BUG_ON(!tty);
41 if (tty->driver->subtype == PTY_TYPE_MASTER)
42 WARN_ON(tty->count > 1);
43 else {
44 if (tty->count > 2)
45 return;
46 }
47 wake_up_interruptible(&tty->read_wait);
48 wake_up_interruptible(&tty->write_wait);
49 tty->packet = 0;
50 if (!tty->link)
51 return;
52 tty->link->packet = 0;
53 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
54 wake_up_interruptible(&tty->link->read_wait);
55 wake_up_interruptible(&tty->link->write_wait);
56 if (tty->driver->subtype == PTY_TYPE_MASTER) {
57 set_bit(TTY_OTHER_CLOSED, &tty->flags);
58#ifdef CONFIG_UNIX98_PTYS
59 if (tty->driver == ptm_driver)
60 devpts_pty_kill(tty->link);
61#endif
62 tty_unlock();
63 tty_vhangup(tty->link);
64 tty_lock();
65 }
66}
67
68/*
69 * The unthrottle routine is called by the line discipline to signal
70 * that it can receive more characters. For PTY's, the TTY_THROTTLED
71 * flag is always set, to force the line discipline to always call the
72 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
73 * characters in the queue. This is necessary since each time this
74 * happens, we need to wake up any sleeping processes that could be
75 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
76 * for the pty buffer to be drained.
77 */
78static void pty_unthrottle(struct tty_struct *tty)
79{
80 tty_wakeup(tty->link);
81 set_bit(TTY_THROTTLED, &tty->flags);
82}
83
84/**
85 * pty_space - report space left for writing
86 * @to: tty we are writing into
87 *
88 * The tty buffers allow 64K but we sneak a peak and clip at 8K this
89 * allows a lot of overspill room for echo and other fun messes to
90 * be handled properly
91 */
92
93static int pty_space(struct tty_struct *to)
94{
95 int n = 8192 - to->buf.memory_used;
96 if (n < 0)
97 return 0;
98 return n;
99}
100
101/**
102 * pty_write - write to a pty
103 * @tty: the tty we write from
104 * @buf: kernel buffer of data
105 * @count: bytes to write
106 *
107 * Our "hardware" write method. Data is coming from the ldisc which
108 * may be in a non sleeping state. We simply throw this at the other
109 * end of the link as if we were an IRQ handler receiving stuff for
110 * the other side of the pty/tty pair.
111 */
112
113static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
114{
115 struct tty_struct *to = tty->link;
116
117 if (tty->stopped)
118 return 0;
119
120 if (c > 0) {
121 /* Stuff the data into the input queue of the other end */
122 c = tty_insert_flip_string(to, buf, c);
123 /* And shovel */
124 if (c) {
125 tty_flip_buffer_push(to);
126 tty_wakeup(tty);
127 }
128 }
129 return c;
130}
131
132/**
133 * pty_write_room - write space
134 * @tty: tty we are writing from
135 *
136 * Report how many bytes the ldisc can send into the queue for
137 * the other device.
138 */
139
140static int pty_write_room(struct tty_struct *tty)
141{
142 if (tty->stopped)
143 return 0;
144 return pty_space(tty->link);
145}
146
147/**
148 * pty_chars_in_buffer - characters currently in our tx queue
149 * @tty: our tty
150 *
151 * Report how much we have in the transmit queue. As everything is
152 * instantly at the other end this is easy to implement.
153 */
154
155static int pty_chars_in_buffer(struct tty_struct *tty)
156{
157 return 0;
158}
159
160/* Set the lock flag on a pty */
161static int pty_set_lock(struct tty_struct *tty, int __user *arg)
162{
163 int val;
164 if (get_user(val, arg))
165 return -EFAULT;
166 if (val)
167 set_bit(TTY_PTY_LOCK, &tty->flags);
168 else
169 clear_bit(TTY_PTY_LOCK, &tty->flags);
170 return 0;
171}
172
173/* Send a signal to the slave */
174static int pty_signal(struct tty_struct *tty, int sig)
175{
176 unsigned long flags;
177 struct pid *pgrp;
178
179 if (tty->link) {
180 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
181 pgrp = get_pid(tty->link->pgrp);
182 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
183
184 kill_pgrp(pgrp, sig, 1);
185 put_pid(pgrp);
186 }
187 return 0;
188}
189
190static void pty_flush_buffer(struct tty_struct *tty)
191{
192 struct tty_struct *to = tty->link;
193 unsigned long flags;
194
195 if (!to)
196 return;
197 /* tty_buffer_flush(to); FIXME */
198 if (to->packet) {
199 spin_lock_irqsave(&tty->ctrl_lock, flags);
200 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
201 wake_up_interruptible(&to->read_wait);
202 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
203 }
204}
205
206static int pty_open(struct tty_struct *tty, struct file *filp)
207{
208 int retval = -ENODEV;
209
210 if (!tty || !tty->link)
211 goto out;
212
213 retval = -EIO;
214 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
215 goto out;
216 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
217 goto out;
218 if (tty->link->count != 1)
219 goto out;
220
221 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
222 set_bit(TTY_THROTTLED, &tty->flags);
223 retval = 0;
224out:
225 return retval;
226}
227
228static void pty_set_termios(struct tty_struct *tty,
229 struct ktermios *old_termios)
230{
231 tty->termios->c_cflag &= ~(CSIZE | PARENB);
232 tty->termios->c_cflag |= (CS8 | CREAD);
233}
234
235/**
236 * pty_do_resize - resize event
237 * @tty: tty being resized
238 * @ws: window size being set.
239 *
240 * Update the termios variables and send the necessary signals to
241 * peform a terminal resize correctly
242 */
243
244int pty_resize(struct tty_struct *tty, struct winsize *ws)
245{
246 struct pid *pgrp, *rpgrp;
247 unsigned long flags;
248 struct tty_struct *pty = tty->link;
249
250 /* For a PTY we need to lock the tty side */
251 mutex_lock(&tty->termios_mutex);
252 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
253 goto done;
254
255 /* Get the PID values and reference them so we can
256 avoid holding the tty ctrl lock while sending signals.
257 We need to lock these individually however. */
258
259 spin_lock_irqsave(&tty->ctrl_lock, flags);
260 pgrp = get_pid(tty->pgrp);
261 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
262
263 spin_lock_irqsave(&pty->ctrl_lock, flags);
264 rpgrp = get_pid(pty->pgrp);
265 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
266
267 if (pgrp)
268 kill_pgrp(pgrp, SIGWINCH, 1);
269 if (rpgrp != pgrp && rpgrp)
270 kill_pgrp(rpgrp, SIGWINCH, 1);
271
272 put_pid(pgrp);
273 put_pid(rpgrp);
274
275 tty->winsize = *ws;
276 pty->winsize = *ws; /* Never used so will go away soon */
277done:
278 mutex_unlock(&tty->termios_mutex);
279 return 0;
280}
281
282/* Traditional BSD devices */
283#ifdef CONFIG_LEGACY_PTYS
284
285static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
286{
287 struct tty_struct *o_tty;
288 int idx = tty->index;
289 int retval;
290
291 o_tty = alloc_tty_struct();
292 if (!o_tty)
293 return -ENOMEM;
294 if (!try_module_get(driver->other->owner)) {
295 /* This cannot in fact currently happen */
296 retval = -ENOMEM;
297 goto err_free_tty;
298 }
299 initialize_tty_struct(o_tty, driver->other, idx);
300
301 /* We always use new tty termios data so we can do this
302 the easy way .. */
303 retval = tty_init_termios(tty);
304 if (retval)
305 goto err_deinit_tty;
306
307 retval = tty_init_termios(o_tty);
308 if (retval)
309 goto err_free_termios;
310
311 /*
312 * Everything allocated ... set up the o_tty structure.
313 */
314 driver->other->ttys[idx] = o_tty;
315 tty_driver_kref_get(driver->other);
316 if (driver->subtype == PTY_TYPE_MASTER)
317 o_tty->count++;
318 /* Establish the links in both directions */
319 tty->link = o_tty;
320 o_tty->link = tty;
321
322 tty_driver_kref_get(driver);
323 tty->count++;
324 driver->ttys[idx] = tty;
325 return 0;
326err_free_termios:
327 tty_free_termios(tty);
328err_deinit_tty:
329 deinitialize_tty_struct(o_tty);
330 module_put(o_tty->driver->owner);
331err_free_tty:
332 free_tty_struct(o_tty);
333 return retval;
334}
335
336static int pty_bsd_ioctl(struct tty_struct *tty,
337 unsigned int cmd, unsigned long arg)
338{
339 switch (cmd) {
340 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
341 return pty_set_lock(tty, (int __user *) arg);
342 case TIOCSIG: /* Send signal to other side of pty */
343 return pty_signal(tty, (int) arg);
344 }
345 return -ENOIOCTLCMD;
346}
347
348static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
349module_param(legacy_count, int, 0);
350
351/*
352 * The master side of a pty can do TIOCSPTLCK and thus
353 * has pty_bsd_ioctl.
354 */
355static const struct tty_operations master_pty_ops_bsd = {
356 .install = pty_install,
357 .open = pty_open,
358 .close = pty_close,
359 .write = pty_write,
360 .write_room = pty_write_room,
361 .flush_buffer = pty_flush_buffer,
362 .chars_in_buffer = pty_chars_in_buffer,
363 .unthrottle = pty_unthrottle,
364 .set_termios = pty_set_termios,
365 .ioctl = pty_bsd_ioctl,
366 .resize = pty_resize
367};
368
369static const struct tty_operations slave_pty_ops_bsd = {
370 .install = pty_install,
371 .open = pty_open,
372 .close = pty_close,
373 .write = pty_write,
374 .write_room = pty_write_room,
375 .flush_buffer = pty_flush_buffer,
376 .chars_in_buffer = pty_chars_in_buffer,
377 .unthrottle = pty_unthrottle,
378 .set_termios = pty_set_termios,
379 .resize = pty_resize
380};
381
382static void __init legacy_pty_init(void)
383{
384 struct tty_driver *pty_driver, *pty_slave_driver;
385
386 if (legacy_count <= 0)
387 return;
388
389 pty_driver = alloc_tty_driver(legacy_count);
390 if (!pty_driver)
391 panic("Couldn't allocate pty driver");
392
393 pty_slave_driver = alloc_tty_driver(legacy_count);
394 if (!pty_slave_driver)
395 panic("Couldn't allocate pty slave driver");
396
397 pty_driver->owner = THIS_MODULE;
398 pty_driver->driver_name = "pty_master";
399 pty_driver->name = "pty";
400 pty_driver->major = PTY_MASTER_MAJOR;
401 pty_driver->minor_start = 0;
402 pty_driver->type = TTY_DRIVER_TYPE_PTY;
403 pty_driver->subtype = PTY_TYPE_MASTER;
404 pty_driver->init_termios = tty_std_termios;
405 pty_driver->init_termios.c_iflag = 0;
406 pty_driver->init_termios.c_oflag = 0;
407 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
408 pty_driver->init_termios.c_lflag = 0;
409 pty_driver->init_termios.c_ispeed = 38400;
410 pty_driver->init_termios.c_ospeed = 38400;
411 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
412 pty_driver->other = pty_slave_driver;
413 tty_set_operations(pty_driver, &master_pty_ops_bsd);
414
415 pty_slave_driver->owner = THIS_MODULE;
416 pty_slave_driver->driver_name = "pty_slave";
417 pty_slave_driver->name = "ttyp";
418 pty_slave_driver->major = PTY_SLAVE_MAJOR;
419 pty_slave_driver->minor_start = 0;
420 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
421 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
422 pty_slave_driver->init_termios = tty_std_termios;
423 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
424 pty_slave_driver->init_termios.c_ispeed = 38400;
425 pty_slave_driver->init_termios.c_ospeed = 38400;
426 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
427 TTY_DRIVER_REAL_RAW;
428 pty_slave_driver->other = pty_driver;
429 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
430
431 if (tty_register_driver(pty_driver))
432 panic("Couldn't register pty driver");
433 if (tty_register_driver(pty_slave_driver))
434 panic("Couldn't register pty slave driver");
435}
436#else
437static inline void legacy_pty_init(void) { }
438#endif
439
440/* Unix98 devices */
441#ifdef CONFIG_UNIX98_PTYS
442/*
443 * sysctl support for setting limits on the number of Unix98 ptys allocated.
444 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
445 */
446int pty_limit = NR_UNIX98_PTY_DEFAULT;
447static int pty_limit_min;
448static int pty_limit_max = NR_UNIX98_PTY_MAX;
449static int pty_count;
450
451static struct cdev ptmx_cdev;
452
453static struct ctl_table pty_table[] = {
454 {
455 .procname = "max",
456 .maxlen = sizeof(int),
457 .mode = 0644,
458 .data = &pty_limit,
459 .proc_handler = proc_dointvec_minmax,
460 .extra1 = &pty_limit_min,
461 .extra2 = &pty_limit_max,
462 }, {
463 .procname = "nr",
464 .maxlen = sizeof(int),
465 .mode = 0444,
466 .data = &pty_count,
467 .proc_handler = proc_dointvec,
468 },
469 {}
470};
471
472static struct ctl_table pty_kern_table[] = {
473 {
474 .procname = "pty",
475 .mode = 0555,
476 .child = pty_table,
477 },
478 {}
479};
480
481static struct ctl_table pty_root_table[] = {
482 {
483 .procname = "kernel",
484 .mode = 0555,
485 .child = pty_kern_table,
486 },
487 {}
488};
489
490
491static int pty_unix98_ioctl(struct tty_struct *tty,
492 unsigned int cmd, unsigned long arg)
493{
494 switch (cmd) {
495 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
496 return pty_set_lock(tty, (int __user *)arg);
497 case TIOCGPTN: /* Get PT Number */
498 return put_user(tty->index, (unsigned int __user *)arg);
499 case TIOCSIG: /* Send signal to other side of pty */
500 return pty_signal(tty, (int) arg);
501 }
502
503 return -ENOIOCTLCMD;
504}
505
506/**
507 * ptm_unix98_lookup - find a pty master
508 * @driver: ptm driver
509 * @idx: tty index
510 *
511 * Look up a pty master device. Called under the tty_mutex for now.
512 * This provides our locking.
513 */
514
515static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
516 struct inode *ptm_inode, int idx)
517{
518 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
519 if (tty)
520 tty = tty->link;
521 return tty;
522}
523
524/**
525 * pts_unix98_lookup - find a pty slave
526 * @driver: pts driver
527 * @idx: tty index
528 *
529 * Look up a pty master device. Called under the tty_mutex for now.
530 * This provides our locking.
531 */
532
533static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
534 struct inode *pts_inode, int idx)
535{
536 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
537 /* Master must be open before slave */
538 if (!tty)
539 return ERR_PTR(-EIO);
540 return tty;
541}
542
543static void pty_unix98_shutdown(struct tty_struct *tty)
544{
545 /* We have our own method as we don't use the tty index */
546 kfree(tty->termios);
547}
548
549/* We have no need to install and remove our tty objects as devpts does all
550 the work for us */
551
552static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
553{
554 struct tty_struct *o_tty;
555 int idx = tty->index;
556
557 o_tty = alloc_tty_struct();
558 if (!o_tty)
559 return -ENOMEM;
560 if (!try_module_get(driver->other->owner)) {
561 /* This cannot in fact currently happen */
562 goto err_free_tty;
563 }
564 initialize_tty_struct(o_tty, driver->other, idx);
565
566 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
567 if (tty->termios == NULL)
568 goto err_free_mem;
569 *tty->termios = driver->init_termios;
570 tty->termios_locked = tty->termios + 1;
571
572 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
573 if (o_tty->termios == NULL)
574 goto err_free_mem;
575 *o_tty->termios = driver->other->init_termios;
576 o_tty->termios_locked = o_tty->termios + 1;
577
578 tty_driver_kref_get(driver->other);
579 if (driver->subtype == PTY_TYPE_MASTER)
580 o_tty->count++;
581 /* Establish the links in both directions */
582 tty->link = o_tty;
583 o_tty->link = tty;
584 /*
585 * All structures have been allocated, so now we install them.
586 * Failures after this point use release_tty to clean up, so
587 * there's no need to null out the local pointers.
588 */
589 tty_driver_kref_get(driver);
590 tty->count++;
591 pty_count++;
592 return 0;
593err_free_mem:
594 deinitialize_tty_struct(o_tty);
595 kfree(o_tty->termios);
596 kfree(tty->termios);
597 module_put(o_tty->driver->owner);
598err_free_tty:
599 free_tty_struct(o_tty);
600 return -ENOMEM;
601}
602
603static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
604{
605 pty_count--;
606}
607
608static const struct tty_operations ptm_unix98_ops = {
609 .lookup = ptm_unix98_lookup,
610 .install = pty_unix98_install,
611 .remove = pty_unix98_remove,
612 .open = pty_open,
613 .close = pty_close,
614 .write = pty_write,
615 .write_room = pty_write_room,
616 .flush_buffer = pty_flush_buffer,
617 .chars_in_buffer = pty_chars_in_buffer,
618 .unthrottle = pty_unthrottle,
619 .set_termios = pty_set_termios,
620 .ioctl = pty_unix98_ioctl,
621 .shutdown = pty_unix98_shutdown,
622 .resize = pty_resize
623};
624
625static const struct tty_operations pty_unix98_ops = {
626 .lookup = pts_unix98_lookup,
627 .install = pty_unix98_install,
628 .remove = pty_unix98_remove,
629 .open = pty_open,
630 .close = pty_close,
631 .write = pty_write,
632 .write_room = pty_write_room,
633 .flush_buffer = pty_flush_buffer,
634 .chars_in_buffer = pty_chars_in_buffer,
635 .unthrottle = pty_unthrottle,
636 .set_termios = pty_set_termios,
637 .shutdown = pty_unix98_shutdown
638};
639
640/**
641 * ptmx_open - open a unix 98 pty master
642 * @inode: inode of device file
643 * @filp: file pointer to tty
644 *
645 * Allocate a unix98 pty master device from the ptmx driver.
646 *
647 * Locking: tty_mutex protects the init_dev work. tty->count should
648 * protect the rest.
649 * allocated_ptys_lock handles the list of free pty numbers
650 */
651
652static int ptmx_open(struct inode *inode, struct file *filp)
653{
654 struct tty_struct *tty;
655 int retval;
656 int index;
657
658 nonseekable_open(inode, filp);
659
660 /* find a device that is not in use. */
661 tty_lock();
662 index = devpts_new_index(inode);
663 tty_unlock();
664 if (index < 0)
665 return index;
666
667 mutex_lock(&tty_mutex);
668 tty_lock();
669 tty = tty_init_dev(ptm_driver, index, 1);
670 mutex_unlock(&tty_mutex);
671
672 if (IS_ERR(tty)) {
673 retval = PTR_ERR(tty);
674 goto out;
675 }
676
677 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
678
679 retval = tty_add_file(tty, filp);
680 if (retval)
681 goto out;
682
683 retval = devpts_pty_new(inode, tty->link);
684 if (retval)
685 goto out1;
686
687 retval = ptm_driver->ops->open(tty, filp);
688 if (retval)
689 goto out2;
690out1:
691 tty_unlock();
692 return retval;
693out2:
694 tty_unlock();
695 tty_release(inode, filp);
696 return retval;
697out:
698 devpts_kill_index(inode, index);
699 tty_unlock();
700 return retval;
701}
702
703static struct file_operations ptmx_fops;
704
705static void __init unix98_pty_init(void)
706{
707 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
708 if (!ptm_driver)
709 panic("Couldn't allocate Unix98 ptm driver");
710 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
711 if (!pts_driver)
712 panic("Couldn't allocate Unix98 pts driver");
713
714 ptm_driver->owner = THIS_MODULE;
715 ptm_driver->driver_name = "pty_master";
716 ptm_driver->name = "ptm";
717 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
718 ptm_driver->minor_start = 0;
719 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
720 ptm_driver->subtype = PTY_TYPE_MASTER;
721 ptm_driver->init_termios = tty_std_termios;
722 ptm_driver->init_termios.c_iflag = 0;
723 ptm_driver->init_termios.c_oflag = 0;
724 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
725 ptm_driver->init_termios.c_lflag = 0;
726 ptm_driver->init_termios.c_ispeed = 38400;
727 ptm_driver->init_termios.c_ospeed = 38400;
728 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
729 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
730 ptm_driver->other = pts_driver;
731 tty_set_operations(ptm_driver, &ptm_unix98_ops);
732
733 pts_driver->owner = THIS_MODULE;
734 pts_driver->driver_name = "pty_slave";
735 pts_driver->name = "pts";
736 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
737 pts_driver->minor_start = 0;
738 pts_driver->type = TTY_DRIVER_TYPE_PTY;
739 pts_driver->subtype = PTY_TYPE_SLAVE;
740 pts_driver->init_termios = tty_std_termios;
741 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
742 pts_driver->init_termios.c_ispeed = 38400;
743 pts_driver->init_termios.c_ospeed = 38400;
744 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
745 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
746 pts_driver->other = ptm_driver;
747 tty_set_operations(pts_driver, &pty_unix98_ops);
748
749 if (tty_register_driver(ptm_driver))
750 panic("Couldn't register Unix98 ptm driver");
751 if (tty_register_driver(pts_driver))
752 panic("Couldn't register Unix98 pts driver");
753
754 register_sysctl_table(pty_root_table);
755
756 /* Now create the /dev/ptmx special device */
757 tty_default_fops(&ptmx_fops);
758 ptmx_fops.open = ptmx_open;
759
760 cdev_init(&ptmx_cdev, &ptmx_fops);
761 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
762 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
763 panic("Couldn't register /dev/ptmx driver\n");
764 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
765}
766
767#else
768static inline void unix98_pty_init(void) { }
769#endif
770
771static int __init pty_init(void)
772{
773 legacy_pty_init();
774 unix98_pty_init();
775 return 0;
776}
777module_init(pty_init);
diff --git a/drivers/tty/rocket.c b/drivers/tty/rocket.c
new file mode 100644
index 000000000000..13043e8d37fe
--- /dev/null
+++ b/drivers/tty/rocket.c
@@ -0,0 +1,3152 @@
1/*
2 * RocketPort device driver for Linux
3 *
4 * Written by Theodore Ts'o, 1995, 1996, 1997, 1998, 1999, 2000.
5 *
6 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003 by Comtrol, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Kernel Synchronization:
25 *
26 * This driver has 2 kernel control paths - exception handlers (calls into the driver
27 * from user mode) and the timer bottom half (tasklet). This is a polled driver, interrupts
28 * are not used.
29 *
30 * Critical data:
31 * - rp_table[], accessed through passed "info" pointers, is a global (static) array of
32 * serial port state information and the xmit_buf circular buffer. Protected by
33 * a per port spinlock.
34 * - xmit_flags[], an array of ints indexed by line (port) number, indicating that there
35 * is data to be transmitted. Protected by atomic bit operations.
36 * - rp_num_ports, int indicating number of open ports, protected by atomic operations.
37 *
38 * rp_write() and rp_write_char() functions use a per port semaphore to protect against
39 * simultaneous access to the same port by more than one process.
40 */
41
42/****** Defines ******/
43#define ROCKET_PARANOIA_CHECK
44#define ROCKET_DISABLE_SIMUSAGE
45
46#undef ROCKET_SOFT_FLOW
47#undef ROCKET_DEBUG_OPEN
48#undef ROCKET_DEBUG_INTR
49#undef ROCKET_DEBUG_WRITE
50#undef ROCKET_DEBUG_FLOW
51#undef ROCKET_DEBUG_THROTTLE
52#undef ROCKET_DEBUG_WAIT_UNTIL_SENT
53#undef ROCKET_DEBUG_RECEIVE
54#undef ROCKET_DEBUG_HANGUP
55#undef REV_PCI_ORDER
56#undef ROCKET_DEBUG_IO
57
58#define POLL_PERIOD HZ/100 /* Polling period .01 seconds (10ms) */
59
60/****** Kernel includes ******/
61
62#include <linux/module.h>
63#include <linux/errno.h>
64#include <linux/major.h>
65#include <linux/kernel.h>
66#include <linux/signal.h>
67#include <linux/slab.h>
68#include <linux/mm.h>
69#include <linux/sched.h>
70#include <linux/timer.h>
71#include <linux/interrupt.h>
72#include <linux/tty.h>
73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h>
75#include <linux/serial.h>
76#include <linux/string.h>
77#include <linux/fcntl.h>
78#include <linux/ptrace.h>
79#include <linux/mutex.h>
80#include <linux/ioport.h>
81#include <linux/delay.h>
82#include <linux/completion.h>
83#include <linux/wait.h>
84#include <linux/pci.h>
85#include <linux/uaccess.h>
86#include <asm/atomic.h>
87#include <asm/unaligned.h>
88#include <linux/bitops.h>
89#include <linux/spinlock.h>
90#include <linux/init.h>
91
92/****** RocketPort includes ******/
93
94#include "rocket_int.h"
95#include "rocket.h"
96
97#define ROCKET_VERSION "2.09"
98#define ROCKET_DATE "12-June-2003"
99
100/****** RocketPort Local Variables ******/
101
102static void rp_do_poll(unsigned long dummy);
103
104static struct tty_driver *rocket_driver;
105
106static struct rocket_version driver_version = {
107 ROCKET_VERSION, ROCKET_DATE
108};
109
110static struct r_port *rp_table[MAX_RP_PORTS]; /* The main repository of serial port state information. */
111static unsigned int xmit_flags[NUM_BOARDS]; /* Bit significant, indicates port had data to transmit. */
112 /* eg. Bit 0 indicates port 0 has xmit data, ... */
113static atomic_t rp_num_ports_open; /* Number of serial ports open */
114static DEFINE_TIMER(rocket_timer, rp_do_poll, 0, 0);
115
116static unsigned long board1; /* ISA addresses, retrieved from rocketport.conf */
117static unsigned long board2;
118static unsigned long board3;
119static unsigned long board4;
120static unsigned long controller;
121static int support_low_speed;
122static unsigned long modem1;
123static unsigned long modem2;
124static unsigned long modem3;
125static unsigned long modem4;
126static unsigned long pc104_1[8];
127static unsigned long pc104_2[8];
128static unsigned long pc104_3[8];
129static unsigned long pc104_4[8];
130static unsigned long *pc104[4] = { pc104_1, pc104_2, pc104_3, pc104_4 };
131
132static int rp_baud_base[NUM_BOARDS]; /* Board config info (Someday make a per-board structure) */
133static unsigned long rcktpt_io_addr[NUM_BOARDS];
134static int rcktpt_type[NUM_BOARDS];
135static int is_PCI[NUM_BOARDS];
136static rocketModel_t rocketModel[NUM_BOARDS];
137static int max_board;
138static const struct tty_port_operations rocket_port_ops;
139
140/*
141 * The following arrays define the interrupt bits corresponding to each AIOP.
142 * These bits are different between the ISA and regular PCI boards and the
143 * Universal PCI boards.
144 */
145
146static Word_t aiop_intr_bits[AIOP_CTL_SIZE] = {
147 AIOP_INTR_BIT_0,
148 AIOP_INTR_BIT_1,
149 AIOP_INTR_BIT_2,
150 AIOP_INTR_BIT_3
151};
152
153static Word_t upci_aiop_intr_bits[AIOP_CTL_SIZE] = {
154 UPCI_AIOP_INTR_BIT_0,
155 UPCI_AIOP_INTR_BIT_1,
156 UPCI_AIOP_INTR_BIT_2,
157 UPCI_AIOP_INTR_BIT_3
158};
159
160static Byte_t RData[RDATASIZE] = {
161 0x00, 0x09, 0xf6, 0x82,
162 0x02, 0x09, 0x86, 0xfb,
163 0x04, 0x09, 0x00, 0x0a,
164 0x06, 0x09, 0x01, 0x0a,
165 0x08, 0x09, 0x8a, 0x13,
166 0x0a, 0x09, 0xc5, 0x11,
167 0x0c, 0x09, 0x86, 0x85,
168 0x0e, 0x09, 0x20, 0x0a,
169 0x10, 0x09, 0x21, 0x0a,
170 0x12, 0x09, 0x41, 0xff,
171 0x14, 0x09, 0x82, 0x00,
172 0x16, 0x09, 0x82, 0x7b,
173 0x18, 0x09, 0x8a, 0x7d,
174 0x1a, 0x09, 0x88, 0x81,
175 0x1c, 0x09, 0x86, 0x7a,
176 0x1e, 0x09, 0x84, 0x81,
177 0x20, 0x09, 0x82, 0x7c,
178 0x22, 0x09, 0x0a, 0x0a
179};
180
181static Byte_t RRegData[RREGDATASIZE] = {
182 0x00, 0x09, 0xf6, 0x82, /* 00: Stop Rx processor */
183 0x08, 0x09, 0x8a, 0x13, /* 04: Tx software flow control */
184 0x0a, 0x09, 0xc5, 0x11, /* 08: XON char */
185 0x0c, 0x09, 0x86, 0x85, /* 0c: XANY */
186 0x12, 0x09, 0x41, 0xff, /* 10: Rx mask char */
187 0x14, 0x09, 0x82, 0x00, /* 14: Compare/Ignore #0 */
188 0x16, 0x09, 0x82, 0x7b, /* 18: Compare #1 */
189 0x18, 0x09, 0x8a, 0x7d, /* 1c: Compare #2 */
190 0x1a, 0x09, 0x88, 0x81, /* 20: Interrupt #1 */
191 0x1c, 0x09, 0x86, 0x7a, /* 24: Ignore/Replace #1 */
192 0x1e, 0x09, 0x84, 0x81, /* 28: Interrupt #2 */
193 0x20, 0x09, 0x82, 0x7c, /* 2c: Ignore/Replace #2 */
194 0x22, 0x09, 0x0a, 0x0a /* 30: Rx FIFO Enable */
195};
196
197static CONTROLLER_T sController[CTL_SIZE] = {
198 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
199 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
200 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
201 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
202 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
203 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
204 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
205 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}
206};
207
208static Byte_t sBitMapClrTbl[8] = {
209 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f
210};
211
212static Byte_t sBitMapSetTbl[8] = {
213 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
214};
215
216static int sClockPrescale = 0x14;
217
218/*
219 * Line number is the ttySIx number (x), the Minor number. We
220 * assign them sequentially, starting at zero. The following
221 * array keeps track of the line number assigned to a given board/aiop/channel.
222 */
223static unsigned char lineNumbers[MAX_RP_PORTS];
224static unsigned long nextLineNumber;
225
226/***** RocketPort Static Prototypes *********/
227static int __init init_ISA(int i);
228static void rp_wait_until_sent(struct tty_struct *tty, int timeout);
229static void rp_flush_buffer(struct tty_struct *tty);
230static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model);
231static unsigned char GetLineNumber(int ctrl, int aiop, int ch);
232static unsigned char SetLineNumber(int ctrl, int aiop, int ch);
233static void rp_start(struct tty_struct *tty);
234static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
235 int ChanNum);
236static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode);
237static void sFlushRxFIFO(CHANNEL_T * ChP);
238static void sFlushTxFIFO(CHANNEL_T * ChP);
239static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags);
240static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags);
241static void sModemReset(CONTROLLER_T * CtlP, int chan, int on);
242static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on);
243static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data);
244static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
245 ByteIO_t * AiopIOList, int AiopIOListSize,
246 WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
247 int PeriodicOnly, int altChanRingIndicator,
248 int UPCIRingInd);
249static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
250 ByteIO_t * AiopIOList, int AiopIOListSize,
251 int IRQNum, Byte_t Frequency, int PeriodicOnly);
252static int sReadAiopID(ByteIO_t io);
253static int sReadAiopNumChan(WordIO_t io);
254
255MODULE_AUTHOR("Theodore Ts'o");
256MODULE_DESCRIPTION("Comtrol RocketPort driver");
257module_param(board1, ulong, 0);
258MODULE_PARM_DESC(board1, "I/O port for (ISA) board #1");
259module_param(board2, ulong, 0);
260MODULE_PARM_DESC(board2, "I/O port for (ISA) board #2");
261module_param(board3, ulong, 0);
262MODULE_PARM_DESC(board3, "I/O port for (ISA) board #3");
263module_param(board4, ulong, 0);
264MODULE_PARM_DESC(board4, "I/O port for (ISA) board #4");
265module_param(controller, ulong, 0);
266MODULE_PARM_DESC(controller, "I/O port for (ISA) rocketport controller");
267module_param(support_low_speed, bool, 0);
268MODULE_PARM_DESC(support_low_speed, "1 means support 50 baud, 0 means support 460400 baud");
269module_param(modem1, ulong, 0);
270MODULE_PARM_DESC(modem1, "1 means (ISA) board #1 is a RocketModem");
271module_param(modem2, ulong, 0);
272MODULE_PARM_DESC(modem2, "1 means (ISA) board #2 is a RocketModem");
273module_param(modem3, ulong, 0);
274MODULE_PARM_DESC(modem3, "1 means (ISA) board #3 is a RocketModem");
275module_param(modem4, ulong, 0);
276MODULE_PARM_DESC(modem4, "1 means (ISA) board #4 is a RocketModem");
277module_param_array(pc104_1, ulong, NULL, 0);
278MODULE_PARM_DESC(pc104_1, "set interface types for ISA(PC104) board #1 (e.g. pc104_1=232,232,485,485,...");
279module_param_array(pc104_2, ulong, NULL, 0);
280MODULE_PARM_DESC(pc104_2, "set interface types for ISA(PC104) board #2 (e.g. pc104_2=232,232,485,485,...");
281module_param_array(pc104_3, ulong, NULL, 0);
282MODULE_PARM_DESC(pc104_3, "set interface types for ISA(PC104) board #3 (e.g. pc104_3=232,232,485,485,...");
283module_param_array(pc104_4, ulong, NULL, 0);
284MODULE_PARM_DESC(pc104_4, "set interface types for ISA(PC104) board #4 (e.g. pc104_4=232,232,485,485,...");
285
286static int rp_init(void);
287static void rp_cleanup_module(void);
288
289module_init(rp_init);
290module_exit(rp_cleanup_module);
291
292
293MODULE_LICENSE("Dual BSD/GPL");
294
295/*************************************************************************/
296/* Module code starts here */
297
298static inline int rocket_paranoia_check(struct r_port *info,
299 const char *routine)
300{
301#ifdef ROCKET_PARANOIA_CHECK
302 if (!info)
303 return 1;
304 if (info->magic != RPORT_MAGIC) {
305 printk(KERN_WARNING "Warning: bad magic number for rocketport "
306 "struct in %s\n", routine);
307 return 1;
308 }
309#endif
310 return 0;
311}
312
313
314/* Serial port receive data function. Called (from timer poll) when an AIOPIC signals
315 * that receive data is present on a serial port. Pulls data from FIFO, moves it into the
316 * tty layer.
317 */
318static void rp_do_receive(struct r_port *info,
319 struct tty_struct *tty,
320 CHANNEL_t * cp, unsigned int ChanStatus)
321{
322 unsigned int CharNStat;
323 int ToRecv, wRecv, space;
324 unsigned char *cbuf;
325
326 ToRecv = sGetRxCnt(cp);
327#ifdef ROCKET_DEBUG_INTR
328 printk(KERN_INFO "rp_do_receive(%d)...\n", ToRecv);
329#endif
330 if (ToRecv == 0)
331 return;
332
333 /*
334 * if status indicates there are errored characters in the
335 * FIFO, then enter status mode (a word in FIFO holds
336 * character and status).
337 */
338 if (ChanStatus & (RXFOVERFL | RXBREAK | RXFRAME | RXPARITY)) {
339 if (!(ChanStatus & STATMODE)) {
340#ifdef ROCKET_DEBUG_RECEIVE
341 printk(KERN_INFO "Entering STATMODE...\n");
342#endif
343 ChanStatus |= STATMODE;
344 sEnRxStatusMode(cp);
345 }
346 }
347
348 /*
349 * if we previously entered status mode, then read down the
350 * FIFO one word at a time, pulling apart the character and
351 * the status. Update error counters depending on status
352 */
353 if (ChanStatus & STATMODE) {
354#ifdef ROCKET_DEBUG_RECEIVE
355 printk(KERN_INFO "Ignore %x, read %x...\n",
356 info->ignore_status_mask, info->read_status_mask);
357#endif
358 while (ToRecv) {
359 char flag;
360
361 CharNStat = sInW(sGetTxRxDataIO(cp));
362#ifdef ROCKET_DEBUG_RECEIVE
363 printk(KERN_INFO "%x...\n", CharNStat);
364#endif
365 if (CharNStat & STMBREAKH)
366 CharNStat &= ~(STMFRAMEH | STMPARITYH);
367 if (CharNStat & info->ignore_status_mask) {
368 ToRecv--;
369 continue;
370 }
371 CharNStat &= info->read_status_mask;
372 if (CharNStat & STMBREAKH)
373 flag = TTY_BREAK;
374 else if (CharNStat & STMPARITYH)
375 flag = TTY_PARITY;
376 else if (CharNStat & STMFRAMEH)
377 flag = TTY_FRAME;
378 else if (CharNStat & STMRCVROVRH)
379 flag = TTY_OVERRUN;
380 else
381 flag = TTY_NORMAL;
382 tty_insert_flip_char(tty, CharNStat & 0xff, flag);
383 ToRecv--;
384 }
385
386 /*
387 * after we've emptied the FIFO in status mode, turn
388 * status mode back off
389 */
390 if (sGetRxCnt(cp) == 0) {
391#ifdef ROCKET_DEBUG_RECEIVE
392 printk(KERN_INFO "Status mode off.\n");
393#endif
394 sDisRxStatusMode(cp);
395 }
396 } else {
397 /*
398 * we aren't in status mode, so read down the FIFO two
399 * characters at time by doing repeated word IO
400 * transfer.
401 */
402 space = tty_prepare_flip_string(tty, &cbuf, ToRecv);
403 if (space < ToRecv) {
404#ifdef ROCKET_DEBUG_RECEIVE
405 printk(KERN_INFO "rp_do_receive:insufficient space ToRecv=%d space=%d\n", ToRecv, space);
406#endif
407 if (space <= 0)
408 return;
409 ToRecv = space;
410 }
411 wRecv = ToRecv >> 1;
412 if (wRecv)
413 sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv);
414 if (ToRecv & 1)
415 cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp));
416 }
417 /* Push the data up to the tty layer */
418 tty_flip_buffer_push(tty);
419}
420
421/*
422 * Serial port transmit data function. Called from the timer polling loop as a
423 * result of a bit set in xmit_flags[], indicating data (from the tty layer) is ready
424 * to be sent out the serial port. Data is buffered in rp_table[line].xmit_buf, it is
425 * moved to the port's xmit FIFO. *info is critical data, protected by spinlocks.
426 */
427static void rp_do_transmit(struct r_port *info)
428{
429 int c;
430 CHANNEL_t *cp = &info->channel;
431 struct tty_struct *tty;
432 unsigned long flags;
433
434#ifdef ROCKET_DEBUG_INTR
435 printk(KERN_DEBUG "%s\n", __func__);
436#endif
437 if (!info)
438 return;
439 tty = tty_port_tty_get(&info->port);
440
441 if (tty == NULL) {
442 printk(KERN_WARNING "rp: WARNING %s called with tty==NULL\n", __func__);
443 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
444 return;
445 }
446
447 spin_lock_irqsave(&info->slock, flags);
448 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
449
450 /* Loop sending data to FIFO until done or FIFO full */
451 while (1) {
452 if (tty->stopped || tty->hw_stopped)
453 break;
454 c = min(info->xmit_fifo_room, info->xmit_cnt);
455 c = min(c, XMIT_BUF_SIZE - info->xmit_tail);
456 if (c <= 0 || info->xmit_fifo_room <= 0)
457 break;
458 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) (info->xmit_buf + info->xmit_tail), c / 2);
459 if (c & 1)
460 sOutB(sGetTxRxDataIO(cp), info->xmit_buf[info->xmit_tail + c - 1]);
461 info->xmit_tail += c;
462 info->xmit_tail &= XMIT_BUF_SIZE - 1;
463 info->xmit_cnt -= c;
464 info->xmit_fifo_room -= c;
465#ifdef ROCKET_DEBUG_INTR
466 printk(KERN_INFO "tx %d chars...\n", c);
467#endif
468 }
469
470 if (info->xmit_cnt == 0)
471 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
472
473 if (info->xmit_cnt < WAKEUP_CHARS) {
474 tty_wakeup(tty);
475#ifdef ROCKETPORT_HAVE_POLL_WAIT
476 wake_up_interruptible(&tty->poll_wait);
477#endif
478 }
479
480 spin_unlock_irqrestore(&info->slock, flags);
481 tty_kref_put(tty);
482
483#ifdef ROCKET_DEBUG_INTR
484 printk(KERN_DEBUG "(%d,%d,%d,%d)...\n", info->xmit_cnt, info->xmit_head,
485 info->xmit_tail, info->xmit_fifo_room);
486#endif
487}
488
489/*
490 * Called when a serial port signals it has read data in it's RX FIFO.
491 * It checks what interrupts are pending and services them, including
492 * receiving serial data.
493 */
494static void rp_handle_port(struct r_port *info)
495{
496 CHANNEL_t *cp;
497 struct tty_struct *tty;
498 unsigned int IntMask, ChanStatus;
499
500 if (!info)
501 return;
502
503 if ((info->port.flags & ASYNC_INITIALIZED) == 0) {
504 printk(KERN_WARNING "rp: WARNING: rp_handle_port called with "
505 "info->flags & NOT_INIT\n");
506 return;
507 }
508 tty = tty_port_tty_get(&info->port);
509 if (!tty) {
510 printk(KERN_WARNING "rp: WARNING: rp_handle_port called with "
511 "tty==NULL\n");
512 return;
513 }
514 cp = &info->channel;
515
516 IntMask = sGetChanIntID(cp) & info->intmask;
517#ifdef ROCKET_DEBUG_INTR
518 printk(KERN_INFO "rp_interrupt %02x...\n", IntMask);
519#endif
520 ChanStatus = sGetChanStatus(cp);
521 if (IntMask & RXF_TRIG) { /* Rx FIFO trigger level */
522 rp_do_receive(info, tty, cp, ChanStatus);
523 }
524 if (IntMask & DELTA_CD) { /* CD change */
525#if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_INTR) || defined(ROCKET_DEBUG_HANGUP))
526 printk(KERN_INFO "ttyR%d CD now %s...\n", info->line,
527 (ChanStatus & CD_ACT) ? "on" : "off");
528#endif
529 if (!(ChanStatus & CD_ACT) && info->cd_status) {
530#ifdef ROCKET_DEBUG_HANGUP
531 printk(KERN_INFO "CD drop, calling hangup.\n");
532#endif
533 tty_hangup(tty);
534 }
535 info->cd_status = (ChanStatus & CD_ACT) ? 1 : 0;
536 wake_up_interruptible(&info->port.open_wait);
537 }
538#ifdef ROCKET_DEBUG_INTR
539 if (IntMask & DELTA_CTS) { /* CTS change */
540 printk(KERN_INFO "CTS change...\n");
541 }
542 if (IntMask & DELTA_DSR) { /* DSR change */
543 printk(KERN_INFO "DSR change...\n");
544 }
545#endif
546 tty_kref_put(tty);
547}
548
549/*
550 * The top level polling routine. Repeats every 1/100 HZ (10ms).
551 */
552static void rp_do_poll(unsigned long dummy)
553{
554 CONTROLLER_t *ctlp;
555 int ctrl, aiop, ch, line;
556 unsigned int xmitmask, i;
557 unsigned int CtlMask;
558 unsigned char AiopMask;
559 Word_t bit;
560
561 /* Walk through all the boards (ctrl's) */
562 for (ctrl = 0; ctrl < max_board; ctrl++) {
563 if (rcktpt_io_addr[ctrl] <= 0)
564 continue;
565
566 /* Get a ptr to the board's control struct */
567 ctlp = sCtlNumToCtlPtr(ctrl);
568
569 /* Get the interrupt status from the board */
570#ifdef CONFIG_PCI
571 if (ctlp->BusType == isPCI)
572 CtlMask = sPCIGetControllerIntStatus(ctlp);
573 else
574#endif
575 CtlMask = sGetControllerIntStatus(ctlp);
576
577 /* Check if any AIOP read bits are set */
578 for (aiop = 0; CtlMask; aiop++) {
579 bit = ctlp->AiopIntrBits[aiop];
580 if (CtlMask & bit) {
581 CtlMask &= ~bit;
582 AiopMask = sGetAiopIntStatus(ctlp, aiop);
583
584 /* Check if any port read bits are set */
585 for (ch = 0; AiopMask; AiopMask >>= 1, ch++) {
586 if (AiopMask & 1) {
587
588 /* Get the line number (/dev/ttyRx number). */
589 /* Read the data from the port. */
590 line = GetLineNumber(ctrl, aiop, ch);
591 rp_handle_port(rp_table[line]);
592 }
593 }
594 }
595 }
596
597 xmitmask = xmit_flags[ctrl];
598
599 /*
600 * xmit_flags contains bit-significant flags, indicating there is data
601 * to xmit on the port. Bit 0 is port 0 on this board, bit 1 is port
602 * 1, ... (32 total possible). The variable i has the aiop and ch
603 * numbers encoded in it (port 0-7 are aiop0, 8-15 are aiop1, etc).
604 */
605 if (xmitmask) {
606 for (i = 0; i < rocketModel[ctrl].numPorts; i++) {
607 if (xmitmask & (1 << i)) {
608 aiop = (i & 0x18) >> 3;
609 ch = i & 0x07;
610 line = GetLineNumber(ctrl, aiop, ch);
611 rp_do_transmit(rp_table[line]);
612 }
613 }
614 }
615 }
616
617 /*
618 * Reset the timer so we get called at the next clock tick (10ms).
619 */
620 if (atomic_read(&rp_num_ports_open))
621 mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
622}
623
624/*
625 * Initializes the r_port structure for a port, as well as enabling the port on
626 * the board.
627 * Inputs: board, aiop, chan numbers
628 */
629static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
630{
631 unsigned rocketMode;
632 struct r_port *info;
633 int line;
634 CONTROLLER_T *ctlp;
635
636 /* Get the next available line number */
637 line = SetLineNumber(board, aiop, chan);
638
639 ctlp = sCtlNumToCtlPtr(board);
640
641 /* Get a r_port struct for the port, fill it in and save it globally, indexed by line number */
642 info = kzalloc(sizeof (struct r_port), GFP_KERNEL);
643 if (!info) {
644 printk(KERN_ERR "Couldn't allocate info struct for line #%d\n",
645 line);
646 return;
647 }
648
649 info->magic = RPORT_MAGIC;
650 info->line = line;
651 info->ctlp = ctlp;
652 info->board = board;
653 info->aiop = aiop;
654 info->chan = chan;
655 tty_port_init(&info->port);
656 info->port.ops = &rocket_port_ops;
657 init_completion(&info->close_wait);
658 info->flags &= ~ROCKET_MODE_MASK;
659 switch (pc104[board][line]) {
660 case 422:
661 info->flags |= ROCKET_MODE_RS422;
662 break;
663 case 485:
664 info->flags |= ROCKET_MODE_RS485;
665 break;
666 case 232:
667 default:
668 info->flags |= ROCKET_MODE_RS232;
669 break;
670 }
671
672 info->intmask = RXF_TRIG | TXFIFO_MT | SRC_INT | DELTA_CD | DELTA_CTS | DELTA_DSR;
673 if (sInitChan(ctlp, &info->channel, aiop, chan) == 0) {
674 printk(KERN_ERR "RocketPort sInitChan(%d, %d, %d) failed!\n",
675 board, aiop, chan);
676 kfree(info);
677 return;
678 }
679
680 rocketMode = info->flags & ROCKET_MODE_MASK;
681
682 if ((info->flags & ROCKET_RTS_TOGGLE) || (rocketMode == ROCKET_MODE_RS485))
683 sEnRTSToggle(&info->channel);
684 else
685 sDisRTSToggle(&info->channel);
686
687 if (ctlp->boardType == ROCKET_TYPE_PC104) {
688 switch (rocketMode) {
689 case ROCKET_MODE_RS485:
690 sSetInterfaceMode(&info->channel, InterfaceModeRS485);
691 break;
692 case ROCKET_MODE_RS422:
693 sSetInterfaceMode(&info->channel, InterfaceModeRS422);
694 break;
695 case ROCKET_MODE_RS232:
696 default:
697 if (info->flags & ROCKET_RTS_TOGGLE)
698 sSetInterfaceMode(&info->channel, InterfaceModeRS232T);
699 else
700 sSetInterfaceMode(&info->channel, InterfaceModeRS232);
701 break;
702 }
703 }
704 spin_lock_init(&info->slock);
705 mutex_init(&info->write_mtx);
706 rp_table[line] = info;
707 tty_register_device(rocket_driver, line, pci_dev ? &pci_dev->dev :
708 NULL);
709}
710
711/*
712 * Configures a rocketport port according to its termio settings. Called from
713 * user mode into the driver (exception handler). *info CD manipulation is spinlock protected.
714 */
715static void configure_r_port(struct tty_struct *tty, struct r_port *info,
716 struct ktermios *old_termios)
717{
718 unsigned cflag;
719 unsigned long flags;
720 unsigned rocketMode;
721 int bits, baud, divisor;
722 CHANNEL_t *cp;
723 struct ktermios *t = tty->termios;
724
725 cp = &info->channel;
726 cflag = t->c_cflag;
727
728 /* Byte size and parity */
729 if ((cflag & CSIZE) == CS8) {
730 sSetData8(cp);
731 bits = 10;
732 } else {
733 sSetData7(cp);
734 bits = 9;
735 }
736 if (cflag & CSTOPB) {
737 sSetStop2(cp);
738 bits++;
739 } else {
740 sSetStop1(cp);
741 }
742
743 if (cflag & PARENB) {
744 sEnParity(cp);
745 bits++;
746 if (cflag & PARODD) {
747 sSetOddParity(cp);
748 } else {
749 sSetEvenParity(cp);
750 }
751 } else {
752 sDisParity(cp);
753 }
754
755 /* baud rate */
756 baud = tty_get_baud_rate(tty);
757 if (!baud)
758 baud = 9600;
759 divisor = ((rp_baud_base[info->board] + (baud >> 1)) / baud) - 1;
760 if ((divisor >= 8192 || divisor < 0) && old_termios) {
761 baud = tty_termios_baud_rate(old_termios);
762 if (!baud)
763 baud = 9600;
764 divisor = (rp_baud_base[info->board] / baud) - 1;
765 }
766 if (divisor >= 8192 || divisor < 0) {
767 baud = 9600;
768 divisor = (rp_baud_base[info->board] / baud) - 1;
769 }
770 info->cps = baud / bits;
771 sSetBaud(cp, divisor);
772
773 /* FIXME: Should really back compute a baud rate from the divisor */
774 tty_encode_baud_rate(tty, baud, baud);
775
776 if (cflag & CRTSCTS) {
777 info->intmask |= DELTA_CTS;
778 sEnCTSFlowCtl(cp);
779 } else {
780 info->intmask &= ~DELTA_CTS;
781 sDisCTSFlowCtl(cp);
782 }
783 if (cflag & CLOCAL) {
784 info->intmask &= ~DELTA_CD;
785 } else {
786 spin_lock_irqsave(&info->slock, flags);
787 if (sGetChanStatus(cp) & CD_ACT)
788 info->cd_status = 1;
789 else
790 info->cd_status = 0;
791 info->intmask |= DELTA_CD;
792 spin_unlock_irqrestore(&info->slock, flags);
793 }
794
795 /*
796 * Handle software flow control in the board
797 */
798#ifdef ROCKET_SOFT_FLOW
799 if (I_IXON(tty)) {
800 sEnTxSoftFlowCtl(cp);
801 if (I_IXANY(tty)) {
802 sEnIXANY(cp);
803 } else {
804 sDisIXANY(cp);
805 }
806 sSetTxXONChar(cp, START_CHAR(tty));
807 sSetTxXOFFChar(cp, STOP_CHAR(tty));
808 } else {
809 sDisTxSoftFlowCtl(cp);
810 sDisIXANY(cp);
811 sClrTxXOFF(cp);
812 }
813#endif
814
815 /*
816 * Set up ignore/read mask words
817 */
818 info->read_status_mask = STMRCVROVRH | 0xFF;
819 if (I_INPCK(tty))
820 info->read_status_mask |= STMFRAMEH | STMPARITYH;
821 if (I_BRKINT(tty) || I_PARMRK(tty))
822 info->read_status_mask |= STMBREAKH;
823
824 /*
825 * Characters to ignore
826 */
827 info->ignore_status_mask = 0;
828 if (I_IGNPAR(tty))
829 info->ignore_status_mask |= STMFRAMEH | STMPARITYH;
830 if (I_IGNBRK(tty)) {
831 info->ignore_status_mask |= STMBREAKH;
832 /*
833 * If we're ignoring parity and break indicators,
834 * ignore overruns too. (For real raw support).
835 */
836 if (I_IGNPAR(tty))
837 info->ignore_status_mask |= STMRCVROVRH;
838 }
839
840 rocketMode = info->flags & ROCKET_MODE_MASK;
841
842 if ((info->flags & ROCKET_RTS_TOGGLE)
843 || (rocketMode == ROCKET_MODE_RS485))
844 sEnRTSToggle(cp);
845 else
846 sDisRTSToggle(cp);
847
848 sSetRTS(&info->channel);
849
850 if (cp->CtlP->boardType == ROCKET_TYPE_PC104) {
851 switch (rocketMode) {
852 case ROCKET_MODE_RS485:
853 sSetInterfaceMode(cp, InterfaceModeRS485);
854 break;
855 case ROCKET_MODE_RS422:
856 sSetInterfaceMode(cp, InterfaceModeRS422);
857 break;
858 case ROCKET_MODE_RS232:
859 default:
860 if (info->flags & ROCKET_RTS_TOGGLE)
861 sSetInterfaceMode(cp, InterfaceModeRS232T);
862 else
863 sSetInterfaceMode(cp, InterfaceModeRS232);
864 break;
865 }
866 }
867}
868
869static int carrier_raised(struct tty_port *port)
870{
871 struct r_port *info = container_of(port, struct r_port, port);
872 return (sGetChanStatusLo(&info->channel) & CD_ACT) ? 1 : 0;
873}
874
875static void dtr_rts(struct tty_port *port, int on)
876{
877 struct r_port *info = container_of(port, struct r_port, port);
878 if (on) {
879 sSetDTR(&info->channel);
880 sSetRTS(&info->channel);
881 } else {
882 sClrDTR(&info->channel);
883 sClrRTS(&info->channel);
884 }
885}
886
887/*
888 * Exception handler that opens a serial port. Creates xmit_buf storage, fills in
889 * port's r_port struct. Initializes the port hardware.
890 */
891static int rp_open(struct tty_struct *tty, struct file *filp)
892{
893 struct r_port *info;
894 struct tty_port *port;
895 int line = 0, retval;
896 CHANNEL_t *cp;
897 unsigned long page;
898
899 line = tty->index;
900 if (line < 0 || line >= MAX_RP_PORTS || ((info = rp_table[line]) == NULL))
901 return -ENXIO;
902 port = &info->port;
903
904 page = __get_free_page(GFP_KERNEL);
905 if (!page)
906 return -ENOMEM;
907
908 if (port->flags & ASYNC_CLOSING) {
909 retval = wait_for_completion_interruptible(&info->close_wait);
910 free_page(page);
911 if (retval)
912 return retval;
913 return ((port->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
914 }
915
916 /*
917 * We must not sleep from here until the port is marked fully in use.
918 */
919 if (info->xmit_buf)
920 free_page(page);
921 else
922 info->xmit_buf = (unsigned char *) page;
923
924 tty->driver_data = info;
925 tty_port_tty_set(port, tty);
926
927 if (port->count++ == 0) {
928 atomic_inc(&rp_num_ports_open);
929
930#ifdef ROCKET_DEBUG_OPEN
931 printk(KERN_INFO "rocket mod++ = %d...\n",
932 atomic_read(&rp_num_ports_open));
933#endif
934 }
935#ifdef ROCKET_DEBUG_OPEN
936 printk(KERN_INFO "rp_open ttyR%d, count=%d\n", info->line, info->port.count);
937#endif
938
939 /*
940 * Info->count is now 1; so it's safe to sleep now.
941 */
942 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
943 cp = &info->channel;
944 sSetRxTrigger(cp, TRIG_1);
945 if (sGetChanStatus(cp) & CD_ACT)
946 info->cd_status = 1;
947 else
948 info->cd_status = 0;
949 sDisRxStatusMode(cp);
950 sFlushRxFIFO(cp);
951 sFlushTxFIFO(cp);
952
953 sEnInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
954 sSetRxTrigger(cp, TRIG_1);
955
956 sGetChanStatus(cp);
957 sDisRxStatusMode(cp);
958 sClrTxXOFF(cp);
959
960 sDisCTSFlowCtl(cp);
961 sDisTxSoftFlowCtl(cp);
962
963 sEnRxFIFO(cp);
964 sEnTransmit(cp);
965
966 set_bit(ASYNCB_INITIALIZED, &info->port.flags);
967
968 /*
969 * Set up the tty->alt_speed kludge
970 */
971 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
972 tty->alt_speed = 57600;
973 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
974 tty->alt_speed = 115200;
975 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
976 tty->alt_speed = 230400;
977 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
978 tty->alt_speed = 460800;
979
980 configure_r_port(tty, info, NULL);
981 if (tty->termios->c_cflag & CBAUD) {
982 sSetDTR(cp);
983 sSetRTS(cp);
984 }
985 }
986 /* Starts (or resets) the maint polling loop */
987 mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
988
989 retval = tty_port_block_til_ready(port, tty, filp);
990 if (retval) {
991#ifdef ROCKET_DEBUG_OPEN
992 printk(KERN_INFO "rp_open returning after block_til_ready with %d\n", retval);
993#endif
994 return retval;
995 }
996 return 0;
997}
998
999/*
1000 * Exception handler that closes a serial port. info->port.count is considered critical.
1001 */
1002static void rp_close(struct tty_struct *tty, struct file *filp)
1003{
1004 struct r_port *info = tty->driver_data;
1005 struct tty_port *port = &info->port;
1006 int timeout;
1007 CHANNEL_t *cp;
1008
1009 if (rocket_paranoia_check(info, "rp_close"))
1010 return;
1011
1012#ifdef ROCKET_DEBUG_OPEN
1013 printk(KERN_INFO "rp_close ttyR%d, count = %d\n", info->line, info->port.count);
1014#endif
1015
1016 if (tty_port_close_start(port, tty, filp) == 0)
1017 return;
1018
1019 mutex_lock(&port->mutex);
1020 cp = &info->channel;
1021 /*
1022 * Before we drop DTR, make sure the UART transmitter
1023 * has completely drained; this is especially
1024 * important if there is a transmit FIFO!
1025 */
1026 timeout = (sGetTxCnt(cp) + 1) * HZ / info->cps;
1027 if (timeout == 0)
1028 timeout = 1;
1029 rp_wait_until_sent(tty, timeout);
1030 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1031
1032 sDisTransmit(cp);
1033 sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1034 sDisCTSFlowCtl(cp);
1035 sDisTxSoftFlowCtl(cp);
1036 sClrTxXOFF(cp);
1037 sFlushRxFIFO(cp);
1038 sFlushTxFIFO(cp);
1039 sClrRTS(cp);
1040 if (C_HUPCL(tty))
1041 sClrDTR(cp);
1042
1043 rp_flush_buffer(tty);
1044
1045 tty_ldisc_flush(tty);
1046
1047 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1048
1049 /* We can't yet use tty_port_close_end as the buffer handling in this
1050 driver is a bit different to the usual */
1051
1052 if (port->blocked_open) {
1053 if (port->close_delay) {
1054 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1055 }
1056 wake_up_interruptible(&port->open_wait);
1057 } else {
1058 if (info->xmit_buf) {
1059 free_page((unsigned long) info->xmit_buf);
1060 info->xmit_buf = NULL;
1061 }
1062 }
1063 spin_lock_irq(&port->lock);
1064 info->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING | ASYNC_NORMAL_ACTIVE);
1065 tty->closing = 0;
1066 spin_unlock_irq(&port->lock);
1067 mutex_unlock(&port->mutex);
1068 tty_port_tty_set(port, NULL);
1069
1070 wake_up_interruptible(&port->close_wait);
1071 complete_all(&info->close_wait);
1072 atomic_dec(&rp_num_ports_open);
1073
1074#ifdef ROCKET_DEBUG_OPEN
1075 printk(KERN_INFO "rocket mod-- = %d...\n",
1076 atomic_read(&rp_num_ports_open));
1077 printk(KERN_INFO "rp_close ttyR%d complete shutdown\n", info->line);
1078#endif
1079
1080}
1081
1082static void rp_set_termios(struct tty_struct *tty,
1083 struct ktermios *old_termios)
1084{
1085 struct r_port *info = tty->driver_data;
1086 CHANNEL_t *cp;
1087 unsigned cflag;
1088
1089 if (rocket_paranoia_check(info, "rp_set_termios"))
1090 return;
1091
1092 cflag = tty->termios->c_cflag;
1093
1094 /*
1095 * This driver doesn't support CS5 or CS6
1096 */
1097 if (((cflag & CSIZE) == CS5) || ((cflag & CSIZE) == CS6))
1098 tty->termios->c_cflag =
1099 ((cflag & ~CSIZE) | (old_termios->c_cflag & CSIZE));
1100 /* Or CMSPAR */
1101 tty->termios->c_cflag &= ~CMSPAR;
1102
1103 configure_r_port(tty, info, old_termios);
1104
1105 cp = &info->channel;
1106
1107 /* Handle transition to B0 status */
1108 if ((old_termios->c_cflag & CBAUD) && !(tty->termios->c_cflag & CBAUD)) {
1109 sClrDTR(cp);
1110 sClrRTS(cp);
1111 }
1112
1113 /* Handle transition away from B0 status */
1114 if (!(old_termios->c_cflag & CBAUD) && (tty->termios->c_cflag & CBAUD)) {
1115 if (!tty->hw_stopped || !(tty->termios->c_cflag & CRTSCTS))
1116 sSetRTS(cp);
1117 sSetDTR(cp);
1118 }
1119
1120 if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
1121 tty->hw_stopped = 0;
1122 rp_start(tty);
1123 }
1124}
1125
1126static int rp_break(struct tty_struct *tty, int break_state)
1127{
1128 struct r_port *info = tty->driver_data;
1129 unsigned long flags;
1130
1131 if (rocket_paranoia_check(info, "rp_break"))
1132 return -EINVAL;
1133
1134 spin_lock_irqsave(&info->slock, flags);
1135 if (break_state == -1)
1136 sSendBreak(&info->channel);
1137 else
1138 sClrBreak(&info->channel);
1139 spin_unlock_irqrestore(&info->slock, flags);
1140 return 0;
1141}
1142
1143/*
1144 * sGetChanRI used to be a macro in rocket_int.h. When the functionality for
1145 * the UPCI boards was added, it was decided to make this a function because
1146 * the macro was getting too complicated. All cases except the first one
1147 * (UPCIRingInd) are taken directly from the original macro.
1148 */
1149static int sGetChanRI(CHANNEL_T * ChP)
1150{
1151 CONTROLLER_t *CtlP = ChP->CtlP;
1152 int ChanNum = ChP->ChanNum;
1153 int RingInd = 0;
1154
1155 if (CtlP->UPCIRingInd)
1156 RingInd = !(sInB(CtlP->UPCIRingInd) & sBitMapSetTbl[ChanNum]);
1157 else if (CtlP->AltChanRingIndicator)
1158 RingInd = sInB((ByteIO_t) (ChP->ChanStat + 8)) & DSR_ACT;
1159 else if (CtlP->boardType == ROCKET_TYPE_PC104)
1160 RingInd = !(sInB(CtlP->AiopIO[3]) & sBitMapSetTbl[ChanNum]);
1161
1162 return RingInd;
1163}
1164
1165/********************************************************************************************/
1166/* Here are the routines used by rp_ioctl. These are all called from exception handlers. */
1167
1168/*
1169 * Returns the state of the serial modem control lines. These next 2 functions
1170 * are the way kernel versions > 2.5 handle modem control lines rather than IOCTLs.
1171 */
1172static int rp_tiocmget(struct tty_struct *tty)
1173{
1174 struct r_port *info = tty->driver_data;
1175 unsigned int control, result, ChanStatus;
1176
1177 ChanStatus = sGetChanStatusLo(&info->channel);
1178 control = info->channel.TxControl[3];
1179 result = ((control & SET_RTS) ? TIOCM_RTS : 0) |
1180 ((control & SET_DTR) ? TIOCM_DTR : 0) |
1181 ((ChanStatus & CD_ACT) ? TIOCM_CAR : 0) |
1182 (sGetChanRI(&info->channel) ? TIOCM_RNG : 0) |
1183 ((ChanStatus & DSR_ACT) ? TIOCM_DSR : 0) |
1184 ((ChanStatus & CTS_ACT) ? TIOCM_CTS : 0);
1185
1186 return result;
1187}
1188
1189/*
1190 * Sets the modem control lines
1191 */
1192static int rp_tiocmset(struct tty_struct *tty,
1193 unsigned int set, unsigned int clear)
1194{
1195 struct r_port *info = tty->driver_data;
1196
1197 if (set & TIOCM_RTS)
1198 info->channel.TxControl[3] |= SET_RTS;
1199 if (set & TIOCM_DTR)
1200 info->channel.TxControl[3] |= SET_DTR;
1201 if (clear & TIOCM_RTS)
1202 info->channel.TxControl[3] &= ~SET_RTS;
1203 if (clear & TIOCM_DTR)
1204 info->channel.TxControl[3] &= ~SET_DTR;
1205
1206 out32(info->channel.IndexAddr, info->channel.TxControl);
1207 return 0;
1208}
1209
1210static int get_config(struct r_port *info, struct rocket_config __user *retinfo)
1211{
1212 struct rocket_config tmp;
1213
1214 if (!retinfo)
1215 return -EFAULT;
1216 memset(&tmp, 0, sizeof (tmp));
1217 mutex_lock(&info->port.mutex);
1218 tmp.line = info->line;
1219 tmp.flags = info->flags;
1220 tmp.close_delay = info->port.close_delay;
1221 tmp.closing_wait = info->port.closing_wait;
1222 tmp.port = rcktpt_io_addr[(info->line >> 5) & 3];
1223 mutex_unlock(&info->port.mutex);
1224
1225 if (copy_to_user(retinfo, &tmp, sizeof (*retinfo)))
1226 return -EFAULT;
1227 return 0;
1228}
1229
1230static int set_config(struct tty_struct *tty, struct r_port *info,
1231 struct rocket_config __user *new_info)
1232{
1233 struct rocket_config new_serial;
1234
1235 if (copy_from_user(&new_serial, new_info, sizeof (new_serial)))
1236 return -EFAULT;
1237
1238 mutex_lock(&info->port.mutex);
1239 if (!capable(CAP_SYS_ADMIN))
1240 {
1241 if ((new_serial.flags & ~ROCKET_USR_MASK) != (info->flags & ~ROCKET_USR_MASK)) {
1242 mutex_unlock(&info->port.mutex);
1243 return -EPERM;
1244 }
1245 info->flags = ((info->flags & ~ROCKET_USR_MASK) | (new_serial.flags & ROCKET_USR_MASK));
1246 configure_r_port(tty, info, NULL);
1247 mutex_unlock(&info->port.mutex);
1248 return 0;
1249 }
1250
1251 info->flags = ((info->flags & ~ROCKET_FLAGS) | (new_serial.flags & ROCKET_FLAGS));
1252 info->port.close_delay = new_serial.close_delay;
1253 info->port.closing_wait = new_serial.closing_wait;
1254
1255 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
1256 tty->alt_speed = 57600;
1257 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
1258 tty->alt_speed = 115200;
1259 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
1260 tty->alt_speed = 230400;
1261 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
1262 tty->alt_speed = 460800;
1263 mutex_unlock(&info->port.mutex);
1264
1265 configure_r_port(tty, info, NULL);
1266 return 0;
1267}
1268
1269/*
1270 * This function fills in a rocket_ports struct with information
1271 * about what boards/ports are in the system. This info is passed
1272 * to user space. See setrocket.c where the info is used to create
1273 * the /dev/ttyRx ports.
1274 */
1275static int get_ports(struct r_port *info, struct rocket_ports __user *retports)
1276{
1277 struct rocket_ports tmp;
1278 int board;
1279
1280 if (!retports)
1281 return -EFAULT;
1282 memset(&tmp, 0, sizeof (tmp));
1283 tmp.tty_major = rocket_driver->major;
1284
1285 for (board = 0; board < 4; board++) {
1286 tmp.rocketModel[board].model = rocketModel[board].model;
1287 strcpy(tmp.rocketModel[board].modelString, rocketModel[board].modelString);
1288 tmp.rocketModel[board].numPorts = rocketModel[board].numPorts;
1289 tmp.rocketModel[board].loadrm2 = rocketModel[board].loadrm2;
1290 tmp.rocketModel[board].startingPortNumber = rocketModel[board].startingPortNumber;
1291 }
1292 if (copy_to_user(retports, &tmp, sizeof (*retports)))
1293 return -EFAULT;
1294 return 0;
1295}
1296
1297static int reset_rm2(struct r_port *info, void __user *arg)
1298{
1299 int reset;
1300
1301 if (!capable(CAP_SYS_ADMIN))
1302 return -EPERM;
1303
1304 if (copy_from_user(&reset, arg, sizeof (int)))
1305 return -EFAULT;
1306 if (reset)
1307 reset = 1;
1308
1309 if (rcktpt_type[info->board] != ROCKET_TYPE_MODEMII &&
1310 rcktpt_type[info->board] != ROCKET_TYPE_MODEMIII)
1311 return -EINVAL;
1312
1313 if (info->ctlp->BusType == isISA)
1314 sModemReset(info->ctlp, info->chan, reset);
1315 else
1316 sPCIModemReset(info->ctlp, info->chan, reset);
1317
1318 return 0;
1319}
1320
1321static int get_version(struct r_port *info, struct rocket_version __user *retvers)
1322{
1323 if (copy_to_user(retvers, &driver_version, sizeof (*retvers)))
1324 return -EFAULT;
1325 return 0;
1326}
1327
1328/* IOCTL call handler into the driver */
1329static int rp_ioctl(struct tty_struct *tty,
1330 unsigned int cmd, unsigned long arg)
1331{
1332 struct r_port *info = tty->driver_data;
1333 void __user *argp = (void __user *)arg;
1334 int ret = 0;
1335
1336 if (cmd != RCKP_GET_PORTS && rocket_paranoia_check(info, "rp_ioctl"))
1337 return -ENXIO;
1338
1339 switch (cmd) {
1340 case RCKP_GET_STRUCT:
1341 if (copy_to_user(argp, info, sizeof (struct r_port)))
1342 ret = -EFAULT;
1343 break;
1344 case RCKP_GET_CONFIG:
1345 ret = get_config(info, argp);
1346 break;
1347 case RCKP_SET_CONFIG:
1348 ret = set_config(tty, info, argp);
1349 break;
1350 case RCKP_GET_PORTS:
1351 ret = get_ports(info, argp);
1352 break;
1353 case RCKP_RESET_RM2:
1354 ret = reset_rm2(info, argp);
1355 break;
1356 case RCKP_GET_VERSION:
1357 ret = get_version(info, argp);
1358 break;
1359 default:
1360 ret = -ENOIOCTLCMD;
1361 }
1362 return ret;
1363}
1364
1365static void rp_send_xchar(struct tty_struct *tty, char ch)
1366{
1367 struct r_port *info = tty->driver_data;
1368 CHANNEL_t *cp;
1369
1370 if (rocket_paranoia_check(info, "rp_send_xchar"))
1371 return;
1372
1373 cp = &info->channel;
1374 if (sGetTxCnt(cp))
1375 sWriteTxPrioByte(cp, ch);
1376 else
1377 sWriteTxByte(sGetTxRxDataIO(cp), ch);
1378}
1379
1380static void rp_throttle(struct tty_struct *tty)
1381{
1382 struct r_port *info = tty->driver_data;
1383
1384#ifdef ROCKET_DEBUG_THROTTLE
1385 printk(KERN_INFO "throttle %s: %d....\n", tty->name,
1386 tty->ldisc.chars_in_buffer(tty));
1387#endif
1388
1389 if (rocket_paranoia_check(info, "rp_throttle"))
1390 return;
1391
1392 if (I_IXOFF(tty))
1393 rp_send_xchar(tty, STOP_CHAR(tty));
1394
1395 sClrRTS(&info->channel);
1396}
1397
1398static void rp_unthrottle(struct tty_struct *tty)
1399{
1400 struct r_port *info = tty->driver_data;
1401#ifdef ROCKET_DEBUG_THROTTLE
1402 printk(KERN_INFO "unthrottle %s: %d....\n", tty->name,
1403 tty->ldisc.chars_in_buffer(tty));
1404#endif
1405
1406 if (rocket_paranoia_check(info, "rp_throttle"))
1407 return;
1408
1409 if (I_IXOFF(tty))
1410 rp_send_xchar(tty, START_CHAR(tty));
1411
1412 sSetRTS(&info->channel);
1413}
1414
1415/*
1416 * ------------------------------------------------------------
1417 * rp_stop() and rp_start()
1418 *
1419 * This routines are called before setting or resetting tty->stopped.
1420 * They enable or disable transmitter interrupts, as necessary.
1421 * ------------------------------------------------------------
1422 */
1423static void rp_stop(struct tty_struct *tty)
1424{
1425 struct r_port *info = tty->driver_data;
1426
1427#ifdef ROCKET_DEBUG_FLOW
1428 printk(KERN_INFO "stop %s: %d %d....\n", tty->name,
1429 info->xmit_cnt, info->xmit_fifo_room);
1430#endif
1431
1432 if (rocket_paranoia_check(info, "rp_stop"))
1433 return;
1434
1435 if (sGetTxCnt(&info->channel))
1436 sDisTransmit(&info->channel);
1437}
1438
1439static void rp_start(struct tty_struct *tty)
1440{
1441 struct r_port *info = tty->driver_data;
1442
1443#ifdef ROCKET_DEBUG_FLOW
1444 printk(KERN_INFO "start %s: %d %d....\n", tty->name,
1445 info->xmit_cnt, info->xmit_fifo_room);
1446#endif
1447
1448 if (rocket_paranoia_check(info, "rp_stop"))
1449 return;
1450
1451 sEnTransmit(&info->channel);
1452 set_bit((info->aiop * 8) + info->chan,
1453 (void *) &xmit_flags[info->board]);
1454}
1455
1456/*
1457 * rp_wait_until_sent() --- wait until the transmitter is empty
1458 */
1459static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
1460{
1461 struct r_port *info = tty->driver_data;
1462 CHANNEL_t *cp;
1463 unsigned long orig_jiffies;
1464 int check_time, exit_time;
1465 int txcnt;
1466
1467 if (rocket_paranoia_check(info, "rp_wait_until_sent"))
1468 return;
1469
1470 cp = &info->channel;
1471
1472 orig_jiffies = jiffies;
1473#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1474 printk(KERN_INFO "In RP_wait_until_sent(%d) (jiff=%lu)...\n", timeout,
1475 jiffies);
1476 printk(KERN_INFO "cps=%d...\n", info->cps);
1477#endif
1478 while (1) {
1479 txcnt = sGetTxCnt(cp);
1480 if (!txcnt) {
1481 if (sGetChanStatusLo(cp) & TXSHRMT)
1482 break;
1483 check_time = (HZ / info->cps) / 5;
1484 } else {
1485 check_time = HZ * txcnt / info->cps;
1486 }
1487 if (timeout) {
1488 exit_time = orig_jiffies + timeout - jiffies;
1489 if (exit_time <= 0)
1490 break;
1491 if (exit_time < check_time)
1492 check_time = exit_time;
1493 }
1494 if (check_time == 0)
1495 check_time = 1;
1496#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1497 printk(KERN_INFO "txcnt = %d (jiff=%lu,check=%d)...\n", txcnt,
1498 jiffies, check_time);
1499#endif
1500 msleep_interruptible(jiffies_to_msecs(check_time));
1501 if (signal_pending(current))
1502 break;
1503 }
1504 __set_current_state(TASK_RUNNING);
1505#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1506 printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies);
1507#endif
1508}
1509
1510/*
1511 * rp_hangup() --- called by tty_hangup() when a hangup is signaled.
1512 */
1513static void rp_hangup(struct tty_struct *tty)
1514{
1515 CHANNEL_t *cp;
1516 struct r_port *info = tty->driver_data;
1517 unsigned long flags;
1518
1519 if (rocket_paranoia_check(info, "rp_hangup"))
1520 return;
1521
1522#if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_HANGUP))
1523 printk(KERN_INFO "rp_hangup of ttyR%d...\n", info->line);
1524#endif
1525 rp_flush_buffer(tty);
1526 spin_lock_irqsave(&info->port.lock, flags);
1527 if (info->port.flags & ASYNC_CLOSING) {
1528 spin_unlock_irqrestore(&info->port.lock, flags);
1529 return;
1530 }
1531 if (info->port.count)
1532 atomic_dec(&rp_num_ports_open);
1533 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1534 spin_unlock_irqrestore(&info->port.lock, flags);
1535
1536 tty_port_hangup(&info->port);
1537
1538 cp = &info->channel;
1539 sDisRxFIFO(cp);
1540 sDisTransmit(cp);
1541 sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1542 sDisCTSFlowCtl(cp);
1543 sDisTxSoftFlowCtl(cp);
1544 sClrTxXOFF(cp);
1545 clear_bit(ASYNCB_INITIALIZED, &info->port.flags);
1546
1547 wake_up_interruptible(&info->port.open_wait);
1548}
1549
1550/*
1551 * Exception handler - write char routine. The RocketPort driver uses a
1552 * double-buffering strategy, with the twist that if the in-memory CPU
1553 * buffer is empty, and there's space in the transmit FIFO, the
1554 * writing routines will write directly to transmit FIFO.
1555 * Write buffer and counters protected by spinlocks
1556 */
1557static int rp_put_char(struct tty_struct *tty, unsigned char ch)
1558{
1559 struct r_port *info = tty->driver_data;
1560 CHANNEL_t *cp;
1561 unsigned long flags;
1562
1563 if (rocket_paranoia_check(info, "rp_put_char"))
1564 return 0;
1565
1566 /*
1567 * Grab the port write mutex, locking out other processes that try to
1568 * write to this port
1569 */
1570 mutex_lock(&info->write_mtx);
1571
1572#ifdef ROCKET_DEBUG_WRITE
1573 printk(KERN_INFO "rp_put_char %c...\n", ch);
1574#endif
1575
1576 spin_lock_irqsave(&info->slock, flags);
1577 cp = &info->channel;
1578
1579 if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room == 0)
1580 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1581
1582 if (tty->stopped || tty->hw_stopped || info->xmit_fifo_room == 0 || info->xmit_cnt != 0) {
1583 info->xmit_buf[info->xmit_head++] = ch;
1584 info->xmit_head &= XMIT_BUF_SIZE - 1;
1585 info->xmit_cnt++;
1586 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1587 } else {
1588 sOutB(sGetTxRxDataIO(cp), ch);
1589 info->xmit_fifo_room--;
1590 }
1591 spin_unlock_irqrestore(&info->slock, flags);
1592 mutex_unlock(&info->write_mtx);
1593 return 1;
1594}
1595
1596/*
1597 * Exception handler - write routine, called when user app writes to the device.
1598 * A per port write mutex is used to protect from another process writing to
1599 * this port at the same time. This other process could be running on the other CPU
1600 * or get control of the CPU if the copy_from_user() blocks due to a page fault (swapped out).
1601 * Spinlocks protect the info xmit members.
1602 */
1603static int rp_write(struct tty_struct *tty,
1604 const unsigned char *buf, int count)
1605{
1606 struct r_port *info = tty->driver_data;
1607 CHANNEL_t *cp;
1608 const unsigned char *b;
1609 int c, retval = 0;
1610 unsigned long flags;
1611
1612 if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
1613 return 0;
1614
1615 if (mutex_lock_interruptible(&info->write_mtx))
1616 return -ERESTARTSYS;
1617
1618#ifdef ROCKET_DEBUG_WRITE
1619 printk(KERN_INFO "rp_write %d chars...\n", count);
1620#endif
1621 cp = &info->channel;
1622
1623 if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room < count)
1624 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1625
1626 /*
1627 * If the write queue for the port is empty, and there is FIFO space, stuff bytes
1628 * into FIFO. Use the write queue for temp storage.
1629 */
1630 if (!tty->stopped && !tty->hw_stopped && info->xmit_cnt == 0 && info->xmit_fifo_room > 0) {
1631 c = min(count, info->xmit_fifo_room);
1632 b = buf;
1633
1634 /* Push data into FIFO, 2 bytes at a time */
1635 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) b, c / 2);
1636
1637 /* If there is a byte remaining, write it */
1638 if (c & 1)
1639 sOutB(sGetTxRxDataIO(cp), b[c - 1]);
1640
1641 retval += c;
1642 buf += c;
1643 count -= c;
1644
1645 spin_lock_irqsave(&info->slock, flags);
1646 info->xmit_fifo_room -= c;
1647 spin_unlock_irqrestore(&info->slock, flags);
1648 }
1649
1650 /* If count is zero, we wrote it all and are done */
1651 if (!count)
1652 goto end;
1653
1654 /* Write remaining data into the port's xmit_buf */
1655 while (1) {
1656 /* Hung up ? */
1657 if (!test_bit(ASYNCB_NORMAL_ACTIVE, &info->port.flags))
1658 goto end;
1659 c = min(count, XMIT_BUF_SIZE - info->xmit_cnt - 1);
1660 c = min(c, XMIT_BUF_SIZE - info->xmit_head);
1661 if (c <= 0)
1662 break;
1663
1664 b = buf;
1665 memcpy(info->xmit_buf + info->xmit_head, b, c);
1666
1667 spin_lock_irqsave(&info->slock, flags);
1668 info->xmit_head =
1669 (info->xmit_head + c) & (XMIT_BUF_SIZE - 1);
1670 info->xmit_cnt += c;
1671 spin_unlock_irqrestore(&info->slock, flags);
1672
1673 buf += c;
1674 count -= c;
1675 retval += c;
1676 }
1677
1678 if ((retval > 0) && !tty->stopped && !tty->hw_stopped)
1679 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1680
1681end:
1682 if (info->xmit_cnt < WAKEUP_CHARS) {
1683 tty_wakeup(tty);
1684#ifdef ROCKETPORT_HAVE_POLL_WAIT
1685 wake_up_interruptible(&tty->poll_wait);
1686#endif
1687 }
1688 mutex_unlock(&info->write_mtx);
1689 return retval;
1690}
1691
1692/*
1693 * Return the number of characters that can be sent. We estimate
1694 * only using the in-memory transmit buffer only, and ignore the
1695 * potential space in the transmit FIFO.
1696 */
1697static int rp_write_room(struct tty_struct *tty)
1698{
1699 struct r_port *info = tty->driver_data;
1700 int ret;
1701
1702 if (rocket_paranoia_check(info, "rp_write_room"))
1703 return 0;
1704
1705 ret = XMIT_BUF_SIZE - info->xmit_cnt - 1;
1706 if (ret < 0)
1707 ret = 0;
1708#ifdef ROCKET_DEBUG_WRITE
1709 printk(KERN_INFO "rp_write_room returns %d...\n", ret);
1710#endif
1711 return ret;
1712}
1713
1714/*
1715 * Return the number of characters in the buffer. Again, this only
1716 * counts those characters in the in-memory transmit buffer.
1717 */
1718static int rp_chars_in_buffer(struct tty_struct *tty)
1719{
1720 struct r_port *info = tty->driver_data;
1721
1722 if (rocket_paranoia_check(info, "rp_chars_in_buffer"))
1723 return 0;
1724
1725#ifdef ROCKET_DEBUG_WRITE
1726 printk(KERN_INFO "rp_chars_in_buffer returns %d...\n", info->xmit_cnt);
1727#endif
1728 return info->xmit_cnt;
1729}
1730
1731/*
1732 * Flushes the TX fifo for a port, deletes data in the xmit_buf stored in the
1733 * r_port struct for the port. Note that spinlock are used to protect info members,
1734 * do not call this function if the spinlock is already held.
1735 */
1736static void rp_flush_buffer(struct tty_struct *tty)
1737{
1738 struct r_port *info = tty->driver_data;
1739 CHANNEL_t *cp;
1740 unsigned long flags;
1741
1742 if (rocket_paranoia_check(info, "rp_flush_buffer"))
1743 return;
1744
1745 spin_lock_irqsave(&info->slock, flags);
1746 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1747 spin_unlock_irqrestore(&info->slock, flags);
1748
1749#ifdef ROCKETPORT_HAVE_POLL_WAIT
1750 wake_up_interruptible(&tty->poll_wait);
1751#endif
1752 tty_wakeup(tty);
1753
1754 cp = &info->channel;
1755 sFlushTxFIFO(cp);
1756}
1757
1758#ifdef CONFIG_PCI
1759
1760static struct pci_device_id __devinitdata __used rocket_pci_ids[] = {
1761 { PCI_DEVICE(PCI_VENDOR_ID_RP, PCI_ANY_ID) },
1762 { }
1763};
1764MODULE_DEVICE_TABLE(pci, rocket_pci_ids);
1765
1766/*
1767 * Called when a PCI card is found. Retrieves and stores model information,
1768 * init's aiopic and serial port hardware.
1769 * Inputs: i is the board number (0-n)
1770 */
1771static __init int register_PCI(int i, struct pci_dev *dev)
1772{
1773 int num_aiops, aiop, max_num_aiops, num_chan, chan;
1774 unsigned int aiopio[MAX_AIOPS_PER_BOARD];
1775 CONTROLLER_t *ctlp;
1776
1777 int fast_clock = 0;
1778 int altChanRingIndicator = 0;
1779 int ports_per_aiop = 8;
1780 WordIO_t ConfigIO = 0;
1781 ByteIO_t UPCIRingInd = 0;
1782
1783 if (!dev || pci_enable_device(dev))
1784 return 0;
1785
1786 rcktpt_io_addr[i] = pci_resource_start(dev, 0);
1787
1788 rcktpt_type[i] = ROCKET_TYPE_NORMAL;
1789 rocketModel[i].loadrm2 = 0;
1790 rocketModel[i].startingPortNumber = nextLineNumber;
1791
1792 /* Depending on the model, set up some config variables */
1793 switch (dev->device) {
1794 case PCI_DEVICE_ID_RP4QUAD:
1795 max_num_aiops = 1;
1796 ports_per_aiop = 4;
1797 rocketModel[i].model = MODEL_RP4QUAD;
1798 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/quad cable");
1799 rocketModel[i].numPorts = 4;
1800 break;
1801 case PCI_DEVICE_ID_RP8OCTA:
1802 max_num_aiops = 1;
1803 rocketModel[i].model = MODEL_RP8OCTA;
1804 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/octa cable");
1805 rocketModel[i].numPorts = 8;
1806 break;
1807 case PCI_DEVICE_ID_URP8OCTA:
1808 max_num_aiops = 1;
1809 rocketModel[i].model = MODEL_UPCI_RP8OCTA;
1810 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/octa cable");
1811 rocketModel[i].numPorts = 8;
1812 break;
1813 case PCI_DEVICE_ID_RP8INTF:
1814 max_num_aiops = 1;
1815 rocketModel[i].model = MODEL_RP8INTF;
1816 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/external I/F");
1817 rocketModel[i].numPorts = 8;
1818 break;
1819 case PCI_DEVICE_ID_URP8INTF:
1820 max_num_aiops = 1;
1821 rocketModel[i].model = MODEL_UPCI_RP8INTF;
1822 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/external I/F");
1823 rocketModel[i].numPorts = 8;
1824 break;
1825 case PCI_DEVICE_ID_RP8J:
1826 max_num_aiops = 1;
1827 rocketModel[i].model = MODEL_RP8J;
1828 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/RJ11 connectors");
1829 rocketModel[i].numPorts = 8;
1830 break;
1831 case PCI_DEVICE_ID_RP4J:
1832 max_num_aiops = 1;
1833 ports_per_aiop = 4;
1834 rocketModel[i].model = MODEL_RP4J;
1835 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/RJ45 connectors");
1836 rocketModel[i].numPorts = 4;
1837 break;
1838 case PCI_DEVICE_ID_RP8SNI:
1839 max_num_aiops = 1;
1840 rocketModel[i].model = MODEL_RP8SNI;
1841 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/ custom DB78");
1842 rocketModel[i].numPorts = 8;
1843 break;
1844 case PCI_DEVICE_ID_RP16SNI:
1845 max_num_aiops = 2;
1846 rocketModel[i].model = MODEL_RP16SNI;
1847 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/ custom DB78");
1848 rocketModel[i].numPorts = 16;
1849 break;
1850 case PCI_DEVICE_ID_RP16INTF:
1851 max_num_aiops = 2;
1852 rocketModel[i].model = MODEL_RP16INTF;
1853 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/external I/F");
1854 rocketModel[i].numPorts = 16;
1855 break;
1856 case PCI_DEVICE_ID_URP16INTF:
1857 max_num_aiops = 2;
1858 rocketModel[i].model = MODEL_UPCI_RP16INTF;
1859 strcpy(rocketModel[i].modelString, "RocketPort UPCI 16 port w/external I/F");
1860 rocketModel[i].numPorts = 16;
1861 break;
1862 case PCI_DEVICE_ID_CRP16INTF:
1863 max_num_aiops = 2;
1864 rocketModel[i].model = MODEL_CPCI_RP16INTF;
1865 strcpy(rocketModel[i].modelString, "RocketPort Compact PCI 16 port w/external I/F");
1866 rocketModel[i].numPorts = 16;
1867 break;
1868 case PCI_DEVICE_ID_RP32INTF:
1869 max_num_aiops = 4;
1870 rocketModel[i].model = MODEL_RP32INTF;
1871 strcpy(rocketModel[i].modelString, "RocketPort 32 port w/external I/F");
1872 rocketModel[i].numPorts = 32;
1873 break;
1874 case PCI_DEVICE_ID_URP32INTF:
1875 max_num_aiops = 4;
1876 rocketModel[i].model = MODEL_UPCI_RP32INTF;
1877 strcpy(rocketModel[i].modelString, "RocketPort UPCI 32 port w/external I/F");
1878 rocketModel[i].numPorts = 32;
1879 break;
1880 case PCI_DEVICE_ID_RPP4:
1881 max_num_aiops = 1;
1882 ports_per_aiop = 4;
1883 altChanRingIndicator++;
1884 fast_clock++;
1885 rocketModel[i].model = MODEL_RPP4;
1886 strcpy(rocketModel[i].modelString, "RocketPort Plus 4 port");
1887 rocketModel[i].numPorts = 4;
1888 break;
1889 case PCI_DEVICE_ID_RPP8:
1890 max_num_aiops = 2;
1891 ports_per_aiop = 4;
1892 altChanRingIndicator++;
1893 fast_clock++;
1894 rocketModel[i].model = MODEL_RPP8;
1895 strcpy(rocketModel[i].modelString, "RocketPort Plus 8 port");
1896 rocketModel[i].numPorts = 8;
1897 break;
1898 case PCI_DEVICE_ID_RP2_232:
1899 max_num_aiops = 1;
1900 ports_per_aiop = 2;
1901 altChanRingIndicator++;
1902 fast_clock++;
1903 rocketModel[i].model = MODEL_RP2_232;
1904 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS232");
1905 rocketModel[i].numPorts = 2;
1906 break;
1907 case PCI_DEVICE_ID_RP2_422:
1908 max_num_aiops = 1;
1909 ports_per_aiop = 2;
1910 altChanRingIndicator++;
1911 fast_clock++;
1912 rocketModel[i].model = MODEL_RP2_422;
1913 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS422");
1914 rocketModel[i].numPorts = 2;
1915 break;
1916 case PCI_DEVICE_ID_RP6M:
1917
1918 max_num_aiops = 1;
1919 ports_per_aiop = 6;
1920
1921 /* If revision is 1, the rocketmodem flash must be loaded.
1922 * If it is 2 it is a "socketed" version. */
1923 if (dev->revision == 1) {
1924 rcktpt_type[i] = ROCKET_TYPE_MODEMII;
1925 rocketModel[i].loadrm2 = 1;
1926 } else {
1927 rcktpt_type[i] = ROCKET_TYPE_MODEM;
1928 }
1929
1930 rocketModel[i].model = MODEL_RP6M;
1931 strcpy(rocketModel[i].modelString, "RocketModem 6 port");
1932 rocketModel[i].numPorts = 6;
1933 break;
1934 case PCI_DEVICE_ID_RP4M:
1935 max_num_aiops = 1;
1936 ports_per_aiop = 4;
1937 if (dev->revision == 1) {
1938 rcktpt_type[i] = ROCKET_TYPE_MODEMII;
1939 rocketModel[i].loadrm2 = 1;
1940 } else {
1941 rcktpt_type[i] = ROCKET_TYPE_MODEM;
1942 }
1943
1944 rocketModel[i].model = MODEL_RP4M;
1945 strcpy(rocketModel[i].modelString, "RocketModem 4 port");
1946 rocketModel[i].numPorts = 4;
1947 break;
1948 default:
1949 max_num_aiops = 0;
1950 break;
1951 }
1952
1953 /*
1954 * Check for UPCI boards.
1955 */
1956
1957 switch (dev->device) {
1958 case PCI_DEVICE_ID_URP32INTF:
1959 case PCI_DEVICE_ID_URP8INTF:
1960 case PCI_DEVICE_ID_URP16INTF:
1961 case PCI_DEVICE_ID_CRP16INTF:
1962 case PCI_DEVICE_ID_URP8OCTA:
1963 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
1964 ConfigIO = pci_resource_start(dev, 1);
1965 if (dev->device == PCI_DEVICE_ID_URP8OCTA) {
1966 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
1967
1968 /*
1969 * Check for octa or quad cable.
1970 */
1971 if (!
1972 (sInW(ConfigIO + _PCI_9030_GPIO_CTRL) &
1973 PCI_GPIO_CTRL_8PORT)) {
1974 ports_per_aiop = 4;
1975 rocketModel[i].numPorts = 4;
1976 }
1977 }
1978 break;
1979 case PCI_DEVICE_ID_UPCI_RM3_8PORT:
1980 max_num_aiops = 1;
1981 rocketModel[i].model = MODEL_UPCI_RM3_8PORT;
1982 strcpy(rocketModel[i].modelString, "RocketModem III 8 port");
1983 rocketModel[i].numPorts = 8;
1984 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
1985 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
1986 ConfigIO = pci_resource_start(dev, 1);
1987 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
1988 break;
1989 case PCI_DEVICE_ID_UPCI_RM3_4PORT:
1990 max_num_aiops = 1;
1991 rocketModel[i].model = MODEL_UPCI_RM3_4PORT;
1992 strcpy(rocketModel[i].modelString, "RocketModem III 4 port");
1993 rocketModel[i].numPorts = 4;
1994 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
1995 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
1996 ConfigIO = pci_resource_start(dev, 1);
1997 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
1998 break;
1999 default:
2000 break;
2001 }
2002
2003 if (fast_clock) {
2004 sClockPrescale = 0x12; /* mod 2 (divide by 3) */
2005 rp_baud_base[i] = 921600;
2006 } else {
2007 /*
2008 * If support_low_speed is set, use the slow clock
2009 * prescale, which supports 50 bps
2010 */
2011 if (support_low_speed) {
2012 /* mod 9 (divide by 10) prescale */
2013 sClockPrescale = 0x19;
2014 rp_baud_base[i] = 230400;
2015 } else {
2016 /* mod 4 (divide by 5) prescale */
2017 sClockPrescale = 0x14;
2018 rp_baud_base[i] = 460800;
2019 }
2020 }
2021
2022 for (aiop = 0; aiop < max_num_aiops; aiop++)
2023 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x40);
2024 ctlp = sCtlNumToCtlPtr(i);
2025 num_aiops = sPCIInitController(ctlp, i, aiopio, max_num_aiops, ConfigIO, 0, FREQ_DIS, 0, altChanRingIndicator, UPCIRingInd);
2026 for (aiop = 0; aiop < max_num_aiops; aiop++)
2027 ctlp->AiopNumChan[aiop] = ports_per_aiop;
2028
2029 dev_info(&dev->dev, "comtrol PCI controller #%d found at "
2030 "address %04lx, %d AIOP(s) (%s), creating ttyR%d - %ld\n",
2031 i, rcktpt_io_addr[i], num_aiops, rocketModel[i].modelString,
2032 rocketModel[i].startingPortNumber,
2033 rocketModel[i].startingPortNumber + rocketModel[i].numPorts-1);
2034
2035 if (num_aiops <= 0) {
2036 rcktpt_io_addr[i] = 0;
2037 return (0);
2038 }
2039 is_PCI[i] = 1;
2040
2041 /* Reset the AIOPIC, init the serial ports */
2042 for (aiop = 0; aiop < num_aiops; aiop++) {
2043 sResetAiopByNum(ctlp, aiop);
2044 num_chan = ports_per_aiop;
2045 for (chan = 0; chan < num_chan; chan++)
2046 init_r_port(i, aiop, chan, dev);
2047 }
2048
2049 /* Rocket modems must be reset */
2050 if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) ||
2051 (rcktpt_type[i] == ROCKET_TYPE_MODEMII) ||
2052 (rcktpt_type[i] == ROCKET_TYPE_MODEMIII)) {
2053 num_chan = ports_per_aiop;
2054 for (chan = 0; chan < num_chan; chan++)
2055 sPCIModemReset(ctlp, chan, 1);
2056 msleep(500);
2057 for (chan = 0; chan < num_chan; chan++)
2058 sPCIModemReset(ctlp, chan, 0);
2059 msleep(500);
2060 rmSpeakerReset(ctlp, rocketModel[i].model);
2061 }
2062 return (1);
2063}
2064
2065/*
2066 * Probes for PCI cards, inits them if found
2067 * Input: board_found = number of ISA boards already found, or the
2068 * starting board number
2069 * Returns: Number of PCI boards found
2070 */
2071static int __init init_PCI(int boards_found)
2072{
2073 struct pci_dev *dev = NULL;
2074 int count = 0;
2075
2076 /* Work through the PCI device list, pulling out ours */
2077 while ((dev = pci_get_device(PCI_VENDOR_ID_RP, PCI_ANY_ID, dev))) {
2078 if (register_PCI(count + boards_found, dev))
2079 count++;
2080 }
2081 return (count);
2082}
2083
2084#endif /* CONFIG_PCI */
2085
2086/*
2087 * Probes for ISA cards
2088 * Input: i = the board number to look for
2089 * Returns: 1 if board found, 0 else
2090 */
2091static int __init init_ISA(int i)
2092{
2093 int num_aiops, num_chan = 0, total_num_chan = 0;
2094 int aiop, chan;
2095 unsigned int aiopio[MAX_AIOPS_PER_BOARD];
2096 CONTROLLER_t *ctlp;
2097 char *type_string;
2098
2099 /* If io_addr is zero, no board configured */
2100 if (rcktpt_io_addr[i] == 0)
2101 return (0);
2102
2103 /* Reserve the IO region */
2104 if (!request_region(rcktpt_io_addr[i], 64, "Comtrol RocketPort")) {
2105 printk(KERN_ERR "Unable to reserve IO region for configured "
2106 "ISA RocketPort at address 0x%lx, board not "
2107 "installed...\n", rcktpt_io_addr[i]);
2108 rcktpt_io_addr[i] = 0;
2109 return (0);
2110 }
2111
2112 ctlp = sCtlNumToCtlPtr(i);
2113
2114 ctlp->boardType = rcktpt_type[i];
2115
2116 switch (rcktpt_type[i]) {
2117 case ROCKET_TYPE_PC104:
2118 type_string = "(PC104)";
2119 break;
2120 case ROCKET_TYPE_MODEM:
2121 type_string = "(RocketModem)";
2122 break;
2123 case ROCKET_TYPE_MODEMII:
2124 type_string = "(RocketModem II)";
2125 break;
2126 default:
2127 type_string = "";
2128 break;
2129 }
2130
2131 /*
2132 * If support_low_speed is set, use the slow clock prescale,
2133 * which supports 50 bps
2134 */
2135 if (support_low_speed) {
2136 sClockPrescale = 0x19; /* mod 9 (divide by 10) prescale */
2137 rp_baud_base[i] = 230400;
2138 } else {
2139 sClockPrescale = 0x14; /* mod 4 (divide by 5) prescale */
2140 rp_baud_base[i] = 460800;
2141 }
2142
2143 for (aiop = 0; aiop < MAX_AIOPS_PER_BOARD; aiop++)
2144 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x400);
2145
2146 num_aiops = sInitController(ctlp, i, controller + (i * 0x400), aiopio, MAX_AIOPS_PER_BOARD, 0, FREQ_DIS, 0);
2147
2148 if (ctlp->boardType == ROCKET_TYPE_PC104) {
2149 sEnAiop(ctlp, 2); /* only one AIOPIC, but these */
2150 sEnAiop(ctlp, 3); /* CSels used for other stuff */
2151 }
2152
2153 /* If something went wrong initing the AIOP's release the ISA IO memory */
2154 if (num_aiops <= 0) {
2155 release_region(rcktpt_io_addr[i], 64);
2156 rcktpt_io_addr[i] = 0;
2157 return (0);
2158 }
2159
2160 rocketModel[i].startingPortNumber = nextLineNumber;
2161
2162 for (aiop = 0; aiop < num_aiops; aiop++) {
2163 sResetAiopByNum(ctlp, aiop);
2164 sEnAiop(ctlp, aiop);
2165 num_chan = sGetAiopNumChan(ctlp, aiop);
2166 total_num_chan += num_chan;
2167 for (chan = 0; chan < num_chan; chan++)
2168 init_r_port(i, aiop, chan, NULL);
2169 }
2170 is_PCI[i] = 0;
2171 if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) || (rcktpt_type[i] == ROCKET_TYPE_MODEMII)) {
2172 num_chan = sGetAiopNumChan(ctlp, 0);
2173 total_num_chan = num_chan;
2174 for (chan = 0; chan < num_chan; chan++)
2175 sModemReset(ctlp, chan, 1);
2176 msleep(500);
2177 for (chan = 0; chan < num_chan; chan++)
2178 sModemReset(ctlp, chan, 0);
2179 msleep(500);
2180 strcpy(rocketModel[i].modelString, "RocketModem ISA");
2181 } else {
2182 strcpy(rocketModel[i].modelString, "RocketPort ISA");
2183 }
2184 rocketModel[i].numPorts = total_num_chan;
2185 rocketModel[i].model = MODEL_ISA;
2186
2187 printk(KERN_INFO "RocketPort ISA card #%d found at 0x%lx - %d AIOPs %s\n",
2188 i, rcktpt_io_addr[i], num_aiops, type_string);
2189
2190 printk(KERN_INFO "Installing %s, creating /dev/ttyR%d - %ld\n",
2191 rocketModel[i].modelString,
2192 rocketModel[i].startingPortNumber,
2193 rocketModel[i].startingPortNumber +
2194 rocketModel[i].numPorts - 1);
2195
2196 return (1);
2197}
2198
2199static const struct tty_operations rocket_ops = {
2200 .open = rp_open,
2201 .close = rp_close,
2202 .write = rp_write,
2203 .put_char = rp_put_char,
2204 .write_room = rp_write_room,
2205 .chars_in_buffer = rp_chars_in_buffer,
2206 .flush_buffer = rp_flush_buffer,
2207 .ioctl = rp_ioctl,
2208 .throttle = rp_throttle,
2209 .unthrottle = rp_unthrottle,
2210 .set_termios = rp_set_termios,
2211 .stop = rp_stop,
2212 .start = rp_start,
2213 .hangup = rp_hangup,
2214 .break_ctl = rp_break,
2215 .send_xchar = rp_send_xchar,
2216 .wait_until_sent = rp_wait_until_sent,
2217 .tiocmget = rp_tiocmget,
2218 .tiocmset = rp_tiocmset,
2219};
2220
2221static const struct tty_port_operations rocket_port_ops = {
2222 .carrier_raised = carrier_raised,
2223 .dtr_rts = dtr_rts,
2224};
2225
2226/*
2227 * The module "startup" routine; it's run when the module is loaded.
2228 */
2229static int __init rp_init(void)
2230{
2231 int ret = -ENOMEM, pci_boards_found, isa_boards_found, i;
2232
2233 printk(KERN_INFO "RocketPort device driver module, version %s, %s\n",
2234 ROCKET_VERSION, ROCKET_DATE);
2235
2236 rocket_driver = alloc_tty_driver(MAX_RP_PORTS);
2237 if (!rocket_driver)
2238 goto err;
2239
2240 /*
2241 * If board 1 is non-zero, there is at least one ISA configured. If controller is
2242 * zero, use the default controller IO address of board1 + 0x40.
2243 */
2244 if (board1) {
2245 if (controller == 0)
2246 controller = board1 + 0x40;
2247 } else {
2248 controller = 0; /* Used as a flag, meaning no ISA boards */
2249 }
2250
2251 /* If an ISA card is configured, reserve the 4 byte IO space for the Mudbac controller */
2252 if (controller && (!request_region(controller, 4, "Comtrol RocketPort"))) {
2253 printk(KERN_ERR "Unable to reserve IO region for first "
2254 "configured ISA RocketPort controller 0x%lx. "
2255 "Driver exiting\n", controller);
2256 ret = -EBUSY;
2257 goto err_tty;
2258 }
2259
2260 /* Store ISA variable retrieved from command line or .conf file. */
2261 rcktpt_io_addr[0] = board1;
2262 rcktpt_io_addr[1] = board2;
2263 rcktpt_io_addr[2] = board3;
2264 rcktpt_io_addr[3] = board4;
2265
2266 rcktpt_type[0] = modem1 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2267 rcktpt_type[0] = pc104_1[0] ? ROCKET_TYPE_PC104 : rcktpt_type[0];
2268 rcktpt_type[1] = modem2 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2269 rcktpt_type[1] = pc104_2[0] ? ROCKET_TYPE_PC104 : rcktpt_type[1];
2270 rcktpt_type[2] = modem3 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2271 rcktpt_type[2] = pc104_3[0] ? ROCKET_TYPE_PC104 : rcktpt_type[2];
2272 rcktpt_type[3] = modem4 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2273 rcktpt_type[3] = pc104_4[0] ? ROCKET_TYPE_PC104 : rcktpt_type[3];
2274
2275 /*
2276 * Set up the tty driver structure and then register this
2277 * driver with the tty layer.
2278 */
2279
2280 rocket_driver->owner = THIS_MODULE;
2281 rocket_driver->flags = TTY_DRIVER_DYNAMIC_DEV;
2282 rocket_driver->name = "ttyR";
2283 rocket_driver->driver_name = "Comtrol RocketPort";
2284 rocket_driver->major = TTY_ROCKET_MAJOR;
2285 rocket_driver->minor_start = 0;
2286 rocket_driver->type = TTY_DRIVER_TYPE_SERIAL;
2287 rocket_driver->subtype = SERIAL_TYPE_NORMAL;
2288 rocket_driver->init_termios = tty_std_termios;
2289 rocket_driver->init_termios.c_cflag =
2290 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2291 rocket_driver->init_termios.c_ispeed = 9600;
2292 rocket_driver->init_termios.c_ospeed = 9600;
2293#ifdef ROCKET_SOFT_FLOW
2294 rocket_driver->flags |= TTY_DRIVER_REAL_RAW;
2295#endif
2296 tty_set_operations(rocket_driver, &rocket_ops);
2297
2298 ret = tty_register_driver(rocket_driver);
2299 if (ret < 0) {
2300 printk(KERN_ERR "Couldn't install tty RocketPort driver\n");
2301 goto err_controller;
2302 }
2303
2304#ifdef ROCKET_DEBUG_OPEN
2305 printk(KERN_INFO "RocketPort driver is major %d\n", rocket_driver.major);
2306#endif
2307
2308 /*
2309 * OK, let's probe each of the controllers looking for boards. Any boards found
2310 * will be initialized here.
2311 */
2312 isa_boards_found = 0;
2313 pci_boards_found = 0;
2314
2315 for (i = 0; i < NUM_BOARDS; i++) {
2316 if (init_ISA(i))
2317 isa_boards_found++;
2318 }
2319
2320#ifdef CONFIG_PCI
2321 if (isa_boards_found < NUM_BOARDS)
2322 pci_boards_found = init_PCI(isa_boards_found);
2323#endif
2324
2325 max_board = pci_boards_found + isa_boards_found;
2326
2327 if (max_board == 0) {
2328 printk(KERN_ERR "No rocketport ports found; unloading driver\n");
2329 ret = -ENXIO;
2330 goto err_ttyu;
2331 }
2332
2333 return 0;
2334err_ttyu:
2335 tty_unregister_driver(rocket_driver);
2336err_controller:
2337 if (controller)
2338 release_region(controller, 4);
2339err_tty:
2340 put_tty_driver(rocket_driver);
2341err:
2342 return ret;
2343}
2344
2345
2346static void rp_cleanup_module(void)
2347{
2348 int retval;
2349 int i;
2350
2351 del_timer_sync(&rocket_timer);
2352
2353 retval = tty_unregister_driver(rocket_driver);
2354 if (retval)
2355 printk(KERN_ERR "Error %d while trying to unregister "
2356 "rocketport driver\n", -retval);
2357
2358 for (i = 0; i < MAX_RP_PORTS; i++)
2359 if (rp_table[i]) {
2360 tty_unregister_device(rocket_driver, i);
2361 kfree(rp_table[i]);
2362 }
2363
2364 put_tty_driver(rocket_driver);
2365
2366 for (i = 0; i < NUM_BOARDS; i++) {
2367 if (rcktpt_io_addr[i] <= 0 || is_PCI[i])
2368 continue;
2369 release_region(rcktpt_io_addr[i], 64);
2370 }
2371 if (controller)
2372 release_region(controller, 4);
2373}
2374
2375/***************************************************************************
2376Function: sInitController
2377Purpose: Initialization of controller global registers and controller
2378 structure.
2379Call: sInitController(CtlP,CtlNum,MudbacIO,AiopIOList,AiopIOListSize,
2380 IRQNum,Frequency,PeriodicOnly)
2381 CONTROLLER_T *CtlP; Ptr to controller structure
2382 int CtlNum; Controller number
2383 ByteIO_t MudbacIO; Mudbac base I/O address.
2384 ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2385 This list must be in the order the AIOPs will be found on the
2386 controller. Once an AIOP in the list is not found, it is
2387 assumed that there are no more AIOPs on the controller.
2388 int AiopIOListSize; Number of addresses in AiopIOList
2389 int IRQNum; Interrupt Request number. Can be any of the following:
2390 0: Disable global interrupts
2391 3: IRQ 3
2392 4: IRQ 4
2393 5: IRQ 5
2394 9: IRQ 9
2395 10: IRQ 10
2396 11: IRQ 11
2397 12: IRQ 12
2398 15: IRQ 15
2399 Byte_t Frequency: A flag identifying the frequency
2400 of the periodic interrupt, can be any one of the following:
2401 FREQ_DIS - periodic interrupt disabled
2402 FREQ_137HZ - 137 Hertz
2403 FREQ_69HZ - 69 Hertz
2404 FREQ_34HZ - 34 Hertz
2405 FREQ_17HZ - 17 Hertz
2406 FREQ_9HZ - 9 Hertz
2407 FREQ_4HZ - 4 Hertz
2408 If IRQNum is set to 0 the Frequency parameter is
2409 overidden, it is forced to a value of FREQ_DIS.
2410 int PeriodicOnly: 1 if all interrupts except the periodic
2411 interrupt are to be blocked.
2412 0 is both the periodic interrupt and
2413 other channel interrupts are allowed.
2414 If IRQNum is set to 0 the PeriodicOnly parameter is
2415 overidden, it is forced to a value of 0.
2416Return: int: Number of AIOPs on the controller, or CTLID_NULL if controller
2417 initialization failed.
2418
2419Comments:
2420 If periodic interrupts are to be disabled but AIOP interrupts
2421 are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2422
2423 If interrupts are to be completely disabled set IRQNum to 0.
2424
2425 Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2426 invalid combination.
2427
2428 This function performs initialization of global interrupt modes,
2429 but it does not actually enable global interrupts. To enable
2430 and disable global interrupts use functions sEnGlobalInt() and
2431 sDisGlobalInt(). Enabling of global interrupts is normally not
2432 done until all other initializations are complete.
2433
2434 Even if interrupts are globally enabled, they must also be
2435 individually enabled for each channel that is to generate
2436 interrupts.
2437
2438Warnings: No range checking on any of the parameters is done.
2439
2440 No context switches are allowed while executing this function.
2441
2442 After this function all AIOPs on the controller are disabled,
2443 they can be enabled with sEnAiop().
2444*/
2445static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
2446 ByteIO_t * AiopIOList, int AiopIOListSize,
2447 int IRQNum, Byte_t Frequency, int PeriodicOnly)
2448{
2449 int i;
2450 ByteIO_t io;
2451 int done;
2452
2453 CtlP->AiopIntrBits = aiop_intr_bits;
2454 CtlP->AltChanRingIndicator = 0;
2455 CtlP->CtlNum = CtlNum;
2456 CtlP->CtlID = CTLID_0001; /* controller release 1 */
2457 CtlP->BusType = isISA;
2458 CtlP->MBaseIO = MudbacIO;
2459 CtlP->MReg1IO = MudbacIO + 1;
2460 CtlP->MReg2IO = MudbacIO + 2;
2461 CtlP->MReg3IO = MudbacIO + 3;
2462#if 1
2463 CtlP->MReg2 = 0; /* interrupt disable */
2464 CtlP->MReg3 = 0; /* no periodic interrupts */
2465#else
2466 if (sIRQMap[IRQNum] == 0) { /* interrupts globally disabled */
2467 CtlP->MReg2 = 0; /* interrupt disable */
2468 CtlP->MReg3 = 0; /* no periodic interrupts */
2469 } else {
2470 CtlP->MReg2 = sIRQMap[IRQNum]; /* set IRQ number */
2471 CtlP->MReg3 = Frequency; /* set frequency */
2472 if (PeriodicOnly) { /* periodic interrupt only */
2473 CtlP->MReg3 |= PERIODIC_ONLY;
2474 }
2475 }
2476#endif
2477 sOutB(CtlP->MReg2IO, CtlP->MReg2);
2478 sOutB(CtlP->MReg3IO, CtlP->MReg3);
2479 sControllerEOI(CtlP); /* clear EOI if warm init */
2480 /* Init AIOPs */
2481 CtlP->NumAiop = 0;
2482 for (i = done = 0; i < AiopIOListSize; i++) {
2483 io = AiopIOList[i];
2484 CtlP->AiopIO[i] = (WordIO_t) io;
2485 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2486 sOutB(CtlP->MReg2IO, CtlP->MReg2 | (i & 0x03)); /* AIOP index */
2487 sOutB(MudbacIO, (Byte_t) (io >> 6)); /* set up AIOP I/O in MUDBAC */
2488 if (done)
2489 continue;
2490 sEnAiop(CtlP, i); /* enable the AIOP */
2491 CtlP->AiopID[i] = sReadAiopID(io); /* read AIOP ID */
2492 if (CtlP->AiopID[i] == AIOPID_NULL) /* if AIOP does not exist */
2493 done = 1; /* done looking for AIOPs */
2494 else {
2495 CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2496 sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE); /* clock prescaler */
2497 sOutB(io + _INDX_DATA, sClockPrescale);
2498 CtlP->NumAiop++; /* bump count of AIOPs */
2499 }
2500 sDisAiop(CtlP, i); /* disable AIOP */
2501 }
2502
2503 if (CtlP->NumAiop == 0)
2504 return (-1);
2505 else
2506 return (CtlP->NumAiop);
2507}
2508
2509/***************************************************************************
2510Function: sPCIInitController
2511Purpose: Initialization of controller global registers and controller
2512 structure.
2513Call: sPCIInitController(CtlP,CtlNum,AiopIOList,AiopIOListSize,
2514 IRQNum,Frequency,PeriodicOnly)
2515 CONTROLLER_T *CtlP; Ptr to controller structure
2516 int CtlNum; Controller number
2517 ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2518 This list must be in the order the AIOPs will be found on the
2519 controller. Once an AIOP in the list is not found, it is
2520 assumed that there are no more AIOPs on the controller.
2521 int AiopIOListSize; Number of addresses in AiopIOList
2522 int IRQNum; Interrupt Request number. Can be any of the following:
2523 0: Disable global interrupts
2524 3: IRQ 3
2525 4: IRQ 4
2526 5: IRQ 5
2527 9: IRQ 9
2528 10: IRQ 10
2529 11: IRQ 11
2530 12: IRQ 12
2531 15: IRQ 15
2532 Byte_t Frequency: A flag identifying the frequency
2533 of the periodic interrupt, can be any one of the following:
2534 FREQ_DIS - periodic interrupt disabled
2535 FREQ_137HZ - 137 Hertz
2536 FREQ_69HZ - 69 Hertz
2537 FREQ_34HZ - 34 Hertz
2538 FREQ_17HZ - 17 Hertz
2539 FREQ_9HZ - 9 Hertz
2540 FREQ_4HZ - 4 Hertz
2541 If IRQNum is set to 0 the Frequency parameter is
2542 overidden, it is forced to a value of FREQ_DIS.
2543 int PeriodicOnly: 1 if all interrupts except the periodic
2544 interrupt are to be blocked.
2545 0 is both the periodic interrupt and
2546 other channel interrupts are allowed.
2547 If IRQNum is set to 0 the PeriodicOnly parameter is
2548 overidden, it is forced to a value of 0.
2549Return: int: Number of AIOPs on the controller, or CTLID_NULL if controller
2550 initialization failed.
2551
2552Comments:
2553 If periodic interrupts are to be disabled but AIOP interrupts
2554 are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2555
2556 If interrupts are to be completely disabled set IRQNum to 0.
2557
2558 Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2559 invalid combination.
2560
2561 This function performs initialization of global interrupt modes,
2562 but it does not actually enable global interrupts. To enable
2563 and disable global interrupts use functions sEnGlobalInt() and
2564 sDisGlobalInt(). Enabling of global interrupts is normally not
2565 done until all other initializations are complete.
2566
2567 Even if interrupts are globally enabled, they must also be
2568 individually enabled for each channel that is to generate
2569 interrupts.
2570
2571Warnings: No range checking on any of the parameters is done.
2572
2573 No context switches are allowed while executing this function.
2574
2575 After this function all AIOPs on the controller are disabled,
2576 they can be enabled with sEnAiop().
2577*/
2578static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
2579 ByteIO_t * AiopIOList, int AiopIOListSize,
2580 WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
2581 int PeriodicOnly, int altChanRingIndicator,
2582 int UPCIRingInd)
2583{
2584 int i;
2585 ByteIO_t io;
2586
2587 CtlP->AltChanRingIndicator = altChanRingIndicator;
2588 CtlP->UPCIRingInd = UPCIRingInd;
2589 CtlP->CtlNum = CtlNum;
2590 CtlP->CtlID = CTLID_0001; /* controller release 1 */
2591 CtlP->BusType = isPCI; /* controller release 1 */
2592
2593 if (ConfigIO) {
2594 CtlP->isUPCI = 1;
2595 CtlP->PCIIO = ConfigIO + _PCI_9030_INT_CTRL;
2596 CtlP->PCIIO2 = ConfigIO + _PCI_9030_GPIO_CTRL;
2597 CtlP->AiopIntrBits = upci_aiop_intr_bits;
2598 } else {
2599 CtlP->isUPCI = 0;
2600 CtlP->PCIIO =
2601 (WordIO_t) ((ByteIO_t) AiopIOList[0] + _PCI_INT_FUNC);
2602 CtlP->AiopIntrBits = aiop_intr_bits;
2603 }
2604
2605 sPCIControllerEOI(CtlP); /* clear EOI if warm init */
2606 /* Init AIOPs */
2607 CtlP->NumAiop = 0;
2608 for (i = 0; i < AiopIOListSize; i++) {
2609 io = AiopIOList[i];
2610 CtlP->AiopIO[i] = (WordIO_t) io;
2611 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2612
2613 CtlP->AiopID[i] = sReadAiopID(io); /* read AIOP ID */
2614 if (CtlP->AiopID[i] == AIOPID_NULL) /* if AIOP does not exist */
2615 break; /* done looking for AIOPs */
2616
2617 CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2618 sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE); /* clock prescaler */
2619 sOutB(io + _INDX_DATA, sClockPrescale);
2620 CtlP->NumAiop++; /* bump count of AIOPs */
2621 }
2622
2623 if (CtlP->NumAiop == 0)
2624 return (-1);
2625 else
2626 return (CtlP->NumAiop);
2627}
2628
2629/***************************************************************************
2630Function: sReadAiopID
2631Purpose: Read the AIOP idenfication number directly from an AIOP.
2632Call: sReadAiopID(io)
2633 ByteIO_t io: AIOP base I/O address
2634Return: int: Flag AIOPID_XXXX if a valid AIOP is found, where X
2635 is replace by an identifying number.
2636 Flag AIOPID_NULL if no valid AIOP is found
2637Warnings: No context switches are allowed while executing this function.
2638
2639*/
2640static int sReadAiopID(ByteIO_t io)
2641{
2642 Byte_t AiopID; /* ID byte from AIOP */
2643
2644 sOutB(io + _CMD_REG, RESET_ALL); /* reset AIOP */
2645 sOutB(io + _CMD_REG, 0x0);
2646 AiopID = sInW(io + _CHN_STAT0) & 0x07;
2647 if (AiopID == 0x06)
2648 return (1);
2649 else /* AIOP does not exist */
2650 return (-1);
2651}
2652
2653/***************************************************************************
2654Function: sReadAiopNumChan
2655Purpose: Read the number of channels available in an AIOP directly from
2656 an AIOP.
2657Call: sReadAiopNumChan(io)
2658 WordIO_t io: AIOP base I/O address
2659Return: int: The number of channels available
2660Comments: The number of channels is determined by write/reads from identical
2661 offsets within the SRAM address spaces for channels 0 and 4.
2662 If the channel 4 space is mirrored to channel 0 it is a 4 channel
2663 AIOP, otherwise it is an 8 channel.
2664Warnings: No context switches are allowed while executing this function.
2665*/
2666static int sReadAiopNumChan(WordIO_t io)
2667{
2668 Word_t x;
2669 static Byte_t R[4] = { 0x00, 0x00, 0x34, 0x12 };
2670
2671 /* write to chan 0 SRAM */
2672 out32((DWordIO_t) io + _INDX_ADDR, R);
2673 sOutW(io + _INDX_ADDR, 0); /* read from SRAM, chan 0 */
2674 x = sInW(io + _INDX_DATA);
2675 sOutW(io + _INDX_ADDR, 0x4000); /* read from SRAM, chan 4 */
2676 if (x != sInW(io + _INDX_DATA)) /* if different must be 8 chan */
2677 return (8);
2678 else
2679 return (4);
2680}
2681
2682/***************************************************************************
2683Function: sInitChan
2684Purpose: Initialization of a channel and channel structure
2685Call: sInitChan(CtlP,ChP,AiopNum,ChanNum)
2686 CONTROLLER_T *CtlP; Ptr to controller structure
2687 CHANNEL_T *ChP; Ptr to channel structure
2688 int AiopNum; AIOP number within controller
2689 int ChanNum; Channel number within AIOP
2690Return: int: 1 if initialization succeeded, 0 if it fails because channel
2691 number exceeds number of channels available in AIOP.
2692Comments: This function must be called before a channel can be used.
2693Warnings: No range checking on any of the parameters is done.
2694
2695 No context switches are allowed while executing this function.
2696*/
2697static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
2698 int ChanNum)
2699{
2700 int i;
2701 WordIO_t AiopIO;
2702 WordIO_t ChIOOff;
2703 Byte_t *ChR;
2704 Word_t ChOff;
2705 static Byte_t R[4];
2706 int brd9600;
2707
2708 if (ChanNum >= CtlP->AiopNumChan[AiopNum])
2709 return 0; /* exceeds num chans in AIOP */
2710
2711 /* Channel, AIOP, and controller identifiers */
2712 ChP->CtlP = CtlP;
2713 ChP->ChanID = CtlP->AiopID[AiopNum];
2714 ChP->AiopNum = AiopNum;
2715 ChP->ChanNum = ChanNum;
2716
2717 /* Global direct addresses */
2718 AiopIO = CtlP->AiopIO[AiopNum];
2719 ChP->Cmd = (ByteIO_t) AiopIO + _CMD_REG;
2720 ChP->IntChan = (ByteIO_t) AiopIO + _INT_CHAN;
2721 ChP->IntMask = (ByteIO_t) AiopIO + _INT_MASK;
2722 ChP->IndexAddr = (DWordIO_t) AiopIO + _INDX_ADDR;
2723 ChP->IndexData = AiopIO + _INDX_DATA;
2724
2725 /* Channel direct addresses */
2726 ChIOOff = AiopIO + ChP->ChanNum * 2;
2727 ChP->TxRxData = ChIOOff + _TD0;
2728 ChP->ChanStat = ChIOOff + _CHN_STAT0;
2729 ChP->TxRxCount = ChIOOff + _FIFO_CNT0;
2730 ChP->IntID = (ByteIO_t) AiopIO + ChP->ChanNum + _INT_ID0;
2731
2732 /* Initialize the channel from the RData array */
2733 for (i = 0; i < RDATASIZE; i += 4) {
2734 R[0] = RData[i];
2735 R[1] = RData[i + 1] + 0x10 * ChanNum;
2736 R[2] = RData[i + 2];
2737 R[3] = RData[i + 3];
2738 out32(ChP->IndexAddr, R);
2739 }
2740
2741 ChR = ChP->R;
2742 for (i = 0; i < RREGDATASIZE; i += 4) {
2743 ChR[i] = RRegData[i];
2744 ChR[i + 1] = RRegData[i + 1] + 0x10 * ChanNum;
2745 ChR[i + 2] = RRegData[i + 2];
2746 ChR[i + 3] = RRegData[i + 3];
2747 }
2748
2749 /* Indexed registers */
2750 ChOff = (Word_t) ChanNum *0x1000;
2751
2752 if (sClockPrescale == 0x14)
2753 brd9600 = 47;
2754 else
2755 brd9600 = 23;
2756
2757 ChP->BaudDiv[0] = (Byte_t) (ChOff + _BAUD);
2758 ChP->BaudDiv[1] = (Byte_t) ((ChOff + _BAUD) >> 8);
2759 ChP->BaudDiv[2] = (Byte_t) brd9600;
2760 ChP->BaudDiv[3] = (Byte_t) (brd9600 >> 8);
2761 out32(ChP->IndexAddr, ChP->BaudDiv);
2762
2763 ChP->TxControl[0] = (Byte_t) (ChOff + _TX_CTRL);
2764 ChP->TxControl[1] = (Byte_t) ((ChOff + _TX_CTRL) >> 8);
2765 ChP->TxControl[2] = 0;
2766 ChP->TxControl[3] = 0;
2767 out32(ChP->IndexAddr, ChP->TxControl);
2768
2769 ChP->RxControl[0] = (Byte_t) (ChOff + _RX_CTRL);
2770 ChP->RxControl[1] = (Byte_t) ((ChOff + _RX_CTRL) >> 8);
2771 ChP->RxControl[2] = 0;
2772 ChP->RxControl[3] = 0;
2773 out32(ChP->IndexAddr, ChP->RxControl);
2774
2775 ChP->TxEnables[0] = (Byte_t) (ChOff + _TX_ENBLS);
2776 ChP->TxEnables[1] = (Byte_t) ((ChOff + _TX_ENBLS) >> 8);
2777 ChP->TxEnables[2] = 0;
2778 ChP->TxEnables[3] = 0;
2779 out32(ChP->IndexAddr, ChP->TxEnables);
2780
2781 ChP->TxCompare[0] = (Byte_t) (ChOff + _TXCMP1);
2782 ChP->TxCompare[1] = (Byte_t) ((ChOff + _TXCMP1) >> 8);
2783 ChP->TxCompare[2] = 0;
2784 ChP->TxCompare[3] = 0;
2785 out32(ChP->IndexAddr, ChP->TxCompare);
2786
2787 ChP->TxReplace1[0] = (Byte_t) (ChOff + _TXREP1B1);
2788 ChP->TxReplace1[1] = (Byte_t) ((ChOff + _TXREP1B1) >> 8);
2789 ChP->TxReplace1[2] = 0;
2790 ChP->TxReplace1[3] = 0;
2791 out32(ChP->IndexAddr, ChP->TxReplace1);
2792
2793 ChP->TxReplace2[0] = (Byte_t) (ChOff + _TXREP2);
2794 ChP->TxReplace2[1] = (Byte_t) ((ChOff + _TXREP2) >> 8);
2795 ChP->TxReplace2[2] = 0;
2796 ChP->TxReplace2[3] = 0;
2797 out32(ChP->IndexAddr, ChP->TxReplace2);
2798
2799 ChP->TxFIFOPtrs = ChOff + _TXF_OUTP;
2800 ChP->TxFIFO = ChOff + _TX_FIFO;
2801
2802 sOutB(ChP->Cmd, (Byte_t) ChanNum | RESTXFCNT); /* apply reset Tx FIFO count */
2803 sOutB(ChP->Cmd, (Byte_t) ChanNum); /* remove reset Tx FIFO count */
2804 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs); /* clear Tx in/out ptrs */
2805 sOutW(ChP->IndexData, 0);
2806 ChP->RxFIFOPtrs = ChOff + _RXF_OUTP;
2807 ChP->RxFIFO = ChOff + _RX_FIFO;
2808
2809 sOutB(ChP->Cmd, (Byte_t) ChanNum | RESRXFCNT); /* apply reset Rx FIFO count */
2810 sOutB(ChP->Cmd, (Byte_t) ChanNum); /* remove reset Rx FIFO count */
2811 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs); /* clear Rx out ptr */
2812 sOutW(ChP->IndexData, 0);
2813 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2); /* clear Rx in ptr */
2814 sOutW(ChP->IndexData, 0);
2815 ChP->TxPrioCnt = ChOff + _TXP_CNT;
2816 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioCnt);
2817 sOutB(ChP->IndexData, 0);
2818 ChP->TxPrioPtr = ChOff + _TXP_PNTR;
2819 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioPtr);
2820 sOutB(ChP->IndexData, 0);
2821 ChP->TxPrioBuf = ChOff + _TXP_BUF;
2822 sEnRxProcessor(ChP); /* start the Rx processor */
2823
2824 return 1;
2825}
2826
2827/***************************************************************************
2828Function: sStopRxProcessor
2829Purpose: Stop the receive processor from processing a channel.
2830Call: sStopRxProcessor(ChP)
2831 CHANNEL_T *ChP; Ptr to channel structure
2832
2833Comments: The receive processor can be started again with sStartRxProcessor().
2834 This function causes the receive processor to skip over the
2835 stopped channel. It does not stop it from processing other channels.
2836
2837Warnings: No context switches are allowed while executing this function.
2838
2839 Do not leave the receive processor stopped for more than one
2840 character time.
2841
2842 After calling this function a delay of 4 uS is required to ensure
2843 that the receive processor is no longer processing this channel.
2844*/
2845static void sStopRxProcessor(CHANNEL_T * ChP)
2846{
2847 Byte_t R[4];
2848
2849 R[0] = ChP->R[0];
2850 R[1] = ChP->R[1];
2851 R[2] = 0x0a;
2852 R[3] = ChP->R[3];
2853 out32(ChP->IndexAddr, R);
2854}
2855
2856/***************************************************************************
2857Function: sFlushRxFIFO
2858Purpose: Flush the Rx FIFO
2859Call: sFlushRxFIFO(ChP)
2860 CHANNEL_T *ChP; Ptr to channel structure
2861Return: void
2862Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
2863 while it is being flushed the receive processor is stopped
2864 and the transmitter is disabled. After these operations a
2865 4 uS delay is done before clearing the pointers to allow
2866 the receive processor to stop. These items are handled inside
2867 this function.
2868Warnings: No context switches are allowed while executing this function.
2869*/
2870static void sFlushRxFIFO(CHANNEL_T * ChP)
2871{
2872 int i;
2873 Byte_t Ch; /* channel number within AIOP */
2874 int RxFIFOEnabled; /* 1 if Rx FIFO enabled */
2875
2876 if (sGetRxCnt(ChP) == 0) /* Rx FIFO empty */
2877 return; /* don't need to flush */
2878
2879 RxFIFOEnabled = 0;
2880 if (ChP->R[0x32] == 0x08) { /* Rx FIFO is enabled */
2881 RxFIFOEnabled = 1;
2882 sDisRxFIFO(ChP); /* disable it */
2883 for (i = 0; i < 2000 / 200; i++) /* delay 2 uS to allow proc to disable FIFO */
2884 sInB(ChP->IntChan); /* depends on bus i/o timing */
2885 }
2886 sGetChanStatus(ChP); /* clear any pending Rx errors in chan stat */
2887 Ch = (Byte_t) sGetChanNum(ChP);
2888 sOutB(ChP->Cmd, Ch | RESRXFCNT); /* apply reset Rx FIFO count */
2889 sOutB(ChP->Cmd, Ch); /* remove reset Rx FIFO count */
2890 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs); /* clear Rx out ptr */
2891 sOutW(ChP->IndexData, 0);
2892 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2); /* clear Rx in ptr */
2893 sOutW(ChP->IndexData, 0);
2894 if (RxFIFOEnabled)
2895 sEnRxFIFO(ChP); /* enable Rx FIFO */
2896}
2897
2898/***************************************************************************
2899Function: sFlushTxFIFO
2900Purpose: Flush the Tx FIFO
2901Call: sFlushTxFIFO(ChP)
2902 CHANNEL_T *ChP; Ptr to channel structure
2903Return: void
2904Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
2905 while it is being flushed the receive processor is stopped
2906 and the transmitter is disabled. After these operations a
2907 4 uS delay is done before clearing the pointers to allow
2908 the receive processor to stop. These items are handled inside
2909 this function.
2910Warnings: No context switches are allowed while executing this function.
2911*/
2912static void sFlushTxFIFO(CHANNEL_T * ChP)
2913{
2914 int i;
2915 Byte_t Ch; /* channel number within AIOP */
2916 int TxEnabled; /* 1 if transmitter enabled */
2917
2918 if (sGetTxCnt(ChP) == 0) /* Tx FIFO empty */
2919 return; /* don't need to flush */
2920
2921 TxEnabled = 0;
2922 if (ChP->TxControl[3] & TX_ENABLE) {
2923 TxEnabled = 1;
2924 sDisTransmit(ChP); /* disable transmitter */
2925 }
2926 sStopRxProcessor(ChP); /* stop Rx processor */
2927 for (i = 0; i < 4000 / 200; i++) /* delay 4 uS to allow proc to stop */
2928 sInB(ChP->IntChan); /* depends on bus i/o timing */
2929 Ch = (Byte_t) sGetChanNum(ChP);
2930 sOutB(ChP->Cmd, Ch | RESTXFCNT); /* apply reset Tx FIFO count */
2931 sOutB(ChP->Cmd, Ch); /* remove reset Tx FIFO count */
2932 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs); /* clear Tx in/out ptrs */
2933 sOutW(ChP->IndexData, 0);
2934 if (TxEnabled)
2935 sEnTransmit(ChP); /* enable transmitter */
2936 sStartRxProcessor(ChP); /* restart Rx processor */
2937}
2938
2939/***************************************************************************
2940Function: sWriteTxPrioByte
2941Purpose: Write a byte of priority transmit data to a channel
2942Call: sWriteTxPrioByte(ChP,Data)
2943 CHANNEL_T *ChP; Ptr to channel structure
2944 Byte_t Data; The transmit data byte
2945
2946Return: int: 1 if the bytes is successfully written, otherwise 0.
2947
2948Comments: The priority byte is transmitted before any data in the Tx FIFO.
2949
2950Warnings: No context switches are allowed while executing this function.
2951*/
2952static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data)
2953{
2954 Byte_t DWBuf[4]; /* buffer for double word writes */
2955 Word_t *WordPtr; /* must be far because Win SS != DS */
2956 register DWordIO_t IndexAddr;
2957
2958 if (sGetTxCnt(ChP) > 1) { /* write it to Tx priority buffer */
2959 IndexAddr = ChP->IndexAddr;
2960 sOutW((WordIO_t) IndexAddr, ChP->TxPrioCnt); /* get priority buffer status */
2961 if (sInB((ByteIO_t) ChP->IndexData) & PRI_PEND) /* priority buffer busy */
2962 return (0); /* nothing sent */
2963
2964 WordPtr = (Word_t *) (&DWBuf[0]);
2965 *WordPtr = ChP->TxPrioBuf; /* data byte address */
2966
2967 DWBuf[2] = Data; /* data byte value */
2968 out32(IndexAddr, DWBuf); /* write it out */
2969
2970 *WordPtr = ChP->TxPrioCnt; /* Tx priority count address */
2971
2972 DWBuf[2] = PRI_PEND + 1; /* indicate 1 byte pending */
2973 DWBuf[3] = 0; /* priority buffer pointer */
2974 out32(IndexAddr, DWBuf); /* write it out */
2975 } else { /* write it to Tx FIFO */
2976
2977 sWriteTxByte(sGetTxRxDataIO(ChP), Data);
2978 }
2979 return (1); /* 1 byte sent */
2980}
2981
2982/***************************************************************************
2983Function: sEnInterrupts
2984Purpose: Enable one or more interrupts for a channel
2985Call: sEnInterrupts(ChP,Flags)
2986 CHANNEL_T *ChP; Ptr to channel structure
2987 Word_t Flags: Interrupt enable flags, can be any combination
2988 of the following flags:
2989 TXINT_EN: Interrupt on Tx FIFO empty
2990 RXINT_EN: Interrupt on Rx FIFO at trigger level (see
2991 sSetRxTrigger())
2992 SRCINT_EN: Interrupt on SRC (Special Rx Condition)
2993 MCINT_EN: Interrupt on modem input change
2994 CHANINT_EN: Allow channel interrupt signal to the AIOP's
2995 Interrupt Channel Register.
2996Return: void
2997Comments: If an interrupt enable flag is set in Flags, that interrupt will be
2998 enabled. If an interrupt enable flag is not set in Flags, that
2999 interrupt will not be changed. Interrupts can be disabled with
3000 function sDisInterrupts().
3001
3002 This function sets the appropriate bit for the channel in the AIOP's
3003 Interrupt Mask Register if the CHANINT_EN flag is set. This allows
3004 this channel's bit to be set in the AIOP's Interrupt Channel Register.
3005
3006 Interrupts must also be globally enabled before channel interrupts
3007 will be passed on to the host. This is done with function
3008 sEnGlobalInt().
3009
3010 In some cases it may be desirable to disable interrupts globally but
3011 enable channel interrupts. This would allow the global interrupt
3012 status register to be used to determine which AIOPs need service.
3013*/
3014static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags)
3015{
3016 Byte_t Mask; /* Interrupt Mask Register */
3017
3018 ChP->RxControl[2] |=
3019 ((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3020
3021 out32(ChP->IndexAddr, ChP->RxControl);
3022
3023 ChP->TxControl[2] |= ((Byte_t) Flags & TXINT_EN);
3024
3025 out32(ChP->IndexAddr, ChP->TxControl);
3026
3027 if (Flags & CHANINT_EN) {
3028 Mask = sInB(ChP->IntMask) | sBitMapSetTbl[ChP->ChanNum];
3029 sOutB(ChP->IntMask, Mask);
3030 }
3031}
3032
3033/***************************************************************************
3034Function: sDisInterrupts
3035Purpose: Disable one or more interrupts for a channel
3036Call: sDisInterrupts(ChP,Flags)
3037 CHANNEL_T *ChP; Ptr to channel structure
3038 Word_t Flags: Interrupt flags, can be any combination
3039 of the following flags:
3040 TXINT_EN: Interrupt on Tx FIFO empty
3041 RXINT_EN: Interrupt on Rx FIFO at trigger level (see
3042 sSetRxTrigger())
3043 SRCINT_EN: Interrupt on SRC (Special Rx Condition)
3044 MCINT_EN: Interrupt on modem input change
3045 CHANINT_EN: Disable channel interrupt signal to the
3046 AIOP's Interrupt Channel Register.
3047Return: void
3048Comments: If an interrupt flag is set in Flags, that interrupt will be
3049 disabled. If an interrupt flag is not set in Flags, that
3050 interrupt will not be changed. Interrupts can be enabled with
3051 function sEnInterrupts().
3052
3053 This function clears the appropriate bit for the channel in the AIOP's
3054 Interrupt Mask Register if the CHANINT_EN flag is set. This blocks
3055 this channel's bit from being set in the AIOP's Interrupt Channel
3056 Register.
3057*/
3058static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags)
3059{
3060 Byte_t Mask; /* Interrupt Mask Register */
3061
3062 ChP->RxControl[2] &=
3063 ~((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3064 out32(ChP->IndexAddr, ChP->RxControl);
3065 ChP->TxControl[2] &= ~((Byte_t) Flags & TXINT_EN);
3066 out32(ChP->IndexAddr, ChP->TxControl);
3067
3068 if (Flags & CHANINT_EN) {
3069 Mask = sInB(ChP->IntMask) & sBitMapClrTbl[ChP->ChanNum];
3070 sOutB(ChP->IntMask, Mask);
3071 }
3072}
3073
3074static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode)
3075{
3076 sOutB(ChP->CtlP->AiopIO[2], (mode & 0x18) | ChP->ChanNum);
3077}
3078
3079/*
3080 * Not an official SSCI function, but how to reset RocketModems.
3081 * ISA bus version
3082 */
3083static void sModemReset(CONTROLLER_T * CtlP, int chan, int on)
3084{
3085 ByteIO_t addr;
3086 Byte_t val;
3087
3088 addr = CtlP->AiopIO[0] + 0x400;
3089 val = sInB(CtlP->MReg3IO);
3090 /* if AIOP[1] is not enabled, enable it */
3091 if ((val & 2) == 0) {
3092 val = sInB(CtlP->MReg2IO);
3093 sOutB(CtlP->MReg2IO, (val & 0xfc) | (1 & 0x03));
3094 sOutB(CtlP->MBaseIO, (unsigned char) (addr >> 6));
3095 }
3096
3097 sEnAiop(CtlP, 1);
3098 if (!on)
3099 addr += 8;
3100 sOutB(addr + chan, 0); /* apply or remove reset */
3101 sDisAiop(CtlP, 1);
3102}
3103
3104/*
3105 * Not an official SSCI function, but how to reset RocketModems.
3106 * PCI bus version
3107 */
3108static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on)
3109{
3110 ByteIO_t addr;
3111
3112 addr = CtlP->AiopIO[0] + 0x40; /* 2nd AIOP */
3113 if (!on)
3114 addr += 8;
3115 sOutB(addr + chan, 0); /* apply or remove reset */
3116}
3117
3118/* Resets the speaker controller on RocketModem II and III devices */
3119static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model)
3120{
3121 ByteIO_t addr;
3122
3123 /* RocketModem II speaker control is at the 8th port location of offset 0x40 */
3124 if ((model == MODEL_RP4M) || (model == MODEL_RP6M)) {
3125 addr = CtlP->AiopIO[0] + 0x4F;
3126 sOutB(addr, 0);
3127 }
3128
3129 /* RocketModem III speaker control is at the 1st port location of offset 0x80 */
3130 if ((model == MODEL_UPCI_RM3_8PORT)
3131 || (model == MODEL_UPCI_RM3_4PORT)) {
3132 addr = CtlP->AiopIO[0] + 0x88;
3133 sOutB(addr, 0);
3134 }
3135}
3136
3137/* Returns the line number given the controller (board), aiop and channel number */
3138static unsigned char GetLineNumber(int ctrl, int aiop, int ch)
3139{
3140 return lineNumbers[(ctrl << 5) | (aiop << 3) | ch];
3141}
3142
3143/*
3144 * Stores the line number associated with a given controller (board), aiop
3145 * and channel number.
3146 * Returns: The line number assigned
3147 */
3148static unsigned char SetLineNumber(int ctrl, int aiop, int ch)
3149{
3150 lineNumbers[(ctrl << 5) | (aiop << 3) | ch] = nextLineNumber++;
3151 return (nextLineNumber - 1);
3152}
diff --git a/drivers/tty/rocket.h b/drivers/tty/rocket.h
new file mode 100644
index 000000000000..ec863f35f1a9
--- /dev/null
+++ b/drivers/tty/rocket.h
@@ -0,0 +1,111 @@
1/*
2 * rocket.h --- the exported interface of the rocket driver to its configuration program.
3 *
4 * Written by Theodore Ts'o, Copyright 1997.
5 * Copyright 1997 Comtrol Corporation.
6 *
7 */
8
9/* Model Information Struct */
10typedef struct {
11 unsigned long model;
12 char modelString[80];
13 unsigned long numPorts;
14 int loadrm2;
15 int startingPortNumber;
16} rocketModel_t;
17
18struct rocket_config {
19 int line;
20 int flags;
21 int closing_wait;
22 int close_delay;
23 int port;
24 int reserved[32];
25};
26
27struct rocket_ports {
28 int tty_major;
29 int callout_major;
30 rocketModel_t rocketModel[8];
31};
32
33struct rocket_version {
34 char rocket_version[32];
35 char rocket_date[32];
36 char reserved[64];
37};
38
39/*
40 * Rocketport flags
41 */
42/*#define ROCKET_CALLOUT_NOHUP 0x00000001 */
43#define ROCKET_FORCE_CD 0x00000002
44#define ROCKET_HUP_NOTIFY 0x00000004
45#define ROCKET_SPLIT_TERMIOS 0x00000008
46#define ROCKET_SPD_MASK 0x00000070
47#define ROCKET_SPD_HI 0x00000010 /* Use 56000 instead of 38400 bps */
48#define ROCKET_SPD_VHI 0x00000020 /* Use 115200 instead of 38400 bps */
49#define ROCKET_SPD_SHI 0x00000030 /* Use 230400 instead of 38400 bps */
50#define ROCKET_SPD_WARP 0x00000040 /* Use 460800 instead of 38400 bps */
51#define ROCKET_SAK 0x00000080
52#define ROCKET_SESSION_LOCKOUT 0x00000100
53#define ROCKET_PGRP_LOCKOUT 0x00000200
54#define ROCKET_RTS_TOGGLE 0x00000400
55#define ROCKET_MODE_MASK 0x00003000
56#define ROCKET_MODE_RS232 0x00000000
57#define ROCKET_MODE_RS485 0x00001000
58#define ROCKET_MODE_RS422 0x00002000
59#define ROCKET_FLAGS 0x00003FFF
60
61#define ROCKET_USR_MASK 0x0071 /* Legal flags that non-privileged
62 * users can set or reset */
63
64/*
65 * For closing_wait and closing_wait2
66 */
67#define ROCKET_CLOSING_WAIT_NONE ASYNC_CLOSING_WAIT_NONE
68#define ROCKET_CLOSING_WAIT_INF ASYNC_CLOSING_WAIT_INF
69
70/*
71 * Rocketport ioctls -- "RP"
72 */
73#define RCKP_GET_STRUCT 0x00525001
74#define RCKP_GET_CONFIG 0x00525002
75#define RCKP_SET_CONFIG 0x00525003
76#define RCKP_GET_PORTS 0x00525004
77#define RCKP_RESET_RM2 0x00525005
78#define RCKP_GET_VERSION 0x00525006
79
80/* Rocketport Models */
81#define MODEL_RP32INTF 0x0001 /* RP 32 port w/external I/F */
82#define MODEL_RP8INTF 0x0002 /* RP 8 port w/external I/F */
83#define MODEL_RP16INTF 0x0003 /* RP 16 port w/external I/F */
84#define MODEL_RP8OCTA 0x0005 /* RP 8 port w/octa cable */
85#define MODEL_RP4QUAD 0x0004 /* RP 4 port w/quad cable */
86#define MODEL_RP8J 0x0006 /* RP 8 port w/RJ11 connectors */
87#define MODEL_RP4J 0x0007 /* RP 4 port w/RJ45 connectors */
88#define MODEL_RP8SNI 0x0008 /* RP 8 port w/ DB78 SNI connector */
89#define MODEL_RP16SNI 0x0009 /* RP 16 port w/ DB78 SNI connector */
90#define MODEL_RPP4 0x000A /* RP Plus 4 port */
91#define MODEL_RPP8 0x000B /* RP Plus 8 port */
92#define MODEL_RP2_232 0x000E /* RP Plus 2 port RS232 */
93#define MODEL_RP2_422 0x000F /* RP Plus 2 port RS232 */
94
95/* Rocketmodem II Models */
96#define MODEL_RP6M 0x000C /* RM 6 port */
97#define MODEL_RP4M 0x000D /* RM 4 port */
98
99/* Universal PCI boards */
100#define MODEL_UPCI_RP32INTF 0x0801 /* RP UPCI 32 port w/external I/F */
101#define MODEL_UPCI_RP8INTF 0x0802 /* RP UPCI 8 port w/external I/F */
102#define MODEL_UPCI_RP16INTF 0x0803 /* RP UPCI 16 port w/external I/F */
103#define MODEL_UPCI_RP8OCTA 0x0805 /* RP UPCI 8 port w/octa cable */
104#define MODEL_UPCI_RM3_8PORT 0x080C /* RP UPCI Rocketmodem III 8 port */
105#define MODEL_UPCI_RM3_4PORT 0x080C /* RP UPCI Rocketmodem III 4 port */
106
107/* Compact PCI 16 port */
108#define MODEL_CPCI_RP16INTF 0x0903 /* RP Compact PCI 16 port w/external I/F */
109
110/* All ISA boards */
111#define MODEL_ISA 0x1000
diff --git a/drivers/tty/rocket_int.h b/drivers/tty/rocket_int.h
new file mode 100644
index 000000000000..67e0f1e778a2
--- /dev/null
+++ b/drivers/tty/rocket_int.h
@@ -0,0 +1,1214 @@
1/*
2 * rocket_int.h --- internal header file for rocket.c
3 *
4 * Written by Theodore Ts'o, Copyright 1997.
5 * Copyright 1997 Comtrol Corporation.
6 *
7 */
8
9/*
10 * Definition of the types in rcktpt_type
11 */
12#define ROCKET_TYPE_NORMAL 0
13#define ROCKET_TYPE_MODEM 1
14#define ROCKET_TYPE_MODEMII 2
15#define ROCKET_TYPE_MODEMIII 3
16#define ROCKET_TYPE_PC104 4
17
18#include <linux/mutex.h>
19
20#include <asm/io.h>
21#include <asm/byteorder.h>
22
23typedef unsigned char Byte_t;
24typedef unsigned int ByteIO_t;
25
26typedef unsigned int Word_t;
27typedef unsigned int WordIO_t;
28
29typedef unsigned int DWordIO_t;
30
31/*
32 * Note! Normally the Linux I/O macros already take care of
33 * byte-swapping the I/O instructions. However, all accesses using
34 * sOutDW aren't really 32-bit accesses, but should be handled in byte
35 * order. Hence the use of the cpu_to_le32() macro to byte-swap
36 * things to no-op the byte swapping done by the big-endian outl()
37 * instruction.
38 */
39
40static inline void sOutB(unsigned short port, unsigned char value)
41{
42#ifdef ROCKET_DEBUG_IO
43 printk(KERN_DEBUG "sOutB(%x, %x)...\n", port, value);
44#endif
45 outb_p(value, port);
46}
47
48static inline void sOutW(unsigned short port, unsigned short value)
49{
50#ifdef ROCKET_DEBUG_IO
51 printk(KERN_DEBUG "sOutW(%x, %x)...\n", port, value);
52#endif
53 outw_p(value, port);
54}
55
56static inline void out32(unsigned short port, Byte_t *p)
57{
58 u32 value = get_unaligned_le32(p);
59#ifdef ROCKET_DEBUG_IO
60 printk(KERN_DEBUG "out32(%x, %lx)...\n", port, value);
61#endif
62 outl_p(value, port);
63}
64
65static inline unsigned char sInB(unsigned short port)
66{
67 return inb_p(port);
68}
69
70static inline unsigned short sInW(unsigned short port)
71{
72 return inw_p(port);
73}
74
75/* This is used to move arrays of bytes so byte swapping isn't appropriate. */
76#define sOutStrW(port, addr, count) if (count) outsw(port, addr, count)
77#define sInStrW(port, addr, count) if (count) insw(port, addr, count)
78
79#define CTL_SIZE 8
80#define AIOP_CTL_SIZE 4
81#define CHAN_AIOP_SIZE 8
82#define MAX_PORTS_PER_AIOP 8
83#define MAX_AIOPS_PER_BOARD 4
84#define MAX_PORTS_PER_BOARD 32
85
86/* Bus type ID */
87#define isISA 0
88#define isPCI 1
89#define isMC 2
90
91/* Controller ID numbers */
92#define CTLID_NULL -1 /* no controller exists */
93#define CTLID_0001 0x0001 /* controller release 1 */
94
95/* AIOP ID numbers, identifies AIOP type implementing channel */
96#define AIOPID_NULL -1 /* no AIOP or channel exists */
97#define AIOPID_0001 0x0001 /* AIOP release 1 */
98
99/************************************************************************
100 Global Register Offsets - Direct Access - Fixed values
101************************************************************************/
102
103#define _CMD_REG 0x38 /* Command Register 8 Write */
104#define _INT_CHAN 0x39 /* Interrupt Channel Register 8 Read */
105#define _INT_MASK 0x3A /* Interrupt Mask Register 8 Read / Write */
106#define _UNUSED 0x3B /* Unused 8 */
107#define _INDX_ADDR 0x3C /* Index Register Address 16 Write */
108#define _INDX_DATA 0x3E /* Index Register Data 8/16 Read / Write */
109
110/************************************************************************
111 Channel Register Offsets for 1st channel in AIOP - Direct Access
112************************************************************************/
113#define _TD0 0x00 /* Transmit Data 16 Write */
114#define _RD0 0x00 /* Receive Data 16 Read */
115#define _CHN_STAT0 0x20 /* Channel Status 8/16 Read / Write */
116#define _FIFO_CNT0 0x10 /* Transmit/Receive FIFO Count 16 Read */
117#define _INT_ID0 0x30 /* Interrupt Identification 8 Read */
118
119/************************************************************************
120 Tx Control Register Offsets - Indexed - External - Fixed
121************************************************************************/
122#define _TX_ENBLS 0x980 /* Tx Processor Enables Register 8 Read / Write */
123#define _TXCMP1 0x988 /* Transmit Compare Value #1 8 Read / Write */
124#define _TXCMP2 0x989 /* Transmit Compare Value #2 8 Read / Write */
125#define _TXREP1B1 0x98A /* Tx Replace Value #1 - Byte 1 8 Read / Write */
126#define _TXREP1B2 0x98B /* Tx Replace Value #1 - Byte 2 8 Read / Write */
127#define _TXREP2 0x98C /* Transmit Replace Value #2 8 Read / Write */
128
129/************************************************************************
130Memory Controller Register Offsets - Indexed - External - Fixed
131************************************************************************/
132#define _RX_FIFO 0x000 /* Rx FIFO */
133#define _TX_FIFO 0x800 /* Tx FIFO */
134#define _RXF_OUTP 0x990 /* Rx FIFO OUT pointer 16 Read / Write */
135#define _RXF_INP 0x992 /* Rx FIFO IN pointer 16 Read / Write */
136#define _TXF_OUTP 0x994 /* Tx FIFO OUT pointer 8 Read / Write */
137#define _TXF_INP 0x995 /* Tx FIFO IN pointer 8 Read / Write */
138#define _TXP_CNT 0x996 /* Tx Priority Count 8 Read / Write */
139#define _TXP_PNTR 0x997 /* Tx Priority Pointer 8 Read / Write */
140
141#define PRI_PEND 0x80 /* Priority data pending (bit7, Tx pri cnt) */
142#define TXFIFO_SIZE 255 /* size of Tx FIFO */
143#define RXFIFO_SIZE 1023 /* size of Rx FIFO */
144
145/************************************************************************
146Tx Priority Buffer - Indexed - External - Fixed
147************************************************************************/
148#define _TXP_BUF 0x9C0 /* Tx Priority Buffer 32 Bytes Read / Write */
149#define TXP_SIZE 0x20 /* 32 bytes */
150
151/************************************************************************
152Channel Register Offsets - Indexed - Internal - Fixed
153************************************************************************/
154
155#define _TX_CTRL 0xFF0 /* Transmit Control 16 Write */
156#define _RX_CTRL 0xFF2 /* Receive Control 8 Write */
157#define _BAUD 0xFF4 /* Baud Rate 16 Write */
158#define _CLK_PRE 0xFF6 /* Clock Prescaler 8 Write */
159
160#define STMBREAK 0x08 /* BREAK */
161#define STMFRAME 0x04 /* framing error */
162#define STMRCVROVR 0x02 /* receiver over run error */
163#define STMPARITY 0x01 /* parity error */
164#define STMERROR (STMBREAK | STMFRAME | STMPARITY)
165#define STMBREAKH 0x800 /* BREAK */
166#define STMFRAMEH 0x400 /* framing error */
167#define STMRCVROVRH 0x200 /* receiver over run error */
168#define STMPARITYH 0x100 /* parity error */
169#define STMERRORH (STMBREAKH | STMFRAMEH | STMPARITYH)
170
171#define CTS_ACT 0x20 /* CTS input asserted */
172#define DSR_ACT 0x10 /* DSR input asserted */
173#define CD_ACT 0x08 /* CD input asserted */
174#define TXFIFOMT 0x04 /* Tx FIFO is empty */
175#define TXSHRMT 0x02 /* Tx shift register is empty */
176#define RDA 0x01 /* Rx data available */
177#define DRAINED (TXFIFOMT | TXSHRMT) /* indicates Tx is drained */
178
179#define STATMODE 0x8000 /* status mode enable bit */
180#define RXFOVERFL 0x2000 /* receive FIFO overflow */
181#define RX2MATCH 0x1000 /* receive compare byte 2 match */
182#define RX1MATCH 0x0800 /* receive compare byte 1 match */
183#define RXBREAK 0x0400 /* received BREAK */
184#define RXFRAME 0x0200 /* received framing error */
185#define RXPARITY 0x0100 /* received parity error */
186#define STATERROR (RXBREAK | RXFRAME | RXPARITY)
187
188#define CTSFC_EN 0x80 /* CTS flow control enable bit */
189#define RTSTOG_EN 0x40 /* RTS toggle enable bit */
190#define TXINT_EN 0x10 /* transmit interrupt enable */
191#define STOP2 0x08 /* enable 2 stop bits (0 = 1 stop) */
192#define PARITY_EN 0x04 /* enable parity (0 = no parity) */
193#define EVEN_PAR 0x02 /* even parity (0 = odd parity) */
194#define DATA8BIT 0x01 /* 8 bit data (0 = 7 bit data) */
195
196#define SETBREAK 0x10 /* send break condition (must clear) */
197#define LOCALLOOP 0x08 /* local loopback set for test */
198#define SET_DTR 0x04 /* assert DTR */
199#define SET_RTS 0x02 /* assert RTS */
200#define TX_ENABLE 0x01 /* enable transmitter */
201
202#define RTSFC_EN 0x40 /* RTS flow control enable */
203#define RXPROC_EN 0x20 /* receive processor enable */
204#define TRIG_NO 0x00 /* Rx FIFO trigger level 0 (no trigger) */
205#define TRIG_1 0x08 /* trigger level 1 char */
206#define TRIG_1_2 0x10 /* trigger level 1/2 */
207#define TRIG_7_8 0x18 /* trigger level 7/8 */
208#define TRIG_MASK 0x18 /* trigger level mask */
209#define SRCINT_EN 0x04 /* special Rx condition interrupt enable */
210#define RXINT_EN 0x02 /* Rx interrupt enable */
211#define MCINT_EN 0x01 /* modem change interrupt enable */
212
213#define RXF_TRIG 0x20 /* Rx FIFO trigger level interrupt */
214#define TXFIFO_MT 0x10 /* Tx FIFO empty interrupt */
215#define SRC_INT 0x08 /* special receive condition interrupt */
216#define DELTA_CD 0x04 /* CD change interrupt */
217#define DELTA_CTS 0x02 /* CTS change interrupt */
218#define DELTA_DSR 0x01 /* DSR change interrupt */
219
220#define REP1W2_EN 0x10 /* replace byte 1 with 2 bytes enable */
221#define IGN2_EN 0x08 /* ignore byte 2 enable */
222#define IGN1_EN 0x04 /* ignore byte 1 enable */
223#define COMP2_EN 0x02 /* compare byte 2 enable */
224#define COMP1_EN 0x01 /* compare byte 1 enable */
225
226#define RESET_ALL 0x80 /* reset AIOP (all channels) */
227#define TXOVERIDE 0x40 /* Transmit software off override */
228#define RESETUART 0x20 /* reset channel's UART */
229#define RESTXFCNT 0x10 /* reset channel's Tx FIFO count register */
230#define RESRXFCNT 0x08 /* reset channel's Rx FIFO count register */
231
232#define INTSTAT0 0x01 /* AIOP 0 interrupt status */
233#define INTSTAT1 0x02 /* AIOP 1 interrupt status */
234#define INTSTAT2 0x04 /* AIOP 2 interrupt status */
235#define INTSTAT3 0x08 /* AIOP 3 interrupt status */
236
237#define INTR_EN 0x08 /* allow interrupts to host */
238#define INT_STROB 0x04 /* strobe and clear interrupt line (EOI) */
239
240/**************************************************************************
241 MUDBAC remapped for PCI
242**************************************************************************/
243
244#define _CFG_INT_PCI 0x40
245#define _PCI_INT_FUNC 0x3A
246
247#define PCI_STROB 0x2000 /* bit 13 of int aiop register */
248#define INTR_EN_PCI 0x0010 /* allow interrupts to host */
249
250/*
251 * Definitions for Universal PCI board registers
252 */
253#define _PCI_9030_INT_CTRL 0x4c /* Offsets from BAR1 */
254#define _PCI_9030_GPIO_CTRL 0x54
255#define PCI_INT_CTRL_AIOP 0x0001
256#define PCI_GPIO_CTRL_8PORT 0x4000
257#define _PCI_9030_RING_IND 0xc0 /* Offsets from BAR1 */
258
259#define CHAN3_EN 0x08 /* enable AIOP 3 */
260#define CHAN2_EN 0x04 /* enable AIOP 2 */
261#define CHAN1_EN 0x02 /* enable AIOP 1 */
262#define CHAN0_EN 0x01 /* enable AIOP 0 */
263#define FREQ_DIS 0x00
264#define FREQ_274HZ 0x60
265#define FREQ_137HZ 0x50
266#define FREQ_69HZ 0x40
267#define FREQ_34HZ 0x30
268#define FREQ_17HZ 0x20
269#define FREQ_9HZ 0x10
270#define PERIODIC_ONLY 0x80 /* only PERIODIC interrupt */
271
272#define CHANINT_EN 0x0100 /* flags to enable/disable channel ints */
273
274#define RDATASIZE 72
275#define RREGDATASIZE 52
276
277/*
278 * AIOP interrupt bits for ISA/PCI boards and UPCI boards.
279 */
280#define AIOP_INTR_BIT_0 0x0001
281#define AIOP_INTR_BIT_1 0x0002
282#define AIOP_INTR_BIT_2 0x0004
283#define AIOP_INTR_BIT_3 0x0008
284
285#define AIOP_INTR_BITS ( \
286 AIOP_INTR_BIT_0 \
287 | AIOP_INTR_BIT_1 \
288 | AIOP_INTR_BIT_2 \
289 | AIOP_INTR_BIT_3)
290
291#define UPCI_AIOP_INTR_BIT_0 0x0004
292#define UPCI_AIOP_INTR_BIT_1 0x0020
293#define UPCI_AIOP_INTR_BIT_2 0x0100
294#define UPCI_AIOP_INTR_BIT_3 0x0800
295
296#define UPCI_AIOP_INTR_BITS ( \
297 UPCI_AIOP_INTR_BIT_0 \
298 | UPCI_AIOP_INTR_BIT_1 \
299 | UPCI_AIOP_INTR_BIT_2 \
300 | UPCI_AIOP_INTR_BIT_3)
301
302/* Controller level information structure */
303typedef struct {
304 int CtlID;
305 int CtlNum;
306 int BusType;
307 int boardType;
308 int isUPCI;
309 WordIO_t PCIIO;
310 WordIO_t PCIIO2;
311 ByteIO_t MBaseIO;
312 ByteIO_t MReg1IO;
313 ByteIO_t MReg2IO;
314 ByteIO_t MReg3IO;
315 Byte_t MReg2;
316 Byte_t MReg3;
317 int NumAiop;
318 int AltChanRingIndicator;
319 ByteIO_t UPCIRingInd;
320 WordIO_t AiopIO[AIOP_CTL_SIZE];
321 ByteIO_t AiopIntChanIO[AIOP_CTL_SIZE];
322 int AiopID[AIOP_CTL_SIZE];
323 int AiopNumChan[AIOP_CTL_SIZE];
324 Word_t *AiopIntrBits;
325} CONTROLLER_T;
326
327typedef CONTROLLER_T CONTROLLER_t;
328
329/* Channel level information structure */
330typedef struct {
331 CONTROLLER_T *CtlP;
332 int AiopNum;
333 int ChanID;
334 int ChanNum;
335 int rtsToggle;
336
337 ByteIO_t Cmd;
338 ByteIO_t IntChan;
339 ByteIO_t IntMask;
340 DWordIO_t IndexAddr;
341 WordIO_t IndexData;
342
343 WordIO_t TxRxData;
344 WordIO_t ChanStat;
345 WordIO_t TxRxCount;
346 ByteIO_t IntID;
347
348 Word_t TxFIFO;
349 Word_t TxFIFOPtrs;
350 Word_t RxFIFO;
351 Word_t RxFIFOPtrs;
352 Word_t TxPrioCnt;
353 Word_t TxPrioPtr;
354 Word_t TxPrioBuf;
355
356 Byte_t R[RREGDATASIZE];
357
358 Byte_t BaudDiv[4];
359 Byte_t TxControl[4];
360 Byte_t RxControl[4];
361 Byte_t TxEnables[4];
362 Byte_t TxCompare[4];
363 Byte_t TxReplace1[4];
364 Byte_t TxReplace2[4];
365} CHANNEL_T;
366
367typedef CHANNEL_T CHANNEL_t;
368typedef CHANNEL_T *CHANPTR_T;
369
370#define InterfaceModeRS232 0x00
371#define InterfaceModeRS422 0x08
372#define InterfaceModeRS485 0x10
373#define InterfaceModeRS232T 0x18
374
375/***************************************************************************
376Function: sClrBreak
377Purpose: Stop sending a transmit BREAK signal
378Call: sClrBreak(ChP)
379 CHANNEL_T *ChP; Ptr to channel structure
380*/
381#define sClrBreak(ChP) \
382do { \
383 (ChP)->TxControl[3] &= ~SETBREAK; \
384 out32((ChP)->IndexAddr,(ChP)->TxControl); \
385} while (0)
386
387/***************************************************************************
388Function: sClrDTR
389Purpose: Clr the DTR output
390Call: sClrDTR(ChP)
391 CHANNEL_T *ChP; Ptr to channel structure
392*/
393#define sClrDTR(ChP) \
394do { \
395 (ChP)->TxControl[3] &= ~SET_DTR; \
396 out32((ChP)->IndexAddr,(ChP)->TxControl); \
397} while (0)
398
399/***************************************************************************
400Function: sClrRTS
401Purpose: Clr the RTS output
402Call: sClrRTS(ChP)
403 CHANNEL_T *ChP; Ptr to channel structure
404*/
405#define sClrRTS(ChP) \
406do { \
407 if ((ChP)->rtsToggle) break; \
408 (ChP)->TxControl[3] &= ~SET_RTS; \
409 out32((ChP)->IndexAddr,(ChP)->TxControl); \
410} while (0)
411
412/***************************************************************************
413Function: sClrTxXOFF
414Purpose: Clear any existing transmit software flow control off condition
415Call: sClrTxXOFF(ChP)
416 CHANNEL_T *ChP; Ptr to channel structure
417*/
418#define sClrTxXOFF(ChP) \
419do { \
420 sOutB((ChP)->Cmd,TXOVERIDE | (Byte_t)(ChP)->ChanNum); \
421 sOutB((ChP)->Cmd,(Byte_t)(ChP)->ChanNum); \
422} while (0)
423
424/***************************************************************************
425Function: sCtlNumToCtlPtr
426Purpose: Convert a controller number to controller structure pointer
427Call: sCtlNumToCtlPtr(CtlNum)
428 int CtlNum; Controller number
429Return: CONTROLLER_T *: Ptr to controller structure
430*/
431#define sCtlNumToCtlPtr(CTLNUM) &sController[CTLNUM]
432
433/***************************************************************************
434Function: sControllerEOI
435Purpose: Strobe the MUDBAC's End Of Interrupt bit.
436Call: sControllerEOI(CtlP)
437 CONTROLLER_T *CtlP; Ptr to controller structure
438*/
439#define sControllerEOI(CTLP) sOutB((CTLP)->MReg2IO,(CTLP)->MReg2 | INT_STROB)
440
441/***************************************************************************
442Function: sPCIControllerEOI
443Purpose: Strobe the PCI End Of Interrupt bit.
444 For the UPCI boards, toggle the AIOP interrupt enable bit
445 (this was taken from the Windows driver).
446Call: sPCIControllerEOI(CtlP)
447 CONTROLLER_T *CtlP; Ptr to controller structure
448*/
449#define sPCIControllerEOI(CTLP) \
450do { \
451 if ((CTLP)->isUPCI) { \
452 Word_t w = sInW((CTLP)->PCIIO); \
453 sOutW((CTLP)->PCIIO, (w ^ PCI_INT_CTRL_AIOP)); \
454 sOutW((CTLP)->PCIIO, w); \
455 } \
456 else { \
457 sOutW((CTLP)->PCIIO, PCI_STROB); \
458 } \
459} while (0)
460
461/***************************************************************************
462Function: sDisAiop
463Purpose: Disable I/O access to an AIOP
464Call: sDisAiop(CltP)
465 CONTROLLER_T *CtlP; Ptr to controller structure
466 int AiopNum; Number of AIOP on controller
467*/
468#define sDisAiop(CTLP,AIOPNUM) \
469do { \
470 (CTLP)->MReg3 &= sBitMapClrTbl[AIOPNUM]; \
471 sOutB((CTLP)->MReg3IO,(CTLP)->MReg3); \
472} while (0)
473
474/***************************************************************************
475Function: sDisCTSFlowCtl
476Purpose: Disable output flow control using CTS
477Call: sDisCTSFlowCtl(ChP)
478 CHANNEL_T *ChP; Ptr to channel structure
479*/
480#define sDisCTSFlowCtl(ChP) \
481do { \
482 (ChP)->TxControl[2] &= ~CTSFC_EN; \
483 out32((ChP)->IndexAddr,(ChP)->TxControl); \
484} while (0)
485
486/***************************************************************************
487Function: sDisIXANY
488Purpose: Disable IXANY Software Flow Control
489Call: sDisIXANY(ChP)
490 CHANNEL_T *ChP; Ptr to channel structure
491*/
492#define sDisIXANY(ChP) \
493do { \
494 (ChP)->R[0x0e] = 0x86; \
495 out32((ChP)->IndexAddr,&(ChP)->R[0x0c]); \
496} while (0)
497
498/***************************************************************************
499Function: DisParity
500Purpose: Disable parity
501Call: sDisParity(ChP)
502 CHANNEL_T *ChP; Ptr to channel structure
503Comments: Function sSetParity() can be used in place of functions sEnParity(),
504 sDisParity(), sSetOddParity(), and sSetEvenParity().
505*/
506#define sDisParity(ChP) \
507do { \
508 (ChP)->TxControl[2] &= ~PARITY_EN; \
509 out32((ChP)->IndexAddr,(ChP)->TxControl); \
510} while (0)
511
512/***************************************************************************
513Function: sDisRTSToggle
514Purpose: Disable RTS toggle
515Call: sDisRTSToggle(ChP)
516 CHANNEL_T *ChP; Ptr to channel structure
517*/
518#define sDisRTSToggle(ChP) \
519do { \
520 (ChP)->TxControl[2] &= ~RTSTOG_EN; \
521 out32((ChP)->IndexAddr,(ChP)->TxControl); \
522 (ChP)->rtsToggle = 0; \
523} while (0)
524
525/***************************************************************************
526Function: sDisRxFIFO
527Purpose: Disable Rx FIFO
528Call: sDisRxFIFO(ChP)
529 CHANNEL_T *ChP; Ptr to channel structure
530*/
531#define sDisRxFIFO(ChP) \
532do { \
533 (ChP)->R[0x32] = 0x0a; \
534 out32((ChP)->IndexAddr,&(ChP)->R[0x30]); \
535} while (0)
536
537/***************************************************************************
538Function: sDisRxStatusMode
539Purpose: Disable the Rx status mode
540Call: sDisRxStatusMode(ChP)
541 CHANNEL_T *ChP; Ptr to channel structure
542Comments: This takes the channel out of the receive status mode. All
543 subsequent reads of receive data using sReadRxWord() will return
544 two data bytes.
545*/
546#define sDisRxStatusMode(ChP) sOutW((ChP)->ChanStat,0)
547
548/***************************************************************************
549Function: sDisTransmit
550Purpose: Disable transmit
551Call: sDisTransmit(ChP)
552 CHANNEL_T *ChP; Ptr to channel structure
553 This disables movement of Tx data from the Tx FIFO into the 1 byte
554 Tx buffer. Therefore there could be up to a 2 byte latency
555 between the time sDisTransmit() is called and the transmit buffer
556 and transmit shift register going completely empty.
557*/
558#define sDisTransmit(ChP) \
559do { \
560 (ChP)->TxControl[3] &= ~TX_ENABLE; \
561 out32((ChP)->IndexAddr,(ChP)->TxControl); \
562} while (0)
563
564/***************************************************************************
565Function: sDisTxSoftFlowCtl
566Purpose: Disable Tx Software Flow Control
567Call: sDisTxSoftFlowCtl(ChP)
568 CHANNEL_T *ChP; Ptr to channel structure
569*/
570#define sDisTxSoftFlowCtl(ChP) \
571do { \
572 (ChP)->R[0x06] = 0x8a; \
573 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
574} while (0)
575
576/***************************************************************************
577Function: sEnAiop
578Purpose: Enable I/O access to an AIOP
579Call: sEnAiop(CltP)
580 CONTROLLER_T *CtlP; Ptr to controller structure
581 int AiopNum; Number of AIOP on controller
582*/
583#define sEnAiop(CTLP,AIOPNUM) \
584do { \
585 (CTLP)->MReg3 |= sBitMapSetTbl[AIOPNUM]; \
586 sOutB((CTLP)->MReg3IO,(CTLP)->MReg3); \
587} while (0)
588
589/***************************************************************************
590Function: sEnCTSFlowCtl
591Purpose: Enable output flow control using CTS
592Call: sEnCTSFlowCtl(ChP)
593 CHANNEL_T *ChP; Ptr to channel structure
594*/
595#define sEnCTSFlowCtl(ChP) \
596do { \
597 (ChP)->TxControl[2] |= CTSFC_EN; \
598 out32((ChP)->IndexAddr,(ChP)->TxControl); \
599} while (0)
600
601/***************************************************************************
602Function: sEnIXANY
603Purpose: Enable IXANY Software Flow Control
604Call: sEnIXANY(ChP)
605 CHANNEL_T *ChP; Ptr to channel structure
606*/
607#define sEnIXANY(ChP) \
608do { \
609 (ChP)->R[0x0e] = 0x21; \
610 out32((ChP)->IndexAddr,&(ChP)->R[0x0c]); \
611} while (0)
612
613/***************************************************************************
614Function: EnParity
615Purpose: Enable parity
616Call: sEnParity(ChP)
617 CHANNEL_T *ChP; Ptr to channel structure
618Comments: Function sSetParity() can be used in place of functions sEnParity(),
619 sDisParity(), sSetOddParity(), and sSetEvenParity().
620
621Warnings: Before enabling parity odd or even parity should be chosen using
622 functions sSetOddParity() or sSetEvenParity().
623*/
624#define sEnParity(ChP) \
625do { \
626 (ChP)->TxControl[2] |= PARITY_EN; \
627 out32((ChP)->IndexAddr,(ChP)->TxControl); \
628} while (0)
629
630/***************************************************************************
631Function: sEnRTSToggle
632Purpose: Enable RTS toggle
633Call: sEnRTSToggle(ChP)
634 CHANNEL_T *ChP; Ptr to channel structure
635Comments: This function will disable RTS flow control and clear the RTS
636 line to allow operation of RTS toggle.
637*/
638#define sEnRTSToggle(ChP) \
639do { \
640 (ChP)->RxControl[2] &= ~RTSFC_EN; \
641 out32((ChP)->IndexAddr,(ChP)->RxControl); \
642 (ChP)->TxControl[2] |= RTSTOG_EN; \
643 (ChP)->TxControl[3] &= ~SET_RTS; \
644 out32((ChP)->IndexAddr,(ChP)->TxControl); \
645 (ChP)->rtsToggle = 1; \
646} while (0)
647
648/***************************************************************************
649Function: sEnRxFIFO
650Purpose: Enable Rx FIFO
651Call: sEnRxFIFO(ChP)
652 CHANNEL_T *ChP; Ptr to channel structure
653*/
654#define sEnRxFIFO(ChP) \
655do { \
656 (ChP)->R[0x32] = 0x08; \
657 out32((ChP)->IndexAddr,&(ChP)->R[0x30]); \
658} while (0)
659
660/***************************************************************************
661Function: sEnRxProcessor
662Purpose: Enable the receive processor
663Call: sEnRxProcessor(ChP)
664 CHANNEL_T *ChP; Ptr to channel structure
665Comments: This function is used to start the receive processor. When
666 the channel is in the reset state the receive processor is not
667 running. This is done to prevent the receive processor from
668 executing invalid microcode instructions prior to the
669 downloading of the microcode.
670
671Warnings: This function must be called after valid microcode has been
672 downloaded to the AIOP, and it must not be called before the
673 microcode has been downloaded.
674*/
675#define sEnRxProcessor(ChP) \
676do { \
677 (ChP)->RxControl[2] |= RXPROC_EN; \
678 out32((ChP)->IndexAddr,(ChP)->RxControl); \
679} while (0)
680
681/***************************************************************************
682Function: sEnRxStatusMode
683Purpose: Enable the Rx status mode
684Call: sEnRxStatusMode(ChP)
685 CHANNEL_T *ChP; Ptr to channel structure
686Comments: This places the channel in the receive status mode. All subsequent
687 reads of receive data using sReadRxWord() will return a data byte
688 in the low word and a status byte in the high word.
689
690*/
691#define sEnRxStatusMode(ChP) sOutW((ChP)->ChanStat,STATMODE)
692
693/***************************************************************************
694Function: sEnTransmit
695Purpose: Enable transmit
696Call: sEnTransmit(ChP)
697 CHANNEL_T *ChP; Ptr to channel structure
698*/
699#define sEnTransmit(ChP) \
700do { \
701 (ChP)->TxControl[3] |= TX_ENABLE; \
702 out32((ChP)->IndexAddr,(ChP)->TxControl); \
703} while (0)
704
705/***************************************************************************
706Function: sEnTxSoftFlowCtl
707Purpose: Enable Tx Software Flow Control
708Call: sEnTxSoftFlowCtl(ChP)
709 CHANNEL_T *ChP; Ptr to channel structure
710*/
711#define sEnTxSoftFlowCtl(ChP) \
712do { \
713 (ChP)->R[0x06] = 0xc5; \
714 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
715} while (0)
716
717/***************************************************************************
718Function: sGetAiopIntStatus
719Purpose: Get the AIOP interrupt status
720Call: sGetAiopIntStatus(CtlP,AiopNum)
721 CONTROLLER_T *CtlP; Ptr to controller structure
722 int AiopNum; AIOP number
723Return: Byte_t: The AIOP interrupt status. Bits 0 through 7
724 represent channels 0 through 7 respectively. If a
725 bit is set that channel is interrupting.
726*/
727#define sGetAiopIntStatus(CTLP,AIOPNUM) sInB((CTLP)->AiopIntChanIO[AIOPNUM])
728
729/***************************************************************************
730Function: sGetAiopNumChan
731Purpose: Get the number of channels supported by an AIOP
732Call: sGetAiopNumChan(CtlP,AiopNum)
733 CONTROLLER_T *CtlP; Ptr to controller structure
734 int AiopNum; AIOP number
735Return: int: The number of channels supported by the AIOP
736*/
737#define sGetAiopNumChan(CTLP,AIOPNUM) (CTLP)->AiopNumChan[AIOPNUM]
738
739/***************************************************************************
740Function: sGetChanIntID
741Purpose: Get a channel's interrupt identification byte
742Call: sGetChanIntID(ChP)
743 CHANNEL_T *ChP; Ptr to channel structure
744Return: Byte_t: The channel interrupt ID. Can be any
745 combination of the following flags:
746 RXF_TRIG: Rx FIFO trigger level interrupt
747 TXFIFO_MT: Tx FIFO empty interrupt
748 SRC_INT: Special receive condition interrupt
749 DELTA_CD: CD change interrupt
750 DELTA_CTS: CTS change interrupt
751 DELTA_DSR: DSR change interrupt
752*/
753#define sGetChanIntID(ChP) (sInB((ChP)->IntID) & (RXF_TRIG | TXFIFO_MT | SRC_INT | DELTA_CD | DELTA_CTS | DELTA_DSR))
754
755/***************************************************************************
756Function: sGetChanNum
757Purpose: Get the number of a channel within an AIOP
758Call: sGetChanNum(ChP)
759 CHANNEL_T *ChP; Ptr to channel structure
760Return: int: Channel number within AIOP, or NULLCHAN if channel does
761 not exist.
762*/
763#define sGetChanNum(ChP) (ChP)->ChanNum
764
765/***************************************************************************
766Function: sGetChanStatus
767Purpose: Get the channel status
768Call: sGetChanStatus(ChP)
769 CHANNEL_T *ChP; Ptr to channel structure
770Return: Word_t: The channel status. Can be any combination of
771 the following flags:
772 LOW BYTE FLAGS
773 CTS_ACT: CTS input asserted
774 DSR_ACT: DSR input asserted
775 CD_ACT: CD input asserted
776 TXFIFOMT: Tx FIFO is empty
777 TXSHRMT: Tx shift register is empty
778 RDA: Rx data available
779
780 HIGH BYTE FLAGS
781 STATMODE: status mode enable bit
782 RXFOVERFL: receive FIFO overflow
783 RX2MATCH: receive compare byte 2 match
784 RX1MATCH: receive compare byte 1 match
785 RXBREAK: received BREAK
786 RXFRAME: received framing error
787 RXPARITY: received parity error
788Warnings: This function will clear the high byte flags in the Channel
789 Status Register.
790*/
791#define sGetChanStatus(ChP) sInW((ChP)->ChanStat)
792
793/***************************************************************************
794Function: sGetChanStatusLo
795Purpose: Get the low byte only of the channel status
796Call: sGetChanStatusLo(ChP)
797 CHANNEL_T *ChP; Ptr to channel structure
798Return: Byte_t: The channel status low byte. Can be any combination
799 of the following flags:
800 CTS_ACT: CTS input asserted
801 DSR_ACT: DSR input asserted
802 CD_ACT: CD input asserted
803 TXFIFOMT: Tx FIFO is empty
804 TXSHRMT: Tx shift register is empty
805 RDA: Rx data available
806*/
807#define sGetChanStatusLo(ChP) sInB((ByteIO_t)(ChP)->ChanStat)
808
809/**********************************************************************
810 * Get RI status of channel
811 * Defined as a function in rocket.c -aes
812 */
813#if 0
814#define sGetChanRI(ChP) ((ChP)->CtlP->AltChanRingIndicator ? \
815 (sInB((ByteIO_t)((ChP)->ChanStat+8)) & DSR_ACT) : \
816 (((ChP)->CtlP->boardType == ROCKET_TYPE_PC104) ? \
817 (!(sInB((ChP)->CtlP->AiopIO[3]) & sBitMapSetTbl[(ChP)->ChanNum])) : \
818 0))
819#endif
820
821/***************************************************************************
822Function: sGetControllerIntStatus
823Purpose: Get the controller interrupt status
824Call: sGetControllerIntStatus(CtlP)
825 CONTROLLER_T *CtlP; Ptr to controller structure
826Return: Byte_t: The controller interrupt status in the lower 4
827 bits. Bits 0 through 3 represent AIOP's 0
828 through 3 respectively. If a bit is set that
829 AIOP is interrupting. Bits 4 through 7 will
830 always be cleared.
831*/
832#define sGetControllerIntStatus(CTLP) (sInB((CTLP)->MReg1IO) & 0x0f)
833
834/***************************************************************************
835Function: sPCIGetControllerIntStatus
836Purpose: Get the controller interrupt status
837Call: sPCIGetControllerIntStatus(CtlP)
838 CONTROLLER_T *CtlP; Ptr to controller structure
839Return: unsigned char: The controller interrupt status in the lower 4
840 bits and bit 4. Bits 0 through 3 represent AIOP's 0
841 through 3 respectively. Bit 4 is set if the int
842 was generated from periodic. If a bit is set the
843 AIOP is interrupting.
844*/
845#define sPCIGetControllerIntStatus(CTLP) \
846 ((CTLP)->isUPCI ? \
847 (sInW((CTLP)->PCIIO2) & UPCI_AIOP_INTR_BITS) : \
848 ((sInW((CTLP)->PCIIO) >> 8) & AIOP_INTR_BITS))
849
850/***************************************************************************
851
852Function: sGetRxCnt
853Purpose: Get the number of data bytes in the Rx FIFO
854Call: sGetRxCnt(ChP)
855 CHANNEL_T *ChP; Ptr to channel structure
856Return: int: The number of data bytes in the Rx FIFO.
857Comments: Byte read of count register is required to obtain Rx count.
858
859*/
860#define sGetRxCnt(ChP) sInW((ChP)->TxRxCount)
861
862/***************************************************************************
863Function: sGetTxCnt
864Purpose: Get the number of data bytes in the Tx FIFO
865Call: sGetTxCnt(ChP)
866 CHANNEL_T *ChP; Ptr to channel structure
867Return: Byte_t: The number of data bytes in the Tx FIFO.
868Comments: Byte read of count register is required to obtain Tx count.
869
870*/
871#define sGetTxCnt(ChP) sInB((ByteIO_t)(ChP)->TxRxCount)
872
873/*****************************************************************************
874Function: sGetTxRxDataIO
875Purpose: Get the I/O address of a channel's TxRx Data register
876Call: sGetTxRxDataIO(ChP)
877 CHANNEL_T *ChP; Ptr to channel structure
878Return: WordIO_t: I/O address of a channel's TxRx Data register
879*/
880#define sGetTxRxDataIO(ChP) (ChP)->TxRxData
881
882/***************************************************************************
883Function: sInitChanDefaults
884Purpose: Initialize a channel structure to it's default state.
885Call: sInitChanDefaults(ChP)
886 CHANNEL_T *ChP; Ptr to the channel structure
887Comments: This function must be called once for every channel structure
888 that exists before any other SSCI calls can be made.
889
890*/
891#define sInitChanDefaults(ChP) \
892do { \
893 (ChP)->CtlP = NULLCTLPTR; \
894 (ChP)->AiopNum = NULLAIOP; \
895 (ChP)->ChanID = AIOPID_NULL; \
896 (ChP)->ChanNum = NULLCHAN; \
897} while (0)
898
899/***************************************************************************
900Function: sResetAiopByNum
901Purpose: Reset the AIOP by number
902Call: sResetAiopByNum(CTLP,AIOPNUM)
903 CONTROLLER_T CTLP; Ptr to controller structure
904 AIOPNUM; AIOP index
905*/
906#define sResetAiopByNum(CTLP,AIOPNUM) \
907do { \
908 sOutB((CTLP)->AiopIO[(AIOPNUM)]+_CMD_REG,RESET_ALL); \
909 sOutB((CTLP)->AiopIO[(AIOPNUM)]+_CMD_REG,0x0); \
910} while (0)
911
912/***************************************************************************
913Function: sSendBreak
914Purpose: Send a transmit BREAK signal
915Call: sSendBreak(ChP)
916 CHANNEL_T *ChP; Ptr to channel structure
917*/
918#define sSendBreak(ChP) \
919do { \
920 (ChP)->TxControl[3] |= SETBREAK; \
921 out32((ChP)->IndexAddr,(ChP)->TxControl); \
922} while (0)
923
924/***************************************************************************
925Function: sSetBaud
926Purpose: Set baud rate
927Call: sSetBaud(ChP,Divisor)
928 CHANNEL_T *ChP; Ptr to channel structure
929 Word_t Divisor; 16 bit baud rate divisor for channel
930*/
931#define sSetBaud(ChP,DIVISOR) \
932do { \
933 (ChP)->BaudDiv[2] = (Byte_t)(DIVISOR); \
934 (ChP)->BaudDiv[3] = (Byte_t)((DIVISOR) >> 8); \
935 out32((ChP)->IndexAddr,(ChP)->BaudDiv); \
936} while (0)
937
938/***************************************************************************
939Function: sSetData7
940Purpose: Set data bits to 7
941Call: sSetData7(ChP)
942 CHANNEL_T *ChP; Ptr to channel structure
943*/
944#define sSetData7(ChP) \
945do { \
946 (ChP)->TxControl[2] &= ~DATA8BIT; \
947 out32((ChP)->IndexAddr,(ChP)->TxControl); \
948} while (0)
949
950/***************************************************************************
951Function: sSetData8
952Purpose: Set data bits to 8
953Call: sSetData8(ChP)
954 CHANNEL_T *ChP; Ptr to channel structure
955*/
956#define sSetData8(ChP) \
957do { \
958 (ChP)->TxControl[2] |= DATA8BIT; \
959 out32((ChP)->IndexAddr,(ChP)->TxControl); \
960} while (0)
961
962/***************************************************************************
963Function: sSetDTR
964Purpose: Set the DTR output
965Call: sSetDTR(ChP)
966 CHANNEL_T *ChP; Ptr to channel structure
967*/
968#define sSetDTR(ChP) \
969do { \
970 (ChP)->TxControl[3] |= SET_DTR; \
971 out32((ChP)->IndexAddr,(ChP)->TxControl); \
972} while (0)
973
974/***************************************************************************
975Function: sSetEvenParity
976Purpose: Set even parity
977Call: sSetEvenParity(ChP)
978 CHANNEL_T *ChP; Ptr to channel structure
979Comments: Function sSetParity() can be used in place of functions sEnParity(),
980 sDisParity(), sSetOddParity(), and sSetEvenParity().
981
982Warnings: This function has no effect unless parity is enabled with function
983 sEnParity().
984*/
985#define sSetEvenParity(ChP) \
986do { \
987 (ChP)->TxControl[2] |= EVEN_PAR; \
988 out32((ChP)->IndexAddr,(ChP)->TxControl); \
989} while (0)
990
991/***************************************************************************
992Function: sSetOddParity
993Purpose: Set odd parity
994Call: sSetOddParity(ChP)
995 CHANNEL_T *ChP; Ptr to channel structure
996Comments: Function sSetParity() can be used in place of functions sEnParity(),
997 sDisParity(), sSetOddParity(), and sSetEvenParity().
998
999Warnings: This function has no effect unless parity is enabled with function
1000 sEnParity().
1001*/
1002#define sSetOddParity(ChP) \
1003do { \
1004 (ChP)->TxControl[2] &= ~EVEN_PAR; \
1005 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1006} while (0)
1007
1008/***************************************************************************
1009Function: sSetRTS
1010Purpose: Set the RTS output
1011Call: sSetRTS(ChP)
1012 CHANNEL_T *ChP; Ptr to channel structure
1013*/
1014#define sSetRTS(ChP) \
1015do { \
1016 if ((ChP)->rtsToggle) break; \
1017 (ChP)->TxControl[3] |= SET_RTS; \
1018 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1019} while (0)
1020
1021/***************************************************************************
1022Function: sSetRxTrigger
1023Purpose: Set the Rx FIFO trigger level
1024Call: sSetRxProcessor(ChP,Level)
1025 CHANNEL_T *ChP; Ptr to channel structure
1026 Byte_t Level; Number of characters in Rx FIFO at which the
1027 interrupt will be generated. Can be any of the following flags:
1028
1029 TRIG_NO: no trigger
1030 TRIG_1: 1 character in FIFO
1031 TRIG_1_2: FIFO 1/2 full
1032 TRIG_7_8: FIFO 7/8 full
1033Comments: An interrupt will be generated when the trigger level is reached
1034 only if function sEnInterrupt() has been called with flag
1035 RXINT_EN set. The RXF_TRIG flag in the Interrupt Idenfification
1036 register will be set whenever the trigger level is reached
1037 regardless of the setting of RXINT_EN.
1038
1039*/
1040#define sSetRxTrigger(ChP,LEVEL) \
1041do { \
1042 (ChP)->RxControl[2] &= ~TRIG_MASK; \
1043 (ChP)->RxControl[2] |= LEVEL; \
1044 out32((ChP)->IndexAddr,(ChP)->RxControl); \
1045} while (0)
1046
1047/***************************************************************************
1048Function: sSetStop1
1049Purpose: Set stop bits to 1
1050Call: sSetStop1(ChP)
1051 CHANNEL_T *ChP; Ptr to channel structure
1052*/
1053#define sSetStop1(ChP) \
1054do { \
1055 (ChP)->TxControl[2] &= ~STOP2; \
1056 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1057} while (0)
1058
1059/***************************************************************************
1060Function: sSetStop2
1061Purpose: Set stop bits to 2
1062Call: sSetStop2(ChP)
1063 CHANNEL_T *ChP; Ptr to channel structure
1064*/
1065#define sSetStop2(ChP) \
1066do { \
1067 (ChP)->TxControl[2] |= STOP2; \
1068 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1069} while (0)
1070
1071/***************************************************************************
1072Function: sSetTxXOFFChar
1073Purpose: Set the Tx XOFF flow control character
1074Call: sSetTxXOFFChar(ChP,Ch)
1075 CHANNEL_T *ChP; Ptr to channel structure
1076 Byte_t Ch; The value to set the Tx XOFF character to
1077*/
1078#define sSetTxXOFFChar(ChP,CH) \
1079do { \
1080 (ChP)->R[0x07] = (CH); \
1081 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
1082} while (0)
1083
1084/***************************************************************************
1085Function: sSetTxXONChar
1086Purpose: Set the Tx XON flow control character
1087Call: sSetTxXONChar(ChP,Ch)
1088 CHANNEL_T *ChP; Ptr to channel structure
1089 Byte_t Ch; The value to set the Tx XON character to
1090*/
1091#define sSetTxXONChar(ChP,CH) \
1092do { \
1093 (ChP)->R[0x0b] = (CH); \
1094 out32((ChP)->IndexAddr,&(ChP)->R[0x08]); \
1095} while (0)
1096
1097/***************************************************************************
1098Function: sStartRxProcessor
1099Purpose: Start a channel's receive processor
1100Call: sStartRxProcessor(ChP)
1101 CHANNEL_T *ChP; Ptr to channel structure
1102Comments: This function is used to start a Rx processor after it was
1103 stopped with sStopRxProcessor() or sStopSWInFlowCtl(). It
1104 will restart both the Rx processor and software input flow control.
1105
1106*/
1107#define sStartRxProcessor(ChP) out32((ChP)->IndexAddr,&(ChP)->R[0])
1108
1109/***************************************************************************
1110Function: sWriteTxByte
1111Purpose: Write a transmit data byte to a channel.
1112 ByteIO_t io: Channel transmit register I/O address. This can
1113 be obtained with sGetTxRxDataIO().
1114 Byte_t Data; The transmit data byte.
1115Warnings: This function writes the data byte without checking to see if
1116 sMaxTxSize is exceeded in the Tx FIFO.
1117*/
1118#define sWriteTxByte(IO,DATA) sOutB(IO,DATA)
1119
1120/*
1121 * Begin Linux specific definitions for the Rocketport driver
1122 *
1123 * This code is Copyright Theodore Ts'o, 1995-1997
1124 */
1125
1126struct r_port {
1127 int magic;
1128 struct tty_port port;
1129 int line;
1130 int flags; /* Don't yet match the ASY_ flags!! */
1131 unsigned int board:3;
1132 unsigned int aiop:2;
1133 unsigned int chan:3;
1134 CONTROLLER_t *ctlp;
1135 CHANNEL_t channel;
1136 int intmask;
1137 int xmit_fifo_room; /* room in xmit fifo */
1138 unsigned char *xmit_buf;
1139 int xmit_head;
1140 int xmit_tail;
1141 int xmit_cnt;
1142 int cd_status;
1143 int ignore_status_mask;
1144 int read_status_mask;
1145 int cps;
1146
1147 struct completion close_wait; /* Not yet matching the core */
1148 spinlock_t slock;
1149 struct mutex write_mtx;
1150};
1151
1152#define RPORT_MAGIC 0x525001
1153
1154#define NUM_BOARDS 8
1155#define MAX_RP_PORTS (32*NUM_BOARDS)
1156
1157/*
1158 * The size of the xmit buffer is 1 page, or 4096 bytes
1159 */
1160#define XMIT_BUF_SIZE 4096
1161
1162/* number of characters left in xmit buffer before we ask for more */
1163#define WAKEUP_CHARS 256
1164
1165/*
1166 * Assigned major numbers for the Comtrol Rocketport
1167 */
1168#define TTY_ROCKET_MAJOR 46
1169#define CUA_ROCKET_MAJOR 47
1170
1171#ifdef PCI_VENDOR_ID_RP
1172#undef PCI_VENDOR_ID_RP
1173#undef PCI_DEVICE_ID_RP8OCTA
1174#undef PCI_DEVICE_ID_RP8INTF
1175#undef PCI_DEVICE_ID_RP16INTF
1176#undef PCI_DEVICE_ID_RP32INTF
1177#undef PCI_DEVICE_ID_URP8OCTA
1178#undef PCI_DEVICE_ID_URP8INTF
1179#undef PCI_DEVICE_ID_URP16INTF
1180#undef PCI_DEVICE_ID_CRP16INTF
1181#undef PCI_DEVICE_ID_URP32INTF
1182#endif
1183
1184/* Comtrol PCI Vendor ID */
1185#define PCI_VENDOR_ID_RP 0x11fe
1186
1187/* Comtrol Device ID's */
1188#define PCI_DEVICE_ID_RP32INTF 0x0001 /* Rocketport 32 port w/external I/F */
1189#define PCI_DEVICE_ID_RP8INTF 0x0002 /* Rocketport 8 port w/external I/F */
1190#define PCI_DEVICE_ID_RP16INTF 0x0003 /* Rocketport 16 port w/external I/F */
1191#define PCI_DEVICE_ID_RP4QUAD 0x0004 /* Rocketport 4 port w/quad cable */
1192#define PCI_DEVICE_ID_RP8OCTA 0x0005 /* Rocketport 8 port w/octa cable */
1193#define PCI_DEVICE_ID_RP8J 0x0006 /* Rocketport 8 port w/RJ11 connectors */
1194#define PCI_DEVICE_ID_RP4J 0x0007 /* Rocketport 4 port w/RJ11 connectors */
1195#define PCI_DEVICE_ID_RP8SNI 0x0008 /* Rocketport 8 port w/ DB78 SNI (Siemens) connector */
1196#define PCI_DEVICE_ID_RP16SNI 0x0009 /* Rocketport 16 port w/ DB78 SNI (Siemens) connector */
1197#define PCI_DEVICE_ID_RPP4 0x000A /* Rocketport Plus 4 port */
1198#define PCI_DEVICE_ID_RPP8 0x000B /* Rocketport Plus 8 port */
1199#define PCI_DEVICE_ID_RP6M 0x000C /* RocketModem 6 port */
1200#define PCI_DEVICE_ID_RP4M 0x000D /* RocketModem 4 port */
1201#define PCI_DEVICE_ID_RP2_232 0x000E /* Rocketport Plus 2 port RS232 */
1202#define PCI_DEVICE_ID_RP2_422 0x000F /* Rocketport Plus 2 port RS422 */
1203
1204/* Universal PCI boards */
1205#define PCI_DEVICE_ID_URP32INTF 0x0801 /* Rocketport UPCI 32 port w/external I/F */
1206#define PCI_DEVICE_ID_URP8INTF 0x0802 /* Rocketport UPCI 8 port w/external I/F */
1207#define PCI_DEVICE_ID_URP16INTF 0x0803 /* Rocketport UPCI 16 port w/external I/F */
1208#define PCI_DEVICE_ID_URP8OCTA 0x0805 /* Rocketport UPCI 8 port w/octa cable */
1209#define PCI_DEVICE_ID_UPCI_RM3_8PORT 0x080C /* Rocketmodem III 8 port */
1210#define PCI_DEVICE_ID_UPCI_RM3_4PORT 0x080D /* Rocketmodem III 4 port */
1211
1212/* Compact PCI device */
1213#define PCI_DEVICE_ID_CRP16INTF 0x0903 /* Rocketport Compact PCI 16 port w/external I/F */
1214
diff --git a/drivers/tty/serial/21285.c b/drivers/tty/serial/21285.c
new file mode 100644
index 000000000000..1b37626e8f13
--- /dev/null
+++ b/drivers/tty/serial/21285.c
@@ -0,0 +1,511 @@
1/*
2 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
3 *
4 * Based on drivers/char/serial.c
5 */
6#include <linux/module.h>
7#include <linux/tty.h>
8#include <linux/ioport.h>
9#include <linux/init.h>
10#include <linux/console.h>
11#include <linux/device.h>
12#include <linux/tty_flip.h>
13#include <linux/serial_core.h>
14#include <linux/serial.h>
15#include <linux/io.h>
16
17#include <asm/irq.h>
18#include <asm/mach-types.h>
19#include <asm/hardware/dec21285.h>
20#include <mach/hardware.h>
21
22#define BAUD_BASE (mem_fclk_21285/64)
23
24#define SERIAL_21285_NAME "ttyFB"
25#define SERIAL_21285_MAJOR 204
26#define SERIAL_21285_MINOR 4
27
28#define RXSTAT_DUMMY_READ 0x80000000
29#define RXSTAT_FRAME (1 << 0)
30#define RXSTAT_PARITY (1 << 1)
31#define RXSTAT_OVERRUN (1 << 2)
32#define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
33
34#define H_UBRLCR_BREAK (1 << 0)
35#define H_UBRLCR_PARENB (1 << 1)
36#define H_UBRLCR_PAREVN (1 << 2)
37#define H_UBRLCR_STOPB (1 << 3)
38#define H_UBRLCR_FIFO (1 << 4)
39
40static const char serial21285_name[] = "Footbridge UART";
41
42#define tx_enabled(port) ((port)->unused[0])
43#define rx_enabled(port) ((port)->unused[1])
44
45/*
46 * The documented expression for selecting the divisor is:
47 * BAUD_BASE / baud - 1
48 * However, typically BAUD_BASE is not divisible by baud, so
49 * we want to select the divisor that gives us the minimum
50 * error. Therefore, we want:
51 * int(BAUD_BASE / baud - 0.5) ->
52 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
53 * int((BAUD_BASE - (baud >> 1)) / baud)
54 */
55
56static void serial21285_stop_tx(struct uart_port *port)
57{
58 if (tx_enabled(port)) {
59 disable_irq_nosync(IRQ_CONTX);
60 tx_enabled(port) = 0;
61 }
62}
63
64static void serial21285_start_tx(struct uart_port *port)
65{
66 if (!tx_enabled(port)) {
67 enable_irq(IRQ_CONTX);
68 tx_enabled(port) = 1;
69 }
70}
71
72static void serial21285_stop_rx(struct uart_port *port)
73{
74 if (rx_enabled(port)) {
75 disable_irq_nosync(IRQ_CONRX);
76 rx_enabled(port) = 0;
77 }
78}
79
80static void serial21285_enable_ms(struct uart_port *port)
81{
82}
83
84static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
85{
86 struct uart_port *port = dev_id;
87 struct tty_struct *tty = port->state->port.tty;
88 unsigned int status, ch, flag, rxs, max_count = 256;
89
90 status = *CSR_UARTFLG;
91 while (!(status & 0x10) && max_count--) {
92 ch = *CSR_UARTDR;
93 flag = TTY_NORMAL;
94 port->icount.rx++;
95
96 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
97 if (unlikely(rxs & RXSTAT_ANYERR)) {
98 if (rxs & RXSTAT_PARITY)
99 port->icount.parity++;
100 else if (rxs & RXSTAT_FRAME)
101 port->icount.frame++;
102 if (rxs & RXSTAT_OVERRUN)
103 port->icount.overrun++;
104
105 rxs &= port->read_status_mask;
106
107 if (rxs & RXSTAT_PARITY)
108 flag = TTY_PARITY;
109 else if (rxs & RXSTAT_FRAME)
110 flag = TTY_FRAME;
111 }
112
113 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
114
115 status = *CSR_UARTFLG;
116 }
117 tty_flip_buffer_push(tty);
118
119 return IRQ_HANDLED;
120}
121
122static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
123{
124 struct uart_port *port = dev_id;
125 struct circ_buf *xmit = &port->state->xmit;
126 int count = 256;
127
128 if (port->x_char) {
129 *CSR_UARTDR = port->x_char;
130 port->icount.tx++;
131 port->x_char = 0;
132 goto out;
133 }
134 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
135 serial21285_stop_tx(port);
136 goto out;
137 }
138
139 do {
140 *CSR_UARTDR = xmit->buf[xmit->tail];
141 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
142 port->icount.tx++;
143 if (uart_circ_empty(xmit))
144 break;
145 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
146
147 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
148 uart_write_wakeup(port);
149
150 if (uart_circ_empty(xmit))
151 serial21285_stop_tx(port);
152
153 out:
154 return IRQ_HANDLED;
155}
156
157static unsigned int serial21285_tx_empty(struct uart_port *port)
158{
159 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
160}
161
162/* no modem control lines */
163static unsigned int serial21285_get_mctrl(struct uart_port *port)
164{
165 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
166}
167
168static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
169{
170}
171
172static void serial21285_break_ctl(struct uart_port *port, int break_state)
173{
174 unsigned long flags;
175 unsigned int h_lcr;
176
177 spin_lock_irqsave(&port->lock, flags);
178 h_lcr = *CSR_H_UBRLCR;
179 if (break_state)
180 h_lcr |= H_UBRLCR_BREAK;
181 else
182 h_lcr &= ~H_UBRLCR_BREAK;
183 *CSR_H_UBRLCR = h_lcr;
184 spin_unlock_irqrestore(&port->lock, flags);
185}
186
187static int serial21285_startup(struct uart_port *port)
188{
189 int ret;
190
191 tx_enabled(port) = 1;
192 rx_enabled(port) = 1;
193
194 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
195 serial21285_name, port);
196 if (ret == 0) {
197 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
198 serial21285_name, port);
199 if (ret)
200 free_irq(IRQ_CONRX, port);
201 }
202
203 return ret;
204}
205
206static void serial21285_shutdown(struct uart_port *port)
207{
208 free_irq(IRQ_CONTX, port);
209 free_irq(IRQ_CONRX, port);
210}
211
212static void
213serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
214 struct ktermios *old)
215{
216 unsigned long flags;
217 unsigned int baud, quot, h_lcr, b;
218
219 /*
220 * We don't support modem control lines.
221 */
222 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
223 termios->c_cflag |= CLOCAL;
224
225 /*
226 * We don't support BREAK character recognition.
227 */
228 termios->c_iflag &= ~(IGNBRK | BRKINT);
229
230 /*
231 * Ask the core to calculate the divisor for us.
232 */
233 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
234 quot = uart_get_divisor(port, baud);
235 b = port->uartclk / (16 * quot);
236 tty_termios_encode_baud_rate(termios, b, b);
237
238 switch (termios->c_cflag & CSIZE) {
239 case CS5:
240 h_lcr = 0x00;
241 break;
242 case CS6:
243 h_lcr = 0x20;
244 break;
245 case CS7:
246 h_lcr = 0x40;
247 break;
248 default: /* CS8 */
249 h_lcr = 0x60;
250 break;
251 }
252
253 if (termios->c_cflag & CSTOPB)
254 h_lcr |= H_UBRLCR_STOPB;
255 if (termios->c_cflag & PARENB) {
256 h_lcr |= H_UBRLCR_PARENB;
257 if (!(termios->c_cflag & PARODD))
258 h_lcr |= H_UBRLCR_PAREVN;
259 }
260
261 if (port->fifosize)
262 h_lcr |= H_UBRLCR_FIFO;
263
264 spin_lock_irqsave(&port->lock, flags);
265
266 /*
267 * Update the per-port timeout.
268 */
269 uart_update_timeout(port, termios->c_cflag, baud);
270
271 /*
272 * Which character status flags are we interested in?
273 */
274 port->read_status_mask = RXSTAT_OVERRUN;
275 if (termios->c_iflag & INPCK)
276 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
277
278 /*
279 * Which character status flags should we ignore?
280 */
281 port->ignore_status_mask = 0;
282 if (termios->c_iflag & IGNPAR)
283 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
284 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
285 port->ignore_status_mask |= RXSTAT_OVERRUN;
286
287 /*
288 * Ignore all characters if CREAD is not set.
289 */
290 if ((termios->c_cflag & CREAD) == 0)
291 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
292
293 quot -= 1;
294
295 *CSR_UARTCON = 0;
296 *CSR_L_UBRLCR = quot & 0xff;
297 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
298 *CSR_H_UBRLCR = h_lcr;
299 *CSR_UARTCON = 1;
300
301 spin_unlock_irqrestore(&port->lock, flags);
302}
303
304static const char *serial21285_type(struct uart_port *port)
305{
306 return port->type == PORT_21285 ? "DC21285" : NULL;
307}
308
309static void serial21285_release_port(struct uart_port *port)
310{
311 release_mem_region(port->mapbase, 32);
312}
313
314static int serial21285_request_port(struct uart_port *port)
315{
316 return request_mem_region(port->mapbase, 32, serial21285_name)
317 != NULL ? 0 : -EBUSY;
318}
319
320static void serial21285_config_port(struct uart_port *port, int flags)
321{
322 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
323 port->type = PORT_21285;
324}
325
326/*
327 * verify the new serial_struct (for TIOCSSERIAL).
328 */
329static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
330{
331 int ret = 0;
332 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
333 ret = -EINVAL;
334 if (ser->irq != NO_IRQ)
335 ret = -EINVAL;
336 if (ser->baud_base != port->uartclk / 16)
337 ret = -EINVAL;
338 return ret;
339}
340
341static struct uart_ops serial21285_ops = {
342 .tx_empty = serial21285_tx_empty,
343 .get_mctrl = serial21285_get_mctrl,
344 .set_mctrl = serial21285_set_mctrl,
345 .stop_tx = serial21285_stop_tx,
346 .start_tx = serial21285_start_tx,
347 .stop_rx = serial21285_stop_rx,
348 .enable_ms = serial21285_enable_ms,
349 .break_ctl = serial21285_break_ctl,
350 .startup = serial21285_startup,
351 .shutdown = serial21285_shutdown,
352 .set_termios = serial21285_set_termios,
353 .type = serial21285_type,
354 .release_port = serial21285_release_port,
355 .request_port = serial21285_request_port,
356 .config_port = serial21285_config_port,
357 .verify_port = serial21285_verify_port,
358};
359
360static struct uart_port serial21285_port = {
361 .mapbase = 0x42000160,
362 .iotype = UPIO_MEM,
363 .irq = NO_IRQ,
364 .fifosize = 16,
365 .ops = &serial21285_ops,
366 .flags = UPF_BOOT_AUTOCONF,
367};
368
369static void serial21285_setup_ports(void)
370{
371 serial21285_port.uartclk = mem_fclk_21285 / 4;
372}
373
374#ifdef CONFIG_SERIAL_21285_CONSOLE
375static void serial21285_console_putchar(struct uart_port *port, int ch)
376{
377 while (*CSR_UARTFLG & 0x20)
378 barrier();
379 *CSR_UARTDR = ch;
380}
381
382static void
383serial21285_console_write(struct console *co, const char *s,
384 unsigned int count)
385{
386 uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
387}
388
389static void __init
390serial21285_get_options(struct uart_port *port, int *baud,
391 int *parity, int *bits)
392{
393 if (*CSR_UARTCON == 1) {
394 unsigned int tmp;
395
396 tmp = *CSR_H_UBRLCR;
397 switch (tmp & 0x60) {
398 case 0x00:
399 *bits = 5;
400 break;
401 case 0x20:
402 *bits = 6;
403 break;
404 case 0x40:
405 *bits = 7;
406 break;
407 default:
408 case 0x60:
409 *bits = 8;
410 break;
411 }
412
413 if (tmp & H_UBRLCR_PARENB) {
414 *parity = 'o';
415 if (tmp & H_UBRLCR_PAREVN)
416 *parity = 'e';
417 }
418
419 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
420
421 *baud = port->uartclk / (16 * (tmp + 1));
422 }
423}
424
425static int __init serial21285_console_setup(struct console *co, char *options)
426{
427 struct uart_port *port = &serial21285_port;
428 int baud = 9600;
429 int bits = 8;
430 int parity = 'n';
431 int flow = 'n';
432
433 if (machine_is_personal_server())
434 baud = 57600;
435
436 /*
437 * Check whether an invalid uart number has been specified, and
438 * if so, search for the first available port that does have
439 * console support.
440 */
441 if (options)
442 uart_parse_options(options, &baud, &parity, &bits, &flow);
443 else
444 serial21285_get_options(port, &baud, &parity, &bits);
445
446 return uart_set_options(port, co, baud, parity, bits, flow);
447}
448
449static struct uart_driver serial21285_reg;
450
451static struct console serial21285_console =
452{
453 .name = SERIAL_21285_NAME,
454 .write = serial21285_console_write,
455 .device = uart_console_device,
456 .setup = serial21285_console_setup,
457 .flags = CON_PRINTBUFFER,
458 .index = -1,
459 .data = &serial21285_reg,
460};
461
462static int __init rs285_console_init(void)
463{
464 serial21285_setup_ports();
465 register_console(&serial21285_console);
466 return 0;
467}
468console_initcall(rs285_console_init);
469
470#define SERIAL_21285_CONSOLE &serial21285_console
471#else
472#define SERIAL_21285_CONSOLE NULL
473#endif
474
475static struct uart_driver serial21285_reg = {
476 .owner = THIS_MODULE,
477 .driver_name = "ttyFB",
478 .dev_name = "ttyFB",
479 .major = SERIAL_21285_MAJOR,
480 .minor = SERIAL_21285_MINOR,
481 .nr = 1,
482 .cons = SERIAL_21285_CONSOLE,
483};
484
485static int __init serial21285_init(void)
486{
487 int ret;
488
489 printk(KERN_INFO "Serial: 21285 driver\n");
490
491 serial21285_setup_ports();
492
493 ret = uart_register_driver(&serial21285_reg);
494 if (ret == 0)
495 uart_add_one_port(&serial21285_reg, &serial21285_port);
496
497 return ret;
498}
499
500static void __exit serial21285_exit(void)
501{
502 uart_remove_one_port(&serial21285_reg, &serial21285_port);
503 uart_unregister_driver(&serial21285_reg);
504}
505
506module_init(serial21285_init);
507module_exit(serial21285_exit);
508
509MODULE_LICENSE("GPL");
510MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
511MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);
diff --git a/drivers/tty/serial/68328serial.c b/drivers/tty/serial/68328serial.c
new file mode 100644
index 000000000000..e0a77540b8ca
--- /dev/null
+++ b/drivers/tty/serial/68328serial.c
@@ -0,0 +1,1448 @@
1/* 68328serial.c: Serial port driver for 68328 microcontroller
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
9 *
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
15 */
16
17#include <asm/dbg.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26#include <linux/major.h>
27#include <linux/string.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/kernel.h>
31#include <linux/console.h>
32#include <linux/reboot.h>
33#include <linux/keyboard.h>
34#include <linux/init.h>
35#include <linux/pm.h>
36#include <linux/bitops.h>
37#include <linux/delay.h>
38#include <linux/gfp.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
43#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
60#include "68328serial.h"
61
62/* Turn off usage of real serial interrupt code, to "support" Copilot */
63#ifdef CONFIG_XCOPILOT_BUGS
64#undef USE_INTS
65#else
66#define USE_INTS
67#endif
68
69static struct m68k_serial m68k_soft[NR_PORTS];
70
71static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72
73/* multiple ports are contiguous in memory */
74m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75
76struct tty_struct m68k_ttys;
77struct m68k_serial *m68k_consinfo = 0;
78
79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80
81struct tty_driver *serial_driver;
82
83/* number of characters left in xmit buffer before we ask for more */
84#define WAKEUP_CHARS 256
85
86/* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
88 */
89#undef SERIAL_DEBUG_INTR
90#undef SERIAL_DEBUG_OPEN
91#undef SERIAL_DEBUG_FLOW
92
93#define RS_ISR_PASS_LIMIT 256
94
95static void change_speed(struct m68k_serial *info);
96
97/*
98 * Setup for console. Argument comes from the boot command line.
99 */
100
101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102#ifdef CONFIG_M68VZ328
103#define CONSOLE_BAUD_RATE 19200
104#define DEFAULT_CBAUD B19200
105#endif
106
107
108#ifndef CONSOLE_BAUD_RATE
109#define CONSOLE_BAUD_RATE 9600
110#define DEFAULT_CBAUD B9600
111#endif
112
113
114static int m68328_console_initted = 0;
115static int m68328_console_baud = CONSOLE_BAUD_RATE;
116static int m68328_console_cbaud = DEFAULT_CBAUD;
117
118
119static inline int serial_paranoia_check(struct m68k_serial *info,
120 char *name, const char *routine)
121{
122#ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct %s in %s\n";
125 static const char *badinfo =
126 "Warning: null m68k_serial for %s in %s\n";
127
128 if (!info) {
129 printk(badinfo, name, routine);
130 return 1;
131 }
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
134 return 1;
135 }
136#endif
137 return 0;
138}
139
140/*
141 * This is used to figure out the divisor speeds and the timeouts
142 */
143static int baud_table[] = {
144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145 9600, 19200, 38400, 57600, 115200, 0 };
146
147/* Sets or clears DTR/RTS on the requested line */
148static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149{
150 if (set) {
151 /* set the RTS/CTS line */
152 } else {
153 /* clear it */
154 }
155 return;
156}
157
158/* Utility routines */
159static inline int get_baud(struct m68k_serial *ss)
160{
161 unsigned long result = 115200;
162 unsigned short int baud = uart_addr[ss->line].ubaud;
163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165
166 return result;
167}
168
169/*
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
172 *
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
176 */
177static void rs_stop(struct tty_struct *tty)
178{
179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180 m68328_uart *uart = &uart_addr[info->line];
181 unsigned long flags;
182
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
184 return;
185
186 local_irq_save(flags);
187 uart->ustcnt &= ~USTCNT_TXEN;
188 local_irq_restore(flags);
189}
190
191static int rs_put_char(char ch)
192{
193 int flags, loops = 0;
194
195 local_irq_save(flags);
196
197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
198 loops++;
199 udelay(5);
200 }
201
202 UTX_TXDATA = ch;
203 udelay(5);
204 local_irq_restore(flags);
205 return 1;
206}
207
208static void rs_start(struct tty_struct *tty)
209{
210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211 m68328_uart *uart = &uart_addr[info->line];
212 unsigned long flags;
213
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
216
217 local_irq_save(flags);
218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219#ifdef USE_INTS
220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221#else
222 uart->ustcnt |= USTCNT_TXEN;
223#endif
224 }
225 local_irq_restore(flags);
226}
227
228/* Drop into either the boot monitor or kadb upon receiving a break
229 * from keyboard/console input.
230 */
231static void batten_down_hatches(void)
232{
233 /* Drop into the debugger */
234}
235
236static void status_handle(struct m68k_serial *info, unsigned short status)
237{
238#if 0
239 if(status & DCD) {
240 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
241 ((info->curregs[3] & AUTO_ENAB)==0)) {
242 info->curregs[3] |= AUTO_ENAB;
243 info->pendregs[3] |= AUTO_ENAB;
244 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
245 }
246 } else {
247 if((info->curregs[3] & AUTO_ENAB)) {
248 info->curregs[3] &= ~AUTO_ENAB;
249 info->pendregs[3] &= ~AUTO_ENAB;
250 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
251 }
252 }
253#endif
254 /* If this is console input and this is a
255 * 'break asserted' status change interrupt
256 * see if we can drop into the debugger
257 */
258 if((status & URX_BREAK) && info->break_abort)
259 batten_down_hatches();
260 return;
261}
262
263static void receive_chars(struct m68k_serial *info, unsigned short rx)
264{
265 struct tty_struct *tty = info->tty;
266 m68328_uart *uart = &uart_addr[info->line];
267 unsigned char ch, flag;
268
269 /*
270 * This do { } while() loop will get ALL chars out of Rx FIFO
271 */
272#ifndef CONFIG_XCOPILOT_BUGS
273 do {
274#endif
275 ch = GET_FIELD(rx, URX_RXDATA);
276
277 if(info->is_cons) {
278 if(URX_BREAK & rx) { /* whee, break received */
279 status_handle(info, rx);
280 return;
281#ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch == 0x10) { /* ^P */
283 show_state();
284 show_free_areas(0);
285 show_buffers();
286/* show_net_buffers(); */
287 return;
288 } else if (ch == 0x12) { /* ^R */
289 emergency_restart();
290 return;
291#endif /* CONFIG_MAGIC_SYSRQ */
292 }
293 }
294
295 if(!tty)
296 goto clear_and_exit;
297
298 flag = TTY_NORMAL;
299
300 if(rx & URX_PARITY_ERROR) {
301 flag = TTY_PARITY;
302 status_handle(info, rx);
303 } else if(rx & URX_OVRUN) {
304 flag = TTY_OVERRUN;
305 status_handle(info, rx);
306 } else if(rx & URX_FRAME_ERROR) {
307 flag = TTY_FRAME;
308 status_handle(info, rx);
309 }
310 tty_insert_flip_char(tty, ch, flag);
311#ifndef CONFIG_XCOPILOT_BUGS
312 } while((rx = uart->urx.w) & URX_DATA_READY);
313#endif
314
315 tty_schedule_flip(tty);
316
317clear_and_exit:
318 return;
319}
320
321static void transmit_chars(struct m68k_serial *info)
322{
323 m68328_uart *uart = &uart_addr[info->line];
324
325 if (info->x_char) {
326 /* Send next char */
327 uart->utx.b.txdata = info->x_char;
328 info->x_char = 0;
329 goto clear_and_return;
330 }
331
332 if((info->xmit_cnt <= 0) || info->tty->stopped) {
333 /* That's peculiar... TX ints off */
334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335 goto clear_and_return;
336 }
337
338 /* Send char */
339 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
340 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
341 info->xmit_cnt--;
342
343 if (info->xmit_cnt < WAKEUP_CHARS)
344 schedule_work(&info->tqueue);
345
346 if(info->xmit_cnt <= 0) {
347 /* All done for now... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
350 }
351
352clear_and_return:
353 /* Clear interrupt (should be auto)*/
354 return;
355}
356
357/*
358 * This is the serial driver's generic interrupt routine
359 */
360irqreturn_t rs_interrupt(int irq, void *dev_id)
361{
362 struct m68k_serial *info = dev_id;
363 m68328_uart *uart;
364 unsigned short rx;
365 unsigned short tx;
366
367 uart = &uart_addr[info->line];
368 rx = uart->urx.w;
369
370#ifdef USE_INTS
371 tx = uart->utx.w;
372
373 if (rx & URX_DATA_READY) receive_chars(info, rx);
374 if (tx & UTX_TX_AVAIL) transmit_chars(info);
375#else
376 receive_chars(info, rx);
377#endif
378 return IRQ_HANDLED;
379}
380
381static void do_softint(struct work_struct *work)
382{
383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
384 struct tty_struct *tty;
385
386 tty = info->tty;
387 if (!tty)
388 return;
389#if 0
390 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
391 tty_wakeup(tty);
392 }
393#endif
394}
395
396static int startup(struct m68k_serial * info)
397{
398 m68328_uart *uart = &uart_addr[info->line];
399 unsigned long flags;
400
401 if (info->flags & S_INITIALIZED)
402 return 0;
403
404 if (!info->xmit_buf) {
405 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
406 if (!info->xmit_buf)
407 return -ENOMEM;
408 }
409
410 local_irq_save(flags);
411
412 /*
413 * Clear the FIFO buffers and disable them
414 * (they will be reenabled in change_speed())
415 */
416
417 uart->ustcnt = USTCNT_UEN;
418 info->xmit_fifo_size = 1;
419 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
420 (void)uart->urx.w;
421
422 /*
423 * Finally, enable sequencing and interrupts
424 */
425#ifdef USE_INTS
426 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
427 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
428#else
429 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
430#endif
431
432 if (info->tty)
433 clear_bit(TTY_IO_ERROR, &info->tty->flags);
434 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
435
436 /*
437 * and set the speed of the serial port
438 */
439
440 change_speed(info);
441
442 info->flags |= S_INITIALIZED;
443 local_irq_restore(flags);
444 return 0;
445}
446
447/*
448 * This routine will shutdown a serial port; interrupts are disabled, and
449 * DTR is dropped if the hangup on close termio flag is on.
450 */
451static void shutdown(struct m68k_serial * info)
452{
453 m68328_uart *uart = &uart_addr[info->line];
454 unsigned long flags;
455
456 uart->ustcnt = 0; /* All off! */
457 if (!(info->flags & S_INITIALIZED))
458 return;
459
460 local_irq_save(flags);
461
462 if (info->xmit_buf) {
463 free_page((unsigned long) info->xmit_buf);
464 info->xmit_buf = 0;
465 }
466
467 if (info->tty)
468 set_bit(TTY_IO_ERROR, &info->tty->flags);
469
470 info->flags &= ~S_INITIALIZED;
471 local_irq_restore(flags);
472}
473
474struct {
475 int divisor, prescale;
476}
477#ifndef CONFIG_M68VZ328
478 hw_baud_table[18] = {
479 {0,0}, /* 0 */
480 {0,0}, /* 50 */
481 {0,0}, /* 75 */
482 {0,0}, /* 110 */
483 {0,0}, /* 134 */
484 {0,0}, /* 150 */
485 {0,0}, /* 200 */
486 {7,0x26}, /* 300 */
487 {6,0x26}, /* 600 */
488 {5,0x26}, /* 1200 */
489 {0,0}, /* 1800 */
490 {4,0x26}, /* 2400 */
491 {3,0x26}, /* 4800 */
492 {2,0x26}, /* 9600 */
493 {1,0x26}, /* 19200 */
494 {0,0x26}, /* 38400 */
495 {1,0x38}, /* 57600 */
496 {0,0x38}, /* 115200 */
497};
498#else
499 hw_baud_table[18] = {
500 {0,0}, /* 0 */
501 {0,0}, /* 50 */
502 {0,0}, /* 75 */
503 {0,0}, /* 110 */
504 {0,0}, /* 134 */
505 {0,0}, /* 150 */
506 {0,0}, /* 200 */
507 {0,0}, /* 300 */
508 {7,0x26}, /* 600 */
509 {6,0x26}, /* 1200 */
510 {0,0}, /* 1800 */
511 {5,0x26}, /* 2400 */
512 {4,0x26}, /* 4800 */
513 {3,0x26}, /* 9600 */
514 {2,0x26}, /* 19200 */
515 {1,0x26}, /* 38400 */
516 {0,0x26}, /* 57600 */
517 {1,0x38}, /* 115200 */
518};
519#endif
520/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
521
522/*
523 * This routine is called to set the UART divisor registers to match
524 * the specified baud rate for a serial port.
525 */
526static void change_speed(struct m68k_serial *info)
527{
528 m68328_uart *uart = &uart_addr[info->line];
529 unsigned short port;
530 unsigned short ustcnt;
531 unsigned cflag;
532 int i;
533
534 if (!info->tty || !info->tty->termios)
535 return;
536 cflag = info->tty->termios->c_cflag;
537 if (!(port = info->port))
538 return;
539
540 ustcnt = uart->ustcnt;
541 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
542
543 i = cflag & CBAUD;
544 if (i & CBAUDEX) {
545 i = (i & ~CBAUDEX) + B38400;
546 }
547
548 info->baud = baud_table[i];
549 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
550 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
551
552 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
553
554 if ((cflag & CSIZE) == CS8)
555 ustcnt |= USTCNT_8_7;
556
557 if (cflag & CSTOPB)
558 ustcnt |= USTCNT_STOP;
559
560 if (cflag & PARENB)
561 ustcnt |= USTCNT_PARITYEN;
562 if (cflag & PARODD)
563 ustcnt |= USTCNT_ODD_EVEN;
564
565#ifdef CONFIG_SERIAL_68328_RTS_CTS
566 if (cflag & CRTSCTS) {
567 uart->utx.w &= ~ UTX_NOCTS;
568 } else {
569 uart->utx.w |= UTX_NOCTS;
570 }
571#endif
572
573 ustcnt |= USTCNT_TXEN;
574
575 uart->ustcnt = ustcnt;
576 return;
577}
578
579/*
580 * Fair output driver allows a process to speak.
581 */
582static void rs_fair_output(void)
583{
584 int left; /* Output no more than that */
585 unsigned long flags;
586 struct m68k_serial *info = &m68k_soft[0];
587 char c;
588
589 if (info == 0) return;
590 if (info->xmit_buf == 0) return;
591
592 local_irq_save(flags);
593 left = info->xmit_cnt;
594 while (left != 0) {
595 c = info->xmit_buf[info->xmit_tail];
596 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
597 info->xmit_cnt--;
598 local_irq_restore(flags);
599
600 rs_put_char(c);
601
602 local_irq_save(flags);
603 left = min(info->xmit_cnt, left-1);
604 }
605
606 /* Last character is being transmitted now (hopefully). */
607 udelay(5);
608
609 local_irq_restore(flags);
610 return;
611}
612
613/*
614 * m68k_console_print is registered for printk.
615 */
616void console_print_68328(const char *p)
617{
618 char c;
619
620 while((c=*(p++)) != 0) {
621 if(c == '\n')
622 rs_put_char('\r');
623 rs_put_char(c);
624 }
625
626 /* Comment this if you want to have a strict interrupt-driven output */
627 rs_fair_output();
628
629 return;
630}
631
632static void rs_set_ldisc(struct tty_struct *tty)
633{
634 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
635
636 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
637 return;
638
639 info->is_cons = (tty->termios->c_line == N_TTY);
640
641 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
642}
643
644static void rs_flush_chars(struct tty_struct *tty)
645{
646 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
647 m68328_uart *uart = &uart_addr[info->line];
648 unsigned long flags;
649
650 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
651 return;
652#ifndef USE_INTS
653 for(;;) {
654#endif
655
656 /* Enable transmitter */
657 local_irq_save(flags);
658
659 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
660 !info->xmit_buf) {
661 local_irq_restore(flags);
662 return;
663 }
664
665#ifdef USE_INTS
666 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
667#else
668 uart->ustcnt |= USTCNT_TXEN;
669#endif
670
671#ifdef USE_INTS
672 if (uart->utx.w & UTX_TX_AVAIL) {
673#else
674 if (1) {
675#endif
676 /* Send char */
677 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
678 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
679 info->xmit_cnt--;
680 }
681
682#ifndef USE_INTS
683 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
684 }
685#endif
686 local_irq_restore(flags);
687}
688
689extern void console_printn(const char * b, int count);
690
691static int rs_write(struct tty_struct * tty,
692 const unsigned char *buf, int count)
693{
694 int c, total = 0;
695 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
696 m68328_uart *uart = &uart_addr[info->line];
697 unsigned long flags;
698
699 if (serial_paranoia_check(info, tty->name, "rs_write"))
700 return 0;
701
702 if (!tty || !info->xmit_buf)
703 return 0;
704
705 local_save_flags(flags);
706 while (1) {
707 local_irq_disable();
708 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
709 SERIAL_XMIT_SIZE - info->xmit_head));
710 local_irq_restore(flags);
711
712 if (c <= 0)
713 break;
714
715 memcpy(info->xmit_buf + info->xmit_head, buf, c);
716
717 local_irq_disable();
718 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
719 info->xmit_cnt += c;
720 local_irq_restore(flags);
721 buf += c;
722 count -= c;
723 total += c;
724 }
725
726 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
727 /* Enable transmitter */
728 local_irq_disable();
729#ifndef USE_INTS
730 while(info->xmit_cnt) {
731#endif
732
733 uart->ustcnt |= USTCNT_TXEN;
734#ifdef USE_INTS
735 uart->ustcnt |= USTCNT_TX_INTR_MASK;
736#else
737 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
738#endif
739 if (uart->utx.w & UTX_TX_AVAIL) {
740 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
741 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
742 info->xmit_cnt--;
743 }
744
745#ifndef USE_INTS
746 }
747#endif
748 local_irq_restore(flags);
749 }
750
751 return total;
752}
753
754static int rs_write_room(struct tty_struct *tty)
755{
756 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
757 int ret;
758
759 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
760 return 0;
761 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
762 if (ret < 0)
763 ret = 0;
764 return ret;
765}
766
767static int rs_chars_in_buffer(struct tty_struct *tty)
768{
769 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
770
771 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
772 return 0;
773 return info->xmit_cnt;
774}
775
776static void rs_flush_buffer(struct tty_struct *tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 unsigned long flags;
780
781 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
782 return;
783 local_irq_save(flags);
784 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
785 local_irq_restore(flags);
786 tty_wakeup(tty);
787}
788
789/*
790 * ------------------------------------------------------------
791 * rs_throttle()
792 *
793 * This routine is called by the upper-layer tty layer to signal that
794 * incoming characters should be throttled.
795 * ------------------------------------------------------------
796 */
797static void rs_throttle(struct tty_struct * tty)
798{
799 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
800
801 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
802 return;
803
804 if (I_IXOFF(tty))
805 info->x_char = STOP_CHAR(tty);
806
807 /* Turn off RTS line (do this atomic) */
808}
809
810static void rs_unthrottle(struct tty_struct * tty)
811{
812 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
813
814 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
815 return;
816
817 if (I_IXOFF(tty)) {
818 if (info->x_char)
819 info->x_char = 0;
820 else
821 info->x_char = START_CHAR(tty);
822 }
823
824 /* Assert RTS line (do this atomic) */
825}
826
827/*
828 * ------------------------------------------------------------
829 * rs_ioctl() and friends
830 * ------------------------------------------------------------
831 */
832
833static int get_serial_info(struct m68k_serial * info,
834 struct serial_struct * retinfo)
835{
836 struct serial_struct tmp;
837
838 if (!retinfo)
839 return -EFAULT;
840 memset(&tmp, 0, sizeof(tmp));
841 tmp.type = info->type;
842 tmp.line = info->line;
843 tmp.port = info->port;
844 tmp.irq = info->irq;
845 tmp.flags = info->flags;
846 tmp.baud_base = info->baud_base;
847 tmp.close_delay = info->close_delay;
848 tmp.closing_wait = info->closing_wait;
849 tmp.custom_divisor = info->custom_divisor;
850 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
851 return -EFAULT;
852
853 return 0;
854}
855
856static int set_serial_info(struct m68k_serial * info,
857 struct serial_struct * new_info)
858{
859 struct serial_struct new_serial;
860 struct m68k_serial old_info;
861 int retval = 0;
862
863 if (!new_info)
864 return -EFAULT;
865 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
866 return -EFAULT;
867 old_info = *info;
868
869 if (!capable(CAP_SYS_ADMIN)) {
870 if ((new_serial.baud_base != info->baud_base) ||
871 (new_serial.type != info->type) ||
872 (new_serial.close_delay != info->close_delay) ||
873 ((new_serial.flags & ~S_USR_MASK) !=
874 (info->flags & ~S_USR_MASK)))
875 return -EPERM;
876 info->flags = ((info->flags & ~S_USR_MASK) |
877 (new_serial.flags & S_USR_MASK));
878 info->custom_divisor = new_serial.custom_divisor;
879 goto check_and_exit;
880 }
881
882 if (info->count > 1)
883 return -EBUSY;
884
885 /*
886 * OK, past this point, all the error checking has been done.
887 * At this point, we start making changes.....
888 */
889
890 info->baud_base = new_serial.baud_base;
891 info->flags = ((info->flags & ~S_FLAGS) |
892 (new_serial.flags & S_FLAGS));
893 info->type = new_serial.type;
894 info->close_delay = new_serial.close_delay;
895 info->closing_wait = new_serial.closing_wait;
896
897check_and_exit:
898 retval = startup(info);
899 return retval;
900}
901
902/*
903 * get_lsr_info - get line status register info
904 *
905 * Purpose: Let user call ioctl() to get info when the UART physically
906 * is emptied. On bus types like RS485, the transmitter must
907 * release the bus after transmitting. This must be done when
908 * the transmit shift register is empty, not be done when the
909 * transmit holding register is empty. This functionality
910 * allows an RS485 driver to be written in user space.
911 */
912static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
913{
914#ifdef CONFIG_SERIAL_68328_RTS_CTS
915 m68328_uart *uart = &uart_addr[info->line];
916#endif
917 unsigned char status;
918 unsigned long flags;
919
920 local_irq_save(flags);
921#ifdef CONFIG_SERIAL_68328_RTS_CTS
922 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
923#else
924 status = 0;
925#endif
926 local_irq_restore(flags);
927 return put_user(status, value);
928}
929
930/*
931 * This routine sends a break character out the serial port.
932 */
933static void send_break(struct m68k_serial * info, unsigned int duration)
934{
935 m68328_uart *uart = &uart_addr[info->line];
936 unsigned long flags;
937 if (!info->port)
938 return;
939 local_irq_save(flags);
940#ifdef USE_INTS
941 uart->utx.w |= UTX_SEND_BREAK;
942 msleep_interruptible(duration);
943 uart->utx.w &= ~UTX_SEND_BREAK;
944#endif
945 local_irq_restore(flags);
946}
947
948static int rs_ioctl(struct tty_struct *tty,
949 unsigned int cmd, unsigned long arg)
950{
951 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
952 int retval;
953
954 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
955 return -ENODEV;
956
957 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
958 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
959 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
960 if (tty->flags & (1 << TTY_IO_ERROR))
961 return -EIO;
962 }
963
964 switch (cmd) {
965 case TCSBRK: /* SVID version: non-zero arg --> no break */
966 retval = tty_check_change(tty);
967 if (retval)
968 return retval;
969 tty_wait_until_sent(tty, 0);
970 if (!arg)
971 send_break(info, 250); /* 1/4 second */
972 return 0;
973 case TCSBRKP: /* support for POSIX tcsendbreak() */
974 retval = tty_check_change(tty);
975 if (retval)
976 return retval;
977 tty_wait_until_sent(tty, 0);
978 send_break(info, arg ? arg*(100) : 250);
979 return 0;
980 case TIOCGSERIAL:
981 return get_serial_info(info,
982 (struct serial_struct *) arg);
983 case TIOCSSERIAL:
984 return set_serial_info(info,
985 (struct serial_struct *) arg);
986 case TIOCSERGETLSR: /* Get line status register */
987 return get_lsr_info(info, (unsigned int *) arg);
988 case TIOCSERGSTRUCT:
989 if (copy_to_user((struct m68k_serial *) arg,
990 info, sizeof(struct m68k_serial)))
991 return -EFAULT;
992 return 0;
993 default:
994 return -ENOIOCTLCMD;
995 }
996 return 0;
997}
998
999static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1000{
1001 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1002
1003 change_speed(info);
1004
1005 if ((old_termios->c_cflag & CRTSCTS) &&
1006 !(tty->termios->c_cflag & CRTSCTS)) {
1007 tty->hw_stopped = 0;
1008 rs_start(tty);
1009 }
1010
1011}
1012
1013/*
1014 * ------------------------------------------------------------
1015 * rs_close()
1016 *
1017 * This routine is called when the serial port gets closed. First, we
1018 * wait for the last remaining data to be sent. Then, we unlink its
1019 * S structure from the interrupt chain if necessary, and we free
1020 * that IRQ if nothing is left in the chain.
1021 * ------------------------------------------------------------
1022 */
1023static void rs_close(struct tty_struct *tty, struct file * filp)
1024{
1025 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1026 m68328_uart *uart = &uart_addr[info->line];
1027 unsigned long flags;
1028
1029 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1030 return;
1031
1032 local_irq_save(flags);
1033
1034 if (tty_hung_up_p(filp)) {
1035 local_irq_restore(flags);
1036 return;
1037 }
1038
1039 if ((tty->count == 1) && (info->count != 1)) {
1040 /*
1041 * Uh, oh. tty->count is 1, which means that the tty
1042 * structure will be freed. Info->count should always
1043 * be one in these conditions. If it's greater than
1044 * one, we've got real problems, since it means the
1045 * serial port won't be shutdown.
1046 */
1047 printk("rs_close: bad serial port count; tty->count is 1, "
1048 "info->count is %d\n", info->count);
1049 info->count = 1;
1050 }
1051 if (--info->count < 0) {
1052 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1053 info->line, info->count);
1054 info->count = 0;
1055 }
1056 if (info->count) {
1057 local_irq_restore(flags);
1058 return;
1059 }
1060 info->flags |= S_CLOSING;
1061 /*
1062 * Now we wait for the transmit buffer to clear; and we notify
1063 * the line discipline to only process XON/XOFF characters.
1064 */
1065 tty->closing = 1;
1066 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1067 tty_wait_until_sent(tty, info->closing_wait);
1068 /*
1069 * At this point we stop accepting input. To do this, we
1070 * disable the receive line status interrupts, and tell the
1071 * interrupt driver to stop checking the data ready bit in the
1072 * line status register.
1073 */
1074
1075 uart->ustcnt &= ~USTCNT_RXEN;
1076 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1077
1078 shutdown(info);
1079 rs_flush_buffer(tty);
1080
1081 tty_ldisc_flush(tty);
1082 tty->closing = 0;
1083 info->event = 0;
1084 info->tty = NULL;
1085#warning "This is not and has never been valid so fix it"
1086#if 0
1087 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1088 if (tty->ldisc.close)
1089 (tty->ldisc.close)(tty);
1090 tty->ldisc = ldiscs[N_TTY];
1091 tty->termios->c_line = N_TTY;
1092 if (tty->ldisc.open)
1093 (tty->ldisc.open)(tty);
1094 }
1095#endif
1096 if (info->blocked_open) {
1097 if (info->close_delay) {
1098 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1099 }
1100 wake_up_interruptible(&info->open_wait);
1101 }
1102 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1103 wake_up_interruptible(&info->close_wait);
1104 local_irq_restore(flags);
1105}
1106
1107/*
1108 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1109 */
1110void rs_hangup(struct tty_struct *tty)
1111{
1112 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1113
1114 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1115 return;
1116
1117 rs_flush_buffer(tty);
1118 shutdown(info);
1119 info->event = 0;
1120 info->count = 0;
1121 info->flags &= ~S_NORMAL_ACTIVE;
1122 info->tty = NULL;
1123 wake_up_interruptible(&info->open_wait);
1124}
1125
1126/*
1127 * ------------------------------------------------------------
1128 * rs_open() and friends
1129 * ------------------------------------------------------------
1130 */
1131static int block_til_ready(struct tty_struct *tty, struct file * filp,
1132 struct m68k_serial *info)
1133{
1134 DECLARE_WAITQUEUE(wait, current);
1135 int retval;
1136 int do_clocal = 0;
1137
1138 /*
1139 * If the device is in the middle of being closed, then block
1140 * until it's done, and then try again.
1141 */
1142 if (info->flags & S_CLOSING) {
1143 interruptible_sleep_on(&info->close_wait);
1144#ifdef SERIAL_DO_RESTART
1145 if (info->flags & S_HUP_NOTIFY)
1146 return -EAGAIN;
1147 else
1148 return -ERESTARTSYS;
1149#else
1150 return -EAGAIN;
1151#endif
1152 }
1153
1154 /*
1155 * If non-blocking mode is set, or the port is not enabled,
1156 * then make the check up front and then exit.
1157 */
1158 if ((filp->f_flags & O_NONBLOCK) ||
1159 (tty->flags & (1 << TTY_IO_ERROR))) {
1160 info->flags |= S_NORMAL_ACTIVE;
1161 return 0;
1162 }
1163
1164 if (tty->termios->c_cflag & CLOCAL)
1165 do_clocal = 1;
1166
1167 /*
1168 * Block waiting for the carrier detect and the line to become
1169 * free (i.e., not in use by the callout). While we are in
1170 * this loop, info->count is dropped by one, so that
1171 * rs_close() knows when to free things. We restore it upon
1172 * exit, either normal or abnormal.
1173 */
1174 retval = 0;
1175 add_wait_queue(&info->open_wait, &wait);
1176
1177 info->count--;
1178 info->blocked_open++;
1179 while (1) {
1180 local_irq_disable();
1181 m68k_rtsdtr(info, 1);
1182 local_irq_enable();
1183 current->state = TASK_INTERRUPTIBLE;
1184 if (tty_hung_up_p(filp) ||
1185 !(info->flags & S_INITIALIZED)) {
1186#ifdef SERIAL_DO_RESTART
1187 if (info->flags & S_HUP_NOTIFY)
1188 retval = -EAGAIN;
1189 else
1190 retval = -ERESTARTSYS;
1191#else
1192 retval = -EAGAIN;
1193#endif
1194 break;
1195 }
1196 if (!(info->flags & S_CLOSING) && do_clocal)
1197 break;
1198 if (signal_pending(current)) {
1199 retval = -ERESTARTSYS;
1200 break;
1201 }
1202 tty_unlock();
1203 schedule();
1204 tty_lock();
1205 }
1206 current->state = TASK_RUNNING;
1207 remove_wait_queue(&info->open_wait, &wait);
1208 if (!tty_hung_up_p(filp))
1209 info->count++;
1210 info->blocked_open--;
1211
1212 if (retval)
1213 return retval;
1214 info->flags |= S_NORMAL_ACTIVE;
1215 return 0;
1216}
1217
1218/*
1219 * This routine is called whenever a serial port is opened. It
1220 * enables interrupts for a serial port, linking in its S structure into
1221 * the IRQ chain. It also performs the serial-specific
1222 * initialization for the tty structure.
1223 */
1224int rs_open(struct tty_struct *tty, struct file * filp)
1225{
1226 struct m68k_serial *info;
1227 int retval, line;
1228
1229 line = tty->index;
1230
1231 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1232 return -ENODEV;
1233
1234 info = &m68k_soft[line];
1235
1236 if (serial_paranoia_check(info, tty->name, "rs_open"))
1237 return -ENODEV;
1238
1239 info->count++;
1240 tty->driver_data = info;
1241 info->tty = tty;
1242
1243 /*
1244 * Start up serial port
1245 */
1246 retval = startup(info);
1247 if (retval)
1248 return retval;
1249
1250 return block_til_ready(tty, filp, info);
1251}
1252
1253/* Finally, routines used to initialize the serial driver. */
1254
1255static void show_serial_version(void)
1256{
1257 printk("MC68328 serial driver version 1.00\n");
1258}
1259
1260static const struct tty_operations rs_ops = {
1261 .open = rs_open,
1262 .close = rs_close,
1263 .write = rs_write,
1264 .flush_chars = rs_flush_chars,
1265 .write_room = rs_write_room,
1266 .chars_in_buffer = rs_chars_in_buffer,
1267 .flush_buffer = rs_flush_buffer,
1268 .ioctl = rs_ioctl,
1269 .throttle = rs_throttle,
1270 .unthrottle = rs_unthrottle,
1271 .set_termios = rs_set_termios,
1272 .stop = rs_stop,
1273 .start = rs_start,
1274 .hangup = rs_hangup,
1275 .set_ldisc = rs_set_ldisc,
1276};
1277
1278/* rs_init inits the driver */
1279static int __init
1280rs68328_init(void)
1281{
1282 int flags, i;
1283 struct m68k_serial *info;
1284
1285 serial_driver = alloc_tty_driver(NR_PORTS);
1286 if (!serial_driver)
1287 return -ENOMEM;
1288
1289 show_serial_version();
1290
1291 /* Initialize the tty_driver structure */
1292 /* SPARC: Not all of this is exactly right for us. */
1293
1294 serial_driver->name = "ttyS";
1295 serial_driver->major = TTY_MAJOR;
1296 serial_driver->minor_start = 64;
1297 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1298 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1299 serial_driver->init_termios = tty_std_termios;
1300 serial_driver->init_termios.c_cflag =
1301 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1302 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1303 tty_set_operations(serial_driver, &rs_ops);
1304
1305 if (tty_register_driver(serial_driver)) {
1306 put_tty_driver(serial_driver);
1307 printk(KERN_ERR "Couldn't register serial driver\n");
1308 return -ENOMEM;
1309 }
1310
1311 local_irq_save(flags);
1312
1313 for(i=0;i<NR_PORTS;i++) {
1314
1315 info = &m68k_soft[i];
1316 info->magic = SERIAL_MAGIC;
1317 info->port = (int) &uart_addr[i];
1318 info->tty = NULL;
1319 info->irq = uart_irqs[i];
1320 info->custom_divisor = 16;
1321 info->close_delay = 50;
1322 info->closing_wait = 3000;
1323 info->x_char = 0;
1324 info->event = 0;
1325 info->count = 0;
1326 info->blocked_open = 0;
1327 INIT_WORK(&info->tqueue, do_softint);
1328 init_waitqueue_head(&info->open_wait);
1329 init_waitqueue_head(&info->close_wait);
1330 info->line = i;
1331 info->is_cons = 1; /* Means shortcuts work */
1332
1333 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1334 info->port, info->irq);
1335 printk(" is a builtin MC68328 UART\n");
1336
1337#ifdef CONFIG_M68VZ328
1338 if (i > 0 )
1339 PJSEL &= 0xCF; /* PSW enable second port output */
1340#endif
1341
1342 if (request_irq(uart_irqs[i],
1343 rs_interrupt,
1344 IRQF_DISABLED,
1345 "M68328_UART", info))
1346 panic("Unable to attach 68328 serial interrupt\n");
1347 }
1348 local_irq_restore(flags);
1349 return 0;
1350}
1351
1352module_init(rs68328_init);
1353
1354
1355
1356static void m68328_set_baud(void)
1357{
1358 unsigned short ustcnt;
1359 int i;
1360
1361 ustcnt = USTCNT;
1362 USTCNT = ustcnt & ~USTCNT_TXEN;
1363
1364again:
1365 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1366 if (baud_table[i] == m68328_console_baud)
1367 break;
1368 if (i >= ARRAY_SIZE(baud_table)) {
1369 m68328_console_baud = 9600;
1370 goto again;
1371 }
1372
1373 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1374 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1375 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1376 ustcnt |= USTCNT_8_7;
1377 ustcnt |= USTCNT_TXEN;
1378 USTCNT = ustcnt;
1379 m68328_console_initted = 1;
1380 return;
1381}
1382
1383
1384int m68328_console_setup(struct console *cp, char *arg)
1385{
1386 int i, n = CONSOLE_BAUD_RATE;
1387
1388 if (!cp)
1389 return(-1);
1390
1391 if (arg)
1392 n = simple_strtoul(arg,NULL,0);
1393
1394 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1395 if (baud_table[i] == n)
1396 break;
1397 if (i < ARRAY_SIZE(baud_table)) {
1398 m68328_console_baud = n;
1399 m68328_console_cbaud = 0;
1400 if (i > 15) {
1401 m68328_console_cbaud |= CBAUDEX;
1402 i -= 15;
1403 }
1404 m68328_console_cbaud |= i;
1405 }
1406
1407 m68328_set_baud(); /* make sure baud rate changes */
1408 return(0);
1409}
1410
1411
1412static struct tty_driver *m68328_console_device(struct console *c, int *index)
1413{
1414 *index = c->index;
1415 return serial_driver;
1416}
1417
1418
1419void m68328_console_write (struct console *co, const char *str,
1420 unsigned int count)
1421{
1422 if (!m68328_console_initted)
1423 m68328_set_baud();
1424 while (count--) {
1425 if (*str == '\n')
1426 rs_put_char('\r');
1427 rs_put_char( *str++ );
1428 }
1429}
1430
1431
1432static struct console m68328_driver = {
1433 .name = "ttyS",
1434 .write = m68328_console_write,
1435 .device = m68328_console_device,
1436 .setup = m68328_console_setup,
1437 .flags = CON_PRINTBUFFER,
1438 .index = -1,
1439};
1440
1441
1442static int __init m68328_console_init(void)
1443{
1444 register_console(&m68328_driver);
1445 return 0;
1446}
1447
1448console_initcall(m68328_console_init);
diff --git a/drivers/tty/serial/68328serial.h b/drivers/tty/serial/68328serial.h
new file mode 100644
index 000000000000..8c9c3c0745db
--- /dev/null
+++ b/drivers/tty/serial/68328serial.h
@@ -0,0 +1,187 @@
1/* 68328serial.h: Definitions for the mc68328 serial driver.
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 *
8 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
9 */
10
11#ifndef _MC683XX_SERIAL_H
12#define _MC683XX_SERIAL_H
13
14
15struct serial_struct {
16 int type;
17 int line;
18 int port;
19 int irq;
20 int flags;
21 int xmit_fifo_size;
22 int custom_divisor;
23 int baud_base;
24 unsigned short close_delay;
25 char reserved_char[2];
26 int hub6; /* FIXME: We don't have AT&T Hub6 boards! */
27 unsigned short closing_wait; /* time to wait before closing */
28 unsigned short closing_wait2; /* no longer used... */
29 int reserved[4];
30};
31
32/*
33 * For the close wait times, 0 means wait forever for serial port to
34 * flush its output. 65535 means don't wait at all.
35 */
36#define S_CLOSING_WAIT_INF 0
37#define S_CLOSING_WAIT_NONE 65535
38
39/*
40 * Definitions for S_struct (and serial_struct) flags field
41 */
42#define S_HUP_NOTIFY 0x0001 /* Notify getty on hangups and closes
43 on the callout port */
44#define S_FOURPORT 0x0002 /* Set OU1, OUT2 per AST Fourport settings */
45#define S_SAK 0x0004 /* Secure Attention Key (Orange book) */
46#define S_SPLIT_TERMIOS 0x0008 /* Separate termios for dialin/callout */
47
48#define S_SPD_MASK 0x0030
49#define S_SPD_HI 0x0010 /* Use 56000 instead of 38400 bps */
50
51#define S_SPD_VHI 0x0020 /* Use 115200 instead of 38400 bps */
52#define S_SPD_CUST 0x0030 /* Use user-specified divisor */
53
54#define S_SKIP_TEST 0x0040 /* Skip UART test during autoconfiguration */
55#define S_AUTO_IRQ 0x0080 /* Do automatic IRQ during autoconfiguration */
56#define S_SESSION_LOCKOUT 0x0100 /* Lock out cua opens based on session */
57#define S_PGRP_LOCKOUT 0x0200 /* Lock out cua opens based on pgrp */
58#define S_CALLOUT_NOHUP 0x0400 /* Don't do hangups for cua device */
59
60#define S_FLAGS 0x0FFF /* Possible legal S flags */
61#define S_USR_MASK 0x0430 /* Legal flags that non-privileged
62 * users can set or reset */
63
64/* Internal flags used only by kernel/chr_drv/serial.c */
65#define S_INITIALIZED 0x80000000 /* Serial port was initialized */
66#define S_CALLOUT_ACTIVE 0x40000000 /* Call out device is active */
67#define S_NORMAL_ACTIVE 0x20000000 /* Normal device is active */
68#define S_BOOT_AUTOCONF 0x10000000 /* Autoconfigure port on bootup */
69#define S_CLOSING 0x08000000 /* Serial port is closing */
70#define S_CTS_FLOW 0x04000000 /* Do CTS flow control */
71#define S_CHECK_CD 0x02000000 /* i.e., CLOCAL */
72
73/* Software state per channel */
74
75#ifdef __KERNEL__
76
77/*
78 * I believe this is the optimal setting that reduces the number of interrupts.
79 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
80 * if that bothers you), but in most cases it will not, since we try to
81 * transmit characters every time rs_interrupt is called. Thus, quite often
82 * you'll see that a receive interrupt occures before the transmit one.
83 * -- Vladimir Gurevich
84 */
85#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
86
87/*
88 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
89 * "Old data interrupt" which occures whenever the data stay in the FIFO
90 * longer than 30 bits time. This allows us to use FIFO without compromising
91 * latency. '328 does not have this feature and without the real 328-based
92 * board I would assume that RXRE is the safest setting.
93 *
94 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
95 * interrupts. RXFE (receive queue full) causes the system to lose data
96 * at least at 115200 baud
97 *
98 * If your board is busy doing other stuff, you might consider to use
99 * RXRE (data ready intrrupt) instead.
100 *
101 * The other option is to make these INTR masks run-time configurable, so
102 * that people can dynamically adapt them according to the current usage.
103 * -- Vladimir Gurevich
104 */
105
106/* (es) */
107#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
108#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
109#elif defined(CONFIG_M68328)
110#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
111#else
112#error Please, define the Rx interrupt events for your CPU
113#endif
114/* (/es) */
115
116/*
117 * This is our internal structure for each serial port's state.
118 *
119 * Many fields are paralleled by the structure used by the serial_struct
120 * structure.
121 *
122 * For definitions of the flags field, see tty.h
123 */
124
125struct m68k_serial {
126 char soft_carrier; /* Use soft carrier on this channel */
127 char break_abort; /* Is serial console in, so process brk/abrt */
128 char is_cons; /* Is this our console. */
129
130 /* We need to know the current clock divisor
131 * to read the bps rate the chip has currently
132 * loaded.
133 */
134 unsigned char clk_divisor; /* May be 1, 16, 32, or 64 */
135 int baud;
136 int magic;
137 int baud_base;
138 int port;
139 int irq;
140 int flags; /* defined in tty.h */
141 int type; /* UART type */
142 struct tty_struct *tty;
143 int read_status_mask;
144 int ignore_status_mask;
145 int timeout;
146 int xmit_fifo_size;
147 int custom_divisor;
148 int x_char; /* xon/xoff character */
149 int close_delay;
150 unsigned short closing_wait;
151 unsigned short closing_wait2;
152 unsigned long event;
153 unsigned long last_active;
154 int line;
155 int count; /* # of fd on device */
156 int blocked_open; /* # of blocked opens */
157 unsigned char *xmit_buf;
158 int xmit_head;
159 int xmit_tail;
160 int xmit_cnt;
161 struct work_struct tqueue;
162 wait_queue_head_t open_wait;
163 wait_queue_head_t close_wait;
164};
165
166
167#define SERIAL_MAGIC 0x5301
168
169/*
170 * The size of the serial xmit buffer is 1 page, or 4096 bytes
171 */
172#define SERIAL_XMIT_SIZE 4096
173
174/*
175 * Events are used to schedule things to happen at timer-interrupt
176 * time, instead of at rs interrupt time.
177 */
178#define RS_EVENT_WRITE_WAKEUP 0
179
180/*
181 * Define the number of ports supported and their irqs.
182 */
183#define NR_PORTS 1
184#define UART_IRQ_DEFNS {UART_IRQ_NUM}
185
186#endif /* __KERNEL__ */
187#endif /* !(_MC683XX_SERIAL_H) */
diff --git a/drivers/tty/serial/68360serial.c b/drivers/tty/serial/68360serial.c
new file mode 100644
index 000000000000..0a3e8787ed50
--- /dev/null
+++ b/drivers/tty/serial/68360serial.c
@@ -0,0 +1,2979 @@
1/*
2 * UART driver for 68360 CPM SCC or SMC
3 * Copyright (c) 2000 D. Jeff Dionne <jeff@uclinux.org>,
4 * Copyright (c) 2000 Michael Leslie <mleslie@lineo.ca>
5 * Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
6 *
7 * I used the serial.c driver as the framework for this driver.
8 * Give credit to those guys.
9 * The original code was written for the MBX860 board. I tried to make
10 * it generic, but there may be some assumptions in the structures that
11 * have to be fixed later.
12 * To save porting time, I did not bother to change any object names
13 * that are not accessed outside of this file.
14 * It still needs lots of work........When it was easy, I included code
15 * to support the SCCs, but this has never been tested, nor is it complete.
16 * Only the SCCs support modem control, so that is not complete either.
17 *
18 * This module exports the following rs232 io functions:
19 *
20 * int rs_360_init(void);
21 */
22
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serialP.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/mm.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <asm/irq.h>
41#include <asm/m68360.h>
42#include <asm/commproc.h>
43
44
45#ifdef CONFIG_KGDB
46extern void breakpoint(void);
47extern void set_debug_traps(void);
48extern int kgdb_output_string (const char* s, unsigned int count);
49#endif
50
51
52/* #ifdef CONFIG_SERIAL_CONSOLE */ /* This seems to be a post 2.0 thing - mles */
53#include <linux/console.h>
54#include <linux/jiffies.h>
55
56/* this defines the index into rs_table for the port to use
57 */
58#ifndef CONFIG_SERIAL_CONSOLE_PORT
59#define CONFIG_SERIAL_CONSOLE_PORT 1 /* ie SMC2 - note USE_SMC2 must be defined */
60#endif
61/* #endif */
62
63#if 0
64/* SCC2 for console
65 */
66#undef CONFIG_SERIAL_CONSOLE_PORT
67#define CONFIG_SERIAL_CONSOLE_PORT 2
68#endif
69
70
71#define TX_WAKEUP ASYNC_SHARE_IRQ
72
73static char *serial_name = "CPM UART driver";
74static char *serial_version = "0.03";
75
76static struct tty_driver *serial_driver;
77int serial_console_setup(struct console *co, char *options);
78
79/*
80 * Serial driver configuration section. Here are the various options:
81 */
82#define SERIAL_PARANOIA_CHECK
83#define CONFIG_SERIAL_NOPAUSE_IO
84#define SERIAL_DO_RESTART
85
86/* Set of debugging defines */
87
88#undef SERIAL_DEBUG_INTR
89#undef SERIAL_DEBUG_OPEN
90#undef SERIAL_DEBUG_FLOW
91#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
92
93#define _INLINE_ inline
94
95#define DBG_CNT(s)
96
97/* We overload some of the items in the data structure to meet our
98 * needs. For example, the port address is the CPM parameter ram
99 * offset for the SCC or SMC. The maximum number of ports is 4 SCCs and
100 * 2 SMCs. The "hub6" field is used to indicate the channel number, with
101 * a flag indicating SCC or SMC, and the number is used as an index into
102 * the CPM parameter area for this device.
103 * The "type" field is currently set to 0, for PORT_UNKNOWN. It is
104 * not currently used. I should probably use it to indicate the port
105 * type of SMC or SCC.
106 * The SMCs do not support any modem control signals.
107 */
108#define smc_scc_num hub6
109#define NUM_IS_SCC ((int)0x00010000)
110#define PORT_NUM(P) ((P) & 0x0000ffff)
111
112
113#if defined (CONFIG_UCQUICC)
114
115volatile extern void *_periph_base;
116/* sipex transceiver
117 * mode bits for are on pins
118 *
119 * SCC2 d16..19
120 * SCC3 d20..23
121 * SCC4 d24..27
122 */
123#define SIPEX_MODE(n,m) ((m & 0x0f)<<(16+4*(n-1)))
124
125static uint sipex_mode_bits = 0x00000000;
126
127#endif
128
129/* There is no `serial_state' defined back here in 2.0.
130 * Try to get by with serial_struct
131 */
132/* #define serial_state serial_struct */
133
134/* 2.4 -> 2.0 portability problem: async_icount in 2.4 has a few
135 * extras: */
136
137#if 0
138struct async_icount_24 {
139 __u32 cts, dsr, rng, dcd, tx, rx;
140 __u32 frame, parity, overrun, brk;
141 __u32 buf_overrun;
142} icount;
143#endif
144
145#if 0
146
147struct serial_state {
148 int magic;
149 int baud_base;
150 unsigned long port;
151 int irq;
152 int flags;
153 int hub6;
154 int type;
155 int line;
156 int revision; /* Chip revision (950) */
157 int xmit_fifo_size;
158 int custom_divisor;
159 int count;
160 u8 *iomem_base;
161 u16 iomem_reg_shift;
162 unsigned short close_delay;
163 unsigned short closing_wait; /* time to wait before closing */
164 struct async_icount_24 icount;
165 int io_type;
166 struct async_struct *info;
167};
168#endif
169
170#define SSTATE_MAGIC 0x5302
171
172
173
174/* SMC2 is sometimes used for low performance TDM interfaces. Define
175 * this as 1 if you want SMC2 as a serial port UART managed by this driver.
176 * Define this as 0 if you wish to use SMC2 for something else.
177 */
178#define USE_SMC2 1
179
180#if 0
181/* Define SCC to ttySx mapping. */
182#define SCC_NUM_BASE (USE_SMC2 + 1) /* SCC base tty "number" */
183
184/* Define which SCC is the first one to use for a serial port. These
185 * are 0-based numbers, i.e. this assumes the first SCC (SCC1) is used
186 * for Ethernet, and the first available SCC for serial UART is SCC2.
187 * NOTE: IF YOU CHANGE THIS, you have to change the PROFF_xxx and
188 * interrupt vectors in the table below to match.
189 */
190#define SCC_IDX_BASE 1 /* table index */
191#endif
192
193
194/* Processors other than the 860 only get SMCs configured by default.
195 * Either they don't have SCCs or they are allocated somewhere else.
196 * Of course, there are now 860s without some SCCs, so we will need to
197 * address that someday.
198 * The Embedded Planet Multimedia I/O cards use TDM interfaces to the
199 * stereo codec parts, and we use SMC2 to help support that.
200 */
201static struct serial_state rs_table[] = {
202/* type line PORT IRQ FLAGS smc_scc_num (F.K.A. hub6) */
203 { 0, 0, PRSLOT_SMC1, CPMVEC_SMC1, 0, 0 } /* SMC1 ttyS0 */
204#if USE_SMC2
205 ,{ 0, 0, PRSLOT_SMC2, CPMVEC_SMC2, 0, 1 } /* SMC2 ttyS1 */
206#endif
207
208#if defined(CONFIG_SERIAL_68360_SCC)
209 ,{ 0, 0, PRSLOT_SCC2, CPMVEC_SCC2, 0, (NUM_IS_SCC | 1) } /* SCC2 ttyS2 */
210 ,{ 0, 0, PRSLOT_SCC3, CPMVEC_SCC3, 0, (NUM_IS_SCC | 2) } /* SCC3 ttyS3 */
211 ,{ 0, 0, PRSLOT_SCC4, CPMVEC_SCC4, 0, (NUM_IS_SCC | 3) } /* SCC4 ttyS4 */
212#endif
213};
214
215#define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
216
217/* The number of buffer descriptors and their sizes.
218 */
219#define RX_NUM_FIFO 4
220#define RX_BUF_SIZE 32
221#define TX_NUM_FIFO 4
222#define TX_BUF_SIZE 32
223
224#define CONSOLE_NUM_FIFO 2
225#define CONSOLE_BUF_SIZE 4
226
227char *console_fifos[CONSOLE_NUM_FIFO * CONSOLE_BUF_SIZE];
228
229/* The async_struct in serial.h does not really give us what we
230 * need, so define our own here.
231 */
232typedef struct serial_info {
233 int magic;
234 int flags;
235
236 struct serial_state *state;
237 /* struct serial_struct *state; */
238 /* struct async_struct *state; */
239
240 struct tty_struct *tty;
241 int read_status_mask;
242 int ignore_status_mask;
243 int timeout;
244 int line;
245 int x_char; /* xon/xoff character */
246 int close_delay;
247 unsigned short closing_wait;
248 unsigned short closing_wait2;
249 unsigned long event;
250 unsigned long last_active;
251 int blocked_open; /* # of blocked opens */
252 struct work_struct tqueue;
253 struct work_struct tqueue_hangup;
254 wait_queue_head_t open_wait;
255 wait_queue_head_t close_wait;
256
257
258/* CPM Buffer Descriptor pointers.
259 */
260 QUICC_BD *rx_bd_base;
261 QUICC_BD *rx_cur;
262 QUICC_BD *tx_bd_base;
263 QUICC_BD *tx_cur;
264} ser_info_t;
265
266
267/* since kmalloc_init() does not get called until much after this initialization: */
268static ser_info_t quicc_ser_info[NR_PORTS];
269static char rx_buf_pool[NR_PORTS * RX_NUM_FIFO * RX_BUF_SIZE];
270static char tx_buf_pool[NR_PORTS * TX_NUM_FIFO * TX_BUF_SIZE];
271
272static void change_speed(ser_info_t *info);
273static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout);
274
275static inline int serial_paranoia_check(ser_info_t *info,
276 char *name, const char *routine)
277{
278#ifdef SERIAL_PARANOIA_CHECK
279 static const char *badmagic =
280 "Warning: bad magic number for serial struct (%s) in %s\n";
281 static const char *badinfo =
282 "Warning: null async_struct for (%s) in %s\n";
283
284 if (!info) {
285 printk(badinfo, name, routine);
286 return 1;
287 }
288 if (info->magic != SERIAL_MAGIC) {
289 printk(badmagic, name, routine);
290 return 1;
291 }
292#endif
293 return 0;
294}
295
296/*
297 * This is used to figure out the divisor speeds and the timeouts,
298 * indexed by the termio value. The generic CPM functions are responsible
299 * for setting and assigning baud rate generators for us.
300 */
301static int baud_table[] = {
302 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
303 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
304
305/* This sucks. There is a better way: */
306#if defined(CONFIG_CONSOLE_9600)
307 #define CONSOLE_BAUDRATE 9600
308#elif defined(CONFIG_CONSOLE_19200)
309 #define CONSOLE_BAUDRATE 19200
310#elif defined(CONFIG_CONSOLE_115200)
311 #define CONSOLE_BAUDRATE 115200
312#else
313 #warning "console baud rate undefined"
314 #define CONSOLE_BAUDRATE 9600
315#endif
316
317/*
318 * ------------------------------------------------------------
319 * rs_stop() and rs_start()
320 *
321 * This routines are called before setting or resetting tty->stopped.
322 * They enable or disable transmitter interrupts, as necessary.
323 * ------------------------------------------------------------
324 */
325static void rs_360_stop(struct tty_struct *tty)
326{
327 ser_info_t *info = (ser_info_t *)tty->driver_data;
328 int idx;
329 unsigned long flags;
330 volatile struct scc_regs *sccp;
331 volatile struct smc_regs *smcp;
332
333 if (serial_paranoia_check(info, tty->name, "rs_stop"))
334 return;
335
336 local_irq_save(flags);
337 idx = PORT_NUM(info->state->smc_scc_num);
338 if (info->state->smc_scc_num & NUM_IS_SCC) {
339 sccp = &pquicc->scc_regs[idx];
340 sccp->scc_sccm &= ~UART_SCCM_TX;
341 } else {
342 /* smcp = &cpmp->cp_smc[idx]; */
343 smcp = &pquicc->smc_regs[idx];
344 smcp->smc_smcm &= ~SMCM_TX;
345 }
346 local_irq_restore(flags);
347}
348
349
350static void rs_360_start(struct tty_struct *tty)
351{
352 ser_info_t *info = (ser_info_t *)tty->driver_data;
353 int idx;
354 unsigned long flags;
355 volatile struct scc_regs *sccp;
356 volatile struct smc_regs *smcp;
357
358 if (serial_paranoia_check(info, tty->name, "rs_stop"))
359 return;
360
361 local_irq_save(flags);
362 idx = PORT_NUM(info->state->smc_scc_num);
363 if (info->state->smc_scc_num & NUM_IS_SCC) {
364 sccp = &pquicc->scc_regs[idx];
365 sccp->scc_sccm |= UART_SCCM_TX;
366 } else {
367 smcp = &pquicc->smc_regs[idx];
368 smcp->smc_smcm |= SMCM_TX;
369 }
370 local_irq_restore(flags);
371}
372
373/*
374 * ----------------------------------------------------------------------
375 *
376 * Here starts the interrupt handling routines. All of the following
377 * subroutines are declared as inline and are folded into
378 * rs_interrupt(). They were separated out for readability's sake.
379 *
380 * Note: rs_interrupt() is a "fast" interrupt, which means that it
381 * runs with interrupts turned off. People who may want to modify
382 * rs_interrupt() should try to keep the interrupt handler as fast as
383 * possible. After you are done making modifications, it is not a bad
384 * idea to do:
385 *
386 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
387 *
388 * and look at the resulting assemble code in serial.s.
389 *
390 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
391 * -----------------------------------------------------------------------
392 */
393
394static _INLINE_ void receive_chars(ser_info_t *info)
395{
396 struct tty_struct *tty = info->port.tty;
397 unsigned char ch, flag, *cp;
398 /*int ignored = 0;*/
399 int i;
400 ushort status;
401 struct async_icount *icount;
402 /* struct async_icount_24 *icount; */
403 volatile QUICC_BD *bdp;
404
405 icount = &info->state->icount;
406
407 /* Just loop through the closed BDs and copy the characters into
408 * the buffer.
409 */
410 bdp = info->rx_cur;
411 for (;;) {
412 if (bdp->status & BD_SC_EMPTY) /* If this one is empty */
413 break; /* we are all done */
414
415 /* The read status mask tell us what we should do with
416 * incoming characters, especially if errors occur.
417 * One special case is the use of BD_SC_EMPTY. If
418 * this is not set, we are supposed to be ignoring
419 * inputs. In this case, just mark the buffer empty and
420 * continue.
421 */
422 if (!(info->read_status_mask & BD_SC_EMPTY)) {
423 bdp->status |= BD_SC_EMPTY;
424 bdp->status &=
425 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
426
427 if (bdp->status & BD_SC_WRAP)
428 bdp = info->rx_bd_base;
429 else
430 bdp++;
431 continue;
432 }
433
434 /* Get the number of characters and the buffer pointer.
435 */
436 i = bdp->length;
437 /* cp = (unsigned char *)__va(bdp->buf); */
438 cp = (char *)bdp->buf;
439 status = bdp->status;
440
441 while (i-- > 0) {
442 ch = *cp++;
443 icount->rx++;
444
445#ifdef SERIAL_DEBUG_INTR
446 printk("DR%02x:%02x...", ch, status);
447#endif
448 flag = TTY_NORMAL;
449
450 if (status & (BD_SC_BR | BD_SC_FR |
451 BD_SC_PR | BD_SC_OV)) {
452 /*
453 * For statistics only
454 */
455 if (status & BD_SC_BR)
456 icount->brk++;
457 else if (status & BD_SC_PR)
458 icount->parity++;
459 else if (status & BD_SC_FR)
460 icount->frame++;
461 if (status & BD_SC_OV)
462 icount->overrun++;
463
464 /*
465 * Now check to see if character should be
466 * ignored, and mask off conditions which
467 * should be ignored.
468 if (status & info->ignore_status_mask) {
469 if (++ignored > 100)
470 break;
471 continue;
472 }
473 */
474 status &= info->read_status_mask;
475
476 if (status & (BD_SC_BR)) {
477#ifdef SERIAL_DEBUG_INTR
478 printk("handling break....");
479#endif
480 *tty->flip.flag_buf_ptr = TTY_BREAK;
481 if (info->flags & ASYNC_SAK)
482 do_SAK(tty);
483 } else if (status & BD_SC_PR)
484 flag = TTY_PARITY;
485 else if (status & BD_SC_FR)
486 flag = TTY_FRAME;
487 }
488 tty_insert_flip_char(tty, ch, flag);
489 if (status & BD_SC_OV)
490 /*
491 * Overrun is special, since it's
492 * reported immediately, and doesn't
493 * affect the current character
494 */
495 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
496 }
497
498 /* This BD is ready to be used again. Clear status.
499 * Get next BD.
500 */
501 bdp->status |= BD_SC_EMPTY;
502 bdp->status &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
503
504 if (bdp->status & BD_SC_WRAP)
505 bdp = info->rx_bd_base;
506 else
507 bdp++;
508 }
509
510 info->rx_cur = (QUICC_BD *)bdp;
511
512 tty_schedule_flip(tty);
513}
514
515static _INLINE_ void receive_break(ser_info_t *info)
516{
517 struct tty_struct *tty = info->port.tty;
518
519 info->state->icount.brk++;
520 /* Check to see if there is room in the tty buffer for
521 * the break. If not, we exit now, losing the break. FIXME
522 */
523 tty_insert_flip_char(tty, 0, TTY_BREAK);
524 tty_schedule_flip(tty);
525}
526
527static _INLINE_ void transmit_chars(ser_info_t *info)
528{
529
530 if ((info->flags & TX_WAKEUP) ||
531 (info->port.tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
532 schedule_work(&info->tqueue);
533 }
534
535#ifdef SERIAL_DEBUG_INTR
536 printk("THRE...");
537#endif
538}
539
540#ifdef notdef
541 /* I need to do this for the SCCs, so it is left as a reminder.
542 */
543static _INLINE_ void check_modem_status(struct async_struct *info)
544{
545 int status;
546 /* struct async_icount *icount; */
547 struct async_icount_24 *icount;
548
549 status = serial_in(info, UART_MSR);
550
551 if (status & UART_MSR_ANY_DELTA) {
552 icount = &info->state->icount;
553 /* update input line counters */
554 if (status & UART_MSR_TERI)
555 icount->rng++;
556 if (status & UART_MSR_DDSR)
557 icount->dsr++;
558 if (status & UART_MSR_DDCD) {
559 icount->dcd++;
560#ifdef CONFIG_HARD_PPS
561 if ((info->flags & ASYNC_HARDPPS_CD) &&
562 (status & UART_MSR_DCD))
563 hardpps();
564#endif
565 }
566 if (status & UART_MSR_DCTS)
567 icount->cts++;
568 wake_up_interruptible(&info->delta_msr_wait);
569 }
570
571 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
572#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
573 printk("ttys%d CD now %s...", info->line,
574 (status & UART_MSR_DCD) ? "on" : "off");
575#endif
576 if (status & UART_MSR_DCD)
577 wake_up_interruptible(&info->open_wait);
578 else {
579#ifdef SERIAL_DEBUG_OPEN
580 printk("scheduling hangup...");
581#endif
582 queue_task(&info->tqueue_hangup,
583 &tq_scheduler);
584 }
585 }
586 if (info->flags & ASYNC_CTS_FLOW) {
587 if (info->port.tty->hw_stopped) {
588 if (status & UART_MSR_CTS) {
589#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
590 printk("CTS tx start...");
591#endif
592 info->port.tty->hw_stopped = 0;
593 info->IER |= UART_IER_THRI;
594 serial_out(info, UART_IER, info->IER);
595 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
596 return;
597 }
598 } else {
599 if (!(status & UART_MSR_CTS)) {
600#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
601 printk("CTS tx stop...");
602#endif
603 info->port.tty->hw_stopped = 1;
604 info->IER &= ~UART_IER_THRI;
605 serial_out(info, UART_IER, info->IER);
606 }
607 }
608 }
609}
610#endif
611
612/*
613 * This is the serial driver's interrupt routine for a single port
614 */
615/* static void rs_360_interrupt(void *dev_id) */ /* until and if we start servicing irqs here */
616static void rs_360_interrupt(int vec, void *dev_id)
617{
618 u_char events;
619 int idx;
620 ser_info_t *info;
621 volatile struct smc_regs *smcp;
622 volatile struct scc_regs *sccp;
623
624 info = dev_id;
625
626 idx = PORT_NUM(info->state->smc_scc_num);
627 if (info->state->smc_scc_num & NUM_IS_SCC) {
628 sccp = &pquicc->scc_regs[idx];
629 events = sccp->scc_scce;
630 if (events & SCCM_RX)
631 receive_chars(info);
632 if (events & SCCM_TX)
633 transmit_chars(info);
634 sccp->scc_scce = events;
635 } else {
636 smcp = &pquicc->smc_regs[idx];
637 events = smcp->smc_smce;
638 if (events & SMCM_BRKE)
639 receive_break(info);
640 if (events & SMCM_RX)
641 receive_chars(info);
642 if (events & SMCM_TX)
643 transmit_chars(info);
644 smcp->smc_smce = events;
645 }
646
647#ifdef SERIAL_DEBUG_INTR
648 printk("rs_interrupt_single(%d, %x)...",
649 info->state->smc_scc_num, events);
650#endif
651#ifdef modem_control
652 check_modem_status(info);
653#endif
654 info->last_active = jiffies;
655#ifdef SERIAL_DEBUG_INTR
656 printk("end.\n");
657#endif
658}
659
660
661/*
662 * -------------------------------------------------------------------
663 * Here ends the serial interrupt routines.
664 * -------------------------------------------------------------------
665 */
666
667
668static void do_softint(void *private_)
669{
670 ser_info_t *info = (ser_info_t *) private_;
671 struct tty_struct *tty;
672
673 tty = info->port.tty;
674 if (!tty)
675 return;
676
677 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
678 tty_wakeup(tty);
679}
680
681
682/*
683 * This routine is called from the scheduler tqueue when the interrupt
684 * routine has signalled that a hangup has occurred. The path of
685 * hangup processing is:
686 *
687 * serial interrupt routine -> (scheduler tqueue) ->
688 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
689 *
690 */
691static void do_serial_hangup(void *private_)
692{
693 struct async_struct *info = (struct async_struct *) private_;
694 struct tty_struct *tty;
695
696 tty = info->port.tty;
697 if (!tty)
698 return;
699
700 tty_hangup(tty);
701}
702
703
704static int startup(ser_info_t *info)
705{
706 unsigned long flags;
707 int retval=0;
708 int idx;
709 /*struct serial_state *state = info->state;*/
710 volatile struct smc_regs *smcp;
711 volatile struct scc_regs *sccp;
712 volatile struct smc_uart_pram *up;
713 volatile struct uart_pram *scup;
714
715
716 local_irq_save(flags);
717
718 if (info->flags & ASYNC_INITIALIZED) {
719 goto errout;
720 }
721
722#ifdef maybe
723 if (!state->port || !state->type) {
724 if (info->port.tty)
725 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
726 goto errout;
727 }
728#endif
729
730#ifdef SERIAL_DEBUG_OPEN
731 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
732#endif
733
734
735#ifdef modem_control
736 info->MCR = 0;
737 if (info->port.tty->termios->c_cflag & CBAUD)
738 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
739#endif
740
741 if (info->port.tty)
742 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
743
744 /*
745 * and set the speed of the serial port
746 */
747 change_speed(info);
748
749 idx = PORT_NUM(info->state->smc_scc_num);
750 if (info->state->smc_scc_num & NUM_IS_SCC) {
751 sccp = &pquicc->scc_regs[idx];
752 scup = &pquicc->pram[info->state->port].scc.pscc.u;
753
754 scup->mrblr = RX_BUF_SIZE;
755 scup->max_idl = RX_BUF_SIZE;
756
757 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
758 sccp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
759
760 } else {
761 smcp = &pquicc->smc_regs[idx];
762
763 /* Enable interrupts and I/O.
764 */
765 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
766 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
767
768 /* We can tune the buffer length and idle characters
769 * to take advantage of the entire incoming buffer size.
770 * If mrblr is something other than 1, maxidl has to be
771 * non-zero or we never get an interrupt. The maxidl
772 * is the number of character times we wait after reception
773 * of the last character before we decide no more characters
774 * are coming.
775 */
776 /* up = (smc_uart_t *)&pquicc->cp_dparam[state->port]; */
777 /* holy unionized structures, Batman: */
778 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
779
780 up->mrblr = RX_BUF_SIZE;
781 up->max_idl = RX_BUF_SIZE;
782
783 up->brkcr = 1; /* number of break chars */
784 }
785
786 info->flags |= ASYNC_INITIALIZED;
787 local_irq_restore(flags);
788 return 0;
789
790errout:
791 local_irq_restore(flags);
792 return retval;
793}
794
795/*
796 * This routine will shutdown a serial port; interrupts are disabled, and
797 * DTR is dropped if the hangup on close termio flag is on.
798 */
799static void shutdown(ser_info_t *info)
800{
801 unsigned long flags;
802 struct serial_state *state;
803 int idx;
804 volatile struct smc_regs *smcp;
805 volatile struct scc_regs *sccp;
806
807 if (!(info->flags & ASYNC_INITIALIZED))
808 return;
809
810 state = info->state;
811
812#ifdef SERIAL_DEBUG_OPEN
813 printk("Shutting down serial port %d (irq %d)....", info->line,
814 state->irq);
815#endif
816
817 local_irq_save(flags);
818
819 idx = PORT_NUM(state->smc_scc_num);
820 if (state->smc_scc_num & NUM_IS_SCC) {
821 sccp = &pquicc->scc_regs[idx];
822 sccp->scc_gsmr.w.low &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
823#ifdef CONFIG_SERIAL_CONSOLE
824 /* We can't disable the transmitter if this is the
825 * system console.
826 */
827 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
828#endif
829 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
830 } else {
831 smcp = &pquicc->smc_regs[idx];
832
833 /* Disable interrupts and I/O.
834 */
835 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
836#ifdef CONFIG_SERIAL_CONSOLE
837 /* We can't disable the transmitter if this is the
838 * system console.
839 */
840 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
841#endif
842 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
843 }
844
845 if (info->port.tty)
846 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
847
848 info->flags &= ~ASYNC_INITIALIZED;
849 local_irq_restore(flags);
850}
851
852/*
853 * This routine is called to set the UART divisor registers to match
854 * the specified baud rate for a serial port.
855 */
856static void change_speed(ser_info_t *info)
857{
858 int baud_rate;
859 unsigned cflag, cval, scval, prev_mode;
860 int i, bits, sbits, idx;
861 unsigned long flags;
862 struct serial_state *state;
863 volatile struct smc_regs *smcp;
864 volatile struct scc_regs *sccp;
865
866 if (!info->port.tty || !info->port.tty->termios)
867 return;
868 cflag = info->port.tty->termios->c_cflag;
869
870 state = info->state;
871
872 /* Character length programmed into the mode register is the
873 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
874 * 1 or 2 stop bits, minus 1.
875 * The value 'bits' counts this for us.
876 */
877 cval = 0;
878 scval = 0;
879
880 /* byte size and parity */
881 switch (cflag & CSIZE) {
882 case CS5: bits = 5; break;
883 case CS6: bits = 6; break;
884 case CS7: bits = 7; break;
885 case CS8: bits = 8; break;
886 /* Never happens, but GCC is too dumb to figure it out */
887 default: bits = 8; break;
888 }
889 sbits = bits - 5;
890
891 if (cflag & CSTOPB) {
892 cval |= SMCMR_SL; /* Two stops */
893 scval |= SCU_PMSR_SL;
894 bits++;
895 }
896 if (cflag & PARENB) {
897 cval |= SMCMR_PEN;
898 scval |= SCU_PMSR_PEN;
899 bits++;
900 }
901 if (!(cflag & PARODD)) {
902 cval |= SMCMR_PM_EVEN;
903 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
904 }
905
906 /* Determine divisor based on baud rate */
907 i = cflag & CBAUD;
908 if (i >= (sizeof(baud_table)/sizeof(int)))
909 baud_rate = 9600;
910 else
911 baud_rate = baud_table[i];
912
913 info->timeout = (TX_BUF_SIZE*HZ*bits);
914 info->timeout += HZ/50; /* Add .02 seconds of slop */
915
916#ifdef modem_control
917 /* CTS flow control flag and modem status interrupts */
918 info->IER &= ~UART_IER_MSI;
919 if (info->flags & ASYNC_HARDPPS_CD)
920 info->IER |= UART_IER_MSI;
921 if (cflag & CRTSCTS) {
922 info->flags |= ASYNC_CTS_FLOW;
923 info->IER |= UART_IER_MSI;
924 } else
925 info->flags &= ~ASYNC_CTS_FLOW;
926 if (cflag & CLOCAL)
927 info->flags &= ~ASYNC_CHECK_CD;
928 else {
929 info->flags |= ASYNC_CHECK_CD;
930 info->IER |= UART_IER_MSI;
931 }
932 serial_out(info, UART_IER, info->IER);
933#endif
934
935 /*
936 * Set up parity check flag
937 */
938 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
939 if (I_INPCK(info->port.tty))
940 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
941 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
942 info->read_status_mask |= BD_SC_BR;
943
944 /*
945 * Characters to ignore
946 */
947 info->ignore_status_mask = 0;
948 if (I_IGNPAR(info->port.tty))
949 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
950 if (I_IGNBRK(info->port.tty)) {
951 info->ignore_status_mask |= BD_SC_BR;
952 /*
953 * If we're ignore parity and break indicators, ignore
954 * overruns too. (For real raw support).
955 */
956 if (I_IGNPAR(info->port.tty))
957 info->ignore_status_mask |= BD_SC_OV;
958 }
959 /*
960 * !!! ignore all characters if CREAD is not set
961 */
962 if ((cflag & CREAD) == 0)
963 info->read_status_mask &= ~BD_SC_EMPTY;
964 local_irq_save(flags);
965
966 /* Start bit has not been added (so don't, because we would just
967 * subtract it later), and we need to add one for the number of
968 * stops bits (there is always at least one).
969 */
970 bits++;
971 idx = PORT_NUM(state->smc_scc_num);
972 if (state->smc_scc_num & NUM_IS_SCC) {
973 sccp = &pquicc->scc_regs[idx];
974 sccp->scc_psmr = (sbits << 12) | scval;
975 } else {
976 smcp = &pquicc->smc_regs[idx];
977
978 /* Set the mode register. We want to keep a copy of the
979 * enables, because we want to put them back if they were
980 * present.
981 */
982 prev_mode = smcp->smc_smcmr;
983 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
984 smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
985 }
986
987 m360_cpm_setbrg((state - rs_table), baud_rate);
988
989 local_irq_restore(flags);
990}
991
992static void rs_360_put_char(struct tty_struct *tty, unsigned char ch)
993{
994 ser_info_t *info = (ser_info_t *)tty->driver_data;
995 volatile QUICC_BD *bdp;
996
997 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
998 return 0;
999
1000 if (!tty)
1001 return 0;
1002
1003 bdp = info->tx_cur;
1004 while (bdp->status & BD_SC_READY);
1005
1006 /* *((char *)__va(bdp->buf)) = ch; */
1007 *((char *)bdp->buf) = ch;
1008 bdp->length = 1;
1009 bdp->status |= BD_SC_READY;
1010
1011 /* Get next BD.
1012 */
1013 if (bdp->status & BD_SC_WRAP)
1014 bdp = info->tx_bd_base;
1015 else
1016 bdp++;
1017
1018 info->tx_cur = (QUICC_BD *)bdp;
1019 return 1;
1020
1021}
1022
1023static int rs_360_write(struct tty_struct * tty,
1024 const unsigned char *buf, int count)
1025{
1026 int c, ret = 0;
1027 ser_info_t *info = (ser_info_t *)tty->driver_data;
1028 volatile QUICC_BD *bdp;
1029
1030#ifdef CONFIG_KGDB
1031 /* Try to let stub handle output. Returns true if it did. */
1032 if (kgdb_output_string(buf, count))
1033 return ret;
1034#endif
1035
1036 if (serial_paranoia_check(info, tty->name, "rs_write"))
1037 return 0;
1038
1039 if (!tty)
1040 return 0;
1041
1042 bdp = info->tx_cur;
1043
1044 while (1) {
1045 c = min(count, TX_BUF_SIZE);
1046
1047 if (c <= 0)
1048 break;
1049
1050 if (bdp->status & BD_SC_READY) {
1051 info->flags |= TX_WAKEUP;
1052 break;
1053 }
1054
1055 /* memcpy(__va(bdp->buf), buf, c); */
1056 memcpy((void *)bdp->buf, buf, c);
1057
1058 bdp->length = c;
1059 bdp->status |= BD_SC_READY;
1060
1061 buf += c;
1062 count -= c;
1063 ret += c;
1064
1065 /* Get next BD.
1066 */
1067 if (bdp->status & BD_SC_WRAP)
1068 bdp = info->tx_bd_base;
1069 else
1070 bdp++;
1071 info->tx_cur = (QUICC_BD *)bdp;
1072 }
1073 return ret;
1074}
1075
1076static int rs_360_write_room(struct tty_struct *tty)
1077{
1078 ser_info_t *info = (ser_info_t *)tty->driver_data;
1079 int ret;
1080
1081 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1082 return 0;
1083
1084 if ((info->tx_cur->status & BD_SC_READY) == 0) {
1085 info->flags &= ~TX_WAKEUP;
1086 ret = TX_BUF_SIZE;
1087 }
1088 else {
1089 info->flags |= TX_WAKEUP;
1090 ret = 0;
1091 }
1092 return ret;
1093}
1094
1095/* I could track this with transmit counters....maybe later.
1096*/
1097static int rs_360_chars_in_buffer(struct tty_struct *tty)
1098{
1099 ser_info_t *info = (ser_info_t *)tty->driver_data;
1100
1101 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1102 return 0;
1103 return 0;
1104}
1105
1106static void rs_360_flush_buffer(struct tty_struct *tty)
1107{
1108 ser_info_t *info = (ser_info_t *)tty->driver_data;
1109
1110 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1111 return;
1112
1113 /* There is nothing to "flush", whatever we gave the CPM
1114 * is on its way out.
1115 */
1116 tty_wakeup(tty);
1117 info->flags &= ~TX_WAKEUP;
1118}
1119
1120/*
1121 * This function is used to send a high-priority XON/XOFF character to
1122 * the device
1123 */
1124static void rs_360_send_xchar(struct tty_struct *tty, char ch)
1125{
1126 volatile QUICC_BD *bdp;
1127
1128 ser_info_t *info = (ser_info_t *)tty->driver_data;
1129
1130 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
1131 return;
1132
1133 bdp = info->tx_cur;
1134 while (bdp->status & BD_SC_READY);
1135
1136 /* *((char *)__va(bdp->buf)) = ch; */
1137 *((char *)bdp->buf) = ch;
1138 bdp->length = 1;
1139 bdp->status |= BD_SC_READY;
1140
1141 /* Get next BD.
1142 */
1143 if (bdp->status & BD_SC_WRAP)
1144 bdp = info->tx_bd_base;
1145 else
1146 bdp++;
1147
1148 info->tx_cur = (QUICC_BD *)bdp;
1149}
1150
1151/*
1152 * ------------------------------------------------------------
1153 * rs_throttle()
1154 *
1155 * This routine is called by the upper-layer tty layer to signal that
1156 * incoming characters should be throttled.
1157 * ------------------------------------------------------------
1158 */
1159static void rs_360_throttle(struct tty_struct * tty)
1160{
1161 ser_info_t *info = (ser_info_t *)tty->driver_data;
1162#ifdef SERIAL_DEBUG_THROTTLE
1163 char buf[64];
1164
1165 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1166 tty->ldisc.chars_in_buffer(tty));
1167#endif
1168
1169 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1170 return;
1171
1172 if (I_IXOFF(tty))
1173 rs_360_send_xchar(tty, STOP_CHAR(tty));
1174
1175#ifdef modem_control
1176 if (tty->termios->c_cflag & CRTSCTS)
1177 info->MCR &= ~UART_MCR_RTS;
1178
1179 local_irq_disable();
1180 serial_out(info, UART_MCR, info->MCR);
1181 local_irq_enable();
1182#endif
1183}
1184
1185static void rs_360_unthrottle(struct tty_struct * tty)
1186{
1187 ser_info_t *info = (ser_info_t *)tty->driver_data;
1188#ifdef SERIAL_DEBUG_THROTTLE
1189 char buf[64];
1190
1191 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1192 tty->ldisc.chars_in_buffer(tty));
1193#endif
1194
1195 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1196 return;
1197
1198 if (I_IXOFF(tty)) {
1199 if (info->x_char)
1200 info->x_char = 0;
1201 else
1202 rs_360_send_xchar(tty, START_CHAR(tty));
1203 }
1204#ifdef modem_control
1205 if (tty->termios->c_cflag & CRTSCTS)
1206 info->MCR |= UART_MCR_RTS;
1207 local_irq_disable();
1208 serial_out(info, UART_MCR, info->MCR);
1209 local_irq_enable();
1210#endif
1211}
1212
1213/*
1214 * ------------------------------------------------------------
1215 * rs_ioctl() and friends
1216 * ------------------------------------------------------------
1217 */
1218
1219#ifdef maybe
1220/*
1221 * get_lsr_info - get line status register info
1222 *
1223 * Purpose: Let user call ioctl() to get info when the UART physically
1224 * is emptied. On bus types like RS485, the transmitter must
1225 * release the bus after transmitting. This must be done when
1226 * the transmit shift register is empty, not be done when the
1227 * transmit holding register is empty. This functionality
1228 * allows an RS485 driver to be written in user space.
1229 */
1230static int get_lsr_info(struct async_struct * info, unsigned int *value)
1231{
1232 unsigned char status;
1233 unsigned int result;
1234
1235 local_irq_disable();
1236 status = serial_in(info, UART_LSR);
1237 local_irq_enable();
1238 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1239 return put_user(result,value);
1240}
1241#endif
1242
1243static int rs_360_tiocmget(struct tty_struct *tty)
1244{
1245 ser_info_t *info = (ser_info_t *)tty->driver_data;
1246 unsigned int result = 0;
1247#ifdef modem_control
1248 unsigned char control, status;
1249
1250 if (serial_paranoia_check(info, tty->name, __func__))
1251 return -ENODEV;
1252
1253 if (tty->flags & (1 << TTY_IO_ERROR))
1254 return -EIO;
1255
1256 control = info->MCR;
1257 local_irq_disable();
1258 status = serial_in(info, UART_MSR);
1259 local_irq_enable();
1260 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1261 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1262#ifdef TIOCM_OUT1
1263 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1264 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1265#endif
1266 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1267 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1268 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1269 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1270#endif
1271 return result;
1272}
1273
1274static int rs_360_tiocmset(struct tty_struct *tty,
1275 unsigned int set, unsigned int clear)
1276{
1277#ifdef modem_control
1278 ser_info_t *info = (ser_info_t *)tty->driver_data;
1279 unsigned int arg;
1280
1281 if (serial_paranoia_check(info, tty->name, __func__))
1282 return -ENODEV;
1283
1284 if (tty->flags & (1 << TTY_IO_ERROR))
1285 return -EIO;
1286 /* FIXME: locking on info->mcr */
1287 if (set & TIOCM_RTS)
1288 info->mcr |= UART_MCR_RTS;
1289 if (set & TIOCM_DTR)
1290 info->mcr |= UART_MCR_DTR;
1291 if (clear & TIOCM_RTS)
1292 info->MCR &= ~UART_MCR_RTS;
1293 if (clear & TIOCM_DTR)
1294 info->MCR &= ~UART_MCR_DTR;
1295
1296#ifdef TIOCM_OUT1
1297 if (set & TIOCM_OUT1)
1298 info->MCR |= UART_MCR_OUT1;
1299 if (set & TIOCM_OUT2)
1300 info->MCR |= UART_MCR_OUT2;
1301 if (clear & TIOCM_OUT1)
1302 info->MCR &= ~UART_MCR_OUT1;
1303 if (clear & TIOCM_OUT2)
1304 info->MCR &= ~UART_MCR_OUT2;
1305#endif
1306
1307 local_irq_disable();
1308 serial_out(info, UART_MCR, info->MCR);
1309 local_irq_enable();
1310#endif
1311 return 0;
1312}
1313
1314/* Sending a break is a two step process on the SMC/SCC. It is accomplished
1315 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1316 * command. We take advantage of the begin/end functions to make this
1317 * happen.
1318 */
1319static ushort smc_chan_map[] = {
1320 CPM_CR_CH_SMC1,
1321 CPM_CR_CH_SMC2
1322};
1323
1324static ushort scc_chan_map[] = {
1325 CPM_CR_CH_SCC1,
1326 CPM_CR_CH_SCC2,
1327 CPM_CR_CH_SCC3,
1328 CPM_CR_CH_SCC4
1329};
1330
1331static void begin_break(ser_info_t *info)
1332{
1333 volatile QUICC *cp;
1334 ushort chan;
1335 int idx;
1336
1337 cp = pquicc;
1338
1339 idx = PORT_NUM(info->state->smc_scc_num);
1340 if (info->state->smc_scc_num & NUM_IS_SCC)
1341 chan = scc_chan_map[idx];
1342 else
1343 chan = smc_chan_map[idx];
1344
1345 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1346 while (cp->cp_cr & CPM_CR_FLG);
1347}
1348
1349static void end_break(ser_info_t *info)
1350{
1351 volatile QUICC *cp;
1352 ushort chan;
1353 int idx;
1354
1355 cp = pquicc;
1356
1357 idx = PORT_NUM(info->state->smc_scc_num);
1358 if (info->state->smc_scc_num & NUM_IS_SCC)
1359 chan = scc_chan_map[idx];
1360 else
1361 chan = smc_chan_map[idx];
1362
1363 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1364 while (cp->cp_cr & CPM_CR_FLG);
1365}
1366
1367/*
1368 * This routine sends a break character out the serial port.
1369 */
1370static void send_break(ser_info_t *info, unsigned int duration)
1371{
1372#ifdef SERIAL_DEBUG_SEND_BREAK
1373 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1374#endif
1375 begin_break(info);
1376 msleep_interruptible(duration);
1377 end_break(info);
1378#ifdef SERIAL_DEBUG_SEND_BREAK
1379 printk("done jiffies=%lu\n", jiffies);
1380#endif
1381}
1382
1383
1384/*
1385 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1386 * Return: write counters to the user passed counter struct
1387 * NB: both 1->0 and 0->1 transitions are counted except for
1388 * RI where only 0->1 is counted.
1389 */
1390static int rs_360_get_icount(struct tty_struct *tty,
1391 struct serial_icounter_struct *icount)
1392{
1393 ser_info_t *info = (ser_info_t *)tty->driver_data;
1394 struct async_icount cnow;
1395
1396 local_irq_disable();
1397 cnow = info->state->icount;
1398 local_irq_enable();
1399
1400 icount->cts = cnow.cts;
1401 icount->dsr = cnow.dsr;
1402 icount->rng = cnow.rng;
1403 icount->dcd = cnow.dcd;
1404
1405 return 0;
1406}
1407
1408static int rs_360_ioctl(struct tty_struct *tty,
1409 unsigned int cmd, unsigned long arg)
1410{
1411 int error;
1412 ser_info_t *info = (ser_info_t *)tty->driver_data;
1413 int retval;
1414 struct async_icount cnow;
1415 /* struct async_icount_24 cnow;*/ /* kernel counter temps */
1416 struct serial_icounter_struct *p_cuser; /* user space */
1417
1418 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1419 return -ENODEV;
1420
1421 if (cmd != TIOCMIWAIT) {
1422 if (tty->flags & (1 << TTY_IO_ERROR))
1423 return -EIO;
1424 }
1425
1426 switch (cmd) {
1427 case TCSBRK: /* SVID version: non-zero arg --> no break */
1428 retval = tty_check_change(tty);
1429 if (retval)
1430 return retval;
1431 tty_wait_until_sent(tty, 0);
1432 if (signal_pending(current))
1433 return -EINTR;
1434 if (!arg) {
1435 send_break(info, 250); /* 1/4 second */
1436 if (signal_pending(current))
1437 return -EINTR;
1438 }
1439 return 0;
1440 case TCSBRKP: /* support for POSIX tcsendbreak() */
1441 retval = tty_check_change(tty);
1442 if (retval)
1443 return retval;
1444 tty_wait_until_sent(tty, 0);
1445 if (signal_pending(current))
1446 return -EINTR;
1447 send_break(info, arg ? arg*100 : 250);
1448 if (signal_pending(current))
1449 return -EINTR;
1450 return 0;
1451 case TIOCSBRK:
1452 retval = tty_check_change(tty);
1453 if (retval)
1454 return retval;
1455 tty_wait_until_sent(tty, 0);
1456 begin_break(info);
1457 return 0;
1458 case TIOCCBRK:
1459 retval = tty_check_change(tty);
1460 if (retval)
1461 return retval;
1462 end_break(info);
1463 return 0;
1464#ifdef maybe
1465 case TIOCSERGETLSR: /* Get line status register */
1466 return get_lsr_info(info, (unsigned int *) arg);
1467#endif
1468 /*
1469 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1470 * - mask passed in arg for lines of interest
1471 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1472 * Caller should use TIOCGICOUNT to see which one it was
1473 */
1474 case TIOCMIWAIT:
1475#ifdef modem_control
1476 local_irq_disable();
1477 /* note the counters on entry */
1478 cprev = info->state->icount;
1479 local_irq_enable();
1480 while (1) {
1481 interruptible_sleep_on(&info->delta_msr_wait);
1482 /* see if a signal did it */
1483 if (signal_pending(current))
1484 return -ERESTARTSYS;
1485 local_irq_disable();
1486 cnow = info->state->icount; /* atomic copy */
1487 local_irq_enable();
1488 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1489 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1490 return -EIO; /* no change => error */
1491 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1492 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1493 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1494 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1495 return 0;
1496 }
1497 cprev = cnow;
1498 }
1499 /* NOTREACHED */
1500#else
1501 return 0;
1502#endif
1503
1504
1505 default:
1506 return -ENOIOCTLCMD;
1507 }
1508 return 0;
1509}
1510
1511/* FIX UP modem control here someday......
1512*/
1513static void rs_360_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1514{
1515 ser_info_t *info = (ser_info_t *)tty->driver_data;
1516
1517 change_speed(info);
1518
1519#ifdef modem_control
1520 /* Handle transition to B0 status */
1521 if ((old_termios->c_cflag & CBAUD) &&
1522 !(tty->termios->c_cflag & CBAUD)) {
1523 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1524 local_irq_disable();
1525 serial_out(info, UART_MCR, info->MCR);
1526 local_irq_enable();
1527 }
1528
1529 /* Handle transition away from B0 status */
1530 if (!(old_termios->c_cflag & CBAUD) &&
1531 (tty->termios->c_cflag & CBAUD)) {
1532 info->MCR |= UART_MCR_DTR;
1533 if (!tty->hw_stopped ||
1534 !(tty->termios->c_cflag & CRTSCTS)) {
1535 info->MCR |= UART_MCR_RTS;
1536 }
1537 local_irq_disable();
1538 serial_out(info, UART_MCR, info->MCR);
1539 local_irq_enable();
1540 }
1541
1542 /* Handle turning off CRTSCTS */
1543 if ((old_termios->c_cflag & CRTSCTS) &&
1544 !(tty->termios->c_cflag & CRTSCTS)) {
1545 tty->hw_stopped = 0;
1546 rs_360_start(tty);
1547 }
1548#endif
1549
1550#if 0
1551 /*
1552 * No need to wake up processes in open wait, since they
1553 * sample the CLOCAL flag once, and don't recheck it.
1554 * XXX It's not clear whether the current behavior is correct
1555 * or not. Hence, this may change.....
1556 */
1557 if (!(old_termios->c_cflag & CLOCAL) &&
1558 (tty->termios->c_cflag & CLOCAL))
1559 wake_up_interruptible(&info->open_wait);
1560#endif
1561}
1562
1563/*
1564 * ------------------------------------------------------------
1565 * rs_close()
1566 *
1567 * This routine is called when the serial port gets closed. First, we
1568 * wait for the last remaining data to be sent. Then, we unlink its
1569 * async structure from the interrupt chain if necessary, and we free
1570 * that IRQ if nothing is left in the chain.
1571 * ------------------------------------------------------------
1572 */
1573static void rs_360_close(struct tty_struct *tty, struct file * filp)
1574{
1575 ser_info_t *info = (ser_info_t *)tty->driver_data;
1576 /* struct async_state *state; */
1577 struct serial_state *state;
1578 unsigned long flags;
1579 int idx;
1580 volatile struct smc_regs *smcp;
1581 volatile struct scc_regs *sccp;
1582
1583 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1584 return;
1585
1586 state = info->state;
1587
1588 local_irq_save(flags);
1589
1590 if (tty_hung_up_p(filp)) {
1591 DBG_CNT("before DEC-hung");
1592 local_irq_restore(flags);
1593 return;
1594 }
1595
1596#ifdef SERIAL_DEBUG_OPEN
1597 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1598#endif
1599 if ((tty->count == 1) && (state->count != 1)) {
1600 /*
1601 * Uh, oh. tty->count is 1, which means that the tty
1602 * structure will be freed. state->count should always
1603 * be one in these conditions. If it's greater than
1604 * one, we've got real problems, since it means the
1605 * serial port won't be shutdown.
1606 */
1607 printk("rs_close: bad serial port count; tty->count is 1, "
1608 "state->count is %d\n", state->count);
1609 state->count = 1;
1610 }
1611 if (--state->count < 0) {
1612 printk("rs_close: bad serial port count for ttys%d: %d\n",
1613 info->line, state->count);
1614 state->count = 0;
1615 }
1616 if (state->count) {
1617 DBG_CNT("before DEC-2");
1618 local_irq_restore(flags);
1619 return;
1620 }
1621 info->flags |= ASYNC_CLOSING;
1622 /*
1623 * Now we wait for the transmit buffer to clear; and we notify
1624 * the line discipline to only process XON/XOFF characters.
1625 */
1626 tty->closing = 1;
1627 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1628 tty_wait_until_sent(tty, info->closing_wait);
1629 /*
1630 * At this point we stop accepting input. To do this, we
1631 * disable the receive line status interrupts, and tell the
1632 * interrupt driver to stop checking the data ready bit in the
1633 * line status register.
1634 */
1635 info->read_status_mask &= ~BD_SC_EMPTY;
1636 if (info->flags & ASYNC_INITIALIZED) {
1637
1638 idx = PORT_NUM(info->state->smc_scc_num);
1639 if (info->state->smc_scc_num & NUM_IS_SCC) {
1640 sccp = &pquicc->scc_regs[idx];
1641 sccp->scc_sccm &= ~UART_SCCM_RX;
1642 sccp->scc_gsmr.w.low &= ~SCC_GSMRL_ENR;
1643 } else {
1644 smcp = &pquicc->smc_regs[idx];
1645 smcp->smc_smcm &= ~SMCM_RX;
1646 smcp->smc_smcmr &= ~SMCMR_REN;
1647 }
1648 /*
1649 * Before we drop DTR, make sure the UART transmitter
1650 * has completely drained; this is especially
1651 * important if there is a transmit FIFO!
1652 */
1653 rs_360_wait_until_sent(tty, info->timeout);
1654 }
1655 shutdown(info);
1656 rs_360_flush_buffer(tty);
1657 tty_ldisc_flush(tty);
1658 tty->closing = 0;
1659 info->event = 0;
1660 info->port.tty = NULL;
1661 if (info->blocked_open) {
1662 if (info->close_delay) {
1663 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1664 }
1665 wake_up_interruptible(&info->open_wait);
1666 }
1667 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1668 wake_up_interruptible(&info->close_wait);
1669 local_irq_restore(flags);
1670}
1671
1672/*
1673 * rs_wait_until_sent() --- wait until the transmitter is empty
1674 */
1675static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout)
1676{
1677 ser_info_t *info = (ser_info_t *)tty->driver_data;
1678 unsigned long orig_jiffies, char_time;
1679 /*int lsr;*/
1680 volatile QUICC_BD *bdp;
1681
1682 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1683 return;
1684
1685#ifdef maybe
1686 if (info->state->type == PORT_UNKNOWN)
1687 return;
1688#endif
1689
1690 orig_jiffies = jiffies;
1691 /*
1692 * Set the check interval to be 1/5 of the estimated time to
1693 * send a single character, and make it at least 1. The check
1694 * interval should also be less than the timeout.
1695 *
1696 * Note: we have to use pretty tight timings here to satisfy
1697 * the NIST-PCTS.
1698 */
1699 char_time = 1;
1700 if (timeout)
1701 char_time = min(char_time, (unsigned long)timeout);
1702#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1703 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1704 printk("jiff=%lu...", jiffies);
1705#endif
1706
1707 /* We go through the loop at least once because we can't tell
1708 * exactly when the last character exits the shifter. There can
1709 * be at least two characters waiting to be sent after the buffers
1710 * are empty.
1711 */
1712 do {
1713#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1714 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1715#endif
1716/* current->counter = 0; make us low-priority */
1717 msleep_interruptible(jiffies_to_msecs(char_time));
1718 if (signal_pending(current))
1719 break;
1720 if (timeout && (time_after(jiffies, orig_jiffies + timeout)))
1721 break;
1722 /* The 'tx_cur' is really the next buffer to send. We
1723 * have to back up to the previous BD and wait for it
1724 * to go. This isn't perfect, because all this indicates
1725 * is the buffer is available. There are still characters
1726 * in the CPM FIFO.
1727 */
1728 bdp = info->tx_cur;
1729 if (bdp == info->tx_bd_base)
1730 bdp += (TX_NUM_FIFO-1);
1731 else
1732 bdp--;
1733 } while (bdp->status & BD_SC_READY);
1734 current->state = TASK_RUNNING;
1735#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1736 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1737#endif
1738}
1739
1740/*
1741 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1742 */
1743static void rs_360_hangup(struct tty_struct *tty)
1744{
1745 ser_info_t *info = (ser_info_t *)tty->driver_data;
1746 struct serial_state *state = info->state;
1747
1748 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1749 return;
1750
1751 state = info->state;
1752
1753 rs_360_flush_buffer(tty);
1754 shutdown(info);
1755 info->event = 0;
1756 state->count = 0;
1757 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1758 info->port.tty = NULL;
1759 wake_up_interruptible(&info->open_wait);
1760}
1761
1762/*
1763 * ------------------------------------------------------------
1764 * rs_open() and friends
1765 * ------------------------------------------------------------
1766 */
1767static int block_til_ready(struct tty_struct *tty, struct file * filp,
1768 ser_info_t *info)
1769{
1770#ifdef DO_THIS_LATER
1771 DECLARE_WAITQUEUE(wait, current);
1772#endif
1773 struct serial_state *state = info->state;
1774 int retval;
1775 int do_clocal = 0;
1776
1777 /*
1778 * If the device is in the middle of being closed, then block
1779 * until it's done, and then try again.
1780 */
1781 if (tty_hung_up_p(filp) ||
1782 (info->flags & ASYNC_CLOSING)) {
1783 if (info->flags & ASYNC_CLOSING)
1784 interruptible_sleep_on(&info->close_wait);
1785#ifdef SERIAL_DO_RESTART
1786 if (info->flags & ASYNC_HUP_NOTIFY)
1787 return -EAGAIN;
1788 else
1789 return -ERESTARTSYS;
1790#else
1791 return -EAGAIN;
1792#endif
1793 }
1794
1795 /*
1796 * If non-blocking mode is set, or the port is not enabled,
1797 * then make the check up front and then exit.
1798 * If this is an SMC port, we don't have modem control to wait
1799 * for, so just get out here.
1800 */
1801 if ((filp->f_flags & O_NONBLOCK) ||
1802 (tty->flags & (1 << TTY_IO_ERROR)) ||
1803 !(info->state->smc_scc_num & NUM_IS_SCC)) {
1804 info->flags |= ASYNC_NORMAL_ACTIVE;
1805 return 0;
1806 }
1807
1808 if (tty->termios->c_cflag & CLOCAL)
1809 do_clocal = 1;
1810
1811 /*
1812 * Block waiting for the carrier detect and the line to become
1813 * free (i.e., not in use by the callout). While we are in
1814 * this loop, state->count is dropped by one, so that
1815 * rs_close() knows when to free things. We restore it upon
1816 * exit, either normal or abnormal.
1817 */
1818 retval = 0;
1819#ifdef DO_THIS_LATER
1820 add_wait_queue(&info->open_wait, &wait);
1821#ifdef SERIAL_DEBUG_OPEN
1822 printk("block_til_ready before block: ttys%d, count = %d\n",
1823 state->line, state->count);
1824#endif
1825 local_irq_disable();
1826 if (!tty_hung_up_p(filp))
1827 state->count--;
1828 local_irq_enable();
1829 info->blocked_open++;
1830 while (1) {
1831 local_irq_disable();
1832 if (tty->termios->c_cflag & CBAUD)
1833 serial_out(info, UART_MCR,
1834 serial_inp(info, UART_MCR) |
1835 (UART_MCR_DTR | UART_MCR_RTS));
1836 local_irq_enable();
1837 set_current_state(TASK_INTERRUPTIBLE);
1838 if (tty_hung_up_p(filp) ||
1839 !(info->flags & ASYNC_INITIALIZED)) {
1840#ifdef SERIAL_DO_RESTART
1841 if (info->flags & ASYNC_HUP_NOTIFY)
1842 retval = -EAGAIN;
1843 else
1844 retval = -ERESTARTSYS;
1845#else
1846 retval = -EAGAIN;
1847#endif
1848 break;
1849 }
1850 if (!(info->flags & ASYNC_CLOSING) &&
1851 (do_clocal || (serial_in(info, UART_MSR) &
1852 UART_MSR_DCD)))
1853 break;
1854 if (signal_pending(current)) {
1855 retval = -ERESTARTSYS;
1856 break;
1857 }
1858#ifdef SERIAL_DEBUG_OPEN
1859 printk("block_til_ready blocking: ttys%d, count = %d\n",
1860 info->line, state->count);
1861#endif
1862 tty_unlock();
1863 schedule();
1864 tty_lock();
1865 }
1866 current->state = TASK_RUNNING;
1867 remove_wait_queue(&info->open_wait, &wait);
1868 if (!tty_hung_up_p(filp))
1869 state->count++;
1870 info->blocked_open--;
1871#ifdef SERIAL_DEBUG_OPEN
1872 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1873 info->line, state->count);
1874#endif
1875#endif /* DO_THIS_LATER */
1876 if (retval)
1877 return retval;
1878 info->flags |= ASYNC_NORMAL_ACTIVE;
1879 return 0;
1880}
1881
1882static int get_async_struct(int line, ser_info_t **ret_info)
1883{
1884 struct serial_state *sstate;
1885
1886 sstate = rs_table + line;
1887 if (sstate->info) {
1888 sstate->count++;
1889 *ret_info = (ser_info_t *)sstate->info;
1890 return 0;
1891 }
1892 else {
1893 return -ENOMEM;
1894 }
1895}
1896
1897/*
1898 * This routine is called whenever a serial port is opened. It
1899 * enables interrupts for a serial port, linking in its async structure into
1900 * the IRQ chain. It also performs the serial-specific
1901 * initialization for the tty structure.
1902 */
1903static int rs_360_open(struct tty_struct *tty, struct file * filp)
1904{
1905 ser_info_t *info;
1906 int retval, line;
1907
1908 line = tty->index;
1909 if ((line < 0) || (line >= NR_PORTS))
1910 return -ENODEV;
1911 retval = get_async_struct(line, &info);
1912 if (retval)
1913 return retval;
1914 if (serial_paranoia_check(info, tty->name, "rs_open"))
1915 return -ENODEV;
1916
1917#ifdef SERIAL_DEBUG_OPEN
1918 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1919#endif
1920 tty->driver_data = info;
1921 info->port.tty = tty;
1922
1923 /*
1924 * Start up serial port
1925 */
1926 retval = startup(info);
1927 if (retval)
1928 return retval;
1929
1930 retval = block_til_ready(tty, filp, info);
1931 if (retval) {
1932#ifdef SERIAL_DEBUG_OPEN
1933 printk("rs_open returning after block_til_ready with %d\n",
1934 retval);
1935#endif
1936 return retval;
1937 }
1938
1939#ifdef SERIAL_DEBUG_OPEN
1940 printk("rs_open %s successful...", tty->name);
1941#endif
1942 return 0;
1943}
1944
1945/*
1946 * /proc fs routines....
1947 */
1948
1949static inline int line_info(char *buf, struct serial_state *state)
1950{
1951#ifdef notdef
1952 struct async_struct *info = state->info, scr_info;
1953 char stat_buf[30], control, status;
1954#endif
1955 int ret;
1956
1957 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
1958 state->line,
1959 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC",
1960 (unsigned int)(state->port), state->irq);
1961
1962 if (!state->port || (state->type == PORT_UNKNOWN)) {
1963 ret += sprintf(buf+ret, "\n");
1964 return ret;
1965 }
1966
1967#ifdef notdef
1968 /*
1969 * Figure out the current RS-232 lines
1970 */
1971 if (!info) {
1972 info = &scr_info; /* This is just for serial_{in,out} */
1973
1974 info->magic = SERIAL_MAGIC;
1975 info->port = state->port;
1976 info->flags = state->flags;
1977 info->quot = 0;
1978 info->port.tty = NULL;
1979 }
1980 local_irq_disable();
1981 status = serial_in(info, UART_MSR);
1982 control = info ? info->MCR : serial_in(info, UART_MCR);
1983 local_irq_enable();
1984
1985 stat_buf[0] = 0;
1986 stat_buf[1] = 0;
1987 if (control & UART_MCR_RTS)
1988 strcat(stat_buf, "|RTS");
1989 if (status & UART_MSR_CTS)
1990 strcat(stat_buf, "|CTS");
1991 if (control & UART_MCR_DTR)
1992 strcat(stat_buf, "|DTR");
1993 if (status & UART_MSR_DSR)
1994 strcat(stat_buf, "|DSR");
1995 if (status & UART_MSR_DCD)
1996 strcat(stat_buf, "|CD");
1997 if (status & UART_MSR_RI)
1998 strcat(stat_buf, "|RI");
1999
2000 if (info->quot) {
2001 ret += sprintf(buf+ret, " baud:%d",
2002 state->baud_base / info->quot);
2003 }
2004
2005 ret += sprintf(buf+ret, " tx:%d rx:%d",
2006 state->icount.tx, state->icount.rx);
2007
2008 if (state->icount.frame)
2009 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2010
2011 if (state->icount.parity)
2012 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2013
2014 if (state->icount.brk)
2015 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2016
2017 if (state->icount.overrun)
2018 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2019
2020 /*
2021 * Last thing is the RS-232 status lines
2022 */
2023 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2024#endif
2025 return ret;
2026}
2027
2028int rs_360_read_proc(char *page, char **start, off_t off, int count,
2029 int *eof, void *data)
2030{
2031 int i, len = 0;
2032 off_t begin = 0;
2033
2034 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2035 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2036 len += line_info(page + len, &rs_table[i]);
2037 if (len+begin > off+count)
2038 goto done;
2039 if (len+begin < off) {
2040 begin += len;
2041 len = 0;
2042 }
2043 }
2044 *eof = 1;
2045done:
2046 if (off >= len+begin)
2047 return 0;
2048 *start = page + (begin-off);
2049 return ((count < begin+len-off) ? count : begin+len-off);
2050}
2051
2052/*
2053 * ---------------------------------------------------------------------
2054 * rs_init() and friends
2055 *
2056 * rs_init() is called at boot-time to initialize the serial driver.
2057 * ---------------------------------------------------------------------
2058 */
2059
2060/*
2061 * This routine prints out the appropriate serial driver version
2062 * number, and identifies which options were configured into this
2063 * driver.
2064 */
2065static _INLINE_ void show_serial_version(void)
2066{
2067 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2068}
2069
2070
2071/*
2072 * The serial console driver used during boot. Note that these names
2073 * clash with those found in "serial.c", so we currently can't support
2074 * the 16xxx uarts and these at the same time. I will fix this to become
2075 * an indirect function call from tty_io.c (or something).
2076 */
2077
2078#ifdef CONFIG_SERIAL_CONSOLE
2079
2080/*
2081 * Print a string to the serial port trying not to disturb any possible
2082 * real use of the port...
2083 */
2084static void my_console_write(int idx, const char *s,
2085 unsigned count)
2086{
2087 struct serial_state *ser;
2088 ser_info_t *info;
2089 unsigned i;
2090 QUICC_BD *bdp, *bdbase;
2091 volatile struct smc_uart_pram *up;
2092 volatile u_char *cp;
2093
2094 ser = rs_table + idx;
2095
2096
2097 /* If the port has been initialized for general use, we have
2098 * to use the buffer descriptors allocated there. Otherwise,
2099 * we simply use the single buffer allocated.
2100 */
2101 if ((info = (ser_info_t *)ser->info) != NULL) {
2102 bdp = info->tx_cur;
2103 bdbase = info->tx_bd_base;
2104 }
2105 else {
2106 /* Pointer to UART in parameter ram.
2107 */
2108 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2109 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2110
2111 /* Get the address of the host memory buffer.
2112 */
2113 bdp = bdbase = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2114 }
2115
2116 /*
2117 * We need to gracefully shut down the transmitter, disable
2118 * interrupts, then send our bytes out.
2119 */
2120
2121 /*
2122 * Now, do each character. This is not as bad as it looks
2123 * since this is a holding FIFO and not a transmitting FIFO.
2124 * We could add the complexity of filling the entire transmit
2125 * buffer, but we would just wait longer between accesses......
2126 */
2127 for (i = 0; i < count; i++, s++) {
2128 /* Wait for transmitter fifo to empty.
2129 * Ready indicates output is ready, and xmt is doing
2130 * that, not that it is ready for us to send.
2131 */
2132 while (bdp->status & BD_SC_READY);
2133
2134 /* Send the character out.
2135 */
2136 cp = bdp->buf;
2137 *cp = *s;
2138
2139 bdp->length = 1;
2140 bdp->status |= BD_SC_READY;
2141
2142 if (bdp->status & BD_SC_WRAP)
2143 bdp = bdbase;
2144 else
2145 bdp++;
2146
2147 /* if a LF, also do CR... */
2148 if (*s == 10) {
2149 while (bdp->status & BD_SC_READY);
2150 /* cp = __va(bdp->buf); */
2151 cp = bdp->buf;
2152 *cp = 13;
2153 bdp->length = 1;
2154 bdp->status |= BD_SC_READY;
2155
2156 if (bdp->status & BD_SC_WRAP) {
2157 bdp = bdbase;
2158 }
2159 else {
2160 bdp++;
2161 }
2162 }
2163 }
2164
2165 /*
2166 * Finally, Wait for transmitter & holding register to empty
2167 * and restore the IER
2168 */
2169 while (bdp->status & BD_SC_READY);
2170
2171 if (info)
2172 info->tx_cur = (QUICC_BD *)bdp;
2173}
2174
2175static void serial_console_write(struct console *c, const char *s,
2176 unsigned count)
2177{
2178#ifdef CONFIG_KGDB
2179 /* Try to let stub handle output. Returns true if it did. */
2180 if (kgdb_output_string(s, count))
2181 return;
2182#endif
2183 my_console_write(c->index, s, count);
2184}
2185
2186
2187
2188/*void console_print_68360(const char *p)
2189{
2190 const char *cp = p;
2191 int i;
2192
2193 for (i=0;cp[i]!=0;i++);
2194
2195 serial_console_write (p, i);
2196
2197 //Comment this if you want to have a strict interrupt-driven output
2198 //rs_fair_output();
2199
2200 return;
2201}*/
2202
2203
2204
2205
2206
2207
2208#ifdef CONFIG_XMON
2209int
2210xmon_360_write(const char *s, unsigned count)
2211{
2212 my_console_write(0, s, count);
2213 return(count);
2214}
2215#endif
2216
2217#ifdef CONFIG_KGDB
2218void
2219putDebugChar(char ch)
2220{
2221 my_console_write(0, &ch, 1);
2222}
2223#endif
2224
2225/*
2226 * Receive character from the serial port. This only works well
2227 * before the port is initialized for real use.
2228 */
2229static int my_console_wait_key(int idx, int xmon, char *obuf)
2230{
2231 struct serial_state *ser;
2232 u_char c, *cp;
2233 ser_info_t *info;
2234 QUICC_BD *bdp;
2235 volatile struct smc_uart_pram *up;
2236 int i;
2237
2238 ser = rs_table + idx;
2239
2240 /* Get the address of the host memory buffer.
2241 * If the port has been initialized for general use, we must
2242 * use information from the port structure.
2243 */
2244 if ((info = (ser_info_t *)ser->info))
2245 bdp = info->rx_cur;
2246 else
2247 /* bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase]; */
2248 bdp = (QUICC_BD *)((uint)pquicc + (uint)up->tbase);
2249
2250 /* Pointer to UART in parameter ram.
2251 */
2252 /* up = (smc_uart_t *)&cpmp->cp_dparam[ser->port]; */
2253 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2254
2255 /*
2256 * We need to gracefully shut down the receiver, disable
2257 * interrupts, then read the input.
2258 * XMON just wants a poll. If no character, return -1, else
2259 * return the character.
2260 */
2261 if (!xmon) {
2262 while (bdp->status & BD_SC_EMPTY);
2263 }
2264 else {
2265 if (bdp->status & BD_SC_EMPTY)
2266 return -1;
2267 }
2268
2269 cp = (char *)bdp->buf;
2270
2271 if (obuf) {
2272 i = c = bdp->length;
2273 while (i-- > 0)
2274 *obuf++ = *cp++;
2275 }
2276 else {
2277 c = *cp;
2278 }
2279 bdp->status |= BD_SC_EMPTY;
2280
2281 if (info) {
2282 if (bdp->status & BD_SC_WRAP) {
2283 bdp = info->rx_bd_base;
2284 }
2285 else {
2286 bdp++;
2287 }
2288 info->rx_cur = (QUICC_BD *)bdp;
2289 }
2290
2291 return((int)c);
2292}
2293
2294static int serial_console_wait_key(struct console *co)
2295{
2296 return(my_console_wait_key(co->index, 0, NULL));
2297}
2298
2299#ifdef CONFIG_XMON
2300int
2301xmon_360_read_poll(void)
2302{
2303 return(my_console_wait_key(0, 1, NULL));
2304}
2305
2306int
2307xmon_360_read_char(void)
2308{
2309 return(my_console_wait_key(0, 0, NULL));
2310}
2311#endif
2312
2313#ifdef CONFIG_KGDB
2314static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2315static int kgdb_chars;
2316
2317unsigned char
2318getDebugChar(void)
2319{
2320 if (kgdb_chars <= 0) {
2321 kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2322 kgdp = kgdb_buf;
2323 }
2324 kgdb_chars--;
2325
2326 return(*kgdp++);
2327}
2328
2329void kgdb_interruptible(int state)
2330{
2331}
2332void kgdb_map_scc(void)
2333{
2334 struct serial_state *ser;
2335 uint mem_addr;
2336 volatile QUICC_BD *bdp;
2337 volatile smc_uart_t *up;
2338
2339 cpmp = (cpm360_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2340
2341 /* To avoid data cache CPM DMA coherency problems, allocate a
2342 * buffer in the CPM DPRAM. This will work until the CPM and
2343 * serial ports are initialized. At that time a memory buffer
2344 * will be allocated.
2345 * The port is already initialized from the boot procedure, all
2346 * we do here is give it a different buffer and make it a FIFO.
2347 */
2348
2349 ser = rs_table;
2350
2351 /* Right now, assume we are using SMCs.
2352 */
2353 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2354
2355 /* Allocate space for an input FIFO, plus a few bytes for output.
2356 * Allocate bytes to maintain word alignment.
2357 */
2358 mem_addr = (uint)(&cpmp->cp_dpmem[0x1000]);
2359
2360 /* Set the physical address of the host memory buffers in
2361 * the buffer descriptors.
2362 */
2363 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_rbase];
2364 bdp->buf = mem_addr;
2365
2366 bdp = (QUICC_BD *)&cpmp->cp_dpmem[up->smc_tbase];
2367 bdp->buf = mem_addr+RX_BUF_SIZE;
2368
2369 up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */
2370 up->smc_maxidl = RX_BUF_SIZE;
2371}
2372#endif
2373
2374static struct tty_struct *serial_console_device(struct console *c, int *index)
2375{
2376 *index = c->index;
2377 return serial_driver;
2378}
2379
2380
2381struct console sercons = {
2382 .name = "ttyS",
2383 .write = serial_console_write,
2384 .device = serial_console_device,
2385 .wait_key = serial_console_wait_key,
2386 .setup = serial_console_setup,
2387 .flags = CON_PRINTBUFFER,
2388 .index = CONFIG_SERIAL_CONSOLE_PORT,
2389};
2390
2391
2392
2393/*
2394 * Register console.
2395 */
2396long console_360_init(long kmem_start, long kmem_end)
2397{
2398 register_console(&sercons);
2399 /*register_console (console_print_68360); - 2.0.38 only required a write
2400 function pointer. */
2401 return kmem_start;
2402}
2403
2404#endif
2405
2406/* Index in baud rate table of the default console baud rate.
2407*/
2408static int baud_idx;
2409
2410static const struct tty_operations rs_360_ops = {
2411 .owner = THIS_MODULE,
2412 .open = rs_360_open,
2413 .close = rs_360_close,
2414 .write = rs_360_write,
2415 .put_char = rs_360_put_char,
2416 .write_room = rs_360_write_room,
2417 .chars_in_buffer = rs_360_chars_in_buffer,
2418 .flush_buffer = rs_360_flush_buffer,
2419 .ioctl = rs_360_ioctl,
2420 .throttle = rs_360_throttle,
2421 .unthrottle = rs_360_unthrottle,
2422 /* .send_xchar = rs_360_send_xchar, */
2423 .set_termios = rs_360_set_termios,
2424 .stop = rs_360_stop,
2425 .start = rs_360_start,
2426 .hangup = rs_360_hangup,
2427 /* .wait_until_sent = rs_360_wait_until_sent, */
2428 /* .read_proc = rs_360_read_proc, */
2429 .tiocmget = rs_360_tiocmget,
2430 .tiocmset = rs_360_tiocmset,
2431 .get_icount = rs_360_get_icount,
2432};
2433
2434static int __init rs_360_init(void)
2435{
2436 struct serial_state * state;
2437 ser_info_t *info;
2438 void *mem_addr;
2439 uint dp_addr, iobits;
2440 int i, j, idx;
2441 ushort chan;
2442 QUICC_BD *bdp;
2443 volatile QUICC *cp;
2444 volatile struct smc_regs *sp;
2445 volatile struct smc_uart_pram *up;
2446 volatile struct scc_regs *scp;
2447 volatile struct uart_pram *sup;
2448 /* volatile immap_t *immap; */
2449
2450 serial_driver = alloc_tty_driver(NR_PORTS);
2451 if (!serial_driver)
2452 return -1;
2453
2454 show_serial_version();
2455
2456 serial_driver->name = "ttyS";
2457 serial_driver->major = TTY_MAJOR;
2458 serial_driver->minor_start = 64;
2459 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2460 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2461 serial_driver->init_termios = tty_std_termios;
2462 serial_driver->init_termios.c_cflag =
2463 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2464 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2465 tty_set_operations(serial_driver, &rs_360_ops);
2466
2467 if (tty_register_driver(serial_driver))
2468 panic("Couldn't register serial driver\n");
2469
2470 cp = pquicc; /* Get pointer to Communication Processor */
2471 /* immap = (immap_t *)IMAP_ADDR; */ /* and to internal registers */
2472
2473
2474 /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2475 */
2476 /* The "standard" configuration through the 860.
2477 */
2478/* immap->im_ioport.iop_papar |= 0x00fc; */
2479/* immap->im_ioport.iop_padir &= ~0x00fc; */
2480/* immap->im_ioport.iop_paodr &= ~0x00fc; */
2481 cp->pio_papar |= 0x00fc;
2482 cp->pio_padir &= ~0x00fc;
2483 /* cp->pio_paodr &= ~0x00fc; */
2484
2485
2486 /* Since we don't yet do modem control, connect the port C pins
2487 * as general purpose I/O. This will assert CTS and CD for the
2488 * SCC ports.
2489 */
2490 /* FIXME: see 360um p.7-365 and 860um p.34-12
2491 * I can't make sense of these bits - mleslie*/
2492/* immap->im_ioport.iop_pcdir |= 0x03c6; */
2493/* immap->im_ioport.iop_pcpar &= ~0x03c6; */
2494
2495/* cp->pio_pcdir |= 0x03c6; */
2496/* cp->pio_pcpar &= ~0x03c6; */
2497
2498
2499
2500 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2501 * BRG4 to SCC3.
2502 */
2503 cp->si_sicr &= ~0x00ffff00;
2504 cp->si_sicr |= 0x001b1200;
2505
2506#ifdef CONFIG_PP04
2507 /* Frequentis PP04 forced to RS-232 until we know better.
2508 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2509 */
2510 immap->im_ioport.iop_pcdir |= 0x000c;
2511 immap->im_ioport.iop_pcpar &= ~0x000c;
2512 immap->im_ioport.iop_pcdat &= ~0x000c;
2513
2514 /* This enables the TX driver.
2515 */
2516 cp->cp_pbpar &= ~0x6000;
2517 cp->cp_pbdat &= ~0x6000;
2518#endif
2519
2520 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2521 state->magic = SSTATE_MAGIC;
2522 state->line = i;
2523 state->type = PORT_UNKNOWN;
2524 state->custom_divisor = 0;
2525 state->close_delay = 5*HZ/10;
2526 state->closing_wait = 30*HZ;
2527 state->icount.cts = state->icount.dsr =
2528 state->icount.rng = state->icount.dcd = 0;
2529 state->icount.rx = state->icount.tx = 0;
2530 state->icount.frame = state->icount.parity = 0;
2531 state->icount.overrun = state->icount.brk = 0;
2532 printk(KERN_INFO "ttyS%d at irq 0x%02x is an %s\n",
2533 i, (unsigned int)(state->irq),
2534 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");
2535
2536#ifdef CONFIG_SERIAL_CONSOLE
2537 /* If we just printed the message on the console port, and
2538 * we are about to initialize it for general use, we have
2539 * to wait a couple of character times for the CR/NL to
2540 * make it out of the transmit buffer.
2541 */
2542 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2543 mdelay(8);
2544
2545
2546/* idx = PORT_NUM(info->state->smc_scc_num); */
2547/* if (info->state->smc_scc_num & NUM_IS_SCC) */
2548/* chan = scc_chan_map[idx]; */
2549/* else */
2550/* chan = smc_chan_map[idx]; */
2551
2552/* cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG; */
2553/* while (cp->cp_cr & CPM_CR_FLG); */
2554
2555#endif
2556 /* info = kmalloc(sizeof(ser_info_t), GFP_KERNEL); */
2557 info = &quicc_ser_info[i];
2558 if (info) {
2559 memset (info, 0, sizeof(ser_info_t));
2560 info->magic = SERIAL_MAGIC;
2561 info->line = i;
2562 info->flags = state->flags;
2563 INIT_WORK(&info->tqueue, do_softint, info);
2564 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2565 init_waitqueue_head(&info->open_wait);
2566 init_waitqueue_head(&info->close_wait);
2567 info->state = state;
2568 state->info = (struct async_struct *)info;
2569
2570 /* We need to allocate a transmit and receive buffer
2571 * descriptors from dual port ram, and a character
2572 * buffer area from host mem.
2573 */
2574 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * RX_NUM_FIFO);
2575
2576 /* Allocate space for FIFOs in the host memory.
2577 * (for now this is from a static array of buffers :(
2578 */
2579 /* mem_addr = m360_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE); */
2580 /* mem_addr = kmalloc (RX_NUM_FIFO * RX_BUF_SIZE, GFP_BUFFER); */
2581 mem_addr = &rx_buf_pool[i * RX_NUM_FIFO * RX_BUF_SIZE];
2582
2583 /* Set the physical address of the host memory
2584 * buffers in the buffer descriptors, and the
2585 * virtual address for us to work with.
2586 */
2587 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2588 info->rx_cur = info->rx_bd_base = bdp;
2589
2590 /* initialize rx buffer descriptors */
2591 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2592 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2593 bdp->status = BD_SC_EMPTY | BD_SC_INTRPT;
2594 mem_addr += RX_BUF_SIZE;
2595 bdp++;
2596 }
2597 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2598 bdp->status = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2599
2600
2601 idx = PORT_NUM(info->state->smc_scc_num);
2602 if (info->state->smc_scc_num & NUM_IS_SCC) {
2603
2604#if defined (CONFIG_UCQUICC) && 1
2605 /* set the transceiver mode to RS232 */
2606 sipex_mode_bits &= ~(uint)SIPEX_MODE(idx,0x0f); /* clear current mode */
2607 sipex_mode_bits |= (uint)SIPEX_MODE(idx,0x02);
2608 *(uint *)_periph_base = sipex_mode_bits;
2609 /* printk ("sipex bits = 0x%08x\n", sipex_mode_bits); */
2610#endif
2611 }
2612
2613 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * TX_NUM_FIFO);
2614
2615 /* Allocate space for FIFOs in the host memory.
2616 */
2617 /* mem_addr = m360_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE); */
2618 /* mem_addr = kmalloc (TX_NUM_FIFO * TX_BUF_SIZE, GFP_BUFFER); */
2619 mem_addr = &tx_buf_pool[i * TX_NUM_FIFO * TX_BUF_SIZE];
2620
2621 /* Set the physical address of the host memory
2622 * buffers in the buffer descriptors, and the
2623 * virtual address for us to work with.
2624 */
2625 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2626 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2627 info->tx_cur = info->tx_bd_base = (QUICC_BD *)bdp;
2628
2629 /* initialize tx buffer descriptors */
2630 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2631 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2632 bdp->status = BD_SC_INTRPT;
2633 mem_addr += TX_BUF_SIZE;
2634 bdp++;
2635 }
2636 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2637 bdp->status = (BD_SC_WRAP | BD_SC_INTRPT);
2638
2639 if (info->state->smc_scc_num & NUM_IS_SCC) {
2640 scp = &pquicc->scc_regs[idx];
2641 sup = &pquicc->pram[info->state->port].scc.pscc.u;
2642 sup->rbase = dp_addr;
2643 sup->tbase = dp_addr;
2644
2645 /* Set up the uart parameters in the
2646 * parameter ram.
2647 */
2648 sup->rfcr = SMC_EB;
2649 sup->tfcr = SMC_EB;
2650
2651 /* Set this to 1 for now, so we get single
2652 * character interrupts. Using idle character
2653 * time requires some additional tuning.
2654 */
2655 sup->mrblr = 1;
2656 sup->max_idl = 0;
2657 sup->brkcr = 1;
2658 sup->parec = 0;
2659 sup->frmer = 0;
2660 sup->nosec = 0;
2661 sup->brkec = 0;
2662 sup->uaddr1 = 0;
2663 sup->uaddr2 = 0;
2664 sup->toseq = 0;
2665 {
2666 int i;
2667 for (i=0;i<8;i++)
2668 sup->cc[i] = 0x8000;
2669 }
2670 sup->rccm = 0xc0ff;
2671
2672 /* Send the CPM an initialize command.
2673 */
2674 chan = scc_chan_map[idx];
2675
2676 /* execute the INIT RX & TX PARAMS command for this channel. */
2677 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2678 while (cp->cp_cr & CPM_CR_FLG);
2679
2680 /* Set UART mode, 8 bit, no parity, one stop.
2681 * Enable receive and transmit.
2682 */
2683 scp->scc_gsmr.w.high = 0;
2684 scp->scc_gsmr.w.low =
2685 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2686
2687 /* Disable all interrupts and clear all pending
2688 * events.
2689 */
2690 scp->scc_sccm = 0;
2691 scp->scc_scce = 0xffff;
2692 scp->scc_dsr = 0x7e7e;
2693 scp->scc_psmr = 0x3000;
2694
2695 /* If the port is the console, enable Rx and Tx.
2696 */
2697#ifdef CONFIG_SERIAL_CONSOLE
2698 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2699 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2700#endif
2701 }
2702 else {
2703 /* Configure SMCs Tx/Rx instead of port B
2704 * parallel I/O.
2705 */
2706 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2707 up->rbase = dp_addr;
2708
2709 iobits = 0xc0 << (idx * 4);
2710 cp->pip_pbpar |= iobits;
2711 cp->pip_pbdir &= ~iobits;
2712 cp->pip_pbodr &= ~iobits;
2713
2714
2715 /* Connect the baud rate generator to the
2716 * SMC based upon index in rs_table. Also
2717 * make sure it is connected to NMSI.
2718 */
2719 cp->si_simode &= ~(0xffff << (idx * 16));
2720 cp->si_simode |= (i << ((idx * 16) + 12));
2721
2722 up->tbase = dp_addr;
2723
2724 /* Set up the uart parameters in the
2725 * parameter ram.
2726 */
2727 up->rfcr = SMC_EB;
2728 up->tfcr = SMC_EB;
2729
2730 /* Set this to 1 for now, so we get single
2731 * character interrupts. Using idle character
2732 * time requires some additional tuning.
2733 */
2734 up->mrblr = 1;
2735 up->max_idl = 0;
2736 up->brkcr = 1;
2737
2738 /* Send the CPM an initialize command.
2739 */
2740 chan = smc_chan_map[idx];
2741
2742 cp->cp_cr = mk_cr_cmd(chan,
2743 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2744#ifdef CONFIG_SERIAL_CONSOLE
2745 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2746 printk("");
2747#endif
2748 while (cp->cp_cr & CPM_CR_FLG);
2749
2750 /* Set UART mode, 8 bit, no parity, one stop.
2751 * Enable receive and transmit.
2752 */
2753 sp = &cp->smc_regs[idx];
2754 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2755
2756 /* Disable all interrupts and clear all pending
2757 * events.
2758 */
2759 sp->smc_smcm = 0;
2760 sp->smc_smce = 0xff;
2761
2762 /* If the port is the console, enable Rx and Tx.
2763 */
2764#ifdef CONFIG_SERIAL_CONSOLE
2765 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2766 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2767#endif
2768 }
2769
2770 /* Install interrupt handler.
2771 */
2772 /* cpm_install_handler(IRQ_MACHSPEC | state->irq, rs_360_interrupt, info); */
2773 /*request_irq(IRQ_MACHSPEC | state->irq, rs_360_interrupt, */
2774 request_irq(state->irq, rs_360_interrupt,
2775 IRQ_FLG_LOCK, "ttyS", (void *)info);
2776
2777 /* Set up the baud rate generator.
2778 */
2779 m360_cpm_setbrg(i, baud_table[baud_idx]);
2780
2781 }
2782 }
2783
2784 return 0;
2785}
2786module_init(rs_360_init);
2787
2788/* This must always be called before the rs_360_init() function, otherwise
2789 * it blows away the port control information.
2790 */
2791//static int __init serial_console_setup( struct console *co, char *options)
2792int serial_console_setup( struct console *co, char *options)
2793{
2794 struct serial_state *ser;
2795 uint mem_addr, dp_addr, bidx, idx, iobits;
2796 ushort chan;
2797 QUICC_BD *bdp;
2798 volatile QUICC *cp;
2799 volatile struct smc_regs *sp;
2800 volatile struct scc_regs *scp;
2801 volatile struct smc_uart_pram *up;
2802 volatile struct uart_pram *sup;
2803
2804/* mleslie TODO:
2805 * add something to the 68k bootloader to store a desired initial console baud rate */
2806
2807/* bd_t *bd; */ /* a board info struct used by EPPC-bug */
2808/* bd = (bd_t *)__res; */
2809
2810 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2811 /* if (bd->bi_baudrate == baud_table[bidx]) */
2812 if (CONSOLE_BAUDRATE == baud_table[bidx])
2813 break;
2814
2815 /* co->cflag = CREAD|CLOCAL|bidx|CS8; */
2816 baud_idx = bidx;
2817
2818 ser = rs_table + CONFIG_SERIAL_CONSOLE_PORT;
2819
2820 cp = pquicc; /* Get pointer to Communication Processor */
2821
2822 idx = PORT_NUM(ser->smc_scc_num);
2823 if (ser->smc_scc_num & NUM_IS_SCC) {
2824
2825 /* TODO: need to set up SCC pin assignment etc. here */
2826
2827 }
2828 else {
2829 iobits = 0xc0 << (idx * 4);
2830 cp->pip_pbpar |= iobits;
2831 cp->pip_pbdir &= ~iobits;
2832 cp->pip_pbodr &= ~iobits;
2833
2834 /* Connect the baud rate generator to the
2835 * SMC based upon index in rs_table. Also
2836 * make sure it is connected to NMSI.
2837 */
2838 cp->si_simode &= ~(0xffff << (idx * 16));
2839 cp->si_simode |= (idx << ((idx * 16) + 12));
2840 }
2841
2842 /* When we get here, the CPM has been reset, so we need
2843 * to configure the port.
2844 * We need to allocate a transmit and receive buffer descriptor
2845 * from dual port ram, and a character buffer area from host mem.
2846 */
2847
2848 /* Allocate space for two buffer descriptors in the DP ram.
2849 */
2850 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * CONSOLE_NUM_FIFO);
2851
2852 /* Allocate space for two 2 byte FIFOs in the host memory.
2853 */
2854 /* mem_addr = m360_cpm_hostalloc(8); */
2855 mem_addr = (uint)console_fifos;
2856
2857
2858 /* Set the physical address of the host memory buffers in
2859 * the buffer descriptors.
2860 */
2861 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2862 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2863 bdp->buf = (char *)mem_addr;
2864 (bdp+1)->buf = (char *)(mem_addr+4);
2865
2866 /* For the receive, set empty and wrap.
2867 * For transmit, set wrap.
2868 */
2869 bdp->status = BD_SC_EMPTY | BD_SC_WRAP;
2870 (bdp+1)->status = BD_SC_WRAP;
2871
2872 /* Set up the uart parameters in the parameter ram.
2873 */
2874 if (ser->smc_scc_num & NUM_IS_SCC) {
2875 scp = &cp->scc_regs[idx];
2876 /* sup = (scc_uart_t *)&cp->cp_dparam[ser->port]; */
2877 sup = &pquicc->pram[ser->port].scc.pscc.u;
2878
2879 sup->rbase = dp_addr;
2880 sup->tbase = dp_addr + sizeof(QUICC_BD);
2881
2882 /* Set up the uart parameters in the
2883 * parameter ram.
2884 */
2885 sup->rfcr = SMC_EB;
2886 sup->tfcr = SMC_EB;
2887
2888 /* Set this to 1 for now, so we get single
2889 * character interrupts. Using idle character
2890 * time requires some additional tuning.
2891 */
2892 sup->mrblr = 1;
2893 sup->max_idl = 0;
2894 sup->brkcr = 1;
2895 sup->parec = 0;
2896 sup->frmer = 0;
2897 sup->nosec = 0;
2898 sup->brkec = 0;
2899 sup->uaddr1 = 0;
2900 sup->uaddr2 = 0;
2901 sup->toseq = 0;
2902 {
2903 int i;
2904 for (i=0;i<8;i++)
2905 sup->cc[i] = 0x8000;
2906 }
2907 sup->rccm = 0xc0ff;
2908
2909 /* Send the CPM an initialize command.
2910 */
2911 chan = scc_chan_map[idx];
2912
2913 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2914 while (cp->cp_cr & CPM_CR_FLG);
2915
2916 /* Set UART mode, 8 bit, no parity, one stop.
2917 * Enable receive and transmit.
2918 */
2919 scp->scc_gsmr.w.high = 0;
2920 scp->scc_gsmr.w.low =
2921 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2922
2923 /* Disable all interrupts and clear all pending
2924 * events.
2925 */
2926 scp->scc_sccm = 0;
2927 scp->scc_scce = 0xffff;
2928 scp->scc_dsr = 0x7e7e;
2929 scp->scc_psmr = 0x3000;
2930
2931 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2932
2933 }
2934 else {
2935 /* up = (smc_uart_t *)&cp->cp_dparam[ser->port]; */
2936 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2937
2938 up->rbase = dp_addr; /* Base of receive buffer desc. */
2939 up->tbase = dp_addr+sizeof(QUICC_BD); /* Base of xmt buffer desc. */
2940 up->rfcr = SMC_EB;
2941 up->tfcr = SMC_EB;
2942
2943 /* Set this to 1 for now, so we get single character interrupts.
2944 */
2945 up->mrblr = 1; /* receive buffer length */
2946 up->max_idl = 0; /* wait forever for next char */
2947
2948 /* Send the CPM an initialize command.
2949 */
2950 chan = smc_chan_map[idx];
2951 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2952 while (cp->cp_cr & CPM_CR_FLG);
2953
2954 /* Set UART mode, 8 bit, no parity, one stop.
2955 * Enable receive and transmit.
2956 */
2957 sp = &cp->smc_regs[idx];
2958 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2959
2960 /* And finally, enable Rx and Tx.
2961 */
2962 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2963 }
2964
2965 /* Set up the baud rate generator.
2966 */
2967 /* m360_cpm_setbrg((ser - rs_table), bd->bi_baudrate); */
2968 m360_cpm_setbrg((ser - rs_table), CONSOLE_BAUDRATE);
2969
2970 return 0;
2971}
2972
2973/*
2974 * Local variables:
2975 * c-indent-level: 4
2976 * c-basic-offset: 4
2977 * tab-width: 4
2978 * End:
2979 */
diff --git a/drivers/tty/serial/8250.c b/drivers/tty/serial/8250.c
new file mode 100644
index 000000000000..b4129f53fb1b
--- /dev/null
+++ b/drivers/tty/serial/8250.c
@@ -0,0 +1,3424 @@
1/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port.
16 * membase is an 'ioremapped' cookie.
17 */
18
19#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20#define SUPPORT_SYSRQ
21#endif
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/delay.h>
30#include <linux/platform_device.h>
31#include <linux/tty.h>
32#include <linux/ratelimit.h>
33#include <linux/tty_flip.h>
34#include <linux/serial_reg.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/serial_8250.h>
38#include <linux/nmi.h>
39#include <linux/mutex.h>
40#include <linux/slab.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
47#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
51/*
52 * Configuration:
53 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
54 * is unsafe when used on edge-triggered interrupts.
55 */
56static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
57
58static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
60static struct uart_driver serial8250_reg;
61
62static int serial_index(struct uart_port *port)
63{
64 return (serial8250_reg.minor - 64) + port->line;
65}
66
67static unsigned int skip_txen_test; /* force skip of txen test at init time */
68
69/*
70 * Debugging.
71 */
72#if 0
73#define DEBUG_AUTOCONF(fmt...) printk(fmt)
74#else
75#define DEBUG_AUTOCONF(fmt...) do { } while (0)
76#endif
77
78#if 0
79#define DEBUG_INTR(fmt...) printk(fmt)
80#else
81#define DEBUG_INTR(fmt...) do { } while (0)
82#endif
83
84#define PASS_LIMIT 256
85
86#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
87
88
89/*
90 * We default to IRQ0 for the "no irq" hack. Some
91 * machine types want others as well - they're free
92 * to redefine this in their header file.
93 */
94#define is_real_interrupt(irq) ((irq) != 0)
95
96#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
97#define CONFIG_SERIAL_DETECT_IRQ 1
98#endif
99#ifdef CONFIG_SERIAL_8250_MANY_PORTS
100#define CONFIG_SERIAL_MANY_PORTS 1
101#endif
102
103/*
104 * HUB6 is always on. This will be removed once the header
105 * files have been cleaned.
106 */
107#define CONFIG_HUB6 1
108
109#include <asm/serial.h>
110/*
111 * SERIAL_PORT_DFNS tells us about built-in ports that have no
112 * standard enumeration mechanism. Platforms that can find all
113 * serial ports via mechanisms like ACPI or PCI need not supply it.
114 */
115#ifndef SERIAL_PORT_DFNS
116#define SERIAL_PORT_DFNS
117#endif
118
119static const struct old_serial_port old_serial_port[] = {
120 SERIAL_PORT_DFNS /* defined in asm/serial.h */
121};
122
123#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
124
125#ifdef CONFIG_SERIAL_8250_RSA
126
127#define PORT_RSA_MAX 4
128static unsigned long probe_rsa[PORT_RSA_MAX];
129static unsigned int probe_rsa_count;
130#endif /* CONFIG_SERIAL_8250_RSA */
131
132struct uart_8250_port {
133 struct uart_port port;
134 struct timer_list timer; /* "no irq" timer */
135 struct list_head list; /* ports on this IRQ */
136 unsigned short capabilities; /* port capabilities */
137 unsigned short bugs; /* port bugs */
138 unsigned int tx_loadsz; /* transmit fifo load size */
139 unsigned char acr;
140 unsigned char ier;
141 unsigned char lcr;
142 unsigned char mcr;
143 unsigned char mcr_mask; /* mask of user bits */
144 unsigned char mcr_force; /* mask of forced bits */
145 unsigned char cur_iotype; /* Running I/O type */
146
147 /*
148 * Some bits in registers are cleared on a read, so they must
149 * be saved whenever the register is read but the bits will not
150 * be immediately processed.
151 */
152#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
153 unsigned char lsr_saved_flags;
154#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
155 unsigned char msr_saved_flags;
156};
157
158struct irq_info {
159 struct hlist_node node;
160 int irq;
161 spinlock_t lock; /* Protects list not the hash */
162 struct list_head *head;
163};
164
165#define NR_IRQ_HASH 32 /* Can be adjusted later */
166static struct hlist_head irq_lists[NR_IRQ_HASH];
167static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
168
169/*
170 * Here we define the default xmit fifo size used for each type of UART.
171 */
172static const struct serial8250_config uart_config[] = {
173 [PORT_UNKNOWN] = {
174 .name = "unknown",
175 .fifo_size = 1,
176 .tx_loadsz = 1,
177 },
178 [PORT_8250] = {
179 .name = "8250",
180 .fifo_size = 1,
181 .tx_loadsz = 1,
182 },
183 [PORT_16450] = {
184 .name = "16450",
185 .fifo_size = 1,
186 .tx_loadsz = 1,
187 },
188 [PORT_16550] = {
189 .name = "16550",
190 .fifo_size = 1,
191 .tx_loadsz = 1,
192 },
193 [PORT_16550A] = {
194 .name = "16550A",
195 .fifo_size = 16,
196 .tx_loadsz = 16,
197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
198 .flags = UART_CAP_FIFO,
199 },
200 [PORT_CIRRUS] = {
201 .name = "Cirrus",
202 .fifo_size = 1,
203 .tx_loadsz = 1,
204 },
205 [PORT_16650] = {
206 .name = "ST16650",
207 .fifo_size = 1,
208 .tx_loadsz = 1,
209 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
210 },
211 [PORT_16650V2] = {
212 .name = "ST16650V2",
213 .fifo_size = 32,
214 .tx_loadsz = 16,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
216 UART_FCR_T_TRIG_00,
217 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
218 },
219 [PORT_16750] = {
220 .name = "TI16750",
221 .fifo_size = 64,
222 .tx_loadsz = 64,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
224 UART_FCR7_64BYTE,
225 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
226 },
227 [PORT_STARTECH] = {
228 .name = "Startech",
229 .fifo_size = 1,
230 .tx_loadsz = 1,
231 },
232 [PORT_16C950] = {
233 .name = "16C950/954",
234 .fifo_size = 128,
235 .tx_loadsz = 128,
236 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
238 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
239 },
240 [PORT_16654] = {
241 .name = "ST16654",
242 .fifo_size = 64,
243 .tx_loadsz = 32,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
245 UART_FCR_T_TRIG_10,
246 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
247 },
248 [PORT_16850] = {
249 .name = "XR16850",
250 .fifo_size = 128,
251 .tx_loadsz = 128,
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
254 },
255 [PORT_RSA] = {
256 .name = "RSA",
257 .fifo_size = 2048,
258 .tx_loadsz = 2048,
259 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
260 .flags = UART_CAP_FIFO,
261 },
262 [PORT_NS16550A] = {
263 .name = "NS16550A",
264 .fifo_size = 16,
265 .tx_loadsz = 16,
266 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
267 .flags = UART_CAP_FIFO | UART_NATSEMI,
268 },
269 [PORT_XSCALE] = {
270 .name = "XScale",
271 .fifo_size = 32,
272 .tx_loadsz = 32,
273 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
274 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
275 },
276 [PORT_RM9000] = {
277 .name = "RM9000",
278 .fifo_size = 16,
279 .tx_loadsz = 16,
280 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
281 .flags = UART_CAP_FIFO,
282 },
283 [PORT_OCTEON] = {
284 .name = "OCTEON",
285 .fifo_size = 64,
286 .tx_loadsz = 64,
287 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
288 .flags = UART_CAP_FIFO,
289 },
290 [PORT_AR7] = {
291 .name = "AR7",
292 .fifo_size = 16,
293 .tx_loadsz = 16,
294 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
295 .flags = UART_CAP_FIFO | UART_CAP_AFE,
296 },
297 [PORT_U6_16550A] = {
298 .name = "U6_16550A",
299 .fifo_size = 64,
300 .tx_loadsz = 64,
301 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
302 .flags = UART_CAP_FIFO | UART_CAP_AFE,
303 },
304 [PORT_TEGRA] = {
305 .name = "Tegra",
306 .fifo_size = 32,
307 .tx_loadsz = 8,
308 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
309 UART_FCR_T_TRIG_01,
310 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
311 },
312};
313
314#if defined(CONFIG_MIPS_ALCHEMY)
315
316/* Au1x00 UART hardware has a weird register layout */
317static const u8 au_io_in_map[] = {
318 [UART_RX] = 0,
319 [UART_IER] = 2,
320 [UART_IIR] = 3,
321 [UART_LCR] = 5,
322 [UART_MCR] = 6,
323 [UART_LSR] = 7,
324 [UART_MSR] = 8,
325};
326
327static const u8 au_io_out_map[] = {
328 [UART_TX] = 1,
329 [UART_IER] = 2,
330 [UART_FCR] = 4,
331 [UART_LCR] = 5,
332 [UART_MCR] = 6,
333};
334
335/* sane hardware needs no mapping */
336static inline int map_8250_in_reg(struct uart_port *p, int offset)
337{
338 if (p->iotype != UPIO_AU)
339 return offset;
340 return au_io_in_map[offset];
341}
342
343static inline int map_8250_out_reg(struct uart_port *p, int offset)
344{
345 if (p->iotype != UPIO_AU)
346 return offset;
347 return au_io_out_map[offset];
348}
349
350#elif defined(CONFIG_SERIAL_8250_RM9K)
351
352static const u8
353 regmap_in[8] = {
354 [UART_RX] = 0x00,
355 [UART_IER] = 0x0c,
356 [UART_IIR] = 0x14,
357 [UART_LCR] = 0x1c,
358 [UART_MCR] = 0x20,
359 [UART_LSR] = 0x24,
360 [UART_MSR] = 0x28,
361 [UART_SCR] = 0x2c
362 },
363 regmap_out[8] = {
364 [UART_TX] = 0x04,
365 [UART_IER] = 0x0c,
366 [UART_FCR] = 0x18,
367 [UART_LCR] = 0x1c,
368 [UART_MCR] = 0x20,
369 [UART_LSR] = 0x24,
370 [UART_MSR] = 0x28,
371 [UART_SCR] = 0x2c
372 };
373
374static inline int map_8250_in_reg(struct uart_port *p, int offset)
375{
376 if (p->iotype != UPIO_RM9000)
377 return offset;
378 return regmap_in[offset];
379}
380
381static inline int map_8250_out_reg(struct uart_port *p, int offset)
382{
383 if (p->iotype != UPIO_RM9000)
384 return offset;
385 return regmap_out[offset];
386}
387
388#else
389
390/* sane hardware needs no mapping */
391#define map_8250_in_reg(up, offset) (offset)
392#define map_8250_out_reg(up, offset) (offset)
393
394#endif
395
396static unsigned int hub6_serial_in(struct uart_port *p, int offset)
397{
398 offset = map_8250_in_reg(p, offset) << p->regshift;
399 outb(p->hub6 - 1 + offset, p->iobase);
400 return inb(p->iobase + 1);
401}
402
403static void hub6_serial_out(struct uart_port *p, int offset, int value)
404{
405 offset = map_8250_out_reg(p, offset) << p->regshift;
406 outb(p->hub6 - 1 + offset, p->iobase);
407 outb(value, p->iobase + 1);
408}
409
410static unsigned int mem_serial_in(struct uart_port *p, int offset)
411{
412 offset = map_8250_in_reg(p, offset) << p->regshift;
413 return readb(p->membase + offset);
414}
415
416static void mem_serial_out(struct uart_port *p, int offset, int value)
417{
418 offset = map_8250_out_reg(p, offset) << p->regshift;
419 writeb(value, p->membase + offset);
420}
421
422static void mem32_serial_out(struct uart_port *p, int offset, int value)
423{
424 offset = map_8250_out_reg(p, offset) << p->regshift;
425 writel(value, p->membase + offset);
426}
427
428static unsigned int mem32_serial_in(struct uart_port *p, int offset)
429{
430 offset = map_8250_in_reg(p, offset) << p->regshift;
431 return readl(p->membase + offset);
432}
433
434static unsigned int au_serial_in(struct uart_port *p, int offset)
435{
436 offset = map_8250_in_reg(p, offset) << p->regshift;
437 return __raw_readl(p->membase + offset);
438}
439
440static void au_serial_out(struct uart_port *p, int offset, int value)
441{
442 offset = map_8250_out_reg(p, offset) << p->regshift;
443 __raw_writel(value, p->membase + offset);
444}
445
446static unsigned int tsi_serial_in(struct uart_port *p, int offset)
447{
448 unsigned int tmp;
449 offset = map_8250_in_reg(p, offset) << p->regshift;
450 if (offset == UART_IIR) {
451 tmp = readl(p->membase + (UART_IIR & ~3));
452 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
453 } else
454 return readb(p->membase + offset);
455}
456
457static void tsi_serial_out(struct uart_port *p, int offset, int value)
458{
459 offset = map_8250_out_reg(p, offset) << p->regshift;
460 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
461 writeb(value, p->membase + offset);
462}
463
464/* Save the LCR value so it can be re-written when a Busy Detect IRQ occurs. */
465static inline void dwapb_save_out_value(struct uart_port *p, int offset,
466 int value)
467{
468 struct uart_8250_port *up =
469 container_of(p, struct uart_8250_port, port);
470
471 if (offset == UART_LCR)
472 up->lcr = value;
473}
474
475/* Read the IER to ensure any interrupt is cleared before returning from ISR. */
476static inline void dwapb_check_clear_ier(struct uart_port *p, int offset)
477{
478 if (offset == UART_TX || offset == UART_IER)
479 p->serial_in(p, UART_IER);
480}
481
482static void dwapb_serial_out(struct uart_port *p, int offset, int value)
483{
484 int save_offset = offset;
485 offset = map_8250_out_reg(p, offset) << p->regshift;
486 dwapb_save_out_value(p, save_offset, value);
487 writeb(value, p->membase + offset);
488 dwapb_check_clear_ier(p, save_offset);
489}
490
491static void dwapb32_serial_out(struct uart_port *p, int offset, int value)
492{
493 int save_offset = offset;
494 offset = map_8250_out_reg(p, offset) << p->regshift;
495 dwapb_save_out_value(p, save_offset, value);
496 writel(value, p->membase + offset);
497 dwapb_check_clear_ier(p, save_offset);
498}
499
500static unsigned int io_serial_in(struct uart_port *p, int offset)
501{
502 offset = map_8250_in_reg(p, offset) << p->regshift;
503 return inb(p->iobase + offset);
504}
505
506static void io_serial_out(struct uart_port *p, int offset, int value)
507{
508 offset = map_8250_out_reg(p, offset) << p->regshift;
509 outb(value, p->iobase + offset);
510}
511
512static void set_io_from_upio(struct uart_port *p)
513{
514 struct uart_8250_port *up =
515 container_of(p, struct uart_8250_port, port);
516 switch (p->iotype) {
517 case UPIO_HUB6:
518 p->serial_in = hub6_serial_in;
519 p->serial_out = hub6_serial_out;
520 break;
521
522 case UPIO_MEM:
523 p->serial_in = mem_serial_in;
524 p->serial_out = mem_serial_out;
525 break;
526
527 case UPIO_RM9000:
528 case UPIO_MEM32:
529 p->serial_in = mem32_serial_in;
530 p->serial_out = mem32_serial_out;
531 break;
532
533 case UPIO_AU:
534 p->serial_in = au_serial_in;
535 p->serial_out = au_serial_out;
536 break;
537
538 case UPIO_TSI:
539 p->serial_in = tsi_serial_in;
540 p->serial_out = tsi_serial_out;
541 break;
542
543 case UPIO_DWAPB:
544 p->serial_in = mem_serial_in;
545 p->serial_out = dwapb_serial_out;
546 break;
547
548 case UPIO_DWAPB32:
549 p->serial_in = mem32_serial_in;
550 p->serial_out = dwapb32_serial_out;
551 break;
552
553 default:
554 p->serial_in = io_serial_in;
555 p->serial_out = io_serial_out;
556 break;
557 }
558 /* Remember loaded iotype */
559 up->cur_iotype = p->iotype;
560}
561
562static void
563serial_out_sync(struct uart_8250_port *up, int offset, int value)
564{
565 struct uart_port *p = &up->port;
566 switch (p->iotype) {
567 case UPIO_MEM:
568 case UPIO_MEM32:
569 case UPIO_AU:
570 case UPIO_DWAPB:
571 case UPIO_DWAPB32:
572 p->serial_out(p, offset, value);
573 p->serial_in(p, UART_LCR); /* safe, no side-effects */
574 break;
575 default:
576 p->serial_out(p, offset, value);
577 }
578}
579
580#define serial_in(up, offset) \
581 (up->port.serial_in(&(up)->port, (offset)))
582#define serial_out(up, offset, value) \
583 (up->port.serial_out(&(up)->port, (offset), (value)))
584/*
585 * We used to support using pause I/O for certain machines. We
586 * haven't supported this for a while, but just in case it's badly
587 * needed for certain old 386 machines, I've left these #define's
588 * in....
589 */
590#define serial_inp(up, offset) serial_in(up, offset)
591#define serial_outp(up, offset, value) serial_out(up, offset, value)
592
593/* Uart divisor latch read */
594static inline int _serial_dl_read(struct uart_8250_port *up)
595{
596 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
597}
598
599/* Uart divisor latch write */
600static inline void _serial_dl_write(struct uart_8250_port *up, int value)
601{
602 serial_outp(up, UART_DLL, value & 0xff);
603 serial_outp(up, UART_DLM, value >> 8 & 0xff);
604}
605
606#if defined(CONFIG_MIPS_ALCHEMY)
607/* Au1x00 haven't got a standard divisor latch */
608static int serial_dl_read(struct uart_8250_port *up)
609{
610 if (up->port.iotype == UPIO_AU)
611 return __raw_readl(up->port.membase + 0x28);
612 else
613 return _serial_dl_read(up);
614}
615
616static void serial_dl_write(struct uart_8250_port *up, int value)
617{
618 if (up->port.iotype == UPIO_AU)
619 __raw_writel(value, up->port.membase + 0x28);
620 else
621 _serial_dl_write(up, value);
622}
623#elif defined(CONFIG_SERIAL_8250_RM9K)
624static int serial_dl_read(struct uart_8250_port *up)
625{
626 return (up->port.iotype == UPIO_RM9000) ?
627 (((__raw_readl(up->port.membase + 0x10) << 8) |
628 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
629 _serial_dl_read(up);
630}
631
632static void serial_dl_write(struct uart_8250_port *up, int value)
633{
634 if (up->port.iotype == UPIO_RM9000) {
635 __raw_writel(value, up->port.membase + 0x08);
636 __raw_writel(value >> 8, up->port.membase + 0x10);
637 } else {
638 _serial_dl_write(up, value);
639 }
640}
641#else
642#define serial_dl_read(up) _serial_dl_read(up)
643#define serial_dl_write(up, value) _serial_dl_write(up, value)
644#endif
645
646/*
647 * For the 16C950
648 */
649static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
650{
651 serial_out(up, UART_SCR, offset);
652 serial_out(up, UART_ICR, value);
653}
654
655static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
656{
657 unsigned int value;
658
659 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
660 serial_out(up, UART_SCR, offset);
661 value = serial_in(up, UART_ICR);
662 serial_icr_write(up, UART_ACR, up->acr);
663
664 return value;
665}
666
667/*
668 * FIFO support.
669 */
670static void serial8250_clear_fifos(struct uart_8250_port *p)
671{
672 if (p->capabilities & UART_CAP_FIFO) {
673 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
674 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
675 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
676 serial_outp(p, UART_FCR, 0);
677 }
678}
679
680/*
681 * IER sleep support. UARTs which have EFRs need the "extended
682 * capability" bit enabled. Note that on XR16C850s, we need to
683 * reset LCR to write to IER.
684 */
685static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
686{
687 if (p->capabilities & UART_CAP_SLEEP) {
688 if (p->capabilities & UART_CAP_EFR) {
689 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
690 serial_outp(p, UART_EFR, UART_EFR_ECB);
691 serial_outp(p, UART_LCR, 0);
692 }
693 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
694 if (p->capabilities & UART_CAP_EFR) {
695 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
696 serial_outp(p, UART_EFR, 0);
697 serial_outp(p, UART_LCR, 0);
698 }
699 }
700}
701
702#ifdef CONFIG_SERIAL_8250_RSA
703/*
704 * Attempts to turn on the RSA FIFO. Returns zero on failure.
705 * We set the port uart clock rate if we succeed.
706 */
707static int __enable_rsa(struct uart_8250_port *up)
708{
709 unsigned char mode;
710 int result;
711
712 mode = serial_inp(up, UART_RSA_MSR);
713 result = mode & UART_RSA_MSR_FIFO;
714
715 if (!result) {
716 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
717 mode = serial_inp(up, UART_RSA_MSR);
718 result = mode & UART_RSA_MSR_FIFO;
719 }
720
721 if (result)
722 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
723
724 return result;
725}
726
727static void enable_rsa(struct uart_8250_port *up)
728{
729 if (up->port.type == PORT_RSA) {
730 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
731 spin_lock_irq(&up->port.lock);
732 __enable_rsa(up);
733 spin_unlock_irq(&up->port.lock);
734 }
735 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
736 serial_outp(up, UART_RSA_FRR, 0);
737 }
738}
739
740/*
741 * Attempts to turn off the RSA FIFO. Returns zero on failure.
742 * It is unknown why interrupts were disabled in here. However,
743 * the caller is expected to preserve this behaviour by grabbing
744 * the spinlock before calling this function.
745 */
746static void disable_rsa(struct uart_8250_port *up)
747{
748 unsigned char mode;
749 int result;
750
751 if (up->port.type == PORT_RSA &&
752 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
753 spin_lock_irq(&up->port.lock);
754
755 mode = serial_inp(up, UART_RSA_MSR);
756 result = !(mode & UART_RSA_MSR_FIFO);
757
758 if (!result) {
759 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
760 mode = serial_inp(up, UART_RSA_MSR);
761 result = !(mode & UART_RSA_MSR_FIFO);
762 }
763
764 if (result)
765 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
766 spin_unlock_irq(&up->port.lock);
767 }
768}
769#endif /* CONFIG_SERIAL_8250_RSA */
770
771/*
772 * This is a quickie test to see how big the FIFO is.
773 * It doesn't work at all the time, more's the pity.
774 */
775static int size_fifo(struct uart_8250_port *up)
776{
777 unsigned char old_fcr, old_mcr, old_lcr;
778 unsigned short old_dl;
779 int count;
780
781 old_lcr = serial_inp(up, UART_LCR);
782 serial_outp(up, UART_LCR, 0);
783 old_fcr = serial_inp(up, UART_FCR);
784 old_mcr = serial_inp(up, UART_MCR);
785 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
786 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
787 serial_outp(up, UART_MCR, UART_MCR_LOOP);
788 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
789 old_dl = serial_dl_read(up);
790 serial_dl_write(up, 0x0001);
791 serial_outp(up, UART_LCR, 0x03);
792 for (count = 0; count < 256; count++)
793 serial_outp(up, UART_TX, count);
794 mdelay(20);/* FIXME - schedule_timeout */
795 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
796 (count < 256); count++)
797 serial_inp(up, UART_RX);
798 serial_outp(up, UART_FCR, old_fcr);
799 serial_outp(up, UART_MCR, old_mcr);
800 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
801 serial_dl_write(up, old_dl);
802 serial_outp(up, UART_LCR, old_lcr);
803
804 return count;
805}
806
807/*
808 * Read UART ID using the divisor method - set DLL and DLM to zero
809 * and the revision will be in DLL and device type in DLM. We
810 * preserve the device state across this.
811 */
812static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
813{
814 unsigned char old_dll, old_dlm, old_lcr;
815 unsigned int id;
816
817 old_lcr = serial_inp(p, UART_LCR);
818 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
819
820 old_dll = serial_inp(p, UART_DLL);
821 old_dlm = serial_inp(p, UART_DLM);
822
823 serial_outp(p, UART_DLL, 0);
824 serial_outp(p, UART_DLM, 0);
825
826 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
827
828 serial_outp(p, UART_DLL, old_dll);
829 serial_outp(p, UART_DLM, old_dlm);
830 serial_outp(p, UART_LCR, old_lcr);
831
832 return id;
833}
834
835/*
836 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
837 * When this function is called we know it is at least a StarTech
838 * 16650 V2, but it might be one of several StarTech UARTs, or one of
839 * its clones. (We treat the broken original StarTech 16650 V1 as a
840 * 16550, and why not? Startech doesn't seem to even acknowledge its
841 * existence.)
842 *
843 * What evil have men's minds wrought...
844 */
845static void autoconfig_has_efr(struct uart_8250_port *up)
846{
847 unsigned int id1, id2, id3, rev;
848
849 /*
850 * Everything with an EFR has SLEEP
851 */
852 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
853
854 /*
855 * First we check to see if it's an Oxford Semiconductor UART.
856 *
857 * If we have to do this here because some non-National
858 * Semiconductor clone chips lock up if you try writing to the
859 * LSR register (which serial_icr_read does)
860 */
861
862 /*
863 * Check for Oxford Semiconductor 16C950.
864 *
865 * EFR [4] must be set else this test fails.
866 *
867 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
868 * claims that it's needed for 952 dual UART's (which are not
869 * recommended for new designs).
870 */
871 up->acr = 0;
872 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
873 serial_out(up, UART_EFR, UART_EFR_ECB);
874 serial_out(up, UART_LCR, 0x00);
875 id1 = serial_icr_read(up, UART_ID1);
876 id2 = serial_icr_read(up, UART_ID2);
877 id3 = serial_icr_read(up, UART_ID3);
878 rev = serial_icr_read(up, UART_REV);
879
880 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
881
882 if (id1 == 0x16 && id2 == 0xC9 &&
883 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
884 up->port.type = PORT_16C950;
885
886 /*
887 * Enable work around for the Oxford Semiconductor 952 rev B
888 * chip which causes it to seriously miscalculate baud rates
889 * when DLL is 0.
890 */
891 if (id3 == 0x52 && rev == 0x01)
892 up->bugs |= UART_BUG_QUOT;
893 return;
894 }
895
896 /*
897 * We check for a XR16C850 by setting DLL and DLM to 0, and then
898 * reading back DLL and DLM. The chip type depends on the DLM
899 * value read back:
900 * 0x10 - XR16C850 and the DLL contains the chip revision.
901 * 0x12 - XR16C2850.
902 * 0x14 - XR16C854.
903 */
904 id1 = autoconfig_read_divisor_id(up);
905 DEBUG_AUTOCONF("850id=%04x ", id1);
906
907 id2 = id1 >> 8;
908 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
909 up->port.type = PORT_16850;
910 return;
911 }
912
913 /*
914 * It wasn't an XR16C850.
915 *
916 * We distinguish between the '654 and the '650 by counting
917 * how many bytes are in the FIFO. I'm using this for now,
918 * since that's the technique that was sent to me in the
919 * serial driver update, but I'm not convinced this works.
920 * I've had problems doing this in the past. -TYT
921 */
922 if (size_fifo(up) == 64)
923 up->port.type = PORT_16654;
924 else
925 up->port.type = PORT_16650V2;
926}
927
928/*
929 * We detected a chip without a FIFO. Only two fall into
930 * this category - the original 8250 and the 16450. The
931 * 16450 has a scratch register (accessible with LCR=0)
932 */
933static void autoconfig_8250(struct uart_8250_port *up)
934{
935 unsigned char scratch, status1, status2;
936
937 up->port.type = PORT_8250;
938
939 scratch = serial_in(up, UART_SCR);
940 serial_outp(up, UART_SCR, 0xa5);
941 status1 = serial_in(up, UART_SCR);
942 serial_outp(up, UART_SCR, 0x5a);
943 status2 = serial_in(up, UART_SCR);
944 serial_outp(up, UART_SCR, scratch);
945
946 if (status1 == 0xa5 && status2 == 0x5a)
947 up->port.type = PORT_16450;
948}
949
950static int broken_efr(struct uart_8250_port *up)
951{
952 /*
953 * Exar ST16C2550 "A2" devices incorrectly detect as
954 * having an EFR, and report an ID of 0x0201. See
955 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
956 */
957 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
958 return 1;
959
960 return 0;
961}
962
963static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
964{
965 unsigned char status;
966
967 status = serial_in(up, 0x04); /* EXCR2 */
968#define PRESL(x) ((x) & 0x30)
969 if (PRESL(status) == 0x10) {
970 /* already in high speed mode */
971 return 0;
972 } else {
973 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
974 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
975 serial_outp(up, 0x04, status);
976 }
977 return 1;
978}
979
980/*
981 * We know that the chip has FIFOs. Does it have an EFR? The
982 * EFR is located in the same register position as the IIR and
983 * we know the top two bits of the IIR are currently set. The
984 * EFR should contain zero. Try to read the EFR.
985 */
986static void autoconfig_16550a(struct uart_8250_port *up)
987{
988 unsigned char status1, status2;
989 unsigned int iersave;
990
991 up->port.type = PORT_16550A;
992 up->capabilities |= UART_CAP_FIFO;
993
994 /*
995 * Check for presence of the EFR when DLAB is set.
996 * Only ST16C650V1 UARTs pass this test.
997 */
998 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
999 if (serial_in(up, UART_EFR) == 0) {
1000 serial_outp(up, UART_EFR, 0xA8);
1001 if (serial_in(up, UART_EFR) != 0) {
1002 DEBUG_AUTOCONF("EFRv1 ");
1003 up->port.type = PORT_16650;
1004 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1005 } else {
1006 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1007 }
1008 serial_outp(up, UART_EFR, 0);
1009 return;
1010 }
1011
1012 /*
1013 * Maybe it requires 0xbf to be written to the LCR.
1014 * (other ST16C650V2 UARTs, TI16C752A, etc)
1015 */
1016 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1017 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1018 DEBUG_AUTOCONF("EFRv2 ");
1019 autoconfig_has_efr(up);
1020 return;
1021 }
1022
1023 /*
1024 * Check for a National Semiconductor SuperIO chip.
1025 * Attempt to switch to bank 2, read the value of the LOOP bit
1026 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1027 * switch back to bank 2, read it from EXCR1 again and check
1028 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1029 */
1030 serial_outp(up, UART_LCR, 0);
1031 status1 = serial_in(up, UART_MCR);
1032 serial_outp(up, UART_LCR, 0xE0);
1033 status2 = serial_in(up, 0x02); /* EXCR1 */
1034
1035 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1036 serial_outp(up, UART_LCR, 0);
1037 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1038 serial_outp(up, UART_LCR, 0xE0);
1039 status2 = serial_in(up, 0x02); /* EXCR1 */
1040 serial_outp(up, UART_LCR, 0);
1041 serial_outp(up, UART_MCR, status1);
1042
1043 if ((status2 ^ status1) & UART_MCR_LOOP) {
1044 unsigned short quot;
1045
1046 serial_outp(up, UART_LCR, 0xE0);
1047
1048 quot = serial_dl_read(up);
1049 quot <<= 3;
1050
1051 if (ns16550a_goto_highspeed(up))
1052 serial_dl_write(up, quot);
1053
1054 serial_outp(up, UART_LCR, 0);
1055
1056 up->port.uartclk = 921600*16;
1057 up->port.type = PORT_NS16550A;
1058 up->capabilities |= UART_NATSEMI;
1059 return;
1060 }
1061 }
1062
1063 /*
1064 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1065 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1066 * Try setting it with and without DLAB set. Cheap clones
1067 * set bit 5 without DLAB set.
1068 */
1069 serial_outp(up, UART_LCR, 0);
1070 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1071 status1 = serial_in(up, UART_IIR) >> 5;
1072 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1073 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
1074 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1075 status2 = serial_in(up, UART_IIR) >> 5;
1076 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1077 serial_outp(up, UART_LCR, 0);
1078
1079 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1080
1081 if (status1 == 6 && status2 == 7) {
1082 up->port.type = PORT_16750;
1083 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1084 return;
1085 }
1086
1087 /*
1088 * Try writing and reading the UART_IER_UUE bit (b6).
1089 * If it works, this is probably one of the Xscale platform's
1090 * internal UARTs.
1091 * We're going to explicitly set the UUE bit to 0 before
1092 * trying to write and read a 1 just to make sure it's not
1093 * already a 1 and maybe locked there before we even start start.
1094 */
1095 iersave = serial_in(up, UART_IER);
1096 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1097 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1098 /*
1099 * OK it's in a known zero state, try writing and reading
1100 * without disturbing the current state of the other bits.
1101 */
1102 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1103 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1104 /*
1105 * It's an Xscale.
1106 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1107 */
1108 DEBUG_AUTOCONF("Xscale ");
1109 up->port.type = PORT_XSCALE;
1110 up->capabilities |= UART_CAP_UUE;
1111 return;
1112 }
1113 } else {
1114 /*
1115 * If we got here we couldn't force the IER_UUE bit to 0.
1116 * Log it and continue.
1117 */
1118 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1119 }
1120 serial_outp(up, UART_IER, iersave);
1121
1122 /*
1123 * We distinguish between 16550A and U6 16550A by counting
1124 * how many bytes are in the FIFO.
1125 */
1126 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1127 up->port.type = PORT_U6_16550A;
1128 up->capabilities |= UART_CAP_AFE;
1129 }
1130}
1131
1132/*
1133 * This routine is called by rs_init() to initialize a specific serial
1134 * port. It determines what type of UART chip this serial port is
1135 * using: 8250, 16450, 16550, 16550A. The important question is
1136 * whether or not this UART is a 16550A or not, since this will
1137 * determine whether or not we can use its FIFO features or not.
1138 */
1139static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1140{
1141 unsigned char status1, scratch, scratch2, scratch3;
1142 unsigned char save_lcr, save_mcr;
1143 unsigned long flags;
1144
1145 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1146 return;
1147
1148 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1149 serial_index(&up->port), up->port.iobase, up->port.membase);
1150
1151 /*
1152 * We really do need global IRQs disabled here - we're going to
1153 * be frobbing the chips IRQ enable register to see if it exists.
1154 */
1155 spin_lock_irqsave(&up->port.lock, flags);
1156
1157 up->capabilities = 0;
1158 up->bugs = 0;
1159
1160 if (!(up->port.flags & UPF_BUGGY_UART)) {
1161 /*
1162 * Do a simple existence test first; if we fail this,
1163 * there's no point trying anything else.
1164 *
1165 * 0x80 is used as a nonsense port to prevent against
1166 * false positives due to ISA bus float. The
1167 * assumption is that 0x80 is a non-existent port;
1168 * which should be safe since include/asm/io.h also
1169 * makes this assumption.
1170 *
1171 * Note: this is safe as long as MCR bit 4 is clear
1172 * and the device is in "PC" mode.
1173 */
1174 scratch = serial_inp(up, UART_IER);
1175 serial_outp(up, UART_IER, 0);
1176#ifdef __i386__
1177 outb(0xff, 0x080);
1178#endif
1179 /*
1180 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1181 * 16C754B) allow only to modify them if an EFR bit is set.
1182 */
1183 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1184 serial_outp(up, UART_IER, 0x0F);
1185#ifdef __i386__
1186 outb(0, 0x080);
1187#endif
1188 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1189 serial_outp(up, UART_IER, scratch);
1190 if (scratch2 != 0 || scratch3 != 0x0F) {
1191 /*
1192 * We failed; there's nothing here
1193 */
1194 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1195 scratch2, scratch3);
1196 goto out;
1197 }
1198 }
1199
1200 save_mcr = serial_in(up, UART_MCR);
1201 save_lcr = serial_in(up, UART_LCR);
1202
1203 /*
1204 * Check to see if a UART is really there. Certain broken
1205 * internal modems based on the Rockwell chipset fail this
1206 * test, because they apparently don't implement the loopback
1207 * test mode. So this test is skipped on the COM 1 through
1208 * COM 4 ports. This *should* be safe, since no board
1209 * manufacturer would be stupid enough to design a board
1210 * that conflicts with COM 1-4 --- we hope!
1211 */
1212 if (!(up->port.flags & UPF_SKIP_TEST)) {
1213 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1214 status1 = serial_inp(up, UART_MSR) & 0xF0;
1215 serial_outp(up, UART_MCR, save_mcr);
1216 if (status1 != 0x90) {
1217 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1218 status1);
1219 goto out;
1220 }
1221 }
1222
1223 /*
1224 * We're pretty sure there's a port here. Lets find out what
1225 * type of port it is. The IIR top two bits allows us to find
1226 * out if it's 8250 or 16450, 16550, 16550A or later. This
1227 * determines what we test for next.
1228 *
1229 * We also initialise the EFR (if any) to zero for later. The
1230 * EFR occupies the same register location as the FCR and IIR.
1231 */
1232 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1233 serial_outp(up, UART_EFR, 0);
1234 serial_outp(up, UART_LCR, 0);
1235
1236 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1237 scratch = serial_in(up, UART_IIR) >> 6;
1238
1239 DEBUG_AUTOCONF("iir=%d ", scratch);
1240
1241 switch (scratch) {
1242 case 0:
1243 autoconfig_8250(up);
1244 break;
1245 case 1:
1246 up->port.type = PORT_UNKNOWN;
1247 break;
1248 case 2:
1249 up->port.type = PORT_16550;
1250 break;
1251 case 3:
1252 autoconfig_16550a(up);
1253 break;
1254 }
1255
1256#ifdef CONFIG_SERIAL_8250_RSA
1257 /*
1258 * Only probe for RSA ports if we got the region.
1259 */
1260 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1261 int i;
1262
1263 for (i = 0 ; i < probe_rsa_count; ++i) {
1264 if (probe_rsa[i] == up->port.iobase &&
1265 __enable_rsa(up)) {
1266 up->port.type = PORT_RSA;
1267 break;
1268 }
1269 }
1270 }
1271#endif
1272
1273 serial_outp(up, UART_LCR, save_lcr);
1274
1275 if (up->capabilities != uart_config[up->port.type].flags) {
1276 printk(KERN_WARNING
1277 "ttyS%d: detected caps %08x should be %08x\n",
1278 serial_index(&up->port), up->capabilities,
1279 uart_config[up->port.type].flags);
1280 }
1281
1282 up->port.fifosize = uart_config[up->port.type].fifo_size;
1283 up->capabilities = uart_config[up->port.type].flags;
1284 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1285
1286 if (up->port.type == PORT_UNKNOWN)
1287 goto out;
1288
1289 /*
1290 * Reset the UART.
1291 */
1292#ifdef CONFIG_SERIAL_8250_RSA
1293 if (up->port.type == PORT_RSA)
1294 serial_outp(up, UART_RSA_FRR, 0);
1295#endif
1296 serial_outp(up, UART_MCR, save_mcr);
1297 serial8250_clear_fifos(up);
1298 serial_in(up, UART_RX);
1299 if (up->capabilities & UART_CAP_UUE)
1300 serial_outp(up, UART_IER, UART_IER_UUE);
1301 else
1302 serial_outp(up, UART_IER, 0);
1303
1304 out:
1305 spin_unlock_irqrestore(&up->port.lock, flags);
1306 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1307}
1308
1309static void autoconfig_irq(struct uart_8250_port *up)
1310{
1311 unsigned char save_mcr, save_ier;
1312 unsigned char save_ICP = 0;
1313 unsigned int ICP = 0;
1314 unsigned long irqs;
1315 int irq;
1316
1317 if (up->port.flags & UPF_FOURPORT) {
1318 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1319 save_ICP = inb_p(ICP);
1320 outb_p(0x80, ICP);
1321 (void) inb_p(ICP);
1322 }
1323
1324 /* forget possible initially masked and pending IRQ */
1325 probe_irq_off(probe_irq_on());
1326 save_mcr = serial_inp(up, UART_MCR);
1327 save_ier = serial_inp(up, UART_IER);
1328 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1329
1330 irqs = probe_irq_on();
1331 serial_outp(up, UART_MCR, 0);
1332 udelay(10);
1333 if (up->port.flags & UPF_FOURPORT) {
1334 serial_outp(up, UART_MCR,
1335 UART_MCR_DTR | UART_MCR_RTS);
1336 } else {
1337 serial_outp(up, UART_MCR,
1338 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1339 }
1340 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1341 (void)serial_inp(up, UART_LSR);
1342 (void)serial_inp(up, UART_RX);
1343 (void)serial_inp(up, UART_IIR);
1344 (void)serial_inp(up, UART_MSR);
1345 serial_outp(up, UART_TX, 0xFF);
1346 udelay(20);
1347 irq = probe_irq_off(irqs);
1348
1349 serial_outp(up, UART_MCR, save_mcr);
1350 serial_outp(up, UART_IER, save_ier);
1351
1352 if (up->port.flags & UPF_FOURPORT)
1353 outb_p(save_ICP, ICP);
1354
1355 up->port.irq = (irq > 0) ? irq : 0;
1356}
1357
1358static inline void __stop_tx(struct uart_8250_port *p)
1359{
1360 if (p->ier & UART_IER_THRI) {
1361 p->ier &= ~UART_IER_THRI;
1362 serial_out(p, UART_IER, p->ier);
1363 }
1364}
1365
1366static void serial8250_stop_tx(struct uart_port *port)
1367{
1368 struct uart_8250_port *up =
1369 container_of(port, struct uart_8250_port, port);
1370
1371 __stop_tx(up);
1372
1373 /*
1374 * We really want to stop the transmitter from sending.
1375 */
1376 if (up->port.type == PORT_16C950) {
1377 up->acr |= UART_ACR_TXDIS;
1378 serial_icr_write(up, UART_ACR, up->acr);
1379 }
1380}
1381
1382static void transmit_chars(struct uart_8250_port *up);
1383
1384static void serial8250_start_tx(struct uart_port *port)
1385{
1386 struct uart_8250_port *up =
1387 container_of(port, struct uart_8250_port, port);
1388
1389 if (!(up->ier & UART_IER_THRI)) {
1390 up->ier |= UART_IER_THRI;
1391 serial_out(up, UART_IER, up->ier);
1392
1393 if (up->bugs & UART_BUG_TXEN) {
1394 unsigned char lsr;
1395 lsr = serial_in(up, UART_LSR);
1396 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1397 if ((up->port.type == PORT_RM9000) ?
1398 (lsr & UART_LSR_THRE) :
1399 (lsr & UART_LSR_TEMT))
1400 transmit_chars(up);
1401 }
1402 }
1403
1404 /*
1405 * Re-enable the transmitter if we disabled it.
1406 */
1407 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1408 up->acr &= ~UART_ACR_TXDIS;
1409 serial_icr_write(up, UART_ACR, up->acr);
1410 }
1411}
1412
1413static void serial8250_stop_rx(struct uart_port *port)
1414{
1415 struct uart_8250_port *up =
1416 container_of(port, struct uart_8250_port, port);
1417
1418 up->ier &= ~UART_IER_RLSI;
1419 up->port.read_status_mask &= ~UART_LSR_DR;
1420 serial_out(up, UART_IER, up->ier);
1421}
1422
1423static void serial8250_enable_ms(struct uart_port *port)
1424{
1425 struct uart_8250_port *up =
1426 container_of(port, struct uart_8250_port, port);
1427
1428 /* no MSR capabilities */
1429 if (up->bugs & UART_BUG_NOMSR)
1430 return;
1431
1432 up->ier |= UART_IER_MSI;
1433 serial_out(up, UART_IER, up->ier);
1434}
1435
1436/*
1437 * Clear the Tegra rx fifo after a break
1438 *
1439 * FIXME: This needs to become a port specific callback once we have a
1440 * framework for this
1441 */
1442static void clear_rx_fifo(struct uart_8250_port *up)
1443{
1444 unsigned int status, tmout = 10000;
1445 do {
1446 status = serial_in(up, UART_LSR);
1447 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1448 status = serial_in(up, UART_RX);
1449 else
1450 break;
1451 if (--tmout == 0)
1452 break;
1453 udelay(1);
1454 } while (1);
1455}
1456
1457static void
1458receive_chars(struct uart_8250_port *up, unsigned int *status)
1459{
1460 struct tty_struct *tty = up->port.state->port.tty;
1461 unsigned char ch, lsr = *status;
1462 int max_count = 256;
1463 char flag;
1464
1465 do {
1466 if (likely(lsr & UART_LSR_DR))
1467 ch = serial_inp(up, UART_RX);
1468 else
1469 /*
1470 * Intel 82571 has a Serial Over Lan device that will
1471 * set UART_LSR_BI without setting UART_LSR_DR when
1472 * it receives a break. To avoid reading from the
1473 * receive buffer without UART_LSR_DR bit set, we
1474 * just force the read character to be 0
1475 */
1476 ch = 0;
1477
1478 flag = TTY_NORMAL;
1479 up->port.icount.rx++;
1480
1481 lsr |= up->lsr_saved_flags;
1482 up->lsr_saved_flags = 0;
1483
1484 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1485 /*
1486 * For statistics only
1487 */
1488 if (lsr & UART_LSR_BI) {
1489 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1490 up->port.icount.brk++;
1491 /*
1492 * If tegra port then clear the rx fifo to
1493 * accept another break/character.
1494 */
1495 if (up->port.type == PORT_TEGRA)
1496 clear_rx_fifo(up);
1497
1498 /*
1499 * We do the SysRQ and SAK checking
1500 * here because otherwise the break
1501 * may get masked by ignore_status_mask
1502 * or read_status_mask.
1503 */
1504 if (uart_handle_break(&up->port))
1505 goto ignore_char;
1506 } else if (lsr & UART_LSR_PE)
1507 up->port.icount.parity++;
1508 else if (lsr & UART_LSR_FE)
1509 up->port.icount.frame++;
1510 if (lsr & UART_LSR_OE)
1511 up->port.icount.overrun++;
1512
1513 /*
1514 * Mask off conditions which should be ignored.
1515 */
1516 lsr &= up->port.read_status_mask;
1517
1518 if (lsr & UART_LSR_BI) {
1519 DEBUG_INTR("handling break....");
1520 flag = TTY_BREAK;
1521 } else if (lsr & UART_LSR_PE)
1522 flag = TTY_PARITY;
1523 else if (lsr & UART_LSR_FE)
1524 flag = TTY_FRAME;
1525 }
1526 if (uart_handle_sysrq_char(&up->port, ch))
1527 goto ignore_char;
1528
1529 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1530
1531ignore_char:
1532 lsr = serial_inp(up, UART_LSR);
1533 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1534 spin_unlock(&up->port.lock);
1535 tty_flip_buffer_push(tty);
1536 spin_lock(&up->port.lock);
1537 *status = lsr;
1538}
1539
1540static void transmit_chars(struct uart_8250_port *up)
1541{
1542 struct circ_buf *xmit = &up->port.state->xmit;
1543 int count;
1544
1545 if (up->port.x_char) {
1546 serial_outp(up, UART_TX, up->port.x_char);
1547 up->port.icount.tx++;
1548 up->port.x_char = 0;
1549 return;
1550 }
1551 if (uart_tx_stopped(&up->port)) {
1552 serial8250_stop_tx(&up->port);
1553 return;
1554 }
1555 if (uart_circ_empty(xmit)) {
1556 __stop_tx(up);
1557 return;
1558 }
1559
1560 count = up->tx_loadsz;
1561 do {
1562 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1563 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1564 up->port.icount.tx++;
1565 if (uart_circ_empty(xmit))
1566 break;
1567 } while (--count > 0);
1568
1569 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1570 uart_write_wakeup(&up->port);
1571
1572 DEBUG_INTR("THRE...");
1573
1574 if (uart_circ_empty(xmit))
1575 __stop_tx(up);
1576}
1577
1578static unsigned int check_modem_status(struct uart_8250_port *up)
1579{
1580 unsigned int status = serial_in(up, UART_MSR);
1581
1582 status |= up->msr_saved_flags;
1583 up->msr_saved_flags = 0;
1584 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1585 up->port.state != NULL) {
1586 if (status & UART_MSR_TERI)
1587 up->port.icount.rng++;
1588 if (status & UART_MSR_DDSR)
1589 up->port.icount.dsr++;
1590 if (status & UART_MSR_DDCD)
1591 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1592 if (status & UART_MSR_DCTS)
1593 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1594
1595 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
1596 }
1597
1598 return status;
1599}
1600
1601/*
1602 * This handles the interrupt from one port.
1603 */
1604static void serial8250_handle_port(struct uart_8250_port *up)
1605{
1606 unsigned int status;
1607 unsigned long flags;
1608
1609 spin_lock_irqsave(&up->port.lock, flags);
1610
1611 status = serial_inp(up, UART_LSR);
1612
1613 DEBUG_INTR("status = %x...", status);
1614
1615 if (status & (UART_LSR_DR | UART_LSR_BI))
1616 receive_chars(up, &status);
1617 check_modem_status(up);
1618 if (status & UART_LSR_THRE)
1619 transmit_chars(up);
1620
1621 spin_unlock_irqrestore(&up->port.lock, flags);
1622}
1623
1624/*
1625 * This is the serial driver's interrupt routine.
1626 *
1627 * Arjan thinks the old way was overly complex, so it got simplified.
1628 * Alan disagrees, saying that need the complexity to handle the weird
1629 * nature of ISA shared interrupts. (This is a special exception.)
1630 *
1631 * In order to handle ISA shared interrupts properly, we need to check
1632 * that all ports have been serviced, and therefore the ISA interrupt
1633 * line has been de-asserted.
1634 *
1635 * This means we need to loop through all ports. checking that they
1636 * don't have an interrupt pending.
1637 */
1638static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1639{
1640 struct irq_info *i = dev_id;
1641 struct list_head *l, *end = NULL;
1642 int pass_counter = 0, handled = 0;
1643
1644 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1645
1646 spin_lock(&i->lock);
1647
1648 l = i->head;
1649 do {
1650 struct uart_8250_port *up;
1651 unsigned int iir;
1652
1653 up = list_entry(l, struct uart_8250_port, list);
1654
1655 iir = serial_in(up, UART_IIR);
1656 if (!(iir & UART_IIR_NO_INT)) {
1657 serial8250_handle_port(up);
1658
1659 handled = 1;
1660
1661 end = NULL;
1662 } else if ((up->port.iotype == UPIO_DWAPB ||
1663 up->port.iotype == UPIO_DWAPB32) &&
1664 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1665 /* The DesignWare APB UART has an Busy Detect (0x07)
1666 * interrupt meaning an LCR write attempt occurred while the
1667 * UART was busy. The interrupt must be cleared by reading
1668 * the UART status register (USR) and the LCR re-written. */
1669 unsigned int status;
1670 status = *(volatile u32 *)up->port.private_data;
1671 serial_out(up, UART_LCR, up->lcr);
1672
1673 handled = 1;
1674
1675 end = NULL;
1676 } else if (end == NULL)
1677 end = l;
1678
1679 l = l->next;
1680
1681 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1682 /* If we hit this, we're dead. */
1683 printk_ratelimited(KERN_ERR
1684 "serial8250: too much work for irq%d\n", irq);
1685 break;
1686 }
1687 } while (l != end);
1688
1689 spin_unlock(&i->lock);
1690
1691 DEBUG_INTR("end.\n");
1692
1693 return IRQ_RETVAL(handled);
1694}
1695
1696/*
1697 * To support ISA shared interrupts, we need to have one interrupt
1698 * handler that ensures that the IRQ line has been deasserted
1699 * before returning. Failing to do this will result in the IRQ
1700 * line being stuck active, and, since ISA irqs are edge triggered,
1701 * no more IRQs will be seen.
1702 */
1703static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1704{
1705 spin_lock_irq(&i->lock);
1706
1707 if (!list_empty(i->head)) {
1708 if (i->head == &up->list)
1709 i->head = i->head->next;
1710 list_del(&up->list);
1711 } else {
1712 BUG_ON(i->head != &up->list);
1713 i->head = NULL;
1714 }
1715 spin_unlock_irq(&i->lock);
1716 /* List empty so throw away the hash node */
1717 if (i->head == NULL) {
1718 hlist_del(&i->node);
1719 kfree(i);
1720 }
1721}
1722
1723static int serial_link_irq_chain(struct uart_8250_port *up)
1724{
1725 struct hlist_head *h;
1726 struct hlist_node *n;
1727 struct irq_info *i;
1728 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1729
1730 mutex_lock(&hash_mutex);
1731
1732 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1733
1734 hlist_for_each(n, h) {
1735 i = hlist_entry(n, struct irq_info, node);
1736 if (i->irq == up->port.irq)
1737 break;
1738 }
1739
1740 if (n == NULL) {
1741 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1742 if (i == NULL) {
1743 mutex_unlock(&hash_mutex);
1744 return -ENOMEM;
1745 }
1746 spin_lock_init(&i->lock);
1747 i->irq = up->port.irq;
1748 hlist_add_head(&i->node, h);
1749 }
1750 mutex_unlock(&hash_mutex);
1751
1752 spin_lock_irq(&i->lock);
1753
1754 if (i->head) {
1755 list_add(&up->list, i->head);
1756 spin_unlock_irq(&i->lock);
1757
1758 ret = 0;
1759 } else {
1760 INIT_LIST_HEAD(&up->list);
1761 i->head = &up->list;
1762 spin_unlock_irq(&i->lock);
1763 irq_flags |= up->port.irqflags;
1764 ret = request_irq(up->port.irq, serial8250_interrupt,
1765 irq_flags, "serial", i);
1766 if (ret < 0)
1767 serial_do_unlink(i, up);
1768 }
1769
1770 return ret;
1771}
1772
1773static void serial_unlink_irq_chain(struct uart_8250_port *up)
1774{
1775 struct irq_info *i;
1776 struct hlist_node *n;
1777 struct hlist_head *h;
1778
1779 mutex_lock(&hash_mutex);
1780
1781 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1782
1783 hlist_for_each(n, h) {
1784 i = hlist_entry(n, struct irq_info, node);
1785 if (i->irq == up->port.irq)
1786 break;
1787 }
1788
1789 BUG_ON(n == NULL);
1790 BUG_ON(i->head == NULL);
1791
1792 if (list_empty(i->head))
1793 free_irq(up->port.irq, i);
1794
1795 serial_do_unlink(i, up);
1796 mutex_unlock(&hash_mutex);
1797}
1798
1799/*
1800 * This function is used to handle ports that do not have an
1801 * interrupt. This doesn't work very well for 16450's, but gives
1802 * barely passable results for a 16550A. (Although at the expense
1803 * of much CPU overhead).
1804 */
1805static void serial8250_timeout(unsigned long data)
1806{
1807 struct uart_8250_port *up = (struct uart_8250_port *)data;
1808 unsigned int iir;
1809
1810 iir = serial_in(up, UART_IIR);
1811 if (!(iir & UART_IIR_NO_INT))
1812 serial8250_handle_port(up);
1813 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
1814}
1815
1816static void serial8250_backup_timeout(unsigned long data)
1817{
1818 struct uart_8250_port *up = (struct uart_8250_port *)data;
1819 unsigned int iir, ier = 0, lsr;
1820 unsigned long flags;
1821
1822 /*
1823 * Must disable interrupts or else we risk racing with the interrupt
1824 * based handler.
1825 */
1826 if (is_real_interrupt(up->port.irq)) {
1827 ier = serial_in(up, UART_IER);
1828 serial_out(up, UART_IER, 0);
1829 }
1830
1831 iir = serial_in(up, UART_IIR);
1832
1833 /*
1834 * This should be a safe test for anyone who doesn't trust the
1835 * IIR bits on their UART, but it's specifically designed for
1836 * the "Diva" UART used on the management processor on many HP
1837 * ia64 and parisc boxes.
1838 */
1839 spin_lock_irqsave(&up->port.lock, flags);
1840 lsr = serial_in(up, UART_LSR);
1841 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1842 spin_unlock_irqrestore(&up->port.lock, flags);
1843 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1844 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1845 (lsr & UART_LSR_THRE)) {
1846 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1847 iir |= UART_IIR_THRI;
1848 }
1849
1850 if (!(iir & UART_IIR_NO_INT))
1851 serial8250_handle_port(up);
1852
1853 if (is_real_interrupt(up->port.irq))
1854 serial_out(up, UART_IER, ier);
1855
1856 /* Standard timer interval plus 0.2s to keep the port running */
1857 mod_timer(&up->timer,
1858 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
1859}
1860
1861static unsigned int serial8250_tx_empty(struct uart_port *port)
1862{
1863 struct uart_8250_port *up =
1864 container_of(port, struct uart_8250_port, port);
1865 unsigned long flags;
1866 unsigned int lsr;
1867
1868 spin_lock_irqsave(&up->port.lock, flags);
1869 lsr = serial_in(up, UART_LSR);
1870 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1871 spin_unlock_irqrestore(&up->port.lock, flags);
1872
1873 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1874}
1875
1876static unsigned int serial8250_get_mctrl(struct uart_port *port)
1877{
1878 struct uart_8250_port *up =
1879 container_of(port, struct uart_8250_port, port);
1880 unsigned int status;
1881 unsigned int ret;
1882
1883 status = check_modem_status(up);
1884
1885 ret = 0;
1886 if (status & UART_MSR_DCD)
1887 ret |= TIOCM_CAR;
1888 if (status & UART_MSR_RI)
1889 ret |= TIOCM_RNG;
1890 if (status & UART_MSR_DSR)
1891 ret |= TIOCM_DSR;
1892 if (status & UART_MSR_CTS)
1893 ret |= TIOCM_CTS;
1894 return ret;
1895}
1896
1897static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1898{
1899 struct uart_8250_port *up =
1900 container_of(port, struct uart_8250_port, port);
1901 unsigned char mcr = 0;
1902
1903 if (mctrl & TIOCM_RTS)
1904 mcr |= UART_MCR_RTS;
1905 if (mctrl & TIOCM_DTR)
1906 mcr |= UART_MCR_DTR;
1907 if (mctrl & TIOCM_OUT1)
1908 mcr |= UART_MCR_OUT1;
1909 if (mctrl & TIOCM_OUT2)
1910 mcr |= UART_MCR_OUT2;
1911 if (mctrl & TIOCM_LOOP)
1912 mcr |= UART_MCR_LOOP;
1913
1914 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1915
1916 serial_out(up, UART_MCR, mcr);
1917}
1918
1919static void serial8250_break_ctl(struct uart_port *port, int break_state)
1920{
1921 struct uart_8250_port *up =
1922 container_of(port, struct uart_8250_port, port);
1923 unsigned long flags;
1924
1925 spin_lock_irqsave(&up->port.lock, flags);
1926 if (break_state == -1)
1927 up->lcr |= UART_LCR_SBC;
1928 else
1929 up->lcr &= ~UART_LCR_SBC;
1930 serial_out(up, UART_LCR, up->lcr);
1931 spin_unlock_irqrestore(&up->port.lock, flags);
1932}
1933
1934/*
1935 * Wait for transmitter & holding register to empty
1936 */
1937static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1938{
1939 unsigned int status, tmout = 10000;
1940
1941 /* Wait up to 10ms for the character(s) to be sent. */
1942 for (;;) {
1943 status = serial_in(up, UART_LSR);
1944
1945 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1946
1947 if ((status & bits) == bits)
1948 break;
1949 if (--tmout == 0)
1950 break;
1951 udelay(1);
1952 }
1953
1954 /* Wait up to 1s for flow control if necessary */
1955 if (up->port.flags & UPF_CONS_FLOW) {
1956 unsigned int tmout;
1957 for (tmout = 1000000; tmout; tmout--) {
1958 unsigned int msr = serial_in(up, UART_MSR);
1959 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1960 if (msr & UART_MSR_CTS)
1961 break;
1962 udelay(1);
1963 touch_nmi_watchdog();
1964 }
1965 }
1966}
1967
1968#ifdef CONFIG_CONSOLE_POLL
1969/*
1970 * Console polling routines for writing and reading from the uart while
1971 * in an interrupt or debug context.
1972 */
1973
1974static int serial8250_get_poll_char(struct uart_port *port)
1975{
1976 struct uart_8250_port *up =
1977 container_of(port, struct uart_8250_port, port);
1978 unsigned char lsr = serial_inp(up, UART_LSR);
1979
1980 if (!(lsr & UART_LSR_DR))
1981 return NO_POLL_CHAR;
1982
1983 return serial_inp(up, UART_RX);
1984}
1985
1986
1987static void serial8250_put_poll_char(struct uart_port *port,
1988 unsigned char c)
1989{
1990 unsigned int ier;
1991 struct uart_8250_port *up =
1992 container_of(port, struct uart_8250_port, port);
1993
1994 /*
1995 * First save the IER then disable the interrupts
1996 */
1997 ier = serial_in(up, UART_IER);
1998 if (up->capabilities & UART_CAP_UUE)
1999 serial_out(up, UART_IER, UART_IER_UUE);
2000 else
2001 serial_out(up, UART_IER, 0);
2002
2003 wait_for_xmitr(up, BOTH_EMPTY);
2004 /*
2005 * Send the character out.
2006 * If a LF, also do CR...
2007 */
2008 serial_out(up, UART_TX, c);
2009 if (c == 10) {
2010 wait_for_xmitr(up, BOTH_EMPTY);
2011 serial_out(up, UART_TX, 13);
2012 }
2013
2014 /*
2015 * Finally, wait for transmitter to become empty
2016 * and restore the IER
2017 */
2018 wait_for_xmitr(up, BOTH_EMPTY);
2019 serial_out(up, UART_IER, ier);
2020}
2021
2022#endif /* CONFIG_CONSOLE_POLL */
2023
2024static int serial8250_startup(struct uart_port *port)
2025{
2026 struct uart_8250_port *up =
2027 container_of(port, struct uart_8250_port, port);
2028 unsigned long flags;
2029 unsigned char lsr, iir;
2030 int retval;
2031
2032 up->port.fifosize = uart_config[up->port.type].fifo_size;
2033 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
2034 up->capabilities = uart_config[up->port.type].flags;
2035 up->mcr = 0;
2036
2037 if (up->port.iotype != up->cur_iotype)
2038 set_io_from_upio(port);
2039
2040 if (up->port.type == PORT_16C950) {
2041 /* Wake up and initialize UART */
2042 up->acr = 0;
2043 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2044 serial_outp(up, UART_EFR, UART_EFR_ECB);
2045 serial_outp(up, UART_IER, 0);
2046 serial_outp(up, UART_LCR, 0);
2047 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2048 serial_outp(up, UART_LCR, 0xBF);
2049 serial_outp(up, UART_EFR, UART_EFR_ECB);
2050 serial_outp(up, UART_LCR, 0);
2051 }
2052
2053#ifdef CONFIG_SERIAL_8250_RSA
2054 /*
2055 * If this is an RSA port, see if we can kick it up to the
2056 * higher speed clock.
2057 */
2058 enable_rsa(up);
2059#endif
2060
2061 /*
2062 * Clear the FIFO buffers and disable them.
2063 * (they will be reenabled in set_termios())
2064 */
2065 serial8250_clear_fifos(up);
2066
2067 /*
2068 * Clear the interrupt registers.
2069 */
2070 (void) serial_inp(up, UART_LSR);
2071 (void) serial_inp(up, UART_RX);
2072 (void) serial_inp(up, UART_IIR);
2073 (void) serial_inp(up, UART_MSR);
2074
2075 /*
2076 * At this point, there's no way the LSR could still be 0xff;
2077 * if it is, then bail out, because there's likely no UART
2078 * here.
2079 */
2080 if (!(up->port.flags & UPF_BUGGY_UART) &&
2081 (serial_inp(up, UART_LSR) == 0xff)) {
2082 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2083 serial_index(&up->port));
2084 return -ENODEV;
2085 }
2086
2087 /*
2088 * For a XR16C850, we need to set the trigger levels
2089 */
2090 if (up->port.type == PORT_16850) {
2091 unsigned char fctr;
2092
2093 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2094
2095 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2096 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2097 serial_outp(up, UART_TRG, UART_TRG_96);
2098 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2099 serial_outp(up, UART_TRG, UART_TRG_96);
2100
2101 serial_outp(up, UART_LCR, 0);
2102 }
2103
2104 if (is_real_interrupt(up->port.irq)) {
2105 unsigned char iir1;
2106 /*
2107 * Test for UARTs that do not reassert THRE when the
2108 * transmitter is idle and the interrupt has already
2109 * been cleared. Real 16550s should always reassert
2110 * this interrupt whenever the transmitter is idle and
2111 * the interrupt is enabled. Delays are necessary to
2112 * allow register changes to become visible.
2113 */
2114 spin_lock_irqsave(&up->port.lock, flags);
2115 if (up->port.irqflags & IRQF_SHARED)
2116 disable_irq_nosync(up->port.irq);
2117
2118 wait_for_xmitr(up, UART_LSR_THRE);
2119 serial_out_sync(up, UART_IER, UART_IER_THRI);
2120 udelay(1); /* allow THRE to set */
2121 iir1 = serial_in(up, UART_IIR);
2122 serial_out(up, UART_IER, 0);
2123 serial_out_sync(up, UART_IER, UART_IER_THRI);
2124 udelay(1); /* allow a working UART time to re-assert THRE */
2125 iir = serial_in(up, UART_IIR);
2126 serial_out(up, UART_IER, 0);
2127
2128 if (up->port.irqflags & IRQF_SHARED)
2129 enable_irq(up->port.irq);
2130 spin_unlock_irqrestore(&up->port.lock, flags);
2131
2132 /*
2133 * If the interrupt is not reasserted, setup a timer to
2134 * kick the UART on a regular basis.
2135 */
2136 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
2137 up->bugs |= UART_BUG_THRE;
2138 pr_debug("ttyS%d - using backup timer\n",
2139 serial_index(port));
2140 }
2141 }
2142
2143 /*
2144 * The above check will only give an accurate result the first time
2145 * the port is opened so this value needs to be preserved.
2146 */
2147 if (up->bugs & UART_BUG_THRE) {
2148 up->timer.function = serial8250_backup_timeout;
2149 up->timer.data = (unsigned long)up;
2150 mod_timer(&up->timer, jiffies +
2151 uart_poll_timeout(port) + HZ / 5);
2152 }
2153
2154 /*
2155 * If the "interrupt" for this port doesn't correspond with any
2156 * hardware interrupt, we use a timer-based system. The original
2157 * driver used to do this with IRQ0.
2158 */
2159 if (!is_real_interrupt(up->port.irq)) {
2160 up->timer.data = (unsigned long)up;
2161 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
2162 } else {
2163 retval = serial_link_irq_chain(up);
2164 if (retval)
2165 return retval;
2166 }
2167
2168 /*
2169 * Now, initialize the UART
2170 */
2171 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2172
2173 spin_lock_irqsave(&up->port.lock, flags);
2174 if (up->port.flags & UPF_FOURPORT) {
2175 if (!is_real_interrupt(up->port.irq))
2176 up->port.mctrl |= TIOCM_OUT1;
2177 } else
2178 /*
2179 * Most PC uarts need OUT2 raised to enable interrupts.
2180 */
2181 if (is_real_interrupt(up->port.irq))
2182 up->port.mctrl |= TIOCM_OUT2;
2183
2184 serial8250_set_mctrl(&up->port, up->port.mctrl);
2185
2186 /* Serial over Lan (SoL) hack:
2187 Intel 8257x Gigabit ethernet chips have a
2188 16550 emulation, to be used for Serial Over Lan.
2189 Those chips take a longer time than a normal
2190 serial device to signalize that a transmission
2191 data was queued. Due to that, the above test generally
2192 fails. One solution would be to delay the reading of
2193 iir. However, this is not reliable, since the timeout
2194 is variable. So, let's just don't test if we receive
2195 TX irq. This way, we'll never enable UART_BUG_TXEN.
2196 */
2197 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
2198 goto dont_test_tx_en;
2199
2200 /*
2201 * Do a quick test to see if we receive an
2202 * interrupt when we enable the TX irq.
2203 */
2204 serial_outp(up, UART_IER, UART_IER_THRI);
2205 lsr = serial_in(up, UART_LSR);
2206 iir = serial_in(up, UART_IIR);
2207 serial_outp(up, UART_IER, 0);
2208
2209 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2210 if (!(up->bugs & UART_BUG_TXEN)) {
2211 up->bugs |= UART_BUG_TXEN;
2212 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2213 serial_index(port));
2214 }
2215 } else {
2216 up->bugs &= ~UART_BUG_TXEN;
2217 }
2218
2219dont_test_tx_en:
2220 spin_unlock_irqrestore(&up->port.lock, flags);
2221
2222 /*
2223 * Clear the interrupt registers again for luck, and clear the
2224 * saved flags to avoid getting false values from polling
2225 * routines or the previous session.
2226 */
2227 serial_inp(up, UART_LSR);
2228 serial_inp(up, UART_RX);
2229 serial_inp(up, UART_IIR);
2230 serial_inp(up, UART_MSR);
2231 up->lsr_saved_flags = 0;
2232 up->msr_saved_flags = 0;
2233
2234 /*
2235 * Finally, enable interrupts. Note: Modem status interrupts
2236 * are set via set_termios(), which will be occurring imminently
2237 * anyway, so we don't enable them here.
2238 */
2239 up->ier = UART_IER_RLSI | UART_IER_RDI;
2240 serial_outp(up, UART_IER, up->ier);
2241
2242 if (up->port.flags & UPF_FOURPORT) {
2243 unsigned int icp;
2244 /*
2245 * Enable interrupts on the AST Fourport board
2246 */
2247 icp = (up->port.iobase & 0xfe0) | 0x01f;
2248 outb_p(0x80, icp);
2249 (void) inb_p(icp);
2250 }
2251
2252 return 0;
2253}
2254
2255static void serial8250_shutdown(struct uart_port *port)
2256{
2257 struct uart_8250_port *up =
2258 container_of(port, struct uart_8250_port, port);
2259 unsigned long flags;
2260
2261 /*
2262 * Disable interrupts from this port
2263 */
2264 up->ier = 0;
2265 serial_outp(up, UART_IER, 0);
2266
2267 spin_lock_irqsave(&up->port.lock, flags);
2268 if (up->port.flags & UPF_FOURPORT) {
2269 /* reset interrupts on the AST Fourport board */
2270 inb((up->port.iobase & 0xfe0) | 0x1f);
2271 up->port.mctrl |= TIOCM_OUT1;
2272 } else
2273 up->port.mctrl &= ~TIOCM_OUT2;
2274
2275 serial8250_set_mctrl(&up->port, up->port.mctrl);
2276 spin_unlock_irqrestore(&up->port.lock, flags);
2277
2278 /*
2279 * Disable break condition and FIFOs
2280 */
2281 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2282 serial8250_clear_fifos(up);
2283
2284#ifdef CONFIG_SERIAL_8250_RSA
2285 /*
2286 * Reset the RSA board back to 115kbps compat mode.
2287 */
2288 disable_rsa(up);
2289#endif
2290
2291 /*
2292 * Read data port to reset things, and then unlink from
2293 * the IRQ chain.
2294 */
2295 (void) serial_in(up, UART_RX);
2296
2297 del_timer_sync(&up->timer);
2298 up->timer.function = serial8250_timeout;
2299 if (is_real_interrupt(up->port.irq))
2300 serial_unlink_irq_chain(up);
2301}
2302
2303static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2304{
2305 unsigned int quot;
2306
2307 /*
2308 * Handle magic divisors for baud rates above baud_base on
2309 * SMSC SuperIO chips.
2310 */
2311 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2312 baud == (port->uartclk/4))
2313 quot = 0x8001;
2314 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2315 baud == (port->uartclk/8))
2316 quot = 0x8002;
2317 else
2318 quot = uart_get_divisor(port, baud);
2319
2320 return quot;
2321}
2322
2323void
2324serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2325 struct ktermios *old)
2326{
2327 struct uart_8250_port *up =
2328 container_of(port, struct uart_8250_port, port);
2329 unsigned char cval, fcr = 0;
2330 unsigned long flags;
2331 unsigned int baud, quot;
2332
2333 switch (termios->c_cflag & CSIZE) {
2334 case CS5:
2335 cval = UART_LCR_WLEN5;
2336 break;
2337 case CS6:
2338 cval = UART_LCR_WLEN6;
2339 break;
2340 case CS7:
2341 cval = UART_LCR_WLEN7;
2342 break;
2343 default:
2344 case CS8:
2345 cval = UART_LCR_WLEN8;
2346 break;
2347 }
2348
2349 if (termios->c_cflag & CSTOPB)
2350 cval |= UART_LCR_STOP;
2351 if (termios->c_cflag & PARENB)
2352 cval |= UART_LCR_PARITY;
2353 if (!(termios->c_cflag & PARODD))
2354 cval |= UART_LCR_EPAR;
2355#ifdef CMSPAR
2356 if (termios->c_cflag & CMSPAR)
2357 cval |= UART_LCR_SPAR;
2358#endif
2359
2360 /*
2361 * Ask the core to calculate the divisor for us.
2362 */
2363 baud = uart_get_baud_rate(port, termios, old,
2364 port->uartclk / 16 / 0xffff,
2365 port->uartclk / 16);
2366 quot = serial8250_get_divisor(port, baud);
2367
2368 /*
2369 * Oxford Semi 952 rev B workaround
2370 */
2371 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2372 quot++;
2373
2374 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2375 if (baud < 2400)
2376 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2377 else
2378 fcr = uart_config[up->port.type].fcr;
2379 }
2380
2381 /*
2382 * MCR-based auto flow control. When AFE is enabled, RTS will be
2383 * deasserted when the receive FIFO contains more characters than
2384 * the trigger, or the MCR RTS bit is cleared. In the case where
2385 * the remote UART is not using CTS auto flow control, we must
2386 * have sufficient FIFO entries for the latency of the remote
2387 * UART to respond. IOW, at least 32 bytes of FIFO.
2388 */
2389 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2390 up->mcr &= ~UART_MCR_AFE;
2391 if (termios->c_cflag & CRTSCTS)
2392 up->mcr |= UART_MCR_AFE;
2393 }
2394
2395 /*
2396 * Ok, we're now changing the port state. Do it with
2397 * interrupts disabled.
2398 */
2399 spin_lock_irqsave(&up->port.lock, flags);
2400
2401 /*
2402 * Update the per-port timeout.
2403 */
2404 uart_update_timeout(port, termios->c_cflag, baud);
2405
2406 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2407 if (termios->c_iflag & INPCK)
2408 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2409 if (termios->c_iflag & (BRKINT | PARMRK))
2410 up->port.read_status_mask |= UART_LSR_BI;
2411
2412 /*
2413 * Characteres to ignore
2414 */
2415 up->port.ignore_status_mask = 0;
2416 if (termios->c_iflag & IGNPAR)
2417 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2418 if (termios->c_iflag & IGNBRK) {
2419 up->port.ignore_status_mask |= UART_LSR_BI;
2420 /*
2421 * If we're ignoring parity and break indicators,
2422 * ignore overruns too (for real raw support).
2423 */
2424 if (termios->c_iflag & IGNPAR)
2425 up->port.ignore_status_mask |= UART_LSR_OE;
2426 }
2427
2428 /*
2429 * ignore all characters if CREAD is not set
2430 */
2431 if ((termios->c_cflag & CREAD) == 0)
2432 up->port.ignore_status_mask |= UART_LSR_DR;
2433
2434 /*
2435 * CTS flow control flag and modem status interrupts
2436 */
2437 up->ier &= ~UART_IER_MSI;
2438 if (!(up->bugs & UART_BUG_NOMSR) &&
2439 UART_ENABLE_MS(&up->port, termios->c_cflag))
2440 up->ier |= UART_IER_MSI;
2441 if (up->capabilities & UART_CAP_UUE)
2442 up->ier |= UART_IER_UUE;
2443 if (up->capabilities & UART_CAP_RTOIE)
2444 up->ier |= UART_IER_RTOIE;
2445
2446 serial_out(up, UART_IER, up->ier);
2447
2448 if (up->capabilities & UART_CAP_EFR) {
2449 unsigned char efr = 0;
2450 /*
2451 * TI16C752/Startech hardware flow control. FIXME:
2452 * - TI16C752 requires control thresholds to be set.
2453 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2454 */
2455 if (termios->c_cflag & CRTSCTS)
2456 efr |= UART_EFR_CTS;
2457
2458 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2459 serial_outp(up, UART_EFR, efr);
2460 }
2461
2462#ifdef CONFIG_ARCH_OMAP
2463 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2464 if (cpu_is_omap1510() && is_omap_port(up)) {
2465 if (baud == 115200) {
2466 quot = 1;
2467 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2468 } else
2469 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2470 }
2471#endif
2472
2473 if (up->capabilities & UART_NATSEMI) {
2474 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2475 serial_outp(up, UART_LCR, 0xe0);
2476 } else {
2477 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2478 }
2479
2480 serial_dl_write(up, quot);
2481
2482 /*
2483 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2484 * is written without DLAB set, this mode will be disabled.
2485 */
2486 if (up->port.type == PORT_16750)
2487 serial_outp(up, UART_FCR, fcr);
2488
2489 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2490 up->lcr = cval; /* Save LCR */
2491 if (up->port.type != PORT_16750) {
2492 if (fcr & UART_FCR_ENABLE_FIFO) {
2493 /* emulated UARTs (Lucent Venus 167x) need two steps */
2494 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2495 }
2496 serial_outp(up, UART_FCR, fcr); /* set fcr */
2497 }
2498 serial8250_set_mctrl(&up->port, up->port.mctrl);
2499 spin_unlock_irqrestore(&up->port.lock, flags);
2500 /* Don't rewrite B0 */
2501 if (tty_termios_baud_rate(termios))
2502 tty_termios_encode_baud_rate(termios, baud, baud);
2503}
2504EXPORT_SYMBOL(serial8250_do_set_termios);
2505
2506static void
2507serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2508 struct ktermios *old)
2509{
2510 if (port->set_termios)
2511 port->set_termios(port, termios, old);
2512 else
2513 serial8250_do_set_termios(port, termios, old);
2514}
2515
2516static void
2517serial8250_set_ldisc(struct uart_port *port, int new)
2518{
2519 if (new == N_PPS) {
2520 port->flags |= UPF_HARDPPS_CD;
2521 serial8250_enable_ms(port);
2522 } else
2523 port->flags &= ~UPF_HARDPPS_CD;
2524}
2525
2526
2527void serial8250_do_pm(struct uart_port *port, unsigned int state,
2528 unsigned int oldstate)
2529{
2530 struct uart_8250_port *p =
2531 container_of(port, struct uart_8250_port, port);
2532
2533 serial8250_set_sleep(p, state != 0);
2534}
2535EXPORT_SYMBOL(serial8250_do_pm);
2536
2537static void
2538serial8250_pm(struct uart_port *port, unsigned int state,
2539 unsigned int oldstate)
2540{
2541 if (port->pm)
2542 port->pm(port, state, oldstate);
2543 else
2544 serial8250_do_pm(port, state, oldstate);
2545}
2546
2547static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2548{
2549 if (pt->port.iotype == UPIO_AU)
2550 return 0x1000;
2551#ifdef CONFIG_ARCH_OMAP
2552 if (is_omap_port(pt))
2553 return 0x16 << pt->port.regshift;
2554#endif
2555 return 8 << pt->port.regshift;
2556}
2557
2558/*
2559 * Resource handling.
2560 */
2561static int serial8250_request_std_resource(struct uart_8250_port *up)
2562{
2563 unsigned int size = serial8250_port_size(up);
2564 int ret = 0;
2565
2566 switch (up->port.iotype) {
2567 case UPIO_AU:
2568 case UPIO_TSI:
2569 case UPIO_MEM32:
2570 case UPIO_MEM:
2571 case UPIO_DWAPB:
2572 case UPIO_DWAPB32:
2573 if (!up->port.mapbase)
2574 break;
2575
2576 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2577 ret = -EBUSY;
2578 break;
2579 }
2580
2581 if (up->port.flags & UPF_IOREMAP) {
2582 up->port.membase = ioremap_nocache(up->port.mapbase,
2583 size);
2584 if (!up->port.membase) {
2585 release_mem_region(up->port.mapbase, size);
2586 ret = -ENOMEM;
2587 }
2588 }
2589 break;
2590
2591 case UPIO_HUB6:
2592 case UPIO_PORT:
2593 if (!request_region(up->port.iobase, size, "serial"))
2594 ret = -EBUSY;
2595 break;
2596 }
2597 return ret;
2598}
2599
2600static void serial8250_release_std_resource(struct uart_8250_port *up)
2601{
2602 unsigned int size = serial8250_port_size(up);
2603
2604 switch (up->port.iotype) {
2605 case UPIO_AU:
2606 case UPIO_TSI:
2607 case UPIO_MEM32:
2608 case UPIO_MEM:
2609 case UPIO_DWAPB:
2610 case UPIO_DWAPB32:
2611 if (!up->port.mapbase)
2612 break;
2613
2614 if (up->port.flags & UPF_IOREMAP) {
2615 iounmap(up->port.membase);
2616 up->port.membase = NULL;
2617 }
2618
2619 release_mem_region(up->port.mapbase, size);
2620 break;
2621
2622 case UPIO_HUB6:
2623 case UPIO_PORT:
2624 release_region(up->port.iobase, size);
2625 break;
2626 }
2627}
2628
2629static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2630{
2631 unsigned long start = UART_RSA_BASE << up->port.regshift;
2632 unsigned int size = 8 << up->port.regshift;
2633 int ret = -EINVAL;
2634
2635 switch (up->port.iotype) {
2636 case UPIO_HUB6:
2637 case UPIO_PORT:
2638 start += up->port.iobase;
2639 if (request_region(start, size, "serial-rsa"))
2640 ret = 0;
2641 else
2642 ret = -EBUSY;
2643 break;
2644 }
2645
2646 return ret;
2647}
2648
2649static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2650{
2651 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2652 unsigned int size = 8 << up->port.regshift;
2653
2654 switch (up->port.iotype) {
2655 case UPIO_HUB6:
2656 case UPIO_PORT:
2657 release_region(up->port.iobase + offset, size);
2658 break;
2659 }
2660}
2661
2662static void serial8250_release_port(struct uart_port *port)
2663{
2664 struct uart_8250_port *up =
2665 container_of(port, struct uart_8250_port, port);
2666
2667 serial8250_release_std_resource(up);
2668 if (up->port.type == PORT_RSA)
2669 serial8250_release_rsa_resource(up);
2670}
2671
2672static int serial8250_request_port(struct uart_port *port)
2673{
2674 struct uart_8250_port *up =
2675 container_of(port, struct uart_8250_port, port);
2676 int ret = 0;
2677
2678 ret = serial8250_request_std_resource(up);
2679 if (ret == 0 && up->port.type == PORT_RSA) {
2680 ret = serial8250_request_rsa_resource(up);
2681 if (ret < 0)
2682 serial8250_release_std_resource(up);
2683 }
2684
2685 return ret;
2686}
2687
2688static void serial8250_config_port(struct uart_port *port, int flags)
2689{
2690 struct uart_8250_port *up =
2691 container_of(port, struct uart_8250_port, port);
2692 int probeflags = PROBE_ANY;
2693 int ret;
2694
2695 /*
2696 * Find the region that we can probe for. This in turn
2697 * tells us whether we can probe for the type of port.
2698 */
2699 ret = serial8250_request_std_resource(up);
2700 if (ret < 0)
2701 return;
2702
2703 ret = serial8250_request_rsa_resource(up);
2704 if (ret < 0)
2705 probeflags &= ~PROBE_RSA;
2706
2707 if (up->port.iotype != up->cur_iotype)
2708 set_io_from_upio(port);
2709
2710 if (flags & UART_CONFIG_TYPE)
2711 autoconfig(up, probeflags);
2712
2713 /* if access method is AU, it is a 16550 with a quirk */
2714 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2715 up->bugs |= UART_BUG_NOMSR;
2716
2717 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2718 autoconfig_irq(up);
2719
2720 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2721 serial8250_release_rsa_resource(up);
2722 if (up->port.type == PORT_UNKNOWN)
2723 serial8250_release_std_resource(up);
2724}
2725
2726static int
2727serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2728{
2729 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2730 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2731 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2732 ser->type == PORT_STARTECH)
2733 return -EINVAL;
2734 return 0;
2735}
2736
2737static const char *
2738serial8250_type(struct uart_port *port)
2739{
2740 int type = port->type;
2741
2742 if (type >= ARRAY_SIZE(uart_config))
2743 type = 0;
2744 return uart_config[type].name;
2745}
2746
2747static struct uart_ops serial8250_pops = {
2748 .tx_empty = serial8250_tx_empty,
2749 .set_mctrl = serial8250_set_mctrl,
2750 .get_mctrl = serial8250_get_mctrl,
2751 .stop_tx = serial8250_stop_tx,
2752 .start_tx = serial8250_start_tx,
2753 .stop_rx = serial8250_stop_rx,
2754 .enable_ms = serial8250_enable_ms,
2755 .break_ctl = serial8250_break_ctl,
2756 .startup = serial8250_startup,
2757 .shutdown = serial8250_shutdown,
2758 .set_termios = serial8250_set_termios,
2759 .set_ldisc = serial8250_set_ldisc,
2760 .pm = serial8250_pm,
2761 .type = serial8250_type,
2762 .release_port = serial8250_release_port,
2763 .request_port = serial8250_request_port,
2764 .config_port = serial8250_config_port,
2765 .verify_port = serial8250_verify_port,
2766#ifdef CONFIG_CONSOLE_POLL
2767 .poll_get_char = serial8250_get_poll_char,
2768 .poll_put_char = serial8250_put_poll_char,
2769#endif
2770};
2771
2772static struct uart_8250_port serial8250_ports[UART_NR];
2773
2774static void (*serial8250_isa_config)(int port, struct uart_port *up,
2775 unsigned short *capabilities);
2776
2777void serial8250_set_isa_configurator(
2778 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2779{
2780 serial8250_isa_config = v;
2781}
2782EXPORT_SYMBOL(serial8250_set_isa_configurator);
2783
2784static void __init serial8250_isa_init_ports(void)
2785{
2786 struct uart_8250_port *up;
2787 static int first = 1;
2788 int i, irqflag = 0;
2789
2790 if (!first)
2791 return;
2792 first = 0;
2793
2794 for (i = 0; i < nr_uarts; i++) {
2795 struct uart_8250_port *up = &serial8250_ports[i];
2796
2797 up->port.line = i;
2798 spin_lock_init(&up->port.lock);
2799
2800 init_timer(&up->timer);
2801 up->timer.function = serial8250_timeout;
2802
2803 /*
2804 * ALPHA_KLUDGE_MCR needs to be killed.
2805 */
2806 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2807 up->mcr_force = ALPHA_KLUDGE_MCR;
2808
2809 up->port.ops = &serial8250_pops;
2810 }
2811
2812 if (share_irqs)
2813 irqflag = IRQF_SHARED;
2814
2815 for (i = 0, up = serial8250_ports;
2816 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2817 i++, up++) {
2818 up->port.iobase = old_serial_port[i].port;
2819 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2820 up->port.irqflags = old_serial_port[i].irqflags;
2821 up->port.uartclk = old_serial_port[i].baud_base * 16;
2822 up->port.flags = old_serial_port[i].flags;
2823 up->port.hub6 = old_serial_port[i].hub6;
2824 up->port.membase = old_serial_port[i].iomem_base;
2825 up->port.iotype = old_serial_port[i].io_type;
2826 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2827 set_io_from_upio(&up->port);
2828 up->port.irqflags |= irqflag;
2829 if (serial8250_isa_config != NULL)
2830 serial8250_isa_config(i, &up->port, &up->capabilities);
2831
2832 }
2833}
2834
2835static void
2836serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2837{
2838 up->port.type = type;
2839 up->port.fifosize = uart_config[type].fifo_size;
2840 up->capabilities = uart_config[type].flags;
2841 up->tx_loadsz = uart_config[type].tx_loadsz;
2842}
2843
2844static void __init
2845serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2846{
2847 int i;
2848
2849 for (i = 0; i < nr_uarts; i++) {
2850 struct uart_8250_port *up = &serial8250_ports[i];
2851 up->cur_iotype = 0xFF;
2852 }
2853
2854 serial8250_isa_init_ports();
2855
2856 for (i = 0; i < nr_uarts; i++) {
2857 struct uart_8250_port *up = &serial8250_ports[i];
2858
2859 up->port.dev = dev;
2860
2861 if (up->port.flags & UPF_FIXED_TYPE)
2862 serial8250_init_fixed_type_port(up, up->port.type);
2863
2864 uart_add_one_port(drv, &up->port);
2865 }
2866}
2867
2868#ifdef CONFIG_SERIAL_8250_CONSOLE
2869
2870static void serial8250_console_putchar(struct uart_port *port, int ch)
2871{
2872 struct uart_8250_port *up =
2873 container_of(port, struct uart_8250_port, port);
2874
2875 wait_for_xmitr(up, UART_LSR_THRE);
2876 serial_out(up, UART_TX, ch);
2877}
2878
2879/*
2880 * Print a string to the serial port trying not to disturb
2881 * any possible real use of the port...
2882 *
2883 * The console_lock must be held when we get here.
2884 */
2885static void
2886serial8250_console_write(struct console *co, const char *s, unsigned int count)
2887{
2888 struct uart_8250_port *up = &serial8250_ports[co->index];
2889 unsigned long flags;
2890 unsigned int ier;
2891 int locked = 1;
2892
2893 touch_nmi_watchdog();
2894
2895 local_irq_save(flags);
2896 if (up->port.sysrq) {
2897 /* serial8250_handle_port() already took the lock */
2898 locked = 0;
2899 } else if (oops_in_progress) {
2900 locked = spin_trylock(&up->port.lock);
2901 } else
2902 spin_lock(&up->port.lock);
2903
2904 /*
2905 * First save the IER then disable the interrupts
2906 */
2907 ier = serial_in(up, UART_IER);
2908
2909 if (up->capabilities & UART_CAP_UUE)
2910 serial_out(up, UART_IER, UART_IER_UUE);
2911 else
2912 serial_out(up, UART_IER, 0);
2913
2914 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2915
2916 /*
2917 * Finally, wait for transmitter to become empty
2918 * and restore the IER
2919 */
2920 wait_for_xmitr(up, BOTH_EMPTY);
2921 serial_out(up, UART_IER, ier);
2922
2923 /*
2924 * The receive handling will happen properly because the
2925 * receive ready bit will still be set; it is not cleared
2926 * on read. However, modem control will not, we must
2927 * call it if we have saved something in the saved flags
2928 * while processing with interrupts off.
2929 */
2930 if (up->msr_saved_flags)
2931 check_modem_status(up);
2932
2933 if (locked)
2934 spin_unlock(&up->port.lock);
2935 local_irq_restore(flags);
2936}
2937
2938static int __init serial8250_console_setup(struct console *co, char *options)
2939{
2940 struct uart_port *port;
2941 int baud = 9600;
2942 int bits = 8;
2943 int parity = 'n';
2944 int flow = 'n';
2945
2946 /*
2947 * Check whether an invalid uart number has been specified, and
2948 * if so, search for the first available port that does have
2949 * console support.
2950 */
2951 if (co->index >= nr_uarts)
2952 co->index = 0;
2953 port = &serial8250_ports[co->index].port;
2954 if (!port->iobase && !port->membase)
2955 return -ENODEV;
2956
2957 if (options)
2958 uart_parse_options(options, &baud, &parity, &bits, &flow);
2959
2960 return uart_set_options(port, co, baud, parity, bits, flow);
2961}
2962
2963static int serial8250_console_early_setup(void)
2964{
2965 return serial8250_find_port_for_earlycon();
2966}
2967
2968static struct console serial8250_console = {
2969 .name = "ttyS",
2970 .write = serial8250_console_write,
2971 .device = uart_console_device,
2972 .setup = serial8250_console_setup,
2973 .early_setup = serial8250_console_early_setup,
2974 .flags = CON_PRINTBUFFER | CON_ANYTIME,
2975 .index = -1,
2976 .data = &serial8250_reg,
2977};
2978
2979static int __init serial8250_console_init(void)
2980{
2981 if (nr_uarts > UART_NR)
2982 nr_uarts = UART_NR;
2983
2984 serial8250_isa_init_ports();
2985 register_console(&serial8250_console);
2986 return 0;
2987}
2988console_initcall(serial8250_console_init);
2989
2990int serial8250_find_port(struct uart_port *p)
2991{
2992 int line;
2993 struct uart_port *port;
2994
2995 for (line = 0; line < nr_uarts; line++) {
2996 port = &serial8250_ports[line].port;
2997 if (uart_match_port(p, port))
2998 return line;
2999 }
3000 return -ENODEV;
3001}
3002
3003#define SERIAL8250_CONSOLE &serial8250_console
3004#else
3005#define SERIAL8250_CONSOLE NULL
3006#endif
3007
3008static struct uart_driver serial8250_reg = {
3009 .owner = THIS_MODULE,
3010 .driver_name = "serial",
3011 .dev_name = "ttyS",
3012 .major = TTY_MAJOR,
3013 .minor = 64,
3014 .cons = SERIAL8250_CONSOLE,
3015};
3016
3017/*
3018 * early_serial_setup - early registration for 8250 ports
3019 *
3020 * Setup an 8250 port structure prior to console initialisation. Use
3021 * after console initialisation will cause undefined behaviour.
3022 */
3023int __init early_serial_setup(struct uart_port *port)
3024{
3025 struct uart_port *p;
3026
3027 if (port->line >= ARRAY_SIZE(serial8250_ports))
3028 return -ENODEV;
3029
3030 serial8250_isa_init_ports();
3031 p = &serial8250_ports[port->line].port;
3032 p->iobase = port->iobase;
3033 p->membase = port->membase;
3034 p->irq = port->irq;
3035 p->irqflags = port->irqflags;
3036 p->uartclk = port->uartclk;
3037 p->fifosize = port->fifosize;
3038 p->regshift = port->regshift;
3039 p->iotype = port->iotype;
3040 p->flags = port->flags;
3041 p->mapbase = port->mapbase;
3042 p->private_data = port->private_data;
3043 p->type = port->type;
3044 p->line = port->line;
3045
3046 set_io_from_upio(p);
3047 if (port->serial_in)
3048 p->serial_in = port->serial_in;
3049 if (port->serial_out)
3050 p->serial_out = port->serial_out;
3051
3052 return 0;
3053}
3054
3055/**
3056 * serial8250_suspend_port - suspend one serial port
3057 * @line: serial line number
3058 *
3059 * Suspend one serial port.
3060 */
3061void serial8250_suspend_port(int line)
3062{
3063 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
3064}
3065
3066/**
3067 * serial8250_resume_port - resume one serial port
3068 * @line: serial line number
3069 *
3070 * Resume one serial port.
3071 */
3072void serial8250_resume_port(int line)
3073{
3074 struct uart_8250_port *up = &serial8250_ports[line];
3075
3076 if (up->capabilities & UART_NATSEMI) {
3077 /* Ensure it's still in high speed mode */
3078 serial_outp(up, UART_LCR, 0xE0);
3079
3080 ns16550a_goto_highspeed(up);
3081
3082 serial_outp(up, UART_LCR, 0);
3083 up->port.uartclk = 921600*16;
3084 }
3085 uart_resume_port(&serial8250_reg, &up->port);
3086}
3087
3088/*
3089 * Register a set of serial devices attached to a platform device. The
3090 * list is terminated with a zero flags entry, which means we expect
3091 * all entries to have at least UPF_BOOT_AUTOCONF set.
3092 */
3093static int __devinit serial8250_probe(struct platform_device *dev)
3094{
3095 struct plat_serial8250_port *p = dev->dev.platform_data;
3096 struct uart_port port;
3097 int ret, i, irqflag = 0;
3098
3099 memset(&port, 0, sizeof(struct uart_port));
3100
3101 if (share_irqs)
3102 irqflag = IRQF_SHARED;
3103
3104 for (i = 0; p && p->flags != 0; p++, i++) {
3105 port.iobase = p->iobase;
3106 port.membase = p->membase;
3107 port.irq = p->irq;
3108 port.irqflags = p->irqflags;
3109 port.uartclk = p->uartclk;
3110 port.regshift = p->regshift;
3111 port.iotype = p->iotype;
3112 port.flags = p->flags;
3113 port.mapbase = p->mapbase;
3114 port.hub6 = p->hub6;
3115 port.private_data = p->private_data;
3116 port.type = p->type;
3117 port.serial_in = p->serial_in;
3118 port.serial_out = p->serial_out;
3119 port.set_termios = p->set_termios;
3120 port.pm = p->pm;
3121 port.dev = &dev->dev;
3122 port.irqflags |= irqflag;
3123 ret = serial8250_register_port(&port);
3124 if (ret < 0) {
3125 dev_err(&dev->dev, "unable to register port at index %d "
3126 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3127 p->iobase, (unsigned long long)p->mapbase,
3128 p->irq, ret);
3129 }
3130 }
3131 return 0;
3132}
3133
3134/*
3135 * Remove serial ports registered against a platform device.
3136 */
3137static int __devexit serial8250_remove(struct platform_device *dev)
3138{
3139 int i;
3140
3141 for (i = 0; i < nr_uarts; i++) {
3142 struct uart_8250_port *up = &serial8250_ports[i];
3143
3144 if (up->port.dev == &dev->dev)
3145 serial8250_unregister_port(i);
3146 }
3147 return 0;
3148}
3149
3150static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
3151{
3152 int i;
3153
3154 for (i = 0; i < UART_NR; i++) {
3155 struct uart_8250_port *up = &serial8250_ports[i];
3156
3157 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3158 uart_suspend_port(&serial8250_reg, &up->port);
3159 }
3160
3161 return 0;
3162}
3163
3164static int serial8250_resume(struct platform_device *dev)
3165{
3166 int i;
3167
3168 for (i = 0; i < UART_NR; i++) {
3169 struct uart_8250_port *up = &serial8250_ports[i];
3170
3171 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3172 serial8250_resume_port(i);
3173 }
3174
3175 return 0;
3176}
3177
3178static struct platform_driver serial8250_isa_driver = {
3179 .probe = serial8250_probe,
3180 .remove = __devexit_p(serial8250_remove),
3181 .suspend = serial8250_suspend,
3182 .resume = serial8250_resume,
3183 .driver = {
3184 .name = "serial8250",
3185 .owner = THIS_MODULE,
3186 },
3187};
3188
3189/*
3190 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3191 * in the table in include/asm/serial.h
3192 */
3193static struct platform_device *serial8250_isa_devs;
3194
3195/*
3196 * serial8250_register_port and serial8250_unregister_port allows for
3197 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3198 * modems and PCI multiport cards.
3199 */
3200static DEFINE_MUTEX(serial_mutex);
3201
3202static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3203{
3204 int i;
3205
3206 /*
3207 * First, find a port entry which matches.
3208 */
3209 for (i = 0; i < nr_uarts; i++)
3210 if (uart_match_port(&serial8250_ports[i].port, port))
3211 return &serial8250_ports[i];
3212
3213 /*
3214 * We didn't find a matching entry, so look for the first
3215 * free entry. We look for one which hasn't been previously
3216 * used (indicated by zero iobase).
3217 */
3218 for (i = 0; i < nr_uarts; i++)
3219 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3220 serial8250_ports[i].port.iobase == 0)
3221 return &serial8250_ports[i];
3222
3223 /*
3224 * That also failed. Last resort is to find any entry which
3225 * doesn't have a real port associated with it.
3226 */
3227 for (i = 0; i < nr_uarts; i++)
3228 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3229 return &serial8250_ports[i];
3230
3231 return NULL;
3232}
3233
3234/**
3235 * serial8250_register_port - register a serial port
3236 * @port: serial port template
3237 *
3238 * Configure the serial port specified by the request. If the
3239 * port exists and is in use, it is hung up and unregistered
3240 * first.
3241 *
3242 * The port is then probed and if necessary the IRQ is autodetected
3243 * If this fails an error is returned.
3244 *
3245 * On success the port is ready to use and the line number is returned.
3246 */
3247int serial8250_register_port(struct uart_port *port)
3248{
3249 struct uart_8250_port *uart;
3250 int ret = -ENOSPC;
3251
3252 if (port->uartclk == 0)
3253 return -EINVAL;
3254
3255 mutex_lock(&serial_mutex);
3256
3257 uart = serial8250_find_match_or_unused(port);
3258 if (uart) {
3259 uart_remove_one_port(&serial8250_reg, &uart->port);
3260
3261 uart->port.iobase = port->iobase;
3262 uart->port.membase = port->membase;
3263 uart->port.irq = port->irq;
3264 uart->port.irqflags = port->irqflags;
3265 uart->port.uartclk = port->uartclk;
3266 uart->port.fifosize = port->fifosize;
3267 uart->port.regshift = port->regshift;
3268 uart->port.iotype = port->iotype;
3269 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3270 uart->port.mapbase = port->mapbase;
3271 uart->port.private_data = port->private_data;
3272 if (port->dev)
3273 uart->port.dev = port->dev;
3274
3275 if (port->flags & UPF_FIXED_TYPE)
3276 serial8250_init_fixed_type_port(uart, port->type);
3277
3278 set_io_from_upio(&uart->port);
3279 /* Possibly override default I/O functions. */
3280 if (port->serial_in)
3281 uart->port.serial_in = port->serial_in;
3282 if (port->serial_out)
3283 uart->port.serial_out = port->serial_out;
3284 /* Possibly override set_termios call */
3285 if (port->set_termios)
3286 uart->port.set_termios = port->set_termios;
3287 if (port->pm)
3288 uart->port.pm = port->pm;
3289
3290 if (serial8250_isa_config != NULL)
3291 serial8250_isa_config(0, &uart->port,
3292 &uart->capabilities);
3293
3294 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3295 if (ret == 0)
3296 ret = uart->port.line;
3297 }
3298 mutex_unlock(&serial_mutex);
3299
3300 return ret;
3301}
3302EXPORT_SYMBOL(serial8250_register_port);
3303
3304/**
3305 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3306 * @line: serial line number
3307 *
3308 * Remove one serial port. This may not be called from interrupt
3309 * context. We hand the port back to the our control.
3310 */
3311void serial8250_unregister_port(int line)
3312{
3313 struct uart_8250_port *uart = &serial8250_ports[line];
3314
3315 mutex_lock(&serial_mutex);
3316 uart_remove_one_port(&serial8250_reg, &uart->port);
3317 if (serial8250_isa_devs) {
3318 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3319 uart->port.type = PORT_UNKNOWN;
3320 uart->port.dev = &serial8250_isa_devs->dev;
3321 uart->capabilities = uart_config[uart->port.type].flags;
3322 uart_add_one_port(&serial8250_reg, &uart->port);
3323 } else {
3324 uart->port.dev = NULL;
3325 }
3326 mutex_unlock(&serial_mutex);
3327}
3328EXPORT_SYMBOL(serial8250_unregister_port);
3329
3330static int __init serial8250_init(void)
3331{
3332 int ret;
3333
3334 if (nr_uarts > UART_NR)
3335 nr_uarts = UART_NR;
3336
3337 printk(KERN_INFO "Serial: 8250/16550 driver, "
3338 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3339 share_irqs ? "en" : "dis");
3340
3341#ifdef CONFIG_SPARC
3342 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3343#else
3344 serial8250_reg.nr = UART_NR;
3345 ret = uart_register_driver(&serial8250_reg);
3346#endif
3347 if (ret)
3348 goto out;
3349
3350 serial8250_isa_devs = platform_device_alloc("serial8250",
3351 PLAT8250_DEV_LEGACY);
3352 if (!serial8250_isa_devs) {
3353 ret = -ENOMEM;
3354 goto unreg_uart_drv;
3355 }
3356
3357 ret = platform_device_add(serial8250_isa_devs);
3358 if (ret)
3359 goto put_dev;
3360
3361 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3362
3363 ret = platform_driver_register(&serial8250_isa_driver);
3364 if (ret == 0)
3365 goto out;
3366
3367 platform_device_del(serial8250_isa_devs);
3368put_dev:
3369 platform_device_put(serial8250_isa_devs);
3370unreg_uart_drv:
3371#ifdef CONFIG_SPARC
3372 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3373#else
3374 uart_unregister_driver(&serial8250_reg);
3375#endif
3376out:
3377 return ret;
3378}
3379
3380static void __exit serial8250_exit(void)
3381{
3382 struct platform_device *isa_dev = serial8250_isa_devs;
3383
3384 /*
3385 * This tells serial8250_unregister_port() not to re-register
3386 * the ports (thereby making serial8250_isa_driver permanently
3387 * in use.)
3388 */
3389 serial8250_isa_devs = NULL;
3390
3391 platform_driver_unregister(&serial8250_isa_driver);
3392 platform_device_unregister(isa_dev);
3393
3394#ifdef CONFIG_SPARC
3395 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3396#else
3397 uart_unregister_driver(&serial8250_reg);
3398#endif
3399}
3400
3401module_init(serial8250_init);
3402module_exit(serial8250_exit);
3403
3404EXPORT_SYMBOL(serial8250_suspend_port);
3405EXPORT_SYMBOL(serial8250_resume_port);
3406
3407MODULE_LICENSE("GPL");
3408MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
3409
3410module_param(share_irqs, uint, 0644);
3411MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3412 " (unsafe)");
3413
3414module_param(nr_uarts, uint, 0644);
3415MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3416
3417module_param(skip_txen_test, uint, 0644);
3418MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3419
3420#ifdef CONFIG_SERIAL_8250_RSA
3421module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3422MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3423#endif
3424MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
diff --git a/drivers/tty/serial/8250.h b/drivers/tty/serial/8250.h
new file mode 100644
index 000000000000..6edf4a6a22d4
--- /dev/null
+++ b/drivers/tty/serial/8250.h
@@ -0,0 +1,79 @@
1/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/serial_8250.h>
15
16struct old_serial_port {
17 unsigned int uart;
18 unsigned int baud_base;
19 unsigned int port;
20 unsigned int irq;
21 unsigned int flags;
22 unsigned char hub6;
23 unsigned char io_type;
24 unsigned char *iomem_base;
25 unsigned short iomem_reg_shift;
26 unsigned long irqflags;
27};
28
29/*
30 * This replaces serial_uart_config in include/linux/serial.h
31 */
32struct serial8250_config {
33 const char *name;
34 unsigned short fifo_size;
35 unsigned short tx_loadsz;
36 unsigned char fcr;
37 unsigned int flags;
38};
39
40#define UART_CAP_FIFO (1 << 8) /* UART has FIFO */
41#define UART_CAP_EFR (1 << 9) /* UART has EFR */
42#define UART_CAP_SLEEP (1 << 10) /* UART has IER sleep */
43#define UART_CAP_AFE (1 << 11) /* MCR-based hw flow control */
44#define UART_CAP_UUE (1 << 12) /* UART needs IER bit 6 set (Xscale) */
45#define UART_CAP_RTOIE (1 << 13) /* UART needs IER bit 4 set (Xscale, Tegra) */
46
47#define UART_BUG_QUOT (1 << 0) /* UART has buggy quot LSB */
48#define UART_BUG_TXEN (1 << 1) /* UART has buggy TX IIR status */
49#define UART_BUG_NOMSR (1 << 2) /* UART has buggy MSR status bits (Au1x00) */
50#define UART_BUG_THRE (1 << 3) /* UART has buggy THRE reassertion */
51
52#define PROBE_RSA (1 << 0)
53#define PROBE_ANY (~0)
54
55#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
56
57#ifdef CONFIG_SERIAL_8250_SHARE_IRQ
58#define SERIAL8250_SHARE_IRQS 1
59#else
60#define SERIAL8250_SHARE_IRQS 0
61#endif
62
63#if defined(__alpha__) && !defined(CONFIG_PCI)
64/*
65 * Digital did something really horribly wrong with the OUT1 and OUT2
66 * lines on at least some ALPHA's. The failure mode is that if either
67 * is cleared, the machine locks up with endless interrupts.
68 */
69#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2 | UART_MCR_OUT1)
70#elif defined(CONFIG_SBC8560)
71/*
72 * WindRiver did something similarly broken on their SBC8560 board. The
73 * UART tristates its IRQ output while OUT2 is clear, but they pulled
74 * the interrupt line _up_ instead of down, so if we register the IRQ
75 * while the UART is in that state, we die in an IRQ storm. */
76#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2)
77#else
78#define ALPHA_KLUDGE_MCR 0
79#endif
diff --git a/drivers/tty/serial/8250_accent.c b/drivers/tty/serial/8250_accent.c
new file mode 100644
index 000000000000..34b51c651192
--- /dev/null
+++ b/drivers/tty/serial/8250_accent.c
@@ -0,0 +1,45 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 }
21
22static struct plat_serial8250_port accent_data[] = {
23 PORT(0x330, 4),
24 PORT(0x338, 4),
25 { },
26};
27
28static struct platform_device accent_device = {
29 .name = "serial8250",
30 .id = PLAT8250_DEV_ACCENT,
31 .dev = {
32 .platform_data = accent_data,
33 },
34};
35
36static int __init accent_init(void)
37{
38 return platform_device_register(&accent_device);
39}
40
41module_init(accent_init);
42
43MODULE_AUTHOR("Russell King");
44MODULE_DESCRIPTION("8250 serial probe module for Accent Async cards");
45MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_acorn.c b/drivers/tty/serial/8250_acorn.c
new file mode 100644
index 000000000000..b0ce8c56f1a4
--- /dev/null
+++ b/drivers/tty/serial/8250_acorn.c
@@ -0,0 +1,141 @@
1/*
2 * linux/drivers/serial/acorn.c
3 *
4 * Copyright (C) 1996-2003 Russell King.
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 version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/tty.h>
13#include <linux/serial_core.h>
14#include <linux/errno.h>
15#include <linux/ioport.h>
16#include <linux/slab.h>
17#include <linux/device.h>
18#include <linux/init.h>
19
20#include <asm/io.h>
21#include <asm/ecard.h>
22#include <asm/string.h>
23
24#include "8250.h"
25
26#define MAX_PORTS 3
27
28struct serial_card_type {
29 unsigned int num_ports;
30 unsigned int uartclk;
31 unsigned int type;
32 unsigned int offset[MAX_PORTS];
33};
34
35struct serial_card_info {
36 unsigned int num_ports;
37 int ports[MAX_PORTS];
38 void __iomem *vaddr;
39};
40
41static int __devinit
42serial_card_probe(struct expansion_card *ec, const struct ecard_id *id)
43{
44 struct serial_card_info *info;
45 struct serial_card_type *type = id->data;
46 struct uart_port port;
47 unsigned long bus_addr;
48 unsigned int i;
49
50 info = kzalloc(sizeof(struct serial_card_info), GFP_KERNEL);
51 if (!info)
52 return -ENOMEM;
53
54 info->num_ports = type->num_ports;
55
56 bus_addr = ecard_resource_start(ec, type->type);
57 info->vaddr = ecardm_iomap(ec, type->type, 0, 0);
58 if (!info->vaddr) {
59 kfree(info);
60 return -ENOMEM;
61 }
62
63 ecard_set_drvdata(ec, info);
64
65 memset(&port, 0, sizeof(struct uart_port));
66 port.irq = ec->irq;
67 port.flags = UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
68 port.uartclk = type->uartclk;
69 port.iotype = UPIO_MEM;
70 port.regshift = 2;
71 port.dev = &ec->dev;
72
73 for (i = 0; i < info->num_ports; i ++) {
74 port.membase = info->vaddr + type->offset[i];
75 port.mapbase = bus_addr + type->offset[i];
76
77 info->ports[i] = serial8250_register_port(&port);
78 }
79
80 return 0;
81}
82
83static void __devexit serial_card_remove(struct expansion_card *ec)
84{
85 struct serial_card_info *info = ecard_get_drvdata(ec);
86 int i;
87
88 ecard_set_drvdata(ec, NULL);
89
90 for (i = 0; i < info->num_ports; i++)
91 if (info->ports[i] > 0)
92 serial8250_unregister_port(info->ports[i]);
93
94 kfree(info);
95}
96
97static struct serial_card_type atomwide_type = {
98 .num_ports = 3,
99 .uartclk = 7372800,
100 .type = ECARD_RES_IOCSLOW,
101 .offset = { 0x2800, 0x2400, 0x2000 },
102};
103
104static struct serial_card_type serport_type = {
105 .num_ports = 2,
106 .uartclk = 3686400,
107 .type = ECARD_RES_IOCSLOW,
108 .offset = { 0x2000, 0x2020 },
109};
110
111static const struct ecard_id serial_cids[] = {
112 { MANU_ATOMWIDE, PROD_ATOMWIDE_3PSERIAL, &atomwide_type },
113 { MANU_SERPORT, PROD_SERPORT_DSPORT, &serport_type },
114 { 0xffff, 0xffff }
115};
116
117static struct ecard_driver serial_card_driver = {
118 .probe = serial_card_probe,
119 .remove = __devexit_p(serial_card_remove),
120 .id_table = serial_cids,
121 .drv = {
122 .name = "8250_acorn",
123 },
124};
125
126static int __init serial_card_init(void)
127{
128 return ecard_register_driver(&serial_card_driver);
129}
130
131static void __exit serial_card_exit(void)
132{
133 ecard_remove_driver(&serial_card_driver);
134}
135
136MODULE_AUTHOR("Russell King");
137MODULE_DESCRIPTION("Acorn 8250-compatible serial port expansion card driver");
138MODULE_LICENSE("GPL");
139
140module_init(serial_card_init);
141module_exit(serial_card_exit);
diff --git a/drivers/tty/serial/8250_boca.c b/drivers/tty/serial/8250_boca.c
new file mode 100644
index 000000000000..d125dc107985
--- /dev/null
+++ b/drivers/tty/serial/8250_boca.c
@@ -0,0 +1,59 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 }
21
22static struct plat_serial8250_port boca_data[] = {
23 PORT(0x100, 12),
24 PORT(0x108, 12),
25 PORT(0x110, 12),
26 PORT(0x118, 12),
27 PORT(0x120, 12),
28 PORT(0x128, 12),
29 PORT(0x130, 12),
30 PORT(0x138, 12),
31 PORT(0x140, 12),
32 PORT(0x148, 12),
33 PORT(0x150, 12),
34 PORT(0x158, 12),
35 PORT(0x160, 12),
36 PORT(0x168, 12),
37 PORT(0x170, 12),
38 PORT(0x178, 12),
39 { },
40};
41
42static struct platform_device boca_device = {
43 .name = "serial8250",
44 .id = PLAT8250_DEV_BOCA,
45 .dev = {
46 .platform_data = boca_data,
47 },
48};
49
50static int __init boca_init(void)
51{
52 return platform_device_register(&boca_device);
53}
54
55module_init(boca_init);
56
57MODULE_AUTHOR("Russell King");
58MODULE_DESCRIPTION("8250 serial probe module for Boca cards");
59MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_early.c b/drivers/tty/serial/8250_early.c
new file mode 100644
index 000000000000..eaafb98debed
--- /dev/null
+++ b/drivers/tty/serial/8250_early.c
@@ -0,0 +1,287 @@
1/*
2 * Early serial console for 8250/16550 devices
3 *
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
13 *
14 * This is for use before the serial driver has initialized, in
15 * particular, before the UARTs have been discovered and named.
16 * Instead of specifying the console device as, e.g., "ttyS0",
17 * we locate the device directly by its MMIO or I/O port address.
18 *
19 * The user can specify the device directly, e.g.,
20 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
22 * earlycon=uart8250,mmio32,0xff5e0000,115200n8
23 * or
24 * console=uart8250,io,0x3f8,9600n8
25 * console=uart8250,mmio,0xff5e0000,115200n8
26 * console=uart8250,mmio32,0xff5e0000,115200n8
27 */
28
29#include <linux/tty.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/serial_core.h>
33#include <linux/serial_reg.h>
34#include <linux/serial.h>
35#include <linux/serial_8250.h>
36#include <asm/io.h>
37#include <asm/serial.h>
38#ifdef CONFIG_FIX_EARLYCON_MEM
39#include <asm/pgtable.h>
40#include <asm/fixmap.h>
41#endif
42
43struct early_serial8250_device {
44 struct uart_port port;
45 char options[16]; /* e.g., 115200n8 */
46 unsigned int baud;
47};
48
49static struct early_serial8250_device early_device;
50
51static unsigned int __init serial_in(struct uart_port *port, int offset)
52{
53 switch (port->iotype) {
54 case UPIO_MEM:
55 return readb(port->membase + offset);
56 case UPIO_MEM32:
57 return readl(port->membase + (offset << 2));
58 case UPIO_PORT:
59 return inb(port->iobase + offset);
60 default:
61 return 0;
62 }
63}
64
65static void __init serial_out(struct uart_port *port, int offset, int value)
66{
67 switch (port->iotype) {
68 case UPIO_MEM:
69 writeb(value, port->membase + offset);
70 break;
71 case UPIO_MEM32:
72 writel(value, port->membase + (offset << 2));
73 break;
74 case UPIO_PORT:
75 outb(value, port->iobase + offset);
76 break;
77 }
78}
79
80#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
81
82static void __init wait_for_xmitr(struct uart_port *port)
83{
84 unsigned int status;
85
86 for (;;) {
87 status = serial_in(port, UART_LSR);
88 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
89 return;
90 cpu_relax();
91 }
92}
93
94static void __init serial_putc(struct uart_port *port, int c)
95{
96 wait_for_xmitr(port);
97 serial_out(port, UART_TX, c);
98}
99
100static void __init early_serial8250_write(struct console *console,
101 const char *s, unsigned int count)
102{
103 struct uart_port *port = &early_device.port;
104 unsigned int ier;
105
106 /* Save the IER and disable interrupts */
107 ier = serial_in(port, UART_IER);
108 serial_out(port, UART_IER, 0);
109
110 uart_console_write(port, s, count, serial_putc);
111
112 /* Wait for transmitter to become empty and restore the IER */
113 wait_for_xmitr(port);
114 serial_out(port, UART_IER, ier);
115}
116
117static unsigned int __init probe_baud(struct uart_port *port)
118{
119 unsigned char lcr, dll, dlm;
120 unsigned int quot;
121
122 lcr = serial_in(port, UART_LCR);
123 serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
124 dll = serial_in(port, UART_DLL);
125 dlm = serial_in(port, UART_DLM);
126 serial_out(port, UART_LCR, lcr);
127
128 quot = (dlm << 8) | dll;
129 return (port->uartclk / 16) / quot;
130}
131
132static void __init init_port(struct early_serial8250_device *device)
133{
134 struct uart_port *port = &device->port;
135 unsigned int divisor;
136 unsigned char c;
137
138 serial_out(port, UART_LCR, 0x3); /* 8n1 */
139 serial_out(port, UART_IER, 0); /* no interrupt */
140 serial_out(port, UART_FCR, 0); /* no fifo */
141 serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
142
143 divisor = port->uartclk / (16 * device->baud);
144 c = serial_in(port, UART_LCR);
145 serial_out(port, UART_LCR, c | UART_LCR_DLAB);
146 serial_out(port, UART_DLL, divisor & 0xff);
147 serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
148 serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
149}
150
151static int __init parse_options(struct early_serial8250_device *device,
152 char *options)
153{
154 struct uart_port *port = &device->port;
155 int mmio, mmio32, length;
156
157 if (!options)
158 return -ENODEV;
159
160 port->uartclk = BASE_BAUD * 16;
161
162 mmio = !strncmp(options, "mmio,", 5);
163 mmio32 = !strncmp(options, "mmio32,", 7);
164 if (mmio || mmio32) {
165 port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
166 port->mapbase = simple_strtoul(options + (mmio ? 5 : 7),
167 &options, 0);
168 if (mmio32)
169 port->regshift = 2;
170#ifdef CONFIG_FIX_EARLYCON_MEM
171 set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
172 port->mapbase & PAGE_MASK);
173 port->membase =
174 (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
175 port->membase += port->mapbase & ~PAGE_MASK;
176#else
177 port->membase = ioremap_nocache(port->mapbase, 64);
178 if (!port->membase) {
179 printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
180 __func__,
181 (unsigned long long) port->mapbase);
182 return -ENOMEM;
183 }
184#endif
185 } else if (!strncmp(options, "io,", 3)) {
186 port->iotype = UPIO_PORT;
187 port->iobase = simple_strtoul(options + 3, &options, 0);
188 mmio = 0;
189 } else
190 return -EINVAL;
191
192 options = strchr(options, ',');
193 if (options) {
194 options++;
195 device->baud = simple_strtoul(options, NULL, 0);
196 length = min(strcspn(options, " "), sizeof(device->options));
197 strncpy(device->options, options, length);
198 } else {
199 device->baud = probe_baud(port);
200 snprintf(device->options, sizeof(device->options), "%u",
201 device->baud);
202 }
203
204 if (mmio || mmio32)
205 printk(KERN_INFO
206 "Early serial console at MMIO%s 0x%llx (options '%s')\n",
207 mmio32 ? "32" : "",
208 (unsigned long long)port->mapbase,
209 device->options);
210 else
211 printk(KERN_INFO
212 "Early serial console at I/O port 0x%lx (options '%s')\n",
213 port->iobase,
214 device->options);
215
216 return 0;
217}
218
219static struct console early_serial8250_console __initdata = {
220 .name = "uart",
221 .write = early_serial8250_write,
222 .flags = CON_PRINTBUFFER | CON_BOOT,
223 .index = -1,
224};
225
226static int __init early_serial8250_setup(char *options)
227{
228 struct early_serial8250_device *device = &early_device;
229 int err;
230
231 if (device->port.membase || device->port.iobase)
232 return 0;
233
234 err = parse_options(device, options);
235 if (err < 0)
236 return err;
237
238 init_port(device);
239 return 0;
240}
241
242int __init setup_early_serial8250_console(char *cmdline)
243{
244 char *options;
245 int err;
246
247 options = strstr(cmdline, "uart8250,");
248 if (!options) {
249 options = strstr(cmdline, "uart,");
250 if (!options)
251 return 0;
252 }
253
254 options = strchr(cmdline, ',') + 1;
255 err = early_serial8250_setup(options);
256 if (err < 0)
257 return err;
258
259 register_console(&early_serial8250_console);
260
261 return 0;
262}
263
264int serial8250_find_port_for_earlycon(void)
265{
266 struct early_serial8250_device *device = &early_device;
267 struct uart_port *port = &device->port;
268 int line;
269 int ret;
270
271 if (!device->port.membase && !device->port.iobase)
272 return -ENODEV;
273
274 line = serial8250_find_port(port);
275 if (line < 0)
276 return -ENODEV;
277
278 ret = update_console_cmdline("uart", 8250,
279 "ttyS", line, device->options);
280 if (ret < 0)
281 ret = update_console_cmdline("uart", 0,
282 "ttyS", line, device->options);
283
284 return ret;
285}
286
287early_param("earlycon", setup_early_serial8250_console);
diff --git a/drivers/tty/serial/8250_exar_st16c554.c b/drivers/tty/serial/8250_exar_st16c554.c
new file mode 100644
index 000000000000..bf53aabf9b5e
--- /dev/null
+++ b/drivers/tty/serial/8250_exar_st16c554.c
@@ -0,0 +1,50 @@
1/*
2 * Written by Paul B Schroeder < pschroeder "at" uplogix "dot" com >
3 * Based on 8250_boca.
4 *
5 * Copyright (C) 2005 Russell King.
6 * Data taken from include/asm-i386/serial.h
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/serial_8250.h>
15
16#define PORT(_base,_irq) \
17 { \
18 .iobase = _base, \
19 .irq = _irq, \
20 .uartclk = 1843200, \
21 .iotype = UPIO_PORT, \
22 .flags = UPF_BOOT_AUTOCONF, \
23 }
24
25static struct plat_serial8250_port exar_data[] = {
26 PORT(0x100, 5),
27 PORT(0x108, 5),
28 PORT(0x110, 5),
29 PORT(0x118, 5),
30 { },
31};
32
33static struct platform_device exar_device = {
34 .name = "serial8250",
35 .id = PLAT8250_DEV_EXAR_ST16C554,
36 .dev = {
37 .platform_data = exar_data,
38 },
39};
40
41static int __init exar_init(void)
42{
43 return platform_device_register(&exar_device);
44}
45
46module_init(exar_init);
47
48MODULE_AUTHOR("Paul B Schroeder");
49MODULE_DESCRIPTION("8250 serial probe module for Exar cards");
50MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_fourport.c b/drivers/tty/serial/8250_fourport.c
new file mode 100644
index 000000000000..be1582609626
--- /dev/null
+++ b/drivers/tty/serial/8250_fourport.c
@@ -0,0 +1,51 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF | UPF_FOURPORT, \
20 }
21
22static struct plat_serial8250_port fourport_data[] = {
23 PORT(0x1a0, 9),
24 PORT(0x1a8, 9),
25 PORT(0x1b0, 9),
26 PORT(0x1b8, 9),
27 PORT(0x2a0, 5),
28 PORT(0x2a8, 5),
29 PORT(0x2b0, 5),
30 PORT(0x2b8, 5),
31 { },
32};
33
34static struct platform_device fourport_device = {
35 .name = "serial8250",
36 .id = PLAT8250_DEV_FOURPORT,
37 .dev = {
38 .platform_data = fourport_data,
39 },
40};
41
42static int __init fourport_init(void)
43{
44 return platform_device_register(&fourport_device);
45}
46
47module_init(fourport_init);
48
49MODULE_AUTHOR("Russell King");
50MODULE_DESCRIPTION("8250 serial probe module for AST Fourport cards");
51MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_gsc.c b/drivers/tty/serial/8250_gsc.c
new file mode 100644
index 000000000000..d8c0ffbfa6e3
--- /dev/null
+++ b/drivers/tty/serial/8250_gsc.c
@@ -0,0 +1,122 @@
1/*
2 * Serial Device Initialisation for Lasi/Asp/Wax/Dino
3 *
4 * (c) Copyright Matthew Wilcox <willy@debian.org> 2001-2002
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
12#include <linux/errno.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/module.h>
17#include <linux/serial_core.h>
18#include <linux/signal.h>
19#include <linux/types.h>
20
21#include <asm/hardware.h>
22#include <asm/parisc-device.h>
23#include <asm/io.h>
24
25#include "8250.h"
26
27static int __init serial_init_chip(struct parisc_device *dev)
28{
29 struct uart_port port;
30 unsigned long address;
31 int err;
32
33 if (!dev->irq) {
34 /* We find some unattached serial ports by walking native
35 * busses. These should be silently ignored. Otherwise,
36 * what we have here is a missing parent device, so tell
37 * the user what they're missing.
38 */
39 if (parisc_parent(dev)->id.hw_type != HPHW_IOA)
40 printk(KERN_INFO
41 "Serial: device 0x%llx not configured.\n"
42 "Enable support for Wax, Lasi, Asp or Dino.\n",
43 (unsigned long long)dev->hpa.start);
44 return -ENODEV;
45 }
46
47 address = dev->hpa.start;
48 if (dev->id.sversion != 0x8d)
49 address += 0x800;
50
51 memset(&port, 0, sizeof(port));
52 port.iotype = UPIO_MEM;
53 /* 7.272727MHz on Lasi. Assumed the same for Dino, Wax and Timi. */
54 port.uartclk = 7272727;
55 port.mapbase = address;
56 port.membase = ioremap_nocache(address, 16);
57 port.irq = dev->irq;
58 port.flags = UPF_BOOT_AUTOCONF;
59 port.dev = &dev->dev;
60
61 err = serial8250_register_port(&port);
62 if (err < 0) {
63 printk(KERN_WARNING
64 "serial8250_register_port returned error %d\n", err);
65 iounmap(port.membase);
66 return err;
67 }
68
69 return 0;
70}
71
72static struct parisc_device_id serial_tbl[] = {
73 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00075 },
74 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0008c },
75 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0008d },
76 { 0 }
77};
78
79/* Hack. Some machines have SERIAL_0 attached to Lasi and SERIAL_1
80 * attached to Dino. Unfortunately, Dino appears before Lasi in the device
81 * tree. To ensure that ttyS0 == SERIAL_0, we register two drivers; one
82 * which only knows about Lasi and then a second which will find all the
83 * other serial ports. HPUX ignores this problem.
84 */
85static struct parisc_device_id lasi_tbl[] = {
86 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03B, 0x0008C }, /* C1xx/C1xxL */
87 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03C, 0x0008C }, /* B132L */
88 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03D, 0x0008C }, /* B160L */
89 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03E, 0x0008C }, /* B132L+ */
90 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x03F, 0x0008C }, /* B180L+ */
91 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x046, 0x0008C }, /* Rocky2 120 */
92 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x047, 0x0008C }, /* Rocky2 150 */
93 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x04E, 0x0008C }, /* Kiji L2 132 */
94 { HPHW_FIO, HVERSION_REV_ANY_ID, 0x056, 0x0008C }, /* Raven+ */
95 { 0 }
96};
97
98
99MODULE_DEVICE_TABLE(parisc, serial_tbl);
100
101static struct parisc_driver lasi_driver = {
102 .name = "serial_1",
103 .id_table = lasi_tbl,
104 .probe = serial_init_chip,
105};
106
107static struct parisc_driver serial_driver = {
108 .name = "serial",
109 .id_table = serial_tbl,
110 .probe = serial_init_chip,
111};
112
113static int __init probe_serial_gsc(void)
114{
115 register_parisc_driver(&lasi_driver);
116 register_parisc_driver(&serial_driver);
117 return 0;
118}
119
120module_init(probe_serial_gsc);
121
122MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_hp300.c b/drivers/tty/serial/8250_hp300.c
new file mode 100644
index 000000000000..c13438c93012
--- /dev/null
+++ b/drivers/tty/serial/8250_hp300.c
@@ -0,0 +1,327 @@
1/*
2 * Driver for the 98626/98644/internal serial interface on hp300/hp400
3 * (based on the National Semiconductor INS8250/NS16550AF/WD16C552 UARTs)
4 *
5 * Ported from 2.2 and modified to use the normal 8250 driver
6 * by Kars de Jong <jongk@linux-m68k.org>, May 2004.
7 */
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/string.h>
11#include <linux/kernel.h>
12#include <linux/serial.h>
13#include <linux/serial_core.h>
14#include <linux/serial_8250.h>
15#include <linux/delay.h>
16#include <linux/dio.h>
17#include <linux/console.h>
18#include <linux/slab.h>
19#include <asm/io.h>
20
21#include "8250.h"
22
23#if !defined(CONFIG_HPDCA) && !defined(CONFIG_HPAPCI)
24#warning CONFIG_8250 defined but neither CONFIG_HPDCA nor CONFIG_HPAPCI defined, are you sure?
25#endif
26
27#ifdef CONFIG_HPAPCI
28struct hp300_port
29{
30 struct hp300_port *next; /* next port */
31 int line; /* line (tty) number */
32};
33
34static struct hp300_port *hp300_ports;
35#endif
36
37#ifdef CONFIG_HPDCA
38
39static int __devinit hpdca_init_one(struct dio_dev *d,
40 const struct dio_device_id *ent);
41static void __devexit hpdca_remove_one(struct dio_dev *d);
42
43static struct dio_device_id hpdca_dio_tbl[] = {
44 { DIO_ID_DCA0 },
45 { DIO_ID_DCA0REM },
46 { DIO_ID_DCA1 },
47 { DIO_ID_DCA1REM },
48 { 0 }
49};
50
51static struct dio_driver hpdca_driver = {
52 .name = "hpdca",
53 .id_table = hpdca_dio_tbl,
54 .probe = hpdca_init_one,
55 .remove = __devexit_p(hpdca_remove_one),
56};
57
58#endif
59
60static unsigned int num_ports;
61
62extern int hp300_uart_scode;
63
64/* Offset to UART registers from base of DCA */
65#define UART_OFFSET 17
66
67#define DCA_ID 0x01 /* ID (read), reset (write) */
68#define DCA_IC 0x03 /* Interrupt control */
69
70/* Interrupt control */
71#define DCA_IC_IE 0x80 /* Master interrupt enable */
72
73#define HPDCA_BAUD_BASE 153600
74
75/* Base address of the Frodo part */
76#define FRODO_BASE (0x41c000)
77
78/*
79 * Where we find the 8250-like APCI ports, and how far apart they are.
80 */
81#define FRODO_APCIBASE 0x0
82#define FRODO_APCISPACE 0x20
83#define FRODO_APCI_OFFSET(x) (FRODO_APCIBASE + ((x) * FRODO_APCISPACE))
84
85#define HPAPCI_BAUD_BASE 500400
86
87#ifdef CONFIG_SERIAL_8250_CONSOLE
88/*
89 * Parse the bootinfo to find descriptions for headless console and
90 * debug serial ports and register them with the 8250 driver.
91 * This function should be called before serial_console_init() is called
92 * to make sure the serial console will be available for use. IA-64 kernel
93 * calls this function from setup_arch() after the EFI and ACPI tables have
94 * been parsed.
95 */
96int __init hp300_setup_serial_console(void)
97{
98 int scode;
99 struct uart_port port;
100
101 memset(&port, 0, sizeof(port));
102
103 if (hp300_uart_scode < 0 || hp300_uart_scode > DIO_SCMAX)
104 return 0;
105
106 if (DIO_SCINHOLE(hp300_uart_scode))
107 return 0;
108
109 scode = hp300_uart_scode;
110
111 /* Memory mapped I/O */
112 port.iotype = UPIO_MEM;
113 port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
114 port.type = PORT_UNKNOWN;
115
116 /* Check for APCI console */
117 if (scode == 256) {
118#ifdef CONFIG_HPAPCI
119 printk(KERN_INFO "Serial console is HP APCI 1\n");
120
121 port.uartclk = HPAPCI_BAUD_BASE * 16;
122 port.mapbase = (FRODO_BASE + FRODO_APCI_OFFSET(1));
123 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
124 port.regshift = 2;
125 add_preferred_console("ttyS", port.line, "9600n8");
126#else
127 printk(KERN_WARNING "Serial console is APCI but support is disabled (CONFIG_HPAPCI)!\n");
128 return 0;
129#endif
130 } else {
131#ifdef CONFIG_HPDCA
132 unsigned long pa = dio_scodetophysaddr(scode);
133 if (!pa)
134 return 0;
135
136 printk(KERN_INFO "Serial console is HP DCA at select code %d\n", scode);
137
138 port.uartclk = HPDCA_BAUD_BASE * 16;
139 port.mapbase = (pa + UART_OFFSET);
140 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
141 port.regshift = 1;
142 port.irq = DIO_IPL(pa + DIO_VIRADDRBASE);
143
144 /* Enable board-interrupts */
145 out_8(pa + DIO_VIRADDRBASE + DCA_IC, DCA_IC_IE);
146
147 if (DIO_ID(pa + DIO_VIRADDRBASE) & 0x80)
148 add_preferred_console("ttyS", port.line, "9600n8");
149#else
150 printk(KERN_WARNING "Serial console is DCA but support is disabled (CONFIG_HPDCA)!\n");
151 return 0;
152#endif
153 }
154
155 if (early_serial_setup(&port) < 0)
156 printk(KERN_WARNING "hp300_setup_serial_console(): early_serial_setup() failed.\n");
157 return 0;
158}
159#endif /* CONFIG_SERIAL_8250_CONSOLE */
160
161#ifdef CONFIG_HPDCA
162static int __devinit hpdca_init_one(struct dio_dev *d,
163 const struct dio_device_id *ent)
164{
165 struct uart_port port;
166 int line;
167
168#ifdef CONFIG_SERIAL_8250_CONSOLE
169 if (hp300_uart_scode == d->scode) {
170 /* Already got it. */
171 return 0;
172 }
173#endif
174 memset(&port, 0, sizeof(struct uart_port));
175
176 /* Memory mapped I/O */
177 port.iotype = UPIO_MEM;
178 port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF;
179 port.irq = d->ipl;
180 port.uartclk = HPDCA_BAUD_BASE * 16;
181 port.mapbase = (d->resource.start + UART_OFFSET);
182 port.membase = (char *)(port.mapbase + DIO_VIRADDRBASE);
183 port.regshift = 1;
184 port.dev = &d->dev;
185 line = serial8250_register_port(&port);
186
187 if (line < 0) {
188 printk(KERN_NOTICE "8250_hp300: register_serial() DCA scode %d"
189 " irq %d failed\n", d->scode, port.irq);
190 return -ENOMEM;
191 }
192
193 /* Enable board-interrupts */
194 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_IC, DCA_IC_IE);
195 dio_set_drvdata(d, (void *)line);
196
197 /* Reset the DCA */
198 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_ID, 0xff);
199 udelay(100);
200
201 num_ports++;
202
203 return 0;
204}
205#endif
206
207static int __init hp300_8250_init(void)
208{
209 static int called;
210#ifdef CONFIG_HPAPCI
211 int line;
212 unsigned long base;
213 struct uart_port uport;
214 struct hp300_port *port;
215 int i;
216#endif
217 if (called)
218 return -ENODEV;
219 called = 1;
220
221 if (!MACH_IS_HP300)
222 return -ENODEV;
223
224#ifdef CONFIG_HPDCA
225 dio_register_driver(&hpdca_driver);
226#endif
227#ifdef CONFIG_HPAPCI
228 if (hp300_model < HP_400) {
229 if (!num_ports)
230 return -ENODEV;
231 return 0;
232 }
233 /* These models have the Frodo chip.
234 * Port 0 is reserved for the Apollo Domain keyboard.
235 * Port 1 is either the console or the DCA.
236 */
237 for (i = 1; i < 4; i++) {
238 /* Port 1 is the console on a 425e, on other machines it's
239 * mapped to DCA.
240 */
241#ifdef CONFIG_SERIAL_8250_CONSOLE
242 if (i == 1)
243 continue;
244#endif
245
246 /* Create new serial device */
247 port = kmalloc(sizeof(struct hp300_port), GFP_KERNEL);
248 if (!port)
249 return -ENOMEM;
250
251 memset(&uport, 0, sizeof(struct uart_port));
252
253 base = (FRODO_BASE + FRODO_APCI_OFFSET(i));
254
255 /* Memory mapped I/O */
256 uport.iotype = UPIO_MEM;
257 uport.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ \
258 | UPF_BOOT_AUTOCONF;
259 /* XXX - no interrupt support yet */
260 uport.irq = 0;
261 uport.uartclk = HPAPCI_BAUD_BASE * 16;
262 uport.mapbase = base;
263 uport.membase = (char *)(base + DIO_VIRADDRBASE);
264 uport.regshift = 2;
265
266 line = serial8250_register_port(&uport);
267
268 if (line < 0) {
269 printk(KERN_NOTICE "8250_hp300: register_serial() APCI"
270 " %d irq %d failed\n", i, uport.irq);
271 kfree(port);
272 continue;
273 }
274
275 port->line = line;
276 port->next = hp300_ports;
277 hp300_ports = port;
278
279 num_ports++;
280 }
281#endif
282
283 /* Any boards found? */
284 if (!num_ports)
285 return -ENODEV;
286
287 return 0;
288}
289
290#ifdef CONFIG_HPDCA
291static void __devexit hpdca_remove_one(struct dio_dev *d)
292{
293 int line;
294
295 line = (int) dio_get_drvdata(d);
296 if (d->resource.start) {
297 /* Disable board-interrupts */
298 out_8(d->resource.start + DIO_VIRADDRBASE + DCA_IC, 0);
299 }
300 serial8250_unregister_port(line);
301}
302#endif
303
304static void __exit hp300_8250_exit(void)
305{
306#ifdef CONFIG_HPAPCI
307 struct hp300_port *port, *to_free;
308
309 for (port = hp300_ports; port; ) {
310 serial8250_unregister_port(port->line);
311 to_free = port;
312 port = port->next;
313 kfree(to_free);
314 }
315
316 hp300_ports = NULL;
317#endif
318#ifdef CONFIG_HPDCA
319 dio_unregister_driver(&hpdca_driver);
320#endif
321}
322
323module_init(hp300_8250_init);
324module_exit(hp300_8250_exit);
325MODULE_DESCRIPTION("HP DCA/APCI serial driver");
326MODULE_AUTHOR("Kars de Jong <jongk@linux-m68k.org>");
327MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_hub6.c b/drivers/tty/serial/8250_hub6.c
new file mode 100644
index 000000000000..a5c778e83de0
--- /dev/null
+++ b/drivers/tty/serial/8250_hub6.c
@@ -0,0 +1,56 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define HUB6(card,port) \
14 { \
15 .iobase = 0x302, \
16 .irq = 3, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_HUB6, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 .hub6 = (card) << 6 | (port) << 3 | 1, \
21 }
22
23static struct plat_serial8250_port hub6_data[] = {
24 HUB6(0, 0),
25 HUB6(0, 1),
26 HUB6(0, 2),
27 HUB6(0, 3),
28 HUB6(0, 4),
29 HUB6(0, 5),
30 HUB6(1, 0),
31 HUB6(1, 1),
32 HUB6(1, 2),
33 HUB6(1, 3),
34 HUB6(1, 4),
35 HUB6(1, 5),
36 { },
37};
38
39static struct platform_device hub6_device = {
40 .name = "serial8250",
41 .id = PLAT8250_DEV_HUB6,
42 .dev = {
43 .platform_data = hub6_data,
44 },
45};
46
47static int __init hub6_init(void)
48{
49 return platform_device_register(&hub6_device);
50}
51
52module_init(hub6_init);
53
54MODULE_AUTHOR("Russell King");
55MODULE_DESCRIPTION("8250 serial probe module for Hub6 cards");
56MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_mca.c b/drivers/tty/serial/8250_mca.c
new file mode 100644
index 000000000000..d20abf04541e
--- /dev/null
+++ b/drivers/tty/serial/8250_mca.c
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/mca.h>
12#include <linux/serial_8250.h>
13
14/*
15 * FIXME: Should we be doing AUTO_IRQ here?
16 */
17#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
18#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_AUTO_IRQ
19#else
20#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST
21#endif
22
23#define PORT(_base,_irq) \
24 { \
25 .iobase = _base, \
26 .irq = _irq, \
27 .uartclk = 1843200, \
28 .iotype = UPIO_PORT, \
29 .flags = MCA_FLAGS, \
30 }
31
32static struct plat_serial8250_port mca_data[] = {
33 PORT(0x3220, 3),
34 PORT(0x3228, 3),
35 PORT(0x4220, 3),
36 PORT(0x4228, 3),
37 PORT(0x5220, 3),
38 PORT(0x5228, 3),
39 { },
40};
41
42static struct platform_device mca_device = {
43 .name = "serial8250",
44 .id = PLAT8250_DEV_MCA,
45 .dev = {
46 .platform_data = mca_data,
47 },
48};
49
50static int __init mca_init(void)
51{
52 if (!MCA_bus)
53 return -ENODEV;
54 return platform_device_register(&mca_device);
55}
56
57module_init(mca_init);
58
59MODULE_AUTHOR("Russell King");
60MODULE_DESCRIPTION("8250 serial probe module for MCA ports");
61MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_pci.c b/drivers/tty/serial/8250_pci.c
new file mode 100644
index 000000000000..f41b4259ecdd
--- /dev/null
+++ b/drivers/tty/serial/8250_pci.c
@@ -0,0 +1,3956 @@
1/*
2 * Probe module for 8250/16550-type PCI serial ports.
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King, All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/pci.h>
15#include <linux/string.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/serial_core.h>
21#include <linux/8250_pci.h>
22#include <linux/bitops.h>
23
24#include <asm/byteorder.h>
25#include <asm/io.h>
26
27#include "8250.h"
28
29#undef SERIAL_DEBUG_PCI
30
31/*
32 * init function returns:
33 * > 0 - number of ports
34 * = 0 - use board->num_ports
35 * < 0 - error
36 */
37struct pci_serial_quirk {
38 u32 vendor;
39 u32 device;
40 u32 subvendor;
41 u32 subdevice;
42 int (*init)(struct pci_dev *dev);
43 int (*setup)(struct serial_private *,
44 const struct pciserial_board *,
45 struct uart_port *, int);
46 void (*exit)(struct pci_dev *dev);
47};
48
49#define PCI_NUM_BAR_RESOURCES 6
50
51struct serial_private {
52 struct pci_dev *dev;
53 unsigned int nr;
54 void __iomem *remapped_bar[PCI_NUM_BAR_RESOURCES];
55 struct pci_serial_quirk *quirk;
56 int line[0];
57};
58
59static void moan_device(const char *str, struct pci_dev *dev)
60{
61 printk(KERN_WARNING
62 "%s: %s\n"
63 "Please send the output of lspci -vv, this\n"
64 "message (0x%04x,0x%04x,0x%04x,0x%04x), the\n"
65 "manufacturer and name of serial board or\n"
66 "modem board to rmk+serial@arm.linux.org.uk.\n",
67 pci_name(dev), str, dev->vendor, dev->device,
68 dev->subsystem_vendor, dev->subsystem_device);
69}
70
71static int
72setup_port(struct serial_private *priv, struct uart_port *port,
73 int bar, int offset, int regshift)
74{
75 struct pci_dev *dev = priv->dev;
76 unsigned long base, len;
77
78 if (bar >= PCI_NUM_BAR_RESOURCES)
79 return -EINVAL;
80
81 base = pci_resource_start(dev, bar);
82
83 if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
84 len = pci_resource_len(dev, bar);
85
86 if (!priv->remapped_bar[bar])
87 priv->remapped_bar[bar] = ioremap_nocache(base, len);
88 if (!priv->remapped_bar[bar])
89 return -ENOMEM;
90
91 port->iotype = UPIO_MEM;
92 port->iobase = 0;
93 port->mapbase = base + offset;
94 port->membase = priv->remapped_bar[bar] + offset;
95 port->regshift = regshift;
96 } else {
97 port->iotype = UPIO_PORT;
98 port->iobase = base + offset;
99 port->mapbase = 0;
100 port->membase = NULL;
101 port->regshift = 0;
102 }
103 return 0;
104}
105
106/*
107 * ADDI-DATA GmbH communication cards <info@addi-data.com>
108 */
109static int addidata_apci7800_setup(struct serial_private *priv,
110 const struct pciserial_board *board,
111 struct uart_port *port, int idx)
112{
113 unsigned int bar = 0, offset = board->first_offset;
114 bar = FL_GET_BASE(board->flags);
115
116 if (idx < 2) {
117 offset += idx * board->uart_offset;
118 } else if ((idx >= 2) && (idx < 4)) {
119 bar += 1;
120 offset += ((idx - 2) * board->uart_offset);
121 } else if ((idx >= 4) && (idx < 6)) {
122 bar += 2;
123 offset += ((idx - 4) * board->uart_offset);
124 } else if (idx >= 6) {
125 bar += 3;
126 offset += ((idx - 6) * board->uart_offset);
127 }
128
129 return setup_port(priv, port, bar, offset, board->reg_shift);
130}
131
132/*
133 * AFAVLAB uses a different mixture of BARs and offsets
134 * Not that ugly ;) -- HW
135 */
136static int
137afavlab_setup(struct serial_private *priv, const struct pciserial_board *board,
138 struct uart_port *port, int idx)
139{
140 unsigned int bar, offset = board->first_offset;
141
142 bar = FL_GET_BASE(board->flags);
143 if (idx < 4)
144 bar += idx;
145 else {
146 bar = 4;
147 offset += (idx - 4) * board->uart_offset;
148 }
149
150 return setup_port(priv, port, bar, offset, board->reg_shift);
151}
152
153/*
154 * HP's Remote Management Console. The Diva chip came in several
155 * different versions. N-class, L2000 and A500 have two Diva chips, each
156 * with 3 UARTs (the third UART on the second chip is unused). Superdome
157 * and Keystone have one Diva chip with 3 UARTs. Some later machines have
158 * one Diva chip, but it has been expanded to 5 UARTs.
159 */
160static int pci_hp_diva_init(struct pci_dev *dev)
161{
162 int rc = 0;
163
164 switch (dev->subsystem_device) {
165 case PCI_DEVICE_ID_HP_DIVA_TOSCA1:
166 case PCI_DEVICE_ID_HP_DIVA_HALFDOME:
167 case PCI_DEVICE_ID_HP_DIVA_KEYSTONE:
168 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
169 rc = 3;
170 break;
171 case PCI_DEVICE_ID_HP_DIVA_TOSCA2:
172 rc = 2;
173 break;
174 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
175 rc = 4;
176 break;
177 case PCI_DEVICE_ID_HP_DIVA_POWERBAR:
178 case PCI_DEVICE_ID_HP_DIVA_HURRICANE:
179 rc = 1;
180 break;
181 }
182
183 return rc;
184}
185
186/*
187 * HP's Diva chip puts the 4th/5th serial port further out, and
188 * some serial ports are supposed to be hidden on certain models.
189 */
190static int
191pci_hp_diva_setup(struct serial_private *priv,
192 const struct pciserial_board *board,
193 struct uart_port *port, int idx)
194{
195 unsigned int offset = board->first_offset;
196 unsigned int bar = FL_GET_BASE(board->flags);
197
198 switch (priv->dev->subsystem_device) {
199 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
200 if (idx == 3)
201 idx++;
202 break;
203 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
204 if (idx > 0)
205 idx++;
206 if (idx > 2)
207 idx++;
208 break;
209 }
210 if (idx > 2)
211 offset = 0x18;
212
213 offset += idx * board->uart_offset;
214
215 return setup_port(priv, port, bar, offset, board->reg_shift);
216}
217
218/*
219 * Added for EKF Intel i960 serial boards
220 */
221static int pci_inteli960ni_init(struct pci_dev *dev)
222{
223 unsigned long oldval;
224
225 if (!(dev->subsystem_device & 0x1000))
226 return -ENODEV;
227
228 /* is firmware started? */
229 pci_read_config_dword(dev, 0x44, (void *)&oldval);
230 if (oldval == 0x00001000L) { /* RESET value */
231 printk(KERN_DEBUG "Local i960 firmware missing");
232 return -ENODEV;
233 }
234 return 0;
235}
236
237/*
238 * Some PCI serial cards using the PLX 9050 PCI interface chip require
239 * that the card interrupt be explicitly enabled or disabled. This
240 * seems to be mainly needed on card using the PLX which also use I/O
241 * mapped memory.
242 */
243static int pci_plx9050_init(struct pci_dev *dev)
244{
245 u8 irq_config;
246 void __iomem *p;
247
248 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0) {
249 moan_device("no memory in bar 0", dev);
250 return 0;
251 }
252
253 irq_config = 0x41;
254 if (dev->vendor == PCI_VENDOR_ID_PANACOM ||
255 dev->subsystem_vendor == PCI_SUBVENDOR_ID_EXSYS)
256 irq_config = 0x43;
257
258 if ((dev->vendor == PCI_VENDOR_ID_PLX) &&
259 (dev->device == PCI_DEVICE_ID_PLX_ROMULUS))
260 /*
261 * As the megawolf cards have the int pins active
262 * high, and have 2 UART chips, both ints must be
263 * enabled on the 9050. Also, the UARTS are set in
264 * 16450 mode by default, so we have to enable the
265 * 16C950 'enhanced' mode so that we can use the
266 * deep FIFOs
267 */
268 irq_config = 0x5b;
269 /*
270 * enable/disable interrupts
271 */
272 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
273 if (p == NULL)
274 return -ENOMEM;
275 writel(irq_config, p + 0x4c);
276
277 /*
278 * Read the register back to ensure that it took effect.
279 */
280 readl(p + 0x4c);
281 iounmap(p);
282
283 return 0;
284}
285
286static void __devexit pci_plx9050_exit(struct pci_dev *dev)
287{
288 u8 __iomem *p;
289
290 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0)
291 return;
292
293 /*
294 * disable interrupts
295 */
296 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
297 if (p != NULL) {
298 writel(0, p + 0x4c);
299
300 /*
301 * Read the register back to ensure that it took effect.
302 */
303 readl(p + 0x4c);
304 iounmap(p);
305 }
306}
307
308#define NI8420_INT_ENABLE_REG 0x38
309#define NI8420_INT_ENABLE_BIT 0x2000
310
311static void __devexit pci_ni8420_exit(struct pci_dev *dev)
312{
313 void __iomem *p;
314 unsigned long base, len;
315 unsigned int bar = 0;
316
317 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
318 moan_device("no memory in bar", dev);
319 return;
320 }
321
322 base = pci_resource_start(dev, bar);
323 len = pci_resource_len(dev, bar);
324 p = ioremap_nocache(base, len);
325 if (p == NULL)
326 return;
327
328 /* Disable the CPU Interrupt */
329 writel(readl(p + NI8420_INT_ENABLE_REG) & ~(NI8420_INT_ENABLE_BIT),
330 p + NI8420_INT_ENABLE_REG);
331 iounmap(p);
332}
333
334
335/* MITE registers */
336#define MITE_IOWBSR1 0xc4
337#define MITE_IOWCR1 0xf4
338#define MITE_LCIMR1 0x08
339#define MITE_LCIMR2 0x10
340
341#define MITE_LCIMR2_CLR_CPU_IE (1 << 30)
342
343static void __devexit pci_ni8430_exit(struct pci_dev *dev)
344{
345 void __iomem *p;
346 unsigned long base, len;
347 unsigned int bar = 0;
348
349 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
350 moan_device("no memory in bar", dev);
351 return;
352 }
353
354 base = pci_resource_start(dev, bar);
355 len = pci_resource_len(dev, bar);
356 p = ioremap_nocache(base, len);
357 if (p == NULL)
358 return;
359
360 /* Disable the CPU Interrupt */
361 writel(MITE_LCIMR2_CLR_CPU_IE, p + MITE_LCIMR2);
362 iounmap(p);
363}
364
365/* SBS Technologies Inc. PMC-OCTPRO and P-OCTAL cards */
366static int
367sbs_setup(struct serial_private *priv, const struct pciserial_board *board,
368 struct uart_port *port, int idx)
369{
370 unsigned int bar, offset = board->first_offset;
371
372 bar = 0;
373
374 if (idx < 4) {
375 /* first four channels map to 0, 0x100, 0x200, 0x300 */
376 offset += idx * board->uart_offset;
377 } else if (idx < 8) {
378 /* last four channels map to 0x1000, 0x1100, 0x1200, 0x1300 */
379 offset += idx * board->uart_offset + 0xC00;
380 } else /* we have only 8 ports on PMC-OCTALPRO */
381 return 1;
382
383 return setup_port(priv, port, bar, offset, board->reg_shift);
384}
385
386/*
387* This does initialization for PMC OCTALPRO cards:
388* maps the device memory, resets the UARTs (needed, bc
389* if the module is removed and inserted again, the card
390* is in the sleep mode) and enables global interrupt.
391*/
392
393/* global control register offset for SBS PMC-OctalPro */
394#define OCT_REG_CR_OFF 0x500
395
396static int sbs_init(struct pci_dev *dev)
397{
398 u8 __iomem *p;
399
400 p = pci_ioremap_bar(dev, 0);
401
402 if (p == NULL)
403 return -ENOMEM;
404 /* Set bit-4 Control Register (UART RESET) in to reset the uarts */
405 writeb(0x10, p + OCT_REG_CR_OFF);
406 udelay(50);
407 writeb(0x0, p + OCT_REG_CR_OFF);
408
409 /* Set bit-2 (INTENABLE) of Control Register */
410 writeb(0x4, p + OCT_REG_CR_OFF);
411 iounmap(p);
412
413 return 0;
414}
415
416/*
417 * Disables the global interrupt of PMC-OctalPro
418 */
419
420static void __devexit sbs_exit(struct pci_dev *dev)
421{
422 u8 __iomem *p;
423
424 p = pci_ioremap_bar(dev, 0);
425 /* FIXME: What if resource_len < OCT_REG_CR_OFF */
426 if (p != NULL)
427 writeb(0, p + OCT_REG_CR_OFF);
428 iounmap(p);
429}
430
431/*
432 * SIIG serial cards have an PCI interface chip which also controls
433 * the UART clocking frequency. Each UART can be clocked independently
434 * (except cards equipped with 4 UARTs) and initial clocking settings
435 * are stored in the EEPROM chip. It can cause problems because this
436 * version of serial driver doesn't support differently clocked UART's
437 * on single PCI card. To prevent this, initialization functions set
438 * high frequency clocking for all UART's on given card. It is safe (I
439 * hope) because it doesn't touch EEPROM settings to prevent conflicts
440 * with other OSes (like M$ DOS).
441 *
442 * SIIG support added by Andrey Panin <pazke@donpac.ru>, 10/1999
443 *
444 * There is two family of SIIG serial cards with different PCI
445 * interface chip and different configuration methods:
446 * - 10x cards have control registers in IO and/or memory space;
447 * - 20x cards have control registers in standard PCI configuration space.
448 *
449 * Note: all 10x cards have PCI device ids 0x10..
450 * all 20x cards have PCI device ids 0x20..
451 *
452 * There are also Quartet Serial cards which use Oxford Semiconductor
453 * 16954 quad UART PCI chip clocked by 18.432 MHz quartz.
454 *
455 * Note: some SIIG cards are probed by the parport_serial object.
456 */
457
458#define PCI_DEVICE_ID_SIIG_1S_10x (PCI_DEVICE_ID_SIIG_1S_10x_550 & 0xfffc)
459#define PCI_DEVICE_ID_SIIG_2S_10x (PCI_DEVICE_ID_SIIG_2S_10x_550 & 0xfff8)
460
461static int pci_siig10x_init(struct pci_dev *dev)
462{
463 u16 data;
464 void __iomem *p;
465
466 switch (dev->device & 0xfff8) {
467 case PCI_DEVICE_ID_SIIG_1S_10x: /* 1S */
468 data = 0xffdf;
469 break;
470 case PCI_DEVICE_ID_SIIG_2S_10x: /* 2S, 2S1P */
471 data = 0xf7ff;
472 break;
473 default: /* 1S1P, 4S */
474 data = 0xfffb;
475 break;
476 }
477
478 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
479 if (p == NULL)
480 return -ENOMEM;
481
482 writew(readw(p + 0x28) & data, p + 0x28);
483 readw(p + 0x28);
484 iounmap(p);
485 return 0;
486}
487
488#define PCI_DEVICE_ID_SIIG_2S_20x (PCI_DEVICE_ID_SIIG_2S_20x_550 & 0xfffc)
489#define PCI_DEVICE_ID_SIIG_2S1P_20x (PCI_DEVICE_ID_SIIG_2S1P_20x_550 & 0xfffc)
490
491static int pci_siig20x_init(struct pci_dev *dev)
492{
493 u8 data;
494
495 /* Change clock frequency for the first UART. */
496 pci_read_config_byte(dev, 0x6f, &data);
497 pci_write_config_byte(dev, 0x6f, data & 0xef);
498
499 /* If this card has 2 UART, we have to do the same with second UART. */
500 if (((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S_20x) ||
501 ((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S1P_20x)) {
502 pci_read_config_byte(dev, 0x73, &data);
503 pci_write_config_byte(dev, 0x73, data & 0xef);
504 }
505 return 0;
506}
507
508static int pci_siig_init(struct pci_dev *dev)
509{
510 unsigned int type = dev->device & 0xff00;
511
512 if (type == 0x1000)
513 return pci_siig10x_init(dev);
514 else if (type == 0x2000)
515 return pci_siig20x_init(dev);
516
517 moan_device("Unknown SIIG card", dev);
518 return -ENODEV;
519}
520
521static int pci_siig_setup(struct serial_private *priv,
522 const struct pciserial_board *board,
523 struct uart_port *port, int idx)
524{
525 unsigned int bar = FL_GET_BASE(board->flags) + idx, offset = 0;
526
527 if (idx > 3) {
528 bar = 4;
529 offset = (idx - 4) * 8;
530 }
531
532 return setup_port(priv, port, bar, offset, 0);
533}
534
535/*
536 * Timedia has an explosion of boards, and to avoid the PCI table from
537 * growing *huge*, we use this function to collapse some 70 entries
538 * in the PCI table into one, for sanity's and compactness's sake.
539 */
540static const unsigned short timedia_single_port[] = {
541 0x4025, 0x4027, 0x4028, 0x5025, 0x5027, 0
542};
543
544static const unsigned short timedia_dual_port[] = {
545 0x0002, 0x4036, 0x4037, 0x4038, 0x4078, 0x4079, 0x4085,
546 0x4088, 0x4089, 0x5037, 0x5078, 0x5079, 0x5085, 0x6079,
547 0x7079, 0x8079, 0x8137, 0x8138, 0x8237, 0x8238, 0x9079,
548 0x9137, 0x9138, 0x9237, 0x9238, 0xA079, 0xB079, 0xC079,
549 0xD079, 0
550};
551
552static const unsigned short timedia_quad_port[] = {
553 0x4055, 0x4056, 0x4095, 0x4096, 0x5056, 0x8156, 0x8157,
554 0x8256, 0x8257, 0x9056, 0x9156, 0x9157, 0x9158, 0x9159,
555 0x9256, 0x9257, 0xA056, 0xA157, 0xA158, 0xA159, 0xB056,
556 0xB157, 0
557};
558
559static const unsigned short timedia_eight_port[] = {
560 0x4065, 0x4066, 0x5065, 0x5066, 0x8166, 0x9066, 0x9166,
561 0x9167, 0x9168, 0xA066, 0xA167, 0xA168, 0
562};
563
564static const struct timedia_struct {
565 int num;
566 const unsigned short *ids;
567} timedia_data[] = {
568 { 1, timedia_single_port },
569 { 2, timedia_dual_port },
570 { 4, timedia_quad_port },
571 { 8, timedia_eight_port }
572};
573
574static int pci_timedia_init(struct pci_dev *dev)
575{
576 const unsigned short *ids;
577 int i, j;
578
579 for (i = 0; i < ARRAY_SIZE(timedia_data); i++) {
580 ids = timedia_data[i].ids;
581 for (j = 0; ids[j]; j++)
582 if (dev->subsystem_device == ids[j])
583 return timedia_data[i].num;
584 }
585 return 0;
586}
587
588/*
589 * Timedia/SUNIX uses a mixture of BARs and offsets
590 * Ugh, this is ugly as all hell --- TYT
591 */
592static int
593pci_timedia_setup(struct serial_private *priv,
594 const struct pciserial_board *board,
595 struct uart_port *port, int idx)
596{
597 unsigned int bar = 0, offset = board->first_offset;
598
599 switch (idx) {
600 case 0:
601 bar = 0;
602 break;
603 case 1:
604 offset = board->uart_offset;
605 bar = 0;
606 break;
607 case 2:
608 bar = 1;
609 break;
610 case 3:
611 offset = board->uart_offset;
612 /* FALLTHROUGH */
613 case 4: /* BAR 2 */
614 case 5: /* BAR 3 */
615 case 6: /* BAR 4 */
616 case 7: /* BAR 5 */
617 bar = idx - 2;
618 }
619
620 return setup_port(priv, port, bar, offset, board->reg_shift);
621}
622
623/*
624 * Some Titan cards are also a little weird
625 */
626static int
627titan_400l_800l_setup(struct serial_private *priv,
628 const struct pciserial_board *board,
629 struct uart_port *port, int idx)
630{
631 unsigned int bar, offset = board->first_offset;
632
633 switch (idx) {
634 case 0:
635 bar = 1;
636 break;
637 case 1:
638 bar = 2;
639 break;
640 default:
641 bar = 4;
642 offset = (idx - 2) * board->uart_offset;
643 }
644
645 return setup_port(priv, port, bar, offset, board->reg_shift);
646}
647
648static int pci_xircom_init(struct pci_dev *dev)
649{
650 msleep(100);
651 return 0;
652}
653
654static int pci_ni8420_init(struct pci_dev *dev)
655{
656 void __iomem *p;
657 unsigned long base, len;
658 unsigned int bar = 0;
659
660 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
661 moan_device("no memory in bar", dev);
662 return 0;
663 }
664
665 base = pci_resource_start(dev, bar);
666 len = pci_resource_len(dev, bar);
667 p = ioremap_nocache(base, len);
668 if (p == NULL)
669 return -ENOMEM;
670
671 /* Enable CPU Interrupt */
672 writel(readl(p + NI8420_INT_ENABLE_REG) | NI8420_INT_ENABLE_BIT,
673 p + NI8420_INT_ENABLE_REG);
674
675 iounmap(p);
676 return 0;
677}
678
679#define MITE_IOWBSR1_WSIZE 0xa
680#define MITE_IOWBSR1_WIN_OFFSET 0x800
681#define MITE_IOWBSR1_WENAB (1 << 7)
682#define MITE_LCIMR1_IO_IE_0 (1 << 24)
683#define MITE_LCIMR2_SET_CPU_IE (1 << 31)
684#define MITE_IOWCR1_RAMSEL_MASK 0xfffffffe
685
686static int pci_ni8430_init(struct pci_dev *dev)
687{
688 void __iomem *p;
689 unsigned long base, len;
690 u32 device_window;
691 unsigned int bar = 0;
692
693 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
694 moan_device("no memory in bar", dev);
695 return 0;
696 }
697
698 base = pci_resource_start(dev, bar);
699 len = pci_resource_len(dev, bar);
700 p = ioremap_nocache(base, len);
701 if (p == NULL)
702 return -ENOMEM;
703
704 /* Set device window address and size in BAR0 */
705 device_window = ((base + MITE_IOWBSR1_WIN_OFFSET) & 0xffffff00)
706 | MITE_IOWBSR1_WENAB | MITE_IOWBSR1_WSIZE;
707 writel(device_window, p + MITE_IOWBSR1);
708
709 /* Set window access to go to RAMSEL IO address space */
710 writel((readl(p + MITE_IOWCR1) & MITE_IOWCR1_RAMSEL_MASK),
711 p + MITE_IOWCR1);
712
713 /* Enable IO Bus Interrupt 0 */
714 writel(MITE_LCIMR1_IO_IE_0, p + MITE_LCIMR1);
715
716 /* Enable CPU Interrupt */
717 writel(MITE_LCIMR2_SET_CPU_IE, p + MITE_LCIMR2);
718
719 iounmap(p);
720 return 0;
721}
722
723/* UART Port Control Register */
724#define NI8430_PORTCON 0x0f
725#define NI8430_PORTCON_TXVR_ENABLE (1 << 3)
726
727static int
728pci_ni8430_setup(struct serial_private *priv,
729 const struct pciserial_board *board,
730 struct uart_port *port, int idx)
731{
732 void __iomem *p;
733 unsigned long base, len;
734 unsigned int bar, offset = board->first_offset;
735
736 if (idx >= board->num_ports)
737 return 1;
738
739 bar = FL_GET_BASE(board->flags);
740 offset += idx * board->uart_offset;
741
742 base = pci_resource_start(priv->dev, bar);
743 len = pci_resource_len(priv->dev, bar);
744 p = ioremap_nocache(base, len);
745
746 /* enable the transciever */
747 writeb(readb(p + offset + NI8430_PORTCON) | NI8430_PORTCON_TXVR_ENABLE,
748 p + offset + NI8430_PORTCON);
749
750 iounmap(p);
751
752 return setup_port(priv, port, bar, offset, board->reg_shift);
753}
754
755
756static int pci_netmos_init(struct pci_dev *dev)
757{
758 /* subdevice 0x00PS means <P> parallel, <S> serial */
759 unsigned int num_serial = dev->subsystem_device & 0xf;
760
761 if ((dev->device == PCI_DEVICE_ID_NETMOS_9901) ||
762 (dev->device == PCI_DEVICE_ID_NETMOS_9865))
763 return 0;
764 if (dev->subsystem_vendor == PCI_VENDOR_ID_IBM &&
765 dev->subsystem_device == 0x0299)
766 return 0;
767
768 if (num_serial == 0)
769 return -ENODEV;
770 return num_serial;
771}
772
773/*
774 * These chips are available with optionally one parallel port and up to
775 * two serial ports. Unfortunately they all have the same product id.
776 *
777 * Basic configuration is done over a region of 32 I/O ports. The base
778 * ioport is called INTA or INTC, depending on docs/other drivers.
779 *
780 * The region of the 32 I/O ports is configured in POSIO0R...
781 */
782
783/* registers */
784#define ITE_887x_MISCR 0x9c
785#define ITE_887x_INTCBAR 0x78
786#define ITE_887x_UARTBAR 0x7c
787#define ITE_887x_PS0BAR 0x10
788#define ITE_887x_POSIO0 0x60
789
790/* I/O space size */
791#define ITE_887x_IOSIZE 32
792/* I/O space size (bits 26-24; 8 bytes = 011b) */
793#define ITE_887x_POSIO_IOSIZE_8 (3 << 24)
794/* I/O space size (bits 26-24; 32 bytes = 101b) */
795#define ITE_887x_POSIO_IOSIZE_32 (5 << 24)
796/* Decoding speed (1 = slow, 2 = medium, 3 = fast) */
797#define ITE_887x_POSIO_SPEED (3 << 29)
798/* enable IO_Space bit */
799#define ITE_887x_POSIO_ENABLE (1 << 31)
800
801static int pci_ite887x_init(struct pci_dev *dev)
802{
803 /* inta_addr are the configuration addresses of the ITE */
804 static const short inta_addr[] = { 0x2a0, 0x2c0, 0x220, 0x240, 0x1e0,
805 0x200, 0x280, 0 };
806 int ret, i, type;
807 struct resource *iobase = NULL;
808 u32 miscr, uartbar, ioport;
809
810 /* search for the base-ioport */
811 i = 0;
812 while (inta_addr[i] && iobase == NULL) {
813 iobase = request_region(inta_addr[i], ITE_887x_IOSIZE,
814 "ite887x");
815 if (iobase != NULL) {
816 /* write POSIO0R - speed | size | ioport */
817 pci_write_config_dword(dev, ITE_887x_POSIO0,
818 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
819 ITE_887x_POSIO_IOSIZE_32 | inta_addr[i]);
820 /* write INTCBAR - ioport */
821 pci_write_config_dword(dev, ITE_887x_INTCBAR,
822 inta_addr[i]);
823 ret = inb(inta_addr[i]);
824 if (ret != 0xff) {
825 /* ioport connected */
826 break;
827 }
828 release_region(iobase->start, ITE_887x_IOSIZE);
829 iobase = NULL;
830 }
831 i++;
832 }
833
834 if (!inta_addr[i]) {
835 printk(KERN_ERR "ite887x: could not find iobase\n");
836 return -ENODEV;
837 }
838
839 /* start of undocumented type checking (see parport_pc.c) */
840 type = inb(iobase->start + 0x18) & 0x0f;
841
842 switch (type) {
843 case 0x2: /* ITE8871 (1P) */
844 case 0xa: /* ITE8875 (1P) */
845 ret = 0;
846 break;
847 case 0xe: /* ITE8872 (2S1P) */
848 ret = 2;
849 break;
850 case 0x6: /* ITE8873 (1S) */
851 ret = 1;
852 break;
853 case 0x8: /* ITE8874 (2S) */
854 ret = 2;
855 break;
856 default:
857 moan_device("Unknown ITE887x", dev);
858 ret = -ENODEV;
859 }
860
861 /* configure all serial ports */
862 for (i = 0; i < ret; i++) {
863 /* read the I/O port from the device */
864 pci_read_config_dword(dev, ITE_887x_PS0BAR + (0x4 * (i + 1)),
865 &ioport);
866 ioport &= 0x0000FF00; /* the actual base address */
867 pci_write_config_dword(dev, ITE_887x_POSIO0 + (0x4 * (i + 1)),
868 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
869 ITE_887x_POSIO_IOSIZE_8 | ioport);
870
871 /* write the ioport to the UARTBAR */
872 pci_read_config_dword(dev, ITE_887x_UARTBAR, &uartbar);
873 uartbar &= ~(0xffff << (16 * i)); /* clear half the reg */
874 uartbar |= (ioport << (16 * i)); /* set the ioport */
875 pci_write_config_dword(dev, ITE_887x_UARTBAR, uartbar);
876
877 /* get current config */
878 pci_read_config_dword(dev, ITE_887x_MISCR, &miscr);
879 /* disable interrupts (UARTx_Routing[3:0]) */
880 miscr &= ~(0xf << (12 - 4 * i));
881 /* activate the UART (UARTx_En) */
882 miscr |= 1 << (23 - i);
883 /* write new config with activated UART */
884 pci_write_config_dword(dev, ITE_887x_MISCR, miscr);
885 }
886
887 if (ret <= 0) {
888 /* the device has no UARTs if we get here */
889 release_region(iobase->start, ITE_887x_IOSIZE);
890 }
891
892 return ret;
893}
894
895static void __devexit pci_ite887x_exit(struct pci_dev *dev)
896{
897 u32 ioport;
898 /* the ioport is bit 0-15 in POSIO0R */
899 pci_read_config_dword(dev, ITE_887x_POSIO0, &ioport);
900 ioport &= 0xffff;
901 release_region(ioport, ITE_887x_IOSIZE);
902}
903
904/*
905 * Oxford Semiconductor Inc.
906 * Check that device is part of the Tornado range of devices, then determine
907 * the number of ports available on the device.
908 */
909static int pci_oxsemi_tornado_init(struct pci_dev *dev)
910{
911 u8 __iomem *p;
912 unsigned long deviceID;
913 unsigned int number_uarts = 0;
914
915 /* OxSemi Tornado devices are all 0xCxxx */
916 if (dev->vendor == PCI_VENDOR_ID_OXSEMI &&
917 (dev->device & 0xF000) != 0xC000)
918 return 0;
919
920 p = pci_iomap(dev, 0, 5);
921 if (p == NULL)
922 return -ENOMEM;
923
924 deviceID = ioread32(p);
925 /* Tornado device */
926 if (deviceID == 0x07000200) {
927 number_uarts = ioread8(p + 4);
928 printk(KERN_DEBUG
929 "%d ports detected on Oxford PCI Express device\n",
930 number_uarts);
931 }
932 pci_iounmap(dev, p);
933 return number_uarts;
934}
935
936static int
937pci_default_setup(struct serial_private *priv,
938 const struct pciserial_board *board,
939 struct uart_port *port, int idx)
940{
941 unsigned int bar, offset = board->first_offset, maxnr;
942
943 bar = FL_GET_BASE(board->flags);
944 if (board->flags & FL_BASE_BARS)
945 bar += idx;
946 else
947 offset += idx * board->uart_offset;
948
949 maxnr = (pci_resource_len(priv->dev, bar) - board->first_offset) >>
950 (board->reg_shift + 3);
951
952 if (board->flags & FL_REGION_SZ_CAP && idx >= maxnr)
953 return 1;
954
955 return setup_port(priv, port, bar, offset, board->reg_shift);
956}
957
958static int
959ce4100_serial_setup(struct serial_private *priv,
960 const struct pciserial_board *board,
961 struct uart_port *port, int idx)
962{
963 int ret;
964
965 ret = setup_port(priv, port, 0, 0, board->reg_shift);
966 port->iotype = UPIO_MEM32;
967 port->type = PORT_XSCALE;
968 port->flags = (port->flags | UPF_FIXED_PORT | UPF_FIXED_TYPE);
969 port->regshift = 2;
970
971 return ret;
972}
973
974static int
975pci_omegapci_setup(struct serial_private *priv,
976 const struct pciserial_board *board,
977 struct uart_port *port, int idx)
978{
979 return setup_port(priv, port, 2, idx * 8, 0);
980}
981
982static int skip_tx_en_setup(struct serial_private *priv,
983 const struct pciserial_board *board,
984 struct uart_port *port, int idx)
985{
986 port->flags |= UPF_NO_TXEN_TEST;
987 printk(KERN_DEBUG "serial8250: skipping TxEn test for device "
988 "[%04x:%04x] subsystem [%04x:%04x]\n",
989 priv->dev->vendor,
990 priv->dev->device,
991 priv->dev->subsystem_vendor,
992 priv->dev->subsystem_device);
993
994 return pci_default_setup(priv, board, port, idx);
995}
996
997static int pci_eg20t_init(struct pci_dev *dev)
998{
999#if defined(CONFIG_SERIAL_PCH_UART) || defined(CONFIG_SERIAL_PCH_UART_MODULE)
1000 return -ENODEV;
1001#else
1002 return 0;
1003#endif
1004}
1005
1006/* This should be in linux/pci_ids.h */
1007#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
1008#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
1009#define PCI_DEVICE_ID_OCTPRO 0x0001
1010#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
1011#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
1012#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
1013#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
1014#define PCI_VENDOR_ID_ADVANTECH 0x13fe
1015#define PCI_DEVICE_ID_INTEL_CE4100_UART 0x2e66
1016#define PCI_DEVICE_ID_ADVANTECH_PCI3620 0x3620
1017#define PCI_DEVICE_ID_TITAN_200I 0x8028
1018#define PCI_DEVICE_ID_TITAN_400I 0x8048
1019#define PCI_DEVICE_ID_TITAN_800I 0x8088
1020#define PCI_DEVICE_ID_TITAN_800EH 0xA007
1021#define PCI_DEVICE_ID_TITAN_800EHB 0xA008
1022#define PCI_DEVICE_ID_TITAN_400EH 0xA009
1023#define PCI_DEVICE_ID_TITAN_100E 0xA010
1024#define PCI_DEVICE_ID_TITAN_200E 0xA012
1025#define PCI_DEVICE_ID_TITAN_400E 0xA013
1026#define PCI_DEVICE_ID_TITAN_800E 0xA014
1027#define PCI_DEVICE_ID_TITAN_200EI 0xA016
1028#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
1029#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
1030#define PCIE_DEVICE_ID_NEO_2_OX_IBM 0x00F6
1031#define PCI_DEVICE_ID_PLX_CRONYX_OMEGA 0xc001
1032
1033/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
1034#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
1035
1036/*
1037 * Master list of serial port init/setup/exit quirks.
1038 * This does not describe the general nature of the port.
1039 * (ie, baud base, number and location of ports, etc)
1040 *
1041 * This list is ordered alphabetically by vendor then device.
1042 * Specific entries must come before more generic entries.
1043 */
1044static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1045 /*
1046 * ADDI-DATA GmbH communication cards <info@addi-data.com>
1047 */
1048 {
1049 .vendor = PCI_VENDOR_ID_ADDIDATA_OLD,
1050 .device = PCI_DEVICE_ID_ADDIDATA_APCI7800,
1051 .subvendor = PCI_ANY_ID,
1052 .subdevice = PCI_ANY_ID,
1053 .setup = addidata_apci7800_setup,
1054 },
1055 /*
1056 * AFAVLAB cards - these may be called via parport_serial
1057 * It is not clear whether this applies to all products.
1058 */
1059 {
1060 .vendor = PCI_VENDOR_ID_AFAVLAB,
1061 .device = PCI_ANY_ID,
1062 .subvendor = PCI_ANY_ID,
1063 .subdevice = PCI_ANY_ID,
1064 .setup = afavlab_setup,
1065 },
1066 /*
1067 * HP Diva
1068 */
1069 {
1070 .vendor = PCI_VENDOR_ID_HP,
1071 .device = PCI_DEVICE_ID_HP_DIVA,
1072 .subvendor = PCI_ANY_ID,
1073 .subdevice = PCI_ANY_ID,
1074 .init = pci_hp_diva_init,
1075 .setup = pci_hp_diva_setup,
1076 },
1077 /*
1078 * Intel
1079 */
1080 {
1081 .vendor = PCI_VENDOR_ID_INTEL,
1082 .device = PCI_DEVICE_ID_INTEL_80960_RP,
1083 .subvendor = 0xe4bf,
1084 .subdevice = PCI_ANY_ID,
1085 .init = pci_inteli960ni_init,
1086 .setup = pci_default_setup,
1087 },
1088 {
1089 .vendor = PCI_VENDOR_ID_INTEL,
1090 .device = PCI_DEVICE_ID_INTEL_8257X_SOL,
1091 .subvendor = PCI_ANY_ID,
1092 .subdevice = PCI_ANY_ID,
1093 .setup = skip_tx_en_setup,
1094 },
1095 {
1096 .vendor = PCI_VENDOR_ID_INTEL,
1097 .device = PCI_DEVICE_ID_INTEL_82573L_SOL,
1098 .subvendor = PCI_ANY_ID,
1099 .subdevice = PCI_ANY_ID,
1100 .setup = skip_tx_en_setup,
1101 },
1102 {
1103 .vendor = PCI_VENDOR_ID_INTEL,
1104 .device = PCI_DEVICE_ID_INTEL_82573E_SOL,
1105 .subvendor = PCI_ANY_ID,
1106 .subdevice = PCI_ANY_ID,
1107 .setup = skip_tx_en_setup,
1108 },
1109 {
1110 .vendor = PCI_VENDOR_ID_INTEL,
1111 .device = PCI_DEVICE_ID_INTEL_CE4100_UART,
1112 .subvendor = PCI_ANY_ID,
1113 .subdevice = PCI_ANY_ID,
1114 .setup = ce4100_serial_setup,
1115 },
1116 /*
1117 * ITE
1118 */
1119 {
1120 .vendor = PCI_VENDOR_ID_ITE,
1121 .device = PCI_DEVICE_ID_ITE_8872,
1122 .subvendor = PCI_ANY_ID,
1123 .subdevice = PCI_ANY_ID,
1124 .init = pci_ite887x_init,
1125 .setup = pci_default_setup,
1126 .exit = __devexit_p(pci_ite887x_exit),
1127 },
1128 /*
1129 * National Instruments
1130 */
1131 {
1132 .vendor = PCI_VENDOR_ID_NI,
1133 .device = PCI_DEVICE_ID_NI_PCI23216,
1134 .subvendor = PCI_ANY_ID,
1135 .subdevice = PCI_ANY_ID,
1136 .init = pci_ni8420_init,
1137 .setup = pci_default_setup,
1138 .exit = __devexit_p(pci_ni8420_exit),
1139 },
1140 {
1141 .vendor = PCI_VENDOR_ID_NI,
1142 .device = PCI_DEVICE_ID_NI_PCI2328,
1143 .subvendor = PCI_ANY_ID,
1144 .subdevice = PCI_ANY_ID,
1145 .init = pci_ni8420_init,
1146 .setup = pci_default_setup,
1147 .exit = __devexit_p(pci_ni8420_exit),
1148 },
1149 {
1150 .vendor = PCI_VENDOR_ID_NI,
1151 .device = PCI_DEVICE_ID_NI_PCI2324,
1152 .subvendor = PCI_ANY_ID,
1153 .subdevice = PCI_ANY_ID,
1154 .init = pci_ni8420_init,
1155 .setup = pci_default_setup,
1156 .exit = __devexit_p(pci_ni8420_exit),
1157 },
1158 {
1159 .vendor = PCI_VENDOR_ID_NI,
1160 .device = PCI_DEVICE_ID_NI_PCI2322,
1161 .subvendor = PCI_ANY_ID,
1162 .subdevice = PCI_ANY_ID,
1163 .init = pci_ni8420_init,
1164 .setup = pci_default_setup,
1165 .exit = __devexit_p(pci_ni8420_exit),
1166 },
1167 {
1168 .vendor = PCI_VENDOR_ID_NI,
1169 .device = PCI_DEVICE_ID_NI_PCI2324I,
1170 .subvendor = PCI_ANY_ID,
1171 .subdevice = PCI_ANY_ID,
1172 .init = pci_ni8420_init,
1173 .setup = pci_default_setup,
1174 .exit = __devexit_p(pci_ni8420_exit),
1175 },
1176 {
1177 .vendor = PCI_VENDOR_ID_NI,
1178 .device = PCI_DEVICE_ID_NI_PCI2322I,
1179 .subvendor = PCI_ANY_ID,
1180 .subdevice = PCI_ANY_ID,
1181 .init = pci_ni8420_init,
1182 .setup = pci_default_setup,
1183 .exit = __devexit_p(pci_ni8420_exit),
1184 },
1185 {
1186 .vendor = PCI_VENDOR_ID_NI,
1187 .device = PCI_DEVICE_ID_NI_PXI8420_23216,
1188 .subvendor = PCI_ANY_ID,
1189 .subdevice = PCI_ANY_ID,
1190 .init = pci_ni8420_init,
1191 .setup = pci_default_setup,
1192 .exit = __devexit_p(pci_ni8420_exit),
1193 },
1194 {
1195 .vendor = PCI_VENDOR_ID_NI,
1196 .device = PCI_DEVICE_ID_NI_PXI8420_2328,
1197 .subvendor = PCI_ANY_ID,
1198 .subdevice = PCI_ANY_ID,
1199 .init = pci_ni8420_init,
1200 .setup = pci_default_setup,
1201 .exit = __devexit_p(pci_ni8420_exit),
1202 },
1203 {
1204 .vendor = PCI_VENDOR_ID_NI,
1205 .device = PCI_DEVICE_ID_NI_PXI8420_2324,
1206 .subvendor = PCI_ANY_ID,
1207 .subdevice = PCI_ANY_ID,
1208 .init = pci_ni8420_init,
1209 .setup = pci_default_setup,
1210 .exit = __devexit_p(pci_ni8420_exit),
1211 },
1212 {
1213 .vendor = PCI_VENDOR_ID_NI,
1214 .device = PCI_DEVICE_ID_NI_PXI8420_2322,
1215 .subvendor = PCI_ANY_ID,
1216 .subdevice = PCI_ANY_ID,
1217 .init = pci_ni8420_init,
1218 .setup = pci_default_setup,
1219 .exit = __devexit_p(pci_ni8420_exit),
1220 },
1221 {
1222 .vendor = PCI_VENDOR_ID_NI,
1223 .device = PCI_DEVICE_ID_NI_PXI8422_2324,
1224 .subvendor = PCI_ANY_ID,
1225 .subdevice = PCI_ANY_ID,
1226 .init = pci_ni8420_init,
1227 .setup = pci_default_setup,
1228 .exit = __devexit_p(pci_ni8420_exit),
1229 },
1230 {
1231 .vendor = PCI_VENDOR_ID_NI,
1232 .device = PCI_DEVICE_ID_NI_PXI8422_2322,
1233 .subvendor = PCI_ANY_ID,
1234 .subdevice = PCI_ANY_ID,
1235 .init = pci_ni8420_init,
1236 .setup = pci_default_setup,
1237 .exit = __devexit_p(pci_ni8420_exit),
1238 },
1239 {
1240 .vendor = PCI_VENDOR_ID_NI,
1241 .device = PCI_ANY_ID,
1242 .subvendor = PCI_ANY_ID,
1243 .subdevice = PCI_ANY_ID,
1244 .init = pci_ni8430_init,
1245 .setup = pci_ni8430_setup,
1246 .exit = __devexit_p(pci_ni8430_exit),
1247 },
1248 /*
1249 * Panacom
1250 */
1251 {
1252 .vendor = PCI_VENDOR_ID_PANACOM,
1253 .device = PCI_DEVICE_ID_PANACOM_QUADMODEM,
1254 .subvendor = PCI_ANY_ID,
1255 .subdevice = PCI_ANY_ID,
1256 .init = pci_plx9050_init,
1257 .setup = pci_default_setup,
1258 .exit = __devexit_p(pci_plx9050_exit),
1259 },
1260 {
1261 .vendor = PCI_VENDOR_ID_PANACOM,
1262 .device = PCI_DEVICE_ID_PANACOM_DUALMODEM,
1263 .subvendor = PCI_ANY_ID,
1264 .subdevice = PCI_ANY_ID,
1265 .init = pci_plx9050_init,
1266 .setup = pci_default_setup,
1267 .exit = __devexit_p(pci_plx9050_exit),
1268 },
1269 /*
1270 * PLX
1271 */
1272 {
1273 .vendor = PCI_VENDOR_ID_PLX,
1274 .device = PCI_DEVICE_ID_PLX_9030,
1275 .subvendor = PCI_SUBVENDOR_ID_PERLE,
1276 .subdevice = PCI_ANY_ID,
1277 .setup = pci_default_setup,
1278 },
1279 {
1280 .vendor = PCI_VENDOR_ID_PLX,
1281 .device = PCI_DEVICE_ID_PLX_9050,
1282 .subvendor = PCI_SUBVENDOR_ID_EXSYS,
1283 .subdevice = PCI_SUBDEVICE_ID_EXSYS_4055,
1284 .init = pci_plx9050_init,
1285 .setup = pci_default_setup,
1286 .exit = __devexit_p(pci_plx9050_exit),
1287 },
1288 {
1289 .vendor = PCI_VENDOR_ID_PLX,
1290 .device = PCI_DEVICE_ID_PLX_9050,
1291 .subvendor = PCI_SUBVENDOR_ID_KEYSPAN,
1292 .subdevice = PCI_SUBDEVICE_ID_KEYSPAN_SX2,
1293 .init = pci_plx9050_init,
1294 .setup = pci_default_setup,
1295 .exit = __devexit_p(pci_plx9050_exit),
1296 },
1297 {
1298 .vendor = PCI_VENDOR_ID_PLX,
1299 .device = PCI_DEVICE_ID_PLX_9050,
1300 .subvendor = PCI_VENDOR_ID_PLX,
1301 .subdevice = PCI_SUBDEVICE_ID_UNKNOWN_0x1584,
1302 .init = pci_plx9050_init,
1303 .setup = pci_default_setup,
1304 .exit = __devexit_p(pci_plx9050_exit),
1305 },
1306 {
1307 .vendor = PCI_VENDOR_ID_PLX,
1308 .device = PCI_DEVICE_ID_PLX_ROMULUS,
1309 .subvendor = PCI_VENDOR_ID_PLX,
1310 .subdevice = PCI_DEVICE_ID_PLX_ROMULUS,
1311 .init = pci_plx9050_init,
1312 .setup = pci_default_setup,
1313 .exit = __devexit_p(pci_plx9050_exit),
1314 },
1315 /*
1316 * SBS Technologies, Inc., PMC-OCTALPRO 232
1317 */
1318 {
1319 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1320 .device = PCI_DEVICE_ID_OCTPRO,
1321 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1322 .subdevice = PCI_SUBDEVICE_ID_OCTPRO232,
1323 .init = sbs_init,
1324 .setup = sbs_setup,
1325 .exit = __devexit_p(sbs_exit),
1326 },
1327 /*
1328 * SBS Technologies, Inc., PMC-OCTALPRO 422
1329 */
1330 {
1331 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1332 .device = PCI_DEVICE_ID_OCTPRO,
1333 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1334 .subdevice = PCI_SUBDEVICE_ID_OCTPRO422,
1335 .init = sbs_init,
1336 .setup = sbs_setup,
1337 .exit = __devexit_p(sbs_exit),
1338 },
1339 /*
1340 * SBS Technologies, Inc., P-Octal 232
1341 */
1342 {
1343 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1344 .device = PCI_DEVICE_ID_OCTPRO,
1345 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1346 .subdevice = PCI_SUBDEVICE_ID_POCTAL232,
1347 .init = sbs_init,
1348 .setup = sbs_setup,
1349 .exit = __devexit_p(sbs_exit),
1350 },
1351 /*
1352 * SBS Technologies, Inc., P-Octal 422
1353 */
1354 {
1355 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1356 .device = PCI_DEVICE_ID_OCTPRO,
1357 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1358 .subdevice = PCI_SUBDEVICE_ID_POCTAL422,
1359 .init = sbs_init,
1360 .setup = sbs_setup,
1361 .exit = __devexit_p(sbs_exit),
1362 },
1363 /*
1364 * SIIG cards - these may be called via parport_serial
1365 */
1366 {
1367 .vendor = PCI_VENDOR_ID_SIIG,
1368 .device = PCI_ANY_ID,
1369 .subvendor = PCI_ANY_ID,
1370 .subdevice = PCI_ANY_ID,
1371 .init = pci_siig_init,
1372 .setup = pci_siig_setup,
1373 },
1374 /*
1375 * Titan cards
1376 */
1377 {
1378 .vendor = PCI_VENDOR_ID_TITAN,
1379 .device = PCI_DEVICE_ID_TITAN_400L,
1380 .subvendor = PCI_ANY_ID,
1381 .subdevice = PCI_ANY_ID,
1382 .setup = titan_400l_800l_setup,
1383 },
1384 {
1385 .vendor = PCI_VENDOR_ID_TITAN,
1386 .device = PCI_DEVICE_ID_TITAN_800L,
1387 .subvendor = PCI_ANY_ID,
1388 .subdevice = PCI_ANY_ID,
1389 .setup = titan_400l_800l_setup,
1390 },
1391 /*
1392 * Timedia cards
1393 */
1394 {
1395 .vendor = PCI_VENDOR_ID_TIMEDIA,
1396 .device = PCI_DEVICE_ID_TIMEDIA_1889,
1397 .subvendor = PCI_VENDOR_ID_TIMEDIA,
1398 .subdevice = PCI_ANY_ID,
1399 .init = pci_timedia_init,
1400 .setup = pci_timedia_setup,
1401 },
1402 {
1403 .vendor = PCI_VENDOR_ID_TIMEDIA,
1404 .device = PCI_ANY_ID,
1405 .subvendor = PCI_ANY_ID,
1406 .subdevice = PCI_ANY_ID,
1407 .setup = pci_timedia_setup,
1408 },
1409 /*
1410 * Xircom cards
1411 */
1412 {
1413 .vendor = PCI_VENDOR_ID_XIRCOM,
1414 .device = PCI_DEVICE_ID_XIRCOM_X3201_MDM,
1415 .subvendor = PCI_ANY_ID,
1416 .subdevice = PCI_ANY_ID,
1417 .init = pci_xircom_init,
1418 .setup = pci_default_setup,
1419 },
1420 /*
1421 * Netmos cards - these may be called via parport_serial
1422 */
1423 {
1424 .vendor = PCI_VENDOR_ID_NETMOS,
1425 .device = PCI_ANY_ID,
1426 .subvendor = PCI_ANY_ID,
1427 .subdevice = PCI_ANY_ID,
1428 .init = pci_netmos_init,
1429 .setup = pci_default_setup,
1430 },
1431 /*
1432 * For Oxford Semiconductor Tornado based devices
1433 */
1434 {
1435 .vendor = PCI_VENDOR_ID_OXSEMI,
1436 .device = PCI_ANY_ID,
1437 .subvendor = PCI_ANY_ID,
1438 .subdevice = PCI_ANY_ID,
1439 .init = pci_oxsemi_tornado_init,
1440 .setup = pci_default_setup,
1441 },
1442 {
1443 .vendor = PCI_VENDOR_ID_MAINPINE,
1444 .device = PCI_ANY_ID,
1445 .subvendor = PCI_ANY_ID,
1446 .subdevice = PCI_ANY_ID,
1447 .init = pci_oxsemi_tornado_init,
1448 .setup = pci_default_setup,
1449 },
1450 {
1451 .vendor = PCI_VENDOR_ID_DIGI,
1452 .device = PCIE_DEVICE_ID_NEO_2_OX_IBM,
1453 .subvendor = PCI_SUBVENDOR_ID_IBM,
1454 .subdevice = PCI_ANY_ID,
1455 .init = pci_oxsemi_tornado_init,
1456 .setup = pci_default_setup,
1457 },
1458 {
1459 .vendor = PCI_VENDOR_ID_INTEL,
1460 .device = 0x8811,
1461 .init = pci_eg20t_init,
1462 },
1463 {
1464 .vendor = PCI_VENDOR_ID_INTEL,
1465 .device = 0x8812,
1466 .init = pci_eg20t_init,
1467 },
1468 {
1469 .vendor = PCI_VENDOR_ID_INTEL,
1470 .device = 0x8813,
1471 .init = pci_eg20t_init,
1472 },
1473 {
1474 .vendor = PCI_VENDOR_ID_INTEL,
1475 .device = 0x8814,
1476 .init = pci_eg20t_init,
1477 },
1478 {
1479 .vendor = 0x10DB,
1480 .device = 0x8027,
1481 .init = pci_eg20t_init,
1482 },
1483 {
1484 .vendor = 0x10DB,
1485 .device = 0x8028,
1486 .init = pci_eg20t_init,
1487 },
1488 {
1489 .vendor = 0x10DB,
1490 .device = 0x8029,
1491 .init = pci_eg20t_init,
1492 },
1493 {
1494 .vendor = 0x10DB,
1495 .device = 0x800C,
1496 .init = pci_eg20t_init,
1497 },
1498 {
1499 .vendor = 0x10DB,
1500 .device = 0x800D,
1501 .init = pci_eg20t_init,
1502 },
1503 {
1504 .vendor = 0x10DB,
1505 .device = 0x800D,
1506 .init = pci_eg20t_init,
1507 },
1508 /*
1509 * Cronyx Omega PCI (PLX-chip based)
1510 */
1511 {
1512 .vendor = PCI_VENDOR_ID_PLX,
1513 .device = PCI_DEVICE_ID_PLX_CRONYX_OMEGA,
1514 .subvendor = PCI_ANY_ID,
1515 .subdevice = PCI_ANY_ID,
1516 .setup = pci_omegapci_setup,
1517 },
1518 /*
1519 * Default "match everything" terminator entry
1520 */
1521 {
1522 .vendor = PCI_ANY_ID,
1523 .device = PCI_ANY_ID,
1524 .subvendor = PCI_ANY_ID,
1525 .subdevice = PCI_ANY_ID,
1526 .setup = pci_default_setup,
1527 }
1528};
1529
1530static inline int quirk_id_matches(u32 quirk_id, u32 dev_id)
1531{
1532 return quirk_id == PCI_ANY_ID || quirk_id == dev_id;
1533}
1534
1535static struct pci_serial_quirk *find_quirk(struct pci_dev *dev)
1536{
1537 struct pci_serial_quirk *quirk;
1538
1539 for (quirk = pci_serial_quirks; ; quirk++)
1540 if (quirk_id_matches(quirk->vendor, dev->vendor) &&
1541 quirk_id_matches(quirk->device, dev->device) &&
1542 quirk_id_matches(quirk->subvendor, dev->subsystem_vendor) &&
1543 quirk_id_matches(quirk->subdevice, dev->subsystem_device))
1544 break;
1545 return quirk;
1546}
1547
1548static inline int get_pci_irq(struct pci_dev *dev,
1549 const struct pciserial_board *board)
1550{
1551 if (board->flags & FL_NOIRQ)
1552 return 0;
1553 else
1554 return dev->irq;
1555}
1556
1557/*
1558 * This is the configuration table for all of the PCI serial boards
1559 * which we support. It is directly indexed by the pci_board_num_t enum
1560 * value, which is encoded in the pci_device_id PCI probe table's
1561 * driver_data member.
1562 *
1563 * The makeup of these names are:
1564 * pbn_bn{_bt}_n_baud{_offsetinhex}
1565 *
1566 * bn = PCI BAR number
1567 * bt = Index using PCI BARs
1568 * n = number of serial ports
1569 * baud = baud rate
1570 * offsetinhex = offset for each sequential port (in hex)
1571 *
1572 * This table is sorted by (in order): bn, bt, baud, offsetindex, n.
1573 *
1574 * Please note: in theory if n = 1, _bt infix should make no difference.
1575 * ie, pbn_b0_1_115200 is the same as pbn_b0_bt_1_115200
1576 */
1577enum pci_board_num_t {
1578 pbn_default = 0,
1579
1580 pbn_b0_1_115200,
1581 pbn_b0_2_115200,
1582 pbn_b0_4_115200,
1583 pbn_b0_5_115200,
1584 pbn_b0_8_115200,
1585
1586 pbn_b0_1_921600,
1587 pbn_b0_2_921600,
1588 pbn_b0_4_921600,
1589
1590 pbn_b0_2_1130000,
1591
1592 pbn_b0_4_1152000,
1593
1594 pbn_b0_2_1843200,
1595 pbn_b0_4_1843200,
1596
1597 pbn_b0_2_1843200_200,
1598 pbn_b0_4_1843200_200,
1599 pbn_b0_8_1843200_200,
1600
1601 pbn_b0_1_4000000,
1602
1603 pbn_b0_bt_1_115200,
1604 pbn_b0_bt_2_115200,
1605 pbn_b0_bt_4_115200,
1606 pbn_b0_bt_8_115200,
1607
1608 pbn_b0_bt_1_460800,
1609 pbn_b0_bt_2_460800,
1610 pbn_b0_bt_4_460800,
1611
1612 pbn_b0_bt_1_921600,
1613 pbn_b0_bt_2_921600,
1614 pbn_b0_bt_4_921600,
1615 pbn_b0_bt_8_921600,
1616
1617 pbn_b1_1_115200,
1618 pbn_b1_2_115200,
1619 pbn_b1_4_115200,
1620 pbn_b1_8_115200,
1621 pbn_b1_16_115200,
1622
1623 pbn_b1_1_921600,
1624 pbn_b1_2_921600,
1625 pbn_b1_4_921600,
1626 pbn_b1_8_921600,
1627
1628 pbn_b1_2_1250000,
1629
1630 pbn_b1_bt_1_115200,
1631 pbn_b1_bt_2_115200,
1632 pbn_b1_bt_4_115200,
1633
1634 pbn_b1_bt_2_921600,
1635
1636 pbn_b1_1_1382400,
1637 pbn_b1_2_1382400,
1638 pbn_b1_4_1382400,
1639 pbn_b1_8_1382400,
1640
1641 pbn_b2_1_115200,
1642 pbn_b2_2_115200,
1643 pbn_b2_4_115200,
1644 pbn_b2_8_115200,
1645
1646 pbn_b2_1_460800,
1647 pbn_b2_4_460800,
1648 pbn_b2_8_460800,
1649 pbn_b2_16_460800,
1650
1651 pbn_b2_1_921600,
1652 pbn_b2_4_921600,
1653 pbn_b2_8_921600,
1654
1655 pbn_b2_8_1152000,
1656
1657 pbn_b2_bt_1_115200,
1658 pbn_b2_bt_2_115200,
1659 pbn_b2_bt_4_115200,
1660
1661 pbn_b2_bt_2_921600,
1662 pbn_b2_bt_4_921600,
1663
1664 pbn_b3_2_115200,
1665 pbn_b3_4_115200,
1666 pbn_b3_8_115200,
1667
1668 pbn_b4_bt_2_921600,
1669 pbn_b4_bt_4_921600,
1670 pbn_b4_bt_8_921600,
1671
1672 /*
1673 * Board-specific versions.
1674 */
1675 pbn_panacom,
1676 pbn_panacom2,
1677 pbn_panacom4,
1678 pbn_exsys_4055,
1679 pbn_plx_romulus,
1680 pbn_oxsemi,
1681 pbn_oxsemi_1_4000000,
1682 pbn_oxsemi_2_4000000,
1683 pbn_oxsemi_4_4000000,
1684 pbn_oxsemi_8_4000000,
1685 pbn_intel_i960,
1686 pbn_sgi_ioc3,
1687 pbn_computone_4,
1688 pbn_computone_6,
1689 pbn_computone_8,
1690 pbn_sbsxrsio,
1691 pbn_exar_XR17C152,
1692 pbn_exar_XR17C154,
1693 pbn_exar_XR17C158,
1694 pbn_exar_ibm_saturn,
1695 pbn_pasemi_1682M,
1696 pbn_ni8430_2,
1697 pbn_ni8430_4,
1698 pbn_ni8430_8,
1699 pbn_ni8430_16,
1700 pbn_ADDIDATA_PCIe_1_3906250,
1701 pbn_ADDIDATA_PCIe_2_3906250,
1702 pbn_ADDIDATA_PCIe_4_3906250,
1703 pbn_ADDIDATA_PCIe_8_3906250,
1704 pbn_ce4100_1_115200,
1705 pbn_omegapci,
1706};
1707
1708/*
1709 * uart_offset - the space between channels
1710 * reg_shift - describes how the UART registers are mapped
1711 * to PCI memory by the card.
1712 * For example IER register on SBS, Inc. PMC-OctPro is located at
1713 * offset 0x10 from the UART base, while UART_IER is defined as 1
1714 * in include/linux/serial_reg.h,
1715 * see first lines of serial_in() and serial_out() in 8250.c
1716*/
1717
1718static struct pciserial_board pci_boards[] __devinitdata = {
1719 [pbn_default] = {
1720 .flags = FL_BASE0,
1721 .num_ports = 1,
1722 .base_baud = 115200,
1723 .uart_offset = 8,
1724 },
1725 [pbn_b0_1_115200] = {
1726 .flags = FL_BASE0,
1727 .num_ports = 1,
1728 .base_baud = 115200,
1729 .uart_offset = 8,
1730 },
1731 [pbn_b0_2_115200] = {
1732 .flags = FL_BASE0,
1733 .num_ports = 2,
1734 .base_baud = 115200,
1735 .uart_offset = 8,
1736 },
1737 [pbn_b0_4_115200] = {
1738 .flags = FL_BASE0,
1739 .num_ports = 4,
1740 .base_baud = 115200,
1741 .uart_offset = 8,
1742 },
1743 [pbn_b0_5_115200] = {
1744 .flags = FL_BASE0,
1745 .num_ports = 5,
1746 .base_baud = 115200,
1747 .uart_offset = 8,
1748 },
1749 [pbn_b0_8_115200] = {
1750 .flags = FL_BASE0,
1751 .num_ports = 8,
1752 .base_baud = 115200,
1753 .uart_offset = 8,
1754 },
1755 [pbn_b0_1_921600] = {
1756 .flags = FL_BASE0,
1757 .num_ports = 1,
1758 .base_baud = 921600,
1759 .uart_offset = 8,
1760 },
1761 [pbn_b0_2_921600] = {
1762 .flags = FL_BASE0,
1763 .num_ports = 2,
1764 .base_baud = 921600,
1765 .uart_offset = 8,
1766 },
1767 [pbn_b0_4_921600] = {
1768 .flags = FL_BASE0,
1769 .num_ports = 4,
1770 .base_baud = 921600,
1771 .uart_offset = 8,
1772 },
1773
1774 [pbn_b0_2_1130000] = {
1775 .flags = FL_BASE0,
1776 .num_ports = 2,
1777 .base_baud = 1130000,
1778 .uart_offset = 8,
1779 },
1780
1781 [pbn_b0_4_1152000] = {
1782 .flags = FL_BASE0,
1783 .num_ports = 4,
1784 .base_baud = 1152000,
1785 .uart_offset = 8,
1786 },
1787
1788 [pbn_b0_2_1843200] = {
1789 .flags = FL_BASE0,
1790 .num_ports = 2,
1791 .base_baud = 1843200,
1792 .uart_offset = 8,
1793 },
1794 [pbn_b0_4_1843200] = {
1795 .flags = FL_BASE0,
1796 .num_ports = 4,
1797 .base_baud = 1843200,
1798 .uart_offset = 8,
1799 },
1800
1801 [pbn_b0_2_1843200_200] = {
1802 .flags = FL_BASE0,
1803 .num_ports = 2,
1804 .base_baud = 1843200,
1805 .uart_offset = 0x200,
1806 },
1807 [pbn_b0_4_1843200_200] = {
1808 .flags = FL_BASE0,
1809 .num_ports = 4,
1810 .base_baud = 1843200,
1811 .uart_offset = 0x200,
1812 },
1813 [pbn_b0_8_1843200_200] = {
1814 .flags = FL_BASE0,
1815 .num_ports = 8,
1816 .base_baud = 1843200,
1817 .uart_offset = 0x200,
1818 },
1819 [pbn_b0_1_4000000] = {
1820 .flags = FL_BASE0,
1821 .num_ports = 1,
1822 .base_baud = 4000000,
1823 .uart_offset = 8,
1824 },
1825
1826 [pbn_b0_bt_1_115200] = {
1827 .flags = FL_BASE0|FL_BASE_BARS,
1828 .num_ports = 1,
1829 .base_baud = 115200,
1830 .uart_offset = 8,
1831 },
1832 [pbn_b0_bt_2_115200] = {
1833 .flags = FL_BASE0|FL_BASE_BARS,
1834 .num_ports = 2,
1835 .base_baud = 115200,
1836 .uart_offset = 8,
1837 },
1838 [pbn_b0_bt_4_115200] = {
1839 .flags = FL_BASE0|FL_BASE_BARS,
1840 .num_ports = 4,
1841 .base_baud = 115200,
1842 .uart_offset = 8,
1843 },
1844 [pbn_b0_bt_8_115200] = {
1845 .flags = FL_BASE0|FL_BASE_BARS,
1846 .num_ports = 8,
1847 .base_baud = 115200,
1848 .uart_offset = 8,
1849 },
1850
1851 [pbn_b0_bt_1_460800] = {
1852 .flags = FL_BASE0|FL_BASE_BARS,
1853 .num_ports = 1,
1854 .base_baud = 460800,
1855 .uart_offset = 8,
1856 },
1857 [pbn_b0_bt_2_460800] = {
1858 .flags = FL_BASE0|FL_BASE_BARS,
1859 .num_ports = 2,
1860 .base_baud = 460800,
1861 .uart_offset = 8,
1862 },
1863 [pbn_b0_bt_4_460800] = {
1864 .flags = FL_BASE0|FL_BASE_BARS,
1865 .num_ports = 4,
1866 .base_baud = 460800,
1867 .uart_offset = 8,
1868 },
1869
1870 [pbn_b0_bt_1_921600] = {
1871 .flags = FL_BASE0|FL_BASE_BARS,
1872 .num_ports = 1,
1873 .base_baud = 921600,
1874 .uart_offset = 8,
1875 },
1876 [pbn_b0_bt_2_921600] = {
1877 .flags = FL_BASE0|FL_BASE_BARS,
1878 .num_ports = 2,
1879 .base_baud = 921600,
1880 .uart_offset = 8,
1881 },
1882 [pbn_b0_bt_4_921600] = {
1883 .flags = FL_BASE0|FL_BASE_BARS,
1884 .num_ports = 4,
1885 .base_baud = 921600,
1886 .uart_offset = 8,
1887 },
1888 [pbn_b0_bt_8_921600] = {
1889 .flags = FL_BASE0|FL_BASE_BARS,
1890 .num_ports = 8,
1891 .base_baud = 921600,
1892 .uart_offset = 8,
1893 },
1894
1895 [pbn_b1_1_115200] = {
1896 .flags = FL_BASE1,
1897 .num_ports = 1,
1898 .base_baud = 115200,
1899 .uart_offset = 8,
1900 },
1901 [pbn_b1_2_115200] = {
1902 .flags = FL_BASE1,
1903 .num_ports = 2,
1904 .base_baud = 115200,
1905 .uart_offset = 8,
1906 },
1907 [pbn_b1_4_115200] = {
1908 .flags = FL_BASE1,
1909 .num_ports = 4,
1910 .base_baud = 115200,
1911 .uart_offset = 8,
1912 },
1913 [pbn_b1_8_115200] = {
1914 .flags = FL_BASE1,
1915 .num_ports = 8,
1916 .base_baud = 115200,
1917 .uart_offset = 8,
1918 },
1919 [pbn_b1_16_115200] = {
1920 .flags = FL_BASE1,
1921 .num_ports = 16,
1922 .base_baud = 115200,
1923 .uart_offset = 8,
1924 },
1925
1926 [pbn_b1_1_921600] = {
1927 .flags = FL_BASE1,
1928 .num_ports = 1,
1929 .base_baud = 921600,
1930 .uart_offset = 8,
1931 },
1932 [pbn_b1_2_921600] = {
1933 .flags = FL_BASE1,
1934 .num_ports = 2,
1935 .base_baud = 921600,
1936 .uart_offset = 8,
1937 },
1938 [pbn_b1_4_921600] = {
1939 .flags = FL_BASE1,
1940 .num_ports = 4,
1941 .base_baud = 921600,
1942 .uart_offset = 8,
1943 },
1944 [pbn_b1_8_921600] = {
1945 .flags = FL_BASE1,
1946 .num_ports = 8,
1947 .base_baud = 921600,
1948 .uart_offset = 8,
1949 },
1950 [pbn_b1_2_1250000] = {
1951 .flags = FL_BASE1,
1952 .num_ports = 2,
1953 .base_baud = 1250000,
1954 .uart_offset = 8,
1955 },
1956
1957 [pbn_b1_bt_1_115200] = {
1958 .flags = FL_BASE1|FL_BASE_BARS,
1959 .num_ports = 1,
1960 .base_baud = 115200,
1961 .uart_offset = 8,
1962 },
1963 [pbn_b1_bt_2_115200] = {
1964 .flags = FL_BASE1|FL_BASE_BARS,
1965 .num_ports = 2,
1966 .base_baud = 115200,
1967 .uart_offset = 8,
1968 },
1969 [pbn_b1_bt_4_115200] = {
1970 .flags = FL_BASE1|FL_BASE_BARS,
1971 .num_ports = 4,
1972 .base_baud = 115200,
1973 .uart_offset = 8,
1974 },
1975
1976 [pbn_b1_bt_2_921600] = {
1977 .flags = FL_BASE1|FL_BASE_BARS,
1978 .num_ports = 2,
1979 .base_baud = 921600,
1980 .uart_offset = 8,
1981 },
1982
1983 [pbn_b1_1_1382400] = {
1984 .flags = FL_BASE1,
1985 .num_ports = 1,
1986 .base_baud = 1382400,
1987 .uart_offset = 8,
1988 },
1989 [pbn_b1_2_1382400] = {
1990 .flags = FL_BASE1,
1991 .num_ports = 2,
1992 .base_baud = 1382400,
1993 .uart_offset = 8,
1994 },
1995 [pbn_b1_4_1382400] = {
1996 .flags = FL_BASE1,
1997 .num_ports = 4,
1998 .base_baud = 1382400,
1999 .uart_offset = 8,
2000 },
2001 [pbn_b1_8_1382400] = {
2002 .flags = FL_BASE1,
2003 .num_ports = 8,
2004 .base_baud = 1382400,
2005 .uart_offset = 8,
2006 },
2007
2008 [pbn_b2_1_115200] = {
2009 .flags = FL_BASE2,
2010 .num_ports = 1,
2011 .base_baud = 115200,
2012 .uart_offset = 8,
2013 },
2014 [pbn_b2_2_115200] = {
2015 .flags = FL_BASE2,
2016 .num_ports = 2,
2017 .base_baud = 115200,
2018 .uart_offset = 8,
2019 },
2020 [pbn_b2_4_115200] = {
2021 .flags = FL_BASE2,
2022 .num_ports = 4,
2023 .base_baud = 115200,
2024 .uart_offset = 8,
2025 },
2026 [pbn_b2_8_115200] = {
2027 .flags = FL_BASE2,
2028 .num_ports = 8,
2029 .base_baud = 115200,
2030 .uart_offset = 8,
2031 },
2032
2033 [pbn_b2_1_460800] = {
2034 .flags = FL_BASE2,
2035 .num_ports = 1,
2036 .base_baud = 460800,
2037 .uart_offset = 8,
2038 },
2039 [pbn_b2_4_460800] = {
2040 .flags = FL_BASE2,
2041 .num_ports = 4,
2042 .base_baud = 460800,
2043 .uart_offset = 8,
2044 },
2045 [pbn_b2_8_460800] = {
2046 .flags = FL_BASE2,
2047 .num_ports = 8,
2048 .base_baud = 460800,
2049 .uart_offset = 8,
2050 },
2051 [pbn_b2_16_460800] = {
2052 .flags = FL_BASE2,
2053 .num_ports = 16,
2054 .base_baud = 460800,
2055 .uart_offset = 8,
2056 },
2057
2058 [pbn_b2_1_921600] = {
2059 .flags = FL_BASE2,
2060 .num_ports = 1,
2061 .base_baud = 921600,
2062 .uart_offset = 8,
2063 },
2064 [pbn_b2_4_921600] = {
2065 .flags = FL_BASE2,
2066 .num_ports = 4,
2067 .base_baud = 921600,
2068 .uart_offset = 8,
2069 },
2070 [pbn_b2_8_921600] = {
2071 .flags = FL_BASE2,
2072 .num_ports = 8,
2073 .base_baud = 921600,
2074 .uart_offset = 8,
2075 },
2076
2077 [pbn_b2_8_1152000] = {
2078 .flags = FL_BASE2,
2079 .num_ports = 8,
2080 .base_baud = 1152000,
2081 .uart_offset = 8,
2082 },
2083
2084 [pbn_b2_bt_1_115200] = {
2085 .flags = FL_BASE2|FL_BASE_BARS,
2086 .num_ports = 1,
2087 .base_baud = 115200,
2088 .uart_offset = 8,
2089 },
2090 [pbn_b2_bt_2_115200] = {
2091 .flags = FL_BASE2|FL_BASE_BARS,
2092 .num_ports = 2,
2093 .base_baud = 115200,
2094 .uart_offset = 8,
2095 },
2096 [pbn_b2_bt_4_115200] = {
2097 .flags = FL_BASE2|FL_BASE_BARS,
2098 .num_ports = 4,
2099 .base_baud = 115200,
2100 .uart_offset = 8,
2101 },
2102
2103 [pbn_b2_bt_2_921600] = {
2104 .flags = FL_BASE2|FL_BASE_BARS,
2105 .num_ports = 2,
2106 .base_baud = 921600,
2107 .uart_offset = 8,
2108 },
2109 [pbn_b2_bt_4_921600] = {
2110 .flags = FL_BASE2|FL_BASE_BARS,
2111 .num_ports = 4,
2112 .base_baud = 921600,
2113 .uart_offset = 8,
2114 },
2115
2116 [pbn_b3_2_115200] = {
2117 .flags = FL_BASE3,
2118 .num_ports = 2,
2119 .base_baud = 115200,
2120 .uart_offset = 8,
2121 },
2122 [pbn_b3_4_115200] = {
2123 .flags = FL_BASE3,
2124 .num_ports = 4,
2125 .base_baud = 115200,
2126 .uart_offset = 8,
2127 },
2128 [pbn_b3_8_115200] = {
2129 .flags = FL_BASE3,
2130 .num_ports = 8,
2131 .base_baud = 115200,
2132 .uart_offset = 8,
2133 },
2134
2135 [pbn_b4_bt_2_921600] = {
2136 .flags = FL_BASE4,
2137 .num_ports = 2,
2138 .base_baud = 921600,
2139 .uart_offset = 8,
2140 },
2141 [pbn_b4_bt_4_921600] = {
2142 .flags = FL_BASE4,
2143 .num_ports = 4,
2144 .base_baud = 921600,
2145 .uart_offset = 8,
2146 },
2147 [pbn_b4_bt_8_921600] = {
2148 .flags = FL_BASE4,
2149 .num_ports = 8,
2150 .base_baud = 921600,
2151 .uart_offset = 8,
2152 },
2153
2154 /*
2155 * Entries following this are board-specific.
2156 */
2157
2158 /*
2159 * Panacom - IOMEM
2160 */
2161 [pbn_panacom] = {
2162 .flags = FL_BASE2,
2163 .num_ports = 2,
2164 .base_baud = 921600,
2165 .uart_offset = 0x400,
2166 .reg_shift = 7,
2167 },
2168 [pbn_panacom2] = {
2169 .flags = FL_BASE2|FL_BASE_BARS,
2170 .num_ports = 2,
2171 .base_baud = 921600,
2172 .uart_offset = 0x400,
2173 .reg_shift = 7,
2174 },
2175 [pbn_panacom4] = {
2176 .flags = FL_BASE2|FL_BASE_BARS,
2177 .num_ports = 4,
2178 .base_baud = 921600,
2179 .uart_offset = 0x400,
2180 .reg_shift = 7,
2181 },
2182
2183 [pbn_exsys_4055] = {
2184 .flags = FL_BASE2,
2185 .num_ports = 4,
2186 .base_baud = 115200,
2187 .uart_offset = 8,
2188 },
2189
2190 /* I think this entry is broken - the first_offset looks wrong --rmk */
2191 [pbn_plx_romulus] = {
2192 .flags = FL_BASE2,
2193 .num_ports = 4,
2194 .base_baud = 921600,
2195 .uart_offset = 8 << 2,
2196 .reg_shift = 2,
2197 .first_offset = 0x03,
2198 },
2199
2200 /*
2201 * This board uses the size of PCI Base region 0 to
2202 * signal now many ports are available
2203 */
2204 [pbn_oxsemi] = {
2205 .flags = FL_BASE0|FL_REGION_SZ_CAP,
2206 .num_ports = 32,
2207 .base_baud = 115200,
2208 .uart_offset = 8,
2209 },
2210 [pbn_oxsemi_1_4000000] = {
2211 .flags = FL_BASE0,
2212 .num_ports = 1,
2213 .base_baud = 4000000,
2214 .uart_offset = 0x200,
2215 .first_offset = 0x1000,
2216 },
2217 [pbn_oxsemi_2_4000000] = {
2218 .flags = FL_BASE0,
2219 .num_ports = 2,
2220 .base_baud = 4000000,
2221 .uart_offset = 0x200,
2222 .first_offset = 0x1000,
2223 },
2224 [pbn_oxsemi_4_4000000] = {
2225 .flags = FL_BASE0,
2226 .num_ports = 4,
2227 .base_baud = 4000000,
2228 .uart_offset = 0x200,
2229 .first_offset = 0x1000,
2230 },
2231 [pbn_oxsemi_8_4000000] = {
2232 .flags = FL_BASE0,
2233 .num_ports = 8,
2234 .base_baud = 4000000,
2235 .uart_offset = 0x200,
2236 .first_offset = 0x1000,
2237 },
2238
2239
2240 /*
2241 * EKF addition for i960 Boards form EKF with serial port.
2242 * Max 256 ports.
2243 */
2244 [pbn_intel_i960] = {
2245 .flags = FL_BASE0,
2246 .num_ports = 32,
2247 .base_baud = 921600,
2248 .uart_offset = 8 << 2,
2249 .reg_shift = 2,
2250 .first_offset = 0x10000,
2251 },
2252 [pbn_sgi_ioc3] = {
2253 .flags = FL_BASE0|FL_NOIRQ,
2254 .num_ports = 1,
2255 .base_baud = 458333,
2256 .uart_offset = 8,
2257 .reg_shift = 0,
2258 .first_offset = 0x20178,
2259 },
2260
2261 /*
2262 * Computone - uses IOMEM.
2263 */
2264 [pbn_computone_4] = {
2265 .flags = FL_BASE0,
2266 .num_ports = 4,
2267 .base_baud = 921600,
2268 .uart_offset = 0x40,
2269 .reg_shift = 2,
2270 .first_offset = 0x200,
2271 },
2272 [pbn_computone_6] = {
2273 .flags = FL_BASE0,
2274 .num_ports = 6,
2275 .base_baud = 921600,
2276 .uart_offset = 0x40,
2277 .reg_shift = 2,
2278 .first_offset = 0x200,
2279 },
2280 [pbn_computone_8] = {
2281 .flags = FL_BASE0,
2282 .num_ports = 8,
2283 .base_baud = 921600,
2284 .uart_offset = 0x40,
2285 .reg_shift = 2,
2286 .first_offset = 0x200,
2287 },
2288 [pbn_sbsxrsio] = {
2289 .flags = FL_BASE0,
2290 .num_ports = 8,
2291 .base_baud = 460800,
2292 .uart_offset = 256,
2293 .reg_shift = 4,
2294 },
2295 /*
2296 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
2297 * Only basic 16550A support.
2298 * XR17C15[24] are not tested, but they should work.
2299 */
2300 [pbn_exar_XR17C152] = {
2301 .flags = FL_BASE0,
2302 .num_ports = 2,
2303 .base_baud = 921600,
2304 .uart_offset = 0x200,
2305 },
2306 [pbn_exar_XR17C154] = {
2307 .flags = FL_BASE0,
2308 .num_ports = 4,
2309 .base_baud = 921600,
2310 .uart_offset = 0x200,
2311 },
2312 [pbn_exar_XR17C158] = {
2313 .flags = FL_BASE0,
2314 .num_ports = 8,
2315 .base_baud = 921600,
2316 .uart_offset = 0x200,
2317 },
2318 [pbn_exar_ibm_saturn] = {
2319 .flags = FL_BASE0,
2320 .num_ports = 1,
2321 .base_baud = 921600,
2322 .uart_offset = 0x200,
2323 },
2324
2325 /*
2326 * PA Semi PWRficient PA6T-1682M on-chip UART
2327 */
2328 [pbn_pasemi_1682M] = {
2329 .flags = FL_BASE0,
2330 .num_ports = 1,
2331 .base_baud = 8333333,
2332 },
2333 /*
2334 * National Instruments 843x
2335 */
2336 [pbn_ni8430_16] = {
2337 .flags = FL_BASE0,
2338 .num_ports = 16,
2339 .base_baud = 3686400,
2340 .uart_offset = 0x10,
2341 .first_offset = 0x800,
2342 },
2343 [pbn_ni8430_8] = {
2344 .flags = FL_BASE0,
2345 .num_ports = 8,
2346 .base_baud = 3686400,
2347 .uart_offset = 0x10,
2348 .first_offset = 0x800,
2349 },
2350 [pbn_ni8430_4] = {
2351 .flags = FL_BASE0,
2352 .num_ports = 4,
2353 .base_baud = 3686400,
2354 .uart_offset = 0x10,
2355 .first_offset = 0x800,
2356 },
2357 [pbn_ni8430_2] = {
2358 .flags = FL_BASE0,
2359 .num_ports = 2,
2360 .base_baud = 3686400,
2361 .uart_offset = 0x10,
2362 .first_offset = 0x800,
2363 },
2364 /*
2365 * ADDI-DATA GmbH PCI-Express communication cards <info@addi-data.com>
2366 */
2367 [pbn_ADDIDATA_PCIe_1_3906250] = {
2368 .flags = FL_BASE0,
2369 .num_ports = 1,
2370 .base_baud = 3906250,
2371 .uart_offset = 0x200,
2372 .first_offset = 0x1000,
2373 },
2374 [pbn_ADDIDATA_PCIe_2_3906250] = {
2375 .flags = FL_BASE0,
2376 .num_ports = 2,
2377 .base_baud = 3906250,
2378 .uart_offset = 0x200,
2379 .first_offset = 0x1000,
2380 },
2381 [pbn_ADDIDATA_PCIe_4_3906250] = {
2382 .flags = FL_BASE0,
2383 .num_ports = 4,
2384 .base_baud = 3906250,
2385 .uart_offset = 0x200,
2386 .first_offset = 0x1000,
2387 },
2388 [pbn_ADDIDATA_PCIe_8_3906250] = {
2389 .flags = FL_BASE0,
2390 .num_ports = 8,
2391 .base_baud = 3906250,
2392 .uart_offset = 0x200,
2393 .first_offset = 0x1000,
2394 },
2395 [pbn_ce4100_1_115200] = {
2396 .flags = FL_BASE0,
2397 .num_ports = 1,
2398 .base_baud = 921600,
2399 .reg_shift = 2,
2400 },
2401 [pbn_omegapci] = {
2402 .flags = FL_BASE0,
2403 .num_ports = 8,
2404 .base_baud = 115200,
2405 .uart_offset = 0x200,
2406 },
2407};
2408
2409static const struct pci_device_id softmodem_blacklist[] = {
2410 { PCI_VDEVICE(AL, 0x5457), }, /* ALi Corporation M5457 AC'97 Modem */
2411 { PCI_VDEVICE(MOTOROLA, 0x3052), }, /* Motorola Si3052-based modem */
2412 { PCI_DEVICE(0x1543, 0x3052), }, /* Si3052-based modem, default IDs */
2413};
2414
2415/*
2416 * Given a complete unknown PCI device, try to use some heuristics to
2417 * guess what the configuration might be, based on the pitiful PCI
2418 * serial specs. Returns 0 on success, 1 on failure.
2419 */
2420static int __devinit
2421serial_pci_guess_board(struct pci_dev *dev, struct pciserial_board *board)
2422{
2423 const struct pci_device_id *blacklist;
2424 int num_iomem, num_port, first_port = -1, i;
2425
2426 /*
2427 * If it is not a communications device or the programming
2428 * interface is greater than 6, give up.
2429 *
2430 * (Should we try to make guesses for multiport serial devices
2431 * later?)
2432 */
2433 if ((((dev->class >> 8) != PCI_CLASS_COMMUNICATION_SERIAL) &&
2434 ((dev->class >> 8) != PCI_CLASS_COMMUNICATION_MODEM)) ||
2435 (dev->class & 0xff) > 6)
2436 return -ENODEV;
2437
2438 /*
2439 * Do not access blacklisted devices that are known not to
2440 * feature serial ports.
2441 */
2442 for (blacklist = softmodem_blacklist;
2443 blacklist < softmodem_blacklist + ARRAY_SIZE(softmodem_blacklist);
2444 blacklist++) {
2445 if (dev->vendor == blacklist->vendor &&
2446 dev->device == blacklist->device)
2447 return -ENODEV;
2448 }
2449
2450 num_iomem = num_port = 0;
2451 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2452 if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
2453 num_port++;
2454 if (first_port == -1)
2455 first_port = i;
2456 }
2457 if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
2458 num_iomem++;
2459 }
2460
2461 /*
2462 * If there is 1 or 0 iomem regions, and exactly one port,
2463 * use it. We guess the number of ports based on the IO
2464 * region size.
2465 */
2466 if (num_iomem <= 1 && num_port == 1) {
2467 board->flags = first_port;
2468 board->num_ports = pci_resource_len(dev, first_port) / 8;
2469 return 0;
2470 }
2471
2472 /*
2473 * Now guess if we've got a board which indexes by BARs.
2474 * Each IO BAR should be 8 bytes, and they should follow
2475 * consecutively.
2476 */
2477 first_port = -1;
2478 num_port = 0;
2479 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2480 if (pci_resource_flags(dev, i) & IORESOURCE_IO &&
2481 pci_resource_len(dev, i) == 8 &&
2482 (first_port == -1 || (first_port + num_port) == i)) {
2483 num_port++;
2484 if (first_port == -1)
2485 first_port = i;
2486 }
2487 }
2488
2489 if (num_port > 1) {
2490 board->flags = first_port | FL_BASE_BARS;
2491 board->num_ports = num_port;
2492 return 0;
2493 }
2494
2495 return -ENODEV;
2496}
2497
2498static inline int
2499serial_pci_matches(const struct pciserial_board *board,
2500 const struct pciserial_board *guessed)
2501{
2502 return
2503 board->num_ports == guessed->num_ports &&
2504 board->base_baud == guessed->base_baud &&
2505 board->uart_offset == guessed->uart_offset &&
2506 board->reg_shift == guessed->reg_shift &&
2507 board->first_offset == guessed->first_offset;
2508}
2509
2510struct serial_private *
2511pciserial_init_ports(struct pci_dev *dev, const struct pciserial_board *board)
2512{
2513 struct uart_port serial_port;
2514 struct serial_private *priv;
2515 struct pci_serial_quirk *quirk;
2516 int rc, nr_ports, i;
2517
2518 nr_ports = board->num_ports;
2519
2520 /*
2521 * Find an init and setup quirks.
2522 */
2523 quirk = find_quirk(dev);
2524
2525 /*
2526 * Run the new-style initialization function.
2527 * The initialization function returns:
2528 * <0 - error
2529 * 0 - use board->num_ports
2530 * >0 - number of ports
2531 */
2532 if (quirk->init) {
2533 rc = quirk->init(dev);
2534 if (rc < 0) {
2535 priv = ERR_PTR(rc);
2536 goto err_out;
2537 }
2538 if (rc)
2539 nr_ports = rc;
2540 }
2541
2542 priv = kzalloc(sizeof(struct serial_private) +
2543 sizeof(unsigned int) * nr_ports,
2544 GFP_KERNEL);
2545 if (!priv) {
2546 priv = ERR_PTR(-ENOMEM);
2547 goto err_deinit;
2548 }
2549
2550 priv->dev = dev;
2551 priv->quirk = quirk;
2552
2553 memset(&serial_port, 0, sizeof(struct uart_port));
2554 serial_port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
2555 serial_port.uartclk = board->base_baud * 16;
2556 serial_port.irq = get_pci_irq(dev, board);
2557 serial_port.dev = &dev->dev;
2558
2559 for (i = 0; i < nr_ports; i++) {
2560 if (quirk->setup(priv, board, &serial_port, i))
2561 break;
2562
2563#ifdef SERIAL_DEBUG_PCI
2564 printk(KERN_DEBUG "Setup PCI port: port %lx, irq %d, type %d\n",
2565 serial_port.iobase, serial_port.irq, serial_port.iotype);
2566#endif
2567
2568 priv->line[i] = serial8250_register_port(&serial_port);
2569 if (priv->line[i] < 0) {
2570 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), priv->line[i]);
2571 break;
2572 }
2573 }
2574 priv->nr = i;
2575 return priv;
2576
2577err_deinit:
2578 if (quirk->exit)
2579 quirk->exit(dev);
2580err_out:
2581 return priv;
2582}
2583EXPORT_SYMBOL_GPL(pciserial_init_ports);
2584
2585void pciserial_remove_ports(struct serial_private *priv)
2586{
2587 struct pci_serial_quirk *quirk;
2588 int i;
2589
2590 for (i = 0; i < priv->nr; i++)
2591 serial8250_unregister_port(priv->line[i]);
2592
2593 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2594 if (priv->remapped_bar[i])
2595 iounmap(priv->remapped_bar[i]);
2596 priv->remapped_bar[i] = NULL;
2597 }
2598
2599 /*
2600 * Find the exit quirks.
2601 */
2602 quirk = find_quirk(priv->dev);
2603 if (quirk->exit)
2604 quirk->exit(priv->dev);
2605
2606 kfree(priv);
2607}
2608EXPORT_SYMBOL_GPL(pciserial_remove_ports);
2609
2610void pciserial_suspend_ports(struct serial_private *priv)
2611{
2612 int i;
2613
2614 for (i = 0; i < priv->nr; i++)
2615 if (priv->line[i] >= 0)
2616 serial8250_suspend_port(priv->line[i]);
2617}
2618EXPORT_SYMBOL_GPL(pciserial_suspend_ports);
2619
2620void pciserial_resume_ports(struct serial_private *priv)
2621{
2622 int i;
2623
2624 /*
2625 * Ensure that the board is correctly configured.
2626 */
2627 if (priv->quirk->init)
2628 priv->quirk->init(priv->dev);
2629
2630 for (i = 0; i < priv->nr; i++)
2631 if (priv->line[i] >= 0)
2632 serial8250_resume_port(priv->line[i]);
2633}
2634EXPORT_SYMBOL_GPL(pciserial_resume_ports);
2635
2636/*
2637 * Probe one serial board. Unfortunately, there is no rhyme nor reason
2638 * to the arrangement of serial ports on a PCI card.
2639 */
2640static int __devinit
2641pciserial_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
2642{
2643 struct serial_private *priv;
2644 const struct pciserial_board *board;
2645 struct pciserial_board tmp;
2646 int rc;
2647
2648 if (ent->driver_data >= ARRAY_SIZE(pci_boards)) {
2649 printk(KERN_ERR "pci_init_one: invalid driver_data: %ld\n",
2650 ent->driver_data);
2651 return -EINVAL;
2652 }
2653
2654 board = &pci_boards[ent->driver_data];
2655
2656 rc = pci_enable_device(dev);
2657 if (rc)
2658 return rc;
2659
2660 if (ent->driver_data == pbn_default) {
2661 /*
2662 * Use a copy of the pci_board entry for this;
2663 * avoid changing entries in the table.
2664 */
2665 memcpy(&tmp, board, sizeof(struct pciserial_board));
2666 board = &tmp;
2667
2668 /*
2669 * We matched one of our class entries. Try to
2670 * determine the parameters of this board.
2671 */
2672 rc = serial_pci_guess_board(dev, &tmp);
2673 if (rc)
2674 goto disable;
2675 } else {
2676 /*
2677 * We matched an explicit entry. If we are able to
2678 * detect this boards settings with our heuristic,
2679 * then we no longer need this entry.
2680 */
2681 memcpy(&tmp, &pci_boards[pbn_default],
2682 sizeof(struct pciserial_board));
2683 rc = serial_pci_guess_board(dev, &tmp);
2684 if (rc == 0 && serial_pci_matches(board, &tmp))
2685 moan_device("Redundant entry in serial pci_table.",
2686 dev);
2687 }
2688
2689 priv = pciserial_init_ports(dev, board);
2690 if (!IS_ERR(priv)) {
2691 pci_set_drvdata(dev, priv);
2692 return 0;
2693 }
2694
2695 rc = PTR_ERR(priv);
2696
2697 disable:
2698 pci_disable_device(dev);
2699 return rc;
2700}
2701
2702static void __devexit pciserial_remove_one(struct pci_dev *dev)
2703{
2704 struct serial_private *priv = pci_get_drvdata(dev);
2705
2706 pci_set_drvdata(dev, NULL);
2707
2708 pciserial_remove_ports(priv);
2709
2710 pci_disable_device(dev);
2711}
2712
2713#ifdef CONFIG_PM
2714static int pciserial_suspend_one(struct pci_dev *dev, pm_message_t state)
2715{
2716 struct serial_private *priv = pci_get_drvdata(dev);
2717
2718 if (priv)
2719 pciserial_suspend_ports(priv);
2720
2721 pci_save_state(dev);
2722 pci_set_power_state(dev, pci_choose_state(dev, state));
2723 return 0;
2724}
2725
2726static int pciserial_resume_one(struct pci_dev *dev)
2727{
2728 int err;
2729 struct serial_private *priv = pci_get_drvdata(dev);
2730
2731 pci_set_power_state(dev, PCI_D0);
2732 pci_restore_state(dev);
2733
2734 if (priv) {
2735 /*
2736 * The device may have been disabled. Re-enable it.
2737 */
2738 err = pci_enable_device(dev);
2739 /* FIXME: We cannot simply error out here */
2740 if (err)
2741 printk(KERN_ERR "pciserial: Unable to re-enable ports, trying to continue.\n");
2742 pciserial_resume_ports(priv);
2743 }
2744 return 0;
2745}
2746#endif
2747
2748static struct pci_device_id serial_pci_tbl[] = {
2749 /* Advantech use PCI_DEVICE_ID_ADVANTECH_PCI3620 (0x3620) as 'PCI_SUBVENDOR_ID' */
2750 { PCI_VENDOR_ID_ADVANTECH, PCI_DEVICE_ID_ADVANTECH_PCI3620,
2751 PCI_DEVICE_ID_ADVANTECH_PCI3620, 0x0001, 0, 0,
2752 pbn_b2_8_921600 },
2753 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2754 PCI_SUBVENDOR_ID_CONNECT_TECH,
2755 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2756 pbn_b1_8_1382400 },
2757 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2758 PCI_SUBVENDOR_ID_CONNECT_TECH,
2759 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2760 pbn_b1_4_1382400 },
2761 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2762 PCI_SUBVENDOR_ID_CONNECT_TECH,
2763 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2764 pbn_b1_2_1382400 },
2765 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2766 PCI_SUBVENDOR_ID_CONNECT_TECH,
2767 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2768 pbn_b1_8_1382400 },
2769 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2770 PCI_SUBVENDOR_ID_CONNECT_TECH,
2771 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2772 pbn_b1_4_1382400 },
2773 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2774 PCI_SUBVENDOR_ID_CONNECT_TECH,
2775 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2776 pbn_b1_2_1382400 },
2777 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2778 PCI_SUBVENDOR_ID_CONNECT_TECH,
2779 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485, 0, 0,
2780 pbn_b1_8_921600 },
2781 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2782 PCI_SUBVENDOR_ID_CONNECT_TECH,
2783 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4, 0, 0,
2784 pbn_b1_8_921600 },
2785 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2786 PCI_SUBVENDOR_ID_CONNECT_TECH,
2787 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485, 0, 0,
2788 pbn_b1_4_921600 },
2789 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2790 PCI_SUBVENDOR_ID_CONNECT_TECH,
2791 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2, 0, 0,
2792 pbn_b1_4_921600 },
2793 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2794 PCI_SUBVENDOR_ID_CONNECT_TECH,
2795 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485, 0, 0,
2796 pbn_b1_2_921600 },
2797 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2798 PCI_SUBVENDOR_ID_CONNECT_TECH,
2799 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_2_6, 0, 0,
2800 pbn_b1_8_921600 },
2801 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2802 PCI_SUBVENDOR_ID_CONNECT_TECH,
2803 PCI_SUBDEVICE_ID_CONNECT_TECH_BH081101V1, 0, 0,
2804 pbn_b1_8_921600 },
2805 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2806 PCI_SUBVENDOR_ID_CONNECT_TECH,
2807 PCI_SUBDEVICE_ID_CONNECT_TECH_BH041101V1, 0, 0,
2808 pbn_b1_4_921600 },
2809 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2810 PCI_SUBVENDOR_ID_CONNECT_TECH,
2811 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_20MHZ, 0, 0,
2812 pbn_b1_2_1250000 },
2813 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2814 PCI_SUBVENDOR_ID_CONNECT_TECH,
2815 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_2, 0, 0,
2816 pbn_b0_2_1843200 },
2817 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2818 PCI_SUBVENDOR_ID_CONNECT_TECH,
2819 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_4, 0, 0,
2820 pbn_b0_4_1843200 },
2821 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2822 PCI_VENDOR_ID_AFAVLAB,
2823 PCI_SUBDEVICE_ID_AFAVLAB_P061, 0, 0,
2824 pbn_b0_4_1152000 },
2825 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2826 PCI_SUBVENDOR_ID_CONNECT_TECH,
2827 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_232, 0, 0,
2828 pbn_b0_2_1843200_200 },
2829 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2830 PCI_SUBVENDOR_ID_CONNECT_TECH,
2831 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_232, 0, 0,
2832 pbn_b0_4_1843200_200 },
2833 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2834 PCI_SUBVENDOR_ID_CONNECT_TECH,
2835 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_232, 0, 0,
2836 pbn_b0_8_1843200_200 },
2837 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2838 PCI_SUBVENDOR_ID_CONNECT_TECH,
2839 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_1_1, 0, 0,
2840 pbn_b0_2_1843200_200 },
2841 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2842 PCI_SUBVENDOR_ID_CONNECT_TECH,
2843 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_2, 0, 0,
2844 pbn_b0_4_1843200_200 },
2845 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2846 PCI_SUBVENDOR_ID_CONNECT_TECH,
2847 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4, 0, 0,
2848 pbn_b0_8_1843200_200 },
2849 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2850 PCI_SUBVENDOR_ID_CONNECT_TECH,
2851 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2, 0, 0,
2852 pbn_b0_2_1843200_200 },
2853 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2854 PCI_SUBVENDOR_ID_CONNECT_TECH,
2855 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4, 0, 0,
2856 pbn_b0_4_1843200_200 },
2857 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2858 PCI_SUBVENDOR_ID_CONNECT_TECH,
2859 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8, 0, 0,
2860 pbn_b0_8_1843200_200 },
2861 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2862 PCI_SUBVENDOR_ID_CONNECT_TECH,
2863 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_485, 0, 0,
2864 pbn_b0_2_1843200_200 },
2865 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2866 PCI_SUBVENDOR_ID_CONNECT_TECH,
2867 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_485, 0, 0,
2868 pbn_b0_4_1843200_200 },
2869 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2870 PCI_SUBVENDOR_ID_CONNECT_TECH,
2871 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_485, 0, 0,
2872 pbn_b0_8_1843200_200 },
2873 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2874 PCI_VENDOR_ID_IBM, PCI_SUBDEVICE_ID_IBM_SATURN_SERIAL_ONE_PORT,
2875 0, 0, pbn_exar_ibm_saturn },
2876
2877 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
2878 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2879 pbn_b2_bt_1_115200 },
2880 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
2881 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2882 pbn_b2_bt_2_115200 },
2883 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
2884 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2885 pbn_b2_bt_4_115200 },
2886 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
2887 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2888 pbn_b2_bt_2_115200 },
2889 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
2890 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2891 pbn_b2_bt_4_115200 },
2892 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
2893 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2894 pbn_b2_8_115200 },
2895 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_7803,
2896 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2897 pbn_b2_8_460800 },
2898 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM8,
2899 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2900 pbn_b2_8_115200 },
2901
2902 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
2903 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2904 pbn_b2_bt_2_115200 },
2905 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
2906 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2907 pbn_b2_bt_2_921600 },
2908 /*
2909 * VScom SPCOM800, from sl@s.pl
2910 */
2911 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM800,
2912 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2913 pbn_b2_8_921600 },
2914 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_1077,
2915 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2916 pbn_b2_4_921600 },
2917 /* Unknown card - subdevice 0x1584 */
2918 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2919 PCI_VENDOR_ID_PLX,
2920 PCI_SUBDEVICE_ID_UNKNOWN_0x1584, 0, 0,
2921 pbn_b0_4_115200 },
2922 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2923 PCI_SUBVENDOR_ID_KEYSPAN,
2924 PCI_SUBDEVICE_ID_KEYSPAN_SX2, 0, 0,
2925 pbn_panacom },
2926 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
2927 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2928 pbn_panacom4 },
2929 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
2930 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2931 pbn_panacom2 },
2932 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
2933 PCI_VENDOR_ID_ESDGMBH,
2934 PCI_DEVICE_ID_ESDGMBH_CPCIASIO4, 0, 0,
2935 pbn_b2_4_115200 },
2936 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2937 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2938 PCI_SUBDEVICE_ID_CHASE_PCIFAST4, 0, 0,
2939 pbn_b2_4_460800 },
2940 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2941 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2942 PCI_SUBDEVICE_ID_CHASE_PCIFAST8, 0, 0,
2943 pbn_b2_8_460800 },
2944 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2945 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2946 PCI_SUBDEVICE_ID_CHASE_PCIFAST16, 0, 0,
2947 pbn_b2_16_460800 },
2948 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2949 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2950 PCI_SUBDEVICE_ID_CHASE_PCIFAST16FMC, 0, 0,
2951 pbn_b2_16_460800 },
2952 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2953 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2954 PCI_SUBDEVICE_ID_CHASE_PCIRAS4, 0, 0,
2955 pbn_b2_4_460800 },
2956 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2957 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2958 PCI_SUBDEVICE_ID_CHASE_PCIRAS8, 0, 0,
2959 pbn_b2_8_460800 },
2960 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2961 PCI_SUBVENDOR_ID_EXSYS,
2962 PCI_SUBDEVICE_ID_EXSYS_4055, 0, 0,
2963 pbn_exsys_4055 },
2964 /*
2965 * Megawolf Romulus PCI Serial Card, from Mike Hudson
2966 * (Exoray@isys.ca)
2967 */
2968 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_ROMULUS,
2969 0x10b5, 0x106a, 0, 0,
2970 pbn_plx_romulus },
2971 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_QSC100,
2972 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2973 pbn_b1_4_115200 },
2974 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_DSC100,
2975 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2976 pbn_b1_2_115200 },
2977 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100D,
2978 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2979 pbn_b1_8_115200 },
2980 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100M,
2981 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2982 pbn_b1_8_115200 },
2983 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_OXSEMI_16PCI954,
2984 PCI_VENDOR_ID_SPECIALIX, PCI_SUBDEVICE_ID_SPECIALIX_SPEED4,
2985 0, 0,
2986 pbn_b0_4_921600 },
2987 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2988 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_QUARTET_SERIAL,
2989 0, 0,
2990 pbn_b0_4_1152000 },
2991 { PCI_VENDOR_ID_OXSEMI, 0x9505,
2992 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2993 pbn_b0_bt_2_921600 },
2994
2995 /*
2996 * The below card is a little controversial since it is the
2997 * subject of a PCI vendor/device ID clash. (See
2998 * www.ussg.iu.edu/hypermail/linux/kernel/0303.1/0516.html).
2999 * For now just used the hex ID 0x950a.
3000 */
3001 { PCI_VENDOR_ID_OXSEMI, 0x950a,
3002 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_DUAL_SERIAL, 0, 0,
3003 pbn_b0_2_115200 },
3004 { PCI_VENDOR_ID_OXSEMI, 0x950a,
3005 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3006 pbn_b0_2_1130000 },
3007 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_C950,
3008 PCI_VENDOR_ID_OXSEMI, PCI_SUBDEVICE_ID_OXSEMI_C950, 0, 0,
3009 pbn_b0_1_921600 },
3010 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
3011 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3012 pbn_b0_4_115200 },
3013 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952,
3014 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3015 pbn_b0_bt_2_921600 },
3016 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI958,
3017 PCI_ANY_ID , PCI_ANY_ID, 0, 0,
3018 pbn_b2_8_1152000 },
3019
3020 /*
3021 * Oxford Semiconductor Inc. Tornado PCI express device range.
3022 */
3023 { PCI_VENDOR_ID_OXSEMI, 0xc101, /* OXPCIe952 1 Legacy UART */
3024 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3025 pbn_b0_1_4000000 },
3026 { PCI_VENDOR_ID_OXSEMI, 0xc105, /* OXPCIe952 1 Legacy UART */
3027 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3028 pbn_b0_1_4000000 },
3029 { PCI_VENDOR_ID_OXSEMI, 0xc11b, /* OXPCIe952 1 Native UART */
3030 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3031 pbn_oxsemi_1_4000000 },
3032 { PCI_VENDOR_ID_OXSEMI, 0xc11f, /* OXPCIe952 1 Native UART */
3033 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3034 pbn_oxsemi_1_4000000 },
3035 { PCI_VENDOR_ID_OXSEMI, 0xc120, /* OXPCIe952 1 Legacy UART */
3036 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3037 pbn_b0_1_4000000 },
3038 { PCI_VENDOR_ID_OXSEMI, 0xc124, /* OXPCIe952 1 Legacy UART */
3039 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3040 pbn_b0_1_4000000 },
3041 { PCI_VENDOR_ID_OXSEMI, 0xc138, /* OXPCIe952 1 Native UART */
3042 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3043 pbn_oxsemi_1_4000000 },
3044 { PCI_VENDOR_ID_OXSEMI, 0xc13d, /* OXPCIe952 1 Native UART */
3045 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3046 pbn_oxsemi_1_4000000 },
3047 { PCI_VENDOR_ID_OXSEMI, 0xc140, /* OXPCIe952 1 Legacy UART */
3048 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3049 pbn_b0_1_4000000 },
3050 { PCI_VENDOR_ID_OXSEMI, 0xc141, /* OXPCIe952 1 Legacy UART */
3051 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3052 pbn_b0_1_4000000 },
3053 { PCI_VENDOR_ID_OXSEMI, 0xc144, /* OXPCIe952 1 Legacy UART */
3054 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3055 pbn_b0_1_4000000 },
3056 { PCI_VENDOR_ID_OXSEMI, 0xc145, /* OXPCIe952 1 Legacy UART */
3057 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3058 pbn_b0_1_4000000 },
3059 { PCI_VENDOR_ID_OXSEMI, 0xc158, /* OXPCIe952 2 Native UART */
3060 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3061 pbn_oxsemi_2_4000000 },
3062 { PCI_VENDOR_ID_OXSEMI, 0xc15d, /* OXPCIe952 2 Native UART */
3063 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3064 pbn_oxsemi_2_4000000 },
3065 { PCI_VENDOR_ID_OXSEMI, 0xc208, /* OXPCIe954 4 Native UART */
3066 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3067 pbn_oxsemi_4_4000000 },
3068 { PCI_VENDOR_ID_OXSEMI, 0xc20d, /* OXPCIe954 4 Native UART */
3069 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3070 pbn_oxsemi_4_4000000 },
3071 { PCI_VENDOR_ID_OXSEMI, 0xc308, /* OXPCIe958 8 Native UART */
3072 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3073 pbn_oxsemi_8_4000000 },
3074 { PCI_VENDOR_ID_OXSEMI, 0xc30d, /* OXPCIe958 8 Native UART */
3075 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3076 pbn_oxsemi_8_4000000 },
3077 { PCI_VENDOR_ID_OXSEMI, 0xc40b, /* OXPCIe200 1 Native UART */
3078 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3079 pbn_oxsemi_1_4000000 },
3080 { PCI_VENDOR_ID_OXSEMI, 0xc40f, /* OXPCIe200 1 Native UART */
3081 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3082 pbn_oxsemi_1_4000000 },
3083 { PCI_VENDOR_ID_OXSEMI, 0xc41b, /* OXPCIe200 1 Native UART */
3084 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3085 pbn_oxsemi_1_4000000 },
3086 { PCI_VENDOR_ID_OXSEMI, 0xc41f, /* OXPCIe200 1 Native UART */
3087 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3088 pbn_oxsemi_1_4000000 },
3089 { PCI_VENDOR_ID_OXSEMI, 0xc42b, /* OXPCIe200 1 Native UART */
3090 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3091 pbn_oxsemi_1_4000000 },
3092 { PCI_VENDOR_ID_OXSEMI, 0xc42f, /* OXPCIe200 1 Native UART */
3093 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3094 pbn_oxsemi_1_4000000 },
3095 { PCI_VENDOR_ID_OXSEMI, 0xc43b, /* OXPCIe200 1 Native UART */
3096 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3097 pbn_oxsemi_1_4000000 },
3098 { PCI_VENDOR_ID_OXSEMI, 0xc43f, /* OXPCIe200 1 Native UART */
3099 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3100 pbn_oxsemi_1_4000000 },
3101 { PCI_VENDOR_ID_OXSEMI, 0xc44b, /* OXPCIe200 1 Native UART */
3102 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3103 pbn_oxsemi_1_4000000 },
3104 { PCI_VENDOR_ID_OXSEMI, 0xc44f, /* OXPCIe200 1 Native UART */
3105 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3106 pbn_oxsemi_1_4000000 },
3107 { PCI_VENDOR_ID_OXSEMI, 0xc45b, /* OXPCIe200 1 Native UART */
3108 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3109 pbn_oxsemi_1_4000000 },
3110 { PCI_VENDOR_ID_OXSEMI, 0xc45f, /* OXPCIe200 1 Native UART */
3111 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3112 pbn_oxsemi_1_4000000 },
3113 { PCI_VENDOR_ID_OXSEMI, 0xc46b, /* OXPCIe200 1 Native UART */
3114 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3115 pbn_oxsemi_1_4000000 },
3116 { PCI_VENDOR_ID_OXSEMI, 0xc46f, /* OXPCIe200 1 Native UART */
3117 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3118 pbn_oxsemi_1_4000000 },
3119 { PCI_VENDOR_ID_OXSEMI, 0xc47b, /* OXPCIe200 1 Native UART */
3120 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3121 pbn_oxsemi_1_4000000 },
3122 { PCI_VENDOR_ID_OXSEMI, 0xc47f, /* OXPCIe200 1 Native UART */
3123 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3124 pbn_oxsemi_1_4000000 },
3125 { PCI_VENDOR_ID_OXSEMI, 0xc48b, /* OXPCIe200 1 Native UART */
3126 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3127 pbn_oxsemi_1_4000000 },
3128 { PCI_VENDOR_ID_OXSEMI, 0xc48f, /* OXPCIe200 1 Native UART */
3129 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3130 pbn_oxsemi_1_4000000 },
3131 { PCI_VENDOR_ID_OXSEMI, 0xc49b, /* OXPCIe200 1 Native UART */
3132 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3133 pbn_oxsemi_1_4000000 },
3134 { PCI_VENDOR_ID_OXSEMI, 0xc49f, /* OXPCIe200 1 Native UART */
3135 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3136 pbn_oxsemi_1_4000000 },
3137 { PCI_VENDOR_ID_OXSEMI, 0xc4ab, /* OXPCIe200 1 Native UART */
3138 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3139 pbn_oxsemi_1_4000000 },
3140 { PCI_VENDOR_ID_OXSEMI, 0xc4af, /* OXPCIe200 1 Native UART */
3141 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3142 pbn_oxsemi_1_4000000 },
3143 { PCI_VENDOR_ID_OXSEMI, 0xc4bb, /* OXPCIe200 1 Native UART */
3144 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3145 pbn_oxsemi_1_4000000 },
3146 { PCI_VENDOR_ID_OXSEMI, 0xc4bf, /* OXPCIe200 1 Native UART */
3147 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3148 pbn_oxsemi_1_4000000 },
3149 { PCI_VENDOR_ID_OXSEMI, 0xc4cb, /* OXPCIe200 1 Native UART */
3150 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3151 pbn_oxsemi_1_4000000 },
3152 { PCI_VENDOR_ID_OXSEMI, 0xc4cf, /* OXPCIe200 1 Native UART */
3153 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3154 pbn_oxsemi_1_4000000 },
3155 /*
3156 * Mainpine Inc. IQ Express "Rev3" utilizing OxSemi Tornado
3157 */
3158 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 1 Port V.34 Super-G3 Fax */
3159 PCI_VENDOR_ID_MAINPINE, 0x4001, 0, 0,
3160 pbn_oxsemi_1_4000000 },
3161 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 2 Port V.34 Super-G3 Fax */
3162 PCI_VENDOR_ID_MAINPINE, 0x4002, 0, 0,
3163 pbn_oxsemi_2_4000000 },
3164 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 4 Port V.34 Super-G3 Fax */
3165 PCI_VENDOR_ID_MAINPINE, 0x4004, 0, 0,
3166 pbn_oxsemi_4_4000000 },
3167 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 8 Port V.34 Super-G3 Fax */
3168 PCI_VENDOR_ID_MAINPINE, 0x4008, 0, 0,
3169 pbn_oxsemi_8_4000000 },
3170
3171 /*
3172 * Digi/IBM PCIe 2-port Async EIA-232 Adapter utilizing OxSemi Tornado
3173 */
3174 { PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_2_OX_IBM,
3175 PCI_SUBVENDOR_ID_IBM, PCI_ANY_ID, 0, 0,
3176 pbn_oxsemi_2_4000000 },
3177
3178 /*
3179 * SBS Technologies, Inc. P-Octal and PMC-OCTPRO cards,
3180 * from skokodyn@yahoo.com
3181 */
3182 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3183 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO232, 0, 0,
3184 pbn_sbsxrsio },
3185 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3186 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO422, 0, 0,
3187 pbn_sbsxrsio },
3188 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3189 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL232, 0, 0,
3190 pbn_sbsxrsio },
3191 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3192 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL422, 0, 0,
3193 pbn_sbsxrsio },
3194
3195 /*
3196 * Digitan DS560-558, from jimd@esoft.com
3197 */
3198 { PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_ATT_VENUS_MODEM,
3199 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3200 pbn_b1_1_115200 },
3201
3202 /*
3203 * Titan Electronic cards
3204 * The 400L and 800L have a custom setup quirk.
3205 */
3206 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100,
3207 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3208 pbn_b0_1_921600 },
3209 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200,
3210 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3211 pbn_b0_2_921600 },
3212 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400,
3213 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3214 pbn_b0_4_921600 },
3215 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800B,
3216 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3217 pbn_b0_4_921600 },
3218 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100L,
3219 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3220 pbn_b1_1_921600 },
3221 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200L,
3222 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3223 pbn_b1_bt_2_921600 },
3224 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400L,
3225 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3226 pbn_b0_bt_4_921600 },
3227 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800L,
3228 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3229 pbn_b0_bt_8_921600 },
3230 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200I,
3231 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3232 pbn_b4_bt_2_921600 },
3233 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400I,
3234 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3235 pbn_b4_bt_4_921600 },
3236 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800I,
3237 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3238 pbn_b4_bt_8_921600 },
3239 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400EH,
3240 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3241 pbn_b0_4_921600 },
3242 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EH,
3243 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3244 pbn_b0_4_921600 },
3245 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EHB,
3246 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3247 pbn_b0_4_921600 },
3248 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100E,
3249 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3250 pbn_oxsemi_1_4000000 },
3251 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200E,
3252 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3253 pbn_oxsemi_2_4000000 },
3254 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400E,
3255 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3256 pbn_oxsemi_4_4000000 },
3257 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800E,
3258 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3259 pbn_oxsemi_8_4000000 },
3260 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EI,
3261 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3262 pbn_oxsemi_2_4000000 },
3263 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EISI,
3264 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3265 pbn_oxsemi_2_4000000 },
3266
3267 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_550,
3268 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3269 pbn_b2_1_460800 },
3270 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_650,
3271 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3272 pbn_b2_1_460800 },
3273 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_850,
3274 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3275 pbn_b2_1_460800 },
3276 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_550,
3277 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3278 pbn_b2_bt_2_921600 },
3279 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_650,
3280 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3281 pbn_b2_bt_2_921600 },
3282 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_850,
3283 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3284 pbn_b2_bt_2_921600 },
3285 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_550,
3286 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3287 pbn_b2_bt_4_921600 },
3288 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_650,
3289 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3290 pbn_b2_bt_4_921600 },
3291 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_850,
3292 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3293 pbn_b2_bt_4_921600 },
3294 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_550,
3295 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3296 pbn_b0_1_921600 },
3297 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_650,
3298 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3299 pbn_b0_1_921600 },
3300 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_850,
3301 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3302 pbn_b0_1_921600 },
3303 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_550,
3304 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3305 pbn_b0_bt_2_921600 },
3306 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_650,
3307 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3308 pbn_b0_bt_2_921600 },
3309 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_850,
3310 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3311 pbn_b0_bt_2_921600 },
3312 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_550,
3313 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3314 pbn_b0_bt_4_921600 },
3315 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_650,
3316 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3317 pbn_b0_bt_4_921600 },
3318 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_850,
3319 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3320 pbn_b0_bt_4_921600 },
3321 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_550,
3322 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3323 pbn_b0_bt_8_921600 },
3324 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_650,
3325 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3326 pbn_b0_bt_8_921600 },
3327 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_850,
3328 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3329 pbn_b0_bt_8_921600 },
3330
3331 /*
3332 * Computone devices submitted by Doug McNash dmcnash@computone.com
3333 */
3334 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3335 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG4,
3336 0, 0, pbn_computone_4 },
3337 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3338 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG8,
3339 0, 0, pbn_computone_8 },
3340 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3341 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG6,
3342 0, 0, pbn_computone_6 },
3343
3344 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI95N,
3345 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3346 pbn_oxsemi },
3347 { PCI_VENDOR_ID_TIMEDIA, PCI_DEVICE_ID_TIMEDIA_1889,
3348 PCI_VENDOR_ID_TIMEDIA, PCI_ANY_ID, 0, 0,
3349 pbn_b0_bt_1_921600 },
3350
3351 /*
3352 * AFAVLAB serial card, from Harald Welte <laforge@gnumonks.org>
3353 */
3354 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P028,
3355 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3356 pbn_b0_bt_8_115200 },
3357 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P030,
3358 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3359 pbn_b0_bt_8_115200 },
3360
3361 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DSERIAL,
3362 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3363 pbn_b0_bt_2_115200 },
3364 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_A,
3365 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3366 pbn_b0_bt_2_115200 },
3367 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_B,
3368 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3369 pbn_b0_bt_2_115200 },
3370 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_A,
3371 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3372 pbn_b0_bt_2_115200 },
3373 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_B,
3374 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3375 pbn_b0_bt_2_115200 },
3376 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_A,
3377 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3378 pbn_b0_bt_4_460800 },
3379 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_B,
3380 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3381 pbn_b0_bt_4_460800 },
3382 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_PLUS,
3383 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3384 pbn_b0_bt_2_460800 },
3385 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_A,
3386 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3387 pbn_b0_bt_2_460800 },
3388 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_B,
3389 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3390 pbn_b0_bt_2_460800 },
3391 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_SSERIAL,
3392 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3393 pbn_b0_bt_1_115200 },
3394 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_650,
3395 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3396 pbn_b0_bt_1_460800 },
3397
3398 /*
3399 * Korenix Jetcard F0/F1 cards (JC1204, JC1208, JC1404, JC1408).
3400 * Cards are identified by their subsystem vendor IDs, which
3401 * (in hex) match the model number.
3402 *
3403 * Note that JC140x are RS422/485 cards which require ox950
3404 * ACR = 0x10, and as such are not currently fully supported.
3405 */
3406 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3407 0x1204, 0x0004, 0, 0,
3408 pbn_b0_4_921600 },
3409 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3410 0x1208, 0x0004, 0, 0,
3411 pbn_b0_4_921600 },
3412/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3413 0x1402, 0x0002, 0, 0,
3414 pbn_b0_2_921600 }, */
3415/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3416 0x1404, 0x0004, 0, 0,
3417 pbn_b0_4_921600 }, */
3418 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF1,
3419 0x1208, 0x0004, 0, 0,
3420 pbn_b0_4_921600 },
3421
3422 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3423 0x1204, 0x0004, 0, 0,
3424 pbn_b0_4_921600 },
3425 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3426 0x1208, 0x0004, 0, 0,
3427 pbn_b0_4_921600 },
3428 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF3,
3429 0x1208, 0x0004, 0, 0,
3430 pbn_b0_4_921600 },
3431 /*
3432 * Dell Remote Access Card 4 - Tim_T_Murphy@Dell.com
3433 */
3434 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RAC4,
3435 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3436 pbn_b1_1_1382400 },
3437
3438 /*
3439 * Dell Remote Access Card III - Tim_T_Murphy@Dell.com
3440 */
3441 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RACIII,
3442 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3443 pbn_b1_1_1382400 },
3444
3445 /*
3446 * RAStel 2 port modem, gerg@moreton.com.au
3447 */
3448 { PCI_VENDOR_ID_MORETON, PCI_DEVICE_ID_RASTEL_2PORT,
3449 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3450 pbn_b2_bt_2_115200 },
3451
3452 /*
3453 * EKF addition for i960 Boards form EKF with serial port
3454 */
3455 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80960_RP,
3456 0xE4BF, PCI_ANY_ID, 0, 0,
3457 pbn_intel_i960 },
3458
3459 /*
3460 * Xircom Cardbus/Ethernet combos
3461 */
3462 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_X3201_MDM,
3463 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3464 pbn_b0_1_115200 },
3465 /*
3466 * Xircom RBM56G cardbus modem - Dirk Arnold (temp entry)
3467 */
3468 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_RBM56G,
3469 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3470 pbn_b0_1_115200 },
3471
3472 /*
3473 * Untested PCI modems, sent in from various folks...
3474 */
3475
3476 /*
3477 * Elsa Model 56K PCI Modem, from Andreas Rath <arh@01019freenet.de>
3478 */
3479 { PCI_VENDOR_ID_ROCKWELL, 0x1004,
3480 0x1048, 0x1500, 0, 0,
3481 pbn_b1_1_115200 },
3482
3483 { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
3484 0xFF00, 0, 0, 0,
3485 pbn_sgi_ioc3 },
3486
3487 /*
3488 * HP Diva card
3489 */
3490 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3491 PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_RMP3, 0, 0,
3492 pbn_b1_1_115200 },
3493 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3494 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3495 pbn_b0_5_115200 },
3496 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_AUX,
3497 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3498 pbn_b2_1_115200 },
3499
3500 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM2,
3501 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3502 pbn_b3_2_115200 },
3503 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM4,
3504 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3505 pbn_b3_4_115200 },
3506 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM8,
3507 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3508 pbn_b3_8_115200 },
3509
3510 /*
3511 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
3512 */
3513 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
3514 PCI_ANY_ID, PCI_ANY_ID,
3515 0,
3516 0, pbn_exar_XR17C152 },
3517 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
3518 PCI_ANY_ID, PCI_ANY_ID,
3519 0,
3520 0, pbn_exar_XR17C154 },
3521 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
3522 PCI_ANY_ID, PCI_ANY_ID,
3523 0,
3524 0, pbn_exar_XR17C158 },
3525
3526 /*
3527 * Topic TP560 Data/Fax/Voice 56k modem (reported by Evan Clarke)
3528 */
3529 { PCI_VENDOR_ID_TOPIC, PCI_DEVICE_ID_TOPIC_TP560,
3530 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3531 pbn_b0_1_115200 },
3532 /*
3533 * ITE
3534 */
3535 { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8872,
3536 PCI_ANY_ID, PCI_ANY_ID,
3537 0, 0,
3538 pbn_b1_bt_1_115200 },
3539
3540 /*
3541 * IntaShield IS-200
3542 */
3543 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS200,
3544 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0811 */
3545 pbn_b2_2_115200 },
3546 /*
3547 * IntaShield IS-400
3548 */
3549 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS400,
3550 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0dc0 */
3551 pbn_b2_4_115200 },
3552 /*
3553 * Perle PCI-RAS cards
3554 */
3555 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3556 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS4,
3557 0, 0, pbn_b2_4_921600 },
3558 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3559 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS8,
3560 0, 0, pbn_b2_8_921600 },
3561
3562 /*
3563 * Mainpine series cards: Fairly standard layout but fools
3564 * parts of the autodetect in some cases and uses otherwise
3565 * unmatched communications subclasses in the PCI Express case
3566 */
3567
3568 { /* RockForceDUO */
3569 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3570 PCI_VENDOR_ID_MAINPINE, 0x0200,
3571 0, 0, pbn_b0_2_115200 },
3572 { /* RockForceQUATRO */
3573 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3574 PCI_VENDOR_ID_MAINPINE, 0x0300,
3575 0, 0, pbn_b0_4_115200 },
3576 { /* RockForceDUO+ */
3577 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3578 PCI_VENDOR_ID_MAINPINE, 0x0400,
3579 0, 0, pbn_b0_2_115200 },
3580 { /* RockForceQUATRO+ */
3581 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3582 PCI_VENDOR_ID_MAINPINE, 0x0500,
3583 0, 0, pbn_b0_4_115200 },
3584 { /* RockForce+ */
3585 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3586 PCI_VENDOR_ID_MAINPINE, 0x0600,
3587 0, 0, pbn_b0_2_115200 },
3588 { /* RockForce+ */
3589 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3590 PCI_VENDOR_ID_MAINPINE, 0x0700,
3591 0, 0, pbn_b0_4_115200 },
3592 { /* RockForceOCTO+ */
3593 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3594 PCI_VENDOR_ID_MAINPINE, 0x0800,
3595 0, 0, pbn_b0_8_115200 },
3596 { /* RockForceDUO+ */
3597 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3598 PCI_VENDOR_ID_MAINPINE, 0x0C00,
3599 0, 0, pbn_b0_2_115200 },
3600 { /* RockForceQUARTRO+ */
3601 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3602 PCI_VENDOR_ID_MAINPINE, 0x0D00,
3603 0, 0, pbn_b0_4_115200 },
3604 { /* RockForceOCTO+ */
3605 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3606 PCI_VENDOR_ID_MAINPINE, 0x1D00,
3607 0, 0, pbn_b0_8_115200 },
3608 { /* RockForceD1 */
3609 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3610 PCI_VENDOR_ID_MAINPINE, 0x2000,
3611 0, 0, pbn_b0_1_115200 },
3612 { /* RockForceF1 */
3613 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3614 PCI_VENDOR_ID_MAINPINE, 0x2100,
3615 0, 0, pbn_b0_1_115200 },
3616 { /* RockForceD2 */
3617 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3618 PCI_VENDOR_ID_MAINPINE, 0x2200,
3619 0, 0, pbn_b0_2_115200 },
3620 { /* RockForceF2 */
3621 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3622 PCI_VENDOR_ID_MAINPINE, 0x2300,
3623 0, 0, pbn_b0_2_115200 },
3624 { /* RockForceD4 */
3625 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3626 PCI_VENDOR_ID_MAINPINE, 0x2400,
3627 0, 0, pbn_b0_4_115200 },
3628 { /* RockForceF4 */
3629 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3630 PCI_VENDOR_ID_MAINPINE, 0x2500,
3631 0, 0, pbn_b0_4_115200 },
3632 { /* RockForceD8 */
3633 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3634 PCI_VENDOR_ID_MAINPINE, 0x2600,
3635 0, 0, pbn_b0_8_115200 },
3636 { /* RockForceF8 */
3637 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3638 PCI_VENDOR_ID_MAINPINE, 0x2700,
3639 0, 0, pbn_b0_8_115200 },
3640 { /* IQ Express D1 */
3641 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3642 PCI_VENDOR_ID_MAINPINE, 0x3000,
3643 0, 0, pbn_b0_1_115200 },
3644 { /* IQ Express F1 */
3645 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3646 PCI_VENDOR_ID_MAINPINE, 0x3100,
3647 0, 0, pbn_b0_1_115200 },
3648 { /* IQ Express D2 */
3649 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3650 PCI_VENDOR_ID_MAINPINE, 0x3200,
3651 0, 0, pbn_b0_2_115200 },
3652 { /* IQ Express F2 */
3653 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3654 PCI_VENDOR_ID_MAINPINE, 0x3300,
3655 0, 0, pbn_b0_2_115200 },
3656 { /* IQ Express D4 */
3657 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3658 PCI_VENDOR_ID_MAINPINE, 0x3400,
3659 0, 0, pbn_b0_4_115200 },
3660 { /* IQ Express F4 */
3661 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3662 PCI_VENDOR_ID_MAINPINE, 0x3500,
3663 0, 0, pbn_b0_4_115200 },
3664 { /* IQ Express D8 */
3665 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3666 PCI_VENDOR_ID_MAINPINE, 0x3C00,
3667 0, 0, pbn_b0_8_115200 },
3668 { /* IQ Express F8 */
3669 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3670 PCI_VENDOR_ID_MAINPINE, 0x3D00,
3671 0, 0, pbn_b0_8_115200 },
3672
3673
3674 /*
3675 * PA Semi PA6T-1682M on-chip UART
3676 */
3677 { PCI_VENDOR_ID_PASEMI, 0xa004,
3678 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3679 pbn_pasemi_1682M },
3680
3681 /*
3682 * National Instruments
3683 */
3684 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI23216,
3685 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3686 pbn_b1_16_115200 },
3687 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2328,
3688 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3689 pbn_b1_8_115200 },
3690 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324,
3691 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3692 pbn_b1_bt_4_115200 },
3693 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322,
3694 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3695 pbn_b1_bt_2_115200 },
3696 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324I,
3697 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3698 pbn_b1_bt_4_115200 },
3699 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322I,
3700 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3701 pbn_b1_bt_2_115200 },
3702 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_23216,
3703 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3704 pbn_b1_16_115200 },
3705 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2328,
3706 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3707 pbn_b1_8_115200 },
3708 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2324,
3709 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3710 pbn_b1_bt_4_115200 },
3711 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2322,
3712 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3713 pbn_b1_bt_2_115200 },
3714 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2324,
3715 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3716 pbn_b1_bt_4_115200 },
3717 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2322,
3718 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3719 pbn_b1_bt_2_115200 },
3720 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2322,
3721 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3722 pbn_ni8430_2 },
3723 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2322,
3724 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3725 pbn_ni8430_2 },
3726 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2324,
3727 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3728 pbn_ni8430_4 },
3729 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2324,
3730 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3731 pbn_ni8430_4 },
3732 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2328,
3733 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3734 pbn_ni8430_8 },
3735 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2328,
3736 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3737 pbn_ni8430_8 },
3738 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_23216,
3739 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3740 pbn_ni8430_16 },
3741 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_23216,
3742 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3743 pbn_ni8430_16 },
3744 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2322,
3745 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3746 pbn_ni8430_2 },
3747 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2322,
3748 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3749 pbn_ni8430_2 },
3750 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2324,
3751 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3752 pbn_ni8430_4 },
3753 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2324,
3754 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3755 pbn_ni8430_4 },
3756
3757 /*
3758 * ADDI-DATA GmbH communication cards <info@addi-data.com>
3759 */
3760 { PCI_VENDOR_ID_ADDIDATA,
3761 PCI_DEVICE_ID_ADDIDATA_APCI7500,
3762 PCI_ANY_ID,
3763 PCI_ANY_ID,
3764 0,
3765 0,
3766 pbn_b0_4_115200 },
3767
3768 { PCI_VENDOR_ID_ADDIDATA,
3769 PCI_DEVICE_ID_ADDIDATA_APCI7420,
3770 PCI_ANY_ID,
3771 PCI_ANY_ID,
3772 0,
3773 0,
3774 pbn_b0_2_115200 },
3775
3776 { PCI_VENDOR_ID_ADDIDATA,
3777 PCI_DEVICE_ID_ADDIDATA_APCI7300,
3778 PCI_ANY_ID,
3779 PCI_ANY_ID,
3780 0,
3781 0,
3782 pbn_b0_1_115200 },
3783
3784 { PCI_VENDOR_ID_ADDIDATA_OLD,
3785 PCI_DEVICE_ID_ADDIDATA_APCI7800,
3786 PCI_ANY_ID,
3787 PCI_ANY_ID,
3788 0,
3789 0,
3790 pbn_b1_8_115200 },
3791
3792 { PCI_VENDOR_ID_ADDIDATA,
3793 PCI_DEVICE_ID_ADDIDATA_APCI7500_2,
3794 PCI_ANY_ID,
3795 PCI_ANY_ID,
3796 0,
3797 0,
3798 pbn_b0_4_115200 },
3799
3800 { PCI_VENDOR_ID_ADDIDATA,
3801 PCI_DEVICE_ID_ADDIDATA_APCI7420_2,
3802 PCI_ANY_ID,
3803 PCI_ANY_ID,
3804 0,
3805 0,
3806 pbn_b0_2_115200 },
3807
3808 { PCI_VENDOR_ID_ADDIDATA,
3809 PCI_DEVICE_ID_ADDIDATA_APCI7300_2,
3810 PCI_ANY_ID,
3811 PCI_ANY_ID,
3812 0,
3813 0,
3814 pbn_b0_1_115200 },
3815
3816 { PCI_VENDOR_ID_ADDIDATA,
3817 PCI_DEVICE_ID_ADDIDATA_APCI7500_3,
3818 PCI_ANY_ID,
3819 PCI_ANY_ID,
3820 0,
3821 0,
3822 pbn_b0_4_115200 },
3823
3824 { PCI_VENDOR_ID_ADDIDATA,
3825 PCI_DEVICE_ID_ADDIDATA_APCI7420_3,
3826 PCI_ANY_ID,
3827 PCI_ANY_ID,
3828 0,
3829 0,
3830 pbn_b0_2_115200 },
3831
3832 { PCI_VENDOR_ID_ADDIDATA,
3833 PCI_DEVICE_ID_ADDIDATA_APCI7300_3,
3834 PCI_ANY_ID,
3835 PCI_ANY_ID,
3836 0,
3837 0,
3838 pbn_b0_1_115200 },
3839
3840 { PCI_VENDOR_ID_ADDIDATA,
3841 PCI_DEVICE_ID_ADDIDATA_APCI7800_3,
3842 PCI_ANY_ID,
3843 PCI_ANY_ID,
3844 0,
3845 0,
3846 pbn_b0_8_115200 },
3847
3848 { PCI_VENDOR_ID_ADDIDATA,
3849 PCI_DEVICE_ID_ADDIDATA_APCIe7500,
3850 PCI_ANY_ID,
3851 PCI_ANY_ID,
3852 0,
3853 0,
3854 pbn_ADDIDATA_PCIe_4_3906250 },
3855
3856 { PCI_VENDOR_ID_ADDIDATA,
3857 PCI_DEVICE_ID_ADDIDATA_APCIe7420,
3858 PCI_ANY_ID,
3859 PCI_ANY_ID,
3860 0,
3861 0,
3862 pbn_ADDIDATA_PCIe_2_3906250 },
3863
3864 { PCI_VENDOR_ID_ADDIDATA,
3865 PCI_DEVICE_ID_ADDIDATA_APCIe7300,
3866 PCI_ANY_ID,
3867 PCI_ANY_ID,
3868 0,
3869 0,
3870 pbn_ADDIDATA_PCIe_1_3906250 },
3871
3872 { PCI_VENDOR_ID_ADDIDATA,
3873 PCI_DEVICE_ID_ADDIDATA_APCIe7800,
3874 PCI_ANY_ID,
3875 PCI_ANY_ID,
3876 0,
3877 0,
3878 pbn_ADDIDATA_PCIe_8_3906250 },
3879
3880 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9835,
3881 PCI_VENDOR_ID_IBM, 0x0299,
3882 0, 0, pbn_b0_bt_2_115200 },
3883
3884 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901,
3885 0xA000, 0x1000,
3886 0, 0, pbn_b0_1_115200 },
3887
3888 /*
3889 * Best Connectivity PCI Multi I/O cards
3890 */
3891
3892 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3893 0xA000, 0x1000,
3894 0, 0, pbn_b0_1_115200 },
3895
3896 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3897 0xA000, 0x3004,
3898 0, 0, pbn_b0_bt_4_115200 },
3899 /* Intel CE4100 */
3900 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CE4100_UART,
3901 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3902 pbn_ce4100_1_115200 },
3903
3904 /*
3905 * Cronyx Omega PCI
3906 */
3907 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_CRONYX_OMEGA,
3908 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3909 pbn_omegapci },
3910
3911 /*
3912 * These entries match devices with class COMMUNICATION_SERIAL,
3913 * COMMUNICATION_MODEM or COMMUNICATION_MULTISERIAL
3914 */
3915 { PCI_ANY_ID, PCI_ANY_ID,
3916 PCI_ANY_ID, PCI_ANY_ID,
3917 PCI_CLASS_COMMUNICATION_SERIAL << 8,
3918 0xffff00, pbn_default },
3919 { PCI_ANY_ID, PCI_ANY_ID,
3920 PCI_ANY_ID, PCI_ANY_ID,
3921 PCI_CLASS_COMMUNICATION_MODEM << 8,
3922 0xffff00, pbn_default },
3923 { PCI_ANY_ID, PCI_ANY_ID,
3924 PCI_ANY_ID, PCI_ANY_ID,
3925 PCI_CLASS_COMMUNICATION_MULTISERIAL << 8,
3926 0xffff00, pbn_default },
3927 { 0, }
3928};
3929
3930static struct pci_driver serial_pci_driver = {
3931 .name = "serial",
3932 .probe = pciserial_init_one,
3933 .remove = __devexit_p(pciserial_remove_one),
3934#ifdef CONFIG_PM
3935 .suspend = pciserial_suspend_one,
3936 .resume = pciserial_resume_one,
3937#endif
3938 .id_table = serial_pci_tbl,
3939};
3940
3941static int __init serial8250_pci_init(void)
3942{
3943 return pci_register_driver(&serial_pci_driver);
3944}
3945
3946static void __exit serial8250_pci_exit(void)
3947{
3948 pci_unregister_driver(&serial_pci_driver);
3949}
3950
3951module_init(serial8250_pci_init);
3952module_exit(serial8250_pci_exit);
3953
3954MODULE_LICENSE("GPL");
3955MODULE_DESCRIPTION("Generic 8250/16x50 PCI serial probe module");
3956MODULE_DEVICE_TABLE(pci, serial_pci_tbl);
diff --git a/drivers/tty/serial/8250_pnp.c b/drivers/tty/serial/8250_pnp.c
new file mode 100644
index 000000000000..fc301f6722e1
--- /dev/null
+++ b/drivers/tty/serial/8250_pnp.c
@@ -0,0 +1,521 @@
1/*
2 * Probe module for 8250/16550-type ISAPNP serial ports.
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King, All Rights Reserved.
7 *
8 * Ported to the Linux PnP Layer - (C) Adam Belay.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License.
13 */
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/pci.h>
17#include <linux/pnp.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/serial_core.h>
21#include <linux/bitops.h>
22
23#include <asm/byteorder.h>
24
25#include "8250.h"
26
27#define UNKNOWN_DEV 0x3000
28
29
30static const struct pnp_device_id pnp_dev_table[] = {
31 /* Archtek America Corp. */
32 /* Archtek SmartLink Modem 3334BT Plug & Play */
33 { "AAC000F", 0 },
34 /* Anchor Datacomm BV */
35 /* SXPro 144 External Data Fax Modem Plug & Play */
36 { "ADC0001", 0 },
37 /* SXPro 288 External Data Fax Modem Plug & Play */
38 { "ADC0002", 0 },
39 /* PROLiNK 1456VH ISA PnP K56flex Fax Modem */
40 { "AEI0250", 0 },
41 /* Actiontec ISA PNP 56K X2 Fax Modem */
42 { "AEI1240", 0 },
43 /* Rockwell 56K ACF II Fax+Data+Voice Modem */
44 { "AKY1021", 0 /*SPCI_FL_NO_SHIRQ*/ },
45 /* AZT3005 PnP SOUND DEVICE */
46 { "AZT4001", 0 },
47 /* Best Data Products Inc. Smart One 336F PnP Modem */
48 { "BDP3336", 0 },
49 /* Boca Research */
50 /* Boca Complete Ofc Communicator 14.4 Data-FAX */
51 { "BRI0A49", 0 },
52 /* Boca Research 33,600 ACF Modem */
53 { "BRI1400", 0 },
54 /* Boca 33.6 Kbps Internal FD34FSVD */
55 { "BRI3400", 0 },
56 /* Boca 33.6 Kbps Internal FD34FSVD */
57 { "BRI0A49", 0 },
58 /* Best Data Products Inc. Smart One 336F PnP Modem */
59 { "BDP3336", 0 },
60 /* Computer Peripherals Inc */
61 /* EuroViVa CommCenter-33.6 SP PnP */
62 { "CPI4050", 0 },
63 /* Creative Labs */
64 /* Creative Labs Phone Blaster 28.8 DSVD PnP Voice */
65 { "CTL3001", 0 },
66 /* Creative Labs Modem Blaster 28.8 DSVD PnP Voice */
67 { "CTL3011", 0 },
68 /* Davicom ISA 33.6K Modem */
69 { "DAV0336", 0 },
70 /* Creative */
71 /* Creative Modem Blaster Flash56 DI5601-1 */
72 { "DMB1032", 0 },
73 /* Creative Modem Blaster V.90 DI5660 */
74 { "DMB2001", 0 },
75 /* E-Tech */
76 /* E-Tech CyberBULLET PC56RVP */
77 { "ETT0002", 0 },
78 /* FUJITSU */
79 /* Fujitsu 33600 PnP-I2 R Plug & Play */
80 { "FUJ0202", 0 },
81 /* Fujitsu FMV-FX431 Plug & Play */
82 { "FUJ0205", 0 },
83 /* Fujitsu 33600 PnP-I4 R Plug & Play */
84 { "FUJ0206", 0 },
85 /* Fujitsu Fax Voice 33600 PNP-I5 R Plug & Play */
86 { "FUJ0209", 0 },
87 /* Archtek America Corp. */
88 /* Archtek SmartLink Modem 3334BT Plug & Play */
89 { "GVC000F", 0 },
90 /* Archtek SmartLink Modem 3334BRV 33.6K Data Fax Voice */
91 { "GVC0303", 0 },
92 /* Hayes */
93 /* Hayes Optima 288 V.34-V.FC + FAX + Voice Plug & Play */
94 { "HAY0001", 0 },
95 /* Hayes Optima 336 V.34 + FAX + Voice PnP */
96 { "HAY000C", 0 },
97 /* Hayes Optima 336B V.34 + FAX + Voice PnP */
98 { "HAY000D", 0 },
99 /* Hayes Accura 56K Ext Fax Modem PnP */
100 { "HAY5670", 0 },
101 /* Hayes Accura 56K Ext Fax Modem PnP */
102 { "HAY5674", 0 },
103 /* Hayes Accura 56K Fax Modem PnP */
104 { "HAY5675", 0 },
105 /* Hayes 288, V.34 + FAX */
106 { "HAYF000", 0 },
107 /* Hayes Optima 288 V.34 + FAX + Voice, Plug & Play */
108 { "HAYF001", 0 },
109 /* IBM */
110 /* IBM Thinkpad 701 Internal Modem Voice */
111 { "IBM0033", 0 },
112 /* Intertex */
113 /* Intertex 28k8 33k6 Voice EXT PnP */
114 { "IXDC801", 0 },
115 /* Intertex 33k6 56k Voice EXT PnP */
116 { "IXDC901", 0 },
117 /* Intertex 28k8 33k6 Voice SP EXT PnP */
118 { "IXDD801", 0 },
119 /* Intertex 33k6 56k Voice SP EXT PnP */
120 { "IXDD901", 0 },
121 /* Intertex 28k8 33k6 Voice SP INT PnP */
122 { "IXDF401", 0 },
123 /* Intertex 28k8 33k6 Voice SP EXT PnP */
124 { "IXDF801", 0 },
125 /* Intertex 33k6 56k Voice SP EXT PnP */
126 { "IXDF901", 0 },
127 /* Kortex International */
128 /* KORTEX 28800 Externe PnP */
129 { "KOR4522", 0 },
130 /* KXPro 33.6 Vocal ASVD PnP */
131 { "KORF661", 0 },
132 /* Lasat */
133 /* LASAT Internet 33600 PnP */
134 { "LAS4040", 0 },
135 /* Lasat Safire 560 PnP */
136 { "LAS4540", 0 },
137 /* Lasat Safire 336 PnP */
138 { "LAS5440", 0 },
139 /* Microcom, Inc. */
140 /* Microcom TravelPorte FAST V.34 Plug & Play */
141 { "MNP0281", 0 },
142 /* Microcom DeskPorte V.34 FAST or FAST+ Plug & Play */
143 { "MNP0336", 0 },
144 /* Microcom DeskPorte FAST EP 28.8 Plug & Play */
145 { "MNP0339", 0 },
146 /* Microcom DeskPorte 28.8P Plug & Play */
147 { "MNP0342", 0 },
148 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
149 { "MNP0500", 0 },
150 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
151 { "MNP0501", 0 },
152 /* Microcom DeskPorte 28.8S Internal Plug & Play */
153 { "MNP0502", 0 },
154 /* Motorola */
155 /* Motorola BitSURFR Plug & Play */
156 { "MOT1105", 0 },
157 /* Motorola TA210 Plug & Play */
158 { "MOT1111", 0 },
159 /* Motorola HMTA 200 (ISDN) Plug & Play */
160 { "MOT1114", 0 },
161 /* Motorola BitSURFR Plug & Play */
162 { "MOT1115", 0 },
163 /* Motorola Lifestyle 28.8 Internal */
164 { "MOT1190", 0 },
165 /* Motorola V.3400 Plug & Play */
166 { "MOT1501", 0 },
167 /* Motorola Lifestyle 28.8 V.34 Plug & Play */
168 { "MOT1502", 0 },
169 /* Motorola Power 28.8 V.34 Plug & Play */
170 { "MOT1505", 0 },
171 /* Motorola ModemSURFR External 28.8 Plug & Play */
172 { "MOT1509", 0 },
173 /* Motorola Premier 33.6 Desktop Plug & Play */
174 { "MOT150A", 0 },
175 /* Motorola VoiceSURFR 56K External PnP */
176 { "MOT150F", 0 },
177 /* Motorola ModemSURFR 56K External PnP */
178 { "MOT1510", 0 },
179 /* Motorola ModemSURFR 56K Internal PnP */
180 { "MOT1550", 0 },
181 /* Motorola ModemSURFR Internal 28.8 Plug & Play */
182 { "MOT1560", 0 },
183 /* Motorola Premier 33.6 Internal Plug & Play */
184 { "MOT1580", 0 },
185 /* Motorola OnlineSURFR 28.8 Internal Plug & Play */
186 { "MOT15B0", 0 },
187 /* Motorola VoiceSURFR 56K Internal PnP */
188 { "MOT15F0", 0 },
189 /* Com 1 */
190 /* Deskline K56 Phone System PnP */
191 { "MVX00A1", 0 },
192 /* PC Rider K56 Phone System PnP */
193 { "MVX00F2", 0 },
194 /* NEC 98NOTE SPEAKER PHONE FAX MODEM(33600bps) */
195 { "nEC8241", 0 },
196 /* Pace 56 Voice Internal Plug & Play Modem */
197 { "PMC2430", 0 },
198 /* Generic */
199 /* Generic standard PC COM port */
200 { "PNP0500", 0 },
201 /* Generic 16550A-compatible COM port */
202 { "PNP0501", 0 },
203 /* Compaq 14400 Modem */
204 { "PNPC000", 0 },
205 /* Compaq 2400/9600 Modem */
206 { "PNPC001", 0 },
207 /* Dial-Up Networking Serial Cable between 2 PCs */
208 { "PNPC031", 0 },
209 /* Dial-Up Networking Parallel Cable between 2 PCs */
210 { "PNPC032", 0 },
211 /* Standard 9600 bps Modem */
212 { "PNPC100", 0 },
213 /* Standard 14400 bps Modem */
214 { "PNPC101", 0 },
215 /* Standard 28800 bps Modem*/
216 { "PNPC102", 0 },
217 /* Standard Modem*/
218 { "PNPC103", 0 },
219 /* Standard 9600 bps Modem*/
220 { "PNPC104", 0 },
221 /* Standard 14400 bps Modem*/
222 { "PNPC105", 0 },
223 /* Standard 28800 bps Modem*/
224 { "PNPC106", 0 },
225 /* Standard Modem */
226 { "PNPC107", 0 },
227 /* Standard 9600 bps Modem */
228 { "PNPC108", 0 },
229 /* Standard 14400 bps Modem */
230 { "PNPC109", 0 },
231 /* Standard 28800 bps Modem */
232 { "PNPC10A", 0 },
233 /* Standard Modem */
234 { "PNPC10B", 0 },
235 /* Standard 9600 bps Modem */
236 { "PNPC10C", 0 },
237 /* Standard 14400 bps Modem */
238 { "PNPC10D", 0 },
239 /* Standard 28800 bps Modem */
240 { "PNPC10E", 0 },
241 /* Standard Modem */
242 { "PNPC10F", 0 },
243 /* Standard PCMCIA Card Modem */
244 { "PNP2000", 0 },
245 /* Rockwell */
246 /* Modular Technology */
247 /* Rockwell 33.6 DPF Internal PnP */
248 /* Modular Technology 33.6 Internal PnP */
249 { "ROK0030", 0 },
250 /* Kortex International */
251 /* KORTEX 14400 Externe PnP */
252 { "ROK0100", 0 },
253 /* Rockwell 28.8 */
254 { "ROK4120", 0 },
255 /* Viking Components, Inc */
256 /* Viking 28.8 INTERNAL Fax+Data+Voice PnP */
257 { "ROK4920", 0 },
258 /* Rockwell */
259 /* British Telecom */
260 /* Modular Technology */
261 /* Rockwell 33.6 DPF External PnP */
262 /* BT Prologue 33.6 External PnP */
263 /* Modular Technology 33.6 External PnP */
264 { "RSS00A0", 0 },
265 /* Viking 56K FAX INT */
266 { "RSS0262", 0 },
267 /* K56 par,VV,Voice,Speakphone,AudioSpan,PnP */
268 { "RSS0250", 0 },
269 /* SupraExpress 28.8 Data/Fax PnP modem */
270 { "SUP1310", 0 },
271 /* SupraExpress 336i PnP Voice Modem */
272 { "SUP1381", 0 },
273 /* SupraExpress 33.6 Data/Fax PnP modem */
274 { "SUP1421", 0 },
275 /* SupraExpress 33.6 Data/Fax PnP modem */
276 { "SUP1590", 0 },
277 /* SupraExpress 336i Sp ASVD */
278 { "SUP1620", 0 },
279 /* SupraExpress 33.6 Data/Fax PnP modem */
280 { "SUP1760", 0 },
281 /* SupraExpress 56i Sp Intl */
282 { "SUP2171", 0 },
283 /* Phoebe Micro */
284 /* Phoebe Micro 33.6 Data Fax 1433VQH Plug & Play */
285 { "TEX0011", 0 },
286 /* Archtek America Corp. */
287 /* Archtek SmartLink Modem 3334BT Plug & Play */
288 { "UAC000F", 0 },
289 /* 3Com Corp. */
290 /* Gateway Telepath IIvi 33.6 */
291 { "USR0000", 0 },
292 /* U.S. Robotics Sporster 33.6K Fax INT PnP */
293 { "USR0002", 0 },
294 /* Sportster Vi 14.4 PnP FAX Voicemail */
295 { "USR0004", 0 },
296 /* U.S. Robotics 33.6K Voice INT PnP */
297 { "USR0006", 0 },
298 /* U.S. Robotics 33.6K Voice EXT PnP */
299 { "USR0007", 0 },
300 /* U.S. Robotics Courier V.Everything INT PnP */
301 { "USR0009", 0 },
302 /* U.S. Robotics 33.6K Voice INT PnP */
303 { "USR2002", 0 },
304 /* U.S. Robotics 56K Voice INT PnP */
305 { "USR2070", 0 },
306 /* U.S. Robotics 56K Voice EXT PnP */
307 { "USR2080", 0 },
308 /* U.S. Robotics 56K FAX INT */
309 { "USR3031", 0 },
310 /* U.S. Robotics 56K FAX INT */
311 { "USR3050", 0 },
312 /* U.S. Robotics 56K Voice INT PnP */
313 { "USR3070", 0 },
314 /* U.S. Robotics 56K Voice EXT PnP */
315 { "USR3080", 0 },
316 /* U.S. Robotics 56K Voice INT PnP */
317 { "USR3090", 0 },
318 /* U.S. Robotics 56K Message */
319 { "USR9100", 0 },
320 /* U.S. Robotics 56K FAX EXT PnP*/
321 { "USR9160", 0 },
322 /* U.S. Robotics 56K FAX INT PnP*/
323 { "USR9170", 0 },
324 /* U.S. Robotics 56K Voice EXT PnP*/
325 { "USR9180", 0 },
326 /* U.S. Robotics 56K Voice INT PnP*/
327 { "USR9190", 0 },
328 /* Wacom tablets */
329 { "WACFXXX", 0 },
330 /* Compaq touchscreen */
331 { "FPI2002", 0 },
332 /* Fujitsu Stylistic touchscreens */
333 { "FUJ02B2", 0 },
334 { "FUJ02B3", 0 },
335 /* Fujitsu Stylistic LT touchscreens */
336 { "FUJ02B4", 0 },
337 /* Passive Fujitsu Stylistic touchscreens */
338 { "FUJ02B6", 0 },
339 { "FUJ02B7", 0 },
340 { "FUJ02B8", 0 },
341 { "FUJ02B9", 0 },
342 { "FUJ02BC", 0 },
343 /* Fujitsu Wacom Tablet PC device */
344 { "FUJ02E5", 0 },
345 /* Fujitsu P-series tablet PC device */
346 { "FUJ02E6", 0 },
347 /* Fujitsu Wacom 2FGT Tablet PC device */
348 { "FUJ02E7", 0 },
349 /* Fujitsu Wacom 1FGT Tablet PC device */
350 { "FUJ02E9", 0 },
351 /*
352 * LG C1 EXPRESS DUAL (C1-PB11A3) touch screen (actually a FUJ02E6 in
353 * disguise)
354 */
355 { "LTS0001", 0 },
356 /* Rockwell's (PORALiNK) 33600 INT PNP */
357 { "WCI0003", 0 },
358 /* Unknown PnP modems */
359 { "PNPCXXX", UNKNOWN_DEV },
360 /* More unknown PnP modems */
361 { "PNPDXXX", UNKNOWN_DEV },
362 { "", 0 }
363};
364
365MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
366
367static char *modem_names[] __devinitdata = {
368 "MODEM", "Modem", "modem", "FAX", "Fax", "fax",
369 "56K", "56k", "K56", "33.6", "28.8", "14.4",
370 "33,600", "28,800", "14,400", "33.600", "28.800", "14.400",
371 "33600", "28800", "14400", "V.90", "V.34", "V.32", NULL
372};
373
374static int __devinit check_name(char *name)
375{
376 char **tmp;
377
378 for (tmp = modem_names; *tmp; tmp++)
379 if (strstr(name, *tmp))
380 return 1;
381
382 return 0;
383}
384
385static int __devinit check_resources(struct pnp_dev *dev)
386{
387 resource_size_t base[] = {0x2f8, 0x3f8, 0x2e8, 0x3e8};
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(base); i++) {
391 if (pnp_possible_config(dev, IORESOURCE_IO, base[i], 8))
392 return 1;
393 }
394
395 return 0;
396}
397
398/*
399 * Given a complete unknown PnP device, try to use some heuristics to
400 * detect modems. Currently use such heuristic set:
401 * - dev->name or dev->bus->name must contain "modem" substring;
402 * - device must have only one IO region (8 byte long) with base address
403 * 0x2e8, 0x3e8, 0x2f8 or 0x3f8.
404 *
405 * Such detection looks very ugly, but can detect at least some of numerous
406 * PnP modems, alternatively we must hardcode all modems in pnp_devices[]
407 * table.
408 */
409static int __devinit serial_pnp_guess_board(struct pnp_dev *dev, int *flags)
410{
411 if (!(check_name(pnp_dev_name(dev)) ||
412 (dev->card && check_name(dev->card->name))))
413 return -ENODEV;
414
415 if (check_resources(dev))
416 return 0;
417
418 return -ENODEV;
419}
420
421static int __devinit
422serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
423{
424 struct uart_port port;
425 int ret, line, flags = dev_id->driver_data;
426
427 if (flags & UNKNOWN_DEV) {
428 ret = serial_pnp_guess_board(dev, &flags);
429 if (ret < 0)
430 return ret;
431 }
432
433 memset(&port, 0, sizeof(struct uart_port));
434 if (pnp_irq_valid(dev, 0))
435 port.irq = pnp_irq(dev, 0);
436 if (pnp_port_valid(dev, 0)) {
437 port.iobase = pnp_port_start(dev, 0);
438 port.iotype = UPIO_PORT;
439 } else if (pnp_mem_valid(dev, 0)) {
440 port.mapbase = pnp_mem_start(dev, 0);
441 port.iotype = UPIO_MEM;
442 port.flags = UPF_IOREMAP;
443 } else
444 return -ENODEV;
445
446#ifdef SERIAL_DEBUG_PNP
447 printk(KERN_DEBUG
448 "Setup PNP port: port %x, mem 0x%lx, irq %d, type %d\n",
449 port.iobase, port.mapbase, port.irq, port.iotype);
450#endif
451
452 port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
453 if (pnp_irq_flags(dev, 0) & IORESOURCE_IRQ_SHAREABLE)
454 port.flags |= UPF_SHARE_IRQ;
455 port.uartclk = 1843200;
456 port.dev = &dev->dev;
457
458 line = serial8250_register_port(&port);
459 if (line < 0)
460 return -ENODEV;
461
462 pnp_set_drvdata(dev, (void *)((long)line + 1));
463 return 0;
464}
465
466static void __devexit serial_pnp_remove(struct pnp_dev *dev)
467{
468 long line = (long)pnp_get_drvdata(dev);
469 if (line)
470 serial8250_unregister_port(line - 1);
471}
472
473#ifdef CONFIG_PM
474static int serial_pnp_suspend(struct pnp_dev *dev, pm_message_t state)
475{
476 long line = (long)pnp_get_drvdata(dev);
477
478 if (!line)
479 return -ENODEV;
480 serial8250_suspend_port(line - 1);
481 return 0;
482}
483
484static int serial_pnp_resume(struct pnp_dev *dev)
485{
486 long line = (long)pnp_get_drvdata(dev);
487
488 if (!line)
489 return -ENODEV;
490 serial8250_resume_port(line - 1);
491 return 0;
492}
493#else
494#define serial_pnp_suspend NULL
495#define serial_pnp_resume NULL
496#endif /* CONFIG_PM */
497
498static struct pnp_driver serial_pnp_driver = {
499 .name = "serial",
500 .probe = serial_pnp_probe,
501 .remove = __devexit_p(serial_pnp_remove),
502 .suspend = serial_pnp_suspend,
503 .resume = serial_pnp_resume,
504 .id_table = pnp_dev_table,
505};
506
507static int __init serial8250_pnp_init(void)
508{
509 return pnp_register_driver(&serial_pnp_driver);
510}
511
512static void __exit serial8250_pnp_exit(void)
513{
514 pnp_unregister_driver(&serial_pnp_driver);
515}
516
517module_init(serial8250_pnp_init);
518module_exit(serial8250_pnp_exit);
519
520MODULE_LICENSE("GPL");
521MODULE_DESCRIPTION("Generic 8250/16x50 PnP serial driver");
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
new file mode 100644
index 000000000000..636144cea932
--- /dev/null
+++ b/drivers/tty/serial/Kconfig
@@ -0,0 +1,1638 @@
1#
2# Serial device configuration
3#
4
5menu "Serial drivers"
6 depends on HAS_IOMEM
7
8#
9# The new 8250/16550 serial drivers
10config SERIAL_8250
11 tristate "8250/16550 and compatible serial support"
12 select SERIAL_CORE
13 ---help---
14 This selects whether you want to include the driver for the standard
15 serial ports. The standard answer is Y. People who might say N
16 here are those that are setting up dedicated Ethernet WWW/FTP
17 servers, or users that have one of the various bus mice instead of a
18 serial mouse and don't intend to use their machine's standard serial
19 port for anything. (Note that the Cyclades and Stallion multi
20 serial port drivers do not need this driver built in for them to
21 work.)
22
23 To compile this driver as a module, choose M here: the
24 module will be called 8250.
25 [WARNING: Do not compile this driver as a module if you are using
26 non-standard serial ports, since the configuration information will
27 be lost when the driver is unloaded. This limitation may be lifted
28 in the future.]
29
30 BTW1: If you have a mouseman serial mouse which is not recognized by
31 the X window system, try running gpm first.
32
33 BTW2: If you intend to use a software modem (also called Winmodem)
34 under Linux, forget it. These modems are crippled and require
35 proprietary drivers which are only available under Windows.
36
37 Most people will say Y or M here, so that they can use serial mice,
38 modems and similar devices connecting to the standard serial ports.
39
40config SERIAL_8250_CONSOLE
41 bool "Console on 8250/16550 and compatible serial port"
42 depends on SERIAL_8250=y
43 select SERIAL_CORE_CONSOLE
44 ---help---
45 If you say Y here, it will be possible to use a serial port as the
46 system console (the system console is the device which receives all
47 kernel messages and warnings and which allows logins in single user
48 mode). This could be useful if some terminal or printer is connected
49 to that serial port.
50
51 Even if you say Y here, the currently visible virtual console
52 (/dev/tty0) will still be used as the system console by default, but
53 you can alter that using a kernel command line option such as
54 "console=ttyS1". (Try "man bootparam" or see the documentation of
55 your boot loader (grub or lilo or loadlin) about how to pass options
56 to the kernel at boot time.)
57
58 If you don't have a VGA card installed and you say Y here, the
59 kernel will automatically use the first serial line, /dev/ttyS0, as
60 system console.
61
62 You can set that using a kernel command line option such as
63 "console=uart8250,io,0x3f8,9600n8"
64 "console=uart8250,mmio,0xff5e0000,115200n8".
65 and it will switch to normal serial console when the corresponding
66 port is ready.
67 "earlycon=uart8250,io,0x3f8,9600n8"
68 "earlycon=uart8250,mmio,0xff5e0000,115200n8".
69 it will not only setup early console.
70
71 If unsure, say N.
72
73config FIX_EARLYCON_MEM
74 bool
75 depends on X86
76 default y
77
78config SERIAL_8250_GSC
79 tristate
80 depends on SERIAL_8250 && GSC
81 default SERIAL_8250
82
83config SERIAL_8250_PCI
84 tristate "8250/16550 PCI device support" if EXPERT
85 depends on SERIAL_8250 && PCI
86 default SERIAL_8250
87 help
88 This builds standard PCI serial support. You may be able to
89 disable this feature if you only need legacy serial support.
90 Saves about 9K.
91
92config SERIAL_8250_PNP
93 tristate "8250/16550 PNP device support" if EXPERT
94 depends on SERIAL_8250 && PNP
95 default SERIAL_8250
96 help
97 This builds standard PNP serial support. You may be able to
98 disable this feature if you only need legacy serial support.
99
100config SERIAL_8250_HP300
101 tristate
102 depends on SERIAL_8250 && HP300
103 default SERIAL_8250
104
105config SERIAL_8250_CS
106 tristate "8250/16550 PCMCIA device support"
107 depends on PCMCIA && SERIAL_8250
108 ---help---
109 Say Y here to enable support for 16-bit PCMCIA serial devices,
110 including serial port cards, modems, and the modem functions of
111 multi-function Ethernet/modem cards. (PCMCIA- or PC-cards are
112 credit-card size devices often used with laptops.)
113
114 To compile this driver as a module, choose M here: the
115 module will be called serial_cs.
116
117 If unsure, say N.
118
119config SERIAL_8250_NR_UARTS
120 int "Maximum number of 8250/16550 serial ports"
121 depends on SERIAL_8250
122 default "4"
123 help
124 Set this to the number of serial ports you want the driver
125 to support. This includes any ports discovered via ACPI or
126 PCI enumeration and any ports that may be added at run-time
127 via hot-plug, or any ISA multi-port serial cards.
128
129config SERIAL_8250_RUNTIME_UARTS
130 int "Number of 8250/16550 serial ports to register at runtime"
131 depends on SERIAL_8250
132 range 0 SERIAL_8250_NR_UARTS
133 default "4"
134 help
135 Set this to the maximum number of serial ports you want
136 the kernel to register at boot time. This can be overridden
137 with the module parameter "nr_uarts", or boot-time parameter
138 8250.nr_uarts
139
140config SERIAL_8250_EXTENDED
141 bool "Extended 8250/16550 serial driver options"
142 depends on SERIAL_8250
143 help
144 If you wish to use any non-standard features of the standard "dumb"
145 driver, say Y here. This includes HUB6 support, shared serial
146 interrupts, special multiport support, support for more than the
147 four COM 1/2/3/4 boards, etc.
148
149 Note that the answer to this question won't directly affect the
150 kernel: saying N will just cause the configurator to skip all
151 the questions about serial driver options. If unsure, say N.
152
153config SERIAL_8250_MANY_PORTS
154 bool "Support more than 4 legacy serial ports"
155 depends on SERIAL_8250_EXTENDED && !IA64
156 help
157 Say Y here if you have dumb serial boards other than the four
158 standard COM 1/2/3/4 ports. This may happen if you have an AST
159 FourPort, Accent Async, Boca (read the Boca mini-HOWTO, available
160 from <http://www.tldp.org/docs.html#howto>), or other custom
161 serial port hardware which acts similar to standard serial port
162 hardware. If you only use the standard COM 1/2/3/4 ports, you can
163 say N here to save some memory. You can also say Y if you have an
164 "intelligent" multiport card such as Cyclades, Digiboards, etc.
165
166#
167# Multi-port serial cards
168#
169
170config SERIAL_8250_FOURPORT
171 tristate "Support Fourport cards"
172 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
173 help
174 Say Y here if you have an AST FourPort serial board.
175
176 To compile this driver as a module, choose M here: the module
177 will be called 8250_fourport.
178
179config SERIAL_8250_ACCENT
180 tristate "Support Accent cards"
181 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
182 help
183 Say Y here if you have an Accent Async serial board.
184
185 To compile this driver as a module, choose M here: the module
186 will be called 8250_accent.
187
188config SERIAL_8250_BOCA
189 tristate "Support Boca cards"
190 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
191 help
192 Say Y here if you have a Boca serial board. Please read the Boca
193 mini-HOWTO, available from <http://www.tldp.org/docs.html#howto>
194
195 To compile this driver as a module, choose M here: the module
196 will be called 8250_boca.
197
198config SERIAL_8250_EXAR_ST16C554
199 tristate "Support Exar ST16C554/554D Quad UART"
200 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
201 help
202 The Uplogix Envoy TU301 uses this Exar Quad UART. If you are
203 tinkering with your Envoy TU301, or have a machine with this UART,
204 say Y here.
205
206 To compile this driver as a module, choose M here: the module
207 will be called 8250_exar_st16c554.
208
209config SERIAL_8250_HUB6
210 tristate "Support Hub6 cards"
211 depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS
212 help
213 Say Y here if you have a HUB6 serial board.
214
215 To compile this driver as a module, choose M here: the module
216 will be called 8250_hub6.
217
218config SERIAL_8250_SHARE_IRQ
219 bool "Support for sharing serial interrupts"
220 depends on SERIAL_8250_EXTENDED
221 help
222 Some serial boards have hardware support which allows multiple dumb
223 serial ports on the same board to share a single IRQ. To enable
224 support for this in the serial driver, say Y here.
225
226config SERIAL_8250_DETECT_IRQ
227 bool "Autodetect IRQ on standard ports (unsafe)"
228 depends on SERIAL_8250_EXTENDED
229 help
230 Say Y here if you want the kernel to try to guess which IRQ
231 to use for your serial port.
232
233 This is considered unsafe; it is far better to configure the IRQ in
234 a boot script using the setserial command.
235
236 If unsure, say N.
237
238config SERIAL_8250_RSA
239 bool "Support RSA serial ports"
240 depends on SERIAL_8250_EXTENDED
241 help
242 ::: To be written :::
243
244config SERIAL_8250_MCA
245 tristate "Support 8250-type ports on MCA buses"
246 depends on SERIAL_8250 != n && MCA
247 help
248 Say Y here if you have a MCA serial ports.
249
250 To compile this driver as a module, choose M here: the module
251 will be called 8250_mca.
252
253config SERIAL_8250_ACORN
254 tristate "Acorn expansion card serial port support"
255 depends on ARCH_ACORN && SERIAL_8250
256 help
257 If you have an Atomwide Serial card or Serial Port card for an Acorn
258 system, say Y to this option. The driver can handle 1, 2, or 3 port
259 cards. If unsure, say N.
260
261config SERIAL_8250_RM9K
262 bool "Support for MIPS RM9xxx integrated serial port"
263 depends on SERIAL_8250 != n && SERIAL_RM9000
264 select SERIAL_8250_SHARE_IRQ
265 help
266 Selecting this option will add support for the integrated serial
267 port hardware found on MIPS RM9122 and similar processors.
268 If unsure, say N.
269
270comment "Non-8250 serial port support"
271
272config SERIAL_AMBA_PL010
273 tristate "ARM AMBA PL010 serial port support"
274 depends on ARM_AMBA && (BROKEN || !ARCH_VERSATILE)
275 select SERIAL_CORE
276 help
277 This selects the ARM(R) AMBA(R) PrimeCell PL010 UART. If you have
278 an Integrator/AP or Integrator/PP2 platform, or if you have a
279 Cirrus Logic EP93xx CPU, say Y or M here.
280
281 If unsure, say N.
282
283config SERIAL_AMBA_PL010_CONSOLE
284 bool "Support for console on AMBA serial port"
285 depends on SERIAL_AMBA_PL010=y
286 select SERIAL_CORE_CONSOLE
287 ---help---
288 Say Y here if you wish to use an AMBA PrimeCell UART as the system
289 console (the system console is the device which receives all kernel
290 messages and warnings and which allows logins in single user mode).
291
292 Even if you say Y here, the currently visible framebuffer console
293 (/dev/tty0) will still be used as the system console by default, but
294 you can alter that using a kernel command line option such as
295 "console=ttyAM0". (Try "man bootparam" or see the documentation of
296 your boot loader (lilo or loadlin) about how to pass options to the
297 kernel at boot time.)
298
299config SERIAL_AMBA_PL011
300 tristate "ARM AMBA PL011 serial port support"
301 depends on ARM_AMBA
302 select SERIAL_CORE
303 help
304 This selects the ARM(R) AMBA(R) PrimeCell PL011 UART. If you have
305 an Integrator/PP2, Integrator/CP or Versatile platform, say Y or M
306 here.
307
308 If unsure, say N.
309
310config SERIAL_AMBA_PL011_CONSOLE
311 bool "Support for console on AMBA serial port"
312 depends on SERIAL_AMBA_PL011=y
313 select SERIAL_CORE_CONSOLE
314 ---help---
315 Say Y here if you wish to use an AMBA PrimeCell UART as the system
316 console (the system console is the device which receives all kernel
317 messages and warnings and which allows logins in single user mode).
318
319 Even if you say Y here, the currently visible framebuffer console
320 (/dev/tty0) will still be used as the system console by default, but
321 you can alter that using a kernel command line option such as
322 "console=ttyAMA0". (Try "man bootparam" or see the documentation of
323 your boot loader (lilo or loadlin) about how to pass options to the
324 kernel at boot time.)
325
326config SERIAL_SB1250_DUART
327 tristate "BCM1xxx on-chip DUART serial support"
328 depends on SIBYTE_SB1xxx_SOC=y
329 select SERIAL_CORE
330 default y
331 ---help---
332 Support for the asynchronous serial interface (DUART) included in
333 the BCM1250 and derived System-On-a-Chip (SOC) devices. Note that
334 the letter D in DUART stands for "dual", which is how the device
335 is implemented. Depending on the SOC configuration there may be
336 one or more DUARTs available of which all are handled.
337
338 If unsure, say Y. To compile this driver as a module, choose M here:
339 the module will be called sb1250-duart.
340
341config SERIAL_SB1250_DUART_CONSOLE
342 bool "Support for console on a BCM1xxx DUART serial port"
343 depends on SERIAL_SB1250_DUART=y
344 select SERIAL_CORE_CONSOLE
345 default y
346 ---help---
347 If you say Y here, it will be possible to use a serial port as the
348 system console (the system console is the device which receives all
349 kernel messages and warnings and which allows logins in single user
350 mode).
351
352 If unsure, say Y.
353
354config SERIAL_ATMEL
355 bool "AT91 / AT32 on-chip serial port support"
356 depends on (ARM && ARCH_AT91) || AVR32
357 select SERIAL_CORE
358 help
359 This enables the driver for the on-chip UARTs of the Atmel
360 AT91 and AT32 processors.
361
362config SERIAL_ATMEL_CONSOLE
363 bool "Support for console on AT91 / AT32 serial port"
364 depends on SERIAL_ATMEL=y
365 select SERIAL_CORE_CONSOLE
366 help
367 Say Y here if you wish to use an on-chip UART on a Atmel
368 AT91 or AT32 processor as the system console (the system
369 console is the device which receives all kernel messages and
370 warnings and which allows logins in single user mode).
371
372config SERIAL_ATMEL_PDC
373 bool "Support DMA transfers on AT91 / AT32 serial port"
374 depends on SERIAL_ATMEL
375 default y
376 help
377 Say Y here if you wish to use the PDC to do DMA transfers to
378 and from the Atmel AT91 / AT32 serial port. In order to
379 actually use DMA transfers, make sure that the use_dma_tx
380 and use_dma_rx members in the atmel_uart_data struct is set
381 appropriately for each port.
382
383 Note that break and error handling currently doesn't work
384 properly when DMA is enabled. Make sure that ports where
385 this matters don't use DMA.
386
387config SERIAL_ATMEL_TTYAT
388 bool "Install as device ttyATn instead of ttySn"
389 depends on SERIAL_ATMEL=y
390 help
391 Say Y here if you wish to have the internal AT91 / AT32 UARTs
392 appear as /dev/ttyATn (major 204, minor starting at 154)
393 instead of the normal /dev/ttySn (major 4, minor starting at
394 64). This is necessary if you also want other UARTs, such as
395 external 8250/16C550 compatible UARTs.
396 The ttySn nodes are legally reserved for the 8250 serial driver
397 but are often misused by other serial drivers.
398
399 To use this, you should create suitable ttyATn device nodes in
400 /dev/, and pass "console=ttyATn" to the kernel.
401
402 Say Y if you have an external 8250/16C550 UART. If unsure, say N.
403
404config SERIAL_KS8695
405 bool "Micrel KS8695 (Centaur) serial port support"
406 depends on ARCH_KS8695
407 select SERIAL_CORE
408 help
409 This selects the Micrel Centaur KS8695 UART. Say Y here.
410
411config SERIAL_KS8695_CONSOLE
412 bool "Support for console on KS8695 (Centaur) serial port"
413 depends on SERIAL_KS8695=y
414 select SERIAL_CORE_CONSOLE
415 help
416 Say Y here if you wish to use a KS8695 (Centaur) UART as the
417 system console (the system console is the device which
418 receives all kernel messages and warnings and which allows
419 logins in single user mode).
420
421config SERIAL_CLPS711X
422 tristate "CLPS711X serial port support"
423 depends on ARM && ARCH_CLPS711X
424 select SERIAL_CORE
425 help
426 ::: To be written :::
427
428config SERIAL_CLPS711X_CONSOLE
429 bool "Support for console on CLPS711X serial port"
430 depends on SERIAL_CLPS711X=y
431 select SERIAL_CORE_CONSOLE
432 help
433 Even if you say Y here, the currently visible virtual console
434 (/dev/tty0) will still be used as the system console by default, but
435 you can alter that using a kernel command line option such as
436 "console=ttyCL1". (Try "man bootparam" or see the documentation of
437 your boot loader (lilo or loadlin) about how to pass options to the
438 kernel at boot time.)
439
440config SERIAL_SAMSUNG
441 tristate "Samsung SoC serial support"
442 depends on ARM && PLAT_SAMSUNG
443 select SERIAL_CORE
444 help
445 Support for the on-chip UARTs on the Samsung S3C24XX series CPUs,
446 providing /dev/ttySAC0, 1 and 2 (note, some machines may not
447 provide all of these ports, depending on how the serial port
448 pins are configured.
449
450config SERIAL_SAMSUNG_UARTS_4
451 bool
452 depends on ARM && PLAT_SAMSUNG
453 default y if CPU_S3C2443
454 help
455 Internal node for the common case of 4 Samsung compatible UARTs
456
457config SERIAL_SAMSUNG_UARTS
458 int
459 depends on ARM && PLAT_SAMSUNG
460 default 2 if ARCH_S3C2400
461 default 6 if ARCH_S5P6450
462 default 4 if SERIAL_SAMSUNG_UARTS_4
463 default 3
464 help
465 Select the number of available UART ports for the Samsung S3C
466 serial driver
467
468config SERIAL_SAMSUNG_DEBUG
469 bool "Samsung SoC serial debug"
470 depends on SERIAL_SAMSUNG && DEBUG_LL
471 help
472 Add support for debugging the serial driver. Since this is
473 generally being used as a console, we use our own output
474 routines that go via the low-level debug printascii()
475 function.
476
477config SERIAL_SAMSUNG_CONSOLE
478 bool "Support for console on Samsung SoC serial port"
479 depends on SERIAL_SAMSUNG=y
480 select SERIAL_CORE_CONSOLE
481 help
482 Allow selection of the S3C24XX on-board serial ports for use as
483 an virtual console.
484
485 Even if you say Y here, the currently visible virtual console
486 (/dev/tty0) will still be used as the system console by default, but
487 you can alter that using a kernel command line option such as
488 "console=ttySACx". (Try "man bootparam" or see the documentation of
489 your boot loader about how to pass options to the kernel at
490 boot time.)
491
492config SERIAL_S3C2400
493 tristate "Samsung S3C2410 Serial port support"
494 depends on ARM && SERIAL_SAMSUNG && CPU_S3C2400
495 default y if CPU_S3C2400
496 help
497 Serial port support for the Samsung S3C2400 SoC
498
499config SERIAL_S3C2410
500 tristate "Samsung S3C2410 Serial port support"
501 depends on SERIAL_SAMSUNG && CPU_S3C2410
502 default y if CPU_S3C2410
503 help
504 Serial port support for the Samsung S3C2410 SoC
505
506config SERIAL_S3C2412
507 tristate "Samsung S3C2412/S3C2413 Serial port support"
508 depends on SERIAL_SAMSUNG && CPU_S3C2412
509 default y if CPU_S3C2412
510 help
511 Serial port support for the Samsung S3C2412 and S3C2413 SoC
512
513config SERIAL_S3C2440
514 tristate "Samsung S3C2440/S3C2442/S3C2416 Serial port support"
515 depends on SERIAL_SAMSUNG && (CPU_S3C2440 || CPU_S3C2442 || CPU_S3C2416)
516 default y if CPU_S3C2440
517 default y if CPU_S3C2442
518 select SERIAL_SAMSUNG_UARTS_4 if CPU_S3C2416
519 help
520 Serial port support for the Samsung S3C2440, S3C2416 and S3C2442 SoC
521
522config SERIAL_S3C24A0
523 tristate "Samsung S3C24A0 Serial port support"
524 depends on SERIAL_SAMSUNG && CPU_S3C24A0
525 default y if CPU_S3C24A0
526 help
527 Serial port support for the Samsung S3C24A0 SoC
528
529config SERIAL_S3C6400
530 tristate "Samsung S3C6400/S3C6410/S5P6440/S5P6450/S5PC100 Serial port support"
531 depends on SERIAL_SAMSUNG && (CPU_S3C6400 || CPU_S3C6410 || CPU_S5P6440 || CPU_S5P6450 || CPU_S5PC100)
532 select SERIAL_SAMSUNG_UARTS_4
533 default y
534 help
535 Serial port support for the Samsung S3C6400, S3C6410, S5P6440, S5P6450
536 and S5PC100 SoCs
537
538config SERIAL_S5PV210
539 tristate "Samsung S5PV210 Serial port support"
540 depends on SERIAL_SAMSUNG && (CPU_S5PV210 || CPU_EXYNOS4210)
541 select SERIAL_SAMSUNG_UARTS_4 if (CPU_S5PV210 || CPU_EXYNOS4210)
542 default y
543 help
544 Serial port support for Samsung's S5P Family of SoC's
545
546
547config SERIAL_MAX3100
548 tristate "MAX3100 support"
549 depends on SPI
550 select SERIAL_CORE
551 help
552 MAX3100 chip support
553
554config SERIAL_MAX3107
555 tristate "MAX3107 support"
556 depends on SPI
557 select SERIAL_CORE
558 help
559 MAX3107 chip support
560
561config SERIAL_MAX3107_AAVA
562 tristate "MAX3107 AAVA platform support"
563 depends on X86_MRST && SERIAL_MAX3107 && GPIOLIB
564 select SERIAL_CORE
565 help
566 Support for the MAX3107 chip configuration found on the AAVA
567 platform. Includes the extra initialisation and GPIO support
568 neded for this device.
569
570config SERIAL_DZ
571 bool "DECstation DZ serial driver"
572 depends on MACH_DECSTATION && 32BIT
573 select SERIAL_CORE
574 default y
575 ---help---
576 DZ11-family serial controllers for DECstations and VAXstations,
577 including the DC7085, M7814, and M7819.
578
579config SERIAL_DZ_CONSOLE
580 bool "Support console on DECstation DZ serial driver"
581 depends on SERIAL_DZ=y
582 select SERIAL_CORE_CONSOLE
583 default y
584 ---help---
585 If you say Y here, it will be possible to use a serial port as the
586 system console (the system console is the device which receives all
587 kernel messages and warnings and which allows logins in single user
588 mode).
589
590 Note that the firmware uses ttyS3 as the serial console on
591 DECstations that use this driver.
592
593 If unsure, say Y.
594
595config SERIAL_ZS
596 tristate "DECstation Z85C30 serial support"
597 depends on MACH_DECSTATION
598 select SERIAL_CORE
599 default y
600 ---help---
601 Support for the Zilog 85C350 serial communications controller used
602 for serial ports in newer DECstation systems. These include the
603 DECsystem 5900 and all models of the DECstation and DECsystem 5000
604 systems except from model 200.
605
606 If unsure, say Y. To compile this driver as a module, choose M here:
607 the module will be called zs.
608
609config SERIAL_ZS_CONSOLE
610 bool "Support for console on a DECstation Z85C30 serial port"
611 depends on SERIAL_ZS=y
612 select SERIAL_CORE_CONSOLE
613 default y
614 ---help---
615 If you say Y here, it will be possible to use a serial port as the
616 system console (the system console is the device which receives all
617 kernel messages and warnings and which allows logins in single user
618 mode).
619
620 Note that the firmware uses ttyS1 as the serial console on the
621 Maxine and ttyS3 on the others using this driver.
622
623 If unsure, say Y.
624
625config SERIAL_21285
626 tristate "DC21285 serial port support"
627 depends on ARM && FOOTBRIDGE
628 select SERIAL_CORE
629 help
630 If you have a machine based on a 21285 (Footbridge) StrongARM(R)/
631 PCI bridge you can enable its onboard serial port by enabling this
632 option.
633
634config SERIAL_21285_CONSOLE
635 bool "Console on DC21285 serial port"
636 depends on SERIAL_21285=y
637 select SERIAL_CORE_CONSOLE
638 help
639 If you have enabled the serial port on the 21285 footbridge you can
640 make it the console by answering Y to this option.
641
642 Even if you say Y here, the currently visible virtual console
643 (/dev/tty0) will still be used as the system console by default, but
644 you can alter that using a kernel command line option such as
645 "console=ttyFB". (Try "man bootparam" or see the documentation of
646 your boot loader (lilo or loadlin) about how to pass options to the
647 kernel at boot time.)
648
649config SERIAL_MPSC
650 bool "Marvell MPSC serial port support"
651 depends on PPC32 && MV64X60
652 select SERIAL_CORE
653 help
654 Say Y here if you want to use the Marvell MPSC serial controller.
655
656config SERIAL_MPSC_CONSOLE
657 bool "Support for console on Marvell MPSC serial port"
658 depends on SERIAL_MPSC
659 select SERIAL_CORE_CONSOLE
660 help
661 Say Y here if you want to support a serial console on a Marvell MPSC.
662
663config SERIAL_PXA
664 bool "PXA serial port support"
665 depends on ARCH_PXA || ARCH_MMP
666 select SERIAL_CORE
667 help
668 If you have a machine based on an Intel XScale PXA2xx CPU you
669 can enable its onboard serial ports by enabling this option.
670
671config SERIAL_PXA_CONSOLE
672 bool "Console on PXA serial port"
673 depends on SERIAL_PXA
674 select SERIAL_CORE_CONSOLE
675 help
676 If you have enabled the serial port on the Intel XScale PXA
677 CPU you can make it the console by answering Y to this option.
678
679 Even if you say Y here, the currently visible virtual console
680 (/dev/tty0) will still be used as the system console by default, but
681 you can alter that using a kernel command line option such as
682 "console=ttySA0". (Try "man bootparam" or see the documentation of
683 your boot loader (lilo or loadlin) about how to pass options to the
684 kernel at boot time.)
685
686config SERIAL_SA1100
687 bool "SA1100 serial port support"
688 depends on ARM && ARCH_SA1100
689 select SERIAL_CORE
690 help
691 If you have a machine based on a SA1100/SA1110 StrongARM(R) CPU you
692 can enable its onboard serial port by enabling this option.
693 Please read <file:Documentation/arm/SA1100/serial_UART> for further
694 info.
695
696config SERIAL_SA1100_CONSOLE
697 bool "Console on SA1100 serial port"
698 depends on SERIAL_SA1100
699 select SERIAL_CORE_CONSOLE
700 help
701 If you have enabled the serial port on the SA1100/SA1110 StrongARM
702 CPU you can make it the console by answering Y to this option.
703
704 Even if you say Y here, the currently visible virtual console
705 (/dev/tty0) will still be used as the system console by default, but
706 you can alter that using a kernel command line option such as
707 "console=ttySA0". (Try "man bootparam" or see the documentation of
708 your boot loader (lilo or loadlin) about how to pass options to the
709 kernel at boot time.)
710
711config SERIAL_MRST_MAX3110
712 tristate "SPI UART driver for Max3110"
713 depends on SPI_DW_PCI
714 select SERIAL_CORE
715 select SERIAL_CORE_CONSOLE
716 help
717 This is the UART protocol driver for the MAX3110 device on
718 the Intel Moorestown platform. On other systems use the max3100
719 driver.
720
721config SERIAL_MFD_HSU
722 tristate "Medfield High Speed UART support"
723 depends on PCI
724 select SERIAL_CORE
725
726config SERIAL_MFD_HSU_CONSOLE
727 boolean "Medfile HSU serial console support"
728 depends on SERIAL_MFD_HSU=y
729 select SERIAL_CORE_CONSOLE
730
731config SERIAL_BFIN
732 tristate "Blackfin serial port support"
733 depends on BLACKFIN
734 select SERIAL_CORE
735 select SERIAL_BFIN_UART0 if (BF531 || BF532 || BF533 || BF561)
736 help
737 Add support for the built-in UARTs on the Blackfin.
738
739 To compile this driver as a module, choose M here: the
740 module will be called bfin_5xx.
741
742config SERIAL_BFIN_CONSOLE
743 bool "Console on Blackfin serial port"
744 depends on SERIAL_BFIN=y
745 select SERIAL_CORE_CONSOLE
746
747choice
748 prompt "UART Mode"
749 depends on SERIAL_BFIN
750 default SERIAL_BFIN_DMA
751 help
752 This driver supports the built-in serial ports of the Blackfin family
753 of CPUs
754
755config SERIAL_BFIN_DMA
756 bool "DMA mode"
757 depends on !DMA_UNCACHED_NONE && KGDB_SERIAL_CONSOLE=n
758 help
759 This driver works under DMA mode. If this option is selected, the
760 blackfin simple dma driver is also enabled.
761
762config SERIAL_BFIN_PIO
763 bool "PIO mode"
764 help
765 This driver works under PIO mode.
766
767endchoice
768
769config SERIAL_BFIN_UART0
770 bool "Enable UART0"
771 depends on SERIAL_BFIN
772 help
773 Enable UART0
774
775config BFIN_UART0_CTSRTS
776 bool "Enable UART0 hardware flow control"
777 depends on SERIAL_BFIN_UART0
778 help
779 Enable hardware flow control in the driver.
780
781config SERIAL_BFIN_UART1
782 bool "Enable UART1"
783 depends on SERIAL_BFIN && (!BF531 && !BF532 && !BF533 && !BF561)
784 help
785 Enable UART1
786
787config BFIN_UART1_CTSRTS
788 bool "Enable UART1 hardware flow control"
789 depends on SERIAL_BFIN_UART1
790 help
791 Enable hardware flow control in the driver.
792
793config SERIAL_BFIN_UART2
794 bool "Enable UART2"
795 depends on SERIAL_BFIN && (BF54x || BF538 || BF539)
796 help
797 Enable UART2
798
799config BFIN_UART2_CTSRTS
800 bool "Enable UART2 hardware flow control"
801 depends on SERIAL_BFIN_UART2
802 help
803 Enable hardware flow control in the driver.
804
805config SERIAL_BFIN_UART3
806 bool "Enable UART3"
807 depends on SERIAL_BFIN && (BF54x)
808 help
809 Enable UART3
810
811config BFIN_UART3_CTSRTS
812 bool "Enable UART3 hardware flow control"
813 depends on SERIAL_BFIN_UART3
814 help
815 Enable hardware flow control in the driver.
816
817config SERIAL_IMX
818 bool "IMX serial port support"
819 depends on ARM && (ARCH_IMX || ARCH_MXC)
820 select SERIAL_CORE
821 select RATIONAL
822 help
823 If you have a machine based on a Motorola IMX CPU you
824 can enable its onboard serial port by enabling this option.
825
826config SERIAL_IMX_CONSOLE
827 bool "Console on IMX serial port"
828 depends on SERIAL_IMX
829 select SERIAL_CORE_CONSOLE
830 help
831 If you have enabled the serial port on the Motorola IMX
832 CPU you can make it the console by answering Y to this option.
833
834 Even if you say Y here, the currently visible virtual console
835 (/dev/tty0) will still be used as the system console by default, but
836 you can alter that using a kernel command line option such as
837 "console=ttySA0". (Try "man bootparam" or see the documentation of
838 your boot loader (lilo or loadlin) about how to pass options to the
839 kernel at boot time.)
840
841config SERIAL_UARTLITE
842 tristate "Xilinx uartlite serial port support"
843 depends on PPC32 || MICROBLAZE || MFD_TIMBERDALE
844 select SERIAL_CORE
845 help
846 Say Y here if you want to use the Xilinx uartlite serial controller.
847
848 To compile this driver as a module, choose M here: the
849 module will be called uartlite.
850
851config SERIAL_UARTLITE_CONSOLE
852 bool "Support for console on Xilinx uartlite serial port"
853 depends on SERIAL_UARTLITE=y
854 select SERIAL_CORE_CONSOLE
855 help
856 Say Y here if you wish to use a Xilinx uartlite as the system
857 console (the system console is the device which receives all kernel
858 messages and warnings and which allows logins in single user mode).
859
860config SERIAL_SUNCORE
861 bool
862 depends on SPARC
863 select SERIAL_CORE
864 select SERIAL_CORE_CONSOLE
865 default y
866
867config SERIAL_SUNZILOG
868 tristate "Sun Zilog8530 serial support"
869 depends on SPARC
870 help
871 This driver supports the Zilog8530 serial ports found on many Sparc
872 systems. Say Y or M if you want to be able to these serial ports.
873
874config SERIAL_SUNZILOG_CONSOLE
875 bool "Console on Sun Zilog8530 serial port"
876 depends on SERIAL_SUNZILOG=y
877 help
878 If you would like to be able to use the Zilog8530 serial port
879 on your Sparc system as the console, you can do so by answering
880 Y to this option.
881
882config SERIAL_SUNSU
883 tristate "Sun SU serial support"
884 depends on SPARC && PCI
885 help
886 This driver supports the 8250 serial ports that run the keyboard and
887 mouse on (PCI) UltraSPARC systems. Say Y or M if you want to be able
888 to these serial ports.
889
890config SERIAL_SUNSU_CONSOLE
891 bool "Console on Sun SU serial port"
892 depends on SERIAL_SUNSU=y
893 help
894 If you would like to be able to use the SU serial port
895 on your Sparc system as the console, you can do so by answering
896 Y to this option.
897
898config SERIAL_MUX
899 tristate "Serial MUX support"
900 depends on GSC
901 select SERIAL_CORE
902 default y
903 ---help---
904 Saying Y here will enable the hardware MUX serial driver for
905 the Nova, K class systems and D class with a 'remote control card'.
906 The hardware MUX is not 8250/16550 compatible therefore the
907 /dev/ttyB0 device is shared between the Serial MUX and the PDC
908 software console. The following steps need to be completed to use
909 the Serial MUX:
910
911 1. create the device entry (mknod /dev/ttyB0 c 11 0)
912 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
913 3. Add device ttyB0 to /etc/securetty (if you want to log on as
914 root on this console.)
915 4. Change the kernel command console parameter to: console=ttyB0
916
917config SERIAL_MUX_CONSOLE
918 bool "Support for console on serial MUX"
919 depends on SERIAL_MUX=y
920 select SERIAL_CORE_CONSOLE
921 default y
922
923config PDC_CONSOLE
924 bool "PDC software console support"
925 depends on PARISC && !SERIAL_MUX && VT
926 default n
927 help
928 Saying Y here will enable the software based PDC console to be
929 used as the system console. This is useful for machines in
930 which the hardware based console has not been written yet. The
931 following steps must be competed to use the PDC console:
932
933 1. create the device entry (mknod /dev/ttyB0 c 11 0)
934 2. Edit the /etc/inittab to start a getty listening on /dev/ttyB0
935 3. Add device ttyB0 to /etc/securetty (if you want to log on as
936 root on this console.)
937 4. Change the kernel command console parameter to: console=ttyB0
938
939config SERIAL_SUNSAB
940 tristate "Sun Siemens SAB82532 serial support"
941 depends on SPARC && PCI
942 help
943 This driver supports the Siemens SAB82532 DUSCC serial ports on newer
944 (PCI) UltraSPARC systems. Say Y or M if you want to be able to these
945 serial ports.
946
947config SERIAL_SUNSAB_CONSOLE
948 bool "Console on Sun Siemens SAB82532 serial port"
949 depends on SERIAL_SUNSAB=y
950 help
951 If you would like to be able to use the SAB82532 serial port
952 on your Sparc system as the console, you can do so by answering
953 Y to this option.
954
955config SERIAL_SUNHV
956 bool "Sun4v Hypervisor Console support"
957 depends on SPARC64
958 help
959 This driver supports the console device found on SUN4V Sparc
960 systems. Say Y if you want to be able to use this device.
961
962config SERIAL_IP22_ZILOG
963 tristate "SGI Zilog8530 serial support"
964 depends on SGI_HAS_ZILOG
965 select SERIAL_CORE
966 help
967 This driver supports the Zilog8530 serial ports found on SGI
968 systems. Say Y or M if you want to be able to these serial ports.
969
970config SERIAL_IP22_ZILOG_CONSOLE
971 bool "Console on SGI Zilog8530 serial port"
972 depends on SERIAL_IP22_ZILOG=y
973 select SERIAL_CORE_CONSOLE
974
975config SERIAL_SH_SCI
976 tristate "SuperH SCI(F) serial port support"
977 depends on HAVE_CLK && (SUPERH || H8300 || ARCH_SHMOBILE)
978 select SERIAL_CORE
979
980config SERIAL_SH_SCI_NR_UARTS
981 int "Maximum number of SCI(F) serial ports"
982 depends on SERIAL_SH_SCI
983 default "2"
984
985config SERIAL_SH_SCI_CONSOLE
986 bool "Support for console on SuperH SCI(F)"
987 depends on SERIAL_SH_SCI=y
988 select SERIAL_CORE_CONSOLE
989
990config SERIAL_SH_SCI_DMA
991 bool "DMA support"
992 depends on SERIAL_SH_SCI && SH_DMAE && EXPERIMENTAL
993
994config SERIAL_PNX8XXX
995 bool "Enable PNX8XXX SoCs' UART Support"
996 depends on MIPS && (SOC_PNX8550 || SOC_PNX833X)
997 select SERIAL_CORE
998 help
999 If you have a MIPS-based Philips SoC such as PNX8550 or PNX8330
1000 and you want to use serial ports, say Y. Otherwise, say N.
1001
1002config SERIAL_PNX8XXX_CONSOLE
1003 bool "Enable PNX8XX0 serial console"
1004 depends on SERIAL_PNX8XXX
1005 select SERIAL_CORE_CONSOLE
1006 help
1007 If you have a MIPS-based Philips SoC such as PNX8550 or PNX8330
1008 and you want to use serial console, say Y. Otherwise, say N.
1009
1010config SERIAL_CORE
1011 tristate
1012
1013config SERIAL_CORE_CONSOLE
1014 bool
1015
1016config CONSOLE_POLL
1017 bool
1018
1019config SERIAL_68328
1020 bool "68328 serial support"
1021 depends on M68328 || M68EZ328 || M68VZ328
1022 help
1023 This driver supports the built-in serial port of the Motorola 68328
1024 (standard, EZ and VZ varieties).
1025
1026config SERIAL_68328_RTS_CTS
1027 bool "Support RTS/CTS on 68328 serial port"
1028 depends on SERIAL_68328
1029
1030config SERIAL_MCF
1031 bool "Coldfire serial support"
1032 depends on COLDFIRE
1033 select SERIAL_CORE
1034 help
1035 This serial driver supports the Freescale Coldfire serial ports.
1036
1037config SERIAL_MCF_BAUDRATE
1038 int "Default baudrate for Coldfire serial ports"
1039 depends on SERIAL_MCF
1040 default 19200
1041 help
1042 This setting lets you define what the default baudrate is for the
1043 ColdFire serial ports. The usual default varies from board to board,
1044 and this setting is a way of catering for that.
1045
1046config SERIAL_MCF_CONSOLE
1047 bool "Coldfire serial console support"
1048 depends on SERIAL_MCF
1049 select SERIAL_CORE_CONSOLE
1050 help
1051 Enable a ColdFire internal serial port to be the system console.
1052
1053config SERIAL_68360_SMC
1054 bool "68360 SMC uart support"
1055 depends on M68360
1056 help
1057 This driver supports the SMC serial ports of the Motorola 68360 CPU.
1058
1059config SERIAL_68360_SCC
1060 bool "68360 SCC uart support"
1061 depends on M68360
1062 help
1063 This driver supports the SCC serial ports of the Motorola 68360 CPU.
1064
1065config SERIAL_68360
1066 bool
1067 depends on SERIAL_68360_SMC || SERIAL_68360_SCC
1068 default y
1069
1070config SERIAL_PMACZILOG
1071 tristate "Mac or PowerMac z85c30 ESCC support"
1072 depends on (M68K && MAC) || (PPC_OF && PPC_PMAC)
1073 select SERIAL_CORE
1074 help
1075 This driver supports the Zilog z85C30 serial ports found on
1076 (Power)Mac machines.
1077 Say Y or M if you want to be able to these serial ports.
1078
1079config SERIAL_PMACZILOG_TTYS
1080 bool "Use ttySn device nodes for Zilog z85c30"
1081 depends on SERIAL_PMACZILOG
1082 help
1083 The pmac_zilog driver for the z85C30 chip on many powermacs
1084 historically used the device numbers for /dev/ttySn. The
1085 8250 serial port driver also uses these numbers, which means
1086 the two drivers being unable to coexist; you could not use
1087 both z85C30 and 8250 type ports at the same time.
1088
1089 If this option is not selected, the pmac_zilog driver will
1090 use the device numbers allocated for /dev/ttyPZn. This allows
1091 the pmac_zilog and 8250 drivers to co-exist, but may cause
1092 existing userspace setups to break. Programs that need to
1093 access the built-in serial ports on powermacs will need to
1094 be reconfigured to use /dev/ttyPZn instead of /dev/ttySn.
1095
1096 If you enable this option, any z85c30 ports in the system will
1097 be registered as ttyS0 onwards as in the past, and you will be
1098 unable to use the 8250 module for PCMCIA or other 16C550-style
1099 UARTs.
1100
1101 Say N unless you need the z85c30 ports on your (Power)Mac
1102 to appear as /dev/ttySn.
1103
1104config SERIAL_PMACZILOG_CONSOLE
1105 bool "Console on Mac or PowerMac z85c30 serial port"
1106 depends on SERIAL_PMACZILOG=y
1107 select SERIAL_CORE_CONSOLE
1108 help
1109 If you would like to be able to use the z85c30 serial port
1110 on your (Power)Mac as the console, you can do so by answering
1111 Y to this option.
1112
1113config SERIAL_CPM
1114 tristate "CPM SCC/SMC serial port support"
1115 depends on CPM2 || 8xx
1116 select SERIAL_CORE
1117 help
1118 This driver supports the SCC and SMC serial ports on Motorola
1119 embedded PowerPC that contain a CPM1 (8xx) or CPM2 (8xxx)
1120
1121config SERIAL_CPM_CONSOLE
1122 bool "Support for console on CPM SCC/SMC serial port"
1123 depends on SERIAL_CPM=y
1124 select SERIAL_CORE_CONSOLE
1125 help
1126 Say Y here if you wish to use a SCC or SMC CPM UART as the system
1127 console (the system console is the device which receives all kernel
1128 messages and warnings and which allows logins in single user mode).
1129
1130 Even if you say Y here, the currently visible framebuffer console
1131 (/dev/tty0) will still be used as the system console by default, but
1132 you can alter that using a kernel command line option such as
1133 "console=ttyCPM0". (Try "man bootparam" or see the documentation of
1134 your boot loader (lilo or loadlin) about how to pass options to the
1135 kernel at boot time.)
1136
1137config SERIAL_SGI_L1_CONSOLE
1138 bool "SGI Altix L1 serial console support"
1139 depends on IA64_GENERIC || IA64_SGI_SN2
1140 select SERIAL_CORE
1141 select SERIAL_CORE_CONSOLE
1142 help
1143 If you have an SGI Altix and you would like to use the system
1144 controller serial port as your console (you want this!),
1145 say Y. Otherwise, say N.
1146
1147config SERIAL_MPC52xx
1148 tristate "Freescale MPC52xx/MPC512x family PSC serial support"
1149 depends on PPC_MPC52xx || PPC_MPC512x
1150 select SERIAL_CORE
1151 help
1152 This driver supports MPC52xx and MPC512x PSC serial ports. If you would
1153 like to use them, you must answer Y or M to this option. Note that
1154 for use as console, it must be included in kernel and not as a
1155 module.
1156
1157config SERIAL_MPC52xx_CONSOLE
1158 bool "Console on a Freescale MPC52xx/MPC512x family PSC serial port"
1159 depends on SERIAL_MPC52xx=y
1160 select SERIAL_CORE_CONSOLE
1161 help
1162 Select this options if you'd like to use one of the PSC serial port
1163 of the Freescale MPC52xx family as a console.
1164
1165config SERIAL_MPC52xx_CONSOLE_BAUD
1166 int "Freescale MPC52xx/MPC512x family PSC serial port baud"
1167 depends on SERIAL_MPC52xx_CONSOLE=y
1168 default "9600"
1169 help
1170 Select the MPC52xx console baud rate.
1171 This value is only used if the bootloader doesn't pass in the
1172 console baudrate
1173
1174config SERIAL_ICOM
1175 tristate "IBM Multiport Serial Adapter"
1176 depends on PCI && (PPC_ISERIES || PPC_PSERIES)
1177 select SERIAL_CORE
1178 select FW_LOADER
1179 help
1180 This driver is for a family of multiport serial adapters
1181 including 2 port RVX, 2 port internal modem, 4 port internal
1182 modem and a split 1 port RVX and 1 port internal modem.
1183
1184 This driver can also be built as a module. If so, the module
1185 will be called icom.
1186
1187config SERIAL_M32R_SIO
1188 bool "M32R SIO I/F"
1189 depends on M32R
1190 default y
1191 select SERIAL_CORE
1192 help
1193 Say Y here if you want to use the M32R serial controller.
1194
1195config SERIAL_M32R_SIO_CONSOLE
1196 bool "use SIO console"
1197 depends on SERIAL_M32R_SIO=y
1198 select SERIAL_CORE_CONSOLE
1199 help
1200 Say Y here if you want to support a serial console.
1201
1202 If you use an M3T-M32700UT or an OPSPUT platform,
1203 please say also y for SERIAL_M32R_PLDSIO.
1204
1205config SERIAL_M32R_PLDSIO
1206 bool "M32R SIO I/F on a PLD"
1207 depends on SERIAL_M32R_SIO=y && (PLAT_OPSPUT || PLAT_USRV || PLAT_M32700UT)
1208 default n
1209 help
1210 Say Y here if you want to use the M32R serial controller
1211 on a PLD (Programmable Logic Device).
1212
1213 If you use an M3T-M32700UT or an OPSPUT platform,
1214 please say Y.
1215
1216config SERIAL_TXX9
1217 bool "TMPTX39XX/49XX SIO support"
1218 depends on HAS_TXX9_SERIAL
1219 select SERIAL_CORE
1220 default y
1221
1222config HAS_TXX9_SERIAL
1223 bool
1224
1225config SERIAL_TXX9_NR_UARTS
1226 int "Maximum number of TMPTX39XX/49XX SIO ports"
1227 depends on SERIAL_TXX9
1228 default "6"
1229
1230config SERIAL_TXX9_CONSOLE
1231 bool "TMPTX39XX/49XX SIO Console support"
1232 depends on SERIAL_TXX9=y
1233 select SERIAL_CORE_CONSOLE
1234
1235config SERIAL_TXX9_STDSERIAL
1236 bool "TX39XX/49XX SIO act as standard serial"
1237 depends on !SERIAL_8250 && SERIAL_TXX9
1238
1239config SERIAL_VR41XX
1240 tristate "NEC VR4100 series Serial Interface Unit support"
1241 depends on CPU_VR41XX
1242 select SERIAL_CORE
1243 help
1244 If you have a NEC VR4100 series processor and you want to use
1245 Serial Interface Unit(SIU) or Debug Serial Interface Unit(DSIU)
1246 (not include VR4111/VR4121 DSIU), say Y. Otherwise, say N.
1247
1248config SERIAL_VR41XX_CONSOLE
1249 bool "Enable NEC VR4100 series Serial Interface Unit console"
1250 depends on SERIAL_VR41XX=y
1251 select SERIAL_CORE_CONSOLE
1252 help
1253 If you have a NEC VR4100 series processor and you want to use
1254 a console on a serial port, say Y. Otherwise, say N.
1255
1256config SERIAL_JSM
1257 tristate "Digi International NEO PCI Support"
1258 depends on PCI
1259 select SERIAL_CORE
1260 help
1261 This is a driver for Digi International's Neo series
1262 of cards which provide multiple serial ports. You would need
1263 something like this to connect more than two modems to your Linux
1264 box, for instance in order to become a dial-in server. This driver
1265 supports PCI boards only.
1266
1267 If you have a card like this, say Y here, otherwise say N.
1268
1269 To compile this driver as a module, choose M here: the
1270 module will be called jsm.
1271
1272config SERIAL_SGI_IOC4
1273 tristate "SGI IOC4 controller serial support"
1274 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC4
1275 select SERIAL_CORE
1276 help
1277 If you have an SGI Altix with an IOC4 based Base IO card
1278 and wish to use the serial ports on this card, say Y.
1279 Otherwise, say N.
1280
1281config SERIAL_SGI_IOC3
1282 tristate "SGI Altix IOC3 serial support"
1283 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC3
1284 select SERIAL_CORE
1285 help
1286 If you have an SGI Altix with an IOC3 serial card,
1287 say Y or M. Otherwise, say N.
1288
1289config SERIAL_MSM
1290 bool "MSM on-chip serial port support"
1291 depends on ARM && ARCH_MSM
1292 select SERIAL_CORE
1293
1294config SERIAL_MSM_CONSOLE
1295 bool "MSM serial console support"
1296 depends on SERIAL_MSM=y
1297 select SERIAL_CORE_CONSOLE
1298
1299config SERIAL_MSM_HS
1300 tristate "MSM UART High Speed: Serial Driver"
1301 depends on ARCH_MSM
1302 select SERIAL_CORE
1303 help
1304 If you have a machine based on MSM family of SoCs, you
1305 can enable its onboard high speed serial port by enabling
1306 this option.
1307
1308 Choose M here to compile it as a module. The module will be
1309 called msm_serial_hs.
1310
1311config SERIAL_VT8500
1312 bool "VIA VT8500 on-chip serial port support"
1313 depends on ARM && ARCH_VT8500
1314 select SERIAL_CORE
1315
1316config SERIAL_VT8500_CONSOLE
1317 bool "VIA VT8500 serial console support"
1318 depends on SERIAL_VT8500=y
1319 select SERIAL_CORE_CONSOLE
1320
1321config SERIAL_NETX
1322 tristate "NetX serial port support"
1323 depends on ARM && ARCH_NETX
1324 select SERIAL_CORE
1325 help
1326 If you have a machine based on a Hilscher NetX SoC you
1327 can enable its onboard serial port by enabling this option.
1328
1329 To compile this driver as a module, choose M here: the
1330 module will be called netx-serial.
1331
1332config SERIAL_NETX_CONSOLE
1333 bool "Console on NetX serial port"
1334 depends on SERIAL_NETX=y
1335 select SERIAL_CORE_CONSOLE
1336 help
1337 If you have enabled the serial port on the Hilscher NetX SoC
1338 you can make it the console by answering Y to this option.
1339
1340config SERIAL_OF_PLATFORM
1341 tristate "Serial port on Open Firmware platform bus"
1342 depends on OF
1343 depends on SERIAL_8250 || SERIAL_OF_PLATFORM_NWPSERIAL
1344 help
1345 If you have a PowerPC based system that has serial ports
1346 on a platform specific bus, you should enable this option.
1347 Currently, only 8250 compatible ports are supported, but
1348 others can easily be added.
1349
1350config SERIAL_OMAP
1351 tristate "OMAP serial port support"
1352 depends on ARCH_OMAP2 || ARCH_OMAP3 || ARCH_OMAP4
1353 select SERIAL_CORE
1354 help
1355 If you have a machine based on an Texas Instruments OMAP CPU you
1356 can enable its onboard serial ports by enabling this option.
1357
1358 By enabling this option you take advantage of dma feature available
1359 with the omap-serial driver. DMA support can be enabled from platform
1360 data.
1361
1362config SERIAL_OMAP_CONSOLE
1363 bool "Console on OMAP serial port"
1364 depends on SERIAL_OMAP
1365 select SERIAL_CORE_CONSOLE
1366 help
1367 Select this option if you would like to use omap serial port as
1368 console.
1369
1370 Even if you say Y here, the currently visible virtual console
1371 (/dev/tty0) will still be used as the system console by default, but
1372 you can alter that using a kernel command line option such as
1373 "console=ttyOx". (Try "man bootparam" or see the documentation of
1374 your boot loader about how to pass options to the kernel at
1375 boot time.)
1376
1377config SERIAL_OF_PLATFORM_NWPSERIAL
1378 tristate "NWP serial port driver"
1379 depends on PPC_OF && PPC_DCR
1380 select SERIAL_OF_PLATFORM
1381 select SERIAL_CORE_CONSOLE
1382 select SERIAL_CORE
1383 help
1384 This driver supports the cell network processor nwp serial
1385 device.
1386
1387config SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
1388 bool "Console on NWP serial port"
1389 depends on SERIAL_OF_PLATFORM_NWPSERIAL=y
1390 select SERIAL_CORE_CONSOLE
1391 help
1392 Support for Console on the NWP serial ports.
1393
1394config SERIAL_LANTIQ
1395 bool "Lantiq serial driver"
1396 depends on LANTIQ
1397 select SERIAL_CORE
1398 select SERIAL_CORE_CONSOLE
1399 help
1400 Support for console and UART on Lantiq SoCs.
1401
1402config SERIAL_QE
1403 tristate "Freescale QUICC Engine serial port support"
1404 depends on QUICC_ENGINE
1405 select SERIAL_CORE
1406 select FW_LOADER
1407 default n
1408 help
1409 This driver supports the QE serial ports on Freescale embedded
1410 PowerPC that contain a QUICC Engine.
1411
1412config SERIAL_SC26XX
1413 tristate "SC2681/SC2692 serial port support"
1414 depends on SNI_RM
1415 select SERIAL_CORE
1416 help
1417 This is a driver for the onboard serial ports of
1418 older RM400 machines.
1419
1420config SERIAL_SC26XX_CONSOLE
1421 bool "Console on SC2681/SC2692 serial port"
1422 depends on SERIAL_SC26XX
1423 select SERIAL_CORE_CONSOLE
1424 help
1425 Support for Console on SC2681/SC2692 serial ports.
1426
1427config SERIAL_BFIN_SPORT
1428 tristate "Blackfin SPORT emulate UART"
1429 depends on BLACKFIN
1430 select SERIAL_CORE
1431 help
1432 Enable SPORT emulate UART on Blackfin series.
1433
1434 To compile this driver as a module, choose M here: the
1435 module will be called bfin_sport_uart.
1436
1437config SERIAL_BFIN_SPORT_CONSOLE
1438 bool "Console on Blackfin sport emulated uart"
1439 depends on SERIAL_BFIN_SPORT=y
1440 select SERIAL_CORE_CONSOLE
1441
1442config SERIAL_BFIN_SPORT0_UART
1443 bool "Enable UART over SPORT0"
1444 depends on SERIAL_BFIN_SPORT && !(BF542 || BF544)
1445 help
1446 Enable UART over SPORT0
1447
1448config SERIAL_BFIN_SPORT0_UART_CTSRTS
1449 bool "Enable UART over SPORT0 hardware flow control"
1450 depends on SERIAL_BFIN_SPORT0_UART
1451 help
1452 Enable hardware flow control in the driver.
1453
1454config SERIAL_BFIN_SPORT1_UART
1455 bool "Enable UART over SPORT1"
1456 depends on SERIAL_BFIN_SPORT
1457 help
1458 Enable UART over SPORT1
1459
1460config SERIAL_BFIN_SPORT1_UART_CTSRTS
1461 bool "Enable UART over SPORT1 hardware flow control"
1462 depends on SERIAL_BFIN_SPORT1_UART
1463 help
1464 Enable hardware flow control in the driver.
1465
1466config SERIAL_BFIN_SPORT2_UART
1467 bool "Enable UART over SPORT2"
1468 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1469 help
1470 Enable UART over SPORT2
1471
1472config SERIAL_BFIN_SPORT2_UART_CTSRTS
1473 bool "Enable UART over SPORT2 hardware flow control"
1474 depends on SERIAL_BFIN_SPORT2_UART
1475 help
1476 Enable hardware flow control in the driver.
1477
1478config SERIAL_BFIN_SPORT3_UART
1479 bool "Enable UART over SPORT3"
1480 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1481 help
1482 Enable UART over SPORT3
1483
1484config SERIAL_BFIN_SPORT3_UART_CTSRTS
1485 bool "Enable UART over SPORT3 hardware flow control"
1486 depends on SERIAL_BFIN_SPORT3_UART
1487 help
1488 Enable hardware flow control in the driver.
1489
1490config SERIAL_TIMBERDALE
1491 tristate "Support for timberdale UART"
1492 select SERIAL_CORE
1493 ---help---
1494 Add support for UART controller on timberdale.
1495
1496config SERIAL_BCM63XX
1497 tristate "bcm63xx serial port support"
1498 select SERIAL_CORE
1499 depends on BCM63XX
1500 help
1501 If you have a bcm63xx CPU, you can enable its onboard
1502 serial port by enabling this options.
1503
1504 To compile this driver as a module, choose M here: the
1505 module will be called bcm963xx_uart.
1506
1507config SERIAL_BCM63XX_CONSOLE
1508 bool "Console on bcm63xx serial port"
1509 depends on SERIAL_BCM63XX=y
1510 select SERIAL_CORE_CONSOLE
1511 help
1512 If you have enabled the serial port on the bcm63xx CPU
1513 you can make it the console by answering Y to this option.
1514
1515config SERIAL_GRLIB_GAISLER_APBUART
1516 tristate "GRLIB APBUART serial support"
1517 depends on OF && SPARC
1518 select SERIAL_CORE
1519 ---help---
1520 Add support for the GRLIB APBUART serial port.
1521
1522config SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
1523 bool "Console on GRLIB APBUART serial port"
1524 depends on SERIAL_GRLIB_GAISLER_APBUART=y
1525 select SERIAL_CORE_CONSOLE
1526 help
1527 Support for running a console on the GRLIB APBUART
1528
1529config SERIAL_ALTERA_JTAGUART
1530 tristate "Altera JTAG UART support"
1531 select SERIAL_CORE
1532 help
1533 This driver supports the Altera JTAG UART port.
1534
1535config SERIAL_ALTERA_JTAGUART_CONSOLE
1536 bool "Altera JTAG UART console support"
1537 depends on SERIAL_ALTERA_JTAGUART=y
1538 select SERIAL_CORE_CONSOLE
1539 help
1540 Enable a Altera JTAG UART port to be the system console.
1541
1542config SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS
1543 bool "Bypass output when no connection"
1544 depends on SERIAL_ALTERA_JTAGUART_CONSOLE
1545 select SERIAL_CORE_CONSOLE
1546 help
1547 Bypass console output and keep going even if there is no
1548 JTAG terminal connection with the host.
1549
1550config SERIAL_ALTERA_UART
1551 tristate "Altera UART support"
1552 select SERIAL_CORE
1553 help
1554 This driver supports the Altera softcore UART port.
1555
1556config SERIAL_ALTERA_UART_MAXPORTS
1557 int "Maximum number of Altera UART ports"
1558 depends on SERIAL_ALTERA_UART
1559 default 4
1560 help
1561 This setting lets you define the maximum number of the Altera
1562 UART ports. The usual default varies from board to board, and
1563 this setting is a way of catering for that.
1564
1565config SERIAL_ALTERA_UART_BAUDRATE
1566 int "Default baudrate for Altera UART ports"
1567 depends on SERIAL_ALTERA_UART
1568 default 115200
1569 help
1570 This setting lets you define what the default baudrate is for the
1571 Altera UART ports. The usual default varies from board to board,
1572 and this setting is a way of catering for that.
1573
1574config SERIAL_ALTERA_UART_CONSOLE
1575 bool "Altera UART console support"
1576 depends on SERIAL_ALTERA_UART=y
1577 select SERIAL_CORE_CONSOLE
1578 help
1579 Enable a Altera UART port to be the system console.
1580
1581config SERIAL_IFX6X60
1582 tristate "SPI protocol driver for Infineon 6x60 modem (EXPERIMENTAL)"
1583 depends on GPIOLIB && SPI && EXPERIMENTAL
1584 help
1585 Support for the IFX6x60 modem devices on Intel MID platforms.
1586
1587config SERIAL_PCH_UART
1588 tristate "Intel EG20T PCH / OKI SEMICONDUCTOR IOH(ML7213/ML7223) UART"
1589 depends on PCI
1590 select SERIAL_CORE
1591 help
1592 This driver is for PCH(Platform controller Hub) UART of Intel EG20T
1593 which is an IOH(Input/Output Hub) for x86 embedded processor.
1594 Enabling PCH_DMA, this PCH UART works as DMA mode.
1595
1596 This driver also can be used for OKI SEMICONDUCTOR IOH(Input/
1597 Output Hub), ML7213 and ML7223.
1598 ML7213 IOH is for IVI(In-Vehicle Infotainment) use and ML7223 IOH is
1599 for MP(Media Phone) use.
1600 ML7213/ML7223 is companion chip for Intel Atom E6xx series.
1601 ML7213/ML7223 is completely compatible for Intel EG20T PCH.
1602
1603config SERIAL_MSM_SMD
1604 bool "Enable tty device interface for some SMD ports"
1605 default n
1606 depends on MSM_SMD
1607 help
1608 Enables userspace clients to read and write to some streaming SMD
1609 ports via tty device interface for MSM chipset.
1610
1611config SERIAL_MXS_AUART
1612 depends on ARCH_MXS
1613 tristate "MXS AUART support"
1614 select SERIAL_CORE
1615 help
1616 This driver supports the MXS Application UART (AUART) port.
1617
1618config SERIAL_MXS_AUART_CONSOLE
1619 bool "MXS AUART console support"
1620 depends on SERIAL_MXS_AUART=y
1621 select SERIAL_CORE_CONSOLE
1622 help
1623 Enable a MXS AUART port to be the system console.
1624
1625config SERIAL_XILINX_PS_UART
1626 tristate "Xilinx PS UART support"
1627 select SERIAL_CORE
1628 help
1629 This driver supports the Xilinx PS UART port.
1630
1631config SERIAL_XILINX_PS_UART_CONSOLE
1632 bool "Xilinx PS UART console support"
1633 depends on SERIAL_XILINX_PS_UART=y
1634 select SERIAL_CORE_CONSOLE
1635 help
1636 Enable a Xilinx PS UART port to be the system console.
1637
1638endmenu
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
new file mode 100644
index 000000000000..cb2628fee4c7
--- /dev/null
+++ b/drivers/tty/serial/Makefile
@@ -0,0 +1,98 @@
1#
2# Makefile for the kernel serial device drivers.
3#
4
5obj-$(CONFIG_SERIAL_CORE) += serial_core.o
6obj-$(CONFIG_SERIAL_21285) += 21285.o
7
8# These Sparc drivers have to appear before others such as 8250
9# which share ttySx minor node space. Otherwise console device
10# names change and other unplesantries.
11obj-$(CONFIG_SERIAL_SUNCORE) += suncore.o
12obj-$(CONFIG_SERIAL_SUNHV) += sunhv.o
13obj-$(CONFIG_SERIAL_SUNZILOG) += sunzilog.o
14obj-$(CONFIG_SERIAL_SUNSU) += sunsu.o
15obj-$(CONFIG_SERIAL_SUNSAB) += sunsab.o
16
17obj-$(CONFIG_SERIAL_8250) += 8250.o
18obj-$(CONFIG_SERIAL_8250_PNP) += 8250_pnp.o
19obj-$(CONFIG_SERIAL_8250_GSC) += 8250_gsc.o
20obj-$(CONFIG_SERIAL_8250_PCI) += 8250_pci.o
21obj-$(CONFIG_SERIAL_8250_HP300) += 8250_hp300.o
22obj-$(CONFIG_SERIAL_8250_CS) += serial_cs.o
23obj-$(CONFIG_SERIAL_8250_ACORN) += 8250_acorn.o
24obj-$(CONFIG_SERIAL_8250_CONSOLE) += 8250_early.o
25obj-$(CONFIG_SERIAL_8250_FOURPORT) += 8250_fourport.o
26obj-$(CONFIG_SERIAL_8250_ACCENT) += 8250_accent.o
27obj-$(CONFIG_SERIAL_8250_BOCA) += 8250_boca.o
28obj-$(CONFIG_SERIAL_8250_EXAR_ST16C554) += 8250_exar_st16c554.o
29obj-$(CONFIG_SERIAL_8250_HUB6) += 8250_hub6.o
30obj-$(CONFIG_SERIAL_8250_MCA) += 8250_mca.o
31obj-$(CONFIG_SERIAL_AMBA_PL010) += amba-pl010.o
32obj-$(CONFIG_SERIAL_AMBA_PL011) += amba-pl011.o
33obj-$(CONFIG_SERIAL_CLPS711X) += clps711x.o
34obj-$(CONFIG_SERIAL_PXA) += pxa.o
35obj-$(CONFIG_SERIAL_PNX8XXX) += pnx8xxx_uart.o
36obj-$(CONFIG_SERIAL_SA1100) += sa1100.o
37obj-$(CONFIG_SERIAL_BCM63XX) += bcm63xx_uart.o
38obj-$(CONFIG_SERIAL_BFIN) += bfin_5xx.o
39obj-$(CONFIG_SERIAL_BFIN_SPORT) += bfin_sport_uart.o
40obj-$(CONFIG_SERIAL_SAMSUNG) += samsung.o
41obj-$(CONFIG_SERIAL_S3C2400) += s3c2400.o
42obj-$(CONFIG_SERIAL_S3C2410) += s3c2410.o
43obj-$(CONFIG_SERIAL_S3C2412) += s3c2412.o
44obj-$(CONFIG_SERIAL_S3C2440) += s3c2440.o
45obj-$(CONFIG_SERIAL_S3C24A0) += s3c24a0.o
46obj-$(CONFIG_SERIAL_S3C6400) += s3c6400.o
47obj-$(CONFIG_SERIAL_S5PV210) += s5pv210.o
48obj-$(CONFIG_SERIAL_MAX3100) += max3100.o
49obj-$(CONFIG_SERIAL_MAX3107) += max3107.o
50obj-$(CONFIG_SERIAL_MAX3107_AAVA) += max3107-aava.o
51obj-$(CONFIG_SERIAL_IP22_ZILOG) += ip22zilog.o
52obj-$(CONFIG_SERIAL_MUX) += mux.o
53obj-$(CONFIG_SERIAL_68328) += 68328serial.o
54obj-$(CONFIG_SERIAL_68360) += 68360serial.o
55obj-$(CONFIG_SERIAL_MCF) += mcf.o
56obj-$(CONFIG_SERIAL_PMACZILOG) += pmac_zilog.o
57obj-$(CONFIG_SERIAL_DZ) += dz.o
58obj-$(CONFIG_SERIAL_ZS) += zs.o
59obj-$(CONFIG_SERIAL_SH_SCI) += sh-sci.o
60obj-$(CONFIG_SERIAL_SGI_L1_CONSOLE) += sn_console.o
61obj-$(CONFIG_SERIAL_CPM) += cpm_uart/
62obj-$(CONFIG_SERIAL_IMX) += imx.o
63obj-$(CONFIG_SERIAL_MPC52xx) += mpc52xx_uart.o
64obj-$(CONFIG_SERIAL_ICOM) += icom.o
65obj-$(CONFIG_SERIAL_M32R_SIO) += m32r_sio.o
66obj-$(CONFIG_SERIAL_MPSC) += mpsc.o
67obj-$(CONFIG_SERIAL_SB1250_DUART) += sb1250-duart.o
68obj-$(CONFIG_ETRAX_SERIAL) += crisv10.o
69obj-$(CONFIG_SERIAL_SC26XX) += sc26xx.o
70obj-$(CONFIG_SERIAL_JSM) += jsm/
71obj-$(CONFIG_SERIAL_TXX9) += serial_txx9.o
72obj-$(CONFIG_SERIAL_VR41XX) += vr41xx_siu.o
73obj-$(CONFIG_SERIAL_SGI_IOC4) += ioc4_serial.o
74obj-$(CONFIG_SERIAL_SGI_IOC3) += ioc3_serial.o
75obj-$(CONFIG_SERIAL_ATMEL) += atmel_serial.o
76obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
77obj-$(CONFIG_SERIAL_MSM) += msm_serial.o
78obj-$(CONFIG_SERIAL_MSM_HS) += msm_serial_hs.o
79obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
80obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
81obj-$(CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL) += nwpserial.o
82obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
83obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
84obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
85obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
86obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o
87obj-$(CONFIG_SERIAL_GRLIB_GAISLER_APBUART) += apbuart.o
88obj-$(CONFIG_SERIAL_ALTERA_JTAGUART) += altera_jtaguart.o
89obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
90obj-$(CONFIG_SERIAL_VT8500) += vt8500_serial.o
91obj-$(CONFIG_SERIAL_MRST_MAX3110) += mrst_max3110.o
92obj-$(CONFIG_SERIAL_MFD_HSU) += mfd.o
93obj-$(CONFIG_SERIAL_IFX6X60) += ifx6x60.o
94obj-$(CONFIG_SERIAL_PCH_UART) += pch_uart.o
95obj-$(CONFIG_SERIAL_MSM_SMD) += msm_smd_tty.o
96obj-$(CONFIG_SERIAL_MXS_AUART) += mxs-auart.o
97obj-$(CONFIG_SERIAL_LANTIQ) += lantiq.o
98obj-$(CONFIG_SERIAL_XILINX_PS_UART) += xilinx_uartps.o
diff --git a/drivers/tty/serial/altera_jtaguart.c b/drivers/tty/serial/altera_jtaguart.c
new file mode 100644
index 000000000000..60e049b041a7
--- /dev/null
+++ b/drivers/tty/serial/altera_jtaguart.c
@@ -0,0 +1,516 @@
1/*
2 * altera_jtaguart.c -- Altera JTAG UART driver
3 *
4 * Based on mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/console.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial.h>
24#include <linux/serial_core.h>
25#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/altera_jtaguart.h>
28
29#define DRV_NAME "altera_jtaguart"
30
31/*
32 * Altera JTAG UART register definitions according to the Altera JTAG UART
33 * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
34 */
35
36#define ALTERA_JTAGUART_SIZE 8
37
38#define ALTERA_JTAGUART_DATA_REG 0
39
40#define ALTERA_JTAGUART_DATA_DATA_MSK 0x000000FF
41#define ALTERA_JTAGUART_DATA_RVALID_MSK 0x00008000
42#define ALTERA_JTAGUART_DATA_RAVAIL_MSK 0xFFFF0000
43#define ALTERA_JTAGUART_DATA_RAVAIL_OFF 16
44
45#define ALTERA_JTAGUART_CONTROL_REG 4
46
47#define ALTERA_JTAGUART_CONTROL_RE_MSK 0x00000001
48#define ALTERA_JTAGUART_CONTROL_WE_MSK 0x00000002
49#define ALTERA_JTAGUART_CONTROL_RI_MSK 0x00000100
50#define ALTERA_JTAGUART_CONTROL_RI_OFF 8
51#define ALTERA_JTAGUART_CONTROL_WI_MSK 0x00000200
52#define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
53#define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
54#define ALTERA_JTAGUART_CONTROL_WSPACE_OFF 16
55
56/*
57 * Local per-uart structure.
58 */
59struct altera_jtaguart {
60 struct uart_port port;
61 unsigned int sigs; /* Local copy of line sigs */
62 unsigned long imr; /* Local IMR mirror */
63};
64
65static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
66{
67 return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
68 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
69}
70
71static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
72{
73 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
74}
75
76static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
77{
78}
79
80static void altera_jtaguart_start_tx(struct uart_port *port)
81{
82 struct altera_jtaguart *pp =
83 container_of(port, struct altera_jtaguart, port);
84
85 pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
86 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
87}
88
89static void altera_jtaguart_stop_tx(struct uart_port *port)
90{
91 struct altera_jtaguart *pp =
92 container_of(port, struct altera_jtaguart, port);
93
94 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
95 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
96}
97
98static void altera_jtaguart_stop_rx(struct uart_port *port)
99{
100 struct altera_jtaguart *pp =
101 container_of(port, struct altera_jtaguart, port);
102
103 pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
104 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
105}
106
107static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
108{
109}
110
111static void altera_jtaguart_enable_ms(struct uart_port *port)
112{
113}
114
115static void altera_jtaguart_set_termios(struct uart_port *port,
116 struct ktermios *termios,
117 struct ktermios *old)
118{
119 /* Just copy the old termios settings back */
120 if (old)
121 tty_termios_copy_hw(termios, old);
122}
123
124static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
125{
126 struct uart_port *port = &pp->port;
127 unsigned char ch, flag;
128 unsigned long status;
129
130 while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
131 ALTERA_JTAGUART_DATA_RVALID_MSK) {
132 ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
133 flag = TTY_NORMAL;
134 port->icount.rx++;
135
136 if (uart_handle_sysrq_char(port, ch))
137 continue;
138 uart_insert_char(port, 0, 0, ch, flag);
139 }
140
141 tty_flip_buffer_push(port->state->port.tty);
142}
143
144static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
145{
146 struct uart_port *port = &pp->port;
147 struct circ_buf *xmit = &port->state->xmit;
148 unsigned int pending, count;
149
150 if (port->x_char) {
151 /* Send special char - probably flow control */
152 writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
153 port->x_char = 0;
154 port->icount.tx++;
155 return;
156 }
157
158 pending = uart_circ_chars_pending(xmit);
159 if (pending > 0) {
160 count = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
161 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
162 ALTERA_JTAGUART_CONTROL_WSPACE_OFF;
163 if (count > pending)
164 count = pending;
165 if (count > 0) {
166 pending -= count;
167 while (count--) {
168 writel(xmit->buf[xmit->tail],
169 port->membase + ALTERA_JTAGUART_DATA_REG);
170 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
171 port->icount.tx++;
172 }
173 if (pending < WAKEUP_CHARS)
174 uart_write_wakeup(port);
175 }
176 }
177
178 if (pending == 0) {
179 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
180 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
181 }
182}
183
184static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
185{
186 struct uart_port *port = data;
187 struct altera_jtaguart *pp =
188 container_of(port, struct altera_jtaguart, port);
189 unsigned int isr;
190
191 isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
192 ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
193
194 spin_lock(&port->lock);
195
196 if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
197 altera_jtaguart_rx_chars(pp);
198 if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
199 altera_jtaguart_tx_chars(pp);
200
201 spin_unlock(&port->lock);
202
203 return IRQ_RETVAL(isr);
204}
205
206static void altera_jtaguart_config_port(struct uart_port *port, int flags)
207{
208 port->type = PORT_ALTERA_JTAGUART;
209
210 /* Clear mask, so no surprise interrupts. */
211 writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
212}
213
214static int altera_jtaguart_startup(struct uart_port *port)
215{
216 struct altera_jtaguart *pp =
217 container_of(port, struct altera_jtaguart, port);
218 unsigned long flags;
219 int ret;
220
221 ret = request_irq(port->irq, altera_jtaguart_interrupt, IRQF_DISABLED,
222 DRV_NAME, port);
223 if (ret) {
224 pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
225 "interrupt vector=%d\n", port->line, port->irq);
226 return ret;
227 }
228
229 spin_lock_irqsave(&port->lock, flags);
230
231 /* Enable RX interrupts now */
232 pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
233 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
234
235 spin_unlock_irqrestore(&port->lock, flags);
236
237 return 0;
238}
239
240static void altera_jtaguart_shutdown(struct uart_port *port)
241{
242 struct altera_jtaguart *pp =
243 container_of(port, struct altera_jtaguart, port);
244 unsigned long flags;
245
246 spin_lock_irqsave(&port->lock, flags);
247
248 /* Disable all interrupts now */
249 pp->imr = 0;
250 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
251
252 spin_unlock_irqrestore(&port->lock, flags);
253
254 free_irq(port->irq, port);
255}
256
257static const char *altera_jtaguart_type(struct uart_port *port)
258{
259 return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
260}
261
262static int altera_jtaguart_request_port(struct uart_port *port)
263{
264 /* UARTs always present */
265 return 0;
266}
267
268static void altera_jtaguart_release_port(struct uart_port *port)
269{
270 /* Nothing to release... */
271}
272
273static int altera_jtaguart_verify_port(struct uart_port *port,
274 struct serial_struct *ser)
275{
276 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
277 return -EINVAL;
278 return 0;
279}
280
281/*
282 * Define the basic serial functions we support.
283 */
284static struct uart_ops altera_jtaguart_ops = {
285 .tx_empty = altera_jtaguart_tx_empty,
286 .get_mctrl = altera_jtaguart_get_mctrl,
287 .set_mctrl = altera_jtaguart_set_mctrl,
288 .start_tx = altera_jtaguart_start_tx,
289 .stop_tx = altera_jtaguart_stop_tx,
290 .stop_rx = altera_jtaguart_stop_rx,
291 .enable_ms = altera_jtaguart_enable_ms,
292 .break_ctl = altera_jtaguart_break_ctl,
293 .startup = altera_jtaguart_startup,
294 .shutdown = altera_jtaguart_shutdown,
295 .set_termios = altera_jtaguart_set_termios,
296 .type = altera_jtaguart_type,
297 .request_port = altera_jtaguart_request_port,
298 .release_port = altera_jtaguart_release_port,
299 .config_port = altera_jtaguart_config_port,
300 .verify_port = altera_jtaguart_verify_port,
301};
302
303#define ALTERA_JTAGUART_MAXPORTS 1
304static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
305
306#if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
307
308#if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
309static void altera_jtaguart_console_putc(struct console *co, const char c)
310{
311 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
312 unsigned long status;
313 unsigned long flags;
314
315 spin_lock_irqsave(&port->lock, flags);
316 while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
317 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
318 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
319 spin_unlock_irqrestore(&port->lock, flags);
320 return; /* no connection activity */
321 }
322 spin_unlock_irqrestore(&port->lock, flags);
323 cpu_relax();
324 spin_lock_irqsave(&port->lock, flags);
325 }
326 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
327 spin_unlock_irqrestore(&port->lock, flags);
328}
329#else
330static void altera_jtaguart_console_putc(struct console *co, const char c)
331{
332 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
333 unsigned long flags;
334
335 spin_lock_irqsave(&port->lock, flags);
336 while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
337 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
338 spin_unlock_irqrestore(&port->lock, flags);
339 cpu_relax();
340 spin_lock_irqsave(&port->lock, flags);
341 }
342 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
343 spin_unlock_irqrestore(&port->lock, flags);
344}
345#endif
346
347static void altera_jtaguart_console_write(struct console *co, const char *s,
348 unsigned int count)
349{
350 for (; count; count--, s++) {
351 altera_jtaguart_console_putc(co, *s);
352 if (*s == '\n')
353 altera_jtaguart_console_putc(co, '\r');
354 }
355}
356
357static int __init altera_jtaguart_console_setup(struct console *co,
358 char *options)
359{
360 struct uart_port *port;
361
362 if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
363 return -EINVAL;
364 port = &altera_jtaguart_ports[co->index].port;
365 if (port->membase == NULL)
366 return -ENODEV;
367 return 0;
368}
369
370static struct uart_driver altera_jtaguart_driver;
371
372static struct console altera_jtaguart_console = {
373 .name = "ttyJ",
374 .write = altera_jtaguart_console_write,
375 .device = uart_console_device,
376 .setup = altera_jtaguart_console_setup,
377 .flags = CON_PRINTBUFFER,
378 .index = -1,
379 .data = &altera_jtaguart_driver,
380};
381
382static int __init altera_jtaguart_console_init(void)
383{
384 register_console(&altera_jtaguart_console);
385 return 0;
386}
387
388console_initcall(altera_jtaguart_console_init);
389
390#define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
391
392#else
393
394#define ALTERA_JTAGUART_CONSOLE NULL
395
396#endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */
397
398static struct uart_driver altera_jtaguart_driver = {
399 .owner = THIS_MODULE,
400 .driver_name = "altera_jtaguart",
401 .dev_name = "ttyJ",
402 .major = ALTERA_JTAGUART_MAJOR,
403 .minor = ALTERA_JTAGUART_MINOR,
404 .nr = ALTERA_JTAGUART_MAXPORTS,
405 .cons = ALTERA_JTAGUART_CONSOLE,
406};
407
408static int __devinit altera_jtaguart_probe(struct platform_device *pdev)
409{
410 struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data;
411 struct uart_port *port;
412 struct resource *res_irq, *res_mem;
413 int i = pdev->id;
414
415 /* -1 emphasizes that the platform must have one port, no .N suffix */
416 if (i == -1)
417 i = 0;
418
419 if (i >= ALTERA_JTAGUART_MAXPORTS)
420 return -EINVAL;
421
422 port = &altera_jtaguart_ports[i].port;
423
424 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
425 if (res_mem)
426 port->mapbase = res_mem->start;
427 else if (platp)
428 port->mapbase = platp->mapbase;
429 else
430 return -ENODEV;
431
432 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
433 if (res_irq)
434 port->irq = res_irq->start;
435 else if (platp)
436 port->irq = platp->irq;
437 else
438 return -ENODEV;
439
440 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
441 if (!port->membase)
442 return -ENOMEM;
443
444 port->line = i;
445 port->type = PORT_ALTERA_JTAGUART;
446 port->iotype = SERIAL_IO_MEM;
447 port->ops = &altera_jtaguart_ops;
448 port->flags = UPF_BOOT_AUTOCONF;
449
450 uart_add_one_port(&altera_jtaguart_driver, port);
451
452 return 0;
453}
454
455static int __devexit altera_jtaguart_remove(struct platform_device *pdev)
456{
457 struct uart_port *port;
458 int i = pdev->id;
459
460 if (i == -1)
461 i = 0;
462
463 port = &altera_jtaguart_ports[i].port;
464 uart_remove_one_port(&altera_jtaguart_driver, port);
465
466 return 0;
467}
468
469#ifdef CONFIG_OF
470static struct of_device_id altera_jtaguart_match[] = {
471 { .compatible = "ALTR,juart-1.0", },
472 {},
473};
474MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
475#else
476#define altera_jtaguart_match NULL
477#endif /* CONFIG_OF */
478
479static struct platform_driver altera_jtaguart_platform_driver = {
480 .probe = altera_jtaguart_probe,
481 .remove = __devexit_p(altera_jtaguart_remove),
482 .driver = {
483 .name = DRV_NAME,
484 .owner = THIS_MODULE,
485 .of_match_table = altera_jtaguart_match,
486 },
487};
488
489static int __init altera_jtaguart_init(void)
490{
491 int rc;
492
493 rc = uart_register_driver(&altera_jtaguart_driver);
494 if (rc)
495 return rc;
496 rc = platform_driver_register(&altera_jtaguart_platform_driver);
497 if (rc) {
498 uart_unregister_driver(&altera_jtaguart_driver);
499 return rc;
500 }
501 return 0;
502}
503
504static void __exit altera_jtaguart_exit(void)
505{
506 platform_driver_unregister(&altera_jtaguart_platform_driver);
507 uart_unregister_driver(&altera_jtaguart_driver);
508}
509
510module_init(altera_jtaguart_init);
511module_exit(altera_jtaguart_exit);
512
513MODULE_DESCRIPTION("Altera JTAG UART driver");
514MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
515MODULE_LICENSE("GPL");
516MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/drivers/tty/serial/altera_uart.c b/drivers/tty/serial/altera_uart.c
new file mode 100644
index 000000000000..50bc5a5ac653
--- /dev/null
+++ b/drivers/tty/serial/altera_uart.c
@@ -0,0 +1,661 @@
1/*
2 * altera_uart.c -- Altera UART driver
3 *
4 * Based on mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
8 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/timer.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/console.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/serial.h>
25#include <linux/serial_core.h>
26#include <linux/platform_device.h>
27#include <linux/of.h>
28#include <linux/io.h>
29#include <linux/altera_uart.h>
30
31#define DRV_NAME "altera_uart"
32#define SERIAL_ALTERA_MAJOR 204
33#define SERIAL_ALTERA_MINOR 213
34
35/*
36 * Altera UART register definitions according to the Nios UART datasheet:
37 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
38 */
39
40#define ALTERA_UART_SIZE 32
41
42#define ALTERA_UART_RXDATA_REG 0
43#define ALTERA_UART_TXDATA_REG 4
44#define ALTERA_UART_STATUS_REG 8
45#define ALTERA_UART_CONTROL_REG 12
46#define ALTERA_UART_DIVISOR_REG 16
47#define ALTERA_UART_EOP_REG 20
48
49#define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
50#define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
51#define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
52#define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
53#define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
54#define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
55#define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
56#define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
57#define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
58#define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
59#define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
60#define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
61
62 /* Enable interrupt on... */
63#define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
64#define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
65#define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
66#define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
67#define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
68#define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
69#define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
70#define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
71#define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
72
73#define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
74#define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
75#define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
76#define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
77
78/*
79 * Local per-uart structure.
80 */
81struct altera_uart {
82 struct uart_port port;
83 struct timer_list tmr;
84 unsigned int sigs; /* Local copy of line sigs */
85 unsigned short imr; /* Local IMR mirror */
86};
87
88static u32 altera_uart_readl(struct uart_port *port, int reg)
89{
90 return readl(port->membase + (reg << port->regshift));
91}
92
93static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
94{
95 writel(dat, port->membase + (reg << port->regshift));
96}
97
98static unsigned int altera_uart_tx_empty(struct uart_port *port)
99{
100 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
101 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
102}
103
104static unsigned int altera_uart_get_mctrl(struct uart_port *port)
105{
106 struct altera_uart *pp = container_of(port, struct altera_uart, port);
107 unsigned int sigs;
108
109 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
110 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
111 sigs |= (pp->sigs & TIOCM_RTS);
112
113 return sigs;
114}
115
116static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
117{
118 struct altera_uart *pp = container_of(port, struct altera_uart, port);
119
120 pp->sigs = sigs;
121 if (sigs & TIOCM_RTS)
122 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
123 else
124 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
125 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
126}
127
128static void altera_uart_start_tx(struct uart_port *port)
129{
130 struct altera_uart *pp = container_of(port, struct altera_uart, port);
131
132 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
133 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
134}
135
136static void altera_uart_stop_tx(struct uart_port *port)
137{
138 struct altera_uart *pp = container_of(port, struct altera_uart, port);
139
140 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
141 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
142}
143
144static void altera_uart_stop_rx(struct uart_port *port)
145{
146 struct altera_uart *pp = container_of(port, struct altera_uart, port);
147
148 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
149 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
150}
151
152static void altera_uart_break_ctl(struct uart_port *port, int break_state)
153{
154 struct altera_uart *pp = container_of(port, struct altera_uart, port);
155 unsigned long flags;
156
157 spin_lock_irqsave(&port->lock, flags);
158 if (break_state == -1)
159 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
160 else
161 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
162 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
163 spin_unlock_irqrestore(&port->lock, flags);
164}
165
166static void altera_uart_enable_ms(struct uart_port *port)
167{
168}
169
170static void altera_uart_set_termios(struct uart_port *port,
171 struct ktermios *termios,
172 struct ktermios *old)
173{
174 unsigned long flags;
175 unsigned int baud, baudclk;
176
177 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
178 baudclk = port->uartclk / baud;
179
180 if (old)
181 tty_termios_copy_hw(termios, old);
182 tty_termios_encode_baud_rate(termios, baud, baud);
183
184 spin_lock_irqsave(&port->lock, flags);
185 uart_update_timeout(port, termios->c_cflag, baud);
186 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
187 spin_unlock_irqrestore(&port->lock, flags);
188}
189
190static void altera_uart_rx_chars(struct altera_uart *pp)
191{
192 struct uart_port *port = &pp->port;
193 unsigned char ch, flag;
194 unsigned short status;
195
196 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
197 ALTERA_UART_STATUS_RRDY_MSK) {
198 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
199 flag = TTY_NORMAL;
200 port->icount.rx++;
201
202 if (status & ALTERA_UART_STATUS_E_MSK) {
203 altera_uart_writel(port, status,
204 ALTERA_UART_STATUS_REG);
205
206 if (status & ALTERA_UART_STATUS_BRK_MSK) {
207 port->icount.brk++;
208 if (uart_handle_break(port))
209 continue;
210 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
211 port->icount.parity++;
212 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
213 port->icount.overrun++;
214 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
215 port->icount.frame++;
216 }
217
218 status &= port->read_status_mask;
219
220 if (status & ALTERA_UART_STATUS_BRK_MSK)
221 flag = TTY_BREAK;
222 else if (status & ALTERA_UART_STATUS_PE_MSK)
223 flag = TTY_PARITY;
224 else if (status & ALTERA_UART_STATUS_FE_MSK)
225 flag = TTY_FRAME;
226 }
227
228 if (uart_handle_sysrq_char(port, ch))
229 continue;
230 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
231 flag);
232 }
233
234 tty_flip_buffer_push(port->state->port.tty);
235}
236
237static void altera_uart_tx_chars(struct altera_uart *pp)
238{
239 struct uart_port *port = &pp->port;
240 struct circ_buf *xmit = &port->state->xmit;
241
242 if (port->x_char) {
243 /* Send special char - probably flow control */
244 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
245 port->x_char = 0;
246 port->icount.tx++;
247 return;
248 }
249
250 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
251 ALTERA_UART_STATUS_TRDY_MSK) {
252 if (xmit->head == xmit->tail)
253 break;
254 altera_uart_writel(port, xmit->buf[xmit->tail],
255 ALTERA_UART_TXDATA_REG);
256 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
257 port->icount.tx++;
258 }
259
260 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
261 uart_write_wakeup(port);
262
263 if (xmit->head == xmit->tail) {
264 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
265 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
266 }
267}
268
269static irqreturn_t altera_uart_interrupt(int irq, void *data)
270{
271 struct uart_port *port = data;
272 struct altera_uart *pp = container_of(port, struct altera_uart, port);
273 unsigned int isr;
274
275 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
276
277 spin_lock(&port->lock);
278 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
279 altera_uart_rx_chars(pp);
280 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
281 altera_uart_tx_chars(pp);
282 spin_unlock(&port->lock);
283
284 return IRQ_RETVAL(isr);
285}
286
287static void altera_uart_timer(unsigned long data)
288{
289 struct uart_port *port = (void *)data;
290 struct altera_uart *pp = container_of(port, struct altera_uart, port);
291
292 altera_uart_interrupt(0, port);
293 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
294}
295
296static void altera_uart_config_port(struct uart_port *port, int flags)
297{
298 port->type = PORT_ALTERA_UART;
299
300 /* Clear mask, so no surprise interrupts. */
301 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
302 /* Clear status register */
303 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
304}
305
306static int altera_uart_startup(struct uart_port *port)
307{
308 struct altera_uart *pp = container_of(port, struct altera_uart, port);
309 unsigned long flags;
310 int ret;
311
312 if (!port->irq) {
313 setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
314 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
315 return 0;
316 }
317
318 ret = request_irq(port->irq, altera_uart_interrupt, IRQF_DISABLED,
319 DRV_NAME, port);
320 if (ret) {
321 pr_err(DRV_NAME ": unable to attach Altera UART %d "
322 "interrupt vector=%d\n", port->line, port->irq);
323 return ret;
324 }
325
326 spin_lock_irqsave(&port->lock, flags);
327
328 /* Enable RX interrupts now */
329 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
330 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
331
332 spin_unlock_irqrestore(&port->lock, flags);
333
334 return 0;
335}
336
337static void altera_uart_shutdown(struct uart_port *port)
338{
339 struct altera_uart *pp = container_of(port, struct altera_uart, port);
340 unsigned long flags;
341
342 spin_lock_irqsave(&port->lock, flags);
343
344 /* Disable all interrupts now */
345 pp->imr = 0;
346 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
347
348 spin_unlock_irqrestore(&port->lock, flags);
349
350 if (port->irq)
351 free_irq(port->irq, port);
352 else
353 del_timer_sync(&pp->tmr);
354}
355
356static const char *altera_uart_type(struct uart_port *port)
357{
358 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
359}
360
361static int altera_uart_request_port(struct uart_port *port)
362{
363 /* UARTs always present */
364 return 0;
365}
366
367static void altera_uart_release_port(struct uart_port *port)
368{
369 /* Nothing to release... */
370}
371
372static int altera_uart_verify_port(struct uart_port *port,
373 struct serial_struct *ser)
374{
375 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
376 return -EINVAL;
377 return 0;
378}
379
380/*
381 * Define the basic serial functions we support.
382 */
383static struct uart_ops altera_uart_ops = {
384 .tx_empty = altera_uart_tx_empty,
385 .get_mctrl = altera_uart_get_mctrl,
386 .set_mctrl = altera_uart_set_mctrl,
387 .start_tx = altera_uart_start_tx,
388 .stop_tx = altera_uart_stop_tx,
389 .stop_rx = altera_uart_stop_rx,
390 .enable_ms = altera_uart_enable_ms,
391 .break_ctl = altera_uart_break_ctl,
392 .startup = altera_uart_startup,
393 .shutdown = altera_uart_shutdown,
394 .set_termios = altera_uart_set_termios,
395 .type = altera_uart_type,
396 .request_port = altera_uart_request_port,
397 .release_port = altera_uart_release_port,
398 .config_port = altera_uart_config_port,
399 .verify_port = altera_uart_verify_port,
400};
401
402static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
403
404#if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
405
406int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
407{
408 struct uart_port *port;
409 int i;
410
411 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
412 port = &altera_uart_ports[i].port;
413
414 port->line = i;
415 port->type = PORT_ALTERA_UART;
416 port->mapbase = platp[i].mapbase;
417 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
418 port->iotype = SERIAL_IO_MEM;
419 port->irq = platp[i].irq;
420 port->uartclk = platp[i].uartclk;
421 port->flags = UPF_BOOT_AUTOCONF;
422 port->ops = &altera_uart_ops;
423 port->private_data = platp;
424 }
425
426 return 0;
427}
428
429static void altera_uart_console_putc(struct uart_port *port, const char c)
430{
431 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
432 ALTERA_UART_STATUS_TRDY_MSK))
433 cpu_relax();
434
435 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
436}
437
438static void altera_uart_console_write(struct console *co, const char *s,
439 unsigned int count)
440{
441 struct uart_port *port = &(altera_uart_ports + co->index)->port;
442
443 for (; count; count--, s++) {
444 altera_uart_console_putc(port, *s);
445 if (*s == '\n')
446 altera_uart_console_putc(port, '\r');
447 }
448}
449
450static int __init altera_uart_console_setup(struct console *co, char *options)
451{
452 struct uart_port *port;
453 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
454 int bits = 8;
455 int parity = 'n';
456 int flow = 'n';
457
458 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
459 return -EINVAL;
460 port = &altera_uart_ports[co->index].port;
461 if (!port->membase)
462 return -ENODEV;
463
464 if (options)
465 uart_parse_options(options, &baud, &parity, &bits, &flow);
466
467 return uart_set_options(port, co, baud, parity, bits, flow);
468}
469
470static struct uart_driver altera_uart_driver;
471
472static struct console altera_uart_console = {
473 .name = "ttyAL",
474 .write = altera_uart_console_write,
475 .device = uart_console_device,
476 .setup = altera_uart_console_setup,
477 .flags = CON_PRINTBUFFER,
478 .index = -1,
479 .data = &altera_uart_driver,
480};
481
482static int __init altera_uart_console_init(void)
483{
484 register_console(&altera_uart_console);
485 return 0;
486}
487
488console_initcall(altera_uart_console_init);
489
490#define ALTERA_UART_CONSOLE (&altera_uart_console)
491
492#else
493
494#define ALTERA_UART_CONSOLE NULL
495
496#endif /* CONFIG_ALTERA_UART_CONSOLE */
497
498/*
499 * Define the altera_uart UART driver structure.
500 */
501static struct uart_driver altera_uart_driver = {
502 .owner = THIS_MODULE,
503 .driver_name = DRV_NAME,
504 .dev_name = "ttyAL",
505 .major = SERIAL_ALTERA_MAJOR,
506 .minor = SERIAL_ALTERA_MINOR,
507 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
508 .cons = ALTERA_UART_CONSOLE,
509};
510
511#ifdef CONFIG_OF
512static int altera_uart_get_of_uartclk(struct platform_device *pdev,
513 struct uart_port *port)
514{
515 int len;
516 const __be32 *clk;
517
518 clk = of_get_property(pdev->dev.of_node, "clock-frequency", &len);
519 if (!clk || len < sizeof(__be32))
520 return -ENODEV;
521
522 port->uartclk = be32_to_cpup(clk);
523
524 return 0;
525}
526#else
527static int altera_uart_get_of_uartclk(struct platform_device *pdev,
528 struct uart_port *port)
529{
530 return -ENODEV;
531}
532#endif /* CONFIG_OF */
533
534static int __devinit altera_uart_probe(struct platform_device *pdev)
535{
536 struct altera_uart_platform_uart *platp = pdev->dev.platform_data;
537 struct uart_port *port;
538 struct resource *res_mem;
539 struct resource *res_irq;
540 int i = pdev->id;
541 int ret;
542
543 /* if id is -1 scan for a free id and use that one */
544 if (i == -1) {
545 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
546 if (altera_uart_ports[i].port.mapbase == 0)
547 break;
548 }
549
550 if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
551 return -EINVAL;
552
553 port = &altera_uart_ports[i].port;
554
555 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
556 if (res_mem)
557 port->mapbase = res_mem->start;
558 else if (platp->mapbase)
559 port->mapbase = platp->mapbase;
560 else
561 return -EINVAL;
562
563 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
564 if (res_irq)
565 port->irq = res_irq->start;
566 else if (platp->irq)
567 port->irq = platp->irq;
568
569 /* Check platform data first so we can override device node data */
570 if (platp)
571 port->uartclk = platp->uartclk;
572 else {
573 ret = altera_uart_get_of_uartclk(pdev, port);
574 if (ret)
575 return ret;
576 }
577
578 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
579 if (!port->membase)
580 return -ENOMEM;
581
582 if (platp)
583 port->regshift = platp->bus_shift;
584 else
585 port->regshift = 0;
586
587 port->line = i;
588 port->type = PORT_ALTERA_UART;
589 port->iotype = SERIAL_IO_MEM;
590 port->ops = &altera_uart_ops;
591 port->flags = UPF_BOOT_AUTOCONF;
592
593 dev_set_drvdata(&pdev->dev, port);
594
595 uart_add_one_port(&altera_uart_driver, port);
596
597 return 0;
598}
599
600static int __devexit altera_uart_remove(struct platform_device *pdev)
601{
602 struct uart_port *port = dev_get_drvdata(&pdev->dev);
603
604 if (port) {
605 uart_remove_one_port(&altera_uart_driver, port);
606 dev_set_drvdata(&pdev->dev, NULL);
607 port->mapbase = 0;
608 }
609
610 return 0;
611}
612
613#ifdef CONFIG_OF
614static struct of_device_id altera_uart_match[] = {
615 { .compatible = "ALTR,uart-1.0", },
616 {},
617};
618MODULE_DEVICE_TABLE(of, altera_uart_match);
619#else
620#define altera_uart_match NULL
621#endif /* CONFIG_OF */
622
623static struct platform_driver altera_uart_platform_driver = {
624 .probe = altera_uart_probe,
625 .remove = __devexit_p(altera_uart_remove),
626 .driver = {
627 .name = DRV_NAME,
628 .owner = THIS_MODULE,
629 .of_match_table = altera_uart_match,
630 },
631};
632
633static int __init altera_uart_init(void)
634{
635 int rc;
636
637 rc = uart_register_driver(&altera_uart_driver);
638 if (rc)
639 return rc;
640 rc = platform_driver_register(&altera_uart_platform_driver);
641 if (rc) {
642 uart_unregister_driver(&altera_uart_driver);
643 return rc;
644 }
645 return 0;
646}
647
648static void __exit altera_uart_exit(void)
649{
650 platform_driver_unregister(&altera_uart_platform_driver);
651 uart_unregister_driver(&altera_uart_driver);
652}
653
654module_init(altera_uart_init);
655module_exit(altera_uart_exit);
656
657MODULE_DESCRIPTION("Altera UART driver");
658MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
659MODULE_LICENSE("GPL");
660MODULE_ALIAS("platform:" DRV_NAME);
661MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);
diff --git a/drivers/tty/serial/amba-pl010.c b/drivers/tty/serial/amba-pl010.c
new file mode 100644
index 000000000000..c0d10c4ddb73
--- /dev/null
+++ b/drivers/tty/serial/amba-pl010.c
@@ -0,0 +1,823 @@
1/*
2 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * This is a generic driver for ARM AMBA-type serial ports. They
24 * have a lot of 16550-like features, but are not register compatible.
25 * Note that although they do have CTS, DCD and DSR inputs, they do
26 * not have an RI input, nor do they have DTR or RTS outputs. If
27 * required, these have to be supplied via some other means (eg, GPIO)
28 * and hooked into this driver.
29 */
30
31#if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/module.h>
36#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/device.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
45#include <linux/amba/bus.h>
46#include <linux/amba/serial.h>
47#include <linux/clk.h>
48#include <linux/slab.h>
49
50#include <asm/io.h>
51
52#define UART_NR 8
53
54#define SERIAL_AMBA_MAJOR 204
55#define SERIAL_AMBA_MINOR 16
56#define SERIAL_AMBA_NR UART_NR
57
58#define AMBA_ISR_PASS_LIMIT 256
59
60#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
61#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
62
63#define UART_DUMMY_RSR_RX 256
64#define UART_PORT_SIZE 64
65
66/*
67 * We wrap our port structure around the generic uart_port.
68 */
69struct uart_amba_port {
70 struct uart_port port;
71 struct clk *clk;
72 struct amba_device *dev;
73 struct amba_pl010_data *data;
74 unsigned int old_status;
75};
76
77static void pl010_stop_tx(struct uart_port *port)
78{
79 struct uart_amba_port *uap = (struct uart_amba_port *)port;
80 unsigned int cr;
81
82 cr = readb(uap->port.membase + UART010_CR);
83 cr &= ~UART010_CR_TIE;
84 writel(cr, uap->port.membase + UART010_CR);
85}
86
87static void pl010_start_tx(struct uart_port *port)
88{
89 struct uart_amba_port *uap = (struct uart_amba_port *)port;
90 unsigned int cr;
91
92 cr = readb(uap->port.membase + UART010_CR);
93 cr |= UART010_CR_TIE;
94 writel(cr, uap->port.membase + UART010_CR);
95}
96
97static void pl010_stop_rx(struct uart_port *port)
98{
99 struct uart_amba_port *uap = (struct uart_amba_port *)port;
100 unsigned int cr;
101
102 cr = readb(uap->port.membase + UART010_CR);
103 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
104 writel(cr, uap->port.membase + UART010_CR);
105}
106
107static void pl010_enable_ms(struct uart_port *port)
108{
109 struct uart_amba_port *uap = (struct uart_amba_port *)port;
110 unsigned int cr;
111
112 cr = readb(uap->port.membase + UART010_CR);
113 cr |= UART010_CR_MSIE;
114 writel(cr, uap->port.membase + UART010_CR);
115}
116
117static void pl010_rx_chars(struct uart_amba_port *uap)
118{
119 struct tty_struct *tty = uap->port.state->port.tty;
120 unsigned int status, ch, flag, rsr, max_count = 256;
121
122 status = readb(uap->port.membase + UART01x_FR);
123 while (UART_RX_DATA(status) && max_count--) {
124 ch = readb(uap->port.membase + UART01x_DR);
125 flag = TTY_NORMAL;
126
127 uap->port.icount.rx++;
128
129 /*
130 * Note that the error handling code is
131 * out of the main execution path
132 */
133 rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
134 if (unlikely(rsr & UART01x_RSR_ANY)) {
135 writel(0, uap->port.membase + UART01x_ECR);
136
137 if (rsr & UART01x_RSR_BE) {
138 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
139 uap->port.icount.brk++;
140 if (uart_handle_break(&uap->port))
141 goto ignore_char;
142 } else if (rsr & UART01x_RSR_PE)
143 uap->port.icount.parity++;
144 else if (rsr & UART01x_RSR_FE)
145 uap->port.icount.frame++;
146 if (rsr & UART01x_RSR_OE)
147 uap->port.icount.overrun++;
148
149 rsr &= uap->port.read_status_mask;
150
151 if (rsr & UART01x_RSR_BE)
152 flag = TTY_BREAK;
153 else if (rsr & UART01x_RSR_PE)
154 flag = TTY_PARITY;
155 else if (rsr & UART01x_RSR_FE)
156 flag = TTY_FRAME;
157 }
158
159 if (uart_handle_sysrq_char(&uap->port, ch))
160 goto ignore_char;
161
162 uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
163
164 ignore_char:
165 status = readb(uap->port.membase + UART01x_FR);
166 }
167 spin_unlock(&uap->port.lock);
168 tty_flip_buffer_push(tty);
169 spin_lock(&uap->port.lock);
170}
171
172static void pl010_tx_chars(struct uart_amba_port *uap)
173{
174 struct circ_buf *xmit = &uap->port.state->xmit;
175 int count;
176
177 if (uap->port.x_char) {
178 writel(uap->port.x_char, uap->port.membase + UART01x_DR);
179 uap->port.icount.tx++;
180 uap->port.x_char = 0;
181 return;
182 }
183 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
184 pl010_stop_tx(&uap->port);
185 return;
186 }
187
188 count = uap->port.fifosize >> 1;
189 do {
190 writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
191 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
192 uap->port.icount.tx++;
193 if (uart_circ_empty(xmit))
194 break;
195 } while (--count > 0);
196
197 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
198 uart_write_wakeup(&uap->port);
199
200 if (uart_circ_empty(xmit))
201 pl010_stop_tx(&uap->port);
202}
203
204static void pl010_modem_status(struct uart_amba_port *uap)
205{
206 unsigned int status, delta;
207
208 writel(0, uap->port.membase + UART010_ICR);
209
210 status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
211
212 delta = status ^ uap->old_status;
213 uap->old_status = status;
214
215 if (!delta)
216 return;
217
218 if (delta & UART01x_FR_DCD)
219 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
220
221 if (delta & UART01x_FR_DSR)
222 uap->port.icount.dsr++;
223
224 if (delta & UART01x_FR_CTS)
225 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
226
227 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
228}
229
230static irqreturn_t pl010_int(int irq, void *dev_id)
231{
232 struct uart_amba_port *uap = dev_id;
233 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
234 int handled = 0;
235
236 spin_lock(&uap->port.lock);
237
238 status = readb(uap->port.membase + UART010_IIR);
239 if (status) {
240 do {
241 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
242 pl010_rx_chars(uap);
243 if (status & UART010_IIR_MIS)
244 pl010_modem_status(uap);
245 if (status & UART010_IIR_TIS)
246 pl010_tx_chars(uap);
247
248 if (pass_counter-- == 0)
249 break;
250
251 status = readb(uap->port.membase + UART010_IIR);
252 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
253 UART010_IIR_TIS));
254 handled = 1;
255 }
256
257 spin_unlock(&uap->port.lock);
258
259 return IRQ_RETVAL(handled);
260}
261
262static unsigned int pl010_tx_empty(struct uart_port *port)
263{
264 struct uart_amba_port *uap = (struct uart_amba_port *)port;
265 unsigned int status = readb(uap->port.membase + UART01x_FR);
266 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
267}
268
269static unsigned int pl010_get_mctrl(struct uart_port *port)
270{
271 struct uart_amba_port *uap = (struct uart_amba_port *)port;
272 unsigned int result = 0;
273 unsigned int status;
274
275 status = readb(uap->port.membase + UART01x_FR);
276 if (status & UART01x_FR_DCD)
277 result |= TIOCM_CAR;
278 if (status & UART01x_FR_DSR)
279 result |= TIOCM_DSR;
280 if (status & UART01x_FR_CTS)
281 result |= TIOCM_CTS;
282
283 return result;
284}
285
286static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
287{
288 struct uart_amba_port *uap = (struct uart_amba_port *)port;
289
290 if (uap->data)
291 uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
292}
293
294static void pl010_break_ctl(struct uart_port *port, int break_state)
295{
296 struct uart_amba_port *uap = (struct uart_amba_port *)port;
297 unsigned long flags;
298 unsigned int lcr_h;
299
300 spin_lock_irqsave(&uap->port.lock, flags);
301 lcr_h = readb(uap->port.membase + UART010_LCRH);
302 if (break_state == -1)
303 lcr_h |= UART01x_LCRH_BRK;
304 else
305 lcr_h &= ~UART01x_LCRH_BRK;
306 writel(lcr_h, uap->port.membase + UART010_LCRH);
307 spin_unlock_irqrestore(&uap->port.lock, flags);
308}
309
310static int pl010_startup(struct uart_port *port)
311{
312 struct uart_amba_port *uap = (struct uart_amba_port *)port;
313 int retval;
314
315 /*
316 * Try to enable the clock producer.
317 */
318 retval = clk_enable(uap->clk);
319 if (retval)
320 goto out;
321
322 uap->port.uartclk = clk_get_rate(uap->clk);
323
324 /*
325 * Allocate the IRQ
326 */
327 retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
328 if (retval)
329 goto clk_dis;
330
331 /*
332 * initialise the old status of the modem signals
333 */
334 uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
335
336 /*
337 * Finally, enable interrupts
338 */
339 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
340 uap->port.membase + UART010_CR);
341
342 return 0;
343
344 clk_dis:
345 clk_disable(uap->clk);
346 out:
347 return retval;
348}
349
350static void pl010_shutdown(struct uart_port *port)
351{
352 struct uart_amba_port *uap = (struct uart_amba_port *)port;
353
354 /*
355 * Free the interrupt
356 */
357 free_irq(uap->port.irq, uap);
358
359 /*
360 * disable all interrupts, disable the port
361 */
362 writel(0, uap->port.membase + UART010_CR);
363
364 /* disable break condition and fifos */
365 writel(readb(uap->port.membase + UART010_LCRH) &
366 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
367 uap->port.membase + UART010_LCRH);
368
369 /*
370 * Shut down the clock producer
371 */
372 clk_disable(uap->clk);
373}
374
375static void
376pl010_set_termios(struct uart_port *port, struct ktermios *termios,
377 struct ktermios *old)
378{
379 struct uart_amba_port *uap = (struct uart_amba_port *)port;
380 unsigned int lcr_h, old_cr;
381 unsigned long flags;
382 unsigned int baud, quot;
383
384 /*
385 * Ask the core to calculate the divisor for us.
386 */
387 baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
388 quot = uart_get_divisor(port, baud);
389
390 switch (termios->c_cflag & CSIZE) {
391 case CS5:
392 lcr_h = UART01x_LCRH_WLEN_5;
393 break;
394 case CS6:
395 lcr_h = UART01x_LCRH_WLEN_6;
396 break;
397 case CS7:
398 lcr_h = UART01x_LCRH_WLEN_7;
399 break;
400 default: // CS8
401 lcr_h = UART01x_LCRH_WLEN_8;
402 break;
403 }
404 if (termios->c_cflag & CSTOPB)
405 lcr_h |= UART01x_LCRH_STP2;
406 if (termios->c_cflag & PARENB) {
407 lcr_h |= UART01x_LCRH_PEN;
408 if (!(termios->c_cflag & PARODD))
409 lcr_h |= UART01x_LCRH_EPS;
410 }
411 if (uap->port.fifosize > 1)
412 lcr_h |= UART01x_LCRH_FEN;
413
414 spin_lock_irqsave(&uap->port.lock, flags);
415
416 /*
417 * Update the per-port timeout.
418 */
419 uart_update_timeout(port, termios->c_cflag, baud);
420
421 uap->port.read_status_mask = UART01x_RSR_OE;
422 if (termios->c_iflag & INPCK)
423 uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
424 if (termios->c_iflag & (BRKINT | PARMRK))
425 uap->port.read_status_mask |= UART01x_RSR_BE;
426
427 /*
428 * Characters to ignore
429 */
430 uap->port.ignore_status_mask = 0;
431 if (termios->c_iflag & IGNPAR)
432 uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
433 if (termios->c_iflag & IGNBRK) {
434 uap->port.ignore_status_mask |= UART01x_RSR_BE;
435 /*
436 * If we're ignoring parity and break indicators,
437 * ignore overruns too (for real raw support).
438 */
439 if (termios->c_iflag & IGNPAR)
440 uap->port.ignore_status_mask |= UART01x_RSR_OE;
441 }
442
443 /*
444 * Ignore all characters if CREAD is not set.
445 */
446 if ((termios->c_cflag & CREAD) == 0)
447 uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
448
449 /* first, disable everything */
450 old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
451
452 if (UART_ENABLE_MS(port, termios->c_cflag))
453 old_cr |= UART010_CR_MSIE;
454
455 writel(0, uap->port.membase + UART010_CR);
456
457 /* Set baud rate */
458 quot -= 1;
459 writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
460 writel(quot & 0xff, uap->port.membase + UART010_LCRL);
461
462 /*
463 * ----------v----------v----------v----------v-----
464 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
465 * ----------^----------^----------^----------^-----
466 */
467 writel(lcr_h, uap->port.membase + UART010_LCRH);
468 writel(old_cr, uap->port.membase + UART010_CR);
469
470 spin_unlock_irqrestore(&uap->port.lock, flags);
471}
472
473static void pl010_set_ldisc(struct uart_port *port, int new)
474{
475 if (new == N_PPS) {
476 port->flags |= UPF_HARDPPS_CD;
477 pl010_enable_ms(port);
478 } else
479 port->flags &= ~UPF_HARDPPS_CD;
480}
481
482static const char *pl010_type(struct uart_port *port)
483{
484 return port->type == PORT_AMBA ? "AMBA" : NULL;
485}
486
487/*
488 * Release the memory region(s) being used by 'port'
489 */
490static void pl010_release_port(struct uart_port *port)
491{
492 release_mem_region(port->mapbase, UART_PORT_SIZE);
493}
494
495/*
496 * Request the memory region(s) being used by 'port'
497 */
498static int pl010_request_port(struct uart_port *port)
499{
500 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
501 != NULL ? 0 : -EBUSY;
502}
503
504/*
505 * Configure/autoconfigure the port.
506 */
507static void pl010_config_port(struct uart_port *port, int flags)
508{
509 if (flags & UART_CONFIG_TYPE) {
510 port->type = PORT_AMBA;
511 pl010_request_port(port);
512 }
513}
514
515/*
516 * verify the new serial_struct (for TIOCSSERIAL).
517 */
518static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
519{
520 int ret = 0;
521 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
522 ret = -EINVAL;
523 if (ser->irq < 0 || ser->irq >= nr_irqs)
524 ret = -EINVAL;
525 if (ser->baud_base < 9600)
526 ret = -EINVAL;
527 return ret;
528}
529
530static struct uart_ops amba_pl010_pops = {
531 .tx_empty = pl010_tx_empty,
532 .set_mctrl = pl010_set_mctrl,
533 .get_mctrl = pl010_get_mctrl,
534 .stop_tx = pl010_stop_tx,
535 .start_tx = pl010_start_tx,
536 .stop_rx = pl010_stop_rx,
537 .enable_ms = pl010_enable_ms,
538 .break_ctl = pl010_break_ctl,
539 .startup = pl010_startup,
540 .shutdown = pl010_shutdown,
541 .set_termios = pl010_set_termios,
542 .set_ldisc = pl010_set_ldisc,
543 .type = pl010_type,
544 .release_port = pl010_release_port,
545 .request_port = pl010_request_port,
546 .config_port = pl010_config_port,
547 .verify_port = pl010_verify_port,
548};
549
550static struct uart_amba_port *amba_ports[UART_NR];
551
552#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
553
554static void pl010_console_putchar(struct uart_port *port, int ch)
555{
556 struct uart_amba_port *uap = (struct uart_amba_port *)port;
557 unsigned int status;
558
559 do {
560 status = readb(uap->port.membase + UART01x_FR);
561 barrier();
562 } while (!UART_TX_READY(status));
563 writel(ch, uap->port.membase + UART01x_DR);
564}
565
566static void
567pl010_console_write(struct console *co, const char *s, unsigned int count)
568{
569 struct uart_amba_port *uap = amba_ports[co->index];
570 unsigned int status, old_cr;
571
572 clk_enable(uap->clk);
573
574 /*
575 * First save the CR then disable the interrupts
576 */
577 old_cr = readb(uap->port.membase + UART010_CR);
578 writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
579
580 uart_console_write(&uap->port, s, count, pl010_console_putchar);
581
582 /*
583 * Finally, wait for transmitter to become empty
584 * and restore the TCR
585 */
586 do {
587 status = readb(uap->port.membase + UART01x_FR);
588 barrier();
589 } while (status & UART01x_FR_BUSY);
590 writel(old_cr, uap->port.membase + UART010_CR);
591
592 clk_disable(uap->clk);
593}
594
595static void __init
596pl010_console_get_options(struct uart_amba_port *uap, int *baud,
597 int *parity, int *bits)
598{
599 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
600 unsigned int lcr_h, quot;
601 lcr_h = readb(uap->port.membase + UART010_LCRH);
602
603 *parity = 'n';
604 if (lcr_h & UART01x_LCRH_PEN) {
605 if (lcr_h & UART01x_LCRH_EPS)
606 *parity = 'e';
607 else
608 *parity = 'o';
609 }
610
611 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
612 *bits = 7;
613 else
614 *bits = 8;
615
616 quot = readb(uap->port.membase + UART010_LCRL) |
617 readb(uap->port.membase + UART010_LCRM) << 8;
618 *baud = uap->port.uartclk / (16 * (quot + 1));
619 }
620}
621
622static int __init pl010_console_setup(struct console *co, char *options)
623{
624 struct uart_amba_port *uap;
625 int baud = 38400;
626 int bits = 8;
627 int parity = 'n';
628 int flow = 'n';
629
630 /*
631 * Check whether an invalid uart number has been specified, and
632 * if so, search for the first available port that does have
633 * console support.
634 */
635 if (co->index >= UART_NR)
636 co->index = 0;
637 uap = amba_ports[co->index];
638 if (!uap)
639 return -ENODEV;
640
641 uap->port.uartclk = clk_get_rate(uap->clk);
642
643 if (options)
644 uart_parse_options(options, &baud, &parity, &bits, &flow);
645 else
646 pl010_console_get_options(uap, &baud, &parity, &bits);
647
648 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
649}
650
651static struct uart_driver amba_reg;
652static struct console amba_console = {
653 .name = "ttyAM",
654 .write = pl010_console_write,
655 .device = uart_console_device,
656 .setup = pl010_console_setup,
657 .flags = CON_PRINTBUFFER,
658 .index = -1,
659 .data = &amba_reg,
660};
661
662#define AMBA_CONSOLE &amba_console
663#else
664#define AMBA_CONSOLE NULL
665#endif
666
667static struct uart_driver amba_reg = {
668 .owner = THIS_MODULE,
669 .driver_name = "ttyAM",
670 .dev_name = "ttyAM",
671 .major = SERIAL_AMBA_MAJOR,
672 .minor = SERIAL_AMBA_MINOR,
673 .nr = UART_NR,
674 .cons = AMBA_CONSOLE,
675};
676
677static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
678{
679 struct uart_amba_port *uap;
680 void __iomem *base;
681 int i, ret;
682
683 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
684 if (amba_ports[i] == NULL)
685 break;
686
687 if (i == ARRAY_SIZE(amba_ports)) {
688 ret = -EBUSY;
689 goto out;
690 }
691
692 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
693 if (!uap) {
694 ret = -ENOMEM;
695 goto out;
696 }
697
698 base = ioremap(dev->res.start, resource_size(&dev->res));
699 if (!base) {
700 ret = -ENOMEM;
701 goto free;
702 }
703
704 uap->clk = clk_get(&dev->dev, NULL);
705 if (IS_ERR(uap->clk)) {
706 ret = PTR_ERR(uap->clk);
707 goto unmap;
708 }
709
710 uap->port.dev = &dev->dev;
711 uap->port.mapbase = dev->res.start;
712 uap->port.membase = base;
713 uap->port.iotype = UPIO_MEM;
714 uap->port.irq = dev->irq[0];
715 uap->port.fifosize = 16;
716 uap->port.ops = &amba_pl010_pops;
717 uap->port.flags = UPF_BOOT_AUTOCONF;
718 uap->port.line = i;
719 uap->dev = dev;
720 uap->data = dev->dev.platform_data;
721
722 amba_ports[i] = uap;
723
724 amba_set_drvdata(dev, uap);
725 ret = uart_add_one_port(&amba_reg, &uap->port);
726 if (ret) {
727 amba_set_drvdata(dev, NULL);
728 amba_ports[i] = NULL;
729 clk_put(uap->clk);
730 unmap:
731 iounmap(base);
732 free:
733 kfree(uap);
734 }
735 out:
736 return ret;
737}
738
739static int pl010_remove(struct amba_device *dev)
740{
741 struct uart_amba_port *uap = amba_get_drvdata(dev);
742 int i;
743
744 amba_set_drvdata(dev, NULL);
745
746 uart_remove_one_port(&amba_reg, &uap->port);
747
748 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
749 if (amba_ports[i] == uap)
750 amba_ports[i] = NULL;
751
752 iounmap(uap->port.membase);
753 clk_put(uap->clk);
754 kfree(uap);
755 return 0;
756}
757
758static int pl010_suspend(struct amba_device *dev, pm_message_t state)
759{
760 struct uart_amba_port *uap = amba_get_drvdata(dev);
761
762 if (uap)
763 uart_suspend_port(&amba_reg, &uap->port);
764
765 return 0;
766}
767
768static int pl010_resume(struct amba_device *dev)
769{
770 struct uart_amba_port *uap = amba_get_drvdata(dev);
771
772 if (uap)
773 uart_resume_port(&amba_reg, &uap->port);
774
775 return 0;
776}
777
778static struct amba_id pl010_ids[] = {
779 {
780 .id = 0x00041010,
781 .mask = 0x000fffff,
782 },
783 { 0, 0 },
784};
785
786static struct amba_driver pl010_driver = {
787 .drv = {
788 .name = "uart-pl010",
789 },
790 .id_table = pl010_ids,
791 .probe = pl010_probe,
792 .remove = pl010_remove,
793 .suspend = pl010_suspend,
794 .resume = pl010_resume,
795};
796
797static int __init pl010_init(void)
798{
799 int ret;
800
801 printk(KERN_INFO "Serial: AMBA driver\n");
802
803 ret = uart_register_driver(&amba_reg);
804 if (ret == 0) {
805 ret = amba_driver_register(&pl010_driver);
806 if (ret)
807 uart_unregister_driver(&amba_reg);
808 }
809 return ret;
810}
811
812static void __exit pl010_exit(void)
813{
814 amba_driver_unregister(&pl010_driver);
815 uart_unregister_driver(&amba_reg);
816}
817
818module_init(pl010_init);
819module_exit(pl010_exit);
820
821MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
822MODULE_DESCRIPTION("ARM AMBA serial port driver");
823MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
new file mode 100644
index 000000000000..f5f6831b0a64
--- /dev/null
+++ b/drivers/tty/serial/amba-pl011.c
@@ -0,0 +1,2027 @@
1/*
2 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 * Copyright (C) 2010 ST-Ericsson SA
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
31
32#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/module.h>
37#include <linux/ioport.h>
38#include <linux/init.h>
39#include <linux/console.h>
40#include <linux/sysrq.h>
41#include <linux/device.h>
42#include <linux/tty.h>
43#include <linux/tty_flip.h>
44#include <linux/serial_core.h>
45#include <linux/serial.h>
46#include <linux/amba/bus.h>
47#include <linux/amba/serial.h>
48#include <linux/clk.h>
49#include <linux/slab.h>
50#include <linux/dmaengine.h>
51#include <linux/dma-mapping.h>
52#include <linux/scatterlist.h>
53#include <linux/delay.h>
54
55#include <asm/io.h>
56#include <asm/sizes.h>
57
58#define UART_NR 14
59
60#define SERIAL_AMBA_MAJOR 204
61#define SERIAL_AMBA_MINOR 64
62#define SERIAL_AMBA_NR UART_NR
63
64#define AMBA_ISR_PASS_LIMIT 256
65
66#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
67#define UART_DUMMY_DR_RX (1 << 16)
68
69
70#define UART_WA_SAVE_NR 14
71
72static void pl011_lockup_wa(unsigned long data);
73static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
74 ST_UART011_DMAWM,
75 ST_UART011_TIMEOUT,
76 ST_UART011_LCRH_RX,
77 UART011_IBRD,
78 UART011_FBRD,
79 ST_UART011_LCRH_TX,
80 UART011_IFLS,
81 ST_UART011_XFCR,
82 ST_UART011_XON1,
83 ST_UART011_XON2,
84 ST_UART011_XOFF1,
85 ST_UART011_XOFF2,
86 UART011_CR,
87 UART011_IMSC
88};
89
90static u32 uart_wa_regdata[UART_WA_SAVE_NR];
91static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
92
93/* There is by now at least one vendor with differing details, so handle it */
94struct vendor_data {
95 unsigned int ifls;
96 unsigned int fifosize;
97 unsigned int lcrh_tx;
98 unsigned int lcrh_rx;
99 bool oversampling;
100 bool interrupt_may_hang; /* vendor-specific */
101 bool dma_threshold;
102};
103
104static struct vendor_data vendor_arm = {
105 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
106 .fifosize = 16,
107 .lcrh_tx = UART011_LCRH,
108 .lcrh_rx = UART011_LCRH,
109 .oversampling = false,
110 .dma_threshold = false,
111};
112
113static struct vendor_data vendor_st = {
114 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
115 .fifosize = 64,
116 .lcrh_tx = ST_UART011_LCRH_TX,
117 .lcrh_rx = ST_UART011_LCRH_RX,
118 .oversampling = true,
119 .interrupt_may_hang = true,
120 .dma_threshold = true,
121};
122
123static struct uart_amba_port *amba_ports[UART_NR];
124
125/* Deals with DMA transactions */
126
127struct pl011_sgbuf {
128 struct scatterlist sg;
129 char *buf;
130};
131
132struct pl011_dmarx_data {
133 struct dma_chan *chan;
134 struct completion complete;
135 bool use_buf_b;
136 struct pl011_sgbuf sgbuf_a;
137 struct pl011_sgbuf sgbuf_b;
138 dma_cookie_t cookie;
139 bool running;
140};
141
142struct pl011_dmatx_data {
143 struct dma_chan *chan;
144 struct scatterlist sg;
145 char *buf;
146 bool queued;
147};
148
149/*
150 * We wrap our port structure around the generic uart_port.
151 */
152struct uart_amba_port {
153 struct uart_port port;
154 struct clk *clk;
155 const struct vendor_data *vendor;
156 unsigned int dmacr; /* dma control reg */
157 unsigned int im; /* interrupt mask */
158 unsigned int old_status;
159 unsigned int fifosize; /* vendor-specific */
160 unsigned int lcrh_tx; /* vendor-specific */
161 unsigned int lcrh_rx; /* vendor-specific */
162 bool autorts;
163 char type[12];
164 bool interrupt_may_hang; /* vendor-specific */
165#ifdef CONFIG_DMA_ENGINE
166 /* DMA stuff */
167 bool using_tx_dma;
168 bool using_rx_dma;
169 struct pl011_dmarx_data dmarx;
170 struct pl011_dmatx_data dmatx;
171#endif
172};
173
174/*
175 * Reads up to 256 characters from the FIFO or until it's empty and
176 * inserts them into the TTY layer. Returns the number of characters
177 * read from the FIFO.
178 */
179static int pl011_fifo_to_tty(struct uart_amba_port *uap)
180{
181 u16 status, ch;
182 unsigned int flag, max_count = 256;
183 int fifotaken = 0;
184
185 while (max_count--) {
186 status = readw(uap->port.membase + UART01x_FR);
187 if (status & UART01x_FR_RXFE)
188 break;
189
190 /* Take chars from the FIFO and update status */
191 ch = readw(uap->port.membase + UART01x_DR) |
192 UART_DUMMY_DR_RX;
193 flag = TTY_NORMAL;
194 uap->port.icount.rx++;
195 fifotaken++;
196
197 if (unlikely(ch & UART_DR_ERROR)) {
198 if (ch & UART011_DR_BE) {
199 ch &= ~(UART011_DR_FE | UART011_DR_PE);
200 uap->port.icount.brk++;
201 if (uart_handle_break(&uap->port))
202 continue;
203 } else if (ch & UART011_DR_PE)
204 uap->port.icount.parity++;
205 else if (ch & UART011_DR_FE)
206 uap->port.icount.frame++;
207 if (ch & UART011_DR_OE)
208 uap->port.icount.overrun++;
209
210 ch &= uap->port.read_status_mask;
211
212 if (ch & UART011_DR_BE)
213 flag = TTY_BREAK;
214 else if (ch & UART011_DR_PE)
215 flag = TTY_PARITY;
216 else if (ch & UART011_DR_FE)
217 flag = TTY_FRAME;
218 }
219
220 if (uart_handle_sysrq_char(&uap->port, ch & 255))
221 continue;
222
223 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
224 }
225
226 return fifotaken;
227}
228
229
230/*
231 * All the DMA operation mode stuff goes inside this ifdef.
232 * This assumes that you have a generic DMA device interface,
233 * no custom DMA interfaces are supported.
234 */
235#ifdef CONFIG_DMA_ENGINE
236
237#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
238
239static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
240 enum dma_data_direction dir)
241{
242 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
243 if (!sg->buf)
244 return -ENOMEM;
245
246 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
247
248 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
249 kfree(sg->buf);
250 return -EINVAL;
251 }
252 return 0;
253}
254
255static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
256 enum dma_data_direction dir)
257{
258 if (sg->buf) {
259 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
260 kfree(sg->buf);
261 }
262}
263
264static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
265{
266 /* DMA is the sole user of the platform data right now */
267 struct amba_pl011_data *plat = uap->port.dev->platform_data;
268 struct dma_slave_config tx_conf = {
269 .dst_addr = uap->port.mapbase + UART01x_DR,
270 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
271 .direction = DMA_TO_DEVICE,
272 .dst_maxburst = uap->fifosize >> 1,
273 };
274 struct dma_chan *chan;
275 dma_cap_mask_t mask;
276
277 /* We need platform data */
278 if (!plat || !plat->dma_filter) {
279 dev_info(uap->port.dev, "no DMA platform data\n");
280 return;
281 }
282
283 /* Try to acquire a generic DMA engine slave TX channel */
284 dma_cap_zero(mask);
285 dma_cap_set(DMA_SLAVE, mask);
286
287 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
288 if (!chan) {
289 dev_err(uap->port.dev, "no TX DMA channel!\n");
290 return;
291 }
292
293 dmaengine_slave_config(chan, &tx_conf);
294 uap->dmatx.chan = chan;
295
296 dev_info(uap->port.dev, "DMA channel TX %s\n",
297 dma_chan_name(uap->dmatx.chan));
298
299 /* Optionally make use of an RX channel as well */
300 if (plat->dma_rx_param) {
301 struct dma_slave_config rx_conf = {
302 .src_addr = uap->port.mapbase + UART01x_DR,
303 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
304 .direction = DMA_FROM_DEVICE,
305 .src_maxburst = uap->fifosize >> 1,
306 };
307
308 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
309 if (!chan) {
310 dev_err(uap->port.dev, "no RX DMA channel!\n");
311 return;
312 }
313
314 dmaengine_slave_config(chan, &rx_conf);
315 uap->dmarx.chan = chan;
316
317 dev_info(uap->port.dev, "DMA channel RX %s\n",
318 dma_chan_name(uap->dmarx.chan));
319 }
320}
321
322#ifndef MODULE
323/*
324 * Stack up the UARTs and let the above initcall be done at device
325 * initcall time, because the serial driver is called as an arch
326 * initcall, and at this time the DMA subsystem is not yet registered.
327 * At this point the driver will switch over to using DMA where desired.
328 */
329struct dma_uap {
330 struct list_head node;
331 struct uart_amba_port *uap;
332};
333
334static LIST_HEAD(pl011_dma_uarts);
335
336static int __init pl011_dma_initcall(void)
337{
338 struct list_head *node, *tmp;
339
340 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
341 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
342 pl011_dma_probe_initcall(dmau->uap);
343 list_del(node);
344 kfree(dmau);
345 }
346 return 0;
347}
348
349device_initcall(pl011_dma_initcall);
350
351static void pl011_dma_probe(struct uart_amba_port *uap)
352{
353 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
354 if (dmau) {
355 dmau->uap = uap;
356 list_add_tail(&dmau->node, &pl011_dma_uarts);
357 }
358}
359#else
360static void pl011_dma_probe(struct uart_amba_port *uap)
361{
362 pl011_dma_probe_initcall(uap);
363}
364#endif
365
366static void pl011_dma_remove(struct uart_amba_port *uap)
367{
368 /* TODO: remove the initcall if it has not yet executed */
369 if (uap->dmatx.chan)
370 dma_release_channel(uap->dmatx.chan);
371 if (uap->dmarx.chan)
372 dma_release_channel(uap->dmarx.chan);
373}
374
375/* Forward declare this for the refill routine */
376static int pl011_dma_tx_refill(struct uart_amba_port *uap);
377
378/*
379 * The current DMA TX buffer has been sent.
380 * Try to queue up another DMA buffer.
381 */
382static void pl011_dma_tx_callback(void *data)
383{
384 struct uart_amba_port *uap = data;
385 struct pl011_dmatx_data *dmatx = &uap->dmatx;
386 unsigned long flags;
387 u16 dmacr;
388
389 spin_lock_irqsave(&uap->port.lock, flags);
390 if (uap->dmatx.queued)
391 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
392 DMA_TO_DEVICE);
393
394 dmacr = uap->dmacr;
395 uap->dmacr = dmacr & ~UART011_TXDMAE;
396 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
397
398 /*
399 * If TX DMA was disabled, it means that we've stopped the DMA for
400 * some reason (eg, XOFF received, or we want to send an X-char.)
401 *
402 * Note: we need to be careful here of a potential race between DMA
403 * and the rest of the driver - if the driver disables TX DMA while
404 * a TX buffer completing, we must update the tx queued status to
405 * get further refills (hence we check dmacr).
406 */
407 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
408 uart_circ_empty(&uap->port.state->xmit)) {
409 uap->dmatx.queued = false;
410 spin_unlock_irqrestore(&uap->port.lock, flags);
411 return;
412 }
413
414 if (pl011_dma_tx_refill(uap) <= 0) {
415 /*
416 * We didn't queue a DMA buffer for some reason, but we
417 * have data pending to be sent. Re-enable the TX IRQ.
418 */
419 uap->im |= UART011_TXIM;
420 writew(uap->im, uap->port.membase + UART011_IMSC);
421 }
422 spin_unlock_irqrestore(&uap->port.lock, flags);
423}
424
425/*
426 * Try to refill the TX DMA buffer.
427 * Locking: called with port lock held and IRQs disabled.
428 * Returns:
429 * 1 if we queued up a TX DMA buffer.
430 * 0 if we didn't want to handle this by DMA
431 * <0 on error
432 */
433static int pl011_dma_tx_refill(struct uart_amba_port *uap)
434{
435 struct pl011_dmatx_data *dmatx = &uap->dmatx;
436 struct dma_chan *chan = dmatx->chan;
437 struct dma_device *dma_dev = chan->device;
438 struct dma_async_tx_descriptor *desc;
439 struct circ_buf *xmit = &uap->port.state->xmit;
440 unsigned int count;
441
442 /*
443 * Try to avoid the overhead involved in using DMA if the
444 * transaction fits in the first half of the FIFO, by using
445 * the standard interrupt handling. This ensures that we
446 * issue a uart_write_wakeup() at the appropriate time.
447 */
448 count = uart_circ_chars_pending(xmit);
449 if (count < (uap->fifosize >> 1)) {
450 uap->dmatx.queued = false;
451 return 0;
452 }
453
454 /*
455 * Bodge: don't send the last character by DMA, as this
456 * will prevent XON from notifying us to restart DMA.
457 */
458 count -= 1;
459
460 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
461 if (count > PL011_DMA_BUFFER_SIZE)
462 count = PL011_DMA_BUFFER_SIZE;
463
464 if (xmit->tail < xmit->head)
465 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
466 else {
467 size_t first = UART_XMIT_SIZE - xmit->tail;
468 size_t second = xmit->head;
469
470 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
471 if (second)
472 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
473 }
474
475 dmatx->sg.length = count;
476
477 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
478 uap->dmatx.queued = false;
479 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
480 return -EBUSY;
481 }
482
483 desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_TO_DEVICE,
484 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
485 if (!desc) {
486 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
487 uap->dmatx.queued = false;
488 /*
489 * If DMA cannot be used right now, we complete this
490 * transaction via IRQ and let the TTY layer retry.
491 */
492 dev_dbg(uap->port.dev, "TX DMA busy\n");
493 return -EBUSY;
494 }
495
496 /* Some data to go along to the callback */
497 desc->callback = pl011_dma_tx_callback;
498 desc->callback_param = uap;
499
500 /* All errors should happen at prepare time */
501 dmaengine_submit(desc);
502
503 /* Fire the DMA transaction */
504 dma_dev->device_issue_pending(chan);
505
506 uap->dmacr |= UART011_TXDMAE;
507 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
508 uap->dmatx.queued = true;
509
510 /*
511 * Now we know that DMA will fire, so advance the ring buffer
512 * with the stuff we just dispatched.
513 */
514 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
515 uap->port.icount.tx += count;
516
517 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
518 uart_write_wakeup(&uap->port);
519
520 return 1;
521}
522
523/*
524 * We received a transmit interrupt without a pending X-char but with
525 * pending characters.
526 * Locking: called with port lock held and IRQs disabled.
527 * Returns:
528 * false if we want to use PIO to transmit
529 * true if we queued a DMA buffer
530 */
531static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
532{
533 if (!uap->using_tx_dma)
534 return false;
535
536 /*
537 * If we already have a TX buffer queued, but received a
538 * TX interrupt, it will be because we've just sent an X-char.
539 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
540 */
541 if (uap->dmatx.queued) {
542 uap->dmacr |= UART011_TXDMAE;
543 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
544 uap->im &= ~UART011_TXIM;
545 writew(uap->im, uap->port.membase + UART011_IMSC);
546 return true;
547 }
548
549 /*
550 * We don't have a TX buffer queued, so try to queue one.
551 * If we successfully queued a buffer, mask the TX IRQ.
552 */
553 if (pl011_dma_tx_refill(uap) > 0) {
554 uap->im &= ~UART011_TXIM;
555 writew(uap->im, uap->port.membase + UART011_IMSC);
556 return true;
557 }
558 return false;
559}
560
561/*
562 * Stop the DMA transmit (eg, due to received XOFF).
563 * Locking: called with port lock held and IRQs disabled.
564 */
565static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
566{
567 if (uap->dmatx.queued) {
568 uap->dmacr &= ~UART011_TXDMAE;
569 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
570 }
571}
572
573/*
574 * Try to start a DMA transmit, or in the case of an XON/OFF
575 * character queued for send, try to get that character out ASAP.
576 * Locking: called with port lock held and IRQs disabled.
577 * Returns:
578 * false if we want the TX IRQ to be enabled
579 * true if we have a buffer queued
580 */
581static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
582{
583 u16 dmacr;
584
585 if (!uap->using_tx_dma)
586 return false;
587
588 if (!uap->port.x_char) {
589 /* no X-char, try to push chars out in DMA mode */
590 bool ret = true;
591
592 if (!uap->dmatx.queued) {
593 if (pl011_dma_tx_refill(uap) > 0) {
594 uap->im &= ~UART011_TXIM;
595 ret = true;
596 } else {
597 uap->im |= UART011_TXIM;
598 ret = false;
599 }
600 writew(uap->im, uap->port.membase + UART011_IMSC);
601 } else if (!(uap->dmacr & UART011_TXDMAE)) {
602 uap->dmacr |= UART011_TXDMAE;
603 writew(uap->dmacr,
604 uap->port.membase + UART011_DMACR);
605 }
606 return ret;
607 }
608
609 /*
610 * We have an X-char to send. Disable DMA to prevent it loading
611 * the TX fifo, and then see if we can stuff it into the FIFO.
612 */
613 dmacr = uap->dmacr;
614 uap->dmacr &= ~UART011_TXDMAE;
615 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
616
617 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
618 /*
619 * No space in the FIFO, so enable the transmit interrupt
620 * so we know when there is space. Note that once we've
621 * loaded the character, we should just re-enable DMA.
622 */
623 return false;
624 }
625
626 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
627 uap->port.icount.tx++;
628 uap->port.x_char = 0;
629
630 /* Success - restore the DMA state */
631 uap->dmacr = dmacr;
632 writew(dmacr, uap->port.membase + UART011_DMACR);
633
634 return true;
635}
636
637/*
638 * Flush the transmit buffer.
639 * Locking: called with port lock held and IRQs disabled.
640 */
641static void pl011_dma_flush_buffer(struct uart_port *port)
642{
643 struct uart_amba_port *uap = (struct uart_amba_port *)port;
644
645 if (!uap->using_tx_dma)
646 return;
647
648 /* Avoid deadlock with the DMA engine callback */
649 spin_unlock(&uap->port.lock);
650 dmaengine_terminate_all(uap->dmatx.chan);
651 spin_lock(&uap->port.lock);
652 if (uap->dmatx.queued) {
653 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
654 DMA_TO_DEVICE);
655 uap->dmatx.queued = false;
656 uap->dmacr &= ~UART011_TXDMAE;
657 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
658 }
659}
660
661static void pl011_dma_rx_callback(void *data);
662
663static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
664{
665 struct dma_chan *rxchan = uap->dmarx.chan;
666 struct dma_device *dma_dev;
667 struct pl011_dmarx_data *dmarx = &uap->dmarx;
668 struct dma_async_tx_descriptor *desc;
669 struct pl011_sgbuf *sgbuf;
670
671 if (!rxchan)
672 return -EIO;
673
674 /* Start the RX DMA job */
675 sgbuf = uap->dmarx.use_buf_b ?
676 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
677 dma_dev = rxchan->device;
678 desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
679 DMA_FROM_DEVICE,
680 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
681 /*
682 * If the DMA engine is busy and cannot prepare a
683 * channel, no big deal, the driver will fall back
684 * to interrupt mode as a result of this error code.
685 */
686 if (!desc) {
687 uap->dmarx.running = false;
688 dmaengine_terminate_all(rxchan);
689 return -EBUSY;
690 }
691
692 /* Some data to go along to the callback */
693 desc->callback = pl011_dma_rx_callback;
694 desc->callback_param = uap;
695 dmarx->cookie = dmaengine_submit(desc);
696 dma_async_issue_pending(rxchan);
697
698 uap->dmacr |= UART011_RXDMAE;
699 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
700 uap->dmarx.running = true;
701
702 uap->im &= ~UART011_RXIM;
703 writew(uap->im, uap->port.membase + UART011_IMSC);
704
705 return 0;
706}
707
708/*
709 * This is called when either the DMA job is complete, or
710 * the FIFO timeout interrupt occurred. This must be called
711 * with the port spinlock uap->port.lock held.
712 */
713static void pl011_dma_rx_chars(struct uart_amba_port *uap,
714 u32 pending, bool use_buf_b,
715 bool readfifo)
716{
717 struct tty_struct *tty = uap->port.state->port.tty;
718 struct pl011_sgbuf *sgbuf = use_buf_b ?
719 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
720 struct device *dev = uap->dmarx.chan->device->dev;
721 int dma_count = 0;
722 u32 fifotaken = 0; /* only used for vdbg() */
723
724 /* Pick everything from the DMA first */
725 if (pending) {
726 /* Sync in buffer */
727 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
728
729 /*
730 * First take all chars in the DMA pipe, then look in the FIFO.
731 * Note that tty_insert_flip_buf() tries to take as many chars
732 * as it can.
733 */
734 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
735 sgbuf->buf, pending);
736
737 /* Return buffer to device */
738 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
739
740 uap->port.icount.rx += dma_count;
741 if (dma_count < pending)
742 dev_warn(uap->port.dev,
743 "couldn't insert all characters (TTY is full?)\n");
744 }
745
746 /*
747 * Only continue with trying to read the FIFO if all DMA chars have
748 * been taken first.
749 */
750 if (dma_count == pending && readfifo) {
751 /* Clear any error flags */
752 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
753 uap->port.membase + UART011_ICR);
754
755 /*
756 * If we read all the DMA'd characters, and we had an
757 * incomplete buffer, that could be due to an rx error, or
758 * maybe we just timed out. Read any pending chars and check
759 * the error status.
760 *
761 * Error conditions will only occur in the FIFO, these will
762 * trigger an immediate interrupt and stop the DMA job, so we
763 * will always find the error in the FIFO, never in the DMA
764 * buffer.
765 */
766 fifotaken = pl011_fifo_to_tty(uap);
767 }
768
769 spin_unlock(&uap->port.lock);
770 dev_vdbg(uap->port.dev,
771 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
772 dma_count, fifotaken);
773 tty_flip_buffer_push(tty);
774 spin_lock(&uap->port.lock);
775}
776
777static void pl011_dma_rx_irq(struct uart_amba_port *uap)
778{
779 struct pl011_dmarx_data *dmarx = &uap->dmarx;
780 struct dma_chan *rxchan = dmarx->chan;
781 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
782 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
783 size_t pending;
784 struct dma_tx_state state;
785 enum dma_status dmastat;
786
787 /*
788 * Pause the transfer so we can trust the current counter,
789 * do this before we pause the PL011 block, else we may
790 * overflow the FIFO.
791 */
792 if (dmaengine_pause(rxchan))
793 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
794 dmastat = rxchan->device->device_tx_status(rxchan,
795 dmarx->cookie, &state);
796 if (dmastat != DMA_PAUSED)
797 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
798
799 /* Disable RX DMA - incoming data will wait in the FIFO */
800 uap->dmacr &= ~UART011_RXDMAE;
801 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
802 uap->dmarx.running = false;
803
804 pending = sgbuf->sg.length - state.residue;
805 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
806 /* Then we terminate the transfer - we now know our residue */
807 dmaengine_terminate_all(rxchan);
808
809 /*
810 * This will take the chars we have so far and insert
811 * into the framework.
812 */
813 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
814
815 /* Switch buffer & re-trigger DMA job */
816 dmarx->use_buf_b = !dmarx->use_buf_b;
817 if (pl011_dma_rx_trigger_dma(uap)) {
818 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
819 "fall back to interrupt mode\n");
820 uap->im |= UART011_RXIM;
821 writew(uap->im, uap->port.membase + UART011_IMSC);
822 }
823}
824
825static void pl011_dma_rx_callback(void *data)
826{
827 struct uart_amba_port *uap = data;
828 struct pl011_dmarx_data *dmarx = &uap->dmarx;
829 bool lastbuf = dmarx->use_buf_b;
830 int ret;
831
832 /*
833 * This completion interrupt occurs typically when the
834 * RX buffer is totally stuffed but no timeout has yet
835 * occurred. When that happens, we just want the RX
836 * routine to flush out the secondary DMA buffer while
837 * we immediately trigger the next DMA job.
838 */
839 spin_lock_irq(&uap->port.lock);
840 uap->dmarx.running = false;
841 dmarx->use_buf_b = !lastbuf;
842 ret = pl011_dma_rx_trigger_dma(uap);
843
844 pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
845 spin_unlock_irq(&uap->port.lock);
846 /*
847 * Do this check after we picked the DMA chars so we don't
848 * get some IRQ immediately from RX.
849 */
850 if (ret) {
851 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
852 "fall back to interrupt mode\n");
853 uap->im |= UART011_RXIM;
854 writew(uap->im, uap->port.membase + UART011_IMSC);
855 }
856}
857
858/*
859 * Stop accepting received characters, when we're shutting down or
860 * suspending this port.
861 * Locking: called with port lock held and IRQs disabled.
862 */
863static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
864{
865 /* FIXME. Just disable the DMA enable */
866 uap->dmacr &= ~UART011_RXDMAE;
867 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
868}
869
870static void pl011_dma_startup(struct uart_amba_port *uap)
871{
872 int ret;
873
874 if (!uap->dmatx.chan)
875 return;
876
877 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
878 if (!uap->dmatx.buf) {
879 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
880 uap->port.fifosize = uap->fifosize;
881 return;
882 }
883
884 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
885
886 /* The DMA buffer is now the FIFO the TTY subsystem can use */
887 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
888 uap->using_tx_dma = true;
889
890 if (!uap->dmarx.chan)
891 goto skip_rx;
892
893 /* Allocate and map DMA RX buffers */
894 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
895 DMA_FROM_DEVICE);
896 if (ret) {
897 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
898 "RX buffer A", ret);
899 goto skip_rx;
900 }
901
902 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
903 DMA_FROM_DEVICE);
904 if (ret) {
905 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
906 "RX buffer B", ret);
907 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
908 DMA_FROM_DEVICE);
909 goto skip_rx;
910 }
911
912 uap->using_rx_dma = true;
913
914skip_rx:
915 /* Turn on DMA error (RX/TX will be enabled on demand) */
916 uap->dmacr |= UART011_DMAONERR;
917 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
918
919 /*
920 * ST Micro variants has some specific dma burst threshold
921 * compensation. Set this to 16 bytes, so burst will only
922 * be issued above/below 16 bytes.
923 */
924 if (uap->vendor->dma_threshold)
925 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
926 uap->port.membase + ST_UART011_DMAWM);
927
928 if (uap->using_rx_dma) {
929 if (pl011_dma_rx_trigger_dma(uap))
930 dev_dbg(uap->port.dev, "could not trigger initial "
931 "RX DMA job, fall back to interrupt mode\n");
932 }
933}
934
935static void pl011_dma_shutdown(struct uart_amba_port *uap)
936{
937 if (!(uap->using_tx_dma || uap->using_rx_dma))
938 return;
939
940 /* Disable RX and TX DMA */
941 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
942 barrier();
943
944 spin_lock_irq(&uap->port.lock);
945 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
946 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
947 spin_unlock_irq(&uap->port.lock);
948
949 if (uap->using_tx_dma) {
950 /* In theory, this should already be done by pl011_dma_flush_buffer */
951 dmaengine_terminate_all(uap->dmatx.chan);
952 if (uap->dmatx.queued) {
953 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
954 DMA_TO_DEVICE);
955 uap->dmatx.queued = false;
956 }
957
958 kfree(uap->dmatx.buf);
959 uap->using_tx_dma = false;
960 }
961
962 if (uap->using_rx_dma) {
963 dmaengine_terminate_all(uap->dmarx.chan);
964 /* Clean up the RX DMA */
965 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
966 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
967 uap->using_rx_dma = false;
968 }
969}
970
971static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
972{
973 return uap->using_rx_dma;
974}
975
976static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
977{
978 return uap->using_rx_dma && uap->dmarx.running;
979}
980
981
982#else
983/* Blank functions if the DMA engine is not available */
984static inline void pl011_dma_probe(struct uart_amba_port *uap)
985{
986}
987
988static inline void pl011_dma_remove(struct uart_amba_port *uap)
989{
990}
991
992static inline void pl011_dma_startup(struct uart_amba_port *uap)
993{
994}
995
996static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
997{
998}
999
1000static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1001{
1002 return false;
1003}
1004
1005static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1006{
1007}
1008
1009static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1010{
1011 return false;
1012}
1013
1014static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1015{
1016}
1017
1018static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1019{
1020}
1021
1022static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1023{
1024 return -EIO;
1025}
1026
1027static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1028{
1029 return false;
1030}
1031
1032static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1033{
1034 return false;
1035}
1036
1037#define pl011_dma_flush_buffer NULL
1038#endif
1039
1040
1041/*
1042 * pl011_lockup_wa
1043 * This workaround aims to break the deadlock situation
1044 * when after long transfer over uart in hardware flow
1045 * control, uart interrupt registers cannot be cleared.
1046 * Hence uart transfer gets blocked.
1047 *
1048 * It is seen that during such deadlock condition ICR
1049 * don't get cleared even on multiple write. This leads
1050 * pass_counter to decrease and finally reach zero. This
1051 * can be taken as trigger point to run this UART_BT_WA.
1052 *
1053 */
1054static void pl011_lockup_wa(unsigned long data)
1055{
1056 struct uart_amba_port *uap = amba_ports[0];
1057 void __iomem *base = uap->port.membase;
1058 struct circ_buf *xmit = &uap->port.state->xmit;
1059 struct tty_struct *tty = uap->port.state->port.tty;
1060 int buf_empty_retries = 200;
1061 int loop;
1062
1063 /* Stop HCI layer from submitting data for tx */
1064 tty->hw_stopped = 1;
1065 while (!uart_circ_empty(xmit)) {
1066 if (buf_empty_retries-- == 0)
1067 break;
1068 udelay(100);
1069 }
1070
1071 /* Backup registers */
1072 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1073 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1074
1075 /* Disable UART so that FIFO data is flushed out */
1076 writew(0x00, uap->port.membase + UART011_CR);
1077
1078 /* Soft reset UART module */
1079 if (uap->port.dev->platform_data) {
1080 struct amba_pl011_data *plat;
1081
1082 plat = uap->port.dev->platform_data;
1083 if (plat->reset)
1084 plat->reset();
1085 }
1086
1087 /* Restore registers */
1088 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1089 writew(uart_wa_regdata[loop] ,
1090 uap->port.membase + uart_wa_reg[loop]);
1091
1092 /* Initialise the old status of the modem signals */
1093 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1094 UART01x_FR_MODEM_ANY;
1095
1096 if (readl(base + UART011_MIS) & 0x2)
1097 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1098
1099 /* Start Tx/Rx */
1100 tty->hw_stopped = 0;
1101}
1102
1103static void pl011_stop_tx(struct uart_port *port)
1104{
1105 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1106
1107 uap->im &= ~UART011_TXIM;
1108 writew(uap->im, uap->port.membase + UART011_IMSC);
1109 pl011_dma_tx_stop(uap);
1110}
1111
1112static void pl011_start_tx(struct uart_port *port)
1113{
1114 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1115
1116 if (!pl011_dma_tx_start(uap)) {
1117 uap->im |= UART011_TXIM;
1118 writew(uap->im, uap->port.membase + UART011_IMSC);
1119 }
1120}
1121
1122static void pl011_stop_rx(struct uart_port *port)
1123{
1124 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1125
1126 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1127 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1128 writew(uap->im, uap->port.membase + UART011_IMSC);
1129
1130 pl011_dma_rx_stop(uap);
1131}
1132
1133static void pl011_enable_ms(struct uart_port *port)
1134{
1135 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1136
1137 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1138 writew(uap->im, uap->port.membase + UART011_IMSC);
1139}
1140
1141static void pl011_rx_chars(struct uart_amba_port *uap)
1142{
1143 struct tty_struct *tty = uap->port.state->port.tty;
1144
1145 pl011_fifo_to_tty(uap);
1146
1147 spin_unlock(&uap->port.lock);
1148 tty_flip_buffer_push(tty);
1149 /*
1150 * If we were temporarily out of DMA mode for a while,
1151 * attempt to switch back to DMA mode again.
1152 */
1153 if (pl011_dma_rx_available(uap)) {
1154 if (pl011_dma_rx_trigger_dma(uap)) {
1155 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1156 "fall back to interrupt mode again\n");
1157 uap->im |= UART011_RXIM;
1158 } else
1159 uap->im &= ~UART011_RXIM;
1160 writew(uap->im, uap->port.membase + UART011_IMSC);
1161 }
1162 spin_lock(&uap->port.lock);
1163}
1164
1165static void pl011_tx_chars(struct uart_amba_port *uap)
1166{
1167 struct circ_buf *xmit = &uap->port.state->xmit;
1168 int count;
1169
1170 if (uap->port.x_char) {
1171 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1172 uap->port.icount.tx++;
1173 uap->port.x_char = 0;
1174 return;
1175 }
1176 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
1177 pl011_stop_tx(&uap->port);
1178 return;
1179 }
1180
1181 /* If we are using DMA mode, try to send some characters. */
1182 if (pl011_dma_tx_irq(uap))
1183 return;
1184
1185 count = uap->fifosize >> 1;
1186 do {
1187 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1188 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1189 uap->port.icount.tx++;
1190 if (uart_circ_empty(xmit))
1191 break;
1192 } while (--count > 0);
1193
1194 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1195 uart_write_wakeup(&uap->port);
1196
1197 if (uart_circ_empty(xmit))
1198 pl011_stop_tx(&uap->port);
1199}
1200
1201static void pl011_modem_status(struct uart_amba_port *uap)
1202{
1203 unsigned int status, delta;
1204
1205 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1206
1207 delta = status ^ uap->old_status;
1208 uap->old_status = status;
1209
1210 if (!delta)
1211 return;
1212
1213 if (delta & UART01x_FR_DCD)
1214 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1215
1216 if (delta & UART01x_FR_DSR)
1217 uap->port.icount.dsr++;
1218
1219 if (delta & UART01x_FR_CTS)
1220 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1221
1222 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
1223}
1224
1225static irqreturn_t pl011_int(int irq, void *dev_id)
1226{
1227 struct uart_amba_port *uap = dev_id;
1228 unsigned long flags;
1229 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1230 int handled = 0;
1231
1232 spin_lock_irqsave(&uap->port.lock, flags);
1233
1234 status = readw(uap->port.membase + UART011_MIS);
1235 if (status) {
1236 do {
1237 writew(status & ~(UART011_TXIS|UART011_RTIS|
1238 UART011_RXIS),
1239 uap->port.membase + UART011_ICR);
1240
1241 if (status & (UART011_RTIS|UART011_RXIS)) {
1242 if (pl011_dma_rx_running(uap))
1243 pl011_dma_rx_irq(uap);
1244 else
1245 pl011_rx_chars(uap);
1246 }
1247 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1248 UART011_CTSMIS|UART011_RIMIS))
1249 pl011_modem_status(uap);
1250 if (status & UART011_TXIS)
1251 pl011_tx_chars(uap);
1252
1253 if (pass_counter-- == 0) {
1254 if (uap->interrupt_may_hang)
1255 tasklet_schedule(&pl011_lockup_tlet);
1256 break;
1257 }
1258
1259 status = readw(uap->port.membase + UART011_MIS);
1260 } while (status != 0);
1261 handled = 1;
1262 }
1263
1264 spin_unlock_irqrestore(&uap->port.lock, flags);
1265
1266 return IRQ_RETVAL(handled);
1267}
1268
1269static unsigned int pl01x_tx_empty(struct uart_port *port)
1270{
1271 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1272 unsigned int status = readw(uap->port.membase + UART01x_FR);
1273 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1274}
1275
1276static unsigned int pl01x_get_mctrl(struct uart_port *port)
1277{
1278 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1279 unsigned int result = 0;
1280 unsigned int status = readw(uap->port.membase + UART01x_FR);
1281
1282#define TIOCMBIT(uartbit, tiocmbit) \
1283 if (status & uartbit) \
1284 result |= tiocmbit
1285
1286 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1287 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1288 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1289 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1290#undef TIOCMBIT
1291 return result;
1292}
1293
1294static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1295{
1296 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1297 unsigned int cr;
1298
1299 cr = readw(uap->port.membase + UART011_CR);
1300
1301#define TIOCMBIT(tiocmbit, uartbit) \
1302 if (mctrl & tiocmbit) \
1303 cr |= uartbit; \
1304 else \
1305 cr &= ~uartbit
1306
1307 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1308 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1309 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1310 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1311 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
1312
1313 if (uap->autorts) {
1314 /* We need to disable auto-RTS if we want to turn RTS off */
1315 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1316 }
1317#undef TIOCMBIT
1318
1319 writew(cr, uap->port.membase + UART011_CR);
1320}
1321
1322static void pl011_break_ctl(struct uart_port *port, int break_state)
1323{
1324 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1325 unsigned long flags;
1326 unsigned int lcr_h;
1327
1328 spin_lock_irqsave(&uap->port.lock, flags);
1329 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1330 if (break_state == -1)
1331 lcr_h |= UART01x_LCRH_BRK;
1332 else
1333 lcr_h &= ~UART01x_LCRH_BRK;
1334 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1335 spin_unlock_irqrestore(&uap->port.lock, flags);
1336}
1337
1338#ifdef CONFIG_CONSOLE_POLL
1339static int pl010_get_poll_char(struct uart_port *port)
1340{
1341 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1342 unsigned int status;
1343
1344 status = readw(uap->port.membase + UART01x_FR);
1345 if (status & UART01x_FR_RXFE)
1346 return NO_POLL_CHAR;
1347
1348 return readw(uap->port.membase + UART01x_DR);
1349}
1350
1351static void pl010_put_poll_char(struct uart_port *port,
1352 unsigned char ch)
1353{
1354 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1355
1356 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1357 barrier();
1358
1359 writew(ch, uap->port.membase + UART01x_DR);
1360}
1361
1362#endif /* CONFIG_CONSOLE_POLL */
1363
1364static int pl011_startup(struct uart_port *port)
1365{
1366 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1367 unsigned int cr;
1368 int retval;
1369
1370 /*
1371 * Try to enable the clock producer.
1372 */
1373 retval = clk_enable(uap->clk);
1374 if (retval)
1375 goto out;
1376
1377 uap->port.uartclk = clk_get_rate(uap->clk);
1378
1379 /*
1380 * Allocate the IRQ
1381 */
1382 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1383 if (retval)
1384 goto clk_dis;
1385
1386 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
1387
1388 /*
1389 * Provoke TX FIFO interrupt into asserting.
1390 */
1391 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1392 writew(cr, uap->port.membase + UART011_CR);
1393 writew(0, uap->port.membase + UART011_FBRD);
1394 writew(1, uap->port.membase + UART011_IBRD);
1395 writew(0, uap->port.membase + uap->lcrh_rx);
1396 if (uap->lcrh_tx != uap->lcrh_rx) {
1397 int i;
1398 /*
1399 * Wait 10 PCLKs before writing LCRH_TX register,
1400 * to get this delay write read only register 10 times
1401 */
1402 for (i = 0; i < 10; ++i)
1403 writew(0xff, uap->port.membase + UART011_MIS);
1404 writew(0, uap->port.membase + uap->lcrh_tx);
1405 }
1406 writew(0, uap->port.membase + UART01x_DR);
1407 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1408 barrier();
1409
1410 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
1411 writew(cr, uap->port.membase + UART011_CR);
1412
1413 /* Clear pending error interrupts */
1414 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
1415 uap->port.membase + UART011_ICR);
1416
1417 /*
1418 * initialise the old status of the modem signals
1419 */
1420 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1421
1422 /* Startup DMA */
1423 pl011_dma_startup(uap);
1424
1425 /*
1426 * Finally, enable interrupts, only timeouts when using DMA
1427 * if initial RX DMA job failed, start in interrupt mode
1428 * as well.
1429 */
1430 spin_lock_irq(&uap->port.lock);
1431 uap->im = UART011_RTIM;
1432 if (!pl011_dma_rx_running(uap))
1433 uap->im |= UART011_RXIM;
1434 writew(uap->im, uap->port.membase + UART011_IMSC);
1435 spin_unlock_irq(&uap->port.lock);
1436
1437 if (uap->port.dev->platform_data) {
1438 struct amba_pl011_data *plat;
1439
1440 plat = uap->port.dev->platform_data;
1441 if (plat->init)
1442 plat->init();
1443 }
1444
1445 return 0;
1446
1447 clk_dis:
1448 clk_disable(uap->clk);
1449 out:
1450 return retval;
1451}
1452
1453static void pl011_shutdown_channel(struct uart_amba_port *uap,
1454 unsigned int lcrh)
1455{
1456 unsigned long val;
1457
1458 val = readw(uap->port.membase + lcrh);
1459 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1460 writew(val, uap->port.membase + lcrh);
1461}
1462
1463static void pl011_shutdown(struct uart_port *port)
1464{
1465 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1466
1467 /*
1468 * disable all interrupts
1469 */
1470 spin_lock_irq(&uap->port.lock);
1471 uap->im = 0;
1472 writew(uap->im, uap->port.membase + UART011_IMSC);
1473 writew(0xffff, uap->port.membase + UART011_ICR);
1474 spin_unlock_irq(&uap->port.lock);
1475
1476 pl011_dma_shutdown(uap);
1477
1478 /*
1479 * Free the interrupt
1480 */
1481 free_irq(uap->port.irq, uap);
1482
1483 /*
1484 * disable the port
1485 */
1486 uap->autorts = false;
1487 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
1488
1489 /*
1490 * disable break condition and fifos
1491 */
1492 pl011_shutdown_channel(uap, uap->lcrh_rx);
1493 if (uap->lcrh_rx != uap->lcrh_tx)
1494 pl011_shutdown_channel(uap, uap->lcrh_tx);
1495
1496 /*
1497 * Shut down the clock producer
1498 */
1499 clk_disable(uap->clk);
1500
1501 if (uap->port.dev->platform_data) {
1502 struct amba_pl011_data *plat;
1503
1504 plat = uap->port.dev->platform_data;
1505 if (plat->exit)
1506 plat->exit();
1507 }
1508
1509}
1510
1511static void
1512pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1513 struct ktermios *old)
1514{
1515 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1516 unsigned int lcr_h, old_cr;
1517 unsigned long flags;
1518 unsigned int baud, quot, clkdiv;
1519
1520 if (uap->vendor->oversampling)
1521 clkdiv = 8;
1522 else
1523 clkdiv = 16;
1524
1525 /*
1526 * Ask the core to calculate the divisor for us.
1527 */
1528 baud = uart_get_baud_rate(port, termios, old, 0,
1529 port->uartclk / clkdiv);
1530
1531 if (baud > port->uartclk/16)
1532 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1533 else
1534 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
1535
1536 switch (termios->c_cflag & CSIZE) {
1537 case CS5:
1538 lcr_h = UART01x_LCRH_WLEN_5;
1539 break;
1540 case CS6:
1541 lcr_h = UART01x_LCRH_WLEN_6;
1542 break;
1543 case CS7:
1544 lcr_h = UART01x_LCRH_WLEN_7;
1545 break;
1546 default: // CS8
1547 lcr_h = UART01x_LCRH_WLEN_8;
1548 break;
1549 }
1550 if (termios->c_cflag & CSTOPB)
1551 lcr_h |= UART01x_LCRH_STP2;
1552 if (termios->c_cflag & PARENB) {
1553 lcr_h |= UART01x_LCRH_PEN;
1554 if (!(termios->c_cflag & PARODD))
1555 lcr_h |= UART01x_LCRH_EPS;
1556 }
1557 if (uap->fifosize > 1)
1558 lcr_h |= UART01x_LCRH_FEN;
1559
1560 spin_lock_irqsave(&port->lock, flags);
1561
1562 /*
1563 * Update the per-port timeout.
1564 */
1565 uart_update_timeout(port, termios->c_cflag, baud);
1566
1567 port->read_status_mask = UART011_DR_OE | 255;
1568 if (termios->c_iflag & INPCK)
1569 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1570 if (termios->c_iflag & (BRKINT | PARMRK))
1571 port->read_status_mask |= UART011_DR_BE;
1572
1573 /*
1574 * Characters to ignore
1575 */
1576 port->ignore_status_mask = 0;
1577 if (termios->c_iflag & IGNPAR)
1578 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1579 if (termios->c_iflag & IGNBRK) {
1580 port->ignore_status_mask |= UART011_DR_BE;
1581 /*
1582 * If we're ignoring parity and break indicators,
1583 * ignore overruns too (for real raw support).
1584 */
1585 if (termios->c_iflag & IGNPAR)
1586 port->ignore_status_mask |= UART011_DR_OE;
1587 }
1588
1589 /*
1590 * Ignore all characters if CREAD is not set.
1591 */
1592 if ((termios->c_cflag & CREAD) == 0)
1593 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1594
1595 if (UART_ENABLE_MS(port, termios->c_cflag))
1596 pl011_enable_ms(port);
1597
1598 /* first, disable everything */
1599 old_cr = readw(port->membase + UART011_CR);
1600 writew(0, port->membase + UART011_CR);
1601
1602 if (termios->c_cflag & CRTSCTS) {
1603 if (old_cr & UART011_CR_RTS)
1604 old_cr |= UART011_CR_RTSEN;
1605
1606 old_cr |= UART011_CR_CTSEN;
1607 uap->autorts = true;
1608 } else {
1609 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1610 uap->autorts = false;
1611 }
1612
1613 if (uap->vendor->oversampling) {
1614 if (baud > port->uartclk / 16)
1615 old_cr |= ST_UART011_CR_OVSFACT;
1616 else
1617 old_cr &= ~ST_UART011_CR_OVSFACT;
1618 }
1619
1620 /* Set baud rate */
1621 writew(quot & 0x3f, port->membase + UART011_FBRD);
1622 writew(quot >> 6, port->membase + UART011_IBRD);
1623
1624 /*
1625 * ----------v----------v----------v----------v-----
1626 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1627 * ----------^----------^----------^----------^-----
1628 */
1629 writew(lcr_h, port->membase + uap->lcrh_rx);
1630 if (uap->lcrh_rx != uap->lcrh_tx) {
1631 int i;
1632 /*
1633 * Wait 10 PCLKs before writing LCRH_TX register,
1634 * to get this delay write read only register 10 times
1635 */
1636 for (i = 0; i < 10; ++i)
1637 writew(0xff, uap->port.membase + UART011_MIS);
1638 writew(lcr_h, port->membase + uap->lcrh_tx);
1639 }
1640 writew(old_cr, port->membase + UART011_CR);
1641
1642 spin_unlock_irqrestore(&port->lock, flags);
1643}
1644
1645static const char *pl011_type(struct uart_port *port)
1646{
1647 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1648 return uap->port.type == PORT_AMBA ? uap->type : NULL;
1649}
1650
1651/*
1652 * Release the memory region(s) being used by 'port'
1653 */
1654static void pl010_release_port(struct uart_port *port)
1655{
1656 release_mem_region(port->mapbase, SZ_4K);
1657}
1658
1659/*
1660 * Request the memory region(s) being used by 'port'
1661 */
1662static int pl010_request_port(struct uart_port *port)
1663{
1664 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1665 != NULL ? 0 : -EBUSY;
1666}
1667
1668/*
1669 * Configure/autoconfigure the port.
1670 */
1671static void pl010_config_port(struct uart_port *port, int flags)
1672{
1673 if (flags & UART_CONFIG_TYPE) {
1674 port->type = PORT_AMBA;
1675 pl010_request_port(port);
1676 }
1677}
1678
1679/*
1680 * verify the new serial_struct (for TIOCSSERIAL).
1681 */
1682static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1683{
1684 int ret = 0;
1685 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1686 ret = -EINVAL;
1687 if (ser->irq < 0 || ser->irq >= nr_irqs)
1688 ret = -EINVAL;
1689 if (ser->baud_base < 9600)
1690 ret = -EINVAL;
1691 return ret;
1692}
1693
1694static struct uart_ops amba_pl011_pops = {
1695 .tx_empty = pl01x_tx_empty,
1696 .set_mctrl = pl011_set_mctrl,
1697 .get_mctrl = pl01x_get_mctrl,
1698 .stop_tx = pl011_stop_tx,
1699 .start_tx = pl011_start_tx,
1700 .stop_rx = pl011_stop_rx,
1701 .enable_ms = pl011_enable_ms,
1702 .break_ctl = pl011_break_ctl,
1703 .startup = pl011_startup,
1704 .shutdown = pl011_shutdown,
1705 .flush_buffer = pl011_dma_flush_buffer,
1706 .set_termios = pl011_set_termios,
1707 .type = pl011_type,
1708 .release_port = pl010_release_port,
1709 .request_port = pl010_request_port,
1710 .config_port = pl010_config_port,
1711 .verify_port = pl010_verify_port,
1712#ifdef CONFIG_CONSOLE_POLL
1713 .poll_get_char = pl010_get_poll_char,
1714 .poll_put_char = pl010_put_poll_char,
1715#endif
1716};
1717
1718static struct uart_amba_port *amba_ports[UART_NR];
1719
1720#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1721
1722static void pl011_console_putchar(struct uart_port *port, int ch)
1723{
1724 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1725
1726 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1727 barrier();
1728 writew(ch, uap->port.membase + UART01x_DR);
1729}
1730
1731static void
1732pl011_console_write(struct console *co, const char *s, unsigned int count)
1733{
1734 struct uart_amba_port *uap = amba_ports[co->index];
1735 unsigned int status, old_cr, new_cr;
1736
1737 clk_enable(uap->clk);
1738
1739 /*
1740 * First save the CR then disable the interrupts
1741 */
1742 old_cr = readw(uap->port.membase + UART011_CR);
1743 new_cr = old_cr & ~UART011_CR_CTSEN;
1744 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1745 writew(new_cr, uap->port.membase + UART011_CR);
1746
1747 uart_console_write(&uap->port, s, count, pl011_console_putchar);
1748
1749 /*
1750 * Finally, wait for transmitter to become empty
1751 * and restore the TCR
1752 */
1753 do {
1754 status = readw(uap->port.membase + UART01x_FR);
1755 } while (status & UART01x_FR_BUSY);
1756 writew(old_cr, uap->port.membase + UART011_CR);
1757
1758 clk_disable(uap->clk);
1759}
1760
1761static void __init
1762pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1763 int *parity, int *bits)
1764{
1765 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1766 unsigned int lcr_h, ibrd, fbrd;
1767
1768 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1769
1770 *parity = 'n';
1771 if (lcr_h & UART01x_LCRH_PEN) {
1772 if (lcr_h & UART01x_LCRH_EPS)
1773 *parity = 'e';
1774 else
1775 *parity = 'o';
1776 }
1777
1778 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1779 *bits = 7;
1780 else
1781 *bits = 8;
1782
1783 ibrd = readw(uap->port.membase + UART011_IBRD);
1784 fbrd = readw(uap->port.membase + UART011_FBRD);
1785
1786 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
1787
1788 if (uap->vendor->oversampling) {
1789 if (readw(uap->port.membase + UART011_CR)
1790 & ST_UART011_CR_OVSFACT)
1791 *baud *= 2;
1792 }
1793 }
1794}
1795
1796static int __init pl011_console_setup(struct console *co, char *options)
1797{
1798 struct uart_amba_port *uap;
1799 int baud = 38400;
1800 int bits = 8;
1801 int parity = 'n';
1802 int flow = 'n';
1803
1804 /*
1805 * Check whether an invalid uart number has been specified, and
1806 * if so, search for the first available port that does have
1807 * console support.
1808 */
1809 if (co->index >= UART_NR)
1810 co->index = 0;
1811 uap = amba_ports[co->index];
1812 if (!uap)
1813 return -ENODEV;
1814
1815 if (uap->port.dev->platform_data) {
1816 struct amba_pl011_data *plat;
1817
1818 plat = uap->port.dev->platform_data;
1819 if (plat->init)
1820 plat->init();
1821 }
1822
1823 uap->port.uartclk = clk_get_rate(uap->clk);
1824
1825 if (options)
1826 uart_parse_options(options, &baud, &parity, &bits, &flow);
1827 else
1828 pl011_console_get_options(uap, &baud, &parity, &bits);
1829
1830 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1831}
1832
1833static struct uart_driver amba_reg;
1834static struct console amba_console = {
1835 .name = "ttyAMA",
1836 .write = pl011_console_write,
1837 .device = uart_console_device,
1838 .setup = pl011_console_setup,
1839 .flags = CON_PRINTBUFFER,
1840 .index = -1,
1841 .data = &amba_reg,
1842};
1843
1844#define AMBA_CONSOLE (&amba_console)
1845#else
1846#define AMBA_CONSOLE NULL
1847#endif
1848
1849static struct uart_driver amba_reg = {
1850 .owner = THIS_MODULE,
1851 .driver_name = "ttyAMA",
1852 .dev_name = "ttyAMA",
1853 .major = SERIAL_AMBA_MAJOR,
1854 .minor = SERIAL_AMBA_MINOR,
1855 .nr = UART_NR,
1856 .cons = AMBA_CONSOLE,
1857};
1858
1859static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
1860{
1861 struct uart_amba_port *uap;
1862 struct vendor_data *vendor = id->data;
1863 void __iomem *base;
1864 int i, ret;
1865
1866 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1867 if (amba_ports[i] == NULL)
1868 break;
1869
1870 if (i == ARRAY_SIZE(amba_ports)) {
1871 ret = -EBUSY;
1872 goto out;
1873 }
1874
1875 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
1876 if (uap == NULL) {
1877 ret = -ENOMEM;
1878 goto out;
1879 }
1880
1881 base = ioremap(dev->res.start, resource_size(&dev->res));
1882 if (!base) {
1883 ret = -ENOMEM;
1884 goto free;
1885 }
1886
1887 uap->clk = clk_get(&dev->dev, NULL);
1888 if (IS_ERR(uap->clk)) {
1889 ret = PTR_ERR(uap->clk);
1890 goto unmap;
1891 }
1892
1893 uap->vendor = vendor;
1894 uap->lcrh_rx = vendor->lcrh_rx;
1895 uap->lcrh_tx = vendor->lcrh_tx;
1896 uap->fifosize = vendor->fifosize;
1897 uap->interrupt_may_hang = vendor->interrupt_may_hang;
1898 uap->port.dev = &dev->dev;
1899 uap->port.mapbase = dev->res.start;
1900 uap->port.membase = base;
1901 uap->port.iotype = UPIO_MEM;
1902 uap->port.irq = dev->irq[0];
1903 uap->port.fifosize = uap->fifosize;
1904 uap->port.ops = &amba_pl011_pops;
1905 uap->port.flags = UPF_BOOT_AUTOCONF;
1906 uap->port.line = i;
1907 pl011_dma_probe(uap);
1908
1909 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1910
1911 amba_ports[i] = uap;
1912
1913 amba_set_drvdata(dev, uap);
1914 ret = uart_add_one_port(&amba_reg, &uap->port);
1915 if (ret) {
1916 amba_set_drvdata(dev, NULL);
1917 amba_ports[i] = NULL;
1918 pl011_dma_remove(uap);
1919 clk_put(uap->clk);
1920 unmap:
1921 iounmap(base);
1922 free:
1923 kfree(uap);
1924 }
1925 out:
1926 return ret;
1927}
1928
1929static int pl011_remove(struct amba_device *dev)
1930{
1931 struct uart_amba_port *uap = amba_get_drvdata(dev);
1932 int i;
1933
1934 amba_set_drvdata(dev, NULL);
1935
1936 uart_remove_one_port(&amba_reg, &uap->port);
1937
1938 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1939 if (amba_ports[i] == uap)
1940 amba_ports[i] = NULL;
1941
1942 pl011_dma_remove(uap);
1943 iounmap(uap->port.membase);
1944 clk_put(uap->clk);
1945 kfree(uap);
1946 return 0;
1947}
1948
1949#ifdef CONFIG_PM
1950static int pl011_suspend(struct amba_device *dev, pm_message_t state)
1951{
1952 struct uart_amba_port *uap = amba_get_drvdata(dev);
1953
1954 if (!uap)
1955 return -EINVAL;
1956
1957 return uart_suspend_port(&amba_reg, &uap->port);
1958}
1959
1960static int pl011_resume(struct amba_device *dev)
1961{
1962 struct uart_amba_port *uap = amba_get_drvdata(dev);
1963
1964 if (!uap)
1965 return -EINVAL;
1966
1967 return uart_resume_port(&amba_reg, &uap->port);
1968}
1969#endif
1970
1971static struct amba_id pl011_ids[] = {
1972 {
1973 .id = 0x00041011,
1974 .mask = 0x000fffff,
1975 .data = &vendor_arm,
1976 },
1977 {
1978 .id = 0x00380802,
1979 .mask = 0x00ffffff,
1980 .data = &vendor_st,
1981 },
1982 { 0, 0 },
1983};
1984
1985static struct amba_driver pl011_driver = {
1986 .drv = {
1987 .name = "uart-pl011",
1988 },
1989 .id_table = pl011_ids,
1990 .probe = pl011_probe,
1991 .remove = pl011_remove,
1992#ifdef CONFIG_PM
1993 .suspend = pl011_suspend,
1994 .resume = pl011_resume,
1995#endif
1996};
1997
1998static int __init pl011_init(void)
1999{
2000 int ret;
2001 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2002
2003 ret = uart_register_driver(&amba_reg);
2004 if (ret == 0) {
2005 ret = amba_driver_register(&pl011_driver);
2006 if (ret)
2007 uart_unregister_driver(&amba_reg);
2008 }
2009 return ret;
2010}
2011
2012static void __exit pl011_exit(void)
2013{
2014 amba_driver_unregister(&pl011_driver);
2015 uart_unregister_driver(&amba_reg);
2016}
2017
2018/*
2019 * While this can be a module, if builtin it's most likely the console
2020 * So let's leave module_exit but move module_init to an earlier place
2021 */
2022arch_initcall(pl011_init);
2023module_exit(pl011_exit);
2024
2025MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2026MODULE_DESCRIPTION("ARM AMBA serial port driver");
2027MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/apbuart.c b/drivers/tty/serial/apbuart.c
new file mode 100644
index 000000000000..19a943693e4c
--- /dev/null
+++ b/drivers/tty/serial/apbuart.c
@@ -0,0 +1,696 @@
1/*
2 * Driver for GRLIB serial ports (APBUART)
3 *
4 * Based on linux/drivers/serial/amba.c
5 *
6 * Copyright (C) 2000 Deep Blue Solutions Ltd.
7 * Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
8 * Copyright (C) 2006 Daniel Hellstrom <daniel@gaisler.com>, Aeroflex Gaisler AB
9 * Copyright (C) 2008 Gilead Kutnick <kutnickg@zin-tech.com>
10 * Copyright (C) 2009 Kristoffer Glembo <kristoffer@gaisler.com>, Aeroflex Gaisler AB
11 */
12
13#if defined(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14#define SUPPORT_SYSRQ
15#endif
16
17#include <linux/module.h>
18#include <linux/tty.h>
19#include <linux/ioport.h>
20#include <linux/init.h>
21#include <linux/serial.h>
22#include <linux/console.h>
23#include <linux/sysrq.h>
24#include <linux/kthread.h>
25#include <linux/device.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/of_platform.h>
29#include <linux/of_irq.h>
30#include <linux/platform_device.h>
31#include <linux/io.h>
32#include <linux/serial_core.h>
33#include <asm/irq.h>
34
35#include "apbuart.h"
36
37#define SERIAL_APBUART_MAJOR TTY_MAJOR
38#define SERIAL_APBUART_MINOR 64
39#define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
40
41static void apbuart_tx_chars(struct uart_port *port);
42
43static void apbuart_stop_tx(struct uart_port *port)
44{
45 unsigned int cr;
46
47 cr = UART_GET_CTRL(port);
48 cr &= ~UART_CTRL_TI;
49 UART_PUT_CTRL(port, cr);
50}
51
52static void apbuart_start_tx(struct uart_port *port)
53{
54 unsigned int cr;
55
56 cr = UART_GET_CTRL(port);
57 cr |= UART_CTRL_TI;
58 UART_PUT_CTRL(port, cr);
59
60 if (UART_GET_STATUS(port) & UART_STATUS_THE)
61 apbuart_tx_chars(port);
62}
63
64static void apbuart_stop_rx(struct uart_port *port)
65{
66 unsigned int cr;
67
68 cr = UART_GET_CTRL(port);
69 cr &= ~(UART_CTRL_RI);
70 UART_PUT_CTRL(port, cr);
71}
72
73static void apbuart_enable_ms(struct uart_port *port)
74{
75 /* No modem status change interrupts for APBUART */
76}
77
78static void apbuart_rx_chars(struct uart_port *port)
79{
80 struct tty_struct *tty = port->state->port.tty;
81 unsigned int status, ch, rsr, flag;
82 unsigned int max_chars = port->fifosize;
83
84 status = UART_GET_STATUS(port);
85
86 while (UART_RX_DATA(status) && (max_chars--)) {
87
88 ch = UART_GET_CHAR(port);
89 flag = TTY_NORMAL;
90
91 port->icount.rx++;
92
93 rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
94 UART_PUT_STATUS(port, 0);
95 if (rsr & UART_STATUS_ERR) {
96
97 if (rsr & UART_STATUS_BR) {
98 rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
99 port->icount.brk++;
100 if (uart_handle_break(port))
101 goto ignore_char;
102 } else if (rsr & UART_STATUS_PE) {
103 port->icount.parity++;
104 } else if (rsr & UART_STATUS_FE) {
105 port->icount.frame++;
106 }
107 if (rsr & UART_STATUS_OE)
108 port->icount.overrun++;
109
110 rsr &= port->read_status_mask;
111
112 if (rsr & UART_STATUS_PE)
113 flag = TTY_PARITY;
114 else if (rsr & UART_STATUS_FE)
115 flag = TTY_FRAME;
116 }
117
118 if (uart_handle_sysrq_char(port, ch))
119 goto ignore_char;
120
121 uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
122
123
124 ignore_char:
125 status = UART_GET_STATUS(port);
126 }
127
128 tty_flip_buffer_push(tty);
129}
130
131static void apbuart_tx_chars(struct uart_port *port)
132{
133 struct circ_buf *xmit = &port->state->xmit;
134 int count;
135
136 if (port->x_char) {
137 UART_PUT_CHAR(port, port->x_char);
138 port->icount.tx++;
139 port->x_char = 0;
140 return;
141 }
142
143 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
144 apbuart_stop_tx(port);
145 return;
146 }
147
148 /* amba: fill FIFO */
149 count = port->fifosize >> 1;
150 do {
151 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
152 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
153 port->icount.tx++;
154 if (uart_circ_empty(xmit))
155 break;
156 } while (--count > 0);
157
158 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
159 uart_write_wakeup(port);
160
161 if (uart_circ_empty(xmit))
162 apbuart_stop_tx(port);
163}
164
165static irqreturn_t apbuart_int(int irq, void *dev_id)
166{
167 struct uart_port *port = dev_id;
168 unsigned int status;
169
170 spin_lock(&port->lock);
171
172 status = UART_GET_STATUS(port);
173 if (status & UART_STATUS_DR)
174 apbuart_rx_chars(port);
175 if (status & UART_STATUS_THE)
176 apbuart_tx_chars(port);
177
178 spin_unlock(&port->lock);
179
180 return IRQ_HANDLED;
181}
182
183static unsigned int apbuart_tx_empty(struct uart_port *port)
184{
185 unsigned int status = UART_GET_STATUS(port);
186 return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
187}
188
189static unsigned int apbuart_get_mctrl(struct uart_port *port)
190{
191 /* The GRLIB APBUART handles flow control in hardware */
192 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
193}
194
195static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
196{
197 /* The GRLIB APBUART handles flow control in hardware */
198}
199
200static void apbuart_break_ctl(struct uart_port *port, int break_state)
201{
202 /* We don't support sending break */
203}
204
205static int apbuart_startup(struct uart_port *port)
206{
207 int retval;
208 unsigned int cr;
209
210 /* Allocate the IRQ */
211 retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
212 if (retval)
213 return retval;
214
215 /* Finally, enable interrupts */
216 cr = UART_GET_CTRL(port);
217 UART_PUT_CTRL(port,
218 cr | UART_CTRL_RE | UART_CTRL_TE |
219 UART_CTRL_RI | UART_CTRL_TI);
220
221 return 0;
222}
223
224static void apbuart_shutdown(struct uart_port *port)
225{
226 unsigned int cr;
227
228 /* disable all interrupts, disable the port */
229 cr = UART_GET_CTRL(port);
230 UART_PUT_CTRL(port,
231 cr & ~(UART_CTRL_RE | UART_CTRL_TE |
232 UART_CTRL_RI | UART_CTRL_TI));
233
234 /* Free the interrupt */
235 free_irq(port->irq, port);
236}
237
238static void apbuart_set_termios(struct uart_port *port,
239 struct ktermios *termios, struct ktermios *old)
240{
241 unsigned int cr;
242 unsigned long flags;
243 unsigned int baud, quot;
244
245 /* Ask the core to calculate the divisor for us. */
246 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
247 if (baud == 0)
248 panic("invalid baudrate %i\n", port->uartclk / 16);
249
250 /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
251 quot = (uart_get_divisor(port, baud)) * 2;
252 cr = UART_GET_CTRL(port);
253 cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
254
255 if (termios->c_cflag & PARENB) {
256 cr |= UART_CTRL_PE;
257 if ((termios->c_cflag & PARODD))
258 cr |= UART_CTRL_PS;
259 }
260
261 /* Enable flow control. */
262 if (termios->c_cflag & CRTSCTS)
263 cr |= UART_CTRL_FL;
264
265 spin_lock_irqsave(&port->lock, flags);
266
267 /* Update the per-port timeout. */
268 uart_update_timeout(port, termios->c_cflag, baud);
269
270 port->read_status_mask = UART_STATUS_OE;
271 if (termios->c_iflag & INPCK)
272 port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
273
274 /* Characters to ignore */
275 port->ignore_status_mask = 0;
276 if (termios->c_iflag & IGNPAR)
277 port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
278
279 /* Ignore all characters if CREAD is not set. */
280 if ((termios->c_cflag & CREAD) == 0)
281 port->ignore_status_mask |= UART_DUMMY_RSR_RX;
282
283 /* Set baud rate */
284 quot -= 1;
285 UART_PUT_SCAL(port, quot);
286 UART_PUT_CTRL(port, cr);
287
288 spin_unlock_irqrestore(&port->lock, flags);
289}
290
291static const char *apbuart_type(struct uart_port *port)
292{
293 return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
294}
295
296static void apbuart_release_port(struct uart_port *port)
297{
298 release_mem_region(port->mapbase, 0x100);
299}
300
301static int apbuart_request_port(struct uart_port *port)
302{
303 return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
304 != NULL ? 0 : -EBUSY;
305 return 0;
306}
307
308/* Configure/autoconfigure the port */
309static void apbuart_config_port(struct uart_port *port, int flags)
310{
311 if (flags & UART_CONFIG_TYPE) {
312 port->type = PORT_APBUART;
313 apbuart_request_port(port);
314 }
315}
316
317/* Verify the new serial_struct (for TIOCSSERIAL) */
318static int apbuart_verify_port(struct uart_port *port,
319 struct serial_struct *ser)
320{
321 int ret = 0;
322 if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
323 ret = -EINVAL;
324 if (ser->irq < 0 || ser->irq >= NR_IRQS)
325 ret = -EINVAL;
326 if (ser->baud_base < 9600)
327 ret = -EINVAL;
328 return ret;
329}
330
331static struct uart_ops grlib_apbuart_ops = {
332 .tx_empty = apbuart_tx_empty,
333 .set_mctrl = apbuart_set_mctrl,
334 .get_mctrl = apbuart_get_mctrl,
335 .stop_tx = apbuart_stop_tx,
336 .start_tx = apbuart_start_tx,
337 .stop_rx = apbuart_stop_rx,
338 .enable_ms = apbuart_enable_ms,
339 .break_ctl = apbuart_break_ctl,
340 .startup = apbuart_startup,
341 .shutdown = apbuart_shutdown,
342 .set_termios = apbuart_set_termios,
343 .type = apbuart_type,
344 .release_port = apbuart_release_port,
345 .request_port = apbuart_request_port,
346 .config_port = apbuart_config_port,
347 .verify_port = apbuart_verify_port,
348};
349
350static struct uart_port grlib_apbuart_ports[UART_NR];
351static struct device_node *grlib_apbuart_nodes[UART_NR];
352
353static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
354{
355 int ctrl, loop = 0;
356 int status;
357 int fifosize;
358 unsigned long flags;
359
360 ctrl = UART_GET_CTRL(port);
361
362 /*
363 * Enable the transceiver and wait for it to be ready to send data.
364 * Clear interrupts so that this process will not be externally
365 * interrupted in the middle (which can cause the transceiver to
366 * drain prematurely).
367 */
368
369 local_irq_save(flags);
370
371 UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
372
373 while (!UART_TX_READY(UART_GET_STATUS(port)))
374 loop++;
375
376 /*
377 * Disable the transceiver so data isn't actually sent during the
378 * actual test.
379 */
380
381 UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
382
383 fifosize = 1;
384 UART_PUT_CHAR(port, 0);
385
386 /*
387 * So long as transmitting a character increments the tranceivier FIFO
388 * length the FIFO must be at least that big. These bytes will
389 * automatically drain off of the FIFO.
390 */
391
392 status = UART_GET_STATUS(port);
393 while (((status >> 20) & 0x3F) == fifosize) {
394 fifosize++;
395 UART_PUT_CHAR(port, 0);
396 status = UART_GET_STATUS(port);
397 }
398
399 fifosize--;
400
401 UART_PUT_CTRL(port, ctrl);
402 local_irq_restore(flags);
403
404 if (fifosize == 0)
405 fifosize = 1;
406
407 return fifosize;
408}
409
410static void apbuart_flush_fifo(struct uart_port *port)
411{
412 int i;
413
414 for (i = 0; i < port->fifosize; i++)
415 UART_GET_CHAR(port);
416}
417
418
419/* ======================================================================== */
420/* Console driver, if enabled */
421/* ======================================================================== */
422
423#ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
424
425static void apbuart_console_putchar(struct uart_port *port, int ch)
426{
427 unsigned int status;
428 do {
429 status = UART_GET_STATUS(port);
430 } while (!UART_TX_READY(status));
431 UART_PUT_CHAR(port, ch);
432}
433
434static void
435apbuart_console_write(struct console *co, const char *s, unsigned int count)
436{
437 struct uart_port *port = &grlib_apbuart_ports[co->index];
438 unsigned int status, old_cr, new_cr;
439
440 /* First save the CR then disable the interrupts */
441 old_cr = UART_GET_CTRL(port);
442 new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
443 UART_PUT_CTRL(port, new_cr);
444
445 uart_console_write(port, s, count, apbuart_console_putchar);
446
447 /*
448 * Finally, wait for transmitter to become empty
449 * and restore the TCR
450 */
451 do {
452 status = UART_GET_STATUS(port);
453 } while (!UART_TX_READY(status));
454 UART_PUT_CTRL(port, old_cr);
455}
456
457static void __init
458apbuart_console_get_options(struct uart_port *port, int *baud,
459 int *parity, int *bits)
460{
461 if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
462
463 unsigned int quot, status;
464 status = UART_GET_STATUS(port);
465
466 *parity = 'n';
467 if (status & UART_CTRL_PE) {
468 if ((status & UART_CTRL_PS) == 0)
469 *parity = 'e';
470 else
471 *parity = 'o';
472 }
473
474 *bits = 8;
475 quot = UART_GET_SCAL(port) / 8;
476 *baud = port->uartclk / (16 * (quot + 1));
477 }
478}
479
480static int __init apbuart_console_setup(struct console *co, char *options)
481{
482 struct uart_port *port;
483 int baud = 38400;
484 int bits = 8;
485 int parity = 'n';
486 int flow = 'n';
487
488 pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
489 co, co->index, options);
490
491 /*
492 * Check whether an invalid uart number has been specified, and
493 * if so, search for the first available port that does have
494 * console support.
495 */
496 if (co->index >= grlib_apbuart_port_nr)
497 co->index = 0;
498
499 port = &grlib_apbuart_ports[co->index];
500
501 spin_lock_init(&port->lock);
502
503 if (options)
504 uart_parse_options(options, &baud, &parity, &bits, &flow);
505 else
506 apbuart_console_get_options(port, &baud, &parity, &bits);
507
508 return uart_set_options(port, co, baud, parity, bits, flow);
509}
510
511static struct uart_driver grlib_apbuart_driver;
512
513static struct console grlib_apbuart_console = {
514 .name = "ttyS",
515 .write = apbuart_console_write,
516 .device = uart_console_device,
517 .setup = apbuart_console_setup,
518 .flags = CON_PRINTBUFFER,
519 .index = -1,
520 .data = &grlib_apbuart_driver,
521};
522
523
524static int grlib_apbuart_configure(void);
525
526static int __init apbuart_console_init(void)
527{
528 if (grlib_apbuart_configure())
529 return -ENODEV;
530 register_console(&grlib_apbuart_console);
531 return 0;
532}
533
534console_initcall(apbuart_console_init);
535
536#define APBUART_CONSOLE (&grlib_apbuart_console)
537#else
538#define APBUART_CONSOLE NULL
539#endif
540
541static struct uart_driver grlib_apbuart_driver = {
542 .owner = THIS_MODULE,
543 .driver_name = "serial",
544 .dev_name = "ttyS",
545 .major = SERIAL_APBUART_MAJOR,
546 .minor = SERIAL_APBUART_MINOR,
547 .nr = UART_NR,
548 .cons = APBUART_CONSOLE,
549};
550
551
552/* ======================================================================== */
553/* OF Platform Driver */
554/* ======================================================================== */
555
556static int __devinit apbuart_probe(struct platform_device *op)
557{
558 int i;
559 struct uart_port *port = NULL;
560
561 for (i = 0; i < grlib_apbuart_port_nr; i++) {
562 if (op->dev.of_node == grlib_apbuart_nodes[i])
563 break;
564 }
565
566 port = &grlib_apbuart_ports[i];
567 port->dev = &op->dev;
568 port->irq = op->archdata.irqs[0];
569
570 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
571
572 apbuart_flush_fifo((struct uart_port *) port);
573
574 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
575 (unsigned long long) port->mapbase, port->irq);
576 return 0;
577}
578
579static struct of_device_id __initdata apbuart_match[] = {
580 {
581 .name = "GAISLER_APBUART",
582 },
583 {
584 .name = "01_00c",
585 },
586 {},
587};
588
589static struct platform_driver grlib_apbuart_of_driver = {
590 .probe = apbuart_probe,
591 .driver = {
592 .owner = THIS_MODULE,
593 .name = "grlib-apbuart",
594 .of_match_table = apbuart_match,
595 },
596};
597
598
599static int grlib_apbuart_configure(void)
600{
601 struct device_node *np;
602 int line = 0;
603
604 for_each_matching_node(np, apbuart_match) {
605 const int *ampopts;
606 const u32 *freq_hz;
607 const struct amba_prom_registers *regs;
608 struct uart_port *port;
609 unsigned long addr;
610
611 ampopts = of_get_property(np, "ampopts", NULL);
612 if (ampopts && (*ampopts == 0))
613 continue; /* Ignore if used by another OS instance */
614 regs = of_get_property(np, "reg", NULL);
615 /* Frequency of APB Bus is frequency of UART */
616 freq_hz = of_get_property(np, "freq", NULL);
617
618 if (!regs || !freq_hz || (*freq_hz == 0))
619 continue;
620
621 grlib_apbuart_nodes[line] = np;
622
623 addr = regs->phys_addr;
624
625 port = &grlib_apbuart_ports[line];
626
627 port->mapbase = addr;
628 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
629 port->irq = 0;
630 port->iotype = UPIO_MEM;
631 port->ops = &grlib_apbuart_ops;
632 port->flags = UPF_BOOT_AUTOCONF;
633 port->line = line;
634 port->uartclk = *freq_hz;
635 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
636 line++;
637
638 /* We support maximum UART_NR uarts ... */
639 if (line == UART_NR)
640 break;
641 }
642
643 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
644 return line ? 0 : -ENODEV;
645}
646
647static int __init grlib_apbuart_init(void)
648{
649 int ret;
650
651 /* Find all APBUARTS in device the tree and initialize their ports */
652 ret = grlib_apbuart_configure();
653 if (ret)
654 return ret;
655
656 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
657
658 ret = uart_register_driver(&grlib_apbuart_driver);
659
660 if (ret) {
661 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
662 __FILE__, ret);
663 return ret;
664 }
665
666 ret = platform_driver_register(&grlib_apbuart_of_driver);
667 if (ret) {
668 printk(KERN_ERR
669 "%s: platform_driver_register failed (%i)\n",
670 __FILE__, ret);
671 uart_unregister_driver(&grlib_apbuart_driver);
672 return ret;
673 }
674
675 return ret;
676}
677
678static void __exit grlib_apbuart_exit(void)
679{
680 int i;
681
682 for (i = 0; i < grlib_apbuart_port_nr; i++)
683 uart_remove_one_port(&grlib_apbuart_driver,
684 &grlib_apbuart_ports[i]);
685
686 uart_unregister_driver(&grlib_apbuart_driver);
687 platform_driver_unregister(&grlib_apbuart_of_driver);
688}
689
690module_init(grlib_apbuart_init);
691module_exit(grlib_apbuart_exit);
692
693MODULE_AUTHOR("Aeroflex Gaisler AB");
694MODULE_DESCRIPTION("GRLIB APBUART serial driver");
695MODULE_VERSION("2.1");
696MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/apbuart.h b/drivers/tty/serial/apbuart.h
new file mode 100644
index 000000000000..5faf87c8d2bc
--- /dev/null
+++ b/drivers/tty/serial/apbuart.h
@@ -0,0 +1,64 @@
1#ifndef __GRLIB_APBUART_H__
2#define __GRLIB_APBUART_H__
3
4#include <asm/io.h>
5
6#define UART_NR 8
7static int grlib_apbuart_port_nr;
8
9struct grlib_apbuart_regs_map {
10 u32 data;
11 u32 status;
12 u32 ctrl;
13 u32 scaler;
14};
15
16struct amba_prom_registers {
17 unsigned int phys_addr;
18 unsigned int reg_size;
19};
20
21/*
22 * The following defines the bits in the APBUART Status Registers.
23 */
24#define UART_STATUS_DR 0x00000001 /* Data Ready */
25#define UART_STATUS_TSE 0x00000002 /* TX Send Register Empty */
26#define UART_STATUS_THE 0x00000004 /* TX Hold Register Empty */
27#define UART_STATUS_BR 0x00000008 /* Break Error */
28#define UART_STATUS_OE 0x00000010 /* RX Overrun Error */
29#define UART_STATUS_PE 0x00000020 /* RX Parity Error */
30#define UART_STATUS_FE 0x00000040 /* RX Framing Error */
31#define UART_STATUS_ERR 0x00000078 /* Error Mask */
32
33/*
34 * The following defines the bits in the APBUART Ctrl Registers.
35 */
36#define UART_CTRL_RE 0x00000001 /* Receiver enable */
37#define UART_CTRL_TE 0x00000002 /* Transmitter enable */
38#define UART_CTRL_RI 0x00000004 /* Receiver interrupt enable */
39#define UART_CTRL_TI 0x00000008 /* Transmitter irq */
40#define UART_CTRL_PS 0x00000010 /* Parity select */
41#define UART_CTRL_PE 0x00000020 /* Parity enable */
42#define UART_CTRL_FL 0x00000040 /* Flow control enable */
43#define UART_CTRL_LB 0x00000080 /* Loopback enable */
44
45#define APBBASE(port) ((struct grlib_apbuart_regs_map *)((port)->membase))
46
47#define APBBASE_DATA_P(port) (&(APBBASE(port)->data))
48#define APBBASE_STATUS_P(port) (&(APBBASE(port)->status))
49#define APBBASE_CTRL_P(port) (&(APBBASE(port)->ctrl))
50#define APBBASE_SCALAR_P(port) (&(APBBASE(port)->scaler))
51
52#define UART_GET_CHAR(port) (__raw_readl(APBBASE_DATA_P(port)))
53#define UART_PUT_CHAR(port, v) (__raw_writel(v, APBBASE_DATA_P(port)))
54#define UART_GET_STATUS(port) (__raw_readl(APBBASE_STATUS_P(port)))
55#define UART_PUT_STATUS(port, v)(__raw_writel(v, APBBASE_STATUS_P(port)))
56#define UART_GET_CTRL(port) (__raw_readl(APBBASE_CTRL_P(port)))
57#define UART_PUT_CTRL(port, v) (__raw_writel(v, APBBASE_CTRL_P(port)))
58#define UART_GET_SCAL(port) (__raw_readl(APBBASE_SCALAR_P(port)))
59#define UART_PUT_SCAL(port, v) (__raw_writel(v, APBBASE_SCALAR_P(port)))
60
61#define UART_RX_DATA(s) (((s) & UART_STATUS_DR) != 0)
62#define UART_TX_READY(s) (((s) & UART_STATUS_THE) != 0)
63
64#endif /* __GRLIB_APBUART_H__ */
diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
new file mode 100644
index 000000000000..af9b7814965a
--- /dev/null
+++ b/drivers/tty/serial/atmel_serial.c
@@ -0,0 +1,1828 @@
1/*
2 * Driver for Atmel AT91 / AT32 Serial ports
3 * Copyright (C) 2003 Rick Bronson
4 *
5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * DMA support added by Chip Coldwell.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25#include <linux/module.h>
26#include <linux/tty.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/serial.h>
31#include <linux/clk.h>
32#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
35#include <linux/platform_device.h>
36#include <linux/dma-mapping.h>
37#include <linux/atmel_pdc.h>
38#include <linux/atmel_serial.h>
39#include <linux/uaccess.h>
40
41#include <asm/io.h>
42#include <asm/ioctls.h>
43
44#include <asm/mach/serial_at91.h>
45#include <mach/board.h>
46
47#ifdef CONFIG_ARM
48#include <mach/cpu.h>
49#include <mach/gpio.h>
50#endif
51
52#define PDC_BUFFER_SIZE 512
53/* Revisit: We should calculate this based on the actual port settings */
54#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
55
56#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61
62static void atmel_start_rx(struct uart_port *port);
63static void atmel_stop_rx(struct uart_port *port);
64
65#ifdef CONFIG_SERIAL_ATMEL_TTYAT
66
67/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
68 * should coexist with the 8250 driver, such as if we have an external 16C550
69 * UART. */
70#define SERIAL_ATMEL_MAJOR 204
71#define MINOR_START 154
72#define ATMEL_DEVICENAME "ttyAT"
73
74#else
75
76/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
77 * name, but it is legally reserved for the 8250 driver. */
78#define SERIAL_ATMEL_MAJOR TTY_MAJOR
79#define MINOR_START 64
80#define ATMEL_DEVICENAME "ttyS"
81
82#endif
83
84#define ATMEL_ISR_PASS_LIMIT 256
85
86/* UART registers. CR is write-only, hence no GET macro */
87#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
88#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
89#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
90#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
91#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
92#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
93#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
94#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
95#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
96#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
97#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
98#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
99#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
100
101 /* PDC registers */
102#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
103#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
104
105#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
106#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
107#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
108#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
109#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
110
111#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
112#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
113#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
114
115static int (*atmel_open_hook)(struct uart_port *);
116static void (*atmel_close_hook)(struct uart_port *);
117
118struct atmel_dma_buffer {
119 unsigned char *buf;
120 dma_addr_t dma_addr;
121 unsigned int dma_size;
122 unsigned int ofs;
123};
124
125struct atmel_uart_char {
126 u16 status;
127 u16 ch;
128};
129
130#define ATMEL_SERIAL_RINGSIZE 1024
131
132/*
133 * We wrap our port structure around the generic uart_port.
134 */
135struct atmel_uart_port {
136 struct uart_port uart; /* uart */
137 struct clk *clk; /* uart clock */
138 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
139 u32 backup_imr; /* IMR saved during suspend */
140 int break_active; /* break being received */
141
142 short use_dma_rx; /* enable PDC receiver */
143 short pdc_rx_idx; /* current PDC RX buffer */
144 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
145
146 short use_dma_tx; /* enable PDC transmitter */
147 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
148
149 struct tasklet_struct tasklet;
150 unsigned int irq_status;
151 unsigned int irq_status_prev;
152
153 struct circ_buf rx_ring;
154
155 struct serial_rs485 rs485; /* rs485 settings */
156 unsigned int tx_done_mask;
157};
158
159static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
160
161#ifdef SUPPORT_SYSRQ
162static struct console atmel_console;
163#endif
164
165static inline struct atmel_uart_port *
166to_atmel_uart_port(struct uart_port *uart)
167{
168 return container_of(uart, struct atmel_uart_port, uart);
169}
170
171#ifdef CONFIG_SERIAL_ATMEL_PDC
172static bool atmel_use_dma_rx(struct uart_port *port)
173{
174 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
175
176 return atmel_port->use_dma_rx;
177}
178
179static bool atmel_use_dma_tx(struct uart_port *port)
180{
181 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
182
183 return atmel_port->use_dma_tx;
184}
185#else
186static bool atmel_use_dma_rx(struct uart_port *port)
187{
188 return false;
189}
190
191static bool atmel_use_dma_tx(struct uart_port *port)
192{
193 return false;
194}
195#endif
196
197/* Enable or disable the rs485 support */
198void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
199{
200 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
201 unsigned int mode;
202
203 spin_lock(&port->lock);
204
205 /* Disable interrupts */
206 UART_PUT_IDR(port, atmel_port->tx_done_mask);
207
208 mode = UART_GET_MR(port);
209
210 /* Resetting serial mode to RS232 (0x0) */
211 mode &= ~ATMEL_US_USMODE;
212
213 atmel_port->rs485 = *rs485conf;
214
215 if (rs485conf->flags & SER_RS485_ENABLED) {
216 dev_dbg(port->dev, "Setting UART to RS485\n");
217 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
218 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
219 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
220 mode |= ATMEL_US_USMODE_RS485;
221 } else {
222 dev_dbg(port->dev, "Setting UART to RS232\n");
223 if (atmel_use_dma_tx(port))
224 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
225 ATMEL_US_TXBUFE;
226 else
227 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
228 }
229 UART_PUT_MR(port, mode);
230
231 /* Enable interrupts */
232 UART_PUT_IER(port, atmel_port->tx_done_mask);
233
234 spin_unlock(&port->lock);
235
236}
237
238/*
239 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
240 */
241static u_int atmel_tx_empty(struct uart_port *port)
242{
243 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
244}
245
246/*
247 * Set state of the modem control output lines
248 */
249static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
250{
251 unsigned int control = 0;
252 unsigned int mode;
253 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
254
255#ifdef CONFIG_ARCH_AT91RM9200
256 if (cpu_is_at91rm9200()) {
257 /*
258 * AT91RM9200 Errata #39: RTS0 is not internally connected
259 * to PA21. We need to drive the pin manually.
260 */
261 if (port->mapbase == AT91RM9200_BASE_US0) {
262 if (mctrl & TIOCM_RTS)
263 at91_set_gpio_value(AT91_PIN_PA21, 0);
264 else
265 at91_set_gpio_value(AT91_PIN_PA21, 1);
266 }
267 }
268#endif
269
270 if (mctrl & TIOCM_RTS)
271 control |= ATMEL_US_RTSEN;
272 else
273 control |= ATMEL_US_RTSDIS;
274
275 if (mctrl & TIOCM_DTR)
276 control |= ATMEL_US_DTREN;
277 else
278 control |= ATMEL_US_DTRDIS;
279
280 UART_PUT_CR(port, control);
281
282 /* Local loopback mode? */
283 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
284 if (mctrl & TIOCM_LOOP)
285 mode |= ATMEL_US_CHMODE_LOC_LOOP;
286 else
287 mode |= ATMEL_US_CHMODE_NORMAL;
288
289 /* Resetting serial mode to RS232 (0x0) */
290 mode &= ~ATMEL_US_USMODE;
291
292 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
293 dev_dbg(port->dev, "Setting UART to RS485\n");
294 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
295 UART_PUT_TTGR(port,
296 atmel_port->rs485.delay_rts_after_send);
297 mode |= ATMEL_US_USMODE_RS485;
298 } else {
299 dev_dbg(port->dev, "Setting UART to RS232\n");
300 }
301 UART_PUT_MR(port, mode);
302}
303
304/*
305 * Get state of the modem control input lines
306 */
307static u_int atmel_get_mctrl(struct uart_port *port)
308{
309 unsigned int status, ret = 0;
310
311 status = UART_GET_CSR(port);
312
313 /*
314 * The control signals are active low.
315 */
316 if (!(status & ATMEL_US_DCD))
317 ret |= TIOCM_CD;
318 if (!(status & ATMEL_US_CTS))
319 ret |= TIOCM_CTS;
320 if (!(status & ATMEL_US_DSR))
321 ret |= TIOCM_DSR;
322 if (!(status & ATMEL_US_RI))
323 ret |= TIOCM_RI;
324
325 return ret;
326}
327
328/*
329 * Stop transmitting.
330 */
331static void atmel_stop_tx(struct uart_port *port)
332{
333 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
334
335 if (atmel_use_dma_tx(port)) {
336 /* disable PDC transmit */
337 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
338 }
339 /* Disable interrupts */
340 UART_PUT_IDR(port, atmel_port->tx_done_mask);
341
342 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
343 atmel_start_rx(port);
344}
345
346/*
347 * Start transmitting.
348 */
349static void atmel_start_tx(struct uart_port *port)
350{
351 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
352
353 if (atmel_use_dma_tx(port)) {
354 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
355 /* The transmitter is already running. Yes, we
356 really need this.*/
357 return;
358
359 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
360 atmel_stop_rx(port);
361
362 /* re-enable PDC transmit */
363 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
364 }
365 /* Enable interrupts */
366 UART_PUT_IER(port, atmel_port->tx_done_mask);
367}
368
369/*
370 * start receiving - port is in process of being opened.
371 */
372static void atmel_start_rx(struct uart_port *port)
373{
374 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
375
376 if (atmel_use_dma_rx(port)) {
377 /* enable PDC controller */
378 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
379 port->read_status_mask);
380 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
381 } else {
382 UART_PUT_IER(port, ATMEL_US_RXRDY);
383 }
384}
385
386/*
387 * Stop receiving - port is in process of being closed.
388 */
389static void atmel_stop_rx(struct uart_port *port)
390{
391 if (atmel_use_dma_rx(port)) {
392 /* disable PDC receive */
393 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
394 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
395 port->read_status_mask);
396 } else {
397 UART_PUT_IDR(port, ATMEL_US_RXRDY);
398 }
399}
400
401/*
402 * Enable modem status interrupts
403 */
404static void atmel_enable_ms(struct uart_port *port)
405{
406 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
407 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
408}
409
410/*
411 * Control the transmission of a break signal
412 */
413static void atmel_break_ctl(struct uart_port *port, int break_state)
414{
415 if (break_state != 0)
416 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
417 else
418 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
419}
420
421/*
422 * Stores the incoming character in the ring buffer
423 */
424static void
425atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
426 unsigned int ch)
427{
428 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
429 struct circ_buf *ring = &atmel_port->rx_ring;
430 struct atmel_uart_char *c;
431
432 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
433 /* Buffer overflow, ignore char */
434 return;
435
436 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
437 c->status = status;
438 c->ch = ch;
439
440 /* Make sure the character is stored before we update head. */
441 smp_wmb();
442
443 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
444}
445
446/*
447 * Deal with parity, framing and overrun errors.
448 */
449static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
450{
451 /* clear error */
452 UART_PUT_CR(port, ATMEL_US_RSTSTA);
453
454 if (status & ATMEL_US_RXBRK) {
455 /* ignore side-effect */
456 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
457 port->icount.brk++;
458 }
459 if (status & ATMEL_US_PARE)
460 port->icount.parity++;
461 if (status & ATMEL_US_FRAME)
462 port->icount.frame++;
463 if (status & ATMEL_US_OVRE)
464 port->icount.overrun++;
465}
466
467/*
468 * Characters received (called from interrupt handler)
469 */
470static void atmel_rx_chars(struct uart_port *port)
471{
472 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
473 unsigned int status, ch;
474
475 status = UART_GET_CSR(port);
476 while (status & ATMEL_US_RXRDY) {
477 ch = UART_GET_CHAR(port);
478
479 /*
480 * note that the error handling code is
481 * out of the main execution path
482 */
483 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
484 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
485 || atmel_port->break_active)) {
486
487 /* clear error */
488 UART_PUT_CR(port, ATMEL_US_RSTSTA);
489
490 if (status & ATMEL_US_RXBRK
491 && !atmel_port->break_active) {
492 atmel_port->break_active = 1;
493 UART_PUT_IER(port, ATMEL_US_RXBRK);
494 } else {
495 /*
496 * This is either the end-of-break
497 * condition or we've received at
498 * least one character without RXBRK
499 * being set. In both cases, the next
500 * RXBRK will indicate start-of-break.
501 */
502 UART_PUT_IDR(port, ATMEL_US_RXBRK);
503 status &= ~ATMEL_US_RXBRK;
504 atmel_port->break_active = 0;
505 }
506 }
507
508 atmel_buffer_rx_char(port, status, ch);
509 status = UART_GET_CSR(port);
510 }
511
512 tasklet_schedule(&atmel_port->tasklet);
513}
514
515/*
516 * Transmit characters (called from tasklet with TXRDY interrupt
517 * disabled)
518 */
519static void atmel_tx_chars(struct uart_port *port)
520{
521 struct circ_buf *xmit = &port->state->xmit;
522 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
523
524 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
525 UART_PUT_CHAR(port, port->x_char);
526 port->icount.tx++;
527 port->x_char = 0;
528 }
529 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
530 return;
531
532 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
533 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
534 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
535 port->icount.tx++;
536 if (uart_circ_empty(xmit))
537 break;
538 }
539
540 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
541 uart_write_wakeup(port);
542
543 if (!uart_circ_empty(xmit))
544 /* Enable interrupts */
545 UART_PUT_IER(port, atmel_port->tx_done_mask);
546}
547
548/*
549 * receive interrupt handler.
550 */
551static void
552atmel_handle_receive(struct uart_port *port, unsigned int pending)
553{
554 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
555
556 if (atmel_use_dma_rx(port)) {
557 /*
558 * PDC receive. Just schedule the tasklet and let it
559 * figure out the details.
560 *
561 * TODO: We're not handling error flags correctly at
562 * the moment.
563 */
564 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
565 UART_PUT_IDR(port, (ATMEL_US_ENDRX
566 | ATMEL_US_TIMEOUT));
567 tasklet_schedule(&atmel_port->tasklet);
568 }
569
570 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
571 ATMEL_US_FRAME | ATMEL_US_PARE))
572 atmel_pdc_rxerr(port, pending);
573 }
574
575 /* Interrupt receive */
576 if (pending & ATMEL_US_RXRDY)
577 atmel_rx_chars(port);
578 else if (pending & ATMEL_US_RXBRK) {
579 /*
580 * End of break detected. If it came along with a
581 * character, atmel_rx_chars will handle it.
582 */
583 UART_PUT_CR(port, ATMEL_US_RSTSTA);
584 UART_PUT_IDR(port, ATMEL_US_RXBRK);
585 atmel_port->break_active = 0;
586 }
587}
588
589/*
590 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
591 */
592static void
593atmel_handle_transmit(struct uart_port *port, unsigned int pending)
594{
595 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
596
597 if (pending & atmel_port->tx_done_mask) {
598 /* Either PDC or interrupt transmission */
599 UART_PUT_IDR(port, atmel_port->tx_done_mask);
600 tasklet_schedule(&atmel_port->tasklet);
601 }
602}
603
604/*
605 * status flags interrupt handler.
606 */
607static void
608atmel_handle_status(struct uart_port *port, unsigned int pending,
609 unsigned int status)
610{
611 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
612
613 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
614 | ATMEL_US_CTSIC)) {
615 atmel_port->irq_status = status;
616 tasklet_schedule(&atmel_port->tasklet);
617 }
618}
619
620/*
621 * Interrupt handler
622 */
623static irqreturn_t atmel_interrupt(int irq, void *dev_id)
624{
625 struct uart_port *port = dev_id;
626 unsigned int status, pending, pass_counter = 0;
627
628 do {
629 status = UART_GET_CSR(port);
630 pending = status & UART_GET_IMR(port);
631 if (!pending)
632 break;
633
634 atmel_handle_receive(port, pending);
635 atmel_handle_status(port, pending, status);
636 atmel_handle_transmit(port, pending);
637 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
638
639 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
640}
641
642/*
643 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
644 */
645static void atmel_tx_dma(struct uart_port *port)
646{
647 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
648 struct circ_buf *xmit = &port->state->xmit;
649 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
650 int count;
651
652 /* nothing left to transmit? */
653 if (UART_GET_TCR(port))
654 return;
655
656 xmit->tail += pdc->ofs;
657 xmit->tail &= UART_XMIT_SIZE - 1;
658
659 port->icount.tx += pdc->ofs;
660 pdc->ofs = 0;
661
662 /* more to transmit - setup next transfer */
663
664 /* disable PDC transmit */
665 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
666
667 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
668 dma_sync_single_for_device(port->dev,
669 pdc->dma_addr,
670 pdc->dma_size,
671 DMA_TO_DEVICE);
672
673 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
674 pdc->ofs = count;
675
676 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
677 UART_PUT_TCR(port, count);
678 /* re-enable PDC transmit */
679 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
680 /* Enable interrupts */
681 UART_PUT_IER(port, atmel_port->tx_done_mask);
682 } else {
683 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
684 /* DMA done, stop TX, start RX for RS485 */
685 atmel_start_rx(port);
686 }
687 }
688
689 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
690 uart_write_wakeup(port);
691}
692
693static void atmel_rx_from_ring(struct uart_port *port)
694{
695 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
696 struct circ_buf *ring = &atmel_port->rx_ring;
697 unsigned int flg;
698 unsigned int status;
699
700 while (ring->head != ring->tail) {
701 struct atmel_uart_char c;
702
703 /* Make sure c is loaded after head. */
704 smp_rmb();
705
706 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
707
708 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
709
710 port->icount.rx++;
711 status = c.status;
712 flg = TTY_NORMAL;
713
714 /*
715 * note that the error handling code is
716 * out of the main execution path
717 */
718 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
719 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
720 if (status & ATMEL_US_RXBRK) {
721 /* ignore side-effect */
722 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
723
724 port->icount.brk++;
725 if (uart_handle_break(port))
726 continue;
727 }
728 if (status & ATMEL_US_PARE)
729 port->icount.parity++;
730 if (status & ATMEL_US_FRAME)
731 port->icount.frame++;
732 if (status & ATMEL_US_OVRE)
733 port->icount.overrun++;
734
735 status &= port->read_status_mask;
736
737 if (status & ATMEL_US_RXBRK)
738 flg = TTY_BREAK;
739 else if (status & ATMEL_US_PARE)
740 flg = TTY_PARITY;
741 else if (status & ATMEL_US_FRAME)
742 flg = TTY_FRAME;
743 }
744
745
746 if (uart_handle_sysrq_char(port, c.ch))
747 continue;
748
749 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
750 }
751
752 /*
753 * Drop the lock here since it might end up calling
754 * uart_start(), which takes the lock.
755 */
756 spin_unlock(&port->lock);
757 tty_flip_buffer_push(port->state->port.tty);
758 spin_lock(&port->lock);
759}
760
761static void atmel_rx_from_dma(struct uart_port *port)
762{
763 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
764 struct tty_struct *tty = port->state->port.tty;
765 struct atmel_dma_buffer *pdc;
766 int rx_idx = atmel_port->pdc_rx_idx;
767 unsigned int head;
768 unsigned int tail;
769 unsigned int count;
770
771 do {
772 /* Reset the UART timeout early so that we don't miss one */
773 UART_PUT_CR(port, ATMEL_US_STTTO);
774
775 pdc = &atmel_port->pdc_rx[rx_idx];
776 head = UART_GET_RPR(port) - pdc->dma_addr;
777 tail = pdc->ofs;
778
779 /* If the PDC has switched buffers, RPR won't contain
780 * any address within the current buffer. Since head
781 * is unsigned, we just need a one-way comparison to
782 * find out.
783 *
784 * In this case, we just need to consume the entire
785 * buffer and resubmit it for DMA. This will clear the
786 * ENDRX bit as well, so that we can safely re-enable
787 * all interrupts below.
788 */
789 head = min(head, pdc->dma_size);
790
791 if (likely(head != tail)) {
792 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
793 pdc->dma_size, DMA_FROM_DEVICE);
794
795 /*
796 * head will only wrap around when we recycle
797 * the DMA buffer, and when that happens, we
798 * explicitly set tail to 0. So head will
799 * always be greater than tail.
800 */
801 count = head - tail;
802
803 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
804
805 dma_sync_single_for_device(port->dev, pdc->dma_addr,
806 pdc->dma_size, DMA_FROM_DEVICE);
807
808 port->icount.rx += count;
809 pdc->ofs = head;
810 }
811
812 /*
813 * If the current buffer is full, we need to check if
814 * the next one contains any additional data.
815 */
816 if (head >= pdc->dma_size) {
817 pdc->ofs = 0;
818 UART_PUT_RNPR(port, pdc->dma_addr);
819 UART_PUT_RNCR(port, pdc->dma_size);
820
821 rx_idx = !rx_idx;
822 atmel_port->pdc_rx_idx = rx_idx;
823 }
824 } while (head >= pdc->dma_size);
825
826 /*
827 * Drop the lock here since it might end up calling
828 * uart_start(), which takes the lock.
829 */
830 spin_unlock(&port->lock);
831 tty_flip_buffer_push(tty);
832 spin_lock(&port->lock);
833
834 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
835}
836
837/*
838 * tasklet handling tty stuff outside the interrupt handler.
839 */
840static void atmel_tasklet_func(unsigned long data)
841{
842 struct uart_port *port = (struct uart_port *)data;
843 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
844 unsigned int status;
845 unsigned int status_change;
846
847 /* The interrupt handler does not take the lock */
848 spin_lock(&port->lock);
849
850 if (atmel_use_dma_tx(port))
851 atmel_tx_dma(port);
852 else
853 atmel_tx_chars(port);
854
855 status = atmel_port->irq_status;
856 status_change = status ^ atmel_port->irq_status_prev;
857
858 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
859 | ATMEL_US_DCD | ATMEL_US_CTS)) {
860 /* TODO: All reads to CSR will clear these interrupts! */
861 if (status_change & ATMEL_US_RI)
862 port->icount.rng++;
863 if (status_change & ATMEL_US_DSR)
864 port->icount.dsr++;
865 if (status_change & ATMEL_US_DCD)
866 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
867 if (status_change & ATMEL_US_CTS)
868 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
869
870 wake_up_interruptible(&port->state->port.delta_msr_wait);
871
872 atmel_port->irq_status_prev = status;
873 }
874
875 if (atmel_use_dma_rx(port))
876 atmel_rx_from_dma(port);
877 else
878 atmel_rx_from_ring(port);
879
880 spin_unlock(&port->lock);
881}
882
883/*
884 * Perform initialization and enable port for reception
885 */
886static int atmel_startup(struct uart_port *port)
887{
888 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
889 struct tty_struct *tty = port->state->port.tty;
890 int retval;
891
892 /*
893 * Ensure that no interrupts are enabled otherwise when
894 * request_irq() is called we could get stuck trying to
895 * handle an unexpected interrupt
896 */
897 UART_PUT_IDR(port, -1);
898
899 /*
900 * Allocate the IRQ
901 */
902 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
903 tty ? tty->name : "atmel_serial", port);
904 if (retval) {
905 printk("atmel_serial: atmel_startup - Can't get irq\n");
906 return retval;
907 }
908
909 /*
910 * Initialize DMA (if necessary)
911 */
912 if (atmel_use_dma_rx(port)) {
913 int i;
914
915 for (i = 0; i < 2; i++) {
916 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
917
918 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
919 if (pdc->buf == NULL) {
920 if (i != 0) {
921 dma_unmap_single(port->dev,
922 atmel_port->pdc_rx[0].dma_addr,
923 PDC_BUFFER_SIZE,
924 DMA_FROM_DEVICE);
925 kfree(atmel_port->pdc_rx[0].buf);
926 }
927 free_irq(port->irq, port);
928 return -ENOMEM;
929 }
930 pdc->dma_addr = dma_map_single(port->dev,
931 pdc->buf,
932 PDC_BUFFER_SIZE,
933 DMA_FROM_DEVICE);
934 pdc->dma_size = PDC_BUFFER_SIZE;
935 pdc->ofs = 0;
936 }
937
938 atmel_port->pdc_rx_idx = 0;
939
940 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
941 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
942
943 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
944 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
945 }
946 if (atmel_use_dma_tx(port)) {
947 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
948 struct circ_buf *xmit = &port->state->xmit;
949
950 pdc->buf = xmit->buf;
951 pdc->dma_addr = dma_map_single(port->dev,
952 pdc->buf,
953 UART_XMIT_SIZE,
954 DMA_TO_DEVICE);
955 pdc->dma_size = UART_XMIT_SIZE;
956 pdc->ofs = 0;
957 }
958
959 /*
960 * If there is a specific "open" function (to register
961 * control line interrupts)
962 */
963 if (atmel_open_hook) {
964 retval = atmel_open_hook(port);
965 if (retval) {
966 free_irq(port->irq, port);
967 return retval;
968 }
969 }
970
971 /* Save current CSR for comparison in atmel_tasklet_func() */
972 atmel_port->irq_status_prev = UART_GET_CSR(port);
973 atmel_port->irq_status = atmel_port->irq_status_prev;
974
975 /*
976 * Finally, enable the serial port
977 */
978 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
979 /* enable xmit & rcvr */
980 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
981
982 if (atmel_use_dma_rx(port)) {
983 /* set UART timeout */
984 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
985 UART_PUT_CR(port, ATMEL_US_STTTO);
986
987 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
988 /* enable PDC controller */
989 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
990 } else {
991 /* enable receive only */
992 UART_PUT_IER(port, ATMEL_US_RXRDY);
993 }
994
995 return 0;
996}
997
998/*
999 * Disable the port
1000 */
1001static void atmel_shutdown(struct uart_port *port)
1002{
1003 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1004 /*
1005 * Ensure everything is stopped.
1006 */
1007 atmel_stop_rx(port);
1008 atmel_stop_tx(port);
1009
1010 /*
1011 * Shut-down the DMA.
1012 */
1013 if (atmel_use_dma_rx(port)) {
1014 int i;
1015
1016 for (i = 0; i < 2; i++) {
1017 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1018
1019 dma_unmap_single(port->dev,
1020 pdc->dma_addr,
1021 pdc->dma_size,
1022 DMA_FROM_DEVICE);
1023 kfree(pdc->buf);
1024 }
1025 }
1026 if (atmel_use_dma_tx(port)) {
1027 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1028
1029 dma_unmap_single(port->dev,
1030 pdc->dma_addr,
1031 pdc->dma_size,
1032 DMA_TO_DEVICE);
1033 }
1034
1035 /*
1036 * Disable all interrupts, port and break condition.
1037 */
1038 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1039 UART_PUT_IDR(port, -1);
1040
1041 /*
1042 * Free the interrupt
1043 */
1044 free_irq(port->irq, port);
1045
1046 /*
1047 * If there is a specific "close" function (to unregister
1048 * control line interrupts)
1049 */
1050 if (atmel_close_hook)
1051 atmel_close_hook(port);
1052}
1053
1054/*
1055 * Flush any TX data submitted for DMA. Called when the TX circular
1056 * buffer is reset.
1057 */
1058static void atmel_flush_buffer(struct uart_port *port)
1059{
1060 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1061
1062 if (atmel_use_dma_tx(port)) {
1063 UART_PUT_TCR(port, 0);
1064 atmel_port->pdc_tx.ofs = 0;
1065 }
1066}
1067
1068/*
1069 * Power / Clock management.
1070 */
1071static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1072 unsigned int oldstate)
1073{
1074 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1075
1076 switch (state) {
1077 case 0:
1078 /*
1079 * Enable the peripheral clock for this serial port.
1080 * This is called on uart_open() or a resume event.
1081 */
1082 clk_enable(atmel_port->clk);
1083
1084 /* re-enable interrupts if we disabled some on suspend */
1085 UART_PUT_IER(port, atmel_port->backup_imr);
1086 break;
1087 case 3:
1088 /* Back up the interrupt mask and disable all interrupts */
1089 atmel_port->backup_imr = UART_GET_IMR(port);
1090 UART_PUT_IDR(port, -1);
1091
1092 /*
1093 * Disable the peripheral clock for this serial port.
1094 * This is called on uart_close() or a suspend event.
1095 */
1096 clk_disable(atmel_port->clk);
1097 break;
1098 default:
1099 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1100 }
1101}
1102
1103/*
1104 * Change the port parameters
1105 */
1106static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1107 struct ktermios *old)
1108{
1109 unsigned long flags;
1110 unsigned int mode, imr, quot, baud;
1111 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1112
1113 /* Get current mode register */
1114 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1115 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1116 | ATMEL_US_USMODE);
1117
1118 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1119 quot = uart_get_divisor(port, baud);
1120
1121 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1122 quot /= 8;
1123 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1124 }
1125
1126 /* byte size */
1127 switch (termios->c_cflag & CSIZE) {
1128 case CS5:
1129 mode |= ATMEL_US_CHRL_5;
1130 break;
1131 case CS6:
1132 mode |= ATMEL_US_CHRL_6;
1133 break;
1134 case CS7:
1135 mode |= ATMEL_US_CHRL_7;
1136 break;
1137 default:
1138 mode |= ATMEL_US_CHRL_8;
1139 break;
1140 }
1141
1142 /* stop bits */
1143 if (termios->c_cflag & CSTOPB)
1144 mode |= ATMEL_US_NBSTOP_2;
1145
1146 /* parity */
1147 if (termios->c_cflag & PARENB) {
1148 /* Mark or Space parity */
1149 if (termios->c_cflag & CMSPAR) {
1150 if (termios->c_cflag & PARODD)
1151 mode |= ATMEL_US_PAR_MARK;
1152 else
1153 mode |= ATMEL_US_PAR_SPACE;
1154 } else if (termios->c_cflag & PARODD)
1155 mode |= ATMEL_US_PAR_ODD;
1156 else
1157 mode |= ATMEL_US_PAR_EVEN;
1158 } else
1159 mode |= ATMEL_US_PAR_NONE;
1160
1161 /* hardware handshake (RTS/CTS) */
1162 if (termios->c_cflag & CRTSCTS)
1163 mode |= ATMEL_US_USMODE_HWHS;
1164 else
1165 mode |= ATMEL_US_USMODE_NORMAL;
1166
1167 spin_lock_irqsave(&port->lock, flags);
1168
1169 port->read_status_mask = ATMEL_US_OVRE;
1170 if (termios->c_iflag & INPCK)
1171 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1172 if (termios->c_iflag & (BRKINT | PARMRK))
1173 port->read_status_mask |= ATMEL_US_RXBRK;
1174
1175 if (atmel_use_dma_rx(port))
1176 /* need to enable error interrupts */
1177 UART_PUT_IER(port, port->read_status_mask);
1178
1179 /*
1180 * Characters to ignore
1181 */
1182 port->ignore_status_mask = 0;
1183 if (termios->c_iflag & IGNPAR)
1184 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1185 if (termios->c_iflag & IGNBRK) {
1186 port->ignore_status_mask |= ATMEL_US_RXBRK;
1187 /*
1188 * If we're ignoring parity and break indicators,
1189 * ignore overruns too (for real raw support).
1190 */
1191 if (termios->c_iflag & IGNPAR)
1192 port->ignore_status_mask |= ATMEL_US_OVRE;
1193 }
1194 /* TODO: Ignore all characters if CREAD is set.*/
1195
1196 /* update the per-port timeout */
1197 uart_update_timeout(port, termios->c_cflag, baud);
1198
1199 /*
1200 * save/disable interrupts. The tty layer will ensure that the
1201 * transmitter is empty if requested by the caller, so there's
1202 * no need to wait for it here.
1203 */
1204 imr = UART_GET_IMR(port);
1205 UART_PUT_IDR(port, -1);
1206
1207 /* disable receiver and transmitter */
1208 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1209
1210 /* Resetting serial mode to RS232 (0x0) */
1211 mode &= ~ATMEL_US_USMODE;
1212
1213 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1214 dev_dbg(port->dev, "Setting UART to RS485\n");
1215 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1216 UART_PUT_TTGR(port,
1217 atmel_port->rs485.delay_rts_after_send);
1218 mode |= ATMEL_US_USMODE_RS485;
1219 } else {
1220 dev_dbg(port->dev, "Setting UART to RS232\n");
1221 }
1222
1223 /* set the parity, stop bits and data size */
1224 UART_PUT_MR(port, mode);
1225
1226 /* set the baud rate */
1227 UART_PUT_BRGR(port, quot);
1228 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1229 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1230
1231 /* restore interrupts */
1232 UART_PUT_IER(port, imr);
1233
1234 /* CTS flow-control and modem-status interrupts */
1235 if (UART_ENABLE_MS(port, termios->c_cflag))
1236 port->ops->enable_ms(port);
1237
1238 spin_unlock_irqrestore(&port->lock, flags);
1239}
1240
1241static void atmel_set_ldisc(struct uart_port *port, int new)
1242{
1243 int line = port->line;
1244
1245 if (line >= port->state->port.tty->driver->num)
1246 return;
1247
1248 if (port->state->port.tty->ldisc->ops->num == N_PPS) {
1249 port->flags |= UPF_HARDPPS_CD;
1250 atmel_enable_ms(port);
1251 } else {
1252 port->flags &= ~UPF_HARDPPS_CD;
1253 }
1254}
1255
1256/*
1257 * Return string describing the specified port
1258 */
1259static const char *atmel_type(struct uart_port *port)
1260{
1261 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1262}
1263
1264/*
1265 * Release the memory region(s) being used by 'port'.
1266 */
1267static void atmel_release_port(struct uart_port *port)
1268{
1269 struct platform_device *pdev = to_platform_device(port->dev);
1270 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1271
1272 release_mem_region(port->mapbase, size);
1273
1274 if (port->flags & UPF_IOREMAP) {
1275 iounmap(port->membase);
1276 port->membase = NULL;
1277 }
1278}
1279
1280/*
1281 * Request the memory region(s) being used by 'port'.
1282 */
1283static int atmel_request_port(struct uart_port *port)
1284{
1285 struct platform_device *pdev = to_platform_device(port->dev);
1286 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1287
1288 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1289 return -EBUSY;
1290
1291 if (port->flags & UPF_IOREMAP) {
1292 port->membase = ioremap(port->mapbase, size);
1293 if (port->membase == NULL) {
1294 release_mem_region(port->mapbase, size);
1295 return -ENOMEM;
1296 }
1297 }
1298
1299 return 0;
1300}
1301
1302/*
1303 * Configure/autoconfigure the port.
1304 */
1305static void atmel_config_port(struct uart_port *port, int flags)
1306{
1307 if (flags & UART_CONFIG_TYPE) {
1308 port->type = PORT_ATMEL;
1309 atmel_request_port(port);
1310 }
1311}
1312
1313/*
1314 * Verify the new serial_struct (for TIOCSSERIAL).
1315 */
1316static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1317{
1318 int ret = 0;
1319 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1320 ret = -EINVAL;
1321 if (port->irq != ser->irq)
1322 ret = -EINVAL;
1323 if (ser->io_type != SERIAL_IO_MEM)
1324 ret = -EINVAL;
1325 if (port->uartclk / 16 != ser->baud_base)
1326 ret = -EINVAL;
1327 if ((void *)port->mapbase != ser->iomem_base)
1328 ret = -EINVAL;
1329 if (port->iobase != ser->port)
1330 ret = -EINVAL;
1331 if (ser->hub6 != 0)
1332 ret = -EINVAL;
1333 return ret;
1334}
1335
1336#ifdef CONFIG_CONSOLE_POLL
1337static int atmel_poll_get_char(struct uart_port *port)
1338{
1339 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1340 cpu_relax();
1341
1342 return UART_GET_CHAR(port);
1343}
1344
1345static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1346{
1347 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1348 cpu_relax();
1349
1350 UART_PUT_CHAR(port, ch);
1351}
1352#endif
1353
1354static int
1355atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1356{
1357 struct serial_rs485 rs485conf;
1358
1359 switch (cmd) {
1360 case TIOCSRS485:
1361 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1362 sizeof(rs485conf)))
1363 return -EFAULT;
1364
1365 atmel_config_rs485(port, &rs485conf);
1366 break;
1367
1368 case TIOCGRS485:
1369 if (copy_to_user((struct serial_rs485 *) arg,
1370 &(to_atmel_uart_port(port)->rs485),
1371 sizeof(rs485conf)))
1372 return -EFAULT;
1373 break;
1374
1375 default:
1376 return -ENOIOCTLCMD;
1377 }
1378 return 0;
1379}
1380
1381
1382
1383static struct uart_ops atmel_pops = {
1384 .tx_empty = atmel_tx_empty,
1385 .set_mctrl = atmel_set_mctrl,
1386 .get_mctrl = atmel_get_mctrl,
1387 .stop_tx = atmel_stop_tx,
1388 .start_tx = atmel_start_tx,
1389 .stop_rx = atmel_stop_rx,
1390 .enable_ms = atmel_enable_ms,
1391 .break_ctl = atmel_break_ctl,
1392 .startup = atmel_startup,
1393 .shutdown = atmel_shutdown,
1394 .flush_buffer = atmel_flush_buffer,
1395 .set_termios = atmel_set_termios,
1396 .set_ldisc = atmel_set_ldisc,
1397 .type = atmel_type,
1398 .release_port = atmel_release_port,
1399 .request_port = atmel_request_port,
1400 .config_port = atmel_config_port,
1401 .verify_port = atmel_verify_port,
1402 .pm = atmel_serial_pm,
1403 .ioctl = atmel_ioctl,
1404#ifdef CONFIG_CONSOLE_POLL
1405 .poll_get_char = atmel_poll_get_char,
1406 .poll_put_char = atmel_poll_put_char,
1407#endif
1408};
1409
1410/*
1411 * Configure the port from the platform device resource info.
1412 */
1413static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1414 struct platform_device *pdev)
1415{
1416 struct uart_port *port = &atmel_port->uart;
1417 struct atmel_uart_data *data = pdev->dev.platform_data;
1418
1419 port->iotype = UPIO_MEM;
1420 port->flags = UPF_BOOT_AUTOCONF;
1421 port->ops = &atmel_pops;
1422 port->fifosize = 1;
1423 port->line = data->num;
1424 port->dev = &pdev->dev;
1425 port->mapbase = pdev->resource[0].start;
1426 port->irq = pdev->resource[1].start;
1427
1428 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1429 (unsigned long)port);
1430
1431 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1432
1433 if (data->regs)
1434 /* Already mapped by setup code */
1435 port->membase = data->regs;
1436 else {
1437 port->flags |= UPF_IOREMAP;
1438 port->membase = NULL;
1439 }
1440
1441 /* for console, the clock could already be configured */
1442 if (!atmel_port->clk) {
1443 atmel_port->clk = clk_get(&pdev->dev, "usart");
1444 clk_enable(atmel_port->clk);
1445 port->uartclk = clk_get_rate(atmel_port->clk);
1446 clk_disable(atmel_port->clk);
1447 /* only enable clock when USART is in use */
1448 }
1449
1450 atmel_port->use_dma_rx = data->use_dma_rx;
1451 atmel_port->use_dma_tx = data->use_dma_tx;
1452 atmel_port->rs485 = data->rs485;
1453 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1454 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1455 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1456 else if (atmel_use_dma_tx(port)) {
1457 port->fifosize = PDC_BUFFER_SIZE;
1458 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1459 } else {
1460 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1461 }
1462}
1463
1464/*
1465 * Register board-specific modem-control line handlers.
1466 */
1467void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1468{
1469 if (fns->enable_ms)
1470 atmel_pops.enable_ms = fns->enable_ms;
1471 if (fns->get_mctrl)
1472 atmel_pops.get_mctrl = fns->get_mctrl;
1473 if (fns->set_mctrl)
1474 atmel_pops.set_mctrl = fns->set_mctrl;
1475 atmel_open_hook = fns->open;
1476 atmel_close_hook = fns->close;
1477 atmel_pops.pm = fns->pm;
1478 atmel_pops.set_wake = fns->set_wake;
1479}
1480
1481#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1482static void atmel_console_putchar(struct uart_port *port, int ch)
1483{
1484 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1485 cpu_relax();
1486 UART_PUT_CHAR(port, ch);
1487}
1488
1489/*
1490 * Interrupts are disabled on entering
1491 */
1492static void atmel_console_write(struct console *co, const char *s, u_int count)
1493{
1494 struct uart_port *port = &atmel_ports[co->index].uart;
1495 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1496 unsigned int status, imr;
1497 unsigned int pdc_tx;
1498
1499 /*
1500 * First, save IMR and then disable interrupts
1501 */
1502 imr = UART_GET_IMR(port);
1503 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1504
1505 /* Store PDC transmit status and disable it */
1506 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1507 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1508
1509 uart_console_write(port, s, count, atmel_console_putchar);
1510
1511 /*
1512 * Finally, wait for transmitter to become empty
1513 * and restore IMR
1514 */
1515 do {
1516 status = UART_GET_CSR(port);
1517 } while (!(status & ATMEL_US_TXRDY));
1518
1519 /* Restore PDC transmit status */
1520 if (pdc_tx)
1521 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1522
1523 /* set interrupts back the way they were */
1524 UART_PUT_IER(port, imr);
1525}
1526
1527/*
1528 * If the port was already initialised (eg, by a boot loader),
1529 * try to determine the current setup.
1530 */
1531static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1532 int *parity, int *bits)
1533{
1534 unsigned int mr, quot;
1535
1536 /*
1537 * If the baud rate generator isn't running, the port wasn't
1538 * initialized by the boot loader.
1539 */
1540 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1541 if (!quot)
1542 return;
1543
1544 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1545 if (mr == ATMEL_US_CHRL_8)
1546 *bits = 8;
1547 else
1548 *bits = 7;
1549
1550 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1551 if (mr == ATMEL_US_PAR_EVEN)
1552 *parity = 'e';
1553 else if (mr == ATMEL_US_PAR_ODD)
1554 *parity = 'o';
1555
1556 /*
1557 * The serial core only rounds down when matching this to a
1558 * supported baud rate. Make sure we don't end up slightly
1559 * lower than one of those, as it would make us fall through
1560 * to a much lower baud rate than we really want.
1561 */
1562 *baud = port->uartclk / (16 * (quot - 1));
1563}
1564
1565static int __init atmel_console_setup(struct console *co, char *options)
1566{
1567 struct uart_port *port = &atmel_ports[co->index].uart;
1568 int baud = 115200;
1569 int bits = 8;
1570 int parity = 'n';
1571 int flow = 'n';
1572
1573 if (port->membase == NULL) {
1574 /* Port not initialized yet - delay setup */
1575 return -ENODEV;
1576 }
1577
1578 clk_enable(atmel_ports[co->index].clk);
1579
1580 UART_PUT_IDR(port, -1);
1581 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1582 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1583
1584 if (options)
1585 uart_parse_options(options, &baud, &parity, &bits, &flow);
1586 else
1587 atmel_console_get_options(port, &baud, &parity, &bits);
1588
1589 return uart_set_options(port, co, baud, parity, bits, flow);
1590}
1591
1592static struct uart_driver atmel_uart;
1593
1594static struct console atmel_console = {
1595 .name = ATMEL_DEVICENAME,
1596 .write = atmel_console_write,
1597 .device = uart_console_device,
1598 .setup = atmel_console_setup,
1599 .flags = CON_PRINTBUFFER,
1600 .index = -1,
1601 .data = &atmel_uart,
1602};
1603
1604#define ATMEL_CONSOLE_DEVICE (&atmel_console)
1605
1606/*
1607 * Early console initialization (before VM subsystem initialized).
1608 */
1609static int __init atmel_console_init(void)
1610{
1611 if (atmel_default_console_device) {
1612 add_preferred_console(ATMEL_DEVICENAME,
1613 atmel_default_console_device->id, NULL);
1614 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1615 atmel_default_console_device);
1616 register_console(&atmel_console);
1617 }
1618
1619 return 0;
1620}
1621
1622console_initcall(atmel_console_init);
1623
1624/*
1625 * Late console initialization.
1626 */
1627static int __init atmel_late_console_init(void)
1628{
1629 if (atmel_default_console_device
1630 && !(atmel_console.flags & CON_ENABLED))
1631 register_console(&atmel_console);
1632
1633 return 0;
1634}
1635
1636core_initcall(atmel_late_console_init);
1637
1638static inline bool atmel_is_console_port(struct uart_port *port)
1639{
1640 return port->cons && port->cons->index == port->line;
1641}
1642
1643#else
1644#define ATMEL_CONSOLE_DEVICE NULL
1645
1646static inline bool atmel_is_console_port(struct uart_port *port)
1647{
1648 return false;
1649}
1650#endif
1651
1652static struct uart_driver atmel_uart = {
1653 .owner = THIS_MODULE,
1654 .driver_name = "atmel_serial",
1655 .dev_name = ATMEL_DEVICENAME,
1656 .major = SERIAL_ATMEL_MAJOR,
1657 .minor = MINOR_START,
1658 .nr = ATMEL_MAX_UART,
1659 .cons = ATMEL_CONSOLE_DEVICE,
1660};
1661
1662#ifdef CONFIG_PM
1663static bool atmel_serial_clk_will_stop(void)
1664{
1665#ifdef CONFIG_ARCH_AT91
1666 return at91_suspend_entering_slow_clock();
1667#else
1668 return false;
1669#endif
1670}
1671
1672static int atmel_serial_suspend(struct platform_device *pdev,
1673 pm_message_t state)
1674{
1675 struct uart_port *port = platform_get_drvdata(pdev);
1676 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1677
1678 if (atmel_is_console_port(port) && console_suspend_enabled) {
1679 /* Drain the TX shifter */
1680 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1681 cpu_relax();
1682 }
1683
1684 /* we can not wake up if we're running on slow clock */
1685 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1686 if (atmel_serial_clk_will_stop())
1687 device_set_wakeup_enable(&pdev->dev, 0);
1688
1689 uart_suspend_port(&atmel_uart, port);
1690
1691 return 0;
1692}
1693
1694static int atmel_serial_resume(struct platform_device *pdev)
1695{
1696 struct uart_port *port = platform_get_drvdata(pdev);
1697 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1698
1699 uart_resume_port(&atmel_uart, port);
1700 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1701
1702 return 0;
1703}
1704#else
1705#define atmel_serial_suspend NULL
1706#define atmel_serial_resume NULL
1707#endif
1708
1709static int __devinit atmel_serial_probe(struct platform_device *pdev)
1710{
1711 struct atmel_uart_port *port;
1712 struct atmel_uart_data *pdata = pdev->dev.platform_data;
1713 void *data;
1714 int ret;
1715
1716 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1717
1718 port = &atmel_ports[pdata->num];
1719 port->backup_imr = 0;
1720
1721 atmel_init_port(port, pdev);
1722
1723 if (!atmel_use_dma_rx(&port->uart)) {
1724 ret = -ENOMEM;
1725 data = kmalloc(sizeof(struct atmel_uart_char)
1726 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1727 if (!data)
1728 goto err_alloc_ring;
1729 port->rx_ring.buf = data;
1730 }
1731
1732 ret = uart_add_one_port(&atmel_uart, &port->uart);
1733 if (ret)
1734 goto err_add_port;
1735
1736#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1737 if (atmel_is_console_port(&port->uart)
1738 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1739 /*
1740 * The serial core enabled the clock for us, so undo
1741 * the clk_enable() in atmel_console_setup()
1742 */
1743 clk_disable(port->clk);
1744 }
1745#endif
1746
1747 device_init_wakeup(&pdev->dev, 1);
1748 platform_set_drvdata(pdev, port);
1749
1750 if (port->rs485.flags & SER_RS485_ENABLED) {
1751 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
1752 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1753 }
1754
1755 return 0;
1756
1757err_add_port:
1758 kfree(port->rx_ring.buf);
1759 port->rx_ring.buf = NULL;
1760err_alloc_ring:
1761 if (!atmel_is_console_port(&port->uart)) {
1762 clk_put(port->clk);
1763 port->clk = NULL;
1764 }
1765
1766 return ret;
1767}
1768
1769static int __devexit atmel_serial_remove(struct platform_device *pdev)
1770{
1771 struct uart_port *port = platform_get_drvdata(pdev);
1772 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1773 int ret = 0;
1774
1775 device_init_wakeup(&pdev->dev, 0);
1776 platform_set_drvdata(pdev, NULL);
1777
1778 ret = uart_remove_one_port(&atmel_uart, port);
1779
1780 tasklet_kill(&atmel_port->tasklet);
1781 kfree(atmel_port->rx_ring.buf);
1782
1783 /* "port" is allocated statically, so we shouldn't free it */
1784
1785 clk_put(atmel_port->clk);
1786
1787 return ret;
1788}
1789
1790static struct platform_driver atmel_serial_driver = {
1791 .probe = atmel_serial_probe,
1792 .remove = __devexit_p(atmel_serial_remove),
1793 .suspend = atmel_serial_suspend,
1794 .resume = atmel_serial_resume,
1795 .driver = {
1796 .name = "atmel_usart",
1797 .owner = THIS_MODULE,
1798 },
1799};
1800
1801static int __init atmel_serial_init(void)
1802{
1803 int ret;
1804
1805 ret = uart_register_driver(&atmel_uart);
1806 if (ret)
1807 return ret;
1808
1809 ret = platform_driver_register(&atmel_serial_driver);
1810 if (ret)
1811 uart_unregister_driver(&atmel_uart);
1812
1813 return ret;
1814}
1815
1816static void __exit atmel_serial_exit(void)
1817{
1818 platform_driver_unregister(&atmel_serial_driver);
1819 uart_unregister_driver(&atmel_uart);
1820}
1821
1822module_init(atmel_serial_init);
1823module_exit(atmel_serial_exit);
1824
1825MODULE_AUTHOR("Rick Bronson");
1826MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1827MODULE_LICENSE("GPL");
1828MODULE_ALIAS("platform:atmel_usart");
diff --git a/drivers/tty/serial/bcm63xx_uart.c b/drivers/tty/serial/bcm63xx_uart.c
new file mode 100644
index 000000000000..c0b68b9cad91
--- /dev/null
+++ b/drivers/tty/serial/bcm63xx_uart.c
@@ -0,0 +1,901 @@
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Derived from many drivers using generic_serial interface.
7 *
8 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
9 *
10 * Serial driver for BCM63xx integrated UART.
11 *
12 * Hardware flow control was _not_ tested since I only have RX/TX on
13 * my board.
14 */
15
16#if defined(CONFIG_SERIAL_BCM63XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17#define SUPPORT_SYSRQ
18#endif
19
20#include <linux/kernel.h>
21#include <linux/platform_device.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/console.h>
26#include <linux/clk.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/sysrq.h>
30#include <linux/serial.h>
31#include <linux/serial_core.h>
32
33#include <bcm63xx_clk.h>
34#include <bcm63xx_irq.h>
35#include <bcm63xx_regs.h>
36#include <bcm63xx_io.h>
37
38#define BCM63XX_NR_UARTS 2
39
40static struct uart_port ports[BCM63XX_NR_UARTS];
41
42/*
43 * rx interrupt mask / stat
44 *
45 * mask:
46 * - rx fifo full
47 * - rx fifo above threshold
48 * - rx fifo not empty for too long
49 */
50#define UART_RX_INT_MASK (UART_IR_MASK(UART_IR_RXOVER) | \
51 UART_IR_MASK(UART_IR_RXTHRESH) | \
52 UART_IR_MASK(UART_IR_RXTIMEOUT))
53
54#define UART_RX_INT_STAT (UART_IR_STAT(UART_IR_RXOVER) | \
55 UART_IR_STAT(UART_IR_RXTHRESH) | \
56 UART_IR_STAT(UART_IR_RXTIMEOUT))
57
58/*
59 * tx interrupt mask / stat
60 *
61 * mask:
62 * - tx fifo empty
63 * - tx fifo below threshold
64 */
65#define UART_TX_INT_MASK (UART_IR_MASK(UART_IR_TXEMPTY) | \
66 UART_IR_MASK(UART_IR_TXTRESH))
67
68#define UART_TX_INT_STAT (UART_IR_STAT(UART_IR_TXEMPTY) | \
69 UART_IR_STAT(UART_IR_TXTRESH))
70
71/*
72 * external input interrupt
73 *
74 * mask: any edge on CTS, DCD
75 */
76#define UART_EXTINP_INT_MASK (UART_EXTINP_IRMASK(UART_EXTINP_IR_CTS) | \
77 UART_EXTINP_IRMASK(UART_EXTINP_IR_DCD))
78
79/*
80 * handy uart register accessor
81 */
82static inline unsigned int bcm_uart_readl(struct uart_port *port,
83 unsigned int offset)
84{
85 return bcm_readl(port->membase + offset);
86}
87
88static inline void bcm_uart_writel(struct uart_port *port,
89 unsigned int value, unsigned int offset)
90{
91 bcm_writel(value, port->membase + offset);
92}
93
94/*
95 * serial core request to check if uart tx fifo is empty
96 */
97static unsigned int bcm_uart_tx_empty(struct uart_port *port)
98{
99 unsigned int val;
100
101 val = bcm_uart_readl(port, UART_IR_REG);
102 return (val & UART_IR_STAT(UART_IR_TXEMPTY)) ? 1 : 0;
103}
104
105/*
106 * serial core request to set RTS and DTR pin state and loopback mode
107 */
108static void bcm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
109{
110 unsigned int val;
111
112 val = bcm_uart_readl(port, UART_MCTL_REG);
113 val &= ~(UART_MCTL_DTR_MASK | UART_MCTL_RTS_MASK);
114 /* invert of written value is reflected on the pin */
115 if (!(mctrl & TIOCM_DTR))
116 val |= UART_MCTL_DTR_MASK;
117 if (!(mctrl & TIOCM_RTS))
118 val |= UART_MCTL_RTS_MASK;
119 bcm_uart_writel(port, val, UART_MCTL_REG);
120
121 val = bcm_uart_readl(port, UART_CTL_REG);
122 if (mctrl & TIOCM_LOOP)
123 val |= UART_CTL_LOOPBACK_MASK;
124 else
125 val &= ~UART_CTL_LOOPBACK_MASK;
126 bcm_uart_writel(port, val, UART_CTL_REG);
127}
128
129/*
130 * serial core request to return RI, CTS, DCD and DSR pin state
131 */
132static unsigned int bcm_uart_get_mctrl(struct uart_port *port)
133{
134 unsigned int val, mctrl;
135
136 mctrl = 0;
137 val = bcm_uart_readl(port, UART_EXTINP_REG);
138 if (val & UART_EXTINP_RI_MASK)
139 mctrl |= TIOCM_RI;
140 if (val & UART_EXTINP_CTS_MASK)
141 mctrl |= TIOCM_CTS;
142 if (val & UART_EXTINP_DCD_MASK)
143 mctrl |= TIOCM_CD;
144 if (val & UART_EXTINP_DSR_MASK)
145 mctrl |= TIOCM_DSR;
146 return mctrl;
147}
148
149/*
150 * serial core request to disable tx ASAP (used for flow control)
151 */
152static void bcm_uart_stop_tx(struct uart_port *port)
153{
154 unsigned int val;
155
156 val = bcm_uart_readl(port, UART_CTL_REG);
157 val &= ~(UART_CTL_TXEN_MASK);
158 bcm_uart_writel(port, val, UART_CTL_REG);
159
160 val = bcm_uart_readl(port, UART_IR_REG);
161 val &= ~UART_TX_INT_MASK;
162 bcm_uart_writel(port, val, UART_IR_REG);
163}
164
165/*
166 * serial core request to (re)enable tx
167 */
168static void bcm_uart_start_tx(struct uart_port *port)
169{
170 unsigned int val;
171
172 val = bcm_uart_readl(port, UART_IR_REG);
173 val |= UART_TX_INT_MASK;
174 bcm_uart_writel(port, val, UART_IR_REG);
175
176 val = bcm_uart_readl(port, UART_CTL_REG);
177 val |= UART_CTL_TXEN_MASK;
178 bcm_uart_writel(port, val, UART_CTL_REG);
179}
180
181/*
182 * serial core request to stop rx, called before port shutdown
183 */
184static void bcm_uart_stop_rx(struct uart_port *port)
185{
186 unsigned int val;
187
188 val = bcm_uart_readl(port, UART_IR_REG);
189 val &= ~UART_RX_INT_MASK;
190 bcm_uart_writel(port, val, UART_IR_REG);
191}
192
193/*
194 * serial core request to enable modem status interrupt reporting
195 */
196static void bcm_uart_enable_ms(struct uart_port *port)
197{
198 unsigned int val;
199
200 val = bcm_uart_readl(port, UART_IR_REG);
201 val |= UART_IR_MASK(UART_IR_EXTIP);
202 bcm_uart_writel(port, val, UART_IR_REG);
203}
204
205/*
206 * serial core request to start/stop emitting break char
207 */
208static void bcm_uart_break_ctl(struct uart_port *port, int ctl)
209{
210 unsigned long flags;
211 unsigned int val;
212
213 spin_lock_irqsave(&port->lock, flags);
214
215 val = bcm_uart_readl(port, UART_CTL_REG);
216 if (ctl)
217 val |= UART_CTL_XMITBRK_MASK;
218 else
219 val &= ~UART_CTL_XMITBRK_MASK;
220 bcm_uart_writel(port, val, UART_CTL_REG);
221
222 spin_unlock_irqrestore(&port->lock, flags);
223}
224
225/*
226 * return port type in string format
227 */
228static const char *bcm_uart_type(struct uart_port *port)
229{
230 return (port->type == PORT_BCM63XX) ? "bcm63xx_uart" : NULL;
231}
232
233/*
234 * read all chars in rx fifo and send them to core
235 */
236static void bcm_uart_do_rx(struct uart_port *port)
237{
238 struct tty_struct *tty;
239 unsigned int max_count;
240
241 /* limit number of char read in interrupt, should not be
242 * higher than fifo size anyway since we're much faster than
243 * serial port */
244 max_count = 32;
245 tty = port->state->port.tty;
246 do {
247 unsigned int iestat, c, cstat;
248 char flag;
249
250 /* get overrun/fifo empty information from ier
251 * register */
252 iestat = bcm_uart_readl(port, UART_IR_REG);
253
254 if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
255 unsigned int val;
256
257 /* fifo reset is required to clear
258 * interrupt */
259 val = bcm_uart_readl(port, UART_CTL_REG);
260 val |= UART_CTL_RSTRXFIFO_MASK;
261 bcm_uart_writel(port, val, UART_CTL_REG);
262
263 port->icount.overrun++;
264 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
265 }
266
267 if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
268 break;
269
270 cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
271 port->icount.rx++;
272 flag = TTY_NORMAL;
273 c &= 0xff;
274
275 if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
276 /* do stats first */
277 if (cstat & UART_FIFO_BRKDET_MASK) {
278 port->icount.brk++;
279 if (uart_handle_break(port))
280 continue;
281 }
282
283 if (cstat & UART_FIFO_PARERR_MASK)
284 port->icount.parity++;
285 if (cstat & UART_FIFO_FRAMEERR_MASK)
286 port->icount.frame++;
287
288 /* update flag wrt read_status_mask */
289 cstat &= port->read_status_mask;
290 if (cstat & UART_FIFO_BRKDET_MASK)
291 flag = TTY_BREAK;
292 if (cstat & UART_FIFO_FRAMEERR_MASK)
293 flag = TTY_FRAME;
294 if (cstat & UART_FIFO_PARERR_MASK)
295 flag = TTY_PARITY;
296 }
297
298 if (uart_handle_sysrq_char(port, c))
299 continue;
300
301
302 if ((cstat & port->ignore_status_mask) == 0)
303 tty_insert_flip_char(tty, c, flag);
304
305 } while (--max_count);
306
307 tty_flip_buffer_push(tty);
308}
309
310/*
311 * fill tx fifo with chars to send, stop when fifo is about to be full
312 * or when all chars have been sent.
313 */
314static void bcm_uart_do_tx(struct uart_port *port)
315{
316 struct circ_buf *xmit;
317 unsigned int val, max_count;
318
319 if (port->x_char) {
320 bcm_uart_writel(port, port->x_char, UART_FIFO_REG);
321 port->icount.tx++;
322 port->x_char = 0;
323 return;
324 }
325
326 if (uart_tx_stopped(port)) {
327 bcm_uart_stop_tx(port);
328 return;
329 }
330
331 xmit = &port->state->xmit;
332 if (uart_circ_empty(xmit))
333 goto txq_empty;
334
335 val = bcm_uart_readl(port, UART_MCTL_REG);
336 val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
337 max_count = port->fifosize - val;
338
339 while (max_count--) {
340 unsigned int c;
341
342 c = xmit->buf[xmit->tail];
343 bcm_uart_writel(port, c, UART_FIFO_REG);
344 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
345 port->icount.tx++;
346 if (uart_circ_empty(xmit))
347 break;
348 }
349
350 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
351 uart_write_wakeup(port);
352
353 if (uart_circ_empty(xmit))
354 goto txq_empty;
355 return;
356
357txq_empty:
358 /* nothing to send, disable transmit interrupt */
359 val = bcm_uart_readl(port, UART_IR_REG);
360 val &= ~UART_TX_INT_MASK;
361 bcm_uart_writel(port, val, UART_IR_REG);
362 return;
363}
364
365/*
366 * process uart interrupt
367 */
368static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
369{
370 struct uart_port *port;
371 unsigned int irqstat;
372
373 port = dev_id;
374 spin_lock(&port->lock);
375
376 irqstat = bcm_uart_readl(port, UART_IR_REG);
377 if (irqstat & UART_RX_INT_STAT)
378 bcm_uart_do_rx(port);
379
380 if (irqstat & UART_TX_INT_STAT)
381 bcm_uart_do_tx(port);
382
383 if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
384 unsigned int estat;
385
386 estat = bcm_uart_readl(port, UART_EXTINP_REG);
387 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
388 uart_handle_cts_change(port,
389 estat & UART_EXTINP_CTS_MASK);
390 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
391 uart_handle_dcd_change(port,
392 estat & UART_EXTINP_DCD_MASK);
393 }
394
395 spin_unlock(&port->lock);
396 return IRQ_HANDLED;
397}
398
399/*
400 * enable rx & tx operation on uart
401 */
402static void bcm_uart_enable(struct uart_port *port)
403{
404 unsigned int val;
405
406 val = bcm_uart_readl(port, UART_CTL_REG);
407 val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
408 bcm_uart_writel(port, val, UART_CTL_REG);
409}
410
411/*
412 * disable rx & tx operation on uart
413 */
414static void bcm_uart_disable(struct uart_port *port)
415{
416 unsigned int val;
417
418 val = bcm_uart_readl(port, UART_CTL_REG);
419 val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
420 UART_CTL_RXEN_MASK);
421 bcm_uart_writel(port, val, UART_CTL_REG);
422}
423
424/*
425 * clear all unread data in rx fifo and unsent data in tx fifo
426 */
427static void bcm_uart_flush(struct uart_port *port)
428{
429 unsigned int val;
430
431 /* empty rx and tx fifo */
432 val = bcm_uart_readl(port, UART_CTL_REG);
433 val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
434 bcm_uart_writel(port, val, UART_CTL_REG);
435
436 /* read any pending char to make sure all irq status are
437 * cleared */
438 (void)bcm_uart_readl(port, UART_FIFO_REG);
439}
440
441/*
442 * serial core request to initialize uart and start rx operation
443 */
444static int bcm_uart_startup(struct uart_port *port)
445{
446 unsigned int val;
447 int ret;
448
449 /* mask all irq and flush port */
450 bcm_uart_disable(port);
451 bcm_uart_writel(port, 0, UART_IR_REG);
452 bcm_uart_flush(port);
453
454 /* clear any pending external input interrupt */
455 (void)bcm_uart_readl(port, UART_EXTINP_REG);
456
457 /* set rx/tx fifo thresh to fifo half size */
458 val = bcm_uart_readl(port, UART_MCTL_REG);
459 val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
460 val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
461 val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
462 bcm_uart_writel(port, val, UART_MCTL_REG);
463
464 /* set rx fifo timeout to 1 char time */
465 val = bcm_uart_readl(port, UART_CTL_REG);
466 val &= ~UART_CTL_RXTMOUTCNT_MASK;
467 val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
468 bcm_uart_writel(port, val, UART_CTL_REG);
469
470 /* report any edge on dcd and cts */
471 val = UART_EXTINP_INT_MASK;
472 val |= UART_EXTINP_DCD_NOSENSE_MASK;
473 val |= UART_EXTINP_CTS_NOSENSE_MASK;
474 bcm_uart_writel(port, val, UART_EXTINP_REG);
475
476 /* register irq and enable rx interrupts */
477 ret = request_irq(port->irq, bcm_uart_interrupt, 0,
478 bcm_uart_type(port), port);
479 if (ret)
480 return ret;
481 bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
482 bcm_uart_enable(port);
483 return 0;
484}
485
486/*
487 * serial core request to flush & disable uart
488 */
489static void bcm_uart_shutdown(struct uart_port *port)
490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&port->lock, flags);
494 bcm_uart_writel(port, 0, UART_IR_REG);
495 spin_unlock_irqrestore(&port->lock, flags);
496
497 bcm_uart_disable(port);
498 bcm_uart_flush(port);
499 free_irq(port->irq, port);
500}
501
502/*
503 * serial core request to change current uart setting
504 */
505static void bcm_uart_set_termios(struct uart_port *port,
506 struct ktermios *new,
507 struct ktermios *old)
508{
509 unsigned int ctl, baud, quot, ier;
510 unsigned long flags;
511
512 spin_lock_irqsave(&port->lock, flags);
513
514 /* disable uart while changing speed */
515 bcm_uart_disable(port);
516 bcm_uart_flush(port);
517
518 /* update Control register */
519 ctl = bcm_uart_readl(port, UART_CTL_REG);
520 ctl &= ~UART_CTL_BITSPERSYM_MASK;
521
522 switch (new->c_cflag & CSIZE) {
523 case CS5:
524 ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
525 break;
526 case CS6:
527 ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
528 break;
529 case CS7:
530 ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
531 break;
532 default:
533 ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
534 break;
535 }
536
537 ctl &= ~UART_CTL_STOPBITS_MASK;
538 if (new->c_cflag & CSTOPB)
539 ctl |= UART_CTL_STOPBITS_2;
540 else
541 ctl |= UART_CTL_STOPBITS_1;
542
543 ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
544 if (new->c_cflag & PARENB)
545 ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
546 ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
547 if (new->c_cflag & PARODD)
548 ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
549 bcm_uart_writel(port, ctl, UART_CTL_REG);
550
551 /* update Baudword register */
552 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
553 quot = uart_get_divisor(port, baud) - 1;
554 bcm_uart_writel(port, quot, UART_BAUD_REG);
555
556 /* update Interrupt register */
557 ier = bcm_uart_readl(port, UART_IR_REG);
558
559 ier &= ~UART_IR_MASK(UART_IR_EXTIP);
560 if (UART_ENABLE_MS(port, new->c_cflag))
561 ier |= UART_IR_MASK(UART_IR_EXTIP);
562
563 bcm_uart_writel(port, ier, UART_IR_REG);
564
565 /* update read/ignore mask */
566 port->read_status_mask = UART_FIFO_VALID_MASK;
567 if (new->c_iflag & INPCK) {
568 port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
569 port->read_status_mask |= UART_FIFO_PARERR_MASK;
570 }
571 if (new->c_iflag & (BRKINT))
572 port->read_status_mask |= UART_FIFO_BRKDET_MASK;
573
574 port->ignore_status_mask = 0;
575 if (new->c_iflag & IGNPAR)
576 port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
577 if (new->c_iflag & IGNBRK)
578 port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
579 if (!(new->c_cflag & CREAD))
580 port->ignore_status_mask |= UART_FIFO_VALID_MASK;
581
582 uart_update_timeout(port, new->c_cflag, baud);
583 bcm_uart_enable(port);
584 spin_unlock_irqrestore(&port->lock, flags);
585}
586
587/*
588 * serial core request to claim uart iomem
589 */
590static int bcm_uart_request_port(struct uart_port *port)
591{
592 unsigned int size;
593
594 size = RSET_UART_SIZE;
595 if (!request_mem_region(port->mapbase, size, "bcm63xx")) {
596 dev_err(port->dev, "Memory region busy\n");
597 return -EBUSY;
598 }
599
600 port->membase = ioremap(port->mapbase, size);
601 if (!port->membase) {
602 dev_err(port->dev, "Unable to map registers\n");
603 release_mem_region(port->mapbase, size);
604 return -EBUSY;
605 }
606 return 0;
607}
608
609/*
610 * serial core request to release uart iomem
611 */
612static void bcm_uart_release_port(struct uart_port *port)
613{
614 release_mem_region(port->mapbase, RSET_UART_SIZE);
615 iounmap(port->membase);
616}
617
618/*
619 * serial core request to do any port required autoconfiguration
620 */
621static void bcm_uart_config_port(struct uart_port *port, int flags)
622{
623 if (flags & UART_CONFIG_TYPE) {
624 if (bcm_uart_request_port(port))
625 return;
626 port->type = PORT_BCM63XX;
627 }
628}
629
630/*
631 * serial core request to check that port information in serinfo are
632 * suitable
633 */
634static int bcm_uart_verify_port(struct uart_port *port,
635 struct serial_struct *serinfo)
636{
637 if (port->type != PORT_BCM63XX)
638 return -EINVAL;
639 if (port->irq != serinfo->irq)
640 return -EINVAL;
641 if (port->iotype != serinfo->io_type)
642 return -EINVAL;
643 if (port->mapbase != (unsigned long)serinfo->iomem_base)
644 return -EINVAL;
645 return 0;
646}
647
648/* serial core callbacks */
649static struct uart_ops bcm_uart_ops = {
650 .tx_empty = bcm_uart_tx_empty,
651 .get_mctrl = bcm_uart_get_mctrl,
652 .set_mctrl = bcm_uart_set_mctrl,
653 .start_tx = bcm_uart_start_tx,
654 .stop_tx = bcm_uart_stop_tx,
655 .stop_rx = bcm_uart_stop_rx,
656 .enable_ms = bcm_uart_enable_ms,
657 .break_ctl = bcm_uart_break_ctl,
658 .startup = bcm_uart_startup,
659 .shutdown = bcm_uart_shutdown,
660 .set_termios = bcm_uart_set_termios,
661 .type = bcm_uart_type,
662 .release_port = bcm_uart_release_port,
663 .request_port = bcm_uart_request_port,
664 .config_port = bcm_uart_config_port,
665 .verify_port = bcm_uart_verify_port,
666};
667
668
669
670#ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
671static inline void wait_for_xmitr(struct uart_port *port)
672{
673 unsigned int tmout;
674
675 /* Wait up to 10ms for the character(s) to be sent. */
676 tmout = 10000;
677 while (--tmout) {
678 unsigned int val;
679
680 val = bcm_uart_readl(port, UART_IR_REG);
681 if (val & UART_IR_STAT(UART_IR_TXEMPTY))
682 break;
683 udelay(1);
684 }
685
686 /* Wait up to 1s for flow control if necessary */
687 if (port->flags & UPF_CONS_FLOW) {
688 tmout = 1000000;
689 while (--tmout) {
690 unsigned int val;
691
692 val = bcm_uart_readl(port, UART_EXTINP_REG);
693 if (val & UART_EXTINP_CTS_MASK)
694 break;
695 udelay(1);
696 }
697 }
698}
699
700/*
701 * output given char
702 */
703static void bcm_console_putchar(struct uart_port *port, int ch)
704{
705 wait_for_xmitr(port);
706 bcm_uart_writel(port, ch, UART_FIFO_REG);
707}
708
709/*
710 * console core request to output given string
711 */
712static void bcm_console_write(struct console *co, const char *s,
713 unsigned int count)
714{
715 struct uart_port *port;
716 unsigned long flags;
717 int locked;
718
719 port = &ports[co->index];
720
721 local_irq_save(flags);
722 if (port->sysrq) {
723 /* bcm_uart_interrupt() already took the lock */
724 locked = 0;
725 } else if (oops_in_progress) {
726 locked = spin_trylock(&port->lock);
727 } else {
728 spin_lock(&port->lock);
729 locked = 1;
730 }
731
732 /* call helper to deal with \r\n */
733 uart_console_write(port, s, count, bcm_console_putchar);
734
735 /* and wait for char to be transmitted */
736 wait_for_xmitr(port);
737
738 if (locked)
739 spin_unlock(&port->lock);
740 local_irq_restore(flags);
741}
742
743/*
744 * console core request to setup given console, find matching uart
745 * port and setup it.
746 */
747static int bcm_console_setup(struct console *co, char *options)
748{
749 struct uart_port *port;
750 int baud = 9600;
751 int bits = 8;
752 int parity = 'n';
753 int flow = 'n';
754
755 if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
756 return -EINVAL;
757 port = &ports[co->index];
758 if (!port->membase)
759 return -ENODEV;
760 if (options)
761 uart_parse_options(options, &baud, &parity, &bits, &flow);
762
763 return uart_set_options(port, co, baud, parity, bits, flow);
764}
765
766static struct uart_driver bcm_uart_driver;
767
768static struct console bcm63xx_console = {
769 .name = "ttyS",
770 .write = bcm_console_write,
771 .device = uart_console_device,
772 .setup = bcm_console_setup,
773 .flags = CON_PRINTBUFFER,
774 .index = -1,
775 .data = &bcm_uart_driver,
776};
777
778static int __init bcm63xx_console_init(void)
779{
780 register_console(&bcm63xx_console);
781 return 0;
782}
783
784console_initcall(bcm63xx_console_init);
785
786#define BCM63XX_CONSOLE (&bcm63xx_console)
787#else
788#define BCM63XX_CONSOLE NULL
789#endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
790
791static struct uart_driver bcm_uart_driver = {
792 .owner = THIS_MODULE,
793 .driver_name = "bcm63xx_uart",
794 .dev_name = "ttyS",
795 .major = TTY_MAJOR,
796 .minor = 64,
797 .nr = BCM63XX_NR_UARTS,
798 .cons = BCM63XX_CONSOLE,
799};
800
801/*
802 * platform driver probe/remove callback
803 */
804static int __devinit bcm_uart_probe(struct platform_device *pdev)
805{
806 struct resource *res_mem, *res_irq;
807 struct uart_port *port;
808 struct clk *clk;
809 int ret;
810
811 if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
812 return -EINVAL;
813
814 if (ports[pdev->id].membase)
815 return -EBUSY;
816
817 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
818 if (!res_mem)
819 return -ENODEV;
820
821 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
822 if (!res_irq)
823 return -ENODEV;
824
825 clk = clk_get(&pdev->dev, "periph");
826 if (IS_ERR(clk))
827 return -ENODEV;
828
829 port = &ports[pdev->id];
830 memset(port, 0, sizeof(*port));
831 port->iotype = UPIO_MEM;
832 port->mapbase = res_mem->start;
833 port->irq = res_irq->start;
834 port->ops = &bcm_uart_ops;
835 port->flags = UPF_BOOT_AUTOCONF;
836 port->dev = &pdev->dev;
837 port->fifosize = 16;
838 port->uartclk = clk_get_rate(clk) / 2;
839 port->line = pdev->id;
840 clk_put(clk);
841
842 ret = uart_add_one_port(&bcm_uart_driver, port);
843 if (ret) {
844 ports[pdev->id].membase = 0;
845 return ret;
846 }
847 platform_set_drvdata(pdev, port);
848 return 0;
849}
850
851static int __devexit bcm_uart_remove(struct platform_device *pdev)
852{
853 struct uart_port *port;
854
855 port = platform_get_drvdata(pdev);
856 uart_remove_one_port(&bcm_uart_driver, port);
857 platform_set_drvdata(pdev, NULL);
858 /* mark port as free */
859 ports[pdev->id].membase = 0;
860 return 0;
861}
862
863/*
864 * platform driver stuff
865 */
866static struct platform_driver bcm_uart_platform_driver = {
867 .probe = bcm_uart_probe,
868 .remove = __devexit_p(bcm_uart_remove),
869 .driver = {
870 .owner = THIS_MODULE,
871 .name = "bcm63xx_uart",
872 },
873};
874
875static int __init bcm_uart_init(void)
876{
877 int ret;
878
879 ret = uart_register_driver(&bcm_uart_driver);
880 if (ret)
881 return ret;
882
883 ret = platform_driver_register(&bcm_uart_platform_driver);
884 if (ret)
885 uart_unregister_driver(&bcm_uart_driver);
886
887 return ret;
888}
889
890static void __exit bcm_uart_exit(void)
891{
892 platform_driver_unregister(&bcm_uart_platform_driver);
893 uart_unregister_driver(&bcm_uart_driver);
894}
895
896module_init(bcm_uart_init);
897module_exit(bcm_uart_exit);
898
899MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
900MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver");
901MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/bfin_5xx.c b/drivers/tty/serial/bfin_5xx.c
new file mode 100644
index 000000000000..9b1ff2b6bb37
--- /dev/null
+++ b/drivers/tty/serial/bfin_5xx.c
@@ -0,0 +1,1597 @@
1/*
2 * Blackfin On-Chip Serial Driver
3 *
4 * Copyright 2006-2010 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
15#define DRIVER_NAME "bfin-uart"
16#define pr_fmt(fmt) DRIVER_NAME ": " fmt
17
18#include <linux/module.h>
19#include <linux/ioport.h>
20#include <linux/gfp.h>
21#include <linux/io.h>
22#include <linux/init.h>
23#include <linux/console.h>
24#include <linux/sysrq.h>
25#include <linux/platform_device.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial_core.h>
29#include <linux/gpio.h>
30#include <linux/irq.h>
31#include <linux/kgdb.h>
32#include <linux/slab.h>
33#include <linux/dma-mapping.h>
34
35#include <asm/portmux.h>
36#include <asm/cacheflush.h>
37#include <asm/dma.h>
38
39#define port_membase(uart) (((struct bfin_serial_port *)(uart))->port.membase)
40#define get_lsr_cache(uart) (((struct bfin_serial_port *)(uart))->lsr)
41#define put_lsr_cache(uart, v) (((struct bfin_serial_port *)(uart))->lsr = (v))
42#include <asm/bfin_serial.h>
43
44#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
48#ifdef CONFIG_SERIAL_BFIN_MODULE
49# undef CONFIG_EARLY_PRINTK
50#endif
51
52/* UART name and device definitions */
53#define BFIN_SERIAL_DEV_NAME "ttyBF"
54#define BFIN_SERIAL_MAJOR 204
55#define BFIN_SERIAL_MINOR 64
56
57static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
58
59#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
60 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
61
62# ifndef CONFIG_SERIAL_BFIN_PIO
63# error KGDB only support UART in PIO mode.
64# endif
65
66static int kgdboc_port_line;
67static int kgdboc_break_enabled;
68#endif
69/*
70 * Setup for console. Argument comes from the menuconfig
71 */
72#define DMA_RX_XCOUNT 512
73#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
74
75#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
76
77#ifdef CONFIG_SERIAL_BFIN_DMA
78static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
79#else
80static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
81#endif
82
83static void bfin_serial_reset_irda(struct uart_port *port);
84
85#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
86 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
87static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
88{
89 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
90 if (uart->cts_pin < 0)
91 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
92
93 /* CTS PIN is negative assertive. */
94 if (UART_GET_CTS(uart))
95 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
96 else
97 return TIOCM_DSR | TIOCM_CAR;
98}
99
100static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
102 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
103 if (uart->rts_pin < 0)
104 return;
105
106 /* RTS PIN is negative assertive. */
107 if (mctrl & TIOCM_RTS)
108 UART_ENABLE_RTS(uart);
109 else
110 UART_DISABLE_RTS(uart);
111}
112
113/*
114 * Handle any change of modem status signal.
115 */
116static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
117{
118 struct bfin_serial_port *uart = dev_id;
119 unsigned int status;
120
121 status = bfin_serial_get_mctrl(&uart->port);
122 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
123#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
124 uart->scts = 1;
125 UART_CLEAR_SCTS(uart);
126 UART_CLEAR_IER(uart, EDSSI);
127#endif
128
129 return IRQ_HANDLED;
130}
131#else
132static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
133{
134 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
135}
136
137static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
138{
139}
140#endif
141
142/*
143 * interrupts are disabled on entry
144 */
145static void bfin_serial_stop_tx(struct uart_port *port)
146{
147 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
148#ifdef CONFIG_SERIAL_BFIN_DMA
149 struct circ_buf *xmit = &uart->port.state->xmit;
150#endif
151
152 while (!(UART_GET_LSR(uart) & TEMT))
153 cpu_relax();
154
155#ifdef CONFIG_SERIAL_BFIN_DMA
156 disable_dma(uart->tx_dma_channel);
157 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
158 uart->port.icount.tx += uart->tx_count;
159 uart->tx_count = 0;
160 uart->tx_done = 1;
161#else
162#ifdef CONFIG_BF54x
163 /* Clear TFI bit */
164 UART_PUT_LSR(uart, TFI);
165#endif
166 UART_CLEAR_IER(uart, ETBEI);
167#endif
168}
169
170/*
171 * port is locked and interrupts are disabled
172 */
173static void bfin_serial_start_tx(struct uart_port *port)
174{
175 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
176 struct tty_struct *tty = uart->port.state->port.tty;
177
178#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
179 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
180 uart->scts = 0;
181 uart_handle_cts_change(&uart->port, uart->scts);
182 }
183#endif
184
185 /*
186 * To avoid losting RX interrupt, we reset IR function
187 * before sending data.
188 */
189 if (tty->termios->c_line == N_IRDA)
190 bfin_serial_reset_irda(port);
191
192#ifdef CONFIG_SERIAL_BFIN_DMA
193 if (uart->tx_done)
194 bfin_serial_dma_tx_chars(uart);
195#else
196 UART_SET_IER(uart, ETBEI);
197 bfin_serial_tx_chars(uart);
198#endif
199}
200
201/*
202 * Interrupts are enabled
203 */
204static void bfin_serial_stop_rx(struct uart_port *port)
205{
206 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
207
208 UART_CLEAR_IER(uart, ERBFI);
209}
210
211/*
212 * Set the modem control timer to fire immediately.
213 */
214static void bfin_serial_enable_ms(struct uart_port *port)
215{
216}
217
218
219#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
220# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
221# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
222#else
223# define UART_GET_ANOMALY_THRESHOLD(uart) 0
224# define UART_SET_ANOMALY_THRESHOLD(uart, v)
225#endif
226
227#ifdef CONFIG_SERIAL_BFIN_PIO
228static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
229{
230 struct tty_struct *tty = NULL;
231 unsigned int status, ch, flg;
232 static struct timeval anomaly_start = { .tv_sec = 0 };
233
234 status = UART_GET_LSR(uart);
235 UART_CLEAR_LSR(uart);
236
237 ch = UART_GET_CHAR(uart);
238 uart->port.icount.rx++;
239
240#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
241 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
242 if (kgdb_connected && kgdboc_port_line == uart->port.line
243 && kgdboc_break_enabled)
244 if (ch == 0x3) {/* Ctrl + C */
245 kgdb_breakpoint();
246 return;
247 }
248
249 if (!uart->port.state || !uart->port.state->port.tty)
250 return;
251#endif
252 tty = uart->port.state->port.tty;
253
254 if (ANOMALY_05000363) {
255 /* The BF533 (and BF561) family of processors have a nice anomaly
256 * where they continuously generate characters for a "single" break.
257 * We have to basically ignore this flood until the "next" valid
258 * character comes across. Due to the nature of the flood, it is
259 * not possible to reliably catch bytes that are sent too quickly
260 * after this break. So application code talking to the Blackfin
261 * which sends a break signal must allow at least 1.5 character
262 * times after the end of the break for things to stabilize. This
263 * timeout was picked as it must absolutely be larger than 1
264 * character time +/- some percent. So 1.5 sounds good. All other
265 * Blackfin families operate properly. Woo.
266 */
267 if (anomaly_start.tv_sec) {
268 struct timeval curr;
269 suseconds_t usecs;
270
271 if ((~ch & (~ch + 1)) & 0xff)
272 goto known_good_char;
273
274 do_gettimeofday(&curr);
275 if (curr.tv_sec - anomaly_start.tv_sec > 1)
276 goto known_good_char;
277
278 usecs = 0;
279 if (curr.tv_sec != anomaly_start.tv_sec)
280 usecs += USEC_PER_SEC;
281 usecs += curr.tv_usec - anomaly_start.tv_usec;
282
283 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
284 goto known_good_char;
285
286 if (ch)
287 anomaly_start.tv_sec = 0;
288 else
289 anomaly_start = curr;
290
291 return;
292
293 known_good_char:
294 status &= ~BI;
295 anomaly_start.tv_sec = 0;
296 }
297 }
298
299 if (status & BI) {
300 if (ANOMALY_05000363)
301 if (bfin_revid() < 5)
302 do_gettimeofday(&anomaly_start);
303 uart->port.icount.brk++;
304 if (uart_handle_break(&uart->port))
305 goto ignore_char;
306 status &= ~(PE | FE);
307 }
308 if (status & PE)
309 uart->port.icount.parity++;
310 if (status & OE)
311 uart->port.icount.overrun++;
312 if (status & FE)
313 uart->port.icount.frame++;
314
315 status &= uart->port.read_status_mask;
316
317 if (status & BI)
318 flg = TTY_BREAK;
319 else if (status & PE)
320 flg = TTY_PARITY;
321 else if (status & FE)
322 flg = TTY_FRAME;
323 else
324 flg = TTY_NORMAL;
325
326 if (uart_handle_sysrq_char(&uart->port, ch))
327 goto ignore_char;
328
329 uart_insert_char(&uart->port, status, OE, ch, flg);
330
331 ignore_char:
332 tty_flip_buffer_push(tty);
333}
334
335static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
336{
337 struct circ_buf *xmit = &uart->port.state->xmit;
338
339 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
340#ifdef CONFIG_BF54x
341 /* Clear TFI bit */
342 UART_PUT_LSR(uart, TFI);
343#endif
344 /* Anomaly notes:
345 * 05000215 - we always clear ETBEI within last UART TX
346 * interrupt to end a string. It is always set
347 * when start a new tx.
348 */
349 UART_CLEAR_IER(uart, ETBEI);
350 return;
351 }
352
353 if (uart->port.x_char) {
354 UART_PUT_CHAR(uart, uart->port.x_char);
355 uart->port.icount.tx++;
356 uart->port.x_char = 0;
357 }
358
359 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
360 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
361 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
362 uart->port.icount.tx++;
363 }
364
365 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
366 uart_write_wakeup(&uart->port);
367}
368
369static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
370{
371 struct bfin_serial_port *uart = dev_id;
372
373 while (UART_GET_LSR(uart) & DR)
374 bfin_serial_rx_chars(uart);
375
376 return IRQ_HANDLED;
377}
378
379static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
380{
381 struct bfin_serial_port *uart = dev_id;
382
383#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
384 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
385 uart->scts = 0;
386 uart_handle_cts_change(&uart->port, uart->scts);
387 }
388#endif
389 spin_lock(&uart->port.lock);
390 if (UART_GET_LSR(uart) & THRE)
391 bfin_serial_tx_chars(uart);
392 spin_unlock(&uart->port.lock);
393
394 return IRQ_HANDLED;
395}
396#endif
397
398#ifdef CONFIG_SERIAL_BFIN_DMA
399static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
400{
401 struct circ_buf *xmit = &uart->port.state->xmit;
402
403 uart->tx_done = 0;
404
405 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
406 uart->tx_count = 0;
407 uart->tx_done = 1;
408 return;
409 }
410
411 if (uart->port.x_char) {
412 UART_PUT_CHAR(uart, uart->port.x_char);
413 uart->port.icount.tx++;
414 uart->port.x_char = 0;
415 }
416
417 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
418 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
419 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
420 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
421 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
422 set_dma_config(uart->tx_dma_channel,
423 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
424 INTR_ON_BUF,
425 DIMENSION_LINEAR,
426 DATA_SIZE_8,
427 DMA_SYNC_RESTART));
428 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
429 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
430 set_dma_x_modify(uart->tx_dma_channel, 1);
431 SSYNC();
432 enable_dma(uart->tx_dma_channel);
433
434 UART_SET_IER(uart, ETBEI);
435}
436
437static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
438{
439 struct tty_struct *tty = uart->port.state->port.tty;
440 int i, flg, status;
441
442 status = UART_GET_LSR(uart);
443 UART_CLEAR_LSR(uart);
444
445 uart->port.icount.rx +=
446 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
447 UART_XMIT_SIZE);
448
449 if (status & BI) {
450 uart->port.icount.brk++;
451 if (uart_handle_break(&uart->port))
452 goto dma_ignore_char;
453 status &= ~(PE | FE);
454 }
455 if (status & PE)
456 uart->port.icount.parity++;
457 if (status & OE)
458 uart->port.icount.overrun++;
459 if (status & FE)
460 uart->port.icount.frame++;
461
462 status &= uart->port.read_status_mask;
463
464 if (status & BI)
465 flg = TTY_BREAK;
466 else if (status & PE)
467 flg = TTY_PARITY;
468 else if (status & FE)
469 flg = TTY_FRAME;
470 else
471 flg = TTY_NORMAL;
472
473 for (i = uart->rx_dma_buf.tail; ; i++) {
474 if (i >= UART_XMIT_SIZE)
475 i = 0;
476 if (i == uart->rx_dma_buf.head)
477 break;
478 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
479 uart_insert_char(&uart->port, status, OE,
480 uart->rx_dma_buf.buf[i], flg);
481 }
482
483 dma_ignore_char:
484 tty_flip_buffer_push(tty);
485}
486
487void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
488{
489 int x_pos, pos;
490
491 dma_disable_irq_nosync(uart->rx_dma_channel);
492 spin_lock_bh(&uart->rx_lock);
493
494 /* 2D DMA RX buffer ring is used. Because curr_y_count and
495 * curr_x_count can't be read as an atomic operation,
496 * curr_y_count should be read before curr_x_count. When
497 * curr_x_count is read, curr_y_count may already indicate
498 * next buffer line. But, the position calculated here is
499 * still indicate the old line. The wrong position data may
500 * be smaller than current buffer tail, which cause garbages
501 * are received if it is not prohibit.
502 */
503 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
504 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
505 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
506 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
507 uart->rx_dma_nrows = 0;
508 x_pos = DMA_RX_XCOUNT - x_pos;
509 if (x_pos == DMA_RX_XCOUNT)
510 x_pos = 0;
511
512 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
513 /* Ignore receiving data if new position is in the same line of
514 * current buffer tail and small.
515 */
516 if (pos > uart->rx_dma_buf.tail ||
517 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
518 uart->rx_dma_buf.head = pos;
519 bfin_serial_dma_rx_chars(uart);
520 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
521 }
522
523 spin_unlock_bh(&uart->rx_lock);
524 dma_enable_irq(uart->rx_dma_channel);
525
526 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
527}
528
529static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
530{
531 struct bfin_serial_port *uart = dev_id;
532 struct circ_buf *xmit = &uart->port.state->xmit;
533
534#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
535 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
536 uart->scts = 0;
537 uart_handle_cts_change(&uart->port, uart->scts);
538 }
539#endif
540
541 spin_lock(&uart->port.lock);
542 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
543 disable_dma(uart->tx_dma_channel);
544 clear_dma_irqstat(uart->tx_dma_channel);
545 /* Anomaly notes:
546 * 05000215 - we always clear ETBEI within last UART TX
547 * interrupt to end a string. It is always set
548 * when start a new tx.
549 */
550 UART_CLEAR_IER(uart, ETBEI);
551 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
552 uart->port.icount.tx += uart->tx_count;
553
554 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
555 uart_write_wakeup(&uart->port);
556
557 bfin_serial_dma_tx_chars(uart);
558 }
559
560 spin_unlock(&uart->port.lock);
561 return IRQ_HANDLED;
562}
563
564static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
565{
566 struct bfin_serial_port *uart = dev_id;
567 unsigned short irqstat;
568 int x_pos, pos;
569
570 spin_lock(&uart->rx_lock);
571 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
572 clear_dma_irqstat(uart->rx_dma_channel);
573
574 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
575 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
576 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
577 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
578 uart->rx_dma_nrows = 0;
579
580 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
581 if (pos > uart->rx_dma_buf.tail ||
582 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
583 uart->rx_dma_buf.head = pos;
584 bfin_serial_dma_rx_chars(uart);
585 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
586 }
587
588 spin_unlock(&uart->rx_lock);
589
590 return IRQ_HANDLED;
591}
592#endif
593
594/*
595 * Return TIOCSER_TEMT when transmitter is not busy.
596 */
597static unsigned int bfin_serial_tx_empty(struct uart_port *port)
598{
599 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
600 unsigned short lsr;
601
602 lsr = UART_GET_LSR(uart);
603 if (lsr & TEMT)
604 return TIOCSER_TEMT;
605 else
606 return 0;
607}
608
609static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
610{
611 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
612 u16 lcr = UART_GET_LCR(uart);
613 if (break_state)
614 lcr |= SB;
615 else
616 lcr &= ~SB;
617 UART_PUT_LCR(uart, lcr);
618 SSYNC();
619}
620
621static int bfin_serial_startup(struct uart_port *port)
622{
623 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
624
625#ifdef CONFIG_SERIAL_BFIN_DMA
626 dma_addr_t dma_handle;
627
628 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
629 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
630 return -EBUSY;
631 }
632
633 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
634 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
635 free_dma(uart->rx_dma_channel);
636 return -EBUSY;
637 }
638
639 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
640 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
641
642 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
643 uart->rx_dma_buf.head = 0;
644 uart->rx_dma_buf.tail = 0;
645 uart->rx_dma_nrows = 0;
646
647 set_dma_config(uart->rx_dma_channel,
648 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
649 INTR_ON_ROW, DIMENSION_2D,
650 DATA_SIZE_8,
651 DMA_SYNC_RESTART));
652 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
653 set_dma_x_modify(uart->rx_dma_channel, 1);
654 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
655 set_dma_y_modify(uart->rx_dma_channel, 1);
656 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
657 enable_dma(uart->rx_dma_channel);
658
659 uart->rx_dma_timer.data = (unsigned long)(uart);
660 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
661 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
662 add_timer(&(uart->rx_dma_timer));
663#else
664# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
665 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
666 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
667 kgdboc_break_enabled = 0;
668 else {
669# endif
670 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
671 "BFIN_UART_RX", uart)) {
672 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
673 return -EBUSY;
674 }
675
676 if (request_irq
677 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
678 "BFIN_UART_TX", uart)) {
679 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
680 free_irq(uart->port.irq, uart);
681 return -EBUSY;
682 }
683
684# ifdef CONFIG_BF54x
685 {
686 /*
687 * UART2 and UART3 on BF548 share interrupt PINs and DMA
688 * controllers with SPORT2 and SPORT3. UART rx and tx
689 * interrupts are generated in PIO mode only when configure
690 * their peripheral mapping registers properly, which means
691 * request corresponding DMA channels in PIO mode as well.
692 */
693 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
694
695 switch (uart->port.irq) {
696 case IRQ_UART3_RX:
697 uart_dma_ch_rx = CH_UART3_RX;
698 uart_dma_ch_tx = CH_UART3_TX;
699 break;
700 case IRQ_UART2_RX:
701 uart_dma_ch_rx = CH_UART2_RX;
702 uart_dma_ch_tx = CH_UART2_TX;
703 break;
704 default:
705 uart_dma_ch_rx = uart_dma_ch_tx = 0;
706 break;
707 };
708
709 if (uart_dma_ch_rx &&
710 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
711 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
712 free_irq(uart->port.irq, uart);
713 free_irq(uart->port.irq + 1, uart);
714 return -EBUSY;
715 }
716 if (uart_dma_ch_tx &&
717 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
718 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
719 free_dma(uart_dma_ch_rx);
720 free_irq(uart->port.irq, uart);
721 free_irq(uart->port.irq + 1, uart);
722 return -EBUSY;
723 }
724 }
725# endif
726# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
727 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
728 }
729# endif
730#endif
731
732#ifdef CONFIG_SERIAL_BFIN_CTSRTS
733 if (uart->cts_pin >= 0) {
734 if (request_irq(gpio_to_irq(uart->cts_pin),
735 bfin_serial_mctrl_cts_int,
736 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
737 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
738 uart->cts_pin = -1;
739 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
740 }
741 }
742 if (uart->rts_pin >= 0) {
743 gpio_direction_output(uart->rts_pin, 0);
744 }
745#endif
746#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
747 if (uart->cts_pin >= 0 && request_irq(uart->status_irq,
748 bfin_serial_mctrl_cts_int,
749 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
750 uart->cts_pin = -1;
751 pr_info("Unable to attach BlackFin UART Modem Status interrupt.\n");
752 }
753
754 /* CTS RTS PINs are negative assertive. */
755 UART_PUT_MCR(uart, ACTS);
756 UART_SET_IER(uart, EDSSI);
757#endif
758
759 UART_SET_IER(uart, ERBFI);
760 return 0;
761}
762
763static void bfin_serial_shutdown(struct uart_port *port)
764{
765 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
766
767#ifdef CONFIG_SERIAL_BFIN_DMA
768 disable_dma(uart->tx_dma_channel);
769 free_dma(uart->tx_dma_channel);
770 disable_dma(uart->rx_dma_channel);
771 free_dma(uart->rx_dma_channel);
772 del_timer(&(uart->rx_dma_timer));
773 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
774#else
775#ifdef CONFIG_BF54x
776 switch (uart->port.irq) {
777 case IRQ_UART3_RX:
778 free_dma(CH_UART3_RX);
779 free_dma(CH_UART3_TX);
780 break;
781 case IRQ_UART2_RX:
782 free_dma(CH_UART2_RX);
783 free_dma(CH_UART2_TX);
784 break;
785 default:
786 break;
787 };
788#endif
789 free_irq(uart->port.irq, uart);
790 free_irq(uart->port.irq+1, uart);
791#endif
792
793#ifdef CONFIG_SERIAL_BFIN_CTSRTS
794 if (uart->cts_pin >= 0)
795 free_irq(gpio_to_irq(uart->cts_pin), uart);
796#endif
797#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
798 if (uart->cts_pin >= 0)
799 free_irq(uart->status_irq, uart);
800#endif
801}
802
803static void
804bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
805 struct ktermios *old)
806{
807 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
808 unsigned long flags;
809 unsigned int baud, quot;
810 unsigned short val, ier, lcr = 0;
811
812 switch (termios->c_cflag & CSIZE) {
813 case CS8:
814 lcr = WLS(8);
815 break;
816 case CS7:
817 lcr = WLS(7);
818 break;
819 case CS6:
820 lcr = WLS(6);
821 break;
822 case CS5:
823 lcr = WLS(5);
824 break;
825 default:
826 printk(KERN_ERR "%s: word lengh not supported\n",
827 __func__);
828 }
829
830 /* Anomaly notes:
831 * 05000231 - STOP bit is always set to 1 whatever the user is set.
832 */
833 if (termios->c_cflag & CSTOPB) {
834 if (ANOMALY_05000231)
835 printk(KERN_WARNING "STOP bits other than 1 is not "
836 "supported in case of anomaly 05000231.\n");
837 else
838 lcr |= STB;
839 }
840 if (termios->c_cflag & PARENB)
841 lcr |= PEN;
842 if (!(termios->c_cflag & PARODD))
843 lcr |= EPS;
844 if (termios->c_cflag & CMSPAR)
845 lcr |= STP;
846
847 spin_lock_irqsave(&uart->port.lock, flags);
848
849 port->read_status_mask = OE;
850 if (termios->c_iflag & INPCK)
851 port->read_status_mask |= (FE | PE);
852 if (termios->c_iflag & (BRKINT | PARMRK))
853 port->read_status_mask |= BI;
854
855 /*
856 * Characters to ignore
857 */
858 port->ignore_status_mask = 0;
859 if (termios->c_iflag & IGNPAR)
860 port->ignore_status_mask |= FE | PE;
861 if (termios->c_iflag & IGNBRK) {
862 port->ignore_status_mask |= BI;
863 /*
864 * If we're ignoring parity and break indicators,
865 * ignore overruns too (for real raw support).
866 */
867 if (termios->c_iflag & IGNPAR)
868 port->ignore_status_mask |= OE;
869 }
870
871 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
872 quot = uart_get_divisor(port, baud);
873
874 /* If discipline is not IRDA, apply ANOMALY_05000230 */
875 if (termios->c_line != N_IRDA)
876 quot -= ANOMALY_05000230;
877
878 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
879
880 /* Disable UART */
881 ier = UART_GET_IER(uart);
882 UART_DISABLE_INTS(uart);
883
884 /* Set DLAB in LCR to Access DLL and DLH */
885 UART_SET_DLAB(uart);
886
887 UART_PUT_DLL(uart, quot & 0xFF);
888 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
889 SSYNC();
890
891 /* Clear DLAB in LCR to Access THR RBR IER */
892 UART_CLEAR_DLAB(uart);
893
894 UART_PUT_LCR(uart, lcr);
895
896 /* Enable UART */
897 UART_ENABLE_INTS(uart, ier);
898
899 val = UART_GET_GCTL(uart);
900 val |= UCEN;
901 UART_PUT_GCTL(uart, val);
902
903 /* Port speed changed, update the per-port timeout. */
904 uart_update_timeout(port, termios->c_cflag, baud);
905
906 spin_unlock_irqrestore(&uart->port.lock, flags);
907}
908
909static const char *bfin_serial_type(struct uart_port *port)
910{
911 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
912
913 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
914}
915
916/*
917 * Release the memory region(s) being used by 'port'.
918 */
919static void bfin_serial_release_port(struct uart_port *port)
920{
921}
922
923/*
924 * Request the memory region(s) being used by 'port'.
925 */
926static int bfin_serial_request_port(struct uart_port *port)
927{
928 return 0;
929}
930
931/*
932 * Configure/autoconfigure the port.
933 */
934static void bfin_serial_config_port(struct uart_port *port, int flags)
935{
936 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
937
938 if (flags & UART_CONFIG_TYPE &&
939 bfin_serial_request_port(&uart->port) == 0)
940 uart->port.type = PORT_BFIN;
941}
942
943/*
944 * Verify the new serial_struct (for TIOCSSERIAL).
945 * The only change we allow are to the flags and type, and
946 * even then only between PORT_BFIN and PORT_UNKNOWN
947 */
948static int
949bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
950{
951 return 0;
952}
953
954/*
955 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
956 * In other cases, disable IrDA function.
957 */
958static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
959{
960 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
961 unsigned short val;
962
963 switch (ld) {
964 case N_IRDA:
965 val = UART_GET_GCTL(uart);
966 val |= (IREN | RPOLC);
967 UART_PUT_GCTL(uart, val);
968 break;
969 default:
970 val = UART_GET_GCTL(uart);
971 val &= ~(IREN | RPOLC);
972 UART_PUT_GCTL(uart, val);
973 }
974}
975
976static void bfin_serial_reset_irda(struct uart_port *port)
977{
978 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
979 unsigned short val;
980
981 val = UART_GET_GCTL(uart);
982 val &= ~(IREN | RPOLC);
983 UART_PUT_GCTL(uart, val);
984 SSYNC();
985 val |= (IREN | RPOLC);
986 UART_PUT_GCTL(uart, val);
987 SSYNC();
988}
989
990#ifdef CONFIG_CONSOLE_POLL
991/* Anomaly notes:
992 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
993 * losing other bits of UART_LSR is not a problem here.
994 */
995static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
996{
997 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
998
999 while (!(UART_GET_LSR(uart) & THRE))
1000 cpu_relax();
1001
1002 UART_CLEAR_DLAB(uart);
1003 UART_PUT_CHAR(uart, (unsigned char)chr);
1004}
1005
1006static int bfin_serial_poll_get_char(struct uart_port *port)
1007{
1008 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1009 unsigned char chr;
1010
1011 while (!(UART_GET_LSR(uart) & DR))
1012 cpu_relax();
1013
1014 UART_CLEAR_DLAB(uart);
1015 chr = UART_GET_CHAR(uart);
1016
1017 return chr;
1018}
1019#endif
1020
1021#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1022 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1023static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1024{
1025 if (kgdboc_break_enabled) {
1026 kgdboc_break_enabled = 0;
1027 bfin_serial_shutdown(port);
1028 }
1029}
1030
1031static int bfin_kgdboc_port_startup(struct uart_port *port)
1032{
1033 kgdboc_port_line = port->line;
1034 kgdboc_break_enabled = !bfin_serial_startup(port);
1035 return 0;
1036}
1037#endif
1038
1039static struct uart_ops bfin_serial_pops = {
1040 .tx_empty = bfin_serial_tx_empty,
1041 .set_mctrl = bfin_serial_set_mctrl,
1042 .get_mctrl = bfin_serial_get_mctrl,
1043 .stop_tx = bfin_serial_stop_tx,
1044 .start_tx = bfin_serial_start_tx,
1045 .stop_rx = bfin_serial_stop_rx,
1046 .enable_ms = bfin_serial_enable_ms,
1047 .break_ctl = bfin_serial_break_ctl,
1048 .startup = bfin_serial_startup,
1049 .shutdown = bfin_serial_shutdown,
1050 .set_termios = bfin_serial_set_termios,
1051 .set_ldisc = bfin_serial_set_ldisc,
1052 .type = bfin_serial_type,
1053 .release_port = bfin_serial_release_port,
1054 .request_port = bfin_serial_request_port,
1055 .config_port = bfin_serial_config_port,
1056 .verify_port = bfin_serial_verify_port,
1057#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1058 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1059 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1060 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1061#endif
1062#ifdef CONFIG_CONSOLE_POLL
1063 .poll_put_char = bfin_serial_poll_put_char,
1064 .poll_get_char = bfin_serial_poll_get_char,
1065#endif
1066};
1067
1068#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1069/*
1070 * If the port was already initialised (eg, by a boot loader),
1071 * try to determine the current setup.
1072 */
1073static void __init
1074bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1075 int *parity, int *bits)
1076{
1077 unsigned short status;
1078
1079 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1080 if (status == (ERBFI | ETBEI)) {
1081 /* ok, the port was enabled */
1082 u16 lcr, dlh, dll;
1083
1084 lcr = UART_GET_LCR(uart);
1085
1086 *parity = 'n';
1087 if (lcr & PEN) {
1088 if (lcr & EPS)
1089 *parity = 'e';
1090 else
1091 *parity = 'o';
1092 }
1093 switch (lcr & 0x03) {
1094 case 0: *bits = 5; break;
1095 case 1: *bits = 6; break;
1096 case 2: *bits = 7; break;
1097 case 3: *bits = 8; break;
1098 }
1099 /* Set DLAB in LCR to Access DLL and DLH */
1100 UART_SET_DLAB(uart);
1101
1102 dll = UART_GET_DLL(uart);
1103 dlh = UART_GET_DLH(uart);
1104
1105 /* Clear DLAB in LCR to Access THR RBR IER */
1106 UART_CLEAR_DLAB(uart);
1107
1108 *baud = get_sclk() / (16*(dll | dlh << 8));
1109 }
1110 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1111}
1112
1113static struct uart_driver bfin_serial_reg;
1114
1115static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1116{
1117 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1118 while (!(UART_GET_LSR(uart) & THRE))
1119 barrier();
1120 UART_PUT_CHAR(uart, ch);
1121}
1122
1123#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1124 defined (CONFIG_EARLY_PRINTK) */
1125
1126#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1127#define CLASS_BFIN_CONSOLE "bfin-console"
1128/*
1129 * Interrupts are disabled on entering
1130 */
1131static void
1132bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1133{
1134 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1135 unsigned long flags;
1136
1137 spin_lock_irqsave(&uart->port.lock, flags);
1138 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1139 spin_unlock_irqrestore(&uart->port.lock, flags);
1140
1141}
1142
1143static int __init
1144bfin_serial_console_setup(struct console *co, char *options)
1145{
1146 struct bfin_serial_port *uart;
1147 int baud = 57600;
1148 int bits = 8;
1149 int parity = 'n';
1150# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1151 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1152 int flow = 'r';
1153# else
1154 int flow = 'n';
1155# endif
1156
1157 /*
1158 * Check whether an invalid uart number has been specified, and
1159 * if so, search for the first available port that does have
1160 * console support.
1161 */
1162 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1163 return -ENODEV;
1164
1165 uart = bfin_serial_ports[co->index];
1166 if (!uart)
1167 return -ENODEV;
1168
1169 if (options)
1170 uart_parse_options(options, &baud, &parity, &bits, &flow);
1171 else
1172 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1173
1174 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1175}
1176
1177static struct console bfin_serial_console = {
1178 .name = BFIN_SERIAL_DEV_NAME,
1179 .write = bfin_serial_console_write,
1180 .device = uart_console_device,
1181 .setup = bfin_serial_console_setup,
1182 .flags = CON_PRINTBUFFER,
1183 .index = -1,
1184 .data = &bfin_serial_reg,
1185};
1186#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1187#else
1188#define BFIN_SERIAL_CONSOLE NULL
1189#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1190
1191#ifdef CONFIG_EARLY_PRINTK
1192static struct bfin_serial_port bfin_earlyprintk_port;
1193#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
1194
1195/*
1196 * Interrupts are disabled on entering
1197 */
1198static void
1199bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
1200{
1201 unsigned long flags;
1202
1203 if (bfin_earlyprintk_port.port.line != co->index)
1204 return;
1205
1206 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1207 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1208 bfin_serial_console_putchar);
1209 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1210}
1211
1212/*
1213 * This should have a .setup or .early_setup in it, but then things get called
1214 * without the command line options, and the baud rate gets messed up - so
1215 * don't let the common infrastructure play with things. (see calls to setup
1216 * & earlysetup in ./kernel/printk.c:register_console()
1217 */
1218static struct __initdata console bfin_early_serial_console = {
1219 .name = "early_BFuart",
1220 .write = bfin_earlyprintk_console_write,
1221 .device = uart_console_device,
1222 .flags = CON_PRINTBUFFER,
1223 .index = -1,
1224 .data = &bfin_serial_reg,
1225};
1226#endif
1227
1228static struct uart_driver bfin_serial_reg = {
1229 .owner = THIS_MODULE,
1230 .driver_name = DRIVER_NAME,
1231 .dev_name = BFIN_SERIAL_DEV_NAME,
1232 .major = BFIN_SERIAL_MAJOR,
1233 .minor = BFIN_SERIAL_MINOR,
1234 .nr = BFIN_UART_NR_PORTS,
1235 .cons = BFIN_SERIAL_CONSOLE,
1236};
1237
1238static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1239{
1240 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1241
1242 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1243}
1244
1245static int bfin_serial_resume(struct platform_device *pdev)
1246{
1247 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1248
1249 return uart_resume_port(&bfin_serial_reg, &uart->port);
1250}
1251
1252static int bfin_serial_probe(struct platform_device *pdev)
1253{
1254 struct resource *res;
1255 struct bfin_serial_port *uart = NULL;
1256 int ret = 0;
1257
1258 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1259 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1260 return -ENOENT;
1261 }
1262
1263 if (bfin_serial_ports[pdev->id] == NULL) {
1264
1265 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1266 if (!uart) {
1267 dev_err(&pdev->dev,
1268 "fail to malloc bfin_serial_port\n");
1269 return -ENOMEM;
1270 }
1271 bfin_serial_ports[pdev->id] = uart;
1272
1273#ifdef CONFIG_EARLY_PRINTK
1274 if (!(bfin_earlyprintk_port.port.membase
1275 && bfin_earlyprintk_port.port.line == pdev->id)) {
1276 /*
1277 * If the peripheral PINs of current port is allocated
1278 * in earlyprintk probe stage, don't do it again.
1279 */
1280#endif
1281 ret = peripheral_request_list(
1282 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1283 if (ret) {
1284 dev_err(&pdev->dev,
1285 "fail to request bfin serial peripherals\n");
1286 goto out_error_free_mem;
1287 }
1288#ifdef CONFIG_EARLY_PRINTK
1289 }
1290#endif
1291
1292 spin_lock_init(&uart->port.lock);
1293 uart->port.uartclk = get_sclk();
1294 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1295 uart->port.ops = &bfin_serial_pops;
1296 uart->port.line = pdev->id;
1297 uart->port.iotype = UPIO_MEM;
1298 uart->port.flags = UPF_BOOT_AUTOCONF;
1299
1300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301 if (res == NULL) {
1302 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1303 ret = -ENOENT;
1304 goto out_error_free_peripherals;
1305 }
1306
1307 uart->port.membase = ioremap(res->start,
1308 res->end - res->start);
1309 if (!uart->port.membase) {
1310 dev_err(&pdev->dev, "Cannot map uart IO\n");
1311 ret = -ENXIO;
1312 goto out_error_free_peripherals;
1313 }
1314 uart->port.mapbase = res->start;
1315
1316 uart->port.irq = platform_get_irq(pdev, 0);
1317 if (uart->port.irq < 0) {
1318 dev_err(&pdev->dev, "No uart RX/TX IRQ specified\n");
1319 ret = -ENOENT;
1320 goto out_error_unmap;
1321 }
1322
1323 uart->status_irq = platform_get_irq(pdev, 1);
1324 if (uart->status_irq < 0) {
1325 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1326 ret = -ENOENT;
1327 goto out_error_unmap;
1328 }
1329
1330#ifdef CONFIG_SERIAL_BFIN_DMA
1331 spin_lock_init(&uart->rx_lock);
1332 uart->tx_done = 1;
1333 uart->tx_count = 0;
1334
1335 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1336 if (res == NULL) {
1337 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1338 ret = -ENOENT;
1339 goto out_error_unmap;
1340 }
1341 uart->tx_dma_channel = res->start;
1342
1343 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1344 if (res == NULL) {
1345 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1346 ret = -ENOENT;
1347 goto out_error_unmap;
1348 }
1349 uart->rx_dma_channel = res->start;
1350
1351 init_timer(&(uart->rx_dma_timer));
1352#endif
1353
1354#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1355 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1356 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1357 if (res == NULL)
1358 uart->cts_pin = -1;
1359 else
1360 uart->cts_pin = res->start;
1361
1362 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1363 if (res == NULL)
1364 uart->rts_pin = -1;
1365 else
1366 uart->rts_pin = res->start;
1367# if defined(CONFIG_SERIAL_BFIN_CTSRTS)
1368 if (uart->rts_pin >= 0)
1369 gpio_request(uart->rts_pin, DRIVER_NAME);
1370# endif
1371#endif
1372 }
1373
1374#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1375 if (!is_early_platform_device(pdev)) {
1376#endif
1377 uart = bfin_serial_ports[pdev->id];
1378 uart->port.dev = &pdev->dev;
1379 dev_set_drvdata(&pdev->dev, uart);
1380 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1381#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1382 }
1383#endif
1384
1385 if (!ret)
1386 return 0;
1387
1388 if (uart) {
1389out_error_unmap:
1390 iounmap(uart->port.membase);
1391out_error_free_peripherals:
1392 peripheral_free_list(
1393 (unsigned short *)pdev->dev.platform_data);
1394out_error_free_mem:
1395 kfree(uart);
1396 bfin_serial_ports[pdev->id] = NULL;
1397 }
1398
1399 return ret;
1400}
1401
1402static int __devexit bfin_serial_remove(struct platform_device *pdev)
1403{
1404 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1405
1406 dev_set_drvdata(&pdev->dev, NULL);
1407
1408 if (uart) {
1409 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1410#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1411 if (uart->rts_pin >= 0)
1412 gpio_free(uart->rts_pin);
1413#endif
1414 iounmap(uart->port.membase);
1415 peripheral_free_list(
1416 (unsigned short *)pdev->dev.platform_data);
1417 kfree(uart);
1418 bfin_serial_ports[pdev->id] = NULL;
1419 }
1420
1421 return 0;
1422}
1423
1424static struct platform_driver bfin_serial_driver = {
1425 .probe = bfin_serial_probe,
1426 .remove = __devexit_p(bfin_serial_remove),
1427 .suspend = bfin_serial_suspend,
1428 .resume = bfin_serial_resume,
1429 .driver = {
1430 .name = DRIVER_NAME,
1431 .owner = THIS_MODULE,
1432 },
1433};
1434
1435#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1436static __initdata struct early_platform_driver early_bfin_serial_driver = {
1437 .class_str = CLASS_BFIN_CONSOLE,
1438 .pdrv = &bfin_serial_driver,
1439 .requested_id = EARLY_PLATFORM_ID_UNSET,
1440};
1441
1442static int __init bfin_serial_rs_console_init(void)
1443{
1444 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1445
1446 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1447
1448 register_console(&bfin_serial_console);
1449
1450 return 0;
1451}
1452console_initcall(bfin_serial_rs_console_init);
1453#endif
1454
1455#ifdef CONFIG_EARLY_PRINTK
1456/*
1457 * Memory can't be allocated dynamically during earlyprink init stage.
1458 * So, do individual probe for earlyprink with a static uart port variable.
1459 */
1460static int bfin_earlyprintk_probe(struct platform_device *pdev)
1461{
1462 struct resource *res;
1463 int ret;
1464
1465 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1466 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1467 return -ENOENT;
1468 }
1469
1470 ret = peripheral_request_list(
1471 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1472 if (ret) {
1473 dev_err(&pdev->dev,
1474 "fail to request bfin serial peripherals\n");
1475 return ret;
1476 }
1477
1478 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1479 if (res == NULL) {
1480 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1481 ret = -ENOENT;
1482 goto out_error_free_peripherals;
1483 }
1484
1485 bfin_earlyprintk_port.port.membase = ioremap(res->start,
1486 res->end - res->start);
1487 if (!bfin_earlyprintk_port.port.membase) {
1488 dev_err(&pdev->dev, "Cannot map uart IO\n");
1489 ret = -ENXIO;
1490 goto out_error_free_peripherals;
1491 }
1492 bfin_earlyprintk_port.port.mapbase = res->start;
1493 bfin_earlyprintk_port.port.line = pdev->id;
1494 bfin_earlyprintk_port.port.uartclk = get_sclk();
1495 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1496 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1497
1498 return 0;
1499
1500out_error_free_peripherals:
1501 peripheral_free_list(
1502 (unsigned short *)pdev->dev.platform_data);
1503
1504 return ret;
1505}
1506
1507static struct platform_driver bfin_earlyprintk_driver = {
1508 .probe = bfin_earlyprintk_probe,
1509 .driver = {
1510 .name = DRIVER_NAME,
1511 .owner = THIS_MODULE,
1512 },
1513};
1514
1515static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1516 .class_str = CLASS_BFIN_EARLYPRINTK,
1517 .pdrv = &bfin_earlyprintk_driver,
1518 .requested_id = EARLY_PLATFORM_ID_UNSET,
1519};
1520
1521struct console __init *bfin_earlyserial_init(unsigned int port,
1522 unsigned int cflag)
1523{
1524 struct ktermios t;
1525 char port_name[20];
1526
1527 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1528 return NULL;
1529
1530 /*
1531 * Only probe resource of the given port in earlyprintk boot arg.
1532 * The expected port id should be indicated in port name string.
1533 */
1534 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1535 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1536 port_name);
1537 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1538
1539 if (!bfin_earlyprintk_port.port.membase)
1540 return NULL;
1541
1542#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1543 /*
1544 * If we are using early serial, don't let the normal console rewind
1545 * log buffer, since that causes things to be printed multiple times
1546 */
1547 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1548#endif
1549
1550 bfin_early_serial_console.index = port;
1551 t.c_cflag = cflag;
1552 t.c_iflag = 0;
1553 t.c_oflag = 0;
1554 t.c_lflag = ICANON;
1555 t.c_line = port;
1556 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1557
1558 return &bfin_early_serial_console;
1559}
1560#endif /* CONFIG_EARLY_PRINTK */
1561
1562static int __init bfin_serial_init(void)
1563{
1564 int ret;
1565
1566 pr_info("Blackfin serial driver\n");
1567
1568 ret = uart_register_driver(&bfin_serial_reg);
1569 if (ret) {
1570 pr_err("failed to register %s:%d\n",
1571 bfin_serial_reg.driver_name, ret);
1572 }
1573
1574 ret = platform_driver_register(&bfin_serial_driver);
1575 if (ret) {
1576 pr_err("fail to register bfin uart\n");
1577 uart_unregister_driver(&bfin_serial_reg);
1578 }
1579
1580 return ret;
1581}
1582
1583static void __exit bfin_serial_exit(void)
1584{
1585 platform_driver_unregister(&bfin_serial_driver);
1586 uart_unregister_driver(&bfin_serial_reg);
1587}
1588
1589
1590module_init(bfin_serial_init);
1591module_exit(bfin_serial_exit);
1592
1593MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
1594MODULE_DESCRIPTION("Blackfin generic serial port driver");
1595MODULE_LICENSE("GPL");
1596MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1597MODULE_ALIAS("platform:bfin-uart");
diff --git a/drivers/tty/serial/bfin_sport_uart.c b/drivers/tty/serial/bfin_sport_uart.c
new file mode 100644
index 000000000000..891d194ae754
--- /dev/null
+++ b/drivers/tty/serial/bfin_sport_uart.c
@@ -0,0 +1,934 @@
1/*
2 * Blackfin On-Chip Sport Emulated UART Driver
3 *
4 * Copyright 2006-2009 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/*
12 * This driver and the hardware supported are in term of EE-191 of ADI.
13 * http://www.analog.com/static/imported-files/application_notes/EE191.pdf
14 * This application note describe how to implement a UART on a Sharc DSP,
15 * but this driver is implemented on Blackfin Processor.
16 * Transmit Frame Sync is not used by this driver to transfer data out.
17 */
18
19/* #define DEBUG */
20
21#define DRV_NAME "bfin-sport-uart"
22#define DEVICE_NAME "ttySS"
23#define pr_fmt(fmt) DRV_NAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/sysrq.h>
31#include <linux/slab.h>
32#include <linux/platform_device.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36
37#include <asm/bfin_sport.h>
38#include <asm/delay.h>
39#include <asm/portmux.h>
40
41#include "bfin_sport_uart.h"
42
43struct sport_uart_port {
44 struct uart_port port;
45 int err_irq;
46 unsigned short csize;
47 unsigned short rxmask;
48 unsigned short txmask1;
49 unsigned short txmask2;
50 unsigned char stopb;
51/* unsigned char parib; */
52#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
53 int cts_pin;
54 int rts_pin;
55#endif
56};
57
58static int sport_uart_tx_chars(struct sport_uart_port *up);
59static void sport_stop_tx(struct uart_port *port);
60
61static inline void tx_one_byte(struct sport_uart_port *up, unsigned int value)
62{
63 pr_debug("%s value:%x, mask1=0x%x, mask2=0x%x\n", __func__, value,
64 up->txmask1, up->txmask2);
65
66 /* Place Start and Stop bits */
67 __asm__ __volatile__ (
68 "%[val] <<= 1;"
69 "%[val] = %[val] & %[mask1];"
70 "%[val] = %[val] | %[mask2];"
71 : [val]"+d"(value)
72 : [mask1]"d"(up->txmask1), [mask2]"d"(up->txmask2)
73 : "ASTAT"
74 );
75 pr_debug("%s value:%x\n", __func__, value);
76
77 SPORT_PUT_TX(up, value);
78}
79
80static inline unsigned char rx_one_byte(struct sport_uart_port *up)
81{
82 unsigned int value;
83 unsigned char extract;
84 u32 tmp_mask1, tmp_mask2, tmp_shift, tmp;
85
86 if ((up->csize + up->stopb) > 7)
87 value = SPORT_GET_RX32(up);
88 else
89 value = SPORT_GET_RX(up);
90
91 pr_debug("%s value:%x, cs=%d, mask=0x%x\n", __func__, value,
92 up->csize, up->rxmask);
93
94 /* Extract data */
95 __asm__ __volatile__ (
96 "%[extr] = 0;"
97 "%[mask1] = %[rxmask];"
98 "%[mask2] = 0x0200(Z);"
99 "%[shift] = 0;"
100 "LSETUP(.Lloop_s, .Lloop_e) LC0 = %[lc];"
101 ".Lloop_s:"
102 "%[tmp] = extract(%[val], %[mask1].L)(Z);"
103 "%[tmp] <<= %[shift];"
104 "%[extr] = %[extr] | %[tmp];"
105 "%[mask1] = %[mask1] - %[mask2];"
106 ".Lloop_e:"
107 "%[shift] += 1;"
108 : [extr]"=&d"(extract), [shift]"=&d"(tmp_shift), [tmp]"=&d"(tmp),
109 [mask1]"=&d"(tmp_mask1), [mask2]"=&d"(tmp_mask2)
110 : [val]"d"(value), [rxmask]"d"(up->rxmask), [lc]"a"(up->csize)
111 : "ASTAT", "LB0", "LC0", "LT0"
112 );
113
114 pr_debug(" extract:%x\n", extract);
115 return extract;
116}
117
118static int sport_uart_setup(struct sport_uart_port *up, int size, int baud_rate)
119{
120 int tclkdiv, rclkdiv;
121 unsigned int sclk = get_sclk();
122
123 /* Set TCR1 and TCR2, TFSR is not enabled for uart */
124 SPORT_PUT_TCR1(up, (LATFS | ITFS | TFSR | TLSBIT | ITCLK));
125 SPORT_PUT_TCR2(up, size + 1);
126 pr_debug("%s TCR1:%x, TCR2:%x\n", __func__, SPORT_GET_TCR1(up), SPORT_GET_TCR2(up));
127
128 /* Set RCR1 and RCR2 */
129 SPORT_PUT_RCR1(up, (RCKFE | LARFS | LRFS | RFSR | IRCLK));
130 SPORT_PUT_RCR2(up, (size + 1) * 2 - 1);
131 pr_debug("%s RCR1:%x, RCR2:%x\n", __func__, SPORT_GET_RCR1(up), SPORT_GET_RCR2(up));
132
133 tclkdiv = sclk / (2 * baud_rate) - 1;
134 /* The actual uart baud rate of devices vary between +/-2%. The sport
135 * RX sample rate should be faster than the double of the worst case,
136 * otherwise, wrong data are received. So, set sport RX clock to be
137 * 3% faster.
138 */
139 rclkdiv = sclk / (2 * baud_rate * 2 * 97 / 100) - 1;
140 SPORT_PUT_TCLKDIV(up, tclkdiv);
141 SPORT_PUT_RCLKDIV(up, rclkdiv);
142 SSYNC();
143 pr_debug("%s sclk:%d, baud_rate:%d, tclkdiv:%d, rclkdiv:%d\n",
144 __func__, sclk, baud_rate, tclkdiv, rclkdiv);
145
146 return 0;
147}
148
149static irqreturn_t sport_uart_rx_irq(int irq, void *dev_id)
150{
151 struct sport_uart_port *up = dev_id;
152 struct tty_struct *tty = up->port.state->port.tty;
153 unsigned int ch;
154
155 spin_lock(&up->port.lock);
156
157 while (SPORT_GET_STAT(up) & RXNE) {
158 ch = rx_one_byte(up);
159 up->port.icount.rx++;
160
161 if (!uart_handle_sysrq_char(&up->port, ch))
162 tty_insert_flip_char(tty, ch, TTY_NORMAL);
163 }
164 tty_flip_buffer_push(tty);
165
166 spin_unlock(&up->port.lock);
167
168 return IRQ_HANDLED;
169}
170
171static irqreturn_t sport_uart_tx_irq(int irq, void *dev_id)
172{
173 struct sport_uart_port *up = dev_id;
174
175 spin_lock(&up->port.lock);
176 sport_uart_tx_chars(up);
177 spin_unlock(&up->port.lock);
178
179 return IRQ_HANDLED;
180}
181
182static irqreturn_t sport_uart_err_irq(int irq, void *dev_id)
183{
184 struct sport_uart_port *up = dev_id;
185 struct tty_struct *tty = up->port.state->port.tty;
186 unsigned int stat = SPORT_GET_STAT(up);
187
188 spin_lock(&up->port.lock);
189
190 /* Overflow in RX FIFO */
191 if (stat & ROVF) {
192 up->port.icount.overrun++;
193 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
194 SPORT_PUT_STAT(up, ROVF); /* Clear ROVF bit */
195 }
196 /* These should not happen */
197 if (stat & (TOVF | TUVF | RUVF)) {
198 pr_err("SPORT Error:%s %s %s\n",
199 (stat & TOVF) ? "TX overflow" : "",
200 (stat & TUVF) ? "TX underflow" : "",
201 (stat & RUVF) ? "RX underflow" : "");
202 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
203 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
204 }
205 SSYNC();
206
207 spin_unlock(&up->port.lock);
208 return IRQ_HANDLED;
209}
210
211#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
212static unsigned int sport_get_mctrl(struct uart_port *port)
213{
214 struct sport_uart_port *up = (struct sport_uart_port *)port;
215 if (up->cts_pin < 0)
216 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
217
218 /* CTS PIN is negative assertive. */
219 if (SPORT_UART_GET_CTS(up))
220 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
221 else
222 return TIOCM_DSR | TIOCM_CAR;
223}
224
225static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
226{
227 struct sport_uart_port *up = (struct sport_uart_port *)port;
228 if (up->rts_pin < 0)
229 return;
230
231 /* RTS PIN is negative assertive. */
232 if (mctrl & TIOCM_RTS)
233 SPORT_UART_ENABLE_RTS(up);
234 else
235 SPORT_UART_DISABLE_RTS(up);
236}
237
238/*
239 * Handle any change of modem status signal.
240 */
241static irqreturn_t sport_mctrl_cts_int(int irq, void *dev_id)
242{
243 struct sport_uart_port *up = (struct sport_uart_port *)dev_id;
244 unsigned int status;
245
246 status = sport_get_mctrl(&up->port);
247 uart_handle_cts_change(&up->port, status & TIOCM_CTS);
248
249 return IRQ_HANDLED;
250}
251#else
252static unsigned int sport_get_mctrl(struct uart_port *port)
253{
254 pr_debug("%s enter\n", __func__);
255 return TIOCM_CTS | TIOCM_CD | TIOCM_DSR;
256}
257
258static void sport_set_mctrl(struct uart_port *port, unsigned int mctrl)
259{
260 pr_debug("%s enter\n", __func__);
261}
262#endif
263
264/* Reqeust IRQ, Setup clock */
265static int sport_startup(struct uart_port *port)
266{
267 struct sport_uart_port *up = (struct sport_uart_port *)port;
268 int ret;
269
270 pr_debug("%s enter\n", __func__);
271 ret = request_irq(up->port.irq, sport_uart_rx_irq, 0,
272 "SPORT_UART_RX", up);
273 if (ret) {
274 dev_err(port->dev, "unable to request SPORT RX interrupt\n");
275 return ret;
276 }
277
278 ret = request_irq(up->port.irq+1, sport_uart_tx_irq, 0,
279 "SPORT_UART_TX", up);
280 if (ret) {
281 dev_err(port->dev, "unable to request SPORT TX interrupt\n");
282 goto fail1;
283 }
284
285 ret = request_irq(up->err_irq, sport_uart_err_irq, 0,
286 "SPORT_UART_STATUS", up);
287 if (ret) {
288 dev_err(port->dev, "unable to request SPORT status interrupt\n");
289 goto fail2;
290 }
291
292#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
293 if (up->cts_pin >= 0) {
294 if (request_irq(gpio_to_irq(up->cts_pin),
295 sport_mctrl_cts_int,
296 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
297 IRQF_DISABLED, "BFIN_SPORT_UART_CTS", up)) {
298 up->cts_pin = -1;
299 dev_info(port->dev, "Unable to attach BlackFin UART over SPORT CTS interrupt. So, disable it.\n");
300 }
301 }
302 if (up->rts_pin >= 0)
303 gpio_direction_output(up->rts_pin, 0);
304#endif
305
306 return 0;
307 fail2:
308 free_irq(up->port.irq+1, up);
309 fail1:
310 free_irq(up->port.irq, up);
311
312 return ret;
313}
314
315/*
316 * sport_uart_tx_chars
317 *
318 * ret 1 means need to enable sport.
319 * ret 0 means do nothing.
320 */
321static int sport_uart_tx_chars(struct sport_uart_port *up)
322{
323 struct circ_buf *xmit = &up->port.state->xmit;
324
325 if (SPORT_GET_STAT(up) & TXF)
326 return 0;
327
328 if (up->port.x_char) {
329 tx_one_byte(up, up->port.x_char);
330 up->port.icount.tx++;
331 up->port.x_char = 0;
332 return 1;
333 }
334
335 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
336 /* The waiting loop to stop SPORT TX from TX interrupt is
337 * too long. This may block SPORT RX interrupts and cause
338 * RX FIFO overflow. So, do stop sport TX only after the last
339 * char in TX FIFO is moved into the shift register.
340 */
341 if (SPORT_GET_STAT(up) & TXHRE)
342 sport_stop_tx(&up->port);
343 return 0;
344 }
345
346 while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
347 tx_one_byte(up, xmit->buf[xmit->tail]);
348 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
349 up->port.icount.tx++;
350 }
351
352 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 uart_write_wakeup(&up->port);
354
355 return 1;
356}
357
358static unsigned int sport_tx_empty(struct uart_port *port)
359{
360 struct sport_uart_port *up = (struct sport_uart_port *)port;
361 unsigned int stat;
362
363 stat = SPORT_GET_STAT(up);
364 pr_debug("%s stat:%04x\n", __func__, stat);
365 if (stat & TXHRE) {
366 return TIOCSER_TEMT;
367 } else
368 return 0;
369}
370
371static void sport_stop_tx(struct uart_port *port)
372{
373 struct sport_uart_port *up = (struct sport_uart_port *)port;
374
375 pr_debug("%s enter\n", __func__);
376
377 if (!(SPORT_GET_TCR1(up) & TSPEN))
378 return;
379
380 /* Although the hold register is empty, last byte is still in shift
381 * register and not sent out yet. So, put a dummy data into TX FIFO.
382 * Then, sport tx stops when last byte is shift out and the dummy
383 * data is moved into the shift register.
384 */
385 SPORT_PUT_TX(up, 0xffff);
386 while (!(SPORT_GET_STAT(up) & TXHRE))
387 cpu_relax();
388
389 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
390 SSYNC();
391
392 return;
393}
394
395static void sport_start_tx(struct uart_port *port)
396{
397 struct sport_uart_port *up = (struct sport_uart_port *)port;
398
399 pr_debug("%s enter\n", __func__);
400
401 /* Write data into SPORT FIFO before enable SPROT to transmit */
402 if (sport_uart_tx_chars(up)) {
403 /* Enable transmit, then an interrupt will generated */
404 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
405 SSYNC();
406 }
407
408 pr_debug("%s exit\n", __func__);
409}
410
411static void sport_stop_rx(struct uart_port *port)
412{
413 struct sport_uart_port *up = (struct sport_uart_port *)port;
414
415 pr_debug("%s enter\n", __func__);
416 /* Disable sport to stop rx */
417 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
418 SSYNC();
419}
420
421static void sport_enable_ms(struct uart_port *port)
422{
423 pr_debug("%s enter\n", __func__);
424}
425
426static void sport_break_ctl(struct uart_port *port, int break_state)
427{
428 pr_debug("%s enter\n", __func__);
429}
430
431static void sport_shutdown(struct uart_port *port)
432{
433 struct sport_uart_port *up = (struct sport_uart_port *)port;
434
435 dev_dbg(port->dev, "%s enter\n", __func__);
436
437 /* Disable sport */
438 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
439 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
440 SSYNC();
441
442 free_irq(up->port.irq, up);
443 free_irq(up->port.irq+1, up);
444 free_irq(up->err_irq, up);
445#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
446 if (up->cts_pin >= 0)
447 free_irq(gpio_to_irq(up->cts_pin), up);
448#endif
449}
450
451static const char *sport_type(struct uart_port *port)
452{
453 struct sport_uart_port *up = (struct sport_uart_port *)port;
454
455 pr_debug("%s enter\n", __func__);
456 return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
457}
458
459static void sport_release_port(struct uart_port *port)
460{
461 pr_debug("%s enter\n", __func__);
462}
463
464static int sport_request_port(struct uart_port *port)
465{
466 pr_debug("%s enter\n", __func__);
467 return 0;
468}
469
470static void sport_config_port(struct uart_port *port, int flags)
471{
472 struct sport_uart_port *up = (struct sport_uart_port *)port;
473
474 pr_debug("%s enter\n", __func__);
475 up->port.type = PORT_BFIN_SPORT;
476}
477
478static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
479{
480 pr_debug("%s enter\n", __func__);
481 return 0;
482}
483
484static void sport_set_termios(struct uart_port *port,
485 struct ktermios *termios, struct ktermios *old)
486{
487 struct sport_uart_port *up = (struct sport_uart_port *)port;
488 unsigned long flags;
489 int i;
490
491 pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
492
493 switch (termios->c_cflag & CSIZE) {
494 case CS8:
495 up->csize = 8;
496 break;
497 case CS7:
498 up->csize = 7;
499 break;
500 case CS6:
501 up->csize = 6;
502 break;
503 case CS5:
504 up->csize = 5;
505 break;
506 default:
507 pr_warning("requested word length not supported\n");
508 }
509
510 if (termios->c_cflag & CSTOPB) {
511 up->stopb = 1;
512 }
513 if (termios->c_cflag & PARENB) {
514 pr_warning("PAREN bits is not supported yet\n");
515 /* up->parib = 1; */
516 }
517
518 spin_lock_irqsave(&up->port.lock, flags);
519
520 port->read_status_mask = 0;
521
522 /*
523 * Characters to ignore
524 */
525 port->ignore_status_mask = 0;
526
527 /* RX extract mask */
528 up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
529 /* TX masks, 8 bit data and 1 bit stop for example:
530 * mask1 = b#0111111110
531 * mask2 = b#1000000000
532 */
533 for (i = 0, up->txmask1 = 0; i < up->csize; i++)
534 up->txmask1 |= (1<<i);
535 up->txmask2 = (1<<i);
536 if (up->stopb) {
537 ++i;
538 up->txmask2 |= (1<<i);
539 }
540 up->txmask1 <<= 1;
541 up->txmask2 <<= 1;
542 /* uart baud rate */
543 port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
544
545 /* Disable UART */
546 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
547 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
548
549 sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
550
551 /* driver TX line high after config, one dummy data is
552 * necessary to stop sport after shift one byte
553 */
554 SPORT_PUT_TX(up, 0xffff);
555 SPORT_PUT_TX(up, 0xffff);
556 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
557 SSYNC();
558 while (!(SPORT_GET_STAT(up) & TXHRE))
559 cpu_relax();
560 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
561 SSYNC();
562
563 /* Port speed changed, update the per-port timeout. */
564 uart_update_timeout(port, termios->c_cflag, port->uartclk);
565
566 /* Enable sport rx */
567 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
568 SSYNC();
569
570 spin_unlock_irqrestore(&up->port.lock, flags);
571}
572
573struct uart_ops sport_uart_ops = {
574 .tx_empty = sport_tx_empty,
575 .set_mctrl = sport_set_mctrl,
576 .get_mctrl = sport_get_mctrl,
577 .stop_tx = sport_stop_tx,
578 .start_tx = sport_start_tx,
579 .stop_rx = sport_stop_rx,
580 .enable_ms = sport_enable_ms,
581 .break_ctl = sport_break_ctl,
582 .startup = sport_startup,
583 .shutdown = sport_shutdown,
584 .set_termios = sport_set_termios,
585 .type = sport_type,
586 .release_port = sport_release_port,
587 .request_port = sport_request_port,
588 .config_port = sport_config_port,
589 .verify_port = sport_verify_port,
590};
591
592#define BFIN_SPORT_UART_MAX_PORTS 4
593
594static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
595
596#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
597#define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
598
599static int __init
600sport_uart_console_setup(struct console *co, char *options)
601{
602 struct sport_uart_port *up;
603 int baud = 57600;
604 int bits = 8;
605 int parity = 'n';
606# ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
607 int flow = 'r';
608# else
609 int flow = 'n';
610# endif
611
612 /* Check whether an invalid uart number has been specified */
613 if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
614 return -ENODEV;
615
616 up = bfin_sport_uart_ports[co->index];
617 if (!up)
618 return -ENODEV;
619
620 if (options)
621 uart_parse_options(options, &baud, &parity, &bits, &flow);
622
623 return uart_set_options(&up->port, co, baud, parity, bits, flow);
624}
625
626static void sport_uart_console_putchar(struct uart_port *port, int ch)
627{
628 struct sport_uart_port *up = (struct sport_uart_port *)port;
629
630 while (SPORT_GET_STAT(up) & TXF)
631 barrier();
632
633 tx_one_byte(up, ch);
634}
635
636/*
637 * Interrupts are disabled on entering
638 */
639static void
640sport_uart_console_write(struct console *co, const char *s, unsigned int count)
641{
642 struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
643 unsigned long flags;
644
645 spin_lock_irqsave(&up->port.lock, flags);
646
647 if (SPORT_GET_TCR1(up) & TSPEN)
648 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
649 else {
650 /* dummy data to start sport */
651 while (SPORT_GET_STAT(up) & TXF)
652 barrier();
653 SPORT_PUT_TX(up, 0xffff);
654 /* Enable transmit, then an interrupt will generated */
655 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
656 SSYNC();
657
658 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
659
660 /* Although the hold register is empty, last byte is still in shift
661 * register and not sent out yet. So, put a dummy data into TX FIFO.
662 * Then, sport tx stops when last byte is shift out and the dummy
663 * data is moved into the shift register.
664 */
665 while (SPORT_GET_STAT(up) & TXF)
666 barrier();
667 SPORT_PUT_TX(up, 0xffff);
668 while (!(SPORT_GET_STAT(up) & TXHRE))
669 barrier();
670
671 /* Stop sport tx transfer */
672 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
673 SSYNC();
674 }
675
676 spin_unlock_irqrestore(&up->port.lock, flags);
677}
678
679static struct uart_driver sport_uart_reg;
680
681static struct console sport_uart_console = {
682 .name = DEVICE_NAME,
683 .write = sport_uart_console_write,
684 .device = uart_console_device,
685 .setup = sport_uart_console_setup,
686 .flags = CON_PRINTBUFFER,
687 .index = -1,
688 .data = &sport_uart_reg,
689};
690
691#define SPORT_UART_CONSOLE (&sport_uart_console)
692#else
693#define SPORT_UART_CONSOLE NULL
694#endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
695
696
697static struct uart_driver sport_uart_reg = {
698 .owner = THIS_MODULE,
699 .driver_name = DRV_NAME,
700 .dev_name = DEVICE_NAME,
701 .major = 204,
702 .minor = 84,
703 .nr = BFIN_SPORT_UART_MAX_PORTS,
704 .cons = SPORT_UART_CONSOLE,
705};
706
707#ifdef CONFIG_PM
708static int sport_uart_suspend(struct device *dev)
709{
710 struct sport_uart_port *sport = dev_get_drvdata(dev);
711
712 dev_dbg(dev, "%s enter\n", __func__);
713 if (sport)
714 uart_suspend_port(&sport_uart_reg, &sport->port);
715
716 return 0;
717}
718
719static int sport_uart_resume(struct device *dev)
720{
721 struct sport_uart_port *sport = dev_get_drvdata(dev);
722
723 dev_dbg(dev, "%s enter\n", __func__);
724 if (sport)
725 uart_resume_port(&sport_uart_reg, &sport->port);
726
727 return 0;
728}
729
730static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
731 .suspend = sport_uart_suspend,
732 .resume = sport_uart_resume,
733};
734#endif
735
736static int __devinit sport_uart_probe(struct platform_device *pdev)
737{
738 struct resource *res;
739 struct sport_uart_port *sport;
740 int ret = 0;
741
742 dev_dbg(&pdev->dev, "%s enter\n", __func__);
743
744 if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
745 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
746 return -ENOENT;
747 }
748
749 if (bfin_sport_uart_ports[pdev->id] == NULL) {
750 bfin_sport_uart_ports[pdev->id] =
751 kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
752 sport = bfin_sport_uart_ports[pdev->id];
753 if (!sport) {
754 dev_err(&pdev->dev,
755 "Fail to malloc sport_uart_port\n");
756 return -ENOMEM;
757 }
758
759 ret = peripheral_request_list(
760 (unsigned short *)pdev->dev.platform_data, DRV_NAME);
761 if (ret) {
762 dev_err(&pdev->dev,
763 "Fail to request SPORT peripherals\n");
764 goto out_error_free_mem;
765 }
766
767 spin_lock_init(&sport->port.lock);
768 sport->port.fifosize = SPORT_TX_FIFO_SIZE,
769 sport->port.ops = &sport_uart_ops;
770 sport->port.line = pdev->id;
771 sport->port.iotype = UPIO_MEM;
772 sport->port.flags = UPF_BOOT_AUTOCONF;
773
774 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
775 if (res == NULL) {
776 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
777 ret = -ENOENT;
778 goto out_error_free_peripherals;
779 }
780
781 sport->port.membase = ioremap(res->start, resource_size(res));
782 if (!sport->port.membase) {
783 dev_err(&pdev->dev, "Cannot map sport IO\n");
784 ret = -ENXIO;
785 goto out_error_free_peripherals;
786 }
787 sport->port.mapbase = res->start;
788
789 sport->port.irq = platform_get_irq(pdev, 0);
790 if ((int)sport->port.irq < 0) {
791 dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
792 ret = -ENOENT;
793 goto out_error_unmap;
794 }
795
796 sport->err_irq = platform_get_irq(pdev, 1);
797 if (sport->err_irq < 0) {
798 dev_err(&pdev->dev, "No sport status IRQ specified\n");
799 ret = -ENOENT;
800 goto out_error_unmap;
801 }
802#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
803 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
804 if (res == NULL)
805 sport->cts_pin = -1;
806 else
807 sport->cts_pin = res->start;
808
809 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
810 if (res == NULL)
811 sport->rts_pin = -1;
812 else
813 sport->rts_pin = res->start;
814
815 if (sport->rts_pin >= 0)
816 gpio_request(sport->rts_pin, DRV_NAME);
817#endif
818 }
819
820#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
821 if (!is_early_platform_device(pdev)) {
822#endif
823 sport = bfin_sport_uart_ports[pdev->id];
824 sport->port.dev = &pdev->dev;
825 dev_set_drvdata(&pdev->dev, sport);
826 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
827#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
828 }
829#endif
830 if (!ret)
831 return 0;
832
833 if (sport) {
834out_error_unmap:
835 iounmap(sport->port.membase);
836out_error_free_peripherals:
837 peripheral_free_list(
838 (unsigned short *)pdev->dev.platform_data);
839out_error_free_mem:
840 kfree(sport);
841 bfin_sport_uart_ports[pdev->id] = NULL;
842 }
843
844 return ret;
845}
846
847static int __devexit sport_uart_remove(struct platform_device *pdev)
848{
849 struct sport_uart_port *sport = platform_get_drvdata(pdev);
850
851 dev_dbg(&pdev->dev, "%s enter\n", __func__);
852 dev_set_drvdata(&pdev->dev, NULL);
853
854 if (sport) {
855 uart_remove_one_port(&sport_uart_reg, &sport->port);
856#ifdef CONFIG_SERIAL_BFIN_CTSRTS
857 if (sport->rts_pin >= 0)
858 gpio_free(sport->rts_pin);
859#endif
860 iounmap(sport->port.membase);
861 peripheral_free_list(
862 (unsigned short *)pdev->dev.platform_data);
863 kfree(sport);
864 bfin_sport_uart_ports[pdev->id] = NULL;
865 }
866
867 return 0;
868}
869
870static struct platform_driver sport_uart_driver = {
871 .probe = sport_uart_probe,
872 .remove = __devexit_p(sport_uart_remove),
873 .driver = {
874 .name = DRV_NAME,
875#ifdef CONFIG_PM
876 .pm = &bfin_sport_uart_dev_pm_ops,
877#endif
878 },
879};
880
881#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
882static __initdata struct early_platform_driver early_sport_uart_driver = {
883 .class_str = CLASS_BFIN_SPORT_CONSOLE,
884 .pdrv = &sport_uart_driver,
885 .requested_id = EARLY_PLATFORM_ID_UNSET,
886};
887
888static int __init sport_uart_rs_console_init(void)
889{
890 early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
891
892 early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
893 BFIN_SPORT_UART_MAX_PORTS, 0);
894
895 register_console(&sport_uart_console);
896
897 return 0;
898}
899console_initcall(sport_uart_rs_console_init);
900#endif
901
902static int __init sport_uart_init(void)
903{
904 int ret;
905
906 pr_info("Blackfin uart over sport driver\n");
907
908 ret = uart_register_driver(&sport_uart_reg);
909 if (ret) {
910 pr_err("failed to register %s:%d\n",
911 sport_uart_reg.driver_name, ret);
912 return ret;
913 }
914
915 ret = platform_driver_register(&sport_uart_driver);
916 if (ret) {
917 pr_err("failed to register sport uart driver:%d\n", ret);
918 uart_unregister_driver(&sport_uart_reg);
919 }
920
921 return ret;
922}
923module_init(sport_uart_init);
924
925static void __exit sport_uart_exit(void)
926{
927 platform_driver_unregister(&sport_uart_driver);
928 uart_unregister_driver(&sport_uart_reg);
929}
930module_exit(sport_uart_exit);
931
932MODULE_AUTHOR("Sonic Zhang, Roy Huang");
933MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
934MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/bfin_sport_uart.h b/drivers/tty/serial/bfin_sport_uart.h
new file mode 100644
index 000000000000..6d06ce1d5675
--- /dev/null
+++ b/drivers/tty/serial/bfin_sport_uart.h
@@ -0,0 +1,86 @@
1/*
2 * Blackfin On-Chip Sport Emulated UART Driver
3 *
4 * Copyright 2006-2008 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/*
12 * This driver and the hardware supported are in term of EE-191 of ADI.
13 * http://www.analog.com/static/imported-files/application_notes/EE191.pdf
14 * This application note describe how to implement a UART on a Sharc DSP,
15 * but this driver is implemented on Blackfin Processor.
16 * Transmit Frame Sync is not used by this driver to transfer data out.
17 */
18
19#ifndef _BFIN_SPORT_UART_H
20#define _BFIN_SPORT_UART_H
21
22#define OFFSET_TCR1 0x00 /* Transmit Configuration 1 Register */
23#define OFFSET_TCR2 0x04 /* Transmit Configuration 2 Register */
24#define OFFSET_TCLKDIV 0x08 /* Transmit Serial Clock Divider Register */
25#define OFFSET_TFSDIV 0x0C /* Transmit Frame Sync Divider Register */
26#define OFFSET_TX 0x10 /* Transmit Data Register */
27#define OFFSET_RX 0x18 /* Receive Data Register */
28#define OFFSET_RCR1 0x20 /* Receive Configuration 1 Register */
29#define OFFSET_RCR2 0x24 /* Receive Configuration 2 Register */
30#define OFFSET_RCLKDIV 0x28 /* Receive Serial Clock Divider Register */
31#define OFFSET_RFSDIV 0x2c /* Receive Frame Sync Divider Register */
32#define OFFSET_STAT 0x30 /* Status Register */
33
34#define SPORT_GET_TCR1(sport) bfin_read16(((sport)->port.membase + OFFSET_TCR1))
35#define SPORT_GET_TCR2(sport) bfin_read16(((sport)->port.membase + OFFSET_TCR2))
36#define SPORT_GET_TCLKDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_TCLKDIV))
37#define SPORT_GET_TFSDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_TFSDIV))
38#define SPORT_GET_TX(sport) bfin_read16(((sport)->port.membase + OFFSET_TX))
39#define SPORT_GET_RX(sport) bfin_read16(((sport)->port.membase + OFFSET_RX))
40/*
41 * If another interrupt fires while doing a 32-bit read from RX FIFO,
42 * a fake RX underflow error will be generated. So disable interrupts
43 * to prevent interruption while reading the FIFO.
44 */
45#define SPORT_GET_RX32(sport) \
46({ \
47 unsigned int __ret; \
48 if (ANOMALY_05000473) \
49 local_irq_disable(); \
50 __ret = bfin_read32((sport)->port.membase + OFFSET_RX); \
51 if (ANOMALY_05000473) \
52 local_irq_enable(); \
53 __ret; \
54})
55#define SPORT_GET_RCR1(sport) bfin_read16(((sport)->port.membase + OFFSET_RCR1))
56#define SPORT_GET_RCR2(sport) bfin_read16(((sport)->port.membase + OFFSET_RCR2))
57#define SPORT_GET_RCLKDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_RCLKDIV))
58#define SPORT_GET_RFSDIV(sport) bfin_read16(((sport)->port.membase + OFFSET_RFSDIV))
59#define SPORT_GET_STAT(sport) bfin_read16(((sport)->port.membase + OFFSET_STAT))
60
61#define SPORT_PUT_TCR1(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCR1), v)
62#define SPORT_PUT_TCR2(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCR2), v)
63#define SPORT_PUT_TCLKDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TCLKDIV), v)
64#define SPORT_PUT_TFSDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TFSDIV), v)
65#define SPORT_PUT_TX(sport, v) bfin_write16(((sport)->port.membase + OFFSET_TX), v)
66#define SPORT_PUT_RX(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RX), v)
67#define SPORT_PUT_RCR1(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCR1), v)
68#define SPORT_PUT_RCR2(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCR2), v)
69#define SPORT_PUT_RCLKDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RCLKDIV), v)
70#define SPORT_PUT_RFSDIV(sport, v) bfin_write16(((sport)->port.membase + OFFSET_RFSDIV), v)
71#define SPORT_PUT_STAT(sport, v) bfin_write16(((sport)->port.membase + OFFSET_STAT), v)
72
73#define SPORT_TX_FIFO_SIZE 8
74
75#define SPORT_UART_GET_CTS(x) gpio_get_value(x->cts_pin)
76#define SPORT_UART_DISABLE_RTS(x) gpio_set_value(x->rts_pin, 1)
77#define SPORT_UART_ENABLE_RTS(x) gpio_set_value(x->rts_pin, 0)
78
79#if defined(CONFIG_SERIAL_BFIN_SPORT0_UART_CTSRTS) \
80 || defined(CONFIG_SERIAL_BFIN_SPORT1_UART_CTSRTS) \
81 || defined(CONFIG_SERIAL_BFIN_SPORT2_UART_CTSRTS) \
82 || defined(CONFIG_SERIAL_BFIN_SPORT3_UART_CTSRTS)
83# define CONFIG_SERIAL_BFIN_SPORT_CTSRTS
84#endif
85
86#endif /* _BFIN_SPORT_UART_H */
diff --git a/drivers/tty/serial/clps711x.c b/drivers/tty/serial/clps711x.c
new file mode 100644
index 000000000000..e6c3dbd781d6
--- /dev/null
+++ b/drivers/tty/serial/clps711x.c
@@ -0,0 +1,577 @@
1/*
2 * Driver for CLPS711x serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/module.h>
29#include <linux/ioport.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/sysrq.h>
33#include <linux/spinlock.h>
34#include <linux/device.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial.h>
39#include <linux/io.h>
40
41#include <mach/hardware.h>
42#include <asm/irq.h>
43#include <asm/hardware/clps7111.h>
44
45#define UART_NR 2
46
47#define SERIAL_CLPS711X_MAJOR 204
48#define SERIAL_CLPS711X_MINOR 40
49#define SERIAL_CLPS711X_NR UART_NR
50
51/*
52 * We use the relevant SYSCON register as a base address for these ports.
53 */
54#define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
55#define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
56#define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
57#define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
58
59#define TX_IRQ(port) ((port)->irq)
60#define RX_IRQ(port) ((port)->irq + 1)
61
62#define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
63
64#define tx_enabled(port) ((port)->unused[0])
65
66static void clps711xuart_stop_tx(struct uart_port *port)
67{
68 if (tx_enabled(port)) {
69 disable_irq(TX_IRQ(port));
70 tx_enabled(port) = 0;
71 }
72}
73
74static void clps711xuart_start_tx(struct uart_port *port)
75{
76 if (!tx_enabled(port)) {
77 enable_irq(TX_IRQ(port));
78 tx_enabled(port) = 1;
79 }
80}
81
82static void clps711xuart_stop_rx(struct uart_port *port)
83{
84 disable_irq(RX_IRQ(port));
85}
86
87static void clps711xuart_enable_ms(struct uart_port *port)
88{
89}
90
91static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
92{
93 struct uart_port *port = dev_id;
94 struct tty_struct *tty = port->state->port.tty;
95 unsigned int status, ch, flg;
96
97 status = clps_readl(SYSFLG(port));
98 while (!(status & SYSFLG_URXFE)) {
99 ch = clps_readl(UARTDR(port));
100
101 port->icount.rx++;
102
103 flg = TTY_NORMAL;
104
105 /*
106 * Note that the error handling code is
107 * out of the main execution path
108 */
109 if (unlikely(ch & UART_ANY_ERR)) {
110 if (ch & UARTDR_PARERR)
111 port->icount.parity++;
112 else if (ch & UARTDR_FRMERR)
113 port->icount.frame++;
114 if (ch & UARTDR_OVERR)
115 port->icount.overrun++;
116
117 ch &= port->read_status_mask;
118
119 if (ch & UARTDR_PARERR)
120 flg = TTY_PARITY;
121 else if (ch & UARTDR_FRMERR)
122 flg = TTY_FRAME;
123
124#ifdef SUPPORT_SYSRQ
125 port->sysrq = 0;
126#endif
127 }
128
129 if (uart_handle_sysrq_char(port, ch))
130 goto ignore_char;
131
132 /*
133 * CHECK: does overrun affect the current character?
134 * ASSUMPTION: it does not.
135 */
136 uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
137
138 ignore_char:
139 status = clps_readl(SYSFLG(port));
140 }
141 tty_flip_buffer_push(tty);
142 return IRQ_HANDLED;
143}
144
145static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
146{
147 struct uart_port *port = dev_id;
148 struct circ_buf *xmit = &port->state->xmit;
149 int count;
150
151 if (port->x_char) {
152 clps_writel(port->x_char, UARTDR(port));
153 port->icount.tx++;
154 port->x_char = 0;
155 return IRQ_HANDLED;
156 }
157 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
158 clps711xuart_stop_tx(port);
159 return IRQ_HANDLED;
160 }
161
162 count = port->fifosize >> 1;
163 do {
164 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
165 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
166 port->icount.tx++;
167 if (uart_circ_empty(xmit))
168 break;
169 } while (--count > 0);
170
171 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
172 uart_write_wakeup(port);
173
174 if (uart_circ_empty(xmit))
175 clps711xuart_stop_tx(port);
176
177 return IRQ_HANDLED;
178}
179
180static unsigned int clps711xuart_tx_empty(struct uart_port *port)
181{
182 unsigned int status = clps_readl(SYSFLG(port));
183 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
184}
185
186static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
187{
188 unsigned int port_addr;
189 unsigned int result = 0;
190 unsigned int status;
191
192 port_addr = SYSFLG(port);
193 if (port_addr == SYSFLG1) {
194 status = clps_readl(SYSFLG1);
195 if (status & SYSFLG1_DCD)
196 result |= TIOCM_CAR;
197 if (status & SYSFLG1_DSR)
198 result |= TIOCM_DSR;
199 if (status & SYSFLG1_CTS)
200 result |= TIOCM_CTS;
201 }
202
203 return result;
204}
205
206static void
207clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
208{
209}
210
211static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
212{
213 unsigned long flags;
214 unsigned int ubrlcr;
215
216 spin_lock_irqsave(&port->lock, flags);
217 ubrlcr = clps_readl(UBRLCR(port));
218 if (break_state == -1)
219 ubrlcr |= UBRLCR_BREAK;
220 else
221 ubrlcr &= ~UBRLCR_BREAK;
222 clps_writel(ubrlcr, UBRLCR(port));
223 spin_unlock_irqrestore(&port->lock, flags);
224}
225
226static int clps711xuart_startup(struct uart_port *port)
227{
228 unsigned int syscon;
229 int retval;
230
231 tx_enabled(port) = 1;
232
233 /*
234 * Allocate the IRQs
235 */
236 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
237 "clps711xuart_tx", port);
238 if (retval)
239 return retval;
240
241 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
242 "clps711xuart_rx", port);
243 if (retval) {
244 free_irq(TX_IRQ(port), port);
245 return retval;
246 }
247
248 /*
249 * enable the port
250 */
251 syscon = clps_readl(SYSCON(port));
252 syscon |= SYSCON_UARTEN;
253 clps_writel(syscon, SYSCON(port));
254
255 return 0;
256}
257
258static void clps711xuart_shutdown(struct uart_port *port)
259{
260 unsigned int ubrlcr, syscon;
261
262 /*
263 * Free the interrupt
264 */
265 free_irq(TX_IRQ(port), port); /* TX interrupt */
266 free_irq(RX_IRQ(port), port); /* RX interrupt */
267
268 /*
269 * disable the port
270 */
271 syscon = clps_readl(SYSCON(port));
272 syscon &= ~SYSCON_UARTEN;
273 clps_writel(syscon, SYSCON(port));
274
275 /*
276 * disable break condition and fifos
277 */
278 ubrlcr = clps_readl(UBRLCR(port));
279 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
280 clps_writel(ubrlcr, UBRLCR(port));
281}
282
283static void
284clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
285 struct ktermios *old)
286{
287 unsigned int ubrlcr, baud, quot;
288 unsigned long flags;
289
290 /*
291 * We don't implement CREAD.
292 */
293 termios->c_cflag |= CREAD;
294
295 /*
296 * Ask the core to calculate the divisor for us.
297 */
298 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
299 quot = uart_get_divisor(port, baud);
300
301 switch (termios->c_cflag & CSIZE) {
302 case CS5:
303 ubrlcr = UBRLCR_WRDLEN5;
304 break;
305 case CS6:
306 ubrlcr = UBRLCR_WRDLEN6;
307 break;
308 case CS7:
309 ubrlcr = UBRLCR_WRDLEN7;
310 break;
311 default: // CS8
312 ubrlcr = UBRLCR_WRDLEN8;
313 break;
314 }
315 if (termios->c_cflag & CSTOPB)
316 ubrlcr |= UBRLCR_XSTOP;
317 if (termios->c_cflag & PARENB) {
318 ubrlcr |= UBRLCR_PRTEN;
319 if (!(termios->c_cflag & PARODD))
320 ubrlcr |= UBRLCR_EVENPRT;
321 }
322 if (port->fifosize > 1)
323 ubrlcr |= UBRLCR_FIFOEN;
324
325 spin_lock_irqsave(&port->lock, flags);
326
327 /*
328 * Update the per-port timeout.
329 */
330 uart_update_timeout(port, termios->c_cflag, baud);
331
332 port->read_status_mask = UARTDR_OVERR;
333 if (termios->c_iflag & INPCK)
334 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
335
336 /*
337 * Characters to ignore
338 */
339 port->ignore_status_mask = 0;
340 if (termios->c_iflag & IGNPAR)
341 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
342 if (termios->c_iflag & IGNBRK) {
343 /*
344 * If we're ignoring parity and break indicators,
345 * ignore overruns to (for real raw support).
346 */
347 if (termios->c_iflag & IGNPAR)
348 port->ignore_status_mask |= UARTDR_OVERR;
349 }
350
351 quot -= 1;
352
353 clps_writel(ubrlcr | quot, UBRLCR(port));
354
355 spin_unlock_irqrestore(&port->lock, flags);
356}
357
358static const char *clps711xuart_type(struct uart_port *port)
359{
360 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
361}
362
363/*
364 * Configure/autoconfigure the port.
365 */
366static void clps711xuart_config_port(struct uart_port *port, int flags)
367{
368 if (flags & UART_CONFIG_TYPE)
369 port->type = PORT_CLPS711X;
370}
371
372static void clps711xuart_release_port(struct uart_port *port)
373{
374}
375
376static int clps711xuart_request_port(struct uart_port *port)
377{
378 return 0;
379}
380
381static struct uart_ops clps711x_pops = {
382 .tx_empty = clps711xuart_tx_empty,
383 .set_mctrl = clps711xuart_set_mctrl_null,
384 .get_mctrl = clps711xuart_get_mctrl,
385 .stop_tx = clps711xuart_stop_tx,
386 .start_tx = clps711xuart_start_tx,
387 .stop_rx = clps711xuart_stop_rx,
388 .enable_ms = clps711xuart_enable_ms,
389 .break_ctl = clps711xuart_break_ctl,
390 .startup = clps711xuart_startup,
391 .shutdown = clps711xuart_shutdown,
392 .set_termios = clps711xuart_set_termios,
393 .type = clps711xuart_type,
394 .config_port = clps711xuart_config_port,
395 .release_port = clps711xuart_release_port,
396 .request_port = clps711xuart_request_port,
397};
398
399static struct uart_port clps711x_ports[UART_NR] = {
400 {
401 .iobase = SYSCON1,
402 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
403 .uartclk = 3686400,
404 .fifosize = 16,
405 .ops = &clps711x_pops,
406 .line = 0,
407 .flags = UPF_BOOT_AUTOCONF,
408 },
409 {
410 .iobase = SYSCON2,
411 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
412 .uartclk = 3686400,
413 .fifosize = 16,
414 .ops = &clps711x_pops,
415 .line = 1,
416 .flags = UPF_BOOT_AUTOCONF,
417 }
418};
419
420#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
421static void clps711xuart_console_putchar(struct uart_port *port, int ch)
422{
423 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
424 barrier();
425 clps_writel(ch, UARTDR(port));
426}
427
428/*
429 * Print a string to the serial port trying not to disturb
430 * any possible real use of the port...
431 *
432 * The console_lock must be held when we get here.
433 *
434 * Note that this is called with interrupts already disabled
435 */
436static void
437clps711xuart_console_write(struct console *co, const char *s,
438 unsigned int count)
439{
440 struct uart_port *port = clps711x_ports + co->index;
441 unsigned int status, syscon;
442
443 /*
444 * Ensure that the port is enabled.
445 */
446 syscon = clps_readl(SYSCON(port));
447 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
448
449 uart_console_write(port, s, count, clps711xuart_console_putchar);
450
451 /*
452 * Finally, wait for transmitter to become empty
453 * and restore the uart state.
454 */
455 do {
456 status = clps_readl(SYSFLG(port));
457 } while (status & SYSFLG_UBUSY);
458
459 clps_writel(syscon, SYSCON(port));
460}
461
462static void __init
463clps711xuart_console_get_options(struct uart_port *port, int *baud,
464 int *parity, int *bits)
465{
466 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
467 unsigned int ubrlcr, quot;
468
469 ubrlcr = clps_readl(UBRLCR(port));
470
471 *parity = 'n';
472 if (ubrlcr & UBRLCR_PRTEN) {
473 if (ubrlcr & UBRLCR_EVENPRT)
474 *parity = 'e';
475 else
476 *parity = 'o';
477 }
478
479 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
480 *bits = 7;
481 else
482 *bits = 8;
483
484 quot = ubrlcr & UBRLCR_BAUD_MASK;
485 *baud = port->uartclk / (16 * (quot + 1));
486 }
487}
488
489static int __init clps711xuart_console_setup(struct console *co, char *options)
490{
491 struct uart_port *port;
492 int baud = 38400;
493 int bits = 8;
494 int parity = 'n';
495 int flow = 'n';
496
497 /*
498 * Check whether an invalid uart number has been specified, and
499 * if so, search for the first available port that does have
500 * console support.
501 */
502 port = uart_get_console(clps711x_ports, UART_NR, co);
503
504 if (options)
505 uart_parse_options(options, &baud, &parity, &bits, &flow);
506 else
507 clps711xuart_console_get_options(port, &baud, &parity, &bits);
508
509 return uart_set_options(port, co, baud, parity, bits, flow);
510}
511
512static struct uart_driver clps711x_reg;
513static struct console clps711x_console = {
514 .name = "ttyCL",
515 .write = clps711xuart_console_write,
516 .device = uart_console_device,
517 .setup = clps711xuart_console_setup,
518 .flags = CON_PRINTBUFFER,
519 .index = -1,
520 .data = &clps711x_reg,
521};
522
523static int __init clps711xuart_console_init(void)
524{
525 register_console(&clps711x_console);
526 return 0;
527}
528console_initcall(clps711xuart_console_init);
529
530#define CLPS711X_CONSOLE &clps711x_console
531#else
532#define CLPS711X_CONSOLE NULL
533#endif
534
535static struct uart_driver clps711x_reg = {
536 .driver_name = "ttyCL",
537 .dev_name = "ttyCL",
538 .major = SERIAL_CLPS711X_MAJOR,
539 .minor = SERIAL_CLPS711X_MINOR,
540 .nr = UART_NR,
541
542 .cons = CLPS711X_CONSOLE,
543};
544
545static int __init clps711xuart_init(void)
546{
547 int ret, i;
548
549 printk(KERN_INFO "Serial: CLPS711x driver\n");
550
551 ret = uart_register_driver(&clps711x_reg);
552 if (ret)
553 return ret;
554
555 for (i = 0; i < UART_NR; i++)
556 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
557
558 return 0;
559}
560
561static void __exit clps711xuart_exit(void)
562{
563 int i;
564
565 for (i = 0; i < UART_NR; i++)
566 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
567
568 uart_unregister_driver(&clps711x_reg);
569}
570
571module_init(clps711xuart_init);
572module_exit(clps711xuart_exit);
573
574MODULE_AUTHOR("Deep Blue Solutions Ltd");
575MODULE_DESCRIPTION("CLPS-711x generic serial driver");
576MODULE_LICENSE("GPL");
577MODULE_ALIAS_CHARDEV(SERIAL_CLPS711X_MAJOR, SERIAL_CLPS711X_MINOR);
diff --git a/drivers/tty/serial/cpm_uart/Makefile b/drivers/tty/serial/cpm_uart/Makefile
new file mode 100644
index 000000000000..e072724ea754
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/Makefile
@@ -0,0 +1,11 @@
1#
2# Makefile for the Motorola 8xx FEC ethernet controller
3#
4
5obj-$(CONFIG_SERIAL_CPM) += cpm_uart.o
6
7# Select the correct platform objects.
8cpm_uart-objs-$(CONFIG_CPM2) += cpm_uart_cpm2.o
9cpm_uart-objs-$(CONFIG_8xx) += cpm_uart_cpm1.o
10
11cpm_uart-objs := cpm_uart_core.o $(cpm_uart-objs-y)
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart.h b/drivers/tty/serial/cpm_uart/cpm_uart.h
new file mode 100644
index 000000000000..cf34d26ff6cd
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart.h
@@ -0,0 +1,145 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * Copyright (C) 2004 Freescale Semiconductor, Inc.
5 *
6 * 2006 (c) MontaVista Software, Inc.
7 * Vitaly Bordug <vbordug@ru.mvista.com>
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 *
13 */
14#ifndef CPM_UART_H
15#define CPM_UART_H
16
17#include <linux/platform_device.h>
18#include <linux/fs_uart_pd.h>
19
20#if defined(CONFIG_CPM2)
21#include "cpm_uart_cpm2.h"
22#elif defined(CONFIG_8xx)
23#include "cpm_uart_cpm1.h"
24#endif
25
26#define SERIAL_CPM_MAJOR 204
27#define SERIAL_CPM_MINOR 46
28
29#define IS_SMC(pinfo) (pinfo->flags & FLAG_SMC)
30#define IS_DISCARDING(pinfo) (pinfo->flags & FLAG_DISCARDING)
31#define FLAG_DISCARDING 0x00000004 /* when set, don't discard */
32#define FLAG_SMC 0x00000002
33#define FLAG_CONSOLE 0x00000001
34
35#define UART_SMC1 fsid_smc1_uart
36#define UART_SMC2 fsid_smc2_uart
37#define UART_SCC1 fsid_scc1_uart
38#define UART_SCC2 fsid_scc2_uart
39#define UART_SCC3 fsid_scc3_uart
40#define UART_SCC4 fsid_scc4_uart
41
42#define UART_NR fs_uart_nr
43
44#define RX_NUM_FIFO 4
45#define RX_BUF_SIZE 32
46#define TX_NUM_FIFO 4
47#define TX_BUF_SIZE 32
48
49#define SCC_WAIT_CLOSING 100
50
51#define GPIO_CTS 0
52#define GPIO_RTS 1
53#define GPIO_DCD 2
54#define GPIO_DSR 3
55#define GPIO_DTR 4
56#define GPIO_RI 5
57
58#define NUM_GPIOS (GPIO_RI+1)
59
60struct uart_cpm_port {
61 struct uart_port port;
62 u16 rx_nrfifos;
63 u16 rx_fifosize;
64 u16 tx_nrfifos;
65 u16 tx_fifosize;
66 smc_t __iomem *smcp;
67 smc_uart_t __iomem *smcup;
68 scc_t __iomem *sccp;
69 scc_uart_t __iomem *sccup;
70 cbd_t __iomem *rx_bd_base;
71 cbd_t __iomem *rx_cur;
72 cbd_t __iomem *tx_bd_base;
73 cbd_t __iomem *tx_cur;
74 unsigned char *tx_buf;
75 unsigned char *rx_buf;
76 u32 flags;
77 struct clk *clk;
78 u8 brg;
79 uint dp_addr;
80 void *mem_addr;
81 dma_addr_t dma_addr;
82 u32 mem_size;
83 /* wait on close if needed */
84 int wait_closing;
85 /* value to combine with opcode to form cpm command */
86 u32 command;
87 int gpios[NUM_GPIOS];
88};
89
90extern int cpm_uart_nr;
91extern struct uart_cpm_port cpm_uart_ports[UART_NR];
92
93/* these are located in their respective files */
94void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd);
95void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
96 struct device_node *np);
97void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram);
98int cpm_uart_init_portdesc(void);
99int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con);
100void cpm_uart_freebuf(struct uart_cpm_port *pinfo);
101
102void smc1_lineif(struct uart_cpm_port *pinfo);
103void smc2_lineif(struct uart_cpm_port *pinfo);
104void scc1_lineif(struct uart_cpm_port *pinfo);
105void scc2_lineif(struct uart_cpm_port *pinfo);
106void scc3_lineif(struct uart_cpm_port *pinfo);
107void scc4_lineif(struct uart_cpm_port *pinfo);
108
109/*
110 virtual to phys transtalion
111*/
112static inline unsigned long cpu2cpm_addr(void *addr,
113 struct uart_cpm_port *pinfo)
114{
115 int offset;
116 u32 val = (u32)addr;
117 u32 mem = (u32)pinfo->mem_addr;
118 /* sane check */
119 if (likely(val >= mem && val < mem + pinfo->mem_size)) {
120 offset = val - mem;
121 return pinfo->dma_addr + offset;
122 }
123 /* something nasty happened */
124 BUG();
125 return 0;
126}
127
128static inline void *cpm2cpu_addr(unsigned long addr,
129 struct uart_cpm_port *pinfo)
130{
131 int offset;
132 u32 val = addr;
133 u32 dma = (u32)pinfo->dma_addr;
134 /* sane check */
135 if (likely(val >= dma && val < dma + pinfo->mem_size)) {
136 offset = val - dma;
137 return pinfo->mem_addr + offset;
138 }
139 /* something nasty happened */
140 BUG();
141 return NULL;
142}
143
144
145#endif /* CPM_UART_H */
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_core.c b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
new file mode 100644
index 000000000000..9488da74d4f7
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
@@ -0,0 +1,1440 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; core driver
3 *
4 * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
5 * Based on ppc8xx.c by Thomas Gleixner
6 * Based on drivers/serial/amba.c by Russell King
7 *
8 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
9 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
10 *
11 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
12 * (C) 2004 Intracom, S.A.
13 * (C) 2005-2006 MontaVista Software, Inc.
14 * Vitaly Bordug <vbordug@ru.mvista.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 */
31
32#include <linux/module.h>
33#include <linux/tty.h>
34#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/serial.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/device.h>
40#include <linux/bootmem.h>
41#include <linux/dma-mapping.h>
42#include <linux/fs_uart_pd.h>
43#include <linux/of_platform.h>
44#include <linux/gpio.h>
45#include <linux/of_gpio.h>
46#include <linux/clk.h>
47
48#include <asm/io.h>
49#include <asm/irq.h>
50#include <asm/delay.h>
51#include <asm/fs_pd.h>
52#include <asm/udbg.h>
53
54#if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
55#define SUPPORT_SYSRQ
56#endif
57
58#include <linux/serial_core.h>
59#include <linux/kernel.h>
60
61#include "cpm_uart.h"
62
63
64/**************************************************************/
65
66static int cpm_uart_tx_pump(struct uart_port *port);
67static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
68static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
69static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
70
71/**************************************************************/
72
73#define HW_BUF_SPD_THRESHOLD 9600
74
75/*
76 * Check, if transmit buffers are processed
77*/
78static unsigned int cpm_uart_tx_empty(struct uart_port *port)
79{
80 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
81 cbd_t __iomem *bdp = pinfo->tx_bd_base;
82 int ret = 0;
83
84 while (1) {
85 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
86 break;
87
88 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
89 ret = TIOCSER_TEMT;
90 break;
91 }
92 bdp++;
93 }
94
95 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
96
97 return ret;
98}
99
100static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
102 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
103
104 if (pinfo->gpios[GPIO_RTS] >= 0)
105 gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
106
107 if (pinfo->gpios[GPIO_DTR] >= 0)
108 gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
109}
110
111static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
112{
113 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114 unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
115
116 if (pinfo->gpios[GPIO_CTS] >= 0) {
117 if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
118 mctrl &= ~TIOCM_CTS;
119 }
120
121 if (pinfo->gpios[GPIO_DSR] >= 0) {
122 if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
123 mctrl &= ~TIOCM_DSR;
124 }
125
126 if (pinfo->gpios[GPIO_DCD] >= 0) {
127 if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
128 mctrl &= ~TIOCM_CAR;
129 }
130
131 if (pinfo->gpios[GPIO_RI] >= 0) {
132 if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
133 mctrl |= TIOCM_RNG;
134 }
135
136 return mctrl;
137}
138
139/*
140 * Stop transmitter
141 */
142static void cpm_uart_stop_tx(struct uart_port *port)
143{
144 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
145 smc_t __iomem *smcp = pinfo->smcp;
146 scc_t __iomem *sccp = pinfo->sccp;
147
148 pr_debug("CPM uart[%d]:stop tx\n", port->line);
149
150 if (IS_SMC(pinfo))
151 clrbits8(&smcp->smc_smcm, SMCM_TX);
152 else
153 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
154}
155
156/*
157 * Start transmitter
158 */
159static void cpm_uart_start_tx(struct uart_port *port)
160{
161 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
162 smc_t __iomem *smcp = pinfo->smcp;
163 scc_t __iomem *sccp = pinfo->sccp;
164
165 pr_debug("CPM uart[%d]:start tx\n", port->line);
166
167 if (IS_SMC(pinfo)) {
168 if (in_8(&smcp->smc_smcm) & SMCM_TX)
169 return;
170 } else {
171 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
172 return;
173 }
174
175 if (cpm_uart_tx_pump(port) != 0) {
176 if (IS_SMC(pinfo)) {
177 setbits8(&smcp->smc_smcm, SMCM_TX);
178 } else {
179 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
180 }
181 }
182}
183
184/*
185 * Stop receiver
186 */
187static void cpm_uart_stop_rx(struct uart_port *port)
188{
189 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
190 smc_t __iomem *smcp = pinfo->smcp;
191 scc_t __iomem *sccp = pinfo->sccp;
192
193 pr_debug("CPM uart[%d]:stop rx\n", port->line);
194
195 if (IS_SMC(pinfo))
196 clrbits8(&smcp->smc_smcm, SMCM_RX);
197 else
198 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
199}
200
201/*
202 * Enable Modem status interrupts
203 */
204static void cpm_uart_enable_ms(struct uart_port *port)
205{
206 pr_debug("CPM uart[%d]:enable ms\n", port->line);
207}
208
209/*
210 * Generate a break.
211 */
212static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
213{
214 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
215
216 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
217 break_state);
218
219 if (break_state)
220 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
221 else
222 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
223}
224
225/*
226 * Transmit characters, refill buffer descriptor, if possible
227 */
228static void cpm_uart_int_tx(struct uart_port *port)
229{
230 pr_debug("CPM uart[%d]:TX INT\n", port->line);
231
232 cpm_uart_tx_pump(port);
233}
234
235#ifdef CONFIG_CONSOLE_POLL
236static int serial_polled;
237#endif
238
239/*
240 * Receive characters
241 */
242static void cpm_uart_int_rx(struct uart_port *port)
243{
244 int i;
245 unsigned char ch;
246 u8 *cp;
247 struct tty_struct *tty = port->state->port.tty;
248 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
249 cbd_t __iomem *bdp;
250 u16 status;
251 unsigned int flg;
252
253 pr_debug("CPM uart[%d]:RX INT\n", port->line);
254
255 /* Just loop through the closed BDs and copy the characters into
256 * the buffer.
257 */
258 bdp = pinfo->rx_cur;
259 for (;;) {
260#ifdef CONFIG_CONSOLE_POLL
261 if (unlikely(serial_polled)) {
262 serial_polled = 0;
263 return;
264 }
265#endif
266 /* get status */
267 status = in_be16(&bdp->cbd_sc);
268 /* If this one is empty, return happy */
269 if (status & BD_SC_EMPTY)
270 break;
271
272 /* get number of characters, and check spce in flip-buffer */
273 i = in_be16(&bdp->cbd_datlen);
274
275 /* If we have not enough room in tty flip buffer, then we try
276 * later, which will be the next rx-interrupt or a timeout
277 */
278 if(tty_buffer_request_room(tty, i) < i) {
279 printk(KERN_WARNING "No room in flip buffer\n");
280 return;
281 }
282
283 /* get pointer */
284 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
285
286 /* loop through the buffer */
287 while (i-- > 0) {
288 ch = *cp++;
289 port->icount.rx++;
290 flg = TTY_NORMAL;
291
292 if (status &
293 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
294 goto handle_error;
295 if (uart_handle_sysrq_char(port, ch))
296 continue;
297#ifdef CONFIG_CONSOLE_POLL
298 if (unlikely(serial_polled)) {
299 serial_polled = 0;
300 return;
301 }
302#endif
303 error_return:
304 tty_insert_flip_char(tty, ch, flg);
305
306 } /* End while (i--) */
307
308 /* This BD is ready to be used again. Clear status. get next */
309 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
310 BD_SC_OV | BD_SC_ID);
311 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
312
313 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
314 bdp = pinfo->rx_bd_base;
315 else
316 bdp++;
317
318 } /* End for (;;) */
319
320 /* Write back buffer pointer */
321 pinfo->rx_cur = bdp;
322
323 /* activate BH processing */
324 tty_flip_buffer_push(tty);
325
326 return;
327
328 /* Error processing */
329
330 handle_error:
331 /* Statistics */
332 if (status & BD_SC_BR)
333 port->icount.brk++;
334 if (status & BD_SC_PR)
335 port->icount.parity++;
336 if (status & BD_SC_FR)
337 port->icount.frame++;
338 if (status & BD_SC_OV)
339 port->icount.overrun++;
340
341 /* Mask out ignored conditions */
342 status &= port->read_status_mask;
343
344 /* Handle the remaining ones */
345 if (status & BD_SC_BR)
346 flg = TTY_BREAK;
347 else if (status & BD_SC_PR)
348 flg = TTY_PARITY;
349 else if (status & BD_SC_FR)
350 flg = TTY_FRAME;
351
352 /* overrun does not affect the current character ! */
353 if (status & BD_SC_OV) {
354 ch = 0;
355 flg = TTY_OVERRUN;
356 /* We skip this buffer */
357 /* CHECK: Is really nothing senseful there */
358 /* ASSUMPTION: it contains nothing valid */
359 i = 0;
360 }
361#ifdef SUPPORT_SYSRQ
362 port->sysrq = 0;
363#endif
364 goto error_return;
365}
366
367/*
368 * Asynchron mode interrupt handler
369 */
370static irqreturn_t cpm_uart_int(int irq, void *data)
371{
372 u8 events;
373 struct uart_port *port = data;
374 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
375 smc_t __iomem *smcp = pinfo->smcp;
376 scc_t __iomem *sccp = pinfo->sccp;
377
378 pr_debug("CPM uart[%d]:IRQ\n", port->line);
379
380 if (IS_SMC(pinfo)) {
381 events = in_8(&smcp->smc_smce);
382 out_8(&smcp->smc_smce, events);
383 if (events & SMCM_BRKE)
384 uart_handle_break(port);
385 if (events & SMCM_RX)
386 cpm_uart_int_rx(port);
387 if (events & SMCM_TX)
388 cpm_uart_int_tx(port);
389 } else {
390 events = in_be16(&sccp->scc_scce);
391 out_be16(&sccp->scc_scce, events);
392 if (events & UART_SCCM_BRKE)
393 uart_handle_break(port);
394 if (events & UART_SCCM_RX)
395 cpm_uart_int_rx(port);
396 if (events & UART_SCCM_TX)
397 cpm_uart_int_tx(port);
398 }
399 return (events) ? IRQ_HANDLED : IRQ_NONE;
400}
401
402static int cpm_uart_startup(struct uart_port *port)
403{
404 int retval;
405 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
406
407 pr_debug("CPM uart[%d]:startup\n", port->line);
408
409 /* If the port is not the console, make sure rx is disabled. */
410 if (!(pinfo->flags & FLAG_CONSOLE)) {
411 /* Disable UART rx */
412 if (IS_SMC(pinfo)) {
413 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);
414 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
415 } else {
416 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);
417 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
418 }
419 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
420 }
421 /* Install interrupt handler. */
422 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
423 if (retval)
424 return retval;
425
426 /* Startup rx-int */
427 if (IS_SMC(pinfo)) {
428 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
429 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
430 } else {
431 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
432 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
433 }
434
435 return 0;
436}
437
438inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
439{
440 set_current_state(TASK_UNINTERRUPTIBLE);
441 schedule_timeout(pinfo->wait_closing);
442}
443
444/*
445 * Shutdown the uart
446 */
447static void cpm_uart_shutdown(struct uart_port *port)
448{
449 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
450
451 pr_debug("CPM uart[%d]:shutdown\n", port->line);
452
453 /* free interrupt handler */
454 free_irq(port->irq, port);
455
456 /* If the port is not the console, disable Rx and Tx. */
457 if (!(pinfo->flags & FLAG_CONSOLE)) {
458 /* Wait for all the BDs marked sent */
459 while(!cpm_uart_tx_empty(port)) {
460 set_current_state(TASK_UNINTERRUPTIBLE);
461 schedule_timeout(2);
462 }
463
464 if (pinfo->wait_closing)
465 cpm_uart_wait_until_send(pinfo);
466
467 /* Stop uarts */
468 if (IS_SMC(pinfo)) {
469 smc_t __iomem *smcp = pinfo->smcp;
470 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
471 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
472 } else {
473 scc_t __iomem *sccp = pinfo->sccp;
474 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
475 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
476 }
477
478 /* Shut them really down and reinit buffer descriptors */
479 if (IS_SMC(pinfo)) {
480 out_be16(&pinfo->smcup->smc_brkcr, 0);
481 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
482 } else {
483 out_be16(&pinfo->sccup->scc_brkcr, 0);
484 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
485 }
486
487 cpm_uart_initbd(pinfo);
488 }
489}
490
491static void cpm_uart_set_termios(struct uart_port *port,
492 struct ktermios *termios,
493 struct ktermios *old)
494{
495 int baud;
496 unsigned long flags;
497 u16 cval, scval, prev_mode;
498 int bits, sbits;
499 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
500 smc_t __iomem *smcp = pinfo->smcp;
501 scc_t __iomem *sccp = pinfo->sccp;
502
503 pr_debug("CPM uart[%d]:set_termios\n", port->line);
504
505 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
506 if (baud <= HW_BUF_SPD_THRESHOLD ||
507 (pinfo->port.state && pinfo->port.state->port.tty->low_latency))
508 pinfo->rx_fifosize = 1;
509 else
510 pinfo->rx_fifosize = RX_BUF_SIZE;
511
512 /* Character length programmed into the mode register is the
513 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
514 * 1 or 2 stop bits, minus 1.
515 * The value 'bits' counts this for us.
516 */
517 cval = 0;
518 scval = 0;
519
520 /* byte size */
521 switch (termios->c_cflag & CSIZE) {
522 case CS5:
523 bits = 5;
524 break;
525 case CS6:
526 bits = 6;
527 break;
528 case CS7:
529 bits = 7;
530 break;
531 case CS8:
532 bits = 8;
533 break;
534 /* Never happens, but GCC is too dumb to figure it out */
535 default:
536 bits = 8;
537 break;
538 }
539 sbits = bits - 5;
540
541 if (termios->c_cflag & CSTOPB) {
542 cval |= SMCMR_SL; /* Two stops */
543 scval |= SCU_PSMR_SL;
544 bits++;
545 }
546
547 if (termios->c_cflag & PARENB) {
548 cval |= SMCMR_PEN;
549 scval |= SCU_PSMR_PEN;
550 bits++;
551 if (!(termios->c_cflag & PARODD)) {
552 cval |= SMCMR_PM_EVEN;
553 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
554 }
555 }
556
557 /*
558 * Update the timeout
559 */
560 uart_update_timeout(port, termios->c_cflag, baud);
561
562 /*
563 * Set up parity check flag
564 */
565#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
566
567 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
568 if (termios->c_iflag & INPCK)
569 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
570 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
571 port->read_status_mask |= BD_SC_BR;
572
573 /*
574 * Characters to ignore
575 */
576 port->ignore_status_mask = 0;
577 if (termios->c_iflag & IGNPAR)
578 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
579 if (termios->c_iflag & IGNBRK) {
580 port->ignore_status_mask |= BD_SC_BR;
581 /*
582 * If we're ignore parity and break indicators, ignore
583 * overruns too. (For real raw support).
584 */
585 if (termios->c_iflag & IGNPAR)
586 port->ignore_status_mask |= BD_SC_OV;
587 }
588 /*
589 * !!! ignore all characters if CREAD is not set
590 */
591 if ((termios->c_cflag & CREAD) == 0)
592 port->read_status_mask &= ~BD_SC_EMPTY;
593
594 spin_lock_irqsave(&port->lock, flags);
595
596 /* Start bit has not been added (so don't, because we would just
597 * subtract it later), and we need to add one for the number of
598 * stops bits (there is always at least one).
599 */
600 bits++;
601 if (IS_SMC(pinfo)) {
602 /*
603 * MRBLR can be changed while an SMC/SCC is operating only
604 * if it is done in a single bus cycle with one 16-bit move
605 * (not two 8-bit bus cycles back-to-back). This occurs when
606 * the cp shifts control to the next RxBD, so the change does
607 * not take effect immediately. To guarantee the exact RxBD
608 * on which the change occurs, change MRBLR only while the
609 * SMC/SCC receiver is disabled.
610 */
611 out_be16(&pinfo->smcup->smc_mrblr, pinfo->rx_fifosize);
612
613 /* Set the mode register. We want to keep a copy of the
614 * enables, because we want to put them back if they were
615 * present.
616 */
617 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
618 /* Output in *one* operation, so we don't interrupt RX/TX if they
619 * were already enabled. */
620 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
621 SMCMR_SM_UART | prev_mode);
622 } else {
623 out_be16(&pinfo->sccup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
624 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
625 }
626
627 if (pinfo->clk)
628 clk_set_rate(pinfo->clk, baud);
629 else
630 cpm_set_brg(pinfo->brg - 1, baud);
631 spin_unlock_irqrestore(&port->lock, flags);
632}
633
634static const char *cpm_uart_type(struct uart_port *port)
635{
636 pr_debug("CPM uart[%d]:uart_type\n", port->line);
637
638 return port->type == PORT_CPM ? "CPM UART" : NULL;
639}
640
641/*
642 * verify the new serial_struct (for TIOCSSERIAL).
643 */
644static int cpm_uart_verify_port(struct uart_port *port,
645 struct serial_struct *ser)
646{
647 int ret = 0;
648
649 pr_debug("CPM uart[%d]:verify_port\n", port->line);
650
651 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
652 ret = -EINVAL;
653 if (ser->irq < 0 || ser->irq >= nr_irqs)
654 ret = -EINVAL;
655 if (ser->baud_base < 9600)
656 ret = -EINVAL;
657 return ret;
658}
659
660/*
661 * Transmit characters, refill buffer descriptor, if possible
662 */
663static int cpm_uart_tx_pump(struct uart_port *port)
664{
665 cbd_t __iomem *bdp;
666 u8 *p;
667 int count;
668 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
669 struct circ_buf *xmit = &port->state->xmit;
670
671 /* Handle xon/xoff */
672 if (port->x_char) {
673 /* Pick next descriptor and fill from buffer */
674 bdp = pinfo->tx_cur;
675
676 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
677
678 *p++ = port->x_char;
679
680 out_be16(&bdp->cbd_datlen, 1);
681 setbits16(&bdp->cbd_sc, BD_SC_READY);
682 /* Get next BD. */
683 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
684 bdp = pinfo->tx_bd_base;
685 else
686 bdp++;
687 pinfo->tx_cur = bdp;
688
689 port->icount.tx++;
690 port->x_char = 0;
691 return 1;
692 }
693
694 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
695 cpm_uart_stop_tx(port);
696 return 0;
697 }
698
699 /* Pick next descriptor and fill from buffer */
700 bdp = pinfo->tx_cur;
701
702 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
703 xmit->tail != xmit->head) {
704 count = 0;
705 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
706 while (count < pinfo->tx_fifosize) {
707 *p++ = xmit->buf[xmit->tail];
708 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
709 port->icount.tx++;
710 count++;
711 if (xmit->head == xmit->tail)
712 break;
713 }
714 out_be16(&bdp->cbd_datlen, count);
715 setbits16(&bdp->cbd_sc, BD_SC_READY);
716 /* Get next BD. */
717 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
718 bdp = pinfo->tx_bd_base;
719 else
720 bdp++;
721 }
722 pinfo->tx_cur = bdp;
723
724 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
725 uart_write_wakeup(port);
726
727 if (uart_circ_empty(xmit)) {
728 cpm_uart_stop_tx(port);
729 return 0;
730 }
731
732 return 1;
733}
734
735/*
736 * init buffer descriptors
737 */
738static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
739{
740 int i;
741 u8 *mem_addr;
742 cbd_t __iomem *bdp;
743
744 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
745
746 /* Set the physical address of the host memory
747 * buffers in the buffer descriptors, and the
748 * virtual address for us to work with.
749 */
750 mem_addr = pinfo->mem_addr;
751 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
752 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
753 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
754 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
755 mem_addr += pinfo->rx_fifosize;
756 }
757
758 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
759 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
760
761 /* Set the physical address of the host memory
762 * buffers in the buffer descriptors, and the
763 * virtual address for us to work with.
764 */
765 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
766 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
767 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
768 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
769 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
770 mem_addr += pinfo->tx_fifosize;
771 }
772
773 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
774 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
775}
776
777static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
778{
779 scc_t __iomem *scp;
780 scc_uart_t __iomem *sup;
781
782 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
783
784 scp = pinfo->sccp;
785 sup = pinfo->sccup;
786
787 /* Store address */
788 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
789 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
790 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
791 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
792
793 /* Set up the uart parameters in the
794 * parameter ram.
795 */
796
797 cpm_set_scc_fcr(sup);
798
799 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
800 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
801 out_be16(&sup->scc_brkcr, 1);
802 out_be16(&sup->scc_parec, 0);
803 out_be16(&sup->scc_frmec, 0);
804 out_be16(&sup->scc_nosec, 0);
805 out_be16(&sup->scc_brkec, 0);
806 out_be16(&sup->scc_uaddr1, 0);
807 out_be16(&sup->scc_uaddr2, 0);
808 out_be16(&sup->scc_toseq, 0);
809 out_be16(&sup->scc_char1, 0x8000);
810 out_be16(&sup->scc_char2, 0x8000);
811 out_be16(&sup->scc_char3, 0x8000);
812 out_be16(&sup->scc_char4, 0x8000);
813 out_be16(&sup->scc_char5, 0x8000);
814 out_be16(&sup->scc_char6, 0x8000);
815 out_be16(&sup->scc_char7, 0x8000);
816 out_be16(&sup->scc_char8, 0x8000);
817 out_be16(&sup->scc_rccm, 0xc0ff);
818
819 /* Send the CPM an initialize command.
820 */
821 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
822
823 /* Set UART mode, 8 bit, no parity, one stop.
824 * Enable receive and transmit.
825 */
826 out_be32(&scp->scc_gsmrh, 0);
827 out_be32(&scp->scc_gsmrl,
828 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
829
830 /* Enable rx interrupts and clear all pending events. */
831 out_be16(&scp->scc_sccm, 0);
832 out_be16(&scp->scc_scce, 0xffff);
833 out_be16(&scp->scc_dsr, 0x7e7e);
834 out_be16(&scp->scc_psmr, 0x3000);
835
836 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
837}
838
839static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
840{
841 smc_t __iomem *sp;
842 smc_uart_t __iomem *up;
843
844 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
845
846 sp = pinfo->smcp;
847 up = pinfo->smcup;
848
849 /* Store address */
850 out_be16(&pinfo->smcup->smc_rbase,
851 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
852 out_be16(&pinfo->smcup->smc_tbase,
853 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
854
855/*
856 * In case SMC1 is being relocated...
857 */
858#if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
859 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
860 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
861 out_be32(&up->smc_rstate, 0);
862 out_be32(&up->smc_tstate, 0);
863 out_be16(&up->smc_brkcr, 1); /* number of break chars */
864 out_be16(&up->smc_brkec, 0);
865#endif
866
867 /* Set up the uart parameters in the
868 * parameter ram.
869 */
870 cpm_set_smc_fcr(up);
871
872 /* Using idle character time requires some additional tuning. */
873 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
874 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
875 out_be16(&up->smc_brklen, 0);
876 out_be16(&up->smc_brkec, 0);
877 out_be16(&up->smc_brkcr, 1);
878
879 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
880
881 /* Set UART mode, 8 bit, no parity, one stop.
882 * Enable receive and transmit.
883 */
884 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
885
886 /* Enable only rx interrupts clear all pending events. */
887 out_8(&sp->smc_smcm, 0);
888 out_8(&sp->smc_smce, 0xff);
889
890 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
891}
892
893/*
894 * Initialize port. This is called from early_console stuff
895 * so we have to be careful here !
896 */
897static int cpm_uart_request_port(struct uart_port *port)
898{
899 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
900 int ret;
901
902 pr_debug("CPM uart[%d]:request port\n", port->line);
903
904 if (pinfo->flags & FLAG_CONSOLE)
905 return 0;
906
907 if (IS_SMC(pinfo)) {
908 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
909 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
910 } else {
911 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
912 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
913 }
914
915 ret = cpm_uart_allocbuf(pinfo, 0);
916
917 if (ret)
918 return ret;
919
920 cpm_uart_initbd(pinfo);
921 if (IS_SMC(pinfo))
922 cpm_uart_init_smc(pinfo);
923 else
924 cpm_uart_init_scc(pinfo);
925
926 return 0;
927}
928
929static void cpm_uart_release_port(struct uart_port *port)
930{
931 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
932
933 if (!(pinfo->flags & FLAG_CONSOLE))
934 cpm_uart_freebuf(pinfo);
935}
936
937/*
938 * Configure/autoconfigure the port.
939 */
940static void cpm_uart_config_port(struct uart_port *port, int flags)
941{
942 pr_debug("CPM uart[%d]:config_port\n", port->line);
943
944 if (flags & UART_CONFIG_TYPE) {
945 port->type = PORT_CPM;
946 cpm_uart_request_port(port);
947 }
948}
949
950#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
951/*
952 * Write a string to the serial port
953 * Note that this is called with interrupts already disabled
954 */
955static void cpm_uart_early_write(struct uart_cpm_port *pinfo,
956 const char *string, u_int count)
957{
958 unsigned int i;
959 cbd_t __iomem *bdp, *bdbase;
960 unsigned char *cpm_outp_addr;
961
962 /* Get the address of the host memory buffer.
963 */
964 bdp = pinfo->tx_cur;
965 bdbase = pinfo->tx_bd_base;
966
967 /*
968 * Now, do each character. This is not as bad as it looks
969 * since this is a holding FIFO and not a transmitting FIFO.
970 * We could add the complexity of filling the entire transmit
971 * buffer, but we would just wait longer between accesses......
972 */
973 for (i = 0; i < count; i++, string++) {
974 /* Wait for transmitter fifo to empty.
975 * Ready indicates output is ready, and xmt is doing
976 * that, not that it is ready for us to send.
977 */
978 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
979 ;
980
981 /* Send the character out.
982 * If the buffer address is in the CPM DPRAM, don't
983 * convert it.
984 */
985 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
986 pinfo);
987 *cpm_outp_addr = *string;
988
989 out_be16(&bdp->cbd_datlen, 1);
990 setbits16(&bdp->cbd_sc, BD_SC_READY);
991
992 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
993 bdp = bdbase;
994 else
995 bdp++;
996
997 /* if a LF, also do CR... */
998 if (*string == 10) {
999 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1000 ;
1001
1002 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
1003 pinfo);
1004 *cpm_outp_addr = 13;
1005
1006 out_be16(&bdp->cbd_datlen, 1);
1007 setbits16(&bdp->cbd_sc, BD_SC_READY);
1008
1009 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1010 bdp = bdbase;
1011 else
1012 bdp++;
1013 }
1014 }
1015
1016 /*
1017 * Finally, Wait for transmitter & holding register to empty
1018 * and restore the IER
1019 */
1020 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1021 ;
1022
1023 pinfo->tx_cur = bdp;
1024}
1025#endif
1026
1027#ifdef CONFIG_CONSOLE_POLL
1028/* Serial polling routines for writing and reading from the uart while
1029 * in an interrupt or debug context.
1030 */
1031
1032#define GDB_BUF_SIZE 512 /* power of 2, please */
1033
1034static char poll_buf[GDB_BUF_SIZE];
1035static char *pollp;
1036static int poll_chars;
1037
1038static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
1039{
1040 u_char c, *cp;
1041 volatile cbd_t *bdp;
1042 int i;
1043
1044 /* Get the address of the host memory buffer.
1045 */
1046 bdp = pinfo->rx_cur;
1047 while (bdp->cbd_sc & BD_SC_EMPTY)
1048 ;
1049
1050 /* If the buffer address is in the CPM DPRAM, don't
1051 * convert it.
1052 */
1053 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1054
1055 if (obuf) {
1056 i = c = bdp->cbd_datlen;
1057 while (i-- > 0)
1058 *obuf++ = *cp++;
1059 } else
1060 c = *cp;
1061 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
1062 bdp->cbd_sc |= BD_SC_EMPTY;
1063
1064 if (bdp->cbd_sc & BD_SC_WRAP)
1065 bdp = pinfo->rx_bd_base;
1066 else
1067 bdp++;
1068 pinfo->rx_cur = (cbd_t *)bdp;
1069
1070 return (int)c;
1071}
1072
1073static int cpm_get_poll_char(struct uart_port *port)
1074{
1075 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1076
1077 if (!serial_polled) {
1078 serial_polled = 1;
1079 poll_chars = 0;
1080 }
1081 if (poll_chars <= 0) {
1082 poll_chars = poll_wait_key(poll_buf, pinfo);
1083 pollp = poll_buf;
1084 }
1085 poll_chars--;
1086 return *pollp++;
1087}
1088
1089static void cpm_put_poll_char(struct uart_port *port,
1090 unsigned char c)
1091{
1092 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1093 static char ch[2];
1094
1095 ch[0] = (char)c;
1096 cpm_uart_early_write(pinfo, ch, 1);
1097}
1098#endif /* CONFIG_CONSOLE_POLL */
1099
1100static struct uart_ops cpm_uart_pops = {
1101 .tx_empty = cpm_uart_tx_empty,
1102 .set_mctrl = cpm_uart_set_mctrl,
1103 .get_mctrl = cpm_uart_get_mctrl,
1104 .stop_tx = cpm_uart_stop_tx,
1105 .start_tx = cpm_uart_start_tx,
1106 .stop_rx = cpm_uart_stop_rx,
1107 .enable_ms = cpm_uart_enable_ms,
1108 .break_ctl = cpm_uart_break_ctl,
1109 .startup = cpm_uart_startup,
1110 .shutdown = cpm_uart_shutdown,
1111 .set_termios = cpm_uart_set_termios,
1112 .type = cpm_uart_type,
1113 .release_port = cpm_uart_release_port,
1114 .request_port = cpm_uart_request_port,
1115 .config_port = cpm_uart_config_port,
1116 .verify_port = cpm_uart_verify_port,
1117#ifdef CONFIG_CONSOLE_POLL
1118 .poll_get_char = cpm_get_poll_char,
1119 .poll_put_char = cpm_put_poll_char,
1120#endif
1121};
1122
1123struct uart_cpm_port cpm_uart_ports[UART_NR];
1124
1125static int cpm_uart_init_port(struct device_node *np,
1126 struct uart_cpm_port *pinfo)
1127{
1128 const u32 *data;
1129 void __iomem *mem, *pram;
1130 int len;
1131 int ret;
1132 int i;
1133
1134 data = of_get_property(np, "clock", NULL);
1135 if (data) {
1136 struct clk *clk = clk_get(NULL, (const char*)data);
1137 if (!IS_ERR(clk))
1138 pinfo->clk = clk;
1139 }
1140 if (!pinfo->clk) {
1141 data = of_get_property(np, "fsl,cpm-brg", &len);
1142 if (!data || len != 4) {
1143 printk(KERN_ERR "CPM UART %s has no/invalid "
1144 "fsl,cpm-brg property.\n", np->name);
1145 return -EINVAL;
1146 }
1147 pinfo->brg = *data;
1148 }
1149
1150 data = of_get_property(np, "fsl,cpm-command", &len);
1151 if (!data || len != 4) {
1152 printk(KERN_ERR "CPM UART %s has no/invalid "
1153 "fsl,cpm-command property.\n", np->name);
1154 return -EINVAL;
1155 }
1156 pinfo->command = *data;
1157
1158 mem = of_iomap(np, 0);
1159 if (!mem)
1160 return -ENOMEM;
1161
1162 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1163 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1164 pinfo->sccp = mem;
1165 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1166 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1167 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1168 pinfo->flags |= FLAG_SMC;
1169 pinfo->smcp = mem;
1170 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1171 } else {
1172 ret = -ENODEV;
1173 goto out_mem;
1174 }
1175
1176 if (!pram) {
1177 ret = -ENOMEM;
1178 goto out_mem;
1179 }
1180
1181 pinfo->tx_nrfifos = TX_NUM_FIFO;
1182 pinfo->tx_fifosize = TX_BUF_SIZE;
1183 pinfo->rx_nrfifos = RX_NUM_FIFO;
1184 pinfo->rx_fifosize = RX_BUF_SIZE;
1185
1186 pinfo->port.uartclk = ppc_proc_freq;
1187 pinfo->port.mapbase = (unsigned long)mem;
1188 pinfo->port.type = PORT_CPM;
1189 pinfo->port.ops = &cpm_uart_pops,
1190 pinfo->port.iotype = UPIO_MEM;
1191 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1192 spin_lock_init(&pinfo->port.lock);
1193
1194 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1195 if (pinfo->port.irq == NO_IRQ) {
1196 ret = -EINVAL;
1197 goto out_pram;
1198 }
1199
1200 for (i = 0; i < NUM_GPIOS; i++)
1201 pinfo->gpios[i] = of_get_gpio(np, i);
1202
1203#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1204 udbg_putc = NULL;
1205#endif
1206
1207 return cpm_uart_request_port(&pinfo->port);
1208
1209out_pram:
1210 cpm_uart_unmap_pram(pinfo, pram);
1211out_mem:
1212 iounmap(mem);
1213 return ret;
1214}
1215
1216#ifdef CONFIG_SERIAL_CPM_CONSOLE
1217/*
1218 * Print a string to the serial port trying not to disturb
1219 * any possible real use of the port...
1220 *
1221 * Note that this is called with interrupts already disabled
1222 */
1223static void cpm_uart_console_write(struct console *co, const char *s,
1224 u_int count)
1225{
1226 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1227 unsigned long flags;
1228 int nolock = oops_in_progress;
1229
1230 if (unlikely(nolock)) {
1231 local_irq_save(flags);
1232 } else {
1233 spin_lock_irqsave(&pinfo->port.lock, flags);
1234 }
1235
1236 cpm_uart_early_write(pinfo, s, count);
1237
1238 if (unlikely(nolock)) {
1239 local_irq_restore(flags);
1240 } else {
1241 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1242 }
1243}
1244
1245
1246static int __init cpm_uart_console_setup(struct console *co, char *options)
1247{
1248 int baud = 38400;
1249 int bits = 8;
1250 int parity = 'n';
1251 int flow = 'n';
1252 int ret;
1253 struct uart_cpm_port *pinfo;
1254 struct uart_port *port;
1255
1256 struct device_node *np = NULL;
1257 int i = 0;
1258
1259 if (co->index >= UART_NR) {
1260 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1261 co->index);
1262 return -ENODEV;
1263 }
1264
1265 do {
1266 np = of_find_node_by_type(np, "serial");
1267 if (!np)
1268 return -ENODEV;
1269
1270 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1271 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1272 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1273 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1274 i--;
1275 } while (i++ != co->index);
1276
1277 pinfo = &cpm_uart_ports[co->index];
1278
1279 pinfo->flags |= FLAG_CONSOLE;
1280 port = &pinfo->port;
1281
1282 ret = cpm_uart_init_port(np, pinfo);
1283 of_node_put(np);
1284 if (ret)
1285 return ret;
1286
1287 if (options) {
1288 uart_parse_options(options, &baud, &parity, &bits, &flow);
1289 } else {
1290 if ((baud = uart_baudrate()) == -1)
1291 baud = 9600;
1292 }
1293
1294 if (IS_SMC(pinfo)) {
1295 out_be16(&pinfo->smcup->smc_brkcr, 0);
1296 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1297 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1298 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1299 } else {
1300 out_be16(&pinfo->sccup->scc_brkcr, 0);
1301 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
1302 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1303 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1304 }
1305
1306 ret = cpm_uart_allocbuf(pinfo, 1);
1307
1308 if (ret)
1309 return ret;
1310
1311 cpm_uart_initbd(pinfo);
1312
1313 if (IS_SMC(pinfo))
1314 cpm_uart_init_smc(pinfo);
1315 else
1316 cpm_uart_init_scc(pinfo);
1317
1318 uart_set_options(port, co, baud, parity, bits, flow);
1319 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1320
1321 return 0;
1322}
1323
1324static struct uart_driver cpm_reg;
1325static struct console cpm_scc_uart_console = {
1326 .name = "ttyCPM",
1327 .write = cpm_uart_console_write,
1328 .device = uart_console_device,
1329 .setup = cpm_uart_console_setup,
1330 .flags = CON_PRINTBUFFER,
1331 .index = -1,
1332 .data = &cpm_reg,
1333};
1334
1335static int __init cpm_uart_console_init(void)
1336{
1337 register_console(&cpm_scc_uart_console);
1338 return 0;
1339}
1340
1341console_initcall(cpm_uart_console_init);
1342
1343#define CPM_UART_CONSOLE &cpm_scc_uart_console
1344#else
1345#define CPM_UART_CONSOLE NULL
1346#endif
1347
1348static struct uart_driver cpm_reg = {
1349 .owner = THIS_MODULE,
1350 .driver_name = "ttyCPM",
1351 .dev_name = "ttyCPM",
1352 .major = SERIAL_CPM_MAJOR,
1353 .minor = SERIAL_CPM_MINOR,
1354 .cons = CPM_UART_CONSOLE,
1355 .nr = UART_NR,
1356};
1357
1358static int probe_index;
1359
1360static int __devinit cpm_uart_probe(struct platform_device *ofdev)
1361{
1362 int index = probe_index++;
1363 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1364 int ret;
1365
1366 pinfo->port.line = index;
1367
1368 if (index >= UART_NR)
1369 return -ENODEV;
1370
1371 dev_set_drvdata(&ofdev->dev, pinfo);
1372
1373 /* initialize the device pointer for the port */
1374 pinfo->port.dev = &ofdev->dev;
1375
1376 ret = cpm_uart_init_port(ofdev->dev.of_node, pinfo);
1377 if (ret)
1378 return ret;
1379
1380 return uart_add_one_port(&cpm_reg, &pinfo->port);
1381}
1382
1383static int __devexit cpm_uart_remove(struct platform_device *ofdev)
1384{
1385 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1386 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1387}
1388
1389static struct of_device_id cpm_uart_match[] = {
1390 {
1391 .compatible = "fsl,cpm1-smc-uart",
1392 },
1393 {
1394 .compatible = "fsl,cpm1-scc-uart",
1395 },
1396 {
1397 .compatible = "fsl,cpm2-smc-uart",
1398 },
1399 {
1400 .compatible = "fsl,cpm2-scc-uart",
1401 },
1402 {}
1403};
1404
1405static struct platform_driver cpm_uart_driver = {
1406 .driver = {
1407 .name = "cpm_uart",
1408 .owner = THIS_MODULE,
1409 .of_match_table = cpm_uart_match,
1410 },
1411 .probe = cpm_uart_probe,
1412 .remove = cpm_uart_remove,
1413 };
1414
1415static int __init cpm_uart_init(void)
1416{
1417 int ret = uart_register_driver(&cpm_reg);
1418 if (ret)
1419 return ret;
1420
1421 ret = platform_driver_register(&cpm_uart_driver);
1422 if (ret)
1423 uart_unregister_driver(&cpm_reg);
1424
1425 return ret;
1426}
1427
1428static void __exit cpm_uart_exit(void)
1429{
1430 platform_driver_unregister(&cpm_uart_driver);
1431 uart_unregister_driver(&cpm_reg);
1432}
1433
1434module_init(cpm_uart_init);
1435module_exit(cpm_uart_exit);
1436
1437MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1438MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1439MODULE_LICENSE("GPL");
1440MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c
new file mode 100644
index 000000000000..18f79575894a
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c
@@ -0,0 +1,136 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; CPM1 definitions
3 *
4 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
5 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
6 *
7 * Copyright (C) 2004 Freescale Semiconductor, Inc.
8 * (C) 2004 Intracom, S.A.
9 * (C) 2006 MontaVista Software, Inc.
10 * Vitaly Bordug <vbordug@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/tty.h>
30#include <linux/gfp.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/fs_pd.h>
43
44#include <linux/serial_core.h>
45#include <linux/kernel.h>
46
47#include <linux/of.h>
48
49#include "cpm_uart.h"
50
51/**************************************************************/
52
53void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
54{
55 cpm_command(port->command, cmd);
56}
57
58void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
59 struct device_node *np)
60{
61 return of_iomap(np, 1);
62}
63
64void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
65{
66 iounmap(pram);
67}
68
69/*
70 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
71 * receive buffer descriptors from dual port ram, and a character
72 * buffer area from host mem. If we are allocating for the console we need
73 * to do it from bootmem
74 */
75int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
76{
77 int dpmemsz, memsz;
78 u8 *dp_mem;
79 unsigned long dp_offset;
80 u8 *mem_addr;
81 dma_addr_t dma_addr = 0;
82
83 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
84
85 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
86 dp_offset = cpm_dpalloc(dpmemsz, 8);
87 if (IS_ERR_VALUE(dp_offset)) {
88 printk(KERN_ERR
89 "cpm_uart_cpm1.c: could not allocate buffer descriptors\n");
90 return -ENOMEM;
91 }
92 dp_mem = cpm_dpram_addr(dp_offset);
93
94 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
95 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
96 if (is_con) {
97 /* was hostalloc but changed cause it blows away the */
98 /* large tlb mapping when pinning the kernel area */
99 mem_addr = (u8 *) cpm_dpram_addr(cpm_dpalloc(memsz, 8));
100 dma_addr = (u32)cpm_dpram_phys(mem_addr);
101 } else
102 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
103 GFP_KERNEL);
104
105 if (mem_addr == NULL) {
106 cpm_dpfree(dp_offset);
107 printk(KERN_ERR
108 "cpm_uart_cpm1.c: could not allocate coherent memory\n");
109 return -ENOMEM;
110 }
111
112 pinfo->dp_addr = dp_offset;
113 pinfo->mem_addr = mem_addr; /* virtual address*/
114 pinfo->dma_addr = dma_addr; /* physical address*/
115 pinfo->mem_size = memsz;
116
117 pinfo->rx_buf = mem_addr;
118 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
119 * pinfo->rx_fifosize);
120
121 pinfo->rx_bd_base = (cbd_t __iomem __force *)dp_mem;
122 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
123
124 return 0;
125}
126
127void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
128{
129 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
130 pinfo->rx_fifosize) +
131 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
132 pinfo->tx_fifosize), pinfo->mem_addr,
133 pinfo->dma_addr);
134
135 cpm_dpfree(pinfo->dp_addr);
136}
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h
new file mode 100644
index 000000000000..60c7e94cde1e
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h
@@ -0,0 +1,32 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * definitions for cpm1
5 *
6 */
7
8#ifndef CPM_UART_CPM1_H
9#define CPM_UART_CPM1_H
10
11#include <asm/cpm1.h>
12
13static inline void cpm_set_brg(int brg, int baud)
14{
15 cpm_setbrg(brg, baud);
16}
17
18static inline void cpm_set_scc_fcr(scc_uart_t __iomem * sup)
19{
20 out_8(&sup->scc_genscc.scc_rfcr, SMC_EB);
21 out_8(&sup->scc_genscc.scc_tfcr, SMC_EB);
22}
23
24static inline void cpm_set_smc_fcr(smc_uart_t __iomem * up)
25{
26 out_8(&up->smc_rfcr, SMC_EB);
27 out_8(&up->smc_tfcr, SMC_EB);
28}
29
30#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
31
32#endif
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c
new file mode 100644
index 000000000000..a4927e66e741
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c
@@ -0,0 +1,172 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
3 *
4 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
5 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
6 *
7 * Copyright (C) 2004 Freescale Semiconductor, Inc.
8 * (C) 2004 Intracom, S.A.
9 * (C) 2006 MontaVista Software, Inc.
10 * Vitaly Bordug <vbordug@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/tty.h>
30#include <linux/ioport.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/fs_pd.h>
43#include <asm/prom.h>
44
45#include <linux/serial_core.h>
46#include <linux/kernel.h>
47
48#include "cpm_uart.h"
49
50/**************************************************************/
51
52void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
53{
54 cpm_command(port->command, cmd);
55}
56
57void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
58 struct device_node *np)
59{
60 void __iomem *pram;
61 unsigned long offset;
62 struct resource res;
63 resource_size_t len;
64
65 /* Don't remap parameter RAM if it has already been initialized
66 * during console setup.
67 */
68 if (IS_SMC(port) && port->smcup)
69 return port->smcup;
70 else if (!IS_SMC(port) && port->sccup)
71 return port->sccup;
72
73 if (of_address_to_resource(np, 1, &res))
74 return NULL;
75
76 len = resource_size(&res);
77 pram = ioremap(res.start, len);
78 if (!pram)
79 return NULL;
80
81 if (!IS_SMC(port))
82 return pram;
83
84 if (len != 2) {
85 printk(KERN_WARNING "cpm_uart[%d]: device tree references "
86 "SMC pram, using boot loader/wrapper pram mapping. "
87 "Please fix your device tree to reference the pram "
88 "base register instead.\n",
89 port->port.line);
90 return pram;
91 }
92
93 offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
94 out_be16(pram, offset);
95 iounmap(pram);
96 return cpm_muram_addr(offset);
97}
98
99void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
100{
101 if (!IS_SMC(port))
102 iounmap(pram);
103}
104
105/*
106 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
107 * receive buffer descriptors from dual port ram, and a character
108 * buffer area from host mem. If we are allocating for the console we need
109 * to do it from bootmem
110 */
111int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
112{
113 int dpmemsz, memsz;
114 u8 __iomem *dp_mem;
115 unsigned long dp_offset;
116 u8 *mem_addr;
117 dma_addr_t dma_addr = 0;
118
119 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
120
121 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
122 dp_offset = cpm_dpalloc(dpmemsz, 8);
123 if (IS_ERR_VALUE(dp_offset)) {
124 printk(KERN_ERR
125 "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
126 return -ENOMEM;
127 }
128
129 dp_mem = cpm_dpram_addr(dp_offset);
130
131 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
132 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
133 if (is_con) {
134 mem_addr = kzalloc(memsz, GFP_NOWAIT);
135 dma_addr = virt_to_bus(mem_addr);
136 }
137 else
138 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
139 GFP_KERNEL);
140
141 if (mem_addr == NULL) {
142 cpm_dpfree(dp_offset);
143 printk(KERN_ERR
144 "cpm_uart_cpm.c: could not allocate coherent memory\n");
145 return -ENOMEM;
146 }
147
148 pinfo->dp_addr = dp_offset;
149 pinfo->mem_addr = mem_addr;
150 pinfo->dma_addr = dma_addr;
151 pinfo->mem_size = memsz;
152
153 pinfo->rx_buf = mem_addr;
154 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
155 * pinfo->rx_fifosize);
156
157 pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
158 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
159
160 return 0;
161}
162
163void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
164{
165 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
166 pinfo->rx_fifosize) +
167 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
168 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
169 pinfo->dma_addr);
170
171 cpm_dpfree(pinfo->dp_addr);
172}
diff --git a/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h
new file mode 100644
index 000000000000..51e651a69938
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h
@@ -0,0 +1,32 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * definitions for cpm2
5 *
6 */
7
8#ifndef CPM_UART_CPM2_H
9#define CPM_UART_CPM2_H
10
11#include <asm/cpm2.h>
12
13static inline void cpm_set_brg(int brg, int baud)
14{
15 cpm_setbrg(brg, baud);
16}
17
18static inline void cpm_set_scc_fcr(scc_uart_t __iomem *sup)
19{
20 out_8(&sup->scc_genscc.scc_rfcr, CPMFCR_GBL | CPMFCR_EB);
21 out_8(&sup->scc_genscc.scc_tfcr, CPMFCR_GBL | CPMFCR_EB);
22}
23
24static inline void cpm_set_smc_fcr(smc_uart_t __iomem *up)
25{
26 out_8(&up->smc_rfcr, CPMFCR_GBL | CPMFCR_EB);
27 out_8(&up->smc_tfcr, CPMFCR_GBL | CPMFCR_EB);
28}
29
30#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
31
32#endif
diff --git a/drivers/tty/serial/crisv10.c b/drivers/tty/serial/crisv10.c
new file mode 100644
index 000000000000..225123b37f19
--- /dev/null
+++ b/drivers/tty/serial/crisv10.c
@@ -0,0 +1,4572 @@
1/*
2 * Serial port driver for the ETRAX 100LX chip
3 *
4 * Copyright (C) 1998-2007 Axis Communications AB
5 *
6 * Many, many authors. Based once upon a time on serial.c for 16x50.
7 *
8 */
9
10static char *serial_version = "$Revision: 1.25 $";
11
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/signal.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/interrupt.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/major.h>
21#include <linux/string.h>
22#include <linux/fcntl.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/kernel.h>
27#include <linux/mutex.h>
28#include <linux/bitops.h>
29#include <linux/seq_file.h>
30#include <linux/delay.h>
31#include <linux/module.h>
32#include <linux/uaccess.h>
33#include <linux/io.h>
34
35#include <asm/irq.h>
36#include <asm/dma.h>
37#include <asm/system.h>
38
39#include <arch/svinto.h>
40
41/* non-arch dependent serial structures are in linux/serial.h */
42#include <linux/serial.h>
43/* while we keep our own stuff (struct e100_serial) in a local .h file */
44#include "crisv10.h"
45#include <asm/fasttimer.h>
46#include <arch/io_interface_mux.h>
47
48#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
49#ifndef CONFIG_ETRAX_FAST_TIMER
50#error "Enable FAST_TIMER to use SERIAL_FAST_TIMER"
51#endif
52#endif
53
54#if defined(CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS) && \
55 (CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS == 0)
56#error "RX_TIMEOUT_TICKS == 0 not allowed, use 1"
57#endif
58
59#if defined(CONFIG_ETRAX_RS485_ON_PA) && defined(CONFIG_ETRAX_RS485_ON_PORT_G)
60#error "Disable either CONFIG_ETRAX_RS485_ON_PA or CONFIG_ETRAX_RS485_ON_PORT_G"
61#endif
62
63/*
64 * All of the compatibilty code so we can compile serial.c against
65 * older kernels is hidden in serial_compat.h
66 */
67#if defined(LOCAL_HEADERS)
68#include "serial_compat.h"
69#endif
70
71struct tty_driver *serial_driver;
72
73/* number of characters left in xmit buffer before we ask for more */
74#define WAKEUP_CHARS 256
75
76//#define SERIAL_DEBUG_INTR
77//#define SERIAL_DEBUG_OPEN
78//#define SERIAL_DEBUG_FLOW
79//#define SERIAL_DEBUG_DATA
80//#define SERIAL_DEBUG_THROTTLE
81//#define SERIAL_DEBUG_IO /* Debug for Extra control and status pins */
82//#define SERIAL_DEBUG_LINE 0 /* What serport we want to debug */
83
84/* Enable this to use serial interrupts to handle when you
85 expect the first received event on the serial port to
86 be an error, break or similar. Used to be able to flash IRMA
87 from eLinux */
88#define SERIAL_HANDLE_EARLY_ERRORS
89
90/* Currently 16 descriptors x 128 bytes = 2048 bytes */
91#define SERIAL_DESCR_BUF_SIZE 256
92
93#define SERIAL_PRESCALE_BASE 3125000 /* 3.125MHz */
94#define DEF_BAUD_BASE SERIAL_PRESCALE_BASE
95
96/* We don't want to load the system with massive fast timer interrupt
97 * on high baudrates so limit it to 250 us (4kHz) */
98#define MIN_FLUSH_TIME_USEC 250
99
100/* Add an x here to log a lot of timer stuff */
101#define TIMERD(x)
102/* Debug details of interrupt handling */
103#define DINTR1(x) /* irq on/off, errors */
104#define DINTR2(x) /* tx and rx */
105/* Debug flip buffer stuff */
106#define DFLIP(x)
107/* Debug flow control and overview of data flow */
108#define DFLOW(x)
109#define DBAUD(x)
110#define DLOG_INT_TRIG(x)
111
112//#define DEBUG_LOG_INCLUDED
113#ifndef DEBUG_LOG_INCLUDED
114#define DEBUG_LOG(line, string, value)
115#else
116struct debug_log_info
117{
118 unsigned long time;
119 unsigned long timer_data;
120// int line;
121 const char *string;
122 int value;
123};
124#define DEBUG_LOG_SIZE 4096
125
126struct debug_log_info debug_log[DEBUG_LOG_SIZE];
127int debug_log_pos = 0;
128
129#define DEBUG_LOG(_line, _string, _value) do { \
130 if ((_line) == SERIAL_DEBUG_LINE) {\
131 debug_log_func(_line, _string, _value); \
132 }\
133}while(0)
134
135void debug_log_func(int line, const char *string, int value)
136{
137 if (debug_log_pos < DEBUG_LOG_SIZE) {
138 debug_log[debug_log_pos].time = jiffies;
139 debug_log[debug_log_pos].timer_data = *R_TIMER_DATA;
140// debug_log[debug_log_pos].line = line;
141 debug_log[debug_log_pos].string = string;
142 debug_log[debug_log_pos].value = value;
143 debug_log_pos++;
144 }
145 /*printk(string, value);*/
146}
147#endif
148
149#ifndef CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS
150/* Default number of timer ticks before flushing rx fifo
151 * When using "little data, low latency applications: use 0
152 * When using "much data applications (PPP)" use ~5
153 */
154#define CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS 5
155#endif
156
157unsigned long timer_data_to_ns(unsigned long timer_data);
158
159static void change_speed(struct e100_serial *info);
160static void rs_throttle(struct tty_struct * tty);
161static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
162static int rs_write(struct tty_struct *tty,
163 const unsigned char *buf, int count);
164#ifdef CONFIG_ETRAX_RS485
165static int e100_write_rs485(struct tty_struct *tty,
166 const unsigned char *buf, int count);
167#endif
168static int get_lsr_info(struct e100_serial *info, unsigned int *value);
169
170
171#define DEF_BAUD 115200 /* 115.2 kbit/s */
172#define STD_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
173#define DEF_RX 0x20 /* or SERIAL_CTRL_W >> 8 */
174/* Default value of tx_ctrl register: has txd(bit 7)=1 (idle) as default */
175#define DEF_TX 0x80 /* or SERIAL_CTRL_B */
176
177/* offsets from R_SERIALx_CTRL */
178
179#define REG_DATA 0
180#define REG_DATA_STATUS32 0 /* this is the 32 bit register R_SERIALx_READ */
181#define REG_TR_DATA 0
182#define REG_STATUS 1
183#define REG_TR_CTRL 1
184#define REG_REC_CTRL 2
185#define REG_BAUD 3
186#define REG_XOFF 4 /* this is a 32 bit register */
187
188/* The bitfields are the same for all serial ports */
189#define SER_RXD_MASK IO_MASK(R_SERIAL0_STATUS, rxd)
190#define SER_DATA_AVAIL_MASK IO_MASK(R_SERIAL0_STATUS, data_avail)
191#define SER_FRAMING_ERR_MASK IO_MASK(R_SERIAL0_STATUS, framing_err)
192#define SER_PAR_ERR_MASK IO_MASK(R_SERIAL0_STATUS, par_err)
193#define SER_OVERRUN_MASK IO_MASK(R_SERIAL0_STATUS, overrun)
194
195#define SER_ERROR_MASK (SER_OVERRUN_MASK | SER_PAR_ERR_MASK | SER_FRAMING_ERR_MASK)
196
197/* Values for info->errorcode */
198#define ERRCODE_SET_BREAK (TTY_BREAK)
199#define ERRCODE_INSERT 0x100
200#define ERRCODE_INSERT_BREAK (ERRCODE_INSERT | TTY_BREAK)
201
202#define FORCE_EOP(info) *R_SET_EOP = 1U << info->iseteop;
203
204/*
205 * General note regarding the use of IO_* macros in this file:
206 *
207 * We will use the bits defined for DMA channel 6 when using various
208 * IO_* macros (e.g. IO_STATE, IO_MASK, IO_EXTRACT) and _assume_ they are
209 * the same for all channels (which of course they are).
210 *
211 * We will also use the bits defined for serial port 0 when writing commands
212 * to the different ports, as these bits too are the same for all ports.
213 */
214
215
216/* Mask for the irqs possibly enabled in R_IRQ_MASK1_RD etc. */
217static const unsigned long e100_ser_int_mask = 0
218#ifdef CONFIG_ETRAX_SERIAL_PORT0
219| IO_MASK(R_IRQ_MASK1_RD, ser0_data) | IO_MASK(R_IRQ_MASK1_RD, ser0_ready)
220#endif
221#ifdef CONFIG_ETRAX_SERIAL_PORT1
222| IO_MASK(R_IRQ_MASK1_RD, ser1_data) | IO_MASK(R_IRQ_MASK1_RD, ser1_ready)
223#endif
224#ifdef CONFIG_ETRAX_SERIAL_PORT2
225| IO_MASK(R_IRQ_MASK1_RD, ser2_data) | IO_MASK(R_IRQ_MASK1_RD, ser2_ready)
226#endif
227#ifdef CONFIG_ETRAX_SERIAL_PORT3
228| IO_MASK(R_IRQ_MASK1_RD, ser3_data) | IO_MASK(R_IRQ_MASK1_RD, ser3_ready)
229#endif
230;
231unsigned long r_alt_ser_baudrate_shadow = 0;
232
233/* this is the data for the four serial ports in the etrax100 */
234/* DMA2(ser2), DMA4(ser3), DMA6(ser0) or DMA8(ser1) */
235/* R_DMA_CHx_CLR_INTR, R_DMA_CHx_FIRST, R_DMA_CHx_CMD */
236
237static struct e100_serial rs_table[] = {
238 { .baud = DEF_BAUD,
239 .ioport = (unsigned char *)R_SERIAL0_CTRL,
240 .irq = 1U << 12, /* uses DMA 6 and 7 */
241 .oclrintradr = R_DMA_CH6_CLR_INTR,
242 .ofirstadr = R_DMA_CH6_FIRST,
243 .ocmdadr = R_DMA_CH6_CMD,
244 .ostatusadr = R_DMA_CH6_STATUS,
245 .iclrintradr = R_DMA_CH7_CLR_INTR,
246 .ifirstadr = R_DMA_CH7_FIRST,
247 .icmdadr = R_DMA_CH7_CMD,
248 .idescradr = R_DMA_CH7_DESCR,
249 .flags = STD_FLAGS,
250 .rx_ctrl = DEF_RX,
251 .tx_ctrl = DEF_TX,
252 .iseteop = 2,
253 .dma_owner = dma_ser0,
254 .io_if = if_serial_0,
255#ifdef CONFIG_ETRAX_SERIAL_PORT0
256 .enabled = 1,
257#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA6_OUT
258 .dma_out_enabled = 1,
259 .dma_out_nbr = SER0_TX_DMA_NBR,
260 .dma_out_irq_nbr = SER0_DMA_TX_IRQ_NBR,
261 .dma_out_irq_flags = IRQF_DISABLED,
262 .dma_out_irq_description = "serial 0 dma tr",
263#else
264 .dma_out_enabled = 0,
265 .dma_out_nbr = UINT_MAX,
266 .dma_out_irq_nbr = 0,
267 .dma_out_irq_flags = 0,
268 .dma_out_irq_description = NULL,
269#endif
270#ifdef CONFIG_ETRAX_SERIAL_PORT0_DMA7_IN
271 .dma_in_enabled = 1,
272 .dma_in_nbr = SER0_RX_DMA_NBR,
273 .dma_in_irq_nbr = SER0_DMA_RX_IRQ_NBR,
274 .dma_in_irq_flags = IRQF_DISABLED,
275 .dma_in_irq_description = "serial 0 dma rec",
276#else
277 .dma_in_enabled = 0,
278 .dma_in_nbr = UINT_MAX,
279 .dma_in_irq_nbr = 0,
280 .dma_in_irq_flags = 0,
281 .dma_in_irq_description = NULL,
282#endif
283#else
284 .enabled = 0,
285 .io_if_description = NULL,
286 .dma_out_enabled = 0,
287 .dma_in_enabled = 0
288#endif
289
290}, /* ttyS0 */
291#ifndef CONFIG_SVINTO_SIM
292 { .baud = DEF_BAUD,
293 .ioport = (unsigned char *)R_SERIAL1_CTRL,
294 .irq = 1U << 16, /* uses DMA 8 and 9 */
295 .oclrintradr = R_DMA_CH8_CLR_INTR,
296 .ofirstadr = R_DMA_CH8_FIRST,
297 .ocmdadr = R_DMA_CH8_CMD,
298 .ostatusadr = R_DMA_CH8_STATUS,
299 .iclrintradr = R_DMA_CH9_CLR_INTR,
300 .ifirstadr = R_DMA_CH9_FIRST,
301 .icmdadr = R_DMA_CH9_CMD,
302 .idescradr = R_DMA_CH9_DESCR,
303 .flags = STD_FLAGS,
304 .rx_ctrl = DEF_RX,
305 .tx_ctrl = DEF_TX,
306 .iseteop = 3,
307 .dma_owner = dma_ser1,
308 .io_if = if_serial_1,
309#ifdef CONFIG_ETRAX_SERIAL_PORT1
310 .enabled = 1,
311 .io_if_description = "ser1",
312#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA8_OUT
313 .dma_out_enabled = 1,
314 .dma_out_nbr = SER1_TX_DMA_NBR,
315 .dma_out_irq_nbr = SER1_DMA_TX_IRQ_NBR,
316 .dma_out_irq_flags = IRQF_DISABLED,
317 .dma_out_irq_description = "serial 1 dma tr",
318#else
319 .dma_out_enabled = 0,
320 .dma_out_nbr = UINT_MAX,
321 .dma_out_irq_nbr = 0,
322 .dma_out_irq_flags = 0,
323 .dma_out_irq_description = NULL,
324#endif
325#ifdef CONFIG_ETRAX_SERIAL_PORT1_DMA9_IN
326 .dma_in_enabled = 1,
327 .dma_in_nbr = SER1_RX_DMA_NBR,
328 .dma_in_irq_nbr = SER1_DMA_RX_IRQ_NBR,
329 .dma_in_irq_flags = IRQF_DISABLED,
330 .dma_in_irq_description = "serial 1 dma rec",
331#else
332 .dma_in_enabled = 0,
333 .dma_in_enabled = 0,
334 .dma_in_nbr = UINT_MAX,
335 .dma_in_irq_nbr = 0,
336 .dma_in_irq_flags = 0,
337 .dma_in_irq_description = NULL,
338#endif
339#else
340 .enabled = 0,
341 .io_if_description = NULL,
342 .dma_in_irq_nbr = 0,
343 .dma_out_enabled = 0,
344 .dma_in_enabled = 0
345#endif
346}, /* ttyS1 */
347
348 { .baud = DEF_BAUD,
349 .ioport = (unsigned char *)R_SERIAL2_CTRL,
350 .irq = 1U << 4, /* uses DMA 2 and 3 */
351 .oclrintradr = R_DMA_CH2_CLR_INTR,
352 .ofirstadr = R_DMA_CH2_FIRST,
353 .ocmdadr = R_DMA_CH2_CMD,
354 .ostatusadr = R_DMA_CH2_STATUS,
355 .iclrintradr = R_DMA_CH3_CLR_INTR,
356 .ifirstadr = R_DMA_CH3_FIRST,
357 .icmdadr = R_DMA_CH3_CMD,
358 .idescradr = R_DMA_CH3_DESCR,
359 .flags = STD_FLAGS,
360 .rx_ctrl = DEF_RX,
361 .tx_ctrl = DEF_TX,
362 .iseteop = 0,
363 .dma_owner = dma_ser2,
364 .io_if = if_serial_2,
365#ifdef CONFIG_ETRAX_SERIAL_PORT2
366 .enabled = 1,
367 .io_if_description = "ser2",
368#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA2_OUT
369 .dma_out_enabled = 1,
370 .dma_out_nbr = SER2_TX_DMA_NBR,
371 .dma_out_irq_nbr = SER2_DMA_TX_IRQ_NBR,
372 .dma_out_irq_flags = IRQF_DISABLED,
373 .dma_out_irq_description = "serial 2 dma tr",
374#else
375 .dma_out_enabled = 0,
376 .dma_out_nbr = UINT_MAX,
377 .dma_out_irq_nbr = 0,
378 .dma_out_irq_flags = 0,
379 .dma_out_irq_description = NULL,
380#endif
381#ifdef CONFIG_ETRAX_SERIAL_PORT2_DMA3_IN
382 .dma_in_enabled = 1,
383 .dma_in_nbr = SER2_RX_DMA_NBR,
384 .dma_in_irq_nbr = SER2_DMA_RX_IRQ_NBR,
385 .dma_in_irq_flags = IRQF_DISABLED,
386 .dma_in_irq_description = "serial 2 dma rec",
387#else
388 .dma_in_enabled = 0,
389 .dma_in_nbr = UINT_MAX,
390 .dma_in_irq_nbr = 0,
391 .dma_in_irq_flags = 0,
392 .dma_in_irq_description = NULL,
393#endif
394#else
395 .enabled = 0,
396 .io_if_description = NULL,
397 .dma_out_enabled = 0,
398 .dma_in_enabled = 0
399#endif
400 }, /* ttyS2 */
401
402 { .baud = DEF_BAUD,
403 .ioport = (unsigned char *)R_SERIAL3_CTRL,
404 .irq = 1U << 8, /* uses DMA 4 and 5 */
405 .oclrintradr = R_DMA_CH4_CLR_INTR,
406 .ofirstadr = R_DMA_CH4_FIRST,
407 .ocmdadr = R_DMA_CH4_CMD,
408 .ostatusadr = R_DMA_CH4_STATUS,
409 .iclrintradr = R_DMA_CH5_CLR_INTR,
410 .ifirstadr = R_DMA_CH5_FIRST,
411 .icmdadr = R_DMA_CH5_CMD,
412 .idescradr = R_DMA_CH5_DESCR,
413 .flags = STD_FLAGS,
414 .rx_ctrl = DEF_RX,
415 .tx_ctrl = DEF_TX,
416 .iseteop = 1,
417 .dma_owner = dma_ser3,
418 .io_if = if_serial_3,
419#ifdef CONFIG_ETRAX_SERIAL_PORT3
420 .enabled = 1,
421 .io_if_description = "ser3",
422#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA4_OUT
423 .dma_out_enabled = 1,
424 .dma_out_nbr = SER3_TX_DMA_NBR,
425 .dma_out_irq_nbr = SER3_DMA_TX_IRQ_NBR,
426 .dma_out_irq_flags = IRQF_DISABLED,
427 .dma_out_irq_description = "serial 3 dma tr",
428#else
429 .dma_out_enabled = 0,
430 .dma_out_nbr = UINT_MAX,
431 .dma_out_irq_nbr = 0,
432 .dma_out_irq_flags = 0,
433 .dma_out_irq_description = NULL,
434#endif
435#ifdef CONFIG_ETRAX_SERIAL_PORT3_DMA5_IN
436 .dma_in_enabled = 1,
437 .dma_in_nbr = SER3_RX_DMA_NBR,
438 .dma_in_irq_nbr = SER3_DMA_RX_IRQ_NBR,
439 .dma_in_irq_flags = IRQF_DISABLED,
440 .dma_in_irq_description = "serial 3 dma rec",
441#else
442 .dma_in_enabled = 0,
443 .dma_in_nbr = UINT_MAX,
444 .dma_in_irq_nbr = 0,
445 .dma_in_irq_flags = 0,
446 .dma_in_irq_description = NULL
447#endif
448#else
449 .enabled = 0,
450 .io_if_description = NULL,
451 .dma_out_enabled = 0,
452 .dma_in_enabled = 0
453#endif
454 } /* ttyS3 */
455#endif
456};
457
458
459#define NR_PORTS (sizeof(rs_table)/sizeof(struct e100_serial))
460
461#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
462static struct fast_timer fast_timers[NR_PORTS];
463#endif
464
465#ifdef CONFIG_ETRAX_SERIAL_PROC_ENTRY
466#define PROCSTAT(x) x
467struct ser_statistics_type {
468 int overrun_cnt;
469 int early_errors_cnt;
470 int ser_ints_ok_cnt;
471 int errors_cnt;
472 unsigned long int processing_flip;
473 unsigned long processing_flip_still_room;
474 unsigned long int timeout_flush_cnt;
475 int rx_dma_ints;
476 int tx_dma_ints;
477 int rx_tot;
478 int tx_tot;
479};
480
481static struct ser_statistics_type ser_stat[NR_PORTS];
482
483#else
484
485#define PROCSTAT(x)
486
487#endif /* CONFIG_ETRAX_SERIAL_PROC_ENTRY */
488
489/* RS-485 */
490#if defined(CONFIG_ETRAX_RS485)
491#ifdef CONFIG_ETRAX_FAST_TIMER
492static struct fast_timer fast_timers_rs485[NR_PORTS];
493#endif
494#if defined(CONFIG_ETRAX_RS485_ON_PA)
495static int rs485_pa_bit = CONFIG_ETRAX_RS485_ON_PA_BIT;
496#endif
497#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
498static int rs485_port_g_bit = CONFIG_ETRAX_RS485_ON_PORT_G_BIT;
499#endif
500#endif
501
502/* Info and macros needed for each ports extra control/status signals. */
503#define E100_STRUCT_PORT(line, pinname) \
504 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
505 (R_PORT_PA_DATA): ( \
506 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
507 (R_PORT_PB_DATA):&dummy_ser[line]))
508
509#define E100_STRUCT_SHADOW(line, pinname) \
510 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
511 (&port_pa_data_shadow): ( \
512 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
513 (&port_pb_data_shadow):&dummy_ser[line]))
514#define E100_STRUCT_MASK(line, pinname) \
515 ((CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT >= 0)? \
516 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PA_BIT): ( \
517 (CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT >= 0)? \
518 (1<<CONFIG_ETRAX_SER##line##_##pinname##_ON_PB_BIT):DUMMY_##pinname##_MASK))
519
520#define DUMMY_DTR_MASK 1
521#define DUMMY_RI_MASK 2
522#define DUMMY_DSR_MASK 4
523#define DUMMY_CD_MASK 8
524static unsigned char dummy_ser[NR_PORTS] = {0xFF, 0xFF, 0xFF,0xFF};
525
526/* If not all status pins are used or disabled, use mixed mode */
527#ifdef CONFIG_ETRAX_SERIAL_PORT0
528
529#define SER0_PA_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PA_BIT+CONFIG_ETRAX_SER0_RI_ON_PA_BIT+CONFIG_ETRAX_SER0_DSR_ON_PA_BIT+CONFIG_ETRAX_SER0_CD_ON_PA_BIT)
530
531#if SER0_PA_BITSUM != -4
532# if CONFIG_ETRAX_SER0_DTR_ON_PA_BIT == -1
533# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
534# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
535# endif
536# endif
537# if CONFIG_ETRAX_SER0_RI_ON_PA_BIT == -1
538# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
539# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
540# endif
541# endif
542# if CONFIG_ETRAX_SER0_DSR_ON_PA_BIT == -1
543# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
544# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
545# endif
546# endif
547# if CONFIG_ETRAX_SER0_CD_ON_PA_BIT == -1
548# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
549# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
550# endif
551# endif
552#endif
553
554#define SER0_PB_BITSUM (CONFIG_ETRAX_SER0_DTR_ON_PB_BIT+CONFIG_ETRAX_SER0_RI_ON_PB_BIT+CONFIG_ETRAX_SER0_DSR_ON_PB_BIT+CONFIG_ETRAX_SER0_CD_ON_PB_BIT)
555
556#if SER0_PB_BITSUM != -4
557# if CONFIG_ETRAX_SER0_DTR_ON_PB_BIT == -1
558# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
559# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
560# endif
561# endif
562# if CONFIG_ETRAX_SER0_RI_ON_PB_BIT == -1
563# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
564# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
565# endif
566# endif
567# if CONFIG_ETRAX_SER0_DSR_ON_PB_BIT == -1
568# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
569# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
570# endif
571# endif
572# if CONFIG_ETRAX_SER0_CD_ON_PB_BIT == -1
573# ifndef CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED
574# define CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED 1
575# endif
576# endif
577#endif
578
579#endif /* PORT0 */
580
581
582#ifdef CONFIG_ETRAX_SERIAL_PORT1
583
584#define SER1_PA_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PA_BIT+CONFIG_ETRAX_SER1_RI_ON_PA_BIT+CONFIG_ETRAX_SER1_DSR_ON_PA_BIT+CONFIG_ETRAX_SER1_CD_ON_PA_BIT)
585
586#if SER1_PA_BITSUM != -4
587# if CONFIG_ETRAX_SER1_DTR_ON_PA_BIT == -1
588# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
589# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
590# endif
591# endif
592# if CONFIG_ETRAX_SER1_RI_ON_PA_BIT == -1
593# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
594# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
595# endif
596# endif
597# if CONFIG_ETRAX_SER1_DSR_ON_PA_BIT == -1
598# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
599# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
600# endif
601# endif
602# if CONFIG_ETRAX_SER1_CD_ON_PA_BIT == -1
603# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
604# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
605# endif
606# endif
607#endif
608
609#define SER1_PB_BITSUM (CONFIG_ETRAX_SER1_DTR_ON_PB_BIT+CONFIG_ETRAX_SER1_RI_ON_PB_BIT+CONFIG_ETRAX_SER1_DSR_ON_PB_BIT+CONFIG_ETRAX_SER1_CD_ON_PB_BIT)
610
611#if SER1_PB_BITSUM != -4
612# if CONFIG_ETRAX_SER1_DTR_ON_PB_BIT == -1
613# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
614# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
615# endif
616# endif
617# if CONFIG_ETRAX_SER1_RI_ON_PB_BIT == -1
618# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
619# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
620# endif
621# endif
622# if CONFIG_ETRAX_SER1_DSR_ON_PB_BIT == -1
623# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
624# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
625# endif
626# endif
627# if CONFIG_ETRAX_SER1_CD_ON_PB_BIT == -1
628# ifndef CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED
629# define CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED 1
630# endif
631# endif
632#endif
633
634#endif /* PORT1 */
635
636#ifdef CONFIG_ETRAX_SERIAL_PORT2
637
638#define SER2_PA_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PA_BIT+CONFIG_ETRAX_SER2_RI_ON_PA_BIT+CONFIG_ETRAX_SER2_DSR_ON_PA_BIT+CONFIG_ETRAX_SER2_CD_ON_PA_BIT)
639
640#if SER2_PA_BITSUM != -4
641# if CONFIG_ETRAX_SER2_DTR_ON_PA_BIT == -1
642# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
643# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
644# endif
645# endif
646# if CONFIG_ETRAX_SER2_RI_ON_PA_BIT == -1
647# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
648# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
649# endif
650# endif
651# if CONFIG_ETRAX_SER2_DSR_ON_PA_BIT == -1
652# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
653# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
654# endif
655# endif
656# if CONFIG_ETRAX_SER2_CD_ON_PA_BIT == -1
657# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
658# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
659# endif
660# endif
661#endif
662
663#define SER2_PB_BITSUM (CONFIG_ETRAX_SER2_DTR_ON_PB_BIT+CONFIG_ETRAX_SER2_RI_ON_PB_BIT+CONFIG_ETRAX_SER2_DSR_ON_PB_BIT+CONFIG_ETRAX_SER2_CD_ON_PB_BIT)
664
665#if SER2_PB_BITSUM != -4
666# if CONFIG_ETRAX_SER2_DTR_ON_PB_BIT == -1
667# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
668# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
669# endif
670# endif
671# if CONFIG_ETRAX_SER2_RI_ON_PB_BIT == -1
672# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
673# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
674# endif
675# endif
676# if CONFIG_ETRAX_SER2_DSR_ON_PB_BIT == -1
677# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
678# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
679# endif
680# endif
681# if CONFIG_ETRAX_SER2_CD_ON_PB_BIT == -1
682# ifndef CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED
683# define CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED 1
684# endif
685# endif
686#endif
687
688#endif /* PORT2 */
689
690#ifdef CONFIG_ETRAX_SERIAL_PORT3
691
692#define SER3_PA_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PA_BIT+CONFIG_ETRAX_SER3_RI_ON_PA_BIT+CONFIG_ETRAX_SER3_DSR_ON_PA_BIT+CONFIG_ETRAX_SER3_CD_ON_PA_BIT)
693
694#if SER3_PA_BITSUM != -4
695# if CONFIG_ETRAX_SER3_DTR_ON_PA_BIT == -1
696# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
697# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
698# endif
699# endif
700# if CONFIG_ETRAX_SER3_RI_ON_PA_BIT == -1
701# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
702# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
703# endif
704# endif
705# if CONFIG_ETRAX_SER3_DSR_ON_PA_BIT == -1
706# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
707# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
708# endif
709# endif
710# if CONFIG_ETRAX_SER3_CD_ON_PA_BIT == -1
711# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
712# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
713# endif
714# endif
715#endif
716
717#define SER3_PB_BITSUM (CONFIG_ETRAX_SER3_DTR_ON_PB_BIT+CONFIG_ETRAX_SER3_RI_ON_PB_BIT+CONFIG_ETRAX_SER3_DSR_ON_PB_BIT+CONFIG_ETRAX_SER3_CD_ON_PB_BIT)
718
719#if SER3_PB_BITSUM != -4
720# if CONFIG_ETRAX_SER3_DTR_ON_PB_BIT == -1
721# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
722# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
723# endif
724# endif
725# if CONFIG_ETRAX_SER3_RI_ON_PB_BIT == -1
726# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
727# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
728# endif
729# endif
730# if CONFIG_ETRAX_SER3_DSR_ON_PB_BIT == -1
731# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
732# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
733# endif
734# endif
735# if CONFIG_ETRAX_SER3_CD_ON_PB_BIT == -1
736# ifndef CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED
737# define CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED 1
738# endif
739# endif
740#endif
741
742#endif /* PORT3 */
743
744
745#if defined(CONFIG_ETRAX_SER0_DTR_RI_DSR_CD_MIXED) || \
746 defined(CONFIG_ETRAX_SER1_DTR_RI_DSR_CD_MIXED) || \
747 defined(CONFIG_ETRAX_SER2_DTR_RI_DSR_CD_MIXED) || \
748 defined(CONFIG_ETRAX_SER3_DTR_RI_DSR_CD_MIXED)
749#define CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
750#endif
751
752#ifdef CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED
753/* The pins can be mixed on PA and PB */
754#define CONTROL_PINS_PORT_NOT_USED(line) \
755 &dummy_ser[line], &dummy_ser[line], \
756 &dummy_ser[line], &dummy_ser[line], \
757 &dummy_ser[line], &dummy_ser[line], \
758 &dummy_ser[line], &dummy_ser[line], \
759 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
760
761
762struct control_pins
763{
764 volatile unsigned char *dtr_port;
765 unsigned char *dtr_shadow;
766 volatile unsigned char *ri_port;
767 unsigned char *ri_shadow;
768 volatile unsigned char *dsr_port;
769 unsigned char *dsr_shadow;
770 volatile unsigned char *cd_port;
771 unsigned char *cd_shadow;
772
773 unsigned char dtr_mask;
774 unsigned char ri_mask;
775 unsigned char dsr_mask;
776 unsigned char cd_mask;
777};
778
779static const struct control_pins e100_modem_pins[NR_PORTS] =
780{
781 /* Ser 0 */
782 {
783#ifdef CONFIG_ETRAX_SERIAL_PORT0
784 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
785 E100_STRUCT_PORT(0,RI), E100_STRUCT_SHADOW(0,RI),
786 E100_STRUCT_PORT(0,DSR), E100_STRUCT_SHADOW(0,DSR),
787 E100_STRUCT_PORT(0,CD), E100_STRUCT_SHADOW(0,CD),
788 E100_STRUCT_MASK(0,DTR),
789 E100_STRUCT_MASK(0,RI),
790 E100_STRUCT_MASK(0,DSR),
791 E100_STRUCT_MASK(0,CD)
792#else
793 CONTROL_PINS_PORT_NOT_USED(0)
794#endif
795 },
796
797 /* Ser 1 */
798 {
799#ifdef CONFIG_ETRAX_SERIAL_PORT1
800 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
801 E100_STRUCT_PORT(1,RI), E100_STRUCT_SHADOW(1,RI),
802 E100_STRUCT_PORT(1,DSR), E100_STRUCT_SHADOW(1,DSR),
803 E100_STRUCT_PORT(1,CD), E100_STRUCT_SHADOW(1,CD),
804 E100_STRUCT_MASK(1,DTR),
805 E100_STRUCT_MASK(1,RI),
806 E100_STRUCT_MASK(1,DSR),
807 E100_STRUCT_MASK(1,CD)
808#else
809 CONTROL_PINS_PORT_NOT_USED(1)
810#endif
811 },
812
813 /* Ser 2 */
814 {
815#ifdef CONFIG_ETRAX_SERIAL_PORT2
816 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
817 E100_STRUCT_PORT(2,RI), E100_STRUCT_SHADOW(2,RI),
818 E100_STRUCT_PORT(2,DSR), E100_STRUCT_SHADOW(2,DSR),
819 E100_STRUCT_PORT(2,CD), E100_STRUCT_SHADOW(2,CD),
820 E100_STRUCT_MASK(2,DTR),
821 E100_STRUCT_MASK(2,RI),
822 E100_STRUCT_MASK(2,DSR),
823 E100_STRUCT_MASK(2,CD)
824#else
825 CONTROL_PINS_PORT_NOT_USED(2)
826#endif
827 },
828
829 /* Ser 3 */
830 {
831#ifdef CONFIG_ETRAX_SERIAL_PORT3
832 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
833 E100_STRUCT_PORT(3,RI), E100_STRUCT_SHADOW(3,RI),
834 E100_STRUCT_PORT(3,DSR), E100_STRUCT_SHADOW(3,DSR),
835 E100_STRUCT_PORT(3,CD), E100_STRUCT_SHADOW(3,CD),
836 E100_STRUCT_MASK(3,DTR),
837 E100_STRUCT_MASK(3,RI),
838 E100_STRUCT_MASK(3,DSR),
839 E100_STRUCT_MASK(3,CD)
840#else
841 CONTROL_PINS_PORT_NOT_USED(3)
842#endif
843 }
844};
845#else /* CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
846
847/* All pins are on either PA or PB for each serial port */
848#define CONTROL_PINS_PORT_NOT_USED(line) \
849 &dummy_ser[line], &dummy_ser[line], \
850 DUMMY_DTR_MASK, DUMMY_RI_MASK, DUMMY_DSR_MASK, DUMMY_CD_MASK
851
852
853struct control_pins
854{
855 volatile unsigned char *port;
856 unsigned char *shadow;
857
858 unsigned char dtr_mask;
859 unsigned char ri_mask;
860 unsigned char dsr_mask;
861 unsigned char cd_mask;
862};
863
864#define dtr_port port
865#define dtr_shadow shadow
866#define ri_port port
867#define ri_shadow shadow
868#define dsr_port port
869#define dsr_shadow shadow
870#define cd_port port
871#define cd_shadow shadow
872
873static const struct control_pins e100_modem_pins[NR_PORTS] =
874{
875 /* Ser 0 */
876 {
877#ifdef CONFIG_ETRAX_SERIAL_PORT0
878 E100_STRUCT_PORT(0,DTR), E100_STRUCT_SHADOW(0,DTR),
879 E100_STRUCT_MASK(0,DTR),
880 E100_STRUCT_MASK(0,RI),
881 E100_STRUCT_MASK(0,DSR),
882 E100_STRUCT_MASK(0,CD)
883#else
884 CONTROL_PINS_PORT_NOT_USED(0)
885#endif
886 },
887
888 /* Ser 1 */
889 {
890#ifdef CONFIG_ETRAX_SERIAL_PORT1
891 E100_STRUCT_PORT(1,DTR), E100_STRUCT_SHADOW(1,DTR),
892 E100_STRUCT_MASK(1,DTR),
893 E100_STRUCT_MASK(1,RI),
894 E100_STRUCT_MASK(1,DSR),
895 E100_STRUCT_MASK(1,CD)
896#else
897 CONTROL_PINS_PORT_NOT_USED(1)
898#endif
899 },
900
901 /* Ser 2 */
902 {
903#ifdef CONFIG_ETRAX_SERIAL_PORT2
904 E100_STRUCT_PORT(2,DTR), E100_STRUCT_SHADOW(2,DTR),
905 E100_STRUCT_MASK(2,DTR),
906 E100_STRUCT_MASK(2,RI),
907 E100_STRUCT_MASK(2,DSR),
908 E100_STRUCT_MASK(2,CD)
909#else
910 CONTROL_PINS_PORT_NOT_USED(2)
911#endif
912 },
913
914 /* Ser 3 */
915 {
916#ifdef CONFIG_ETRAX_SERIAL_PORT3
917 E100_STRUCT_PORT(3,DTR), E100_STRUCT_SHADOW(3,DTR),
918 E100_STRUCT_MASK(3,DTR),
919 E100_STRUCT_MASK(3,RI),
920 E100_STRUCT_MASK(3,DSR),
921 E100_STRUCT_MASK(3,CD)
922#else
923 CONTROL_PINS_PORT_NOT_USED(3)
924#endif
925 }
926};
927#endif /* !CONFIG_ETRAX_SERX_DTR_RI_DSR_CD_MIXED */
928
929#define E100_RTS_MASK 0x20
930#define E100_CTS_MASK 0x40
931
932/* All serial port signals are active low:
933 * active = 0 -> 3.3V to RS-232 driver -> -12V on RS-232 level
934 * inactive = 1 -> 0V to RS-232 driver -> +12V on RS-232 level
935 *
936 * These macros returns the pin value: 0=0V, >=1 = 3.3V on ETRAX chip
937 */
938
939/* Output */
940#define E100_RTS_GET(info) ((info)->rx_ctrl & E100_RTS_MASK)
941/* Input */
942#define E100_CTS_GET(info) ((info)->ioport[REG_STATUS] & E100_CTS_MASK)
943
944/* These are typically PA or PB and 0 means 0V, 1 means 3.3V */
945/* Is an output */
946#define E100_DTR_GET(info) ((*e100_modem_pins[(info)->line].dtr_shadow) & e100_modem_pins[(info)->line].dtr_mask)
947
948/* Normally inputs */
949#define E100_RI_GET(info) ((*e100_modem_pins[(info)->line].ri_port) & e100_modem_pins[(info)->line].ri_mask)
950#define E100_CD_GET(info) ((*e100_modem_pins[(info)->line].cd_port) & e100_modem_pins[(info)->line].cd_mask)
951
952/* Input */
953#define E100_DSR_GET(info) ((*e100_modem_pins[(info)->line].dsr_port) & e100_modem_pins[(info)->line].dsr_mask)
954
955
956/*
957 * tmp_buf is used as a temporary buffer by serial_write. We need to
958 * lock it in case the memcpy_fromfs blocks while swapping in a page,
959 * and some other program tries to do a serial write at the same time.
960 * Since the lock will only come under contention when the system is
961 * swapping and available memory is low, it makes sense to share one
962 * buffer across all the serial ports, since it significantly saves
963 * memory if large numbers of serial ports are open.
964 */
965static unsigned char *tmp_buf;
966static DEFINE_MUTEX(tmp_buf_mutex);
967
968/* Calculate the chartime depending on baudrate, numbor of bits etc. */
969static void update_char_time(struct e100_serial * info)
970{
971 tcflag_t cflags = info->port.tty->termios->c_cflag;
972 int bits;
973
974 /* calc. number of bits / data byte */
975 /* databits + startbit and 1 stopbit */
976 if ((cflags & CSIZE) == CS7)
977 bits = 9;
978 else
979 bits = 10;
980
981 if (cflags & CSTOPB) /* 2 stopbits ? */
982 bits++;
983
984 if (cflags & PARENB) /* parity bit ? */
985 bits++;
986
987 /* calc timeout */
988 info->char_time_usec = ((bits * 1000000) / info->baud) + 1;
989 info->flush_time_usec = 4*info->char_time_usec;
990 if (info->flush_time_usec < MIN_FLUSH_TIME_USEC)
991 info->flush_time_usec = MIN_FLUSH_TIME_USEC;
992
993}
994
995/*
996 * This function maps from the Bxxxx defines in asm/termbits.h into real
997 * baud rates.
998 */
999
1000static int
1001cflag_to_baud(unsigned int cflag)
1002{
1003 static int baud_table[] = {
1004 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
1005 4800, 9600, 19200, 38400 };
1006
1007 static int ext_baud_table[] = {
1008 0, 57600, 115200, 230400, 460800, 921600, 1843200, 6250000,
1009 0, 0, 0, 0, 0, 0, 0, 0 };
1010
1011 if (cflag & CBAUDEX)
1012 return ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1013 else
1014 return baud_table[cflag & CBAUD];
1015}
1016
1017/* and this maps to an etrax100 hardware baud constant */
1018
1019static unsigned char
1020cflag_to_etrax_baud(unsigned int cflag)
1021{
1022 char retval;
1023
1024 static char baud_table[] = {
1025 -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, 4, 5, 6, 7 };
1026
1027 static char ext_baud_table[] = {
1028 -1, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 };
1029
1030 if (cflag & CBAUDEX)
1031 retval = ext_baud_table[(cflag & CBAUD) & ~CBAUDEX];
1032 else
1033 retval = baud_table[cflag & CBAUD];
1034
1035 if (retval < 0) {
1036 printk(KERN_WARNING "serdriver tried setting invalid baud rate, flags %x.\n", cflag);
1037 retval = 5; /* choose default 9600 instead */
1038 }
1039
1040 return retval | (retval << 4); /* choose same for both TX and RX */
1041}
1042
1043
1044/* Various static support functions */
1045
1046/* Functions to set or clear DTR/RTS on the requested line */
1047/* It is complicated by the fact that RTS is a serial port register, while
1048 * DTR might not be implemented in the HW at all, and if it is, it can be on
1049 * any general port.
1050 */
1051
1052
1053static inline void
1054e100_dtr(struct e100_serial *info, int set)
1055{
1056#ifndef CONFIG_SVINTO_SIM
1057 unsigned char mask = e100_modem_pins[info->line].dtr_mask;
1058
1059#ifdef SERIAL_DEBUG_IO
1060 printk("ser%i dtr %i mask: 0x%02X\n", info->line, set, mask);
1061 printk("ser%i shadow before 0x%02X get: %i\n",
1062 info->line, *e100_modem_pins[info->line].dtr_shadow,
1063 E100_DTR_GET(info));
1064#endif
1065 /* DTR is active low */
1066 {
1067 unsigned long flags;
1068
1069 local_irq_save(flags);
1070 *e100_modem_pins[info->line].dtr_shadow &= ~mask;
1071 *e100_modem_pins[info->line].dtr_shadow |= (set ? 0 : mask);
1072 *e100_modem_pins[info->line].dtr_port = *e100_modem_pins[info->line].dtr_shadow;
1073 local_irq_restore(flags);
1074 }
1075
1076#ifdef SERIAL_DEBUG_IO
1077 printk("ser%i shadow after 0x%02X get: %i\n",
1078 info->line, *e100_modem_pins[info->line].dtr_shadow,
1079 E100_DTR_GET(info));
1080#endif
1081#endif
1082}
1083
1084/* set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
1085 * 0=0V , 1=3.3V
1086 */
1087static inline void
1088e100_rts(struct e100_serial *info, int set)
1089{
1090#ifndef CONFIG_SVINTO_SIM
1091 unsigned long flags;
1092 local_irq_save(flags);
1093 info->rx_ctrl &= ~E100_RTS_MASK;
1094 info->rx_ctrl |= (set ? 0 : E100_RTS_MASK); /* RTS is active low */
1095 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
1096 local_irq_restore(flags);
1097#ifdef SERIAL_DEBUG_IO
1098 printk("ser%i rts %i\n", info->line, set);
1099#endif
1100#endif
1101}
1102
1103
1104/* If this behaves as a modem, RI and CD is an output */
1105static inline void
1106e100_ri_out(struct e100_serial *info, int set)
1107{
1108#ifndef CONFIG_SVINTO_SIM
1109 /* RI is active low */
1110 {
1111 unsigned char mask = e100_modem_pins[info->line].ri_mask;
1112 unsigned long flags;
1113
1114 local_irq_save(flags);
1115 *e100_modem_pins[info->line].ri_shadow &= ~mask;
1116 *e100_modem_pins[info->line].ri_shadow |= (set ? 0 : mask);
1117 *e100_modem_pins[info->line].ri_port = *e100_modem_pins[info->line].ri_shadow;
1118 local_irq_restore(flags);
1119 }
1120#endif
1121}
1122static inline void
1123e100_cd_out(struct e100_serial *info, int set)
1124{
1125#ifndef CONFIG_SVINTO_SIM
1126 /* CD is active low */
1127 {
1128 unsigned char mask = e100_modem_pins[info->line].cd_mask;
1129 unsigned long flags;
1130
1131 local_irq_save(flags);
1132 *e100_modem_pins[info->line].cd_shadow &= ~mask;
1133 *e100_modem_pins[info->line].cd_shadow |= (set ? 0 : mask);
1134 *e100_modem_pins[info->line].cd_port = *e100_modem_pins[info->line].cd_shadow;
1135 local_irq_restore(flags);
1136 }
1137#endif
1138}
1139
1140static inline void
1141e100_disable_rx(struct e100_serial *info)
1142{
1143#ifndef CONFIG_SVINTO_SIM
1144 /* disable the receiver */
1145 info->ioport[REG_REC_CTRL] =
1146 (info->rx_ctrl &= ~IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1147#endif
1148}
1149
1150static inline void
1151e100_enable_rx(struct e100_serial *info)
1152{
1153#ifndef CONFIG_SVINTO_SIM
1154 /* enable the receiver */
1155 info->ioport[REG_REC_CTRL] =
1156 (info->rx_ctrl |= IO_MASK(R_SERIAL0_REC_CTRL, rec_enable));
1157#endif
1158}
1159
1160/* the rx DMA uses both the dma_descr and the dma_eop interrupts */
1161
1162static inline void
1163e100_disable_rxdma_irq(struct e100_serial *info)
1164{
1165#ifdef SERIAL_DEBUG_INTR
1166 printk("rxdma_irq(%d): 0\n",info->line);
1167#endif
1168 DINTR1(DEBUG_LOG(info->line,"IRQ disable_rxdma_irq %i\n", info->line));
1169 *R_IRQ_MASK2_CLR = (info->irq << 2) | (info->irq << 3);
1170}
1171
1172static inline void
1173e100_enable_rxdma_irq(struct e100_serial *info)
1174{
1175#ifdef SERIAL_DEBUG_INTR
1176 printk("rxdma_irq(%d): 1\n",info->line);
1177#endif
1178 DINTR1(DEBUG_LOG(info->line,"IRQ enable_rxdma_irq %i\n", info->line));
1179 *R_IRQ_MASK2_SET = (info->irq << 2) | (info->irq << 3);
1180}
1181
1182/* the tx DMA uses only dma_descr interrupt */
1183
1184static void e100_disable_txdma_irq(struct e100_serial *info)
1185{
1186#ifdef SERIAL_DEBUG_INTR
1187 printk("txdma_irq(%d): 0\n",info->line);
1188#endif
1189 DINTR1(DEBUG_LOG(info->line,"IRQ disable_txdma_irq %i\n", info->line));
1190 *R_IRQ_MASK2_CLR = info->irq;
1191}
1192
1193static void e100_enable_txdma_irq(struct e100_serial *info)
1194{
1195#ifdef SERIAL_DEBUG_INTR
1196 printk("txdma_irq(%d): 1\n",info->line);
1197#endif
1198 DINTR1(DEBUG_LOG(info->line,"IRQ enable_txdma_irq %i\n", info->line));
1199 *R_IRQ_MASK2_SET = info->irq;
1200}
1201
1202static void e100_disable_txdma_channel(struct e100_serial *info)
1203{
1204 unsigned long flags;
1205
1206 /* Disable output DMA channel for the serial port in question
1207 * ( set to something other than serialX)
1208 */
1209 local_irq_save(flags);
1210 DFLOW(DEBUG_LOG(info->line, "disable_txdma_channel %i\n", info->line));
1211 if (info->line == 0) {
1212 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma6)) ==
1213 IO_STATE(R_GEN_CONFIG, dma6, serial0)) {
1214 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1215 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
1216 }
1217 } else if (info->line == 1) {
1218 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma8)) ==
1219 IO_STATE(R_GEN_CONFIG, dma8, serial1)) {
1220 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1221 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
1222 }
1223 } else if (info->line == 2) {
1224 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma2)) ==
1225 IO_STATE(R_GEN_CONFIG, dma2, serial2)) {
1226 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1227 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
1228 }
1229 } else if (info->line == 3) {
1230 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma4)) ==
1231 IO_STATE(R_GEN_CONFIG, dma4, serial3)) {
1232 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1233 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
1234 }
1235 }
1236 *R_GEN_CONFIG = genconfig_shadow;
1237 local_irq_restore(flags);
1238}
1239
1240
1241static void e100_enable_txdma_channel(struct e100_serial *info)
1242{
1243 unsigned long flags;
1244
1245 local_irq_save(flags);
1246 DFLOW(DEBUG_LOG(info->line, "enable_txdma_channel %i\n", info->line));
1247 /* Enable output DMA channel for the serial port in question */
1248 if (info->line == 0) {
1249 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
1250 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, serial0);
1251 } else if (info->line == 1) {
1252 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
1253 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, serial1);
1254 } else if (info->line == 2) {
1255 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
1256 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, serial2);
1257 } else if (info->line == 3) {
1258 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
1259 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, serial3);
1260 }
1261 *R_GEN_CONFIG = genconfig_shadow;
1262 local_irq_restore(flags);
1263}
1264
1265static void e100_disable_rxdma_channel(struct e100_serial *info)
1266{
1267 unsigned long flags;
1268
1269 /* Disable input DMA channel for the serial port in question
1270 * ( set to something other than serialX)
1271 */
1272 local_irq_save(flags);
1273 if (info->line == 0) {
1274 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma7)) ==
1275 IO_STATE(R_GEN_CONFIG, dma7, serial0)) {
1276 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1277 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, unused);
1278 }
1279 } else if (info->line == 1) {
1280 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma9)) ==
1281 IO_STATE(R_GEN_CONFIG, dma9, serial1)) {
1282 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1283 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, usb);
1284 }
1285 } else if (info->line == 2) {
1286 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma3)) ==
1287 IO_STATE(R_GEN_CONFIG, dma3, serial2)) {
1288 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1289 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
1290 }
1291 } else if (info->line == 3) {
1292 if ((genconfig_shadow & IO_MASK(R_GEN_CONFIG, dma5)) ==
1293 IO_STATE(R_GEN_CONFIG, dma5, serial3)) {
1294 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1295 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
1296 }
1297 }
1298 *R_GEN_CONFIG = genconfig_shadow;
1299 local_irq_restore(flags);
1300}
1301
1302
1303static void e100_enable_rxdma_channel(struct e100_serial *info)
1304{
1305 unsigned long flags;
1306
1307 local_irq_save(flags);
1308 /* Enable input DMA channel for the serial port in question */
1309 if (info->line == 0) {
1310 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma7);
1311 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma7, serial0);
1312 } else if (info->line == 1) {
1313 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma9);
1314 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma9, serial1);
1315 } else if (info->line == 2) {
1316 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
1317 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, serial2);
1318 } else if (info->line == 3) {
1319 genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
1320 genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, serial3);
1321 }
1322 *R_GEN_CONFIG = genconfig_shadow;
1323 local_irq_restore(flags);
1324}
1325
1326#ifdef SERIAL_HANDLE_EARLY_ERRORS
1327/* in order to detect and fix errors on the first byte
1328 we have to use the serial interrupts as well. */
1329
1330static inline void
1331e100_disable_serial_data_irq(struct e100_serial *info)
1332{
1333#ifdef SERIAL_DEBUG_INTR
1334 printk("ser_irq(%d): 0\n",info->line);
1335#endif
1336 DINTR1(DEBUG_LOG(info->line,"IRQ disable data_irq %i\n", info->line));
1337 *R_IRQ_MASK1_CLR = (1U << (8+2*info->line));
1338}
1339
1340static inline void
1341e100_enable_serial_data_irq(struct e100_serial *info)
1342{
1343#ifdef SERIAL_DEBUG_INTR
1344 printk("ser_irq(%d): 1\n",info->line);
1345 printk("**** %d = %d\n",
1346 (8+2*info->line),
1347 (1U << (8+2*info->line)));
1348#endif
1349 DINTR1(DEBUG_LOG(info->line,"IRQ enable data_irq %i\n", info->line));
1350 *R_IRQ_MASK1_SET = (1U << (8+2*info->line));
1351}
1352#endif
1353
1354static inline void
1355e100_disable_serial_tx_ready_irq(struct e100_serial *info)
1356{
1357#ifdef SERIAL_DEBUG_INTR
1358 printk("ser_tx_irq(%d): 0\n",info->line);
1359#endif
1360 DINTR1(DEBUG_LOG(info->line,"IRQ disable ready_irq %i\n", info->line));
1361 *R_IRQ_MASK1_CLR = (1U << (8+1+2*info->line));
1362}
1363
1364static inline void
1365e100_enable_serial_tx_ready_irq(struct e100_serial *info)
1366{
1367#ifdef SERIAL_DEBUG_INTR
1368 printk("ser_tx_irq(%d): 1\n",info->line);
1369 printk("**** %d = %d\n",
1370 (8+1+2*info->line),
1371 (1U << (8+1+2*info->line)));
1372#endif
1373 DINTR2(DEBUG_LOG(info->line,"IRQ enable ready_irq %i\n", info->line));
1374 *R_IRQ_MASK1_SET = (1U << (8+1+2*info->line));
1375}
1376
1377static inline void e100_enable_rx_irq(struct e100_serial *info)
1378{
1379 if (info->uses_dma_in)
1380 e100_enable_rxdma_irq(info);
1381 else
1382 e100_enable_serial_data_irq(info);
1383}
1384static inline void e100_disable_rx_irq(struct e100_serial *info)
1385{
1386 if (info->uses_dma_in)
1387 e100_disable_rxdma_irq(info);
1388 else
1389 e100_disable_serial_data_irq(info);
1390}
1391
1392#if defined(CONFIG_ETRAX_RS485)
1393/* Enable RS-485 mode on selected port. This is UGLY. */
1394static int
1395e100_enable_rs485(struct tty_struct *tty, struct serial_rs485 *r)
1396{
1397 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1398
1399#if defined(CONFIG_ETRAX_RS485_ON_PA)
1400 *R_PORT_PA_DATA = port_pa_data_shadow |= (1 << rs485_pa_bit);
1401#endif
1402#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
1403 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1404 rs485_port_g_bit, 1);
1405#endif
1406#if defined(CONFIG_ETRAX_RS485_LTC1387)
1407 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1408 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 1);
1409 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
1410 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 1);
1411#endif
1412
1413 info->rs485 = *r;
1414
1415 /* Maximum delay before RTS equal to 1000 */
1416 if (info->rs485.delay_rts_before_send >= 1000)
1417 info->rs485.delay_rts_before_send = 1000;
1418
1419/* printk("rts: on send = %i, after = %i, enabled = %i",
1420 info->rs485.rts_on_send,
1421 info->rs485.rts_after_sent,
1422 info->rs485.enabled
1423 );
1424*/
1425 return 0;
1426}
1427
1428static int
1429e100_write_rs485(struct tty_struct *tty,
1430 const unsigned char *buf, int count)
1431{
1432 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
1433 int old_value = (info->rs485.flags) & SER_RS485_ENABLED;
1434
1435 /* rs485 is always implicitly enabled if we're using the ioctl()
1436 * but it doesn't have to be set in the serial_rs485
1437 * (to be backward compatible with old apps)
1438 * So we store, set and restore it.
1439 */
1440 info->rs485.flags |= SER_RS485_ENABLED;
1441 /* rs_write now deals with RS485 if enabled */
1442 count = rs_write(tty, buf, count);
1443 if (!old_value)
1444 info->rs485.flags &= ~(SER_RS485_ENABLED);
1445 return count;
1446}
1447
1448#ifdef CONFIG_ETRAX_FAST_TIMER
1449/* Timer function to toggle RTS when using FAST_TIMER */
1450static void rs485_toggle_rts_timer_function(unsigned long data)
1451{
1452 struct e100_serial *info = (struct e100_serial *)data;
1453
1454 fast_timers_rs485[info->line].function = NULL;
1455 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
1456#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
1457 e100_enable_rx(info);
1458 e100_enable_rx_irq(info);
1459#endif
1460}
1461#endif
1462#endif /* CONFIG_ETRAX_RS485 */
1463
1464/*
1465 * ------------------------------------------------------------
1466 * rs_stop() and rs_start()
1467 *
1468 * This routines are called before setting or resetting tty->stopped.
1469 * They enable or disable transmitter using the XOFF registers, as necessary.
1470 * ------------------------------------------------------------
1471 */
1472
1473static void
1474rs_stop(struct tty_struct *tty)
1475{
1476 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1477 if (info) {
1478 unsigned long flags;
1479 unsigned long xoff;
1480
1481 local_irq_save(flags);
1482 DFLOW(DEBUG_LOG(info->line, "XOFF rs_stop xmit %i\n",
1483 CIRC_CNT(info->xmit.head,
1484 info->xmit.tail,SERIAL_XMIT_SIZE)));
1485
1486 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char,
1487 STOP_CHAR(info->port.tty));
1488 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, stop);
1489 if (tty->termios->c_iflag & IXON ) {
1490 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1491 }
1492
1493 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
1494 local_irq_restore(flags);
1495 }
1496}
1497
1498static void
1499rs_start(struct tty_struct *tty)
1500{
1501 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
1502 if (info) {
1503 unsigned long flags;
1504 unsigned long xoff;
1505
1506 local_irq_save(flags);
1507 DFLOW(DEBUG_LOG(info->line, "XOFF rs_start xmit %i\n",
1508 CIRC_CNT(info->xmit.head,
1509 info->xmit.tail,SERIAL_XMIT_SIZE)));
1510 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(tty));
1511 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
1512 if (tty->termios->c_iflag & IXON ) {
1513 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
1514 }
1515
1516 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
1517 if (!info->uses_dma_out &&
1518 info->xmit.head != info->xmit.tail && info->xmit.buf)
1519 e100_enable_serial_tx_ready_irq(info);
1520
1521 local_irq_restore(flags);
1522 }
1523}
1524
1525/*
1526 * ----------------------------------------------------------------------
1527 *
1528 * Here starts the interrupt handling routines. All of the following
1529 * subroutines are declared as inline and are folded into
1530 * rs_interrupt(). They were separated out for readability's sake.
1531 *
1532 * Note: rs_interrupt() is a "fast" interrupt, which means that it
1533 * runs with interrupts turned off. People who may want to modify
1534 * rs_interrupt() should try to keep the interrupt handler as fast as
1535 * possible. After you are done making modifications, it is not a bad
1536 * idea to do:
1537 *
1538 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
1539 *
1540 * and look at the resulting assemble code in serial.s.
1541 *
1542 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
1543 * -----------------------------------------------------------------------
1544 */
1545
1546/*
1547 * This routine is used by the interrupt handler to schedule
1548 * processing in the software interrupt portion of the driver.
1549 */
1550static void rs_sched_event(struct e100_serial *info, int event)
1551{
1552 if (info->event & (1 << event))
1553 return;
1554 info->event |= 1 << event;
1555 schedule_work(&info->work);
1556}
1557
1558/* The output DMA channel is free - use it to send as many chars as possible
1559 * NOTES:
1560 * We don't pay attention to info->x_char, which means if the TTY wants to
1561 * use XON/XOFF it will set info->x_char but we won't send any X char!
1562 *
1563 * To implement this, we'd just start a DMA send of 1 byte pointing at a
1564 * buffer containing the X char, and skip updating xmit. We'd also have to
1565 * check if the last sent char was the X char when we enter this function
1566 * the next time, to avoid updating xmit with the sent X value.
1567 */
1568
1569static void
1570transmit_chars_dma(struct e100_serial *info)
1571{
1572 unsigned int c, sentl;
1573 struct etrax_dma_descr *descr;
1574
1575#ifdef CONFIG_SVINTO_SIM
1576 /* This will output too little if tail is not 0 always since
1577 * we don't reloop to send the other part. Anyway this SHOULD be a
1578 * no-op - transmit_chars_dma would never really be called during sim
1579 * since rs_write does not write into the xmit buffer then.
1580 */
1581 if (info->xmit.tail)
1582 printk("Error in serial.c:transmit_chars-dma(), tail!=0\n");
1583 if (info->xmit.head != info->xmit.tail) {
1584 SIMCOUT(info->xmit.buf + info->xmit.tail,
1585 CIRC_CNT(info->xmit.head,
1586 info->xmit.tail,
1587 SERIAL_XMIT_SIZE));
1588 info->xmit.head = info->xmit.tail; /* move back head */
1589 info->tr_running = 0;
1590 }
1591 return;
1592#endif
1593 /* acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1594 *info->oclrintradr =
1595 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1596 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1597
1598#ifdef SERIAL_DEBUG_INTR
1599 if (info->line == SERIAL_DEBUG_LINE)
1600 printk("tc\n");
1601#endif
1602 if (!info->tr_running) {
1603 /* weirdo... we shouldn't get here! */
1604 printk(KERN_WARNING "Achtung: transmit_chars_dma with !tr_running\n");
1605 return;
1606 }
1607
1608 descr = &info->tr_descr;
1609
1610 /* first get the amount of bytes sent during the last DMA transfer,
1611 and update xmit accordingly */
1612
1613 /* if the stop bit was not set, all data has been sent */
1614 if (!(descr->status & d_stop)) {
1615 sentl = descr->sw_len;
1616 } else
1617 /* otherwise we find the amount of data sent here */
1618 sentl = descr->hw_len;
1619
1620 DFLOW(DEBUG_LOG(info->line, "TX %i done\n", sentl));
1621
1622 /* update stats */
1623 info->icount.tx += sentl;
1624
1625 /* update xmit buffer */
1626 info->xmit.tail = (info->xmit.tail + sentl) & (SERIAL_XMIT_SIZE - 1);
1627
1628 /* if there is only a few chars left in the buf, wake up the blocked
1629 write if any */
1630 if (CIRC_CNT(info->xmit.head,
1631 info->xmit.tail,
1632 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
1633 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
1634
1635 /* find out the largest amount of consecutive bytes we want to send now */
1636
1637 c = CIRC_CNT_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1638
1639 /* Don't send all in one DMA transfer - divide it so we wake up
1640 * application before all is sent
1641 */
1642
1643 if (c >= 4*WAKEUP_CHARS)
1644 c = c/2;
1645
1646 if (c <= 0) {
1647 /* our job here is done, don't schedule any new DMA transfer */
1648 info->tr_running = 0;
1649
1650#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
1651 if (info->rs485.flags & SER_RS485_ENABLED) {
1652 /* Set a short timer to toggle RTS */
1653 start_one_shot_timer(&fast_timers_rs485[info->line],
1654 rs485_toggle_rts_timer_function,
1655 (unsigned long)info,
1656 info->char_time_usec*2,
1657 "RS-485");
1658 }
1659#endif /* RS485 */
1660 return;
1661 }
1662
1663 /* ok we can schedule a dma send of c chars starting at info->xmit.tail */
1664 /* set up the descriptor correctly for output */
1665 DFLOW(DEBUG_LOG(info->line, "TX %i\n", c));
1666 descr->ctrl = d_int | d_eol | d_wait; /* Wait needed for tty_wait_until_sent() */
1667 descr->sw_len = c;
1668 descr->buf = virt_to_phys(info->xmit.buf + info->xmit.tail);
1669 descr->status = 0;
1670
1671 *info->ofirstadr = virt_to_phys(descr); /* write to R_DMAx_FIRST */
1672 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1673
1674 /* DMA is now running (hopefully) */
1675} /* transmit_chars_dma */
1676
1677static void
1678start_transmit(struct e100_serial *info)
1679{
1680#if 0
1681 if (info->line == SERIAL_DEBUG_LINE)
1682 printk("x\n");
1683#endif
1684
1685 info->tr_descr.sw_len = 0;
1686 info->tr_descr.hw_len = 0;
1687 info->tr_descr.status = 0;
1688 info->tr_running = 1;
1689 if (info->uses_dma_out)
1690 transmit_chars_dma(info);
1691 else
1692 e100_enable_serial_tx_ready_irq(info);
1693} /* start_transmit */
1694
1695#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
1696static int serial_fast_timer_started = 0;
1697static int serial_fast_timer_expired = 0;
1698static void flush_timeout_function(unsigned long data);
1699#define START_FLUSH_FAST_TIMER_TIME(info, string, usec) {\
1700 unsigned long timer_flags; \
1701 local_irq_save(timer_flags); \
1702 if (fast_timers[info->line].function == NULL) { \
1703 serial_fast_timer_started++; \
1704 TIMERD(DEBUG_LOG(info->line, "start_timer %i ", info->line)); \
1705 TIMERD(DEBUG_LOG(info->line, "num started: %i\n", serial_fast_timer_started)); \
1706 start_one_shot_timer(&fast_timers[info->line], \
1707 flush_timeout_function, \
1708 (unsigned long)info, \
1709 (usec), \
1710 string); \
1711 } \
1712 else { \
1713 TIMERD(DEBUG_LOG(info->line, "timer %i already running\n", info->line)); \
1714 } \
1715 local_irq_restore(timer_flags); \
1716}
1717#define START_FLUSH_FAST_TIMER(info, string) START_FLUSH_FAST_TIMER_TIME(info, string, info->flush_time_usec)
1718
1719#else
1720#define START_FLUSH_FAST_TIMER_TIME(info, string, usec)
1721#define START_FLUSH_FAST_TIMER(info, string)
1722#endif
1723
1724static struct etrax_recv_buffer *
1725alloc_recv_buffer(unsigned int size)
1726{
1727 struct etrax_recv_buffer *buffer;
1728
1729 if (!(buffer = kmalloc(sizeof *buffer + size, GFP_ATOMIC)))
1730 return NULL;
1731
1732 buffer->next = NULL;
1733 buffer->length = 0;
1734 buffer->error = TTY_NORMAL;
1735
1736 return buffer;
1737}
1738
1739static void
1740append_recv_buffer(struct e100_serial *info, struct etrax_recv_buffer *buffer)
1741{
1742 unsigned long flags;
1743
1744 local_irq_save(flags);
1745
1746 if (!info->first_recv_buffer)
1747 info->first_recv_buffer = buffer;
1748 else
1749 info->last_recv_buffer->next = buffer;
1750
1751 info->last_recv_buffer = buffer;
1752
1753 info->recv_cnt += buffer->length;
1754 if (info->recv_cnt > info->max_recv_cnt)
1755 info->max_recv_cnt = info->recv_cnt;
1756
1757 local_irq_restore(flags);
1758}
1759
1760static int
1761add_char_and_flag(struct e100_serial *info, unsigned char data, unsigned char flag)
1762{
1763 struct etrax_recv_buffer *buffer;
1764 if (info->uses_dma_in) {
1765 if (!(buffer = alloc_recv_buffer(4)))
1766 return 0;
1767
1768 buffer->length = 1;
1769 buffer->error = flag;
1770 buffer->buffer[0] = data;
1771
1772 append_recv_buffer(info, buffer);
1773
1774 info->icount.rx++;
1775 } else {
1776 struct tty_struct *tty = info->port.tty;
1777 tty_insert_flip_char(tty, data, flag);
1778 info->icount.rx++;
1779 }
1780
1781 return 1;
1782}
1783
1784static unsigned int handle_descr_data(struct e100_serial *info,
1785 struct etrax_dma_descr *descr,
1786 unsigned int recvl)
1787{
1788 struct etrax_recv_buffer *buffer = phys_to_virt(descr->buf) - sizeof *buffer;
1789
1790 if (info->recv_cnt + recvl > 65536) {
1791 printk(KERN_CRIT
1792 "%s: Too much pending incoming serial data! Dropping %u bytes.\n", __func__, recvl);
1793 return 0;
1794 }
1795
1796 buffer->length = recvl;
1797
1798 if (info->errorcode == ERRCODE_SET_BREAK)
1799 buffer->error = TTY_BREAK;
1800 info->errorcode = 0;
1801
1802 append_recv_buffer(info, buffer);
1803
1804 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1805 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
1806
1807 descr->buf = virt_to_phys(buffer->buffer);
1808
1809 return recvl;
1810}
1811
1812static unsigned int handle_all_descr_data(struct e100_serial *info)
1813{
1814 struct etrax_dma_descr *descr;
1815 unsigned int recvl;
1816 unsigned int ret = 0;
1817
1818 while (1)
1819 {
1820 descr = &info->rec_descr[info->cur_rec_descr];
1821
1822 if (descr == phys_to_virt(*info->idescradr))
1823 break;
1824
1825 if (++info->cur_rec_descr == SERIAL_RECV_DESCRIPTORS)
1826 info->cur_rec_descr = 0;
1827
1828 /* find out how many bytes were read */
1829
1830 /* if the eop bit was not set, all data has been received */
1831 if (!(descr->status & d_eop)) {
1832 recvl = descr->sw_len;
1833 } else {
1834 /* otherwise we find the amount of data received here */
1835 recvl = descr->hw_len;
1836 }
1837
1838 /* Reset the status information */
1839 descr->status = 0;
1840
1841 DFLOW( DEBUG_LOG(info->line, "RX %lu\n", recvl);
1842 if (info->port.tty->stopped) {
1843 unsigned char *buf = phys_to_virt(descr->buf);
1844 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[0]);
1845 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[1]);
1846 DEBUG_LOG(info->line, "rx 0x%02X\n", buf[2]);
1847 }
1848 );
1849
1850 /* update stats */
1851 info->icount.rx += recvl;
1852
1853 ret += handle_descr_data(info, descr, recvl);
1854 }
1855
1856 return ret;
1857}
1858
1859static void receive_chars_dma(struct e100_serial *info)
1860{
1861 struct tty_struct *tty;
1862 unsigned char rstat;
1863
1864#ifdef CONFIG_SVINTO_SIM
1865 /* No receive in the simulator. Will probably be when the rest of
1866 * the serial interface works, and this piece will just be removed.
1867 */
1868 return;
1869#endif
1870
1871 /* Acknowledge both dma_descr and dma_eop irq in R_DMA_CHx_CLR_INTR */
1872 *info->iclrintradr =
1873 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
1874 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
1875
1876 tty = info->port.tty;
1877 if (!tty) /* Something wrong... */
1878 return;
1879
1880#ifdef SERIAL_HANDLE_EARLY_ERRORS
1881 if (info->uses_dma_in)
1882 e100_enable_serial_data_irq(info);
1883#endif
1884
1885 if (info->errorcode == ERRCODE_INSERT_BREAK)
1886 add_char_and_flag(info, '\0', TTY_BREAK);
1887
1888 handle_all_descr_data(info);
1889
1890 /* Read the status register to detect errors */
1891 rstat = info->ioport[REG_STATUS];
1892 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
1893 DFLOW(DEBUG_LOG(info->line, "XOFF detect stat %x\n", rstat));
1894 }
1895
1896 if (rstat & SER_ERROR_MASK) {
1897 /* If we got an error, we must reset it by reading the
1898 * data_in field
1899 */
1900 unsigned char data = info->ioport[REG_DATA];
1901
1902 PROCSTAT(ser_stat[info->line].errors_cnt++);
1903 DEBUG_LOG(info->line, "#dERR: s d 0x%04X\n",
1904 ((rstat & SER_ERROR_MASK) << 8) | data);
1905
1906 if (rstat & SER_PAR_ERR_MASK)
1907 add_char_and_flag(info, data, TTY_PARITY);
1908 else if (rstat & SER_OVERRUN_MASK)
1909 add_char_and_flag(info, data, TTY_OVERRUN);
1910 else if (rstat & SER_FRAMING_ERR_MASK)
1911 add_char_and_flag(info, data, TTY_FRAME);
1912 }
1913
1914 START_FLUSH_FAST_TIMER(info, "receive_chars");
1915
1916 /* Restart the receiving DMA */
1917 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
1918}
1919
1920static int start_recv_dma(struct e100_serial *info)
1921{
1922 struct etrax_dma_descr *descr = info->rec_descr;
1923 struct etrax_recv_buffer *buffer;
1924 int i;
1925
1926 /* Set up the receiving descriptors */
1927 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++) {
1928 if (!(buffer = alloc_recv_buffer(SERIAL_DESCR_BUF_SIZE)))
1929 panic("%s: Failed to allocate memory for receive buffer!\n", __func__);
1930
1931 descr[i].ctrl = d_int;
1932 descr[i].buf = virt_to_phys(buffer->buffer);
1933 descr[i].sw_len = SERIAL_DESCR_BUF_SIZE;
1934 descr[i].hw_len = 0;
1935 descr[i].status = 0;
1936 descr[i].next = virt_to_phys(&descr[i+1]);
1937 }
1938
1939 /* Link the last descriptor to the first */
1940 descr[i-1].next = virt_to_phys(&descr[0]);
1941
1942 /* Start with the first descriptor in the list */
1943 info->cur_rec_descr = 0;
1944
1945 /* Start the DMA */
1946 *info->ifirstadr = virt_to_phys(&descr[info->cur_rec_descr]);
1947 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, start);
1948
1949 /* Input DMA should be running now */
1950 return 1;
1951}
1952
1953static void
1954start_receive(struct e100_serial *info)
1955{
1956#ifdef CONFIG_SVINTO_SIM
1957 /* No receive in the simulator. Will probably be when the rest of
1958 * the serial interface works, and this piece will just be removed.
1959 */
1960 return;
1961#endif
1962 if (info->uses_dma_in) {
1963 /* reset the input dma channel to be sure it works */
1964
1965 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
1966 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
1967 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
1968
1969 start_recv_dma(info);
1970 }
1971}
1972
1973
1974/* the bits in the MASK2 register are laid out like this:
1975 DMAI_EOP DMAI_DESCR DMAO_EOP DMAO_DESCR
1976 where I is the input channel and O is the output channel for the port.
1977 info->irq is the bit number for the DMAO_DESCR so to check the others we
1978 shift info->irq to the left.
1979*/
1980
1981/* dma output channel interrupt handler
1982 this interrupt is called from DMA2(ser2), DMA4(ser3), DMA6(ser0) or
1983 DMA8(ser1) when they have finished a descriptor with the intr flag set.
1984*/
1985
1986static irqreturn_t
1987tr_interrupt(int irq, void *dev_id)
1988{
1989 struct e100_serial *info;
1990 unsigned long ireg;
1991 int i;
1992 int handled = 0;
1993
1994#ifdef CONFIG_SVINTO_SIM
1995 /* No receive in the simulator. Will probably be when the rest of
1996 * the serial interface works, and this piece will just be removed.
1997 */
1998 {
1999 const char *s = "What? tr_interrupt in simulator??\n";
2000 SIMCOUT(s,strlen(s));
2001 }
2002 return IRQ_HANDLED;
2003#endif
2004
2005 /* find out the line that caused this irq and get it from rs_table */
2006
2007 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2008
2009 for (i = 0; i < NR_PORTS; i++) {
2010 info = rs_table + i;
2011 if (!info->enabled || !info->uses_dma_out)
2012 continue;
2013 /* check for dma_descr (don't need to check for dma_eop in output dma for serial */
2014 if (ireg & info->irq) {
2015 handled = 1;
2016 /* we can send a new dma bunch. make it so. */
2017 DINTR2(DEBUG_LOG(info->line, "tr_interrupt %i\n", i));
2018 /* Read jiffies_usec first,
2019 * we want this time to be as late as possible
2020 */
2021 PROCSTAT(ser_stat[info->line].tx_dma_ints++);
2022 info->last_tx_active_usec = GET_JIFFIES_USEC();
2023 info->last_tx_active = jiffies;
2024 transmit_chars_dma(info);
2025 }
2026
2027 /* FIXME: here we should really check for a change in the
2028 status lines and if so call status_handle(info) */
2029 }
2030 return IRQ_RETVAL(handled);
2031} /* tr_interrupt */
2032
2033/* dma input channel interrupt handler */
2034
2035static irqreturn_t
2036rec_interrupt(int irq, void *dev_id)
2037{
2038 struct e100_serial *info;
2039 unsigned long ireg;
2040 int i;
2041 int handled = 0;
2042
2043#ifdef CONFIG_SVINTO_SIM
2044 /* No receive in the simulator. Will probably be when the rest of
2045 * the serial interface works, and this piece will just be removed.
2046 */
2047 {
2048 const char *s = "What? rec_interrupt in simulator??\n";
2049 SIMCOUT(s,strlen(s));
2050 }
2051 return IRQ_HANDLED;
2052#endif
2053
2054 /* find out the line that caused this irq and get it from rs_table */
2055
2056 ireg = *R_IRQ_MASK2_RD; /* get the active irq bits for the dma channels */
2057
2058 for (i = 0; i < NR_PORTS; i++) {
2059 info = rs_table + i;
2060 if (!info->enabled || !info->uses_dma_in)
2061 continue;
2062 /* check for both dma_eop and dma_descr for the input dma channel */
2063 if (ireg & ((info->irq << 2) | (info->irq << 3))) {
2064 handled = 1;
2065 /* we have received something */
2066 receive_chars_dma(info);
2067 }
2068
2069 /* FIXME: here we should really check for a change in the
2070 status lines and if so call status_handle(info) */
2071 }
2072 return IRQ_RETVAL(handled);
2073} /* rec_interrupt */
2074
2075static int force_eop_if_needed(struct e100_serial *info)
2076{
2077 /* We check data_avail bit to determine if data has
2078 * arrived since last time
2079 */
2080 unsigned char rstat = info->ioport[REG_STATUS];
2081
2082 /* error or datavail? */
2083 if (rstat & SER_ERROR_MASK) {
2084 /* Some error has occurred. If there has been valid data, an
2085 * EOP interrupt will be made automatically. If no data, the
2086 * normal ser_interrupt should be enabled and handle it.
2087 * So do nothing!
2088 */
2089 DEBUG_LOG(info->line, "timeout err: rstat 0x%03X\n",
2090 rstat | (info->line << 8));
2091 return 0;
2092 }
2093
2094 if (rstat & SER_DATA_AVAIL_MASK) {
2095 /* Ok data, no error, count it */
2096 TIMERD(DEBUG_LOG(info->line, "timeout: rstat 0x%03X\n",
2097 rstat | (info->line << 8)));
2098 /* Read data to clear status flags */
2099 (void)info->ioport[REG_DATA];
2100
2101 info->forced_eop = 0;
2102 START_FLUSH_FAST_TIMER(info, "magic");
2103 return 0;
2104 }
2105
2106 /* hit the timeout, force an EOP for the input
2107 * dma channel if we haven't already
2108 */
2109 if (!info->forced_eop) {
2110 info->forced_eop = 1;
2111 PROCSTAT(ser_stat[info->line].timeout_flush_cnt++);
2112 TIMERD(DEBUG_LOG(info->line, "timeout EOP %i\n", info->line));
2113 FORCE_EOP(info);
2114 }
2115
2116 return 1;
2117}
2118
2119static void flush_to_flip_buffer(struct e100_serial *info)
2120{
2121 struct tty_struct *tty;
2122 struct etrax_recv_buffer *buffer;
2123 unsigned long flags;
2124
2125 local_irq_save(flags);
2126 tty = info->port.tty;
2127
2128 if (!tty) {
2129 local_irq_restore(flags);
2130 return;
2131 }
2132
2133 while ((buffer = info->first_recv_buffer) != NULL) {
2134 unsigned int count = buffer->length;
2135
2136 tty_insert_flip_string(tty, buffer->buffer, count);
2137 info->recv_cnt -= count;
2138
2139 if (count == buffer->length) {
2140 info->first_recv_buffer = buffer->next;
2141 kfree(buffer);
2142 } else {
2143 buffer->length -= count;
2144 memmove(buffer->buffer, buffer->buffer + count, buffer->length);
2145 buffer->error = TTY_NORMAL;
2146 }
2147 }
2148
2149 if (!info->first_recv_buffer)
2150 info->last_recv_buffer = NULL;
2151
2152 local_irq_restore(flags);
2153
2154 /* This includes a check for low-latency */
2155 tty_flip_buffer_push(tty);
2156}
2157
2158static void check_flush_timeout(struct e100_serial *info)
2159{
2160 /* Flip what we've got (if we can) */
2161 flush_to_flip_buffer(info);
2162
2163 /* We might need to flip later, but not to fast
2164 * since the system is busy processing input... */
2165 if (info->first_recv_buffer)
2166 START_FLUSH_FAST_TIMER_TIME(info, "flip", 2000);
2167
2168 /* Force eop last, since data might have come while we're processing
2169 * and if we started the slow timer above, we won't start a fast
2170 * below.
2171 */
2172 force_eop_if_needed(info);
2173}
2174
2175#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
2176static void flush_timeout_function(unsigned long data)
2177{
2178 struct e100_serial *info = (struct e100_serial *)data;
2179
2180 fast_timers[info->line].function = NULL;
2181 serial_fast_timer_expired++;
2182 TIMERD(DEBUG_LOG(info->line, "flush_timout %i ", info->line));
2183 TIMERD(DEBUG_LOG(info->line, "num expired: %i\n", serial_fast_timer_expired));
2184 check_flush_timeout(info);
2185}
2186
2187#else
2188
2189/* dma fifo/buffer timeout handler
2190 forces an end-of-packet for the dma input channel if no chars
2191 have been received for CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS/100 s.
2192*/
2193
2194static struct timer_list flush_timer;
2195
2196static void
2197timed_flush_handler(unsigned long ptr)
2198{
2199 struct e100_serial *info;
2200 int i;
2201
2202#ifdef CONFIG_SVINTO_SIM
2203 return;
2204#endif
2205
2206 for (i = 0; i < NR_PORTS; i++) {
2207 info = rs_table + i;
2208 if (info->uses_dma_in)
2209 check_flush_timeout(info);
2210 }
2211
2212 /* restart flush timer */
2213 mod_timer(&flush_timer, jiffies + CONFIG_ETRAX_SERIAL_RX_TIMEOUT_TICKS);
2214}
2215#endif
2216
2217#ifdef SERIAL_HANDLE_EARLY_ERRORS
2218
2219/* If there is an error (ie break) when the DMA is running and
2220 * there are no bytes in the fifo the DMA is stopped and we get no
2221 * eop interrupt. Thus we have to monitor the first bytes on a DMA
2222 * transfer, and if it is without error we can turn the serial
2223 * interrupts off.
2224 */
2225
2226/*
2227BREAK handling on ETRAX 100:
2228ETRAX will generate interrupt although there is no stop bit between the
2229characters.
2230
2231Depending on how long the break sequence is, the end of the breaksequence
2232will look differently:
2233| indicates start/end of a character.
2234
2235B= Break character (0x00) with framing error.
2236E= Error byte with parity error received after B characters.
2237F= "Faked" valid byte received immediately after B characters.
2238V= Valid byte
2239
22401.
2241 B BL ___________________________ V
2242.._|__________|__________| |valid data |
2243
2244Multiple frame errors with data == 0x00 (B),
2245the timing matches up "perfectly" so no extra ending char is detected.
2246The RXD pin is 1 in the last interrupt, in that case
2247we set info->errorcode = ERRCODE_INSERT_BREAK, but we can't really
2248know if another byte will come and this really is case 2. below
2249(e.g F=0xFF or 0xFE)
2250If RXD pin is 0 we can expect another character (see 2. below).
2251
2252
22532.
2254
2255 B B E or F__________________..__ V
2256.._|__________|__________|______ | |valid data
2257 "valid" or
2258 parity error
2259
2260Multiple frame errors with data == 0x00 (B),
2261but the part of the break trigs is interpreted as a start bit (and possibly
2262some 0 bits followed by a number of 1 bits and a stop bit).
2263Depending on parity settings etc. this last character can be either
2264a fake "valid" char (F) or have a parity error (E).
2265
2266If the character is valid it will be put in the buffer,
2267we set info->errorcode = ERRCODE_SET_BREAK so the receive interrupt
2268will set the flags so the tty will handle it,
2269if it's an error byte it will not be put in the buffer
2270and we set info->errorcode = ERRCODE_INSERT_BREAK.
2271
2272To distinguish a V byte in 1. from an F byte in 2. we keep a timestamp
2273of the last faulty char (B) and compares it with the current time:
2274If the time elapsed time is less then 2*char_time_usec we will assume
2275it's a faked F char and not a Valid char and set
2276info->errorcode = ERRCODE_SET_BREAK.
2277
2278Flaws in the above solution:
2279~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2280We use the timer to distinguish a F character from a V character,
2281if a V character is to close after the break we might make the wrong decision.
2282
2283TODO: The break will be delayed until an F or V character is received.
2284
2285*/
2286
2287static
2288struct e100_serial * handle_ser_rx_interrupt_no_dma(struct e100_serial *info)
2289{
2290 unsigned long data_read;
2291 struct tty_struct *tty = info->port.tty;
2292
2293 if (!tty) {
2294 printk("!NO TTY!\n");
2295 return info;
2296 }
2297
2298 /* Read data and status at the same time */
2299 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
2300more_data:
2301 if (data_read & IO_MASK(R_SERIAL0_READ, xoff_detect) ) {
2302 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2303 }
2304 DINTR2(DEBUG_LOG(info->line, "ser_rx %c\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read)));
2305
2306 if (data_read & ( IO_MASK(R_SERIAL0_READ, framing_err) |
2307 IO_MASK(R_SERIAL0_READ, par_err) |
2308 IO_MASK(R_SERIAL0_READ, overrun) )) {
2309 /* An error */
2310 info->last_rx_active_usec = GET_JIFFIES_USEC();
2311 info->last_rx_active = jiffies;
2312 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat_data %04X\n", data_read));
2313 DLOG_INT_TRIG(
2314 if (!log_int_trig1_pos) {
2315 log_int_trig1_pos = log_int_pos;
2316 log_int(rdpc(), 0, 0);
2317 }
2318 );
2319
2320
2321 if ( ((data_read & IO_MASK(R_SERIAL0_READ, data_in)) == 0) &&
2322 (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) ) {
2323 /* Most likely a break, but we get interrupts over and
2324 * over again.
2325 */
2326
2327 if (!info->break_detected_cnt) {
2328 DEBUG_LOG(info->line, "#BRK start\n", 0);
2329 }
2330 if (data_read & IO_MASK(R_SERIAL0_READ, rxd)) {
2331 /* The RX pin is high now, so the break
2332 * must be over, but....
2333 * we can't really know if we will get another
2334 * last byte ending the break or not.
2335 * And we don't know if the byte (if any) will
2336 * have an error or look valid.
2337 */
2338 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2339 info->errorcode = ERRCODE_INSERT_BREAK;
2340 }
2341 info->break_detected_cnt++;
2342 } else {
2343 /* The error does not look like a break, but could be
2344 * the end of one
2345 */
2346 if (info->break_detected_cnt) {
2347 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2348 info->errorcode = ERRCODE_INSERT_BREAK;
2349 } else {
2350 unsigned char data = IO_EXTRACT(R_SERIAL0_READ,
2351 data_in, data_read);
2352 char flag = TTY_NORMAL;
2353 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2354 struct tty_struct *tty = info->port.tty;
2355 tty_insert_flip_char(tty, 0, flag);
2356 info->icount.rx++;
2357 }
2358
2359 if (data_read & IO_MASK(R_SERIAL0_READ, par_err)) {
2360 info->icount.parity++;
2361 flag = TTY_PARITY;
2362 } else if (data_read & IO_MASK(R_SERIAL0_READ, overrun)) {
2363 info->icount.overrun++;
2364 flag = TTY_OVERRUN;
2365 } else if (data_read & IO_MASK(R_SERIAL0_READ, framing_err)) {
2366 info->icount.frame++;
2367 flag = TTY_FRAME;
2368 }
2369 tty_insert_flip_char(tty, data, flag);
2370 info->errorcode = 0;
2371 }
2372 info->break_detected_cnt = 0;
2373 }
2374 } else if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2375 /* No error */
2376 DLOG_INT_TRIG(
2377 if (!log_int_trig1_pos) {
2378 if (log_int_pos >= log_int_size) {
2379 log_int_pos = 0;
2380 }
2381 log_int_trig0_pos = log_int_pos;
2382 log_int(rdpc(), 0, 0);
2383 }
2384 );
2385 tty_insert_flip_char(tty,
2386 IO_EXTRACT(R_SERIAL0_READ, data_in, data_read),
2387 TTY_NORMAL);
2388 } else {
2389 DEBUG_LOG(info->line, "ser_rx int but no data_avail %08lX\n", data_read);
2390 }
2391
2392
2393 info->icount.rx++;
2394 data_read = *((unsigned long *)&info->ioport[REG_DATA_STATUS32]);
2395 if (data_read & IO_MASK(R_SERIAL0_READ, data_avail)) {
2396 DEBUG_LOG(info->line, "ser_rx %c in loop\n", IO_EXTRACT(R_SERIAL0_READ, data_in, data_read));
2397 goto more_data;
2398 }
2399
2400 tty_flip_buffer_push(info->port.tty);
2401 return info;
2402}
2403
2404static struct e100_serial* handle_ser_rx_interrupt(struct e100_serial *info)
2405{
2406 unsigned char rstat;
2407
2408#ifdef SERIAL_DEBUG_INTR
2409 printk("Interrupt from serport %d\n", i);
2410#endif
2411/* DEBUG_LOG(info->line, "ser_interrupt stat %03X\n", rstat | (i << 8)); */
2412 if (!info->uses_dma_in) {
2413 return handle_ser_rx_interrupt_no_dma(info);
2414 }
2415 /* DMA is used */
2416 rstat = info->ioport[REG_STATUS];
2417 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect) ) {
2418 DFLOW(DEBUG_LOG(info->line, "XOFF detect\n", 0));
2419 }
2420
2421 if (rstat & SER_ERROR_MASK) {
2422 unsigned char data;
2423
2424 info->last_rx_active_usec = GET_JIFFIES_USEC();
2425 info->last_rx_active = jiffies;
2426 /* If we got an error, we must reset it by reading the
2427 * data_in field
2428 */
2429 data = info->ioport[REG_DATA];
2430 DINTR1(DEBUG_LOG(info->line, "ser_rx! %c\n", data));
2431 DINTR1(DEBUG_LOG(info->line, "ser_rx err stat %02X\n", rstat));
2432 if (!data && (rstat & SER_FRAMING_ERR_MASK)) {
2433 /* Most likely a break, but we get interrupts over and
2434 * over again.
2435 */
2436
2437 if (!info->break_detected_cnt) {
2438 DEBUG_LOG(info->line, "#BRK start\n", 0);
2439 }
2440 if (rstat & SER_RXD_MASK) {
2441 /* The RX pin is high now, so the break
2442 * must be over, but....
2443 * we can't really know if we will get another
2444 * last byte ending the break or not.
2445 * And we don't know if the byte (if any) will
2446 * have an error or look valid.
2447 */
2448 DEBUG_LOG(info->line, "# BL BRK\n", 0);
2449 info->errorcode = ERRCODE_INSERT_BREAK;
2450 }
2451 info->break_detected_cnt++;
2452 } else {
2453 /* The error does not look like a break, but could be
2454 * the end of one
2455 */
2456 if (info->break_detected_cnt) {
2457 DEBUG_LOG(info->line, "EBRK %i\n", info->break_detected_cnt);
2458 info->errorcode = ERRCODE_INSERT_BREAK;
2459 } else {
2460 if (info->errorcode == ERRCODE_INSERT_BREAK) {
2461 info->icount.brk++;
2462 add_char_and_flag(info, '\0', TTY_BREAK);
2463 }
2464
2465 if (rstat & SER_PAR_ERR_MASK) {
2466 info->icount.parity++;
2467 add_char_and_flag(info, data, TTY_PARITY);
2468 } else if (rstat & SER_OVERRUN_MASK) {
2469 info->icount.overrun++;
2470 add_char_and_flag(info, data, TTY_OVERRUN);
2471 } else if (rstat & SER_FRAMING_ERR_MASK) {
2472 info->icount.frame++;
2473 add_char_and_flag(info, data, TTY_FRAME);
2474 }
2475
2476 info->errorcode = 0;
2477 }
2478 info->break_detected_cnt = 0;
2479 DEBUG_LOG(info->line, "#iERR s d %04X\n",
2480 ((rstat & SER_ERROR_MASK) << 8) | data);
2481 }
2482 PROCSTAT(ser_stat[info->line].early_errors_cnt++);
2483 } else { /* It was a valid byte, now let the DMA do the rest */
2484 unsigned long curr_time_u = GET_JIFFIES_USEC();
2485 unsigned long curr_time = jiffies;
2486
2487 if (info->break_detected_cnt) {
2488 /* Detect if this character is a new valid char or the
2489 * last char in a break sequence: If LSBits are 0 and
2490 * MSBits are high AND the time is close to the
2491 * previous interrupt we should discard it.
2492 */
2493 long elapsed_usec =
2494 (curr_time - info->last_rx_active) * (1000000/HZ) +
2495 curr_time_u - info->last_rx_active_usec;
2496 if (elapsed_usec < 2*info->char_time_usec) {
2497 DEBUG_LOG(info->line, "FBRK %i\n", info->line);
2498 /* Report as BREAK (error) and let
2499 * receive_chars_dma() handle it
2500 */
2501 info->errorcode = ERRCODE_SET_BREAK;
2502 } else {
2503 DEBUG_LOG(info->line, "Not end of BRK (V)%i\n", info->line);
2504 }
2505 DEBUG_LOG(info->line, "num brk %i\n", info->break_detected_cnt);
2506 }
2507
2508#ifdef SERIAL_DEBUG_INTR
2509 printk("** OK, disabling ser_interrupts\n");
2510#endif
2511 e100_disable_serial_data_irq(info);
2512 DINTR2(DEBUG_LOG(info->line, "ser_rx OK %d\n", info->line));
2513 info->break_detected_cnt = 0;
2514
2515 PROCSTAT(ser_stat[info->line].ser_ints_ok_cnt++);
2516 }
2517 /* Restarting the DMA never hurts */
2518 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, restart);
2519 START_FLUSH_FAST_TIMER(info, "ser_int");
2520 return info;
2521} /* handle_ser_rx_interrupt */
2522
2523static void handle_ser_tx_interrupt(struct e100_serial *info)
2524{
2525 unsigned long flags;
2526
2527 if (info->x_char) {
2528 unsigned char rstat;
2529 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar 0x%02X\n", info->x_char));
2530 local_irq_save(flags);
2531 rstat = info->ioport[REG_STATUS];
2532 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2533
2534 info->ioport[REG_TR_DATA] = info->x_char;
2535 info->icount.tx++;
2536 info->x_char = 0;
2537 /* We must enable since it is disabled in ser_interrupt */
2538 e100_enable_serial_tx_ready_irq(info);
2539 local_irq_restore(flags);
2540 return;
2541 }
2542 if (info->uses_dma_out) {
2543 unsigned char rstat;
2544 int i;
2545 /* We only use normal tx interrupt when sending x_char */
2546 DFLOW(DEBUG_LOG(info->line, "tx_int: xchar sent\n", 0));
2547 local_irq_save(flags);
2548 rstat = info->ioport[REG_STATUS];
2549 DFLOW(DEBUG_LOG(info->line, "stat %x\n", rstat));
2550 e100_disable_serial_tx_ready_irq(info);
2551 if (info->port.tty->stopped)
2552 rs_stop(info->port.tty);
2553 /* Enable the DMA channel and tell it to continue */
2554 e100_enable_txdma_channel(info);
2555 /* Wait 12 cycles before doing the DMA command */
2556 for(i = 6; i > 0; i--)
2557 nop();
2558
2559 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, continue);
2560 local_irq_restore(flags);
2561 return;
2562 }
2563 /* Normal char-by-char interrupt */
2564 if (info->xmit.head == info->xmit.tail
2565 || info->port.tty->stopped
2566 || info->port.tty->hw_stopped) {
2567 DFLOW(DEBUG_LOG(info->line, "tx_int: stopped %i\n",
2568 info->port.tty->stopped));
2569 e100_disable_serial_tx_ready_irq(info);
2570 info->tr_running = 0;
2571 return;
2572 }
2573 DINTR2(DEBUG_LOG(info->line, "tx_int %c\n", info->xmit.buf[info->xmit.tail]));
2574 /* Send a byte, rs485 timing is critical so turn of ints */
2575 local_irq_save(flags);
2576 info->ioport[REG_TR_DATA] = info->xmit.buf[info->xmit.tail];
2577 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
2578 info->icount.tx++;
2579 if (info->xmit.head == info->xmit.tail) {
2580#if defined(CONFIG_ETRAX_RS485) && defined(CONFIG_ETRAX_FAST_TIMER)
2581 if (info->rs485.flags & SER_RS485_ENABLED) {
2582 /* Set a short timer to toggle RTS */
2583 start_one_shot_timer(&fast_timers_rs485[info->line],
2584 rs485_toggle_rts_timer_function,
2585 (unsigned long)info,
2586 info->char_time_usec*2,
2587 "RS-485");
2588 }
2589#endif /* RS485 */
2590 info->last_tx_active_usec = GET_JIFFIES_USEC();
2591 info->last_tx_active = jiffies;
2592 e100_disable_serial_tx_ready_irq(info);
2593 info->tr_running = 0;
2594 DFLOW(DEBUG_LOG(info->line, "tx_int: stop2\n", 0));
2595 } else {
2596 /* We must enable since it is disabled in ser_interrupt */
2597 e100_enable_serial_tx_ready_irq(info);
2598 }
2599 local_irq_restore(flags);
2600
2601 if (CIRC_CNT(info->xmit.head,
2602 info->xmit.tail,
2603 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
2604 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
2605
2606} /* handle_ser_tx_interrupt */
2607
2608/* result of time measurements:
2609 * RX duration 54-60 us when doing something, otherwise 6-9 us
2610 * ser_int duration: just sending: 8-15 us normally, up to 73 us
2611 */
2612static irqreturn_t
2613ser_interrupt(int irq, void *dev_id)
2614{
2615 static volatile int tx_started = 0;
2616 struct e100_serial *info;
2617 int i;
2618 unsigned long flags;
2619 unsigned long irq_mask1_rd;
2620 unsigned long data_mask = (1 << (8+2*0)); /* ser0 data_avail */
2621 int handled = 0;
2622 static volatile unsigned long reentered_ready_mask = 0;
2623
2624 local_irq_save(flags);
2625 irq_mask1_rd = *R_IRQ_MASK1_RD;
2626 /* First handle all rx interrupts with ints disabled */
2627 info = rs_table;
2628 irq_mask1_rd &= e100_ser_int_mask;
2629 for (i = 0; i < NR_PORTS; i++) {
2630 /* Which line caused the data irq? */
2631 if (irq_mask1_rd & data_mask) {
2632 handled = 1;
2633 handle_ser_rx_interrupt(info);
2634 }
2635 info += 1;
2636 data_mask <<= 2;
2637 }
2638 /* Handle tx interrupts with interrupts enabled so we
2639 * can take care of new data interrupts while transmitting
2640 * We protect the tx part with the tx_started flag.
2641 * We disable the tr_ready interrupts we are about to handle and
2642 * unblock the serial interrupt so new serial interrupts may come.
2643 *
2644 * If we get a new interrupt:
2645 * - it migth be due to synchronous serial ports.
2646 * - serial irq will be blocked by general irq handler.
2647 * - async data will be handled above (sync will be ignored).
2648 * - tx_started flag will prevent us from trying to send again and
2649 * we will exit fast - no need to unblock serial irq.
2650 * - Next (sync) serial interrupt handler will be runned with
2651 * disabled interrupt due to restore_flags() at end of function,
2652 * so sync handler will not be preempted or reentered.
2653 */
2654 if (!tx_started) {
2655 unsigned long ready_mask;
2656 unsigned long
2657 tx_started = 1;
2658 /* Only the tr_ready interrupts left */
2659 irq_mask1_rd &= (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2660 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2661 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2662 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2663 while (irq_mask1_rd) {
2664 /* Disable those we are about to handle */
2665 *R_IRQ_MASK1_CLR = irq_mask1_rd;
2666 /* Unblock the serial interrupt */
2667 *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
2668
2669 local_irq_enable();
2670 ready_mask = (1 << (8+1+2*0)); /* ser0 tr_ready */
2671 info = rs_table;
2672 for (i = 0; i < NR_PORTS; i++) {
2673 /* Which line caused the ready irq? */
2674 if (irq_mask1_rd & ready_mask) {
2675 handled = 1;
2676 handle_ser_tx_interrupt(info);
2677 }
2678 info += 1;
2679 ready_mask <<= 2;
2680 }
2681 /* handle_ser_tx_interrupt enables tr_ready interrupts */
2682 local_irq_disable();
2683 /* Handle reentered TX interrupt */
2684 irq_mask1_rd = reentered_ready_mask;
2685 }
2686 local_irq_disable();
2687 tx_started = 0;
2688 } else {
2689 unsigned long ready_mask;
2690 ready_mask = irq_mask1_rd & (IO_MASK(R_IRQ_MASK1_RD, ser0_ready) |
2691 IO_MASK(R_IRQ_MASK1_RD, ser1_ready) |
2692 IO_MASK(R_IRQ_MASK1_RD, ser2_ready) |
2693 IO_MASK(R_IRQ_MASK1_RD, ser3_ready));
2694 if (ready_mask) {
2695 reentered_ready_mask |= ready_mask;
2696 /* Disable those we are about to handle */
2697 *R_IRQ_MASK1_CLR = ready_mask;
2698 DFLOW(DEBUG_LOG(SERIAL_DEBUG_LINE, "ser_int reentered with TX %X\n", ready_mask));
2699 }
2700 }
2701
2702 local_irq_restore(flags);
2703 return IRQ_RETVAL(handled);
2704} /* ser_interrupt */
2705#endif
2706
2707/*
2708 * -------------------------------------------------------------------
2709 * Here ends the serial interrupt routines.
2710 * -------------------------------------------------------------------
2711 */
2712
2713/*
2714 * This routine is used to handle the "bottom half" processing for the
2715 * serial driver, known also the "software interrupt" processing.
2716 * This processing is done at the kernel interrupt level, after the
2717 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
2718 * is where time-consuming activities which can not be done in the
2719 * interrupt driver proper are done; the interrupt driver schedules
2720 * them using rs_sched_event(), and they get done here.
2721 */
2722static void
2723do_softint(struct work_struct *work)
2724{
2725 struct e100_serial *info;
2726 struct tty_struct *tty;
2727
2728 info = container_of(work, struct e100_serial, work);
2729
2730 tty = info->port.tty;
2731 if (!tty)
2732 return;
2733
2734 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
2735 tty_wakeup(tty);
2736}
2737
2738static int
2739startup(struct e100_serial * info)
2740{
2741 unsigned long flags;
2742 unsigned long xmit_page;
2743 int i;
2744
2745 xmit_page = get_zeroed_page(GFP_KERNEL);
2746 if (!xmit_page)
2747 return -ENOMEM;
2748
2749 local_irq_save(flags);
2750
2751 /* if it was already initialized, skip this */
2752
2753 if (info->flags & ASYNC_INITIALIZED) {
2754 local_irq_restore(flags);
2755 free_page(xmit_page);
2756 return 0;
2757 }
2758
2759 if (info->xmit.buf)
2760 free_page(xmit_page);
2761 else
2762 info->xmit.buf = (unsigned char *) xmit_page;
2763
2764#ifdef SERIAL_DEBUG_OPEN
2765 printk("starting up ttyS%d (xmit_buf 0x%p)...\n", info->line, info->xmit.buf);
2766#endif
2767
2768#ifdef CONFIG_SVINTO_SIM
2769 /* Bits and pieces collected from below. Better to have them
2770 in one ifdef:ed clause than to mix in a lot of ifdefs,
2771 right? */
2772 if (info->port.tty)
2773 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2774
2775 info->xmit.head = info->xmit.tail = 0;
2776 info->first_recv_buffer = info->last_recv_buffer = NULL;
2777 info->recv_cnt = info->max_recv_cnt = 0;
2778
2779 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2780 info->rec_descr[i].buf = NULL;
2781
2782 /* No real action in the simulator, but may set info important
2783 to ioctl. */
2784 change_speed(info);
2785#else
2786
2787 /*
2788 * Clear the FIFO buffers and disable them
2789 * (they will be reenabled in change_speed())
2790 */
2791
2792 /*
2793 * Reset the DMA channels and make sure their interrupts are cleared
2794 */
2795
2796 if (info->dma_in_enabled) {
2797 info->uses_dma_in = 1;
2798 e100_enable_rxdma_channel(info);
2799
2800 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2801
2802 /* Wait until reset cycle is complete */
2803 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->icmdadr) ==
2804 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2805
2806 /* Make sure the irqs are cleared */
2807 *info->iclrintradr =
2808 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2809 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2810 } else {
2811 e100_disable_rxdma_channel(info);
2812 }
2813
2814 if (info->dma_out_enabled) {
2815 info->uses_dma_out = 1;
2816 e100_enable_txdma_channel(info);
2817 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2818
2819 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) ==
2820 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
2821
2822 /* Make sure the irqs are cleared */
2823 *info->oclrintradr =
2824 IO_STATE(R_DMA_CH6_CLR_INTR, clr_descr, do) |
2825 IO_STATE(R_DMA_CH6_CLR_INTR, clr_eop, do);
2826 } else {
2827 e100_disable_txdma_channel(info);
2828 }
2829
2830 if (info->port.tty)
2831 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2832
2833 info->xmit.head = info->xmit.tail = 0;
2834 info->first_recv_buffer = info->last_recv_buffer = NULL;
2835 info->recv_cnt = info->max_recv_cnt = 0;
2836
2837 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2838 info->rec_descr[i].buf = 0;
2839
2840 /*
2841 * and set the speed and other flags of the serial port
2842 * this will start the rx/tx as well
2843 */
2844#ifdef SERIAL_HANDLE_EARLY_ERRORS
2845 e100_enable_serial_data_irq(info);
2846#endif
2847 change_speed(info);
2848
2849 /* dummy read to reset any serial errors */
2850
2851 (void)info->ioport[REG_DATA];
2852
2853 /* enable the interrupts */
2854 if (info->uses_dma_out)
2855 e100_enable_txdma_irq(info);
2856
2857 e100_enable_rx_irq(info);
2858
2859 info->tr_running = 0; /* to be sure we don't lock up the transmitter */
2860
2861 /* setup the dma input descriptor and start dma */
2862
2863 start_receive(info);
2864
2865 /* for safety, make sure the descriptors last result is 0 bytes written */
2866
2867 info->tr_descr.sw_len = 0;
2868 info->tr_descr.hw_len = 0;
2869 info->tr_descr.status = 0;
2870
2871 /* enable RTS/DTR last */
2872
2873 e100_rts(info, 1);
2874 e100_dtr(info, 1);
2875
2876#endif /* CONFIG_SVINTO_SIM */
2877
2878 info->flags |= ASYNC_INITIALIZED;
2879
2880 local_irq_restore(flags);
2881 return 0;
2882}
2883
2884/*
2885 * This routine will shutdown a serial port; interrupts are disabled, and
2886 * DTR is dropped if the hangup on close termio flag is on.
2887 */
2888static void
2889shutdown(struct e100_serial * info)
2890{
2891 unsigned long flags;
2892 struct etrax_dma_descr *descr = info->rec_descr;
2893 struct etrax_recv_buffer *buffer;
2894 int i;
2895
2896#ifndef CONFIG_SVINTO_SIM
2897 /* shut down the transmitter and receiver */
2898 DFLOW(DEBUG_LOG(info->line, "shutdown %i\n", info->line));
2899 e100_disable_rx(info);
2900 info->ioport[REG_TR_CTRL] = (info->tx_ctrl &= ~0x40);
2901
2902 /* disable interrupts, reset dma channels */
2903 if (info->uses_dma_in) {
2904 e100_disable_rxdma_irq(info);
2905 *info->icmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2906 info->uses_dma_in = 0;
2907 } else {
2908 e100_disable_serial_data_irq(info);
2909 }
2910
2911 if (info->uses_dma_out) {
2912 e100_disable_txdma_irq(info);
2913 info->tr_running = 0;
2914 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
2915 info->uses_dma_out = 0;
2916 } else {
2917 e100_disable_serial_tx_ready_irq(info);
2918 info->tr_running = 0;
2919 }
2920
2921#endif /* CONFIG_SVINTO_SIM */
2922
2923 if (!(info->flags & ASYNC_INITIALIZED))
2924 return;
2925
2926#ifdef SERIAL_DEBUG_OPEN
2927 printk("Shutting down serial port %d (irq %d)....\n", info->line,
2928 info->irq);
2929#endif
2930
2931 local_irq_save(flags);
2932
2933 if (info->xmit.buf) {
2934 free_page((unsigned long)info->xmit.buf);
2935 info->xmit.buf = NULL;
2936 }
2937
2938 for (i = 0; i < SERIAL_RECV_DESCRIPTORS; i++)
2939 if (descr[i].buf) {
2940 buffer = phys_to_virt(descr[i].buf) - sizeof *buffer;
2941 kfree(buffer);
2942 descr[i].buf = 0;
2943 }
2944
2945 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL)) {
2946 /* hang up DTR and RTS if HUPCL is enabled */
2947 e100_dtr(info, 0);
2948 e100_rts(info, 0); /* could check CRTSCTS before doing this */
2949 }
2950
2951 if (info->port.tty)
2952 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2953
2954 info->flags &= ~ASYNC_INITIALIZED;
2955 local_irq_restore(flags);
2956}
2957
2958
2959/* change baud rate and other assorted parameters */
2960
2961static void
2962change_speed(struct e100_serial *info)
2963{
2964 unsigned int cflag;
2965 unsigned long xoff;
2966 unsigned long flags;
2967 /* first some safety checks */
2968
2969 if (!info->port.tty || !info->port.tty->termios)
2970 return;
2971 if (!info->ioport)
2972 return;
2973
2974 cflag = info->port.tty->termios->c_cflag;
2975
2976 /* possibly, the tx/rx should be disabled first to do this safely */
2977
2978 /* change baud-rate and write it to the hardware */
2979 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) {
2980 /* Special baudrate */
2981 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
2982 unsigned long alt_source =
2983 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
2984 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
2985 /* R_ALT_SER_BAUDRATE selects the source */
2986 DBAUD(printk("Custom baudrate: baud_base/divisor %lu/%i\n",
2987 (unsigned long)info->baud_base, info->custom_divisor));
2988 if (info->baud_base == SERIAL_PRESCALE_BASE) {
2989 /* 0, 2-65535 (0=65536) */
2990 u16 divisor = info->custom_divisor;
2991 /* R_SERIAL_PRESCALE (upper 16 bits of R_CLOCK_PRESCALE) */
2992 /* baudrate is 3.125MHz/custom_divisor */
2993 alt_source =
2994 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, prescale) |
2995 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, prescale);
2996 alt_source = 0x11;
2997 DBAUD(printk("Writing SERIAL_PRESCALE: divisor %i\n", divisor));
2998 *R_SERIAL_PRESCALE = divisor;
2999 info->baud = SERIAL_PRESCALE_BASE/divisor;
3000 }
3001#ifdef CONFIG_ETRAX_EXTERN_PB6CLK_ENABLED
3002 else if ((info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8 &&
3003 info->custom_divisor == 1) ||
3004 (info->baud_base==CONFIG_ETRAX_EXTERN_PB6CLK_FREQ &&
3005 info->custom_divisor == 8)) {
3006 /* ext_clk selected */
3007 alt_source =
3008 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, extern) |
3009 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, extern);
3010 DBAUD(printk("using external baudrate: %lu\n", CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8));
3011 info->baud = CONFIG_ETRAX_EXTERN_PB6CLK_FREQ/8;
3012 }
3013#endif
3014 else
3015 {
3016 /* Bad baudbase, we don't support using timer0
3017 * for baudrate.
3018 */
3019 printk(KERN_WARNING "Bad baud_base/custom_divisor: %lu/%i\n",
3020 (unsigned long)info->baud_base, info->custom_divisor);
3021 }
3022 r_alt_ser_baudrate_shadow &= ~mask;
3023 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3024 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3025 } else {
3026 /* Normal baudrate */
3027 /* Make sure we use normal baudrate */
3028 u32 mask = 0xFF << (info->line*8); /* Each port has 8 bits */
3029 unsigned long alt_source =
3030 IO_STATE(R_ALT_SER_BAUDRATE, ser0_rec, normal) |
3031 IO_STATE(R_ALT_SER_BAUDRATE, ser0_tr, normal);
3032 r_alt_ser_baudrate_shadow &= ~mask;
3033 r_alt_ser_baudrate_shadow |= (alt_source << (info->line*8));
3034#ifndef CONFIG_SVINTO_SIM
3035 *R_ALT_SER_BAUDRATE = r_alt_ser_baudrate_shadow;
3036#endif /* CONFIG_SVINTO_SIM */
3037
3038 info->baud = cflag_to_baud(cflag);
3039#ifndef CONFIG_SVINTO_SIM
3040 info->ioport[REG_BAUD] = cflag_to_etrax_baud(cflag);
3041#endif /* CONFIG_SVINTO_SIM */
3042 }
3043
3044#ifndef CONFIG_SVINTO_SIM
3045 /* start with default settings and then fill in changes */
3046 local_irq_save(flags);
3047 /* 8 bit, no/even parity */
3048 info->rx_ctrl &= ~(IO_MASK(R_SERIAL0_REC_CTRL, rec_bitnr) |
3049 IO_MASK(R_SERIAL0_REC_CTRL, rec_par_en) |
3050 IO_MASK(R_SERIAL0_REC_CTRL, rec_par));
3051
3052 /* 8 bit, no/even parity, 1 stop bit, no cts */
3053 info->tx_ctrl &= ~(IO_MASK(R_SERIAL0_TR_CTRL, tr_bitnr) |
3054 IO_MASK(R_SERIAL0_TR_CTRL, tr_par_en) |
3055 IO_MASK(R_SERIAL0_TR_CTRL, tr_par) |
3056 IO_MASK(R_SERIAL0_TR_CTRL, stop_bits) |
3057 IO_MASK(R_SERIAL0_TR_CTRL, auto_cts));
3058
3059 if ((cflag & CSIZE) == CS7) {
3060 /* set 7 bit mode */
3061 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
3062 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
3063 }
3064
3065 if (cflag & CSTOPB) {
3066 /* set 2 stop bit mode */
3067 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, two_bits);
3068 }
3069
3070 if (cflag & PARENB) {
3071 /* enable parity */
3072 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
3073 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
3074 }
3075
3076 if (cflag & CMSPAR) {
3077 /* enable stick parity, PARODD mean Mark which matches ETRAX */
3078 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, stick);
3079 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, stick);
3080 }
3081 if (cflag & PARODD) {
3082 /* set odd parity (or Mark if CMSPAR) */
3083 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd);
3084 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd);
3085 }
3086
3087 if (cflag & CRTSCTS) {
3088 /* enable automatic CTS handling */
3089 DFLOW(DEBUG_LOG(info->line, "FLOW auto_cts enabled\n", 0));
3090 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, active);
3091 }
3092
3093 /* make sure the tx and rx are enabled */
3094
3095 info->tx_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable);
3096 info->rx_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
3097
3098 /* actually write the control regs to the hardware */
3099
3100 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
3101 info->ioport[REG_REC_CTRL] = info->rx_ctrl;
3102 xoff = IO_FIELD(R_SERIAL0_XOFF, xoff_char, STOP_CHAR(info->port.tty));
3103 xoff |= IO_STATE(R_SERIAL0_XOFF, tx_stop, enable);
3104 if (info->port.tty->termios->c_iflag & IXON ) {
3105 DFLOW(DEBUG_LOG(info->line, "FLOW XOFF enabled 0x%02X\n",
3106 STOP_CHAR(info->port.tty)));
3107 xoff |= IO_STATE(R_SERIAL0_XOFF, auto_xoff, enable);
3108 }
3109
3110 *((unsigned long *)&info->ioport[REG_XOFF]) = xoff;
3111 local_irq_restore(flags);
3112#endif /* !CONFIG_SVINTO_SIM */
3113
3114 update_char_time(info);
3115
3116} /* change_speed */
3117
3118/* start transmitting chars NOW */
3119
3120static void
3121rs_flush_chars(struct tty_struct *tty)
3122{
3123 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3124 unsigned long flags;
3125
3126 if (info->tr_running ||
3127 info->xmit.head == info->xmit.tail ||
3128 tty->stopped ||
3129 tty->hw_stopped ||
3130 !info->xmit.buf)
3131 return;
3132
3133#ifdef SERIAL_DEBUG_FLOW
3134 printk("rs_flush_chars\n");
3135#endif
3136
3137 /* this protection might not exactly be necessary here */
3138
3139 local_irq_save(flags);
3140 start_transmit(info);
3141 local_irq_restore(flags);
3142}
3143
3144static int rs_raw_write(struct tty_struct *tty,
3145 const unsigned char *buf, int count)
3146{
3147 int c, ret = 0;
3148 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3149 unsigned long flags;
3150
3151 /* first some sanity checks */
3152
3153 if (!tty || !info->xmit.buf || !tmp_buf)
3154 return 0;
3155
3156#ifdef SERIAL_DEBUG_DATA
3157 if (info->line == SERIAL_DEBUG_LINE)
3158 printk("rs_raw_write (%d), status %d\n",
3159 count, info->ioport[REG_STATUS]);
3160#endif
3161
3162#ifdef CONFIG_SVINTO_SIM
3163 /* Really simple. The output is here and now. */
3164 SIMCOUT(buf, count);
3165 return count;
3166#endif
3167 local_save_flags(flags);
3168 DFLOW(DEBUG_LOG(info->line, "write count %i ", count));
3169 DFLOW(DEBUG_LOG(info->line, "ldisc %i\n", tty->ldisc.chars_in_buffer(tty)));
3170
3171
3172 /* The local_irq_disable/restore_flags pairs below are needed
3173 * because the DMA interrupt handler moves the info->xmit values.
3174 * the memcpy needs to be in the critical region unfortunately,
3175 * because we need to read xmit values, memcpy, write xmit values
3176 * in one atomic operation... this could perhaps be avoided by
3177 * more clever design.
3178 */
3179 local_irq_disable();
3180 while (count) {
3181 c = CIRC_SPACE_TO_END(info->xmit.head,
3182 info->xmit.tail,
3183 SERIAL_XMIT_SIZE);
3184
3185 if (count < c)
3186 c = count;
3187 if (c <= 0)
3188 break;
3189
3190 memcpy(info->xmit.buf + info->xmit.head, buf, c);
3191 info->xmit.head = (info->xmit.head + c) &
3192 (SERIAL_XMIT_SIZE-1);
3193 buf += c;
3194 count -= c;
3195 ret += c;
3196 }
3197 local_irq_restore(flags);
3198
3199 /* enable transmitter if not running, unless the tty is stopped
3200 * this does not need IRQ protection since if tr_running == 0
3201 * the IRQ's are not running anyway for this port.
3202 */
3203 DFLOW(DEBUG_LOG(info->line, "write ret %i\n", ret));
3204
3205 if (info->xmit.head != info->xmit.tail &&
3206 !tty->stopped &&
3207 !tty->hw_stopped &&
3208 !info->tr_running) {
3209 start_transmit(info);
3210 }
3211
3212 return ret;
3213} /* raw_raw_write() */
3214
3215static int
3216rs_write(struct tty_struct *tty,
3217 const unsigned char *buf, int count)
3218{
3219#if defined(CONFIG_ETRAX_RS485)
3220 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3221
3222 if (info->rs485.flags & SER_RS485_ENABLED)
3223 {
3224 /* If we are in RS-485 mode, we need to toggle RTS and disable
3225 * the receiver before initiating a DMA transfer
3226 */
3227#ifdef CONFIG_ETRAX_FAST_TIMER
3228 /* Abort any started timer */
3229 fast_timers_rs485[info->line].function = NULL;
3230 del_fast_timer(&fast_timers_rs485[info->line]);
3231#endif
3232 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_ON_SEND));
3233#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3234 e100_disable_rx(info);
3235 e100_enable_rx_irq(info);
3236#endif
3237 if ((info->rs485.flags & SER_RS485_RTS_BEFORE_SEND) &&
3238 (info->rs485.delay_rts_before_send > 0))
3239 msleep(info->rs485.delay_rts_before_send);
3240 }
3241#endif /* CONFIG_ETRAX_RS485 */
3242
3243 count = rs_raw_write(tty, buf, count);
3244
3245#if defined(CONFIG_ETRAX_RS485)
3246 if (info->rs485.flags & SER_RS485_ENABLED)
3247 {
3248 unsigned int val;
3249 /* If we are in RS-485 mode the following has to be done:
3250 * wait until DMA is ready
3251 * wait on transmit shift register
3252 * toggle RTS
3253 * enable the receiver
3254 */
3255
3256 /* Sleep until all sent */
3257 tty_wait_until_sent(tty, 0);
3258#ifdef CONFIG_ETRAX_FAST_TIMER
3259 /* Now sleep a little more so that shift register is empty */
3260 schedule_usleep(info->char_time_usec * 2);
3261#endif
3262 /* wait on transmit shift register */
3263 do{
3264 get_lsr_info(info, &val);
3265 }while (!(val & TIOCSER_TEMT));
3266
3267 e100_rts(info, (info->rs485.flags & SER_RS485_RTS_AFTER_SEND));
3268
3269#if defined(CONFIG_ETRAX_RS485_DISABLE_RECEIVER)
3270 e100_enable_rx(info);
3271 e100_enable_rxdma_irq(info);
3272#endif
3273 }
3274#endif /* CONFIG_ETRAX_RS485 */
3275
3276 return count;
3277} /* rs_write */
3278
3279
3280/* how much space is available in the xmit buffer? */
3281
3282static int
3283rs_write_room(struct tty_struct *tty)
3284{
3285 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3286
3287 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3288}
3289
3290/* How many chars are in the xmit buffer?
3291 * This does not include any chars in the transmitter FIFO.
3292 * Use wait_until_sent for waiting for FIFO drain.
3293 */
3294
3295static int
3296rs_chars_in_buffer(struct tty_struct *tty)
3297{
3298 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3299
3300 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
3301}
3302
3303/* discard everything in the xmit buffer */
3304
3305static void
3306rs_flush_buffer(struct tty_struct *tty)
3307{
3308 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3309 unsigned long flags;
3310
3311 local_irq_save(flags);
3312 info->xmit.head = info->xmit.tail = 0;
3313 local_irq_restore(flags);
3314
3315 tty_wakeup(tty);
3316}
3317
3318/*
3319 * This function is used to send a high-priority XON/XOFF character to
3320 * the device
3321 *
3322 * Since we use DMA we don't check for info->x_char in transmit_chars_dma(),
3323 * but we do it in handle_ser_tx_interrupt().
3324 * We disable DMA channel and enable tx ready interrupt and write the
3325 * character when possible.
3326 */
3327static void rs_send_xchar(struct tty_struct *tty, char ch)
3328{
3329 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3330 unsigned long flags;
3331 local_irq_save(flags);
3332 if (info->uses_dma_out) {
3333 /* Put the DMA on hold and disable the channel */
3334 *info->ocmdadr = IO_STATE(R_DMA_CH6_CMD, cmd, hold);
3335 while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *info->ocmdadr) !=
3336 IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, hold));
3337 e100_disable_txdma_channel(info);
3338 }
3339
3340 /* Must make sure transmitter is not stopped before we can transmit */
3341 if (tty->stopped)
3342 rs_start(tty);
3343
3344 /* Enable manual transmit interrupt and send from there */
3345 DFLOW(DEBUG_LOG(info->line, "rs_send_xchar 0x%02X\n", ch));
3346 info->x_char = ch;
3347 e100_enable_serial_tx_ready_irq(info);
3348 local_irq_restore(flags);
3349}
3350
3351/*
3352 * ------------------------------------------------------------
3353 * rs_throttle()
3354 *
3355 * This routine is called by the upper-layer tty layer to signal that
3356 * incoming characters should be throttled.
3357 * ------------------------------------------------------------
3358 */
3359static void
3360rs_throttle(struct tty_struct * tty)
3361{
3362 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3363#ifdef SERIAL_DEBUG_THROTTLE
3364 char buf[64];
3365
3366 printk("throttle %s: %lu....\n", tty_name(tty, buf),
3367 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3368#endif
3369 DFLOW(DEBUG_LOG(info->line,"rs_throttle %lu\n", tty->ldisc.chars_in_buffer(tty)));
3370
3371 /* Do RTS before XOFF since XOFF might take some time */
3372 if (tty->termios->c_cflag & CRTSCTS) {
3373 /* Turn off RTS line */
3374 e100_rts(info, 0);
3375 }
3376 if (I_IXOFF(tty))
3377 rs_send_xchar(tty, STOP_CHAR(tty));
3378
3379}
3380
3381static void
3382rs_unthrottle(struct tty_struct * tty)
3383{
3384 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3385#ifdef SERIAL_DEBUG_THROTTLE
3386 char buf[64];
3387
3388 printk("unthrottle %s: %lu....\n", tty_name(tty, buf),
3389 (unsigned long)tty->ldisc.chars_in_buffer(tty));
3390#endif
3391 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle ldisc %d\n", tty->ldisc.chars_in_buffer(tty)));
3392 DFLOW(DEBUG_LOG(info->line,"rs_unthrottle flip.count: %i\n", tty->flip.count));
3393 /* Do RTS before XOFF since XOFF might take some time */
3394 if (tty->termios->c_cflag & CRTSCTS) {
3395 /* Assert RTS line */
3396 e100_rts(info, 1);
3397 }
3398
3399 if (I_IXOFF(tty)) {
3400 if (info->x_char)
3401 info->x_char = 0;
3402 else
3403 rs_send_xchar(tty, START_CHAR(tty));
3404 }
3405
3406}
3407
3408/*
3409 * ------------------------------------------------------------
3410 * rs_ioctl() and friends
3411 * ------------------------------------------------------------
3412 */
3413
3414static int
3415get_serial_info(struct e100_serial * info,
3416 struct serial_struct * retinfo)
3417{
3418 struct serial_struct tmp;
3419
3420 /* this is all probably wrong, there are a lot of fields
3421 * here that we don't have in e100_serial and maybe we
3422 * should set them to something else than 0.
3423 */
3424
3425 if (!retinfo)
3426 return -EFAULT;
3427 memset(&tmp, 0, sizeof(tmp));
3428 tmp.type = info->type;
3429 tmp.line = info->line;
3430 tmp.port = (int)info->ioport;
3431 tmp.irq = info->irq;
3432 tmp.flags = info->flags;
3433 tmp.baud_base = info->baud_base;
3434 tmp.close_delay = info->close_delay;
3435 tmp.closing_wait = info->closing_wait;
3436 tmp.custom_divisor = info->custom_divisor;
3437 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3438 return -EFAULT;
3439 return 0;
3440}
3441
3442static int
3443set_serial_info(struct e100_serial *info,
3444 struct serial_struct *new_info)
3445{
3446 struct serial_struct new_serial;
3447 struct e100_serial old_info;
3448 int retval = 0;
3449
3450 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
3451 return -EFAULT;
3452
3453 old_info = *info;
3454
3455 if (!capable(CAP_SYS_ADMIN)) {
3456 if ((new_serial.type != info->type) ||
3457 (new_serial.close_delay != info->close_delay) ||
3458 ((new_serial.flags & ~ASYNC_USR_MASK) !=
3459 (info->flags & ~ASYNC_USR_MASK)))
3460 return -EPERM;
3461 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
3462 (new_serial.flags & ASYNC_USR_MASK));
3463 goto check_and_exit;
3464 }
3465
3466 if (info->count > 1)
3467 return -EBUSY;
3468
3469 /*
3470 * OK, past this point, all the error checking has been done.
3471 * At this point, we start making changes.....
3472 */
3473
3474 info->baud_base = new_serial.baud_base;
3475 info->flags = ((info->flags & ~ASYNC_FLAGS) |
3476 (new_serial.flags & ASYNC_FLAGS));
3477 info->custom_divisor = new_serial.custom_divisor;
3478 info->type = new_serial.type;
3479 info->close_delay = new_serial.close_delay;
3480 info->closing_wait = new_serial.closing_wait;
3481 info->port.tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3482
3483 check_and_exit:
3484 if (info->flags & ASYNC_INITIALIZED) {
3485 change_speed(info);
3486 } else
3487 retval = startup(info);
3488 return retval;
3489}
3490
3491/*
3492 * get_lsr_info - get line status register info
3493 *
3494 * Purpose: Let user call ioctl() to get info when the UART physically
3495 * is emptied. On bus types like RS485, the transmitter must
3496 * release the bus after transmitting. This must be done when
3497 * the transmit shift register is empty, not be done when the
3498 * transmit holding register is empty. This functionality
3499 * allows an RS485 driver to be written in user space.
3500 */
3501static int
3502get_lsr_info(struct e100_serial * info, unsigned int *value)
3503{
3504 unsigned int result = TIOCSER_TEMT;
3505#ifndef CONFIG_SVINTO_SIM
3506 unsigned long curr_time = jiffies;
3507 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3508 unsigned long elapsed_usec =
3509 (curr_time - info->last_tx_active) * 1000000/HZ +
3510 curr_time_usec - info->last_tx_active_usec;
3511
3512 if (info->xmit.head != info->xmit.tail ||
3513 elapsed_usec < 2*info->char_time_usec) {
3514 result = 0;
3515 }
3516#endif
3517
3518 if (copy_to_user(value, &result, sizeof(int)))
3519 return -EFAULT;
3520 return 0;
3521}
3522
3523#ifdef SERIAL_DEBUG_IO
3524struct state_str
3525{
3526 int state;
3527 const char *str;
3528};
3529
3530const struct state_str control_state_str[] = {
3531 {TIOCM_DTR, "DTR" },
3532 {TIOCM_RTS, "RTS"},
3533 {TIOCM_ST, "ST?" },
3534 {TIOCM_SR, "SR?" },
3535 {TIOCM_CTS, "CTS" },
3536 {TIOCM_CD, "CD" },
3537 {TIOCM_RI, "RI" },
3538 {TIOCM_DSR, "DSR" },
3539 {0, NULL }
3540};
3541
3542char *get_control_state_str(int MLines, char *s)
3543{
3544 int i = 0;
3545
3546 s[0]='\0';
3547 while (control_state_str[i].str != NULL) {
3548 if (MLines & control_state_str[i].state) {
3549 if (s[0] != '\0') {
3550 strcat(s, ", ");
3551 }
3552 strcat(s, control_state_str[i].str);
3553 }
3554 i++;
3555 }
3556 return s;
3557}
3558#endif
3559
3560static int
3561rs_break(struct tty_struct *tty, int break_state)
3562{
3563 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3564 unsigned long flags;
3565
3566 if (!info->ioport)
3567 return -EIO;
3568
3569 local_irq_save(flags);
3570 if (break_state == -1) {
3571 /* Go to manual mode and set the txd pin to 0 */
3572 /* Clear bit 7 (txd) and 6 (tr_enable) */
3573 info->tx_ctrl &= 0x3F;
3574 } else {
3575 /* Set bit 7 (txd) and 6 (tr_enable) */
3576 info->tx_ctrl |= (0x80 | 0x40);
3577 }
3578 info->ioport[REG_TR_CTRL] = info->tx_ctrl;
3579 local_irq_restore(flags);
3580 return 0;
3581}
3582
3583static int
3584rs_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
3585{
3586 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3587 unsigned long flags;
3588
3589 local_irq_save(flags);
3590
3591 if (clear & TIOCM_RTS)
3592 e100_rts(info, 0);
3593 if (clear & TIOCM_DTR)
3594 e100_dtr(info, 0);
3595 /* Handle FEMALE behaviour */
3596 if (clear & TIOCM_RI)
3597 e100_ri_out(info, 0);
3598 if (clear & TIOCM_CD)
3599 e100_cd_out(info, 0);
3600
3601 if (set & TIOCM_RTS)
3602 e100_rts(info, 1);
3603 if (set & TIOCM_DTR)
3604 e100_dtr(info, 1);
3605 /* Handle FEMALE behaviour */
3606 if (set & TIOCM_RI)
3607 e100_ri_out(info, 1);
3608 if (set & TIOCM_CD)
3609 e100_cd_out(info, 1);
3610
3611 local_irq_restore(flags);
3612 return 0;
3613}
3614
3615static int
3616rs_tiocmget(struct tty_struct *tty)
3617{
3618 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3619 unsigned int result;
3620 unsigned long flags;
3621
3622 local_irq_save(flags);
3623
3624 result =
3625 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3626 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3627 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3628 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3629 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3630 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3631
3632 local_irq_restore(flags);
3633
3634#ifdef SERIAL_DEBUG_IO
3635 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3636 info->line, result, result);
3637 {
3638 char s[100];
3639
3640 get_control_state_str(result, s);
3641 printk(KERN_DEBUG "state: %s\n", s);
3642 }
3643#endif
3644 return result;
3645
3646}
3647
3648
3649static int
3650rs_ioctl(struct tty_struct *tty,
3651 unsigned int cmd, unsigned long arg)
3652{
3653 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3654
3655 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3656 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3657 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3658 if (tty->flags & (1 << TTY_IO_ERROR))
3659 return -EIO;
3660 }
3661
3662 switch (cmd) {
3663 case TIOCGSERIAL:
3664 return get_serial_info(info,
3665 (struct serial_struct *) arg);
3666 case TIOCSSERIAL:
3667 return set_serial_info(info,
3668 (struct serial_struct *) arg);
3669 case TIOCSERGETLSR: /* Get line status register */
3670 return get_lsr_info(info, (unsigned int *) arg);
3671
3672 case TIOCSERGSTRUCT:
3673 if (copy_to_user((struct e100_serial *) arg,
3674 info, sizeof(struct e100_serial)))
3675 return -EFAULT;
3676 return 0;
3677
3678#if defined(CONFIG_ETRAX_RS485)
3679 case TIOCSERSETRS485:
3680 {
3681 /* In this ioctl we still use the old structure
3682 * rs485_control for backward compatibility
3683 * (if we use serial_rs485, then old user-level code
3684 * wouldn't work anymore...).
3685 * The use of this ioctl is deprecated: use TIOCSRS485
3686 * instead.*/
3687 struct rs485_control rs485ctrl;
3688 struct serial_rs485 rs485data;
3689 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
3690 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3691 sizeof(rs485ctrl)))
3692 return -EFAULT;
3693
3694 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3695 rs485data.flags = 0;
3696 if (rs485data.delay_rts_before_send != 0)
3697 rs485data.flags |= SER_RS485_RTS_BEFORE_SEND;
3698 else
3699 rs485data.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
3700
3701 if (rs485ctrl.enabled)
3702 rs485data.flags |= SER_RS485_ENABLED;
3703 else
3704 rs485data.flags &= ~(SER_RS485_ENABLED);
3705
3706 if (rs485ctrl.rts_on_send)
3707 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3708 else
3709 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3710
3711 if (rs485ctrl.rts_after_sent)
3712 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3713 else
3714 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3715
3716 return e100_enable_rs485(tty, &rs485data);
3717 }
3718
3719 case TIOCSRS485:
3720 {
3721 /* This is the new version of TIOCSRS485, with new
3722 * data structure serial_rs485 */
3723 struct serial_rs485 rs485data;
3724 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3725 sizeof(rs485data)))
3726 return -EFAULT;
3727
3728 return e100_enable_rs485(tty, &rs485data);
3729 }
3730
3731 case TIOCGRS485:
3732 {
3733 struct serial_rs485 *rs485data =
3734 &(((struct e100_serial *)tty->driver_data)->rs485);
3735 /* This is the ioctl to get RS485 data from user-space */
3736 if (copy_to_user((struct serial_rs485 *) arg,
3737 rs485data,
3738 sizeof(struct serial_rs485)))
3739 return -EFAULT;
3740 break;
3741 }
3742
3743 case TIOCSERWRRS485:
3744 {
3745 struct rs485_write rs485wr;
3746 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3747 sizeof(rs485wr)))
3748 return -EFAULT;
3749
3750 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3751 }
3752#endif
3753
3754 default:
3755 return -ENOIOCTLCMD;
3756 }
3757 return 0;
3758}
3759
3760static void
3761rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3762{
3763 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3764
3765 change_speed(info);
3766
3767 /* Handle turning off CRTSCTS */
3768 if ((old_termios->c_cflag & CRTSCTS) &&
3769 !(tty->termios->c_cflag & CRTSCTS)) {
3770 tty->hw_stopped = 0;
3771 rs_start(tty);
3772 }
3773
3774}
3775
3776/*
3777 * ------------------------------------------------------------
3778 * rs_close()
3779 *
3780 * This routine is called when the serial port gets closed. First, we
3781 * wait for the last remaining data to be sent. Then, we unlink its
3782 * S structure from the interrupt chain if necessary, and we free
3783 * that IRQ if nothing is left in the chain.
3784 * ------------------------------------------------------------
3785 */
3786static void
3787rs_close(struct tty_struct *tty, struct file * filp)
3788{
3789 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3790 unsigned long flags;
3791
3792 if (!info)
3793 return;
3794
3795 /* interrupts are disabled for this entire function */
3796
3797 local_irq_save(flags);
3798
3799 if (tty_hung_up_p(filp)) {
3800 local_irq_restore(flags);
3801 return;
3802 }
3803
3804#ifdef SERIAL_DEBUG_OPEN
3805 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3806 info->line, info->count);
3807#endif
3808 if ((tty->count == 1) && (info->count != 1)) {
3809 /*
3810 * Uh, oh. tty->count is 1, which means that the tty
3811 * structure will be freed. Info->count should always
3812 * be one in these conditions. If it's greater than
3813 * one, we've got real problems, since it means the
3814 * serial port won't be shutdown.
3815 */
3816 printk(KERN_CRIT
3817 "rs_close: bad serial port count; tty->count is 1, "
3818 "info->count is %d\n", info->count);
3819 info->count = 1;
3820 }
3821 if (--info->count < 0) {
3822 printk(KERN_CRIT "rs_close: bad serial port count for ttyS%d: %d\n",
3823 info->line, info->count);
3824 info->count = 0;
3825 }
3826 if (info->count) {
3827 local_irq_restore(flags);
3828 return;
3829 }
3830 info->flags |= ASYNC_CLOSING;
3831 /*
3832 * Save the termios structure, since this port may have
3833 * separate termios for callout and dialin.
3834 */
3835 if (info->flags & ASYNC_NORMAL_ACTIVE)
3836 info->normal_termios = *tty->termios;
3837 /*
3838 * Now we wait for the transmit buffer to clear; and we notify
3839 * the line discipline to only process XON/XOFF characters.
3840 */
3841 tty->closing = 1;
3842 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
3843 tty_wait_until_sent(tty, info->closing_wait);
3844 /*
3845 * At this point we stop accepting input. To do this, we
3846 * disable the serial receiver and the DMA receive interrupt.
3847 */
3848#ifdef SERIAL_HANDLE_EARLY_ERRORS
3849 e100_disable_serial_data_irq(info);
3850#endif
3851
3852#ifndef CONFIG_SVINTO_SIM
3853 e100_disable_rx(info);
3854 e100_disable_rx_irq(info);
3855
3856 if (info->flags & ASYNC_INITIALIZED) {
3857 /*
3858 * Before we drop DTR, make sure the UART transmitter
3859 * has completely drained; this is especially
3860 * important as we have a transmit FIFO!
3861 */
3862 rs_wait_until_sent(tty, HZ);
3863 }
3864#endif
3865
3866 shutdown(info);
3867 rs_flush_buffer(tty);
3868 tty_ldisc_flush(tty);
3869 tty->closing = 0;
3870 info->event = 0;
3871 info->port.tty = NULL;
3872 if (info->blocked_open) {
3873 if (info->close_delay)
3874 schedule_timeout_interruptible(info->close_delay);
3875 wake_up_interruptible(&info->open_wait);
3876 }
3877 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
3878 wake_up_interruptible(&info->close_wait);
3879 local_irq_restore(flags);
3880
3881 /* port closed */
3882
3883#if defined(CONFIG_ETRAX_RS485)
3884 if (info->rs485.flags & SER_RS485_ENABLED) {
3885 info->rs485.flags &= ~(SER_RS485_ENABLED);
3886#if defined(CONFIG_ETRAX_RS485_ON_PA)
3887 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3888#endif
3889#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
3890 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3891 rs485_port_g_bit, 0);
3892#endif
3893#if defined(CONFIG_ETRAX_RS485_LTC1387)
3894 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3895 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 0);
3896 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3897 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 0);
3898#endif
3899 }
3900#endif
3901
3902 /*
3903 * Release any allocated DMA irq's.
3904 */
3905 if (info->dma_in_enabled) {
3906 free_irq(info->dma_in_irq_nbr, info);
3907 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3908 info->uses_dma_in = 0;
3909#ifdef SERIAL_DEBUG_OPEN
3910 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3911 info->dma_in_irq_description);
3912#endif
3913 }
3914 if (info->dma_out_enabled) {
3915 free_irq(info->dma_out_irq_nbr, info);
3916 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3917 info->uses_dma_out = 0;
3918#ifdef SERIAL_DEBUG_OPEN
3919 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3920 info->dma_out_irq_description);
3921#endif
3922 }
3923}
3924
3925/*
3926 * rs_wait_until_sent() --- wait until the transmitter is empty
3927 */
3928static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3929{
3930 unsigned long orig_jiffies;
3931 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3932 unsigned long curr_time = jiffies;
3933 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3934 long elapsed_usec =
3935 (curr_time - info->last_tx_active) * (1000000/HZ) +
3936 curr_time_usec - info->last_tx_active_usec;
3937
3938 /*
3939 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3940 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3941 */
3942 orig_jiffies = jiffies;
3943 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3944 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3945 (elapsed_usec < 2*info->char_time_usec)) {
3946 schedule_timeout_interruptible(1);
3947 if (signal_pending(current))
3948 break;
3949 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3950 break;
3951 curr_time = jiffies;
3952 curr_time_usec = GET_JIFFIES_USEC();
3953 elapsed_usec =
3954 (curr_time - info->last_tx_active) * (1000000/HZ) +
3955 curr_time_usec - info->last_tx_active_usec;
3956 }
3957 set_current_state(TASK_RUNNING);
3958}
3959
3960/*
3961 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3962 */
3963void
3964rs_hangup(struct tty_struct *tty)
3965{
3966 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3967
3968 rs_flush_buffer(tty);
3969 shutdown(info);
3970 info->event = 0;
3971 info->count = 0;
3972 info->flags &= ~ASYNC_NORMAL_ACTIVE;
3973 info->port.tty = NULL;
3974 wake_up_interruptible(&info->open_wait);
3975}
3976
3977/*
3978 * ------------------------------------------------------------
3979 * rs_open() and friends
3980 * ------------------------------------------------------------
3981 */
3982static int
3983block_til_ready(struct tty_struct *tty, struct file * filp,
3984 struct e100_serial *info)
3985{
3986 DECLARE_WAITQUEUE(wait, current);
3987 unsigned long flags;
3988 int retval;
3989 int do_clocal = 0, extra_count = 0;
3990
3991 /*
3992 * If the device is in the middle of being closed, then block
3993 * until it's done, and then try again.
3994 */
3995 if (tty_hung_up_p(filp) ||
3996 (info->flags & ASYNC_CLOSING)) {
3997 wait_event_interruptible_tty(info->close_wait,
3998 !(info->flags & ASYNC_CLOSING));
3999#ifdef SERIAL_DO_RESTART
4000 if (info->flags & ASYNC_HUP_NOTIFY)
4001 return -EAGAIN;
4002 else
4003 return -ERESTARTSYS;
4004#else
4005 return -EAGAIN;
4006#endif
4007 }
4008
4009 /*
4010 * If non-blocking mode is set, or the port is not enabled,
4011 * then make the check up front and then exit.
4012 */
4013 if ((filp->f_flags & O_NONBLOCK) ||
4014 (tty->flags & (1 << TTY_IO_ERROR))) {
4015 info->flags |= ASYNC_NORMAL_ACTIVE;
4016 return 0;
4017 }
4018
4019 if (tty->termios->c_cflag & CLOCAL) {
4020 do_clocal = 1;
4021 }
4022
4023 /*
4024 * Block waiting for the carrier detect and the line to become
4025 * free (i.e., not in use by the callout). While we are in
4026 * this loop, info->count is dropped by one, so that
4027 * rs_close() knows when to free things. We restore it upon
4028 * exit, either normal or abnormal.
4029 */
4030 retval = 0;
4031 add_wait_queue(&info->open_wait, &wait);
4032#ifdef SERIAL_DEBUG_OPEN
4033 printk("block_til_ready before block: ttyS%d, count = %d\n",
4034 info->line, info->count);
4035#endif
4036 local_irq_save(flags);
4037 if (!tty_hung_up_p(filp)) {
4038 extra_count++;
4039 info->count--;
4040 }
4041 local_irq_restore(flags);
4042 info->blocked_open++;
4043 while (1) {
4044 local_irq_save(flags);
4045 /* assert RTS and DTR */
4046 e100_rts(info, 1);
4047 e100_dtr(info, 1);
4048 local_irq_restore(flags);
4049 set_current_state(TASK_INTERRUPTIBLE);
4050 if (tty_hung_up_p(filp) ||
4051 !(info->flags & ASYNC_INITIALIZED)) {
4052#ifdef SERIAL_DO_RESTART
4053 if (info->flags & ASYNC_HUP_NOTIFY)
4054 retval = -EAGAIN;
4055 else
4056 retval = -ERESTARTSYS;
4057#else
4058 retval = -EAGAIN;
4059#endif
4060 break;
4061 }
4062 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
4063 /* && (do_clocal || DCD_IS_ASSERTED) */
4064 break;
4065 if (signal_pending(current)) {
4066 retval = -ERESTARTSYS;
4067 break;
4068 }
4069#ifdef SERIAL_DEBUG_OPEN
4070 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4071 info->line, info->count);
4072#endif
4073 tty_unlock();
4074 schedule();
4075 tty_lock();
4076 }
4077 set_current_state(TASK_RUNNING);
4078 remove_wait_queue(&info->open_wait, &wait);
4079 if (extra_count)
4080 info->count++;
4081 info->blocked_open--;
4082#ifdef SERIAL_DEBUG_OPEN
4083 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4084 info->line, info->count);
4085#endif
4086 if (retval)
4087 return retval;
4088 info->flags |= ASYNC_NORMAL_ACTIVE;
4089 return 0;
4090}
4091
4092static void
4093deinit_port(struct e100_serial *info)
4094{
4095 if (info->dma_out_enabled) {
4096 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
4097 free_irq(info->dma_out_irq_nbr, info);
4098 }
4099 if (info->dma_in_enabled) {
4100 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
4101 free_irq(info->dma_in_irq_nbr, info);
4102 }
4103}
4104
4105/*
4106 * This routine is called whenever a serial port is opened.
4107 * It performs the serial-specific initialization for the tty structure.
4108 */
4109static int
4110rs_open(struct tty_struct *tty, struct file * filp)
4111{
4112 struct e100_serial *info;
4113 int retval, line;
4114 unsigned long page;
4115 int allocated_resources = 0;
4116
4117 /* find which port we want to open */
4118 line = tty->index;
4119
4120 if (line < 0 || line >= NR_PORTS)
4121 return -ENODEV;
4122
4123 /* find the corresponding e100_serial struct in the table */
4124 info = rs_table + line;
4125
4126 /* don't allow the opening of ports that are not enabled in the HW config */
4127 if (!info->enabled)
4128 return -ENODEV;
4129
4130#ifdef SERIAL_DEBUG_OPEN
4131 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
4132 info->count);
4133#endif
4134
4135 info->count++;
4136 tty->driver_data = info;
4137 info->port.tty = tty;
4138
4139 info->port.tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
4140
4141 if (!tmp_buf) {
4142 page = get_zeroed_page(GFP_KERNEL);
4143 if (!page) {
4144 return -ENOMEM;
4145 }
4146 if (tmp_buf)
4147 free_page(page);
4148 else
4149 tmp_buf = (unsigned char *) page;
4150 }
4151
4152 /*
4153 * If the port is in the middle of closing, bail out now
4154 */
4155 if (tty_hung_up_p(filp) ||
4156 (info->flags & ASYNC_CLOSING)) {
4157 wait_event_interruptible_tty(info->close_wait,
4158 !(info->flags & ASYNC_CLOSING));
4159#ifdef SERIAL_DO_RESTART
4160 return ((info->flags & ASYNC_HUP_NOTIFY) ?
4161 -EAGAIN : -ERESTARTSYS);
4162#else
4163 return -EAGAIN;
4164#endif
4165 }
4166
4167 /*
4168 * If DMA is enabled try to allocate the irq's.
4169 */
4170 if (info->count == 1) {
4171 allocated_resources = 1;
4172 if (info->dma_in_enabled) {
4173 if (request_irq(info->dma_in_irq_nbr,
4174 rec_interrupt,
4175 info->dma_in_irq_flags,
4176 info->dma_in_irq_description,
4177 info)) {
4178 printk(KERN_WARNING "DMA irq '%s' busy; "
4179 "falling back to non-DMA mode\n",
4180 info->dma_in_irq_description);
4181 /* Make sure we never try to use DMA in */
4182 /* for the port again. */
4183 info->dma_in_enabled = 0;
4184 } else if (cris_request_dma(info->dma_in_nbr,
4185 info->dma_in_irq_description,
4186 DMA_VERBOSE_ON_ERROR,
4187 info->dma_owner)) {
4188 free_irq(info->dma_in_irq_nbr, info);
4189 printk(KERN_WARNING "DMA '%s' busy; "
4190 "falling back to non-DMA mode\n",
4191 info->dma_in_irq_description);
4192 /* Make sure we never try to use DMA in */
4193 /* for the port again. */
4194 info->dma_in_enabled = 0;
4195 }
4196#ifdef SERIAL_DEBUG_OPEN
4197 else
4198 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4199 info->dma_in_irq_description);
4200#endif
4201 }
4202 if (info->dma_out_enabled) {
4203 if (request_irq(info->dma_out_irq_nbr,
4204 tr_interrupt,
4205 info->dma_out_irq_flags,
4206 info->dma_out_irq_description,
4207 info)) {
4208 printk(KERN_WARNING "DMA irq '%s' busy; "
4209 "falling back to non-DMA mode\n",
4210 info->dma_out_irq_description);
4211 /* Make sure we never try to use DMA out */
4212 /* for the port again. */
4213 info->dma_out_enabled = 0;
4214 } else if (cris_request_dma(info->dma_out_nbr,
4215 info->dma_out_irq_description,
4216 DMA_VERBOSE_ON_ERROR,
4217 info->dma_owner)) {
4218 free_irq(info->dma_out_irq_nbr, info);
4219 printk(KERN_WARNING "DMA '%s' busy; "
4220 "falling back to non-DMA mode\n",
4221 info->dma_out_irq_description);
4222 /* Make sure we never try to use DMA out */
4223 /* for the port again. */
4224 info->dma_out_enabled = 0;
4225 }
4226#ifdef SERIAL_DEBUG_OPEN
4227 else
4228 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4229 info->dma_out_irq_description);
4230#endif
4231 }
4232 }
4233
4234 /*
4235 * Start up the serial port
4236 */
4237
4238 retval = startup(info);
4239 if (retval) {
4240 if (allocated_resources)
4241 deinit_port(info);
4242
4243 /* FIXME Decrease count info->count here too? */
4244 return retval;
4245 }
4246
4247
4248 retval = block_til_ready(tty, filp, info);
4249 if (retval) {
4250#ifdef SERIAL_DEBUG_OPEN
4251 printk("rs_open returning after block_til_ready with %d\n",
4252 retval);
4253#endif
4254 if (allocated_resources)
4255 deinit_port(info);
4256
4257 return retval;
4258 }
4259
4260 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
4261 *tty->termios = info->normal_termios;
4262 change_speed(info);
4263 }
4264
4265#ifdef SERIAL_DEBUG_OPEN
4266 printk("rs_open ttyS%d successful...\n", info->line);
4267#endif
4268 DLOG_INT_TRIG( log_int_pos = 0);
4269
4270 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4271 info->icount.rx = 0;
4272 } );
4273
4274 return 0;
4275}
4276
4277#ifdef CONFIG_PROC_FS
4278/*
4279 * /proc fs routines....
4280 */
4281
4282static void seq_line_info(struct seq_file *m, struct e100_serial *info)
4283{
4284 unsigned long tmp;
4285
4286 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
4287 info->line, (unsigned long)info->ioport, info->irq);
4288
4289 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
4290 seq_printf(m, "\n");
4291 return;
4292 }
4293
4294 seq_printf(m, " baud:%d", info->baud);
4295 seq_printf(m, " tx:%lu rx:%lu",
4296 (unsigned long)info->icount.tx,
4297 (unsigned long)info->icount.rx);
4298 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
4299 if (tmp)
4300 seq_printf(m, " tx_pend:%lu/%lu",
4301 (unsigned long)tmp,
4302 (unsigned long)SERIAL_XMIT_SIZE);
4303
4304 seq_printf(m, " rx_pend:%lu/%lu",
4305 (unsigned long)info->recv_cnt,
4306 (unsigned long)info->max_recv_cnt);
4307
4308#if 1
4309 if (info->port.tty) {
4310 if (info->port.tty->stopped)
4311 seq_printf(m, " stopped:%i",
4312 (int)info->port.tty->stopped);
4313 if (info->port.tty->hw_stopped)
4314 seq_printf(m, " hw_stopped:%i",
4315 (int)info->port.tty->hw_stopped);
4316 }
4317
4318 {
4319 unsigned char rstat = info->ioport[REG_STATUS];
4320 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4321 seq_printf(m, " xoff_detect:1");
4322 }
4323
4324#endif
4325
4326 if (info->icount.frame)
4327 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
4328
4329 if (info->icount.parity)
4330 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
4331
4332 if (info->icount.brk)
4333 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
4334
4335 if (info->icount.overrun)
4336 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
4337
4338 /*
4339 * Last thing is the RS-232 status lines
4340 */
4341 if (!E100_RTS_GET(info))
4342 seq_puts(m, "|RTS");
4343 if (!E100_CTS_GET(info))
4344 seq_puts(m, "|CTS");
4345 if (!E100_DTR_GET(info))
4346 seq_puts(m, "|DTR");
4347 if (!E100_DSR_GET(info))
4348 seq_puts(m, "|DSR");
4349 if (!E100_CD_GET(info))
4350 seq_puts(m, "|CD");
4351 if (!E100_RI_GET(info))
4352 seq_puts(m, "|RI");
4353 seq_puts(m, "\n");
4354}
4355
4356
4357static int crisv10_proc_show(struct seq_file *m, void *v)
4358{
4359 int i;
4360
4361 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4362
4363 for (i = 0; i < NR_PORTS; i++) {
4364 if (!rs_table[i].enabled)
4365 continue;
4366 seq_line_info(m, &rs_table[i]);
4367 }
4368#ifdef DEBUG_LOG_INCLUDED
4369 for (i = 0; i < debug_log_pos; i++) {
4370 seq_printf(m, "%-4i %lu.%lu ",
4371 i, debug_log[i].time,
4372 timer_data_to_ns(debug_log[i].timer_data));
4373 seq_printf(m, debug_log[i].string, debug_log[i].value);
4374 }
4375 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
4376 debug_log_pos = 0;
4377#endif
4378 return 0;
4379}
4380
4381static int crisv10_proc_open(struct inode *inode, struct file *file)
4382{
4383 return single_open(file, crisv10_proc_show, NULL);
4384}
4385
4386static const struct file_operations crisv10_proc_fops = {
4387 .owner = THIS_MODULE,
4388 .open = crisv10_proc_open,
4389 .read = seq_read,
4390 .llseek = seq_lseek,
4391 .release = single_release,
4392};
4393#endif
4394
4395
4396/* Finally, routines used to initialize the serial driver. */
4397
4398static void show_serial_version(void)
4399{
4400 printk(KERN_INFO
4401 "ETRAX 100LX serial-driver %s, "
4402 "(c) 2000-2004 Axis Communications AB\r\n",
4403 &serial_version[11]); /* "$Revision: x.yy" */
4404}
4405
4406/* rs_init inits the driver at boot (using the module_init chain) */
4407
4408static const struct tty_operations rs_ops = {
4409 .open = rs_open,
4410 .close = rs_close,
4411 .write = rs_write,
4412 .flush_chars = rs_flush_chars,
4413 .write_room = rs_write_room,
4414 .chars_in_buffer = rs_chars_in_buffer,
4415 .flush_buffer = rs_flush_buffer,
4416 .ioctl = rs_ioctl,
4417 .throttle = rs_throttle,
4418 .unthrottle = rs_unthrottle,
4419 .set_termios = rs_set_termios,
4420 .stop = rs_stop,
4421 .start = rs_start,
4422 .hangup = rs_hangup,
4423 .break_ctl = rs_break,
4424 .send_xchar = rs_send_xchar,
4425 .wait_until_sent = rs_wait_until_sent,
4426 .tiocmget = rs_tiocmget,
4427 .tiocmset = rs_tiocmset,
4428#ifdef CONFIG_PROC_FS
4429 .proc_fops = &crisv10_proc_fops,
4430#endif
4431};
4432
4433static int __init rs_init(void)
4434{
4435 int i;
4436 struct e100_serial *info;
4437 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4438
4439 if (!driver)
4440 return -ENOMEM;
4441
4442 show_serial_version();
4443
4444 /* Setup the timed flush handler system */
4445
4446#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
4447 setup_timer(&flush_timer, timed_flush_handler, 0);
4448 mod_timer(&flush_timer, jiffies + 5);
4449#endif
4450
4451#if defined(CONFIG_ETRAX_RS485)
4452#if defined(CONFIG_ETRAX_RS485_ON_PA)
4453 if (cris_io_interface_allocate_pins(if_ser0, 'a', rs485_pa_bit,
4454 rs485_pa_bit)) {
4455 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4456 "RS485 pin\n");
4457 put_tty_driver(driver);
4458 return -EBUSY;
4459 }
4460#endif
4461#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
4462 if (cris_io_interface_allocate_pins(if_ser0, 'g', rs485_pa_bit,
4463 rs485_port_g_bit)) {
4464 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4465 "RS485 pin\n");
4466 put_tty_driver(driver);
4467 return -EBUSY;
4468 }
4469#endif
4470#endif
4471
4472 /* Initialize the tty_driver structure */
4473
4474 driver->driver_name = "serial";
4475 driver->name = "ttyS";
4476 driver->major = TTY_MAJOR;
4477 driver->minor_start = 64;
4478 driver->type = TTY_DRIVER_TYPE_SERIAL;
4479 driver->subtype = SERIAL_TYPE_NORMAL;
4480 driver->init_termios = tty_std_termios;
4481 driver->init_termios.c_cflag =
4482 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
4483 driver->init_termios.c_ispeed = 115200;
4484 driver->init_termios.c_ospeed = 115200;
4485 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4486
4487 tty_set_operations(driver, &rs_ops);
4488 serial_driver = driver;
4489 if (tty_register_driver(driver))
4490 panic("Couldn't register serial driver\n");
4491 /* do some initializing for the separate ports */
4492
4493 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
4494 if (info->enabled) {
4495 if (cris_request_io_interface(info->io_if,
4496 info->io_if_description)) {
4497 printk(KERN_CRIT "ETRAX100LX async serial: "
4498 "Could not allocate IO pins for "
4499 "%s, port %d\n",
4500 info->io_if_description, i);
4501 info->enabled = 0;
4502 }
4503 }
4504 info->uses_dma_in = 0;
4505 info->uses_dma_out = 0;
4506 info->line = i;
4507 info->port.tty = NULL;
4508 info->type = PORT_ETRAX;
4509 info->tr_running = 0;
4510 info->forced_eop = 0;
4511 info->baud_base = DEF_BAUD_BASE;
4512 info->custom_divisor = 0;
4513 info->flags = 0;
4514 info->close_delay = 5*HZ/10;
4515 info->closing_wait = 30*HZ;
4516 info->x_char = 0;
4517 info->event = 0;
4518 info->count = 0;
4519 info->blocked_open = 0;
4520 info->normal_termios = driver->init_termios;
4521 init_waitqueue_head(&info->open_wait);
4522 init_waitqueue_head(&info->close_wait);
4523 info->xmit.buf = NULL;
4524 info->xmit.tail = info->xmit.head = 0;
4525 info->first_recv_buffer = info->last_recv_buffer = NULL;
4526 info->recv_cnt = info->max_recv_cnt = 0;
4527 info->last_tx_active_usec = 0;
4528 info->last_tx_active = 0;
4529
4530#if defined(CONFIG_ETRAX_RS485)
4531 /* Set sane defaults */
4532 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4533 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
4534 info->rs485.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
4535 info->rs485.delay_rts_before_send = 0;
4536 info->rs485.flags &= ~(SER_RS485_ENABLED);
4537#endif
4538 INIT_WORK(&info->work, do_softint);
4539
4540 if (info->enabled) {
4541 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4542 serial_driver->name, info->line, info->ioport);
4543 }
4544 }
4545#ifdef CONFIG_ETRAX_FAST_TIMER
4546#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4547 memset(fast_timers, 0, sizeof(fast_timers));
4548#endif
4549#ifdef CONFIG_ETRAX_RS485
4550 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4551#endif
4552 fast_timer_init();
4553#endif
4554
4555#ifndef CONFIG_SVINTO_SIM
4556#ifndef CONFIG_ETRAX_KGDB
4557 /* Not needed in simulator. May only complicate stuff. */
4558 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4559
4560 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
4561 IRQF_SHARED | IRQF_DISABLED, "serial ", driver))
4562 panic("%s: Failed to request irq8", __func__);
4563
4564#endif
4565#endif /* CONFIG_SVINTO_SIM */
4566
4567 return 0;
4568}
4569
4570/* this makes sure that rs_init is called during kernel boot */
4571
4572module_init(rs_init);
diff --git a/drivers/tty/serial/crisv10.h b/drivers/tty/serial/crisv10.h
new file mode 100644
index 000000000000..ea0beb46a10d
--- /dev/null
+++ b/drivers/tty/serial/crisv10.h
@@ -0,0 +1,147 @@
1/*
2 * serial.h: Arch-dep definitions for the Etrax100 serial driver.
3 *
4 * Copyright (C) 1998-2007 Axis Communications AB
5 */
6
7#ifndef _ETRAX_SERIAL_H
8#define _ETRAX_SERIAL_H
9
10#include <linux/circ_buf.h>
11#include <asm/termios.h>
12#include <asm/dma.h>
13#include <arch/io_interface_mux.h>
14
15/* Software state per channel */
16
17#ifdef __KERNEL__
18/*
19 * This is our internal structure for each serial port's state.
20 *
21 * Many fields are paralleled by the structure used by the serial_struct
22 * structure.
23 *
24 * For definitions of the flags field, see tty.h
25 */
26
27#define SERIAL_RECV_DESCRIPTORS 8
28
29struct etrax_recv_buffer {
30 struct etrax_recv_buffer *next;
31 unsigned short length;
32 unsigned char error;
33 unsigned char pad;
34
35 unsigned char buffer[0];
36};
37
38struct e100_serial {
39 struct tty_port port;
40 int baud;
41 volatile u8 *ioport; /* R_SERIALx_CTRL */
42 u32 irq; /* bitnr in R_IRQ_MASK2 for dmaX_descr */
43
44 /* Output registers */
45 volatile u8 *oclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
46 volatile u32 *ofirstadr; /* adr to R_DMA_CHx_FIRST */
47 volatile u8 *ocmdadr; /* adr to R_DMA_CHx_CMD */
48 const volatile u8 *ostatusadr; /* adr to R_DMA_CHx_STATUS */
49
50 /* Input registers */
51 volatile u8 *iclrintradr; /* adr to R_DMA_CHx_CLR_INTR */
52 volatile u32 *ifirstadr; /* adr to R_DMA_CHx_FIRST */
53 volatile u8 *icmdadr; /* adr to R_DMA_CHx_CMD */
54 volatile u32 *idescradr; /* adr to R_DMA_CHx_DESCR */
55
56 int flags; /* defined in tty.h */
57
58 u8 rx_ctrl; /* shadow for R_SERIALx_REC_CTRL */
59 u8 tx_ctrl; /* shadow for R_SERIALx_TR_CTRL */
60 u8 iseteop; /* bit number for R_SET_EOP for the input dma */
61 int enabled; /* Set to 1 if the port is enabled in HW config */
62
63 u8 dma_out_enabled; /* Set to 1 if DMA should be used */
64 u8 dma_in_enabled; /* Set to 1 if DMA should be used */
65
66 /* end of fields defined in rs_table[] in .c-file */
67 int dma_owner;
68 unsigned int dma_in_nbr;
69 unsigned int dma_out_nbr;
70 unsigned int dma_in_irq_nbr;
71 unsigned int dma_out_irq_nbr;
72 unsigned long dma_in_irq_flags;
73 unsigned long dma_out_irq_flags;
74 char *dma_in_irq_description;
75 char *dma_out_irq_description;
76
77 enum cris_io_interface io_if;
78 char *io_if_description;
79
80 u8 uses_dma_in; /* Set to 1 if DMA is used */
81 u8 uses_dma_out; /* Set to 1 if DMA is used */
82 u8 forced_eop; /* a fifo eop has been forced */
83 int baud_base; /* For special baudrates */
84 int custom_divisor; /* For special baudrates */
85 struct etrax_dma_descr tr_descr;
86 struct etrax_dma_descr rec_descr[SERIAL_RECV_DESCRIPTORS];
87 int cur_rec_descr;
88
89 volatile int tr_running; /* 1 if output is running */
90
91 struct tty_struct *tty;
92 int read_status_mask;
93 int ignore_status_mask;
94 int x_char; /* xon/xoff character */
95 int close_delay;
96 unsigned short closing_wait;
97 unsigned short closing_wait2;
98 unsigned long event;
99 unsigned long last_active;
100 int line;
101 int type; /* PORT_ETRAX */
102 int count; /* # of fd on device */
103 int blocked_open; /* # of blocked opens */
104 struct circ_buf xmit;
105 struct etrax_recv_buffer *first_recv_buffer;
106 struct etrax_recv_buffer *last_recv_buffer;
107 unsigned int recv_cnt;
108 unsigned int max_recv_cnt;
109
110 struct work_struct work;
111 struct async_icount icount; /* error-statistics etc.*/
112 struct ktermios normal_termios;
113 struct ktermios callout_termios;
114 wait_queue_head_t open_wait;
115 wait_queue_head_t close_wait;
116
117 unsigned long char_time_usec; /* The time for 1 char, in usecs */
118 unsigned long flush_time_usec; /* How often we should flush */
119 unsigned long last_tx_active_usec; /* Last tx usec in the jiffies */
120 unsigned long last_tx_active; /* Last tx time in jiffies */
121 unsigned long last_rx_active_usec; /* Last rx usec in the jiffies */
122 unsigned long last_rx_active; /* Last rx time in jiffies */
123
124 int break_detected_cnt;
125 int errorcode;
126
127#ifdef CONFIG_ETRAX_RS485
128 struct serial_rs485 rs485; /* RS-485 support */
129#endif
130};
131
132/* this PORT is not in the standard serial.h. it's not actually used for
133 * anything since we only have one type of async serial-port anyway in this
134 * system.
135 */
136
137#define PORT_ETRAX 1
138
139/*
140 * Events are used to schedule things to happen at timer-interrupt
141 * time, instead of at rs interrupt time.
142 */
143#define RS_EVENT_WRITE_WAKEUP 0
144
145#endif /* __KERNEL__ */
146
147#endif /* !_ETRAX_SERIAL_H */
diff --git a/drivers/tty/serial/dz.c b/drivers/tty/serial/dz.c
new file mode 100644
index 000000000000..57421d776329
--- /dev/null
+++ b/drivers/tty/serial/dz.c
@@ -0,0 +1,955 @@
1/*
2 * dz.c: Serial port driver for DECstations equipped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
10 *
11 * [31-AUG-98] triemer
12 * Changed IRQ to use Harald's dec internals interrupts.h
13 * removed base_addr code - moving address assignment to setup.c
14 * Changed name of dz_init to rs_init to be consistent with tc code
15 * [13-NOV-98] triemer fixed code to receive characters
16 * after patches by harald to irq code.
17 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
18 * field from "current" - somewhere between 2.1.121 and 2.1.131
19 Qua Jun 27 15:02:26 BRT 2001
20 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
21 *
22 * Parts (C) 1999 David Airlie, airlied@linux.ie
23 * [07-SEP-99] Bugfixes
24 *
25 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
26 * Converted to new serial core
27 */
28
29#undef DEBUG_DZ
30
31#if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/bitops.h>
36#include <linux/compiler.h>
37#include <linux/console.h>
38#include <linux/delay.h>
39#include <linux/errno.h>
40#include <linux/init.h>
41#include <linux/interrupt.h>
42#include <linux/ioport.h>
43#include <linux/kernel.h>
44#include <linux/major.h>
45#include <linux/module.h>
46#include <linux/serial.h>
47#include <linux/serial_core.h>
48#include <linux/sysrq.h>
49#include <linux/tty.h>
50
51#include <asm/atomic.h>
52#include <asm/bootinfo.h>
53#include <asm/io.h>
54#include <asm/system.h>
55
56#include <asm/dec/interrupts.h>
57#include <asm/dec/kn01.h>
58#include <asm/dec/kn02.h>
59#include <asm/dec/machtype.h>
60#include <asm/dec/prom.h>
61#include <asm/dec/system.h>
62
63#include "dz.h"
64
65
66MODULE_DESCRIPTION("DECstation DZ serial driver");
67MODULE_LICENSE("GPL");
68
69
70static char dz_name[] __initdata = "DECstation DZ serial driver version ";
71static char dz_version[] __initdata = "1.04";
72
73struct dz_port {
74 struct dz_mux *mux;
75 struct uart_port port;
76 unsigned int cflag;
77};
78
79struct dz_mux {
80 struct dz_port dport[DZ_NB_PORT];
81 atomic_t map_guard;
82 atomic_t irq_guard;
83 int initialised;
84};
85
86static struct dz_mux dz_mux;
87
88static inline struct dz_port *to_dport(struct uart_port *uport)
89{
90 return container_of(uport, struct dz_port, port);
91}
92
93/*
94 * ------------------------------------------------------------
95 * dz_in () and dz_out ()
96 *
97 * These routines are used to access the registers of the DZ
98 * chip, hiding relocation differences between implementation.
99 * ------------------------------------------------------------
100 */
101
102static u16 dz_in(struct dz_port *dport, unsigned offset)
103{
104 void __iomem *addr = dport->port.membase + offset;
105
106 return readw(addr);
107}
108
109static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
110{
111 void __iomem *addr = dport->port.membase + offset;
112
113 writew(value, addr);
114}
115
116/*
117 * ------------------------------------------------------------
118 * rs_stop () and rs_start ()
119 *
120 * These routines are called before setting or resetting
121 * tty->stopped. They enable or disable transmitter interrupts,
122 * as necessary.
123 * ------------------------------------------------------------
124 */
125
126static void dz_stop_tx(struct uart_port *uport)
127{
128 struct dz_port *dport = to_dport(uport);
129 u16 tmp, mask = 1 << dport->port.line;
130
131 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
132 tmp &= ~mask; /* clear the TX flag */
133 dz_out(dport, DZ_TCR, tmp);
134}
135
136static void dz_start_tx(struct uart_port *uport)
137{
138 struct dz_port *dport = to_dport(uport);
139 u16 tmp, mask = 1 << dport->port.line;
140
141 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
142 tmp |= mask; /* set the TX flag */
143 dz_out(dport, DZ_TCR, tmp);
144}
145
146static void dz_stop_rx(struct uart_port *uport)
147{
148 struct dz_port *dport = to_dport(uport);
149
150 dport->cflag &= ~DZ_RXENAB;
151 dz_out(dport, DZ_LPR, dport->cflag);
152}
153
154static void dz_enable_ms(struct uart_port *uport)
155{
156 /* nothing to do */
157}
158
159/*
160 * ------------------------------------------------------------
161 *
162 * Here start the interrupt handling routines. All of the following
163 * subroutines are declared as inline and are folded into
164 * dz_interrupt. They were separated out for readability's sake.
165 *
166 * Note: dz_interrupt() is a "fast" interrupt, which means that it
167 * runs with interrupts turned off. People who may want to modify
168 * dz_interrupt() should try to keep the interrupt handler as fast as
169 * possible. After you are done making modifications, it is not a bad
170 * idea to do:
171 *
172 * make drivers/serial/dz.s
173 *
174 * and look at the resulting assemble code in dz.s.
175 *
176 * ------------------------------------------------------------
177 */
178
179/*
180 * ------------------------------------------------------------
181 * receive_char ()
182 *
183 * This routine deals with inputs from any lines.
184 * ------------------------------------------------------------
185 */
186static inline void dz_receive_chars(struct dz_mux *mux)
187{
188 struct uart_port *uport;
189 struct dz_port *dport = &mux->dport[0];
190 struct tty_struct *tty = NULL;
191 struct uart_icount *icount;
192 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
193 unsigned char ch, flag;
194 u16 status;
195 int i;
196
197 while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
198 dport = &mux->dport[LINE(status)];
199 uport = &dport->port;
200 tty = uport->state->port.tty; /* point to the proper dev */
201
202 ch = UCHAR(status); /* grab the char */
203 flag = TTY_NORMAL;
204
205 icount = &uport->icount;
206 icount->rx++;
207
208 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
209
210 /*
211 * There is no separate BREAK status bit, so treat
212 * null characters with framing errors as BREAKs;
213 * normally, otherwise. For this move the Framing
214 * Error bit to a simulated BREAK bit.
215 */
216 if (!ch) {
217 status |= (status & DZ_FERR) >>
218 (ffs(DZ_FERR) - ffs(DZ_BREAK));
219 status &= ~DZ_FERR;
220 }
221
222 /* Handle SysRq/SAK & keep track of the statistics. */
223 if (status & DZ_BREAK) {
224 icount->brk++;
225 if (uart_handle_break(uport))
226 continue;
227 } else if (status & DZ_FERR)
228 icount->frame++;
229 else if (status & DZ_PERR)
230 icount->parity++;
231 if (status & DZ_OERR)
232 icount->overrun++;
233
234 status &= uport->read_status_mask;
235 if (status & DZ_BREAK)
236 flag = TTY_BREAK;
237 else if (status & DZ_FERR)
238 flag = TTY_FRAME;
239 else if (status & DZ_PERR)
240 flag = TTY_PARITY;
241
242 }
243
244 if (uart_handle_sysrq_char(uport, ch))
245 continue;
246
247 uart_insert_char(uport, status, DZ_OERR, ch, flag);
248 lines_rx[LINE(status)] = 1;
249 }
250 for (i = 0; i < DZ_NB_PORT; i++)
251 if (lines_rx[i])
252 tty_flip_buffer_push(mux->dport[i].port.state->port.tty);
253}
254
255/*
256 * ------------------------------------------------------------
257 * transmit_char ()
258 *
259 * This routine deals with outputs to any lines.
260 * ------------------------------------------------------------
261 */
262static inline void dz_transmit_chars(struct dz_mux *mux)
263{
264 struct dz_port *dport = &mux->dport[0];
265 struct circ_buf *xmit;
266 unsigned char tmp;
267 u16 status;
268
269 status = dz_in(dport, DZ_CSR);
270 dport = &mux->dport[LINE(status)];
271 xmit = &dport->port.state->xmit;
272
273 if (dport->port.x_char) { /* XON/XOFF chars */
274 dz_out(dport, DZ_TDR, dport->port.x_char);
275 dport->port.icount.tx++;
276 dport->port.x_char = 0;
277 return;
278 }
279 /* If nothing to do or stopped or hardware stopped. */
280 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
281 spin_lock(&dport->port.lock);
282 dz_stop_tx(&dport->port);
283 spin_unlock(&dport->port.lock);
284 return;
285 }
286
287 /*
288 * If something to do... (remember the dz has no output fifo,
289 * so we go one char at a time) :-<
290 */
291 tmp = xmit->buf[xmit->tail];
292 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
293 dz_out(dport, DZ_TDR, tmp);
294 dport->port.icount.tx++;
295
296 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
297 uart_write_wakeup(&dport->port);
298
299 /* Are we are done. */
300 if (uart_circ_empty(xmit)) {
301 spin_lock(&dport->port.lock);
302 dz_stop_tx(&dport->port);
303 spin_unlock(&dport->port.lock);
304 }
305}
306
307/*
308 * ------------------------------------------------------------
309 * check_modem_status()
310 *
311 * DS 3100 & 5100: Only valid for the MODEM line, duh!
312 * DS 5000/200: Valid for the MODEM and PRINTER line.
313 * ------------------------------------------------------------
314 */
315static inline void check_modem_status(struct dz_port *dport)
316{
317 /*
318 * FIXME:
319 * 1. No status change interrupt; use a timer.
320 * 2. Handle the 3100/5000 as appropriate. --macro
321 */
322 u16 status;
323
324 /* If not the modem line just return. */
325 if (dport->port.line != DZ_MODEM)
326 return;
327
328 status = dz_in(dport, DZ_MSR);
329
330 /* it's easy, since DSR2 is the only bit in the register */
331 if (status)
332 dport->port.icount.dsr++;
333}
334
335/*
336 * ------------------------------------------------------------
337 * dz_interrupt ()
338 *
339 * this is the main interrupt routine for the DZ chip.
340 * It deals with the multiple ports.
341 * ------------------------------------------------------------
342 */
343static irqreturn_t dz_interrupt(int irq, void *dev_id)
344{
345 struct dz_mux *mux = dev_id;
346 struct dz_port *dport = &mux->dport[0];
347 u16 status;
348
349 /* get the reason why we just got an irq */
350 status = dz_in(dport, DZ_CSR);
351
352 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
353 dz_receive_chars(mux);
354
355 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
356 dz_transmit_chars(mux);
357
358 return IRQ_HANDLED;
359}
360
361/*
362 * -------------------------------------------------------------------
363 * Here ends the DZ interrupt routines.
364 * -------------------------------------------------------------------
365 */
366
367static unsigned int dz_get_mctrl(struct uart_port *uport)
368{
369 /*
370 * FIXME: Handle the 3100/5000 as appropriate. --macro
371 */
372 struct dz_port *dport = to_dport(uport);
373 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
374
375 if (dport->port.line == DZ_MODEM) {
376 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
377 mctrl &= ~TIOCM_DSR;
378 }
379
380 return mctrl;
381}
382
383static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
384{
385 /*
386 * FIXME: Handle the 3100/5000 as appropriate. --macro
387 */
388 struct dz_port *dport = to_dport(uport);
389 u16 tmp;
390
391 if (dport->port.line == DZ_MODEM) {
392 tmp = dz_in(dport, DZ_TCR);
393 if (mctrl & TIOCM_DTR)
394 tmp &= ~DZ_MODEM_DTR;
395 else
396 tmp |= DZ_MODEM_DTR;
397 dz_out(dport, DZ_TCR, tmp);
398 }
399}
400
401/*
402 * -------------------------------------------------------------------
403 * startup ()
404 *
405 * various initialization tasks
406 * -------------------------------------------------------------------
407 */
408static int dz_startup(struct uart_port *uport)
409{
410 struct dz_port *dport = to_dport(uport);
411 struct dz_mux *mux = dport->mux;
412 unsigned long flags;
413 int irq_guard;
414 int ret;
415 u16 tmp;
416
417 irq_guard = atomic_add_return(1, &mux->irq_guard);
418 if (irq_guard != 1)
419 return 0;
420
421 ret = request_irq(dport->port.irq, dz_interrupt,
422 IRQF_SHARED, "dz", mux);
423 if (ret) {
424 atomic_add(-1, &mux->irq_guard);
425 printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
426 return ret;
427 }
428
429 spin_lock_irqsave(&dport->port.lock, flags);
430
431 /* Enable interrupts. */
432 tmp = dz_in(dport, DZ_CSR);
433 tmp |= DZ_RIE | DZ_TIE;
434 dz_out(dport, DZ_CSR, tmp);
435
436 spin_unlock_irqrestore(&dport->port.lock, flags);
437
438 return 0;
439}
440
441/*
442 * -------------------------------------------------------------------
443 * shutdown ()
444 *
445 * This routine will shutdown a serial port; interrupts are disabled, and
446 * DTR is dropped if the hangup on close termio flag is on.
447 * -------------------------------------------------------------------
448 */
449static void dz_shutdown(struct uart_port *uport)
450{
451 struct dz_port *dport = to_dport(uport);
452 struct dz_mux *mux = dport->mux;
453 unsigned long flags;
454 int irq_guard;
455 u16 tmp;
456
457 spin_lock_irqsave(&dport->port.lock, flags);
458 dz_stop_tx(&dport->port);
459 spin_unlock_irqrestore(&dport->port.lock, flags);
460
461 irq_guard = atomic_add_return(-1, &mux->irq_guard);
462 if (!irq_guard) {
463 /* Disable interrupts. */
464 tmp = dz_in(dport, DZ_CSR);
465 tmp &= ~(DZ_RIE | DZ_TIE);
466 dz_out(dport, DZ_CSR, tmp);
467
468 free_irq(dport->port.irq, mux);
469 }
470}
471
472/*
473 * -------------------------------------------------------------------
474 * dz_tx_empty() -- get the transmitter empty status
475 *
476 * Purpose: Let user call ioctl() to get info when the UART physically
477 * is emptied. On bus types like RS485, the transmitter must
478 * release the bus after transmitting. This must be done when
479 * the transmit shift register is empty, not be done when the
480 * transmit holding register is empty. This functionality
481 * allows an RS485 driver to be written in user space.
482 * -------------------------------------------------------------------
483 */
484static unsigned int dz_tx_empty(struct uart_port *uport)
485{
486 struct dz_port *dport = to_dport(uport);
487 unsigned short tmp, mask = 1 << dport->port.line;
488
489 tmp = dz_in(dport, DZ_TCR);
490 tmp &= mask;
491
492 return tmp ? 0 : TIOCSER_TEMT;
493}
494
495static void dz_break_ctl(struct uart_port *uport, int break_state)
496{
497 /*
498 * FIXME: Can't access BREAK bits in TDR easily;
499 * reuse the code for polled TX. --macro
500 */
501 struct dz_port *dport = to_dport(uport);
502 unsigned long flags;
503 unsigned short tmp, mask = 1 << dport->port.line;
504
505 spin_lock_irqsave(&uport->lock, flags);
506 tmp = dz_in(dport, DZ_TCR);
507 if (break_state)
508 tmp |= mask;
509 else
510 tmp &= ~mask;
511 dz_out(dport, DZ_TCR, tmp);
512 spin_unlock_irqrestore(&uport->lock, flags);
513}
514
515static int dz_encode_baud_rate(unsigned int baud)
516{
517 switch (baud) {
518 case 50:
519 return DZ_B50;
520 case 75:
521 return DZ_B75;
522 case 110:
523 return DZ_B110;
524 case 134:
525 return DZ_B134;
526 case 150:
527 return DZ_B150;
528 case 300:
529 return DZ_B300;
530 case 600:
531 return DZ_B600;
532 case 1200:
533 return DZ_B1200;
534 case 1800:
535 return DZ_B1800;
536 case 2000:
537 return DZ_B2000;
538 case 2400:
539 return DZ_B2400;
540 case 3600:
541 return DZ_B3600;
542 case 4800:
543 return DZ_B4800;
544 case 7200:
545 return DZ_B7200;
546 case 9600:
547 return DZ_B9600;
548 default:
549 return -1;
550 }
551}
552
553
554static void dz_reset(struct dz_port *dport)
555{
556 struct dz_mux *mux = dport->mux;
557
558 if (mux->initialised)
559 return;
560
561 dz_out(dport, DZ_CSR, DZ_CLR);
562 while (dz_in(dport, DZ_CSR) & DZ_CLR);
563 iob();
564
565 /* Enable scanning. */
566 dz_out(dport, DZ_CSR, DZ_MSE);
567
568 mux->initialised = 1;
569}
570
571static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
572 struct ktermios *old_termios)
573{
574 struct dz_port *dport = to_dport(uport);
575 unsigned long flags;
576 unsigned int cflag, baud;
577 int bflag;
578
579 cflag = dport->port.line;
580
581 switch (termios->c_cflag & CSIZE) {
582 case CS5:
583 cflag |= DZ_CS5;
584 break;
585 case CS6:
586 cflag |= DZ_CS6;
587 break;
588 case CS7:
589 cflag |= DZ_CS7;
590 break;
591 case CS8:
592 default:
593 cflag |= DZ_CS8;
594 }
595
596 if (termios->c_cflag & CSTOPB)
597 cflag |= DZ_CSTOPB;
598 if (termios->c_cflag & PARENB)
599 cflag |= DZ_PARENB;
600 if (termios->c_cflag & PARODD)
601 cflag |= DZ_PARODD;
602
603 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
604 bflag = dz_encode_baud_rate(baud);
605 if (bflag < 0) { /* Try to keep unchanged. */
606 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
607 bflag = dz_encode_baud_rate(baud);
608 if (bflag < 0) { /* Resort to 9600. */
609 baud = 9600;
610 bflag = DZ_B9600;
611 }
612 tty_termios_encode_baud_rate(termios, baud, baud);
613 }
614 cflag |= bflag;
615
616 if (termios->c_cflag & CREAD)
617 cflag |= DZ_RXENAB;
618
619 spin_lock_irqsave(&dport->port.lock, flags);
620
621 uart_update_timeout(uport, termios->c_cflag, baud);
622
623 dz_out(dport, DZ_LPR, cflag);
624 dport->cflag = cflag;
625
626 /* setup accept flag */
627 dport->port.read_status_mask = DZ_OERR;
628 if (termios->c_iflag & INPCK)
629 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
630 if (termios->c_iflag & (BRKINT | PARMRK))
631 dport->port.read_status_mask |= DZ_BREAK;
632
633 /* characters to ignore */
634 uport->ignore_status_mask = 0;
635 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
636 dport->port.ignore_status_mask |= DZ_OERR;
637 if (termios->c_iflag & IGNPAR)
638 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
639 if (termios->c_iflag & IGNBRK)
640 dport->port.ignore_status_mask |= DZ_BREAK;
641
642 spin_unlock_irqrestore(&dport->port.lock, flags);
643}
644
645/*
646 * Hack alert!
647 * Required solely so that the initial PROM-based console
648 * works undisturbed in parallel with this one.
649 */
650static void dz_pm(struct uart_port *uport, unsigned int state,
651 unsigned int oldstate)
652{
653 struct dz_port *dport = to_dport(uport);
654 unsigned long flags;
655
656 spin_lock_irqsave(&dport->port.lock, flags);
657 if (state < 3)
658 dz_start_tx(&dport->port);
659 else
660 dz_stop_tx(&dport->port);
661 spin_unlock_irqrestore(&dport->port.lock, flags);
662}
663
664
665static const char *dz_type(struct uart_port *uport)
666{
667 return "DZ";
668}
669
670static void dz_release_port(struct uart_port *uport)
671{
672 struct dz_mux *mux = to_dport(uport)->mux;
673 int map_guard;
674
675 iounmap(uport->membase);
676 uport->membase = NULL;
677
678 map_guard = atomic_add_return(-1, &mux->map_guard);
679 if (!map_guard)
680 release_mem_region(uport->mapbase, dec_kn_slot_size);
681}
682
683static int dz_map_port(struct uart_port *uport)
684{
685 if (!uport->membase)
686 uport->membase = ioremap_nocache(uport->mapbase,
687 dec_kn_slot_size);
688 if (!uport->membase) {
689 printk(KERN_ERR "dz: Cannot map MMIO\n");
690 return -ENOMEM;
691 }
692 return 0;
693}
694
695static int dz_request_port(struct uart_port *uport)
696{
697 struct dz_mux *mux = to_dport(uport)->mux;
698 int map_guard;
699 int ret;
700
701 map_guard = atomic_add_return(1, &mux->map_guard);
702 if (map_guard == 1) {
703 if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
704 "dz")) {
705 atomic_add(-1, &mux->map_guard);
706 printk(KERN_ERR
707 "dz: Unable to reserve MMIO resource\n");
708 return -EBUSY;
709 }
710 }
711 ret = dz_map_port(uport);
712 if (ret) {
713 map_guard = atomic_add_return(-1, &mux->map_guard);
714 if (!map_guard)
715 release_mem_region(uport->mapbase, dec_kn_slot_size);
716 return ret;
717 }
718 return 0;
719}
720
721static void dz_config_port(struct uart_port *uport, int flags)
722{
723 struct dz_port *dport = to_dport(uport);
724
725 if (flags & UART_CONFIG_TYPE) {
726 if (dz_request_port(uport))
727 return;
728
729 uport->type = PORT_DZ;
730
731 dz_reset(dport);
732 }
733}
734
735/*
736 * Verify the new serial_struct (for TIOCSSERIAL).
737 */
738static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
739{
740 int ret = 0;
741
742 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
743 ret = -EINVAL;
744 if (ser->irq != uport->irq)
745 ret = -EINVAL;
746 return ret;
747}
748
749static struct uart_ops dz_ops = {
750 .tx_empty = dz_tx_empty,
751 .get_mctrl = dz_get_mctrl,
752 .set_mctrl = dz_set_mctrl,
753 .stop_tx = dz_stop_tx,
754 .start_tx = dz_start_tx,
755 .stop_rx = dz_stop_rx,
756 .enable_ms = dz_enable_ms,
757 .break_ctl = dz_break_ctl,
758 .startup = dz_startup,
759 .shutdown = dz_shutdown,
760 .set_termios = dz_set_termios,
761 .pm = dz_pm,
762 .type = dz_type,
763 .release_port = dz_release_port,
764 .request_port = dz_request_port,
765 .config_port = dz_config_port,
766 .verify_port = dz_verify_port,
767};
768
769static void __init dz_init_ports(void)
770{
771 static int first = 1;
772 unsigned long base;
773 int line;
774
775 if (!first)
776 return;
777 first = 0;
778
779 if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
780 base = dec_kn_slot_base + KN01_DZ11;
781 else
782 base = dec_kn_slot_base + KN02_DZ11;
783
784 for (line = 0; line < DZ_NB_PORT; line++) {
785 struct dz_port *dport = &dz_mux.dport[line];
786 struct uart_port *uport = &dport->port;
787
788 dport->mux = &dz_mux;
789
790 uport->irq = dec_interrupt[DEC_IRQ_DZ11];
791 uport->fifosize = 1;
792 uport->iotype = UPIO_MEM;
793 uport->flags = UPF_BOOT_AUTOCONF;
794 uport->ops = &dz_ops;
795 uport->line = line;
796 uport->mapbase = base;
797 }
798}
799
800#ifdef CONFIG_SERIAL_DZ_CONSOLE
801/*
802 * -------------------------------------------------------------------
803 * dz_console_putchar() -- transmit a character
804 *
805 * Polled transmission. This is tricky. We need to mask transmit
806 * interrupts so that they do not interfere, enable the transmitter
807 * for the line requested and then wait till the transmit scanner
808 * requests data for this line. But it may request data for another
809 * line first, in which case we have to disable its transmitter and
810 * repeat waiting till our line pops up. Only then the character may
811 * be transmitted. Finally, the state of the transmitter mask is
812 * restored. Welcome to the world of PDP-11!
813 * -------------------------------------------------------------------
814 */
815static void dz_console_putchar(struct uart_port *uport, int ch)
816{
817 struct dz_port *dport = to_dport(uport);
818 unsigned long flags;
819 unsigned short csr, tcr, trdy, mask;
820 int loops = 10000;
821
822 spin_lock_irqsave(&dport->port.lock, flags);
823 csr = dz_in(dport, DZ_CSR);
824 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
825 tcr = dz_in(dport, DZ_TCR);
826 tcr |= 1 << dport->port.line;
827 mask = tcr;
828 dz_out(dport, DZ_TCR, mask);
829 iob();
830 spin_unlock_irqrestore(&dport->port.lock, flags);
831
832 do {
833 trdy = dz_in(dport, DZ_CSR);
834 if (!(trdy & DZ_TRDY))
835 continue;
836 trdy = (trdy & DZ_TLINE) >> 8;
837 if (trdy == dport->port.line)
838 break;
839 mask &= ~(1 << trdy);
840 dz_out(dport, DZ_TCR, mask);
841 iob();
842 udelay(2);
843 } while (--loops);
844
845 if (loops) /* Cannot send otherwise. */
846 dz_out(dport, DZ_TDR, ch);
847
848 dz_out(dport, DZ_TCR, tcr);
849 dz_out(dport, DZ_CSR, csr);
850}
851
852/*
853 * -------------------------------------------------------------------
854 * dz_console_print ()
855 *
856 * dz_console_print is registered for printk.
857 * The console must be locked when we get here.
858 * -------------------------------------------------------------------
859 */
860static void dz_console_print(struct console *co,
861 const char *str,
862 unsigned int count)
863{
864 struct dz_port *dport = &dz_mux.dport[co->index];
865#ifdef DEBUG_DZ
866 prom_printf((char *) str);
867#endif
868 uart_console_write(&dport->port, str, count, dz_console_putchar);
869}
870
871static int __init dz_console_setup(struct console *co, char *options)
872{
873 struct dz_port *dport = &dz_mux.dport[co->index];
874 struct uart_port *uport = &dport->port;
875 int baud = 9600;
876 int bits = 8;
877 int parity = 'n';
878 int flow = 'n';
879 int ret;
880
881 ret = dz_map_port(uport);
882 if (ret)
883 return ret;
884
885 spin_lock_init(&dport->port.lock); /* For dz_pm(). */
886
887 dz_reset(dport);
888 dz_pm(uport, 0, -1);
889
890 if (options)
891 uart_parse_options(options, &baud, &parity, &bits, &flow);
892
893 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
894}
895
896static struct uart_driver dz_reg;
897static struct console dz_console = {
898 .name = "ttyS",
899 .write = dz_console_print,
900 .device = uart_console_device,
901 .setup = dz_console_setup,
902 .flags = CON_PRINTBUFFER,
903 .index = -1,
904 .data = &dz_reg,
905};
906
907static int __init dz_serial_console_init(void)
908{
909 if (!IOASIC) {
910 dz_init_ports();
911 register_console(&dz_console);
912 return 0;
913 } else
914 return -ENXIO;
915}
916
917console_initcall(dz_serial_console_init);
918
919#define SERIAL_DZ_CONSOLE &dz_console
920#else
921#define SERIAL_DZ_CONSOLE NULL
922#endif /* CONFIG_SERIAL_DZ_CONSOLE */
923
924static struct uart_driver dz_reg = {
925 .owner = THIS_MODULE,
926 .driver_name = "serial",
927 .dev_name = "ttyS",
928 .major = TTY_MAJOR,
929 .minor = 64,
930 .nr = DZ_NB_PORT,
931 .cons = SERIAL_DZ_CONSOLE,
932};
933
934static int __init dz_init(void)
935{
936 int ret, i;
937
938 if (IOASIC)
939 return -ENXIO;
940
941 printk("%s%s\n", dz_name, dz_version);
942
943 dz_init_ports();
944
945 ret = uart_register_driver(&dz_reg);
946 if (ret)
947 return ret;
948
949 for (i = 0; i < DZ_NB_PORT; i++)
950 uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
951
952 return 0;
953}
954
955module_init(dz_init);
diff --git a/drivers/tty/serial/dz.h b/drivers/tty/serial/dz.h
new file mode 100644
index 000000000000..faf169ed27b3
--- /dev/null
+++ b/drivers/tty/serial/dz.h
@@ -0,0 +1,129 @@
1/*
2 * dz.h: Serial port driver for DECstations equipped
3 * with the DZ chipset.
4 *
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
7 * Email: olivier.lebaillif@ifrsys.com
8 *
9 * Copyright (C) 2004, 2006 Maciej W. Rozycki
10 */
11#ifndef DZ_SERIAL_H
12#define DZ_SERIAL_H
13
14/*
15 * Definitions for the Control and Status Register.
16 */
17#define DZ_TRDY 0x8000 /* Transmitter empty */
18#define DZ_TIE 0x4000 /* Transmitter Interrupt Enbl */
19#define DZ_TLINE 0x0300 /* Transmitter Line Number */
20#define DZ_RDONE 0x0080 /* Receiver data ready */
21#define DZ_RIE 0x0040 /* Receive Interrupt Enable */
22#define DZ_MSE 0x0020 /* Master Scan Enable */
23#define DZ_CLR 0x0010 /* Master reset */
24#define DZ_MAINT 0x0008 /* Loop Back Mode */
25
26/*
27 * Definitions for the Receiver Buffer Register.
28 */
29#define DZ_RBUF_MASK 0x00FF /* Data Mask */
30#define DZ_LINE_MASK 0x0300 /* Line Mask */
31#define DZ_DVAL 0x8000 /* Valid Data indicator */
32#define DZ_OERR 0x4000 /* Overrun error indicator */
33#define DZ_FERR 0x2000 /* Frame error indicator */
34#define DZ_PERR 0x1000 /* Parity error indicator */
35
36#define DZ_BREAK 0x0800 /* BREAK event software flag */
37
38#define LINE(x) ((x & DZ_LINE_MASK) >> 8) /* Get the line number
39 from the input buffer */
40#define UCHAR(x) ((unsigned char)(x & DZ_RBUF_MASK))
41
42/*
43 * Definitions for the Transmit Control Register.
44 */
45#define DZ_LINE_KEYBOARD 0x0001
46#define DZ_LINE_MOUSE 0x0002
47#define DZ_LINE_MODEM 0x0004
48#define DZ_LINE_PRINTER 0x0008
49
50#define DZ_MODEM_RTS 0x0800 /* RTS for the modem line (2) */
51#define DZ_MODEM_DTR 0x0400 /* DTR for the modem line (2) */
52#define DZ_PRINT_RTS 0x0200 /* RTS for the prntr line (3) */
53#define DZ_PRINT_DTR 0x0100 /* DTR for the prntr line (3) */
54#define DZ_LNENB 0x000f /* Transmitter Line Enable */
55
56/*
57 * Definitions for the Modem Status Register.
58 */
59#define DZ_MODEM_RI 0x0800 /* RI for the modem line (2) */
60#define DZ_MODEM_CD 0x0400 /* CD for the modem line (2) */
61#define DZ_MODEM_DSR 0x0200 /* DSR for the modem line (2) */
62#define DZ_MODEM_CTS 0x0100 /* CTS for the modem line (2) */
63#define DZ_PRINT_RI 0x0008 /* RI for the printer line (3) */
64#define DZ_PRINT_CD 0x0004 /* CD for the printer line (3) */
65#define DZ_PRINT_DSR 0x0002 /* DSR for the prntr line (3) */
66#define DZ_PRINT_CTS 0x0001 /* CTS for the prntr line (3) */
67
68/*
69 * Definitions for the Transmit Data Register.
70 */
71#define DZ_BRK0 0x0100 /* Break assertion for line 0 */
72#define DZ_BRK1 0x0200 /* Break assertion for line 1 */
73#define DZ_BRK2 0x0400 /* Break assertion for line 2 */
74#define DZ_BRK3 0x0800 /* Break assertion for line 3 */
75
76/*
77 * Definitions for the Line Parameter Register.
78 */
79#define DZ_KEYBOARD 0x0000 /* line 0 = keyboard */
80#define DZ_MOUSE 0x0001 /* line 1 = mouse */
81#define DZ_MODEM 0x0002 /* line 2 = modem */
82#define DZ_PRINTER 0x0003 /* line 3 = printer */
83
84#define DZ_CSIZE 0x0018 /* Number of bits per byte (mask) */
85#define DZ_CS5 0x0000 /* 5 bits per byte */
86#define DZ_CS6 0x0008 /* 6 bits per byte */
87#define DZ_CS7 0x0010 /* 7 bits per byte */
88#define DZ_CS8 0x0018 /* 8 bits per byte */
89
90#define DZ_CSTOPB 0x0020 /* 2 stop bits instead of one */
91
92#define DZ_PARENB 0x0040 /* Parity enable */
93#define DZ_PARODD 0x0080 /* Odd parity instead of even */
94
95#define DZ_CBAUD 0x0E00 /* Baud Rate (mask) */
96#define DZ_B50 0x0000
97#define DZ_B75 0x0100
98#define DZ_B110 0x0200
99#define DZ_B134 0x0300
100#define DZ_B150 0x0400
101#define DZ_B300 0x0500
102#define DZ_B600 0x0600
103#define DZ_B1200 0x0700
104#define DZ_B1800 0x0800
105#define DZ_B2000 0x0900
106#define DZ_B2400 0x0A00
107#define DZ_B3600 0x0B00
108#define DZ_B4800 0x0C00
109#define DZ_B7200 0x0D00
110#define DZ_B9600 0x0E00
111
112#define DZ_RXENAB 0x1000 /* Receiver Enable */
113
114/*
115 * Addresses for the DZ registers
116 */
117#define DZ_CSR 0x00 /* Control and Status Register */
118#define DZ_RBUF 0x08 /* Receive Buffer */
119#define DZ_LPR 0x08 /* Line Parameters Register */
120#define DZ_TCR 0x10 /* Transmitter Control Register */
121#define DZ_MSR 0x18 /* Modem Status Register */
122#define DZ_TDR 0x18 /* Transmit Data Register */
123
124#define DZ_NB_PORT 4
125
126#define DZ_XMIT_SIZE 4096 /* buffer size */
127#define DZ_WAKEUP_CHARS DZ_XMIT_SIZE/4
128
129#endif /* DZ_SERIAL_H */
diff --git a/drivers/tty/serial/icom.c b/drivers/tty/serial/icom.c
new file mode 100644
index 000000000000..8a869e58f6d7
--- /dev/null
+++ b/drivers/tty/serial/icom.c
@@ -0,0 +1,1658 @@
1/*
2 * icom.c
3 *
4 * Copyright (C) 2001 IBM Corporation. All rights reserved.
5 *
6 * Serial device driver.
7 *
8 * Based on code from serial.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25#define SERIAL_DO_RESTART
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/signal.h>
30#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/termios.h>
34#include <linux/fs.h>
35#include <linux/tty_flip.h>
36#include <linux/serial.h>
37#include <linux/serial_reg.h>
38#include <linux/major.h>
39#include <linux/string.h>
40#include <linux/fcntl.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/mm.h>
44#include <linux/slab.h>
45#include <linux/init.h>
46#include <linux/delay.h>
47#include <linux/pci.h>
48#include <linux/vmalloc.h>
49#include <linux/smp.h>
50#include <linux/spinlock.h>
51#include <linux/kref.h>
52#include <linux/firmware.h>
53#include <linux/bitops.h>
54
55#include <asm/system.h>
56#include <asm/io.h>
57#include <asm/irq.h>
58#include <asm/uaccess.h>
59
60#include "icom.h"
61
62/*#define ICOM_TRACE enable port trace capabilities */
63
64#define ICOM_DRIVER_NAME "icom"
65#define ICOM_VERSION_STR "1.3.1"
66#define NR_PORTS 128
67#define ICOM_PORT ((struct icom_port *)port)
68#define to_icom_adapter(d) container_of(d, struct icom_adapter, kref)
69
70static const struct pci_device_id icom_pci_table[] = {
71 {
72 .vendor = PCI_VENDOR_ID_IBM,
73 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_1,
74 .subvendor = PCI_ANY_ID,
75 .subdevice = PCI_ANY_ID,
76 .driver_data = ADAPTER_V1,
77 },
78 {
79 .vendor = PCI_VENDOR_ID_IBM,
80 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
81 .subvendor = PCI_VENDOR_ID_IBM,
82 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_TWO_PORTS_RVX,
83 .driver_data = ADAPTER_V2,
84 },
85 {
86 .vendor = PCI_VENDOR_ID_IBM,
87 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
88 .subvendor = PCI_VENDOR_ID_IBM,
89 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM,
90 .driver_data = ADAPTER_V2,
91 },
92 {
93 .vendor = PCI_VENDOR_ID_IBM,
94 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
95 .subvendor = PCI_VENDOR_ID_IBM,
96 .subdevice = PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL,
97 .driver_data = ADAPTER_V2,
98 },
99 {
100 .vendor = PCI_VENDOR_ID_IBM,
101 .device = PCI_DEVICE_ID_IBM_ICOM_DEV_ID_2,
102 .subvendor = PCI_VENDOR_ID_IBM,
103 .subdevice = PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM_PCIE,
104 .driver_data = ADAPTER_V2,
105 },
106 {}
107};
108
109struct lookup_proc_table start_proc[4] = {
110 {NULL, ICOM_CONTROL_START_A},
111 {NULL, ICOM_CONTROL_START_B},
112 {NULL, ICOM_CONTROL_START_C},
113 {NULL, ICOM_CONTROL_START_D}
114};
115
116
117struct lookup_proc_table stop_proc[4] = {
118 {NULL, ICOM_CONTROL_STOP_A},
119 {NULL, ICOM_CONTROL_STOP_B},
120 {NULL, ICOM_CONTROL_STOP_C},
121 {NULL, ICOM_CONTROL_STOP_D}
122};
123
124struct lookup_int_table int_mask_tbl[4] = {
125 {NULL, ICOM_INT_MASK_PRC_A},
126 {NULL, ICOM_INT_MASK_PRC_B},
127 {NULL, ICOM_INT_MASK_PRC_C},
128 {NULL, ICOM_INT_MASK_PRC_D},
129};
130
131
132MODULE_DEVICE_TABLE(pci, icom_pci_table);
133
134static LIST_HEAD(icom_adapter_head);
135
136/* spinlock for adapter initialization and changing adapter operations */
137static spinlock_t icom_lock;
138
139#ifdef ICOM_TRACE
140static inline void trace(struct icom_port *icom_port, char *trace_pt,
141 unsigned long trace_data)
142{
143 dev_info(&icom_port->adapter->pci_dev->dev, ":%d:%s - %lx\n",
144 icom_port->port, trace_pt, trace_data);
145}
146#else
147static inline void trace(struct icom_port *icom_port, char *trace_pt, unsigned long trace_data) {};
148#endif
149static void icom_kref_release(struct kref *kref);
150
151static void free_port_memory(struct icom_port *icom_port)
152{
153 struct pci_dev *dev = icom_port->adapter->pci_dev;
154
155 trace(icom_port, "RET_PORT_MEM", 0);
156 if (icom_port->recv_buf) {
157 pci_free_consistent(dev, 4096, icom_port->recv_buf,
158 icom_port->recv_buf_pci);
159 icom_port->recv_buf = NULL;
160 }
161 if (icom_port->xmit_buf) {
162 pci_free_consistent(dev, 4096, icom_port->xmit_buf,
163 icom_port->xmit_buf_pci);
164 icom_port->xmit_buf = NULL;
165 }
166 if (icom_port->statStg) {
167 pci_free_consistent(dev, 4096, icom_port->statStg,
168 icom_port->statStg_pci);
169 icom_port->statStg = NULL;
170 }
171
172 if (icom_port->xmitRestart) {
173 pci_free_consistent(dev, 4096, icom_port->xmitRestart,
174 icom_port->xmitRestart_pci);
175 icom_port->xmitRestart = NULL;
176 }
177}
178
179static int __devinit get_port_memory(struct icom_port *icom_port)
180{
181 int index;
182 unsigned long stgAddr;
183 unsigned long startStgAddr;
184 unsigned long offset;
185 struct pci_dev *dev = icom_port->adapter->pci_dev;
186
187 icom_port->xmit_buf =
188 pci_alloc_consistent(dev, 4096, &icom_port->xmit_buf_pci);
189 if (!icom_port->xmit_buf) {
190 dev_err(&dev->dev, "Can not allocate Transmit buffer\n");
191 return -ENOMEM;
192 }
193
194 trace(icom_port, "GET_PORT_MEM",
195 (unsigned long) icom_port->xmit_buf);
196
197 icom_port->recv_buf =
198 pci_alloc_consistent(dev, 4096, &icom_port->recv_buf_pci);
199 if (!icom_port->recv_buf) {
200 dev_err(&dev->dev, "Can not allocate Receive buffer\n");
201 free_port_memory(icom_port);
202 return -ENOMEM;
203 }
204 trace(icom_port, "GET_PORT_MEM",
205 (unsigned long) icom_port->recv_buf);
206
207 icom_port->statStg =
208 pci_alloc_consistent(dev, 4096, &icom_port->statStg_pci);
209 if (!icom_port->statStg) {
210 dev_err(&dev->dev, "Can not allocate Status buffer\n");
211 free_port_memory(icom_port);
212 return -ENOMEM;
213 }
214 trace(icom_port, "GET_PORT_MEM",
215 (unsigned long) icom_port->statStg);
216
217 icom_port->xmitRestart =
218 pci_alloc_consistent(dev, 4096, &icom_port->xmitRestart_pci);
219 if (!icom_port->xmitRestart) {
220 dev_err(&dev->dev,
221 "Can not allocate xmit Restart buffer\n");
222 free_port_memory(icom_port);
223 return -ENOMEM;
224 }
225
226 memset(icom_port->statStg, 0, 4096);
227
228 /* FODs: Frame Out Descriptor Queue, this is a FIFO queue that
229 indicates that frames are to be transmitted
230 */
231
232 stgAddr = (unsigned long) icom_port->statStg;
233 for (index = 0; index < NUM_XBUFFS; index++) {
234 trace(icom_port, "FOD_ADDR", stgAddr);
235 stgAddr = stgAddr + sizeof(icom_port->statStg->xmit[0]);
236 if (index < (NUM_XBUFFS - 1)) {
237 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
238 icom_port->statStg->xmit[index].leLengthASD =
239 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
240 trace(icom_port, "FOD_ADDR", stgAddr);
241 trace(icom_port, "FOD_XBUFF",
242 (unsigned long) icom_port->xmit_buf);
243 icom_port->statStg->xmit[index].leBuffer =
244 cpu_to_le32(icom_port->xmit_buf_pci);
245 } else if (index == (NUM_XBUFFS - 1)) {
246 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
247 icom_port->statStg->xmit[index].leLengthASD =
248 (unsigned short int) cpu_to_le16(XMIT_BUFF_SZ);
249 trace(icom_port, "FOD_XBUFF",
250 (unsigned long) icom_port->xmit_buf);
251 icom_port->statStg->xmit[index].leBuffer =
252 cpu_to_le32(icom_port->xmit_buf_pci);
253 } else {
254 memset(&icom_port->statStg->xmit[index], 0, sizeof(struct xmit_status_area));
255 }
256 }
257 /* FIDs */
258 startStgAddr = stgAddr;
259
260 /* fill in every entry, even if no buffer */
261 for (index = 0; index < NUM_RBUFFS; index++) {
262 trace(icom_port, "FID_ADDR", stgAddr);
263 stgAddr = stgAddr + sizeof(icom_port->statStg->rcv[0]);
264 icom_port->statStg->rcv[index].leLength = 0;
265 icom_port->statStg->rcv[index].WorkingLength =
266 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
267 if (index < (NUM_RBUFFS - 1) ) {
268 offset = stgAddr - (unsigned long) icom_port->statStg;
269 icom_port->statStg->rcv[index].leNext =
270 cpu_to_le32(icom_port-> statStg_pci + offset);
271 trace(icom_port, "FID_RBUFF",
272 (unsigned long) icom_port->recv_buf);
273 icom_port->statStg->rcv[index].leBuffer =
274 cpu_to_le32(icom_port->recv_buf_pci);
275 } else if (index == (NUM_RBUFFS -1) ) {
276 offset = startStgAddr - (unsigned long) icom_port->statStg;
277 icom_port->statStg->rcv[index].leNext =
278 cpu_to_le32(icom_port-> statStg_pci + offset);
279 trace(icom_port, "FID_RBUFF",
280 (unsigned long) icom_port->recv_buf + 2048);
281 icom_port->statStg->rcv[index].leBuffer =
282 cpu_to_le32(icom_port->recv_buf_pci + 2048);
283 } else {
284 icom_port->statStg->rcv[index].leNext = 0;
285 icom_port->statStg->rcv[index].leBuffer = 0;
286 }
287 }
288
289 return 0;
290}
291
292static void stop_processor(struct icom_port *icom_port)
293{
294 unsigned long temp;
295 unsigned long flags;
296 int port;
297
298 spin_lock_irqsave(&icom_lock, flags);
299
300 port = icom_port->port;
301 if (port == 0 || port == 1)
302 stop_proc[port].global_control_reg = &icom_port->global_reg->control;
303 else
304 stop_proc[port].global_control_reg = &icom_port->global_reg->control_2;
305
306
307 if (port < 4) {
308 temp = readl(stop_proc[port].global_control_reg);
309 temp =
310 (temp & ~start_proc[port].processor_id) | stop_proc[port].processor_id;
311 writel(temp, stop_proc[port].global_control_reg);
312
313 /* write flush */
314 readl(stop_proc[port].global_control_reg);
315 } else {
316 dev_err(&icom_port->adapter->pci_dev->dev,
317 "Invalid port assignment\n");
318 }
319
320 spin_unlock_irqrestore(&icom_lock, flags);
321}
322
323static void start_processor(struct icom_port *icom_port)
324{
325 unsigned long temp;
326 unsigned long flags;
327 int port;
328
329 spin_lock_irqsave(&icom_lock, flags);
330
331 port = icom_port->port;
332 if (port == 0 || port == 1)
333 start_proc[port].global_control_reg = &icom_port->global_reg->control;
334 else
335 start_proc[port].global_control_reg = &icom_port->global_reg->control_2;
336 if (port < 4) {
337 temp = readl(start_proc[port].global_control_reg);
338 temp =
339 (temp & ~stop_proc[port].processor_id) | start_proc[port].processor_id;
340 writel(temp, start_proc[port].global_control_reg);
341
342 /* write flush */
343 readl(start_proc[port].global_control_reg);
344 } else {
345 dev_err(&icom_port->adapter->pci_dev->dev,
346 "Invalid port assignment\n");
347 }
348
349 spin_unlock_irqrestore(&icom_lock, flags);
350}
351
352static void load_code(struct icom_port *icom_port)
353{
354 const struct firmware *fw;
355 char __iomem *iram_ptr;
356 int index;
357 int status = 0;
358 void __iomem *dram_ptr = icom_port->dram;
359 dma_addr_t temp_pci;
360 unsigned char *new_page = NULL;
361 unsigned char cable_id = NO_CABLE;
362 struct pci_dev *dev = icom_port->adapter->pci_dev;
363
364 /* Clear out any pending interrupts */
365 writew(0x3FFF, icom_port->int_reg);
366
367 trace(icom_port, "CLEAR_INTERRUPTS", 0);
368
369 /* Stop processor */
370 stop_processor(icom_port);
371
372 /* Zero out DRAM */
373 memset_io(dram_ptr, 0, 512);
374
375 /* Load Call Setup into Adapter */
376 if (request_firmware(&fw, "icom_call_setup.bin", &dev->dev) < 0) {
377 dev_err(&dev->dev,"Unable to load icom_call_setup.bin firmware image\n");
378 status = -1;
379 goto load_code_exit;
380 }
381
382 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
383 dev_err(&dev->dev, "Invalid firmware image for icom_call_setup.bin found.\n");
384 release_firmware(fw);
385 status = -1;
386 goto load_code_exit;
387 }
388
389 iram_ptr = (char __iomem *)icom_port->dram + ICOM_IRAM_OFFSET;
390 for (index = 0; index < fw->size; index++)
391 writeb(fw->data[index], &iram_ptr[index]);
392
393 release_firmware(fw);
394
395 /* Load Resident DCE portion of Adapter */
396 if (request_firmware(&fw, "icom_res_dce.bin", &dev->dev) < 0) {
397 dev_err(&dev->dev,"Unable to load icom_res_dce.bin firmware image\n");
398 status = -1;
399 goto load_code_exit;
400 }
401
402 if (fw->size > ICOM_IRAM_SIZE) {
403 dev_err(&dev->dev, "Invalid firmware image for icom_res_dce.bin found.\n");
404 release_firmware(fw);
405 status = -1;
406 goto load_code_exit;
407 }
408
409 iram_ptr = (char __iomem *) icom_port->dram + ICOM_IRAM_OFFSET;
410 for (index = ICOM_DCE_IRAM_OFFSET; index < fw->size; index++)
411 writeb(fw->data[index], &iram_ptr[index]);
412
413 release_firmware(fw);
414
415 /* Set Hardware level */
416 if (icom_port->adapter->version == ADAPTER_V2)
417 writeb(V2_HARDWARE, &(icom_port->dram->misc_flags));
418
419 /* Start the processor in Adapter */
420 start_processor(icom_port);
421
422 writeb((HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL),
423 &(icom_port->dram->HDLCConfigReg));
424 writeb(0x04, &(icom_port->dram->FlagFillIdleTimer)); /* 0.5 seconds */
425 writeb(0x00, &(icom_port->dram->CmdReg));
426 writeb(0x10, &(icom_port->dram->async_config3));
427 writeb((ICOM_ACFG_DRIVE1 | ICOM_ACFG_NO_PARITY | ICOM_ACFG_8BPC |
428 ICOM_ACFG_1STOP_BIT), &(icom_port->dram->async_config2));
429
430 /*Set up data in icom DRAM to indicate where personality
431 *code is located and its length.
432 */
433 new_page = pci_alloc_consistent(dev, 4096, &temp_pci);
434
435 if (!new_page) {
436 dev_err(&dev->dev, "Can not allocate DMA buffer\n");
437 status = -1;
438 goto load_code_exit;
439 }
440
441 if (request_firmware(&fw, "icom_asc.bin", &dev->dev) < 0) {
442 dev_err(&dev->dev,"Unable to load icom_asc.bin firmware image\n");
443 status = -1;
444 goto load_code_exit;
445 }
446
447 if (fw->size > ICOM_DCE_IRAM_OFFSET) {
448 dev_err(&dev->dev, "Invalid firmware image for icom_asc.bin found.\n");
449 release_firmware(fw);
450 status = -1;
451 goto load_code_exit;
452 }
453
454 for (index = 0; index < fw->size; index++)
455 new_page[index] = fw->data[index];
456
457 release_firmware(fw);
458
459 writeb((char) ((fw->size + 16)/16), &icom_port->dram->mac_length);
460 writel(temp_pci, &icom_port->dram->mac_load_addr);
461
462 /*Setting the syncReg to 0x80 causes adapter to start downloading
463 the personality code into adapter instruction RAM.
464 Once code is loaded, it will begin executing and, based on
465 information provided above, will start DMAing data from
466 shared memory to adapter DRAM.
467 */
468 /* the wait loop below verifies this write operation has been done
469 and processed
470 */
471 writeb(START_DOWNLOAD, &icom_port->dram->sync);
472
473 /* Wait max 1 Sec for data download and processor to start */
474 for (index = 0; index < 10; index++) {
475 msleep(100);
476 if (readb(&icom_port->dram->misc_flags) & ICOM_HDW_ACTIVE)
477 break;
478 }
479
480 if (index == 10)
481 status = -1;
482
483 /*
484 * check Cable ID
485 */
486 cable_id = readb(&icom_port->dram->cable_id);
487
488 if (cable_id & ICOM_CABLE_ID_VALID) {
489 /* Get cable ID into the lower 4 bits (standard form) */
490 cable_id = (cable_id & ICOM_CABLE_ID_MASK) >> 4;
491 icom_port->cable_id = cable_id;
492 } else {
493 dev_err(&dev->dev,"Invalid or no cable attached\n");
494 icom_port->cable_id = NO_CABLE;
495 }
496
497 load_code_exit:
498
499 if (status != 0) {
500 /* Clear out any pending interrupts */
501 writew(0x3FFF, icom_port->int_reg);
502
503 /* Turn off port */
504 writeb(ICOM_DISABLE, &(icom_port->dram->disable));
505
506 /* Stop processor */
507 stop_processor(icom_port);
508
509 dev_err(&icom_port->adapter->pci_dev->dev,"Port not opertional\n");
510 }
511
512 if (new_page != NULL)
513 pci_free_consistent(dev, 4096, new_page, temp_pci);
514}
515
516static int startup(struct icom_port *icom_port)
517{
518 unsigned long temp;
519 unsigned char cable_id, raw_cable_id;
520 unsigned long flags;
521 int port;
522
523 trace(icom_port, "STARTUP", 0);
524
525 if (!icom_port->dram) {
526 /* should NEVER be NULL */
527 dev_err(&icom_port->adapter->pci_dev->dev,
528 "Unusable Port, port configuration missing\n");
529 return -ENODEV;
530 }
531
532 /*
533 * check Cable ID
534 */
535 raw_cable_id = readb(&icom_port->dram->cable_id);
536 trace(icom_port, "CABLE_ID", raw_cable_id);
537
538 /* Get cable ID into the lower 4 bits (standard form) */
539 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
540
541 /* Check for valid Cable ID */
542 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
543 (cable_id != icom_port->cable_id)) {
544
545 /* reload adapter code, pick up any potential changes in cable id */
546 load_code(icom_port);
547
548 /* still no sign of cable, error out */
549 raw_cable_id = readb(&icom_port->dram->cable_id);
550 cable_id = (raw_cable_id & ICOM_CABLE_ID_MASK) >> 4;
551 if (!(raw_cable_id & ICOM_CABLE_ID_VALID) ||
552 (icom_port->cable_id == NO_CABLE))
553 return -EIO;
554 }
555
556 /*
557 * Finally, clear and enable interrupts
558 */
559 spin_lock_irqsave(&icom_lock, flags);
560 port = icom_port->port;
561 if (port == 0 || port == 1)
562 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
563 else
564 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
565
566 if (port == 0 || port == 2)
567 writew(0x00FF, icom_port->int_reg);
568 else
569 writew(0x3F00, icom_port->int_reg);
570 if (port < 4) {
571 temp = readl(int_mask_tbl[port].global_int_mask);
572 writel(temp & ~int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
573
574 /* write flush */
575 readl(int_mask_tbl[port].global_int_mask);
576 } else {
577 dev_err(&icom_port->adapter->pci_dev->dev,
578 "Invalid port assignment\n");
579 }
580
581 spin_unlock_irqrestore(&icom_lock, flags);
582 return 0;
583}
584
585static void shutdown(struct icom_port *icom_port)
586{
587 unsigned long temp;
588 unsigned char cmdReg;
589 unsigned long flags;
590 int port;
591
592 spin_lock_irqsave(&icom_lock, flags);
593 trace(icom_port, "SHUTDOWN", 0);
594
595 /*
596 * disable all interrupts
597 */
598 port = icom_port->port;
599 if (port == 0 || port == 1)
600 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask;
601 else
602 int_mask_tbl[port].global_int_mask = &icom_port->global_reg->int_mask_2;
603
604 if (port < 4) {
605 temp = readl(int_mask_tbl[port].global_int_mask);
606 writel(temp | int_mask_tbl[port].processor_id, int_mask_tbl[port].global_int_mask);
607
608 /* write flush */
609 readl(int_mask_tbl[port].global_int_mask);
610 } else {
611 dev_err(&icom_port->adapter->pci_dev->dev,
612 "Invalid port assignment\n");
613 }
614 spin_unlock_irqrestore(&icom_lock, flags);
615
616 /*
617 * disable break condition
618 */
619 cmdReg = readb(&icom_port->dram->CmdReg);
620 if (cmdReg & CMD_SND_BREAK) {
621 writeb(cmdReg & ~CMD_SND_BREAK, &icom_port->dram->CmdReg);
622 }
623}
624
625static int icom_write(struct uart_port *port)
626{
627 unsigned long data_count;
628 unsigned char cmdReg;
629 unsigned long offset;
630 int temp_tail = port->state->xmit.tail;
631
632 trace(ICOM_PORT, "WRITE", 0);
633
634 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
635 SA_FLAGS_READY_TO_XMIT) {
636 trace(ICOM_PORT, "WRITE_FULL", 0);
637 return 0;
638 }
639
640 data_count = 0;
641 while ((port->state->xmit.head != temp_tail) &&
642 (data_count <= XMIT_BUFF_SZ)) {
643
644 ICOM_PORT->xmit_buf[data_count++] =
645 port->state->xmit.buf[temp_tail];
646
647 temp_tail++;
648 temp_tail &= (UART_XMIT_SIZE - 1);
649 }
650
651 if (data_count) {
652 ICOM_PORT->statStg->xmit[0].flags =
653 cpu_to_le16(SA_FLAGS_READY_TO_XMIT);
654 ICOM_PORT->statStg->xmit[0].leLength =
655 cpu_to_le16(data_count);
656 offset =
657 (unsigned long) &ICOM_PORT->statStg->xmit[0] -
658 (unsigned long) ICOM_PORT->statStg;
659 *ICOM_PORT->xmitRestart =
660 cpu_to_le32(ICOM_PORT->statStg_pci + offset);
661 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
662 writeb(cmdReg | CMD_XMIT_RCV_ENABLE,
663 &ICOM_PORT->dram->CmdReg);
664 writeb(START_XMIT, &ICOM_PORT->dram->StartXmitCmd);
665 trace(ICOM_PORT, "WRITE_START", data_count);
666 /* write flush */
667 readb(&ICOM_PORT->dram->StartXmitCmd);
668 }
669
670 return data_count;
671}
672
673static inline void check_modem_status(struct icom_port *icom_port)
674{
675 static char old_status = 0;
676 char delta_status;
677 unsigned char status;
678
679 spin_lock(&icom_port->uart_port.lock);
680
681 /*modem input register */
682 status = readb(&icom_port->dram->isr);
683 trace(icom_port, "CHECK_MODEM", status);
684 delta_status = status ^ old_status;
685 if (delta_status) {
686 if (delta_status & ICOM_RI)
687 icom_port->uart_port.icount.rng++;
688 if (delta_status & ICOM_DSR)
689 icom_port->uart_port.icount.dsr++;
690 if (delta_status & ICOM_DCD)
691 uart_handle_dcd_change(&icom_port->uart_port,
692 delta_status & ICOM_DCD);
693 if (delta_status & ICOM_CTS)
694 uart_handle_cts_change(&icom_port->uart_port,
695 delta_status & ICOM_CTS);
696
697 wake_up_interruptible(&icom_port->uart_port.state->
698 port.delta_msr_wait);
699 old_status = status;
700 }
701 spin_unlock(&icom_port->uart_port.lock);
702}
703
704static void xmit_interrupt(u16 port_int_reg, struct icom_port *icom_port)
705{
706 unsigned short int count;
707 int i;
708
709 if (port_int_reg & (INT_XMIT_COMPLETED)) {
710 trace(icom_port, "XMIT_COMPLETE", 0);
711
712 /* clear buffer in use bit */
713 icom_port->statStg->xmit[0].flags &=
714 cpu_to_le16(~SA_FLAGS_READY_TO_XMIT);
715
716 count = (unsigned short int)
717 cpu_to_le16(icom_port->statStg->xmit[0].leLength);
718 icom_port->uart_port.icount.tx += count;
719
720 for (i=0; i<count &&
721 !uart_circ_empty(&icom_port->uart_port.state->xmit); i++) {
722
723 icom_port->uart_port.state->xmit.tail++;
724 icom_port->uart_port.state->xmit.tail &=
725 (UART_XMIT_SIZE - 1);
726 }
727
728 if (!icom_write(&icom_port->uart_port))
729 /* activate write queue */
730 uart_write_wakeup(&icom_port->uart_port);
731 } else
732 trace(icom_port, "XMIT_DISABLED", 0);
733}
734
735static void recv_interrupt(u16 port_int_reg, struct icom_port *icom_port)
736{
737 short int count, rcv_buff;
738 struct tty_struct *tty = icom_port->uart_port.state->port.tty;
739 unsigned short int status;
740 struct uart_icount *icount;
741 unsigned long offset;
742 unsigned char flag;
743
744 trace(icom_port, "RCV_COMPLETE", 0);
745 rcv_buff = icom_port->next_rcv;
746
747 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
748 while (status & SA_FL_RCV_DONE) {
749 int first = -1;
750
751 trace(icom_port, "FID_STATUS", status);
752 count = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].leLength);
753
754 trace(icom_port, "RCV_COUNT", count);
755
756 trace(icom_port, "REAL_COUNT", count);
757
758 offset =
759 cpu_to_le32(icom_port->statStg->rcv[rcv_buff].leBuffer) -
760 icom_port->recv_buf_pci;
761
762 /* Block copy all but the last byte as this may have status */
763 if (count > 0) {
764 first = icom_port->recv_buf[offset];
765 tty_insert_flip_string(tty, icom_port->recv_buf + offset, count - 1);
766 }
767
768 icount = &icom_port->uart_port.icount;
769 icount->rx += count;
770
771 /* Break detect logic */
772 if ((status & SA_FLAGS_FRAME_ERROR)
773 && first == 0) {
774 status &= ~SA_FLAGS_FRAME_ERROR;
775 status |= SA_FLAGS_BREAK_DET;
776 trace(icom_port, "BREAK_DET", 0);
777 }
778
779 flag = TTY_NORMAL;
780
781 if (status &
782 (SA_FLAGS_BREAK_DET | SA_FLAGS_PARITY_ERROR |
783 SA_FLAGS_FRAME_ERROR | SA_FLAGS_OVERRUN)) {
784
785 if (status & SA_FLAGS_BREAK_DET)
786 icount->brk++;
787 if (status & SA_FLAGS_PARITY_ERROR)
788 icount->parity++;
789 if (status & SA_FLAGS_FRAME_ERROR)
790 icount->frame++;
791 if (status & SA_FLAGS_OVERRUN)
792 icount->overrun++;
793
794 /*
795 * Now check to see if character should be
796 * ignored, and mask off conditions which
797 * should be ignored.
798 */
799 if (status & icom_port->ignore_status_mask) {
800 trace(icom_port, "IGNORE_CHAR", 0);
801 goto ignore_char;
802 }
803
804 status &= icom_port->read_status_mask;
805
806 if (status & SA_FLAGS_BREAK_DET) {
807 flag = TTY_BREAK;
808 } else if (status & SA_FLAGS_PARITY_ERROR) {
809 trace(icom_port, "PARITY_ERROR", 0);
810 flag = TTY_PARITY;
811 } else if (status & SA_FLAGS_FRAME_ERROR)
812 flag = TTY_FRAME;
813
814 }
815
816 tty_insert_flip_char(tty, *(icom_port->recv_buf + offset + count - 1), flag);
817
818 if (status & SA_FLAGS_OVERRUN)
819 /*
820 * Overrun is special, since it's
821 * reported immediately, and doesn't
822 * affect the current character
823 */
824 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
825ignore_char:
826 icom_port->statStg->rcv[rcv_buff].flags = 0;
827 icom_port->statStg->rcv[rcv_buff].leLength = 0;
828 icom_port->statStg->rcv[rcv_buff].WorkingLength =
829 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
830
831 rcv_buff++;
832 if (rcv_buff == NUM_RBUFFS)
833 rcv_buff = 0;
834
835 status = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].flags);
836 }
837 icom_port->next_rcv = rcv_buff;
838 tty_flip_buffer_push(tty);
839}
840
841static void process_interrupt(u16 port_int_reg,
842 struct icom_port *icom_port)
843{
844
845 spin_lock(&icom_port->uart_port.lock);
846 trace(icom_port, "INTERRUPT", port_int_reg);
847
848 if (port_int_reg & (INT_XMIT_COMPLETED | INT_XMIT_DISABLED))
849 xmit_interrupt(port_int_reg, icom_port);
850
851 if (port_int_reg & INT_RCV_COMPLETED)
852 recv_interrupt(port_int_reg, icom_port);
853
854 spin_unlock(&icom_port->uart_port.lock);
855}
856
857static irqreturn_t icom_interrupt(int irq, void *dev_id)
858{
859 void __iomem * int_reg;
860 u32 adapter_interrupts;
861 u16 port_int_reg;
862 struct icom_adapter *icom_adapter;
863 struct icom_port *icom_port;
864
865 /* find icom_port for this interrupt */
866 icom_adapter = (struct icom_adapter *) dev_id;
867
868 if (icom_adapter->version == ADAPTER_V2) {
869 int_reg = icom_adapter->base_addr + 0x8024;
870
871 adapter_interrupts = readl(int_reg);
872
873 if (adapter_interrupts & 0x00003FFF) {
874 /* port 2 interrupt, NOTE: for all ADAPTER_V2, port 2 will be active */
875 icom_port = &icom_adapter->port_info[2];
876 port_int_reg = (u16) adapter_interrupts;
877 process_interrupt(port_int_reg, icom_port);
878 check_modem_status(icom_port);
879 }
880 if (adapter_interrupts & 0x3FFF0000) {
881 /* port 3 interrupt */
882 icom_port = &icom_adapter->port_info[3];
883 if (icom_port->status == ICOM_PORT_ACTIVE) {
884 port_int_reg =
885 (u16) (adapter_interrupts >> 16);
886 process_interrupt(port_int_reg, icom_port);
887 check_modem_status(icom_port);
888 }
889 }
890
891 /* Clear out any pending interrupts */
892 writel(adapter_interrupts, int_reg);
893
894 int_reg = icom_adapter->base_addr + 0x8004;
895 } else {
896 int_reg = icom_adapter->base_addr + 0x4004;
897 }
898
899 adapter_interrupts = readl(int_reg);
900
901 if (adapter_interrupts & 0x00003FFF) {
902 /* port 0 interrupt, NOTE: for all adapters, port 0 will be active */
903 icom_port = &icom_adapter->port_info[0];
904 port_int_reg = (u16) adapter_interrupts;
905 process_interrupt(port_int_reg, icom_port);
906 check_modem_status(icom_port);
907 }
908 if (adapter_interrupts & 0x3FFF0000) {
909 /* port 1 interrupt */
910 icom_port = &icom_adapter->port_info[1];
911 if (icom_port->status == ICOM_PORT_ACTIVE) {
912 port_int_reg = (u16) (adapter_interrupts >> 16);
913 process_interrupt(port_int_reg, icom_port);
914 check_modem_status(icom_port);
915 }
916 }
917
918 /* Clear out any pending interrupts */
919 writel(adapter_interrupts, int_reg);
920
921 /* flush the write */
922 adapter_interrupts = readl(int_reg);
923
924 return IRQ_HANDLED;
925}
926
927/*
928 * ------------------------------------------------------------------
929 * Begin serial-core API
930 * ------------------------------------------------------------------
931 */
932static unsigned int icom_tx_empty(struct uart_port *port)
933{
934 int ret;
935 unsigned long flags;
936
937 spin_lock_irqsave(&port->lock, flags);
938 if (cpu_to_le16(ICOM_PORT->statStg->xmit[0].flags) &
939 SA_FLAGS_READY_TO_XMIT)
940 ret = TIOCSER_TEMT;
941 else
942 ret = 0;
943
944 spin_unlock_irqrestore(&port->lock, flags);
945 return ret;
946}
947
948static void icom_set_mctrl(struct uart_port *port, unsigned int mctrl)
949{
950 unsigned char local_osr;
951
952 trace(ICOM_PORT, "SET_MODEM", 0);
953 local_osr = readb(&ICOM_PORT->dram->osr);
954
955 if (mctrl & TIOCM_RTS) {
956 trace(ICOM_PORT, "RAISE_RTS", 0);
957 local_osr |= ICOM_RTS;
958 } else {
959 trace(ICOM_PORT, "LOWER_RTS", 0);
960 local_osr &= ~ICOM_RTS;
961 }
962
963 if (mctrl & TIOCM_DTR) {
964 trace(ICOM_PORT, "RAISE_DTR", 0);
965 local_osr |= ICOM_DTR;
966 } else {
967 trace(ICOM_PORT, "LOWER_DTR", 0);
968 local_osr &= ~ICOM_DTR;
969 }
970
971 writeb(local_osr, &ICOM_PORT->dram->osr);
972}
973
974static unsigned int icom_get_mctrl(struct uart_port *port)
975{
976 unsigned char status;
977 unsigned int result;
978
979 trace(ICOM_PORT, "GET_MODEM", 0);
980
981 status = readb(&ICOM_PORT->dram->isr);
982
983 result = ((status & ICOM_DCD) ? TIOCM_CAR : 0)
984 | ((status & ICOM_RI) ? TIOCM_RNG : 0)
985 | ((status & ICOM_DSR) ? TIOCM_DSR : 0)
986 | ((status & ICOM_CTS) ? TIOCM_CTS : 0);
987 return result;
988}
989
990static void icom_stop_tx(struct uart_port *port)
991{
992 unsigned char cmdReg;
993
994 trace(ICOM_PORT, "STOP", 0);
995 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
996 writeb(cmdReg | CMD_HOLD_XMIT, &ICOM_PORT->dram->CmdReg);
997}
998
999static void icom_start_tx(struct uart_port *port)
1000{
1001 unsigned char cmdReg;
1002
1003 trace(ICOM_PORT, "START", 0);
1004 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1005 if ((cmdReg & CMD_HOLD_XMIT) == CMD_HOLD_XMIT)
1006 writeb(cmdReg & ~CMD_HOLD_XMIT,
1007 &ICOM_PORT->dram->CmdReg);
1008
1009 icom_write(port);
1010}
1011
1012static void icom_send_xchar(struct uart_port *port, char ch)
1013{
1014 unsigned char xdata;
1015 int index;
1016 unsigned long flags;
1017
1018 trace(ICOM_PORT, "SEND_XCHAR", ch);
1019
1020 /* wait .1 sec to send char */
1021 for (index = 0; index < 10; index++) {
1022 spin_lock_irqsave(&port->lock, flags);
1023 xdata = readb(&ICOM_PORT->dram->xchar);
1024 if (xdata == 0x00) {
1025 trace(ICOM_PORT, "QUICK_WRITE", 0);
1026 writeb(ch, &ICOM_PORT->dram->xchar);
1027
1028 /* flush write operation */
1029 xdata = readb(&ICOM_PORT->dram->xchar);
1030 spin_unlock_irqrestore(&port->lock, flags);
1031 break;
1032 }
1033 spin_unlock_irqrestore(&port->lock, flags);
1034 msleep(10);
1035 }
1036}
1037
1038static void icom_stop_rx(struct uart_port *port)
1039{
1040 unsigned char cmdReg;
1041
1042 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1043 writeb(cmdReg & ~CMD_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1044}
1045
1046static void icom_enable_ms(struct uart_port *port)
1047{
1048 /* no-op */
1049}
1050
1051static void icom_break(struct uart_port *port, int break_state)
1052{
1053 unsigned char cmdReg;
1054 unsigned long flags;
1055
1056 spin_lock_irqsave(&port->lock, flags);
1057 trace(ICOM_PORT, "BREAK", 0);
1058 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1059 if (break_state == -1) {
1060 writeb(cmdReg | CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1061 } else {
1062 writeb(cmdReg & ~CMD_SND_BREAK, &ICOM_PORT->dram->CmdReg);
1063 }
1064 spin_unlock_irqrestore(&port->lock, flags);
1065}
1066
1067static int icom_open(struct uart_port *port)
1068{
1069 int retval;
1070
1071 kref_get(&ICOM_PORT->adapter->kref);
1072 retval = startup(ICOM_PORT);
1073
1074 if (retval) {
1075 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1076 trace(ICOM_PORT, "STARTUP_ERROR", 0);
1077 return retval;
1078 }
1079
1080 return 0;
1081}
1082
1083static void icom_close(struct uart_port *port)
1084{
1085 unsigned char cmdReg;
1086
1087 trace(ICOM_PORT, "CLOSE", 0);
1088
1089 /* stop receiver */
1090 cmdReg = readb(&ICOM_PORT->dram->CmdReg);
1091 writeb(cmdReg & (unsigned char) ~CMD_RCV_ENABLE,
1092 &ICOM_PORT->dram->CmdReg);
1093
1094 shutdown(ICOM_PORT);
1095
1096 kref_put(&ICOM_PORT->adapter->kref, icom_kref_release);
1097}
1098
1099static void icom_set_termios(struct uart_port *port,
1100 struct ktermios *termios,
1101 struct ktermios *old_termios)
1102{
1103 int baud;
1104 unsigned cflag, iflag;
1105 char new_config2;
1106 char new_config3 = 0;
1107 char tmp_byte;
1108 int index;
1109 int rcv_buff, xmit_buff;
1110 unsigned long offset;
1111 unsigned long flags;
1112
1113 spin_lock_irqsave(&port->lock, flags);
1114 trace(ICOM_PORT, "CHANGE_SPEED", 0);
1115
1116 cflag = termios->c_cflag;
1117 iflag = termios->c_iflag;
1118
1119 new_config2 = ICOM_ACFG_DRIVE1;
1120
1121 /* byte size and parity */
1122 switch (cflag & CSIZE) {
1123 case CS5: /* 5 bits/char */
1124 new_config2 |= ICOM_ACFG_5BPC;
1125 break;
1126 case CS6: /* 6 bits/char */
1127 new_config2 |= ICOM_ACFG_6BPC;
1128 break;
1129 case CS7: /* 7 bits/char */
1130 new_config2 |= ICOM_ACFG_7BPC;
1131 break;
1132 case CS8: /* 8 bits/char */
1133 new_config2 |= ICOM_ACFG_8BPC;
1134 break;
1135 default:
1136 break;
1137 }
1138 if (cflag & CSTOPB) {
1139 /* 2 stop bits */
1140 new_config2 |= ICOM_ACFG_2STOP_BIT;
1141 }
1142 if (cflag & PARENB) {
1143 /* parity bit enabled */
1144 new_config2 |= ICOM_ACFG_PARITY_ENAB;
1145 trace(ICOM_PORT, "PARENB", 0);
1146 }
1147 if (cflag & PARODD) {
1148 /* odd parity */
1149 new_config2 |= ICOM_ACFG_PARITY_ODD;
1150 trace(ICOM_PORT, "PARODD", 0);
1151 }
1152
1153 /* Determine divisor based on baud rate */
1154 baud = uart_get_baud_rate(port, termios, old_termios,
1155 icom_acfg_baud[0],
1156 icom_acfg_baud[BAUD_TABLE_LIMIT]);
1157 if (!baud)
1158 baud = 9600; /* B0 transition handled in rs_set_termios */
1159
1160 for (index = 0; index < BAUD_TABLE_LIMIT; index++) {
1161 if (icom_acfg_baud[index] == baud) {
1162 new_config3 = index;
1163 break;
1164 }
1165 }
1166
1167 uart_update_timeout(port, cflag, baud);
1168
1169 /* CTS flow control flag and modem status interrupts */
1170 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1171 if (cflag & CRTSCTS)
1172 tmp_byte |= HDLC_HDW_FLOW;
1173 else
1174 tmp_byte &= ~HDLC_HDW_FLOW;
1175 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1176
1177 /*
1178 * Set up parity check flag
1179 */
1180 ICOM_PORT->read_status_mask = SA_FLAGS_OVERRUN | SA_FL_RCV_DONE;
1181 if (iflag & INPCK)
1182 ICOM_PORT->read_status_mask |=
1183 SA_FLAGS_FRAME_ERROR | SA_FLAGS_PARITY_ERROR;
1184
1185 if ((iflag & BRKINT) || (iflag & PARMRK))
1186 ICOM_PORT->read_status_mask |= SA_FLAGS_BREAK_DET;
1187
1188 /*
1189 * Characters to ignore
1190 */
1191 ICOM_PORT->ignore_status_mask = 0;
1192 if (iflag & IGNPAR)
1193 ICOM_PORT->ignore_status_mask |=
1194 SA_FLAGS_PARITY_ERROR | SA_FLAGS_FRAME_ERROR;
1195 if (iflag & IGNBRK) {
1196 ICOM_PORT->ignore_status_mask |= SA_FLAGS_BREAK_DET;
1197 /*
1198 * If we're ignore parity and break indicators, ignore
1199 * overruns too. (For real raw support).
1200 */
1201 if (iflag & IGNPAR)
1202 ICOM_PORT->ignore_status_mask |= SA_FLAGS_OVERRUN;
1203 }
1204
1205 /*
1206 * !!! ignore all characters if CREAD is not set
1207 */
1208 if ((cflag & CREAD) == 0)
1209 ICOM_PORT->ignore_status_mask |= SA_FL_RCV_DONE;
1210
1211 /* Turn off Receiver to prepare for reset */
1212 writeb(CMD_RCV_DISABLE, &ICOM_PORT->dram->CmdReg);
1213
1214 for (index = 0; index < 10; index++) {
1215 if (readb(&ICOM_PORT->dram->PrevCmdReg) == 0x00) {
1216 break;
1217 }
1218 }
1219
1220 /* clear all current buffers of data */
1221 for (rcv_buff = 0; rcv_buff < NUM_RBUFFS; rcv_buff++) {
1222 ICOM_PORT->statStg->rcv[rcv_buff].flags = 0;
1223 ICOM_PORT->statStg->rcv[rcv_buff].leLength = 0;
1224 ICOM_PORT->statStg->rcv[rcv_buff].WorkingLength =
1225 (unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
1226 }
1227
1228 for (xmit_buff = 0; xmit_buff < NUM_XBUFFS; xmit_buff++) {
1229 ICOM_PORT->statStg->xmit[xmit_buff].flags = 0;
1230 }
1231
1232 /* activate changes and start xmit and receiver here */
1233 /* Enable the receiver */
1234 writeb(new_config3, &(ICOM_PORT->dram->async_config3));
1235 writeb(new_config2, &(ICOM_PORT->dram->async_config2));
1236 tmp_byte = readb(&(ICOM_PORT->dram->HDLCConfigReg));
1237 tmp_byte |= HDLC_PPP_PURE_ASYNC | HDLC_FF_FILL;
1238 writeb(tmp_byte, &(ICOM_PORT->dram->HDLCConfigReg));
1239 writeb(0x04, &(ICOM_PORT->dram->FlagFillIdleTimer)); /* 0.5 seconds */
1240 writeb(0xFF, &(ICOM_PORT->dram->ier)); /* enable modem signal interrupts */
1241
1242 /* reset processor */
1243 writeb(CMD_RESTART, &ICOM_PORT->dram->CmdReg);
1244
1245 for (index = 0; index < 10; index++) {
1246 if (readb(&ICOM_PORT->dram->CmdReg) == 0x00) {
1247 break;
1248 }
1249 }
1250
1251 /* Enable Transmitter and Receiver */
1252 offset =
1253 (unsigned long) &ICOM_PORT->statStg->rcv[0] -
1254 (unsigned long) ICOM_PORT->statStg;
1255 writel(ICOM_PORT->statStg_pci + offset,
1256 &ICOM_PORT->dram->RcvStatusAddr);
1257 ICOM_PORT->next_rcv = 0;
1258 ICOM_PORT->put_length = 0;
1259 *ICOM_PORT->xmitRestart = 0;
1260 writel(ICOM_PORT->xmitRestart_pci,
1261 &ICOM_PORT->dram->XmitStatusAddr);
1262 trace(ICOM_PORT, "XR_ENAB", 0);
1263 writeb(CMD_XMIT_RCV_ENABLE, &ICOM_PORT->dram->CmdReg);
1264
1265 spin_unlock_irqrestore(&port->lock, flags);
1266}
1267
1268static const char *icom_type(struct uart_port *port)
1269{
1270 return "icom";
1271}
1272
1273static void icom_release_port(struct uart_port *port)
1274{
1275}
1276
1277static int icom_request_port(struct uart_port *port)
1278{
1279 return 0;
1280}
1281
1282static void icom_config_port(struct uart_port *port, int flags)
1283{
1284 port->type = PORT_ICOM;
1285}
1286
1287static struct uart_ops icom_ops = {
1288 .tx_empty = icom_tx_empty,
1289 .set_mctrl = icom_set_mctrl,
1290 .get_mctrl = icom_get_mctrl,
1291 .stop_tx = icom_stop_tx,
1292 .start_tx = icom_start_tx,
1293 .send_xchar = icom_send_xchar,
1294 .stop_rx = icom_stop_rx,
1295 .enable_ms = icom_enable_ms,
1296 .break_ctl = icom_break,
1297 .startup = icom_open,
1298 .shutdown = icom_close,
1299 .set_termios = icom_set_termios,
1300 .type = icom_type,
1301 .release_port = icom_release_port,
1302 .request_port = icom_request_port,
1303 .config_port = icom_config_port,
1304};
1305
1306#define ICOM_CONSOLE NULL
1307
1308static struct uart_driver icom_uart_driver = {
1309 .owner = THIS_MODULE,
1310 .driver_name = ICOM_DRIVER_NAME,
1311 .dev_name = "ttyA",
1312 .major = ICOM_MAJOR,
1313 .minor = ICOM_MINOR_START,
1314 .nr = NR_PORTS,
1315 .cons = ICOM_CONSOLE,
1316};
1317
1318static int __devinit icom_init_ports(struct icom_adapter *icom_adapter)
1319{
1320 u32 subsystem_id = icom_adapter->subsystem_id;
1321 int i;
1322 struct icom_port *icom_port;
1323
1324 if (icom_adapter->version == ADAPTER_V1) {
1325 icom_adapter->numb_ports = 2;
1326
1327 for (i = 0; i < 2; i++) {
1328 icom_port = &icom_adapter->port_info[i];
1329 icom_port->port = i;
1330 icom_port->status = ICOM_PORT_ACTIVE;
1331 icom_port->imbed_modem = ICOM_UNKNOWN;
1332 }
1333 } else {
1334 if (subsystem_id == PCI_DEVICE_ID_IBM_ICOM_FOUR_PORT_MODEL) {
1335 icom_adapter->numb_ports = 4;
1336
1337 for (i = 0; i < 4; i++) {
1338 icom_port = &icom_adapter->port_info[i];
1339
1340 icom_port->port = i;
1341 icom_port->status = ICOM_PORT_ACTIVE;
1342 icom_port->imbed_modem = ICOM_IMBED_MODEM;
1343 }
1344 } else {
1345 icom_adapter->numb_ports = 4;
1346
1347 icom_adapter->port_info[0].port = 0;
1348 icom_adapter->port_info[0].status = ICOM_PORT_ACTIVE;
1349
1350 if (subsystem_id ==
1351 PCI_DEVICE_ID_IBM_ICOM_V2_ONE_PORT_RVX_ONE_PORT_MDM) {
1352 icom_adapter->port_info[0].imbed_modem = ICOM_IMBED_MODEM;
1353 } else {
1354 icom_adapter->port_info[0].imbed_modem = ICOM_RVX;
1355 }
1356
1357 icom_adapter->port_info[1].status = ICOM_PORT_OFF;
1358
1359 icom_adapter->port_info[2].port = 2;
1360 icom_adapter->port_info[2].status = ICOM_PORT_ACTIVE;
1361 icom_adapter->port_info[2].imbed_modem = ICOM_RVX;
1362 icom_adapter->port_info[3].status = ICOM_PORT_OFF;
1363 }
1364 }
1365
1366 return 0;
1367}
1368
1369static void icom_port_active(struct icom_port *icom_port, struct icom_adapter *icom_adapter, int port_num)
1370{
1371 if (icom_adapter->version == ADAPTER_V1) {
1372 icom_port->global_reg = icom_adapter->base_addr + 0x4000;
1373 icom_port->int_reg = icom_adapter->base_addr +
1374 0x4004 + 2 - 2 * port_num;
1375 } else {
1376 icom_port->global_reg = icom_adapter->base_addr + 0x8000;
1377 if (icom_port->port < 2)
1378 icom_port->int_reg = icom_adapter->base_addr +
1379 0x8004 + 2 - 2 * icom_port->port;
1380 else
1381 icom_port->int_reg = icom_adapter->base_addr +
1382 0x8024 + 2 - 2 * (icom_port->port - 2);
1383 }
1384}
1385static int __devinit icom_load_ports(struct icom_adapter *icom_adapter)
1386{
1387 struct icom_port *icom_port;
1388 int port_num;
1389
1390 for (port_num = 0; port_num < icom_adapter->numb_ports; port_num++) {
1391
1392 icom_port = &icom_adapter->port_info[port_num];
1393
1394 if (icom_port->status == ICOM_PORT_ACTIVE) {
1395 icom_port_active(icom_port, icom_adapter, port_num);
1396 icom_port->dram = icom_adapter->base_addr +
1397 0x2000 * icom_port->port;
1398
1399 icom_port->adapter = icom_adapter;
1400
1401 /* get port memory */
1402 if (get_port_memory(icom_port) != 0) {
1403 dev_err(&icom_port->adapter->pci_dev->dev,
1404 "Memory allocation for port FAILED\n");
1405 }
1406 }
1407 }
1408 return 0;
1409}
1410
1411static int __devinit icom_alloc_adapter(struct icom_adapter
1412 **icom_adapter_ref)
1413{
1414 int adapter_count = 0;
1415 struct icom_adapter *icom_adapter;
1416 struct icom_adapter *cur_adapter_entry;
1417 struct list_head *tmp;
1418
1419 icom_adapter = (struct icom_adapter *)
1420 kzalloc(sizeof(struct icom_adapter), GFP_KERNEL);
1421
1422 if (!icom_adapter) {
1423 return -ENOMEM;
1424 }
1425
1426 list_for_each(tmp, &icom_adapter_head) {
1427 cur_adapter_entry =
1428 list_entry(tmp, struct icom_adapter,
1429 icom_adapter_entry);
1430 if (cur_adapter_entry->index != adapter_count) {
1431 break;
1432 }
1433 adapter_count++;
1434 }
1435
1436 icom_adapter->index = adapter_count;
1437 list_add_tail(&icom_adapter->icom_adapter_entry, tmp);
1438
1439 *icom_adapter_ref = icom_adapter;
1440 return 0;
1441}
1442
1443static void icom_free_adapter(struct icom_adapter *icom_adapter)
1444{
1445 list_del(&icom_adapter->icom_adapter_entry);
1446 kfree(icom_adapter);
1447}
1448
1449static void icom_remove_adapter(struct icom_adapter *icom_adapter)
1450{
1451 struct icom_port *icom_port;
1452 int index;
1453
1454 for (index = 0; index < icom_adapter->numb_ports; index++) {
1455 icom_port = &icom_adapter->port_info[index];
1456
1457 if (icom_port->status == ICOM_PORT_ACTIVE) {
1458 dev_info(&icom_adapter->pci_dev->dev,
1459 "Device removed\n");
1460
1461 uart_remove_one_port(&icom_uart_driver,
1462 &icom_port->uart_port);
1463
1464 /* be sure that DTR and RTS are dropped */
1465 writeb(0x00, &icom_port->dram->osr);
1466
1467 /* Wait 0.1 Sec for simple Init to complete */
1468 msleep(100);
1469
1470 /* Stop proccessor */
1471 stop_processor(icom_port);
1472
1473 free_port_memory(icom_port);
1474 }
1475 }
1476
1477 free_irq(icom_adapter->pci_dev->irq, (void *) icom_adapter);
1478 iounmap(icom_adapter->base_addr);
1479 pci_release_regions(icom_adapter->pci_dev);
1480 icom_free_adapter(icom_adapter);
1481}
1482
1483static void icom_kref_release(struct kref *kref)
1484{
1485 struct icom_adapter *icom_adapter;
1486
1487 icom_adapter = to_icom_adapter(kref);
1488 icom_remove_adapter(icom_adapter);
1489}
1490
1491static int __devinit icom_probe(struct pci_dev *dev,
1492 const struct pci_device_id *ent)
1493{
1494 int index;
1495 unsigned int command_reg;
1496 int retval;
1497 struct icom_adapter *icom_adapter;
1498 struct icom_port *icom_port;
1499
1500 retval = pci_enable_device(dev);
1501 if (retval) {
1502 dev_err(&dev->dev, "Device enable FAILED\n");
1503 return retval;
1504 }
1505
1506 if ( (retval = pci_request_regions(dev, "icom"))) {
1507 dev_err(&dev->dev, "pci_request_regions FAILED\n");
1508 pci_disable_device(dev);
1509 return retval;
1510 }
1511
1512 pci_set_master(dev);
1513
1514 if ( (retval = pci_read_config_dword(dev, PCI_COMMAND, &command_reg))) {
1515 dev_err(&dev->dev, "PCI Config read FAILED\n");
1516 return retval;
1517 }
1518
1519 pci_write_config_dword(dev, PCI_COMMAND,
1520 command_reg | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1521 | PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1522
1523 if (ent->driver_data == ADAPTER_V1) {
1524 pci_write_config_dword(dev, 0x44, 0x8300830A);
1525 } else {
1526 pci_write_config_dword(dev, 0x44, 0x42004200);
1527 pci_write_config_dword(dev, 0x48, 0x42004200);
1528 }
1529
1530
1531 retval = icom_alloc_adapter(&icom_adapter);
1532 if (retval) {
1533 dev_err(&dev->dev, "icom_alloc_adapter FAILED\n");
1534 retval = -EIO;
1535 goto probe_exit0;
1536 }
1537
1538 icom_adapter->base_addr_pci = pci_resource_start(dev, 0);
1539 icom_adapter->pci_dev = dev;
1540 icom_adapter->version = ent->driver_data;
1541 icom_adapter->subsystem_id = ent->subdevice;
1542
1543
1544 retval = icom_init_ports(icom_adapter);
1545 if (retval) {
1546 dev_err(&dev->dev, "Port configuration failed\n");
1547 goto probe_exit1;
1548 }
1549
1550 icom_adapter->base_addr = pci_ioremap_bar(dev, 0);
1551
1552 if (!icom_adapter->base_addr)
1553 goto probe_exit1;
1554
1555 /* save off irq and request irq line */
1556 if ( (retval = request_irq(dev->irq, icom_interrupt,
1557 IRQF_DISABLED | IRQF_SHARED, ICOM_DRIVER_NAME,
1558 (void *) icom_adapter))) {
1559 goto probe_exit2;
1560 }
1561
1562 retval = icom_load_ports(icom_adapter);
1563
1564 for (index = 0; index < icom_adapter->numb_ports; index++) {
1565 icom_port = &icom_adapter->port_info[index];
1566
1567 if (icom_port->status == ICOM_PORT_ACTIVE) {
1568 icom_port->uart_port.irq = icom_port->adapter->pci_dev->irq;
1569 icom_port->uart_port.type = PORT_ICOM;
1570 icom_port->uart_port.iotype = UPIO_MEM;
1571 icom_port->uart_port.membase =
1572 (char *) icom_adapter->base_addr_pci;
1573 icom_port->uart_port.fifosize = 16;
1574 icom_port->uart_port.ops = &icom_ops;
1575 icom_port->uart_port.line =
1576 icom_port->port + icom_adapter->index * 4;
1577 if (uart_add_one_port (&icom_uart_driver, &icom_port->uart_port)) {
1578 icom_port->status = ICOM_PORT_OFF;
1579 dev_err(&dev->dev, "Device add failed\n");
1580 } else
1581 dev_info(&dev->dev, "Device added\n");
1582 }
1583 }
1584
1585 kref_init(&icom_adapter->kref);
1586 return 0;
1587
1588probe_exit2:
1589 iounmap(icom_adapter->base_addr);
1590probe_exit1:
1591 icom_free_adapter(icom_adapter);
1592
1593probe_exit0:
1594 pci_release_regions(dev);
1595 pci_disable_device(dev);
1596
1597 return retval;
1598}
1599
1600static void __devexit icom_remove(struct pci_dev *dev)
1601{
1602 struct icom_adapter *icom_adapter;
1603 struct list_head *tmp;
1604
1605 list_for_each(tmp, &icom_adapter_head) {
1606 icom_adapter = list_entry(tmp, struct icom_adapter,
1607 icom_adapter_entry);
1608 if (icom_adapter->pci_dev == dev) {
1609 kref_put(&icom_adapter->kref, icom_kref_release);
1610 return;
1611 }
1612 }
1613
1614 dev_err(&dev->dev, "Unable to find device to remove\n");
1615}
1616
1617static struct pci_driver icom_pci_driver = {
1618 .name = ICOM_DRIVER_NAME,
1619 .id_table = icom_pci_table,
1620 .probe = icom_probe,
1621 .remove = __devexit_p(icom_remove),
1622};
1623
1624static int __init icom_init(void)
1625{
1626 int ret;
1627
1628 spin_lock_init(&icom_lock);
1629
1630 ret = uart_register_driver(&icom_uart_driver);
1631 if (ret)
1632 return ret;
1633
1634 ret = pci_register_driver(&icom_pci_driver);
1635
1636 if (ret < 0)
1637 uart_unregister_driver(&icom_uart_driver);
1638
1639 return ret;
1640}
1641
1642static void __exit icom_exit(void)
1643{
1644 pci_unregister_driver(&icom_pci_driver);
1645 uart_unregister_driver(&icom_uart_driver);
1646}
1647
1648module_init(icom_init);
1649module_exit(icom_exit);
1650
1651MODULE_AUTHOR("Michael Anderson <mjanders@us.ibm.com>");
1652MODULE_DESCRIPTION("IBM iSeries Serial IOA driver");
1653MODULE_SUPPORTED_DEVICE
1654 ("IBM iSeries 2745, 2771, 2772, 2742, 2793 and 2805 Communications adapters");
1655MODULE_LICENSE("GPL");
1656MODULE_FIRMWARE("icom_call_setup.bin");
1657MODULE_FIRMWARE("icom_res_dce.bin");
1658MODULE_FIRMWARE("icom_asc.bin");
diff --git a/drivers/tty/serial/icom.h b/drivers/tty/serial/icom.h
new file mode 100644
index 000000000000..c8029e0025c9
--- /dev/null
+++ b/drivers/tty/serial/icom.h
@@ -0,0 +1,287 @@
1/*
2 * icom.h
3 *
4 * Copyright (C) 2001 Michael Anderson, IBM Corporation
5 *
6 * Serial device driver include file.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/serial_core.h>
24
25#define BAUD_TABLE_LIMIT ((sizeof(icom_acfg_baud)/sizeof(int)) - 1)
26static int icom_acfg_baud[] = {
27 300,
28 600,
29 900,
30 1200,
31 1800,
32 2400,
33 3600,
34 4800,
35 7200,
36 9600,
37 14400,
38 19200,
39 28800,
40 38400,
41 57600,
42 76800,
43 115200,
44 153600,
45 230400,
46 307200,
47 460800,
48};
49
50struct icom_regs {
51 u32 control; /* Adapter Control Register */
52 u32 interrupt; /* Adapter Interrupt Register */
53 u32 int_mask; /* Adapter Interrupt Mask Reg */
54 u32 int_pri; /* Adapter Interrupt Priority r */
55 u32 int_reg_b; /* Adapter non-masked Interrupt */
56 u32 resvd01;
57 u32 resvd02;
58 u32 resvd03;
59 u32 control_2; /* Adapter Control Register 2 */
60 u32 interrupt_2; /* Adapter Interrupt Register 2 */
61 u32 int_mask_2; /* Adapter Interrupt Mask 2 */
62 u32 int_pri_2; /* Adapter Interrupt Prior 2 */
63 u32 int_reg_2b; /* Adapter non-masked 2 */
64};
65
66struct func_dram {
67 u32 reserved[108]; /* 0-1B0 reserved by personality code */
68 u32 RcvStatusAddr; /* 1B0-1B3 Status Address for Next rcv */
69 u8 RcvStnAddr; /* 1B4 Receive Station Addr */
70 u8 IdleState; /* 1B5 Idle State */
71 u8 IdleMonitor; /* 1B6 Idle Monitor */
72 u8 FlagFillIdleTimer; /* 1B7 Flag Fill Idle Timer */
73 u32 XmitStatusAddr; /* 1B8-1BB Transmit Status Address */
74 u8 StartXmitCmd; /* 1BC Start Xmit Command */
75 u8 HDLCConfigReg; /* 1BD Reserved */
76 u8 CauseCode; /* 1BE Cause code for fatal error */
77 u8 xchar; /* 1BF High priority send */
78 u32 reserved3; /* 1C0-1C3 Reserved */
79 u8 PrevCmdReg; /* 1C4 Reserved */
80 u8 CmdReg; /* 1C5 Command Register */
81 u8 async_config2; /* 1C6 Async Config Byte 2 */
82 u8 async_config3; /* 1C7 Async Config Byte 3 */
83 u8 dce_resvd[20]; /* 1C8-1DB DCE Rsvd */
84 u8 dce_resvd21; /* 1DC DCE Rsvd (21st byte */
85 u8 misc_flags; /* 1DD misc flags */
86#define V2_HARDWARE 0x40
87#define ICOM_HDW_ACTIVE 0x01
88 u8 call_length; /* 1DE Phone #/CFI buff ln */
89 u8 call_length2; /* 1DF Upper byte (unused) */
90 u32 call_addr; /* 1E0-1E3 Phn #/CFI buff addr */
91 u16 timer_value; /* 1E4-1E5 general timer value */
92 u8 timer_command; /* 1E6 general timer cmd */
93 u8 dce_command; /* 1E7 dce command reg */
94 u8 dce_cmd_status; /* 1E8 dce command stat */
95 u8 x21_r1_ioff; /* 1E9 dce ready counter */
96 u8 x21_r0_ioff; /* 1EA dce not ready ctr */
97 u8 x21_ralt_ioff; /* 1EB dce CNR counter */
98 u8 x21_r1_ion; /* 1EC dce ready I on ctr */
99 u8 rsvd_ier; /* 1ED Rsvd for IER (if ne */
100 u8 ier; /* 1EE Interrupt Enable */
101 u8 isr; /* 1EF Input Signal Reg */
102 u8 osr; /* 1F0 Output Signal Reg */
103 u8 reset; /* 1F1 Reset/Reload Reg */
104 u8 disable; /* 1F2 Disable Reg */
105 u8 sync; /* 1F3 Sync Reg */
106 u8 error_stat; /* 1F4 Error Status */
107 u8 cable_id; /* 1F5 Cable ID */
108 u8 cs_length; /* 1F6 CS Load Length */
109 u8 mac_length; /* 1F7 Mac Load Length */
110 u32 cs_load_addr; /* 1F8-1FB Call Load PCI Addr */
111 u32 mac_load_addr; /* 1FC-1FF Mac Load PCI Addr */
112};
113
114/*
115 * adapter defines and structures
116 */
117#define ICOM_CONTROL_START_A 0x00000008
118#define ICOM_CONTROL_STOP_A 0x00000004
119#define ICOM_CONTROL_START_B 0x00000002
120#define ICOM_CONTROL_STOP_B 0x00000001
121#define ICOM_CONTROL_START_C 0x00000008
122#define ICOM_CONTROL_STOP_C 0x00000004
123#define ICOM_CONTROL_START_D 0x00000002
124#define ICOM_CONTROL_STOP_D 0x00000001
125#define ICOM_IRAM_OFFSET 0x1000
126#define ICOM_IRAM_SIZE 0x0C00
127#define ICOM_DCE_IRAM_OFFSET 0x0A00
128#define ICOM_CABLE_ID_VALID 0x01
129#define ICOM_CABLE_ID_MASK 0xF0
130#define ICOM_DISABLE 0x80
131#define CMD_XMIT_RCV_ENABLE 0xC0
132#define CMD_XMIT_ENABLE 0x40
133#define CMD_RCV_DISABLE 0x00
134#define CMD_RCV_ENABLE 0x80
135#define CMD_RESTART 0x01
136#define CMD_HOLD_XMIT 0x02
137#define CMD_SND_BREAK 0x04
138#define RS232_CABLE 0x06
139#define V24_CABLE 0x0E
140#define V35_CABLE 0x0C
141#define V36_CABLE 0x02
142#define NO_CABLE 0x00
143#define START_DOWNLOAD 0x80
144#define ICOM_INT_MASK_PRC_A 0x00003FFF
145#define ICOM_INT_MASK_PRC_B 0x3FFF0000
146#define ICOM_INT_MASK_PRC_C 0x00003FFF
147#define ICOM_INT_MASK_PRC_D 0x3FFF0000
148#define INT_RCV_COMPLETED 0x1000
149#define INT_XMIT_COMPLETED 0x2000
150#define INT_IDLE_DETECT 0x0800
151#define INT_RCV_DISABLED 0x0400
152#define INT_XMIT_DISABLED 0x0200
153#define INT_RCV_XMIT_SHUTDOWN 0x0100
154#define INT_FATAL_ERROR 0x0080
155#define INT_CABLE_PULL 0x0020
156#define INT_SIGNAL_CHANGE 0x0010
157#define HDLC_PPP_PURE_ASYNC 0x02
158#define HDLC_FF_FILL 0x00
159#define HDLC_HDW_FLOW 0x01
160#define START_XMIT 0x80
161#define ICOM_ACFG_DRIVE1 0x20
162#define ICOM_ACFG_NO_PARITY 0x00
163#define ICOM_ACFG_PARITY_ENAB 0x02
164#define ICOM_ACFG_PARITY_ODD 0x01
165#define ICOM_ACFG_8BPC 0x00
166#define ICOM_ACFG_7BPC 0x04
167#define ICOM_ACFG_6BPC 0x08
168#define ICOM_ACFG_5BPC 0x0C
169#define ICOM_ACFG_1STOP_BIT 0x00
170#define ICOM_ACFG_2STOP_BIT 0x10
171#define ICOM_DTR 0x80
172#define ICOM_RTS 0x40
173#define ICOM_RI 0x08
174#define ICOM_DSR 0x80
175#define ICOM_DCD 0x20
176#define ICOM_CTS 0x40
177
178#define NUM_XBUFFS 1
179#define NUM_RBUFFS 2
180#define RCV_BUFF_SZ 0x0200
181#define XMIT_BUFF_SZ 0x1000
182struct statusArea {
183 /**********************************************/
184 /* Transmit Status Area */
185 /**********************************************/
186 struct xmit_status_area{
187 u32 leNext; /* Next entry in Little Endian on Adapter */
188 u32 leNextASD;
189 u32 leBuffer; /* Buffer for entry in LE for Adapter */
190 u16 leLengthASD;
191 u16 leOffsetASD;
192 u16 leLength; /* Length of data in segment */
193 u16 flags;
194#define SA_FLAGS_DONE 0x0080 /* Done with Segment */
195#define SA_FLAGS_CONTINUED 0x8000 /* More Segments */
196#define SA_FLAGS_IDLE 0x4000 /* Mark IDLE after frm */
197#define SA_FLAGS_READY_TO_XMIT 0x0800
198#define SA_FLAGS_STAT_MASK 0x007F
199 } xmit[NUM_XBUFFS];
200
201 /**********************************************/
202 /* Receive Status Area */
203 /**********************************************/
204 struct {
205 u32 leNext; /* Next entry in Little Endian on Adapter */
206 u32 leNextASD;
207 u32 leBuffer; /* Buffer for entry in LE for Adapter */
208 u16 WorkingLength; /* size of segment */
209 u16 reserv01;
210 u16 leLength; /* Length of data in segment */
211 u16 flags;
212#define SA_FL_RCV_DONE 0x0010 /* Data ready */
213#define SA_FLAGS_OVERRUN 0x0040
214#define SA_FLAGS_PARITY_ERROR 0x0080
215#define SA_FLAGS_FRAME_ERROR 0x0001
216#define SA_FLAGS_FRAME_TRUNC 0x0002
217#define SA_FLAGS_BREAK_DET 0x0004 /* set conditionally by device driver, not hardware */
218#define SA_FLAGS_RCV_MASK 0xFFE6
219 } rcv[NUM_RBUFFS];
220};
221
222struct icom_adapter;
223
224
225#define ICOM_MAJOR 243
226#define ICOM_MINOR_START 0
227
228struct icom_port {
229 struct uart_port uart_port;
230 u8 imbed_modem;
231#define ICOM_UNKNOWN 1
232#define ICOM_RVX 2
233#define ICOM_IMBED_MODEM 3
234 unsigned char cable_id;
235 unsigned char read_status_mask;
236 unsigned char ignore_status_mask;
237 void __iomem * int_reg;
238 struct icom_regs __iomem *global_reg;
239 struct func_dram __iomem *dram;
240 int port;
241 struct statusArea *statStg;
242 dma_addr_t statStg_pci;
243 u32 *xmitRestart;
244 dma_addr_t xmitRestart_pci;
245 unsigned char *xmit_buf;
246 dma_addr_t xmit_buf_pci;
247 unsigned char *recv_buf;
248 dma_addr_t recv_buf_pci;
249 int next_rcv;
250 int put_length;
251 int status;
252#define ICOM_PORT_ACTIVE 1 /* Port exists. */
253#define ICOM_PORT_OFF 0 /* Port does not exist. */
254 int load_in_progress;
255 struct icom_adapter *adapter;
256};
257
258struct icom_adapter {
259 void __iomem * base_addr;
260 unsigned long base_addr_pci;
261 struct pci_dev *pci_dev;
262 struct icom_port port_info[4];
263 int index;
264 int version;
265#define ADAPTER_V1 0x0001
266#define ADAPTER_V2 0x0002
267 u32 subsystem_id;
268#define FOUR_PORT_MODEL 0x0252
269#define V2_TWO_PORTS_RVX 0x021A
270#define V2_ONE_PORT_RVX_ONE_PORT_IMBED_MDM 0x0251
271 int numb_ports;
272 struct list_head icom_adapter_entry;
273 struct kref kref;
274};
275
276/* prototype */
277extern void iCom_sercons_init(void);
278
279struct lookup_proc_table {
280 u32 __iomem *global_control_reg;
281 unsigned long processor_id;
282};
283
284struct lookup_int_table {
285 u32 __iomem *global_int_mask;
286 unsigned long processor_id;
287};
diff --git a/drivers/tty/serial/ifx6x60.c b/drivers/tty/serial/ifx6x60.c
new file mode 100644
index 000000000000..5315525220fb
--- /dev/null
+++ b/drivers/tty/serial/ifx6x60.c
@@ -0,0 +1,1414 @@
1/****************************************************************************
2 *
3 * Driver for the IFX 6x60 spi modem.
4 *
5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com>
8 * Jan Dumon <j.dumon@option.com>
9 *
10 * Copyright (C) 2009, 2010 Intel Corp
11 * Russ Gorby <russ.gorby@intel.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 * USA
26 *
27 * Driver modified by Intel from Option gtm501l_spi.c
28 *
29 * Notes
30 * o The driver currently assumes a single device only. If you need to
31 * change this then look for saved_ifx_dev and add a device lookup
32 * o The driver is intended to be big-endian safe but has never been
33 * tested that way (no suitable hardware). There are a couple of FIXME
34 * notes by areas that may need addressing
35 * o Some of the GPIO naming/setup assumptions may need revisiting if
36 * you need to use this driver for another platform.
37 *
38 *****************************************************************************/
39#include <linux/module.h>
40#include <linux/termios.h>
41#include <linux/tty.h>
42#include <linux/device.h>
43#include <linux/spi/spi.h>
44#include <linux/kfifo.h>
45#include <linux/tty_flip.h>
46#include <linux/timer.h>
47#include <linux/serial.h>
48#include <linux/interrupt.h>
49#include <linux/irq.h>
50#include <linux/rfkill.h>
51#include <linux/fs.h>
52#include <linux/ip.h>
53#include <linux/dmapool.h>
54#include <linux/gpio.h>
55#include <linux/sched.h>
56#include <linux/time.h>
57#include <linux/wait.h>
58#include <linux/pm.h>
59#include <linux/pm_runtime.h>
60#include <linux/spi/ifx_modem.h>
61#include <linux/delay.h>
62
63#include "ifx6x60.h"
64
65#define IFX_SPI_MORE_MASK 0x10
66#define IFX_SPI_MORE_BIT 12 /* bit position in u16 */
67#define IFX_SPI_CTS_BIT 13 /* bit position in u16 */
68#define IFX_SPI_MODE SPI_MODE_1
69#define IFX_SPI_TTY_ID 0
70#define IFX_SPI_TIMEOUT_SEC 2
71#define IFX_SPI_HEADER_0 (-1)
72#define IFX_SPI_HEADER_F (-2)
73
74/* forward reference */
75static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
76
77/* local variables */
78static int spi_bpw = 16; /* 8, 16 or 32 bit word length */
79static struct tty_driver *tty_drv;
80static struct ifx_spi_device *saved_ifx_dev;
81static struct lock_class_key ifx_spi_key;
82
83/* GPIO/GPE settings */
84
85/**
86 * mrdy_set_high - set MRDY GPIO
87 * @ifx: device we are controlling
88 *
89 */
90static inline void mrdy_set_high(struct ifx_spi_device *ifx)
91{
92 gpio_set_value(ifx->gpio.mrdy, 1);
93}
94
95/**
96 * mrdy_set_low - clear MRDY GPIO
97 * @ifx: device we are controlling
98 *
99 */
100static inline void mrdy_set_low(struct ifx_spi_device *ifx)
101{
102 gpio_set_value(ifx->gpio.mrdy, 0);
103}
104
105/**
106 * ifx_spi_power_state_set
107 * @ifx_dev: our SPI device
108 * @val: bits to set
109 *
110 * Set bit in power status and signal power system if status becomes non-0
111 */
112static void
113ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
114{
115 unsigned long flags;
116
117 spin_lock_irqsave(&ifx_dev->power_lock, flags);
118
119 /*
120 * if power status is already non-0, just update, else
121 * tell power system
122 */
123 if (!ifx_dev->power_status)
124 pm_runtime_get(&ifx_dev->spi_dev->dev);
125 ifx_dev->power_status |= val;
126
127 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
128}
129
130/**
131 * ifx_spi_power_state_clear - clear power bit
132 * @ifx_dev: our SPI device
133 * @val: bits to clear
134 *
135 * clear bit in power status and signal power system if status becomes 0
136 */
137static void
138ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
139{
140 unsigned long flags;
141
142 spin_lock_irqsave(&ifx_dev->power_lock, flags);
143
144 if (ifx_dev->power_status) {
145 ifx_dev->power_status &= ~val;
146 if (!ifx_dev->power_status)
147 pm_runtime_put(&ifx_dev->spi_dev->dev);
148 }
149
150 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
151}
152
153/**
154 * swap_buf
155 * @buf: our buffer
156 * @len : number of bytes (not words) in the buffer
157 * @end: end of buffer
158 *
159 * Swap the contents of a buffer into big endian format
160 */
161static inline void swap_buf(u16 *buf, int len, void *end)
162{
163 int n;
164
165 len = ((len + 1) >> 1);
166 if ((void *)&buf[len] > end) {
167 pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
168 &buf[len], end);
169 return;
170 }
171 for (n = 0; n < len; n++) {
172 *buf = cpu_to_be16(*buf);
173 buf++;
174 }
175}
176
177/**
178 * mrdy_assert - assert MRDY line
179 * @ifx_dev: our SPI device
180 *
181 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
182 * now.
183 *
184 * FIXME: Can SRDY even go high as we are running this code ?
185 */
186static void mrdy_assert(struct ifx_spi_device *ifx_dev)
187{
188 int val = gpio_get_value(ifx_dev->gpio.srdy);
189 if (!val) {
190 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
191 &ifx_dev->flags)) {
192 ifx_dev->spi_timer.expires =
193 jiffies + IFX_SPI_TIMEOUT_SEC*HZ;
194 add_timer(&ifx_dev->spi_timer);
195
196 }
197 }
198 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
199 mrdy_set_high(ifx_dev);
200}
201
202/**
203 * ifx_spi_hangup - hang up an IFX device
204 * @ifx_dev: our SPI device
205 *
206 * Hang up the tty attached to the IFX device if one is currently
207 * open. If not take no action
208 */
209static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
210{
211 struct tty_port *pport = &ifx_dev->tty_port;
212 struct tty_struct *tty = tty_port_tty_get(pport);
213 if (tty) {
214 tty_hangup(tty);
215 tty_kref_put(tty);
216 }
217}
218
219/**
220 * ifx_spi_timeout - SPI timeout
221 * @arg: our SPI device
222 *
223 * The SPI has timed out: hang up the tty. Users will then see a hangup
224 * and error events.
225 */
226static void ifx_spi_timeout(unsigned long arg)
227{
228 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
229
230 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
231 ifx_spi_ttyhangup(ifx_dev);
232 mrdy_set_low(ifx_dev);
233 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
234}
235
236/* char/tty operations */
237
238/**
239 * ifx_spi_tiocmget - get modem lines
240 * @tty: our tty device
241 * @filp: file handle issuing the request
242 *
243 * Map the signal state into Linux modem flags and report the value
244 * in Linux terms
245 */
246static int ifx_spi_tiocmget(struct tty_struct *tty)
247{
248 unsigned int value;
249 struct ifx_spi_device *ifx_dev = tty->driver_data;
250
251 value =
252 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
253 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
254 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
255 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
256 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
257 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
258 return value;
259}
260
261/**
262 * ifx_spi_tiocmset - set modem bits
263 * @tty: the tty structure
264 * @set: bits to set
265 * @clear: bits to clear
266 *
267 * The IFX6x60 only supports DTR and RTS. Set them accordingly
268 * and flag that an update to the modem is needed.
269 *
270 * FIXME: do we need to kick the tranfers when we do this ?
271 */
272static int ifx_spi_tiocmset(struct tty_struct *tty,
273 unsigned int set, unsigned int clear)
274{
275 struct ifx_spi_device *ifx_dev = tty->driver_data;
276
277 if (set & TIOCM_RTS)
278 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
279 if (set & TIOCM_DTR)
280 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
281 if (clear & TIOCM_RTS)
282 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
283 if (clear & TIOCM_DTR)
284 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
285
286 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
287 return 0;
288}
289
290/**
291 * ifx_spi_open - called on tty open
292 * @tty: our tty device
293 * @filp: file handle being associated with the tty
294 *
295 * Open the tty interface. We let the tty_port layer do all the work
296 * for us.
297 *
298 * FIXME: Remove single device assumption and saved_ifx_dev
299 */
300static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
301{
302 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
303}
304
305/**
306 * ifx_spi_close - called when our tty closes
307 * @tty: the tty being closed
308 * @filp: the file handle being closed
309 *
310 * Perform the close of the tty. We use the tty_port layer to do all
311 * our hard work.
312 */
313static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
314{
315 struct ifx_spi_device *ifx_dev = tty->driver_data;
316 tty_port_close(&ifx_dev->tty_port, tty, filp);
317 /* FIXME: should we do an ifx_spi_reset here ? */
318}
319
320/**
321 * ifx_decode_spi_header - decode received header
322 * @buffer: the received data
323 * @length: decoded length
324 * @more: decoded more flag
325 * @received_cts: status of cts we received
326 *
327 * Note how received_cts is handled -- if header is all F it is left
328 * the same as it was, if header is all 0 it is set to 0 otherwise it is
329 * taken from the incoming header.
330 *
331 * FIXME: endianness
332 */
333static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
334 unsigned char *more, unsigned char *received_cts)
335{
336 u16 h1;
337 u16 h2;
338 u16 *in_buffer = (u16 *)buffer;
339
340 h1 = *in_buffer;
341 h2 = *(in_buffer+1);
342
343 if (h1 == 0 && h2 == 0) {
344 *received_cts = 0;
345 return IFX_SPI_HEADER_0;
346 } else if (h1 == 0xffff && h2 == 0xffff) {
347 /* spi_slave_cts remains as it was */
348 return IFX_SPI_HEADER_F;
349 }
350
351 *length = h1 & 0xfff; /* upper bits of byte are flags */
352 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
353 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
354 return 0;
355}
356
357/**
358 * ifx_setup_spi_header - set header fields
359 * @txbuffer: pointer to start of SPI buffer
360 * @tx_count: bytes
361 * @more: indicate if more to follow
362 *
363 * Format up an SPI header for a transfer
364 *
365 * FIXME: endianness?
366 */
367static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
368 unsigned char more)
369{
370 *(u16 *)(txbuffer) = tx_count;
371 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
372 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
373}
374
375/**
376 * ifx_spi_wakeup_serial - SPI space made
377 * @port_data: our SPI device
378 *
379 * We have emptied the FIFO enough that we want to get more data
380 * queued into it. Poke the line discipline via tty_wakeup so that
381 * it will feed us more bits
382 */
383static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
384{
385 struct tty_struct *tty;
386
387 tty = tty_port_tty_get(&ifx_dev->tty_port);
388 if (!tty)
389 return;
390 tty_wakeup(tty);
391 tty_kref_put(tty);
392}
393
394/**
395 * ifx_spi_prepare_tx_buffer - prepare transmit frame
396 * @ifx_dev: our SPI device
397 *
398 * The transmit buffr needs a header and various other bits of
399 * information followed by as much data as we can pull from the FIFO
400 * and transfer. This function formats up a suitable buffer in the
401 * ifx_dev->tx_buffer
402 *
403 * FIXME: performance - should we wake the tty when the queue is half
404 * empty ?
405 */
406static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
407{
408 int temp_count;
409 int queue_length;
410 int tx_count;
411 unsigned char *tx_buffer;
412
413 tx_buffer = ifx_dev->tx_buffer;
414 memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE);
415
416 /* make room for required SPI header */
417 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
418 tx_count = IFX_SPI_HEADER_OVERHEAD;
419
420 /* clear to signal no more data if this turns out to be the
421 * last buffer sent in a sequence */
422 ifx_dev->spi_more = 0;
423
424 /* if modem cts is set, just send empty buffer */
425 if (!ifx_dev->spi_slave_cts) {
426 /* see if there's tx data */
427 queue_length = kfifo_len(&ifx_dev->tx_fifo);
428 if (queue_length != 0) {
429 /* data to mux -- see if there's room for it */
430 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
431 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
432 tx_buffer, temp_count,
433 &ifx_dev->fifo_lock);
434
435 /* update buffer pointer and data count in message */
436 tx_buffer += temp_count;
437 tx_count += temp_count;
438 if (temp_count == queue_length)
439 /* poke port to get more data */
440 ifx_spi_wakeup_serial(ifx_dev);
441 else /* more data in port, use next SPI message */
442 ifx_dev->spi_more = 1;
443 }
444 }
445 /* have data and info for header -- set up SPI header in buffer */
446 /* spi header needs payload size, not entire buffer size */
447 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
448 tx_count-IFX_SPI_HEADER_OVERHEAD,
449 ifx_dev->spi_more);
450 /* swap actual data in the buffer */
451 swap_buf((u16 *)(ifx_dev->tx_buffer), tx_count,
452 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
453 return tx_count;
454}
455
456/**
457 * ifx_spi_write - line discipline write
458 * @tty: our tty device
459 * @buf: pointer to buffer to write (kernel space)
460 * @count: size of buffer
461 *
462 * Write the characters we have been given into the FIFO. If the device
463 * is not active then activate it, when the SRDY line is asserted back
464 * this will commence I/O
465 */
466static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
467 int count)
468{
469 struct ifx_spi_device *ifx_dev = tty->driver_data;
470 unsigned char *tmp_buf = (unsigned char *)buf;
471 int tx_count = kfifo_in_locked(&ifx_dev->tx_fifo, tmp_buf, count,
472 &ifx_dev->fifo_lock);
473 mrdy_assert(ifx_dev);
474 return tx_count;
475}
476
477/**
478 * ifx_spi_chars_in_buffer - line discipline helper
479 * @tty: our tty device
480 *
481 * Report how much data we can accept before we drop bytes. As we use
482 * a simple FIFO this is nice and easy.
483 */
484static int ifx_spi_write_room(struct tty_struct *tty)
485{
486 struct ifx_spi_device *ifx_dev = tty->driver_data;
487 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
488}
489
490/**
491 * ifx_spi_chars_in_buffer - line discipline helper
492 * @tty: our tty device
493 *
494 * Report how many characters we have buffered. In our case this is the
495 * number of bytes sitting in our transmit FIFO.
496 */
497static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
498{
499 struct ifx_spi_device *ifx_dev = tty->driver_data;
500 return kfifo_len(&ifx_dev->tx_fifo);
501}
502
503/**
504 * ifx_port_hangup
505 * @port: our tty port
506 *
507 * tty port hang up. Called when tty_hangup processing is invoked either
508 * by loss of carrier, or by software (eg vhangup). Serialized against
509 * activate/shutdown by the tty layer.
510 */
511static void ifx_spi_hangup(struct tty_struct *tty)
512{
513 struct ifx_spi_device *ifx_dev = tty->driver_data;
514 tty_port_hangup(&ifx_dev->tty_port);
515}
516
517/**
518 * ifx_port_activate
519 * @port: our tty port
520 *
521 * tty port activate method - called for first open. Serialized
522 * with hangup and shutdown by the tty layer.
523 */
524static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
525{
526 struct ifx_spi_device *ifx_dev =
527 container_of(port, struct ifx_spi_device, tty_port);
528
529 /* clear any old data; can't do this in 'close' */
530 kfifo_reset(&ifx_dev->tx_fifo);
531
532 /* put port data into this tty */
533 tty->driver_data = ifx_dev;
534
535 /* allows flip string push from int context */
536 tty->low_latency = 1;
537
538 return 0;
539}
540
541/**
542 * ifx_port_shutdown
543 * @port: our tty port
544 *
545 * tty port shutdown method - called for last port close. Serialized
546 * with hangup and activate by the tty layer.
547 */
548static void ifx_port_shutdown(struct tty_port *port)
549{
550 struct ifx_spi_device *ifx_dev =
551 container_of(port, struct ifx_spi_device, tty_port);
552
553 mrdy_set_low(ifx_dev);
554 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
555 tasklet_kill(&ifx_dev->io_work_tasklet);
556}
557
558static const struct tty_port_operations ifx_tty_port_ops = {
559 .activate = ifx_port_activate,
560 .shutdown = ifx_port_shutdown,
561};
562
563static const struct tty_operations ifx_spi_serial_ops = {
564 .open = ifx_spi_open,
565 .close = ifx_spi_close,
566 .write = ifx_spi_write,
567 .hangup = ifx_spi_hangup,
568 .write_room = ifx_spi_write_room,
569 .chars_in_buffer = ifx_spi_chars_in_buffer,
570 .tiocmget = ifx_spi_tiocmget,
571 .tiocmset = ifx_spi_tiocmset,
572};
573
574/**
575 * ifx_spi_insert_fip_string - queue received data
576 * @ifx_ser: our SPI device
577 * @chars: buffer we have received
578 * @size: number of chars reeived
579 *
580 * Queue bytes to the tty assuming the tty side is currently open. If
581 * not the discard the data.
582 */
583static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
584 unsigned char *chars, size_t size)
585{
586 struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
587 if (!tty)
588 return;
589 tty_insert_flip_string(tty, chars, size);
590 tty_flip_buffer_push(tty);
591 tty_kref_put(tty);
592}
593
594/**
595 * ifx_spi_complete - SPI transfer completed
596 * @ctx: our SPI device
597 *
598 * An SPI transfer has completed. Process any received data and kick off
599 * any further transmits we can commence.
600 */
601static void ifx_spi_complete(void *ctx)
602{
603 struct ifx_spi_device *ifx_dev = ctx;
604 struct tty_struct *tty;
605 struct tty_ldisc *ldisc = NULL;
606 int length;
607 int actual_length;
608 unsigned char more;
609 unsigned char cts;
610 int local_write_pending = 0;
611 int queue_length;
612 int srdy;
613 int decode_result;
614
615 mrdy_set_low(ifx_dev);
616
617 if (!ifx_dev->spi_msg.status) {
618 /* check header validity, get comm flags */
619 swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
620 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
621 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
622 &length, &more, &cts);
623 if (decode_result == IFX_SPI_HEADER_0) {
624 dev_dbg(&ifx_dev->spi_dev->dev,
625 "ignore input: invalid header 0");
626 ifx_dev->spi_slave_cts = 0;
627 goto complete_exit;
628 } else if (decode_result == IFX_SPI_HEADER_F) {
629 dev_dbg(&ifx_dev->spi_dev->dev,
630 "ignore input: invalid header F");
631 goto complete_exit;
632 }
633
634 ifx_dev->spi_slave_cts = cts;
635
636 actual_length = min((unsigned int)length,
637 ifx_dev->spi_msg.actual_length);
638 swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
639 actual_length,
640 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
641 ifx_spi_insert_flip_string(
642 ifx_dev,
643 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
644 (size_t)actual_length);
645 } else {
646 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
647 ifx_dev->spi_msg.status);
648 }
649
650complete_exit:
651 if (ifx_dev->write_pending) {
652 ifx_dev->write_pending = 0;
653 local_write_pending = 1;
654 }
655
656 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
657
658 queue_length = kfifo_len(&ifx_dev->tx_fifo);
659 srdy = gpio_get_value(ifx_dev->gpio.srdy);
660 if (!srdy)
661 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
662
663 /* schedule output if there is more to do */
664 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
665 tasklet_schedule(&ifx_dev->io_work_tasklet);
666 else {
667 if (more || ifx_dev->spi_more || queue_length > 0 ||
668 local_write_pending) {
669 if (ifx_dev->spi_slave_cts) {
670 if (more)
671 mrdy_assert(ifx_dev);
672 } else
673 mrdy_assert(ifx_dev);
674 } else {
675 /*
676 * poke line discipline driver if any for more data
677 * may or may not get more data to write
678 * for now, say not busy
679 */
680 ifx_spi_power_state_clear(ifx_dev,
681 IFX_SPI_POWER_DATA_PENDING);
682 tty = tty_port_tty_get(&ifx_dev->tty_port);
683 if (tty) {
684 ldisc = tty_ldisc_ref(tty);
685 if (ldisc) {
686 ldisc->ops->write_wakeup(tty);
687 tty_ldisc_deref(ldisc);
688 }
689 tty_kref_put(tty);
690 }
691 }
692 }
693}
694
695/**
696 * ifx_spio_io - I/O tasklet
697 * @data: our SPI device
698 *
699 * Queue data for transmission if possible and then kick off the
700 * transfer.
701 */
702static void ifx_spi_io(unsigned long data)
703{
704 int retval;
705 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
706
707 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) {
708 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
709 ifx_dev->gpio.unack_srdy_int_nb--;
710
711 ifx_spi_prepare_tx_buffer(ifx_dev);
712
713 spi_message_init(&ifx_dev->spi_msg);
714 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
715
716 ifx_dev->spi_msg.context = ifx_dev;
717 ifx_dev->spi_msg.complete = ifx_spi_complete;
718
719 /* set up our spi transfer */
720 /* note len is BYTES, not transfers */
721 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
722 ifx_dev->spi_xfer.cs_change = 0;
723 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
724 /* ifx_dev->spi_xfer.speed_hz = 390625; */
725 ifx_dev->spi_xfer.bits_per_word = spi_bpw;
726
727 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
728 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
729
730 /*
731 * setup dma pointers
732 */
733 if (ifx_dev->use_dma) {
734 ifx_dev->spi_msg.is_dma_mapped = 1;
735 ifx_dev->tx_dma = ifx_dev->tx_bus;
736 ifx_dev->rx_dma = ifx_dev->rx_bus;
737 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
738 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
739 } else {
740 ifx_dev->spi_msg.is_dma_mapped = 0;
741 ifx_dev->tx_dma = (dma_addr_t)0;
742 ifx_dev->rx_dma = (dma_addr_t)0;
743 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
744 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
745 }
746
747 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
748
749 /* Assert MRDY. This may have already been done by the write
750 * routine.
751 */
752 mrdy_assert(ifx_dev);
753
754 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
755 if (retval) {
756 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
757 &ifx_dev->flags);
758 tasklet_schedule(&ifx_dev->io_work_tasklet);
759 return;
760 }
761 } else
762 ifx_dev->write_pending = 1;
763}
764
765/**
766 * ifx_spi_free_port - free up the tty side
767 * @ifx_dev: IFX device going away
768 *
769 * Unregister and free up a port when the device goes away
770 */
771static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
772{
773 if (ifx_dev->tty_dev)
774 tty_unregister_device(tty_drv, ifx_dev->minor);
775 kfifo_free(&ifx_dev->tx_fifo);
776}
777
778/**
779 * ifx_spi_create_port - create a new port
780 * @ifx_dev: our spi device
781 *
782 * Allocate and initialise the tty port that goes with this interface
783 * and add it to the tty layer so that it can be opened.
784 */
785static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
786{
787 int ret = 0;
788 struct tty_port *pport = &ifx_dev->tty_port;
789
790 spin_lock_init(&ifx_dev->fifo_lock);
791 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
792 &ifx_spi_key, 0);
793
794 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
795 ret = -ENOMEM;
796 goto error_ret;
797 }
798
799 tty_port_init(pport);
800 pport->ops = &ifx_tty_port_ops;
801 ifx_dev->minor = IFX_SPI_TTY_ID;
802 ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor,
803 &ifx_dev->spi_dev->dev);
804 if (IS_ERR(ifx_dev->tty_dev)) {
805 dev_dbg(&ifx_dev->spi_dev->dev,
806 "%s: registering tty device failed", __func__);
807 ret = PTR_ERR(ifx_dev->tty_dev);
808 goto error_ret;
809 }
810 return 0;
811
812error_ret:
813 ifx_spi_free_port(ifx_dev);
814 return ret;
815}
816
817/**
818 * ifx_spi_handle_srdy - handle SRDY
819 * @ifx_dev: device asserting SRDY
820 *
821 * Check our device state and see what we need to kick off when SRDY
822 * is asserted. This usually means killing the timer and firing off the
823 * I/O processing.
824 */
825static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
826{
827 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
828 del_timer_sync(&ifx_dev->spi_timer);
829 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
830 }
831
832 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
833
834 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
835 tasklet_schedule(&ifx_dev->io_work_tasklet);
836 else
837 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
838}
839
840/**
841 * ifx_spi_srdy_interrupt - SRDY asserted
842 * @irq: our IRQ number
843 * @dev: our ifx device
844 *
845 * The modem asserted SRDY. Handle the srdy event
846 */
847static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
848{
849 struct ifx_spi_device *ifx_dev = dev;
850 ifx_dev->gpio.unack_srdy_int_nb++;
851 ifx_spi_handle_srdy(ifx_dev);
852 return IRQ_HANDLED;
853}
854
855/**
856 * ifx_spi_reset_interrupt - Modem has changed reset state
857 * @irq: interrupt number
858 * @dev: our device pointer
859 *
860 * The modem has either entered or left reset state. Check the GPIO
861 * line to see which.
862 *
863 * FIXME: review locking on MR_INPROGRESS versus
864 * parallel unsolicited reset/solicited reset
865 */
866static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
867{
868 struct ifx_spi_device *ifx_dev = dev;
869 int val = gpio_get_value(ifx_dev->gpio.reset_out);
870 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
871
872 if (val == 0) {
873 /* entered reset */
874 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
875 if (!solreset) {
876 /* unsolicited reset */
877 ifx_spi_ttyhangup(ifx_dev);
878 }
879 } else {
880 /* exited reset */
881 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
882 if (solreset) {
883 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
884 wake_up(&ifx_dev->mdm_reset_wait);
885 }
886 }
887 return IRQ_HANDLED;
888}
889
890/**
891 * ifx_spi_free_device - free device
892 * @ifx_dev: device to free
893 *
894 * Free the IFX device
895 */
896static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
897{
898 ifx_spi_free_port(ifx_dev);
899 dma_free_coherent(&ifx_dev->spi_dev->dev,
900 IFX_SPI_TRANSFER_SIZE,
901 ifx_dev->tx_buffer,
902 ifx_dev->tx_bus);
903 dma_free_coherent(&ifx_dev->spi_dev->dev,
904 IFX_SPI_TRANSFER_SIZE,
905 ifx_dev->rx_buffer,
906 ifx_dev->rx_bus);
907}
908
909/**
910 * ifx_spi_reset - reset modem
911 * @ifx_dev: modem to reset
912 *
913 * Perform a reset on the modem
914 */
915static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
916{
917 int ret;
918 /*
919 * set up modem power, reset
920 *
921 * delays are required on some platforms for the modem
922 * to reset properly
923 */
924 set_bit(MR_START, &ifx_dev->mdm_reset_state);
925 gpio_set_value(ifx_dev->gpio.po, 0);
926 gpio_set_value(ifx_dev->gpio.reset, 0);
927 msleep(25);
928 gpio_set_value(ifx_dev->gpio.reset, 1);
929 msleep(1);
930 gpio_set_value(ifx_dev->gpio.po, 1);
931 msleep(1);
932 gpio_set_value(ifx_dev->gpio.po, 0);
933 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
934 test_bit(MR_COMPLETE,
935 &ifx_dev->mdm_reset_state),
936 IFX_RESET_TIMEOUT);
937 if (!ret)
938 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
939 ifx_dev->mdm_reset_state);
940
941 ifx_dev->mdm_reset_state = 0;
942 return ret;
943}
944
945/**
946 * ifx_spi_spi_probe - probe callback
947 * @spi: our possible matching SPI device
948 *
949 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
950 * GPIO setup.
951 *
952 * FIXME:
953 * - Support for multiple devices
954 * - Split out MID specific GPIO handling eventually
955 */
956
957static int ifx_spi_spi_probe(struct spi_device *spi)
958{
959 int ret;
960 int srdy;
961 struct ifx_modem_platform_data *pl_data;
962 struct ifx_spi_device *ifx_dev;
963
964 if (saved_ifx_dev) {
965 dev_dbg(&spi->dev, "ignoring subsequent detection");
966 return -ENODEV;
967 }
968
969 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
970 if (!pl_data) {
971 dev_err(&spi->dev, "missing platform data!");
972 return -ENODEV;
973 }
974
975 /* initialize structure to hold our device variables */
976 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
977 if (!ifx_dev) {
978 dev_err(&spi->dev, "spi device allocation failed");
979 return -ENOMEM;
980 }
981 saved_ifx_dev = ifx_dev;
982 ifx_dev->spi_dev = spi;
983 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
984 spin_lock_init(&ifx_dev->write_lock);
985 spin_lock_init(&ifx_dev->power_lock);
986 ifx_dev->power_status = 0;
987 init_timer(&ifx_dev->spi_timer);
988 ifx_dev->spi_timer.function = ifx_spi_timeout;
989 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
990 ifx_dev->modem = pl_data->modem_type;
991 ifx_dev->use_dma = pl_data->use_dma;
992 ifx_dev->max_hz = pl_data->max_hz;
993 /* initialize spi mode, etc */
994 spi->max_speed_hz = ifx_dev->max_hz;
995 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
996 spi->bits_per_word = spi_bpw;
997 ret = spi_setup(spi);
998 if (ret) {
999 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1000 return -ENODEV;
1001 }
1002
1003 /* ensure SPI protocol flags are initialized to enable transfer */
1004 ifx_dev->spi_more = 0;
1005 ifx_dev->spi_slave_cts = 0;
1006
1007 /*initialize transfer and dma buffers */
1008 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1009 IFX_SPI_TRANSFER_SIZE,
1010 &ifx_dev->tx_bus,
1011 GFP_KERNEL);
1012 if (!ifx_dev->tx_buffer) {
1013 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1014 ret = -ENOMEM;
1015 goto error_ret;
1016 }
1017 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1018 IFX_SPI_TRANSFER_SIZE,
1019 &ifx_dev->rx_bus,
1020 GFP_KERNEL);
1021 if (!ifx_dev->rx_buffer) {
1022 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1023 ret = -ENOMEM;
1024 goto error_ret;
1025 }
1026
1027 /* initialize waitq for modem reset */
1028 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1029
1030 spi_set_drvdata(spi, ifx_dev);
1031 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1032 (unsigned long)ifx_dev);
1033
1034 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1035
1036 /* create our tty port */
1037 ret = ifx_spi_create_port(ifx_dev);
1038 if (ret != 0) {
1039 dev_err(&spi->dev, "create default tty port failed");
1040 goto error_ret;
1041 }
1042
1043 ifx_dev->gpio.reset = pl_data->rst_pmu;
1044 ifx_dev->gpio.po = pl_data->pwr_on;
1045 ifx_dev->gpio.mrdy = pl_data->mrdy;
1046 ifx_dev->gpio.srdy = pl_data->srdy;
1047 ifx_dev->gpio.reset_out = pl_data->rst_out;
1048
1049 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1050 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1051 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1052
1053 /* Configure gpios */
1054 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1055 if (ret < 0) {
1056 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1057 ifx_dev->gpio.reset);
1058 goto error_ret;
1059 }
1060 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1061 ret += gpio_export(ifx_dev->gpio.reset, 1);
1062 if (ret) {
1063 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1064 ifx_dev->gpio.reset);
1065 ret = -EBUSY;
1066 goto error_ret2;
1067 }
1068
1069 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1070 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1071 ret += gpio_export(ifx_dev->gpio.po, 1);
1072 if (ret) {
1073 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1074 ifx_dev->gpio.po);
1075 ret = -EBUSY;
1076 goto error_ret3;
1077 }
1078
1079 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1080 if (ret < 0) {
1081 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1082 ifx_dev->gpio.mrdy);
1083 goto error_ret3;
1084 }
1085 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1086 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1087 if (ret) {
1088 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1089 ifx_dev->gpio.mrdy);
1090 ret = -EBUSY;
1091 goto error_ret4;
1092 }
1093
1094 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1095 if (ret < 0) {
1096 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1097 ifx_dev->gpio.srdy);
1098 ret = -EBUSY;
1099 goto error_ret4;
1100 }
1101 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1102 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1103 if (ret) {
1104 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1105 ifx_dev->gpio.srdy);
1106 ret = -EBUSY;
1107 goto error_ret5;
1108 }
1109
1110 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1111 if (ret < 0) {
1112 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1113 ifx_dev->gpio.reset_out);
1114 goto error_ret5;
1115 }
1116 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1117 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1118 if (ret) {
1119 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1120 ifx_dev->gpio.reset_out);
1121 ret = -EBUSY;
1122 goto error_ret6;
1123 }
1124
1125 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1126 ifx_spi_reset_interrupt,
1127 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1128 (void *)ifx_dev);
1129 if (ret) {
1130 dev_err(&spi->dev, "Unable to get irq %x\n",
1131 gpio_to_irq(ifx_dev->gpio.reset_out));
1132 goto error_ret6;
1133 }
1134
1135 ret = ifx_spi_reset(ifx_dev);
1136
1137 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1138 ifx_spi_srdy_interrupt,
1139 IRQF_TRIGGER_RISING, DRVNAME,
1140 (void *)ifx_dev);
1141 if (ret) {
1142 dev_err(&spi->dev, "Unable to get irq %x",
1143 gpio_to_irq(ifx_dev->gpio.srdy));
1144 goto error_ret7;
1145 }
1146
1147 /* set pm runtime power state and register with power system */
1148 pm_runtime_set_active(&spi->dev);
1149 pm_runtime_enable(&spi->dev);
1150
1151 /* handle case that modem is already signaling SRDY */
1152 /* no outgoing tty open at this point, this just satisfies the
1153 * modem's read and should reset communication properly
1154 */
1155 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1156
1157 if (srdy) {
1158 mrdy_assert(ifx_dev);
1159 ifx_spi_handle_srdy(ifx_dev);
1160 } else
1161 mrdy_set_low(ifx_dev);
1162 return 0;
1163
1164error_ret7:
1165 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1166error_ret6:
1167 gpio_free(ifx_dev->gpio.srdy);
1168error_ret5:
1169 gpio_free(ifx_dev->gpio.mrdy);
1170error_ret4:
1171 gpio_free(ifx_dev->gpio.reset);
1172error_ret3:
1173 gpio_free(ifx_dev->gpio.po);
1174error_ret2:
1175 gpio_free(ifx_dev->gpio.reset_out);
1176error_ret:
1177 ifx_spi_free_device(ifx_dev);
1178 saved_ifx_dev = NULL;
1179 return ret;
1180}
1181
1182/**
1183 * ifx_spi_spi_remove - SPI device was removed
1184 * @spi: SPI device
1185 *
1186 * FIXME: We should be shutting the device down here not in
1187 * the module unload path.
1188 */
1189
1190static int ifx_spi_spi_remove(struct spi_device *spi)
1191{
1192 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1193 /* stop activity */
1194 tasklet_kill(&ifx_dev->io_work_tasklet);
1195 /* free irq */
1196 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1197 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1198
1199 gpio_free(ifx_dev->gpio.srdy);
1200 gpio_free(ifx_dev->gpio.mrdy);
1201 gpio_free(ifx_dev->gpio.reset);
1202 gpio_free(ifx_dev->gpio.po);
1203 gpio_free(ifx_dev->gpio.reset_out);
1204
1205 /* free allocations */
1206 ifx_spi_free_device(ifx_dev);
1207
1208 saved_ifx_dev = NULL;
1209 return 0;
1210}
1211
1212/**
1213 * ifx_spi_spi_shutdown - called on SPI shutdown
1214 * @spi: SPI device
1215 *
1216 * No action needs to be taken here
1217 */
1218
1219static void ifx_spi_spi_shutdown(struct spi_device *spi)
1220{
1221}
1222
1223/*
1224 * various suspends and resumes have nothing to do
1225 * no hardware to save state for
1226 */
1227
1228/**
1229 * ifx_spi_spi_suspend - suspend SPI on system suspend
1230 * @dev: device being suspended
1231 *
1232 * Suspend the SPI side. No action needed on Intel MID platforms, may
1233 * need extending for other systems.
1234 */
1235static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1236{
1237 return 0;
1238}
1239
1240/**
1241 * ifx_spi_spi_resume - resume SPI side on system resume
1242 * @dev: device being suspended
1243 *
1244 * Suspend the SPI side. No action needed on Intel MID platforms, may
1245 * need extending for other systems.
1246 */
1247static int ifx_spi_spi_resume(struct spi_device *spi)
1248{
1249 return 0;
1250}
1251
1252/**
1253 * ifx_spi_pm_suspend - suspend modem on system suspend
1254 * @dev: device being suspended
1255 *
1256 * Suspend the modem. No action needed on Intel MID platforms, may
1257 * need extending for other systems.
1258 */
1259static int ifx_spi_pm_suspend(struct device *dev)
1260{
1261 return 0;
1262}
1263
1264/**
1265 * ifx_spi_pm_resume - resume modem on system resume
1266 * @dev: device being suspended
1267 *
1268 * Allow the modem to resume. No action needed.
1269 *
1270 * FIXME: do we need to reset anything here ?
1271 */
1272static int ifx_spi_pm_resume(struct device *dev)
1273{
1274 return 0;
1275}
1276
1277/**
1278 * ifx_spi_pm_runtime_resume - suspend modem
1279 * @dev: device being suspended
1280 *
1281 * Allow the modem to resume. No action needed.
1282 */
1283static int ifx_spi_pm_runtime_resume(struct device *dev)
1284{
1285 return 0;
1286}
1287
1288/**
1289 * ifx_spi_pm_runtime_suspend - suspend modem
1290 * @dev: device being suspended
1291 *
1292 * Allow the modem to suspend and thus suspend to continue up the
1293 * device tree.
1294 */
1295static int ifx_spi_pm_runtime_suspend(struct device *dev)
1296{
1297 return 0;
1298}
1299
1300/**
1301 * ifx_spi_pm_runtime_idle - check if modem idle
1302 * @dev: our device
1303 *
1304 * Check conditions and queue runtime suspend if idle.
1305 */
1306static int ifx_spi_pm_runtime_idle(struct device *dev)
1307{
1308 struct spi_device *spi = to_spi_device(dev);
1309 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1310
1311 if (!ifx_dev->power_status)
1312 pm_runtime_suspend(dev);
1313
1314 return 0;
1315}
1316
1317static const struct dev_pm_ops ifx_spi_pm = {
1318 .resume = ifx_spi_pm_resume,
1319 .suspend = ifx_spi_pm_suspend,
1320 .runtime_resume = ifx_spi_pm_runtime_resume,
1321 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1322 .runtime_idle = ifx_spi_pm_runtime_idle
1323};
1324
1325static const struct spi_device_id ifx_id_table[] = {
1326 {"ifx6160", 0},
1327 {"ifx6260", 0},
1328 { }
1329};
1330MODULE_DEVICE_TABLE(spi, ifx_id_table);
1331
1332/* spi operations */
1333static const struct spi_driver ifx_spi_driver = {
1334 .driver = {
1335 .name = DRVNAME,
1336 .bus = &spi_bus_type,
1337 .pm = &ifx_spi_pm,
1338 .owner = THIS_MODULE},
1339 .probe = ifx_spi_spi_probe,
1340 .shutdown = ifx_spi_spi_shutdown,
1341 .remove = __devexit_p(ifx_spi_spi_remove),
1342 .suspend = ifx_spi_spi_suspend,
1343 .resume = ifx_spi_spi_resume,
1344 .id_table = ifx_id_table
1345};
1346
1347/**
1348 * ifx_spi_exit - module exit
1349 *
1350 * Unload the module.
1351 */
1352
1353static void __exit ifx_spi_exit(void)
1354{
1355 /* unregister */
1356 tty_unregister_driver(tty_drv);
1357 spi_unregister_driver((void *)&ifx_spi_driver);
1358}
1359
1360/**
1361 * ifx_spi_init - module entry point
1362 *
1363 * Initialise the SPI and tty interfaces for the IFX SPI driver
1364 * We need to initialize upper-edge spi driver after the tty
1365 * driver because otherwise the spi probe will race
1366 */
1367
1368static int __init ifx_spi_init(void)
1369{
1370 int result;
1371
1372 tty_drv = alloc_tty_driver(1);
1373 if (!tty_drv) {
1374 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1375 return -ENOMEM;
1376 }
1377
1378 tty_drv->magic = TTY_DRIVER_MAGIC;
1379 tty_drv->owner = THIS_MODULE;
1380 tty_drv->driver_name = DRVNAME;
1381 tty_drv->name = TTYNAME;
1382 tty_drv->minor_start = IFX_SPI_TTY_ID;
1383 tty_drv->num = 1;
1384 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1385 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1386 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1387 tty_drv->init_termios = tty_std_termios;
1388
1389 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1390
1391 result = tty_register_driver(tty_drv);
1392 if (result) {
1393 pr_err("%s: tty_register_driver failed(%d)",
1394 DRVNAME, result);
1395 put_tty_driver(tty_drv);
1396 return result;
1397 }
1398
1399 result = spi_register_driver((void *)&ifx_spi_driver);
1400 if (result) {
1401 pr_err("%s: spi_register_driver failed(%d)",
1402 DRVNAME, result);
1403 tty_unregister_driver(tty_drv);
1404 }
1405 return result;
1406}
1407
1408module_init(ifx_spi_init);
1409module_exit(ifx_spi_exit);
1410
1411MODULE_AUTHOR("Intel");
1412MODULE_DESCRIPTION("IFX6x60 spi driver");
1413MODULE_LICENSE("GPL");
1414MODULE_INFO(Version, "0.1-IFX6x60");
diff --git a/drivers/tty/serial/ifx6x60.h b/drivers/tty/serial/ifx6x60.h
new file mode 100644
index 000000000000..e8464baf9e75
--- /dev/null
+++ b/drivers/tty/serial/ifx6x60.h
@@ -0,0 +1,129 @@
1/****************************************************************************
2 *
3 * Driver for the IFX spi modem.
4 *
5 * Copyright (C) 2009, 2010 Intel Corp
6 * Jim Stanley <jim.stanley@intel.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
22 *
23 *
24 *
25 *****************************************************************************/
26#ifndef _IFX6X60_H
27#define _IFX6X60_H
28
29#define DRVNAME "ifx6x60"
30#define TTYNAME "ttyIFX"
31
32#define IFX_SPI_MAX_MINORS 1
33#define IFX_SPI_TRANSFER_SIZE 2048
34#define IFX_SPI_FIFO_SIZE 4096
35
36#define IFX_SPI_HEADER_OVERHEAD 4
37#define IFX_RESET_TIMEOUT msecs_to_jiffies(50)
38
39/* device flags bitfield definitions */
40#define IFX_SPI_STATE_PRESENT 0
41#define IFX_SPI_STATE_IO_IN_PROGRESS 1
42#define IFX_SPI_STATE_IO_READY 2
43#define IFX_SPI_STATE_TIMER_PENDING 3
44
45/* flow control bitfields */
46#define IFX_SPI_DCD 0
47#define IFX_SPI_CTS 1
48#define IFX_SPI_DSR 2
49#define IFX_SPI_RI 3
50#define IFX_SPI_DTR 4
51#define IFX_SPI_RTS 5
52#define IFX_SPI_TX_FC 6
53#define IFX_SPI_RX_FC 7
54#define IFX_SPI_UPDATE 8
55
56#define IFX_SPI_PAYLOAD_SIZE (IFX_SPI_TRANSFER_SIZE - \
57 IFX_SPI_HEADER_OVERHEAD)
58
59#define IFX_SPI_IRQ_TYPE DETECT_EDGE_RISING
60#define IFX_SPI_GPIO_TARGET 0
61#define IFX_SPI_GPIO0 0x105
62
63#define IFX_SPI_STATUS_TIMEOUT (2000*HZ)
64
65/* values for bits in power status byte */
66#define IFX_SPI_POWER_DATA_PENDING 1
67#define IFX_SPI_POWER_SRDY 2
68
69struct ifx_spi_device {
70 /* Our SPI device */
71 struct spi_device *spi_dev;
72
73 /* Port specific data */
74 struct kfifo tx_fifo;
75 spinlock_t fifo_lock;
76 unsigned long signal_state;
77
78 /* TTY Layer logic */
79 struct tty_port tty_port;
80 struct device *tty_dev;
81 int minor;
82
83 /* Low level I/O work */
84 struct tasklet_struct io_work_tasklet;
85 unsigned long flags;
86 dma_addr_t rx_dma;
87 dma_addr_t tx_dma;
88
89 int modem; /* Modem type */
90 int use_dma; /* provide dma-able addrs in SPI msg */
91 long max_hz; /* max SPI frequency */
92
93 spinlock_t write_lock;
94 int write_pending;
95 spinlock_t power_lock;
96 unsigned char power_status;
97
98 unsigned char *rx_buffer;
99 unsigned char *tx_buffer;
100 dma_addr_t rx_bus;
101 dma_addr_t tx_bus;
102 unsigned char spi_more;
103 unsigned char spi_slave_cts;
104
105 struct timer_list spi_timer;
106
107 struct spi_message spi_msg;
108 struct spi_transfer spi_xfer;
109
110 struct {
111 /* gpio lines */
112 unsigned short srdy; /* slave-ready gpio */
113 unsigned short mrdy; /* master-ready gpio */
114 unsigned short reset; /* modem-reset gpio */
115 unsigned short po; /* modem-on gpio */
116 unsigned short reset_out; /* modem-in-reset gpio */
117 /* state/stats */
118 int unack_srdy_int_nb;
119 } gpio;
120
121 /* modem reset */
122 unsigned long mdm_reset_state;
123#define MR_START 0
124#define MR_INPROGRESS 1
125#define MR_COMPLETE 2
126 wait_queue_head_t mdm_reset_wait;
127};
128
129#endif /* _IFX6X60_H */
diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
new file mode 100644
index 000000000000..a54473123e0a
--- /dev/null
+++ b/drivers/tty/serial/imx.c
@@ -0,0 +1,1379 @@
1/*
2 * Driver for Motorola IMX serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Author: Sascha Hauer <sascha@saschahauer.de>
7 * Copyright (C) 2004 Pengutronix
8 *
9 * Copyright (C) 2009 emlix GmbH
10 * Author: Fabian Godehardt (added IrDA support for iMX)
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * [29-Mar-2005] Mike Lee
27 * Added hardware handshake
28 */
29
30#if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/module.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/platform_device.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/serial.h>
44#include <linux/clk.h>
45#include <linux/delay.h>
46#include <linux/rational.h>
47#include <linux/slab.h>
48
49#include <asm/io.h>
50#include <asm/irq.h>
51#include <mach/hardware.h>
52#include <mach/imx-uart.h>
53
54/* Register definitions */
55#define URXD0 0x0 /* Receiver Register */
56#define URTX0 0x40 /* Transmitter Register */
57#define UCR1 0x80 /* Control Register 1 */
58#define UCR2 0x84 /* Control Register 2 */
59#define UCR3 0x88 /* Control Register 3 */
60#define UCR4 0x8c /* Control Register 4 */
61#define UFCR 0x90 /* FIFO Control Register */
62#define USR1 0x94 /* Status Register 1 */
63#define USR2 0x98 /* Status Register 2 */
64#define UESC 0x9c /* Escape Character Register */
65#define UTIM 0xa0 /* Escape Timer Register */
66#define UBIR 0xa4 /* BRM Incremental Register */
67#define UBMR 0xa8 /* BRM Modulator Register */
68#define UBRC 0xac /* Baud Rate Count Register */
69#define MX2_ONEMS 0xb0 /* One Millisecond register */
70#define UTS (cpu_is_mx1() ? 0xd0 : 0xb4) /* UART Test Register */
71
72/* UART Control Register Bit Fields.*/
73#define URXD_CHARRDY (1<<15)
74#define URXD_ERR (1<<14)
75#define URXD_OVRRUN (1<<13)
76#define URXD_FRMERR (1<<12)
77#define URXD_BRK (1<<11)
78#define URXD_PRERR (1<<10)
79#define UCR1_ADEN (1<<15) /* Auto detect interrupt */
80#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
81#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
82#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
83#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
84#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
85#define UCR1_IREN (1<<7) /* Infrared interface enable */
86#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
87#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
88#define UCR1_SNDBRK (1<<4) /* Send break */
89#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
90#define MX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, mx1 only */
91#define UCR1_DOZE (1<<1) /* Doze */
92#define UCR1_UARTEN (1<<0) /* UART enabled */
93#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
94#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
95#define UCR2_CTSC (1<<13) /* CTS pin control */
96#define UCR2_CTS (1<<12) /* Clear to send */
97#define UCR2_ESCEN (1<<11) /* Escape enable */
98#define UCR2_PREN (1<<8) /* Parity enable */
99#define UCR2_PROE (1<<7) /* Parity odd/even */
100#define UCR2_STPB (1<<6) /* Stop */
101#define UCR2_WS (1<<5) /* Word size */
102#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
103#define UCR2_TXEN (1<<2) /* Transmitter enabled */
104#define UCR2_RXEN (1<<1) /* Receiver enabled */
105#define UCR2_SRST (1<<0) /* SW reset */
106#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
107#define UCR3_PARERREN (1<<12) /* Parity enable */
108#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
109#define UCR3_DSR (1<<10) /* Data set ready */
110#define UCR3_DCD (1<<9) /* Data carrier detect */
111#define UCR3_RI (1<<8) /* Ring indicator */
112#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
113#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
114#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
115#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
116#define MX1_UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
117#define MX1_UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
118#define MX2_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
119#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
120#define UCR3_BPEN (1<<0) /* Preset registers enable */
121#define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
122#define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
123#define UCR4_INVR (1<<9) /* Inverted infrared reception */
124#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
125#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
126#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
127#define UCR4_IRSC (1<<5) /* IR special case */
128#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
129#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
130#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
131#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
132#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
133#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
134#define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
135#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
136#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
137#define USR1_RTSS (1<<14) /* RTS pin status */
138#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
139#define USR1_RTSD (1<<12) /* RTS delta */
140#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
141#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
142#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
143#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
144#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
145#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
146#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
147#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
148#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
149#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
150#define USR2_IDLE (1<<12) /* Idle condition */
151#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
152#define USR2_WAKE (1<<7) /* Wake */
153#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
154#define USR2_TXDC (1<<3) /* Transmitter complete */
155#define USR2_BRCD (1<<2) /* Break condition */
156#define USR2_ORE (1<<1) /* Overrun error */
157#define USR2_RDR (1<<0) /* Recv data ready */
158#define UTS_FRCPERR (1<<13) /* Force parity error */
159#define UTS_LOOP (1<<12) /* Loop tx and rx */
160#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
161#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
162#define UTS_TXFULL (1<<4) /* TxFIFO full */
163#define UTS_RXFULL (1<<3) /* RxFIFO full */
164#define UTS_SOFTRST (1<<0) /* Software reset */
165
166/* We've been assigned a range on the "Low-density serial ports" major */
167#define SERIAL_IMX_MAJOR 207
168#define MINOR_START 16
169#define DEV_NAME "ttymxc"
170#define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
171
172/*
173 * This determines how often we check the modem status signals
174 * for any change. They generally aren't connected to an IRQ
175 * so we have to poll them. We also check immediately before
176 * filling the TX fifo incase CTS has been dropped.
177 */
178#define MCTRL_TIMEOUT (250*HZ/1000)
179
180#define DRIVER_NAME "IMX-uart"
181
182#define UART_NR 8
183
184struct imx_port {
185 struct uart_port port;
186 struct timer_list timer;
187 unsigned int old_status;
188 int txirq,rxirq,rtsirq;
189 unsigned int have_rtscts:1;
190 unsigned int use_irda:1;
191 unsigned int irda_inv_rx:1;
192 unsigned int irda_inv_tx:1;
193 unsigned short trcv_delay; /* transceiver delay */
194 struct clk *clk;
195};
196
197#ifdef CONFIG_IRDA
198#define USE_IRDA(sport) ((sport)->use_irda)
199#else
200#define USE_IRDA(sport) (0)
201#endif
202
203/*
204 * Handle any change of modem status signal since we were last called.
205 */
206static void imx_mctrl_check(struct imx_port *sport)
207{
208 unsigned int status, changed;
209
210 status = sport->port.ops->get_mctrl(&sport->port);
211 changed = status ^ sport->old_status;
212
213 if (changed == 0)
214 return;
215
216 sport->old_status = status;
217
218 if (changed & TIOCM_RI)
219 sport->port.icount.rng++;
220 if (changed & TIOCM_DSR)
221 sport->port.icount.dsr++;
222 if (changed & TIOCM_CAR)
223 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
224 if (changed & TIOCM_CTS)
225 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
226
227 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
228}
229
230/*
231 * This is our per-port timeout handler, for checking the
232 * modem status signals.
233 */
234static void imx_timeout(unsigned long data)
235{
236 struct imx_port *sport = (struct imx_port *)data;
237 unsigned long flags;
238
239 if (sport->port.state) {
240 spin_lock_irqsave(&sport->port.lock, flags);
241 imx_mctrl_check(sport);
242 spin_unlock_irqrestore(&sport->port.lock, flags);
243
244 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
245 }
246}
247
248/*
249 * interrupts disabled on entry
250 */
251static void imx_stop_tx(struct uart_port *port)
252{
253 struct imx_port *sport = (struct imx_port *)port;
254 unsigned long temp;
255
256 if (USE_IRDA(sport)) {
257 /* half duplex - wait for end of transmission */
258 int n = 256;
259 while ((--n > 0) &&
260 !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
261 udelay(5);
262 barrier();
263 }
264 /*
265 * irda transceiver - wait a bit more to avoid
266 * cutoff, hardware dependent
267 */
268 udelay(sport->trcv_delay);
269
270 /*
271 * half duplex - reactivate receive mode,
272 * flush receive pipe echo crap
273 */
274 if (readl(sport->port.membase + USR2) & USR2_TXDC) {
275 temp = readl(sport->port.membase + UCR1);
276 temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
277 writel(temp, sport->port.membase + UCR1);
278
279 temp = readl(sport->port.membase + UCR4);
280 temp &= ~(UCR4_TCEN);
281 writel(temp, sport->port.membase + UCR4);
282
283 while (readl(sport->port.membase + URXD0) &
284 URXD_CHARRDY)
285 barrier();
286
287 temp = readl(sport->port.membase + UCR1);
288 temp |= UCR1_RRDYEN;
289 writel(temp, sport->port.membase + UCR1);
290
291 temp = readl(sport->port.membase + UCR4);
292 temp |= UCR4_DREN;
293 writel(temp, sport->port.membase + UCR4);
294 }
295 return;
296 }
297
298 temp = readl(sport->port.membase + UCR1);
299 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
300}
301
302/*
303 * interrupts disabled on entry
304 */
305static void imx_stop_rx(struct uart_port *port)
306{
307 struct imx_port *sport = (struct imx_port *)port;
308 unsigned long temp;
309
310 temp = readl(sport->port.membase + UCR2);
311 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
312}
313
314/*
315 * Set the modem control timer to fire immediately.
316 */
317static void imx_enable_ms(struct uart_port *port)
318{
319 struct imx_port *sport = (struct imx_port *)port;
320
321 mod_timer(&sport->timer, jiffies);
322}
323
324static inline void imx_transmit_buffer(struct imx_port *sport)
325{
326 struct circ_buf *xmit = &sport->port.state->xmit;
327
328 while (!uart_circ_empty(xmit) &&
329 !(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
330 /* send xmit->buf[xmit->tail]
331 * out the port here */
332 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
333 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
334 sport->port.icount.tx++;
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(&sport->port);
339
340 if (uart_circ_empty(xmit))
341 imx_stop_tx(&sport->port);
342}
343
344/*
345 * interrupts disabled on entry
346 */
347static void imx_start_tx(struct uart_port *port)
348{
349 struct imx_port *sport = (struct imx_port *)port;
350 unsigned long temp;
351
352 if (USE_IRDA(sport)) {
353 /* half duplex in IrDA mode; have to disable receive mode */
354 temp = readl(sport->port.membase + UCR4);
355 temp &= ~(UCR4_DREN);
356 writel(temp, sport->port.membase + UCR4);
357
358 temp = readl(sport->port.membase + UCR1);
359 temp &= ~(UCR1_RRDYEN);
360 writel(temp, sport->port.membase + UCR1);
361 }
362
363 temp = readl(sport->port.membase + UCR1);
364 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
365
366 if (USE_IRDA(sport)) {
367 temp = readl(sport->port.membase + UCR1);
368 temp |= UCR1_TRDYEN;
369 writel(temp, sport->port.membase + UCR1);
370
371 temp = readl(sport->port.membase + UCR4);
372 temp |= UCR4_TCEN;
373 writel(temp, sport->port.membase + UCR4);
374 }
375
376 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
377 imx_transmit_buffer(sport);
378}
379
380static irqreturn_t imx_rtsint(int irq, void *dev_id)
381{
382 struct imx_port *sport = dev_id;
383 unsigned int val;
384 unsigned long flags;
385
386 spin_lock_irqsave(&sport->port.lock, flags);
387
388 writel(USR1_RTSD, sport->port.membase + USR1);
389 val = readl(sport->port.membase + USR1) & USR1_RTSS;
390 uart_handle_cts_change(&sport->port, !!val);
391 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
392
393 spin_unlock_irqrestore(&sport->port.lock, flags);
394 return IRQ_HANDLED;
395}
396
397static irqreturn_t imx_txint(int irq, void *dev_id)
398{
399 struct imx_port *sport = dev_id;
400 struct circ_buf *xmit = &sport->port.state->xmit;
401 unsigned long flags;
402
403 spin_lock_irqsave(&sport->port.lock,flags);
404 if (sport->port.x_char)
405 {
406 /* Send next char */
407 writel(sport->port.x_char, sport->port.membase + URTX0);
408 goto out;
409 }
410
411 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
412 imx_stop_tx(&sport->port);
413 goto out;
414 }
415
416 imx_transmit_buffer(sport);
417
418 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
419 uart_write_wakeup(&sport->port);
420
421out:
422 spin_unlock_irqrestore(&sport->port.lock,flags);
423 return IRQ_HANDLED;
424}
425
426static irqreturn_t imx_rxint(int irq, void *dev_id)
427{
428 struct imx_port *sport = dev_id;
429 unsigned int rx,flg,ignored = 0;
430 struct tty_struct *tty = sport->port.state->port.tty;
431 unsigned long flags, temp;
432
433 spin_lock_irqsave(&sport->port.lock,flags);
434
435 while (readl(sport->port.membase + USR2) & USR2_RDR) {
436 flg = TTY_NORMAL;
437 sport->port.icount.rx++;
438
439 rx = readl(sport->port.membase + URXD0);
440
441 temp = readl(sport->port.membase + USR2);
442 if (temp & USR2_BRCD) {
443 writel(USR2_BRCD, sport->port.membase + USR2);
444 if (uart_handle_break(&sport->port))
445 continue;
446 }
447
448 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
449 continue;
450
451 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
452 if (rx & URXD_PRERR)
453 sport->port.icount.parity++;
454 else if (rx & URXD_FRMERR)
455 sport->port.icount.frame++;
456 if (rx & URXD_OVRRUN)
457 sport->port.icount.overrun++;
458
459 if (rx & sport->port.ignore_status_mask) {
460 if (++ignored > 100)
461 goto out;
462 continue;
463 }
464
465 rx &= sport->port.read_status_mask;
466
467 if (rx & URXD_PRERR)
468 flg = TTY_PARITY;
469 else if (rx & URXD_FRMERR)
470 flg = TTY_FRAME;
471 if (rx & URXD_OVRRUN)
472 flg = TTY_OVERRUN;
473
474#ifdef SUPPORT_SYSRQ
475 sport->port.sysrq = 0;
476#endif
477 }
478
479 tty_insert_flip_char(tty, rx, flg);
480 }
481
482out:
483 spin_unlock_irqrestore(&sport->port.lock,flags);
484 tty_flip_buffer_push(tty);
485 return IRQ_HANDLED;
486}
487
488static irqreturn_t imx_int(int irq, void *dev_id)
489{
490 struct imx_port *sport = dev_id;
491 unsigned int sts;
492
493 sts = readl(sport->port.membase + USR1);
494
495 if (sts & USR1_RRDY)
496 imx_rxint(irq, dev_id);
497
498 if (sts & USR1_TRDY &&
499 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
500 imx_txint(irq, dev_id);
501
502 if (sts & USR1_RTSD)
503 imx_rtsint(irq, dev_id);
504
505 return IRQ_HANDLED;
506}
507
508/*
509 * Return TIOCSER_TEMT when transmitter is not busy.
510 */
511static unsigned int imx_tx_empty(struct uart_port *port)
512{
513 struct imx_port *sport = (struct imx_port *)port;
514
515 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
516}
517
518/*
519 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
520 */
521static unsigned int imx_get_mctrl(struct uart_port *port)
522{
523 struct imx_port *sport = (struct imx_port *)port;
524 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
525
526 if (readl(sport->port.membase + USR1) & USR1_RTSS)
527 tmp |= TIOCM_CTS;
528
529 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
530 tmp |= TIOCM_RTS;
531
532 return tmp;
533}
534
535static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
536{
537 struct imx_port *sport = (struct imx_port *)port;
538 unsigned long temp;
539
540 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
541
542 if (mctrl & TIOCM_RTS)
543 temp |= UCR2_CTS;
544
545 writel(temp, sport->port.membase + UCR2);
546}
547
548/*
549 * Interrupts always disabled.
550 */
551static void imx_break_ctl(struct uart_port *port, int break_state)
552{
553 struct imx_port *sport = (struct imx_port *)port;
554 unsigned long flags, temp;
555
556 spin_lock_irqsave(&sport->port.lock, flags);
557
558 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
559
560 if ( break_state != 0 )
561 temp |= UCR1_SNDBRK;
562
563 writel(temp, sport->port.membase + UCR1);
564
565 spin_unlock_irqrestore(&sport->port.lock, flags);
566}
567
568#define TXTL 2 /* reset default */
569#define RXTL 1 /* reset default */
570
571static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
572{
573 unsigned int val;
574 unsigned int ufcr_rfdiv;
575
576 /* set receiver / transmitter trigger level.
577 * RFDIV is set such way to satisfy requested uartclk value
578 */
579 val = TXTL << 10 | RXTL;
580 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
581 / sport->port.uartclk;
582
583 if(!ufcr_rfdiv)
584 ufcr_rfdiv = 1;
585
586 val |= UFCR_RFDIV_REG(ufcr_rfdiv);
587
588 writel(val, sport->port.membase + UFCR);
589
590 return 0;
591}
592
593/* half the RX buffer size */
594#define CTSTL 16
595
596static int imx_startup(struct uart_port *port)
597{
598 struct imx_port *sport = (struct imx_port *)port;
599 int retval;
600 unsigned long flags, temp;
601
602 imx_setup_ufcr(sport, 0);
603
604 /* disable the DREN bit (Data Ready interrupt enable) before
605 * requesting IRQs
606 */
607 temp = readl(sport->port.membase + UCR4);
608
609 if (USE_IRDA(sport))
610 temp |= UCR4_IRSC;
611
612 /* set the trigger level for CTS */
613 temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
614 temp |= CTSTL<< UCR4_CTSTL_SHF;
615
616 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
617
618 if (USE_IRDA(sport)) {
619 /* reset fifo's and state machines */
620 int i = 100;
621 temp = readl(sport->port.membase + UCR2);
622 temp &= ~UCR2_SRST;
623 writel(temp, sport->port.membase + UCR2);
624 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
625 (--i > 0)) {
626 udelay(1);
627 }
628 }
629
630 /*
631 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
632 * chips only have one interrupt.
633 */
634 if (sport->txirq > 0) {
635 retval = request_irq(sport->rxirq, imx_rxint, 0,
636 DRIVER_NAME, sport);
637 if (retval)
638 goto error_out1;
639
640 retval = request_irq(sport->txirq, imx_txint, 0,
641 DRIVER_NAME, sport);
642 if (retval)
643 goto error_out2;
644
645 /* do not use RTS IRQ on IrDA */
646 if (!USE_IRDA(sport)) {
647 retval = request_irq(sport->rtsirq, imx_rtsint,
648 (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
649 IRQF_TRIGGER_FALLING |
650 IRQF_TRIGGER_RISING,
651 DRIVER_NAME, sport);
652 if (retval)
653 goto error_out3;
654 }
655 } else {
656 retval = request_irq(sport->port.irq, imx_int, 0,
657 DRIVER_NAME, sport);
658 if (retval) {
659 free_irq(sport->port.irq, sport);
660 goto error_out1;
661 }
662 }
663
664 /*
665 * Finally, clear and enable interrupts
666 */
667 writel(USR1_RTSD, sport->port.membase + USR1);
668
669 temp = readl(sport->port.membase + UCR1);
670 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
671
672 if (USE_IRDA(sport)) {
673 temp |= UCR1_IREN;
674 temp &= ~(UCR1_RTSDEN);
675 }
676
677 writel(temp, sport->port.membase + UCR1);
678
679 temp = readl(sport->port.membase + UCR2);
680 temp |= (UCR2_RXEN | UCR2_TXEN);
681 writel(temp, sport->port.membase + UCR2);
682
683 if (USE_IRDA(sport)) {
684 /* clear RX-FIFO */
685 int i = 64;
686 while ((--i > 0) &&
687 (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
688 barrier();
689 }
690 }
691
692 if (!cpu_is_mx1()) {
693 temp = readl(sport->port.membase + UCR3);
694 temp |= MX2_UCR3_RXDMUXSEL;
695 writel(temp, sport->port.membase + UCR3);
696 }
697
698 if (USE_IRDA(sport)) {
699 temp = readl(sport->port.membase + UCR4);
700 if (sport->irda_inv_rx)
701 temp |= UCR4_INVR;
702 else
703 temp &= ~(UCR4_INVR);
704 writel(temp | UCR4_DREN, sport->port.membase + UCR4);
705
706 temp = readl(sport->port.membase + UCR3);
707 if (sport->irda_inv_tx)
708 temp |= UCR3_INVT;
709 else
710 temp &= ~(UCR3_INVT);
711 writel(temp, sport->port.membase + UCR3);
712 }
713
714 /*
715 * Enable modem status interrupts
716 */
717 spin_lock_irqsave(&sport->port.lock,flags);
718 imx_enable_ms(&sport->port);
719 spin_unlock_irqrestore(&sport->port.lock,flags);
720
721 if (USE_IRDA(sport)) {
722 struct imxuart_platform_data *pdata;
723 pdata = sport->port.dev->platform_data;
724 sport->irda_inv_rx = pdata->irda_inv_rx;
725 sport->irda_inv_tx = pdata->irda_inv_tx;
726 sport->trcv_delay = pdata->transceiver_delay;
727 if (pdata->irda_enable)
728 pdata->irda_enable(1);
729 }
730
731 return 0;
732
733error_out3:
734 if (sport->txirq)
735 free_irq(sport->txirq, sport);
736error_out2:
737 if (sport->rxirq)
738 free_irq(sport->rxirq, sport);
739error_out1:
740 return retval;
741}
742
743static void imx_shutdown(struct uart_port *port)
744{
745 struct imx_port *sport = (struct imx_port *)port;
746 unsigned long temp;
747
748 temp = readl(sport->port.membase + UCR2);
749 temp &= ~(UCR2_TXEN);
750 writel(temp, sport->port.membase + UCR2);
751
752 if (USE_IRDA(sport)) {
753 struct imxuart_platform_data *pdata;
754 pdata = sport->port.dev->platform_data;
755 if (pdata->irda_enable)
756 pdata->irda_enable(0);
757 }
758
759 /*
760 * Stop our timer.
761 */
762 del_timer_sync(&sport->timer);
763
764 /*
765 * Free the interrupts
766 */
767 if (sport->txirq > 0) {
768 if (!USE_IRDA(sport))
769 free_irq(sport->rtsirq, sport);
770 free_irq(sport->txirq, sport);
771 free_irq(sport->rxirq, sport);
772 } else
773 free_irq(sport->port.irq, sport);
774
775 /*
776 * Disable all interrupts, port and break condition.
777 */
778
779 temp = readl(sport->port.membase + UCR1);
780 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
781 if (USE_IRDA(sport))
782 temp &= ~(UCR1_IREN);
783
784 writel(temp, sport->port.membase + UCR1);
785}
786
787static void
788imx_set_termios(struct uart_port *port, struct ktermios *termios,
789 struct ktermios *old)
790{
791 struct imx_port *sport = (struct imx_port *)port;
792 unsigned long flags;
793 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
794 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
795 unsigned int div, ufcr;
796 unsigned long num, denom;
797 uint64_t tdiv64;
798
799 /*
800 * If we don't support modem control lines, don't allow
801 * these to be set.
802 */
803 if (0) {
804 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
805 termios->c_cflag |= CLOCAL;
806 }
807
808 /*
809 * We only support CS7 and CS8.
810 */
811 while ((termios->c_cflag & CSIZE) != CS7 &&
812 (termios->c_cflag & CSIZE) != CS8) {
813 termios->c_cflag &= ~CSIZE;
814 termios->c_cflag |= old_csize;
815 old_csize = CS8;
816 }
817
818 if ((termios->c_cflag & CSIZE) == CS8)
819 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
820 else
821 ucr2 = UCR2_SRST | UCR2_IRTS;
822
823 if (termios->c_cflag & CRTSCTS) {
824 if( sport->have_rtscts ) {
825 ucr2 &= ~UCR2_IRTS;
826 ucr2 |= UCR2_CTSC;
827 } else {
828 termios->c_cflag &= ~CRTSCTS;
829 }
830 }
831
832 if (termios->c_cflag & CSTOPB)
833 ucr2 |= UCR2_STPB;
834 if (termios->c_cflag & PARENB) {
835 ucr2 |= UCR2_PREN;
836 if (termios->c_cflag & PARODD)
837 ucr2 |= UCR2_PROE;
838 }
839
840 /*
841 * Ask the core to calculate the divisor for us.
842 */
843 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
844 quot = uart_get_divisor(port, baud);
845
846 spin_lock_irqsave(&sport->port.lock, flags);
847
848 sport->port.read_status_mask = 0;
849 if (termios->c_iflag & INPCK)
850 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
851 if (termios->c_iflag & (BRKINT | PARMRK))
852 sport->port.read_status_mask |= URXD_BRK;
853
854 /*
855 * Characters to ignore
856 */
857 sport->port.ignore_status_mask = 0;
858 if (termios->c_iflag & IGNPAR)
859 sport->port.ignore_status_mask |= URXD_PRERR;
860 if (termios->c_iflag & IGNBRK) {
861 sport->port.ignore_status_mask |= URXD_BRK;
862 /*
863 * If we're ignoring parity and break indicators,
864 * ignore overruns too (for real raw support).
865 */
866 if (termios->c_iflag & IGNPAR)
867 sport->port.ignore_status_mask |= URXD_OVRRUN;
868 }
869
870 del_timer_sync(&sport->timer);
871
872 /*
873 * Update the per-port timeout.
874 */
875 uart_update_timeout(port, termios->c_cflag, baud);
876
877 /*
878 * disable interrupts and drain transmitter
879 */
880 old_ucr1 = readl(sport->port.membase + UCR1);
881 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
882 sport->port.membase + UCR1);
883
884 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
885 barrier();
886
887 /* then, disable everything */
888 old_txrxen = readl(sport->port.membase + UCR2);
889 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
890 sport->port.membase + UCR2);
891 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
892
893 if (USE_IRDA(sport)) {
894 /*
895 * use maximum available submodule frequency to
896 * avoid missing short pulses due to low sampling rate
897 */
898 div = 1;
899 } else {
900 div = sport->port.uartclk / (baud * 16);
901 if (div > 7)
902 div = 7;
903 if (!div)
904 div = 1;
905 }
906
907 rational_best_approximation(16 * div * baud, sport->port.uartclk,
908 1 << 16, 1 << 16, &num, &denom);
909
910 tdiv64 = sport->port.uartclk;
911 tdiv64 *= num;
912 do_div(tdiv64, denom * 16 * div);
913 tty_termios_encode_baud_rate(termios,
914 (speed_t)tdiv64, (speed_t)tdiv64);
915
916 num -= 1;
917 denom -= 1;
918
919 ufcr = readl(sport->port.membase + UFCR);
920 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
921 writel(ufcr, sport->port.membase + UFCR);
922
923 writel(num, sport->port.membase + UBIR);
924 writel(denom, sport->port.membase + UBMR);
925
926 if (!cpu_is_mx1())
927 writel(sport->port.uartclk / div / 1000,
928 sport->port.membase + MX2_ONEMS);
929
930 writel(old_ucr1, sport->port.membase + UCR1);
931
932 /* set the parity, stop bits and data size */
933 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
934
935 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
936 imx_enable_ms(&sport->port);
937
938 spin_unlock_irqrestore(&sport->port.lock, flags);
939}
940
941static const char *imx_type(struct uart_port *port)
942{
943 struct imx_port *sport = (struct imx_port *)port;
944
945 return sport->port.type == PORT_IMX ? "IMX" : NULL;
946}
947
948/*
949 * Release the memory region(s) being used by 'port'.
950 */
951static void imx_release_port(struct uart_port *port)
952{
953 struct platform_device *pdev = to_platform_device(port->dev);
954 struct resource *mmres;
955
956 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
958}
959
960/*
961 * Request the memory region(s) being used by 'port'.
962 */
963static int imx_request_port(struct uart_port *port)
964{
965 struct platform_device *pdev = to_platform_device(port->dev);
966 struct resource *mmres;
967 void *ret;
968
969 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
970 if (!mmres)
971 return -ENODEV;
972
973 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
974 "imx-uart");
975
976 return ret ? 0 : -EBUSY;
977}
978
979/*
980 * Configure/autoconfigure the port.
981 */
982static void imx_config_port(struct uart_port *port, int flags)
983{
984 struct imx_port *sport = (struct imx_port *)port;
985
986 if (flags & UART_CONFIG_TYPE &&
987 imx_request_port(&sport->port) == 0)
988 sport->port.type = PORT_IMX;
989}
990
991/*
992 * Verify the new serial_struct (for TIOCSSERIAL).
993 * The only change we allow are to the flags and type, and
994 * even then only between PORT_IMX and PORT_UNKNOWN
995 */
996static int
997imx_verify_port(struct uart_port *port, struct serial_struct *ser)
998{
999 struct imx_port *sport = (struct imx_port *)port;
1000 int ret = 0;
1001
1002 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1003 ret = -EINVAL;
1004 if (sport->port.irq != ser->irq)
1005 ret = -EINVAL;
1006 if (ser->io_type != UPIO_MEM)
1007 ret = -EINVAL;
1008 if (sport->port.uartclk / 16 != ser->baud_base)
1009 ret = -EINVAL;
1010 if ((void *)sport->port.mapbase != ser->iomem_base)
1011 ret = -EINVAL;
1012 if (sport->port.iobase != ser->port)
1013 ret = -EINVAL;
1014 if (ser->hub6 != 0)
1015 ret = -EINVAL;
1016 return ret;
1017}
1018
1019static struct uart_ops imx_pops = {
1020 .tx_empty = imx_tx_empty,
1021 .set_mctrl = imx_set_mctrl,
1022 .get_mctrl = imx_get_mctrl,
1023 .stop_tx = imx_stop_tx,
1024 .start_tx = imx_start_tx,
1025 .stop_rx = imx_stop_rx,
1026 .enable_ms = imx_enable_ms,
1027 .break_ctl = imx_break_ctl,
1028 .startup = imx_startup,
1029 .shutdown = imx_shutdown,
1030 .set_termios = imx_set_termios,
1031 .type = imx_type,
1032 .release_port = imx_release_port,
1033 .request_port = imx_request_port,
1034 .config_port = imx_config_port,
1035 .verify_port = imx_verify_port,
1036};
1037
1038static struct imx_port *imx_ports[UART_NR];
1039
1040#ifdef CONFIG_SERIAL_IMX_CONSOLE
1041static void imx_console_putchar(struct uart_port *port, int ch)
1042{
1043 struct imx_port *sport = (struct imx_port *)port;
1044
1045 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
1046 barrier();
1047
1048 writel(ch, sport->port.membase + URTX0);
1049}
1050
1051/*
1052 * Interrupts are disabled on entering
1053 */
1054static void
1055imx_console_write(struct console *co, const char *s, unsigned int count)
1056{
1057 struct imx_port *sport = imx_ports[co->index];
1058 unsigned int old_ucr1, old_ucr2, ucr1;
1059
1060 /*
1061 * First, save UCR1/2 and then disable interrupts
1062 */
1063 ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
1064 old_ucr2 = readl(sport->port.membase + UCR2);
1065
1066 if (cpu_is_mx1())
1067 ucr1 |= MX1_UCR1_UARTCLKEN;
1068 ucr1 |= UCR1_UARTEN;
1069 ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
1070
1071 writel(ucr1, sport->port.membase + UCR1);
1072
1073 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1074
1075 uart_console_write(&sport->port, s, count, imx_console_putchar);
1076
1077 /*
1078 * Finally, wait for transmitter to become empty
1079 * and restore UCR1/2
1080 */
1081 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1082
1083 writel(old_ucr1, sport->port.membase + UCR1);
1084 writel(old_ucr2, sport->port.membase + UCR2);
1085}
1086
1087/*
1088 * If the port was already initialised (eg, by a boot loader),
1089 * try to determine the current setup.
1090 */
1091static void __init
1092imx_console_get_options(struct imx_port *sport, int *baud,
1093 int *parity, int *bits)
1094{
1095
1096 if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
1097 /* ok, the port was enabled */
1098 unsigned int ucr2, ubir,ubmr, uartclk;
1099 unsigned int baud_raw;
1100 unsigned int ucfr_rfdiv;
1101
1102 ucr2 = readl(sport->port.membase + UCR2);
1103
1104 *parity = 'n';
1105 if (ucr2 & UCR2_PREN) {
1106 if (ucr2 & UCR2_PROE)
1107 *parity = 'o';
1108 else
1109 *parity = 'e';
1110 }
1111
1112 if (ucr2 & UCR2_WS)
1113 *bits = 8;
1114 else
1115 *bits = 7;
1116
1117 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1118 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
1119
1120 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
1121 if (ucfr_rfdiv == 6)
1122 ucfr_rfdiv = 7;
1123 else
1124 ucfr_rfdiv = 6 - ucfr_rfdiv;
1125
1126 uartclk = clk_get_rate(sport->clk);
1127 uartclk /= ucfr_rfdiv;
1128
1129 { /*
1130 * The next code provides exact computation of
1131 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1132 * without need of float support or long long division,
1133 * which would be required to prevent 32bit arithmetic overflow
1134 */
1135 unsigned int mul = ubir + 1;
1136 unsigned int div = 16 * (ubmr + 1);
1137 unsigned int rem = uartclk % div;
1138
1139 baud_raw = (uartclk / div) * mul;
1140 baud_raw += (rem * mul + div / 2) / div;
1141 *baud = (baud_raw + 50) / 100 * 100;
1142 }
1143
1144 if(*baud != baud_raw)
1145 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1146 baud_raw, *baud);
1147 }
1148}
1149
1150static int __init
1151imx_console_setup(struct console *co, char *options)
1152{
1153 struct imx_port *sport;
1154 int baud = 9600;
1155 int bits = 8;
1156 int parity = 'n';
1157 int flow = 'n';
1158
1159 /*
1160 * Check whether an invalid uart number has been specified, and
1161 * if so, search for the first available port that does have
1162 * console support.
1163 */
1164 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1165 co->index = 0;
1166 sport = imx_ports[co->index];
1167 if(sport == NULL)
1168 return -ENODEV;
1169
1170 if (options)
1171 uart_parse_options(options, &baud, &parity, &bits, &flow);
1172 else
1173 imx_console_get_options(sport, &baud, &parity, &bits);
1174
1175 imx_setup_ufcr(sport, 0);
1176
1177 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
1178}
1179
1180static struct uart_driver imx_reg;
1181static struct console imx_console = {
1182 .name = DEV_NAME,
1183 .write = imx_console_write,
1184 .device = uart_console_device,
1185 .setup = imx_console_setup,
1186 .flags = CON_PRINTBUFFER,
1187 .index = -1,
1188 .data = &imx_reg,
1189};
1190
1191#define IMX_CONSOLE &imx_console
1192#else
1193#define IMX_CONSOLE NULL
1194#endif
1195
1196static struct uart_driver imx_reg = {
1197 .owner = THIS_MODULE,
1198 .driver_name = DRIVER_NAME,
1199 .dev_name = DEV_NAME,
1200 .major = SERIAL_IMX_MAJOR,
1201 .minor = MINOR_START,
1202 .nr = ARRAY_SIZE(imx_ports),
1203 .cons = IMX_CONSOLE,
1204};
1205
1206static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
1207{
1208 struct imx_port *sport = platform_get_drvdata(dev);
1209
1210 if (sport)
1211 uart_suspend_port(&imx_reg, &sport->port);
1212
1213 return 0;
1214}
1215
1216static int serial_imx_resume(struct platform_device *dev)
1217{
1218 struct imx_port *sport = platform_get_drvdata(dev);
1219
1220 if (sport)
1221 uart_resume_port(&imx_reg, &sport->port);
1222
1223 return 0;
1224}
1225
1226static int serial_imx_probe(struct platform_device *pdev)
1227{
1228 struct imx_port *sport;
1229 struct imxuart_platform_data *pdata;
1230 void __iomem *base;
1231 int ret = 0;
1232 struct resource *res;
1233
1234 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1235 if (!sport)
1236 return -ENOMEM;
1237
1238 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1239 if (!res) {
1240 ret = -ENODEV;
1241 goto free;
1242 }
1243
1244 base = ioremap(res->start, PAGE_SIZE);
1245 if (!base) {
1246 ret = -ENOMEM;
1247 goto free;
1248 }
1249
1250 sport->port.dev = &pdev->dev;
1251 sport->port.mapbase = res->start;
1252 sport->port.membase = base;
1253 sport->port.type = PORT_IMX,
1254 sport->port.iotype = UPIO_MEM;
1255 sport->port.irq = platform_get_irq(pdev, 0);
1256 sport->rxirq = platform_get_irq(pdev, 0);
1257 sport->txirq = platform_get_irq(pdev, 1);
1258 sport->rtsirq = platform_get_irq(pdev, 2);
1259 sport->port.fifosize = 32;
1260 sport->port.ops = &imx_pops;
1261 sport->port.flags = UPF_BOOT_AUTOCONF;
1262 sport->port.line = pdev->id;
1263 init_timer(&sport->timer);
1264 sport->timer.function = imx_timeout;
1265 sport->timer.data = (unsigned long)sport;
1266
1267 sport->clk = clk_get(&pdev->dev, "uart");
1268 if (IS_ERR(sport->clk)) {
1269 ret = PTR_ERR(sport->clk);
1270 goto unmap;
1271 }
1272 clk_enable(sport->clk);
1273
1274 sport->port.uartclk = clk_get_rate(sport->clk);
1275
1276 imx_ports[pdev->id] = sport;
1277
1278 pdata = pdev->dev.platform_data;
1279 if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
1280 sport->have_rtscts = 1;
1281
1282#ifdef CONFIG_IRDA
1283 if (pdata && (pdata->flags & IMXUART_IRDA))
1284 sport->use_irda = 1;
1285#endif
1286
1287 if (pdata && pdata->init) {
1288 ret = pdata->init(pdev);
1289 if (ret)
1290 goto clkput;
1291 }
1292
1293 ret = uart_add_one_port(&imx_reg, &sport->port);
1294 if (ret)
1295 goto deinit;
1296 platform_set_drvdata(pdev, &sport->port);
1297
1298 return 0;
1299deinit:
1300 if (pdata && pdata->exit)
1301 pdata->exit(pdev);
1302clkput:
1303 clk_put(sport->clk);
1304 clk_disable(sport->clk);
1305unmap:
1306 iounmap(sport->port.membase);
1307free:
1308 kfree(sport);
1309
1310 return ret;
1311}
1312
1313static int serial_imx_remove(struct platform_device *pdev)
1314{
1315 struct imxuart_platform_data *pdata;
1316 struct imx_port *sport = platform_get_drvdata(pdev);
1317
1318 pdata = pdev->dev.platform_data;
1319
1320 platform_set_drvdata(pdev, NULL);
1321
1322 if (sport) {
1323 uart_remove_one_port(&imx_reg, &sport->port);
1324 clk_put(sport->clk);
1325 }
1326
1327 clk_disable(sport->clk);
1328
1329 if (pdata && pdata->exit)
1330 pdata->exit(pdev);
1331
1332 iounmap(sport->port.membase);
1333 kfree(sport);
1334
1335 return 0;
1336}
1337
1338static struct platform_driver serial_imx_driver = {
1339 .probe = serial_imx_probe,
1340 .remove = serial_imx_remove,
1341
1342 .suspend = serial_imx_suspend,
1343 .resume = serial_imx_resume,
1344 .driver = {
1345 .name = "imx-uart",
1346 .owner = THIS_MODULE,
1347 },
1348};
1349
1350static int __init imx_serial_init(void)
1351{
1352 int ret;
1353
1354 printk(KERN_INFO "Serial: IMX driver\n");
1355
1356 ret = uart_register_driver(&imx_reg);
1357 if (ret)
1358 return ret;
1359
1360 ret = platform_driver_register(&serial_imx_driver);
1361 if (ret != 0)
1362 uart_unregister_driver(&imx_reg);
1363
1364 return 0;
1365}
1366
1367static void __exit imx_serial_exit(void)
1368{
1369 platform_driver_unregister(&serial_imx_driver);
1370 uart_unregister_driver(&imx_reg);
1371}
1372
1373module_init(imx_serial_init);
1374module_exit(imx_serial_exit);
1375
1376MODULE_AUTHOR("Sascha Hauer");
1377MODULE_DESCRIPTION("IMX generic serial port driver");
1378MODULE_LICENSE("GPL");
1379MODULE_ALIAS("platform:imx-uart");
diff --git a/drivers/tty/serial/ioc3_serial.c b/drivers/tty/serial/ioc3_serial.c
new file mode 100644
index 000000000000..ee43efc7bdcc
--- /dev/null
+++ b/drivers/tty/serial/ioc3_serial.c
@@ -0,0 +1,2199 @@
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9/*
10 * This file contains a module version of the ioc3 serial driver. This
11 * includes all the support functions needed (support functions, etc.)
12 * and the serial driver itself.
13 */
14#include <linux/errno.h>
15#include <linux/tty.h>
16#include <linux/serial.h>
17#include <linux/circ_buf.h>
18#include <linux/serial_reg.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/serial_core.h>
22#include <linux/ioc3.h>
23#include <linux/slab.h>
24
25/*
26 * Interesting things about the ioc3
27 */
28
29#define LOGICAL_PORTS 2 /* rs232(0) and rs422(1) */
30#define PORTS_PER_CARD 2
31#define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
32#define MAX_CARDS 8
33#define MAX_LOGICAL_PORTS (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
34
35/* determine given the sio_ir what port it applies to */
36#define GET_PORT_FROM_SIO_IR(_x) (_x & SIO_IR_SA) ? 0 : 1
37
38
39/*
40 * we have 2 logical ports (rs232, rs422) for each physical port
41 * evens are rs232, odds are rs422
42 */
43#define GET_PHYSICAL_PORT(_x) ((_x) >> 1)
44#define GET_LOGICAL_PORT(_x) ((_x) & 1)
45#define IS_PHYSICAL_PORT(_x) !((_x) & 1)
46#define IS_RS232(_x) !((_x) & 1)
47
48static unsigned int Num_of_ioc3_cards;
49static unsigned int Submodule_slot;
50
51/* defining this will get you LOTS of great debug info */
52//#define DEBUG_INTERRUPTS
53#define DPRINT_CONFIG(_x...) ;
54//#define DPRINT_CONFIG(_x...) printk _x
55#define NOT_PROGRESS() ;
56//#define NOT_PROGRESS() printk("%s : fails %d\n", __func__, __LINE__)
57
58/* number of characters we want to transmit to the lower level at a time */
59#define MAX_CHARS 256
60#define FIFO_SIZE (MAX_CHARS-1) /* it's a uchar */
61
62/* Device name we're using */
63#define DEVICE_NAME "ttySIOC"
64#define DEVICE_MAJOR 204
65#define DEVICE_MINOR 116
66
67/* flags for next_char_state */
68#define NCS_BREAK 0x1
69#define NCS_PARITY 0x2
70#define NCS_FRAMING 0x4
71#define NCS_OVERRUN 0x8
72
73/* cause we need SOME parameters ... */
74#define MIN_BAUD_SUPPORTED 1200
75#define MAX_BAUD_SUPPORTED 115200
76
77/* protocol types supported */
78#define PROTO_RS232 0
79#define PROTO_RS422 1
80
81/* Notification types */
82#define N_DATA_READY 0x01
83#define N_OUTPUT_LOWAT 0x02
84#define N_BREAK 0x04
85#define N_PARITY_ERROR 0x08
86#define N_FRAMING_ERROR 0x10
87#define N_OVERRUN_ERROR 0x20
88#define N_DDCD 0x40
89#define N_DCTS 0x80
90
91#define N_ALL_INPUT (N_DATA_READY | N_BREAK \
92 | N_PARITY_ERROR | N_FRAMING_ERROR \
93 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
94
95#define N_ALL_OUTPUT N_OUTPUT_LOWAT
96
97#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR \
98 | N_OVERRUN_ERROR)
99
100#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK \
101 | N_PARITY_ERROR | N_FRAMING_ERROR \
102 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
103
104#define SER_CLK_SPEED(prediv) ((22000000 << 1) / prediv)
105#define SER_DIVISOR(x, clk) (((clk) + (x) * 8) / ((x) * 16))
106#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
107
108/* Some masks */
109#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
110 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
111#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
112
113#define PENDING(_a, _p) (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
114
115#define RING_BUF_SIZE 4096
116#define BUF_SIZE_BIT SBBR_L_SIZE
117#define PROD_CONS_MASK PROD_CONS_PTR_4K
118
119#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
120
121/* driver specific - one per card */
122struct ioc3_card {
123 struct {
124 /* uart ports are allocated here */
125 struct uart_port icp_uart_port[LOGICAL_PORTS];
126 /* the ioc3_port used for this port */
127 struct ioc3_port *icp_port;
128 } ic_port[PORTS_PER_CARD];
129 /* currently enabled interrupts */
130 uint32_t ic_enable;
131};
132
133/* Local port info for each IOC3 serial port */
134struct ioc3_port {
135 /* handy reference material */
136 struct uart_port *ip_port;
137 struct ioc3_card *ip_card;
138 struct ioc3_driver_data *ip_idd;
139 struct ioc3_submodule *ip_is;
140
141 /* pci mem addresses for this port */
142 struct ioc3_serialregs __iomem *ip_serial_regs;
143 struct ioc3_uartregs __iomem *ip_uart_regs;
144
145 /* Ring buffer page for this port */
146 dma_addr_t ip_dma_ringbuf;
147 /* vaddr of ring buffer */
148 struct ring_buffer *ip_cpu_ringbuf;
149
150 /* Rings for this port */
151 struct ring *ip_inring;
152 struct ring *ip_outring;
153
154 /* Hook to port specific values */
155 struct port_hooks *ip_hooks;
156
157 spinlock_t ip_lock;
158
159 /* Various rx/tx parameters */
160 int ip_baud;
161 int ip_tx_lowat;
162 int ip_rx_timeout;
163
164 /* Copy of notification bits */
165 int ip_notify;
166
167 /* Shadow copies of various registers so we don't need to PIO
168 * read them constantly
169 */
170 uint32_t ip_sscr;
171 uint32_t ip_tx_prod;
172 uint32_t ip_rx_cons;
173 unsigned char ip_flags;
174};
175
176/* tx low water mark. We need to notify the driver whenever tx is getting
177 * close to empty so it can refill the tx buffer and keep things going.
178 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
179 * have no trouble getting in more chars in time (I certainly hope so).
180 */
181#define TX_LOWAT_LATENCY 1000
182#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
183#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
184
185/* Flags per port */
186#define INPUT_HIGH 0x01
187 /* used to signify that we have turned off the rx_high
188 * temporarily - we need to drain the fifo and don't
189 * want to get blasted with interrupts.
190 */
191#define DCD_ON 0x02
192 /* DCD state is on */
193#define LOWAT_WRITTEN 0x04
194#define READ_ABORTED 0x08
195 /* the read was aborted - used to avaoid infinate looping
196 * in the interrupt handler
197 */
198#define INPUT_ENABLE 0x10
199
200/* Since each port has different register offsets and bitmasks
201 * for everything, we'll store those that we need in tables so we
202 * don't have to be constantly checking the port we are dealing with.
203 */
204struct port_hooks {
205 uint32_t intr_delta_dcd;
206 uint32_t intr_delta_cts;
207 uint32_t intr_tx_mt;
208 uint32_t intr_rx_timer;
209 uint32_t intr_rx_high;
210 uint32_t intr_tx_explicit;
211 uint32_t intr_clear;
212 uint32_t intr_all;
213 char rs422_select_pin;
214};
215
216static struct port_hooks hooks_array[PORTS_PER_CARD] = {
217 /* values for port A */
218 {
219 .intr_delta_dcd = SIO_IR_SA_DELTA_DCD,
220 .intr_delta_cts = SIO_IR_SA_DELTA_CTS,
221 .intr_tx_mt = SIO_IR_SA_TX_MT,
222 .intr_rx_timer = SIO_IR_SA_RX_TIMER,
223 .intr_rx_high = SIO_IR_SA_RX_HIGH,
224 .intr_tx_explicit = SIO_IR_SA_TX_EXPLICIT,
225 .intr_clear = (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL
226 | SIO_IR_SA_RX_HIGH
227 | SIO_IR_SA_RX_TIMER
228 | SIO_IR_SA_DELTA_DCD
229 | SIO_IR_SA_DELTA_CTS
230 | SIO_IR_SA_INT
231 | SIO_IR_SA_TX_EXPLICIT
232 | SIO_IR_SA_MEMERR),
233 .intr_all = SIO_IR_SA,
234 .rs422_select_pin = GPPR_UARTA_MODESEL_PIN,
235 },
236
237 /* values for port B */
238 {
239 .intr_delta_dcd = SIO_IR_SB_DELTA_DCD,
240 .intr_delta_cts = SIO_IR_SB_DELTA_CTS,
241 .intr_tx_mt = SIO_IR_SB_TX_MT,
242 .intr_rx_timer = SIO_IR_SB_RX_TIMER,
243 .intr_rx_high = SIO_IR_SB_RX_HIGH,
244 .intr_tx_explicit = SIO_IR_SB_TX_EXPLICIT,
245 .intr_clear = (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL
246 | SIO_IR_SB_RX_HIGH
247 | SIO_IR_SB_RX_TIMER
248 | SIO_IR_SB_DELTA_DCD
249 | SIO_IR_SB_DELTA_CTS
250 | SIO_IR_SB_INT
251 | SIO_IR_SB_TX_EXPLICIT
252 | SIO_IR_SB_MEMERR),
253 .intr_all = SIO_IR_SB,
254 .rs422_select_pin = GPPR_UARTB_MODESEL_PIN,
255 }
256};
257
258struct ring_entry {
259 union {
260 struct {
261 uint32_t alldata;
262 uint32_t allsc;
263 } all;
264 struct {
265 char data[4]; /* data bytes */
266 char sc[4]; /* status/control */
267 } s;
268 } u;
269};
270
271/* Test the valid bits in any of the 4 sc chars using "allsc" member */
272#define RING_ANY_VALID \
273 ((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
274
275#define ring_sc u.s.sc
276#define ring_data u.s.data
277#define ring_allsc u.all.allsc
278
279/* Number of entries per ring buffer. */
280#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
281
282/* An individual ring */
283struct ring {
284 struct ring_entry entries[ENTRIES_PER_RING];
285};
286
287/* The whole enchilada */
288struct ring_buffer {
289 struct ring TX_A;
290 struct ring RX_A;
291 struct ring TX_B;
292 struct ring RX_B;
293};
294
295/* Get a ring from a port struct */
296#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
297
298/* for Infinite loop detection */
299#define MAXITER 10000000
300
301
302/**
303 * set_baud - Baud rate setting code
304 * @port: port to set
305 * @baud: baud rate to use
306 */
307static int set_baud(struct ioc3_port *port, int baud)
308{
309 int divisor;
310 int actual_baud;
311 int diff;
312 int lcr, prediv;
313 struct ioc3_uartregs __iomem *uart;
314
315 for (prediv = 6; prediv < 64; prediv++) {
316 divisor = SER_DIVISOR(baud, SER_CLK_SPEED(prediv));
317 if (!divisor)
318 continue; /* invalid divisor */
319 actual_baud = DIVISOR_TO_BAUD(divisor, SER_CLK_SPEED(prediv));
320
321 diff = actual_baud - baud;
322 if (diff < 0)
323 diff = -diff;
324
325 /* if we're within 1% we've found a match */
326 if (diff * 100 <= actual_baud)
327 break;
328 }
329
330 /* if the above loop completed, we didn't match
331 * the baud rate. give up.
332 */
333 if (prediv == 64) {
334 NOT_PROGRESS();
335 return 1;
336 }
337
338 uart = port->ip_uart_regs;
339 lcr = readb(&uart->iu_lcr);
340
341 writeb(lcr | UART_LCR_DLAB, &uart->iu_lcr);
342 writeb((unsigned char)divisor, &uart->iu_dll);
343 writeb((unsigned char)(divisor >> 8), &uart->iu_dlm);
344 writeb((unsigned char)prediv, &uart->iu_scr);
345 writeb((unsigned char)lcr, &uart->iu_lcr);
346
347 return 0;
348}
349
350/**
351 * get_ioc3_port - given a uart port, return the control structure
352 * @the_port: uart port to find
353 */
354static struct ioc3_port *get_ioc3_port(struct uart_port *the_port)
355{
356 struct ioc3_driver_data *idd = dev_get_drvdata(the_port->dev);
357 struct ioc3_card *card_ptr = idd->data[Submodule_slot];
358 int ii, jj;
359
360 if (!card_ptr) {
361 NOT_PROGRESS();
362 return NULL;
363 }
364 for (ii = 0; ii < PORTS_PER_CARD; ii++) {
365 for (jj = 0; jj < LOGICAL_PORTS; jj++) {
366 if (the_port == &card_ptr->ic_port[ii].icp_uart_port[jj])
367 return card_ptr->ic_port[ii].icp_port;
368 }
369 }
370 NOT_PROGRESS();
371 return NULL;
372}
373
374/**
375 * port_init - Initialize the sio and ioc3 hardware for a given port
376 * called per port from attach...
377 * @port: port to initialize
378 */
379static int inline port_init(struct ioc3_port *port)
380{
381 uint32_t sio_cr;
382 struct port_hooks *hooks = port->ip_hooks;
383 struct ioc3_uartregs __iomem *uart;
384 int reset_loop_counter = 0xfffff;
385 struct ioc3_driver_data *idd = port->ip_idd;
386
387 /* Idle the IOC3 serial interface */
388 writel(SSCR_RESET, &port->ip_serial_regs->sscr);
389
390 /* Wait until any pending bus activity for this port has ceased */
391 do {
392 sio_cr = readl(&idd->vma->sio_cr);
393 if (reset_loop_counter-- <= 0) {
394 printk(KERN_WARNING
395 "IOC3 unable to come out of reset"
396 " scr 0x%x\n", sio_cr);
397 return -1;
398 }
399 } while (!(sio_cr & SIO_CR_ARB_DIAG_IDLE) &&
400 (((sio_cr &= SIO_CR_ARB_DIAG) == SIO_CR_ARB_DIAG_TXA)
401 || sio_cr == SIO_CR_ARB_DIAG_TXB
402 || sio_cr == SIO_CR_ARB_DIAG_RXA
403 || sio_cr == SIO_CR_ARB_DIAG_RXB));
404
405 /* Finish reset sequence */
406 writel(0, &port->ip_serial_regs->sscr);
407
408 /* Once RESET is done, reload cached tx_prod and rx_cons values
409 * and set rings to empty by making prod == cons
410 */
411 port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
412 writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
413 port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
414 writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
415
416 /* Disable interrupts for this 16550 */
417 uart = port->ip_uart_regs;
418 writeb(0, &uart->iu_lcr);
419 writeb(0, &uart->iu_ier);
420
421 /* Set the default baud */
422 set_baud(port, port->ip_baud);
423
424 /* Set line control to 8 bits no parity */
425 writeb(UART_LCR_WLEN8 | 0, &uart->iu_lcr);
426 /* UART_LCR_STOP == 1 stop */
427
428 /* Enable the FIFOs */
429 writeb(UART_FCR_ENABLE_FIFO, &uart->iu_fcr);
430 /* then reset 16550 FIFOs */
431 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
432 &uart->iu_fcr);
433
434 /* Clear modem control register */
435 writeb(0, &uart->iu_mcr);
436
437 /* Clear deltas in modem status register */
438 writel(0, &port->ip_serial_regs->shadow);
439
440 /* Only do this once per port pair */
441 if (port->ip_hooks == &hooks_array[0]) {
442 unsigned long ring_pci_addr;
443 uint32_t __iomem *sbbr_l, *sbbr_h;
444
445 sbbr_l = &idd->vma->sbbr_l;
446 sbbr_h = &idd->vma->sbbr_h;
447 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
448 DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
449 __func__, (void *)ring_pci_addr));
450
451 writel((unsigned int)((uint64_t) ring_pci_addr >> 32), sbbr_h);
452 writel((unsigned int)ring_pci_addr | BUF_SIZE_BIT, sbbr_l);
453 }
454
455 /* Set the receive timeout value to 10 msec */
456 writel(SRTR_HZ / 100, &port->ip_serial_regs->srtr);
457
458 /* Set rx threshold, enable DMA */
459 /* Set high water mark at 3/4 of full ring */
460 port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
461
462 /* uart experiences pauses at high baud rate reducing actual
463 * throughput by 10% or so unless we enable high speed polling
464 * XXX when this hardware bug is resolved we should revert to
465 * normal polling speed
466 */
467 port->ip_sscr |= SSCR_HIGH_SPD;
468
469 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
470
471 /* Disable and clear all serial related interrupt bits */
472 port->ip_card->ic_enable &= ~hooks->intr_clear;
473 ioc3_disable(port->ip_is, idd, hooks->intr_clear);
474 ioc3_ack(port->ip_is, idd, hooks->intr_clear);
475 return 0;
476}
477
478/**
479 * enable_intrs - enable interrupts
480 * @port: port to enable
481 * @mask: mask to use
482 */
483static void enable_intrs(struct ioc3_port *port, uint32_t mask)
484{
485 if ((port->ip_card->ic_enable & mask) != mask) {
486 port->ip_card->ic_enable |= mask;
487 ioc3_enable(port->ip_is, port->ip_idd, mask);
488 }
489}
490
491/**
492 * local_open - local open a port
493 * @port: port to open
494 */
495static inline int local_open(struct ioc3_port *port)
496{
497 int spiniter = 0;
498
499 port->ip_flags = INPUT_ENABLE;
500
501 /* Pause the DMA interface if necessary */
502 if (port->ip_sscr & SSCR_DMA_EN) {
503 writel(port->ip_sscr | SSCR_DMA_PAUSE,
504 &port->ip_serial_regs->sscr);
505 while ((readl(&port->ip_serial_regs->sscr)
506 & SSCR_PAUSE_STATE) == 0) {
507 spiniter++;
508 if (spiniter > MAXITER) {
509 NOT_PROGRESS();
510 return -1;
511 }
512 }
513 }
514
515 /* Reset the input fifo. If the uart received chars while the port
516 * was closed and DMA is not enabled, the uart may have a bunch of
517 * chars hanging around in its rx fifo which will not be discarded
518 * by rclr in the upper layer. We must get rid of them here.
519 */
520 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
521 &port->ip_uart_regs->iu_fcr);
522
523 writeb(UART_LCR_WLEN8, &port->ip_uart_regs->iu_lcr);
524 /* UART_LCR_STOP == 1 stop */
525
526 /* Re-enable DMA, set default threshold to intr whenever there is
527 * data available.
528 */
529 port->ip_sscr &= ~SSCR_RX_THRESHOLD;
530 port->ip_sscr |= 1; /* default threshold */
531
532 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
533 * flag if it was set above
534 */
535 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
536 port->ip_tx_lowat = 1;
537 return 0;
538}
539
540/**
541 * set_rx_timeout - Set rx timeout and threshold values.
542 * @port: port to use
543 * @timeout: timeout value in ticks
544 */
545static inline int set_rx_timeout(struct ioc3_port *port, int timeout)
546{
547 int threshold;
548
549 port->ip_rx_timeout = timeout;
550
551 /* Timeout is in ticks. Let's figure out how many chars we
552 * can receive at the current baud rate in that interval
553 * and set the rx threshold to that amount. There are 4 chars
554 * per ring entry, so we'll divide the number of chars that will
555 * arrive in timeout by 4.
556 * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
557 */
558 threshold = timeout * port->ip_baud / 4000;
559 if (threshold == 0)
560 threshold = 1; /* otherwise we'll intr all the time! */
561
562 if ((unsigned)threshold > (unsigned)SSCR_RX_THRESHOLD)
563 return 1;
564
565 port->ip_sscr &= ~SSCR_RX_THRESHOLD;
566 port->ip_sscr |= threshold;
567 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
568
569 /* Now set the rx timeout to the given value
570 * again timeout * SRTR_HZ / HZ
571 */
572 timeout = timeout * SRTR_HZ / 100;
573 if (timeout > SRTR_CNT)
574 timeout = SRTR_CNT;
575 writel(timeout, &port->ip_serial_regs->srtr);
576 return 0;
577}
578
579/**
580 * config_port - config the hardware
581 * @port: port to config
582 * @baud: baud rate for the port
583 * @byte_size: data size
584 * @stop_bits: number of stop bits
585 * @parenb: parity enable ?
586 * @parodd: odd parity ?
587 */
588static inline int
589config_port(struct ioc3_port *port,
590 int baud, int byte_size, int stop_bits, int parenb, int parodd)
591{
592 char lcr, sizebits;
593 int spiniter = 0;
594
595 DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
596 "parodd %d\n",
597 __func__, ((struct uart_port *)port->ip_port)->line,
598 baud, byte_size, stop_bits, parenb, parodd));
599
600 if (set_baud(port, baud))
601 return 1;
602
603 switch (byte_size) {
604 case 5:
605 sizebits = UART_LCR_WLEN5;
606 break;
607 case 6:
608 sizebits = UART_LCR_WLEN6;
609 break;
610 case 7:
611 sizebits = UART_LCR_WLEN7;
612 break;
613 case 8:
614 sizebits = UART_LCR_WLEN8;
615 break;
616 default:
617 return 1;
618 }
619
620 /* Pause the DMA interface if necessary */
621 if (port->ip_sscr & SSCR_DMA_EN) {
622 writel(port->ip_sscr | SSCR_DMA_PAUSE,
623 &port->ip_serial_regs->sscr);
624 while ((readl(&port->ip_serial_regs->sscr)
625 & SSCR_PAUSE_STATE) == 0) {
626 spiniter++;
627 if (spiniter > MAXITER)
628 return -1;
629 }
630 }
631
632 /* Clear relevant fields in lcr */
633 lcr = readb(&port->ip_uart_regs->iu_lcr);
634 lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
635 UART_LCR_PARITY | LCR_MASK_STOP_BITS);
636
637 /* Set byte size in lcr */
638 lcr |= sizebits;
639
640 /* Set parity */
641 if (parenb) {
642 lcr |= UART_LCR_PARITY;
643 if (!parodd)
644 lcr |= UART_LCR_EPAR;
645 }
646
647 /* Set stop bits */
648 if (stop_bits)
649 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
650
651 writeb(lcr, &port->ip_uart_regs->iu_lcr);
652
653 /* Re-enable the DMA interface if necessary */
654 if (port->ip_sscr & SSCR_DMA_EN) {
655 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
656 }
657 port->ip_baud = baud;
658
659 /* When we get within this number of ring entries of filling the
660 * entire ring on tx, place an EXPLICIT intr to generate a lowat
661 * notification when output has drained.
662 */
663 port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
664 if (port->ip_tx_lowat == 0)
665 port->ip_tx_lowat = 1;
666
667 set_rx_timeout(port, 2);
668 return 0;
669}
670
671/**
672 * do_write - Write bytes to the port. Returns the number of bytes
673 * actually written. Called from transmit_chars
674 * @port: port to use
675 * @buf: the stuff to write
676 * @len: how many bytes in 'buf'
677 */
678static inline int do_write(struct ioc3_port *port, char *buf, int len)
679{
680 int prod_ptr, cons_ptr, total = 0;
681 struct ring *outring;
682 struct ring_entry *entry;
683 struct port_hooks *hooks = port->ip_hooks;
684
685 BUG_ON(!(len >= 0));
686
687 prod_ptr = port->ip_tx_prod;
688 cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
689 outring = port->ip_outring;
690
691 /* Maintain a 1-entry red-zone. The ring buffer is full when
692 * (cons - prod) % ring_size is 1. Rather than do this subtraction
693 * in the body of the loop, I'll do it now.
694 */
695 cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
696
697 /* Stuff the bytes into the output */
698 while ((prod_ptr != cons_ptr) && (len > 0)) {
699 int xx;
700
701 /* Get 4 bytes (one ring entry) at a time */
702 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
703
704 /* Invalidate all entries */
705 entry->ring_allsc = 0;
706
707 /* Copy in some bytes */
708 for (xx = 0; (xx < 4) && (len > 0); xx++) {
709 entry->ring_data[xx] = *buf++;
710 entry->ring_sc[xx] = TXCB_VALID;
711 len--;
712 total++;
713 }
714
715 /* If we are within some small threshold of filling up the
716 * entire ring buffer, we must place an EXPLICIT intr here
717 * to generate a lowat interrupt in case we subsequently
718 * really do fill up the ring and the caller goes to sleep.
719 * No need to place more than one though.
720 */
721 if (!(port->ip_flags & LOWAT_WRITTEN) &&
722 ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
723 <= port->ip_tx_lowat * (int)sizeof(struct ring_entry)) {
724 port->ip_flags |= LOWAT_WRITTEN;
725 entry->ring_sc[0] |= TXCB_INT_WHEN_DONE;
726 }
727
728 /* Go on to next entry */
729 prod_ptr += sizeof(struct ring_entry);
730 prod_ptr &= PROD_CONS_MASK;
731 }
732
733 /* If we sent something, start DMA if necessary */
734 if (total > 0 && !(port->ip_sscr & SSCR_DMA_EN)) {
735 port->ip_sscr |= SSCR_DMA_EN;
736 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
737 }
738
739 /* Store the new producer pointer. If tx is disabled, we stuff the
740 * data into the ring buffer, but we don't actually start tx.
741 */
742 if (!uart_tx_stopped(port->ip_port)) {
743 writel(prod_ptr, &port->ip_serial_regs->stpir);
744
745 /* If we are now transmitting, enable tx_mt interrupt so we
746 * can disable DMA if necessary when the tx finishes.
747 */
748 if (total > 0)
749 enable_intrs(port, hooks->intr_tx_mt);
750 }
751 port->ip_tx_prod = prod_ptr;
752
753 return total;
754}
755
756/**
757 * disable_intrs - disable interrupts
758 * @port: port to enable
759 * @mask: mask to use
760 */
761static inline void disable_intrs(struct ioc3_port *port, uint32_t mask)
762{
763 if (port->ip_card->ic_enable & mask) {
764 ioc3_disable(port->ip_is, port->ip_idd, mask);
765 port->ip_card->ic_enable &= ~mask;
766 }
767}
768
769/**
770 * set_notification - Modify event notification
771 * @port: port to use
772 * @mask: events mask
773 * @set_on: set ?
774 */
775static int set_notification(struct ioc3_port *port, int mask, int set_on)
776{
777 struct port_hooks *hooks = port->ip_hooks;
778 uint32_t intrbits, sscrbits;
779
780 BUG_ON(!mask);
781
782 intrbits = sscrbits = 0;
783
784 if (mask & N_DATA_READY)
785 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
786 if (mask & N_OUTPUT_LOWAT)
787 intrbits |= hooks->intr_tx_explicit;
788 if (mask & N_DDCD) {
789 intrbits |= hooks->intr_delta_dcd;
790 sscrbits |= SSCR_RX_RING_DCD;
791 }
792 if (mask & N_DCTS)
793 intrbits |= hooks->intr_delta_cts;
794
795 if (set_on) {
796 enable_intrs(port, intrbits);
797 port->ip_notify |= mask;
798 port->ip_sscr |= sscrbits;
799 } else {
800 disable_intrs(port, intrbits);
801 port->ip_notify &= ~mask;
802 port->ip_sscr &= ~sscrbits;
803 }
804
805 /* We require DMA if either DATA_READY or DDCD notification is
806 * currently requested. If neither of these is requested and
807 * there is currently no tx in progress, DMA may be disabled.
808 */
809 if (port->ip_notify & (N_DATA_READY | N_DDCD))
810 port->ip_sscr |= SSCR_DMA_EN;
811 else if (!(port->ip_card->ic_enable & hooks->intr_tx_mt))
812 port->ip_sscr &= ~SSCR_DMA_EN;
813
814 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
815 return 0;
816}
817
818/**
819 * set_mcr - set the master control reg
820 * @the_port: port to use
821 * @mask1: mcr mask
822 * @mask2: shadow mask
823 */
824static inline int set_mcr(struct uart_port *the_port,
825 int mask1, int mask2)
826{
827 struct ioc3_port *port = get_ioc3_port(the_port);
828 uint32_t shadow;
829 int spiniter = 0;
830 char mcr;
831
832 if (!port)
833 return -1;
834
835 /* Pause the DMA interface if necessary */
836 if (port->ip_sscr & SSCR_DMA_EN) {
837 writel(port->ip_sscr | SSCR_DMA_PAUSE,
838 &port->ip_serial_regs->sscr);
839 while ((readl(&port->ip_serial_regs->sscr)
840 & SSCR_PAUSE_STATE) == 0) {
841 spiniter++;
842 if (spiniter > MAXITER)
843 return -1;
844 }
845 }
846 shadow = readl(&port->ip_serial_regs->shadow);
847 mcr = (shadow & 0xff000000) >> 24;
848
849 /* Set new value */
850 mcr |= mask1;
851 shadow |= mask2;
852 writeb(mcr, &port->ip_uart_regs->iu_mcr);
853 writel(shadow, &port->ip_serial_regs->shadow);
854
855 /* Re-enable the DMA interface if necessary */
856 if (port->ip_sscr & SSCR_DMA_EN) {
857 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
858 }
859 return 0;
860}
861
862/**
863 * ioc3_set_proto - set the protocol for the port
864 * @port: port to use
865 * @proto: protocol to use
866 */
867static int ioc3_set_proto(struct ioc3_port *port, int proto)
868{
869 struct port_hooks *hooks = port->ip_hooks;
870
871 switch (proto) {
872 default:
873 case PROTO_RS232:
874 /* Clear the appropriate GIO pin */
875 DPRINT_CONFIG(("%s: rs232\n", __func__));
876 writel(0, (&port->ip_idd->vma->gppr[0]
877 + hooks->rs422_select_pin));
878 break;
879
880 case PROTO_RS422:
881 /* Set the appropriate GIO pin */
882 DPRINT_CONFIG(("%s: rs422\n", __func__));
883 writel(1, (&port->ip_idd->vma->gppr[0]
884 + hooks->rs422_select_pin));
885 break;
886 }
887 return 0;
888}
889
890/**
891 * transmit_chars - upper level write, called with the_port->lock
892 * @the_port: port to write
893 */
894static void transmit_chars(struct uart_port *the_port)
895{
896 int xmit_count, tail, head;
897 int result;
898 char *start;
899 struct tty_struct *tty;
900 struct ioc3_port *port = get_ioc3_port(the_port);
901 struct uart_state *state;
902
903 if (!the_port)
904 return;
905 if (!port)
906 return;
907
908 state = the_port->state;
909 tty = state->port.tty;
910
911 if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
912 /* Nothing to do or hw stopped */
913 set_notification(port, N_ALL_OUTPUT, 0);
914 return;
915 }
916
917 head = state->xmit.head;
918 tail = state->xmit.tail;
919 start = (char *)&state->xmit.buf[tail];
920
921 /* write out all the data or until the end of the buffer */
922 xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
923 if (xmit_count > 0) {
924 result = do_write(port, start, xmit_count);
925 if (result > 0) {
926 /* booking */
927 xmit_count -= result;
928 the_port->icount.tx += result;
929 /* advance the pointers */
930 tail += result;
931 tail &= UART_XMIT_SIZE - 1;
932 state->xmit.tail = tail;
933 start = (char *)&state->xmit.buf[tail];
934 }
935 }
936 if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
937 uart_write_wakeup(the_port);
938
939 if (uart_circ_empty(&state->xmit)) {
940 set_notification(port, N_OUTPUT_LOWAT, 0);
941 } else {
942 set_notification(port, N_OUTPUT_LOWAT, 1);
943 }
944}
945
946/**
947 * ioc3_change_speed - change the speed of the port
948 * @the_port: port to change
949 * @new_termios: new termios settings
950 * @old_termios: old termios settings
951 */
952static void
953ioc3_change_speed(struct uart_port *the_port,
954 struct ktermios *new_termios, struct ktermios *old_termios)
955{
956 struct ioc3_port *port = get_ioc3_port(the_port);
957 unsigned int cflag, iflag;
958 int baud;
959 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
960 struct uart_state *state = the_port->state;
961
962 cflag = new_termios->c_cflag;
963 iflag = new_termios->c_iflag;
964
965 switch (cflag & CSIZE) {
966 case CS5:
967 new_data = 5;
968 break;
969 case CS6:
970 new_data = 6;
971 break;
972 case CS7:
973 new_data = 7;
974 break;
975 case CS8:
976 new_data = 8;
977 break;
978 default:
979 /* cuz we always need a default ... */
980 new_data = 5;
981 break;
982 }
983 if (cflag & CSTOPB) {
984 new_stop = 1;
985 }
986 if (cflag & PARENB) {
987 new_parity_enable = 1;
988 if (cflag & PARODD)
989 new_parity = 1;
990 }
991 baud = uart_get_baud_rate(the_port, new_termios, old_termios,
992 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
993 DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__, baud,
994 the_port->line));
995
996 if (!the_port->fifosize)
997 the_port->fifosize = FIFO_SIZE;
998 uart_update_timeout(the_port, cflag, baud);
999
1000 the_port->ignore_status_mask = N_ALL_INPUT;
1001
1002 state->port.tty->low_latency = 1;
1003
1004 if (iflag & IGNPAR)
1005 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1006 | N_FRAMING_ERROR);
1007 if (iflag & IGNBRK) {
1008 the_port->ignore_status_mask &= ~N_BREAK;
1009 if (iflag & IGNPAR)
1010 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1011 }
1012 if (!(cflag & CREAD)) {
1013 /* ignore everything */
1014 the_port->ignore_status_mask &= ~N_DATA_READY;
1015 }
1016
1017 if (cflag & CRTSCTS) {
1018 /* enable hardware flow control */
1019 port->ip_sscr |= SSCR_HFC_EN;
1020 }
1021 else {
1022 /* disable hardware flow control */
1023 port->ip_sscr &= ~SSCR_HFC_EN;
1024 }
1025 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1026
1027 /* Set the configuration and proper notification call */
1028 DPRINT_CONFIG(("%s : port 0x%p line %d cflag 0%o "
1029 "config_port(baud %d data %d stop %d penable %d "
1030 " parity %d), notification 0x%x\n",
1031 __func__, (void *)port, the_port->line, cflag, baud,
1032 new_data, new_stop, new_parity_enable, new_parity,
1033 the_port->ignore_status_mask));
1034
1035 if ((config_port(port, baud, /* baud */
1036 new_data, /* byte size */
1037 new_stop, /* stop bits */
1038 new_parity_enable, /* set parity */
1039 new_parity)) >= 0) { /* parity 1==odd */
1040 set_notification(port, the_port->ignore_status_mask, 1);
1041 }
1042}
1043
1044/**
1045 * ic3_startup_local - Start up the serial port - returns >= 0 if no errors
1046 * @the_port: Port to operate on
1047 */
1048static inline int ic3_startup_local(struct uart_port *the_port)
1049{
1050 struct ioc3_port *port;
1051
1052 if (!the_port) {
1053 NOT_PROGRESS();
1054 return -1;
1055 }
1056
1057 port = get_ioc3_port(the_port);
1058 if (!port) {
1059 NOT_PROGRESS();
1060 return -1;
1061 }
1062
1063 local_open(port);
1064
1065 /* set the protocol */
1066 ioc3_set_proto(port, IS_RS232(the_port->line) ? PROTO_RS232 :
1067 PROTO_RS422);
1068 return 0;
1069}
1070
1071/*
1072 * ioc3_cb_output_lowat - called when the output low water mark is hit
1073 * @port: port to output
1074 */
1075static void ioc3_cb_output_lowat(struct ioc3_port *port)
1076{
1077 unsigned long pflags;
1078
1079 /* the_port->lock is set on the call here */
1080 if (port->ip_port) {
1081 spin_lock_irqsave(&port->ip_port->lock, pflags);
1082 transmit_chars(port->ip_port);
1083 spin_unlock_irqrestore(&port->ip_port->lock, pflags);
1084 }
1085}
1086
1087/*
1088 * ioc3_cb_post_ncs - called for some basic errors
1089 * @port: port to use
1090 * @ncs: event
1091 */
1092static void ioc3_cb_post_ncs(struct uart_port *the_port, int ncs)
1093{
1094 struct uart_icount *icount;
1095
1096 icount = &the_port->icount;
1097
1098 if (ncs & NCS_BREAK)
1099 icount->brk++;
1100 if (ncs & NCS_FRAMING)
1101 icount->frame++;
1102 if (ncs & NCS_OVERRUN)
1103 icount->overrun++;
1104 if (ncs & NCS_PARITY)
1105 icount->parity++;
1106}
1107
1108/**
1109 * do_read - Read in bytes from the port. Return the number of bytes
1110 * actually read.
1111 * @the_port: port to use
1112 * @buf: place to put the stuff we read
1113 * @len: how big 'buf' is
1114 */
1115
1116static inline int do_read(struct uart_port *the_port, char *buf, int len)
1117{
1118 int prod_ptr, cons_ptr, total;
1119 struct ioc3_port *port = get_ioc3_port(the_port);
1120 struct ring *inring;
1121 struct ring_entry *entry;
1122 struct port_hooks *hooks = port->ip_hooks;
1123 int byte_num;
1124 char *sc;
1125 int loop_counter;
1126
1127 BUG_ON(!(len >= 0));
1128 BUG_ON(!port);
1129
1130 /* There is a nasty timing issue in the IOC3. When the rx_timer
1131 * expires or the rx_high condition arises, we take an interrupt.
1132 * At some point while servicing the interrupt, we read bytes from
1133 * the ring buffer and re-arm the rx_timer. However the rx_timer is
1134 * not started until the first byte is received *after* it is armed,
1135 * and any bytes pending in the rx construction buffers are not drained
1136 * to memory until either there are 4 bytes available or the rx_timer
1137 * expires. This leads to a potential situation where data is left
1138 * in the construction buffers forever - 1 to 3 bytes were received
1139 * after the interrupt was generated but before the rx_timer was
1140 * re-armed. At that point as long as no subsequent bytes are received
1141 * the timer will never be started and the bytes will remain in the
1142 * construction buffer forever. The solution is to execute a DRAIN
1143 * command after rearming the timer. This way any bytes received before
1144 * the DRAIN will be drained to memory, and any bytes received after
1145 * the DRAIN will start the TIMER and be drained when it expires.
1146 * Luckily, this only needs to be done when the DMA buffer is empty
1147 * since there is no requirement that this function return all
1148 * available data as long as it returns some.
1149 */
1150 /* Re-arm the timer */
1151
1152 writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
1153
1154 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1155 cons_ptr = port->ip_rx_cons;
1156
1157 if (prod_ptr == cons_ptr) {
1158 int reset_dma = 0;
1159
1160 /* Input buffer appears empty, do a flush. */
1161
1162 /* DMA must be enabled for this to work. */
1163 if (!(port->ip_sscr & SSCR_DMA_EN)) {
1164 port->ip_sscr |= SSCR_DMA_EN;
1165 reset_dma = 1;
1166 }
1167
1168 /* Potential race condition: we must reload the srpir after
1169 * issuing the drain command, otherwise we could think the rx
1170 * buffer is empty, then take a very long interrupt, and when
1171 * we come back it's full and we wait forever for the drain to
1172 * complete.
1173 */
1174 writel(port->ip_sscr | SSCR_RX_DRAIN,
1175 &port->ip_serial_regs->sscr);
1176 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1177
1178 /* We must not wait for the DRAIN to complete unless there are
1179 * at least 8 bytes (2 ring entries) available to receive the
1180 * data otherwise the DRAIN will never complete and we'll
1181 * deadlock here.
1182 * In fact, to make things easier, I'll just ignore the flush if
1183 * there is any data at all now available.
1184 */
1185 if (prod_ptr == cons_ptr) {
1186 loop_counter = 0;
1187 while (readl(&port->ip_serial_regs->sscr) &
1188 SSCR_RX_DRAIN) {
1189 loop_counter++;
1190 if (loop_counter > MAXITER)
1191 return -1;
1192 }
1193
1194 /* SIGH. We have to reload the prod_ptr *again* since
1195 * the drain may have caused it to change
1196 */
1197 prod_ptr = readl(&port->ip_serial_regs->srpir)
1198 & PROD_CONS_MASK;
1199 }
1200 if (reset_dma) {
1201 port->ip_sscr &= ~SSCR_DMA_EN;
1202 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1203 }
1204 }
1205 inring = port->ip_inring;
1206 port->ip_flags &= ~READ_ABORTED;
1207
1208 total = 0;
1209 loop_counter = 0xfffff; /* to avoid hangs */
1210
1211 /* Grab bytes from the hardware */
1212 while ((prod_ptr != cons_ptr) && (len > 0)) {
1213 entry = (struct ring_entry *)((caddr_t) inring + cons_ptr);
1214
1215 if (loop_counter-- <= 0) {
1216 printk(KERN_WARNING "IOC3 serial: "
1217 "possible hang condition/"
1218 "port stuck on read (line %d).\n",
1219 the_port->line);
1220 break;
1221 }
1222
1223 /* According to the producer pointer, this ring entry
1224 * must contain some data. But if the PIO happened faster
1225 * than the DMA, the data may not be available yet, so let's
1226 * wait until it arrives.
1227 */
1228 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1229 /* Indicate the read is aborted so we don't disable
1230 * the interrupt thinking that the consumer is
1231 * congested.
1232 */
1233 port->ip_flags |= READ_ABORTED;
1234 len = 0;
1235 break;
1236 }
1237
1238 /* Load the bytes/status out of the ring entry */
1239 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
1240 sc = &(entry->ring_sc[byte_num]);
1241
1242 /* Check for change in modem state or overrun */
1243 if ((*sc & RXSB_MODEM_VALID)
1244 && (port->ip_notify & N_DDCD)) {
1245 /* Notify upper layer if DCD dropped */
1246 if ((port->ip_flags & DCD_ON)
1247 && !(*sc & RXSB_DCD)) {
1248 /* If we have already copied some data,
1249 * return it. We'll pick up the carrier
1250 * drop on the next pass. That way we
1251 * don't throw away the data that has
1252 * already been copied back to
1253 * the caller's buffer.
1254 */
1255 if (total > 0) {
1256 len = 0;
1257 break;
1258 }
1259 port->ip_flags &= ~DCD_ON;
1260
1261 /* Turn off this notification so the
1262 * carrier drop protocol won't see it
1263 * again when it does a read.
1264 */
1265 *sc &= ~RXSB_MODEM_VALID;
1266
1267 /* To keep things consistent, we need
1268 * to update the consumer pointer so
1269 * the next reader won't come in and
1270 * try to read the same ring entries
1271 * again. This must be done here before
1272 * the dcd change.
1273 */
1274
1275 if ((entry->ring_allsc & RING_ANY_VALID)
1276 == 0) {
1277 cons_ptr += (int)sizeof
1278 (struct ring_entry);
1279 cons_ptr &= PROD_CONS_MASK;
1280 }
1281 writel(cons_ptr,
1282 &port->ip_serial_regs->srcir);
1283 port->ip_rx_cons = cons_ptr;
1284
1285 /* Notify upper layer of carrier drop */
1286 if ((port->ip_notify & N_DDCD)
1287 && port->ip_port) {
1288 uart_handle_dcd_change
1289 (port->ip_port, 0);
1290 wake_up_interruptible
1291 (&the_port->state->
1292 port.delta_msr_wait);
1293 }
1294
1295 /* If we had any data to return, we
1296 * would have returned it above.
1297 */
1298 return 0;
1299 }
1300 }
1301 if (*sc & RXSB_MODEM_VALID) {
1302 /* Notify that an input overrun occurred */
1303 if ((*sc & RXSB_OVERRUN)
1304 && (port->ip_notify & N_OVERRUN_ERROR)) {
1305 ioc3_cb_post_ncs(the_port, NCS_OVERRUN);
1306 }
1307 /* Don't look at this byte again */
1308 *sc &= ~RXSB_MODEM_VALID;
1309 }
1310
1311 /* Check for valid data or RX errors */
1312 if ((*sc & RXSB_DATA_VALID) &&
1313 ((*sc & (RXSB_PAR_ERR
1314 | RXSB_FRAME_ERR | RXSB_BREAK))
1315 && (port->ip_notify & (N_PARITY_ERROR
1316 | N_FRAMING_ERROR
1317 | N_BREAK)))) {
1318 /* There is an error condition on the next byte.
1319 * If we have already transferred some bytes,
1320 * we'll stop here. Otherwise if this is the
1321 * first byte to be read, we'll just transfer
1322 * it alone after notifying the
1323 * upper layer of its status.
1324 */
1325 if (total > 0) {
1326 len = 0;
1327 break;
1328 } else {
1329 if ((*sc & RXSB_PAR_ERR) &&
1330 (port->
1331 ip_notify & N_PARITY_ERROR)) {
1332 ioc3_cb_post_ncs(the_port,
1333 NCS_PARITY);
1334 }
1335 if ((*sc & RXSB_FRAME_ERR) &&
1336 (port->
1337 ip_notify & N_FRAMING_ERROR)) {
1338 ioc3_cb_post_ncs(the_port,
1339 NCS_FRAMING);
1340 }
1341 if ((*sc & RXSB_BREAK)
1342 && (port->ip_notify & N_BREAK)) {
1343 ioc3_cb_post_ncs
1344 (the_port, NCS_BREAK);
1345 }
1346 len = 1;
1347 }
1348 }
1349 if (*sc & RXSB_DATA_VALID) {
1350 *sc &= ~RXSB_DATA_VALID;
1351 *buf = entry->ring_data[byte_num];
1352 buf++;
1353 len--;
1354 total++;
1355 }
1356 }
1357
1358 /* If we used up this entry entirely, go on to the next one,
1359 * otherwise we must have run out of buffer space, so
1360 * leave the consumer pointer here for the next read in case
1361 * there are still unread bytes in this entry.
1362 */
1363 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1364 cons_ptr += (int)sizeof(struct ring_entry);
1365 cons_ptr &= PROD_CONS_MASK;
1366 }
1367 }
1368
1369 /* Update consumer pointer and re-arm rx timer interrupt */
1370 writel(cons_ptr, &port->ip_serial_regs->srcir);
1371 port->ip_rx_cons = cons_ptr;
1372
1373 /* If we have now dipped below the rx high water mark and we have
1374 * rx_high interrupt turned off, we can now turn it back on again.
1375 */
1376 if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
1377 & PROD_CONS_MASK) <
1378 ((port->
1379 ip_sscr &
1380 SSCR_RX_THRESHOLD)
1381 << PROD_CONS_PTR_OFF))) {
1382 port->ip_flags &= ~INPUT_HIGH;
1383 enable_intrs(port, hooks->intr_rx_high);
1384 }
1385 return total;
1386}
1387
1388/**
1389 * receive_chars - upper level read.
1390 * @the_port: port to read from
1391 */
1392static int receive_chars(struct uart_port *the_port)
1393{
1394 struct tty_struct *tty;
1395 unsigned char ch[MAX_CHARS];
1396 int read_count = 0, read_room, flip = 0;
1397 struct uart_state *state = the_port->state;
1398 struct ioc3_port *port = get_ioc3_port(the_port);
1399 unsigned long pflags;
1400
1401 /* Make sure all the pointers are "good" ones */
1402 if (!state)
1403 return 0;
1404 if (!state->port.tty)
1405 return 0;
1406
1407 if (!(port->ip_flags & INPUT_ENABLE))
1408 return 0;
1409
1410 spin_lock_irqsave(&the_port->lock, pflags);
1411 tty = state->port.tty;
1412
1413 read_count = do_read(the_port, ch, MAX_CHARS);
1414 if (read_count > 0) {
1415 flip = 1;
1416 read_room = tty_insert_flip_string(tty, ch, read_count);
1417 the_port->icount.rx += read_count;
1418 }
1419 spin_unlock_irqrestore(&the_port->lock, pflags);
1420
1421 if (flip)
1422 tty_flip_buffer_push(tty);
1423
1424 return read_count;
1425}
1426
1427/**
1428 * ioc3uart_intr_one - lowest level (per port) interrupt handler.
1429 * @is : submodule
1430 * @idd: driver data
1431 * @pending: interrupts to handle
1432 */
1433
1434static int inline
1435ioc3uart_intr_one(struct ioc3_submodule *is,
1436 struct ioc3_driver_data *idd,
1437 unsigned int pending)
1438{
1439 int port_num = GET_PORT_FROM_SIO_IR(pending);
1440 struct port_hooks *hooks;
1441 unsigned int rx_high_rd_aborted = 0;
1442 unsigned long flags;
1443 struct uart_port *the_port;
1444 struct ioc3_port *port;
1445 int loop_counter;
1446 struct ioc3_card *card_ptr;
1447 unsigned int sio_ir;
1448
1449 card_ptr = idd->data[is->id];
1450 port = card_ptr->ic_port[port_num].icp_port;
1451 hooks = port->ip_hooks;
1452
1453 /* Possible race condition here: The tx_mt interrupt bit may be
1454 * cleared without the intervention of the interrupt handler,
1455 * e.g. by a write. If the top level interrupt handler reads a
1456 * tx_mt, then some other processor does a write, starting up
1457 * output, then we come in here, see the tx_mt and stop DMA, the
1458 * output started by the other processor will hang. Thus we can
1459 * only rely on tx_mt being legitimate if it is read while the
1460 * port lock is held. Therefore this bit must be ignored in the
1461 * passed in interrupt mask which was read by the top level
1462 * interrupt handler since the port lock was not held at the time
1463 * it was read. We can only rely on this bit being accurate if it
1464 * is read while the port lock is held. So we'll clear it for now,
1465 * and reload it later once we have the port lock.
1466 */
1467
1468 sio_ir = pending & ~(hooks->intr_tx_mt);
1469 spin_lock_irqsave(&port->ip_lock, flags);
1470
1471 loop_counter = MAXITER; /* to avoid hangs */
1472
1473 do {
1474 uint32_t shadow;
1475
1476 if (loop_counter-- <= 0) {
1477 printk(KERN_WARNING "IOC3 serial: "
1478 "possible hang condition/"
1479 "port stuck on interrupt (line %d).\n",
1480 ((struct uart_port *)port->ip_port)->line);
1481 break;
1482 }
1483 /* Handle a DCD change */
1484 if (sio_ir & hooks->intr_delta_dcd) {
1485 ioc3_ack(is, idd, hooks->intr_delta_dcd);
1486 shadow = readl(&port->ip_serial_regs->shadow);
1487
1488 if ((port->ip_notify & N_DDCD)
1489 && (shadow & SHADOW_DCD)
1490 && (port->ip_port)) {
1491 the_port = port->ip_port;
1492 uart_handle_dcd_change(the_port,
1493 shadow & SHADOW_DCD);
1494 wake_up_interruptible
1495 (&the_port->state->port.delta_msr_wait);
1496 } else if ((port->ip_notify & N_DDCD)
1497 && !(shadow & SHADOW_DCD)) {
1498 /* Flag delta DCD/no DCD */
1499 uart_handle_dcd_change(port->ip_port,
1500 shadow & SHADOW_DCD);
1501 port->ip_flags |= DCD_ON;
1502 }
1503 }
1504
1505 /* Handle a CTS change */
1506 if (sio_ir & hooks->intr_delta_cts) {
1507 ioc3_ack(is, idd, hooks->intr_delta_cts);
1508 shadow = readl(&port->ip_serial_regs->shadow);
1509
1510 if ((port->ip_notify & N_DCTS) && (port->ip_port)) {
1511 the_port = port->ip_port;
1512 uart_handle_cts_change(the_port, shadow
1513 & SHADOW_CTS);
1514 wake_up_interruptible
1515 (&the_port->state->port.delta_msr_wait);
1516 }
1517 }
1518
1519 /* rx timeout interrupt. Must be some data available. Put this
1520 * before the check for rx_high since servicing this condition
1521 * may cause that condition to clear.
1522 */
1523 if (sio_ir & hooks->intr_rx_timer) {
1524 ioc3_ack(is, idd, hooks->intr_rx_timer);
1525 if ((port->ip_notify & N_DATA_READY)
1526 && (port->ip_port)) {
1527 receive_chars(port->ip_port);
1528 }
1529 }
1530
1531 /* rx high interrupt. Must be after rx_timer. */
1532 else if (sio_ir & hooks->intr_rx_high) {
1533 /* Data available, notify upper layer */
1534 if ((port->ip_notify & N_DATA_READY) && port->ip_port) {
1535 receive_chars(port->ip_port);
1536 }
1537
1538 /* We can't ACK this interrupt. If receive_chars didn't
1539 * cause the condition to clear, we'll have to disable
1540 * the interrupt until the data is drained.
1541 * If the read was aborted, don't disable the interrupt
1542 * as this may cause us to hang indefinitely. An
1543 * aborted read generally means that this interrupt
1544 * hasn't been delivered to the cpu yet anyway, even
1545 * though we see it as asserted when we read the sio_ir.
1546 */
1547 if ((sio_ir = PENDING(card_ptr, idd))
1548 & hooks->intr_rx_high) {
1549 if (port->ip_flags & READ_ABORTED) {
1550 rx_high_rd_aborted++;
1551 }
1552 else {
1553 card_ptr->ic_enable &= ~hooks->intr_rx_high;
1554 port->ip_flags |= INPUT_HIGH;
1555 }
1556 }
1557 }
1558
1559 /* We got a low water interrupt: notify upper layer to
1560 * send more data. Must come before tx_mt since servicing
1561 * this condition may cause that condition to clear.
1562 */
1563 if (sio_ir & hooks->intr_tx_explicit) {
1564 port->ip_flags &= ~LOWAT_WRITTEN;
1565 ioc3_ack(is, idd, hooks->intr_tx_explicit);
1566 if (port->ip_notify & N_OUTPUT_LOWAT)
1567 ioc3_cb_output_lowat(port);
1568 }
1569
1570 /* Handle tx_mt. Must come after tx_explicit. */
1571 else if (sio_ir & hooks->intr_tx_mt) {
1572 /* If we are expecting a lowat notification
1573 * and we get to this point it probably means that for
1574 * some reason the tx_explicit didn't work as expected
1575 * (that can legitimately happen if the output buffer is
1576 * filled up in just the right way).
1577 * So send the notification now.
1578 */
1579 if (port->ip_notify & N_OUTPUT_LOWAT) {
1580 ioc3_cb_output_lowat(port);
1581
1582 /* We need to reload the sio_ir since the lowat
1583 * call may have caused another write to occur,
1584 * clearing the tx_mt condition.
1585 */
1586 sio_ir = PENDING(card_ptr, idd);
1587 }
1588
1589 /* If the tx_mt condition still persists even after the
1590 * lowat call, we've got some work to do.
1591 */
1592 if (sio_ir & hooks->intr_tx_mt) {
1593 /* If we are not currently expecting DMA input,
1594 * and the transmitter has just gone idle,
1595 * there is no longer any reason for DMA, so
1596 * disable it.
1597 */
1598 if (!(port->ip_notify
1599 & (N_DATA_READY | N_DDCD))) {
1600 BUG_ON(!(port->ip_sscr
1601 & SSCR_DMA_EN));
1602 port->ip_sscr &= ~SSCR_DMA_EN;
1603 writel(port->ip_sscr,
1604 &port->ip_serial_regs->sscr);
1605 }
1606 /* Prevent infinite tx_mt interrupt */
1607 card_ptr->ic_enable &= ~hooks->intr_tx_mt;
1608 }
1609 }
1610 sio_ir = PENDING(card_ptr, idd);
1611
1612 /* if the read was aborted and only hooks->intr_rx_high,
1613 * clear hooks->intr_rx_high, so we do not loop forever.
1614 */
1615
1616 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
1617 sio_ir &= ~hooks->intr_rx_high;
1618 }
1619 } while (sio_ir & hooks->intr_all);
1620
1621 spin_unlock_irqrestore(&port->ip_lock, flags);
1622 ioc3_enable(is, idd, card_ptr->ic_enable);
1623 return 0;
1624}
1625
1626/**
1627 * ioc3uart_intr - field all serial interrupts
1628 * @is : submodule
1629 * @idd: driver data
1630 * @pending: interrupts to handle
1631 *
1632 */
1633
1634static int ioc3uart_intr(struct ioc3_submodule *is,
1635 struct ioc3_driver_data *idd,
1636 unsigned int pending)
1637{
1638 int ret = 0;
1639
1640 /*
1641 * The upper level interrupt handler sends interrupts for both ports
1642 * here. So we need to call for each port with its interrupts.
1643 */
1644
1645 if (pending & SIO_IR_SA)
1646 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SA);
1647 if (pending & SIO_IR_SB)
1648 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SB);
1649
1650 return ret;
1651}
1652
1653/**
1654 * ic3_type
1655 * @port: Port to operate with (we ignore since we only have one port)
1656 *
1657 */
1658static const char *ic3_type(struct uart_port *the_port)
1659{
1660 if (IS_RS232(the_port->line))
1661 return "SGI IOC3 Serial [rs232]";
1662 else
1663 return "SGI IOC3 Serial [rs422]";
1664}
1665
1666/**
1667 * ic3_tx_empty - Is the transmitter empty?
1668 * @port: Port to operate on
1669 *
1670 */
1671static unsigned int ic3_tx_empty(struct uart_port *the_port)
1672{
1673 unsigned int ret = 0;
1674 struct ioc3_port *port = get_ioc3_port(the_port);
1675
1676 if (readl(&port->ip_serial_regs->shadow) & SHADOW_TEMT)
1677 ret = TIOCSER_TEMT;
1678 return ret;
1679}
1680
1681/**
1682 * ic3_stop_tx - stop the transmitter
1683 * @port: Port to operate on
1684 *
1685 */
1686static void ic3_stop_tx(struct uart_port *the_port)
1687{
1688 struct ioc3_port *port = get_ioc3_port(the_port);
1689
1690 if (port)
1691 set_notification(port, N_OUTPUT_LOWAT, 0);
1692}
1693
1694/**
1695 * ic3_stop_rx - stop the receiver
1696 * @port: Port to operate on
1697 *
1698 */
1699static void ic3_stop_rx(struct uart_port *the_port)
1700{
1701 struct ioc3_port *port = get_ioc3_port(the_port);
1702
1703 if (port)
1704 port->ip_flags &= ~INPUT_ENABLE;
1705}
1706
1707/**
1708 * null_void_function
1709 * @port: Port to operate on
1710 *
1711 */
1712static void null_void_function(struct uart_port *the_port)
1713{
1714}
1715
1716/**
1717 * ic3_shutdown - shut down the port - free irq and disable
1718 * @port: port to shut down
1719 *
1720 */
1721static void ic3_shutdown(struct uart_port *the_port)
1722{
1723 unsigned long port_flags;
1724 struct ioc3_port *port;
1725 struct uart_state *state;
1726
1727 port = get_ioc3_port(the_port);
1728 if (!port)
1729 return;
1730
1731 state = the_port->state;
1732 wake_up_interruptible(&state->port.delta_msr_wait);
1733
1734 spin_lock_irqsave(&the_port->lock, port_flags);
1735 set_notification(port, N_ALL, 0);
1736 spin_unlock_irqrestore(&the_port->lock, port_flags);
1737}
1738
1739/**
1740 * ic3_set_mctrl - set control lines (dtr, rts, etc)
1741 * @port: Port to operate on
1742 * @mctrl: Lines to set/unset
1743 *
1744 */
1745static void ic3_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
1746{
1747 unsigned char mcr = 0;
1748
1749 if (mctrl & TIOCM_RTS)
1750 mcr |= UART_MCR_RTS;
1751 if (mctrl & TIOCM_DTR)
1752 mcr |= UART_MCR_DTR;
1753 if (mctrl & TIOCM_OUT1)
1754 mcr |= UART_MCR_OUT1;
1755 if (mctrl & TIOCM_OUT2)
1756 mcr |= UART_MCR_OUT2;
1757 if (mctrl & TIOCM_LOOP)
1758 mcr |= UART_MCR_LOOP;
1759
1760 set_mcr(the_port, mcr, SHADOW_DTR);
1761}
1762
1763/**
1764 * ic3_get_mctrl - get control line info
1765 * @port: port to operate on
1766 *
1767 */
1768static unsigned int ic3_get_mctrl(struct uart_port *the_port)
1769{
1770 struct ioc3_port *port = get_ioc3_port(the_port);
1771 uint32_t shadow;
1772 unsigned int ret = 0;
1773
1774 if (!port)
1775 return 0;
1776
1777 shadow = readl(&port->ip_serial_regs->shadow);
1778 if (shadow & SHADOW_DCD)
1779 ret |= TIOCM_CD;
1780 if (shadow & SHADOW_DR)
1781 ret |= TIOCM_DSR;
1782 if (shadow & SHADOW_CTS)
1783 ret |= TIOCM_CTS;
1784 return ret;
1785}
1786
1787/**
1788 * ic3_start_tx - Start transmitter. Called with the_port->lock
1789 * @port: Port to operate on
1790 *
1791 */
1792static void ic3_start_tx(struct uart_port *the_port)
1793{
1794 struct ioc3_port *port = get_ioc3_port(the_port);
1795
1796 if (port) {
1797 set_notification(port, N_OUTPUT_LOWAT, 1);
1798 enable_intrs(port, port->ip_hooks->intr_tx_mt);
1799 }
1800}
1801
1802/**
1803 * ic3_break_ctl - handle breaks
1804 * @port: Port to operate on
1805 * @break_state: Break state
1806 *
1807 */
1808static void ic3_break_ctl(struct uart_port *the_port, int break_state)
1809{
1810}
1811
1812/**
1813 * ic3_startup - Start up the serial port - always return 0 (We're always on)
1814 * @port: Port to operate on
1815 *
1816 */
1817static int ic3_startup(struct uart_port *the_port)
1818{
1819 int retval;
1820 struct ioc3_port *port;
1821 struct ioc3_card *card_ptr;
1822 unsigned long port_flags;
1823
1824 if (!the_port) {
1825 NOT_PROGRESS();
1826 return -ENODEV;
1827 }
1828 port = get_ioc3_port(the_port);
1829 if (!port) {
1830 NOT_PROGRESS();
1831 return -ENODEV;
1832 }
1833 card_ptr = port->ip_card;
1834 port->ip_port = the_port;
1835
1836 if (!card_ptr) {
1837 NOT_PROGRESS();
1838 return -ENODEV;
1839 }
1840
1841 /* Start up the serial port */
1842 spin_lock_irqsave(&the_port->lock, port_flags);
1843 retval = ic3_startup_local(the_port);
1844 spin_unlock_irqrestore(&the_port->lock, port_flags);
1845 return retval;
1846}
1847
1848/**
1849 * ic3_set_termios - set termios stuff
1850 * @port: port to operate on
1851 * @termios: New settings
1852 * @termios: Old
1853 *
1854 */
1855static void
1856ic3_set_termios(struct uart_port *the_port,
1857 struct ktermios *termios, struct ktermios *old_termios)
1858{
1859 unsigned long port_flags;
1860
1861 spin_lock_irqsave(&the_port->lock, port_flags);
1862 ioc3_change_speed(the_port, termios, old_termios);
1863 spin_unlock_irqrestore(&the_port->lock, port_flags);
1864}
1865
1866/**
1867 * ic3_request_port - allocate resources for port - no op....
1868 * @port: port to operate on
1869 *
1870 */
1871static int ic3_request_port(struct uart_port *port)
1872{
1873 return 0;
1874}
1875
1876/* Associate the uart functions above - given to serial core */
1877static struct uart_ops ioc3_ops = {
1878 .tx_empty = ic3_tx_empty,
1879 .set_mctrl = ic3_set_mctrl,
1880 .get_mctrl = ic3_get_mctrl,
1881 .stop_tx = ic3_stop_tx,
1882 .start_tx = ic3_start_tx,
1883 .stop_rx = ic3_stop_rx,
1884 .enable_ms = null_void_function,
1885 .break_ctl = ic3_break_ctl,
1886 .startup = ic3_startup,
1887 .shutdown = ic3_shutdown,
1888 .set_termios = ic3_set_termios,
1889 .type = ic3_type,
1890 .release_port = null_void_function,
1891 .request_port = ic3_request_port,
1892};
1893
1894/*
1895 * Boot-time initialization code
1896 */
1897
1898static struct uart_driver ioc3_uart = {
1899 .owner = THIS_MODULE,
1900 .driver_name = "ioc3_serial",
1901 .dev_name = DEVICE_NAME,
1902 .major = DEVICE_MAJOR,
1903 .minor = DEVICE_MINOR,
1904 .nr = MAX_LOGICAL_PORTS
1905};
1906
1907/**
1908 * ioc3_serial_core_attach - register with serial core
1909 * This is done during pci probing
1910 * @is: submodule struct for this
1911 * @idd: handle for this card
1912 */
1913static inline int ioc3_serial_core_attach( struct ioc3_submodule *is,
1914 struct ioc3_driver_data *idd)
1915{
1916 struct ioc3_port *port;
1917 struct uart_port *the_port;
1918 struct ioc3_card *card_ptr = idd->data[is->id];
1919 int ii, phys_port;
1920 struct pci_dev *pdev = idd->pdev;
1921
1922 DPRINT_CONFIG(("%s: attach pdev 0x%p - card_ptr 0x%p\n",
1923 __func__, pdev, (void *)card_ptr));
1924
1925 if (!card_ptr)
1926 return -ENODEV;
1927
1928 /* once around for each logical port on this card */
1929 for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1930 phys_port = GET_PHYSICAL_PORT(ii);
1931 the_port = &card_ptr->ic_port[phys_port].
1932 icp_uart_port[GET_LOGICAL_PORT(ii)];
1933 port = card_ptr->ic_port[phys_port].icp_port;
1934 port->ip_port = the_port;
1935
1936 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p [%d/%d]\n",
1937 __func__, (void *)the_port, (void *)port,
1938 phys_port, ii));
1939
1940 /* membase, iobase and mapbase just need to be non-0 */
1941 the_port->membase = (unsigned char __iomem *)1;
1942 the_port->iobase = (pdev->bus->number << 16) | ii;
1943 the_port->line = (Num_of_ioc3_cards << 2) | ii;
1944 the_port->mapbase = 1;
1945 the_port->type = PORT_16550A;
1946 the_port->fifosize = FIFO_SIZE;
1947 the_port->ops = &ioc3_ops;
1948 the_port->irq = idd->irq_io;
1949 the_port->dev = &pdev->dev;
1950
1951 if (uart_add_one_port(&ioc3_uart, the_port) < 0) {
1952 printk(KERN_WARNING
1953 "%s: unable to add port %d bus %d\n",
1954 __func__, the_port->line, pdev->bus->number);
1955 } else {
1956 DPRINT_CONFIG(("IOC3 serial port %d irq %d bus %d\n",
1957 the_port->line, the_port->irq, pdev->bus->number));
1958 }
1959
1960 /* all ports are rs232 for now */
1961 if (IS_PHYSICAL_PORT(ii))
1962 ioc3_set_proto(port, PROTO_RS232);
1963 }
1964 return 0;
1965}
1966
1967/**
1968 * ioc3uart_remove - register detach function
1969 * @is: submodule struct for this submodule
1970 * @idd: ioc3 driver data for this submodule
1971 */
1972
1973static int ioc3uart_remove(struct ioc3_submodule *is,
1974 struct ioc3_driver_data *idd)
1975{
1976 struct ioc3_card *card_ptr = idd->data[is->id];
1977 struct uart_port *the_port;
1978 struct ioc3_port *port;
1979 int ii;
1980
1981 if (card_ptr) {
1982 for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1983 the_port = &card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1984 icp_uart_port[GET_LOGICAL_PORT(ii)];
1985 if (the_port)
1986 uart_remove_one_port(&ioc3_uart, the_port);
1987 port = card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].icp_port;
1988 if (port && IS_PHYSICAL_PORT(ii)
1989 && (GET_PHYSICAL_PORT(ii) == 0)) {
1990 pci_free_consistent(port->ip_idd->pdev,
1991 TOTAL_RING_BUF_SIZE,
1992 (void *)port->ip_cpu_ringbuf,
1993 port->ip_dma_ringbuf);
1994 kfree(port);
1995 card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1996 icp_port = NULL;
1997 }
1998 }
1999 kfree(card_ptr);
2000 idd->data[is->id] = NULL;
2001 }
2002 return 0;
2003}
2004
2005/**
2006 * ioc3uart_probe - card probe function called from shim driver
2007 * @is: submodule struct for this submodule
2008 * @idd: ioc3 driver data for this card
2009 */
2010
2011static int __devinit
2012ioc3uart_probe(struct ioc3_submodule *is, struct ioc3_driver_data *idd)
2013{
2014 struct pci_dev *pdev = idd->pdev;
2015 struct ioc3_card *card_ptr;
2016 int ret = 0;
2017 struct ioc3_port *port;
2018 struct ioc3_port *ports[PORTS_PER_CARD];
2019 int phys_port;
2020 int cnt;
2021
2022 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, is, idd));
2023
2024 card_ptr = kzalloc(sizeof(struct ioc3_card), GFP_KERNEL);
2025 if (!card_ptr) {
2026 printk(KERN_WARNING "ioc3_attach_one"
2027 ": unable to get memory for the IOC3\n");
2028 return -ENOMEM;
2029 }
2030 idd->data[is->id] = card_ptr;
2031 Submodule_slot = is->id;
2032
2033 writel(((UARTA_BASE >> 3) << SIO_CR_SER_A_BASE_SHIFT) |
2034 ((UARTB_BASE >> 3) << SIO_CR_SER_B_BASE_SHIFT) |
2035 (0xf << SIO_CR_CMD_PULSE_SHIFT), &idd->vma->sio_cr);
2036
2037 pci_write_config_dword(pdev, PCI_LAT, 0xff00);
2038
2039 /* Enable serial port mode select generic PIO pins as outputs */
2040 ioc3_gpcr_set(idd, GPCR_UARTA_MODESEL | GPCR_UARTB_MODESEL);
2041
2042 /* Create port structures for each port */
2043 for (phys_port = 0; phys_port < PORTS_PER_CARD; phys_port++) {
2044 port = kzalloc(sizeof(struct ioc3_port), GFP_KERNEL);
2045 if (!port) {
2046 printk(KERN_WARNING
2047 "IOC3 serial memory not available for port\n");
2048 ret = -ENOMEM;
2049 goto out4;
2050 }
2051 spin_lock_init(&port->ip_lock);
2052
2053 /* we need to remember the previous ones, to point back to
2054 * them farther down - setting up the ring buffers.
2055 */
2056 ports[phys_port] = port;
2057
2058 /* init to something useful */
2059 card_ptr->ic_port[phys_port].icp_port = port;
2060 port->ip_is = is;
2061 port->ip_idd = idd;
2062 port->ip_baud = 9600;
2063 port->ip_card = card_ptr;
2064 port->ip_hooks = &hooks_array[phys_port];
2065
2066 /* Setup each port */
2067 if (phys_port == 0) {
2068 port->ip_serial_regs = &idd->vma->port_a;
2069 port->ip_uart_regs = &idd->vma->sregs.uarta;
2070
2071 DPRINT_CONFIG(("%s : Port A ip_serial_regs 0x%p "
2072 "ip_uart_regs 0x%p\n",
2073 __func__,
2074 (void *)port->ip_serial_regs,
2075 (void *)port->ip_uart_regs));
2076
2077 /* setup ring buffers */
2078 port->ip_cpu_ringbuf = pci_alloc_consistent(pdev,
2079 TOTAL_RING_BUF_SIZE, &port->ip_dma_ringbuf);
2080
2081 BUG_ON(!((((int64_t) port->ip_dma_ringbuf) &
2082 (TOTAL_RING_BUF_SIZE - 1)) == 0));
2083 port->ip_inring = RING(port, RX_A);
2084 port->ip_outring = RING(port, TX_A);
2085 DPRINT_CONFIG(("%s : Port A ip_cpu_ringbuf 0x%p "
2086 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2087 "ip_outring 0x%p\n",
2088 __func__,
2089 (void *)port->ip_cpu_ringbuf,
2090 (void *)port->ip_dma_ringbuf,
2091 (void *)port->ip_inring,
2092 (void *)port->ip_outring));
2093 }
2094 else {
2095 port->ip_serial_regs = &idd->vma->port_b;
2096 port->ip_uart_regs = &idd->vma->sregs.uartb;
2097
2098 DPRINT_CONFIG(("%s : Port B ip_serial_regs 0x%p "
2099 "ip_uart_regs 0x%p\n",
2100 __func__,
2101 (void *)port->ip_serial_regs,
2102 (void *)port->ip_uart_regs));
2103
2104 /* share the ring buffers */
2105 port->ip_dma_ringbuf =
2106 ports[phys_port - 1]->ip_dma_ringbuf;
2107 port->ip_cpu_ringbuf =
2108 ports[phys_port - 1]->ip_cpu_ringbuf;
2109 port->ip_inring = RING(port, RX_B);
2110 port->ip_outring = RING(port, TX_B);
2111 DPRINT_CONFIG(("%s : Port B ip_cpu_ringbuf 0x%p "
2112 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2113 "ip_outring 0x%p\n",
2114 __func__,
2115 (void *)port->ip_cpu_ringbuf,
2116 (void *)port->ip_dma_ringbuf,
2117 (void *)port->ip_inring,
2118 (void *)port->ip_outring));
2119 }
2120
2121 DPRINT_CONFIG(("%s : port %d [addr 0x%p] card_ptr 0x%p",
2122 __func__,
2123 phys_port, (void *)port, (void *)card_ptr));
2124 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
2125 (void *)port->ip_serial_regs,
2126 (void *)port->ip_uart_regs));
2127
2128 /* Initialize the hardware for IOC3 */
2129 port_init(port);
2130
2131 DPRINT_CONFIG(("%s: phys_port %d port 0x%p inring 0x%p "
2132 "outring 0x%p\n",
2133 __func__,
2134 phys_port, (void *)port,
2135 (void *)port->ip_inring,
2136 (void *)port->ip_outring));
2137
2138 }
2139
2140 /* register port with the serial core */
2141
2142 if ((ret = ioc3_serial_core_attach(is, idd)))
2143 goto out4;
2144
2145 Num_of_ioc3_cards++;
2146
2147 return ret;
2148
2149 /* error exits that give back resources */
2150out4:
2151 for (cnt = 0; cnt < phys_port; cnt++)
2152 kfree(ports[cnt]);
2153
2154 kfree(card_ptr);
2155 return ret;
2156}
2157
2158static struct ioc3_submodule ioc3uart_ops = {
2159 .name = "IOC3uart",
2160 .probe = ioc3uart_probe,
2161 .remove = ioc3uart_remove,
2162 /* call .intr for both ports initially */
2163 .irq_mask = SIO_IR_SA | SIO_IR_SB,
2164 .intr = ioc3uart_intr,
2165 .owner = THIS_MODULE,
2166};
2167
2168/**
2169 * ioc3_detect - module init called,
2170 */
2171static int __init ioc3uart_init(void)
2172{
2173 int ret;
2174
2175 /* register with serial core */
2176 if ((ret = uart_register_driver(&ioc3_uart)) < 0) {
2177 printk(KERN_WARNING
2178 "%s: Couldn't register IOC3 uart serial driver\n",
2179 __func__);
2180 return ret;
2181 }
2182 ret = ioc3_register_submodule(&ioc3uart_ops);
2183 if (ret)
2184 uart_unregister_driver(&ioc3_uart);
2185 return ret;
2186}
2187
2188static void __exit ioc3uart_exit(void)
2189{
2190 ioc3_unregister_submodule(&ioc3uart_ops);
2191 uart_unregister_driver(&ioc3_uart);
2192}
2193
2194module_init(ioc3uart_init);
2195module_exit(ioc3uart_exit);
2196
2197MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2198MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC3 card");
2199MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ioc4_serial.c b/drivers/tty/serial/ioc4_serial.c
new file mode 100644
index 000000000000..fcfe82653ac8
--- /dev/null
+++ b/drivers/tty/serial/ioc4_serial.c
@@ -0,0 +1,2953 @@
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2003-2006 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9
10/*
11 * This file contains a module version of the ioc4 serial driver. This
12 * includes all the support functions needed (support functions, etc.)
13 * and the serial driver itself.
14 */
15#include <linux/errno.h>
16#include <linux/tty.h>
17#include <linux/serial.h>
18#include <linux/serialP.h>
19#include <linux/circ_buf.h>
20#include <linux/serial_reg.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/ioc4.h>
24#include <linux/serial_core.h>
25#include <linux/slab.h>
26
27/*
28 * interesting things about the ioc4
29 */
30
31#define IOC4_NUM_SERIAL_PORTS 4 /* max ports per card */
32#define IOC4_NUM_CARDS 8 /* max cards per partition */
33
34#define GET_SIO_IR(_n) (_n == 0) ? (IOC4_SIO_IR_S0) : \
35 (_n == 1) ? (IOC4_SIO_IR_S1) : \
36 (_n == 2) ? (IOC4_SIO_IR_S2) : \
37 (IOC4_SIO_IR_S3)
38
39#define GET_OTHER_IR(_n) (_n == 0) ? (IOC4_OTHER_IR_S0_MEMERR) : \
40 (_n == 1) ? (IOC4_OTHER_IR_S1_MEMERR) : \
41 (_n == 2) ? (IOC4_OTHER_IR_S2_MEMERR) : \
42 (IOC4_OTHER_IR_S3_MEMERR)
43
44
45/*
46 * All IOC4 registers are 32 bits wide.
47 */
48
49/*
50 * PCI Memory Space Map
51 */
52#define IOC4_PCI_ERR_ADDR_L 0x000 /* Low Error Address */
53#define IOC4_PCI_ERR_ADDR_VLD (0x1 << 0)
54#define IOC4_PCI_ERR_ADDR_MST_ID_MSK (0xf << 1)
55#define IOC4_PCI_ERR_ADDR_MST_NUM_MSK (0xe << 1)
56#define IOC4_PCI_ERR_ADDR_MST_TYP_MSK (0x1 << 1)
57#define IOC4_PCI_ERR_ADDR_MUL_ERR (0x1 << 5)
58#define IOC4_PCI_ERR_ADDR_ADDR_MSK (0x3ffffff << 6)
59
60/* Interrupt types */
61#define IOC4_SIO_INTR_TYPE 0
62#define IOC4_OTHER_INTR_TYPE 1
63#define IOC4_NUM_INTR_TYPES 2
64
65/* Bitmasks for IOC4_SIO_IR, IOC4_SIO_IEC, and IOC4_SIO_IES */
66#define IOC4_SIO_IR_S0_TX_MT 0x00000001 /* Serial port 0 TX empty */
67#define IOC4_SIO_IR_S0_RX_FULL 0x00000002 /* Port 0 RX buf full */
68#define IOC4_SIO_IR_S0_RX_HIGH 0x00000004 /* Port 0 RX hiwat */
69#define IOC4_SIO_IR_S0_RX_TIMER 0x00000008 /* Port 0 RX timeout */
70#define IOC4_SIO_IR_S0_DELTA_DCD 0x00000010 /* Port 0 delta DCD */
71#define IOC4_SIO_IR_S0_DELTA_CTS 0x00000020 /* Port 0 delta CTS */
72#define IOC4_SIO_IR_S0_INT 0x00000040 /* Port 0 pass-thru intr */
73#define IOC4_SIO_IR_S0_TX_EXPLICIT 0x00000080 /* Port 0 explicit TX thru */
74#define IOC4_SIO_IR_S1_TX_MT 0x00000100 /* Serial port 1 */
75#define IOC4_SIO_IR_S1_RX_FULL 0x00000200 /* */
76#define IOC4_SIO_IR_S1_RX_HIGH 0x00000400 /* */
77#define IOC4_SIO_IR_S1_RX_TIMER 0x00000800 /* */
78#define IOC4_SIO_IR_S1_DELTA_DCD 0x00001000 /* */
79#define IOC4_SIO_IR_S1_DELTA_CTS 0x00002000 /* */
80#define IOC4_SIO_IR_S1_INT 0x00004000 /* */
81#define IOC4_SIO_IR_S1_TX_EXPLICIT 0x00008000 /* */
82#define IOC4_SIO_IR_S2_TX_MT 0x00010000 /* Serial port 2 */
83#define IOC4_SIO_IR_S2_RX_FULL 0x00020000 /* */
84#define IOC4_SIO_IR_S2_RX_HIGH 0x00040000 /* */
85#define IOC4_SIO_IR_S2_RX_TIMER 0x00080000 /* */
86#define IOC4_SIO_IR_S2_DELTA_DCD 0x00100000 /* */
87#define IOC4_SIO_IR_S2_DELTA_CTS 0x00200000 /* */
88#define IOC4_SIO_IR_S2_INT 0x00400000 /* */
89#define IOC4_SIO_IR_S2_TX_EXPLICIT 0x00800000 /* */
90#define IOC4_SIO_IR_S3_TX_MT 0x01000000 /* Serial port 3 */
91#define IOC4_SIO_IR_S3_RX_FULL 0x02000000 /* */
92#define IOC4_SIO_IR_S3_RX_HIGH 0x04000000 /* */
93#define IOC4_SIO_IR_S3_RX_TIMER 0x08000000 /* */
94#define IOC4_SIO_IR_S3_DELTA_DCD 0x10000000 /* */
95#define IOC4_SIO_IR_S3_DELTA_CTS 0x20000000 /* */
96#define IOC4_SIO_IR_S3_INT 0x40000000 /* */
97#define IOC4_SIO_IR_S3_TX_EXPLICIT 0x80000000 /* */
98
99/* Per device interrupt masks */
100#define IOC4_SIO_IR_S0 (IOC4_SIO_IR_S0_TX_MT | \
101 IOC4_SIO_IR_S0_RX_FULL | \
102 IOC4_SIO_IR_S0_RX_HIGH | \
103 IOC4_SIO_IR_S0_RX_TIMER | \
104 IOC4_SIO_IR_S0_DELTA_DCD | \
105 IOC4_SIO_IR_S0_DELTA_CTS | \
106 IOC4_SIO_IR_S0_INT | \
107 IOC4_SIO_IR_S0_TX_EXPLICIT)
108#define IOC4_SIO_IR_S1 (IOC4_SIO_IR_S1_TX_MT | \
109 IOC4_SIO_IR_S1_RX_FULL | \
110 IOC4_SIO_IR_S1_RX_HIGH | \
111 IOC4_SIO_IR_S1_RX_TIMER | \
112 IOC4_SIO_IR_S1_DELTA_DCD | \
113 IOC4_SIO_IR_S1_DELTA_CTS | \
114 IOC4_SIO_IR_S1_INT | \
115 IOC4_SIO_IR_S1_TX_EXPLICIT)
116#define IOC4_SIO_IR_S2 (IOC4_SIO_IR_S2_TX_MT | \
117 IOC4_SIO_IR_S2_RX_FULL | \
118 IOC4_SIO_IR_S2_RX_HIGH | \
119 IOC4_SIO_IR_S2_RX_TIMER | \
120 IOC4_SIO_IR_S2_DELTA_DCD | \
121 IOC4_SIO_IR_S2_DELTA_CTS | \
122 IOC4_SIO_IR_S2_INT | \
123 IOC4_SIO_IR_S2_TX_EXPLICIT)
124#define IOC4_SIO_IR_S3 (IOC4_SIO_IR_S3_TX_MT | \
125 IOC4_SIO_IR_S3_RX_FULL | \
126 IOC4_SIO_IR_S3_RX_HIGH | \
127 IOC4_SIO_IR_S3_RX_TIMER | \
128 IOC4_SIO_IR_S3_DELTA_DCD | \
129 IOC4_SIO_IR_S3_DELTA_CTS | \
130 IOC4_SIO_IR_S3_INT | \
131 IOC4_SIO_IR_S3_TX_EXPLICIT)
132
133/* Bitmasks for IOC4_OTHER_IR, IOC4_OTHER_IEC, and IOC4_OTHER_IES */
134#define IOC4_OTHER_IR_ATA_INT 0x00000001 /* ATAPI intr pass-thru */
135#define IOC4_OTHER_IR_ATA_MEMERR 0x00000002 /* ATAPI DMA PCI error */
136#define IOC4_OTHER_IR_S0_MEMERR 0x00000004 /* Port 0 PCI error */
137#define IOC4_OTHER_IR_S1_MEMERR 0x00000008 /* Port 1 PCI error */
138#define IOC4_OTHER_IR_S2_MEMERR 0x00000010 /* Port 2 PCI error */
139#define IOC4_OTHER_IR_S3_MEMERR 0x00000020 /* Port 3 PCI error */
140#define IOC4_OTHER_IR_KBD_INT 0x00000040 /* Keyboard/mouse */
141#define IOC4_OTHER_IR_RESERVED 0x007fff80 /* Reserved */
142#define IOC4_OTHER_IR_RT_INT 0x00800000 /* INT_OUT section output */
143#define IOC4_OTHER_IR_GEN_INT 0xff000000 /* Generic pins */
144
145#define IOC4_OTHER_IR_SER_MEMERR (IOC4_OTHER_IR_S0_MEMERR | IOC4_OTHER_IR_S1_MEMERR | \
146 IOC4_OTHER_IR_S2_MEMERR | IOC4_OTHER_IR_S3_MEMERR)
147
148/* Bitmasks for IOC4_SIO_CR */
149#define IOC4_SIO_CR_CMD_PULSE_SHIFT 0 /* byte bus strobe shift */
150#define IOC4_SIO_CR_ARB_DIAG_TX0 0x00000000
151#define IOC4_SIO_CR_ARB_DIAG_RX0 0x00000010
152#define IOC4_SIO_CR_ARB_DIAG_TX1 0x00000020
153#define IOC4_SIO_CR_ARB_DIAG_RX1 0x00000030
154#define IOC4_SIO_CR_ARB_DIAG_TX2 0x00000040
155#define IOC4_SIO_CR_ARB_DIAG_RX2 0x00000050
156#define IOC4_SIO_CR_ARB_DIAG_TX3 0x00000060
157#define IOC4_SIO_CR_ARB_DIAG_RX3 0x00000070
158#define IOC4_SIO_CR_SIO_DIAG_IDLE 0x00000080 /* 0 -> active request among
159 serial ports (ro) */
160/* Defs for some of the generic I/O pins */
161#define IOC4_GPCR_UART0_MODESEL 0x10 /* Pin is output to port 0
162 mode sel */
163#define IOC4_GPCR_UART1_MODESEL 0x20 /* Pin is output to port 1
164 mode sel */
165#define IOC4_GPCR_UART2_MODESEL 0x40 /* Pin is output to port 2
166 mode sel */
167#define IOC4_GPCR_UART3_MODESEL 0x80 /* Pin is output to port 3
168 mode sel */
169
170#define IOC4_GPPR_UART0_MODESEL_PIN 4 /* GIO pin controlling
171 uart 0 mode select */
172#define IOC4_GPPR_UART1_MODESEL_PIN 5 /* GIO pin controlling
173 uart 1 mode select */
174#define IOC4_GPPR_UART2_MODESEL_PIN 6 /* GIO pin controlling
175 uart 2 mode select */
176#define IOC4_GPPR_UART3_MODESEL_PIN 7 /* GIO pin controlling
177 uart 3 mode select */
178
179/* Bitmasks for serial RX status byte */
180#define IOC4_RXSB_OVERRUN 0x01 /* Char(s) lost */
181#define IOC4_RXSB_PAR_ERR 0x02 /* Parity error */
182#define IOC4_RXSB_FRAME_ERR 0x04 /* Framing error */
183#define IOC4_RXSB_BREAK 0x08 /* Break character */
184#define IOC4_RXSB_CTS 0x10 /* State of CTS */
185#define IOC4_RXSB_DCD 0x20 /* State of DCD */
186#define IOC4_RXSB_MODEM_VALID 0x40 /* DCD, CTS, and OVERRUN are valid */
187#define IOC4_RXSB_DATA_VALID 0x80 /* Data byte, FRAME_ERR PAR_ERR
188 * & BREAK valid */
189
190/* Bitmasks for serial TX control byte */
191#define IOC4_TXCB_INT_WHEN_DONE 0x20 /* Interrupt after this byte is sent */
192#define IOC4_TXCB_INVALID 0x00 /* Byte is invalid */
193#define IOC4_TXCB_VALID 0x40 /* Byte is valid */
194#define IOC4_TXCB_MCR 0x80 /* Data<7:0> to modem control reg */
195#define IOC4_TXCB_DELAY 0xc0 /* Delay data<7:0> mSec */
196
197/* Bitmasks for IOC4_SBBR_L */
198#define IOC4_SBBR_L_SIZE 0x00000001 /* 0 == 1KB rings, 1 == 4KB rings */
199
200/* Bitmasks for IOC4_SSCR_<3:0> */
201#define IOC4_SSCR_RX_THRESHOLD 0x000001ff /* Hiwater mark */
202#define IOC4_SSCR_TX_TIMER_BUSY 0x00010000 /* TX timer in progress */
203#define IOC4_SSCR_HFC_EN 0x00020000 /* Hardware flow control enabled */
204#define IOC4_SSCR_RX_RING_DCD 0x00040000 /* Post RX record on delta-DCD */
205#define IOC4_SSCR_RX_RING_CTS 0x00080000 /* Post RX record on delta-CTS */
206#define IOC4_SSCR_DIAG 0x00200000 /* Bypass clock divider for sim */
207#define IOC4_SSCR_RX_DRAIN 0x08000000 /* Drain RX buffer to memory */
208#define IOC4_SSCR_DMA_EN 0x10000000 /* Enable ring buffer DMA */
209#define IOC4_SSCR_DMA_PAUSE 0x20000000 /* Pause DMA */
210#define IOC4_SSCR_PAUSE_STATE 0x40000000 /* Sets when PAUSE takes effect */
211#define IOC4_SSCR_RESET 0x80000000 /* Reset DMA channels */
212
213/* All producer/comsumer pointers are the same bitfield */
214#define IOC4_PROD_CONS_PTR_4K 0x00000ff8 /* For 4K buffers */
215#define IOC4_PROD_CONS_PTR_1K 0x000003f8 /* For 1K buffers */
216#define IOC4_PROD_CONS_PTR_OFF 3
217
218/* Bitmasks for IOC4_SRCIR_<3:0> */
219#define IOC4_SRCIR_ARM 0x80000000 /* Arm RX timer */
220
221/* Bitmasks for IOC4_SHADOW_<3:0> */
222#define IOC4_SHADOW_DR 0x00000001 /* Data ready */
223#define IOC4_SHADOW_OE 0x00000002 /* Overrun error */
224#define IOC4_SHADOW_PE 0x00000004 /* Parity error */
225#define IOC4_SHADOW_FE 0x00000008 /* Framing error */
226#define IOC4_SHADOW_BI 0x00000010 /* Break interrupt */
227#define IOC4_SHADOW_THRE 0x00000020 /* Xmit holding register empty */
228#define IOC4_SHADOW_TEMT 0x00000040 /* Xmit shift register empty */
229#define IOC4_SHADOW_RFCE 0x00000080 /* Char in RX fifo has an error */
230#define IOC4_SHADOW_DCTS 0x00010000 /* Delta clear to send */
231#define IOC4_SHADOW_DDCD 0x00080000 /* Delta data carrier detect */
232#define IOC4_SHADOW_CTS 0x00100000 /* Clear to send */
233#define IOC4_SHADOW_DCD 0x00800000 /* Data carrier detect */
234#define IOC4_SHADOW_DTR 0x01000000 /* Data terminal ready */
235#define IOC4_SHADOW_RTS 0x02000000 /* Request to send */
236#define IOC4_SHADOW_OUT1 0x04000000 /* 16550 OUT1 bit */
237#define IOC4_SHADOW_OUT2 0x08000000 /* 16550 OUT2 bit */
238#define IOC4_SHADOW_LOOP 0x10000000 /* Loopback enabled */
239
240/* Bitmasks for IOC4_SRTR_<3:0> */
241#define IOC4_SRTR_CNT 0x00000fff /* Reload value for RX timer */
242#define IOC4_SRTR_CNT_VAL 0x0fff0000 /* Current value of RX timer */
243#define IOC4_SRTR_CNT_VAL_SHIFT 16
244#define IOC4_SRTR_HZ 16000 /* SRTR clock frequency */
245
246/* Serial port register map used for DMA and PIO serial I/O */
247struct ioc4_serialregs {
248 uint32_t sscr;
249 uint32_t stpir;
250 uint32_t stcir;
251 uint32_t srpir;
252 uint32_t srcir;
253 uint32_t srtr;
254 uint32_t shadow;
255};
256
257/* IOC4 UART register map */
258struct ioc4_uartregs {
259 char i4u_lcr;
260 union {
261 char iir; /* read only */
262 char fcr; /* write only */
263 } u3;
264 union {
265 char ier; /* DLAB == 0 */
266 char dlm; /* DLAB == 1 */
267 } u2;
268 union {
269 char rbr; /* read only, DLAB == 0 */
270 char thr; /* write only, DLAB == 0 */
271 char dll; /* DLAB == 1 */
272 } u1;
273 char i4u_scr;
274 char i4u_msr;
275 char i4u_lsr;
276 char i4u_mcr;
277};
278
279/* short names */
280#define i4u_dll u1.dll
281#define i4u_ier u2.ier
282#define i4u_dlm u2.dlm
283#define i4u_fcr u3.fcr
284
285/* Serial port registers used for DMA serial I/O */
286struct ioc4_serial {
287 uint32_t sbbr01_l;
288 uint32_t sbbr01_h;
289 uint32_t sbbr23_l;
290 uint32_t sbbr23_h;
291
292 struct ioc4_serialregs port_0;
293 struct ioc4_serialregs port_1;
294 struct ioc4_serialregs port_2;
295 struct ioc4_serialregs port_3;
296 struct ioc4_uartregs uart_0;
297 struct ioc4_uartregs uart_1;
298 struct ioc4_uartregs uart_2;
299 struct ioc4_uartregs uart_3;
300} ioc4_serial;
301
302/* UART clock speed */
303#define IOC4_SER_XIN_CLK_66 66666667
304#define IOC4_SER_XIN_CLK_33 33333333
305
306#define IOC4_W_IES 0
307#define IOC4_W_IEC 1
308
309typedef void ioc4_intr_func_f(void *, uint32_t);
310typedef ioc4_intr_func_f *ioc4_intr_func_t;
311
312static unsigned int Num_of_ioc4_cards;
313
314/* defining this will get you LOTS of great debug info */
315//#define DEBUG_INTERRUPTS
316#define DPRINT_CONFIG(_x...) ;
317//#define DPRINT_CONFIG(_x...) printk _x
318
319/* number of characters left in xmit buffer before we ask for more */
320#define WAKEUP_CHARS 256
321
322/* number of characters we want to transmit to the lower level at a time */
323#define IOC4_MAX_CHARS 256
324#define IOC4_FIFO_CHARS 255
325
326/* Device name we're using */
327#define DEVICE_NAME_RS232 "ttyIOC"
328#define DEVICE_NAME_RS422 "ttyAIOC"
329#define DEVICE_MAJOR 204
330#define DEVICE_MINOR_RS232 50
331#define DEVICE_MINOR_RS422 84
332
333
334/* register offsets */
335#define IOC4_SERIAL_OFFSET 0x300
336
337/* flags for next_char_state */
338#define NCS_BREAK 0x1
339#define NCS_PARITY 0x2
340#define NCS_FRAMING 0x4
341#define NCS_OVERRUN 0x8
342
343/* cause we need SOME parameters ... */
344#define MIN_BAUD_SUPPORTED 1200
345#define MAX_BAUD_SUPPORTED 115200
346
347/* protocol types supported */
348#define PROTO_RS232 3
349#define PROTO_RS422 7
350
351/* Notification types */
352#define N_DATA_READY 0x01
353#define N_OUTPUT_LOWAT 0x02
354#define N_BREAK 0x04
355#define N_PARITY_ERROR 0x08
356#define N_FRAMING_ERROR 0x10
357#define N_OVERRUN_ERROR 0x20
358#define N_DDCD 0x40
359#define N_DCTS 0x80
360
361#define N_ALL_INPUT (N_DATA_READY | N_BREAK | \
362 N_PARITY_ERROR | N_FRAMING_ERROR | \
363 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
364
365#define N_ALL_OUTPUT N_OUTPUT_LOWAT
366
367#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
368
369#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK | \
370 N_PARITY_ERROR | N_FRAMING_ERROR | \
371 N_OVERRUN_ERROR | N_DDCD | N_DCTS)
372
373#define SER_DIVISOR(_x, clk) (((clk) + (_x) * 8) / ((_x) * 16))
374#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
375
376/* Some masks */
377#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
378 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
379#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
380
381#define PENDING(_p) (readl(&(_p)->ip_mem->sio_ir.raw) & _p->ip_ienb)
382#define READ_SIO_IR(_p) readl(&(_p)->ip_mem->sio_ir.raw)
383
384/* Default to 4k buffers */
385#ifdef IOC4_1K_BUFFERS
386#define RING_BUF_SIZE 1024
387#define IOC4_BUF_SIZE_BIT 0
388#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_1K
389#else
390#define RING_BUF_SIZE 4096
391#define IOC4_BUF_SIZE_BIT IOC4_SBBR_L_SIZE
392#define PROD_CONS_MASK IOC4_PROD_CONS_PTR_4K
393#endif
394
395#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
396
397/*
398 * This is the entry saved by the driver - one per card
399 */
400
401#define UART_PORT_MIN 0
402#define UART_PORT_RS232 UART_PORT_MIN
403#define UART_PORT_RS422 1
404#define UART_PORT_COUNT 2 /* one for each mode */
405
406struct ioc4_control {
407 int ic_irq;
408 struct {
409 /* uart ports are allocated here - 1 for rs232, 1 for rs422 */
410 struct uart_port icp_uart_port[UART_PORT_COUNT];
411 /* Handy reference material */
412 struct ioc4_port *icp_port;
413 } ic_port[IOC4_NUM_SERIAL_PORTS];
414 struct ioc4_soft *ic_soft;
415};
416
417/*
418 * per-IOC4 data structure
419 */
420#define MAX_IOC4_INTR_ENTS (8 * sizeof(uint32_t))
421struct ioc4_soft {
422 struct ioc4_misc_regs __iomem *is_ioc4_misc_addr;
423 struct ioc4_serial __iomem *is_ioc4_serial_addr;
424
425 /* Each interrupt type has an entry in the array */
426 struct ioc4_intr_type {
427
428 /*
429 * Each in-use entry in this array contains at least
430 * one nonzero bit in sd_bits; no two entries in this
431 * array have overlapping sd_bits values.
432 */
433 struct ioc4_intr_info {
434 uint32_t sd_bits;
435 ioc4_intr_func_f *sd_intr;
436 void *sd_info;
437 } is_intr_info[MAX_IOC4_INTR_ENTS];
438
439 /* Number of entries active in the above array */
440 atomic_t is_num_intrs;
441 } is_intr_type[IOC4_NUM_INTR_TYPES];
442
443 /* is_ir_lock must be held while
444 * modifying sio_ie values, so
445 * we can be sure that sio_ie is
446 * not changing when we read it
447 * along with sio_ir.
448 */
449 spinlock_t is_ir_lock; /* SIO_IE[SC] mod lock */
450};
451
452/* Local port info for each IOC4 serial ports */
453struct ioc4_port {
454 struct uart_port *ip_port; /* current active port ptr */
455 /* Ptrs for all ports */
456 struct uart_port *ip_all_ports[UART_PORT_COUNT];
457 /* Back ptrs for this port */
458 struct ioc4_control *ip_control;
459 struct pci_dev *ip_pdev;
460 struct ioc4_soft *ip_ioc4_soft;
461
462 /* pci mem addresses */
463 struct ioc4_misc_regs __iomem *ip_mem;
464 struct ioc4_serial __iomem *ip_serial;
465 struct ioc4_serialregs __iomem *ip_serial_regs;
466 struct ioc4_uartregs __iomem *ip_uart_regs;
467
468 /* Ring buffer page for this port */
469 dma_addr_t ip_dma_ringbuf;
470 /* vaddr of ring buffer */
471 struct ring_buffer *ip_cpu_ringbuf;
472
473 /* Rings for this port */
474 struct ring *ip_inring;
475 struct ring *ip_outring;
476
477 /* Hook to port specific values */
478 struct hooks *ip_hooks;
479
480 spinlock_t ip_lock;
481
482 /* Various rx/tx parameters */
483 int ip_baud;
484 int ip_tx_lowat;
485 int ip_rx_timeout;
486
487 /* Copy of notification bits */
488 int ip_notify;
489
490 /* Shadow copies of various registers so we don't need to PIO
491 * read them constantly
492 */
493 uint32_t ip_ienb; /* Enabled interrupts */
494 uint32_t ip_sscr;
495 uint32_t ip_tx_prod;
496 uint32_t ip_rx_cons;
497 int ip_pci_bus_speed;
498 unsigned char ip_flags;
499};
500
501/* tx low water mark. We need to notify the driver whenever tx is getting
502 * close to empty so it can refill the tx buffer and keep things going.
503 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
504 * have no trouble getting in more chars in time (I certainly hope so).
505 */
506#define TX_LOWAT_LATENCY 1000
507#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
508#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
509
510/* Flags per port */
511#define INPUT_HIGH 0x01
512#define DCD_ON 0x02
513#define LOWAT_WRITTEN 0x04
514#define READ_ABORTED 0x08
515#define PORT_ACTIVE 0x10
516#define PORT_INACTIVE 0 /* This is the value when "off" */
517
518
519/* Since each port has different register offsets and bitmasks
520 * for everything, we'll store those that we need in tables so we
521 * don't have to be constantly checking the port we are dealing with.
522 */
523struct hooks {
524 uint32_t intr_delta_dcd;
525 uint32_t intr_delta_cts;
526 uint32_t intr_tx_mt;
527 uint32_t intr_rx_timer;
528 uint32_t intr_rx_high;
529 uint32_t intr_tx_explicit;
530 uint32_t intr_dma_error;
531 uint32_t intr_clear;
532 uint32_t intr_all;
533 int rs422_select_pin;
534};
535
536static struct hooks hooks_array[IOC4_NUM_SERIAL_PORTS] = {
537 /* Values for port 0 */
538 {
539 IOC4_SIO_IR_S0_DELTA_DCD, IOC4_SIO_IR_S0_DELTA_CTS,
540 IOC4_SIO_IR_S0_TX_MT, IOC4_SIO_IR_S0_RX_TIMER,
541 IOC4_SIO_IR_S0_RX_HIGH, IOC4_SIO_IR_S0_TX_EXPLICIT,
542 IOC4_OTHER_IR_S0_MEMERR,
543 (IOC4_SIO_IR_S0_TX_MT | IOC4_SIO_IR_S0_RX_FULL |
544 IOC4_SIO_IR_S0_RX_HIGH | IOC4_SIO_IR_S0_RX_TIMER |
545 IOC4_SIO_IR_S0_DELTA_DCD | IOC4_SIO_IR_S0_DELTA_CTS |
546 IOC4_SIO_IR_S0_INT | IOC4_SIO_IR_S0_TX_EXPLICIT),
547 IOC4_SIO_IR_S0, IOC4_GPPR_UART0_MODESEL_PIN,
548 },
549
550 /* Values for port 1 */
551 {
552 IOC4_SIO_IR_S1_DELTA_DCD, IOC4_SIO_IR_S1_DELTA_CTS,
553 IOC4_SIO_IR_S1_TX_MT, IOC4_SIO_IR_S1_RX_TIMER,
554 IOC4_SIO_IR_S1_RX_HIGH, IOC4_SIO_IR_S1_TX_EXPLICIT,
555 IOC4_OTHER_IR_S1_MEMERR,
556 (IOC4_SIO_IR_S1_TX_MT | IOC4_SIO_IR_S1_RX_FULL |
557 IOC4_SIO_IR_S1_RX_HIGH | IOC4_SIO_IR_S1_RX_TIMER |
558 IOC4_SIO_IR_S1_DELTA_DCD | IOC4_SIO_IR_S1_DELTA_CTS |
559 IOC4_SIO_IR_S1_INT | IOC4_SIO_IR_S1_TX_EXPLICIT),
560 IOC4_SIO_IR_S1, IOC4_GPPR_UART1_MODESEL_PIN,
561 },
562
563 /* Values for port 2 */
564 {
565 IOC4_SIO_IR_S2_DELTA_DCD, IOC4_SIO_IR_S2_DELTA_CTS,
566 IOC4_SIO_IR_S2_TX_MT, IOC4_SIO_IR_S2_RX_TIMER,
567 IOC4_SIO_IR_S2_RX_HIGH, IOC4_SIO_IR_S2_TX_EXPLICIT,
568 IOC4_OTHER_IR_S2_MEMERR,
569 (IOC4_SIO_IR_S2_TX_MT | IOC4_SIO_IR_S2_RX_FULL |
570 IOC4_SIO_IR_S2_RX_HIGH | IOC4_SIO_IR_S2_RX_TIMER |
571 IOC4_SIO_IR_S2_DELTA_DCD | IOC4_SIO_IR_S2_DELTA_CTS |
572 IOC4_SIO_IR_S2_INT | IOC4_SIO_IR_S2_TX_EXPLICIT),
573 IOC4_SIO_IR_S2, IOC4_GPPR_UART2_MODESEL_PIN,
574 },
575
576 /* Values for port 3 */
577 {
578 IOC4_SIO_IR_S3_DELTA_DCD, IOC4_SIO_IR_S3_DELTA_CTS,
579 IOC4_SIO_IR_S3_TX_MT, IOC4_SIO_IR_S3_RX_TIMER,
580 IOC4_SIO_IR_S3_RX_HIGH, IOC4_SIO_IR_S3_TX_EXPLICIT,
581 IOC4_OTHER_IR_S3_MEMERR,
582 (IOC4_SIO_IR_S3_TX_MT | IOC4_SIO_IR_S3_RX_FULL |
583 IOC4_SIO_IR_S3_RX_HIGH | IOC4_SIO_IR_S3_RX_TIMER |
584 IOC4_SIO_IR_S3_DELTA_DCD | IOC4_SIO_IR_S3_DELTA_CTS |
585 IOC4_SIO_IR_S3_INT | IOC4_SIO_IR_S3_TX_EXPLICIT),
586 IOC4_SIO_IR_S3, IOC4_GPPR_UART3_MODESEL_PIN,
587 }
588};
589
590/* A ring buffer entry */
591struct ring_entry {
592 union {
593 struct {
594 uint32_t alldata;
595 uint32_t allsc;
596 } all;
597 struct {
598 char data[4]; /* data bytes */
599 char sc[4]; /* status/control */
600 } s;
601 } u;
602};
603
604/* Test the valid bits in any of the 4 sc chars using "allsc" member */
605#define RING_ANY_VALID \
606 ((uint32_t)(IOC4_RXSB_MODEM_VALID | IOC4_RXSB_DATA_VALID) * 0x01010101)
607
608#define ring_sc u.s.sc
609#define ring_data u.s.data
610#define ring_allsc u.all.allsc
611
612/* Number of entries per ring buffer. */
613#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
614
615/* An individual ring */
616struct ring {
617 struct ring_entry entries[ENTRIES_PER_RING];
618};
619
620/* The whole enchilada */
621struct ring_buffer {
622 struct ring TX_0_OR_2;
623 struct ring RX_0_OR_2;
624 struct ring TX_1_OR_3;
625 struct ring RX_1_OR_3;
626};
627
628/* Get a ring from a port struct */
629#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
630
631/* Infinite loop detection.
632 */
633#define MAXITER 10000000
634
635/* Prototypes */
636static void receive_chars(struct uart_port *);
637static void handle_intr(void *arg, uint32_t sio_ir);
638
639/*
640 * port_is_active - determines if this port is currently active
641 * @port: ptr to soft struct for this port
642 * @uart_port: uart port to test for
643 */
644static inline int port_is_active(struct ioc4_port *port,
645 struct uart_port *uart_port)
646{
647 if (port) {
648 if ((port->ip_flags & PORT_ACTIVE)
649 && (port->ip_port == uart_port))
650 return 1;
651 }
652 return 0;
653}
654
655
656/**
657 * write_ireg - write the interrupt regs
658 * @ioc4_soft: ptr to soft struct for this port
659 * @val: value to write
660 * @which: which register
661 * @type: which ireg set
662 */
663static inline void
664write_ireg(struct ioc4_soft *ioc4_soft, uint32_t val, int which, int type)
665{
666 struct ioc4_misc_regs __iomem *mem = ioc4_soft->is_ioc4_misc_addr;
667 unsigned long flags;
668
669 spin_lock_irqsave(&ioc4_soft->is_ir_lock, flags);
670
671 switch (type) {
672 case IOC4_SIO_INTR_TYPE:
673 switch (which) {
674 case IOC4_W_IES:
675 writel(val, &mem->sio_ies.raw);
676 break;
677
678 case IOC4_W_IEC:
679 writel(val, &mem->sio_iec.raw);
680 break;
681 }
682 break;
683
684 case IOC4_OTHER_INTR_TYPE:
685 switch (which) {
686 case IOC4_W_IES:
687 writel(val, &mem->other_ies.raw);
688 break;
689
690 case IOC4_W_IEC:
691 writel(val, &mem->other_iec.raw);
692 break;
693 }
694 break;
695
696 default:
697 break;
698 }
699 spin_unlock_irqrestore(&ioc4_soft->is_ir_lock, flags);
700}
701
702/**
703 * set_baud - Baud rate setting code
704 * @port: port to set
705 * @baud: baud rate to use
706 */
707static int set_baud(struct ioc4_port *port, int baud)
708{
709 int actual_baud;
710 int diff;
711 int lcr;
712 unsigned short divisor;
713 struct ioc4_uartregs __iomem *uart;
714
715 divisor = SER_DIVISOR(baud, port->ip_pci_bus_speed);
716 if (!divisor)
717 return 1;
718 actual_baud = DIVISOR_TO_BAUD(divisor, port->ip_pci_bus_speed);
719
720 diff = actual_baud - baud;
721 if (diff < 0)
722 diff = -diff;
723
724 /* If we're within 1%, we've found a match */
725 if (diff * 100 > actual_baud)
726 return 1;
727
728 uart = port->ip_uart_regs;
729 lcr = readb(&uart->i4u_lcr);
730 writeb(lcr | UART_LCR_DLAB, &uart->i4u_lcr);
731 writeb((unsigned char)divisor, &uart->i4u_dll);
732 writeb((unsigned char)(divisor >> 8), &uart->i4u_dlm);
733 writeb(lcr, &uart->i4u_lcr);
734 return 0;
735}
736
737
738/**
739 * get_ioc4_port - given a uart port, return the control structure
740 * @port: uart port
741 * @set: set this port as current
742 */
743static struct ioc4_port *get_ioc4_port(struct uart_port *the_port, int set)
744{
745 struct ioc4_driver_data *idd = dev_get_drvdata(the_port->dev);
746 struct ioc4_control *control = idd->idd_serial_data;
747 struct ioc4_port *port;
748 int port_num, port_type;
749
750 if (control) {
751 for ( port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS;
752 port_num++ ) {
753 port = control->ic_port[port_num].icp_port;
754 if (!port)
755 continue;
756 for (port_type = UART_PORT_MIN;
757 port_type < UART_PORT_COUNT;
758 port_type++) {
759 if (the_port == port->ip_all_ports
760 [port_type]) {
761 /* set local copy */
762 if (set) {
763 port->ip_port = the_port;
764 }
765 return port;
766 }
767 }
768 }
769 }
770 return NULL;
771}
772
773/* The IOC4 hardware provides no atomic way to determine if interrupts
774 * are pending since two reads are required to do so. The handler must
775 * read the SIO_IR and the SIO_IES, and take the logical and of the
776 * two. When this value is zero, all interrupts have been serviced and
777 * the handler may return.
778 *
779 * This has the unfortunate "hole" that, if some other CPU or
780 * some other thread or some higher level interrupt manages to
781 * modify SIO_IE between our reads of SIO_IR and SIO_IE, we may
782 * think we have observed SIO_IR&SIO_IE==0 when in fact this
783 * condition never really occurred.
784 *
785 * To solve this, we use a simple spinlock that must be held
786 * whenever modifying SIO_IE; holding this lock while observing
787 * both SIO_IR and SIO_IE guarantees that we do not falsely
788 * conclude that no enabled interrupts are pending.
789 */
790
791static inline uint32_t
792pending_intrs(struct ioc4_soft *soft, int type)
793{
794 struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
795 unsigned long flag;
796 uint32_t intrs = 0;
797
798 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
799 || (type == IOC4_OTHER_INTR_TYPE)));
800
801 spin_lock_irqsave(&soft->is_ir_lock, flag);
802
803 switch (type) {
804 case IOC4_SIO_INTR_TYPE:
805 intrs = readl(&mem->sio_ir.raw) & readl(&mem->sio_ies.raw);
806 break;
807
808 case IOC4_OTHER_INTR_TYPE:
809 intrs = readl(&mem->other_ir.raw) & readl(&mem->other_ies.raw);
810
811 /* Don't process any ATA interrupte */
812 intrs &= ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
813 break;
814
815 default:
816 break;
817 }
818 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
819 return intrs;
820}
821
822/**
823 * port_init - Initialize the sio and ioc4 hardware for a given port
824 * called per port from attach...
825 * @port: port to initialize
826 */
827static int inline port_init(struct ioc4_port *port)
828{
829 uint32_t sio_cr;
830 struct hooks *hooks = port->ip_hooks;
831 struct ioc4_uartregs __iomem *uart;
832
833 /* Idle the IOC4 serial interface */
834 writel(IOC4_SSCR_RESET, &port->ip_serial_regs->sscr);
835
836 /* Wait until any pending bus activity for this port has ceased */
837 do
838 sio_cr = readl(&port->ip_mem->sio_cr.raw);
839 while (!(sio_cr & IOC4_SIO_CR_SIO_DIAG_IDLE));
840
841 /* Finish reset sequence */
842 writel(0, &port->ip_serial_regs->sscr);
843
844 /* Once RESET is done, reload cached tx_prod and rx_cons values
845 * and set rings to empty by making prod == cons
846 */
847 port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
848 writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
849 port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
850 writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
851
852 /* Disable interrupts for this 16550 */
853 uart = port->ip_uart_regs;
854 writeb(0, &uart->i4u_lcr);
855 writeb(0, &uart->i4u_ier);
856
857 /* Set the default baud */
858 set_baud(port, port->ip_baud);
859
860 /* Set line control to 8 bits no parity */
861 writeb(UART_LCR_WLEN8 | 0, &uart->i4u_lcr);
862 /* UART_LCR_STOP == 1 stop */
863
864 /* Enable the FIFOs */
865 writeb(UART_FCR_ENABLE_FIFO, &uart->i4u_fcr);
866 /* then reset 16550 FIFOs */
867 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
868 &uart->i4u_fcr);
869
870 /* Clear modem control register */
871 writeb(0, &uart->i4u_mcr);
872
873 /* Clear deltas in modem status register */
874 readb(&uart->i4u_msr);
875
876 /* Only do this once per port pair */
877 if (port->ip_hooks == &hooks_array[0]
878 || port->ip_hooks == &hooks_array[2]) {
879 unsigned long ring_pci_addr;
880 uint32_t __iomem *sbbr_l;
881 uint32_t __iomem *sbbr_h;
882
883 if (port->ip_hooks == &hooks_array[0]) {
884 sbbr_l = &port->ip_serial->sbbr01_l;
885 sbbr_h = &port->ip_serial->sbbr01_h;
886 } else {
887 sbbr_l = &port->ip_serial->sbbr23_l;
888 sbbr_h = &port->ip_serial->sbbr23_h;
889 }
890
891 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
892 DPRINT_CONFIG(("%s: ring_pci_addr 0x%lx\n",
893 __func__, ring_pci_addr));
894
895 writel((unsigned int)((uint64_t)ring_pci_addr >> 32), sbbr_h);
896 writel((unsigned int)ring_pci_addr | IOC4_BUF_SIZE_BIT, sbbr_l);
897 }
898
899 /* Set the receive timeout value to 10 msec */
900 writel(IOC4_SRTR_HZ / 100, &port->ip_serial_regs->srtr);
901
902 /* Set rx threshold, enable DMA */
903 /* Set high water mark at 3/4 of full ring */
904 port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
905 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
906
907 /* Disable and clear all serial related interrupt bits */
908 write_ireg(port->ip_ioc4_soft, hooks->intr_clear,
909 IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
910 port->ip_ienb &= ~hooks->intr_clear;
911 writel(hooks->intr_clear, &port->ip_mem->sio_ir.raw);
912 return 0;
913}
914
915/**
916 * handle_dma_error_intr - service any pending DMA error interrupts for the
917 * given port - 2nd level called via sd_intr
918 * @arg: handler arg
919 * @other_ir: ioc4regs
920 */
921static void handle_dma_error_intr(void *arg, uint32_t other_ir)
922{
923 struct ioc4_port *port = (struct ioc4_port *)arg;
924 struct hooks *hooks = port->ip_hooks;
925 unsigned long flags;
926
927 spin_lock_irqsave(&port->ip_lock, flags);
928
929 /* ACK the interrupt */
930 writel(hooks->intr_dma_error, &port->ip_mem->other_ir.raw);
931
932 if (readl(&port->ip_mem->pci_err_addr_l.raw) & IOC4_PCI_ERR_ADDR_VLD) {
933 printk(KERN_ERR
934 "PCI error address is 0x%llx, "
935 "master is serial port %c %s\n",
936 (((uint64_t)readl(&port->ip_mem->pci_err_addr_h)
937 << 32)
938 | readl(&port->ip_mem->pci_err_addr_l.raw))
939 & IOC4_PCI_ERR_ADDR_ADDR_MSK, '1' +
940 ((char)(readl(&port->ip_mem->pci_err_addr_l.raw) &
941 IOC4_PCI_ERR_ADDR_MST_NUM_MSK) >> 1),
942 (readl(&port->ip_mem->pci_err_addr_l.raw)
943 & IOC4_PCI_ERR_ADDR_MST_TYP_MSK)
944 ? "RX" : "TX");
945
946 if (readl(&port->ip_mem->pci_err_addr_l.raw)
947 & IOC4_PCI_ERR_ADDR_MUL_ERR) {
948 printk(KERN_ERR
949 "Multiple errors occurred\n");
950 }
951 }
952 spin_unlock_irqrestore(&port->ip_lock, flags);
953
954 /* Re-enable DMA error interrupts */
955 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error, IOC4_W_IES,
956 IOC4_OTHER_INTR_TYPE);
957}
958
959/**
960 * intr_connect - interrupt connect function
961 * @soft: soft struct for this card
962 * @type: interrupt type
963 * @intrbits: bit pattern to set
964 * @intr: handler function
965 * @info: handler arg
966 */
967static void
968intr_connect(struct ioc4_soft *soft, int type,
969 uint32_t intrbits, ioc4_intr_func_f * intr, void *info)
970{
971 int i;
972 struct ioc4_intr_info *intr_ptr;
973
974 BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
975 || (type == IOC4_OTHER_INTR_TYPE)));
976
977 i = atomic_inc(&soft-> is_intr_type[type].is_num_intrs) - 1;
978 BUG_ON(!(i < MAX_IOC4_INTR_ENTS || (printk("i %d\n", i), 0)));
979
980 /* Save off the lower level interrupt handler */
981 intr_ptr = &soft->is_intr_type[type].is_intr_info[i];
982 intr_ptr->sd_bits = intrbits;
983 intr_ptr->sd_intr = intr;
984 intr_ptr->sd_info = info;
985}
986
987/**
988 * ioc4_intr - Top level IOC4 interrupt handler.
989 * @irq: irq value
990 * @arg: handler arg
991 */
992
993static irqreturn_t ioc4_intr(int irq, void *arg)
994{
995 struct ioc4_soft *soft;
996 uint32_t this_ir, this_mir;
997 int xx, num_intrs = 0;
998 int intr_type;
999 int handled = 0;
1000 struct ioc4_intr_info *intr_info;
1001
1002 soft = arg;
1003 for (intr_type = 0; intr_type < IOC4_NUM_INTR_TYPES; intr_type++) {
1004 num_intrs = (int)atomic_read(
1005 &soft->is_intr_type[intr_type].is_num_intrs);
1006
1007 this_mir = this_ir = pending_intrs(soft, intr_type);
1008
1009 /* Farm out the interrupt to the various drivers depending on
1010 * which interrupt bits are set.
1011 */
1012 for (xx = 0; xx < num_intrs; xx++) {
1013 intr_info = &soft->is_intr_type[intr_type].is_intr_info[xx];
1014 if ((this_mir = this_ir & intr_info->sd_bits)) {
1015 /* Disable owned interrupts, call handler */
1016 handled++;
1017 write_ireg(soft, intr_info->sd_bits, IOC4_W_IEC,
1018 intr_type);
1019 intr_info->sd_intr(intr_info->sd_info, this_mir);
1020 this_ir &= ~this_mir;
1021 }
1022 }
1023 }
1024#ifdef DEBUG_INTERRUPTS
1025 {
1026 struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
1027 unsigned long flag;
1028
1029 spin_lock_irqsave(&soft->is_ir_lock, flag);
1030 printk ("%s : %d : mem 0x%p sio_ir 0x%x sio_ies 0x%x "
1031 "other_ir 0x%x other_ies 0x%x mask 0x%x\n",
1032 __func__, __LINE__,
1033 (void *)mem, readl(&mem->sio_ir.raw),
1034 readl(&mem->sio_ies.raw),
1035 readl(&mem->other_ir.raw),
1036 readl(&mem->other_ies.raw),
1037 IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
1038 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
1039 }
1040#endif
1041 return handled ? IRQ_HANDLED : IRQ_NONE;
1042}
1043
1044/**
1045 * ioc4_attach_local - Device initialization.
1046 * Called at *_attach() time for each
1047 * IOC4 with serial ports in the system.
1048 * @idd: Master module data for this IOC4
1049 */
1050static int inline ioc4_attach_local(struct ioc4_driver_data *idd)
1051{
1052 struct ioc4_port *port;
1053 struct ioc4_port *ports[IOC4_NUM_SERIAL_PORTS];
1054 int port_number;
1055 uint16_t ioc4_revid_min = 62;
1056 uint16_t ioc4_revid;
1057 struct pci_dev *pdev = idd->idd_pdev;
1058 struct ioc4_control* control = idd->idd_serial_data;
1059 struct ioc4_soft *soft = control->ic_soft;
1060 void __iomem *ioc4_misc = idd->idd_misc_regs;
1061 void __iomem *ioc4_serial = soft->is_ioc4_serial_addr;
1062
1063 /* IOC4 firmware must be at least rev 62 */
1064 pci_read_config_word(pdev, PCI_COMMAND_SPECIAL, &ioc4_revid);
1065
1066 printk(KERN_INFO "IOC4 firmware revision %d\n", ioc4_revid);
1067 if (ioc4_revid < ioc4_revid_min) {
1068 printk(KERN_WARNING
1069 "IOC4 serial not supported on firmware rev %d, "
1070 "please upgrade to rev %d or higher\n",
1071 ioc4_revid, ioc4_revid_min);
1072 return -EPERM;
1073 }
1074 BUG_ON(ioc4_misc == NULL);
1075 BUG_ON(ioc4_serial == NULL);
1076
1077 /* Create port structures for each port */
1078 for (port_number = 0; port_number < IOC4_NUM_SERIAL_PORTS;
1079 port_number++) {
1080 port = kzalloc(sizeof(struct ioc4_port), GFP_KERNEL);
1081 if (!port) {
1082 printk(KERN_WARNING
1083 "IOC4 serial memory not available for port\n");
1084 return -ENOMEM;
1085 }
1086 spin_lock_init(&port->ip_lock);
1087
1088 /* we need to remember the previous ones, to point back to
1089 * them farther down - setting up the ring buffers.
1090 */
1091 ports[port_number] = port;
1092
1093 /* Allocate buffers and jumpstart the hardware. */
1094 control->ic_port[port_number].icp_port = port;
1095 port->ip_ioc4_soft = soft;
1096 port->ip_pdev = pdev;
1097 port->ip_ienb = 0;
1098 /* Use baud rate calculations based on detected PCI
1099 * bus speed. Simply test whether the PCI clock is
1100 * running closer to 66MHz or 33MHz.
1101 */
1102 if (idd->count_period/IOC4_EXTINT_COUNT_DIVISOR < 20) {
1103 port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_66;
1104 } else {
1105 port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_33;
1106 }
1107 port->ip_baud = 9600;
1108 port->ip_control = control;
1109 port->ip_mem = ioc4_misc;
1110 port->ip_serial = ioc4_serial;
1111
1112 /* point to the right hook */
1113 port->ip_hooks = &hooks_array[port_number];
1114
1115 /* Get direct hooks to the serial regs and uart regs
1116 * for this port
1117 */
1118 switch (port_number) {
1119 case 0:
1120 port->ip_serial_regs = &(port->ip_serial->port_0);
1121 port->ip_uart_regs = &(port->ip_serial->uart_0);
1122 break;
1123 case 1:
1124 port->ip_serial_regs = &(port->ip_serial->port_1);
1125 port->ip_uart_regs = &(port->ip_serial->uart_1);
1126 break;
1127 case 2:
1128 port->ip_serial_regs = &(port->ip_serial->port_2);
1129 port->ip_uart_regs = &(port->ip_serial->uart_2);
1130 break;
1131 default:
1132 case 3:
1133 port->ip_serial_regs = &(port->ip_serial->port_3);
1134 port->ip_uart_regs = &(port->ip_serial->uart_3);
1135 break;
1136 }
1137
1138 /* ring buffers are 1 to a pair of ports */
1139 if (port_number && (port_number & 1)) {
1140 /* odd use the evens buffer */
1141 port->ip_dma_ringbuf =
1142 ports[port_number - 1]->ip_dma_ringbuf;
1143 port->ip_cpu_ringbuf =
1144 ports[port_number - 1]->ip_cpu_ringbuf;
1145 port->ip_inring = RING(port, RX_1_OR_3);
1146 port->ip_outring = RING(port, TX_1_OR_3);
1147
1148 } else {
1149 if (port->ip_dma_ringbuf == 0) {
1150 port->ip_cpu_ringbuf = pci_alloc_consistent
1151 (pdev, TOTAL_RING_BUF_SIZE,
1152 &port->ip_dma_ringbuf);
1153
1154 }
1155 BUG_ON(!((((int64_t)port->ip_dma_ringbuf) &
1156 (TOTAL_RING_BUF_SIZE - 1)) == 0));
1157 DPRINT_CONFIG(("%s : ip_cpu_ringbuf 0x%p "
1158 "ip_dma_ringbuf 0x%p\n",
1159 __func__,
1160 (void *)port->ip_cpu_ringbuf,
1161 (void *)port->ip_dma_ringbuf));
1162 port->ip_inring = RING(port, RX_0_OR_2);
1163 port->ip_outring = RING(port, TX_0_OR_2);
1164 }
1165 DPRINT_CONFIG(("%s : port %d [addr 0x%p] control 0x%p",
1166 __func__,
1167 port_number, (void *)port, (void *)control));
1168 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
1169 (void *)port->ip_serial_regs,
1170 (void *)port->ip_uart_regs));
1171
1172 /* Initialize the hardware for IOC4 */
1173 port_init(port);
1174
1175 DPRINT_CONFIG(("%s: port_number %d port 0x%p inring 0x%p "
1176 "outring 0x%p\n",
1177 __func__,
1178 port_number, (void *)port,
1179 (void *)port->ip_inring,
1180 (void *)port->ip_outring));
1181
1182 /* Attach interrupt handlers */
1183 intr_connect(soft, IOC4_SIO_INTR_TYPE,
1184 GET_SIO_IR(port_number),
1185 handle_intr, port);
1186
1187 intr_connect(soft, IOC4_OTHER_INTR_TYPE,
1188 GET_OTHER_IR(port_number),
1189 handle_dma_error_intr, port);
1190 }
1191 return 0;
1192}
1193
1194/**
1195 * enable_intrs - enable interrupts
1196 * @port: port to enable
1197 * @mask: mask to use
1198 */
1199static void enable_intrs(struct ioc4_port *port, uint32_t mask)
1200{
1201 struct hooks *hooks = port->ip_hooks;
1202
1203 if ((port->ip_ienb & mask) != mask) {
1204 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IES,
1205 IOC4_SIO_INTR_TYPE);
1206 port->ip_ienb |= mask;
1207 }
1208
1209 if (port->ip_ienb)
1210 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1211 IOC4_W_IES, IOC4_OTHER_INTR_TYPE);
1212}
1213
1214/**
1215 * local_open - local open a port
1216 * @port: port to open
1217 */
1218static inline int local_open(struct ioc4_port *port)
1219{
1220 int spiniter = 0;
1221
1222 port->ip_flags = PORT_ACTIVE;
1223
1224 /* Pause the DMA interface if necessary */
1225 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1226 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1227 &port->ip_serial_regs->sscr);
1228 while((readl(&port->ip_serial_regs-> sscr)
1229 & IOC4_SSCR_PAUSE_STATE) == 0) {
1230 spiniter++;
1231 if (spiniter > MAXITER) {
1232 port->ip_flags = PORT_INACTIVE;
1233 return -1;
1234 }
1235 }
1236 }
1237
1238 /* Reset the input fifo. If the uart received chars while the port
1239 * was closed and DMA is not enabled, the uart may have a bunch of
1240 * chars hanging around in its rx fifo which will not be discarded
1241 * by rclr in the upper layer. We must get rid of them here.
1242 */
1243 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1244 &port->ip_uart_regs->i4u_fcr);
1245
1246 writeb(UART_LCR_WLEN8, &port->ip_uart_regs->i4u_lcr);
1247 /* UART_LCR_STOP == 1 stop */
1248
1249 /* Re-enable DMA, set default threshold to intr whenever there is
1250 * data available.
1251 */
1252 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1253 port->ip_sscr |= 1; /* default threshold */
1254
1255 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
1256 * flag if it was set above
1257 */
1258 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1259 port->ip_tx_lowat = 1;
1260 return 0;
1261}
1262
1263/**
1264 * set_rx_timeout - Set rx timeout and threshold values.
1265 * @port: port to use
1266 * @timeout: timeout value in ticks
1267 */
1268static inline int set_rx_timeout(struct ioc4_port *port, int timeout)
1269{
1270 int threshold;
1271
1272 port->ip_rx_timeout = timeout;
1273
1274 /* Timeout is in ticks. Let's figure out how many chars we
1275 * can receive at the current baud rate in that interval
1276 * and set the rx threshold to that amount. There are 4 chars
1277 * per ring entry, so we'll divide the number of chars that will
1278 * arrive in timeout by 4.
1279 * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
1280 */
1281 threshold = timeout * port->ip_baud / 4000;
1282 if (threshold == 0)
1283 threshold = 1; /* otherwise we'll intr all the time! */
1284
1285 if ((unsigned)threshold > (unsigned)IOC4_SSCR_RX_THRESHOLD)
1286 return 1;
1287
1288 port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1289 port->ip_sscr |= threshold;
1290
1291 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1292
1293 /* Now set the rx timeout to the given value
1294 * again timeout * IOC4_SRTR_HZ / HZ
1295 */
1296 timeout = timeout * IOC4_SRTR_HZ / 100;
1297 if (timeout > IOC4_SRTR_CNT)
1298 timeout = IOC4_SRTR_CNT;
1299
1300 writel(timeout, &port->ip_serial_regs->srtr);
1301 return 0;
1302}
1303
1304/**
1305 * config_port - config the hardware
1306 * @port: port to config
1307 * @baud: baud rate for the port
1308 * @byte_size: data size
1309 * @stop_bits: number of stop bits
1310 * @parenb: parity enable ?
1311 * @parodd: odd parity ?
1312 */
1313static inline int
1314config_port(struct ioc4_port *port,
1315 int baud, int byte_size, int stop_bits, int parenb, int parodd)
1316{
1317 char lcr, sizebits;
1318 int spiniter = 0;
1319
1320 DPRINT_CONFIG(("%s: baud %d byte_size %d stop %d parenb %d parodd %d\n",
1321 __func__, baud, byte_size, stop_bits, parenb, parodd));
1322
1323 if (set_baud(port, baud))
1324 return 1;
1325
1326 switch (byte_size) {
1327 case 5:
1328 sizebits = UART_LCR_WLEN5;
1329 break;
1330 case 6:
1331 sizebits = UART_LCR_WLEN6;
1332 break;
1333 case 7:
1334 sizebits = UART_LCR_WLEN7;
1335 break;
1336 case 8:
1337 sizebits = UART_LCR_WLEN8;
1338 break;
1339 default:
1340 return 1;
1341 }
1342
1343 /* Pause the DMA interface if necessary */
1344 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1345 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1346 &port->ip_serial_regs->sscr);
1347 while((readl(&port->ip_serial_regs->sscr)
1348 & IOC4_SSCR_PAUSE_STATE) == 0) {
1349 spiniter++;
1350 if (spiniter > MAXITER)
1351 return -1;
1352 }
1353 }
1354
1355 /* Clear relevant fields in lcr */
1356 lcr = readb(&port->ip_uart_regs->i4u_lcr);
1357 lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
1358 UART_LCR_PARITY | LCR_MASK_STOP_BITS);
1359
1360 /* Set byte size in lcr */
1361 lcr |= sizebits;
1362
1363 /* Set parity */
1364 if (parenb) {
1365 lcr |= UART_LCR_PARITY;
1366 if (!parodd)
1367 lcr |= UART_LCR_EPAR;
1368 }
1369
1370 /* Set stop bits */
1371 if (stop_bits)
1372 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
1373
1374 writeb(lcr, &port->ip_uart_regs->i4u_lcr);
1375
1376 /* Re-enable the DMA interface if necessary */
1377 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1378 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1379 }
1380 port->ip_baud = baud;
1381
1382 /* When we get within this number of ring entries of filling the
1383 * entire ring on tx, place an EXPLICIT intr to generate a lowat
1384 * notification when output has drained.
1385 */
1386 port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
1387 if (port->ip_tx_lowat == 0)
1388 port->ip_tx_lowat = 1;
1389
1390 set_rx_timeout(port, 2);
1391
1392 return 0;
1393}
1394
1395/**
1396 * do_write - Write bytes to the port. Returns the number of bytes
1397 * actually written. Called from transmit_chars
1398 * @port: port to use
1399 * @buf: the stuff to write
1400 * @len: how many bytes in 'buf'
1401 */
1402static inline int do_write(struct ioc4_port *port, char *buf, int len)
1403{
1404 int prod_ptr, cons_ptr, total = 0;
1405 struct ring *outring;
1406 struct ring_entry *entry;
1407 struct hooks *hooks = port->ip_hooks;
1408
1409 BUG_ON(!(len >= 0));
1410
1411 prod_ptr = port->ip_tx_prod;
1412 cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
1413 outring = port->ip_outring;
1414
1415 /* Maintain a 1-entry red-zone. The ring buffer is full when
1416 * (cons - prod) % ring_size is 1. Rather than do this subtraction
1417 * in the body of the loop, I'll do it now.
1418 */
1419 cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
1420
1421 /* Stuff the bytes into the output */
1422 while ((prod_ptr != cons_ptr) && (len > 0)) {
1423 int xx;
1424
1425 /* Get 4 bytes (one ring entry) at a time */
1426 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
1427
1428 /* Invalidate all entries */
1429 entry->ring_allsc = 0;
1430
1431 /* Copy in some bytes */
1432 for (xx = 0; (xx < 4) && (len > 0); xx++) {
1433 entry->ring_data[xx] = *buf++;
1434 entry->ring_sc[xx] = IOC4_TXCB_VALID;
1435 len--;
1436 total++;
1437 }
1438
1439 /* If we are within some small threshold of filling up the
1440 * entire ring buffer, we must place an EXPLICIT intr here
1441 * to generate a lowat interrupt in case we subsequently
1442 * really do fill up the ring and the caller goes to sleep.
1443 * No need to place more than one though.
1444 */
1445 if (!(port->ip_flags & LOWAT_WRITTEN) &&
1446 ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
1447 <= port->ip_tx_lowat
1448 * (int)sizeof(struct ring_entry)) {
1449 port->ip_flags |= LOWAT_WRITTEN;
1450 entry->ring_sc[0] |= IOC4_TXCB_INT_WHEN_DONE;
1451 }
1452
1453 /* Go on to next entry */
1454 prod_ptr += sizeof(struct ring_entry);
1455 prod_ptr &= PROD_CONS_MASK;
1456 }
1457
1458 /* If we sent something, start DMA if necessary */
1459 if (total > 0 && !(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
1460 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1461 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1462 }
1463
1464 /* Store the new producer pointer. If tx is disabled, we stuff the
1465 * data into the ring buffer, but we don't actually start tx.
1466 */
1467 if (!uart_tx_stopped(port->ip_port)) {
1468 writel(prod_ptr, &port->ip_serial_regs->stpir);
1469
1470 /* If we are now transmitting, enable tx_mt interrupt so we
1471 * can disable DMA if necessary when the tx finishes.
1472 */
1473 if (total > 0)
1474 enable_intrs(port, hooks->intr_tx_mt);
1475 }
1476 port->ip_tx_prod = prod_ptr;
1477 return total;
1478}
1479
1480/**
1481 * disable_intrs - disable interrupts
1482 * @port: port to enable
1483 * @mask: mask to use
1484 */
1485static void disable_intrs(struct ioc4_port *port, uint32_t mask)
1486{
1487 struct hooks *hooks = port->ip_hooks;
1488
1489 if (port->ip_ienb & mask) {
1490 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IEC,
1491 IOC4_SIO_INTR_TYPE);
1492 port->ip_ienb &= ~mask;
1493 }
1494
1495 if (!port->ip_ienb)
1496 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1497 IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
1498}
1499
1500/**
1501 * set_notification - Modify event notification
1502 * @port: port to use
1503 * @mask: events mask
1504 * @set_on: set ?
1505 */
1506static int set_notification(struct ioc4_port *port, int mask, int set_on)
1507{
1508 struct hooks *hooks = port->ip_hooks;
1509 uint32_t intrbits, sscrbits;
1510
1511 BUG_ON(!mask);
1512
1513 intrbits = sscrbits = 0;
1514
1515 if (mask & N_DATA_READY)
1516 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
1517 if (mask & N_OUTPUT_LOWAT)
1518 intrbits |= hooks->intr_tx_explicit;
1519 if (mask & N_DDCD) {
1520 intrbits |= hooks->intr_delta_dcd;
1521 sscrbits |= IOC4_SSCR_RX_RING_DCD;
1522 }
1523 if (mask & N_DCTS)
1524 intrbits |= hooks->intr_delta_cts;
1525
1526 if (set_on) {
1527 enable_intrs(port, intrbits);
1528 port->ip_notify |= mask;
1529 port->ip_sscr |= sscrbits;
1530 } else {
1531 disable_intrs(port, intrbits);
1532 port->ip_notify &= ~mask;
1533 port->ip_sscr &= ~sscrbits;
1534 }
1535
1536 /* We require DMA if either DATA_READY or DDCD notification is
1537 * currently requested. If neither of these is requested and
1538 * there is currently no tx in progress, DMA may be disabled.
1539 */
1540 if (port->ip_notify & (N_DATA_READY | N_DDCD))
1541 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1542 else if (!(port->ip_ienb & hooks->intr_tx_mt))
1543 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
1544
1545 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1546 return 0;
1547}
1548
1549/**
1550 * set_mcr - set the master control reg
1551 * @the_port: port to use
1552 * @mask1: mcr mask
1553 * @mask2: shadow mask
1554 */
1555static inline int set_mcr(struct uart_port *the_port,
1556 int mask1, int mask2)
1557{
1558 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1559 uint32_t shadow;
1560 int spiniter = 0;
1561 char mcr;
1562
1563 if (!port)
1564 return -1;
1565
1566 /* Pause the DMA interface if necessary */
1567 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1568 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1569 &port->ip_serial_regs->sscr);
1570 while ((readl(&port->ip_serial_regs->sscr)
1571 & IOC4_SSCR_PAUSE_STATE) == 0) {
1572 spiniter++;
1573 if (spiniter > MAXITER)
1574 return -1;
1575 }
1576 }
1577 shadow = readl(&port->ip_serial_regs->shadow);
1578 mcr = (shadow & 0xff000000) >> 24;
1579
1580 /* Set new value */
1581 mcr |= mask1;
1582 shadow |= mask2;
1583
1584 writeb(mcr, &port->ip_uart_regs->i4u_mcr);
1585 writel(shadow, &port->ip_serial_regs->shadow);
1586
1587 /* Re-enable the DMA interface if necessary */
1588 if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1589 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1590 }
1591 return 0;
1592}
1593
1594/**
1595 * ioc4_set_proto - set the protocol for the port
1596 * @port: port to use
1597 * @proto: protocol to use
1598 */
1599static int ioc4_set_proto(struct ioc4_port *port, int proto)
1600{
1601 struct hooks *hooks = port->ip_hooks;
1602
1603 switch (proto) {
1604 case PROTO_RS232:
1605 /* Clear the appropriate GIO pin */
1606 writel(0, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
1607 break;
1608
1609 case PROTO_RS422:
1610 /* Set the appropriate GIO pin */
1611 writel(1, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
1612 break;
1613
1614 default:
1615 return 1;
1616 }
1617 return 0;
1618}
1619
1620/**
1621 * transmit_chars - upper level write, called with ip_lock
1622 * @the_port: port to write
1623 */
1624static void transmit_chars(struct uart_port *the_port)
1625{
1626 int xmit_count, tail, head;
1627 int result;
1628 char *start;
1629 struct tty_struct *tty;
1630 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1631 struct uart_state *state;
1632
1633 if (!the_port)
1634 return;
1635 if (!port)
1636 return;
1637
1638 state = the_port->state;
1639 tty = state->port.tty;
1640
1641 if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
1642 /* Nothing to do or hw stopped */
1643 set_notification(port, N_ALL_OUTPUT, 0);
1644 return;
1645 }
1646
1647 head = state->xmit.head;
1648 tail = state->xmit.tail;
1649 start = (char *)&state->xmit.buf[tail];
1650
1651 /* write out all the data or until the end of the buffer */
1652 xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
1653 if (xmit_count > 0) {
1654 result = do_write(port, start, xmit_count);
1655 if (result > 0) {
1656 /* booking */
1657 xmit_count -= result;
1658 the_port->icount.tx += result;
1659 /* advance the pointers */
1660 tail += result;
1661 tail &= UART_XMIT_SIZE - 1;
1662 state->xmit.tail = tail;
1663 start = (char *)&state->xmit.buf[tail];
1664 }
1665 }
1666 if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
1667 uart_write_wakeup(the_port);
1668
1669 if (uart_circ_empty(&state->xmit)) {
1670 set_notification(port, N_OUTPUT_LOWAT, 0);
1671 } else {
1672 set_notification(port, N_OUTPUT_LOWAT, 1);
1673 }
1674}
1675
1676/**
1677 * ioc4_change_speed - change the speed of the port
1678 * @the_port: port to change
1679 * @new_termios: new termios settings
1680 * @old_termios: old termios settings
1681 */
1682static void
1683ioc4_change_speed(struct uart_port *the_port,
1684 struct ktermios *new_termios, struct ktermios *old_termios)
1685{
1686 struct ioc4_port *port = get_ioc4_port(the_port, 0);
1687 int baud, bits;
1688 unsigned cflag, iflag;
1689 int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
1690 struct uart_state *state = the_port->state;
1691
1692 cflag = new_termios->c_cflag;
1693 iflag = new_termios->c_iflag;
1694
1695 switch (cflag & CSIZE) {
1696 case CS5:
1697 new_data = 5;
1698 bits = 7;
1699 break;
1700 case CS6:
1701 new_data = 6;
1702 bits = 8;
1703 break;
1704 case CS7:
1705 new_data = 7;
1706 bits = 9;
1707 break;
1708 case CS8:
1709 new_data = 8;
1710 bits = 10;
1711 break;
1712 default:
1713 /* cuz we always need a default ... */
1714 new_data = 5;
1715 bits = 7;
1716 break;
1717 }
1718 if (cflag & CSTOPB) {
1719 bits++;
1720 new_stop = 1;
1721 }
1722 if (cflag & PARENB) {
1723 bits++;
1724 new_parity_enable = 1;
1725 if (cflag & PARODD)
1726 new_parity = 1;
1727 }
1728 baud = uart_get_baud_rate(the_port, new_termios, old_termios,
1729 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
1730 DPRINT_CONFIG(("%s: returned baud %d\n", __func__, baud));
1731
1732 /* default is 9600 */
1733 if (!baud)
1734 baud = 9600;
1735
1736 if (!the_port->fifosize)
1737 the_port->fifosize = IOC4_FIFO_CHARS;
1738 the_port->timeout = ((the_port->fifosize * HZ * bits) / (baud / 10));
1739 the_port->timeout += HZ / 50; /* Add .02 seconds of slop */
1740
1741 the_port->ignore_status_mask = N_ALL_INPUT;
1742
1743 state->port.tty->low_latency = 1;
1744
1745 if (iflag & IGNPAR)
1746 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1747 | N_FRAMING_ERROR);
1748 if (iflag & IGNBRK) {
1749 the_port->ignore_status_mask &= ~N_BREAK;
1750 if (iflag & IGNPAR)
1751 the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1752 }
1753 if (!(cflag & CREAD)) {
1754 /* ignore everything */
1755 the_port->ignore_status_mask &= ~N_DATA_READY;
1756 }
1757
1758 if (cflag & CRTSCTS) {
1759 port->ip_sscr |= IOC4_SSCR_HFC_EN;
1760 }
1761 else {
1762 port->ip_sscr &= ~IOC4_SSCR_HFC_EN;
1763 }
1764 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1765
1766 /* Set the configuration and proper notification call */
1767 DPRINT_CONFIG(("%s : port 0x%p cflag 0%o "
1768 "config_port(baud %d data %d stop %d p enable %d parity %d),"
1769 " notification 0x%x\n",
1770 __func__, (void *)port, cflag, baud, new_data, new_stop,
1771 new_parity_enable, new_parity, the_port->ignore_status_mask));
1772
1773 if ((config_port(port, baud, /* baud */
1774 new_data, /* byte size */
1775 new_stop, /* stop bits */
1776 new_parity_enable, /* set parity */
1777 new_parity)) >= 0) { /* parity 1==odd */
1778 set_notification(port, the_port->ignore_status_mask, 1);
1779 }
1780}
1781
1782/**
1783 * ic4_startup_local - Start up the serial port - returns >= 0 if no errors
1784 * @the_port: Port to operate on
1785 */
1786static inline int ic4_startup_local(struct uart_port *the_port)
1787{
1788 struct ioc4_port *port;
1789 struct uart_state *state;
1790
1791 if (!the_port)
1792 return -1;
1793
1794 port = get_ioc4_port(the_port, 0);
1795 if (!port)
1796 return -1;
1797
1798 state = the_port->state;
1799
1800 local_open(port);
1801
1802 /* set the protocol - mapbase has the port type */
1803 ioc4_set_proto(port, the_port->mapbase);
1804
1805 /* set the speed of the serial port */
1806 ioc4_change_speed(the_port, state->port.tty->termios,
1807 (struct ktermios *)0);
1808
1809 return 0;
1810}
1811
1812/*
1813 * ioc4_cb_output_lowat - called when the output low water mark is hit
1814 * @the_port: port to output
1815 */
1816static void ioc4_cb_output_lowat(struct uart_port *the_port)
1817{
1818 unsigned long pflags;
1819
1820 /* ip_lock is set on the call here */
1821 if (the_port) {
1822 spin_lock_irqsave(&the_port->lock, pflags);
1823 transmit_chars(the_port);
1824 spin_unlock_irqrestore(&the_port->lock, pflags);
1825 }
1826}
1827
1828/**
1829 * handle_intr - service any interrupts for the given port - 2nd level
1830 * called via sd_intr
1831 * @arg: handler arg
1832 * @sio_ir: ioc4regs
1833 */
1834static void handle_intr(void *arg, uint32_t sio_ir)
1835{
1836 struct ioc4_port *port = (struct ioc4_port *)arg;
1837 struct hooks *hooks = port->ip_hooks;
1838 unsigned int rx_high_rd_aborted = 0;
1839 unsigned long flags;
1840 struct uart_port *the_port;
1841 int loop_counter;
1842
1843 /* Possible race condition here: The tx_mt interrupt bit may be
1844 * cleared without the intervention of the interrupt handler,
1845 * e.g. by a write. If the top level interrupt handler reads a
1846 * tx_mt, then some other processor does a write, starting up
1847 * output, then we come in here, see the tx_mt and stop DMA, the
1848 * output started by the other processor will hang. Thus we can
1849 * only rely on tx_mt being legitimate if it is read while the
1850 * port lock is held. Therefore this bit must be ignored in the
1851 * passed in interrupt mask which was read by the top level
1852 * interrupt handler since the port lock was not held at the time
1853 * it was read. We can only rely on this bit being accurate if it
1854 * is read while the port lock is held. So we'll clear it for now,
1855 * and reload it later once we have the port lock.
1856 */
1857 sio_ir &= ~(hooks->intr_tx_mt);
1858
1859 spin_lock_irqsave(&port->ip_lock, flags);
1860
1861 loop_counter = MAXITER; /* to avoid hangs */
1862
1863 do {
1864 uint32_t shadow;
1865
1866 if ( loop_counter-- <= 0 ) {
1867 printk(KERN_WARNING "IOC4 serial: "
1868 "possible hang condition/"
1869 "port stuck on interrupt.\n");
1870 break;
1871 }
1872
1873 /* Handle a DCD change */
1874 if (sio_ir & hooks->intr_delta_dcd) {
1875 /* ACK the interrupt */
1876 writel(hooks->intr_delta_dcd,
1877 &port->ip_mem->sio_ir.raw);
1878
1879 shadow = readl(&port->ip_serial_regs->shadow);
1880
1881 if ((port->ip_notify & N_DDCD)
1882 && (shadow & IOC4_SHADOW_DCD)
1883 && (port->ip_port)) {
1884 the_port = port->ip_port;
1885 the_port->icount.dcd = 1;
1886 wake_up_interruptible
1887 (&the_port->state->port.delta_msr_wait);
1888 } else if ((port->ip_notify & N_DDCD)
1889 && !(shadow & IOC4_SHADOW_DCD)) {
1890 /* Flag delta DCD/no DCD */
1891 port->ip_flags |= DCD_ON;
1892 }
1893 }
1894
1895 /* Handle a CTS change */
1896 if (sio_ir & hooks->intr_delta_cts) {
1897 /* ACK the interrupt */
1898 writel(hooks->intr_delta_cts,
1899 &port->ip_mem->sio_ir.raw);
1900
1901 shadow = readl(&port->ip_serial_regs->shadow);
1902
1903 if ((port->ip_notify & N_DCTS)
1904 && (port->ip_port)) {
1905 the_port = port->ip_port;
1906 the_port->icount.cts =
1907 (shadow & IOC4_SHADOW_CTS) ? 1 : 0;
1908 wake_up_interruptible
1909 (&the_port->state->port.delta_msr_wait);
1910 }
1911 }
1912
1913 /* rx timeout interrupt. Must be some data available. Put this
1914 * before the check for rx_high since servicing this condition
1915 * may cause that condition to clear.
1916 */
1917 if (sio_ir & hooks->intr_rx_timer) {
1918 /* ACK the interrupt */
1919 writel(hooks->intr_rx_timer,
1920 &port->ip_mem->sio_ir.raw);
1921
1922 if ((port->ip_notify & N_DATA_READY)
1923 && (port->ip_port)) {
1924 /* ip_lock is set on call here */
1925 receive_chars(port->ip_port);
1926 }
1927 }
1928
1929 /* rx high interrupt. Must be after rx_timer. */
1930 else if (sio_ir & hooks->intr_rx_high) {
1931 /* Data available, notify upper layer */
1932 if ((port->ip_notify & N_DATA_READY)
1933 && port->ip_port) {
1934 /* ip_lock is set on call here */
1935 receive_chars(port->ip_port);
1936 }
1937
1938 /* We can't ACK this interrupt. If receive_chars didn't
1939 * cause the condition to clear, we'll have to disable
1940 * the interrupt until the data is drained.
1941 * If the read was aborted, don't disable the interrupt
1942 * as this may cause us to hang indefinitely. An
1943 * aborted read generally means that this interrupt
1944 * hasn't been delivered to the cpu yet anyway, even
1945 * though we see it as asserted when we read the sio_ir.
1946 */
1947 if ((sio_ir = PENDING(port)) & hooks->intr_rx_high) {
1948 if ((port->ip_flags & READ_ABORTED) == 0) {
1949 port->ip_ienb &= ~hooks->intr_rx_high;
1950 port->ip_flags |= INPUT_HIGH;
1951 } else {
1952 rx_high_rd_aborted++;
1953 }
1954 }
1955 }
1956
1957 /* We got a low water interrupt: notify upper layer to
1958 * send more data. Must come before tx_mt since servicing
1959 * this condition may cause that condition to clear.
1960 */
1961 if (sio_ir & hooks->intr_tx_explicit) {
1962 port->ip_flags &= ~LOWAT_WRITTEN;
1963
1964 /* ACK the interrupt */
1965 writel(hooks->intr_tx_explicit,
1966 &port->ip_mem->sio_ir.raw);
1967
1968 if (port->ip_notify & N_OUTPUT_LOWAT)
1969 ioc4_cb_output_lowat(port->ip_port);
1970 }
1971
1972 /* Handle tx_mt. Must come after tx_explicit. */
1973 else if (sio_ir & hooks->intr_tx_mt) {
1974 /* If we are expecting a lowat notification
1975 * and we get to this point it probably means that for
1976 * some reason the tx_explicit didn't work as expected
1977 * (that can legitimately happen if the output buffer is
1978 * filled up in just the right way).
1979 * So send the notification now.
1980 */
1981 if (port->ip_notify & N_OUTPUT_LOWAT) {
1982 ioc4_cb_output_lowat(port->ip_port);
1983
1984 /* We need to reload the sio_ir since the lowat
1985 * call may have caused another write to occur,
1986 * clearing the tx_mt condition.
1987 */
1988 sio_ir = PENDING(port);
1989 }
1990
1991 /* If the tx_mt condition still persists even after the
1992 * lowat call, we've got some work to do.
1993 */
1994 if (sio_ir & hooks->intr_tx_mt) {
1995
1996 /* If we are not currently expecting DMA input,
1997 * and the transmitter has just gone idle,
1998 * there is no longer any reason for DMA, so
1999 * disable it.
2000 */
2001 if (!(port->ip_notify
2002 & (N_DATA_READY | N_DDCD))) {
2003 BUG_ON(!(port->ip_sscr
2004 & IOC4_SSCR_DMA_EN));
2005 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2006 writel(port->ip_sscr,
2007 &port->ip_serial_regs->sscr);
2008 }
2009
2010 /* Prevent infinite tx_mt interrupt */
2011 port->ip_ienb &= ~hooks->intr_tx_mt;
2012 }
2013 }
2014 sio_ir = PENDING(port);
2015
2016 /* if the read was aborted and only hooks->intr_rx_high,
2017 * clear hooks->intr_rx_high, so we do not loop forever.
2018 */
2019
2020 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
2021 sio_ir &= ~hooks->intr_rx_high;
2022 }
2023 } while (sio_ir & hooks->intr_all);
2024
2025 spin_unlock_irqrestore(&port->ip_lock, flags);
2026
2027 /* Re-enable interrupts before returning from interrupt handler.
2028 * Getting interrupted here is okay. It'll just v() our semaphore, and
2029 * we'll come through the loop again.
2030 */
2031
2032 write_ireg(port->ip_ioc4_soft, port->ip_ienb, IOC4_W_IES,
2033 IOC4_SIO_INTR_TYPE);
2034}
2035
2036/*
2037 * ioc4_cb_post_ncs - called for some basic errors
2038 * @port: port to use
2039 * @ncs: event
2040 */
2041static void ioc4_cb_post_ncs(struct uart_port *the_port, int ncs)
2042{
2043 struct uart_icount *icount;
2044
2045 icount = &the_port->icount;
2046
2047 if (ncs & NCS_BREAK)
2048 icount->brk++;
2049 if (ncs & NCS_FRAMING)
2050 icount->frame++;
2051 if (ncs & NCS_OVERRUN)
2052 icount->overrun++;
2053 if (ncs & NCS_PARITY)
2054 icount->parity++;
2055}
2056
2057/**
2058 * do_read - Read in bytes from the port. Return the number of bytes
2059 * actually read.
2060 * @the_port: port to use
2061 * @buf: place to put the stuff we read
2062 * @len: how big 'buf' is
2063 */
2064
2065static inline int do_read(struct uart_port *the_port, unsigned char *buf,
2066 int len)
2067{
2068 int prod_ptr, cons_ptr, total;
2069 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2070 struct ring *inring;
2071 struct ring_entry *entry;
2072 struct hooks *hooks = port->ip_hooks;
2073 int byte_num;
2074 char *sc;
2075 int loop_counter;
2076
2077 BUG_ON(!(len >= 0));
2078 BUG_ON(!port);
2079
2080 /* There is a nasty timing issue in the IOC4. When the rx_timer
2081 * expires or the rx_high condition arises, we take an interrupt.
2082 * At some point while servicing the interrupt, we read bytes from
2083 * the ring buffer and re-arm the rx_timer. However the rx_timer is
2084 * not started until the first byte is received *after* it is armed,
2085 * and any bytes pending in the rx construction buffers are not drained
2086 * to memory until either there are 4 bytes available or the rx_timer
2087 * expires. This leads to a potential situation where data is left
2088 * in the construction buffers forever - 1 to 3 bytes were received
2089 * after the interrupt was generated but before the rx_timer was
2090 * re-armed. At that point as long as no subsequent bytes are received
2091 * the timer will never be started and the bytes will remain in the
2092 * construction buffer forever. The solution is to execute a DRAIN
2093 * command after rearming the timer. This way any bytes received before
2094 * the DRAIN will be drained to memory, and any bytes received after
2095 * the DRAIN will start the TIMER and be drained when it expires.
2096 * Luckily, this only needs to be done when the DMA buffer is empty
2097 * since there is no requirement that this function return all
2098 * available data as long as it returns some.
2099 */
2100 /* Re-arm the timer */
2101 writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
2102
2103 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
2104 cons_ptr = port->ip_rx_cons;
2105
2106 if (prod_ptr == cons_ptr) {
2107 int reset_dma = 0;
2108
2109 /* Input buffer appears empty, do a flush. */
2110
2111 /* DMA must be enabled for this to work. */
2112 if (!(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
2113 port->ip_sscr |= IOC4_SSCR_DMA_EN;
2114 reset_dma = 1;
2115 }
2116
2117 /* Potential race condition: we must reload the srpir after
2118 * issuing the drain command, otherwise we could think the rx
2119 * buffer is empty, then take a very long interrupt, and when
2120 * we come back it's full and we wait forever for the drain to
2121 * complete.
2122 */
2123 writel(port->ip_sscr | IOC4_SSCR_RX_DRAIN,
2124 &port->ip_serial_regs->sscr);
2125 prod_ptr = readl(&port->ip_serial_regs->srpir)
2126 & PROD_CONS_MASK;
2127
2128 /* We must not wait for the DRAIN to complete unless there are
2129 * at least 8 bytes (2 ring entries) available to receive the
2130 * data otherwise the DRAIN will never complete and we'll
2131 * deadlock here.
2132 * In fact, to make things easier, I'll just ignore the flush if
2133 * there is any data at all now available.
2134 */
2135 if (prod_ptr == cons_ptr) {
2136 loop_counter = 0;
2137 while (readl(&port->ip_serial_regs->sscr) &
2138 IOC4_SSCR_RX_DRAIN) {
2139 loop_counter++;
2140 if (loop_counter > MAXITER)
2141 return -1;
2142 }
2143
2144 /* SIGH. We have to reload the prod_ptr *again* since
2145 * the drain may have caused it to change
2146 */
2147 prod_ptr = readl(&port->ip_serial_regs->srpir)
2148 & PROD_CONS_MASK;
2149 }
2150 if (reset_dma) {
2151 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2152 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
2153 }
2154 }
2155 inring = port->ip_inring;
2156 port->ip_flags &= ~READ_ABORTED;
2157
2158 total = 0;
2159 loop_counter = 0xfffff; /* to avoid hangs */
2160
2161 /* Grab bytes from the hardware */
2162 while ((prod_ptr != cons_ptr) && (len > 0)) {
2163 entry = (struct ring_entry *)((caddr_t)inring + cons_ptr);
2164
2165 if ( loop_counter-- <= 0 ) {
2166 printk(KERN_WARNING "IOC4 serial: "
2167 "possible hang condition/"
2168 "port stuck on read.\n");
2169 break;
2170 }
2171
2172 /* According to the producer pointer, this ring entry
2173 * must contain some data. But if the PIO happened faster
2174 * than the DMA, the data may not be available yet, so let's
2175 * wait until it arrives.
2176 */
2177 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2178 /* Indicate the read is aborted so we don't disable
2179 * the interrupt thinking that the consumer is
2180 * congested.
2181 */
2182 port->ip_flags |= READ_ABORTED;
2183 len = 0;
2184 break;
2185 }
2186
2187 /* Load the bytes/status out of the ring entry */
2188 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
2189 sc = &(entry->ring_sc[byte_num]);
2190
2191 /* Check for change in modem state or overrun */
2192 if ((*sc & IOC4_RXSB_MODEM_VALID)
2193 && (port->ip_notify & N_DDCD)) {
2194 /* Notify upper layer if DCD dropped */
2195
2196 if ((port->ip_flags & DCD_ON)
2197 && !(*sc & IOC4_RXSB_DCD)) {
2198
2199 /* If we have already copied some data,
2200 * return it. We'll pick up the carrier
2201 * drop on the next pass. That way we
2202 * don't throw away the data that has
2203 * already been copied back to
2204 * the caller's buffer.
2205 */
2206 if (total > 0) {
2207 len = 0;
2208 break;
2209 }
2210 port->ip_flags &= ~DCD_ON;
2211
2212 /* Turn off this notification so the
2213 * carrier drop protocol won't see it
2214 * again when it does a read.
2215 */
2216 *sc &= ~IOC4_RXSB_MODEM_VALID;
2217
2218 /* To keep things consistent, we need
2219 * to update the consumer pointer so
2220 * the next reader won't come in and
2221 * try to read the same ring entries
2222 * again. This must be done here before
2223 * the dcd change.
2224 */
2225
2226 if ((entry->ring_allsc & RING_ANY_VALID)
2227 == 0) {
2228 cons_ptr += (int)sizeof
2229 (struct ring_entry);
2230 cons_ptr &= PROD_CONS_MASK;
2231 }
2232 writel(cons_ptr,
2233 &port->ip_serial_regs->srcir);
2234 port->ip_rx_cons = cons_ptr;
2235
2236 /* Notify upper layer of carrier drop */
2237 if ((port->ip_notify & N_DDCD)
2238 && port->ip_port) {
2239 the_port->icount.dcd = 0;
2240 wake_up_interruptible
2241 (&the_port->state->
2242 port.delta_msr_wait);
2243 }
2244
2245 /* If we had any data to return, we
2246 * would have returned it above.
2247 */
2248 return 0;
2249 }
2250 }
2251 if (*sc & IOC4_RXSB_MODEM_VALID) {
2252 /* Notify that an input overrun occurred */
2253 if ((*sc & IOC4_RXSB_OVERRUN)
2254 && (port->ip_notify & N_OVERRUN_ERROR)) {
2255 ioc4_cb_post_ncs(the_port, NCS_OVERRUN);
2256 }
2257 /* Don't look at this byte again */
2258 *sc &= ~IOC4_RXSB_MODEM_VALID;
2259 }
2260
2261 /* Check for valid data or RX errors */
2262 if ((*sc & IOC4_RXSB_DATA_VALID) &&
2263 ((*sc & (IOC4_RXSB_PAR_ERR
2264 | IOC4_RXSB_FRAME_ERR
2265 | IOC4_RXSB_BREAK))
2266 && (port->ip_notify & (N_PARITY_ERROR
2267 | N_FRAMING_ERROR
2268 | N_BREAK)))) {
2269 /* There is an error condition on the next byte.
2270 * If we have already transferred some bytes,
2271 * we'll stop here. Otherwise if this is the
2272 * first byte to be read, we'll just transfer
2273 * it alone after notifying the
2274 * upper layer of its status.
2275 */
2276 if (total > 0) {
2277 len = 0;
2278 break;
2279 } else {
2280 if ((*sc & IOC4_RXSB_PAR_ERR) &&
2281 (port->ip_notify & N_PARITY_ERROR)) {
2282 ioc4_cb_post_ncs(the_port,
2283 NCS_PARITY);
2284 }
2285 if ((*sc & IOC4_RXSB_FRAME_ERR) &&
2286 (port->ip_notify & N_FRAMING_ERROR)){
2287 ioc4_cb_post_ncs(the_port,
2288 NCS_FRAMING);
2289 }
2290 if ((*sc & IOC4_RXSB_BREAK)
2291 && (port->ip_notify & N_BREAK)) {
2292 ioc4_cb_post_ncs
2293 (the_port,
2294 NCS_BREAK);
2295 }
2296 len = 1;
2297 }
2298 }
2299 if (*sc & IOC4_RXSB_DATA_VALID) {
2300 *sc &= ~IOC4_RXSB_DATA_VALID;
2301 *buf = entry->ring_data[byte_num];
2302 buf++;
2303 len--;
2304 total++;
2305 }
2306 }
2307
2308 /* If we used up this entry entirely, go on to the next one,
2309 * otherwise we must have run out of buffer space, so
2310 * leave the consumer pointer here for the next read in case
2311 * there are still unread bytes in this entry.
2312 */
2313 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2314 cons_ptr += (int)sizeof(struct ring_entry);
2315 cons_ptr &= PROD_CONS_MASK;
2316 }
2317 }
2318
2319 /* Update consumer pointer and re-arm rx timer interrupt */
2320 writel(cons_ptr, &port->ip_serial_regs->srcir);
2321 port->ip_rx_cons = cons_ptr;
2322
2323 /* If we have now dipped below the rx high water mark and we have
2324 * rx_high interrupt turned off, we can now turn it back on again.
2325 */
2326 if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
2327 & PROD_CONS_MASK) < ((port->ip_sscr &
2328 IOC4_SSCR_RX_THRESHOLD)
2329 << IOC4_PROD_CONS_PTR_OFF))) {
2330 port->ip_flags &= ~INPUT_HIGH;
2331 enable_intrs(port, hooks->intr_rx_high);
2332 }
2333 return total;
2334}
2335
2336/**
2337 * receive_chars - upper level read. Called with ip_lock.
2338 * @the_port: port to read from
2339 */
2340static void receive_chars(struct uart_port *the_port)
2341{
2342 struct tty_struct *tty;
2343 unsigned char ch[IOC4_MAX_CHARS];
2344 int read_count, request_count = IOC4_MAX_CHARS;
2345 struct uart_icount *icount;
2346 struct uart_state *state = the_port->state;
2347 unsigned long pflags;
2348
2349 /* Make sure all the pointers are "good" ones */
2350 if (!state)
2351 return;
2352 if (!state->port.tty)
2353 return;
2354
2355 spin_lock_irqsave(&the_port->lock, pflags);
2356 tty = state->port.tty;
2357
2358 request_count = tty_buffer_request_room(tty, IOC4_MAX_CHARS);
2359
2360 if (request_count > 0) {
2361 icount = &the_port->icount;
2362 read_count = do_read(the_port, ch, request_count);
2363 if (read_count > 0) {
2364 tty_insert_flip_string(tty, ch, read_count);
2365 icount->rx += read_count;
2366 }
2367 }
2368
2369 spin_unlock_irqrestore(&the_port->lock, pflags);
2370
2371 tty_flip_buffer_push(tty);
2372}
2373
2374/**
2375 * ic4_type - What type of console are we?
2376 * @port: Port to operate with (we ignore since we only have one port)
2377 *
2378 */
2379static const char *ic4_type(struct uart_port *the_port)
2380{
2381 if (the_port->mapbase == PROTO_RS232)
2382 return "SGI IOC4 Serial [rs232]";
2383 else
2384 return "SGI IOC4 Serial [rs422]";
2385}
2386
2387/**
2388 * ic4_tx_empty - Is the transmitter empty?
2389 * @port: Port to operate on
2390 *
2391 */
2392static unsigned int ic4_tx_empty(struct uart_port *the_port)
2393{
2394 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2395 unsigned int ret = 0;
2396
2397 if (port_is_active(port, the_port)) {
2398 if (readl(&port->ip_serial_regs->shadow) & IOC4_SHADOW_TEMT)
2399 ret = TIOCSER_TEMT;
2400 }
2401 return ret;
2402}
2403
2404/**
2405 * ic4_stop_tx - stop the transmitter
2406 * @port: Port to operate on
2407 *
2408 */
2409static void ic4_stop_tx(struct uart_port *the_port)
2410{
2411 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2412
2413 if (port_is_active(port, the_port))
2414 set_notification(port, N_OUTPUT_LOWAT, 0);
2415}
2416
2417/**
2418 * null_void_function -
2419 * @port: Port to operate on
2420 *
2421 */
2422static void null_void_function(struct uart_port *the_port)
2423{
2424}
2425
2426/**
2427 * ic4_shutdown - shut down the port - free irq and disable
2428 * @port: Port to shut down
2429 *
2430 */
2431static void ic4_shutdown(struct uart_port *the_port)
2432{
2433 unsigned long port_flags;
2434 struct ioc4_port *port;
2435 struct uart_state *state;
2436
2437 port = get_ioc4_port(the_port, 0);
2438 if (!port)
2439 return;
2440
2441 state = the_port->state;
2442 port->ip_port = NULL;
2443
2444 wake_up_interruptible(&state->port.delta_msr_wait);
2445
2446 if (state->port.tty)
2447 set_bit(TTY_IO_ERROR, &state->port.tty->flags);
2448
2449 spin_lock_irqsave(&the_port->lock, port_flags);
2450 set_notification(port, N_ALL, 0);
2451 port->ip_flags = PORT_INACTIVE;
2452 spin_unlock_irqrestore(&the_port->lock, port_flags);
2453}
2454
2455/**
2456 * ic4_set_mctrl - set control lines (dtr, rts, etc)
2457 * @port: Port to operate on
2458 * @mctrl: Lines to set/unset
2459 *
2460 */
2461static void ic4_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
2462{
2463 unsigned char mcr = 0;
2464 struct ioc4_port *port;
2465
2466 port = get_ioc4_port(the_port, 0);
2467 if (!port_is_active(port, the_port))
2468 return;
2469
2470 if (mctrl & TIOCM_RTS)
2471 mcr |= UART_MCR_RTS;
2472 if (mctrl & TIOCM_DTR)
2473 mcr |= UART_MCR_DTR;
2474 if (mctrl & TIOCM_OUT1)
2475 mcr |= UART_MCR_OUT1;
2476 if (mctrl & TIOCM_OUT2)
2477 mcr |= UART_MCR_OUT2;
2478 if (mctrl & TIOCM_LOOP)
2479 mcr |= UART_MCR_LOOP;
2480
2481 set_mcr(the_port, mcr, IOC4_SHADOW_DTR);
2482}
2483
2484/**
2485 * ic4_get_mctrl - get control line info
2486 * @port: port to operate on
2487 *
2488 */
2489static unsigned int ic4_get_mctrl(struct uart_port *the_port)
2490{
2491 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2492 uint32_t shadow;
2493 unsigned int ret = 0;
2494
2495 if (!port_is_active(port, the_port))
2496 return 0;
2497
2498 shadow = readl(&port->ip_serial_regs->shadow);
2499 if (shadow & IOC4_SHADOW_DCD)
2500 ret |= TIOCM_CAR;
2501 if (shadow & IOC4_SHADOW_DR)
2502 ret |= TIOCM_DSR;
2503 if (shadow & IOC4_SHADOW_CTS)
2504 ret |= TIOCM_CTS;
2505 return ret;
2506}
2507
2508/**
2509 * ic4_start_tx - Start transmitter, flush any output
2510 * @port: Port to operate on
2511 *
2512 */
2513static void ic4_start_tx(struct uart_port *the_port)
2514{
2515 struct ioc4_port *port = get_ioc4_port(the_port, 0);
2516
2517 if (port_is_active(port, the_port)) {
2518 set_notification(port, N_OUTPUT_LOWAT, 1);
2519 enable_intrs(port, port->ip_hooks->intr_tx_mt);
2520 }
2521}
2522
2523/**
2524 * ic4_break_ctl - handle breaks
2525 * @port: Port to operate on
2526 * @break_state: Break state
2527 *
2528 */
2529static void ic4_break_ctl(struct uart_port *the_port, int break_state)
2530{
2531}
2532
2533/**
2534 * ic4_startup - Start up the serial port
2535 * @port: Port to operate on
2536 *
2537 */
2538static int ic4_startup(struct uart_port *the_port)
2539{
2540 int retval;
2541 struct ioc4_port *port;
2542 struct ioc4_control *control;
2543 struct uart_state *state;
2544 unsigned long port_flags;
2545
2546 if (!the_port)
2547 return -ENODEV;
2548 port = get_ioc4_port(the_port, 1);
2549 if (!port)
2550 return -ENODEV;
2551 state = the_port->state;
2552
2553 control = port->ip_control;
2554 if (!control) {
2555 port->ip_port = NULL;
2556 return -ENODEV;
2557 }
2558
2559 /* Start up the serial port */
2560 spin_lock_irqsave(&the_port->lock, port_flags);
2561 retval = ic4_startup_local(the_port);
2562 spin_unlock_irqrestore(&the_port->lock, port_flags);
2563 return retval;
2564}
2565
2566/**
2567 * ic4_set_termios - set termios stuff
2568 * @port: port to operate on
2569 * @termios: New settings
2570 * @termios: Old
2571 *
2572 */
2573static void
2574ic4_set_termios(struct uart_port *the_port,
2575 struct ktermios *termios, struct ktermios *old_termios)
2576{
2577 unsigned long port_flags;
2578
2579 spin_lock_irqsave(&the_port->lock, port_flags);
2580 ioc4_change_speed(the_port, termios, old_termios);
2581 spin_unlock_irqrestore(&the_port->lock, port_flags);
2582}
2583
2584/**
2585 * ic4_request_port - allocate resources for port - no op....
2586 * @port: port to operate on
2587 *
2588 */
2589static int ic4_request_port(struct uart_port *port)
2590{
2591 return 0;
2592}
2593
2594/* Associate the uart functions above - given to serial core */
2595
2596static struct uart_ops ioc4_ops = {
2597 .tx_empty = ic4_tx_empty,
2598 .set_mctrl = ic4_set_mctrl,
2599 .get_mctrl = ic4_get_mctrl,
2600 .stop_tx = ic4_stop_tx,
2601 .start_tx = ic4_start_tx,
2602 .stop_rx = null_void_function,
2603 .enable_ms = null_void_function,
2604 .break_ctl = ic4_break_ctl,
2605 .startup = ic4_startup,
2606 .shutdown = ic4_shutdown,
2607 .set_termios = ic4_set_termios,
2608 .type = ic4_type,
2609 .release_port = null_void_function,
2610 .request_port = ic4_request_port,
2611};
2612
2613/*
2614 * Boot-time initialization code
2615 */
2616
2617static struct uart_driver ioc4_uart_rs232 = {
2618 .owner = THIS_MODULE,
2619 .driver_name = "ioc4_serial_rs232",
2620 .dev_name = DEVICE_NAME_RS232,
2621 .major = DEVICE_MAJOR,
2622 .minor = DEVICE_MINOR_RS232,
2623 .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2624};
2625
2626static struct uart_driver ioc4_uart_rs422 = {
2627 .owner = THIS_MODULE,
2628 .driver_name = "ioc4_serial_rs422",
2629 .dev_name = DEVICE_NAME_RS422,
2630 .major = DEVICE_MAJOR,
2631 .minor = DEVICE_MINOR_RS422,
2632 .nr = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2633};
2634
2635
2636/**
2637 * ioc4_serial_remove_one - detach function
2638 *
2639 * @idd: IOC4 master module data for this IOC4
2640 */
2641
2642static int ioc4_serial_remove_one(struct ioc4_driver_data *idd)
2643{
2644 int port_num, port_type;
2645 struct ioc4_control *control;
2646 struct uart_port *the_port;
2647 struct ioc4_port *port;
2648 struct ioc4_soft *soft;
2649
2650 /* If serial driver did not attach, don't try to detach */
2651 control = idd->idd_serial_data;
2652 if (!control)
2653 return 0;
2654
2655 for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
2656 for (port_type = UART_PORT_MIN;
2657 port_type < UART_PORT_COUNT;
2658 port_type++) {
2659 the_port = &control->ic_port[port_num].icp_uart_port
2660 [port_type];
2661 if (the_port) {
2662 switch (port_type) {
2663 case UART_PORT_RS422:
2664 uart_remove_one_port(&ioc4_uart_rs422,
2665 the_port);
2666 break;
2667 default:
2668 case UART_PORT_RS232:
2669 uart_remove_one_port(&ioc4_uart_rs232,
2670 the_port);
2671 break;
2672 }
2673 }
2674 }
2675 port = control->ic_port[port_num].icp_port;
2676 /* we allocate in pairs */
2677 if (!(port_num & 1) && port) {
2678 pci_free_consistent(port->ip_pdev,
2679 TOTAL_RING_BUF_SIZE,
2680 port->ip_cpu_ringbuf,
2681 port->ip_dma_ringbuf);
2682 kfree(port);
2683 }
2684 }
2685 soft = control->ic_soft;
2686 if (soft) {
2687 free_irq(control->ic_irq, soft);
2688 if (soft->is_ioc4_serial_addr) {
2689 iounmap(soft->is_ioc4_serial_addr);
2690 release_mem_region((unsigned long)
2691 soft->is_ioc4_serial_addr,
2692 sizeof(struct ioc4_serial));
2693 }
2694 kfree(soft);
2695 }
2696 kfree(control);
2697 idd->idd_serial_data = NULL;
2698
2699 return 0;
2700}
2701
2702
2703/**
2704 * ioc4_serial_core_attach_rs232 - register with serial core
2705 * This is done during pci probing
2706 * @pdev: handle for this card
2707 */
2708static inline int
2709ioc4_serial_core_attach(struct pci_dev *pdev, int port_type)
2710{
2711 struct ioc4_port *port;
2712 struct uart_port *the_port;
2713 struct ioc4_driver_data *idd = pci_get_drvdata(pdev);
2714 struct ioc4_control *control = idd->idd_serial_data;
2715 int port_num;
2716 int port_type_idx;
2717 struct uart_driver *u_driver;
2718
2719
2720 DPRINT_CONFIG(("%s: attach pdev 0x%p - control 0x%p\n",
2721 __func__, pdev, (void *)control));
2722
2723 if (!control)
2724 return -ENODEV;
2725
2726 port_type_idx = (port_type == PROTO_RS232) ? UART_PORT_RS232
2727 : UART_PORT_RS422;
2728
2729 u_driver = (port_type == PROTO_RS232) ? &ioc4_uart_rs232
2730 : &ioc4_uart_rs422;
2731
2732 /* once around for each port on this card */
2733 for (port_num = 0; port_num < IOC4_NUM_SERIAL_PORTS; port_num++) {
2734 the_port = &control->ic_port[port_num].icp_uart_port
2735 [port_type_idx];
2736 port = control->ic_port[port_num].icp_port;
2737 port->ip_all_ports[port_type_idx] = the_port;
2738
2739 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p : type %s\n",
2740 __func__, (void *)the_port,
2741 (void *)port,
2742 port_type == PROTO_RS232 ? "rs232" : "rs422"));
2743
2744 /* membase, iobase and mapbase just need to be non-0 */
2745 the_port->membase = (unsigned char __iomem *)1;
2746 the_port->iobase = (pdev->bus->number << 16) | port_num;
2747 the_port->line = (Num_of_ioc4_cards << 2) | port_num;
2748 the_port->mapbase = port_type;
2749 the_port->type = PORT_16550A;
2750 the_port->fifosize = IOC4_FIFO_CHARS;
2751 the_port->ops = &ioc4_ops;
2752 the_port->irq = control->ic_irq;
2753 the_port->dev = &pdev->dev;
2754 spin_lock_init(&the_port->lock);
2755 if (uart_add_one_port(u_driver, the_port) < 0) {
2756 printk(KERN_WARNING
2757 "%s: unable to add port %d bus %d\n",
2758 __func__, the_port->line, pdev->bus->number);
2759 } else {
2760 DPRINT_CONFIG(
2761 ("IOC4 serial port %d irq = %d, bus %d\n",
2762 the_port->line, the_port->irq, pdev->bus->number));
2763 }
2764 }
2765 return 0;
2766}
2767
2768/**
2769 * ioc4_serial_attach_one - register attach function
2770 * called per card found from IOC4 master module.
2771 * @idd: Master module data for this IOC4
2772 */
2773int
2774ioc4_serial_attach_one(struct ioc4_driver_data *idd)
2775{
2776 unsigned long tmp_addr1;
2777 struct ioc4_serial __iomem *serial;
2778 struct ioc4_soft *soft;
2779 struct ioc4_control *control;
2780 int ret = 0;
2781
2782
2783 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, idd->idd_pdev,
2784 idd->idd_pci_id));
2785
2786 /* PCI-RT does not bring out serial connections.
2787 * Do not attach to this particular IOC4.
2788 */
2789 if (idd->idd_variant == IOC4_VARIANT_PCI_RT)
2790 return 0;
2791
2792 /* request serial registers */
2793 tmp_addr1 = idd->idd_bar0 + IOC4_SERIAL_OFFSET;
2794
2795 if (!request_mem_region(tmp_addr1, sizeof(struct ioc4_serial),
2796 "sioc4_uart")) {
2797 printk(KERN_WARNING
2798 "ioc4 (%p): unable to get request region for "
2799 "uart space\n", (void *)idd->idd_pdev);
2800 ret = -ENODEV;
2801 goto out1;
2802 }
2803 serial = ioremap(tmp_addr1, sizeof(struct ioc4_serial));
2804 if (!serial) {
2805 printk(KERN_WARNING
2806 "ioc4 (%p) : unable to remap ioc4 serial register\n",
2807 (void *)idd->idd_pdev);
2808 ret = -ENODEV;
2809 goto out2;
2810 }
2811 DPRINT_CONFIG(("%s : mem 0x%p, serial 0x%p\n",
2812 __func__, (void *)idd->idd_misc_regs,
2813 (void *)serial));
2814
2815 /* Get memory for the new card */
2816 control = kzalloc(sizeof(struct ioc4_control), GFP_KERNEL);
2817
2818 if (!control) {
2819 printk(KERN_WARNING "ioc4_attach_one"
2820 ": unable to get memory for the IOC4\n");
2821 ret = -ENOMEM;
2822 goto out2;
2823 }
2824 idd->idd_serial_data = control;
2825
2826 /* Allocate the soft structure */
2827 soft = kzalloc(sizeof(struct ioc4_soft), GFP_KERNEL);
2828 if (!soft) {
2829 printk(KERN_WARNING
2830 "ioc4 (%p): unable to get memory for the soft struct\n",
2831 (void *)idd->idd_pdev);
2832 ret = -ENOMEM;
2833 goto out3;
2834 }
2835
2836 spin_lock_init(&soft->is_ir_lock);
2837 soft->is_ioc4_misc_addr = idd->idd_misc_regs;
2838 soft->is_ioc4_serial_addr = serial;
2839
2840 /* Init the IOC4 */
2841 writel(0xf << IOC4_SIO_CR_CMD_PULSE_SHIFT,
2842 &idd->idd_misc_regs->sio_cr.raw);
2843
2844 /* Enable serial port mode select generic PIO pins as outputs */
2845 writel(IOC4_GPCR_UART0_MODESEL | IOC4_GPCR_UART1_MODESEL
2846 | IOC4_GPCR_UART2_MODESEL | IOC4_GPCR_UART3_MODESEL,
2847 &idd->idd_misc_regs->gpcr_s.raw);
2848
2849 /* Clear and disable all serial interrupts */
2850 write_ireg(soft, ~0, IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
2851 writel(~0, &idd->idd_misc_regs->sio_ir.raw);
2852 write_ireg(soft, IOC4_OTHER_IR_SER_MEMERR, IOC4_W_IEC,
2853 IOC4_OTHER_INTR_TYPE);
2854 writel(IOC4_OTHER_IR_SER_MEMERR, &idd->idd_misc_regs->other_ir.raw);
2855 control->ic_soft = soft;
2856
2857 /* Hook up interrupt handler */
2858 if (!request_irq(idd->idd_pdev->irq, ioc4_intr, IRQF_SHARED,
2859 "sgi-ioc4serial", soft)) {
2860 control->ic_irq = idd->idd_pdev->irq;
2861 } else {
2862 printk(KERN_WARNING
2863 "%s : request_irq fails for IRQ 0x%x\n ",
2864 __func__, idd->idd_pdev->irq);
2865 }
2866 ret = ioc4_attach_local(idd);
2867 if (ret)
2868 goto out4;
2869
2870 /* register port with the serial core - 1 rs232, 1 rs422 */
2871
2872 if ((ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS232)))
2873 goto out4;
2874
2875 if ((ret = ioc4_serial_core_attach(idd->idd_pdev, PROTO_RS422)))
2876 goto out5;
2877
2878 Num_of_ioc4_cards++;
2879
2880 return ret;
2881
2882 /* error exits that give back resources */
2883out5:
2884 ioc4_serial_remove_one(idd);
2885out4:
2886 kfree(soft);
2887out3:
2888 kfree(control);
2889out2:
2890 if (serial)
2891 iounmap(serial);
2892 release_mem_region(tmp_addr1, sizeof(struct ioc4_serial));
2893out1:
2894
2895 return ret;
2896}
2897
2898
2899static struct ioc4_submodule ioc4_serial_submodule = {
2900 .is_name = "IOC4_serial",
2901 .is_owner = THIS_MODULE,
2902 .is_probe = ioc4_serial_attach_one,
2903 .is_remove = ioc4_serial_remove_one,
2904};
2905
2906/**
2907 * ioc4_serial_init - module init
2908 */
2909static int __init ioc4_serial_init(void)
2910{
2911 int ret;
2912
2913 /* register with serial core */
2914 if ((ret = uart_register_driver(&ioc4_uart_rs232)) < 0) {
2915 printk(KERN_WARNING
2916 "%s: Couldn't register rs232 IOC4 serial driver\n",
2917 __func__);
2918 goto out;
2919 }
2920 if ((ret = uart_register_driver(&ioc4_uart_rs422)) < 0) {
2921 printk(KERN_WARNING
2922 "%s: Couldn't register rs422 IOC4 serial driver\n",
2923 __func__);
2924 goto out_uart_rs232;
2925 }
2926
2927 /* register with IOC4 main module */
2928 ret = ioc4_register_submodule(&ioc4_serial_submodule);
2929 if (ret)
2930 goto out_uart_rs422;
2931 return 0;
2932
2933out_uart_rs422:
2934 uart_unregister_driver(&ioc4_uart_rs422);
2935out_uart_rs232:
2936 uart_unregister_driver(&ioc4_uart_rs232);
2937out:
2938 return ret;
2939}
2940
2941static void __exit ioc4_serial_exit(void)
2942{
2943 ioc4_unregister_submodule(&ioc4_serial_submodule);
2944 uart_unregister_driver(&ioc4_uart_rs232);
2945 uart_unregister_driver(&ioc4_uart_rs422);
2946}
2947
2948late_initcall(ioc4_serial_init); /* Call only after tty init is done */
2949module_exit(ioc4_serial_exit);
2950
2951MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2952MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC4 Base-IO Card");
2953MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ip22zilog.c b/drivers/tty/serial/ip22zilog.c
new file mode 100644
index 000000000000..7b1cda59ebb5
--- /dev/null
+++ b/drivers/tty/serial/ip22zilog.c
@@ -0,0 +1,1221 @@
1/*
2 * Driver for Zilog serial chips found on SGI workstations and
3 * servers. This driver could actually be made more generic.
4 *
5 * This is based on the drivers/serial/sunzilog.c code as of 2.6.0-test7 and the
6 * old drivers/sgi/char/sgiserial.c code which itself is based of the original
7 * drivers/sbus/char/zs.c code. A lot of code has been simply moved over
8 * directly from there but much has been rewritten. Credits therefore go out
9 * to David S. Miller, Eddie C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell
10 * for their work there.
11 *
12 * Copyright (C) 2002 Ralf Baechle (ralf@linux-mips.org)
13 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
14 */
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/major.h>
22#include <linux/string.h>
23#include <linux/ptrace.h>
24#include <linux/ioport.h>
25#include <linux/slab.h>
26#include <linux/circ_buf.h>
27#include <linux/serial.h>
28#include <linux/sysrq.h>
29#include <linux/console.h>
30#include <linux/spinlock.h>
31#include <linux/init.h>
32
33#include <asm/io.h>
34#include <asm/irq.h>
35#include <asm/sgialib.h>
36#include <asm/sgi/ioc.h>
37#include <asm/sgi/hpc3.h>
38#include <asm/sgi/ip22.h>
39
40#if defined(CONFIG_SERIAL_IP22_ZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
41#define SUPPORT_SYSRQ
42#endif
43
44#include <linux/serial_core.h>
45
46#include "ip22zilog.h"
47
48/*
49 * On IP22 we need to delay after register accesses but we do not need to
50 * flush writes.
51 */
52#define ZSDELAY() udelay(5)
53#define ZSDELAY_LONG() udelay(20)
54#define ZS_WSYNC(channel) do { } while (0)
55
56#define NUM_IP22ZILOG 1
57#define NUM_CHANNELS (NUM_IP22ZILOG * 2)
58
59#define ZS_CLOCK 3672000 /* Zilog input clock rate. */
60#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
61
62/*
63 * We wrap our port structure around the generic uart_port.
64 */
65struct uart_ip22zilog_port {
66 struct uart_port port;
67
68 /* IRQ servicing chain. */
69 struct uart_ip22zilog_port *next;
70
71 /* Current values of Zilog write registers. */
72 unsigned char curregs[NUM_ZSREGS];
73
74 unsigned int flags;
75#define IP22ZILOG_FLAG_IS_CONS 0x00000004
76#define IP22ZILOG_FLAG_IS_KGDB 0x00000008
77#define IP22ZILOG_FLAG_MODEM_STATUS 0x00000010
78#define IP22ZILOG_FLAG_IS_CHANNEL_A 0x00000020
79#define IP22ZILOG_FLAG_REGS_HELD 0x00000040
80#define IP22ZILOG_FLAG_TX_STOPPED 0x00000080
81#define IP22ZILOG_FLAG_TX_ACTIVE 0x00000100
82#define IP22ZILOG_FLAG_RESET_DONE 0x00000200
83
84 unsigned int tty_break;
85
86 unsigned char parity_mask;
87 unsigned char prev_status;
88};
89
90#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel *)((PORT)->membase))
91#define UART_ZILOG(PORT) ((struct uart_ip22zilog_port *)(PORT))
92#define IP22ZILOG_GET_CURR_REG(PORT, REGNUM) \
93 (UART_ZILOG(PORT)->curregs[REGNUM])
94#define IP22ZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL) \
95 ((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
96#define ZS_IS_CONS(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CONS)
97#define ZS_IS_KGDB(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_KGDB)
98#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & IP22ZILOG_FLAG_MODEM_STATUS)
99#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CHANNEL_A)
100#define ZS_REGS_HELD(UP) ((UP)->flags & IP22ZILOG_FLAG_REGS_HELD)
101#define ZS_TX_STOPPED(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_STOPPED)
102#define ZS_TX_ACTIVE(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_ACTIVE)
103
104/* Reading and writing Zilog8530 registers. The delays are to make this
105 * driver work on the IP22 which needs a settling delay after each chip
106 * register access, other machines handle this in hardware via auxiliary
107 * flip-flops which implement the settle time we do in software.
108 *
109 * The port lock must be held and local IRQs must be disabled
110 * when {read,write}_zsreg is invoked.
111 */
112static unsigned char read_zsreg(struct zilog_channel *channel,
113 unsigned char reg)
114{
115 unsigned char retval;
116
117 writeb(reg, &channel->control);
118 ZSDELAY();
119 retval = readb(&channel->control);
120 ZSDELAY();
121
122 return retval;
123}
124
125static void write_zsreg(struct zilog_channel *channel,
126 unsigned char reg, unsigned char value)
127{
128 writeb(reg, &channel->control);
129 ZSDELAY();
130 writeb(value, &channel->control);
131 ZSDELAY();
132}
133
134static void ip22zilog_clear_fifo(struct zilog_channel *channel)
135{
136 int i;
137
138 for (i = 0; i < 32; i++) {
139 unsigned char regval;
140
141 regval = readb(&channel->control);
142 ZSDELAY();
143 if (regval & Rx_CH_AV)
144 break;
145
146 regval = read_zsreg(channel, R1);
147 readb(&channel->data);
148 ZSDELAY();
149
150 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
151 writeb(ERR_RES, &channel->control);
152 ZSDELAY();
153 ZS_WSYNC(channel);
154 }
155 }
156}
157
158/* This function must only be called when the TX is not busy. The UART
159 * port lock must be held and local interrupts disabled.
160 */
161static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
162{
163 int i;
164
165 /* Let pending transmits finish. */
166 for (i = 0; i < 1000; i++) {
167 unsigned char stat = read_zsreg(channel, R1);
168 if (stat & ALL_SNT)
169 break;
170 udelay(100);
171 }
172
173 writeb(ERR_RES, &channel->control);
174 ZSDELAY();
175 ZS_WSYNC(channel);
176
177 ip22zilog_clear_fifo(channel);
178
179 /* Disable all interrupts. */
180 write_zsreg(channel, R1,
181 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
182
183 /* Set parity, sync config, stop bits, and clock divisor. */
184 write_zsreg(channel, R4, regs[R4]);
185
186 /* Set misc. TX/RX control bits. */
187 write_zsreg(channel, R10, regs[R10]);
188
189 /* Set TX/RX controls sans the enable bits. */
190 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
191 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
192
193 /* Synchronous mode config. */
194 write_zsreg(channel, R6, regs[R6]);
195 write_zsreg(channel, R7, regs[R7]);
196
197 /* Don't mess with the interrupt vector (R2, unused by us) and
198 * master interrupt control (R9). We make sure this is setup
199 * properly at probe time then never touch it again.
200 */
201
202 /* Disable baud generator. */
203 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
204
205 /* Clock mode control. */
206 write_zsreg(channel, R11, regs[R11]);
207
208 /* Lower and upper byte of baud rate generator divisor. */
209 write_zsreg(channel, R12, regs[R12]);
210 write_zsreg(channel, R13, regs[R13]);
211
212 /* Now rewrite R14, with BRENAB (if set). */
213 write_zsreg(channel, R14, regs[R14]);
214
215 /* External status interrupt control. */
216 write_zsreg(channel, R15, regs[R15]);
217
218 /* Reset external status interrupts. */
219 write_zsreg(channel, R0, RES_EXT_INT);
220 write_zsreg(channel, R0, RES_EXT_INT);
221
222 /* Rewrite R3/R5, this time without enables masked. */
223 write_zsreg(channel, R3, regs[R3]);
224 write_zsreg(channel, R5, regs[R5]);
225
226 /* Rewrite R1, this time without IRQ enabled masked. */
227 write_zsreg(channel, R1, regs[R1]);
228}
229
230/* Reprogram the Zilog channel HW registers with the copies found in the
231 * software state struct. If the transmitter is busy, we defer this update
232 * until the next TX complete interrupt. Else, we do it right now.
233 *
234 * The UART port lock must be held and local interrupts disabled.
235 */
236static void ip22zilog_maybe_update_regs(struct uart_ip22zilog_port *up,
237 struct zilog_channel *channel)
238{
239 if (!ZS_REGS_HELD(up)) {
240 if (ZS_TX_ACTIVE(up)) {
241 up->flags |= IP22ZILOG_FLAG_REGS_HELD;
242 } else {
243 __load_zsregs(channel, up->curregs);
244 }
245 }
246}
247
248#define Rx_BRK 0x0100 /* BREAK event software flag. */
249#define Rx_SYS 0x0200 /* SysRq event software flag. */
250
251static struct tty_struct *ip22zilog_receive_chars(struct uart_ip22zilog_port *up,
252 struct zilog_channel *channel)
253{
254 struct tty_struct *tty;
255 unsigned char ch, flag;
256 unsigned int r1;
257
258 tty = NULL;
259 if (up->port.state != NULL &&
260 up->port.state->port.tty != NULL)
261 tty = up->port.state->port.tty;
262
263 for (;;) {
264 ch = readb(&channel->control);
265 ZSDELAY();
266 if (!(ch & Rx_CH_AV))
267 break;
268
269 r1 = read_zsreg(channel, R1);
270 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
271 writeb(ERR_RES, &channel->control);
272 ZSDELAY();
273 ZS_WSYNC(channel);
274 }
275
276 ch = readb(&channel->data);
277 ZSDELAY();
278
279 ch &= up->parity_mask;
280
281 /* Handle the null char got when BREAK is removed. */
282 if (!ch)
283 r1 |= up->tty_break;
284
285 /* A real serial line, record the character and status. */
286 flag = TTY_NORMAL;
287 up->port.icount.rx++;
288 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | Rx_SYS | Rx_BRK)) {
289 up->tty_break = 0;
290
291 if (r1 & (Rx_SYS | Rx_BRK)) {
292 up->port.icount.brk++;
293 if (r1 & Rx_SYS)
294 continue;
295 r1 &= ~(PAR_ERR | CRC_ERR);
296 }
297 else if (r1 & PAR_ERR)
298 up->port.icount.parity++;
299 else if (r1 & CRC_ERR)
300 up->port.icount.frame++;
301 if (r1 & Rx_OVR)
302 up->port.icount.overrun++;
303 r1 &= up->port.read_status_mask;
304 if (r1 & Rx_BRK)
305 flag = TTY_BREAK;
306 else if (r1 & PAR_ERR)
307 flag = TTY_PARITY;
308 else if (r1 & CRC_ERR)
309 flag = TTY_FRAME;
310 }
311
312 if (uart_handle_sysrq_char(&up->port, ch))
313 continue;
314
315 if (tty)
316 uart_insert_char(&up->port, r1, Rx_OVR, ch, flag);
317 }
318 return tty;
319}
320
321static void ip22zilog_status_handle(struct uart_ip22zilog_port *up,
322 struct zilog_channel *channel)
323{
324 unsigned char status;
325
326 status = readb(&channel->control);
327 ZSDELAY();
328
329 writeb(RES_EXT_INT, &channel->control);
330 ZSDELAY();
331 ZS_WSYNC(channel);
332
333 if (up->curregs[R15] & BRKIE) {
334 if ((status & BRK_ABRT) && !(up->prev_status & BRK_ABRT)) {
335 if (uart_handle_break(&up->port))
336 up->tty_break = Rx_SYS;
337 else
338 up->tty_break = Rx_BRK;
339 }
340 }
341
342 if (ZS_WANTS_MODEM_STATUS(up)) {
343 if (status & SYNC)
344 up->port.icount.dsr++;
345
346 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
347 * But it does not tell us which bit has changed, we have to keep
348 * track of this ourselves.
349 */
350 if ((status ^ up->prev_status) ^ DCD)
351 uart_handle_dcd_change(&up->port,
352 (status & DCD));
353 if ((status ^ up->prev_status) ^ CTS)
354 uart_handle_cts_change(&up->port,
355 (status & CTS));
356
357 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
358 }
359
360 up->prev_status = status;
361}
362
363static void ip22zilog_transmit_chars(struct uart_ip22zilog_port *up,
364 struct zilog_channel *channel)
365{
366 struct circ_buf *xmit;
367
368 if (ZS_IS_CONS(up)) {
369 unsigned char status = readb(&channel->control);
370 ZSDELAY();
371
372 /* TX still busy? Just wait for the next TX done interrupt.
373 *
374 * It can occur because of how we do serial console writes. It would
375 * be nice to transmit console writes just like we normally would for
376 * a TTY line. (ie. buffered and TX interrupt driven). That is not
377 * easy because console writes cannot sleep. One solution might be
378 * to poll on enough port->xmit space becoming free. -DaveM
379 */
380 if (!(status & Tx_BUF_EMP))
381 return;
382 }
383
384 up->flags &= ~IP22ZILOG_FLAG_TX_ACTIVE;
385
386 if (ZS_REGS_HELD(up)) {
387 __load_zsregs(channel, up->curregs);
388 up->flags &= ~IP22ZILOG_FLAG_REGS_HELD;
389 }
390
391 if (ZS_TX_STOPPED(up)) {
392 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
393 goto ack_tx_int;
394 }
395
396 if (up->port.x_char) {
397 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
398 writeb(up->port.x_char, &channel->data);
399 ZSDELAY();
400 ZS_WSYNC(channel);
401
402 up->port.icount.tx++;
403 up->port.x_char = 0;
404 return;
405 }
406
407 if (up->port.state == NULL)
408 goto ack_tx_int;
409 xmit = &up->port.state->xmit;
410 if (uart_circ_empty(xmit))
411 goto ack_tx_int;
412 if (uart_tx_stopped(&up->port))
413 goto ack_tx_int;
414
415 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
416 writeb(xmit->buf[xmit->tail], &channel->data);
417 ZSDELAY();
418 ZS_WSYNC(channel);
419
420 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
421 up->port.icount.tx++;
422
423 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
424 uart_write_wakeup(&up->port);
425
426 return;
427
428ack_tx_int:
429 writeb(RES_Tx_P, &channel->control);
430 ZSDELAY();
431 ZS_WSYNC(channel);
432}
433
434static irqreturn_t ip22zilog_interrupt(int irq, void *dev_id)
435{
436 struct uart_ip22zilog_port *up = dev_id;
437
438 while (up) {
439 struct zilog_channel *channel
440 = ZILOG_CHANNEL_FROM_PORT(&up->port);
441 struct tty_struct *tty;
442 unsigned char r3;
443
444 spin_lock(&up->port.lock);
445 r3 = read_zsreg(channel, R3);
446
447 /* Channel A */
448 tty = NULL;
449 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
450 writeb(RES_H_IUS, &channel->control);
451 ZSDELAY();
452 ZS_WSYNC(channel);
453
454 if (r3 & CHARxIP)
455 tty = ip22zilog_receive_chars(up, channel);
456 if (r3 & CHAEXT)
457 ip22zilog_status_handle(up, channel);
458 if (r3 & CHATxIP)
459 ip22zilog_transmit_chars(up, channel);
460 }
461 spin_unlock(&up->port.lock);
462
463 if (tty)
464 tty_flip_buffer_push(tty);
465
466 /* Channel B */
467 up = up->next;
468 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
469
470 spin_lock(&up->port.lock);
471 tty = NULL;
472 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
473 writeb(RES_H_IUS, &channel->control);
474 ZSDELAY();
475 ZS_WSYNC(channel);
476
477 if (r3 & CHBRxIP)
478 tty = ip22zilog_receive_chars(up, channel);
479 if (r3 & CHBEXT)
480 ip22zilog_status_handle(up, channel);
481 if (r3 & CHBTxIP)
482 ip22zilog_transmit_chars(up, channel);
483 }
484 spin_unlock(&up->port.lock);
485
486 if (tty)
487 tty_flip_buffer_push(tty);
488
489 up = up->next;
490 }
491
492 return IRQ_HANDLED;
493}
494
495/* A convenient way to quickly get R0 status. The caller must _not_ hold the
496 * port lock, it is acquired here.
497 */
498static __inline__ unsigned char ip22zilog_read_channel_status(struct uart_port *port)
499{
500 struct zilog_channel *channel;
501 unsigned char status;
502
503 channel = ZILOG_CHANNEL_FROM_PORT(port);
504 status = readb(&channel->control);
505 ZSDELAY();
506
507 return status;
508}
509
510/* The port lock is not held. */
511static unsigned int ip22zilog_tx_empty(struct uart_port *port)
512{
513 unsigned long flags;
514 unsigned char status;
515 unsigned int ret;
516
517 spin_lock_irqsave(&port->lock, flags);
518
519 status = ip22zilog_read_channel_status(port);
520
521 spin_unlock_irqrestore(&port->lock, flags);
522
523 if (status & Tx_BUF_EMP)
524 ret = TIOCSER_TEMT;
525 else
526 ret = 0;
527
528 return ret;
529}
530
531/* The port lock is held and interrupts are disabled. */
532static unsigned int ip22zilog_get_mctrl(struct uart_port *port)
533{
534 unsigned char status;
535 unsigned int ret;
536
537 status = ip22zilog_read_channel_status(port);
538
539 ret = 0;
540 if (status & DCD)
541 ret |= TIOCM_CAR;
542 if (status & SYNC)
543 ret |= TIOCM_DSR;
544 if (status & CTS)
545 ret |= TIOCM_CTS;
546
547 return ret;
548}
549
550/* The port lock is held and interrupts are disabled. */
551static void ip22zilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
552{
553 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
554 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
555 unsigned char set_bits, clear_bits;
556
557 set_bits = clear_bits = 0;
558
559 if (mctrl & TIOCM_RTS)
560 set_bits |= RTS;
561 else
562 clear_bits |= RTS;
563 if (mctrl & TIOCM_DTR)
564 set_bits |= DTR;
565 else
566 clear_bits |= DTR;
567
568 /* NOTE: Not subject to 'transmitter active' rule. */
569 up->curregs[R5] |= set_bits;
570 up->curregs[R5] &= ~clear_bits;
571 write_zsreg(channel, R5, up->curregs[R5]);
572}
573
574/* The port lock is held and interrupts are disabled. */
575static void ip22zilog_stop_tx(struct uart_port *port)
576{
577 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
578
579 up->flags |= IP22ZILOG_FLAG_TX_STOPPED;
580}
581
582/* The port lock is held and interrupts are disabled. */
583static void ip22zilog_start_tx(struct uart_port *port)
584{
585 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
586 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
587 unsigned char status;
588
589 up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
590 up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
591
592 status = readb(&channel->control);
593 ZSDELAY();
594
595 /* TX busy? Just wait for the TX done interrupt. */
596 if (!(status & Tx_BUF_EMP))
597 return;
598
599 /* Send the first character to jump-start the TX done
600 * IRQ sending engine.
601 */
602 if (port->x_char) {
603 writeb(port->x_char, &channel->data);
604 ZSDELAY();
605 ZS_WSYNC(channel);
606
607 port->icount.tx++;
608 port->x_char = 0;
609 } else {
610 struct circ_buf *xmit = &port->state->xmit;
611
612 writeb(xmit->buf[xmit->tail], &channel->data);
613 ZSDELAY();
614 ZS_WSYNC(channel);
615
616 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
617 port->icount.tx++;
618
619 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
620 uart_write_wakeup(&up->port);
621 }
622}
623
624/* The port lock is held and interrupts are disabled. */
625static void ip22zilog_stop_rx(struct uart_port *port)
626{
627 struct uart_ip22zilog_port *up = UART_ZILOG(port);
628 struct zilog_channel *channel;
629
630 if (ZS_IS_CONS(up))
631 return;
632
633 channel = ZILOG_CHANNEL_FROM_PORT(port);
634
635 /* Disable all RX interrupts. */
636 up->curregs[R1] &= ~RxINT_MASK;
637 ip22zilog_maybe_update_regs(up, channel);
638}
639
640/* The port lock is held. */
641static void ip22zilog_enable_ms(struct uart_port *port)
642{
643 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
644 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
645 unsigned char new_reg;
646
647 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
648 if (new_reg != up->curregs[R15]) {
649 up->curregs[R15] = new_reg;
650
651 /* NOTE: Not subject to 'transmitter active' rule. */
652 write_zsreg(channel, R15, up->curregs[R15]);
653 }
654}
655
656/* The port lock is not held. */
657static void ip22zilog_break_ctl(struct uart_port *port, int break_state)
658{
659 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
660 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
661 unsigned char set_bits, clear_bits, new_reg;
662 unsigned long flags;
663
664 set_bits = clear_bits = 0;
665
666 if (break_state)
667 set_bits |= SND_BRK;
668 else
669 clear_bits |= SND_BRK;
670
671 spin_lock_irqsave(&port->lock, flags);
672
673 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
674 if (new_reg != up->curregs[R5]) {
675 up->curregs[R5] = new_reg;
676
677 /* NOTE: Not subject to 'transmitter active' rule. */
678 write_zsreg(channel, R5, up->curregs[R5]);
679 }
680
681 spin_unlock_irqrestore(&port->lock, flags);
682}
683
684static void __ip22zilog_reset(struct uart_ip22zilog_port *up)
685{
686 struct zilog_channel *channel;
687 int i;
688
689 if (up->flags & IP22ZILOG_FLAG_RESET_DONE)
690 return;
691
692 /* Let pending transmits finish. */
693 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
694 for (i = 0; i < 1000; i++) {
695 unsigned char stat = read_zsreg(channel, R1);
696 if (stat & ALL_SNT)
697 break;
698 udelay(100);
699 }
700
701 if (!ZS_IS_CHANNEL_A(up)) {
702 up++;
703 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
704 }
705 write_zsreg(channel, R9, FHWRES);
706 ZSDELAY_LONG();
707 (void) read_zsreg(channel, R0);
708
709 up->flags |= IP22ZILOG_FLAG_RESET_DONE;
710 up->next->flags |= IP22ZILOG_FLAG_RESET_DONE;
711}
712
713static void __ip22zilog_startup(struct uart_ip22zilog_port *up)
714{
715 struct zilog_channel *channel;
716
717 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
718
719 __ip22zilog_reset(up);
720
721 __load_zsregs(channel, up->curregs);
722 /* set master interrupt enable */
723 write_zsreg(channel, R9, up->curregs[R9]);
724 up->prev_status = readb(&channel->control);
725
726 /* Enable receiver and transmitter. */
727 up->curregs[R3] |= RxENAB;
728 up->curregs[R5] |= TxENAB;
729
730 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
731 ip22zilog_maybe_update_regs(up, channel);
732}
733
734static int ip22zilog_startup(struct uart_port *port)
735{
736 struct uart_ip22zilog_port *up = UART_ZILOG(port);
737 unsigned long flags;
738
739 if (ZS_IS_CONS(up))
740 return 0;
741
742 spin_lock_irqsave(&port->lock, flags);
743 __ip22zilog_startup(up);
744 spin_unlock_irqrestore(&port->lock, flags);
745 return 0;
746}
747
748/*
749 * The test for ZS_IS_CONS is explained by the following e-mail:
750 *****
751 * From: Russell King <rmk@arm.linux.org.uk>
752 * Date: Sun, 8 Dec 2002 10:18:38 +0000
753 *
754 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
755 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
756 * > and I noticed that something is not right with reference
757 * > counting in this case. It seems that when the console
758 * > is open by kernel initially, this is not accounted
759 * > as an open, and uart_startup is not called.
760 *
761 * That is correct. We are unable to call uart_startup when the serial
762 * console is initialised because it may need to allocate memory (as
763 * request_irq does) and the memory allocators may not have been
764 * initialised.
765 *
766 * 1. initialise the port into a state where it can send characters in the
767 * console write method.
768 *
769 * 2. don't do the actual hardware shutdown in your shutdown() method (but
770 * do the normal software shutdown - ie, free irqs etc)
771 *****
772 */
773static void ip22zilog_shutdown(struct uart_port *port)
774{
775 struct uart_ip22zilog_port *up = UART_ZILOG(port);
776 struct zilog_channel *channel;
777 unsigned long flags;
778
779 if (ZS_IS_CONS(up))
780 return;
781
782 spin_lock_irqsave(&port->lock, flags);
783
784 channel = ZILOG_CHANNEL_FROM_PORT(port);
785
786 /* Disable receiver and transmitter. */
787 up->curregs[R3] &= ~RxENAB;
788 up->curregs[R5] &= ~TxENAB;
789
790 /* Disable all interrupts and BRK assertion. */
791 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
792 up->curregs[R5] &= ~SND_BRK;
793 ip22zilog_maybe_update_regs(up, channel);
794
795 spin_unlock_irqrestore(&port->lock, flags);
796}
797
798/* Shared by TTY driver and serial console setup. The port lock is held
799 * and local interrupts are disabled.
800 */
801static void
802ip22zilog_convert_to_zs(struct uart_ip22zilog_port *up, unsigned int cflag,
803 unsigned int iflag, int brg)
804{
805
806 up->curregs[R10] = NRZ;
807 up->curregs[R11] = TCBR | RCBR;
808
809 /* Program BAUD and clock source. */
810 up->curregs[R4] &= ~XCLK_MASK;
811 up->curregs[R4] |= X16CLK;
812 up->curregs[R12] = brg & 0xff;
813 up->curregs[R13] = (brg >> 8) & 0xff;
814 up->curregs[R14] = BRENAB;
815
816 /* Character size, stop bits, and parity. */
817 up->curregs[3] &= ~RxN_MASK;
818 up->curregs[5] &= ~TxN_MASK;
819 switch (cflag & CSIZE) {
820 case CS5:
821 up->curregs[3] |= Rx5;
822 up->curregs[5] |= Tx5;
823 up->parity_mask = 0x1f;
824 break;
825 case CS6:
826 up->curregs[3] |= Rx6;
827 up->curregs[5] |= Tx6;
828 up->parity_mask = 0x3f;
829 break;
830 case CS7:
831 up->curregs[3] |= Rx7;
832 up->curregs[5] |= Tx7;
833 up->parity_mask = 0x7f;
834 break;
835 case CS8:
836 default:
837 up->curregs[3] |= Rx8;
838 up->curregs[5] |= Tx8;
839 up->parity_mask = 0xff;
840 break;
841 };
842 up->curregs[4] &= ~0x0c;
843 if (cflag & CSTOPB)
844 up->curregs[4] |= SB2;
845 else
846 up->curregs[4] |= SB1;
847 if (cflag & PARENB)
848 up->curregs[4] |= PAR_ENAB;
849 else
850 up->curregs[4] &= ~PAR_ENAB;
851 if (!(cflag & PARODD))
852 up->curregs[4] |= PAR_EVEN;
853 else
854 up->curregs[4] &= ~PAR_EVEN;
855
856 up->port.read_status_mask = Rx_OVR;
857 if (iflag & INPCK)
858 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
859 if (iflag & (BRKINT | PARMRK))
860 up->port.read_status_mask |= BRK_ABRT;
861
862 up->port.ignore_status_mask = 0;
863 if (iflag & IGNPAR)
864 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
865 if (iflag & IGNBRK) {
866 up->port.ignore_status_mask |= BRK_ABRT;
867 if (iflag & IGNPAR)
868 up->port.ignore_status_mask |= Rx_OVR;
869 }
870
871 if ((cflag & CREAD) == 0)
872 up->port.ignore_status_mask = 0xff;
873}
874
875/* The port lock is not held. */
876static void
877ip22zilog_set_termios(struct uart_port *port, struct ktermios *termios,
878 struct ktermios *old)
879{
880 struct uart_ip22zilog_port *up = (struct uart_ip22zilog_port *) port;
881 unsigned long flags;
882 int baud, brg;
883
884 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
885
886 spin_lock_irqsave(&up->port.lock, flags);
887
888 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
889
890 ip22zilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
891
892 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
893 up->flags |= IP22ZILOG_FLAG_MODEM_STATUS;
894 else
895 up->flags &= ~IP22ZILOG_FLAG_MODEM_STATUS;
896
897 ip22zilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
898 uart_update_timeout(port, termios->c_cflag, baud);
899
900 spin_unlock_irqrestore(&up->port.lock, flags);
901}
902
903static const char *ip22zilog_type(struct uart_port *port)
904{
905 return "IP22-Zilog";
906}
907
908/* We do not request/release mappings of the registers here, this
909 * happens at early serial probe time.
910 */
911static void ip22zilog_release_port(struct uart_port *port)
912{
913}
914
915static int ip22zilog_request_port(struct uart_port *port)
916{
917 return 0;
918}
919
920/* These do not need to do anything interesting either. */
921static void ip22zilog_config_port(struct uart_port *port, int flags)
922{
923}
924
925/* We do not support letting the user mess with the divisor, IRQ, etc. */
926static int ip22zilog_verify_port(struct uart_port *port, struct serial_struct *ser)
927{
928 return -EINVAL;
929}
930
931static struct uart_ops ip22zilog_pops = {
932 .tx_empty = ip22zilog_tx_empty,
933 .set_mctrl = ip22zilog_set_mctrl,
934 .get_mctrl = ip22zilog_get_mctrl,
935 .stop_tx = ip22zilog_stop_tx,
936 .start_tx = ip22zilog_start_tx,
937 .stop_rx = ip22zilog_stop_rx,
938 .enable_ms = ip22zilog_enable_ms,
939 .break_ctl = ip22zilog_break_ctl,
940 .startup = ip22zilog_startup,
941 .shutdown = ip22zilog_shutdown,
942 .set_termios = ip22zilog_set_termios,
943 .type = ip22zilog_type,
944 .release_port = ip22zilog_release_port,
945 .request_port = ip22zilog_request_port,
946 .config_port = ip22zilog_config_port,
947 .verify_port = ip22zilog_verify_port,
948};
949
950static struct uart_ip22zilog_port *ip22zilog_port_table;
951static struct zilog_layout **ip22zilog_chip_regs;
952
953static struct uart_ip22zilog_port *ip22zilog_irq_chain;
954static int zilog_irq = -1;
955
956static void * __init alloc_one_table(unsigned long size)
957{
958 return kzalloc(size, GFP_KERNEL);
959}
960
961static void __init ip22zilog_alloc_tables(void)
962{
963 ip22zilog_port_table = (struct uart_ip22zilog_port *)
964 alloc_one_table(NUM_CHANNELS * sizeof(struct uart_ip22zilog_port));
965 ip22zilog_chip_regs = (struct zilog_layout **)
966 alloc_one_table(NUM_IP22ZILOG * sizeof(struct zilog_layout *));
967
968 if (ip22zilog_port_table == NULL || ip22zilog_chip_regs == NULL) {
969 panic("IP22-Zilog: Cannot allocate IP22-Zilog tables.");
970 }
971}
972
973/* Get the address of the registers for IP22-Zilog instance CHIP. */
974static struct zilog_layout * __init get_zs(int chip)
975{
976 unsigned long base;
977
978 if (chip < 0 || chip >= NUM_IP22ZILOG) {
979 panic("IP22-Zilog: Illegal chip number %d in get_zs.", chip);
980 }
981
982 /* Not probe-able, hard code it. */
983 base = (unsigned long) &sgioc->uart;
984
985 zilog_irq = SGI_SERIAL_IRQ;
986 request_mem_region(base, 8, "IP22-Zilog");
987
988 return (struct zilog_layout *) base;
989}
990
991#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
992
993#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
994static void ip22zilog_put_char(struct uart_port *port, int ch)
995{
996 struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
997 int loops = ZS_PUT_CHAR_MAX_DELAY;
998
999 /* This is a timed polling loop so do not switch the explicit
1000 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1001 */
1002 do {
1003 unsigned char val = readb(&channel->control);
1004 if (val & Tx_BUF_EMP) {
1005 ZSDELAY();
1006 break;
1007 }
1008 udelay(5);
1009 } while (--loops);
1010
1011 writeb(ch, &channel->data);
1012 ZSDELAY();
1013 ZS_WSYNC(channel);
1014}
1015
1016static void
1017ip22zilog_console_write(struct console *con, const char *s, unsigned int count)
1018{
1019 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1020 unsigned long flags;
1021
1022 spin_lock_irqsave(&up->port.lock, flags);
1023 uart_console_write(&up->port, s, count, ip22zilog_put_char);
1024 udelay(2);
1025 spin_unlock_irqrestore(&up->port.lock, flags);
1026}
1027
1028static int __init ip22zilog_console_setup(struct console *con, char *options)
1029{
1030 struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
1031 unsigned long flags;
1032 int baud = 9600, bits = 8;
1033 int parity = 'n';
1034 int flow = 'n';
1035
1036 up->flags |= IP22ZILOG_FLAG_IS_CONS;
1037
1038 printk(KERN_INFO "Console: ttyS%d (IP22-Zilog)\n", con->index);
1039
1040 spin_lock_irqsave(&up->port.lock, flags);
1041
1042 up->curregs[R15] |= BRKIE;
1043
1044 __ip22zilog_startup(up);
1045
1046 spin_unlock_irqrestore(&up->port.lock, flags);
1047
1048 if (options)
1049 uart_parse_options(options, &baud, &parity, &bits, &flow);
1050 return uart_set_options(&up->port, con, baud, parity, bits, flow);
1051}
1052
1053static struct uart_driver ip22zilog_reg;
1054
1055static struct console ip22zilog_console = {
1056 .name = "ttyS",
1057 .write = ip22zilog_console_write,
1058 .device = uart_console_device,
1059 .setup = ip22zilog_console_setup,
1060 .flags = CON_PRINTBUFFER,
1061 .index = -1,
1062 .data = &ip22zilog_reg,
1063};
1064#endif /* CONFIG_SERIAL_IP22_ZILOG_CONSOLE */
1065
1066static struct uart_driver ip22zilog_reg = {
1067 .owner = THIS_MODULE,
1068 .driver_name = "serial",
1069 .dev_name = "ttyS",
1070 .major = TTY_MAJOR,
1071 .minor = 64,
1072 .nr = NUM_CHANNELS,
1073#ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
1074 .cons = &ip22zilog_console,
1075#endif
1076};
1077
1078static void __init ip22zilog_prepare(void)
1079{
1080 struct uart_ip22zilog_port *up;
1081 struct zilog_layout *rp;
1082 int channel, chip;
1083
1084 /*
1085 * Temporary fix.
1086 */
1087 for (channel = 0; channel < NUM_CHANNELS; channel++)
1088 spin_lock_init(&ip22zilog_port_table[channel].port.lock);
1089
1090 ip22zilog_irq_chain = &ip22zilog_port_table[NUM_CHANNELS - 1];
1091 up = &ip22zilog_port_table[0];
1092 for (channel = NUM_CHANNELS - 1 ; channel > 0; channel--)
1093 up[channel].next = &up[channel - 1];
1094 up[channel].next = NULL;
1095
1096 for (chip = 0; chip < NUM_IP22ZILOG; chip++) {
1097 if (!ip22zilog_chip_regs[chip]) {
1098 ip22zilog_chip_regs[chip] = rp = get_zs(chip);
1099
1100 up[(chip * 2) + 0].port.membase = (char *) &rp->channelB;
1101 up[(chip * 2) + 1].port.membase = (char *) &rp->channelA;
1102
1103 /* In theory mapbase is the physical address ... */
1104 up[(chip * 2) + 0].port.mapbase =
1105 (unsigned long) ioremap((unsigned long) &rp->channelB, 8);
1106 up[(chip * 2) + 1].port.mapbase =
1107 (unsigned long) ioremap((unsigned long) &rp->channelA, 8);
1108 }
1109
1110 /* Channel A */
1111 up[(chip * 2) + 0].port.iotype = UPIO_MEM;
1112 up[(chip * 2) + 0].port.irq = zilog_irq;
1113 up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1114 up[(chip * 2) + 0].port.fifosize = 1;
1115 up[(chip * 2) + 0].port.ops = &ip22zilog_pops;
1116 up[(chip * 2) + 0].port.type = PORT_IP22ZILOG;
1117 up[(chip * 2) + 0].port.flags = 0;
1118 up[(chip * 2) + 0].port.line = (chip * 2) + 0;
1119 up[(chip * 2) + 0].flags = 0;
1120
1121 /* Channel B */
1122 up[(chip * 2) + 1].port.iotype = UPIO_MEM;
1123 up[(chip * 2) + 1].port.irq = zilog_irq;
1124 up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1125 up[(chip * 2) + 1].port.fifosize = 1;
1126 up[(chip * 2) + 1].port.ops = &ip22zilog_pops;
1127 up[(chip * 2) + 1].port.type = PORT_IP22ZILOG;
1128 up[(chip * 2) + 1].port.line = (chip * 2) + 1;
1129 up[(chip * 2) + 1].flags |= IP22ZILOG_FLAG_IS_CHANNEL_A;
1130 }
1131
1132 for (channel = 0; channel < NUM_CHANNELS; channel++) {
1133 struct uart_ip22zilog_port *up = &ip22zilog_port_table[channel];
1134 int brg;
1135
1136 /* Normal serial TTY. */
1137 up->parity_mask = 0xff;
1138 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1139 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1140 up->curregs[R3] = RxENAB | Rx8;
1141 up->curregs[R5] = TxENAB | Tx8;
1142 up->curregs[R9] = NV | MIE;
1143 up->curregs[R10] = NRZ;
1144 up->curregs[R11] = TCBR | RCBR;
1145 brg = BPS_TO_BRG(9600, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1146 up->curregs[R12] = (brg & 0xff);
1147 up->curregs[R13] = (brg >> 8) & 0xff;
1148 up->curregs[R14] = BRENAB;
1149 }
1150}
1151
1152static int __init ip22zilog_ports_init(void)
1153{
1154 int ret;
1155
1156 printk(KERN_INFO "Serial: IP22 Zilog driver (%d chips).\n", NUM_IP22ZILOG);
1157
1158 ip22zilog_prepare();
1159
1160 if (request_irq(zilog_irq, ip22zilog_interrupt, 0,
1161 "IP22-Zilog", ip22zilog_irq_chain)) {
1162 panic("IP22-Zilog: Unable to register zs interrupt handler.\n");
1163 }
1164
1165 ret = uart_register_driver(&ip22zilog_reg);
1166 if (ret == 0) {
1167 int i;
1168
1169 for (i = 0; i < NUM_CHANNELS; i++) {
1170 struct uart_ip22zilog_port *up = &ip22zilog_port_table[i];
1171
1172 uart_add_one_port(&ip22zilog_reg, &up->port);
1173 }
1174 }
1175
1176 return ret;
1177}
1178
1179static int __init ip22zilog_init(void)
1180{
1181 /* IP22 Zilog setup is hard coded, no probing to do. */
1182 ip22zilog_alloc_tables();
1183 ip22zilog_ports_init();
1184
1185 return 0;
1186}
1187
1188static void __exit ip22zilog_exit(void)
1189{
1190 int i;
1191 struct uart_ip22zilog_port *up;
1192
1193 for (i = 0; i < NUM_CHANNELS; i++) {
1194 up = &ip22zilog_port_table[i];
1195
1196 uart_remove_one_port(&ip22zilog_reg, &up->port);
1197 }
1198
1199 /* Free IO mem */
1200 up = &ip22zilog_port_table[0];
1201 for (i = 0; i < NUM_IP22ZILOG; i++) {
1202 if (up[(i * 2) + 0].port.mapbase) {
1203 iounmap((void*)up[(i * 2) + 0].port.mapbase);
1204 up[(i * 2) + 0].port.mapbase = 0;
1205 }
1206 if (up[(i * 2) + 1].port.mapbase) {
1207 iounmap((void*)up[(i * 2) + 1].port.mapbase);
1208 up[(i * 2) + 1].port.mapbase = 0;
1209 }
1210 }
1211
1212 uart_unregister_driver(&ip22zilog_reg);
1213}
1214
1215module_init(ip22zilog_init);
1216module_exit(ip22zilog_exit);
1217
1218/* David wrote it but I'm to blame for the bugs ... */
1219MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
1220MODULE_DESCRIPTION("SGI Zilog serial port driver");
1221MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ip22zilog.h b/drivers/tty/serial/ip22zilog.h
new file mode 100644
index 000000000000..a59a9a8341d2
--- /dev/null
+++ b/drivers/tty/serial/ip22zilog.h
@@ -0,0 +1,281 @@
1#ifndef _IP22_ZILOG_H
2#define _IP22_ZILOG_H
3
4#include <asm/byteorder.h>
5
6struct zilog_channel {
7#ifdef __BIG_ENDIAN
8 volatile unsigned char unused0[3];
9 volatile unsigned char control;
10 volatile unsigned char unused1[3];
11 volatile unsigned char data;
12#else /* __LITTLE_ENDIAN */
13 volatile unsigned char control;
14 volatile unsigned char unused0[3];
15 volatile unsigned char data;
16 volatile unsigned char unused1[3];
17#endif
18};
19
20struct zilog_layout {
21 struct zilog_channel channelB;
22 struct zilog_channel channelA;
23};
24
25#define NUM_ZSREGS 16
26
27/* Conversion routines to/from brg time constants from/to bits
28 * per second.
29 */
30#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
31#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
32
33/* The Zilog register set */
34
35#define FLAG 0x7e
36
37/* Write Register 0 */
38#define R0 0 /* Register selects */
39#define R1 1
40#define R2 2
41#define R3 3
42#define R4 4
43#define R5 5
44#define R6 6
45#define R7 7
46#define R8 8
47#define R9 9
48#define R10 10
49#define R11 11
50#define R12 12
51#define R13 13
52#define R14 14
53#define R15 15
54
55#define NULLCODE 0 /* Null Code */
56#define POINT_HIGH 0x8 /* Select upper half of registers */
57#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
58#define SEND_ABORT 0x18 /* HDLC Abort */
59#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
60#define RES_Tx_P 0x28 /* Reset TxINT Pending */
61#define ERR_RES 0x30 /* Error Reset */
62#define RES_H_IUS 0x38 /* Reset highest IUS */
63
64#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
65#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
66#define RES_EOM_L 0xC0 /* Reset EOM latch */
67
68/* Write Register 1 */
69
70#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
71#define TxINT_ENAB 0x2 /* Tx Int Enable */
72#define PAR_SPEC 0x4 /* Parity is special condition */
73
74#define RxINT_DISAB 0 /* Rx Int Disable */
75#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
76#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
77#define INT_ERR_Rx 0x18 /* Int on error only */
78#define RxINT_MASK 0x18
79
80#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
81#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
82#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
83
84/* Write Register #2 (Interrupt Vector) */
85
86/* Write Register 3 */
87
88#define RxENAB 0x1 /* Rx Enable */
89#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
90#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
91#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
92#define ENT_HM 0x10 /* Enter Hunt Mode */
93#define AUTO_ENAB 0x20 /* Auto Enables */
94#define Rx5 0x0 /* Rx 5 Bits/Character */
95#define Rx7 0x40 /* Rx 7 Bits/Character */
96#define Rx6 0x80 /* Rx 6 Bits/Character */
97#define Rx8 0xc0 /* Rx 8 Bits/Character */
98#define RxN_MASK 0xc0
99
100/* Write Register 4 */
101
102#define PAR_ENAB 0x1 /* Parity Enable */
103#define PAR_EVEN 0x2 /* Parity Even/Odd* */
104
105#define SYNC_ENAB 0 /* Sync Modes Enable */
106#define SB1 0x4 /* 1 stop bit/char */
107#define SB15 0x8 /* 1.5 stop bits/char */
108#define SB2 0xc /* 2 stop bits/char */
109
110#define MONSYNC 0 /* 8 Bit Sync character */
111#define BISYNC 0x10 /* 16 bit sync character */
112#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
113#define EXTSYNC 0x30 /* External Sync Mode */
114
115#define X1CLK 0x0 /* x1 clock mode */
116#define X16CLK 0x40 /* x16 clock mode */
117#define X32CLK 0x80 /* x32 clock mode */
118#define X64CLK 0xC0 /* x64 clock mode */
119#define XCLK_MASK 0xC0
120
121/* Write Register 5 */
122
123#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
124#define RTS 0x2 /* RTS */
125#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
126#define TxENAB 0x8 /* Tx Enable */
127#define SND_BRK 0x10 /* Send Break */
128#define Tx5 0x0 /* Tx 5 bits (or less)/character */
129#define Tx7 0x20 /* Tx 7 bits/character */
130#define Tx6 0x40 /* Tx 6 bits/character */
131#define Tx8 0x60 /* Tx 8 bits/character */
132#define TxN_MASK 0x60
133#define DTR 0x80 /* DTR */
134
135/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
136
137/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
138
139/* Write Register 8 (transmit buffer) */
140
141/* Write Register 9 (Master interrupt control) */
142#define VIS 1 /* Vector Includes Status */
143#define NV 2 /* No Vector */
144#define DLC 4 /* Disable Lower Chain */
145#define MIE 8 /* Master Interrupt Enable */
146#define STATHI 0x10 /* Status high */
147#define NORESET 0 /* No reset on write to R9 */
148#define CHRB 0x40 /* Reset channel B */
149#define CHRA 0x80 /* Reset channel A */
150#define FHWRES 0xc0 /* Force hardware reset */
151
152/* Write Register 10 (misc control bits) */
153#define BIT6 1 /* 6 bit/8bit sync */
154#define LOOPMODE 2 /* SDLC Loop mode */
155#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
156#define MARKIDLE 8 /* Mark/flag on idle */
157#define GAOP 0x10 /* Go active on poll */
158#define NRZ 0 /* NRZ mode */
159#define NRZI 0x20 /* NRZI mode */
160#define FM1 0x40 /* FM1 (transition = 1) */
161#define FM0 0x60 /* FM0 (transition = 0) */
162#define CRCPS 0x80 /* CRC Preset I/O */
163
164/* Write Register 11 (Clock Mode control) */
165#define TRxCXT 0 /* TRxC = Xtal output */
166#define TRxCTC 1 /* TRxC = Transmit clock */
167#define TRxCBR 2 /* TRxC = BR Generator Output */
168#define TRxCDP 3 /* TRxC = DPLL output */
169#define TRxCOI 4 /* TRxC O/I */
170#define TCRTxCP 0 /* Transmit clock = RTxC pin */
171#define TCTRxCP 8 /* Transmit clock = TRxC pin */
172#define TCBR 0x10 /* Transmit clock = BR Generator output */
173#define TCDPLL 0x18 /* Transmit clock = DPLL output */
174#define RCRTxCP 0 /* Receive clock = RTxC pin */
175#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
176#define RCBR 0x40 /* Receive clock = BR Generator output */
177#define RCDPLL 0x60 /* Receive clock = DPLL output */
178#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
179
180/* Write Register 12 (lower byte of baud rate generator time constant) */
181
182/* Write Register 13 (upper byte of baud rate generator time constant) */
183
184/* Write Register 14 (Misc control bits) */
185#define BRENAB 1 /* Baud rate generator enable */
186#define BRSRC 2 /* Baud rate generator source */
187#define DTRREQ 4 /* DTR/Request function */
188#define AUTOECHO 8 /* Auto Echo */
189#define LOOPBAK 0x10 /* Local loopback */
190#define SEARCH 0x20 /* Enter search mode */
191#define RMC 0x40 /* Reset missing clock */
192#define DISDPLL 0x60 /* Disable DPLL */
193#define SSBR 0x80 /* Set DPLL source = BR generator */
194#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
195#define SFMM 0xc0 /* Set FM mode */
196#define SNRZI 0xe0 /* Set NRZI mode */
197
198/* Write Register 15 (external/status interrupt control) */
199#define ZCIE 2 /* Zero count IE */
200#define DCDIE 8 /* DCD IE */
201#define SYNCIE 0x10 /* Sync/hunt IE */
202#define CTSIE 0x20 /* CTS IE */
203#define TxUIE 0x40 /* Tx Underrun/EOM IE */
204#define BRKIE 0x80 /* Break/Abort IE */
205
206
207/* Read Register 0 */
208#define Rx_CH_AV 0x1 /* Rx Character Available */
209#define ZCOUNT 0x2 /* Zero count */
210#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
211#define DCD 0x8 /* DCD */
212#define SYNC 0x10 /* Sync/hunt */
213#define CTS 0x20 /* CTS */
214#define TxEOM 0x40 /* Tx underrun */
215#define BRK_ABRT 0x80 /* Break/Abort */
216
217/* Read Register 1 */
218#define ALL_SNT 0x1 /* All sent */
219/* Residue Data for 8 Rx bits/char programmed */
220#define RES3 0x8 /* 0/3 */
221#define RES4 0x4 /* 0/4 */
222#define RES5 0xc /* 0/5 */
223#define RES6 0x2 /* 0/6 */
224#define RES7 0xa /* 0/7 */
225#define RES8 0x6 /* 0/8 */
226#define RES18 0xe /* 1/8 */
227#define RES28 0x0 /* 2/8 */
228/* Special Rx Condition Interrupts */
229#define PAR_ERR 0x10 /* Parity error */
230#define Rx_OVR 0x20 /* Rx Overrun Error */
231#define CRC_ERR 0x40 /* CRC/Framing Error */
232#define END_FR 0x80 /* End of Frame (SDLC) */
233
234/* Read Register 2 (channel b only) - Interrupt vector */
235#define CHB_Tx_EMPTY 0x00
236#define CHB_EXT_STAT 0x02
237#define CHB_Rx_AVAIL 0x04
238#define CHB_SPECIAL 0x06
239#define CHA_Tx_EMPTY 0x08
240#define CHA_EXT_STAT 0x0a
241#define CHA_Rx_AVAIL 0x0c
242#define CHA_SPECIAL 0x0e
243#define STATUS_MASK 0x0e
244
245/* Read Register 3 (interrupt pending register) ch a only */
246#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
247#define CHBTxIP 0x2 /* Channel B Tx IP */
248#define CHBRxIP 0x4 /* Channel B Rx IP */
249#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
250#define CHATxIP 0x10 /* Channel A Tx IP */
251#define CHARxIP 0x20 /* Channel A Rx IP */
252
253/* Read Register 8 (receive data register) */
254
255/* Read Register 10 (misc status bits) */
256#define ONLOOP 2 /* On loop */
257#define LOOPSEND 0x10 /* Loop sending */
258#define CLK2MIS 0x40 /* Two clocks missing */
259#define CLK1MIS 0x80 /* One clock missing */
260
261/* Read Register 12 (lower byte of baud rate generator constant) */
262
263/* Read Register 13 (upper byte of baud rate generator constant) */
264
265/* Read Register 15 (value of WR 15) */
266
267/* Misc macros */
268#define ZS_CLEARERR(channel) do { writeb(ERR_RES, &channel->control); \
269 udelay(5); } while(0)
270
271#define ZS_CLEARSTAT(channel) do { writeb(RES_EXT_INT, &channel->control); \
272 udelay(5); } while(0)
273
274#define ZS_CLEARFIFO(channel) do { readb(&channel->data); \
275 udelay(2); \
276 readb(&channel->data); \
277 udelay(2); \
278 readb(&channel->data); \
279 udelay(2); } while(0)
280
281#endif /* _IP22_ZILOG_H */
diff --git a/drivers/tty/serial/jsm/Makefile b/drivers/tty/serial/jsm/Makefile
new file mode 100644
index 000000000000..e46b6e0f8b18
--- /dev/null
+++ b/drivers/tty/serial/jsm/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for Jasmine adapter
3#
4
5obj-$(CONFIG_SERIAL_JSM) += jsm.o
6
7jsm-objs := jsm_driver.o jsm_neo.o jsm_tty.o
8
diff --git a/drivers/tty/serial/jsm/jsm.h b/drivers/tty/serial/jsm/jsm.h
new file mode 100644
index 000000000000..b704c8ce0d71
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm.h
@@ -0,0 +1,388 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
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, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the 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., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ibm.com>
24 *
25 ***********************************************************************/
26
27#ifndef __JSM_DRIVER_H
28#define __JSM_DRIVER_H
29
30#include <linux/kernel.h>
31#include <linux/types.h> /* To pick up the varions Linux types */
32#include <linux/tty.h>
33#include <linux/serial_core.h>
34#include <linux/device.h>
35
36/*
37 * Debugging levels can be set using debug insmod variable
38 * They can also be compiled out completely.
39 */
40enum {
41 DBG_INIT = 0x01,
42 DBG_BASIC = 0x02,
43 DBG_CORE = 0x04,
44 DBG_OPEN = 0x08,
45 DBG_CLOSE = 0x10,
46 DBG_READ = 0x20,
47 DBG_WRITE = 0x40,
48 DBG_IOCTL = 0x80,
49 DBG_PROC = 0x100,
50 DBG_PARAM = 0x200,
51 DBG_PSCAN = 0x400,
52 DBG_EVENT = 0x800,
53 DBG_DRAIN = 0x1000,
54 DBG_MSIGS = 0x2000,
55 DBG_MGMT = 0x4000,
56 DBG_INTR = 0x8000,
57 DBG_CARR = 0x10000,
58};
59
60#define jsm_printk(nlevel, klevel, pdev, fmt, args...) \
61 if ((DBG_##nlevel & jsm_debug)) \
62 dev_printk(KERN_##klevel, pdev->dev, fmt, ## args)
63
64#define MAXLINES 256
65#define MAXPORTS 8
66#define MAX_STOPS_SENT 5
67
68/* Board type definitions */
69
70#define T_NEO 0000
71#define T_CLASSIC 0001
72#define T_PCIBUS 0400
73
74/* Board State Definitions */
75
76#define BD_RUNNING 0x0
77#define BD_REASON 0x7f
78#define BD_NOTFOUND 0x1
79#define BD_NOIOPORT 0x2
80#define BD_NOMEM 0x3
81#define BD_NOBIOS 0x4
82#define BD_NOFEP 0x5
83#define BD_FAILED 0x6
84#define BD_ALLOCATED 0x7
85#define BD_TRIBOOT 0x8
86#define BD_BADKME 0x80
87
88
89/* 4 extra for alignment play space */
90#define WRITEBUFLEN ((4096) + 4)
91#define MYFLIPLEN N_TTY_BUF_SIZE
92
93#define JSM_VERSION "jsm: 1.2-1-INKERNEL"
94#define JSM_PARTNUM "40002438_A-INKERNEL"
95
96struct jsm_board;
97struct jsm_channel;
98
99/************************************************************************
100 * Per board operations structure *
101 ************************************************************************/
102struct board_ops {
103 irq_handler_t intr;
104 void (*uart_init) (struct jsm_channel *ch);
105 void (*uart_off) (struct jsm_channel *ch);
106 void (*param) (struct jsm_channel *ch);
107 void (*assert_modem_signals) (struct jsm_channel *ch);
108 void (*flush_uart_write) (struct jsm_channel *ch);
109 void (*flush_uart_read) (struct jsm_channel *ch);
110 void (*disable_receiver) (struct jsm_channel *ch);
111 void (*enable_receiver) (struct jsm_channel *ch);
112 void (*send_break) (struct jsm_channel *ch);
113 void (*clear_break) (struct jsm_channel *ch, int);
114 void (*send_start_character) (struct jsm_channel *ch);
115 void (*send_stop_character) (struct jsm_channel *ch);
116 void (*copy_data_from_queue_to_uart) (struct jsm_channel *ch);
117 u32 (*get_uart_bytes_left) (struct jsm_channel *ch);
118 void (*send_immediate_char) (struct jsm_channel *ch, unsigned char);
119};
120
121
122/*
123 * Per-board information
124 */
125struct jsm_board
126{
127 int boardnum; /* Board number: 0-32 */
128
129 int type; /* Type of board */
130 u8 rev; /* PCI revision ID */
131 struct pci_dev *pci_dev;
132 u32 maxports; /* MAX ports this board can handle */
133
134 spinlock_t bd_intr_lock; /* Used to protect the poller tasklet and
135 * the interrupt routine from each other.
136 */
137
138 u32 nasync; /* Number of ports on card */
139
140 u32 irq; /* Interrupt request number */
141
142 u64 membase; /* Start of base memory of the card */
143 u64 membase_end; /* End of base memory of the card */
144
145 u8 __iomem *re_map_membase;/* Remapped memory of the card */
146
147 u64 iobase; /* Start of io base of the card */
148 u64 iobase_end; /* End of io base of the card */
149
150 u32 bd_uart_offset; /* Space between each UART */
151
152 struct jsm_channel *channels[MAXPORTS]; /* array of pointers to our channels. */
153 char *flipbuf; /* Our flip buffer, alloced if board is found */
154
155 u32 bd_dividend; /* Board/UARTs specific dividend */
156
157 struct board_ops *bd_ops;
158
159 struct list_head jsm_board_entry;
160};
161
162/************************************************************************
163 * Device flag definitions for ch_flags.
164 ************************************************************************/
165#define CH_PRON 0x0001 /* Printer on string */
166#define CH_STOP 0x0002 /* Output is stopped */
167#define CH_STOPI 0x0004 /* Input is stopped */
168#define CH_CD 0x0008 /* Carrier is present */
169#define CH_FCAR 0x0010 /* Carrier forced on */
170#define CH_HANGUP 0x0020 /* Hangup received */
171
172#define CH_RECEIVER_OFF 0x0040 /* Receiver is off */
173#define CH_OPENING 0x0080 /* Port in fragile open state */
174#define CH_CLOSING 0x0100 /* Port in fragile close state */
175#define CH_FIFO_ENABLED 0x0200 /* Port has FIFOs enabled */
176#define CH_TX_FIFO_EMPTY 0x0400 /* TX Fifo is completely empty */
177#define CH_TX_FIFO_LWM 0x0800 /* TX Fifo is below Low Water */
178#define CH_BREAK_SENDING 0x1000 /* Break is being sent */
179#define CH_LOOPBACK 0x2000 /* Channel is in lookback mode */
180#define CH_FLIPBUF_IN_USE 0x4000 /* Channel's flipbuf is in use */
181#define CH_BAUD0 0x08000 /* Used for checking B0 transitions */
182
183/* Our Read/Error/Write queue sizes */
184#define RQUEUEMASK 0x1FFF /* 8 K - 1 */
185#define EQUEUEMASK 0x1FFF /* 8 K - 1 */
186#define WQUEUEMASK 0x0FFF /* 4 K - 1 */
187#define RQUEUESIZE (RQUEUEMASK + 1)
188#define EQUEUESIZE RQUEUESIZE
189#define WQUEUESIZE (WQUEUEMASK + 1)
190
191
192/************************************************************************
193 * Channel information structure.
194 ************************************************************************/
195struct jsm_channel {
196 struct uart_port uart_port;
197 struct jsm_board *ch_bd; /* Board structure pointer */
198
199 spinlock_t ch_lock; /* provide for serialization */
200 wait_queue_head_t ch_flags_wait;
201
202 u32 ch_portnum; /* Port number, 0 offset. */
203 u32 ch_open_count; /* open count */
204 u32 ch_flags; /* Channel flags */
205
206 u64 ch_close_delay; /* How long we should drop RTS/DTR for */
207
208 tcflag_t ch_c_iflag; /* channel iflags */
209 tcflag_t ch_c_cflag; /* channel cflags */
210 tcflag_t ch_c_oflag; /* channel oflags */
211 tcflag_t ch_c_lflag; /* channel lflags */
212 u8 ch_stopc; /* Stop character */
213 u8 ch_startc; /* Start character */
214
215 u8 ch_mostat; /* FEP output modem status */
216 u8 ch_mistat; /* FEP input modem status */
217
218 struct neo_uart_struct __iomem *ch_neo_uart; /* Pointer to the "mapped" UART struct */
219 u8 ch_cached_lsr; /* Cached value of the LSR register */
220
221 u8 *ch_rqueue; /* Our read queue buffer - malloc'ed */
222 u16 ch_r_head; /* Head location of the read queue */
223 u16 ch_r_tail; /* Tail location of the read queue */
224
225 u8 *ch_equeue; /* Our error queue buffer - malloc'ed */
226 u16 ch_e_head; /* Head location of the error queue */
227 u16 ch_e_tail; /* Tail location of the error queue */
228
229 u8 *ch_wqueue; /* Our write queue buffer - malloc'ed */
230 u16 ch_w_head; /* Head location of the write queue */
231 u16 ch_w_tail; /* Tail location of the write queue */
232
233 u64 ch_rxcount; /* total of data received so far */
234 u64 ch_txcount; /* total of data transmitted so far */
235
236 u8 ch_r_tlevel; /* Receive Trigger level */
237 u8 ch_t_tlevel; /* Transmit Trigger level */
238
239 u8 ch_r_watermark; /* Receive Watermark */
240
241
242 u32 ch_stops_sent; /* How many times I have sent a stop character
243 * to try to stop the other guy sending.
244 */
245 u64 ch_err_parity; /* Count of parity errors on channel */
246 u64 ch_err_frame; /* Count of framing errors on channel */
247 u64 ch_err_break; /* Count of breaks on channel */
248 u64 ch_err_overrun; /* Count of overruns on channel */
249
250 u64 ch_xon_sends; /* Count of xons transmitted */
251 u64 ch_xoff_sends; /* Count of xoffs transmitted */
252};
253
254
255/************************************************************************
256 * Per channel/port NEO UART structure *
257 ************************************************************************
258 * Base Structure Entries Usage Meanings to Host *
259 * *
260 * W = read write R = read only *
261 * U = Unused. *
262 ************************************************************************/
263
264struct neo_uart_struct {
265 u8 txrx; /* WR RHR/THR - Holding Reg */
266 u8 ier; /* WR IER - Interrupt Enable Reg */
267 u8 isr_fcr; /* WR ISR/FCR - Interrupt Status Reg/Fifo Control Reg */
268 u8 lcr; /* WR LCR - Line Control Reg */
269 u8 mcr; /* WR MCR - Modem Control Reg */
270 u8 lsr; /* WR LSR - Line Status Reg */
271 u8 msr; /* WR MSR - Modem Status Reg */
272 u8 spr; /* WR SPR - Scratch Pad Reg */
273 u8 fctr; /* WR FCTR - Feature Control Reg */
274 u8 efr; /* WR EFR - Enhanced Function Reg */
275 u8 tfifo; /* WR TXCNT/TXTRG - Transmit FIFO Reg */
276 u8 rfifo; /* WR RXCNT/RXTRG - Receive FIFO Reg */
277 u8 xoffchar1; /* WR XOFF 1 - XOff Character 1 Reg */
278 u8 xoffchar2; /* WR XOFF 2 - XOff Character 2 Reg */
279 u8 xonchar1; /* WR XON 1 - Xon Character 1 Reg */
280 u8 xonchar2; /* WR XON 2 - XOn Character 2 Reg */
281
282 u8 reserved1[0x2ff - 0x200]; /* U Reserved by Exar */
283 u8 txrxburst[64]; /* RW 64 bytes of RX/TX FIFO Data */
284 u8 reserved2[0x37f - 0x340]; /* U Reserved by Exar */
285 u8 rxburst_with_errors[64]; /* R 64 bytes of RX FIFO Data + LSR */
286};
287
288/* Where to read the extended interrupt register (32bits instead of 8bits) */
289#define UART_17158_POLL_ADDR_OFFSET 0x80
290
291/*
292 * These are the redefinitions for the FCTR on the XR17C158, since
293 * Exar made them different than their earlier design. (XR16C854)
294 */
295
296/* These are only applicable when table D is selected */
297#define UART_17158_FCTR_RTS_NODELAY 0x00
298#define UART_17158_FCTR_RTS_4DELAY 0x01
299#define UART_17158_FCTR_RTS_6DELAY 0x02
300#define UART_17158_FCTR_RTS_8DELAY 0x03
301#define UART_17158_FCTR_RTS_12DELAY 0x12
302#define UART_17158_FCTR_RTS_16DELAY 0x05
303#define UART_17158_FCTR_RTS_20DELAY 0x13
304#define UART_17158_FCTR_RTS_24DELAY 0x06
305#define UART_17158_FCTR_RTS_28DELAY 0x14
306#define UART_17158_FCTR_RTS_32DELAY 0x07
307#define UART_17158_FCTR_RTS_36DELAY 0x16
308#define UART_17158_FCTR_RTS_40DELAY 0x08
309#define UART_17158_FCTR_RTS_44DELAY 0x09
310#define UART_17158_FCTR_RTS_48DELAY 0x10
311#define UART_17158_FCTR_RTS_52DELAY 0x11
312
313#define UART_17158_FCTR_RTS_IRDA 0x10
314#define UART_17158_FCTR_RS485 0x20
315#define UART_17158_FCTR_TRGA 0x00
316#define UART_17158_FCTR_TRGB 0x40
317#define UART_17158_FCTR_TRGC 0x80
318#define UART_17158_FCTR_TRGD 0xC0
319
320/* 17158 trigger table selects.. */
321#define UART_17158_FCTR_BIT6 0x40
322#define UART_17158_FCTR_BIT7 0x80
323
324/* 17158 TX/RX memmapped buffer offsets */
325#define UART_17158_RX_FIFOSIZE 64
326#define UART_17158_TX_FIFOSIZE 64
327
328/* 17158 Extended IIR's */
329#define UART_17158_IIR_RDI_TIMEOUT 0x0C /* Receiver data TIMEOUT */
330#define UART_17158_IIR_XONXOFF 0x10 /* Received an XON/XOFF char */
331#define UART_17158_IIR_HWFLOW_STATE_CHANGE 0x20 /* CTS/DSR or RTS/DTR state change */
332#define UART_17158_IIR_FIFO_ENABLED 0xC0 /* 16550 FIFOs are Enabled */
333
334/*
335 * These are the extended interrupts that get sent
336 * back to us from the UART's 32bit interrupt register
337 */
338#define UART_17158_RX_LINE_STATUS 0x1 /* RX Ready */
339#define UART_17158_RXRDY_TIMEOUT 0x2 /* RX Ready Timeout */
340#define UART_17158_TXRDY 0x3 /* TX Ready */
341#define UART_17158_MSR 0x4 /* Modem State Change */
342#define UART_17158_TX_AND_FIFO_CLR 0x40 /* Transmitter Holding Reg Empty */
343#define UART_17158_RX_FIFO_DATA_ERROR 0x80 /* UART detected an RX FIFO Data error */
344
345/*
346 * These are the EXTENDED definitions for the 17C158's Interrupt
347 * Enable Register.
348 */
349#define UART_17158_EFR_ECB 0x10 /* Enhanced control bit */
350#define UART_17158_EFR_IXON 0x2 /* Receiver compares Xon1/Xoff1 */
351#define UART_17158_EFR_IXOFF 0x8 /* Transmit Xon1/Xoff1 */
352#define UART_17158_EFR_RTSDTR 0x40 /* Auto RTS/DTR Flow Control Enable */
353#define UART_17158_EFR_CTSDSR 0x80 /* Auto CTS/DSR Flow COntrol Enable */
354
355#define UART_17158_XOFF_DETECT 0x1 /* Indicates whether chip saw an incoming XOFF char */
356#define UART_17158_XON_DETECT 0x2 /* Indicates whether chip saw an incoming XON char */
357
358#define UART_17158_IER_RSVD1 0x10 /* Reserved by Exar */
359#define UART_17158_IER_XOFF 0x20 /* Xoff Interrupt Enable */
360#define UART_17158_IER_RTSDTR 0x40 /* Output Interrupt Enable */
361#define UART_17158_IER_CTSDSR 0x80 /* Input Interrupt Enable */
362
363#define PCI_DEVICE_NEO_2DB9_PCI_NAME "Neo 2 - DB9 Universal PCI"
364#define PCI_DEVICE_NEO_2DB9PRI_PCI_NAME "Neo 2 - DB9 Universal PCI - Powered Ring Indicator"
365#define PCI_DEVICE_NEO_2RJ45_PCI_NAME "Neo 2 - RJ45 Universal PCI"
366#define PCI_DEVICE_NEO_2RJ45PRI_PCI_NAME "Neo 2 - RJ45 Universal PCI - Powered Ring Indicator"
367#define PCIE_DEVICE_NEO_IBM_PCI_NAME "Neo 4 - PCI Express - IBM"
368
369/*
370 * Our Global Variables.
371 */
372extern struct uart_driver jsm_uart_driver;
373extern struct board_ops jsm_neo_ops;
374extern int jsm_debug;
375
376/*************************************************************************
377 *
378 * Prototypes for non-static functions used in more than one module
379 *
380 *************************************************************************/
381int jsm_tty_write(struct uart_port *port);
382int jsm_tty_init(struct jsm_board *);
383int jsm_uart_port_init(struct jsm_board *);
384int jsm_remove_uart_port(struct jsm_board *);
385void jsm_input(struct jsm_channel *ch);
386void jsm_check_queue_flow_control(struct jsm_channel *ch);
387
388#endif
diff --git a/drivers/tty/serial/jsm/jsm_driver.c b/drivers/tty/serial/jsm/jsm_driver.c
new file mode 100644
index 000000000000..96da17868cf3
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_driver.c
@@ -0,0 +1,297 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
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, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the 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., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ibm.com>
24 *
25 *
26 ***********************************************************************/
27#include <linux/moduleparam.h>
28#include <linux/pci.h>
29#include <linux/slab.h>
30
31#include "jsm.h"
32
33MODULE_AUTHOR("Digi International, http://www.digi.com");
34MODULE_DESCRIPTION("Driver for the Digi International "
35 "Neo PCI based product line");
36MODULE_LICENSE("GPL");
37MODULE_SUPPORTED_DEVICE("jsm");
38
39#define JSM_DRIVER_NAME "jsm"
40#define NR_PORTS 32
41#define JSM_MINOR_START 0
42
43struct uart_driver jsm_uart_driver = {
44 .owner = THIS_MODULE,
45 .driver_name = JSM_DRIVER_NAME,
46 .dev_name = "ttyn",
47 .major = 0,
48 .minor = JSM_MINOR_START,
49 .nr = NR_PORTS,
50};
51
52static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
53 pci_channel_state_t state);
54static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
55static void jsm_io_resume(struct pci_dev *pdev);
56
57static struct pci_error_handlers jsm_err_handler = {
58 .error_detected = jsm_io_error_detected,
59 .slot_reset = jsm_io_slot_reset,
60 .resume = jsm_io_resume,
61};
62
63int jsm_debug;
64module_param(jsm_debug, int, 0);
65MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
66
67static int __devinit jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
68{
69 int rc = 0;
70 struct jsm_board *brd;
71 static int adapter_count = 0;
72
73 rc = pci_enable_device(pdev);
74 if (rc) {
75 dev_err(&pdev->dev, "Device enable FAILED\n");
76 goto out;
77 }
78
79 rc = pci_request_regions(pdev, "jsm");
80 if (rc) {
81 dev_err(&pdev->dev, "pci_request_region FAILED\n");
82 goto out_disable_device;
83 }
84
85 brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
86 if (!brd) {
87 dev_err(&pdev->dev,
88 "memory allocation for board structure failed\n");
89 rc = -ENOMEM;
90 goto out_release_regions;
91 }
92
93 /* store the info for the board we've found */
94 brd->boardnum = adapter_count++;
95 brd->pci_dev = pdev;
96 if (pdev->device == PCIE_DEVICE_ID_NEO_4_IBM)
97 brd->maxports = 4;
98 else if (pdev->device == PCI_DEVICE_ID_DIGI_NEO_8)
99 brd->maxports = 8;
100 else
101 brd->maxports = 2;
102
103 spin_lock_init(&brd->bd_intr_lock);
104
105 /* store which revision we have */
106 brd->rev = pdev->revision;
107
108 brd->irq = pdev->irq;
109
110 jsm_printk(INIT, INFO, &brd->pci_dev,
111 "jsm_found_board - NEO adapter\n");
112
113 /* get the PCI Base Address Registers */
114 brd->membase = pci_resource_start(pdev, 0);
115 brd->membase_end = pci_resource_end(pdev, 0);
116
117 if (brd->membase & 1)
118 brd->membase &= ~3;
119 else
120 brd->membase &= ~15;
121
122 /* Assign the board_ops struct */
123 brd->bd_ops = &jsm_neo_ops;
124
125 brd->bd_uart_offset = 0x200;
126 brd->bd_dividend = 921600;
127
128 brd->re_map_membase = ioremap(brd->membase, pci_resource_len(pdev, 0));
129 if (!brd->re_map_membase) {
130 dev_err(&pdev->dev,
131 "card has no PCI Memory resources, "
132 "failing board.\n");
133 rc = -ENOMEM;
134 goto out_kfree_brd;
135 }
136
137 rc = request_irq(brd->irq, brd->bd_ops->intr,
138 IRQF_SHARED, "JSM", brd);
139 if (rc) {
140 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
141 goto out_iounmap;
142 }
143
144 rc = jsm_tty_init(brd);
145 if (rc < 0) {
146 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
147 rc = -ENXIO;
148 goto out_free_irq;
149 }
150
151 rc = jsm_uart_port_init(brd);
152 if (rc < 0) {
153 /* XXX: leaking all resources from jsm_tty_init here! */
154 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
155 rc = -ENXIO;
156 goto out_free_irq;
157 }
158
159 /* Log the information about the board */
160 dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
161 adapter_count, brd->rev, brd->irq);
162
163 /*
164 * allocate flip buffer for board.
165 *
166 * Okay to malloc with GFP_KERNEL, we are not at interrupt
167 * context, and there are no locks held.
168 */
169 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
170 if (!brd->flipbuf) {
171 /* XXX: leaking all resources from jsm_tty_init and
172 jsm_uart_port_init here! */
173 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
174 rc = -ENOMEM;
175 goto out_free_uart;
176 }
177
178 pci_set_drvdata(pdev, brd);
179 pci_save_state(pdev);
180
181 return 0;
182 out_free_uart:
183 jsm_remove_uart_port(brd);
184 out_free_irq:
185 jsm_remove_uart_port(brd);
186 free_irq(brd->irq, brd);
187 out_iounmap:
188 iounmap(brd->re_map_membase);
189 out_kfree_brd:
190 kfree(brd);
191 out_release_regions:
192 pci_release_regions(pdev);
193 out_disable_device:
194 pci_disable_device(pdev);
195 out:
196 return rc;
197}
198
199static void __devexit jsm_remove_one(struct pci_dev *pdev)
200{
201 struct jsm_board *brd = pci_get_drvdata(pdev);
202 int i = 0;
203
204 jsm_remove_uart_port(brd);
205
206 free_irq(brd->irq, brd);
207 iounmap(brd->re_map_membase);
208
209 /* Free all allocated channels structs */
210 for (i = 0; i < brd->maxports; i++) {
211 if (brd->channels[i]) {
212 kfree(brd->channels[i]->ch_rqueue);
213 kfree(brd->channels[i]->ch_equeue);
214 kfree(brd->channels[i]->ch_wqueue);
215 kfree(brd->channels[i]);
216 }
217 }
218
219 pci_release_regions(pdev);
220 pci_disable_device(pdev);
221 kfree(brd->flipbuf);
222 kfree(brd);
223}
224
225static struct pci_device_id jsm_pci_tbl[] = {
226 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
227 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
228 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
229 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
230 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
231 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
232 { 0, }
233};
234MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
235
236static struct pci_driver jsm_driver = {
237 .name = "jsm",
238 .id_table = jsm_pci_tbl,
239 .probe = jsm_probe_one,
240 .remove = __devexit_p(jsm_remove_one),
241 .err_handler = &jsm_err_handler,
242};
243
244static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
245 pci_channel_state_t state)
246{
247 struct jsm_board *brd = pci_get_drvdata(pdev);
248
249 jsm_remove_uart_port(brd);
250
251 return PCI_ERS_RESULT_NEED_RESET;
252}
253
254static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
255{
256 int rc;
257
258 rc = pci_enable_device(pdev);
259
260 if (rc)
261 return PCI_ERS_RESULT_DISCONNECT;
262
263 pci_set_master(pdev);
264
265 return PCI_ERS_RESULT_RECOVERED;
266}
267
268static void jsm_io_resume(struct pci_dev *pdev)
269{
270 struct jsm_board *brd = pci_get_drvdata(pdev);
271
272 pci_restore_state(pdev);
273
274 jsm_uart_port_init(brd);
275}
276
277static int __init jsm_init_module(void)
278{
279 int rc;
280
281 rc = uart_register_driver(&jsm_uart_driver);
282 if (!rc) {
283 rc = pci_register_driver(&jsm_driver);
284 if (rc)
285 uart_unregister_driver(&jsm_uart_driver);
286 }
287 return rc;
288}
289
290static void __exit jsm_exit_module(void)
291{
292 pci_unregister_driver(&jsm_driver);
293 uart_unregister_driver(&jsm_uart_driver);
294}
295
296module_init(jsm_init_module);
297module_exit(jsm_exit_module);
diff --git a/drivers/tty/serial/jsm/jsm_neo.c b/drivers/tty/serial/jsm/jsm_neo.c
new file mode 100644
index 000000000000..4538c3e3646e
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_neo.c
@@ -0,0 +1,1412 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
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, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the 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., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ibm.com>
24 *
25 ***********************************************************************/
26#include <linux/delay.h> /* For udelay */
27#include <linux/serial_reg.h> /* For the various UART offsets */
28#include <linux/tty.h>
29#include <linux/pci.h>
30#include <asm/io.h>
31
32#include "jsm.h" /* Driver main header file */
33
34static u32 jsm_offset_table[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
35
36/*
37 * This function allows calls to ensure that all outstanding
38 * PCI writes have been completed, by doing a PCI read against
39 * a non-destructive, read-only location on the Neo card.
40 *
41 * In this case, we are reading the DVID (Read-only Device Identification)
42 * value of the Neo card.
43 */
44static inline void neo_pci_posting_flush(struct jsm_board *bd)
45{
46 readb(bd->re_map_membase + 0x8D);
47}
48
49static void neo_set_cts_flow_control(struct jsm_channel *ch)
50{
51 u8 ier, efr;
52 ier = readb(&ch->ch_neo_uart->ier);
53 efr = readb(&ch->ch_neo_uart->efr);
54
55 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting CTSFLOW\n");
56
57 /* Turn on auto CTS flow control */
58 ier |= (UART_17158_IER_CTSDSR);
59 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
60
61 /* Turn off auto Xon flow control */
62 efr &= ~(UART_17158_EFR_IXON);
63
64 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
65 writeb(0, &ch->ch_neo_uart->efr);
66
67 /* Turn on UART enhanced bits */
68 writeb(efr, &ch->ch_neo_uart->efr);
69
70 /* Turn on table D, with 8 char hi/low watermarks */
71 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
72
73 /* Feed the UART our trigger levels */
74 writeb(8, &ch->ch_neo_uart->tfifo);
75 ch->ch_t_tlevel = 8;
76
77 writeb(ier, &ch->ch_neo_uart->ier);
78}
79
80static void neo_set_rts_flow_control(struct jsm_channel *ch)
81{
82 u8 ier, efr;
83 ier = readb(&ch->ch_neo_uart->ier);
84 efr = readb(&ch->ch_neo_uart->efr);
85
86 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting RTSFLOW\n");
87
88 /* Turn on auto RTS flow control */
89 ier |= (UART_17158_IER_RTSDTR);
90 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
91
92 /* Turn off auto Xoff flow control */
93 ier &= ~(UART_17158_IER_XOFF);
94 efr &= ~(UART_17158_EFR_IXOFF);
95
96 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
97 writeb(0, &ch->ch_neo_uart->efr);
98
99 /* Turn on UART enhanced bits */
100 writeb(efr, &ch->ch_neo_uart->efr);
101
102 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
103 ch->ch_r_watermark = 4;
104
105 writeb(56, &ch->ch_neo_uart->rfifo);
106 ch->ch_r_tlevel = 56;
107
108 writeb(ier, &ch->ch_neo_uart->ier);
109
110 /*
111 * From the Neo UART spec sheet:
112 * The auto RTS/DTR function must be started by asserting
113 * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
114 * it is enabled.
115 */
116 ch->ch_mostat |= (UART_MCR_RTS);
117}
118
119
120static void neo_set_ixon_flow_control(struct jsm_channel *ch)
121{
122 u8 ier, efr;
123 ier = readb(&ch->ch_neo_uart->ier);
124 efr = readb(&ch->ch_neo_uart->efr);
125
126 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXON FLOW\n");
127
128 /* Turn off auto CTS flow control */
129 ier &= ~(UART_17158_IER_CTSDSR);
130 efr &= ~(UART_17158_EFR_CTSDSR);
131
132 /* Turn on auto Xon flow control */
133 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
134
135 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
136 writeb(0, &ch->ch_neo_uart->efr);
137
138 /* Turn on UART enhanced bits */
139 writeb(efr, &ch->ch_neo_uart->efr);
140
141 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
142 ch->ch_r_watermark = 4;
143
144 writeb(32, &ch->ch_neo_uart->rfifo);
145 ch->ch_r_tlevel = 32;
146
147 /* Tell UART what start/stop chars it should be looking for */
148 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
149 writeb(0, &ch->ch_neo_uart->xonchar2);
150
151 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
152 writeb(0, &ch->ch_neo_uart->xoffchar2);
153
154 writeb(ier, &ch->ch_neo_uart->ier);
155}
156
157static void neo_set_ixoff_flow_control(struct jsm_channel *ch)
158{
159 u8 ier, efr;
160 ier = readb(&ch->ch_neo_uart->ier);
161 efr = readb(&ch->ch_neo_uart->efr);
162
163 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXOFF FLOW\n");
164
165 /* Turn off auto RTS flow control */
166 ier &= ~(UART_17158_IER_RTSDTR);
167 efr &= ~(UART_17158_EFR_RTSDTR);
168
169 /* Turn on auto Xoff flow control */
170 ier |= (UART_17158_IER_XOFF);
171 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
172
173 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
174 writeb(0, &ch->ch_neo_uart->efr);
175
176 /* Turn on UART enhanced bits */
177 writeb(efr, &ch->ch_neo_uart->efr);
178
179 /* Turn on table D, with 8 char hi/low watermarks */
180 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
181
182 writeb(8, &ch->ch_neo_uart->tfifo);
183 ch->ch_t_tlevel = 8;
184
185 /* Tell UART what start/stop chars it should be looking for */
186 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
187 writeb(0, &ch->ch_neo_uart->xonchar2);
188
189 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
190 writeb(0, &ch->ch_neo_uart->xoffchar2);
191
192 writeb(ier, &ch->ch_neo_uart->ier);
193}
194
195static void neo_set_no_input_flow_control(struct jsm_channel *ch)
196{
197 u8 ier, efr;
198 ier = readb(&ch->ch_neo_uart->ier);
199 efr = readb(&ch->ch_neo_uart->efr);
200
201 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Input FLOW\n");
202
203 /* Turn off auto RTS flow control */
204 ier &= ~(UART_17158_IER_RTSDTR);
205 efr &= ~(UART_17158_EFR_RTSDTR);
206
207 /* Turn off auto Xoff flow control */
208 ier &= ~(UART_17158_IER_XOFF);
209 if (ch->ch_c_iflag & IXON)
210 efr &= ~(UART_17158_EFR_IXOFF);
211 else
212 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
213
214 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
215 writeb(0, &ch->ch_neo_uart->efr);
216
217 /* Turn on UART enhanced bits */
218 writeb(efr, &ch->ch_neo_uart->efr);
219
220 /* Turn on table D, with 8 char hi/low watermarks */
221 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
222
223 ch->ch_r_watermark = 0;
224
225 writeb(16, &ch->ch_neo_uart->tfifo);
226 ch->ch_t_tlevel = 16;
227
228 writeb(16, &ch->ch_neo_uart->rfifo);
229 ch->ch_r_tlevel = 16;
230
231 writeb(ier, &ch->ch_neo_uart->ier);
232}
233
234static void neo_set_no_output_flow_control(struct jsm_channel *ch)
235{
236 u8 ier, efr;
237 ier = readb(&ch->ch_neo_uart->ier);
238 efr = readb(&ch->ch_neo_uart->efr);
239
240 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Output FLOW\n");
241
242 /* Turn off auto CTS flow control */
243 ier &= ~(UART_17158_IER_CTSDSR);
244 efr &= ~(UART_17158_EFR_CTSDSR);
245
246 /* Turn off auto Xon flow control */
247 if (ch->ch_c_iflag & IXOFF)
248 efr &= ~(UART_17158_EFR_IXON);
249 else
250 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
251
252 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
253 writeb(0, &ch->ch_neo_uart->efr);
254
255 /* Turn on UART enhanced bits */
256 writeb(efr, &ch->ch_neo_uart->efr);
257
258 /* Turn on table D, with 8 char hi/low watermarks */
259 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
260
261 ch->ch_r_watermark = 0;
262
263 writeb(16, &ch->ch_neo_uart->tfifo);
264 ch->ch_t_tlevel = 16;
265
266 writeb(16, &ch->ch_neo_uart->rfifo);
267 ch->ch_r_tlevel = 16;
268
269 writeb(ier, &ch->ch_neo_uart->ier);
270}
271
272static inline void neo_set_new_start_stop_chars(struct jsm_channel *ch)
273{
274
275 /* if hardware flow control is set, then skip this whole thing */
276 if (ch->ch_c_cflag & CRTSCTS)
277 return;
278
279 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "start\n");
280
281 /* Tell UART what start/stop chars it should be looking for */
282 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
283 writeb(0, &ch->ch_neo_uart->xonchar2);
284
285 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
286 writeb(0, &ch->ch_neo_uart->xoffchar2);
287}
288
289static void neo_copy_data_from_uart_to_queue(struct jsm_channel *ch)
290{
291 int qleft = 0;
292 u8 linestatus = 0;
293 u8 error_mask = 0;
294 int n = 0;
295 int total = 0;
296 u16 head;
297 u16 tail;
298
299 if (!ch)
300 return;
301
302 /* cache head and tail of queue */
303 head = ch->ch_r_head & RQUEUEMASK;
304 tail = ch->ch_r_tail & RQUEUEMASK;
305
306 /* Get our cached LSR */
307 linestatus = ch->ch_cached_lsr;
308 ch->ch_cached_lsr = 0;
309
310 /* Store how much space we have left in the queue */
311 if ((qleft = tail - head - 1) < 0)
312 qleft += RQUEUEMASK + 1;
313
314 /*
315 * If the UART is not in FIFO mode, force the FIFO copy to
316 * NOT be run, by setting total to 0.
317 *
318 * On the other hand, if the UART IS in FIFO mode, then ask
319 * the UART to give us an approximation of data it has RX'ed.
320 */
321 if (!(ch->ch_flags & CH_FIFO_ENABLED))
322 total = 0;
323 else {
324 total = readb(&ch->ch_neo_uart->rfifo);
325
326 /*
327 * EXAR chip bug - RX FIFO COUNT - Fudge factor.
328 *
329 * This resolves a problem/bug with the Exar chip that sometimes
330 * returns a bogus value in the rfifo register.
331 * The count can be any where from 0-3 bytes "off".
332 * Bizarre, but true.
333 */
334 total -= 3;
335 }
336
337 /*
338 * Finally, bound the copy to make sure we don't overflow
339 * our own queue...
340 * The byte by byte copy loop below this loop this will
341 * deal with the queue overflow possibility.
342 */
343 total = min(total, qleft);
344
345 while (total > 0) {
346 /*
347 * Grab the linestatus register, we need to check
348 * to see if there are any errors in the FIFO.
349 */
350 linestatus = readb(&ch->ch_neo_uart->lsr);
351
352 /*
353 * Break out if there is a FIFO error somewhere.
354 * This will allow us to go byte by byte down below,
355 * finding the exact location of the error.
356 */
357 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
358 break;
359
360 /* Make sure we don't go over the end of our queue */
361 n = min(((u32) total), (RQUEUESIZE - (u32) head));
362
363 /*
364 * Cut down n even further if needed, this is to fix
365 * a problem with memcpy_fromio() with the Neo on the
366 * IBM pSeries platform.
367 * 15 bytes max appears to be the magic number.
368 */
369 n = min((u32) n, (u32) 12);
370
371 /*
372 * Since we are grabbing the linestatus register, which
373 * will reset some bits after our read, we need to ensure
374 * we don't miss our TX FIFO emptys.
375 */
376 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
377 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
378
379 linestatus = 0;
380
381 /* Copy data from uart to the queue */
382 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, n);
383 /*
384 * Since RX_FIFO_DATA_ERROR was 0, we are guaranteed
385 * that all the data currently in the FIFO is free of
386 * breaks and parity/frame/orun errors.
387 */
388 memset(ch->ch_equeue + head, 0, n);
389
390 /* Add to and flip head if needed */
391 head = (head + n) & RQUEUEMASK;
392 total -= n;
393 qleft -= n;
394 ch->ch_rxcount += n;
395 }
396
397 /*
398 * Create a mask to determine whether we should
399 * insert the character (if any) into our queue.
400 */
401 if (ch->ch_c_iflag & IGNBRK)
402 error_mask |= UART_LSR_BI;
403
404 /*
405 * Now cleanup any leftover bytes still in the UART.
406 * Also deal with any possible queue overflow here as well.
407 */
408 while (1) {
409
410 /*
411 * Its possible we have a linestatus from the loop above
412 * this, so we "OR" on any extra bits.
413 */
414 linestatus |= readb(&ch->ch_neo_uart->lsr);
415
416 /*
417 * If the chip tells us there is no more data pending to
418 * be read, we can then leave.
419 * But before we do, cache the linestatus, just in case.
420 */
421 if (!(linestatus & UART_LSR_DR)) {
422 ch->ch_cached_lsr = linestatus;
423 break;
424 }
425
426 /* No need to store this bit */
427 linestatus &= ~UART_LSR_DR;
428
429 /*
430 * Since we are grabbing the linestatus register, which
431 * will reset some bits after our read, we need to ensure
432 * we don't miss our TX FIFO emptys.
433 */
434 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
435 linestatus &= ~(UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR);
436 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
437 }
438
439 /*
440 * Discard character if we are ignoring the error mask.
441 */
442 if (linestatus & error_mask) {
443 u8 discard;
444 linestatus = 0;
445 memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
446 continue;
447 }
448
449 /*
450 * If our queue is full, we have no choice but to drop some data.
451 * The assumption is that HWFLOW or SWFLOW should have stopped
452 * things way way before we got to this point.
453 *
454 * I decided that I wanted to ditch the oldest data first,
455 * I hope thats okay with everyone? Yes? Good.
456 */
457 while (qleft < 1) {
458 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
459 "Queue full, dropping DATA:%x LSR:%x\n",
460 ch->ch_rqueue[tail], ch->ch_equeue[tail]);
461
462 ch->ch_r_tail = tail = (tail + 1) & RQUEUEMASK;
463 ch->ch_err_overrun++;
464 qleft++;
465 }
466
467 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, 1);
468 ch->ch_equeue[head] = (u8) linestatus;
469
470 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
471 "DATA/LSR pair: %x %x\n", ch->ch_rqueue[head], ch->ch_equeue[head]);
472
473 /* Ditch any remaining linestatus value. */
474 linestatus = 0;
475
476 /* Add to and flip head if needed */
477 head = (head + 1) & RQUEUEMASK;
478
479 qleft--;
480 ch->ch_rxcount++;
481 }
482
483 /*
484 * Write new final heads to channel structure.
485 */
486 ch->ch_r_head = head & RQUEUEMASK;
487 ch->ch_e_head = head & EQUEUEMASK;
488 jsm_input(ch);
489}
490
491static void neo_copy_data_from_queue_to_uart(struct jsm_channel *ch)
492{
493 u16 head;
494 u16 tail;
495 int n;
496 int s;
497 int qlen;
498 u32 len_written = 0;
499
500 if (!ch)
501 return;
502
503 /* No data to write to the UART */
504 if (ch->ch_w_tail == ch->ch_w_head)
505 return;
506
507 /* If port is "stopped", don't send any data to the UART */
508 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
509 return;
510 /*
511 * If FIFOs are disabled. Send data directly to txrx register
512 */
513 if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
514 u8 lsrbits = readb(&ch->ch_neo_uart->lsr);
515
516 ch->ch_cached_lsr |= lsrbits;
517 if (ch->ch_cached_lsr & UART_LSR_THRE) {
518 ch->ch_cached_lsr &= ~(UART_LSR_THRE);
519
520 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_neo_uart->txrx);
521 jsm_printk(WRITE, INFO, &ch->ch_bd->pci_dev,
522 "Tx data: %x\n", ch->ch_wqueue[ch->ch_w_head]);
523 ch->ch_w_tail++;
524 ch->ch_w_tail &= WQUEUEMASK;
525 ch->ch_txcount++;
526 }
527 return;
528 }
529
530 /*
531 * We have to do it this way, because of the EXAR TXFIFO count bug.
532 */
533 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
534 return;
535
536 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
537
538 /* cache head and tail of queue */
539 head = ch->ch_w_head & WQUEUEMASK;
540 tail = ch->ch_w_tail & WQUEUEMASK;
541 qlen = (head - tail) & WQUEUEMASK;
542
543 /* Find minimum of the FIFO space, versus queue length */
544 n = min(n, qlen);
545
546 while (n > 0) {
547
548 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
549 s = min(s, n);
550
551 if (s <= 0)
552 break;
553
554 memcpy_toio(&ch->ch_neo_uart->txrxburst, ch->ch_wqueue + tail, s);
555 /* Add and flip queue if needed */
556 tail = (tail + s) & WQUEUEMASK;
557 n -= s;
558 ch->ch_txcount += s;
559 len_written += s;
560 }
561
562 /* Update the final tail */
563 ch->ch_w_tail = tail & WQUEUEMASK;
564
565 if (len_written >= ch->ch_t_tlevel)
566 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
567
568 if (!jsm_tty_write(&ch->uart_port))
569 uart_write_wakeup(&ch->uart_port);
570}
571
572static void neo_parse_modem(struct jsm_channel *ch, u8 signals)
573{
574 u8 msignals = signals;
575
576 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
577 "neo_parse_modem: port: %d msignals: %x\n", ch->ch_portnum, msignals);
578
579 /* Scrub off lower bits. They signify delta's, which I don't care about */
580 /* Keep DDCD and DDSR though */
581 msignals &= 0xf8;
582
583 if (msignals & UART_MSR_DDCD)
584 uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_DCD);
585 if (msignals & UART_MSR_DDSR)
586 uart_handle_cts_change(&ch->uart_port, msignals & UART_MSR_CTS);
587 if (msignals & UART_MSR_DCD)
588 ch->ch_mistat |= UART_MSR_DCD;
589 else
590 ch->ch_mistat &= ~UART_MSR_DCD;
591
592 if (msignals & UART_MSR_DSR)
593 ch->ch_mistat |= UART_MSR_DSR;
594 else
595 ch->ch_mistat &= ~UART_MSR_DSR;
596
597 if (msignals & UART_MSR_RI)
598 ch->ch_mistat |= UART_MSR_RI;
599 else
600 ch->ch_mistat &= ~UART_MSR_RI;
601
602 if (msignals & UART_MSR_CTS)
603 ch->ch_mistat |= UART_MSR_CTS;
604 else
605 ch->ch_mistat &= ~UART_MSR_CTS;
606
607 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
608 "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
609 ch->ch_portnum,
610 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
611 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
612 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
613 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
614 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
615 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
616}
617
618/* Make the UART raise any of the output signals we want up */
619static void neo_assert_modem_signals(struct jsm_channel *ch)
620{
621 if (!ch)
622 return;
623
624 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
625
626 /* flush write operation */
627 neo_pci_posting_flush(ch->ch_bd);
628}
629
630/*
631 * Flush the WRITE FIFO on the Neo.
632 *
633 * NOTE: Channel lock MUST be held before calling this function!
634 */
635static void neo_flush_uart_write(struct jsm_channel *ch)
636{
637 u8 tmp = 0;
638 int i = 0;
639
640 if (!ch)
641 return;
642
643 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
644
645 for (i = 0; i < 10; i++) {
646
647 /* Check to see if the UART feels it completely flushed the FIFO. */
648 tmp = readb(&ch->ch_neo_uart->isr_fcr);
649 if (tmp & 4) {
650 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
651 "Still flushing TX UART... i: %d\n", i);
652 udelay(10);
653 }
654 else
655 break;
656 }
657
658 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
659}
660
661
662/*
663 * Flush the READ FIFO on the Neo.
664 *
665 * NOTE: Channel lock MUST be held before calling this function!
666 */
667static void neo_flush_uart_read(struct jsm_channel *ch)
668{
669 u8 tmp = 0;
670 int i = 0;
671
672 if (!ch)
673 return;
674
675 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR), &ch->ch_neo_uart->isr_fcr);
676
677 for (i = 0; i < 10; i++) {
678
679 /* Check to see if the UART feels it completely flushed the FIFO. */
680 tmp = readb(&ch->ch_neo_uart->isr_fcr);
681 if (tmp & 2) {
682 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
683 "Still flushing RX UART... i: %d\n", i);
684 udelay(10);
685 }
686 else
687 break;
688 }
689}
690
691/*
692 * No locks are assumed to be held when calling this function.
693 */
694static void neo_clear_break(struct jsm_channel *ch, int force)
695{
696 unsigned long lock_flags;
697
698 spin_lock_irqsave(&ch->ch_lock, lock_flags);
699
700 /* Turn break off, and unset some variables */
701 if (ch->ch_flags & CH_BREAK_SENDING) {
702 u8 temp = readb(&ch->ch_neo_uart->lcr);
703 writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
704
705 ch->ch_flags &= ~(CH_BREAK_SENDING);
706 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
707 "clear break Finishing UART_LCR_SBC! finished: %lx\n", jiffies);
708
709 /* flush write operation */
710 neo_pci_posting_flush(ch->ch_bd);
711 }
712 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
713}
714
715/*
716 * Parse the ISR register.
717 */
718static inline void neo_parse_isr(struct jsm_board *brd, u32 port)
719{
720 struct jsm_channel *ch;
721 u8 isr;
722 u8 cause;
723 unsigned long lock_flags;
724
725 if (!brd)
726 return;
727
728 if (port > brd->maxports)
729 return;
730
731 ch = brd->channels[port];
732 if (!ch)
733 return;
734
735 /* Here we try to figure out what caused the interrupt to happen */
736 while (1) {
737
738 isr = readb(&ch->ch_neo_uart->isr_fcr);
739
740 /* Bail if no pending interrupt */
741 if (isr & UART_IIR_NO_INT)
742 break;
743
744 /*
745 * Yank off the upper 2 bits, which just show that the FIFO's are enabled.
746 */
747 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
748
749 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
750 "%s:%d isr: %x\n", __FILE__, __LINE__, isr);
751
752 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
753 /* Read data from uart -> queue */
754 neo_copy_data_from_uart_to_queue(ch);
755
756 /* Call our tty layer to enforce queue flow control if needed. */
757 spin_lock_irqsave(&ch->ch_lock, lock_flags);
758 jsm_check_queue_flow_control(ch);
759 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
760 }
761
762 if (isr & UART_IIR_THRI) {
763 /* Transfer data (if any) from Write Queue -> UART. */
764 spin_lock_irqsave(&ch->ch_lock, lock_flags);
765 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
766 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
767 neo_copy_data_from_queue_to_uart(ch);
768 }
769
770 if (isr & UART_17158_IIR_XONXOFF) {
771 cause = readb(&ch->ch_neo_uart->xoffchar1);
772
773 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
774 "Port %d. Got ISR_XONXOFF: cause:%x\n", port, cause);
775
776 /*
777 * Since the UART detected either an XON or
778 * XOFF match, we need to figure out which
779 * one it was, so we can suspend or resume data flow.
780 */
781 spin_lock_irqsave(&ch->ch_lock, lock_flags);
782 if (cause == UART_17158_XON_DETECT) {
783 /* Is output stopped right now, if so, resume it */
784 if (brd->channels[port]->ch_flags & CH_STOP) {
785 ch->ch_flags &= ~(CH_STOP);
786 }
787 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
788 "Port %d. XON detected in incoming data\n", port);
789 }
790 else if (cause == UART_17158_XOFF_DETECT) {
791 if (!(brd->channels[port]->ch_flags & CH_STOP)) {
792 ch->ch_flags |= CH_STOP;
793 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
794 "Setting CH_STOP\n");
795 }
796 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
797 "Port: %d. XOFF detected in incoming data\n", port);
798 }
799 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
800 }
801
802 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
803 /*
804 * If we get here, this means the hardware is doing auto flow control.
805 * Check to see whether RTS/DTR or CTS/DSR caused this interrupt.
806 */
807 cause = readb(&ch->ch_neo_uart->mcr);
808
809 /* Which pin is doing auto flow? RTS or DTR? */
810 spin_lock_irqsave(&ch->ch_lock, lock_flags);
811 if ((cause & 0x4) == 0) {
812 if (cause & UART_MCR_RTS)
813 ch->ch_mostat |= UART_MCR_RTS;
814 else
815 ch->ch_mostat &= ~(UART_MCR_RTS);
816 } else {
817 if (cause & UART_MCR_DTR)
818 ch->ch_mostat |= UART_MCR_DTR;
819 else
820 ch->ch_mostat &= ~(UART_MCR_DTR);
821 }
822 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
823 }
824
825 /* Parse any modem signal changes */
826 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
827 "MOD_STAT: sending to parse_modem_sigs\n");
828 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
829 }
830}
831
832static inline void neo_parse_lsr(struct jsm_board *brd, u32 port)
833{
834 struct jsm_channel *ch;
835 int linestatus;
836 unsigned long lock_flags;
837
838 if (!brd)
839 return;
840
841 if (port > brd->maxports)
842 return;
843
844 ch = brd->channels[port];
845 if (!ch)
846 return;
847
848 linestatus = readb(&ch->ch_neo_uart->lsr);
849
850 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
851 "%s:%d port: %d linestatus: %x\n", __FILE__, __LINE__, port, linestatus);
852
853 ch->ch_cached_lsr |= linestatus;
854
855 if (ch->ch_cached_lsr & UART_LSR_DR) {
856 /* Read data from uart -> queue */
857 neo_copy_data_from_uart_to_queue(ch);
858 spin_lock_irqsave(&ch->ch_lock, lock_flags);
859 jsm_check_queue_flow_control(ch);
860 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
861 }
862
863 /*
864 * This is a special flag. It indicates that at least 1
865 * RX error (parity, framing, or break) has happened.
866 * Mark this in our struct, which will tell me that I have
867 *to do the special RX+LSR read for this FIFO load.
868 */
869 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
870 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
871 "%s:%d Port: %d Got an RX error, need to parse LSR\n",
872 __FILE__, __LINE__, port);
873
874 /*
875 * The next 3 tests should *NOT* happen, as the above test
876 * should encapsulate all 3... At least, thats what Exar says.
877 */
878
879 if (linestatus & UART_LSR_PE) {
880 ch->ch_err_parity++;
881 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
882 "%s:%d Port: %d. PAR ERR!\n", __FILE__, __LINE__, port);
883 }
884
885 if (linestatus & UART_LSR_FE) {
886 ch->ch_err_frame++;
887 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
888 "%s:%d Port: %d. FRM ERR!\n", __FILE__, __LINE__, port);
889 }
890
891 if (linestatus & UART_LSR_BI) {
892 ch->ch_err_break++;
893 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
894 "%s:%d Port: %d. BRK INTR!\n", __FILE__, __LINE__, port);
895 }
896
897 if (linestatus & UART_LSR_OE) {
898 /*
899 * Rx Oruns. Exar says that an orun will NOT corrupt
900 * the FIFO. It will just replace the holding register
901 * with this new data byte. So basically just ignore this.
902 * Probably we should eventually have an orun stat in our driver...
903 */
904 ch->ch_err_overrun++;
905 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
906 "%s:%d Port: %d. Rx Overrun!\n", __FILE__, __LINE__, port);
907 }
908
909 if (linestatus & UART_LSR_THRE) {
910 spin_lock_irqsave(&ch->ch_lock, lock_flags);
911 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
912 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
913
914 /* Transfer data (if any) from Write Queue -> UART. */
915 neo_copy_data_from_queue_to_uart(ch);
916 }
917 else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
918 spin_lock_irqsave(&ch->ch_lock, lock_flags);
919 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
920 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
921
922 /* Transfer data (if any) from Write Queue -> UART. */
923 neo_copy_data_from_queue_to_uart(ch);
924 }
925}
926
927/*
928 * neo_param()
929 * Send any/all changes to the line to the UART.
930 */
931static void neo_param(struct jsm_channel *ch)
932{
933 u8 lcr = 0;
934 u8 uart_lcr, ier;
935 u32 baud;
936 int quot;
937 struct jsm_board *bd;
938
939 bd = ch->ch_bd;
940 if (!bd)
941 return;
942
943 /*
944 * If baud rate is zero, flush queues, and set mval to drop DTR.
945 */
946 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
947 ch->ch_r_head = ch->ch_r_tail = 0;
948 ch->ch_e_head = ch->ch_e_tail = 0;
949 ch->ch_w_head = ch->ch_w_tail = 0;
950
951 neo_flush_uart_write(ch);
952 neo_flush_uart_read(ch);
953
954 ch->ch_flags |= (CH_BAUD0);
955 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
956 neo_assert_modem_signals(ch);
957 return;
958
959 } else {
960 int i;
961 unsigned int cflag;
962 static struct {
963 unsigned int rate;
964 unsigned int cflag;
965 } baud_rates[] = {
966 { 921600, B921600 },
967 { 460800, B460800 },
968 { 230400, B230400 },
969 { 115200, B115200 },
970 { 57600, B57600 },
971 { 38400, B38400 },
972 { 19200, B19200 },
973 { 9600, B9600 },
974 { 4800, B4800 },
975 { 2400, B2400 },
976 { 1200, B1200 },
977 { 600, B600 },
978 { 300, B300 },
979 { 200, B200 },
980 { 150, B150 },
981 { 134, B134 },
982 { 110, B110 },
983 { 75, B75 },
984 { 50, B50 },
985 };
986
987 cflag = C_BAUD(ch->uart_port.state->port.tty);
988 baud = 9600;
989 for (i = 0; i < ARRAY_SIZE(baud_rates); i++) {
990 if (baud_rates[i].cflag == cflag) {
991 baud = baud_rates[i].rate;
992 break;
993 }
994 }
995
996 if (ch->ch_flags & CH_BAUD0)
997 ch->ch_flags &= ~(CH_BAUD0);
998 }
999
1000 if (ch->ch_c_cflag & PARENB)
1001 lcr |= UART_LCR_PARITY;
1002
1003 if (!(ch->ch_c_cflag & PARODD))
1004 lcr |= UART_LCR_EPAR;
1005
1006 /*
1007 * Not all platforms support mark/space parity,
1008 * so this will hide behind an ifdef.
1009 */
1010#ifdef CMSPAR
1011 if (ch->ch_c_cflag & CMSPAR)
1012 lcr |= UART_LCR_SPAR;
1013#endif
1014
1015 if (ch->ch_c_cflag & CSTOPB)
1016 lcr |= UART_LCR_STOP;
1017
1018 switch (ch->ch_c_cflag & CSIZE) {
1019 case CS5:
1020 lcr |= UART_LCR_WLEN5;
1021 break;
1022 case CS6:
1023 lcr |= UART_LCR_WLEN6;
1024 break;
1025 case CS7:
1026 lcr |= UART_LCR_WLEN7;
1027 break;
1028 case CS8:
1029 default:
1030 lcr |= UART_LCR_WLEN8;
1031 break;
1032 }
1033
1034 ier = readb(&ch->ch_neo_uart->ier);
1035 uart_lcr = readb(&ch->ch_neo_uart->lcr);
1036
1037 if (baud == 0)
1038 baud = 9600;
1039
1040 quot = ch->ch_bd->bd_dividend / baud;
1041
1042 if (quot != 0) {
1043 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
1044 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
1045 writeb((quot >> 8), &ch->ch_neo_uart->ier);
1046 writeb(lcr, &ch->ch_neo_uart->lcr);
1047 }
1048
1049 if (uart_lcr != lcr)
1050 writeb(lcr, &ch->ch_neo_uart->lcr);
1051
1052 if (ch->ch_c_cflag & CREAD)
1053 ier |= (UART_IER_RDI | UART_IER_RLSI);
1054
1055 ier |= (UART_IER_THRI | UART_IER_MSI);
1056
1057 writeb(ier, &ch->ch_neo_uart->ier);
1058
1059 /* Set new start/stop chars */
1060 neo_set_new_start_stop_chars(ch);
1061
1062 if (ch->ch_c_cflag & CRTSCTS)
1063 neo_set_cts_flow_control(ch);
1064 else if (ch->ch_c_iflag & IXON) {
1065 /* If start/stop is set to disable, then we should disable flow control */
1066 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1067 neo_set_no_output_flow_control(ch);
1068 else
1069 neo_set_ixon_flow_control(ch);
1070 }
1071 else
1072 neo_set_no_output_flow_control(ch);
1073
1074 if (ch->ch_c_cflag & CRTSCTS)
1075 neo_set_rts_flow_control(ch);
1076 else if (ch->ch_c_iflag & IXOFF) {
1077 /* If start/stop is set to disable, then we should disable flow control */
1078 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1079 neo_set_no_input_flow_control(ch);
1080 else
1081 neo_set_ixoff_flow_control(ch);
1082 }
1083 else
1084 neo_set_no_input_flow_control(ch);
1085 /*
1086 * Adjust the RX FIFO Trigger level if baud is less than 9600.
1087 * Not exactly elegant, but this is needed because of the Exar chip's
1088 * delay on firing off the RX FIFO interrupt on slower baud rates.
1089 */
1090 if (baud < 9600) {
1091 writeb(1, &ch->ch_neo_uart->rfifo);
1092 ch->ch_r_tlevel = 1;
1093 }
1094
1095 neo_assert_modem_signals(ch);
1096
1097 /* Get current status of the modem signals now */
1098 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
1099 return;
1100}
1101
1102/*
1103 * jsm_neo_intr()
1104 *
1105 * Neo specific interrupt handler.
1106 */
1107static irqreturn_t neo_intr(int irq, void *voidbrd)
1108{
1109 struct jsm_board *brd = voidbrd;
1110 struct jsm_channel *ch;
1111 int port = 0;
1112 int type = 0;
1113 int current_port;
1114 u32 tmp;
1115 u32 uart_poll;
1116 unsigned long lock_flags;
1117 unsigned long lock_flags2;
1118 int outofloop_count = 0;
1119
1120 /* Lock out the slow poller from running on this board. */
1121 spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
1122
1123 /*
1124 * Read in "extended" IRQ information from the 32bit Neo register.
1125 * Bits 0-7: What port triggered the interrupt.
1126 * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
1127 */
1128 uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
1129
1130 jsm_printk(INTR, INFO, &brd->pci_dev,
1131 "%s:%d uart_poll: %x\n", __FILE__, __LINE__, uart_poll);
1132
1133 if (!uart_poll) {
1134 jsm_printk(INTR, INFO, &brd->pci_dev,
1135 "Kernel interrupted to me, but no pending interrupts...\n");
1136 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1137 return IRQ_NONE;
1138 }
1139
1140 /* At this point, we have at least SOMETHING to service, dig further... */
1141
1142 current_port = 0;
1143
1144 /* Loop on each port */
1145 while (((uart_poll & 0xff) != 0) && (outofloop_count < 0xff)){
1146
1147 tmp = uart_poll;
1148 outofloop_count++;
1149
1150 /* Check current port to see if it has interrupt pending */
1151 if ((tmp & jsm_offset_table[current_port]) != 0) {
1152 port = current_port;
1153 type = tmp >> (8 + (port * 3));
1154 type &= 0x7;
1155 } else {
1156 current_port++;
1157 continue;
1158 }
1159
1160 jsm_printk(INTR, INFO, &brd->pci_dev,
1161 "%s:%d port: %x type: %x\n", __FILE__, __LINE__, port, type);
1162
1163 /* Remove this port + type from uart_poll */
1164 uart_poll &= ~(jsm_offset_table[port]);
1165
1166 if (!type) {
1167 /* If no type, just ignore it, and move onto next port */
1168 jsm_printk(INTR, ERR, &brd->pci_dev,
1169 "Interrupt with no type! port: %d\n", port);
1170 continue;
1171 }
1172
1173 /* Switch on type of interrupt we have */
1174 switch (type) {
1175
1176 case UART_17158_RXRDY_TIMEOUT:
1177 /*
1178 * RXRDY Time-out is cleared by reading data in the
1179 * RX FIFO until it falls below the trigger level.
1180 */
1181
1182 /* Verify the port is in range. */
1183 if (port > brd->nasync)
1184 continue;
1185
1186 ch = brd->channels[port];
1187 neo_copy_data_from_uart_to_queue(ch);
1188
1189 /* Call our tty layer to enforce queue flow control if needed. */
1190 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1191 jsm_check_queue_flow_control(ch);
1192 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1193
1194 continue;
1195
1196 case UART_17158_RX_LINE_STATUS:
1197 /*
1198 * RXRDY and RX LINE Status (logic OR of LSR[4:1])
1199 */
1200 neo_parse_lsr(brd, port);
1201 continue;
1202
1203 case UART_17158_TXRDY:
1204 /*
1205 * TXRDY interrupt clears after reading ISR register for the UART channel.
1206 */
1207
1208 /*
1209 * Yes, this is odd...
1210 * Why would I check EVERY possibility of type of
1211 * interrupt, when we know its TXRDY???
1212 * Becuz for some reason, even tho we got triggered for TXRDY,
1213 * it seems to be occasionally wrong. Instead of TX, which
1214 * it should be, I was getting things like RXDY too. Weird.
1215 */
1216 neo_parse_isr(brd, port);
1217 continue;
1218
1219 case UART_17158_MSR:
1220 /*
1221 * MSR or flow control was seen.
1222 */
1223 neo_parse_isr(brd, port);
1224 continue;
1225
1226 default:
1227 /*
1228 * The UART triggered us with a bogus interrupt type.
1229 * It appears the Exar chip, when REALLY bogged down, will throw
1230 * these once and awhile.
1231 * Its harmless, just ignore it and move on.
1232 */
1233 jsm_printk(INTR, ERR, &brd->pci_dev,
1234 "%s:%d Unknown Interrupt type: %x\n", __FILE__, __LINE__, type);
1235 continue;
1236 }
1237 }
1238
1239 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1240
1241 jsm_printk(INTR, INFO, &brd->pci_dev, "finish.\n");
1242 return IRQ_HANDLED;
1243}
1244
1245/*
1246 * Neo specific way of turning off the receiver.
1247 * Used as a way to enforce queue flow control when in
1248 * hardware flow control mode.
1249 */
1250static void neo_disable_receiver(struct jsm_channel *ch)
1251{
1252 u8 tmp = readb(&ch->ch_neo_uart->ier);
1253 tmp &= ~(UART_IER_RDI);
1254 writeb(tmp, &ch->ch_neo_uart->ier);
1255
1256 /* flush write operation */
1257 neo_pci_posting_flush(ch->ch_bd);
1258}
1259
1260
1261/*
1262 * Neo specific way of turning on the receiver.
1263 * Used as a way to un-enforce queue flow control when in
1264 * hardware flow control mode.
1265 */
1266static void neo_enable_receiver(struct jsm_channel *ch)
1267{
1268 u8 tmp = readb(&ch->ch_neo_uart->ier);
1269 tmp |= (UART_IER_RDI);
1270 writeb(tmp, &ch->ch_neo_uart->ier);
1271
1272 /* flush write operation */
1273 neo_pci_posting_flush(ch->ch_bd);
1274}
1275
1276static void neo_send_start_character(struct jsm_channel *ch)
1277{
1278 if (!ch)
1279 return;
1280
1281 if (ch->ch_startc != __DISABLED_CHAR) {
1282 ch->ch_xon_sends++;
1283 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1284
1285 /* flush write operation */
1286 neo_pci_posting_flush(ch->ch_bd);
1287 }
1288}
1289
1290static void neo_send_stop_character(struct jsm_channel *ch)
1291{
1292 if (!ch)
1293 return;
1294
1295 if (ch->ch_stopc != __DISABLED_CHAR) {
1296 ch->ch_xoff_sends++;
1297 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1298
1299 /* flush write operation */
1300 neo_pci_posting_flush(ch->ch_bd);
1301 }
1302}
1303
1304/*
1305 * neo_uart_init
1306 */
1307static void neo_uart_init(struct jsm_channel *ch)
1308{
1309 writeb(0, &ch->ch_neo_uart->ier);
1310 writeb(0, &ch->ch_neo_uart->efr);
1311 writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1312
1313 /* Clear out UART and FIFO */
1314 readb(&ch->ch_neo_uart->txrx);
1315 writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
1316 readb(&ch->ch_neo_uart->lsr);
1317 readb(&ch->ch_neo_uart->msr);
1318
1319 ch->ch_flags |= CH_FIFO_ENABLED;
1320
1321 /* Assert any signals we want up */
1322 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1323}
1324
1325/*
1326 * Make the UART completely turn off.
1327 */
1328static void neo_uart_off(struct jsm_channel *ch)
1329{
1330 /* Turn off UART enhanced bits */
1331 writeb(0, &ch->ch_neo_uart->efr);
1332
1333 /* Stop all interrupts from occurring. */
1334 writeb(0, &ch->ch_neo_uart->ier);
1335}
1336
1337static u32 neo_get_uart_bytes_left(struct jsm_channel *ch)
1338{
1339 u8 left = 0;
1340 u8 lsr = readb(&ch->ch_neo_uart->lsr);
1341
1342 /* We must cache the LSR as some of the bits get reset once read... */
1343 ch->ch_cached_lsr |= lsr;
1344
1345 /* Determine whether the Transmitter is empty or not */
1346 if (!(lsr & UART_LSR_TEMT))
1347 left = 1;
1348 else {
1349 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1350 left = 0;
1351 }
1352
1353 return left;
1354}
1355
1356/* Channel lock MUST be held by the calling function! */
1357static void neo_send_break(struct jsm_channel *ch)
1358{
1359 /*
1360 * Set the time we should stop sending the break.
1361 * If we are already sending a break, toss away the existing
1362 * time to stop, and use this new value instead.
1363 */
1364
1365 /* Tell the UART to start sending the break */
1366 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1367 u8 temp = readb(&ch->ch_neo_uart->lcr);
1368 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1369 ch->ch_flags |= (CH_BREAK_SENDING);
1370
1371 /* flush write operation */
1372 neo_pci_posting_flush(ch->ch_bd);
1373 }
1374}
1375
1376/*
1377 * neo_send_immediate_char.
1378 *
1379 * Sends a specific character as soon as possible to the UART,
1380 * jumping over any bytes that might be in the write queue.
1381 *
1382 * The channel lock MUST be held by the calling function.
1383 */
1384static void neo_send_immediate_char(struct jsm_channel *ch, unsigned char c)
1385{
1386 if (!ch)
1387 return;
1388
1389 writeb(c, &ch->ch_neo_uart->txrx);
1390
1391 /* flush write operation */
1392 neo_pci_posting_flush(ch->ch_bd);
1393}
1394
1395struct board_ops jsm_neo_ops = {
1396 .intr = neo_intr,
1397 .uart_init = neo_uart_init,
1398 .uart_off = neo_uart_off,
1399 .param = neo_param,
1400 .assert_modem_signals = neo_assert_modem_signals,
1401 .flush_uart_write = neo_flush_uart_write,
1402 .flush_uart_read = neo_flush_uart_read,
1403 .disable_receiver = neo_disable_receiver,
1404 .enable_receiver = neo_enable_receiver,
1405 .send_break = neo_send_break,
1406 .clear_break = neo_clear_break,
1407 .send_start_character = neo_send_start_character,
1408 .send_stop_character = neo_send_stop_character,
1409 .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
1410 .get_uart_bytes_left = neo_get_uart_bytes_left,
1411 .send_immediate_char = neo_send_immediate_char
1412};
diff --git a/drivers/tty/serial/jsm/jsm_tty.c b/drivers/tty/serial/jsm/jsm_tty.c
new file mode 100644
index 000000000000..7a4a914ecff0
--- /dev/null
+++ b/drivers/tty/serial/jsm/jsm_tty.c
@@ -0,0 +1,910 @@
1/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
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, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the 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., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Ananda Venkatarman <mansarov@us.ibm.com>
24 * Modifications:
25 * 01/19/06: changed jsm_input routine to use the dynamically allocated
26 * tty_buffer changes. Contributors: Scott Kilau and Ananda V.
27 ***********************************************************************/
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial_reg.h>
31#include <linux/delay.h> /* For udelay */
32#include <linux/pci.h>
33#include <linux/slab.h>
34
35#include "jsm.h"
36
37static DECLARE_BITMAP(linemap, MAXLINES);
38
39static void jsm_carrier(struct jsm_channel *ch);
40
41static inline int jsm_get_mstat(struct jsm_channel *ch)
42{
43 unsigned char mstat;
44 unsigned result;
45
46 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
47
48 mstat = (ch->ch_mostat | ch->ch_mistat);
49
50 result = 0;
51
52 if (mstat & UART_MCR_DTR)
53 result |= TIOCM_DTR;
54 if (mstat & UART_MCR_RTS)
55 result |= TIOCM_RTS;
56 if (mstat & UART_MSR_CTS)
57 result |= TIOCM_CTS;
58 if (mstat & UART_MSR_DSR)
59 result |= TIOCM_DSR;
60 if (mstat & UART_MSR_RI)
61 result |= TIOCM_RI;
62 if (mstat & UART_MSR_DCD)
63 result |= TIOCM_CD;
64
65 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
66 return result;
67}
68
69static unsigned int jsm_tty_tx_empty(struct uart_port *port)
70{
71 return TIOCSER_TEMT;
72}
73
74/*
75 * Return modem signals to ld.
76 */
77static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
78{
79 int result;
80 struct jsm_channel *channel = (struct jsm_channel *)port;
81
82 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
83
84 result = jsm_get_mstat(channel);
85
86 if (result < 0)
87 return -ENXIO;
88
89 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
90
91 return result;
92}
93
94/*
95 * jsm_set_modem_info()
96 *
97 * Set modem signals, called by ld.
98 */
99static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
100{
101 struct jsm_channel *channel = (struct jsm_channel *)port;
102
103 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
104
105 if (mctrl & TIOCM_RTS)
106 channel->ch_mostat |= UART_MCR_RTS;
107 else
108 channel->ch_mostat &= ~UART_MCR_RTS;
109
110 if (mctrl & TIOCM_DTR)
111 channel->ch_mostat |= UART_MCR_DTR;
112 else
113 channel->ch_mostat &= ~UART_MCR_DTR;
114
115 channel->ch_bd->bd_ops->assert_modem_signals(channel);
116
117 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
118 udelay(10);
119}
120
121static void jsm_tty_start_tx(struct uart_port *port)
122{
123 struct jsm_channel *channel = (struct jsm_channel *)port;
124
125 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
126
127 channel->ch_flags &= ~(CH_STOP);
128 jsm_tty_write(port);
129
130 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
131}
132
133static void jsm_tty_stop_tx(struct uart_port *port)
134{
135 struct jsm_channel *channel = (struct jsm_channel *)port;
136
137 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
138
139 channel->ch_flags |= (CH_STOP);
140
141 jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
142}
143
144static void jsm_tty_send_xchar(struct uart_port *port, char ch)
145{
146 unsigned long lock_flags;
147 struct jsm_channel *channel = (struct jsm_channel *)port;
148 struct ktermios *termios;
149
150 spin_lock_irqsave(&port->lock, lock_flags);
151 termios = port->state->port.tty->termios;
152 if (ch == termios->c_cc[VSTART])
153 channel->ch_bd->bd_ops->send_start_character(channel);
154
155 if (ch == termios->c_cc[VSTOP])
156 channel->ch_bd->bd_ops->send_stop_character(channel);
157 spin_unlock_irqrestore(&port->lock, lock_flags);
158}
159
160static void jsm_tty_stop_rx(struct uart_port *port)
161{
162 struct jsm_channel *channel = (struct jsm_channel *)port;
163
164 channel->ch_bd->bd_ops->disable_receiver(channel);
165}
166
167static void jsm_tty_enable_ms(struct uart_port *port)
168{
169 /* Nothing needed */
170}
171
172static void jsm_tty_break(struct uart_port *port, int break_state)
173{
174 unsigned long lock_flags;
175 struct jsm_channel *channel = (struct jsm_channel *)port;
176
177 spin_lock_irqsave(&port->lock, lock_flags);
178 if (break_state == -1)
179 channel->ch_bd->bd_ops->send_break(channel);
180 else
181 channel->ch_bd->bd_ops->clear_break(channel, 0);
182
183 spin_unlock_irqrestore(&port->lock, lock_flags);
184}
185
186static int jsm_tty_open(struct uart_port *port)
187{
188 struct jsm_board *brd;
189 struct jsm_channel *channel = (struct jsm_channel *)port;
190 struct ktermios *termios;
191
192 /* Get board pointer from our array of majors we have allocated */
193 brd = channel->ch_bd;
194
195 /*
196 * Allocate channel buffers for read/write/error.
197 * Set flag, so we don't get trounced on.
198 */
199 channel->ch_flags |= (CH_OPENING);
200
201 /* Drop locks, as malloc with GFP_KERNEL can sleep */
202
203 if (!channel->ch_rqueue) {
204 channel->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
205 if (!channel->ch_rqueue) {
206 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
207 "unable to allocate read queue buf");
208 return -ENOMEM;
209 }
210 }
211 if (!channel->ch_equeue) {
212 channel->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
213 if (!channel->ch_equeue) {
214 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
215 "unable to allocate error queue buf");
216 return -ENOMEM;
217 }
218 }
219 if (!channel->ch_wqueue) {
220 channel->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
221 if (!channel->ch_wqueue) {
222 jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
223 "unable to allocate write queue buf");
224 return -ENOMEM;
225 }
226 }
227
228 channel->ch_flags &= ~(CH_OPENING);
229 /*
230 * Initialize if neither terminal is open.
231 */
232 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
233 "jsm_open: initializing channel in open...\n");
234
235 /*
236 * Flush input queues.
237 */
238 channel->ch_r_head = channel->ch_r_tail = 0;
239 channel->ch_e_head = channel->ch_e_tail = 0;
240 channel->ch_w_head = channel->ch_w_tail = 0;
241
242 brd->bd_ops->flush_uart_write(channel);
243 brd->bd_ops->flush_uart_read(channel);
244
245 channel->ch_flags = 0;
246 channel->ch_cached_lsr = 0;
247 channel->ch_stops_sent = 0;
248
249 termios = port->state->port.tty->termios;
250 channel->ch_c_cflag = termios->c_cflag;
251 channel->ch_c_iflag = termios->c_iflag;
252 channel->ch_c_oflag = termios->c_oflag;
253 channel->ch_c_lflag = termios->c_lflag;
254 channel->ch_startc = termios->c_cc[VSTART];
255 channel->ch_stopc = termios->c_cc[VSTOP];
256
257 /* Tell UART to init itself */
258 brd->bd_ops->uart_init(channel);
259
260 /*
261 * Run param in case we changed anything
262 */
263 brd->bd_ops->param(channel);
264
265 jsm_carrier(channel);
266
267 channel->ch_open_count++;
268
269 jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
270 return 0;
271}
272
273static void jsm_tty_close(struct uart_port *port)
274{
275 struct jsm_board *bd;
276 struct ktermios *ts;
277 struct jsm_channel *channel = (struct jsm_channel *)port;
278
279 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
280
281 bd = channel->ch_bd;
282 ts = port->state->port.tty->termios;
283
284 channel->ch_flags &= ~(CH_STOPI);
285
286 channel->ch_open_count--;
287
288 /*
289 * If we have HUPCL set, lower DTR and RTS
290 */
291 if (channel->ch_c_cflag & HUPCL) {
292 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
293 "Close. HUPCL set, dropping DTR/RTS\n");
294
295 /* Drop RTS/DTR */
296 channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
297 bd->bd_ops->assert_modem_signals(channel);
298 }
299
300 /* Turn off UART interrupts for this port */
301 channel->ch_bd->bd_ops->uart_off(channel);
302
303 jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
304}
305
306static void jsm_tty_set_termios(struct uart_port *port,
307 struct ktermios *termios,
308 struct ktermios *old_termios)
309{
310 unsigned long lock_flags;
311 struct jsm_channel *channel = (struct jsm_channel *)port;
312
313 spin_lock_irqsave(&port->lock, lock_flags);
314 channel->ch_c_cflag = termios->c_cflag;
315 channel->ch_c_iflag = termios->c_iflag;
316 channel->ch_c_oflag = termios->c_oflag;
317 channel->ch_c_lflag = termios->c_lflag;
318 channel->ch_startc = termios->c_cc[VSTART];
319 channel->ch_stopc = termios->c_cc[VSTOP];
320
321 channel->ch_bd->bd_ops->param(channel);
322 jsm_carrier(channel);
323 spin_unlock_irqrestore(&port->lock, lock_flags);
324}
325
326static const char *jsm_tty_type(struct uart_port *port)
327{
328 return "jsm";
329}
330
331static void jsm_tty_release_port(struct uart_port *port)
332{
333}
334
335static int jsm_tty_request_port(struct uart_port *port)
336{
337 return 0;
338}
339
340static void jsm_config_port(struct uart_port *port, int flags)
341{
342 port->type = PORT_JSM;
343}
344
345static struct uart_ops jsm_ops = {
346 .tx_empty = jsm_tty_tx_empty,
347 .set_mctrl = jsm_tty_set_mctrl,
348 .get_mctrl = jsm_tty_get_mctrl,
349 .stop_tx = jsm_tty_stop_tx,
350 .start_tx = jsm_tty_start_tx,
351 .send_xchar = jsm_tty_send_xchar,
352 .stop_rx = jsm_tty_stop_rx,
353 .enable_ms = jsm_tty_enable_ms,
354 .break_ctl = jsm_tty_break,
355 .startup = jsm_tty_open,
356 .shutdown = jsm_tty_close,
357 .set_termios = jsm_tty_set_termios,
358 .type = jsm_tty_type,
359 .release_port = jsm_tty_release_port,
360 .request_port = jsm_tty_request_port,
361 .config_port = jsm_config_port,
362};
363
364/*
365 * jsm_tty_init()
366 *
367 * Init the tty subsystem. Called once per board after board has been
368 * downloaded and init'ed.
369 */
370int __devinit jsm_tty_init(struct jsm_board *brd)
371{
372 int i;
373 void __iomem *vaddr;
374 struct jsm_channel *ch;
375
376 if (!brd)
377 return -ENXIO;
378
379 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
380
381 /*
382 * Initialize board structure elements.
383 */
384
385 brd->nasync = brd->maxports;
386
387 /*
388 * Allocate channel memory that might not have been allocated
389 * when the driver was first loaded.
390 */
391 for (i = 0; i < brd->nasync; i++) {
392 if (!brd->channels[i]) {
393
394 /*
395 * Okay to malloc with GFP_KERNEL, we are not at
396 * interrupt context, and there are no locks held.
397 */
398 brd->channels[i] = kzalloc(sizeof(struct jsm_channel), GFP_KERNEL);
399 if (!brd->channels[i]) {
400 jsm_printk(CORE, ERR, &brd->pci_dev,
401 "%s:%d Unable to allocate memory for channel struct\n",
402 __FILE__, __LINE__);
403 }
404 }
405 }
406
407 ch = brd->channels[0];
408 vaddr = brd->re_map_membase;
409
410 /* Set up channel variables */
411 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
412
413 if (!brd->channels[i])
414 continue;
415
416 spin_lock_init(&ch->ch_lock);
417
418 if (brd->bd_uart_offset == 0x200)
419 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
420
421 ch->ch_bd = brd;
422 ch->ch_portnum = i;
423
424 /* .25 second delay */
425 ch->ch_close_delay = 250;
426
427 init_waitqueue_head(&ch->ch_flags_wait);
428 }
429
430 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
431 return 0;
432}
433
434int jsm_uart_port_init(struct jsm_board *brd)
435{
436 int i, rc;
437 unsigned int line;
438 struct jsm_channel *ch;
439
440 if (!brd)
441 return -ENXIO;
442
443 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
444
445 /*
446 * Initialize board structure elements.
447 */
448
449 brd->nasync = brd->maxports;
450
451 /* Set up channel variables */
452 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
453
454 if (!brd->channels[i])
455 continue;
456
457 brd->channels[i]->uart_port.irq = brd->irq;
458 brd->channels[i]->uart_port.uartclk = 14745600;
459 brd->channels[i]->uart_port.type = PORT_JSM;
460 brd->channels[i]->uart_port.iotype = UPIO_MEM;
461 brd->channels[i]->uart_port.membase = brd->re_map_membase;
462 brd->channels[i]->uart_port.fifosize = 16;
463 brd->channels[i]->uart_port.ops = &jsm_ops;
464 line = find_first_zero_bit(linemap, MAXLINES);
465 if (line >= MAXLINES) {
466 printk(KERN_INFO "jsm: linemap is full, added device failed\n");
467 continue;
468 } else
469 set_bit(line, linemap);
470 brd->channels[i]->uart_port.line = line;
471 rc = uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port);
472 if (rc){
473 printk(KERN_INFO "jsm: Port %d failed. Aborting...\n", i);
474 return rc;
475 }
476 else
477 printk(KERN_INFO "jsm: Port %d added\n", i);
478 }
479
480 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
481 return 0;
482}
483
484int jsm_remove_uart_port(struct jsm_board *brd)
485{
486 int i;
487 struct jsm_channel *ch;
488
489 if (!brd)
490 return -ENXIO;
491
492 jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
493
494 /*
495 * Initialize board structure elements.
496 */
497
498 brd->nasync = brd->maxports;
499
500 /* Set up channel variables */
501 for (i = 0; i < brd->nasync; i++) {
502
503 if (!brd->channels[i])
504 continue;
505
506 ch = brd->channels[i];
507
508 clear_bit(ch->uart_port.line, linemap);
509 uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
510 }
511
512 jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
513 return 0;
514}
515
516void jsm_input(struct jsm_channel *ch)
517{
518 struct jsm_board *bd;
519 struct tty_struct *tp;
520 u32 rmask;
521 u16 head;
522 u16 tail;
523 int data_len;
524 unsigned long lock_flags;
525 int len = 0;
526 int n = 0;
527 int s = 0;
528 int i = 0;
529
530 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
531
532 if (!ch)
533 return;
534
535 tp = ch->uart_port.state->port.tty;
536
537 bd = ch->ch_bd;
538 if(!bd)
539 return;
540
541 spin_lock_irqsave(&ch->ch_lock, lock_flags);
542
543 /*
544 *Figure the number of characters in the buffer.
545 *Exit immediately if none.
546 */
547
548 rmask = RQUEUEMASK;
549
550 head = ch->ch_r_head & rmask;
551 tail = ch->ch_r_tail & rmask;
552
553 data_len = (head - tail) & rmask;
554 if (data_len == 0) {
555 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
556 return;
557 }
558
559 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
560
561 /*
562 *If the device is not open, or CREAD is off, flush
563 *input data and return immediately.
564 */
565 if (!tp ||
566 !(tp->termios->c_cflag & CREAD) ) {
567
568 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
569 "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
570 ch->ch_r_head = tail;
571
572 /* Force queue flow control to be released, if needed */
573 jsm_check_queue_flow_control(ch);
574
575 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
576 return;
577 }
578
579 /*
580 * If we are throttled, simply don't read any data.
581 */
582 if (ch->ch_flags & CH_STOPI) {
583 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
584 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
585 "Port %d throttled, not reading any data. head: %x tail: %x\n",
586 ch->ch_portnum, head, tail);
587 return;
588 }
589
590 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
591
592 if (data_len <= 0) {
593 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
594 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
595 return;
596 }
597
598 len = tty_buffer_request_room(tp, data_len);
599 n = len;
600
601 /*
602 * n now contains the most amount of data we can copy,
603 * bounded either by the flip buffer size or the amount
604 * of data the card actually has pending...
605 */
606 while (n) {
607 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
608 s = min(s, n);
609
610 if (s <= 0)
611 break;
612
613 /*
614 * If conditions are such that ld needs to see all
615 * UART errors, we will have to walk each character
616 * and error byte and send them to the buffer one at
617 * a time.
618 */
619
620 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
621 for (i = 0; i < s; i++) {
622 /*
623 * Give the Linux ld the flags in the
624 * format it likes.
625 */
626 if (*(ch->ch_equeue +tail +i) & UART_LSR_BI)
627 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_BREAK);
628 else if (*(ch->ch_equeue +tail +i) & UART_LSR_PE)
629 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_PARITY);
630 else if (*(ch->ch_equeue +tail +i) & UART_LSR_FE)
631 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_FRAME);
632 else
633 tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_NORMAL);
634 }
635 } else {
636 tty_insert_flip_string(tp, ch->ch_rqueue + tail, s) ;
637 }
638 tail += s;
639 n -= s;
640 /* Flip queue if needed */
641 tail &= rmask;
642 }
643
644 ch->ch_r_tail = tail & rmask;
645 ch->ch_e_tail = tail & rmask;
646 jsm_check_queue_flow_control(ch);
647 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
648
649 /* Tell the tty layer its okay to "eat" the data now */
650 tty_flip_buffer_push(tp);
651
652 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
653}
654
655static void jsm_carrier(struct jsm_channel *ch)
656{
657 struct jsm_board *bd;
658
659 int virt_carrier = 0;
660 int phys_carrier = 0;
661
662 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
663 if (!ch)
664 return;
665
666 bd = ch->ch_bd;
667
668 if (!bd)
669 return;
670
671 if (ch->ch_mistat & UART_MSR_DCD) {
672 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
673 "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
674 phys_carrier = 1;
675 }
676
677 if (ch->ch_c_cflag & CLOCAL)
678 virt_carrier = 1;
679
680 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
681 "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
682
683 /*
684 * Test for a VIRTUAL carrier transition to HIGH.
685 */
686 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
687
688 /*
689 * When carrier rises, wake any threads waiting
690 * for carrier in the open routine.
691 */
692
693 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
694 "carrier: virt DCD rose\n");
695
696 if (waitqueue_active(&(ch->ch_flags_wait)))
697 wake_up_interruptible(&ch->ch_flags_wait);
698 }
699
700 /*
701 * Test for a PHYSICAL carrier transition to HIGH.
702 */
703 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
704
705 /*
706 * When carrier rises, wake any threads waiting
707 * for carrier in the open routine.
708 */
709
710 jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
711 "carrier: physical DCD rose\n");
712
713 if (waitqueue_active(&(ch->ch_flags_wait)))
714 wake_up_interruptible(&ch->ch_flags_wait);
715 }
716
717 /*
718 * Test for a PHYSICAL transition to low, so long as we aren't
719 * currently ignoring physical transitions (which is what "virtual
720 * carrier" indicates).
721 *
722 * The transition of the virtual carrier to low really doesn't
723 * matter... it really only means "ignore carrier state", not
724 * "make pretend that carrier is there".
725 */
726 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
727 && (phys_carrier == 0)) {
728 /*
729 * When carrier drops:
730 *
731 * Drop carrier on all open units.
732 *
733 * Flush queues, waking up any task waiting in the
734 * line discipline.
735 *
736 * Send a hangup to the control terminal.
737 *
738 * Enable all select calls.
739 */
740 if (waitqueue_active(&(ch->ch_flags_wait)))
741 wake_up_interruptible(&ch->ch_flags_wait);
742 }
743
744 /*
745 * Make sure that our cached values reflect the current reality.
746 */
747 if (virt_carrier == 1)
748 ch->ch_flags |= CH_FCAR;
749 else
750 ch->ch_flags &= ~CH_FCAR;
751
752 if (phys_carrier == 1)
753 ch->ch_flags |= CH_CD;
754 else
755 ch->ch_flags &= ~CH_CD;
756}
757
758
759void jsm_check_queue_flow_control(struct jsm_channel *ch)
760{
761 struct board_ops *bd_ops = ch->ch_bd->bd_ops;
762 int qleft;
763
764 /* Store how much space we have left in the queue */
765 if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
766 qleft += RQUEUEMASK + 1;
767
768 /*
769 * Check to see if we should enforce flow control on our queue because
770 * the ld (or user) isn't reading data out of our queue fast enuf.
771 *
772 * NOTE: This is done based on what the current flow control of the
773 * port is set for.
774 *
775 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
776 * This will cause the UART's FIFO to back up, and force
777 * the RTS signal to be dropped.
778 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
779 * the other side, in hopes it will stop sending data to us.
780 * 3) NONE - Nothing we can do. We will simply drop any extra data
781 * that gets sent into us when the queue fills up.
782 */
783 if (qleft < 256) {
784 /* HWFLOW */
785 if (ch->ch_c_cflag & CRTSCTS) {
786 if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
787 bd_ops->disable_receiver(ch);
788 ch->ch_flags |= (CH_RECEIVER_OFF);
789 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
790 "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
791 qleft);
792 }
793 }
794 /* SWFLOW */
795 else if (ch->ch_c_iflag & IXOFF) {
796 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
797 bd_ops->send_stop_character(ch);
798 ch->ch_stops_sent++;
799 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
800 "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
801 }
802 }
803 }
804
805 /*
806 * Check to see if we should unenforce flow control because
807 * ld (or user) finally read enuf data out of our queue.
808 *
809 * NOTE: This is done based on what the current flow control of the
810 * port is set for.
811 *
812 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
813 * This will cause the UART's FIFO to raise RTS back up,
814 * which will allow the other side to start sending data again.
815 * 2) SWFLOW (IXOFF) - Send a start character to
816 * the other side, so it will start sending data to us again.
817 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
818 * other side, we don't need to do anything now.
819 */
820 if (qleft > (RQUEUESIZE / 2)) {
821 /* HWFLOW */
822 if (ch->ch_c_cflag & CRTSCTS) {
823 if (ch->ch_flags & CH_RECEIVER_OFF) {
824 bd_ops->enable_receiver(ch);
825 ch->ch_flags &= ~(CH_RECEIVER_OFF);
826 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
827 "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
828 qleft);
829 }
830 }
831 /* SWFLOW */
832 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
833 ch->ch_stops_sent = 0;
834 bd_ops->send_start_character(ch);
835 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
836 }
837 }
838}
839
840/*
841 * jsm_tty_write()
842 *
843 * Take data from the user or kernel and send it out to the FEP.
844 * In here exists all the Transparent Print magic as well.
845 */
846int jsm_tty_write(struct uart_port *port)
847{
848 int bufcount;
849 int data_count = 0,data_count1 =0;
850 u16 head;
851 u16 tail;
852 u16 tmask;
853 u32 remain;
854 int temp_tail = port->state->xmit.tail;
855 struct jsm_channel *channel = (struct jsm_channel *)port;
856
857 tmask = WQUEUEMASK;
858 head = (channel->ch_w_head) & tmask;
859 tail = (channel->ch_w_tail) & tmask;
860
861 if ((bufcount = tail - head - 1) < 0)
862 bufcount += WQUEUESIZE;
863
864 bufcount = min(bufcount, 56);
865 remain = WQUEUESIZE - head;
866
867 data_count = 0;
868 if (bufcount >= remain) {
869 bufcount -= remain;
870 while ((port->state->xmit.head != temp_tail) &&
871 (data_count < remain)) {
872 channel->ch_wqueue[head++] =
873 port->state->xmit.buf[temp_tail];
874
875 temp_tail++;
876 temp_tail &= (UART_XMIT_SIZE - 1);
877 data_count++;
878 }
879 if (data_count == remain) head = 0;
880 }
881
882 data_count1 = 0;
883 if (bufcount > 0) {
884 remain = bufcount;
885 while ((port->state->xmit.head != temp_tail) &&
886 (data_count1 < remain)) {
887 channel->ch_wqueue[head++] =
888 port->state->xmit.buf[temp_tail];
889
890 temp_tail++;
891 temp_tail &= (UART_XMIT_SIZE - 1);
892 data_count1++;
893
894 }
895 }
896
897 port->state->xmit.tail = temp_tail;
898
899 data_count += data_count1;
900 if (data_count) {
901 head &= tmask;
902 channel->ch_w_head = head;
903 }
904
905 if (data_count) {
906 channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
907 }
908
909 return data_count;
910}
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
new file mode 100644
index 000000000000..87e7e6c876d4
--- /dev/null
+++ b/drivers/tty/serial/kgdboc.c
@@ -0,0 +1,328 @@
1/*
2 * Based on the same principle as kgdboe using the NETPOLL api, this
3 * driver uses a console polling api to implement a gdb serial inteface
4 * which is multiplexed on a console port.
5 *
6 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7 *
8 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
14#include <linux/kernel.h>
15#include <linux/ctype.h>
16#include <linux/kgdb.h>
17#include <linux/kdb.h>
18#include <linux/tty.h>
19#include <linux/console.h>
20#include <linux/vt_kern.h>
21#include <linux/input.h>
22
23#define MAX_CONFIG_LEN 40
24
25static struct kgdb_io kgdboc_io_ops;
26
27/* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
28static int configured = -1;
29
30static char config[MAX_CONFIG_LEN];
31static struct kparam_string kps = {
32 .string = config,
33 .maxlen = MAX_CONFIG_LEN,
34};
35
36static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
37static struct tty_driver *kgdb_tty_driver;
38static int kgdb_tty_line;
39
40#ifdef CONFIG_KDB_KEYBOARD
41static int kgdboc_reset_connect(struct input_handler *handler,
42 struct input_dev *dev,
43 const struct input_device_id *id)
44{
45 input_reset_device(dev);
46
47 /* Retrun an error - we do not want to bind, just to reset */
48 return -ENODEV;
49}
50
51static void kgdboc_reset_disconnect(struct input_handle *handle)
52{
53 /* We do not expect anyone to actually bind to us */
54 BUG();
55}
56
57static const struct input_device_id kgdboc_reset_ids[] = {
58 {
59 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
60 .evbit = { BIT_MASK(EV_KEY) },
61 },
62 { }
63};
64
65static struct input_handler kgdboc_reset_handler = {
66 .connect = kgdboc_reset_connect,
67 .disconnect = kgdboc_reset_disconnect,
68 .name = "kgdboc_reset",
69 .id_table = kgdboc_reset_ids,
70};
71
72static DEFINE_MUTEX(kgdboc_reset_mutex);
73
74static void kgdboc_restore_input_helper(struct work_struct *dummy)
75{
76 /*
77 * We need to take a mutex to prevent several instances of
78 * this work running on different CPUs so they don't try
79 * to register again already registered handler.
80 */
81 mutex_lock(&kgdboc_reset_mutex);
82
83 if (input_register_handler(&kgdboc_reset_handler) == 0)
84 input_unregister_handler(&kgdboc_reset_handler);
85
86 mutex_unlock(&kgdboc_reset_mutex);
87}
88
89static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
90
91static void kgdboc_restore_input(void)
92{
93 if (likely(system_state == SYSTEM_RUNNING))
94 schedule_work(&kgdboc_restore_input_work);
95}
96
97static int kgdboc_register_kbd(char **cptr)
98{
99 if (strncmp(*cptr, "kbd", 3) == 0) {
100 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
101 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
102 kdb_poll_idx++;
103 if (cptr[0][3] == ',')
104 *cptr += 4;
105 else
106 return 1;
107 }
108 }
109 return 0;
110}
111
112static void kgdboc_unregister_kbd(void)
113{
114 int i;
115
116 for (i = 0; i < kdb_poll_idx; i++) {
117 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
118 kdb_poll_idx--;
119 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
120 kdb_poll_funcs[kdb_poll_idx] = NULL;
121 i--;
122 }
123 }
124 flush_work_sync(&kgdboc_restore_input_work);
125}
126#else /* ! CONFIG_KDB_KEYBOARD */
127#define kgdboc_register_kbd(x) 0
128#define kgdboc_unregister_kbd()
129#define kgdboc_restore_input()
130#endif /* ! CONFIG_KDB_KEYBOARD */
131
132static int kgdboc_option_setup(char *opt)
133{
134 if (strlen(opt) >= MAX_CONFIG_LEN) {
135 printk(KERN_ERR "kgdboc: config string too long\n");
136 return -ENOSPC;
137 }
138 strcpy(config, opt);
139
140 return 0;
141}
142
143__setup("kgdboc=", kgdboc_option_setup);
144
145static void cleanup_kgdboc(void)
146{
147 kgdboc_unregister_kbd();
148 if (configured == 1)
149 kgdb_unregister_io_module(&kgdboc_io_ops);
150}
151
152static int configure_kgdboc(void)
153{
154 struct tty_driver *p;
155 int tty_line = 0;
156 int err;
157 char *cptr = config;
158 struct console *cons;
159
160 err = kgdboc_option_setup(config);
161 if (err || !strlen(config) || isspace(config[0]))
162 goto noconfig;
163
164 err = -ENODEV;
165 kgdboc_io_ops.is_console = 0;
166 kgdb_tty_driver = NULL;
167
168 kgdboc_use_kms = 0;
169 if (strncmp(cptr, "kms,", 4) == 0) {
170 cptr += 4;
171 kgdboc_use_kms = 1;
172 }
173
174 if (kgdboc_register_kbd(&cptr))
175 goto do_register;
176
177 p = tty_find_polling_driver(cptr, &tty_line);
178 if (!p)
179 goto noconfig;
180
181 cons = console_drivers;
182 while (cons) {
183 int idx;
184 if (cons->device && cons->device(cons, &idx) == p &&
185 idx == tty_line) {
186 kgdboc_io_ops.is_console = 1;
187 break;
188 }
189 cons = cons->next;
190 }
191
192 kgdb_tty_driver = p;
193 kgdb_tty_line = tty_line;
194
195do_register:
196 err = kgdb_register_io_module(&kgdboc_io_ops);
197 if (err)
198 goto noconfig;
199
200 configured = 1;
201
202 return 0;
203
204noconfig:
205 config[0] = 0;
206 configured = 0;
207 cleanup_kgdboc();
208
209 return err;
210}
211
212static int __init init_kgdboc(void)
213{
214 /* Already configured? */
215 if (configured == 1)
216 return 0;
217
218 return configure_kgdboc();
219}
220
221static int kgdboc_get_char(void)
222{
223 if (!kgdb_tty_driver)
224 return -1;
225 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
226 kgdb_tty_line);
227}
228
229static void kgdboc_put_char(u8 chr)
230{
231 if (!kgdb_tty_driver)
232 return;
233 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
234 kgdb_tty_line, chr);
235}
236
237static int param_set_kgdboc_var(const char *kmessage, struct kernel_param *kp)
238{
239 int len = strlen(kmessage);
240
241 if (len >= MAX_CONFIG_LEN) {
242 printk(KERN_ERR "kgdboc: config string too long\n");
243 return -ENOSPC;
244 }
245
246 /* Only copy in the string if the init function has not run yet */
247 if (configured < 0) {
248 strcpy(config, kmessage);
249 return 0;
250 }
251
252 if (kgdb_connected) {
253 printk(KERN_ERR
254 "kgdboc: Cannot reconfigure while KGDB is connected.\n");
255
256 return -EBUSY;
257 }
258
259 strcpy(config, kmessage);
260 /* Chop out \n char as a result of echo */
261 if (config[len - 1] == '\n')
262 config[len - 1] = '\0';
263
264 if (configured == 1)
265 cleanup_kgdboc();
266
267 /* Go and configure with the new params. */
268 return configure_kgdboc();
269}
270
271static int dbg_restore_graphics;
272
273static void kgdboc_pre_exp_handler(void)
274{
275 if (!dbg_restore_graphics && kgdboc_use_kms) {
276 dbg_restore_graphics = 1;
277 con_debug_enter(vc_cons[fg_console].d);
278 }
279 /* Increment the module count when the debugger is active */
280 if (!kgdb_connected)
281 try_module_get(THIS_MODULE);
282}
283
284static void kgdboc_post_exp_handler(void)
285{
286 /* decrement the module count when the debugger detaches */
287 if (!kgdb_connected)
288 module_put(THIS_MODULE);
289 if (kgdboc_use_kms && dbg_restore_graphics) {
290 dbg_restore_graphics = 0;
291 con_debug_leave();
292 }
293 kgdboc_restore_input();
294}
295
296static struct kgdb_io kgdboc_io_ops = {
297 .name = "kgdboc",
298 .read_char = kgdboc_get_char,
299 .write_char = kgdboc_put_char,
300 .pre_exception = kgdboc_pre_exp_handler,
301 .post_exception = kgdboc_post_exp_handler,
302};
303
304#ifdef CONFIG_KGDB_SERIAL_CONSOLE
305/* This is only available if kgdboc is a built in for early debugging */
306static int __init kgdboc_early_init(char *opt)
307{
308 /* save the first character of the config string because the
309 * init routine can destroy it.
310 */
311 char save_ch;
312
313 kgdboc_option_setup(opt);
314 save_ch = config[0];
315 init_kgdboc();
316 config[0] = save_ch;
317 return 0;
318}
319
320early_param("ekgdboc", kgdboc_early_init);
321#endif /* CONFIG_KGDB_SERIAL_CONSOLE */
322
323module_init(init_kgdboc);
324module_exit(cleanup_kgdboc);
325module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
326MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
327MODULE_DESCRIPTION("KGDB Console TTY Driver");
328MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/lantiq.c b/drivers/tty/serial/lantiq.c
new file mode 100644
index 000000000000..58cf279ed879
--- /dev/null
+++ b/drivers/tty/serial/lantiq.c
@@ -0,0 +1,756 @@
1/*
2 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
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 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Copyright (C) 2004 Infineon IFAP DC COM CPE
18 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
19 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20 * Copyright (C) 2010 Thomas Langer, <thomas.langer@lantiq.com>
21 */
22
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/device.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
36#include <linux/clk.h>
37
38#include <lantiq_soc.h>
39
40#define PORT_LTQ_ASC 111
41#define MAXPORTS 2
42#define UART_DUMMY_UER_RX 1
43#define DRVNAME "ltq_asc"
44#ifdef __BIG_ENDIAN
45#define LTQ_ASC_TBUF (0x0020 + 3)
46#define LTQ_ASC_RBUF (0x0024 + 3)
47#else
48#define LTQ_ASC_TBUF 0x0020
49#define LTQ_ASC_RBUF 0x0024
50#endif
51#define LTQ_ASC_FSTAT 0x0048
52#define LTQ_ASC_WHBSTATE 0x0018
53#define LTQ_ASC_STATE 0x0014
54#define LTQ_ASC_IRNCR 0x00F8
55#define LTQ_ASC_CLC 0x0000
56#define LTQ_ASC_ID 0x0008
57#define LTQ_ASC_PISEL 0x0004
58#define LTQ_ASC_TXFCON 0x0044
59#define LTQ_ASC_RXFCON 0x0040
60#define LTQ_ASC_CON 0x0010
61#define LTQ_ASC_BG 0x0050
62#define LTQ_ASC_IRNREN 0x00F4
63
64#define ASC_IRNREN_TX 0x1
65#define ASC_IRNREN_RX 0x2
66#define ASC_IRNREN_ERR 0x4
67#define ASC_IRNREN_TX_BUF 0x8
68#define ASC_IRNCR_TIR 0x1
69#define ASC_IRNCR_RIR 0x2
70#define ASC_IRNCR_EIR 0x4
71
72#define ASCOPT_CSIZE 0x3
73#define TXFIFO_FL 1
74#define RXFIFO_FL 1
75#define ASCCLC_DISS 0x2
76#define ASCCLC_RMCMASK 0x0000FF00
77#define ASCCLC_RMCOFFSET 8
78#define ASCCON_M_8ASYNC 0x0
79#define ASCCON_M_7ASYNC 0x2
80#define ASCCON_ODD 0x00000020
81#define ASCCON_STP 0x00000080
82#define ASCCON_BRS 0x00000100
83#define ASCCON_FDE 0x00000200
84#define ASCCON_R 0x00008000
85#define ASCCON_FEN 0x00020000
86#define ASCCON_ROEN 0x00080000
87#define ASCCON_TOEN 0x00100000
88#define ASCSTATE_PE 0x00010000
89#define ASCSTATE_FE 0x00020000
90#define ASCSTATE_ROE 0x00080000
91#define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
92#define ASCWHBSTATE_CLRREN 0x00000001
93#define ASCWHBSTATE_SETREN 0x00000002
94#define ASCWHBSTATE_CLRPE 0x00000004
95#define ASCWHBSTATE_CLRFE 0x00000008
96#define ASCWHBSTATE_CLRROE 0x00000020
97#define ASCTXFCON_TXFEN 0x0001
98#define ASCTXFCON_TXFFLU 0x0002
99#define ASCTXFCON_TXFITLMASK 0x3F00
100#define ASCTXFCON_TXFITLOFF 8
101#define ASCRXFCON_RXFEN 0x0001
102#define ASCRXFCON_RXFFLU 0x0002
103#define ASCRXFCON_RXFITLMASK 0x3F00
104#define ASCRXFCON_RXFITLOFF 8
105#define ASCFSTAT_RXFFLMASK 0x003F
106#define ASCFSTAT_TXFFLMASK 0x3F00
107#define ASCFSTAT_TXFREEMASK 0x3F000000
108#define ASCFSTAT_TXFREEOFF 24
109
110static void lqasc_tx_chars(struct uart_port *port);
111static struct ltq_uart_port *lqasc_port[MAXPORTS];
112static struct uart_driver lqasc_reg;
113static DEFINE_SPINLOCK(ltq_asc_lock);
114
115struct ltq_uart_port {
116 struct uart_port port;
117 struct clk *clk;
118 unsigned int tx_irq;
119 unsigned int rx_irq;
120 unsigned int err_irq;
121};
122
123static inline struct
124ltq_uart_port *to_ltq_uart_port(struct uart_port *port)
125{
126 return container_of(port, struct ltq_uart_port, port);
127}
128
129static void
130lqasc_stop_tx(struct uart_port *port)
131{
132 return;
133}
134
135static void
136lqasc_start_tx(struct uart_port *port)
137{
138 unsigned long flags;
139 spin_lock_irqsave(&ltq_asc_lock, flags);
140 lqasc_tx_chars(port);
141 spin_unlock_irqrestore(&ltq_asc_lock, flags);
142 return;
143}
144
145static void
146lqasc_stop_rx(struct uart_port *port)
147{
148 ltq_w32(ASCWHBSTATE_CLRREN, port->membase + LTQ_ASC_WHBSTATE);
149}
150
151static void
152lqasc_enable_ms(struct uart_port *port)
153{
154}
155
156static int
157lqasc_rx_chars(struct uart_port *port)
158{
159 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
160 unsigned int ch = 0, rsr = 0, fifocnt;
161
162 if (!tty) {
163 dev_dbg(port->dev, "%s:tty is busy now", __func__);
164 return -EBUSY;
165 }
166 fifocnt =
167 ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_RXFFLMASK;
168 while (fifocnt--) {
169 u8 flag = TTY_NORMAL;
170 ch = ltq_r8(port->membase + LTQ_ASC_RBUF);
171 rsr = (ltq_r32(port->membase + LTQ_ASC_STATE)
172 & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
173 tty_flip_buffer_push(tty);
174 port->icount.rx++;
175
176 /*
177 * Note that the error handling code is
178 * out of the main execution path
179 */
180 if (rsr & ASCSTATE_ANY) {
181 if (rsr & ASCSTATE_PE) {
182 port->icount.parity++;
183 ltq_w32_mask(0, ASCWHBSTATE_CLRPE,
184 port->membase + LTQ_ASC_WHBSTATE);
185 } else if (rsr & ASCSTATE_FE) {
186 port->icount.frame++;
187 ltq_w32_mask(0, ASCWHBSTATE_CLRFE,
188 port->membase + LTQ_ASC_WHBSTATE);
189 }
190 if (rsr & ASCSTATE_ROE) {
191 port->icount.overrun++;
192 ltq_w32_mask(0, ASCWHBSTATE_CLRROE,
193 port->membase + LTQ_ASC_WHBSTATE);
194 }
195
196 rsr &= port->read_status_mask;
197
198 if (rsr & ASCSTATE_PE)
199 flag = TTY_PARITY;
200 else if (rsr & ASCSTATE_FE)
201 flag = TTY_FRAME;
202 }
203
204 if ((rsr & port->ignore_status_mask) == 0)
205 tty_insert_flip_char(tty, ch, flag);
206
207 if (rsr & ASCSTATE_ROE)
208 /*
209 * Overrun is special, since it's reported
210 * immediately, and doesn't affect the current
211 * character
212 */
213 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
214 }
215 if (ch != 0)
216 tty_flip_buffer_push(tty);
217 tty_kref_put(tty);
218 return 0;
219}
220
221static void
222lqasc_tx_chars(struct uart_port *port)
223{
224 struct circ_buf *xmit = &port->state->xmit;
225 if (uart_tx_stopped(port)) {
226 lqasc_stop_tx(port);
227 return;
228 }
229
230 while (((ltq_r32(port->membase + LTQ_ASC_FSTAT) &
231 ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF) != 0) {
232 if (port->x_char) {
233 ltq_w8(port->x_char, port->membase + LTQ_ASC_TBUF);
234 port->icount.tx++;
235 port->x_char = 0;
236 continue;
237 }
238
239 if (uart_circ_empty(xmit))
240 break;
241
242 ltq_w8(port->state->xmit.buf[port->state->xmit.tail],
243 port->membase + LTQ_ASC_TBUF);
244 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
245 port->icount.tx++;
246 }
247
248 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
249 uart_write_wakeup(port);
250}
251
252static irqreturn_t
253lqasc_tx_int(int irq, void *_port)
254{
255 unsigned long flags;
256 struct uart_port *port = (struct uart_port *)_port;
257 spin_lock_irqsave(&ltq_asc_lock, flags);
258 ltq_w32(ASC_IRNCR_TIR, port->membase + LTQ_ASC_IRNCR);
259 spin_unlock_irqrestore(&ltq_asc_lock, flags);
260 lqasc_start_tx(port);
261 return IRQ_HANDLED;
262}
263
264static irqreturn_t
265lqasc_err_int(int irq, void *_port)
266{
267 unsigned long flags;
268 struct uart_port *port = (struct uart_port *)_port;
269 spin_lock_irqsave(&ltq_asc_lock, flags);
270 /* clear any pending interrupts */
271 ltq_w32_mask(0, ASCWHBSTATE_CLRPE | ASCWHBSTATE_CLRFE |
272 ASCWHBSTATE_CLRROE, port->membase + LTQ_ASC_WHBSTATE);
273 spin_unlock_irqrestore(&ltq_asc_lock, flags);
274 return IRQ_HANDLED;
275}
276
277static irqreturn_t
278lqasc_rx_int(int irq, void *_port)
279{
280 unsigned long flags;
281 struct uart_port *port = (struct uart_port *)_port;
282 spin_lock_irqsave(&ltq_asc_lock, flags);
283 ltq_w32(ASC_IRNCR_RIR, port->membase + LTQ_ASC_IRNCR);
284 lqasc_rx_chars(port);
285 spin_unlock_irqrestore(&ltq_asc_lock, flags);
286 return IRQ_HANDLED;
287}
288
289static unsigned int
290lqasc_tx_empty(struct uart_port *port)
291{
292 int status;
293 status = ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_TXFFLMASK;
294 return status ? 0 : TIOCSER_TEMT;
295}
296
297static unsigned int
298lqasc_get_mctrl(struct uart_port *port)
299{
300 return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
301}
302
303static void
304lqasc_set_mctrl(struct uart_port *port, u_int mctrl)
305{
306}
307
308static void
309lqasc_break_ctl(struct uart_port *port, int break_state)
310{
311}
312
313static int
314lqasc_startup(struct uart_port *port)
315{
316 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
317 int retval;
318
319 port->uartclk = clk_get_rate(ltq_port->clk);
320
321 ltq_w32_mask(ASCCLC_DISS | ASCCLC_RMCMASK, (1 << ASCCLC_RMCOFFSET),
322 port->membase + LTQ_ASC_CLC);
323
324 ltq_w32(0, port->membase + LTQ_ASC_PISEL);
325 ltq_w32(
326 ((TXFIFO_FL << ASCTXFCON_TXFITLOFF) & ASCTXFCON_TXFITLMASK) |
327 ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU,
328 port->membase + LTQ_ASC_TXFCON);
329 ltq_w32(
330 ((RXFIFO_FL << ASCRXFCON_RXFITLOFF) & ASCRXFCON_RXFITLMASK)
331 | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU,
332 port->membase + LTQ_ASC_RXFCON);
333 /* make sure other settings are written to hardware before
334 * setting enable bits
335 */
336 wmb();
337 ltq_w32_mask(0, ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN |
338 ASCCON_ROEN, port->membase + LTQ_ASC_CON);
339
340 retval = request_irq(ltq_port->tx_irq, lqasc_tx_int,
341 IRQF_DISABLED, "asc_tx", port);
342 if (retval) {
343 pr_err("failed to request lqasc_tx_int\n");
344 return retval;
345 }
346
347 retval = request_irq(ltq_port->rx_irq, lqasc_rx_int,
348 IRQF_DISABLED, "asc_rx", port);
349 if (retval) {
350 pr_err("failed to request lqasc_rx_int\n");
351 goto err1;
352 }
353
354 retval = request_irq(ltq_port->err_irq, lqasc_err_int,
355 IRQF_DISABLED, "asc_err", port);
356 if (retval) {
357 pr_err("failed to request lqasc_err_int\n");
358 goto err2;
359 }
360
361 ltq_w32(ASC_IRNREN_RX | ASC_IRNREN_ERR | ASC_IRNREN_TX,
362 port->membase + LTQ_ASC_IRNREN);
363 return 0;
364
365err2:
366 free_irq(ltq_port->rx_irq, port);
367err1:
368 free_irq(ltq_port->tx_irq, port);
369 return retval;
370}
371
372static void
373lqasc_shutdown(struct uart_port *port)
374{
375 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
376 free_irq(ltq_port->tx_irq, port);
377 free_irq(ltq_port->rx_irq, port);
378 free_irq(ltq_port->err_irq, port);
379
380 ltq_w32(0, port->membase + LTQ_ASC_CON);
381 ltq_w32_mask(ASCRXFCON_RXFEN, ASCRXFCON_RXFFLU,
382 port->membase + LTQ_ASC_RXFCON);
383 ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
384 port->membase + LTQ_ASC_TXFCON);
385}
386
387static void
388lqasc_set_termios(struct uart_port *port,
389 struct ktermios *new, struct ktermios *old)
390{
391 unsigned int cflag;
392 unsigned int iflag;
393 unsigned int divisor;
394 unsigned int baud;
395 unsigned int con = 0;
396 unsigned long flags;
397
398 cflag = new->c_cflag;
399 iflag = new->c_iflag;
400
401 switch (cflag & CSIZE) {
402 case CS7:
403 con = ASCCON_M_7ASYNC;
404 break;
405
406 case CS5:
407 case CS6:
408 default:
409 new->c_cflag &= ~ CSIZE;
410 new->c_cflag |= CS8;
411 con = ASCCON_M_8ASYNC;
412 break;
413 }
414
415 cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
416
417 if (cflag & CSTOPB)
418 con |= ASCCON_STP;
419
420 if (cflag & PARENB) {
421 if (!(cflag & PARODD))
422 con &= ~ASCCON_ODD;
423 else
424 con |= ASCCON_ODD;
425 }
426
427 port->read_status_mask = ASCSTATE_ROE;
428 if (iflag & INPCK)
429 port->read_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
430
431 port->ignore_status_mask = 0;
432 if (iflag & IGNPAR)
433 port->ignore_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
434
435 if (iflag & IGNBRK) {
436 /*
437 * If we're ignoring parity and break indicators,
438 * ignore overruns too (for real raw support).
439 */
440 if (iflag & IGNPAR)
441 port->ignore_status_mask |= ASCSTATE_ROE;
442 }
443
444 if ((cflag & CREAD) == 0)
445 port->ignore_status_mask |= UART_DUMMY_UER_RX;
446
447 /* set error signals - framing, parity and overrun, enable receiver */
448 con |= ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN;
449
450 spin_lock_irqsave(&ltq_asc_lock, flags);
451
452 /* set up CON */
453 ltq_w32_mask(0, con, port->membase + LTQ_ASC_CON);
454
455 /* Set baud rate - take a divider of 2 into account */
456 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
457 divisor = uart_get_divisor(port, baud);
458 divisor = divisor / 2 - 1;
459
460 /* disable the baudrate generator */
461 ltq_w32_mask(ASCCON_R, 0, port->membase + LTQ_ASC_CON);
462
463 /* make sure the fractional divider is off */
464 ltq_w32_mask(ASCCON_FDE, 0, port->membase + LTQ_ASC_CON);
465
466 /* set up to use divisor of 2 */
467 ltq_w32_mask(ASCCON_BRS, 0, port->membase + LTQ_ASC_CON);
468
469 /* now we can write the new baudrate into the register */
470 ltq_w32(divisor, port->membase + LTQ_ASC_BG);
471
472 /* turn the baudrate generator back on */
473 ltq_w32_mask(0, ASCCON_R, port->membase + LTQ_ASC_CON);
474
475 /* enable rx */
476 ltq_w32(ASCWHBSTATE_SETREN, port->membase + LTQ_ASC_WHBSTATE);
477
478 spin_unlock_irqrestore(&ltq_asc_lock, flags);
479
480 /* Don't rewrite B0 */
481 if (tty_termios_baud_rate(new))
482 tty_termios_encode_baud_rate(new, baud, baud);
483}
484
485static const char*
486lqasc_type(struct uart_port *port)
487{
488 if (port->type == PORT_LTQ_ASC)
489 return DRVNAME;
490 else
491 return NULL;
492}
493
494static void
495lqasc_release_port(struct uart_port *port)
496{
497 if (port->flags & UPF_IOREMAP) {
498 iounmap(port->membase);
499 port->membase = NULL;
500 }
501}
502
503static int
504lqasc_request_port(struct uart_port *port)
505{
506 struct platform_device *pdev = to_platform_device(port->dev);
507 struct resource *res;
508 int size;
509
510 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
511 if (!res) {
512 dev_err(&pdev->dev, "cannot obtain I/O memory region");
513 return -ENODEV;
514 }
515 size = resource_size(res);
516
517 res = devm_request_mem_region(&pdev->dev, res->start,
518 size, dev_name(&pdev->dev));
519 if (!res) {
520 dev_err(&pdev->dev, "cannot request I/O memory region");
521 return -EBUSY;
522 }
523
524 if (port->flags & UPF_IOREMAP) {
525 port->membase = devm_ioremap_nocache(&pdev->dev,
526 port->mapbase, size);
527 if (port->membase == NULL)
528 return -ENOMEM;
529 }
530 return 0;
531}
532
533static void
534lqasc_config_port(struct uart_port *port, int flags)
535{
536 if (flags & UART_CONFIG_TYPE) {
537 port->type = PORT_LTQ_ASC;
538 lqasc_request_port(port);
539 }
540}
541
542static int
543lqasc_verify_port(struct uart_port *port,
544 struct serial_struct *ser)
545{
546 int ret = 0;
547 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LTQ_ASC)
548 ret = -EINVAL;
549 if (ser->irq < 0 || ser->irq >= NR_IRQS)
550 ret = -EINVAL;
551 if (ser->baud_base < 9600)
552 ret = -EINVAL;
553 return ret;
554}
555
556static struct uart_ops lqasc_pops = {
557 .tx_empty = lqasc_tx_empty,
558 .set_mctrl = lqasc_set_mctrl,
559 .get_mctrl = lqasc_get_mctrl,
560 .stop_tx = lqasc_stop_tx,
561 .start_tx = lqasc_start_tx,
562 .stop_rx = lqasc_stop_rx,
563 .enable_ms = lqasc_enable_ms,
564 .break_ctl = lqasc_break_ctl,
565 .startup = lqasc_startup,
566 .shutdown = lqasc_shutdown,
567 .set_termios = lqasc_set_termios,
568 .type = lqasc_type,
569 .release_port = lqasc_release_port,
570 .request_port = lqasc_request_port,
571 .config_port = lqasc_config_port,
572 .verify_port = lqasc_verify_port,
573};
574
575static void
576lqasc_console_putchar(struct uart_port *port, int ch)
577{
578 int fifofree;
579
580 if (!port->membase)
581 return;
582
583 do {
584 fifofree = (ltq_r32(port->membase + LTQ_ASC_FSTAT)
585 & ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF;
586 } while (fifofree == 0);
587 ltq_w8(ch, port->membase + LTQ_ASC_TBUF);
588}
589
590
591static void
592lqasc_console_write(struct console *co, const char *s, u_int count)
593{
594 struct ltq_uart_port *ltq_port;
595 struct uart_port *port;
596 unsigned long flags;
597
598 if (co->index >= MAXPORTS)
599 return;
600
601 ltq_port = lqasc_port[co->index];
602 if (!ltq_port)
603 return;
604
605 port = &ltq_port->port;
606
607 spin_lock_irqsave(&ltq_asc_lock, flags);
608 uart_console_write(port, s, count, lqasc_console_putchar);
609 spin_unlock_irqrestore(&ltq_asc_lock, flags);
610}
611
612static int __init
613lqasc_console_setup(struct console *co, char *options)
614{
615 struct ltq_uart_port *ltq_port;
616 struct uart_port *port;
617 int baud = 115200;
618 int bits = 8;
619 int parity = 'n';
620 int flow = 'n';
621
622 if (co->index >= MAXPORTS)
623 return -ENODEV;
624
625 ltq_port = lqasc_port[co->index];
626 if (!ltq_port)
627 return -ENODEV;
628
629 port = &ltq_port->port;
630
631 port->uartclk = clk_get_rate(ltq_port->clk);
632
633 if (options)
634 uart_parse_options(options, &baud, &parity, &bits, &flow);
635 return uart_set_options(port, co, baud, parity, bits, flow);
636}
637
638static struct console lqasc_console = {
639 .name = "ttyLTQ",
640 .write = lqasc_console_write,
641 .device = uart_console_device,
642 .setup = lqasc_console_setup,
643 .flags = CON_PRINTBUFFER,
644 .index = -1,
645 .data = &lqasc_reg,
646};
647
648static int __init
649lqasc_console_init(void)
650{
651 register_console(&lqasc_console);
652 return 0;
653}
654console_initcall(lqasc_console_init);
655
656static struct uart_driver lqasc_reg = {
657 .owner = THIS_MODULE,
658 .driver_name = DRVNAME,
659 .dev_name = "ttyLTQ",
660 .major = 0,
661 .minor = 0,
662 .nr = MAXPORTS,
663 .cons = &lqasc_console,
664};
665
666static int __init
667lqasc_probe(struct platform_device *pdev)
668{
669 struct ltq_uart_port *ltq_port;
670 struct uart_port *port;
671 struct resource *mmres, *irqres;
672 int tx_irq, rx_irq, err_irq;
673 struct clk *clk;
674 int ret;
675
676 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
677 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
678 if (!mmres || !irqres)
679 return -ENODEV;
680
681 if (pdev->id >= MAXPORTS)
682 return -EBUSY;
683
684 if (lqasc_port[pdev->id] != NULL)
685 return -EBUSY;
686
687 clk = clk_get(&pdev->dev, "fpi");
688 if (IS_ERR(clk)) {
689 pr_err("failed to get fpi clk\n");
690 return -ENOENT;
691 }
692
693 tx_irq = platform_get_irq_byname(pdev, "tx");
694 rx_irq = platform_get_irq_byname(pdev, "rx");
695 err_irq = platform_get_irq_byname(pdev, "err");
696 if ((tx_irq < 0) | (rx_irq < 0) | (err_irq < 0))
697 return -ENODEV;
698
699 ltq_port = kzalloc(sizeof(struct ltq_uart_port), GFP_KERNEL);
700 if (!ltq_port)
701 return -ENOMEM;
702
703 port = &ltq_port->port;
704
705 port->iotype = SERIAL_IO_MEM;
706 port->flags = ASYNC_BOOT_AUTOCONF | UPF_IOREMAP;
707 port->ops = &lqasc_pops;
708 port->fifosize = 16;
709 port->type = PORT_LTQ_ASC,
710 port->line = pdev->id;
711 port->dev = &pdev->dev;
712
713 port->irq = tx_irq; /* unused, just to be backward-compatibe */
714 port->mapbase = mmres->start;
715
716 ltq_port->clk = clk;
717
718 ltq_port->tx_irq = tx_irq;
719 ltq_port->rx_irq = rx_irq;
720 ltq_port->err_irq = err_irq;
721
722 lqasc_port[pdev->id] = ltq_port;
723 platform_set_drvdata(pdev, ltq_port);
724
725 ret = uart_add_one_port(&lqasc_reg, port);
726
727 return ret;
728}
729
730static struct platform_driver lqasc_driver = {
731 .driver = {
732 .name = DRVNAME,
733 .owner = THIS_MODULE,
734 },
735};
736
737int __init
738init_lqasc(void)
739{
740 int ret;
741
742 ret = uart_register_driver(&lqasc_reg);
743 if (ret != 0)
744 return ret;
745
746 ret = platform_driver_probe(&lqasc_driver, lqasc_probe);
747 if (ret != 0)
748 uart_unregister_driver(&lqasc_reg);
749
750 return ret;
751}
752
753module_init(init_lqasc);
754
755MODULE_DESCRIPTION("Lantiq serial port driver");
756MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/m32r_sio.c b/drivers/tty/serial/m32r_sio.c
new file mode 100644
index 000000000000..84db7321cce8
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio.c
@@ -0,0 +1,1193 @@
1/*
2 * m32r_sio.c
3 *
4 * Driver for M32R serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.c.
8 *
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18/*
19 * A note about mapbase / membase
20 *
21 * mapbase is the physical address of the IO port. Currently, we don't
22 * support this very well, and it may well be dropped from this driver
23 * in future. As such, mapbase should be NULL.
24 *
25 * membase is an 'ioremapped' cookie. This is compatible with the old
26 * serial.c driver, and is currently the preferred form.
27 */
28
29#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/tty.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/serial.h>
40#include <linux/serialP.h>
41#include <linux/delay.h>
42
43#include <asm/m32r.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46
47#define PORT_M32R_BASE PORT_M32R_SIO
48#define PORT_INDEX(x) (x - PORT_M32R_BASE + 1)
49#define BAUD_RATE 115200
50
51#include <linux/serial_core.h>
52#include "m32r_sio.h"
53#include "m32r_sio_reg.h"
54
55/*
56 * Debugging.
57 */
58#if 0
59#define DEBUG_AUTOCONF(fmt...) printk(fmt)
60#else
61#define DEBUG_AUTOCONF(fmt...) do { } while (0)
62#endif
63
64#if 0
65#define DEBUG_INTR(fmt...) printk(fmt)
66#else
67#define DEBUG_INTR(fmt...) do { } while (0)
68#endif
69
70#define PASS_LIMIT 256
71
72/*
73 * We default to IRQ0 for the "no irq" hack. Some
74 * machine types want others as well - they're free
75 * to redefine this in their header file.
76 */
77#define is_real_interrupt(irq) ((irq) != 0)
78
79#define BASE_BAUD 115200
80
81/* Standard COM flags */
82#define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)
83
84/*
85 * SERIAL_PORT_DFNS tells us about built-in ports that have no
86 * standard enumeration mechanism. Platforms that can find all
87 * serial ports via mechanisms like ACPI or PCI need not supply it.
88 */
89#if defined(CONFIG_PLAT_USRV)
90
91#define SERIAL_PORT_DFNS \
92 /* UART CLK PORT IRQ FLAGS */ \
93 { 0, BASE_BAUD, 0x3F8, PLD_IRQ_UART0, STD_COM_FLAGS }, /* ttyS0 */ \
94 { 0, BASE_BAUD, 0x2F8, PLD_IRQ_UART1, STD_COM_FLAGS }, /* ttyS1 */
95
96#else /* !CONFIG_PLAT_USRV */
97
98#if defined(CONFIG_SERIAL_M32R_PLDSIO)
99#define SERIAL_PORT_DFNS \
100 { 0, BASE_BAUD, ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV, \
101 STD_COM_FLAGS }, /* ttyS0 */
102#else
103#define SERIAL_PORT_DFNS \
104 { 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R, \
105 STD_COM_FLAGS }, /* ttyS0 */
106#endif
107
108#endif /* !CONFIG_PLAT_USRV */
109
110static struct old_serial_port old_serial_port[] = {
111 SERIAL_PORT_DFNS
112};
113
114#define UART_NR ARRAY_SIZE(old_serial_port)
115
116struct uart_sio_port {
117 struct uart_port port;
118 struct timer_list timer; /* "no irq" timer */
119 struct list_head list; /* ports on this IRQ */
120 unsigned short rev;
121 unsigned char acr;
122 unsigned char ier;
123 unsigned char lcr;
124 unsigned char mcr_mask; /* mask of user bits */
125 unsigned char mcr_force; /* mask of forced bits */
126 unsigned char lsr_break_flag;
127
128 /*
129 * We provide a per-port pm hook.
130 */
131 void (*pm)(struct uart_port *port,
132 unsigned int state, unsigned int old);
133};
134
135struct irq_info {
136 spinlock_t lock;
137 struct list_head *head;
138};
139
140static struct irq_info irq_lists[NR_IRQS];
141
142/*
143 * Here we define the default xmit fifo size used for each type of UART.
144 */
145static const struct serial_uart_config uart_config[] = {
146 [PORT_UNKNOWN] = {
147 .name = "unknown",
148 .dfl_xmit_fifo_size = 1,
149 .flags = 0,
150 },
151 [PORT_INDEX(PORT_M32R_SIO)] = {
152 .name = "M32RSIO",
153 .dfl_xmit_fifo_size = 1,
154 .flags = 0,
155 },
156};
157
158#ifdef CONFIG_SERIAL_M32R_PLDSIO
159
160#define __sio_in(x) inw((unsigned long)(x))
161#define __sio_out(v,x) outw((v),(unsigned long)(x))
162
163static inline void sio_set_baud_rate(unsigned long baud)
164{
165 unsigned short sbaud;
166 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
167 __sio_out(sbaud, PLD_ESIO0BAUR);
168}
169
170static void sio_reset(void)
171{
172 unsigned short tmp;
173
174 tmp = __sio_in(PLD_ESIO0RXB);
175 tmp = __sio_in(PLD_ESIO0RXB);
176 tmp = __sio_in(PLD_ESIO0CR);
177 sio_set_baud_rate(BAUD_RATE);
178 __sio_out(0x0300, PLD_ESIO0CR);
179 __sio_out(0x0003, PLD_ESIO0CR);
180}
181
182static void sio_init(void)
183{
184 unsigned short tmp;
185
186 tmp = __sio_in(PLD_ESIO0RXB);
187 tmp = __sio_in(PLD_ESIO0RXB);
188 tmp = __sio_in(PLD_ESIO0CR);
189 __sio_out(0x0300, PLD_ESIO0CR);
190 __sio_out(0x0003, PLD_ESIO0CR);
191}
192
193static void sio_error(int *status)
194{
195 printk("SIO0 error[%04x]\n", *status);
196 do {
197 sio_init();
198 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
199}
200
201#else /* not CONFIG_SERIAL_M32R_PLDSIO */
202
203#define __sio_in(x) inl(x)
204#define __sio_out(v,x) outl((v),(x))
205
206static inline void sio_set_baud_rate(unsigned long baud)
207{
208 unsigned long i, j;
209
210 i = boot_cpu_data.bus_clock / (baud * 16);
211 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
212 i -= 1;
213 j = (j + 1) >> 1;
214
215 __sio_out(i, M32R_SIO0_BAUR_PORTL);
216 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
217}
218
219static void sio_reset(void)
220{
221 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
222 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
223 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
224 sio_set_baud_rate(BAUD_RATE);
225 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
226 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
227}
228
229static void sio_init(void)
230{
231 unsigned int tmp;
232
233 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
234 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
235 tmp = __sio_in(M32R_SIO0_STS_PORTL);
236 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
237}
238
239static void sio_error(int *status)
240{
241 printk("SIO0 error[%04x]\n", *status);
242 do {
243 sio_init();
244 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
245}
246
247#endif /* CONFIG_SERIAL_M32R_PLDSIO */
248
249static unsigned int sio_in(struct uart_sio_port *up, int offset)
250{
251 return __sio_in(up->port.iobase + offset);
252}
253
254static void sio_out(struct uart_sio_port *up, int offset, int value)
255{
256 __sio_out(value, up->port.iobase + offset);
257}
258
259static unsigned int serial_in(struct uart_sio_port *up, int offset)
260{
261 if (!offset)
262 return 0;
263
264 return __sio_in(offset);
265}
266
267static void serial_out(struct uart_sio_port *up, int offset, int value)
268{
269 if (!offset)
270 return;
271
272 __sio_out(value, offset);
273}
274
275static void m32r_sio_stop_tx(struct uart_port *port)
276{
277 struct uart_sio_port *up = (struct uart_sio_port *)port;
278
279 if (up->ier & UART_IER_THRI) {
280 up->ier &= ~UART_IER_THRI;
281 serial_out(up, UART_IER, up->ier);
282 }
283}
284
285static void m32r_sio_start_tx(struct uart_port *port)
286{
287#ifdef CONFIG_SERIAL_M32R_PLDSIO
288 struct uart_sio_port *up = (struct uart_sio_port *)port;
289 struct circ_buf *xmit = &up->port.state->xmit;
290
291 if (!(up->ier & UART_IER_THRI)) {
292 up->ier |= UART_IER_THRI;
293 serial_out(up, UART_IER, up->ier);
294 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
295 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
296 up->port.icount.tx++;
297 }
298 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
299#else
300 struct uart_sio_port *up = (struct uart_sio_port *)port;
301
302 if (!(up->ier & UART_IER_THRI)) {
303 up->ier |= UART_IER_THRI;
304 serial_out(up, UART_IER, up->ier);
305 }
306#endif
307}
308
309static void m32r_sio_stop_rx(struct uart_port *port)
310{
311 struct uart_sio_port *up = (struct uart_sio_port *)port;
312
313 up->ier &= ~UART_IER_RLSI;
314 up->port.read_status_mask &= ~UART_LSR_DR;
315 serial_out(up, UART_IER, up->ier);
316}
317
318static void m32r_sio_enable_ms(struct uart_port *port)
319{
320 struct uart_sio_port *up = (struct uart_sio_port *)port;
321
322 up->ier |= UART_IER_MSI;
323 serial_out(up, UART_IER, up->ier);
324}
325
326static void receive_chars(struct uart_sio_port *up, int *status)
327{
328 struct tty_struct *tty = up->port.state->port.tty;
329 unsigned char ch;
330 unsigned char flag;
331 int max_count = 256;
332
333 do {
334 ch = sio_in(up, SIORXB);
335 flag = TTY_NORMAL;
336 up->port.icount.rx++;
337
338 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
339 UART_LSR_FE | UART_LSR_OE))) {
340 /*
341 * For statistics only
342 */
343 if (*status & UART_LSR_BI) {
344 *status &= ~(UART_LSR_FE | UART_LSR_PE);
345 up->port.icount.brk++;
346 /*
347 * We do the SysRQ and SAK checking
348 * here because otherwise the break
349 * may get masked by ignore_status_mask
350 * or read_status_mask.
351 */
352 if (uart_handle_break(&up->port))
353 goto ignore_char;
354 } else if (*status & UART_LSR_PE)
355 up->port.icount.parity++;
356 else if (*status & UART_LSR_FE)
357 up->port.icount.frame++;
358 if (*status & UART_LSR_OE)
359 up->port.icount.overrun++;
360
361 /*
362 * Mask off conditions which should be ingored.
363 */
364 *status &= up->port.read_status_mask;
365
366 if (up->port.line == up->port.cons->index) {
367 /* Recover the break flag from console xmit */
368 *status |= up->lsr_break_flag;
369 up->lsr_break_flag = 0;
370 }
371
372 if (*status & UART_LSR_BI) {
373 DEBUG_INTR("handling break....");
374 flag = TTY_BREAK;
375 } else if (*status & UART_LSR_PE)
376 flag = TTY_PARITY;
377 else if (*status & UART_LSR_FE)
378 flag = TTY_FRAME;
379 }
380 if (uart_handle_sysrq_char(&up->port, ch))
381 goto ignore_char;
382 if ((*status & up->port.ignore_status_mask) == 0)
383 tty_insert_flip_char(tty, ch, flag);
384
385 if (*status & UART_LSR_OE) {
386 /*
387 * Overrun is special, since it's reported
388 * immediately, and doesn't affect the current
389 * character.
390 */
391 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
392 }
393 ignore_char:
394 *status = serial_in(up, UART_LSR);
395 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
396 tty_flip_buffer_push(tty);
397}
398
399static void transmit_chars(struct uart_sio_port *up)
400{
401 struct circ_buf *xmit = &up->port.state->xmit;
402 int count;
403
404 if (up->port.x_char) {
405#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
406 serial_out(up, UART_TX, up->port.x_char);
407#endif
408 up->port.icount.tx++;
409 up->port.x_char = 0;
410 return;
411 }
412 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
413 m32r_sio_stop_tx(&up->port);
414 return;
415 }
416
417 count = up->port.fifosize;
418 do {
419 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
420 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
421 up->port.icount.tx++;
422 if (uart_circ_empty(xmit))
423 break;
424 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
425
426 } while (--count > 0);
427
428 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
429 uart_write_wakeup(&up->port);
430
431 DEBUG_INTR("THRE...");
432
433 if (uart_circ_empty(xmit))
434 m32r_sio_stop_tx(&up->port);
435}
436
437/*
438 * This handles the interrupt from one port.
439 */
440static inline void m32r_sio_handle_port(struct uart_sio_port *up,
441 unsigned int status)
442{
443 DEBUG_INTR("status = %x...", status);
444
445 if (status & 0x04)
446 receive_chars(up, &status);
447 if (status & 0x01)
448 transmit_chars(up);
449}
450
451/*
452 * This is the serial driver's interrupt routine.
453 *
454 * Arjan thinks the old way was overly complex, so it got simplified.
455 * Alan disagrees, saying that need the complexity to handle the weird
456 * nature of ISA shared interrupts. (This is a special exception.)
457 *
458 * In order to handle ISA shared interrupts properly, we need to check
459 * that all ports have been serviced, and therefore the ISA interrupt
460 * line has been de-asserted.
461 *
462 * This means we need to loop through all ports. checking that they
463 * don't have an interrupt pending.
464 */
465static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
466{
467 struct irq_info *i = dev_id;
468 struct list_head *l, *end = NULL;
469 int pass_counter = 0;
470
471 DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
472
473#ifdef CONFIG_SERIAL_M32R_PLDSIO
474// if (irq == PLD_IRQ_SIO0_SND)
475// irq = PLD_IRQ_SIO0_RCV;
476#else
477 if (irq == M32R_IRQ_SIO0_S)
478 irq = M32R_IRQ_SIO0_R;
479#endif
480
481 spin_lock(&i->lock);
482
483 l = i->head;
484 do {
485 struct uart_sio_port *up;
486 unsigned int sts;
487
488 up = list_entry(l, struct uart_sio_port, list);
489
490 sts = sio_in(up, SIOSTS);
491 if (sts & 0x5) {
492 spin_lock(&up->port.lock);
493 m32r_sio_handle_port(up, sts);
494 spin_unlock(&up->port.lock);
495
496 end = NULL;
497 } else if (end == NULL)
498 end = l;
499
500 l = l->next;
501
502 if (l == i->head && pass_counter++ > PASS_LIMIT) {
503 if (sts & 0xe0)
504 sio_error(&sts);
505 break;
506 }
507 } while (l != end);
508
509 spin_unlock(&i->lock);
510
511 DEBUG_INTR("end.\n");
512
513 return IRQ_HANDLED;
514}
515
516/*
517 * To support ISA shared interrupts, we need to have one interrupt
518 * handler that ensures that the IRQ line has been deasserted
519 * before returning. Failing to do this will result in the IRQ
520 * line being stuck active, and, since ISA irqs are edge triggered,
521 * no more IRQs will be seen.
522 */
523static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
524{
525 spin_lock_irq(&i->lock);
526
527 if (!list_empty(i->head)) {
528 if (i->head == &up->list)
529 i->head = i->head->next;
530 list_del(&up->list);
531 } else {
532 BUG_ON(i->head != &up->list);
533 i->head = NULL;
534 }
535
536 spin_unlock_irq(&i->lock);
537}
538
539static int serial_link_irq_chain(struct uart_sio_port *up)
540{
541 struct irq_info *i = irq_lists + up->port.irq;
542 int ret, irq_flags = 0;
543
544 spin_lock_irq(&i->lock);
545
546 if (i->head) {
547 list_add(&up->list, i->head);
548 spin_unlock_irq(&i->lock);
549
550 ret = 0;
551 } else {
552 INIT_LIST_HEAD(&up->list);
553 i->head = &up->list;
554 spin_unlock_irq(&i->lock);
555
556 ret = request_irq(up->port.irq, m32r_sio_interrupt,
557 irq_flags, "SIO0-RX", i);
558 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
559 irq_flags, "SIO0-TX", i);
560 if (ret < 0)
561 serial_do_unlink(i, up);
562 }
563
564 return ret;
565}
566
567static void serial_unlink_irq_chain(struct uart_sio_port *up)
568{
569 struct irq_info *i = irq_lists + up->port.irq;
570
571 BUG_ON(i->head == NULL);
572
573 if (list_empty(i->head)) {
574 free_irq(up->port.irq, i);
575 free_irq(up->port.irq + 1, i);
576 }
577
578 serial_do_unlink(i, up);
579}
580
581/*
582 * This function is used to handle ports that do not have an interrupt.
583 */
584static void m32r_sio_timeout(unsigned long data)
585{
586 struct uart_sio_port *up = (struct uart_sio_port *)data;
587 unsigned int timeout;
588 unsigned int sts;
589
590 sts = sio_in(up, SIOSTS);
591 if (sts & 0x5) {
592 spin_lock(&up->port.lock);
593 m32r_sio_handle_port(up, sts);
594 spin_unlock(&up->port.lock);
595 }
596
597 timeout = up->port.timeout;
598 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
599 mod_timer(&up->timer, jiffies + timeout);
600}
601
602static unsigned int m32r_sio_tx_empty(struct uart_port *port)
603{
604 struct uart_sio_port *up = (struct uart_sio_port *)port;
605 unsigned long flags;
606 unsigned int ret;
607
608 spin_lock_irqsave(&up->port.lock, flags);
609 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
610 spin_unlock_irqrestore(&up->port.lock, flags);
611
612 return ret;
613}
614
615static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
616{
617 return 0;
618}
619
620static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
621{
622
623}
624
625static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
626{
627
628}
629
630static int m32r_sio_startup(struct uart_port *port)
631{
632 struct uart_sio_port *up = (struct uart_sio_port *)port;
633 int retval;
634
635 sio_init();
636
637 /*
638 * If the "interrupt" for this port doesn't correspond with any
639 * hardware interrupt, we use a timer-based system. The original
640 * driver used to do this with IRQ0.
641 */
642 if (!is_real_interrupt(up->port.irq)) {
643 unsigned int timeout = up->port.timeout;
644
645 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
646
647 up->timer.data = (unsigned long)up;
648 mod_timer(&up->timer, jiffies + timeout);
649 } else {
650 retval = serial_link_irq_chain(up);
651 if (retval)
652 return retval;
653 }
654
655 /*
656 * Finally, enable interrupts. Note: Modem status interrupts
657 * are set via set_termios(), which will be occurring imminently
658 * anyway, so we don't enable them here.
659 * - M32R_SIO: 0x0c
660 * - M32R_PLDSIO: 0x04
661 */
662 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
663 sio_out(up, SIOTRCR, up->ier);
664
665 /*
666 * And clear the interrupt registers again for luck.
667 */
668 sio_reset();
669
670 return 0;
671}
672
673static void m32r_sio_shutdown(struct uart_port *port)
674{
675 struct uart_sio_port *up = (struct uart_sio_port *)port;
676
677 /*
678 * Disable interrupts from this port
679 */
680 up->ier = 0;
681 sio_out(up, SIOTRCR, 0);
682
683 /*
684 * Disable break condition and FIFOs
685 */
686
687 sio_init();
688
689 if (!is_real_interrupt(up->port.irq))
690 del_timer_sync(&up->timer);
691 else
692 serial_unlink_irq_chain(up);
693}
694
695static unsigned int m32r_sio_get_divisor(struct uart_port *port,
696 unsigned int baud)
697{
698 return uart_get_divisor(port, baud);
699}
700
701static void m32r_sio_set_termios(struct uart_port *port,
702 struct ktermios *termios, struct ktermios *old)
703{
704 struct uart_sio_port *up = (struct uart_sio_port *)port;
705 unsigned char cval = 0;
706 unsigned long flags;
707 unsigned int baud, quot;
708
709 switch (termios->c_cflag & CSIZE) {
710 case CS5:
711 cval = UART_LCR_WLEN5;
712 break;
713 case CS6:
714 cval = UART_LCR_WLEN6;
715 break;
716 case CS7:
717 cval = UART_LCR_WLEN7;
718 break;
719 default:
720 case CS8:
721 cval = UART_LCR_WLEN8;
722 break;
723 }
724
725 if (termios->c_cflag & CSTOPB)
726 cval |= UART_LCR_STOP;
727 if (termios->c_cflag & PARENB)
728 cval |= UART_LCR_PARITY;
729 if (!(termios->c_cflag & PARODD))
730 cval |= UART_LCR_EPAR;
731#ifdef CMSPAR
732 if (termios->c_cflag & CMSPAR)
733 cval |= UART_LCR_SPAR;
734#endif
735
736 /*
737 * Ask the core to calculate the divisor for us.
738 */
739#ifdef CONFIG_SERIAL_M32R_PLDSIO
740 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
741#else
742 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
743#endif
744 quot = m32r_sio_get_divisor(port, baud);
745
746 /*
747 * Ok, we're now changing the port state. Do it with
748 * interrupts disabled.
749 */
750 spin_lock_irqsave(&up->port.lock, flags);
751
752 sio_set_baud_rate(baud);
753
754 /*
755 * Update the per-port timeout.
756 */
757 uart_update_timeout(port, termios->c_cflag, baud);
758
759 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
760 if (termios->c_iflag & INPCK)
761 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
762 if (termios->c_iflag & (BRKINT | PARMRK))
763 up->port.read_status_mask |= UART_LSR_BI;
764
765 /*
766 * Characteres to ignore
767 */
768 up->port.ignore_status_mask = 0;
769 if (termios->c_iflag & IGNPAR)
770 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
771 if (termios->c_iflag & IGNBRK) {
772 up->port.ignore_status_mask |= UART_LSR_BI;
773 /*
774 * If we're ignoring parity and break indicators,
775 * ignore overruns too (for real raw support).
776 */
777 if (termios->c_iflag & IGNPAR)
778 up->port.ignore_status_mask |= UART_LSR_OE;
779 }
780
781 /*
782 * ignore all characters if CREAD is not set
783 */
784 if ((termios->c_cflag & CREAD) == 0)
785 up->port.ignore_status_mask |= UART_LSR_DR;
786
787 /*
788 * CTS flow control flag and modem status interrupts
789 */
790 up->ier &= ~UART_IER_MSI;
791 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
792 up->ier |= UART_IER_MSI;
793
794 serial_out(up, UART_IER, up->ier);
795
796 up->lcr = cval; /* Save LCR */
797 spin_unlock_irqrestore(&up->port.lock, flags);
798}
799
800static void m32r_sio_pm(struct uart_port *port, unsigned int state,
801 unsigned int oldstate)
802{
803 struct uart_sio_port *up = (struct uart_sio_port *)port;
804
805 if (up->pm)
806 up->pm(port, state, oldstate);
807}
808
809/*
810 * Resource handling. This is complicated by the fact that resources
811 * depend on the port type. Maybe we should be claiming the standard
812 * 8250 ports, and then trying to get other resources as necessary?
813 */
814static int
815m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
816{
817 unsigned int size = 8 << up->port.regshift;
818#ifndef CONFIG_SERIAL_M32R_PLDSIO
819 unsigned long start;
820#endif
821 int ret = 0;
822
823 switch (up->port.iotype) {
824 case UPIO_MEM:
825 if (up->port.mapbase) {
826#ifdef CONFIG_SERIAL_M32R_PLDSIO
827 *res = request_mem_region(up->port.mapbase, size, "serial");
828#else
829 start = up->port.mapbase;
830 *res = request_mem_region(start, size, "serial");
831#endif
832 if (!*res)
833 ret = -EBUSY;
834 }
835 break;
836
837 case UPIO_PORT:
838 *res = request_region(up->port.iobase, size, "serial");
839 if (!*res)
840 ret = -EBUSY;
841 break;
842 }
843 return ret;
844}
845
846static void m32r_sio_release_port(struct uart_port *port)
847{
848 struct uart_sio_port *up = (struct uart_sio_port *)port;
849 unsigned long start, offset = 0, size = 0;
850
851 size <<= up->port.regshift;
852
853 switch (up->port.iotype) {
854 case UPIO_MEM:
855 if (up->port.mapbase) {
856 /*
857 * Unmap the area.
858 */
859 iounmap(up->port.membase);
860 up->port.membase = NULL;
861
862 start = up->port.mapbase;
863
864 if (size)
865 release_mem_region(start + offset, size);
866 release_mem_region(start, 8 << up->port.regshift);
867 }
868 break;
869
870 case UPIO_PORT:
871 start = up->port.iobase;
872
873 if (size)
874 release_region(start + offset, size);
875 release_region(start + offset, 8 << up->port.regshift);
876 break;
877
878 default:
879 break;
880 }
881}
882
883static int m32r_sio_request_port(struct uart_port *port)
884{
885 struct uart_sio_port *up = (struct uart_sio_port *)port;
886 struct resource *res = NULL;
887 int ret = 0;
888
889 ret = m32r_sio_request_std_resource(up, &res);
890
891 /*
892 * If we have a mapbase, then request that as well.
893 */
894 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
895 int size = res->end - res->start + 1;
896
897 up->port.membase = ioremap(up->port.mapbase, size);
898 if (!up->port.membase)
899 ret = -ENOMEM;
900 }
901
902 if (ret < 0) {
903 if (res)
904 release_resource(res);
905 }
906
907 return ret;
908}
909
910static void m32r_sio_config_port(struct uart_port *port, int unused)
911{
912 struct uart_sio_port *up = (struct uart_sio_port *)port;
913 unsigned long flags;
914
915 spin_lock_irqsave(&up->port.lock, flags);
916
917 up->port.type = (PORT_M32R_SIO - PORT_M32R_BASE + 1);
918 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
919
920 spin_unlock_irqrestore(&up->port.lock, flags);
921}
922
923static int
924m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
925{
926 if (ser->irq >= nr_irqs || ser->irq < 0 ||
927 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
928 ser->type >= ARRAY_SIZE(uart_config))
929 return -EINVAL;
930 return 0;
931}
932
933static const char *
934m32r_sio_type(struct uart_port *port)
935{
936 int type = port->type;
937
938 if (type >= ARRAY_SIZE(uart_config))
939 type = 0;
940 return uart_config[type].name;
941}
942
943static struct uart_ops m32r_sio_pops = {
944 .tx_empty = m32r_sio_tx_empty,
945 .set_mctrl = m32r_sio_set_mctrl,
946 .get_mctrl = m32r_sio_get_mctrl,
947 .stop_tx = m32r_sio_stop_tx,
948 .start_tx = m32r_sio_start_tx,
949 .stop_rx = m32r_sio_stop_rx,
950 .enable_ms = m32r_sio_enable_ms,
951 .break_ctl = m32r_sio_break_ctl,
952 .startup = m32r_sio_startup,
953 .shutdown = m32r_sio_shutdown,
954 .set_termios = m32r_sio_set_termios,
955 .pm = m32r_sio_pm,
956 .type = m32r_sio_type,
957 .release_port = m32r_sio_release_port,
958 .request_port = m32r_sio_request_port,
959 .config_port = m32r_sio_config_port,
960 .verify_port = m32r_sio_verify_port,
961};
962
963static struct uart_sio_port m32r_sio_ports[UART_NR];
964
965static void __init m32r_sio_init_ports(void)
966{
967 struct uart_sio_port *up;
968 static int first = 1;
969 int i;
970
971 if (!first)
972 return;
973 first = 0;
974
975 for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
976 i++, up++) {
977 up->port.iobase = old_serial_port[i].port;
978 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
979 up->port.uartclk = old_serial_port[i].baud_base * 16;
980 up->port.flags = old_serial_port[i].flags;
981 up->port.membase = old_serial_port[i].iomem_base;
982 up->port.iotype = old_serial_port[i].io_type;
983 up->port.regshift = old_serial_port[i].iomem_reg_shift;
984 up->port.ops = &m32r_sio_pops;
985 }
986}
987
988static void __init m32r_sio_register_ports(struct uart_driver *drv)
989{
990 int i;
991
992 m32r_sio_init_ports();
993
994 for (i = 0; i < UART_NR; i++) {
995 struct uart_sio_port *up = &m32r_sio_ports[i];
996
997 up->port.line = i;
998 up->port.ops = &m32r_sio_pops;
999 init_timer(&up->timer);
1000 up->timer.function = m32r_sio_timeout;
1001
1002 /*
1003 * ALPHA_KLUDGE_MCR needs to be killed.
1004 */
1005 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1006 up->mcr_force = ALPHA_KLUDGE_MCR;
1007
1008 uart_add_one_port(drv, &up->port);
1009 }
1010}
1011
1012#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
1013
1014/*
1015 * Wait for transmitter & holding register to empty
1016 */
1017static inline void wait_for_xmitr(struct uart_sio_port *up)
1018{
1019 unsigned int status, tmout = 10000;
1020
1021 /* Wait up to 10ms for the character(s) to be sent. */
1022 do {
1023 status = sio_in(up, SIOSTS);
1024
1025 if (--tmout == 0)
1026 break;
1027 udelay(1);
1028 } while ((status & UART_EMPTY) != UART_EMPTY);
1029
1030 /* Wait up to 1s for flow control if necessary */
1031 if (up->port.flags & UPF_CONS_FLOW) {
1032 tmout = 1000000;
1033 while (--tmout)
1034 udelay(1);
1035 }
1036}
1037
1038static void m32r_sio_console_putchar(struct uart_port *port, int ch)
1039{
1040 struct uart_sio_port *up = (struct uart_sio_port *)port;
1041
1042 wait_for_xmitr(up);
1043 sio_out(up, SIOTXB, ch);
1044}
1045
1046/*
1047 * Print a string to the serial port trying not to disturb
1048 * any possible real use of the port...
1049 *
1050 * The console_lock must be held when we get here.
1051 */
1052static void m32r_sio_console_write(struct console *co, const char *s,
1053 unsigned int count)
1054{
1055 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1056 unsigned int ier;
1057
1058 /*
1059 * First save the UER then disable the interrupts
1060 */
1061 ier = sio_in(up, SIOTRCR);
1062 sio_out(up, SIOTRCR, 0);
1063
1064 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
1065
1066 /*
1067 * Finally, wait for transmitter to become empty
1068 * and restore the IER
1069 */
1070 wait_for_xmitr(up);
1071 sio_out(up, SIOTRCR, ier);
1072}
1073
1074static int __init m32r_sio_console_setup(struct console *co, char *options)
1075{
1076 struct uart_port *port;
1077 int baud = 9600;
1078 int bits = 8;
1079 int parity = 'n';
1080 int flow = 'n';
1081
1082 /*
1083 * Check whether an invalid uart number has been specified, and
1084 * if so, search for the first available port that does have
1085 * console support.
1086 */
1087 if (co->index >= UART_NR)
1088 co->index = 0;
1089 port = &m32r_sio_ports[co->index].port;
1090
1091 /*
1092 * Temporary fix.
1093 */
1094 spin_lock_init(&port->lock);
1095
1096 if (options)
1097 uart_parse_options(options, &baud, &parity, &bits, &flow);
1098
1099 return uart_set_options(port, co, baud, parity, bits, flow);
1100}
1101
1102static struct uart_driver m32r_sio_reg;
1103static struct console m32r_sio_console = {
1104 .name = "ttyS",
1105 .write = m32r_sio_console_write,
1106 .device = uart_console_device,
1107 .setup = m32r_sio_console_setup,
1108 .flags = CON_PRINTBUFFER,
1109 .index = -1,
1110 .data = &m32r_sio_reg,
1111};
1112
1113static int __init m32r_sio_console_init(void)
1114{
1115 sio_reset();
1116 sio_init();
1117 m32r_sio_init_ports();
1118 register_console(&m32r_sio_console);
1119 return 0;
1120}
1121console_initcall(m32r_sio_console_init);
1122
1123#define M32R_SIO_CONSOLE &m32r_sio_console
1124#else
1125#define M32R_SIO_CONSOLE NULL
1126#endif
1127
1128static struct uart_driver m32r_sio_reg = {
1129 .owner = THIS_MODULE,
1130 .driver_name = "sio",
1131 .dev_name = "ttyS",
1132 .major = TTY_MAJOR,
1133 .minor = 64,
1134 .nr = UART_NR,
1135 .cons = M32R_SIO_CONSOLE,
1136};
1137
1138/**
1139 * m32r_sio_suspend_port - suspend one serial port
1140 * @line: serial line number
1141 *
1142 * Suspend one serial port.
1143 */
1144void m32r_sio_suspend_port(int line)
1145{
1146 uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1147}
1148
1149/**
1150 * m32r_sio_resume_port - resume one serial port
1151 * @line: serial line number
1152 *
1153 * Resume one serial port.
1154 */
1155void m32r_sio_resume_port(int line)
1156{
1157 uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1158}
1159
1160static int __init m32r_sio_init(void)
1161{
1162 int ret, i;
1163
1164 printk(KERN_INFO "Serial: M32R SIO driver\n");
1165
1166 for (i = 0; i < nr_irqs; i++)
1167 spin_lock_init(&irq_lists[i].lock);
1168
1169 ret = uart_register_driver(&m32r_sio_reg);
1170 if (ret >= 0)
1171 m32r_sio_register_ports(&m32r_sio_reg);
1172
1173 return ret;
1174}
1175
1176static void __exit m32r_sio_exit(void)
1177{
1178 int i;
1179
1180 for (i = 0; i < UART_NR; i++)
1181 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1182
1183 uart_unregister_driver(&m32r_sio_reg);
1184}
1185
1186module_init(m32r_sio_init);
1187module_exit(m32r_sio_exit);
1188
1189EXPORT_SYMBOL(m32r_sio_suspend_port);
1190EXPORT_SYMBOL(m32r_sio_resume_port);
1191
1192MODULE_LICENSE("GPL");
1193MODULE_DESCRIPTION("Generic M32R SIO serial driver");
diff --git a/drivers/tty/serial/m32r_sio.h b/drivers/tty/serial/m32r_sio.h
new file mode 100644
index 000000000000..e9b7e11793b1
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio.h
@@ -0,0 +1,48 @@
1/*
2 * m32r_sio.h
3 *
4 * Driver for M32R serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.h.
8 *
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18
19struct m32r_sio_probe {
20 struct module *owner;
21 int (*pci_init_one)(struct pci_dev *dev);
22 void (*pci_remove_one)(struct pci_dev *dev);
23 void (*pnp_init)(void);
24};
25
26int m32r_sio_register_probe(struct m32r_sio_probe *probe);
27void m32r_sio_unregister_probe(struct m32r_sio_probe *probe);
28void m32r_sio_get_irq_map(unsigned int *map);
29void m32r_sio_suspend_port(int line);
30void m32r_sio_resume_port(int line);
31
32struct old_serial_port {
33 unsigned int uart;
34 unsigned int baud_base;
35 unsigned int port;
36 unsigned int irq;
37 unsigned int flags;
38 unsigned char io_type;
39 unsigned char __iomem *iomem_base;
40 unsigned short iomem_reg_shift;
41};
42
43#define _INLINE_ inline
44
45#define PROBE_RSA (1 << 0)
46#define PROBE_ANY (~0)
47
48#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
diff --git a/drivers/tty/serial/m32r_sio_reg.h b/drivers/tty/serial/m32r_sio_reg.h
new file mode 100644
index 000000000000..4671473793e3
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio_reg.h
@@ -0,0 +1,152 @@
1/*
2 * m32r_sio_reg.h
3 *
4 * Copyright (C) 1992, 1994 by Theodore Ts'o.
5 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
6 *
7 * Redistribution of this file is permitted under the terms of the GNU
8 * Public License (GPL)
9 *
10 * These are the UART port assignments, expressed as offsets from the base
11 * register. These assignments should hold for any serial port based on
12 * a 8250, 16450, or 16550(A).
13 */
14
15#ifndef _M32R_SIO_REG_H
16#define _M32R_SIO_REG_H
17
18
19#ifdef CONFIG_SERIAL_M32R_PLDSIO
20
21#define SIOCR 0x000
22#define SIOMOD0 0x002
23#define SIOMOD1 0x004
24#define SIOSTS 0x006
25#define SIOTRCR 0x008
26#define SIOBAUR 0x00a
27// #define SIORBAUR 0x018
28#define SIOTXB 0x00c
29#define SIORXB 0x00e
30
31#define UART_RX ((unsigned long) PLD_ESIO0RXB)
32 /* In: Receive buffer (DLAB=0) */
33#define UART_TX ((unsigned long) PLD_ESIO0TXB)
34 /* Out: Transmit buffer (DLAB=0) */
35#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
36#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
37 * In: Fifo count
38 * Out: Fifo custom trigger levels
39 * XR16C85x only */
40
41#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
42#define UART_IER ((unsigned long) PLD_ESIO0INTCR)
43 /* Out: Interrupt Enable Register */
44#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
45 * XR16C85x only */
46
47#define UART_IIR 0 /* In: Interrupt ID Register */
48#define UART_FCR 0 /* Out: FIFO Control Register */
49#define UART_EFR 0 /* I/O: Extended Features Register */
50 /* (DLAB=1, 16C660 only) */
51
52#define UART_LCR 0 /* Out: Line Control Register */
53#define UART_MCR 0 /* Out: Modem Control Register */
54#define UART_LSR ((unsigned long) PLD_ESIO0STS)
55 /* In: Line Status Register */
56#define UART_MSR 0 /* In: Modem Status Register */
57#define UART_SCR 0 /* I/O: Scratch Register */
58#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
59 * FCTR bit 6 selects SCR or EMSR
60 * XR16c85x only */
61
62#else /* not CONFIG_SERIAL_M32R_PLDSIO */
63
64#define SIOCR 0x000
65#define SIOMOD0 0x004
66#define SIOMOD1 0x008
67#define SIOSTS 0x00c
68#define SIOTRCR 0x010
69#define SIOBAUR 0x014
70#define SIORBAUR 0x018
71#define SIOTXB 0x01c
72#define SIORXB 0x020
73
74#define UART_RX M32R_SIO0_RXB_PORTL /* In: Receive buffer (DLAB=0) */
75#define UART_TX M32R_SIO0_TXB_PORTL /* Out: Transmit buffer (DLAB=0) */
76#define UART_DLL 0 /* Out: Divisor Latch Low (DLAB=1) */
77#define UART_TRG 0 /* (LCR=BF) FCTR bit 7 selects Rx or Tx
78 * In: Fifo count
79 * Out: Fifo custom trigger levels
80 * XR16C85x only */
81
82#define UART_DLM 0 /* Out: Divisor Latch High (DLAB=1) */
83#define UART_IER M32R_SIO0_TRCR_PORTL /* Out: Interrupt Enable Register */
84#define UART_FCTR 0 /* (LCR=BF) Feature Control Register
85 * XR16C85x only */
86
87#define UART_IIR 0 /* In: Interrupt ID Register */
88#define UART_FCR 0 /* Out: FIFO Control Register */
89#define UART_EFR 0 /* I/O: Extended Features Register */
90 /* (DLAB=1, 16C660 only) */
91
92#define UART_LCR 0 /* Out: Line Control Register */
93#define UART_MCR 0 /* Out: Modem Control Register */
94#define UART_LSR M32R_SIO0_STS_PORTL /* In: Line Status Register */
95#define UART_MSR 0 /* In: Modem Status Register */
96#define UART_SCR 0 /* I/O: Scratch Register */
97#define UART_EMSR 0 /* (LCR=BF) Extended Mode Select Register
98 * FCTR bit 6 selects SCR or EMSR
99 * XR16c85x only */
100
101#endif /* CONFIG_SERIAL_M32R_PLDSIO */
102
103#define UART_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
104
105/*
106 * These are the definitions for the Line Control Register
107 *
108 * Note: if the word length is 5 bits (UART_LCR_WLEN5), then setting
109 * UART_LCR_STOP will select 1.5 stop bits, not 2 stop bits.
110 */
111#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
112#define UART_LCR_SBC 0x40 /* Set break control */
113#define UART_LCR_SPAR 0x20 /* Stick parity (?) */
114#define UART_LCR_EPAR 0x10 /* Even parity select */
115#define UART_LCR_PARITY 0x08 /* Parity Enable */
116#define UART_LCR_STOP 0x04 /* Stop bits: 0=1 stop bit, 1= 2 stop bits */
117#define UART_LCR_WLEN5 0x00 /* Wordlength: 5 bits */
118#define UART_LCR_WLEN6 0x01 /* Wordlength: 6 bits */
119#define UART_LCR_WLEN7 0x02 /* Wordlength: 7 bits */
120#define UART_LCR_WLEN8 0x03 /* Wordlength: 8 bits */
121
122/*
123 * These are the definitions for the Line Status Register
124 */
125#define UART_LSR_TEMT 0x02 /* Transmitter empty */
126#define UART_LSR_THRE 0x01 /* Transmit-hold-register empty */
127#define UART_LSR_BI 0x00 /* Break interrupt indicator */
128#define UART_LSR_FE 0x80 /* Frame error indicator */
129#define UART_LSR_PE 0x40 /* Parity error indicator */
130#define UART_LSR_OE 0x20 /* Overrun error indicator */
131#define UART_LSR_DR 0x04 /* Receiver data ready */
132
133/*
134 * These are the definitions for the Interrupt Identification Register
135 */
136#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
137#define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
138
139#define UART_IIR_MSI 0x00 /* Modem status interrupt */
140#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
141#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
142#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
143
144/*
145 * These are the definitions for the Interrupt Enable Register
146 */
147#define UART_IER_MSI 0x00 /* Enable Modem status interrupt */
148#define UART_IER_RLSI 0x08 /* Enable receiver line status interrupt */
149#define UART_IER_THRI 0x03 /* Enable Transmitter holding register int. */
150#define UART_IER_RDI 0x04 /* Enable receiver data interrupt */
151
152#endif /* _M32R_SIO_REG_H */
diff --git a/drivers/tty/serial/max3100.c b/drivers/tty/serial/max3100.c
new file mode 100644
index 000000000000..7b951adac54b
--- /dev/null
+++ b/drivers/tty/serial/max3100.c
@@ -0,0 +1,926 @@
1/*
2 *
3 * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 *
11 * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
12 * to use polling for flow control. TX empty IRQ is unusable, since
13 * writing conf clears FIFO buffer and we cannot have this interrupt
14 * always asking us for attention.
15 *
16 * Example platform data:
17
18 static struct plat_max3100 max3100_plat_data = {
19 .loopback = 0,
20 .crystal = 0,
21 .poll_time = 100,
22 };
23
24 static struct spi_board_info spi_board_info[] = {
25 {
26 .modalias = "max3100",
27 .platform_data = &max3100_plat_data,
28 .irq = IRQ_EINT12,
29 .max_speed_hz = 5*1000*1000,
30 .chip_select = 0,
31 },
32 };
33
34 * The initial minor number is 209 in the low-density serial port:
35 * mknod /dev/ttyMAX0 c 204 209
36 */
37
38#define MAX3100_MAJOR 204
39#define MAX3100_MINOR 209
40/* 4 MAX3100s should be enough for everyone */
41#define MAX_MAX3100 4
42
43#include <linux/delay.h>
44#include <linux/slab.h>
45#include <linux/device.h>
46#include <linux/serial_core.h>
47#include <linux/serial.h>
48#include <linux/spi/spi.h>
49#include <linux/freezer.h>
50
51#include <linux/serial_max3100.h>
52
53#define MAX3100_C (1<<14)
54#define MAX3100_D (0<<14)
55#define MAX3100_W (1<<15)
56#define MAX3100_RX (0<<15)
57
58#define MAX3100_WC (MAX3100_W | MAX3100_C)
59#define MAX3100_RC (MAX3100_RX | MAX3100_C)
60#define MAX3100_WD (MAX3100_W | MAX3100_D)
61#define MAX3100_RD (MAX3100_RX | MAX3100_D)
62#define MAX3100_CMD (3 << 14)
63
64#define MAX3100_T (1<<14)
65#define MAX3100_R (1<<15)
66
67#define MAX3100_FEN (1<<13)
68#define MAX3100_SHDN (1<<12)
69#define MAX3100_TM (1<<11)
70#define MAX3100_RM (1<<10)
71#define MAX3100_PM (1<<9)
72#define MAX3100_RAM (1<<8)
73#define MAX3100_IR (1<<7)
74#define MAX3100_ST (1<<6)
75#define MAX3100_PE (1<<5)
76#define MAX3100_L (1<<4)
77#define MAX3100_BAUD (0xf)
78
79#define MAX3100_TE (1<<10)
80#define MAX3100_RAFE (1<<10)
81#define MAX3100_RTS (1<<9)
82#define MAX3100_CTS (1<<9)
83#define MAX3100_PT (1<<8)
84#define MAX3100_DATA (0xff)
85
86#define MAX3100_RT (MAX3100_R | MAX3100_T)
87#define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
88
89/* the following simulate a status reg for ignore_status_mask */
90#define MAX3100_STATUS_PE 1
91#define MAX3100_STATUS_FE 2
92#define MAX3100_STATUS_OE 4
93
94struct max3100_port {
95 struct uart_port port;
96 struct spi_device *spi;
97
98 int cts; /* last CTS received for flow ctrl */
99 int tx_empty; /* last TX empty bit */
100
101 spinlock_t conf_lock; /* shared data */
102 int conf_commit; /* need to make changes */
103 int conf; /* configuration for the MAX31000
104 * (bits 0-7, bits 8-11 are irqs) */
105 int rts_commit; /* need to change rts */
106 int rts; /* rts status */
107 int baud; /* current baud rate */
108
109 int parity; /* keeps track if we should send parity */
110#define MAX3100_PARITY_ON 1
111#define MAX3100_PARITY_ODD 2
112#define MAX3100_7BIT 4
113 int rx_enabled; /* if we should rx chars */
114
115 int irq; /* irq assigned to the max3100 */
116
117 int minor; /* minor number */
118 int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
119 int loopback; /* 1 if we are in loopback mode */
120
121 /* for handling irqs: need workqueue since we do spi_sync */
122 struct workqueue_struct *workqueue;
123 struct work_struct work;
124 /* set to 1 to make the workhandler exit as soon as possible */
125 int force_end_work;
126 /* need to know we are suspending to avoid deadlock on workqueue */
127 int suspending;
128
129 /* hook for suspending MAX3100 via dedicated pin */
130 void (*max3100_hw_suspend) (int suspend);
131
132 /* poll time (in ms) for ctrl lines */
133 int poll_time;
134 /* and its timer */
135 struct timer_list timer;
136};
137
138static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
139static DEFINE_MUTEX(max3100s_lock); /* race on probe */
140
141static int max3100_do_parity(struct max3100_port *s, u16 c)
142{
143 int parity;
144
145 if (s->parity & MAX3100_PARITY_ODD)
146 parity = 1;
147 else
148 parity = 0;
149
150 if (s->parity & MAX3100_7BIT)
151 c &= 0x7f;
152 else
153 c &= 0xff;
154
155 parity = parity ^ (hweight8(c) & 1);
156 return parity;
157}
158
159static int max3100_check_parity(struct max3100_port *s, u16 c)
160{
161 return max3100_do_parity(s, c) == ((c >> 8) & 1);
162}
163
164static void max3100_calc_parity(struct max3100_port *s, u16 *c)
165{
166 if (s->parity & MAX3100_7BIT)
167 *c &= 0x7f;
168 else
169 *c &= 0xff;
170
171 if (s->parity & MAX3100_PARITY_ON)
172 *c |= max3100_do_parity(s, *c) << 8;
173}
174
175static void max3100_work(struct work_struct *w);
176
177static void max3100_dowork(struct max3100_port *s)
178{
179 if (!s->force_end_work && !work_pending(&s->work) &&
180 !freezing(current) && !s->suspending)
181 queue_work(s->workqueue, &s->work);
182}
183
184static void max3100_timeout(unsigned long data)
185{
186 struct max3100_port *s = (struct max3100_port *)data;
187
188 if (s->port.state) {
189 max3100_dowork(s);
190 mod_timer(&s->timer, jiffies + s->poll_time);
191 }
192}
193
194static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
195{
196 struct spi_message message;
197 u16 etx, erx;
198 int status;
199 struct spi_transfer tran = {
200 .tx_buf = &etx,
201 .rx_buf = &erx,
202 .len = 2,
203 };
204
205 etx = cpu_to_be16(tx);
206 spi_message_init(&message);
207 spi_message_add_tail(&tran, &message);
208 status = spi_sync(s->spi, &message);
209 if (status) {
210 dev_warn(&s->spi->dev, "error while calling spi_sync\n");
211 return -EIO;
212 }
213 *rx = be16_to_cpu(erx);
214 s->tx_empty = (*rx & MAX3100_T) > 0;
215 dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
216 return 0;
217}
218
219static int max3100_handlerx(struct max3100_port *s, u16 rx)
220{
221 unsigned int ch, flg, status = 0;
222 int ret = 0, cts;
223
224 if (rx & MAX3100_R && s->rx_enabled) {
225 dev_dbg(&s->spi->dev, "%s\n", __func__);
226 ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
227 if (rx & MAX3100_RAFE) {
228 s->port.icount.frame++;
229 flg = TTY_FRAME;
230 status |= MAX3100_STATUS_FE;
231 } else {
232 if (s->parity & MAX3100_PARITY_ON) {
233 if (max3100_check_parity(s, rx)) {
234 s->port.icount.rx++;
235 flg = TTY_NORMAL;
236 } else {
237 s->port.icount.parity++;
238 flg = TTY_PARITY;
239 status |= MAX3100_STATUS_PE;
240 }
241 } else {
242 s->port.icount.rx++;
243 flg = TTY_NORMAL;
244 }
245 }
246 uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
247 ret = 1;
248 }
249
250 cts = (rx & MAX3100_CTS) > 0;
251 if (s->cts != cts) {
252 s->cts = cts;
253 uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
254 }
255
256 return ret;
257}
258
259static void max3100_work(struct work_struct *w)
260{
261 struct max3100_port *s = container_of(w, struct max3100_port, work);
262 int rxchars;
263 u16 tx, rx;
264 int conf, cconf, rts, crts;
265 struct circ_buf *xmit = &s->port.state->xmit;
266
267 dev_dbg(&s->spi->dev, "%s\n", __func__);
268
269 rxchars = 0;
270 do {
271 spin_lock(&s->conf_lock);
272 conf = s->conf;
273 cconf = s->conf_commit;
274 s->conf_commit = 0;
275 rts = s->rts;
276 crts = s->rts_commit;
277 s->rts_commit = 0;
278 spin_unlock(&s->conf_lock);
279 if (cconf)
280 max3100_sr(s, MAX3100_WC | conf, &rx);
281 if (crts) {
282 max3100_sr(s, MAX3100_WD | MAX3100_TE |
283 (s->rts ? MAX3100_RTS : 0), &rx);
284 rxchars += max3100_handlerx(s, rx);
285 }
286
287 max3100_sr(s, MAX3100_RD, &rx);
288 rxchars += max3100_handlerx(s, rx);
289
290 if (rx & MAX3100_T) {
291 tx = 0xffff;
292 if (s->port.x_char) {
293 tx = s->port.x_char;
294 s->port.icount.tx++;
295 s->port.x_char = 0;
296 } else if (!uart_circ_empty(xmit) &&
297 !uart_tx_stopped(&s->port)) {
298 tx = xmit->buf[xmit->tail];
299 xmit->tail = (xmit->tail + 1) &
300 (UART_XMIT_SIZE - 1);
301 s->port.icount.tx++;
302 }
303 if (tx != 0xffff) {
304 max3100_calc_parity(s, &tx);
305 tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
306 max3100_sr(s, tx, &rx);
307 rxchars += max3100_handlerx(s, rx);
308 }
309 }
310
311 if (rxchars > 16 && s->port.state->port.tty != NULL) {
312 tty_flip_buffer_push(s->port.state->port.tty);
313 rxchars = 0;
314 }
315 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
316 uart_write_wakeup(&s->port);
317
318 } while (!s->force_end_work &&
319 !freezing(current) &&
320 ((rx & MAX3100_R) ||
321 (!uart_circ_empty(xmit) &&
322 !uart_tx_stopped(&s->port))));
323
324 if (rxchars > 0 && s->port.state->port.tty != NULL)
325 tty_flip_buffer_push(s->port.state->port.tty);
326}
327
328static irqreturn_t max3100_irq(int irqno, void *dev_id)
329{
330 struct max3100_port *s = dev_id;
331
332 dev_dbg(&s->spi->dev, "%s\n", __func__);
333
334 max3100_dowork(s);
335 return IRQ_HANDLED;
336}
337
338static void max3100_enable_ms(struct uart_port *port)
339{
340 struct max3100_port *s = container_of(port,
341 struct max3100_port,
342 port);
343
344 if (s->poll_time > 0)
345 mod_timer(&s->timer, jiffies);
346 dev_dbg(&s->spi->dev, "%s\n", __func__);
347}
348
349static void max3100_start_tx(struct uart_port *port)
350{
351 struct max3100_port *s = container_of(port,
352 struct max3100_port,
353 port);
354
355 dev_dbg(&s->spi->dev, "%s\n", __func__);
356
357 max3100_dowork(s);
358}
359
360static void max3100_stop_rx(struct uart_port *port)
361{
362 struct max3100_port *s = container_of(port,
363 struct max3100_port,
364 port);
365
366 dev_dbg(&s->spi->dev, "%s\n", __func__);
367
368 s->rx_enabled = 0;
369 spin_lock(&s->conf_lock);
370 s->conf &= ~MAX3100_RM;
371 s->conf_commit = 1;
372 spin_unlock(&s->conf_lock);
373 max3100_dowork(s);
374}
375
376static unsigned int max3100_tx_empty(struct uart_port *port)
377{
378 struct max3100_port *s = container_of(port,
379 struct max3100_port,
380 port);
381
382 dev_dbg(&s->spi->dev, "%s\n", __func__);
383
384 /* may not be truly up-to-date */
385 max3100_dowork(s);
386 return s->tx_empty;
387}
388
389static unsigned int max3100_get_mctrl(struct uart_port *port)
390{
391 struct max3100_port *s = container_of(port,
392 struct max3100_port,
393 port);
394
395 dev_dbg(&s->spi->dev, "%s\n", __func__);
396
397 /* may not be truly up-to-date */
398 max3100_dowork(s);
399 /* always assert DCD and DSR since these lines are not wired */
400 return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
401}
402
403static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
404{
405 struct max3100_port *s = container_of(port,
406 struct max3100_port,
407 port);
408 int rts;
409
410 dev_dbg(&s->spi->dev, "%s\n", __func__);
411
412 rts = (mctrl & TIOCM_RTS) > 0;
413
414 spin_lock(&s->conf_lock);
415 if (s->rts != rts) {
416 s->rts = rts;
417 s->rts_commit = 1;
418 max3100_dowork(s);
419 }
420 spin_unlock(&s->conf_lock);
421}
422
423static void
424max3100_set_termios(struct uart_port *port, struct ktermios *termios,
425 struct ktermios *old)
426{
427 struct max3100_port *s = container_of(port,
428 struct max3100_port,
429 port);
430 int baud = 0;
431 unsigned cflag;
432 u32 param_new, param_mask, parity = 0;
433
434 dev_dbg(&s->spi->dev, "%s\n", __func__);
435
436 cflag = termios->c_cflag;
437 param_new = 0;
438 param_mask = 0;
439
440 baud = tty_termios_baud_rate(termios);
441 param_new = s->conf & MAX3100_BAUD;
442 switch (baud) {
443 case 300:
444 if (s->crystal)
445 baud = s->baud;
446 else
447 param_new = 15;
448 break;
449 case 600:
450 param_new = 14 + s->crystal;
451 break;
452 case 1200:
453 param_new = 13 + s->crystal;
454 break;
455 case 2400:
456 param_new = 12 + s->crystal;
457 break;
458 case 4800:
459 param_new = 11 + s->crystal;
460 break;
461 case 9600:
462 param_new = 10 + s->crystal;
463 break;
464 case 19200:
465 param_new = 9 + s->crystal;
466 break;
467 case 38400:
468 param_new = 8 + s->crystal;
469 break;
470 case 57600:
471 param_new = 1 + s->crystal;
472 break;
473 case 115200:
474 param_new = 0 + s->crystal;
475 break;
476 case 230400:
477 if (s->crystal)
478 param_new = 0;
479 else
480 baud = s->baud;
481 break;
482 default:
483 baud = s->baud;
484 }
485 tty_termios_encode_baud_rate(termios, baud, baud);
486 s->baud = baud;
487 param_mask |= MAX3100_BAUD;
488
489 if ((cflag & CSIZE) == CS8) {
490 param_new &= ~MAX3100_L;
491 parity &= ~MAX3100_7BIT;
492 } else {
493 param_new |= MAX3100_L;
494 parity |= MAX3100_7BIT;
495 cflag = (cflag & ~CSIZE) | CS7;
496 }
497 param_mask |= MAX3100_L;
498
499 if (cflag & CSTOPB)
500 param_new |= MAX3100_ST;
501 else
502 param_new &= ~MAX3100_ST;
503 param_mask |= MAX3100_ST;
504
505 if (cflag & PARENB) {
506 param_new |= MAX3100_PE;
507 parity |= MAX3100_PARITY_ON;
508 } else {
509 param_new &= ~MAX3100_PE;
510 parity &= ~MAX3100_PARITY_ON;
511 }
512 param_mask |= MAX3100_PE;
513
514 if (cflag & PARODD)
515 parity |= MAX3100_PARITY_ODD;
516 else
517 parity &= ~MAX3100_PARITY_ODD;
518
519 /* mask termios capabilities we don't support */
520 cflag &= ~CMSPAR;
521 termios->c_cflag = cflag;
522
523 s->port.ignore_status_mask = 0;
524 if (termios->c_iflag & IGNPAR)
525 s->port.ignore_status_mask |=
526 MAX3100_STATUS_PE | MAX3100_STATUS_FE |
527 MAX3100_STATUS_OE;
528
529 /* we are sending char from a workqueue so enable */
530 s->port.state->port.tty->low_latency = 1;
531
532 if (s->poll_time > 0)
533 del_timer_sync(&s->timer);
534
535 uart_update_timeout(port, termios->c_cflag, baud);
536
537 spin_lock(&s->conf_lock);
538 s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
539 s->conf_commit = 1;
540 s->parity = parity;
541 spin_unlock(&s->conf_lock);
542 max3100_dowork(s);
543
544 if (UART_ENABLE_MS(&s->port, termios->c_cflag))
545 max3100_enable_ms(&s->port);
546}
547
548static void max3100_shutdown(struct uart_port *port)
549{
550 struct max3100_port *s = container_of(port,
551 struct max3100_port,
552 port);
553
554 dev_dbg(&s->spi->dev, "%s\n", __func__);
555
556 if (s->suspending)
557 return;
558
559 s->force_end_work = 1;
560
561 if (s->poll_time > 0)
562 del_timer_sync(&s->timer);
563
564 if (s->workqueue) {
565 flush_workqueue(s->workqueue);
566 destroy_workqueue(s->workqueue);
567 s->workqueue = NULL;
568 }
569 if (s->irq)
570 free_irq(s->irq, s);
571
572 /* set shutdown mode to save power */
573 if (s->max3100_hw_suspend)
574 s->max3100_hw_suspend(1);
575 else {
576 u16 tx, rx;
577
578 tx = MAX3100_WC | MAX3100_SHDN;
579 max3100_sr(s, tx, &rx);
580 }
581}
582
583static int max3100_startup(struct uart_port *port)
584{
585 struct max3100_port *s = container_of(port,
586 struct max3100_port,
587 port);
588 char b[12];
589
590 dev_dbg(&s->spi->dev, "%s\n", __func__);
591
592 s->conf = MAX3100_RM;
593 s->baud = s->crystal ? 230400 : 115200;
594 s->rx_enabled = 1;
595
596 if (s->suspending)
597 return 0;
598
599 s->force_end_work = 0;
600 s->parity = 0;
601 s->rts = 0;
602
603 sprintf(b, "max3100-%d", s->minor);
604 s->workqueue = create_freezable_workqueue(b);
605 if (!s->workqueue) {
606 dev_warn(&s->spi->dev, "cannot create workqueue\n");
607 return -EBUSY;
608 }
609 INIT_WORK(&s->work, max3100_work);
610
611 if (request_irq(s->irq, max3100_irq,
612 IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
613 dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
614 s->irq = 0;
615 destroy_workqueue(s->workqueue);
616 s->workqueue = NULL;
617 return -EBUSY;
618 }
619
620 if (s->loopback) {
621 u16 tx, rx;
622 tx = 0x4001;
623 max3100_sr(s, tx, &rx);
624 }
625
626 if (s->max3100_hw_suspend)
627 s->max3100_hw_suspend(0);
628 s->conf_commit = 1;
629 max3100_dowork(s);
630 /* wait for clock to settle */
631 msleep(50);
632
633 max3100_enable_ms(&s->port);
634
635 return 0;
636}
637
638static const char *max3100_type(struct uart_port *port)
639{
640 struct max3100_port *s = container_of(port,
641 struct max3100_port,
642 port);
643
644 dev_dbg(&s->spi->dev, "%s\n", __func__);
645
646 return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
647}
648
649static void max3100_release_port(struct uart_port *port)
650{
651 struct max3100_port *s = container_of(port,
652 struct max3100_port,
653 port);
654
655 dev_dbg(&s->spi->dev, "%s\n", __func__);
656}
657
658static void max3100_config_port(struct uart_port *port, int flags)
659{
660 struct max3100_port *s = container_of(port,
661 struct max3100_port,
662 port);
663
664 dev_dbg(&s->spi->dev, "%s\n", __func__);
665
666 if (flags & UART_CONFIG_TYPE)
667 s->port.type = PORT_MAX3100;
668}
669
670static int max3100_verify_port(struct uart_port *port,
671 struct serial_struct *ser)
672{
673 struct max3100_port *s = container_of(port,
674 struct max3100_port,
675 port);
676 int ret = -EINVAL;
677
678 dev_dbg(&s->spi->dev, "%s\n", __func__);
679
680 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
681 ret = 0;
682 return ret;
683}
684
685static void max3100_stop_tx(struct uart_port *port)
686{
687 struct max3100_port *s = container_of(port,
688 struct max3100_port,
689 port);
690
691 dev_dbg(&s->spi->dev, "%s\n", __func__);
692}
693
694static int max3100_request_port(struct uart_port *port)
695{
696 struct max3100_port *s = container_of(port,
697 struct max3100_port,
698 port);
699
700 dev_dbg(&s->spi->dev, "%s\n", __func__);
701 return 0;
702}
703
704static void max3100_break_ctl(struct uart_port *port, int break_state)
705{
706 struct max3100_port *s = container_of(port,
707 struct max3100_port,
708 port);
709
710 dev_dbg(&s->spi->dev, "%s\n", __func__);
711}
712
713static struct uart_ops max3100_ops = {
714 .tx_empty = max3100_tx_empty,
715 .set_mctrl = max3100_set_mctrl,
716 .get_mctrl = max3100_get_mctrl,
717 .stop_tx = max3100_stop_tx,
718 .start_tx = max3100_start_tx,
719 .stop_rx = max3100_stop_rx,
720 .enable_ms = max3100_enable_ms,
721 .break_ctl = max3100_break_ctl,
722 .startup = max3100_startup,
723 .shutdown = max3100_shutdown,
724 .set_termios = max3100_set_termios,
725 .type = max3100_type,
726 .release_port = max3100_release_port,
727 .request_port = max3100_request_port,
728 .config_port = max3100_config_port,
729 .verify_port = max3100_verify_port,
730};
731
732static struct uart_driver max3100_uart_driver = {
733 .owner = THIS_MODULE,
734 .driver_name = "ttyMAX",
735 .dev_name = "ttyMAX",
736 .major = MAX3100_MAJOR,
737 .minor = MAX3100_MINOR,
738 .nr = MAX_MAX3100,
739};
740static int uart_driver_registered;
741
742static int __devinit max3100_probe(struct spi_device *spi)
743{
744 int i, retval;
745 struct plat_max3100 *pdata;
746 u16 tx, rx;
747
748 mutex_lock(&max3100s_lock);
749
750 if (!uart_driver_registered) {
751 uart_driver_registered = 1;
752 retval = uart_register_driver(&max3100_uart_driver);
753 if (retval) {
754 printk(KERN_ERR "Couldn't register max3100 uart driver\n");
755 mutex_unlock(&max3100s_lock);
756 return retval;
757 }
758 }
759
760 for (i = 0; i < MAX_MAX3100; i++)
761 if (!max3100s[i])
762 break;
763 if (i == MAX_MAX3100) {
764 dev_warn(&spi->dev, "too many MAX3100 chips\n");
765 mutex_unlock(&max3100s_lock);
766 return -ENOMEM;
767 }
768
769 max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
770 if (!max3100s[i]) {
771 dev_warn(&spi->dev,
772 "kmalloc for max3100 structure %d failed!\n", i);
773 mutex_unlock(&max3100s_lock);
774 return -ENOMEM;
775 }
776 max3100s[i]->spi = spi;
777 max3100s[i]->irq = spi->irq;
778 spin_lock_init(&max3100s[i]->conf_lock);
779 dev_set_drvdata(&spi->dev, max3100s[i]);
780 pdata = spi->dev.platform_data;
781 max3100s[i]->crystal = pdata->crystal;
782 max3100s[i]->loopback = pdata->loopback;
783 max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
784 if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
785 max3100s[i]->poll_time = 1;
786 max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
787 max3100s[i]->minor = i;
788 init_timer(&max3100s[i]->timer);
789 max3100s[i]->timer.function = max3100_timeout;
790 max3100s[i]->timer.data = (unsigned long) max3100s[i];
791
792 dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
793 max3100s[i]->port.irq = max3100s[i]->irq;
794 max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
795 max3100s[i]->port.fifosize = 16;
796 max3100s[i]->port.ops = &max3100_ops;
797 max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
798 max3100s[i]->port.line = i;
799 max3100s[i]->port.type = PORT_MAX3100;
800 max3100s[i]->port.dev = &spi->dev;
801 retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
802 if (retval < 0)
803 dev_warn(&spi->dev,
804 "uart_add_one_port failed for line %d with error %d\n",
805 i, retval);
806
807 /* set shutdown mode to save power. Will be woken-up on open */
808 if (max3100s[i]->max3100_hw_suspend)
809 max3100s[i]->max3100_hw_suspend(1);
810 else {
811 tx = MAX3100_WC | MAX3100_SHDN;
812 max3100_sr(max3100s[i], tx, &rx);
813 }
814 mutex_unlock(&max3100s_lock);
815 return 0;
816}
817
818static int __devexit max3100_remove(struct spi_device *spi)
819{
820 struct max3100_port *s = dev_get_drvdata(&spi->dev);
821 int i;
822
823 mutex_lock(&max3100s_lock);
824
825 /* find out the index for the chip we are removing */
826 for (i = 0; i < MAX_MAX3100; i++)
827 if (max3100s[i] == s)
828 break;
829
830 dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
831 uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
832 kfree(max3100s[i]);
833 max3100s[i] = NULL;
834
835 /* check if this is the last chip we have */
836 for (i = 0; i < MAX_MAX3100; i++)
837 if (max3100s[i]) {
838 mutex_unlock(&max3100s_lock);
839 return 0;
840 }
841 pr_debug("removing max3100 driver\n");
842 uart_unregister_driver(&max3100_uart_driver);
843
844 mutex_unlock(&max3100s_lock);
845 return 0;
846}
847
848#ifdef CONFIG_PM
849
850static int max3100_suspend(struct spi_device *spi, pm_message_t state)
851{
852 struct max3100_port *s = dev_get_drvdata(&spi->dev);
853
854 dev_dbg(&s->spi->dev, "%s\n", __func__);
855
856 disable_irq(s->irq);
857
858 s->suspending = 1;
859 uart_suspend_port(&max3100_uart_driver, &s->port);
860
861 if (s->max3100_hw_suspend)
862 s->max3100_hw_suspend(1);
863 else {
864 /* no HW suspend, so do SW one */
865 u16 tx, rx;
866
867 tx = MAX3100_WC | MAX3100_SHDN;
868 max3100_sr(s, tx, &rx);
869 }
870 return 0;
871}
872
873static int max3100_resume(struct spi_device *spi)
874{
875 struct max3100_port *s = dev_get_drvdata(&spi->dev);
876
877 dev_dbg(&s->spi->dev, "%s\n", __func__);
878
879 if (s->max3100_hw_suspend)
880 s->max3100_hw_suspend(0);
881 uart_resume_port(&max3100_uart_driver, &s->port);
882 s->suspending = 0;
883
884 enable_irq(s->irq);
885
886 s->conf_commit = 1;
887 if (s->workqueue)
888 max3100_dowork(s);
889
890 return 0;
891}
892
893#else
894#define max3100_suspend NULL
895#define max3100_resume NULL
896#endif
897
898static struct spi_driver max3100_driver = {
899 .driver = {
900 .name = "max3100",
901 .bus = &spi_bus_type,
902 .owner = THIS_MODULE,
903 },
904
905 .probe = max3100_probe,
906 .remove = __devexit_p(max3100_remove),
907 .suspend = max3100_suspend,
908 .resume = max3100_resume,
909};
910
911static int __init max3100_init(void)
912{
913 return spi_register_driver(&max3100_driver);
914}
915module_init(max3100_init);
916
917static void __exit max3100_exit(void)
918{
919 spi_unregister_driver(&max3100_driver);
920}
921module_exit(max3100_exit);
922
923MODULE_DESCRIPTION("MAX3100 driver");
924MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
925MODULE_LICENSE("GPL");
926MODULE_ALIAS("spi:max3100");
diff --git a/drivers/tty/serial/max3107-aava.c b/drivers/tty/serial/max3107-aava.c
new file mode 100644
index 000000000000..a1fe304f2f52
--- /dev/null
+++ b/drivers/tty/serial/max3107-aava.c
@@ -0,0 +1,344 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/spi/spi.h>
35#include <linux/freezer.h>
36#include <linux/platform_device.h>
37#include <linux/gpio.h>
38#include <linux/sfi.h>
39#include <asm/mrst.h>
40#include "max3107.h"
41
42/* GPIO direction to input function */
43static int max3107_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
44{
45 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
46 u16 buf[1]; /* Buffer for SPI transfer */
47
48 if (offset >= MAX3107_GPIO_COUNT) {
49 dev_err(&s->spi->dev, "Invalid GPIO\n");
50 return -EINVAL;
51 }
52
53 /* Read current GPIO configuration register */
54 buf[0] = MAX3107_GPIOCFG_REG;
55 /* Perform SPI transfer */
56 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
57 dev_err(&s->spi->dev, "SPI transfer GPIO read failed\n");
58 return -EIO;
59 }
60 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
61
62 /* Set GPIO to input */
63 buf[0] &= ~(0x0001 << offset);
64
65 /* Write new GPIO configuration register value */
66 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
67 /* Perform SPI transfer */
68 if (max3107_rw(s, (u8 *)buf, NULL, 2)) {
69 dev_err(&s->spi->dev, "SPI transfer GPIO write failed\n");
70 return -EIO;
71 }
72 return 0;
73}
74
75/* GPIO direction to output function */
76static int max3107_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
77 int value)
78{
79 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
80 u16 buf[2]; /* Buffer for SPI transfers */
81
82 if (offset >= MAX3107_GPIO_COUNT) {
83 dev_err(&s->spi->dev, "Invalid GPIO\n");
84 return -EINVAL;
85 }
86
87 /* Read current GPIO configuration and data registers */
88 buf[0] = MAX3107_GPIOCFG_REG;
89 buf[1] = MAX3107_GPIODATA_REG;
90 /* Perform SPI transfer */
91 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
92 dev_err(&s->spi->dev, "SPI transfer gpio failed\n");
93 return -EIO;
94 }
95 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
96 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
97
98 /* Set GPIO to output */
99 buf[0] |= (0x0001 << offset);
100 /* Set value */
101 if (value)
102 buf[1] |= (0x0001 << offset);
103 else
104 buf[1] &= ~(0x0001 << offset);
105
106 /* Write new GPIO configuration and data register values */
107 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIOCFG_REG);
108 buf[1] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
109 /* Perform SPI transfer */
110 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
111 dev_err(&s->spi->dev,
112 "SPI transfer for GPIO conf data w failed\n");
113 return -EIO;
114 }
115 return 0;
116}
117
118/* GPIO value query function */
119static int max3107_gpio_get(struct gpio_chip *chip, unsigned offset)
120{
121 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
122 u16 buf[1]; /* Buffer for SPI transfer */
123
124 if (offset >= MAX3107_GPIO_COUNT) {
125 dev_err(&s->spi->dev, "Invalid GPIO\n");
126 return -EINVAL;
127 }
128
129 /* Read current GPIO data register */
130 buf[0] = MAX3107_GPIODATA_REG;
131 /* Perform SPI transfer */
132 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
133 dev_err(&s->spi->dev, "SPI transfer GPIO data r failed\n");
134 return -EIO;
135 }
136 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
137
138 /* Return value */
139 return buf[0] & (0x0001 << offset);
140}
141
142/* GPIO value set function */
143static void max3107_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
144{
145 struct max3107_port *s = container_of(chip, struct max3107_port, chip);
146 u16 buf[2]; /* Buffer for SPI transfers */
147
148 if (offset >= MAX3107_GPIO_COUNT) {
149 dev_err(&s->spi->dev, "Invalid GPIO\n");
150 return;
151 }
152
153 /* Read current GPIO configuration registers*/
154 buf[0] = MAX3107_GPIODATA_REG;
155 buf[1] = MAX3107_GPIOCFG_REG;
156 /* Perform SPI transfer */
157 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 4)) {
158 dev_err(&s->spi->dev,
159 "SPI transfer for GPIO data and config read failed\n");
160 return;
161 }
162 buf[0] &= MAX3107_SPI_RX_DATA_MASK;
163 buf[1] &= MAX3107_SPI_RX_DATA_MASK;
164
165 if (!(buf[1] & (0x0001 << offset))) {
166 /* Configured as input, can't set value */
167 dev_warn(&s->spi->dev,
168 "Trying to set value for input GPIO\n");
169 return;
170 }
171
172 /* Set value */
173 if (value)
174 buf[0] |= (0x0001 << offset);
175 else
176 buf[0] &= ~(0x0001 << offset);
177
178 /* Write new GPIO data register value */
179 buf[0] |= (MAX3107_WRITE_BIT | MAX3107_GPIODATA_REG);
180 /* Perform SPI transfer */
181 if (max3107_rw(s, (u8 *)buf, NULL, 2))
182 dev_err(&s->spi->dev, "SPI transfer GPIO data w failed\n");
183}
184
185/* GPIO chip data */
186static struct gpio_chip max3107_gpio_chip = {
187 .owner = THIS_MODULE,
188 .direction_input = max3107_gpio_direction_in,
189 .direction_output = max3107_gpio_direction_out,
190 .get = max3107_gpio_get,
191 .set = max3107_gpio_set,
192 .can_sleep = 1,
193 .base = MAX3107_GPIO_BASE,
194 .ngpio = MAX3107_GPIO_COUNT,
195};
196
197/**
198 * max3107_aava_reset - reset on AAVA systems
199 * @spi: The SPI device we are probing
200 *
201 * Reset the device ready for probing.
202 */
203
204static int max3107_aava_reset(struct spi_device *spi)
205{
206 /* Reset the chip */
207 if (gpio_request(MAX3107_RESET_GPIO, "max3107")) {
208 pr_err("Requesting RESET GPIO failed\n");
209 return -EIO;
210 }
211 if (gpio_direction_output(MAX3107_RESET_GPIO, 0)) {
212 pr_err("Setting RESET GPIO to 0 failed\n");
213 gpio_free(MAX3107_RESET_GPIO);
214 return -EIO;
215 }
216 msleep(MAX3107_RESET_DELAY);
217 if (gpio_direction_output(MAX3107_RESET_GPIO, 1)) {
218 pr_err("Setting RESET GPIO to 1 failed\n");
219 gpio_free(MAX3107_RESET_GPIO);
220 return -EIO;
221 }
222 gpio_free(MAX3107_RESET_GPIO);
223 msleep(MAX3107_WAKEUP_DELAY);
224 return 0;
225}
226
227static int max3107_aava_configure(struct max3107_port *s)
228{
229 int retval;
230
231 /* Initialize GPIO chip data */
232 s->chip = max3107_gpio_chip;
233 s->chip.label = s->spi->modalias;
234 s->chip.dev = &s->spi->dev;
235
236 /* Add GPIO chip */
237 retval = gpiochip_add(&s->chip);
238 if (retval) {
239 dev_err(&s->spi->dev, "Adding GPIO chip failed\n");
240 return retval;
241 }
242
243 /* Temporary fix for EV2 boot problems, set modem reset to 0 */
244 max3107_gpio_direction_out(&s->chip, 3, 0);
245 return 0;
246}
247
248#if 0
249/* This will get enabled once we have the board stuff merged for this
250 specific case */
251
252static const struct baud_table brg13_ext[] = {
253 { 300, MAX3107_BRG13_B300 },
254 { 600, MAX3107_BRG13_B600 },
255 { 1200, MAX3107_BRG13_B1200 },
256 { 2400, MAX3107_BRG13_B2400 },
257 { 4800, MAX3107_BRG13_B4800 },
258 { 9600, MAX3107_BRG13_B9600 },
259 { 19200, MAX3107_BRG13_B19200 },
260 { 57600, MAX3107_BRG13_B57600 },
261 { 115200, MAX3107_BRG13_B115200 },
262 { 230400, MAX3107_BRG13_B230400 },
263 { 460800, MAX3107_BRG13_B460800 },
264 { 921600, MAX3107_BRG13_B921600 },
265 { 0, 0 }
266};
267
268static void max3107_aava_init(struct max3107_port *s)
269{
270 /*override for AAVA SC specific*/
271 if (mrst_platform_id() == MRST_PLATFORM_AAVA_SC) {
272 if (get_koski_build_id() <= KOSKI_EV2)
273 if (s->ext_clk) {
274 s->brg_cfg = MAX3107_BRG13_B9600;
275 s->baud_tbl = (struct baud_table *)brg13_ext;
276 }
277 }
278}
279#endif
280
281static int __devexit max3107_aava_remove(struct spi_device *spi)
282{
283 struct max3107_port *s = dev_get_drvdata(&spi->dev);
284
285 /* Remove GPIO chip */
286 if (gpiochip_remove(&s->chip))
287 dev_warn(&spi->dev, "Removing GPIO chip failed\n");
288
289 /* Then do the default remove */
290 return max3107_remove(spi);
291}
292
293/* Platform data */
294static struct max3107_plat aava_plat_data = {
295 .loopback = 0,
296 .ext_clk = 1,
297/* .init = max3107_aava_init, */
298 .configure = max3107_aava_configure,
299 .hw_suspend = max3107_hw_susp,
300 .polled_mode = 0,
301 .poll_time = 0,
302};
303
304
305static int __devinit max3107_probe_aava(struct spi_device *spi)
306{
307 int err = max3107_aava_reset(spi);
308 if (err < 0)
309 return err;
310 return max3107_probe(spi, &aava_plat_data);
311}
312
313/* Spi driver data */
314static struct spi_driver max3107_driver = {
315 .driver = {
316 .name = "aava-max3107",
317 .bus = &spi_bus_type,
318 .owner = THIS_MODULE,
319 },
320 .probe = max3107_probe_aava,
321 .remove = __devexit_p(max3107_aava_remove),
322 .suspend = max3107_suspend,
323 .resume = max3107_resume,
324};
325
326/* Driver init function */
327static int __init max3107_init(void)
328{
329 return spi_register_driver(&max3107_driver);
330}
331
332/* Driver exit function */
333static void __exit max3107_exit(void)
334{
335 spi_unregister_driver(&max3107_driver);
336}
337
338module_init(max3107_init);
339module_exit(max3107_exit);
340
341MODULE_DESCRIPTION("MAX3107 driver");
342MODULE_AUTHOR("Aavamobile");
343MODULE_ALIAS("aava-max3107-spi");
344MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/max3107.c b/drivers/tty/serial/max3107.c
new file mode 100644
index 000000000000..750b4f627315
--- /dev/null
+++ b/drivers/tty/serial/max3107.c
@@ -0,0 +1,1213 @@
1/*
2 * max3107.c - spi uart protocol driver for Maxim 3107
3 * Based on max3100.c
4 * by Christian Pellegrin <chripell@evolware.org>
5 * and max3110.c
6 * by Feng Tang <feng.tang@intel.com>
7 *
8 * Copyright (C) Aavamobile 2009
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/gpio.h>
35#include <linux/spi/spi.h>
36#include <linux/freezer.h>
37#include "max3107.h"
38
39static const struct baud_table brg26_ext[] = {
40 { 300, MAX3107_BRG26_B300 },
41 { 600, MAX3107_BRG26_B600 },
42 { 1200, MAX3107_BRG26_B1200 },
43 { 2400, MAX3107_BRG26_B2400 },
44 { 4800, MAX3107_BRG26_B4800 },
45 { 9600, MAX3107_BRG26_B9600 },
46 { 19200, MAX3107_BRG26_B19200 },
47 { 57600, MAX3107_BRG26_B57600 },
48 { 115200, MAX3107_BRG26_B115200 },
49 { 230400, MAX3107_BRG26_B230400 },
50 { 460800, MAX3107_BRG26_B460800 },
51 { 921600, MAX3107_BRG26_B921600 },
52 { 0, 0 }
53};
54
55static const struct baud_table brg13_int[] = {
56 { 300, MAX3107_BRG13_IB300 },
57 { 600, MAX3107_BRG13_IB600 },
58 { 1200, MAX3107_BRG13_IB1200 },
59 { 2400, MAX3107_BRG13_IB2400 },
60 { 4800, MAX3107_BRG13_IB4800 },
61 { 9600, MAX3107_BRG13_IB9600 },
62 { 19200, MAX3107_BRG13_IB19200 },
63 { 57600, MAX3107_BRG13_IB57600 },
64 { 115200, MAX3107_BRG13_IB115200 },
65 { 230400, MAX3107_BRG13_IB230400 },
66 { 460800, MAX3107_BRG13_IB460800 },
67 { 921600, MAX3107_BRG13_IB921600 },
68 { 0, 0 }
69};
70
71static u32 get_new_brg(int baud, struct max3107_port *s)
72{
73 int i;
74 const struct baud_table *baud_tbl = s->baud_tbl;
75
76 for (i = 0; i < 13; i++) {
77 if (baud == baud_tbl[i].baud)
78 return baud_tbl[i].new_brg;
79 }
80
81 return 0;
82}
83
84/* Perform SPI transfer for write/read of device register(s) */
85int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len)
86{
87 struct spi_message spi_msg;
88 struct spi_transfer spi_xfer;
89
90 /* Initialize SPI ,message */
91 spi_message_init(&spi_msg);
92
93 /* Initialize SPI transfer */
94 memset(&spi_xfer, 0, sizeof spi_xfer);
95 spi_xfer.len = len;
96 spi_xfer.tx_buf = tx;
97 spi_xfer.rx_buf = rx;
98 spi_xfer.speed_hz = MAX3107_SPI_SPEED;
99
100 /* Add SPI transfer to SPI message */
101 spi_message_add_tail(&spi_xfer, &spi_msg);
102
103#ifdef DBG_TRACE_SPI_DATA
104 {
105 int i;
106 pr_info("tx len %d:\n", spi_xfer.len);
107 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
108 pr_info(" %x", ((u8 *)spi_xfer.tx_buf)[i]);
109 pr_info("\n");
110 }
111#endif
112
113 /* Perform synchronous SPI transfer */
114 if (spi_sync(s->spi, &spi_msg)) {
115 dev_err(&s->spi->dev, "spi_sync failure\n");
116 return -EIO;
117 }
118
119#ifdef DBG_TRACE_SPI_DATA
120 if (spi_xfer.rx_buf) {
121 int i;
122 pr_info("rx len %d:\n", spi_xfer.len);
123 for (i = 0 ; i < spi_xfer.len && i < 32 ; i++)
124 pr_info(" %x", ((u8 *)spi_xfer.rx_buf)[i]);
125 pr_info("\n");
126 }
127#endif
128 return 0;
129}
130EXPORT_SYMBOL_GPL(max3107_rw);
131
132/* Puts received data to circular buffer */
133static void put_data_to_circ_buf(struct max3107_port *s, unsigned char *data,
134 int len)
135{
136 struct uart_port *port = &s->port;
137 struct tty_struct *tty;
138
139 if (!port->state)
140 return;
141
142 tty = port->state->port.tty;
143 if (!tty)
144 return;
145
146 /* Insert received data */
147 tty_insert_flip_string(tty, data, len);
148 /* Update RX counter */
149 port->icount.rx += len;
150}
151
152/* Handle data receiving */
153static void max3107_handlerx(struct max3107_port *s, u16 rxlvl)
154{
155 int i;
156 int j;
157 int len; /* SPI transfer buffer length */
158 u16 *buf;
159 u8 *valid_str;
160
161 if (!s->rx_enabled)
162 /* RX is disabled */
163 return;
164
165 if (rxlvl == 0) {
166 /* RX fifo is empty */
167 return;
168 } else if (rxlvl >= MAX3107_RX_FIFO_SIZE) {
169 dev_warn(&s->spi->dev, "Possible RX FIFO overrun %d\n", rxlvl);
170 /* Ensure sanity of RX level */
171 rxlvl = MAX3107_RX_FIFO_SIZE;
172 }
173 if ((s->rxbuf == 0) || (s->rxstr == 0)) {
174 dev_warn(&s->spi->dev, "Rx buffer/str isn't ready\n");
175 return;
176 }
177 buf = s->rxbuf;
178 valid_str = s->rxstr;
179 while (rxlvl) {
180 pr_debug("rxlvl %d\n", rxlvl);
181 /* Clear buffer */
182 memset(buf, 0, sizeof(u16) * (MAX3107_RX_FIFO_SIZE + 2));
183 len = 0;
184 if (s->irqen_reg & MAX3107_IRQ_RXFIFO_BIT) {
185 /* First disable RX FIFO interrupt */
186 pr_debug("Disabling RX INT\n");
187 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
188 s->irqen_reg &= ~MAX3107_IRQ_RXFIFO_BIT;
189 buf[0] |= s->irqen_reg;
190 len++;
191 }
192 /* Just increase the length by amount of words in FIFO since
193 * buffer was zeroed and SPI transfer of 0x0000 means reading
194 * from RX FIFO
195 */
196 len += rxlvl;
197 /* Append RX level query */
198 buf[len] = MAX3107_RXFIFOLVL_REG;
199 len++;
200
201 /* Perform the SPI transfer */
202 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, len * 2)) {
203 dev_err(&s->spi->dev, "SPI transfer for RX h failed\n");
204 return;
205 }
206
207 /* Skip RX FIFO interrupt disabling word if it was added */
208 j = ((len - 1) - rxlvl);
209 /* Read received words */
210 for (i = 0; i < rxlvl; i++, j++)
211 valid_str[i] = (u8)buf[j];
212 put_data_to_circ_buf(s, valid_str, rxlvl);
213 /* Get new RX level */
214 rxlvl = (buf[len - 1] & MAX3107_SPI_RX_DATA_MASK);
215 }
216
217 if (s->rx_enabled) {
218 /* RX still enabled, re-enable RX FIFO interrupt */
219 pr_debug("Enabling RX INT\n");
220 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
221 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
222 buf[0] |= s->irqen_reg;
223 if (max3107_rw(s, (u8 *)buf, NULL, 2))
224 dev_err(&s->spi->dev, "RX FIFO INT enabling failed\n");
225 }
226
227 /* Push the received data to receivers */
228 if (s->port.state->port.tty)
229 tty_flip_buffer_push(s->port.state->port.tty);
230}
231
232
233/* Handle data sending */
234static void max3107_handletx(struct max3107_port *s)
235{
236 struct circ_buf *xmit = &s->port.state->xmit;
237 int i;
238 unsigned long flags;
239 int len; /* SPI transfer buffer length */
240 u16 *buf;
241
242 if (!s->tx_fifo_empty)
243 /* Don't send more data before previous data is sent */
244 return;
245
246 if (uart_circ_empty(xmit) || uart_tx_stopped(&s->port))
247 /* No data to send or TX is stopped */
248 return;
249
250 if (!s->txbuf) {
251 dev_warn(&s->spi->dev, "Txbuf isn't ready\n");
252 return;
253 }
254 buf = s->txbuf;
255 /* Get length of data pending in circular buffer */
256 len = uart_circ_chars_pending(xmit);
257 if (len) {
258 /* Limit to size of TX FIFO */
259 if (len > MAX3107_TX_FIFO_SIZE)
260 len = MAX3107_TX_FIFO_SIZE;
261
262 pr_debug("txlen %d\n", len);
263
264 /* Update TX counter */
265 s->port.icount.tx += len;
266
267 /* TX FIFO will no longer be empty */
268 s->tx_fifo_empty = 0;
269
270 i = 0;
271 if (s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT) {
272 /* First disable TX empty interrupt */
273 pr_debug("Disabling TE INT\n");
274 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
275 s->irqen_reg &= ~MAX3107_IRQ_TXEMPTY_BIT;
276 buf[i] |= s->irqen_reg;
277 i++;
278 len++;
279 }
280 /* Add data to send */
281 spin_lock_irqsave(&s->port.lock, flags);
282 for ( ; i < len ; i++) {
283 buf[i] = (MAX3107_WRITE_BIT | MAX3107_THR_REG);
284 buf[i] |= ((u16)xmit->buf[xmit->tail] &
285 MAX3107_SPI_TX_DATA_MASK);
286 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
287 }
288 spin_unlock_irqrestore(&s->port.lock, flags);
289 if (!(s->irqen_reg & MAX3107_IRQ_TXEMPTY_BIT)) {
290 /* Enable TX empty interrupt */
291 pr_debug("Enabling TE INT\n");
292 buf[i] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG);
293 s->irqen_reg |= MAX3107_IRQ_TXEMPTY_BIT;
294 buf[i] |= s->irqen_reg;
295 i++;
296 len++;
297 }
298 if (!s->tx_enabled) {
299 /* Enable TX */
300 pr_debug("Enable TX\n");
301 buf[i] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
302 spin_lock_irqsave(&s->data_lock, flags);
303 s->mode1_reg &= ~MAX3107_MODE1_TXDIS_BIT;
304 buf[i] |= s->mode1_reg;
305 spin_unlock_irqrestore(&s->data_lock, flags);
306 s->tx_enabled = 1;
307 i++;
308 len++;
309 }
310
311 /* Perform the SPI transfer */
312 if (max3107_rw(s, (u8 *)buf, NULL, len*2)) {
313 dev_err(&s->spi->dev,
314 "SPI transfer TX handling failed\n");
315 return;
316 }
317 }
318
319 /* Indicate wake up if circular buffer is getting low on data */
320 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
321 uart_write_wakeup(&s->port);
322
323}
324
325/* Handle interrupts
326 * Also reads and returns current RX FIFO level
327 */
328static u16 handle_interrupt(struct max3107_port *s)
329{
330 u16 buf[4]; /* Buffer for SPI transfers */
331 u8 irq_status;
332 u16 rx_level;
333 unsigned long flags;
334
335 /* Read IRQ status register */
336 buf[0] = MAX3107_IRQSTS_REG;
337 /* Read status IRQ status register */
338 buf[1] = MAX3107_STS_IRQSTS_REG;
339 /* Read LSR IRQ status register */
340 buf[2] = MAX3107_LSR_IRQSTS_REG;
341 /* Query RX level */
342 buf[3] = MAX3107_RXFIFOLVL_REG;
343
344 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 8)) {
345 dev_err(&s->spi->dev,
346 "SPI transfer for INTR handling failed\n");
347 return 0;
348 }
349
350 irq_status = (u8)buf[0];
351 pr_debug("IRQSTS %x\n", irq_status);
352 rx_level = (buf[3] & MAX3107_SPI_RX_DATA_MASK);
353
354 if (irq_status & MAX3107_IRQ_LSR_BIT) {
355 /* LSR interrupt */
356 if (buf[2] & MAX3107_LSR_RXTO_BIT)
357 /* RX timeout interrupt,
358 * handled by normal RX handling
359 */
360 pr_debug("RX TO INT\n");
361 }
362
363 if (irq_status & MAX3107_IRQ_TXEMPTY_BIT) {
364 /* Tx empty interrupt,
365 * disable TX and set tx_fifo_empty flag
366 */
367 pr_debug("TE INT, disabling TX\n");
368 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
369 spin_lock_irqsave(&s->data_lock, flags);
370 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
371 buf[0] |= s->mode1_reg;
372 spin_unlock_irqrestore(&s->data_lock, flags);
373 if (max3107_rw(s, (u8 *)buf, NULL, 2))
374 dev_err(&s->spi->dev, "SPI transfer TX dis failed\n");
375 s->tx_enabled = 0;
376 s->tx_fifo_empty = 1;
377 }
378
379 if (irq_status & MAX3107_IRQ_RXFIFO_BIT)
380 /* RX FIFO interrupt,
381 * handled by normal RX handling
382 */
383 pr_debug("RFIFO INT\n");
384
385 /* Return RX level */
386 return rx_level;
387}
388
389/* Trigger work thread*/
390static void max3107_dowork(struct max3107_port *s)
391{
392 if (!work_pending(&s->work) && !freezing(current) && !s->suspended)
393 queue_work(s->workqueue, &s->work);
394 else
395 dev_warn(&s->spi->dev, "interrup isn't serviced normally!\n");
396}
397
398/* Work thread */
399static void max3107_work(struct work_struct *w)
400{
401 struct max3107_port *s = container_of(w, struct max3107_port, work);
402 u16 rxlvl = 0;
403 int len; /* SPI transfer buffer length */
404 u16 buf[5]; /* Buffer for SPI transfers */
405 unsigned long flags;
406
407 /* Start by reading current RX FIFO level */
408 buf[0] = MAX3107_RXFIFOLVL_REG;
409 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
410 dev_err(&s->spi->dev, "SPI transfer RX lev failed\n");
411 rxlvl = 0;
412 } else {
413 rxlvl = (buf[0] & MAX3107_SPI_RX_DATA_MASK);
414 }
415
416 do {
417 pr_debug("rxlvl %d\n", rxlvl);
418
419 /* Handle RX */
420 max3107_handlerx(s, rxlvl);
421 rxlvl = 0;
422
423 if (s->handle_irq) {
424 /* Handle pending interrupts
425 * We also get new RX FIFO level since new data may
426 * have been received while pushing received data to
427 * receivers
428 */
429 s->handle_irq = 0;
430 rxlvl = handle_interrupt(s);
431 }
432
433 /* Handle TX */
434 max3107_handletx(s);
435
436 /* Handle configuration changes */
437 len = 0;
438 spin_lock_irqsave(&s->data_lock, flags);
439 if (s->mode1_commit) {
440 pr_debug("mode1_commit\n");
441 buf[len] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
442 buf[len++] |= s->mode1_reg;
443 s->mode1_commit = 0;
444 }
445 if (s->lcr_commit) {
446 pr_debug("lcr_commit\n");
447 buf[len] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG);
448 buf[len++] |= s->lcr_reg;
449 s->lcr_commit = 0;
450 }
451 if (s->brg_commit) {
452 pr_debug("brg_commit\n");
453 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG);
454 buf[len++] |= ((s->brg_cfg >> 16) &
455 MAX3107_SPI_TX_DATA_MASK);
456 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG);
457 buf[len++] |= ((s->brg_cfg >> 8) &
458 MAX3107_SPI_TX_DATA_MASK);
459 buf[len] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG);
460 buf[len++] |= ((s->brg_cfg) & 0xff);
461 s->brg_commit = 0;
462 }
463 spin_unlock_irqrestore(&s->data_lock, flags);
464
465 if (len > 0) {
466 if (max3107_rw(s, (u8 *)buf, NULL, len * 2))
467 dev_err(&s->spi->dev,
468 "SPI transfer config failed\n");
469 }
470
471 /* Reloop if interrupt handling indicated data in RX FIFO */
472 } while (rxlvl);
473
474}
475
476/* Set sleep mode */
477static void max3107_set_sleep(struct max3107_port *s, int mode)
478{
479 u16 buf[1]; /* Buffer for SPI transfer */
480 unsigned long flags;
481 pr_debug("enter, mode %d\n", mode);
482
483 buf[0] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG);
484 spin_lock_irqsave(&s->data_lock, flags);
485 switch (mode) {
486 case MAX3107_DISABLE_FORCED_SLEEP:
487 s->mode1_reg &= ~MAX3107_MODE1_FORCESLEEP_BIT;
488 break;
489 case MAX3107_ENABLE_FORCED_SLEEP:
490 s->mode1_reg |= MAX3107_MODE1_FORCESLEEP_BIT;
491 break;
492 case MAX3107_DISABLE_AUTOSLEEP:
493 s->mode1_reg &= ~MAX3107_MODE1_AUTOSLEEP_BIT;
494 break;
495 case MAX3107_ENABLE_AUTOSLEEP:
496 s->mode1_reg |= MAX3107_MODE1_AUTOSLEEP_BIT;
497 break;
498 default:
499 spin_unlock_irqrestore(&s->data_lock, flags);
500 dev_warn(&s->spi->dev, "invalid sleep mode\n");
501 return;
502 }
503 buf[0] |= s->mode1_reg;
504 spin_unlock_irqrestore(&s->data_lock, flags);
505
506 if (max3107_rw(s, (u8 *)buf, NULL, 2))
507 dev_err(&s->spi->dev, "SPI transfer sleep mode failed\n");
508
509 if (mode == MAX3107_DISABLE_AUTOSLEEP ||
510 mode == MAX3107_DISABLE_FORCED_SLEEP)
511 msleep(MAX3107_WAKEUP_DELAY);
512}
513
514/* Perform full register initialization */
515static void max3107_register_init(struct max3107_port *s)
516{
517 u16 buf[11]; /* Buffer for SPI transfers */
518
519 /* 1. Configure baud rate, 9600 as default */
520 s->baud = 9600;
521 /* the below is default*/
522 if (s->ext_clk) {
523 s->brg_cfg = MAX3107_BRG26_B9600;
524 s->baud_tbl = (struct baud_table *)brg26_ext;
525 } else {
526 s->brg_cfg = MAX3107_BRG13_IB9600;
527 s->baud_tbl = (struct baud_table *)brg13_int;
528 }
529
530 if (s->pdata->init)
531 s->pdata->init(s);
532
533 buf[0] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVMSB_REG)
534 | ((s->brg_cfg >> 16) & MAX3107_SPI_TX_DATA_MASK);
535 buf[1] = (MAX3107_WRITE_BIT | MAX3107_BRGDIVLSB_REG)
536 | ((s->brg_cfg >> 8) & MAX3107_SPI_TX_DATA_MASK);
537 buf[2] = (MAX3107_WRITE_BIT | MAX3107_BRGCFG_REG)
538 | ((s->brg_cfg) & 0xff);
539
540 /* 2. Configure LCR register, 8N1 mode by default */
541 s->lcr_reg = MAX3107_LCR_WORD_LEN_8;
542 buf[3] = (MAX3107_WRITE_BIT | MAX3107_LCR_REG)
543 | s->lcr_reg;
544
545 /* 3. Configure MODE 1 register */
546 s->mode1_reg = 0;
547 /* Enable IRQ pin */
548 s->mode1_reg |= MAX3107_MODE1_IRQSEL_BIT;
549 /* Disable TX */
550 s->mode1_reg |= MAX3107_MODE1_TXDIS_BIT;
551 s->tx_enabled = 0;
552 /* RX is enabled */
553 s->rx_enabled = 1;
554 buf[4] = (MAX3107_WRITE_BIT | MAX3107_MODE1_REG)
555 | s->mode1_reg;
556
557 /* 4. Configure MODE 2 register */
558 buf[5] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
559 if (s->loopback) {
560 /* Enable loopback */
561 buf[5] |= MAX3107_MODE2_LOOPBACK_BIT;
562 }
563 /* Reset FIFOs */
564 buf[5] |= MAX3107_MODE2_FIFORST_BIT;
565 s->tx_fifo_empty = 1;
566
567 /* 5. Configure FIFO trigger level register */
568 buf[6] = (MAX3107_WRITE_BIT | MAX3107_FIFOTRIGLVL_REG);
569 /* RX FIFO trigger for 16 words, TX FIFO trigger not used */
570 buf[6] |= (MAX3107_FIFOTRIGLVL_RX(16) | MAX3107_FIFOTRIGLVL_TX(0));
571
572 /* 6. Configure flow control levels */
573 buf[7] = (MAX3107_WRITE_BIT | MAX3107_FLOWLVL_REG);
574 /* Flow control halt level 96, resume level 48 */
575 buf[7] |= (MAX3107_FLOWLVL_RES(48) | MAX3107_FLOWLVL_HALT(96));
576
577 /* 7. Configure flow control */
578 buf[8] = (MAX3107_WRITE_BIT | MAX3107_FLOWCTRL_REG);
579 /* Enable auto CTS and auto RTS flow control */
580 buf[8] |= (MAX3107_FLOWCTRL_AUTOCTS_BIT | MAX3107_FLOWCTRL_AUTORTS_BIT);
581
582 /* 8. Configure RX timeout register */
583 buf[9] = (MAX3107_WRITE_BIT | MAX3107_RXTO_REG);
584 /* Timeout after 48 character intervals */
585 buf[9] |= 0x0030;
586
587 /* 9. Configure LSR interrupt enable register */
588 buf[10] = (MAX3107_WRITE_BIT | MAX3107_LSR_IRQEN_REG);
589 /* Enable RX timeout interrupt */
590 buf[10] |= MAX3107_LSR_RXTO_BIT;
591
592 /* Perform SPI transfer */
593 if (max3107_rw(s, (u8 *)buf, NULL, 22))
594 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
595
596 /* 10. Clear IRQ status register by reading it */
597 buf[0] = MAX3107_IRQSTS_REG;
598
599 /* 11. Configure interrupt enable register */
600 /* Enable LSR interrupt */
601 s->irqen_reg = MAX3107_IRQ_LSR_BIT;
602 /* Enable RX FIFO interrupt */
603 s->irqen_reg |= MAX3107_IRQ_RXFIFO_BIT;
604 buf[1] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG)
605 | s->irqen_reg;
606
607 /* 12. Clear FIFO reset that was set in step 6 */
608 buf[2] = (MAX3107_WRITE_BIT | MAX3107_MODE2_REG);
609 if (s->loopback) {
610 /* Keep loopback enabled */
611 buf[2] |= MAX3107_MODE2_LOOPBACK_BIT;
612 }
613
614 /* Perform SPI transfer */
615 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 6))
616 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
617
618}
619
620/* IRQ handler */
621static irqreturn_t max3107_irq(int irqno, void *dev_id)
622{
623 struct max3107_port *s = dev_id;
624
625 if (irqno != s->spi->irq) {
626 /* Unexpected IRQ */
627 return IRQ_NONE;
628 }
629
630 /* Indicate irq */
631 s->handle_irq = 1;
632
633 /* Trigger work thread */
634 max3107_dowork(s);
635
636 return IRQ_HANDLED;
637}
638
639/* HW suspension function
640 *
641 * Currently autosleep is used to decrease current consumption, alternative
642 * approach would be to set the chip to reset mode if UART is not being
643 * used but that would mess the GPIOs
644 *
645 */
646void max3107_hw_susp(struct max3107_port *s, int suspend)
647{
648 pr_debug("enter, suspend %d\n", suspend);
649
650 if (suspend) {
651 /* Suspend requested,
652 * enable autosleep to decrease current consumption
653 */
654 s->suspended = 1;
655 max3107_set_sleep(s, MAX3107_ENABLE_AUTOSLEEP);
656 } else {
657 /* Resume requested,
658 * disable autosleep
659 */
660 s->suspended = 0;
661 max3107_set_sleep(s, MAX3107_DISABLE_AUTOSLEEP);
662 }
663}
664EXPORT_SYMBOL_GPL(max3107_hw_susp);
665
666/* Modem status IRQ enabling */
667static void max3107_enable_ms(struct uart_port *port)
668{
669 /* Modem status not supported */
670}
671
672/* Data send function */
673static void max3107_start_tx(struct uart_port *port)
674{
675 struct max3107_port *s = container_of(port, struct max3107_port, port);
676
677 /* Trigger work thread for sending data */
678 max3107_dowork(s);
679}
680
681/* Function for checking that there is no pending transfers */
682static unsigned int max3107_tx_empty(struct uart_port *port)
683{
684 struct max3107_port *s = container_of(port, struct max3107_port, port);
685
686 pr_debug("returning %d\n",
687 (s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit)));
688 return s->tx_fifo_empty && uart_circ_empty(&s->port.state->xmit);
689}
690
691/* Function for stopping RX */
692static void max3107_stop_rx(struct uart_port *port)
693{
694 struct max3107_port *s = container_of(port, struct max3107_port, port);
695 unsigned long flags;
696
697 /* Set RX disabled in MODE 1 register */
698 spin_lock_irqsave(&s->data_lock, flags);
699 s->mode1_reg |= MAX3107_MODE1_RXDIS_BIT;
700 s->mode1_commit = 1;
701 spin_unlock_irqrestore(&s->data_lock, flags);
702 /* Set RX disabled */
703 s->rx_enabled = 0;
704 /* Trigger work thread for doing the actual configuration change */
705 max3107_dowork(s);
706}
707
708/* Function for returning control pin states */
709static unsigned int max3107_get_mctrl(struct uart_port *port)
710{
711 /* DCD and DSR are not wired and CTS/RTS is handled automatically
712 * so just indicate DSR and CAR asserted
713 */
714 return TIOCM_DSR | TIOCM_CAR;
715}
716
717/* Function for setting control pin states */
718static void max3107_set_mctrl(struct uart_port *port, unsigned int mctrl)
719{
720 /* DCD and DSR are not wired and CTS/RTS is hadnled automatically
721 * so do nothing
722 */
723}
724
725/* Function for configuring UART parameters */
726static void max3107_set_termios(struct uart_port *port,
727 struct ktermios *termios,
728 struct ktermios *old)
729{
730 struct max3107_port *s = container_of(port, struct max3107_port, port);
731 struct tty_struct *tty;
732 int baud;
733 u16 new_lcr = 0;
734 u32 new_brg = 0;
735 unsigned long flags;
736
737 if (!port->state)
738 return;
739
740 tty = port->state->port.tty;
741 if (!tty)
742 return;
743
744 /* Get new LCR register values */
745 /* Word size */
746 if ((termios->c_cflag & CSIZE) == CS7)
747 new_lcr |= MAX3107_LCR_WORD_LEN_7;
748 else
749 new_lcr |= MAX3107_LCR_WORD_LEN_8;
750
751 /* Parity */
752 if (termios->c_cflag & PARENB) {
753 new_lcr |= MAX3107_LCR_PARITY_BIT;
754 if (!(termios->c_cflag & PARODD))
755 new_lcr |= MAX3107_LCR_EVENPARITY_BIT;
756 }
757
758 /* Stop bits */
759 if (termios->c_cflag & CSTOPB) {
760 /* 2 stop bits */
761 new_lcr |= MAX3107_LCR_STOPLEN_BIT;
762 }
763
764 /* Mask termios capabilities we don't support */
765 termios->c_cflag &= ~CMSPAR;
766
767 /* Set status ignore mask */
768 s->port.ignore_status_mask = 0;
769 if (termios->c_iflag & IGNPAR)
770 s->port.ignore_status_mask |= MAX3107_ALL_ERRORS;
771
772 /* Set low latency to immediately handle pushed data */
773 s->port.state->port.tty->low_latency = 1;
774
775 /* Get new baud rate generator configuration */
776 baud = tty_get_baud_rate(tty);
777
778 spin_lock_irqsave(&s->data_lock, flags);
779 new_brg = get_new_brg(baud, s);
780 /* if can't find the corrent config, use previous */
781 if (!new_brg) {
782 baud = s->baud;
783 new_brg = s->brg_cfg;
784 }
785 spin_unlock_irqrestore(&s->data_lock, flags);
786 tty_termios_encode_baud_rate(termios, baud, baud);
787 s->baud = baud;
788
789 /* Update timeout according to new baud rate */
790 uart_update_timeout(port, termios->c_cflag, baud);
791
792 spin_lock_irqsave(&s->data_lock, flags);
793 if (s->lcr_reg != new_lcr) {
794 s->lcr_reg = new_lcr;
795 s->lcr_commit = 1;
796 }
797 if (s->brg_cfg != new_brg) {
798 s->brg_cfg = new_brg;
799 s->brg_commit = 1;
800 }
801 spin_unlock_irqrestore(&s->data_lock, flags);
802
803 /* Trigger work thread for doing the actual configuration change */
804 max3107_dowork(s);
805}
806
807/* Port shutdown function */
808static void max3107_shutdown(struct uart_port *port)
809{
810 struct max3107_port *s = container_of(port, struct max3107_port, port);
811
812 if (s->suspended && s->pdata->hw_suspend)
813 s->pdata->hw_suspend(s, 0);
814
815 /* Free the interrupt */
816 free_irq(s->spi->irq, s);
817
818 if (s->workqueue) {
819 /* Flush and destroy work queue */
820 flush_workqueue(s->workqueue);
821 destroy_workqueue(s->workqueue);
822 s->workqueue = NULL;
823 }
824
825 /* Suspend HW */
826 if (s->pdata->hw_suspend)
827 s->pdata->hw_suspend(s, 1);
828}
829
830/* Port startup function */
831static int max3107_startup(struct uart_port *port)
832{
833 struct max3107_port *s = container_of(port, struct max3107_port, port);
834
835 /* Initialize work queue */
836 s->workqueue = create_freezable_workqueue("max3107");
837 if (!s->workqueue) {
838 dev_err(&s->spi->dev, "Workqueue creation failed\n");
839 return -EBUSY;
840 }
841 INIT_WORK(&s->work, max3107_work);
842
843 /* Setup IRQ */
844 if (request_irq(s->spi->irq, max3107_irq, IRQF_TRIGGER_FALLING,
845 "max3107", s)) {
846 dev_err(&s->spi->dev, "IRQ reguest failed\n");
847 destroy_workqueue(s->workqueue);
848 s->workqueue = NULL;
849 return -EBUSY;
850 }
851
852 /* Resume HW */
853 if (s->pdata->hw_suspend)
854 s->pdata->hw_suspend(s, 0);
855
856 /* Init registers */
857 max3107_register_init(s);
858
859 return 0;
860}
861
862/* Port type function */
863static const char *max3107_type(struct uart_port *port)
864{
865 struct max3107_port *s = container_of(port, struct max3107_port, port);
866 return s->spi->modalias;
867}
868
869/* Port release function */
870static void max3107_release_port(struct uart_port *port)
871{
872 /* Do nothing */
873}
874
875/* Port request function */
876static int max3107_request_port(struct uart_port *port)
877{
878 /* Do nothing */
879 return 0;
880}
881
882/* Port config function */
883static void max3107_config_port(struct uart_port *port, int flags)
884{
885 struct max3107_port *s = container_of(port, struct max3107_port, port);
886 s->port.type = PORT_MAX3107;
887}
888
889/* Port verify function */
890static int max3107_verify_port(struct uart_port *port,
891 struct serial_struct *ser)
892{
893 if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3107)
894 return 0;
895
896 return -EINVAL;
897}
898
899/* Port stop TX function */
900static void max3107_stop_tx(struct uart_port *port)
901{
902 /* Do nothing */
903}
904
905/* Port break control function */
906static void max3107_break_ctl(struct uart_port *port, int break_state)
907{
908 /* We don't support break control, do nothing */
909}
910
911
912/* Port functions */
913static struct uart_ops max3107_ops = {
914 .tx_empty = max3107_tx_empty,
915 .set_mctrl = max3107_set_mctrl,
916 .get_mctrl = max3107_get_mctrl,
917 .stop_tx = max3107_stop_tx,
918 .start_tx = max3107_start_tx,
919 .stop_rx = max3107_stop_rx,
920 .enable_ms = max3107_enable_ms,
921 .break_ctl = max3107_break_ctl,
922 .startup = max3107_startup,
923 .shutdown = max3107_shutdown,
924 .set_termios = max3107_set_termios,
925 .type = max3107_type,
926 .release_port = max3107_release_port,
927 .request_port = max3107_request_port,
928 .config_port = max3107_config_port,
929 .verify_port = max3107_verify_port,
930};
931
932/* UART driver data */
933static struct uart_driver max3107_uart_driver = {
934 .owner = THIS_MODULE,
935 .driver_name = "ttyMAX",
936 .dev_name = "ttyMAX",
937 .nr = 1,
938};
939
940static int driver_registered = 0;
941
942
943
944/* 'Generic' platform data */
945static struct max3107_plat generic_plat_data = {
946 .loopback = 0,
947 .ext_clk = 1,
948 .hw_suspend = max3107_hw_susp,
949 .polled_mode = 0,
950 .poll_time = 0,
951};
952
953
954/*******************************************************************/
955
956/**
957 * max3107_probe - SPI bus probe entry point
958 * @spi: the spi device
959 *
960 * SPI wants us to probe this device and if appropriate claim it.
961 * Perform any platform specific requirements and then initialise
962 * the device.
963 */
964
965int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata)
966{
967 struct max3107_port *s;
968 u16 buf[2]; /* Buffer for SPI transfers */
969 int retval;
970
971 pr_info("enter max3107 probe\n");
972
973 /* Allocate port structure */
974 s = kzalloc(sizeof(*s), GFP_KERNEL);
975 if (!s) {
976 pr_err("Allocating port structure failed\n");
977 return -ENOMEM;
978 }
979
980 s->pdata = pdata;
981
982 /* SPI Rx buffer
983 * +2 for RX FIFO interrupt
984 * disabling and RX level query
985 */
986 s->rxbuf = kzalloc(sizeof(u16) * (MAX3107_RX_FIFO_SIZE+2), GFP_KERNEL);
987 if (!s->rxbuf) {
988 pr_err("Allocating RX buffer failed\n");
989 retval = -ENOMEM;
990 goto err_free4;
991 }
992 s->rxstr = kzalloc(sizeof(u8) * MAX3107_RX_FIFO_SIZE, GFP_KERNEL);
993 if (!s->rxstr) {
994 pr_err("Allocating RX buffer failed\n");
995 retval = -ENOMEM;
996 goto err_free3;
997 }
998 /* SPI Tx buffer
999 * SPI transfer buffer
1000 * +3 for TX FIFO empty
1001 * interrupt disabling and
1002 * enabling and TX enabling
1003 */
1004 s->txbuf = kzalloc(sizeof(u16) * MAX3107_TX_FIFO_SIZE + 3, GFP_KERNEL);
1005 if (!s->txbuf) {
1006 pr_err("Allocating TX buffer failed\n");
1007 retval = -ENOMEM;
1008 goto err_free2;
1009 }
1010 /* Initialize shared data lock */
1011 spin_lock_init(&s->data_lock);
1012
1013 /* SPI intializations */
1014 dev_set_drvdata(&spi->dev, s);
1015 spi->mode = SPI_MODE_0;
1016 spi->dev.platform_data = pdata;
1017 spi->bits_per_word = 16;
1018 s->ext_clk = pdata->ext_clk;
1019 s->loopback = pdata->loopback;
1020 spi_setup(spi);
1021 s->spi = spi;
1022
1023 /* Check REV ID to ensure we are talking to what we expect */
1024 buf[0] = MAX3107_REVID_REG;
1025 if (max3107_rw(s, (u8 *)buf, (u8 *)buf, 2)) {
1026 dev_err(&s->spi->dev, "SPI transfer for REVID read failed\n");
1027 retval = -EIO;
1028 goto err_free1;
1029 }
1030 if ((buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID1 &&
1031 (buf[0] & MAX3107_SPI_RX_DATA_MASK) != MAX3107_REVID2) {
1032 dev_err(&s->spi->dev, "REVID %x does not match\n",
1033 (buf[0] & MAX3107_SPI_RX_DATA_MASK));
1034 retval = -ENODEV;
1035 goto err_free1;
1036 }
1037
1038 /* Disable all interrupts */
1039 buf[0] = (MAX3107_WRITE_BIT | MAX3107_IRQEN_REG | 0x0000);
1040 buf[0] |= 0x0000;
1041
1042 /* Configure clock source */
1043 buf[1] = (MAX3107_WRITE_BIT | MAX3107_CLKSRC_REG);
1044 if (s->ext_clk) {
1045 /* External clock */
1046 buf[1] |= MAX3107_CLKSRC_EXTCLK_BIT;
1047 }
1048
1049 /* PLL bypass ON */
1050 buf[1] |= MAX3107_CLKSRC_PLLBYP_BIT;
1051
1052 /* Perform SPI transfer */
1053 if (max3107_rw(s, (u8 *)buf, NULL, 4)) {
1054 dev_err(&s->spi->dev, "SPI transfer for init failed\n");
1055 retval = -EIO;
1056 goto err_free1;
1057 }
1058
1059 /* Register UART driver */
1060 if (!driver_registered) {
1061 retval = uart_register_driver(&max3107_uart_driver);
1062 if (retval) {
1063 dev_err(&s->spi->dev, "Registering UART driver failed\n");
1064 goto err_free1;
1065 }
1066 driver_registered = 1;
1067 }
1068
1069 /* Initialize UART port data */
1070 s->port.fifosize = 128;
1071 s->port.ops = &max3107_ops;
1072 s->port.line = 0;
1073 s->port.dev = &spi->dev;
1074 s->port.uartclk = 9600;
1075 s->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
1076 s->port.irq = s->spi->irq;
1077 s->port.type = PORT_MAX3107;
1078
1079 /* Add UART port */
1080 retval = uart_add_one_port(&max3107_uart_driver, &s->port);
1081 if (retval < 0) {
1082 dev_err(&s->spi->dev, "Adding UART port failed\n");
1083 goto err_free1;
1084 }
1085
1086 if (pdata->configure) {
1087 retval = pdata->configure(s);
1088 if (retval < 0)
1089 goto err_free1;
1090 }
1091
1092 /* Go to suspend mode */
1093 if (pdata->hw_suspend)
1094 pdata->hw_suspend(s, 1);
1095
1096 return 0;
1097
1098err_free1:
1099 kfree(s->txbuf);
1100err_free2:
1101 kfree(s->rxstr);
1102err_free3:
1103 kfree(s->rxbuf);
1104err_free4:
1105 kfree(s);
1106 return retval;
1107}
1108EXPORT_SYMBOL_GPL(max3107_probe);
1109
1110/* Driver remove function */
1111int max3107_remove(struct spi_device *spi)
1112{
1113 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1114
1115 pr_info("enter max3107 remove\n");
1116
1117 /* Remove port */
1118 if (uart_remove_one_port(&max3107_uart_driver, &s->port))
1119 dev_warn(&s->spi->dev, "Removing UART port failed\n");
1120
1121
1122 /* Free TxRx buffer */
1123 kfree(s->rxbuf);
1124 kfree(s->rxstr);
1125 kfree(s->txbuf);
1126
1127 /* Free port structure */
1128 kfree(s);
1129
1130 return 0;
1131}
1132EXPORT_SYMBOL_GPL(max3107_remove);
1133
1134/* Driver suspend function */
1135int max3107_suspend(struct spi_device *spi, pm_message_t state)
1136{
1137#ifdef CONFIG_PM
1138 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1139
1140 pr_debug("enter suspend\n");
1141
1142 /* Suspend UART port */
1143 uart_suspend_port(&max3107_uart_driver, &s->port);
1144
1145 /* Go to suspend mode */
1146 if (s->pdata->hw_suspend)
1147 s->pdata->hw_suspend(s, 1);
1148#endif /* CONFIG_PM */
1149 return 0;
1150}
1151EXPORT_SYMBOL_GPL(max3107_suspend);
1152
1153/* Driver resume function */
1154int max3107_resume(struct spi_device *spi)
1155{
1156#ifdef CONFIG_PM
1157 struct max3107_port *s = dev_get_drvdata(&spi->dev);
1158
1159 pr_debug("enter resume\n");
1160
1161 /* Resume from suspend */
1162 if (s->pdata->hw_suspend)
1163 s->pdata->hw_suspend(s, 0);
1164
1165 /* Resume UART port */
1166 uart_resume_port(&max3107_uart_driver, &s->port);
1167#endif /* CONFIG_PM */
1168 return 0;
1169}
1170EXPORT_SYMBOL_GPL(max3107_resume);
1171
1172static int max3107_probe_generic(struct spi_device *spi)
1173{
1174 return max3107_probe(spi, &generic_plat_data);
1175}
1176
1177/* Spi driver data */
1178static struct spi_driver max3107_driver = {
1179 .driver = {
1180 .name = "max3107",
1181 .bus = &spi_bus_type,
1182 .owner = THIS_MODULE,
1183 },
1184 .probe = max3107_probe_generic,
1185 .remove = __devexit_p(max3107_remove),
1186 .suspend = max3107_suspend,
1187 .resume = max3107_resume,
1188};
1189
1190/* Driver init function */
1191static int __init max3107_init(void)
1192{
1193 pr_info("enter max3107 init\n");
1194 return spi_register_driver(&max3107_driver);
1195}
1196
1197/* Driver exit function */
1198static void __exit max3107_exit(void)
1199{
1200 pr_info("enter max3107 exit\n");
1201 /* Unregister UART driver */
1202 if (driver_registered)
1203 uart_unregister_driver(&max3107_uart_driver);
1204 spi_unregister_driver(&max3107_driver);
1205}
1206
1207module_init(max3107_init);
1208module_exit(max3107_exit);
1209
1210MODULE_DESCRIPTION("MAX3107 driver");
1211MODULE_AUTHOR("Aavamobile");
1212MODULE_ALIAS("max3107-spi");
1213MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/max3107.h b/drivers/tty/serial/max3107.h
new file mode 100644
index 000000000000..8415fc723b96
--- /dev/null
+++ b/drivers/tty/serial/max3107.h
@@ -0,0 +1,441 @@
1/*
2 * max3107.h - spi uart protocol driver header for Maxim 3107
3 *
4 * Copyright (C) Aavamobile 2009
5 * Based on serial_max3100.h by Christian Pellegrin
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef _MAX3107_H
14#define _MAX3107_H
15
16/* Serial error status definitions */
17#define MAX3107_PARITY_ERROR 1
18#define MAX3107_FRAME_ERROR 2
19#define MAX3107_OVERRUN_ERROR 4
20#define MAX3107_ALL_ERRORS (MAX3107_PARITY_ERROR | \
21 MAX3107_FRAME_ERROR | \
22 MAX3107_OVERRUN_ERROR)
23
24/* GPIO definitions */
25#define MAX3107_GPIO_BASE 88
26#define MAX3107_GPIO_COUNT 4
27
28
29/* GPIO connected to chip's reset pin */
30#define MAX3107_RESET_GPIO 87
31
32
33/* Chip reset delay */
34#define MAX3107_RESET_DELAY 10
35
36/* Chip wakeup delay */
37#define MAX3107_WAKEUP_DELAY 50
38
39
40/* Sleep mode definitions */
41#define MAX3107_DISABLE_FORCED_SLEEP 0
42#define MAX3107_ENABLE_FORCED_SLEEP 1
43#define MAX3107_DISABLE_AUTOSLEEP 2
44#define MAX3107_ENABLE_AUTOSLEEP 3
45
46
47/* Definitions for register access with SPI transfers
48 *
49 * SPI transfer format:
50 *
51 * Master to slave bits xzzzzzzzyyyyyyyy
52 * Slave to master bits aaaaaaaabbbbbbbb
53 *
54 * where:
55 * x = 0 for reads, 1 for writes
56 * z = register address
57 * y = new register value if write, 0 if read
58 * a = unspecified
59 * b = register value if read, unspecified if write
60 */
61
62/* SPI speed */
63#define MAX3107_SPI_SPEED (3125000 * 2)
64
65/* Write bit */
66#define MAX3107_WRITE_BIT (1 << 15)
67
68/* SPI TX data mask */
69#define MAX3107_SPI_RX_DATA_MASK (0x00ff)
70
71/* SPI RX data mask */
72#define MAX3107_SPI_TX_DATA_MASK (0x00ff)
73
74/* Register access masks */
75#define MAX3107_RHR_REG (0x0000) /* RX FIFO */
76#define MAX3107_THR_REG (0x0000) /* TX FIFO */
77#define MAX3107_IRQEN_REG (0x0100) /* IRQ enable */
78#define MAX3107_IRQSTS_REG (0x0200) /* IRQ status */
79#define MAX3107_LSR_IRQEN_REG (0x0300) /* LSR IRQ enable */
80#define MAX3107_LSR_IRQSTS_REG (0x0400) /* LSR IRQ status */
81#define MAX3107_SPCHR_IRQEN_REG (0x0500) /* Special char IRQ enable */
82#define MAX3107_SPCHR_IRQSTS_REG (0x0600) /* Special char IRQ status */
83#define MAX3107_STS_IRQEN_REG (0x0700) /* Status IRQ enable */
84#define MAX3107_STS_IRQSTS_REG (0x0800) /* Status IRQ status */
85#define MAX3107_MODE1_REG (0x0900) /* MODE1 */
86#define MAX3107_MODE2_REG (0x0a00) /* MODE2 */
87#define MAX3107_LCR_REG (0x0b00) /* LCR */
88#define MAX3107_RXTO_REG (0x0c00) /* RX timeout */
89#define MAX3107_HDPIXDELAY_REG (0x0d00) /* Auto transceiver delays */
90#define MAX3107_IRDA_REG (0x0e00) /* IRDA settings */
91#define MAX3107_FLOWLVL_REG (0x0f00) /* Flow control levels */
92#define MAX3107_FIFOTRIGLVL_REG (0x1000) /* FIFO IRQ trigger levels */
93#define MAX3107_TXFIFOLVL_REG (0x1100) /* TX FIFO level */
94#define MAX3107_RXFIFOLVL_REG (0x1200) /* RX FIFO level */
95#define MAX3107_FLOWCTRL_REG (0x1300) /* Flow control */
96#define MAX3107_XON1_REG (0x1400) /* XON1 character */
97#define MAX3107_XON2_REG (0x1500) /* XON2 character */
98#define MAX3107_XOFF1_REG (0x1600) /* XOFF1 character */
99#define MAX3107_XOFF2_REG (0x1700) /* XOFF2 character */
100#define MAX3107_GPIOCFG_REG (0x1800) /* GPIO config */
101#define MAX3107_GPIODATA_REG (0x1900) /* GPIO data */
102#define MAX3107_PLLCFG_REG (0x1a00) /* PLL config */
103#define MAX3107_BRGCFG_REG (0x1b00) /* Baud rate generator conf */
104#define MAX3107_BRGDIVLSB_REG (0x1c00) /* Baud rate divisor LSB */
105#define MAX3107_BRGDIVMSB_REG (0x1d00) /* Baud rate divisor MSB */
106#define MAX3107_CLKSRC_REG (0x1e00) /* Clock source */
107#define MAX3107_REVID_REG (0x1f00) /* Revision identification */
108
109/* IRQ register bits */
110#define MAX3107_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
111#define MAX3107_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
112#define MAX3107_IRQ_STS_BIT (1 << 2) /* Status interrupt */
113#define MAX3107_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
114#define MAX3107_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
115#define MAX3107_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
116#define MAX3107_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
117#define MAX3107_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
118
119/* LSR register bits */
120#define MAX3107_LSR_RXTO_BIT (1 << 0) /* RX timeout */
121#define MAX3107_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
122#define MAX3107_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
123#define MAX3107_LSR_FRERR_BIT (1 << 3) /* Frame error */
124#define MAX3107_LSR_RXBRK_BIT (1 << 4) /* RX break */
125#define MAX3107_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
126#define MAX3107_LSR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
127#define MAX3107_LSR_CTS_BIT (1 << 7) /* CTS pin state */
128
129/* Special character register bits */
130#define MAX3107_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
131#define MAX3107_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
132#define MAX3107_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
133#define MAX3107_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
134#define MAX3107_SPCHR_BREAK_BIT (1 << 4) /* RX break */
135#define MAX3107_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
136#define MAX3107_SPCHR_UNDEF6_BIT (1 << 6) /* Undefined/not used */
137#define MAX3107_SPCHR_UNDEF7_BIT (1 << 7) /* Undefined/not used */
138
139/* Status register bits */
140#define MAX3107_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
141#define MAX3107_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
142#define MAX3107_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
143#define MAX3107_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
144#define MAX3107_STS_UNDEF4_BIT (1 << 4) /* Undefined/not used */
145#define MAX3107_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
146#define MAX3107_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
147#define MAX3107_STS_UNDEF7_BIT (1 << 7) /* Undefined/not used */
148
149/* MODE1 register bits */
150#define MAX3107_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
151#define MAX3107_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
152#define MAX3107_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
153#define MAX3107_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
154#define MAX3107_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
155#define MAX3107_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
156#define MAX3107_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
157#define MAX3107_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
158
159/* MODE2 register bits */
160#define MAX3107_MODE2_RST_BIT (1 << 0) /* Chip reset */
161#define MAX3107_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
162#define MAX3107_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
163#define MAX3107_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
164#define MAX3107_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
165#define MAX3107_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
166#define MAX3107_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
167#define MAX3107_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
168
169/* LCR register bits */
170#define MAX3107_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
171#define MAX3107_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
172 *
173 * Word length bits table:
174 * 00 -> 5 bit words
175 * 01 -> 6 bit words
176 * 10 -> 7 bit words
177 * 11 -> 8 bit words
178 */
179#define MAX3107_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
180 *
181 * STOP length bit table:
182 * 0 -> 1 stop bit
183 * 1 -> 1-1.5 stop bits if
184 * word length is 5,
185 * 2 stop bits otherwise
186 */
187#define MAX3107_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
188#define MAX3107_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
189#define MAX3107_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
190#define MAX3107_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
191#define MAX3107_LCR_RTS_BIT (1 << 7) /* RTS pin control */
192#define MAX3107_LCR_WORD_LEN_5 (0x0000)
193#define MAX3107_LCR_WORD_LEN_6 (0x0001)
194#define MAX3107_LCR_WORD_LEN_7 (0x0002)
195#define MAX3107_LCR_WORD_LEN_8 (0x0003)
196
197
198/* IRDA register bits */
199#define MAX3107_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
200#define MAX3107_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
201#define MAX3107_IRDA_SHORTIR_BIT (1 << 2) /* Short SIR mode enable */
202#define MAX3107_IRDA_MIR_BIT (1 << 3) /* MIR mode enable */
203#define MAX3107_IRDA_RXINV_BIT (1 << 4) /* RX logic inversion enable */
204#define MAX3107_IRDA_TXINV_BIT (1 << 5) /* TX logic inversion enable */
205#define MAX3107_IRDA_UNDEF6_BIT (1 << 6) /* Undefined/not used */
206#define MAX3107_IRDA_UNDEF7_BIT (1 << 7) /* Undefined/not used */
207
208/* Flow control trigger level register masks */
209#define MAX3107_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
210#define MAX3107_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
211#define MAX3107_FLOWLVL_HALT(words) ((words/8) & 0x000f)
212#define MAX3107_FLOWLVL_RES(words) (((words/8) & 0x000f) << 4)
213
214/* FIFO interrupt trigger level register masks */
215#define MAX3107_FIFOTRIGLVL_TX_MASK (0x000f) /* TX FIFO trigger level */
216#define MAX3107_FIFOTRIGLVL_RX_MASK (0x00f0) /* RX FIFO trigger level */
217#define MAX3107_FIFOTRIGLVL_TX(words) ((words/8) & 0x000f)
218#define MAX3107_FIFOTRIGLVL_RX(words) (((words/8) & 0x000f) << 4)
219
220/* Flow control register bits */
221#define MAX3107_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
222#define MAX3107_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
223#define MAX3107_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
224 * are used in conjunction with
225 * XOFF2 for definition of
226 * special character */
227#define MAX3107_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
228#define MAX3107_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
229#define MAX3107_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
230 *
231 * SWFLOW bits 1 & 0 table:
232 * 00 -> no transmitter flow
233 * control
234 * 01 -> receiver compares
235 * XON2 and XOFF2
236 * and controls
237 * transmitter
238 * 10 -> receiver compares
239 * XON1 and XOFF1
240 * and controls
241 * transmitter
242 * 11 -> receiver compares
243 * XON1, XON2, XOFF1 and
244 * XOFF2 and controls
245 * transmitter
246 */
247#define MAX3107_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
248#define MAX3107_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
249 *
250 * SWFLOW bits 3 & 2 table:
251 * 00 -> no received flow
252 * control
253 * 01 -> transmitter generates
254 * XON2 and XOFF2
255 * 10 -> transmitter generates
256 * XON1 and XOFF1
257 * 11 -> transmitter generates
258 * XON1, XON2, XOFF1 and
259 * XOFF2
260 */
261
262/* GPIO configuration register bits */
263#define MAX3107_GPIOCFG_GP0OUT_BIT (1 << 0) /* GPIO 0 output enable */
264#define MAX3107_GPIOCFG_GP1OUT_BIT (1 << 1) /* GPIO 1 output enable */
265#define MAX3107_GPIOCFG_GP2OUT_BIT (1 << 2) /* GPIO 2 output enable */
266#define MAX3107_GPIOCFG_GP3OUT_BIT (1 << 3) /* GPIO 3 output enable */
267#define MAX3107_GPIOCFG_GP0OD_BIT (1 << 4) /* GPIO 0 open-drain enable */
268#define MAX3107_GPIOCFG_GP1OD_BIT (1 << 5) /* GPIO 1 open-drain enable */
269#define MAX3107_GPIOCFG_GP2OD_BIT (1 << 6) /* GPIO 2 open-drain enable */
270#define MAX3107_GPIOCFG_GP3OD_BIT (1 << 7) /* GPIO 3 open-drain enable */
271
272/* GPIO DATA register bits */
273#define MAX3107_GPIODATA_GP0OUT_BIT (1 << 0) /* GPIO 0 output value */
274#define MAX3107_GPIODATA_GP1OUT_BIT (1 << 1) /* GPIO 1 output value */
275#define MAX3107_GPIODATA_GP2OUT_BIT (1 << 2) /* GPIO 2 output value */
276#define MAX3107_GPIODATA_GP3OUT_BIT (1 << 3) /* GPIO 3 output value */
277#define MAX3107_GPIODATA_GP0IN_BIT (1 << 4) /* GPIO 0 input value */
278#define MAX3107_GPIODATA_GP1IN_BIT (1 << 5) /* GPIO 1 input value */
279#define MAX3107_GPIODATA_GP2IN_BIT (1 << 6) /* GPIO 2 input value */
280#define MAX3107_GPIODATA_GP3IN_BIT (1 << 7) /* GPIO 3 input value */
281
282/* PLL configuration register masks */
283#define MAX3107_PLLCFG_PREDIV_MASK (0x003f) /* PLL predivision value */
284#define MAX3107_PLLCFG_PLLFACTOR_MASK (0x00c0) /* PLL multiplication factor */
285
286/* Baud rate generator configuration register masks and bits */
287#define MAX3107_BRGCFG_FRACT_MASK (0x000f) /* Fractional portion of
288 * Baud rate generator divisor
289 */
290#define MAX3107_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
291#define MAX3107_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
292#define MAX3107_BRGCFG_UNDEF6_BIT (1 << 6) /* Undefined/not used */
293#define MAX3107_BRGCFG_UNDEF7_BIT (1 << 7) /* Undefined/not used */
294
295/* Clock source register bits */
296#define MAX3107_CLKSRC_INTOSC_BIT (1 << 0) /* Internal osc enable */
297#define MAX3107_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
298#define MAX3107_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
299#define MAX3107_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
300#define MAX3107_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
301#define MAX3107_CLKSRC_UNDEF5_BIT (1 << 5) /* Undefined/not used */
302#define MAX3107_CLKSRC_UNDEF6_BIT (1 << 6) /* Undefined/not used */
303#define MAX3107_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
304
305
306/* HW definitions */
307#define MAX3107_RX_FIFO_SIZE 128
308#define MAX3107_TX_FIFO_SIZE 128
309#define MAX3107_REVID1 0x00a0
310#define MAX3107_REVID2 0x00a1
311
312
313/* Baud rate generator configuration values for external clock 13MHz */
314#define MAX3107_BRG13_B300 (0x0A9400 | 0x05)
315#define MAX3107_BRG13_B600 (0x054A00 | 0x03)
316#define MAX3107_BRG13_B1200 (0x02A500 | 0x01)
317#define MAX3107_BRG13_B2400 (0x015200 | 0x09)
318#define MAX3107_BRG13_B4800 (0x00A900 | 0x04)
319#define MAX3107_BRG13_B9600 (0x005400 | 0x0A)
320#define MAX3107_BRG13_B19200 (0x002A00 | 0x05)
321#define MAX3107_BRG13_B38400 (0x001500 | 0x03)
322#define MAX3107_BRG13_B57600 (0x000E00 | 0x02)
323#define MAX3107_BRG13_B115200 (0x000700 | 0x01)
324#define MAX3107_BRG13_B230400 (0x000300 | 0x08)
325#define MAX3107_BRG13_B460800 (0x000100 | 0x0c)
326#define MAX3107_BRG13_B921600 (0x000100 | 0x1c)
327
328/* Baud rate generator configuration values for external clock 26MHz */
329#define MAX3107_BRG26_B300 (0x152800 | 0x0A)
330#define MAX3107_BRG26_B600 (0x0A9400 | 0x05)
331#define MAX3107_BRG26_B1200 (0x054A00 | 0x03)
332#define MAX3107_BRG26_B2400 (0x02A500 | 0x01)
333#define MAX3107_BRG26_B4800 (0x015200 | 0x09)
334#define MAX3107_BRG26_B9600 (0x00A900 | 0x04)
335#define MAX3107_BRG26_B19200 (0x005400 | 0x0A)
336#define MAX3107_BRG26_B38400 (0x002A00 | 0x05)
337#define MAX3107_BRG26_B57600 (0x001C00 | 0x03)
338#define MAX3107_BRG26_B115200 (0x000E00 | 0x02)
339#define MAX3107_BRG26_B230400 (0x000700 | 0x01)
340#define MAX3107_BRG26_B460800 (0x000300 | 0x08)
341#define MAX3107_BRG26_B921600 (0x000100 | 0x0C)
342
343/* Baud rate generator configuration values for internal clock */
344#define MAX3107_BRG13_IB300 (0x008000 | 0x00)
345#define MAX3107_BRG13_IB600 (0x004000 | 0x00)
346#define MAX3107_BRG13_IB1200 (0x002000 | 0x00)
347#define MAX3107_BRG13_IB2400 (0x001000 | 0x00)
348#define MAX3107_BRG13_IB4800 (0x000800 | 0x00)
349#define MAX3107_BRG13_IB9600 (0x000400 | 0x00)
350#define MAX3107_BRG13_IB19200 (0x000200 | 0x00)
351#define MAX3107_BRG13_IB38400 (0x000100 | 0x00)
352#define MAX3107_BRG13_IB57600 (0x000000 | 0x0B)
353#define MAX3107_BRG13_IB115200 (0x000000 | 0x05)
354#define MAX3107_BRG13_IB230400 (0x000000 | 0x03)
355#define MAX3107_BRG13_IB460800 (0x000000 | 0x00)
356#define MAX3107_BRG13_IB921600 (0x000000 | 0x00)
357
358
359struct baud_table {
360 int baud;
361 u32 new_brg;
362};
363
364struct max3107_port {
365 /* UART port structure */
366 struct uart_port port;
367
368 /* SPI device structure */
369 struct spi_device *spi;
370
371#if defined(CONFIG_GPIOLIB)
372 /* GPIO chip structure */
373 struct gpio_chip chip;
374#endif
375
376 /* Workqueue that does all the magic */
377 struct workqueue_struct *workqueue;
378 struct work_struct work;
379
380 /* Lock for shared data */
381 spinlock_t data_lock;
382
383 /* Device configuration */
384 int ext_clk; /* 1 if external clock used */
385 int loopback; /* Current loopback mode state */
386 int baud; /* Current baud rate */
387
388 /* State flags */
389 int suspended; /* Indicates suspend mode */
390 int tx_fifo_empty; /* Flag for TX FIFO state */
391 int rx_enabled; /* Flag for receiver state */
392 int tx_enabled; /* Flag for transmitter state */
393
394 u16 irqen_reg; /* Current IRQ enable register value */
395 /* Shared data */
396 u16 mode1_reg; /* Current mode1 register value*/
397 int mode1_commit; /* Flag for setting new mode1 register value */
398 u16 lcr_reg; /* Current LCR register value */
399 int lcr_commit; /* Flag for setting new LCR register value */
400 u32 brg_cfg; /* Current Baud rate generator config */
401 int brg_commit; /* Flag for setting new baud rate generator
402 * config
403 */
404 struct baud_table *baud_tbl;
405 int handle_irq; /* Indicates that IRQ should be handled */
406
407 /* Rx buffer and str*/
408 u16 *rxbuf;
409 u8 *rxstr;
410 /* Tx buffer*/
411 u16 *txbuf;
412
413 struct max3107_plat *pdata; /* Platform data */
414};
415
416/* Platform data structure */
417struct max3107_plat {
418 /* Loopback mode enable */
419 int loopback;
420 /* External clock enable */
421 int ext_clk;
422 /* Called during the register initialisation */
423 void (*init)(struct max3107_port *s);
424 /* Called when the port is found and configured */
425 int (*configure)(struct max3107_port *s);
426 /* HW suspend function */
427 void (*hw_suspend) (struct max3107_port *s, int suspend);
428 /* Polling mode enable */
429 int polled_mode;
430 /* Polling period if polling mode enabled */
431 int poll_time;
432};
433
434extern int max3107_rw(struct max3107_port *s, u8 *tx, u8 *rx, int len);
435extern void max3107_hw_susp(struct max3107_port *s, int suspend);
436extern int max3107_probe(struct spi_device *spi, struct max3107_plat *pdata);
437extern int max3107_remove(struct spi_device *spi);
438extern int max3107_suspend(struct spi_device *spi, pm_message_t state);
439extern int max3107_resume(struct spi_device *spi);
440
441#endif /* _LINUX_SERIAL_MAX3107_H */
diff --git a/drivers/tty/serial/mcf.c b/drivers/tty/serial/mcf.c
new file mode 100644
index 000000000000..3394b7cc1722
--- /dev/null
+++ b/drivers/tty/serial/mcf.c
@@ -0,0 +1,662 @@
1/****************************************************************************/
2
3/*
4 * mcf.c -- Freescale ColdFire UART driver
5 *
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14/****************************************************************************/
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/console.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial.h>
24#include <linux/serial_core.h>
25#include <linux/io.h>
26#include <asm/coldfire.h>
27#include <asm/mcfsim.h>
28#include <asm/mcfuart.h>
29#include <asm/nettel.h>
30
31/****************************************************************************/
32
33/*
34 * Some boards implement the DTR/DCD lines using GPIO lines, most
35 * don't. Dummy out the access macros for those that don't. Those
36 * that do should define these macros somewhere in there board
37 * specific inlude files.
38 */
39#if !defined(mcf_getppdcd)
40#define mcf_getppdcd(p) (1)
41#endif
42#if !defined(mcf_getppdtr)
43#define mcf_getppdtr(p) (1)
44#endif
45#if !defined(mcf_setppdtr)
46#define mcf_setppdtr(p, v) do { } while (0)
47#endif
48
49/****************************************************************************/
50
51/*
52 * Local per-uart structure.
53 */
54struct mcf_uart {
55 struct uart_port port;
56 unsigned int sigs; /* Local copy of line sigs */
57 unsigned char imr; /* Local IMR mirror */
58};
59
60/****************************************************************************/
61
62static unsigned int mcf_tx_empty(struct uart_port *port)
63{
64 return (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXEMPTY) ?
65 TIOCSER_TEMT : 0;
66}
67
68/****************************************************************************/
69
70static unsigned int mcf_get_mctrl(struct uart_port *port)
71{
72 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
73 unsigned int sigs;
74
75 sigs = (readb(port->membase + MCFUART_UIPR) & MCFUART_UIPR_CTS) ?
76 0 : TIOCM_CTS;
77 sigs |= (pp->sigs & TIOCM_RTS);
78 sigs |= (mcf_getppdcd(port->line) ? TIOCM_CD : 0);
79 sigs |= (mcf_getppdtr(port->line) ? TIOCM_DTR : 0);
80
81 return sigs;
82}
83
84/****************************************************************************/
85
86static void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
87{
88 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
89
90 pp->sigs = sigs;
91 mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
92 if (sigs & TIOCM_RTS)
93 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP1);
94 else
95 writeb(MCFUART_UOP_RTS, port->membase + MCFUART_UOP0);
96}
97
98/****************************************************************************/
99
100static void mcf_start_tx(struct uart_port *port)
101{
102 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
103
104 pp->imr |= MCFUART_UIR_TXREADY;
105 writeb(pp->imr, port->membase + MCFUART_UIMR);
106}
107
108/****************************************************************************/
109
110static void mcf_stop_tx(struct uart_port *port)
111{
112 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
113
114 pp->imr &= ~MCFUART_UIR_TXREADY;
115 writeb(pp->imr, port->membase + MCFUART_UIMR);
116}
117
118/****************************************************************************/
119
120static void mcf_stop_rx(struct uart_port *port)
121{
122 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
123
124 pp->imr &= ~MCFUART_UIR_RXREADY;
125 writeb(pp->imr, port->membase + MCFUART_UIMR);
126}
127
128/****************************************************************************/
129
130static void mcf_break_ctl(struct uart_port *port, int break_state)
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&port->lock, flags);
135 if (break_state == -1)
136 writeb(MCFUART_UCR_CMDBREAKSTART, port->membase + MCFUART_UCR);
137 else
138 writeb(MCFUART_UCR_CMDBREAKSTOP, port->membase + MCFUART_UCR);
139 spin_unlock_irqrestore(&port->lock, flags);
140}
141
142/****************************************************************************/
143
144static void mcf_enable_ms(struct uart_port *port)
145{
146}
147
148/****************************************************************************/
149
150static int mcf_startup(struct uart_port *port)
151{
152 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
153 unsigned long flags;
154
155 spin_lock_irqsave(&port->lock, flags);
156
157 /* Reset UART, get it into known state... */
158 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
159 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
160
161 /* Enable the UART transmitter and receiver */
162 writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
163 port->membase + MCFUART_UCR);
164
165 /* Enable RX interrupts now */
166 pp->imr = MCFUART_UIR_RXREADY;
167 writeb(pp->imr, port->membase + MCFUART_UIMR);
168
169 spin_unlock_irqrestore(&port->lock, flags);
170
171 return 0;
172}
173
174/****************************************************************************/
175
176static void mcf_shutdown(struct uart_port *port)
177{
178 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
179 unsigned long flags;
180
181 spin_lock_irqsave(&port->lock, flags);
182
183 /* Disable all interrupts now */
184 pp->imr = 0;
185 writeb(pp->imr, port->membase + MCFUART_UIMR);
186
187 /* Disable UART transmitter and receiver */
188 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
189 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
190
191 spin_unlock_irqrestore(&port->lock, flags);
192}
193
194/****************************************************************************/
195
196static void mcf_set_termios(struct uart_port *port, struct ktermios *termios,
197 struct ktermios *old)
198{
199 unsigned long flags;
200 unsigned int baud, baudclk;
201#if defined(CONFIG_M5272)
202 unsigned int baudfr;
203#endif
204 unsigned char mr1, mr2;
205
206 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
207#if defined(CONFIG_M5272)
208 baudclk = (MCF_BUSCLK / baud) / 32;
209 baudfr = (((MCF_BUSCLK / baud) + 1) / 2) % 16;
210#else
211 baudclk = ((MCF_BUSCLK / baud) + 16) / 32;
212#endif
213
214 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
215 mr2 = 0;
216
217 switch (termios->c_cflag & CSIZE) {
218 case CS5: mr1 |= MCFUART_MR1_CS5; break;
219 case CS6: mr1 |= MCFUART_MR1_CS6; break;
220 case CS7: mr1 |= MCFUART_MR1_CS7; break;
221 case CS8:
222 default: mr1 |= MCFUART_MR1_CS8; break;
223 }
224
225 if (termios->c_cflag & PARENB) {
226 if (termios->c_cflag & CMSPAR) {
227 if (termios->c_cflag & PARODD)
228 mr1 |= MCFUART_MR1_PARITYMARK;
229 else
230 mr1 |= MCFUART_MR1_PARITYSPACE;
231 } else {
232 if (termios->c_cflag & PARODD)
233 mr1 |= MCFUART_MR1_PARITYODD;
234 else
235 mr1 |= MCFUART_MR1_PARITYEVEN;
236 }
237 } else {
238 mr1 |= MCFUART_MR1_PARITYNONE;
239 }
240
241 if (termios->c_cflag & CSTOPB)
242 mr2 |= MCFUART_MR2_STOP2;
243 else
244 mr2 |= MCFUART_MR2_STOP1;
245
246 if (termios->c_cflag & CRTSCTS) {
247 mr1 |= MCFUART_MR1_RXRTS;
248 mr2 |= MCFUART_MR2_TXCTS;
249 }
250
251 spin_lock_irqsave(&port->lock, flags);
252 uart_update_timeout(port, termios->c_cflag, baud);
253 writeb(MCFUART_UCR_CMDRESETRX, port->membase + MCFUART_UCR);
254 writeb(MCFUART_UCR_CMDRESETTX, port->membase + MCFUART_UCR);
255 writeb(MCFUART_UCR_CMDRESETMRPTR, port->membase + MCFUART_UCR);
256 writeb(mr1, port->membase + MCFUART_UMR);
257 writeb(mr2, port->membase + MCFUART_UMR);
258 writeb((baudclk & 0xff00) >> 8, port->membase + MCFUART_UBG1);
259 writeb((baudclk & 0xff), port->membase + MCFUART_UBG2);
260#if defined(CONFIG_M5272)
261 writeb((baudfr & 0x0f), port->membase + MCFUART_UFPD);
262#endif
263 writeb(MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER,
264 port->membase + MCFUART_UCSR);
265 writeb(MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE,
266 port->membase + MCFUART_UCR);
267 spin_unlock_irqrestore(&port->lock, flags);
268}
269
270/****************************************************************************/
271
272static void mcf_rx_chars(struct mcf_uart *pp)
273{
274 struct uart_port *port = &pp->port;
275 unsigned char status, ch, flag;
276
277 while ((status = readb(port->membase + MCFUART_USR)) & MCFUART_USR_RXREADY) {
278 ch = readb(port->membase + MCFUART_URB);
279 flag = TTY_NORMAL;
280 port->icount.rx++;
281
282 if (status & MCFUART_USR_RXERR) {
283 writeb(MCFUART_UCR_CMDRESETERR,
284 port->membase + MCFUART_UCR);
285
286 if (status & MCFUART_USR_RXBREAK) {
287 port->icount.brk++;
288 if (uart_handle_break(port))
289 continue;
290 } else if (status & MCFUART_USR_RXPARITY) {
291 port->icount.parity++;
292 } else if (status & MCFUART_USR_RXOVERRUN) {
293 port->icount.overrun++;
294 } else if (status & MCFUART_USR_RXFRAMING) {
295 port->icount.frame++;
296 }
297
298 status &= port->read_status_mask;
299
300 if (status & MCFUART_USR_RXBREAK)
301 flag = TTY_BREAK;
302 else if (status & MCFUART_USR_RXPARITY)
303 flag = TTY_PARITY;
304 else if (status & MCFUART_USR_RXFRAMING)
305 flag = TTY_FRAME;
306 }
307
308 if (uart_handle_sysrq_char(port, ch))
309 continue;
310 uart_insert_char(port, status, MCFUART_USR_RXOVERRUN, ch, flag);
311 }
312
313 tty_flip_buffer_push(port->state->port.tty);
314}
315
316/****************************************************************************/
317
318static void mcf_tx_chars(struct mcf_uart *pp)
319{
320 struct uart_port *port = &pp->port;
321 struct circ_buf *xmit = &port->state->xmit;
322
323 if (port->x_char) {
324 /* Send special char - probably flow control */
325 writeb(port->x_char, port->membase + MCFUART_UTB);
326 port->x_char = 0;
327 port->icount.tx++;
328 return;
329 }
330
331 while (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY) {
332 if (xmit->head == xmit->tail)
333 break;
334 writeb(xmit->buf[xmit->tail], port->membase + MCFUART_UTB);
335 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
336 port->icount.tx++;
337 }
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(port);
341
342 if (xmit->head == xmit->tail) {
343 pp->imr &= ~MCFUART_UIR_TXREADY;
344 writeb(pp->imr, port->membase + MCFUART_UIMR);
345 }
346}
347
348/****************************************************************************/
349
350static irqreturn_t mcf_interrupt(int irq, void *data)
351{
352 struct uart_port *port = data;
353 struct mcf_uart *pp = container_of(port, struct mcf_uart, port);
354 unsigned int isr;
355 irqreturn_t ret = IRQ_NONE;
356
357 isr = readb(port->membase + MCFUART_UISR) & pp->imr;
358
359 spin_lock(&port->lock);
360 if (isr & MCFUART_UIR_RXREADY) {
361 mcf_rx_chars(pp);
362 ret = IRQ_HANDLED;
363 }
364 if (isr & MCFUART_UIR_TXREADY) {
365 mcf_tx_chars(pp);
366 ret = IRQ_HANDLED;
367 }
368 spin_unlock(&port->lock);
369
370 return ret;
371}
372
373/****************************************************************************/
374
375static void mcf_config_port(struct uart_port *port, int flags)
376{
377 port->type = PORT_MCF;
378 port->fifosize = MCFUART_TXFIFOSIZE;
379
380 /* Clear mask, so no surprise interrupts. */
381 writeb(0, port->membase + MCFUART_UIMR);
382
383 if (request_irq(port->irq, mcf_interrupt, IRQF_DISABLED, "UART", port))
384 printk(KERN_ERR "MCF: unable to attach ColdFire UART %d "
385 "interrupt vector=%d\n", port->line, port->irq);
386}
387
388/****************************************************************************/
389
390static const char *mcf_type(struct uart_port *port)
391{
392 return (port->type == PORT_MCF) ? "ColdFire UART" : NULL;
393}
394
395/****************************************************************************/
396
397static int mcf_request_port(struct uart_port *port)
398{
399 /* UARTs always present */
400 return 0;
401}
402
403/****************************************************************************/
404
405static void mcf_release_port(struct uart_port *port)
406{
407 /* Nothing to release... */
408}
409
410/****************************************************************************/
411
412static int mcf_verify_port(struct uart_port *port, struct serial_struct *ser)
413{
414 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_MCF))
415 return -EINVAL;
416 return 0;
417}
418
419/****************************************************************************/
420
421/*
422 * Define the basic serial functions we support.
423 */
424static const struct uart_ops mcf_uart_ops = {
425 .tx_empty = mcf_tx_empty,
426 .get_mctrl = mcf_get_mctrl,
427 .set_mctrl = mcf_set_mctrl,
428 .start_tx = mcf_start_tx,
429 .stop_tx = mcf_stop_tx,
430 .stop_rx = mcf_stop_rx,
431 .enable_ms = mcf_enable_ms,
432 .break_ctl = mcf_break_ctl,
433 .startup = mcf_startup,
434 .shutdown = mcf_shutdown,
435 .set_termios = mcf_set_termios,
436 .type = mcf_type,
437 .request_port = mcf_request_port,
438 .release_port = mcf_release_port,
439 .config_port = mcf_config_port,
440 .verify_port = mcf_verify_port,
441};
442
443static struct mcf_uart mcf_ports[4];
444
445#define MCF_MAXPORTS ARRAY_SIZE(mcf_ports)
446
447/****************************************************************************/
448#if defined(CONFIG_SERIAL_MCF_CONSOLE)
449/****************************************************************************/
450
451int __init early_mcf_setup(struct mcf_platform_uart *platp)
452{
453 struct uart_port *port;
454 int i;
455
456 for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
457 port = &mcf_ports[i].port;
458
459 port->line = i;
460 port->type = PORT_MCF;
461 port->mapbase = platp[i].mapbase;
462 port->membase = (platp[i].membase) ? platp[i].membase :
463 (unsigned char __iomem *) port->mapbase;
464 port->iotype = SERIAL_IO_MEM;
465 port->irq = platp[i].irq;
466 port->uartclk = MCF_BUSCLK;
467 port->flags = ASYNC_BOOT_AUTOCONF;
468 port->ops = &mcf_uart_ops;
469 }
470
471 return 0;
472}
473
474/****************************************************************************/
475
476static void mcf_console_putc(struct console *co, const char c)
477{
478 struct uart_port *port = &(mcf_ports + co->index)->port;
479 int i;
480
481 for (i = 0; (i < 0x10000); i++) {
482 if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
483 break;
484 }
485 writeb(c, port->membase + MCFUART_UTB);
486 for (i = 0; (i < 0x10000); i++) {
487 if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
488 break;
489 }
490}
491
492/****************************************************************************/
493
494static void mcf_console_write(struct console *co, const char *s, unsigned int count)
495{
496 for (; (count); count--, s++) {
497 mcf_console_putc(co, *s);
498 if (*s == '\n')
499 mcf_console_putc(co, '\r');
500 }
501}
502
503/****************************************************************************/
504
505static int __init mcf_console_setup(struct console *co, char *options)
506{
507 struct uart_port *port;
508 int baud = CONFIG_SERIAL_MCF_BAUDRATE;
509 int bits = 8;
510 int parity = 'n';
511 int flow = 'n';
512
513 if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
514 co->index = 0;
515 port = &mcf_ports[co->index].port;
516 if (port->membase == 0)
517 return -ENODEV;
518
519 if (options)
520 uart_parse_options(options, &baud, &parity, &bits, &flow);
521
522 return uart_set_options(port, co, baud, parity, bits, flow);
523}
524
525/****************************************************************************/
526
527static struct uart_driver mcf_driver;
528
529static struct console mcf_console = {
530 .name = "ttyS",
531 .write = mcf_console_write,
532 .device = uart_console_device,
533 .setup = mcf_console_setup,
534 .flags = CON_PRINTBUFFER,
535 .index = -1,
536 .data = &mcf_driver,
537};
538
539static int __init mcf_console_init(void)
540{
541 register_console(&mcf_console);
542 return 0;
543}
544
545console_initcall(mcf_console_init);
546
547#define MCF_CONSOLE &mcf_console
548
549/****************************************************************************/
550#else
551/****************************************************************************/
552
553#define MCF_CONSOLE NULL
554
555/****************************************************************************/
556#endif /* CONFIG_MCF_CONSOLE */
557/****************************************************************************/
558
559/*
560 * Define the mcf UART driver structure.
561 */
562static struct uart_driver mcf_driver = {
563 .owner = THIS_MODULE,
564 .driver_name = "mcf",
565 .dev_name = "ttyS",
566 .major = TTY_MAJOR,
567 .minor = 64,
568 .nr = MCF_MAXPORTS,
569 .cons = MCF_CONSOLE,
570};
571
572/****************************************************************************/
573
574static int __devinit mcf_probe(struct platform_device *pdev)
575{
576 struct mcf_platform_uart *platp = pdev->dev.platform_data;
577 struct uart_port *port;
578 int i;
579
580 for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
581 port = &mcf_ports[i].port;
582
583 port->line = i;
584 port->type = PORT_MCF;
585 port->mapbase = platp[i].mapbase;
586 port->membase = (platp[i].membase) ? platp[i].membase :
587 (unsigned char __iomem *) platp[i].mapbase;
588 port->iotype = SERIAL_IO_MEM;
589 port->irq = platp[i].irq;
590 port->uartclk = MCF_BUSCLK;
591 port->ops = &mcf_uart_ops;
592 port->flags = ASYNC_BOOT_AUTOCONF;
593
594 uart_add_one_port(&mcf_driver, port);
595 }
596
597 return 0;
598}
599
600/****************************************************************************/
601
602static int __devexit mcf_remove(struct platform_device *pdev)
603{
604 struct uart_port *port;
605 int i;
606
607 for (i = 0; (i < MCF_MAXPORTS); i++) {
608 port = &mcf_ports[i].port;
609 if (port)
610 uart_remove_one_port(&mcf_driver, port);
611 }
612
613 return 0;
614}
615
616/****************************************************************************/
617
618static struct platform_driver mcf_platform_driver = {
619 .probe = mcf_probe,
620 .remove = __devexit_p(mcf_remove),
621 .driver = {
622 .name = "mcfuart",
623 .owner = THIS_MODULE,
624 },
625};
626
627/****************************************************************************/
628
629static int __init mcf_init(void)
630{
631 int rc;
632
633 printk("ColdFire internal UART serial driver\n");
634
635 rc = uart_register_driver(&mcf_driver);
636 if (rc)
637 return rc;
638 rc = platform_driver_register(&mcf_platform_driver);
639 if (rc)
640 return rc;
641 return 0;
642}
643
644/****************************************************************************/
645
646static void __exit mcf_exit(void)
647{
648 platform_driver_unregister(&mcf_platform_driver);
649 uart_unregister_driver(&mcf_driver);
650}
651
652/****************************************************************************/
653
654module_init(mcf_init);
655module_exit(mcf_exit);
656
657MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
658MODULE_DESCRIPTION("Freescale ColdFire UART driver");
659MODULE_LICENSE("GPL");
660MODULE_ALIAS("platform:mcfuart");
661
662/****************************************************************************/
diff --git a/drivers/tty/serial/mfd.c b/drivers/tty/serial/mfd.c
new file mode 100644
index 000000000000..cab52f4a88b0
--- /dev/null
+++ b/drivers/tty/serial/mfd.c
@@ -0,0 +1,1470 @@
1/*
2 * mfd.c: driver for High Speed UART device of Intel Medfield platform
3 *
4 * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
5 *
6 * (C) Copyright 2010 Intel Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 */
13
14/* Notes:
15 * 1. DMA channel allocation: 0/1 channel are assigned to port 0,
16 * 2/3 chan to port 1, 4/5 chan to port 3. Even number chans
17 * are used for RX, odd chans for TX
18 *
19 * 2. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
20 * asserted, only when the HW is reset the DDCD and DDSR will
21 * be triggered
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
28#include <linux/slab.h>
29#include <linux/serial_reg.h>
30#include <linux/circ_buf.h>
31#include <linux/delay.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36#include <linux/serial_mfd.h>
37#include <linux/dma-mapping.h>
38#include <linux/pci.h>
39#include <linux/io.h>
40#include <linux/debugfs.h>
41
42#define HSU_DMA_BUF_SIZE 2048
43
44#define chan_readl(chan, offset) readl(chan->reg + offset)
45#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
46
47#define mfd_readl(obj, offset) readl(obj->reg + offset)
48#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
49
50static int hsu_dma_enable;
51module_param(hsu_dma_enable, int, 0);
52MODULE_PARM_DESC(hsu_dma_enable,
53 "It is a bitmap to set working mode, if bit[x] is 1, then port[x] will work in DMA mode, otherwise in PIO mode.");
54
55struct hsu_dma_buffer {
56 u8 *buf;
57 dma_addr_t dma_addr;
58 u32 dma_size;
59 u32 ofs;
60};
61
62struct hsu_dma_chan {
63 u32 id;
64 enum dma_data_direction dirt;
65 struct uart_hsu_port *uport;
66 void __iomem *reg;
67};
68
69struct uart_hsu_port {
70 struct uart_port port;
71 unsigned char ier;
72 unsigned char lcr;
73 unsigned char mcr;
74 unsigned int lsr_break_flag;
75 char name[12];
76 int index;
77 struct device *dev;
78
79 struct hsu_dma_chan *txc;
80 struct hsu_dma_chan *rxc;
81 struct hsu_dma_buffer txbuf;
82 struct hsu_dma_buffer rxbuf;
83 int use_dma; /* flag for DMA/PIO */
84 int running;
85 int dma_tx_on;
86};
87
88/* Top level data structure of HSU */
89struct hsu_port {
90 void __iomem *reg;
91 unsigned long paddr;
92 unsigned long iolen;
93 u32 irq;
94
95 struct uart_hsu_port port[3];
96 struct hsu_dma_chan chans[10];
97
98 struct dentry *debugfs;
99};
100
101static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
102{
103 unsigned int val;
104
105 if (offset > UART_MSR) {
106 offset <<= 2;
107 val = readl(up->port.membase + offset);
108 } else
109 val = (unsigned int)readb(up->port.membase + offset);
110
111 return val;
112}
113
114static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
115{
116 if (offset > UART_MSR) {
117 offset <<= 2;
118 writel(value, up->port.membase + offset);
119 } else {
120 unsigned char val = value & 0xff;
121 writeb(val, up->port.membase + offset);
122 }
123}
124
125#ifdef CONFIG_DEBUG_FS
126
127#define HSU_REGS_BUFSIZE 1024
128
129static int hsu_show_regs_open(struct inode *inode, struct file *file)
130{
131 file->private_data = inode->i_private;
132 return 0;
133}
134
135static ssize_t port_show_regs(struct file *file, char __user *user_buf,
136 size_t count, loff_t *ppos)
137{
138 struct uart_hsu_port *up = file->private_data;
139 char *buf;
140 u32 len = 0;
141 ssize_t ret;
142
143 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
144 if (!buf)
145 return 0;
146
147 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
148 "MFD HSU port[%d] regs:\n", up->index);
149
150 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
151 "=================================\n");
152 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
153 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
154 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
155 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
156 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
157 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
158 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
159 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
160 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
161 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
162 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
163 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
164 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
165 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
166 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
167 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
168 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
169 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
170 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
171 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
172
173 if (len > HSU_REGS_BUFSIZE)
174 len = HSU_REGS_BUFSIZE;
175
176 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
177 kfree(buf);
178 return ret;
179}
180
181static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
182 size_t count, loff_t *ppos)
183{
184 struct hsu_dma_chan *chan = file->private_data;
185 char *buf;
186 u32 len = 0;
187 ssize_t ret;
188
189 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
190 if (!buf)
191 return 0;
192
193 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
194 "MFD HSU DMA channel [%d] regs:\n", chan->id);
195
196 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
197 "=================================\n");
198 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
199 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
200 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
201 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
202 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
203 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
204 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
205 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
206 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
207 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
208 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
209 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
210 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
211 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
212 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
213 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
214 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
215 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
216 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
217 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
218 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
220 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
221 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
222
223 if (len > HSU_REGS_BUFSIZE)
224 len = HSU_REGS_BUFSIZE;
225
226 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
227 kfree(buf);
228 return ret;
229}
230
231static const struct file_operations port_regs_ops = {
232 .owner = THIS_MODULE,
233 .open = hsu_show_regs_open,
234 .read = port_show_regs,
235 .llseek = default_llseek,
236};
237
238static const struct file_operations dma_regs_ops = {
239 .owner = THIS_MODULE,
240 .open = hsu_show_regs_open,
241 .read = dma_show_regs,
242 .llseek = default_llseek,
243};
244
245static int hsu_debugfs_init(struct hsu_port *hsu)
246{
247 int i;
248 char name[32];
249
250 hsu->debugfs = debugfs_create_dir("hsu", NULL);
251 if (!hsu->debugfs)
252 return -ENOMEM;
253
254 for (i = 0; i < 3; i++) {
255 snprintf(name, sizeof(name), "port_%d_regs", i);
256 debugfs_create_file(name, S_IFREG | S_IRUGO,
257 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
258 }
259
260 for (i = 0; i < 6; i++) {
261 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
262 debugfs_create_file(name, S_IFREG | S_IRUGO,
263 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
264 }
265
266 return 0;
267}
268
269static void hsu_debugfs_remove(struct hsu_port *hsu)
270{
271 if (hsu->debugfs)
272 debugfs_remove_recursive(hsu->debugfs);
273}
274
275#else
276static inline int hsu_debugfs_init(struct hsu_port *hsu)
277{
278 return 0;
279}
280
281static inline void hsu_debugfs_remove(struct hsu_port *hsu)
282{
283}
284#endif /* CONFIG_DEBUG_FS */
285
286static void serial_hsu_enable_ms(struct uart_port *port)
287{
288 struct uart_hsu_port *up =
289 container_of(port, struct uart_hsu_port, port);
290
291 up->ier |= UART_IER_MSI;
292 serial_out(up, UART_IER, up->ier);
293}
294
295void hsu_dma_tx(struct uart_hsu_port *up)
296{
297 struct circ_buf *xmit = &up->port.state->xmit;
298 struct hsu_dma_buffer *dbuf = &up->txbuf;
299 int count;
300
301 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
302 if (up->dma_tx_on)
303 return;
304
305 /* Update the circ buf info */
306 xmit->tail += dbuf->ofs;
307 xmit->tail &= UART_XMIT_SIZE - 1;
308
309 up->port.icount.tx += dbuf->ofs;
310 dbuf->ofs = 0;
311
312 /* Disable the channel */
313 chan_writel(up->txc, HSU_CH_CR, 0x0);
314
315 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
316 dma_sync_single_for_device(up->port.dev,
317 dbuf->dma_addr,
318 dbuf->dma_size,
319 DMA_TO_DEVICE);
320
321 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
322 dbuf->ofs = count;
323
324 /* Reprogram the channel */
325 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
326 chan_writel(up->txc, HSU_CH_D0TSR, count);
327
328 /* Reenable the channel */
329 chan_writel(up->txc, HSU_CH_DCR, 0x1
330 | (0x1 << 8)
331 | (0x1 << 16)
332 | (0x1 << 24));
333 up->dma_tx_on = 1;
334 chan_writel(up->txc, HSU_CH_CR, 0x1);
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(&up->port);
339}
340
341/* The buffer is already cache coherent */
342void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
343{
344 dbuf->ofs = 0;
345
346 chan_writel(rxc, HSU_CH_BSR, 32);
347 chan_writel(rxc, HSU_CH_MOTSR, 4);
348
349 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
350 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
351 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
352 | (0x1 << 16)
353 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
354 );
355 chan_writel(rxc, HSU_CH_CR, 0x3);
356}
357
358/* Protected by spin_lock_irqsave(port->lock) */
359static void serial_hsu_start_tx(struct uart_port *port)
360{
361 struct uart_hsu_port *up =
362 container_of(port, struct uart_hsu_port, port);
363
364 if (up->use_dma) {
365 hsu_dma_tx(up);
366 } else if (!(up->ier & UART_IER_THRI)) {
367 up->ier |= UART_IER_THRI;
368 serial_out(up, UART_IER, up->ier);
369 }
370}
371
372static void serial_hsu_stop_tx(struct uart_port *port)
373{
374 struct uart_hsu_port *up =
375 container_of(port, struct uart_hsu_port, port);
376 struct hsu_dma_chan *txc = up->txc;
377
378 if (up->use_dma)
379 chan_writel(txc, HSU_CH_CR, 0x0);
380 else if (up->ier & UART_IER_THRI) {
381 up->ier &= ~UART_IER_THRI;
382 serial_out(up, UART_IER, up->ier);
383 }
384}
385
386/* This is always called in spinlock protected mode, so
387 * modify timeout timer is safe here */
388void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
389{
390 struct hsu_dma_buffer *dbuf = &up->rxbuf;
391 struct hsu_dma_chan *chan = up->rxc;
392 struct uart_port *port = &up->port;
393 struct tty_struct *tty = port->state->port.tty;
394 int count;
395
396 if (!tty)
397 return;
398
399 /*
400 * First need to know how many is already transferred,
401 * then check if its a timeout DMA irq, and return
402 * the trail bytes out, push them up and reenable the
403 * channel
404 */
405
406 /* Timeout IRQ, need wait some time, see Errata 2 */
407 if (int_sts & 0xf00)
408 udelay(2);
409
410 /* Stop the channel */
411 chan_writel(chan, HSU_CH_CR, 0x0);
412
413 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
414 if (!count) {
415 /* Restart the channel before we leave */
416 chan_writel(chan, HSU_CH_CR, 0x3);
417 return;
418 }
419
420 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
421 dbuf->dma_size, DMA_FROM_DEVICE);
422
423 /*
424 * Head will only wrap around when we recycle
425 * the DMA buffer, and when that happens, we
426 * explicitly set tail to 0. So head will
427 * always be greater than tail.
428 */
429 tty_insert_flip_string(tty, dbuf->buf, count);
430 port->icount.rx += count;
431
432 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
433 dbuf->dma_size, DMA_FROM_DEVICE);
434
435 /* Reprogram the channel */
436 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
437 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
438 chan_writel(chan, HSU_CH_DCR, 0x1
439 | (0x1 << 8)
440 | (0x1 << 16)
441 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
442 );
443 tty_flip_buffer_push(tty);
444
445 chan_writel(chan, HSU_CH_CR, 0x3);
446
447}
448
449static void serial_hsu_stop_rx(struct uart_port *port)
450{
451 struct uart_hsu_port *up =
452 container_of(port, struct uart_hsu_port, port);
453 struct hsu_dma_chan *chan = up->rxc;
454
455 if (up->use_dma)
456 chan_writel(chan, HSU_CH_CR, 0x2);
457 else {
458 up->ier &= ~UART_IER_RLSI;
459 up->port.read_status_mask &= ~UART_LSR_DR;
460 serial_out(up, UART_IER, up->ier);
461 }
462}
463
464static inline void receive_chars(struct uart_hsu_port *up, int *status)
465{
466 struct tty_struct *tty = up->port.state->port.tty;
467 unsigned int ch, flag;
468 unsigned int max_count = 256;
469
470 if (!tty)
471 return;
472
473 do {
474 ch = serial_in(up, UART_RX);
475 flag = TTY_NORMAL;
476 up->port.icount.rx++;
477
478 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
479 UART_LSR_FE | UART_LSR_OE))) {
480
481 dev_warn(up->dev, "We really rush into ERR/BI case"
482 "status = 0x%02x", *status);
483 /* For statistics only */
484 if (*status & UART_LSR_BI) {
485 *status &= ~(UART_LSR_FE | UART_LSR_PE);
486 up->port.icount.brk++;
487 /*
488 * We do the SysRQ and SAK checking
489 * here because otherwise the break
490 * may get masked by ignore_status_mask
491 * or read_status_mask.
492 */
493 if (uart_handle_break(&up->port))
494 goto ignore_char;
495 } else if (*status & UART_LSR_PE)
496 up->port.icount.parity++;
497 else if (*status & UART_LSR_FE)
498 up->port.icount.frame++;
499 if (*status & UART_LSR_OE)
500 up->port.icount.overrun++;
501
502 /* Mask off conditions which should be ignored. */
503 *status &= up->port.read_status_mask;
504
505#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
506 if (up->port.cons &&
507 up->port.cons->index == up->port.line) {
508 /* Recover the break flag from console xmit */
509 *status |= up->lsr_break_flag;
510 up->lsr_break_flag = 0;
511 }
512#endif
513 if (*status & UART_LSR_BI) {
514 flag = TTY_BREAK;
515 } else if (*status & UART_LSR_PE)
516 flag = TTY_PARITY;
517 else if (*status & UART_LSR_FE)
518 flag = TTY_FRAME;
519 }
520
521 if (uart_handle_sysrq_char(&up->port, ch))
522 goto ignore_char;
523
524 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
525 ignore_char:
526 *status = serial_in(up, UART_LSR);
527 } while ((*status & UART_LSR_DR) && max_count--);
528 tty_flip_buffer_push(tty);
529}
530
531static void transmit_chars(struct uart_hsu_port *up)
532{
533 struct circ_buf *xmit = &up->port.state->xmit;
534 int count;
535
536 if (up->port.x_char) {
537 serial_out(up, UART_TX, up->port.x_char);
538 up->port.icount.tx++;
539 up->port.x_char = 0;
540 return;
541 }
542 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
543 serial_hsu_stop_tx(&up->port);
544 return;
545 }
546
547 /* The IRQ is for TX FIFO half-empty */
548 count = up->port.fifosize / 2;
549
550 do {
551 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
552 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
553
554 up->port.icount.tx++;
555 if (uart_circ_empty(xmit))
556 break;
557 } while (--count > 0);
558
559 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
560 uart_write_wakeup(&up->port);
561
562 if (uart_circ_empty(xmit))
563 serial_hsu_stop_tx(&up->port);
564}
565
566static inline void check_modem_status(struct uart_hsu_port *up)
567{
568 int status;
569
570 status = serial_in(up, UART_MSR);
571
572 if ((status & UART_MSR_ANY_DELTA) == 0)
573 return;
574
575 if (status & UART_MSR_TERI)
576 up->port.icount.rng++;
577 if (status & UART_MSR_DDSR)
578 up->port.icount.dsr++;
579 /* We may only get DDCD when HW init and reset */
580 if (status & UART_MSR_DDCD)
581 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
582 /* Will start/stop_tx accordingly */
583 if (status & UART_MSR_DCTS)
584 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
585
586 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
587}
588
589/*
590 * This handles the interrupt from one port.
591 */
592static irqreturn_t port_irq(int irq, void *dev_id)
593{
594 struct uart_hsu_port *up = dev_id;
595 unsigned int iir, lsr;
596 unsigned long flags;
597
598 if (unlikely(!up->running))
599 return IRQ_NONE;
600
601 spin_lock_irqsave(&up->port.lock, flags);
602 if (up->use_dma) {
603 lsr = serial_in(up, UART_LSR);
604 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
605 UART_LSR_FE | UART_LSR_OE)))
606 dev_warn(up->dev,
607 "Got lsr irq while using DMA, lsr = 0x%2x\n",
608 lsr);
609 check_modem_status(up);
610 spin_unlock_irqrestore(&up->port.lock, flags);
611 return IRQ_HANDLED;
612 }
613
614 iir = serial_in(up, UART_IIR);
615 if (iir & UART_IIR_NO_INT) {
616 spin_unlock_irqrestore(&up->port.lock, flags);
617 return IRQ_NONE;
618 }
619
620 lsr = serial_in(up, UART_LSR);
621 if (lsr & UART_LSR_DR)
622 receive_chars(up, &lsr);
623 check_modem_status(up);
624
625 /* lsr will be renewed during the receive_chars */
626 if (lsr & UART_LSR_THRE)
627 transmit_chars(up);
628
629 spin_unlock_irqrestore(&up->port.lock, flags);
630 return IRQ_HANDLED;
631}
632
633static inline void dma_chan_irq(struct hsu_dma_chan *chan)
634{
635 struct uart_hsu_port *up = chan->uport;
636 unsigned long flags;
637 u32 int_sts;
638
639 spin_lock_irqsave(&up->port.lock, flags);
640
641 if (!up->use_dma || !up->running)
642 goto exit;
643
644 /*
645 * No matter what situation, need read clear the IRQ status
646 * There is a bug, see Errata 5, HSD 2900918
647 */
648 int_sts = chan_readl(chan, HSU_CH_SR);
649
650 /* Rx channel */
651 if (chan->dirt == DMA_FROM_DEVICE)
652 hsu_dma_rx(up, int_sts);
653
654 /* Tx channel */
655 if (chan->dirt == DMA_TO_DEVICE) {
656 chan_writel(chan, HSU_CH_CR, 0x0);
657 up->dma_tx_on = 0;
658 hsu_dma_tx(up);
659 }
660
661exit:
662 spin_unlock_irqrestore(&up->port.lock, flags);
663 return;
664}
665
666static irqreturn_t dma_irq(int irq, void *dev_id)
667{
668 struct hsu_port *hsu = dev_id;
669 u32 int_sts, i;
670
671 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
672
673 /* Currently we only have 6 channels may be used */
674 for (i = 0; i < 6; i++) {
675 if (int_sts & 0x1)
676 dma_chan_irq(&hsu->chans[i]);
677 int_sts >>= 1;
678 }
679
680 return IRQ_HANDLED;
681}
682
683static unsigned int serial_hsu_tx_empty(struct uart_port *port)
684{
685 struct uart_hsu_port *up =
686 container_of(port, struct uart_hsu_port, port);
687 unsigned long flags;
688 unsigned int ret;
689
690 spin_lock_irqsave(&up->port.lock, flags);
691 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
692 spin_unlock_irqrestore(&up->port.lock, flags);
693
694 return ret;
695}
696
697static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
698{
699 struct uart_hsu_port *up =
700 container_of(port, struct uart_hsu_port, port);
701 unsigned char status;
702 unsigned int ret;
703
704 status = serial_in(up, UART_MSR);
705
706 ret = 0;
707 if (status & UART_MSR_DCD)
708 ret |= TIOCM_CAR;
709 if (status & UART_MSR_RI)
710 ret |= TIOCM_RNG;
711 if (status & UART_MSR_DSR)
712 ret |= TIOCM_DSR;
713 if (status & UART_MSR_CTS)
714 ret |= TIOCM_CTS;
715 return ret;
716}
717
718static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
719{
720 struct uart_hsu_port *up =
721 container_of(port, struct uart_hsu_port, port);
722 unsigned char mcr = 0;
723
724 if (mctrl & TIOCM_RTS)
725 mcr |= UART_MCR_RTS;
726 if (mctrl & TIOCM_DTR)
727 mcr |= UART_MCR_DTR;
728 if (mctrl & TIOCM_OUT1)
729 mcr |= UART_MCR_OUT1;
730 if (mctrl & TIOCM_OUT2)
731 mcr |= UART_MCR_OUT2;
732 if (mctrl & TIOCM_LOOP)
733 mcr |= UART_MCR_LOOP;
734
735 mcr |= up->mcr;
736
737 serial_out(up, UART_MCR, mcr);
738}
739
740static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
741{
742 struct uart_hsu_port *up =
743 container_of(port, struct uart_hsu_port, port);
744 unsigned long flags;
745
746 spin_lock_irqsave(&up->port.lock, flags);
747 if (break_state == -1)
748 up->lcr |= UART_LCR_SBC;
749 else
750 up->lcr &= ~UART_LCR_SBC;
751 serial_out(up, UART_LCR, up->lcr);
752 spin_unlock_irqrestore(&up->port.lock, flags);
753}
754
755/*
756 * What special to do:
757 * 1. chose the 64B fifo mode
758 * 2. start dma or pio depends on configuration
759 * 3. we only allocate dma memory when needed
760 */
761static int serial_hsu_startup(struct uart_port *port)
762{
763 struct uart_hsu_port *up =
764 container_of(port, struct uart_hsu_port, port);
765 unsigned long flags;
766
767 /*
768 * Clear the FIFO buffers and disable them.
769 * (they will be reenabled in set_termios())
770 */
771 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
772 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
773 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
774 serial_out(up, UART_FCR, 0);
775
776 /* Clear the interrupt registers. */
777 (void) serial_in(up, UART_LSR);
778 (void) serial_in(up, UART_RX);
779 (void) serial_in(up, UART_IIR);
780 (void) serial_in(up, UART_MSR);
781
782 /* Now, initialize the UART, default is 8n1 */
783 serial_out(up, UART_LCR, UART_LCR_WLEN8);
784
785 spin_lock_irqsave(&up->port.lock, flags);
786
787 up->port.mctrl |= TIOCM_OUT2;
788 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
789
790 /*
791 * Finally, enable interrupts. Note: Modem status interrupts
792 * are set via set_termios(), which will be occurring imminently
793 * anyway, so we don't enable them here.
794 */
795 if (!up->use_dma)
796 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
797 else
798 up->ier = 0;
799 serial_out(up, UART_IER, up->ier);
800
801 spin_unlock_irqrestore(&up->port.lock, flags);
802
803 /* DMA init */
804 if (up->use_dma) {
805 struct hsu_dma_buffer *dbuf;
806 struct circ_buf *xmit = &port->state->xmit;
807
808 up->dma_tx_on = 0;
809
810 /* First allocate the RX buffer */
811 dbuf = &up->rxbuf;
812 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
813 if (!dbuf->buf) {
814 up->use_dma = 0;
815 goto exit;
816 }
817 dbuf->dma_addr = dma_map_single(port->dev,
818 dbuf->buf,
819 HSU_DMA_BUF_SIZE,
820 DMA_FROM_DEVICE);
821 dbuf->dma_size = HSU_DMA_BUF_SIZE;
822
823 /* Start the RX channel right now */
824 hsu_dma_start_rx_chan(up->rxc, dbuf);
825
826 /* Next init the TX DMA */
827 dbuf = &up->txbuf;
828 dbuf->buf = xmit->buf;
829 dbuf->dma_addr = dma_map_single(port->dev,
830 dbuf->buf,
831 UART_XMIT_SIZE,
832 DMA_TO_DEVICE);
833 dbuf->dma_size = UART_XMIT_SIZE;
834
835 /* This should not be changed all around */
836 chan_writel(up->txc, HSU_CH_BSR, 32);
837 chan_writel(up->txc, HSU_CH_MOTSR, 4);
838 dbuf->ofs = 0;
839 }
840
841exit:
842 /* And clear the interrupt registers again for luck. */
843 (void) serial_in(up, UART_LSR);
844 (void) serial_in(up, UART_RX);
845 (void) serial_in(up, UART_IIR);
846 (void) serial_in(up, UART_MSR);
847
848 up->running = 1;
849 return 0;
850}
851
852static void serial_hsu_shutdown(struct uart_port *port)
853{
854 struct uart_hsu_port *up =
855 container_of(port, struct uart_hsu_port, port);
856 unsigned long flags;
857
858 /* Disable interrupts from this port */
859 up->ier = 0;
860 serial_out(up, UART_IER, 0);
861 up->running = 0;
862
863 spin_lock_irqsave(&up->port.lock, flags);
864 up->port.mctrl &= ~TIOCM_OUT2;
865 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
866 spin_unlock_irqrestore(&up->port.lock, flags);
867
868 /* Disable break condition and FIFOs */
869 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
870 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
871 UART_FCR_CLEAR_RCVR |
872 UART_FCR_CLEAR_XMIT);
873 serial_out(up, UART_FCR, 0);
874}
875
876static void
877serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
878 struct ktermios *old)
879{
880 struct uart_hsu_port *up =
881 container_of(port, struct uart_hsu_port, port);
882 struct tty_struct *tty = port->state->port.tty;
883 unsigned char cval, fcr = 0;
884 unsigned long flags;
885 unsigned int baud, quot;
886 u32 ps, mul;
887
888 switch (termios->c_cflag & CSIZE) {
889 case CS5:
890 cval = UART_LCR_WLEN5;
891 break;
892 case CS6:
893 cval = UART_LCR_WLEN6;
894 break;
895 case CS7:
896 cval = UART_LCR_WLEN7;
897 break;
898 default:
899 case CS8:
900 cval = UART_LCR_WLEN8;
901 break;
902 }
903
904 /* CMSPAR isn't supported by this driver */
905 if (tty)
906 tty->termios->c_cflag &= ~CMSPAR;
907
908 if (termios->c_cflag & CSTOPB)
909 cval |= UART_LCR_STOP;
910 if (termios->c_cflag & PARENB)
911 cval |= UART_LCR_PARITY;
912 if (!(termios->c_cflag & PARODD))
913 cval |= UART_LCR_EPAR;
914
915 /*
916 * The base clk is 50Mhz, and the baud rate come from:
917 * baud = 50M * MUL / (DIV * PS * DLAB)
918 *
919 * For those basic low baud rate we can get the direct
920 * scalar from 2746800, like 115200 = 2746800/24. For those
921 * higher baud rate, we handle them case by case, mainly by
922 * adjusting the MUL/PS registers, and DIV register is kept
923 * as default value 0x3d09 to make things simple
924 */
925 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
926
927 quot = 1;
928 ps = 0x10;
929 mul = 0x3600;
930 switch (baud) {
931 case 3500000:
932 mul = 0x3345;
933 ps = 0xC;
934 break;
935 case 1843200:
936 mul = 0x2400;
937 break;
938 case 3000000:
939 case 2500000:
940 case 2000000:
941 case 1500000:
942 case 1000000:
943 case 500000:
944 /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
945 mul = baud / 500000 * 0x9C4;
946 break;
947 default:
948 /* Use uart_get_divisor to get quot for other baud rates */
949 quot = 0;
950 }
951
952 if (!quot)
953 quot = uart_get_divisor(port, baud);
954
955 if ((up->port.uartclk / quot) < (2400 * 16))
956 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
957 else if ((up->port.uartclk / quot) < (230400 * 16))
958 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
959 else
960 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
961
962 fcr |= UART_FCR_HSU_64B_FIFO;
963
964 /*
965 * Ok, we're now changing the port state. Do it with
966 * interrupts disabled.
967 */
968 spin_lock_irqsave(&up->port.lock, flags);
969
970 /* Update the per-port timeout */
971 uart_update_timeout(port, termios->c_cflag, baud);
972
973 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
974 if (termios->c_iflag & INPCK)
975 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
976 if (termios->c_iflag & (BRKINT | PARMRK))
977 up->port.read_status_mask |= UART_LSR_BI;
978
979 /* Characters to ignore */
980 up->port.ignore_status_mask = 0;
981 if (termios->c_iflag & IGNPAR)
982 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
983 if (termios->c_iflag & IGNBRK) {
984 up->port.ignore_status_mask |= UART_LSR_BI;
985 /*
986 * If we're ignoring parity and break indicators,
987 * ignore overruns too (for real raw support).
988 */
989 if (termios->c_iflag & IGNPAR)
990 up->port.ignore_status_mask |= UART_LSR_OE;
991 }
992
993 /* Ignore all characters if CREAD is not set */
994 if ((termios->c_cflag & CREAD) == 0)
995 up->port.ignore_status_mask |= UART_LSR_DR;
996
997 /*
998 * CTS flow control flag and modem status interrupts, disable
999 * MSI by default
1000 */
1001 up->ier &= ~UART_IER_MSI;
1002 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1003 up->ier |= UART_IER_MSI;
1004
1005 serial_out(up, UART_IER, up->ier);
1006
1007 if (termios->c_cflag & CRTSCTS)
1008 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1009 else
1010 up->mcr &= ~UART_MCR_AFE;
1011
1012 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1013 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1014 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1015 serial_out(up, UART_LCR, cval); /* reset DLAB */
1016 serial_out(up, UART_MUL, mul); /* set MUL */
1017 serial_out(up, UART_PS, ps); /* set PS */
1018 up->lcr = cval; /* Save LCR */
1019 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1020 serial_out(up, UART_FCR, fcr);
1021 spin_unlock_irqrestore(&up->port.lock, flags);
1022}
1023
1024static void
1025serial_hsu_pm(struct uart_port *port, unsigned int state,
1026 unsigned int oldstate)
1027{
1028}
1029
1030static void serial_hsu_release_port(struct uart_port *port)
1031{
1032}
1033
1034static int serial_hsu_request_port(struct uart_port *port)
1035{
1036 return 0;
1037}
1038
1039static void serial_hsu_config_port(struct uart_port *port, int flags)
1040{
1041 struct uart_hsu_port *up =
1042 container_of(port, struct uart_hsu_port, port);
1043 up->port.type = PORT_MFD;
1044}
1045
1046static int
1047serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1048{
1049 /* We don't want the core code to modify any port params */
1050 return -EINVAL;
1051}
1052
1053static const char *
1054serial_hsu_type(struct uart_port *port)
1055{
1056 struct uart_hsu_port *up =
1057 container_of(port, struct uart_hsu_port, port);
1058 return up->name;
1059}
1060
1061/* Mainly for uart console use */
1062static struct uart_hsu_port *serial_hsu_ports[3];
1063static struct uart_driver serial_hsu_reg;
1064
1065#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1066
1067#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1068
1069/* Wait for transmitter & holding register to empty */
1070static inline void wait_for_xmitr(struct uart_hsu_port *up)
1071{
1072 unsigned int status, tmout = 1000;
1073
1074 /* Wait up to 1ms for the character to be sent. */
1075 do {
1076 status = serial_in(up, UART_LSR);
1077
1078 if (status & UART_LSR_BI)
1079 up->lsr_break_flag = UART_LSR_BI;
1080
1081 if (--tmout == 0)
1082 break;
1083 udelay(1);
1084 } while (!(status & BOTH_EMPTY));
1085
1086 /* Wait up to 1s for flow control if necessary */
1087 if (up->port.flags & UPF_CONS_FLOW) {
1088 tmout = 1000000;
1089 while (--tmout &&
1090 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1091 udelay(1);
1092 }
1093}
1094
1095static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1096{
1097 struct uart_hsu_port *up =
1098 container_of(port, struct uart_hsu_port, port);
1099
1100 wait_for_xmitr(up);
1101 serial_out(up, UART_TX, ch);
1102}
1103
1104/*
1105 * Print a string to the serial port trying not to disturb
1106 * any possible real use of the port...
1107 *
1108 * The console_lock must be held when we get here.
1109 */
1110static void
1111serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1112{
1113 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1114 unsigned long flags;
1115 unsigned int ier;
1116 int locked = 1;
1117
1118 local_irq_save(flags);
1119 if (up->port.sysrq)
1120 locked = 0;
1121 else if (oops_in_progress) {
1122 locked = spin_trylock(&up->port.lock);
1123 } else
1124 spin_lock(&up->port.lock);
1125
1126 /* First save the IER then disable the interrupts */
1127 ier = serial_in(up, UART_IER);
1128 serial_out(up, UART_IER, 0);
1129
1130 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1131
1132 /*
1133 * Finally, wait for transmitter to become empty
1134 * and restore the IER
1135 */
1136 wait_for_xmitr(up);
1137 serial_out(up, UART_IER, ier);
1138
1139 if (locked)
1140 spin_unlock(&up->port.lock);
1141 local_irq_restore(flags);
1142}
1143
1144static struct console serial_hsu_console;
1145
1146static int __init
1147serial_hsu_console_setup(struct console *co, char *options)
1148{
1149 struct uart_hsu_port *up;
1150 int baud = 115200;
1151 int bits = 8;
1152 int parity = 'n';
1153 int flow = 'n';
1154 int ret;
1155
1156 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1157 co->index = 0;
1158 up = serial_hsu_ports[co->index];
1159 if (!up)
1160 return -ENODEV;
1161
1162 if (options)
1163 uart_parse_options(options, &baud, &parity, &bits, &flow);
1164
1165 ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
1166
1167 return ret;
1168}
1169
1170static struct console serial_hsu_console = {
1171 .name = "ttyMFD",
1172 .write = serial_hsu_console_write,
1173 .device = uart_console_device,
1174 .setup = serial_hsu_console_setup,
1175 .flags = CON_PRINTBUFFER,
1176 .index = 2,
1177 .data = &serial_hsu_reg,
1178};
1179#endif
1180
1181struct uart_ops serial_hsu_pops = {
1182 .tx_empty = serial_hsu_tx_empty,
1183 .set_mctrl = serial_hsu_set_mctrl,
1184 .get_mctrl = serial_hsu_get_mctrl,
1185 .stop_tx = serial_hsu_stop_tx,
1186 .start_tx = serial_hsu_start_tx,
1187 .stop_rx = serial_hsu_stop_rx,
1188 .enable_ms = serial_hsu_enable_ms,
1189 .break_ctl = serial_hsu_break_ctl,
1190 .startup = serial_hsu_startup,
1191 .shutdown = serial_hsu_shutdown,
1192 .set_termios = serial_hsu_set_termios,
1193 .pm = serial_hsu_pm,
1194 .type = serial_hsu_type,
1195 .release_port = serial_hsu_release_port,
1196 .request_port = serial_hsu_request_port,
1197 .config_port = serial_hsu_config_port,
1198 .verify_port = serial_hsu_verify_port,
1199};
1200
1201static struct uart_driver serial_hsu_reg = {
1202 .owner = THIS_MODULE,
1203 .driver_name = "MFD serial",
1204 .dev_name = "ttyMFD",
1205 .major = TTY_MAJOR,
1206 .minor = 128,
1207 .nr = 3,
1208};
1209
1210#ifdef CONFIG_PM
1211static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1212{
1213 void *priv = pci_get_drvdata(pdev);
1214 struct uart_hsu_port *up;
1215
1216 /* Make sure this is not the internal dma controller */
1217 if (priv && (pdev->device != 0x081E)) {
1218 up = priv;
1219 uart_suspend_port(&serial_hsu_reg, &up->port);
1220 }
1221
1222 pci_save_state(pdev);
1223 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1224 return 0;
1225}
1226
1227static int serial_hsu_resume(struct pci_dev *pdev)
1228{
1229 void *priv = pci_get_drvdata(pdev);
1230 struct uart_hsu_port *up;
1231 int ret;
1232
1233 pci_set_power_state(pdev, PCI_D0);
1234 pci_restore_state(pdev);
1235
1236 ret = pci_enable_device(pdev);
1237 if (ret)
1238 dev_warn(&pdev->dev,
1239 "HSU: can't re-enable device, try to continue\n");
1240
1241 if (priv && (pdev->device != 0x081E)) {
1242 up = priv;
1243 uart_resume_port(&serial_hsu_reg, &up->port);
1244 }
1245 return 0;
1246}
1247#else
1248#define serial_hsu_suspend NULL
1249#define serial_hsu_resume NULL
1250#endif
1251
1252/* temp global pointer before we settle down on using one or four PCI dev */
1253static struct hsu_port *phsu;
1254
1255static int serial_hsu_probe(struct pci_dev *pdev,
1256 const struct pci_device_id *ent)
1257{
1258 struct uart_hsu_port *uport;
1259 int index, ret;
1260
1261 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1262 pdev->vendor, pdev->device);
1263
1264 switch (pdev->device) {
1265 case 0x081B:
1266 index = 0;
1267 break;
1268 case 0x081C:
1269 index = 1;
1270 break;
1271 case 0x081D:
1272 index = 2;
1273 break;
1274 case 0x081E:
1275 /* internal DMA controller */
1276 index = 3;
1277 break;
1278 default:
1279 dev_err(&pdev->dev, "HSU: out of index!");
1280 return -ENODEV;
1281 }
1282
1283 ret = pci_enable_device(pdev);
1284 if (ret)
1285 return ret;
1286
1287 if (index == 3) {
1288 /* DMA controller */
1289 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1290 if (ret) {
1291 dev_err(&pdev->dev, "can not get IRQ\n");
1292 goto err_disable;
1293 }
1294 pci_set_drvdata(pdev, phsu);
1295 } else {
1296 /* UART port 0~2 */
1297 uport = &phsu->port[index];
1298 uport->port.irq = pdev->irq;
1299 uport->port.dev = &pdev->dev;
1300 uport->dev = &pdev->dev;
1301
1302 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1303 if (ret) {
1304 dev_err(&pdev->dev, "can not get IRQ\n");
1305 goto err_disable;
1306 }
1307 uart_add_one_port(&serial_hsu_reg, &uport->port);
1308
1309#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1310 if (index == 2) {
1311 register_console(&serial_hsu_console);
1312 uport->port.cons = &serial_hsu_console;
1313 }
1314#endif
1315 pci_set_drvdata(pdev, uport);
1316 }
1317
1318 return 0;
1319
1320err_disable:
1321 pci_disable_device(pdev);
1322 return ret;
1323}
1324
1325static void hsu_global_init(void)
1326{
1327 struct hsu_port *hsu;
1328 struct uart_hsu_port *uport;
1329 struct hsu_dma_chan *dchan;
1330 int i, ret;
1331
1332 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1333 if (!hsu)
1334 return;
1335
1336 /* Get basic io resource and map it */
1337 hsu->paddr = 0xffa28000;
1338 hsu->iolen = 0x1000;
1339
1340 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1341 pr_warning("HSU: error in request mem region\n");
1342
1343 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1344 if (!hsu->reg) {
1345 pr_err("HSU: error in ioremap\n");
1346 ret = -ENOMEM;
1347 goto err_free_region;
1348 }
1349
1350 /* Initialise the 3 UART ports */
1351 uport = hsu->port;
1352 for (i = 0; i < 3; i++) {
1353 uport->port.type = PORT_MFD;
1354 uport->port.iotype = UPIO_MEM;
1355 uport->port.mapbase = (resource_size_t)hsu->paddr
1356 + HSU_PORT_REG_OFFSET
1357 + i * HSU_PORT_REG_LENGTH;
1358 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1359 + i * HSU_PORT_REG_LENGTH;
1360
1361 sprintf(uport->name, "hsu_port%d", i);
1362 uport->port.fifosize = 64;
1363 uport->port.ops = &serial_hsu_pops;
1364 uport->port.line = i;
1365 uport->port.flags = UPF_IOREMAP;
1366 /* set the scalable maxim support rate to 2746800 bps */
1367 uport->port.uartclk = 115200 * 24 * 16;
1368
1369 uport->running = 0;
1370 uport->txc = &hsu->chans[i * 2];
1371 uport->rxc = &hsu->chans[i * 2 + 1];
1372
1373 serial_hsu_ports[i] = uport;
1374 uport->index = i;
1375
1376 if (hsu_dma_enable & (1<<i))
1377 uport->use_dma = 1;
1378 else
1379 uport->use_dma = 0;
1380
1381 uport++;
1382 }
1383
1384 /* Initialise 6 dma channels */
1385 dchan = hsu->chans;
1386 for (i = 0; i < 6; i++) {
1387 dchan->id = i;
1388 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1389 dchan->uport = &hsu->port[i/2];
1390 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1391 i * HSU_DMA_CHANS_REG_LENGTH;
1392
1393 dchan++;
1394 }
1395
1396 phsu = hsu;
1397 hsu_debugfs_init(hsu);
1398 return;
1399
1400err_free_region:
1401 release_mem_region(hsu->paddr, hsu->iolen);
1402 kfree(hsu);
1403 return;
1404}
1405
1406static void serial_hsu_remove(struct pci_dev *pdev)
1407{
1408 void *priv = pci_get_drvdata(pdev);
1409 struct uart_hsu_port *up;
1410
1411 if (!priv)
1412 return;
1413
1414 /* For port 0/1/2, priv is the address of uart_hsu_port */
1415 if (pdev->device != 0x081E) {
1416 up = priv;
1417 uart_remove_one_port(&serial_hsu_reg, &up->port);
1418 }
1419
1420 pci_set_drvdata(pdev, NULL);
1421 free_irq(pdev->irq, priv);
1422 pci_disable_device(pdev);
1423}
1424
1425/* First 3 are UART ports, and the 4th is the DMA */
1426static const struct pci_device_id pci_ids[] __devinitdata = {
1427 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1428 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1429 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1430 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1431 {},
1432};
1433
1434static struct pci_driver hsu_pci_driver = {
1435 .name = "HSU serial",
1436 .id_table = pci_ids,
1437 .probe = serial_hsu_probe,
1438 .remove = __devexit_p(serial_hsu_remove),
1439 .suspend = serial_hsu_suspend,
1440 .resume = serial_hsu_resume,
1441};
1442
1443static int __init hsu_pci_init(void)
1444{
1445 int ret;
1446
1447 hsu_global_init();
1448
1449 ret = uart_register_driver(&serial_hsu_reg);
1450 if (ret)
1451 return ret;
1452
1453 return pci_register_driver(&hsu_pci_driver);
1454}
1455
1456static void __exit hsu_pci_exit(void)
1457{
1458 pci_unregister_driver(&hsu_pci_driver);
1459 uart_unregister_driver(&serial_hsu_reg);
1460
1461 hsu_debugfs_remove(phsu);
1462
1463 kfree(phsu);
1464}
1465
1466module_init(hsu_pci_init);
1467module_exit(hsu_pci_exit);
1468
1469MODULE_LICENSE("GPL v2");
1470MODULE_ALIAS("platform:medfield-hsu");
diff --git a/drivers/tty/serial/mpc52xx_uart.c b/drivers/tty/serial/mpc52xx_uart.c
new file mode 100644
index 000000000000..a0bcd8a3758d
--- /dev/null
+++ b/drivers/tty/serial/mpc52xx_uart.c
@@ -0,0 +1,1524 @@
1/*
2 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
3 *
4 * FIXME According to the usermanual the status bits in the status register
5 * are only updated when the peripherals access the FIFO and not when the
6 * CPU access them. So since we use this bits to know when we stop writing
7 * and reading, they may not be updated in-time and a race condition may
8 * exists. But I haven't be able to prove this and I don't care. But if
9 * any problem arises, it might worth checking. The TX/RX FIFO Stats
10 * registers should be used in addition.
11 * Update: Actually, they seem updated ... At least the bits we use.
12 *
13 *
14 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
15 *
16 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
18 *
19 * Copyright (C) 2008 Freescale Semiconductor Inc.
20 * John Rigby <jrigby@gmail.com>
21 * Added support for MPC5121
22 * Copyright (C) 2006 Secret Lab Technologies Ltd.
23 * Grant Likely <grant.likely@secretlab.ca>
24 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
25 * Copyright (C) 2003 MontaVista, Software, Inc.
26 *
27 * This file is licensed under the terms of the GNU General Public License
28 * version 2. This program is licensed "as is" without any warranty of any
29 * kind, whether express or implied.
30 */
31
32#undef DEBUG
33
34#include <linux/device.h>
35#include <linux/module.h>
36#include <linux/tty.h>
37#include <linux/serial.h>
38#include <linux/sysrq.h>
39#include <linux/console.h>
40#include <linux/delay.h>
41#include <linux/io.h>
42#include <linux/of.h>
43#include <linux/of_platform.h>
44#include <linux/clk.h>
45
46#include <asm/mpc52xx.h>
47#include <asm/mpc52xx_psc.h>
48
49#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50#define SUPPORT_SYSRQ
51#endif
52
53#include <linux/serial_core.h>
54
55
56/* We've been assigned a range on the "Low-density serial ports" major */
57#define SERIAL_PSC_MAJOR 204
58#define SERIAL_PSC_MINOR 148
59
60
61#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
62
63
64static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
65 /* Rem: - We use the read_status_mask as a shadow of
66 * psc->mpc52xx_psc_imr
67 * - It's important that is array is all zero on start as we
68 * use it to know if it's initialized or not ! If it's not sure
69 * it's cleared, then a memset(...,0,...) should be added to
70 * the console_init
71 */
72
73/* lookup table for matching device nodes to index numbers */
74static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
75
76static void mpc52xx_uart_of_enumerate(void);
77
78
79#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
80
81
82/* Forward declaration of the interruption handling routine */
83static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
84static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
85
86
87/* Simple macro to test if a port is console or not. This one is taken
88 * for serial_core.c and maybe should be moved to serial_core.h ? */
89#ifdef CONFIG_SERIAL_CORE_CONSOLE
90#define uart_console(port) \
91 ((port)->cons && (port)->cons->index == (port)->line)
92#else
93#define uart_console(port) (0)
94#endif
95
96/* ======================================================================== */
97/* PSC fifo operations for isolating differences between 52xx and 512x */
98/* ======================================================================== */
99
100struct psc_ops {
101 void (*fifo_init)(struct uart_port *port);
102 int (*raw_rx_rdy)(struct uart_port *port);
103 int (*raw_tx_rdy)(struct uart_port *port);
104 int (*rx_rdy)(struct uart_port *port);
105 int (*tx_rdy)(struct uart_port *port);
106 int (*tx_empty)(struct uart_port *port);
107 void (*stop_rx)(struct uart_port *port);
108 void (*start_tx)(struct uart_port *port);
109 void (*stop_tx)(struct uart_port *port);
110 void (*rx_clr_irq)(struct uart_port *port);
111 void (*tx_clr_irq)(struct uart_port *port);
112 void (*write_char)(struct uart_port *port, unsigned char c);
113 unsigned char (*read_char)(struct uart_port *port);
114 void (*cw_disable_ints)(struct uart_port *port);
115 void (*cw_restore_ints)(struct uart_port *port);
116 unsigned int (*set_baudrate)(struct uart_port *port,
117 struct ktermios *new,
118 struct ktermios *old);
119 int (*clock)(struct uart_port *port, int enable);
120 int (*fifoc_init)(void);
121 void (*fifoc_uninit)(void);
122 void (*get_irq)(struct uart_port *, struct device_node *);
123 irqreturn_t (*handle_irq)(struct uart_port *port);
124};
125
126/* setting the prescaler and divisor reg is common for all chips */
127static inline void mpc52xx_set_divisor(struct mpc52xx_psc __iomem *psc,
128 u16 prescaler, unsigned int divisor)
129{
130 /* select prescaler */
131 out_be16(&psc->mpc52xx_psc_clock_select, prescaler);
132 out_8(&psc->ctur, divisor >> 8);
133 out_8(&psc->ctlr, divisor & 0xff);
134}
135
136#ifdef CONFIG_PPC_MPC52xx
137#define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
138static void mpc52xx_psc_fifo_init(struct uart_port *port)
139{
140 struct mpc52xx_psc __iomem *psc = PSC(port);
141 struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
142
143 out_8(&fifo->rfcntl, 0x00);
144 out_be16(&fifo->rfalarm, 0x1ff);
145 out_8(&fifo->tfcntl, 0x07);
146 out_be16(&fifo->tfalarm, 0x80);
147
148 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
149 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
150}
151
152static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
153{
154 return in_be16(&PSC(port)->mpc52xx_psc_status)
155 & MPC52xx_PSC_SR_RXRDY;
156}
157
158static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
159{
160 return in_be16(&PSC(port)->mpc52xx_psc_status)
161 & MPC52xx_PSC_SR_TXRDY;
162}
163
164
165static int mpc52xx_psc_rx_rdy(struct uart_port *port)
166{
167 return in_be16(&PSC(port)->mpc52xx_psc_isr)
168 & port->read_status_mask
169 & MPC52xx_PSC_IMR_RXRDY;
170}
171
172static int mpc52xx_psc_tx_rdy(struct uart_port *port)
173{
174 return in_be16(&PSC(port)->mpc52xx_psc_isr)
175 & port->read_status_mask
176 & MPC52xx_PSC_IMR_TXRDY;
177}
178
179static int mpc52xx_psc_tx_empty(struct uart_port *port)
180{
181 return in_be16(&PSC(port)->mpc52xx_psc_status)
182 & MPC52xx_PSC_SR_TXEMP;
183}
184
185static void mpc52xx_psc_start_tx(struct uart_port *port)
186{
187 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
188 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
189}
190
191static void mpc52xx_psc_stop_tx(struct uart_port *port)
192{
193 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
194 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
195}
196
197static void mpc52xx_psc_stop_rx(struct uart_port *port)
198{
199 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
200 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
201}
202
203static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
204{
205}
206
207static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
208{
209}
210
211static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
212{
213 out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
214}
215
216static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
217{
218 return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
219}
220
221static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
222{
223 out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
224}
225
226static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
227{
228 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
229}
230
231static unsigned int mpc5200_psc_set_baudrate(struct uart_port *port,
232 struct ktermios *new,
233 struct ktermios *old)
234{
235 unsigned int baud;
236 unsigned int divisor;
237
238 /* The 5200 has a fixed /32 prescaler, uartclk contains the ipb freq */
239 baud = uart_get_baud_rate(port, new, old,
240 port->uartclk / (32 * 0xffff) + 1,
241 port->uartclk / 32);
242 divisor = (port->uartclk + 16 * baud) / (32 * baud);
243
244 /* enable the /32 prescaler and set the divisor */
245 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
246 return baud;
247}
248
249static unsigned int mpc5200b_psc_set_baudrate(struct uart_port *port,
250 struct ktermios *new,
251 struct ktermios *old)
252{
253 unsigned int baud;
254 unsigned int divisor;
255 u16 prescaler;
256
257 /* The 5200B has a selectable /4 or /32 prescaler, uartclk contains the
258 * ipb freq */
259 baud = uart_get_baud_rate(port, new, old,
260 port->uartclk / (32 * 0xffff) + 1,
261 port->uartclk / 4);
262 divisor = (port->uartclk + 2 * baud) / (4 * baud);
263
264 /* select the proper prescaler and set the divisor */
265 if (divisor > 0xffff) {
266 divisor = (divisor + 4) / 8;
267 prescaler = 0xdd00; /* /32 */
268 } else
269 prescaler = 0xff00; /* /4 */
270 mpc52xx_set_divisor(PSC(port), prescaler, divisor);
271 return baud;
272}
273
274static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
275{
276 port->irqflags = IRQF_DISABLED;
277 port->irq = irq_of_parse_and_map(np, 0);
278}
279
280/* 52xx specific interrupt handler. The caller holds the port lock */
281static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
282{
283 return mpc5xxx_uart_process_int(port);
284}
285
286static struct psc_ops mpc52xx_psc_ops = {
287 .fifo_init = mpc52xx_psc_fifo_init,
288 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
289 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
290 .rx_rdy = mpc52xx_psc_rx_rdy,
291 .tx_rdy = mpc52xx_psc_tx_rdy,
292 .tx_empty = mpc52xx_psc_tx_empty,
293 .stop_rx = mpc52xx_psc_stop_rx,
294 .start_tx = mpc52xx_psc_start_tx,
295 .stop_tx = mpc52xx_psc_stop_tx,
296 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
297 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
298 .write_char = mpc52xx_psc_write_char,
299 .read_char = mpc52xx_psc_read_char,
300 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
301 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
302 .set_baudrate = mpc5200_psc_set_baudrate,
303 .get_irq = mpc52xx_psc_get_irq,
304 .handle_irq = mpc52xx_psc_handle_irq,
305};
306
307static struct psc_ops mpc5200b_psc_ops = {
308 .fifo_init = mpc52xx_psc_fifo_init,
309 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
310 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
311 .rx_rdy = mpc52xx_psc_rx_rdy,
312 .tx_rdy = mpc52xx_psc_tx_rdy,
313 .tx_empty = mpc52xx_psc_tx_empty,
314 .stop_rx = mpc52xx_psc_stop_rx,
315 .start_tx = mpc52xx_psc_start_tx,
316 .stop_tx = mpc52xx_psc_stop_tx,
317 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
318 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
319 .write_char = mpc52xx_psc_write_char,
320 .read_char = mpc52xx_psc_read_char,
321 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
322 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
323 .set_baudrate = mpc5200b_psc_set_baudrate,
324 .get_irq = mpc52xx_psc_get_irq,
325 .handle_irq = mpc52xx_psc_handle_irq,
326};
327
328#endif /* CONFIG_MPC52xx */
329
330#ifdef CONFIG_PPC_MPC512x
331#define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
332
333/* PSC FIFO Controller for mpc512x */
334struct psc_fifoc {
335 u32 fifoc_cmd;
336 u32 fifoc_int;
337 u32 fifoc_dma;
338 u32 fifoc_axe;
339 u32 fifoc_debug;
340};
341
342static struct psc_fifoc __iomem *psc_fifoc;
343static unsigned int psc_fifoc_irq;
344
345static void mpc512x_psc_fifo_init(struct uart_port *port)
346{
347 /* /32 prescaler */
348 out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
349
350 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
351 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
352 out_be32(&FIFO_512x(port)->txalarm, 1);
353 out_be32(&FIFO_512x(port)->tximr, 0);
354
355 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
356 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
357 out_be32(&FIFO_512x(port)->rxalarm, 1);
358 out_be32(&FIFO_512x(port)->rximr, 0);
359
360 out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
361 out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
362}
363
364static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
365{
366 return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
367}
368
369static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
370{
371 return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
372}
373
374static int mpc512x_psc_rx_rdy(struct uart_port *port)
375{
376 return in_be32(&FIFO_512x(port)->rxsr)
377 & in_be32(&FIFO_512x(port)->rximr)
378 & MPC512x_PSC_FIFO_ALARM;
379}
380
381static int mpc512x_psc_tx_rdy(struct uart_port *port)
382{
383 return in_be32(&FIFO_512x(port)->txsr)
384 & in_be32(&FIFO_512x(port)->tximr)
385 & MPC512x_PSC_FIFO_ALARM;
386}
387
388static int mpc512x_psc_tx_empty(struct uart_port *port)
389{
390 return in_be32(&FIFO_512x(port)->txsr)
391 & MPC512x_PSC_FIFO_EMPTY;
392}
393
394static void mpc512x_psc_stop_rx(struct uart_port *port)
395{
396 unsigned long rx_fifo_imr;
397
398 rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
399 rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
400 out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
401}
402
403static void mpc512x_psc_start_tx(struct uart_port *port)
404{
405 unsigned long tx_fifo_imr;
406
407 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
408 tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
409 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
410}
411
412static void mpc512x_psc_stop_tx(struct uart_port *port)
413{
414 unsigned long tx_fifo_imr;
415
416 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
417 tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
418 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
419}
420
421static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
422{
423 out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
424}
425
426static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
427{
428 out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
429}
430
431static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
432{
433 out_8(&FIFO_512x(port)->txdata_8, c);
434}
435
436static unsigned char mpc512x_psc_read_char(struct uart_port *port)
437{
438 return in_8(&FIFO_512x(port)->rxdata_8);
439}
440
441static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
442{
443 port->read_status_mask =
444 in_be32(&FIFO_512x(port)->tximr) << 16 |
445 in_be32(&FIFO_512x(port)->rximr);
446 out_be32(&FIFO_512x(port)->tximr, 0);
447 out_be32(&FIFO_512x(port)->rximr, 0);
448}
449
450static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
451{
452 out_be32(&FIFO_512x(port)->tximr,
453 (port->read_status_mask >> 16) & 0x7f);
454 out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
455}
456
457static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
458 struct ktermios *new,
459 struct ktermios *old)
460{
461 unsigned int baud;
462 unsigned int divisor;
463
464 /*
465 * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
466 * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
467 * Furthermore, it states that "After reset, the prescaler by 10
468 * for the UART mode is selected", but the reset register value is
469 * 0x0000 which means a /32 prescaler. This is wrong.
470 *
471 * In reality using /32 prescaler doesn't work, as it is not supported!
472 * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
473 * Chapter 4.1 PSC in UART Mode.
474 * Calculate with a /16 prescaler here.
475 */
476
477 /* uartclk contains the ips freq */
478 baud = uart_get_baud_rate(port, new, old,
479 port->uartclk / (16 * 0xffff) + 1,
480 port->uartclk / 16);
481 divisor = (port->uartclk + 8 * baud) / (16 * baud);
482
483 /* enable the /16 prescaler and set the divisor */
484 mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
485 return baud;
486}
487
488/* Init PSC FIFO Controller */
489static int __init mpc512x_psc_fifoc_init(void)
490{
491 struct device_node *np;
492
493 np = of_find_compatible_node(NULL, NULL,
494 "fsl,mpc5121-psc-fifo");
495 if (!np) {
496 pr_err("%s: Can't find FIFOC node\n", __func__);
497 return -ENODEV;
498 }
499
500 psc_fifoc = of_iomap(np, 0);
501 if (!psc_fifoc) {
502 pr_err("%s: Can't map FIFOC\n", __func__);
503 of_node_put(np);
504 return -ENODEV;
505 }
506
507 psc_fifoc_irq = irq_of_parse_and_map(np, 0);
508 of_node_put(np);
509 if (psc_fifoc_irq == NO_IRQ) {
510 pr_err("%s: Can't get FIFOC irq\n", __func__);
511 iounmap(psc_fifoc);
512 return -ENODEV;
513 }
514
515 return 0;
516}
517
518static void __exit mpc512x_psc_fifoc_uninit(void)
519{
520 iounmap(psc_fifoc);
521}
522
523/* 512x specific interrupt handler. The caller holds the port lock */
524static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
525{
526 unsigned long fifoc_int;
527 int psc_num;
528
529 /* Read pending PSC FIFOC interrupts */
530 fifoc_int = in_be32(&psc_fifoc->fifoc_int);
531
532 /* Check if it is an interrupt for this port */
533 psc_num = (port->mapbase & 0xf00) >> 8;
534 if (test_bit(psc_num, &fifoc_int) ||
535 test_bit(psc_num + 16, &fifoc_int))
536 return mpc5xxx_uart_process_int(port);
537
538 return IRQ_NONE;
539}
540
541static int mpc512x_psc_clock(struct uart_port *port, int enable)
542{
543 struct clk *psc_clk;
544 int psc_num;
545 char clk_name[10];
546
547 if (uart_console(port))
548 return 0;
549
550 psc_num = (port->mapbase & 0xf00) >> 8;
551 snprintf(clk_name, sizeof(clk_name), "psc%d_clk", psc_num);
552 psc_clk = clk_get(port->dev, clk_name);
553 if (IS_ERR(psc_clk)) {
554 dev_err(port->dev, "Failed to get PSC clock entry!\n");
555 return -ENODEV;
556 }
557
558 dev_dbg(port->dev, "%s %sable\n", clk_name, enable ? "en" : "dis");
559
560 if (enable)
561 clk_enable(psc_clk);
562 else
563 clk_disable(psc_clk);
564
565 return 0;
566}
567
568static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
569{
570 port->irqflags = IRQF_SHARED;
571 port->irq = psc_fifoc_irq;
572}
573
574static struct psc_ops mpc512x_psc_ops = {
575 .fifo_init = mpc512x_psc_fifo_init,
576 .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
577 .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
578 .rx_rdy = mpc512x_psc_rx_rdy,
579 .tx_rdy = mpc512x_psc_tx_rdy,
580 .tx_empty = mpc512x_psc_tx_empty,
581 .stop_rx = mpc512x_psc_stop_rx,
582 .start_tx = mpc512x_psc_start_tx,
583 .stop_tx = mpc512x_psc_stop_tx,
584 .rx_clr_irq = mpc512x_psc_rx_clr_irq,
585 .tx_clr_irq = mpc512x_psc_tx_clr_irq,
586 .write_char = mpc512x_psc_write_char,
587 .read_char = mpc512x_psc_read_char,
588 .cw_disable_ints = mpc512x_psc_cw_disable_ints,
589 .cw_restore_ints = mpc512x_psc_cw_restore_ints,
590 .set_baudrate = mpc512x_psc_set_baudrate,
591 .clock = mpc512x_psc_clock,
592 .fifoc_init = mpc512x_psc_fifoc_init,
593 .fifoc_uninit = mpc512x_psc_fifoc_uninit,
594 .get_irq = mpc512x_psc_get_irq,
595 .handle_irq = mpc512x_psc_handle_irq,
596};
597#endif
598
599static struct psc_ops *psc_ops;
600
601/* ======================================================================== */
602/* UART operations */
603/* ======================================================================== */
604
605static unsigned int
606mpc52xx_uart_tx_empty(struct uart_port *port)
607{
608 return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
609}
610
611static void
612mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
613{
614 if (mctrl & TIOCM_RTS)
615 out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
616 else
617 out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
618}
619
620static unsigned int
621mpc52xx_uart_get_mctrl(struct uart_port *port)
622{
623 unsigned int ret = TIOCM_DSR;
624 u8 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
625
626 if (!(status & MPC52xx_PSC_CTS))
627 ret |= TIOCM_CTS;
628 if (!(status & MPC52xx_PSC_DCD))
629 ret |= TIOCM_CAR;
630
631 return ret;
632}
633
634static void
635mpc52xx_uart_stop_tx(struct uart_port *port)
636{
637 /* port->lock taken by caller */
638 psc_ops->stop_tx(port);
639}
640
641static void
642mpc52xx_uart_start_tx(struct uart_port *port)
643{
644 /* port->lock taken by caller */
645 psc_ops->start_tx(port);
646}
647
648static void
649mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
650{
651 unsigned long flags;
652 spin_lock_irqsave(&port->lock, flags);
653
654 port->x_char = ch;
655 if (ch) {
656 /* Make sure tx interrupts are on */
657 /* Truly necessary ??? They should be anyway */
658 psc_ops->start_tx(port);
659 }
660
661 spin_unlock_irqrestore(&port->lock, flags);
662}
663
664static void
665mpc52xx_uart_stop_rx(struct uart_port *port)
666{
667 /* port->lock taken by caller */
668 psc_ops->stop_rx(port);
669}
670
671static void
672mpc52xx_uart_enable_ms(struct uart_port *port)
673{
674 struct mpc52xx_psc __iomem *psc = PSC(port);
675
676 /* clear D_*-bits by reading them */
677 in_8(&psc->mpc52xx_psc_ipcr);
678 /* enable CTS and DCD as IPC interrupts */
679 out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
680
681 port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
682 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
683}
684
685static void
686mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
687{
688 unsigned long flags;
689 spin_lock_irqsave(&port->lock, flags);
690
691 if (ctl == -1)
692 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
693 else
694 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
695
696 spin_unlock_irqrestore(&port->lock, flags);
697}
698
699static int
700mpc52xx_uart_startup(struct uart_port *port)
701{
702 struct mpc52xx_psc __iomem *psc = PSC(port);
703 int ret;
704
705 if (psc_ops->clock) {
706 ret = psc_ops->clock(port, 1);
707 if (ret)
708 return ret;
709 }
710
711 /* Request IRQ */
712 ret = request_irq(port->irq, mpc52xx_uart_int,
713 port->irqflags, "mpc52xx_psc_uart", port);
714 if (ret)
715 return ret;
716
717 /* Reset/activate the port, clear and enable interrupts */
718 out_8(&psc->command, MPC52xx_PSC_RST_RX);
719 out_8(&psc->command, MPC52xx_PSC_RST_TX);
720
721 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
722
723 psc_ops->fifo_init(port);
724
725 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
726 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
727
728 return 0;
729}
730
731static void
732mpc52xx_uart_shutdown(struct uart_port *port)
733{
734 struct mpc52xx_psc __iomem *psc = PSC(port);
735
736 /* Shut down the port. Leave TX active if on a console port */
737 out_8(&psc->command, MPC52xx_PSC_RST_RX);
738 if (!uart_console(port))
739 out_8(&psc->command, MPC52xx_PSC_RST_TX);
740
741 port->read_status_mask = 0;
742 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
743
744 if (psc_ops->clock)
745 psc_ops->clock(port, 0);
746
747 /* Release interrupt */
748 free_irq(port->irq, port);
749}
750
751static void
752mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
753 struct ktermios *old)
754{
755 struct mpc52xx_psc __iomem *psc = PSC(port);
756 unsigned long flags;
757 unsigned char mr1, mr2;
758 unsigned int j;
759 unsigned int baud;
760
761 /* Prepare what we're gonna write */
762 mr1 = 0;
763
764 switch (new->c_cflag & CSIZE) {
765 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
766 break;
767 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
768 break;
769 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
770 break;
771 case CS8:
772 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
773 }
774
775 if (new->c_cflag & PARENB) {
776 mr1 |= (new->c_cflag & PARODD) ?
777 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
778 } else
779 mr1 |= MPC52xx_PSC_MODE_PARNONE;
780
781
782 mr2 = 0;
783
784 if (new->c_cflag & CSTOPB)
785 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
786 else
787 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
788 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
789 MPC52xx_PSC_MODE_ONE_STOP;
790
791 if (new->c_cflag & CRTSCTS) {
792 mr1 |= MPC52xx_PSC_MODE_RXRTS;
793 mr2 |= MPC52xx_PSC_MODE_TXCTS;
794 }
795
796 /* Get the lock */
797 spin_lock_irqsave(&port->lock, flags);
798
799 /* Do our best to flush TX & RX, so we don't lose anything */
800 /* But we don't wait indefinitely ! */
801 j = 5000000; /* Maximum wait */
802 /* FIXME Can't receive chars since set_termios might be called at early
803 * boot for the console, all stuff is not yet ready to receive at that
804 * time and that just makes the kernel oops */
805 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
806 while (!mpc52xx_uart_tx_empty(port) && --j)
807 udelay(1);
808
809 if (!j)
810 printk(KERN_ERR "mpc52xx_uart.c: "
811 "Unable to flush RX & TX fifos in-time in set_termios."
812 "Some chars may have been lost.\n");
813
814 /* Reset the TX & RX */
815 out_8(&psc->command, MPC52xx_PSC_RST_RX);
816 out_8(&psc->command, MPC52xx_PSC_RST_TX);
817
818 /* Send new mode settings */
819 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
820 out_8(&psc->mode, mr1);
821 out_8(&psc->mode, mr2);
822 baud = psc_ops->set_baudrate(port, new, old);
823
824 /* Update the per-port timeout */
825 uart_update_timeout(port, new->c_cflag, baud);
826
827 if (UART_ENABLE_MS(port, new->c_cflag))
828 mpc52xx_uart_enable_ms(port);
829
830 /* Reenable TX & RX */
831 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
832 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
833
834 /* We're all set, release the lock */
835 spin_unlock_irqrestore(&port->lock, flags);
836}
837
838static const char *
839mpc52xx_uart_type(struct uart_port *port)
840{
841 /*
842 * We keep using PORT_MPC52xx for historic reasons although it applies
843 * for MPC512x, too, but print "MPC5xxx" to not irritate users
844 */
845 return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
846}
847
848static void
849mpc52xx_uart_release_port(struct uart_port *port)
850{
851 /* remapped by us ? */
852 if (port->flags & UPF_IOREMAP) {
853 iounmap(port->membase);
854 port->membase = NULL;
855 }
856
857 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
858}
859
860static int
861mpc52xx_uart_request_port(struct uart_port *port)
862{
863 int err;
864
865 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
866 port->membase = ioremap(port->mapbase,
867 sizeof(struct mpc52xx_psc));
868
869 if (!port->membase)
870 return -EINVAL;
871
872 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
873 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
874
875 if (err && (port->flags & UPF_IOREMAP)) {
876 iounmap(port->membase);
877 port->membase = NULL;
878 }
879
880 return err;
881}
882
883static void
884mpc52xx_uart_config_port(struct uart_port *port, int flags)
885{
886 if ((flags & UART_CONFIG_TYPE)
887 && (mpc52xx_uart_request_port(port) == 0))
888 port->type = PORT_MPC52xx;
889}
890
891static int
892mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
893{
894 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
895 return -EINVAL;
896
897 if ((ser->irq != port->irq) ||
898 (ser->io_type != UPIO_MEM) ||
899 (ser->baud_base != port->uartclk) ||
900 (ser->iomem_base != (void *)port->mapbase) ||
901 (ser->hub6 != 0))
902 return -EINVAL;
903
904 return 0;
905}
906
907
908static struct uart_ops mpc52xx_uart_ops = {
909 .tx_empty = mpc52xx_uart_tx_empty,
910 .set_mctrl = mpc52xx_uart_set_mctrl,
911 .get_mctrl = mpc52xx_uart_get_mctrl,
912 .stop_tx = mpc52xx_uart_stop_tx,
913 .start_tx = mpc52xx_uart_start_tx,
914 .send_xchar = mpc52xx_uart_send_xchar,
915 .stop_rx = mpc52xx_uart_stop_rx,
916 .enable_ms = mpc52xx_uart_enable_ms,
917 .break_ctl = mpc52xx_uart_break_ctl,
918 .startup = mpc52xx_uart_startup,
919 .shutdown = mpc52xx_uart_shutdown,
920 .set_termios = mpc52xx_uart_set_termios,
921/* .pm = mpc52xx_uart_pm, Not supported yet */
922/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
923 .type = mpc52xx_uart_type,
924 .release_port = mpc52xx_uart_release_port,
925 .request_port = mpc52xx_uart_request_port,
926 .config_port = mpc52xx_uart_config_port,
927 .verify_port = mpc52xx_uart_verify_port
928};
929
930
931/* ======================================================================== */
932/* Interrupt handling */
933/* ======================================================================== */
934
935static inline int
936mpc52xx_uart_int_rx_chars(struct uart_port *port)
937{
938 struct tty_struct *tty = port->state->port.tty;
939 unsigned char ch, flag;
940 unsigned short status;
941
942 /* While we can read, do so ! */
943 while (psc_ops->raw_rx_rdy(port)) {
944 /* Get the char */
945 ch = psc_ops->read_char(port);
946
947 /* Handle sysreq char */
948#ifdef SUPPORT_SYSRQ
949 if (uart_handle_sysrq_char(port, ch)) {
950 port->sysrq = 0;
951 continue;
952 }
953#endif
954
955 /* Store it */
956
957 flag = TTY_NORMAL;
958 port->icount.rx++;
959
960 status = in_be16(&PSC(port)->mpc52xx_psc_status);
961
962 if (status & (MPC52xx_PSC_SR_PE |
963 MPC52xx_PSC_SR_FE |
964 MPC52xx_PSC_SR_RB)) {
965
966 if (status & MPC52xx_PSC_SR_RB) {
967 flag = TTY_BREAK;
968 uart_handle_break(port);
969 port->icount.brk++;
970 } else if (status & MPC52xx_PSC_SR_PE) {
971 flag = TTY_PARITY;
972 port->icount.parity++;
973 }
974 else if (status & MPC52xx_PSC_SR_FE) {
975 flag = TTY_FRAME;
976 port->icount.frame++;
977 }
978
979 /* Clear error condition */
980 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
981
982 }
983 tty_insert_flip_char(tty, ch, flag);
984 if (status & MPC52xx_PSC_SR_OE) {
985 /*
986 * Overrun is special, since it's
987 * reported immediately, and doesn't
988 * affect the current character
989 */
990 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
991 port->icount.overrun++;
992 }
993 }
994
995 spin_unlock(&port->lock);
996 tty_flip_buffer_push(tty);
997 spin_lock(&port->lock);
998
999 return psc_ops->raw_rx_rdy(port);
1000}
1001
1002static inline int
1003mpc52xx_uart_int_tx_chars(struct uart_port *port)
1004{
1005 struct circ_buf *xmit = &port->state->xmit;
1006
1007 /* Process out of band chars */
1008 if (port->x_char) {
1009 psc_ops->write_char(port, port->x_char);
1010 port->icount.tx++;
1011 port->x_char = 0;
1012 return 1;
1013 }
1014
1015 /* Nothing to do ? */
1016 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
1017 mpc52xx_uart_stop_tx(port);
1018 return 0;
1019 }
1020
1021 /* Send chars */
1022 while (psc_ops->raw_tx_rdy(port)) {
1023 psc_ops->write_char(port, xmit->buf[xmit->tail]);
1024 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1025 port->icount.tx++;
1026 if (uart_circ_empty(xmit))
1027 break;
1028 }
1029
1030 /* Wake up */
1031 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1032 uart_write_wakeup(port);
1033
1034 /* Maybe we're done after all */
1035 if (uart_circ_empty(xmit)) {
1036 mpc52xx_uart_stop_tx(port);
1037 return 0;
1038 }
1039
1040 return 1;
1041}
1042
1043static irqreturn_t
1044mpc5xxx_uart_process_int(struct uart_port *port)
1045{
1046 unsigned long pass = ISR_PASS_LIMIT;
1047 unsigned int keepgoing;
1048 u8 status;
1049
1050 /* While we have stuff to do, we continue */
1051 do {
1052 /* If we don't find anything to do, we stop */
1053 keepgoing = 0;
1054
1055 psc_ops->rx_clr_irq(port);
1056 if (psc_ops->rx_rdy(port))
1057 keepgoing |= mpc52xx_uart_int_rx_chars(port);
1058
1059 psc_ops->tx_clr_irq(port);
1060 if (psc_ops->tx_rdy(port))
1061 keepgoing |= mpc52xx_uart_int_tx_chars(port);
1062
1063 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
1064 if (status & MPC52xx_PSC_D_DCD)
1065 uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
1066
1067 if (status & MPC52xx_PSC_D_CTS)
1068 uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
1069
1070 /* Limit number of iteration */
1071 if (!(--pass))
1072 keepgoing = 0;
1073
1074 } while (keepgoing);
1075
1076 return IRQ_HANDLED;
1077}
1078
1079static irqreturn_t
1080mpc52xx_uart_int(int irq, void *dev_id)
1081{
1082 struct uart_port *port = dev_id;
1083 irqreturn_t ret;
1084
1085 spin_lock(&port->lock);
1086
1087 ret = psc_ops->handle_irq(port);
1088
1089 spin_unlock(&port->lock);
1090
1091 return ret;
1092}
1093
1094/* ======================================================================== */
1095/* Console ( if applicable ) */
1096/* ======================================================================== */
1097
1098#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
1099
1100static void __init
1101mpc52xx_console_get_options(struct uart_port *port,
1102 int *baud, int *parity, int *bits, int *flow)
1103{
1104 struct mpc52xx_psc __iomem *psc = PSC(port);
1105 unsigned char mr1;
1106
1107 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
1108
1109 /* Read the mode registers */
1110 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
1111 mr1 = in_8(&psc->mode);
1112
1113 /* CT{U,L}R are write-only ! */
1114 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1115
1116 /* Parse them */
1117 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
1118 case MPC52xx_PSC_MODE_5_BITS:
1119 *bits = 5;
1120 break;
1121 case MPC52xx_PSC_MODE_6_BITS:
1122 *bits = 6;
1123 break;
1124 case MPC52xx_PSC_MODE_7_BITS:
1125 *bits = 7;
1126 break;
1127 case MPC52xx_PSC_MODE_8_BITS:
1128 default:
1129 *bits = 8;
1130 }
1131
1132 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
1133 *parity = 'n';
1134 else
1135 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
1136}
1137
1138static void
1139mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
1140{
1141 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1142 unsigned int i, j;
1143
1144 /* Disable interrupts */
1145 psc_ops->cw_disable_ints(port);
1146
1147 /* Wait the TX buffer to be empty */
1148 j = 5000000; /* Maximum wait */
1149 while (!mpc52xx_uart_tx_empty(port) && --j)
1150 udelay(1);
1151
1152 /* Write all the chars */
1153 for (i = 0; i < count; i++, s++) {
1154 /* Line return handling */
1155 if (*s == '\n')
1156 psc_ops->write_char(port, '\r');
1157
1158 /* Send the char */
1159 psc_ops->write_char(port, *s);
1160
1161 /* Wait the TX buffer to be empty */
1162 j = 20000; /* Maximum wait */
1163 while (!mpc52xx_uart_tx_empty(port) && --j)
1164 udelay(1);
1165 }
1166
1167 /* Restore interrupt state */
1168 psc_ops->cw_restore_ints(port);
1169}
1170
1171
1172static int __init
1173mpc52xx_console_setup(struct console *co, char *options)
1174{
1175 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1176 struct device_node *np = mpc52xx_uart_nodes[co->index];
1177 unsigned int uartclk;
1178 struct resource res;
1179 int ret;
1180
1181 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1182 int bits = 8;
1183 int parity = 'n';
1184 int flow = 'n';
1185
1186 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
1187 co, co->index, options);
1188
1189 if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
1190 pr_debug("PSC%x out of range\n", co->index);
1191 return -EINVAL;
1192 }
1193
1194 if (!np) {
1195 pr_debug("PSC%x not found in device tree\n", co->index);
1196 return -EINVAL;
1197 }
1198
1199 pr_debug("Console on ttyPSC%x is %s\n",
1200 co->index, mpc52xx_uart_nodes[co->index]->full_name);
1201
1202 /* Fetch register locations */
1203 ret = of_address_to_resource(np, 0, &res);
1204 if (ret) {
1205 pr_debug("Could not get resources for PSC%x\n", co->index);
1206 return ret;
1207 }
1208
1209 uartclk = mpc5xxx_get_bus_frequency(np);
1210 if (uartclk == 0) {
1211 pr_debug("Could not find uart clock frequency!\n");
1212 return -EINVAL;
1213 }
1214
1215 /* Basic port init. Needed since we use some uart_??? func before
1216 * real init for early access */
1217 spin_lock_init(&port->lock);
1218 port->uartclk = uartclk;
1219 port->ops = &mpc52xx_uart_ops;
1220 port->mapbase = res.start;
1221 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
1222 port->irq = irq_of_parse_and_map(np, 0);
1223
1224 if (port->membase == NULL)
1225 return -EINVAL;
1226
1227 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
1228 (void *)port->mapbase, port->membase,
1229 port->irq, port->uartclk);
1230
1231 /* Setup the port parameters accoding to options */
1232 if (options)
1233 uart_parse_options(options, &baud, &parity, &bits, &flow);
1234 else
1235 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
1236
1237 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
1238 baud, bits, parity, flow);
1239
1240 return uart_set_options(port, co, baud, parity, bits, flow);
1241}
1242
1243
1244static struct uart_driver mpc52xx_uart_driver;
1245
1246static struct console mpc52xx_console = {
1247 .name = "ttyPSC",
1248 .write = mpc52xx_console_write,
1249 .device = uart_console_device,
1250 .setup = mpc52xx_console_setup,
1251 .flags = CON_PRINTBUFFER,
1252 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
1253 .data = &mpc52xx_uart_driver,
1254};
1255
1256
1257static int __init
1258mpc52xx_console_init(void)
1259{
1260 mpc52xx_uart_of_enumerate();
1261 register_console(&mpc52xx_console);
1262 return 0;
1263}
1264
1265console_initcall(mpc52xx_console_init);
1266
1267#define MPC52xx_PSC_CONSOLE &mpc52xx_console
1268#else
1269#define MPC52xx_PSC_CONSOLE NULL
1270#endif
1271
1272
1273/* ======================================================================== */
1274/* UART Driver */
1275/* ======================================================================== */
1276
1277static struct uart_driver mpc52xx_uart_driver = {
1278 .driver_name = "mpc52xx_psc_uart",
1279 .dev_name = "ttyPSC",
1280 .major = SERIAL_PSC_MAJOR,
1281 .minor = SERIAL_PSC_MINOR,
1282 .nr = MPC52xx_PSC_MAXNUM,
1283 .cons = MPC52xx_PSC_CONSOLE,
1284};
1285
1286/* ======================================================================== */
1287/* OF Platform Driver */
1288/* ======================================================================== */
1289
1290static struct of_device_id mpc52xx_uart_of_match[] = {
1291#ifdef CONFIG_PPC_MPC52xx
1292 { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
1293 { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1294 /* binding used by old lite5200 device trees: */
1295 { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1296 /* binding used by efika: */
1297 { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
1298#endif
1299#ifdef CONFIG_PPC_MPC512x
1300 { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
1301#endif
1302 {},
1303};
1304
1305static int __devinit mpc52xx_uart_of_probe(struct platform_device *op)
1306{
1307 int idx = -1;
1308 unsigned int uartclk;
1309 struct uart_port *port = NULL;
1310 struct resource res;
1311 int ret;
1312
1313 /* Check validity & presence */
1314 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
1315 if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
1316 break;
1317 if (idx >= MPC52xx_PSC_MAXNUM)
1318 return -EINVAL;
1319 pr_debug("Found %s assigned to ttyPSC%x\n",
1320 mpc52xx_uart_nodes[idx]->full_name, idx);
1321
1322 /* set the uart clock to the input clock of the psc, the different
1323 * prescalers are taken into account in the set_baudrate() methods
1324 * of the respective chip */
1325 uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
1326 if (uartclk == 0) {
1327 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
1328 return -EINVAL;
1329 }
1330
1331 /* Init the port structure */
1332 port = &mpc52xx_uart_ports[idx];
1333
1334 spin_lock_init(&port->lock);
1335 port->uartclk = uartclk;
1336 port->fifosize = 512;
1337 port->iotype = UPIO_MEM;
1338 port->flags = UPF_BOOT_AUTOCONF |
1339 (uart_console(port) ? 0 : UPF_IOREMAP);
1340 port->line = idx;
1341 port->ops = &mpc52xx_uart_ops;
1342 port->dev = &op->dev;
1343
1344 /* Search for IRQ and mapbase */
1345 ret = of_address_to_resource(op->dev.of_node, 0, &res);
1346 if (ret)
1347 return ret;
1348
1349 port->mapbase = res.start;
1350 if (!port->mapbase) {
1351 dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
1352 return -EINVAL;
1353 }
1354
1355 psc_ops->get_irq(port, op->dev.of_node);
1356 if (port->irq == NO_IRQ) {
1357 dev_dbg(&op->dev, "Could not get irq\n");
1358 return -EINVAL;
1359 }
1360
1361 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1362 (void *)port->mapbase, port->irq, port->uartclk);
1363
1364 /* Add the port to the uart sub-system */
1365 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1366 if (ret)
1367 return ret;
1368
1369 dev_set_drvdata(&op->dev, (void *)port);
1370 return 0;
1371}
1372
1373static int
1374mpc52xx_uart_of_remove(struct platform_device *op)
1375{
1376 struct uart_port *port = dev_get_drvdata(&op->dev);
1377 dev_set_drvdata(&op->dev, NULL);
1378
1379 if (port)
1380 uart_remove_one_port(&mpc52xx_uart_driver, port);
1381
1382 return 0;
1383}
1384
1385#ifdef CONFIG_PM
1386static int
1387mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
1388{
1389 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1390
1391 if (port)
1392 uart_suspend_port(&mpc52xx_uart_driver, port);
1393
1394 return 0;
1395}
1396
1397static int
1398mpc52xx_uart_of_resume(struct platform_device *op)
1399{
1400 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1401
1402 if (port)
1403 uart_resume_port(&mpc52xx_uart_driver, port);
1404
1405 return 0;
1406}
1407#endif
1408
1409static void
1410mpc52xx_uart_of_assign(struct device_node *np)
1411{
1412 int i;
1413
1414 /* Find the first free PSC number */
1415 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1416 if (mpc52xx_uart_nodes[i] == NULL) {
1417 of_node_get(np);
1418 mpc52xx_uart_nodes[i] = np;
1419 return;
1420 }
1421 }
1422}
1423
1424static void
1425mpc52xx_uart_of_enumerate(void)
1426{
1427 static int enum_done;
1428 struct device_node *np;
1429 const struct of_device_id *match;
1430 int i;
1431
1432 if (enum_done)
1433 return;
1434
1435 /* Assign index to each PSC in device tree */
1436 for_each_matching_node(np, mpc52xx_uart_of_match) {
1437 match = of_match_node(mpc52xx_uart_of_match, np);
1438 psc_ops = match->data;
1439 mpc52xx_uart_of_assign(np);
1440 }
1441
1442 enum_done = 1;
1443
1444 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1445 if (mpc52xx_uart_nodes[i])
1446 pr_debug("%s assigned to ttyPSC%x\n",
1447 mpc52xx_uart_nodes[i]->full_name, i);
1448 }
1449}
1450
1451MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1452
1453static struct platform_driver mpc52xx_uart_of_driver = {
1454 .probe = mpc52xx_uart_of_probe,
1455 .remove = mpc52xx_uart_of_remove,
1456#ifdef CONFIG_PM
1457 .suspend = mpc52xx_uart_of_suspend,
1458 .resume = mpc52xx_uart_of_resume,
1459#endif
1460 .driver = {
1461 .name = "mpc52xx-psc-uart",
1462 .owner = THIS_MODULE,
1463 .of_match_table = mpc52xx_uart_of_match,
1464 },
1465};
1466
1467
1468/* ======================================================================== */
1469/* Module */
1470/* ======================================================================== */
1471
1472static int __init
1473mpc52xx_uart_init(void)
1474{
1475 int ret;
1476
1477 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1478
1479 ret = uart_register_driver(&mpc52xx_uart_driver);
1480 if (ret) {
1481 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1482 __FILE__, ret);
1483 return ret;
1484 }
1485
1486 mpc52xx_uart_of_enumerate();
1487
1488 /*
1489 * Map the PSC FIFO Controller and init if on MPC512x.
1490 */
1491 if (psc_ops && psc_ops->fifoc_init) {
1492 ret = psc_ops->fifoc_init();
1493 if (ret)
1494 return ret;
1495 }
1496
1497 ret = platform_driver_register(&mpc52xx_uart_of_driver);
1498 if (ret) {
1499 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1500 __FILE__, ret);
1501 uart_unregister_driver(&mpc52xx_uart_driver);
1502 return ret;
1503 }
1504
1505 return 0;
1506}
1507
1508static void __exit
1509mpc52xx_uart_exit(void)
1510{
1511 if (psc_ops->fifoc_uninit)
1512 psc_ops->fifoc_uninit();
1513
1514 platform_driver_unregister(&mpc52xx_uart_of_driver);
1515 uart_unregister_driver(&mpc52xx_uart_driver);
1516}
1517
1518
1519module_init(mpc52xx_uart_init);
1520module_exit(mpc52xx_uart_exit);
1521
1522MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1523MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1524MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/mpsc.c b/drivers/tty/serial/mpsc.c
new file mode 100644
index 000000000000..6a9c6605666a
--- /dev/null
+++ b/drivers/tty/serial/mpsc.c
@@ -0,0 +1,2159 @@
1/*
2 * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
3 * GT64260, MV64340, MV64360, GT96100, ... ).
4 *
5 * Author: Mark A. Greer <mgreer@mvista.com>
6 *
7 * Based on an old MPSC driver that was in the linuxppc tree. It appears to
8 * have been created by Chris Zankel (formerly of MontaVista) but there
9 * is no proper Copyright so I'm not sure. Apparently, parts were also
10 * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
11 * by Russell King.
12 *
13 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
14 * the terms of the GNU General Public License version 2. This program
15 * is licensed "as is" without any warranty of any kind, whether express
16 * or implied.
17 */
18/*
19 * The MPSC interface is much like a typical network controller's interface.
20 * That is, you set up separate rings of descriptors for transmitting and
21 * receiving data. There is also a pool of buffers with (one buffer per
22 * descriptor) that incoming data are dma'd into or outgoing data are dma'd
23 * out of.
24 *
25 * The MPSC requires two other controllers to be able to work. The Baud Rate
26 * Generator (BRG) provides a clock at programmable frequencies which determines
27 * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
28 * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
29 * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
30 * transmit and receive "engines" going (i.e., indicate data has been
31 * transmitted or received).
32 *
33 * NOTES:
34 *
35 * 1) Some chips have an erratum where several regs cannot be
36 * read. To work around that, we keep a local copy of those regs in
37 * 'mpsc_port_info'.
38 *
39 * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
40 * accesses system mem with coherency enabled. For that reason, the driver
41 * assumes that coherency for that ctlr has been disabled. This means
42 * that when in a cache coherent system, the driver has to manually manage
43 * the data cache on the areas that it touches because the dma_* macro are
44 * basically no-ops.
45 *
46 * 3) There is an erratum (on PPC) where you can't use the instruction to do
47 * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
48 * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
49 *
50 * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
51 */
52
53
54#if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
55#define SUPPORT_SYSRQ
56#endif
57
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/tty.h>
61#include <linux/tty_flip.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/console.h>
65#include <linux/sysrq.h>
66#include <linux/serial.h>
67#include <linux/serial_core.h>
68#include <linux/delay.h>
69#include <linux/device.h>
70#include <linux/dma-mapping.h>
71#include <linux/mv643xx.h>
72#include <linux/platform_device.h>
73#include <linux/gfp.h>
74
75#include <asm/io.h>
76#include <asm/irq.h>
77
78#define MPSC_NUM_CTLRS 2
79
80/*
81 * Descriptors and buffers must be cache line aligned.
82 * Buffers lengths must be multiple of cache line size.
83 * Number of Tx & Rx descriptors must be powers of 2.
84 */
85#define MPSC_RXR_ENTRIES 32
86#define MPSC_RXRE_SIZE dma_get_cache_alignment()
87#define MPSC_RXR_SIZE (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
88#define MPSC_RXBE_SIZE dma_get_cache_alignment()
89#define MPSC_RXB_SIZE (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
90
91#define MPSC_TXR_ENTRIES 32
92#define MPSC_TXRE_SIZE dma_get_cache_alignment()
93#define MPSC_TXR_SIZE (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
94#define MPSC_TXBE_SIZE dma_get_cache_alignment()
95#define MPSC_TXB_SIZE (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
96
97#define MPSC_DMA_ALLOC_SIZE (MPSC_RXR_SIZE + MPSC_RXB_SIZE + MPSC_TXR_SIZE \
98 + MPSC_TXB_SIZE + dma_get_cache_alignment() /* for alignment */)
99
100/* Rx and Tx Ring entry descriptors -- assume entry size is <= cacheline size */
101struct mpsc_rx_desc {
102 u16 bufsize;
103 u16 bytecnt;
104 u32 cmdstat;
105 u32 link;
106 u32 buf_ptr;
107} __attribute((packed));
108
109struct mpsc_tx_desc {
110 u16 bytecnt;
111 u16 shadow;
112 u32 cmdstat;
113 u32 link;
114 u32 buf_ptr;
115} __attribute((packed));
116
117/*
118 * Some regs that have the erratum that you can't read them are are shared
119 * between the two MPSC controllers. This struct contains those shared regs.
120 */
121struct mpsc_shared_regs {
122 phys_addr_t mpsc_routing_base_p;
123 phys_addr_t sdma_intr_base_p;
124
125 void __iomem *mpsc_routing_base;
126 void __iomem *sdma_intr_base;
127
128 u32 MPSC_MRR_m;
129 u32 MPSC_RCRR_m;
130 u32 MPSC_TCRR_m;
131 u32 SDMA_INTR_CAUSE_m;
132 u32 SDMA_INTR_MASK_m;
133};
134
135/* The main driver data structure */
136struct mpsc_port_info {
137 struct uart_port port; /* Overlay uart_port structure */
138
139 /* Internal driver state for this ctlr */
140 u8 ready;
141 u8 rcv_data;
142 tcflag_t c_iflag; /* save termios->c_iflag */
143 tcflag_t c_cflag; /* save termios->c_cflag */
144
145 /* Info passed in from platform */
146 u8 mirror_regs; /* Need to mirror regs? */
147 u8 cache_mgmt; /* Need manual cache mgmt? */
148 u8 brg_can_tune; /* BRG has baud tuning? */
149 u32 brg_clk_src;
150 u16 mpsc_max_idle;
151 int default_baud;
152 int default_bits;
153 int default_parity;
154 int default_flow;
155
156 /* Physical addresses of various blocks of registers (from platform) */
157 phys_addr_t mpsc_base_p;
158 phys_addr_t sdma_base_p;
159 phys_addr_t brg_base_p;
160
161 /* Virtual addresses of various blocks of registers (from platform) */
162 void __iomem *mpsc_base;
163 void __iomem *sdma_base;
164 void __iomem *brg_base;
165
166 /* Descriptor ring and buffer allocations */
167 void *dma_region;
168 dma_addr_t dma_region_p;
169
170 dma_addr_t rxr; /* Rx descriptor ring */
171 dma_addr_t rxr_p; /* Phys addr of rxr */
172 u8 *rxb; /* Rx Ring I/O buf */
173 u8 *rxb_p; /* Phys addr of rxb */
174 u32 rxr_posn; /* First desc w/ Rx data */
175
176 dma_addr_t txr; /* Tx descriptor ring */
177 dma_addr_t txr_p; /* Phys addr of txr */
178 u8 *txb; /* Tx Ring I/O buf */
179 u8 *txb_p; /* Phys addr of txb */
180 int txr_head; /* Where new data goes */
181 int txr_tail; /* Where sent data comes off */
182 spinlock_t tx_lock; /* transmit lock */
183
184 /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
185 u32 MPSC_MPCR_m;
186 u32 MPSC_CHR_1_m;
187 u32 MPSC_CHR_2_m;
188 u32 MPSC_CHR_10_m;
189 u32 BRG_BCR_m;
190 struct mpsc_shared_regs *shared_regs;
191};
192
193/* Hooks to platform-specific code */
194int mpsc_platform_register_driver(void);
195void mpsc_platform_unregister_driver(void);
196
197/* Hooks back in to mpsc common to be called by platform-specific code */
198struct mpsc_port_info *mpsc_device_probe(int index);
199struct mpsc_port_info *mpsc_device_remove(int index);
200
201/* Main MPSC Configuration Register Offsets */
202#define MPSC_MMCRL 0x0000
203#define MPSC_MMCRH 0x0004
204#define MPSC_MPCR 0x0008
205#define MPSC_CHR_1 0x000c
206#define MPSC_CHR_2 0x0010
207#define MPSC_CHR_3 0x0014
208#define MPSC_CHR_4 0x0018
209#define MPSC_CHR_5 0x001c
210#define MPSC_CHR_6 0x0020
211#define MPSC_CHR_7 0x0024
212#define MPSC_CHR_8 0x0028
213#define MPSC_CHR_9 0x002c
214#define MPSC_CHR_10 0x0030
215#define MPSC_CHR_11 0x0034
216
217#define MPSC_MPCR_FRZ (1 << 9)
218#define MPSC_MPCR_CL_5 0
219#define MPSC_MPCR_CL_6 1
220#define MPSC_MPCR_CL_7 2
221#define MPSC_MPCR_CL_8 3
222#define MPSC_MPCR_SBL_1 0
223#define MPSC_MPCR_SBL_2 1
224
225#define MPSC_CHR_2_TEV (1<<1)
226#define MPSC_CHR_2_TA (1<<7)
227#define MPSC_CHR_2_TTCS (1<<9)
228#define MPSC_CHR_2_REV (1<<17)
229#define MPSC_CHR_2_RA (1<<23)
230#define MPSC_CHR_2_CRD (1<<25)
231#define MPSC_CHR_2_EH (1<<31)
232#define MPSC_CHR_2_PAR_ODD 0
233#define MPSC_CHR_2_PAR_SPACE 1
234#define MPSC_CHR_2_PAR_EVEN 2
235#define MPSC_CHR_2_PAR_MARK 3
236
237/* MPSC Signal Routing */
238#define MPSC_MRR 0x0000
239#define MPSC_RCRR 0x0004
240#define MPSC_TCRR 0x0008
241
242/* Serial DMA Controller Interface Registers */
243#define SDMA_SDC 0x0000
244#define SDMA_SDCM 0x0008
245#define SDMA_RX_DESC 0x0800
246#define SDMA_RX_BUF_PTR 0x0808
247#define SDMA_SCRDP 0x0810
248#define SDMA_TX_DESC 0x0c00
249#define SDMA_SCTDP 0x0c10
250#define SDMA_SFTDP 0x0c14
251
252#define SDMA_DESC_CMDSTAT_PE (1<<0)
253#define SDMA_DESC_CMDSTAT_CDL (1<<1)
254#define SDMA_DESC_CMDSTAT_FR (1<<3)
255#define SDMA_DESC_CMDSTAT_OR (1<<6)
256#define SDMA_DESC_CMDSTAT_BR (1<<9)
257#define SDMA_DESC_CMDSTAT_MI (1<<10)
258#define SDMA_DESC_CMDSTAT_A (1<<11)
259#define SDMA_DESC_CMDSTAT_AM (1<<12)
260#define SDMA_DESC_CMDSTAT_CT (1<<13)
261#define SDMA_DESC_CMDSTAT_C (1<<14)
262#define SDMA_DESC_CMDSTAT_ES (1<<15)
263#define SDMA_DESC_CMDSTAT_L (1<<16)
264#define SDMA_DESC_CMDSTAT_F (1<<17)
265#define SDMA_DESC_CMDSTAT_P (1<<18)
266#define SDMA_DESC_CMDSTAT_EI (1<<23)
267#define SDMA_DESC_CMDSTAT_O (1<<31)
268
269#define SDMA_DESC_DFLT (SDMA_DESC_CMDSTAT_O \
270 | SDMA_DESC_CMDSTAT_EI)
271
272#define SDMA_SDC_RFT (1<<0)
273#define SDMA_SDC_SFM (1<<1)
274#define SDMA_SDC_BLMR (1<<6)
275#define SDMA_SDC_BLMT (1<<7)
276#define SDMA_SDC_POVR (1<<8)
277#define SDMA_SDC_RIFB (1<<9)
278
279#define SDMA_SDCM_ERD (1<<7)
280#define SDMA_SDCM_AR (1<<15)
281#define SDMA_SDCM_STD (1<<16)
282#define SDMA_SDCM_TXD (1<<23)
283#define SDMA_SDCM_AT (1<<31)
284
285#define SDMA_0_CAUSE_RXBUF (1<<0)
286#define SDMA_0_CAUSE_RXERR (1<<1)
287#define SDMA_0_CAUSE_TXBUF (1<<2)
288#define SDMA_0_CAUSE_TXEND (1<<3)
289#define SDMA_1_CAUSE_RXBUF (1<<8)
290#define SDMA_1_CAUSE_RXERR (1<<9)
291#define SDMA_1_CAUSE_TXBUF (1<<10)
292#define SDMA_1_CAUSE_TXEND (1<<11)
293
294#define SDMA_CAUSE_RX_MASK (SDMA_0_CAUSE_RXBUF | SDMA_0_CAUSE_RXERR \
295 | SDMA_1_CAUSE_RXBUF | SDMA_1_CAUSE_RXERR)
296#define SDMA_CAUSE_TX_MASK (SDMA_0_CAUSE_TXBUF | SDMA_0_CAUSE_TXEND \
297 | SDMA_1_CAUSE_TXBUF | SDMA_1_CAUSE_TXEND)
298
299/* SDMA Interrupt registers */
300#define SDMA_INTR_CAUSE 0x0000
301#define SDMA_INTR_MASK 0x0080
302
303/* Baud Rate Generator Interface Registers */
304#define BRG_BCR 0x0000
305#define BRG_BTR 0x0004
306
307/*
308 * Define how this driver is known to the outside (we've been assigned a
309 * range on the "Low-density serial ports" major).
310 */
311#define MPSC_MAJOR 204
312#define MPSC_MINOR_START 44
313#define MPSC_DRIVER_NAME "MPSC"
314#define MPSC_DEV_NAME "ttyMM"
315#define MPSC_VERSION "1.00"
316
317static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
318static struct mpsc_shared_regs mpsc_shared_regs;
319static struct uart_driver mpsc_reg;
320
321static void mpsc_start_rx(struct mpsc_port_info *pi);
322static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
323static void mpsc_release_port(struct uart_port *port);
324/*
325 ******************************************************************************
326 *
327 * Baud Rate Generator Routines (BRG)
328 *
329 ******************************************************************************
330 */
331static void mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
332{
333 u32 v;
334
335 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
336 v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
337
338 if (pi->brg_can_tune)
339 v &= ~(1 << 25);
340
341 if (pi->mirror_regs)
342 pi->BRG_BCR_m = v;
343 writel(v, pi->brg_base + BRG_BCR);
344
345 writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
346 pi->brg_base + BRG_BTR);
347}
348
349static void mpsc_brg_enable(struct mpsc_port_info *pi)
350{
351 u32 v;
352
353 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
354 v |= (1 << 16);
355
356 if (pi->mirror_regs)
357 pi->BRG_BCR_m = v;
358 writel(v, pi->brg_base + BRG_BCR);
359}
360
361static void mpsc_brg_disable(struct mpsc_port_info *pi)
362{
363 u32 v;
364
365 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
366 v &= ~(1 << 16);
367
368 if (pi->mirror_regs)
369 pi->BRG_BCR_m = v;
370 writel(v, pi->brg_base + BRG_BCR);
371}
372
373/*
374 * To set the baud, we adjust the CDV field in the BRG_BCR reg.
375 * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
376 * However, the input clock is divided by 16 in the MPSC b/c of how
377 * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
378 * calculation by 16 to account for that. So the real calculation
379 * that accounts for the way the mpsc is set up is:
380 * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
381 */
382static void mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
383{
384 u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
385 u32 v;
386
387 mpsc_brg_disable(pi);
388 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
389 v = (v & 0xffff0000) | (cdv & 0xffff);
390
391 if (pi->mirror_regs)
392 pi->BRG_BCR_m = v;
393 writel(v, pi->brg_base + BRG_BCR);
394 mpsc_brg_enable(pi);
395}
396
397/*
398 ******************************************************************************
399 *
400 * Serial DMA Routines (SDMA)
401 *
402 ******************************************************************************
403 */
404
405static void mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
406{
407 u32 v;
408
409 pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
410 pi->port.line, burst_size);
411
412 burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
413
414 if (burst_size < 2)
415 v = 0x0; /* 1 64-bit word */
416 else if (burst_size < 4)
417 v = 0x1; /* 2 64-bit words */
418 else if (burst_size < 8)
419 v = 0x2; /* 4 64-bit words */
420 else
421 v = 0x3; /* 8 64-bit words */
422
423 writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
424 pi->sdma_base + SDMA_SDC);
425}
426
427static void mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
428{
429 pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
430 burst_size);
431
432 writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
433 pi->sdma_base + SDMA_SDC);
434 mpsc_sdma_burstsize(pi, burst_size);
435}
436
437static u32 mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
438{
439 u32 old, v;
440
441 pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
442
443 old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
444 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
445
446 mask &= 0xf;
447 if (pi->port.line)
448 mask <<= 8;
449 v &= ~mask;
450
451 if (pi->mirror_regs)
452 pi->shared_regs->SDMA_INTR_MASK_m = v;
453 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
454
455 if (pi->port.line)
456 old >>= 8;
457 return old & 0xf;
458}
459
460static void mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
461{
462 u32 v;
463
464 pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
465
466 v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m
467 : readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
468
469 mask &= 0xf;
470 if (pi->port.line)
471 mask <<= 8;
472 v |= mask;
473
474 if (pi->mirror_regs)
475 pi->shared_regs->SDMA_INTR_MASK_m = v;
476 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
477}
478
479static void mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
480{
481 pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
482
483 if (pi->mirror_regs)
484 pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
485 writeb(0x00, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE
486 + pi->port.line);
487}
488
489static void mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi,
490 struct mpsc_rx_desc *rxre_p)
491{
492 pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
493 pi->port.line, (u32)rxre_p);
494
495 writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
496}
497
498static void mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi,
499 struct mpsc_tx_desc *txre_p)
500{
501 writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
502 writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
503}
504
505static void mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
506{
507 u32 v;
508
509 v = readl(pi->sdma_base + SDMA_SDCM);
510 if (val)
511 v |= val;
512 else
513 v = 0;
514 wmb();
515 writel(v, pi->sdma_base + SDMA_SDCM);
516 wmb();
517}
518
519static uint mpsc_sdma_tx_active(struct mpsc_port_info *pi)
520{
521 return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
522}
523
524static void mpsc_sdma_start_tx(struct mpsc_port_info *pi)
525{
526 struct mpsc_tx_desc *txre, *txre_p;
527
528 /* If tx isn't running & there's a desc ready to go, start it */
529 if (!mpsc_sdma_tx_active(pi)) {
530 txre = (struct mpsc_tx_desc *)(pi->txr
531 + (pi->txr_tail * MPSC_TXRE_SIZE));
532 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
533 DMA_FROM_DEVICE);
534#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
535 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
536 invalidate_dcache_range((ulong)txre,
537 (ulong)txre + MPSC_TXRE_SIZE);
538#endif
539
540 if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
541 txre_p = (struct mpsc_tx_desc *)
542 (pi->txr_p + (pi->txr_tail * MPSC_TXRE_SIZE));
543
544 mpsc_sdma_set_tx_ring(pi, txre_p);
545 mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
546 }
547 }
548}
549
550static void mpsc_sdma_stop(struct mpsc_port_info *pi)
551{
552 pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
553
554 /* Abort any SDMA transfers */
555 mpsc_sdma_cmd(pi, 0);
556 mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
557
558 /* Clear the SDMA current and first TX and RX pointers */
559 mpsc_sdma_set_tx_ring(pi, NULL);
560 mpsc_sdma_set_rx_ring(pi, NULL);
561
562 /* Disable interrupts */
563 mpsc_sdma_intr_mask(pi, 0xf);
564 mpsc_sdma_intr_ack(pi);
565}
566
567/*
568 ******************************************************************************
569 *
570 * Multi-Protocol Serial Controller Routines (MPSC)
571 *
572 ******************************************************************************
573 */
574
575static void mpsc_hw_init(struct mpsc_port_info *pi)
576{
577 u32 v;
578
579 pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
580
581 /* Set up clock routing */
582 if (pi->mirror_regs) {
583 v = pi->shared_regs->MPSC_MRR_m;
584 v &= ~0x1c7;
585 pi->shared_regs->MPSC_MRR_m = v;
586 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
587
588 v = pi->shared_regs->MPSC_RCRR_m;
589 v = (v & ~0xf0f) | 0x100;
590 pi->shared_regs->MPSC_RCRR_m = v;
591 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
592
593 v = pi->shared_regs->MPSC_TCRR_m;
594 v = (v & ~0xf0f) | 0x100;
595 pi->shared_regs->MPSC_TCRR_m = v;
596 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
597 } else {
598 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
599 v &= ~0x1c7;
600 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
601
602 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
603 v = (v & ~0xf0f) | 0x100;
604 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
605
606 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
607 v = (v & ~0xf0f) | 0x100;
608 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
609 }
610
611 /* Put MPSC in UART mode & enabel Tx/Rx egines */
612 writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
613
614 /* No preamble, 16x divider, low-latency, */
615 writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
616 mpsc_set_baudrate(pi, pi->default_baud);
617
618 if (pi->mirror_regs) {
619 pi->MPSC_CHR_1_m = 0;
620 pi->MPSC_CHR_2_m = 0;
621 }
622 writel(0, pi->mpsc_base + MPSC_CHR_1);
623 writel(0, pi->mpsc_base + MPSC_CHR_2);
624 writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
625 writel(0, pi->mpsc_base + MPSC_CHR_4);
626 writel(0, pi->mpsc_base + MPSC_CHR_5);
627 writel(0, pi->mpsc_base + MPSC_CHR_6);
628 writel(0, pi->mpsc_base + MPSC_CHR_7);
629 writel(0, pi->mpsc_base + MPSC_CHR_8);
630 writel(0, pi->mpsc_base + MPSC_CHR_9);
631 writel(0, pi->mpsc_base + MPSC_CHR_10);
632}
633
634static void mpsc_enter_hunt(struct mpsc_port_info *pi)
635{
636 pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
637
638 if (pi->mirror_regs) {
639 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
640 pi->mpsc_base + MPSC_CHR_2);
641 /* Erratum prevents reading CHR_2 so just delay for a while */
642 udelay(100);
643 } else {
644 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
645 pi->mpsc_base + MPSC_CHR_2);
646
647 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
648 udelay(10);
649 }
650}
651
652static void mpsc_freeze(struct mpsc_port_info *pi)
653{
654 u32 v;
655
656 pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
657
658 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
659 readl(pi->mpsc_base + MPSC_MPCR);
660 v |= MPSC_MPCR_FRZ;
661
662 if (pi->mirror_regs)
663 pi->MPSC_MPCR_m = v;
664 writel(v, pi->mpsc_base + MPSC_MPCR);
665}
666
667static void mpsc_unfreeze(struct mpsc_port_info *pi)
668{
669 u32 v;
670
671 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
672 readl(pi->mpsc_base + MPSC_MPCR);
673 v &= ~MPSC_MPCR_FRZ;
674
675 if (pi->mirror_regs)
676 pi->MPSC_MPCR_m = v;
677 writel(v, pi->mpsc_base + MPSC_MPCR);
678
679 pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
680}
681
682static void mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
683{
684 u32 v;
685
686 pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
687
688 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
689 readl(pi->mpsc_base + MPSC_MPCR);
690 v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
691
692 if (pi->mirror_regs)
693 pi->MPSC_MPCR_m = v;
694 writel(v, pi->mpsc_base + MPSC_MPCR);
695}
696
697static void mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
698{
699 u32 v;
700
701 pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
702 pi->port.line, len);
703
704 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
705 readl(pi->mpsc_base + MPSC_MPCR);
706
707 v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
708
709 if (pi->mirror_regs)
710 pi->MPSC_MPCR_m = v;
711 writel(v, pi->mpsc_base + MPSC_MPCR);
712}
713
714static void mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
715{
716 u32 v;
717
718 pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
719
720 v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
721 readl(pi->mpsc_base + MPSC_CHR_2);
722
723 p &= 0x3;
724 v = (v & ~0xc000c) | (p << 18) | (p << 2);
725
726 if (pi->mirror_regs)
727 pi->MPSC_CHR_2_m = v;
728 writel(v, pi->mpsc_base + MPSC_CHR_2);
729}
730
731/*
732 ******************************************************************************
733 *
734 * Driver Init Routines
735 *
736 ******************************************************************************
737 */
738
739static void mpsc_init_hw(struct mpsc_port_info *pi)
740{
741 pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
742
743 mpsc_brg_init(pi, pi->brg_clk_src);
744 mpsc_brg_enable(pi);
745 mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
746 mpsc_sdma_stop(pi);
747 mpsc_hw_init(pi);
748}
749
750static int mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
751{
752 int rc = 0;
753
754 pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
755 pi->port.line);
756
757 if (!pi->dma_region) {
758 if (!dma_supported(pi->port.dev, 0xffffffff)) {
759 printk(KERN_ERR "MPSC: Inadequate DMA support\n");
760 rc = -ENXIO;
761 } else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
762 MPSC_DMA_ALLOC_SIZE,
763 &pi->dma_region_p, GFP_KERNEL))
764 == NULL) {
765 printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
766 rc = -ENOMEM;
767 }
768 }
769
770 return rc;
771}
772
773static void mpsc_free_ring_mem(struct mpsc_port_info *pi)
774{
775 pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
776
777 if (pi->dma_region) {
778 dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
779 pi->dma_region, pi->dma_region_p);
780 pi->dma_region = NULL;
781 pi->dma_region_p = (dma_addr_t)NULL;
782 }
783}
784
785static void mpsc_init_rings(struct mpsc_port_info *pi)
786{
787 struct mpsc_rx_desc *rxre;
788 struct mpsc_tx_desc *txre;
789 dma_addr_t dp, dp_p;
790 u8 *bp, *bp_p;
791 int i;
792
793 pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
794
795 BUG_ON(pi->dma_region == NULL);
796
797 memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
798
799 /*
800 * Descriptors & buffers are multiples of cacheline size and must be
801 * cacheline aligned.
802 */
803 dp = ALIGN((u32)pi->dma_region, dma_get_cache_alignment());
804 dp_p = ALIGN((u32)pi->dma_region_p, dma_get_cache_alignment());
805
806 /*
807 * Partition dma region into rx ring descriptor, rx buffers,
808 * tx ring descriptors, and tx buffers.
809 */
810 pi->rxr = dp;
811 pi->rxr_p = dp_p;
812 dp += MPSC_RXR_SIZE;
813 dp_p += MPSC_RXR_SIZE;
814
815 pi->rxb = (u8 *)dp;
816 pi->rxb_p = (u8 *)dp_p;
817 dp += MPSC_RXB_SIZE;
818 dp_p += MPSC_RXB_SIZE;
819
820 pi->rxr_posn = 0;
821
822 pi->txr = dp;
823 pi->txr_p = dp_p;
824 dp += MPSC_TXR_SIZE;
825 dp_p += MPSC_TXR_SIZE;
826
827 pi->txb = (u8 *)dp;
828 pi->txb_p = (u8 *)dp_p;
829
830 pi->txr_head = 0;
831 pi->txr_tail = 0;
832
833 /* Init rx ring descriptors */
834 dp = pi->rxr;
835 dp_p = pi->rxr_p;
836 bp = pi->rxb;
837 bp_p = pi->rxb_p;
838
839 for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
840 rxre = (struct mpsc_rx_desc *)dp;
841
842 rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
843 rxre->bytecnt = cpu_to_be16(0);
844 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O
845 | SDMA_DESC_CMDSTAT_EI | SDMA_DESC_CMDSTAT_F
846 | SDMA_DESC_CMDSTAT_L);
847 rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
848 rxre->buf_ptr = cpu_to_be32(bp_p);
849
850 dp += MPSC_RXRE_SIZE;
851 dp_p += MPSC_RXRE_SIZE;
852 bp += MPSC_RXBE_SIZE;
853 bp_p += MPSC_RXBE_SIZE;
854 }
855 rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
856
857 /* Init tx ring descriptors */
858 dp = pi->txr;
859 dp_p = pi->txr_p;
860 bp = pi->txb;
861 bp_p = pi->txb_p;
862
863 for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
864 txre = (struct mpsc_tx_desc *)dp;
865
866 txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
867 txre->buf_ptr = cpu_to_be32(bp_p);
868
869 dp += MPSC_TXRE_SIZE;
870 dp_p += MPSC_TXRE_SIZE;
871 bp += MPSC_TXBE_SIZE;
872 bp_p += MPSC_TXBE_SIZE;
873 }
874 txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
875
876 dma_cache_sync(pi->port.dev, (void *)pi->dma_region,
877 MPSC_DMA_ALLOC_SIZE, DMA_BIDIRECTIONAL);
878#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
879 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
880 flush_dcache_range((ulong)pi->dma_region,
881 (ulong)pi->dma_region
882 + MPSC_DMA_ALLOC_SIZE);
883#endif
884
885 return;
886}
887
888static void mpsc_uninit_rings(struct mpsc_port_info *pi)
889{
890 pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
891
892 BUG_ON(pi->dma_region == NULL);
893
894 pi->rxr = 0;
895 pi->rxr_p = 0;
896 pi->rxb = NULL;
897 pi->rxb_p = NULL;
898 pi->rxr_posn = 0;
899
900 pi->txr = 0;
901 pi->txr_p = 0;
902 pi->txb = NULL;
903 pi->txb_p = NULL;
904 pi->txr_head = 0;
905 pi->txr_tail = 0;
906}
907
908static int mpsc_make_ready(struct mpsc_port_info *pi)
909{
910 int rc;
911
912 pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
913
914 if (!pi->ready) {
915 mpsc_init_hw(pi);
916 if ((rc = mpsc_alloc_ring_mem(pi)))
917 return rc;
918 mpsc_init_rings(pi);
919 pi->ready = 1;
920 }
921
922 return 0;
923}
924
925#ifdef CONFIG_CONSOLE_POLL
926static int serial_polled;
927#endif
928
929/*
930 ******************************************************************************
931 *
932 * Interrupt Handling Routines
933 *
934 ******************************************************************************
935 */
936
937static int mpsc_rx_intr(struct mpsc_port_info *pi)
938{
939 struct mpsc_rx_desc *rxre;
940 struct tty_struct *tty = pi->port.state->port.tty;
941 u32 cmdstat, bytes_in, i;
942 int rc = 0;
943 u8 *bp;
944 char flag = TTY_NORMAL;
945
946 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
947
948 rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
949
950 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
951 DMA_FROM_DEVICE);
952#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
953 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
954 invalidate_dcache_range((ulong)rxre,
955 (ulong)rxre + MPSC_RXRE_SIZE);
956#endif
957
958 /*
959 * Loop through Rx descriptors handling ones that have been completed.
960 */
961 while (!((cmdstat = be32_to_cpu(rxre->cmdstat))
962 & SDMA_DESC_CMDSTAT_O)) {
963 bytes_in = be16_to_cpu(rxre->bytecnt);
964#ifdef CONFIG_CONSOLE_POLL
965 if (unlikely(serial_polled)) {
966 serial_polled = 0;
967 return 0;
968 }
969#endif
970 /* Following use of tty struct directly is deprecated */
971 if (unlikely(tty_buffer_request_room(tty, bytes_in)
972 < bytes_in)) {
973 if (tty->low_latency)
974 tty_flip_buffer_push(tty);
975 /*
976 * If this failed then we will throw away the bytes
977 * but must do so to clear interrupts.
978 */
979 }
980
981 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
982 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_RXBE_SIZE,
983 DMA_FROM_DEVICE);
984#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
985 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
986 invalidate_dcache_range((ulong)bp,
987 (ulong)bp + MPSC_RXBE_SIZE);
988#endif
989
990 /*
991 * Other than for parity error, the manual provides little
992 * info on what data will be in a frame flagged by any of
993 * these errors. For parity error, it is the last byte in
994 * the buffer that had the error. As for the rest, I guess
995 * we'll assume there is no data in the buffer.
996 * If there is...it gets lost.
997 */
998 if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR
999 | SDMA_DESC_CMDSTAT_FR
1000 | SDMA_DESC_CMDSTAT_OR))) {
1001
1002 pi->port.icount.rx++;
1003
1004 if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
1005 pi->port.icount.brk++;
1006
1007 if (uart_handle_break(&pi->port))
1008 goto next_frame;
1009 } else if (cmdstat & SDMA_DESC_CMDSTAT_FR) {
1010 pi->port.icount.frame++;
1011 } else if (cmdstat & SDMA_DESC_CMDSTAT_OR) {
1012 pi->port.icount.overrun++;
1013 }
1014
1015 cmdstat &= pi->port.read_status_mask;
1016
1017 if (cmdstat & SDMA_DESC_CMDSTAT_BR)
1018 flag = TTY_BREAK;
1019 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
1020 flag = TTY_FRAME;
1021 else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
1022 flag = TTY_OVERRUN;
1023 else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
1024 flag = TTY_PARITY;
1025 }
1026
1027 if (uart_handle_sysrq_char(&pi->port, *bp)) {
1028 bp++;
1029 bytes_in--;
1030#ifdef CONFIG_CONSOLE_POLL
1031 if (unlikely(serial_polled)) {
1032 serial_polled = 0;
1033 return 0;
1034 }
1035#endif
1036 goto next_frame;
1037 }
1038
1039 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR
1040 | SDMA_DESC_CMDSTAT_FR
1041 | SDMA_DESC_CMDSTAT_OR)))
1042 && !(cmdstat & pi->port.ignore_status_mask)) {
1043 tty_insert_flip_char(tty, *bp, flag);
1044 } else {
1045 for (i=0; i<bytes_in; i++)
1046 tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
1047
1048 pi->port.icount.rx += bytes_in;
1049 }
1050
1051next_frame:
1052 rxre->bytecnt = cpu_to_be16(0);
1053 wmb();
1054 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O
1055 | SDMA_DESC_CMDSTAT_EI | SDMA_DESC_CMDSTAT_F
1056 | SDMA_DESC_CMDSTAT_L);
1057 wmb();
1058 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
1059 DMA_BIDIRECTIONAL);
1060#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1061 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1062 flush_dcache_range((ulong)rxre,
1063 (ulong)rxre + MPSC_RXRE_SIZE);
1064#endif
1065
1066 /* Advance to next descriptor */
1067 pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
1068 rxre = (struct mpsc_rx_desc *)
1069 (pi->rxr + (pi->rxr_posn * MPSC_RXRE_SIZE));
1070 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE,
1071 DMA_FROM_DEVICE);
1072#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1073 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1074 invalidate_dcache_range((ulong)rxre,
1075 (ulong)rxre + MPSC_RXRE_SIZE);
1076#endif
1077 rc = 1;
1078 }
1079
1080 /* Restart rx engine, if its stopped */
1081 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1082 mpsc_start_rx(pi);
1083
1084 tty_flip_buffer_push(tty);
1085 return rc;
1086}
1087
1088static void mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
1089{
1090 struct mpsc_tx_desc *txre;
1091
1092 txre = (struct mpsc_tx_desc *)(pi->txr
1093 + (pi->txr_head * MPSC_TXRE_SIZE));
1094
1095 txre->bytecnt = cpu_to_be16(count);
1096 txre->shadow = txre->bytecnt;
1097 wmb(); /* ensure cmdstat is last field updated */
1098 txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F
1099 | SDMA_DESC_CMDSTAT_L
1100 | ((intr) ? SDMA_DESC_CMDSTAT_EI : 0));
1101 wmb();
1102 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
1103 DMA_BIDIRECTIONAL);
1104#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1105 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1106 flush_dcache_range((ulong)txre,
1107 (ulong)txre + MPSC_TXRE_SIZE);
1108#endif
1109}
1110
1111static void mpsc_copy_tx_data(struct mpsc_port_info *pi)
1112{
1113 struct circ_buf *xmit = &pi->port.state->xmit;
1114 u8 *bp;
1115 u32 i;
1116
1117 /* Make sure the desc ring isn't full */
1118 while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES)
1119 < (MPSC_TXR_ENTRIES - 1)) {
1120 if (pi->port.x_char) {
1121 /*
1122 * Ideally, we should use the TCS field in
1123 * CHR_1 to put the x_char out immediately but
1124 * errata prevents us from being able to read
1125 * CHR_2 to know that its safe to write to
1126 * CHR_1. Instead, just put it in-band with
1127 * all the other Tx data.
1128 */
1129 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1130 *bp = pi->port.x_char;
1131 pi->port.x_char = 0;
1132 i = 1;
1133 } else if (!uart_circ_empty(xmit)
1134 && !uart_tx_stopped(&pi->port)) {
1135 i = min((u32)MPSC_TXBE_SIZE,
1136 (u32)uart_circ_chars_pending(xmit));
1137 i = min(i, (u32)CIRC_CNT_TO_END(xmit->head, xmit->tail,
1138 UART_XMIT_SIZE));
1139 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1140 memcpy(bp, &xmit->buf[xmit->tail], i);
1141 xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
1142
1143 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1144 uart_write_wakeup(&pi->port);
1145 } else { /* All tx data copied into ring bufs */
1146 return;
1147 }
1148
1149 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_TXBE_SIZE,
1150 DMA_BIDIRECTIONAL);
1151#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1152 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1153 flush_dcache_range((ulong)bp,
1154 (ulong)bp + MPSC_TXBE_SIZE);
1155#endif
1156 mpsc_setup_tx_desc(pi, i, 1);
1157
1158 /* Advance to next descriptor */
1159 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1160 }
1161}
1162
1163static int mpsc_tx_intr(struct mpsc_port_info *pi)
1164{
1165 struct mpsc_tx_desc *txre;
1166 int rc = 0;
1167 unsigned long iflags;
1168
1169 spin_lock_irqsave(&pi->tx_lock, iflags);
1170
1171 if (!mpsc_sdma_tx_active(pi)) {
1172 txre = (struct mpsc_tx_desc *)(pi->txr
1173 + (pi->txr_tail * MPSC_TXRE_SIZE));
1174
1175 dma_cache_sync(pi->port.dev, (void *)txre, MPSC_TXRE_SIZE,
1176 DMA_FROM_DEVICE);
1177#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1178 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1179 invalidate_dcache_range((ulong)txre,
1180 (ulong)txre + MPSC_TXRE_SIZE);
1181#endif
1182
1183 while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
1184 rc = 1;
1185 pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
1186 pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
1187
1188 /* If no more data to tx, fall out of loop */
1189 if (pi->txr_head == pi->txr_tail)
1190 break;
1191
1192 txre = (struct mpsc_tx_desc *)(pi->txr
1193 + (pi->txr_tail * MPSC_TXRE_SIZE));
1194 dma_cache_sync(pi->port.dev, (void *)txre,
1195 MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
1196#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1197 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1198 invalidate_dcache_range((ulong)txre,
1199 (ulong)txre + MPSC_TXRE_SIZE);
1200#endif
1201 }
1202
1203 mpsc_copy_tx_data(pi);
1204 mpsc_sdma_start_tx(pi); /* start next desc if ready */
1205 }
1206
1207 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1208 return rc;
1209}
1210
1211/*
1212 * This is the driver's interrupt handler. To avoid a race, we first clear
1213 * the interrupt, then handle any completed Rx/Tx descriptors. When done
1214 * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
1215 */
1216static irqreturn_t mpsc_sdma_intr(int irq, void *dev_id)
1217{
1218 struct mpsc_port_info *pi = dev_id;
1219 ulong iflags;
1220 int rc = IRQ_NONE;
1221
1222 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
1223
1224 spin_lock_irqsave(&pi->port.lock, iflags);
1225 mpsc_sdma_intr_ack(pi);
1226 if (mpsc_rx_intr(pi))
1227 rc = IRQ_HANDLED;
1228 if (mpsc_tx_intr(pi))
1229 rc = IRQ_HANDLED;
1230 spin_unlock_irqrestore(&pi->port.lock, iflags);
1231
1232 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
1233 return rc;
1234}
1235
1236/*
1237 ******************************************************************************
1238 *
1239 * serial_core.c Interface routines
1240 *
1241 ******************************************************************************
1242 */
1243static uint mpsc_tx_empty(struct uart_port *port)
1244{
1245 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1246 ulong iflags;
1247 uint rc;
1248
1249 spin_lock_irqsave(&pi->port.lock, iflags);
1250 rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
1251 spin_unlock_irqrestore(&pi->port.lock, iflags);
1252
1253 return rc;
1254}
1255
1256static void mpsc_set_mctrl(struct uart_port *port, uint mctrl)
1257{
1258 /* Have no way to set modem control lines AFAICT */
1259}
1260
1261static uint mpsc_get_mctrl(struct uart_port *port)
1262{
1263 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1264 u32 mflags, status;
1265
1266 status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m
1267 : readl(pi->mpsc_base + MPSC_CHR_10);
1268
1269 mflags = 0;
1270 if (status & 0x1)
1271 mflags |= TIOCM_CTS;
1272 if (status & 0x2)
1273 mflags |= TIOCM_CAR;
1274
1275 return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
1276}
1277
1278static void mpsc_stop_tx(struct uart_port *port)
1279{
1280 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1281
1282 pr_debug("mpsc_stop_tx[%d]\n", port->line);
1283
1284 mpsc_freeze(pi);
1285}
1286
1287static void mpsc_start_tx(struct uart_port *port)
1288{
1289 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1290 unsigned long iflags;
1291
1292 spin_lock_irqsave(&pi->tx_lock, iflags);
1293
1294 mpsc_unfreeze(pi);
1295 mpsc_copy_tx_data(pi);
1296 mpsc_sdma_start_tx(pi);
1297
1298 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1299
1300 pr_debug("mpsc_start_tx[%d]\n", port->line);
1301}
1302
1303static void mpsc_start_rx(struct mpsc_port_info *pi)
1304{
1305 pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
1306
1307 if (pi->rcv_data) {
1308 mpsc_enter_hunt(pi);
1309 mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
1310 }
1311}
1312
1313static void mpsc_stop_rx(struct uart_port *port)
1314{
1315 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1316
1317 pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
1318
1319 if (pi->mirror_regs) {
1320 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_RA,
1321 pi->mpsc_base + MPSC_CHR_2);
1322 /* Erratum prevents reading CHR_2 so just delay for a while */
1323 udelay(100);
1324 } else {
1325 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_RA,
1326 pi->mpsc_base + MPSC_CHR_2);
1327
1328 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_RA)
1329 udelay(10);
1330 }
1331
1332 mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
1333}
1334
1335static void mpsc_enable_ms(struct uart_port *port)
1336{
1337}
1338
1339static void mpsc_break_ctl(struct uart_port *port, int ctl)
1340{
1341 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1342 ulong flags;
1343 u32 v;
1344
1345 v = ctl ? 0x00ff0000 : 0;
1346
1347 spin_lock_irqsave(&pi->port.lock, flags);
1348 if (pi->mirror_regs)
1349 pi->MPSC_CHR_1_m = v;
1350 writel(v, pi->mpsc_base + MPSC_CHR_1);
1351 spin_unlock_irqrestore(&pi->port.lock, flags);
1352}
1353
1354static int mpsc_startup(struct uart_port *port)
1355{
1356 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1357 u32 flag = 0;
1358 int rc;
1359
1360 pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
1361 port->line, pi->port.irq);
1362
1363 if ((rc = mpsc_make_ready(pi)) == 0) {
1364 /* Setup IRQ handler */
1365 mpsc_sdma_intr_ack(pi);
1366
1367 /* If irq's are shared, need to set flag */
1368 if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
1369 flag = IRQF_SHARED;
1370
1371 if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
1372 "mpsc-sdma", pi))
1373 printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
1374 pi->port.irq);
1375
1376 mpsc_sdma_intr_unmask(pi, 0xf);
1377 mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p
1378 + (pi->rxr_posn * MPSC_RXRE_SIZE)));
1379 }
1380
1381 return rc;
1382}
1383
1384static void mpsc_shutdown(struct uart_port *port)
1385{
1386 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1387
1388 pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
1389
1390 mpsc_sdma_stop(pi);
1391 free_irq(pi->port.irq, pi);
1392}
1393
1394static void mpsc_set_termios(struct uart_port *port, struct ktermios *termios,
1395 struct ktermios *old)
1396{
1397 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1398 u32 baud;
1399 ulong flags;
1400 u32 chr_bits, stop_bits, par;
1401
1402 pi->c_iflag = termios->c_iflag;
1403 pi->c_cflag = termios->c_cflag;
1404
1405 switch (termios->c_cflag & CSIZE) {
1406 case CS5:
1407 chr_bits = MPSC_MPCR_CL_5;
1408 break;
1409 case CS6:
1410 chr_bits = MPSC_MPCR_CL_6;
1411 break;
1412 case CS7:
1413 chr_bits = MPSC_MPCR_CL_7;
1414 break;
1415 case CS8:
1416 default:
1417 chr_bits = MPSC_MPCR_CL_8;
1418 break;
1419 }
1420
1421 if (termios->c_cflag & CSTOPB)
1422 stop_bits = MPSC_MPCR_SBL_2;
1423 else
1424 stop_bits = MPSC_MPCR_SBL_1;
1425
1426 par = MPSC_CHR_2_PAR_EVEN;
1427 if (termios->c_cflag & PARENB)
1428 if (termios->c_cflag & PARODD)
1429 par = MPSC_CHR_2_PAR_ODD;
1430#ifdef CMSPAR
1431 if (termios->c_cflag & CMSPAR) {
1432 if (termios->c_cflag & PARODD)
1433 par = MPSC_CHR_2_PAR_MARK;
1434 else
1435 par = MPSC_CHR_2_PAR_SPACE;
1436 }
1437#endif
1438
1439 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
1440
1441 spin_lock_irqsave(&pi->port.lock, flags);
1442
1443 uart_update_timeout(port, termios->c_cflag, baud);
1444
1445 mpsc_set_char_length(pi, chr_bits);
1446 mpsc_set_stop_bit_length(pi, stop_bits);
1447 mpsc_set_parity(pi, par);
1448 mpsc_set_baudrate(pi, baud);
1449
1450 /* Characters/events to read */
1451 pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
1452
1453 if (termios->c_iflag & INPCK)
1454 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE
1455 | SDMA_DESC_CMDSTAT_FR;
1456
1457 if (termios->c_iflag & (BRKINT | PARMRK))
1458 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
1459
1460 /* Characters/events to ignore */
1461 pi->port.ignore_status_mask = 0;
1462
1463 if (termios->c_iflag & IGNPAR)
1464 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE
1465 | SDMA_DESC_CMDSTAT_FR;
1466
1467 if (termios->c_iflag & IGNBRK) {
1468 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
1469
1470 if (termios->c_iflag & IGNPAR)
1471 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
1472 }
1473
1474 if ((termios->c_cflag & CREAD)) {
1475 if (!pi->rcv_data) {
1476 pi->rcv_data = 1;
1477 mpsc_start_rx(pi);
1478 }
1479 } else if (pi->rcv_data) {
1480 mpsc_stop_rx(port);
1481 pi->rcv_data = 0;
1482 }
1483
1484 spin_unlock_irqrestore(&pi->port.lock, flags);
1485}
1486
1487static const char *mpsc_type(struct uart_port *port)
1488{
1489 pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
1490 return MPSC_DRIVER_NAME;
1491}
1492
1493static int mpsc_request_port(struct uart_port *port)
1494{
1495 /* Should make chip/platform specific call */
1496 return 0;
1497}
1498
1499static void mpsc_release_port(struct uart_port *port)
1500{
1501 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1502
1503 if (pi->ready) {
1504 mpsc_uninit_rings(pi);
1505 mpsc_free_ring_mem(pi);
1506 pi->ready = 0;
1507 }
1508}
1509
1510static void mpsc_config_port(struct uart_port *port, int flags)
1511{
1512}
1513
1514static int mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
1515{
1516 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1517 int rc = 0;
1518
1519 pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
1520
1521 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
1522 rc = -EINVAL;
1523 else if (pi->port.irq != ser->irq)
1524 rc = -EINVAL;
1525 else if (ser->io_type != SERIAL_IO_MEM)
1526 rc = -EINVAL;
1527 else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
1528 rc = -EINVAL;
1529 else if ((void *)pi->port.mapbase != ser->iomem_base)
1530 rc = -EINVAL;
1531 else if (pi->port.iobase != ser->port)
1532 rc = -EINVAL;
1533 else if (ser->hub6 != 0)
1534 rc = -EINVAL;
1535
1536 return rc;
1537}
1538#ifdef CONFIG_CONSOLE_POLL
1539/* Serial polling routines for writing and reading from the uart while
1540 * in an interrupt or debug context.
1541 */
1542
1543static char poll_buf[2048];
1544static int poll_ptr;
1545static int poll_cnt;
1546static void mpsc_put_poll_char(struct uart_port *port,
1547 unsigned char c);
1548
1549static int mpsc_get_poll_char(struct uart_port *port)
1550{
1551 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1552 struct mpsc_rx_desc *rxre;
1553 u32 cmdstat, bytes_in, i;
1554 u8 *bp;
1555
1556 if (!serial_polled)
1557 serial_polled = 1;
1558
1559 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
1560
1561 if (poll_cnt) {
1562 poll_cnt--;
1563 return poll_buf[poll_ptr++];
1564 }
1565 poll_ptr = 0;
1566 poll_cnt = 0;
1567
1568 while (poll_cnt == 0) {
1569 rxre = (struct mpsc_rx_desc *)(pi->rxr +
1570 (pi->rxr_posn*MPSC_RXRE_SIZE));
1571 dma_cache_sync(pi->port.dev, (void *)rxre,
1572 MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
1573#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1574 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1575 invalidate_dcache_range((ulong)rxre,
1576 (ulong)rxre + MPSC_RXRE_SIZE);
1577#endif
1578 /*
1579 * Loop through Rx descriptors handling ones that have
1580 * been completed.
1581 */
1582 while (poll_cnt == 0 &&
1583 !((cmdstat = be32_to_cpu(rxre->cmdstat)) &
1584 SDMA_DESC_CMDSTAT_O)){
1585 bytes_in = be16_to_cpu(rxre->bytecnt);
1586 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
1587 dma_cache_sync(pi->port.dev, (void *) bp,
1588 MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
1589#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1590 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1591 invalidate_dcache_range((ulong)bp,
1592 (ulong)bp + MPSC_RXBE_SIZE);
1593#endif
1594 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
1595 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
1596 !(cmdstat & pi->port.ignore_status_mask)) {
1597 poll_buf[poll_cnt] = *bp;
1598 poll_cnt++;
1599 } else {
1600 for (i = 0; i < bytes_in; i++) {
1601 poll_buf[poll_cnt] = *bp++;
1602 poll_cnt++;
1603 }
1604 pi->port.icount.rx += bytes_in;
1605 }
1606 rxre->bytecnt = cpu_to_be16(0);
1607 wmb();
1608 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
1609 SDMA_DESC_CMDSTAT_EI |
1610 SDMA_DESC_CMDSTAT_F |
1611 SDMA_DESC_CMDSTAT_L);
1612 wmb();
1613 dma_cache_sync(pi->port.dev, (void *)rxre,
1614 MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
1615#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1616 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1617 flush_dcache_range((ulong)rxre,
1618 (ulong)rxre + MPSC_RXRE_SIZE);
1619#endif
1620
1621 /* Advance to next descriptor */
1622 pi->rxr_posn = (pi->rxr_posn + 1) &
1623 (MPSC_RXR_ENTRIES - 1);
1624 rxre = (struct mpsc_rx_desc *)(pi->rxr +
1625 (pi->rxr_posn * MPSC_RXRE_SIZE));
1626 dma_cache_sync(pi->port.dev, (void *)rxre,
1627 MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
1628#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1629 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1630 invalidate_dcache_range((ulong)rxre,
1631 (ulong)rxre + MPSC_RXRE_SIZE);
1632#endif
1633 }
1634
1635 /* Restart rx engine, if its stopped */
1636 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1637 mpsc_start_rx(pi);
1638 }
1639 if (poll_cnt) {
1640 poll_cnt--;
1641 return poll_buf[poll_ptr++];
1642 }
1643
1644 return 0;
1645}
1646
1647
1648static void mpsc_put_poll_char(struct uart_port *port,
1649 unsigned char c)
1650{
1651 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1652 u32 data;
1653
1654 data = readl(pi->mpsc_base + MPSC_MPCR);
1655 writeb(c, pi->mpsc_base + MPSC_CHR_1);
1656 mb();
1657 data = readl(pi->mpsc_base + MPSC_CHR_2);
1658 data |= MPSC_CHR_2_TTCS;
1659 writel(data, pi->mpsc_base + MPSC_CHR_2);
1660 mb();
1661
1662 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_TTCS);
1663}
1664#endif
1665
1666static struct uart_ops mpsc_pops = {
1667 .tx_empty = mpsc_tx_empty,
1668 .set_mctrl = mpsc_set_mctrl,
1669 .get_mctrl = mpsc_get_mctrl,
1670 .stop_tx = mpsc_stop_tx,
1671 .start_tx = mpsc_start_tx,
1672 .stop_rx = mpsc_stop_rx,
1673 .enable_ms = mpsc_enable_ms,
1674 .break_ctl = mpsc_break_ctl,
1675 .startup = mpsc_startup,
1676 .shutdown = mpsc_shutdown,
1677 .set_termios = mpsc_set_termios,
1678 .type = mpsc_type,
1679 .release_port = mpsc_release_port,
1680 .request_port = mpsc_request_port,
1681 .config_port = mpsc_config_port,
1682 .verify_port = mpsc_verify_port,
1683#ifdef CONFIG_CONSOLE_POLL
1684 .poll_get_char = mpsc_get_poll_char,
1685 .poll_put_char = mpsc_put_poll_char,
1686#endif
1687};
1688
1689/*
1690 ******************************************************************************
1691 *
1692 * Console Interface Routines
1693 *
1694 ******************************************************************************
1695 */
1696
1697#ifdef CONFIG_SERIAL_MPSC_CONSOLE
1698static void mpsc_console_write(struct console *co, const char *s, uint count)
1699{
1700 struct mpsc_port_info *pi = &mpsc_ports[co->index];
1701 u8 *bp, *dp, add_cr = 0;
1702 int i;
1703 unsigned long iflags;
1704
1705 spin_lock_irqsave(&pi->tx_lock, iflags);
1706
1707 while (pi->txr_head != pi->txr_tail) {
1708 while (mpsc_sdma_tx_active(pi))
1709 udelay(100);
1710 mpsc_sdma_intr_ack(pi);
1711 mpsc_tx_intr(pi);
1712 }
1713
1714 while (mpsc_sdma_tx_active(pi))
1715 udelay(100);
1716
1717 while (count > 0) {
1718 bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1719
1720 for (i = 0; i < MPSC_TXBE_SIZE; i++) {
1721 if (count == 0)
1722 break;
1723
1724 if (add_cr) {
1725 *(dp++) = '\r';
1726 add_cr = 0;
1727 } else {
1728 *(dp++) = *s;
1729
1730 if (*(s++) == '\n') { /* add '\r' after '\n' */
1731 add_cr = 1;
1732 count++;
1733 }
1734 }
1735
1736 count--;
1737 }
1738
1739 dma_cache_sync(pi->port.dev, (void *)bp, MPSC_TXBE_SIZE,
1740 DMA_BIDIRECTIONAL);
1741#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1742 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1743 flush_dcache_range((ulong)bp,
1744 (ulong)bp + MPSC_TXBE_SIZE);
1745#endif
1746 mpsc_setup_tx_desc(pi, i, 0);
1747 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1748 mpsc_sdma_start_tx(pi);
1749
1750 while (mpsc_sdma_tx_active(pi))
1751 udelay(100);
1752
1753 pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
1754 }
1755
1756 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1757}
1758
1759static int __init mpsc_console_setup(struct console *co, char *options)
1760{
1761 struct mpsc_port_info *pi;
1762 int baud, bits, parity, flow;
1763
1764 pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
1765
1766 if (co->index >= MPSC_NUM_CTLRS)
1767 co->index = 0;
1768
1769 pi = &mpsc_ports[co->index];
1770
1771 baud = pi->default_baud;
1772 bits = pi->default_bits;
1773 parity = pi->default_parity;
1774 flow = pi->default_flow;
1775
1776 if (!pi->port.ops)
1777 return -ENODEV;
1778
1779 spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
1780
1781 if (options)
1782 uart_parse_options(options, &baud, &parity, &bits, &flow);
1783
1784 return uart_set_options(&pi->port, co, baud, parity, bits, flow);
1785}
1786
1787static struct console mpsc_console = {
1788 .name = MPSC_DEV_NAME,
1789 .write = mpsc_console_write,
1790 .device = uart_console_device,
1791 .setup = mpsc_console_setup,
1792 .flags = CON_PRINTBUFFER,
1793 .index = -1,
1794 .data = &mpsc_reg,
1795};
1796
1797static int __init mpsc_late_console_init(void)
1798{
1799 pr_debug("mpsc_late_console_init: Enter\n");
1800
1801 if (!(mpsc_console.flags & CON_ENABLED))
1802 register_console(&mpsc_console);
1803 return 0;
1804}
1805
1806late_initcall(mpsc_late_console_init);
1807
1808#define MPSC_CONSOLE &mpsc_console
1809#else
1810#define MPSC_CONSOLE NULL
1811#endif
1812/*
1813 ******************************************************************************
1814 *
1815 * Dummy Platform Driver to extract & map shared register regions
1816 *
1817 ******************************************************************************
1818 */
1819static void mpsc_resource_err(char *s)
1820{
1821 printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
1822}
1823
1824static int mpsc_shared_map_regs(struct platform_device *pd)
1825{
1826 struct resource *r;
1827
1828 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1829 MPSC_ROUTING_BASE_ORDER))
1830 && request_mem_region(r->start,
1831 MPSC_ROUTING_REG_BLOCK_SIZE,
1832 "mpsc_routing_regs")) {
1833 mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
1834 MPSC_ROUTING_REG_BLOCK_SIZE);
1835 mpsc_shared_regs.mpsc_routing_base_p = r->start;
1836 } else {
1837 mpsc_resource_err("MPSC routing base");
1838 return -ENOMEM;
1839 }
1840
1841 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1842 MPSC_SDMA_INTR_BASE_ORDER))
1843 && request_mem_region(r->start,
1844 MPSC_SDMA_INTR_REG_BLOCK_SIZE,
1845 "sdma_intr_regs")) {
1846 mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
1847 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1848 mpsc_shared_regs.sdma_intr_base_p = r->start;
1849 } else {
1850 iounmap(mpsc_shared_regs.mpsc_routing_base);
1851 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1852 MPSC_ROUTING_REG_BLOCK_SIZE);
1853 mpsc_resource_err("SDMA intr base");
1854 return -ENOMEM;
1855 }
1856
1857 return 0;
1858}
1859
1860static void mpsc_shared_unmap_regs(void)
1861{
1862 if (!mpsc_shared_regs.mpsc_routing_base) {
1863 iounmap(mpsc_shared_regs.mpsc_routing_base);
1864 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1865 MPSC_ROUTING_REG_BLOCK_SIZE);
1866 }
1867 if (!mpsc_shared_regs.sdma_intr_base) {
1868 iounmap(mpsc_shared_regs.sdma_intr_base);
1869 release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
1870 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1871 }
1872
1873 mpsc_shared_regs.mpsc_routing_base = NULL;
1874 mpsc_shared_regs.sdma_intr_base = NULL;
1875
1876 mpsc_shared_regs.mpsc_routing_base_p = 0;
1877 mpsc_shared_regs.sdma_intr_base_p = 0;
1878}
1879
1880static int mpsc_shared_drv_probe(struct platform_device *dev)
1881{
1882 struct mpsc_shared_pdata *pdata;
1883 int rc = -ENODEV;
1884
1885 if (dev->id == 0) {
1886 if (!(rc = mpsc_shared_map_regs(dev))) {
1887 pdata = (struct mpsc_shared_pdata *)
1888 dev->dev.platform_data;
1889
1890 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1891 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
1892 mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
1893 mpsc_shared_regs.SDMA_INTR_CAUSE_m =
1894 pdata->intr_cause_val;
1895 mpsc_shared_regs.SDMA_INTR_MASK_m =
1896 pdata->intr_mask_val;
1897
1898 rc = 0;
1899 }
1900 }
1901
1902 return rc;
1903}
1904
1905static int mpsc_shared_drv_remove(struct platform_device *dev)
1906{
1907 int rc = -ENODEV;
1908
1909 if (dev->id == 0) {
1910 mpsc_shared_unmap_regs();
1911 mpsc_shared_regs.MPSC_MRR_m = 0;
1912 mpsc_shared_regs.MPSC_RCRR_m = 0;
1913 mpsc_shared_regs.MPSC_TCRR_m = 0;
1914 mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
1915 mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
1916 rc = 0;
1917 }
1918
1919 return rc;
1920}
1921
1922static struct platform_driver mpsc_shared_driver = {
1923 .probe = mpsc_shared_drv_probe,
1924 .remove = mpsc_shared_drv_remove,
1925 .driver = {
1926 .name = MPSC_SHARED_NAME,
1927 },
1928};
1929
1930/*
1931 ******************************************************************************
1932 *
1933 * Driver Interface Routines
1934 *
1935 ******************************************************************************
1936 */
1937static struct uart_driver mpsc_reg = {
1938 .owner = THIS_MODULE,
1939 .driver_name = MPSC_DRIVER_NAME,
1940 .dev_name = MPSC_DEV_NAME,
1941 .major = MPSC_MAJOR,
1942 .minor = MPSC_MINOR_START,
1943 .nr = MPSC_NUM_CTLRS,
1944 .cons = MPSC_CONSOLE,
1945};
1946
1947static int mpsc_drv_map_regs(struct mpsc_port_info *pi,
1948 struct platform_device *pd)
1949{
1950 struct resource *r;
1951
1952 if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER))
1953 && request_mem_region(r->start, MPSC_REG_BLOCK_SIZE,
1954 "mpsc_regs")) {
1955 pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
1956 pi->mpsc_base_p = r->start;
1957 } else {
1958 mpsc_resource_err("MPSC base");
1959 goto err;
1960 }
1961
1962 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1963 MPSC_SDMA_BASE_ORDER))
1964 && request_mem_region(r->start,
1965 MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
1966 pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
1967 pi->sdma_base_p = r->start;
1968 } else {
1969 mpsc_resource_err("SDMA base");
1970 if (pi->mpsc_base) {
1971 iounmap(pi->mpsc_base);
1972 pi->mpsc_base = NULL;
1973 }
1974 goto err;
1975 }
1976
1977 if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
1978 && request_mem_region(r->start,
1979 MPSC_BRG_REG_BLOCK_SIZE, "brg_regs")) {
1980 pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
1981 pi->brg_base_p = r->start;
1982 } else {
1983 mpsc_resource_err("BRG base");
1984 if (pi->mpsc_base) {
1985 iounmap(pi->mpsc_base);
1986 pi->mpsc_base = NULL;
1987 }
1988 if (pi->sdma_base) {
1989 iounmap(pi->sdma_base);
1990 pi->sdma_base = NULL;
1991 }
1992 goto err;
1993 }
1994 return 0;
1995
1996err:
1997 return -ENOMEM;
1998}
1999
2000static void mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
2001{
2002 if (!pi->mpsc_base) {
2003 iounmap(pi->mpsc_base);
2004 release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
2005 }
2006 if (!pi->sdma_base) {
2007 iounmap(pi->sdma_base);
2008 release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
2009 }
2010 if (!pi->brg_base) {
2011 iounmap(pi->brg_base);
2012 release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
2013 }
2014
2015 pi->mpsc_base = NULL;
2016 pi->sdma_base = NULL;
2017 pi->brg_base = NULL;
2018
2019 pi->mpsc_base_p = 0;
2020 pi->sdma_base_p = 0;
2021 pi->brg_base_p = 0;
2022}
2023
2024static void mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
2025 struct platform_device *pd, int num)
2026{
2027 struct mpsc_pdata *pdata;
2028
2029 pdata = (struct mpsc_pdata *)pd->dev.platform_data;
2030
2031 pi->port.uartclk = pdata->brg_clk_freq;
2032 pi->port.iotype = UPIO_MEM;
2033 pi->port.line = num;
2034 pi->port.type = PORT_MPSC;
2035 pi->port.fifosize = MPSC_TXBE_SIZE;
2036 pi->port.membase = pi->mpsc_base;
2037 pi->port.mapbase = (ulong)pi->mpsc_base;
2038 pi->port.ops = &mpsc_pops;
2039
2040 pi->mirror_regs = pdata->mirror_regs;
2041 pi->cache_mgmt = pdata->cache_mgmt;
2042 pi->brg_can_tune = pdata->brg_can_tune;
2043 pi->brg_clk_src = pdata->brg_clk_src;
2044 pi->mpsc_max_idle = pdata->max_idle;
2045 pi->default_baud = pdata->default_baud;
2046 pi->default_bits = pdata->default_bits;
2047 pi->default_parity = pdata->default_parity;
2048 pi->default_flow = pdata->default_flow;
2049
2050 /* Initial values of mirrored regs */
2051 pi->MPSC_CHR_1_m = pdata->chr_1_val;
2052 pi->MPSC_CHR_2_m = pdata->chr_2_val;
2053 pi->MPSC_CHR_10_m = pdata->chr_10_val;
2054 pi->MPSC_MPCR_m = pdata->mpcr_val;
2055 pi->BRG_BCR_m = pdata->bcr_val;
2056
2057 pi->shared_regs = &mpsc_shared_regs;
2058
2059 pi->port.irq = platform_get_irq(pd, 0);
2060}
2061
2062static int mpsc_drv_probe(struct platform_device *dev)
2063{
2064 struct mpsc_port_info *pi;
2065 int rc = -ENODEV;
2066
2067 pr_debug("mpsc_drv_probe: Adding MPSC %d\n", dev->id);
2068
2069 if (dev->id < MPSC_NUM_CTLRS) {
2070 pi = &mpsc_ports[dev->id];
2071
2072 if (!(rc = mpsc_drv_map_regs(pi, dev))) {
2073 mpsc_drv_get_platform_data(pi, dev, dev->id);
2074 pi->port.dev = &dev->dev;
2075
2076 if (!(rc = mpsc_make_ready(pi))) {
2077 spin_lock_init(&pi->tx_lock);
2078 if (!(rc = uart_add_one_port(&mpsc_reg,
2079 &pi->port))) {
2080 rc = 0;
2081 } else {
2082 mpsc_release_port((struct uart_port *)
2083 pi);
2084 mpsc_drv_unmap_regs(pi);
2085 }
2086 } else {
2087 mpsc_drv_unmap_regs(pi);
2088 }
2089 }
2090 }
2091
2092 return rc;
2093}
2094
2095static int mpsc_drv_remove(struct platform_device *dev)
2096{
2097 pr_debug("mpsc_drv_exit: Removing MPSC %d\n", dev->id);
2098
2099 if (dev->id < MPSC_NUM_CTLRS) {
2100 uart_remove_one_port(&mpsc_reg, &mpsc_ports[dev->id].port);
2101 mpsc_release_port((struct uart_port *)
2102 &mpsc_ports[dev->id].port);
2103 mpsc_drv_unmap_regs(&mpsc_ports[dev->id]);
2104 return 0;
2105 } else {
2106 return -ENODEV;
2107 }
2108}
2109
2110static struct platform_driver mpsc_driver = {
2111 .probe = mpsc_drv_probe,
2112 .remove = mpsc_drv_remove,
2113 .driver = {
2114 .name = MPSC_CTLR_NAME,
2115 .owner = THIS_MODULE,
2116 },
2117};
2118
2119static int __init mpsc_drv_init(void)
2120{
2121 int rc;
2122
2123 printk(KERN_INFO "Serial: MPSC driver\n");
2124
2125 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2126 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2127
2128 if (!(rc = uart_register_driver(&mpsc_reg))) {
2129 if (!(rc = platform_driver_register(&mpsc_shared_driver))) {
2130 if ((rc = platform_driver_register(&mpsc_driver))) {
2131 platform_driver_unregister(&mpsc_shared_driver);
2132 uart_unregister_driver(&mpsc_reg);
2133 }
2134 } else {
2135 uart_unregister_driver(&mpsc_reg);
2136 }
2137 }
2138
2139 return rc;
2140}
2141
2142static void __exit mpsc_drv_exit(void)
2143{
2144 platform_driver_unregister(&mpsc_driver);
2145 platform_driver_unregister(&mpsc_shared_driver);
2146 uart_unregister_driver(&mpsc_reg);
2147 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2148 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2149}
2150
2151module_init(mpsc_drv_init);
2152module_exit(mpsc_drv_exit);
2153
2154MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
2155MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver");
2156MODULE_VERSION(MPSC_VERSION);
2157MODULE_LICENSE("GPL");
2158MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);
2159MODULE_ALIAS("platform:" MPSC_CTLR_NAME);
diff --git a/drivers/tty/serial/mrst_max3110.c b/drivers/tty/serial/mrst_max3110.c
new file mode 100644
index 000000000000..a764bf99743b
--- /dev/null
+++ b/drivers/tty/serial/mrst_max3110.c
@@ -0,0 +1,920 @@
1/*
2 * mrst_max3110.c - spi uart protocol driver for Maxim 3110
3 *
4 * Copyright (c) 2008-2010, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20/*
21 * Note:
22 * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has
23 * 1 word. If SPI master controller doesn't support sclk frequency change,
24 * then the char need be sent out one by one with some delay
25 *
26 * 2. Currently only RX available interrrupt is used, no need for waiting TXE
27 * interrupt for a low speed UART device
28 */
29
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/irq.h>
33#include <linux/init.h>
34#include <linux/console.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/serial_core.h>
38#include <linux/serial_reg.h>
39
40#include <linux/kthread.h>
41#include <linux/spi/spi.h>
42
43#include "mrst_max3110.h"
44
45#define PR_FMT "mrst_max3110: "
46
47#define UART_TX_NEEDED 1
48#define CON_TX_NEEDED 2
49#define BIT_IRQ_PENDING 3
50
51struct uart_max3110 {
52 struct uart_port port;
53 struct spi_device *spi;
54 char name[SPI_NAME_SIZE];
55
56 wait_queue_head_t wq;
57 struct task_struct *main_thread;
58 struct task_struct *read_thread;
59 struct mutex thread_mutex;
60
61 u32 baud;
62 u16 cur_conf;
63 u8 clock;
64 u8 parity, word_7bits;
65 u16 irq;
66
67 unsigned long uart_flags;
68
69 /* console related */
70 struct circ_buf con_xmit;
71};
72
73/* global data structure, may need be removed */
74static struct uart_max3110 *pmax;
75
76static void receive_chars(struct uart_max3110 *max,
77 unsigned char *str, int len);
78static int max3110_read_multi(struct uart_max3110 *max, u8 *buf);
79static void max3110_con_receive(struct uart_max3110 *max);
80
81static int max3110_write_then_read(struct uart_max3110 *max,
82 const void *txbuf, void *rxbuf, unsigned len, int always_fast)
83{
84 struct spi_device *spi = max->spi;
85 struct spi_message message;
86 struct spi_transfer x;
87 int ret;
88
89 spi_message_init(&message);
90 memset(&x, 0, sizeof x);
91 x.len = len;
92 x.tx_buf = txbuf;
93 x.rx_buf = rxbuf;
94 spi_message_add_tail(&x, &message);
95
96 if (always_fast)
97 x.speed_hz = spi->max_speed_hz;
98 else if (max->baud)
99 x.speed_hz = max->baud;
100
101 /* Do the i/o */
102 ret = spi_sync(spi, &message);
103 return ret;
104}
105
106/* Write a 16b word to the device */
107static int max3110_out(struct uart_max3110 *max, const u16 out)
108{
109 void *buf;
110 u16 *obuf, *ibuf;
111 u8 ch;
112 int ret;
113
114 buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
115 if (!buf)
116 return -ENOMEM;
117
118 obuf = buf;
119 ibuf = buf + 4;
120 *obuf = out;
121 ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
122 if (ret) {
123 pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
124 __func__, ret, out);
125 goto exit;
126 }
127
128 /* If some valid data is read back */
129 if (*ibuf & MAX3110_READ_DATA_AVAILABLE) {
130 ch = *ibuf & 0xff;
131 receive_chars(max, &ch, 1);
132 }
133
134exit:
135 kfree(buf);
136 return ret;
137}
138
139/*
140 * This is usually used to read data from SPIC RX FIFO, which doesn't
141 * need any delay like flushing character out.
142 *
143 * Return how many valide bytes are read back
144 */
145static int max3110_read_multi(struct uart_max3110 *max, u8 *rxbuf)
146{
147 void *buf;
148 u16 *obuf, *ibuf;
149 u8 *pbuf, valid_str[M3110_RX_FIFO_DEPTH];
150 int i, j, blen;
151
152 blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
153 buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
154 if (!buf) {
155 pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
156 return 0;
157 }
158
159 /* tx/rx always have the same length */
160 obuf = buf;
161 ibuf = buf + blen;
162
163 if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
164 kfree(buf);
165 return 0;
166 }
167
168 /* If caller doesn't provide a buffer, then handle received char */
169 pbuf = rxbuf ? rxbuf : valid_str;
170
171 for (i = 0, j = 0; i < M3110_RX_FIFO_DEPTH; i++) {
172 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
173 pbuf[j++] = ibuf[i] & 0xff;
174 }
175
176 if (j && (pbuf == valid_str))
177 receive_chars(max, valid_str, j);
178
179 kfree(buf);
180 return j;
181}
182
183static void serial_m3110_con_putchar(struct uart_port *port, int ch)
184{
185 struct uart_max3110 *max =
186 container_of(port, struct uart_max3110, port);
187 struct circ_buf *xmit = &max->con_xmit;
188
189 if (uart_circ_chars_free(xmit)) {
190 xmit->buf[xmit->head] = (char)ch;
191 xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
192 }
193}
194
195/*
196 * Print a string to the serial port trying not to disturb
197 * any possible real use of the port...
198 *
199 * The console_lock must be held when we get here.
200 */
201static void serial_m3110_con_write(struct console *co,
202 const char *s, unsigned int count)
203{
204 if (!pmax)
205 return;
206
207 uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
208
209 if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
210 wake_up_process(pmax->main_thread);
211}
212
213static int __init
214serial_m3110_con_setup(struct console *co, char *options)
215{
216 struct uart_max3110 *max = pmax;
217 int baud = 115200;
218 int bits = 8;
219 int parity = 'n';
220 int flow = 'n';
221
222 pr_info(PR_FMT "setting up console\n");
223
224 if (co->index == -1)
225 co->index = 0;
226
227 if (!max) {
228 pr_err(PR_FMT "pmax is NULL, return");
229 return -ENODEV;
230 }
231
232 if (options)
233 uart_parse_options(options, &baud, &parity, &bits, &flow);
234
235 return uart_set_options(&max->port, co, baud, parity, bits, flow);
236}
237
238static struct tty_driver *serial_m3110_con_device(struct console *co,
239 int *index)
240{
241 struct uart_driver *p = co->data;
242 *index = co->index;
243 return p->tty_driver;
244}
245
246static struct uart_driver serial_m3110_reg;
247static struct console serial_m3110_console = {
248 .name = "ttyS",
249 .write = serial_m3110_con_write,
250 .device = serial_m3110_con_device,
251 .setup = serial_m3110_con_setup,
252 .flags = CON_PRINTBUFFER,
253 .index = -1,
254 .data = &serial_m3110_reg,
255};
256
257static unsigned int serial_m3110_tx_empty(struct uart_port *port)
258{
259 return 1;
260}
261
262static void serial_m3110_stop_tx(struct uart_port *port)
263{
264 return;
265}
266
267/* stop_rx will be called in spin_lock env */
268static void serial_m3110_stop_rx(struct uart_port *port)
269{
270 return;
271}
272
273#define WORDS_PER_XFER 128
274static void send_circ_buf(struct uart_max3110 *max,
275 struct circ_buf *xmit)
276{
277 void *buf;
278 u16 *obuf, *ibuf;
279 u8 valid_str[WORDS_PER_XFER];
280 int i, j, len, blen, dma_size, left, ret = 0;
281
282
283 dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
284 buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
285 if (!buf)
286 return;
287 obuf = buf;
288 ibuf = buf + dma_size/2;
289
290 while (!uart_circ_empty(xmit)) {
291 left = uart_circ_chars_pending(xmit);
292 while (left) {
293 len = min(left, WORDS_PER_XFER);
294 blen = len * sizeof(u16);
295 memset(ibuf, 0, blen);
296
297 for (i = 0; i < len; i++) {
298 obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
299 xmit->tail = (xmit->tail + 1) &
300 (UART_XMIT_SIZE - 1);
301 }
302
303 /* Fail to send msg to console is not very critical */
304 ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
305 if (ret)
306 pr_warning(PR_FMT "%s(): get err msg %d\n",
307 __func__, ret);
308
309 for (i = 0, j = 0; i < len; i++) {
310 if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
311 valid_str[j++] = ibuf[i] & 0xff;
312 }
313
314 if (j)
315 receive_chars(max, valid_str, j);
316
317 max->port.icount.tx += len;
318 left -= len;
319 }
320 }
321
322 kfree(buf);
323}
324
325static void transmit_char(struct uart_max3110 *max)
326{
327 struct uart_port *port = &max->port;
328 struct circ_buf *xmit = &port->state->xmit;
329
330 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
331 return;
332
333 send_circ_buf(max, xmit);
334
335 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
336 uart_write_wakeup(port);
337
338 if (uart_circ_empty(xmit))
339 serial_m3110_stop_tx(port);
340}
341
342/*
343 * This will be called by uart_write() and tty_write, can't
344 * go to sleep
345 */
346static void serial_m3110_start_tx(struct uart_port *port)
347{
348 struct uart_max3110 *max =
349 container_of(port, struct uart_max3110, port);
350
351 if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
352 wake_up_process(max->main_thread);
353}
354
355static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
356{
357 struct uart_port *port = &max->port;
358 struct tty_struct *tty;
359 int usable;
360
361 /* If uart is not opened, just return */
362 if (!port->state)
363 return;
364
365 tty = port->state->port.tty;
366 if (!tty)
367 return;
368
369 while (len) {
370 usable = tty_buffer_request_room(tty, len);
371 if (usable) {
372 tty_insert_flip_string(tty, str, usable);
373 str += usable;
374 port->icount.rx += usable;
375 }
376 len -= usable;
377 }
378 tty_flip_buffer_push(tty);
379}
380
381/*
382 * This routine will be used in read_thread or RX IRQ handling,
383 * it will first do one round buffer read(8 words), if there is some
384 * valid RX data, will try to read 5 more rounds till all data
385 * is read out.
386 *
387 * Use stack space as data buffer to save some system load, and chose
388 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
389 * receiving bulk data, a much bigger buffer may cause stack overflow
390 */
391static void max3110_con_receive(struct uart_max3110 *max)
392{
393 int loop = 1, num, total = 0;
394 u8 recv_buf[512], *pbuf;
395
396 pbuf = recv_buf;
397 do {
398 num = max3110_read_multi(max, pbuf);
399
400 if (num) {
401 loop = 5;
402 pbuf += num;
403 total += num;
404
405 if (total >= 504) {
406 receive_chars(max, recv_buf, total);
407 pbuf = recv_buf;
408 total = 0;
409 }
410 }
411 } while (--loop);
412
413 if (total)
414 receive_chars(max, recv_buf, total);
415}
416
417static int max3110_main_thread(void *_max)
418{
419 struct uart_max3110 *max = _max;
420 wait_queue_head_t *wq = &max->wq;
421 int ret = 0;
422 struct circ_buf *xmit = &max->con_xmit;
423
424 pr_info(PR_FMT "start main thread\n");
425
426 do {
427 wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
428
429 mutex_lock(&max->thread_mutex);
430
431 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
432 max3110_con_receive(max);
433
434 /* first handle console output */
435 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
436 send_circ_buf(max, xmit);
437
438 /* handle uart output */
439 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
440 transmit_char(max);
441
442 mutex_unlock(&max->thread_mutex);
443
444 } while (!kthread_should_stop());
445
446 return ret;
447}
448
449static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
450{
451 struct uart_max3110 *max = dev_id;
452
453 /* max3110's irq is a falling edge, not level triggered,
454 * so no need to disable the irq */
455 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
456 wake_up_process(max->main_thread);
457
458 return IRQ_HANDLED;
459}
460
461/* if don't use RX IRQ, then need a thread to polling read */
462static int max3110_read_thread(void *_max)
463{
464 struct uart_max3110 *max = _max;
465
466 pr_info(PR_FMT "start read thread\n");
467 do {
468 /*
469 * If can't acquire the mutex, it means the main thread
470 * is running which will also perform the rx job
471 */
472 if (mutex_trylock(&max->thread_mutex)) {
473 max3110_con_receive(max);
474 mutex_unlock(&max->thread_mutex);
475 }
476
477 set_current_state(TASK_INTERRUPTIBLE);
478 schedule_timeout(HZ / 20);
479 } while (!kthread_should_stop());
480
481 return 0;
482}
483
484static int serial_m3110_startup(struct uart_port *port)
485{
486 struct uart_max3110 *max =
487 container_of(port, struct uart_max3110, port);
488 u16 config = 0;
489 int ret = 0;
490
491 if (port->line != 0) {
492 pr_err(PR_FMT "uart port startup failed\n");
493 return -1;
494 }
495
496 /* Disable all IRQ and config it to 115200, 8n1 */
497 config = WC_TAG | WC_FIFO_ENABLE
498 | WC_1_STOPBITS
499 | WC_8BIT_WORD
500 | WC_BAUD_DR2;
501
502 /* as we use thread to handle tx/rx, need set low latency */
503 port->state->port.tty->low_latency = 1;
504
505 if (max->irq) {
506 max->read_thread = NULL;
507 ret = request_irq(max->irq, serial_m3110_irq,
508 IRQ_TYPE_EDGE_FALLING, "max3110", max);
509 if (ret) {
510 max->irq = 0;
511 pr_err(PR_FMT "unable to allocate IRQ, polling\n");
512 } else {
513 /* Enable RX IRQ only */
514 config |= WC_RXA_IRQ_ENABLE;
515 }
516 }
517
518 if (max->irq == 0) {
519 /* If IRQ is disabled, start a read thread for input data */
520 max->read_thread =
521 kthread_run(max3110_read_thread, max, "max3110_read");
522 if (IS_ERR(max->read_thread)) {
523 ret = PTR_ERR(max->read_thread);
524 max->read_thread = NULL;
525 pr_err(PR_FMT "Can't create read thread!\n");
526 return ret;
527 }
528 }
529
530 ret = max3110_out(max, config);
531 if (ret) {
532 if (max->irq)
533 free_irq(max->irq, max);
534 if (max->read_thread)
535 kthread_stop(max->read_thread);
536 max->read_thread = NULL;
537 return ret;
538 }
539
540 max->cur_conf = config;
541 return 0;
542}
543
544static void serial_m3110_shutdown(struct uart_port *port)
545{
546 struct uart_max3110 *max =
547 container_of(port, struct uart_max3110, port);
548 u16 config;
549
550 if (max->read_thread) {
551 kthread_stop(max->read_thread);
552 max->read_thread = NULL;
553 }
554
555 if (max->irq)
556 free_irq(max->irq, max);
557
558 /* Disable interrupts from this port */
559 config = WC_TAG | WC_SW_SHDI;
560 max3110_out(max, config);
561}
562
563static void serial_m3110_release_port(struct uart_port *port)
564{
565}
566
567static int serial_m3110_request_port(struct uart_port *port)
568{
569 return 0;
570}
571
572static void serial_m3110_config_port(struct uart_port *port, int flags)
573{
574 port->type = PORT_MAX3100;
575}
576
577static int
578serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
579{
580 /* we don't want the core code to modify any port params */
581 return -EINVAL;
582}
583
584
585static const char *serial_m3110_type(struct uart_port *port)
586{
587 struct uart_max3110 *max =
588 container_of(port, struct uart_max3110, port);
589 return max->name;
590}
591
592static void
593serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
594 struct ktermios *old)
595{
596 struct uart_max3110 *max =
597 container_of(port, struct uart_max3110, port);
598 unsigned char cval;
599 unsigned int baud, parity = 0;
600 int clk_div = -1;
601 u16 new_conf = max->cur_conf;
602
603 switch (termios->c_cflag & CSIZE) {
604 case CS7:
605 cval = UART_LCR_WLEN7;
606 new_conf |= WC_7BIT_WORD;
607 break;
608 default:
609 /* We only support CS7 & CS8 */
610 termios->c_cflag &= ~CSIZE;
611 termios->c_cflag |= CS8;
612 case CS8:
613 cval = UART_LCR_WLEN8;
614 new_conf |= WC_8BIT_WORD;
615 break;
616 }
617
618 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
619
620 /* First calc the div for 1.8MHZ clock case */
621 switch (baud) {
622 case 300:
623 clk_div = WC_BAUD_DR384;
624 break;
625 case 600:
626 clk_div = WC_BAUD_DR192;
627 break;
628 case 1200:
629 clk_div = WC_BAUD_DR96;
630 break;
631 case 2400:
632 clk_div = WC_BAUD_DR48;
633 break;
634 case 4800:
635 clk_div = WC_BAUD_DR24;
636 break;
637 case 9600:
638 clk_div = WC_BAUD_DR12;
639 break;
640 case 19200:
641 clk_div = WC_BAUD_DR6;
642 break;
643 case 38400:
644 clk_div = WC_BAUD_DR3;
645 break;
646 case 57600:
647 clk_div = WC_BAUD_DR2;
648 break;
649 case 115200:
650 clk_div = WC_BAUD_DR1;
651 break;
652 case 230400:
653 if (max->clock & MAX3110_HIGH_CLK)
654 break;
655 default:
656 /* Pick the previous baud rate */
657 baud = max->baud;
658 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
659 tty_termios_encode_baud_rate(termios, baud, baud);
660 }
661
662 if (max->clock & MAX3110_HIGH_CLK) {
663 clk_div += 1;
664 /* High clk version max3110 doesn't support B300 */
665 if (baud == 300) {
666 baud = 600;
667 clk_div = WC_BAUD_DR384;
668 }
669 if (baud == 230400)
670 clk_div = WC_BAUD_DR1;
671 tty_termios_encode_baud_rate(termios, baud, baud);
672 }
673
674 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
675
676 if (unlikely(termios->c_cflag & CMSPAR))
677 termios->c_cflag &= ~CMSPAR;
678
679 if (termios->c_cflag & CSTOPB)
680 new_conf |= WC_2_STOPBITS;
681 else
682 new_conf &= ~WC_2_STOPBITS;
683
684 if (termios->c_cflag & PARENB) {
685 new_conf |= WC_PARITY_ENABLE;
686 parity |= UART_LCR_PARITY;
687 } else
688 new_conf &= ~WC_PARITY_ENABLE;
689
690 if (!(termios->c_cflag & PARODD))
691 parity |= UART_LCR_EPAR;
692 max->parity = parity;
693
694 uart_update_timeout(port, termios->c_cflag, baud);
695
696 new_conf |= WC_TAG;
697 if (new_conf != max->cur_conf) {
698 if (!max3110_out(max, new_conf)) {
699 max->cur_conf = new_conf;
700 max->baud = baud;
701 }
702 }
703}
704
705/* Don't handle hw handshaking */
706static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
707{
708 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
709}
710
711static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
712{
713}
714
715static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
716{
717}
718
719static void serial_m3110_pm(struct uart_port *port, unsigned int state,
720 unsigned int oldstate)
721{
722}
723
724static void serial_m3110_enable_ms(struct uart_port *port)
725{
726}
727
728struct uart_ops serial_m3110_ops = {
729 .tx_empty = serial_m3110_tx_empty,
730 .set_mctrl = serial_m3110_set_mctrl,
731 .get_mctrl = serial_m3110_get_mctrl,
732 .stop_tx = serial_m3110_stop_tx,
733 .start_tx = serial_m3110_start_tx,
734 .stop_rx = serial_m3110_stop_rx,
735 .enable_ms = serial_m3110_enable_ms,
736 .break_ctl = serial_m3110_break_ctl,
737 .startup = serial_m3110_startup,
738 .shutdown = serial_m3110_shutdown,
739 .set_termios = serial_m3110_set_termios,
740 .pm = serial_m3110_pm,
741 .type = serial_m3110_type,
742 .release_port = serial_m3110_release_port,
743 .request_port = serial_m3110_request_port,
744 .config_port = serial_m3110_config_port,
745 .verify_port = serial_m3110_verify_port,
746};
747
748static struct uart_driver serial_m3110_reg = {
749 .owner = THIS_MODULE,
750 .driver_name = "MRST serial",
751 .dev_name = "ttyS",
752 .major = TTY_MAJOR,
753 .minor = 64,
754 .nr = 1,
755 .cons = &serial_m3110_console,
756};
757
758#ifdef CONFIG_PM
759static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
760{
761 struct uart_max3110 *max = spi_get_drvdata(spi);
762
763 disable_irq(max->irq);
764 uart_suspend_port(&serial_m3110_reg, &max->port);
765 max3110_out(max, max->cur_conf | WC_SW_SHDI);
766 return 0;
767}
768
769static int serial_m3110_resume(struct spi_device *spi)
770{
771 struct uart_max3110 *max = spi_get_drvdata(spi);
772
773 max3110_out(max, max->cur_conf);
774 uart_resume_port(&serial_m3110_reg, &max->port);
775 enable_irq(max->irq);
776 return 0;
777}
778#else
779#define serial_m3110_suspend NULL
780#define serial_m3110_resume NULL
781#endif
782
783static int __devinit serial_m3110_probe(struct spi_device *spi)
784{
785 struct uart_max3110 *max;
786 void *buffer;
787 u16 res;
788 int ret = 0;
789
790 max = kzalloc(sizeof(*max), GFP_KERNEL);
791 if (!max)
792 return -ENOMEM;
793
794 /* Set spi info */
795 spi->bits_per_word = 16;
796 max->clock = MAX3110_HIGH_CLK;
797
798 spi_setup(spi);
799
800 max->port.type = PORT_MAX3100;
801 max->port.fifosize = 2; /* Only have 16b buffer */
802 max->port.ops = &serial_m3110_ops;
803 max->port.line = 0;
804 max->port.dev = &spi->dev;
805 max->port.uartclk = 115200;
806
807 max->spi = spi;
808 strcpy(max->name, spi->modalias);
809 max->irq = (u16)spi->irq;
810
811 mutex_init(&max->thread_mutex);
812
813 max->word_7bits = 0;
814 max->parity = 0;
815 max->baud = 0;
816
817 max->cur_conf = 0;
818 max->uart_flags = 0;
819
820 /* Check if reading configuration register returns something sane */
821
822 res = RC_TAG;
823 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
824 if (ret < 0 || res == 0 || res == 0xffff) {
825 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
826 res);
827 ret = -ENODEV;
828 goto err_get_page;
829 }
830
831 buffer = (void *)__get_free_page(GFP_KERNEL);
832 if (!buffer) {
833 ret = -ENOMEM;
834 goto err_get_page;
835 }
836 max->con_xmit.buf = buffer;
837 max->con_xmit.head = 0;
838 max->con_xmit.tail = 0;
839
840 init_waitqueue_head(&max->wq);
841
842 max->main_thread = kthread_run(max3110_main_thread,
843 max, "max3110_main");
844 if (IS_ERR(max->main_thread)) {
845 ret = PTR_ERR(max->main_thread);
846 goto err_kthread;
847 }
848
849 spi_set_drvdata(spi, max);
850 pmax = max;
851
852 /* Give membase a psudo value to pass serial_core's check */
853 max->port.membase = (void *)0xff110000;
854 uart_add_one_port(&serial_m3110_reg, &max->port);
855
856 return 0;
857
858err_kthread:
859 free_page((unsigned long)buffer);
860err_get_page:
861 kfree(max);
862 return ret;
863}
864
865static int __devexit serial_m3110_remove(struct spi_device *dev)
866{
867 struct uart_max3110 *max = spi_get_drvdata(dev);
868
869 if (!max)
870 return 0;
871
872 uart_remove_one_port(&serial_m3110_reg, &max->port);
873
874 free_page((unsigned long)max->con_xmit.buf);
875
876 if (max->main_thread)
877 kthread_stop(max->main_thread);
878
879 kfree(max);
880 return 0;
881}
882
883static struct spi_driver uart_max3110_driver = {
884 .driver = {
885 .name = "spi_max3111",
886 .bus = &spi_bus_type,
887 .owner = THIS_MODULE,
888 },
889 .probe = serial_m3110_probe,
890 .remove = __devexit_p(serial_m3110_remove),
891 .suspend = serial_m3110_suspend,
892 .resume = serial_m3110_resume,
893};
894
895static int __init serial_m3110_init(void)
896{
897 int ret = 0;
898
899 ret = uart_register_driver(&serial_m3110_reg);
900 if (ret)
901 return ret;
902
903 ret = spi_register_driver(&uart_max3110_driver);
904 if (ret)
905 uart_unregister_driver(&serial_m3110_reg);
906
907 return ret;
908}
909
910static void __exit serial_m3110_exit(void)
911{
912 spi_unregister_driver(&uart_max3110_driver);
913 uart_unregister_driver(&serial_m3110_reg);
914}
915
916module_init(serial_m3110_init);
917module_exit(serial_m3110_exit);
918
919MODULE_LICENSE("GPL v2");
920MODULE_ALIAS("max3110-uart");
diff --git a/drivers/tty/serial/mrst_max3110.h b/drivers/tty/serial/mrst_max3110.h
new file mode 100644
index 000000000000..c37ea48c825a
--- /dev/null
+++ b/drivers/tty/serial/mrst_max3110.h
@@ -0,0 +1,60 @@
1#ifndef _MRST_MAX3110_H
2#define _MRST_MAX3110_H
3
4#define MAX3110_HIGH_CLK 0x1 /* 3.6864 MHZ */
5#define MAX3110_LOW_CLK 0x0 /* 1.8432 MHZ */
6
7/* status bits for all 4 MAX3110 operate modes */
8#define MAX3110_READ_DATA_AVAILABLE (1 << 15)
9#define MAX3110_WRITE_BUF_EMPTY (1 << 14)
10
11#define WC_TAG (3 << 14)
12#define RC_TAG (1 << 14)
13#define WD_TAG (2 << 14)
14#define RD_TAG (0 << 14)
15
16/* bits def for write configuration */
17#define WC_FIFO_ENABLE_MASK (1 << 13)
18#define WC_FIFO_ENABLE (0 << 13)
19
20#define WC_SW_SHDI (1 << 12)
21
22#define WC_IRQ_MASK (0xF << 8)
23#define WC_TXE_IRQ_ENABLE (1 << 11) /* TX empty irq */
24#define WC_RXA_IRQ_ENABLE (1 << 10) /* RX available irq */
25#define WC_PAR_HIGH_IRQ_ENABLE (1 << 9)
26#define WC_REC_ACT_IRQ_ENABLE (1 << 8)
27
28#define WC_IRDA_ENABLE (1 << 7)
29
30#define WC_STOPBITS_MASK (1 << 6)
31#define WC_2_STOPBITS (1 << 6)
32#define WC_1_STOPBITS (0 << 6)
33
34#define WC_PARITY_ENABLE_MASK (1 << 5)
35#define WC_PARITY_ENABLE (1 << 5)
36
37#define WC_WORDLEN_MASK (1 << 4)
38#define WC_7BIT_WORD (1 << 4)
39#define WC_8BIT_WORD (0 << 4)
40
41#define WC_BAUD_DIV_MASK (0xF)
42#define WC_BAUD_DR1 (0x0)
43#define WC_BAUD_DR2 (0x1)
44#define WC_BAUD_DR4 (0x2)
45#define WC_BAUD_DR8 (0x3)
46#define WC_BAUD_DR16 (0x4)
47#define WC_BAUD_DR32 (0x5)
48#define WC_BAUD_DR64 (0x6)
49#define WC_BAUD_DR128 (0x7)
50#define WC_BAUD_DR3 (0x8)
51#define WC_BAUD_DR6 (0x9)
52#define WC_BAUD_DR12 (0xA)
53#define WC_BAUD_DR24 (0xB)
54#define WC_BAUD_DR48 (0xC)
55#define WC_BAUD_DR96 (0xD)
56#define WC_BAUD_DR192 (0xE)
57#define WC_BAUD_DR384 (0xF)
58
59#define M3110_RX_FIFO_DEPTH 8
60#endif
diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c
new file mode 100644
index 000000000000..e6ba83876508
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.c
@@ -0,0 +1,966 @@
1/*
2 * Driver for msm7k serial device and console
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
6 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
22#include <linux/hrtimer.h>
23#include <linux/module.h>
24#include <linux/io.h>
25#include <linux/ioport.h>
26#include <linux/irq.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33#include <linux/clk.h>
34#include <linux/platform_device.h>
35#include <linux/delay.h>
36
37#include "msm_serial.h"
38
39struct msm_port {
40 struct uart_port uart;
41 char name[16];
42 struct clk *clk;
43 struct clk *pclk;
44 unsigned int imr;
45 unsigned int *gsbi_base;
46 int is_uartdm;
47 unsigned int old_snap_state;
48};
49
50static inline void wait_for_xmitr(struct uart_port *port, int bits)
51{
52 if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY))
53 while ((msm_read(port, UART_ISR) & bits) != bits)
54 cpu_relax();
55}
56
57static void msm_stop_tx(struct uart_port *port)
58{
59 struct msm_port *msm_port = UART_TO_MSM(port);
60
61 msm_port->imr &= ~UART_IMR_TXLEV;
62 msm_write(port, msm_port->imr, UART_IMR);
63}
64
65static void msm_start_tx(struct uart_port *port)
66{
67 struct msm_port *msm_port = UART_TO_MSM(port);
68
69 msm_port->imr |= UART_IMR_TXLEV;
70 msm_write(port, msm_port->imr, UART_IMR);
71}
72
73static void msm_stop_rx(struct uart_port *port)
74{
75 struct msm_port *msm_port = UART_TO_MSM(port);
76
77 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
78 msm_write(port, msm_port->imr, UART_IMR);
79}
80
81static void msm_enable_ms(struct uart_port *port)
82{
83 struct msm_port *msm_port = UART_TO_MSM(port);
84
85 msm_port->imr |= UART_IMR_DELTA_CTS;
86 msm_write(port, msm_port->imr, UART_IMR);
87}
88
89static void handle_rx_dm(struct uart_port *port, unsigned int misr)
90{
91 struct tty_struct *tty = port->state->port.tty;
92 unsigned int sr;
93 int count = 0;
94 struct msm_port *msm_port = UART_TO_MSM(port);
95
96 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
97 port->icount.overrun++;
98 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
99 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
100 }
101
102 if (misr & UART_IMR_RXSTALE) {
103 count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
104 msm_port->old_snap_state;
105 msm_port->old_snap_state = 0;
106 } else {
107 count = 4 * (msm_read(port, UART_RFWR));
108 msm_port->old_snap_state += count;
109 }
110
111 /* TODO: Precise error reporting */
112
113 port->icount.rx += count;
114
115 while (count > 0) {
116 unsigned int c;
117
118 sr = msm_read(port, UART_SR);
119 if ((sr & UART_SR_RX_READY) == 0) {
120 msm_port->old_snap_state -= count;
121 break;
122 }
123 c = msm_read(port, UARTDM_RF);
124 if (sr & UART_SR_RX_BREAK) {
125 port->icount.brk++;
126 if (uart_handle_break(port))
127 continue;
128 } else if (sr & UART_SR_PAR_FRAME_ERR)
129 port->icount.frame++;
130
131 /* TODO: handle sysrq */
132 tty_insert_flip_string(tty, (char *) &c,
133 (count > 4) ? 4 : count);
134 count -= 4;
135 }
136
137 tty_flip_buffer_push(tty);
138 if (misr & (UART_IMR_RXSTALE))
139 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
140 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
141 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
142}
143
144static void handle_rx(struct uart_port *port)
145{
146 struct tty_struct *tty = port->state->port.tty;
147 unsigned int sr;
148
149 /*
150 * Handle overrun. My understanding of the hardware is that overrun
151 * is not tied to the RX buffer, so we handle the case out of band.
152 */
153 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
154 port->icount.overrun++;
155 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
156 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
157 }
158
159 /* and now the main RX loop */
160 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
161 unsigned int c;
162 char flag = TTY_NORMAL;
163
164 c = msm_read(port, UART_RF);
165
166 if (sr & UART_SR_RX_BREAK) {
167 port->icount.brk++;
168 if (uart_handle_break(port))
169 continue;
170 } else if (sr & UART_SR_PAR_FRAME_ERR) {
171 port->icount.frame++;
172 } else {
173 port->icount.rx++;
174 }
175
176 /* Mask conditions we're ignorning. */
177 sr &= port->read_status_mask;
178
179 if (sr & UART_SR_RX_BREAK) {
180 flag = TTY_BREAK;
181 } else if (sr & UART_SR_PAR_FRAME_ERR) {
182 flag = TTY_FRAME;
183 }
184
185 if (!uart_handle_sysrq_char(port, c))
186 tty_insert_flip_char(tty, c, flag);
187 }
188
189 tty_flip_buffer_push(tty);
190}
191
192static void reset_dm_count(struct uart_port *port)
193{
194 wait_for_xmitr(port, UART_ISR_TX_READY);
195 msm_write(port, 1, UARTDM_NCF_TX);
196}
197
198static void handle_tx(struct uart_port *port)
199{
200 struct circ_buf *xmit = &port->state->xmit;
201 struct msm_port *msm_port = UART_TO_MSM(port);
202 int sent_tx;
203
204 if (port->x_char) {
205 if (msm_port->is_uartdm)
206 reset_dm_count(port);
207
208 msm_write(port, port->x_char,
209 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
210 port->icount.tx++;
211 port->x_char = 0;
212 }
213
214 if (msm_port->is_uartdm)
215 reset_dm_count(port);
216
217 while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
218 if (uart_circ_empty(xmit)) {
219 /* disable tx interrupts */
220 msm_port->imr &= ~UART_IMR_TXLEV;
221 msm_write(port, msm_port->imr, UART_IMR);
222 break;
223 }
224 msm_write(port, xmit->buf[xmit->tail],
225 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
226
227 if (msm_port->is_uartdm)
228 reset_dm_count(port);
229
230 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
231 port->icount.tx++;
232 sent_tx = 1;
233 }
234
235 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
236 uart_write_wakeup(port);
237}
238
239static void handle_delta_cts(struct uart_port *port)
240{
241 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
242 port->icount.cts++;
243 wake_up_interruptible(&port->state->port.delta_msr_wait);
244}
245
246static irqreturn_t msm_irq(int irq, void *dev_id)
247{
248 struct uart_port *port = dev_id;
249 struct msm_port *msm_port = UART_TO_MSM(port);
250 unsigned int misr;
251
252 spin_lock(&port->lock);
253 misr = msm_read(port, UART_MISR);
254 msm_write(port, 0, UART_IMR); /* disable interrupt */
255
256 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
257 if (msm_port->is_uartdm)
258 handle_rx_dm(port, misr);
259 else
260 handle_rx(port);
261 }
262 if (misr & UART_IMR_TXLEV)
263 handle_tx(port);
264 if (misr & UART_IMR_DELTA_CTS)
265 handle_delta_cts(port);
266
267 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
268 spin_unlock(&port->lock);
269
270 return IRQ_HANDLED;
271}
272
273static unsigned int msm_tx_empty(struct uart_port *port)
274{
275 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
276}
277
278static unsigned int msm_get_mctrl(struct uart_port *port)
279{
280 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
281}
282
283
284static void msm_reset(struct uart_port *port)
285{
286 /* reset everything */
287 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
288 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
289 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
290 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
291 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
292 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
293}
294
295void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
296{
297 unsigned int mr;
298 mr = msm_read(port, UART_MR1);
299
300 if (!(mctrl & TIOCM_RTS)) {
301 mr &= ~UART_MR1_RX_RDY_CTL;
302 msm_write(port, mr, UART_MR1);
303 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
304 } else {
305 mr |= UART_MR1_RX_RDY_CTL;
306 msm_write(port, mr, UART_MR1);
307 }
308}
309
310static void msm_break_ctl(struct uart_port *port, int break_ctl)
311{
312 if (break_ctl)
313 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
314 else
315 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
316}
317
318static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
319{
320 unsigned int baud_code, rxstale, watermark;
321 struct msm_port *msm_port = UART_TO_MSM(port);
322
323 switch (baud) {
324 case 300:
325 baud_code = UART_CSR_300;
326 rxstale = 1;
327 break;
328 case 600:
329 baud_code = UART_CSR_600;
330 rxstale = 1;
331 break;
332 case 1200:
333 baud_code = UART_CSR_1200;
334 rxstale = 1;
335 break;
336 case 2400:
337 baud_code = UART_CSR_2400;
338 rxstale = 1;
339 break;
340 case 4800:
341 baud_code = UART_CSR_4800;
342 rxstale = 1;
343 break;
344 case 9600:
345 baud_code = UART_CSR_9600;
346 rxstale = 2;
347 break;
348 case 14400:
349 baud_code = UART_CSR_14400;
350 rxstale = 3;
351 break;
352 case 19200:
353 baud_code = UART_CSR_19200;
354 rxstale = 4;
355 break;
356 case 28800:
357 baud_code = UART_CSR_28800;
358 rxstale = 6;
359 break;
360 case 38400:
361 baud_code = UART_CSR_38400;
362 rxstale = 8;
363 break;
364 case 57600:
365 baud_code = UART_CSR_57600;
366 rxstale = 16;
367 break;
368 case 115200:
369 default:
370 baud_code = UART_CSR_115200;
371 baud = 115200;
372 rxstale = 31;
373 break;
374 }
375
376 if (msm_port->is_uartdm)
377 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
378
379 msm_write(port, baud_code, UART_CSR);
380
381 /* RX stale watermark */
382 watermark = UART_IPR_STALE_LSB & rxstale;
383 watermark |= UART_IPR_RXSTALE_LAST;
384 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
385 msm_write(port, watermark, UART_IPR);
386
387 /* set RX watermark */
388 watermark = (port->fifosize * 3) / 4;
389 msm_write(port, watermark, UART_RFWR);
390
391 /* set TX watermark */
392 msm_write(port, 10, UART_TFWR);
393
394 if (msm_port->is_uartdm) {
395 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
396 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
397 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
398 }
399
400 return baud;
401}
402
403
404static void msm_init_clock(struct uart_port *port)
405{
406 struct msm_port *msm_port = UART_TO_MSM(port);
407
408 clk_enable(msm_port->clk);
409 if (!IS_ERR(msm_port->pclk))
410 clk_enable(msm_port->pclk);
411 msm_serial_set_mnd_regs(port);
412}
413
414static int msm_startup(struct uart_port *port)
415{
416 struct msm_port *msm_port = UART_TO_MSM(port);
417 unsigned int data, rfr_level;
418 int ret;
419
420 snprintf(msm_port->name, sizeof(msm_port->name),
421 "msm_serial%d", port->line);
422
423 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
424 msm_port->name, port);
425 if (unlikely(ret))
426 return ret;
427
428 msm_init_clock(port);
429
430 if (likely(port->fifosize > 12))
431 rfr_level = port->fifosize - 12;
432 else
433 rfr_level = port->fifosize;
434
435 /* set automatic RFR level */
436 data = msm_read(port, UART_MR1);
437 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
438 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
439 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
440 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
441 msm_write(port, data, UART_MR1);
442
443 /* make sure that RXSTALE count is non-zero */
444 data = msm_read(port, UART_IPR);
445 if (unlikely(!data)) {
446 data |= UART_IPR_RXSTALE_LAST;
447 data |= UART_IPR_STALE_LSB;
448 msm_write(port, data, UART_IPR);
449 }
450
451 data = 0;
452 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
453 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
454 msm_reset(port);
455 data = UART_CR_TX_ENABLE;
456 }
457
458 data |= UART_CR_RX_ENABLE;
459 msm_write(port, data, UART_CR); /* enable TX & RX */
460
461 /* Make sure IPR is not 0 to start with*/
462 if (msm_port->is_uartdm)
463 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
464
465 /* turn on RX and CTS interrupts */
466 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
467 UART_IMR_CURRENT_CTS;
468
469 if (msm_port->is_uartdm) {
470 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
471 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
472 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
473 }
474
475 msm_write(port, msm_port->imr, UART_IMR);
476 return 0;
477}
478
479static void msm_shutdown(struct uart_port *port)
480{
481 struct msm_port *msm_port = UART_TO_MSM(port);
482
483 msm_port->imr = 0;
484 msm_write(port, 0, UART_IMR); /* disable interrupts */
485
486 clk_disable(msm_port->clk);
487
488 free_irq(port->irq, port);
489}
490
491static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
492 struct ktermios *old)
493{
494 unsigned long flags;
495 unsigned int baud, mr;
496
497 spin_lock_irqsave(&port->lock, flags);
498
499 /* calculate and set baud rate */
500 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
501 baud = msm_set_baud_rate(port, baud);
502 if (tty_termios_baud_rate(termios))
503 tty_termios_encode_baud_rate(termios, baud, baud);
504
505 /* calculate parity */
506 mr = msm_read(port, UART_MR2);
507 mr &= ~UART_MR2_PARITY_MODE;
508 if (termios->c_cflag & PARENB) {
509 if (termios->c_cflag & PARODD)
510 mr |= UART_MR2_PARITY_MODE_ODD;
511 else if (termios->c_cflag & CMSPAR)
512 mr |= UART_MR2_PARITY_MODE_SPACE;
513 else
514 mr |= UART_MR2_PARITY_MODE_EVEN;
515 }
516
517 /* calculate bits per char */
518 mr &= ~UART_MR2_BITS_PER_CHAR;
519 switch (termios->c_cflag & CSIZE) {
520 case CS5:
521 mr |= UART_MR2_BITS_PER_CHAR_5;
522 break;
523 case CS6:
524 mr |= UART_MR2_BITS_PER_CHAR_6;
525 break;
526 case CS7:
527 mr |= UART_MR2_BITS_PER_CHAR_7;
528 break;
529 case CS8:
530 default:
531 mr |= UART_MR2_BITS_PER_CHAR_8;
532 break;
533 }
534
535 /* calculate stop bits */
536 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
537 if (termios->c_cflag & CSTOPB)
538 mr |= UART_MR2_STOP_BIT_LEN_TWO;
539 else
540 mr |= UART_MR2_STOP_BIT_LEN_ONE;
541
542 /* set parity, bits per char, and stop bit */
543 msm_write(port, mr, UART_MR2);
544
545 /* calculate and set hardware flow control */
546 mr = msm_read(port, UART_MR1);
547 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
548 if (termios->c_cflag & CRTSCTS) {
549 mr |= UART_MR1_CTS_CTL;
550 mr |= UART_MR1_RX_RDY_CTL;
551 }
552 msm_write(port, mr, UART_MR1);
553
554 /* Configure status bits to ignore based on termio flags. */
555 port->read_status_mask = 0;
556 if (termios->c_iflag & INPCK)
557 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
558 if (termios->c_iflag & (BRKINT | PARMRK))
559 port->read_status_mask |= UART_SR_RX_BREAK;
560
561 uart_update_timeout(port, termios->c_cflag, baud);
562
563 spin_unlock_irqrestore(&port->lock, flags);
564}
565
566static const char *msm_type(struct uart_port *port)
567{
568 return "MSM";
569}
570
571static void msm_release_port(struct uart_port *port)
572{
573 struct platform_device *pdev = to_platform_device(port->dev);
574 struct msm_port *msm_port = UART_TO_MSM(port);
575 struct resource *uart_resource;
576 struct resource *gsbi_resource;
577 resource_size_t size;
578
579 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
580 if (unlikely(!uart_resource))
581 return;
582 size = resource_size(uart_resource);
583
584 release_mem_region(port->mapbase, size);
585 iounmap(port->membase);
586 port->membase = NULL;
587
588 if (msm_port->gsbi_base) {
589 iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base +
590 GSBI_CONTROL);
591
592 gsbi_resource = platform_get_resource_byname(pdev,
593 IORESOURCE_MEM,
594 "gsbi_resource");
595
596 if (unlikely(!gsbi_resource))
597 return;
598
599 size = resource_size(gsbi_resource);
600 release_mem_region(gsbi_resource->start, size);
601 iounmap(msm_port->gsbi_base);
602 msm_port->gsbi_base = NULL;
603 }
604}
605
606static int msm_request_port(struct uart_port *port)
607{
608 struct msm_port *msm_port = UART_TO_MSM(port);
609 struct platform_device *pdev = to_platform_device(port->dev);
610 struct resource *uart_resource;
611 struct resource *gsbi_resource;
612 resource_size_t size;
613 int ret;
614
615 uart_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
616 "uart_resource");
617 if (unlikely(!uart_resource))
618 return -ENXIO;
619
620 size = resource_size(uart_resource);
621
622 if (!request_mem_region(port->mapbase, size, "msm_serial"))
623 return -EBUSY;
624
625 port->membase = ioremap(port->mapbase, size);
626 if (!port->membase) {
627 ret = -EBUSY;
628 goto fail_release_port;
629 }
630
631 gsbi_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
632 "gsbi_resource");
633 /* Is this a GSBI-based port? */
634 if (gsbi_resource) {
635 size = resource_size(gsbi_resource);
636
637 if (!request_mem_region(gsbi_resource->start, size,
638 "msm_serial")) {
639 ret = -EBUSY;
640 goto fail_release_port;
641 }
642
643 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
644 if (!msm_port->gsbi_base) {
645 ret = -EBUSY;
646 goto fail_release_gsbi;
647 }
648 }
649
650 return 0;
651
652fail_release_gsbi:
653 release_mem_region(gsbi_resource->start, size);
654fail_release_port:
655 release_mem_region(port->mapbase, size);
656 return ret;
657}
658
659static void msm_config_port(struct uart_port *port, int flags)
660{
661 struct msm_port *msm_port = UART_TO_MSM(port);
662 int ret;
663 if (flags & UART_CONFIG_TYPE) {
664 port->type = PORT_MSM;
665 ret = msm_request_port(port);
666 if (ret)
667 return;
668 }
669
670 if (msm_port->is_uartdm)
671 iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base +
672 GSBI_CONTROL);
673}
674
675static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
676{
677 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
678 return -EINVAL;
679 if (unlikely(port->irq != ser->irq))
680 return -EINVAL;
681 return 0;
682}
683
684static void msm_power(struct uart_port *port, unsigned int state,
685 unsigned int oldstate)
686{
687 struct msm_port *msm_port = UART_TO_MSM(port);
688
689 switch (state) {
690 case 0:
691 clk_enable(msm_port->clk);
692 if (!IS_ERR(msm_port->pclk))
693 clk_enable(msm_port->pclk);
694 break;
695 case 3:
696 clk_disable(msm_port->clk);
697 if (!IS_ERR(msm_port->pclk))
698 clk_disable(msm_port->pclk);
699 break;
700 default:
701 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
702 }
703}
704
705static struct uart_ops msm_uart_pops = {
706 .tx_empty = msm_tx_empty,
707 .set_mctrl = msm_set_mctrl,
708 .get_mctrl = msm_get_mctrl,
709 .stop_tx = msm_stop_tx,
710 .start_tx = msm_start_tx,
711 .stop_rx = msm_stop_rx,
712 .enable_ms = msm_enable_ms,
713 .break_ctl = msm_break_ctl,
714 .startup = msm_startup,
715 .shutdown = msm_shutdown,
716 .set_termios = msm_set_termios,
717 .type = msm_type,
718 .release_port = msm_release_port,
719 .request_port = msm_request_port,
720 .config_port = msm_config_port,
721 .verify_port = msm_verify_port,
722 .pm = msm_power,
723};
724
725static struct msm_port msm_uart_ports[] = {
726 {
727 .uart = {
728 .iotype = UPIO_MEM,
729 .ops = &msm_uart_pops,
730 .flags = UPF_BOOT_AUTOCONF,
731 .fifosize = 64,
732 .line = 0,
733 },
734 },
735 {
736 .uart = {
737 .iotype = UPIO_MEM,
738 .ops = &msm_uart_pops,
739 .flags = UPF_BOOT_AUTOCONF,
740 .fifosize = 64,
741 .line = 1,
742 },
743 },
744 {
745 .uart = {
746 .iotype = UPIO_MEM,
747 .ops = &msm_uart_pops,
748 .flags = UPF_BOOT_AUTOCONF,
749 .fifosize = 64,
750 .line = 2,
751 },
752 },
753};
754
755#define UART_NR ARRAY_SIZE(msm_uart_ports)
756
757static inline struct uart_port *get_port_from_line(unsigned int line)
758{
759 return &msm_uart_ports[line].uart;
760}
761
762#ifdef CONFIG_SERIAL_MSM_CONSOLE
763
764static void msm_console_putchar(struct uart_port *port, int c)
765{
766 struct msm_port *msm_port = UART_TO_MSM(port);
767
768 if (msm_port->is_uartdm)
769 reset_dm_count(port);
770
771 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
772 ;
773 msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
774}
775
776static void msm_console_write(struct console *co, const char *s,
777 unsigned int count)
778{
779 struct uart_port *port;
780 struct msm_port *msm_port;
781
782 BUG_ON(co->index < 0 || co->index >= UART_NR);
783
784 port = get_port_from_line(co->index);
785 msm_port = UART_TO_MSM(port);
786
787 spin_lock(&port->lock);
788 uart_console_write(port, s, count, msm_console_putchar);
789 spin_unlock(&port->lock);
790}
791
792static int __init msm_console_setup(struct console *co, char *options)
793{
794 struct uart_port *port;
795 struct msm_port *msm_port;
796 int baud, flow, bits, parity;
797
798 if (unlikely(co->index >= UART_NR || co->index < 0))
799 return -ENXIO;
800
801 port = get_port_from_line(co->index);
802 msm_port = UART_TO_MSM(port);
803
804 if (unlikely(!port->membase))
805 return -ENXIO;
806
807 port->cons = co;
808
809 msm_init_clock(port);
810
811 if (options)
812 uart_parse_options(options, &baud, &parity, &bits, &flow);
813
814 bits = 8;
815 parity = 'n';
816 flow = 'n';
817 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
818 UART_MR2); /* 8N1 */
819
820 if (baud < 300 || baud > 115200)
821 baud = 115200;
822 msm_set_baud_rate(port, baud);
823
824 msm_reset(port);
825
826 if (msm_port->is_uartdm) {
827 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
828 msm_write(port, UART_CR_TX_ENABLE, UART_CR);
829 }
830
831 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
832
833 return uart_set_options(port, co, baud, parity, bits, flow);
834}
835
836static struct uart_driver msm_uart_driver;
837
838static struct console msm_console = {
839 .name = "ttyMSM",
840 .write = msm_console_write,
841 .device = uart_console_device,
842 .setup = msm_console_setup,
843 .flags = CON_PRINTBUFFER,
844 .index = -1,
845 .data = &msm_uart_driver,
846};
847
848#define MSM_CONSOLE (&msm_console)
849
850#else
851#define MSM_CONSOLE NULL
852#endif
853
854static struct uart_driver msm_uart_driver = {
855 .owner = THIS_MODULE,
856 .driver_name = "msm_serial",
857 .dev_name = "ttyMSM",
858 .nr = UART_NR,
859 .cons = MSM_CONSOLE,
860};
861
862static int __init msm_serial_probe(struct platform_device *pdev)
863{
864 struct msm_port *msm_port;
865 struct resource *resource;
866 struct uart_port *port;
867 int irq;
868
869 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
870 return -ENXIO;
871
872 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
873
874 port = get_port_from_line(pdev->id);
875 port->dev = &pdev->dev;
876 msm_port = UART_TO_MSM(port);
877
878 if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsbi_resource"))
879 msm_port->is_uartdm = 1;
880 else
881 msm_port->is_uartdm = 0;
882
883 if (msm_port->is_uartdm) {
884 msm_port->clk = clk_get(&pdev->dev, "gsbi_uart_clk");
885 msm_port->pclk = clk_get(&pdev->dev, "gsbi_pclk");
886 } else {
887 msm_port->clk = clk_get(&pdev->dev, "uart_clk");
888 msm_port->pclk = ERR_PTR(-ENOENT);
889 }
890
891 if (unlikely(IS_ERR(msm_port->clk) || (IS_ERR(msm_port->pclk) &&
892 msm_port->is_uartdm)))
893 return PTR_ERR(msm_port->clk);
894
895 if (msm_port->is_uartdm)
896 clk_set_rate(msm_port->clk, 7372800);
897
898 port->uartclk = clk_get_rate(msm_port->clk);
899 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
900
901
902 resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
903 "uart_resource");
904 if (unlikely(!resource))
905 return -ENXIO;
906 port->mapbase = resource->start;
907
908 irq = platform_get_irq(pdev, 0);
909 if (unlikely(irq < 0))
910 return -ENXIO;
911 port->irq = irq;
912
913 platform_set_drvdata(pdev, port);
914
915 return uart_add_one_port(&msm_uart_driver, port);
916}
917
918static int __devexit msm_serial_remove(struct platform_device *pdev)
919{
920 struct msm_port *msm_port = platform_get_drvdata(pdev);
921
922 clk_put(msm_port->clk);
923
924 return 0;
925}
926
927static struct platform_driver msm_platform_driver = {
928 .remove = msm_serial_remove,
929 .driver = {
930 .name = "msm_serial",
931 .owner = THIS_MODULE,
932 },
933};
934
935static int __init msm_serial_init(void)
936{
937 int ret;
938
939 ret = uart_register_driver(&msm_uart_driver);
940 if (unlikely(ret))
941 return ret;
942
943 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
944 if (unlikely(ret))
945 uart_unregister_driver(&msm_uart_driver);
946
947 printk(KERN_INFO "msm_serial: driver initialized\n");
948
949 return ret;
950}
951
952static void __exit msm_serial_exit(void)
953{
954#ifdef CONFIG_SERIAL_MSM_CONSOLE
955 unregister_console(&msm_console);
956#endif
957 platform_driver_unregister(&msm_platform_driver);
958 uart_unregister_driver(&msm_uart_driver);
959}
960
961module_init(msm_serial_init);
962module_exit(msm_serial_exit);
963
964MODULE_AUTHOR("Robert Love <rlove@google.com>");
965MODULE_DESCRIPTION("Driver for msm7x serial device");
966MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/msm_serial.h b/drivers/tty/serial/msm_serial.h
new file mode 100644
index 000000000000..e4acef5de77e
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.h
@@ -0,0 +1,187 @@
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Author: Robert Love <rlove@google.com>
4 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#ifndef __DRIVERS_SERIAL_MSM_SERIAL_H
17#define __DRIVERS_SERIAL_MSM_SERIAL_H
18
19#define UART_MR1 0x0000
20
21#define UART_MR1_AUTO_RFR_LEVEL0 0x3F
22#define UART_MR1_AUTO_RFR_LEVEL1 0x3FF00
23#define UART_MR1_RX_RDY_CTL (1 << 7)
24#define UART_MR1_CTS_CTL (1 << 6)
25
26#define UART_MR2 0x0004
27#define UART_MR2_ERROR_MODE (1 << 6)
28#define UART_MR2_BITS_PER_CHAR 0x30
29#define UART_MR2_BITS_PER_CHAR_5 (0x0 << 4)
30#define UART_MR2_BITS_PER_CHAR_6 (0x1 << 4)
31#define UART_MR2_BITS_PER_CHAR_7 (0x2 << 4)
32#define UART_MR2_BITS_PER_CHAR_8 (0x3 << 4)
33#define UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2)
34#define UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2)
35#define UART_MR2_PARITY_MODE_NONE 0x0
36#define UART_MR2_PARITY_MODE_ODD 0x1
37#define UART_MR2_PARITY_MODE_EVEN 0x2
38#define UART_MR2_PARITY_MODE_SPACE 0x3
39#define UART_MR2_PARITY_MODE 0x3
40
41#define UART_CSR 0x0008
42#define UART_CSR_115200 0xFF
43#define UART_CSR_57600 0xEE
44#define UART_CSR_38400 0xDD
45#define UART_CSR_28800 0xCC
46#define UART_CSR_19200 0xBB
47#define UART_CSR_14400 0xAA
48#define UART_CSR_9600 0x99
49#define UART_CSR_4800 0x77
50#define UART_CSR_2400 0x55
51#define UART_CSR_1200 0x44
52#define UART_CSR_600 0x33
53#define UART_CSR_300 0x22
54
55#define UART_TF 0x000C
56#define UARTDM_TF 0x0070
57
58#define UART_CR 0x0010
59#define UART_CR_CMD_NULL (0 << 4)
60#define UART_CR_CMD_RESET_RX (1 << 4)
61#define UART_CR_CMD_RESET_TX (2 << 4)
62#define UART_CR_CMD_RESET_ERR (3 << 4)
63#define UART_CR_CMD_RESET_BREAK_INT (4 << 4)
64#define UART_CR_CMD_START_BREAK (5 << 4)
65#define UART_CR_CMD_STOP_BREAK (6 << 4)
66#define UART_CR_CMD_RESET_CTS (7 << 4)
67#define UART_CR_CMD_RESET_STALE_INT (8 << 4)
68#define UART_CR_CMD_PACKET_MODE (9 << 4)
69#define UART_CR_CMD_MODE_RESET (12 << 4)
70#define UART_CR_CMD_SET_RFR (13 << 4)
71#define UART_CR_CMD_RESET_RFR (14 << 4)
72#define UART_CR_CMD_PROTECTION_EN (16 << 4)
73#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
74#define UART_CR_TX_DISABLE (1 << 3)
75#define UART_CR_TX_ENABLE (1 << 2)
76#define UART_CR_RX_DISABLE (1 << 1)
77#define UART_CR_RX_ENABLE (1 << 0)
78
79#define UART_IMR 0x0014
80#define UART_IMR_TXLEV (1 << 0)
81#define UART_IMR_RXSTALE (1 << 3)
82#define UART_IMR_RXLEV (1 << 4)
83#define UART_IMR_DELTA_CTS (1 << 5)
84#define UART_IMR_CURRENT_CTS (1 << 6)
85
86#define UART_IPR_RXSTALE_LAST 0x20
87#define UART_IPR_STALE_LSB 0x1F
88#define UART_IPR_STALE_TIMEOUT_MSB 0x3FF80
89
90#define UART_IPR 0x0018
91#define UART_TFWR 0x001C
92#define UART_RFWR 0x0020
93#define UART_HCR 0x0024
94
95#define UART_MREG 0x0028
96#define UART_NREG 0x002C
97#define UART_DREG 0x0030
98#define UART_MNDREG 0x0034
99#define UART_IRDA 0x0038
100#define UART_MISR_MODE 0x0040
101#define UART_MISR_RESET 0x0044
102#define UART_MISR_EXPORT 0x0048
103#define UART_MISR_VAL 0x004C
104#define UART_TEST_CTRL 0x0050
105
106#define UART_SR 0x0008
107#define UART_SR_HUNT_CHAR (1 << 7)
108#define UART_SR_RX_BREAK (1 << 6)
109#define UART_SR_PAR_FRAME_ERR (1 << 5)
110#define UART_SR_OVERRUN (1 << 4)
111#define UART_SR_TX_EMPTY (1 << 3)
112#define UART_SR_TX_READY (1 << 2)
113#define UART_SR_RX_FULL (1 << 1)
114#define UART_SR_RX_READY (1 << 0)
115
116#define UART_RF 0x000C
117#define UARTDM_RF 0x0070
118#define UART_MISR 0x0010
119#define UART_ISR 0x0014
120#define UART_ISR_TX_READY (1 << 7)
121
122#define GSBI_CONTROL 0x0
123#define GSBI_PROTOCOL_CODE 0x30
124#define GSBI_PROTOCOL_UART 0x40
125#define GSBI_PROTOCOL_IDLE 0x0
126
127#define UARTDM_DMRX 0x34
128#define UARTDM_NCF_TX 0x40
129#define UARTDM_RX_TOTAL_SNAP 0x38
130
131#define UART_TO_MSM(uart_port) ((struct msm_port *) uart_port)
132
133static inline
134void msm_write(struct uart_port *port, unsigned int val, unsigned int off)
135{
136 __raw_writel(val, port->membase + off);
137}
138
139static inline
140unsigned int msm_read(struct uart_port *port, unsigned int off)
141{
142 return __raw_readl(port->membase + off);
143}
144
145/*
146 * Setup the MND registers to use the TCXO clock.
147 */
148static inline void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
149{
150 msm_write(port, 0x06, UART_MREG);
151 msm_write(port, 0xF1, UART_NREG);
152 msm_write(port, 0x0F, UART_DREG);
153 msm_write(port, 0x1A, UART_MNDREG);
154}
155
156/*
157 * Setup the MND registers to use the TCXO clock divided by 4.
158 */
159static inline void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
160{
161 msm_write(port, 0x18, UART_MREG);
162 msm_write(port, 0xF6, UART_NREG);
163 msm_write(port, 0x0F, UART_DREG);
164 msm_write(port, 0x0A, UART_MNDREG);
165}
166
167static inline
168void msm_serial_set_mnd_regs_from_uartclk(struct uart_port *port)
169{
170 if (port->uartclk == 19200000)
171 msm_serial_set_mnd_regs_tcxo(port);
172 else
173 msm_serial_set_mnd_regs_tcxoby4(port);
174}
175
176/*
177 * TROUT has a specific defect that makes it report it's uartclk
178 * as 19.2Mhz (TCXO) when it's actually 4.8Mhz (TCXO/4). This special
179 * cases TROUT to use the right clock.
180 */
181#ifdef CONFIG_MACH_TROUT
182#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_tcxoby4
183#else
184#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_from_uartclk
185#endif
186
187#endif /* __DRIVERS_SERIAL_MSM_SERIAL_H */
diff --git a/drivers/tty/serial/msm_serial_hs.c b/drivers/tty/serial/msm_serial_hs.c
new file mode 100644
index 000000000000..624701f8138a
--- /dev/null
+++ b/drivers/tty/serial/msm_serial_hs.c
@@ -0,0 +1,1880 @@
1/*
2 * MSM 7k/8k High speed uart driver
3 *
4 * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
5 * Copyright (c) 2008 Google Inc.
6 * Modified: Nick Pelly <npelly@google.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * Has optional support for uart power management independent of linux
18 * suspend/resume:
19 *
20 * RX wakeup.
21 * UART wakeup can be triggered by RX activity (using a wakeup GPIO on the
22 * UART RX pin). This should only be used if there is not a wakeup
23 * GPIO on the UART CTS, and the first RX byte is known (for example, with the
24 * Bluetooth Texas Instruments HCILL protocol), since the first RX byte will
25 * always be lost. RTS will be asserted even while the UART is off in this mode
26 * of operation. See msm_serial_hs_platform_data.rx_wakeup_irq.
27 */
28
29#include <linux/module.h>
30
31#include <linux/serial.h>
32#include <linux/serial_core.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/interrupt.h>
36#include <linux/irq.h>
37#include <linux/io.h>
38#include <linux/ioport.h>
39#include <linux/kernel.h>
40#include <linux/timer.h>
41#include <linux/clk.h>
42#include <linux/platform_device.h>
43#include <linux/pm_runtime.h>
44#include <linux/dma-mapping.h>
45#include <linux/dmapool.h>
46#include <linux/wait.h>
47#include <linux/workqueue.h>
48
49#include <linux/atomic.h>
50#include <asm/irq.h>
51#include <asm/system.h>
52
53#include <mach/hardware.h>
54#include <mach/dma.h>
55#include <linux/platform_data/msm_serial_hs.h>
56
57/* HSUART Registers */
58#define UARTDM_MR1_ADDR 0x0
59#define UARTDM_MR2_ADDR 0x4
60
61/* Data Mover result codes */
62#define RSLT_FIFO_CNTR_BMSK (0xE << 28)
63#define RSLT_VLD BIT(1)
64
65/* write only register */
66#define UARTDM_CSR_ADDR 0x8
67#define UARTDM_CSR_115200 0xFF
68#define UARTDM_CSR_57600 0xEE
69#define UARTDM_CSR_38400 0xDD
70#define UARTDM_CSR_28800 0xCC
71#define UARTDM_CSR_19200 0xBB
72#define UARTDM_CSR_14400 0xAA
73#define UARTDM_CSR_9600 0x99
74#define UARTDM_CSR_7200 0x88
75#define UARTDM_CSR_4800 0x77
76#define UARTDM_CSR_3600 0x66
77#define UARTDM_CSR_2400 0x55
78#define UARTDM_CSR_1200 0x44
79#define UARTDM_CSR_600 0x33
80#define UARTDM_CSR_300 0x22
81#define UARTDM_CSR_150 0x11
82#define UARTDM_CSR_75 0x00
83
84/* write only register */
85#define UARTDM_TF_ADDR 0x70
86#define UARTDM_TF2_ADDR 0x74
87#define UARTDM_TF3_ADDR 0x78
88#define UARTDM_TF4_ADDR 0x7C
89
90/* write only register */
91#define UARTDM_CR_ADDR 0x10
92#define UARTDM_IMR_ADDR 0x14
93
94#define UARTDM_IPR_ADDR 0x18
95#define UARTDM_TFWR_ADDR 0x1c
96#define UARTDM_RFWR_ADDR 0x20
97#define UARTDM_HCR_ADDR 0x24
98#define UARTDM_DMRX_ADDR 0x34
99#define UARTDM_IRDA_ADDR 0x38
100#define UARTDM_DMEN_ADDR 0x3c
101
102/* UART_DM_NO_CHARS_FOR_TX */
103#define UARTDM_NCF_TX_ADDR 0x40
104
105#define UARTDM_BADR_ADDR 0x44
106
107#define UARTDM_SIM_CFG_ADDR 0x80
108/* Read Only register */
109#define UARTDM_SR_ADDR 0x8
110
111/* Read Only register */
112#define UARTDM_RF_ADDR 0x70
113#define UARTDM_RF2_ADDR 0x74
114#define UARTDM_RF3_ADDR 0x78
115#define UARTDM_RF4_ADDR 0x7C
116
117/* Read Only register */
118#define UARTDM_MISR_ADDR 0x10
119
120/* Read Only register */
121#define UARTDM_ISR_ADDR 0x14
122#define UARTDM_RX_TOTAL_SNAP_ADDR 0x38
123
124#define UARTDM_RXFS_ADDR 0x50
125
126/* Register field Mask Mapping */
127#define UARTDM_SR_PAR_FRAME_BMSK BIT(5)
128#define UARTDM_SR_OVERRUN_BMSK BIT(4)
129#define UARTDM_SR_TXEMT_BMSK BIT(3)
130#define UARTDM_SR_TXRDY_BMSK BIT(2)
131#define UARTDM_SR_RXRDY_BMSK BIT(0)
132
133#define UARTDM_CR_TX_DISABLE_BMSK BIT(3)
134#define UARTDM_CR_RX_DISABLE_BMSK BIT(1)
135#define UARTDM_CR_TX_EN_BMSK BIT(2)
136#define UARTDM_CR_RX_EN_BMSK BIT(0)
137
138/* UARTDM_CR channel_comman bit value (register field is bits 8:4) */
139#define RESET_RX 0x10
140#define RESET_TX 0x20
141#define RESET_ERROR_STATUS 0x30
142#define RESET_BREAK_INT 0x40
143#define START_BREAK 0x50
144#define STOP_BREAK 0x60
145#define RESET_CTS 0x70
146#define RESET_STALE_INT 0x80
147#define RFR_LOW 0xD0
148#define RFR_HIGH 0xE0
149#define CR_PROTECTION_EN 0x100
150#define STALE_EVENT_ENABLE 0x500
151#define STALE_EVENT_DISABLE 0x600
152#define FORCE_STALE_EVENT 0x400
153#define CLEAR_TX_READY 0x300
154#define RESET_TX_ERROR 0x800
155#define RESET_TX_DONE 0x810
156
157#define UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK 0xffffff00
158#define UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK 0x3f
159#define UARTDM_MR1_CTS_CTL_BMSK 0x40
160#define UARTDM_MR1_RX_RDY_CTL_BMSK 0x80
161
162#define UARTDM_MR2_ERROR_MODE_BMSK 0x40
163#define UARTDM_MR2_BITS_PER_CHAR_BMSK 0x30
164
165/* bits per character configuration */
166#define FIVE_BPC (0 << 4)
167#define SIX_BPC (1 << 4)
168#define SEVEN_BPC (2 << 4)
169#define EIGHT_BPC (3 << 4)
170
171#define UARTDM_MR2_STOP_BIT_LEN_BMSK 0xc
172#define STOP_BIT_ONE (1 << 2)
173#define STOP_BIT_TWO (3 << 2)
174
175#define UARTDM_MR2_PARITY_MODE_BMSK 0x3
176
177/* Parity configuration */
178#define NO_PARITY 0x0
179#define EVEN_PARITY 0x1
180#define ODD_PARITY 0x2
181#define SPACE_PARITY 0x3
182
183#define UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK 0xffffff80
184#define UARTDM_IPR_STALE_LSB_BMSK 0x1f
185
186/* These can be used for both ISR and IMR register */
187#define UARTDM_ISR_TX_READY_BMSK BIT(7)
188#define UARTDM_ISR_CURRENT_CTS_BMSK BIT(6)
189#define UARTDM_ISR_DELTA_CTS_BMSK BIT(5)
190#define UARTDM_ISR_RXLEV_BMSK BIT(4)
191#define UARTDM_ISR_RXSTALE_BMSK BIT(3)
192#define UARTDM_ISR_RXBREAK_BMSK BIT(2)
193#define UARTDM_ISR_RXHUNT_BMSK BIT(1)
194#define UARTDM_ISR_TXLEV_BMSK BIT(0)
195
196/* Field definitions for UART_DM_DMEN*/
197#define UARTDM_TX_DM_EN_BMSK 0x1
198#define UARTDM_RX_DM_EN_BMSK 0x2
199
200#define UART_FIFOSIZE 64
201#define UARTCLK 7372800
202
203/* Rx DMA request states */
204enum flush_reason {
205 FLUSH_NONE,
206 FLUSH_DATA_READY,
207 FLUSH_DATA_INVALID, /* values after this indicate invalid data */
208 FLUSH_IGNORE = FLUSH_DATA_INVALID,
209 FLUSH_STOP,
210 FLUSH_SHUTDOWN,
211};
212
213/* UART clock states */
214enum msm_hs_clk_states_e {
215 MSM_HS_CLK_PORT_OFF, /* port not in use */
216 MSM_HS_CLK_OFF, /* clock disabled */
217 MSM_HS_CLK_REQUEST_OFF, /* disable after TX and RX flushed */
218 MSM_HS_CLK_ON, /* clock enabled */
219};
220
221/* Track the forced RXSTALE flush during clock off sequence.
222 * These states are only valid during MSM_HS_CLK_REQUEST_OFF */
223enum msm_hs_clk_req_off_state_e {
224 CLK_REQ_OFF_START,
225 CLK_REQ_OFF_RXSTALE_ISSUED,
226 CLK_REQ_OFF_FLUSH_ISSUED,
227 CLK_REQ_OFF_RXSTALE_FLUSHED,
228};
229
230/**
231 * struct msm_hs_tx
232 * @tx_ready_int_en: ok to dma more tx?
233 * @dma_in_flight: tx dma in progress
234 * @xfer: top level DMA command pointer structure
235 * @command_ptr: third level command struct pointer
236 * @command_ptr_ptr: second level command list struct pointer
237 * @mapped_cmd_ptr: DMA view of third level command struct
238 * @mapped_cmd_ptr_ptr: DMA view of second level command list struct
239 * @tx_count: number of bytes to transfer in DMA transfer
240 * @dma_base: DMA view of UART xmit buffer
241 *
242 * This structure describes a single Tx DMA transaction. MSM DMA
243 * commands have two levels of indirection. The top level command
244 * ptr points to a list of command ptr which in turn points to a
245 * single DMA 'command'. In our case each Tx transaction consists
246 * of a single second level pointer pointing to a 'box type' command.
247 */
248struct msm_hs_tx {
249 unsigned int tx_ready_int_en;
250 unsigned int dma_in_flight;
251 struct msm_dmov_cmd xfer;
252 dmov_box *command_ptr;
253 u32 *command_ptr_ptr;
254 dma_addr_t mapped_cmd_ptr;
255 dma_addr_t mapped_cmd_ptr_ptr;
256 int tx_count;
257 dma_addr_t dma_base;
258};
259
260/**
261 * struct msm_hs_rx
262 * @flush: Rx DMA request state
263 * @xfer: top level DMA command pointer structure
264 * @cmdptr_dmaaddr: DMA view of second level command structure
265 * @command_ptr: third level DMA command pointer structure
266 * @command_ptr_ptr: second level DMA command list pointer
267 * @mapped_cmd_ptr: DMA view of the third level command structure
268 * @wait: wait for DMA completion before shutdown
269 * @buffer: destination buffer for RX DMA
270 * @rbuffer: DMA view of buffer
271 * @pool: dma pool out of which coherent rx buffer is allocated
272 * @tty_work: private work-queue for tty flip buffer push task
273 *
274 * This structure describes a single Rx DMA transaction. Rx DMA
275 * transactions use box mode DMA commands.
276 */
277struct msm_hs_rx {
278 enum flush_reason flush;
279 struct msm_dmov_cmd xfer;
280 dma_addr_t cmdptr_dmaaddr;
281 dmov_box *command_ptr;
282 u32 *command_ptr_ptr;
283 dma_addr_t mapped_cmd_ptr;
284 wait_queue_head_t wait;
285 dma_addr_t rbuffer;
286 unsigned char *buffer;
287 struct dma_pool *pool;
288 struct work_struct tty_work;
289};
290
291/**
292 * struct msm_hs_rx_wakeup
293 * @irq: IRQ line to be configured as interrupt source on Rx activity
294 * @ignore: boolean value. 1 = ignore the wakeup interrupt
295 * @rx_to_inject: extra character to be inserted to Rx tty on wakeup
296 * @inject_rx: 1 = insert rx_to_inject. 0 = do not insert extra character
297 *
298 * This is an optional structure required for UART Rx GPIO IRQ based
299 * wakeup from low power state. UART wakeup can be triggered by RX activity
300 * (using a wakeup GPIO on the UART RX pin). This should only be used if
301 * there is not a wakeup GPIO on the UART CTS, and the first RX byte is
302 * known (eg., with the Bluetooth Texas Instruments HCILL protocol),
303 * since the first RX byte will always be lost. RTS will be asserted even
304 * while the UART is clocked off in this mode of operation.
305 */
306struct msm_hs_rx_wakeup {
307 int irq; /* < 0 indicates low power wakeup disabled */
308 unsigned char ignore;
309 unsigned char inject_rx;
310 char rx_to_inject;
311};
312
313/**
314 * struct msm_hs_port
315 * @uport: embedded uart port structure
316 * @imr_reg: shadow value of UARTDM_IMR
317 * @clk: uart input clock handle
318 * @tx: Tx transaction related data structure
319 * @rx: Rx transaction related data structure
320 * @dma_tx_channel: Tx DMA command channel
321 * @dma_rx_channel Rx DMA command channel
322 * @dma_tx_crci: Tx channel rate control interface number
323 * @dma_rx_crci: Rx channel rate control interface number
324 * @clk_off_timer: Timer to poll DMA event completion before clock off
325 * @clk_off_delay: clk_off_timer poll interval
326 * @clk_state: overall clock state
327 * @clk_req_off_state: post flush clock states
328 * @rx_wakeup: optional rx_wakeup feature related data
329 * @exit_lpm_cb: optional callback to exit low power mode
330 *
331 * Low level serial port structure.
332 */
333struct msm_hs_port {
334 struct uart_port uport;
335 unsigned long imr_reg;
336 struct clk *clk;
337 struct msm_hs_tx tx;
338 struct msm_hs_rx rx;
339
340 int dma_tx_channel;
341 int dma_rx_channel;
342 int dma_tx_crci;
343 int dma_rx_crci;
344
345 struct hrtimer clk_off_timer;
346 ktime_t clk_off_delay;
347 enum msm_hs_clk_states_e clk_state;
348 enum msm_hs_clk_req_off_state_e clk_req_off_state;
349
350 struct msm_hs_rx_wakeup rx_wakeup;
351 void (*exit_lpm_cb)(struct uart_port *);
352};
353
354#define MSM_UARTDM_BURST_SIZE 16 /* DM burst size (in bytes) */
355#define UARTDM_TX_BUF_SIZE UART_XMIT_SIZE
356#define UARTDM_RX_BUF_SIZE 512
357
358#define UARTDM_NR 2
359
360static struct msm_hs_port q_uart_port[UARTDM_NR];
361static struct platform_driver msm_serial_hs_platform_driver;
362static struct uart_driver msm_hs_driver;
363static struct uart_ops msm_hs_ops;
364static struct workqueue_struct *msm_hs_workqueue;
365
366#define UARTDM_TO_MSM(uart_port) \
367 container_of((uart_port), struct msm_hs_port, uport)
368
369static unsigned int use_low_power_rx_wakeup(struct msm_hs_port
370 *msm_uport)
371{
372 return (msm_uport->rx_wakeup.irq >= 0);
373}
374
375static unsigned int msm_hs_read(struct uart_port *uport,
376 unsigned int offset)
377{
378 return ioread32(uport->membase + offset);
379}
380
381static void msm_hs_write(struct uart_port *uport, unsigned int offset,
382 unsigned int value)
383{
384 iowrite32(value, uport->membase + offset);
385}
386
387static void msm_hs_release_port(struct uart_port *port)
388{
389 iounmap(port->membase);
390}
391
392static int msm_hs_request_port(struct uart_port *port)
393{
394 port->membase = ioremap(port->mapbase, PAGE_SIZE);
395 if (unlikely(!port->membase))
396 return -ENOMEM;
397
398 /* configure the CR Protection to Enable */
399 msm_hs_write(port, UARTDM_CR_ADDR, CR_PROTECTION_EN);
400 return 0;
401}
402
403static int __devexit msm_hs_remove(struct platform_device *pdev)
404{
405
406 struct msm_hs_port *msm_uport;
407 struct device *dev;
408
409 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
410 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
411 return -EINVAL;
412 }
413
414 msm_uport = &q_uart_port[pdev->id];
415 dev = msm_uport->uport.dev;
416
417 dma_unmap_single(dev, msm_uport->rx.mapped_cmd_ptr, sizeof(dmov_box),
418 DMA_TO_DEVICE);
419 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
420 msm_uport->rx.rbuffer);
421 dma_pool_destroy(msm_uport->rx.pool);
422
423 dma_unmap_single(dev, msm_uport->rx.cmdptr_dmaaddr, sizeof(u32 *),
424 DMA_TO_DEVICE);
425 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr_ptr, sizeof(u32 *),
426 DMA_TO_DEVICE);
427 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr, sizeof(dmov_box),
428 DMA_TO_DEVICE);
429
430 uart_remove_one_port(&msm_hs_driver, &msm_uport->uport);
431 clk_put(msm_uport->clk);
432
433 /* Free the tx resources */
434 kfree(msm_uport->tx.command_ptr);
435 kfree(msm_uport->tx.command_ptr_ptr);
436
437 /* Free the rx resources */
438 kfree(msm_uport->rx.command_ptr);
439 kfree(msm_uport->rx.command_ptr_ptr);
440
441 iounmap(msm_uport->uport.membase);
442
443 return 0;
444}
445
446static int msm_hs_init_clk_locked(struct uart_port *uport)
447{
448 int ret;
449 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
450
451 ret = clk_enable(msm_uport->clk);
452 if (ret) {
453 printk(KERN_ERR "Error could not turn on UART clk\n");
454 return ret;
455 }
456
457 /* Set up the MREG/NREG/DREG/MNDREG */
458 ret = clk_set_rate(msm_uport->clk, uport->uartclk);
459 if (ret) {
460 printk(KERN_WARNING "Error setting clock rate on UART\n");
461 clk_disable(msm_uport->clk);
462 return ret;
463 }
464
465 msm_uport->clk_state = MSM_HS_CLK_ON;
466 return 0;
467}
468
469/* Enable and Disable clocks (Used for power management) */
470static void msm_hs_pm(struct uart_port *uport, unsigned int state,
471 unsigned int oldstate)
472{
473 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
474
475 if (use_low_power_rx_wakeup(msm_uport) ||
476 msm_uport->exit_lpm_cb)
477 return; /* ignore linux PM states,
478 use msm_hs_request_clock API */
479
480 switch (state) {
481 case 0:
482 clk_enable(msm_uport->clk);
483 break;
484 case 3:
485 clk_disable(msm_uport->clk);
486 break;
487 default:
488 dev_err(uport->dev, "msm_serial: Unknown PM state %d\n",
489 state);
490 }
491}
492
493/*
494 * programs the UARTDM_CSR register with correct bit rates
495 *
496 * Interrupts should be disabled before we are called, as
497 * we modify Set Baud rate
498 * Set receive stale interrupt level, dependent on Bit Rate
499 * Goal is to have around 8 ms before indicate stale.
500 * roundup (((Bit Rate * .008) / 10) + 1
501 */
502static void msm_hs_set_bps_locked(struct uart_port *uport,
503 unsigned int bps)
504{
505 unsigned long rxstale;
506 unsigned long data;
507 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
508
509 switch (bps) {
510 case 300:
511 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_75);
512 rxstale = 1;
513 break;
514 case 600:
515 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_150);
516 rxstale = 1;
517 break;
518 case 1200:
519 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_300);
520 rxstale = 1;
521 break;
522 case 2400:
523 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_600);
524 rxstale = 1;
525 break;
526 case 4800:
527 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_1200);
528 rxstale = 1;
529 break;
530 case 9600:
531 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
532 rxstale = 2;
533 break;
534 case 14400:
535 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_3600);
536 rxstale = 3;
537 break;
538 case 19200:
539 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_4800);
540 rxstale = 4;
541 break;
542 case 28800:
543 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_7200);
544 rxstale = 6;
545 break;
546 case 38400:
547 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_9600);
548 rxstale = 8;
549 break;
550 case 57600:
551 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_14400);
552 rxstale = 16;
553 break;
554 case 76800:
555 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_19200);
556 rxstale = 16;
557 break;
558 case 115200:
559 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_28800);
560 rxstale = 31;
561 break;
562 case 230400:
563 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_57600);
564 rxstale = 31;
565 break;
566 case 460800:
567 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
568 rxstale = 31;
569 break;
570 case 4000000:
571 case 3686400:
572 case 3200000:
573 case 3500000:
574 case 3000000:
575 case 2500000:
576 case 1500000:
577 case 1152000:
578 case 1000000:
579 case 921600:
580 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
581 rxstale = 31;
582 break;
583 default:
584 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
585 /* default to 9600 */
586 bps = 9600;
587 rxstale = 2;
588 break;
589 }
590 if (bps > 460800)
591 uport->uartclk = bps * 16;
592 else
593 uport->uartclk = UARTCLK;
594
595 if (clk_set_rate(msm_uport->clk, uport->uartclk)) {
596 printk(KERN_WARNING "Error setting clock rate on UART\n");
597 return;
598 }
599
600 data = rxstale & UARTDM_IPR_STALE_LSB_BMSK;
601 data |= UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK & (rxstale << 2);
602
603 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
604}
605
606/*
607 * termios : new ktermios
608 * oldtermios: old ktermios previous setting
609 *
610 * Configure the serial port
611 */
612static void msm_hs_set_termios(struct uart_port *uport,
613 struct ktermios *termios,
614 struct ktermios *oldtermios)
615{
616 unsigned int bps;
617 unsigned long data;
618 unsigned long flags;
619 unsigned int c_cflag = termios->c_cflag;
620 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
621
622 spin_lock_irqsave(&uport->lock, flags);
623 clk_enable(msm_uport->clk);
624
625 /* 300 is the minimum baud support by the driver */
626 bps = uart_get_baud_rate(uport, termios, oldtermios, 200, 4000000);
627
628 /* Temporary remapping 200 BAUD to 3.2 mbps */
629 if (bps == 200)
630 bps = 3200000;
631
632 msm_hs_set_bps_locked(uport, bps);
633
634 data = msm_hs_read(uport, UARTDM_MR2_ADDR);
635 data &= ~UARTDM_MR2_PARITY_MODE_BMSK;
636 /* set parity */
637 if (PARENB == (c_cflag & PARENB)) {
638 if (PARODD == (c_cflag & PARODD))
639 data |= ODD_PARITY;
640 else if (CMSPAR == (c_cflag & CMSPAR))
641 data |= SPACE_PARITY;
642 else
643 data |= EVEN_PARITY;
644 }
645
646 /* Set bits per char */
647 data &= ~UARTDM_MR2_BITS_PER_CHAR_BMSK;
648
649 switch (c_cflag & CSIZE) {
650 case CS5:
651 data |= FIVE_BPC;
652 break;
653 case CS6:
654 data |= SIX_BPC;
655 break;
656 case CS7:
657 data |= SEVEN_BPC;
658 break;
659 default:
660 data |= EIGHT_BPC;
661 break;
662 }
663 /* stop bits */
664 if (c_cflag & CSTOPB) {
665 data |= STOP_BIT_TWO;
666 } else {
667 /* otherwise 1 stop bit */
668 data |= STOP_BIT_ONE;
669 }
670 data |= UARTDM_MR2_ERROR_MODE_BMSK;
671 /* write parity/bits per char/stop bit configuration */
672 msm_hs_write(uport, UARTDM_MR2_ADDR, data);
673
674 /* Configure HW flow control */
675 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
676
677 data &= ~(UARTDM_MR1_CTS_CTL_BMSK | UARTDM_MR1_RX_RDY_CTL_BMSK);
678
679 if (c_cflag & CRTSCTS) {
680 data |= UARTDM_MR1_CTS_CTL_BMSK;
681 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
682 }
683
684 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
685
686 uport->ignore_status_mask = termios->c_iflag & INPCK;
687 uport->ignore_status_mask |= termios->c_iflag & IGNPAR;
688 uport->read_status_mask = (termios->c_cflag & CREAD);
689
690 msm_hs_write(uport, UARTDM_IMR_ADDR, 0);
691
692 /* Set Transmit software time out */
693 uart_update_timeout(uport, c_cflag, bps);
694
695 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
696 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
697
698 if (msm_uport->rx.flush == FLUSH_NONE) {
699 msm_uport->rx.flush = FLUSH_IGNORE;
700 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
701 }
702
703 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
704
705 clk_disable(msm_uport->clk);
706 spin_unlock_irqrestore(&uport->lock, flags);
707}
708
709/*
710 * Standard API, Transmitter
711 * Any character in the transmit shift register is sent
712 */
713static unsigned int msm_hs_tx_empty(struct uart_port *uport)
714{
715 unsigned int data;
716 unsigned int ret = 0;
717 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
718
719 clk_enable(msm_uport->clk);
720
721 data = msm_hs_read(uport, UARTDM_SR_ADDR);
722 if (data & UARTDM_SR_TXEMT_BMSK)
723 ret = TIOCSER_TEMT;
724
725 clk_disable(msm_uport->clk);
726
727 return ret;
728}
729
730/*
731 * Standard API, Stop transmitter.
732 * Any character in the transmit shift register is sent as
733 * well as the current data mover transfer .
734 */
735static void msm_hs_stop_tx_locked(struct uart_port *uport)
736{
737 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
738
739 msm_uport->tx.tx_ready_int_en = 0;
740}
741
742/*
743 * Standard API, Stop receiver as soon as possible.
744 *
745 * Function immediately terminates the operation of the
746 * channel receiver and any incoming characters are lost. None
747 * of the receiver status bits are affected by this command and
748 * characters that are already in the receive FIFO there.
749 */
750static void msm_hs_stop_rx_locked(struct uart_port *uport)
751{
752 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
753 unsigned int data;
754
755 clk_enable(msm_uport->clk);
756
757 /* disable dlink */
758 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
759 data &= ~UARTDM_RX_DM_EN_BMSK;
760 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
761
762 /* Disable the receiver */
763 if (msm_uport->rx.flush == FLUSH_NONE)
764 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
765
766 if (msm_uport->rx.flush != FLUSH_SHUTDOWN)
767 msm_uport->rx.flush = FLUSH_STOP;
768
769 clk_disable(msm_uport->clk);
770}
771
772/* Transmit the next chunk of data */
773static void msm_hs_submit_tx_locked(struct uart_port *uport)
774{
775 int left;
776 int tx_count;
777 dma_addr_t src_addr;
778 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
779 struct msm_hs_tx *tx = &msm_uport->tx;
780 struct circ_buf *tx_buf = &msm_uport->uport.state->xmit;
781
782 if (uart_circ_empty(tx_buf) || uport->state->port.tty->stopped) {
783 msm_hs_stop_tx_locked(uport);
784 return;
785 }
786
787 tx->dma_in_flight = 1;
788
789 tx_count = uart_circ_chars_pending(tx_buf);
790
791 if (UARTDM_TX_BUF_SIZE < tx_count)
792 tx_count = UARTDM_TX_BUF_SIZE;
793
794 left = UART_XMIT_SIZE - tx_buf->tail;
795
796 if (tx_count > left)
797 tx_count = left;
798
799 src_addr = tx->dma_base + tx_buf->tail;
800 dma_sync_single_for_device(uport->dev, src_addr, tx_count,
801 DMA_TO_DEVICE);
802
803 tx->command_ptr->num_rows = (((tx_count + 15) >> 4) << 16) |
804 ((tx_count + 15) >> 4);
805 tx->command_ptr->src_row_addr = src_addr;
806
807 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr,
808 sizeof(dmov_box), DMA_TO_DEVICE);
809
810 *tx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(tx->mapped_cmd_ptr);
811
812 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr_ptr,
813 sizeof(u32 *), DMA_TO_DEVICE);
814
815 /* Save tx_count to use in Callback */
816 tx->tx_count = tx_count;
817 msm_hs_write(uport, UARTDM_NCF_TX_ADDR, tx_count);
818
819 /* Disable the tx_ready interrupt */
820 msm_uport->imr_reg &= ~UARTDM_ISR_TX_READY_BMSK;
821 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
822 msm_dmov_enqueue_cmd(msm_uport->dma_tx_channel, &tx->xfer);
823}
824
825/* Start to receive the next chunk of data */
826static void msm_hs_start_rx_locked(struct uart_port *uport)
827{
828 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
829
830 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
831 msm_hs_write(uport, UARTDM_DMRX_ADDR, UARTDM_RX_BUF_SIZE);
832 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_ENABLE);
833 msm_uport->imr_reg |= UARTDM_ISR_RXLEV_BMSK;
834 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
835
836 msm_uport->rx.flush = FLUSH_NONE;
837 msm_dmov_enqueue_cmd(msm_uport->dma_rx_channel, &msm_uport->rx.xfer);
838
839 /* might have finished RX and be ready to clock off */
840 hrtimer_start(&msm_uport->clk_off_timer, msm_uport->clk_off_delay,
841 HRTIMER_MODE_REL);
842}
843
844/* Enable the transmitter Interrupt */
845static void msm_hs_start_tx_locked(struct uart_port *uport)
846{
847 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
848
849 clk_enable(msm_uport->clk);
850
851 if (msm_uport->exit_lpm_cb)
852 msm_uport->exit_lpm_cb(uport);
853
854 if (msm_uport->tx.tx_ready_int_en == 0) {
855 msm_uport->tx.tx_ready_int_en = 1;
856 msm_hs_submit_tx_locked(uport);
857 }
858
859 clk_disable(msm_uport->clk);
860}
861
862/*
863 * This routine is called when we are done with a DMA transfer
864 *
865 * This routine is registered with Data mover when we set
866 * up a Data Mover transfer. It is called from Data mover ISR
867 * when the DMA transfer is done.
868 */
869static void msm_hs_dmov_tx_callback(struct msm_dmov_cmd *cmd_ptr,
870 unsigned int result,
871 struct msm_dmov_errdata *err)
872{
873 unsigned long flags;
874 struct msm_hs_port *msm_uport;
875
876 /* DMA did not finish properly */
877 WARN_ON((((result & RSLT_FIFO_CNTR_BMSK) >> 28) == 1) &&
878 !(result & RSLT_VLD));
879
880 msm_uport = container_of(cmd_ptr, struct msm_hs_port, tx.xfer);
881
882 spin_lock_irqsave(&msm_uport->uport.lock, flags);
883 clk_enable(msm_uport->clk);
884
885 msm_uport->imr_reg |= UARTDM_ISR_TX_READY_BMSK;
886 msm_hs_write(&msm_uport->uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
887
888 clk_disable(msm_uport->clk);
889 spin_unlock_irqrestore(&msm_uport->uport.lock, flags);
890}
891
892/*
893 * This routine is called when we are done with a DMA transfer or the
894 * a flush has been sent to the data mover driver.
895 *
896 * This routine is registered with Data mover when we set up a Data Mover
897 * transfer. It is called from Data mover ISR when the DMA transfer is done.
898 */
899static void msm_hs_dmov_rx_callback(struct msm_dmov_cmd *cmd_ptr,
900 unsigned int result,
901 struct msm_dmov_errdata *err)
902{
903 int retval;
904 int rx_count;
905 unsigned long status;
906 unsigned int error_f = 0;
907 unsigned long flags;
908 unsigned int flush;
909 struct tty_struct *tty;
910 struct uart_port *uport;
911 struct msm_hs_port *msm_uport;
912
913 msm_uport = container_of(cmd_ptr, struct msm_hs_port, rx.xfer);
914 uport = &msm_uport->uport;
915
916 spin_lock_irqsave(&uport->lock, flags);
917 clk_enable(msm_uport->clk);
918
919 tty = uport->state->port.tty;
920
921 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
922
923 status = msm_hs_read(uport, UARTDM_SR_ADDR);
924
925 /* overflow is not connect to data in a FIFO */
926 if (unlikely((status & UARTDM_SR_OVERRUN_BMSK) &&
927 (uport->read_status_mask & CREAD))) {
928 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
929 uport->icount.buf_overrun++;
930 error_f = 1;
931 }
932
933 if (!(uport->ignore_status_mask & INPCK))
934 status = status & ~(UARTDM_SR_PAR_FRAME_BMSK);
935
936 if (unlikely(status & UARTDM_SR_PAR_FRAME_BMSK)) {
937 /* Can not tell difference between parity & frame error */
938 uport->icount.parity++;
939 error_f = 1;
940 if (uport->ignore_status_mask & IGNPAR)
941 tty_insert_flip_char(tty, 0, TTY_PARITY);
942 }
943
944 if (error_f)
945 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
946
947 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_FLUSH_ISSUED)
948 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_FLUSHED;
949
950 flush = msm_uport->rx.flush;
951 if (flush == FLUSH_IGNORE)
952 msm_hs_start_rx_locked(uport);
953 if (flush == FLUSH_STOP)
954 msm_uport->rx.flush = FLUSH_SHUTDOWN;
955 if (flush >= FLUSH_DATA_INVALID)
956 goto out;
957
958 rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);
959
960 if (0 != (uport->read_status_mask & CREAD)) {
961 retval = tty_insert_flip_string(tty, msm_uport->rx.buffer,
962 rx_count);
963 BUG_ON(retval != rx_count);
964 }
965
966 msm_hs_start_rx_locked(uport);
967
968out:
969 clk_disable(msm_uport->clk);
970
971 spin_unlock_irqrestore(&uport->lock, flags);
972
973 if (flush < FLUSH_DATA_INVALID)
974 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
975}
976
977static void msm_hs_tty_flip_buffer_work(struct work_struct *work)
978{
979 struct msm_hs_port *msm_uport =
980 container_of(work, struct msm_hs_port, rx.tty_work);
981 struct tty_struct *tty = msm_uport->uport.state->port.tty;
982
983 tty_flip_buffer_push(tty);
984}
985
986/*
987 * Standard API, Current states of modem control inputs
988 *
989 * Since CTS can be handled entirely by HARDWARE we always
990 * indicate clear to send and count on the TX FIFO to block when
991 * it fills up.
992 *
993 * - TIOCM_DCD
994 * - TIOCM_CTS
995 * - TIOCM_DSR
996 * - TIOCM_RI
997 * (Unsupported) DCD and DSR will return them high. RI will return low.
998 */
999static unsigned int msm_hs_get_mctrl_locked(struct uart_port *uport)
1000{
1001 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
1002}
1003
1004/*
1005 * True enables UART auto RFR, which indicates we are ready for data if the RX
1006 * buffer is not full. False disables auto RFR, and deasserts RFR to indicate
1007 * we are not ready for data. Must be called with UART clock on.
1008 */
1009static void set_rfr_locked(struct uart_port *uport, int auto_rfr)
1010{
1011 unsigned int data;
1012
1013 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1014
1015 if (auto_rfr) {
1016 /* enable auto ready-for-receiving */
1017 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
1018 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1019 } else {
1020 /* disable auto ready-for-receiving */
1021 data &= ~UARTDM_MR1_RX_RDY_CTL_BMSK;
1022 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1023 /* RFR is active low, set high */
1024 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_HIGH);
1025 }
1026}
1027
1028/*
1029 * Standard API, used to set or clear RFR
1030 */
1031static void msm_hs_set_mctrl_locked(struct uart_port *uport,
1032 unsigned int mctrl)
1033{
1034 unsigned int auto_rfr;
1035 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1036
1037 clk_enable(msm_uport->clk);
1038
1039 auto_rfr = TIOCM_RTS & mctrl ? 1 : 0;
1040 set_rfr_locked(uport, auto_rfr);
1041
1042 clk_disable(msm_uport->clk);
1043}
1044
1045/* Standard API, Enable modem status (CTS) interrupt */
1046static void msm_hs_enable_ms_locked(struct uart_port *uport)
1047{
1048 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1049
1050 clk_enable(msm_uport->clk);
1051
1052 /* Enable DELTA_CTS Interrupt */
1053 msm_uport->imr_reg |= UARTDM_ISR_DELTA_CTS_BMSK;
1054 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1055
1056 clk_disable(msm_uport->clk);
1057
1058}
1059
1060/*
1061 * Standard API, Break Signal
1062 *
1063 * Control the transmission of a break signal. ctl eq 0 => break
1064 * signal terminate ctl ne 0 => start break signal
1065 */
1066static void msm_hs_break_ctl(struct uart_port *uport, int ctl)
1067{
1068 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1069
1070 clk_enable(msm_uport->clk);
1071 msm_hs_write(uport, UARTDM_CR_ADDR, ctl ? START_BREAK : STOP_BREAK);
1072 clk_disable(msm_uport->clk);
1073}
1074
1075static void msm_hs_config_port(struct uart_port *uport, int cfg_flags)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&uport->lock, flags);
1080 if (cfg_flags & UART_CONFIG_TYPE) {
1081 uport->type = PORT_MSM;
1082 msm_hs_request_port(uport);
1083 }
1084 spin_unlock_irqrestore(&uport->lock, flags);
1085}
1086
1087/* Handle CTS changes (Called from interrupt handler) */
1088static void msm_hs_handle_delta_cts(struct uart_port *uport)
1089{
1090 unsigned long flags;
1091 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1092
1093 spin_lock_irqsave(&uport->lock, flags);
1094 clk_enable(msm_uport->clk);
1095
1096 /* clear interrupt */
1097 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1098 uport->icount.cts++;
1099
1100 clk_disable(msm_uport->clk);
1101 spin_unlock_irqrestore(&uport->lock, flags);
1102
1103 /* clear the IOCTL TIOCMIWAIT if called */
1104 wake_up_interruptible(&uport->state->port.delta_msr_wait);
1105}
1106
1107/* check if the TX path is flushed, and if so clock off
1108 * returns 0 did not clock off, need to retry (still sending final byte)
1109 * -1 did not clock off, do not retry
1110 * 1 if we clocked off
1111 */
1112static int msm_hs_check_clock_off_locked(struct uart_port *uport)
1113{
1114 unsigned long sr_status;
1115 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1116 struct circ_buf *tx_buf = &uport->state->xmit;
1117
1118 /* Cancel if tx tty buffer is not empty, dma is in flight,
1119 * or tx fifo is not empty, or rx fifo is not empty */
1120 if (msm_uport->clk_state != MSM_HS_CLK_REQUEST_OFF ||
1121 !uart_circ_empty(tx_buf) || msm_uport->tx.dma_in_flight ||
1122 (msm_uport->imr_reg & UARTDM_ISR_TXLEV_BMSK) ||
1123 !(msm_uport->imr_reg & UARTDM_ISR_RXLEV_BMSK)) {
1124 return -1;
1125 }
1126
1127 /* Make sure the uart is finished with the last byte */
1128 sr_status = msm_hs_read(uport, UARTDM_SR_ADDR);
1129 if (!(sr_status & UARTDM_SR_TXEMT_BMSK))
1130 return 0; /* retry */
1131
1132 /* Make sure forced RXSTALE flush complete */
1133 switch (msm_uport->clk_req_off_state) {
1134 case CLK_REQ_OFF_START:
1135 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_ISSUED;
1136 msm_hs_write(uport, UARTDM_CR_ADDR, FORCE_STALE_EVENT);
1137 return 0; /* RXSTALE flush not complete - retry */
1138 case CLK_REQ_OFF_RXSTALE_ISSUED:
1139 case CLK_REQ_OFF_FLUSH_ISSUED:
1140 return 0; /* RXSTALE flush not complete - retry */
1141 case CLK_REQ_OFF_RXSTALE_FLUSHED:
1142 break; /* continue */
1143 }
1144
1145 if (msm_uport->rx.flush != FLUSH_SHUTDOWN) {
1146 if (msm_uport->rx.flush == FLUSH_NONE)
1147 msm_hs_stop_rx_locked(uport);
1148 return 0; /* come back later to really clock off */
1149 }
1150
1151 /* we really want to clock off */
1152 clk_disable(msm_uport->clk);
1153 msm_uport->clk_state = MSM_HS_CLK_OFF;
1154
1155 if (use_low_power_rx_wakeup(msm_uport)) {
1156 msm_uport->rx_wakeup.ignore = 1;
1157 enable_irq(msm_uport->rx_wakeup.irq);
1158 }
1159 return 1;
1160}
1161
1162static enum hrtimer_restart msm_hs_clk_off_retry(struct hrtimer *timer)
1163{
1164 unsigned long flags;
1165 int ret = HRTIMER_NORESTART;
1166 struct msm_hs_port *msm_uport = container_of(timer, struct msm_hs_port,
1167 clk_off_timer);
1168 struct uart_port *uport = &msm_uport->uport;
1169
1170 spin_lock_irqsave(&uport->lock, flags);
1171
1172 if (!msm_hs_check_clock_off_locked(uport)) {
1173 hrtimer_forward_now(timer, msm_uport->clk_off_delay);
1174 ret = HRTIMER_RESTART;
1175 }
1176
1177 spin_unlock_irqrestore(&uport->lock, flags);
1178
1179 return ret;
1180}
1181
1182static irqreturn_t msm_hs_isr(int irq, void *dev)
1183{
1184 unsigned long flags;
1185 unsigned long isr_status;
1186 struct msm_hs_port *msm_uport = dev;
1187 struct uart_port *uport = &msm_uport->uport;
1188 struct circ_buf *tx_buf = &uport->state->xmit;
1189 struct msm_hs_tx *tx = &msm_uport->tx;
1190 struct msm_hs_rx *rx = &msm_uport->rx;
1191
1192 spin_lock_irqsave(&uport->lock, flags);
1193
1194 isr_status = msm_hs_read(uport, UARTDM_MISR_ADDR);
1195
1196 /* Uart RX starting */
1197 if (isr_status & UARTDM_ISR_RXLEV_BMSK) {
1198 msm_uport->imr_reg &= ~UARTDM_ISR_RXLEV_BMSK;
1199 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1200 }
1201 /* Stale rx interrupt */
1202 if (isr_status & UARTDM_ISR_RXSTALE_BMSK) {
1203 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
1204 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1205
1206 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_RXSTALE_ISSUED)
1207 msm_uport->clk_req_off_state =
1208 CLK_REQ_OFF_FLUSH_ISSUED;
1209 if (rx->flush == FLUSH_NONE) {
1210 rx->flush = FLUSH_DATA_READY;
1211 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
1212 }
1213 }
1214 /* tx ready interrupt */
1215 if (isr_status & UARTDM_ISR_TX_READY_BMSK) {
1216 /* Clear TX Ready */
1217 msm_hs_write(uport, UARTDM_CR_ADDR, CLEAR_TX_READY);
1218
1219 if (msm_uport->clk_state == MSM_HS_CLK_REQUEST_OFF) {
1220 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1221 msm_hs_write(uport, UARTDM_IMR_ADDR,
1222 msm_uport->imr_reg);
1223 }
1224
1225 /* Complete DMA TX transactions and submit new transactions */
1226 tx_buf->tail = (tx_buf->tail + tx->tx_count) & ~UART_XMIT_SIZE;
1227
1228 tx->dma_in_flight = 0;
1229
1230 uport->icount.tx += tx->tx_count;
1231 if (tx->tx_ready_int_en)
1232 msm_hs_submit_tx_locked(uport);
1233
1234 if (uart_circ_chars_pending(tx_buf) < WAKEUP_CHARS)
1235 uart_write_wakeup(uport);
1236 }
1237 if (isr_status & UARTDM_ISR_TXLEV_BMSK) {
1238 /* TX FIFO is empty */
1239 msm_uport->imr_reg &= ~UARTDM_ISR_TXLEV_BMSK;
1240 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1241 if (!msm_hs_check_clock_off_locked(uport))
1242 hrtimer_start(&msm_uport->clk_off_timer,
1243 msm_uport->clk_off_delay,
1244 HRTIMER_MODE_REL);
1245 }
1246
1247 /* Change in CTS interrupt */
1248 if (isr_status & UARTDM_ISR_DELTA_CTS_BMSK)
1249 msm_hs_handle_delta_cts(uport);
1250
1251 spin_unlock_irqrestore(&uport->lock, flags);
1252
1253 return IRQ_HANDLED;
1254}
1255
1256void msm_hs_request_clock_off_locked(struct uart_port *uport)
1257{
1258 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1259
1260 if (msm_uport->clk_state == MSM_HS_CLK_ON) {
1261 msm_uport->clk_state = MSM_HS_CLK_REQUEST_OFF;
1262 msm_uport->clk_req_off_state = CLK_REQ_OFF_START;
1263 if (!use_low_power_rx_wakeup(msm_uport))
1264 set_rfr_locked(uport, 0);
1265 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1266 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1267 }
1268}
1269
1270/**
1271 * msm_hs_request_clock_off - request to (i.e. asynchronously) turn off uart
1272 * clock once pending TX is flushed and Rx DMA command is terminated.
1273 * @uport: uart_port structure for the device instance.
1274 *
1275 * This functions puts the device into a partially active low power mode. It
1276 * waits to complete all pending tx transactions, flushes ongoing Rx DMA
1277 * command and terminates UART side Rx transaction, puts UART HW in non DMA
1278 * mode and then clocks off the device. A client calls this when no UART
1279 * data is expected. msm_request_clock_on() must be called before any further
1280 * UART can be sent or received.
1281 */
1282void msm_hs_request_clock_off(struct uart_port *uport)
1283{
1284 unsigned long flags;
1285
1286 spin_lock_irqsave(&uport->lock, flags);
1287 msm_hs_request_clock_off_locked(uport);
1288 spin_unlock_irqrestore(&uport->lock, flags);
1289}
1290
1291void msm_hs_request_clock_on_locked(struct uart_port *uport)
1292{
1293 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1294 unsigned int data;
1295
1296 switch (msm_uport->clk_state) {
1297 case MSM_HS_CLK_OFF:
1298 clk_enable(msm_uport->clk);
1299 disable_irq_nosync(msm_uport->rx_wakeup.irq);
1300 /* fall-through */
1301 case MSM_HS_CLK_REQUEST_OFF:
1302 if (msm_uport->rx.flush == FLUSH_STOP ||
1303 msm_uport->rx.flush == FLUSH_SHUTDOWN) {
1304 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1305 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
1306 data |= UARTDM_RX_DM_EN_BMSK;
1307 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1308 }
1309 hrtimer_try_to_cancel(&msm_uport->clk_off_timer);
1310 if (msm_uport->rx.flush == FLUSH_SHUTDOWN)
1311 msm_hs_start_rx_locked(uport);
1312 if (!use_low_power_rx_wakeup(msm_uport))
1313 set_rfr_locked(uport, 1);
1314 if (msm_uport->rx.flush == FLUSH_STOP)
1315 msm_uport->rx.flush = FLUSH_IGNORE;
1316 msm_uport->clk_state = MSM_HS_CLK_ON;
1317 break;
1318 case MSM_HS_CLK_ON:
1319 break;
1320 case MSM_HS_CLK_PORT_OFF:
1321 break;
1322 }
1323}
1324
1325/**
1326 * msm_hs_request_clock_on - Switch the device from partially active low
1327 * power mode to fully active (i.e. clock on) mode.
1328 * @uport: uart_port structure for the device.
1329 *
1330 * This function switches on the input clock, puts UART HW into DMA mode
1331 * and enqueues an Rx DMA command if the device was in partially active
1332 * mode. It has no effect if called with the device in inactive state.
1333 */
1334void msm_hs_request_clock_on(struct uart_port *uport)
1335{
1336 unsigned long flags;
1337
1338 spin_lock_irqsave(&uport->lock, flags);
1339 msm_hs_request_clock_on_locked(uport);
1340 spin_unlock_irqrestore(&uport->lock, flags);
1341}
1342
1343static irqreturn_t msm_hs_rx_wakeup_isr(int irq, void *dev)
1344{
1345 unsigned int wakeup = 0;
1346 unsigned long flags;
1347 struct msm_hs_port *msm_uport = dev;
1348 struct uart_port *uport = &msm_uport->uport;
1349 struct tty_struct *tty = NULL;
1350
1351 spin_lock_irqsave(&uport->lock, flags);
1352 if (msm_uport->clk_state == MSM_HS_CLK_OFF) {
1353 /* ignore the first irq - it is a pending irq that occurred
1354 * before enable_irq() */
1355 if (msm_uport->rx_wakeup.ignore)
1356 msm_uport->rx_wakeup.ignore = 0;
1357 else
1358 wakeup = 1;
1359 }
1360
1361 if (wakeup) {
1362 /* the uart was clocked off during an rx, wake up and
1363 * optionally inject char into tty rx */
1364 msm_hs_request_clock_on_locked(uport);
1365 if (msm_uport->rx_wakeup.inject_rx) {
1366 tty = uport->state->port.tty;
1367 tty_insert_flip_char(tty,
1368 msm_uport->rx_wakeup.rx_to_inject,
1369 TTY_NORMAL);
1370 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
1371 }
1372 }
1373
1374 spin_unlock_irqrestore(&uport->lock, flags);
1375
1376 return IRQ_HANDLED;
1377}
1378
1379static const char *msm_hs_type(struct uart_port *port)
1380{
1381 return (port->type == PORT_MSM) ? "MSM_HS_UART" : NULL;
1382}
1383
1384/* Called when port is opened */
1385static int msm_hs_startup(struct uart_port *uport)
1386{
1387 int ret;
1388 int rfr_level;
1389 unsigned long flags;
1390 unsigned int data;
1391 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1392 struct circ_buf *tx_buf = &uport->state->xmit;
1393 struct msm_hs_tx *tx = &msm_uport->tx;
1394 struct msm_hs_rx *rx = &msm_uport->rx;
1395
1396 rfr_level = uport->fifosize;
1397 if (rfr_level > 16)
1398 rfr_level -= 16;
1399
1400 tx->dma_base = dma_map_single(uport->dev, tx_buf->buf, UART_XMIT_SIZE,
1401 DMA_TO_DEVICE);
1402
1403 /* do not let tty layer execute RX in global workqueue, use a
1404 * dedicated workqueue managed by this driver */
1405 uport->state->port.tty->low_latency = 1;
1406
1407 /* turn on uart clk */
1408 ret = msm_hs_init_clk_locked(uport);
1409 if (unlikely(ret)) {
1410 printk(KERN_ERR "Turning uartclk failed!\n");
1411 goto err_msm_hs_init_clk;
1412 }
1413
1414 /* Set auto RFR Level */
1415 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1416 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK;
1417 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK;
1418 data |= (UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK & (rfr_level << 2));
1419 data |= (UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK & rfr_level);
1420 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1421
1422 /* Make sure RXSTALE count is non-zero */
1423 data = msm_hs_read(uport, UARTDM_IPR_ADDR);
1424 if (!data) {
1425 data |= 0x1f & UARTDM_IPR_STALE_LSB_BMSK;
1426 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
1427 }
1428
1429 /* Enable Data Mover Mode */
1430 data = UARTDM_TX_DM_EN_BMSK | UARTDM_RX_DM_EN_BMSK;
1431 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1432
1433 /* Reset TX */
1434 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
1435 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1436 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
1437 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_BREAK_INT);
1438 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1439 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1440 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_LOW);
1441 /* Turn on Uart Receiver */
1442 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_EN_BMSK);
1443
1444 /* Turn on Uart Transmitter */
1445 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_EN_BMSK);
1446
1447 /* Initialize the tx */
1448 tx->tx_ready_int_en = 0;
1449 tx->dma_in_flight = 0;
1450
1451 tx->xfer.complete_func = msm_hs_dmov_tx_callback;
1452 tx->xfer.execute_func = NULL;
1453
1454 tx->command_ptr->cmd = CMD_LC |
1455 CMD_DST_CRCI(msm_uport->dma_tx_crci) | CMD_MODE_BOX;
1456
1457 tx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1458 | (MSM_UARTDM_BURST_SIZE);
1459
1460 tx->command_ptr->row_offset = (MSM_UARTDM_BURST_SIZE << 16);
1461
1462 tx->command_ptr->dst_row_addr =
1463 msm_uport->uport.mapbase + UARTDM_TF_ADDR;
1464
1465
1466 /* Turn on Uart Receive */
1467 rx->xfer.complete_func = msm_hs_dmov_rx_callback;
1468 rx->xfer.execute_func = NULL;
1469
1470 rx->command_ptr->cmd = CMD_LC |
1471 CMD_SRC_CRCI(msm_uport->dma_rx_crci) | CMD_MODE_BOX;
1472
1473 rx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1474 | (MSM_UARTDM_BURST_SIZE);
1475 rx->command_ptr->row_offset = MSM_UARTDM_BURST_SIZE;
1476 rx->command_ptr->src_row_addr = uport->mapbase + UARTDM_RF_ADDR;
1477
1478
1479 msm_uport->imr_reg |= UARTDM_ISR_RXSTALE_BMSK;
1480 /* Enable reading the current CTS, no harm even if CTS is ignored */
1481 msm_uport->imr_reg |= UARTDM_ISR_CURRENT_CTS_BMSK;
1482
1483 msm_hs_write(uport, UARTDM_TFWR_ADDR, 0); /* TXLEV on empty TX fifo */
1484
1485
1486 ret = request_irq(uport->irq, msm_hs_isr, IRQF_TRIGGER_HIGH,
1487 "msm_hs_uart", msm_uport);
1488 if (unlikely(ret)) {
1489 printk(KERN_ERR "Request msm_hs_uart IRQ failed!\n");
1490 goto err_request_irq;
1491 }
1492 if (use_low_power_rx_wakeup(msm_uport)) {
1493 ret = request_irq(msm_uport->rx_wakeup.irq,
1494 msm_hs_rx_wakeup_isr,
1495 IRQF_TRIGGER_FALLING,
1496 "msm_hs_rx_wakeup", msm_uport);
1497 if (unlikely(ret)) {
1498 printk(KERN_ERR "Request msm_hs_rx_wakeup IRQ failed!\n");
1499 free_irq(uport->irq, msm_uport);
1500 goto err_request_irq;
1501 }
1502 disable_irq(msm_uport->rx_wakeup.irq);
1503 }
1504
1505 spin_lock_irqsave(&uport->lock, flags);
1506
1507 msm_hs_write(uport, UARTDM_RFWR_ADDR, 0);
1508 msm_hs_start_rx_locked(uport);
1509
1510 spin_unlock_irqrestore(&uport->lock, flags);
1511 ret = pm_runtime_set_active(uport->dev);
1512 if (ret)
1513 dev_err(uport->dev, "set active error:%d\n", ret);
1514 pm_runtime_enable(uport->dev);
1515
1516 return 0;
1517
1518err_request_irq:
1519err_msm_hs_init_clk:
1520 dma_unmap_single(uport->dev, tx->dma_base,
1521 UART_XMIT_SIZE, DMA_TO_DEVICE);
1522 return ret;
1523}
1524
1525/* Initialize tx and rx data structures */
1526static int __devinit uartdm_init_port(struct uart_port *uport)
1527{
1528 int ret = 0;
1529 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1530 struct msm_hs_tx *tx = &msm_uport->tx;
1531 struct msm_hs_rx *rx = &msm_uport->rx;
1532
1533 /* Allocate the command pointer. Needs to be 64 bit aligned */
1534 tx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1535 if (!tx->command_ptr)
1536 return -ENOMEM;
1537
1538 tx->command_ptr_ptr = kmalloc(sizeof(u32 *), GFP_KERNEL | __GFP_DMA);
1539 if (!tx->command_ptr_ptr) {
1540 ret = -ENOMEM;
1541 goto err_tx_command_ptr_ptr;
1542 }
1543
1544 tx->mapped_cmd_ptr = dma_map_single(uport->dev, tx->command_ptr,
1545 sizeof(dmov_box), DMA_TO_DEVICE);
1546 tx->mapped_cmd_ptr_ptr = dma_map_single(uport->dev,
1547 tx->command_ptr_ptr,
1548 sizeof(u32 *), DMA_TO_DEVICE);
1549 tx->xfer.cmdptr = DMOV_CMD_ADDR(tx->mapped_cmd_ptr_ptr);
1550
1551 init_waitqueue_head(&rx->wait);
1552
1553 rx->pool = dma_pool_create("rx_buffer_pool", uport->dev,
1554 UARTDM_RX_BUF_SIZE, 16, 0);
1555 if (!rx->pool) {
1556 pr_err("%s(): cannot allocate rx_buffer_pool", __func__);
1557 ret = -ENOMEM;
1558 goto err_dma_pool_create;
1559 }
1560
1561 rx->buffer = dma_pool_alloc(rx->pool, GFP_KERNEL, &rx->rbuffer);
1562 if (!rx->buffer) {
1563 pr_err("%s(): cannot allocate rx->buffer", __func__);
1564 ret = -ENOMEM;
1565 goto err_dma_pool_alloc;
1566 }
1567
1568 /* Allocate the command pointer. Needs to be 64 bit aligned */
1569 rx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1570 if (!rx->command_ptr) {
1571 pr_err("%s(): cannot allocate rx->command_ptr", __func__);
1572 ret = -ENOMEM;
1573 goto err_rx_command_ptr;
1574 }
1575
1576 rx->command_ptr_ptr = kmalloc(sizeof(u32 *), GFP_KERNEL | __GFP_DMA);
1577 if (!rx->command_ptr_ptr) {
1578 pr_err("%s(): cannot allocate rx->command_ptr_ptr", __func__);
1579 ret = -ENOMEM;
1580 goto err_rx_command_ptr_ptr;
1581 }
1582
1583 rx->command_ptr->num_rows = ((UARTDM_RX_BUF_SIZE >> 4) << 16) |
1584 (UARTDM_RX_BUF_SIZE >> 4);
1585
1586 rx->command_ptr->dst_row_addr = rx->rbuffer;
1587
1588 rx->mapped_cmd_ptr = dma_map_single(uport->dev, rx->command_ptr,
1589 sizeof(dmov_box), DMA_TO_DEVICE);
1590
1591 *rx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(rx->mapped_cmd_ptr);
1592
1593 rx->cmdptr_dmaaddr = dma_map_single(uport->dev, rx->command_ptr_ptr,
1594 sizeof(u32 *), DMA_TO_DEVICE);
1595 rx->xfer.cmdptr = DMOV_CMD_ADDR(rx->cmdptr_dmaaddr);
1596
1597 INIT_WORK(&rx->tty_work, msm_hs_tty_flip_buffer_work);
1598
1599 return ret;
1600
1601err_rx_command_ptr_ptr:
1602 kfree(rx->command_ptr);
1603err_rx_command_ptr:
1604 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
1605 msm_uport->rx.rbuffer);
1606err_dma_pool_alloc:
1607 dma_pool_destroy(msm_uport->rx.pool);
1608err_dma_pool_create:
1609 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr_ptr,
1610 sizeof(u32 *), DMA_TO_DEVICE);
1611 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr,
1612 sizeof(dmov_box), DMA_TO_DEVICE);
1613 kfree(msm_uport->tx.command_ptr_ptr);
1614err_tx_command_ptr_ptr:
1615 kfree(msm_uport->tx.command_ptr);
1616 return ret;
1617}
1618
1619static int __devinit msm_hs_probe(struct platform_device *pdev)
1620{
1621 int ret;
1622 struct uart_port *uport;
1623 struct msm_hs_port *msm_uport;
1624 struct resource *resource;
1625 const struct msm_serial_hs_platform_data *pdata =
1626 pdev->dev.platform_data;
1627
1628 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
1629 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
1630 return -EINVAL;
1631 }
1632
1633 msm_uport = &q_uart_port[pdev->id];
1634 uport = &msm_uport->uport;
1635
1636 uport->dev = &pdev->dev;
1637
1638 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1639 if (unlikely(!resource))
1640 return -ENXIO;
1641
1642 uport->mapbase = resource->start;
1643 uport->irq = platform_get_irq(pdev, 0);
1644 if (unlikely(uport->irq < 0))
1645 return -ENXIO;
1646
1647 if (unlikely(irq_set_irq_wake(uport->irq, 1)))
1648 return -ENXIO;
1649
1650 if (pdata == NULL || pdata->rx_wakeup_irq < 0)
1651 msm_uport->rx_wakeup.irq = -1;
1652 else {
1653 msm_uport->rx_wakeup.irq = pdata->rx_wakeup_irq;
1654 msm_uport->rx_wakeup.ignore = 1;
1655 msm_uport->rx_wakeup.inject_rx = pdata->inject_rx_on_wakeup;
1656 msm_uport->rx_wakeup.rx_to_inject = pdata->rx_to_inject;
1657
1658 if (unlikely(msm_uport->rx_wakeup.irq < 0))
1659 return -ENXIO;
1660
1661 if (unlikely(irq_set_irq_wake(msm_uport->rx_wakeup.irq, 1)))
1662 return -ENXIO;
1663 }
1664
1665 if (pdata == NULL)
1666 msm_uport->exit_lpm_cb = NULL;
1667 else
1668 msm_uport->exit_lpm_cb = pdata->exit_lpm_cb;
1669
1670 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1671 "uartdm_channels");
1672 if (unlikely(!resource))
1673 return -ENXIO;
1674
1675 msm_uport->dma_tx_channel = resource->start;
1676 msm_uport->dma_rx_channel = resource->end;
1677
1678 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1679 "uartdm_crci");
1680 if (unlikely(!resource))
1681 return -ENXIO;
1682
1683 msm_uport->dma_tx_crci = resource->start;
1684 msm_uport->dma_rx_crci = resource->end;
1685
1686 uport->iotype = UPIO_MEM;
1687 uport->fifosize = UART_FIFOSIZE;
1688 uport->ops = &msm_hs_ops;
1689 uport->flags = UPF_BOOT_AUTOCONF;
1690 uport->uartclk = UARTCLK;
1691 msm_uport->imr_reg = 0x0;
1692 msm_uport->clk = clk_get(&pdev->dev, "uartdm_clk");
1693 if (IS_ERR(msm_uport->clk))
1694 return PTR_ERR(msm_uport->clk);
1695
1696 ret = uartdm_init_port(uport);
1697 if (unlikely(ret))
1698 return ret;
1699
1700 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1701 hrtimer_init(&msm_uport->clk_off_timer, CLOCK_MONOTONIC,
1702 HRTIMER_MODE_REL);
1703 msm_uport->clk_off_timer.function = msm_hs_clk_off_retry;
1704 msm_uport->clk_off_delay = ktime_set(0, 1000000); /* 1ms */
1705
1706 uport->line = pdev->id;
1707 return uart_add_one_port(&msm_hs_driver, uport);
1708}
1709
1710static int __init msm_serial_hs_init(void)
1711{
1712 int ret, i;
1713
1714 /* Init all UARTS as non-configured */
1715 for (i = 0; i < UARTDM_NR; i++)
1716 q_uart_port[i].uport.type = PORT_UNKNOWN;
1717
1718 msm_hs_workqueue = create_singlethread_workqueue("msm_serial_hs");
1719 if (unlikely(!msm_hs_workqueue))
1720 return -ENOMEM;
1721
1722 ret = uart_register_driver(&msm_hs_driver);
1723 if (unlikely(ret)) {
1724 printk(KERN_ERR "%s failed to load\n", __func__);
1725 goto err_uart_register_driver;
1726 }
1727
1728 ret = platform_driver_register(&msm_serial_hs_platform_driver);
1729 if (ret) {
1730 printk(KERN_ERR "%s failed to load\n", __func__);
1731 goto err_platform_driver_register;
1732 }
1733
1734 return ret;
1735
1736err_platform_driver_register:
1737 uart_unregister_driver(&msm_hs_driver);
1738err_uart_register_driver:
1739 destroy_workqueue(msm_hs_workqueue);
1740 return ret;
1741}
1742module_init(msm_serial_hs_init);
1743
1744/*
1745 * Called by the upper layer when port is closed.
1746 * - Disables the port
1747 * - Unhook the ISR
1748 */
1749static void msm_hs_shutdown(struct uart_port *uport)
1750{
1751 unsigned long flags;
1752 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1753
1754 BUG_ON(msm_uport->rx.flush < FLUSH_STOP);
1755
1756 spin_lock_irqsave(&uport->lock, flags);
1757 clk_enable(msm_uport->clk);
1758
1759 /* Disable the transmitter */
1760 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_DISABLE_BMSK);
1761 /* Disable the receiver */
1762 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_DISABLE_BMSK);
1763
1764 pm_runtime_disable(uport->dev);
1765 pm_runtime_set_suspended(uport->dev);
1766
1767 /* Free the interrupt */
1768 free_irq(uport->irq, msm_uport);
1769 if (use_low_power_rx_wakeup(msm_uport))
1770 free_irq(msm_uport->rx_wakeup.irq, msm_uport);
1771
1772 msm_uport->imr_reg = 0;
1773 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1774
1775 wait_event(msm_uport->rx.wait, msm_uport->rx.flush == FLUSH_SHUTDOWN);
1776
1777 clk_disable(msm_uport->clk); /* to balance local clk_enable() */
1778 if (msm_uport->clk_state != MSM_HS_CLK_OFF)
1779 clk_disable(msm_uport->clk); /* to balance clk_state */
1780 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1781
1782 dma_unmap_single(uport->dev, msm_uport->tx.dma_base,
1783 UART_XMIT_SIZE, DMA_TO_DEVICE);
1784
1785 spin_unlock_irqrestore(&uport->lock, flags);
1786
1787 if (cancel_work_sync(&msm_uport->rx.tty_work))
1788 msm_hs_tty_flip_buffer_work(&msm_uport->rx.tty_work);
1789}
1790
1791static void __exit msm_serial_hs_exit(void)
1792{
1793 flush_workqueue(msm_hs_workqueue);
1794 destroy_workqueue(msm_hs_workqueue);
1795 platform_driver_unregister(&msm_serial_hs_platform_driver);
1796 uart_unregister_driver(&msm_hs_driver);
1797}
1798module_exit(msm_serial_hs_exit);
1799
1800#ifdef CONFIG_PM_RUNTIME
1801static int msm_hs_runtime_idle(struct device *dev)
1802{
1803 /*
1804 * returning success from idle results in runtime suspend to be
1805 * called
1806 */
1807 return 0;
1808}
1809
1810static int msm_hs_runtime_resume(struct device *dev)
1811{
1812 struct platform_device *pdev = container_of(dev, struct
1813 platform_device, dev);
1814 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1815
1816 msm_hs_request_clock_on(&msm_uport->uport);
1817 return 0;
1818}
1819
1820static int msm_hs_runtime_suspend(struct device *dev)
1821{
1822 struct platform_device *pdev = container_of(dev, struct
1823 platform_device, dev);
1824 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1825
1826 msm_hs_request_clock_off(&msm_uport->uport);
1827 return 0;
1828}
1829#else
1830#define msm_hs_runtime_idle NULL
1831#define msm_hs_runtime_resume NULL
1832#define msm_hs_runtime_suspend NULL
1833#endif
1834
1835static const struct dev_pm_ops msm_hs_dev_pm_ops = {
1836 .runtime_suspend = msm_hs_runtime_suspend,
1837 .runtime_resume = msm_hs_runtime_resume,
1838 .runtime_idle = msm_hs_runtime_idle,
1839};
1840
1841static struct platform_driver msm_serial_hs_platform_driver = {
1842 .probe = msm_hs_probe,
1843 .remove = __devexit_p(msm_hs_remove),
1844 .driver = {
1845 .name = "msm_serial_hs",
1846 .owner = THIS_MODULE,
1847 .pm = &msm_hs_dev_pm_ops,
1848 },
1849};
1850
1851static struct uart_driver msm_hs_driver = {
1852 .owner = THIS_MODULE,
1853 .driver_name = "msm_serial_hs",
1854 .dev_name = "ttyHS",
1855 .nr = UARTDM_NR,
1856 .cons = 0,
1857};
1858
1859static struct uart_ops msm_hs_ops = {
1860 .tx_empty = msm_hs_tx_empty,
1861 .set_mctrl = msm_hs_set_mctrl_locked,
1862 .get_mctrl = msm_hs_get_mctrl_locked,
1863 .stop_tx = msm_hs_stop_tx_locked,
1864 .start_tx = msm_hs_start_tx_locked,
1865 .stop_rx = msm_hs_stop_rx_locked,
1866 .enable_ms = msm_hs_enable_ms_locked,
1867 .break_ctl = msm_hs_break_ctl,
1868 .startup = msm_hs_startup,
1869 .shutdown = msm_hs_shutdown,
1870 .set_termios = msm_hs_set_termios,
1871 .pm = msm_hs_pm,
1872 .type = msm_hs_type,
1873 .config_port = msm_hs_config_port,
1874 .release_port = msm_hs_release_port,
1875 .request_port = msm_hs_request_port,
1876};
1877
1878MODULE_DESCRIPTION("High Speed UART Driver for the MSM chipset");
1879MODULE_VERSION("1.2");
1880MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/msm_smd_tty.c b/drivers/tty/serial/msm_smd_tty.c
new file mode 100644
index 000000000000..4f41dcdcb771
--- /dev/null
+++ b/drivers/tty/serial/msm_smd_tty.c
@@ -0,0 +1,235 @@
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
4 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/cdev.h>
20#include <linux/device.h>
21#include <linux/wait.h>
22
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26
27#include <mach/msm_smd.h>
28
29#define MAX_SMD_TTYS 32
30
31struct smd_tty_info {
32 struct tty_port port;
33 smd_channel_t *ch;
34};
35
36struct smd_tty_channel_desc {
37 int id;
38 const char *name;
39};
40
41static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
42
43static const struct smd_tty_channel_desc smd_default_tty_channels[] = {
44 { .id = 0, .name = "SMD_DS" },
45 { .id = 27, .name = "SMD_GPSNMEA" },
46};
47
48static const struct smd_tty_channel_desc *smd_tty_channels =
49 smd_default_tty_channels;
50static int smd_tty_channels_len = ARRAY_SIZE(smd_default_tty_channels);
51
52static void smd_tty_notify(void *priv, unsigned event)
53{
54 unsigned char *ptr;
55 int avail;
56 struct smd_tty_info *info = priv;
57 struct tty_struct *tty;
58
59 if (event != SMD_EVENT_DATA)
60 return;
61
62 tty = tty_port_tty_get(&info->port);
63 if (!tty)
64 return;
65
66 for (;;) {
67 if (test_bit(TTY_THROTTLED, &tty->flags))
68 break;
69 avail = smd_read_avail(info->ch);
70 if (avail == 0)
71 break;
72
73 avail = tty_prepare_flip_string(tty, &ptr, avail);
74
75 if (smd_read(info->ch, ptr, avail) != avail) {
76 /* shouldn't be possible since we're in interrupt
77 ** context here and nobody else could 'steal' our
78 ** characters.
79 */
80 pr_err("OOPS - smd_tty_buffer mismatch?!");
81 }
82
83 tty_flip_buffer_push(tty);
84 }
85
86 /* XXX only when writable and necessary */
87 tty_wakeup(tty);
88 tty_kref_put(tty);
89}
90
91static int smd_tty_port_activate(struct tty_port *tport, struct tty_struct *tty)
92{
93 int i, res = 0;
94 int n = tty->index;
95 const char *name = NULL;
96 struct smd_tty_info *info = smd_tty + n;
97
98 for (i = 0; i < smd_tty_channels_len; i++) {
99 if (smd_tty_channels[i].id == n) {
100 name = smd_tty_channels[i].name;
101 break;
102 }
103 }
104 if (!name)
105 return -ENODEV;
106
107 if (info->ch)
108 smd_kick(info->ch);
109 else
110 res = smd_open(name, &info->ch, info, smd_tty_notify);
111
112 if (!res)
113 tty->driver_data = info;
114
115 return res;
116}
117
118static void smd_tty_port_shutdown(struct tty_port *tport)
119{
120 struct smd_tty_info *info;
121 struct tty_struct *tty = tty_port_tty_get(tport);
122
123 info = tty->driver_data;
124 if (info->ch) {
125 smd_close(info->ch);
126 info->ch = 0;
127 }
128
129 tty->driver_data = 0;
130 tty_kref_put(tty);
131}
132
133static int smd_tty_open(struct tty_struct *tty, struct file *f)
134{
135 struct smd_tty_info *info = smd_tty + tty->index;
136
137 return tty_port_open(&info->port, tty, f);
138}
139
140static void smd_tty_close(struct tty_struct *tty, struct file *f)
141{
142 struct smd_tty_info *info = tty->driver_data;
143
144 tty_port_close(&info->port, tty, f);
145}
146
147static int smd_tty_write(struct tty_struct *tty,
148 const unsigned char *buf, int len)
149{
150 struct smd_tty_info *info = tty->driver_data;
151 int avail;
152
153 /* if we're writing to a packet channel we will
154 ** never be able to write more data than there
155 ** is currently space for
156 */
157 avail = smd_write_avail(info->ch);
158 if (len > avail)
159 len = avail;
160
161 return smd_write(info->ch, buf, len);
162}
163
164static int smd_tty_write_room(struct tty_struct *tty)
165{
166 struct smd_tty_info *info = tty->driver_data;
167 return smd_write_avail(info->ch);
168}
169
170static int smd_tty_chars_in_buffer(struct tty_struct *tty)
171{
172 struct smd_tty_info *info = tty->driver_data;
173 return smd_read_avail(info->ch);
174}
175
176static void smd_tty_unthrottle(struct tty_struct *tty)
177{
178 struct smd_tty_info *info = tty->driver_data;
179 smd_kick(info->ch);
180}
181
182static const struct tty_port_operations smd_tty_port_ops = {
183 .shutdown = smd_tty_port_shutdown,
184 .activate = smd_tty_port_activate,
185};
186
187static const struct tty_operations smd_tty_ops = {
188 .open = smd_tty_open,
189 .close = smd_tty_close,
190 .write = smd_tty_write,
191 .write_room = smd_tty_write_room,
192 .chars_in_buffer = smd_tty_chars_in_buffer,
193 .unthrottle = smd_tty_unthrottle,
194};
195
196static struct tty_driver *smd_tty_driver;
197
198static int __init smd_tty_init(void)
199{
200 int ret, i;
201
202 smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
203 if (smd_tty_driver == 0)
204 return -ENOMEM;
205
206 smd_tty_driver->owner = THIS_MODULE;
207 smd_tty_driver->driver_name = "smd_tty_driver";
208 smd_tty_driver->name = "smd";
209 smd_tty_driver->major = 0;
210 smd_tty_driver->minor_start = 0;
211 smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
212 smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
213 smd_tty_driver->init_termios = tty_std_termios;
214 smd_tty_driver->init_termios.c_iflag = 0;
215 smd_tty_driver->init_termios.c_oflag = 0;
216 smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
217 smd_tty_driver->init_termios.c_lflag = 0;
218 smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
219 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
220 tty_set_operations(smd_tty_driver, &smd_tty_ops);
221
222 ret = tty_register_driver(smd_tty_driver);
223 if (ret)
224 return ret;
225
226 for (i = 0; i < smd_tty_channels_len; i++) {
227 tty_port_init(&smd_tty[smd_tty_channels[i].id].port);
228 smd_tty[smd_tty_channels[i].id].port.ops = &smd_tty_port_ops;
229 tty_register_device(smd_tty_driver, smd_tty_channels[i].id, 0);
230 }
231
232 return 0;
233}
234
235module_init(smd_tty_init);
diff --git a/drivers/tty/serial/mux.c b/drivers/tty/serial/mux.c
new file mode 100644
index 000000000000..9711e06a8374
--- /dev/null
+++ b/drivers/tty/serial/mux.c
@@ -0,0 +1,633 @@
1/*
2** mux.c:
3** serial driver for the Mux console found in some PA-RISC servers.
4**
5** (c) Copyright 2002 Ryan Bradetich
6** (c) Copyright 2002 Hewlett-Packard Company
7**
8** This program is free software; you can redistribute it and/or modify
9** it under the terms of the GNU General Public License as published by
10** the Free Software Foundation; either version 2 of the License, or
11** (at your option) any later version.
12**
13** This Driver currently only supports the console (port 0) on the MUX.
14** Additional work will be needed on this driver to enable the full
15** functionality of the MUX.
16**
17*/
18
19#include <linux/module.h>
20#include <linux/tty.h>
21#include <linux/ioport.h>
22#include <linux/init.h>
23#include <linux/serial.h>
24#include <linux/console.h>
25#include <linux/delay.h> /* for udelay */
26#include <linux/device.h>
27#include <asm/io.h>
28#include <asm/irq.h>
29#include <asm/parisc-device.h>
30
31#ifdef CONFIG_MAGIC_SYSRQ
32#include <linux/sysrq.h>
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/serial_core.h>
37
38#define MUX_OFFSET 0x800
39#define MUX_LINE_OFFSET 0x80
40
41#define MUX_FIFO_SIZE 255
42#define MUX_POLL_DELAY (30 * HZ / 1000)
43
44#define IO_DATA_REG_OFFSET 0x3c
45#define IO_DCOUNT_REG_OFFSET 0x40
46
47#define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
48#define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
49#define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
50
51#define MUX_NR 256
52static unsigned int port_cnt __read_mostly;
53struct mux_port {
54 struct uart_port port;
55 int enabled;
56};
57static struct mux_port mux_ports[MUX_NR];
58
59static struct uart_driver mux_driver = {
60 .owner = THIS_MODULE,
61 .driver_name = "ttyB",
62 .dev_name = "ttyB",
63 .major = MUX_MAJOR,
64 .minor = 0,
65 .nr = MUX_NR,
66};
67
68static struct timer_list mux_timer;
69
70#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + IO_DATA_REG_OFFSET)
71#define UART_GET_FIFO_CNT(p) __raw_readl((p)->membase + IO_DCOUNT_REG_OFFSET)
72
73/**
74 * get_mux_port_count - Get the number of available ports on the Mux.
75 * @dev: The parisc device.
76 *
77 * This function is used to determine the number of ports the Mux
78 * supports. The IODC data reports the number of ports the Mux
79 * can support, but there are cases where not all the Mux ports
80 * are connected. This function can override the IODC and
81 * return the true port count.
82 */
83static int __init get_mux_port_count(struct parisc_device *dev)
84{
85 int status;
86 u8 iodc_data[32];
87 unsigned long bytecnt;
88
89 /* If this is the built-in Mux for the K-Class (Eole CAP/MUX),
90 * we only need to allocate resources for 1 port since the
91 * other 7 ports are not connected.
92 */
93 if(dev->id.hversion == 0x15)
94 return 1;
95
96 status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
97 BUG_ON(status != PDC_OK);
98
99 /* Return the number of ports specified in the iodc data. */
100 return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
101}
102
103/**
104 * mux_tx_empty - Check if the transmitter fifo is empty.
105 * @port: Ptr to the uart_port.
106 *
107 * This function test if the transmitter fifo for the port
108 * described by 'port' is empty. If it is empty, this function
109 * should return TIOCSER_TEMT, otherwise return 0.
110 */
111static unsigned int mux_tx_empty(struct uart_port *port)
112{
113 return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
114}
115
116/**
117 * mux_set_mctrl - Set the current state of the modem control inputs.
118 * @ports: Ptr to the uart_port.
119 * @mctrl: Modem control bits.
120 *
121 * The Serial MUX does not support CTS, DCD or DSR so this function
122 * is ignored.
123 */
124static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
125{
126}
127
128/**
129 * mux_get_mctrl - Returns the current state of modem control inputs.
130 * @port: Ptr to the uart_port.
131 *
132 * The Serial MUX does not support CTS, DCD or DSR so these lines are
133 * treated as permanently active.
134 */
135static unsigned int mux_get_mctrl(struct uart_port *port)
136{
137 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
138}
139
140/**
141 * mux_stop_tx - Stop transmitting characters.
142 * @port: Ptr to the uart_port.
143 *
144 * The Serial MUX does not support this function.
145 */
146static void mux_stop_tx(struct uart_port *port)
147{
148}
149
150/**
151 * mux_start_tx - Start transmitting characters.
152 * @port: Ptr to the uart_port.
153 *
154 * The Serial Mux does not support this function.
155 */
156static void mux_start_tx(struct uart_port *port)
157{
158}
159
160/**
161 * mux_stop_rx - Stop receiving characters.
162 * @port: Ptr to the uart_port.
163 *
164 * The Serial Mux does not support this function.
165 */
166static void mux_stop_rx(struct uart_port *port)
167{
168}
169
170/**
171 * mux_enable_ms - Enable modum status interrupts.
172 * @port: Ptr to the uart_port.
173 *
174 * The Serial Mux does not support this function.
175 */
176static void mux_enable_ms(struct uart_port *port)
177{
178}
179
180/**
181 * mux_break_ctl - Control the transmitssion of a break signal.
182 * @port: Ptr to the uart_port.
183 * @break_state: Raise/Lower the break signal.
184 *
185 * The Serial Mux does not support this function.
186 */
187static void mux_break_ctl(struct uart_port *port, int break_state)
188{
189}
190
191/**
192 * mux_write - Write chars to the mux fifo.
193 * @port: Ptr to the uart_port.
194 *
195 * This function writes all the data from the uart buffer to
196 * the mux fifo.
197 */
198static void mux_write(struct uart_port *port)
199{
200 int count;
201 struct circ_buf *xmit = &port->state->xmit;
202
203 if(port->x_char) {
204 UART_PUT_CHAR(port, port->x_char);
205 port->icount.tx++;
206 port->x_char = 0;
207 return;
208 }
209
210 if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
211 mux_stop_tx(port);
212 return;
213 }
214
215 count = (port->fifosize) - UART_GET_FIFO_CNT(port);
216 do {
217 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
218 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
219 port->icount.tx++;
220 if(uart_circ_empty(xmit))
221 break;
222
223 } while(--count > 0);
224
225 while(UART_GET_FIFO_CNT(port))
226 udelay(1);
227
228 if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
229 uart_write_wakeup(port);
230
231 if (uart_circ_empty(xmit))
232 mux_stop_tx(port);
233}
234
235/**
236 * mux_read - Read chars from the mux fifo.
237 * @port: Ptr to the uart_port.
238 *
239 * This reads all available data from the mux's fifo and pushes
240 * the data to the tty layer.
241 */
242static void mux_read(struct uart_port *port)
243{
244 int data;
245 struct tty_struct *tty = port->state->port.tty;
246 __u32 start_count = port->icount.rx;
247
248 while(1) {
249 data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
250
251 if (MUX_STATUS(data))
252 continue;
253
254 if (MUX_EOFIFO(data))
255 break;
256
257 port->icount.rx++;
258
259 if (MUX_BREAK(data)) {
260 port->icount.brk++;
261 if(uart_handle_break(port))
262 continue;
263 }
264
265 if (uart_handle_sysrq_char(port, data & 0xffu))
266 continue;
267
268 tty_insert_flip_char(tty, data & 0xFF, TTY_NORMAL);
269 }
270
271 if (start_count != port->icount.rx) {
272 tty_flip_buffer_push(tty);
273 }
274}
275
276/**
277 * mux_startup - Initialize the port.
278 * @port: Ptr to the uart_port.
279 *
280 * Grab any resources needed for this port and start the
281 * mux timer.
282 */
283static int mux_startup(struct uart_port *port)
284{
285 mux_ports[port->line].enabled = 1;
286 return 0;
287}
288
289/**
290 * mux_shutdown - Disable the port.
291 * @port: Ptr to the uart_port.
292 *
293 * Release any resources needed for the port.
294 */
295static void mux_shutdown(struct uart_port *port)
296{
297 mux_ports[port->line].enabled = 0;
298}
299
300/**
301 * mux_set_termios - Chane port parameters.
302 * @port: Ptr to the uart_port.
303 * @termios: new termios settings.
304 * @old: old termios settings.
305 *
306 * The Serial Mux does not support this function.
307 */
308static void
309mux_set_termios(struct uart_port *port, struct ktermios *termios,
310 struct ktermios *old)
311{
312}
313
314/**
315 * mux_type - Describe the port.
316 * @port: Ptr to the uart_port.
317 *
318 * Return a pointer to a string constant describing the
319 * specified port.
320 */
321static const char *mux_type(struct uart_port *port)
322{
323 return "Mux";
324}
325
326/**
327 * mux_release_port - Release memory and IO regions.
328 * @port: Ptr to the uart_port.
329 *
330 * Release any memory and IO region resources currently in use by
331 * the port.
332 */
333static void mux_release_port(struct uart_port *port)
334{
335}
336
337/**
338 * mux_request_port - Request memory and IO regions.
339 * @port: Ptr to the uart_port.
340 *
341 * Request any memory and IO region resources required by the port.
342 * If any fail, no resources should be registered when this function
343 * returns, and it should return -EBUSY on failure.
344 */
345static int mux_request_port(struct uart_port *port)
346{
347 return 0;
348}
349
350/**
351 * mux_config_port - Perform port autoconfiguration.
352 * @port: Ptr to the uart_port.
353 * @type: Bitmask of required configurations.
354 *
355 * Perform any autoconfiguration steps for the port. This function is
356 * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
357 * [Note: This is required for now because of a bug in the Serial core.
358 * rmk has already submitted a patch to linus, should be available for
359 * 2.5.47.]
360 */
361static void mux_config_port(struct uart_port *port, int type)
362{
363 port->type = PORT_MUX;
364}
365
366/**
367 * mux_verify_port - Verify the port information.
368 * @port: Ptr to the uart_port.
369 * @ser: Ptr to the serial information.
370 *
371 * Verify the new serial port information contained within serinfo is
372 * suitable for this port type.
373 */
374static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
375{
376 if(port->membase == NULL)
377 return -EINVAL;
378
379 return 0;
380}
381
382/**
383 * mux_drv_poll - Mux poll function.
384 * @unused: Unused variable
385 *
386 * This function periodically polls the Serial MUX to check for new data.
387 */
388static void mux_poll(unsigned long unused)
389{
390 int i;
391
392 for(i = 0; i < port_cnt; ++i) {
393 if(!mux_ports[i].enabled)
394 continue;
395
396 mux_read(&mux_ports[i].port);
397 mux_write(&mux_ports[i].port);
398 }
399
400 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
401}
402
403
404#ifdef CONFIG_SERIAL_MUX_CONSOLE
405static void mux_console_write(struct console *co, const char *s, unsigned count)
406{
407 /* Wait until the FIFO drains. */
408 while(UART_GET_FIFO_CNT(&mux_ports[0].port))
409 udelay(1);
410
411 while(count--) {
412 if(*s == '\n') {
413 UART_PUT_CHAR(&mux_ports[0].port, '\r');
414 }
415 UART_PUT_CHAR(&mux_ports[0].port, *s++);
416 }
417
418}
419
420static int mux_console_setup(struct console *co, char *options)
421{
422 return 0;
423}
424
425struct tty_driver *mux_console_device(struct console *co, int *index)
426{
427 *index = co->index;
428 return mux_driver.tty_driver;
429}
430
431static struct console mux_console = {
432 .name = "ttyB",
433 .write = mux_console_write,
434 .device = mux_console_device,
435 .setup = mux_console_setup,
436 .flags = CON_ENABLED | CON_PRINTBUFFER,
437 .index = 0,
438};
439
440#define MUX_CONSOLE &mux_console
441#else
442#define MUX_CONSOLE NULL
443#endif
444
445static struct uart_ops mux_pops = {
446 .tx_empty = mux_tx_empty,
447 .set_mctrl = mux_set_mctrl,
448 .get_mctrl = mux_get_mctrl,
449 .stop_tx = mux_stop_tx,
450 .start_tx = mux_start_tx,
451 .stop_rx = mux_stop_rx,
452 .enable_ms = mux_enable_ms,
453 .break_ctl = mux_break_ctl,
454 .startup = mux_startup,
455 .shutdown = mux_shutdown,
456 .set_termios = mux_set_termios,
457 .type = mux_type,
458 .release_port = mux_release_port,
459 .request_port = mux_request_port,
460 .config_port = mux_config_port,
461 .verify_port = mux_verify_port,
462};
463
464/**
465 * mux_probe - Determine if the Serial Mux should claim this device.
466 * @dev: The parisc device.
467 *
468 * Deterimine if the Serial Mux should claim this chip (return 0)
469 * or not (return 1).
470 */
471static int __init mux_probe(struct parisc_device *dev)
472{
473 int i, status;
474
475 int port_count = get_mux_port_count(dev);
476 printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
477
478 dev_set_drvdata(&dev->dev, (void *)(long)port_count);
479 request_mem_region(dev->hpa.start + MUX_OFFSET,
480 port_count * MUX_LINE_OFFSET, "Mux");
481
482 if(!port_cnt) {
483 mux_driver.cons = MUX_CONSOLE;
484
485 status = uart_register_driver(&mux_driver);
486 if(status) {
487 printk(KERN_ERR "Serial mux: Unable to register driver.\n");
488 return 1;
489 }
490 }
491
492 for(i = 0; i < port_count; ++i, ++port_cnt) {
493 struct uart_port *port = &mux_ports[port_cnt].port;
494 port->iobase = 0;
495 port->mapbase = dev->hpa.start + MUX_OFFSET +
496 (i * MUX_LINE_OFFSET);
497 port->membase = ioremap_nocache(port->mapbase, MUX_LINE_OFFSET);
498 port->iotype = UPIO_MEM;
499 port->type = PORT_MUX;
500 port->irq = NO_IRQ;
501 port->uartclk = 0;
502 port->fifosize = MUX_FIFO_SIZE;
503 port->ops = &mux_pops;
504 port->flags = UPF_BOOT_AUTOCONF;
505 port->line = port_cnt;
506
507 /* The port->timeout needs to match what is present in
508 * uart_wait_until_sent in serial_core.c. Otherwise
509 * the time spent in msleep_interruptable will be very
510 * long, causing the appearance of a console hang.
511 */
512 port->timeout = HZ / 50;
513 spin_lock_init(&port->lock);
514
515 status = uart_add_one_port(&mux_driver, port);
516 BUG_ON(status);
517 }
518
519 return 0;
520}
521
522static int __devexit mux_remove(struct parisc_device *dev)
523{
524 int i, j;
525 int port_count = (long)dev_get_drvdata(&dev->dev);
526
527 /* Find Port 0 for this card in the mux_ports list. */
528 for(i = 0; i < port_cnt; ++i) {
529 if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET)
530 break;
531 }
532 BUG_ON(i + port_count > port_cnt);
533
534 /* Release the resources associated with each port on the device. */
535 for(j = 0; j < port_count; ++j, ++i) {
536 struct uart_port *port = &mux_ports[i].port;
537
538 uart_remove_one_port(&mux_driver, port);
539 if(port->membase)
540 iounmap(port->membase);
541 }
542
543 release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
544 return 0;
545}
546
547/* Hack. This idea was taken from the 8250_gsc.c on how to properly order
548 * the serial port detection in the proper order. The idea is we always
549 * want the builtin mux to be detected before addin mux cards, so we
550 * specifically probe for the builtin mux cards first.
551 *
552 * This table only contains the parisc_device_id of known builtin mux
553 * devices. All other mux cards will be detected by the generic mux_tbl.
554 */
555static struct parisc_device_id builtin_mux_tbl[] = {
556 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
557 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
558 { 0, }
559};
560
561static struct parisc_device_id mux_tbl[] = {
562 { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
563 { 0, }
564};
565
566MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
567MODULE_DEVICE_TABLE(parisc, mux_tbl);
568
569static struct parisc_driver builtin_serial_mux_driver = {
570 .name = "builtin_serial_mux",
571 .id_table = builtin_mux_tbl,
572 .probe = mux_probe,
573 .remove = __devexit_p(mux_remove),
574};
575
576static struct parisc_driver serial_mux_driver = {
577 .name = "serial_mux",
578 .id_table = mux_tbl,
579 .probe = mux_probe,
580 .remove = __devexit_p(mux_remove),
581};
582
583/**
584 * mux_init - Serial MUX initialization procedure.
585 *
586 * Register the Serial MUX driver.
587 */
588static int __init mux_init(void)
589{
590 register_parisc_driver(&builtin_serial_mux_driver);
591 register_parisc_driver(&serial_mux_driver);
592
593 if(port_cnt > 0) {
594 /* Start the Mux timer */
595 init_timer(&mux_timer);
596 mux_timer.function = mux_poll;
597 mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
598
599#ifdef CONFIG_SERIAL_MUX_CONSOLE
600 register_console(&mux_console);
601#endif
602 }
603
604 return 0;
605}
606
607/**
608 * mux_exit - Serial MUX cleanup procedure.
609 *
610 * Unregister the Serial MUX driver from the tty layer.
611 */
612static void __exit mux_exit(void)
613{
614 /* Delete the Mux timer. */
615 if(port_cnt > 0) {
616 del_timer(&mux_timer);
617#ifdef CONFIG_SERIAL_MUX_CONSOLE
618 unregister_console(&mux_console);
619#endif
620 }
621
622 unregister_parisc_driver(&builtin_serial_mux_driver);
623 unregister_parisc_driver(&serial_mux_driver);
624 uart_unregister_driver(&mux_driver);
625}
626
627module_init(mux_init);
628module_exit(mux_exit);
629
630MODULE_AUTHOR("Ryan Bradetich");
631MODULE_DESCRIPTION("Serial MUX driver");
632MODULE_LICENSE("GPL");
633MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);
diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
new file mode 100644
index 000000000000..7e02c9c344fe
--- /dev/null
+++ b/drivers/tty/serial/mxs-auart.c
@@ -0,0 +1,798 @@
1/*
2 * Freescale STMP37XX/STMP378X Application UART driver
3 *
4 * Author: dmitry pervushin <dimka@embeddedalley.com>
5 *
6 * Copyright 2008-2010 Freescale Semiconductor, Inc.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8 *
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
12 *
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
15 */
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/console.h>
21#include <linux/interrupt.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/wait.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <linux/serial.h>
29#include <linux/serial_core.h>
30#include <linux/platform_device.h>
31#include <linux/device.h>
32#include <linux/clk.h>
33#include <linux/delay.h>
34#include <linux/io.h>
35
36#include <asm/cacheflush.h>
37
38#define MXS_AUART_PORTS 5
39
40#define AUART_CTRL0 0x00000000
41#define AUART_CTRL0_SET 0x00000004
42#define AUART_CTRL0_CLR 0x00000008
43#define AUART_CTRL0_TOG 0x0000000c
44#define AUART_CTRL1 0x00000010
45#define AUART_CTRL1_SET 0x00000014
46#define AUART_CTRL1_CLR 0x00000018
47#define AUART_CTRL1_TOG 0x0000001c
48#define AUART_CTRL2 0x00000020
49#define AUART_CTRL2_SET 0x00000024
50#define AUART_CTRL2_CLR 0x00000028
51#define AUART_CTRL2_TOG 0x0000002c
52#define AUART_LINECTRL 0x00000030
53#define AUART_LINECTRL_SET 0x00000034
54#define AUART_LINECTRL_CLR 0x00000038
55#define AUART_LINECTRL_TOG 0x0000003c
56#define AUART_LINECTRL2 0x00000040
57#define AUART_LINECTRL2_SET 0x00000044
58#define AUART_LINECTRL2_CLR 0x00000048
59#define AUART_LINECTRL2_TOG 0x0000004c
60#define AUART_INTR 0x00000050
61#define AUART_INTR_SET 0x00000054
62#define AUART_INTR_CLR 0x00000058
63#define AUART_INTR_TOG 0x0000005c
64#define AUART_DATA 0x00000060
65#define AUART_STAT 0x00000070
66#define AUART_DEBUG 0x00000080
67#define AUART_VERSION 0x00000090
68#define AUART_AUTOBAUD 0x000000a0
69
70#define AUART_CTRL0_SFTRST (1 << 31)
71#define AUART_CTRL0_CLKGATE (1 << 30)
72
73#define AUART_CTRL2_CTSEN (1 << 15)
74#define AUART_CTRL2_RTS (1 << 11)
75#define AUART_CTRL2_RXE (1 << 9)
76#define AUART_CTRL2_TXE (1 << 8)
77#define AUART_CTRL2_UARTEN (1 << 0)
78
79#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
80#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
81#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
82#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
83#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
84#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
85#define AUART_LINECTRL_WLEN_MASK 0x00000060
86#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
87#define AUART_LINECTRL_FEN (1 << 4)
88#define AUART_LINECTRL_STP2 (1 << 3)
89#define AUART_LINECTRL_EPS (1 << 2)
90#define AUART_LINECTRL_PEN (1 << 1)
91#define AUART_LINECTRL_BRK (1 << 0)
92
93#define AUART_INTR_RTIEN (1 << 22)
94#define AUART_INTR_TXIEN (1 << 21)
95#define AUART_INTR_RXIEN (1 << 20)
96#define AUART_INTR_CTSMIEN (1 << 17)
97#define AUART_INTR_RTIS (1 << 6)
98#define AUART_INTR_TXIS (1 << 5)
99#define AUART_INTR_RXIS (1 << 4)
100#define AUART_INTR_CTSMIS (1 << 1)
101
102#define AUART_STAT_BUSY (1 << 29)
103#define AUART_STAT_CTS (1 << 28)
104#define AUART_STAT_TXFE (1 << 27)
105#define AUART_STAT_TXFF (1 << 25)
106#define AUART_STAT_RXFE (1 << 24)
107#define AUART_STAT_OERR (1 << 19)
108#define AUART_STAT_BERR (1 << 18)
109#define AUART_STAT_PERR (1 << 17)
110#define AUART_STAT_FERR (1 << 16)
111
112static struct uart_driver auart_driver;
113
114struct mxs_auart_port {
115 struct uart_port port;
116
117 unsigned int flags;
118 unsigned int ctrl;
119
120 unsigned int irq;
121
122 struct clk *clk;
123 struct device *dev;
124};
125
126static void mxs_auart_stop_tx(struct uart_port *u);
127
128#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
129
130static inline void mxs_auart_tx_chars(struct mxs_auart_port *s)
131{
132 struct circ_buf *xmit = &s->port.state->xmit;
133
134 while (!(readl(s->port.membase + AUART_STAT) &
135 AUART_STAT_TXFF)) {
136 if (s->port.x_char) {
137 s->port.icount.tx++;
138 writel(s->port.x_char,
139 s->port.membase + AUART_DATA);
140 s->port.x_char = 0;
141 continue;
142 }
143 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
144 s->port.icount.tx++;
145 writel(xmit->buf[xmit->tail],
146 s->port.membase + AUART_DATA);
147 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
148 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
149 uart_write_wakeup(&s->port);
150 } else
151 break;
152 }
153 if (uart_circ_empty(&(s->port.state->xmit)))
154 writel(AUART_INTR_TXIEN,
155 s->port.membase + AUART_INTR_CLR);
156 else
157 writel(AUART_INTR_TXIEN,
158 s->port.membase + AUART_INTR_SET);
159
160 if (uart_tx_stopped(&s->port))
161 mxs_auart_stop_tx(&s->port);
162}
163
164static void mxs_auart_rx_char(struct mxs_auart_port *s)
165{
166 int flag;
167 u32 stat;
168 u8 c;
169
170 c = readl(s->port.membase + AUART_DATA);
171 stat = readl(s->port.membase + AUART_STAT);
172
173 flag = TTY_NORMAL;
174 s->port.icount.rx++;
175
176 if (stat & AUART_STAT_BERR) {
177 s->port.icount.brk++;
178 if (uart_handle_break(&s->port))
179 goto out;
180 } else if (stat & AUART_STAT_PERR) {
181 s->port.icount.parity++;
182 } else if (stat & AUART_STAT_FERR) {
183 s->port.icount.frame++;
184 }
185
186 /*
187 * Mask off conditions which should be ingored.
188 */
189 stat &= s->port.read_status_mask;
190
191 if (stat & AUART_STAT_BERR) {
192 flag = TTY_BREAK;
193 } else if (stat & AUART_STAT_PERR)
194 flag = TTY_PARITY;
195 else if (stat & AUART_STAT_FERR)
196 flag = TTY_FRAME;
197
198 if (stat & AUART_STAT_OERR)
199 s->port.icount.overrun++;
200
201 if (uart_handle_sysrq_char(&s->port, c))
202 goto out;
203
204 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
205out:
206 writel(stat, s->port.membase + AUART_STAT);
207}
208
209static void mxs_auart_rx_chars(struct mxs_auart_port *s)
210{
211 struct tty_struct *tty = s->port.state->port.tty;
212 u32 stat = 0;
213
214 for (;;) {
215 stat = readl(s->port.membase + AUART_STAT);
216 if (stat & AUART_STAT_RXFE)
217 break;
218 mxs_auart_rx_char(s);
219 }
220
221 writel(stat, s->port.membase + AUART_STAT);
222 tty_flip_buffer_push(tty);
223}
224
225static int mxs_auart_request_port(struct uart_port *u)
226{
227 return 0;
228}
229
230static int mxs_auart_verify_port(struct uart_port *u,
231 struct serial_struct *ser)
232{
233 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
234 return -EINVAL;
235 return 0;
236}
237
238static void mxs_auart_config_port(struct uart_port *u, int flags)
239{
240}
241
242static const char *mxs_auart_type(struct uart_port *u)
243{
244 struct mxs_auart_port *s = to_auart_port(u);
245
246 return dev_name(s->dev);
247}
248
249static void mxs_auart_release_port(struct uart_port *u)
250{
251}
252
253static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
254{
255 struct mxs_auart_port *s = to_auart_port(u);
256
257 u32 ctrl = readl(u->membase + AUART_CTRL2);
258
259 ctrl &= ~AUART_CTRL2_RTS;
260 if (mctrl & TIOCM_RTS)
261 ctrl |= AUART_CTRL2_RTS;
262 s->ctrl = mctrl;
263 writel(ctrl, u->membase + AUART_CTRL2);
264}
265
266static u32 mxs_auart_get_mctrl(struct uart_port *u)
267{
268 struct mxs_auart_port *s = to_auart_port(u);
269 u32 stat = readl(u->membase + AUART_STAT);
270 int ctrl2 = readl(u->membase + AUART_CTRL2);
271 u32 mctrl = s->ctrl;
272
273 mctrl &= ~TIOCM_CTS;
274 if (stat & AUART_STAT_CTS)
275 mctrl |= TIOCM_CTS;
276
277 if (ctrl2 & AUART_CTRL2_RTS)
278 mctrl |= TIOCM_RTS;
279
280 return mctrl;
281}
282
283static void mxs_auart_settermios(struct uart_port *u,
284 struct ktermios *termios,
285 struct ktermios *old)
286{
287 u32 bm, ctrl, ctrl2, div;
288 unsigned int cflag, baud;
289
290 cflag = termios->c_cflag;
291
292 ctrl = AUART_LINECTRL_FEN;
293 ctrl2 = readl(u->membase + AUART_CTRL2);
294
295 /* byte size */
296 switch (cflag & CSIZE) {
297 case CS5:
298 bm = 0;
299 break;
300 case CS6:
301 bm = 1;
302 break;
303 case CS7:
304 bm = 2;
305 break;
306 case CS8:
307 bm = 3;
308 break;
309 default:
310 return;
311 }
312
313 ctrl |= AUART_LINECTRL_WLEN(bm);
314
315 /* parity */
316 if (cflag & PARENB) {
317 ctrl |= AUART_LINECTRL_PEN;
318 if ((cflag & PARODD) == 0)
319 ctrl |= AUART_LINECTRL_EPS;
320 }
321
322 u->read_status_mask = 0;
323
324 if (termios->c_iflag & INPCK)
325 u->read_status_mask |= AUART_STAT_PERR;
326 if (termios->c_iflag & (BRKINT | PARMRK))
327 u->read_status_mask |= AUART_STAT_BERR;
328
329 /*
330 * Characters to ignore
331 */
332 u->ignore_status_mask = 0;
333 if (termios->c_iflag & IGNPAR)
334 u->ignore_status_mask |= AUART_STAT_PERR;
335 if (termios->c_iflag & IGNBRK) {
336 u->ignore_status_mask |= AUART_STAT_BERR;
337 /*
338 * If we're ignoring parity and break indicators,
339 * ignore overruns too (for real raw support).
340 */
341 if (termios->c_iflag & IGNPAR)
342 u->ignore_status_mask |= AUART_STAT_OERR;
343 }
344
345 /*
346 * ignore all characters if CREAD is not set
347 */
348 if (cflag & CREAD)
349 ctrl2 |= AUART_CTRL2_RXE;
350 else
351 ctrl2 &= ~AUART_CTRL2_RXE;
352
353 /* figure out the stop bits requested */
354 if (cflag & CSTOPB)
355 ctrl |= AUART_LINECTRL_STP2;
356
357 /* figure out the hardware flow control settings */
358 if (cflag & CRTSCTS)
359 ctrl2 |= AUART_CTRL2_CTSEN;
360 else
361 ctrl2 &= ~AUART_CTRL2_CTSEN;
362
363 /* set baud rate */
364 baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
365 div = u->uartclk * 32 / baud;
366 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
367 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
368
369 writel(ctrl, u->membase + AUART_LINECTRL);
370 writel(ctrl2, u->membase + AUART_CTRL2);
371}
372
373static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
374{
375 u32 istatus, istat;
376 struct mxs_auart_port *s = context;
377 u32 stat = readl(s->port.membase + AUART_STAT);
378
379 istatus = istat = readl(s->port.membase + AUART_INTR);
380
381 if (istat & AUART_INTR_CTSMIS) {
382 uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
383 writel(AUART_INTR_CTSMIS,
384 s->port.membase + AUART_INTR_CLR);
385 istat &= ~AUART_INTR_CTSMIS;
386 }
387
388 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
389 mxs_auart_rx_chars(s);
390 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
391 }
392
393 if (istat & AUART_INTR_TXIS) {
394 mxs_auart_tx_chars(s);
395 istat &= ~AUART_INTR_TXIS;
396 }
397
398 writel(istatus & (AUART_INTR_RTIS
399 | AUART_INTR_TXIS
400 | AUART_INTR_RXIS
401 | AUART_INTR_CTSMIS),
402 s->port.membase + AUART_INTR_CLR);
403
404 return IRQ_HANDLED;
405}
406
407static void mxs_auart_reset(struct uart_port *u)
408{
409 int i;
410 unsigned int reg;
411
412 writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
413
414 for (i = 0; i < 10000; i++) {
415 reg = readl(u->membase + AUART_CTRL0);
416 if (!(reg & AUART_CTRL0_SFTRST))
417 break;
418 udelay(3);
419 }
420 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
421}
422
423static int mxs_auart_startup(struct uart_port *u)
424{
425 struct mxs_auart_port *s = to_auart_port(u);
426
427 clk_enable(s->clk);
428
429 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
430
431 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
432
433 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
434 u->membase + AUART_INTR);
435
436 /*
437 * Enable fifo so all four bytes of a DMA word are written to
438 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
439 */
440 writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
441
442 return 0;
443}
444
445static void mxs_auart_shutdown(struct uart_port *u)
446{
447 struct mxs_auart_port *s = to_auart_port(u);
448
449 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
450
451 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
452
453 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
454 u->membase + AUART_INTR_CLR);
455
456 clk_disable(s->clk);
457}
458
459static unsigned int mxs_auart_tx_empty(struct uart_port *u)
460{
461 if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
462 return TIOCSER_TEMT;
463 else
464 return 0;
465}
466
467static void mxs_auart_start_tx(struct uart_port *u)
468{
469 struct mxs_auart_port *s = to_auart_port(u);
470
471 /* enable transmitter */
472 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
473
474 mxs_auart_tx_chars(s);
475}
476
477static void mxs_auart_stop_tx(struct uart_port *u)
478{
479 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
480}
481
482static void mxs_auart_stop_rx(struct uart_port *u)
483{
484 writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
485}
486
487static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
488{
489 if (ctl)
490 writel(AUART_LINECTRL_BRK,
491 u->membase + AUART_LINECTRL_SET);
492 else
493 writel(AUART_LINECTRL_BRK,
494 u->membase + AUART_LINECTRL_CLR);
495}
496
497static void mxs_auart_enable_ms(struct uart_port *port)
498{
499 /* just empty */
500}
501
502static struct uart_ops mxs_auart_ops = {
503 .tx_empty = mxs_auart_tx_empty,
504 .start_tx = mxs_auart_start_tx,
505 .stop_tx = mxs_auart_stop_tx,
506 .stop_rx = mxs_auart_stop_rx,
507 .enable_ms = mxs_auart_enable_ms,
508 .break_ctl = mxs_auart_break_ctl,
509 .set_mctrl = mxs_auart_set_mctrl,
510 .get_mctrl = mxs_auart_get_mctrl,
511 .startup = mxs_auart_startup,
512 .shutdown = mxs_auart_shutdown,
513 .set_termios = mxs_auart_settermios,
514 .type = mxs_auart_type,
515 .release_port = mxs_auart_release_port,
516 .request_port = mxs_auart_request_port,
517 .config_port = mxs_auart_config_port,
518 .verify_port = mxs_auart_verify_port,
519};
520
521static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
522
523#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
524static void mxs_auart_console_putchar(struct uart_port *port, int ch)
525{
526 unsigned int to = 1000;
527
528 while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
529 if (!to--)
530 break;
531 udelay(1);
532 }
533
534 writel(ch, port->membase + AUART_DATA);
535}
536
537static void
538auart_console_write(struct console *co, const char *str, unsigned int count)
539{
540 struct mxs_auart_port *s;
541 struct uart_port *port;
542 unsigned int old_ctrl0, old_ctrl2;
543 unsigned int to = 1000;
544
545 if (co->index > MXS_AUART_PORTS || co->index < 0)
546 return;
547
548 s = auart_port[co->index];
549 port = &s->port;
550
551 clk_enable(s->clk);
552
553 /* First save the CR then disable the interrupts */
554 old_ctrl2 = readl(port->membase + AUART_CTRL2);
555 old_ctrl0 = readl(port->membase + AUART_CTRL0);
556
557 writel(AUART_CTRL0_CLKGATE,
558 port->membase + AUART_CTRL0_CLR);
559 writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
560 port->membase + AUART_CTRL2_SET);
561
562 uart_console_write(port, str, count, mxs_auart_console_putchar);
563
564 /*
565 * Finally, wait for transmitter to become empty
566 * and restore the TCR
567 */
568 while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
569 if (!to--)
570 break;
571 udelay(1);
572 }
573
574 writel(old_ctrl0, port->membase + AUART_CTRL0);
575 writel(old_ctrl2, port->membase + AUART_CTRL2);
576
577 clk_disable(s->clk);
578}
579
580static void __init
581auart_console_get_options(struct uart_port *port, int *baud,
582 int *parity, int *bits)
583{
584 unsigned int lcr_h, quot;
585
586 if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
587 return;
588
589 lcr_h = readl(port->membase + AUART_LINECTRL);
590
591 *parity = 'n';
592 if (lcr_h & AUART_LINECTRL_PEN) {
593 if (lcr_h & AUART_LINECTRL_EPS)
594 *parity = 'e';
595 else
596 *parity = 'o';
597 }
598
599 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
600 *bits = 7;
601 else
602 *bits = 8;
603
604 quot = ((readl(port->membase + AUART_LINECTRL)
605 & AUART_LINECTRL_BAUD_DIVINT_MASK))
606 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
607 quot |= ((readl(port->membase + AUART_LINECTRL)
608 & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
609 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
610 if (quot == 0)
611 quot = 1;
612
613 *baud = (port->uartclk << 2) / quot;
614}
615
616static int __init
617auart_console_setup(struct console *co, char *options)
618{
619 struct mxs_auart_port *s;
620 int baud = 9600;
621 int bits = 8;
622 int parity = 'n';
623 int flow = 'n';
624 int ret;
625
626 /*
627 * Check whether an invalid uart number has been specified, and
628 * if so, search for the first available port that does have
629 * console support.
630 */
631 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
632 co->index = 0;
633 s = auart_port[co->index];
634 if (!s)
635 return -ENODEV;
636
637 clk_enable(s->clk);
638
639 if (options)
640 uart_parse_options(options, &baud, &parity, &bits, &flow);
641 else
642 auart_console_get_options(&s->port, &baud, &parity, &bits);
643
644 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
645
646 clk_disable(s->clk);
647
648 return ret;
649}
650
651static struct console auart_console = {
652 .name = "ttyAPP",
653 .write = auart_console_write,
654 .device = uart_console_device,
655 .setup = auart_console_setup,
656 .flags = CON_PRINTBUFFER,
657 .index = -1,
658 .data = &auart_driver,
659};
660#endif
661
662static struct uart_driver auart_driver = {
663 .owner = THIS_MODULE,
664 .driver_name = "ttyAPP",
665 .dev_name = "ttyAPP",
666 .major = 0,
667 .minor = 0,
668 .nr = MXS_AUART_PORTS,
669#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
670 .cons = &auart_console,
671#endif
672};
673
674static int __devinit mxs_auart_probe(struct platform_device *pdev)
675{
676 struct mxs_auart_port *s;
677 u32 version;
678 int ret = 0;
679 struct resource *r;
680
681 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
682 if (!s) {
683 ret = -ENOMEM;
684 goto out;
685 }
686
687 s->clk = clk_get(&pdev->dev, NULL);
688 if (IS_ERR(s->clk)) {
689 ret = PTR_ERR(s->clk);
690 goto out_free;
691 }
692
693 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
694 if (!r) {
695 ret = -ENXIO;
696 goto out_free_clk;
697 }
698
699 s->port.mapbase = r->start;
700 s->port.membase = ioremap(r->start, resource_size(r));
701 s->port.ops = &mxs_auart_ops;
702 s->port.iotype = UPIO_MEM;
703 s->port.line = pdev->id < 0 ? 0 : pdev->id;
704 s->port.fifosize = 16;
705 s->port.uartclk = clk_get_rate(s->clk);
706 s->port.type = PORT_IMX;
707 s->port.dev = s->dev = get_device(&pdev->dev);
708
709 s->flags = 0;
710 s->ctrl = 0;
711
712 s->irq = platform_get_irq(pdev, 0);
713 s->port.irq = s->irq;
714 ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
715 if (ret)
716 goto out_free_clk;
717
718 platform_set_drvdata(pdev, s);
719
720 auart_port[pdev->id] = s;
721
722 mxs_auart_reset(&s->port);
723
724 ret = uart_add_one_port(&auart_driver, &s->port);
725 if (ret)
726 goto out_free_irq;
727
728 version = readl(s->port.membase + AUART_VERSION);
729 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
730 (version >> 24) & 0xff,
731 (version >> 16) & 0xff, version & 0xffff);
732
733 return 0;
734
735out_free_irq:
736 auart_port[pdev->id] = NULL;
737 free_irq(s->irq, s);
738out_free_clk:
739 clk_put(s->clk);
740out_free:
741 kfree(s);
742out:
743 return ret;
744}
745
746static int __devexit mxs_auart_remove(struct platform_device *pdev)
747{
748 struct mxs_auart_port *s = platform_get_drvdata(pdev);
749
750 uart_remove_one_port(&auart_driver, &s->port);
751
752 auart_port[pdev->id] = NULL;
753
754 clk_put(s->clk);
755 free_irq(s->irq, s);
756 kfree(s);
757
758 return 0;
759}
760
761static struct platform_driver mxs_auart_driver = {
762 .probe = mxs_auart_probe,
763 .remove = __devexit_p(mxs_auart_remove),
764 .driver = {
765 .name = "mxs-auart",
766 .owner = THIS_MODULE,
767 },
768};
769
770static int __init mxs_auart_init(void)
771{
772 int r;
773
774 r = uart_register_driver(&auart_driver);
775 if (r)
776 goto out;
777
778 r = platform_driver_register(&mxs_auart_driver);
779 if (r)
780 goto out_err;
781
782 return 0;
783out_err:
784 uart_unregister_driver(&auart_driver);
785out:
786 return r;
787}
788
789static void __exit mxs_auart_exit(void)
790{
791 platform_driver_unregister(&mxs_auart_driver);
792 uart_unregister_driver(&auart_driver);
793}
794
795module_init(mxs_auart_init);
796module_exit(mxs_auart_exit);
797MODULE_LICENSE("GPL");
798MODULE_DESCRIPTION("Freescale MXS application uart driver");
diff --git a/drivers/tty/serial/netx-serial.c b/drivers/tty/serial/netx-serial.c
new file mode 100644
index 000000000000..d40da78e7c85
--- /dev/null
+++ b/drivers/tty/serial/netx-serial.c
@@ -0,0 +1,748 @@
1/*
2 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
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 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#if defined(CONFIG_SERIAL_NETX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
22#include <linux/device.h>
23#include <linux/module.h>
24#include <linux/ioport.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
28#include <linux/platform_device.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <mach/hardware.h>
37#include <mach/netx-regs.h>
38
39/* We've been assigned a range on the "Low-density serial ports" major */
40#define SERIAL_NX_MAJOR 204
41#define MINOR_START 170
42
43enum uart_regs {
44 UART_DR = 0x00,
45 UART_SR = 0x04,
46 UART_LINE_CR = 0x08,
47 UART_BAUDDIV_MSB = 0x0c,
48 UART_BAUDDIV_LSB = 0x10,
49 UART_CR = 0x14,
50 UART_FR = 0x18,
51 UART_IIR = 0x1c,
52 UART_ILPR = 0x20,
53 UART_RTS_CR = 0x24,
54 UART_RTS_LEAD = 0x28,
55 UART_RTS_TRAIL = 0x2c,
56 UART_DRV_ENABLE = 0x30,
57 UART_BRM_CR = 0x34,
58 UART_RXFIFO_IRQLEVEL = 0x38,
59 UART_TXFIFO_IRQLEVEL = 0x3c,
60};
61
62#define SR_FE (1<<0)
63#define SR_PE (1<<1)
64#define SR_BE (1<<2)
65#define SR_OE (1<<3)
66
67#define LINE_CR_BRK (1<<0)
68#define LINE_CR_PEN (1<<1)
69#define LINE_CR_EPS (1<<2)
70#define LINE_CR_STP2 (1<<3)
71#define LINE_CR_FEN (1<<4)
72#define LINE_CR_5BIT (0<<5)
73#define LINE_CR_6BIT (1<<5)
74#define LINE_CR_7BIT (2<<5)
75#define LINE_CR_8BIT (3<<5)
76#define LINE_CR_BITS_MASK (3<<5)
77
78#define CR_UART_EN (1<<0)
79#define CR_SIREN (1<<1)
80#define CR_SIRLP (1<<2)
81#define CR_MSIE (1<<3)
82#define CR_RIE (1<<4)
83#define CR_TIE (1<<5)
84#define CR_RTIE (1<<6)
85#define CR_LBE (1<<7)
86
87#define FR_CTS (1<<0)
88#define FR_DSR (1<<1)
89#define FR_DCD (1<<2)
90#define FR_BUSY (1<<3)
91#define FR_RXFE (1<<4)
92#define FR_TXFF (1<<5)
93#define FR_RXFF (1<<6)
94#define FR_TXFE (1<<7)
95
96#define IIR_MIS (1<<0)
97#define IIR_RIS (1<<1)
98#define IIR_TIS (1<<2)
99#define IIR_RTIS (1<<3)
100#define IIR_MASK 0xf
101
102#define RTS_CR_AUTO (1<<0)
103#define RTS_CR_RTS (1<<1)
104#define RTS_CR_COUNT (1<<2)
105#define RTS_CR_MOD2 (1<<3)
106#define RTS_CR_RTS_POL (1<<4)
107#define RTS_CR_CTS_CTR (1<<5)
108#define RTS_CR_CTS_POL (1<<6)
109#define RTS_CR_STICK (1<<7)
110
111#define UART_PORT_SIZE 0x40
112#define DRIVER_NAME "netx-uart"
113
114struct netx_port {
115 struct uart_port port;
116};
117
118static void netx_stop_tx(struct uart_port *port)
119{
120 unsigned int val;
121 val = readl(port->membase + UART_CR);
122 writel(val & ~CR_TIE, port->membase + UART_CR);
123}
124
125static void netx_stop_rx(struct uart_port *port)
126{
127 unsigned int val;
128 val = readl(port->membase + UART_CR);
129 writel(val & ~CR_RIE, port->membase + UART_CR);
130}
131
132static void netx_enable_ms(struct uart_port *port)
133{
134 unsigned int val;
135 val = readl(port->membase + UART_CR);
136 writel(val | CR_MSIE, port->membase + UART_CR);
137}
138
139static inline void netx_transmit_buffer(struct uart_port *port)
140{
141 struct circ_buf *xmit = &port->state->xmit;
142
143 if (port->x_char) {
144 writel(port->x_char, port->membase + UART_DR);
145 port->icount.tx++;
146 port->x_char = 0;
147 return;
148 }
149
150 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
151 netx_stop_tx(port);
152 return;
153 }
154
155 do {
156 /* send xmit->buf[xmit->tail]
157 * out the port here */
158 writel(xmit->buf[xmit->tail], port->membase + UART_DR);
159 xmit->tail = (xmit->tail + 1) &
160 (UART_XMIT_SIZE - 1);
161 port->icount.tx++;
162 if (uart_circ_empty(xmit))
163 break;
164 } while (!(readl(port->membase + UART_FR) & FR_TXFF));
165
166 if (uart_circ_empty(xmit))
167 netx_stop_tx(port);
168}
169
170static void netx_start_tx(struct uart_port *port)
171{
172 writel(
173 readl(port->membase + UART_CR) | CR_TIE, port->membase + UART_CR);
174
175 if (!(readl(port->membase + UART_FR) & FR_TXFF))
176 netx_transmit_buffer(port);
177}
178
179static unsigned int netx_tx_empty(struct uart_port *port)
180{
181 return readl(port->membase + UART_FR) & FR_BUSY ? 0 : TIOCSER_TEMT;
182}
183
184static void netx_txint(struct uart_port *port)
185{
186 struct circ_buf *xmit = &port->state->xmit;
187
188 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
189 netx_stop_tx(port);
190 return;
191 }
192
193 netx_transmit_buffer(port);
194
195 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
196 uart_write_wakeup(port);
197}
198
199static void netx_rxint(struct uart_port *port)
200{
201 unsigned char rx, flg, status;
202 struct tty_struct *tty = port->state->port.tty;
203
204 while (!(readl(port->membase + UART_FR) & FR_RXFE)) {
205 rx = readl(port->membase + UART_DR);
206 flg = TTY_NORMAL;
207 port->icount.rx++;
208 status = readl(port->membase + UART_SR);
209 if (status & SR_BE) {
210 writel(0, port->membase + UART_SR);
211 if (uart_handle_break(port))
212 continue;
213 }
214
215 if (unlikely(status & (SR_FE | SR_PE | SR_OE))) {
216
217 if (status & SR_PE)
218 port->icount.parity++;
219 else if (status & SR_FE)
220 port->icount.frame++;
221 if (status & SR_OE)
222 port->icount.overrun++;
223
224 status &= port->read_status_mask;
225
226 if (status & SR_BE)
227 flg = TTY_BREAK;
228 else if (status & SR_PE)
229 flg = TTY_PARITY;
230 else if (status & SR_FE)
231 flg = TTY_FRAME;
232 }
233
234 if (uart_handle_sysrq_char(port, rx))
235 continue;
236
237 uart_insert_char(port, status, SR_OE, rx, flg);
238 }
239
240 tty_flip_buffer_push(tty);
241 return;
242}
243
244static irqreturn_t netx_int(int irq, void *dev_id)
245{
246 struct uart_port *port = dev_id;
247 unsigned long flags;
248 unsigned char status;
249
250 spin_lock_irqsave(&port->lock,flags);
251
252 status = readl(port->membase + UART_IIR) & IIR_MASK;
253 while (status) {
254 if (status & IIR_RIS)
255 netx_rxint(port);
256 if (status & IIR_TIS)
257 netx_txint(port);
258 if (status & IIR_MIS) {
259 if (readl(port->membase + UART_FR) & FR_CTS)
260 uart_handle_cts_change(port, 1);
261 else
262 uart_handle_cts_change(port, 0);
263 }
264 writel(0, port->membase + UART_IIR);
265 status = readl(port->membase + UART_IIR) & IIR_MASK;
266 }
267
268 spin_unlock_irqrestore(&port->lock,flags);
269 return IRQ_HANDLED;
270}
271
272static unsigned int netx_get_mctrl(struct uart_port *port)
273{
274 unsigned int ret = TIOCM_DSR | TIOCM_CAR;
275
276 if (readl(port->membase + UART_FR) & FR_CTS)
277 ret |= TIOCM_CTS;
278
279 return ret;
280}
281
282static void netx_set_mctrl(struct uart_port *port, unsigned int mctrl)
283{
284 unsigned int val;
285
286 /* FIXME: Locking needed ? */
287 if (mctrl & TIOCM_RTS) {
288 val = readl(port->membase + UART_RTS_CR);
289 writel(val | RTS_CR_RTS, port->membase + UART_RTS_CR);
290 }
291}
292
293static void netx_break_ctl(struct uart_port *port, int break_state)
294{
295 unsigned int line_cr;
296 spin_lock_irq(&port->lock);
297
298 line_cr = readl(port->membase + UART_LINE_CR);
299 if (break_state != 0)
300 line_cr |= LINE_CR_BRK;
301 else
302 line_cr &= ~LINE_CR_BRK;
303 writel(line_cr, port->membase + UART_LINE_CR);
304
305 spin_unlock_irq(&port->lock);
306}
307
308static int netx_startup(struct uart_port *port)
309{
310 int ret;
311
312 ret = request_irq(port->irq, netx_int, 0,
313 DRIVER_NAME, port);
314 if (ret) {
315 dev_err(port->dev, "unable to grab irq%d\n",port->irq);
316 goto exit;
317 }
318
319 writel(readl(port->membase + UART_LINE_CR) | LINE_CR_FEN,
320 port->membase + UART_LINE_CR);
321
322 writel(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE | CR_UART_EN,
323 port->membase + UART_CR);
324
325exit:
326 return ret;
327}
328
329static void netx_shutdown(struct uart_port *port)
330{
331 writel(0, port->membase + UART_CR) ;
332
333 free_irq(port->irq, port);
334}
335
336static void
337netx_set_termios(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old)
339{
340 unsigned int baud, quot;
341 unsigned char old_cr;
342 unsigned char line_cr = LINE_CR_FEN;
343 unsigned char rts_cr = 0;
344
345 switch (termios->c_cflag & CSIZE) {
346 case CS5:
347 line_cr |= LINE_CR_5BIT;
348 break;
349 case CS6:
350 line_cr |= LINE_CR_6BIT;
351 break;
352 case CS7:
353 line_cr |= LINE_CR_7BIT;
354 break;
355 case CS8:
356 line_cr |= LINE_CR_8BIT;
357 break;
358 }
359
360 if (termios->c_cflag & CSTOPB)
361 line_cr |= LINE_CR_STP2;
362
363 if (termios->c_cflag & PARENB) {
364 line_cr |= LINE_CR_PEN;
365 if (!(termios->c_cflag & PARODD))
366 line_cr |= LINE_CR_EPS;
367 }
368
369 if (termios->c_cflag & CRTSCTS)
370 rts_cr = RTS_CR_AUTO | RTS_CR_CTS_CTR | RTS_CR_RTS_POL;
371
372 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
373 quot = baud * 4096;
374 quot /= 1000;
375 quot *= 256;
376 quot /= 100000;
377
378 spin_lock_irq(&port->lock);
379
380 uart_update_timeout(port, termios->c_cflag, baud);
381
382 old_cr = readl(port->membase + UART_CR);
383
384 /* disable interrupts */
385 writel(old_cr & ~(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE),
386 port->membase + UART_CR);
387
388 /* drain transmitter */
389 while (readl(port->membase + UART_FR) & FR_BUSY);
390
391 /* disable UART */
392 writel(old_cr & ~CR_UART_EN, port->membase + UART_CR);
393
394 /* modem status interrupts */
395 old_cr &= ~CR_MSIE;
396 if (UART_ENABLE_MS(port, termios->c_cflag))
397 old_cr |= CR_MSIE;
398
399 writel((quot>>8) & 0xff, port->membase + UART_BAUDDIV_MSB);
400 writel(quot & 0xff, port->membase + UART_BAUDDIV_LSB);
401 writel(line_cr, port->membase + UART_LINE_CR);
402
403 writel(rts_cr, port->membase + UART_RTS_CR);
404
405 /*
406 * Characters to ignore
407 */
408 port->ignore_status_mask = 0;
409 if (termios->c_iflag & IGNPAR)
410 port->ignore_status_mask |= SR_PE;
411 if (termios->c_iflag & IGNBRK) {
412 port->ignore_status_mask |= SR_BE;
413 /*
414 * If we're ignoring parity and break indicators,
415 * ignore overruns too (for real raw support).
416 */
417 if (termios->c_iflag & IGNPAR)
418 port->ignore_status_mask |= SR_PE;
419 }
420
421 port->read_status_mask = 0;
422 if (termios->c_iflag & (BRKINT | PARMRK))
423 port->read_status_mask |= SR_BE;
424 if (termios->c_iflag & INPCK)
425 port->read_status_mask |= SR_PE | SR_FE;
426
427 writel(old_cr, port->membase + UART_CR);
428
429 spin_unlock_irq(&port->lock);
430}
431
432static const char *netx_type(struct uart_port *port)
433{
434 return port->type == PORT_NETX ? "NETX" : NULL;
435}
436
437static void netx_release_port(struct uart_port *port)
438{
439 release_mem_region(port->mapbase, UART_PORT_SIZE);
440}
441
442static int netx_request_port(struct uart_port *port)
443{
444 return request_mem_region(port->mapbase, UART_PORT_SIZE,
445 DRIVER_NAME) != NULL ? 0 : -EBUSY;
446}
447
448static void netx_config_port(struct uart_port *port, int flags)
449{
450 if (flags & UART_CONFIG_TYPE && netx_request_port(port) == 0)
451 port->type = PORT_NETX;
452}
453
454static int
455netx_verify_port(struct uart_port *port, struct serial_struct *ser)
456{
457 int ret = 0;
458
459 if (ser->type != PORT_UNKNOWN && ser->type != PORT_NETX)
460 ret = -EINVAL;
461
462 return ret;
463}
464
465static struct uart_ops netx_pops = {
466 .tx_empty = netx_tx_empty,
467 .set_mctrl = netx_set_mctrl,
468 .get_mctrl = netx_get_mctrl,
469 .stop_tx = netx_stop_tx,
470 .start_tx = netx_start_tx,
471 .stop_rx = netx_stop_rx,
472 .enable_ms = netx_enable_ms,
473 .break_ctl = netx_break_ctl,
474 .startup = netx_startup,
475 .shutdown = netx_shutdown,
476 .set_termios = netx_set_termios,
477 .type = netx_type,
478 .release_port = netx_release_port,
479 .request_port = netx_request_port,
480 .config_port = netx_config_port,
481 .verify_port = netx_verify_port,
482};
483
484static struct netx_port netx_ports[] = {
485 {
486 .port = {
487 .type = PORT_NETX,
488 .iotype = UPIO_MEM,
489 .membase = (char __iomem *)io_p2v(NETX_PA_UART0),
490 .mapbase = NETX_PA_UART0,
491 .irq = NETX_IRQ_UART0,
492 .uartclk = 100000000,
493 .fifosize = 16,
494 .flags = UPF_BOOT_AUTOCONF,
495 .ops = &netx_pops,
496 .line = 0,
497 },
498 }, {
499 .port = {
500 .type = PORT_NETX,
501 .iotype = UPIO_MEM,
502 .membase = (char __iomem *)io_p2v(NETX_PA_UART1),
503 .mapbase = NETX_PA_UART1,
504 .irq = NETX_IRQ_UART1,
505 .uartclk = 100000000,
506 .fifosize = 16,
507 .flags = UPF_BOOT_AUTOCONF,
508 .ops = &netx_pops,
509 .line = 1,
510 },
511 }, {
512 .port = {
513 .type = PORT_NETX,
514 .iotype = UPIO_MEM,
515 .membase = (char __iomem *)io_p2v(NETX_PA_UART2),
516 .mapbase = NETX_PA_UART2,
517 .irq = NETX_IRQ_UART2,
518 .uartclk = 100000000,
519 .fifosize = 16,
520 .flags = UPF_BOOT_AUTOCONF,
521 .ops = &netx_pops,
522 .line = 2,
523 },
524 }
525};
526
527#ifdef CONFIG_SERIAL_NETX_CONSOLE
528
529static void netx_console_putchar(struct uart_port *port, int ch)
530{
531 while (readl(port->membase + UART_FR) & FR_BUSY);
532 writel(ch, port->membase + UART_DR);
533}
534
535static void
536netx_console_write(struct console *co, const char *s, unsigned int count)
537{
538 struct uart_port *port = &netx_ports[co->index].port;
539 unsigned char cr_save;
540
541 cr_save = readl(port->membase + UART_CR);
542 writel(cr_save | CR_UART_EN, port->membase + UART_CR);
543
544 uart_console_write(port, s, count, netx_console_putchar);
545
546 while (readl(port->membase + UART_FR) & FR_BUSY);
547 writel(cr_save, port->membase + UART_CR);
548}
549
550static void __init
551netx_console_get_options(struct uart_port *port, int *baud,
552 int *parity, int *bits, int *flow)
553{
554 unsigned char line_cr;
555
556 *baud = (readl(port->membase + UART_BAUDDIV_MSB) << 8) |
557 readl(port->membase + UART_BAUDDIV_LSB);
558 *baud *= 1000;
559 *baud /= 4096;
560 *baud *= 1000;
561 *baud /= 256;
562 *baud *= 100;
563
564 line_cr = readl(port->membase + UART_LINE_CR);
565 *parity = 'n';
566 if (line_cr & LINE_CR_PEN) {
567 if (line_cr & LINE_CR_EPS)
568 *parity = 'e';
569 else
570 *parity = 'o';
571 }
572
573 switch (line_cr & LINE_CR_BITS_MASK) {
574 case LINE_CR_8BIT:
575 *bits = 8;
576 break;
577 case LINE_CR_7BIT:
578 *bits = 7;
579 break;
580 case LINE_CR_6BIT:
581 *bits = 6;
582 break;
583 case LINE_CR_5BIT:
584 *bits = 5;
585 break;
586 }
587
588 if (readl(port->membase + UART_RTS_CR) & RTS_CR_AUTO)
589 *flow = 'r';
590}
591
592static int __init
593netx_console_setup(struct console *co, char *options)
594{
595 struct netx_port *sport;
596 int baud = 9600;
597 int bits = 8;
598 int parity = 'n';
599 int flow = 'n';
600
601 /*
602 * Check whether an invalid uart number has been specified, and
603 * if so, search for the first available port that does have
604 * console support.
605 */
606 if (co->index == -1 || co->index >= ARRAY_SIZE(netx_ports))
607 co->index = 0;
608 sport = &netx_ports[co->index];
609
610 if (options) {
611 uart_parse_options(options, &baud, &parity, &bits, &flow);
612 } else {
613 /* if the UART is enabled, assume it has been correctly setup
614 * by the bootloader and get the options
615 */
616 if (readl(sport->port.membase + UART_CR) & CR_UART_EN) {
617 netx_console_get_options(&sport->port, &baud,
618 &parity, &bits, &flow);
619 }
620
621 }
622
623 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
624}
625
626static struct uart_driver netx_reg;
627static struct console netx_console = {
628 .name = "ttyNX",
629 .write = netx_console_write,
630 .device = uart_console_device,
631 .setup = netx_console_setup,
632 .flags = CON_PRINTBUFFER,
633 .index = -1,
634 .data = &netx_reg,
635};
636
637static int __init netx_console_init(void)
638{
639 register_console(&netx_console);
640 return 0;
641}
642console_initcall(netx_console_init);
643
644#define NETX_CONSOLE &netx_console
645#else
646#define NETX_CONSOLE NULL
647#endif
648
649static struct uart_driver netx_reg = {
650 .owner = THIS_MODULE,
651 .driver_name = DRIVER_NAME,
652 .dev_name = "ttyNX",
653 .major = SERIAL_NX_MAJOR,
654 .minor = MINOR_START,
655 .nr = ARRAY_SIZE(netx_ports),
656 .cons = NETX_CONSOLE,
657};
658
659static int serial_netx_suspend(struct platform_device *pdev, pm_message_t state)
660{
661 struct netx_port *sport = platform_get_drvdata(pdev);
662
663 if (sport)
664 uart_suspend_port(&netx_reg, &sport->port);
665
666 return 0;
667}
668
669static int serial_netx_resume(struct platform_device *pdev)
670{
671 struct netx_port *sport = platform_get_drvdata(pdev);
672
673 if (sport)
674 uart_resume_port(&netx_reg, &sport->port);
675
676 return 0;
677}
678
679static int serial_netx_probe(struct platform_device *pdev)
680{
681 struct uart_port *port = &netx_ports[pdev->id].port;
682
683 dev_info(&pdev->dev, "initialising\n");
684
685 port->dev = &pdev->dev;
686
687 writel(1, port->membase + UART_RXFIFO_IRQLEVEL);
688 uart_add_one_port(&netx_reg, &netx_ports[pdev->id].port);
689 platform_set_drvdata(pdev, &netx_ports[pdev->id]);
690
691 return 0;
692}
693
694static int serial_netx_remove(struct platform_device *pdev)
695{
696 struct netx_port *sport = platform_get_drvdata(pdev);
697
698 platform_set_drvdata(pdev, NULL);
699
700 if (sport)
701 uart_remove_one_port(&netx_reg, &sport->port);
702
703 return 0;
704}
705
706static struct platform_driver serial_netx_driver = {
707 .probe = serial_netx_probe,
708 .remove = serial_netx_remove,
709
710 .suspend = serial_netx_suspend,
711 .resume = serial_netx_resume,
712
713 .driver = {
714 .name = DRIVER_NAME,
715 .owner = THIS_MODULE,
716 },
717};
718
719static int __init netx_serial_init(void)
720{
721 int ret;
722
723 printk(KERN_INFO "Serial: NetX driver\n");
724
725 ret = uart_register_driver(&netx_reg);
726 if (ret)
727 return ret;
728
729 ret = platform_driver_register(&serial_netx_driver);
730 if (ret != 0)
731 uart_unregister_driver(&netx_reg);
732
733 return 0;
734}
735
736static void __exit netx_serial_exit(void)
737{
738 platform_driver_unregister(&serial_netx_driver);
739 uart_unregister_driver(&netx_reg);
740}
741
742module_init(netx_serial_init);
743module_exit(netx_serial_exit);
744
745MODULE_AUTHOR("Sascha Hauer");
746MODULE_DESCRIPTION("NetX serial port driver");
747MODULE_LICENSE("GPL");
748MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/tty/serial/nwpserial.c b/drivers/tty/serial/nwpserial.c
new file mode 100644
index 000000000000..de173671e3d0
--- /dev/null
+++ b/drivers/tty/serial/nwpserial.c
@@ -0,0 +1,477 @@
1/*
2 * Serial Port driver for a NWP uart device
3 *
4 * Copyright (C) 2008 IBM Corp., Benjamin Krill <ben@codiert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <linux/init.h>
13#include <linux/console.h>
14#include <linux/serial.h>
15#include <linux/serial_reg.h>
16#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/irqreturn.h>
19#include <linux/mutex.h>
20#include <linux/of_platform.h>
21#include <linux/of_device.h>
22#include <linux/nwpserial.h>
23#include <asm/prom.h>
24#include <asm/dcr.h>
25
26#define NWPSERIAL_NR 2
27
28#define NWPSERIAL_STATUS_RXVALID 0x1
29#define NWPSERIAL_STATUS_TXFULL 0x2
30
31struct nwpserial_port {
32 struct uart_port port;
33 dcr_host_t dcr_host;
34 unsigned int ier;
35 unsigned int mcr;
36};
37
38static DEFINE_MUTEX(nwpserial_mutex);
39static struct nwpserial_port nwpserial_ports[NWPSERIAL_NR];
40
41static void wait_for_bits(struct nwpserial_port *up, int bits)
42{
43 unsigned int status, tmout = 10000;
44
45 /* Wait up to 10ms for the character(s) to be sent. */
46 do {
47 status = dcr_read(up->dcr_host, UART_LSR);
48
49 if (--tmout == 0)
50 break;
51 udelay(1);
52 } while ((status & bits) != bits);
53}
54
55#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
56static void nwpserial_console_putchar(struct uart_port *port, int c)
57{
58 struct nwpserial_port *up;
59 up = container_of(port, struct nwpserial_port, port);
60 /* check if tx buffer is full */
61 wait_for_bits(up, UART_LSR_THRE);
62 dcr_write(up->dcr_host, UART_TX, c);
63 up->port.icount.tx++;
64}
65
66static void
67nwpserial_console_write(struct console *co, const char *s, unsigned int count)
68{
69 struct nwpserial_port *up = &nwpserial_ports[co->index];
70 unsigned long flags;
71 int locked = 1;
72
73 if (oops_in_progress)
74 locked = spin_trylock_irqsave(&up->port.lock, flags);
75 else
76 spin_lock_irqsave(&up->port.lock, flags);
77
78 /* save and disable interrupt */
79 up->ier = dcr_read(up->dcr_host, UART_IER);
80 dcr_write(up->dcr_host, UART_IER, up->ier & ~UART_IER_RDI);
81
82 uart_console_write(&up->port, s, count, nwpserial_console_putchar);
83
84 /* wait for transmitter to become empty */
85 while ((dcr_read(up->dcr_host, UART_LSR) & UART_LSR_THRE) == 0)
86 cpu_relax();
87
88 /* restore interrupt state */
89 dcr_write(up->dcr_host, UART_IER, up->ier);
90
91 if (locked)
92 spin_unlock_irqrestore(&up->port.lock, flags);
93}
94
95static struct uart_driver nwpserial_reg;
96static struct console nwpserial_console = {
97 .name = "ttySQ",
98 .write = nwpserial_console_write,
99 .device = uart_console_device,
100 .flags = CON_PRINTBUFFER,
101 .index = -1,
102 .data = &nwpserial_reg,
103};
104#define NWPSERIAL_CONSOLE (&nwpserial_console)
105#else
106#define NWPSERIAL_CONSOLE NULL
107#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
108
109/**************************************************************************/
110
111static int nwpserial_request_port(struct uart_port *port)
112{
113 return 0;
114}
115
116static void nwpserial_release_port(struct uart_port *port)
117{
118 /* N/A */
119}
120
121static void nwpserial_config_port(struct uart_port *port, int flags)
122{
123 port->type = PORT_NWPSERIAL;
124}
125
126static irqreturn_t nwpserial_interrupt(int irq, void *dev_id)
127{
128 struct nwpserial_port *up = dev_id;
129 struct tty_struct *tty = up->port.state->port.tty;
130 irqreturn_t ret;
131 unsigned int iir;
132 unsigned char ch;
133
134 spin_lock(&up->port.lock);
135
136 /* check if the uart was the interrupt source. */
137 iir = dcr_read(up->dcr_host, UART_IIR);
138 if (!iir) {
139 ret = IRQ_NONE;
140 goto out;
141 }
142
143 do {
144 up->port.icount.rx++;
145 ch = dcr_read(up->dcr_host, UART_RX);
146 if (up->port.ignore_status_mask != NWPSERIAL_STATUS_RXVALID)
147 tty_insert_flip_char(tty, ch, TTY_NORMAL);
148 } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR);
149
150 tty_flip_buffer_push(tty);
151 ret = IRQ_HANDLED;
152
153 /* clear interrupt */
154 dcr_write(up->dcr_host, UART_IIR, 1);
155out:
156 spin_unlock(&up->port.lock);
157 return ret;
158}
159
160static int nwpserial_startup(struct uart_port *port)
161{
162 struct nwpserial_port *up;
163 int err;
164
165 up = container_of(port, struct nwpserial_port, port);
166
167 /* disable flow control by default */
168 up->mcr = dcr_read(up->dcr_host, UART_MCR) & ~UART_MCR_AFE;
169 dcr_write(up->dcr_host, UART_MCR, up->mcr);
170
171 /* register interrupt handler */
172 err = request_irq(up->port.irq, nwpserial_interrupt,
173 IRQF_SHARED, "nwpserial", up);
174 if (err)
175 return err;
176
177 /* enable interrupts */
178 up->ier = UART_IER_RDI;
179 dcr_write(up->dcr_host, UART_IER, up->ier);
180
181 /* enable receiving */
182 up->port.ignore_status_mask &= ~NWPSERIAL_STATUS_RXVALID;
183
184 return 0;
185}
186
187static void nwpserial_shutdown(struct uart_port *port)
188{
189 struct nwpserial_port *up;
190 up = container_of(port, struct nwpserial_port, port);
191
192 /* disable receiving */
193 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
194
195 /* disable interrupts from this port */
196 up->ier = 0;
197 dcr_write(up->dcr_host, UART_IER, up->ier);
198
199 /* free irq */
200 free_irq(up->port.irq, port);
201}
202
203static int nwpserial_verify_port(struct uart_port *port,
204 struct serial_struct *ser)
205{
206 return -EINVAL;
207}
208
209static const char *nwpserial_type(struct uart_port *port)
210{
211 return port->type == PORT_NWPSERIAL ? "nwpserial" : NULL;
212}
213
214static void nwpserial_set_termios(struct uart_port *port,
215 struct ktermios *termios, struct ktermios *old)
216{
217 struct nwpserial_port *up;
218 up = container_of(port, struct nwpserial_port, port);
219
220 up->port.read_status_mask = NWPSERIAL_STATUS_RXVALID
221 | NWPSERIAL_STATUS_TXFULL;
222
223 up->port.ignore_status_mask = 0;
224 /* ignore all characters if CREAD is not set */
225 if ((termios->c_cflag & CREAD) == 0)
226 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
227
228 /* Copy back the old hardware settings */
229 if (old)
230 tty_termios_copy_hw(termios, old);
231}
232
233static void nwpserial_break_ctl(struct uart_port *port, int ctl)
234{
235 /* N/A */
236}
237
238static void nwpserial_enable_ms(struct uart_port *port)
239{
240 /* N/A */
241}
242
243static void nwpserial_stop_rx(struct uart_port *port)
244{
245 struct nwpserial_port *up;
246 up = container_of(port, struct nwpserial_port, port);
247 /* don't forward any more data (like !CREAD) */
248 up->port.ignore_status_mask = NWPSERIAL_STATUS_RXVALID;
249}
250
251static void nwpserial_putchar(struct nwpserial_port *up, unsigned char c)
252{
253 /* check if tx buffer is full */
254 wait_for_bits(up, UART_LSR_THRE);
255 dcr_write(up->dcr_host, UART_TX, c);
256 up->port.icount.tx++;
257}
258
259static void nwpserial_start_tx(struct uart_port *port)
260{
261 struct nwpserial_port *up;
262 struct circ_buf *xmit;
263 up = container_of(port, struct nwpserial_port, port);
264 xmit = &up->port.state->xmit;
265
266 if (port->x_char) {
267 nwpserial_putchar(up, up->port.x_char);
268 port->x_char = 0;
269 }
270
271 while (!(uart_circ_empty(xmit) || uart_tx_stopped(&up->port))) {
272 nwpserial_putchar(up, xmit->buf[xmit->tail]);
273 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
274 }
275}
276
277static unsigned int nwpserial_get_mctrl(struct uart_port *port)
278{
279 return 0;
280}
281
282static void nwpserial_set_mctrl(struct uart_port *port, unsigned int mctrl)
283{
284 /* N/A */
285}
286
287static void nwpserial_stop_tx(struct uart_port *port)
288{
289 /* N/A */
290}
291
292static unsigned int nwpserial_tx_empty(struct uart_port *port)
293{
294 struct nwpserial_port *up;
295 unsigned long flags;
296 int ret;
297 up = container_of(port, struct nwpserial_port, port);
298
299 spin_lock_irqsave(&up->port.lock, flags);
300 ret = dcr_read(up->dcr_host, UART_LSR);
301 spin_unlock_irqrestore(&up->port.lock, flags);
302
303 return ret & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
304}
305
306static struct uart_ops nwpserial_pops = {
307 .tx_empty = nwpserial_tx_empty,
308 .set_mctrl = nwpserial_set_mctrl,
309 .get_mctrl = nwpserial_get_mctrl,
310 .stop_tx = nwpserial_stop_tx,
311 .start_tx = nwpserial_start_tx,
312 .stop_rx = nwpserial_stop_rx,
313 .enable_ms = nwpserial_enable_ms,
314 .break_ctl = nwpserial_break_ctl,
315 .startup = nwpserial_startup,
316 .shutdown = nwpserial_shutdown,
317 .set_termios = nwpserial_set_termios,
318 .type = nwpserial_type,
319 .release_port = nwpserial_release_port,
320 .request_port = nwpserial_request_port,
321 .config_port = nwpserial_config_port,
322 .verify_port = nwpserial_verify_port,
323};
324
325static struct uart_driver nwpserial_reg = {
326 .owner = THIS_MODULE,
327 .driver_name = "nwpserial",
328 .dev_name = "ttySQ",
329 .major = TTY_MAJOR,
330 .minor = 68,
331 .nr = NWPSERIAL_NR,
332 .cons = NWPSERIAL_CONSOLE,
333};
334
335int nwpserial_register_port(struct uart_port *port)
336{
337 struct nwpserial_port *up = NULL;
338 int ret = -1;
339 int i;
340 static int first = 1;
341 int dcr_len;
342 int dcr_base;
343 struct device_node *dn;
344
345 mutex_lock(&nwpserial_mutex);
346
347 dn = port->dev->of_node;
348 if (dn == NULL)
349 goto out;
350
351 /* get dcr base. */
352 dcr_base = dcr_resource_start(dn, 0);
353
354 /* find matching entry */
355 for (i = 0; i < NWPSERIAL_NR; i++)
356 if (nwpserial_ports[i].port.iobase == dcr_base) {
357 up = &nwpserial_ports[i];
358 break;
359 }
360
361 /* we didn't find a mtching entry, search for a free port */
362 if (up == NULL)
363 for (i = 0; i < NWPSERIAL_NR; i++)
364 if (nwpserial_ports[i].port.type == PORT_UNKNOWN &&
365 nwpserial_ports[i].port.iobase == 0) {
366 up = &nwpserial_ports[i];
367 break;
368 }
369
370 if (up == NULL) {
371 ret = -EBUSY;
372 goto out;
373 }
374
375 if (first)
376 uart_register_driver(&nwpserial_reg);
377 first = 0;
378
379 up->port.membase = port->membase;
380 up->port.irq = port->irq;
381 up->port.uartclk = port->uartclk;
382 up->port.fifosize = port->fifosize;
383 up->port.regshift = port->regshift;
384 up->port.iotype = port->iotype;
385 up->port.flags = port->flags;
386 up->port.mapbase = port->mapbase;
387 up->port.private_data = port->private_data;
388
389 if (port->dev)
390 up->port.dev = port->dev;
391
392 if (up->port.iobase != dcr_base) {
393 up->port.ops = &nwpserial_pops;
394 up->port.fifosize = 16;
395
396 spin_lock_init(&up->port.lock);
397
398 up->port.iobase = dcr_base;
399 dcr_len = dcr_resource_len(dn, 0);
400
401 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
402 if (!DCR_MAP_OK(up->dcr_host)) {
403 printk(KERN_ERR "Cannot map DCR resources for NWPSERIAL");
404 goto out;
405 }
406 }
407
408 ret = uart_add_one_port(&nwpserial_reg, &up->port);
409 if (ret == 0)
410 ret = up->port.line;
411
412out:
413 mutex_unlock(&nwpserial_mutex);
414
415 return ret;
416}
417EXPORT_SYMBOL(nwpserial_register_port);
418
419void nwpserial_unregister_port(int line)
420{
421 struct nwpserial_port *up = &nwpserial_ports[line];
422 mutex_lock(&nwpserial_mutex);
423 uart_remove_one_port(&nwpserial_reg, &up->port);
424
425 up->port.type = PORT_UNKNOWN;
426
427 mutex_unlock(&nwpserial_mutex);
428}
429EXPORT_SYMBOL(nwpserial_unregister_port);
430
431#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
432static int __init nwpserial_console_init(void)
433{
434 struct nwpserial_port *up = NULL;
435 struct device_node *dn;
436 const char *name;
437 int dcr_base;
438 int dcr_len;
439 int i;
440
441 /* search for a free port */
442 for (i = 0; i < NWPSERIAL_NR; i++)
443 if (nwpserial_ports[i].port.type == PORT_UNKNOWN) {
444 up = &nwpserial_ports[i];
445 break;
446 }
447
448 if (up == NULL)
449 return -1;
450
451 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
452 if (name == NULL)
453 return -1;
454
455 dn = of_find_node_by_path(name);
456 if (!dn)
457 return -1;
458
459 spin_lock_init(&up->port.lock);
460 up->port.ops = &nwpserial_pops;
461 up->port.type = PORT_NWPSERIAL;
462 up->port.fifosize = 16;
463
464 dcr_base = dcr_resource_start(dn, 0);
465 dcr_len = dcr_resource_len(dn, 0);
466 up->port.iobase = dcr_base;
467
468 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
469 if (!DCR_MAP_OK(up->dcr_host)) {
470 printk("Cannot map DCR resources for SERIAL");
471 return -1;
472 }
473 register_console(&nwpserial_console);
474 return 0;
475}
476console_initcall(nwpserial_console_init);
477#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
diff --git a/drivers/tty/serial/of_serial.c b/drivers/tty/serial/of_serial.c
new file mode 100644
index 000000000000..c911b2419abb
--- /dev/null
+++ b/drivers/tty/serial/of_serial.c
@@ -0,0 +1,206 @@
1/*
2 * Serial Port driver for Open Firmware platform devices
3 *
4 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/serial_core.h>
16#include <linux/serial_8250.h>
17#include <linux/of_address.h>
18#include <linux/of_irq.h>
19#include <linux/of_platform.h>
20#include <linux/nwpserial.h>
21
22struct of_serial_info {
23 int type;
24 int line;
25};
26
27/*
28 * Fill a struct uart_port for a given device node
29 */
30static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
31 int type, struct uart_port *port)
32{
33 struct resource resource;
34 struct device_node *np = ofdev->dev.of_node;
35 const __be32 *clk, *spd;
36 const __be32 *prop;
37 int ret, prop_size;
38
39 memset(port, 0, sizeof *port);
40 spd = of_get_property(np, "current-speed", NULL);
41 clk = of_get_property(np, "clock-frequency", NULL);
42 if (!clk) {
43 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
44 return -ENODEV;
45 }
46
47 ret = of_address_to_resource(np, 0, &resource);
48 if (ret) {
49 dev_warn(&ofdev->dev, "invalid address\n");
50 return ret;
51 }
52
53 spin_lock_init(&port->lock);
54 port->mapbase = resource.start;
55
56 /* Check for shifted address mapping */
57 prop = of_get_property(np, "reg-offset", &prop_size);
58 if (prop && (prop_size == sizeof(u32)))
59 port->mapbase += be32_to_cpup(prop);
60
61 /* Check for registers offset within the devices address range */
62 prop = of_get_property(np, "reg-shift", &prop_size);
63 if (prop && (prop_size == sizeof(u32)))
64 port->regshift = be32_to_cpup(prop);
65
66 port->irq = irq_of_parse_and_map(np, 0);
67 port->iotype = UPIO_MEM;
68 port->type = type;
69 port->uartclk = be32_to_cpup(clk);
70 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
71 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
72 port->dev = &ofdev->dev;
73 /* If current-speed was set, then try not to change it. */
74 if (spd)
75 port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd)));
76
77 return 0;
78}
79
80/*
81 * Try to register a serial port
82 */
83static struct of_device_id of_platform_serial_table[];
84static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
85{
86 const struct of_device_id *match;
87 struct of_serial_info *info;
88 struct uart_port port;
89 int port_type;
90 int ret;
91
92 match = of_match_device(of_platform_serial_table, &ofdev->dev);
93 if (!match)
94 return -EINVAL;
95
96 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
97 return -EBUSY;
98
99 info = kmalloc(sizeof(*info), GFP_KERNEL);
100 if (info == NULL)
101 return -ENOMEM;
102
103 port_type = (unsigned long)match->data;
104 ret = of_platform_serial_setup(ofdev, port_type, &port);
105 if (ret)
106 goto out;
107
108 switch (port_type) {
109#ifdef CONFIG_SERIAL_8250
110 case PORT_8250 ... PORT_MAX_8250:
111 ret = serial8250_register_port(&port);
112 break;
113#endif
114#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
115 case PORT_NWPSERIAL:
116 ret = nwpserial_register_port(&port);
117 break;
118#endif
119 default:
120 /* need to add code for these */
121 case PORT_UNKNOWN:
122 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
123 ret = -ENODEV;
124 break;
125 }
126 if (ret < 0)
127 goto out;
128
129 info->type = port_type;
130 info->line = ret;
131 dev_set_drvdata(&ofdev->dev, info);
132 return 0;
133out:
134 kfree(info);
135 irq_dispose_mapping(port.irq);
136 return ret;
137}
138
139/*
140 * Release a line
141 */
142static int of_platform_serial_remove(struct platform_device *ofdev)
143{
144 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
145 switch (info->type) {
146#ifdef CONFIG_SERIAL_8250
147 case PORT_8250 ... PORT_MAX_8250:
148 serial8250_unregister_port(info->line);
149 break;
150#endif
151#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
152 case PORT_NWPSERIAL:
153 nwpserial_unregister_port(info->line);
154 break;
155#endif
156 default:
157 /* need to add code for these */
158 break;
159 }
160 kfree(info);
161 return 0;
162}
163
164/*
165 * A few common types, add more as needed.
166 */
167static struct of_device_id __devinitdata of_platform_serial_table[] = {
168 { .compatible = "ns8250", .data = (void *)PORT_8250, },
169 { .compatible = "ns16450", .data = (void *)PORT_16450, },
170 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
171 { .compatible = "ns16550", .data = (void *)PORT_16550, },
172 { .compatible = "ns16750", .data = (void *)PORT_16750, },
173 { .compatible = "ns16850", .data = (void *)PORT_16850, },
174#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
175 { .compatible = "ibm,qpace-nwp-serial",
176 .data = (void *)PORT_NWPSERIAL, },
177#endif
178 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
179 { /* end of list */ },
180};
181
182static struct platform_driver of_platform_serial_driver = {
183 .driver = {
184 .name = "of_serial",
185 .owner = THIS_MODULE,
186 .of_match_table = of_platform_serial_table,
187 },
188 .probe = of_platform_serial_probe,
189 .remove = of_platform_serial_remove,
190};
191
192static int __init of_platform_serial_init(void)
193{
194 return platform_driver_register(&of_platform_serial_driver);
195}
196module_init(of_platform_serial_init);
197
198static void __exit of_platform_serial_exit(void)
199{
200 return platform_driver_unregister(&of_platform_serial_driver);
201};
202module_exit(of_platform_serial_exit);
203
204MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
205MODULE_LICENSE("GPL");
206MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");
diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
new file mode 100644
index 000000000000..47cadf474149
--- /dev/null
+++ b/drivers/tty/serial/omap-serial.c
@@ -0,0 +1,1362 @@
1/*
2 * Driver for OMAP-UART controller.
3 * Based on drivers/serial/8250.c
4 *
5 * Copyright (C) 2010 Texas Instruments.
6 *
7 * Authors:
8 * Govindraj R <govindraj.raja@ti.com>
9 * Thara Gopinath <thara@ti.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Note: This driver is made separate from 8250 driver as we cannot
17 * over load 8250 driver with omap platform specific configuration for
18 * features like DMA, it makes easier to implement features like DMA and
19 * hardware flow control and software flow control configuration with
20 * this driver as required for the omap-platform.
21 */
22
23#if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/serial_reg.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/io.h>
36#include <linux/dma-mapping.h>
37#include <linux/clk.h>
38#include <linux/serial_core.h>
39#include <linux/irq.h>
40
41#include <plat/dma.h>
42#include <plat/dmtimer.h>
43#include <plat/omap-serial.h>
44
45static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
46
47/* Forward declaration of functions */
48static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
49static void serial_omap_rx_timeout(unsigned long uart_no);
50static int serial_omap_start_rxdma(struct uart_omap_port *up);
51
52static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
53{
54 offset <<= up->port.regshift;
55 return readw(up->port.membase + offset);
56}
57
58static inline void serial_out(struct uart_omap_port *up, int offset, int value)
59{
60 offset <<= up->port.regshift;
61 writew(value, up->port.membase + offset);
62}
63
64static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
65{
66 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
67 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
68 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
69 serial_out(up, UART_FCR, 0);
70}
71
72/*
73 * serial_omap_get_divisor - calculate divisor value
74 * @port: uart port info
75 * @baud: baudrate for which divisor needs to be calculated.
76 *
77 * We have written our own function to get the divisor so as to support
78 * 13x mode. 3Mbps Baudrate as an different divisor.
79 * Reference OMAP TRM Chapter 17:
80 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
81 * referring to oversampling - divisor value
82 * baudrate 460,800 to 3,686,400 all have divisor 13
83 * except 3,000,000 which has divisor value 16
84 */
85static unsigned int
86serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
87{
88 unsigned int divisor;
89
90 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
91 divisor = 13;
92 else
93 divisor = 16;
94 return port->uartclk/(baud * divisor);
95}
96
97static void serial_omap_stop_rxdma(struct uart_omap_port *up)
98{
99 if (up->uart_dma.rx_dma_used) {
100 del_timer(&up->uart_dma.rx_timer);
101 omap_stop_dma(up->uart_dma.rx_dma_channel);
102 omap_free_dma(up->uart_dma.rx_dma_channel);
103 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
104 up->uart_dma.rx_dma_used = false;
105 }
106}
107
108static void serial_omap_enable_ms(struct uart_port *port)
109{
110 struct uart_omap_port *up = (struct uart_omap_port *)port;
111
112 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
113 up->ier |= UART_IER_MSI;
114 serial_out(up, UART_IER, up->ier);
115}
116
117static void serial_omap_stop_tx(struct uart_port *port)
118{
119 struct uart_omap_port *up = (struct uart_omap_port *)port;
120
121 if (up->use_dma &&
122 up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
123 /*
124 * Check if dma is still active. If yes do nothing,
125 * return. Else stop dma
126 */
127 if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
128 return;
129 omap_stop_dma(up->uart_dma.tx_dma_channel);
130 omap_free_dma(up->uart_dma.tx_dma_channel);
131 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
132 }
133
134 if (up->ier & UART_IER_THRI) {
135 up->ier &= ~UART_IER_THRI;
136 serial_out(up, UART_IER, up->ier);
137 }
138}
139
140static void serial_omap_stop_rx(struct uart_port *port)
141{
142 struct uart_omap_port *up = (struct uart_omap_port *)port;
143
144 if (up->use_dma)
145 serial_omap_stop_rxdma(up);
146 up->ier &= ~UART_IER_RLSI;
147 up->port.read_status_mask &= ~UART_LSR_DR;
148 serial_out(up, UART_IER, up->ier);
149}
150
151static inline void receive_chars(struct uart_omap_port *up, int *status)
152{
153 struct tty_struct *tty = up->port.state->port.tty;
154 unsigned int flag;
155 unsigned char ch, lsr = *status;
156 int max_count = 256;
157
158 do {
159 if (likely(lsr & UART_LSR_DR))
160 ch = serial_in(up, UART_RX);
161 flag = TTY_NORMAL;
162 up->port.icount.rx++;
163
164 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
165 /*
166 * For statistics only
167 */
168 if (lsr & UART_LSR_BI) {
169 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
170 up->port.icount.brk++;
171 /*
172 * We do the SysRQ and SAK checking
173 * here because otherwise the break
174 * may get masked by ignore_status_mask
175 * or read_status_mask.
176 */
177 if (uart_handle_break(&up->port))
178 goto ignore_char;
179 } else if (lsr & UART_LSR_PE) {
180 up->port.icount.parity++;
181 } else if (lsr & UART_LSR_FE) {
182 up->port.icount.frame++;
183 }
184
185 if (lsr & UART_LSR_OE)
186 up->port.icount.overrun++;
187
188 /*
189 * Mask off conditions which should be ignored.
190 */
191 lsr &= up->port.read_status_mask;
192
193#ifdef CONFIG_SERIAL_OMAP_CONSOLE
194 if (up->port.line == up->port.cons->index) {
195 /* Recover the break flag from console xmit */
196 lsr |= up->lsr_break_flag;
197 }
198#endif
199 if (lsr & UART_LSR_BI)
200 flag = TTY_BREAK;
201 else if (lsr & UART_LSR_PE)
202 flag = TTY_PARITY;
203 else if (lsr & UART_LSR_FE)
204 flag = TTY_FRAME;
205 }
206
207 if (uart_handle_sysrq_char(&up->port, ch))
208 goto ignore_char;
209 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
210ignore_char:
211 lsr = serial_in(up, UART_LSR);
212 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
213 spin_unlock(&up->port.lock);
214 tty_flip_buffer_push(tty);
215 spin_lock(&up->port.lock);
216}
217
218static void transmit_chars(struct uart_omap_port *up)
219{
220 struct circ_buf *xmit = &up->port.state->xmit;
221 int count;
222
223 if (up->port.x_char) {
224 serial_out(up, UART_TX, up->port.x_char);
225 up->port.icount.tx++;
226 up->port.x_char = 0;
227 return;
228 }
229 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
230 serial_omap_stop_tx(&up->port);
231 return;
232 }
233 count = up->port.fifosize / 4;
234 do {
235 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
236 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
237 up->port.icount.tx++;
238 if (uart_circ_empty(xmit))
239 break;
240 } while (--count > 0);
241
242 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
243 uart_write_wakeup(&up->port);
244
245 if (uart_circ_empty(xmit))
246 serial_omap_stop_tx(&up->port);
247}
248
249static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
250{
251 if (!(up->ier & UART_IER_THRI)) {
252 up->ier |= UART_IER_THRI;
253 serial_out(up, UART_IER, up->ier);
254 }
255}
256
257static void serial_omap_start_tx(struct uart_port *port)
258{
259 struct uart_omap_port *up = (struct uart_omap_port *)port;
260 struct circ_buf *xmit;
261 unsigned int start;
262 int ret = 0;
263
264 if (!up->use_dma) {
265 serial_omap_enable_ier_thri(up);
266 return;
267 }
268
269 if (up->uart_dma.tx_dma_used)
270 return;
271
272 xmit = &up->port.state->xmit;
273
274 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
275 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
276 "UART Tx DMA",
277 (void *)uart_tx_dma_callback, up,
278 &(up->uart_dma.tx_dma_channel));
279
280 if (ret < 0) {
281 serial_omap_enable_ier_thri(up);
282 return;
283 }
284 }
285 spin_lock(&(up->uart_dma.tx_lock));
286 up->uart_dma.tx_dma_used = true;
287 spin_unlock(&(up->uart_dma.tx_lock));
288
289 start = up->uart_dma.tx_buf_dma_phys +
290 (xmit->tail & (UART_XMIT_SIZE - 1));
291
292 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
293 /*
294 * It is a circular buffer. See if the buffer has wounded back.
295 * If yes it will have to be transferred in two separate dma
296 * transfers
297 */
298 if (start + up->uart_dma.tx_buf_size >=
299 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
300 up->uart_dma.tx_buf_size =
301 (up->uart_dma.tx_buf_dma_phys +
302 UART_XMIT_SIZE) - start;
303
304 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
305 OMAP_DMA_AMODE_CONSTANT,
306 up->uart_dma.uart_base, 0, 0);
307 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
308 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
309 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
310 OMAP_DMA_DATA_TYPE_S8,
311 up->uart_dma.tx_buf_size, 1,
312 OMAP_DMA_SYNC_ELEMENT,
313 up->uart_dma.uart_dma_tx, 0);
314 /* FIXME: Cache maintenance needed here? */
315 omap_start_dma(up->uart_dma.tx_dma_channel);
316}
317
318static unsigned int check_modem_status(struct uart_omap_port *up)
319{
320 unsigned int status;
321
322 status = serial_in(up, UART_MSR);
323 status |= up->msr_saved_flags;
324 up->msr_saved_flags = 0;
325 if ((status & UART_MSR_ANY_DELTA) == 0)
326 return status;
327
328 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
329 up->port.state != NULL) {
330 if (status & UART_MSR_TERI)
331 up->port.icount.rng++;
332 if (status & UART_MSR_DDSR)
333 up->port.icount.dsr++;
334 if (status & UART_MSR_DDCD)
335 uart_handle_dcd_change
336 (&up->port, status & UART_MSR_DCD);
337 if (status & UART_MSR_DCTS)
338 uart_handle_cts_change
339 (&up->port, status & UART_MSR_CTS);
340 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
341 }
342
343 return status;
344}
345
346/**
347 * serial_omap_irq() - This handles the interrupt from one port
348 * @irq: uart port irq number
349 * @dev_id: uart port info
350 */
351static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
352{
353 struct uart_omap_port *up = dev_id;
354 unsigned int iir, lsr;
355 unsigned long flags;
356
357 iir = serial_in(up, UART_IIR);
358 if (iir & UART_IIR_NO_INT)
359 return IRQ_NONE;
360
361 spin_lock_irqsave(&up->port.lock, flags);
362 lsr = serial_in(up, UART_LSR);
363 if (iir & UART_IIR_RLSI) {
364 if (!up->use_dma) {
365 if (lsr & UART_LSR_DR)
366 receive_chars(up, &lsr);
367 } else {
368 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
369 serial_out(up, UART_IER, up->ier);
370 if ((serial_omap_start_rxdma(up) != 0) &&
371 (lsr & UART_LSR_DR))
372 receive_chars(up, &lsr);
373 }
374 }
375
376 check_modem_status(up);
377 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
378 transmit_chars(up);
379
380 spin_unlock_irqrestore(&up->port.lock, flags);
381 up->port_activity = jiffies;
382 return IRQ_HANDLED;
383}
384
385static unsigned int serial_omap_tx_empty(struct uart_port *port)
386{
387 struct uart_omap_port *up = (struct uart_omap_port *)port;
388 unsigned long flags = 0;
389 unsigned int ret = 0;
390
391 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
392 spin_lock_irqsave(&up->port.lock, flags);
393 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
394 spin_unlock_irqrestore(&up->port.lock, flags);
395
396 return ret;
397}
398
399static unsigned int serial_omap_get_mctrl(struct uart_port *port)
400{
401 struct uart_omap_port *up = (struct uart_omap_port *)port;
402 unsigned char status;
403 unsigned int ret = 0;
404
405 status = check_modem_status(up);
406 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
407
408 if (status & UART_MSR_DCD)
409 ret |= TIOCM_CAR;
410 if (status & UART_MSR_RI)
411 ret |= TIOCM_RNG;
412 if (status & UART_MSR_DSR)
413 ret |= TIOCM_DSR;
414 if (status & UART_MSR_CTS)
415 ret |= TIOCM_CTS;
416 return ret;
417}
418
419static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
420{
421 struct uart_omap_port *up = (struct uart_omap_port *)port;
422 unsigned char mcr = 0;
423
424 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
425 if (mctrl & TIOCM_RTS)
426 mcr |= UART_MCR_RTS;
427 if (mctrl & TIOCM_DTR)
428 mcr |= UART_MCR_DTR;
429 if (mctrl & TIOCM_OUT1)
430 mcr |= UART_MCR_OUT1;
431 if (mctrl & TIOCM_OUT2)
432 mcr |= UART_MCR_OUT2;
433 if (mctrl & TIOCM_LOOP)
434 mcr |= UART_MCR_LOOP;
435
436 mcr |= up->mcr;
437 serial_out(up, UART_MCR, mcr);
438}
439
440static void serial_omap_break_ctl(struct uart_port *port, int break_state)
441{
442 struct uart_omap_port *up = (struct uart_omap_port *)port;
443 unsigned long flags = 0;
444
445 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
446 spin_lock_irqsave(&up->port.lock, flags);
447 if (break_state == -1)
448 up->lcr |= UART_LCR_SBC;
449 else
450 up->lcr &= ~UART_LCR_SBC;
451 serial_out(up, UART_LCR, up->lcr);
452 spin_unlock_irqrestore(&up->port.lock, flags);
453}
454
455static int serial_omap_startup(struct uart_port *port)
456{
457 struct uart_omap_port *up = (struct uart_omap_port *)port;
458 unsigned long flags = 0;
459 int retval;
460
461 /*
462 * Allocate the IRQ
463 */
464 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
465 up->name, up);
466 if (retval)
467 return retval;
468
469 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
470
471 /*
472 * Clear the FIFO buffers and disable them.
473 * (they will be reenabled in set_termios())
474 */
475 serial_omap_clear_fifos(up);
476 /* For Hardware flow control */
477 serial_out(up, UART_MCR, UART_MCR_RTS);
478
479 /*
480 * Clear the interrupt registers.
481 */
482 (void) serial_in(up, UART_LSR);
483 if (serial_in(up, UART_LSR) & UART_LSR_DR)
484 (void) serial_in(up, UART_RX);
485 (void) serial_in(up, UART_IIR);
486 (void) serial_in(up, UART_MSR);
487
488 /*
489 * Now, initialize the UART
490 */
491 serial_out(up, UART_LCR, UART_LCR_WLEN8);
492 spin_lock_irqsave(&up->port.lock, flags);
493 /*
494 * Most PC uarts need OUT2 raised to enable interrupts.
495 */
496 up->port.mctrl |= TIOCM_OUT2;
497 serial_omap_set_mctrl(&up->port, up->port.mctrl);
498 spin_unlock_irqrestore(&up->port.lock, flags);
499
500 up->msr_saved_flags = 0;
501 if (up->use_dma) {
502 free_page((unsigned long)up->port.state->xmit.buf);
503 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
504 UART_XMIT_SIZE,
505 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
506 0);
507 init_timer(&(up->uart_dma.rx_timer));
508 up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
509 up->uart_dma.rx_timer.data = up->pdev->id;
510 /* Currently the buffer size is 4KB. Can increase it */
511 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
512 up->uart_dma.rx_buf_size,
513 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
514 }
515 /*
516 * Finally, enable interrupts. Note: Modem status interrupts
517 * are set via set_termios(), which will be occurring imminently
518 * anyway, so we don't enable them here.
519 */
520 up->ier = UART_IER_RLSI | UART_IER_RDI;
521 serial_out(up, UART_IER, up->ier);
522
523 /* Enable module level wake up */
524 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
525
526 up->port_activity = jiffies;
527 return 0;
528}
529
530static void serial_omap_shutdown(struct uart_port *port)
531{
532 struct uart_omap_port *up = (struct uart_omap_port *)port;
533 unsigned long flags = 0;
534
535 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
536 /*
537 * Disable interrupts from this port
538 */
539 up->ier = 0;
540 serial_out(up, UART_IER, 0);
541
542 spin_lock_irqsave(&up->port.lock, flags);
543 up->port.mctrl &= ~TIOCM_OUT2;
544 serial_omap_set_mctrl(&up->port, up->port.mctrl);
545 spin_unlock_irqrestore(&up->port.lock, flags);
546
547 /*
548 * Disable break condition and FIFOs
549 */
550 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
551 serial_omap_clear_fifos(up);
552
553 /*
554 * Read data port to reset things, and then free the irq
555 */
556 if (serial_in(up, UART_LSR) & UART_LSR_DR)
557 (void) serial_in(up, UART_RX);
558 if (up->use_dma) {
559 dma_free_coherent(up->port.dev,
560 UART_XMIT_SIZE, up->port.state->xmit.buf,
561 up->uart_dma.tx_buf_dma_phys);
562 up->port.state->xmit.buf = NULL;
563 serial_omap_stop_rx(port);
564 dma_free_coherent(up->port.dev,
565 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
566 up->uart_dma.rx_buf_dma_phys);
567 up->uart_dma.rx_buf = NULL;
568 }
569 free_irq(up->port.irq, up);
570}
571
572static inline void
573serial_omap_configure_xonxoff
574 (struct uart_omap_port *up, struct ktermios *termios)
575{
576 unsigned char efr = 0;
577
578 up->lcr = serial_in(up, UART_LCR);
579 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
580 up->efr = serial_in(up, UART_EFR);
581 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
582
583 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
584 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
585
586 /* clear SW control mode bits */
587 efr = up->efr;
588 efr &= OMAP_UART_SW_CLR;
589
590 /*
591 * IXON Flag:
592 * Enable XON/XOFF flow control on output.
593 * Transmit XON1, XOFF1
594 */
595 if (termios->c_iflag & IXON)
596 efr |= OMAP_UART_SW_TX;
597
598 /*
599 * IXOFF Flag:
600 * Enable XON/XOFF flow control on input.
601 * Receiver compares XON1, XOFF1.
602 */
603 if (termios->c_iflag & IXOFF)
604 efr |= OMAP_UART_SW_RX;
605
606 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
607 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
608
609 up->mcr = serial_in(up, UART_MCR);
610
611 /*
612 * IXANY Flag:
613 * Enable any character to restart output.
614 * Operation resumes after receiving any
615 * character after recognition of the XOFF character
616 */
617 if (termios->c_iflag & IXANY)
618 up->mcr |= UART_MCR_XONANY;
619
620 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
621 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
622 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
623 /* Enable special char function UARTi.EFR_REG[5] and
624 * load the new software flow control mode IXON or IXOFF
625 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
626 */
627 serial_out(up, UART_EFR, efr | UART_EFR_SCD);
628 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
629
630 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
631 serial_out(up, UART_LCR, up->lcr);
632}
633
634static void
635serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
636 struct ktermios *old)
637{
638 struct uart_omap_port *up = (struct uart_omap_port *)port;
639 unsigned char cval = 0;
640 unsigned char efr = 0;
641 unsigned long flags = 0;
642 unsigned int baud, quot;
643
644 switch (termios->c_cflag & CSIZE) {
645 case CS5:
646 cval = UART_LCR_WLEN5;
647 break;
648 case CS6:
649 cval = UART_LCR_WLEN6;
650 break;
651 case CS7:
652 cval = UART_LCR_WLEN7;
653 break;
654 default:
655 case CS8:
656 cval = UART_LCR_WLEN8;
657 break;
658 }
659
660 if (termios->c_cflag & CSTOPB)
661 cval |= UART_LCR_STOP;
662 if (termios->c_cflag & PARENB)
663 cval |= UART_LCR_PARITY;
664 if (!(termios->c_cflag & PARODD))
665 cval |= UART_LCR_EPAR;
666
667 /*
668 * Ask the core to calculate the divisor for us.
669 */
670
671 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
672 quot = serial_omap_get_divisor(port, baud);
673
674 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
675 UART_FCR_ENABLE_FIFO;
676 if (up->use_dma)
677 up->fcr |= UART_FCR_DMA_SELECT;
678
679 /*
680 * Ok, we're now changing the port state. Do it with
681 * interrupts disabled.
682 */
683 spin_lock_irqsave(&up->port.lock, flags);
684
685 /*
686 * Update the per-port timeout.
687 */
688 uart_update_timeout(port, termios->c_cflag, baud);
689
690 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
691 if (termios->c_iflag & INPCK)
692 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
693 if (termios->c_iflag & (BRKINT | PARMRK))
694 up->port.read_status_mask |= UART_LSR_BI;
695
696 /*
697 * Characters to ignore
698 */
699 up->port.ignore_status_mask = 0;
700 if (termios->c_iflag & IGNPAR)
701 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
702 if (termios->c_iflag & IGNBRK) {
703 up->port.ignore_status_mask |= UART_LSR_BI;
704 /*
705 * If we're ignoring parity and break indicators,
706 * ignore overruns too (for real raw support).
707 */
708 if (termios->c_iflag & IGNPAR)
709 up->port.ignore_status_mask |= UART_LSR_OE;
710 }
711
712 /*
713 * ignore all characters if CREAD is not set
714 */
715 if ((termios->c_cflag & CREAD) == 0)
716 up->port.ignore_status_mask |= UART_LSR_DR;
717
718 /*
719 * Modem status interrupts
720 */
721 up->ier &= ~UART_IER_MSI;
722 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
723 up->ier |= UART_IER_MSI;
724 serial_out(up, UART_IER, up->ier);
725 serial_out(up, UART_LCR, cval); /* reset DLAB */
726
727 /* FIFOs and DMA Settings */
728
729 /* FCR can be changed only when the
730 * baud clock is not running
731 * DLL_REG and DLH_REG set to 0.
732 */
733 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
734 serial_out(up, UART_DLL, 0);
735 serial_out(up, UART_DLM, 0);
736 serial_out(up, UART_LCR, 0);
737
738 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
739
740 up->efr = serial_in(up, UART_EFR);
741 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
742
743 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
744 up->mcr = serial_in(up, UART_MCR);
745 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
746 /* FIFO ENABLE, DMA MODE */
747 serial_out(up, UART_FCR, up->fcr);
748 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
749
750 if (up->use_dma) {
751 serial_out(up, UART_TI752_TLR, 0);
752 serial_out(up, UART_OMAP_SCR,
753 (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8));
754 }
755
756 serial_out(up, UART_EFR, up->efr);
757 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
758 serial_out(up, UART_MCR, up->mcr);
759
760 /* Protocol, Baud Rate, and Interrupt Settings */
761
762 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
763 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
764
765 up->efr = serial_in(up, UART_EFR);
766 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
767
768 serial_out(up, UART_LCR, 0);
769 serial_out(up, UART_IER, 0);
770 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
771
772 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
773 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
774
775 serial_out(up, UART_LCR, 0);
776 serial_out(up, UART_IER, up->ier);
777 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
778
779 serial_out(up, UART_EFR, up->efr);
780 serial_out(up, UART_LCR, cval);
781
782 if (baud > 230400 && baud != 3000000)
783 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
784 else
785 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
786
787 /* Hardware Flow Control Configuration */
788
789 if (termios->c_cflag & CRTSCTS) {
790 efr |= (UART_EFR_CTS | UART_EFR_RTS);
791 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
792
793 up->mcr = serial_in(up, UART_MCR);
794 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
795
796 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
797 up->efr = serial_in(up, UART_EFR);
798 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
799
800 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
801 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
802 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
803 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
804 serial_out(up, UART_LCR, cval);
805 }
806
807 serial_omap_set_mctrl(&up->port, up->port.mctrl);
808 /* Software Flow Control Configuration */
809 if (termios->c_iflag & (IXON | IXOFF))
810 serial_omap_configure_xonxoff(up, termios);
811
812 spin_unlock_irqrestore(&up->port.lock, flags);
813 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
814}
815
816static void
817serial_omap_pm(struct uart_port *port, unsigned int state,
818 unsigned int oldstate)
819{
820 struct uart_omap_port *up = (struct uart_omap_port *)port;
821 unsigned char efr;
822
823 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
824 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
825 efr = serial_in(up, UART_EFR);
826 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
827 serial_out(up, UART_LCR, 0);
828
829 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
830 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
831 serial_out(up, UART_EFR, efr);
832 serial_out(up, UART_LCR, 0);
833}
834
835static void serial_omap_release_port(struct uart_port *port)
836{
837 dev_dbg(port->dev, "serial_omap_release_port+\n");
838}
839
840static int serial_omap_request_port(struct uart_port *port)
841{
842 dev_dbg(port->dev, "serial_omap_request_port+\n");
843 return 0;
844}
845
846static void serial_omap_config_port(struct uart_port *port, int flags)
847{
848 struct uart_omap_port *up = (struct uart_omap_port *)port;
849
850 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
851 up->pdev->id);
852 up->port.type = PORT_OMAP;
853}
854
855static int
856serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
857{
858 /* we don't want the core code to modify any port params */
859 dev_dbg(port->dev, "serial_omap_verify_port+\n");
860 return -EINVAL;
861}
862
863static const char *
864serial_omap_type(struct uart_port *port)
865{
866 struct uart_omap_port *up = (struct uart_omap_port *)port;
867
868 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
869 return up->name;
870}
871
872#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
873
874static inline void wait_for_xmitr(struct uart_omap_port *up)
875{
876 unsigned int status, tmout = 10000;
877
878 /* Wait up to 10ms for the character(s) to be sent. */
879 do {
880 status = serial_in(up, UART_LSR);
881
882 if (status & UART_LSR_BI)
883 up->lsr_break_flag = UART_LSR_BI;
884
885 if (--tmout == 0)
886 break;
887 udelay(1);
888 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
889
890 /* Wait up to 1s for flow control if necessary */
891 if (up->port.flags & UPF_CONS_FLOW) {
892 tmout = 1000000;
893 for (tmout = 1000000; tmout; tmout--) {
894 unsigned int msr = serial_in(up, UART_MSR);
895
896 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
897 if (msr & UART_MSR_CTS)
898 break;
899
900 udelay(1);
901 }
902 }
903}
904
905#ifdef CONFIG_CONSOLE_POLL
906
907static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
908{
909 struct uart_omap_port *up = (struct uart_omap_port *)port;
910 wait_for_xmitr(up);
911 serial_out(up, UART_TX, ch);
912}
913
914static int serial_omap_poll_get_char(struct uart_port *port)
915{
916 struct uart_omap_port *up = (struct uart_omap_port *)port;
917 unsigned int status = serial_in(up, UART_LSR);
918
919 if (!(status & UART_LSR_DR))
920 return NO_POLL_CHAR;
921
922 return serial_in(up, UART_RX);
923}
924
925#endif /* CONFIG_CONSOLE_POLL */
926
927#ifdef CONFIG_SERIAL_OMAP_CONSOLE
928
929static struct uart_omap_port *serial_omap_console_ports[4];
930
931static struct uart_driver serial_omap_reg;
932
933static void serial_omap_console_putchar(struct uart_port *port, int ch)
934{
935 struct uart_omap_port *up = (struct uart_omap_port *)port;
936
937 wait_for_xmitr(up);
938 serial_out(up, UART_TX, ch);
939}
940
941static void
942serial_omap_console_write(struct console *co, const char *s,
943 unsigned int count)
944{
945 struct uart_omap_port *up = serial_omap_console_ports[co->index];
946 unsigned long flags;
947 unsigned int ier;
948 int locked = 1;
949
950 local_irq_save(flags);
951 if (up->port.sysrq)
952 locked = 0;
953 else if (oops_in_progress)
954 locked = spin_trylock(&up->port.lock);
955 else
956 spin_lock(&up->port.lock);
957
958 /*
959 * First save the IER then disable the interrupts
960 */
961 ier = serial_in(up, UART_IER);
962 serial_out(up, UART_IER, 0);
963
964 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
965
966 /*
967 * Finally, wait for transmitter to become empty
968 * and restore the IER
969 */
970 wait_for_xmitr(up);
971 serial_out(up, UART_IER, ier);
972 /*
973 * The receive handling will happen properly because the
974 * receive ready bit will still be set; it is not cleared
975 * on read. However, modem control will not, we must
976 * call it if we have saved something in the saved flags
977 * while processing with interrupts off.
978 */
979 if (up->msr_saved_flags)
980 check_modem_status(up);
981
982 if (locked)
983 spin_unlock(&up->port.lock);
984 local_irq_restore(flags);
985}
986
987static int __init
988serial_omap_console_setup(struct console *co, char *options)
989{
990 struct uart_omap_port *up;
991 int baud = 115200;
992 int bits = 8;
993 int parity = 'n';
994 int flow = 'n';
995
996 if (serial_omap_console_ports[co->index] == NULL)
997 return -ENODEV;
998 up = serial_omap_console_ports[co->index];
999
1000 if (options)
1001 uart_parse_options(options, &baud, &parity, &bits, &flow);
1002
1003 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1004}
1005
1006static struct console serial_omap_console = {
1007 .name = OMAP_SERIAL_NAME,
1008 .write = serial_omap_console_write,
1009 .device = uart_console_device,
1010 .setup = serial_omap_console_setup,
1011 .flags = CON_PRINTBUFFER,
1012 .index = -1,
1013 .data = &serial_omap_reg,
1014};
1015
1016static void serial_omap_add_console_port(struct uart_omap_port *up)
1017{
1018 serial_omap_console_ports[up->pdev->id] = up;
1019}
1020
1021#define OMAP_CONSOLE (&serial_omap_console)
1022
1023#else
1024
1025#define OMAP_CONSOLE NULL
1026
1027static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1028{}
1029
1030#endif
1031
1032static struct uart_ops serial_omap_pops = {
1033 .tx_empty = serial_omap_tx_empty,
1034 .set_mctrl = serial_omap_set_mctrl,
1035 .get_mctrl = serial_omap_get_mctrl,
1036 .stop_tx = serial_omap_stop_tx,
1037 .start_tx = serial_omap_start_tx,
1038 .stop_rx = serial_omap_stop_rx,
1039 .enable_ms = serial_omap_enable_ms,
1040 .break_ctl = serial_omap_break_ctl,
1041 .startup = serial_omap_startup,
1042 .shutdown = serial_omap_shutdown,
1043 .set_termios = serial_omap_set_termios,
1044 .pm = serial_omap_pm,
1045 .type = serial_omap_type,
1046 .release_port = serial_omap_release_port,
1047 .request_port = serial_omap_request_port,
1048 .config_port = serial_omap_config_port,
1049 .verify_port = serial_omap_verify_port,
1050#ifdef CONFIG_CONSOLE_POLL
1051 .poll_put_char = serial_omap_poll_put_char,
1052 .poll_get_char = serial_omap_poll_get_char,
1053#endif
1054};
1055
1056static struct uart_driver serial_omap_reg = {
1057 .owner = THIS_MODULE,
1058 .driver_name = "OMAP-SERIAL",
1059 .dev_name = OMAP_SERIAL_NAME,
1060 .nr = OMAP_MAX_HSUART_PORTS,
1061 .cons = OMAP_CONSOLE,
1062};
1063
1064static int
1065serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
1066{
1067 struct uart_omap_port *up = platform_get_drvdata(pdev);
1068
1069 if (up)
1070 uart_suspend_port(&serial_omap_reg, &up->port);
1071 return 0;
1072}
1073
1074static int serial_omap_resume(struct platform_device *dev)
1075{
1076 struct uart_omap_port *up = platform_get_drvdata(dev);
1077
1078 if (up)
1079 uart_resume_port(&serial_omap_reg, &up->port);
1080 return 0;
1081}
1082
1083static void serial_omap_rx_timeout(unsigned long uart_no)
1084{
1085 struct uart_omap_port *up = ui[uart_no];
1086 unsigned int curr_dma_pos, curr_transmitted_size;
1087 int ret = 0;
1088
1089 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1090 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1091 (curr_dma_pos == 0)) {
1092 if (jiffies_to_msecs(jiffies - up->port_activity) <
1093 RX_TIMEOUT) {
1094 mod_timer(&up->uart_dma.rx_timer, jiffies +
1095 usecs_to_jiffies(up->uart_dma.rx_timeout));
1096 } else {
1097 serial_omap_stop_rxdma(up);
1098 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1099 serial_out(up, UART_IER, up->ier);
1100 }
1101 return;
1102 }
1103
1104 curr_transmitted_size = curr_dma_pos -
1105 up->uart_dma.prev_rx_dma_pos;
1106 up->port.icount.rx += curr_transmitted_size;
1107 tty_insert_flip_string(up->port.state->port.tty,
1108 up->uart_dma.rx_buf +
1109 (up->uart_dma.prev_rx_dma_pos -
1110 up->uart_dma.rx_buf_dma_phys),
1111 curr_transmitted_size);
1112 tty_flip_buffer_push(up->port.state->port.tty);
1113 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1114 if (up->uart_dma.rx_buf_size +
1115 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1116 ret = serial_omap_start_rxdma(up);
1117 if (ret < 0) {
1118 serial_omap_stop_rxdma(up);
1119 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1120 serial_out(up, UART_IER, up->ier);
1121 }
1122 } else {
1123 mod_timer(&up->uart_dma.rx_timer, jiffies +
1124 usecs_to_jiffies(up->uart_dma.rx_timeout));
1125 }
1126 up->port_activity = jiffies;
1127}
1128
1129static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1130{
1131 return;
1132}
1133
1134static int serial_omap_start_rxdma(struct uart_omap_port *up)
1135{
1136 int ret = 0;
1137
1138 if (up->uart_dma.rx_dma_channel == -1) {
1139 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1140 "UART Rx DMA",
1141 (void *)uart_rx_dma_callback, up,
1142 &(up->uart_dma.rx_dma_channel));
1143 if (ret < 0)
1144 return ret;
1145
1146 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1147 OMAP_DMA_AMODE_CONSTANT,
1148 up->uart_dma.uart_base, 0, 0);
1149 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1150 OMAP_DMA_AMODE_POST_INC,
1151 up->uart_dma.rx_buf_dma_phys, 0, 0);
1152 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1153 OMAP_DMA_DATA_TYPE_S8,
1154 up->uart_dma.rx_buf_size, 1,
1155 OMAP_DMA_SYNC_ELEMENT,
1156 up->uart_dma.uart_dma_rx, 0);
1157 }
1158 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1159 /* FIXME: Cache maintenance needed here? */
1160 omap_start_dma(up->uart_dma.rx_dma_channel);
1161 mod_timer(&up->uart_dma.rx_timer, jiffies +
1162 usecs_to_jiffies(up->uart_dma.rx_timeout));
1163 up->uart_dma.rx_dma_used = true;
1164 return ret;
1165}
1166
1167static void serial_omap_continue_tx(struct uart_omap_port *up)
1168{
1169 struct circ_buf *xmit = &up->port.state->xmit;
1170 unsigned int start = up->uart_dma.tx_buf_dma_phys
1171 + (xmit->tail & (UART_XMIT_SIZE - 1));
1172
1173 if (uart_circ_empty(xmit))
1174 return;
1175
1176 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1177 /*
1178 * It is a circular buffer. See if the buffer has wounded back.
1179 * If yes it will have to be transferred in two separate dma
1180 * transfers
1181 */
1182 if (start + up->uart_dma.tx_buf_size >=
1183 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1184 up->uart_dma.tx_buf_size =
1185 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1186 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1187 OMAP_DMA_AMODE_CONSTANT,
1188 up->uart_dma.uart_base, 0, 0);
1189 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1190 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1191 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1192 OMAP_DMA_DATA_TYPE_S8,
1193 up->uart_dma.tx_buf_size, 1,
1194 OMAP_DMA_SYNC_ELEMENT,
1195 up->uart_dma.uart_dma_tx, 0);
1196 /* FIXME: Cache maintenance needed here? */
1197 omap_start_dma(up->uart_dma.tx_dma_channel);
1198}
1199
1200static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1201{
1202 struct uart_omap_port *up = (struct uart_omap_port *)data;
1203 struct circ_buf *xmit = &up->port.state->xmit;
1204
1205 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1206 (UART_XMIT_SIZE - 1);
1207 up->port.icount.tx += up->uart_dma.tx_buf_size;
1208
1209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1210 uart_write_wakeup(&up->port);
1211
1212 if (uart_circ_empty(xmit)) {
1213 spin_lock(&(up->uart_dma.tx_lock));
1214 serial_omap_stop_tx(&up->port);
1215 up->uart_dma.tx_dma_used = false;
1216 spin_unlock(&(up->uart_dma.tx_lock));
1217 } else {
1218 omap_stop_dma(up->uart_dma.tx_dma_channel);
1219 serial_omap_continue_tx(up);
1220 }
1221 up->port_activity = jiffies;
1222 return;
1223}
1224
1225static int serial_omap_probe(struct platform_device *pdev)
1226{
1227 struct uart_omap_port *up;
1228 struct resource *mem, *irq, *dma_tx, *dma_rx;
1229 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1230 int ret = -ENOSPC;
1231
1232 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1233 if (!mem) {
1234 dev_err(&pdev->dev, "no mem resource?\n");
1235 return -ENODEV;
1236 }
1237
1238 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1239 if (!irq) {
1240 dev_err(&pdev->dev, "no irq resource?\n");
1241 return -ENODEV;
1242 }
1243
1244 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1245 pdev->dev.driver->name)) {
1246 dev_err(&pdev->dev, "memory region already claimed\n");
1247 return -EBUSY;
1248 }
1249
1250 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1251 if (!dma_rx) {
1252 ret = -EINVAL;
1253 goto err;
1254 }
1255
1256 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1257 if (!dma_tx) {
1258 ret = -EINVAL;
1259 goto err;
1260 }
1261
1262 up = kzalloc(sizeof(*up), GFP_KERNEL);
1263 if (up == NULL) {
1264 ret = -ENOMEM;
1265 goto do_release_region;
1266 }
1267 sprintf(up->name, "OMAP UART%d", pdev->id);
1268 up->pdev = pdev;
1269 up->port.dev = &pdev->dev;
1270 up->port.type = PORT_OMAP;
1271 up->port.iotype = UPIO_MEM;
1272 up->port.irq = irq->start;
1273
1274 up->port.regshift = 2;
1275 up->port.fifosize = 64;
1276 up->port.ops = &serial_omap_pops;
1277 up->port.line = pdev->id;
1278
1279 up->port.membase = omap_up_info->membase;
1280 up->port.mapbase = omap_up_info->mapbase;
1281 up->port.flags = omap_up_info->flags;
1282 up->port.irqflags = omap_up_info->irqflags;
1283 up->port.uartclk = omap_up_info->uartclk;
1284 up->uart_dma.uart_base = mem->start;
1285
1286 if (omap_up_info->dma_enabled) {
1287 up->uart_dma.uart_dma_tx = dma_tx->start;
1288 up->uart_dma.uart_dma_rx = dma_rx->start;
1289 up->use_dma = 1;
1290 up->uart_dma.rx_buf_size = 4096;
1291 up->uart_dma.rx_timeout = 2;
1292 spin_lock_init(&(up->uart_dma.tx_lock));
1293 spin_lock_init(&(up->uart_dma.rx_lock));
1294 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1295 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1296 }
1297
1298 ui[pdev->id] = up;
1299 serial_omap_add_console_port(up);
1300
1301 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1302 if (ret != 0)
1303 goto do_release_region;
1304
1305 platform_set_drvdata(pdev, up);
1306 return 0;
1307err:
1308 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1309 pdev->id, __func__, ret);
1310do_release_region:
1311 release_mem_region(mem->start, (mem->end - mem->start) + 1);
1312 return ret;
1313}
1314
1315static int serial_omap_remove(struct platform_device *dev)
1316{
1317 struct uart_omap_port *up = platform_get_drvdata(dev);
1318
1319 platform_set_drvdata(dev, NULL);
1320 if (up) {
1321 uart_remove_one_port(&serial_omap_reg, &up->port);
1322 kfree(up);
1323 }
1324 return 0;
1325}
1326
1327static struct platform_driver serial_omap_driver = {
1328 .probe = serial_omap_probe,
1329 .remove = serial_omap_remove,
1330
1331 .suspend = serial_omap_suspend,
1332 .resume = serial_omap_resume,
1333 .driver = {
1334 .name = DRIVER_NAME,
1335 },
1336};
1337
1338static int __init serial_omap_init(void)
1339{
1340 int ret;
1341
1342 ret = uart_register_driver(&serial_omap_reg);
1343 if (ret != 0)
1344 return ret;
1345 ret = platform_driver_register(&serial_omap_driver);
1346 if (ret != 0)
1347 uart_unregister_driver(&serial_omap_reg);
1348 return ret;
1349}
1350
1351static void __exit serial_omap_exit(void)
1352{
1353 platform_driver_unregister(&serial_omap_driver);
1354 uart_unregister_driver(&serial_omap_reg);
1355}
1356
1357module_init(serial_omap_init);
1358module_exit(serial_omap_exit);
1359
1360MODULE_DESCRIPTION("OMAP High Speed UART driver");
1361MODULE_LICENSE("GPL");
1362MODULE_AUTHOR("Texas Instruments Inc");
diff --git a/drivers/tty/serial/pch_uart.c b/drivers/tty/serial/pch_uart.c
new file mode 100644
index 000000000000..465210930890
--- /dev/null
+++ b/drivers/tty/serial/pch_uart.c
@@ -0,0 +1,1612 @@
1/*
2 *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
3 *
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; version 2 of the License.
7 *
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 *
13 *You should have received a copy of the GNU General Public License
14 *along with this program; if not, write to the Free Software
15 *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
17#include <linux/serial_reg.h>
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/serial_core.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/dmi.h>
25
26#include <linux/dmaengine.h>
27#include <linux/pch_dma.h>
28
29enum {
30 PCH_UART_HANDLED_RX_INT_SHIFT,
31 PCH_UART_HANDLED_TX_INT_SHIFT,
32 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
33 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
34 PCH_UART_HANDLED_MS_INT_SHIFT,
35};
36
37enum {
38 PCH_UART_8LINE,
39 PCH_UART_2LINE,
40};
41
42#define PCH_UART_DRIVER_DEVICE "ttyPCH"
43
44/* Set the max number of UART port
45 * Intel EG20T PCH: 4 port
46 * OKI SEMICONDUCTOR ML7213 IOH: 3 port
47*/
48#define PCH_UART_NR 4
49
50#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
51#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
52#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
53 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
54#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
55 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
56#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
57
58#define PCH_UART_RBR 0x00
59#define PCH_UART_THR 0x00
60
61#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
62 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
63#define PCH_UART_IER_ERBFI 0x00000001
64#define PCH_UART_IER_ETBEI 0x00000002
65#define PCH_UART_IER_ELSI 0x00000004
66#define PCH_UART_IER_EDSSI 0x00000008
67
68#define PCH_UART_IIR_IP 0x00000001
69#define PCH_UART_IIR_IID 0x00000006
70#define PCH_UART_IIR_MSI 0x00000000
71#define PCH_UART_IIR_TRI 0x00000002
72#define PCH_UART_IIR_RRI 0x00000004
73#define PCH_UART_IIR_REI 0x00000006
74#define PCH_UART_IIR_TOI 0x00000008
75#define PCH_UART_IIR_FIFO256 0x00000020
76#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
77#define PCH_UART_IIR_FE 0x000000C0
78
79#define PCH_UART_FCR_FIFOE 0x00000001
80#define PCH_UART_FCR_RFR 0x00000002
81#define PCH_UART_FCR_TFR 0x00000004
82#define PCH_UART_FCR_DMS 0x00000008
83#define PCH_UART_FCR_FIFO256 0x00000020
84#define PCH_UART_FCR_RFTL 0x000000C0
85
86#define PCH_UART_FCR_RFTL1 0x00000000
87#define PCH_UART_FCR_RFTL64 0x00000040
88#define PCH_UART_FCR_RFTL128 0x00000080
89#define PCH_UART_FCR_RFTL224 0x000000C0
90#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
91#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
92#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
93#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
94#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
95#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
96#define PCH_UART_FCR_RFTL_SHIFT 6
97
98#define PCH_UART_LCR_WLS 0x00000003
99#define PCH_UART_LCR_STB 0x00000004
100#define PCH_UART_LCR_PEN 0x00000008
101#define PCH_UART_LCR_EPS 0x00000010
102#define PCH_UART_LCR_SP 0x00000020
103#define PCH_UART_LCR_SB 0x00000040
104#define PCH_UART_LCR_DLAB 0x00000080
105#define PCH_UART_LCR_NP 0x00000000
106#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
107#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
108#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
109#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
110 PCH_UART_LCR_SP)
111
112#define PCH_UART_LCR_5BIT 0x00000000
113#define PCH_UART_LCR_6BIT 0x00000001
114#define PCH_UART_LCR_7BIT 0x00000002
115#define PCH_UART_LCR_8BIT 0x00000003
116
117#define PCH_UART_MCR_DTR 0x00000001
118#define PCH_UART_MCR_RTS 0x00000002
119#define PCH_UART_MCR_OUT 0x0000000C
120#define PCH_UART_MCR_LOOP 0x00000010
121#define PCH_UART_MCR_AFE 0x00000020
122
123#define PCH_UART_LSR_DR 0x00000001
124#define PCH_UART_LSR_ERR (1<<7)
125
126#define PCH_UART_MSR_DCTS 0x00000001
127#define PCH_UART_MSR_DDSR 0x00000002
128#define PCH_UART_MSR_TERI 0x00000004
129#define PCH_UART_MSR_DDCD 0x00000008
130#define PCH_UART_MSR_CTS 0x00000010
131#define PCH_UART_MSR_DSR 0x00000020
132#define PCH_UART_MSR_RI 0x00000040
133#define PCH_UART_MSR_DCD 0x00000080
134#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
135 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
136
137#define PCH_UART_DLL 0x00
138#define PCH_UART_DLM 0x01
139
140#define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
141
142#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
143#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
144#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
145#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
146#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
147
148#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
149#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
150#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
151#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
152#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
153#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
154#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
155#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
156#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
157#define PCH_UART_HAL_STB1 0
158#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
159
160#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
161#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
162#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
163 PCH_UART_HAL_CLR_RX_FIFO)
164
165#define PCH_UART_HAL_DMA_MODE0 0
166#define PCH_UART_HAL_FIFO_DIS 0
167#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
168#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
169 PCH_UART_FCR_FIFO256)
170#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
171#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
172#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
173#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
174#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
175#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
176#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
177#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
178#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
179#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
180#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
181#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
182#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
183#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
184
185#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
186#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
187#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
188#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
189#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
190
191#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
192#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
193#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
194#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
195#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
196
197#define PCI_VENDOR_ID_ROHM 0x10DB
198
199struct pch_uart_buffer {
200 unsigned char *buf;
201 int size;
202};
203
204struct eg20t_port {
205 struct uart_port port;
206 int port_type;
207 void __iomem *membase;
208 resource_size_t mapbase;
209 unsigned int iobase;
210 struct pci_dev *pdev;
211 int fifo_size;
212 int base_baud;
213 int start_tx;
214 int start_rx;
215 int tx_empty;
216 int int_dis_flag;
217 int trigger;
218 int trigger_level;
219 struct pch_uart_buffer rxbuf;
220 unsigned int dmsr;
221 unsigned int fcr;
222 unsigned int mcr;
223 unsigned int use_dma;
224 unsigned int use_dma_flag;
225 struct dma_async_tx_descriptor *desc_tx;
226 struct dma_async_tx_descriptor *desc_rx;
227 struct pch_dma_slave param_tx;
228 struct pch_dma_slave param_rx;
229 struct dma_chan *chan_tx;
230 struct dma_chan *chan_rx;
231 struct scatterlist *sg_tx_p;
232 int nent;
233 struct scatterlist sg_rx;
234 int tx_dma_use;
235 void *rx_buf_virt;
236 dma_addr_t rx_buf_dma;
237};
238
239/**
240 * struct pch_uart_driver_data - private data structure for UART-DMA
241 * @port_type: The number of DMA channel
242 * @line_no: UART port line number (0, 1, 2...)
243 */
244struct pch_uart_driver_data {
245 int port_type;
246 int line_no;
247};
248
249enum pch_uart_num_t {
250 pch_et20t_uart0 = 0,
251 pch_et20t_uart1,
252 pch_et20t_uart2,
253 pch_et20t_uart3,
254 pch_ml7213_uart0,
255 pch_ml7213_uart1,
256 pch_ml7213_uart2,
257 pch_ml7223_uart0,
258 pch_ml7223_uart1,
259};
260
261static struct pch_uart_driver_data drv_dat[] = {
262 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
263 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
264 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
265 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
266 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
267 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
268 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
269 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
270 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
271};
272
273static unsigned int default_baud = 9600;
274static const int trigger_level_256[4] = { 1, 64, 128, 224 };
275static const int trigger_level_64[4] = { 1, 16, 32, 56 };
276static const int trigger_level_16[4] = { 1, 4, 8, 14 };
277static const int trigger_level_1[4] = { 1, 1, 1, 1 };
278
279static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
280 int base_baud)
281{
282 struct eg20t_port *priv = pci_get_drvdata(pdev);
283
284 priv->trigger_level = 1;
285 priv->fcr = 0;
286}
287
288static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
289{
290 unsigned int msr = ioread8(base + UART_MSR);
291 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
292
293 return msr;
294}
295
296static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
297 unsigned int flag)
298{
299 u8 ier = ioread8(priv->membase + UART_IER);
300 ier |= flag & PCH_UART_IER_MASK;
301 iowrite8(ier, priv->membase + UART_IER);
302}
303
304static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
305 unsigned int flag)
306{
307 u8 ier = ioread8(priv->membase + UART_IER);
308 ier &= ~(flag & PCH_UART_IER_MASK);
309 iowrite8(ier, priv->membase + UART_IER);
310}
311
312static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
313 unsigned int parity, unsigned int bits,
314 unsigned int stb)
315{
316 unsigned int dll, dlm, lcr;
317 int div;
318
319 div = DIV_ROUND(priv->base_baud / 16, baud);
320 if (div < 0 || USHRT_MAX <= div) {
321 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
322 return -EINVAL;
323 }
324
325 dll = (unsigned int)div & 0x00FFU;
326 dlm = ((unsigned int)div >> 8) & 0x00FFU;
327
328 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
329 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
330 return -EINVAL;
331 }
332
333 if (bits & ~PCH_UART_LCR_WLS) {
334 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
335 return -EINVAL;
336 }
337
338 if (stb & ~PCH_UART_LCR_STB) {
339 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
340 return -EINVAL;
341 }
342
343 lcr = parity;
344 lcr |= bits;
345 lcr |= stb;
346
347 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
348 __func__, baud, div, lcr, jiffies);
349 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
350 iowrite8(dll, priv->membase + PCH_UART_DLL);
351 iowrite8(dlm, priv->membase + PCH_UART_DLM);
352 iowrite8(lcr, priv->membase + UART_LCR);
353
354 return 0;
355}
356
357static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
358 unsigned int flag)
359{
360 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
361 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
362 __func__, flag);
363 return -EINVAL;
364 }
365
366 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
367 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
368 priv->membase + UART_FCR);
369 iowrite8(priv->fcr, priv->membase + UART_FCR);
370
371 return 0;
372}
373
374static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
375 unsigned int dmamode,
376 unsigned int fifo_size, unsigned int trigger)
377{
378 u8 fcr;
379
380 if (dmamode & ~PCH_UART_FCR_DMS) {
381 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
382 __func__, dmamode);
383 return -EINVAL;
384 }
385
386 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
387 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
388 __func__, fifo_size);
389 return -EINVAL;
390 }
391
392 if (trigger & ~PCH_UART_FCR_RFTL) {
393 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
394 __func__, trigger);
395 return -EINVAL;
396 }
397
398 switch (priv->fifo_size) {
399 case 256:
400 priv->trigger_level =
401 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
402 break;
403 case 64:
404 priv->trigger_level =
405 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
406 break;
407 case 16:
408 priv->trigger_level =
409 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
410 break;
411 default:
412 priv->trigger_level =
413 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
414 break;
415 }
416 fcr =
417 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
418 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
419 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
420 priv->membase + UART_FCR);
421 iowrite8(fcr, priv->membase + UART_FCR);
422 priv->fcr = fcr;
423
424 return 0;
425}
426
427static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
428{
429 priv->dmsr = 0;
430 return get_msr(priv, priv->membase);
431}
432
433static void pch_uart_hal_write(struct eg20t_port *priv,
434 const unsigned char *buf, int tx_size)
435{
436 int i;
437 unsigned int thr;
438
439 for (i = 0; i < tx_size;) {
440 thr = buf[i++];
441 iowrite8(thr, priv->membase + PCH_UART_THR);
442 }
443}
444
445static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
446 int rx_size)
447{
448 int i;
449 u8 rbr, lsr;
450
451 lsr = ioread8(priv->membase + UART_LSR);
452 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
453 i < rx_size && lsr & UART_LSR_DR;
454 lsr = ioread8(priv->membase + UART_LSR)) {
455 rbr = ioread8(priv->membase + PCH_UART_RBR);
456 buf[i++] = rbr;
457 }
458 return i;
459}
460
461static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
462{
463 unsigned int iir;
464 int ret;
465
466 iir = ioread8(priv->membase + UART_IIR);
467 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
468 return ret;
469}
470
471static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
472{
473 return ioread8(priv->membase + UART_LSR);
474}
475
476static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
477{
478 unsigned int lcr;
479
480 lcr = ioread8(priv->membase + UART_LCR);
481 if (on)
482 lcr |= PCH_UART_LCR_SB;
483 else
484 lcr &= ~PCH_UART_LCR_SB;
485
486 iowrite8(lcr, priv->membase + UART_LCR);
487}
488
489static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
490 int size)
491{
492 struct uart_port *port;
493 struct tty_struct *tty;
494
495 port = &priv->port;
496 tty = tty_port_tty_get(&port->state->port);
497 if (!tty) {
498 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
499 return -EBUSY;
500 }
501
502 tty_insert_flip_string(tty, buf, size);
503 tty_flip_buffer_push(tty);
504 tty_kref_put(tty);
505
506 return 0;
507}
508
509static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
510{
511 int ret;
512 struct uart_port *port = &priv->port;
513
514 if (port->x_char) {
515 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
516 __func__, port->x_char, jiffies);
517 buf[0] = port->x_char;
518 port->x_char = 0;
519 ret = 1;
520 } else {
521 ret = 0;
522 }
523
524 return ret;
525}
526
527static int dma_push_rx(struct eg20t_port *priv, int size)
528{
529 struct tty_struct *tty;
530 int room;
531 struct uart_port *port = &priv->port;
532
533 port = &priv->port;
534 tty = tty_port_tty_get(&port->state->port);
535 if (!tty) {
536 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
537 return 0;
538 }
539
540 room = tty_buffer_request_room(tty, size);
541
542 if (room < size)
543 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
544 size - room);
545 if (!room)
546 return room;
547
548 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
549
550 port->icount.rx += room;
551 tty_kref_put(tty);
552
553 return room;
554}
555
556static void pch_free_dma(struct uart_port *port)
557{
558 struct eg20t_port *priv;
559 priv = container_of(port, struct eg20t_port, port);
560
561 if (priv->chan_tx) {
562 dma_release_channel(priv->chan_tx);
563 priv->chan_tx = NULL;
564 }
565 if (priv->chan_rx) {
566 dma_release_channel(priv->chan_rx);
567 priv->chan_rx = NULL;
568 }
569 if (sg_dma_address(&priv->sg_rx))
570 dma_free_coherent(port->dev, port->fifosize,
571 sg_virt(&priv->sg_rx),
572 sg_dma_address(&priv->sg_rx));
573
574 return;
575}
576
577static bool filter(struct dma_chan *chan, void *slave)
578{
579 struct pch_dma_slave *param = slave;
580
581 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
582 chan->device->dev)) {
583 chan->private = param;
584 return true;
585 } else {
586 return false;
587 }
588}
589
590static void pch_request_dma(struct uart_port *port)
591{
592 dma_cap_mask_t mask;
593 struct dma_chan *chan;
594 struct pci_dev *dma_dev;
595 struct pch_dma_slave *param;
596 struct eg20t_port *priv =
597 container_of(port, struct eg20t_port, port);
598 dma_cap_zero(mask);
599 dma_cap_set(DMA_SLAVE, mask);
600
601 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
602 information */
603 /* Set Tx DMA */
604 param = &priv->param_tx;
605 param->dma_dev = &dma_dev->dev;
606 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
607
608 param->tx_reg = port->mapbase + UART_TX;
609 chan = dma_request_channel(mask, filter, param);
610 if (!chan) {
611 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
612 __func__);
613 return;
614 }
615 priv->chan_tx = chan;
616
617 /* Set Rx DMA */
618 param = &priv->param_rx;
619 param->dma_dev = &dma_dev->dev;
620 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
621
622 param->rx_reg = port->mapbase + UART_RX;
623 chan = dma_request_channel(mask, filter, param);
624 if (!chan) {
625 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
626 __func__);
627 dma_release_channel(priv->chan_tx);
628 return;
629 }
630
631 /* Get Consistent memory for DMA */
632 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
633 &priv->rx_buf_dma, GFP_KERNEL);
634 priv->chan_rx = chan;
635}
636
637static void pch_dma_rx_complete(void *arg)
638{
639 struct eg20t_port *priv = arg;
640 struct uart_port *port = &priv->port;
641 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
642 int count;
643
644 if (!tty) {
645 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
646 return;
647 }
648
649 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
650 count = dma_push_rx(priv, priv->trigger_level);
651 if (count)
652 tty_flip_buffer_push(tty);
653 tty_kref_put(tty);
654 async_tx_ack(priv->desc_rx);
655 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
656}
657
658static void pch_dma_tx_complete(void *arg)
659{
660 struct eg20t_port *priv = arg;
661 struct uart_port *port = &priv->port;
662 struct circ_buf *xmit = &port->state->xmit;
663 struct scatterlist *sg = priv->sg_tx_p;
664 int i;
665
666 for (i = 0; i < priv->nent; i++, sg++) {
667 xmit->tail += sg_dma_len(sg);
668 port->icount.tx += sg_dma_len(sg);
669 }
670 xmit->tail &= UART_XMIT_SIZE - 1;
671 async_tx_ack(priv->desc_tx);
672 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
673 priv->tx_dma_use = 0;
674 priv->nent = 0;
675 kfree(priv->sg_tx_p);
676 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
677}
678
679static int pop_tx(struct eg20t_port *priv, int size)
680{
681 int count = 0;
682 struct uart_port *port = &priv->port;
683 struct circ_buf *xmit = &port->state->xmit;
684
685 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
686 goto pop_tx_end;
687
688 do {
689 int cnt_to_end =
690 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
691 int sz = min(size - count, cnt_to_end);
692 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
693 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
694 count += sz;
695 } while (!uart_circ_empty(xmit) && count < size);
696
697pop_tx_end:
698 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
699 count, size - count, jiffies);
700
701 return count;
702}
703
704static int handle_rx_to(struct eg20t_port *priv)
705{
706 struct pch_uart_buffer *buf;
707 int rx_size;
708 int ret;
709 if (!priv->start_rx) {
710 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
711 return 0;
712 }
713 buf = &priv->rxbuf;
714 do {
715 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
716 ret = push_rx(priv, buf->buf, rx_size);
717 if (ret)
718 return 0;
719 } while (rx_size == buf->size);
720
721 return PCH_UART_HANDLED_RX_INT;
722}
723
724static int handle_rx(struct eg20t_port *priv)
725{
726 return handle_rx_to(priv);
727}
728
729static int dma_handle_rx(struct eg20t_port *priv)
730{
731 struct uart_port *port = &priv->port;
732 struct dma_async_tx_descriptor *desc;
733 struct scatterlist *sg;
734
735 priv = container_of(port, struct eg20t_port, port);
736 sg = &priv->sg_rx;
737
738 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
739
740 sg_dma_len(sg) = priv->trigger_level;
741
742 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
743 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
744 ~PAGE_MASK);
745
746 sg_dma_address(sg) = priv->rx_buf_dma;
747
748 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
749 sg, 1, DMA_FROM_DEVICE,
750 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
751
752 if (!desc)
753 return 0;
754
755 priv->desc_rx = desc;
756 desc->callback = pch_dma_rx_complete;
757 desc->callback_param = priv;
758 desc->tx_submit(desc);
759 dma_async_issue_pending(priv->chan_rx);
760
761 return PCH_UART_HANDLED_RX_INT;
762}
763
764static unsigned int handle_tx(struct eg20t_port *priv)
765{
766 struct uart_port *port = &priv->port;
767 struct circ_buf *xmit = &port->state->xmit;
768 int fifo_size;
769 int tx_size;
770 int size;
771 int tx_empty;
772
773 if (!priv->start_tx) {
774 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
775 __func__, jiffies);
776 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
777 priv->tx_empty = 1;
778 return 0;
779 }
780
781 fifo_size = max(priv->fifo_size, 1);
782 tx_empty = 1;
783 if (pop_tx_x(priv, xmit->buf)) {
784 pch_uart_hal_write(priv, xmit->buf, 1);
785 port->icount.tx++;
786 tx_empty = 0;
787 fifo_size--;
788 }
789 size = min(xmit->head - xmit->tail, fifo_size);
790 if (size < 0)
791 size = fifo_size;
792
793 tx_size = pop_tx(priv, size);
794 if (tx_size > 0) {
795 port->icount.tx += tx_size;
796 tx_empty = 0;
797 }
798
799 priv->tx_empty = tx_empty;
800
801 if (tx_empty) {
802 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
803 uart_write_wakeup(port);
804 }
805
806 return PCH_UART_HANDLED_TX_INT;
807}
808
809static unsigned int dma_handle_tx(struct eg20t_port *priv)
810{
811 struct uart_port *port = &priv->port;
812 struct circ_buf *xmit = &port->state->xmit;
813 struct scatterlist *sg;
814 int nent;
815 int fifo_size;
816 int tx_empty;
817 struct dma_async_tx_descriptor *desc;
818 int num;
819 int i;
820 int bytes;
821 int size;
822 int rem;
823
824 if (!priv->start_tx) {
825 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
826 __func__, jiffies);
827 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
828 priv->tx_empty = 1;
829 return 0;
830 }
831
832 if (priv->tx_dma_use) {
833 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
834 __func__, jiffies);
835 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
836 priv->tx_empty = 1;
837 return 0;
838 }
839
840 fifo_size = max(priv->fifo_size, 1);
841 tx_empty = 1;
842 if (pop_tx_x(priv, xmit->buf)) {
843 pch_uart_hal_write(priv, xmit->buf, 1);
844 port->icount.tx++;
845 tx_empty = 0;
846 fifo_size--;
847 }
848
849 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
850 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
851 xmit->tail, UART_XMIT_SIZE));
852 if (!bytes) {
853 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
854 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
855 uart_write_wakeup(port);
856 return 0;
857 }
858
859 if (bytes > fifo_size) {
860 num = bytes / fifo_size + 1;
861 size = fifo_size;
862 rem = bytes % fifo_size;
863 } else {
864 num = 1;
865 size = bytes;
866 rem = bytes;
867 }
868
869 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
870 __func__, num, size, rem);
871
872 priv->tx_dma_use = 1;
873
874 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
875
876 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
877 sg = priv->sg_tx_p;
878
879 for (i = 0; i < num; i++, sg++) {
880 if (i == (num - 1))
881 sg_set_page(sg, virt_to_page(xmit->buf),
882 rem, fifo_size * i);
883 else
884 sg_set_page(sg, virt_to_page(xmit->buf),
885 size, fifo_size * i);
886 }
887
888 sg = priv->sg_tx_p;
889 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
890 if (!nent) {
891 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
892 return 0;
893 }
894 priv->nent = nent;
895
896 for (i = 0; i < nent; i++, sg++) {
897 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
898 fifo_size * i;
899 sg_dma_address(sg) = (sg_dma_address(sg) &
900 ~(UART_XMIT_SIZE - 1)) + sg->offset;
901 if (i == (nent - 1))
902 sg_dma_len(sg) = rem;
903 else
904 sg_dma_len(sg) = size;
905 }
906
907 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
908 priv->sg_tx_p, nent, DMA_TO_DEVICE,
909 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
910 if (!desc) {
911 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
912 __func__);
913 return 0;
914 }
915 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
916 priv->desc_tx = desc;
917 desc->callback = pch_dma_tx_complete;
918 desc->callback_param = priv;
919
920 desc->tx_submit(desc);
921
922 dma_async_issue_pending(priv->chan_tx);
923
924 return PCH_UART_HANDLED_TX_INT;
925}
926
927static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
928{
929 u8 fcr = ioread8(priv->membase + UART_FCR);
930
931 /* Reset FIFO */
932 fcr |= UART_FCR_CLEAR_RCVR;
933 iowrite8(fcr, priv->membase + UART_FCR);
934
935 if (lsr & PCH_UART_LSR_ERR)
936 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
937
938 if (lsr & UART_LSR_FE)
939 dev_err(&priv->pdev->dev, "Framing Error\n");
940
941 if (lsr & UART_LSR_PE)
942 dev_err(&priv->pdev->dev, "Parity Error\n");
943
944 if (lsr & UART_LSR_OE)
945 dev_err(&priv->pdev->dev, "Overrun Error\n");
946}
947
948static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
949{
950 struct eg20t_port *priv = dev_id;
951 unsigned int handled;
952 u8 lsr;
953 int ret = 0;
954 unsigned int iid;
955 unsigned long flags;
956
957 spin_lock_irqsave(&priv->port.lock, flags);
958 handled = 0;
959 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
960 switch (iid) {
961 case PCH_UART_IID_RLS: /* Receiver Line Status */
962 lsr = pch_uart_hal_get_line_status(priv);
963 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
964 UART_LSR_PE | UART_LSR_OE)) {
965 pch_uart_err_ir(priv, lsr);
966 ret = PCH_UART_HANDLED_RX_ERR_INT;
967 }
968 break;
969 case PCH_UART_IID_RDR: /* Received Data Ready */
970 if (priv->use_dma) {
971 pch_uart_hal_disable_interrupt(priv,
972 PCH_UART_HAL_RX_INT);
973 ret = dma_handle_rx(priv);
974 if (!ret)
975 pch_uart_hal_enable_interrupt(priv,
976 PCH_UART_HAL_RX_INT);
977 } else {
978 ret = handle_rx(priv);
979 }
980 break;
981 case PCH_UART_IID_RDR_TO: /* Received Data Ready
982 (FIFO Timeout) */
983 ret = handle_rx_to(priv);
984 break;
985 case PCH_UART_IID_THRE: /* Transmitter Holding Register
986 Empty */
987 if (priv->use_dma)
988 ret = dma_handle_tx(priv);
989 else
990 ret = handle_tx(priv);
991 break;
992 case PCH_UART_IID_MS: /* Modem Status */
993 ret = PCH_UART_HANDLED_MS_INT;
994 break;
995 default: /* Never junp to this label */
996 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
997 iid, jiffies);
998 ret = -1;
999 break;
1000 }
1001 handled |= (unsigned int)ret;
1002 }
1003 if (handled == 0 && iid <= 1) {
1004 if (priv->int_dis_flag)
1005 priv->int_dis_flag = 0;
1006 }
1007
1008 spin_unlock_irqrestore(&priv->port.lock, flags);
1009 return IRQ_RETVAL(handled);
1010}
1011
1012/* This function tests whether the transmitter fifo and shifter for the port
1013 described by 'port' is empty. */
1014static unsigned int pch_uart_tx_empty(struct uart_port *port)
1015{
1016 struct eg20t_port *priv;
1017 int ret;
1018 priv = container_of(port, struct eg20t_port, port);
1019 if (priv->tx_empty)
1020 ret = TIOCSER_TEMT;
1021 else
1022 ret = 0;
1023
1024 return ret;
1025}
1026
1027/* Returns the current state of modem control inputs. */
1028static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1029{
1030 struct eg20t_port *priv;
1031 u8 modem;
1032 unsigned int ret = 0;
1033
1034 priv = container_of(port, struct eg20t_port, port);
1035 modem = pch_uart_hal_get_modem(priv);
1036
1037 if (modem & UART_MSR_DCD)
1038 ret |= TIOCM_CAR;
1039
1040 if (modem & UART_MSR_RI)
1041 ret |= TIOCM_RNG;
1042
1043 if (modem & UART_MSR_DSR)
1044 ret |= TIOCM_DSR;
1045
1046 if (modem & UART_MSR_CTS)
1047 ret |= TIOCM_CTS;
1048
1049 return ret;
1050}
1051
1052static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1053{
1054 u32 mcr = 0;
1055 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1056
1057 if (mctrl & TIOCM_DTR)
1058 mcr |= UART_MCR_DTR;
1059 if (mctrl & TIOCM_RTS)
1060 mcr |= UART_MCR_RTS;
1061 if (mctrl & TIOCM_LOOP)
1062 mcr |= UART_MCR_LOOP;
1063
1064 if (priv->mcr & UART_MCR_AFE)
1065 mcr |= UART_MCR_AFE;
1066
1067 if (mctrl)
1068 iowrite8(mcr, priv->membase + UART_MCR);
1069}
1070
1071static void pch_uart_stop_tx(struct uart_port *port)
1072{
1073 struct eg20t_port *priv;
1074 priv = container_of(port, struct eg20t_port, port);
1075 priv->start_tx = 0;
1076 priv->tx_dma_use = 0;
1077}
1078
1079static void pch_uart_start_tx(struct uart_port *port)
1080{
1081 struct eg20t_port *priv;
1082
1083 priv = container_of(port, struct eg20t_port, port);
1084
1085 if (priv->use_dma) {
1086 if (priv->tx_dma_use) {
1087 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1088 __func__);
1089 return;
1090 }
1091 }
1092
1093 priv->start_tx = 1;
1094 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1095}
1096
1097static void pch_uart_stop_rx(struct uart_port *port)
1098{
1099 struct eg20t_port *priv;
1100 priv = container_of(port, struct eg20t_port, port);
1101 priv->start_rx = 0;
1102 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1103 priv->int_dis_flag = 1;
1104}
1105
1106/* Enable the modem status interrupts. */
1107static void pch_uart_enable_ms(struct uart_port *port)
1108{
1109 struct eg20t_port *priv;
1110 priv = container_of(port, struct eg20t_port, port);
1111 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1112}
1113
1114/* Control the transmission of a break signal. */
1115static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1116{
1117 struct eg20t_port *priv;
1118 unsigned long flags;
1119
1120 priv = container_of(port, struct eg20t_port, port);
1121 spin_lock_irqsave(&port->lock, flags);
1122 pch_uart_hal_set_break(priv, ctl);
1123 spin_unlock_irqrestore(&port->lock, flags);
1124}
1125
1126/* Grab any interrupt resources and initialise any low level driver state. */
1127static int pch_uart_startup(struct uart_port *port)
1128{
1129 struct eg20t_port *priv;
1130 int ret;
1131 int fifo_size;
1132 int trigger_level;
1133
1134 priv = container_of(port, struct eg20t_port, port);
1135 priv->tx_empty = 1;
1136
1137 if (port->uartclk)
1138 priv->base_baud = port->uartclk;
1139 else
1140 port->uartclk = priv->base_baud;
1141
1142 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1143 ret = pch_uart_hal_set_line(priv, default_baud,
1144 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1145 PCH_UART_HAL_STB1);
1146 if (ret)
1147 return ret;
1148
1149 switch (priv->fifo_size) {
1150 case 256:
1151 fifo_size = PCH_UART_HAL_FIFO256;
1152 break;
1153 case 64:
1154 fifo_size = PCH_UART_HAL_FIFO64;
1155 break;
1156 case 16:
1157 fifo_size = PCH_UART_HAL_FIFO16;
1158 case 1:
1159 default:
1160 fifo_size = PCH_UART_HAL_FIFO_DIS;
1161 break;
1162 }
1163
1164 switch (priv->trigger) {
1165 case PCH_UART_HAL_TRIGGER1:
1166 trigger_level = 1;
1167 break;
1168 case PCH_UART_HAL_TRIGGER_L:
1169 trigger_level = priv->fifo_size / 4;
1170 break;
1171 case PCH_UART_HAL_TRIGGER_M:
1172 trigger_level = priv->fifo_size / 2;
1173 break;
1174 case PCH_UART_HAL_TRIGGER_H:
1175 default:
1176 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1177 break;
1178 }
1179
1180 priv->trigger_level = trigger_level;
1181 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1182 fifo_size, priv->trigger);
1183 if (ret < 0)
1184 return ret;
1185
1186 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1187 KBUILD_MODNAME, priv);
1188 if (ret < 0)
1189 return ret;
1190
1191 if (priv->use_dma)
1192 pch_request_dma(port);
1193
1194 priv->start_rx = 1;
1195 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1196 uart_update_timeout(port, CS8, default_baud);
1197
1198 return 0;
1199}
1200
1201static void pch_uart_shutdown(struct uart_port *port)
1202{
1203 struct eg20t_port *priv;
1204 int ret;
1205
1206 priv = container_of(port, struct eg20t_port, port);
1207 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1208 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1209 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1210 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1211 if (ret)
1212 dev_err(priv->port.dev,
1213 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1214
1215 if (priv->use_dma_flag)
1216 pch_free_dma(port);
1217
1218 free_irq(priv->port.irq, priv);
1219}
1220
1221/* Change the port parameters, including word length, parity, stop
1222 *bits. Update read_status_mask and ignore_status_mask to indicate
1223 *the types of events we are interested in receiving. */
1224static void pch_uart_set_termios(struct uart_port *port,
1225 struct ktermios *termios, struct ktermios *old)
1226{
1227 int baud;
1228 int rtn;
1229 unsigned int parity, bits, stb;
1230 struct eg20t_port *priv;
1231 unsigned long flags;
1232
1233 priv = container_of(port, struct eg20t_port, port);
1234 switch (termios->c_cflag & CSIZE) {
1235 case CS5:
1236 bits = PCH_UART_HAL_5BIT;
1237 break;
1238 case CS6:
1239 bits = PCH_UART_HAL_6BIT;
1240 break;
1241 case CS7:
1242 bits = PCH_UART_HAL_7BIT;
1243 break;
1244 default: /* CS8 */
1245 bits = PCH_UART_HAL_8BIT;
1246 break;
1247 }
1248 if (termios->c_cflag & CSTOPB)
1249 stb = PCH_UART_HAL_STB2;
1250 else
1251 stb = PCH_UART_HAL_STB1;
1252
1253 if (termios->c_cflag & PARENB) {
1254 if (!(termios->c_cflag & PARODD))
1255 parity = PCH_UART_HAL_PARITY_ODD;
1256 else
1257 parity = PCH_UART_HAL_PARITY_EVEN;
1258
1259 } else {
1260 parity = PCH_UART_HAL_PARITY_NONE;
1261 }
1262
1263 /* Only UART0 has auto hardware flow function */
1264 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1265 priv->mcr |= UART_MCR_AFE;
1266 else
1267 priv->mcr &= ~UART_MCR_AFE;
1268
1269 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1270
1271 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1272
1273 spin_lock_irqsave(&port->lock, flags);
1274
1275 uart_update_timeout(port, termios->c_cflag, baud);
1276 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1277 if (rtn)
1278 goto out;
1279
1280 /* Don't rewrite B0 */
1281 if (tty_termios_baud_rate(termios))
1282 tty_termios_encode_baud_rate(termios, baud, baud);
1283
1284out:
1285 spin_unlock_irqrestore(&port->lock, flags);
1286}
1287
1288static const char *pch_uart_type(struct uart_port *port)
1289{
1290 return KBUILD_MODNAME;
1291}
1292
1293static void pch_uart_release_port(struct uart_port *port)
1294{
1295 struct eg20t_port *priv;
1296
1297 priv = container_of(port, struct eg20t_port, port);
1298 pci_iounmap(priv->pdev, priv->membase);
1299 pci_release_regions(priv->pdev);
1300}
1301
1302static int pch_uart_request_port(struct uart_port *port)
1303{
1304 struct eg20t_port *priv;
1305 int ret;
1306 void __iomem *membase;
1307
1308 priv = container_of(port, struct eg20t_port, port);
1309 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1310 if (ret < 0)
1311 return -EBUSY;
1312
1313 membase = pci_iomap(priv->pdev, 1, 0);
1314 if (!membase) {
1315 pci_release_regions(priv->pdev);
1316 return -EBUSY;
1317 }
1318 priv->membase = port->membase = membase;
1319
1320 return 0;
1321}
1322
1323static void pch_uart_config_port(struct uart_port *port, int type)
1324{
1325 struct eg20t_port *priv;
1326
1327 priv = container_of(port, struct eg20t_port, port);
1328 if (type & UART_CONFIG_TYPE) {
1329 port->type = priv->port_type;
1330 pch_uart_request_port(port);
1331 }
1332}
1333
1334static int pch_uart_verify_port(struct uart_port *port,
1335 struct serial_struct *serinfo)
1336{
1337 struct eg20t_port *priv;
1338
1339 priv = container_of(port, struct eg20t_port, port);
1340 if (serinfo->flags & UPF_LOW_LATENCY) {
1341 dev_info(priv->port.dev,
1342 "PCH UART : Use PIO Mode (without DMA)\n");
1343 priv->use_dma = 0;
1344 serinfo->flags &= ~UPF_LOW_LATENCY;
1345 } else {
1346#ifndef CONFIG_PCH_DMA
1347 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1348 __func__);
1349 return -EOPNOTSUPP;
1350#endif
1351 priv->use_dma = 1;
1352 priv->use_dma_flag = 1;
1353 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
1354 }
1355
1356 return 0;
1357}
1358
1359static struct uart_ops pch_uart_ops = {
1360 .tx_empty = pch_uart_tx_empty,
1361 .set_mctrl = pch_uart_set_mctrl,
1362 .get_mctrl = pch_uart_get_mctrl,
1363 .stop_tx = pch_uart_stop_tx,
1364 .start_tx = pch_uart_start_tx,
1365 .stop_rx = pch_uart_stop_rx,
1366 .enable_ms = pch_uart_enable_ms,
1367 .break_ctl = pch_uart_break_ctl,
1368 .startup = pch_uart_startup,
1369 .shutdown = pch_uart_shutdown,
1370 .set_termios = pch_uart_set_termios,
1371/* .pm = pch_uart_pm, Not supported yet */
1372/* .set_wake = pch_uart_set_wake, Not supported yet */
1373 .type = pch_uart_type,
1374 .release_port = pch_uart_release_port,
1375 .request_port = pch_uart_request_port,
1376 .config_port = pch_uart_config_port,
1377 .verify_port = pch_uart_verify_port
1378};
1379
1380static struct uart_driver pch_uart_driver = {
1381 .owner = THIS_MODULE,
1382 .driver_name = KBUILD_MODNAME,
1383 .dev_name = PCH_UART_DRIVER_DEVICE,
1384 .major = 0,
1385 .minor = 0,
1386 .nr = PCH_UART_NR,
1387};
1388
1389static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1390 const struct pci_device_id *id)
1391{
1392 struct eg20t_port *priv;
1393 int ret;
1394 unsigned int iobase;
1395 unsigned int mapbase;
1396 unsigned char *rxbuf;
1397 int fifosize, base_baud;
1398 int port_type;
1399 struct pch_uart_driver_data *board;
1400 const char *board_name;
1401
1402 board = &drv_dat[id->driver_data];
1403 port_type = board->port_type;
1404
1405 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1406 if (priv == NULL)
1407 goto init_port_alloc_err;
1408
1409 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1410 if (!rxbuf)
1411 goto init_port_free_txbuf;
1412
1413 base_baud = 1843200; /* 1.8432MHz */
1414
1415 /* quirk for CM-iTC board */
1416 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1417 if (board_name && strstr(board_name, "CM-iTC"))
1418 base_baud = 192000000; /* 192.0MHz */
1419
1420 switch (port_type) {
1421 case PORT_UNKNOWN:
1422 fifosize = 256; /* EG20T/ML7213: UART0 */
1423 break;
1424 case PORT_8250:
1425 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
1426 break;
1427 default:
1428 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1429 goto init_port_hal_free;
1430 }
1431
1432 iobase = pci_resource_start(pdev, 0);
1433 mapbase = pci_resource_start(pdev, 1);
1434 priv->mapbase = mapbase;
1435 priv->iobase = iobase;
1436 priv->pdev = pdev;
1437 priv->tx_empty = 1;
1438 priv->rxbuf.buf = rxbuf;
1439 priv->rxbuf.size = PAGE_SIZE;
1440
1441 priv->fifo_size = fifosize;
1442 priv->base_baud = base_baud;
1443 priv->port_type = PORT_MAX_8250 + port_type + 1;
1444 priv->port.dev = &pdev->dev;
1445 priv->port.iobase = iobase;
1446 priv->port.membase = NULL;
1447 priv->port.mapbase = mapbase;
1448 priv->port.irq = pdev->irq;
1449 priv->port.iotype = UPIO_PORT;
1450 priv->port.ops = &pch_uart_ops;
1451 priv->port.flags = UPF_BOOT_AUTOCONF;
1452 priv->port.fifosize = fifosize;
1453 priv->port.line = board->line_no;
1454 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1455
1456 spin_lock_init(&priv->port.lock);
1457
1458 pci_set_drvdata(pdev, priv);
1459 pch_uart_hal_request(pdev, fifosize, base_baud);
1460
1461 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1462 if (ret < 0)
1463 goto init_port_hal_free;
1464
1465 return priv;
1466
1467init_port_hal_free:
1468 free_page((unsigned long)rxbuf);
1469init_port_free_txbuf:
1470 kfree(priv);
1471init_port_alloc_err:
1472
1473 return NULL;
1474}
1475
1476static void pch_uart_exit_port(struct eg20t_port *priv)
1477{
1478 uart_remove_one_port(&pch_uart_driver, &priv->port);
1479 pci_set_drvdata(priv->pdev, NULL);
1480 free_page((unsigned long)priv->rxbuf.buf);
1481}
1482
1483static void pch_uart_pci_remove(struct pci_dev *pdev)
1484{
1485 struct eg20t_port *priv;
1486
1487 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1488 pch_uart_exit_port(priv);
1489 pci_disable_device(pdev);
1490 kfree(priv);
1491 return;
1492}
1493#ifdef CONFIG_PM
1494static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1495{
1496 struct eg20t_port *priv = pci_get_drvdata(pdev);
1497
1498 uart_suspend_port(&pch_uart_driver, &priv->port);
1499
1500 pci_save_state(pdev);
1501 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1502 return 0;
1503}
1504
1505static int pch_uart_pci_resume(struct pci_dev *pdev)
1506{
1507 struct eg20t_port *priv = pci_get_drvdata(pdev);
1508 int ret;
1509
1510 pci_set_power_state(pdev, PCI_D0);
1511 pci_restore_state(pdev);
1512
1513 ret = pci_enable_device(pdev);
1514 if (ret) {
1515 dev_err(&pdev->dev,
1516 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1517 return ret;
1518 }
1519
1520 uart_resume_port(&pch_uart_driver, &priv->port);
1521
1522 return 0;
1523}
1524#else
1525#define pch_uart_pci_suspend NULL
1526#define pch_uart_pci_resume NULL
1527#endif
1528
1529static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1530 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1531 .driver_data = pch_et20t_uart0},
1532 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1533 .driver_data = pch_et20t_uart1},
1534 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1535 .driver_data = pch_et20t_uart2},
1536 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1537 .driver_data = pch_et20t_uart3},
1538 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1539 .driver_data = pch_ml7213_uart0},
1540 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1541 .driver_data = pch_ml7213_uart1},
1542 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1543 .driver_data = pch_ml7213_uart2},
1544 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1545 .driver_data = pch_ml7223_uart0},
1546 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1547 .driver_data = pch_ml7223_uart1},
1548 {0,},
1549};
1550
1551static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1552 const struct pci_device_id *id)
1553{
1554 int ret;
1555 struct eg20t_port *priv;
1556
1557 ret = pci_enable_device(pdev);
1558 if (ret < 0)
1559 goto probe_error;
1560
1561 priv = pch_uart_init_port(pdev, id);
1562 if (!priv) {
1563 ret = -EBUSY;
1564 goto probe_disable_device;
1565 }
1566 pci_set_drvdata(pdev, priv);
1567
1568 return ret;
1569
1570probe_disable_device:
1571 pci_disable_device(pdev);
1572probe_error:
1573 return ret;
1574}
1575
1576static struct pci_driver pch_uart_pci_driver = {
1577 .name = "pch_uart",
1578 .id_table = pch_uart_pci_id,
1579 .probe = pch_uart_pci_probe,
1580 .remove = __devexit_p(pch_uart_pci_remove),
1581 .suspend = pch_uart_pci_suspend,
1582 .resume = pch_uart_pci_resume,
1583};
1584
1585static int __init pch_uart_module_init(void)
1586{
1587 int ret;
1588
1589 /* register as UART driver */
1590 ret = uart_register_driver(&pch_uart_driver);
1591 if (ret < 0)
1592 return ret;
1593
1594 /* register as PCI driver */
1595 ret = pci_register_driver(&pch_uart_pci_driver);
1596 if (ret < 0)
1597 uart_unregister_driver(&pch_uart_driver);
1598
1599 return ret;
1600}
1601module_init(pch_uart_module_init);
1602
1603static void __exit pch_uart_module_exit(void)
1604{
1605 pci_unregister_driver(&pch_uart_pci_driver);
1606 uart_unregister_driver(&pch_uart_driver);
1607}
1608module_exit(pch_uart_module_exit);
1609
1610MODULE_LICENSE("GPL v2");
1611MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1612module_param(default_baud, uint, S_IRUGO);
diff --git a/drivers/tty/serial/pmac_zilog.c b/drivers/tty/serial/pmac_zilog.c
new file mode 100644
index 000000000000..5acd24a27d08
--- /dev/null
+++ b/drivers/tty/serial/pmac_zilog.c
@@ -0,0 +1,2206 @@
1/*
2 * Driver for PowerMac Z85c30 based ESCC cell found in the
3 * "macio" ASICs of various PowerMac models
4 *
5 * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 *
7 * Derived from drivers/macintosh/macserial.c by Paul Mackerras
8 * and drivers/serial/sunzilog.c by David S. Miller
9 *
10 * Hrm... actually, I ripped most of sunzilog (Thanks David !) and
11 * adapted special tweaks needed for us. I don't think it's worth
12 * merging back those though. The DMA code still has to get in
13 * and once done, I expect that driver to remain fairly stable in
14 * the long term, unless we change the driver model again...
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * 2004-08-06 Harald Welte <laforge@gnumonks.org>
31 * - Enable BREAK interrupt
32 * - Add support for sysreq
33 *
34 * TODO: - Add DMA support
35 * - Defer port shutdown to a few seconds after close
36 * - maybe put something right into uap->clk_divisor
37 */
38
39#undef DEBUG
40#undef DEBUG_HARD
41#undef USE_CTRL_O_SYSRQ
42
43#include <linux/module.h>
44#include <linux/tty.h>
45
46#include <linux/tty_flip.h>
47#include <linux/major.h>
48#include <linux/string.h>
49#include <linux/fcntl.h>
50#include <linux/mm.h>
51#include <linux/kernel.h>
52#include <linux/delay.h>
53#include <linux/init.h>
54#include <linux/console.h>
55#include <linux/adb.h>
56#include <linux/pmu.h>
57#include <linux/bitops.h>
58#include <linux/sysrq.h>
59#include <linux/mutex.h>
60#include <asm/sections.h>
61#include <asm/io.h>
62#include <asm/irq.h>
63
64#ifdef CONFIG_PPC_PMAC
65#include <asm/prom.h>
66#include <asm/machdep.h>
67#include <asm/pmac_feature.h>
68#include <asm/dbdma.h>
69#include <asm/macio.h>
70#else
71#include <linux/platform_device.h>
72#define of_machine_is_compatible(x) (0)
73#endif
74
75#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
76#define SUPPORT_SYSRQ
77#endif
78
79#include <linux/serial.h>
80#include <linux/serial_core.h>
81
82#include "pmac_zilog.h"
83
84/* Not yet implemented */
85#undef HAS_DBDMA
86
87static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
88MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
89MODULE_DESCRIPTION("Driver for the Mac and PowerMac serial ports.");
90MODULE_LICENSE("GPL");
91
92#ifdef CONFIG_SERIAL_PMACZILOG_TTYS
93#define PMACZILOG_MAJOR TTY_MAJOR
94#define PMACZILOG_MINOR 64
95#define PMACZILOG_NAME "ttyS"
96#else
97#define PMACZILOG_MAJOR 204
98#define PMACZILOG_MINOR 192
99#define PMACZILOG_NAME "ttyPZ"
100#endif
101
102
103/*
104 * For the sake of early serial console, we can do a pre-probe
105 * (optional) of the ports at rather early boot time.
106 */
107static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
108static int pmz_ports_count;
109static DEFINE_MUTEX(pmz_irq_mutex);
110
111static struct uart_driver pmz_uart_reg = {
112 .owner = THIS_MODULE,
113 .driver_name = PMACZILOG_NAME,
114 .dev_name = PMACZILOG_NAME,
115 .major = PMACZILOG_MAJOR,
116 .minor = PMACZILOG_MINOR,
117};
118
119
120/*
121 * Load all registers to reprogram the port
122 * This function must only be called when the TX is not busy. The UART
123 * port lock must be held and local interrupts disabled.
124 */
125static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
126{
127 int i;
128
129 if (ZS_IS_ASLEEP(uap))
130 return;
131
132 /* Let pending transmits finish. */
133 for (i = 0; i < 1000; i++) {
134 unsigned char stat = read_zsreg(uap, R1);
135 if (stat & ALL_SNT)
136 break;
137 udelay(100);
138 }
139
140 ZS_CLEARERR(uap);
141 zssync(uap);
142 ZS_CLEARFIFO(uap);
143 zssync(uap);
144 ZS_CLEARERR(uap);
145
146 /* Disable all interrupts. */
147 write_zsreg(uap, R1,
148 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
149
150 /* Set parity, sync config, stop bits, and clock divisor. */
151 write_zsreg(uap, R4, regs[R4]);
152
153 /* Set misc. TX/RX control bits. */
154 write_zsreg(uap, R10, regs[R10]);
155
156 /* Set TX/RX controls sans the enable bits. */
157 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
158 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
159
160 /* now set R7 "prime" on ESCC */
161 write_zsreg(uap, R15, regs[R15] | EN85C30);
162 write_zsreg(uap, R7, regs[R7P]);
163
164 /* make sure we use R7 "non-prime" on ESCC */
165 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
166
167 /* Synchronous mode config. */
168 write_zsreg(uap, R6, regs[R6]);
169 write_zsreg(uap, R7, regs[R7]);
170
171 /* Disable baud generator. */
172 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
173
174 /* Clock mode control. */
175 write_zsreg(uap, R11, regs[R11]);
176
177 /* Lower and upper byte of baud rate generator divisor. */
178 write_zsreg(uap, R12, regs[R12]);
179 write_zsreg(uap, R13, regs[R13]);
180
181 /* Now rewrite R14, with BRENAB (if set). */
182 write_zsreg(uap, R14, regs[R14]);
183
184 /* Reset external status interrupts. */
185 write_zsreg(uap, R0, RES_EXT_INT);
186 write_zsreg(uap, R0, RES_EXT_INT);
187
188 /* Rewrite R3/R5, this time without enables masked. */
189 write_zsreg(uap, R3, regs[R3]);
190 write_zsreg(uap, R5, regs[R5]);
191
192 /* Rewrite R1, this time without IRQ enabled masked. */
193 write_zsreg(uap, R1, regs[R1]);
194
195 /* Enable interrupts */
196 write_zsreg(uap, R9, regs[R9]);
197}
198
199/*
200 * We do like sunzilog to avoid disrupting pending Tx
201 * Reprogram the Zilog channel HW registers with the copies found in the
202 * software state struct. If the transmitter is busy, we defer this update
203 * until the next TX complete interrupt. Else, we do it right now.
204 *
205 * The UART port lock must be held and local interrupts disabled.
206 */
207static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
208{
209 if (!ZS_REGS_HELD(uap)) {
210 if (ZS_TX_ACTIVE(uap)) {
211 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
212 } else {
213 pmz_debug("pmz: maybe_update_regs: updating\n");
214 pmz_load_zsregs(uap, uap->curregs);
215 }
216 }
217}
218
219static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
220{
221 struct tty_struct *tty = NULL;
222 unsigned char ch, r1, drop, error, flag;
223 int loops = 0;
224
225 /* The interrupt can be enabled when the port isn't open, typically
226 * that happens when using one port is open and the other closed (stale
227 * interrupt) or when one port is used as a console.
228 */
229 if (!ZS_IS_OPEN(uap)) {
230 pmz_debug("pmz: draining input\n");
231 /* Port is closed, drain input data */
232 for (;;) {
233 if ((++loops) > 1000)
234 goto flood;
235 (void)read_zsreg(uap, R1);
236 write_zsreg(uap, R0, ERR_RES);
237 (void)read_zsdata(uap);
238 ch = read_zsreg(uap, R0);
239 if (!(ch & Rx_CH_AV))
240 break;
241 }
242 return NULL;
243 }
244
245 /* Sanity check, make sure the old bug is no longer happening */
246 if (uap->port.state == NULL || uap->port.state->port.tty == NULL) {
247 WARN_ON(1);
248 (void)read_zsdata(uap);
249 return NULL;
250 }
251 tty = uap->port.state->port.tty;
252
253 while (1) {
254 error = 0;
255 drop = 0;
256
257 r1 = read_zsreg(uap, R1);
258 ch = read_zsdata(uap);
259
260 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
261 write_zsreg(uap, R0, ERR_RES);
262 zssync(uap);
263 }
264
265 ch &= uap->parity_mask;
266 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
267 uap->flags &= ~PMACZILOG_FLAG_BREAK;
268 }
269
270#if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
271#ifdef USE_CTRL_O_SYSRQ
272 /* Handle the SysRq ^O Hack */
273 if (ch == '\x0f') {
274 uap->port.sysrq = jiffies + HZ*5;
275 goto next_char;
276 }
277#endif /* USE_CTRL_O_SYSRQ */
278 if (uap->port.sysrq) {
279 int swallow;
280 spin_unlock(&uap->port.lock);
281 swallow = uart_handle_sysrq_char(&uap->port, ch);
282 spin_lock(&uap->port.lock);
283 if (swallow)
284 goto next_char;
285 }
286#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
287
288 /* A real serial line, record the character and status. */
289 if (drop)
290 goto next_char;
291
292 flag = TTY_NORMAL;
293 uap->port.icount.rx++;
294
295 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
296 error = 1;
297 if (r1 & BRK_ABRT) {
298 pmz_debug("pmz: got break !\n");
299 r1 &= ~(PAR_ERR | CRC_ERR);
300 uap->port.icount.brk++;
301 if (uart_handle_break(&uap->port))
302 goto next_char;
303 }
304 else if (r1 & PAR_ERR)
305 uap->port.icount.parity++;
306 else if (r1 & CRC_ERR)
307 uap->port.icount.frame++;
308 if (r1 & Rx_OVR)
309 uap->port.icount.overrun++;
310 r1 &= uap->port.read_status_mask;
311 if (r1 & BRK_ABRT)
312 flag = TTY_BREAK;
313 else if (r1 & PAR_ERR)
314 flag = TTY_PARITY;
315 else if (r1 & CRC_ERR)
316 flag = TTY_FRAME;
317 }
318
319 if (uap->port.ignore_status_mask == 0xff ||
320 (r1 & uap->port.ignore_status_mask) == 0) {
321 tty_insert_flip_char(tty, ch, flag);
322 }
323 if (r1 & Rx_OVR)
324 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
325 next_char:
326 /* We can get stuck in an infinite loop getting char 0 when the
327 * line is in a wrong HW state, we break that here.
328 * When that happens, I disable the receive side of the driver.
329 * Note that what I've been experiencing is a real irq loop where
330 * I'm getting flooded regardless of the actual port speed.
331 * Something strange is going on with the HW
332 */
333 if ((++loops) > 1000)
334 goto flood;
335 ch = read_zsreg(uap, R0);
336 if (!(ch & Rx_CH_AV))
337 break;
338 }
339
340 return tty;
341 flood:
342 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
343 write_zsreg(uap, R1, uap->curregs[R1]);
344 zssync(uap);
345 pmz_error("pmz: rx irq flood !\n");
346 return tty;
347}
348
349static void pmz_status_handle(struct uart_pmac_port *uap)
350{
351 unsigned char status;
352
353 status = read_zsreg(uap, R0);
354 write_zsreg(uap, R0, RES_EXT_INT);
355 zssync(uap);
356
357 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
358 if (status & SYNC_HUNT)
359 uap->port.icount.dsr++;
360
361 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
362 * But it does not tell us which bit has changed, we have to keep
363 * track of this ourselves.
364 * The CTS input is inverted for some reason. -- paulus
365 */
366 if ((status ^ uap->prev_status) & DCD)
367 uart_handle_dcd_change(&uap->port,
368 (status & DCD));
369 if ((status ^ uap->prev_status) & CTS)
370 uart_handle_cts_change(&uap->port,
371 !(status & CTS));
372
373 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
374 }
375
376 if (status & BRK_ABRT)
377 uap->flags |= PMACZILOG_FLAG_BREAK;
378
379 uap->prev_status = status;
380}
381
382static void pmz_transmit_chars(struct uart_pmac_port *uap)
383{
384 struct circ_buf *xmit;
385
386 if (ZS_IS_ASLEEP(uap))
387 return;
388 if (ZS_IS_CONS(uap)) {
389 unsigned char status = read_zsreg(uap, R0);
390
391 /* TX still busy? Just wait for the next TX done interrupt.
392 *
393 * It can occur because of how we do serial console writes. It would
394 * be nice to transmit console writes just like we normally would for
395 * a TTY line. (ie. buffered and TX interrupt driven). That is not
396 * easy because console writes cannot sleep. One solution might be
397 * to poll on enough port->xmit space becoming free. -DaveM
398 */
399 if (!(status & Tx_BUF_EMP))
400 return;
401 }
402
403 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
404
405 if (ZS_REGS_HELD(uap)) {
406 pmz_load_zsregs(uap, uap->curregs);
407 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
408 }
409
410 if (ZS_TX_STOPPED(uap)) {
411 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
412 goto ack_tx_int;
413 }
414
415 /* Under some circumstances, we see interrupts reported for
416 * a closed channel. The interrupt mask in R1 is clear, but
417 * R3 still signals the interrupts and we see them when taking
418 * an interrupt for the other channel (this could be a qemu
419 * bug but since the ESCC doc doesn't specify precsiely whether
420 * R3 interrup status bits are masked by R1 interrupt enable
421 * bits, better safe than sorry). --BenH.
422 */
423 if (!ZS_IS_OPEN(uap))
424 goto ack_tx_int;
425
426 if (uap->port.x_char) {
427 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
428 write_zsdata(uap, uap->port.x_char);
429 zssync(uap);
430 uap->port.icount.tx++;
431 uap->port.x_char = 0;
432 return;
433 }
434
435 if (uap->port.state == NULL)
436 goto ack_tx_int;
437 xmit = &uap->port.state->xmit;
438 if (uart_circ_empty(xmit)) {
439 uart_write_wakeup(&uap->port);
440 goto ack_tx_int;
441 }
442 if (uart_tx_stopped(&uap->port))
443 goto ack_tx_int;
444
445 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
446 write_zsdata(uap, xmit->buf[xmit->tail]);
447 zssync(uap);
448
449 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
450 uap->port.icount.tx++;
451
452 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
453 uart_write_wakeup(&uap->port);
454
455 return;
456
457ack_tx_int:
458 write_zsreg(uap, R0, RES_Tx_P);
459 zssync(uap);
460}
461
462/* Hrm... we register that twice, fixme later.... */
463static irqreturn_t pmz_interrupt(int irq, void *dev_id)
464{
465 struct uart_pmac_port *uap = dev_id;
466 struct uart_pmac_port *uap_a;
467 struct uart_pmac_port *uap_b;
468 int rc = IRQ_NONE;
469 struct tty_struct *tty;
470 u8 r3;
471
472 uap_a = pmz_get_port_A(uap);
473 uap_b = uap_a->mate;
474
475 spin_lock(&uap_a->port.lock);
476 r3 = read_zsreg(uap_a, R3);
477
478#ifdef DEBUG_HARD
479 pmz_debug("irq, r3: %x\n", r3);
480#endif
481 /* Channel A */
482 tty = NULL;
483 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
484 write_zsreg(uap_a, R0, RES_H_IUS);
485 zssync(uap_a);
486 if (r3 & CHAEXT)
487 pmz_status_handle(uap_a);
488 if (r3 & CHARxIP)
489 tty = pmz_receive_chars(uap_a);
490 if (r3 & CHATxIP)
491 pmz_transmit_chars(uap_a);
492 rc = IRQ_HANDLED;
493 }
494 spin_unlock(&uap_a->port.lock);
495 if (tty != NULL)
496 tty_flip_buffer_push(tty);
497
498 if (uap_b->node == NULL)
499 goto out;
500
501 spin_lock(&uap_b->port.lock);
502 tty = NULL;
503 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
504 write_zsreg(uap_b, R0, RES_H_IUS);
505 zssync(uap_b);
506 if (r3 & CHBEXT)
507 pmz_status_handle(uap_b);
508 if (r3 & CHBRxIP)
509 tty = pmz_receive_chars(uap_b);
510 if (r3 & CHBTxIP)
511 pmz_transmit_chars(uap_b);
512 rc = IRQ_HANDLED;
513 }
514 spin_unlock(&uap_b->port.lock);
515 if (tty != NULL)
516 tty_flip_buffer_push(tty);
517
518 out:
519#ifdef DEBUG_HARD
520 pmz_debug("irq done.\n");
521#endif
522 return rc;
523}
524
525/*
526 * Peek the status register, lock not held by caller
527 */
528static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
529{
530 unsigned long flags;
531 u8 status;
532
533 spin_lock_irqsave(&uap->port.lock, flags);
534 status = read_zsreg(uap, R0);
535 spin_unlock_irqrestore(&uap->port.lock, flags);
536
537 return status;
538}
539
540/*
541 * Check if transmitter is empty
542 * The port lock is not held.
543 */
544static unsigned int pmz_tx_empty(struct uart_port *port)
545{
546 struct uart_pmac_port *uap = to_pmz(port);
547 unsigned char status;
548
549 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
550 return TIOCSER_TEMT;
551
552 status = pmz_peek_status(to_pmz(port));
553 if (status & Tx_BUF_EMP)
554 return TIOCSER_TEMT;
555 return 0;
556}
557
558/*
559 * Set Modem Control (RTS & DTR) bits
560 * The port lock is held and interrupts are disabled.
561 * Note: Shall we really filter out RTS on external ports or
562 * should that be dealt at higher level only ?
563 */
564static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
565{
566 struct uart_pmac_port *uap = to_pmz(port);
567 unsigned char set_bits, clear_bits;
568
569 /* Do nothing for irda for now... */
570 if (ZS_IS_IRDA(uap))
571 return;
572 /* We get called during boot with a port not up yet */
573 if (ZS_IS_ASLEEP(uap) ||
574 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
575 return;
576
577 set_bits = clear_bits = 0;
578
579 if (ZS_IS_INTMODEM(uap)) {
580 if (mctrl & TIOCM_RTS)
581 set_bits |= RTS;
582 else
583 clear_bits |= RTS;
584 }
585 if (mctrl & TIOCM_DTR)
586 set_bits |= DTR;
587 else
588 clear_bits |= DTR;
589
590 /* NOTE: Not subject to 'transmitter active' rule. */
591 uap->curregs[R5] |= set_bits;
592 uap->curregs[R5] &= ~clear_bits;
593 if (ZS_IS_ASLEEP(uap))
594 return;
595 write_zsreg(uap, R5, uap->curregs[R5]);
596 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
597 set_bits, clear_bits, uap->curregs[R5]);
598 zssync(uap);
599}
600
601/*
602 * Get Modem Control bits (only the input ones, the core will
603 * or that with a cached value of the control ones)
604 * The port lock is held and interrupts are disabled.
605 */
606static unsigned int pmz_get_mctrl(struct uart_port *port)
607{
608 struct uart_pmac_port *uap = to_pmz(port);
609 unsigned char status;
610 unsigned int ret;
611
612 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
613 return 0;
614
615 status = read_zsreg(uap, R0);
616
617 ret = 0;
618 if (status & DCD)
619 ret |= TIOCM_CAR;
620 if (status & SYNC_HUNT)
621 ret |= TIOCM_DSR;
622 if (!(status & CTS))
623 ret |= TIOCM_CTS;
624
625 return ret;
626}
627
628/*
629 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
630 * though for DMA, we will have to do a bit more.
631 * The port lock is held and interrupts are disabled.
632 */
633static void pmz_stop_tx(struct uart_port *port)
634{
635 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
636}
637
638/*
639 * Kick the Tx side.
640 * The port lock is held and interrupts are disabled.
641 */
642static void pmz_start_tx(struct uart_port *port)
643{
644 struct uart_pmac_port *uap = to_pmz(port);
645 unsigned char status;
646
647 pmz_debug("pmz: start_tx()\n");
648
649 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
650 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
651
652 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
653 return;
654
655 status = read_zsreg(uap, R0);
656
657 /* TX busy? Just wait for the TX done interrupt. */
658 if (!(status & Tx_BUF_EMP))
659 return;
660
661 /* Send the first character to jump-start the TX done
662 * IRQ sending engine.
663 */
664 if (port->x_char) {
665 write_zsdata(uap, port->x_char);
666 zssync(uap);
667 port->icount.tx++;
668 port->x_char = 0;
669 } else {
670 struct circ_buf *xmit = &port->state->xmit;
671
672 write_zsdata(uap, xmit->buf[xmit->tail]);
673 zssync(uap);
674 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
675 port->icount.tx++;
676
677 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
678 uart_write_wakeup(&uap->port);
679 }
680 pmz_debug("pmz: start_tx() done.\n");
681}
682
683/*
684 * Stop Rx side, basically disable emitting of
685 * Rx interrupts on the port. We don't disable the rx
686 * side of the chip proper though
687 * The port lock is held.
688 */
689static void pmz_stop_rx(struct uart_port *port)
690{
691 struct uart_pmac_port *uap = to_pmz(port);
692
693 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
694 return;
695
696 pmz_debug("pmz: stop_rx()()\n");
697
698 /* Disable all RX interrupts. */
699 uap->curregs[R1] &= ~RxINT_MASK;
700 pmz_maybe_update_regs(uap);
701
702 pmz_debug("pmz: stop_rx() done.\n");
703}
704
705/*
706 * Enable modem status change interrupts
707 * The port lock is held.
708 */
709static void pmz_enable_ms(struct uart_port *port)
710{
711 struct uart_pmac_port *uap = to_pmz(port);
712 unsigned char new_reg;
713
714 if (ZS_IS_IRDA(uap) || uap->node == NULL)
715 return;
716 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
717 if (new_reg != uap->curregs[R15]) {
718 uap->curregs[R15] = new_reg;
719
720 if (ZS_IS_ASLEEP(uap))
721 return;
722 /* NOTE: Not subject to 'transmitter active' rule. */
723 write_zsreg(uap, R15, uap->curregs[R15]);
724 }
725}
726
727/*
728 * Control break state emission
729 * The port lock is not held.
730 */
731static void pmz_break_ctl(struct uart_port *port, int break_state)
732{
733 struct uart_pmac_port *uap = to_pmz(port);
734 unsigned char set_bits, clear_bits, new_reg;
735 unsigned long flags;
736
737 if (uap->node == NULL)
738 return;
739 set_bits = clear_bits = 0;
740
741 if (break_state)
742 set_bits |= SND_BRK;
743 else
744 clear_bits |= SND_BRK;
745
746 spin_lock_irqsave(&port->lock, flags);
747
748 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
749 if (new_reg != uap->curregs[R5]) {
750 uap->curregs[R5] = new_reg;
751
752 /* NOTE: Not subject to 'transmitter active' rule. */
753 if (ZS_IS_ASLEEP(uap)) {
754 spin_unlock_irqrestore(&port->lock, flags);
755 return;
756 }
757 write_zsreg(uap, R5, uap->curregs[R5]);
758 }
759
760 spin_unlock_irqrestore(&port->lock, flags);
761}
762
763#ifdef CONFIG_PPC_PMAC
764
765/*
766 * Turn power on or off to the SCC and associated stuff
767 * (port drivers, modem, IR port, etc.)
768 * Returns the number of milliseconds we should wait before
769 * trying to use the port.
770 */
771static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
772{
773 int delay = 0;
774 int rc;
775
776 if (state) {
777 rc = pmac_call_feature(
778 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
779 pmz_debug("port power on result: %d\n", rc);
780 if (ZS_IS_INTMODEM(uap)) {
781 rc = pmac_call_feature(
782 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
783 delay = 2500; /* wait for 2.5s before using */
784 pmz_debug("modem power result: %d\n", rc);
785 }
786 } else {
787 /* TODO: Make that depend on a timer, don't power down
788 * immediately
789 */
790 if (ZS_IS_INTMODEM(uap)) {
791 rc = pmac_call_feature(
792 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
793 pmz_debug("port power off result: %d\n", rc);
794 }
795 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
796 }
797 return delay;
798}
799
800#else
801
802static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
803{
804 return 0;
805}
806
807#endif /* !CONFIG_PPC_PMAC */
808
809/*
810 * FixZeroBug....Works around a bug in the SCC receiving channel.
811 * Inspired from Darwin code, 15 Sept. 2000 -DanM
812 *
813 * The following sequence prevents a problem that is seen with O'Hare ASICs
814 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
815 * at the input to the receiver becomes 'stuck' and locks up the receiver.
816 * This problem can occur as a result of a zero bit at the receiver input
817 * coincident with any of the following events:
818 *
819 * The SCC is initialized (hardware or software).
820 * A framing error is detected.
821 * The clocking option changes from synchronous or X1 asynchronous
822 * clocking to X16, X32, or X64 asynchronous clocking.
823 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
824 *
825 * This workaround attempts to recover from the lockup condition by placing
826 * the SCC in synchronous loopback mode with a fast clock before programming
827 * any of the asynchronous modes.
828 */
829static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
830{
831 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
832 zssync(uap);
833 udelay(10);
834 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
835 zssync(uap);
836
837 write_zsreg(uap, 4, X1CLK | MONSYNC);
838 write_zsreg(uap, 3, Rx8);
839 write_zsreg(uap, 5, Tx8 | RTS);
840 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
841 write_zsreg(uap, 11, RCBR | TCBR);
842 write_zsreg(uap, 12, 0);
843 write_zsreg(uap, 13, 0);
844 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
845 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
846 write_zsreg(uap, 3, Rx8 | RxENABLE);
847 write_zsreg(uap, 0, RES_EXT_INT);
848 write_zsreg(uap, 0, RES_EXT_INT);
849 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
850
851 /* The channel should be OK now, but it is probably receiving
852 * loopback garbage.
853 * Switch to asynchronous mode, disable the receiver,
854 * and discard everything in the receive buffer.
855 */
856 write_zsreg(uap, 9, NV);
857 write_zsreg(uap, 4, X16CLK | SB_MASK);
858 write_zsreg(uap, 3, Rx8);
859
860 while (read_zsreg(uap, 0) & Rx_CH_AV) {
861 (void)read_zsreg(uap, 8);
862 write_zsreg(uap, 0, RES_EXT_INT);
863 write_zsreg(uap, 0, ERR_RES);
864 }
865}
866
867/*
868 * Real startup routine, powers up the hardware and sets up
869 * the SCC. Returns a delay in ms where you need to wait before
870 * actually using the port, this is typically the internal modem
871 * powerup delay. This routine expect the lock to be taken.
872 */
873static int __pmz_startup(struct uart_pmac_port *uap)
874{
875 int pwr_delay = 0;
876
877 memset(&uap->curregs, 0, sizeof(uap->curregs));
878
879 /* Power up the SCC & underlying hardware (modem/irda) */
880 pwr_delay = pmz_set_scc_power(uap, 1);
881
882 /* Nice buggy HW ... */
883 pmz_fix_zero_bug_scc(uap);
884
885 /* Reset the channel */
886 uap->curregs[R9] = 0;
887 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
888 zssync(uap);
889 udelay(10);
890 write_zsreg(uap, 9, 0);
891 zssync(uap);
892
893 /* Clear the interrupt registers */
894 write_zsreg(uap, R1, 0);
895 write_zsreg(uap, R0, ERR_RES);
896 write_zsreg(uap, R0, ERR_RES);
897 write_zsreg(uap, R0, RES_H_IUS);
898 write_zsreg(uap, R0, RES_H_IUS);
899
900 /* Setup some valid baud rate */
901 uap->curregs[R4] = X16CLK | SB1;
902 uap->curregs[R3] = Rx8;
903 uap->curregs[R5] = Tx8 | RTS;
904 if (!ZS_IS_IRDA(uap))
905 uap->curregs[R5] |= DTR;
906 uap->curregs[R12] = 0;
907 uap->curregs[R13] = 0;
908 uap->curregs[R14] = BRENAB;
909
910 /* Clear handshaking, enable BREAK interrupts */
911 uap->curregs[R15] = BRKIE;
912
913 /* Master interrupt enable */
914 uap->curregs[R9] |= NV | MIE;
915
916 pmz_load_zsregs(uap, uap->curregs);
917
918 /* Enable receiver and transmitter. */
919 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
920 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
921
922 /* Remember status for DCD/CTS changes */
923 uap->prev_status = read_zsreg(uap, R0);
924
925 return pwr_delay;
926}
927
928static void pmz_irda_reset(struct uart_pmac_port *uap)
929{
930 uap->curregs[R5] |= DTR;
931 write_zsreg(uap, R5, uap->curregs[R5]);
932 zssync(uap);
933 mdelay(110);
934 uap->curregs[R5] &= ~DTR;
935 write_zsreg(uap, R5, uap->curregs[R5]);
936 zssync(uap);
937 mdelay(10);
938}
939
940/*
941 * This is the "normal" startup routine, using the above one
942 * wrapped with the lock and doing a schedule delay
943 */
944static int pmz_startup(struct uart_port *port)
945{
946 struct uart_pmac_port *uap = to_pmz(port);
947 unsigned long flags;
948 int pwr_delay = 0;
949
950 pmz_debug("pmz: startup()\n");
951
952 if (ZS_IS_ASLEEP(uap))
953 return -EAGAIN;
954 if (uap->node == NULL)
955 return -ENODEV;
956
957 mutex_lock(&pmz_irq_mutex);
958
959 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
960
961 /* A console is never powered down. Else, power up and
962 * initialize the chip
963 */
964 if (!ZS_IS_CONS(uap)) {
965 spin_lock_irqsave(&port->lock, flags);
966 pwr_delay = __pmz_startup(uap);
967 spin_unlock_irqrestore(&port->lock, flags);
968 }
969
970 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
971 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED,
972 "SCC", uap)) {
973 pmz_error("Unable to register zs interrupt handler.\n");
974 pmz_set_scc_power(uap, 0);
975 mutex_unlock(&pmz_irq_mutex);
976 return -ENXIO;
977 }
978
979 mutex_unlock(&pmz_irq_mutex);
980
981 /* Right now, we deal with delay by blocking here, I'll be
982 * smarter later on
983 */
984 if (pwr_delay != 0) {
985 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
986 msleep(pwr_delay);
987 }
988
989 /* IrDA reset is done now */
990 if (ZS_IS_IRDA(uap))
991 pmz_irda_reset(uap);
992
993 /* Enable interrupts emission from the chip */
994 spin_lock_irqsave(&port->lock, flags);
995 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
996 if (!ZS_IS_EXTCLK(uap))
997 uap->curregs[R1] |= EXT_INT_ENAB;
998 write_zsreg(uap, R1, uap->curregs[R1]);
999 spin_unlock_irqrestore(&port->lock, flags);
1000
1001 pmz_debug("pmz: startup() done.\n");
1002
1003 return 0;
1004}
1005
1006static void pmz_shutdown(struct uart_port *port)
1007{
1008 struct uart_pmac_port *uap = to_pmz(port);
1009 unsigned long flags;
1010
1011 pmz_debug("pmz: shutdown()\n");
1012
1013 if (uap->node == NULL)
1014 return;
1015
1016 mutex_lock(&pmz_irq_mutex);
1017
1018 /* Release interrupt handler */
1019 free_irq(uap->port.irq, uap);
1020
1021 spin_lock_irqsave(&port->lock, flags);
1022
1023 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1024
1025 if (!ZS_IS_OPEN(uap->mate))
1026 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1027
1028 /* Disable interrupts */
1029 if (!ZS_IS_ASLEEP(uap)) {
1030 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1031 write_zsreg(uap, R1, uap->curregs[R1]);
1032 zssync(uap);
1033 }
1034
1035 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1036 spin_unlock_irqrestore(&port->lock, flags);
1037 mutex_unlock(&pmz_irq_mutex);
1038 return;
1039 }
1040
1041 /* Disable receiver and transmitter. */
1042 uap->curregs[R3] &= ~RxENABLE;
1043 uap->curregs[R5] &= ~TxENABLE;
1044
1045 /* Disable all interrupts and BRK assertion. */
1046 uap->curregs[R5] &= ~SND_BRK;
1047 pmz_maybe_update_regs(uap);
1048
1049 /* Shut the chip down */
1050 pmz_set_scc_power(uap, 0);
1051
1052 spin_unlock_irqrestore(&port->lock, flags);
1053
1054 mutex_unlock(&pmz_irq_mutex);
1055
1056 pmz_debug("pmz: shutdown() done.\n");
1057}
1058
1059/* Shared by TTY driver and serial console setup. The port lock is held
1060 * and local interrupts are disabled.
1061 */
1062static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1063 unsigned int iflag, unsigned long baud)
1064{
1065 int brg;
1066
1067 /* Switch to external clocking for IrDA high clock rates. That
1068 * code could be re-used for Midi interfaces with different
1069 * multipliers
1070 */
1071 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1072 uap->curregs[R4] = X1CLK;
1073 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1074 uap->curregs[R14] = 0; /* BRG off */
1075 uap->curregs[R12] = 0;
1076 uap->curregs[R13] = 0;
1077 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1078 } else {
1079 switch (baud) {
1080 case ZS_CLOCK/16: /* 230400 */
1081 uap->curregs[R4] = X16CLK;
1082 uap->curregs[R11] = 0;
1083 uap->curregs[R14] = 0;
1084 break;
1085 case ZS_CLOCK/32: /* 115200 */
1086 uap->curregs[R4] = X32CLK;
1087 uap->curregs[R11] = 0;
1088 uap->curregs[R14] = 0;
1089 break;
1090 default:
1091 uap->curregs[R4] = X16CLK;
1092 uap->curregs[R11] = TCBR | RCBR;
1093 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1094 uap->curregs[R12] = (brg & 255);
1095 uap->curregs[R13] = ((brg >> 8) & 255);
1096 uap->curregs[R14] = BRENAB;
1097 }
1098 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1099 }
1100
1101 /* Character size, stop bits, and parity. */
1102 uap->curregs[3] &= ~RxN_MASK;
1103 uap->curregs[5] &= ~TxN_MASK;
1104
1105 switch (cflag & CSIZE) {
1106 case CS5:
1107 uap->curregs[3] |= Rx5;
1108 uap->curregs[5] |= Tx5;
1109 uap->parity_mask = 0x1f;
1110 break;
1111 case CS6:
1112 uap->curregs[3] |= Rx6;
1113 uap->curregs[5] |= Tx6;
1114 uap->parity_mask = 0x3f;
1115 break;
1116 case CS7:
1117 uap->curregs[3] |= Rx7;
1118 uap->curregs[5] |= Tx7;
1119 uap->parity_mask = 0x7f;
1120 break;
1121 case CS8:
1122 default:
1123 uap->curregs[3] |= Rx8;
1124 uap->curregs[5] |= Tx8;
1125 uap->parity_mask = 0xff;
1126 break;
1127 };
1128 uap->curregs[4] &= ~(SB_MASK);
1129 if (cflag & CSTOPB)
1130 uap->curregs[4] |= SB2;
1131 else
1132 uap->curregs[4] |= SB1;
1133 if (cflag & PARENB)
1134 uap->curregs[4] |= PAR_ENAB;
1135 else
1136 uap->curregs[4] &= ~PAR_ENAB;
1137 if (!(cflag & PARODD))
1138 uap->curregs[4] |= PAR_EVEN;
1139 else
1140 uap->curregs[4] &= ~PAR_EVEN;
1141
1142 uap->port.read_status_mask = Rx_OVR;
1143 if (iflag & INPCK)
1144 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1145 if (iflag & (BRKINT | PARMRK))
1146 uap->port.read_status_mask |= BRK_ABRT;
1147
1148 uap->port.ignore_status_mask = 0;
1149 if (iflag & IGNPAR)
1150 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1151 if (iflag & IGNBRK) {
1152 uap->port.ignore_status_mask |= BRK_ABRT;
1153 if (iflag & IGNPAR)
1154 uap->port.ignore_status_mask |= Rx_OVR;
1155 }
1156
1157 if ((cflag & CREAD) == 0)
1158 uap->port.ignore_status_mask = 0xff;
1159}
1160
1161
1162/*
1163 * Set the irda codec on the imac to the specified baud rate.
1164 */
1165static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1166{
1167 u8 cmdbyte;
1168 int t, version;
1169
1170 switch (*baud) {
1171 /* SIR modes */
1172 case 2400:
1173 cmdbyte = 0x53;
1174 break;
1175 case 4800:
1176 cmdbyte = 0x52;
1177 break;
1178 case 9600:
1179 cmdbyte = 0x51;
1180 break;
1181 case 19200:
1182 cmdbyte = 0x50;
1183 break;
1184 case 38400:
1185 cmdbyte = 0x4f;
1186 break;
1187 case 57600:
1188 cmdbyte = 0x4e;
1189 break;
1190 case 115200:
1191 cmdbyte = 0x4d;
1192 break;
1193 /* The FIR modes aren't really supported at this point, how
1194 * do we select the speed ? via the FCR on KeyLargo ?
1195 */
1196 case 1152000:
1197 cmdbyte = 0;
1198 break;
1199 case 4000000:
1200 cmdbyte = 0;
1201 break;
1202 default: /* 9600 */
1203 cmdbyte = 0x51;
1204 *baud = 9600;
1205 break;
1206 }
1207
1208 /* Wait for transmitter to drain */
1209 t = 10000;
1210 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1211 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1212 if (--t <= 0) {
1213 pmz_error("transmitter didn't drain\n");
1214 return;
1215 }
1216 udelay(10);
1217 }
1218
1219 /* Drain the receiver too */
1220 t = 100;
1221 (void)read_zsdata(uap);
1222 (void)read_zsdata(uap);
1223 (void)read_zsdata(uap);
1224 mdelay(10);
1225 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1226 read_zsdata(uap);
1227 mdelay(10);
1228 if (--t <= 0) {
1229 pmz_error("receiver didn't drain\n");
1230 return;
1231 }
1232 }
1233
1234 /* Switch to command mode */
1235 uap->curregs[R5] |= DTR;
1236 write_zsreg(uap, R5, uap->curregs[R5]);
1237 zssync(uap);
1238 mdelay(1);
1239
1240 /* Switch SCC to 19200 */
1241 pmz_convert_to_zs(uap, CS8, 0, 19200);
1242 pmz_load_zsregs(uap, uap->curregs);
1243 mdelay(1);
1244
1245 /* Write get_version command byte */
1246 write_zsdata(uap, 1);
1247 t = 5000;
1248 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1249 if (--t <= 0) {
1250 pmz_error("irda_setup timed out on get_version byte\n");
1251 goto out;
1252 }
1253 udelay(10);
1254 }
1255 version = read_zsdata(uap);
1256
1257 if (version < 4) {
1258 pmz_info("IrDA: dongle version %d not supported\n", version);
1259 goto out;
1260 }
1261
1262 /* Send speed mode */
1263 write_zsdata(uap, cmdbyte);
1264 t = 5000;
1265 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1266 if (--t <= 0) {
1267 pmz_error("irda_setup timed out on speed mode byte\n");
1268 goto out;
1269 }
1270 udelay(10);
1271 }
1272 t = read_zsdata(uap);
1273 if (t != cmdbyte)
1274 pmz_error("irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1275
1276 pmz_info("IrDA setup for %ld bps, dongle version: %d\n",
1277 *baud, version);
1278
1279 (void)read_zsdata(uap);
1280 (void)read_zsdata(uap);
1281 (void)read_zsdata(uap);
1282
1283 out:
1284 /* Switch back to data mode */
1285 uap->curregs[R5] &= ~DTR;
1286 write_zsreg(uap, R5, uap->curregs[R5]);
1287 zssync(uap);
1288
1289 (void)read_zsdata(uap);
1290 (void)read_zsdata(uap);
1291 (void)read_zsdata(uap);
1292}
1293
1294
1295static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1296 struct ktermios *old)
1297{
1298 struct uart_pmac_port *uap = to_pmz(port);
1299 unsigned long baud;
1300
1301 pmz_debug("pmz: set_termios()\n");
1302
1303 if (ZS_IS_ASLEEP(uap))
1304 return;
1305
1306 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
1307
1308 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1309 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1310 * about the FIR mode and high speed modes. So these are unused. For
1311 * implementing proper support for these, we should probably add some
1312 * DMA as well, at least on the Rx side, which isn't a simple thing
1313 * at this point.
1314 */
1315 if (ZS_IS_IRDA(uap)) {
1316 /* Calc baud rate */
1317 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1318 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1319 /* Cet the irda codec to the right rate */
1320 pmz_irda_setup(uap, &baud);
1321 /* Set final baud rate */
1322 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1323 pmz_load_zsregs(uap, uap->curregs);
1324 zssync(uap);
1325 } else {
1326 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1327 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1328 /* Make sure modem status interrupts are correctly configured */
1329 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1330 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1331 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1332 } else {
1333 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1334 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1335 }
1336
1337 /* Load registers to the chip */
1338 pmz_maybe_update_regs(uap);
1339 }
1340 uart_update_timeout(port, termios->c_cflag, baud);
1341
1342 pmz_debug("pmz: set_termios() done.\n");
1343}
1344
1345/* The port lock is not held. */
1346static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1347 struct ktermios *old)
1348{
1349 struct uart_pmac_port *uap = to_pmz(port);
1350 unsigned long flags;
1351
1352 spin_lock_irqsave(&port->lock, flags);
1353
1354 /* Disable IRQs on the port */
1355 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1356 write_zsreg(uap, R1, uap->curregs[R1]);
1357
1358 /* Setup new port configuration */
1359 __pmz_set_termios(port, termios, old);
1360
1361 /* Re-enable IRQs on the port */
1362 if (ZS_IS_OPEN(uap)) {
1363 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1364 if (!ZS_IS_EXTCLK(uap))
1365 uap->curregs[R1] |= EXT_INT_ENAB;
1366 write_zsreg(uap, R1, uap->curregs[R1]);
1367 }
1368 spin_unlock_irqrestore(&port->lock, flags);
1369}
1370
1371static const char *pmz_type(struct uart_port *port)
1372{
1373 struct uart_pmac_port *uap = to_pmz(port);
1374
1375 if (ZS_IS_IRDA(uap))
1376 return "Z85c30 ESCC - Infrared port";
1377 else if (ZS_IS_INTMODEM(uap))
1378 return "Z85c30 ESCC - Internal modem";
1379 return "Z85c30 ESCC - Serial port";
1380}
1381
1382/* We do not request/release mappings of the registers here, this
1383 * happens at early serial probe time.
1384 */
1385static void pmz_release_port(struct uart_port *port)
1386{
1387}
1388
1389static int pmz_request_port(struct uart_port *port)
1390{
1391 return 0;
1392}
1393
1394/* These do not need to do anything interesting either. */
1395static void pmz_config_port(struct uart_port *port, int flags)
1396{
1397}
1398
1399/* We do not support letting the user mess with the divisor, IRQ, etc. */
1400static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1401{
1402 return -EINVAL;
1403}
1404
1405#ifdef CONFIG_CONSOLE_POLL
1406
1407static int pmz_poll_get_char(struct uart_port *port)
1408{
1409 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1410
1411 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1412 udelay(5);
1413 return read_zsdata(uap);
1414}
1415
1416static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1417{
1418 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1419
1420 /* Wait for the transmit buffer to empty. */
1421 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1422 udelay(5);
1423 write_zsdata(uap, c);
1424}
1425
1426#endif /* CONFIG_CONSOLE_POLL */
1427
1428static struct uart_ops pmz_pops = {
1429 .tx_empty = pmz_tx_empty,
1430 .set_mctrl = pmz_set_mctrl,
1431 .get_mctrl = pmz_get_mctrl,
1432 .stop_tx = pmz_stop_tx,
1433 .start_tx = pmz_start_tx,
1434 .stop_rx = pmz_stop_rx,
1435 .enable_ms = pmz_enable_ms,
1436 .break_ctl = pmz_break_ctl,
1437 .startup = pmz_startup,
1438 .shutdown = pmz_shutdown,
1439 .set_termios = pmz_set_termios,
1440 .type = pmz_type,
1441 .release_port = pmz_release_port,
1442 .request_port = pmz_request_port,
1443 .config_port = pmz_config_port,
1444 .verify_port = pmz_verify_port,
1445#ifdef CONFIG_CONSOLE_POLL
1446 .poll_get_char = pmz_poll_get_char,
1447 .poll_put_char = pmz_poll_put_char,
1448#endif
1449};
1450
1451#ifdef CONFIG_PPC_PMAC
1452
1453/*
1454 * Setup one port structure after probing, HW is down at this point,
1455 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1456 * register our console before uart_add_one_port() is called
1457 */
1458static int __init pmz_init_port(struct uart_pmac_port *uap)
1459{
1460 struct device_node *np = uap->node;
1461 const char *conn;
1462 const struct slot_names_prop {
1463 int count;
1464 char name[1];
1465 } *slots;
1466 int len;
1467 struct resource r_ports, r_rxdma, r_txdma;
1468
1469 /*
1470 * Request & map chip registers
1471 */
1472 if (of_address_to_resource(np, 0, &r_ports))
1473 return -ENODEV;
1474 uap->port.mapbase = r_ports.start;
1475 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1476
1477 uap->control_reg = uap->port.membase;
1478 uap->data_reg = uap->control_reg + 0x10;
1479
1480 /*
1481 * Request & map DBDMA registers
1482 */
1483#ifdef HAS_DBDMA
1484 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1485 of_address_to_resource(np, 2, &r_rxdma) == 0)
1486 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1487#else
1488 memset(&r_txdma, 0, sizeof(struct resource));
1489 memset(&r_rxdma, 0, sizeof(struct resource));
1490#endif
1491 if (ZS_HAS_DMA(uap)) {
1492 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
1493 if (uap->tx_dma_regs == NULL) {
1494 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1495 goto no_dma;
1496 }
1497 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
1498 if (uap->rx_dma_regs == NULL) {
1499 iounmap(uap->tx_dma_regs);
1500 uap->tx_dma_regs = NULL;
1501 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1502 goto no_dma;
1503 }
1504 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1505 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
1506 }
1507no_dma:
1508
1509 /*
1510 * Detect port type
1511 */
1512 if (of_device_is_compatible(np, "cobalt"))
1513 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1514 conn = of_get_property(np, "AAPL,connector", &len);
1515 if (conn && (strcmp(conn, "infrared") == 0))
1516 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1517 uap->port_type = PMAC_SCC_ASYNC;
1518 /* 1999 Powerbook G3 has slot-names property instead */
1519 slots = of_get_property(np, "slot-names", &len);
1520 if (slots && slots->count > 0) {
1521 if (strcmp(slots->name, "IrDA") == 0)
1522 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1523 else if (strcmp(slots->name, "Modem") == 0)
1524 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1525 }
1526 if (ZS_IS_IRDA(uap))
1527 uap->port_type = PMAC_SCC_IRDA;
1528 if (ZS_IS_INTMODEM(uap)) {
1529 struct device_node* i2c_modem =
1530 of_find_node_by_name(NULL, "i2c-modem");
1531 if (i2c_modem) {
1532 const char* mid =
1533 of_get_property(i2c_modem, "modem-id", NULL);
1534 if (mid) switch(*mid) {
1535 case 0x04 :
1536 case 0x05 :
1537 case 0x07 :
1538 case 0x08 :
1539 case 0x0b :
1540 case 0x0c :
1541 uap->port_type = PMAC_SCC_I2S1;
1542 }
1543 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1544 mid ? (*mid) : 0);
1545 of_node_put(i2c_modem);
1546 } else {
1547 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1548 }
1549 }
1550
1551 /*
1552 * Init remaining bits of "port" structure
1553 */
1554 uap->port.iotype = UPIO_MEM;
1555 uap->port.irq = irq_of_parse_and_map(np, 0);
1556 uap->port.uartclk = ZS_CLOCK;
1557 uap->port.fifosize = 1;
1558 uap->port.ops = &pmz_pops;
1559 uap->port.type = PORT_PMAC_ZILOG;
1560 uap->port.flags = 0;
1561
1562 /*
1563 * Fixup for the port on Gatwick for which the device-tree has
1564 * missing interrupts. Normally, the macio_dev would contain
1565 * fixed up interrupt info, but we use the device-tree directly
1566 * here due to early probing so we need the fixup too.
1567 */
1568 if (uap->port.irq == NO_IRQ &&
1569 np->parent && np->parent->parent &&
1570 of_device_is_compatible(np->parent->parent, "gatwick")) {
1571 /* IRQs on gatwick are offset by 64 */
1572 uap->port.irq = irq_create_mapping(NULL, 64 + 15);
1573 uap->tx_dma_irq = irq_create_mapping(NULL, 64 + 4);
1574 uap->rx_dma_irq = irq_create_mapping(NULL, 64 + 5);
1575 }
1576
1577 /* Setup some valid baud rate information in the register
1578 * shadows so we don't write crap there before baud rate is
1579 * first initialized.
1580 */
1581 pmz_convert_to_zs(uap, CS8, 0, 9600);
1582
1583 return 0;
1584}
1585
1586/*
1587 * Get rid of a port on module removal
1588 */
1589static void pmz_dispose_port(struct uart_pmac_port *uap)
1590{
1591 struct device_node *np;
1592
1593 np = uap->node;
1594 iounmap(uap->rx_dma_regs);
1595 iounmap(uap->tx_dma_regs);
1596 iounmap(uap->control_reg);
1597 uap->node = NULL;
1598 of_node_put(np);
1599 memset(uap, 0, sizeof(struct uart_pmac_port));
1600}
1601
1602/*
1603 * Called upon match with an escc node in the device-tree.
1604 */
1605static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
1606{
1607 int i;
1608
1609 /* Iterate the pmz_ports array to find a matching entry
1610 */
1611 for (i = 0; i < MAX_ZS_PORTS; i++)
1612 if (pmz_ports[i].node == mdev->ofdev.dev.of_node) {
1613 struct uart_pmac_port *uap = &pmz_ports[i];
1614
1615 uap->dev = mdev;
1616 dev_set_drvdata(&mdev->ofdev.dev, uap);
1617 if (macio_request_resources(uap->dev, "pmac_zilog"))
1618 printk(KERN_WARNING "%s: Failed to request resource"
1619 ", port still active\n",
1620 uap->node->name);
1621 else
1622 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1623 return 0;
1624 }
1625 return -ENODEV;
1626}
1627
1628/*
1629 * That one should not be called, macio isn't really a hotswap device,
1630 * we don't expect one of those serial ports to go away...
1631 */
1632static int pmz_detach(struct macio_dev *mdev)
1633{
1634 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1635
1636 if (!uap)
1637 return -ENODEV;
1638
1639 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1640 macio_release_resources(uap->dev);
1641 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1642 }
1643 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1644 uap->dev = NULL;
1645
1646 return 0;
1647}
1648
1649
1650static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
1651{
1652 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1653 struct uart_state *state;
1654 unsigned long flags;
1655
1656 if (uap == NULL) {
1657 printk("HRM... pmz_suspend with NULL uap\n");
1658 return 0;
1659 }
1660
1661 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
1662 return 0;
1663
1664 pmz_debug("suspend, switching to state %d\n", pm_state.event);
1665
1666 state = pmz_uart_reg.state + uap->port.line;
1667
1668 mutex_lock(&pmz_irq_mutex);
1669 mutex_lock(&state->port.mutex);
1670
1671 spin_lock_irqsave(&uap->port.lock, flags);
1672
1673 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1674 /* Disable receiver and transmitter. */
1675 uap->curregs[R3] &= ~RxENABLE;
1676 uap->curregs[R5] &= ~TxENABLE;
1677
1678 /* Disable all interrupts and BRK assertion. */
1679 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1680 uap->curregs[R5] &= ~SND_BRK;
1681 pmz_load_zsregs(uap, uap->curregs);
1682 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1683 mb();
1684 }
1685
1686 spin_unlock_irqrestore(&uap->port.lock, flags);
1687
1688 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1689 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1690 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1691 disable_irq(uap->port.irq);
1692 }
1693
1694 if (ZS_IS_CONS(uap))
1695 uap->port.cons->flags &= ~CON_ENABLED;
1696
1697 /* Shut the chip down */
1698 pmz_set_scc_power(uap, 0);
1699
1700 mutex_unlock(&state->port.mutex);
1701 mutex_unlock(&pmz_irq_mutex);
1702
1703 pmz_debug("suspend, switching complete\n");
1704
1705 mdev->ofdev.dev.power.power_state = pm_state;
1706
1707 return 0;
1708}
1709
1710
1711static int pmz_resume(struct macio_dev *mdev)
1712{
1713 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1714 struct uart_state *state;
1715 unsigned long flags;
1716 int pwr_delay = 0;
1717
1718 if (uap == NULL)
1719 return 0;
1720
1721 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
1722 return 0;
1723
1724 pmz_debug("resume, switching to state 0\n");
1725
1726 state = pmz_uart_reg.state + uap->port.line;
1727
1728 mutex_lock(&pmz_irq_mutex);
1729 mutex_lock(&state->port.mutex);
1730
1731 spin_lock_irqsave(&uap->port.lock, flags);
1732 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1733 spin_unlock_irqrestore(&uap->port.lock, flags);
1734 goto bail;
1735 }
1736 pwr_delay = __pmz_startup(uap);
1737
1738 /* Take care of config that may have changed while asleep */
1739 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1740
1741 if (ZS_IS_OPEN(uap)) {
1742 /* Enable interrupts */
1743 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1744 if (!ZS_IS_EXTCLK(uap))
1745 uap->curregs[R1] |= EXT_INT_ENAB;
1746 write_zsreg(uap, R1, uap->curregs[R1]);
1747 }
1748
1749 spin_unlock_irqrestore(&uap->port.lock, flags);
1750
1751 if (ZS_IS_CONS(uap))
1752 uap->port.cons->flags |= CON_ENABLED;
1753
1754 /* Re-enable IRQ on the controller */
1755 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1756 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1757 enable_irq(uap->port.irq);
1758 }
1759
1760 bail:
1761 mutex_unlock(&state->port.mutex);
1762 mutex_unlock(&pmz_irq_mutex);
1763
1764 /* Right now, we deal with delay by blocking here, I'll be
1765 * smarter later on
1766 */
1767 if (pwr_delay != 0) {
1768 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1769 msleep(pwr_delay);
1770 }
1771
1772 pmz_debug("resume, switching complete\n");
1773
1774 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
1775
1776 return 0;
1777}
1778
1779/*
1780 * Probe all ports in the system and build the ports array, we register
1781 * with the serial layer at this point, the macio-type probing is only
1782 * used later to "attach" to the sysfs tree so we get power management
1783 * events
1784 */
1785static int __init pmz_probe(void)
1786{
1787 struct device_node *node_p, *node_a, *node_b, *np;
1788 int count = 0;
1789 int rc;
1790
1791 /*
1792 * Find all escc chips in the system
1793 */
1794 node_p = of_find_node_by_name(NULL, "escc");
1795 while (node_p) {
1796 /*
1797 * First get channel A/B node pointers
1798 *
1799 * TODO: Add routines with proper locking to do that...
1800 */
1801 node_a = node_b = NULL;
1802 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1803 if (strncmp(np->name, "ch-a", 4) == 0)
1804 node_a = of_node_get(np);
1805 else if (strncmp(np->name, "ch-b", 4) == 0)
1806 node_b = of_node_get(np);
1807 }
1808 if (!node_a && !node_b) {
1809 of_node_put(node_a);
1810 of_node_put(node_b);
1811 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1812 (!node_a) ? 'a' : 'b', node_p->full_name);
1813 goto next;
1814 }
1815
1816 /*
1817 * Fill basic fields in the port structures
1818 */
1819 pmz_ports[count].mate = &pmz_ports[count+1];
1820 pmz_ports[count+1].mate = &pmz_ports[count];
1821 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1822 pmz_ports[count].node = node_a;
1823 pmz_ports[count+1].node = node_b;
1824 pmz_ports[count].port.line = count;
1825 pmz_ports[count+1].port.line = count+1;
1826
1827 /*
1828 * Setup the ports for real
1829 */
1830 rc = pmz_init_port(&pmz_ports[count]);
1831 if (rc == 0 && node_b != NULL)
1832 rc = pmz_init_port(&pmz_ports[count+1]);
1833 if (rc != 0) {
1834 of_node_put(node_a);
1835 of_node_put(node_b);
1836 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1837 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1838 goto next;
1839 }
1840 count += 2;
1841next:
1842 node_p = of_find_node_by_name(node_p, "escc");
1843 }
1844 pmz_ports_count = count;
1845
1846 return 0;
1847}
1848
1849#else
1850
1851extern struct platform_device scc_a_pdev, scc_b_pdev;
1852
1853static int __init pmz_init_port(struct uart_pmac_port *uap)
1854{
1855 struct resource *r_ports;
1856 int irq;
1857
1858 r_ports = platform_get_resource(uap->node, IORESOURCE_MEM, 0);
1859 irq = platform_get_irq(uap->node, 0);
1860 if (!r_ports || !irq)
1861 return -ENODEV;
1862
1863 uap->port.mapbase = r_ports->start;
1864 uap->port.membase = (unsigned char __iomem *) r_ports->start;
1865 uap->port.iotype = UPIO_MEM;
1866 uap->port.irq = irq;
1867 uap->port.uartclk = ZS_CLOCK;
1868 uap->port.fifosize = 1;
1869 uap->port.ops = &pmz_pops;
1870 uap->port.type = PORT_PMAC_ZILOG;
1871 uap->port.flags = 0;
1872
1873 uap->control_reg = uap->port.membase;
1874 uap->data_reg = uap->control_reg + 4;
1875 uap->port_type = 0;
1876
1877 pmz_convert_to_zs(uap, CS8, 0, 9600);
1878
1879 return 0;
1880}
1881
1882static int __init pmz_probe(void)
1883{
1884 int err;
1885
1886 pmz_ports_count = 0;
1887
1888 pmz_ports[0].mate = &pmz_ports[1];
1889 pmz_ports[0].port.line = 0;
1890 pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1891 pmz_ports[0].node = &scc_a_pdev;
1892 err = pmz_init_port(&pmz_ports[0]);
1893 if (err)
1894 return err;
1895 pmz_ports_count++;
1896
1897 pmz_ports[1].mate = &pmz_ports[0];
1898 pmz_ports[1].port.line = 1;
1899 pmz_ports[1].flags = 0;
1900 pmz_ports[1].node = &scc_b_pdev;
1901 err = pmz_init_port(&pmz_ports[1]);
1902 if (err)
1903 return err;
1904 pmz_ports_count++;
1905
1906 return 0;
1907}
1908
1909static void pmz_dispose_port(struct uart_pmac_port *uap)
1910{
1911 memset(uap, 0, sizeof(struct uart_pmac_port));
1912}
1913
1914static int __init pmz_attach(struct platform_device *pdev)
1915{
1916 int i;
1917
1918 for (i = 0; i < pmz_ports_count; i++)
1919 if (pmz_ports[i].node == pdev)
1920 return 0;
1921 return -ENODEV;
1922}
1923
1924static int __exit pmz_detach(struct platform_device *pdev)
1925{
1926 return 0;
1927}
1928
1929#endif /* !CONFIG_PPC_PMAC */
1930
1931#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1932
1933static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1934static int __init pmz_console_setup(struct console *co, char *options);
1935
1936static struct console pmz_console = {
1937 .name = PMACZILOG_NAME,
1938 .write = pmz_console_write,
1939 .device = uart_console_device,
1940 .setup = pmz_console_setup,
1941 .flags = CON_PRINTBUFFER,
1942 .index = -1,
1943 .data = &pmz_uart_reg,
1944};
1945
1946#define PMACZILOG_CONSOLE &pmz_console
1947#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1948#define PMACZILOG_CONSOLE (NULL)
1949#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1950
1951/*
1952 * Register the driver, console driver and ports with the serial
1953 * core
1954 */
1955static int __init pmz_register(void)
1956{
1957 int i, rc;
1958
1959 pmz_uart_reg.nr = pmz_ports_count;
1960 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1961
1962 /*
1963 * Register this driver with the serial core
1964 */
1965 rc = uart_register_driver(&pmz_uart_reg);
1966 if (rc)
1967 return rc;
1968
1969 /*
1970 * Register each port with the serial core
1971 */
1972 for (i = 0; i < pmz_ports_count; i++) {
1973 struct uart_pmac_port *uport = &pmz_ports[i];
1974 /* NULL node may happen on wallstreet */
1975 if (uport->node != NULL)
1976 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1977 if (rc)
1978 goto err_out;
1979 }
1980
1981 return 0;
1982err_out:
1983 while (i-- > 0) {
1984 struct uart_pmac_port *uport = &pmz_ports[i];
1985 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1986 }
1987 uart_unregister_driver(&pmz_uart_reg);
1988 return rc;
1989}
1990
1991#ifdef CONFIG_PPC_PMAC
1992
1993static struct of_device_id pmz_match[] =
1994{
1995 {
1996 .name = "ch-a",
1997 },
1998 {
1999 .name = "ch-b",
2000 },
2001 {},
2002};
2003MODULE_DEVICE_TABLE (of, pmz_match);
2004
2005static struct macio_driver pmz_driver = {
2006 .driver = {
2007 .name = "pmac_zilog",
2008 .owner = THIS_MODULE,
2009 .of_match_table = pmz_match,
2010 },
2011 .probe = pmz_attach,
2012 .remove = pmz_detach,
2013 .suspend = pmz_suspend,
2014 .resume = pmz_resume,
2015};
2016
2017#else
2018
2019static struct platform_driver pmz_driver = {
2020 .remove = __exit_p(pmz_detach),
2021 .driver = {
2022 .name = "scc",
2023 .owner = THIS_MODULE,
2024 },
2025};
2026
2027#endif /* !CONFIG_PPC_PMAC */
2028
2029static int __init init_pmz(void)
2030{
2031 int rc, i;
2032 printk(KERN_INFO "%s\n", version);
2033
2034 /*
2035 * First, we need to do a direct OF-based probe pass. We
2036 * do that because we want serial console up before the
2037 * macio stuffs calls us back, and since that makes it
2038 * easier to pass the proper number of channels to
2039 * uart_register_driver()
2040 */
2041 if (pmz_ports_count == 0)
2042 pmz_probe();
2043
2044 /*
2045 * Bail early if no port found
2046 */
2047 if (pmz_ports_count == 0)
2048 return -ENODEV;
2049
2050 /*
2051 * Now we register with the serial layer
2052 */
2053 rc = pmz_register();
2054 if (rc) {
2055 printk(KERN_ERR
2056 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
2057 "pmac_zilog: Did another serial driver already claim the minors?\n");
2058 /* effectively "pmz_unprobe()" */
2059 for (i=0; i < pmz_ports_count; i++)
2060 pmz_dispose_port(&pmz_ports[i]);
2061 return rc;
2062 }
2063
2064 /*
2065 * Then we register the macio driver itself
2066 */
2067#ifdef CONFIG_PPC_PMAC
2068 return macio_register_driver(&pmz_driver);
2069#else
2070 return platform_driver_probe(&pmz_driver, pmz_attach);
2071#endif
2072}
2073
2074static void __exit exit_pmz(void)
2075{
2076 int i;
2077
2078#ifdef CONFIG_PPC_PMAC
2079 /* Get rid of macio-driver (detach from macio) */
2080 macio_unregister_driver(&pmz_driver);
2081#else
2082 platform_driver_unregister(&pmz_driver);
2083#endif
2084
2085 for (i = 0; i < pmz_ports_count; i++) {
2086 struct uart_pmac_port *uport = &pmz_ports[i];
2087 if (uport->node != NULL) {
2088 uart_remove_one_port(&pmz_uart_reg, &uport->port);
2089 pmz_dispose_port(uport);
2090 }
2091 }
2092 /* Unregister UART driver */
2093 uart_unregister_driver(&pmz_uart_reg);
2094}
2095
2096#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
2097
2098static void pmz_console_putchar(struct uart_port *port, int ch)
2099{
2100 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
2101
2102 /* Wait for the transmit buffer to empty. */
2103 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
2104 udelay(5);
2105 write_zsdata(uap, ch);
2106}
2107
2108/*
2109 * Print a string to the serial port trying not to disturb
2110 * any possible real use of the port...
2111 */
2112static void pmz_console_write(struct console *con, const char *s, unsigned int count)
2113{
2114 struct uart_pmac_port *uap = &pmz_ports[con->index];
2115 unsigned long flags;
2116
2117 if (ZS_IS_ASLEEP(uap))
2118 return;
2119 spin_lock_irqsave(&uap->port.lock, flags);
2120
2121 /* Turn of interrupts and enable the transmitter. */
2122 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
2123 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
2124
2125 uart_console_write(&uap->port, s, count, pmz_console_putchar);
2126
2127 /* Restore the values in the registers. */
2128 write_zsreg(uap, R1, uap->curregs[1]);
2129 /* Don't disable the transmitter. */
2130
2131 spin_unlock_irqrestore(&uap->port.lock, flags);
2132}
2133
2134/*
2135 * Setup the serial console
2136 */
2137static int __init pmz_console_setup(struct console *co, char *options)
2138{
2139 struct uart_pmac_port *uap;
2140 struct uart_port *port;
2141 int baud = 38400;
2142 int bits = 8;
2143 int parity = 'n';
2144 int flow = 'n';
2145 unsigned long pwr_delay;
2146
2147 /*
2148 * XServe's default to 57600 bps
2149 */
2150 if (of_machine_is_compatible("RackMac1,1")
2151 || of_machine_is_compatible("RackMac1,2")
2152 || of_machine_is_compatible("MacRISC4"))
2153 baud = 57600;
2154
2155 /*
2156 * Check whether an invalid uart number has been specified, and
2157 * if so, search for the first available port that does have
2158 * console support.
2159 */
2160 if (co->index >= pmz_ports_count)
2161 co->index = 0;
2162 uap = &pmz_ports[co->index];
2163 if (uap->node == NULL)
2164 return -ENODEV;
2165 port = &uap->port;
2166
2167 /*
2168 * Mark port as beeing a console
2169 */
2170 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2171
2172 /*
2173 * Temporary fix for uart layer who didn't setup the spinlock yet
2174 */
2175 spin_lock_init(&port->lock);
2176
2177 /*
2178 * Enable the hardware
2179 */
2180 pwr_delay = __pmz_startup(uap);
2181 if (pwr_delay)
2182 mdelay(pwr_delay);
2183
2184 if (options)
2185 uart_parse_options(options, &baud, &parity, &bits, &flow);
2186
2187 return uart_set_options(port, co, baud, parity, bits, flow);
2188}
2189
2190static int __init pmz_console_init(void)
2191{
2192 /* Probe ports */
2193 pmz_probe();
2194
2195 /* TODO: Autoprobe console based on OF */
2196 /* pmz_console.index = i; */
2197 register_console(&pmz_console);
2198
2199 return 0;
2200
2201}
2202console_initcall(pmz_console_init);
2203#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2204
2205module_init(init_pmz);
2206module_exit(exit_pmz);
diff --git a/drivers/tty/serial/pmac_zilog.h b/drivers/tty/serial/pmac_zilog.h
new file mode 100644
index 000000000000..cbc34fbb1b20
--- /dev/null
+++ b/drivers/tty/serial/pmac_zilog.h
@@ -0,0 +1,396 @@
1#ifndef __PMAC_ZILOG_H__
2#define __PMAC_ZILOG_H__
3
4#ifdef CONFIG_PPC_PMAC
5#define pmz_debug(fmt, arg...) dev_dbg(&uap->dev->ofdev.dev, fmt, ## arg)
6#define pmz_error(fmt, arg...) dev_err(&uap->dev->ofdev.dev, fmt, ## arg)
7#define pmz_info(fmt, arg...) dev_info(&uap->dev->ofdev.dev, fmt, ## arg)
8#else
9#define pmz_debug(fmt, arg...) dev_dbg(&uap->node->dev, fmt, ## arg)
10#define pmz_error(fmt, arg...) dev_err(&uap->node->dev, fmt, ## arg)
11#define pmz_info(fmt, arg...) dev_info(&uap->node->dev, fmt, ## arg)
12#endif
13
14/*
15 * At most 2 ESCCs with 2 ports each
16 */
17#define MAX_ZS_PORTS 4
18
19/*
20 * We wrap our port structure around the generic uart_port.
21 */
22#define NUM_ZSREGS 17
23
24struct uart_pmac_port {
25 struct uart_port port;
26 struct uart_pmac_port *mate;
27
28#ifdef CONFIG_PPC_PMAC
29 /* macio_dev for the escc holding this port (maybe be null on
30 * early inited port)
31 */
32 struct macio_dev *dev;
33 /* device node to this port, this points to one of 2 childs
34 * of "escc" node (ie. ch-a or ch-b)
35 */
36 struct device_node *node;
37#else
38 struct platform_device *node;
39#endif
40
41 /* Port type as obtained from device tree (IRDA, modem, ...) */
42 int port_type;
43 u8 curregs[NUM_ZSREGS];
44
45 unsigned int flags;
46#define PMACZILOG_FLAG_IS_CONS 0x00000001
47#define PMACZILOG_FLAG_IS_KGDB 0x00000002
48#define PMACZILOG_FLAG_MODEM_STATUS 0x00000004
49#define PMACZILOG_FLAG_IS_CHANNEL_A 0x00000008
50#define PMACZILOG_FLAG_REGS_HELD 0x00000010
51#define PMACZILOG_FLAG_TX_STOPPED 0x00000020
52#define PMACZILOG_FLAG_TX_ACTIVE 0x00000040
53#define PMACZILOG_FLAG_ENABLED 0x00000080
54#define PMACZILOG_FLAG_IS_IRDA 0x00000100
55#define PMACZILOG_FLAG_IS_INTMODEM 0x00000200
56#define PMACZILOG_FLAG_HAS_DMA 0x00000400
57#define PMACZILOG_FLAG_RSRC_REQUESTED 0x00000800
58#define PMACZILOG_FLAG_IS_ASLEEP 0x00001000
59#define PMACZILOG_FLAG_IS_OPEN 0x00002000
60#define PMACZILOG_FLAG_IS_IRQ_ON 0x00004000
61#define PMACZILOG_FLAG_IS_EXTCLK 0x00008000
62#define PMACZILOG_FLAG_BREAK 0x00010000
63
64 unsigned char parity_mask;
65 unsigned char prev_status;
66
67 volatile u8 __iomem *control_reg;
68 volatile u8 __iomem *data_reg;
69
70#ifdef CONFIG_PPC_PMAC
71 unsigned int tx_dma_irq;
72 unsigned int rx_dma_irq;
73 volatile struct dbdma_regs __iomem *tx_dma_regs;
74 volatile struct dbdma_regs __iomem *rx_dma_regs;
75#endif
76
77 struct ktermios termios_cache;
78};
79
80#define to_pmz(p) ((struct uart_pmac_port *)(p))
81
82static inline struct uart_pmac_port *pmz_get_port_A(struct uart_pmac_port *uap)
83{
84 if (uap->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
85 return uap;
86 return uap->mate;
87}
88
89/*
90 * Register accessors. Note that we don't need to enforce a recovery
91 * delay on PCI PowerMac hardware, it's dealt in HW by the MacIO chip,
92 * though if we try to use this driver on older machines, we might have
93 * to add it back
94 */
95static inline u8 read_zsreg(struct uart_pmac_port *port, u8 reg)
96{
97 if (reg != 0)
98 writeb(reg, port->control_reg);
99 return readb(port->control_reg);
100}
101
102static inline void write_zsreg(struct uart_pmac_port *port, u8 reg, u8 value)
103{
104 if (reg != 0)
105 writeb(reg, port->control_reg);
106 writeb(value, port->control_reg);
107}
108
109static inline u8 read_zsdata(struct uart_pmac_port *port)
110{
111 return readb(port->data_reg);
112}
113
114static inline void write_zsdata(struct uart_pmac_port *port, u8 data)
115{
116 writeb(data, port->data_reg);
117}
118
119static inline void zssync(struct uart_pmac_port *port)
120{
121 (void)readb(port->control_reg);
122}
123
124/* Conversion routines to/from brg time constants from/to bits
125 * per second.
126 */
127#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
128#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
129
130#define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */
131
132/* The Zilog register set */
133
134#define FLAG 0x7e
135
136/* Write Register 0 */
137#define R0 0 /* Register selects */
138#define R1 1
139#define R2 2
140#define R3 3
141#define R4 4
142#define R5 5
143#define R6 6
144#define R7 7
145#define R8 8
146#define R9 9
147#define R10 10
148#define R11 11
149#define R12 12
150#define R13 13
151#define R14 14
152#define R15 15
153#define R7P 16
154
155#define NULLCODE 0 /* Null Code */
156#define POINT_HIGH 0x8 /* Select upper half of registers */
157#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
158#define SEND_ABORT 0x18 /* HDLC Abort */
159#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
160#define RES_Tx_P 0x28 /* Reset TxINT Pending */
161#define ERR_RES 0x30 /* Error Reset */
162#define RES_H_IUS 0x38 /* Reset highest IUS */
163
164#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
165#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
166#define RES_EOM_L 0xC0 /* Reset EOM latch */
167
168/* Write Register 1 */
169
170#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
171#define TxINT_ENAB 0x2 /* Tx Int Enable */
172#define PAR_SPEC 0x4 /* Parity is special condition */
173
174#define RxINT_DISAB 0 /* Rx Int Disable */
175#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
176#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
177#define INT_ERR_Rx 0x18 /* Int on error only */
178#define RxINT_MASK 0x18
179
180#define WT_RDY_RT 0x20 /* W/Req reflects recv if 1, xmit if 0 */
181#define WT_FN_RDYFN 0x40 /* W/Req pin is DMA request if 1, wait if 0 */
182#define WT_RDY_ENAB 0x80 /* Enable W/Req pin */
183
184/* Write Register #2 (Interrupt Vector) */
185
186/* Write Register 3 */
187
188#define RxENABLE 0x1 /* Rx Enable */
189#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
190#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
191#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
192#define ENT_HM 0x10 /* Enter Hunt Mode */
193#define AUTO_ENAB 0x20 /* Auto Enables */
194#define Rx5 0x0 /* Rx 5 Bits/Character */
195#define Rx7 0x40 /* Rx 7 Bits/Character */
196#define Rx6 0x80 /* Rx 6 Bits/Character */
197#define Rx8 0xc0 /* Rx 8 Bits/Character */
198#define RxN_MASK 0xc0
199
200/* Write Register 4 */
201
202#define PAR_ENAB 0x1 /* Parity Enable */
203#define PAR_EVEN 0x2 /* Parity Even/Odd* */
204
205#define SYNC_ENAB 0 /* Sync Modes Enable */
206#define SB1 0x4 /* 1 stop bit/char */
207#define SB15 0x8 /* 1.5 stop bits/char */
208#define SB2 0xc /* 2 stop bits/char */
209#define SB_MASK 0xc
210
211#define MONSYNC 0 /* 8 Bit Sync character */
212#define BISYNC 0x10 /* 16 bit sync character */
213#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
214#define EXTSYNC 0x30 /* External Sync Mode */
215
216#define X1CLK 0x0 /* x1 clock mode */
217#define X16CLK 0x40 /* x16 clock mode */
218#define X32CLK 0x80 /* x32 clock mode */
219#define X64CLK 0xC0 /* x64 clock mode */
220#define XCLK_MASK 0xC0
221
222/* Write Register 5 */
223
224#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
225#define RTS 0x2 /* RTS */
226#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
227#define TxENABLE 0x8 /* Tx Enable */
228#define SND_BRK 0x10 /* Send Break */
229#define Tx5 0x0 /* Tx 5 bits (or less)/character */
230#define Tx7 0x20 /* Tx 7 bits/character */
231#define Tx6 0x40 /* Tx 6 bits/character */
232#define Tx8 0x60 /* Tx 8 bits/character */
233#define TxN_MASK 0x60
234#define DTR 0x80 /* DTR */
235
236/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
237
238/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
239
240/* Write Register 7' (Some enhanced feature control) */
241#define ENEXREAD 0x40 /* Enable read of some write registers */
242
243/* Write Register 8 (transmit buffer) */
244
245/* Write Register 9 (Master interrupt control) */
246#define VIS 1 /* Vector Includes Status */
247#define NV 2 /* No Vector */
248#define DLC 4 /* Disable Lower Chain */
249#define MIE 8 /* Master Interrupt Enable */
250#define STATHI 0x10 /* Status high */
251#define NORESET 0 /* No reset on write to R9 */
252#define CHRB 0x40 /* Reset channel B */
253#define CHRA 0x80 /* Reset channel A */
254#define FHWRES 0xc0 /* Force hardware reset */
255
256/* Write Register 10 (misc control bits) */
257#define BIT6 1 /* 6 bit/8bit sync */
258#define LOOPMODE 2 /* SDLC Loop mode */
259#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
260#define MARKIDLE 8 /* Mark/flag on idle */
261#define GAOP 0x10 /* Go active on poll */
262#define NRZ 0 /* NRZ mode */
263#define NRZI 0x20 /* NRZI mode */
264#define FM1 0x40 /* FM1 (transition = 1) */
265#define FM0 0x60 /* FM0 (transition = 0) */
266#define CRCPS 0x80 /* CRC Preset I/O */
267
268/* Write Register 11 (Clock Mode control) */
269#define TRxCXT 0 /* TRxC = Xtal output */
270#define TRxCTC 1 /* TRxC = Transmit clock */
271#define TRxCBR 2 /* TRxC = BR Generator Output */
272#define TRxCDP 3 /* TRxC = DPLL output */
273#define TRxCOI 4 /* TRxC O/I */
274#define TCRTxCP 0 /* Transmit clock = RTxC pin */
275#define TCTRxCP 8 /* Transmit clock = TRxC pin */
276#define TCBR 0x10 /* Transmit clock = BR Generator output */
277#define TCDPLL 0x18 /* Transmit clock = DPLL output */
278#define RCRTxCP 0 /* Receive clock = RTxC pin */
279#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
280#define RCBR 0x40 /* Receive clock = BR Generator output */
281#define RCDPLL 0x60 /* Receive clock = DPLL output */
282#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
283
284/* Write Register 12 (lower byte of baud rate generator time constant) */
285
286/* Write Register 13 (upper byte of baud rate generator time constant) */
287
288/* Write Register 14 (Misc control bits) */
289#define BRENAB 1 /* Baud rate generator enable */
290#define BRSRC 2 /* Baud rate generator source */
291#define DTRREQ 4 /* DTR/Request function */
292#define AUTOECHO 8 /* Auto Echo */
293#define LOOPBAK 0x10 /* Local loopback */
294#define SEARCH 0x20 /* Enter search mode */
295#define RMC 0x40 /* Reset missing clock */
296#define DISDPLL 0x60 /* Disable DPLL */
297#define SSBR 0x80 /* Set DPLL source = BR generator */
298#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
299#define SFMM 0xc0 /* Set FM mode */
300#define SNRZI 0xe0 /* Set NRZI mode */
301
302/* Write Register 15 (external/status interrupt control) */
303#define EN85C30 1 /* Enable some 85c30-enhanced registers */
304#define ZCIE 2 /* Zero count IE */
305#define ENSTFIFO 4 /* Enable status FIFO (SDLC) */
306#define DCDIE 8 /* DCD IE */
307#define SYNCIE 0x10 /* Sync/hunt IE */
308#define CTSIE 0x20 /* CTS IE */
309#define TxUIE 0x40 /* Tx Underrun/EOM IE */
310#define BRKIE 0x80 /* Break/Abort IE */
311
312
313/* Read Register 0 */
314#define Rx_CH_AV 0x1 /* Rx Character Available */
315#define ZCOUNT 0x2 /* Zero count */
316#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
317#define DCD 0x8 /* DCD */
318#define SYNC_HUNT 0x10 /* Sync/hunt */
319#define CTS 0x20 /* CTS */
320#define TxEOM 0x40 /* Tx underrun */
321#define BRK_ABRT 0x80 /* Break/Abort */
322
323/* Read Register 1 */
324#define ALL_SNT 0x1 /* All sent */
325/* Residue Data for 8 Rx bits/char programmed */
326#define RES3 0x8 /* 0/3 */
327#define RES4 0x4 /* 0/4 */
328#define RES5 0xc /* 0/5 */
329#define RES6 0x2 /* 0/6 */
330#define RES7 0xa /* 0/7 */
331#define RES8 0x6 /* 0/8 */
332#define RES18 0xe /* 1/8 */
333#define RES28 0x0 /* 2/8 */
334/* Special Rx Condition Interrupts */
335#define PAR_ERR 0x10 /* Parity error */
336#define Rx_OVR 0x20 /* Rx Overrun Error */
337#define CRC_ERR 0x40 /* CRC/Framing Error */
338#define END_FR 0x80 /* End of Frame (SDLC) */
339
340/* Read Register 2 (channel b only) - Interrupt vector */
341#define CHB_Tx_EMPTY 0x00
342#define CHB_EXT_STAT 0x02
343#define CHB_Rx_AVAIL 0x04
344#define CHB_SPECIAL 0x06
345#define CHA_Tx_EMPTY 0x08
346#define CHA_EXT_STAT 0x0a
347#define CHA_Rx_AVAIL 0x0c
348#define CHA_SPECIAL 0x0e
349#define STATUS_MASK 0x06
350
351/* Read Register 3 (interrupt pending register) ch a only */
352#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
353#define CHBTxIP 0x2 /* Channel B Tx IP */
354#define CHBRxIP 0x4 /* Channel B Rx IP */
355#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
356#define CHATxIP 0x10 /* Channel A Tx IP */
357#define CHARxIP 0x20 /* Channel A Rx IP */
358
359/* Read Register 8 (receive data register) */
360
361/* Read Register 10 (misc status bits) */
362#define ONLOOP 2 /* On loop */
363#define LOOPSEND 0x10 /* Loop sending */
364#define CLK2MIS 0x40 /* Two clocks missing */
365#define CLK1MIS 0x80 /* One clock missing */
366
367/* Read Register 12 (lower byte of baud rate generator constant) */
368
369/* Read Register 13 (upper byte of baud rate generator constant) */
370
371/* Read Register 15 (value of WR 15) */
372
373/* Misc macros */
374#define ZS_CLEARERR(port) (write_zsreg(port, 0, ERR_RES))
375#define ZS_CLEARFIFO(port) do { volatile unsigned char garbage; \
376 garbage = read_zsdata(port); \
377 garbage = read_zsdata(port); \
378 garbage = read_zsdata(port); \
379 } while(0)
380
381#define ZS_IS_CONS(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CONS)
382#define ZS_IS_KGDB(UP) ((UP)->flags & PMACZILOG_FLAG_IS_KGDB)
383#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & PMACZILOG_FLAG_IS_CHANNEL_A)
384#define ZS_REGS_HELD(UP) ((UP)->flags & PMACZILOG_FLAG_REGS_HELD)
385#define ZS_TX_STOPPED(UP) ((UP)->flags & PMACZILOG_FLAG_TX_STOPPED)
386#define ZS_TX_ACTIVE(UP) ((UP)->flags & PMACZILOG_FLAG_TX_ACTIVE)
387#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & PMACZILOG_FLAG_MODEM_STATUS)
388#define ZS_IS_IRDA(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRDA)
389#define ZS_IS_INTMODEM(UP) ((UP)->flags & PMACZILOG_FLAG_IS_INTMODEM)
390#define ZS_HAS_DMA(UP) ((UP)->flags & PMACZILOG_FLAG_HAS_DMA)
391#define ZS_IS_ASLEEP(UP) ((UP)->flags & PMACZILOG_FLAG_IS_ASLEEP)
392#define ZS_IS_OPEN(UP) ((UP)->flags & PMACZILOG_FLAG_IS_OPEN)
393#define ZS_IS_IRQ_ON(UP) ((UP)->flags & PMACZILOG_FLAG_IS_IRQ_ON)
394#define ZS_IS_EXTCLK(UP) ((UP)->flags & PMACZILOG_FLAG_IS_EXTCLK)
395
396#endif /* __PMAC_ZILOG_H__ */
diff --git a/drivers/tty/serial/pnx8xxx_uart.c b/drivers/tty/serial/pnx8xxx_uart.c
new file mode 100644
index 000000000000..0aa75a97531c
--- /dev/null
+++ b/drivers/tty/serial/pnx8xxx_uart.c
@@ -0,0 +1,854 @@
1/*
2 * UART driver for PNX8XXX SoCs
3 *
4 * Author: Per Hallsmark per.hallsmark@mvista.com
5 * Ported to 2.6 kernel by EmbeddedAlley
6 * Reworked by Vitaly Wool <vitalywool@gmail.com>
7 *
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of
13 * any kind, whether express or implied.
14 *
15 */
16
17#if defined(CONFIG_SERIAL_PNX8XXX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
21#include <linux/module.h>
22#include <linux/ioport.h>
23#include <linux/init.h>
24#include <linux/console.h>
25#include <linux/sysrq.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial_core.h>
31#include <linux/serial.h>
32#include <linux/serial_pnx8xxx.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36
37/* We'll be using StrongARM sa1100 serial port major/minor */
38#define SERIAL_PNX8XXX_MAJOR 204
39#define MINOR_START 5
40
41#define NR_PORTS 2
42
43#define PNX8XXX_ISR_PASS_LIMIT 256
44
45/*
46 * Convert from ignore_status_mask or read_status_mask to FIFO
47 * and interrupt status bits
48 */
49#define SM_TO_FIFO(x) ((x) >> 10)
50#define SM_TO_ISTAT(x) ((x) & 0x000001ff)
51#define FIFO_TO_SM(x) ((x) << 10)
52#define ISTAT_TO_SM(x) ((x) & 0x000001ff)
53
54/*
55 * This is the size of our serial port register set.
56 */
57#define UART_PORT_SIZE 0x1000
58
59/*
60 * This determines how often we check the modem status signals
61 * for any change. They generally aren't connected to an IRQ
62 * so we have to poll them. We also check immediately before
63 * filling the TX fifo incase CTS has been dropped.
64 */
65#define MCTRL_TIMEOUT (250*HZ/1000)
66
67extern struct pnx8xxx_port pnx8xxx_ports[];
68
69static inline int serial_in(struct pnx8xxx_port *sport, int offset)
70{
71 return (__raw_readl(sport->port.membase + offset));
72}
73
74static inline void serial_out(struct pnx8xxx_port *sport, int offset, int value)
75{
76 __raw_writel(value, sport->port.membase + offset);
77}
78
79/*
80 * Handle any change of modem status signal since we were last called.
81 */
82static void pnx8xxx_mctrl_check(struct pnx8xxx_port *sport)
83{
84 unsigned int status, changed;
85
86 status = sport->port.ops->get_mctrl(&sport->port);
87 changed = status ^ sport->old_status;
88
89 if (changed == 0)
90 return;
91
92 sport->old_status = status;
93
94 if (changed & TIOCM_RI)
95 sport->port.icount.rng++;
96 if (changed & TIOCM_DSR)
97 sport->port.icount.dsr++;
98 if (changed & TIOCM_CAR)
99 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
100 if (changed & TIOCM_CTS)
101 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
102
103 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
104}
105
106/*
107 * This is our per-port timeout handler, for checking the
108 * modem status signals.
109 */
110static void pnx8xxx_timeout(unsigned long data)
111{
112 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)data;
113 unsigned long flags;
114
115 if (sport->port.state) {
116 spin_lock_irqsave(&sport->port.lock, flags);
117 pnx8xxx_mctrl_check(sport);
118 spin_unlock_irqrestore(&sport->port.lock, flags);
119
120 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
121 }
122}
123
124/*
125 * interrupts disabled on entry
126 */
127static void pnx8xxx_stop_tx(struct uart_port *port)
128{
129 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
130 u32 ien;
131
132 /* Disable TX intr */
133 ien = serial_in(sport, PNX8XXX_IEN);
134 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLTX);
135
136 /* Clear all pending TX intr */
137 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
138}
139
140/*
141 * interrupts may not be disabled on entry
142 */
143static void pnx8xxx_start_tx(struct uart_port *port)
144{
145 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
146 u32 ien;
147
148 /* Clear all pending TX intr */
149 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
150
151 /* Enable TX intr */
152 ien = serial_in(sport, PNX8XXX_IEN);
153 serial_out(sport, PNX8XXX_IEN, ien | PNX8XXX_UART_INT_ALLTX);
154}
155
156/*
157 * Interrupts enabled
158 */
159static void pnx8xxx_stop_rx(struct uart_port *port)
160{
161 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
162 u32 ien;
163
164 /* Disable RX intr */
165 ien = serial_in(sport, PNX8XXX_IEN);
166 serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLRX);
167
168 /* Clear all pending RX intr */
169 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX);
170}
171
172/*
173 * Set the modem control timer to fire immediately.
174 */
175static void pnx8xxx_enable_ms(struct uart_port *port)
176{
177 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
178
179 mod_timer(&sport->timer, jiffies);
180}
181
182static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
183{
184 struct tty_struct *tty = sport->port.state->port.tty;
185 unsigned int status, ch, flg;
186
187 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
188 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
189 while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
190 ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
191
192 sport->port.icount.rx++;
193
194 flg = TTY_NORMAL;
195
196 /*
197 * note that the error handling code is
198 * out of the main execution path
199 */
200 if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
201 PNX8XXX_UART_FIFO_RXPAR |
202 PNX8XXX_UART_FIFO_RXBRK) |
203 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
204 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
205 status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
206 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
207 sport->port.icount.brk++;
208 if (uart_handle_break(&sport->port))
209 goto ignore_char;
210 } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
211 sport->port.icount.parity++;
212 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
213 sport->port.icount.frame++;
214 if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
215 sport->port.icount.overrun++;
216
217 status &= sport->port.read_status_mask;
218
219 if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
220 flg = TTY_PARITY;
221 else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
222 flg = TTY_FRAME;
223
224#ifdef SUPPORT_SYSRQ
225 sport->port.sysrq = 0;
226#endif
227 }
228
229 if (uart_handle_sysrq_char(&sport->port, ch))
230 goto ignore_char;
231
232 uart_insert_char(&sport->port, status,
233 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
234
235 ignore_char:
236 serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
237 PNX8XXX_UART_LCR_RX_NEXT);
238 status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
239 ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
240 }
241 tty_flip_buffer_push(tty);
242}
243
244static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
245{
246 struct circ_buf *xmit = &sport->port.state->xmit;
247
248 if (sport->port.x_char) {
249 serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
250 sport->port.icount.tx++;
251 sport->port.x_char = 0;
252 return;
253 }
254
255 /*
256 * Check the modem control lines before
257 * transmitting anything.
258 */
259 pnx8xxx_mctrl_check(sport);
260
261 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
262 pnx8xxx_stop_tx(&sport->port);
263 return;
264 }
265
266 /*
267 * TX while bytes available
268 */
269 while (((serial_in(sport, PNX8XXX_FIFO) &
270 PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
271 serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
272 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
273 sport->port.icount.tx++;
274 if (uart_circ_empty(xmit))
275 break;
276 }
277
278 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
279 uart_write_wakeup(&sport->port);
280
281 if (uart_circ_empty(xmit))
282 pnx8xxx_stop_tx(&sport->port);
283}
284
285static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
286{
287 struct pnx8xxx_port *sport = dev_id;
288 unsigned int status;
289
290 spin_lock(&sport->port.lock);
291 /* Get the interrupts */
292 status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
293
294 /* Byte or break signal received */
295 if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
296 pnx8xxx_rx_chars(sport);
297
298 /* TX holding register empty - transmit a byte */
299 if (status & PNX8XXX_UART_INT_TX)
300 pnx8xxx_tx_chars(sport);
301
302 /* Clear the ISTAT register */
303 serial_out(sport, PNX8XXX_ICLR, status);
304
305 spin_unlock(&sport->port.lock);
306 return IRQ_HANDLED;
307}
308
309/*
310 * Return TIOCSER_TEMT when transmitter is not busy.
311 */
312static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
313{
314 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
315
316 return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
317}
318
319static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
320{
321 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
322 unsigned int mctrl = TIOCM_DSR;
323 unsigned int msr;
324
325 /* REVISIT */
326
327 msr = serial_in(sport, PNX8XXX_MCR);
328
329 mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
330 mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
331
332 return mctrl;
333}
334
335static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
336{
337#if 0 /* FIXME */
338 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
339 unsigned int msr;
340#endif
341}
342
343/*
344 * Interrupts always disabled.
345 */
346static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
347{
348 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
349 unsigned long flags;
350 unsigned int lcr;
351
352 spin_lock_irqsave(&sport->port.lock, flags);
353 lcr = serial_in(sport, PNX8XXX_LCR);
354 if (break_state == -1)
355 lcr |= PNX8XXX_UART_LCR_TXBREAK;
356 else
357 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
358 serial_out(sport, PNX8XXX_LCR, lcr);
359 spin_unlock_irqrestore(&sport->port.lock, flags);
360}
361
362static int pnx8xxx_startup(struct uart_port *port)
363{
364 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
365 int retval;
366
367 /*
368 * Allocate the IRQ
369 */
370 retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
371 "pnx8xxx-uart", sport);
372 if (retval)
373 return retval;
374
375 /*
376 * Finally, clear and enable interrupts
377 */
378
379 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
380 PNX8XXX_UART_INT_ALLTX);
381
382 serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
383 PNX8XXX_UART_INT_ALLRX |
384 PNX8XXX_UART_INT_ALLTX);
385
386 /*
387 * Enable modem status interrupts
388 */
389 spin_lock_irq(&sport->port.lock);
390 pnx8xxx_enable_ms(&sport->port);
391 spin_unlock_irq(&sport->port.lock);
392
393 return 0;
394}
395
396static void pnx8xxx_shutdown(struct uart_port *port)
397{
398 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
399 int lcr;
400
401 /*
402 * Stop our timer.
403 */
404 del_timer_sync(&sport->timer);
405
406 /*
407 * Disable all interrupts
408 */
409 serial_out(sport, PNX8XXX_IEN, 0);
410
411 /*
412 * Reset the Tx and Rx FIFOS, disable the break condition
413 */
414 lcr = serial_in(sport, PNX8XXX_LCR);
415 lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
416 lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
417 serial_out(sport, PNX8XXX_LCR, lcr);
418
419 /*
420 * Clear all interrupts
421 */
422 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
423 PNX8XXX_UART_INT_ALLTX);
424
425 /*
426 * Free the interrupt
427 */
428 free_irq(sport->port.irq, sport);
429}
430
431static void
432pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
433 struct ktermios *old)
434{
435 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
436 unsigned long flags;
437 unsigned int lcr_fcr, old_ien, baud, quot;
438 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
439
440 /*
441 * We only support CS7 and CS8.
442 */
443 while ((termios->c_cflag & CSIZE) != CS7 &&
444 (termios->c_cflag & CSIZE) != CS8) {
445 termios->c_cflag &= ~CSIZE;
446 termios->c_cflag |= old_csize;
447 old_csize = CS8;
448 }
449
450 if ((termios->c_cflag & CSIZE) == CS8)
451 lcr_fcr = PNX8XXX_UART_LCR_8BIT;
452 else
453 lcr_fcr = 0;
454
455 if (termios->c_cflag & CSTOPB)
456 lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
457 if (termios->c_cflag & PARENB) {
458 lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
459 if (!(termios->c_cflag & PARODD))
460 lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
461 }
462
463 /*
464 * Ask the core to calculate the divisor for us.
465 */
466 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
467 quot = uart_get_divisor(port, baud);
468
469 spin_lock_irqsave(&sport->port.lock, flags);
470
471 sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
472 ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
473 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
474 if (termios->c_iflag & INPCK)
475 sport->port.read_status_mask |=
476 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
477 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
478 if (termios->c_iflag & (BRKINT | PARMRK))
479 sport->port.read_status_mask |=
480 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
481
482 /*
483 * Characters to ignore
484 */
485 sport->port.ignore_status_mask = 0;
486 if (termios->c_iflag & IGNPAR)
487 sport->port.ignore_status_mask |=
488 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
489 FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
490 if (termios->c_iflag & IGNBRK) {
491 sport->port.ignore_status_mask |=
492 ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
493 /*
494 * If we're ignoring parity and break indicators,
495 * ignore overruns too (for real raw support).
496 */
497 if (termios->c_iflag & IGNPAR)
498 sport->port.ignore_status_mask |=
499 ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
500 }
501
502 /*
503 * ignore all characters if CREAD is not set
504 */
505 if ((termios->c_cflag & CREAD) == 0)
506 sport->port.ignore_status_mask |=
507 ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
508
509 del_timer_sync(&sport->timer);
510
511 /*
512 * Update the per-port timeout.
513 */
514 uart_update_timeout(port, termios->c_cflag, baud);
515
516 /*
517 * disable interrupts and drain transmitter
518 */
519 old_ien = serial_in(sport, PNX8XXX_IEN);
520 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
521 PNX8XXX_UART_INT_ALLRX));
522
523 while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
524 barrier();
525
526 /* then, disable everything */
527 serial_out(sport, PNX8XXX_IEN, 0);
528
529 /* Reset the Rx and Tx FIFOs too */
530 lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
531 lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
532
533 /* set the parity, stop bits and data size */
534 serial_out(sport, PNX8XXX_LCR, lcr_fcr);
535
536 /* set the baud rate */
537 quot -= 1;
538 serial_out(sport, PNX8XXX_BAUD, quot);
539
540 serial_out(sport, PNX8XXX_ICLR, -1);
541
542 serial_out(sport, PNX8XXX_IEN, old_ien);
543
544 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
545 pnx8xxx_enable_ms(&sport->port);
546
547 spin_unlock_irqrestore(&sport->port.lock, flags);
548}
549
550static const char *pnx8xxx_type(struct uart_port *port)
551{
552 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
553
554 return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
555}
556
557/*
558 * Release the memory region(s) being used by 'port'.
559 */
560static void pnx8xxx_release_port(struct uart_port *port)
561{
562 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
563
564 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
565}
566
567/*
568 * Request the memory region(s) being used by 'port'.
569 */
570static int pnx8xxx_request_port(struct uart_port *port)
571{
572 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
573 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
574 "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
575}
576
577/*
578 * Configure/autoconfigure the port.
579 */
580static void pnx8xxx_config_port(struct uart_port *port, int flags)
581{
582 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
583
584 if (flags & UART_CONFIG_TYPE &&
585 pnx8xxx_request_port(&sport->port) == 0)
586 sport->port.type = PORT_PNX8XXX;
587}
588
589/*
590 * Verify the new serial_struct (for TIOCSSERIAL).
591 * The only change we allow are to the flags and type, and
592 * even then only between PORT_PNX8XXX and PORT_UNKNOWN
593 */
594static int
595pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
596{
597 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
598 int ret = 0;
599
600 if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
601 ret = -EINVAL;
602 if (sport->port.irq != ser->irq)
603 ret = -EINVAL;
604 if (ser->io_type != SERIAL_IO_MEM)
605 ret = -EINVAL;
606 if (sport->port.uartclk / 16 != ser->baud_base)
607 ret = -EINVAL;
608 if ((void *)sport->port.mapbase != ser->iomem_base)
609 ret = -EINVAL;
610 if (sport->port.iobase != ser->port)
611 ret = -EINVAL;
612 if (ser->hub6 != 0)
613 ret = -EINVAL;
614 return ret;
615}
616
617static struct uart_ops pnx8xxx_pops = {
618 .tx_empty = pnx8xxx_tx_empty,
619 .set_mctrl = pnx8xxx_set_mctrl,
620 .get_mctrl = pnx8xxx_get_mctrl,
621 .stop_tx = pnx8xxx_stop_tx,
622 .start_tx = pnx8xxx_start_tx,
623 .stop_rx = pnx8xxx_stop_rx,
624 .enable_ms = pnx8xxx_enable_ms,
625 .break_ctl = pnx8xxx_break_ctl,
626 .startup = pnx8xxx_startup,
627 .shutdown = pnx8xxx_shutdown,
628 .set_termios = pnx8xxx_set_termios,
629 .type = pnx8xxx_type,
630 .release_port = pnx8xxx_release_port,
631 .request_port = pnx8xxx_request_port,
632 .config_port = pnx8xxx_config_port,
633 .verify_port = pnx8xxx_verify_port,
634};
635
636
637/*
638 * Setup the PNX8XXX serial ports.
639 *
640 * Note also that we support "console=ttySx" where "x" is either 0 or 1.
641 */
642static void __init pnx8xxx_init_ports(void)
643{
644 static int first = 1;
645 int i;
646
647 if (!first)
648 return;
649 first = 0;
650
651 for (i = 0; i < NR_PORTS; i++) {
652 init_timer(&pnx8xxx_ports[i].timer);
653 pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
654 pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
655 pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
656 }
657}
658
659#ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
660
661static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
662{
663 struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
664 int status;
665
666 do {
667 /* Wait for UART_TX register to empty */
668 status = serial_in(sport, PNX8XXX_FIFO);
669 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
670 serial_out(sport, PNX8XXX_FIFO, ch);
671}
672
673/*
674 * Interrupts are disabled on entering
675 */static void
676pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
677{
678 struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
679 unsigned int old_ien, status;
680
681 /*
682 * First, save IEN and then disable interrupts
683 */
684 old_ien = serial_in(sport, PNX8XXX_IEN);
685 serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
686 PNX8XXX_UART_INT_ALLRX));
687
688 uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
689
690 /*
691 * Finally, wait for transmitter to become empty
692 * and restore IEN
693 */
694 do {
695 /* Wait for UART_TX register to empty */
696 status = serial_in(sport, PNX8XXX_FIFO);
697 } while (status & PNX8XXX_UART_FIFO_TXFIFO);
698
699 /* Clear TX and EMPTY interrupt */
700 serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
701 PNX8XXX_UART_INT_EMPTY);
702
703 serial_out(sport, PNX8XXX_IEN, old_ien);
704}
705
706static int __init
707pnx8xxx_console_setup(struct console *co, char *options)
708{
709 struct pnx8xxx_port *sport;
710 int baud = 38400;
711 int bits = 8;
712 int parity = 'n';
713 int flow = 'n';
714
715 /*
716 * Check whether an invalid uart number has been specified, and
717 * if so, search for the first available port that does have
718 * console support.
719 */
720 if (co->index == -1 || co->index >= NR_PORTS)
721 co->index = 0;
722 sport = &pnx8xxx_ports[co->index];
723
724 if (options)
725 uart_parse_options(options, &baud, &parity, &bits, &flow);
726
727 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
728}
729
730static struct uart_driver pnx8xxx_reg;
731static struct console pnx8xxx_console = {
732 .name = "ttyS",
733 .write = pnx8xxx_console_write,
734 .device = uart_console_device,
735 .setup = pnx8xxx_console_setup,
736 .flags = CON_PRINTBUFFER,
737 .index = -1,
738 .data = &pnx8xxx_reg,
739};
740
741static int __init pnx8xxx_rs_console_init(void)
742{
743 pnx8xxx_init_ports();
744 register_console(&pnx8xxx_console);
745 return 0;
746}
747console_initcall(pnx8xxx_rs_console_init);
748
749#define PNX8XXX_CONSOLE &pnx8xxx_console
750#else
751#define PNX8XXX_CONSOLE NULL
752#endif
753
754static struct uart_driver pnx8xxx_reg = {
755 .owner = THIS_MODULE,
756 .driver_name = "ttyS",
757 .dev_name = "ttyS",
758 .major = SERIAL_PNX8XXX_MAJOR,
759 .minor = MINOR_START,
760 .nr = NR_PORTS,
761 .cons = PNX8XXX_CONSOLE,
762};
763
764static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
765{
766 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
767
768 return uart_suspend_port(&pnx8xxx_reg, &sport->port);
769}
770
771static int pnx8xxx_serial_resume(struct platform_device *pdev)
772{
773 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
774
775 return uart_resume_port(&pnx8xxx_reg, &sport->port);
776}
777
778static int pnx8xxx_serial_probe(struct platform_device *pdev)
779{
780 struct resource *res = pdev->resource;
781 int i;
782
783 for (i = 0; i < pdev->num_resources; i++, res++) {
784 if (!(res->flags & IORESOURCE_MEM))
785 continue;
786
787 for (i = 0; i < NR_PORTS; i++) {
788 if (pnx8xxx_ports[i].port.mapbase != res->start)
789 continue;
790
791 pnx8xxx_ports[i].port.dev = &pdev->dev;
792 uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
793 platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
794 break;
795 }
796 }
797
798 return 0;
799}
800
801static int pnx8xxx_serial_remove(struct platform_device *pdev)
802{
803 struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
804
805 platform_set_drvdata(pdev, NULL);
806
807 if (sport)
808 uart_remove_one_port(&pnx8xxx_reg, &sport->port);
809
810 return 0;
811}
812
813static struct platform_driver pnx8xxx_serial_driver = {
814 .driver = {
815 .name = "pnx8xxx-uart",
816 .owner = THIS_MODULE,
817 },
818 .probe = pnx8xxx_serial_probe,
819 .remove = pnx8xxx_serial_remove,
820 .suspend = pnx8xxx_serial_suspend,
821 .resume = pnx8xxx_serial_resume,
822};
823
824static int __init pnx8xxx_serial_init(void)
825{
826 int ret;
827
828 printk(KERN_INFO "Serial: PNX8XXX driver\n");
829
830 pnx8xxx_init_ports();
831
832 ret = uart_register_driver(&pnx8xxx_reg);
833 if (ret == 0) {
834 ret = platform_driver_register(&pnx8xxx_serial_driver);
835 if (ret)
836 uart_unregister_driver(&pnx8xxx_reg);
837 }
838 return ret;
839}
840
841static void __exit pnx8xxx_serial_exit(void)
842{
843 platform_driver_unregister(&pnx8xxx_serial_driver);
844 uart_unregister_driver(&pnx8xxx_reg);
845}
846
847module_init(pnx8xxx_serial_init);
848module_exit(pnx8xxx_serial_exit);
849
850MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
851MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
852MODULE_LICENSE("GPL");
853MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
854MODULE_ALIAS("platform:pnx8xxx-uart");
diff --git a/drivers/tty/serial/pxa.c b/drivers/tty/serial/pxa.c
new file mode 100644
index 000000000000..4302e6e3768e
--- /dev/null
+++ b/drivers/tty/serial/pxa.c
@@ -0,0 +1,877 @@
1/*
2 * Based on drivers/serial/8250.c by Russell King.
3 *
4 * Author: Nicolas Pitre
5 * Created: Feb 20, 2003
6 * Copyright: (C) 2003 Monta Vista Software, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Note 1: This driver is made separate from the already too overloaded
14 * 8250.c because it needs some kirks of its own and that'll make it
15 * easier to add DMA support.
16 *
17 * Note 2: I'm too sick of device allocation policies for serial ports.
18 * If someone else wants to request an "official" allocation of major/minor
19 * for this driver please be my guest. And don't forget that new hardware
20 * to come from Intel might have more than 3 or 4 of those UARTs. Let's
21 * hope for a better port registration and dynamic device allocation scheme
22 * with the serial core maintainer satisfaction to appear soon.
23 */
24
25
26#if defined(CONFIG_SERIAL_PXA_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27#define SUPPORT_SYSRQ
28#endif
29
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/serial_reg.h>
36#include <linux/circ_buf.h>
37#include <linux/delay.h>
38#include <linux/interrupt.h>
39#include <linux/platform_device.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/clk.h>
44#include <linux/io.h>
45#include <linux/slab.h>
46
47struct uart_pxa_port {
48 struct uart_port port;
49 unsigned char ier;
50 unsigned char lcr;
51 unsigned char mcr;
52 unsigned int lsr_break_flag;
53 struct clk *clk;
54 char *name;
55};
56
57static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
58{
59 offset <<= 2;
60 return readl(up->port.membase + offset);
61}
62
63static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
64{
65 offset <<= 2;
66 writel(value, up->port.membase + offset);
67}
68
69static void serial_pxa_enable_ms(struct uart_port *port)
70{
71 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
72
73 up->ier |= UART_IER_MSI;
74 serial_out(up, UART_IER, up->ier);
75}
76
77static void serial_pxa_stop_tx(struct uart_port *port)
78{
79 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
80
81 if (up->ier & UART_IER_THRI) {
82 up->ier &= ~UART_IER_THRI;
83 serial_out(up, UART_IER, up->ier);
84 }
85}
86
87static void serial_pxa_stop_rx(struct uart_port *port)
88{
89 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
90
91 up->ier &= ~UART_IER_RLSI;
92 up->port.read_status_mask &= ~UART_LSR_DR;
93 serial_out(up, UART_IER, up->ier);
94}
95
96static inline void receive_chars(struct uart_pxa_port *up, int *status)
97{
98 struct tty_struct *tty = up->port.state->port.tty;
99 unsigned int ch, flag;
100 int max_count = 256;
101
102 do {
103 ch = serial_in(up, UART_RX);
104 flag = TTY_NORMAL;
105 up->port.icount.rx++;
106
107 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
108 UART_LSR_FE | UART_LSR_OE))) {
109 /*
110 * For statistics only
111 */
112 if (*status & UART_LSR_BI) {
113 *status &= ~(UART_LSR_FE | UART_LSR_PE);
114 up->port.icount.brk++;
115 /*
116 * We do the SysRQ and SAK checking
117 * here because otherwise the break
118 * may get masked by ignore_status_mask
119 * or read_status_mask.
120 */
121 if (uart_handle_break(&up->port))
122 goto ignore_char;
123 } else if (*status & UART_LSR_PE)
124 up->port.icount.parity++;
125 else if (*status & UART_LSR_FE)
126 up->port.icount.frame++;
127 if (*status & UART_LSR_OE)
128 up->port.icount.overrun++;
129
130 /*
131 * Mask off conditions which should be ignored.
132 */
133 *status &= up->port.read_status_mask;
134
135#ifdef CONFIG_SERIAL_PXA_CONSOLE
136 if (up->port.line == up->port.cons->index) {
137 /* Recover the break flag from console xmit */
138 *status |= up->lsr_break_flag;
139 up->lsr_break_flag = 0;
140 }
141#endif
142 if (*status & UART_LSR_BI) {
143 flag = TTY_BREAK;
144 } else if (*status & UART_LSR_PE)
145 flag = TTY_PARITY;
146 else if (*status & UART_LSR_FE)
147 flag = TTY_FRAME;
148 }
149
150 if (uart_handle_sysrq_char(&up->port, ch))
151 goto ignore_char;
152
153 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
154
155 ignore_char:
156 *status = serial_in(up, UART_LSR);
157 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
158 tty_flip_buffer_push(tty);
159}
160
161static void transmit_chars(struct uart_pxa_port *up)
162{
163 struct circ_buf *xmit = &up->port.state->xmit;
164 int count;
165
166 if (up->port.x_char) {
167 serial_out(up, UART_TX, up->port.x_char);
168 up->port.icount.tx++;
169 up->port.x_char = 0;
170 return;
171 }
172 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
173 serial_pxa_stop_tx(&up->port);
174 return;
175 }
176
177 count = up->port.fifosize / 2;
178 do {
179 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
180 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
181 up->port.icount.tx++;
182 if (uart_circ_empty(xmit))
183 break;
184 } while (--count > 0);
185
186 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
187 uart_write_wakeup(&up->port);
188
189
190 if (uart_circ_empty(xmit))
191 serial_pxa_stop_tx(&up->port);
192}
193
194static void serial_pxa_start_tx(struct uart_port *port)
195{
196 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
197
198 if (!(up->ier & UART_IER_THRI)) {
199 up->ier |= UART_IER_THRI;
200 serial_out(up, UART_IER, up->ier);
201 }
202}
203
204static inline void check_modem_status(struct uart_pxa_port *up)
205{
206 int status;
207
208 status = serial_in(up, UART_MSR);
209
210 if ((status & UART_MSR_ANY_DELTA) == 0)
211 return;
212
213 if (status & UART_MSR_TERI)
214 up->port.icount.rng++;
215 if (status & UART_MSR_DDSR)
216 up->port.icount.dsr++;
217 if (status & UART_MSR_DDCD)
218 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
219 if (status & UART_MSR_DCTS)
220 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
221
222 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
223}
224
225/*
226 * This handles the interrupt from one port.
227 */
228static inline irqreturn_t serial_pxa_irq(int irq, void *dev_id)
229{
230 struct uart_pxa_port *up = dev_id;
231 unsigned int iir, lsr;
232
233 iir = serial_in(up, UART_IIR);
234 if (iir & UART_IIR_NO_INT)
235 return IRQ_NONE;
236 lsr = serial_in(up, UART_LSR);
237 if (lsr & UART_LSR_DR)
238 receive_chars(up, &lsr);
239 check_modem_status(up);
240 if (lsr & UART_LSR_THRE)
241 transmit_chars(up);
242 return IRQ_HANDLED;
243}
244
245static unsigned int serial_pxa_tx_empty(struct uart_port *port)
246{
247 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
248 unsigned long flags;
249 unsigned int ret;
250
251 spin_lock_irqsave(&up->port.lock, flags);
252 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
253 spin_unlock_irqrestore(&up->port.lock, flags);
254
255 return ret;
256}
257
258static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
259{
260 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
261 unsigned char status;
262 unsigned int ret;
263
264 status = serial_in(up, UART_MSR);
265
266 ret = 0;
267 if (status & UART_MSR_DCD)
268 ret |= TIOCM_CAR;
269 if (status & UART_MSR_RI)
270 ret |= TIOCM_RNG;
271 if (status & UART_MSR_DSR)
272 ret |= TIOCM_DSR;
273 if (status & UART_MSR_CTS)
274 ret |= TIOCM_CTS;
275 return ret;
276}
277
278static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
279{
280 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
281 unsigned char mcr = 0;
282
283 if (mctrl & TIOCM_RTS)
284 mcr |= UART_MCR_RTS;
285 if (mctrl & TIOCM_DTR)
286 mcr |= UART_MCR_DTR;
287 if (mctrl & TIOCM_OUT1)
288 mcr |= UART_MCR_OUT1;
289 if (mctrl & TIOCM_OUT2)
290 mcr |= UART_MCR_OUT2;
291 if (mctrl & TIOCM_LOOP)
292 mcr |= UART_MCR_LOOP;
293
294 mcr |= up->mcr;
295
296 serial_out(up, UART_MCR, mcr);
297}
298
299static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
300{
301 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
302 unsigned long flags;
303
304 spin_lock_irqsave(&up->port.lock, flags);
305 if (break_state == -1)
306 up->lcr |= UART_LCR_SBC;
307 else
308 up->lcr &= ~UART_LCR_SBC;
309 serial_out(up, UART_LCR, up->lcr);
310 spin_unlock_irqrestore(&up->port.lock, flags);
311}
312
313#if 0
314static void serial_pxa_dma_init(struct pxa_uart *up)
315{
316 up->rxdma =
317 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
318 if (up->rxdma < 0)
319 goto out;
320 up->txdma =
321 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
322 if (up->txdma < 0)
323 goto err_txdma;
324 up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
325 if (!up->dmadesc)
326 goto err_alloc;
327
328 /* ... */
329err_alloc:
330 pxa_free_dma(up->txdma);
331err_rxdma:
332 pxa_free_dma(up->rxdma);
333out:
334 return;
335}
336#endif
337
338static int serial_pxa_startup(struct uart_port *port)
339{
340 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
341 unsigned long flags;
342 int retval;
343
344 if (port->line == 3) /* HWUART */
345 up->mcr |= UART_MCR_AFE;
346 else
347 up->mcr = 0;
348
349 up->port.uartclk = clk_get_rate(up->clk);
350
351 /*
352 * Allocate the IRQ
353 */
354 retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
355 if (retval)
356 return retval;
357
358 /*
359 * Clear the FIFO buffers and disable them.
360 * (they will be reenabled in set_termios())
361 */
362 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
363 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
364 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
365 serial_out(up, UART_FCR, 0);
366
367 /*
368 * Clear the interrupt registers.
369 */
370 (void) serial_in(up, UART_LSR);
371 (void) serial_in(up, UART_RX);
372 (void) serial_in(up, UART_IIR);
373 (void) serial_in(up, UART_MSR);
374
375 /*
376 * Now, initialize the UART
377 */
378 serial_out(up, UART_LCR, UART_LCR_WLEN8);
379
380 spin_lock_irqsave(&up->port.lock, flags);
381 up->port.mctrl |= TIOCM_OUT2;
382 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
383 spin_unlock_irqrestore(&up->port.lock, flags);
384
385 /*
386 * Finally, enable interrupts. Note: Modem status interrupts
387 * are set via set_termios(), which will be occurring imminently
388 * anyway, so we don't enable them here.
389 */
390 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
391 serial_out(up, UART_IER, up->ier);
392
393 /*
394 * And clear the interrupt registers again for luck.
395 */
396 (void) serial_in(up, UART_LSR);
397 (void) serial_in(up, UART_RX);
398 (void) serial_in(up, UART_IIR);
399 (void) serial_in(up, UART_MSR);
400
401 return 0;
402}
403
404static void serial_pxa_shutdown(struct uart_port *port)
405{
406 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
407 unsigned long flags;
408
409 free_irq(up->port.irq, up);
410
411 /*
412 * Disable interrupts from this port
413 */
414 up->ier = 0;
415 serial_out(up, UART_IER, 0);
416
417 spin_lock_irqsave(&up->port.lock, flags);
418 up->port.mctrl &= ~TIOCM_OUT2;
419 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
420 spin_unlock_irqrestore(&up->port.lock, flags);
421
422 /*
423 * Disable break condition and FIFOs
424 */
425 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
426 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
427 UART_FCR_CLEAR_RCVR |
428 UART_FCR_CLEAR_XMIT);
429 serial_out(up, UART_FCR, 0);
430}
431
432static void
433serial_pxa_set_termios(struct uart_port *port, struct ktermios *termios,
434 struct ktermios *old)
435{
436 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
437 unsigned char cval, fcr = 0;
438 unsigned long flags;
439 unsigned int baud, quot;
440 unsigned int dll;
441
442 switch (termios->c_cflag & CSIZE) {
443 case CS5:
444 cval = UART_LCR_WLEN5;
445 break;
446 case CS6:
447 cval = UART_LCR_WLEN6;
448 break;
449 case CS7:
450 cval = UART_LCR_WLEN7;
451 break;
452 default:
453 case CS8:
454 cval = UART_LCR_WLEN8;
455 break;
456 }
457
458 if (termios->c_cflag & CSTOPB)
459 cval |= UART_LCR_STOP;
460 if (termios->c_cflag & PARENB)
461 cval |= UART_LCR_PARITY;
462 if (!(termios->c_cflag & PARODD))
463 cval |= UART_LCR_EPAR;
464
465 /*
466 * Ask the core to calculate the divisor for us.
467 */
468 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
469 quot = uart_get_divisor(port, baud);
470
471 if ((up->port.uartclk / quot) < (2400 * 16))
472 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
473 else if ((up->port.uartclk / quot) < (230400 * 16))
474 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
475 else
476 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR32;
477
478 /*
479 * Ok, we're now changing the port state. Do it with
480 * interrupts disabled.
481 */
482 spin_lock_irqsave(&up->port.lock, flags);
483
484 /*
485 * Ensure the port will be enabled.
486 * This is required especially for serial console.
487 */
488 up->ier |= UART_IER_UUE;
489
490 /*
491 * Update the per-port timeout.
492 */
493 uart_update_timeout(port, termios->c_cflag, baud);
494
495 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
496 if (termios->c_iflag & INPCK)
497 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
498 if (termios->c_iflag & (BRKINT | PARMRK))
499 up->port.read_status_mask |= UART_LSR_BI;
500
501 /*
502 * Characters to ignore
503 */
504 up->port.ignore_status_mask = 0;
505 if (termios->c_iflag & IGNPAR)
506 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
507 if (termios->c_iflag & IGNBRK) {
508 up->port.ignore_status_mask |= UART_LSR_BI;
509 /*
510 * If we're ignoring parity and break indicators,
511 * ignore overruns too (for real raw support).
512 */
513 if (termios->c_iflag & IGNPAR)
514 up->port.ignore_status_mask |= UART_LSR_OE;
515 }
516
517 /*
518 * ignore all characters if CREAD is not set
519 */
520 if ((termios->c_cflag & CREAD) == 0)
521 up->port.ignore_status_mask |= UART_LSR_DR;
522
523 /*
524 * CTS flow control flag and modem status interrupts
525 */
526 up->ier &= ~UART_IER_MSI;
527 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
528 up->ier |= UART_IER_MSI;
529
530 serial_out(up, UART_IER, up->ier);
531
532 if (termios->c_cflag & CRTSCTS)
533 up->mcr |= UART_MCR_AFE;
534 else
535 up->mcr &= ~UART_MCR_AFE;
536
537 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
538 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
539
540 /*
541 * work around Errata #75 according to Intel(R) PXA27x Processor Family
542 * Specification Update (Nov 2005)
543 */
544 dll = serial_in(up, UART_DLL);
545 WARN_ON(dll != (quot & 0xff));
546
547 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
548 serial_out(up, UART_LCR, cval); /* reset DLAB */
549 up->lcr = cval; /* Save LCR */
550 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
551 serial_out(up, UART_FCR, fcr);
552 spin_unlock_irqrestore(&up->port.lock, flags);
553}
554
555static void
556serial_pxa_pm(struct uart_port *port, unsigned int state,
557 unsigned int oldstate)
558{
559 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
560
561 if (!state)
562 clk_enable(up->clk);
563 else
564 clk_disable(up->clk);
565}
566
567static void serial_pxa_release_port(struct uart_port *port)
568{
569}
570
571static int serial_pxa_request_port(struct uart_port *port)
572{
573 return 0;
574}
575
576static void serial_pxa_config_port(struct uart_port *port, int flags)
577{
578 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
579 up->port.type = PORT_PXA;
580}
581
582static int
583serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
584{
585 /* we don't want the core code to modify any port params */
586 return -EINVAL;
587}
588
589static const char *
590serial_pxa_type(struct uart_port *port)
591{
592 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
593 return up->name;
594}
595
596static struct uart_pxa_port *serial_pxa_ports[4];
597static struct uart_driver serial_pxa_reg;
598
599#ifdef CONFIG_SERIAL_PXA_CONSOLE
600
601#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
602
603/*
604 * Wait for transmitter & holding register to empty
605 */
606static inline void wait_for_xmitr(struct uart_pxa_port *up)
607{
608 unsigned int status, tmout = 10000;
609
610 /* Wait up to 10ms for the character(s) to be sent. */
611 do {
612 status = serial_in(up, UART_LSR);
613
614 if (status & UART_LSR_BI)
615 up->lsr_break_flag = UART_LSR_BI;
616
617 if (--tmout == 0)
618 break;
619 udelay(1);
620 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
621
622 /* Wait up to 1s for flow control if necessary */
623 if (up->port.flags & UPF_CONS_FLOW) {
624 tmout = 1000000;
625 while (--tmout &&
626 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
627 udelay(1);
628 }
629}
630
631static void serial_pxa_console_putchar(struct uart_port *port, int ch)
632{
633 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
634
635 wait_for_xmitr(up);
636 serial_out(up, UART_TX, ch);
637}
638
639/*
640 * Print a string to the serial port trying not to disturb
641 * any possible real use of the port...
642 *
643 * The console_lock must be held when we get here.
644 */
645static void
646serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
647{
648 struct uart_pxa_port *up = serial_pxa_ports[co->index];
649 unsigned int ier;
650
651 clk_enable(up->clk);
652
653 /*
654 * First save the IER then disable the interrupts
655 */
656 ier = serial_in(up, UART_IER);
657 serial_out(up, UART_IER, UART_IER_UUE);
658
659 uart_console_write(&up->port, s, count, serial_pxa_console_putchar);
660
661 /*
662 * Finally, wait for transmitter to become empty
663 * and restore the IER
664 */
665 wait_for_xmitr(up);
666 serial_out(up, UART_IER, ier);
667
668 clk_disable(up->clk);
669}
670
671static int __init
672serial_pxa_console_setup(struct console *co, char *options)
673{
674 struct uart_pxa_port *up;
675 int baud = 9600;
676 int bits = 8;
677 int parity = 'n';
678 int flow = 'n';
679
680 if (co->index == -1 || co->index >= serial_pxa_reg.nr)
681 co->index = 0;
682 up = serial_pxa_ports[co->index];
683 if (!up)
684 return -ENODEV;
685
686 if (options)
687 uart_parse_options(options, &baud, &parity, &bits, &flow);
688
689 return uart_set_options(&up->port, co, baud, parity, bits, flow);
690}
691
692static struct console serial_pxa_console = {
693 .name = "ttyS",
694 .write = serial_pxa_console_write,
695 .device = uart_console_device,
696 .setup = serial_pxa_console_setup,
697 .flags = CON_PRINTBUFFER,
698 .index = -1,
699 .data = &serial_pxa_reg,
700};
701
702#define PXA_CONSOLE &serial_pxa_console
703#else
704#define PXA_CONSOLE NULL
705#endif
706
707struct uart_ops serial_pxa_pops = {
708 .tx_empty = serial_pxa_tx_empty,
709 .set_mctrl = serial_pxa_set_mctrl,
710 .get_mctrl = serial_pxa_get_mctrl,
711 .stop_tx = serial_pxa_stop_tx,
712 .start_tx = serial_pxa_start_tx,
713 .stop_rx = serial_pxa_stop_rx,
714 .enable_ms = serial_pxa_enable_ms,
715 .break_ctl = serial_pxa_break_ctl,
716 .startup = serial_pxa_startup,
717 .shutdown = serial_pxa_shutdown,
718 .set_termios = serial_pxa_set_termios,
719 .pm = serial_pxa_pm,
720 .type = serial_pxa_type,
721 .release_port = serial_pxa_release_port,
722 .request_port = serial_pxa_request_port,
723 .config_port = serial_pxa_config_port,
724 .verify_port = serial_pxa_verify_port,
725};
726
727static struct uart_driver serial_pxa_reg = {
728 .owner = THIS_MODULE,
729 .driver_name = "PXA serial",
730 .dev_name = "ttyS",
731 .major = TTY_MAJOR,
732 .minor = 64,
733 .nr = 4,
734 .cons = PXA_CONSOLE,
735};
736
737#ifdef CONFIG_PM
738static int serial_pxa_suspend(struct device *dev)
739{
740 struct uart_pxa_port *sport = dev_get_drvdata(dev);
741
742 if (sport)
743 uart_suspend_port(&serial_pxa_reg, &sport->port);
744
745 return 0;
746}
747
748static int serial_pxa_resume(struct device *dev)
749{
750 struct uart_pxa_port *sport = dev_get_drvdata(dev);
751
752 if (sport)
753 uart_resume_port(&serial_pxa_reg, &sport->port);
754
755 return 0;
756}
757
758static const struct dev_pm_ops serial_pxa_pm_ops = {
759 .suspend = serial_pxa_suspend,
760 .resume = serial_pxa_resume,
761};
762#endif
763
764static int serial_pxa_probe(struct platform_device *dev)
765{
766 struct uart_pxa_port *sport;
767 struct resource *mmres, *irqres;
768 int ret;
769
770 mmres = platform_get_resource(dev, IORESOURCE_MEM, 0);
771 irqres = platform_get_resource(dev, IORESOURCE_IRQ, 0);
772 if (!mmres || !irqres)
773 return -ENODEV;
774
775 sport = kzalloc(sizeof(struct uart_pxa_port), GFP_KERNEL);
776 if (!sport)
777 return -ENOMEM;
778
779 sport->clk = clk_get(&dev->dev, NULL);
780 if (IS_ERR(sport->clk)) {
781 ret = PTR_ERR(sport->clk);
782 goto err_free;
783 }
784
785 sport->port.type = PORT_PXA;
786 sport->port.iotype = UPIO_MEM;
787 sport->port.mapbase = mmres->start;
788 sport->port.irq = irqres->start;
789 sport->port.fifosize = 64;
790 sport->port.ops = &serial_pxa_pops;
791 sport->port.line = dev->id;
792 sport->port.dev = &dev->dev;
793 sport->port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
794 sport->port.uartclk = clk_get_rate(sport->clk);
795
796 switch (dev->id) {
797 case 0: sport->name = "FFUART"; break;
798 case 1: sport->name = "BTUART"; break;
799 case 2: sport->name = "STUART"; break;
800 case 3: sport->name = "HWUART"; break;
801 default:
802 sport->name = "???";
803 break;
804 }
805
806 sport->port.membase = ioremap(mmres->start, mmres->end - mmres->start + 1);
807 if (!sport->port.membase) {
808 ret = -ENOMEM;
809 goto err_clk;
810 }
811
812 serial_pxa_ports[dev->id] = sport;
813
814 uart_add_one_port(&serial_pxa_reg, &sport->port);
815 platform_set_drvdata(dev, sport);
816
817 return 0;
818
819 err_clk:
820 clk_put(sport->clk);
821 err_free:
822 kfree(sport);
823 return ret;
824}
825
826static int serial_pxa_remove(struct platform_device *dev)
827{
828 struct uart_pxa_port *sport = platform_get_drvdata(dev);
829
830 platform_set_drvdata(dev, NULL);
831
832 uart_remove_one_port(&serial_pxa_reg, &sport->port);
833 clk_put(sport->clk);
834 kfree(sport);
835
836 return 0;
837}
838
839static struct platform_driver serial_pxa_driver = {
840 .probe = serial_pxa_probe,
841 .remove = serial_pxa_remove,
842
843 .driver = {
844 .name = "pxa2xx-uart",
845 .owner = THIS_MODULE,
846#ifdef CONFIG_PM
847 .pm = &serial_pxa_pm_ops,
848#endif
849 },
850};
851
852int __init serial_pxa_init(void)
853{
854 int ret;
855
856 ret = uart_register_driver(&serial_pxa_reg);
857 if (ret != 0)
858 return ret;
859
860 ret = platform_driver_register(&serial_pxa_driver);
861 if (ret != 0)
862 uart_unregister_driver(&serial_pxa_reg);
863
864 return ret;
865}
866
867void __exit serial_pxa_exit(void)
868{
869 platform_driver_unregister(&serial_pxa_driver);
870 uart_unregister_driver(&serial_pxa_reg);
871}
872
873module_init(serial_pxa_init);
874module_exit(serial_pxa_exit);
875
876MODULE_LICENSE("GPL");
877MODULE_ALIAS("platform:pxa2xx-uart");
diff --git a/drivers/tty/serial/s3c2400.c b/drivers/tty/serial/s3c2400.c
new file mode 100644
index 000000000000..d13051b3df87
--- /dev/null
+++ b/drivers/tty/serial/s3c2400.c
@@ -0,0 +1,105 @@
1/*
2 * Driver for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2005 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16
17#include <asm/irq.h>
18
19#include <mach/hardware.h>
20
21#include <plat/regs-serial.h>
22#include <mach/regs-gpio.h>
23
24#include "samsung.h"
25
26static int s3c2400_serial_getsource(struct uart_port *port,
27 struct s3c24xx_uart_clksrc *clk)
28{
29 clk->divisor = 1;
30 clk->name = "pclk";
31
32 return 0;
33}
34
35static int s3c2400_serial_setsource(struct uart_port *port,
36 struct s3c24xx_uart_clksrc *clk)
37{
38 return 0;
39}
40
41static int s3c2400_serial_resetport(struct uart_port *port,
42 struct s3c2410_uartcfg *cfg)
43{
44 dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
45 port, port->mapbase, cfg);
46
47 wr_regl(port, S3C2410_UCON, cfg->ucon);
48 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
49
50 /* reset both fifos */
51
52 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
53 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
54
55 return 0;
56}
57
58static struct s3c24xx_uart_info s3c2400_uart_inf = {
59 .name = "Samsung S3C2400 UART",
60 .type = PORT_S3C2400,
61 .fifosize = 16,
62 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
63 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
64 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
65 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
66 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
67 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
68 .get_clksrc = s3c2400_serial_getsource,
69 .set_clksrc = s3c2400_serial_setsource,
70 .reset_port = s3c2400_serial_resetport,
71};
72
73static int s3c2400_serial_probe(struct platform_device *dev)
74{
75 return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
76}
77
78static struct platform_driver s3c2400_serial_driver = {
79 .probe = s3c2400_serial_probe,
80 .remove = __devexit_p(s3c24xx_serial_remove),
81 .driver = {
82 .name = "s3c2400-uart",
83 .owner = THIS_MODULE,
84 },
85};
86
87s3c24xx_console_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
88
89static inline int s3c2400_serial_init(void)
90{
91 return s3c24xx_serial_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
92}
93
94static inline void s3c2400_serial_exit(void)
95{
96 platform_driver_unregister(&s3c2400_serial_driver);
97}
98
99module_init(s3c2400_serial_init);
100module_exit(s3c2400_serial_exit);
101
102MODULE_LICENSE("GPL v2");
103MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
104MODULE_DESCRIPTION("Samsung S3C2400 SoC Serial port driver");
105MODULE_ALIAS("platform:s3c2400-uart");
diff --git a/drivers/tty/serial/s3c2410.c b/drivers/tty/serial/s3c2410.c
new file mode 100644
index 000000000000..bffe6ff9b158
--- /dev/null
+++ b/drivers/tty/serial/s3c2410.c
@@ -0,0 +1,117 @@
1/*
2 * Driver for Samsung S3C2410 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28static int s3c2410_serial_setsource(struct uart_port *port,
29 struct s3c24xx_uart_clksrc *clk)
30{
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 if (strcmp(clk->name, "uclk") == 0)
34 ucon |= S3C2410_UCON_UCLK;
35 else
36 ucon &= ~S3C2410_UCON_UCLK;
37
38 wr_regl(port, S3C2410_UCON, ucon);
39 return 0;
40}
41
42static int s3c2410_serial_getsource(struct uart_port *port,
43 struct s3c24xx_uart_clksrc *clk)
44{
45 unsigned long ucon = rd_regl(port, S3C2410_UCON);
46
47 clk->divisor = 1;
48 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
49
50 return 0;
51}
52
53static int s3c2410_serial_resetport(struct uart_port *port,
54 struct s3c2410_uartcfg *cfg)
55{
56 dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
57 port, port->mapbase, cfg);
58
59 wr_regl(port, S3C2410_UCON, cfg->ucon);
60 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
61
62 /* reset both fifos */
63
64 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
65 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
66
67 return 0;
68}
69
70static struct s3c24xx_uart_info s3c2410_uart_inf = {
71 .name = "Samsung S3C2410 UART",
72 .type = PORT_S3C2410,
73 .fifosize = 16,
74 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
75 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
76 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
77 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
78 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
79 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
80 .get_clksrc = s3c2410_serial_getsource,
81 .set_clksrc = s3c2410_serial_setsource,
82 .reset_port = s3c2410_serial_resetport,
83};
84
85static int s3c2410_serial_probe(struct platform_device *dev)
86{
87 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
88}
89
90static struct platform_driver s3c2410_serial_driver = {
91 .probe = s3c2410_serial_probe,
92 .remove = __devexit_p(s3c24xx_serial_remove),
93 .driver = {
94 .name = "s3c2410-uart",
95 .owner = THIS_MODULE,
96 },
97};
98
99s3c24xx_console_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
100
101static int __init s3c2410_serial_init(void)
102{
103 return s3c24xx_serial_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
104}
105
106static void __exit s3c2410_serial_exit(void)
107{
108 platform_driver_unregister(&s3c2410_serial_driver);
109}
110
111module_init(s3c2410_serial_init);
112module_exit(s3c2410_serial_exit);
113
114MODULE_LICENSE("GPL v2");
115MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
116MODULE_DESCRIPTION("Samsung S3C2410 SoC Serial port driver");
117MODULE_ALIAS("platform:s3c2410-uart");
diff --git a/drivers/tty/serial/s3c2412.c b/drivers/tty/serial/s3c2412.c
new file mode 100644
index 000000000000..7e2b9504a687
--- /dev/null
+++ b/drivers/tty/serial/s3c2412.c
@@ -0,0 +1,151 @@
1/*
2 * Driver for Samsung S3C2412 and S3C2413 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28static int s3c2412_serial_setsource(struct uart_port *port,
29 struct s3c24xx_uart_clksrc *clk)
30{
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 ucon &= ~S3C2412_UCON_CLKMASK;
34
35 if (strcmp(clk->name, "uclk") == 0)
36 ucon |= S3C2440_UCON_UCLK;
37 else if (strcmp(clk->name, "pclk") == 0)
38 ucon |= S3C2440_UCON_PCLK;
39 else if (strcmp(clk->name, "usysclk") == 0)
40 ucon |= S3C2412_UCON_USYSCLK;
41 else {
42 printk(KERN_ERR "unknown clock source %s\n", clk->name);
43 return -EINVAL;
44 }
45
46 wr_regl(port, S3C2410_UCON, ucon);
47 return 0;
48}
49
50
51static int s3c2412_serial_getsource(struct uart_port *port,
52 struct s3c24xx_uart_clksrc *clk)
53{
54 unsigned long ucon = rd_regl(port, S3C2410_UCON);
55
56 switch (ucon & S3C2412_UCON_CLKMASK) {
57 case S3C2412_UCON_UCLK:
58 clk->divisor = 1;
59 clk->name = "uclk";
60 break;
61
62 case S3C2412_UCON_PCLK:
63 case S3C2412_UCON_PCLK2:
64 clk->divisor = 1;
65 clk->name = "pclk";
66 break;
67
68 case S3C2412_UCON_USYSCLK:
69 clk->divisor = 1;
70 clk->name = "usysclk";
71 break;
72 }
73
74 return 0;
75}
76
77static int s3c2412_serial_resetport(struct uart_port *port,
78 struct s3c2410_uartcfg *cfg)
79{
80 unsigned long ucon = rd_regl(port, S3C2410_UCON);
81
82 dbg("%s: port=%p (%08lx), cfg=%p\n",
83 __func__, port, port->mapbase, cfg);
84
85 /* ensure we don't change the clock settings... */
86
87 ucon &= S3C2412_UCON_CLKMASK;
88
89 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
90 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
91
92 /* reset both fifos */
93
94 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
96
97 return 0;
98}
99
100static struct s3c24xx_uart_info s3c2412_uart_inf = {
101 .name = "Samsung S3C2412 UART",
102 .type = PORT_S3C2412,
103 .fifosize = 64,
104 .has_divslot = 1,
105 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
106 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
107 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
108 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
109 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
110 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
111 .get_clksrc = s3c2412_serial_getsource,
112 .set_clksrc = s3c2412_serial_setsource,
113 .reset_port = s3c2412_serial_resetport,
114};
115
116/* device management */
117
118static int s3c2412_serial_probe(struct platform_device *dev)
119{
120 dbg("s3c2440_serial_probe: dev=%p\n", dev);
121 return s3c24xx_serial_probe(dev, &s3c2412_uart_inf);
122}
123
124static struct platform_driver s3c2412_serial_driver = {
125 .probe = s3c2412_serial_probe,
126 .remove = __devexit_p(s3c24xx_serial_remove),
127 .driver = {
128 .name = "s3c2412-uart",
129 .owner = THIS_MODULE,
130 },
131};
132
133s3c24xx_console_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
134
135static inline int s3c2412_serial_init(void)
136{
137 return s3c24xx_serial_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
138}
139
140static inline void s3c2412_serial_exit(void)
141{
142 platform_driver_unregister(&s3c2412_serial_driver);
143}
144
145module_init(s3c2412_serial_init);
146module_exit(s3c2412_serial_exit);
147
148MODULE_DESCRIPTION("Samsung S3C2412,S3C2413 SoC Serial port driver");
149MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
150MODULE_LICENSE("GPL v2");
151MODULE_ALIAS("platform:s3c2412-uart");
diff --git a/drivers/tty/serial/s3c2440.c b/drivers/tty/serial/s3c2440.c
new file mode 100644
index 000000000000..9e10d415d5fd
--- /dev/null
+++ b/drivers/tty/serial/s3c2440.c
@@ -0,0 +1,180 @@
1/*
2 * Driver for Samsung S3C2440 and S3C2442 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28
29static int s3c2440_serial_setsource(struct uart_port *port,
30 struct s3c24xx_uart_clksrc *clk)
31{
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 /* todo - proper fclk<>nonfclk switch. */
35
36 ucon &= ~S3C2440_UCON_CLKMASK;
37
38 if (strcmp(clk->name, "uclk") == 0)
39 ucon |= S3C2440_UCON_UCLK;
40 else if (strcmp(clk->name, "pclk") == 0)
41 ucon |= S3C2440_UCON_PCLK;
42 else if (strcmp(clk->name, "fclk") == 0)
43 ucon |= S3C2440_UCON_FCLK;
44 else {
45 printk(KERN_ERR "unknown clock source %s\n", clk->name);
46 return -EINVAL;
47 }
48
49 wr_regl(port, S3C2410_UCON, ucon);
50 return 0;
51}
52
53
54static int s3c2440_serial_getsource(struct uart_port *port,
55 struct s3c24xx_uart_clksrc *clk)
56{
57 unsigned long ucon = rd_regl(port, S3C2410_UCON);
58 unsigned long ucon0, ucon1, ucon2;
59
60 switch (ucon & S3C2440_UCON_CLKMASK) {
61 case S3C2440_UCON_UCLK:
62 clk->divisor = 1;
63 clk->name = "uclk";
64 break;
65
66 case S3C2440_UCON_PCLK:
67 case S3C2440_UCON_PCLK2:
68 clk->divisor = 1;
69 clk->name = "pclk";
70 break;
71
72 case S3C2440_UCON_FCLK:
73 /* the fun of calculating the uart divisors on
74 * the s3c2440 */
75
76 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
77 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
78 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
79
80 printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
81
82 ucon0 &= S3C2440_UCON0_DIVMASK;
83 ucon1 &= S3C2440_UCON1_DIVMASK;
84 ucon2 &= S3C2440_UCON2_DIVMASK;
85
86 if (ucon0 != 0) {
87 clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
88 clk->divisor += 6;
89 } else if (ucon1 != 0) {
90 clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
91 clk->divisor += 21;
92 } else if (ucon2 != 0) {
93 clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
94 clk->divisor += 36;
95 } else {
96 /* manual calims 44, seems to be 9 */
97 clk->divisor = 9;
98 }
99
100 clk->name = "fclk";
101 break;
102 }
103
104 return 0;
105}
106
107static int s3c2440_serial_resetport(struct uart_port *port,
108 struct s3c2410_uartcfg *cfg)
109{
110 unsigned long ucon = rd_regl(port, S3C2410_UCON);
111
112 dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
113 port, port->mapbase, cfg);
114
115 /* ensure we don't change the clock settings... */
116
117 ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
118
119 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
120 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
121
122 /* reset both fifos */
123
124 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
125 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
126
127 return 0;
128}
129
130static struct s3c24xx_uart_info s3c2440_uart_inf = {
131 .name = "Samsung S3C2440 UART",
132 .type = PORT_S3C2440,
133 .fifosize = 64,
134 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
135 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
136 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
137 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
138 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
139 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
140 .get_clksrc = s3c2440_serial_getsource,
141 .set_clksrc = s3c2440_serial_setsource,
142 .reset_port = s3c2440_serial_resetport,
143};
144
145/* device management */
146
147static int s3c2440_serial_probe(struct platform_device *dev)
148{
149 dbg("s3c2440_serial_probe: dev=%p\n", dev);
150 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
151}
152
153static struct platform_driver s3c2440_serial_driver = {
154 .probe = s3c2440_serial_probe,
155 .remove = __devexit_p(s3c24xx_serial_remove),
156 .driver = {
157 .name = "s3c2440-uart",
158 .owner = THIS_MODULE,
159 },
160};
161
162s3c24xx_console_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
163
164static int __init s3c2440_serial_init(void)
165{
166 return s3c24xx_serial_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
167}
168
169static void __exit s3c2440_serial_exit(void)
170{
171 platform_driver_unregister(&s3c2440_serial_driver);
172}
173
174module_init(s3c2440_serial_init);
175module_exit(s3c2440_serial_exit);
176
177MODULE_DESCRIPTION("Samsung S3C2440,S3C2442 SoC Serial port driver");
178MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
179MODULE_LICENSE("GPL v2");
180MODULE_ALIAS("platform:s3c2440-uart");
diff --git a/drivers/tty/serial/s3c24a0.c b/drivers/tty/serial/s3c24a0.c
new file mode 100644
index 000000000000..914eff22e499
--- /dev/null
+++ b/drivers/tty/serial/s3c24a0.c
@@ -0,0 +1,117 @@
1/*
2 * Driver for Samsung S3C24A0 SoC onboard UARTs.
3 *
4 * Based on drivers/serial/s3c2410.c
5 *
6 * Author: Sandeep Patil <sandeep.patil@azingo.com>
7 *
8 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
9 * http://armlinux.simtec.co.uk/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14*/
15
16#include <linux/module.h>
17#include <linux/ioport.h>
18#include <linux/platform_device.h>
19#include <linux/init.h>
20#include <linux/serial_core.h>
21#include <linux/serial.h>
22#include <linux/io.h>
23#include <linux/irq.h>
24
25#include <mach/hardware.h>
26
27#include <plat/regs-serial.h>
28#include <mach/regs-gpio.h>
29
30#include "samsung.h"
31
32static int s3c24a0_serial_setsource(struct uart_port *port,
33 struct s3c24xx_uart_clksrc *clk)
34{
35 unsigned long ucon = rd_regl(port, S3C2410_UCON);
36
37 if (strcmp(clk->name, "uclk") == 0)
38 ucon |= S3C2410_UCON_UCLK;
39 else
40 ucon &= ~S3C2410_UCON_UCLK;
41
42 wr_regl(port, S3C2410_UCON, ucon);
43 return 0;
44}
45
46static int s3c24a0_serial_getsource(struct uart_port *port,
47 struct s3c24xx_uart_clksrc *clk)
48{
49 unsigned long ucon = rd_regl(port, S3C2410_UCON);
50
51 clk->divisor = 1;
52 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
53
54 return 0;
55}
56
57static int s3c24a0_serial_resetport(struct uart_port *port,
58 struct s3c2410_uartcfg *cfg)
59{
60 dbg("s3c24a0_serial_resetport: port=%p (%08lx), cfg=%p\n",
61 port, port->mapbase, cfg);
62
63 wr_regl(port, S3C2410_UCON, cfg->ucon);
64 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
65
66 /* reset both fifos */
67
68 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
69 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
70
71 return 0;
72}
73
74static struct s3c24xx_uart_info s3c24a0_uart_inf = {
75 .name = "Samsung S3C24A0 UART",
76 .type = PORT_S3C2410,
77 .fifosize = 16,
78 .rx_fifomask = S3C24A0_UFSTAT_RXMASK,
79 .rx_fifoshift = S3C24A0_UFSTAT_RXSHIFT,
80 .rx_fifofull = S3C24A0_UFSTAT_RXFULL,
81 .tx_fifofull = S3C24A0_UFSTAT_TXFULL,
82 .tx_fifomask = S3C24A0_UFSTAT_TXMASK,
83 .tx_fifoshift = S3C24A0_UFSTAT_TXSHIFT,
84 .get_clksrc = s3c24a0_serial_getsource,
85 .set_clksrc = s3c24a0_serial_setsource,
86 .reset_port = s3c24a0_serial_resetport,
87};
88
89static int s3c24a0_serial_probe(struct platform_device *dev)
90{
91 return s3c24xx_serial_probe(dev, &s3c24a0_uart_inf);
92}
93
94static struct platform_driver s3c24a0_serial_driver = {
95 .probe = s3c24a0_serial_probe,
96 .remove = __devexit_p(s3c24xx_serial_remove),
97 .driver = {
98 .name = "s3c24a0-uart",
99 .owner = THIS_MODULE,
100 },
101};
102
103s3c24xx_console_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
104
105static int __init s3c24a0_serial_init(void)
106{
107 return s3c24xx_serial_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
108}
109
110static void __exit s3c24a0_serial_exit(void)
111{
112 platform_driver_unregister(&s3c24a0_serial_driver);
113}
114
115module_init(s3c24a0_serial_init);
116module_exit(s3c24a0_serial_exit);
117
diff --git a/drivers/tty/serial/s3c6400.c b/drivers/tty/serial/s3c6400.c
new file mode 100644
index 000000000000..ded26c42ff37
--- /dev/null
+++ b/drivers/tty/serial/s3c6400.c
@@ -0,0 +1,151 @@
1/*
2 * Driver for Samsung S3C6400 and S3C6410 SoC onboard UARTs.
3 *
4 * Copyright 2008 Openmoko, Inc.
5 * Copyright 2008 Simtec Electronics
6 * Ben Dooks <ben@simtec.co.uk>
7 * http://armlinux.simtec.co.uk/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
14#include <linux/module.h>
15#include <linux/ioport.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <linux/init.h>
19#include <linux/serial_core.h>
20#include <linux/serial.h>
21
22#include <asm/irq.h>
23#include <mach/hardware.h>
24
25#include <plat/regs-serial.h>
26
27#include "samsung.h"
28
29static int s3c6400_serial_setsource(struct uart_port *port,
30 struct s3c24xx_uart_clksrc *clk)
31{
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 if (strcmp(clk->name, "uclk0") == 0) {
35 ucon &= ~S3C6400_UCON_CLKMASK;
36 ucon |= S3C6400_UCON_UCLK0;
37 } else if (strcmp(clk->name, "uclk1") == 0)
38 ucon |= S3C6400_UCON_UCLK1;
39 else if (strcmp(clk->name, "pclk") == 0) {
40 /* See notes about transitioning from UCLK to PCLK */
41 ucon &= ~S3C6400_UCON_UCLK0;
42 } else {
43 printk(KERN_ERR "unknown clock source %s\n", clk->name);
44 return -EINVAL;
45 }
46
47 wr_regl(port, S3C2410_UCON, ucon);
48 return 0;
49}
50
51
52static int s3c6400_serial_getsource(struct uart_port *port,
53 struct s3c24xx_uart_clksrc *clk)
54{
55 u32 ucon = rd_regl(port, S3C2410_UCON);
56
57 clk->divisor = 1;
58
59 switch (ucon & S3C6400_UCON_CLKMASK) {
60 case S3C6400_UCON_UCLK0:
61 clk->name = "uclk0";
62 break;
63
64 case S3C6400_UCON_UCLK1:
65 clk->name = "uclk1";
66 break;
67
68 case S3C6400_UCON_PCLK:
69 case S3C6400_UCON_PCLK2:
70 clk->name = "pclk";
71 break;
72 }
73
74 return 0;
75}
76
77static int s3c6400_serial_resetport(struct uart_port *port,
78 struct s3c2410_uartcfg *cfg)
79{
80 unsigned long ucon = rd_regl(port, S3C2410_UCON);
81
82 dbg("s3c6400_serial_resetport: port=%p (%08lx), cfg=%p\n",
83 port, port->mapbase, cfg);
84
85 /* ensure we don't change the clock settings... */
86
87 ucon &= S3C6400_UCON_CLKMASK;
88
89 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
90 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
91
92 /* reset both fifos */
93
94 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
96
97 return 0;
98}
99
100static struct s3c24xx_uart_info s3c6400_uart_inf = {
101 .name = "Samsung S3C6400 UART",
102 .type = PORT_S3C6400,
103 .fifosize = 64,
104 .has_divslot = 1,
105 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
106 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
107 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
108 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
109 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
110 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
111 .get_clksrc = s3c6400_serial_getsource,
112 .set_clksrc = s3c6400_serial_setsource,
113 .reset_port = s3c6400_serial_resetport,
114};
115
116/* device management */
117
118static int s3c6400_serial_probe(struct platform_device *dev)
119{
120 dbg("s3c6400_serial_probe: dev=%p\n", dev);
121 return s3c24xx_serial_probe(dev, &s3c6400_uart_inf);
122}
123
124static struct platform_driver s3c6400_serial_driver = {
125 .probe = s3c6400_serial_probe,
126 .remove = __devexit_p(s3c24xx_serial_remove),
127 .driver = {
128 .name = "s3c6400-uart",
129 .owner = THIS_MODULE,
130 },
131};
132
133s3c24xx_console_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
134
135static int __init s3c6400_serial_init(void)
136{
137 return s3c24xx_serial_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
138}
139
140static void __exit s3c6400_serial_exit(void)
141{
142 platform_driver_unregister(&s3c6400_serial_driver);
143}
144
145module_init(s3c6400_serial_init);
146module_exit(s3c6400_serial_exit);
147
148MODULE_DESCRIPTION("Samsung S3C6400,S3C6410 SoC Serial port driver");
149MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
150MODULE_LICENSE("GPL v2");
151MODULE_ALIAS("platform:s3c6400-uart");
diff --git a/drivers/tty/serial/s5pv210.c b/drivers/tty/serial/s5pv210.c
new file mode 100644
index 000000000000..dd194dc80ee9
--- /dev/null
+++ b/drivers/tty/serial/s5pv210.c
@@ -0,0 +1,161 @@
1/*
2 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com/
4 *
5 * Based on drivers/serial/s3c6400.c
6 *
7 * Driver for Samsung S5PV210 SoC UARTs.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
14#include <linux/module.h>
15#include <linux/ioport.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <linux/init.h>
19#include <linux/serial_core.h>
20#include <linux/serial.h>
21
22#include <asm/irq.h>
23#include <mach/hardware.h>
24#include <plat/regs-serial.h>
25#include "samsung.h"
26
27static int s5pv210_serial_setsource(struct uart_port *port,
28 struct s3c24xx_uart_clksrc *clk)
29{
30 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 if (cfg->flags & NO_NEED_CHECK_CLKSRC)
34 return 0;
35
36 if (strcmp(clk->name, "pclk") == 0)
37 ucon &= ~S5PV210_UCON_CLKMASK;
38 else if (strcmp(clk->name, "uclk1") == 0)
39 ucon |= S5PV210_UCON_CLKMASK;
40 else {
41 printk(KERN_ERR "unknown clock source %s\n", clk->name);
42 return -EINVAL;
43 }
44
45 wr_regl(port, S3C2410_UCON, ucon);
46 return 0;
47}
48
49
50static int s5pv210_serial_getsource(struct uart_port *port,
51 struct s3c24xx_uart_clksrc *clk)
52{
53 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
54 u32 ucon = rd_regl(port, S3C2410_UCON);
55
56 clk->divisor = 1;
57
58 if (cfg->flags & NO_NEED_CHECK_CLKSRC)
59 return 0;
60
61 switch (ucon & S5PV210_UCON_CLKMASK) {
62 case S5PV210_UCON_PCLK:
63 clk->name = "pclk";
64 break;
65 case S5PV210_UCON_UCLK:
66 clk->name = "uclk1";
67 break;
68 }
69
70 return 0;
71}
72
73static int s5pv210_serial_resetport(struct uart_port *port,
74 struct s3c2410_uartcfg *cfg)
75{
76 unsigned long ucon = rd_regl(port, S3C2410_UCON);
77
78 ucon &= S5PV210_UCON_CLKMASK;
79 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
80 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
81
82 /* reset both fifos */
83 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
84 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
85
86 return 0;
87}
88
89#define S5PV210_UART_DEFAULT_INFO(fifo_size) \
90 .name = "Samsung S5PV210 UART0", \
91 .type = PORT_S3C6400, \
92 .fifosize = fifo_size, \
93 .has_divslot = 1, \
94 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
95 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
96 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
97 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
98 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
99 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
100 .get_clksrc = s5pv210_serial_getsource, \
101 .set_clksrc = s5pv210_serial_setsource, \
102 .reset_port = s5pv210_serial_resetport
103
104static struct s3c24xx_uart_info s5p_port_fifo256 = {
105 S5PV210_UART_DEFAULT_INFO(256),
106};
107
108static struct s3c24xx_uart_info s5p_port_fifo64 = {
109 S5PV210_UART_DEFAULT_INFO(64),
110};
111
112static struct s3c24xx_uart_info s5p_port_fifo16 = {
113 S5PV210_UART_DEFAULT_INFO(16),
114};
115
116static struct s3c24xx_uart_info *s5p_uart_inf[] = {
117 [0] = &s5p_port_fifo256,
118 [1] = &s5p_port_fifo64,
119 [2] = &s5p_port_fifo16,
120 [3] = &s5p_port_fifo16,
121};
122
123/* device management */
124static int s5p_serial_probe(struct platform_device *pdev)
125{
126 return s3c24xx_serial_probe(pdev, s5p_uart_inf[pdev->id]);
127}
128
129static struct platform_driver s5p_serial_driver = {
130 .probe = s5p_serial_probe,
131 .remove = __devexit_p(s3c24xx_serial_remove),
132 .driver = {
133 .name = "s5pv210-uart",
134 .owner = THIS_MODULE,
135 },
136};
137
138static int __init s5pv210_serial_console_init(void)
139{
140 return s3c24xx_serial_initconsole(&s5p_serial_driver, s5p_uart_inf);
141}
142
143console_initcall(s5pv210_serial_console_init);
144
145static int __init s5p_serial_init(void)
146{
147 return s3c24xx_serial_init(&s5p_serial_driver, *s5p_uart_inf);
148}
149
150static void __exit s5p_serial_exit(void)
151{
152 platform_driver_unregister(&s5p_serial_driver);
153}
154
155module_init(s5p_serial_init);
156module_exit(s5p_serial_exit);
157
158MODULE_LICENSE("GPL");
159MODULE_ALIAS("platform:s5pv210-uart");
160MODULE_DESCRIPTION("Samsung S5PV210 UART Driver support");
161MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
diff --git a/drivers/tty/serial/sa1100.c b/drivers/tty/serial/sa1100.c
new file mode 100644
index 000000000000..ef7a21a6a01b
--- /dev/null
+++ b/drivers/tty/serial/sa1100.c
@@ -0,0 +1,916 @@
1/*
2 * Driver for SA11x0 serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2000 Deep Blue Solutions Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#if defined(CONFIG_SERIAL_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/module.h>
28#include <linux/ioport.h>
29#include <linux/init.h>
30#include <linux/console.h>
31#include <linux/sysrq.h>
32#include <linux/platform_device.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/io.h>
38
39#include <asm/irq.h>
40#include <mach/hardware.h>
41#include <asm/mach/serial_sa1100.h>
42
43/* We've been assigned a range on the "Low-density serial ports" major */
44#define SERIAL_SA1100_MAJOR 204
45#define MINOR_START 5
46
47#define NR_PORTS 3
48
49#define SA1100_ISR_PASS_LIMIT 256
50
51/*
52 * Convert from ignore_status_mask or read_status_mask to UTSR[01]
53 */
54#define SM_TO_UTSR0(x) ((x) & 0xff)
55#define SM_TO_UTSR1(x) ((x) >> 8)
56#define UTSR0_TO_SM(x) ((x))
57#define UTSR1_TO_SM(x) ((x) << 8)
58
59#define UART_GET_UTCR0(sport) __raw_readl((sport)->port.membase + UTCR0)
60#define UART_GET_UTCR1(sport) __raw_readl((sport)->port.membase + UTCR1)
61#define UART_GET_UTCR2(sport) __raw_readl((sport)->port.membase + UTCR2)
62#define UART_GET_UTCR3(sport) __raw_readl((sport)->port.membase + UTCR3)
63#define UART_GET_UTSR0(sport) __raw_readl((sport)->port.membase + UTSR0)
64#define UART_GET_UTSR1(sport) __raw_readl((sport)->port.membase + UTSR1)
65#define UART_GET_CHAR(sport) __raw_readl((sport)->port.membase + UTDR)
66
67#define UART_PUT_UTCR0(sport,v) __raw_writel((v),(sport)->port.membase + UTCR0)
68#define UART_PUT_UTCR1(sport,v) __raw_writel((v),(sport)->port.membase + UTCR1)
69#define UART_PUT_UTCR2(sport,v) __raw_writel((v),(sport)->port.membase + UTCR2)
70#define UART_PUT_UTCR3(sport,v) __raw_writel((v),(sport)->port.membase + UTCR3)
71#define UART_PUT_UTSR0(sport,v) __raw_writel((v),(sport)->port.membase + UTSR0)
72#define UART_PUT_UTSR1(sport,v) __raw_writel((v),(sport)->port.membase + UTSR1)
73#define UART_PUT_CHAR(sport,v) __raw_writel((v),(sport)->port.membase + UTDR)
74
75/*
76 * This is the size of our serial port register set.
77 */
78#define UART_PORT_SIZE 0x24
79
80/*
81 * This determines how often we check the modem status signals
82 * for any change. They generally aren't connected to an IRQ
83 * so we have to poll them. We also check immediately before
84 * filling the TX fifo incase CTS has been dropped.
85 */
86#define MCTRL_TIMEOUT (250*HZ/1000)
87
88struct sa1100_port {
89 struct uart_port port;
90 struct timer_list timer;
91 unsigned int old_status;
92};
93
94/*
95 * Handle any change of modem status signal since we were last called.
96 */
97static void sa1100_mctrl_check(struct sa1100_port *sport)
98{
99 unsigned int status, changed;
100
101 status = sport->port.ops->get_mctrl(&sport->port);
102 changed = status ^ sport->old_status;
103
104 if (changed == 0)
105 return;
106
107 sport->old_status = status;
108
109 if (changed & TIOCM_RI)
110 sport->port.icount.rng++;
111 if (changed & TIOCM_DSR)
112 sport->port.icount.dsr++;
113 if (changed & TIOCM_CAR)
114 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
115 if (changed & TIOCM_CTS)
116 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
117
118 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
119}
120
121/*
122 * This is our per-port timeout handler, for checking the
123 * modem status signals.
124 */
125static void sa1100_timeout(unsigned long data)
126{
127 struct sa1100_port *sport = (struct sa1100_port *)data;
128 unsigned long flags;
129
130 if (sport->port.state) {
131 spin_lock_irqsave(&sport->port.lock, flags);
132 sa1100_mctrl_check(sport);
133 spin_unlock_irqrestore(&sport->port.lock, flags);
134
135 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
136 }
137}
138
139/*
140 * interrupts disabled on entry
141 */
142static void sa1100_stop_tx(struct uart_port *port)
143{
144 struct sa1100_port *sport = (struct sa1100_port *)port;
145 u32 utcr3;
146
147 utcr3 = UART_GET_UTCR3(sport);
148 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);
149 sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);
150}
151
152/*
153 * port locked and interrupts disabled
154 */
155static void sa1100_start_tx(struct uart_port *port)
156{
157 struct sa1100_port *sport = (struct sa1100_port *)port;
158 u32 utcr3;
159
160 utcr3 = UART_GET_UTCR3(sport);
161 sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);
162 UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);
163}
164
165/*
166 * Interrupts enabled
167 */
168static void sa1100_stop_rx(struct uart_port *port)
169{
170 struct sa1100_port *sport = (struct sa1100_port *)port;
171 u32 utcr3;
172
173 utcr3 = UART_GET_UTCR3(sport);
174 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);
175}
176
177/*
178 * Set the modem control timer to fire immediately.
179 */
180static void sa1100_enable_ms(struct uart_port *port)
181{
182 struct sa1100_port *sport = (struct sa1100_port *)port;
183
184 mod_timer(&sport->timer, jiffies);
185}
186
187static void
188sa1100_rx_chars(struct sa1100_port *sport)
189{
190 struct tty_struct *tty = sport->port.state->port.tty;
191 unsigned int status, ch, flg;
192
193 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
194 UTSR0_TO_SM(UART_GET_UTSR0(sport));
195 while (status & UTSR1_TO_SM(UTSR1_RNE)) {
196 ch = UART_GET_CHAR(sport);
197
198 sport->port.icount.rx++;
199
200 flg = TTY_NORMAL;
201
202 /*
203 * note that the error handling code is
204 * out of the main execution path
205 */
206 if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR)) {
207 if (status & UTSR1_TO_SM(UTSR1_PRE))
208 sport->port.icount.parity++;
209 else if (status & UTSR1_TO_SM(UTSR1_FRE))
210 sport->port.icount.frame++;
211 if (status & UTSR1_TO_SM(UTSR1_ROR))
212 sport->port.icount.overrun++;
213
214 status &= sport->port.read_status_mask;
215
216 if (status & UTSR1_TO_SM(UTSR1_PRE))
217 flg = TTY_PARITY;
218 else if (status & UTSR1_TO_SM(UTSR1_FRE))
219 flg = TTY_FRAME;
220
221#ifdef SUPPORT_SYSRQ
222 sport->port.sysrq = 0;
223#endif
224 }
225
226 if (uart_handle_sysrq_char(&sport->port, ch))
227 goto ignore_char;
228
229 uart_insert_char(&sport->port, status, UTSR1_TO_SM(UTSR1_ROR), ch, flg);
230
231 ignore_char:
232 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
233 UTSR0_TO_SM(UART_GET_UTSR0(sport));
234 }
235 tty_flip_buffer_push(tty);
236}
237
238static void sa1100_tx_chars(struct sa1100_port *sport)
239{
240 struct circ_buf *xmit = &sport->port.state->xmit;
241
242 if (sport->port.x_char) {
243 UART_PUT_CHAR(sport, sport->port.x_char);
244 sport->port.icount.tx++;
245 sport->port.x_char = 0;
246 return;
247 }
248
249 /*
250 * Check the modem control lines before
251 * transmitting anything.
252 */
253 sa1100_mctrl_check(sport);
254
255 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
256 sa1100_stop_tx(&sport->port);
257 return;
258 }
259
260 /*
261 * Tried using FIFO (not checking TNF) for fifo fill:
262 * still had the '4 bytes repeated' problem.
263 */
264 while (UART_GET_UTSR1(sport) & UTSR1_TNF) {
265 UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);
266 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
267 sport->port.icount.tx++;
268 if (uart_circ_empty(xmit))
269 break;
270 }
271
272 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
273 uart_write_wakeup(&sport->port);
274
275 if (uart_circ_empty(xmit))
276 sa1100_stop_tx(&sport->port);
277}
278
279static irqreturn_t sa1100_int(int irq, void *dev_id)
280{
281 struct sa1100_port *sport = dev_id;
282 unsigned int status, pass_counter = 0;
283
284 spin_lock(&sport->port.lock);
285 status = UART_GET_UTSR0(sport);
286 status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;
287 do {
288 if (status & (UTSR0_RFS | UTSR0_RID)) {
289 /* Clear the receiver idle bit, if set */
290 if (status & UTSR0_RID)
291 UART_PUT_UTSR0(sport, UTSR0_RID);
292 sa1100_rx_chars(sport);
293 }
294
295 /* Clear the relevant break bits */
296 if (status & (UTSR0_RBB | UTSR0_REB))
297 UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));
298
299 if (status & UTSR0_RBB)
300 sport->port.icount.brk++;
301
302 if (status & UTSR0_REB)
303 uart_handle_break(&sport->port);
304
305 if (status & UTSR0_TFS)
306 sa1100_tx_chars(sport);
307 if (pass_counter++ > SA1100_ISR_PASS_LIMIT)
308 break;
309 status = UART_GET_UTSR0(sport);
310 status &= SM_TO_UTSR0(sport->port.read_status_mask) |
311 ~UTSR0_TFS;
312 } while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));
313 spin_unlock(&sport->port.lock);
314
315 return IRQ_HANDLED;
316}
317
318/*
319 * Return TIOCSER_TEMT when transmitter is not busy.
320 */
321static unsigned int sa1100_tx_empty(struct uart_port *port)
322{
323 struct sa1100_port *sport = (struct sa1100_port *)port;
324
325 return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;
326}
327
328static unsigned int sa1100_get_mctrl(struct uart_port *port)
329{
330 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
331}
332
333static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl)
334{
335}
336
337/*
338 * Interrupts always disabled.
339 */
340static void sa1100_break_ctl(struct uart_port *port, int break_state)
341{
342 struct sa1100_port *sport = (struct sa1100_port *)port;
343 unsigned long flags;
344 unsigned int utcr3;
345
346 spin_lock_irqsave(&sport->port.lock, flags);
347 utcr3 = UART_GET_UTCR3(sport);
348 if (break_state == -1)
349 utcr3 |= UTCR3_BRK;
350 else
351 utcr3 &= ~UTCR3_BRK;
352 UART_PUT_UTCR3(sport, utcr3);
353 spin_unlock_irqrestore(&sport->port.lock, flags);
354}
355
356static int sa1100_startup(struct uart_port *port)
357{
358 struct sa1100_port *sport = (struct sa1100_port *)port;
359 int retval;
360
361 /*
362 * Allocate the IRQ
363 */
364 retval = request_irq(sport->port.irq, sa1100_int, 0,
365 "sa11x0-uart", sport);
366 if (retval)
367 return retval;
368
369 /*
370 * Finally, clear and enable interrupts
371 */
372 UART_PUT_UTSR0(sport, -1);
373 UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);
374
375 /*
376 * Enable modem status interrupts
377 */
378 spin_lock_irq(&sport->port.lock);
379 sa1100_enable_ms(&sport->port);
380 spin_unlock_irq(&sport->port.lock);
381
382 return 0;
383}
384
385static void sa1100_shutdown(struct uart_port *port)
386{
387 struct sa1100_port *sport = (struct sa1100_port *)port;
388
389 /*
390 * Stop our timer.
391 */
392 del_timer_sync(&sport->timer);
393
394 /*
395 * Free the interrupt
396 */
397 free_irq(sport->port.irq, sport);
398
399 /*
400 * Disable all interrupts, port and break condition.
401 */
402 UART_PUT_UTCR3(sport, 0);
403}
404
405static void
406sa1100_set_termios(struct uart_port *port, struct ktermios *termios,
407 struct ktermios *old)
408{
409 struct sa1100_port *sport = (struct sa1100_port *)port;
410 unsigned long flags;
411 unsigned int utcr0, old_utcr3, baud, quot;
412 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
413
414 /*
415 * We only support CS7 and CS8.
416 */
417 while ((termios->c_cflag & CSIZE) != CS7 &&
418 (termios->c_cflag & CSIZE) != CS8) {
419 termios->c_cflag &= ~CSIZE;
420 termios->c_cflag |= old_csize;
421 old_csize = CS8;
422 }
423
424 if ((termios->c_cflag & CSIZE) == CS8)
425 utcr0 = UTCR0_DSS;
426 else
427 utcr0 = 0;
428
429 if (termios->c_cflag & CSTOPB)
430 utcr0 |= UTCR0_SBS;
431 if (termios->c_cflag & PARENB) {
432 utcr0 |= UTCR0_PE;
433 if (!(termios->c_cflag & PARODD))
434 utcr0 |= UTCR0_OES;
435 }
436
437 /*
438 * Ask the core to calculate the divisor for us.
439 */
440 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
441 quot = uart_get_divisor(port, baud);
442
443 spin_lock_irqsave(&sport->port.lock, flags);
444
445 sport->port.read_status_mask &= UTSR0_TO_SM(UTSR0_TFS);
446 sport->port.read_status_mask |= UTSR1_TO_SM(UTSR1_ROR);
447 if (termios->c_iflag & INPCK)
448 sport->port.read_status_mask |=
449 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
450 if (termios->c_iflag & (BRKINT | PARMRK))
451 sport->port.read_status_mask |=
452 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
453
454 /*
455 * Characters to ignore
456 */
457 sport->port.ignore_status_mask = 0;
458 if (termios->c_iflag & IGNPAR)
459 sport->port.ignore_status_mask |=
460 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
461 if (termios->c_iflag & IGNBRK) {
462 sport->port.ignore_status_mask |=
463 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
464 /*
465 * If we're ignoring parity and break indicators,
466 * ignore overruns too (for real raw support).
467 */
468 if (termios->c_iflag & IGNPAR)
469 sport->port.ignore_status_mask |=
470 UTSR1_TO_SM(UTSR1_ROR);
471 }
472
473 del_timer_sync(&sport->timer);
474
475 /*
476 * Update the per-port timeout.
477 */
478 uart_update_timeout(port, termios->c_cflag, baud);
479
480 /*
481 * disable interrupts and drain transmitter
482 */
483 old_utcr3 = UART_GET_UTCR3(sport);
484 UART_PUT_UTCR3(sport, old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE));
485
486 while (UART_GET_UTSR1(sport) & UTSR1_TBY)
487 barrier();
488
489 /* then, disable everything */
490 UART_PUT_UTCR3(sport, 0);
491
492 /* set the parity, stop bits and data size */
493 UART_PUT_UTCR0(sport, utcr0);
494
495 /* set the baud rate */
496 quot -= 1;
497 UART_PUT_UTCR1(sport, ((quot & 0xf00) >> 8));
498 UART_PUT_UTCR2(sport, (quot & 0xff));
499
500 UART_PUT_UTSR0(sport, -1);
501
502 UART_PUT_UTCR3(sport, old_utcr3);
503
504 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
505 sa1100_enable_ms(&sport->port);
506
507 spin_unlock_irqrestore(&sport->port.lock, flags);
508}
509
510static const char *sa1100_type(struct uart_port *port)
511{
512 struct sa1100_port *sport = (struct sa1100_port *)port;
513
514 return sport->port.type == PORT_SA1100 ? "SA1100" : NULL;
515}
516
517/*
518 * Release the memory region(s) being used by 'port'.
519 */
520static void sa1100_release_port(struct uart_port *port)
521{
522 struct sa1100_port *sport = (struct sa1100_port *)port;
523
524 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
525}
526
527/*
528 * Request the memory region(s) being used by 'port'.
529 */
530static int sa1100_request_port(struct uart_port *port)
531{
532 struct sa1100_port *sport = (struct sa1100_port *)port;
533
534 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
535 "sa11x0-uart") != NULL ? 0 : -EBUSY;
536}
537
538/*
539 * Configure/autoconfigure the port.
540 */
541static void sa1100_config_port(struct uart_port *port, int flags)
542{
543 struct sa1100_port *sport = (struct sa1100_port *)port;
544
545 if (flags & UART_CONFIG_TYPE &&
546 sa1100_request_port(&sport->port) == 0)
547 sport->port.type = PORT_SA1100;
548}
549
550/*
551 * Verify the new serial_struct (for TIOCSSERIAL).
552 * The only change we allow are to the flags and type, and
553 * even then only between PORT_SA1100 and PORT_UNKNOWN
554 */
555static int
556sa1100_verify_port(struct uart_port *port, struct serial_struct *ser)
557{
558 struct sa1100_port *sport = (struct sa1100_port *)port;
559 int ret = 0;
560
561 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SA1100)
562 ret = -EINVAL;
563 if (sport->port.irq != ser->irq)
564 ret = -EINVAL;
565 if (ser->io_type != SERIAL_IO_MEM)
566 ret = -EINVAL;
567 if (sport->port.uartclk / 16 != ser->baud_base)
568 ret = -EINVAL;
569 if ((void *)sport->port.mapbase != ser->iomem_base)
570 ret = -EINVAL;
571 if (sport->port.iobase != ser->port)
572 ret = -EINVAL;
573 if (ser->hub6 != 0)
574 ret = -EINVAL;
575 return ret;
576}
577
578static struct uart_ops sa1100_pops = {
579 .tx_empty = sa1100_tx_empty,
580 .set_mctrl = sa1100_set_mctrl,
581 .get_mctrl = sa1100_get_mctrl,
582 .stop_tx = sa1100_stop_tx,
583 .start_tx = sa1100_start_tx,
584 .stop_rx = sa1100_stop_rx,
585 .enable_ms = sa1100_enable_ms,
586 .break_ctl = sa1100_break_ctl,
587 .startup = sa1100_startup,
588 .shutdown = sa1100_shutdown,
589 .set_termios = sa1100_set_termios,
590 .type = sa1100_type,
591 .release_port = sa1100_release_port,
592 .request_port = sa1100_request_port,
593 .config_port = sa1100_config_port,
594 .verify_port = sa1100_verify_port,
595};
596
597static struct sa1100_port sa1100_ports[NR_PORTS];
598
599/*
600 * Setup the SA1100 serial ports. Note that we don't include the IrDA
601 * port here since we have our own SIR/FIR driver (see drivers/net/irda)
602 *
603 * Note also that we support "console=ttySAx" where "x" is either 0 or 1.
604 * Which serial port this ends up being depends on the machine you're
605 * running this kernel on. I'm not convinced that this is a good idea,
606 * but that's the way it traditionally works.
607 *
608 * Note that NanoEngine UART3 becomes UART2, and UART2 is no longer
609 * used here.
610 */
611static void __init sa1100_init_ports(void)
612{
613 static int first = 1;
614 int i;
615
616 if (!first)
617 return;
618 first = 0;
619
620 for (i = 0; i < NR_PORTS; i++) {
621 sa1100_ports[i].port.uartclk = 3686400;
622 sa1100_ports[i].port.ops = &sa1100_pops;
623 sa1100_ports[i].port.fifosize = 8;
624 sa1100_ports[i].port.line = i;
625 sa1100_ports[i].port.iotype = UPIO_MEM;
626 init_timer(&sa1100_ports[i].timer);
627 sa1100_ports[i].timer.function = sa1100_timeout;
628 sa1100_ports[i].timer.data = (unsigned long)&sa1100_ports[i];
629 }
630
631 /*
632 * make transmit lines outputs, so that when the port
633 * is closed, the output is in the MARK state.
634 */
635 PPDR |= PPC_TXD1 | PPC_TXD3;
636 PPSR |= PPC_TXD1 | PPC_TXD3;
637}
638
639void __devinit sa1100_register_uart_fns(struct sa1100_port_fns *fns)
640{
641 if (fns->get_mctrl)
642 sa1100_pops.get_mctrl = fns->get_mctrl;
643 if (fns->set_mctrl)
644 sa1100_pops.set_mctrl = fns->set_mctrl;
645
646 sa1100_pops.pm = fns->pm;
647 sa1100_pops.set_wake = fns->set_wake;
648}
649
650void __init sa1100_register_uart(int idx, int port)
651{
652 if (idx >= NR_PORTS) {
653 printk(KERN_ERR "%s: bad index number %d\n", __func__, idx);
654 return;
655 }
656
657 switch (port) {
658 case 1:
659 sa1100_ports[idx].port.membase = (void __iomem *)&Ser1UTCR0;
660 sa1100_ports[idx].port.mapbase = _Ser1UTCR0;
661 sa1100_ports[idx].port.irq = IRQ_Ser1UART;
662 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
663 break;
664
665 case 2:
666 sa1100_ports[idx].port.membase = (void __iomem *)&Ser2UTCR0;
667 sa1100_ports[idx].port.mapbase = _Ser2UTCR0;
668 sa1100_ports[idx].port.irq = IRQ_Ser2ICP;
669 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
670 break;
671
672 case 3:
673 sa1100_ports[idx].port.membase = (void __iomem *)&Ser3UTCR0;
674 sa1100_ports[idx].port.mapbase = _Ser3UTCR0;
675 sa1100_ports[idx].port.irq = IRQ_Ser3UART;
676 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
677 break;
678
679 default:
680 printk(KERN_ERR "%s: bad port number %d\n", __func__, port);
681 }
682}
683
684
685#ifdef CONFIG_SERIAL_SA1100_CONSOLE
686static void sa1100_console_putchar(struct uart_port *port, int ch)
687{
688 struct sa1100_port *sport = (struct sa1100_port *)port;
689
690 while (!(UART_GET_UTSR1(sport) & UTSR1_TNF))
691 barrier();
692 UART_PUT_CHAR(sport, ch);
693}
694
695/*
696 * Interrupts are disabled on entering
697 */
698static void
699sa1100_console_write(struct console *co, const char *s, unsigned int count)
700{
701 struct sa1100_port *sport = &sa1100_ports[co->index];
702 unsigned int old_utcr3, status;
703
704 /*
705 * First, save UTCR3 and then disable interrupts
706 */
707 old_utcr3 = UART_GET_UTCR3(sport);
708 UART_PUT_UTCR3(sport, (old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE)) |
709 UTCR3_TXE);
710
711 uart_console_write(&sport->port, s, count, sa1100_console_putchar);
712
713 /*
714 * Finally, wait for transmitter to become empty
715 * and restore UTCR3
716 */
717 do {
718 status = UART_GET_UTSR1(sport);
719 } while (status & UTSR1_TBY);
720 UART_PUT_UTCR3(sport, old_utcr3);
721}
722
723/*
724 * If the port was already initialised (eg, by a boot loader),
725 * try to determine the current setup.
726 */
727static void __init
728sa1100_console_get_options(struct sa1100_port *sport, int *baud,
729 int *parity, int *bits)
730{
731 unsigned int utcr3;
732
733 utcr3 = UART_GET_UTCR3(sport) & (UTCR3_RXE | UTCR3_TXE);
734 if (utcr3 == (UTCR3_RXE | UTCR3_TXE)) {
735 /* ok, the port was enabled */
736 unsigned int utcr0, quot;
737
738 utcr0 = UART_GET_UTCR0(sport);
739
740 *parity = 'n';
741 if (utcr0 & UTCR0_PE) {
742 if (utcr0 & UTCR0_OES)
743 *parity = 'e';
744 else
745 *parity = 'o';
746 }
747
748 if (utcr0 & UTCR0_DSS)
749 *bits = 8;
750 else
751 *bits = 7;
752
753 quot = UART_GET_UTCR2(sport) | UART_GET_UTCR1(sport) << 8;
754 quot &= 0xfff;
755 *baud = sport->port.uartclk / (16 * (quot + 1));
756 }
757}
758
759static int __init
760sa1100_console_setup(struct console *co, char *options)
761{
762 struct sa1100_port *sport;
763 int baud = 9600;
764 int bits = 8;
765 int parity = 'n';
766 int flow = 'n';
767
768 /*
769 * Check whether an invalid uart number has been specified, and
770 * if so, search for the first available port that does have
771 * console support.
772 */
773 if (co->index == -1 || co->index >= NR_PORTS)
774 co->index = 0;
775 sport = &sa1100_ports[co->index];
776
777 if (options)
778 uart_parse_options(options, &baud, &parity, &bits, &flow);
779 else
780 sa1100_console_get_options(sport, &baud, &parity, &bits);
781
782 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
783}
784
785static struct uart_driver sa1100_reg;
786static struct console sa1100_console = {
787 .name = "ttySA",
788 .write = sa1100_console_write,
789 .device = uart_console_device,
790 .setup = sa1100_console_setup,
791 .flags = CON_PRINTBUFFER,
792 .index = -1,
793 .data = &sa1100_reg,
794};
795
796static int __init sa1100_rs_console_init(void)
797{
798 sa1100_init_ports();
799 register_console(&sa1100_console);
800 return 0;
801}
802console_initcall(sa1100_rs_console_init);
803
804#define SA1100_CONSOLE &sa1100_console
805#else
806#define SA1100_CONSOLE NULL
807#endif
808
809static struct uart_driver sa1100_reg = {
810 .owner = THIS_MODULE,
811 .driver_name = "ttySA",
812 .dev_name = "ttySA",
813 .major = SERIAL_SA1100_MAJOR,
814 .minor = MINOR_START,
815 .nr = NR_PORTS,
816 .cons = SA1100_CONSOLE,
817};
818
819static int sa1100_serial_suspend(struct platform_device *dev, pm_message_t state)
820{
821 struct sa1100_port *sport = platform_get_drvdata(dev);
822
823 if (sport)
824 uart_suspend_port(&sa1100_reg, &sport->port);
825
826 return 0;
827}
828
829static int sa1100_serial_resume(struct platform_device *dev)
830{
831 struct sa1100_port *sport = platform_get_drvdata(dev);
832
833 if (sport)
834 uart_resume_port(&sa1100_reg, &sport->port);
835
836 return 0;
837}
838
839static int sa1100_serial_probe(struct platform_device *dev)
840{
841 struct resource *res = dev->resource;
842 int i;
843
844 for (i = 0; i < dev->num_resources; i++, res++)
845 if (res->flags & IORESOURCE_MEM)
846 break;
847
848 if (i < dev->num_resources) {
849 for (i = 0; i < NR_PORTS; i++) {
850 if (sa1100_ports[i].port.mapbase != res->start)
851 continue;
852
853 sa1100_ports[i].port.dev = &dev->dev;
854 uart_add_one_port(&sa1100_reg, &sa1100_ports[i].port);
855 platform_set_drvdata(dev, &sa1100_ports[i]);
856 break;
857 }
858 }
859
860 return 0;
861}
862
863static int sa1100_serial_remove(struct platform_device *pdev)
864{
865 struct sa1100_port *sport = platform_get_drvdata(pdev);
866
867 platform_set_drvdata(pdev, NULL);
868
869 if (sport)
870 uart_remove_one_port(&sa1100_reg, &sport->port);
871
872 return 0;
873}
874
875static struct platform_driver sa11x0_serial_driver = {
876 .probe = sa1100_serial_probe,
877 .remove = sa1100_serial_remove,
878 .suspend = sa1100_serial_suspend,
879 .resume = sa1100_serial_resume,
880 .driver = {
881 .name = "sa11x0-uart",
882 .owner = THIS_MODULE,
883 },
884};
885
886static int __init sa1100_serial_init(void)
887{
888 int ret;
889
890 printk(KERN_INFO "Serial: SA11x0 driver\n");
891
892 sa1100_init_ports();
893
894 ret = uart_register_driver(&sa1100_reg);
895 if (ret == 0) {
896 ret = platform_driver_register(&sa11x0_serial_driver);
897 if (ret)
898 uart_unregister_driver(&sa1100_reg);
899 }
900 return ret;
901}
902
903static void __exit sa1100_serial_exit(void)
904{
905 platform_driver_unregister(&sa11x0_serial_driver);
906 uart_unregister_driver(&sa1100_reg);
907}
908
909module_init(sa1100_serial_init);
910module_exit(sa1100_serial_exit);
911
912MODULE_AUTHOR("Deep Blue Solutions Ltd");
913MODULE_DESCRIPTION("SA1100 generic serial port driver");
914MODULE_LICENSE("GPL");
915MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_SA1100_MAJOR);
916MODULE_ALIAS("platform:sa11x0-uart");
diff --git a/drivers/tty/serial/samsung.c b/drivers/tty/serial/samsung.c
new file mode 100644
index 000000000000..f66f64829303
--- /dev/null
+++ b/drivers/tty/serial/samsung.c
@@ -0,0 +1,1486 @@
1/*
2 * Driver core for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12/* Hote on 2410 error handling
13 *
14 * The s3c2410 manual has a love/hate affair with the contents of the
15 * UERSTAT register in the UART blocks, and keeps marking some of the
16 * error bits as reserved. Having checked with the s3c2410x01,
17 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
18 * feature from the latter versions of the manual.
19 *
20 * If it becomes aparrent that latter versions of the 2410 remove these
21 * bits, then action will have to be taken to differentiate the versions
22 * and change the policy on BREAK
23 *
24 * BJD, 04-Nov-2004
25*/
26
27#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/module.h>
32#include <linux/ioport.h>
33#include <linux/io.h>
34#include <linux/platform_device.h>
35#include <linux/init.h>
36#include <linux/sysrq.h>
37#include <linux/console.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40#include <linux/serial_core.h>
41#include <linux/serial.h>
42#include <linux/delay.h>
43#include <linux/clk.h>
44#include <linux/cpufreq.h>
45
46#include <asm/irq.h>
47
48#include <mach/hardware.h>
49#include <mach/map.h>
50
51#include <plat/regs-serial.h>
52
53#include "samsung.h"
54
55/* UART name and device definitions */
56
57#define S3C24XX_SERIAL_NAME "ttySAC"
58#define S3C24XX_SERIAL_MAJOR 204
59#define S3C24XX_SERIAL_MINOR 64
60
61/* macros to change one thing to another */
62
63#define tx_enabled(port) ((port)->unused[0])
64#define rx_enabled(port) ((port)->unused[1])
65
66/* flag to ignore all characters coming in */
67#define RXSTAT_DUMMY_READ (0x10000000)
68
69static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
70{
71 return container_of(port, struct s3c24xx_uart_port, port);
72}
73
74/* translate a port to the device name */
75
76static inline const char *s3c24xx_serial_portname(struct uart_port *port)
77{
78 return to_platform_device(port->dev)->name;
79}
80
81static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
82{
83 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
84}
85
86static void s3c24xx_serial_rx_enable(struct uart_port *port)
87{
88 unsigned long flags;
89 unsigned int ucon, ufcon;
90 int count = 10000;
91
92 spin_lock_irqsave(&port->lock, flags);
93
94 while (--count && !s3c24xx_serial_txempty_nofifo(port))
95 udelay(100);
96
97 ufcon = rd_regl(port, S3C2410_UFCON);
98 ufcon |= S3C2410_UFCON_RESETRX;
99 wr_regl(port, S3C2410_UFCON, ufcon);
100
101 ucon = rd_regl(port, S3C2410_UCON);
102 ucon |= S3C2410_UCON_RXIRQMODE;
103 wr_regl(port, S3C2410_UCON, ucon);
104
105 rx_enabled(port) = 1;
106 spin_unlock_irqrestore(&port->lock, flags);
107}
108
109static void s3c24xx_serial_rx_disable(struct uart_port *port)
110{
111 unsigned long flags;
112 unsigned int ucon;
113
114 spin_lock_irqsave(&port->lock, flags);
115
116 ucon = rd_regl(port, S3C2410_UCON);
117 ucon &= ~S3C2410_UCON_RXIRQMODE;
118 wr_regl(port, S3C2410_UCON, ucon);
119
120 rx_enabled(port) = 0;
121 spin_unlock_irqrestore(&port->lock, flags);
122}
123
124static void s3c24xx_serial_stop_tx(struct uart_port *port)
125{
126 struct s3c24xx_uart_port *ourport = to_ourport(port);
127
128 if (tx_enabled(port)) {
129 disable_irq_nosync(ourport->tx_irq);
130 tx_enabled(port) = 0;
131 if (port->flags & UPF_CONS_FLOW)
132 s3c24xx_serial_rx_enable(port);
133 }
134}
135
136static void s3c24xx_serial_start_tx(struct uart_port *port)
137{
138 struct s3c24xx_uart_port *ourport = to_ourport(port);
139
140 if (!tx_enabled(port)) {
141 if (port->flags & UPF_CONS_FLOW)
142 s3c24xx_serial_rx_disable(port);
143
144 enable_irq(ourport->tx_irq);
145 tx_enabled(port) = 1;
146 }
147}
148
149
150static void s3c24xx_serial_stop_rx(struct uart_port *port)
151{
152 struct s3c24xx_uart_port *ourport = to_ourport(port);
153
154 if (rx_enabled(port)) {
155 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
156 disable_irq_nosync(ourport->rx_irq);
157 rx_enabled(port) = 0;
158 }
159}
160
161static void s3c24xx_serial_enable_ms(struct uart_port *port)
162{
163}
164
165static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
166{
167 return to_ourport(port)->info;
168}
169
170static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
171{
172 if (port->dev == NULL)
173 return NULL;
174
175 return (struct s3c2410_uartcfg *)port->dev->platform_data;
176}
177
178static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
179 unsigned long ufstat)
180{
181 struct s3c24xx_uart_info *info = ourport->info;
182
183 if (ufstat & info->rx_fifofull)
184 return info->fifosize;
185
186 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
187}
188
189
190/* ? - where has parity gone?? */
191#define S3C2410_UERSTAT_PARITY (0x1000)
192
193static irqreturn_t
194s3c24xx_serial_rx_chars(int irq, void *dev_id)
195{
196 struct s3c24xx_uart_port *ourport = dev_id;
197 struct uart_port *port = &ourport->port;
198 struct tty_struct *tty = port->state->port.tty;
199 unsigned int ufcon, ch, flag, ufstat, uerstat;
200 int max_count = 64;
201
202 while (max_count-- > 0) {
203 ufcon = rd_regl(port, S3C2410_UFCON);
204 ufstat = rd_regl(port, S3C2410_UFSTAT);
205
206 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
207 break;
208
209 uerstat = rd_regl(port, S3C2410_UERSTAT);
210 ch = rd_regb(port, S3C2410_URXH);
211
212 if (port->flags & UPF_CONS_FLOW) {
213 int txe = s3c24xx_serial_txempty_nofifo(port);
214
215 if (rx_enabled(port)) {
216 if (!txe) {
217 rx_enabled(port) = 0;
218 continue;
219 }
220 } else {
221 if (txe) {
222 ufcon |= S3C2410_UFCON_RESETRX;
223 wr_regl(port, S3C2410_UFCON, ufcon);
224 rx_enabled(port) = 1;
225 goto out;
226 }
227 continue;
228 }
229 }
230
231 /* insert the character into the buffer */
232
233 flag = TTY_NORMAL;
234 port->icount.rx++;
235
236 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
237 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
238 ch, uerstat);
239
240 /* check for break */
241 if (uerstat & S3C2410_UERSTAT_BREAK) {
242 dbg("break!\n");
243 port->icount.brk++;
244 if (uart_handle_break(port))
245 goto ignore_char;
246 }
247
248 if (uerstat & S3C2410_UERSTAT_FRAME)
249 port->icount.frame++;
250 if (uerstat & S3C2410_UERSTAT_OVERRUN)
251 port->icount.overrun++;
252
253 uerstat &= port->read_status_mask;
254
255 if (uerstat & S3C2410_UERSTAT_BREAK)
256 flag = TTY_BREAK;
257 else if (uerstat & S3C2410_UERSTAT_PARITY)
258 flag = TTY_PARITY;
259 else if (uerstat & (S3C2410_UERSTAT_FRAME |
260 S3C2410_UERSTAT_OVERRUN))
261 flag = TTY_FRAME;
262 }
263
264 if (uart_handle_sysrq_char(port, ch))
265 goto ignore_char;
266
267 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
268 ch, flag);
269
270 ignore_char:
271 continue;
272 }
273 tty_flip_buffer_push(tty);
274
275 out:
276 return IRQ_HANDLED;
277}
278
279static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
280{
281 struct s3c24xx_uart_port *ourport = id;
282 struct uart_port *port = &ourport->port;
283 struct circ_buf *xmit = &port->state->xmit;
284 int count = 256;
285
286 if (port->x_char) {
287 wr_regb(port, S3C2410_UTXH, port->x_char);
288 port->icount.tx++;
289 port->x_char = 0;
290 goto out;
291 }
292
293 /* if there isn't anything more to transmit, or the uart is now
294 * stopped, disable the uart and exit
295 */
296
297 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
298 s3c24xx_serial_stop_tx(port);
299 goto out;
300 }
301
302 /* try and drain the buffer... */
303
304 while (!uart_circ_empty(xmit) && count-- > 0) {
305 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
306 break;
307
308 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
309 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
310 port->icount.tx++;
311 }
312
313 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
314 uart_write_wakeup(port);
315
316 if (uart_circ_empty(xmit))
317 s3c24xx_serial_stop_tx(port);
318
319 out:
320 return IRQ_HANDLED;
321}
322
323static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
324{
325 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
326 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
327 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
328
329 if (ufcon & S3C2410_UFCON_FIFOMODE) {
330 if ((ufstat & info->tx_fifomask) != 0 ||
331 (ufstat & info->tx_fifofull))
332 return 0;
333
334 return 1;
335 }
336
337 return s3c24xx_serial_txempty_nofifo(port);
338}
339
340/* no modem control lines */
341static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
342{
343 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
344
345 if (umstat & S3C2410_UMSTAT_CTS)
346 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
347 else
348 return TIOCM_CAR | TIOCM_DSR;
349}
350
351static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
352{
353 /* todo - possibly remove AFC and do manual CTS */
354}
355
356static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
357{
358 unsigned long flags;
359 unsigned int ucon;
360
361 spin_lock_irqsave(&port->lock, flags);
362
363 ucon = rd_regl(port, S3C2410_UCON);
364
365 if (break_state)
366 ucon |= S3C2410_UCON_SBREAK;
367 else
368 ucon &= ~S3C2410_UCON_SBREAK;
369
370 wr_regl(port, S3C2410_UCON, ucon);
371
372 spin_unlock_irqrestore(&port->lock, flags);
373}
374
375static void s3c24xx_serial_shutdown(struct uart_port *port)
376{
377 struct s3c24xx_uart_port *ourport = to_ourport(port);
378
379 if (ourport->tx_claimed) {
380 free_irq(ourport->tx_irq, ourport);
381 tx_enabled(port) = 0;
382 ourport->tx_claimed = 0;
383 }
384
385 if (ourport->rx_claimed) {
386 free_irq(ourport->rx_irq, ourport);
387 ourport->rx_claimed = 0;
388 rx_enabled(port) = 0;
389 }
390}
391
392
393static int s3c24xx_serial_startup(struct uart_port *port)
394{
395 struct s3c24xx_uart_port *ourport = to_ourport(port);
396 int ret;
397
398 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
399 port->mapbase, port->membase);
400
401 rx_enabled(port) = 1;
402
403 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
404 s3c24xx_serial_portname(port), ourport);
405
406 if (ret != 0) {
407 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
408 return ret;
409 }
410
411 ourport->rx_claimed = 1;
412
413 dbg("requesting tx irq...\n");
414
415 tx_enabled(port) = 1;
416
417 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
418 s3c24xx_serial_portname(port), ourport);
419
420 if (ret) {
421 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
422 goto err;
423 }
424
425 ourport->tx_claimed = 1;
426
427 dbg("s3c24xx_serial_startup ok\n");
428
429 /* the port reset code should have done the correct
430 * register setup for the port controls */
431
432 return ret;
433
434 err:
435 s3c24xx_serial_shutdown(port);
436 return ret;
437}
438
439/* power power management control */
440
441static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
442 unsigned int old)
443{
444 struct s3c24xx_uart_port *ourport = to_ourport(port);
445
446 ourport->pm_level = level;
447
448 switch (level) {
449 case 3:
450 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
451 clk_disable(ourport->baudclk);
452
453 clk_disable(ourport->clk);
454 break;
455
456 case 0:
457 clk_enable(ourport->clk);
458
459 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
460 clk_enable(ourport->baudclk);
461
462 break;
463 default:
464 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
465 }
466}
467
468/* baud rate calculation
469 *
470 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
471 * of different sources, including the peripheral clock ("pclk") and an
472 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
473 * with a programmable extra divisor.
474 *
475 * The following code goes through the clock sources, and calculates the
476 * baud clocks (and the resultant actual baud rates) and then tries to
477 * pick the closest one and select that.
478 *
479*/
480
481
482#define MAX_CLKS (8)
483
484static struct s3c24xx_uart_clksrc tmp_clksrc = {
485 .name = "pclk",
486 .min_baud = 0,
487 .max_baud = 0,
488 .divisor = 1,
489};
490
491static inline int
492s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
493{
494 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
495
496 return (info->get_clksrc)(port, c);
497}
498
499static inline int
500s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
501{
502 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
503
504 return (info->set_clksrc)(port, c);
505}
506
507struct baud_calc {
508 struct s3c24xx_uart_clksrc *clksrc;
509 unsigned int calc;
510 unsigned int divslot;
511 unsigned int quot;
512 struct clk *src;
513};
514
515static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
516 struct uart_port *port,
517 struct s3c24xx_uart_clksrc *clksrc,
518 unsigned int baud)
519{
520 struct s3c24xx_uart_port *ourport = to_ourport(port);
521 unsigned long rate;
522
523 calc->src = clk_get(port->dev, clksrc->name);
524 if (calc->src == NULL || IS_ERR(calc->src))
525 return 0;
526
527 rate = clk_get_rate(calc->src);
528 rate /= clksrc->divisor;
529
530 calc->clksrc = clksrc;
531
532 if (ourport->info->has_divslot) {
533 unsigned long div = rate / baud;
534
535 /* The UDIVSLOT register on the newer UARTs allows us to
536 * get a divisor adjustment of 1/16th on the baud clock.
537 *
538 * We don't keep the UDIVSLOT value (the 16ths we calculated
539 * by not multiplying the baud by 16) as it is easy enough
540 * to recalculate.
541 */
542
543 calc->quot = div / 16;
544 calc->calc = rate / div;
545 } else {
546 calc->quot = (rate + (8 * baud)) / (16 * baud);
547 calc->calc = (rate / (calc->quot * 16));
548 }
549
550 calc->quot--;
551 return 1;
552}
553
554static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
555 struct s3c24xx_uart_clksrc **clksrc,
556 struct clk **clk,
557 unsigned int baud)
558{
559 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
560 struct s3c24xx_uart_clksrc *clkp;
561 struct baud_calc res[MAX_CLKS];
562 struct baud_calc *resptr, *best, *sptr;
563 int i;
564
565 clkp = cfg->clocks;
566 best = NULL;
567
568 if (cfg->clocks_size < 2) {
569 if (cfg->clocks_size == 0)
570 clkp = &tmp_clksrc;
571
572 /* check to see if we're sourcing fclk, and if so we're
573 * going to have to update the clock source
574 */
575
576 if (strcmp(clkp->name, "fclk") == 0) {
577 struct s3c24xx_uart_clksrc src;
578
579 s3c24xx_serial_getsource(port, &src);
580
581 /* check that the port already using fclk, and if
582 * not, then re-select fclk
583 */
584
585 if (strcmp(src.name, clkp->name) == 0) {
586 s3c24xx_serial_setsource(port, clkp);
587 s3c24xx_serial_getsource(port, &src);
588 }
589
590 clkp->divisor = src.divisor;
591 }
592
593 s3c24xx_serial_calcbaud(res, port, clkp, baud);
594 best = res;
595 resptr = best + 1;
596 } else {
597 resptr = res;
598
599 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
600 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
601 resptr++;
602 }
603 }
604
605 /* ok, we now need to select the best clock we found */
606
607 if (!best) {
608 unsigned int deviation = (1<<30)|((1<<30)-1);
609 int calc_deviation;
610
611 for (sptr = res; sptr < resptr; sptr++) {
612 calc_deviation = baud - sptr->calc;
613 if (calc_deviation < 0)
614 calc_deviation = -calc_deviation;
615
616 if (calc_deviation < deviation) {
617 best = sptr;
618 deviation = calc_deviation;
619 }
620 }
621 }
622
623 /* store results to pass back */
624
625 *clksrc = best->clksrc;
626 *clk = best->src;
627
628 return best->quot;
629}
630
631/* udivslot_table[]
632 *
633 * This table takes the fractional value of the baud divisor and gives
634 * the recommended setting for the UDIVSLOT register.
635 */
636static u16 udivslot_table[16] = {
637 [0] = 0x0000,
638 [1] = 0x0080,
639 [2] = 0x0808,
640 [3] = 0x0888,
641 [4] = 0x2222,
642 [5] = 0x4924,
643 [6] = 0x4A52,
644 [7] = 0x54AA,
645 [8] = 0x5555,
646 [9] = 0xD555,
647 [10] = 0xD5D5,
648 [11] = 0xDDD5,
649 [12] = 0xDDDD,
650 [13] = 0xDFDD,
651 [14] = 0xDFDF,
652 [15] = 0xFFDF,
653};
654
655static void s3c24xx_serial_set_termios(struct uart_port *port,
656 struct ktermios *termios,
657 struct ktermios *old)
658{
659 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
660 struct s3c24xx_uart_port *ourport = to_ourport(port);
661 struct s3c24xx_uart_clksrc *clksrc = NULL;
662 struct clk *clk = NULL;
663 unsigned long flags;
664 unsigned int baud, quot;
665 unsigned int ulcon;
666 unsigned int umcon;
667 unsigned int udivslot = 0;
668
669 /*
670 * We don't support modem control lines.
671 */
672 termios->c_cflag &= ~(HUPCL | CMSPAR);
673 termios->c_cflag |= CLOCAL;
674
675 /*
676 * Ask the core to calculate the divisor for us.
677 */
678
679 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
680
681 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
682 quot = port->custom_divisor;
683 else
684 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
685
686 /* check to see if we need to change clock source */
687
688 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
689 dbg("selecting clock %p\n", clk);
690 s3c24xx_serial_setsource(port, clksrc);
691
692 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
693 clk_disable(ourport->baudclk);
694 ourport->baudclk = NULL;
695 }
696
697 clk_enable(clk);
698
699 ourport->clksrc = clksrc;
700 ourport->baudclk = clk;
701 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
702 }
703
704 if (ourport->info->has_divslot) {
705 unsigned int div = ourport->baudclk_rate / baud;
706
707 if (cfg->has_fracval) {
708 udivslot = (div & 15);
709 dbg("fracval = %04x\n", udivslot);
710 } else {
711 udivslot = udivslot_table[div & 15];
712 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
713 }
714 }
715
716 switch (termios->c_cflag & CSIZE) {
717 case CS5:
718 dbg("config: 5bits/char\n");
719 ulcon = S3C2410_LCON_CS5;
720 break;
721 case CS6:
722 dbg("config: 6bits/char\n");
723 ulcon = S3C2410_LCON_CS6;
724 break;
725 case CS7:
726 dbg("config: 7bits/char\n");
727 ulcon = S3C2410_LCON_CS7;
728 break;
729 case CS8:
730 default:
731 dbg("config: 8bits/char\n");
732 ulcon = S3C2410_LCON_CS8;
733 break;
734 }
735
736 /* preserve original lcon IR settings */
737 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
738
739 if (termios->c_cflag & CSTOPB)
740 ulcon |= S3C2410_LCON_STOPB;
741
742 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
743
744 if (termios->c_cflag & PARENB) {
745 if (termios->c_cflag & PARODD)
746 ulcon |= S3C2410_LCON_PODD;
747 else
748 ulcon |= S3C2410_LCON_PEVEN;
749 } else {
750 ulcon |= S3C2410_LCON_PNONE;
751 }
752
753 spin_lock_irqsave(&port->lock, flags);
754
755 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
756 ulcon, quot, udivslot);
757
758 wr_regl(port, S3C2410_ULCON, ulcon);
759 wr_regl(port, S3C2410_UBRDIV, quot);
760 wr_regl(port, S3C2410_UMCON, umcon);
761
762 if (ourport->info->has_divslot)
763 wr_regl(port, S3C2443_DIVSLOT, udivslot);
764
765 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
766 rd_regl(port, S3C2410_ULCON),
767 rd_regl(port, S3C2410_UCON),
768 rd_regl(port, S3C2410_UFCON));
769
770 /*
771 * Update the per-port timeout.
772 */
773 uart_update_timeout(port, termios->c_cflag, baud);
774
775 /*
776 * Which character status flags are we interested in?
777 */
778 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
779 if (termios->c_iflag & INPCK)
780 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
781
782 /*
783 * Which character status flags should we ignore?
784 */
785 port->ignore_status_mask = 0;
786 if (termios->c_iflag & IGNPAR)
787 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
788 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
789 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
790
791 /*
792 * Ignore all characters if CREAD is not set.
793 */
794 if ((termios->c_cflag & CREAD) == 0)
795 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
796
797 spin_unlock_irqrestore(&port->lock, flags);
798}
799
800static const char *s3c24xx_serial_type(struct uart_port *port)
801{
802 switch (port->type) {
803 case PORT_S3C2410:
804 return "S3C2410";
805 case PORT_S3C2440:
806 return "S3C2440";
807 case PORT_S3C2412:
808 return "S3C2412";
809 case PORT_S3C6400:
810 return "S3C6400/10";
811 default:
812 return NULL;
813 }
814}
815
816#define MAP_SIZE (0x100)
817
818static void s3c24xx_serial_release_port(struct uart_port *port)
819{
820 release_mem_region(port->mapbase, MAP_SIZE);
821}
822
823static int s3c24xx_serial_request_port(struct uart_port *port)
824{
825 const char *name = s3c24xx_serial_portname(port);
826 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
827}
828
829static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
830{
831 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
832
833 if (flags & UART_CONFIG_TYPE &&
834 s3c24xx_serial_request_port(port) == 0)
835 port->type = info->type;
836}
837
838/*
839 * verify the new serial_struct (for TIOCSSERIAL).
840 */
841static int
842s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
843{
844 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
845
846 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
847 return -EINVAL;
848
849 return 0;
850}
851
852
853#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
854
855static struct console s3c24xx_serial_console;
856
857#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
858#else
859#define S3C24XX_SERIAL_CONSOLE NULL
860#endif
861
862static struct uart_ops s3c24xx_serial_ops = {
863 .pm = s3c24xx_serial_pm,
864 .tx_empty = s3c24xx_serial_tx_empty,
865 .get_mctrl = s3c24xx_serial_get_mctrl,
866 .set_mctrl = s3c24xx_serial_set_mctrl,
867 .stop_tx = s3c24xx_serial_stop_tx,
868 .start_tx = s3c24xx_serial_start_tx,
869 .stop_rx = s3c24xx_serial_stop_rx,
870 .enable_ms = s3c24xx_serial_enable_ms,
871 .break_ctl = s3c24xx_serial_break_ctl,
872 .startup = s3c24xx_serial_startup,
873 .shutdown = s3c24xx_serial_shutdown,
874 .set_termios = s3c24xx_serial_set_termios,
875 .type = s3c24xx_serial_type,
876 .release_port = s3c24xx_serial_release_port,
877 .request_port = s3c24xx_serial_request_port,
878 .config_port = s3c24xx_serial_config_port,
879 .verify_port = s3c24xx_serial_verify_port,
880};
881
882
883static struct uart_driver s3c24xx_uart_drv = {
884 .owner = THIS_MODULE,
885 .driver_name = "s3c2410_serial",
886 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
887 .cons = S3C24XX_SERIAL_CONSOLE,
888 .dev_name = S3C24XX_SERIAL_NAME,
889 .major = S3C24XX_SERIAL_MAJOR,
890 .minor = S3C24XX_SERIAL_MINOR,
891};
892
893static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
894 [0] = {
895 .port = {
896 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
897 .iotype = UPIO_MEM,
898 .irq = IRQ_S3CUART_RX0,
899 .uartclk = 0,
900 .fifosize = 16,
901 .ops = &s3c24xx_serial_ops,
902 .flags = UPF_BOOT_AUTOCONF,
903 .line = 0,
904 }
905 },
906 [1] = {
907 .port = {
908 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
909 .iotype = UPIO_MEM,
910 .irq = IRQ_S3CUART_RX1,
911 .uartclk = 0,
912 .fifosize = 16,
913 .ops = &s3c24xx_serial_ops,
914 .flags = UPF_BOOT_AUTOCONF,
915 .line = 1,
916 }
917 },
918#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
919
920 [2] = {
921 .port = {
922 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
923 .iotype = UPIO_MEM,
924 .irq = IRQ_S3CUART_RX2,
925 .uartclk = 0,
926 .fifosize = 16,
927 .ops = &s3c24xx_serial_ops,
928 .flags = UPF_BOOT_AUTOCONF,
929 .line = 2,
930 }
931 },
932#endif
933#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
934 [3] = {
935 .port = {
936 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
937 .iotype = UPIO_MEM,
938 .irq = IRQ_S3CUART_RX3,
939 .uartclk = 0,
940 .fifosize = 16,
941 .ops = &s3c24xx_serial_ops,
942 .flags = UPF_BOOT_AUTOCONF,
943 .line = 3,
944 }
945 }
946#endif
947};
948
949/* s3c24xx_serial_resetport
950 *
951 * wrapper to call the specific reset for this port (reset the fifos
952 * and the settings)
953*/
954
955static inline int s3c24xx_serial_resetport(struct uart_port *port,
956 struct s3c2410_uartcfg *cfg)
957{
958 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
959
960 return (info->reset_port)(port, cfg);
961}
962
963
964#ifdef CONFIG_CPU_FREQ
965
966static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
967 unsigned long val, void *data)
968{
969 struct s3c24xx_uart_port *port;
970 struct uart_port *uport;
971
972 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
973 uport = &port->port;
974
975 /* check to see if port is enabled */
976
977 if (port->pm_level != 0)
978 return 0;
979
980 /* try and work out if the baudrate is changing, we can detect
981 * a change in rate, but we do not have support for detecting
982 * a disturbance in the clock-rate over the change.
983 */
984
985 if (IS_ERR(port->clk))
986 goto exit;
987
988 if (port->baudclk_rate == clk_get_rate(port->clk))
989 goto exit;
990
991 if (val == CPUFREQ_PRECHANGE) {
992 /* we should really shut the port down whilst the
993 * frequency change is in progress. */
994
995 } else if (val == CPUFREQ_POSTCHANGE) {
996 struct ktermios *termios;
997 struct tty_struct *tty;
998
999 if (uport->state == NULL)
1000 goto exit;
1001
1002 tty = uport->state->port.tty;
1003
1004 if (tty == NULL)
1005 goto exit;
1006
1007 termios = tty->termios;
1008
1009 if (termios == NULL) {
1010 printk(KERN_WARNING "%s: no termios?\n", __func__);
1011 goto exit;
1012 }
1013
1014 s3c24xx_serial_set_termios(uport, termios, NULL);
1015 }
1016
1017 exit:
1018 return 0;
1019}
1020
1021static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1022{
1023 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1024
1025 return cpufreq_register_notifier(&port->freq_transition,
1026 CPUFREQ_TRANSITION_NOTIFIER);
1027}
1028
1029static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1030{
1031 cpufreq_unregister_notifier(&port->freq_transition,
1032 CPUFREQ_TRANSITION_NOTIFIER);
1033}
1034
1035#else
1036static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1037{
1038 return 0;
1039}
1040
1041static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1042{
1043}
1044#endif
1045
1046/* s3c24xx_serial_init_port
1047 *
1048 * initialise a single serial port from the platform device given
1049 */
1050
1051static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1052 struct s3c24xx_uart_info *info,
1053 struct platform_device *platdev)
1054{
1055 struct uart_port *port = &ourport->port;
1056 struct s3c2410_uartcfg *cfg;
1057 struct resource *res;
1058 int ret;
1059
1060 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1061
1062 if (platdev == NULL)
1063 return -ENODEV;
1064
1065 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1066
1067 if (port->mapbase != 0)
1068 return 0;
1069
1070 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1071 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1072 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1073 return -ERANGE;
1074 }
1075
1076 /* setup info for port */
1077 port->dev = &platdev->dev;
1078 ourport->info = info;
1079
1080 /* copy the info in from provided structure */
1081 ourport->port.fifosize = info->fifosize;
1082
1083 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1084
1085 port->uartclk = 1;
1086
1087 if (cfg->uart_flags & UPF_CONS_FLOW) {
1088 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1089 port->flags |= UPF_CONS_FLOW;
1090 }
1091
1092 /* sort our the physical and virtual addresses for each UART */
1093
1094 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1095 if (res == NULL) {
1096 printk(KERN_ERR "failed to find memory resource for uart\n");
1097 return -EINVAL;
1098 }
1099
1100 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1101
1102 port->mapbase = res->start;
1103 port->membase = S3C_VA_UART + (res->start & 0xfffff);
1104 ret = platform_get_irq(platdev, 0);
1105 if (ret < 0)
1106 port->irq = 0;
1107 else {
1108 port->irq = ret;
1109 ourport->rx_irq = ret;
1110 ourport->tx_irq = ret + 1;
1111 }
1112
1113 ret = platform_get_irq(platdev, 1);
1114 if (ret > 0)
1115 ourport->tx_irq = ret;
1116
1117 ourport->clk = clk_get(&platdev->dev, "uart");
1118
1119 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1120 port->mapbase, port->membase, port->irq,
1121 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1122
1123 /* reset the fifos (and setup the uart) */
1124 s3c24xx_serial_resetport(port, cfg);
1125 return 0;
1126}
1127
1128static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1129 struct device_attribute *attr,
1130 char *buf)
1131{
1132 struct uart_port *port = s3c24xx_dev_to_port(dev);
1133 struct s3c24xx_uart_port *ourport = to_ourport(port);
1134
1135 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1136}
1137
1138static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1139
1140/* Device driver serial port probe */
1141
1142static int probe_index;
1143
1144int s3c24xx_serial_probe(struct platform_device *dev,
1145 struct s3c24xx_uart_info *info)
1146{
1147 struct s3c24xx_uart_port *ourport;
1148 int ret;
1149
1150 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1151
1152 ourport = &s3c24xx_serial_ports[probe_index];
1153 probe_index++;
1154
1155 dbg("%s: initialising port %p...\n", __func__, ourport);
1156
1157 ret = s3c24xx_serial_init_port(ourport, info, dev);
1158 if (ret < 0)
1159 goto probe_err;
1160
1161 dbg("%s: adding port\n", __func__);
1162 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1163 platform_set_drvdata(dev, &ourport->port);
1164
1165 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1166 if (ret < 0)
1167 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1168
1169 ret = s3c24xx_serial_cpufreq_register(ourport);
1170 if (ret < 0)
1171 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1172
1173 return 0;
1174
1175 probe_err:
1176 return ret;
1177}
1178
1179EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1180
1181int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1182{
1183 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1184
1185 if (port) {
1186 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1187 device_remove_file(&dev->dev, &dev_attr_clock_source);
1188 uart_remove_one_port(&s3c24xx_uart_drv, port);
1189 }
1190
1191 return 0;
1192}
1193
1194EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1195
1196/* UART power management code */
1197
1198#ifdef CONFIG_PM
1199
1200static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1201{
1202 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1203
1204 if (port)
1205 uart_suspend_port(&s3c24xx_uart_drv, port);
1206
1207 return 0;
1208}
1209
1210static int s3c24xx_serial_resume(struct platform_device *dev)
1211{
1212 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1213 struct s3c24xx_uart_port *ourport = to_ourport(port);
1214
1215 if (port) {
1216 clk_enable(ourport->clk);
1217 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1218 clk_disable(ourport->clk);
1219
1220 uart_resume_port(&s3c24xx_uart_drv, port);
1221 }
1222
1223 return 0;
1224}
1225#endif
1226
1227int s3c24xx_serial_init(struct platform_driver *drv,
1228 struct s3c24xx_uart_info *info)
1229{
1230 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1231
1232#ifdef CONFIG_PM
1233 drv->suspend = s3c24xx_serial_suspend;
1234 drv->resume = s3c24xx_serial_resume;
1235#endif
1236
1237 return platform_driver_register(drv);
1238}
1239
1240EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1241
1242/* module initialisation code */
1243
1244static int __init s3c24xx_serial_modinit(void)
1245{
1246 int ret;
1247
1248 ret = uart_register_driver(&s3c24xx_uart_drv);
1249 if (ret < 0) {
1250 printk(KERN_ERR "failed to register UART driver\n");
1251 return -1;
1252 }
1253
1254 return 0;
1255}
1256
1257static void __exit s3c24xx_serial_modexit(void)
1258{
1259 uart_unregister_driver(&s3c24xx_uart_drv);
1260}
1261
1262module_init(s3c24xx_serial_modinit);
1263module_exit(s3c24xx_serial_modexit);
1264
1265/* Console code */
1266
1267#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1268
1269static struct uart_port *cons_uart;
1270
1271static int
1272s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1273{
1274 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1275 unsigned long ufstat, utrstat;
1276
1277 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1278 /* fifo mode - check amount of data in fifo registers... */
1279
1280 ufstat = rd_regl(port, S3C2410_UFSTAT);
1281 return (ufstat & info->tx_fifofull) ? 0 : 1;
1282 }
1283
1284 /* in non-fifo mode, we go and use the tx buffer empty */
1285
1286 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1287 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1288}
1289
1290static void
1291s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1292{
1293 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1294 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1295 barrier();
1296 wr_regb(cons_uart, S3C2410_UTXH, ch);
1297}
1298
1299static void
1300s3c24xx_serial_console_write(struct console *co, const char *s,
1301 unsigned int count)
1302{
1303 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1304}
1305
1306static void __init
1307s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1308 int *parity, int *bits)
1309{
1310 struct s3c24xx_uart_clksrc clksrc;
1311 struct clk *clk;
1312 unsigned int ulcon;
1313 unsigned int ucon;
1314 unsigned int ubrdiv;
1315 unsigned long rate;
1316
1317 ulcon = rd_regl(port, S3C2410_ULCON);
1318 ucon = rd_regl(port, S3C2410_UCON);
1319 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1320
1321 dbg("s3c24xx_serial_get_options: port=%p\n"
1322 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1323 port, ulcon, ucon, ubrdiv);
1324
1325 if ((ucon & 0xf) != 0) {
1326 /* consider the serial port configured if the tx/rx mode set */
1327
1328 switch (ulcon & S3C2410_LCON_CSMASK) {
1329 case S3C2410_LCON_CS5:
1330 *bits = 5;
1331 break;
1332 case S3C2410_LCON_CS6:
1333 *bits = 6;
1334 break;
1335 case S3C2410_LCON_CS7:
1336 *bits = 7;
1337 break;
1338 default:
1339 case S3C2410_LCON_CS8:
1340 *bits = 8;
1341 break;
1342 }
1343
1344 switch (ulcon & S3C2410_LCON_PMASK) {
1345 case S3C2410_LCON_PEVEN:
1346 *parity = 'e';
1347 break;
1348
1349 case S3C2410_LCON_PODD:
1350 *parity = 'o';
1351 break;
1352
1353 case S3C2410_LCON_PNONE:
1354 default:
1355 *parity = 'n';
1356 }
1357
1358 /* now calculate the baud rate */
1359
1360 s3c24xx_serial_getsource(port, &clksrc);
1361
1362 clk = clk_get(port->dev, clksrc.name);
1363 if (!IS_ERR(clk) && clk != NULL)
1364 rate = clk_get_rate(clk) / clksrc.divisor;
1365 else
1366 rate = 1;
1367
1368
1369 *baud = rate / (16 * (ubrdiv + 1));
1370 dbg("calculated baud %d\n", *baud);
1371 }
1372
1373}
1374
1375/* s3c24xx_serial_init_ports
1376 *
1377 * initialise the serial ports from the machine provided initialisation
1378 * data.
1379*/
1380
1381static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
1382{
1383 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1384 struct platform_device **platdev_ptr;
1385 int i;
1386
1387 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1388
1389 platdev_ptr = s3c24xx_uart_devs;
1390
1391 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
1392 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
1393 }
1394
1395 return 0;
1396}
1397
1398static int __init
1399s3c24xx_serial_console_setup(struct console *co, char *options)
1400{
1401 struct uart_port *port;
1402 int baud = 9600;
1403 int bits = 8;
1404 int parity = 'n';
1405 int flow = 'n';
1406
1407 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1408 co, co->index, options);
1409
1410 /* is this a valid port */
1411
1412 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1413 co->index = 0;
1414
1415 port = &s3c24xx_serial_ports[co->index].port;
1416
1417 /* is the port configured? */
1418
1419 if (port->mapbase == 0x0) {
1420 co->index = 0;
1421 port = &s3c24xx_serial_ports[co->index].port;
1422 }
1423
1424 cons_uart = port;
1425
1426 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1427
1428 /*
1429 * Check whether an invalid uart number has been specified, and
1430 * if so, search for the first available port that does have
1431 * console support.
1432 */
1433 if (options)
1434 uart_parse_options(options, &baud, &parity, &bits, &flow);
1435 else
1436 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1437
1438 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1439
1440 return uart_set_options(port, co, baud, parity, bits, flow);
1441}
1442
1443/* s3c24xx_serial_initconsole
1444 *
1445 * initialise the console from one of the uart drivers
1446*/
1447
1448static struct console s3c24xx_serial_console = {
1449 .name = S3C24XX_SERIAL_NAME,
1450 .device = uart_console_device,
1451 .flags = CON_PRINTBUFFER,
1452 .index = -1,
1453 .write = s3c24xx_serial_console_write,
1454 .setup = s3c24xx_serial_console_setup
1455};
1456
1457int s3c24xx_serial_initconsole(struct platform_driver *drv,
1458 struct s3c24xx_uart_info **info)
1459
1460{
1461 struct platform_device *dev = s3c24xx_uart_devs[0];
1462
1463 dbg("s3c24xx_serial_initconsole\n");
1464
1465 /* select driver based on the cpu */
1466
1467 if (dev == NULL) {
1468 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1469 return 0;
1470 }
1471
1472 if (strcmp(dev->name, drv->driver.name) != 0)
1473 return 0;
1474
1475 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1476 s3c24xx_serial_init_ports(info);
1477
1478 register_console(&s3c24xx_serial_console);
1479 return 0;
1480}
1481
1482#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1483
1484MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1485MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1486MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/samsung.h b/drivers/tty/serial/samsung.h
new file mode 100644
index 000000000000..5b098cd76040
--- /dev/null
+++ b/drivers/tty/serial/samsung.h
@@ -0,0 +1,119 @@
1/*
2 * Driver for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12struct s3c24xx_uart_info {
13 char *name;
14 unsigned int type;
15 unsigned int fifosize;
16 unsigned long rx_fifomask;
17 unsigned long rx_fifoshift;
18 unsigned long rx_fifofull;
19 unsigned long tx_fifomask;
20 unsigned long tx_fifoshift;
21 unsigned long tx_fifofull;
22
23 /* uart port features */
24
25 unsigned int has_divslot:1;
26
27 /* clock source control */
28
29 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
30 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
31
32 /* uart controls */
33 int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
34};
35
36struct s3c24xx_uart_port {
37 unsigned char rx_claimed;
38 unsigned char tx_claimed;
39 unsigned int pm_level;
40 unsigned long baudclk_rate;
41
42 unsigned int rx_irq;
43 unsigned int tx_irq;
44
45 struct s3c24xx_uart_info *info;
46 struct s3c24xx_uart_clksrc *clksrc;
47 struct clk *clk;
48 struct clk *baudclk;
49 struct uart_port port;
50
51#ifdef CONFIG_CPU_FREQ
52 struct notifier_block freq_transition;
53#endif
54};
55
56/* conversion functions */
57
58#define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
59#define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
60
61/* register access controls */
62
63#define portaddr(port, reg) ((port)->membase + (reg))
64
65#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
66#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
67
68#define wr_regb(port, reg, val) __raw_writeb(val, portaddr(port, reg))
69#define wr_regl(port, reg, val) __raw_writel(val, portaddr(port, reg))
70
71extern int s3c24xx_serial_probe(struct platform_device *dev,
72 struct s3c24xx_uart_info *uart);
73
74extern int __devexit s3c24xx_serial_remove(struct platform_device *dev);
75
76extern int s3c24xx_serial_initconsole(struct platform_driver *drv,
77 struct s3c24xx_uart_info **uart);
78
79extern int s3c24xx_serial_init(struct platform_driver *drv,
80 struct s3c24xx_uart_info *info);
81
82#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
83
84#define s3c24xx_console_init(__drv, __inf) \
85static int __init s3c_serial_console_init(void) \
86{ \
87 struct s3c24xx_uart_info *uinfo[CONFIG_SERIAL_SAMSUNG_UARTS]; \
88 int i; \
89 \
90 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++) \
91 uinfo[i] = __inf; \
92 return s3c24xx_serial_initconsole(__drv, uinfo); \
93} \
94 \
95console_initcall(s3c_serial_console_init)
96
97#else
98#define s3c24xx_console_init(drv, inf) extern void no_console(void)
99#endif
100
101#ifdef CONFIG_SERIAL_SAMSUNG_DEBUG
102
103extern void printascii(const char *);
104
105static void dbg(const char *fmt, ...)
106{
107 va_list va;
108 char buff[256];
109
110 va_start(va, fmt);
111 vsprintf(buff, fmt, va);
112 va_end(va);
113
114 printascii(buff);
115}
116
117#else
118#define dbg(x...) do { } while (0)
119#endif
diff --git a/drivers/tty/serial/sb1250-duart.c b/drivers/tty/serial/sb1250-duart.c
new file mode 100644
index 000000000000..ea2340b814e9
--- /dev/null
+++ b/drivers/tty/serial/sb1250-duart.c
@@ -0,0 +1,974 @@
1/*
2 * Support for the asynchronous serial interface (DUART) included
3 * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
4 *
5 * Copyright (c) 2007 Maciej W. Rozycki
6 *
7 * Derived from drivers/char/sb1250_duart.c for which the following
8 * copyright applies:
9 *
10 * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * References:
18 *
19 * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
20 */
21
22#if defined(CONFIG_SERIAL_SB1250_DUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
25
26#include <linux/compiler.h>
27#include <linux/console.h>
28#include <linux/delay.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/ioport.h>
33#include <linux/kernel.h>
34#include <linux/major.h>
35#include <linux/serial.h>
36#include <linux/serial_core.h>
37#include <linux/spinlock.h>
38#include <linux/sysrq.h>
39#include <linux/tty.h>
40#include <linux/types.h>
41
42#include <asm/atomic.h>
43#include <asm/io.h>
44#include <asm/war.h>
45
46#include <asm/sibyte/sb1250.h>
47#include <asm/sibyte/sb1250_uart.h>
48#include <asm/sibyte/swarm.h>
49
50
51#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
52#include <asm/sibyte/bcm1480_regs.h>
53#include <asm/sibyte/bcm1480_int.h>
54
55#define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
56#define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
57#define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
58
59#define DUART_CHANREG_SPACING BCM1480_DUART_CHANREG_SPACING
60
61#define R_DUART_IMRREG(line) R_BCM1480_DUART_IMRREG(line)
62#define R_DUART_INCHREG(line) R_BCM1480_DUART_INCHREG(line)
63#define R_DUART_ISRREG(line) R_BCM1480_DUART_ISRREG(line)
64
65#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
66#include <asm/sibyte/sb1250_regs.h>
67#include <asm/sibyte/sb1250_int.h>
68
69#define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
70#define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
71#define SBD_INT(line) (K_INT_UART_0 + (line))
72
73#else
74#error invalid SB1250 UART configuration
75
76#endif
77
78
79MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
80MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
81MODULE_LICENSE("GPL");
82
83
84#define DUART_MAX_CHIP 2
85#define DUART_MAX_SIDE 2
86
87/*
88 * Per-port state.
89 */
90struct sbd_port {
91 struct sbd_duart *duart;
92 struct uart_port port;
93 unsigned char __iomem *memctrl;
94 int tx_stopped;
95 int initialised;
96};
97
98/*
99 * Per-DUART state for the shared register space.
100 */
101struct sbd_duart {
102 struct sbd_port sport[2];
103 unsigned long mapctrl;
104 atomic_t map_guard;
105};
106
107#define to_sport(uport) container_of(uport, struct sbd_port, port)
108
109static struct sbd_duart sbd_duarts[DUART_MAX_CHIP];
110
111
112/*
113 * Reading and writing SB1250 DUART registers.
114 *
115 * There are three register spaces: two per-channel ones and
116 * a shared one. We have to define accessors appropriately.
117 * All registers are 64-bit and all but the Baud Rate Clock
118 * registers only define 8 least significant bits. There is
119 * also a workaround to take into account. Raw accessors use
120 * the full register width, but cooked ones truncate it
121 * intentionally so that the rest of the driver does not care.
122 */
123static u64 __read_sbdchn(struct sbd_port *sport, int reg)
124{
125 void __iomem *csr = sport->port.membase + reg;
126
127 return __raw_readq(csr);
128}
129
130static u64 __read_sbdshr(struct sbd_port *sport, int reg)
131{
132 void __iomem *csr = sport->memctrl + reg;
133
134 return __raw_readq(csr);
135}
136
137static void __write_sbdchn(struct sbd_port *sport, int reg, u64 value)
138{
139 void __iomem *csr = sport->port.membase + reg;
140
141 __raw_writeq(value, csr);
142}
143
144static void __write_sbdshr(struct sbd_port *sport, int reg, u64 value)
145{
146 void __iomem *csr = sport->memctrl + reg;
147
148 __raw_writeq(value, csr);
149}
150
151/*
152 * In bug 1956, we get glitches that can mess up uart registers. This
153 * "read-mode-reg after any register access" is an accepted workaround.
154 */
155static void __war_sbd1956(struct sbd_port *sport)
156{
157 __read_sbdchn(sport, R_DUART_MODE_REG_1);
158 __read_sbdchn(sport, R_DUART_MODE_REG_2);
159}
160
161static unsigned char read_sbdchn(struct sbd_port *sport, int reg)
162{
163 unsigned char retval;
164
165 retval = __read_sbdchn(sport, reg);
166 if (SIBYTE_1956_WAR)
167 __war_sbd1956(sport);
168 return retval;
169}
170
171static unsigned char read_sbdshr(struct sbd_port *sport, int reg)
172{
173 unsigned char retval;
174
175 retval = __read_sbdshr(sport, reg);
176 if (SIBYTE_1956_WAR)
177 __war_sbd1956(sport);
178 return retval;
179}
180
181static void write_sbdchn(struct sbd_port *sport, int reg, unsigned int value)
182{
183 __write_sbdchn(sport, reg, value);
184 if (SIBYTE_1956_WAR)
185 __war_sbd1956(sport);
186}
187
188static void write_sbdshr(struct sbd_port *sport, int reg, unsigned int value)
189{
190 __write_sbdshr(sport, reg, value);
191 if (SIBYTE_1956_WAR)
192 __war_sbd1956(sport);
193}
194
195
196static int sbd_receive_ready(struct sbd_port *sport)
197{
198 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_RX_RDY;
199}
200
201static int sbd_receive_drain(struct sbd_port *sport)
202{
203 int loops = 10000;
204
205 while (sbd_receive_ready(sport) && --loops)
206 read_sbdchn(sport, R_DUART_RX_HOLD);
207 return loops;
208}
209
210static int __maybe_unused sbd_transmit_ready(struct sbd_port *sport)
211{
212 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_RDY;
213}
214
215static int __maybe_unused sbd_transmit_drain(struct sbd_port *sport)
216{
217 int loops = 10000;
218
219 while (!sbd_transmit_ready(sport) && --loops)
220 udelay(2);
221 return loops;
222}
223
224static int sbd_transmit_empty(struct sbd_port *sport)
225{
226 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_EMT;
227}
228
229static int sbd_line_drain(struct sbd_port *sport)
230{
231 int loops = 10000;
232
233 while (!sbd_transmit_empty(sport) && --loops)
234 udelay(2);
235 return loops;
236}
237
238
239static unsigned int sbd_tx_empty(struct uart_port *uport)
240{
241 struct sbd_port *sport = to_sport(uport);
242
243 return sbd_transmit_empty(sport) ? TIOCSER_TEMT : 0;
244}
245
246static unsigned int sbd_get_mctrl(struct uart_port *uport)
247{
248 struct sbd_port *sport = to_sport(uport);
249 unsigned int mctrl, status;
250
251 status = read_sbdshr(sport, R_DUART_IN_PORT);
252 status >>= (uport->line) % 2;
253 mctrl = (!(status & M_DUART_IN_PIN0_VAL) ? TIOCM_CTS : 0) |
254 (!(status & M_DUART_IN_PIN4_VAL) ? TIOCM_CAR : 0) |
255 (!(status & M_DUART_RIN0_PIN) ? TIOCM_RNG : 0) |
256 (!(status & M_DUART_IN_PIN2_VAL) ? TIOCM_DSR : 0);
257 return mctrl;
258}
259
260static void sbd_set_mctrl(struct uart_port *uport, unsigned int mctrl)
261{
262 struct sbd_port *sport = to_sport(uport);
263 unsigned int clr = 0, set = 0, mode2;
264
265 if (mctrl & TIOCM_DTR)
266 set |= M_DUART_SET_OPR2;
267 else
268 clr |= M_DUART_CLR_OPR2;
269 if (mctrl & TIOCM_RTS)
270 set |= M_DUART_SET_OPR0;
271 else
272 clr |= M_DUART_CLR_OPR0;
273 clr <<= (uport->line) % 2;
274 set <<= (uport->line) % 2;
275
276 mode2 = read_sbdchn(sport, R_DUART_MODE_REG_2);
277 mode2 &= ~M_DUART_CHAN_MODE;
278 if (mctrl & TIOCM_LOOP)
279 mode2 |= V_DUART_CHAN_MODE_LCL_LOOP;
280 else
281 mode2 |= V_DUART_CHAN_MODE_NORMAL;
282
283 write_sbdshr(sport, R_DUART_CLEAR_OPR, clr);
284 write_sbdshr(sport, R_DUART_SET_OPR, set);
285 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2);
286}
287
288static void sbd_stop_tx(struct uart_port *uport)
289{
290 struct sbd_port *sport = to_sport(uport);
291
292 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
293 sport->tx_stopped = 1;
294};
295
296static void sbd_start_tx(struct uart_port *uport)
297{
298 struct sbd_port *sport = to_sport(uport);
299 unsigned int mask;
300
301 /* Enable tx interrupts. */
302 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
303 mask |= M_DUART_IMR_TX;
304 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
305
306 /* Go!, go!, go!... */
307 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
308 sport->tx_stopped = 0;
309};
310
311static void sbd_stop_rx(struct uart_port *uport)
312{
313 struct sbd_port *sport = to_sport(uport);
314
315 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
316};
317
318static void sbd_enable_ms(struct uart_port *uport)
319{
320 struct sbd_port *sport = to_sport(uport);
321
322 write_sbdchn(sport, R_DUART_AUXCTL_X,
323 M_DUART_CIN_CHNG_ENA | M_DUART_CTS_CHNG_ENA);
324}
325
326static void sbd_break_ctl(struct uart_port *uport, int break_state)
327{
328 struct sbd_port *sport = to_sport(uport);
329
330 if (break_state == -1)
331 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_START_BREAK);
332 else
333 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_STOP_BREAK);
334}
335
336
337static void sbd_receive_chars(struct sbd_port *sport)
338{
339 struct uart_port *uport = &sport->port;
340 struct uart_icount *icount;
341 unsigned int status, ch, flag;
342 int count;
343
344 for (count = 16; count; count--) {
345 status = read_sbdchn(sport, R_DUART_STATUS);
346 if (!(status & M_DUART_RX_RDY))
347 break;
348
349 ch = read_sbdchn(sport, R_DUART_RX_HOLD);
350
351 flag = TTY_NORMAL;
352
353 icount = &uport->icount;
354 icount->rx++;
355
356 if (unlikely(status &
357 (M_DUART_RCVD_BRK | M_DUART_FRM_ERR |
358 M_DUART_PARITY_ERR | M_DUART_OVRUN_ERR))) {
359 if (status & M_DUART_RCVD_BRK) {
360 icount->brk++;
361 if (uart_handle_break(uport))
362 continue;
363 } else if (status & M_DUART_FRM_ERR)
364 icount->frame++;
365 else if (status & M_DUART_PARITY_ERR)
366 icount->parity++;
367 if (status & M_DUART_OVRUN_ERR)
368 icount->overrun++;
369
370 status &= uport->read_status_mask;
371 if (status & M_DUART_RCVD_BRK)
372 flag = TTY_BREAK;
373 else if (status & M_DUART_FRM_ERR)
374 flag = TTY_FRAME;
375 else if (status & M_DUART_PARITY_ERR)
376 flag = TTY_PARITY;
377 }
378
379 if (uart_handle_sysrq_char(uport, ch))
380 continue;
381
382 uart_insert_char(uport, status, M_DUART_OVRUN_ERR, ch, flag);
383 }
384
385 tty_flip_buffer_push(uport->state->port.tty);
386}
387
388static void sbd_transmit_chars(struct sbd_port *sport)
389{
390 struct uart_port *uport = &sport->port;
391 struct circ_buf *xmit = &sport->port.state->xmit;
392 unsigned int mask;
393 int stop_tx;
394
395 /* XON/XOFF chars. */
396 if (sport->port.x_char) {
397 write_sbdchn(sport, R_DUART_TX_HOLD, sport->port.x_char);
398 sport->port.icount.tx++;
399 sport->port.x_char = 0;
400 return;
401 }
402
403 /* If nothing to do or stopped or hardware stopped. */
404 stop_tx = (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port));
405
406 /* Send char. */
407 if (!stop_tx) {
408 write_sbdchn(sport, R_DUART_TX_HOLD, xmit->buf[xmit->tail]);
409 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
410 sport->port.icount.tx++;
411
412 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
413 uart_write_wakeup(&sport->port);
414 }
415
416 /* Are we are done? */
417 if (stop_tx || uart_circ_empty(xmit)) {
418 /* Disable tx interrupts. */
419 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
420 mask &= ~M_DUART_IMR_TX;
421 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
422 }
423}
424
425static void sbd_status_handle(struct sbd_port *sport)
426{
427 struct uart_port *uport = &sport->port;
428 unsigned int delta;
429
430 delta = read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
431 delta >>= (uport->line) % 2;
432
433 if (delta & (M_DUART_IN_PIN0_VAL << S_DUART_IN_PIN_CHNG))
434 uart_handle_cts_change(uport, !(delta & M_DUART_IN_PIN0_VAL));
435
436 if (delta & (M_DUART_IN_PIN2_VAL << S_DUART_IN_PIN_CHNG))
437 uport->icount.dsr++;
438
439 if (delta & ((M_DUART_IN_PIN2_VAL | M_DUART_IN_PIN0_VAL) <<
440 S_DUART_IN_PIN_CHNG))
441 wake_up_interruptible(&uport->state->port.delta_msr_wait);
442}
443
444static irqreturn_t sbd_interrupt(int irq, void *dev_id)
445{
446 struct sbd_port *sport = dev_id;
447 struct uart_port *uport = &sport->port;
448 irqreturn_t status = IRQ_NONE;
449 unsigned int intstat;
450 int count;
451
452 for (count = 16; count; count--) {
453 intstat = read_sbdshr(sport,
454 R_DUART_ISRREG((uport->line) % 2));
455 intstat &= read_sbdshr(sport,
456 R_DUART_IMRREG((uport->line) % 2));
457 intstat &= M_DUART_ISR_ALL;
458 if (!intstat)
459 break;
460
461 if (intstat & M_DUART_ISR_RX)
462 sbd_receive_chars(sport);
463 if (intstat & M_DUART_ISR_IN)
464 sbd_status_handle(sport);
465 if (intstat & M_DUART_ISR_TX)
466 sbd_transmit_chars(sport);
467
468 status = IRQ_HANDLED;
469 }
470
471 return status;
472}
473
474
475static int sbd_startup(struct uart_port *uport)
476{
477 struct sbd_port *sport = to_sport(uport);
478 unsigned int mode1;
479 int ret;
480
481 ret = request_irq(sport->port.irq, sbd_interrupt,
482 IRQF_SHARED, "sb1250-duart", sport);
483 if (ret)
484 return ret;
485
486 /* Clear the receive FIFO. */
487 sbd_receive_drain(sport);
488
489 /* Clear the interrupt registers. */
490 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT);
491 read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
492
493 /* Set rx/tx interrupt to FIFO available. */
494 mode1 = read_sbdchn(sport, R_DUART_MODE_REG_1);
495 mode1 &= ~(M_DUART_RX_IRQ_SEL_RXFULL | M_DUART_TX_IRQ_SEL_TXEMPT);
496 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1);
497
498 /* Disable tx, enable rx. */
499 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_EN);
500 sport->tx_stopped = 1;
501
502 /* Enable interrupts. */
503 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
504 M_DUART_IMR_IN | M_DUART_IMR_RX);
505
506 return 0;
507}
508
509static void sbd_shutdown(struct uart_port *uport)
510{
511 struct sbd_port *sport = to_sport(uport);
512
513 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
514 sport->tx_stopped = 1;
515 free_irq(sport->port.irq, sport);
516}
517
518
519static void sbd_init_port(struct sbd_port *sport)
520{
521 struct uart_port *uport = &sport->port;
522
523 if (sport->initialised)
524 return;
525
526 /* There is no DUART reset feature, so just set some sane defaults. */
527 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_TX);
528 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_RX);
529 write_sbdchn(sport, R_DUART_MODE_REG_1, V_DUART_BITS_PER_CHAR_8);
530 write_sbdchn(sport, R_DUART_MODE_REG_2, 0);
531 write_sbdchn(sport, R_DUART_FULL_CTL,
532 V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
533 write_sbdchn(sport, R_DUART_OPCR_X, 0);
534 write_sbdchn(sport, R_DUART_AUXCTL_X, 0);
535 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
536
537 sport->initialised = 1;
538}
539
540static void sbd_set_termios(struct uart_port *uport, struct ktermios *termios,
541 struct ktermios *old_termios)
542{
543 struct sbd_port *sport = to_sport(uport);
544 unsigned int mode1 = 0, mode2 = 0, aux = 0;
545 unsigned int mode1mask = 0, mode2mask = 0, auxmask = 0;
546 unsigned int oldmode1, oldmode2, oldaux;
547 unsigned int baud, brg;
548 unsigned int command;
549
550 mode1mask |= ~(M_DUART_PARITY_MODE | M_DUART_PARITY_TYPE_ODD |
551 M_DUART_BITS_PER_CHAR);
552 mode2mask |= ~M_DUART_STOP_BIT_LEN_2;
553 auxmask |= ~M_DUART_CTS_CHNG_ENA;
554
555 /* Byte size. */
556 switch (termios->c_cflag & CSIZE) {
557 case CS5:
558 case CS6:
559 /* Unsupported, leave unchanged. */
560 mode1mask |= M_DUART_PARITY_MODE;
561 break;
562 case CS7:
563 mode1 |= V_DUART_BITS_PER_CHAR_7;
564 break;
565 case CS8:
566 default:
567 mode1 |= V_DUART_BITS_PER_CHAR_8;
568 break;
569 }
570
571 /* Parity and stop bits. */
572 if (termios->c_cflag & CSTOPB)
573 mode2 |= M_DUART_STOP_BIT_LEN_2;
574 else
575 mode2 |= M_DUART_STOP_BIT_LEN_1;
576 if (termios->c_cflag & PARENB)
577 mode1 |= V_DUART_PARITY_MODE_ADD;
578 else
579 mode1 |= V_DUART_PARITY_MODE_NONE;
580 if (termios->c_cflag & PARODD)
581 mode1 |= M_DUART_PARITY_TYPE_ODD;
582 else
583 mode1 |= M_DUART_PARITY_TYPE_EVEN;
584
585 baud = uart_get_baud_rate(uport, termios, old_termios, 1200, 5000000);
586 brg = V_DUART_BAUD_RATE(baud);
587 /* The actual lower bound is 1221bps, so compensate. */
588 if (brg > M_DUART_CLK_COUNTER)
589 brg = M_DUART_CLK_COUNTER;
590
591 uart_update_timeout(uport, termios->c_cflag, baud);
592
593 uport->read_status_mask = M_DUART_OVRUN_ERR;
594 if (termios->c_iflag & INPCK)
595 uport->read_status_mask |= M_DUART_FRM_ERR |
596 M_DUART_PARITY_ERR;
597 if (termios->c_iflag & (BRKINT | PARMRK))
598 uport->read_status_mask |= M_DUART_RCVD_BRK;
599
600 uport->ignore_status_mask = 0;
601 if (termios->c_iflag & IGNPAR)
602 uport->ignore_status_mask |= M_DUART_FRM_ERR |
603 M_DUART_PARITY_ERR;
604 if (termios->c_iflag & IGNBRK) {
605 uport->ignore_status_mask |= M_DUART_RCVD_BRK;
606 if (termios->c_iflag & IGNPAR)
607 uport->ignore_status_mask |= M_DUART_OVRUN_ERR;
608 }
609
610 if (termios->c_cflag & CREAD)
611 command = M_DUART_RX_EN;
612 else
613 command = M_DUART_RX_DIS;
614
615 if (termios->c_cflag & CRTSCTS)
616 aux |= M_DUART_CTS_CHNG_ENA;
617 else
618 aux &= ~M_DUART_CTS_CHNG_ENA;
619
620 spin_lock(&uport->lock);
621
622 if (sport->tx_stopped)
623 command |= M_DUART_TX_DIS;
624 else
625 command |= M_DUART_TX_EN;
626
627 oldmode1 = read_sbdchn(sport, R_DUART_MODE_REG_1) & mode1mask;
628 oldmode2 = read_sbdchn(sport, R_DUART_MODE_REG_2) & mode2mask;
629 oldaux = read_sbdchn(sport, R_DUART_AUXCTL_X) & auxmask;
630
631 if (!sport->tx_stopped)
632 sbd_line_drain(sport);
633 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
634
635 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1 | oldmode1);
636 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2 | oldmode2);
637 write_sbdchn(sport, R_DUART_CLK_SEL, brg);
638 write_sbdchn(sport, R_DUART_AUXCTL_X, aux | oldaux);
639
640 write_sbdchn(sport, R_DUART_CMD, command);
641
642 spin_unlock(&uport->lock);
643}
644
645
646static const char *sbd_type(struct uart_port *uport)
647{
648 return "SB1250 DUART";
649}
650
651static void sbd_release_port(struct uart_port *uport)
652{
653 struct sbd_port *sport = to_sport(uport);
654 struct sbd_duart *duart = sport->duart;
655 int map_guard;
656
657 iounmap(sport->memctrl);
658 sport->memctrl = NULL;
659 iounmap(uport->membase);
660 uport->membase = NULL;
661
662 map_guard = atomic_add_return(-1, &duart->map_guard);
663 if (!map_guard)
664 release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
665 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
666}
667
668static int sbd_map_port(struct uart_port *uport)
669{
670 const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
671 struct sbd_port *sport = to_sport(uport);
672 struct sbd_duart *duart = sport->duart;
673
674 if (!uport->membase)
675 uport->membase = ioremap_nocache(uport->mapbase,
676 DUART_CHANREG_SPACING);
677 if (!uport->membase) {
678 printk(err);
679 return -ENOMEM;
680 }
681
682 if (!sport->memctrl)
683 sport->memctrl = ioremap_nocache(duart->mapctrl,
684 DUART_CHANREG_SPACING);
685 if (!sport->memctrl) {
686 printk(err);
687 iounmap(uport->membase);
688 uport->membase = NULL;
689 return -ENOMEM;
690 }
691
692 return 0;
693}
694
695static int sbd_request_port(struct uart_port *uport)
696{
697 const char *err = KERN_ERR "sbd: Unable to reserve MMIO resource\n";
698 struct sbd_duart *duart = to_sport(uport)->duart;
699 int map_guard;
700 int ret = 0;
701
702 if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
703 "sb1250-duart")) {
704 printk(err);
705 return -EBUSY;
706 }
707 map_guard = atomic_add_return(1, &duart->map_guard);
708 if (map_guard == 1) {
709 if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
710 "sb1250-duart")) {
711 atomic_add(-1, &duart->map_guard);
712 printk(err);
713 ret = -EBUSY;
714 }
715 }
716 if (!ret) {
717 ret = sbd_map_port(uport);
718 if (ret) {
719 map_guard = atomic_add_return(-1, &duart->map_guard);
720 if (!map_guard)
721 release_mem_region(duart->mapctrl,
722 DUART_CHANREG_SPACING);
723 }
724 }
725 if (ret) {
726 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
727 return ret;
728 }
729 return 0;
730}
731
732static void sbd_config_port(struct uart_port *uport, int flags)
733{
734 struct sbd_port *sport = to_sport(uport);
735
736 if (flags & UART_CONFIG_TYPE) {
737 if (sbd_request_port(uport))
738 return;
739
740 uport->type = PORT_SB1250_DUART;
741
742 sbd_init_port(sport);
743 }
744}
745
746static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
747{
748 int ret = 0;
749
750 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
751 ret = -EINVAL;
752 if (ser->irq != uport->irq)
753 ret = -EINVAL;
754 if (ser->baud_base != uport->uartclk / 16)
755 ret = -EINVAL;
756 return ret;
757}
758
759
760static const struct uart_ops sbd_ops = {
761 .tx_empty = sbd_tx_empty,
762 .set_mctrl = sbd_set_mctrl,
763 .get_mctrl = sbd_get_mctrl,
764 .stop_tx = sbd_stop_tx,
765 .start_tx = sbd_start_tx,
766 .stop_rx = sbd_stop_rx,
767 .enable_ms = sbd_enable_ms,
768 .break_ctl = sbd_break_ctl,
769 .startup = sbd_startup,
770 .shutdown = sbd_shutdown,
771 .set_termios = sbd_set_termios,
772 .type = sbd_type,
773 .release_port = sbd_release_port,
774 .request_port = sbd_request_port,
775 .config_port = sbd_config_port,
776 .verify_port = sbd_verify_port,
777};
778
779/* Initialize SB1250 DUART port structures. */
780static void __init sbd_probe_duarts(void)
781{
782 static int probed;
783 int chip, side;
784 int max_lines, line;
785
786 if (probed)
787 return;
788
789 /* Set the number of available units based on the SOC type. */
790 switch (soc_type) {
791 case K_SYS_SOC_TYPE_BCM1x55:
792 case K_SYS_SOC_TYPE_BCM1x80:
793 max_lines = 4;
794 break;
795 default:
796 /* Assume at least two serial ports at the normal address. */
797 max_lines = 2;
798 break;
799 }
800
801 probed = 1;
802
803 for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
804 chip++) {
805 sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
806
807 for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
808 side++, line++) {
809 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
810 struct uart_port *uport = &sport->port;
811
812 sport->duart = &sbd_duarts[chip];
813
814 uport->irq = SBD_INT(line);
815 uport->uartclk = 100000000 / 20 * 16;
816 uport->fifosize = 16;
817 uport->iotype = UPIO_MEM;
818 uport->flags = UPF_BOOT_AUTOCONF;
819 uport->ops = &sbd_ops;
820 uport->line = line;
821 uport->mapbase = SBD_CHANREGS(line);
822 }
823 }
824}
825
826
827#ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
828/*
829 * Serial console stuff. Very basic, polling driver for doing serial
830 * console output. The console_lock is held by the caller, so we
831 * shouldn't be interrupted for more console activity.
832 */
833static void sbd_console_putchar(struct uart_port *uport, int ch)
834{
835 struct sbd_port *sport = to_sport(uport);
836
837 sbd_transmit_drain(sport);
838 write_sbdchn(sport, R_DUART_TX_HOLD, ch);
839}
840
841static void sbd_console_write(struct console *co, const char *s,
842 unsigned int count)
843{
844 int chip = co->index / DUART_MAX_SIDE;
845 int side = co->index % DUART_MAX_SIDE;
846 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
847 struct uart_port *uport = &sport->port;
848 unsigned long flags;
849 unsigned int mask;
850
851 /* Disable transmit interrupts and enable the transmitter. */
852 spin_lock_irqsave(&uport->lock, flags);
853 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
854 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
855 mask & ~M_DUART_IMR_TX);
856 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
857 spin_unlock_irqrestore(&uport->lock, flags);
858
859 uart_console_write(&sport->port, s, count, sbd_console_putchar);
860
861 /* Restore transmit interrupts and the transmitter enable. */
862 spin_lock_irqsave(&uport->lock, flags);
863 sbd_line_drain(sport);
864 if (sport->tx_stopped)
865 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
866 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
867 spin_unlock_irqrestore(&uport->lock, flags);
868}
869
870static int __init sbd_console_setup(struct console *co, char *options)
871{
872 int chip = co->index / DUART_MAX_SIDE;
873 int side = co->index % DUART_MAX_SIDE;
874 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
875 struct uart_port *uport = &sport->port;
876 int baud = 115200;
877 int bits = 8;
878 int parity = 'n';
879 int flow = 'n';
880 int ret;
881
882 if (!sport->duart)
883 return -ENXIO;
884
885 ret = sbd_map_port(uport);
886 if (ret)
887 return ret;
888
889 sbd_init_port(sport);
890
891 if (options)
892 uart_parse_options(options, &baud, &parity, &bits, &flow);
893 return uart_set_options(uport, co, baud, parity, bits, flow);
894}
895
896static struct uart_driver sbd_reg;
897static struct console sbd_console = {
898 .name = "duart",
899 .write = sbd_console_write,
900 .device = uart_console_device,
901 .setup = sbd_console_setup,
902 .flags = CON_PRINTBUFFER,
903 .index = -1,
904 .data = &sbd_reg
905};
906
907static int __init sbd_serial_console_init(void)
908{
909 sbd_probe_duarts();
910 register_console(&sbd_console);
911
912 return 0;
913}
914
915console_initcall(sbd_serial_console_init);
916
917#define SERIAL_SB1250_DUART_CONSOLE &sbd_console
918#else
919#define SERIAL_SB1250_DUART_CONSOLE NULL
920#endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
921
922
923static struct uart_driver sbd_reg = {
924 .owner = THIS_MODULE,
925 .driver_name = "sb1250_duart",
926 .dev_name = "duart",
927 .major = TTY_MAJOR,
928 .minor = SB1250_DUART_MINOR_BASE,
929 .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
930 .cons = SERIAL_SB1250_DUART_CONSOLE,
931};
932
933/* Set up the driver and register it. */
934static int __init sbd_init(void)
935{
936 int i, ret;
937
938 sbd_probe_duarts();
939
940 ret = uart_register_driver(&sbd_reg);
941 if (ret)
942 return ret;
943
944 for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
945 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
946 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
947 struct uart_port *uport = &sport->port;
948
949 if (sport->duart)
950 uart_add_one_port(&sbd_reg, uport);
951 }
952
953 return 0;
954}
955
956/* Unload the driver. Unregister stuff, get ready to go away. */
957static void __exit sbd_exit(void)
958{
959 int i;
960
961 for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
962 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
963 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
964 struct uart_port *uport = &sport->port;
965
966 if (sport->duart)
967 uart_remove_one_port(&sbd_reg, uport);
968 }
969
970 uart_unregister_driver(&sbd_reg);
971}
972
973module_init(sbd_init);
974module_exit(sbd_exit);
diff --git a/drivers/tty/serial/sc26xx.c b/drivers/tty/serial/sc26xx.c
new file mode 100644
index 000000000000..75038ad2b242
--- /dev/null
+++ b/drivers/tty/serial/sc26xx.c
@@ -0,0 +1,757 @@
1/*
2 * SC268xx.c: Serial driver for Philiphs SC2681/SC2692 devices.
3 *
4 * Copyright (C) 2006,2007 Thomas Bogendörfer (tsbogend@alpha.franken.de)
5 */
6
7#include <linux/module.h>
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/tty.h>
11#include <linux/tty_flip.h>
12#include <linux/major.h>
13#include <linux/circ_buf.h>
14#include <linux/serial.h>
15#include <linux/sysrq.h>
16#include <linux/console.h>
17#include <linux/spinlock.h>
18#include <linux/slab.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/irq.h>
23
24#if defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/serial_core.h>
29
30#define SC26XX_MAJOR 204
31#define SC26XX_MINOR_START 205
32#define SC26XX_NR 2
33
34struct uart_sc26xx_port {
35 struct uart_port port[2];
36 u8 dsr_mask[2];
37 u8 cts_mask[2];
38 u8 dcd_mask[2];
39 u8 ri_mask[2];
40 u8 dtr_mask[2];
41 u8 rts_mask[2];
42 u8 imr;
43};
44
45/* register common to both ports */
46#define RD_ISR 0x14
47#define RD_IPR 0x34
48
49#define WR_ACR 0x10
50#define WR_IMR 0x14
51#define WR_OPCR 0x34
52#define WR_OPR_SET 0x38
53#define WR_OPR_CLR 0x3C
54
55/* access common register */
56#define READ_SC(p, r) readb((p)->membase + RD_##r)
57#define WRITE_SC(p, r, v) writeb((v), (p)->membase + WR_##r)
58
59/* register per port */
60#define RD_PORT_MRx 0x00
61#define RD_PORT_SR 0x04
62#define RD_PORT_RHR 0x0c
63
64#define WR_PORT_MRx 0x00
65#define WR_PORT_CSR 0x04
66#define WR_PORT_CR 0x08
67#define WR_PORT_THR 0x0c
68
69/* SR bits */
70#define SR_BREAK (1 << 7)
71#define SR_FRAME (1 << 6)
72#define SR_PARITY (1 << 5)
73#define SR_OVERRUN (1 << 4)
74#define SR_TXRDY (1 << 2)
75#define SR_RXRDY (1 << 0)
76
77#define CR_RES_MR (1 << 4)
78#define CR_RES_RX (2 << 4)
79#define CR_RES_TX (3 << 4)
80#define CR_STRT_BRK (6 << 4)
81#define CR_STOP_BRK (7 << 4)
82#define CR_DIS_TX (1 << 3)
83#define CR_ENA_TX (1 << 2)
84#define CR_DIS_RX (1 << 1)
85#define CR_ENA_RX (1 << 0)
86
87/* ISR bits */
88#define ISR_RXRDYB (1 << 5)
89#define ISR_TXRDYB (1 << 4)
90#define ISR_RXRDYA (1 << 1)
91#define ISR_TXRDYA (1 << 0)
92
93/* IMR bits */
94#define IMR_RXRDY (1 << 1)
95#define IMR_TXRDY (1 << 0)
96
97/* access port register */
98static inline u8 read_sc_port(struct uart_port *p, u8 reg)
99{
100 return readb(p->membase + p->line * 0x20 + reg);
101}
102
103static inline void write_sc_port(struct uart_port *p, u8 reg, u8 val)
104{
105 writeb(val, p->membase + p->line * 0x20 + reg);
106}
107
108#define READ_SC_PORT(p, r) read_sc_port(p, RD_PORT_##r)
109#define WRITE_SC_PORT(p, r, v) write_sc_port(p, WR_PORT_##r, v)
110
111static void sc26xx_enable_irq(struct uart_port *port, int mask)
112{
113 struct uart_sc26xx_port *up;
114 int line = port->line;
115
116 port -= line;
117 up = container_of(port, struct uart_sc26xx_port, port[0]);
118
119 up->imr |= mask << (line * 4);
120 WRITE_SC(port, IMR, up->imr);
121}
122
123static void sc26xx_disable_irq(struct uart_port *port, int mask)
124{
125 struct uart_sc26xx_port *up;
126 int line = port->line;
127
128 port -= line;
129 up = container_of(port, struct uart_sc26xx_port, port[0]);
130
131 up->imr &= ~(mask << (line * 4));
132 WRITE_SC(port, IMR, up->imr);
133}
134
135static struct tty_struct *receive_chars(struct uart_port *port)
136{
137 struct tty_struct *tty = NULL;
138 int limit = 10000;
139 unsigned char ch;
140 char flag;
141 u8 status;
142
143 if (port->state != NULL) /* Unopened serial console */
144 tty = port->state->port.tty;
145
146 while (limit-- > 0) {
147 status = READ_SC_PORT(port, SR);
148 if (!(status & SR_RXRDY))
149 break;
150 ch = READ_SC_PORT(port, RHR);
151
152 flag = TTY_NORMAL;
153 port->icount.rx++;
154
155 if (unlikely(status & (SR_BREAK | SR_FRAME |
156 SR_PARITY | SR_OVERRUN))) {
157 if (status & SR_BREAK) {
158 status &= ~(SR_PARITY | SR_FRAME);
159 port->icount.brk++;
160 if (uart_handle_break(port))
161 continue;
162 } else if (status & SR_PARITY)
163 port->icount.parity++;
164 else if (status & SR_FRAME)
165 port->icount.frame++;
166 if (status & SR_OVERRUN)
167 port->icount.overrun++;
168
169 status &= port->read_status_mask;
170 if (status & SR_BREAK)
171 flag = TTY_BREAK;
172 else if (status & SR_PARITY)
173 flag = TTY_PARITY;
174 else if (status & SR_FRAME)
175 flag = TTY_FRAME;
176 }
177
178 if (uart_handle_sysrq_char(port, ch))
179 continue;
180
181 if (status & port->ignore_status_mask)
182 continue;
183
184 tty_insert_flip_char(tty, ch, flag);
185 }
186 return tty;
187}
188
189static void transmit_chars(struct uart_port *port)
190{
191 struct circ_buf *xmit;
192
193 if (!port->state)
194 return;
195
196 xmit = &port->state->xmit;
197 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
198 sc26xx_disable_irq(port, IMR_TXRDY);
199 return;
200 }
201 while (!uart_circ_empty(xmit)) {
202 if (!(READ_SC_PORT(port, SR) & SR_TXRDY))
203 break;
204
205 WRITE_SC_PORT(port, THR, xmit->buf[xmit->tail]);
206 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
207 port->icount.tx++;
208 }
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
211}
212
213static irqreturn_t sc26xx_interrupt(int irq, void *dev_id)
214{
215 struct uart_sc26xx_port *up = dev_id;
216 struct tty_struct *tty;
217 unsigned long flags;
218 u8 isr;
219
220 spin_lock_irqsave(&up->port[0].lock, flags);
221
222 tty = NULL;
223 isr = READ_SC(&up->port[0], ISR);
224 if (isr & ISR_TXRDYA)
225 transmit_chars(&up->port[0]);
226 if (isr & ISR_RXRDYA)
227 tty = receive_chars(&up->port[0]);
228
229 spin_unlock(&up->port[0].lock);
230
231 if (tty)
232 tty_flip_buffer_push(tty);
233
234 spin_lock(&up->port[1].lock);
235
236 tty = NULL;
237 if (isr & ISR_TXRDYB)
238 transmit_chars(&up->port[1]);
239 if (isr & ISR_RXRDYB)
240 tty = receive_chars(&up->port[1]);
241
242 spin_unlock_irqrestore(&up->port[1].lock, flags);
243
244 if (tty)
245 tty_flip_buffer_push(tty);
246
247 return IRQ_HANDLED;
248}
249
250/* port->lock is not held. */
251static unsigned int sc26xx_tx_empty(struct uart_port *port)
252{
253 return (READ_SC_PORT(port, SR) & SR_TXRDY) ? TIOCSER_TEMT : 0;
254}
255
256/* port->lock held by caller. */
257static void sc26xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
258{
259 struct uart_sc26xx_port *up;
260 int line = port->line;
261
262 port -= line;
263 up = container_of(port, struct uart_sc26xx_port, port[0]);
264
265 if (up->dtr_mask[line]) {
266 if (mctrl & TIOCM_DTR)
267 WRITE_SC(port, OPR_SET, up->dtr_mask[line]);
268 else
269 WRITE_SC(port, OPR_CLR, up->dtr_mask[line]);
270 }
271 if (up->rts_mask[line]) {
272 if (mctrl & TIOCM_RTS)
273 WRITE_SC(port, OPR_SET, up->rts_mask[line]);
274 else
275 WRITE_SC(port, OPR_CLR, up->rts_mask[line]);
276 }
277}
278
279/* port->lock is held by caller and interrupts are disabled. */
280static unsigned int sc26xx_get_mctrl(struct uart_port *port)
281{
282 struct uart_sc26xx_port *up;
283 int line = port->line;
284 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
285 u8 ipr;
286
287 port -= line;
288 up = container_of(port, struct uart_sc26xx_port, port[0]);
289 ipr = READ_SC(port, IPR) ^ 0xff;
290
291 if (up->dsr_mask[line]) {
292 mctrl &= ~TIOCM_DSR;
293 mctrl |= ipr & up->dsr_mask[line] ? TIOCM_DSR : 0;
294 }
295 if (up->cts_mask[line]) {
296 mctrl &= ~TIOCM_CTS;
297 mctrl |= ipr & up->cts_mask[line] ? TIOCM_CTS : 0;
298 }
299 if (up->dcd_mask[line]) {
300 mctrl &= ~TIOCM_CAR;
301 mctrl |= ipr & up->dcd_mask[line] ? TIOCM_CAR : 0;
302 }
303 if (up->ri_mask[line]) {
304 mctrl &= ~TIOCM_RNG;
305 mctrl |= ipr & up->ri_mask[line] ? TIOCM_RNG : 0;
306 }
307 return mctrl;
308}
309
310/* port->lock held by caller. */
311static void sc26xx_stop_tx(struct uart_port *port)
312{
313 return;
314}
315
316/* port->lock held by caller. */
317static void sc26xx_start_tx(struct uart_port *port)
318{
319 struct circ_buf *xmit = &port->state->xmit;
320
321 while (!uart_circ_empty(xmit)) {
322 if (!(READ_SC_PORT(port, SR) & SR_TXRDY)) {
323 sc26xx_enable_irq(port, IMR_TXRDY);
324 break;
325 }
326 WRITE_SC_PORT(port, THR, xmit->buf[xmit->tail]);
327 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
328 port->icount.tx++;
329 }
330}
331
332/* port->lock held by caller. */
333static void sc26xx_stop_rx(struct uart_port *port)
334{
335}
336
337/* port->lock held by caller. */
338static void sc26xx_enable_ms(struct uart_port *port)
339{
340}
341
342/* port->lock is not held. */
343static void sc26xx_break_ctl(struct uart_port *port, int break_state)
344{
345 if (break_state == -1)
346 WRITE_SC_PORT(port, CR, CR_STRT_BRK);
347 else
348 WRITE_SC_PORT(port, CR, CR_STOP_BRK);
349}
350
351/* port->lock is not held. */
352static int sc26xx_startup(struct uart_port *port)
353{
354 sc26xx_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
355 WRITE_SC(port, OPCR, 0);
356
357 /* reset tx and rx */
358 WRITE_SC_PORT(port, CR, CR_RES_RX);
359 WRITE_SC_PORT(port, CR, CR_RES_TX);
360
361 /* start rx/tx */
362 WRITE_SC_PORT(port, CR, CR_ENA_TX | CR_ENA_RX);
363
364 /* enable irqs */
365 sc26xx_enable_irq(port, IMR_RXRDY);
366 return 0;
367}
368
369/* port->lock is not held. */
370static void sc26xx_shutdown(struct uart_port *port)
371{
372 /* disable interrupst */
373 sc26xx_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
374
375 /* stop tx/rx */
376 WRITE_SC_PORT(port, CR, CR_DIS_TX | CR_DIS_RX);
377}
378
379/* port->lock is not held. */
380static void sc26xx_set_termios(struct uart_port *port, struct ktermios *termios,
381 struct ktermios *old)
382{
383 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
384 unsigned int quot = uart_get_divisor(port, baud);
385 unsigned int iflag, cflag;
386 unsigned long flags;
387 u8 mr1, mr2, csr;
388
389 spin_lock_irqsave(&port->lock, flags);
390
391 while ((READ_SC_PORT(port, SR) & ((1 << 3) | (1 << 2))) != 0xc)
392 udelay(2);
393
394 WRITE_SC_PORT(port, CR, CR_DIS_TX | CR_DIS_RX);
395
396 iflag = termios->c_iflag;
397 cflag = termios->c_cflag;
398
399 port->read_status_mask = SR_OVERRUN;
400 if (iflag & INPCK)
401 port->read_status_mask |= SR_PARITY | SR_FRAME;
402 if (iflag & (BRKINT | PARMRK))
403 port->read_status_mask |= SR_BREAK;
404
405 port->ignore_status_mask = 0;
406 if (iflag & IGNBRK)
407 port->ignore_status_mask |= SR_BREAK;
408 if ((cflag & CREAD) == 0)
409 port->ignore_status_mask |= SR_BREAK | SR_FRAME |
410 SR_PARITY | SR_OVERRUN;
411
412 switch (cflag & CSIZE) {
413 case CS5:
414 mr1 = 0x00;
415 break;
416 case CS6:
417 mr1 = 0x01;
418 break;
419 case CS7:
420 mr1 = 0x02;
421 break;
422 default:
423 case CS8:
424 mr1 = 0x03;
425 break;
426 }
427 mr2 = 0x07;
428 if (cflag & CSTOPB)
429 mr2 = 0x0f;
430 if (cflag & PARENB) {
431 if (cflag & PARODD)
432 mr1 |= (1 << 2);
433 } else
434 mr1 |= (2 << 3);
435
436 switch (baud) {
437 case 50:
438 csr = 0x00;
439 break;
440 case 110:
441 csr = 0x11;
442 break;
443 case 134:
444 csr = 0x22;
445 break;
446 case 200:
447 csr = 0x33;
448 break;
449 case 300:
450 csr = 0x44;
451 break;
452 case 600:
453 csr = 0x55;
454 break;
455 case 1200:
456 csr = 0x66;
457 break;
458 case 2400:
459 csr = 0x88;
460 break;
461 case 4800:
462 csr = 0x99;
463 break;
464 default:
465 case 9600:
466 csr = 0xbb;
467 break;
468 case 19200:
469 csr = 0xcc;
470 break;
471 }
472
473 WRITE_SC_PORT(port, CR, CR_RES_MR);
474 WRITE_SC_PORT(port, MRx, mr1);
475 WRITE_SC_PORT(port, MRx, mr2);
476
477 WRITE_SC(port, ACR, 0x80);
478 WRITE_SC_PORT(port, CSR, csr);
479
480 /* reset tx and rx */
481 WRITE_SC_PORT(port, CR, CR_RES_RX);
482 WRITE_SC_PORT(port, CR, CR_RES_TX);
483
484 WRITE_SC_PORT(port, CR, CR_ENA_TX | CR_ENA_RX);
485 while ((READ_SC_PORT(port, SR) & ((1 << 3) | (1 << 2))) != 0xc)
486 udelay(2);
487
488 /* XXX */
489 uart_update_timeout(port, cflag,
490 (port->uartclk / (16 * quot)));
491
492 spin_unlock_irqrestore(&port->lock, flags);
493}
494
495static const char *sc26xx_type(struct uart_port *port)
496{
497 return "SC26XX";
498}
499
500static void sc26xx_release_port(struct uart_port *port)
501{
502}
503
504static int sc26xx_request_port(struct uart_port *port)
505{
506 return 0;
507}
508
509static void sc26xx_config_port(struct uart_port *port, int flags)
510{
511}
512
513static int sc26xx_verify_port(struct uart_port *port, struct serial_struct *ser)
514{
515 return -EINVAL;
516}
517
518static struct uart_ops sc26xx_ops = {
519 .tx_empty = sc26xx_tx_empty,
520 .set_mctrl = sc26xx_set_mctrl,
521 .get_mctrl = sc26xx_get_mctrl,
522 .stop_tx = sc26xx_stop_tx,
523 .start_tx = sc26xx_start_tx,
524 .stop_rx = sc26xx_stop_rx,
525 .enable_ms = sc26xx_enable_ms,
526 .break_ctl = sc26xx_break_ctl,
527 .startup = sc26xx_startup,
528 .shutdown = sc26xx_shutdown,
529 .set_termios = sc26xx_set_termios,
530 .type = sc26xx_type,
531 .release_port = sc26xx_release_port,
532 .request_port = sc26xx_request_port,
533 .config_port = sc26xx_config_port,
534 .verify_port = sc26xx_verify_port,
535};
536
537static struct uart_port *sc26xx_port;
538
539#ifdef CONFIG_SERIAL_SC26XX_CONSOLE
540static void sc26xx_console_putchar(struct uart_port *port, char c)
541{
542 unsigned long flags;
543 int limit = 1000000;
544
545 spin_lock_irqsave(&port->lock, flags);
546
547 while (limit-- > 0) {
548 if (READ_SC_PORT(port, SR) & SR_TXRDY) {
549 WRITE_SC_PORT(port, THR, c);
550 break;
551 }
552 udelay(2);
553 }
554
555 spin_unlock_irqrestore(&port->lock, flags);
556}
557
558static void sc26xx_console_write(struct console *con, const char *s, unsigned n)
559{
560 struct uart_port *port = sc26xx_port;
561 int i;
562
563 for (i = 0; i < n; i++) {
564 if (*s == '\n')
565 sc26xx_console_putchar(port, '\r');
566 sc26xx_console_putchar(port, *s++);
567 }
568}
569
570static int __init sc26xx_console_setup(struct console *con, char *options)
571{
572 struct uart_port *port = sc26xx_port;
573 int baud = 9600;
574 int bits = 8;
575 int parity = 'n';
576 int flow = 'n';
577
578 if (port->type != PORT_SC26XX)
579 return -1;
580
581 printk(KERN_INFO "Console: ttySC%d (SC26XX)\n", con->index);
582 if (options)
583 uart_parse_options(options, &baud, &parity, &bits, &flow);
584
585 return uart_set_options(port, con, baud, parity, bits, flow);
586}
587
588static struct uart_driver sc26xx_reg;
589static struct console sc26xx_console = {
590 .name = "ttySC",
591 .write = sc26xx_console_write,
592 .device = uart_console_device,
593 .setup = sc26xx_console_setup,
594 .flags = CON_PRINTBUFFER,
595 .index = -1,
596 .data = &sc26xx_reg,
597};
598#define SC26XX_CONSOLE &sc26xx_console
599#else
600#define SC26XX_CONSOLE NULL
601#endif
602
603static struct uart_driver sc26xx_reg = {
604 .owner = THIS_MODULE,
605 .driver_name = "SC26xx",
606 .dev_name = "ttySC",
607 .major = SC26XX_MAJOR,
608 .minor = SC26XX_MINOR_START,
609 .nr = SC26XX_NR,
610 .cons = SC26XX_CONSOLE,
611};
612
613static u8 sc26xx_flags2mask(unsigned int flags, unsigned int bitpos)
614{
615 unsigned int bit = (flags >> bitpos) & 15;
616
617 return bit ? (1 << (bit - 1)) : 0;
618}
619
620static void __devinit sc26xx_init_masks(struct uart_sc26xx_port *up,
621 int line, unsigned int data)
622{
623 up->dtr_mask[line] = sc26xx_flags2mask(data, 0);
624 up->rts_mask[line] = sc26xx_flags2mask(data, 4);
625 up->dsr_mask[line] = sc26xx_flags2mask(data, 8);
626 up->cts_mask[line] = sc26xx_flags2mask(data, 12);
627 up->dcd_mask[line] = sc26xx_flags2mask(data, 16);
628 up->ri_mask[line] = sc26xx_flags2mask(data, 20);
629}
630
631static int __devinit sc26xx_probe(struct platform_device *dev)
632{
633 struct resource *res;
634 struct uart_sc26xx_port *up;
635 unsigned int *sc26xx_data = dev->dev.platform_data;
636 int err;
637
638 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
639 if (!res)
640 return -ENODEV;
641
642 up = kzalloc(sizeof *up, GFP_KERNEL);
643 if (unlikely(!up))
644 return -ENOMEM;
645
646 up->port[0].line = 0;
647 up->port[0].ops = &sc26xx_ops;
648 up->port[0].type = PORT_SC26XX;
649 up->port[0].uartclk = (29491200 / 16); /* arbitrary */
650
651 up->port[0].mapbase = res->start;
652 up->port[0].membase = ioremap_nocache(up->port[0].mapbase, 0x40);
653 up->port[0].iotype = UPIO_MEM;
654 up->port[0].irq = platform_get_irq(dev, 0);
655
656 up->port[0].dev = &dev->dev;
657
658 sc26xx_init_masks(up, 0, sc26xx_data[0]);
659
660 sc26xx_port = &up->port[0];
661
662 up->port[1].line = 1;
663 up->port[1].ops = &sc26xx_ops;
664 up->port[1].type = PORT_SC26XX;
665 up->port[1].uartclk = (29491200 / 16); /* arbitrary */
666
667 up->port[1].mapbase = up->port[0].mapbase;
668 up->port[1].membase = up->port[0].membase;
669 up->port[1].iotype = UPIO_MEM;
670 up->port[1].irq = up->port[0].irq;
671
672 up->port[1].dev = &dev->dev;
673
674 sc26xx_init_masks(up, 1, sc26xx_data[1]);
675
676 err = uart_register_driver(&sc26xx_reg);
677 if (err)
678 goto out_free_port;
679
680 sc26xx_reg.tty_driver->name_base = sc26xx_reg.minor;
681
682 err = uart_add_one_port(&sc26xx_reg, &up->port[0]);
683 if (err)
684 goto out_unregister_driver;
685
686 err = uart_add_one_port(&sc26xx_reg, &up->port[1]);
687 if (err)
688 goto out_remove_port0;
689
690 err = request_irq(up->port[0].irq, sc26xx_interrupt, 0, "sc26xx", up);
691 if (err)
692 goto out_remove_ports;
693
694 dev_set_drvdata(&dev->dev, up);
695 return 0;
696
697out_remove_ports:
698 uart_remove_one_port(&sc26xx_reg, &up->port[1]);
699out_remove_port0:
700 uart_remove_one_port(&sc26xx_reg, &up->port[0]);
701
702out_unregister_driver:
703 uart_unregister_driver(&sc26xx_reg);
704
705out_free_port:
706 kfree(up);
707 sc26xx_port = NULL;
708 return err;
709}
710
711
712static int __exit sc26xx_driver_remove(struct platform_device *dev)
713{
714 struct uart_sc26xx_port *up = dev_get_drvdata(&dev->dev);
715
716 free_irq(up->port[0].irq, up);
717
718 uart_remove_one_port(&sc26xx_reg, &up->port[0]);
719 uart_remove_one_port(&sc26xx_reg, &up->port[1]);
720
721 uart_unregister_driver(&sc26xx_reg);
722
723 kfree(up);
724 sc26xx_port = NULL;
725
726 dev_set_drvdata(&dev->dev, NULL);
727 return 0;
728}
729
730static struct platform_driver sc26xx_driver = {
731 .probe = sc26xx_probe,
732 .remove = __devexit_p(sc26xx_driver_remove),
733 .driver = {
734 .name = "SC26xx",
735 .owner = THIS_MODULE,
736 },
737};
738
739static int __init sc26xx_init(void)
740{
741 return platform_driver_register(&sc26xx_driver);
742}
743
744static void __exit sc26xx_exit(void)
745{
746 platform_driver_unregister(&sc26xx_driver);
747}
748
749module_init(sc26xx_init);
750module_exit(sc26xx_exit);
751
752
753MODULE_AUTHOR("Thomas Bogendörfer");
754MODULE_DESCRIPTION("SC681/SC2692 serial driver");
755MODULE_VERSION("1.0");
756MODULE_LICENSE("GPL");
757MODULE_ALIAS("platform:SC26xx");
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
new file mode 100644
index 000000000000..db7912cb7ae0
--- /dev/null
+++ b/drivers/tty/serial/serial_core.c
@@ -0,0 +1,2504 @@
1/*
2 * Driver core for serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/module.h>
24#include <linux/tty.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/proc_fs.h>
29#include <linux/seq_file.h>
30#include <linux/device.h>
31#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
32#include <linux/serial_core.h>
33#include <linux/delay.h>
34#include <linux/mutex.h>
35
36#include <asm/irq.h>
37#include <asm/uaccess.h>
38
39/*
40 * This is used to lock changes in serial line configuration.
41 */
42static DEFINE_MUTEX(port_mutex);
43
44/*
45 * lockdep: port->lock is initialized in two places, but we
46 * want only one lock-class:
47 */
48static struct lock_class_key port_lock_key;
49
50#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
51
52#ifdef CONFIG_SERIAL_CORE_CONSOLE
53#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
54#else
55#define uart_console(port) (0)
56#endif
57
58static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
59 struct ktermios *old_termios);
60static void __uart_wait_until_sent(struct uart_port *port, int timeout);
61static void uart_change_pm(struct uart_state *state, int pm_state);
62
63/*
64 * This routine is used by the interrupt handler to schedule processing in
65 * the software interrupt portion of the driver.
66 */
67void uart_write_wakeup(struct uart_port *port)
68{
69 struct uart_state *state = port->state;
70 /*
71 * This means you called this function _after_ the port was
72 * closed. No cookie for you.
73 */
74 BUG_ON(!state);
75 tasklet_schedule(&state->tlet);
76}
77
78static void uart_stop(struct tty_struct *tty)
79{
80 struct uart_state *state = tty->driver_data;
81 struct uart_port *port = state->uart_port;
82 unsigned long flags;
83
84 spin_lock_irqsave(&port->lock, flags);
85 port->ops->stop_tx(port);
86 spin_unlock_irqrestore(&port->lock, flags);
87}
88
89static void __uart_start(struct tty_struct *tty)
90{
91 struct uart_state *state = tty->driver_data;
92 struct uart_port *port = state->uart_port;
93
94 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
95 !tty->stopped && !tty->hw_stopped)
96 port->ops->start_tx(port);
97}
98
99static void uart_start(struct tty_struct *tty)
100{
101 struct uart_state *state = tty->driver_data;
102 struct uart_port *port = state->uart_port;
103 unsigned long flags;
104
105 spin_lock_irqsave(&port->lock, flags);
106 __uart_start(tty);
107 spin_unlock_irqrestore(&port->lock, flags);
108}
109
110static void uart_tasklet_action(unsigned long data)
111{
112 struct uart_state *state = (struct uart_state *)data;
113 tty_wakeup(state->port.tty);
114}
115
116static inline void
117uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
118{
119 unsigned long flags;
120 unsigned int old;
121
122 spin_lock_irqsave(&port->lock, flags);
123 old = port->mctrl;
124 port->mctrl = (old & ~clear) | set;
125 if (old != port->mctrl)
126 port->ops->set_mctrl(port, port->mctrl);
127 spin_unlock_irqrestore(&port->lock, flags);
128}
129
130#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
131#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
132
133/*
134 * Startup the port. This will be called once per open. All calls
135 * will be serialised by the per-port mutex.
136 */
137static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
138{
139 struct uart_port *uport = state->uart_port;
140 struct tty_port *port = &state->port;
141 unsigned long page;
142 int retval = 0;
143
144 if (port->flags & ASYNC_INITIALIZED)
145 return 0;
146
147 /*
148 * Set the TTY IO error marker - we will only clear this
149 * once we have successfully opened the port. Also set
150 * up the tty->alt_speed kludge
151 */
152 set_bit(TTY_IO_ERROR, &tty->flags);
153
154 if (uport->type == PORT_UNKNOWN)
155 return 0;
156
157 /*
158 * Initialise and allocate the transmit and temporary
159 * buffer.
160 */
161 if (!state->xmit.buf) {
162 /* This is protected by the per port mutex */
163 page = get_zeroed_page(GFP_KERNEL);
164 if (!page)
165 return -ENOMEM;
166
167 state->xmit.buf = (unsigned char *) page;
168 uart_circ_clear(&state->xmit);
169 }
170
171 retval = uport->ops->startup(uport);
172 if (retval == 0) {
173 if (uart_console(uport) && uport->cons->cflag) {
174 tty->termios->c_cflag = uport->cons->cflag;
175 uport->cons->cflag = 0;
176 }
177 /*
178 * Initialise the hardware port settings.
179 */
180 uart_change_speed(tty, state, NULL);
181
182 if (init_hw) {
183 /*
184 * Setup the RTS and DTR signals once the
185 * port is open and ready to respond.
186 */
187 if (tty->termios->c_cflag & CBAUD)
188 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
189 }
190
191 if (port->flags & ASYNC_CTS_FLOW) {
192 spin_lock_irq(&uport->lock);
193 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
194 tty->hw_stopped = 1;
195 spin_unlock_irq(&uport->lock);
196 }
197
198 set_bit(ASYNCB_INITIALIZED, &port->flags);
199
200 clear_bit(TTY_IO_ERROR, &tty->flags);
201 }
202
203 if (retval && capable(CAP_SYS_ADMIN))
204 retval = 0;
205
206 return retval;
207}
208
209/*
210 * This routine will shutdown a serial port; interrupts are disabled, and
211 * DTR is dropped if the hangup on close termio flag is on. Calls to
212 * uart_shutdown are serialised by the per-port semaphore.
213 */
214static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
215{
216 struct uart_port *uport = state->uart_port;
217 struct tty_port *port = &state->port;
218
219 /*
220 * Set the TTY IO error marker
221 */
222 if (tty)
223 set_bit(TTY_IO_ERROR, &tty->flags);
224
225 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
226 /*
227 * Turn off DTR and RTS early.
228 */
229 if (!tty || (tty->termios->c_cflag & HUPCL))
230 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
231
232 /*
233 * clear delta_msr_wait queue to avoid mem leaks: we may free
234 * the irq here so the queue might never be woken up. Note
235 * that we won't end up waiting on delta_msr_wait again since
236 * any outstanding file descriptors should be pointing at
237 * hung_up_tty_fops now.
238 */
239 wake_up_interruptible(&port->delta_msr_wait);
240
241 /*
242 * Free the IRQ and disable the port.
243 */
244 uport->ops->shutdown(uport);
245
246 /*
247 * Ensure that the IRQ handler isn't running on another CPU.
248 */
249 synchronize_irq(uport->irq);
250 }
251
252 /*
253 * kill off our tasklet
254 */
255 tasklet_kill(&state->tlet);
256
257 /*
258 * Free the transmit buffer page.
259 */
260 if (state->xmit.buf) {
261 free_page((unsigned long)state->xmit.buf);
262 state->xmit.buf = NULL;
263 }
264}
265
266/**
267 * uart_update_timeout - update per-port FIFO timeout.
268 * @port: uart_port structure describing the port
269 * @cflag: termios cflag value
270 * @baud: speed of the port
271 *
272 * Set the port FIFO timeout value. The @cflag value should
273 * reflect the actual hardware settings.
274 */
275void
276uart_update_timeout(struct uart_port *port, unsigned int cflag,
277 unsigned int baud)
278{
279 unsigned int bits;
280
281 /* byte size and parity */
282 switch (cflag & CSIZE) {
283 case CS5:
284 bits = 7;
285 break;
286 case CS6:
287 bits = 8;
288 break;
289 case CS7:
290 bits = 9;
291 break;
292 default:
293 bits = 10;
294 break; /* CS8 */
295 }
296
297 if (cflag & CSTOPB)
298 bits++;
299 if (cflag & PARENB)
300 bits++;
301
302 /*
303 * The total number of bits to be transmitted in the fifo.
304 */
305 bits = bits * port->fifosize;
306
307 /*
308 * Figure the timeout to send the above number of bits.
309 * Add .02 seconds of slop
310 */
311 port->timeout = (HZ * bits) / baud + HZ/50;
312}
313
314EXPORT_SYMBOL(uart_update_timeout);
315
316/**
317 * uart_get_baud_rate - return baud rate for a particular port
318 * @port: uart_port structure describing the port in question.
319 * @termios: desired termios settings.
320 * @old: old termios (or NULL)
321 * @min: minimum acceptable baud rate
322 * @max: maximum acceptable baud rate
323 *
324 * Decode the termios structure into a numeric baud rate,
325 * taking account of the magic 38400 baud rate (with spd_*
326 * flags), and mapping the %B0 rate to 9600 baud.
327 *
328 * If the new baud rate is invalid, try the old termios setting.
329 * If it's still invalid, we try 9600 baud.
330 *
331 * Update the @termios structure to reflect the baud rate
332 * we're actually going to be using. Don't do this for the case
333 * where B0 is requested ("hang up").
334 */
335unsigned int
336uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
337 struct ktermios *old, unsigned int min, unsigned int max)
338{
339 unsigned int try, baud, altbaud = 38400;
340 int hung_up = 0;
341 upf_t flags = port->flags & UPF_SPD_MASK;
342
343 if (flags == UPF_SPD_HI)
344 altbaud = 57600;
345 else if (flags == UPF_SPD_VHI)
346 altbaud = 115200;
347 else if (flags == UPF_SPD_SHI)
348 altbaud = 230400;
349 else if (flags == UPF_SPD_WARP)
350 altbaud = 460800;
351
352 for (try = 0; try < 2; try++) {
353 baud = tty_termios_baud_rate(termios);
354
355 /*
356 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Die! Die! Die!
358 */
359 if (baud == 38400)
360 baud = altbaud;
361
362 /*
363 * Special case: B0 rate.
364 */
365 if (baud == 0) {
366 hung_up = 1;
367 baud = 9600;
368 }
369
370 if (baud >= min && baud <= max)
371 return baud;
372
373 /*
374 * Oops, the quotient was zero. Try again with
375 * the old baud rate if possible.
376 */
377 termios->c_cflag &= ~CBAUD;
378 if (old) {
379 baud = tty_termios_baud_rate(old);
380 if (!hung_up)
381 tty_termios_encode_baud_rate(termios,
382 baud, baud);
383 old = NULL;
384 continue;
385 }
386
387 /*
388 * As a last resort, if the range cannot be met then clip to
389 * the nearest chip supported rate.
390 */
391 if (!hung_up) {
392 if (baud <= min)
393 tty_termios_encode_baud_rate(termios,
394 min + 1, min + 1);
395 else
396 tty_termios_encode_baud_rate(termios,
397 max - 1, max - 1);
398 }
399 }
400 /* Should never happen */
401 WARN_ON(1);
402 return 0;
403}
404
405EXPORT_SYMBOL(uart_get_baud_rate);
406
407/**
408 * uart_get_divisor - return uart clock divisor
409 * @port: uart_port structure describing the port.
410 * @baud: desired baud rate
411 *
412 * Calculate the uart clock divisor for the port.
413 */
414unsigned int
415uart_get_divisor(struct uart_port *port, unsigned int baud)
416{
417 unsigned int quot;
418
419 /*
420 * Old custom speed handling.
421 */
422 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
423 quot = port->custom_divisor;
424 else
425 quot = (port->uartclk + (8 * baud)) / (16 * baud);
426
427 return quot;
428}
429
430EXPORT_SYMBOL(uart_get_divisor);
431
432/* FIXME: Consistent locking policy */
433static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
434 struct ktermios *old_termios)
435{
436 struct tty_port *port = &state->port;
437 struct uart_port *uport = state->uart_port;
438 struct ktermios *termios;
439
440 /*
441 * If we have no tty, termios, or the port does not exist,
442 * then we can't set the parameters for this port.
443 */
444 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
445 return;
446
447 termios = tty->termios;
448
449 /*
450 * Set flags based on termios cflag
451 */
452 if (termios->c_cflag & CRTSCTS)
453 set_bit(ASYNCB_CTS_FLOW, &port->flags);
454 else
455 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
456
457 if (termios->c_cflag & CLOCAL)
458 clear_bit(ASYNCB_CHECK_CD, &port->flags);
459 else
460 set_bit(ASYNCB_CHECK_CD, &port->flags);
461
462 uport->ops->set_termios(uport, termios, old_termios);
463}
464
465static inline int __uart_put_char(struct uart_port *port,
466 struct circ_buf *circ, unsigned char c)
467{
468 unsigned long flags;
469 int ret = 0;
470
471 if (!circ->buf)
472 return 0;
473
474 spin_lock_irqsave(&port->lock, flags);
475 if (uart_circ_chars_free(circ) != 0) {
476 circ->buf[circ->head] = c;
477 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
478 ret = 1;
479 }
480 spin_unlock_irqrestore(&port->lock, flags);
481 return ret;
482}
483
484static int uart_put_char(struct tty_struct *tty, unsigned char ch)
485{
486 struct uart_state *state = tty->driver_data;
487
488 return __uart_put_char(state->uart_port, &state->xmit, ch);
489}
490
491static void uart_flush_chars(struct tty_struct *tty)
492{
493 uart_start(tty);
494}
495
496static int uart_write(struct tty_struct *tty,
497 const unsigned char *buf, int count)
498{
499 struct uart_state *state = tty->driver_data;
500 struct uart_port *port;
501 struct circ_buf *circ;
502 unsigned long flags;
503 int c, ret = 0;
504
505 /*
506 * This means you called this function _after_ the port was
507 * closed. No cookie for you.
508 */
509 if (!state) {
510 WARN_ON(1);
511 return -EL3HLT;
512 }
513
514 port = state->uart_port;
515 circ = &state->xmit;
516
517 if (!circ->buf)
518 return 0;
519
520 spin_lock_irqsave(&port->lock, flags);
521 while (1) {
522 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
523 if (count < c)
524 c = count;
525 if (c <= 0)
526 break;
527 memcpy(circ->buf + circ->head, buf, c);
528 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
529 buf += c;
530 count -= c;
531 ret += c;
532 }
533 spin_unlock_irqrestore(&port->lock, flags);
534
535 uart_start(tty);
536 return ret;
537}
538
539static int uart_write_room(struct tty_struct *tty)
540{
541 struct uart_state *state = tty->driver_data;
542 unsigned long flags;
543 int ret;
544
545 spin_lock_irqsave(&state->uart_port->lock, flags);
546 ret = uart_circ_chars_free(&state->xmit);
547 spin_unlock_irqrestore(&state->uart_port->lock, flags);
548 return ret;
549}
550
551static int uart_chars_in_buffer(struct tty_struct *tty)
552{
553 struct uart_state *state = tty->driver_data;
554 unsigned long flags;
555 int ret;
556
557 spin_lock_irqsave(&state->uart_port->lock, flags);
558 ret = uart_circ_chars_pending(&state->xmit);
559 spin_unlock_irqrestore(&state->uart_port->lock, flags);
560 return ret;
561}
562
563static void uart_flush_buffer(struct tty_struct *tty)
564{
565 struct uart_state *state = tty->driver_data;
566 struct uart_port *port;
567 unsigned long flags;
568
569 /*
570 * This means you called this function _after_ the port was
571 * closed. No cookie for you.
572 */
573 if (!state) {
574 WARN_ON(1);
575 return;
576 }
577
578 port = state->uart_port;
579 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
580
581 spin_lock_irqsave(&port->lock, flags);
582 uart_circ_clear(&state->xmit);
583 if (port->ops->flush_buffer)
584 port->ops->flush_buffer(port);
585 spin_unlock_irqrestore(&port->lock, flags);
586 tty_wakeup(tty);
587}
588
589/*
590 * This function is used to send a high-priority XON/XOFF character to
591 * the device
592 */
593static void uart_send_xchar(struct tty_struct *tty, char ch)
594{
595 struct uart_state *state = tty->driver_data;
596 struct uart_port *port = state->uart_port;
597 unsigned long flags;
598
599 if (port->ops->send_xchar)
600 port->ops->send_xchar(port, ch);
601 else {
602 port->x_char = ch;
603 if (ch) {
604 spin_lock_irqsave(&port->lock, flags);
605 port->ops->start_tx(port);
606 spin_unlock_irqrestore(&port->lock, flags);
607 }
608 }
609}
610
611static void uart_throttle(struct tty_struct *tty)
612{
613 struct uart_state *state = tty->driver_data;
614
615 if (I_IXOFF(tty))
616 uart_send_xchar(tty, STOP_CHAR(tty));
617
618 if (tty->termios->c_cflag & CRTSCTS)
619 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
620}
621
622static void uart_unthrottle(struct tty_struct *tty)
623{
624 struct uart_state *state = tty->driver_data;
625 struct uart_port *port = state->uart_port;
626
627 if (I_IXOFF(tty)) {
628 if (port->x_char)
629 port->x_char = 0;
630 else
631 uart_send_xchar(tty, START_CHAR(tty));
632 }
633
634 if (tty->termios->c_cflag & CRTSCTS)
635 uart_set_mctrl(port, TIOCM_RTS);
636}
637
638static int uart_get_info(struct uart_state *state,
639 struct serial_struct __user *retinfo)
640{
641 struct uart_port *uport = state->uart_port;
642 struct tty_port *port = &state->port;
643 struct serial_struct tmp;
644
645 memset(&tmp, 0, sizeof(tmp));
646
647 /* Ensure the state we copy is consistent and no hardware changes
648 occur as we go */
649 mutex_lock(&port->mutex);
650
651 tmp.type = uport->type;
652 tmp.line = uport->line;
653 tmp.port = uport->iobase;
654 if (HIGH_BITS_OFFSET)
655 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
656 tmp.irq = uport->irq;
657 tmp.flags = uport->flags;
658 tmp.xmit_fifo_size = uport->fifosize;
659 tmp.baud_base = uport->uartclk / 16;
660 tmp.close_delay = port->close_delay / 10;
661 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
662 ASYNC_CLOSING_WAIT_NONE :
663 port->closing_wait / 10;
664 tmp.custom_divisor = uport->custom_divisor;
665 tmp.hub6 = uport->hub6;
666 tmp.io_type = uport->iotype;
667 tmp.iomem_reg_shift = uport->regshift;
668 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
669
670 mutex_unlock(&port->mutex);
671
672 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
673 return -EFAULT;
674 return 0;
675}
676
677static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
678 struct serial_struct __user *newinfo)
679{
680 struct serial_struct new_serial;
681 struct uart_port *uport = state->uart_port;
682 struct tty_port *port = &state->port;
683 unsigned long new_port;
684 unsigned int change_irq, change_port, closing_wait;
685 unsigned int old_custom_divisor, close_delay;
686 upf_t old_flags, new_flags;
687 int retval = 0;
688
689 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
690 return -EFAULT;
691
692 new_port = new_serial.port;
693 if (HIGH_BITS_OFFSET)
694 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
695
696 new_serial.irq = irq_canonicalize(new_serial.irq);
697 close_delay = new_serial.close_delay * 10;
698 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
699 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
700
701 /*
702 * This semaphore protects port->count. It is also
703 * very useful to prevent opens. Also, take the
704 * port configuration semaphore to make sure that a
705 * module insertion/removal doesn't change anything
706 * under us.
707 */
708 mutex_lock(&port->mutex);
709
710 change_irq = !(uport->flags & UPF_FIXED_PORT)
711 && new_serial.irq != uport->irq;
712
713 /*
714 * Since changing the 'type' of the port changes its resource
715 * allocations, we should treat type changes the same as
716 * IO port changes.
717 */
718 change_port = !(uport->flags & UPF_FIXED_PORT)
719 && (new_port != uport->iobase ||
720 (unsigned long)new_serial.iomem_base != uport->mapbase ||
721 new_serial.hub6 != uport->hub6 ||
722 new_serial.io_type != uport->iotype ||
723 new_serial.iomem_reg_shift != uport->regshift ||
724 new_serial.type != uport->type);
725
726 old_flags = uport->flags;
727 new_flags = new_serial.flags;
728 old_custom_divisor = uport->custom_divisor;
729
730 if (!capable(CAP_SYS_ADMIN)) {
731 retval = -EPERM;
732 if (change_irq || change_port ||
733 (new_serial.baud_base != uport->uartclk / 16) ||
734 (close_delay != port->close_delay) ||
735 (closing_wait != port->closing_wait) ||
736 (new_serial.xmit_fifo_size &&
737 new_serial.xmit_fifo_size != uport->fifosize) ||
738 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
739 goto exit;
740 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
741 (new_flags & UPF_USR_MASK));
742 uport->custom_divisor = new_serial.custom_divisor;
743 goto check_and_exit;
744 }
745
746 /*
747 * Ask the low level driver to verify the settings.
748 */
749 if (uport->ops->verify_port)
750 retval = uport->ops->verify_port(uport, &new_serial);
751
752 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
753 (new_serial.baud_base < 9600))
754 retval = -EINVAL;
755
756 if (retval)
757 goto exit;
758
759 if (change_port || change_irq) {
760 retval = -EBUSY;
761
762 /*
763 * Make sure that we are the sole user of this port.
764 */
765 if (tty_port_users(port) > 1)
766 goto exit;
767
768 /*
769 * We need to shutdown the serial port at the old
770 * port/type/irq combination.
771 */
772 uart_shutdown(tty, state);
773 }
774
775 if (change_port) {
776 unsigned long old_iobase, old_mapbase;
777 unsigned int old_type, old_iotype, old_hub6, old_shift;
778
779 old_iobase = uport->iobase;
780 old_mapbase = uport->mapbase;
781 old_type = uport->type;
782 old_hub6 = uport->hub6;
783 old_iotype = uport->iotype;
784 old_shift = uport->regshift;
785
786 /*
787 * Free and release old regions
788 */
789 if (old_type != PORT_UNKNOWN)
790 uport->ops->release_port(uport);
791
792 uport->iobase = new_port;
793 uport->type = new_serial.type;
794 uport->hub6 = new_serial.hub6;
795 uport->iotype = new_serial.io_type;
796 uport->regshift = new_serial.iomem_reg_shift;
797 uport->mapbase = (unsigned long)new_serial.iomem_base;
798
799 /*
800 * Claim and map the new regions
801 */
802 if (uport->type != PORT_UNKNOWN) {
803 retval = uport->ops->request_port(uport);
804 } else {
805 /* Always success - Jean II */
806 retval = 0;
807 }
808
809 /*
810 * If we fail to request resources for the
811 * new port, try to restore the old settings.
812 */
813 if (retval && old_type != PORT_UNKNOWN) {
814 uport->iobase = old_iobase;
815 uport->type = old_type;
816 uport->hub6 = old_hub6;
817 uport->iotype = old_iotype;
818 uport->regshift = old_shift;
819 uport->mapbase = old_mapbase;
820 retval = uport->ops->request_port(uport);
821 /*
822 * If we failed to restore the old settings,
823 * we fail like this.
824 */
825 if (retval)
826 uport->type = PORT_UNKNOWN;
827
828 /*
829 * We failed anyway.
830 */
831 retval = -EBUSY;
832 /* Added to return the correct error -Ram Gupta */
833 goto exit;
834 }
835 }
836
837 if (change_irq)
838 uport->irq = new_serial.irq;
839 if (!(uport->flags & UPF_FIXED_PORT))
840 uport->uartclk = new_serial.baud_base * 16;
841 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
842 (new_flags & UPF_CHANGE_MASK);
843 uport->custom_divisor = new_serial.custom_divisor;
844 port->close_delay = close_delay;
845 port->closing_wait = closing_wait;
846 if (new_serial.xmit_fifo_size)
847 uport->fifosize = new_serial.xmit_fifo_size;
848 if (port->tty)
849 port->tty->low_latency =
850 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
851
852 check_and_exit:
853 retval = 0;
854 if (uport->type == PORT_UNKNOWN)
855 goto exit;
856 if (port->flags & ASYNC_INITIALIZED) {
857 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
858 old_custom_divisor != uport->custom_divisor) {
859 /*
860 * If they're setting up a custom divisor or speed,
861 * instead of clearing it, then bitch about it. No
862 * need to rate-limit; it's CAP_SYS_ADMIN only.
863 */
864 if (uport->flags & UPF_SPD_MASK) {
865 char buf[64];
866 printk(KERN_NOTICE
867 "%s sets custom speed on %s. This "
868 "is deprecated.\n", current->comm,
869 tty_name(port->tty, buf));
870 }
871 uart_change_speed(tty, state, NULL);
872 }
873 } else
874 retval = uart_startup(tty, state, 1);
875 exit:
876 mutex_unlock(&port->mutex);
877 return retval;
878}
879
880/**
881 * uart_get_lsr_info - get line status register info
882 * @tty: tty associated with the UART
883 * @state: UART being queried
884 * @value: returned modem value
885 *
886 * Note: uart_ioctl protects us against hangups.
887 */
888static int uart_get_lsr_info(struct tty_struct *tty,
889 struct uart_state *state, unsigned int __user *value)
890{
891 struct uart_port *uport = state->uart_port;
892 unsigned int result;
893
894 result = uport->ops->tx_empty(uport);
895
896 /*
897 * If we're about to load something into the transmit
898 * register, we'll pretend the transmitter isn't empty to
899 * avoid a race condition (depending on when the transmit
900 * interrupt happens).
901 */
902 if (uport->x_char ||
903 ((uart_circ_chars_pending(&state->xmit) > 0) &&
904 !tty->stopped && !tty->hw_stopped))
905 result &= ~TIOCSER_TEMT;
906
907 return put_user(result, value);
908}
909
910static int uart_tiocmget(struct tty_struct *tty)
911{
912 struct uart_state *state = tty->driver_data;
913 struct tty_port *port = &state->port;
914 struct uart_port *uport = state->uart_port;
915 int result = -EIO;
916
917 mutex_lock(&port->mutex);
918 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
919 result = uport->mctrl;
920 spin_lock_irq(&uport->lock);
921 result |= uport->ops->get_mctrl(uport);
922 spin_unlock_irq(&uport->lock);
923 }
924 mutex_unlock(&port->mutex);
925
926 return result;
927}
928
929static int
930uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
931{
932 struct uart_state *state = tty->driver_data;
933 struct uart_port *uport = state->uart_port;
934 struct tty_port *port = &state->port;
935 int ret = -EIO;
936
937 mutex_lock(&port->mutex);
938 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
939 uart_update_mctrl(uport, set, clear);
940 ret = 0;
941 }
942 mutex_unlock(&port->mutex);
943 return ret;
944}
945
946static int uart_break_ctl(struct tty_struct *tty, int break_state)
947{
948 struct uart_state *state = tty->driver_data;
949 struct tty_port *port = &state->port;
950 struct uart_port *uport = state->uart_port;
951
952 mutex_lock(&port->mutex);
953
954 if (uport->type != PORT_UNKNOWN)
955 uport->ops->break_ctl(uport, break_state);
956
957 mutex_unlock(&port->mutex);
958 return 0;
959}
960
961static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
962{
963 struct uart_port *uport = state->uart_port;
964 struct tty_port *port = &state->port;
965 int flags, ret;
966
967 if (!capable(CAP_SYS_ADMIN))
968 return -EPERM;
969
970 /*
971 * Take the per-port semaphore. This prevents count from
972 * changing, and hence any extra opens of the port while
973 * we're auto-configuring.
974 */
975 if (mutex_lock_interruptible(&port->mutex))
976 return -ERESTARTSYS;
977
978 ret = -EBUSY;
979 if (tty_port_users(port) == 1) {
980 uart_shutdown(tty, state);
981
982 /*
983 * If we already have a port type configured,
984 * we must release its resources.
985 */
986 if (uport->type != PORT_UNKNOWN)
987 uport->ops->release_port(uport);
988
989 flags = UART_CONFIG_TYPE;
990 if (uport->flags & UPF_AUTO_IRQ)
991 flags |= UART_CONFIG_IRQ;
992
993 /*
994 * This will claim the ports resources if
995 * a port is found.
996 */
997 uport->ops->config_port(uport, flags);
998
999 ret = uart_startup(tty, state, 1);
1000 }
1001 mutex_unlock(&port->mutex);
1002 return ret;
1003}
1004
1005/*
1006 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1007 * - mask passed in arg for lines of interest
1008 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1009 * Caller should use TIOCGICOUNT to see which one it was
1010 *
1011 * FIXME: This wants extracting into a common all driver implementation
1012 * of TIOCMWAIT using tty_port.
1013 */
1014static int
1015uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1016{
1017 struct uart_port *uport = state->uart_port;
1018 struct tty_port *port = &state->port;
1019 DECLARE_WAITQUEUE(wait, current);
1020 struct uart_icount cprev, cnow;
1021 int ret;
1022
1023 /*
1024 * note the counters on entry
1025 */
1026 spin_lock_irq(&uport->lock);
1027 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1028
1029 /*
1030 * Force modem status interrupts on
1031 */
1032 uport->ops->enable_ms(uport);
1033 spin_unlock_irq(&uport->lock);
1034
1035 add_wait_queue(&port->delta_msr_wait, &wait);
1036 for (;;) {
1037 spin_lock_irq(&uport->lock);
1038 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1039 spin_unlock_irq(&uport->lock);
1040
1041 set_current_state(TASK_INTERRUPTIBLE);
1042
1043 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1044 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1045 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1046 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1047 ret = 0;
1048 break;
1049 }
1050
1051 schedule();
1052
1053 /* see if a signal did it */
1054 if (signal_pending(current)) {
1055 ret = -ERESTARTSYS;
1056 break;
1057 }
1058
1059 cprev = cnow;
1060 }
1061
1062 current->state = TASK_RUNNING;
1063 remove_wait_queue(&port->delta_msr_wait, &wait);
1064
1065 return ret;
1066}
1067
1068/*
1069 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1070 * Return: write counters to the user passed counter struct
1071 * NB: both 1->0 and 0->1 transitions are counted except for
1072 * RI where only 0->1 is counted.
1073 */
1074static int uart_get_icount(struct tty_struct *tty,
1075 struct serial_icounter_struct *icount)
1076{
1077 struct uart_state *state = tty->driver_data;
1078 struct uart_icount cnow;
1079 struct uart_port *uport = state->uart_port;
1080
1081 spin_lock_irq(&uport->lock);
1082 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1083 spin_unlock_irq(&uport->lock);
1084
1085 icount->cts = cnow.cts;
1086 icount->dsr = cnow.dsr;
1087 icount->rng = cnow.rng;
1088 icount->dcd = cnow.dcd;
1089 icount->rx = cnow.rx;
1090 icount->tx = cnow.tx;
1091 icount->frame = cnow.frame;
1092 icount->overrun = cnow.overrun;
1093 icount->parity = cnow.parity;
1094 icount->brk = cnow.brk;
1095 icount->buf_overrun = cnow.buf_overrun;
1096
1097 return 0;
1098}
1099
1100/*
1101 * Called via sys_ioctl. We can use spin_lock_irq() here.
1102 */
1103static int
1104uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1105 unsigned long arg)
1106{
1107 struct uart_state *state = tty->driver_data;
1108 struct tty_port *port = &state->port;
1109 void __user *uarg = (void __user *)arg;
1110 int ret = -ENOIOCTLCMD;
1111
1112
1113 /*
1114 * These ioctls don't rely on the hardware to be present.
1115 */
1116 switch (cmd) {
1117 case TIOCGSERIAL:
1118 ret = uart_get_info(state, uarg);
1119 break;
1120
1121 case TIOCSSERIAL:
1122 ret = uart_set_info(tty, state, uarg);
1123 break;
1124
1125 case TIOCSERCONFIG:
1126 ret = uart_do_autoconfig(tty, state);
1127 break;
1128
1129 case TIOCSERGWILD: /* obsolete */
1130 case TIOCSERSWILD: /* obsolete */
1131 ret = 0;
1132 break;
1133 }
1134
1135 if (ret != -ENOIOCTLCMD)
1136 goto out;
1137
1138 if (tty->flags & (1 << TTY_IO_ERROR)) {
1139 ret = -EIO;
1140 goto out;
1141 }
1142
1143 /*
1144 * The following should only be used when hardware is present.
1145 */
1146 switch (cmd) {
1147 case TIOCMIWAIT:
1148 ret = uart_wait_modem_status(state, arg);
1149 break;
1150 }
1151
1152 if (ret != -ENOIOCTLCMD)
1153 goto out;
1154
1155 mutex_lock(&port->mutex);
1156
1157 if (tty->flags & (1 << TTY_IO_ERROR)) {
1158 ret = -EIO;
1159 goto out_up;
1160 }
1161
1162 /*
1163 * All these rely on hardware being present and need to be
1164 * protected against the tty being hung up.
1165 */
1166 switch (cmd) {
1167 case TIOCSERGETLSR: /* Get line status register */
1168 ret = uart_get_lsr_info(tty, state, uarg);
1169 break;
1170
1171 default: {
1172 struct uart_port *uport = state->uart_port;
1173 if (uport->ops->ioctl)
1174 ret = uport->ops->ioctl(uport, cmd, arg);
1175 break;
1176 }
1177 }
1178out_up:
1179 mutex_unlock(&port->mutex);
1180out:
1181 return ret;
1182}
1183
1184static void uart_set_ldisc(struct tty_struct *tty)
1185{
1186 struct uart_state *state = tty->driver_data;
1187 struct uart_port *uport = state->uart_port;
1188
1189 if (uport->ops->set_ldisc)
1190 uport->ops->set_ldisc(uport, tty->termios->c_line);
1191}
1192
1193static void uart_set_termios(struct tty_struct *tty,
1194 struct ktermios *old_termios)
1195{
1196 struct uart_state *state = tty->driver_data;
1197 unsigned long flags;
1198 unsigned int cflag = tty->termios->c_cflag;
1199
1200
1201 /*
1202 * These are the bits that are used to setup various
1203 * flags in the low level driver. We can ignore the Bfoo
1204 * bits in c_cflag; c_[io]speed will always be set
1205 * appropriately by set_termios() in tty_ioctl.c
1206 */
1207#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1208 if ((cflag ^ old_termios->c_cflag) == 0 &&
1209 tty->termios->c_ospeed == old_termios->c_ospeed &&
1210 tty->termios->c_ispeed == old_termios->c_ispeed &&
1211 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1212 return;
1213 }
1214
1215 uart_change_speed(tty, state, old_termios);
1216
1217 /* Handle transition to B0 status */
1218 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1219 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1220 /* Handle transition away from B0 status */
1221 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1222 unsigned int mask = TIOCM_DTR;
1223 if (!(cflag & CRTSCTS) ||
1224 !test_bit(TTY_THROTTLED, &tty->flags))
1225 mask |= TIOCM_RTS;
1226 uart_set_mctrl(state->uart_port, mask);
1227 }
1228
1229 /* Handle turning off CRTSCTS */
1230 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1231 spin_lock_irqsave(&state->uart_port->lock, flags);
1232 tty->hw_stopped = 0;
1233 __uart_start(tty);
1234 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1235 }
1236 /* Handle turning on CRTSCTS */
1237 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1238 spin_lock_irqsave(&state->uart_port->lock, flags);
1239 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1240 tty->hw_stopped = 1;
1241 state->uart_port->ops->stop_tx(state->uart_port);
1242 }
1243 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1244 }
1245}
1246
1247/*
1248 * In 2.4.5, calls to this will be serialized via the BKL in
1249 * linux/drivers/char/tty_io.c:tty_release()
1250 * linux/drivers/char/tty_io.c:do_tty_handup()
1251 */
1252static void uart_close(struct tty_struct *tty, struct file *filp)
1253{
1254 struct uart_state *state = tty->driver_data;
1255 struct tty_port *port;
1256 struct uart_port *uport;
1257 unsigned long flags;
1258
1259 BUG_ON(!tty_locked());
1260
1261 if (!state)
1262 return;
1263
1264 uport = state->uart_port;
1265 port = &state->port;
1266
1267 pr_debug("uart_close(%d) called\n", uport->line);
1268
1269 mutex_lock(&port->mutex);
1270 spin_lock_irqsave(&port->lock, flags);
1271
1272 if (tty_hung_up_p(filp)) {
1273 spin_unlock_irqrestore(&port->lock, flags);
1274 goto done;
1275 }
1276
1277 if ((tty->count == 1) && (port->count != 1)) {
1278 /*
1279 * Uh, oh. tty->count is 1, which means that the tty
1280 * structure will be freed. port->count should always
1281 * be one in these conditions. If it's greater than
1282 * one, we've got real problems, since it means the
1283 * serial port won't be shutdown.
1284 */
1285 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1286 "port->count is %d\n", port->count);
1287 port->count = 1;
1288 }
1289 if (--port->count < 0) {
1290 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1291 tty->name, port->count);
1292 port->count = 0;
1293 }
1294 if (port->count) {
1295 spin_unlock_irqrestore(&port->lock, flags);
1296 goto done;
1297 }
1298
1299 /*
1300 * Now we wait for the transmit buffer to clear; and we notify
1301 * the line discipline to only process XON/XOFF characters by
1302 * setting tty->closing.
1303 */
1304 tty->closing = 1;
1305 spin_unlock_irqrestore(&port->lock, flags);
1306
1307 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
1308 /*
1309 * hack: open-coded tty_wait_until_sent to avoid
1310 * recursive tty_lock
1311 */
1312 long timeout = msecs_to_jiffies(port->closing_wait);
1313 if (wait_event_interruptible_timeout(tty->write_wait,
1314 !tty_chars_in_buffer(tty), timeout) >= 0)
1315 __uart_wait_until_sent(uport, timeout);
1316 }
1317
1318 /*
1319 * At this point, we stop accepting input. To do this, we
1320 * disable the receive line status interrupts.
1321 */
1322 if (port->flags & ASYNC_INITIALIZED) {
1323 unsigned long flags;
1324 spin_lock_irqsave(&uport->lock, flags);
1325 uport->ops->stop_rx(uport);
1326 spin_unlock_irqrestore(&uport->lock, flags);
1327 /*
1328 * Before we drop DTR, make sure the UART transmitter
1329 * has completely drained; this is especially
1330 * important if there is a transmit FIFO!
1331 */
1332 __uart_wait_until_sent(uport, uport->timeout);
1333 }
1334
1335 uart_shutdown(tty, state);
1336 uart_flush_buffer(tty);
1337
1338 tty_ldisc_flush(tty);
1339
1340 tty_port_tty_set(port, NULL);
1341 spin_lock_irqsave(&port->lock, flags);
1342 tty->closing = 0;
1343
1344 if (port->blocked_open) {
1345 spin_unlock_irqrestore(&port->lock, flags);
1346 if (port->close_delay)
1347 msleep_interruptible(port->close_delay);
1348 spin_lock_irqsave(&port->lock, flags);
1349 } else if (!uart_console(uport)) {
1350 spin_unlock_irqrestore(&port->lock, flags);
1351 uart_change_pm(state, 3);
1352 spin_lock_irqsave(&port->lock, flags);
1353 }
1354
1355 /*
1356 * Wake up anyone trying to open this port.
1357 */
1358 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1359 spin_unlock_irqrestore(&port->lock, flags);
1360 wake_up_interruptible(&port->open_wait);
1361
1362done:
1363 mutex_unlock(&port->mutex);
1364}
1365
1366static void __uart_wait_until_sent(struct uart_port *port, int timeout)
1367{
1368 unsigned long char_time, expire;
1369
1370 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1371 return;
1372
1373 /*
1374 * Set the check interval to be 1/5 of the estimated time to
1375 * send a single character, and make it at least 1. The check
1376 * interval should also be less than the timeout.
1377 *
1378 * Note: we have to use pretty tight timings here to satisfy
1379 * the NIST-PCTS.
1380 */
1381 char_time = (port->timeout - HZ/50) / port->fifosize;
1382 char_time = char_time / 5;
1383 if (char_time == 0)
1384 char_time = 1;
1385 if (timeout && timeout < char_time)
1386 char_time = timeout;
1387
1388 /*
1389 * If the transmitter hasn't cleared in twice the approximate
1390 * amount of time to send the entire FIFO, it probably won't
1391 * ever clear. This assumes the UART isn't doing flow
1392 * control, which is currently the case. Hence, if it ever
1393 * takes longer than port->timeout, this is probably due to a
1394 * UART bug of some kind. So, we clamp the timeout parameter at
1395 * 2*port->timeout.
1396 */
1397 if (timeout == 0 || timeout > 2 * port->timeout)
1398 timeout = 2 * port->timeout;
1399
1400 expire = jiffies + timeout;
1401
1402 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1403 port->line, jiffies, expire);
1404
1405 /*
1406 * Check whether the transmitter is empty every 'char_time'.
1407 * 'timeout' / 'expire' give us the maximum amount of time
1408 * we wait.
1409 */
1410 while (!port->ops->tx_empty(port)) {
1411 msleep_interruptible(jiffies_to_msecs(char_time));
1412 if (signal_pending(current))
1413 break;
1414 if (time_after(jiffies, expire))
1415 break;
1416 }
1417}
1418
1419static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1420{
1421 struct uart_state *state = tty->driver_data;
1422 struct uart_port *port = state->uart_port;
1423
1424 tty_lock();
1425 __uart_wait_until_sent(port, timeout);
1426 tty_unlock();
1427}
1428
1429/*
1430 * This is called with the BKL held in
1431 * linux/drivers/char/tty_io.c:do_tty_hangup()
1432 * We're called from the eventd thread, so we can sleep for
1433 * a _short_ time only.
1434 */
1435static void uart_hangup(struct tty_struct *tty)
1436{
1437 struct uart_state *state = tty->driver_data;
1438 struct tty_port *port = &state->port;
1439 unsigned long flags;
1440
1441 BUG_ON(!tty_locked());
1442 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1443
1444 mutex_lock(&port->mutex);
1445 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1446 uart_flush_buffer(tty);
1447 uart_shutdown(tty, state);
1448 spin_lock_irqsave(&port->lock, flags);
1449 port->count = 0;
1450 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1451 spin_unlock_irqrestore(&port->lock, flags);
1452 tty_port_tty_set(port, NULL);
1453 wake_up_interruptible(&port->open_wait);
1454 wake_up_interruptible(&port->delta_msr_wait);
1455 }
1456 mutex_unlock(&port->mutex);
1457}
1458
1459static int uart_carrier_raised(struct tty_port *port)
1460{
1461 struct uart_state *state = container_of(port, struct uart_state, port);
1462 struct uart_port *uport = state->uart_port;
1463 int mctrl;
1464 spin_lock_irq(&uport->lock);
1465 uport->ops->enable_ms(uport);
1466 mctrl = uport->ops->get_mctrl(uport);
1467 spin_unlock_irq(&uport->lock);
1468 if (mctrl & TIOCM_CAR)
1469 return 1;
1470 return 0;
1471}
1472
1473static void uart_dtr_rts(struct tty_port *port, int onoff)
1474{
1475 struct uart_state *state = container_of(port, struct uart_state, port);
1476 struct uart_port *uport = state->uart_port;
1477
1478 if (onoff)
1479 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1480 else
1481 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1482}
1483
1484static struct uart_state *uart_get(struct uart_driver *drv, int line)
1485{
1486 struct uart_state *state;
1487 struct tty_port *port;
1488 int ret = 0;
1489
1490 state = drv->state + line;
1491 port = &state->port;
1492 if (mutex_lock_interruptible(&port->mutex)) {
1493 ret = -ERESTARTSYS;
1494 goto err;
1495 }
1496
1497 port->count++;
1498 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1499 ret = -ENXIO;
1500 goto err_unlock;
1501 }
1502 return state;
1503
1504 err_unlock:
1505 port->count--;
1506 mutex_unlock(&port->mutex);
1507 err:
1508 return ERR_PTR(ret);
1509}
1510
1511/*
1512 * calls to uart_open are serialised by the BKL in
1513 * fs/char_dev.c:chrdev_open()
1514 * Note that if this fails, then uart_close() _will_ be called.
1515 *
1516 * In time, we want to scrap the "opening nonpresent ports"
1517 * behaviour and implement an alternative way for setserial
1518 * to set base addresses/ports/types. This will allow us to
1519 * get rid of a certain amount of extra tests.
1520 */
1521static int uart_open(struct tty_struct *tty, struct file *filp)
1522{
1523 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1524 struct uart_state *state;
1525 struct tty_port *port;
1526 int retval, line = tty->index;
1527
1528 BUG_ON(!tty_locked());
1529 pr_debug("uart_open(%d) called\n", line);
1530
1531 /*
1532 * We take the semaphore inside uart_get to guarantee that we won't
1533 * be re-entered while allocating the state structure, or while we
1534 * request any IRQs that the driver may need. This also has the nice
1535 * side-effect that it delays the action of uart_hangup, so we can
1536 * guarantee that state->port.tty will always contain something
1537 * reasonable.
1538 */
1539 state = uart_get(drv, line);
1540 if (IS_ERR(state)) {
1541 retval = PTR_ERR(state);
1542 goto fail;
1543 }
1544 port = &state->port;
1545
1546 /*
1547 * Once we set tty->driver_data here, we are guaranteed that
1548 * uart_close() will decrement the driver module use count.
1549 * Any failures from here onwards should not touch the count.
1550 */
1551 tty->driver_data = state;
1552 state->uart_port->state = state;
1553 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1554 tty->alt_speed = 0;
1555 tty_port_tty_set(port, tty);
1556
1557 /*
1558 * If the port is in the middle of closing, bail out now.
1559 */
1560 if (tty_hung_up_p(filp)) {
1561 retval = -EAGAIN;
1562 port->count--;
1563 mutex_unlock(&port->mutex);
1564 goto fail;
1565 }
1566
1567 /*
1568 * Make sure the device is in D0 state.
1569 */
1570 if (port->count == 1)
1571 uart_change_pm(state, 0);
1572
1573 /*
1574 * Start up the serial port.
1575 */
1576 retval = uart_startup(tty, state, 0);
1577
1578 /*
1579 * If we succeeded, wait until the port is ready.
1580 */
1581 mutex_unlock(&port->mutex);
1582 if (retval == 0)
1583 retval = tty_port_block_til_ready(port, tty, filp);
1584
1585fail:
1586 return retval;
1587}
1588
1589static const char *uart_type(struct uart_port *port)
1590{
1591 const char *str = NULL;
1592
1593 if (port->ops->type)
1594 str = port->ops->type(port);
1595
1596 if (!str)
1597 str = "unknown";
1598
1599 return str;
1600}
1601
1602#ifdef CONFIG_PROC_FS
1603
1604static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1605{
1606 struct uart_state *state = drv->state + i;
1607 struct tty_port *port = &state->port;
1608 int pm_state;
1609 struct uart_port *uport = state->uart_port;
1610 char stat_buf[32];
1611 unsigned int status;
1612 int mmio;
1613
1614 if (!uport)
1615 return;
1616
1617 mmio = uport->iotype >= UPIO_MEM;
1618 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1619 uport->line, uart_type(uport),
1620 mmio ? "mmio:0x" : "port:",
1621 mmio ? (unsigned long long)uport->mapbase
1622 : (unsigned long long)uport->iobase,
1623 uport->irq);
1624
1625 if (uport->type == PORT_UNKNOWN) {
1626 seq_putc(m, '\n');
1627 return;
1628 }
1629
1630 if (capable(CAP_SYS_ADMIN)) {
1631 mutex_lock(&port->mutex);
1632 pm_state = state->pm_state;
1633 if (pm_state)
1634 uart_change_pm(state, 0);
1635 spin_lock_irq(&uport->lock);
1636 status = uport->ops->get_mctrl(uport);
1637 spin_unlock_irq(&uport->lock);
1638 if (pm_state)
1639 uart_change_pm(state, pm_state);
1640 mutex_unlock(&port->mutex);
1641
1642 seq_printf(m, " tx:%d rx:%d",
1643 uport->icount.tx, uport->icount.rx);
1644 if (uport->icount.frame)
1645 seq_printf(m, " fe:%d",
1646 uport->icount.frame);
1647 if (uport->icount.parity)
1648 seq_printf(m, " pe:%d",
1649 uport->icount.parity);
1650 if (uport->icount.brk)
1651 seq_printf(m, " brk:%d",
1652 uport->icount.brk);
1653 if (uport->icount.overrun)
1654 seq_printf(m, " oe:%d",
1655 uport->icount.overrun);
1656
1657#define INFOBIT(bit, str) \
1658 if (uport->mctrl & (bit)) \
1659 strncat(stat_buf, (str), sizeof(stat_buf) - \
1660 strlen(stat_buf) - 2)
1661#define STATBIT(bit, str) \
1662 if (status & (bit)) \
1663 strncat(stat_buf, (str), sizeof(stat_buf) - \
1664 strlen(stat_buf) - 2)
1665
1666 stat_buf[0] = '\0';
1667 stat_buf[1] = '\0';
1668 INFOBIT(TIOCM_RTS, "|RTS");
1669 STATBIT(TIOCM_CTS, "|CTS");
1670 INFOBIT(TIOCM_DTR, "|DTR");
1671 STATBIT(TIOCM_DSR, "|DSR");
1672 STATBIT(TIOCM_CAR, "|CD");
1673 STATBIT(TIOCM_RNG, "|RI");
1674 if (stat_buf[0])
1675 stat_buf[0] = ' ';
1676
1677 seq_puts(m, stat_buf);
1678 }
1679 seq_putc(m, '\n');
1680#undef STATBIT
1681#undef INFOBIT
1682}
1683
1684static int uart_proc_show(struct seq_file *m, void *v)
1685{
1686 struct tty_driver *ttydrv = m->private;
1687 struct uart_driver *drv = ttydrv->driver_state;
1688 int i;
1689
1690 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1691 "", "", "");
1692 for (i = 0; i < drv->nr; i++)
1693 uart_line_info(m, drv, i);
1694 return 0;
1695}
1696
1697static int uart_proc_open(struct inode *inode, struct file *file)
1698{
1699 return single_open(file, uart_proc_show, PDE(inode)->data);
1700}
1701
1702static const struct file_operations uart_proc_fops = {
1703 .owner = THIS_MODULE,
1704 .open = uart_proc_open,
1705 .read = seq_read,
1706 .llseek = seq_lseek,
1707 .release = single_release,
1708};
1709#endif
1710
1711#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1712/*
1713 * uart_console_write - write a console message to a serial port
1714 * @port: the port to write the message
1715 * @s: array of characters
1716 * @count: number of characters in string to write
1717 * @write: function to write character to port
1718 */
1719void uart_console_write(struct uart_port *port, const char *s,
1720 unsigned int count,
1721 void (*putchar)(struct uart_port *, int))
1722{
1723 unsigned int i;
1724
1725 for (i = 0; i < count; i++, s++) {
1726 if (*s == '\n')
1727 putchar(port, '\r');
1728 putchar(port, *s);
1729 }
1730}
1731EXPORT_SYMBOL_GPL(uart_console_write);
1732
1733/*
1734 * Check whether an invalid uart number has been specified, and
1735 * if so, search for the first available port that does have
1736 * console support.
1737 */
1738struct uart_port * __init
1739uart_get_console(struct uart_port *ports, int nr, struct console *co)
1740{
1741 int idx = co->index;
1742
1743 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1744 ports[idx].membase == NULL))
1745 for (idx = 0; idx < nr; idx++)
1746 if (ports[idx].iobase != 0 ||
1747 ports[idx].membase != NULL)
1748 break;
1749
1750 co->index = idx;
1751
1752 return ports + idx;
1753}
1754
1755/**
1756 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1757 * @options: pointer to option string
1758 * @baud: pointer to an 'int' variable for the baud rate.
1759 * @parity: pointer to an 'int' variable for the parity.
1760 * @bits: pointer to an 'int' variable for the number of data bits.
1761 * @flow: pointer to an 'int' variable for the flow control character.
1762 *
1763 * uart_parse_options decodes a string containing the serial console
1764 * options. The format of the string is <baud><parity><bits><flow>,
1765 * eg: 115200n8r
1766 */
1767void
1768uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1769{
1770 char *s = options;
1771
1772 *baud = simple_strtoul(s, NULL, 10);
1773 while (*s >= '0' && *s <= '9')
1774 s++;
1775 if (*s)
1776 *parity = *s++;
1777 if (*s)
1778 *bits = *s++ - '0';
1779 if (*s)
1780 *flow = *s;
1781}
1782EXPORT_SYMBOL_GPL(uart_parse_options);
1783
1784struct baud_rates {
1785 unsigned int rate;
1786 unsigned int cflag;
1787};
1788
1789static const struct baud_rates baud_rates[] = {
1790 { 921600, B921600 },
1791 { 460800, B460800 },
1792 { 230400, B230400 },
1793 { 115200, B115200 },
1794 { 57600, B57600 },
1795 { 38400, B38400 },
1796 { 19200, B19200 },
1797 { 9600, B9600 },
1798 { 4800, B4800 },
1799 { 2400, B2400 },
1800 { 1200, B1200 },
1801 { 0, B38400 }
1802};
1803
1804/**
1805 * uart_set_options - setup the serial console parameters
1806 * @port: pointer to the serial ports uart_port structure
1807 * @co: console pointer
1808 * @baud: baud rate
1809 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1810 * @bits: number of data bits
1811 * @flow: flow control character - 'r' (rts)
1812 */
1813int
1814uart_set_options(struct uart_port *port, struct console *co,
1815 int baud, int parity, int bits, int flow)
1816{
1817 struct ktermios termios;
1818 static struct ktermios dummy;
1819 int i;
1820
1821 /*
1822 * Ensure that the serial console lock is initialised
1823 * early.
1824 */
1825 spin_lock_init(&port->lock);
1826 lockdep_set_class(&port->lock, &port_lock_key);
1827
1828 memset(&termios, 0, sizeof(struct ktermios));
1829
1830 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1831
1832 /*
1833 * Construct a cflag setting.
1834 */
1835 for (i = 0; baud_rates[i].rate; i++)
1836 if (baud_rates[i].rate <= baud)
1837 break;
1838
1839 termios.c_cflag |= baud_rates[i].cflag;
1840
1841 if (bits == 7)
1842 termios.c_cflag |= CS7;
1843 else
1844 termios.c_cflag |= CS8;
1845
1846 switch (parity) {
1847 case 'o': case 'O':
1848 termios.c_cflag |= PARODD;
1849 /*fall through*/
1850 case 'e': case 'E':
1851 termios.c_cflag |= PARENB;
1852 break;
1853 }
1854
1855 if (flow == 'r')
1856 termios.c_cflag |= CRTSCTS;
1857
1858 /*
1859 * some uarts on other side don't support no flow control.
1860 * So we set * DTR in host uart to make them happy
1861 */
1862 port->mctrl |= TIOCM_DTR;
1863
1864 port->ops->set_termios(port, &termios, &dummy);
1865 /*
1866 * Allow the setting of the UART parameters with a NULL console
1867 * too:
1868 */
1869 if (co)
1870 co->cflag = termios.c_cflag;
1871
1872 return 0;
1873}
1874EXPORT_SYMBOL_GPL(uart_set_options);
1875#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1876
1877static void uart_change_pm(struct uart_state *state, int pm_state)
1878{
1879 struct uart_port *port = state->uart_port;
1880
1881 if (state->pm_state != pm_state) {
1882 if (port->ops->pm)
1883 port->ops->pm(port, pm_state, state->pm_state);
1884 state->pm_state = pm_state;
1885 }
1886}
1887
1888struct uart_match {
1889 struct uart_port *port;
1890 struct uart_driver *driver;
1891};
1892
1893static int serial_match_port(struct device *dev, void *data)
1894{
1895 struct uart_match *match = data;
1896 struct tty_driver *tty_drv = match->driver->tty_driver;
1897 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1898 match->port->line;
1899
1900 return dev->devt == devt; /* Actually, only one tty per port */
1901}
1902
1903int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1904{
1905 struct uart_state *state = drv->state + uport->line;
1906 struct tty_port *port = &state->port;
1907 struct device *tty_dev;
1908 struct uart_match match = {uport, drv};
1909
1910 mutex_lock(&port->mutex);
1911
1912 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1913 if (device_may_wakeup(tty_dev)) {
1914 if (!enable_irq_wake(uport->irq))
1915 uport->irq_wake = 1;
1916 put_device(tty_dev);
1917 mutex_unlock(&port->mutex);
1918 return 0;
1919 }
1920 if (console_suspend_enabled || !uart_console(uport))
1921 uport->suspended = 1;
1922
1923 if (port->flags & ASYNC_INITIALIZED) {
1924 const struct uart_ops *ops = uport->ops;
1925 int tries;
1926
1927 if (console_suspend_enabled || !uart_console(uport)) {
1928 set_bit(ASYNCB_SUSPENDED, &port->flags);
1929 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1930
1931 spin_lock_irq(&uport->lock);
1932 ops->stop_tx(uport);
1933 ops->set_mctrl(uport, 0);
1934 ops->stop_rx(uport);
1935 spin_unlock_irq(&uport->lock);
1936 }
1937
1938 /*
1939 * Wait for the transmitter to empty.
1940 */
1941 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1942 msleep(10);
1943 if (!tries)
1944 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1945 "transmitter\n",
1946 uport->dev ? dev_name(uport->dev) : "",
1947 uport->dev ? ": " : "",
1948 drv->dev_name,
1949 drv->tty_driver->name_base + uport->line);
1950
1951 if (console_suspend_enabled || !uart_console(uport))
1952 ops->shutdown(uport);
1953 }
1954
1955 /*
1956 * Disable the console device before suspending.
1957 */
1958 if (console_suspend_enabled && uart_console(uport))
1959 console_stop(uport->cons);
1960
1961 if (console_suspend_enabled || !uart_console(uport))
1962 uart_change_pm(state, 3);
1963
1964 mutex_unlock(&port->mutex);
1965
1966 return 0;
1967}
1968
1969int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1970{
1971 struct uart_state *state = drv->state + uport->line;
1972 struct tty_port *port = &state->port;
1973 struct device *tty_dev;
1974 struct uart_match match = {uport, drv};
1975 struct ktermios termios;
1976
1977 mutex_lock(&port->mutex);
1978
1979 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1980 if (!uport->suspended && device_may_wakeup(tty_dev)) {
1981 if (uport->irq_wake) {
1982 disable_irq_wake(uport->irq);
1983 uport->irq_wake = 0;
1984 }
1985 mutex_unlock(&port->mutex);
1986 return 0;
1987 }
1988 uport->suspended = 0;
1989
1990 /*
1991 * Re-enable the console device after suspending.
1992 */
1993 if (uart_console(uport)) {
1994 /*
1995 * First try to use the console cflag setting.
1996 */
1997 memset(&termios, 0, sizeof(struct ktermios));
1998 termios.c_cflag = uport->cons->cflag;
1999
2000 /*
2001 * If that's unset, use the tty termios setting.
2002 */
2003 if (port->tty && port->tty->termios && termios.c_cflag == 0)
2004 termios = *(port->tty->termios);
2005
2006 uport->ops->set_termios(uport, &termios, NULL);
2007 if (console_suspend_enabled)
2008 console_start(uport->cons);
2009 }
2010
2011 if (port->flags & ASYNC_SUSPENDED) {
2012 const struct uart_ops *ops = uport->ops;
2013 int ret;
2014
2015 uart_change_pm(state, 0);
2016 spin_lock_irq(&uport->lock);
2017 ops->set_mctrl(uport, 0);
2018 spin_unlock_irq(&uport->lock);
2019 if (console_suspend_enabled || !uart_console(uport)) {
2020 /* Protected by port mutex for now */
2021 struct tty_struct *tty = port->tty;
2022 ret = ops->startup(uport);
2023 if (ret == 0) {
2024 if (tty)
2025 uart_change_speed(tty, state, NULL);
2026 spin_lock_irq(&uport->lock);
2027 ops->set_mctrl(uport, uport->mctrl);
2028 ops->start_tx(uport);
2029 spin_unlock_irq(&uport->lock);
2030 set_bit(ASYNCB_INITIALIZED, &port->flags);
2031 } else {
2032 /*
2033 * Failed to resume - maybe hardware went away?
2034 * Clear the "initialized" flag so we won't try
2035 * to call the low level drivers shutdown method.
2036 */
2037 uart_shutdown(tty, state);
2038 }
2039 }
2040
2041 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2042 }
2043
2044 mutex_unlock(&port->mutex);
2045
2046 return 0;
2047}
2048
2049static inline void
2050uart_report_port(struct uart_driver *drv, struct uart_port *port)
2051{
2052 char address[64];
2053
2054 switch (port->iotype) {
2055 case UPIO_PORT:
2056 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2057 break;
2058 case UPIO_HUB6:
2059 snprintf(address, sizeof(address),
2060 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2061 break;
2062 case UPIO_MEM:
2063 case UPIO_MEM32:
2064 case UPIO_AU:
2065 case UPIO_TSI:
2066 case UPIO_DWAPB:
2067 case UPIO_DWAPB32:
2068 snprintf(address, sizeof(address),
2069 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2070 break;
2071 default:
2072 strlcpy(address, "*unknown*", sizeof(address));
2073 break;
2074 }
2075
2076 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2077 port->dev ? dev_name(port->dev) : "",
2078 port->dev ? ": " : "",
2079 drv->dev_name,
2080 drv->tty_driver->name_base + port->line,
2081 address, port->irq, uart_type(port));
2082}
2083
2084static void
2085uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2086 struct uart_port *port)
2087{
2088 unsigned int flags;
2089
2090 /*
2091 * If there isn't a port here, don't do anything further.
2092 */
2093 if (!port->iobase && !port->mapbase && !port->membase)
2094 return;
2095
2096 /*
2097 * Now do the auto configuration stuff. Note that config_port
2098 * is expected to claim the resources and map the port for us.
2099 */
2100 flags = 0;
2101 if (port->flags & UPF_AUTO_IRQ)
2102 flags |= UART_CONFIG_IRQ;
2103 if (port->flags & UPF_BOOT_AUTOCONF) {
2104 if (!(port->flags & UPF_FIXED_TYPE)) {
2105 port->type = PORT_UNKNOWN;
2106 flags |= UART_CONFIG_TYPE;
2107 }
2108 port->ops->config_port(port, flags);
2109 }
2110
2111 if (port->type != PORT_UNKNOWN) {
2112 unsigned long flags;
2113
2114 uart_report_port(drv, port);
2115
2116 /* Power up port for set_mctrl() */
2117 uart_change_pm(state, 0);
2118
2119 /*
2120 * Ensure that the modem control lines are de-activated.
2121 * keep the DTR setting that is set in uart_set_options()
2122 * We probably don't need a spinlock around this, but
2123 */
2124 spin_lock_irqsave(&port->lock, flags);
2125 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2126 spin_unlock_irqrestore(&port->lock, flags);
2127
2128 /*
2129 * If this driver supports console, and it hasn't been
2130 * successfully registered yet, try to re-register it.
2131 * It may be that the port was not available.
2132 */
2133 if (port->cons && !(port->cons->flags & CON_ENABLED))
2134 register_console(port->cons);
2135
2136 /*
2137 * Power down all ports by default, except the
2138 * console if we have one.
2139 */
2140 if (!uart_console(port))
2141 uart_change_pm(state, 3);
2142 }
2143}
2144
2145#ifdef CONFIG_CONSOLE_POLL
2146
2147static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2148{
2149 struct uart_driver *drv = driver->driver_state;
2150 struct uart_state *state = drv->state + line;
2151 struct uart_port *port;
2152 int baud = 9600;
2153 int bits = 8;
2154 int parity = 'n';
2155 int flow = 'n';
2156
2157 if (!state || !state->uart_port)
2158 return -1;
2159
2160 port = state->uart_port;
2161 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2162 return -1;
2163
2164 if (options) {
2165 uart_parse_options(options, &baud, &parity, &bits, &flow);
2166 return uart_set_options(port, NULL, baud, parity, bits, flow);
2167 }
2168
2169 return 0;
2170}
2171
2172static int uart_poll_get_char(struct tty_driver *driver, int line)
2173{
2174 struct uart_driver *drv = driver->driver_state;
2175 struct uart_state *state = drv->state + line;
2176 struct uart_port *port;
2177
2178 if (!state || !state->uart_port)
2179 return -1;
2180
2181 port = state->uart_port;
2182 return port->ops->poll_get_char(port);
2183}
2184
2185static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2186{
2187 struct uart_driver *drv = driver->driver_state;
2188 struct uart_state *state = drv->state + line;
2189 struct uart_port *port;
2190
2191 if (!state || !state->uart_port)
2192 return;
2193
2194 port = state->uart_port;
2195 port->ops->poll_put_char(port, ch);
2196}
2197#endif
2198
2199static const struct tty_operations uart_ops = {
2200 .open = uart_open,
2201 .close = uart_close,
2202 .write = uart_write,
2203 .put_char = uart_put_char,
2204 .flush_chars = uart_flush_chars,
2205 .write_room = uart_write_room,
2206 .chars_in_buffer= uart_chars_in_buffer,
2207 .flush_buffer = uart_flush_buffer,
2208 .ioctl = uart_ioctl,
2209 .throttle = uart_throttle,
2210 .unthrottle = uart_unthrottle,
2211 .send_xchar = uart_send_xchar,
2212 .set_termios = uart_set_termios,
2213 .set_ldisc = uart_set_ldisc,
2214 .stop = uart_stop,
2215 .start = uart_start,
2216 .hangup = uart_hangup,
2217 .break_ctl = uart_break_ctl,
2218 .wait_until_sent= uart_wait_until_sent,
2219#ifdef CONFIG_PROC_FS
2220 .proc_fops = &uart_proc_fops,
2221#endif
2222 .tiocmget = uart_tiocmget,
2223 .tiocmset = uart_tiocmset,
2224 .get_icount = uart_get_icount,
2225#ifdef CONFIG_CONSOLE_POLL
2226 .poll_init = uart_poll_init,
2227 .poll_get_char = uart_poll_get_char,
2228 .poll_put_char = uart_poll_put_char,
2229#endif
2230};
2231
2232static const struct tty_port_operations uart_port_ops = {
2233 .carrier_raised = uart_carrier_raised,
2234 .dtr_rts = uart_dtr_rts,
2235};
2236
2237/**
2238 * uart_register_driver - register a driver with the uart core layer
2239 * @drv: low level driver structure
2240 *
2241 * Register a uart driver with the core driver. We in turn register
2242 * with the tty layer, and initialise the core driver per-port state.
2243 *
2244 * We have a proc file in /proc/tty/driver which is named after the
2245 * normal driver.
2246 *
2247 * drv->port should be NULL, and the per-port structures should be
2248 * registered using uart_add_one_port after this call has succeeded.
2249 */
2250int uart_register_driver(struct uart_driver *drv)
2251{
2252 struct tty_driver *normal;
2253 int i, retval;
2254
2255 BUG_ON(drv->state);
2256
2257 /*
2258 * Maybe we should be using a slab cache for this, especially if
2259 * we have a large number of ports to handle.
2260 */
2261 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2262 if (!drv->state)
2263 goto out;
2264
2265 normal = alloc_tty_driver(drv->nr);
2266 if (!normal)
2267 goto out_kfree;
2268
2269 drv->tty_driver = normal;
2270
2271 normal->owner = drv->owner;
2272 normal->driver_name = drv->driver_name;
2273 normal->name = drv->dev_name;
2274 normal->major = drv->major;
2275 normal->minor_start = drv->minor;
2276 normal->type = TTY_DRIVER_TYPE_SERIAL;
2277 normal->subtype = SERIAL_TYPE_NORMAL;
2278 normal->init_termios = tty_std_termios;
2279 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2280 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2281 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2282 normal->driver_state = drv;
2283 tty_set_operations(normal, &uart_ops);
2284
2285 /*
2286 * Initialise the UART state(s).
2287 */
2288 for (i = 0; i < drv->nr; i++) {
2289 struct uart_state *state = drv->state + i;
2290 struct tty_port *port = &state->port;
2291
2292 tty_port_init(port);
2293 port->ops = &uart_port_ops;
2294 port->close_delay = 500; /* .5 seconds */
2295 port->closing_wait = 30000; /* 30 seconds */
2296 tasklet_init(&state->tlet, uart_tasklet_action,
2297 (unsigned long)state);
2298 }
2299
2300 retval = tty_register_driver(normal);
2301 if (retval >= 0)
2302 return retval;
2303
2304 put_tty_driver(normal);
2305out_kfree:
2306 kfree(drv->state);
2307out:
2308 return -ENOMEM;
2309}
2310
2311/**
2312 * uart_unregister_driver - remove a driver from the uart core layer
2313 * @drv: low level driver structure
2314 *
2315 * Remove all references to a driver from the core driver. The low
2316 * level driver must have removed all its ports via the
2317 * uart_remove_one_port() if it registered them with uart_add_one_port().
2318 * (ie, drv->port == NULL)
2319 */
2320void uart_unregister_driver(struct uart_driver *drv)
2321{
2322 struct tty_driver *p = drv->tty_driver;
2323 tty_unregister_driver(p);
2324 put_tty_driver(p);
2325 kfree(drv->state);
2326 drv->tty_driver = NULL;
2327}
2328
2329struct tty_driver *uart_console_device(struct console *co, int *index)
2330{
2331 struct uart_driver *p = co->data;
2332 *index = co->index;
2333 return p->tty_driver;
2334}
2335
2336/**
2337 * uart_add_one_port - attach a driver-defined port structure
2338 * @drv: pointer to the uart low level driver structure for this port
2339 * @uport: uart port structure to use for this port.
2340 *
2341 * This allows the driver to register its own uart_port structure
2342 * with the core driver. The main purpose is to allow the low
2343 * level uart drivers to expand uart_port, rather than having yet
2344 * more levels of structures.
2345 */
2346int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2347{
2348 struct uart_state *state;
2349 struct tty_port *port;
2350 int ret = 0;
2351 struct device *tty_dev;
2352
2353 BUG_ON(in_interrupt());
2354
2355 if (uport->line >= drv->nr)
2356 return -EINVAL;
2357
2358 state = drv->state + uport->line;
2359 port = &state->port;
2360
2361 mutex_lock(&port_mutex);
2362 mutex_lock(&port->mutex);
2363 if (state->uart_port) {
2364 ret = -EINVAL;
2365 goto out;
2366 }
2367
2368 state->uart_port = uport;
2369 state->pm_state = -1;
2370
2371 uport->cons = drv->cons;
2372 uport->state = state;
2373
2374 /*
2375 * If this port is a console, then the spinlock is already
2376 * initialised.
2377 */
2378 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2379 spin_lock_init(&uport->lock);
2380 lockdep_set_class(&uport->lock, &port_lock_key);
2381 }
2382
2383 uart_configure_port(drv, state, uport);
2384
2385 /*
2386 * Register the port whether it's detected or not. This allows
2387 * setserial to be used to alter this ports parameters.
2388 */
2389 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2390 if (likely(!IS_ERR(tty_dev))) {
2391 device_init_wakeup(tty_dev, 1);
2392 device_set_wakeup_enable(tty_dev, 0);
2393 } else
2394 printk(KERN_ERR "Cannot register tty device on line %d\n",
2395 uport->line);
2396
2397 /*
2398 * Ensure UPF_DEAD is not set.
2399 */
2400 uport->flags &= ~UPF_DEAD;
2401
2402 out:
2403 mutex_unlock(&port->mutex);
2404 mutex_unlock(&port_mutex);
2405
2406 return ret;
2407}
2408
2409/**
2410 * uart_remove_one_port - detach a driver defined port structure
2411 * @drv: pointer to the uart low level driver structure for this port
2412 * @uport: uart port structure for this port
2413 *
2414 * This unhooks (and hangs up) the specified port structure from the
2415 * core driver. No further calls will be made to the low-level code
2416 * for this port.
2417 */
2418int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2419{
2420 struct uart_state *state = drv->state + uport->line;
2421 struct tty_port *port = &state->port;
2422
2423 BUG_ON(in_interrupt());
2424
2425 if (state->uart_port != uport)
2426 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2427 state->uart_port, uport);
2428
2429 mutex_lock(&port_mutex);
2430
2431 /*
2432 * Mark the port "dead" - this prevents any opens from
2433 * succeeding while we shut down the port.
2434 */
2435 mutex_lock(&port->mutex);
2436 uport->flags |= UPF_DEAD;
2437 mutex_unlock(&port->mutex);
2438
2439 /*
2440 * Remove the devices from the tty layer
2441 */
2442 tty_unregister_device(drv->tty_driver, uport->line);
2443
2444 if (port->tty)
2445 tty_vhangup(port->tty);
2446
2447 /*
2448 * Free the port IO and memory resources, if any.
2449 */
2450 if (uport->type != PORT_UNKNOWN)
2451 uport->ops->release_port(uport);
2452
2453 /*
2454 * Indicate that there isn't a port here anymore.
2455 */
2456 uport->type = PORT_UNKNOWN;
2457
2458 /*
2459 * Kill the tasklet, and free resources.
2460 */
2461 tasklet_kill(&state->tlet);
2462
2463 state->uart_port = NULL;
2464 mutex_unlock(&port_mutex);
2465
2466 return 0;
2467}
2468
2469/*
2470 * Are the two ports equivalent?
2471 */
2472int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2473{
2474 if (port1->iotype != port2->iotype)
2475 return 0;
2476
2477 switch (port1->iotype) {
2478 case UPIO_PORT:
2479 return (port1->iobase == port2->iobase);
2480 case UPIO_HUB6:
2481 return (port1->iobase == port2->iobase) &&
2482 (port1->hub6 == port2->hub6);
2483 case UPIO_MEM:
2484 case UPIO_MEM32:
2485 case UPIO_AU:
2486 case UPIO_TSI:
2487 case UPIO_DWAPB:
2488 case UPIO_DWAPB32:
2489 return (port1->mapbase == port2->mapbase);
2490 }
2491 return 0;
2492}
2493EXPORT_SYMBOL(uart_match_port);
2494
2495EXPORT_SYMBOL(uart_write_wakeup);
2496EXPORT_SYMBOL(uart_register_driver);
2497EXPORT_SYMBOL(uart_unregister_driver);
2498EXPORT_SYMBOL(uart_suspend_port);
2499EXPORT_SYMBOL(uart_resume_port);
2500EXPORT_SYMBOL(uart_add_one_port);
2501EXPORT_SYMBOL(uart_remove_one_port);
2502
2503MODULE_DESCRIPTION("Serial driver core");
2504MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_cs.c b/drivers/tty/serial/serial_cs.c
new file mode 100644
index 000000000000..eef736ff810a
--- /dev/null
+++ b/drivers/tty/serial/serial_cs.c
@@ -0,0 +1,870 @@
1/*======================================================================
2
3 A driver for PCMCIA serial devices
4
5 serial_cs.c 1.134 2002/05/04 05:48:53
6
7 The contents of this file are subject to the Mozilla Public
8 License Version 1.1 (the "License"); you may not use this file
9 except in compliance with the License. You may obtain a copy of
10 the License at http://www.mozilla.org/MPL/
11
12 Software distributed under the License is distributed on an "AS
13 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 implied. See the License for the specific language governing
15 rights and limitations under the License.
16
17 The initial developer of the original code is David A. Hinds
18 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
19 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
20
21 Alternatively, the contents of this file may be used under the
22 terms of the GNU General Public License version 2 (the "GPL"), in which
23 case the provisions of the GPL are applicable instead of the
24 above. If you wish to allow the use of your version of this file
25 only under the terms of the GPL and not to allow others to use
26 your version of this file under the MPL, indicate your decision
27 by deleting the provisions above and replace them with the notice
28 and other provisions required by the GPL. If you do not delete
29 the provisions above, a recipient may use your version of this
30 file under either the MPL or the GPL.
31
32======================================================================*/
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/kernel.h>
37#include <linux/init.h>
38#include <linux/ptrace.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/timer.h>
42#include <linux/serial_core.h>
43#include <linux/delay.h>
44#include <linux/major.h>
45#include <asm/io.h>
46#include <asm/system.h>
47
48#include <pcmcia/cistpl.h>
49#include <pcmcia/ciscode.h>
50#include <pcmcia/ds.h>
51#include <pcmcia/cisreg.h>
52
53#include "8250.h"
54
55
56/*====================================================================*/
57
58/* Parameters that can be set with 'insmod' */
59
60/* Enable the speaker? */
61static int do_sound = 1;
62/* Skip strict UART tests? */
63static int buggy_uart;
64
65module_param(do_sound, int, 0444);
66module_param(buggy_uart, int, 0444);
67
68/*====================================================================*/
69
70/* Table of multi-port card ID's */
71
72struct serial_quirk {
73 unsigned int manfid;
74 unsigned int prodid;
75 int multi; /* 1 = multifunction, > 1 = # ports */
76 void (*config)(struct pcmcia_device *);
77 void (*setup)(struct pcmcia_device *, struct uart_port *);
78 void (*wakeup)(struct pcmcia_device *);
79 int (*post)(struct pcmcia_device *);
80};
81
82struct serial_info {
83 struct pcmcia_device *p_dev;
84 int ndev;
85 int multi;
86 int slave;
87 int manfid;
88 int prodid;
89 int c950ctrl;
90 int line[4];
91 const struct serial_quirk *quirk;
92};
93
94struct serial_cfg_mem {
95 tuple_t tuple;
96 cisparse_t parse;
97 u_char buf[256];
98};
99
100/*
101 * vers_1 5.0, "Brain Boxes", "2-Port RS232 card", "r6"
102 * manfid 0x0160, 0x0104
103 * This card appears to have a 14.7456MHz clock.
104 */
105/* Generic Modem: MD55x (GPRS/EDGE) have
106 * Elan VPU16551 UART with 14.7456MHz oscillator
107 * manfid 0x015D, 0x4C45
108 */
109static void quirk_setup_brainboxes_0104(struct pcmcia_device *link, struct uart_port *port)
110{
111 port->uartclk = 14745600;
112}
113
114static int quirk_post_ibm(struct pcmcia_device *link)
115{
116 u8 val;
117 int ret;
118
119 ret = pcmcia_read_config_byte(link, 0x800, &val);
120 if (ret)
121 goto failed;
122
123 ret = pcmcia_write_config_byte(link, 0x800, val | 1);
124 if (ret)
125 goto failed;
126 return 0;
127
128 failed:
129 return -ENODEV;
130}
131
132/*
133 * Nokia cards are not really multiport cards. Shouldn't this
134 * be handled by setting the quirk entry .multi = 0 | 1 ?
135 */
136static void quirk_config_nokia(struct pcmcia_device *link)
137{
138 struct serial_info *info = link->priv;
139
140 if (info->multi > 1)
141 info->multi = 1;
142}
143
144static void quirk_wakeup_oxsemi(struct pcmcia_device *link)
145{
146 struct serial_info *info = link->priv;
147
148 if (info->c950ctrl)
149 outb(12, info->c950ctrl + 1);
150}
151
152/* request_region? oxsemi branch does no request_region too... */
153/*
154 * This sequence is needed to properly initialize MC45 attached to OXCF950.
155 * I tried decreasing these msleep()s, but it worked properly (survived
156 * 1000 stop/start operations) with these timeouts (or bigger).
157 */
158static void quirk_wakeup_possio_gcc(struct pcmcia_device *link)
159{
160 struct serial_info *info = link->priv;
161 unsigned int ctrl = info->c950ctrl;
162
163 outb(0xA, ctrl + 1);
164 msleep(100);
165 outb(0xE, ctrl + 1);
166 msleep(300);
167 outb(0xC, ctrl + 1);
168 msleep(100);
169 outb(0xE, ctrl + 1);
170 msleep(200);
171 outb(0xF, ctrl + 1);
172 msleep(100);
173 outb(0xE, ctrl + 1);
174 msleep(100);
175 outb(0xC, ctrl + 1);
176}
177
178/*
179 * Socket Dual IO: this enables irq's for second port
180 */
181static void quirk_config_socket(struct pcmcia_device *link)
182{
183 struct serial_info *info = link->priv;
184
185 if (info->multi)
186 link->config_flags |= CONF_ENABLE_ESR;
187}
188
189static const struct serial_quirk quirks[] = {
190 {
191 .manfid = 0x0160,
192 .prodid = 0x0104,
193 .multi = -1,
194 .setup = quirk_setup_brainboxes_0104,
195 }, {
196 .manfid = 0x015D,
197 .prodid = 0x4C45,
198 .multi = -1,
199 .setup = quirk_setup_brainboxes_0104,
200 }, {
201 .manfid = MANFID_IBM,
202 .prodid = ~0,
203 .multi = -1,
204 .post = quirk_post_ibm,
205 }, {
206 .manfid = MANFID_INTEL,
207 .prodid = PRODID_INTEL_DUAL_RS232,
208 .multi = 2,
209 }, {
210 .manfid = MANFID_NATINST,
211 .prodid = PRODID_NATINST_QUAD_RS232,
212 .multi = 4,
213 }, {
214 .manfid = MANFID_NOKIA,
215 .prodid = ~0,
216 .multi = -1,
217 .config = quirk_config_nokia,
218 }, {
219 .manfid = MANFID_OMEGA,
220 .prodid = PRODID_OMEGA_QSP_100,
221 .multi = 4,
222 }, {
223 .manfid = MANFID_OXSEMI,
224 .prodid = ~0,
225 .multi = -1,
226 .wakeup = quirk_wakeup_oxsemi,
227 }, {
228 .manfid = MANFID_POSSIO,
229 .prodid = PRODID_POSSIO_GCC,
230 .multi = -1,
231 .wakeup = quirk_wakeup_possio_gcc,
232 }, {
233 .manfid = MANFID_QUATECH,
234 .prodid = PRODID_QUATECH_DUAL_RS232,
235 .multi = 2,
236 }, {
237 .manfid = MANFID_QUATECH,
238 .prodid = PRODID_QUATECH_DUAL_RS232_D1,
239 .multi = 2,
240 }, {
241 .manfid = MANFID_QUATECH,
242 .prodid = PRODID_QUATECH_DUAL_RS232_G,
243 .multi = 2,
244 }, {
245 .manfid = MANFID_QUATECH,
246 .prodid = PRODID_QUATECH_QUAD_RS232,
247 .multi = 4,
248 }, {
249 .manfid = MANFID_SOCKET,
250 .prodid = PRODID_SOCKET_DUAL_RS232,
251 .multi = 2,
252 .config = quirk_config_socket,
253 }, {
254 .manfid = MANFID_SOCKET,
255 .prodid = ~0,
256 .multi = -1,
257 .config = quirk_config_socket,
258 }
259};
260
261
262static int serial_config(struct pcmcia_device * link);
263
264
265static void serial_remove(struct pcmcia_device *link)
266{
267 struct serial_info *info = link->priv;
268 int i;
269
270 dev_dbg(&link->dev, "serial_release\n");
271
272 /*
273 * Recheck to see if the device is still configured.
274 */
275 for (i = 0; i < info->ndev; i++)
276 serial8250_unregister_port(info->line[i]);
277
278 if (!info->slave)
279 pcmcia_disable_device(link);
280}
281
282static int serial_suspend(struct pcmcia_device *link)
283{
284 struct serial_info *info = link->priv;
285 int i;
286
287 for (i = 0; i < info->ndev; i++)
288 serial8250_suspend_port(info->line[i]);
289
290 return 0;
291}
292
293static int serial_resume(struct pcmcia_device *link)
294{
295 struct serial_info *info = link->priv;
296 int i;
297
298 for (i = 0; i < info->ndev; i++)
299 serial8250_resume_port(info->line[i]);
300
301 if (info->quirk && info->quirk->wakeup)
302 info->quirk->wakeup(link);
303
304 return 0;
305}
306
307static int serial_probe(struct pcmcia_device *link)
308{
309 struct serial_info *info;
310
311 dev_dbg(&link->dev, "serial_attach()\n");
312
313 /* Create new serial device */
314 info = kzalloc(sizeof (*info), GFP_KERNEL);
315 if (!info)
316 return -ENOMEM;
317 info->p_dev = link;
318 link->priv = info;
319
320 link->config_flags |= CONF_ENABLE_IRQ;
321 if (do_sound)
322 link->config_flags |= CONF_ENABLE_SPKR;
323
324 return serial_config(link);
325}
326
327static void serial_detach(struct pcmcia_device *link)
328{
329 struct serial_info *info = link->priv;
330
331 dev_dbg(&link->dev, "serial_detach\n");
332
333 /*
334 * Ensure that the ports have been released.
335 */
336 serial_remove(link);
337
338 /* free bits */
339 kfree(info);
340}
341
342/*====================================================================*/
343
344static int setup_serial(struct pcmcia_device *handle, struct serial_info * info,
345 unsigned int iobase, int irq)
346{
347 struct uart_port port;
348 int line;
349
350 memset(&port, 0, sizeof (struct uart_port));
351 port.iobase = iobase;
352 port.irq = irq;
353 port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
354 port.uartclk = 1843200;
355 port.dev = &handle->dev;
356 if (buggy_uart)
357 port.flags |= UPF_BUGGY_UART;
358
359 if (info->quirk && info->quirk->setup)
360 info->quirk->setup(handle, &port);
361
362 line = serial8250_register_port(&port);
363 if (line < 0) {
364 printk(KERN_NOTICE "serial_cs: serial8250_register_port() at "
365 "0x%04lx, irq %d failed\n", (u_long)iobase, irq);
366 return -EINVAL;
367 }
368
369 info->line[info->ndev] = line;
370 info->ndev++;
371
372 return 0;
373}
374
375/*====================================================================*/
376
377static int pfc_config(struct pcmcia_device *p_dev)
378{
379 unsigned int port = 0;
380 struct serial_info *info = p_dev->priv;
381
382 if ((p_dev->resource[1]->end != 0) &&
383 (resource_size(p_dev->resource[1]) == 8)) {
384 port = p_dev->resource[1]->start;
385 info->slave = 1;
386 } else if ((info->manfid == MANFID_OSITECH) &&
387 (resource_size(p_dev->resource[0]) == 0x40)) {
388 port = p_dev->resource[0]->start + 0x28;
389 info->slave = 1;
390 }
391 if (info->slave)
392 return setup_serial(p_dev, info, port, p_dev->irq);
393
394 dev_warn(&p_dev->dev, "no usable port range found, giving up\n");
395 return -ENODEV;
396}
397
398static int simple_config_check(struct pcmcia_device *p_dev, void *priv_data)
399{
400 static const int size_table[2] = { 8, 16 };
401 int *try = priv_data;
402
403 if (p_dev->resource[0]->start == 0)
404 return -ENODEV;
405
406 if ((*try & 0x1) == 0)
407 p_dev->io_lines = 16;
408
409 if (p_dev->resource[0]->end != size_table[(*try >> 1)])
410 return -ENODEV;
411
412 p_dev->resource[0]->end = 8;
413 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
414 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
415
416 return pcmcia_request_io(p_dev);
417}
418
419static int simple_config_check_notpicky(struct pcmcia_device *p_dev,
420 void *priv_data)
421{
422 static const unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
423 int j;
424
425 if (p_dev->io_lines > 3)
426 return -ENODEV;
427
428 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
429 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
430 p_dev->resource[0]->end = 8;
431
432 for (j = 0; j < 5; j++) {
433 p_dev->resource[0]->start = base[j];
434 p_dev->io_lines = base[j] ? 16 : 3;
435 if (!pcmcia_request_io(p_dev))
436 return 0;
437 }
438 return -ENODEV;
439}
440
441static int simple_config(struct pcmcia_device *link)
442{
443 struct serial_info *info = link->priv;
444 int i = -ENODEV, try;
445
446 /* First pass: look for a config entry that looks normal.
447 * Two tries: without IO aliases, then with aliases */
448 link->config_flags |= CONF_AUTO_SET_VPP | CONF_AUTO_SET_IO;
449 for (try = 0; try < 4; try++)
450 if (!pcmcia_loop_config(link, simple_config_check, &try))
451 goto found_port;
452
453 /* Second pass: try to find an entry that isn't picky about
454 its base address, then try to grab any standard serial port
455 address, and finally try to get any free port. */
456 if (!pcmcia_loop_config(link, simple_config_check_notpicky, NULL))
457 goto found_port;
458
459 dev_warn(&link->dev, "no usable port range found, giving up\n");
460 return -1;
461
462found_port:
463 if (info->multi && (info->manfid == MANFID_3COM))
464 link->config_index &= ~(0x08);
465
466 /*
467 * Apply any configuration quirks.
468 */
469 if (info->quirk && info->quirk->config)
470 info->quirk->config(link);
471
472 i = pcmcia_enable_device(link);
473 if (i != 0)
474 return -1;
475 return setup_serial(link, info, link->resource[0]->start, link->irq);
476}
477
478static int multi_config_check(struct pcmcia_device *p_dev, void *priv_data)
479{
480 int *multi = priv_data;
481
482 if (p_dev->resource[1]->end)
483 return -EINVAL;
484
485 /* The quad port cards have bad CIS's, so just look for a
486 window larger than 8 ports and assume it will be right */
487 if (p_dev->resource[0]->end <= 8)
488 return -EINVAL;
489
490 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
491 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
492 p_dev->resource[0]->end = *multi * 8;
493
494 if (pcmcia_request_io(p_dev))
495 return -ENODEV;
496 return 0;
497}
498
499static int multi_config_check_notpicky(struct pcmcia_device *p_dev,
500 void *priv_data)
501{
502 int *base2 = priv_data;
503
504 if (!p_dev->resource[0]->end || !p_dev->resource[1]->end)
505 return -ENODEV;
506
507 p_dev->resource[0]->end = p_dev->resource[1]->end = 8;
508 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
509 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
510
511 if (pcmcia_request_io(p_dev))
512 return -ENODEV;
513
514 *base2 = p_dev->resource[0]->start + 8;
515 return 0;
516}
517
518static int multi_config(struct pcmcia_device *link)
519{
520 struct serial_info *info = link->priv;
521 int i, base2 = 0;
522
523 link->config_flags |= CONF_AUTO_SET_IO;
524 /* First, look for a generic full-sized window */
525 if (!pcmcia_loop_config(link, multi_config_check, &info->multi))
526 base2 = link->resource[0]->start + 8;
527 else {
528 /* If that didn't work, look for two windows */
529 info->multi = 2;
530 if (pcmcia_loop_config(link, multi_config_check_notpicky,
531 &base2)) {
532 dev_warn(&link->dev, "no usable port range "
533 "found, giving up\n");
534 return -ENODEV;
535 }
536 }
537
538 if (!link->irq)
539 dev_warn(&link->dev, "no usable IRQ found, continuing...\n");
540
541 /*
542 * Apply any configuration quirks.
543 */
544 if (info->quirk && info->quirk->config)
545 info->quirk->config(link);
546
547 i = pcmcia_enable_device(link);
548 if (i != 0)
549 return -ENODEV;
550
551 /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
552 * 8 registers are for the UART, the others are extra registers.
553 * Siemen's MC45 PCMCIA (Possio's GCC) is OXCF950 based too.
554 */
555 if (info->manfid == MANFID_OXSEMI || (info->manfid == MANFID_POSSIO &&
556 info->prodid == PRODID_POSSIO_GCC)) {
557 int err;
558
559 if (link->config_index == 1 ||
560 link->config_index == 3) {
561 err = setup_serial(link, info, base2,
562 link->irq);
563 base2 = link->resource[0]->start;
564 } else {
565 err = setup_serial(link, info, link->resource[0]->start,
566 link->irq);
567 }
568 info->c950ctrl = base2;
569
570 /*
571 * FIXME: We really should wake up the port prior to
572 * handing it over to the serial layer.
573 */
574 if (info->quirk && info->quirk->wakeup)
575 info->quirk->wakeup(link);
576
577 return 0;
578 }
579
580 setup_serial(link, info, link->resource[0]->start, link->irq);
581 for (i = 0; i < info->multi - 1; i++)
582 setup_serial(link, info, base2 + (8 * i),
583 link->irq);
584 return 0;
585}
586
587static int serial_check_for_multi(struct pcmcia_device *p_dev, void *priv_data)
588{
589 struct serial_info *info = p_dev->priv;
590
591 if (!p_dev->resource[0]->end)
592 return -EINVAL;
593
594 if ((!p_dev->resource[1]->end) && (p_dev->resource[0]->end % 8 == 0))
595 info->multi = p_dev->resource[0]->end >> 3;
596
597 if ((p_dev->resource[1]->end) && (p_dev->resource[0]->end == 8)
598 && (p_dev->resource[1]->end == 8))
599 info->multi = 2;
600
601 return 0; /* break */
602}
603
604
605static int serial_config(struct pcmcia_device * link)
606{
607 struct serial_info *info = link->priv;
608 int i;
609
610 dev_dbg(&link->dev, "serial_config\n");
611
612 /* Is this a compliant multifunction card? */
613 info->multi = (link->socket->functions > 1);
614
615 /* Is this a multiport card? */
616 info->manfid = link->manf_id;
617 info->prodid = link->card_id;
618
619 for (i = 0; i < ARRAY_SIZE(quirks); i++)
620 if ((quirks[i].manfid == ~0 ||
621 quirks[i].manfid == info->manfid) &&
622 (quirks[i].prodid == ~0 ||
623 quirks[i].prodid == info->prodid)) {
624 info->quirk = &quirks[i];
625 break;
626 }
627
628 /* Another check for dual-serial cards: look for either serial or
629 multifunction cards that ask for appropriate IO port ranges */
630 if ((info->multi == 0) &&
631 (link->has_func_id) &&
632 (link->socket->pcmcia_pfc == 0) &&
633 ((link->func_id == CISTPL_FUNCID_MULTI) ||
634 (link->func_id == CISTPL_FUNCID_SERIAL)))
635 pcmcia_loop_config(link, serial_check_for_multi, info);
636
637 /*
638 * Apply any multi-port quirk.
639 */
640 if (info->quirk && info->quirk->multi != -1)
641 info->multi = info->quirk->multi;
642
643 dev_info(&link->dev,
644 "trying to set up [0x%04x:0x%04x] (pfc: %d, multi: %d, quirk: %p)\n",
645 link->manf_id, link->card_id,
646 link->socket->pcmcia_pfc, info->multi, info->quirk);
647 if (link->socket->pcmcia_pfc)
648 i = pfc_config(link);
649 else if (info->multi > 1)
650 i = multi_config(link);
651 else
652 i = simple_config(link);
653
654 if (i || info->ndev == 0)
655 goto failed;
656
657 /*
658 * Apply any post-init quirk. FIXME: This should really happen
659 * before we register the port, since it might already be in use.
660 */
661 if (info->quirk && info->quirk->post)
662 if (info->quirk->post(link))
663 goto failed;
664
665 return 0;
666
667failed:
668 dev_warn(&link->dev, "failed to initialize\n");
669 serial_remove(link);
670 return -ENODEV;
671}
672
673static const struct pcmcia_device_id serial_ids[] = {
674 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0057, 0x0021),
675 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0089, 0x110a),
676 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0104, 0x000a),
677 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0x0d0a),
678 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0x0e0a),
679 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0105, 0xea15),
680 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0109, 0x0501),
681 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0138, 0x110a),
682 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0140, 0x000a),
683 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0143, 0x3341),
684 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0143, 0xc0ab),
685 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x016c, 0x0081),
686 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x021b, 0x0101),
687 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x08a1, 0xc0ab),
688 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
689 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
690 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
691 PCMCIA_PFC_DEVICE_PROD_ID123(1, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
692 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM28", 0x2e3ee845, 0x0ea978ea),
693 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM33", 0x2e3ee845, 0x80609023),
694 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "CEM56", 0x2e3ee845, 0xa650c32a),
695 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "REM10", 0x2e3ee845, 0x76df1d29),
696 PCMCIA_PFC_DEVICE_PROD_ID13(1, "Xircom", "XEM5600", 0x2e3ee845, 0xf1403719),
697 PCMCIA_PFC_DEVICE_PROD_ID12(1, "AnyCom", "Fast Ethernet + 56K COMBO", 0x578ba6e7, 0xb0ac62c4),
698 PCMCIA_PFC_DEVICE_PROD_ID12(1, "ATKK", "LM33-PCM-T", 0xba9eb7e2, 0x077c174e),
699 PCMCIA_PFC_DEVICE_PROD_ID12(1, "D-Link", "DME336T", 0x1a424a1c, 0xb23897ff),
700 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
701 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Grey Cell", "GCS3000", 0x2a151fac, 0x48b932ae),
702 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Linksys", "EtherFast 10&100 + 56K PC Card (PCMLM56)", 0x0733cc81, 0xb3765033),
703 PCMCIA_PFC_DEVICE_PROD_ID12(1, "LINKSYS", "PCMLM336", 0xf7cb0b07, 0x7a821b58),
704 PCMCIA_PFC_DEVICE_PROD_ID12(1, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
705 PCMCIA_PFC_DEVICE_PROD_ID12(1, "MICRO RESEARCH", "COMBO-L/M-336", 0xb2ced065, 0x3ced0555),
706 PCMCIA_PFC_DEVICE_PROD_ID12(1, "NEC", "PK-UG-J001" ,0x18df0ba0 ,0x831b1064),
707 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
708 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
709 PCMCIA_PFC_DEVICE_PROD_ID12(1, "PCMCIAs", "ComboCard", 0xdcfe12d3, 0xcd8906cc),
710 PCMCIA_PFC_DEVICE_PROD_ID12(1, "PCMCIAs", "LanModem", 0xdcfe12d3, 0xc67c648f),
711 PCMCIA_PFC_DEVICE_PROD_ID12(1, "TDK", "GlobalNetworker 3410/3412", 0x1eae9475, 0xd9a93bed),
712 PCMCIA_PFC_DEVICE_PROD_ID12(1, "Xircom", "CreditCard Ethernet+Modem II", 0x2e3ee845, 0xeca401bf),
713 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0e01),
714 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0a05),
715 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x0b05),
716 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x1101),
717 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0104, 0x0070),
718 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0101, 0x0562),
719 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0104, 0x0070),
720 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x016c, 0x0020),
721 PCMCIA_MFC_DEVICE_PROD_ID123(1, "APEX DATA", "MULTICARD", "ETHERNET-MODEM", 0x11c2da09, 0x7289dc5d, 0xaad95e1f),
722 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away 28.8 PC Card ", 0xb569a6e5, 0x5bd4ff2c),
723 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away Credit Card Adapter", 0xb569a6e5, 0x4bdf15c3),
724 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "w95 Home and Away Credit Card ", 0xb569a6e5, 0xae911c15),
725 PCMCIA_MFC_DEVICE_PROD_ID1(1, "Motorola MARQUIS", 0xf03e4e77),
726 PCMCIA_MFC_DEVICE_PROD_ID2(1, "FAX/Modem/Ethernet Combo Card ", 0x1ed59302),
727 PCMCIA_DEVICE_MANF_CARD(0x0089, 0x0301),
728 PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x0276),
729 PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0039),
730 PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0006),
731 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x0101), /* TDK DF2814 */
732 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x100a), /* Xircom CM-56G */
733 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x3e0a), /* TDK DF5660 */
734 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x410a),
735 PCMCIA_DEVICE_MANF_CARD(0x0107, 0x0002), /* USRobotics 14,400 */
736 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d50),
737 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d51),
738 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d52),
739 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d53),
740 PCMCIA_DEVICE_MANF_CARD(0x010b, 0xd180),
741 PCMCIA_DEVICE_MANF_CARD(0x0115, 0x3330), /* USRobotics/SUN 14,400 */
742 PCMCIA_DEVICE_MANF_CARD(0x0124, 0x0100), /* Nokia DTP-2 ver II */
743 PCMCIA_DEVICE_MANF_CARD(0x0134, 0x5600), /* LASAT COMMUNICATIONS A/S */
744 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x000e),
745 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x001b),
746 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0025),
747 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0045),
748 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0052),
749 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0006), /* Psion 56K+Fax */
750 PCMCIA_DEVICE_MANF_CARD(0x0200, 0x0001), /* MultiMobile */
751 PCMCIA_DEVICE_PROD_ID134("ADV", "TECH", "COMpad-32/85", 0x67459937, 0x916d02ba, 0x8fbe92ae),
752 PCMCIA_DEVICE_PROD_ID124("GATEWAY2000", "CC3144", "PCMCIA MODEM", 0x506bccae, 0xcb3685f1, 0xbd6c43ef),
753 PCMCIA_DEVICE_PROD_ID14("MEGAHERTZ", "PCMCIA MODEM", 0xf510db04, 0xbd6c43ef),
754 PCMCIA_DEVICE_PROD_ID124("TOSHIBA", "T144PF", "PCMCIA MODEM", 0xb4585a1a, 0x7271409c, 0xbd6c43ef),
755 PCMCIA_DEVICE_PROD_ID123("FUJITSU", "FC14F ", "MBH10213", 0x6ee5a3d8, 0x30ead12b, 0xb00f05a0),
756 PCMCIA_DEVICE_PROD_ID123("Novatel Wireless", "Merlin UMTS Modem", "U630", 0x32607776, 0xd9e73b13, 0xe87332e),
757 PCMCIA_DEVICE_PROD_ID13("MEGAHERTZ", "V.34 PCMCIA MODEM", 0xf510db04, 0xbb2cce4a),
758 PCMCIA_DEVICE_PROD_ID12("Brain Boxes", "Bluetooth PC Card", 0xee138382, 0xd4ce9b02),
759 PCMCIA_DEVICE_PROD_ID12("CIRRUS LOGIC", "FAX MODEM", 0xe625f451, 0xcecd6dfa),
760 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 28800 FAX/DATA MODEM", 0xa3a3062c, 0x8cbd7c76),
761 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 33600 FAX/DATA MODEM", 0xa3a3062c, 0x5a00ce95),
762 PCMCIA_DEVICE_PROD_ID12("Computerboards, Inc.", "PCM-COM422", 0xd0b78f51, 0x7e2d49ed),
763 PCMCIA_DEVICE_PROD_ID12("Dr. Neuhaus", "FURY CARD 14K4", 0x76942813, 0x8b96ce65),
764 PCMCIA_DEVICE_PROD_ID12("IBM", "ISDN/56K/GSM", 0xb569a6e5, 0xfee5297b),
765 PCMCIA_DEVICE_PROD_ID12("Intelligent", "ANGIA FAX/MODEM", 0xb496e65e, 0xf31602a6),
766 PCMCIA_DEVICE_PROD_ID12("Intel", "MODEM 2400+", 0x816cc815, 0x412729fb),
767 PCMCIA_DEVICE_PROD_ID12("Intertex", "IX34-PCMCIA", 0xf8a097e3, 0x97880447),
768 PCMCIA_DEVICE_PROD_ID12("IOTech Inc ", "PCMCIA Dual RS-232 Serial Port Card", 0x3bd2d898, 0x92abc92f),
769 PCMCIA_DEVICE_PROD_ID12("MACRONIX", "FAX/MODEM", 0x668388b3, 0x3f9bdf2f),
770 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT1432LT", 0x5f73be51, 0x0b3e2383),
771 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT2834LT", 0x5f73be51, 0x4cd7c09e),
772 PCMCIA_DEVICE_PROD_ID12("OEM ", "C288MX ", 0xb572d360, 0xd2385b7a),
773 PCMCIA_DEVICE_PROD_ID12("Option International", "V34bis GSM/PSTN Data/Fax Modem", 0x9d7cd6f5, 0x5cb8bf41),
774 PCMCIA_DEVICE_PROD_ID12("PCMCIA ", "C336MX ", 0x99bcafe9, 0xaa25bcab),
775 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "PCMCIA Dual RS-232 Serial Port Card", 0xc4420b35, 0x92abc92f),
776 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "Dual RS-232 Serial Port PC Card", 0xc4420b35, 0x031a380d),
777 PCMCIA_DEVICE_PROD_ID12("Telia", "SurfinBird 560P/A+", 0xe2cdd5e, 0xc9314b38),
778 PCMCIA_DEVICE_PROD_ID1("Smart Serial Port", 0x2d8ce292),
779 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "EN2218-LAN/MODEM", 0x281f1c5d, 0x570f348e, "cis/PCMLM28.cis"),
780 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "UE2218-LAN/MODEM", 0x281f1c5d, 0x6fdcacee, "cis/PCMLM28.cis"),
781 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet", 0xf5f025c2, 0x338e8155, "cis/PCMLM28.cis"),
782 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet GSM", 0xf5f025c2, 0x4ae85d35, "cis/PCMLM28.cis"),
783 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "LINKSYS", "PCMLM28", 0xf7cb0b07, 0x66881874, "cis/PCMLM28.cis"),
784 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "TOSHIBA", "Modem/LAN Card", 0xb4585a1a, 0x53f922f8, "cis/PCMLM28.cis"),
785 PCMCIA_MFC_DEVICE_CIS_PROD_ID12(1, "DAYNA COMMUNICATIONS", "LAN AND MODEM MULTIFUNCTION", 0x8fdf8f89, 0xdd5ed9e8, "cis/DP83903.cis"),
786 PCMCIA_MFC_DEVICE_CIS_PROD_ID4(1, "NSC MF LAN/Modem", 0x58fc6056, "cis/DP83903.cis"),
787 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0556, "cis/3CCFEM556.cis"),
788 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0175, 0x0000, "cis/DP83903.cis"),
789 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0035, "cis/3CXEM556.cis"),
790 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x003d, "cis/3CXEM556.cis"),
791 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC850", 0xd85f6206, 0x42a2c018, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC850 3G Network Adapter R1 */
792 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC860", 0xd85f6206, 0x698f93db, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC860 3G Network Adapter R1 */
793 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC710/AC750", 0xd85f6206, 0x761b11e0, "cis/SW_7xx_SER.cis"), /* Sierra Wireless AC710/AC750 GPRS Network Adapter R1 */
794 PCMCIA_DEVICE_CIS_MANF_CARD(0x0192, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- pre update */
795 PCMCIA_DEVICE_CIS_MANF_CARD(0x013f, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- post update */
796 PCMCIA_DEVICE_CIS_PROD_ID12("MultiTech", "PCMCIA 56K DataFax", 0x842047ee, 0xc2efcf03, "cis/MT5634ZLX.cis"),
797 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-2", 0x96913a85, 0x27ab5437, "cis/COMpad2.cis"),
798 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-4", 0x96913a85, 0xcec8f102, "cis/COMpad4.cis"),
799 PCMCIA_DEVICE_CIS_PROD_ID123("ADVANTECH", "COMpad-32/85", "1.0", 0x96913a85, 0x8fbe92ae, 0x0877b627, "cis/COMpad2.cis"),
800 PCMCIA_DEVICE_CIS_PROD_ID2("RS-COM 2P", 0xad20b156, "cis/RS-COM-2P.cis"),
801 PCMCIA_DEVICE_CIS_MANF_CARD(0x0013, 0x0000, "cis/GLOBETROTTER.cis"),
802 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100 1.00.",0x19ca78af,0xf964f42b),
803 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100",0x19ca78af,0x71d98e83),
804 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232 1.00.",0x19ca78af,0x69fb7490),
805 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232",0x19ca78af,0xb6bc0235),
806 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232",0x63f2e0bd,0xb9e175d3),
807 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232-5",0x63f2e0bd,0xfce33442),
808 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232",0x3beb8cf2,0x171e7190),
809 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232-5",0x3beb8cf2,0x20da4262),
810 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF428",0x3beb8cf2,0xea5dd57d),
811 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF500",0x3beb8cf2,0xd77255fa),
812 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: IC232",0x3beb8cf2,0x6a709903),
813 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: SL232",0x3beb8cf2,0x18430676),
814 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: XL232",0x3beb8cf2,0x6f933767),
815 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
816 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
817 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
818 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
819 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial+Parallel Port: SP230",0x3beb8cf2,0xdb9e58bc),
820 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
821 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
822 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
823 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
824 PCMCIA_MFC_DEVICE_PROD_ID12(2,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
825 PCMCIA_MFC_DEVICE_PROD_ID12(3,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
826 PCMCIA_DEVICE_MANF_CARD(0x0279, 0x950b),
827 /* too generic */
828 /* PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0160, 0x0002), */
829 /* PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0160, 0x0002), */
830 PCMCIA_DEVICE_FUNC_ID(2),
831 PCMCIA_DEVICE_NULL,
832};
833MODULE_DEVICE_TABLE(pcmcia, serial_ids);
834
835MODULE_FIRMWARE("cis/PCMLM28.cis");
836MODULE_FIRMWARE("cis/DP83903.cis");
837MODULE_FIRMWARE("cis/3CCFEM556.cis");
838MODULE_FIRMWARE("cis/3CXEM556.cis");
839MODULE_FIRMWARE("cis/SW_8xx_SER.cis");
840MODULE_FIRMWARE("cis/SW_7xx_SER.cis");
841MODULE_FIRMWARE("cis/SW_555_SER.cis");
842MODULE_FIRMWARE("cis/MT5634ZLX.cis");
843MODULE_FIRMWARE("cis/COMpad2.cis");
844MODULE_FIRMWARE("cis/COMpad4.cis");
845MODULE_FIRMWARE("cis/RS-COM-2P.cis");
846
847static struct pcmcia_driver serial_cs_driver = {
848 .owner = THIS_MODULE,
849 .name = "serial_cs",
850 .probe = serial_probe,
851 .remove = serial_detach,
852 .id_table = serial_ids,
853 .suspend = serial_suspend,
854 .resume = serial_resume,
855};
856
857static int __init init_serial_cs(void)
858{
859 return pcmcia_register_driver(&serial_cs_driver);
860}
861
862static void __exit exit_serial_cs(void)
863{
864 pcmcia_unregister_driver(&serial_cs_driver);
865}
866
867module_init(init_serial_cs);
868module_exit(exit_serial_cs);
869
870MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_ks8695.c b/drivers/tty/serial/serial_ks8695.c
new file mode 100644
index 000000000000..2430319f2f52
--- /dev/null
+++ b/drivers/tty/serial/serial_ks8695.c
@@ -0,0 +1,703 @@
1/*
2 * Driver for KS8695 serial ports
3 *
4 * Based on drivers/serial/serial_amba.c, by Kam Lee.
5 *
6 * Copyright 2002-2005 Micrel Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14#include <linux/module.h>
15#include <linux/tty.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/serial.h>
19#include <linux/console.h>
20#include <linux/sysrq.h>
21#include <linux/device.h>
22
23#include <asm/io.h>
24#include <asm/irq.h>
25#include <asm/mach/irq.h>
26
27#include <mach/regs-uart.h>
28#include <mach/regs-irq.h>
29
30#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/serial_core.h>
35
36
37#define SERIAL_KS8695_MAJOR 204
38#define SERIAL_KS8695_MINOR 16
39#define SERIAL_KS8695_DEVNAME "ttyAM"
40
41#define SERIAL_KS8695_NR 1
42
43/*
44 * Access macros for the KS8695 UART
45 */
46#define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
47#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
48#define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
49#define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
50#define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
51#define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
52#define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
53#define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
54#define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
55#define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
56#define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
57#define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
58
59#define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
60
61#define UART_DUMMY_LSR_RX 0x100
62#define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
63
64static inline int tx_enabled(struct uart_port *port)
65{
66 return port->unused[0] & 1;
67}
68
69static inline int rx_enabled(struct uart_port *port)
70{
71 return port->unused[0] & 2;
72}
73
74static inline int ms_enabled(struct uart_port *port)
75{
76 return port->unused[0] & 4;
77}
78
79static inline void ms_enable(struct uart_port *port, int enabled)
80{
81 if(enabled)
82 port->unused[0] |= 4;
83 else
84 port->unused[0] &= ~4;
85}
86
87static inline void rx_enable(struct uart_port *port, int enabled)
88{
89 if(enabled)
90 port->unused[0] |= 2;
91 else
92 port->unused[0] &= ~2;
93}
94
95static inline void tx_enable(struct uart_port *port, int enabled)
96{
97 if(enabled)
98 port->unused[0] |= 1;
99 else
100 port->unused[0] &= ~1;
101}
102
103
104#ifdef SUPPORT_SYSRQ
105static struct console ks8695_console;
106#endif
107
108static void ks8695uart_stop_tx(struct uart_port *port)
109{
110 if (tx_enabled(port)) {
111 /* use disable_irq_nosync() and not disable_irq() to avoid self
112 * imposed deadlock by not waiting for irq handler to end,
113 * since this ks8695uart_stop_tx() is called from interrupt context.
114 */
115 disable_irq_nosync(KS8695_IRQ_UART_TX);
116 tx_enable(port, 0);
117 }
118}
119
120static void ks8695uart_start_tx(struct uart_port *port)
121{
122 if (!tx_enabled(port)) {
123 enable_irq(KS8695_IRQ_UART_TX);
124 tx_enable(port, 1);
125 }
126}
127
128static void ks8695uart_stop_rx(struct uart_port *port)
129{
130 if (rx_enabled(port)) {
131 disable_irq(KS8695_IRQ_UART_RX);
132 rx_enable(port, 0);
133 }
134}
135
136static void ks8695uart_enable_ms(struct uart_port *port)
137{
138 if (!ms_enabled(port)) {
139 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
140 ms_enable(port,1);
141 }
142}
143
144static void ks8695uart_disable_ms(struct uart_port *port)
145{
146 if (ms_enabled(port)) {
147 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
148 ms_enable(port,0);
149 }
150}
151
152static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
153{
154 struct uart_port *port = dev_id;
155 struct tty_struct *tty = port->state->port.tty;
156 unsigned int status, ch, lsr, flg, max_count = 256;
157
158 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
159 while ((status & URLS_URDR) && max_count--) {
160 ch = UART_GET_CHAR(port);
161 flg = TTY_NORMAL;
162
163 port->icount.rx++;
164
165 /*
166 * Note that the error handling code is
167 * out of the main execution path
168 */
169 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
170 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
171 if (lsr & URLS_URBI) {
172 lsr &= ~(URLS_URFE | URLS_URPE);
173 port->icount.brk++;
174 if (uart_handle_break(port))
175 goto ignore_char;
176 }
177 if (lsr & URLS_URPE)
178 port->icount.parity++;
179 if (lsr & URLS_URFE)
180 port->icount.frame++;
181 if (lsr & URLS_URROE)
182 port->icount.overrun++;
183
184 lsr &= port->read_status_mask;
185
186 if (lsr & URLS_URBI)
187 flg = TTY_BREAK;
188 else if (lsr & URLS_URPE)
189 flg = TTY_PARITY;
190 else if (lsr & URLS_URFE)
191 flg = TTY_FRAME;
192 }
193
194 if (uart_handle_sysrq_char(port, ch))
195 goto ignore_char;
196
197 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
198
199ignore_char:
200 status = UART_GET_LSR(port);
201 }
202 tty_flip_buffer_push(tty);
203
204 return IRQ_HANDLED;
205}
206
207
208static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
209{
210 struct uart_port *port = dev_id;
211 struct circ_buf *xmit = &port->state->xmit;
212 unsigned int count;
213
214 if (port->x_char) {
215 KS8695_CLR_TX_INT();
216 UART_PUT_CHAR(port, port->x_char);
217 port->icount.tx++;
218 port->x_char = 0;
219 return IRQ_HANDLED;
220 }
221
222 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
223 ks8695uart_stop_tx(port);
224 return IRQ_HANDLED;
225 }
226
227 count = 16; /* fifo size */
228 while (!uart_circ_empty(xmit) && (count-- > 0)) {
229 KS8695_CLR_TX_INT();
230 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
231
232 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
233 port->icount.tx++;
234 }
235
236 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
237 uart_write_wakeup(port);
238
239 if (uart_circ_empty(xmit))
240 ks8695uart_stop_tx(port);
241
242 return IRQ_HANDLED;
243}
244
245static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
246{
247 struct uart_port *port = dev_id;
248 unsigned int status;
249
250 /*
251 * clear modem interrupt by reading MSR
252 */
253 status = UART_GET_MSR(port);
254
255 if (status & URMS_URDDCD)
256 uart_handle_dcd_change(port, status & URMS_URDDCD);
257
258 if (status & URMS_URDDST)
259 port->icount.dsr++;
260
261 if (status & URMS_URDCTS)
262 uart_handle_cts_change(port, status & URMS_URDCTS);
263
264 if (status & URMS_URTERI)
265 port->icount.rng++;
266
267 wake_up_interruptible(&port->state->port.delta_msr_wait);
268
269 return IRQ_HANDLED;
270}
271
272static unsigned int ks8695uart_tx_empty(struct uart_port *port)
273{
274 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
275}
276
277static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
278{
279 unsigned int result = 0;
280 unsigned int status;
281
282 status = UART_GET_MSR(port);
283 if (status & URMS_URDCD)
284 result |= TIOCM_CAR;
285 if (status & URMS_URDSR)
286 result |= TIOCM_DSR;
287 if (status & URMS_URCTS)
288 result |= TIOCM_CTS;
289 if (status & URMS_URRI)
290 result |= TIOCM_RI;
291
292 return result;
293}
294
295static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
296{
297 unsigned int mcr;
298
299 mcr = UART_GET_MCR(port);
300 if (mctrl & TIOCM_RTS)
301 mcr |= URMC_URRTS;
302 else
303 mcr &= ~URMC_URRTS;
304
305 if (mctrl & TIOCM_DTR)
306 mcr |= URMC_URDTR;
307 else
308 mcr &= ~URMC_URDTR;
309
310 UART_PUT_MCR(port, mcr);
311}
312
313static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
314{
315 unsigned int lcr;
316
317 lcr = UART_GET_LCR(port);
318
319 if (break_state == -1)
320 lcr |= URLC_URSBC;
321 else
322 lcr &= ~URLC_URSBC;
323
324 UART_PUT_LCR(port, lcr);
325}
326
327static int ks8695uart_startup(struct uart_port *port)
328{
329 int retval;
330
331 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
332 tx_enable(port, 0);
333 rx_enable(port, 1);
334 ms_enable(port, 1);
335
336 /*
337 * Allocate the IRQ
338 */
339 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
340 if (retval)
341 goto err_tx;
342
343 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
344 if (retval)
345 goto err_rx;
346
347 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
348 if (retval)
349 goto err_ls;
350
351 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
352 if (retval)
353 goto err_ms;
354
355 return 0;
356
357err_ms:
358 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
359err_ls:
360 free_irq(KS8695_IRQ_UART_RX, port);
361err_rx:
362 free_irq(KS8695_IRQ_UART_TX, port);
363err_tx:
364 return retval;
365}
366
367static void ks8695uart_shutdown(struct uart_port *port)
368{
369 /*
370 * Free the interrupt
371 */
372 free_irq(KS8695_IRQ_UART_RX, port);
373 free_irq(KS8695_IRQ_UART_TX, port);
374 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
375 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
376
377 /* disable break condition and fifos */
378 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
379 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
380}
381
382static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
383{
384 unsigned int lcr, fcr = 0;
385 unsigned long flags;
386 unsigned int baud, quot;
387
388 /*
389 * Ask the core to calculate the divisor for us.
390 */
391 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
392 quot = uart_get_divisor(port, baud);
393
394 switch (termios->c_cflag & CSIZE) {
395 case CS5:
396 lcr = URCL_5;
397 break;
398 case CS6:
399 lcr = URCL_6;
400 break;
401 case CS7:
402 lcr = URCL_7;
403 break;
404 default:
405 lcr = URCL_8;
406 break;
407 }
408
409 /* stop bits */
410 if (termios->c_cflag & CSTOPB)
411 lcr |= URLC_URSB;
412
413 /* parity */
414 if (termios->c_cflag & PARENB) {
415 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
416 if (termios->c_cflag & PARODD)
417 lcr |= URPE_MARK;
418 else
419 lcr |= URPE_SPACE;
420 }
421 else if (termios->c_cflag & PARODD)
422 lcr |= URPE_ODD;
423 else
424 lcr |= URPE_EVEN;
425 }
426
427 if (port->fifosize > 1)
428 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
429
430 spin_lock_irqsave(&port->lock, flags);
431
432 /*
433 * Update the per-port timeout.
434 */
435 uart_update_timeout(port, termios->c_cflag, baud);
436
437 port->read_status_mask = URLS_URROE;
438 if (termios->c_iflag & INPCK)
439 port->read_status_mask |= (URLS_URFE | URLS_URPE);
440 if (termios->c_iflag & (BRKINT | PARMRK))
441 port->read_status_mask |= URLS_URBI;
442
443 /*
444 * Characters to ignore
445 */
446 port->ignore_status_mask = 0;
447 if (termios->c_iflag & IGNPAR)
448 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
449 if (termios->c_iflag & IGNBRK) {
450 port->ignore_status_mask |= URLS_URBI;
451 /*
452 * If we're ignoring parity and break indicators,
453 * ignore overruns too (for real raw support).
454 */
455 if (termios->c_iflag & IGNPAR)
456 port->ignore_status_mask |= URLS_URROE;
457 }
458
459 /*
460 * Ignore all characters if CREAD is not set.
461 */
462 if ((termios->c_cflag & CREAD) == 0)
463 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
464
465 /* first, disable everything */
466 if (UART_ENABLE_MS(port, termios->c_cflag))
467 ks8695uart_enable_ms(port);
468 else
469 ks8695uart_disable_ms(port);
470
471 /* Set baud rate */
472 UART_PUT_BRDR(port, quot);
473
474 UART_PUT_LCR(port, lcr);
475 UART_PUT_FCR(port, fcr);
476
477 spin_unlock_irqrestore(&port->lock, flags);
478}
479
480static const char *ks8695uart_type(struct uart_port *port)
481{
482 return port->type == PORT_KS8695 ? "KS8695" : NULL;
483}
484
485/*
486 * Release the memory region(s) being used by 'port'
487 */
488static void ks8695uart_release_port(struct uart_port *port)
489{
490 release_mem_region(port->mapbase, UART_PORT_SIZE);
491}
492
493/*
494 * Request the memory region(s) being used by 'port'
495 */
496static int ks8695uart_request_port(struct uart_port *port)
497{
498 return request_mem_region(port->mapbase, UART_PORT_SIZE,
499 "serial_ks8695") != NULL ? 0 : -EBUSY;
500}
501
502/*
503 * Configure/autoconfigure the port.
504 */
505static void ks8695uart_config_port(struct uart_port *port, int flags)
506{
507 if (flags & UART_CONFIG_TYPE) {
508 port->type = PORT_KS8695;
509 ks8695uart_request_port(port);
510 }
511}
512
513/*
514 * verify the new serial_struct (for TIOCSSERIAL).
515 */
516static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
517{
518 int ret = 0;
519
520 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
521 ret = -EINVAL;
522 if (ser->irq != port->irq)
523 ret = -EINVAL;
524 if (ser->baud_base < 9600)
525 ret = -EINVAL;
526 return ret;
527}
528
529static struct uart_ops ks8695uart_pops = {
530 .tx_empty = ks8695uart_tx_empty,
531 .set_mctrl = ks8695uart_set_mctrl,
532 .get_mctrl = ks8695uart_get_mctrl,
533 .stop_tx = ks8695uart_stop_tx,
534 .start_tx = ks8695uart_start_tx,
535 .stop_rx = ks8695uart_stop_rx,
536 .enable_ms = ks8695uart_enable_ms,
537 .break_ctl = ks8695uart_break_ctl,
538 .startup = ks8695uart_startup,
539 .shutdown = ks8695uart_shutdown,
540 .set_termios = ks8695uart_set_termios,
541 .type = ks8695uart_type,
542 .release_port = ks8695uart_release_port,
543 .request_port = ks8695uart_request_port,
544 .config_port = ks8695uart_config_port,
545 .verify_port = ks8695uart_verify_port,
546};
547
548static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
549 {
550 .membase = (void *) KS8695_UART_VA,
551 .mapbase = KS8695_UART_VA,
552 .iotype = SERIAL_IO_MEM,
553 .irq = KS8695_IRQ_UART_TX,
554 .uartclk = KS8695_CLOCK_RATE * 16,
555 .fifosize = 16,
556 .ops = &ks8695uart_pops,
557 .flags = ASYNC_BOOT_AUTOCONF,
558 .line = 0,
559 }
560};
561
562#ifdef CONFIG_SERIAL_KS8695_CONSOLE
563static void ks8695_console_putchar(struct uart_port *port, int ch)
564{
565 while (!(UART_GET_LSR(port) & URLS_URTHRE))
566 barrier();
567
568 UART_PUT_CHAR(port, ch);
569}
570
571static void ks8695_console_write(struct console *co, const char *s, u_int count)
572{
573 struct uart_port *port = ks8695uart_ports + co->index;
574
575 uart_console_write(port, s, count, ks8695_console_putchar);
576}
577
578static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
579{
580 unsigned int lcr;
581
582 lcr = UART_GET_LCR(port);
583
584 switch (lcr & URLC_PARITY) {
585 case URPE_ODD:
586 *parity = 'o';
587 break;
588 case URPE_EVEN:
589 *parity = 'e';
590 break;
591 default:
592 *parity = 'n';
593 }
594
595 switch (lcr & URLC_URCL) {
596 case URCL_5:
597 *bits = 5;
598 break;
599 case URCL_6:
600 *bits = 6;
601 break;
602 case URCL_7:
603 *bits = 7;
604 break;
605 default:
606 *bits = 8;
607 }
608
609 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
610 *baud /= 16;
611 *baud &= 0xFFFFFFF0;
612}
613
614static int __init ks8695_console_setup(struct console *co, char *options)
615{
616 struct uart_port *port;
617 int baud = 115200;
618 int bits = 8;
619 int parity = 'n';
620 int flow = 'n';
621
622 /*
623 * Check whether an invalid uart number has been specified, and
624 * if so, search for the first available port that does have
625 * console support.
626 */
627 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
628
629 if (options)
630 uart_parse_options(options, &baud, &parity, &bits, &flow);
631 else
632 ks8695_console_get_options(port, &baud, &parity, &bits);
633
634 return uart_set_options(port, co, baud, parity, bits, flow);
635}
636
637static struct uart_driver ks8695_reg;
638
639static struct console ks8695_console = {
640 .name = SERIAL_KS8695_DEVNAME,
641 .write = ks8695_console_write,
642 .device = uart_console_device,
643 .setup = ks8695_console_setup,
644 .flags = CON_PRINTBUFFER,
645 .index = -1,
646 .data = &ks8695_reg,
647};
648
649static int __init ks8695_console_init(void)
650{
651 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
652 register_console(&ks8695_console);
653 return 0;
654}
655
656console_initcall(ks8695_console_init);
657
658#define KS8695_CONSOLE &ks8695_console
659#else
660#define KS8695_CONSOLE NULL
661#endif
662
663static struct uart_driver ks8695_reg = {
664 .owner = THIS_MODULE,
665 .driver_name = "serial_ks8695",
666 .dev_name = SERIAL_KS8695_DEVNAME,
667 .major = SERIAL_KS8695_MAJOR,
668 .minor = SERIAL_KS8695_MINOR,
669 .nr = SERIAL_KS8695_NR,
670 .cons = KS8695_CONSOLE,
671};
672
673static int __init ks8695uart_init(void)
674{
675 int i, ret;
676
677 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
678
679 ret = uart_register_driver(&ks8695_reg);
680 if (ret)
681 return ret;
682
683 for (i = 0; i < SERIAL_KS8695_NR; i++)
684 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
685
686 return 0;
687}
688
689static void __exit ks8695uart_exit(void)
690{
691 int i;
692
693 for (i = 0; i < SERIAL_KS8695_NR; i++)
694 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
695 uart_unregister_driver(&ks8695_reg);
696}
697
698module_init(ks8695uart_init);
699module_exit(ks8695uart_exit);
700
701MODULE_DESCRIPTION("KS8695 serial port driver");
702MODULE_AUTHOR("Micrel Inc.");
703MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_txx9.c b/drivers/tty/serial/serial_txx9.c
new file mode 100644
index 000000000000..8e3fc1944e6d
--- /dev/null
+++ b/drivers/tty/serial/serial_txx9.c
@@ -0,0 +1,1342 @@
1/*
2 * Derived from many drivers using generic_serial interface,
3 * especially serial_tx3912.c by Steven J. Hill and r39xx_serial.c
4 * (was in Linux/VR tree) by Jim Pick.
5 *
6 * Copyright (C) 1999 Harald Koerfgen
7 * Copyright (C) 2000 Jim Pick <jim@jimpick.com>
8 * Copyright (C) 2001 Steven J. Hill (sjhill@realitydiluted.com)
9 * Copyright (C) 2000-2002 Toshiba Corporation
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * Serial driver for TX3927/TX4927/TX4925/TX4938 internal SIO controller
16 */
17
18#if defined(CONFIG_SERIAL_TXX9_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
22#include <linux/module.h>
23#include <linux/ioport.h>
24#include <linux/init.h>
25#include <linux/console.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/pci.h>
29#include <linux/serial_core.h>
30#include <linux/serial.h>
31
32#include <asm/io.h>
33
34static char *serial_version = "1.11";
35static char *serial_name = "TX39/49 Serial driver";
36
37#define PASS_LIMIT 256
38
39#if !defined(CONFIG_SERIAL_TXX9_STDSERIAL)
40/* "ttyS" is used for standard serial driver */
41#define TXX9_TTY_NAME "ttyTX"
42#define TXX9_TTY_MINOR_START 196
43#define TXX9_TTY_MAJOR 204
44#else
45/* acts like standard serial driver */
46#define TXX9_TTY_NAME "ttyS"
47#define TXX9_TTY_MINOR_START 64
48#define TXX9_TTY_MAJOR TTY_MAJOR
49#endif
50
51/* flag aliases */
52#define UPF_TXX9_HAVE_CTS_LINE UPF_BUGGY_UART
53#define UPF_TXX9_USE_SCLK UPF_MAGIC_MULTIPLIER
54
55#ifdef CONFIG_PCI
56/* support for Toshiba TC86C001 SIO */
57#define ENABLE_SERIAL_TXX9_PCI
58#endif
59
60/*
61 * Number of serial ports
62 */
63#define UART_NR CONFIG_SERIAL_TXX9_NR_UARTS
64
65struct uart_txx9_port {
66 struct uart_port port;
67 /* No additional info for now */
68};
69
70#define TXX9_REGION_SIZE 0x24
71
72/* TXX9 Serial Registers */
73#define TXX9_SILCR 0x00
74#define TXX9_SIDICR 0x04
75#define TXX9_SIDISR 0x08
76#define TXX9_SICISR 0x0c
77#define TXX9_SIFCR 0x10
78#define TXX9_SIFLCR 0x14
79#define TXX9_SIBGR 0x18
80#define TXX9_SITFIFO 0x1c
81#define TXX9_SIRFIFO 0x20
82
83/* SILCR : Line Control */
84#define TXX9_SILCR_SCS_MASK 0x00000060
85#define TXX9_SILCR_SCS_IMCLK 0x00000000
86#define TXX9_SILCR_SCS_IMCLK_BG 0x00000020
87#define TXX9_SILCR_SCS_SCLK 0x00000040
88#define TXX9_SILCR_SCS_SCLK_BG 0x00000060
89#define TXX9_SILCR_UEPS 0x00000010
90#define TXX9_SILCR_UPEN 0x00000008
91#define TXX9_SILCR_USBL_MASK 0x00000004
92#define TXX9_SILCR_USBL_1BIT 0x00000000
93#define TXX9_SILCR_USBL_2BIT 0x00000004
94#define TXX9_SILCR_UMODE_MASK 0x00000003
95#define TXX9_SILCR_UMODE_8BIT 0x00000000
96#define TXX9_SILCR_UMODE_7BIT 0x00000001
97
98/* SIDICR : DMA/Int. Control */
99#define TXX9_SIDICR_TDE 0x00008000
100#define TXX9_SIDICR_RDE 0x00004000
101#define TXX9_SIDICR_TIE 0x00002000
102#define TXX9_SIDICR_RIE 0x00001000
103#define TXX9_SIDICR_SPIE 0x00000800
104#define TXX9_SIDICR_CTSAC 0x00000600
105#define TXX9_SIDICR_STIE_MASK 0x0000003f
106#define TXX9_SIDICR_STIE_OERS 0x00000020
107#define TXX9_SIDICR_STIE_CTSS 0x00000010
108#define TXX9_SIDICR_STIE_RBRKD 0x00000008
109#define TXX9_SIDICR_STIE_TRDY 0x00000004
110#define TXX9_SIDICR_STIE_TXALS 0x00000002
111#define TXX9_SIDICR_STIE_UBRKD 0x00000001
112
113/* SIDISR : DMA/Int. Status */
114#define TXX9_SIDISR_UBRK 0x00008000
115#define TXX9_SIDISR_UVALID 0x00004000
116#define TXX9_SIDISR_UFER 0x00002000
117#define TXX9_SIDISR_UPER 0x00001000
118#define TXX9_SIDISR_UOER 0x00000800
119#define TXX9_SIDISR_ERI 0x00000400
120#define TXX9_SIDISR_TOUT 0x00000200
121#define TXX9_SIDISR_TDIS 0x00000100
122#define TXX9_SIDISR_RDIS 0x00000080
123#define TXX9_SIDISR_STIS 0x00000040
124#define TXX9_SIDISR_RFDN_MASK 0x0000001f
125
126/* SICISR : Change Int. Status */
127#define TXX9_SICISR_OERS 0x00000020
128#define TXX9_SICISR_CTSS 0x00000010
129#define TXX9_SICISR_RBRKD 0x00000008
130#define TXX9_SICISR_TRDY 0x00000004
131#define TXX9_SICISR_TXALS 0x00000002
132#define TXX9_SICISR_UBRKD 0x00000001
133
134/* SIFCR : FIFO Control */
135#define TXX9_SIFCR_SWRST 0x00008000
136#define TXX9_SIFCR_RDIL_MASK 0x00000180
137#define TXX9_SIFCR_RDIL_1 0x00000000
138#define TXX9_SIFCR_RDIL_4 0x00000080
139#define TXX9_SIFCR_RDIL_8 0x00000100
140#define TXX9_SIFCR_RDIL_12 0x00000180
141#define TXX9_SIFCR_RDIL_MAX 0x00000180
142#define TXX9_SIFCR_TDIL_MASK 0x00000018
143#define TXX9_SIFCR_TDIL_MASK 0x00000018
144#define TXX9_SIFCR_TDIL_1 0x00000000
145#define TXX9_SIFCR_TDIL_4 0x00000001
146#define TXX9_SIFCR_TDIL_8 0x00000010
147#define TXX9_SIFCR_TDIL_MAX 0x00000010
148#define TXX9_SIFCR_TFRST 0x00000004
149#define TXX9_SIFCR_RFRST 0x00000002
150#define TXX9_SIFCR_FRSTE 0x00000001
151#define TXX9_SIO_TX_FIFO 8
152#define TXX9_SIO_RX_FIFO 16
153
154/* SIFLCR : Flow Control */
155#define TXX9_SIFLCR_RCS 0x00001000
156#define TXX9_SIFLCR_TES 0x00000800
157#define TXX9_SIFLCR_RTSSC 0x00000200
158#define TXX9_SIFLCR_RSDE 0x00000100
159#define TXX9_SIFLCR_TSDE 0x00000080
160#define TXX9_SIFLCR_RTSTL_MASK 0x0000001e
161#define TXX9_SIFLCR_RTSTL_MAX 0x0000001e
162#define TXX9_SIFLCR_TBRK 0x00000001
163
164/* SIBGR : Baudrate Control */
165#define TXX9_SIBGR_BCLK_MASK 0x00000300
166#define TXX9_SIBGR_BCLK_T0 0x00000000
167#define TXX9_SIBGR_BCLK_T2 0x00000100
168#define TXX9_SIBGR_BCLK_T4 0x00000200
169#define TXX9_SIBGR_BCLK_T6 0x00000300
170#define TXX9_SIBGR_BRD_MASK 0x000000ff
171
172static inline unsigned int sio_in(struct uart_txx9_port *up, int offset)
173{
174 switch (up->port.iotype) {
175 default:
176 return __raw_readl(up->port.membase + offset);
177 case UPIO_PORT:
178 return inl(up->port.iobase + offset);
179 }
180}
181
182static inline void
183sio_out(struct uart_txx9_port *up, int offset, int value)
184{
185 switch (up->port.iotype) {
186 default:
187 __raw_writel(value, up->port.membase + offset);
188 break;
189 case UPIO_PORT:
190 outl(value, up->port.iobase + offset);
191 break;
192 }
193}
194
195static inline void
196sio_mask(struct uart_txx9_port *up, int offset, unsigned int value)
197{
198 sio_out(up, offset, sio_in(up, offset) & ~value);
199}
200static inline void
201sio_set(struct uart_txx9_port *up, int offset, unsigned int value)
202{
203 sio_out(up, offset, sio_in(up, offset) | value);
204}
205
206static inline void
207sio_quot_set(struct uart_txx9_port *up, int quot)
208{
209 quot >>= 1;
210 if (quot < 256)
211 sio_out(up, TXX9_SIBGR, quot | TXX9_SIBGR_BCLK_T0);
212 else if (quot < (256 << 2))
213 sio_out(up, TXX9_SIBGR, (quot >> 2) | TXX9_SIBGR_BCLK_T2);
214 else if (quot < (256 << 4))
215 sio_out(up, TXX9_SIBGR, (quot >> 4) | TXX9_SIBGR_BCLK_T4);
216 else if (quot < (256 << 6))
217 sio_out(up, TXX9_SIBGR, (quot >> 6) | TXX9_SIBGR_BCLK_T6);
218 else
219 sio_out(up, TXX9_SIBGR, 0xff | TXX9_SIBGR_BCLK_T6);
220}
221
222static struct uart_txx9_port *to_uart_txx9_port(struct uart_port *port)
223{
224 return container_of(port, struct uart_txx9_port, port);
225}
226
227static void serial_txx9_stop_tx(struct uart_port *port)
228{
229 struct uart_txx9_port *up = to_uart_txx9_port(port);
230 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
231}
232
233static void serial_txx9_start_tx(struct uart_port *port)
234{
235 struct uart_txx9_port *up = to_uart_txx9_port(port);
236 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
237}
238
239static void serial_txx9_stop_rx(struct uart_port *port)
240{
241 struct uart_txx9_port *up = to_uart_txx9_port(port);
242 up->port.read_status_mask &= ~TXX9_SIDISR_RDIS;
243}
244
245static void serial_txx9_enable_ms(struct uart_port *port)
246{
247 /* TXX9-SIO can not control DTR... */
248}
249
250static void serial_txx9_initialize(struct uart_port *port)
251{
252 struct uart_txx9_port *up = to_uart_txx9_port(port);
253 unsigned int tmout = 10000;
254
255 sio_out(up, TXX9_SIFCR, TXX9_SIFCR_SWRST);
256 /* TX4925 BUG WORKAROUND. Accessing SIOC register
257 * immediately after soft reset causes bus error. */
258 mmiowb();
259 udelay(1);
260 while ((sio_in(up, TXX9_SIFCR) & TXX9_SIFCR_SWRST) && --tmout)
261 udelay(1);
262 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
263 sio_set(up, TXX9_SIFCR,
264 TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1);
265 /* initial settings */
266 sio_out(up, TXX9_SILCR,
267 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
268 ((up->port.flags & UPF_TXX9_USE_SCLK) ?
269 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
270 sio_quot_set(up, uart_get_divisor(port, 9600));
271 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
272 sio_out(up, TXX9_SIDICR, 0);
273}
274
275static inline void
276receive_chars(struct uart_txx9_port *up, unsigned int *status)
277{
278 struct tty_struct *tty = up->port.state->port.tty;
279 unsigned char ch;
280 unsigned int disr = *status;
281 int max_count = 256;
282 char flag;
283 unsigned int next_ignore_status_mask;
284
285 do {
286 ch = sio_in(up, TXX9_SIRFIFO);
287 flag = TTY_NORMAL;
288 up->port.icount.rx++;
289
290 /* mask out RFDN_MASK bit added by previous overrun */
291 next_ignore_status_mask =
292 up->port.ignore_status_mask & ~TXX9_SIDISR_RFDN_MASK;
293 if (unlikely(disr & (TXX9_SIDISR_UBRK | TXX9_SIDISR_UPER |
294 TXX9_SIDISR_UFER | TXX9_SIDISR_UOER))) {
295 /*
296 * For statistics only
297 */
298 if (disr & TXX9_SIDISR_UBRK) {
299 disr &= ~(TXX9_SIDISR_UFER | TXX9_SIDISR_UPER);
300 up->port.icount.brk++;
301 /*
302 * We do the SysRQ and SAK checking
303 * here because otherwise the break
304 * may get masked by ignore_status_mask
305 * or read_status_mask.
306 */
307 if (uart_handle_break(&up->port))
308 goto ignore_char;
309 } else if (disr & TXX9_SIDISR_UPER)
310 up->port.icount.parity++;
311 else if (disr & TXX9_SIDISR_UFER)
312 up->port.icount.frame++;
313 if (disr & TXX9_SIDISR_UOER) {
314 up->port.icount.overrun++;
315 /*
316 * The receiver read buffer still hold
317 * a char which caused overrun.
318 * Ignore next char by adding RFDN_MASK
319 * to ignore_status_mask temporarily.
320 */
321 next_ignore_status_mask |=
322 TXX9_SIDISR_RFDN_MASK;
323 }
324
325 /*
326 * Mask off conditions which should be ingored.
327 */
328 disr &= up->port.read_status_mask;
329
330 if (disr & TXX9_SIDISR_UBRK) {
331 flag = TTY_BREAK;
332 } else if (disr & TXX9_SIDISR_UPER)
333 flag = TTY_PARITY;
334 else if (disr & TXX9_SIDISR_UFER)
335 flag = TTY_FRAME;
336 }
337 if (uart_handle_sysrq_char(&up->port, ch))
338 goto ignore_char;
339
340 uart_insert_char(&up->port, disr, TXX9_SIDISR_UOER, ch, flag);
341
342 ignore_char:
343 up->port.ignore_status_mask = next_ignore_status_mask;
344 disr = sio_in(up, TXX9_SIDISR);
345 } while (!(disr & TXX9_SIDISR_UVALID) && (max_count-- > 0));
346 spin_unlock(&up->port.lock);
347 tty_flip_buffer_push(tty);
348 spin_lock(&up->port.lock);
349 *status = disr;
350}
351
352static inline void transmit_chars(struct uart_txx9_port *up)
353{
354 struct circ_buf *xmit = &up->port.state->xmit;
355 int count;
356
357 if (up->port.x_char) {
358 sio_out(up, TXX9_SITFIFO, up->port.x_char);
359 up->port.icount.tx++;
360 up->port.x_char = 0;
361 return;
362 }
363 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
364 serial_txx9_stop_tx(&up->port);
365 return;
366 }
367
368 count = TXX9_SIO_TX_FIFO;
369 do {
370 sio_out(up, TXX9_SITFIFO, xmit->buf[xmit->tail]);
371 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
372 up->port.icount.tx++;
373 if (uart_circ_empty(xmit))
374 break;
375 } while (--count > 0);
376
377 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
378 uart_write_wakeup(&up->port);
379
380 if (uart_circ_empty(xmit))
381 serial_txx9_stop_tx(&up->port);
382}
383
384static irqreturn_t serial_txx9_interrupt(int irq, void *dev_id)
385{
386 int pass_counter = 0;
387 struct uart_txx9_port *up = dev_id;
388 unsigned int status;
389
390 while (1) {
391 spin_lock(&up->port.lock);
392 status = sio_in(up, TXX9_SIDISR);
393 if (!(sio_in(up, TXX9_SIDICR) & TXX9_SIDICR_TIE))
394 status &= ~TXX9_SIDISR_TDIS;
395 if (!(status & (TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
396 TXX9_SIDISR_TOUT))) {
397 spin_unlock(&up->port.lock);
398 break;
399 }
400
401 if (status & TXX9_SIDISR_RDIS)
402 receive_chars(up, &status);
403 if (status & TXX9_SIDISR_TDIS)
404 transmit_chars(up);
405 /* Clear TX/RX Int. Status */
406 sio_mask(up, TXX9_SIDISR,
407 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
408 TXX9_SIDISR_TOUT);
409 spin_unlock(&up->port.lock);
410
411 if (pass_counter++ > PASS_LIMIT)
412 break;
413 }
414
415 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
416}
417
418static unsigned int serial_txx9_tx_empty(struct uart_port *port)
419{
420 struct uart_txx9_port *up = to_uart_txx9_port(port);
421 unsigned long flags;
422 unsigned int ret;
423
424 spin_lock_irqsave(&up->port.lock, flags);
425 ret = (sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS) ? TIOCSER_TEMT : 0;
426 spin_unlock_irqrestore(&up->port.lock, flags);
427
428 return ret;
429}
430
431static unsigned int serial_txx9_get_mctrl(struct uart_port *port)
432{
433 struct uart_txx9_port *up = to_uart_txx9_port(port);
434 unsigned int ret;
435
436 /* no modem control lines */
437 ret = TIOCM_CAR | TIOCM_DSR;
438 ret |= (sio_in(up, TXX9_SIFLCR) & TXX9_SIFLCR_RTSSC) ? 0 : TIOCM_RTS;
439 ret |= (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS) ? 0 : TIOCM_CTS;
440
441 return ret;
442}
443
444static void serial_txx9_set_mctrl(struct uart_port *port, unsigned int mctrl)
445{
446 struct uart_txx9_port *up = to_uart_txx9_port(port);
447
448 if (mctrl & TIOCM_RTS)
449 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
450 else
451 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
452}
453
454static void serial_txx9_break_ctl(struct uart_port *port, int break_state)
455{
456 struct uart_txx9_port *up = to_uart_txx9_port(port);
457 unsigned long flags;
458
459 spin_lock_irqsave(&up->port.lock, flags);
460 if (break_state == -1)
461 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
462 else
463 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
464 spin_unlock_irqrestore(&up->port.lock, flags);
465}
466
467#if defined(CONFIG_SERIAL_TXX9_CONSOLE) || (CONFIG_CONSOLE_POLL)
468/*
469 * Wait for transmitter & holding register to empty
470 */
471static void wait_for_xmitr(struct uart_txx9_port *up)
472{
473 unsigned int tmout = 10000;
474
475 /* Wait up to 10ms for the character(s) to be sent. */
476 while (--tmout &&
477 !(sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS))
478 udelay(1);
479
480 /* Wait up to 1s for flow control if necessary */
481 if (up->port.flags & UPF_CONS_FLOW) {
482 tmout = 1000000;
483 while (--tmout &&
484 (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS))
485 udelay(1);
486 }
487}
488#endif
489
490#ifdef CONFIG_CONSOLE_POLL
491/*
492 * Console polling routines for writing and reading from the uart while
493 * in an interrupt or debug context.
494 */
495
496static int serial_txx9_get_poll_char(struct uart_port *port)
497{
498 unsigned int ier;
499 unsigned char c;
500 struct uart_txx9_port *up = to_uart_txx9_port(port);
501
502 /*
503 * First save the IER then disable the interrupts
504 */
505 ier = sio_in(up, TXX9_SIDICR);
506 sio_out(up, TXX9_SIDICR, 0);
507
508 while (sio_in(up, TXX9_SIDISR) & TXX9_SIDISR_UVALID)
509 ;
510
511 c = sio_in(up, TXX9_SIRFIFO);
512
513 /*
514 * Finally, clear RX interrupt status
515 * and restore the IER
516 */
517 sio_mask(up, TXX9_SIDISR, TXX9_SIDISR_RDIS);
518 sio_out(up, TXX9_SIDICR, ier);
519 return c;
520}
521
522
523static void serial_txx9_put_poll_char(struct uart_port *port, unsigned char c)
524{
525 unsigned int ier;
526 struct uart_txx9_port *up = to_uart_txx9_port(port);
527
528 /*
529 * First save the IER then disable the interrupts
530 */
531 ier = sio_in(up, TXX9_SIDICR);
532 sio_out(up, TXX9_SIDICR, 0);
533
534 wait_for_xmitr(up);
535 /*
536 * Send the character out.
537 * If a LF, also do CR...
538 */
539 sio_out(up, TXX9_SITFIFO, c);
540 if (c == 10) {
541 wait_for_xmitr(up);
542 sio_out(up, TXX9_SITFIFO, 13);
543 }
544
545 /*
546 * Finally, wait for transmitter to become empty
547 * and restore the IER
548 */
549 wait_for_xmitr(up);
550 sio_out(up, TXX9_SIDICR, ier);
551}
552
553#endif /* CONFIG_CONSOLE_POLL */
554
555static int serial_txx9_startup(struct uart_port *port)
556{
557 struct uart_txx9_port *up = to_uart_txx9_port(port);
558 unsigned long flags;
559 int retval;
560
561 /*
562 * Clear the FIFO buffers and disable them.
563 * (they will be reenabled in set_termios())
564 */
565 sio_set(up, TXX9_SIFCR,
566 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
567 /* clear reset */
568 sio_mask(up, TXX9_SIFCR,
569 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
570 sio_out(up, TXX9_SIDICR, 0);
571
572 /*
573 * Clear the interrupt registers.
574 */
575 sio_out(up, TXX9_SIDISR, 0);
576
577 retval = request_irq(up->port.irq, serial_txx9_interrupt,
578 IRQF_SHARED, "serial_txx9", up);
579 if (retval)
580 return retval;
581
582 /*
583 * Now, initialize the UART
584 */
585 spin_lock_irqsave(&up->port.lock, flags);
586 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
587 spin_unlock_irqrestore(&up->port.lock, flags);
588
589 /* Enable RX/TX */
590 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
591
592 /*
593 * Finally, enable interrupts.
594 */
595 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
596
597 return 0;
598}
599
600static void serial_txx9_shutdown(struct uart_port *port)
601{
602 struct uart_txx9_port *up = to_uart_txx9_port(port);
603 unsigned long flags;
604
605 /*
606 * Disable interrupts from this port
607 */
608 sio_out(up, TXX9_SIDICR, 0); /* disable all intrs */
609
610 spin_lock_irqsave(&up->port.lock, flags);
611 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
612 spin_unlock_irqrestore(&up->port.lock, flags);
613
614 /*
615 * Disable break condition
616 */
617 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
618
619#ifdef CONFIG_SERIAL_TXX9_CONSOLE
620 if (up->port.cons && up->port.line == up->port.cons->index) {
621 free_irq(up->port.irq, up);
622 return;
623 }
624#endif
625 /* reset FIFOs */
626 sio_set(up, TXX9_SIFCR,
627 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
628 /* clear reset */
629 sio_mask(up, TXX9_SIFCR,
630 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
631
632 /* Disable RX/TX */
633 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
634
635 free_irq(up->port.irq, up);
636}
637
638static void
639serial_txx9_set_termios(struct uart_port *port, struct ktermios *termios,
640 struct ktermios *old)
641{
642 struct uart_txx9_port *up = to_uart_txx9_port(port);
643 unsigned int cval, fcr = 0;
644 unsigned long flags;
645 unsigned int baud, quot;
646
647 /*
648 * We don't support modem control lines.
649 */
650 termios->c_cflag &= ~(HUPCL | CMSPAR);
651 termios->c_cflag |= CLOCAL;
652
653 cval = sio_in(up, TXX9_SILCR);
654 /* byte size and parity */
655 cval &= ~TXX9_SILCR_UMODE_MASK;
656 switch (termios->c_cflag & CSIZE) {
657 case CS7:
658 cval |= TXX9_SILCR_UMODE_7BIT;
659 break;
660 default:
661 case CS5: /* not supported */
662 case CS6: /* not supported */
663 case CS8:
664 cval |= TXX9_SILCR_UMODE_8BIT;
665 break;
666 }
667
668 cval &= ~TXX9_SILCR_USBL_MASK;
669 if (termios->c_cflag & CSTOPB)
670 cval |= TXX9_SILCR_USBL_2BIT;
671 else
672 cval |= TXX9_SILCR_USBL_1BIT;
673 cval &= ~(TXX9_SILCR_UPEN | TXX9_SILCR_UEPS);
674 if (termios->c_cflag & PARENB)
675 cval |= TXX9_SILCR_UPEN;
676 if (!(termios->c_cflag & PARODD))
677 cval |= TXX9_SILCR_UEPS;
678
679 /*
680 * Ask the core to calculate the divisor for us.
681 */
682 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16/2);
683 quot = uart_get_divisor(port, baud);
684
685 /* Set up FIFOs */
686 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
687 fcr = TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1;
688
689 /*
690 * Ok, we're now changing the port state. Do it with
691 * interrupts disabled.
692 */
693 spin_lock_irqsave(&up->port.lock, flags);
694
695 /*
696 * Update the per-port timeout.
697 */
698 uart_update_timeout(port, termios->c_cflag, baud);
699
700 up->port.read_status_mask = TXX9_SIDISR_UOER |
701 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS;
702 if (termios->c_iflag & INPCK)
703 up->port.read_status_mask |= TXX9_SIDISR_UFER | TXX9_SIDISR_UPER;
704 if (termios->c_iflag & (BRKINT | PARMRK))
705 up->port.read_status_mask |= TXX9_SIDISR_UBRK;
706
707 /*
708 * Characteres to ignore
709 */
710 up->port.ignore_status_mask = 0;
711 if (termios->c_iflag & IGNPAR)
712 up->port.ignore_status_mask |= TXX9_SIDISR_UPER | TXX9_SIDISR_UFER;
713 if (termios->c_iflag & IGNBRK) {
714 up->port.ignore_status_mask |= TXX9_SIDISR_UBRK;
715 /*
716 * If we're ignoring parity and break indicators,
717 * ignore overruns too (for real raw support).
718 */
719 if (termios->c_iflag & IGNPAR)
720 up->port.ignore_status_mask |= TXX9_SIDISR_UOER;
721 }
722
723 /*
724 * ignore all characters if CREAD is not set
725 */
726 if ((termios->c_cflag & CREAD) == 0)
727 up->port.ignore_status_mask |= TXX9_SIDISR_RDIS;
728
729 /* CTS flow control flag */
730 if ((termios->c_cflag & CRTSCTS) &&
731 (up->port.flags & UPF_TXX9_HAVE_CTS_LINE)) {
732 sio_set(up, TXX9_SIFLCR,
733 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
734 } else {
735 sio_mask(up, TXX9_SIFLCR,
736 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
737 }
738
739 sio_out(up, TXX9_SILCR, cval);
740 sio_quot_set(up, quot);
741 sio_out(up, TXX9_SIFCR, fcr);
742
743 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
744 spin_unlock_irqrestore(&up->port.lock, flags);
745}
746
747static void
748serial_txx9_pm(struct uart_port *port, unsigned int state,
749 unsigned int oldstate)
750{
751 /*
752 * If oldstate was -1 this is called from
753 * uart_configure_port(). In this case do not initialize the
754 * port now, because the port was already initialized (for
755 * non-console port) or should not be initialized here (for
756 * console port). If we initialized the port here we lose
757 * serial console settings.
758 */
759 if (state == 0 && oldstate != -1)
760 serial_txx9_initialize(port);
761}
762
763static int serial_txx9_request_resource(struct uart_txx9_port *up)
764{
765 unsigned int size = TXX9_REGION_SIZE;
766 int ret = 0;
767
768 switch (up->port.iotype) {
769 default:
770 if (!up->port.mapbase)
771 break;
772
773 if (!request_mem_region(up->port.mapbase, size, "serial_txx9")) {
774 ret = -EBUSY;
775 break;
776 }
777
778 if (up->port.flags & UPF_IOREMAP) {
779 up->port.membase = ioremap(up->port.mapbase, size);
780 if (!up->port.membase) {
781 release_mem_region(up->port.mapbase, size);
782 ret = -ENOMEM;
783 }
784 }
785 break;
786
787 case UPIO_PORT:
788 if (!request_region(up->port.iobase, size, "serial_txx9"))
789 ret = -EBUSY;
790 break;
791 }
792 return ret;
793}
794
795static void serial_txx9_release_resource(struct uart_txx9_port *up)
796{
797 unsigned int size = TXX9_REGION_SIZE;
798
799 switch (up->port.iotype) {
800 default:
801 if (!up->port.mapbase)
802 break;
803
804 if (up->port.flags & UPF_IOREMAP) {
805 iounmap(up->port.membase);
806 up->port.membase = NULL;
807 }
808
809 release_mem_region(up->port.mapbase, size);
810 break;
811
812 case UPIO_PORT:
813 release_region(up->port.iobase, size);
814 break;
815 }
816}
817
818static void serial_txx9_release_port(struct uart_port *port)
819{
820 struct uart_txx9_port *up = to_uart_txx9_port(port);
821 serial_txx9_release_resource(up);
822}
823
824static int serial_txx9_request_port(struct uart_port *port)
825{
826 struct uart_txx9_port *up = to_uart_txx9_port(port);
827 return serial_txx9_request_resource(up);
828}
829
830static void serial_txx9_config_port(struct uart_port *port, int uflags)
831{
832 struct uart_txx9_port *up = to_uart_txx9_port(port);
833 int ret;
834
835 /*
836 * Find the region that we can probe for. This in turn
837 * tells us whether we can probe for the type of port.
838 */
839 ret = serial_txx9_request_resource(up);
840 if (ret < 0)
841 return;
842 port->type = PORT_TXX9;
843 up->port.fifosize = TXX9_SIO_TX_FIFO;
844
845#ifdef CONFIG_SERIAL_TXX9_CONSOLE
846 if (up->port.line == up->port.cons->index)
847 return;
848#endif
849 serial_txx9_initialize(port);
850}
851
852static const char *
853serial_txx9_type(struct uart_port *port)
854{
855 return "txx9";
856}
857
858static struct uart_ops serial_txx9_pops = {
859 .tx_empty = serial_txx9_tx_empty,
860 .set_mctrl = serial_txx9_set_mctrl,
861 .get_mctrl = serial_txx9_get_mctrl,
862 .stop_tx = serial_txx9_stop_tx,
863 .start_tx = serial_txx9_start_tx,
864 .stop_rx = serial_txx9_stop_rx,
865 .enable_ms = serial_txx9_enable_ms,
866 .break_ctl = serial_txx9_break_ctl,
867 .startup = serial_txx9_startup,
868 .shutdown = serial_txx9_shutdown,
869 .set_termios = serial_txx9_set_termios,
870 .pm = serial_txx9_pm,
871 .type = serial_txx9_type,
872 .release_port = serial_txx9_release_port,
873 .request_port = serial_txx9_request_port,
874 .config_port = serial_txx9_config_port,
875#ifdef CONFIG_CONSOLE_POLL
876 .poll_get_char = serial_txx9_get_poll_char,
877 .poll_put_char = serial_txx9_put_poll_char,
878#endif
879};
880
881static struct uart_txx9_port serial_txx9_ports[UART_NR];
882
883static void __init serial_txx9_register_ports(struct uart_driver *drv,
884 struct device *dev)
885{
886 int i;
887
888 for (i = 0; i < UART_NR; i++) {
889 struct uart_txx9_port *up = &serial_txx9_ports[i];
890
891 up->port.line = i;
892 up->port.ops = &serial_txx9_pops;
893 up->port.dev = dev;
894 if (up->port.iobase || up->port.mapbase)
895 uart_add_one_port(drv, &up->port);
896 }
897}
898
899#ifdef CONFIG_SERIAL_TXX9_CONSOLE
900
901static void serial_txx9_console_putchar(struct uart_port *port, int ch)
902{
903 struct uart_txx9_port *up = to_uart_txx9_port(port);
904
905 wait_for_xmitr(up);
906 sio_out(up, TXX9_SITFIFO, ch);
907}
908
909/*
910 * Print a string to the serial port trying not to disturb
911 * any possible real use of the port...
912 *
913 * The console_lock must be held when we get here.
914 */
915static void
916serial_txx9_console_write(struct console *co, const char *s, unsigned int count)
917{
918 struct uart_txx9_port *up = &serial_txx9_ports[co->index];
919 unsigned int ier, flcr;
920
921 /*
922 * First save the UER then disable the interrupts
923 */
924 ier = sio_in(up, TXX9_SIDICR);
925 sio_out(up, TXX9_SIDICR, 0);
926 /*
927 * Disable flow-control if enabled (and unnecessary)
928 */
929 flcr = sio_in(up, TXX9_SIFLCR);
930 if (!(up->port.flags & UPF_CONS_FLOW) && (flcr & TXX9_SIFLCR_TES))
931 sio_out(up, TXX9_SIFLCR, flcr & ~TXX9_SIFLCR_TES);
932
933 uart_console_write(&up->port, s, count, serial_txx9_console_putchar);
934
935 /*
936 * Finally, wait for transmitter to become empty
937 * and restore the IER
938 */
939 wait_for_xmitr(up);
940 sio_out(up, TXX9_SIFLCR, flcr);
941 sio_out(up, TXX9_SIDICR, ier);
942}
943
944static int __init serial_txx9_console_setup(struct console *co, char *options)
945{
946 struct uart_port *port;
947 struct uart_txx9_port *up;
948 int baud = 9600;
949 int bits = 8;
950 int parity = 'n';
951 int flow = 'n';
952
953 /*
954 * Check whether an invalid uart number has been specified, and
955 * if so, search for the first available port that does have
956 * console support.
957 */
958 if (co->index >= UART_NR)
959 co->index = 0;
960 up = &serial_txx9_ports[co->index];
961 port = &up->port;
962 if (!port->ops)
963 return -ENODEV;
964
965 serial_txx9_initialize(&up->port);
966
967 if (options)
968 uart_parse_options(options, &baud, &parity, &bits, &flow);
969
970 return uart_set_options(port, co, baud, parity, bits, flow);
971}
972
973static struct uart_driver serial_txx9_reg;
974static struct console serial_txx9_console = {
975 .name = TXX9_TTY_NAME,
976 .write = serial_txx9_console_write,
977 .device = uart_console_device,
978 .setup = serial_txx9_console_setup,
979 .flags = CON_PRINTBUFFER,
980 .index = -1,
981 .data = &serial_txx9_reg,
982};
983
984static int __init serial_txx9_console_init(void)
985{
986 register_console(&serial_txx9_console);
987 return 0;
988}
989console_initcall(serial_txx9_console_init);
990
991#define SERIAL_TXX9_CONSOLE &serial_txx9_console
992#else
993#define SERIAL_TXX9_CONSOLE NULL
994#endif
995
996static struct uart_driver serial_txx9_reg = {
997 .owner = THIS_MODULE,
998 .driver_name = "serial_txx9",
999 .dev_name = TXX9_TTY_NAME,
1000 .major = TXX9_TTY_MAJOR,
1001 .minor = TXX9_TTY_MINOR_START,
1002 .nr = UART_NR,
1003 .cons = SERIAL_TXX9_CONSOLE,
1004};
1005
1006int __init early_serial_txx9_setup(struct uart_port *port)
1007{
1008 if (port->line >= ARRAY_SIZE(serial_txx9_ports))
1009 return -ENODEV;
1010
1011 serial_txx9_ports[port->line].port = *port;
1012 serial_txx9_ports[port->line].port.ops = &serial_txx9_pops;
1013 serial_txx9_ports[port->line].port.flags |=
1014 UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1015 return 0;
1016}
1017
1018static DEFINE_MUTEX(serial_txx9_mutex);
1019
1020/**
1021 * serial_txx9_register_port - register a serial port
1022 * @port: serial port template
1023 *
1024 * Configure the serial port specified by the request.
1025 *
1026 * The port is then probed and if necessary the IRQ is autodetected
1027 * If this fails an error is returned.
1028 *
1029 * On success the port is ready to use and the line number is returned.
1030 */
1031static int __devinit serial_txx9_register_port(struct uart_port *port)
1032{
1033 int i;
1034 struct uart_txx9_port *uart;
1035 int ret = -ENOSPC;
1036
1037 mutex_lock(&serial_txx9_mutex);
1038 for (i = 0; i < UART_NR; i++) {
1039 uart = &serial_txx9_ports[i];
1040 if (uart_match_port(&uart->port, port)) {
1041 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1042 break;
1043 }
1044 }
1045 if (i == UART_NR) {
1046 /* Find unused port */
1047 for (i = 0; i < UART_NR; i++) {
1048 uart = &serial_txx9_ports[i];
1049 if (!(uart->port.iobase || uart->port.mapbase))
1050 break;
1051 }
1052 }
1053 if (i < UART_NR) {
1054 uart->port.iobase = port->iobase;
1055 uart->port.membase = port->membase;
1056 uart->port.irq = port->irq;
1057 uart->port.uartclk = port->uartclk;
1058 uart->port.iotype = port->iotype;
1059 uart->port.flags = port->flags
1060 | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1061 uart->port.mapbase = port->mapbase;
1062 if (port->dev)
1063 uart->port.dev = port->dev;
1064 ret = uart_add_one_port(&serial_txx9_reg, &uart->port);
1065 if (ret == 0)
1066 ret = uart->port.line;
1067 }
1068 mutex_unlock(&serial_txx9_mutex);
1069 return ret;
1070}
1071
1072/**
1073 * serial_txx9_unregister_port - remove a txx9 serial port at runtime
1074 * @line: serial line number
1075 *
1076 * Remove one serial port. This may not be called from interrupt
1077 * context. We hand the port back to the our control.
1078 */
1079static void __devexit serial_txx9_unregister_port(int line)
1080{
1081 struct uart_txx9_port *uart = &serial_txx9_ports[line];
1082
1083 mutex_lock(&serial_txx9_mutex);
1084 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1085 uart->port.flags = 0;
1086 uart->port.type = PORT_UNKNOWN;
1087 uart->port.iobase = 0;
1088 uart->port.mapbase = 0;
1089 uart->port.membase = NULL;
1090 uart->port.dev = NULL;
1091 mutex_unlock(&serial_txx9_mutex);
1092}
1093
1094/*
1095 * Register a set of serial devices attached to a platform device.
1096 */
1097static int __devinit serial_txx9_probe(struct platform_device *dev)
1098{
1099 struct uart_port *p = dev->dev.platform_data;
1100 struct uart_port port;
1101 int ret, i;
1102
1103 memset(&port, 0, sizeof(struct uart_port));
1104 for (i = 0; p && p->uartclk != 0; p++, i++) {
1105 port.iobase = p->iobase;
1106 port.membase = p->membase;
1107 port.irq = p->irq;
1108 port.uartclk = p->uartclk;
1109 port.iotype = p->iotype;
1110 port.flags = p->flags;
1111 port.mapbase = p->mapbase;
1112 port.dev = &dev->dev;
1113 ret = serial_txx9_register_port(&port);
1114 if (ret < 0) {
1115 dev_err(&dev->dev, "unable to register port at index %d "
1116 "(IO%lx MEM%llx IRQ%d): %d\n", i,
1117 p->iobase, (unsigned long long)p->mapbase,
1118 p->irq, ret);
1119 }
1120 }
1121 return 0;
1122}
1123
1124/*
1125 * Remove serial ports registered against a platform device.
1126 */
1127static int __devexit serial_txx9_remove(struct platform_device *dev)
1128{
1129 int i;
1130
1131 for (i = 0; i < UART_NR; i++) {
1132 struct uart_txx9_port *up = &serial_txx9_ports[i];
1133
1134 if (up->port.dev == &dev->dev)
1135 serial_txx9_unregister_port(i);
1136 }
1137 return 0;
1138}
1139
1140#ifdef CONFIG_PM
1141static int serial_txx9_suspend(struct platform_device *dev, pm_message_t state)
1142{
1143 int i;
1144
1145 for (i = 0; i < UART_NR; i++) {
1146 struct uart_txx9_port *up = &serial_txx9_ports[i];
1147
1148 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1149 uart_suspend_port(&serial_txx9_reg, &up->port);
1150 }
1151
1152 return 0;
1153}
1154
1155static int serial_txx9_resume(struct platform_device *dev)
1156{
1157 int i;
1158
1159 for (i = 0; i < UART_NR; i++) {
1160 struct uart_txx9_port *up = &serial_txx9_ports[i];
1161
1162 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1163 uart_resume_port(&serial_txx9_reg, &up->port);
1164 }
1165
1166 return 0;
1167}
1168#endif
1169
1170static struct platform_driver serial_txx9_plat_driver = {
1171 .probe = serial_txx9_probe,
1172 .remove = __devexit_p(serial_txx9_remove),
1173#ifdef CONFIG_PM
1174 .suspend = serial_txx9_suspend,
1175 .resume = serial_txx9_resume,
1176#endif
1177 .driver = {
1178 .name = "serial_txx9",
1179 .owner = THIS_MODULE,
1180 },
1181};
1182
1183#ifdef ENABLE_SERIAL_TXX9_PCI
1184/*
1185 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1186 * to the arrangement of serial ports on a PCI card.
1187 */
1188static int __devinit
1189pciserial_txx9_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1190{
1191 struct uart_port port;
1192 int line;
1193 int rc;
1194
1195 rc = pci_enable_device(dev);
1196 if (rc)
1197 return rc;
1198
1199 memset(&port, 0, sizeof(port));
1200 port.ops = &serial_txx9_pops;
1201 port.flags |= UPF_TXX9_HAVE_CTS_LINE;
1202 port.uartclk = 66670000;
1203 port.irq = dev->irq;
1204 port.iotype = UPIO_PORT;
1205 port.iobase = pci_resource_start(dev, 1);
1206 port.dev = &dev->dev;
1207 line = serial_txx9_register_port(&port);
1208 if (line < 0) {
1209 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), line);
1210 pci_disable_device(dev);
1211 return line;
1212 }
1213 pci_set_drvdata(dev, &serial_txx9_ports[line]);
1214
1215 return 0;
1216}
1217
1218static void __devexit pciserial_txx9_remove_one(struct pci_dev *dev)
1219{
1220 struct uart_txx9_port *up = pci_get_drvdata(dev);
1221
1222 pci_set_drvdata(dev, NULL);
1223
1224 if (up) {
1225 serial_txx9_unregister_port(up->port.line);
1226 pci_disable_device(dev);
1227 }
1228}
1229
1230#ifdef CONFIG_PM
1231static int pciserial_txx9_suspend_one(struct pci_dev *dev, pm_message_t state)
1232{
1233 struct uart_txx9_port *up = pci_get_drvdata(dev);
1234
1235 if (up)
1236 uart_suspend_port(&serial_txx9_reg, &up->port);
1237 pci_save_state(dev);
1238 pci_set_power_state(dev, pci_choose_state(dev, state));
1239 return 0;
1240}
1241
1242static int pciserial_txx9_resume_one(struct pci_dev *dev)
1243{
1244 struct uart_txx9_port *up = pci_get_drvdata(dev);
1245
1246 pci_set_power_state(dev, PCI_D0);
1247 pci_restore_state(dev);
1248 if (up)
1249 uart_resume_port(&serial_txx9_reg, &up->port);
1250 return 0;
1251}
1252#endif
1253
1254static const struct pci_device_id serial_txx9_pci_tbl[] = {
1255 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC) },
1256 { 0, }
1257};
1258
1259static struct pci_driver serial_txx9_pci_driver = {
1260 .name = "serial_txx9",
1261 .probe = pciserial_txx9_init_one,
1262 .remove = __devexit_p(pciserial_txx9_remove_one),
1263#ifdef CONFIG_PM
1264 .suspend = pciserial_txx9_suspend_one,
1265 .resume = pciserial_txx9_resume_one,
1266#endif
1267 .id_table = serial_txx9_pci_tbl,
1268};
1269
1270MODULE_DEVICE_TABLE(pci, serial_txx9_pci_tbl);
1271#endif /* ENABLE_SERIAL_TXX9_PCI */
1272
1273static struct platform_device *serial_txx9_plat_devs;
1274
1275static int __init serial_txx9_init(void)
1276{
1277 int ret;
1278
1279 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1280
1281 ret = uart_register_driver(&serial_txx9_reg);
1282 if (ret)
1283 goto out;
1284
1285 serial_txx9_plat_devs = platform_device_alloc("serial_txx9", -1);
1286 if (!serial_txx9_plat_devs) {
1287 ret = -ENOMEM;
1288 goto unreg_uart_drv;
1289 }
1290
1291 ret = platform_device_add(serial_txx9_plat_devs);
1292 if (ret)
1293 goto put_dev;
1294
1295 serial_txx9_register_ports(&serial_txx9_reg,
1296 &serial_txx9_plat_devs->dev);
1297
1298 ret = platform_driver_register(&serial_txx9_plat_driver);
1299 if (ret)
1300 goto del_dev;
1301
1302#ifdef ENABLE_SERIAL_TXX9_PCI
1303 ret = pci_register_driver(&serial_txx9_pci_driver);
1304#endif
1305 if (ret == 0)
1306 goto out;
1307
1308 del_dev:
1309 platform_device_del(serial_txx9_plat_devs);
1310 put_dev:
1311 platform_device_put(serial_txx9_plat_devs);
1312 unreg_uart_drv:
1313 uart_unregister_driver(&serial_txx9_reg);
1314 out:
1315 return ret;
1316}
1317
1318static void __exit serial_txx9_exit(void)
1319{
1320 int i;
1321
1322#ifdef ENABLE_SERIAL_TXX9_PCI
1323 pci_unregister_driver(&serial_txx9_pci_driver);
1324#endif
1325 platform_driver_unregister(&serial_txx9_plat_driver);
1326 platform_device_unregister(serial_txx9_plat_devs);
1327 for (i = 0; i < UART_NR; i++) {
1328 struct uart_txx9_port *up = &serial_txx9_ports[i];
1329 if (up->port.iobase || up->port.mapbase)
1330 uart_remove_one_port(&serial_txx9_reg, &up->port);
1331 }
1332
1333 uart_unregister_driver(&serial_txx9_reg);
1334}
1335
1336module_init(serial_txx9_init);
1337module_exit(serial_txx9_exit);
1338
1339MODULE_LICENSE("GPL");
1340MODULE_DESCRIPTION("TX39/49 serial driver");
1341
1342MODULE_ALIAS_CHARDEV_MAJOR(TXX9_TTY_MAJOR);
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
new file mode 100644
index 000000000000..ebd8629c108d
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.c
@@ -0,0 +1,2083 @@
1/*
2 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
4 * Copyright (C) 2002 - 2011 Paul Mundt
5 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
6 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
14 * Removed SH7300 support (Jul 2007).
15 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
20#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
23
24#undef DEBUG
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/console.h>
41#include <linux/platform_device.h>
42#include <linux/serial_sci.h>
43#include <linux/notifier.h>
44#include <linux/pm_runtime.h>
45#include <linux/cpufreq.h>
46#include <linux/clk.h>
47#include <linux/ctype.h>
48#include <linux/err.h>
49#include <linux/dmaengine.h>
50#include <linux/scatterlist.h>
51#include <linux/slab.h>
52
53#ifdef CONFIG_SUPERH
54#include <asm/sh_bios.h>
55#endif
56
57#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
61#include "sh-sci.h"
62
63struct sci_port {
64 struct uart_port port;
65
66 /* Platform configuration */
67 struct plat_sci_port *cfg;
68
69 /* Port enable callback */
70 void (*enable)(struct uart_port *port);
71
72 /* Port disable callback */
73 void (*disable)(struct uart_port *port);
74
75 /* Break timer */
76 struct timer_list break_timer;
77 int break_flag;
78
79 /* Interface clock */
80 struct clk *iclk;
81 /* Function clock */
82 struct clk *fclk;
83
84 struct dma_chan *chan_tx;
85 struct dma_chan *chan_rx;
86
87#ifdef CONFIG_SERIAL_SH_SCI_DMA
88 struct dma_async_tx_descriptor *desc_tx;
89 struct dma_async_tx_descriptor *desc_rx[2];
90 dma_cookie_t cookie_tx;
91 dma_cookie_t cookie_rx[2];
92 dma_cookie_t active_rx;
93 struct scatterlist sg_tx;
94 unsigned int sg_len_tx;
95 struct scatterlist sg_rx[2];
96 size_t buf_len_rx;
97 struct sh_dmae_slave param_tx;
98 struct sh_dmae_slave param_rx;
99 struct work_struct work_tx;
100 struct work_struct work_rx;
101 struct timer_list rx_timer;
102 unsigned int rx_timeout;
103#endif
104
105 struct notifier_block freq_transition;
106};
107
108/* Function prototypes */
109static void sci_start_tx(struct uart_port *port);
110static void sci_stop_tx(struct uart_port *port);
111static void sci_start_rx(struct uart_port *port);
112
113#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
114
115static struct sci_port sci_ports[SCI_NPORTS];
116static struct uart_driver sci_uart_driver;
117
118static inline struct sci_port *
119to_sci_port(struct uart_port *uart)
120{
121 return container_of(uart, struct sci_port, port);
122}
123
124#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
125
126#ifdef CONFIG_CONSOLE_POLL
127static int sci_poll_get_char(struct uart_port *port)
128{
129 unsigned short status;
130 int c;
131
132 do {
133 status = sci_in(port, SCxSR);
134 if (status & SCxSR_ERRORS(port)) {
135 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
136 continue;
137 }
138 break;
139 } while (1);
140
141 if (!(status & SCxSR_RDxF(port)))
142 return NO_POLL_CHAR;
143
144 c = sci_in(port, SCxRDR);
145
146 /* Dummy read */
147 sci_in(port, SCxSR);
148 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
149
150 return c;
151}
152#endif
153
154static void sci_poll_put_char(struct uart_port *port, unsigned char c)
155{
156 unsigned short status;
157
158 do {
159 status = sci_in(port, SCxSR);
160 } while (!(status & SCxSR_TDxE(port)));
161
162 sci_out(port, SCxTDR, c);
163 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
164}
165#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
166
167#if defined(__H8300H__) || defined(__H8300S__)
168static void sci_init_pins(struct uart_port *port, unsigned int cflag)
169{
170 int ch = (port->mapbase - SMR0) >> 3;
171
172 /* set DDR regs */
173 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
174 h8300_sci_pins[ch].rx,
175 H8300_GPIO_INPUT);
176 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
177 h8300_sci_pins[ch].tx,
178 H8300_GPIO_OUTPUT);
179
180 /* tx mark output*/
181 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
182}
183#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
184static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
185{
186 if (port->mapbase == 0xA4400000) {
187 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
188 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
189 } else if (port->mapbase == 0xA4410000)
190 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
191}
192#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
193static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
194{
195 unsigned short data;
196
197 if (cflag & CRTSCTS) {
198 /* enable RTS/CTS */
199 if (port->mapbase == 0xa4430000) { /* SCIF0 */
200 /* Clear PTCR bit 9-2; enable all scif pins but sck */
201 data = __raw_readw(PORT_PTCR);
202 __raw_writew((data & 0xfc03), PORT_PTCR);
203 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
204 /* Clear PVCR bit 9-2 */
205 data = __raw_readw(PORT_PVCR);
206 __raw_writew((data & 0xfc03), PORT_PVCR);
207 }
208 } else {
209 if (port->mapbase == 0xa4430000) { /* SCIF0 */
210 /* Clear PTCR bit 5-2; enable only tx and rx */
211 data = __raw_readw(PORT_PTCR);
212 __raw_writew((data & 0xffc3), PORT_PTCR);
213 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
214 /* Clear PVCR bit 5-2 */
215 data = __raw_readw(PORT_PVCR);
216 __raw_writew((data & 0xffc3), PORT_PVCR);
217 }
218 }
219}
220#elif defined(CONFIG_CPU_SH3)
221/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
222static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
223{
224 unsigned short data;
225
226 /* We need to set SCPCR to enable RTS/CTS */
227 data = __raw_readw(SCPCR);
228 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
229 __raw_writew(data & 0x0fcf, SCPCR);
230
231 if (!(cflag & CRTSCTS)) {
232 /* We need to set SCPCR to enable RTS/CTS */
233 data = __raw_readw(SCPCR);
234 /* Clear out SCP7MD1,0, SCP4MD1,0,
235 Set SCP6MD1,0 = {01} (output) */
236 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
237
238 data = __raw_readb(SCPDR);
239 /* Set /RTS2 (bit6) = 0 */
240 __raw_writeb(data & 0xbf, SCPDR);
241 }
242}
243#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
244static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
245{
246 unsigned short data;
247
248 if (port->mapbase == 0xffe00000) {
249 data = __raw_readw(PSCR);
250 data &= ~0x03cf;
251 if (!(cflag & CRTSCTS))
252 data |= 0x0340;
253
254 __raw_writew(data, PSCR);
255 }
256}
257#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
258 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
259 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
260 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
261 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
262 defined(CONFIG_CPU_SUBTYPE_SHX3)
263static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
264{
265 if (!(cflag & CRTSCTS))
266 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
267}
268#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
269static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
270{
271 if (!(cflag & CRTSCTS))
272 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
273}
274#else
275static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
276{
277 /* Nothing to do */
278}
279#endif
280
281#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
283 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
284 defined(CONFIG_CPU_SUBTYPE_SH7786)
285static int scif_txfill(struct uart_port *port)
286{
287 return sci_in(port, SCTFDR) & 0xff;
288}
289
290static int scif_txroom(struct uart_port *port)
291{
292 return SCIF_TXROOM_MAX - scif_txfill(port);
293}
294
295static int scif_rxfill(struct uart_port *port)
296{
297 return sci_in(port, SCRFDR) & 0xff;
298}
299#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
300static int scif_txfill(struct uart_port *port)
301{
302 if (port->mapbase == 0xffe00000 ||
303 port->mapbase == 0xffe08000)
304 /* SCIF0/1*/
305 return sci_in(port, SCTFDR) & 0xff;
306 else
307 /* SCIF2 */
308 return sci_in(port, SCFDR) >> 8;
309}
310
311static int scif_txroom(struct uart_port *port)
312{
313 if (port->mapbase == 0xffe00000 ||
314 port->mapbase == 0xffe08000)
315 /* SCIF0/1*/
316 return SCIF_TXROOM_MAX - scif_txfill(port);
317 else
318 /* SCIF2 */
319 return SCIF2_TXROOM_MAX - scif_txfill(port);
320}
321
322static int scif_rxfill(struct uart_port *port)
323{
324 if ((port->mapbase == 0xffe00000) ||
325 (port->mapbase == 0xffe08000)) {
326 /* SCIF0/1*/
327 return sci_in(port, SCRFDR) & 0xff;
328 } else {
329 /* SCIF2 */
330 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
331 }
332}
333#elif defined(CONFIG_ARCH_SH7372)
334static int scif_txfill(struct uart_port *port)
335{
336 if (port->type == PORT_SCIFA)
337 return sci_in(port, SCFDR) >> 8;
338 else
339 return sci_in(port, SCTFDR);
340}
341
342static int scif_txroom(struct uart_port *port)
343{
344 return port->fifosize - scif_txfill(port);
345}
346
347static int scif_rxfill(struct uart_port *port)
348{
349 if (port->type == PORT_SCIFA)
350 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
351 else
352 return sci_in(port, SCRFDR);
353}
354#else
355static int scif_txfill(struct uart_port *port)
356{
357 return sci_in(port, SCFDR) >> 8;
358}
359
360static int scif_txroom(struct uart_port *port)
361{
362 return SCIF_TXROOM_MAX - scif_txfill(port);
363}
364
365static int scif_rxfill(struct uart_port *port)
366{
367 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
368}
369#endif
370
371static int sci_txfill(struct uart_port *port)
372{
373 return !(sci_in(port, SCxSR) & SCI_TDRE);
374}
375
376static int sci_txroom(struct uart_port *port)
377{
378 return !sci_txfill(port);
379}
380
381static int sci_rxfill(struct uart_port *port)
382{
383 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
384}
385
386/* ********************************************************************** *
387 * the interrupt related routines *
388 * ********************************************************************** */
389
390static void sci_transmit_chars(struct uart_port *port)
391{
392 struct circ_buf *xmit = &port->state->xmit;
393 unsigned int stopped = uart_tx_stopped(port);
394 unsigned short status;
395 unsigned short ctrl;
396 int count;
397
398 status = sci_in(port, SCxSR);
399 if (!(status & SCxSR_TDxE(port))) {
400 ctrl = sci_in(port, SCSCR);
401 if (uart_circ_empty(xmit))
402 ctrl &= ~SCSCR_TIE;
403 else
404 ctrl |= SCSCR_TIE;
405 sci_out(port, SCSCR, ctrl);
406 return;
407 }
408
409 if (port->type == PORT_SCI)
410 count = sci_txroom(port);
411 else
412 count = scif_txroom(port);
413
414 do {
415 unsigned char c;
416
417 if (port->x_char) {
418 c = port->x_char;
419 port->x_char = 0;
420 } else if (!uart_circ_empty(xmit) && !stopped) {
421 c = xmit->buf[xmit->tail];
422 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
423 } else {
424 break;
425 }
426
427 sci_out(port, SCxTDR, c);
428
429 port->icount.tx++;
430 } while (--count > 0);
431
432 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
433
434 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
435 uart_write_wakeup(port);
436 if (uart_circ_empty(xmit)) {
437 sci_stop_tx(port);
438 } else {
439 ctrl = sci_in(port, SCSCR);
440
441 if (port->type != PORT_SCI) {
442 sci_in(port, SCxSR); /* Dummy read */
443 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
444 }
445
446 ctrl |= SCSCR_TIE;
447 sci_out(port, SCSCR, ctrl);
448 }
449}
450
451/* On SH3, SCIF may read end-of-break as a space->mark char */
452#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
453
454static void sci_receive_chars(struct uart_port *port)
455{
456 struct sci_port *sci_port = to_sci_port(port);
457 struct tty_struct *tty = port->state->port.tty;
458 int i, count, copied = 0;
459 unsigned short status;
460 unsigned char flag;
461
462 status = sci_in(port, SCxSR);
463 if (!(status & SCxSR_RDxF(port)))
464 return;
465
466 while (1) {
467 if (port->type == PORT_SCI)
468 count = sci_rxfill(port);
469 else
470 count = scif_rxfill(port);
471
472 /* Don't copy more bytes than there is room for in the buffer */
473 count = tty_buffer_request_room(tty, count);
474
475 /* If for any reason we can't copy more data, we're done! */
476 if (count == 0)
477 break;
478
479 if (port->type == PORT_SCI) {
480 char c = sci_in(port, SCxRDR);
481 if (uart_handle_sysrq_char(port, c) ||
482 sci_port->break_flag)
483 count = 0;
484 else
485 tty_insert_flip_char(tty, c, TTY_NORMAL);
486 } else {
487 for (i = 0; i < count; i++) {
488 char c = sci_in(port, SCxRDR);
489 status = sci_in(port, SCxSR);
490#if defined(CONFIG_CPU_SH3)
491 /* Skip "chars" during break */
492 if (sci_port->break_flag) {
493 if ((c == 0) &&
494 (status & SCxSR_FER(port))) {
495 count--; i--;
496 continue;
497 }
498
499 /* Nonzero => end-of-break */
500 dev_dbg(port->dev, "debounce<%02x>\n", c);
501 sci_port->break_flag = 0;
502
503 if (STEPFN(c)) {
504 count--; i--;
505 continue;
506 }
507 }
508#endif /* CONFIG_CPU_SH3 */
509 if (uart_handle_sysrq_char(port, c)) {
510 count--; i--;
511 continue;
512 }
513
514 /* Store data and status */
515 if (status & SCxSR_FER(port)) {
516 flag = TTY_FRAME;
517 dev_notice(port->dev, "frame error\n");
518 } else if (status & SCxSR_PER(port)) {
519 flag = TTY_PARITY;
520 dev_notice(port->dev, "parity error\n");
521 } else
522 flag = TTY_NORMAL;
523
524 tty_insert_flip_char(tty, c, flag);
525 }
526 }
527
528 sci_in(port, SCxSR); /* dummy read */
529 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
530
531 copied += count;
532 port->icount.rx += count;
533 }
534
535 if (copied) {
536 /* Tell the rest of the system the news. New characters! */
537 tty_flip_buffer_push(tty);
538 } else {
539 sci_in(port, SCxSR); /* dummy read */
540 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
541 }
542}
543
544#define SCI_BREAK_JIFFIES (HZ/20)
545
546/*
547 * The sci generates interrupts during the break,
548 * 1 per millisecond or so during the break period, for 9600 baud.
549 * So dont bother disabling interrupts.
550 * But dont want more than 1 break event.
551 * Use a kernel timer to periodically poll the rx line until
552 * the break is finished.
553 */
554static inline void sci_schedule_break_timer(struct sci_port *port)
555{
556 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
557}
558
559/* Ensure that two consecutive samples find the break over. */
560static void sci_break_timer(unsigned long data)
561{
562 struct sci_port *port = (struct sci_port *)data;
563
564 if (port->enable)
565 port->enable(&port->port);
566
567 if (sci_rxd_in(&port->port) == 0) {
568 port->break_flag = 1;
569 sci_schedule_break_timer(port);
570 } else if (port->break_flag == 1) {
571 /* break is over. */
572 port->break_flag = 2;
573 sci_schedule_break_timer(port);
574 } else
575 port->break_flag = 0;
576
577 if (port->disable)
578 port->disable(&port->port);
579}
580
581static int sci_handle_errors(struct uart_port *port)
582{
583 int copied = 0;
584 unsigned short status = sci_in(port, SCxSR);
585 struct tty_struct *tty = port->state->port.tty;
586
587 if (status & SCxSR_ORER(port)) {
588 /* overrun error */
589 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
590 copied++;
591
592 dev_notice(port->dev, "overrun error");
593 }
594
595 if (status & SCxSR_FER(port)) {
596 if (sci_rxd_in(port) == 0) {
597 /* Notify of BREAK */
598 struct sci_port *sci_port = to_sci_port(port);
599
600 if (!sci_port->break_flag) {
601 sci_port->break_flag = 1;
602 sci_schedule_break_timer(sci_port);
603
604 /* Do sysrq handling. */
605 if (uart_handle_break(port))
606 return 0;
607
608 dev_dbg(port->dev, "BREAK detected\n");
609
610 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
611 copied++;
612 }
613
614 } else {
615 /* frame error */
616 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
617 copied++;
618
619 dev_notice(port->dev, "frame error\n");
620 }
621 }
622
623 if (status & SCxSR_PER(port)) {
624 /* parity error */
625 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
626 copied++;
627
628 dev_notice(port->dev, "parity error");
629 }
630
631 if (copied)
632 tty_flip_buffer_push(tty);
633
634 return copied;
635}
636
637static int sci_handle_fifo_overrun(struct uart_port *port)
638{
639 struct tty_struct *tty = port->state->port.tty;
640 int copied = 0;
641
642 if (port->type != PORT_SCIF)
643 return 0;
644
645 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
646 sci_out(port, SCLSR, 0);
647
648 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
649 tty_flip_buffer_push(tty);
650
651 dev_notice(port->dev, "overrun error\n");
652 copied++;
653 }
654
655 return copied;
656}
657
658static int sci_handle_breaks(struct uart_port *port)
659{
660 int copied = 0;
661 unsigned short status = sci_in(port, SCxSR);
662 struct tty_struct *tty = port->state->port.tty;
663 struct sci_port *s = to_sci_port(port);
664
665 if (uart_handle_break(port))
666 return 0;
667
668 if (!s->break_flag && status & SCxSR_BRK(port)) {
669#if defined(CONFIG_CPU_SH3)
670 /* Debounce break */
671 s->break_flag = 1;
672#endif
673 /* Notify of BREAK */
674 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
675 copied++;
676
677 dev_dbg(port->dev, "BREAK detected\n");
678 }
679
680 if (copied)
681 tty_flip_buffer_push(tty);
682
683 copied += sci_handle_fifo_overrun(port);
684
685 return copied;
686}
687
688static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
689{
690#ifdef CONFIG_SERIAL_SH_SCI_DMA
691 struct uart_port *port = ptr;
692 struct sci_port *s = to_sci_port(port);
693
694 if (s->chan_rx) {
695 u16 scr = sci_in(port, SCSCR);
696 u16 ssr = sci_in(port, SCxSR);
697
698 /* Disable future Rx interrupts */
699 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
700 disable_irq_nosync(irq);
701 scr |= 0x4000;
702 } else {
703 scr &= ~SCSCR_RIE;
704 }
705 sci_out(port, SCSCR, scr);
706 /* Clear current interrupt */
707 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
708 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
709 jiffies, s->rx_timeout);
710 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
711
712 return IRQ_HANDLED;
713 }
714#endif
715
716 /* I think sci_receive_chars has to be called irrespective
717 * of whether the I_IXOFF is set, otherwise, how is the interrupt
718 * to be disabled?
719 */
720 sci_receive_chars(ptr);
721
722 return IRQ_HANDLED;
723}
724
725static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
726{
727 struct uart_port *port = ptr;
728 unsigned long flags;
729
730 spin_lock_irqsave(&port->lock, flags);
731 sci_transmit_chars(port);
732 spin_unlock_irqrestore(&port->lock, flags);
733
734 return IRQ_HANDLED;
735}
736
737static irqreturn_t sci_er_interrupt(int irq, void *ptr)
738{
739 struct uart_port *port = ptr;
740
741 /* Handle errors */
742 if (port->type == PORT_SCI) {
743 if (sci_handle_errors(port)) {
744 /* discard character in rx buffer */
745 sci_in(port, SCxSR);
746 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
747 }
748 } else {
749 sci_handle_fifo_overrun(port);
750 sci_rx_interrupt(irq, ptr);
751 }
752
753 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
754
755 /* Kick the transmission */
756 sci_tx_interrupt(irq, ptr);
757
758 return IRQ_HANDLED;
759}
760
761static irqreturn_t sci_br_interrupt(int irq, void *ptr)
762{
763 struct uart_port *port = ptr;
764
765 /* Handle BREAKs */
766 sci_handle_breaks(port);
767 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
768
769 return IRQ_HANDLED;
770}
771
772static inline unsigned long port_rx_irq_mask(struct uart_port *port)
773{
774 /*
775 * Not all ports (such as SCIFA) will support REIE. Rather than
776 * special-casing the port type, we check the port initialization
777 * IRQ enable mask to see whether the IRQ is desired at all. If
778 * it's unset, it's logically inferred that there's no point in
779 * testing for it.
780 */
781 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
782}
783
784static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
785{
786 unsigned short ssr_status, scr_status, err_enabled;
787 struct uart_port *port = ptr;
788 struct sci_port *s = to_sci_port(port);
789 irqreturn_t ret = IRQ_NONE;
790
791 ssr_status = sci_in(port, SCxSR);
792 scr_status = sci_in(port, SCSCR);
793 err_enabled = scr_status & port_rx_irq_mask(port);
794
795 /* Tx Interrupt */
796 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
797 !s->chan_tx)
798 ret = sci_tx_interrupt(irq, ptr);
799
800 /*
801 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
802 * DR flags
803 */
804 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
805 (scr_status & SCSCR_RIE))
806 ret = sci_rx_interrupt(irq, ptr);
807
808 /* Error Interrupt */
809 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
810 ret = sci_er_interrupt(irq, ptr);
811
812 /* Break Interrupt */
813 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
814 ret = sci_br_interrupt(irq, ptr);
815
816 return ret;
817}
818
819/*
820 * Here we define a transition notifier so that we can update all of our
821 * ports' baud rate when the peripheral clock changes.
822 */
823static int sci_notifier(struct notifier_block *self,
824 unsigned long phase, void *p)
825{
826 struct sci_port *sci_port;
827 unsigned long flags;
828
829 sci_port = container_of(self, struct sci_port, freq_transition);
830
831 if ((phase == CPUFREQ_POSTCHANGE) ||
832 (phase == CPUFREQ_RESUMECHANGE)) {
833 struct uart_port *port = &sci_port->port;
834
835 spin_lock_irqsave(&port->lock, flags);
836 port->uartclk = clk_get_rate(sci_port->iclk);
837 spin_unlock_irqrestore(&port->lock, flags);
838 }
839
840 return NOTIFY_OK;
841}
842
843static void sci_clk_enable(struct uart_port *port)
844{
845 struct sci_port *sci_port = to_sci_port(port);
846
847 pm_runtime_get_sync(port->dev);
848
849 clk_enable(sci_port->iclk);
850 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
851 clk_enable(sci_port->fclk);
852}
853
854static void sci_clk_disable(struct uart_port *port)
855{
856 struct sci_port *sci_port = to_sci_port(port);
857
858 clk_disable(sci_port->fclk);
859 clk_disable(sci_port->iclk);
860
861 pm_runtime_put_sync(port->dev);
862}
863
864static int sci_request_irq(struct sci_port *port)
865{
866 int i;
867 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
868 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
869 sci_br_interrupt,
870 };
871 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
872 "SCI Transmit Data Empty", "SCI Break" };
873
874 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
875 if (unlikely(!port->cfg->irqs[0]))
876 return -ENODEV;
877
878 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
879 IRQF_DISABLED, "sci", port)) {
880 dev_err(port->port.dev, "Can't allocate IRQ\n");
881 return -ENODEV;
882 }
883 } else {
884 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
885 if (unlikely(!port->cfg->irqs[i]))
886 continue;
887
888 if (request_irq(port->cfg->irqs[i], handlers[i],
889 IRQF_DISABLED, desc[i], port)) {
890 dev_err(port->port.dev, "Can't allocate IRQ\n");
891 return -ENODEV;
892 }
893 }
894 }
895
896 return 0;
897}
898
899static void sci_free_irq(struct sci_port *port)
900{
901 int i;
902
903 if (port->cfg->irqs[0] == port->cfg->irqs[1])
904 free_irq(port->cfg->irqs[0], port);
905 else {
906 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
907 if (!port->cfg->irqs[i])
908 continue;
909
910 free_irq(port->cfg->irqs[i], port);
911 }
912 }
913}
914
915static unsigned int sci_tx_empty(struct uart_port *port)
916{
917 unsigned short status = sci_in(port, SCxSR);
918 unsigned short in_tx_fifo = scif_txfill(port);
919
920 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
921}
922
923static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
924{
925 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
926 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
927 /* If you have signals for DTR and DCD, please implement here. */
928}
929
930static unsigned int sci_get_mctrl(struct uart_port *port)
931{
932 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
933 and CTS/RTS */
934
935 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
936}
937
938#ifdef CONFIG_SERIAL_SH_SCI_DMA
939static void sci_dma_tx_complete(void *arg)
940{
941 struct sci_port *s = arg;
942 struct uart_port *port = &s->port;
943 struct circ_buf *xmit = &port->state->xmit;
944 unsigned long flags;
945
946 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
947
948 spin_lock_irqsave(&port->lock, flags);
949
950 xmit->tail += sg_dma_len(&s->sg_tx);
951 xmit->tail &= UART_XMIT_SIZE - 1;
952
953 port->icount.tx += sg_dma_len(&s->sg_tx);
954
955 async_tx_ack(s->desc_tx);
956 s->cookie_tx = -EINVAL;
957 s->desc_tx = NULL;
958
959 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
960 uart_write_wakeup(port);
961
962 if (!uart_circ_empty(xmit)) {
963 schedule_work(&s->work_tx);
964 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
965 u16 ctrl = sci_in(port, SCSCR);
966 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
967 }
968
969 spin_unlock_irqrestore(&port->lock, flags);
970}
971
972/* Locking: called with port lock held */
973static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
974 size_t count)
975{
976 struct uart_port *port = &s->port;
977 int i, active, room;
978
979 room = tty_buffer_request_room(tty, count);
980
981 if (s->active_rx == s->cookie_rx[0]) {
982 active = 0;
983 } else if (s->active_rx == s->cookie_rx[1]) {
984 active = 1;
985 } else {
986 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
987 return 0;
988 }
989
990 if (room < count)
991 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
992 count - room);
993 if (!room)
994 return room;
995
996 for (i = 0; i < room; i++)
997 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
998 TTY_NORMAL);
999
1000 port->icount.rx += room;
1001
1002 return room;
1003}
1004
1005static void sci_dma_rx_complete(void *arg)
1006{
1007 struct sci_port *s = arg;
1008 struct uart_port *port = &s->port;
1009 struct tty_struct *tty = port->state->port.tty;
1010 unsigned long flags;
1011 int count;
1012
1013 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
1014
1015 spin_lock_irqsave(&port->lock, flags);
1016
1017 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1018
1019 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1020
1021 spin_unlock_irqrestore(&port->lock, flags);
1022
1023 if (count)
1024 tty_flip_buffer_push(tty);
1025
1026 schedule_work(&s->work_rx);
1027}
1028
1029static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1030{
1031 struct dma_chan *chan = s->chan_rx;
1032 struct uart_port *port = &s->port;
1033
1034 s->chan_rx = NULL;
1035 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1036 dma_release_channel(chan);
1037 if (sg_dma_address(&s->sg_rx[0]))
1038 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1039 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1040 if (enable_pio)
1041 sci_start_rx(port);
1042}
1043
1044static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1045{
1046 struct dma_chan *chan = s->chan_tx;
1047 struct uart_port *port = &s->port;
1048
1049 s->chan_tx = NULL;
1050 s->cookie_tx = -EINVAL;
1051 dma_release_channel(chan);
1052 if (enable_pio)
1053 sci_start_tx(port);
1054}
1055
1056static void sci_submit_rx(struct sci_port *s)
1057{
1058 struct dma_chan *chan = s->chan_rx;
1059 int i;
1060
1061 for (i = 0; i < 2; i++) {
1062 struct scatterlist *sg = &s->sg_rx[i];
1063 struct dma_async_tx_descriptor *desc;
1064
1065 desc = chan->device->device_prep_slave_sg(chan,
1066 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1067
1068 if (desc) {
1069 s->desc_rx[i] = desc;
1070 desc->callback = sci_dma_rx_complete;
1071 desc->callback_param = s;
1072 s->cookie_rx[i] = desc->tx_submit(desc);
1073 }
1074
1075 if (!desc || s->cookie_rx[i] < 0) {
1076 if (i) {
1077 async_tx_ack(s->desc_rx[0]);
1078 s->cookie_rx[0] = -EINVAL;
1079 }
1080 if (desc) {
1081 async_tx_ack(desc);
1082 s->cookie_rx[i] = -EINVAL;
1083 }
1084 dev_warn(s->port.dev,
1085 "failed to re-start DMA, using PIO\n");
1086 sci_rx_dma_release(s, true);
1087 return;
1088 }
1089 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1090 s->cookie_rx[i], i);
1091 }
1092
1093 s->active_rx = s->cookie_rx[0];
1094
1095 dma_async_issue_pending(chan);
1096}
1097
1098static void work_fn_rx(struct work_struct *work)
1099{
1100 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1101 struct uart_port *port = &s->port;
1102 struct dma_async_tx_descriptor *desc;
1103 int new;
1104
1105 if (s->active_rx == s->cookie_rx[0]) {
1106 new = 0;
1107 } else if (s->active_rx == s->cookie_rx[1]) {
1108 new = 1;
1109 } else {
1110 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1111 return;
1112 }
1113 desc = s->desc_rx[new];
1114
1115 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1116 DMA_SUCCESS) {
1117 /* Handle incomplete DMA receive */
1118 struct tty_struct *tty = port->state->port.tty;
1119 struct dma_chan *chan = s->chan_rx;
1120 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1121 async_tx);
1122 unsigned long flags;
1123 int count;
1124
1125 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
1126 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1127 sh_desc->partial, sh_desc->cookie);
1128
1129 spin_lock_irqsave(&port->lock, flags);
1130 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1131 spin_unlock_irqrestore(&port->lock, flags);
1132
1133 if (count)
1134 tty_flip_buffer_push(tty);
1135
1136 sci_submit_rx(s);
1137
1138 return;
1139 }
1140
1141 s->cookie_rx[new] = desc->tx_submit(desc);
1142 if (s->cookie_rx[new] < 0) {
1143 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1144 sci_rx_dma_release(s, true);
1145 return;
1146 }
1147
1148 s->active_rx = s->cookie_rx[!new];
1149
1150 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1151 s->cookie_rx[new], new, s->active_rx);
1152}
1153
1154static void work_fn_tx(struct work_struct *work)
1155{
1156 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1157 struct dma_async_tx_descriptor *desc;
1158 struct dma_chan *chan = s->chan_tx;
1159 struct uart_port *port = &s->port;
1160 struct circ_buf *xmit = &port->state->xmit;
1161 struct scatterlist *sg = &s->sg_tx;
1162
1163 /*
1164 * DMA is idle now.
1165 * Port xmit buffer is already mapped, and it is one page... Just adjust
1166 * offsets and lengths. Since it is a circular buffer, we have to
1167 * transmit till the end, and then the rest. Take the port lock to get a
1168 * consistent xmit buffer state.
1169 */
1170 spin_lock_irq(&port->lock);
1171 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
1172 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
1173 sg->offset;
1174 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1175 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1176 spin_unlock_irq(&port->lock);
1177
1178 BUG_ON(!sg_dma_len(sg));
1179
1180 desc = chan->device->device_prep_slave_sg(chan,
1181 sg, s->sg_len_tx, DMA_TO_DEVICE,
1182 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1183 if (!desc) {
1184 /* switch to PIO */
1185 sci_tx_dma_release(s, true);
1186 return;
1187 }
1188
1189 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1190
1191 spin_lock_irq(&port->lock);
1192 s->desc_tx = desc;
1193 desc->callback = sci_dma_tx_complete;
1194 desc->callback_param = s;
1195 spin_unlock_irq(&port->lock);
1196 s->cookie_tx = desc->tx_submit(desc);
1197 if (s->cookie_tx < 0) {
1198 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1199 /* switch to PIO */
1200 sci_tx_dma_release(s, true);
1201 return;
1202 }
1203
1204 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1205 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1206
1207 dma_async_issue_pending(chan);
1208}
1209#endif
1210
1211static void sci_start_tx(struct uart_port *port)
1212{
1213 struct sci_port *s = to_sci_port(port);
1214 unsigned short ctrl;
1215
1216#ifdef CONFIG_SERIAL_SH_SCI_DMA
1217 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1218 u16 new, scr = sci_in(port, SCSCR);
1219 if (s->chan_tx)
1220 new = scr | 0x8000;
1221 else
1222 new = scr & ~0x8000;
1223 if (new != scr)
1224 sci_out(port, SCSCR, new);
1225 }
1226
1227 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1228 s->cookie_tx < 0)
1229 schedule_work(&s->work_tx);
1230#endif
1231
1232 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1233 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1234 ctrl = sci_in(port, SCSCR);
1235 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
1236 }
1237}
1238
1239static void sci_stop_tx(struct uart_port *port)
1240{
1241 unsigned short ctrl;
1242
1243 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1244 ctrl = sci_in(port, SCSCR);
1245
1246 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1247 ctrl &= ~0x8000;
1248
1249 ctrl &= ~SCSCR_TIE;
1250
1251 sci_out(port, SCSCR, ctrl);
1252}
1253
1254static void sci_start_rx(struct uart_port *port)
1255{
1256 unsigned short ctrl;
1257
1258 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1259
1260 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1261 ctrl &= ~0x4000;
1262
1263 sci_out(port, SCSCR, ctrl);
1264}
1265
1266static void sci_stop_rx(struct uart_port *port)
1267{
1268 unsigned short ctrl;
1269
1270 ctrl = sci_in(port, SCSCR);
1271
1272 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1273 ctrl &= ~0x4000;
1274
1275 ctrl &= ~port_rx_irq_mask(port);
1276
1277 sci_out(port, SCSCR, ctrl);
1278}
1279
1280static void sci_enable_ms(struct uart_port *port)
1281{
1282 /* Nothing here yet .. */
1283}
1284
1285static void sci_break_ctl(struct uart_port *port, int break_state)
1286{
1287 /* Nothing here yet .. */
1288}
1289
1290#ifdef CONFIG_SERIAL_SH_SCI_DMA
1291static bool filter(struct dma_chan *chan, void *slave)
1292{
1293 struct sh_dmae_slave *param = slave;
1294
1295 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1296 param->slave_id);
1297
1298 if (param->dma_dev == chan->device->dev) {
1299 chan->private = param;
1300 return true;
1301 } else {
1302 return false;
1303 }
1304}
1305
1306static void rx_timer_fn(unsigned long arg)
1307{
1308 struct sci_port *s = (struct sci_port *)arg;
1309 struct uart_port *port = &s->port;
1310 u16 scr = sci_in(port, SCSCR);
1311
1312 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1313 scr &= ~0x4000;
1314 enable_irq(s->cfg->irqs[1]);
1315 }
1316 sci_out(port, SCSCR, scr | SCSCR_RIE);
1317 dev_dbg(port->dev, "DMA Rx timed out\n");
1318 schedule_work(&s->work_rx);
1319}
1320
1321static void sci_request_dma(struct uart_port *port)
1322{
1323 struct sci_port *s = to_sci_port(port);
1324 struct sh_dmae_slave *param;
1325 struct dma_chan *chan;
1326 dma_cap_mask_t mask;
1327 int nent;
1328
1329 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1330 port->line, s->cfg->dma_dev);
1331
1332 if (!s->cfg->dma_dev)
1333 return;
1334
1335 dma_cap_zero(mask);
1336 dma_cap_set(DMA_SLAVE, mask);
1337
1338 param = &s->param_tx;
1339
1340 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1341 param->slave_id = s->cfg->dma_slave_tx;
1342 param->dma_dev = s->cfg->dma_dev;
1343
1344 s->cookie_tx = -EINVAL;
1345 chan = dma_request_channel(mask, filter, param);
1346 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1347 if (chan) {
1348 s->chan_tx = chan;
1349 sg_init_table(&s->sg_tx, 1);
1350 /* UART circular tx buffer is an aligned page. */
1351 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1352 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1353 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1354 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1355 if (!nent)
1356 sci_tx_dma_release(s, false);
1357 else
1358 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1359 sg_dma_len(&s->sg_tx),
1360 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1361
1362 s->sg_len_tx = nent;
1363
1364 INIT_WORK(&s->work_tx, work_fn_tx);
1365 }
1366
1367 param = &s->param_rx;
1368
1369 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1370 param->slave_id = s->cfg->dma_slave_rx;
1371 param->dma_dev = s->cfg->dma_dev;
1372
1373 chan = dma_request_channel(mask, filter, param);
1374 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1375 if (chan) {
1376 dma_addr_t dma[2];
1377 void *buf[2];
1378 int i;
1379
1380 s->chan_rx = chan;
1381
1382 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1383 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1384 &dma[0], GFP_KERNEL);
1385
1386 if (!buf[0]) {
1387 dev_warn(port->dev,
1388 "failed to allocate dma buffer, using PIO\n");
1389 sci_rx_dma_release(s, true);
1390 return;
1391 }
1392
1393 buf[1] = buf[0] + s->buf_len_rx;
1394 dma[1] = dma[0] + s->buf_len_rx;
1395
1396 for (i = 0; i < 2; i++) {
1397 struct scatterlist *sg = &s->sg_rx[i];
1398
1399 sg_init_table(sg, 1);
1400 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1401 (int)buf[i] & ~PAGE_MASK);
1402 sg_dma_address(sg) = dma[i];
1403 }
1404
1405 INIT_WORK(&s->work_rx, work_fn_rx);
1406 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1407
1408 sci_submit_rx(s);
1409 }
1410}
1411
1412static void sci_free_dma(struct uart_port *port)
1413{
1414 struct sci_port *s = to_sci_port(port);
1415
1416 if (!s->cfg->dma_dev)
1417 return;
1418
1419 if (s->chan_tx)
1420 sci_tx_dma_release(s, false);
1421 if (s->chan_rx)
1422 sci_rx_dma_release(s, false);
1423}
1424#else
1425static inline void sci_request_dma(struct uart_port *port)
1426{
1427}
1428
1429static inline void sci_free_dma(struct uart_port *port)
1430{
1431}
1432#endif
1433
1434static int sci_startup(struct uart_port *port)
1435{
1436 struct sci_port *s = to_sci_port(port);
1437 int ret;
1438
1439 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1440
1441 if (s->enable)
1442 s->enable(port);
1443
1444 ret = sci_request_irq(s);
1445 if (unlikely(ret < 0))
1446 return ret;
1447
1448 sci_request_dma(port);
1449
1450 sci_start_tx(port);
1451 sci_start_rx(port);
1452
1453 return 0;
1454}
1455
1456static void sci_shutdown(struct uart_port *port)
1457{
1458 struct sci_port *s = to_sci_port(port);
1459
1460 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1461
1462 sci_stop_rx(port);
1463 sci_stop_tx(port);
1464
1465 sci_free_dma(port);
1466 sci_free_irq(s);
1467
1468 if (s->disable)
1469 s->disable(port);
1470}
1471
1472static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1473 unsigned long freq)
1474{
1475 switch (algo_id) {
1476 case SCBRR_ALGO_1:
1477 return ((freq + 16 * bps) / (16 * bps) - 1);
1478 case SCBRR_ALGO_2:
1479 return ((freq + 16 * bps) / (32 * bps) - 1);
1480 case SCBRR_ALGO_3:
1481 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1482 case SCBRR_ALGO_4:
1483 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1484 case SCBRR_ALGO_5:
1485 return (((freq * 1000 / 32) / bps) - 1);
1486 }
1487
1488 /* Warn, but use a safe default */
1489 WARN_ON(1);
1490
1491 return ((freq + 16 * bps) / (32 * bps) - 1);
1492}
1493
1494static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1495 struct ktermios *old)
1496{
1497 struct sci_port *s = to_sci_port(port);
1498 unsigned int status, baud, smr_val, max_baud;
1499 int t = -1;
1500 u16 scfcr = 0;
1501
1502 /*
1503 * earlyprintk comes here early on with port->uartclk set to zero.
1504 * the clock framework is not up and running at this point so here
1505 * we assume that 115200 is the maximum baud rate. please note that
1506 * the baud rate is not programmed during earlyprintk - it is assumed
1507 * that the previous boot loader has enabled required clocks and
1508 * setup the baud rate generator hardware for us already.
1509 */
1510 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1511
1512 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1513 if (likely(baud && port->uartclk))
1514 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
1515
1516 if (s->enable)
1517 s->enable(port);
1518
1519 do {
1520 status = sci_in(port, SCxSR);
1521 } while (!(status & SCxSR_TEND(port)));
1522
1523 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1524
1525 if (port->type != PORT_SCI)
1526 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
1527
1528 smr_val = sci_in(port, SCSMR) & 3;
1529
1530 if ((termios->c_cflag & CSIZE) == CS7)
1531 smr_val |= 0x40;
1532 if (termios->c_cflag & PARENB)
1533 smr_val |= 0x20;
1534 if (termios->c_cflag & PARODD)
1535 smr_val |= 0x30;
1536 if (termios->c_cflag & CSTOPB)
1537 smr_val |= 0x08;
1538
1539 uart_update_timeout(port, termios->c_cflag, baud);
1540
1541 sci_out(port, SCSMR, smr_val);
1542
1543 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1544 s->cfg->scscr);
1545
1546 if (t > 0) {
1547 if (t >= 256) {
1548 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1549 t >>= 2;
1550 } else
1551 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1552
1553 sci_out(port, SCBRR, t);
1554 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1555 }
1556
1557 sci_init_pins(port, termios->c_cflag);
1558 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
1559
1560 sci_out(port, SCSCR, s->cfg->scscr);
1561
1562#ifdef CONFIG_SERIAL_SH_SCI_DMA
1563 /*
1564 * Calculate delay for 1.5 DMA buffers: see
1565 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1566 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1567 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1568 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1569 * sizes), but it has been found out experimentally, that this is not
1570 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1571 * as a minimum seem to work perfectly.
1572 */
1573 if (s->chan_rx) {
1574 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1575 port->fifosize / 2;
1576 dev_dbg(port->dev,
1577 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1578 s->rx_timeout * 1000 / HZ, port->timeout);
1579 if (s->rx_timeout < msecs_to_jiffies(20))
1580 s->rx_timeout = msecs_to_jiffies(20);
1581 }
1582#endif
1583
1584 if ((termios->c_cflag & CREAD) != 0)
1585 sci_start_rx(port);
1586
1587 if (s->disable)
1588 s->disable(port);
1589}
1590
1591static const char *sci_type(struct uart_port *port)
1592{
1593 switch (port->type) {
1594 case PORT_IRDA:
1595 return "irda";
1596 case PORT_SCI:
1597 return "sci";
1598 case PORT_SCIF:
1599 return "scif";
1600 case PORT_SCIFA:
1601 return "scifa";
1602 case PORT_SCIFB:
1603 return "scifb";
1604 }
1605
1606 return NULL;
1607}
1608
1609static inline unsigned long sci_port_size(struct uart_port *port)
1610{
1611 /*
1612 * Pick an arbitrary size that encapsulates all of the base
1613 * registers by default. This can be optimized later, or derived
1614 * from platform resource data at such a time that ports begin to
1615 * behave more erratically.
1616 */
1617 return 64;
1618}
1619
1620static int sci_remap_port(struct uart_port *port)
1621{
1622 unsigned long size = sci_port_size(port);
1623
1624 /*
1625 * Nothing to do if there's already an established membase.
1626 */
1627 if (port->membase)
1628 return 0;
1629
1630 if (port->flags & UPF_IOREMAP) {
1631 port->membase = ioremap_nocache(port->mapbase, size);
1632 if (unlikely(!port->membase)) {
1633 dev_err(port->dev, "can't remap port#%d\n", port->line);
1634 return -ENXIO;
1635 }
1636 } else {
1637 /*
1638 * For the simple (and majority of) cases where we don't
1639 * need to do any remapping, just cast the cookie
1640 * directly.
1641 */
1642 port->membase = (void __iomem *)port->mapbase;
1643 }
1644
1645 return 0;
1646}
1647
1648static void sci_release_port(struct uart_port *port)
1649{
1650 if (port->flags & UPF_IOREMAP) {
1651 iounmap(port->membase);
1652 port->membase = NULL;
1653 }
1654
1655 release_mem_region(port->mapbase, sci_port_size(port));
1656}
1657
1658static int sci_request_port(struct uart_port *port)
1659{
1660 unsigned long size = sci_port_size(port);
1661 struct resource *res;
1662 int ret;
1663
1664 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
1665 if (unlikely(res == NULL))
1666 return -EBUSY;
1667
1668 ret = sci_remap_port(port);
1669 if (unlikely(ret != 0)) {
1670 release_resource(res);
1671 return ret;
1672 }
1673
1674 return 0;
1675}
1676
1677static void sci_config_port(struct uart_port *port, int flags)
1678{
1679 if (flags & UART_CONFIG_TYPE) {
1680 struct sci_port *sport = to_sci_port(port);
1681
1682 port->type = sport->cfg->type;
1683 sci_request_port(port);
1684 }
1685}
1686
1687static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1688{
1689 struct sci_port *s = to_sci_port(port);
1690
1691 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
1692 return -EINVAL;
1693 if (ser->baud_base < 2400)
1694 /* No paper tape reader for Mitch.. */
1695 return -EINVAL;
1696
1697 return 0;
1698}
1699
1700static struct uart_ops sci_uart_ops = {
1701 .tx_empty = sci_tx_empty,
1702 .set_mctrl = sci_set_mctrl,
1703 .get_mctrl = sci_get_mctrl,
1704 .start_tx = sci_start_tx,
1705 .stop_tx = sci_stop_tx,
1706 .stop_rx = sci_stop_rx,
1707 .enable_ms = sci_enable_ms,
1708 .break_ctl = sci_break_ctl,
1709 .startup = sci_startup,
1710 .shutdown = sci_shutdown,
1711 .set_termios = sci_set_termios,
1712 .type = sci_type,
1713 .release_port = sci_release_port,
1714 .request_port = sci_request_port,
1715 .config_port = sci_config_port,
1716 .verify_port = sci_verify_port,
1717#ifdef CONFIG_CONSOLE_POLL
1718 .poll_get_char = sci_poll_get_char,
1719 .poll_put_char = sci_poll_put_char,
1720#endif
1721};
1722
1723static int __devinit sci_init_single(struct platform_device *dev,
1724 struct sci_port *sci_port,
1725 unsigned int index,
1726 struct plat_sci_port *p)
1727{
1728 struct uart_port *port = &sci_port->port;
1729
1730 port->ops = &sci_uart_ops;
1731 port->iotype = UPIO_MEM;
1732 port->line = index;
1733
1734 switch (p->type) {
1735 case PORT_SCIFB:
1736 port->fifosize = 256;
1737 break;
1738 case PORT_SCIFA:
1739 port->fifosize = 64;
1740 break;
1741 case PORT_SCIF:
1742 port->fifosize = 16;
1743 break;
1744 default:
1745 port->fifosize = 1;
1746 break;
1747 }
1748
1749 if (dev) {
1750 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1751 if (IS_ERR(sci_port->iclk)) {
1752 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1753 if (IS_ERR(sci_port->iclk)) {
1754 dev_err(&dev->dev, "can't get iclk\n");
1755 return PTR_ERR(sci_port->iclk);
1756 }
1757 }
1758
1759 /*
1760 * The function clock is optional, ignore it if we can't
1761 * find it.
1762 */
1763 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1764 if (IS_ERR(sci_port->fclk))
1765 sci_port->fclk = NULL;
1766
1767 sci_port->enable = sci_clk_enable;
1768 sci_port->disable = sci_clk_disable;
1769 port->dev = &dev->dev;
1770
1771 pm_runtime_enable(&dev->dev);
1772 }
1773
1774 sci_port->break_timer.data = (unsigned long)sci_port;
1775 sci_port->break_timer.function = sci_break_timer;
1776 init_timer(&sci_port->break_timer);
1777
1778 sci_port->cfg = p;
1779
1780 port->mapbase = p->mapbase;
1781 port->type = p->type;
1782 port->flags = p->flags;
1783
1784 /*
1785 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1786 * for the multi-IRQ ports, which is where we are primarily
1787 * concerned with the shutdown path synchronization.
1788 *
1789 * For the muxed case there's nothing more to do.
1790 */
1791 port->irq = p->irqs[SCIx_RXI_IRQ];
1792
1793 if (p->dma_dev)
1794 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1795 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1796
1797 return 0;
1798}
1799
1800#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1801static void serial_console_putchar(struct uart_port *port, int ch)
1802{
1803 sci_poll_put_char(port, ch);
1804}
1805
1806/*
1807 * Print a string to the serial port trying not to disturb
1808 * any possible real use of the port...
1809 */
1810static void serial_console_write(struct console *co, const char *s,
1811 unsigned count)
1812{
1813 struct sci_port *sci_port = &sci_ports[co->index];
1814 struct uart_port *port = &sci_port->port;
1815 unsigned short bits;
1816
1817 if (sci_port->enable)
1818 sci_port->enable(port);
1819
1820 uart_console_write(port, s, count, serial_console_putchar);
1821
1822 /* wait until fifo is empty and last bit has been transmitted */
1823 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1824 while ((sci_in(port, SCxSR) & bits) != bits)
1825 cpu_relax();
1826
1827 if (sci_port->disable)
1828 sci_port->disable(port);
1829}
1830
1831static int __devinit serial_console_setup(struct console *co, char *options)
1832{
1833 struct sci_port *sci_port;
1834 struct uart_port *port;
1835 int baud = 115200;
1836 int bits = 8;
1837 int parity = 'n';
1838 int flow = 'n';
1839 int ret;
1840
1841 /*
1842 * Refuse to handle any bogus ports.
1843 */
1844 if (co->index < 0 || co->index >= SCI_NPORTS)
1845 return -ENODEV;
1846
1847 sci_port = &sci_ports[co->index];
1848 port = &sci_port->port;
1849
1850 /*
1851 * Refuse to handle uninitialized ports.
1852 */
1853 if (!port->ops)
1854 return -ENODEV;
1855
1856 ret = sci_remap_port(port);
1857 if (unlikely(ret != 0))
1858 return ret;
1859
1860 if (sci_port->enable)
1861 sci_port->enable(port);
1862
1863 if (options)
1864 uart_parse_options(options, &baud, &parity, &bits, &flow);
1865
1866 ret = uart_set_options(port, co, baud, parity, bits, flow);
1867#if defined(__H8300H__) || defined(__H8300S__)
1868 /* disable rx interrupt */
1869 if (ret == 0)
1870 sci_stop_rx(port);
1871#endif
1872 /* TODO: disable clock */
1873 return ret;
1874}
1875
1876static struct console serial_console = {
1877 .name = "ttySC",
1878 .device = uart_console_device,
1879 .write = serial_console_write,
1880 .setup = serial_console_setup,
1881 .flags = CON_PRINTBUFFER,
1882 .index = -1,
1883 .data = &sci_uart_driver,
1884};
1885
1886static struct console early_serial_console = {
1887 .name = "early_ttySC",
1888 .write = serial_console_write,
1889 .flags = CON_PRINTBUFFER,
1890 .index = -1,
1891};
1892
1893static char early_serial_buf[32];
1894
1895static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1896{
1897 struct plat_sci_port *cfg = pdev->dev.platform_data;
1898
1899 if (early_serial_console.data)
1900 return -EEXIST;
1901
1902 early_serial_console.index = pdev->id;
1903
1904 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
1905
1906 serial_console_setup(&early_serial_console, early_serial_buf);
1907
1908 if (!strstr(early_serial_buf, "keep"))
1909 early_serial_console.flags |= CON_BOOT;
1910
1911 register_console(&early_serial_console);
1912 return 0;
1913}
1914
1915#define SCI_CONSOLE (&serial_console)
1916
1917#else
1918static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1919{
1920 return -EINVAL;
1921}
1922
1923#define SCI_CONSOLE NULL
1924
1925#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1926
1927static char banner[] __initdata =
1928 KERN_INFO "SuperH SCI(F) driver initialized\n";
1929
1930static struct uart_driver sci_uart_driver = {
1931 .owner = THIS_MODULE,
1932 .driver_name = "sci",
1933 .dev_name = "ttySC",
1934 .major = SCI_MAJOR,
1935 .minor = SCI_MINOR_START,
1936 .nr = SCI_NPORTS,
1937 .cons = SCI_CONSOLE,
1938};
1939
1940static int sci_remove(struct platform_device *dev)
1941{
1942 struct sci_port *port = platform_get_drvdata(dev);
1943
1944 cpufreq_unregister_notifier(&port->freq_transition,
1945 CPUFREQ_TRANSITION_NOTIFIER);
1946
1947 uart_remove_one_port(&sci_uart_driver, &port->port);
1948
1949 clk_put(port->iclk);
1950 clk_put(port->fclk);
1951
1952 pm_runtime_disable(&dev->dev);
1953 return 0;
1954}
1955
1956static int __devinit sci_probe_single(struct platform_device *dev,
1957 unsigned int index,
1958 struct plat_sci_port *p,
1959 struct sci_port *sciport)
1960{
1961 int ret;
1962
1963 /* Sanity check */
1964 if (unlikely(index >= SCI_NPORTS)) {
1965 dev_notice(&dev->dev, "Attempting to register port "
1966 "%d when only %d are available.\n",
1967 index+1, SCI_NPORTS);
1968 dev_notice(&dev->dev, "Consider bumping "
1969 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1970 return 0;
1971 }
1972
1973 ret = sci_init_single(dev, sciport, index, p);
1974 if (ret)
1975 return ret;
1976
1977 return uart_add_one_port(&sci_uart_driver, &sciport->port);
1978}
1979
1980static int __devinit sci_probe(struct platform_device *dev)
1981{
1982 struct plat_sci_port *p = dev->dev.platform_data;
1983 struct sci_port *sp = &sci_ports[dev->id];
1984 int ret;
1985
1986 /*
1987 * If we've come here via earlyprintk initialization, head off to
1988 * the special early probe. We don't have sufficient device state
1989 * to make it beyond this yet.
1990 */
1991 if (is_early_platform_device(dev))
1992 return sci_probe_earlyprintk(dev);
1993
1994 platform_set_drvdata(dev, sp);
1995
1996 ret = sci_probe_single(dev, dev->id, p, sp);
1997 if (ret)
1998 goto err_unreg;
1999
2000 sp->freq_transition.notifier_call = sci_notifier;
2001
2002 ret = cpufreq_register_notifier(&sp->freq_transition,
2003 CPUFREQ_TRANSITION_NOTIFIER);
2004 if (unlikely(ret < 0))
2005 goto err_unreg;
2006
2007#ifdef CONFIG_SH_STANDARD_BIOS
2008 sh_bios_gdb_detach();
2009#endif
2010
2011 return 0;
2012
2013err_unreg:
2014 sci_remove(dev);
2015 return ret;
2016}
2017
2018static int sci_suspend(struct device *dev)
2019{
2020 struct sci_port *sport = dev_get_drvdata(dev);
2021
2022 if (sport)
2023 uart_suspend_port(&sci_uart_driver, &sport->port);
2024
2025 return 0;
2026}
2027
2028static int sci_resume(struct device *dev)
2029{
2030 struct sci_port *sport = dev_get_drvdata(dev);
2031
2032 if (sport)
2033 uart_resume_port(&sci_uart_driver, &sport->port);
2034
2035 return 0;
2036}
2037
2038static const struct dev_pm_ops sci_dev_pm_ops = {
2039 .suspend = sci_suspend,
2040 .resume = sci_resume,
2041};
2042
2043static struct platform_driver sci_driver = {
2044 .probe = sci_probe,
2045 .remove = sci_remove,
2046 .driver = {
2047 .name = "sh-sci",
2048 .owner = THIS_MODULE,
2049 .pm = &sci_dev_pm_ops,
2050 },
2051};
2052
2053static int __init sci_init(void)
2054{
2055 int ret;
2056
2057 printk(banner);
2058
2059 ret = uart_register_driver(&sci_uart_driver);
2060 if (likely(ret == 0)) {
2061 ret = platform_driver_register(&sci_driver);
2062 if (unlikely(ret))
2063 uart_unregister_driver(&sci_uart_driver);
2064 }
2065
2066 return ret;
2067}
2068
2069static void __exit sci_exit(void)
2070{
2071 platform_driver_unregister(&sci_driver);
2072 uart_unregister_driver(&sci_uart_driver);
2073}
2074
2075#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2076early_platform_init_buffer("earlyprintk", &sci_driver,
2077 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2078#endif
2079module_init(sci_init);
2080module_exit(sci_exit);
2081
2082MODULE_LICENSE("GPL");
2083MODULE_ALIAS("platform:sh-sci");
diff --git a/drivers/tty/serial/sh-sci.h b/drivers/tty/serial/sh-sci.h
new file mode 100644
index 000000000000..b04d937c9110
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.h
@@ -0,0 +1,468 @@
1#include <linux/serial_core.h>
2#include <linux/io.h>
3#include <linux/gpio.h>
4
5#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
6#include <asm/regs306x.h>
7#endif
8#if defined(CONFIG_H8S2678)
9#include <asm/regs267x.h>
10#endif
11
12#if defined(CONFIG_CPU_SUBTYPE_SH7706) || \
13 defined(CONFIG_CPU_SUBTYPE_SH7707) || \
14 defined(CONFIG_CPU_SUBTYPE_SH7708) || \
15 defined(CONFIG_CPU_SUBTYPE_SH7709)
16# define SCPCR 0xA4000116 /* 16 bit SCI and SCIF */
17# define SCPDR 0xA4000136 /* 8 bit SCI and SCIF */
18#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
19# define SCIF0 0xA4400000
20# define SCIF2 0xA4410000
21# define SCPCR 0xA4000116
22# define SCPDR 0xA4000136
23#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || \
24 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
25 defined(CONFIG_ARCH_SH73A0) || \
26 defined(CONFIG_ARCH_SH7367) || \
27 defined(CONFIG_ARCH_SH7377) || \
28 defined(CONFIG_ARCH_SH7372)
29# define PORT_PTCR 0xA405011EUL
30# define PORT_PVCR 0xA4050122UL
31# define SCIF_ORER 0x0200 /* overrun error bit */
32#elif defined(CONFIG_SH_RTS7751R2D)
33# define SCSPTR1 0xFFE0001C /* 8 bit SCIF */
34# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
35# define SCIF_ORER 0x0001 /* overrun error bit */
36#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
37 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
38 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
39 defined(CONFIG_CPU_SUBTYPE_SH7091) || \
40 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
41 defined(CONFIG_CPU_SUBTYPE_SH7751R)
42# define SCSPTR1 0xffe0001c /* 8 bit SCI */
43# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
44# define SCIF_ORER 0x0001 /* overrun error bit */
45#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
46# define SCSPTR0 0xfe600024 /* 16 bit SCIF */
47# define SCSPTR1 0xfe610024 /* 16 bit SCIF */
48# define SCSPTR2 0xfe620024 /* 16 bit SCIF */
49# define SCIF_ORER 0x0001 /* overrun error bit */
50#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
51# define SCSPTR0 0xA4400000 /* 16 bit SCIF */
52# define SCIF_ORER 0x0001 /* overrun error bit */
53# define PACR 0xa4050100
54# define PBCR 0xa4050102
55#elif defined(CONFIG_CPU_SUBTYPE_SH7343)
56# define SCSPTR0 0xffe00010 /* 16 bit SCIF */
57#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
58# define PADR 0xA4050120
59# define PSDR 0xA405013e
60# define PWDR 0xA4050166
61# define PSCR 0xA405011E
62# define SCIF_ORER 0x0001 /* overrun error bit */
63#elif defined(CONFIG_CPU_SUBTYPE_SH7366)
64# define SCPDR0 0xA405013E /* 16 bit SCIF0 PSDR */
65# define SCSPTR0 SCPDR0
66# define SCIF_ORER 0x0001 /* overrun error bit */
67#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
68# define SCSPTR0 0xa4050160
69# define SCIF_ORER 0x0001 /* overrun error bit */
70#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
71# define SCIF_ORER 0x0001 /* overrun error bit */
72#elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
73# define SCSPTR2 0xffe80020 /* 16 bit SCIF */
74# define SCIF_ORER 0x0001 /* overrun error bit */
75#elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
76# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
77#elif defined(CONFIG_H8S2678)
78# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
79#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
80# define SCSPTR0 0xfe4b0020
81# define SCIF_ORER 0x0001
82#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
83# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
84# define SCIF_ORER 0x0001 /* overrun error bit */
85#elif defined(CONFIG_CPU_SUBTYPE_SH7770)
86# define SCSPTR0 0xff923020 /* 16 bit SCIF */
87# define SCIF_ORER 0x0001 /* overrun error bit */
88#elif defined(CONFIG_CPU_SUBTYPE_SH7780)
89# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
90# define SCIF_ORER 0x0001 /* Overrun error bit */
91#elif defined(CONFIG_CPU_SUBTYPE_SH7785) || \
92 defined(CONFIG_CPU_SUBTYPE_SH7786)
93# define SCSPTR0 0xffea0024 /* 16 bit SCIF */
94# define SCIF_ORER 0x0001 /* Overrun error bit */
95#elif defined(CONFIG_CPU_SUBTYPE_SH7201) || \
96 defined(CONFIG_CPU_SUBTYPE_SH7203) || \
97 defined(CONFIG_CPU_SUBTYPE_SH7206) || \
98 defined(CONFIG_CPU_SUBTYPE_SH7263)
99# define SCSPTR0 0xfffe8020 /* 16 bit SCIF */
100#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
101# define SCSPTR0 0xf8400020 /* 16 bit SCIF */
102# define SCIF_ORER 0x0001 /* overrun error bit */
103#elif defined(CONFIG_CPU_SUBTYPE_SHX3)
104# define SCSPTR0 0xffc30020 /* 16 bit SCIF */
105# define SCIF_ORER 0x0001 /* Overrun error bit */
106#else
107# error CPU subtype not defined
108#endif
109
110/* SCxSR SCI */
111#define SCI_TDRE 0x80 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
112#define SCI_RDRF 0x40 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
113#define SCI_ORER 0x20 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
114#define SCI_FER 0x10 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
115#define SCI_PER 0x08 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
116#define SCI_TEND 0x04 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
117/* SCI_MPB 0x02 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
118/* SCI_MPBT 0x01 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
119
120#define SCI_ERRORS ( SCI_PER | SCI_FER | SCI_ORER)
121
122/* SCxSR SCIF */
123#define SCIF_ER 0x0080 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
124#define SCIF_TEND 0x0040 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
125#define SCIF_TDFE 0x0020 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
126#define SCIF_BRK 0x0010 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
127#define SCIF_FER 0x0008 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
128#define SCIF_PER 0x0004 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
129#define SCIF_RDF 0x0002 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
130#define SCIF_DR 0x0001 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
131
132#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
133 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
134 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
135 defined(CONFIG_ARCH_SH73A0) || \
136 defined(CONFIG_ARCH_SH7367) || \
137 defined(CONFIG_ARCH_SH7377) || \
138 defined(CONFIG_ARCH_SH7372)
139# define SCIF_ORER 0x0200
140# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK | SCIF_ORER)
141# define SCIF_RFDC_MASK 0x007f
142# define SCIF_TXROOM_MAX 64
143#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
144# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK )
145# define SCIF_RFDC_MASK 0x007f
146# define SCIF_TXROOM_MAX 64
147/* SH7763 SCIF2 support */
148# define SCIF2_RFDC_MASK 0x001f
149# define SCIF2_TXROOM_MAX 16
150#else
151# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK)
152# define SCIF_RFDC_MASK 0x001f
153# define SCIF_TXROOM_MAX 16
154#endif
155
156#ifndef SCIF_ORER
157#define SCIF_ORER 0x0000
158#endif
159
160#define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND)
161#define SCxSR_ERRORS(port) (((port)->type == PORT_SCI) ? SCI_ERRORS : SCIF_ERRORS)
162#define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_RDF)
163#define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE)
164#define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER)
165#define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER)
166#define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK)
167#define SCxSR_ORER(port) (((port)->type == PORT_SCI) ? SCI_ORER : SCIF_ORER)
168
169#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
170 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
171 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
172 defined(CONFIG_ARCH_SH73A0) || \
173 defined(CONFIG_ARCH_SH7367) || \
174 defined(CONFIG_ARCH_SH7377) || \
175 defined(CONFIG_ARCH_SH7372)
176# define SCxSR_RDxF_CLEAR(port) (sci_in(port, SCxSR) & 0xfffc)
177# define SCxSR_ERROR_CLEAR(port) (sci_in(port, SCxSR) & 0xfd73)
178# define SCxSR_TDxE_CLEAR(port) (sci_in(port, SCxSR) & 0xffdf)
179# define SCxSR_BREAK_CLEAR(port) (sci_in(port, SCxSR) & 0xffe3)
180#else
181# define SCxSR_RDxF_CLEAR(port) (((port)->type == PORT_SCI) ? 0xbc : 0x00fc)
182# define SCxSR_ERROR_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x0073)
183# define SCxSR_TDxE_CLEAR(port) (((port)->type == PORT_SCI) ? 0x78 : 0x00df)
184# define SCxSR_BREAK_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x00e3)
185#endif
186
187/* SCFCR */
188#define SCFCR_RFRST 0x0002
189#define SCFCR_TFRST 0x0004
190#define SCFCR_MCE 0x0008
191
192#define SCI_MAJOR 204
193#define SCI_MINOR_START 8
194
195#define SCI_IN(size, offset) \
196 if ((size) == 8) { \
197 return ioread8(port->membase + (offset)); \
198 } else { \
199 return ioread16(port->membase + (offset)); \
200 }
201#define SCI_OUT(size, offset, value) \
202 if ((size) == 8) { \
203 iowrite8(value, port->membase + (offset)); \
204 } else if ((size) == 16) { \
205 iowrite16(value, port->membase + (offset)); \
206 }
207
208#define CPU_SCIx_FNS(name, sci_offset, sci_size, scif_offset, scif_size)\
209 static inline unsigned int sci_##name##_in(struct uart_port *port) \
210 { \
211 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
212 SCI_IN(scif_size, scif_offset) \
213 } else { /* PORT_SCI or PORT_SCIFA */ \
214 SCI_IN(sci_size, sci_offset); \
215 } \
216 } \
217 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
218 { \
219 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
220 SCI_OUT(scif_size, scif_offset, value) \
221 } else { /* PORT_SCI or PORT_SCIFA */ \
222 SCI_OUT(sci_size, sci_offset, value); \
223 } \
224 }
225
226#ifdef CONFIG_H8300
227/* h8300 don't have SCIF */
228#define CPU_SCIF_FNS(name) \
229 static inline unsigned int sci_##name##_in(struct uart_port *port) \
230 { \
231 return 0; \
232 } \
233 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
234 { \
235 }
236#else
237#define CPU_SCIF_FNS(name, scif_offset, scif_size) \
238 static inline unsigned int sci_##name##_in(struct uart_port *port) \
239 { \
240 SCI_IN(scif_size, scif_offset); \
241 } \
242 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
243 { \
244 SCI_OUT(scif_size, scif_offset, value); \
245 }
246#endif
247
248#define CPU_SCI_FNS(name, sci_offset, sci_size) \
249 static inline unsigned int sci_##name##_in(struct uart_port* port) \
250 { \
251 SCI_IN(sci_size, sci_offset); \
252 } \
253 static inline void sci_##name##_out(struct uart_port* port, unsigned int value) \
254 { \
255 SCI_OUT(sci_size, sci_offset, value); \
256 }
257
258#if defined(CONFIG_CPU_SH3) || \
259 defined(CONFIG_ARCH_SH73A0) || \
260 defined(CONFIG_ARCH_SH7367) || \
261 defined(CONFIG_ARCH_SH7377) || \
262 defined(CONFIG_ARCH_SH7372)
263#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
264#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
265 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
266 h8_sci_offset, h8_sci_size) \
267 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
268#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
269 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
270#elif defined(CONFIG_CPU_SUBTYPE_SH7705) || \
271 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
272 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
273 defined(CONFIG_ARCH_SH7367)
274#define SCIF_FNS(name, scif_offset, scif_size) \
275 CPU_SCIF_FNS(name, scif_offset, scif_size)
276#elif defined(CONFIG_ARCH_SH7377) || \
277 defined(CONFIG_ARCH_SH7372) || \
278 defined(CONFIG_ARCH_SH73A0)
279#define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size) \
280 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size)
281#define SCIF_FNS(name, scif_offset, scif_size) \
282 CPU_SCIF_FNS(name, scif_offset, scif_size)
283#else
284#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
285 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
286 h8_sci_offset, h8_sci_size) \
287 CPU_SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh3_scif_offset, sh3_scif_size)
288#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
289 CPU_SCIF_FNS(name, sh3_scif_offset, sh3_scif_size)
290#endif
291#elif defined(__H8300H__) || defined(__H8300S__)
292#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
293 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
294 h8_sci_offset, h8_sci_size) \
295 CPU_SCI_FNS(name, h8_sci_offset, h8_sci_size)
296#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
297 CPU_SCIF_FNS(name)
298#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
299 defined(CONFIG_CPU_SUBTYPE_SH7724)
300 #define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size) \
301 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size)
302 #define SCIF_FNS(name, sh4_scif_offset, sh4_scif_size) \
303 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
304#else
305#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
306 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
307 h8_sci_offset, h8_sci_size) \
308 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
309#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
310 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
311#endif
312
313#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
314 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
315 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
316 defined(CONFIG_ARCH_SH7367)
317
318SCIF_FNS(SCSMR, 0x00, 16)
319SCIF_FNS(SCBRR, 0x04, 8)
320SCIF_FNS(SCSCR, 0x08, 16)
321SCIF_FNS(SCxSR, 0x14, 16)
322SCIF_FNS(SCFCR, 0x18, 16)
323SCIF_FNS(SCFDR, 0x1c, 16)
324SCIF_FNS(SCxTDR, 0x20, 8)
325SCIF_FNS(SCxRDR, 0x24, 8)
326SCIF_FNS(SCLSR, 0x00, 0)
327#elif defined(CONFIG_ARCH_SH7377) || \
328 defined(CONFIG_ARCH_SH7372) || \
329 defined(CONFIG_ARCH_SH73A0)
330SCIF_FNS(SCSMR, 0x00, 16)
331SCIF_FNS(SCBRR, 0x04, 8)
332SCIF_FNS(SCSCR, 0x08, 16)
333SCIF_FNS(SCTDSR, 0x0c, 16)
334SCIF_FNS(SCFER, 0x10, 16)
335SCIF_FNS(SCxSR, 0x14, 16)
336SCIF_FNS(SCFCR, 0x18, 16)
337SCIF_FNS(SCFDR, 0x1c, 16)
338SCIF_FNS(SCTFDR, 0x38, 16)
339SCIF_FNS(SCRFDR, 0x3c, 16)
340SCIx_FNS(SCxTDR, 0x20, 8, 0x40, 8)
341SCIx_FNS(SCxRDR, 0x24, 8, 0x60, 8)
342SCIF_FNS(SCLSR, 0x00, 0)
343#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
344 defined(CONFIG_CPU_SUBTYPE_SH7724)
345SCIx_FNS(SCSMR, 0x00, 16, 0x00, 16)
346SCIx_FNS(SCBRR, 0x04, 8, 0x04, 8)
347SCIx_FNS(SCSCR, 0x08, 16, 0x08, 16)
348SCIx_FNS(SCxTDR, 0x20, 8, 0x0c, 8)
349SCIx_FNS(SCxSR, 0x14, 16, 0x10, 16)
350SCIx_FNS(SCxRDR, 0x24, 8, 0x14, 8)
351SCIx_FNS(SCSPTR, 0, 0, 0, 0)
352SCIF_FNS(SCFCR, 0x18, 16)
353SCIF_FNS(SCFDR, 0x1c, 16)
354SCIF_FNS(SCLSR, 0x24, 16)
355#else
356/* reg SCI/SH3 SCI/SH4 SCIF/SH3 SCIF/SH4 SCI/H8*/
357/* name off sz off sz off sz off sz off sz*/
358SCIx_FNS(SCSMR, 0x00, 8, 0x00, 8, 0x00, 8, 0x00, 16, 0x00, 8)
359SCIx_FNS(SCBRR, 0x02, 8, 0x04, 8, 0x02, 8, 0x04, 8, 0x01, 8)
360SCIx_FNS(SCSCR, 0x04, 8, 0x08, 8, 0x04, 8, 0x08, 16, 0x02, 8)
361SCIx_FNS(SCxTDR, 0x06, 8, 0x0c, 8, 0x06, 8, 0x0C, 8, 0x03, 8)
362SCIx_FNS(SCxSR, 0x08, 8, 0x10, 8, 0x08, 16, 0x10, 16, 0x04, 8)
363SCIx_FNS(SCxRDR, 0x0a, 8, 0x14, 8, 0x0A, 8, 0x14, 8, 0x05, 8)
364SCIF_FNS(SCFCR, 0x0c, 8, 0x18, 16)
365#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
366 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
367 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
368 defined(CONFIG_CPU_SUBTYPE_SH7786)
369SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
370SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
371SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
372SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
373SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
374#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
375SCIF_FNS(SCFDR, 0, 0, 0x1C, 16)
376SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
377SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
378SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
379SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
380#else
381SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
382#if defined(CONFIG_CPU_SUBTYPE_SH7722)
383SCIF_FNS(SCSPTR, 0, 0, 0, 0)
384#else
385SCIF_FNS(SCSPTR, 0, 0, 0x20, 16)
386#endif
387SCIF_FNS(SCLSR, 0, 0, 0x24, 16)
388#endif
389#endif
390#define sci_in(port, reg) sci_##reg##_in(port)
391#define sci_out(port, reg, value) sci_##reg##_out(port, value)
392
393/* H8/300 series SCI pins assignment */
394#if defined(__H8300H__) || defined(__H8300S__)
395static const struct __attribute__((packed)) {
396 int port; /* GPIO port no */
397 unsigned short rx,tx; /* GPIO bit no */
398} h8300_sci_pins[] = {
399#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
400 { /* SCI0 */
401 .port = H8300_GPIO_P9,
402 .rx = H8300_GPIO_B2,
403 .tx = H8300_GPIO_B0,
404 },
405 { /* SCI1 */
406 .port = H8300_GPIO_P9,
407 .rx = H8300_GPIO_B3,
408 .tx = H8300_GPIO_B1,
409 },
410 { /* SCI2 */
411 .port = H8300_GPIO_PB,
412 .rx = H8300_GPIO_B7,
413 .tx = H8300_GPIO_B6,
414 }
415#elif defined(CONFIG_H8S2678)
416 { /* SCI0 */
417 .port = H8300_GPIO_P3,
418 .rx = H8300_GPIO_B2,
419 .tx = H8300_GPIO_B0,
420 },
421 { /* SCI1 */
422 .port = H8300_GPIO_P3,
423 .rx = H8300_GPIO_B3,
424 .tx = H8300_GPIO_B1,
425 },
426 { /* SCI2 */
427 .port = H8300_GPIO_P5,
428 .rx = H8300_GPIO_B1,
429 .tx = H8300_GPIO_B0,
430 }
431#endif
432};
433#endif
434
435#if defined(CONFIG_CPU_SUBTYPE_SH7706) || \
436 defined(CONFIG_CPU_SUBTYPE_SH7707) || \
437 defined(CONFIG_CPU_SUBTYPE_SH7708) || \
438 defined(CONFIG_CPU_SUBTYPE_SH7709)
439static inline int sci_rxd_in(struct uart_port *port)
440{
441 if (port->mapbase == 0xfffffe80)
442 return __raw_readb(SCPDR)&0x01 ? 1 : 0; /* SCI */
443 return 1;
444}
445#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
446 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
447 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
448 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
449 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
450 defined(CONFIG_CPU_SUBTYPE_SH7091)
451static inline int sci_rxd_in(struct uart_port *port)
452{
453 if (port->mapbase == 0xffe00000)
454 return __raw_readb(SCSPTR1)&0x01 ? 1 : 0; /* SCI */
455 return 1;
456}
457#elif defined(__H8300H__) || defined(__H8300S__)
458static inline int sci_rxd_in(struct uart_port *port)
459{
460 int ch = (port->mapbase - SMR0) >> 3;
461 return (H8300_SCI_DR(ch) & h8300_sci_pins[ch].rx) ? 1 : 0;
462}
463#else /* default case for non-SCI processors */
464static inline int sci_rxd_in(struct uart_port *port)
465{
466 return 1;
467}
468#endif
diff --git a/drivers/tty/serial/sn_console.c b/drivers/tty/serial/sn_console.c
new file mode 100644
index 000000000000..377ae74e7154
--- /dev/null
+++ b/drivers/tty/serial/sn_console.c
@@ -0,0 +1,1085 @@
1/*
2 * C-Brick Serial Port (and console) driver for SGI Altix machines.
3 *
4 * This driver is NOT suitable for talking to the l1-controller for
5 * anything other than 'console activities' --- please use the l1
6 * driver for that.
7 *
8 *
9 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All Rights Reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of version 2 of the GNU General Public License
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it would be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Further, this software is distributed without any warranty that it is
20 * free of the rightful claim of any third person regarding infringement
21 * or the like. Any license provided herein, whether implied or
22 * otherwise, applies only to this software file. Patent licenses, if
23 * any, provided herein do not apply to combinations of this program with
24 * other software, or any other product whatsoever.
25 *
26 * You should have received a copy of the GNU General Public
27 * License along with this program; if not, write the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
29 *
30 * Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
31 * Mountain View, CA 94043, or:
32 *
33 * http://www.sgi.com
34 *
35 * For further information regarding this notice, see:
36 *
37 * http://oss.sgi.com/projects/GenInfo/NoticeExplan
38 */
39
40#include <linux/interrupt.h>
41#include <linux/tty.h>
42#include <linux/serial.h>
43#include <linux/console.h>
44#include <linux/module.h>
45#include <linux/sysrq.h>
46#include <linux/circ_buf.h>
47#include <linux/serial_reg.h>
48#include <linux/delay.h> /* for mdelay */
49#include <linux/miscdevice.h>
50#include <linux/serial_core.h>
51
52#include <asm/io.h>
53#include <asm/sn/simulator.h>
54#include <asm/sn/sn_sal.h>
55
56/* number of characters we can transmit to the SAL console at a time */
57#define SN_SAL_MAX_CHARS 120
58
59/* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
60 * avoid losing chars, (always has to be a power of 2) */
61#define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
62
63#define SN_SAL_UART_FIFO_DEPTH 16
64#define SN_SAL_UART_FIFO_SPEED_CPS (9600/10)
65
66/* sn_transmit_chars() calling args */
67#define TRANSMIT_BUFFERED 0
68#define TRANSMIT_RAW 1
69
70/* To use dynamic numbers only and not use the assigned major and minor,
71 * define the following.. */
72 /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
73#define USE_DYNAMIC_MINOR 0 /* Don't rely on misc_register dynamic minor */
74
75/* Device name we're using */
76#define DEVICE_NAME "ttySG"
77#define DEVICE_NAME_DYNAMIC "ttySG0" /* need full name for misc_register */
78/* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
79#define DEVICE_MAJOR 204
80#define DEVICE_MINOR 40
81
82#ifdef CONFIG_MAGIC_SYSRQ
83static char sysrq_serial_str[] = "\eSYS";
84static char *sysrq_serial_ptr = sysrq_serial_str;
85static unsigned long sysrq_requested;
86#endif /* CONFIG_MAGIC_SYSRQ */
87
88/*
89 * Port definition - this kinda drives it all
90 */
91struct sn_cons_port {
92 struct timer_list sc_timer;
93 struct uart_port sc_port;
94 struct sn_sal_ops {
95 int (*sal_puts_raw) (const char *s, int len);
96 int (*sal_puts) (const char *s, int len);
97 int (*sal_getc) (void);
98 int (*sal_input_pending) (void);
99 void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
100 } *sc_ops;
101 unsigned long sc_interrupt_timeout;
102 int sc_is_asynch;
103};
104
105static struct sn_cons_port sal_console_port;
106static int sn_process_input;
107
108/* Only used if USE_DYNAMIC_MINOR is set to 1 */
109static struct miscdevice misc; /* used with misc_register for dynamic */
110
111extern void early_sn_setup(void);
112
113#undef DEBUG
114#ifdef DEBUG
115static int sn_debug_printf(const char *fmt, ...);
116#define DPRINTF(x...) sn_debug_printf(x)
117#else
118#define DPRINTF(x...) do { } while (0)
119#endif
120
121/* Prototypes */
122static int snt_hw_puts_raw(const char *, int);
123static int snt_hw_puts_buffered(const char *, int);
124static int snt_poll_getc(void);
125static int snt_poll_input_pending(void);
126static int snt_intr_getc(void);
127static int snt_intr_input_pending(void);
128static void sn_transmit_chars(struct sn_cons_port *, int);
129
130/* A table for polling:
131 */
132static struct sn_sal_ops poll_ops = {
133 .sal_puts_raw = snt_hw_puts_raw,
134 .sal_puts = snt_hw_puts_raw,
135 .sal_getc = snt_poll_getc,
136 .sal_input_pending = snt_poll_input_pending
137};
138
139/* A table for interrupts enabled */
140static struct sn_sal_ops intr_ops = {
141 .sal_puts_raw = snt_hw_puts_raw,
142 .sal_puts = snt_hw_puts_buffered,
143 .sal_getc = snt_intr_getc,
144 .sal_input_pending = snt_intr_input_pending,
145 .sal_wakeup_transmit = sn_transmit_chars
146};
147
148/* the console does output in two distinctly different ways:
149 * synchronous (raw) and asynchronous (buffered). initially, early_printk
150 * does synchronous output. any data written goes directly to the SAL
151 * to be output (incidentally, it is internally buffered by the SAL)
152 * after interrupts and timers are initialized and available for use,
153 * the console init code switches to asynchronous output. this is
154 * also the earliest opportunity to begin polling for console input.
155 * after console initialization, console output and tty (serial port)
156 * output is buffered and sent to the SAL asynchronously (either by
157 * timer callback or by UART interrupt) */
158
159/* routines for running the console in polling mode */
160
161/**
162 * snt_poll_getc - Get a character from the console in polling mode
163 *
164 */
165static int snt_poll_getc(void)
166{
167 int ch;
168
169 ia64_sn_console_getc(&ch);
170 return ch;
171}
172
173/**
174 * snt_poll_input_pending - Check if any input is waiting - polling mode.
175 *
176 */
177static int snt_poll_input_pending(void)
178{
179 int status, input;
180
181 status = ia64_sn_console_check(&input);
182 return !status && input;
183}
184
185/* routines for an interrupt driven console (normal) */
186
187/**
188 * snt_intr_getc - Get a character from the console, interrupt mode
189 *
190 */
191static int snt_intr_getc(void)
192{
193 return ia64_sn_console_readc();
194}
195
196/**
197 * snt_intr_input_pending - Check if input is pending, interrupt mode
198 *
199 */
200static int snt_intr_input_pending(void)
201{
202 return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
203}
204
205/* these functions are polled and interrupt */
206
207/**
208 * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
209 * @s: String
210 * @len: Length
211 *
212 */
213static int snt_hw_puts_raw(const char *s, int len)
214{
215 /* this will call the PROM and not return until this is done */
216 return ia64_sn_console_putb(s, len);
217}
218
219/**
220 * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
221 * @s: String
222 * @len: Length
223 *
224 */
225static int snt_hw_puts_buffered(const char *s, int len)
226{
227 /* queue data to the PROM */
228 return ia64_sn_console_xmit_chars((char *)s, len);
229}
230
231/* uart interface structs
232 * These functions are associated with the uart_port that the serial core
233 * infrastructure calls.
234 *
235 * Note: Due to how the console works, many routines are no-ops.
236 */
237
238/**
239 * snp_type - What type of console are we?
240 * @port: Port to operate with (we ignore since we only have one port)
241 *
242 */
243static const char *snp_type(struct uart_port *port)
244{
245 return ("SGI SN L1");
246}
247
248/**
249 * snp_tx_empty - Is the transmitter empty? We pretend we're always empty
250 * @port: Port to operate on (we ignore since we only have one port)
251 *
252 */
253static unsigned int snp_tx_empty(struct uart_port *port)
254{
255 return 1;
256}
257
258/**
259 * snp_stop_tx - stop the transmitter - no-op for us
260 * @port: Port to operat eon - we ignore - no-op function
261 *
262 */
263static void snp_stop_tx(struct uart_port *port)
264{
265}
266
267/**
268 * snp_release_port - Free i/o and resources for port - no-op for us
269 * @port: Port to operate on - we ignore - no-op function
270 *
271 */
272static void snp_release_port(struct uart_port *port)
273{
274}
275
276/**
277 * snp_enable_ms - Force modem status interrupts on - no-op for us
278 * @port: Port to operate on - we ignore - no-op function
279 *
280 */
281static void snp_enable_ms(struct uart_port *port)
282{
283}
284
285/**
286 * snp_shutdown - shut down the port - free irq and disable - no-op for us
287 * @port: Port to shut down - we ignore
288 *
289 */
290static void snp_shutdown(struct uart_port *port)
291{
292}
293
294/**
295 * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
296 * @port: Port to operate on - we ignore
297 * @mctrl: Lines to set/unset - we ignore
298 *
299 */
300static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
301{
302}
303
304/**
305 * snp_get_mctrl - get contorl line info, we just return a static value
306 * @port: port to operate on - we only have one port so we ignore this
307 *
308 */
309static unsigned int snp_get_mctrl(struct uart_port *port)
310{
311 return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
312}
313
314/**
315 * snp_stop_rx - Stop the receiver - we ignor ethis
316 * @port: Port to operate on - we ignore
317 *
318 */
319static void snp_stop_rx(struct uart_port *port)
320{
321}
322
323/**
324 * snp_start_tx - Start transmitter
325 * @port: Port to operate on
326 *
327 */
328static void snp_start_tx(struct uart_port *port)
329{
330 if (sal_console_port.sc_ops->sal_wakeup_transmit)
331 sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
332 TRANSMIT_BUFFERED);
333
334}
335
336/**
337 * snp_break_ctl - handle breaks - ignored by us
338 * @port: Port to operate on
339 * @break_state: Break state
340 *
341 */
342static void snp_break_ctl(struct uart_port *port, int break_state)
343{
344}
345
346/**
347 * snp_startup - Start up the serial port - always return 0 (We're always on)
348 * @port: Port to operate on
349 *
350 */
351static int snp_startup(struct uart_port *port)
352{
353 return 0;
354}
355
356/**
357 * snp_set_termios - set termios stuff - we ignore these
358 * @port: port to operate on
359 * @termios: New settings
360 * @termios: Old
361 *
362 */
363static void
364snp_set_termios(struct uart_port *port, struct ktermios *termios,
365 struct ktermios *old)
366{
367}
368
369/**
370 * snp_request_port - allocate resources for port - ignored by us
371 * @port: port to operate on
372 *
373 */
374static int snp_request_port(struct uart_port *port)
375{
376 return 0;
377}
378
379/**
380 * snp_config_port - allocate resources, set up - we ignore, we're always on
381 * @port: Port to operate on
382 * @flags: flags used for port setup
383 *
384 */
385static void snp_config_port(struct uart_port *port, int flags)
386{
387}
388
389/* Associate the uart functions above - given to serial core */
390
391static struct uart_ops sn_console_ops = {
392 .tx_empty = snp_tx_empty,
393 .set_mctrl = snp_set_mctrl,
394 .get_mctrl = snp_get_mctrl,
395 .stop_tx = snp_stop_tx,
396 .start_tx = snp_start_tx,
397 .stop_rx = snp_stop_rx,
398 .enable_ms = snp_enable_ms,
399 .break_ctl = snp_break_ctl,
400 .startup = snp_startup,
401 .shutdown = snp_shutdown,
402 .set_termios = snp_set_termios,
403 .pm = NULL,
404 .type = snp_type,
405 .release_port = snp_release_port,
406 .request_port = snp_request_port,
407 .config_port = snp_config_port,
408 .verify_port = NULL,
409};
410
411/* End of uart struct functions and defines */
412
413#ifdef DEBUG
414
415/**
416 * sn_debug_printf - close to hardware debugging printf
417 * @fmt: printf format
418 *
419 * This is as "close to the metal" as we can get, used when the driver
420 * itself may be broken.
421 *
422 */
423static int sn_debug_printf(const char *fmt, ...)
424{
425 static char printk_buf[1024];
426 int printed_len;
427 va_list args;
428
429 va_start(args, fmt);
430 printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
431
432 if (!sal_console_port.sc_ops) {
433 sal_console_port.sc_ops = &poll_ops;
434 early_sn_setup();
435 }
436 sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
437
438 va_end(args);
439 return printed_len;
440}
441#endif /* DEBUG */
442
443/*
444 * Interrupt handling routines.
445 */
446
447/**
448 * sn_receive_chars - Grab characters, pass them to tty layer
449 * @port: Port to operate on
450 * @flags: irq flags
451 *
452 * Note: If we're not registered with the serial core infrastructure yet,
453 * we don't try to send characters to it...
454 *
455 */
456static void
457sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
458{
459 int ch;
460 struct tty_struct *tty;
461
462 if (!port) {
463 printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
464 return;
465 }
466
467 if (!port->sc_ops) {
468 printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
469 return;
470 }
471
472 if (port->sc_port.state) {
473 /* The serial_core stuffs are initialized, use them */
474 tty = port->sc_port.state->port.tty;
475 }
476 else {
477 /* Not registered yet - can't pass to tty layer. */
478 tty = NULL;
479 }
480
481 while (port->sc_ops->sal_input_pending()) {
482 ch = port->sc_ops->sal_getc();
483 if (ch < 0) {
484 printk(KERN_ERR "sn_console: An error occurred while "
485 "obtaining data from the console (0x%0x)\n", ch);
486 break;
487 }
488#ifdef CONFIG_MAGIC_SYSRQ
489 if (sysrq_requested) {
490 unsigned long sysrq_timeout = sysrq_requested + HZ*5;
491
492 sysrq_requested = 0;
493 if (ch && time_before(jiffies, sysrq_timeout)) {
494 spin_unlock_irqrestore(&port->sc_port.lock, flags);
495 handle_sysrq(ch);
496 spin_lock_irqsave(&port->sc_port.lock, flags);
497 /* ignore actual sysrq command char */
498 continue;
499 }
500 }
501 if (ch == *sysrq_serial_ptr) {
502 if (!(*++sysrq_serial_ptr)) {
503 sysrq_requested = jiffies;
504 sysrq_serial_ptr = sysrq_serial_str;
505 }
506 /*
507 * ignore the whole sysrq string except for the
508 * leading escape
509 */
510 if (ch != '\e')
511 continue;
512 }
513 else
514 sysrq_serial_ptr = sysrq_serial_str;
515#endif /* CONFIG_MAGIC_SYSRQ */
516
517 /* record the character to pass up to the tty layer */
518 if (tty) {
519 if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
520 break;
521 }
522 port->sc_port.icount.rx++;
523 }
524
525 if (tty)
526 tty_flip_buffer_push(tty);
527}
528
529/**
530 * sn_transmit_chars - grab characters from serial core, send off
531 * @port: Port to operate on
532 * @raw: Transmit raw or buffered
533 *
534 * Note: If we're early, before we're registered with serial core, the
535 * writes are going through sn_sal_console_write because that's how
536 * register_console has been set up. We currently could have asynch
537 * polls calling this function due to sn_sal_switch_to_asynch but we can
538 * ignore them until we register with the serial core stuffs.
539 *
540 */
541static void sn_transmit_chars(struct sn_cons_port *port, int raw)
542{
543 int xmit_count, tail, head, loops, ii;
544 int result;
545 char *start;
546 struct circ_buf *xmit;
547
548 if (!port)
549 return;
550
551 BUG_ON(!port->sc_is_asynch);
552
553 if (port->sc_port.state) {
554 /* We're initialized, using serial core infrastructure */
555 xmit = &port->sc_port.state->xmit;
556 } else {
557 /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
558 * initialized yet. Just return. Writes are going through
559 * sn_sal_console_write (due to register_console) at this time.
560 */
561 return;
562 }
563
564 if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
565 /* Nothing to do. */
566 ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
567 return;
568 }
569
570 head = xmit->head;
571 tail = xmit->tail;
572 start = &xmit->buf[tail];
573
574 /* twice around gets the tail to the end of the buffer and
575 * then to the head, if needed */
576 loops = (head < tail) ? 2 : 1;
577
578 for (ii = 0; ii < loops; ii++) {
579 xmit_count = (head < tail) ?
580 (UART_XMIT_SIZE - tail) : (head - tail);
581
582 if (xmit_count > 0) {
583 if (raw == TRANSMIT_RAW)
584 result =
585 port->sc_ops->sal_puts_raw(start,
586 xmit_count);
587 else
588 result =
589 port->sc_ops->sal_puts(start, xmit_count);
590#ifdef DEBUG
591 if (!result)
592 DPRINTF("`");
593#endif
594 if (result > 0) {
595 xmit_count -= result;
596 port->sc_port.icount.tx += result;
597 tail += result;
598 tail &= UART_XMIT_SIZE - 1;
599 xmit->tail = tail;
600 start = &xmit->buf[tail];
601 }
602 }
603 }
604
605 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
606 uart_write_wakeup(&port->sc_port);
607
608 if (uart_circ_empty(xmit))
609 snp_stop_tx(&port->sc_port); /* no-op for us */
610}
611
612/**
613 * sn_sal_interrupt - Handle console interrupts
614 * @irq: irq #, useful for debug statements
615 * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
616 *
617 */
618static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
619{
620 struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
621 unsigned long flags;
622 int status = ia64_sn_console_intr_status();
623
624 if (!port)
625 return IRQ_NONE;
626
627 spin_lock_irqsave(&port->sc_port.lock, flags);
628 if (status & SAL_CONSOLE_INTR_RECV) {
629 sn_receive_chars(port, flags);
630 }
631 if (status & SAL_CONSOLE_INTR_XMIT) {
632 sn_transmit_chars(port, TRANSMIT_BUFFERED);
633 }
634 spin_unlock_irqrestore(&port->sc_port.lock, flags);
635 return IRQ_HANDLED;
636}
637
638/**
639 * sn_sal_timer_poll - this function handles polled console mode
640 * @data: A pointer to our sn_cons_port (which contains the uart port)
641 *
642 * data is the pointer that init_timer will store for us. This function is
643 * associated with init_timer to see if there is any console traffic.
644 * Obviously not used in interrupt mode
645 *
646 */
647static void sn_sal_timer_poll(unsigned long data)
648{
649 struct sn_cons_port *port = (struct sn_cons_port *)data;
650 unsigned long flags;
651
652 if (!port)
653 return;
654
655 if (!port->sc_port.irq) {
656 spin_lock_irqsave(&port->sc_port.lock, flags);
657 if (sn_process_input)
658 sn_receive_chars(port, flags);
659 sn_transmit_chars(port, TRANSMIT_RAW);
660 spin_unlock_irqrestore(&port->sc_port.lock, flags);
661 mod_timer(&port->sc_timer,
662 jiffies + port->sc_interrupt_timeout);
663 }
664}
665
666/*
667 * Boot-time initialization code
668 */
669
670/**
671 * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
672 * @port: Our sn_cons_port (which contains the uart port)
673 *
674 * So this is used by sn_sal_serial_console_init (early on, before we're
675 * registered with serial core). It's also used by sn_sal_module_init
676 * right after we've registered with serial core. The later only happens
677 * if we didn't already come through here via sn_sal_serial_console_init.
678 *
679 */
680static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
681{
682 unsigned long flags;
683
684 if (!port)
685 return;
686
687 DPRINTF("sn_console: about to switch to asynchronous console\n");
688
689 /* without early_printk, we may be invoked late enough to race
690 * with other cpus doing console IO at this point, however
691 * console interrupts will never be enabled */
692 spin_lock_irqsave(&port->sc_port.lock, flags);
693
694 /* early_printk invocation may have done this for us */
695 if (!port->sc_ops)
696 port->sc_ops = &poll_ops;
697
698 /* we can't turn on the console interrupt (as request_irq
699 * calls kmalloc, which isn't set up yet), so we rely on a
700 * timer to poll for input and push data from the console
701 * buffer.
702 */
703 init_timer(&port->sc_timer);
704 port->sc_timer.function = sn_sal_timer_poll;
705 port->sc_timer.data = (unsigned long)port;
706
707 if (IS_RUNNING_ON_SIMULATOR())
708 port->sc_interrupt_timeout = 6;
709 else {
710 /* 960cps / 16 char FIFO = 60HZ
711 * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
712 port->sc_interrupt_timeout =
713 HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
714 }
715 mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
716
717 port->sc_is_asynch = 1;
718 spin_unlock_irqrestore(&port->sc_port.lock, flags);
719}
720
721/**
722 * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
723 * @port: Our sn_cons_port (which contains the uart port)
724 *
725 * In sn_sal_module_init, after we're registered with serial core and
726 * the port is added, this function is called to switch us to interrupt
727 * mode. We were previously in asynch/polling mode (using init_timer).
728 *
729 * We attempt to switch to interrupt mode here by calling
730 * request_irq. If that works out, we enable receive interrupts.
731 */
732static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
733{
734 unsigned long flags;
735
736 if (port) {
737 DPRINTF("sn_console: switching to interrupt driven console\n");
738
739 if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
740 IRQF_DISABLED | IRQF_SHARED,
741 "SAL console driver", port) >= 0) {
742 spin_lock_irqsave(&port->sc_port.lock, flags);
743 port->sc_port.irq = SGI_UART_VECTOR;
744 port->sc_ops = &intr_ops;
745
746 /* turn on receive interrupts */
747 ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
748 spin_unlock_irqrestore(&port->sc_port.lock, flags);
749 }
750 else {
751 printk(KERN_INFO
752 "sn_console: console proceeding in polled mode\n");
753 }
754 }
755}
756
757/*
758 * Kernel console definitions
759 */
760
761static void sn_sal_console_write(struct console *, const char *, unsigned);
762static int sn_sal_console_setup(struct console *, char *);
763static struct uart_driver sal_console_uart;
764extern struct tty_driver *uart_console_device(struct console *, int *);
765
766static struct console sal_console = {
767 .name = DEVICE_NAME,
768 .write = sn_sal_console_write,
769 .device = uart_console_device,
770 .setup = sn_sal_console_setup,
771 .index = -1, /* unspecified */
772 .data = &sal_console_uart,
773};
774
775#define SAL_CONSOLE &sal_console
776
777static struct uart_driver sal_console_uart = {
778 .owner = THIS_MODULE,
779 .driver_name = "sn_console",
780 .dev_name = DEVICE_NAME,
781 .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
782 .minor = 0,
783 .nr = 1, /* one port */
784 .cons = SAL_CONSOLE,
785};
786
787/**
788 * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
789 *
790 * Before this is called, we've been printing kernel messages in a special
791 * early mode not making use of the serial core infrastructure. When our
792 * driver is loaded for real, we register the driver and port with serial
793 * core and try to enable interrupt driven mode.
794 *
795 */
796static int __init sn_sal_module_init(void)
797{
798 int retval;
799
800 if (!ia64_platform_is("sn2"))
801 return 0;
802
803 printk(KERN_INFO "sn_console: Console driver init\n");
804
805 if (USE_DYNAMIC_MINOR == 1) {
806 misc.minor = MISC_DYNAMIC_MINOR;
807 misc.name = DEVICE_NAME_DYNAMIC;
808 retval = misc_register(&misc);
809 if (retval != 0) {
810 printk(KERN_WARNING "Failed to register console "
811 "device using misc_register.\n");
812 return -ENODEV;
813 }
814 sal_console_uart.major = MISC_MAJOR;
815 sal_console_uart.minor = misc.minor;
816 } else {
817 sal_console_uart.major = DEVICE_MAJOR;
818 sal_console_uart.minor = DEVICE_MINOR;
819 }
820
821 /* We register the driver and the port before switching to interrupts
822 * or async above so the proper uart structures are populated */
823
824 if (uart_register_driver(&sal_console_uart) < 0) {
825 printk
826 ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
827 __LINE__);
828 return -ENODEV;
829 }
830
831 spin_lock_init(&sal_console_port.sc_port.lock);
832
833 /* Setup the port struct with the minimum needed */
834 sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
835 sal_console_port.sc_port.type = PORT_16550A;
836 sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
837 sal_console_port.sc_port.ops = &sn_console_ops;
838 sal_console_port.sc_port.line = 0;
839
840 if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
841 /* error - not sure what I'd do - so I'll do nothing */
842 printk(KERN_ERR "%s: unable to add port\n", __func__);
843 }
844
845 /* when this driver is compiled in, the console initialization
846 * will have already switched us into asynchronous operation
847 * before we get here through the module initcalls */
848 if (!sal_console_port.sc_is_asynch) {
849 sn_sal_switch_to_asynch(&sal_console_port);
850 }
851
852 /* at this point (module_init) we can try to turn on interrupts */
853 if (!IS_RUNNING_ON_SIMULATOR()) {
854 sn_sal_switch_to_interrupts(&sal_console_port);
855 }
856 sn_process_input = 1;
857 return 0;
858}
859
860/**
861 * sn_sal_module_exit - When we're unloaded, remove the driver/port
862 *
863 */
864static void __exit sn_sal_module_exit(void)
865{
866 del_timer_sync(&sal_console_port.sc_timer);
867 uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
868 uart_unregister_driver(&sal_console_uart);
869 misc_deregister(&misc);
870}
871
872module_init(sn_sal_module_init);
873module_exit(sn_sal_module_exit);
874
875/**
876 * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
877 * @puts_raw : puts function to do the writing
878 * @s: input string
879 * @count: length
880 *
881 * We need a \r ahead of every \n for direct writes through
882 * ia64_sn_console_putb (what sal_puts_raw below actually does).
883 *
884 */
885
886static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
887 const char *s, int count)
888{
889 const char *s1;
890
891 /* Output '\r' before each '\n' */
892 while ((s1 = memchr(s, '\n', count)) != NULL) {
893 puts_raw(s, s1 - s);
894 puts_raw("\r\n", 2);
895 count -= s1 + 1 - s;
896 s = s1 + 1;
897 }
898 puts_raw(s, count);
899}
900
901/**
902 * sn_sal_console_write - Print statements before serial core available
903 * @console: Console to operate on - we ignore since we have just one
904 * @s: String to send
905 * @count: length
906 *
907 * This is referenced in the console struct. It is used for early
908 * console printing before we register with serial core and for things
909 * such as kdb. The console_lock must be held when we get here.
910 *
911 * This function has some code for trying to print output even if the lock
912 * is held. We try to cover the case where a lock holder could have died.
913 * We don't use this special case code if we're not registered with serial
914 * core yet. After we're registered with serial core, the only time this
915 * function would be used is for high level kernel output like magic sys req,
916 * kdb, and printk's.
917 */
918static void
919sn_sal_console_write(struct console *co, const char *s, unsigned count)
920{
921 unsigned long flags = 0;
922 struct sn_cons_port *port = &sal_console_port;
923 static int stole_lock = 0;
924
925 BUG_ON(!port->sc_is_asynch);
926
927 /* We can't look at the xmit buffer if we're not registered with serial core
928 * yet. So only do the fancy recovery after registering
929 */
930 if (!port->sc_port.state) {
931 /* Not yet registered with serial core - simple case */
932 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
933 return;
934 }
935
936 /* somebody really wants this output, might be an
937 * oops, kdb, panic, etc. make sure they get it. */
938 if (spin_is_locked(&port->sc_port.lock)) {
939 int lhead = port->sc_port.state->xmit.head;
940 int ltail = port->sc_port.state->xmit.tail;
941 int counter, got_lock = 0;
942
943 /*
944 * We attempt to determine if someone has died with the
945 * lock. We wait ~20 secs after the head and tail ptrs
946 * stop moving and assume the lock holder is not functional
947 * and plow ahead. If the lock is freed within the time out
948 * period we re-get the lock and go ahead normally. We also
949 * remember if we have plowed ahead so that we don't have
950 * to wait out the time out period again - the asumption
951 * is that we will time out again.
952 */
953
954 for (counter = 0; counter < 150; mdelay(125), counter++) {
955 if (!spin_is_locked(&port->sc_port.lock)
956 || stole_lock) {
957 if (!stole_lock) {
958 spin_lock_irqsave(&port->sc_port.lock,
959 flags);
960 got_lock = 1;
961 }
962 break;
963 } else {
964 /* still locked */
965 if ((lhead != port->sc_port.state->xmit.head)
966 || (ltail !=
967 port->sc_port.state->xmit.tail)) {
968 lhead =
969 port->sc_port.state->xmit.head;
970 ltail =
971 port->sc_port.state->xmit.tail;
972 counter = 0;
973 }
974 }
975 }
976 /* flush anything in the serial core xmit buffer, raw */
977 sn_transmit_chars(port, 1);
978 if (got_lock) {
979 spin_unlock_irqrestore(&port->sc_port.lock, flags);
980 stole_lock = 0;
981 } else {
982 /* fell thru */
983 stole_lock = 1;
984 }
985 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
986 } else {
987 stole_lock = 0;
988 spin_lock_irqsave(&port->sc_port.lock, flags);
989 sn_transmit_chars(port, 1);
990 spin_unlock_irqrestore(&port->sc_port.lock, flags);
991
992 puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
993 }
994}
995
996
997/**
998 * sn_sal_console_setup - Set up console for early printing
999 * @co: Console to work with
1000 * @options: Options to set
1001 *
1002 * Altix console doesn't do anything with baud rates, etc, anyway.
1003 *
1004 * This isn't required since not providing the setup function in the
1005 * console struct is ok. However, other patches like KDB plop something
1006 * here so providing it is easier.
1007 *
1008 */
1009static int sn_sal_console_setup(struct console *co, char *options)
1010{
1011 return 0;
1012}
1013
1014/**
1015 * sn_sal_console_write_early - simple early output routine
1016 * @co - console struct
1017 * @s - string to print
1018 * @count - count
1019 *
1020 * Simple function to provide early output, before even
1021 * sn_sal_serial_console_init is called. Referenced in the
1022 * console struct registerd in sn_serial_console_early_setup.
1023 *
1024 */
1025static void __init
1026sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
1027{
1028 puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
1029}
1030
1031/* Used for very early console printing - again, before
1032 * sn_sal_serial_console_init is run */
1033static struct console sal_console_early __initdata = {
1034 .name = "sn_sal",
1035 .write = sn_sal_console_write_early,
1036 .flags = CON_PRINTBUFFER,
1037 .index = -1,
1038};
1039
1040/**
1041 * sn_serial_console_early_setup - Sets up early console output support
1042 *
1043 * Register a console early on... This is for output before even
1044 * sn_sal_serial_cosnole_init is called. This function is called from
1045 * setup.c. This allows us to do really early polled writes. When
1046 * sn_sal_serial_console_init is called, this console is unregistered
1047 * and a new one registered.
1048 */
1049int __init sn_serial_console_early_setup(void)
1050{
1051 if (!ia64_platform_is("sn2"))
1052 return -1;
1053
1054 sal_console_port.sc_ops = &poll_ops;
1055 spin_lock_init(&sal_console_port.sc_port.lock);
1056 early_sn_setup(); /* Find SAL entry points */
1057 register_console(&sal_console_early);
1058
1059 return 0;
1060}
1061
1062/**
1063 * sn_sal_serial_console_init - Early console output - set up for register
1064 *
1065 * This function is called when regular console init happens. Because we
1066 * support even earlier console output with sn_serial_console_early_setup
1067 * (called from setup.c directly), this function unregisters the really
1068 * early console.
1069 *
1070 * Note: Even if setup.c doesn't register sal_console_early, unregistering
1071 * it here doesn't hurt anything.
1072 *
1073 */
1074static int __init sn_sal_serial_console_init(void)
1075{
1076 if (ia64_platform_is("sn2")) {
1077 sn_sal_switch_to_asynch(&sal_console_port);
1078 DPRINTF("sn_sal_serial_console_init : register console\n");
1079 register_console(&sal_console);
1080 unregister_console(&sal_console_early);
1081 }
1082 return 0;
1083}
1084
1085console_initcall(sn_sal_serial_console_init);
diff --git a/drivers/tty/serial/suncore.c b/drivers/tty/serial/suncore.c
new file mode 100644
index 000000000000..6381a0282ee7
--- /dev/null
+++ b/drivers/tty/serial/suncore.c
@@ -0,0 +1,247 @@
1/* suncore.c
2 *
3 * Common SUN serial routines. Based entirely
4 * upon drivers/sbus/char/sunserial.c which is:
5 *
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 *
8 * Adaptation to new UART layer is:
9 *
10 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/console.h>
16#include <linux/tty.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/serial_core.h>
20#include <linux/init.h>
21
22#include <asm/prom.h>
23
24#include "suncore.h"
25
26static int sunserial_current_minor = 64;
27
28int sunserial_register_minors(struct uart_driver *drv, int count)
29{
30 int err = 0;
31
32 drv->minor = sunserial_current_minor;
33 drv->nr += count;
34 /* Register the driver on the first call */
35 if (drv->nr == count)
36 err = uart_register_driver(drv);
37 if (err == 0) {
38 sunserial_current_minor += count;
39 drv->tty_driver->name_base = drv->minor - 64;
40 }
41 return err;
42}
43EXPORT_SYMBOL(sunserial_register_minors);
44
45void sunserial_unregister_minors(struct uart_driver *drv, int count)
46{
47 drv->nr -= count;
48 sunserial_current_minor -= count;
49
50 if (drv->nr == 0)
51 uart_unregister_driver(drv);
52}
53EXPORT_SYMBOL(sunserial_unregister_minors);
54
55int sunserial_console_match(struct console *con, struct device_node *dp,
56 struct uart_driver *drv, int line, bool ignore_line)
57{
58 if (!con)
59 return 0;
60
61 drv->cons = con;
62
63 if (of_console_device != dp)
64 return 0;
65
66 if (!ignore_line) {
67 int off = 0;
68
69 if (of_console_options &&
70 *of_console_options == 'b')
71 off = 1;
72
73 if ((line & 1) != off)
74 return 0;
75 }
76
77 if (!console_set_on_cmdline) {
78 con->index = line;
79 add_preferred_console(con->name, line, NULL);
80 }
81 return 1;
82}
83EXPORT_SYMBOL(sunserial_console_match);
84
85void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
86{
87 const char *mode, *s;
88 char mode_prop[] = "ttyX-mode";
89 int baud, bits, stop, cflag;
90 char parity;
91
92 if (!strcmp(uart_dp->name, "rsc") ||
93 !strcmp(uart_dp->name, "rsc-console") ||
94 !strcmp(uart_dp->name, "rsc-control")) {
95 mode = of_get_property(uart_dp,
96 "ssp-console-modes", NULL);
97 if (!mode)
98 mode = "115200,8,n,1,-";
99 } else if (!strcmp(uart_dp->name, "lom-console")) {
100 mode = "9600,8,n,1,-";
101 } else {
102 struct device_node *dp;
103 char c;
104
105 c = 'a';
106 if (of_console_options)
107 c = *of_console_options;
108
109 mode_prop[3] = c;
110
111 dp = of_find_node_by_path("/options");
112 mode = of_get_property(dp, mode_prop, NULL);
113 if (!mode)
114 mode = "9600,8,n,1,-";
115 }
116
117 cflag = CREAD | HUPCL | CLOCAL;
118
119 s = mode;
120 baud = simple_strtoul(s, NULL, 0);
121 s = strchr(s, ',');
122 bits = simple_strtoul(++s, NULL, 0);
123 s = strchr(s, ',');
124 parity = *(++s);
125 s = strchr(s, ',');
126 stop = simple_strtoul(++s, NULL, 0);
127 s = strchr(s, ',');
128 /* XXX handshake is not handled here. */
129
130 switch (baud) {
131 case 150: cflag |= B150; break;
132 case 300: cflag |= B300; break;
133 case 600: cflag |= B600; break;
134 case 1200: cflag |= B1200; break;
135 case 2400: cflag |= B2400; break;
136 case 4800: cflag |= B4800; break;
137 case 9600: cflag |= B9600; break;
138 case 19200: cflag |= B19200; break;
139 case 38400: cflag |= B38400; break;
140 case 57600: cflag |= B57600; break;
141 case 115200: cflag |= B115200; break;
142 case 230400: cflag |= B230400; break;
143 case 460800: cflag |= B460800; break;
144 default: baud = 9600; cflag |= B9600; break;
145 }
146
147 switch (bits) {
148 case 5: cflag |= CS5; break;
149 case 6: cflag |= CS6; break;
150 case 7: cflag |= CS7; break;
151 case 8: cflag |= CS8; break;
152 default: cflag |= CS8; break;
153 }
154
155 switch (parity) {
156 case 'o': cflag |= (PARENB | PARODD); break;
157 case 'e': cflag |= PARENB; break;
158 case 'n': default: break;
159 }
160
161 switch (stop) {
162 case 2: cflag |= CSTOPB; break;
163 case 1: default: break;
164 }
165
166 con->cflag = cflag;
167}
168
169/* Sun serial MOUSE auto baud rate detection. */
170static struct mouse_baud_cflag {
171 int baud;
172 unsigned int cflag;
173} mouse_baud_table[] = {
174 { 1200, B1200 },
175 { 2400, B2400 },
176 { 4800, B4800 },
177 { 9600, B9600 },
178 { -1, ~0 },
179 { -1, ~0 },
180};
181
182unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
183{
184 int i;
185
186 for (i = 0; mouse_baud_table[i].baud != -1; i++)
187 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
188 break;
189
190 i += 1;
191 if (mouse_baud_table[i].baud == -1)
192 i = 0;
193
194 *new_baud = mouse_baud_table[i].baud;
195 return mouse_baud_table[i].cflag;
196}
197
198EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
199
200/* Basically, when the baud rate is wrong the mouse spits out
201 * breaks to us.
202 */
203int suncore_mouse_baud_detection(unsigned char ch, int is_break)
204{
205 static int mouse_got_break = 0;
206 static int ctr = 0;
207
208 if (is_break) {
209 /* Let a few normal bytes go by before we jump the gun
210 * and say we need to try another baud rate.
211 */
212 if (mouse_got_break && ctr < 8)
213 return 1;
214
215 /* Ok, we need to try another baud. */
216 ctr = 0;
217 mouse_got_break = 1;
218 return 2;
219 }
220 if (mouse_got_break) {
221 ctr++;
222 if (ch == 0x87) {
223 /* Correct baud rate determined. */
224 mouse_got_break = 0;
225 }
226 return 1;
227 }
228 return 0;
229}
230
231EXPORT_SYMBOL(suncore_mouse_baud_detection);
232
233static int __init suncore_init(void)
234{
235 return 0;
236}
237
238static void __exit suncore_exit(void)
239{
240}
241
242module_init(suncore_init);
243module_exit(suncore_exit);
244
245MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
246MODULE_DESCRIPTION("Sun serial common layer");
247MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/suncore.h b/drivers/tty/serial/suncore.h
new file mode 100644
index 000000000000..db2057936c31
--- /dev/null
+++ b/drivers/tty/serial/suncore.h
@@ -0,0 +1,33 @@
1/* suncore.h
2 *
3 * Generic SUN serial/kbd/ms layer. Based entirely
4 * upon drivers/sbus/char/sunserial.h which is:
5 *
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 *
8 * Port to new UART layer is:
9 *
10 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11 */
12
13#ifndef _SERIAL_SUN_H
14#define _SERIAL_SUN_H
15
16/* Serial keyboard defines for L1-A processing... */
17#define SUNKBD_RESET 0xff
18#define SUNKBD_L1 0x01
19#define SUNKBD_UP 0x80
20#define SUNKBD_A 0x4d
21
22extern unsigned int suncore_mouse_baud_cflag_next(unsigned int, int *);
23extern int suncore_mouse_baud_detection(unsigned char, int);
24
25extern int sunserial_register_minors(struct uart_driver *, int);
26extern void sunserial_unregister_minors(struct uart_driver *, int);
27
28extern int sunserial_console_match(struct console *, struct device_node *,
29 struct uart_driver *, int, bool);
30extern void sunserial_console_termios(struct console *,
31 struct device_node *);
32
33#endif /* !(_SERIAL_SUN_H) */
diff --git a/drivers/tty/serial/sunhv.c b/drivers/tty/serial/sunhv.c
new file mode 100644
index 000000000000..c0b7246d7339
--- /dev/null
+++ b/drivers/tty/serial/sunhv.c
@@ -0,0 +1,661 @@
1/* sunhv.c: Serial driver for SUN4V hypervisor console.
2 *
3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/tty.h>
10#include <linux/tty_flip.h>
11#include <linux/major.h>
12#include <linux/circ_buf.h>
13#include <linux/serial.h>
14#include <linux/sysrq.h>
15#include <linux/console.h>
16#include <linux/spinlock.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/init.h>
20#include <linux/of_device.h>
21
22#include <asm/hypervisor.h>
23#include <asm/spitfire.h>
24#include <asm/prom.h>
25#include <asm/irq.h>
26
27#if defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/serial_core.h>
32
33#include "suncore.h"
34
35#define CON_BREAK ((long)-1)
36#define CON_HUP ((long)-2)
37
38#define IGNORE_BREAK 0x1
39#define IGNORE_ALL 0x2
40
41static char *con_write_page;
42static char *con_read_page;
43
44static int hung_up = 0;
45
46static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
47{
48 while (!uart_circ_empty(xmit)) {
49 long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
50
51 if (status != HV_EOK)
52 break;
53
54 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
55 port->icount.tx++;
56 }
57}
58
59static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
60{
61 while (!uart_circ_empty(xmit)) {
62 unsigned long ra = __pa(xmit->buf + xmit->tail);
63 unsigned long len, status, sent;
64
65 len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
66 UART_XMIT_SIZE);
67 status = sun4v_con_write(ra, len, &sent);
68 if (status != HV_EOK)
69 break;
70 xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
71 port->icount.tx += sent;
72 }
73}
74
75static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
76{
77 int saw_console_brk = 0;
78 int limit = 10000;
79
80 while (limit-- > 0) {
81 long status;
82 long c = sun4v_con_getchar(&status);
83
84 if (status == HV_EWOULDBLOCK)
85 break;
86
87 if (c == CON_BREAK) {
88 if (uart_handle_break(port))
89 continue;
90 saw_console_brk = 1;
91 c = 0;
92 }
93
94 if (c == CON_HUP) {
95 hung_up = 1;
96 uart_handle_dcd_change(port, 0);
97 } else if (hung_up) {
98 hung_up = 0;
99 uart_handle_dcd_change(port, 1);
100 }
101
102 if (tty == NULL) {
103 uart_handle_sysrq_char(port, c);
104 continue;
105 }
106
107 port->icount.rx++;
108
109 if (uart_handle_sysrq_char(port, c))
110 continue;
111
112 tty_insert_flip_char(tty, c, TTY_NORMAL);
113 }
114
115 return saw_console_brk;
116}
117
118static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
119{
120 int saw_console_brk = 0;
121 int limit = 10000;
122
123 while (limit-- > 0) {
124 unsigned long ra = __pa(con_read_page);
125 unsigned long bytes_read, i;
126 long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);
127
128 if (stat != HV_EOK) {
129 bytes_read = 0;
130
131 if (stat == CON_BREAK) {
132 if (uart_handle_break(port))
133 continue;
134 saw_console_brk = 1;
135 *con_read_page = 0;
136 bytes_read = 1;
137 } else if (stat == CON_HUP) {
138 hung_up = 1;
139 uart_handle_dcd_change(port, 0);
140 continue;
141 } else {
142 /* HV_EWOULDBLOCK, etc. */
143 break;
144 }
145 }
146
147 if (hung_up) {
148 hung_up = 0;
149 uart_handle_dcd_change(port, 1);
150 }
151
152 for (i = 0; i < bytes_read; i++)
153 uart_handle_sysrq_char(port, con_read_page[i]);
154
155 if (tty == NULL)
156 continue;
157
158 port->icount.rx += bytes_read;
159
160 tty_insert_flip_string(tty, con_read_page, bytes_read);
161 }
162
163 return saw_console_brk;
164}
165
166struct sunhv_ops {
167 void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
168 int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
169};
170
171static struct sunhv_ops bychar_ops = {
172 .transmit_chars = transmit_chars_putchar,
173 .receive_chars = receive_chars_getchar,
174};
175
176static struct sunhv_ops bywrite_ops = {
177 .transmit_chars = transmit_chars_write,
178 .receive_chars = receive_chars_read,
179};
180
181static struct sunhv_ops *sunhv_ops = &bychar_ops;
182
183static struct tty_struct *receive_chars(struct uart_port *port)
184{
185 struct tty_struct *tty = NULL;
186
187 if (port->state != NULL) /* Unopened serial console */
188 tty = port->state->port.tty;
189
190 if (sunhv_ops->receive_chars(port, tty))
191 sun_do_break();
192
193 return tty;
194}
195
196static void transmit_chars(struct uart_port *port)
197{
198 struct circ_buf *xmit;
199
200 if (!port->state)
201 return;
202
203 xmit = &port->state->xmit;
204 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
205 return;
206
207 sunhv_ops->transmit_chars(port, xmit);
208
209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
210 uart_write_wakeup(port);
211}
212
213static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
214{
215 struct uart_port *port = dev_id;
216 struct tty_struct *tty;
217 unsigned long flags;
218
219 spin_lock_irqsave(&port->lock, flags);
220 tty = receive_chars(port);
221 transmit_chars(port);
222 spin_unlock_irqrestore(&port->lock, flags);
223
224 if (tty)
225 tty_flip_buffer_push(tty);
226
227 return IRQ_HANDLED;
228}
229
230/* port->lock is not held. */
231static unsigned int sunhv_tx_empty(struct uart_port *port)
232{
233 /* Transmitter is always empty for us. If the circ buffer
234 * is non-empty or there is an x_char pending, our caller
235 * will do the right thing and ignore what we return here.
236 */
237 return TIOCSER_TEMT;
238}
239
240/* port->lock held by caller. */
241static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
242{
243 return;
244}
245
246/* port->lock is held by caller and interrupts are disabled. */
247static unsigned int sunhv_get_mctrl(struct uart_port *port)
248{
249 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
250}
251
252/* port->lock held by caller. */
253static void sunhv_stop_tx(struct uart_port *port)
254{
255 return;
256}
257
258/* port->lock held by caller. */
259static void sunhv_start_tx(struct uart_port *port)
260{
261 transmit_chars(port);
262}
263
264/* port->lock is not held. */
265static void sunhv_send_xchar(struct uart_port *port, char ch)
266{
267 unsigned long flags;
268 int limit = 10000;
269
270 spin_lock_irqsave(&port->lock, flags);
271
272 while (limit-- > 0) {
273 long status = sun4v_con_putchar(ch);
274 if (status == HV_EOK)
275 break;
276 udelay(1);
277 }
278
279 spin_unlock_irqrestore(&port->lock, flags);
280}
281
282/* port->lock held by caller. */
283static void sunhv_stop_rx(struct uart_port *port)
284{
285}
286
287/* port->lock held by caller. */
288static void sunhv_enable_ms(struct uart_port *port)
289{
290}
291
292/* port->lock is not held. */
293static void sunhv_break_ctl(struct uart_port *port, int break_state)
294{
295 if (break_state) {
296 unsigned long flags;
297 int limit = 10000;
298
299 spin_lock_irqsave(&port->lock, flags);
300
301 while (limit-- > 0) {
302 long status = sun4v_con_putchar(CON_BREAK);
303 if (status == HV_EOK)
304 break;
305 udelay(1);
306 }
307
308 spin_unlock_irqrestore(&port->lock, flags);
309 }
310}
311
312/* port->lock is not held. */
313static int sunhv_startup(struct uart_port *port)
314{
315 return 0;
316}
317
318/* port->lock is not held. */
319static void sunhv_shutdown(struct uart_port *port)
320{
321}
322
323/* port->lock is not held. */
324static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
325 struct ktermios *old)
326{
327 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
328 unsigned int quot = uart_get_divisor(port, baud);
329 unsigned int iflag, cflag;
330 unsigned long flags;
331
332 spin_lock_irqsave(&port->lock, flags);
333
334 iflag = termios->c_iflag;
335 cflag = termios->c_cflag;
336
337 port->ignore_status_mask = 0;
338 if (iflag & IGNBRK)
339 port->ignore_status_mask |= IGNORE_BREAK;
340 if ((cflag & CREAD) == 0)
341 port->ignore_status_mask |= IGNORE_ALL;
342
343 /* XXX */
344 uart_update_timeout(port, cflag,
345 (port->uartclk / (16 * quot)));
346
347 spin_unlock_irqrestore(&port->lock, flags);
348}
349
350static const char *sunhv_type(struct uart_port *port)
351{
352 return "SUN4V HCONS";
353}
354
355static void sunhv_release_port(struct uart_port *port)
356{
357}
358
359static int sunhv_request_port(struct uart_port *port)
360{
361 return 0;
362}
363
364static void sunhv_config_port(struct uart_port *port, int flags)
365{
366}
367
368static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
369{
370 return -EINVAL;
371}
372
373static struct uart_ops sunhv_pops = {
374 .tx_empty = sunhv_tx_empty,
375 .set_mctrl = sunhv_set_mctrl,
376 .get_mctrl = sunhv_get_mctrl,
377 .stop_tx = sunhv_stop_tx,
378 .start_tx = sunhv_start_tx,
379 .send_xchar = sunhv_send_xchar,
380 .stop_rx = sunhv_stop_rx,
381 .enable_ms = sunhv_enable_ms,
382 .break_ctl = sunhv_break_ctl,
383 .startup = sunhv_startup,
384 .shutdown = sunhv_shutdown,
385 .set_termios = sunhv_set_termios,
386 .type = sunhv_type,
387 .release_port = sunhv_release_port,
388 .request_port = sunhv_request_port,
389 .config_port = sunhv_config_port,
390 .verify_port = sunhv_verify_port,
391};
392
393static struct uart_driver sunhv_reg = {
394 .owner = THIS_MODULE,
395 .driver_name = "sunhv",
396 .dev_name = "ttyS",
397 .major = TTY_MAJOR,
398};
399
400static struct uart_port *sunhv_port;
401
402/* Copy 's' into the con_write_page, decoding "\n" into
403 * "\r\n" along the way. We have to return two lengths
404 * because the caller needs to know how much to advance
405 * 's' and also how many bytes to output via con_write_page.
406 */
407static int fill_con_write_page(const char *s, unsigned int n,
408 unsigned long *page_bytes)
409{
410 const char *orig_s = s;
411 char *p = con_write_page;
412 int left = PAGE_SIZE;
413
414 while (n--) {
415 if (*s == '\n') {
416 if (left < 2)
417 break;
418 *p++ = '\r';
419 left--;
420 } else if (left < 1)
421 break;
422 *p++ = *s++;
423 left--;
424 }
425 *page_bytes = p - con_write_page;
426 return s - orig_s;
427}
428
429static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
430{
431 struct uart_port *port = sunhv_port;
432 unsigned long flags;
433 int locked = 1;
434
435 local_irq_save(flags);
436 if (port->sysrq) {
437 locked = 0;
438 } else if (oops_in_progress) {
439 locked = spin_trylock(&port->lock);
440 } else
441 spin_lock(&port->lock);
442
443 while (n > 0) {
444 unsigned long ra = __pa(con_write_page);
445 unsigned long page_bytes;
446 unsigned int cpy = fill_con_write_page(s, n,
447 &page_bytes);
448
449 n -= cpy;
450 s += cpy;
451 while (page_bytes > 0) {
452 unsigned long written;
453 int limit = 1000000;
454
455 while (limit--) {
456 unsigned long stat;
457
458 stat = sun4v_con_write(ra, page_bytes,
459 &written);
460 if (stat == HV_EOK)
461 break;
462 udelay(1);
463 }
464 if (limit < 0)
465 break;
466 page_bytes -= written;
467 ra += written;
468 }
469 }
470
471 if (locked)
472 spin_unlock(&port->lock);
473 local_irq_restore(flags);
474}
475
476static inline void sunhv_console_putchar(struct uart_port *port, char c)
477{
478 int limit = 1000000;
479
480 while (limit-- > 0) {
481 long status = sun4v_con_putchar(c);
482 if (status == HV_EOK)
483 break;
484 udelay(1);
485 }
486}
487
488static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
489{
490 struct uart_port *port = sunhv_port;
491 unsigned long flags;
492 int i, locked = 1;
493
494 local_irq_save(flags);
495 if (port->sysrq) {
496 locked = 0;
497 } else if (oops_in_progress) {
498 locked = spin_trylock(&port->lock);
499 } else
500 spin_lock(&port->lock);
501
502 for (i = 0; i < n; i++) {
503 if (*s == '\n')
504 sunhv_console_putchar(port, '\r');
505 sunhv_console_putchar(port, *s++);
506 }
507
508 if (locked)
509 spin_unlock(&port->lock);
510 local_irq_restore(flags);
511}
512
513static struct console sunhv_console = {
514 .name = "ttyHV",
515 .write = sunhv_console_write_bychar,
516 .device = uart_console_device,
517 .flags = CON_PRINTBUFFER,
518 .index = -1,
519 .data = &sunhv_reg,
520};
521
522static int __devinit hv_probe(struct platform_device *op)
523{
524 struct uart_port *port;
525 unsigned long minor;
526 int err;
527
528 if (op->archdata.irqs[0] == 0xffffffff)
529 return -ENODEV;
530
531 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
532 if (unlikely(!port))
533 return -ENOMEM;
534
535 minor = 1;
536 if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
537 minor >= 1) {
538 err = -ENOMEM;
539 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
540 if (!con_write_page)
541 goto out_free_port;
542
543 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
544 if (!con_read_page)
545 goto out_free_con_write_page;
546
547 sunhv_console.write = sunhv_console_write_paged;
548 sunhv_ops = &bywrite_ops;
549 }
550
551 sunhv_port = port;
552
553 port->line = 0;
554 port->ops = &sunhv_pops;
555 port->type = PORT_SUNHV;
556 port->uartclk = ( 29491200 / 16 ); /* arbitrary */
557
558 port->membase = (unsigned char __iomem *) __pa(port);
559
560 port->irq = op->archdata.irqs[0];
561
562 port->dev = &op->dev;
563
564 err = sunserial_register_minors(&sunhv_reg, 1);
565 if (err)
566 goto out_free_con_read_page;
567
568 sunserial_console_match(&sunhv_console, op->dev.of_node,
569 &sunhv_reg, port->line, false);
570
571 err = uart_add_one_port(&sunhv_reg, port);
572 if (err)
573 goto out_unregister_driver;
574
575 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
576 if (err)
577 goto out_remove_port;
578
579 dev_set_drvdata(&op->dev, port);
580
581 return 0;
582
583out_remove_port:
584 uart_remove_one_port(&sunhv_reg, port);
585
586out_unregister_driver:
587 sunserial_unregister_minors(&sunhv_reg, 1);
588
589out_free_con_read_page:
590 kfree(con_read_page);
591
592out_free_con_write_page:
593 kfree(con_write_page);
594
595out_free_port:
596 kfree(port);
597 sunhv_port = NULL;
598 return err;
599}
600
601static int __devexit hv_remove(struct platform_device *dev)
602{
603 struct uart_port *port = dev_get_drvdata(&dev->dev);
604
605 free_irq(port->irq, port);
606
607 uart_remove_one_port(&sunhv_reg, port);
608
609 sunserial_unregister_minors(&sunhv_reg, 1);
610
611 kfree(port);
612 sunhv_port = NULL;
613
614 dev_set_drvdata(&dev->dev, NULL);
615
616 return 0;
617}
618
619static const struct of_device_id hv_match[] = {
620 {
621 .name = "console",
622 .compatible = "qcn",
623 },
624 {
625 .name = "console",
626 .compatible = "SUNW,sun4v-console",
627 },
628 {},
629};
630MODULE_DEVICE_TABLE(of, hv_match);
631
632static struct platform_driver hv_driver = {
633 .driver = {
634 .name = "hv",
635 .owner = THIS_MODULE,
636 .of_match_table = hv_match,
637 },
638 .probe = hv_probe,
639 .remove = __devexit_p(hv_remove),
640};
641
642static int __init sunhv_init(void)
643{
644 if (tlb_type != hypervisor)
645 return -ENODEV;
646
647 return platform_driver_register(&hv_driver);
648}
649
650static void __exit sunhv_exit(void)
651{
652 platform_driver_unregister(&hv_driver);
653}
654
655module_init(sunhv_init);
656module_exit(sunhv_exit);
657
658MODULE_AUTHOR("David S. Miller");
659MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
660MODULE_VERSION("2.0");
661MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunsab.c b/drivers/tty/serial/sunsab.c
new file mode 100644
index 000000000000..b5fa2a57b9da
--- /dev/null
+++ b/drivers/tty/serial/sunsab.c
@@ -0,0 +1,1152 @@
1/* sunsab.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 * Copyright (C) 2002, 2006 David S. Miller (davem@davemloft.net)
5 *
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 * Maxim Krasnyanskiy <maxk@qualcomm.com>
8 *
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART. Also eliminated a lot of
11 * duplicated code in the console setup.
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 *
14 * Ported to new 2.5.x UART layer.
15 * David S. Miller <davem@davemloft.net>
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/major.h>
24#include <linux/string.h>
25#include <linux/ptrace.h>
26#include <linux/ioport.h>
27#include <linux/circ_buf.h>
28#include <linux/serial.h>
29#include <linux/sysrq.h>
30#include <linux/console.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/init.h>
35#include <linux/of_device.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40
41#if defined(CONFIG_SERIAL_SUNSAB_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
42#define SUPPORT_SYSRQ
43#endif
44
45#include <linux/serial_core.h>
46
47#include "suncore.h"
48#include "sunsab.h"
49
50struct uart_sunsab_port {
51 struct uart_port port; /* Generic UART port */
52 union sab82532_async_regs __iomem *regs; /* Chip registers */
53 unsigned long irqflags; /* IRQ state flags */
54 int dsr; /* Current DSR state */
55 unsigned int cec_timeout; /* Chip poll timeout... */
56 unsigned int tec_timeout; /* likewise */
57 unsigned char interrupt_mask0;/* ISR0 masking */
58 unsigned char interrupt_mask1;/* ISR1 masking */
59 unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
60 unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
61 unsigned int gis_shift;
62 int type; /* SAB82532 version */
63
64 /* Setting configuration bits while the transmitter is active
65 * can cause garbage characters to get emitted by the chip.
66 * Therefore, we cache such writes here and do the real register
67 * write the next time the transmitter becomes idle.
68 */
69 unsigned int cached_ebrg;
70 unsigned char cached_mode;
71 unsigned char cached_pvr;
72 unsigned char cached_dafo;
73};
74
75/*
76 * This assumes you have a 29.4912 MHz clock for your UART.
77 */
78#define SAB_BASE_BAUD ( 29491200 / 16 )
79
80static char *sab82532_version[16] = {
81 "V1.0", "V2.0", "V3.2", "V(0x03)",
82 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
83 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
84 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
85};
86
87#define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
88#define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
89
90#define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
91#define SAB82532_XMIT_FIFO_SIZE 32
92
93static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
94{
95 int timeout = up->tec_timeout;
96
97 while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
98 udelay(1);
99}
100
101static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
102{
103 int timeout = up->cec_timeout;
104
105 while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
106 udelay(1);
107}
108
109static struct tty_struct *
110receive_chars(struct uart_sunsab_port *up,
111 union sab82532_irq_status *stat)
112{
113 struct tty_struct *tty = NULL;
114 unsigned char buf[32];
115 int saw_console_brk = 0;
116 int free_fifo = 0;
117 int count = 0;
118 int i;
119
120 if (up->port.state != NULL) /* Unopened serial console */
121 tty = up->port.state->port.tty;
122
123 /* Read number of BYTES (Character + Status) available. */
124 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
125 count = SAB82532_RECV_FIFO_SIZE;
126 free_fifo++;
127 }
128
129 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
130 count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
131 free_fifo++;
132 }
133
134 /* Issue a FIFO read command in case we where idle. */
135 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
136 sunsab_cec_wait(up);
137 writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
138 return tty;
139 }
140
141 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
142 free_fifo++;
143
144 /* Read the FIFO. */
145 for (i = 0; i < count; i++)
146 buf[i] = readb(&up->regs->r.rfifo[i]);
147
148 /* Issue Receive Message Complete command. */
149 if (free_fifo) {
150 sunsab_cec_wait(up);
151 writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
152 }
153
154 /* Count may be zero for BRK, so we check for it here */
155 if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
156 (up->port.line == up->port.cons->index))
157 saw_console_brk = 1;
158
159 for (i = 0; i < count; i++) {
160 unsigned char ch = buf[i], flag;
161
162 if (tty == NULL) {
163 uart_handle_sysrq_char(&up->port, ch);
164 continue;
165 }
166
167 flag = TTY_NORMAL;
168 up->port.icount.rx++;
169
170 if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
171 SAB82532_ISR0_FERR |
172 SAB82532_ISR0_RFO)) ||
173 unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
174 /*
175 * For statistics only
176 */
177 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
178 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
179 SAB82532_ISR0_FERR);
180 up->port.icount.brk++;
181 /*
182 * We do the SysRQ and SAK checking
183 * here because otherwise the break
184 * may get masked by ignore_status_mask
185 * or read_status_mask.
186 */
187 if (uart_handle_break(&up->port))
188 continue;
189 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
190 up->port.icount.parity++;
191 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
192 up->port.icount.frame++;
193 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
194 up->port.icount.overrun++;
195
196 /*
197 * Mask off conditions which should be ingored.
198 */
199 stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
200 stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
201
202 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
203 flag = TTY_BREAK;
204 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
205 flag = TTY_PARITY;
206 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
207 flag = TTY_FRAME;
208 }
209
210 if (uart_handle_sysrq_char(&up->port, ch))
211 continue;
212
213 if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
214 (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0)
215 tty_insert_flip_char(tty, ch, flag);
216 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
217 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
218 }
219
220 if (saw_console_brk)
221 sun_do_break();
222
223 return tty;
224}
225
226static void sunsab_stop_tx(struct uart_port *);
227static void sunsab_tx_idle(struct uart_sunsab_port *);
228
229static void transmit_chars(struct uart_sunsab_port *up,
230 union sab82532_irq_status *stat)
231{
232 struct circ_buf *xmit = &up->port.state->xmit;
233 int i;
234
235 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
236 up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
237 writeb(up->interrupt_mask1, &up->regs->w.imr1);
238 set_bit(SAB82532_ALLS, &up->irqflags);
239 }
240
241#if 0 /* bde@nwlink.com says this check causes problems */
242 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
243 return;
244#endif
245
246 if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
247 return;
248
249 set_bit(SAB82532_XPR, &up->irqflags);
250 sunsab_tx_idle(up);
251
252 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
253 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
254 writeb(up->interrupt_mask1, &up->regs->w.imr1);
255 return;
256 }
257
258 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
259 writeb(up->interrupt_mask1, &up->regs->w.imr1);
260 clear_bit(SAB82532_ALLS, &up->irqflags);
261
262 /* Stuff 32 bytes into Transmit FIFO. */
263 clear_bit(SAB82532_XPR, &up->irqflags);
264 for (i = 0; i < up->port.fifosize; i++) {
265 writeb(xmit->buf[xmit->tail],
266 &up->regs->w.xfifo[i]);
267 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
268 up->port.icount.tx++;
269 if (uart_circ_empty(xmit))
270 break;
271 }
272
273 /* Issue a Transmit Frame command. */
274 sunsab_cec_wait(up);
275 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
276
277 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
278 uart_write_wakeup(&up->port);
279
280 if (uart_circ_empty(xmit))
281 sunsab_stop_tx(&up->port);
282}
283
284static void check_status(struct uart_sunsab_port *up,
285 union sab82532_irq_status *stat)
286{
287 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
288 uart_handle_dcd_change(&up->port,
289 !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
290
291 if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
292 uart_handle_cts_change(&up->port,
293 (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
294
295 if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
296 up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
297 up->port.icount.dsr++;
298 }
299
300 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
301}
302
303static irqreturn_t sunsab_interrupt(int irq, void *dev_id)
304{
305 struct uart_sunsab_port *up = dev_id;
306 struct tty_struct *tty;
307 union sab82532_irq_status status;
308 unsigned long flags;
309 unsigned char gis;
310
311 spin_lock_irqsave(&up->port.lock, flags);
312
313 status.stat = 0;
314 gis = readb(&up->regs->r.gis) >> up->gis_shift;
315 if (gis & 1)
316 status.sreg.isr0 = readb(&up->regs->r.isr0);
317 if (gis & 2)
318 status.sreg.isr1 = readb(&up->regs->r.isr1);
319
320 tty = NULL;
321 if (status.stat) {
322 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
323 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
324 (status.sreg.isr1 & SAB82532_ISR1_BRK))
325 tty = receive_chars(up, &status);
326 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
327 (status.sreg.isr1 & SAB82532_ISR1_CSC))
328 check_status(up, &status);
329 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
330 transmit_chars(up, &status);
331 }
332
333 spin_unlock_irqrestore(&up->port.lock, flags);
334
335 if (tty)
336 tty_flip_buffer_push(tty);
337
338 return IRQ_HANDLED;
339}
340
341/* port->lock is not held. */
342static unsigned int sunsab_tx_empty(struct uart_port *port)
343{
344 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
345 int ret;
346
347 /* Do not need a lock for a state test like this. */
348 if (test_bit(SAB82532_ALLS, &up->irqflags))
349 ret = TIOCSER_TEMT;
350 else
351 ret = 0;
352
353 return ret;
354}
355
356/* port->lock held by caller. */
357static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
358{
359 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
360
361 if (mctrl & TIOCM_RTS) {
362 up->cached_mode &= ~SAB82532_MODE_FRTS;
363 up->cached_mode |= SAB82532_MODE_RTS;
364 } else {
365 up->cached_mode |= (SAB82532_MODE_FRTS |
366 SAB82532_MODE_RTS);
367 }
368 if (mctrl & TIOCM_DTR) {
369 up->cached_pvr &= ~(up->pvr_dtr_bit);
370 } else {
371 up->cached_pvr |= up->pvr_dtr_bit;
372 }
373
374 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
375 if (test_bit(SAB82532_XPR, &up->irqflags))
376 sunsab_tx_idle(up);
377}
378
379/* port->lock is held by caller and interrupts are disabled. */
380static unsigned int sunsab_get_mctrl(struct uart_port *port)
381{
382 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
383 unsigned char val;
384 unsigned int result;
385
386 result = 0;
387
388 val = readb(&up->regs->r.pvr);
389 result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
390
391 val = readb(&up->regs->r.vstr);
392 result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
393
394 val = readb(&up->regs->r.star);
395 result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
396
397 return result;
398}
399
400/* port->lock held by caller. */
401static void sunsab_stop_tx(struct uart_port *port)
402{
403 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
404
405 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
406 writeb(up->interrupt_mask1, &up->regs->w.imr1);
407}
408
409/* port->lock held by caller. */
410static void sunsab_tx_idle(struct uart_sunsab_port *up)
411{
412 if (test_bit(SAB82532_REGS_PENDING, &up->irqflags)) {
413 u8 tmp;
414
415 clear_bit(SAB82532_REGS_PENDING, &up->irqflags);
416 writeb(up->cached_mode, &up->regs->rw.mode);
417 writeb(up->cached_pvr, &up->regs->rw.pvr);
418 writeb(up->cached_dafo, &up->regs->w.dafo);
419
420 writeb(up->cached_ebrg & 0xff, &up->regs->w.bgr);
421 tmp = readb(&up->regs->rw.ccr2);
422 tmp &= ~0xc0;
423 tmp |= (up->cached_ebrg >> 2) & 0xc0;
424 writeb(tmp, &up->regs->rw.ccr2);
425 }
426}
427
428/* port->lock held by caller. */
429static void sunsab_start_tx(struct uart_port *port)
430{
431 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
432 struct circ_buf *xmit = &up->port.state->xmit;
433 int i;
434
435 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
436 writeb(up->interrupt_mask1, &up->regs->w.imr1);
437
438 if (!test_bit(SAB82532_XPR, &up->irqflags))
439 return;
440
441 clear_bit(SAB82532_ALLS, &up->irqflags);
442 clear_bit(SAB82532_XPR, &up->irqflags);
443
444 for (i = 0; i < up->port.fifosize; i++) {
445 writeb(xmit->buf[xmit->tail],
446 &up->regs->w.xfifo[i]);
447 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
448 up->port.icount.tx++;
449 if (uart_circ_empty(xmit))
450 break;
451 }
452
453 /* Issue a Transmit Frame command. */
454 sunsab_cec_wait(up);
455 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
456}
457
458/* port->lock is not held. */
459static void sunsab_send_xchar(struct uart_port *port, char ch)
460{
461 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
462 unsigned long flags;
463
464 spin_lock_irqsave(&up->port.lock, flags);
465
466 sunsab_tec_wait(up);
467 writeb(ch, &up->regs->w.tic);
468
469 spin_unlock_irqrestore(&up->port.lock, flags);
470}
471
472/* port->lock held by caller. */
473static void sunsab_stop_rx(struct uart_port *port)
474{
475 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
476
477 up->interrupt_mask0 |= SAB82532_IMR0_TCD;
478 writeb(up->interrupt_mask1, &up->regs->w.imr0);
479}
480
481/* port->lock held by caller. */
482static void sunsab_enable_ms(struct uart_port *port)
483{
484 /* For now we always receive these interrupts. */
485}
486
487/* port->lock is not held. */
488static void sunsab_break_ctl(struct uart_port *port, int break_state)
489{
490 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
491 unsigned long flags;
492 unsigned char val;
493
494 spin_lock_irqsave(&up->port.lock, flags);
495
496 val = up->cached_dafo;
497 if (break_state)
498 val |= SAB82532_DAFO_XBRK;
499 else
500 val &= ~SAB82532_DAFO_XBRK;
501 up->cached_dafo = val;
502
503 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
504 if (test_bit(SAB82532_XPR, &up->irqflags))
505 sunsab_tx_idle(up);
506
507 spin_unlock_irqrestore(&up->port.lock, flags);
508}
509
510/* port->lock is not held. */
511static int sunsab_startup(struct uart_port *port)
512{
513 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
514 unsigned long flags;
515 unsigned char tmp;
516 int err = request_irq(up->port.irq, sunsab_interrupt,
517 IRQF_SHARED, "sab", up);
518 if (err)
519 return err;
520
521 spin_lock_irqsave(&up->port.lock, flags);
522
523 /*
524 * Wait for any commands or immediate characters
525 */
526 sunsab_cec_wait(up);
527 sunsab_tec_wait(up);
528
529 /*
530 * Clear the FIFO buffers.
531 */
532 writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
533 sunsab_cec_wait(up);
534 writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
535
536 /*
537 * Clear the interrupt registers.
538 */
539 (void) readb(&up->regs->r.isr0);
540 (void) readb(&up->regs->r.isr1);
541
542 /*
543 * Now, initialize the UART
544 */
545 writeb(0, &up->regs->w.ccr0); /* power-down */
546 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
547 SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
548 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
549 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
550 SAB82532_CCR2_TOE, &up->regs->w.ccr2);
551 writeb(0, &up->regs->w.ccr3);
552 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
553 up->cached_mode = (SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
554 SAB82532_MODE_RAC);
555 writeb(up->cached_mode, &up->regs->w.mode);
556 writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
557
558 tmp = readb(&up->regs->rw.ccr0);
559 tmp |= SAB82532_CCR0_PU; /* power-up */
560 writeb(tmp, &up->regs->rw.ccr0);
561
562 /*
563 * Finally, enable interrupts
564 */
565 up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
566 SAB82532_IMR0_PLLA);
567 writeb(up->interrupt_mask0, &up->regs->w.imr0);
568 up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
569 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
570 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
571 SAB82532_IMR1_XPR);
572 writeb(up->interrupt_mask1, &up->regs->w.imr1);
573 set_bit(SAB82532_ALLS, &up->irqflags);
574 set_bit(SAB82532_XPR, &up->irqflags);
575
576 spin_unlock_irqrestore(&up->port.lock, flags);
577
578 return 0;
579}
580
581/* port->lock is not held. */
582static void sunsab_shutdown(struct uart_port *port)
583{
584 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
585 unsigned long flags;
586
587 spin_lock_irqsave(&up->port.lock, flags);
588
589 /* Disable Interrupts */
590 up->interrupt_mask0 = 0xff;
591 writeb(up->interrupt_mask0, &up->regs->w.imr0);
592 up->interrupt_mask1 = 0xff;
593 writeb(up->interrupt_mask1, &up->regs->w.imr1);
594
595 /* Disable break condition */
596 up->cached_dafo = readb(&up->regs->rw.dafo);
597 up->cached_dafo &= ~SAB82532_DAFO_XBRK;
598 writeb(up->cached_dafo, &up->regs->rw.dafo);
599
600 /* Disable Receiver */
601 up->cached_mode &= ~SAB82532_MODE_RAC;
602 writeb(up->cached_mode, &up->regs->rw.mode);
603
604 /*
605 * XXX FIXME
606 *
607 * If the chip is powered down here the system hangs/crashes during
608 * reboot or shutdown. This needs to be investigated further,
609 * similar behaviour occurs in 2.4 when the driver is configured
610 * as a module only. One hint may be that data is sometimes
611 * transmitted at 9600 baud during shutdown (regardless of the
612 * speed the chip was configured for when the port was open).
613 */
614#if 0
615 /* Power Down */
616 tmp = readb(&up->regs->rw.ccr0);
617 tmp &= ~SAB82532_CCR0_PU;
618 writeb(tmp, &up->regs->rw.ccr0);
619#endif
620
621 spin_unlock_irqrestore(&up->port.lock, flags);
622 free_irq(up->port.irq, up);
623}
624
625/*
626 * This is used to figure out the divisor speeds.
627 *
628 * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
629 *
630 * with 0 <= N < 64 and 0 <= M < 16
631 */
632
633static void calc_ebrg(int baud, int *n_ret, int *m_ret)
634{
635 int n, m;
636
637 if (baud == 0) {
638 *n_ret = 0;
639 *m_ret = 0;
640 return;
641 }
642
643 /*
644 * We scale numbers by 10 so that we get better accuracy
645 * without having to use floating point. Here we increment m
646 * until n is within the valid range.
647 */
648 n = (SAB_BASE_BAUD * 10) / baud;
649 m = 0;
650 while (n >= 640) {
651 n = n / 2;
652 m++;
653 }
654 n = (n+5) / 10;
655 /*
656 * We try very hard to avoid speeds with M == 0 since they may
657 * not work correctly for XTAL frequences above 10 MHz.
658 */
659 if ((m == 0) && ((n & 1) == 0)) {
660 n = n / 2;
661 m++;
662 }
663 *n_ret = n - 1;
664 *m_ret = m;
665}
666
667/* Internal routine, port->lock is held and local interrupts are disabled. */
668static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
669 unsigned int iflag, unsigned int baud,
670 unsigned int quot)
671{
672 unsigned char dafo;
673 int bits, n, m;
674
675 /* Byte size and parity */
676 switch (cflag & CSIZE) {
677 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
678 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
679 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
680 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
681 /* Never happens, but GCC is too dumb to figure it out */
682 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
683 }
684
685 if (cflag & CSTOPB) {
686 dafo |= SAB82532_DAFO_STOP;
687 bits++;
688 }
689
690 if (cflag & PARENB) {
691 dafo |= SAB82532_DAFO_PARE;
692 bits++;
693 }
694
695 if (cflag & PARODD) {
696 dafo |= SAB82532_DAFO_PAR_ODD;
697 } else {
698 dafo |= SAB82532_DAFO_PAR_EVEN;
699 }
700 up->cached_dafo = dafo;
701
702 calc_ebrg(baud, &n, &m);
703
704 up->cached_ebrg = n | (m << 6);
705
706 up->tec_timeout = (10 * 1000000) / baud;
707 up->cec_timeout = up->tec_timeout >> 2;
708
709 /* CTS flow control flags */
710 /* We encode read_status_mask and ignore_status_mask like so:
711 *
712 * ---------------------
713 * | ... | ISR1 | ISR0 |
714 * ---------------------
715 * .. 15 8 7 0
716 */
717
718 up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
719 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
720 SAB82532_ISR0_CDSC);
721 up->port.read_status_mask |= (SAB82532_ISR1_CSC |
722 SAB82532_ISR1_ALLS |
723 SAB82532_ISR1_XPR) << 8;
724 if (iflag & INPCK)
725 up->port.read_status_mask |= (SAB82532_ISR0_PERR |
726 SAB82532_ISR0_FERR);
727 if (iflag & (BRKINT | PARMRK))
728 up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
729
730 /*
731 * Characteres to ignore
732 */
733 up->port.ignore_status_mask = 0;
734 if (iflag & IGNPAR)
735 up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
736 SAB82532_ISR0_FERR);
737 if (iflag & IGNBRK) {
738 up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
739 /*
740 * If we're ignoring parity and break indicators,
741 * ignore overruns too (for real raw support).
742 */
743 if (iflag & IGNPAR)
744 up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
745 }
746
747 /*
748 * ignore all characters if CREAD is not set
749 */
750 if ((cflag & CREAD) == 0)
751 up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
752 SAB82532_ISR0_TCD);
753
754 uart_update_timeout(&up->port, cflag,
755 (up->port.uartclk / (16 * quot)));
756
757 /* Now schedule a register update when the chip's
758 * transmitter is idle.
759 */
760 up->cached_mode |= SAB82532_MODE_RAC;
761 set_bit(SAB82532_REGS_PENDING, &up->irqflags);
762 if (test_bit(SAB82532_XPR, &up->irqflags))
763 sunsab_tx_idle(up);
764}
765
766/* port->lock is not held. */
767static void sunsab_set_termios(struct uart_port *port, struct ktermios *termios,
768 struct ktermios *old)
769{
770 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
771 unsigned long flags;
772 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
773 unsigned int quot = uart_get_divisor(port, baud);
774
775 spin_lock_irqsave(&up->port.lock, flags);
776 sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
777 spin_unlock_irqrestore(&up->port.lock, flags);
778}
779
780static const char *sunsab_type(struct uart_port *port)
781{
782 struct uart_sunsab_port *up = (void *)port;
783 static char buf[36];
784
785 sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
786 return buf;
787}
788
789static void sunsab_release_port(struct uart_port *port)
790{
791}
792
793static int sunsab_request_port(struct uart_port *port)
794{
795 return 0;
796}
797
798static void sunsab_config_port(struct uart_port *port, int flags)
799{
800}
801
802static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
803{
804 return -EINVAL;
805}
806
807static struct uart_ops sunsab_pops = {
808 .tx_empty = sunsab_tx_empty,
809 .set_mctrl = sunsab_set_mctrl,
810 .get_mctrl = sunsab_get_mctrl,
811 .stop_tx = sunsab_stop_tx,
812 .start_tx = sunsab_start_tx,
813 .send_xchar = sunsab_send_xchar,
814 .stop_rx = sunsab_stop_rx,
815 .enable_ms = sunsab_enable_ms,
816 .break_ctl = sunsab_break_ctl,
817 .startup = sunsab_startup,
818 .shutdown = sunsab_shutdown,
819 .set_termios = sunsab_set_termios,
820 .type = sunsab_type,
821 .release_port = sunsab_release_port,
822 .request_port = sunsab_request_port,
823 .config_port = sunsab_config_port,
824 .verify_port = sunsab_verify_port,
825};
826
827static struct uart_driver sunsab_reg = {
828 .owner = THIS_MODULE,
829 .driver_name = "sunsab",
830 .dev_name = "ttyS",
831 .major = TTY_MAJOR,
832};
833
834static struct uart_sunsab_port *sunsab_ports;
835
836#ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
837
838static void sunsab_console_putchar(struct uart_port *port, int c)
839{
840 struct uart_sunsab_port *up = (struct uart_sunsab_port *)port;
841
842 sunsab_tec_wait(up);
843 writeb(c, &up->regs->w.tic);
844}
845
846static void sunsab_console_write(struct console *con, const char *s, unsigned n)
847{
848 struct uart_sunsab_port *up = &sunsab_ports[con->index];
849 unsigned long flags;
850 int locked = 1;
851
852 local_irq_save(flags);
853 if (up->port.sysrq) {
854 locked = 0;
855 } else if (oops_in_progress) {
856 locked = spin_trylock(&up->port.lock);
857 } else
858 spin_lock(&up->port.lock);
859
860 uart_console_write(&up->port, s, n, sunsab_console_putchar);
861 sunsab_tec_wait(up);
862
863 if (locked)
864 spin_unlock(&up->port.lock);
865 local_irq_restore(flags);
866}
867
868static int sunsab_console_setup(struct console *con, char *options)
869{
870 struct uart_sunsab_port *up = &sunsab_ports[con->index];
871 unsigned long flags;
872 unsigned int baud, quot;
873
874 /*
875 * The console framework calls us for each and every port
876 * registered. Defer the console setup until the requested
877 * port has been properly discovered. A bit of a hack,
878 * though...
879 */
880 if (up->port.type != PORT_SUNSAB)
881 return -1;
882
883 printk("Console: ttyS%d (SAB82532)\n",
884 (sunsab_reg.minor - 64) + con->index);
885
886 sunserial_console_termios(con, up->port.dev->of_node);
887
888 switch (con->cflag & CBAUD) {
889 case B150: baud = 150; break;
890 case B300: baud = 300; break;
891 case B600: baud = 600; break;
892 case B1200: baud = 1200; break;
893 case B2400: baud = 2400; break;
894 case B4800: baud = 4800; break;
895 default: case B9600: baud = 9600; break;
896 case B19200: baud = 19200; break;
897 case B38400: baud = 38400; break;
898 case B57600: baud = 57600; break;
899 case B115200: baud = 115200; break;
900 case B230400: baud = 230400; break;
901 case B460800: baud = 460800; break;
902 };
903
904 /*
905 * Temporary fix.
906 */
907 spin_lock_init(&up->port.lock);
908
909 /*
910 * Initialize the hardware
911 */
912 sunsab_startup(&up->port);
913
914 spin_lock_irqsave(&up->port.lock, flags);
915
916 /*
917 * Finally, enable interrupts
918 */
919 up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
920 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
921 writeb(up->interrupt_mask0, &up->regs->w.imr0);
922 up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
923 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
924 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
925 SAB82532_IMR1_XPR;
926 writeb(up->interrupt_mask1, &up->regs->w.imr1);
927
928 quot = uart_get_divisor(&up->port, baud);
929 sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
930 sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
931
932 spin_unlock_irqrestore(&up->port.lock, flags);
933
934 return 0;
935}
936
937static struct console sunsab_console = {
938 .name = "ttyS",
939 .write = sunsab_console_write,
940 .device = uart_console_device,
941 .setup = sunsab_console_setup,
942 .flags = CON_PRINTBUFFER,
943 .index = -1,
944 .data = &sunsab_reg,
945};
946
947static inline struct console *SUNSAB_CONSOLE(void)
948{
949 return &sunsab_console;
950}
951#else
952#define SUNSAB_CONSOLE() (NULL)
953#define sunsab_console_init() do { } while (0)
954#endif
955
956static int __devinit sunsab_init_one(struct uart_sunsab_port *up,
957 struct platform_device *op,
958 unsigned long offset,
959 int line)
960{
961 up->port.line = line;
962 up->port.dev = &op->dev;
963
964 up->port.mapbase = op->resource[0].start + offset;
965 up->port.membase = of_ioremap(&op->resource[0], offset,
966 sizeof(union sab82532_async_regs),
967 "sab");
968 if (!up->port.membase)
969 return -ENOMEM;
970 up->regs = (union sab82532_async_regs __iomem *) up->port.membase;
971
972 up->port.irq = op->archdata.irqs[0];
973
974 up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
975 up->port.iotype = UPIO_MEM;
976
977 writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
978
979 up->port.ops = &sunsab_pops;
980 up->port.type = PORT_SUNSAB;
981 up->port.uartclk = SAB_BASE_BAUD;
982
983 up->type = readb(&up->regs->r.vstr) & 0x0f;
984 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
985 writeb(0xff, &up->regs->w.pim);
986 if ((up->port.line & 0x1) == 0) {
987 up->pvr_dsr_bit = (1 << 0);
988 up->pvr_dtr_bit = (1 << 1);
989 up->gis_shift = 2;
990 } else {
991 up->pvr_dsr_bit = (1 << 3);
992 up->pvr_dtr_bit = (1 << 2);
993 up->gis_shift = 0;
994 }
995 up->cached_pvr = (1 << 1) | (1 << 2) | (1 << 4);
996 writeb(up->cached_pvr, &up->regs->w.pvr);
997 up->cached_mode = readb(&up->regs->rw.mode);
998 up->cached_mode |= SAB82532_MODE_FRTS;
999 writeb(up->cached_mode, &up->regs->rw.mode);
1000 up->cached_mode |= SAB82532_MODE_RTS;
1001 writeb(up->cached_mode, &up->regs->rw.mode);
1002
1003 up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
1004 up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1005
1006 return 0;
1007}
1008
1009static int __devinit sab_probe(struct platform_device *op)
1010{
1011 static int inst;
1012 struct uart_sunsab_port *up;
1013 int err;
1014
1015 up = &sunsab_ports[inst * 2];
1016
1017 err = sunsab_init_one(&up[0], op,
1018 0,
1019 (inst * 2) + 0);
1020 if (err)
1021 goto out;
1022
1023 err = sunsab_init_one(&up[1], op,
1024 sizeof(union sab82532_async_regs),
1025 (inst * 2) + 1);
1026 if (err)
1027 goto out1;
1028
1029 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1030 &sunsab_reg, up[0].port.line,
1031 false);
1032
1033 sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
1034 &sunsab_reg, up[1].port.line,
1035 false);
1036
1037 err = uart_add_one_port(&sunsab_reg, &up[0].port);
1038 if (err)
1039 goto out2;
1040
1041 err = uart_add_one_port(&sunsab_reg, &up[1].port);
1042 if (err)
1043 goto out3;
1044
1045 dev_set_drvdata(&op->dev, &up[0]);
1046
1047 inst++;
1048
1049 return 0;
1050
1051out3:
1052 uart_remove_one_port(&sunsab_reg, &up[0].port);
1053out2:
1054 of_iounmap(&op->resource[0],
1055 up[1].port.membase,
1056 sizeof(union sab82532_async_regs));
1057out1:
1058 of_iounmap(&op->resource[0],
1059 up[0].port.membase,
1060 sizeof(union sab82532_async_regs));
1061out:
1062 return err;
1063}
1064
1065static int __devexit sab_remove(struct platform_device *op)
1066{
1067 struct uart_sunsab_port *up = dev_get_drvdata(&op->dev);
1068
1069 uart_remove_one_port(&sunsab_reg, &up[1].port);
1070 uart_remove_one_port(&sunsab_reg, &up[0].port);
1071 of_iounmap(&op->resource[0],
1072 up[1].port.membase,
1073 sizeof(union sab82532_async_regs));
1074 of_iounmap(&op->resource[0],
1075 up[0].port.membase,
1076 sizeof(union sab82532_async_regs));
1077
1078 dev_set_drvdata(&op->dev, NULL);
1079
1080 return 0;
1081}
1082
1083static const struct of_device_id sab_match[] = {
1084 {
1085 .name = "se",
1086 },
1087 {
1088 .name = "serial",
1089 .compatible = "sab82532",
1090 },
1091 {},
1092};
1093MODULE_DEVICE_TABLE(of, sab_match);
1094
1095static struct platform_driver sab_driver = {
1096 .driver = {
1097 .name = "sab",
1098 .owner = THIS_MODULE,
1099 .of_match_table = sab_match,
1100 },
1101 .probe = sab_probe,
1102 .remove = __devexit_p(sab_remove),
1103};
1104
1105static int __init sunsab_init(void)
1106{
1107 struct device_node *dp;
1108 int err;
1109 int num_channels = 0;
1110
1111 for_each_node_by_name(dp, "se")
1112 num_channels += 2;
1113 for_each_node_by_name(dp, "serial") {
1114 if (of_device_is_compatible(dp, "sab82532"))
1115 num_channels += 2;
1116 }
1117
1118 if (num_channels) {
1119 sunsab_ports = kzalloc(sizeof(struct uart_sunsab_port) *
1120 num_channels, GFP_KERNEL);
1121 if (!sunsab_ports)
1122 return -ENOMEM;
1123
1124 err = sunserial_register_minors(&sunsab_reg, num_channels);
1125 if (err) {
1126 kfree(sunsab_ports);
1127 sunsab_ports = NULL;
1128
1129 return err;
1130 }
1131 }
1132
1133 return platform_driver_register(&sab_driver);
1134}
1135
1136static void __exit sunsab_exit(void)
1137{
1138 platform_driver_unregister(&sab_driver);
1139 if (sunsab_reg.nr) {
1140 sunserial_unregister_minors(&sunsab_reg, sunsab_reg.nr);
1141 }
1142
1143 kfree(sunsab_ports);
1144 sunsab_ports = NULL;
1145}
1146
1147module_init(sunsab_init);
1148module_exit(sunsab_exit);
1149
1150MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
1151MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
1152MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunsab.h b/drivers/tty/serial/sunsab.h
new file mode 100644
index 000000000000..b78e1f7b8050
--- /dev/null
+++ b/drivers/tty/serial/sunsab.h
@@ -0,0 +1,322 @@
1/* sunsab.h: Register Definitions for the Siemens SAB82532 DUSCC
2 *
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 */
5
6#ifndef _SUNSAB_H
7#define _SUNSAB_H
8
9struct sab82532_async_rd_regs {
10 u8 rfifo[0x20]; /* Receive FIFO */
11 u8 star; /* Status Register */
12 u8 __pad1;
13 u8 mode; /* Mode Register */
14 u8 timr; /* Timer Register */
15 u8 xon; /* XON Character */
16 u8 xoff; /* XOFF Character */
17 u8 tcr; /* Termination Character Register */
18 u8 dafo; /* Data Format */
19 u8 rfc; /* RFIFO Control Register */
20 u8 __pad2;
21 u8 rbcl; /* Receive Byte Count Low */
22 u8 rbch; /* Receive Byte Count High */
23 u8 ccr0; /* Channel Configuration Register 0 */
24 u8 ccr1; /* Channel Configuration Register 1 */
25 u8 ccr2; /* Channel Configuration Register 2 */
26 u8 ccr3; /* Channel Configuration Register 3 */
27 u8 __pad3[4];
28 u8 vstr; /* Version Status Register */
29 u8 __pad4[3];
30 u8 gis; /* Global Interrupt Status */
31 u8 ipc; /* Interrupt Port Configuration */
32 u8 isr0; /* Interrupt Status 0 */
33 u8 isr1; /* Interrupt Status 1 */
34 u8 pvr; /* Port Value Register */
35 u8 pis; /* Port Interrupt Status */
36 u8 pcr; /* Port Configuration Register */
37 u8 ccr4; /* Channel Configuration Register 4 */
38};
39
40struct sab82532_async_wr_regs {
41 u8 xfifo[0x20]; /* Transmit FIFO */
42 u8 cmdr; /* Command Register */
43 u8 __pad1;
44 u8 mode;
45 u8 timr;
46 u8 xon;
47 u8 xoff;
48 u8 tcr;
49 u8 dafo;
50 u8 rfc;
51 u8 __pad2;
52 u8 xbcl; /* Transmit Byte Count Low */
53 u8 xbch; /* Transmit Byte Count High */
54 u8 ccr0;
55 u8 ccr1;
56 u8 ccr2;
57 u8 ccr3;
58 u8 tsax; /* Time-Slot Assignment Reg. Transmit */
59 u8 tsar; /* Time-Slot Assignment Reg. Receive */
60 u8 xccr; /* Transmit Channel Capacity Register */
61 u8 rccr; /* Receive Channel Capacity Register */
62 u8 bgr; /* Baud Rate Generator Register */
63 u8 tic; /* Transmit Immediate Character */
64 u8 mxn; /* Mask XON Character */
65 u8 mxf; /* Mask XOFF Character */
66 u8 iva; /* Interrupt Vector Address */
67 u8 ipc;
68 u8 imr0; /* Interrupt Mask Register 0 */
69 u8 imr1; /* Interrupt Mask Register 1 */
70 u8 pvr;
71 u8 pim; /* Port Interrupt Mask */
72 u8 pcr;
73 u8 ccr4;
74};
75
76struct sab82532_async_rw_regs { /* Read/Write registers */
77 u8 __pad1[0x20];
78 u8 __pad2;
79 u8 __pad3;
80 u8 mode;
81 u8 timr;
82 u8 xon;
83 u8 xoff;
84 u8 tcr;
85 u8 dafo;
86 u8 rfc;
87 u8 __pad4;
88 u8 __pad5;
89 u8 __pad6;
90 u8 ccr0;
91 u8 ccr1;
92 u8 ccr2;
93 u8 ccr3;
94 u8 __pad7;
95 u8 __pad8;
96 u8 __pad9;
97 u8 __pad10;
98 u8 __pad11;
99 u8 __pad12;
100 u8 __pad13;
101 u8 __pad14;
102 u8 __pad15;
103 u8 ipc;
104 u8 __pad16;
105 u8 __pad17;
106 u8 pvr;
107 u8 __pad18;
108 u8 pcr;
109 u8 ccr4;
110};
111
112union sab82532_async_regs {
113 __volatile__ struct sab82532_async_rd_regs r;
114 __volatile__ struct sab82532_async_wr_regs w;
115 __volatile__ struct sab82532_async_rw_regs rw;
116};
117
118union sab82532_irq_status {
119 unsigned short stat;
120 struct {
121 unsigned char isr0;
122 unsigned char isr1;
123 } sreg;
124};
125
126/* irqflags bits */
127#define SAB82532_ALLS 0x00000001
128#define SAB82532_XPR 0x00000002
129#define SAB82532_REGS_PENDING 0x00000004
130
131/* RFIFO Status Byte */
132#define SAB82532_RSTAT_PE 0x80
133#define SAB82532_RSTAT_FE 0x40
134#define SAB82532_RSTAT_PARITY 0x01
135
136/* Status Register (STAR) */
137#define SAB82532_STAR_XDOV 0x80
138#define SAB82532_STAR_XFW 0x40
139#define SAB82532_STAR_RFNE 0x20
140#define SAB82532_STAR_FCS 0x10
141#define SAB82532_STAR_TEC 0x08
142#define SAB82532_STAR_CEC 0x04
143#define SAB82532_STAR_CTS 0x02
144
145/* Command Register (CMDR) */
146#define SAB82532_CMDR_RMC 0x80
147#define SAB82532_CMDR_RRES 0x40
148#define SAB82532_CMDR_RFRD 0x20
149#define SAB82532_CMDR_STI 0x10
150#define SAB82532_CMDR_XF 0x08
151#define SAB82532_CMDR_XRES 0x01
152
153/* Mode Register (MODE) */
154#define SAB82532_MODE_FRTS 0x40
155#define SAB82532_MODE_FCTS 0x20
156#define SAB82532_MODE_FLON 0x10
157#define SAB82532_MODE_RAC 0x08
158#define SAB82532_MODE_RTS 0x04
159#define SAB82532_MODE_TRS 0x02
160#define SAB82532_MODE_TLP 0x01
161
162/* Timer Register (TIMR) */
163#define SAB82532_TIMR_CNT_MASK 0xe0
164#define SAB82532_TIMR_VALUE_MASK 0x1f
165
166/* Data Format (DAFO) */
167#define SAB82532_DAFO_XBRK 0x40
168#define SAB82532_DAFO_STOP 0x20
169#define SAB82532_DAFO_PAR_SPACE 0x00
170#define SAB82532_DAFO_PAR_ODD 0x08
171#define SAB82532_DAFO_PAR_EVEN 0x10
172#define SAB82532_DAFO_PAR_MARK 0x18
173#define SAB82532_DAFO_PARE 0x04
174#define SAB82532_DAFO_CHL8 0x00
175#define SAB82532_DAFO_CHL7 0x01
176#define SAB82532_DAFO_CHL6 0x02
177#define SAB82532_DAFO_CHL5 0x03
178
179/* RFIFO Control Register (RFC) */
180#define SAB82532_RFC_DPS 0x40
181#define SAB82532_RFC_DXS 0x20
182#define SAB82532_RFC_RFDF 0x10
183#define SAB82532_RFC_RFTH_1 0x00
184#define SAB82532_RFC_RFTH_4 0x04
185#define SAB82532_RFC_RFTH_16 0x08
186#define SAB82532_RFC_RFTH_32 0x0c
187#define SAB82532_RFC_TCDE 0x01
188
189/* Received Byte Count High (RBCH) */
190#define SAB82532_RBCH_DMA 0x80
191#define SAB82532_RBCH_CAS 0x20
192
193/* Transmit Byte Count High (XBCH) */
194#define SAB82532_XBCH_DMA 0x80
195#define SAB82532_XBCH_CAS 0x20
196#define SAB82532_XBCH_XC 0x10
197
198/* Channel Configuration Register 0 (CCR0) */
199#define SAB82532_CCR0_PU 0x80
200#define SAB82532_CCR0_MCE 0x40
201#define SAB82532_CCR0_SC_NRZ 0x00
202#define SAB82532_CCR0_SC_NRZI 0x08
203#define SAB82532_CCR0_SC_FM0 0x10
204#define SAB82532_CCR0_SC_FM1 0x14
205#define SAB82532_CCR0_SC_MANCH 0x18
206#define SAB82532_CCR0_SM_HDLC 0x00
207#define SAB82532_CCR0_SM_SDLC_LOOP 0x01
208#define SAB82532_CCR0_SM_BISYNC 0x02
209#define SAB82532_CCR0_SM_ASYNC 0x03
210
211/* Channel Configuration Register 1 (CCR1) */
212#define SAB82532_CCR1_ODS 0x10
213#define SAB82532_CCR1_BCR 0x08
214#define SAB82532_CCR1_CM_MASK 0x07
215
216/* Channel Configuration Register 2 (CCR2) */
217#define SAB82532_CCR2_SOC1 0x80
218#define SAB82532_CCR2_SOC0 0x40
219#define SAB82532_CCR2_BR9 0x80
220#define SAB82532_CCR2_BR8 0x40
221#define SAB82532_CCR2_BDF 0x20
222#define SAB82532_CCR2_SSEL 0x10
223#define SAB82532_CCR2_XCS0 0x20
224#define SAB82532_CCR2_RCS0 0x10
225#define SAB82532_CCR2_TOE 0x08
226#define SAB82532_CCR2_RWX 0x04
227#define SAB82532_CCR2_DIV 0x01
228
229/* Channel Configuration Register 3 (CCR3) */
230#define SAB82532_CCR3_PSD 0x01
231
232/* Time Slot Assignment Register Transmit (TSAX) */
233#define SAB82532_TSAX_TSNX_MASK 0xfc
234#define SAB82532_TSAX_XCS2 0x02 /* see also CCR2 */
235#define SAB82532_TSAX_XCS1 0x01
236
237/* Time Slot Assignment Register Receive (TSAR) */
238#define SAB82532_TSAR_TSNR_MASK 0xfc
239#define SAB82532_TSAR_RCS2 0x02 /* see also CCR2 */
240#define SAB82532_TSAR_RCS1 0x01
241
242/* Version Status Register (VSTR) */
243#define SAB82532_VSTR_CD 0x80
244#define SAB82532_VSTR_DPLA 0x40
245#define SAB82532_VSTR_VN_MASK 0x0f
246#define SAB82532_VSTR_VN_1 0x00
247#define SAB82532_VSTR_VN_2 0x01
248#define SAB82532_VSTR_VN_3_2 0x02
249
250/* Global Interrupt Status Register (GIS) */
251#define SAB82532_GIS_PI 0x80
252#define SAB82532_GIS_ISA1 0x08
253#define SAB82532_GIS_ISA0 0x04
254#define SAB82532_GIS_ISB1 0x02
255#define SAB82532_GIS_ISB0 0x01
256
257/* Interrupt Vector Address (IVA) */
258#define SAB82532_IVA_MASK 0xf1
259
260/* Interrupt Port Configuration (IPC) */
261#define SAB82532_IPC_VIS 0x80
262#define SAB82532_IPC_SLA1 0x10
263#define SAB82532_IPC_SLA0 0x08
264#define SAB82532_IPC_CASM 0x04
265#define SAB82532_IPC_IC_OPEN_DRAIN 0x00
266#define SAB82532_IPC_IC_ACT_LOW 0x01
267#define SAB82532_IPC_IC_ACT_HIGH 0x03
268
269/* Interrupt Status Register 0 (ISR0) */
270#define SAB82532_ISR0_TCD 0x80
271#define SAB82532_ISR0_TIME 0x40
272#define SAB82532_ISR0_PERR 0x20
273#define SAB82532_ISR0_FERR 0x10
274#define SAB82532_ISR0_PLLA 0x08
275#define SAB82532_ISR0_CDSC 0x04
276#define SAB82532_ISR0_RFO 0x02
277#define SAB82532_ISR0_RPF 0x01
278
279/* Interrupt Status Register 1 (ISR1) */
280#define SAB82532_ISR1_BRK 0x80
281#define SAB82532_ISR1_BRKT 0x40
282#define SAB82532_ISR1_ALLS 0x20
283#define SAB82532_ISR1_XOFF 0x10
284#define SAB82532_ISR1_TIN 0x08
285#define SAB82532_ISR1_CSC 0x04
286#define SAB82532_ISR1_XON 0x02
287#define SAB82532_ISR1_XPR 0x01
288
289/* Interrupt Mask Register 0 (IMR0) */
290#define SAB82532_IMR0_TCD 0x80
291#define SAB82532_IMR0_TIME 0x40
292#define SAB82532_IMR0_PERR 0x20
293#define SAB82532_IMR0_FERR 0x10
294#define SAB82532_IMR0_PLLA 0x08
295#define SAB82532_IMR0_CDSC 0x04
296#define SAB82532_IMR0_RFO 0x02
297#define SAB82532_IMR0_RPF 0x01
298
299/* Interrupt Mask Register 1 (IMR1) */
300#define SAB82532_IMR1_BRK 0x80
301#define SAB82532_IMR1_BRKT 0x40
302#define SAB82532_IMR1_ALLS 0x20
303#define SAB82532_IMR1_XOFF 0x10
304#define SAB82532_IMR1_TIN 0x08
305#define SAB82532_IMR1_CSC 0x04
306#define SAB82532_IMR1_XON 0x02
307#define SAB82532_IMR1_XPR 0x01
308
309/* Port Interrupt Status Register (PIS) */
310#define SAB82532_PIS_SYNC_B 0x08
311#define SAB82532_PIS_DTR_B 0x04
312#define SAB82532_PIS_DTR_A 0x02
313#define SAB82532_PIS_SYNC_A 0x01
314
315/* Channel Configuration Register 4 (CCR4) */
316#define SAB82532_CCR4_MCK4 0x80
317#define SAB82532_CCR4_EBRG 0x40
318#define SAB82532_CCR4_TST1 0x20
319#define SAB82532_CCR4_ICD 0x10
320
321
322#endif /* !(_SUNSAB_H) */
diff --git a/drivers/tty/serial/sunsu.c b/drivers/tty/serial/sunsu.c
new file mode 100644
index 000000000000..92aa54550e84
--- /dev/null
+++ b/drivers/tty/serial/sunsu.c
@@ -0,0 +1,1608 @@
1/*
2 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
3 *
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998-1999 Pete Zaitcev (zaitcev@yahoo.com)
6 *
7 * This is mainly a variation of 8250.c, credits go to authors mentioned
8 * therein. In fact this driver should be merged into the generic 8250.c
9 * infrastructure perhaps using a 8250_sparc.c module.
10 *
11 * Fixed to use tty_get_baud_rate().
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 *
14 * Converted to new 2.5.x UART layer.
15 * David S. Miller (davem@davemloft.net), 2002-Jul-29
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/spinlock.h>
21#include <linux/errno.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/major.h>
25#include <linux/string.h>
26#include <linux/ptrace.h>
27#include <linux/ioport.h>
28#include <linux/circ_buf.h>
29#include <linux/serial.h>
30#include <linux/sysrq.h>
31#include <linux/console.h>
32#include <linux/slab.h>
33#ifdef CONFIG_SERIO
34#include <linux/serio.h>
35#endif
36#include <linux/serial_reg.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/of_device.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43#include <asm/prom.h>
44
45#if defined(CONFIG_SERIAL_SUNSU_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
46#define SUPPORT_SYSRQ
47#endif
48
49#include <linux/serial_core.h>
50
51#include "suncore.h"
52
53/* We are on a NS PC87303 clocked with 24.0 MHz, which results
54 * in a UART clock of 1.8462 MHz.
55 */
56#define SU_BASE_BAUD (1846200 / 16)
57
58enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
59static char *su_typev[] = { "su(???)", "su(mouse)", "su(kbd)", "su(serial)" };
60
61/*
62 * Here we define the default xmit fifo size used for each type of UART.
63 */
64static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
65 { "unknown", 1, 0 },
66 { "8250", 1, 0 },
67 { "16450", 1, 0 },
68 { "16550", 1, 0 },
69 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
70 { "Cirrus", 1, 0 },
71 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
72 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
73 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO },
74 { "Startech", 1, 0 },
75 { "16C950/954", 128, UART_CLEAR_FIFO | UART_USE_FIFO },
76 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
77 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
78 { "RSA", 2048, UART_CLEAR_FIFO | UART_USE_FIFO }
79};
80
81struct uart_sunsu_port {
82 struct uart_port port;
83 unsigned char acr;
84 unsigned char ier;
85 unsigned short rev;
86 unsigned char lcr;
87 unsigned int lsr_break_flag;
88 unsigned int cflag;
89
90 /* Probing information. */
91 enum su_type su_type;
92 unsigned int type_probed; /* XXX Stupid */
93 unsigned long reg_size;
94
95#ifdef CONFIG_SERIO
96 struct serio serio;
97 int serio_open;
98#endif
99};
100
101static unsigned int serial_in(struct uart_sunsu_port *up, int offset)
102{
103 offset <<= up->port.regshift;
104
105 switch (up->port.iotype) {
106 case UPIO_HUB6:
107 outb(up->port.hub6 - 1 + offset, up->port.iobase);
108 return inb(up->port.iobase + 1);
109
110 case UPIO_MEM:
111 return readb(up->port.membase + offset);
112
113 default:
114 return inb(up->port.iobase + offset);
115 }
116}
117
118static void serial_out(struct uart_sunsu_port *up, int offset, int value)
119{
120#ifndef CONFIG_SPARC64
121 /*
122 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
123 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
124 * gate outputs a logical one. Since we use level triggered interrupts
125 * we have lockup and watchdog reset. We cannot mask IRQ because
126 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
127 * This problem is similar to what Alpha people suffer, see serial.c.
128 */
129 if (offset == UART_MCR)
130 value |= UART_MCR_OUT2;
131#endif
132 offset <<= up->port.regshift;
133
134 switch (up->port.iotype) {
135 case UPIO_HUB6:
136 outb(up->port.hub6 - 1 + offset, up->port.iobase);
137 outb(value, up->port.iobase + 1);
138 break;
139
140 case UPIO_MEM:
141 writeb(value, up->port.membase + offset);
142 break;
143
144 default:
145 outb(value, up->port.iobase + offset);
146 }
147}
148
149/*
150 * We used to support using pause I/O for certain machines. We
151 * haven't supported this for a while, but just in case it's badly
152 * needed for certain old 386 machines, I've left these #define's
153 * in....
154 */
155#define serial_inp(up, offset) serial_in(up, offset)
156#define serial_outp(up, offset, value) serial_out(up, offset, value)
157
158
159/*
160 * For the 16C950
161 */
162static void serial_icr_write(struct uart_sunsu_port *up, int offset, int value)
163{
164 serial_out(up, UART_SCR, offset);
165 serial_out(up, UART_ICR, value);
166}
167
168#if 0 /* Unused currently */
169static unsigned int serial_icr_read(struct uart_sunsu_port *up, int offset)
170{
171 unsigned int value;
172
173 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
174 serial_out(up, UART_SCR, offset);
175 value = serial_in(up, UART_ICR);
176 serial_icr_write(up, UART_ACR, up->acr);
177
178 return value;
179}
180#endif
181
182#ifdef CONFIG_SERIAL_8250_RSA
183/*
184 * Attempts to turn on the RSA FIFO. Returns zero on failure.
185 * We set the port uart clock rate if we succeed.
186 */
187static int __enable_rsa(struct uart_sunsu_port *up)
188{
189 unsigned char mode;
190 int result;
191
192 mode = serial_inp(up, UART_RSA_MSR);
193 result = mode & UART_RSA_MSR_FIFO;
194
195 if (!result) {
196 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
197 mode = serial_inp(up, UART_RSA_MSR);
198 result = mode & UART_RSA_MSR_FIFO;
199 }
200
201 if (result)
202 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
203
204 return result;
205}
206
207static void enable_rsa(struct uart_sunsu_port *up)
208{
209 if (up->port.type == PORT_RSA) {
210 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
211 spin_lock_irq(&up->port.lock);
212 __enable_rsa(up);
213 spin_unlock_irq(&up->port.lock);
214 }
215 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
216 serial_outp(up, UART_RSA_FRR, 0);
217 }
218}
219
220/*
221 * Attempts to turn off the RSA FIFO. Returns zero on failure.
222 * It is unknown why interrupts were disabled in here. However,
223 * the caller is expected to preserve this behaviour by grabbing
224 * the spinlock before calling this function.
225 */
226static void disable_rsa(struct uart_sunsu_port *up)
227{
228 unsigned char mode;
229 int result;
230
231 if (up->port.type == PORT_RSA &&
232 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
233 spin_lock_irq(&up->port.lock);
234
235 mode = serial_inp(up, UART_RSA_MSR);
236 result = !(mode & UART_RSA_MSR_FIFO);
237
238 if (!result) {
239 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
240 mode = serial_inp(up, UART_RSA_MSR);
241 result = !(mode & UART_RSA_MSR_FIFO);
242 }
243
244 if (result)
245 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
246 spin_unlock_irq(&up->port.lock);
247 }
248}
249#endif /* CONFIG_SERIAL_8250_RSA */
250
251static inline void __stop_tx(struct uart_sunsu_port *p)
252{
253 if (p->ier & UART_IER_THRI) {
254 p->ier &= ~UART_IER_THRI;
255 serial_out(p, UART_IER, p->ier);
256 }
257}
258
259static void sunsu_stop_tx(struct uart_port *port)
260{
261 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
262
263 __stop_tx(up);
264
265 /*
266 * We really want to stop the transmitter from sending.
267 */
268 if (up->port.type == PORT_16C950) {
269 up->acr |= UART_ACR_TXDIS;
270 serial_icr_write(up, UART_ACR, up->acr);
271 }
272}
273
274static void sunsu_start_tx(struct uart_port *port)
275{
276 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
277
278 if (!(up->ier & UART_IER_THRI)) {
279 up->ier |= UART_IER_THRI;
280 serial_out(up, UART_IER, up->ier);
281 }
282
283 /*
284 * Re-enable the transmitter if we disabled it.
285 */
286 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
287 up->acr &= ~UART_ACR_TXDIS;
288 serial_icr_write(up, UART_ACR, up->acr);
289 }
290}
291
292static void sunsu_stop_rx(struct uart_port *port)
293{
294 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
295
296 up->ier &= ~UART_IER_RLSI;
297 up->port.read_status_mask &= ~UART_LSR_DR;
298 serial_out(up, UART_IER, up->ier);
299}
300
301static void sunsu_enable_ms(struct uart_port *port)
302{
303 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
304 unsigned long flags;
305
306 spin_lock_irqsave(&up->port.lock, flags);
307 up->ier |= UART_IER_MSI;
308 serial_out(up, UART_IER, up->ier);
309 spin_unlock_irqrestore(&up->port.lock, flags);
310}
311
312static struct tty_struct *
313receive_chars(struct uart_sunsu_port *up, unsigned char *status)
314{
315 struct tty_struct *tty = up->port.state->port.tty;
316 unsigned char ch, flag;
317 int max_count = 256;
318 int saw_console_brk = 0;
319
320 do {
321 ch = serial_inp(up, UART_RX);
322 flag = TTY_NORMAL;
323 up->port.icount.rx++;
324
325 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
326 UART_LSR_FE | UART_LSR_OE))) {
327 /*
328 * For statistics only
329 */
330 if (*status & UART_LSR_BI) {
331 *status &= ~(UART_LSR_FE | UART_LSR_PE);
332 up->port.icount.brk++;
333 if (up->port.cons != NULL &&
334 up->port.line == up->port.cons->index)
335 saw_console_brk = 1;
336 /*
337 * We do the SysRQ and SAK checking
338 * here because otherwise the break
339 * may get masked by ignore_status_mask
340 * or read_status_mask.
341 */
342 if (uart_handle_break(&up->port))
343 goto ignore_char;
344 } else if (*status & UART_LSR_PE)
345 up->port.icount.parity++;
346 else if (*status & UART_LSR_FE)
347 up->port.icount.frame++;
348 if (*status & UART_LSR_OE)
349 up->port.icount.overrun++;
350
351 /*
352 * Mask off conditions which should be ingored.
353 */
354 *status &= up->port.read_status_mask;
355
356 if (up->port.cons != NULL &&
357 up->port.line == up->port.cons->index) {
358 /* Recover the break flag from console xmit */
359 *status |= up->lsr_break_flag;
360 up->lsr_break_flag = 0;
361 }
362
363 if (*status & UART_LSR_BI) {
364 flag = TTY_BREAK;
365 } else if (*status & UART_LSR_PE)
366 flag = TTY_PARITY;
367 else if (*status & UART_LSR_FE)
368 flag = TTY_FRAME;
369 }
370 if (uart_handle_sysrq_char(&up->port, ch))
371 goto ignore_char;
372 if ((*status & up->port.ignore_status_mask) == 0)
373 tty_insert_flip_char(tty, ch, flag);
374 if (*status & UART_LSR_OE)
375 /*
376 * Overrun is special, since it's reported
377 * immediately, and doesn't affect the current
378 * character.
379 */
380 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
381 ignore_char:
382 *status = serial_inp(up, UART_LSR);
383 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
384
385 if (saw_console_brk)
386 sun_do_break();
387
388 return tty;
389}
390
391static void transmit_chars(struct uart_sunsu_port *up)
392{
393 struct circ_buf *xmit = &up->port.state->xmit;
394 int count;
395
396 if (up->port.x_char) {
397 serial_outp(up, UART_TX, up->port.x_char);
398 up->port.icount.tx++;
399 up->port.x_char = 0;
400 return;
401 }
402 if (uart_tx_stopped(&up->port)) {
403 sunsu_stop_tx(&up->port);
404 return;
405 }
406 if (uart_circ_empty(xmit)) {
407 __stop_tx(up);
408 return;
409 }
410
411 count = up->port.fifosize;
412 do {
413 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
414 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
415 up->port.icount.tx++;
416 if (uart_circ_empty(xmit))
417 break;
418 } while (--count > 0);
419
420 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
421 uart_write_wakeup(&up->port);
422
423 if (uart_circ_empty(xmit))
424 __stop_tx(up);
425}
426
427static void check_modem_status(struct uart_sunsu_port *up)
428{
429 int status;
430
431 status = serial_in(up, UART_MSR);
432
433 if ((status & UART_MSR_ANY_DELTA) == 0)
434 return;
435
436 if (status & UART_MSR_TERI)
437 up->port.icount.rng++;
438 if (status & UART_MSR_DDSR)
439 up->port.icount.dsr++;
440 if (status & UART_MSR_DDCD)
441 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
442 if (status & UART_MSR_DCTS)
443 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
444
445 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
446}
447
448static irqreturn_t sunsu_serial_interrupt(int irq, void *dev_id)
449{
450 struct uart_sunsu_port *up = dev_id;
451 unsigned long flags;
452 unsigned char status;
453
454 spin_lock_irqsave(&up->port.lock, flags);
455
456 do {
457 struct tty_struct *tty;
458
459 status = serial_inp(up, UART_LSR);
460 tty = NULL;
461 if (status & UART_LSR_DR)
462 tty = receive_chars(up, &status);
463 check_modem_status(up);
464 if (status & UART_LSR_THRE)
465 transmit_chars(up);
466
467 spin_unlock_irqrestore(&up->port.lock, flags);
468
469 if (tty)
470 tty_flip_buffer_push(tty);
471
472 spin_lock_irqsave(&up->port.lock, flags);
473
474 } while (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT));
475
476 spin_unlock_irqrestore(&up->port.lock, flags);
477
478 return IRQ_HANDLED;
479}
480
481/* Separate interrupt handling path for keyboard/mouse ports. */
482
483static void
484sunsu_change_speed(struct uart_port *port, unsigned int cflag,
485 unsigned int iflag, unsigned int quot);
486
487static void sunsu_change_mouse_baud(struct uart_sunsu_port *up)
488{
489 unsigned int cur_cflag = up->cflag;
490 int quot, new_baud;
491
492 up->cflag &= ~CBAUD;
493 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
494
495 quot = up->port.uartclk / (16 * new_baud);
496
497 sunsu_change_speed(&up->port, up->cflag, 0, quot);
498}
499
500static void receive_kbd_ms_chars(struct uart_sunsu_port *up, int is_break)
501{
502 do {
503 unsigned char ch = serial_inp(up, UART_RX);
504
505 /* Stop-A is handled by drivers/char/keyboard.c now. */
506 if (up->su_type == SU_PORT_KBD) {
507#ifdef CONFIG_SERIO
508 serio_interrupt(&up->serio, ch, 0);
509#endif
510 } else if (up->su_type == SU_PORT_MS) {
511 int ret = suncore_mouse_baud_detection(ch, is_break);
512
513 switch (ret) {
514 case 2:
515 sunsu_change_mouse_baud(up);
516 /* fallthru */
517 case 1:
518 break;
519
520 case 0:
521#ifdef CONFIG_SERIO
522 serio_interrupt(&up->serio, ch, 0);
523#endif
524 break;
525 };
526 }
527 } while (serial_in(up, UART_LSR) & UART_LSR_DR);
528}
529
530static irqreturn_t sunsu_kbd_ms_interrupt(int irq, void *dev_id)
531{
532 struct uart_sunsu_port *up = dev_id;
533
534 if (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT)) {
535 unsigned char status = serial_inp(up, UART_LSR);
536
537 if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
538 receive_kbd_ms_chars(up, (status & UART_LSR_BI) != 0);
539 }
540
541 return IRQ_HANDLED;
542}
543
544static unsigned int sunsu_tx_empty(struct uart_port *port)
545{
546 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
547 unsigned long flags;
548 unsigned int ret;
549
550 spin_lock_irqsave(&up->port.lock, flags);
551 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
552 spin_unlock_irqrestore(&up->port.lock, flags);
553
554 return ret;
555}
556
557static unsigned int sunsu_get_mctrl(struct uart_port *port)
558{
559 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
560 unsigned char status;
561 unsigned int ret;
562
563 status = serial_in(up, UART_MSR);
564
565 ret = 0;
566 if (status & UART_MSR_DCD)
567 ret |= TIOCM_CAR;
568 if (status & UART_MSR_RI)
569 ret |= TIOCM_RNG;
570 if (status & UART_MSR_DSR)
571 ret |= TIOCM_DSR;
572 if (status & UART_MSR_CTS)
573 ret |= TIOCM_CTS;
574 return ret;
575}
576
577static void sunsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
578{
579 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
580 unsigned char mcr = 0;
581
582 if (mctrl & TIOCM_RTS)
583 mcr |= UART_MCR_RTS;
584 if (mctrl & TIOCM_DTR)
585 mcr |= UART_MCR_DTR;
586 if (mctrl & TIOCM_OUT1)
587 mcr |= UART_MCR_OUT1;
588 if (mctrl & TIOCM_OUT2)
589 mcr |= UART_MCR_OUT2;
590 if (mctrl & TIOCM_LOOP)
591 mcr |= UART_MCR_LOOP;
592
593 serial_out(up, UART_MCR, mcr);
594}
595
596static void sunsu_break_ctl(struct uart_port *port, int break_state)
597{
598 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
599 unsigned long flags;
600
601 spin_lock_irqsave(&up->port.lock, flags);
602 if (break_state == -1)
603 up->lcr |= UART_LCR_SBC;
604 else
605 up->lcr &= ~UART_LCR_SBC;
606 serial_out(up, UART_LCR, up->lcr);
607 spin_unlock_irqrestore(&up->port.lock, flags);
608}
609
610static int sunsu_startup(struct uart_port *port)
611{
612 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
613 unsigned long flags;
614 int retval;
615
616 if (up->port.type == PORT_16C950) {
617 /* Wake up and initialize UART */
618 up->acr = 0;
619 serial_outp(up, UART_LCR, 0xBF);
620 serial_outp(up, UART_EFR, UART_EFR_ECB);
621 serial_outp(up, UART_IER, 0);
622 serial_outp(up, UART_LCR, 0);
623 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
624 serial_outp(up, UART_LCR, 0xBF);
625 serial_outp(up, UART_EFR, UART_EFR_ECB);
626 serial_outp(up, UART_LCR, 0);
627 }
628
629#ifdef CONFIG_SERIAL_8250_RSA
630 /*
631 * If this is an RSA port, see if we can kick it up to the
632 * higher speed clock.
633 */
634 enable_rsa(up);
635#endif
636
637 /*
638 * Clear the FIFO buffers and disable them.
639 * (they will be reenabled in set_termios())
640 */
641 if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
642 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
643 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
644 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
645 serial_outp(up, UART_FCR, 0);
646 }
647
648 /*
649 * Clear the interrupt registers.
650 */
651 (void) serial_inp(up, UART_LSR);
652 (void) serial_inp(up, UART_RX);
653 (void) serial_inp(up, UART_IIR);
654 (void) serial_inp(up, UART_MSR);
655
656 /*
657 * At this point, there's no way the LSR could still be 0xff;
658 * if it is, then bail out, because there's likely no UART
659 * here.
660 */
661 if (!(up->port.flags & UPF_BUGGY_UART) &&
662 (serial_inp(up, UART_LSR) == 0xff)) {
663 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
664 return -ENODEV;
665 }
666
667 if (up->su_type != SU_PORT_PORT) {
668 retval = request_irq(up->port.irq, sunsu_kbd_ms_interrupt,
669 IRQF_SHARED, su_typev[up->su_type], up);
670 } else {
671 retval = request_irq(up->port.irq, sunsu_serial_interrupt,
672 IRQF_SHARED, su_typev[up->su_type], up);
673 }
674 if (retval) {
675 printk("su: Cannot register IRQ %d\n", up->port.irq);
676 return retval;
677 }
678
679 /*
680 * Now, initialize the UART
681 */
682 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
683
684 spin_lock_irqsave(&up->port.lock, flags);
685
686 up->port.mctrl |= TIOCM_OUT2;
687
688 sunsu_set_mctrl(&up->port, up->port.mctrl);
689 spin_unlock_irqrestore(&up->port.lock, flags);
690
691 /*
692 * Finally, enable interrupts. Note: Modem status interrupts
693 * are set via set_termios(), which will be occurring imminently
694 * anyway, so we don't enable them here.
695 */
696 up->ier = UART_IER_RLSI | UART_IER_RDI;
697 serial_outp(up, UART_IER, up->ier);
698
699 if (up->port.flags & UPF_FOURPORT) {
700 unsigned int icp;
701 /*
702 * Enable interrupts on the AST Fourport board
703 */
704 icp = (up->port.iobase & 0xfe0) | 0x01f;
705 outb_p(0x80, icp);
706 (void) inb_p(icp);
707 }
708
709 /*
710 * And clear the interrupt registers again for luck.
711 */
712 (void) serial_inp(up, UART_LSR);
713 (void) serial_inp(up, UART_RX);
714 (void) serial_inp(up, UART_IIR);
715 (void) serial_inp(up, UART_MSR);
716
717 return 0;
718}
719
720static void sunsu_shutdown(struct uart_port *port)
721{
722 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
723 unsigned long flags;
724
725 /*
726 * Disable interrupts from this port
727 */
728 up->ier = 0;
729 serial_outp(up, UART_IER, 0);
730
731 spin_lock_irqsave(&up->port.lock, flags);
732 if (up->port.flags & UPF_FOURPORT) {
733 /* reset interrupts on the AST Fourport board */
734 inb((up->port.iobase & 0xfe0) | 0x1f);
735 up->port.mctrl |= TIOCM_OUT1;
736 } else
737 up->port.mctrl &= ~TIOCM_OUT2;
738
739 sunsu_set_mctrl(&up->port, up->port.mctrl);
740 spin_unlock_irqrestore(&up->port.lock, flags);
741
742 /*
743 * Disable break condition and FIFOs
744 */
745 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
746 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
747 UART_FCR_CLEAR_RCVR |
748 UART_FCR_CLEAR_XMIT);
749 serial_outp(up, UART_FCR, 0);
750
751#ifdef CONFIG_SERIAL_8250_RSA
752 /*
753 * Reset the RSA board back to 115kbps compat mode.
754 */
755 disable_rsa(up);
756#endif
757
758 /*
759 * Read data port to reset things.
760 */
761 (void) serial_in(up, UART_RX);
762
763 free_irq(up->port.irq, up);
764}
765
766static void
767sunsu_change_speed(struct uart_port *port, unsigned int cflag,
768 unsigned int iflag, unsigned int quot)
769{
770 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
771 unsigned char cval, fcr = 0;
772 unsigned long flags;
773
774 switch (cflag & CSIZE) {
775 case CS5:
776 cval = 0x00;
777 break;
778 case CS6:
779 cval = 0x01;
780 break;
781 case CS7:
782 cval = 0x02;
783 break;
784 default:
785 case CS8:
786 cval = 0x03;
787 break;
788 }
789
790 if (cflag & CSTOPB)
791 cval |= 0x04;
792 if (cflag & PARENB)
793 cval |= UART_LCR_PARITY;
794 if (!(cflag & PARODD))
795 cval |= UART_LCR_EPAR;
796#ifdef CMSPAR
797 if (cflag & CMSPAR)
798 cval |= UART_LCR_SPAR;
799#endif
800
801 /*
802 * Work around a bug in the Oxford Semiconductor 952 rev B
803 * chip which causes it to seriously miscalculate baud rates
804 * when DLL is 0.
805 */
806 if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
807 up->rev == 0x5201)
808 quot ++;
809
810 if (uart_config[up->port.type].flags & UART_USE_FIFO) {
811 if ((up->port.uartclk / quot) < (2400 * 16))
812 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
813#ifdef CONFIG_SERIAL_8250_RSA
814 else if (up->port.type == PORT_RSA)
815 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
816#endif
817 else
818 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
819 }
820 if (up->port.type == PORT_16750)
821 fcr |= UART_FCR7_64BYTE;
822
823 /*
824 * Ok, we're now changing the port state. Do it with
825 * interrupts disabled.
826 */
827 spin_lock_irqsave(&up->port.lock, flags);
828
829 /*
830 * Update the per-port timeout.
831 */
832 uart_update_timeout(port, cflag, (port->uartclk / (16 * quot)));
833
834 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
835 if (iflag & INPCK)
836 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
837 if (iflag & (BRKINT | PARMRK))
838 up->port.read_status_mask |= UART_LSR_BI;
839
840 /*
841 * Characteres to ignore
842 */
843 up->port.ignore_status_mask = 0;
844 if (iflag & IGNPAR)
845 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
846 if (iflag & IGNBRK) {
847 up->port.ignore_status_mask |= UART_LSR_BI;
848 /*
849 * If we're ignoring parity and break indicators,
850 * ignore overruns too (for real raw support).
851 */
852 if (iflag & IGNPAR)
853 up->port.ignore_status_mask |= UART_LSR_OE;
854 }
855
856 /*
857 * ignore all characters if CREAD is not set
858 */
859 if ((cflag & CREAD) == 0)
860 up->port.ignore_status_mask |= UART_LSR_DR;
861
862 /*
863 * CTS flow control flag and modem status interrupts
864 */
865 up->ier &= ~UART_IER_MSI;
866 if (UART_ENABLE_MS(&up->port, cflag))
867 up->ier |= UART_IER_MSI;
868
869 serial_out(up, UART_IER, up->ier);
870
871 if (uart_config[up->port.type].flags & UART_STARTECH) {
872 serial_outp(up, UART_LCR, 0xBF);
873 serial_outp(up, UART_EFR, cflag & CRTSCTS ? UART_EFR_CTS :0);
874 }
875 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
876 serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
877 serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
878 if (up->port.type == PORT_16750)
879 serial_outp(up, UART_FCR, fcr); /* set fcr */
880 serial_outp(up, UART_LCR, cval); /* reset DLAB */
881 up->lcr = cval; /* Save LCR */
882 if (up->port.type != PORT_16750) {
883 if (fcr & UART_FCR_ENABLE_FIFO) {
884 /* emulated UARTs (Lucent Venus 167x) need two steps */
885 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
886 }
887 serial_outp(up, UART_FCR, fcr); /* set fcr */
888 }
889
890 up->cflag = cflag;
891
892 spin_unlock_irqrestore(&up->port.lock, flags);
893}
894
895static void
896sunsu_set_termios(struct uart_port *port, struct ktermios *termios,
897 struct ktermios *old)
898{
899 unsigned int baud, quot;
900
901 /*
902 * Ask the core to calculate the divisor for us.
903 */
904 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
905 quot = uart_get_divisor(port, baud);
906
907 sunsu_change_speed(port, termios->c_cflag, termios->c_iflag, quot);
908}
909
910static void sunsu_release_port(struct uart_port *port)
911{
912}
913
914static int sunsu_request_port(struct uart_port *port)
915{
916 return 0;
917}
918
919static void sunsu_config_port(struct uart_port *port, int flags)
920{
921 struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
922
923 if (flags & UART_CONFIG_TYPE) {
924 /*
925 * We are supposed to call autoconfig here, but this requires
926 * splitting all the OBP probing crap from the UART probing.
927 * We'll do it when we kill sunsu.c altogether.
928 */
929 port->type = up->type_probed; /* XXX */
930 }
931}
932
933static int
934sunsu_verify_port(struct uart_port *port, struct serial_struct *ser)
935{
936 return -EINVAL;
937}
938
939static const char *
940sunsu_type(struct uart_port *port)
941{
942 int type = port->type;
943
944 if (type >= ARRAY_SIZE(uart_config))
945 type = 0;
946 return uart_config[type].name;
947}
948
949static struct uart_ops sunsu_pops = {
950 .tx_empty = sunsu_tx_empty,
951 .set_mctrl = sunsu_set_mctrl,
952 .get_mctrl = sunsu_get_mctrl,
953 .stop_tx = sunsu_stop_tx,
954 .start_tx = sunsu_start_tx,
955 .stop_rx = sunsu_stop_rx,
956 .enable_ms = sunsu_enable_ms,
957 .break_ctl = sunsu_break_ctl,
958 .startup = sunsu_startup,
959 .shutdown = sunsu_shutdown,
960 .set_termios = sunsu_set_termios,
961 .type = sunsu_type,
962 .release_port = sunsu_release_port,
963 .request_port = sunsu_request_port,
964 .config_port = sunsu_config_port,
965 .verify_port = sunsu_verify_port,
966};
967
968#define UART_NR 4
969
970static struct uart_sunsu_port sunsu_ports[UART_NR];
971
972#ifdef CONFIG_SERIO
973
974static DEFINE_SPINLOCK(sunsu_serio_lock);
975
976static int sunsu_serio_write(struct serio *serio, unsigned char ch)
977{
978 struct uart_sunsu_port *up = serio->port_data;
979 unsigned long flags;
980 int lsr;
981
982 spin_lock_irqsave(&sunsu_serio_lock, flags);
983
984 do {
985 lsr = serial_in(up, UART_LSR);
986 } while (!(lsr & UART_LSR_THRE));
987
988 /* Send the character out. */
989 serial_out(up, UART_TX, ch);
990
991 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
992
993 return 0;
994}
995
996static int sunsu_serio_open(struct serio *serio)
997{
998 struct uart_sunsu_port *up = serio->port_data;
999 unsigned long flags;
1000 int ret;
1001
1002 spin_lock_irqsave(&sunsu_serio_lock, flags);
1003 if (!up->serio_open) {
1004 up->serio_open = 1;
1005 ret = 0;
1006 } else
1007 ret = -EBUSY;
1008 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1009
1010 return ret;
1011}
1012
1013static void sunsu_serio_close(struct serio *serio)
1014{
1015 struct uart_sunsu_port *up = serio->port_data;
1016 unsigned long flags;
1017
1018 spin_lock_irqsave(&sunsu_serio_lock, flags);
1019 up->serio_open = 0;
1020 spin_unlock_irqrestore(&sunsu_serio_lock, flags);
1021}
1022
1023#endif /* CONFIG_SERIO */
1024
1025static void sunsu_autoconfig(struct uart_sunsu_port *up)
1026{
1027 unsigned char status1, status2, scratch, scratch2, scratch3;
1028 unsigned char save_lcr, save_mcr;
1029 unsigned long flags;
1030
1031 if (up->su_type == SU_PORT_NONE)
1032 return;
1033
1034 up->type_probed = PORT_UNKNOWN;
1035 up->port.iotype = UPIO_MEM;
1036
1037 spin_lock_irqsave(&up->port.lock, flags);
1038
1039 if (!(up->port.flags & UPF_BUGGY_UART)) {
1040 /*
1041 * Do a simple existence test first; if we fail this, there's
1042 * no point trying anything else.
1043 *
1044 * 0x80 is used as a nonsense port to prevent against false
1045 * positives due to ISA bus float. The assumption is that
1046 * 0x80 is a non-existent port; which should be safe since
1047 * include/asm/io.h also makes this assumption.
1048 */
1049 scratch = serial_inp(up, UART_IER);
1050 serial_outp(up, UART_IER, 0);
1051#ifdef __i386__
1052 outb(0xff, 0x080);
1053#endif
1054 scratch2 = serial_inp(up, UART_IER);
1055 serial_outp(up, UART_IER, 0x0f);
1056#ifdef __i386__
1057 outb(0, 0x080);
1058#endif
1059 scratch3 = serial_inp(up, UART_IER);
1060 serial_outp(up, UART_IER, scratch);
1061 if (scratch2 != 0 || scratch3 != 0x0F)
1062 goto out; /* We failed; there's nothing here */
1063 }
1064
1065 save_mcr = serial_in(up, UART_MCR);
1066 save_lcr = serial_in(up, UART_LCR);
1067
1068 /*
1069 * Check to see if a UART is really there. Certain broken
1070 * internal modems based on the Rockwell chipset fail this
1071 * test, because they apparently don't implement the loopback
1072 * test mode. So this test is skipped on the COM 1 through
1073 * COM 4 ports. This *should* be safe, since no board
1074 * manufacturer would be stupid enough to design a board
1075 * that conflicts with COM 1-4 --- we hope!
1076 */
1077 if (!(up->port.flags & UPF_SKIP_TEST)) {
1078 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1079 status1 = serial_inp(up, UART_MSR) & 0xF0;
1080 serial_outp(up, UART_MCR, save_mcr);
1081 if (status1 != 0x90)
1082 goto out; /* We failed loopback test */
1083 }
1084 serial_outp(up, UART_LCR, 0xBF); /* set up for StarTech test */
1085 serial_outp(up, UART_EFR, 0); /* EFR is the same as FCR */
1086 serial_outp(up, UART_LCR, 0);
1087 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1088 scratch = serial_in(up, UART_IIR) >> 6;
1089 switch (scratch) {
1090 case 0:
1091 up->port.type = PORT_16450;
1092 break;
1093 case 1:
1094 up->port.type = PORT_UNKNOWN;
1095 break;
1096 case 2:
1097 up->port.type = PORT_16550;
1098 break;
1099 case 3:
1100 up->port.type = PORT_16550A;
1101 break;
1102 }
1103 if (up->port.type == PORT_16550A) {
1104 /* Check for Startech UART's */
1105 serial_outp(up, UART_LCR, UART_LCR_DLAB);
1106 if (serial_in(up, UART_EFR) == 0) {
1107 up->port.type = PORT_16650;
1108 } else {
1109 serial_outp(up, UART_LCR, 0xBF);
1110 if (serial_in(up, UART_EFR) == 0)
1111 up->port.type = PORT_16650V2;
1112 }
1113 }
1114 if (up->port.type == PORT_16550A) {
1115 /* Check for TI 16750 */
1116 serial_outp(up, UART_LCR, save_lcr | UART_LCR_DLAB);
1117 serial_outp(up, UART_FCR,
1118 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1119 scratch = serial_in(up, UART_IIR) >> 5;
1120 if (scratch == 7) {
1121 /*
1122 * If this is a 16750, and not a cheap UART
1123 * clone, then it should only go into 64 byte
1124 * mode if the UART_FCR7_64BYTE bit was set
1125 * while UART_LCR_DLAB was latched.
1126 */
1127 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1128 serial_outp(up, UART_LCR, 0);
1129 serial_outp(up, UART_FCR,
1130 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1131 scratch = serial_in(up, UART_IIR) >> 5;
1132 if (scratch == 6)
1133 up->port.type = PORT_16750;
1134 }
1135 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1136 }
1137 serial_outp(up, UART_LCR, save_lcr);
1138 if (up->port.type == PORT_16450) {
1139 scratch = serial_in(up, UART_SCR);
1140 serial_outp(up, UART_SCR, 0xa5);
1141 status1 = serial_in(up, UART_SCR);
1142 serial_outp(up, UART_SCR, 0x5a);
1143 status2 = serial_in(up, UART_SCR);
1144 serial_outp(up, UART_SCR, scratch);
1145
1146 if ((status1 != 0xa5) || (status2 != 0x5a))
1147 up->port.type = PORT_8250;
1148 }
1149
1150 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
1151
1152 if (up->port.type == PORT_UNKNOWN)
1153 goto out;
1154 up->type_probed = up->port.type; /* XXX */
1155
1156 /*
1157 * Reset the UART.
1158 */
1159#ifdef CONFIG_SERIAL_8250_RSA
1160 if (up->port.type == PORT_RSA)
1161 serial_outp(up, UART_RSA_FRR, 0);
1162#endif
1163 serial_outp(up, UART_MCR, save_mcr);
1164 serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
1165 UART_FCR_CLEAR_RCVR |
1166 UART_FCR_CLEAR_XMIT));
1167 serial_outp(up, UART_FCR, 0);
1168 (void)serial_in(up, UART_RX);
1169 serial_outp(up, UART_IER, 0);
1170
1171out:
1172 spin_unlock_irqrestore(&up->port.lock, flags);
1173}
1174
1175static struct uart_driver sunsu_reg = {
1176 .owner = THIS_MODULE,
1177 .driver_name = "sunsu",
1178 .dev_name = "ttyS",
1179 .major = TTY_MAJOR,
1180};
1181
1182static int __devinit sunsu_kbd_ms_init(struct uart_sunsu_port *up)
1183{
1184 int quot, baud;
1185#ifdef CONFIG_SERIO
1186 struct serio *serio;
1187#endif
1188
1189 if (up->su_type == SU_PORT_KBD) {
1190 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1191 baud = 1200;
1192 } else {
1193 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1194 baud = 4800;
1195 }
1196 quot = up->port.uartclk / (16 * baud);
1197
1198 sunsu_autoconfig(up);
1199 if (up->port.type == PORT_UNKNOWN)
1200 return -ENODEV;
1201
1202 printk("%s: %s port at %llx, irq %u\n",
1203 up->port.dev->of_node->full_name,
1204 (up->su_type == SU_PORT_KBD) ? "Keyboard" : "Mouse",
1205 (unsigned long long) up->port.mapbase,
1206 up->port.irq);
1207
1208#ifdef CONFIG_SERIO
1209 serio = &up->serio;
1210 serio->port_data = up;
1211
1212 serio->id.type = SERIO_RS232;
1213 if (up->su_type == SU_PORT_KBD) {
1214 serio->id.proto = SERIO_SUNKBD;
1215 strlcpy(serio->name, "sukbd", sizeof(serio->name));
1216 } else {
1217 serio->id.proto = SERIO_SUN;
1218 serio->id.extra = 1;
1219 strlcpy(serio->name, "sums", sizeof(serio->name));
1220 }
1221 strlcpy(serio->phys,
1222 (!(up->port.line & 1) ? "su/serio0" : "su/serio1"),
1223 sizeof(serio->phys));
1224
1225 serio->write = sunsu_serio_write;
1226 serio->open = sunsu_serio_open;
1227 serio->close = sunsu_serio_close;
1228 serio->dev.parent = up->port.dev;
1229
1230 serio_register_port(serio);
1231#endif
1232
1233 sunsu_change_speed(&up->port, up->cflag, 0, quot);
1234
1235 sunsu_startup(&up->port);
1236 return 0;
1237}
1238
1239/*
1240 * ------------------------------------------------------------
1241 * Serial console driver
1242 * ------------------------------------------------------------
1243 */
1244
1245#ifdef CONFIG_SERIAL_SUNSU_CONSOLE
1246
1247#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1248
1249/*
1250 * Wait for transmitter & holding register to empty
1251 */
1252static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
1253{
1254 unsigned int status, tmout = 10000;
1255
1256 /* Wait up to 10ms for the character(s) to be sent. */
1257 do {
1258 status = serial_in(up, UART_LSR);
1259
1260 if (status & UART_LSR_BI)
1261 up->lsr_break_flag = UART_LSR_BI;
1262
1263 if (--tmout == 0)
1264 break;
1265 udelay(1);
1266 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
1267
1268 /* Wait up to 1s for flow control if necessary */
1269 if (up->port.flags & UPF_CONS_FLOW) {
1270 tmout = 1000000;
1271 while (--tmout &&
1272 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1273 udelay(1);
1274 }
1275}
1276
1277static void sunsu_console_putchar(struct uart_port *port, int ch)
1278{
1279 struct uart_sunsu_port *up = (struct uart_sunsu_port *)port;
1280
1281 wait_for_xmitr(up);
1282 serial_out(up, UART_TX, ch);
1283}
1284
1285/*
1286 * Print a string to the serial port trying not to disturb
1287 * any possible real use of the port...
1288 */
1289static void sunsu_console_write(struct console *co, const char *s,
1290 unsigned int count)
1291{
1292 struct uart_sunsu_port *up = &sunsu_ports[co->index];
1293 unsigned long flags;
1294 unsigned int ier;
1295 int locked = 1;
1296
1297 local_irq_save(flags);
1298 if (up->port.sysrq) {
1299 locked = 0;
1300 } else if (oops_in_progress) {
1301 locked = spin_trylock(&up->port.lock);
1302 } else
1303 spin_lock(&up->port.lock);
1304
1305 /*
1306 * First save the UER then disable the interrupts
1307 */
1308 ier = serial_in(up, UART_IER);
1309 serial_out(up, UART_IER, 0);
1310
1311 uart_console_write(&up->port, s, count, sunsu_console_putchar);
1312
1313 /*
1314 * Finally, wait for transmitter to become empty
1315 * and restore the IER
1316 */
1317 wait_for_xmitr(up);
1318 serial_out(up, UART_IER, ier);
1319
1320 if (locked)
1321 spin_unlock(&up->port.lock);
1322 local_irq_restore(flags);
1323}
1324
1325/*
1326 * Setup initial baud/bits/parity. We do two things here:
1327 * - construct a cflag setting for the first su_open()
1328 * - initialize the serial port
1329 * Return non-zero if we didn't find a serial port.
1330 */
1331static int __init sunsu_console_setup(struct console *co, char *options)
1332{
1333 static struct ktermios dummy;
1334 struct ktermios termios;
1335 struct uart_port *port;
1336
1337 printk("Console: ttyS%d (SU)\n",
1338 (sunsu_reg.minor - 64) + co->index);
1339
1340 /*
1341 * Check whether an invalid uart number has been specified, and
1342 * if so, search for the first available port that does have
1343 * console support.
1344 */
1345 if (co->index >= UART_NR)
1346 co->index = 0;
1347 port = &sunsu_ports[co->index].port;
1348
1349 /*
1350 * Temporary fix.
1351 */
1352 spin_lock_init(&port->lock);
1353
1354 /* Get firmware console settings. */
1355 sunserial_console_termios(co, port->dev->of_node);
1356
1357 memset(&termios, 0, sizeof(struct ktermios));
1358 termios.c_cflag = co->cflag;
1359 port->mctrl |= TIOCM_DTR;
1360 port->ops->set_termios(port, &termios, &dummy);
1361
1362 return 0;
1363}
1364
1365static struct console sunsu_console = {
1366 .name = "ttyS",
1367 .write = sunsu_console_write,
1368 .device = uart_console_device,
1369 .setup = sunsu_console_setup,
1370 .flags = CON_PRINTBUFFER,
1371 .index = -1,
1372 .data = &sunsu_reg,
1373};
1374
1375/*
1376 * Register console.
1377 */
1378
1379static inline struct console *SUNSU_CONSOLE(void)
1380{
1381 return &sunsu_console;
1382}
1383#else
1384#define SUNSU_CONSOLE() (NULL)
1385#define sunsu_serial_console_init() do { } while (0)
1386#endif
1387
1388static enum su_type __devinit su_get_type(struct device_node *dp)
1389{
1390 struct device_node *ap = of_find_node_by_path("/aliases");
1391
1392 if (ap) {
1393 const char *keyb = of_get_property(ap, "keyboard", NULL);
1394 const char *ms = of_get_property(ap, "mouse", NULL);
1395
1396 if (keyb) {
1397 if (dp == of_find_node_by_path(keyb))
1398 return SU_PORT_KBD;
1399 }
1400 if (ms) {
1401 if (dp == of_find_node_by_path(ms))
1402 return SU_PORT_MS;
1403 }
1404 }
1405
1406 return SU_PORT_PORT;
1407}
1408
1409static int __devinit su_probe(struct platform_device *op)
1410{
1411 static int inst;
1412 struct device_node *dp = op->dev.of_node;
1413 struct uart_sunsu_port *up;
1414 struct resource *rp;
1415 enum su_type type;
1416 bool ignore_line;
1417 int err;
1418
1419 type = su_get_type(dp);
1420 if (type == SU_PORT_PORT) {
1421 if (inst >= UART_NR)
1422 return -EINVAL;
1423 up = &sunsu_ports[inst];
1424 } else {
1425 up = kzalloc(sizeof(*up), GFP_KERNEL);
1426 if (!up)
1427 return -ENOMEM;
1428 }
1429
1430 up->port.line = inst;
1431
1432 spin_lock_init(&up->port.lock);
1433
1434 up->su_type = type;
1435
1436 rp = &op->resource[0];
1437 up->port.mapbase = rp->start;
1438 up->reg_size = (rp->end - rp->start) + 1;
1439 up->port.membase = of_ioremap(rp, 0, up->reg_size, "su");
1440 if (!up->port.membase) {
1441 if (type != SU_PORT_PORT)
1442 kfree(up);
1443 return -ENOMEM;
1444 }
1445
1446 up->port.irq = op->archdata.irqs[0];
1447
1448 up->port.dev = &op->dev;
1449
1450 up->port.type = PORT_UNKNOWN;
1451 up->port.uartclk = (SU_BASE_BAUD * 16);
1452
1453 err = 0;
1454 if (up->su_type == SU_PORT_KBD || up->su_type == SU_PORT_MS) {
1455 err = sunsu_kbd_ms_init(up);
1456 if (err) {
1457 of_iounmap(&op->resource[0],
1458 up->port.membase, up->reg_size);
1459 kfree(up);
1460 return err;
1461 }
1462 dev_set_drvdata(&op->dev, up);
1463
1464 return 0;
1465 }
1466
1467 up->port.flags |= UPF_BOOT_AUTOCONF;
1468
1469 sunsu_autoconfig(up);
1470
1471 err = -ENODEV;
1472 if (up->port.type == PORT_UNKNOWN)
1473 goto out_unmap;
1474
1475 up->port.ops = &sunsu_pops;
1476
1477 ignore_line = false;
1478 if (!strcmp(dp->name, "rsc-console") ||
1479 !strcmp(dp->name, "lom-console"))
1480 ignore_line = true;
1481
1482 sunserial_console_match(SUNSU_CONSOLE(), dp,
1483 &sunsu_reg, up->port.line,
1484 ignore_line);
1485 err = uart_add_one_port(&sunsu_reg, &up->port);
1486 if (err)
1487 goto out_unmap;
1488
1489 dev_set_drvdata(&op->dev, up);
1490
1491 inst++;
1492
1493 return 0;
1494
1495out_unmap:
1496 of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1497 return err;
1498}
1499
1500static int __devexit su_remove(struct platform_device *op)
1501{
1502 struct uart_sunsu_port *up = dev_get_drvdata(&op->dev);
1503 bool kbdms = false;
1504
1505 if (up->su_type == SU_PORT_MS ||
1506 up->su_type == SU_PORT_KBD)
1507 kbdms = true;
1508
1509 if (kbdms) {
1510#ifdef CONFIG_SERIO
1511 serio_unregister_port(&up->serio);
1512#endif
1513 } else if (up->port.type != PORT_UNKNOWN)
1514 uart_remove_one_port(&sunsu_reg, &up->port);
1515
1516 if (up->port.membase)
1517 of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1518
1519 if (kbdms)
1520 kfree(up);
1521
1522 dev_set_drvdata(&op->dev, NULL);
1523
1524 return 0;
1525}
1526
1527static const struct of_device_id su_match[] = {
1528 {
1529 .name = "su",
1530 },
1531 {
1532 .name = "su_pnp",
1533 },
1534 {
1535 .name = "serial",
1536 .compatible = "su",
1537 },
1538 {
1539 .type = "serial",
1540 .compatible = "su",
1541 },
1542 {},
1543};
1544MODULE_DEVICE_TABLE(of, su_match);
1545
1546static struct platform_driver su_driver = {
1547 .driver = {
1548 .name = "su",
1549 .owner = THIS_MODULE,
1550 .of_match_table = su_match,
1551 },
1552 .probe = su_probe,
1553 .remove = __devexit_p(su_remove),
1554};
1555
1556static int __init sunsu_init(void)
1557{
1558 struct device_node *dp;
1559 int err;
1560 int num_uart = 0;
1561
1562 for_each_node_by_name(dp, "su") {
1563 if (su_get_type(dp) == SU_PORT_PORT)
1564 num_uart++;
1565 }
1566 for_each_node_by_name(dp, "su_pnp") {
1567 if (su_get_type(dp) == SU_PORT_PORT)
1568 num_uart++;
1569 }
1570 for_each_node_by_name(dp, "serial") {
1571 if (of_device_is_compatible(dp, "su")) {
1572 if (su_get_type(dp) == SU_PORT_PORT)
1573 num_uart++;
1574 }
1575 }
1576 for_each_node_by_type(dp, "serial") {
1577 if (of_device_is_compatible(dp, "su")) {
1578 if (su_get_type(dp) == SU_PORT_PORT)
1579 num_uart++;
1580 }
1581 }
1582
1583 if (num_uart) {
1584 err = sunserial_register_minors(&sunsu_reg, num_uart);
1585 if (err)
1586 return err;
1587 }
1588
1589 err = platform_driver_register(&su_driver);
1590 if (err && num_uart)
1591 sunserial_unregister_minors(&sunsu_reg, num_uart);
1592
1593 return err;
1594}
1595
1596static void __exit sunsu_exit(void)
1597{
1598 if (sunsu_reg.nr)
1599 sunserial_unregister_minors(&sunsu_reg, sunsu_reg.nr);
1600}
1601
1602module_init(sunsu_init);
1603module_exit(sunsu_exit);
1604
1605MODULE_AUTHOR("Eddie C. Dost, Peter Zaitcev, and David S. Miller");
1606MODULE_DESCRIPTION("Sun SU serial port driver");
1607MODULE_VERSION("2.0");
1608MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunzilog.c b/drivers/tty/serial/sunzilog.c
new file mode 100644
index 000000000000..8e916e76b7b5
--- /dev/null
+++ b/drivers/tty/serial/sunzilog.c
@@ -0,0 +1,1655 @@
1/* sunzilog.c: Zilog serial driver for Sparc systems.
2 *
3 * Driver for Zilog serial chips found on Sun workstations and
4 * servers. This driver could actually be made more generic.
5 *
6 * This is based on the old drivers/sbus/char/zs.c code. A lot
7 * of code has been simply moved over directly from there but
8 * much has been rewritten. Credits therefore go out to Eddie
9 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
10 * work there.
11 *
12 * Copyright (C) 2002, 2006, 2007 David S. Miller (davem@davemloft.net)
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/major.h>
22#include <linux/string.h>
23#include <linux/ptrace.h>
24#include <linux/ioport.h>
25#include <linux/slab.h>
26#include <linux/circ_buf.h>
27#include <linux/serial.h>
28#include <linux/sysrq.h>
29#include <linux/console.h>
30#include <linux/spinlock.h>
31#ifdef CONFIG_SERIO
32#include <linux/serio.h>
33#endif
34#include <linux/init.h>
35#include <linux/of_device.h>
36
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40
41#if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
42#define SUPPORT_SYSRQ
43#endif
44
45#include <linux/serial_core.h>
46
47#include "suncore.h"
48#include "sunzilog.h"
49
50/* On 32-bit sparcs we need to delay after register accesses
51 * to accommodate sun4 systems, but we do not need to flush writes.
52 * On 64-bit sparc we only need to flush single writes to ensure
53 * completion.
54 */
55#ifndef CONFIG_SPARC64
56#define ZSDELAY() udelay(5)
57#define ZSDELAY_LONG() udelay(20)
58#define ZS_WSYNC(channel) do { } while (0)
59#else
60#define ZSDELAY()
61#define ZSDELAY_LONG()
62#define ZS_WSYNC(__channel) \
63 readb(&((__channel)->control))
64#endif
65
66#define ZS_CLOCK 4915200 /* Zilog input clock rate. */
67#define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
68
69/*
70 * We wrap our port structure around the generic uart_port.
71 */
72struct uart_sunzilog_port {
73 struct uart_port port;
74
75 /* IRQ servicing chain. */
76 struct uart_sunzilog_port *next;
77
78 /* Current values of Zilog write registers. */
79 unsigned char curregs[NUM_ZSREGS];
80
81 unsigned int flags;
82#define SUNZILOG_FLAG_CONS_KEYB 0x00000001
83#define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
84#define SUNZILOG_FLAG_IS_CONS 0x00000004
85#define SUNZILOG_FLAG_IS_KGDB 0x00000008
86#define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
87#define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
88#define SUNZILOG_FLAG_REGS_HELD 0x00000040
89#define SUNZILOG_FLAG_TX_STOPPED 0x00000080
90#define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
91#define SUNZILOG_FLAG_ESCC 0x00000200
92#define SUNZILOG_FLAG_ISR_HANDLER 0x00000400
93
94 unsigned int cflag;
95
96 unsigned char parity_mask;
97 unsigned char prev_status;
98
99#ifdef CONFIG_SERIO
100 struct serio serio;
101 int serio_open;
102#endif
103};
104
105static void sunzilog_putchar(struct uart_port *port, int ch);
106
107#define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
108#define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
109
110#define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
111#define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
112#define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
113#define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
114#define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
115#define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
116#define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
117#define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
118#define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
119
120/* Reading and writing Zilog8530 registers. The delays are to make this
121 * driver work on the Sun4 which needs a settling delay after each chip
122 * register access, other machines handle this in hardware via auxiliary
123 * flip-flops which implement the settle time we do in software.
124 *
125 * The port lock must be held and local IRQs must be disabled
126 * when {read,write}_zsreg is invoked.
127 */
128static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
129 unsigned char reg)
130{
131 unsigned char retval;
132
133 writeb(reg, &channel->control);
134 ZSDELAY();
135 retval = readb(&channel->control);
136 ZSDELAY();
137
138 return retval;
139}
140
141static void write_zsreg(struct zilog_channel __iomem *channel,
142 unsigned char reg, unsigned char value)
143{
144 writeb(reg, &channel->control);
145 ZSDELAY();
146 writeb(value, &channel->control);
147 ZSDELAY();
148}
149
150static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
151{
152 int i;
153
154 for (i = 0; i < 32; i++) {
155 unsigned char regval;
156
157 regval = readb(&channel->control);
158 ZSDELAY();
159 if (regval & Rx_CH_AV)
160 break;
161
162 regval = read_zsreg(channel, R1);
163 readb(&channel->data);
164 ZSDELAY();
165
166 if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
167 writeb(ERR_RES, &channel->control);
168 ZSDELAY();
169 ZS_WSYNC(channel);
170 }
171 }
172}
173
174/* This function must only be called when the TX is not busy. The UART
175 * port lock must be held and local interrupts disabled.
176 */
177static int __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
178{
179 int i;
180 int escc;
181 unsigned char r15;
182
183 /* Let pending transmits finish. */
184 for (i = 0; i < 1000; i++) {
185 unsigned char stat = read_zsreg(channel, R1);
186 if (stat & ALL_SNT)
187 break;
188 udelay(100);
189 }
190
191 writeb(ERR_RES, &channel->control);
192 ZSDELAY();
193 ZS_WSYNC(channel);
194
195 sunzilog_clear_fifo(channel);
196
197 /* Disable all interrupts. */
198 write_zsreg(channel, R1,
199 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
200
201 /* Set parity, sync config, stop bits, and clock divisor. */
202 write_zsreg(channel, R4, regs[R4]);
203
204 /* Set misc. TX/RX control bits. */
205 write_zsreg(channel, R10, regs[R10]);
206
207 /* Set TX/RX controls sans the enable bits. */
208 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
209 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
210
211 /* Synchronous mode config. */
212 write_zsreg(channel, R6, regs[R6]);
213 write_zsreg(channel, R7, regs[R7]);
214
215 /* Don't mess with the interrupt vector (R2, unused by us) and
216 * master interrupt control (R9). We make sure this is setup
217 * properly at probe time then never touch it again.
218 */
219
220 /* Disable baud generator. */
221 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
222
223 /* Clock mode control. */
224 write_zsreg(channel, R11, regs[R11]);
225
226 /* Lower and upper byte of baud rate generator divisor. */
227 write_zsreg(channel, R12, regs[R12]);
228 write_zsreg(channel, R13, regs[R13]);
229
230 /* Now rewrite R14, with BRENAB (if set). */
231 write_zsreg(channel, R14, regs[R14]);
232
233 /* External status interrupt control. */
234 write_zsreg(channel, R15, (regs[R15] | WR7pEN) & ~FIFOEN);
235
236 /* ESCC Extension Register */
237 r15 = read_zsreg(channel, R15);
238 if (r15 & 0x01) {
239 write_zsreg(channel, R7, regs[R7p]);
240
241 /* External status interrupt and FIFO control. */
242 write_zsreg(channel, R15, regs[R15] & ~WR7pEN);
243 escc = 1;
244 } else {
245 /* Clear FIFO bit case it is an issue */
246 regs[R15] &= ~FIFOEN;
247 escc = 0;
248 }
249
250 /* Reset external status interrupts. */
251 write_zsreg(channel, R0, RES_EXT_INT); /* First Latch */
252 write_zsreg(channel, R0, RES_EXT_INT); /* Second Latch */
253
254 /* Rewrite R3/R5, this time without enables masked. */
255 write_zsreg(channel, R3, regs[R3]);
256 write_zsreg(channel, R5, regs[R5]);
257
258 /* Rewrite R1, this time without IRQ enabled masked. */
259 write_zsreg(channel, R1, regs[R1]);
260
261 return escc;
262}
263
264/* Reprogram the Zilog channel HW registers with the copies found in the
265 * software state struct. If the transmitter is busy, we defer this update
266 * until the next TX complete interrupt. Else, we do it right now.
267 *
268 * The UART port lock must be held and local interrupts disabled.
269 */
270static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
271 struct zilog_channel __iomem *channel)
272{
273 if (!ZS_REGS_HELD(up)) {
274 if (ZS_TX_ACTIVE(up)) {
275 up->flags |= SUNZILOG_FLAG_REGS_HELD;
276 } else {
277 __load_zsregs(channel, up->curregs);
278 }
279 }
280}
281
282static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
283{
284 unsigned int cur_cflag = up->cflag;
285 int brg, new_baud;
286
287 up->cflag &= ~CBAUD;
288 up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
289
290 brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
291 up->curregs[R12] = (brg & 0xff);
292 up->curregs[R13] = (brg >> 8) & 0xff;
293 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
294}
295
296static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
297 unsigned char ch, int is_break)
298{
299 if (ZS_IS_KEYB(up)) {
300 /* Stop-A is handled by drivers/char/keyboard.c now. */
301#ifdef CONFIG_SERIO
302 if (up->serio_open)
303 serio_interrupt(&up->serio, ch, 0);
304#endif
305 } else if (ZS_IS_MOUSE(up)) {
306 int ret = suncore_mouse_baud_detection(ch, is_break);
307
308 switch (ret) {
309 case 2:
310 sunzilog_change_mouse_baud(up);
311 /* fallthru */
312 case 1:
313 break;
314
315 case 0:
316#ifdef CONFIG_SERIO
317 if (up->serio_open)
318 serio_interrupt(&up->serio, ch, 0);
319#endif
320 break;
321 };
322 }
323}
324
325static struct tty_struct *
326sunzilog_receive_chars(struct uart_sunzilog_port *up,
327 struct zilog_channel __iomem *channel)
328{
329 struct tty_struct *tty;
330 unsigned char ch, r1, flag;
331
332 tty = NULL;
333 if (up->port.state != NULL && /* Unopened serial console */
334 up->port.state->port.tty != NULL) /* Keyboard || mouse */
335 tty = up->port.state->port.tty;
336
337 for (;;) {
338
339 r1 = read_zsreg(channel, R1);
340 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
341 writeb(ERR_RES, &channel->control);
342 ZSDELAY();
343 ZS_WSYNC(channel);
344 }
345
346 ch = readb(&channel->control);
347 ZSDELAY();
348
349 /* This funny hack depends upon BRK_ABRT not interfering
350 * with the other bits we care about in R1.
351 */
352 if (ch & BRK_ABRT)
353 r1 |= BRK_ABRT;
354
355 if (!(ch & Rx_CH_AV))
356 break;
357
358 ch = readb(&channel->data);
359 ZSDELAY();
360
361 ch &= up->parity_mask;
362
363 if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
364 sunzilog_kbdms_receive_chars(up, ch, 0);
365 continue;
366 }
367
368 if (tty == NULL) {
369 uart_handle_sysrq_char(&up->port, ch);
370 continue;
371 }
372
373 /* A real serial line, record the character and status. */
374 flag = TTY_NORMAL;
375 up->port.icount.rx++;
376 if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
377 if (r1 & BRK_ABRT) {
378 r1 &= ~(PAR_ERR | CRC_ERR);
379 up->port.icount.brk++;
380 if (uart_handle_break(&up->port))
381 continue;
382 }
383 else if (r1 & PAR_ERR)
384 up->port.icount.parity++;
385 else if (r1 & CRC_ERR)
386 up->port.icount.frame++;
387 if (r1 & Rx_OVR)
388 up->port.icount.overrun++;
389 r1 &= up->port.read_status_mask;
390 if (r1 & BRK_ABRT)
391 flag = TTY_BREAK;
392 else if (r1 & PAR_ERR)
393 flag = TTY_PARITY;
394 else if (r1 & CRC_ERR)
395 flag = TTY_FRAME;
396 }
397 if (uart_handle_sysrq_char(&up->port, ch))
398 continue;
399
400 if (up->port.ignore_status_mask == 0xff ||
401 (r1 & up->port.ignore_status_mask) == 0) {
402 tty_insert_flip_char(tty, ch, flag);
403 }
404 if (r1 & Rx_OVR)
405 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
406 }
407
408 return tty;
409}
410
411static void sunzilog_status_handle(struct uart_sunzilog_port *up,
412 struct zilog_channel __iomem *channel)
413{
414 unsigned char status;
415
416 status = readb(&channel->control);
417 ZSDELAY();
418
419 writeb(RES_EXT_INT, &channel->control);
420 ZSDELAY();
421 ZS_WSYNC(channel);
422
423 if (status & BRK_ABRT) {
424 if (ZS_IS_MOUSE(up))
425 sunzilog_kbdms_receive_chars(up, 0, 1);
426 if (ZS_IS_CONS(up)) {
427 /* Wait for BREAK to deassert to avoid potentially
428 * confusing the PROM.
429 */
430 while (1) {
431 status = readb(&channel->control);
432 ZSDELAY();
433 if (!(status & BRK_ABRT))
434 break;
435 }
436 sun_do_break();
437 return;
438 }
439 }
440
441 if (ZS_WANTS_MODEM_STATUS(up)) {
442 if (status & SYNC)
443 up->port.icount.dsr++;
444
445 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
446 * But it does not tell us which bit has changed, we have to keep
447 * track of this ourselves.
448 */
449 if ((status ^ up->prev_status) ^ DCD)
450 uart_handle_dcd_change(&up->port,
451 (status & DCD));
452 if ((status ^ up->prev_status) ^ CTS)
453 uart_handle_cts_change(&up->port,
454 (status & CTS));
455
456 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
457 }
458
459 up->prev_status = status;
460}
461
462static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
463 struct zilog_channel __iomem *channel)
464{
465 struct circ_buf *xmit;
466
467 if (ZS_IS_CONS(up)) {
468 unsigned char status = readb(&channel->control);
469 ZSDELAY();
470
471 /* TX still busy? Just wait for the next TX done interrupt.
472 *
473 * It can occur because of how we do serial console writes. It would
474 * be nice to transmit console writes just like we normally would for
475 * a TTY line. (ie. buffered and TX interrupt driven). That is not
476 * easy because console writes cannot sleep. One solution might be
477 * to poll on enough port->xmit space becoming free. -DaveM
478 */
479 if (!(status & Tx_BUF_EMP))
480 return;
481 }
482
483 up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
484
485 if (ZS_REGS_HELD(up)) {
486 __load_zsregs(channel, up->curregs);
487 up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
488 }
489
490 if (ZS_TX_STOPPED(up)) {
491 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
492 goto ack_tx_int;
493 }
494
495 if (up->port.x_char) {
496 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
497 writeb(up->port.x_char, &channel->data);
498 ZSDELAY();
499 ZS_WSYNC(channel);
500
501 up->port.icount.tx++;
502 up->port.x_char = 0;
503 return;
504 }
505
506 if (up->port.state == NULL)
507 goto ack_tx_int;
508 xmit = &up->port.state->xmit;
509 if (uart_circ_empty(xmit))
510 goto ack_tx_int;
511
512 if (uart_tx_stopped(&up->port))
513 goto ack_tx_int;
514
515 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
516 writeb(xmit->buf[xmit->tail], &channel->data);
517 ZSDELAY();
518 ZS_WSYNC(channel);
519
520 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
521 up->port.icount.tx++;
522
523 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
524 uart_write_wakeup(&up->port);
525
526 return;
527
528ack_tx_int:
529 writeb(RES_Tx_P, &channel->control);
530 ZSDELAY();
531 ZS_WSYNC(channel);
532}
533
534static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
535{
536 struct uart_sunzilog_port *up = dev_id;
537
538 while (up) {
539 struct zilog_channel __iomem *channel
540 = ZILOG_CHANNEL_FROM_PORT(&up->port);
541 struct tty_struct *tty;
542 unsigned char r3;
543
544 spin_lock(&up->port.lock);
545 r3 = read_zsreg(channel, R3);
546
547 /* Channel A */
548 tty = NULL;
549 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
550 writeb(RES_H_IUS, &channel->control);
551 ZSDELAY();
552 ZS_WSYNC(channel);
553
554 if (r3 & CHARxIP)
555 tty = sunzilog_receive_chars(up, channel);
556 if (r3 & CHAEXT)
557 sunzilog_status_handle(up, channel);
558 if (r3 & CHATxIP)
559 sunzilog_transmit_chars(up, channel);
560 }
561 spin_unlock(&up->port.lock);
562
563 if (tty)
564 tty_flip_buffer_push(tty);
565
566 /* Channel B */
567 up = up->next;
568 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
569
570 spin_lock(&up->port.lock);
571 tty = NULL;
572 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
573 writeb(RES_H_IUS, &channel->control);
574 ZSDELAY();
575 ZS_WSYNC(channel);
576
577 if (r3 & CHBRxIP)
578 tty = sunzilog_receive_chars(up, channel);
579 if (r3 & CHBEXT)
580 sunzilog_status_handle(up, channel);
581 if (r3 & CHBTxIP)
582 sunzilog_transmit_chars(up, channel);
583 }
584 spin_unlock(&up->port.lock);
585
586 if (tty)
587 tty_flip_buffer_push(tty);
588
589 up = up->next;
590 }
591
592 return IRQ_HANDLED;
593}
594
595/* A convenient way to quickly get R0 status. The caller must _not_ hold the
596 * port lock, it is acquired here.
597 */
598static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
599{
600 struct zilog_channel __iomem *channel;
601 unsigned char status;
602
603 channel = ZILOG_CHANNEL_FROM_PORT(port);
604 status = readb(&channel->control);
605 ZSDELAY();
606
607 return status;
608}
609
610/* The port lock is not held. */
611static unsigned int sunzilog_tx_empty(struct uart_port *port)
612{
613 unsigned long flags;
614 unsigned char status;
615 unsigned int ret;
616
617 spin_lock_irqsave(&port->lock, flags);
618
619 status = sunzilog_read_channel_status(port);
620
621 spin_unlock_irqrestore(&port->lock, flags);
622
623 if (status & Tx_BUF_EMP)
624 ret = TIOCSER_TEMT;
625 else
626 ret = 0;
627
628 return ret;
629}
630
631/* The port lock is held and interrupts are disabled. */
632static unsigned int sunzilog_get_mctrl(struct uart_port *port)
633{
634 unsigned char status;
635 unsigned int ret;
636
637 status = sunzilog_read_channel_status(port);
638
639 ret = 0;
640 if (status & DCD)
641 ret |= TIOCM_CAR;
642 if (status & SYNC)
643 ret |= TIOCM_DSR;
644 if (status & CTS)
645 ret |= TIOCM_CTS;
646
647 return ret;
648}
649
650/* The port lock is held and interrupts are disabled. */
651static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
652{
653 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
654 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
655 unsigned char set_bits, clear_bits;
656
657 set_bits = clear_bits = 0;
658
659 if (mctrl & TIOCM_RTS)
660 set_bits |= RTS;
661 else
662 clear_bits |= RTS;
663 if (mctrl & TIOCM_DTR)
664 set_bits |= DTR;
665 else
666 clear_bits |= DTR;
667
668 /* NOTE: Not subject to 'transmitter active' rule. */
669 up->curregs[R5] |= set_bits;
670 up->curregs[R5] &= ~clear_bits;
671 write_zsreg(channel, R5, up->curregs[R5]);
672}
673
674/* The port lock is held and interrupts are disabled. */
675static void sunzilog_stop_tx(struct uart_port *port)
676{
677 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
678
679 up->flags |= SUNZILOG_FLAG_TX_STOPPED;
680}
681
682/* The port lock is held and interrupts are disabled. */
683static void sunzilog_start_tx(struct uart_port *port)
684{
685 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
686 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
687 unsigned char status;
688
689 up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
690 up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
691
692 status = readb(&channel->control);
693 ZSDELAY();
694
695 /* TX busy? Just wait for the TX done interrupt. */
696 if (!(status & Tx_BUF_EMP))
697 return;
698
699 /* Send the first character to jump-start the TX done
700 * IRQ sending engine.
701 */
702 if (port->x_char) {
703 writeb(port->x_char, &channel->data);
704 ZSDELAY();
705 ZS_WSYNC(channel);
706
707 port->icount.tx++;
708 port->x_char = 0;
709 } else {
710 struct circ_buf *xmit = &port->state->xmit;
711
712 writeb(xmit->buf[xmit->tail], &channel->data);
713 ZSDELAY();
714 ZS_WSYNC(channel);
715
716 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
717 port->icount.tx++;
718
719 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
720 uart_write_wakeup(&up->port);
721 }
722}
723
724/* The port lock is held. */
725static void sunzilog_stop_rx(struct uart_port *port)
726{
727 struct uart_sunzilog_port *up = UART_ZILOG(port);
728 struct zilog_channel __iomem *channel;
729
730 if (ZS_IS_CONS(up))
731 return;
732
733 channel = ZILOG_CHANNEL_FROM_PORT(port);
734
735 /* Disable all RX interrupts. */
736 up->curregs[R1] &= ~RxINT_MASK;
737 sunzilog_maybe_update_regs(up, channel);
738}
739
740/* The port lock is held. */
741static void sunzilog_enable_ms(struct uart_port *port)
742{
743 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
744 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
745 unsigned char new_reg;
746
747 new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
748 if (new_reg != up->curregs[R15]) {
749 up->curregs[R15] = new_reg;
750
751 /* NOTE: Not subject to 'transmitter active' rule. */
752 write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
753 }
754}
755
756/* The port lock is not held. */
757static void sunzilog_break_ctl(struct uart_port *port, int break_state)
758{
759 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
760 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
761 unsigned char set_bits, clear_bits, new_reg;
762 unsigned long flags;
763
764 set_bits = clear_bits = 0;
765
766 if (break_state)
767 set_bits |= SND_BRK;
768 else
769 clear_bits |= SND_BRK;
770
771 spin_lock_irqsave(&port->lock, flags);
772
773 new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
774 if (new_reg != up->curregs[R5]) {
775 up->curregs[R5] = new_reg;
776
777 /* NOTE: Not subject to 'transmitter active' rule. */
778 write_zsreg(channel, R5, up->curregs[R5]);
779 }
780
781 spin_unlock_irqrestore(&port->lock, flags);
782}
783
784static void __sunzilog_startup(struct uart_sunzilog_port *up)
785{
786 struct zilog_channel __iomem *channel;
787
788 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
789 up->prev_status = readb(&channel->control);
790
791 /* Enable receiver and transmitter. */
792 up->curregs[R3] |= RxENAB;
793 up->curregs[R5] |= TxENAB;
794
795 up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
796 sunzilog_maybe_update_regs(up, channel);
797}
798
799static int sunzilog_startup(struct uart_port *port)
800{
801 struct uart_sunzilog_port *up = UART_ZILOG(port);
802 unsigned long flags;
803
804 if (ZS_IS_CONS(up))
805 return 0;
806
807 spin_lock_irqsave(&port->lock, flags);
808 __sunzilog_startup(up);
809 spin_unlock_irqrestore(&port->lock, flags);
810 return 0;
811}
812
813/*
814 * The test for ZS_IS_CONS is explained by the following e-mail:
815 *****
816 * From: Russell King <rmk@arm.linux.org.uk>
817 * Date: Sun, 8 Dec 2002 10:18:38 +0000
818 *
819 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
820 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
821 * > and I noticed that something is not right with reference
822 * > counting in this case. It seems that when the console
823 * > is open by kernel initially, this is not accounted
824 * > as an open, and uart_startup is not called.
825 *
826 * That is correct. We are unable to call uart_startup when the serial
827 * console is initialised because it may need to allocate memory (as
828 * request_irq does) and the memory allocators may not have been
829 * initialised.
830 *
831 * 1. initialise the port into a state where it can send characters in the
832 * console write method.
833 *
834 * 2. don't do the actual hardware shutdown in your shutdown() method (but
835 * do the normal software shutdown - ie, free irqs etc)
836 *****
837 */
838static void sunzilog_shutdown(struct uart_port *port)
839{
840 struct uart_sunzilog_port *up = UART_ZILOG(port);
841 struct zilog_channel __iomem *channel;
842 unsigned long flags;
843
844 if (ZS_IS_CONS(up))
845 return;
846
847 spin_lock_irqsave(&port->lock, flags);
848
849 channel = ZILOG_CHANNEL_FROM_PORT(port);
850
851 /* Disable receiver and transmitter. */
852 up->curregs[R3] &= ~RxENAB;
853 up->curregs[R5] &= ~TxENAB;
854
855 /* Disable all interrupts and BRK assertion. */
856 up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
857 up->curregs[R5] &= ~SND_BRK;
858 sunzilog_maybe_update_regs(up, channel);
859
860 spin_unlock_irqrestore(&port->lock, flags);
861}
862
863/* Shared by TTY driver and serial console setup. The port lock is held
864 * and local interrupts are disabled.
865 */
866static void
867sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
868 unsigned int iflag, int brg)
869{
870
871 up->curregs[R10] = NRZ;
872 up->curregs[R11] = TCBR | RCBR;
873
874 /* Program BAUD and clock source. */
875 up->curregs[R4] &= ~XCLK_MASK;
876 up->curregs[R4] |= X16CLK;
877 up->curregs[R12] = brg & 0xff;
878 up->curregs[R13] = (brg >> 8) & 0xff;
879 up->curregs[R14] = BRSRC | BRENAB;
880
881 /* Character size, stop bits, and parity. */
882 up->curregs[R3] &= ~RxN_MASK;
883 up->curregs[R5] &= ~TxN_MASK;
884 switch (cflag & CSIZE) {
885 case CS5:
886 up->curregs[R3] |= Rx5;
887 up->curregs[R5] |= Tx5;
888 up->parity_mask = 0x1f;
889 break;
890 case CS6:
891 up->curregs[R3] |= Rx6;
892 up->curregs[R5] |= Tx6;
893 up->parity_mask = 0x3f;
894 break;
895 case CS7:
896 up->curregs[R3] |= Rx7;
897 up->curregs[R5] |= Tx7;
898 up->parity_mask = 0x7f;
899 break;
900 case CS8:
901 default:
902 up->curregs[R3] |= Rx8;
903 up->curregs[R5] |= Tx8;
904 up->parity_mask = 0xff;
905 break;
906 };
907 up->curregs[R4] &= ~0x0c;
908 if (cflag & CSTOPB)
909 up->curregs[R4] |= SB2;
910 else
911 up->curregs[R4] |= SB1;
912 if (cflag & PARENB)
913 up->curregs[R4] |= PAR_ENAB;
914 else
915 up->curregs[R4] &= ~PAR_ENAB;
916 if (!(cflag & PARODD))
917 up->curregs[R4] |= PAR_EVEN;
918 else
919 up->curregs[R4] &= ~PAR_EVEN;
920
921 up->port.read_status_mask = Rx_OVR;
922 if (iflag & INPCK)
923 up->port.read_status_mask |= CRC_ERR | PAR_ERR;
924 if (iflag & (BRKINT | PARMRK))
925 up->port.read_status_mask |= BRK_ABRT;
926
927 up->port.ignore_status_mask = 0;
928 if (iflag & IGNPAR)
929 up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
930 if (iflag & IGNBRK) {
931 up->port.ignore_status_mask |= BRK_ABRT;
932 if (iflag & IGNPAR)
933 up->port.ignore_status_mask |= Rx_OVR;
934 }
935
936 if ((cflag & CREAD) == 0)
937 up->port.ignore_status_mask = 0xff;
938}
939
940/* The port lock is not held. */
941static void
942sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
943 struct ktermios *old)
944{
945 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
946 unsigned long flags;
947 int baud, brg;
948
949 baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
950
951 spin_lock_irqsave(&up->port.lock, flags);
952
953 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
954
955 sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
956
957 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
958 up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
959 else
960 up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
961
962 up->cflag = termios->c_cflag;
963
964 sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
965
966 uart_update_timeout(port, termios->c_cflag, baud);
967
968 spin_unlock_irqrestore(&up->port.lock, flags);
969}
970
971static const char *sunzilog_type(struct uart_port *port)
972{
973 struct uart_sunzilog_port *up = UART_ZILOG(port);
974
975 return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
976}
977
978/* We do not request/release mappings of the registers here, this
979 * happens at early serial probe time.
980 */
981static void sunzilog_release_port(struct uart_port *port)
982{
983}
984
985static int sunzilog_request_port(struct uart_port *port)
986{
987 return 0;
988}
989
990/* These do not need to do anything interesting either. */
991static void sunzilog_config_port(struct uart_port *port, int flags)
992{
993}
994
995/* We do not support letting the user mess with the divisor, IRQ, etc. */
996static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
997{
998 return -EINVAL;
999}
1000
1001#ifdef CONFIG_CONSOLE_POLL
1002static int sunzilog_get_poll_char(struct uart_port *port)
1003{
1004 unsigned char ch, r1;
1005 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
1006 struct zilog_channel __iomem *channel
1007 = ZILOG_CHANNEL_FROM_PORT(&up->port);
1008
1009
1010 r1 = read_zsreg(channel, R1);
1011 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
1012 writeb(ERR_RES, &channel->control);
1013 ZSDELAY();
1014 ZS_WSYNC(channel);
1015 }
1016
1017 ch = readb(&channel->control);
1018 ZSDELAY();
1019
1020 /* This funny hack depends upon BRK_ABRT not interfering
1021 * with the other bits we care about in R1.
1022 */
1023 if (ch & BRK_ABRT)
1024 r1 |= BRK_ABRT;
1025
1026 if (!(ch & Rx_CH_AV))
1027 return NO_POLL_CHAR;
1028
1029 ch = readb(&channel->data);
1030 ZSDELAY();
1031
1032 ch &= up->parity_mask;
1033 return ch;
1034}
1035
1036static void sunzilog_put_poll_char(struct uart_port *port,
1037 unsigned char ch)
1038{
1039 struct uart_sunzilog_port *up = (struct uart_sunzilog_port *)port;
1040
1041 sunzilog_putchar(&up->port, ch);
1042}
1043#endif /* CONFIG_CONSOLE_POLL */
1044
1045static struct uart_ops sunzilog_pops = {
1046 .tx_empty = sunzilog_tx_empty,
1047 .set_mctrl = sunzilog_set_mctrl,
1048 .get_mctrl = sunzilog_get_mctrl,
1049 .stop_tx = sunzilog_stop_tx,
1050 .start_tx = sunzilog_start_tx,
1051 .stop_rx = sunzilog_stop_rx,
1052 .enable_ms = sunzilog_enable_ms,
1053 .break_ctl = sunzilog_break_ctl,
1054 .startup = sunzilog_startup,
1055 .shutdown = sunzilog_shutdown,
1056 .set_termios = sunzilog_set_termios,
1057 .type = sunzilog_type,
1058 .release_port = sunzilog_release_port,
1059 .request_port = sunzilog_request_port,
1060 .config_port = sunzilog_config_port,
1061 .verify_port = sunzilog_verify_port,
1062#ifdef CONFIG_CONSOLE_POLL
1063 .poll_get_char = sunzilog_get_poll_char,
1064 .poll_put_char = sunzilog_put_poll_char,
1065#endif
1066};
1067
1068static int uart_chip_count;
1069static struct uart_sunzilog_port *sunzilog_port_table;
1070static struct zilog_layout __iomem **sunzilog_chip_regs;
1071
1072static struct uart_sunzilog_port *sunzilog_irq_chain;
1073
1074static struct uart_driver sunzilog_reg = {
1075 .owner = THIS_MODULE,
1076 .driver_name = "sunzilog",
1077 .dev_name = "ttyS",
1078 .major = TTY_MAJOR,
1079};
1080
1081static int __init sunzilog_alloc_tables(int num_sunzilog)
1082{
1083 struct uart_sunzilog_port *up;
1084 unsigned long size;
1085 int num_channels = num_sunzilog * 2;
1086 int i;
1087
1088 size = num_channels * sizeof(struct uart_sunzilog_port);
1089 sunzilog_port_table = kzalloc(size, GFP_KERNEL);
1090 if (!sunzilog_port_table)
1091 return -ENOMEM;
1092
1093 for (i = 0; i < num_channels; i++) {
1094 up = &sunzilog_port_table[i];
1095
1096 spin_lock_init(&up->port.lock);
1097
1098 if (i == 0)
1099 sunzilog_irq_chain = up;
1100
1101 if (i < num_channels - 1)
1102 up->next = up + 1;
1103 else
1104 up->next = NULL;
1105 }
1106
1107 size = num_sunzilog * sizeof(struct zilog_layout __iomem *);
1108 sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
1109 if (!sunzilog_chip_regs) {
1110 kfree(sunzilog_port_table);
1111 sunzilog_irq_chain = NULL;
1112 return -ENOMEM;
1113 }
1114
1115 return 0;
1116}
1117
1118static void sunzilog_free_tables(void)
1119{
1120 kfree(sunzilog_port_table);
1121 sunzilog_irq_chain = NULL;
1122 kfree(sunzilog_chip_regs);
1123}
1124
1125#define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
1126
1127static void sunzilog_putchar(struct uart_port *port, int ch)
1128{
1129 struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
1130 int loops = ZS_PUT_CHAR_MAX_DELAY;
1131
1132 /* This is a timed polling loop so do not switch the explicit
1133 * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
1134 */
1135 do {
1136 unsigned char val = readb(&channel->control);
1137 if (val & Tx_BUF_EMP) {
1138 ZSDELAY();
1139 break;
1140 }
1141 udelay(5);
1142 } while (--loops);
1143
1144 writeb(ch, &channel->data);
1145 ZSDELAY();
1146 ZS_WSYNC(channel);
1147}
1148
1149#ifdef CONFIG_SERIO
1150
1151static DEFINE_SPINLOCK(sunzilog_serio_lock);
1152
1153static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1154{
1155 struct uart_sunzilog_port *up = serio->port_data;
1156 unsigned long flags;
1157
1158 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1159
1160 sunzilog_putchar(&up->port, ch);
1161
1162 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1163
1164 return 0;
1165}
1166
1167static int sunzilog_serio_open(struct serio *serio)
1168{
1169 struct uart_sunzilog_port *up = serio->port_data;
1170 unsigned long flags;
1171 int ret;
1172
1173 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1174 if (!up->serio_open) {
1175 up->serio_open = 1;
1176 ret = 0;
1177 } else
1178 ret = -EBUSY;
1179 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1180
1181 return ret;
1182}
1183
1184static void sunzilog_serio_close(struct serio *serio)
1185{
1186 struct uart_sunzilog_port *up = serio->port_data;
1187 unsigned long flags;
1188
1189 spin_lock_irqsave(&sunzilog_serio_lock, flags);
1190 up->serio_open = 0;
1191 spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1192}
1193
1194#endif /* CONFIG_SERIO */
1195
1196#ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
1197static void
1198sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1199{
1200 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1201 unsigned long flags;
1202 int locked = 1;
1203
1204 local_irq_save(flags);
1205 if (up->port.sysrq) {
1206 locked = 0;
1207 } else if (oops_in_progress) {
1208 locked = spin_trylock(&up->port.lock);
1209 } else
1210 spin_lock(&up->port.lock);
1211
1212 uart_console_write(&up->port, s, count, sunzilog_putchar);
1213 udelay(2);
1214
1215 if (locked)
1216 spin_unlock(&up->port.lock);
1217 local_irq_restore(flags);
1218}
1219
1220static int __init sunzilog_console_setup(struct console *con, char *options)
1221{
1222 struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1223 unsigned long flags;
1224 int baud, brg;
1225
1226 if (up->port.type != PORT_SUNZILOG)
1227 return -1;
1228
1229 printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
1230 (sunzilog_reg.minor - 64) + con->index, con->index);
1231
1232 /* Get firmware console settings. */
1233 sunserial_console_termios(con, up->port.dev->of_node);
1234
1235 /* Firmware console speed is limited to 150-->38400 baud so
1236 * this hackish cflag thing is OK.
1237 */
1238 switch (con->cflag & CBAUD) {
1239 case B150: baud = 150; break;
1240 case B300: baud = 300; break;
1241 case B600: baud = 600; break;
1242 case B1200: baud = 1200; break;
1243 case B2400: baud = 2400; break;
1244 case B4800: baud = 4800; break;
1245 default: case B9600: baud = 9600; break;
1246 case B19200: baud = 19200; break;
1247 case B38400: baud = 38400; break;
1248 };
1249
1250 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1251
1252 spin_lock_irqsave(&up->port.lock, flags);
1253
1254 up->curregs[R15] |= BRKIE;
1255 sunzilog_convert_to_zs(up, con->cflag, 0, brg);
1256
1257 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1258 __sunzilog_startup(up);
1259
1260 spin_unlock_irqrestore(&up->port.lock, flags);
1261
1262 return 0;
1263}
1264
1265static struct console sunzilog_console_ops = {
1266 .name = "ttyS",
1267 .write = sunzilog_console_write,
1268 .device = uart_console_device,
1269 .setup = sunzilog_console_setup,
1270 .flags = CON_PRINTBUFFER,
1271 .index = -1,
1272 .data = &sunzilog_reg,
1273};
1274
1275static inline struct console *SUNZILOG_CONSOLE(void)
1276{
1277 return &sunzilog_console_ops;
1278}
1279
1280#else
1281#define SUNZILOG_CONSOLE() (NULL)
1282#endif
1283
1284static void __devinit sunzilog_init_kbdms(struct uart_sunzilog_port *up)
1285{
1286 int baud, brg;
1287
1288 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1289 up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1290 baud = 1200;
1291 } else {
1292 up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1293 baud = 4800;
1294 }
1295
1296 up->curregs[R15] |= BRKIE;
1297 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1298 sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1299 sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1300 __sunzilog_startup(up);
1301}
1302
1303#ifdef CONFIG_SERIO
1304static void __devinit sunzilog_register_serio(struct uart_sunzilog_port *up)
1305{
1306 struct serio *serio = &up->serio;
1307
1308 serio->port_data = up;
1309
1310 serio->id.type = SERIO_RS232;
1311 if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1312 serio->id.proto = SERIO_SUNKBD;
1313 strlcpy(serio->name, "zskbd", sizeof(serio->name));
1314 } else {
1315 serio->id.proto = SERIO_SUN;
1316 serio->id.extra = 1;
1317 strlcpy(serio->name, "zsms", sizeof(serio->name));
1318 }
1319 strlcpy(serio->phys,
1320 ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
1321 "zs/serio0" : "zs/serio1"),
1322 sizeof(serio->phys));
1323
1324 serio->write = sunzilog_serio_write;
1325 serio->open = sunzilog_serio_open;
1326 serio->close = sunzilog_serio_close;
1327 serio->dev.parent = up->port.dev;
1328
1329 serio_register_port(serio);
1330}
1331#endif
1332
1333static void __devinit sunzilog_init_hw(struct uart_sunzilog_port *up)
1334{
1335 struct zilog_channel __iomem *channel;
1336 unsigned long flags;
1337 int baud, brg;
1338
1339 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1340
1341 spin_lock_irqsave(&up->port.lock, flags);
1342 if (ZS_IS_CHANNEL_A(up)) {
1343 write_zsreg(channel, R9, FHWRES);
1344 ZSDELAY_LONG();
1345 (void) read_zsreg(channel, R0);
1346 }
1347
1348 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1349 SUNZILOG_FLAG_CONS_MOUSE)) {
1350 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1351 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1352 up->curregs[R3] = RxENAB | Rx8;
1353 up->curregs[R5] = TxENAB | Tx8;
1354 up->curregs[R6] = 0x00; /* SDLC Address */
1355 up->curregs[R7] = 0x7E; /* SDLC Flag */
1356 up->curregs[R9] = NV;
1357 up->curregs[R7p] = 0x00;
1358 sunzilog_init_kbdms(up);
1359 /* Only enable interrupts if an ISR handler available */
1360 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1361 up->curregs[R9] |= MIE;
1362 write_zsreg(channel, R9, up->curregs[R9]);
1363 } else {
1364 /* Normal serial TTY. */
1365 up->parity_mask = 0xff;
1366 up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
1367 up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
1368 up->curregs[R3] = RxENAB | Rx8;
1369 up->curregs[R5] = TxENAB | Tx8;
1370 up->curregs[R6] = 0x00; /* SDLC Address */
1371 up->curregs[R7] = 0x7E; /* SDLC Flag */
1372 up->curregs[R9] = NV;
1373 up->curregs[R10] = NRZ;
1374 up->curregs[R11] = TCBR | RCBR;
1375 baud = 9600;
1376 brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1377 up->curregs[R12] = (brg & 0xff);
1378 up->curregs[R13] = (brg >> 8) & 0xff;
1379 up->curregs[R14] = BRSRC | BRENAB;
1380 up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
1381 up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
1382 if (__load_zsregs(channel, up->curregs)) {
1383 up->flags |= SUNZILOG_FLAG_ESCC;
1384 }
1385 /* Only enable interrupts if an ISR handler available */
1386 if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
1387 up->curregs[R9] |= MIE;
1388 write_zsreg(channel, R9, up->curregs[R9]);
1389 }
1390
1391 spin_unlock_irqrestore(&up->port.lock, flags);
1392
1393#ifdef CONFIG_SERIO
1394 if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
1395 SUNZILOG_FLAG_CONS_MOUSE))
1396 sunzilog_register_serio(up);
1397#endif
1398}
1399
1400static int zilog_irq = -1;
1401
1402static int __devinit zs_probe(struct platform_device *op)
1403{
1404 static int kbm_inst, uart_inst;
1405 int inst;
1406 struct uart_sunzilog_port *up;
1407 struct zilog_layout __iomem *rp;
1408 int keyboard_mouse = 0;
1409 int err;
1410
1411 if (of_find_property(op->dev.of_node, "keyboard", NULL))
1412 keyboard_mouse = 1;
1413
1414 /* uarts must come before keyboards/mice */
1415 if (keyboard_mouse)
1416 inst = uart_chip_count + kbm_inst;
1417 else
1418 inst = uart_inst;
1419
1420 sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
1421 sizeof(struct zilog_layout),
1422 "zs");
1423 if (!sunzilog_chip_regs[inst])
1424 return -ENOMEM;
1425
1426 rp = sunzilog_chip_regs[inst];
1427
1428 if (zilog_irq == -1)
1429 zilog_irq = op->archdata.irqs[0];
1430
1431 up = &sunzilog_port_table[inst * 2];
1432
1433 /* Channel A */
1434 up[0].port.mapbase = op->resource[0].start + 0x00;
1435 up[0].port.membase = (void __iomem *) &rp->channelA;
1436 up[0].port.iotype = UPIO_MEM;
1437 up[0].port.irq = op->archdata.irqs[0];
1438 up[0].port.uartclk = ZS_CLOCK;
1439 up[0].port.fifosize = 1;
1440 up[0].port.ops = &sunzilog_pops;
1441 up[0].port.type = PORT_SUNZILOG;
1442 up[0].port.flags = 0;
1443 up[0].port.line = (inst * 2) + 0;
1444 up[0].port.dev = &op->dev;
1445 up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1446 if (keyboard_mouse)
1447 up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
1448 sunzilog_init_hw(&up[0]);
1449
1450 /* Channel B */
1451 up[1].port.mapbase = op->resource[0].start + 0x04;
1452 up[1].port.membase = (void __iomem *) &rp->channelB;
1453 up[1].port.iotype = UPIO_MEM;
1454 up[1].port.irq = op->archdata.irqs[0];
1455 up[1].port.uartclk = ZS_CLOCK;
1456 up[1].port.fifosize = 1;
1457 up[1].port.ops = &sunzilog_pops;
1458 up[1].port.type = PORT_SUNZILOG;
1459 up[1].port.flags = 0;
1460 up[1].port.line = (inst * 2) + 1;
1461 up[1].port.dev = &op->dev;
1462 up[1].flags |= 0;
1463 if (keyboard_mouse)
1464 up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
1465 sunzilog_init_hw(&up[1]);
1466
1467 if (!keyboard_mouse) {
1468 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
1469 &sunzilog_reg, up[0].port.line,
1470 false))
1471 up->flags |= SUNZILOG_FLAG_IS_CONS;
1472 err = uart_add_one_port(&sunzilog_reg, &up[0].port);
1473 if (err) {
1474 of_iounmap(&op->resource[0],
1475 rp, sizeof(struct zilog_layout));
1476 return err;
1477 }
1478 if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
1479 &sunzilog_reg, up[1].port.line,
1480 false))
1481 up->flags |= SUNZILOG_FLAG_IS_CONS;
1482 err = uart_add_one_port(&sunzilog_reg, &up[1].port);
1483 if (err) {
1484 uart_remove_one_port(&sunzilog_reg, &up[0].port);
1485 of_iounmap(&op->resource[0],
1486 rp, sizeof(struct zilog_layout));
1487 return err;
1488 }
1489 uart_inst++;
1490 } else {
1491 printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
1492 "is a %s\n",
1493 dev_name(&op->dev),
1494 (unsigned long long) up[0].port.mapbase,
1495 op->archdata.irqs[0], sunzilog_type(&up[0].port));
1496 printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
1497 "is a %s\n",
1498 dev_name(&op->dev),
1499 (unsigned long long) up[1].port.mapbase,
1500 op->archdata.irqs[0], sunzilog_type(&up[1].port));
1501 kbm_inst++;
1502 }
1503
1504 dev_set_drvdata(&op->dev, &up[0]);
1505
1506 return 0;
1507}
1508
1509static void __devexit zs_remove_one(struct uart_sunzilog_port *up)
1510{
1511 if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
1512#ifdef CONFIG_SERIO
1513 serio_unregister_port(&up->serio);
1514#endif
1515 } else
1516 uart_remove_one_port(&sunzilog_reg, &up->port);
1517}
1518
1519static int __devexit zs_remove(struct platform_device *op)
1520{
1521 struct uart_sunzilog_port *up = dev_get_drvdata(&op->dev);
1522 struct zilog_layout __iomem *regs;
1523
1524 zs_remove_one(&up[0]);
1525 zs_remove_one(&up[1]);
1526
1527 regs = sunzilog_chip_regs[up[0].port.line / 2];
1528 of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
1529
1530 dev_set_drvdata(&op->dev, NULL);
1531
1532 return 0;
1533}
1534
1535static const struct of_device_id zs_match[] = {
1536 {
1537 .name = "zs",
1538 },
1539 {},
1540};
1541MODULE_DEVICE_TABLE(of, zs_match);
1542
1543static struct platform_driver zs_driver = {
1544 .driver = {
1545 .name = "zs",
1546 .owner = THIS_MODULE,
1547 .of_match_table = zs_match,
1548 },
1549 .probe = zs_probe,
1550 .remove = __devexit_p(zs_remove),
1551};
1552
1553static int __init sunzilog_init(void)
1554{
1555 struct device_node *dp;
1556 int err;
1557 int num_keybms = 0;
1558 int num_sunzilog = 0;
1559
1560 for_each_node_by_name(dp, "zs") {
1561 num_sunzilog++;
1562 if (of_find_property(dp, "keyboard", NULL))
1563 num_keybms++;
1564 }
1565
1566 if (num_sunzilog) {
1567 err = sunzilog_alloc_tables(num_sunzilog);
1568 if (err)
1569 goto out;
1570
1571 uart_chip_count = num_sunzilog - num_keybms;
1572
1573 err = sunserial_register_minors(&sunzilog_reg,
1574 uart_chip_count * 2);
1575 if (err)
1576 goto out_free_tables;
1577 }
1578
1579 err = platform_driver_register(&zs_driver);
1580 if (err)
1581 goto out_unregister_uart;
1582
1583 if (zilog_irq != -1) {
1584 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1585 err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
1586 "zs", sunzilog_irq_chain);
1587 if (err)
1588 goto out_unregister_driver;
1589
1590 /* Enable Interrupts */
1591 while (up) {
1592 struct zilog_channel __iomem *channel;
1593
1594 /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
1595 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1596 up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
1597 up->curregs[R9] |= MIE;
1598 write_zsreg(channel, R9, up->curregs[R9]);
1599 up = up->next;
1600 }
1601 }
1602
1603out:
1604 return err;
1605
1606out_unregister_driver:
1607 platform_driver_unregister(&zs_driver);
1608
1609out_unregister_uart:
1610 if (num_sunzilog) {
1611 sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
1612 sunzilog_reg.cons = NULL;
1613 }
1614
1615out_free_tables:
1616 sunzilog_free_tables();
1617 goto out;
1618}
1619
1620static void __exit sunzilog_exit(void)
1621{
1622 platform_driver_unregister(&zs_driver);
1623
1624 if (zilog_irq != -1) {
1625 struct uart_sunzilog_port *up = sunzilog_irq_chain;
1626
1627 /* Disable Interrupts */
1628 while (up) {
1629 struct zilog_channel __iomem *channel;
1630
1631 /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
1632 channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1633 up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
1634 up->curregs[R9] &= ~MIE;
1635 write_zsreg(channel, R9, up->curregs[R9]);
1636 up = up->next;
1637 }
1638
1639 free_irq(zilog_irq, sunzilog_irq_chain);
1640 zilog_irq = -1;
1641 }
1642
1643 if (sunzilog_reg.nr) {
1644 sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
1645 sunzilog_free_tables();
1646 }
1647}
1648
1649module_init(sunzilog_init);
1650module_exit(sunzilog_exit);
1651
1652MODULE_AUTHOR("David S. Miller");
1653MODULE_DESCRIPTION("Sun Zilog serial port driver");
1654MODULE_VERSION("2.0");
1655MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/sunzilog.h b/drivers/tty/serial/sunzilog.h
new file mode 100644
index 000000000000..5dec7b47cc38
--- /dev/null
+++ b/drivers/tty/serial/sunzilog.h
@@ -0,0 +1,289 @@
1#ifndef _SUNZILOG_H
2#define _SUNZILOG_H
3
4struct zilog_channel {
5 volatile unsigned char control;
6 volatile unsigned char __pad1;
7 volatile unsigned char data;
8 volatile unsigned char __pad2;
9};
10
11struct zilog_layout {
12 struct zilog_channel channelB;
13 struct zilog_channel channelA;
14};
15
16#define NUM_ZSREGS 17
17#define R7p 16 /* Written as R7 with P15 bit 0 set */
18
19/* Conversion routines to/from brg time constants from/to bits
20 * per second.
21 */
22#define BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
23#define BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
24
25/* The Zilog register set */
26
27#define FLAG 0x7e
28
29/* Write Register 0 */
30#define R0 0 /* Register selects */
31#define R1 1
32#define R2 2
33#define R3 3
34#define R4 4
35#define R5 5
36#define R6 6
37#define R7 7
38#define R8 8
39#define R9 9
40#define R10 10
41#define R11 11
42#define R12 12
43#define R13 13
44#define R14 14
45#define R15 15
46
47#define NULLCODE 0 /* Null Code */
48#define POINT_HIGH 0x8 /* Select upper half of registers */
49#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
50#define SEND_ABORT 0x18 /* HDLC Abort */
51#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
52#define RES_Tx_P 0x28 /* Reset TxINT Pending */
53#define ERR_RES 0x30 /* Error Reset */
54#define RES_H_IUS 0x38 /* Reset highest IUS */
55
56#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
57#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
58#define RES_EOM_L 0xC0 /* Reset EOM latch */
59
60/* Write Register 1 */
61
62#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
63#define TxINT_ENAB 0x2 /* Tx Int Enable */
64#define PAR_SPEC 0x4 /* Parity is special condition */
65
66#define RxINT_DISAB 0 /* Rx Int Disable */
67#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
68#define INT_ALL_Rx 0x10 /* Int on all Rx Characters or error */
69#define INT_ERR_Rx 0x18 /* Int on error only */
70#define RxINT_MASK 0x18
71
72#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
73#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
74#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
75
76/* Write Register #2 (Interrupt Vector) */
77
78/* Write Register 3 */
79
80#define RxENAB 0x1 /* Rx Enable */
81#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
82#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
83#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
84#define ENT_HM 0x10 /* Enter Hunt Mode */
85#define AUTO_ENAB 0x20 /* Auto Enables */
86#define Rx5 0x0 /* Rx 5 Bits/Character */
87#define Rx7 0x40 /* Rx 7 Bits/Character */
88#define Rx6 0x80 /* Rx 6 Bits/Character */
89#define Rx8 0xc0 /* Rx 8 Bits/Character */
90#define RxN_MASK 0xc0
91
92/* Write Register 4 */
93
94#define PAR_ENAB 0x1 /* Parity Enable */
95#define PAR_EVEN 0x2 /* Parity Even/Odd* */
96
97#define SYNC_ENAB 0 /* Sync Modes Enable */
98#define SB1 0x4 /* 1 stop bit/char */
99#define SB15 0x8 /* 1.5 stop bits/char */
100#define SB2 0xc /* 2 stop bits/char */
101
102#define MONSYNC 0 /* 8 Bit Sync character */
103#define BISYNC 0x10 /* 16 bit sync character */
104#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
105#define EXTSYNC 0x30 /* External Sync Mode */
106
107#define X1CLK 0x0 /* x1 clock mode */
108#define X16CLK 0x40 /* x16 clock mode */
109#define X32CLK 0x80 /* x32 clock mode */
110#define X64CLK 0xC0 /* x64 clock mode */
111#define XCLK_MASK 0xC0
112
113/* Write Register 5 */
114
115#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
116#define RTS 0x2 /* RTS */
117#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
118#define TxENAB 0x8 /* Tx Enable */
119#define SND_BRK 0x10 /* Send Break */
120#define Tx5 0x0 /* Tx 5 bits (or less)/character */
121#define Tx7 0x20 /* Tx 7 bits/character */
122#define Tx6 0x40 /* Tx 6 bits/character */
123#define Tx8 0x60 /* Tx 8 bits/character */
124#define TxN_MASK 0x60
125#define DTR 0x80 /* DTR */
126
127/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
128
129/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
130
131/* Write Register 7' (ESCC Only) */
132#define AUTO_TxFLAG 1 /* Automatic Tx SDLC Flag */
133#define AUTO_EOM_RST 2 /* Automatic EOM Reset */
134#define AUTOnRTS 4 /* Automatic /RTS pin deactivation */
135#define RxFIFO_LVL 8 /* Receive FIFO interrupt level */
136#define nDTRnREQ 0x10 /* /DTR/REQ timing */
137#define TxFIFO_LVL 0x20 /* Transmit FIFO interrupt level */
138#define EXT_RD_EN 0x40 /* Extended read register enable */
139
140/* Write Register 8 (transmit buffer) */
141
142/* Write Register 9 (Master interrupt control) */
143#define VIS 1 /* Vector Includes Status */
144#define NV 2 /* No Vector */
145#define DLC 4 /* Disable Lower Chain */
146#define MIE 8 /* Master Interrupt Enable */
147#define STATHI 0x10 /* Status high */
148#define SWIACK 0x20 /* Software Interrupt Ack (not on NMOS) */
149#define NORESET 0 /* No reset on write to R9 */
150#define CHRB 0x40 /* Reset channel B */
151#define CHRA 0x80 /* Reset channel A */
152#define FHWRES 0xc0 /* Force hardware reset */
153
154/* Write Register 10 (misc control bits) */
155#define BIT6 1 /* 6 bit/8bit sync */
156#define LOOPMODE 2 /* SDLC Loop mode */
157#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
158#define MARKIDLE 8 /* Mark/flag on idle */
159#define GAOP 0x10 /* Go active on poll */
160#define NRZ 0 /* NRZ mode */
161#define NRZI 0x20 /* NRZI mode */
162#define FM1 0x40 /* FM1 (transition = 1) */
163#define FM0 0x60 /* FM0 (transition = 0) */
164#define CRCPS 0x80 /* CRC Preset I/O */
165
166/* Write Register 11 (Clock Mode control) */
167#define TRxCXT 0 /* TRxC = Xtal output */
168#define TRxCTC 1 /* TRxC = Transmit clock */
169#define TRxCBR 2 /* TRxC = BR Generator Output */
170#define TRxCDP 3 /* TRxC = DPLL output */
171#define TRxCOI 4 /* TRxC O/I */
172#define TCRTxCP 0 /* Transmit clock = RTxC pin */
173#define TCTRxCP 8 /* Transmit clock = TRxC pin */
174#define TCBR 0x10 /* Transmit clock = BR Generator output */
175#define TCDPLL 0x18 /* Transmit clock = DPLL output */
176#define RCRTxCP 0 /* Receive clock = RTxC pin */
177#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
178#define RCBR 0x40 /* Receive clock = BR Generator output */
179#define RCDPLL 0x60 /* Receive clock = DPLL output */
180#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
181
182/* Write Register 12 (lower byte of baud rate generator time constant) */
183
184/* Write Register 13 (upper byte of baud rate generator time constant) */
185
186/* Write Register 14 (Misc control bits) */
187#define BRENAB 1 /* Baud rate generator enable */
188#define BRSRC 2 /* Baud rate generator source */
189#define DTRREQ 4 /* DTR/Request function */
190#define AUTOECHO 8 /* Auto Echo */
191#define LOOPBAK 0x10 /* Local loopback */
192#define SEARCH 0x20 /* Enter search mode */
193#define RMC 0x40 /* Reset missing clock */
194#define DISDPLL 0x60 /* Disable DPLL */
195#define SSBR 0x80 /* Set DPLL source = BR generator */
196#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
197#define SFMM 0xc0 /* Set FM mode */
198#define SNRZI 0xe0 /* Set NRZI mode */
199
200/* Write Register 15 (external/status interrupt control) */
201#define WR7pEN 1 /* WR7' Enable (ESCC only) */
202#define ZCIE 2 /* Zero count IE */
203#define FIFOEN 4 /* FIFO Enable (ESCC only) */
204#define DCDIE 8 /* DCD IE */
205#define SYNCIE 0x10 /* Sync/hunt IE */
206#define CTSIE 0x20 /* CTS IE */
207#define TxUIE 0x40 /* Tx Underrun/EOM IE */
208#define BRKIE 0x80 /* Break/Abort IE */
209
210
211/* Read Register 0 */
212#define Rx_CH_AV 0x1 /* Rx Character Available */
213#define ZCOUNT 0x2 /* Zero count */
214#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
215#define DCD 0x8 /* DCD */
216#define SYNC 0x10 /* Sync/hunt */
217#define CTS 0x20 /* CTS */
218#define TxEOM 0x40 /* Tx underrun */
219#define BRK_ABRT 0x80 /* Break/Abort */
220
221/* Read Register 1 */
222#define ALL_SNT 0x1 /* All sent */
223/* Residue Data for 8 Rx bits/char programmed */
224#define RES3 0x8 /* 0/3 */
225#define RES4 0x4 /* 0/4 */
226#define RES5 0xc /* 0/5 */
227#define RES6 0x2 /* 0/6 */
228#define RES7 0xa /* 0/7 */
229#define RES8 0x6 /* 0/8 */
230#define RES18 0xe /* 1/8 */
231#define RES28 0x0 /* 2/8 */
232/* Special Rx Condition Interrupts */
233#define PAR_ERR 0x10 /* Parity error */
234#define Rx_OVR 0x20 /* Rx Overrun Error */
235#define CRC_ERR 0x40 /* CRC/Framing Error */
236#define END_FR 0x80 /* End of Frame (SDLC) */
237
238/* Read Register 2 (channel b only) - Interrupt vector */
239#define CHB_Tx_EMPTY 0x00
240#define CHB_EXT_STAT 0x02
241#define CHB_Rx_AVAIL 0x04
242#define CHB_SPECIAL 0x06
243#define CHA_Tx_EMPTY 0x08
244#define CHA_EXT_STAT 0x0a
245#define CHA_Rx_AVAIL 0x0c
246#define CHA_SPECIAL 0x0e
247#define STATUS_MASK 0x0e
248
249/* Read Register 3 (interrupt pending register) ch a only */
250#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
251#define CHBTxIP 0x2 /* Channel B Tx IP */
252#define CHBRxIP 0x4 /* Channel B Rx IP */
253#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
254#define CHATxIP 0x10 /* Channel A Tx IP */
255#define CHARxIP 0x20 /* Channel A Rx IP */
256
257/* Read Register 6 (LSB frame byte count [Not on NMOS]) */
258
259/* Read Register 7 (MSB frame byte count and FIFO status [Not on NMOS]) */
260
261/* Read Register 8 (receive data register) */
262
263/* Read Register 10 (misc status bits) */
264#define ONLOOP 2 /* On loop */
265#define LOOPSEND 0x10 /* Loop sending */
266#define CLK2MIS 0x40 /* Two clocks missing */
267#define CLK1MIS 0x80 /* One clock missing */
268
269/* Read Register 12 (lower byte of baud rate generator constant) */
270
271/* Read Register 13 (upper byte of baud rate generator constant) */
272
273/* Read Register 15 (value of WR 15) */
274
275/* Misc macros */
276#define ZS_CLEARERR(channel) do { sbus_writeb(ERR_RES, &channel->control); \
277 udelay(5); } while(0)
278
279#define ZS_CLEARSTAT(channel) do { sbus_writeb(RES_EXT_INT, &channel->control); \
280 udelay(5); } while(0)
281
282#define ZS_CLEARFIFO(channel) do { sbus_readb(&channel->data); \
283 udelay(2); \
284 sbus_readb(&channel->data); \
285 udelay(2); \
286 sbus_readb(&channel->data); \
287 udelay(2); } while(0)
288
289#endif /* _SUNZILOG_H */
diff --git a/drivers/tty/serial/timbuart.c b/drivers/tty/serial/timbuart.c
new file mode 100644
index 000000000000..1f36b7eb7351
--- /dev/null
+++ b/drivers/tty/serial/timbuart.c
@@ -0,0 +1,531 @@
1/*
2 * timbuart.c timberdale FPGA UART driver
3 * Copyright (c) 2009 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * Timberdale FPGA UART
21 */
22
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/serial_core.h>
26#include <linux/kernel.h>
27#include <linux/platform_device.h>
28#include <linux/ioport.h>
29#include <linux/slab.h>
30
31#include "timbuart.h"
32
33struct timbuart_port {
34 struct uart_port port;
35 struct tasklet_struct tasklet;
36 int usedma;
37 u32 last_ier;
38 struct platform_device *dev;
39};
40
41static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
42 921600, 1843200, 3250000};
43
44static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
45
46static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
47
48static void timbuart_stop_rx(struct uart_port *port)
49{
50 /* spin lock held by upper layer, disable all RX interrupts */
51 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
52 iowrite32(ier, port->membase + TIMBUART_IER);
53}
54
55static void timbuart_stop_tx(struct uart_port *port)
56{
57 /* spinlock held by upper layer, disable TX interrupt */
58 u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
59 iowrite32(ier, port->membase + TIMBUART_IER);
60}
61
62static void timbuart_start_tx(struct uart_port *port)
63{
64 struct timbuart_port *uart =
65 container_of(port, struct timbuart_port, port);
66
67 /* do not transfer anything here -> fire off the tasklet */
68 tasklet_schedule(&uart->tasklet);
69}
70
71static unsigned int timbuart_tx_empty(struct uart_port *port)
72{
73 u32 isr = ioread32(port->membase + TIMBUART_ISR);
74
75 return (isr & TXBE) ? TIOCSER_TEMT : 0;
76}
77
78static void timbuart_flush_buffer(struct uart_port *port)
79{
80 if (!timbuart_tx_empty(port)) {
81 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
82 TIMBUART_CTRL_FLSHTX;
83
84 iowrite8(ctl, port->membase + TIMBUART_CTRL);
85 iowrite32(TXBF, port->membase + TIMBUART_ISR);
86 }
87}
88
89static void timbuart_rx_chars(struct uart_port *port)
90{
91 struct tty_struct *tty = port->state->port.tty;
92
93 while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
94 u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
95 port->icount.rx++;
96 tty_insert_flip_char(tty, ch, TTY_NORMAL);
97 }
98
99 spin_unlock(&port->lock);
100 tty_flip_buffer_push(port->state->port.tty);
101 spin_lock(&port->lock);
102
103 dev_dbg(port->dev, "%s - total read %d bytes\n",
104 __func__, port->icount.rx);
105}
106
107static void timbuart_tx_chars(struct uart_port *port)
108{
109 struct circ_buf *xmit = &port->state->xmit;
110
111 while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
112 !uart_circ_empty(xmit)) {
113 iowrite8(xmit->buf[xmit->tail],
114 port->membase + TIMBUART_TXFIFO);
115 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
116 port->icount.tx++;
117 }
118
119 dev_dbg(port->dev,
120 "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
121 __func__,
122 port->icount.tx,
123 ioread8(port->membase + TIMBUART_CTRL),
124 port->mctrl & TIOCM_RTS,
125 ioread8(port->membase + TIMBUART_BAUDRATE));
126}
127
128static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
129{
130 struct timbuart_port *uart =
131 container_of(port, struct timbuart_port, port);
132 struct circ_buf *xmit = &port->state->xmit;
133
134 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
135 return;
136
137 if (port->x_char)
138 return;
139
140 if (isr & TXFLAGS) {
141 timbuart_tx_chars(port);
142 /* clear all TX interrupts */
143 iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
144
145 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
146 uart_write_wakeup(port);
147 } else
148 /* Re-enable any tx interrupt */
149 *ier |= uart->last_ier & TXFLAGS;
150
151 /* enable interrupts if there are chars in the transmit buffer,
152 * Or if we delivered some bytes and want the almost empty interrupt
153 * we wake up the upper layer later when we got the interrupt
154 * to give it some time to go out...
155 */
156 if (!uart_circ_empty(xmit))
157 *ier |= TXBAE;
158
159 dev_dbg(port->dev, "%s - leaving\n", __func__);
160}
161
162void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
163{
164 if (isr & RXFLAGS) {
165 /* Some RX status is set */
166 if (isr & RXBF) {
167 u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
168 TIMBUART_CTRL_FLSHRX;
169 iowrite8(ctl, port->membase + TIMBUART_CTRL);
170 port->icount.overrun++;
171 } else if (isr & (RXDP))
172 timbuart_rx_chars(port);
173
174 /* ack all RX interrupts */
175 iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
176 }
177
178 /* always have the RX interrupts enabled */
179 *ier |= RXBAF | RXBF | RXTT;
180
181 dev_dbg(port->dev, "%s - leaving\n", __func__);
182}
183
184void timbuart_tasklet(unsigned long arg)
185{
186 struct timbuart_port *uart = (struct timbuart_port *)arg;
187 u32 isr, ier = 0;
188
189 spin_lock(&uart->port.lock);
190
191 isr = ioread32(uart->port.membase + TIMBUART_ISR);
192 dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
193
194 if (!uart->usedma)
195 timbuart_handle_tx_port(&uart->port, isr, &ier);
196
197 timbuart_mctrl_check(&uart->port, isr, &ier);
198
199 if (!uart->usedma)
200 timbuart_handle_rx_port(&uart->port, isr, &ier);
201
202 iowrite32(ier, uart->port.membase + TIMBUART_IER);
203
204 spin_unlock(&uart->port.lock);
205 dev_dbg(uart->port.dev, "%s leaving\n", __func__);
206}
207
208static unsigned int timbuart_get_mctrl(struct uart_port *port)
209{
210 u8 cts = ioread8(port->membase + TIMBUART_CTRL);
211 dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
212
213 if (cts & TIMBUART_CTRL_CTS)
214 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
215 else
216 return TIOCM_DSR | TIOCM_CAR;
217}
218
219static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
220{
221 dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
222
223 if (mctrl & TIOCM_RTS)
224 iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
225 else
226 iowrite8(0, port->membase + TIMBUART_CTRL);
227}
228
229static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
230{
231 unsigned int cts;
232
233 if (isr & CTS_DELTA) {
234 /* ack */
235 iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
236 cts = timbuart_get_mctrl(port);
237 uart_handle_cts_change(port, cts & TIOCM_CTS);
238 wake_up_interruptible(&port->state->port.delta_msr_wait);
239 }
240
241 *ier |= CTS_DELTA;
242}
243
244static void timbuart_enable_ms(struct uart_port *port)
245{
246 /* N/A */
247}
248
249static void timbuart_break_ctl(struct uart_port *port, int ctl)
250{
251 /* N/A */
252}
253
254static int timbuart_startup(struct uart_port *port)
255{
256 struct timbuart_port *uart =
257 container_of(port, struct timbuart_port, port);
258
259 dev_dbg(port->dev, "%s\n", __func__);
260
261 iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
262 iowrite32(0x1ff, port->membase + TIMBUART_ISR);
263 /* Enable all but TX interrupts */
264 iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
265 port->membase + TIMBUART_IER);
266
267 return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
268 "timb-uart", uart);
269}
270
271static void timbuart_shutdown(struct uart_port *port)
272{
273 struct timbuart_port *uart =
274 container_of(port, struct timbuart_port, port);
275 dev_dbg(port->dev, "%s\n", __func__);
276 free_irq(port->irq, uart);
277 iowrite32(0, port->membase + TIMBUART_IER);
278}
279
280static int get_bindex(int baud)
281{
282 int i;
283
284 for (i = 0; i < ARRAY_SIZE(baudrates); i++)
285 if (baud <= baudrates[i])
286 return i;
287
288 return -1;
289}
290
291static void timbuart_set_termios(struct uart_port *port,
292 struct ktermios *termios,
293 struct ktermios *old)
294{
295 unsigned int baud;
296 short bindex;
297 unsigned long flags;
298
299 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
300 bindex = get_bindex(baud);
301 dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
302
303 if (bindex < 0)
304 bindex = 0;
305 baud = baudrates[bindex];
306
307 /* The serial layer calls into this once with old = NULL when setting
308 up initially */
309 if (old)
310 tty_termios_copy_hw(termios, old);
311 tty_termios_encode_baud_rate(termios, baud, baud);
312
313 spin_lock_irqsave(&port->lock, flags);
314 iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
315 uart_update_timeout(port, termios->c_cflag, baud);
316 spin_unlock_irqrestore(&port->lock, flags);
317}
318
319static const char *timbuart_type(struct uart_port *port)
320{
321 return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
322}
323
324/* We do not request/release mappings of the registers here,
325 * currently it's done in the proble function.
326 */
327static void timbuart_release_port(struct uart_port *port)
328{
329 struct platform_device *pdev = to_platform_device(port->dev);
330 int size =
331 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
332
333 if (port->flags & UPF_IOREMAP) {
334 iounmap(port->membase);
335 port->membase = NULL;
336 }
337
338 release_mem_region(port->mapbase, size);
339}
340
341static int timbuart_request_port(struct uart_port *port)
342{
343 struct platform_device *pdev = to_platform_device(port->dev);
344 int size =
345 resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
346
347 if (!request_mem_region(port->mapbase, size, "timb-uart"))
348 return -EBUSY;
349
350 if (port->flags & UPF_IOREMAP) {
351 port->membase = ioremap(port->mapbase, size);
352 if (port->membase == NULL) {
353 release_mem_region(port->mapbase, size);
354 return -ENOMEM;
355 }
356 }
357
358 return 0;
359}
360
361static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
362{
363 struct timbuart_port *uart = (struct timbuart_port *)devid;
364
365 if (ioread8(uart->port.membase + TIMBUART_IPR)) {
366 uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
367
368 /* disable interrupts, the tasklet enables them again */
369 iowrite32(0, uart->port.membase + TIMBUART_IER);
370
371 /* fire off bottom half */
372 tasklet_schedule(&uart->tasklet);
373
374 return IRQ_HANDLED;
375 } else
376 return IRQ_NONE;
377}
378
379/*
380 * Configure/autoconfigure the port.
381 */
382static void timbuart_config_port(struct uart_port *port, int flags)
383{
384 if (flags & UART_CONFIG_TYPE) {
385 port->type = PORT_TIMBUART;
386 timbuart_request_port(port);
387 }
388}
389
390static int timbuart_verify_port(struct uart_port *port,
391 struct serial_struct *ser)
392{
393 /* we don't want the core code to modify any port params */
394 return -EINVAL;
395}
396
397static struct uart_ops timbuart_ops = {
398 .tx_empty = timbuart_tx_empty,
399 .set_mctrl = timbuart_set_mctrl,
400 .get_mctrl = timbuart_get_mctrl,
401 .stop_tx = timbuart_stop_tx,
402 .start_tx = timbuart_start_tx,
403 .flush_buffer = timbuart_flush_buffer,
404 .stop_rx = timbuart_stop_rx,
405 .enable_ms = timbuart_enable_ms,
406 .break_ctl = timbuart_break_ctl,
407 .startup = timbuart_startup,
408 .shutdown = timbuart_shutdown,
409 .set_termios = timbuart_set_termios,
410 .type = timbuart_type,
411 .release_port = timbuart_release_port,
412 .request_port = timbuart_request_port,
413 .config_port = timbuart_config_port,
414 .verify_port = timbuart_verify_port
415};
416
417static struct uart_driver timbuart_driver = {
418 .owner = THIS_MODULE,
419 .driver_name = "timberdale_uart",
420 .dev_name = "ttyTU",
421 .major = TIMBUART_MAJOR,
422 .minor = TIMBUART_MINOR,
423 .nr = 1
424};
425
426static int __devinit timbuart_probe(struct platform_device *dev)
427{
428 int err, irq;
429 struct timbuart_port *uart;
430 struct resource *iomem;
431
432 dev_dbg(&dev->dev, "%s\n", __func__);
433
434 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
435 if (!uart) {
436 err = -EINVAL;
437 goto err_mem;
438 }
439
440 uart->usedma = 0;
441
442 uart->port.uartclk = 3250000 * 16;
443 uart->port.fifosize = TIMBUART_FIFO_SIZE;
444 uart->port.regshift = 2;
445 uart->port.iotype = UPIO_MEM;
446 uart->port.ops = &timbuart_ops;
447 uart->port.irq = 0;
448 uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
449 uart->port.line = 0;
450 uart->port.dev = &dev->dev;
451
452 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
453 if (!iomem) {
454 err = -ENOMEM;
455 goto err_register;
456 }
457 uart->port.mapbase = iomem->start;
458 uart->port.membase = NULL;
459
460 irq = platform_get_irq(dev, 0);
461 if (irq < 0) {
462 err = -EINVAL;
463 goto err_register;
464 }
465 uart->port.irq = irq;
466
467 tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
468
469 err = uart_register_driver(&timbuart_driver);
470 if (err)
471 goto err_register;
472
473 err = uart_add_one_port(&timbuart_driver, &uart->port);
474 if (err)
475 goto err_add_port;
476
477 platform_set_drvdata(dev, uart);
478
479 return 0;
480
481err_add_port:
482 uart_unregister_driver(&timbuart_driver);
483err_register:
484 kfree(uart);
485err_mem:
486 printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
487 err);
488
489 return err;
490}
491
492static int __devexit timbuart_remove(struct platform_device *dev)
493{
494 struct timbuart_port *uart = platform_get_drvdata(dev);
495
496 tasklet_kill(&uart->tasklet);
497 uart_remove_one_port(&timbuart_driver, &uart->port);
498 uart_unregister_driver(&timbuart_driver);
499 kfree(uart);
500
501 return 0;
502}
503
504static struct platform_driver timbuart_platform_driver = {
505 .driver = {
506 .name = "timb-uart",
507 .owner = THIS_MODULE,
508 },
509 .probe = timbuart_probe,
510 .remove = __devexit_p(timbuart_remove),
511};
512
513/*--------------------------------------------------------------------------*/
514
515static int __init timbuart_init(void)
516{
517 return platform_driver_register(&timbuart_platform_driver);
518}
519
520static void __exit timbuart_exit(void)
521{
522 platform_driver_unregister(&timbuart_platform_driver);
523}
524
525module_init(timbuart_init);
526module_exit(timbuart_exit);
527
528MODULE_DESCRIPTION("Timberdale UART driver");
529MODULE_LICENSE("GPL v2");
530MODULE_ALIAS("platform:timb-uart");
531
diff --git a/drivers/tty/serial/timbuart.h b/drivers/tty/serial/timbuart.h
new file mode 100644
index 000000000000..7e566766bc43
--- /dev/null
+++ b/drivers/tty/serial/timbuart.h
@@ -0,0 +1,58 @@
1/*
2 * timbuart.c timberdale FPGA GPIO driver
3 * Copyright (c) 2009 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * Timberdale FPGA UART
21 */
22
23#ifndef _TIMBUART_H
24#define _TIMBUART_H
25
26#define TIMBUART_FIFO_SIZE 2048
27
28#define TIMBUART_RXFIFO 0x08
29#define TIMBUART_TXFIFO 0x0c
30#define TIMBUART_IER 0x10
31#define TIMBUART_IPR 0x14
32#define TIMBUART_ISR 0x18
33#define TIMBUART_CTRL 0x1c
34#define TIMBUART_BAUDRATE 0x20
35
36#define TIMBUART_CTRL_RTS 0x01
37#define TIMBUART_CTRL_CTS 0x02
38#define TIMBUART_CTRL_FLSHTX 0x40
39#define TIMBUART_CTRL_FLSHRX 0x80
40
41#define TXBF 0x01
42#define TXBAE 0x02
43#define CTS_DELTA 0x04
44#define RXDP 0x08
45#define RXBAF 0x10
46#define RXBF 0x20
47#define RXTT 0x40
48#define RXBNAE 0x80
49#define TXBE 0x100
50
51#define RXFLAGS (RXDP | RXBAF | RXBF | RXTT | RXBNAE)
52#define TXFLAGS (TXBF | TXBAE)
53
54#define TIMBUART_MAJOR 204
55#define TIMBUART_MINOR 192
56
57#endif /* _TIMBUART_H */
58
diff --git a/drivers/tty/serial/uartlite.c b/drivers/tty/serial/uartlite.c
new file mode 100644
index 000000000000..8af1ed83a4c0
--- /dev/null
+++ b/drivers/tty/serial/uartlite.c
@@ -0,0 +1,654 @@
1/*
2 * uartlite.c: Serial driver for Xilinx uartlite serial controller
3 *
4 * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
5 * Copyright (C) 2007 Secret Lab Technologies Ltd.
6 *
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
10 */
11
12#include <linux/platform_device.h>
13#include <linux/module.h>
14#include <linux/console.h>
15#include <linux/serial.h>
16#include <linux/serial_core.h>
17#include <linux/tty.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/init.h>
21#include <asm/io.h>
22#include <linux/of.h>
23#include <linux/of_address.h>
24#include <linux/of_device.h>
25#include <linux/of_platform.h>
26
27#define ULITE_NAME "ttyUL"
28#define ULITE_MAJOR 204
29#define ULITE_MINOR 187
30#define ULITE_NR_UARTS 4
31
32/* ---------------------------------------------------------------------
33 * Register definitions
34 *
35 * For register details see datasheet:
36 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
37 */
38
39#define ULITE_RX 0x00
40#define ULITE_TX 0x04
41#define ULITE_STATUS 0x08
42#define ULITE_CONTROL 0x0c
43
44#define ULITE_REGION 16
45
46#define ULITE_STATUS_RXVALID 0x01
47#define ULITE_STATUS_RXFULL 0x02
48#define ULITE_STATUS_TXEMPTY 0x04
49#define ULITE_STATUS_TXFULL 0x08
50#define ULITE_STATUS_IE 0x10
51#define ULITE_STATUS_OVERRUN 0x20
52#define ULITE_STATUS_FRAME 0x40
53#define ULITE_STATUS_PARITY 0x80
54
55#define ULITE_CONTROL_RST_TX 0x01
56#define ULITE_CONTROL_RST_RX 0x02
57#define ULITE_CONTROL_IE 0x10
58
59
60static struct uart_port ulite_ports[ULITE_NR_UARTS];
61
62/* ---------------------------------------------------------------------
63 * Core UART driver operations
64 */
65
66static int ulite_receive(struct uart_port *port, int stat)
67{
68 struct tty_struct *tty = port->state->port.tty;
69 unsigned char ch = 0;
70 char flag = TTY_NORMAL;
71
72 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
73 | ULITE_STATUS_FRAME)) == 0)
74 return 0;
75
76 /* stats */
77 if (stat & ULITE_STATUS_RXVALID) {
78 port->icount.rx++;
79 ch = ioread32be(port->membase + ULITE_RX);
80
81 if (stat & ULITE_STATUS_PARITY)
82 port->icount.parity++;
83 }
84
85 if (stat & ULITE_STATUS_OVERRUN)
86 port->icount.overrun++;
87
88 if (stat & ULITE_STATUS_FRAME)
89 port->icount.frame++;
90
91
92 /* drop byte with parity error if IGNPAR specificed */
93 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
94 stat &= ~ULITE_STATUS_RXVALID;
95
96 stat &= port->read_status_mask;
97
98 if (stat & ULITE_STATUS_PARITY)
99 flag = TTY_PARITY;
100
101
102 stat &= ~port->ignore_status_mask;
103
104 if (stat & ULITE_STATUS_RXVALID)
105 tty_insert_flip_char(tty, ch, flag);
106
107 if (stat & ULITE_STATUS_FRAME)
108 tty_insert_flip_char(tty, 0, TTY_FRAME);
109
110 if (stat & ULITE_STATUS_OVERRUN)
111 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
112
113 return 1;
114}
115
116static int ulite_transmit(struct uart_port *port, int stat)
117{
118 struct circ_buf *xmit = &port->state->xmit;
119
120 if (stat & ULITE_STATUS_TXFULL)
121 return 0;
122
123 if (port->x_char) {
124 iowrite32be(port->x_char, port->membase + ULITE_TX);
125 port->x_char = 0;
126 port->icount.tx++;
127 return 1;
128 }
129
130 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
131 return 0;
132
133 iowrite32be(xmit->buf[xmit->tail], port->membase + ULITE_TX);
134 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
135 port->icount.tx++;
136
137 /* wake up */
138 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
139 uart_write_wakeup(port);
140
141 return 1;
142}
143
144static irqreturn_t ulite_isr(int irq, void *dev_id)
145{
146 struct uart_port *port = dev_id;
147 int busy, n = 0;
148
149 do {
150 int stat = ioread32be(port->membase + ULITE_STATUS);
151 busy = ulite_receive(port, stat);
152 busy |= ulite_transmit(port, stat);
153 n++;
154 } while (busy);
155
156 /* work done? */
157 if (n > 1) {
158 tty_flip_buffer_push(port->state->port.tty);
159 return IRQ_HANDLED;
160 } else {
161 return IRQ_NONE;
162 }
163}
164
165static unsigned int ulite_tx_empty(struct uart_port *port)
166{
167 unsigned long flags;
168 unsigned int ret;
169
170 spin_lock_irqsave(&port->lock, flags);
171 ret = ioread32be(port->membase + ULITE_STATUS);
172 spin_unlock_irqrestore(&port->lock, flags);
173
174 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
175}
176
177static unsigned int ulite_get_mctrl(struct uart_port *port)
178{
179 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
180}
181
182static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
183{
184 /* N/A */
185}
186
187static void ulite_stop_tx(struct uart_port *port)
188{
189 /* N/A */
190}
191
192static void ulite_start_tx(struct uart_port *port)
193{
194 ulite_transmit(port, ioread32be(port->membase + ULITE_STATUS));
195}
196
197static void ulite_stop_rx(struct uart_port *port)
198{
199 /* don't forward any more data (like !CREAD) */
200 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
201 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
202}
203
204static void ulite_enable_ms(struct uart_port *port)
205{
206 /* N/A */
207}
208
209static void ulite_break_ctl(struct uart_port *port, int ctl)
210{
211 /* N/A */
212}
213
214static int ulite_startup(struct uart_port *port)
215{
216 int ret;
217
218 ret = request_irq(port->irq, ulite_isr,
219 IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
220 if (ret)
221 return ret;
222
223 iowrite32be(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
224 port->membase + ULITE_CONTROL);
225 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
226
227 return 0;
228}
229
230static void ulite_shutdown(struct uart_port *port)
231{
232 iowrite32be(0, port->membase + ULITE_CONTROL);
233 ioread32be(port->membase + ULITE_CONTROL); /* dummy */
234 free_irq(port->irq, port);
235}
236
237static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
238 struct ktermios *old)
239{
240 unsigned long flags;
241 unsigned int baud;
242
243 spin_lock_irqsave(&port->lock, flags);
244
245 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
246 | ULITE_STATUS_TXFULL;
247
248 if (termios->c_iflag & INPCK)
249 port->read_status_mask |=
250 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
251
252 port->ignore_status_mask = 0;
253 if (termios->c_iflag & IGNPAR)
254 port->ignore_status_mask |= ULITE_STATUS_PARITY
255 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
256
257 /* ignore all characters if CREAD is not set */
258 if ((termios->c_cflag & CREAD) == 0)
259 port->ignore_status_mask |=
260 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
261 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
262
263 /* update timeout */
264 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
265 uart_update_timeout(port, termios->c_cflag, baud);
266
267 spin_unlock_irqrestore(&port->lock, flags);
268}
269
270static const char *ulite_type(struct uart_port *port)
271{
272 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
273}
274
275static void ulite_release_port(struct uart_port *port)
276{
277 release_mem_region(port->mapbase, ULITE_REGION);
278 iounmap(port->membase);
279 port->membase = NULL;
280}
281
282static int ulite_request_port(struct uart_port *port)
283{
284 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
285 port, (unsigned long long) port->mapbase);
286
287 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
288 dev_err(port->dev, "Memory region busy\n");
289 return -EBUSY;
290 }
291
292 port->membase = ioremap(port->mapbase, ULITE_REGION);
293 if (!port->membase) {
294 dev_err(port->dev, "Unable to map registers\n");
295 release_mem_region(port->mapbase, ULITE_REGION);
296 return -EBUSY;
297 }
298
299 return 0;
300}
301
302static void ulite_config_port(struct uart_port *port, int flags)
303{
304 if (!ulite_request_port(port))
305 port->type = PORT_UARTLITE;
306}
307
308static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
309{
310 /* we don't want the core code to modify any port params */
311 return -EINVAL;
312}
313
314#ifdef CONFIG_CONSOLE_POLL
315static int ulite_get_poll_char(struct uart_port *port)
316{
317 if (!(ioread32be(port->membase + ULITE_STATUS)
318 & ULITE_STATUS_RXVALID))
319 return NO_POLL_CHAR;
320
321 return ioread32be(port->membase + ULITE_RX);
322}
323
324static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
325{
326 while (ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL)
327 cpu_relax();
328
329 /* write char to device */
330 iowrite32be(ch, port->membase + ULITE_TX);
331}
332#endif
333
334static struct uart_ops ulite_ops = {
335 .tx_empty = ulite_tx_empty,
336 .set_mctrl = ulite_set_mctrl,
337 .get_mctrl = ulite_get_mctrl,
338 .stop_tx = ulite_stop_tx,
339 .start_tx = ulite_start_tx,
340 .stop_rx = ulite_stop_rx,
341 .enable_ms = ulite_enable_ms,
342 .break_ctl = ulite_break_ctl,
343 .startup = ulite_startup,
344 .shutdown = ulite_shutdown,
345 .set_termios = ulite_set_termios,
346 .type = ulite_type,
347 .release_port = ulite_release_port,
348 .request_port = ulite_request_port,
349 .config_port = ulite_config_port,
350 .verify_port = ulite_verify_port,
351#ifdef CONFIG_CONSOLE_POLL
352 .poll_get_char = ulite_get_poll_char,
353 .poll_put_char = ulite_put_poll_char,
354#endif
355};
356
357/* ---------------------------------------------------------------------
358 * Console driver operations
359 */
360
361#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
362static void ulite_console_wait_tx(struct uart_port *port)
363{
364 int i;
365 u8 val;
366
367 /* Spin waiting for TX fifo to have space available */
368 for (i = 0; i < 100000; i++) {
369 val = ioread32be(port->membase + ULITE_STATUS);
370 if ((val & ULITE_STATUS_TXFULL) == 0)
371 break;
372 cpu_relax();
373 }
374}
375
376static void ulite_console_putchar(struct uart_port *port, int ch)
377{
378 ulite_console_wait_tx(port);
379 iowrite32be(ch, port->membase + ULITE_TX);
380}
381
382static void ulite_console_write(struct console *co, const char *s,
383 unsigned int count)
384{
385 struct uart_port *port = &ulite_ports[co->index];
386 unsigned long flags;
387 unsigned int ier;
388 int locked = 1;
389
390 if (oops_in_progress) {
391 locked = spin_trylock_irqsave(&port->lock, flags);
392 } else
393 spin_lock_irqsave(&port->lock, flags);
394
395 /* save and disable interrupt */
396 ier = ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
397 iowrite32be(0, port->membase + ULITE_CONTROL);
398
399 uart_console_write(port, s, count, ulite_console_putchar);
400
401 ulite_console_wait_tx(port);
402
403 /* restore interrupt state */
404 if (ier)
405 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
406
407 if (locked)
408 spin_unlock_irqrestore(&port->lock, flags);
409}
410
411static int __devinit ulite_console_setup(struct console *co, char *options)
412{
413 struct uart_port *port;
414 int baud = 9600;
415 int bits = 8;
416 int parity = 'n';
417 int flow = 'n';
418
419 if (co->index < 0 || co->index >= ULITE_NR_UARTS)
420 return -EINVAL;
421
422 port = &ulite_ports[co->index];
423
424 /* Has the device been initialized yet? */
425 if (!port->mapbase) {
426 pr_debug("console on ttyUL%i not present\n", co->index);
427 return -ENODEV;
428 }
429
430 /* not initialized yet? */
431 if (!port->membase) {
432 if (ulite_request_port(port))
433 return -ENODEV;
434 }
435
436 if (options)
437 uart_parse_options(options, &baud, &parity, &bits, &flow);
438
439 return uart_set_options(port, co, baud, parity, bits, flow);
440}
441
442static struct uart_driver ulite_uart_driver;
443
444static struct console ulite_console = {
445 .name = ULITE_NAME,
446 .write = ulite_console_write,
447 .device = uart_console_device,
448 .setup = ulite_console_setup,
449 .flags = CON_PRINTBUFFER,
450 .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
451 .data = &ulite_uart_driver,
452};
453
454static int __init ulite_console_init(void)
455{
456 register_console(&ulite_console);
457 return 0;
458}
459
460console_initcall(ulite_console_init);
461
462#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
463
464static struct uart_driver ulite_uart_driver = {
465 .owner = THIS_MODULE,
466 .driver_name = "uartlite",
467 .dev_name = ULITE_NAME,
468 .major = ULITE_MAJOR,
469 .minor = ULITE_MINOR,
470 .nr = ULITE_NR_UARTS,
471#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
472 .cons = &ulite_console,
473#endif
474};
475
476/* ---------------------------------------------------------------------
477 * Port assignment functions (mapping devices to uart_port structures)
478 */
479
480/** ulite_assign: register a uartlite device with the driver
481 *
482 * @dev: pointer to device structure
483 * @id: requested id number. Pass -1 for automatic port assignment
484 * @base: base address of uartlite registers
485 * @irq: irq number for uartlite
486 *
487 * Returns: 0 on success, <0 otherwise
488 */
489static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
490{
491 struct uart_port *port;
492 int rc;
493
494 /* if id = -1; then scan for a free id and use that */
495 if (id < 0) {
496 for (id = 0; id < ULITE_NR_UARTS; id++)
497 if (ulite_ports[id].mapbase == 0)
498 break;
499 }
500 if (id < 0 || id >= ULITE_NR_UARTS) {
501 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
502 return -EINVAL;
503 }
504
505 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
506 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
507 ULITE_NAME, id);
508 return -EBUSY;
509 }
510
511 port = &ulite_ports[id];
512
513 spin_lock_init(&port->lock);
514 port->fifosize = 16;
515 port->regshift = 2;
516 port->iotype = UPIO_MEM;
517 port->iobase = 1; /* mark port in use */
518 port->mapbase = base;
519 port->membase = NULL;
520 port->ops = &ulite_ops;
521 port->irq = irq;
522 port->flags = UPF_BOOT_AUTOCONF;
523 port->dev = dev;
524 port->type = PORT_UNKNOWN;
525 port->line = id;
526
527 dev_set_drvdata(dev, port);
528
529 /* Register the port */
530 rc = uart_add_one_port(&ulite_uart_driver, port);
531 if (rc) {
532 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
533 port->mapbase = 0;
534 dev_set_drvdata(dev, NULL);
535 return rc;
536 }
537
538 return 0;
539}
540
541/** ulite_release: register a uartlite device with the driver
542 *
543 * @dev: pointer to device structure
544 */
545static int __devexit ulite_release(struct device *dev)
546{
547 struct uart_port *port = dev_get_drvdata(dev);
548 int rc = 0;
549
550 if (port) {
551 rc = uart_remove_one_port(&ulite_uart_driver, port);
552 dev_set_drvdata(dev, NULL);
553 port->mapbase = 0;
554 }
555
556 return rc;
557}
558
559/* ---------------------------------------------------------------------
560 * Platform bus binding
561 */
562
563#if defined(CONFIG_OF)
564/* Match table for of_platform binding */
565static struct of_device_id ulite_of_match[] __devinitdata = {
566 { .compatible = "xlnx,opb-uartlite-1.00.b", },
567 { .compatible = "xlnx,xps-uartlite-1.00.a", },
568 {}
569};
570MODULE_DEVICE_TABLE(of, ulite_of_match);
571#else /* CONFIG_OF */
572#define ulite_of_match NULL
573#endif /* CONFIG_OF */
574
575static int __devinit ulite_probe(struct platform_device *pdev)
576{
577 struct resource *res, *res2;
578 int id = pdev->id;
579#ifdef CONFIG_OF
580 const __be32 *prop;
581
582 prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
583 if (prop)
584 id = be32_to_cpup(prop);
585#endif
586
587 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
588 if (!res)
589 return -ENODEV;
590
591 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
592 if (!res2)
593 return -ENODEV;
594
595 return ulite_assign(&pdev->dev, id, res->start, res2->start);
596}
597
598static int __devexit ulite_remove(struct platform_device *pdev)
599{
600 return ulite_release(&pdev->dev);
601}
602
603/* work with hotplug and coldplug */
604MODULE_ALIAS("platform:uartlite");
605
606static struct platform_driver ulite_platform_driver = {
607 .probe = ulite_probe,
608 .remove = __devexit_p(ulite_remove),
609 .driver = {
610 .owner = THIS_MODULE,
611 .name = "uartlite",
612 .of_match_table = ulite_of_match,
613 },
614};
615
616/* ---------------------------------------------------------------------
617 * Module setup/teardown
618 */
619
620int __init ulite_init(void)
621{
622 int ret;
623
624 pr_debug("uartlite: calling uart_register_driver()\n");
625 ret = uart_register_driver(&ulite_uart_driver);
626 if (ret)
627 goto err_uart;
628
629 pr_debug("uartlite: calling platform_driver_register()\n");
630 ret = platform_driver_register(&ulite_platform_driver);
631 if (ret)
632 goto err_plat;
633
634 return 0;
635
636err_plat:
637 uart_unregister_driver(&ulite_uart_driver);
638err_uart:
639 printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
640 return ret;
641}
642
643void __exit ulite_exit(void)
644{
645 platform_driver_unregister(&ulite_platform_driver);
646 uart_unregister_driver(&ulite_uart_driver);
647}
648
649module_init(ulite_init);
650module_exit(ulite_exit);
651
652MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
653MODULE_DESCRIPTION("Xilinx uartlite serial driver");
654MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ucc_uart.c b/drivers/tty/serial/ucc_uart.c
new file mode 100644
index 000000000000..c327218cad44
--- /dev/null
+++ b/drivers/tty/serial/ucc_uart.c
@@ -0,0 +1,1539 @@
1/*
2 * Freescale QUICC Engine UART device driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
6 * Copyright 2007 Freescale Semiconductor, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
10 *
11 * This driver adds support for UART devices via Freescale's QUICC Engine
12 * found on some Freescale SOCs.
13 *
14 * If Soft-UART support is needed but not already present, then this driver
15 * will request and upload the "Soft-UART" microcode upon probe. The
16 * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
17 * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
18 * (e.g. "11" for 1.1).
19 */
20
21#include <linux/module.h>
22#include <linux/serial.h>
23#include <linux/slab.h>
24#include <linux/serial_core.h>
25#include <linux/io.h>
26#include <linux/of_platform.h>
27#include <linux/dma-mapping.h>
28
29#include <linux/fs_uart_pd.h>
30#include <asm/ucc_slow.h>
31
32#include <linux/firmware.h>
33#include <asm/reg.h>
34
35/*
36 * The GUMR flag for Soft UART. This would normally be defined in qe.h,
37 * but Soft-UART is a hack and we want to keep everything related to it in
38 * this file.
39 */
40#define UCC_SLOW_GUMR_H_SUART 0x00004000 /* Soft-UART */
41
42/*
43 * soft_uart is 1 if we need to use Soft-UART mode
44 */
45static int soft_uart;
46/*
47 * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
48 */
49static int firmware_loaded;
50
51/* Enable this macro to configure all serial ports in internal loopback
52 mode */
53/* #define LOOPBACK */
54
55/* The major and minor device numbers are defined in
56 * http://www.lanana.org/docs/device-list/devices-2.6+.txt. For the QE
57 * UART, we have major number 204 and minor numbers 46 - 49, which are the
58 * same as for the CPM2. This decision was made because no Freescale part
59 * has both a CPM and a QE.
60 */
61#define SERIAL_QE_MAJOR 204
62#define SERIAL_QE_MINOR 46
63
64/* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
65#define UCC_MAX_UART 4
66
67/* The number of buffer descriptors for receiving characters. */
68#define RX_NUM_FIFO 4
69
70/* The number of buffer descriptors for transmitting characters. */
71#define TX_NUM_FIFO 4
72
73/* The maximum size of the character buffer for a single RX BD. */
74#define RX_BUF_SIZE 32
75
76/* The maximum size of the character buffer for a single TX BD. */
77#define TX_BUF_SIZE 32
78
79/*
80 * The number of jiffies to wait after receiving a close command before the
81 * device is actually closed. This allows the last few characters to be
82 * sent over the wire.
83 */
84#define UCC_WAIT_CLOSING 100
85
86struct ucc_uart_pram {
87 struct ucc_slow_pram common;
88 u8 res1[8]; /* reserved */
89 __be16 maxidl; /* Maximum idle chars */
90 __be16 idlc; /* temp idle counter */
91 __be16 brkcr; /* Break count register */
92 __be16 parec; /* receive parity error counter */
93 __be16 frmec; /* receive framing error counter */
94 __be16 nosec; /* receive noise counter */
95 __be16 brkec; /* receive break condition counter */
96 __be16 brkln; /* last received break length */
97 __be16 uaddr[2]; /* UART address character 1 & 2 */
98 __be16 rtemp; /* Temp storage */
99 __be16 toseq; /* Transmit out of sequence char */
100 __be16 cchars[8]; /* control characters 1-8 */
101 __be16 rccm; /* receive control character mask */
102 __be16 rccr; /* receive control character register */
103 __be16 rlbc; /* receive last break character */
104 __be16 res2; /* reserved */
105 __be32 res3; /* reserved, should be cleared */
106 u8 res4; /* reserved, should be cleared */
107 u8 res5[3]; /* reserved, should be cleared */
108 __be32 res6; /* reserved, should be cleared */
109 __be32 res7; /* reserved, should be cleared */
110 __be32 res8; /* reserved, should be cleared */
111 __be32 res9; /* reserved, should be cleared */
112 __be32 res10; /* reserved, should be cleared */
113 __be32 res11; /* reserved, should be cleared */
114 __be32 res12; /* reserved, should be cleared */
115 __be32 res13; /* reserved, should be cleared */
116/* The rest is for Soft-UART only */
117 __be16 supsmr; /* 0x90, Shadow UPSMR */
118 __be16 res92; /* 0x92, reserved, initialize to 0 */
119 __be32 rx_state; /* 0x94, RX state, initialize to 0 */
120 __be32 rx_cnt; /* 0x98, RX count, initialize to 0 */
121 u8 rx_length; /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
122 u8 rx_bitmark; /* 0x9D, reserved, initialize to 0 */
123 u8 rx_temp_dlst_qe; /* 0x9E, reserved, initialize to 0 */
124 u8 res14[0xBC - 0x9F]; /* reserved */
125 __be32 dump_ptr; /* 0xBC, Dump pointer */
126 __be32 rx_frame_rem; /* 0xC0, reserved, initialize to 0 */
127 u8 rx_frame_rem_size; /* 0xC4, reserved, initialize to 0 */
128 u8 tx_mode; /* 0xC5, mode, 0=AHDLC, 1=UART */
129 __be16 tx_state; /* 0xC6, TX state */
130 u8 res15[0xD0 - 0xC8]; /* reserved */
131 __be32 resD0; /* 0xD0, reserved, initialize to 0 */
132 u8 resD4; /* 0xD4, reserved, initialize to 0 */
133 __be16 resD5; /* 0xD5, reserved, initialize to 0 */
134} __attribute__ ((packed));
135
136/* SUPSMR definitions, for Soft-UART only */
137#define UCC_UART_SUPSMR_SL 0x8000
138#define UCC_UART_SUPSMR_RPM_MASK 0x6000
139#define UCC_UART_SUPSMR_RPM_ODD 0x0000
140#define UCC_UART_SUPSMR_RPM_LOW 0x2000
141#define UCC_UART_SUPSMR_RPM_EVEN 0x4000
142#define UCC_UART_SUPSMR_RPM_HIGH 0x6000
143#define UCC_UART_SUPSMR_PEN 0x1000
144#define UCC_UART_SUPSMR_TPM_MASK 0x0C00
145#define UCC_UART_SUPSMR_TPM_ODD 0x0000
146#define UCC_UART_SUPSMR_TPM_LOW 0x0400
147#define UCC_UART_SUPSMR_TPM_EVEN 0x0800
148#define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
149#define UCC_UART_SUPSMR_FRZ 0x0100
150#define UCC_UART_SUPSMR_UM_MASK 0x00c0
151#define UCC_UART_SUPSMR_UM_NORMAL 0x0000
152#define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
153#define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
154#define UCC_UART_SUPSMR_CL_MASK 0x0030
155#define UCC_UART_SUPSMR_CL_8 0x0030
156#define UCC_UART_SUPSMR_CL_7 0x0020
157#define UCC_UART_SUPSMR_CL_6 0x0010
158#define UCC_UART_SUPSMR_CL_5 0x0000
159
160#define UCC_UART_TX_STATE_AHDLC 0x00
161#define UCC_UART_TX_STATE_UART 0x01
162#define UCC_UART_TX_STATE_X1 0x00
163#define UCC_UART_TX_STATE_X16 0x80
164
165#define UCC_UART_PRAM_ALIGNMENT 0x100
166
167#define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
168#define NUM_CONTROL_CHARS 8
169
170/* Private per-port data structure */
171struct uart_qe_port {
172 struct uart_port port;
173 struct ucc_slow __iomem *uccp;
174 struct ucc_uart_pram __iomem *uccup;
175 struct ucc_slow_info us_info;
176 struct ucc_slow_private *us_private;
177 struct device_node *np;
178 unsigned int ucc_num; /* First ucc is 0, not 1 */
179
180 u16 rx_nrfifos;
181 u16 rx_fifosize;
182 u16 tx_nrfifos;
183 u16 tx_fifosize;
184 int wait_closing;
185 u32 flags;
186 struct qe_bd *rx_bd_base;
187 struct qe_bd *rx_cur;
188 struct qe_bd *tx_bd_base;
189 struct qe_bd *tx_cur;
190 unsigned char *tx_buf;
191 unsigned char *rx_buf;
192 void *bd_virt; /* virtual address of the BD buffers */
193 dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
194 unsigned int bd_size; /* size of BD buffer space */
195};
196
197static struct uart_driver ucc_uart_driver = {
198 .owner = THIS_MODULE,
199 .driver_name = "ucc_uart",
200 .dev_name = "ttyQE",
201 .major = SERIAL_QE_MAJOR,
202 .minor = SERIAL_QE_MINOR,
203 .nr = UCC_MAX_UART,
204};
205
206/*
207 * Virtual to physical address translation.
208 *
209 * Given the virtual address for a character buffer, this function returns
210 * the physical (DMA) equivalent.
211 */
212static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
213{
214 if (likely((addr >= qe_port->bd_virt)) &&
215 (addr < (qe_port->bd_virt + qe_port->bd_size)))
216 return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
217
218 /* something nasty happened */
219 printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
220 BUG();
221 return 0;
222}
223
224/*
225 * Physical to virtual address translation.
226 *
227 * Given the physical (DMA) address for a character buffer, this function
228 * returns the virtual equivalent.
229 */
230static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
231{
232 /* sanity check */
233 if (likely((addr >= qe_port->bd_dma_addr) &&
234 (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
235 return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
236
237 /* something nasty happened */
238 printk(KERN_ERR "%s: addr=%x\n", __func__, addr);
239 BUG();
240 return NULL;
241}
242
243/*
244 * Return 1 if the QE is done transmitting all buffers for this port
245 *
246 * This function scans each BD in sequence. If we find a BD that is not
247 * ready (READY=1), then we return 0 indicating that the QE is still sending
248 * data. If we reach the last BD (WRAP=1), then we know we've scanned
249 * the entire list, and all BDs are done.
250 */
251static unsigned int qe_uart_tx_empty(struct uart_port *port)
252{
253 struct uart_qe_port *qe_port =
254 container_of(port, struct uart_qe_port, port);
255 struct qe_bd *bdp = qe_port->tx_bd_base;
256
257 while (1) {
258 if (in_be16(&bdp->status) & BD_SC_READY)
259 /* This BD is not done, so return "not done" */
260 return 0;
261
262 if (in_be16(&bdp->status) & BD_SC_WRAP)
263 /*
264 * This BD is done and it's the last one, so return
265 * "done"
266 */
267 return 1;
268
269 bdp++;
270 };
271}
272
273/*
274 * Set the modem control lines
275 *
276 * Although the QE can control the modem control lines (e.g. CTS), we
277 * don't need that support. This function must exist, however, otherwise
278 * the kernel will panic.
279 */
280void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
281{
282}
283
284/*
285 * Get the current modem control line status
286 *
287 * Although the QE can control the modem control lines (e.g. CTS), this
288 * driver currently doesn't support that, so we always return Carrier
289 * Detect, Data Set Ready, and Clear To Send.
290 */
291static unsigned int qe_uart_get_mctrl(struct uart_port *port)
292{
293 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
294}
295
296/*
297 * Disable the transmit interrupt.
298 *
299 * Although this function is called "stop_tx", it does not actually stop
300 * transmission of data. Instead, it tells the QE to not generate an
301 * interrupt when the UCC is finished sending characters.
302 */
303static void qe_uart_stop_tx(struct uart_port *port)
304{
305 struct uart_qe_port *qe_port =
306 container_of(port, struct uart_qe_port, port);
307
308 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
309}
310
311/*
312 * Transmit as many characters to the HW as possible.
313 *
314 * This function will attempt to stuff of all the characters from the
315 * kernel's transmit buffer into TX BDs.
316 *
317 * A return value of non-zero indicates that it successfully stuffed all
318 * characters from the kernel buffer.
319 *
320 * A return value of zero indicates that there are still characters in the
321 * kernel's buffer that have not been transmitted, but there are no more BDs
322 * available. This function should be called again after a BD has been made
323 * available.
324 */
325static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
326{
327 struct qe_bd *bdp;
328 unsigned char *p;
329 unsigned int count;
330 struct uart_port *port = &qe_port->port;
331 struct circ_buf *xmit = &port->state->xmit;
332
333 bdp = qe_port->rx_cur;
334
335 /* Handle xon/xoff */
336 if (port->x_char) {
337 /* Pick next descriptor and fill from buffer */
338 bdp = qe_port->tx_cur;
339
340 p = qe2cpu_addr(bdp->buf, qe_port);
341
342 *p++ = port->x_char;
343 out_be16(&bdp->length, 1);
344 setbits16(&bdp->status, BD_SC_READY);
345 /* Get next BD. */
346 if (in_be16(&bdp->status) & BD_SC_WRAP)
347 bdp = qe_port->tx_bd_base;
348 else
349 bdp++;
350 qe_port->tx_cur = bdp;
351
352 port->icount.tx++;
353 port->x_char = 0;
354 return 1;
355 }
356
357 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
358 qe_uart_stop_tx(port);
359 return 0;
360 }
361
362 /* Pick next descriptor and fill from buffer */
363 bdp = qe_port->tx_cur;
364
365 while (!(in_be16(&bdp->status) & BD_SC_READY) &&
366 (xmit->tail != xmit->head)) {
367 count = 0;
368 p = qe2cpu_addr(bdp->buf, qe_port);
369 while (count < qe_port->tx_fifosize) {
370 *p++ = xmit->buf[xmit->tail];
371 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
372 port->icount.tx++;
373 count++;
374 if (xmit->head == xmit->tail)
375 break;
376 }
377
378 out_be16(&bdp->length, count);
379 setbits16(&bdp->status, BD_SC_READY);
380
381 /* Get next BD. */
382 if (in_be16(&bdp->status) & BD_SC_WRAP)
383 bdp = qe_port->tx_bd_base;
384 else
385 bdp++;
386 }
387 qe_port->tx_cur = bdp;
388
389 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
390 uart_write_wakeup(port);
391
392 if (uart_circ_empty(xmit)) {
393 /* The kernel buffer is empty, so turn off TX interrupts. We
394 don't need to be told when the QE is finished transmitting
395 the data. */
396 qe_uart_stop_tx(port);
397 return 0;
398 }
399
400 return 1;
401}
402
403/*
404 * Start transmitting data
405 *
406 * This function will start transmitting any available data, if the port
407 * isn't already transmitting data.
408 */
409static void qe_uart_start_tx(struct uart_port *port)
410{
411 struct uart_qe_port *qe_port =
412 container_of(port, struct uart_qe_port, port);
413
414 /* If we currently are transmitting, then just return */
415 if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
416 return;
417
418 /* Otherwise, pump the port and start transmission */
419 if (qe_uart_tx_pump(qe_port))
420 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
421}
422
423/*
424 * Stop transmitting data
425 */
426static void qe_uart_stop_rx(struct uart_port *port)
427{
428 struct uart_qe_port *qe_port =
429 container_of(port, struct uart_qe_port, port);
430
431 clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
432}
433
434/*
435 * Enable status change interrupts
436 *
437 * We don't support status change interrupts, but we need to define this
438 * function otherwise the kernel will panic.
439 */
440static void qe_uart_enable_ms(struct uart_port *port)
441{
442}
443
444/* Start or stop sending break signal
445 *
446 * This function controls the sending of a break signal. If break_state=1,
447 * then we start sending a break signal. If break_state=0, then we stop
448 * sending the break signal.
449 */
450static void qe_uart_break_ctl(struct uart_port *port, int break_state)
451{
452 struct uart_qe_port *qe_port =
453 container_of(port, struct uart_qe_port, port);
454
455 if (break_state)
456 ucc_slow_stop_tx(qe_port->us_private);
457 else
458 ucc_slow_restart_tx(qe_port->us_private);
459}
460
461/* ISR helper function for receiving character.
462 *
463 * This function is called by the ISR to handling receiving characters
464 */
465static void qe_uart_int_rx(struct uart_qe_port *qe_port)
466{
467 int i;
468 unsigned char ch, *cp;
469 struct uart_port *port = &qe_port->port;
470 struct tty_struct *tty = port->state->port.tty;
471 struct qe_bd *bdp;
472 u16 status;
473 unsigned int flg;
474
475 /* Just loop through the closed BDs and copy the characters into
476 * the buffer.
477 */
478 bdp = qe_port->rx_cur;
479 while (1) {
480 status = in_be16(&bdp->status);
481
482 /* If this one is empty, then we assume we've read them all */
483 if (status & BD_SC_EMPTY)
484 break;
485
486 /* get number of characters, and check space in RX buffer */
487 i = in_be16(&bdp->length);
488
489 /* If we don't have enough room in RX buffer for the entire BD,
490 * then we try later, which will be the next RX interrupt.
491 */
492 if (tty_buffer_request_room(tty, i) < i) {
493 dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
494 return;
495 }
496
497 /* get pointer */
498 cp = qe2cpu_addr(bdp->buf, qe_port);
499
500 /* loop through the buffer */
501 while (i-- > 0) {
502 ch = *cp++;
503 port->icount.rx++;
504 flg = TTY_NORMAL;
505
506 if (!i && status &
507 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
508 goto handle_error;
509 if (uart_handle_sysrq_char(port, ch))
510 continue;
511
512error_return:
513 tty_insert_flip_char(tty, ch, flg);
514
515 }
516
517 /* This BD is ready to be used again. Clear status. get next */
518 clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
519 BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
520 if (in_be16(&bdp->status) & BD_SC_WRAP)
521 bdp = qe_port->rx_bd_base;
522 else
523 bdp++;
524
525 }
526
527 /* Write back buffer pointer */
528 qe_port->rx_cur = bdp;
529
530 /* Activate BH processing */
531 tty_flip_buffer_push(tty);
532
533 return;
534
535 /* Error processing */
536
537handle_error:
538 /* Statistics */
539 if (status & BD_SC_BR)
540 port->icount.brk++;
541 if (status & BD_SC_PR)
542 port->icount.parity++;
543 if (status & BD_SC_FR)
544 port->icount.frame++;
545 if (status & BD_SC_OV)
546 port->icount.overrun++;
547
548 /* Mask out ignored conditions */
549 status &= port->read_status_mask;
550
551 /* Handle the remaining ones */
552 if (status & BD_SC_BR)
553 flg = TTY_BREAK;
554 else if (status & BD_SC_PR)
555 flg = TTY_PARITY;
556 else if (status & BD_SC_FR)
557 flg = TTY_FRAME;
558
559 /* Overrun does not affect the current character ! */
560 if (status & BD_SC_OV)
561 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
562#ifdef SUPPORT_SYSRQ
563 port->sysrq = 0;
564#endif
565 goto error_return;
566}
567
568/* Interrupt handler
569 *
570 * This interrupt handler is called after a BD is processed.
571 */
572static irqreturn_t qe_uart_int(int irq, void *data)
573{
574 struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
575 struct ucc_slow __iomem *uccp = qe_port->uccp;
576 u16 events;
577
578 /* Clear the interrupts */
579 events = in_be16(&uccp->ucce);
580 out_be16(&uccp->ucce, events);
581
582 if (events & UCC_UART_UCCE_BRKE)
583 uart_handle_break(&qe_port->port);
584
585 if (events & UCC_UART_UCCE_RX)
586 qe_uart_int_rx(qe_port);
587
588 if (events & UCC_UART_UCCE_TX)
589 qe_uart_tx_pump(qe_port);
590
591 return events ? IRQ_HANDLED : IRQ_NONE;
592}
593
594/* Initialize buffer descriptors
595 *
596 * This function initializes all of the RX and TX buffer descriptors.
597 */
598static void qe_uart_initbd(struct uart_qe_port *qe_port)
599{
600 int i;
601 void *bd_virt;
602 struct qe_bd *bdp;
603
604 /* Set the physical address of the host memory buffers in the buffer
605 * descriptors, and the virtual address for us to work with.
606 */
607 bd_virt = qe_port->bd_virt;
608 bdp = qe_port->rx_bd_base;
609 qe_port->rx_cur = qe_port->rx_bd_base;
610 for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
611 out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
612 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
613 out_be16(&bdp->length, 0);
614 bd_virt += qe_port->rx_fifosize;
615 bdp++;
616 }
617
618 /* */
619 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
620 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
621 out_be16(&bdp->length, 0);
622
623 /* Set the physical address of the host memory
624 * buffers in the buffer descriptors, and the
625 * virtual address for us to work with.
626 */
627 bd_virt = qe_port->bd_virt +
628 L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
629 qe_port->tx_cur = qe_port->tx_bd_base;
630 bdp = qe_port->tx_bd_base;
631 for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
632 out_be16(&bdp->status, BD_SC_INTRPT);
633 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
634 out_be16(&bdp->length, 0);
635 bd_virt += qe_port->tx_fifosize;
636 bdp++;
637 }
638
639 /* Loopback requires the preamble bit to be set on the first TX BD */
640#ifdef LOOPBACK
641 setbits16(&qe_port->tx_cur->status, BD_SC_P);
642#endif
643
644 out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
645 out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
646 out_be16(&bdp->length, 0);
647}
648
649/*
650 * Initialize a UCC for UART.
651 *
652 * This function configures a given UCC to be used as a UART device. Basic
653 * UCC initialization is handled in qe_uart_request_port(). This function
654 * does all the UART-specific stuff.
655 */
656static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
657{
658 u32 cecr_subblock;
659 struct ucc_slow __iomem *uccp = qe_port->uccp;
660 struct ucc_uart_pram *uccup = qe_port->uccup;
661
662 unsigned int i;
663
664 /* First, disable TX and RX in the UCC */
665 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
666
667 /* Program the UCC UART parameter RAM */
668 out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
669 out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
670 out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
671 out_be16(&uccup->maxidl, 0x10);
672 out_be16(&uccup->brkcr, 1);
673 out_be16(&uccup->parec, 0);
674 out_be16(&uccup->frmec, 0);
675 out_be16(&uccup->nosec, 0);
676 out_be16(&uccup->brkec, 0);
677 out_be16(&uccup->uaddr[0], 0);
678 out_be16(&uccup->uaddr[1], 0);
679 out_be16(&uccup->toseq, 0);
680 for (i = 0; i < 8; i++)
681 out_be16(&uccup->cchars[i], 0xC000);
682 out_be16(&uccup->rccm, 0xc0ff);
683
684 /* Configure the GUMR registers for UART */
685 if (soft_uart) {
686 /* Soft-UART requires a 1X multiplier for TX */
687 clrsetbits_be32(&uccp->gumr_l,
688 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
689 UCC_SLOW_GUMR_L_RDCR_MASK,
690 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
691 UCC_SLOW_GUMR_L_RDCR_16);
692
693 clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
694 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
695 } else {
696 clrsetbits_be32(&uccp->gumr_l,
697 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
698 UCC_SLOW_GUMR_L_RDCR_MASK,
699 UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
700 UCC_SLOW_GUMR_L_RDCR_16);
701
702 clrsetbits_be32(&uccp->gumr_h,
703 UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
704 UCC_SLOW_GUMR_H_RFW);
705 }
706
707#ifdef LOOPBACK
708 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
709 UCC_SLOW_GUMR_L_DIAG_LOOP);
710 clrsetbits_be32(&uccp->gumr_h,
711 UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
712 UCC_SLOW_GUMR_H_CDS);
713#endif
714
715 /* Disable rx interrupts and clear all pending events. */
716 out_be16(&uccp->uccm, 0);
717 out_be16(&uccp->ucce, 0xffff);
718 out_be16(&uccp->udsr, 0x7e7e);
719
720 /* Initialize UPSMR */
721 out_be16(&uccp->upsmr, 0);
722
723 if (soft_uart) {
724 out_be16(&uccup->supsmr, 0x30);
725 out_be16(&uccup->res92, 0);
726 out_be32(&uccup->rx_state, 0);
727 out_be32(&uccup->rx_cnt, 0);
728 out_8(&uccup->rx_bitmark, 0);
729 out_8(&uccup->rx_length, 10);
730 out_be32(&uccup->dump_ptr, 0x4000);
731 out_8(&uccup->rx_temp_dlst_qe, 0);
732 out_be32(&uccup->rx_frame_rem, 0);
733 out_8(&uccup->rx_frame_rem_size, 0);
734 /* Soft-UART requires TX to be 1X */
735 out_8(&uccup->tx_mode,
736 UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
737 out_be16(&uccup->tx_state, 0);
738 out_8(&uccup->resD4, 0);
739 out_be16(&uccup->resD5, 0);
740
741 /* Set UART mode.
742 * Enable receive and transmit.
743 */
744
745 /* From the microcode errata:
746 * 1.GUMR_L register, set mode=0010 (QMC).
747 * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
748 * 3.Set GUMR_H[19:20] (Transparent mode)
749 * 4.Clear GUMR_H[26] (RFW)
750 * ...
751 * 6.Receiver must use 16x over sampling
752 */
753 clrsetbits_be32(&uccp->gumr_l,
754 UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
755 UCC_SLOW_GUMR_L_RDCR_MASK,
756 UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
757 UCC_SLOW_GUMR_L_RDCR_16);
758
759 clrsetbits_be32(&uccp->gumr_h,
760 UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
761 UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
762 UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
763
764#ifdef LOOPBACK
765 clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
766 UCC_SLOW_GUMR_L_DIAG_LOOP);
767 clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
768 UCC_SLOW_GUMR_H_CDS);
769#endif
770
771 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
772 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
773 QE_CR_PROTOCOL_UNSPECIFIED, 0);
774 } else {
775 cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
776 qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
777 QE_CR_PROTOCOL_UART, 0);
778 }
779}
780
781/*
782 * Initialize the port.
783 */
784static int qe_uart_startup(struct uart_port *port)
785{
786 struct uart_qe_port *qe_port =
787 container_of(port, struct uart_qe_port, port);
788 int ret;
789
790 /*
791 * If we're using Soft-UART mode, then we need to make sure the
792 * firmware has been uploaded first.
793 */
794 if (soft_uart && !firmware_loaded) {
795 dev_err(port->dev, "Soft-UART firmware not uploaded\n");
796 return -ENODEV;
797 }
798
799 qe_uart_initbd(qe_port);
800 qe_uart_init_ucc(qe_port);
801
802 /* Install interrupt handler. */
803 ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
804 qe_port);
805 if (ret) {
806 dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
807 return ret;
808 }
809
810 /* Startup rx-int */
811 setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
812 ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
813
814 return 0;
815}
816
817/*
818 * Shutdown the port.
819 */
820static void qe_uart_shutdown(struct uart_port *port)
821{
822 struct uart_qe_port *qe_port =
823 container_of(port, struct uart_qe_port, port);
824 struct ucc_slow __iomem *uccp = qe_port->uccp;
825 unsigned int timeout = 20;
826
827 /* Disable RX and TX */
828
829 /* Wait for all the BDs marked sent */
830 while (!qe_uart_tx_empty(port)) {
831 if (!--timeout) {
832 dev_warn(port->dev, "shutdown timeout\n");
833 break;
834 }
835 set_current_state(TASK_UNINTERRUPTIBLE);
836 schedule_timeout(2);
837 }
838
839 if (qe_port->wait_closing) {
840 /* Wait a bit longer */
841 set_current_state(TASK_UNINTERRUPTIBLE);
842 schedule_timeout(qe_port->wait_closing);
843 }
844
845 /* Stop uarts */
846 ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
847 clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
848
849 /* Shut them really down and reinit buffer descriptors */
850 ucc_slow_graceful_stop_tx(qe_port->us_private);
851 qe_uart_initbd(qe_port);
852
853 free_irq(port->irq, qe_port);
854}
855
856/*
857 * Set the serial port parameters.
858 */
859static void qe_uart_set_termios(struct uart_port *port,
860 struct ktermios *termios, struct ktermios *old)
861{
862 struct uart_qe_port *qe_port =
863 container_of(port, struct uart_qe_port, port);
864 struct ucc_slow __iomem *uccp = qe_port->uccp;
865 unsigned int baud;
866 unsigned long flags;
867 u16 upsmr = in_be16(&uccp->upsmr);
868 struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
869 u16 supsmr = in_be16(&uccup->supsmr);
870 u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
871
872 /* Character length programmed into the mode register is the
873 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
874 * 1 or 2 stop bits, minus 1.
875 * The value 'bits' counts this for us.
876 */
877
878 /* byte size */
879 upsmr &= UCC_UART_UPSMR_CL_MASK;
880 supsmr &= UCC_UART_SUPSMR_CL_MASK;
881
882 switch (termios->c_cflag & CSIZE) {
883 case CS5:
884 upsmr |= UCC_UART_UPSMR_CL_5;
885 supsmr |= UCC_UART_SUPSMR_CL_5;
886 char_length += 5;
887 break;
888 case CS6:
889 upsmr |= UCC_UART_UPSMR_CL_6;
890 supsmr |= UCC_UART_SUPSMR_CL_6;
891 char_length += 6;
892 break;
893 case CS7:
894 upsmr |= UCC_UART_UPSMR_CL_7;
895 supsmr |= UCC_UART_SUPSMR_CL_7;
896 char_length += 7;
897 break;
898 default: /* case CS8 */
899 upsmr |= UCC_UART_UPSMR_CL_8;
900 supsmr |= UCC_UART_SUPSMR_CL_8;
901 char_length += 8;
902 break;
903 }
904
905 /* If CSTOPB is set, we want two stop bits */
906 if (termios->c_cflag & CSTOPB) {
907 upsmr |= UCC_UART_UPSMR_SL;
908 supsmr |= UCC_UART_SUPSMR_SL;
909 char_length++; /* + SL */
910 }
911
912 if (termios->c_cflag & PARENB) {
913 upsmr |= UCC_UART_UPSMR_PEN;
914 supsmr |= UCC_UART_SUPSMR_PEN;
915 char_length++; /* + PEN */
916
917 if (!(termios->c_cflag & PARODD)) {
918 upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
919 UCC_UART_UPSMR_TPM_MASK);
920 upsmr |= UCC_UART_UPSMR_RPM_EVEN |
921 UCC_UART_UPSMR_TPM_EVEN;
922 supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
923 UCC_UART_SUPSMR_TPM_MASK);
924 supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
925 UCC_UART_SUPSMR_TPM_EVEN;
926 }
927 }
928
929 /*
930 * Set up parity check flag
931 */
932 port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
933 if (termios->c_iflag & INPCK)
934 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
935 if (termios->c_iflag & (BRKINT | PARMRK))
936 port->read_status_mask |= BD_SC_BR;
937
938 /*
939 * Characters to ignore
940 */
941 port->ignore_status_mask = 0;
942 if (termios->c_iflag & IGNPAR)
943 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
944 if (termios->c_iflag & IGNBRK) {
945 port->ignore_status_mask |= BD_SC_BR;
946 /*
947 * If we're ignore parity and break indicators, ignore
948 * overruns too. (For real raw support).
949 */
950 if (termios->c_iflag & IGNPAR)
951 port->ignore_status_mask |= BD_SC_OV;
952 }
953 /*
954 * !!! ignore all characters if CREAD is not set
955 */
956 if ((termios->c_cflag & CREAD) == 0)
957 port->read_status_mask &= ~BD_SC_EMPTY;
958
959 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
960
961 /* Do we really need a spinlock here? */
962 spin_lock_irqsave(&port->lock, flags);
963
964 out_be16(&uccp->upsmr, upsmr);
965 if (soft_uart) {
966 out_be16(&uccup->supsmr, supsmr);
967 out_8(&uccup->rx_length, char_length);
968
969 /* Soft-UART requires a 1X multiplier for TX */
970 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
971 qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
972 } else {
973 qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
974 qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
975 }
976
977 spin_unlock_irqrestore(&port->lock, flags);
978}
979
980/*
981 * Return a pointer to a string that describes what kind of port this is.
982 */
983static const char *qe_uart_type(struct uart_port *port)
984{
985 return "QE";
986}
987
988/*
989 * Allocate any memory and I/O resources required by the port.
990 */
991static int qe_uart_request_port(struct uart_port *port)
992{
993 int ret;
994 struct uart_qe_port *qe_port =
995 container_of(port, struct uart_qe_port, port);
996 struct ucc_slow_info *us_info = &qe_port->us_info;
997 struct ucc_slow_private *uccs;
998 unsigned int rx_size, tx_size;
999 void *bd_virt;
1000 dma_addr_t bd_dma_addr = 0;
1001
1002 ret = ucc_slow_init(us_info, &uccs);
1003 if (ret) {
1004 dev_err(port->dev, "could not initialize UCC%u\n",
1005 qe_port->ucc_num);
1006 return ret;
1007 }
1008
1009 qe_port->us_private = uccs;
1010 qe_port->uccp = uccs->us_regs;
1011 qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
1012 qe_port->rx_bd_base = uccs->rx_bd;
1013 qe_port->tx_bd_base = uccs->tx_bd;
1014
1015 /*
1016 * Allocate the transmit and receive data buffers.
1017 */
1018
1019 rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
1020 tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
1021
1022 bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
1023 GFP_KERNEL);
1024 if (!bd_virt) {
1025 dev_err(port->dev, "could not allocate buffer descriptors\n");
1026 return -ENOMEM;
1027 }
1028
1029 qe_port->bd_virt = bd_virt;
1030 qe_port->bd_dma_addr = bd_dma_addr;
1031 qe_port->bd_size = rx_size + tx_size;
1032
1033 qe_port->rx_buf = bd_virt;
1034 qe_port->tx_buf = qe_port->rx_buf + rx_size;
1035
1036 return 0;
1037}
1038
1039/*
1040 * Configure the port.
1041 *
1042 * We say we're a CPM-type port because that's mostly true. Once the device
1043 * is configured, this driver operates almost identically to the CPM serial
1044 * driver.
1045 */
1046static void qe_uart_config_port(struct uart_port *port, int flags)
1047{
1048 if (flags & UART_CONFIG_TYPE) {
1049 port->type = PORT_CPM;
1050 qe_uart_request_port(port);
1051 }
1052}
1053
1054/*
1055 * Release any memory and I/O resources that were allocated in
1056 * qe_uart_request_port().
1057 */
1058static void qe_uart_release_port(struct uart_port *port)
1059{
1060 struct uart_qe_port *qe_port =
1061 container_of(port, struct uart_qe_port, port);
1062 struct ucc_slow_private *uccs = qe_port->us_private;
1063
1064 dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
1065 qe_port->bd_dma_addr);
1066
1067 ucc_slow_free(uccs);
1068}
1069
1070/*
1071 * Verify that the data in serial_struct is suitable for this device.
1072 */
1073static int qe_uart_verify_port(struct uart_port *port,
1074 struct serial_struct *ser)
1075{
1076 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
1077 return -EINVAL;
1078
1079 if (ser->irq < 0 || ser->irq >= nr_irqs)
1080 return -EINVAL;
1081
1082 if (ser->baud_base < 9600)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087/* UART operations
1088 *
1089 * Details on these functions can be found in Documentation/serial/driver
1090 */
1091static struct uart_ops qe_uart_pops = {
1092 .tx_empty = qe_uart_tx_empty,
1093 .set_mctrl = qe_uart_set_mctrl,
1094 .get_mctrl = qe_uart_get_mctrl,
1095 .stop_tx = qe_uart_stop_tx,
1096 .start_tx = qe_uart_start_tx,
1097 .stop_rx = qe_uart_stop_rx,
1098 .enable_ms = qe_uart_enable_ms,
1099 .break_ctl = qe_uart_break_ctl,
1100 .startup = qe_uart_startup,
1101 .shutdown = qe_uart_shutdown,
1102 .set_termios = qe_uart_set_termios,
1103 .type = qe_uart_type,
1104 .release_port = qe_uart_release_port,
1105 .request_port = qe_uart_request_port,
1106 .config_port = qe_uart_config_port,
1107 .verify_port = qe_uart_verify_port,
1108};
1109
1110/*
1111 * Obtain the SOC model number and revision level
1112 *
1113 * This function parses the device tree to obtain the SOC model. It then
1114 * reads the SVR register to the revision.
1115 *
1116 * The device tree stores the SOC model two different ways.
1117 *
1118 * The new way is:
1119 *
1120 * cpu@0 {
1121 * compatible = "PowerPC,8323";
1122 * device_type = "cpu";
1123 * ...
1124 *
1125 *
1126 * The old way is:
1127 * PowerPC,8323@0 {
1128 * device_type = "cpu";
1129 * ...
1130 *
1131 * This code first checks the new way, and then the old way.
1132 */
1133static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
1134{
1135 struct device_node *np;
1136 const char *soc_string;
1137 unsigned int svr;
1138 unsigned int soc;
1139
1140 /* Find the CPU node */
1141 np = of_find_node_by_type(NULL, "cpu");
1142 if (!np)
1143 return 0;
1144 /* Find the compatible property */
1145 soc_string = of_get_property(np, "compatible", NULL);
1146 if (!soc_string)
1147 /* No compatible property, so try the name. */
1148 soc_string = np->name;
1149
1150 /* Extract the SOC number from the "PowerPC," string */
1151 if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
1152 return 0;
1153
1154 /* Get the revision from the SVR */
1155 svr = mfspr(SPRN_SVR);
1156 *rev_h = (svr >> 4) & 0xf;
1157 *rev_l = svr & 0xf;
1158
1159 return soc;
1160}
1161
1162/*
1163 * requst_firmware_nowait() callback function
1164 *
1165 * This function is called by the kernel when a firmware is made available,
1166 * or if it times out waiting for the firmware.
1167 */
1168static void uart_firmware_cont(const struct firmware *fw, void *context)
1169{
1170 struct qe_firmware *firmware;
1171 struct device *dev = context;
1172 int ret;
1173
1174 if (!fw) {
1175 dev_err(dev, "firmware not found\n");
1176 return;
1177 }
1178
1179 firmware = (struct qe_firmware *) fw->data;
1180
1181 if (firmware->header.length != fw->size) {
1182 dev_err(dev, "invalid firmware\n");
1183 goto out;
1184 }
1185
1186 ret = qe_upload_firmware(firmware);
1187 if (ret) {
1188 dev_err(dev, "could not load firmware\n");
1189 goto out;
1190 }
1191
1192 firmware_loaded = 1;
1193 out:
1194 release_firmware(fw);
1195}
1196
1197static int ucc_uart_probe(struct platform_device *ofdev)
1198{
1199 struct device_node *np = ofdev->dev.of_node;
1200 const unsigned int *iprop; /* Integer OF properties */
1201 const char *sprop; /* String OF properties */
1202 struct uart_qe_port *qe_port = NULL;
1203 struct resource res;
1204 int ret;
1205
1206 /*
1207 * Determine if we need Soft-UART mode
1208 */
1209 if (of_find_property(np, "soft-uart", NULL)) {
1210 dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1211 soft_uart = 1;
1212 }
1213
1214 /*
1215 * If we are using Soft-UART, determine if we need to upload the
1216 * firmware, too.
1217 */
1218 if (soft_uart) {
1219 struct qe_firmware_info *qe_fw_info;
1220
1221 qe_fw_info = qe_get_firmware_info();
1222
1223 /* Check if the firmware has been uploaded. */
1224 if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1225 firmware_loaded = 1;
1226 } else {
1227 char filename[32];
1228 unsigned int soc;
1229 unsigned int rev_h;
1230 unsigned int rev_l;
1231
1232 soc = soc_info(&rev_h, &rev_l);
1233 if (!soc) {
1234 dev_err(&ofdev->dev, "unknown CPU model\n");
1235 return -ENXIO;
1236 }
1237 sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1238 soc, rev_h, rev_l);
1239
1240 dev_info(&ofdev->dev, "waiting for firmware %s\n",
1241 filename);
1242
1243 /*
1244 * We call request_firmware_nowait instead of
1245 * request_firmware so that the driver can load and
1246 * initialize the ports without holding up the rest of
1247 * the kernel. If hotplug support is enabled in the
1248 * kernel, then we use it.
1249 */
1250 ret = request_firmware_nowait(THIS_MODULE,
1251 FW_ACTION_HOTPLUG, filename, &ofdev->dev,
1252 GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
1253 if (ret) {
1254 dev_err(&ofdev->dev,
1255 "could not load firmware %s\n",
1256 filename);
1257 return ret;
1258 }
1259 }
1260 }
1261
1262 qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1263 if (!qe_port) {
1264 dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1265 return -ENOMEM;
1266 }
1267
1268 /* Search for IRQ and mapbase */
1269 ret = of_address_to_resource(np, 0, &res);
1270 if (ret) {
1271 dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1272 goto out_free;
1273 }
1274 if (!res.start) {
1275 dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1276 ret = -EINVAL;
1277 goto out_free;
1278 }
1279 qe_port->port.mapbase = res.start;
1280
1281 /* Get the UCC number (device ID) */
1282 /* UCCs are numbered 1-7 */
1283 iprop = of_get_property(np, "cell-index", NULL);
1284 if (!iprop) {
1285 iprop = of_get_property(np, "device-id", NULL);
1286 if (!iprop) {
1287 dev_err(&ofdev->dev, "UCC is unspecified in "
1288 "device tree\n");
1289 ret = -EINVAL;
1290 goto out_free;
1291 }
1292 }
1293
1294 if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
1295 dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
1296 ret = -ENODEV;
1297 goto out_free;
1298 }
1299 qe_port->ucc_num = *iprop - 1;
1300
1301 /*
1302 * In the future, we should not require the BRG to be specified in the
1303 * device tree. If no clock-source is specified, then just pick a BRG
1304 * to use. This requires a new QE library function that manages BRG
1305 * assignments.
1306 */
1307
1308 sprop = of_get_property(np, "rx-clock-name", NULL);
1309 if (!sprop) {
1310 dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1311 ret = -ENODEV;
1312 goto out_free;
1313 }
1314
1315 qe_port->us_info.rx_clock = qe_clock_source(sprop);
1316 if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1317 (qe_port->us_info.rx_clock > QE_BRG16)) {
1318 dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1319 ret = -ENODEV;
1320 goto out_free;
1321 }
1322
1323#ifdef LOOPBACK
1324 /* In internal loopback mode, TX and RX must use the same clock */
1325 qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1326#else
1327 sprop = of_get_property(np, "tx-clock-name", NULL);
1328 if (!sprop) {
1329 dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1330 ret = -ENODEV;
1331 goto out_free;
1332 }
1333 qe_port->us_info.tx_clock = qe_clock_source(sprop);
1334#endif
1335 if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1336 (qe_port->us_info.tx_clock > QE_BRG16)) {
1337 dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1338 ret = -ENODEV;
1339 goto out_free;
1340 }
1341
1342 /* Get the port number, numbered 0-3 */
1343 iprop = of_get_property(np, "port-number", NULL);
1344 if (!iprop) {
1345 dev_err(&ofdev->dev, "missing port-number in device tree\n");
1346 ret = -EINVAL;
1347 goto out_free;
1348 }
1349 qe_port->port.line = *iprop;
1350 if (qe_port->port.line >= UCC_MAX_UART) {
1351 dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1352 UCC_MAX_UART - 1);
1353 ret = -EINVAL;
1354 goto out_free;
1355 }
1356
1357 qe_port->port.irq = irq_of_parse_and_map(np, 0);
1358 if (qe_port->port.irq == NO_IRQ) {
1359 dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1360 qe_port->ucc_num + 1);
1361 ret = -EINVAL;
1362 goto out_free;
1363 }
1364
1365 /*
1366 * Newer device trees have an "fsl,qe" compatible property for the QE
1367 * node, but we still need to support older device trees.
1368 */
1369 np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1370 if (!np) {
1371 np = of_find_node_by_type(NULL, "qe");
1372 if (!np) {
1373 dev_err(&ofdev->dev, "could not find 'qe' node\n");
1374 ret = -EINVAL;
1375 goto out_free;
1376 }
1377 }
1378
1379 iprop = of_get_property(np, "brg-frequency", NULL);
1380 if (!iprop) {
1381 dev_err(&ofdev->dev,
1382 "missing brg-frequency in device tree\n");
1383 ret = -EINVAL;
1384 goto out_np;
1385 }
1386
1387 if (*iprop)
1388 qe_port->port.uartclk = *iprop;
1389 else {
1390 /*
1391 * Older versions of U-Boot do not initialize the brg-frequency
1392 * property, so in this case we assume the BRG frequency is
1393 * half the QE bus frequency.
1394 */
1395 iprop = of_get_property(np, "bus-frequency", NULL);
1396 if (!iprop) {
1397 dev_err(&ofdev->dev,
1398 "missing QE bus-frequency in device tree\n");
1399 ret = -EINVAL;
1400 goto out_np;
1401 }
1402 if (*iprop)
1403 qe_port->port.uartclk = *iprop / 2;
1404 else {
1405 dev_err(&ofdev->dev,
1406 "invalid QE bus-frequency in device tree\n");
1407 ret = -EINVAL;
1408 goto out_np;
1409 }
1410 }
1411
1412 spin_lock_init(&qe_port->port.lock);
1413 qe_port->np = np;
1414 qe_port->port.dev = &ofdev->dev;
1415 qe_port->port.ops = &qe_uart_pops;
1416 qe_port->port.iotype = UPIO_MEM;
1417
1418 qe_port->tx_nrfifos = TX_NUM_FIFO;
1419 qe_port->tx_fifosize = TX_BUF_SIZE;
1420 qe_port->rx_nrfifos = RX_NUM_FIFO;
1421 qe_port->rx_fifosize = RX_BUF_SIZE;
1422
1423 qe_port->wait_closing = UCC_WAIT_CLOSING;
1424 qe_port->port.fifosize = 512;
1425 qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1426
1427 qe_port->us_info.ucc_num = qe_port->ucc_num;
1428 qe_port->us_info.regs = (phys_addr_t) res.start;
1429 qe_port->us_info.irq = qe_port->port.irq;
1430
1431 qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1432 qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1433
1434 /* Make sure ucc_slow_init() initializes both TX and RX */
1435 qe_port->us_info.init_tx = 1;
1436 qe_port->us_info.init_rx = 1;
1437
1438 /* Add the port to the uart sub-system. This will cause
1439 * qe_uart_config_port() to be called, so the us_info structure must
1440 * be initialized.
1441 */
1442 ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1443 if (ret) {
1444 dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1445 qe_port->port.line);
1446 goto out_np;
1447 }
1448
1449 dev_set_drvdata(&ofdev->dev, qe_port);
1450
1451 dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1452 qe_port->ucc_num + 1, qe_port->port.line);
1453
1454 /* Display the mknod command for this device */
1455 dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1456 qe_port->port.line, SERIAL_QE_MAJOR,
1457 SERIAL_QE_MINOR + qe_port->port.line);
1458
1459 return 0;
1460out_np:
1461 of_node_put(np);
1462out_free:
1463 kfree(qe_port);
1464 return ret;
1465}
1466
1467static int ucc_uart_remove(struct platform_device *ofdev)
1468{
1469 struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
1470
1471 dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1472
1473 uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1474
1475 dev_set_drvdata(&ofdev->dev, NULL);
1476 kfree(qe_port);
1477
1478 return 0;
1479}
1480
1481static struct of_device_id ucc_uart_match[] = {
1482 {
1483 .type = "serial",
1484 .compatible = "ucc_uart",
1485 },
1486 {},
1487};
1488MODULE_DEVICE_TABLE(of, ucc_uart_match);
1489
1490static struct platform_driver ucc_uart_of_driver = {
1491 .driver = {
1492 .name = "ucc_uart",
1493 .owner = THIS_MODULE,
1494 .of_match_table = ucc_uart_match,
1495 },
1496 .probe = ucc_uart_probe,
1497 .remove = ucc_uart_remove,
1498};
1499
1500static int __init ucc_uart_init(void)
1501{
1502 int ret;
1503
1504 printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1505#ifdef LOOPBACK
1506 printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1507#endif
1508
1509 ret = uart_register_driver(&ucc_uart_driver);
1510 if (ret) {
1511 printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1512 return ret;
1513 }
1514
1515 ret = platform_driver_register(&ucc_uart_of_driver);
1516 if (ret)
1517 printk(KERN_ERR
1518 "ucc-uart: could not register platform driver\n");
1519
1520 return ret;
1521}
1522
1523static void __exit ucc_uart_exit(void)
1524{
1525 printk(KERN_INFO
1526 "Freescale QUICC Engine UART device driver unloading\n");
1527
1528 platform_driver_unregister(&ucc_uart_of_driver);
1529 uart_unregister_driver(&ucc_uart_driver);
1530}
1531
1532module_init(ucc_uart_init);
1533module_exit(ucc_uart_exit);
1534
1535MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1536MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1537MODULE_LICENSE("GPL v2");
1538MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1539
diff --git a/drivers/tty/serial/vr41xx_siu.c b/drivers/tty/serial/vr41xx_siu.c
new file mode 100644
index 000000000000..3beb6ab4fa68
--- /dev/null
+++ b/drivers/tty/serial/vr41xx_siu.c
@@ -0,0 +1,978 @@
1/*
2 * Driver for NEC VR4100 series Serial Interface Unit.
3 *
4 * Copyright (C) 2004-2008 Yoichi Yuasa <yuasa@linux-mips.org>
5 *
6 * Based on drivers/serial/8250.c, by Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/console.h>
28#include <linux/errno.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/ioport.h>
32#include <linux/module.h>
33#include <linux/platform_device.h>
34#include <linux/serial.h>
35#include <linux/serial_core.h>
36#include <linux/serial_reg.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40#include <asm/io.h>
41#include <asm/vr41xx/siu.h>
42#include <asm/vr41xx/vr41xx.h>
43
44#define SIU_BAUD_BASE 1152000
45#define SIU_MAJOR 204
46#define SIU_MINOR_BASE 82
47
48#define RX_MAX_COUNT 256
49#define TX_MAX_COUNT 15
50
51#define SIUIRSEL 0x08
52 #define TMICMODE 0x20
53 #define TMICTX 0x10
54 #define IRMSEL 0x0c
55 #define IRMSEL_HP 0x08
56 #define IRMSEL_TEMIC 0x04
57 #define IRMSEL_SHARP 0x00
58 #define IRUSESEL 0x02
59 #define SIRSEL 0x01
60
61static struct uart_port siu_uart_ports[SIU_PORTS_MAX] = {
62 [0 ... SIU_PORTS_MAX-1] = {
63 .lock = __SPIN_LOCK_UNLOCKED(siu_uart_ports->lock),
64 .irq = -1,
65 },
66};
67
68#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
69static uint8_t lsr_break_flag[SIU_PORTS_MAX];
70#endif
71
72#define siu_read(port, offset) readb((port)->membase + (offset))
73#define siu_write(port, offset, value) writeb((value), (port)->membase + (offset))
74
75void vr41xx_select_siu_interface(siu_interface_t interface)
76{
77 struct uart_port *port;
78 unsigned long flags;
79 uint8_t irsel;
80
81 port = &siu_uart_ports[0];
82
83 spin_lock_irqsave(&port->lock, flags);
84
85 irsel = siu_read(port, SIUIRSEL);
86 if (interface == SIU_INTERFACE_IRDA)
87 irsel |= SIRSEL;
88 else
89 irsel &= ~SIRSEL;
90 siu_write(port, SIUIRSEL, irsel);
91
92 spin_unlock_irqrestore(&port->lock, flags);
93}
94EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
95
96void vr41xx_use_irda(irda_use_t use)
97{
98 struct uart_port *port;
99 unsigned long flags;
100 uint8_t irsel;
101
102 port = &siu_uart_ports[0];
103
104 spin_lock_irqsave(&port->lock, flags);
105
106 irsel = siu_read(port, SIUIRSEL);
107 if (use == FIR_USE_IRDA)
108 irsel |= IRUSESEL;
109 else
110 irsel &= ~IRUSESEL;
111 siu_write(port, SIUIRSEL, irsel);
112
113 spin_unlock_irqrestore(&port->lock, flags);
114}
115EXPORT_SYMBOL_GPL(vr41xx_use_irda);
116
117void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
118{
119 struct uart_port *port;
120 unsigned long flags;
121 uint8_t irsel;
122
123 port = &siu_uart_ports[0];
124
125 spin_lock_irqsave(&port->lock, flags);
126
127 irsel = siu_read(port, SIUIRSEL);
128 irsel &= ~(IRMSEL | TMICTX | TMICMODE);
129 switch (module) {
130 case SHARP_IRDA:
131 irsel |= IRMSEL_SHARP;
132 break;
133 case TEMIC_IRDA:
134 irsel |= IRMSEL_TEMIC | TMICMODE;
135 if (speed == IRDA_TX_4MBPS)
136 irsel |= TMICTX;
137 break;
138 case HP_IRDA:
139 irsel |= IRMSEL_HP;
140 break;
141 default:
142 break;
143 }
144 siu_write(port, SIUIRSEL, irsel);
145
146 spin_unlock_irqrestore(&port->lock, flags);
147}
148EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
149
150static inline void siu_clear_fifo(struct uart_port *port)
151{
152 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
153 siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
154 UART_FCR_CLEAR_XMIT);
155 siu_write(port, UART_FCR, 0);
156}
157
158static inline unsigned long siu_port_size(struct uart_port *port)
159{
160 switch (port->type) {
161 case PORT_VR41XX_SIU:
162 return 11UL;
163 case PORT_VR41XX_DSIU:
164 return 8UL;
165 }
166
167 return 0;
168}
169
170static inline unsigned int siu_check_type(struct uart_port *port)
171{
172 if (port->line == 0)
173 return PORT_VR41XX_SIU;
174 if (port->line == 1 && port->irq != -1)
175 return PORT_VR41XX_DSIU;
176
177 return PORT_UNKNOWN;
178}
179
180static inline const char *siu_type_name(struct uart_port *port)
181{
182 switch (port->type) {
183 case PORT_VR41XX_SIU:
184 return "SIU";
185 case PORT_VR41XX_DSIU:
186 return "DSIU";
187 }
188
189 return NULL;
190}
191
192static unsigned int siu_tx_empty(struct uart_port *port)
193{
194 uint8_t lsr;
195
196 lsr = siu_read(port, UART_LSR);
197 if (lsr & UART_LSR_TEMT)
198 return TIOCSER_TEMT;
199
200 return 0;
201}
202
203static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
204{
205 uint8_t mcr = 0;
206
207 if (mctrl & TIOCM_DTR)
208 mcr |= UART_MCR_DTR;
209 if (mctrl & TIOCM_RTS)
210 mcr |= UART_MCR_RTS;
211 if (mctrl & TIOCM_OUT1)
212 mcr |= UART_MCR_OUT1;
213 if (mctrl & TIOCM_OUT2)
214 mcr |= UART_MCR_OUT2;
215 if (mctrl & TIOCM_LOOP)
216 mcr |= UART_MCR_LOOP;
217
218 siu_write(port, UART_MCR, mcr);
219}
220
221static unsigned int siu_get_mctrl(struct uart_port *port)
222{
223 uint8_t msr;
224 unsigned int mctrl = 0;
225
226 msr = siu_read(port, UART_MSR);
227 if (msr & UART_MSR_DCD)
228 mctrl |= TIOCM_CAR;
229 if (msr & UART_MSR_RI)
230 mctrl |= TIOCM_RNG;
231 if (msr & UART_MSR_DSR)
232 mctrl |= TIOCM_DSR;
233 if (msr & UART_MSR_CTS)
234 mctrl |= TIOCM_CTS;
235
236 return mctrl;
237}
238
239static void siu_stop_tx(struct uart_port *port)
240{
241 unsigned long flags;
242 uint8_t ier;
243
244 spin_lock_irqsave(&port->lock, flags);
245
246 ier = siu_read(port, UART_IER);
247 ier &= ~UART_IER_THRI;
248 siu_write(port, UART_IER, ier);
249
250 spin_unlock_irqrestore(&port->lock, flags);
251}
252
253static void siu_start_tx(struct uart_port *port)
254{
255 unsigned long flags;
256 uint8_t ier;
257
258 spin_lock_irqsave(&port->lock, flags);
259
260 ier = siu_read(port, UART_IER);
261 ier |= UART_IER_THRI;
262 siu_write(port, UART_IER, ier);
263
264 spin_unlock_irqrestore(&port->lock, flags);
265}
266
267static void siu_stop_rx(struct uart_port *port)
268{
269 unsigned long flags;
270 uint8_t ier;
271
272 spin_lock_irqsave(&port->lock, flags);
273
274 ier = siu_read(port, UART_IER);
275 ier &= ~UART_IER_RLSI;
276 siu_write(port, UART_IER, ier);
277
278 port->read_status_mask &= ~UART_LSR_DR;
279
280 spin_unlock_irqrestore(&port->lock, flags);
281}
282
283static void siu_enable_ms(struct uart_port *port)
284{
285 unsigned long flags;
286 uint8_t ier;
287
288 spin_lock_irqsave(&port->lock, flags);
289
290 ier = siu_read(port, UART_IER);
291 ier |= UART_IER_MSI;
292 siu_write(port, UART_IER, ier);
293
294 spin_unlock_irqrestore(&port->lock, flags);
295}
296
297static void siu_break_ctl(struct uart_port *port, int ctl)
298{
299 unsigned long flags;
300 uint8_t lcr;
301
302 spin_lock_irqsave(&port->lock, flags);
303
304 lcr = siu_read(port, UART_LCR);
305 if (ctl == -1)
306 lcr |= UART_LCR_SBC;
307 else
308 lcr &= ~UART_LCR_SBC;
309 siu_write(port, UART_LCR, lcr);
310
311 spin_unlock_irqrestore(&port->lock, flags);
312}
313
314static inline void receive_chars(struct uart_port *port, uint8_t *status)
315{
316 struct tty_struct *tty;
317 uint8_t lsr, ch;
318 char flag;
319 int max_count = RX_MAX_COUNT;
320
321 tty = port->state->port.tty;
322 lsr = *status;
323
324 do {
325 ch = siu_read(port, UART_RX);
326 port->icount.rx++;
327 flag = TTY_NORMAL;
328
329#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
330 lsr |= lsr_break_flag[port->line];
331 lsr_break_flag[port->line] = 0;
332#endif
333 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
334 UART_LSR_PE | UART_LSR_OE))) {
335 if (lsr & UART_LSR_BI) {
336 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
337 port->icount.brk++;
338
339 if (uart_handle_break(port))
340 goto ignore_char;
341 }
342
343 if (lsr & UART_LSR_FE)
344 port->icount.frame++;
345 if (lsr & UART_LSR_PE)
346 port->icount.parity++;
347 if (lsr & UART_LSR_OE)
348 port->icount.overrun++;
349
350 lsr &= port->read_status_mask;
351 if (lsr & UART_LSR_BI)
352 flag = TTY_BREAK;
353 if (lsr & UART_LSR_FE)
354 flag = TTY_FRAME;
355 if (lsr & UART_LSR_PE)
356 flag = TTY_PARITY;
357 }
358
359 if (uart_handle_sysrq_char(port, ch))
360 goto ignore_char;
361
362 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
363
364 ignore_char:
365 lsr = siu_read(port, UART_LSR);
366 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
367
368 tty_flip_buffer_push(tty);
369
370 *status = lsr;
371}
372
373static inline void check_modem_status(struct uart_port *port)
374{
375 uint8_t msr;
376
377 msr = siu_read(port, UART_MSR);
378 if ((msr & UART_MSR_ANY_DELTA) == 0)
379 return;
380 if (msr & UART_MSR_DDCD)
381 uart_handle_dcd_change(port, msr & UART_MSR_DCD);
382 if (msr & UART_MSR_TERI)
383 port->icount.rng++;
384 if (msr & UART_MSR_DDSR)
385 port->icount.dsr++;
386 if (msr & UART_MSR_DCTS)
387 uart_handle_cts_change(port, msr & UART_MSR_CTS);
388
389 wake_up_interruptible(&port->state->port.delta_msr_wait);
390}
391
392static inline void transmit_chars(struct uart_port *port)
393{
394 struct circ_buf *xmit;
395 int max_count = TX_MAX_COUNT;
396
397 xmit = &port->state->xmit;
398
399 if (port->x_char) {
400 siu_write(port, UART_TX, port->x_char);
401 port->icount.tx++;
402 port->x_char = 0;
403 return;
404 }
405
406 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
407 siu_stop_tx(port);
408 return;
409 }
410
411 do {
412 siu_write(port, UART_TX, xmit->buf[xmit->tail]);
413 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
414 port->icount.tx++;
415 if (uart_circ_empty(xmit))
416 break;
417 } while (max_count-- > 0);
418
419 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
420 uart_write_wakeup(port);
421
422 if (uart_circ_empty(xmit))
423 siu_stop_tx(port);
424}
425
426static irqreturn_t siu_interrupt(int irq, void *dev_id)
427{
428 struct uart_port *port;
429 uint8_t iir, lsr;
430
431 port = (struct uart_port *)dev_id;
432
433 iir = siu_read(port, UART_IIR);
434 if (iir & UART_IIR_NO_INT)
435 return IRQ_NONE;
436
437 lsr = siu_read(port, UART_LSR);
438 if (lsr & UART_LSR_DR)
439 receive_chars(port, &lsr);
440
441 check_modem_status(port);
442
443 if (lsr & UART_LSR_THRE)
444 transmit_chars(port);
445
446 return IRQ_HANDLED;
447}
448
449static int siu_startup(struct uart_port *port)
450{
451 int retval;
452
453 if (port->membase == NULL)
454 return -ENODEV;
455
456 siu_clear_fifo(port);
457
458 (void)siu_read(port, UART_LSR);
459 (void)siu_read(port, UART_RX);
460 (void)siu_read(port, UART_IIR);
461 (void)siu_read(port, UART_MSR);
462
463 if (siu_read(port, UART_LSR) == 0xff)
464 return -ENODEV;
465
466 retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
467 if (retval)
468 return retval;
469
470 if (port->type == PORT_VR41XX_DSIU)
471 vr41xx_enable_dsiuint(DSIUINT_ALL);
472
473 siu_write(port, UART_LCR, UART_LCR_WLEN8);
474
475 spin_lock_irq(&port->lock);
476 siu_set_mctrl(port, port->mctrl);
477 spin_unlock_irq(&port->lock);
478
479 siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
480
481 (void)siu_read(port, UART_LSR);
482 (void)siu_read(port, UART_RX);
483 (void)siu_read(port, UART_IIR);
484 (void)siu_read(port, UART_MSR);
485
486 return 0;
487}
488
489static void siu_shutdown(struct uart_port *port)
490{
491 unsigned long flags;
492 uint8_t lcr;
493
494 siu_write(port, UART_IER, 0);
495
496 spin_lock_irqsave(&port->lock, flags);
497
498 port->mctrl &= ~TIOCM_OUT2;
499 siu_set_mctrl(port, port->mctrl);
500
501 spin_unlock_irqrestore(&port->lock, flags);
502
503 lcr = siu_read(port, UART_LCR);
504 lcr &= ~UART_LCR_SBC;
505 siu_write(port, UART_LCR, lcr);
506
507 siu_clear_fifo(port);
508
509 (void)siu_read(port, UART_RX);
510
511 if (port->type == PORT_VR41XX_DSIU)
512 vr41xx_disable_dsiuint(DSIUINT_ALL);
513
514 free_irq(port->irq, port);
515}
516
517static void siu_set_termios(struct uart_port *port, struct ktermios *new,
518 struct ktermios *old)
519{
520 tcflag_t c_cflag, c_iflag;
521 uint8_t lcr, fcr, ier;
522 unsigned int baud, quot;
523 unsigned long flags;
524
525 c_cflag = new->c_cflag;
526 switch (c_cflag & CSIZE) {
527 case CS5:
528 lcr = UART_LCR_WLEN5;
529 break;
530 case CS6:
531 lcr = UART_LCR_WLEN6;
532 break;
533 case CS7:
534 lcr = UART_LCR_WLEN7;
535 break;
536 default:
537 lcr = UART_LCR_WLEN8;
538 break;
539 }
540
541 if (c_cflag & CSTOPB)
542 lcr |= UART_LCR_STOP;
543 if (c_cflag & PARENB)
544 lcr |= UART_LCR_PARITY;
545 if ((c_cflag & PARODD) != PARODD)
546 lcr |= UART_LCR_EPAR;
547 if (c_cflag & CMSPAR)
548 lcr |= UART_LCR_SPAR;
549
550 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
551 quot = uart_get_divisor(port, baud);
552
553 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
554
555 spin_lock_irqsave(&port->lock, flags);
556
557 uart_update_timeout(port, c_cflag, baud);
558
559 c_iflag = new->c_iflag;
560
561 port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
562 if (c_iflag & INPCK)
563 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
564 if (c_iflag & (BRKINT | PARMRK))
565 port->read_status_mask |= UART_LSR_BI;
566
567 port->ignore_status_mask = 0;
568 if (c_iflag & IGNPAR)
569 port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
570 if (c_iflag & IGNBRK) {
571 port->ignore_status_mask |= UART_LSR_BI;
572 if (c_iflag & IGNPAR)
573 port->ignore_status_mask |= UART_LSR_OE;
574 }
575
576 if ((c_cflag & CREAD) == 0)
577 port->ignore_status_mask |= UART_LSR_DR;
578
579 ier = siu_read(port, UART_IER);
580 ier &= ~UART_IER_MSI;
581 if (UART_ENABLE_MS(port, c_cflag))
582 ier |= UART_IER_MSI;
583 siu_write(port, UART_IER, ier);
584
585 siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
586
587 siu_write(port, UART_DLL, (uint8_t)quot);
588 siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
589
590 siu_write(port, UART_LCR, lcr);
591
592 siu_write(port, UART_FCR, fcr);
593
594 siu_set_mctrl(port, port->mctrl);
595
596 spin_unlock_irqrestore(&port->lock, flags);
597}
598
599static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
600{
601 switch (state) {
602 case 0:
603 switch (port->type) {
604 case PORT_VR41XX_SIU:
605 vr41xx_supply_clock(SIU_CLOCK);
606 break;
607 case PORT_VR41XX_DSIU:
608 vr41xx_supply_clock(DSIU_CLOCK);
609 break;
610 }
611 break;
612 case 3:
613 switch (port->type) {
614 case PORT_VR41XX_SIU:
615 vr41xx_mask_clock(SIU_CLOCK);
616 break;
617 case PORT_VR41XX_DSIU:
618 vr41xx_mask_clock(DSIU_CLOCK);
619 break;
620 }
621 break;
622 }
623}
624
625static const char *siu_type(struct uart_port *port)
626{
627 return siu_type_name(port);
628}
629
630static void siu_release_port(struct uart_port *port)
631{
632 unsigned long size;
633
634 if (port->flags & UPF_IOREMAP) {
635 iounmap(port->membase);
636 port->membase = NULL;
637 }
638
639 size = siu_port_size(port);
640 release_mem_region(port->mapbase, size);
641}
642
643static int siu_request_port(struct uart_port *port)
644{
645 unsigned long size;
646 struct resource *res;
647
648 size = siu_port_size(port);
649 res = request_mem_region(port->mapbase, size, siu_type_name(port));
650 if (res == NULL)
651 return -EBUSY;
652
653 if (port->flags & UPF_IOREMAP) {
654 port->membase = ioremap(port->mapbase, size);
655 if (port->membase == NULL) {
656 release_resource(res);
657 return -ENOMEM;
658 }
659 }
660
661 return 0;
662}
663
664static void siu_config_port(struct uart_port *port, int flags)
665{
666 if (flags & UART_CONFIG_TYPE) {
667 port->type = siu_check_type(port);
668 (void)siu_request_port(port);
669 }
670}
671
672static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
673{
674 if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
675 return -EINVAL;
676 if (port->irq != serial->irq)
677 return -EINVAL;
678 if (port->iotype != serial->io_type)
679 return -EINVAL;
680 if (port->mapbase != (unsigned long)serial->iomem_base)
681 return -EINVAL;
682
683 return 0;
684}
685
686static struct uart_ops siu_uart_ops = {
687 .tx_empty = siu_tx_empty,
688 .set_mctrl = siu_set_mctrl,
689 .get_mctrl = siu_get_mctrl,
690 .stop_tx = siu_stop_tx,
691 .start_tx = siu_start_tx,
692 .stop_rx = siu_stop_rx,
693 .enable_ms = siu_enable_ms,
694 .break_ctl = siu_break_ctl,
695 .startup = siu_startup,
696 .shutdown = siu_shutdown,
697 .set_termios = siu_set_termios,
698 .pm = siu_pm,
699 .type = siu_type,
700 .release_port = siu_release_port,
701 .request_port = siu_request_port,
702 .config_port = siu_config_port,
703 .verify_port = siu_verify_port,
704};
705
706static int siu_init_ports(struct platform_device *pdev)
707{
708 struct uart_port *port;
709 struct resource *res;
710 int *type = pdev->dev.platform_data;
711 int i;
712
713 if (!type)
714 return 0;
715
716 port = siu_uart_ports;
717 for (i = 0; i < SIU_PORTS_MAX; i++) {
718 port->type = type[i];
719 if (port->type == PORT_UNKNOWN)
720 continue;
721 port->irq = platform_get_irq(pdev, i);
722 port->uartclk = SIU_BAUD_BASE * 16;
723 port->fifosize = 16;
724 port->regshift = 0;
725 port->iotype = UPIO_MEM;
726 port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
727 port->line = i;
728 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
729 port->mapbase = res->start;
730 port++;
731 }
732
733 return i;
734}
735
736#ifdef CONFIG_SERIAL_VR41XX_CONSOLE
737
738#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
739
740static void wait_for_xmitr(struct uart_port *port)
741{
742 int timeout = 10000;
743 uint8_t lsr, msr;
744
745 do {
746 lsr = siu_read(port, UART_LSR);
747 if (lsr & UART_LSR_BI)
748 lsr_break_flag[port->line] = UART_LSR_BI;
749
750 if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
751 break;
752 } while (timeout-- > 0);
753
754 if (port->flags & UPF_CONS_FLOW) {
755 timeout = 1000000;
756
757 do {
758 msr = siu_read(port, UART_MSR);
759 if ((msr & UART_MSR_CTS) != 0)
760 break;
761 } while (timeout-- > 0);
762 }
763}
764
765static void siu_console_putchar(struct uart_port *port, int ch)
766{
767 wait_for_xmitr(port);
768 siu_write(port, UART_TX, ch);
769}
770
771static void siu_console_write(struct console *con, const char *s, unsigned count)
772{
773 struct uart_port *port;
774 uint8_t ier;
775
776 port = &siu_uart_ports[con->index];
777
778 ier = siu_read(port, UART_IER);
779 siu_write(port, UART_IER, 0);
780
781 uart_console_write(port, s, count, siu_console_putchar);
782
783 wait_for_xmitr(port);
784 siu_write(port, UART_IER, ier);
785}
786
787static int __init siu_console_setup(struct console *con, char *options)
788{
789 struct uart_port *port;
790 int baud = 9600;
791 int parity = 'n';
792 int bits = 8;
793 int flow = 'n';
794
795 if (con->index >= SIU_PORTS_MAX)
796 con->index = 0;
797
798 port = &siu_uart_ports[con->index];
799 if (port->membase == NULL) {
800 if (port->mapbase == 0)
801 return -ENODEV;
802 port->membase = ioremap(port->mapbase, siu_port_size(port));
803 }
804
805 if (port->type == PORT_VR41XX_SIU)
806 vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
807
808 if (options != NULL)
809 uart_parse_options(options, &baud, &parity, &bits, &flow);
810
811 return uart_set_options(port, con, baud, parity, bits, flow);
812}
813
814static struct uart_driver siu_uart_driver;
815
816static struct console siu_console = {
817 .name = "ttyVR",
818 .write = siu_console_write,
819 .device = uart_console_device,
820 .setup = siu_console_setup,
821 .flags = CON_PRINTBUFFER,
822 .index = -1,
823 .data = &siu_uart_driver,
824};
825
826static int __devinit siu_console_init(void)
827{
828 struct uart_port *port;
829 int i;
830
831 for (i = 0; i < SIU_PORTS_MAX; i++) {
832 port = &siu_uart_ports[i];
833 port->ops = &siu_uart_ops;
834 }
835
836 register_console(&siu_console);
837
838 return 0;
839}
840
841console_initcall(siu_console_init);
842
843void __init vr41xx_siu_early_setup(struct uart_port *port)
844{
845 if (port->type == PORT_UNKNOWN)
846 return;
847
848 siu_uart_ports[port->line].line = port->line;
849 siu_uart_ports[port->line].type = port->type;
850 siu_uart_ports[port->line].uartclk = SIU_BAUD_BASE * 16;
851 siu_uart_ports[port->line].mapbase = port->mapbase;
852 siu_uart_ports[port->line].mapbase = port->mapbase;
853 siu_uart_ports[port->line].ops = &siu_uart_ops;
854}
855
856#define SERIAL_VR41XX_CONSOLE &siu_console
857#else
858#define SERIAL_VR41XX_CONSOLE NULL
859#endif
860
861static struct uart_driver siu_uart_driver = {
862 .owner = THIS_MODULE,
863 .driver_name = "SIU",
864 .dev_name = "ttyVR",
865 .major = SIU_MAJOR,
866 .minor = SIU_MINOR_BASE,
867 .cons = SERIAL_VR41XX_CONSOLE,
868};
869
870static int __devinit siu_probe(struct platform_device *dev)
871{
872 struct uart_port *port;
873 int num, i, retval;
874
875 num = siu_init_ports(dev);
876 if (num <= 0)
877 return -ENODEV;
878
879 siu_uart_driver.nr = num;
880 retval = uart_register_driver(&siu_uart_driver);
881 if (retval)
882 return retval;
883
884 for (i = 0; i < num; i++) {
885 port = &siu_uart_ports[i];
886 port->ops = &siu_uart_ops;
887 port->dev = &dev->dev;
888
889 retval = uart_add_one_port(&siu_uart_driver, port);
890 if (retval < 0) {
891 port->dev = NULL;
892 break;
893 }
894 }
895
896 if (i == 0 && retval < 0) {
897 uart_unregister_driver(&siu_uart_driver);
898 return retval;
899 }
900
901 return 0;
902}
903
904static int __devexit siu_remove(struct platform_device *dev)
905{
906 struct uart_port *port;
907 int i;
908
909 for (i = 0; i < siu_uart_driver.nr; i++) {
910 port = &siu_uart_ports[i];
911 if (port->dev == &dev->dev) {
912 uart_remove_one_port(&siu_uart_driver, port);
913 port->dev = NULL;
914 }
915 }
916
917 uart_unregister_driver(&siu_uart_driver);
918
919 return 0;
920}
921
922static int siu_suspend(struct platform_device *dev, pm_message_t state)
923{
924 struct uart_port *port;
925 int i;
926
927 for (i = 0; i < siu_uart_driver.nr; i++) {
928 port = &siu_uart_ports[i];
929 if ((port->type == PORT_VR41XX_SIU ||
930 port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
931 uart_suspend_port(&siu_uart_driver, port);
932
933 }
934
935 return 0;
936}
937
938static int siu_resume(struct platform_device *dev)
939{
940 struct uart_port *port;
941 int i;
942
943 for (i = 0; i < siu_uart_driver.nr; i++) {
944 port = &siu_uart_ports[i];
945 if ((port->type == PORT_VR41XX_SIU ||
946 port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
947 uart_resume_port(&siu_uart_driver, port);
948 }
949
950 return 0;
951}
952
953static struct platform_driver siu_device_driver = {
954 .probe = siu_probe,
955 .remove = __devexit_p(siu_remove),
956 .suspend = siu_suspend,
957 .resume = siu_resume,
958 .driver = {
959 .name = "SIU",
960 .owner = THIS_MODULE,
961 },
962};
963
964static int __init vr41xx_siu_init(void)
965{
966 return platform_driver_register(&siu_device_driver);
967}
968
969static void __exit vr41xx_siu_exit(void)
970{
971 platform_driver_unregister(&siu_device_driver);
972}
973
974module_init(vr41xx_siu_init);
975module_exit(vr41xx_siu_exit);
976
977MODULE_LICENSE("GPL");
978MODULE_ALIAS("platform:SIU");
diff --git a/drivers/tty/serial/vt8500_serial.c b/drivers/tty/serial/vt8500_serial.c
new file mode 100644
index 000000000000..37fc4e3d487c
--- /dev/null
+++ b/drivers/tty/serial/vt8500_serial.c
@@ -0,0 +1,646 @@
1/*
2 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
3 *
4 * Based on msm_serial.c, which is:
5 * Copyright (C) 2007 Google, Inc.
6 * Author: Robert Love <rlove@google.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
22#include <linux/hrtimer.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/io.h>
26#include <linux/ioport.h>
27#include <linux/irq.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/slab.h>
35#include <linux/clk.h>
36#include <linux/platform_device.h>
37
38/*
39 * UART Register offsets
40 */
41
42#define VT8500_URTDR 0x0000 /* Transmit data */
43#define VT8500_URRDR 0x0004 /* Receive data */
44#define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
45#define VT8500_URLCR 0x000C /* Line control */
46#define VT8500_URICR 0x0010 /* IrDA control */
47#define VT8500_URIER 0x0014 /* Interrupt enable */
48#define VT8500_URISR 0x0018 /* Interrupt status */
49#define VT8500_URUSR 0x001c /* UART status */
50#define VT8500_URFCR 0x0020 /* FIFO control */
51#define VT8500_URFIDX 0x0024 /* FIFO index */
52#define VT8500_URBKR 0x0028 /* Break signal count */
53#define VT8500_URTOD 0x002c /* Time out divisor */
54#define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
55#define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
56
57/*
58 * Interrupt enable and status bits
59 */
60
61#define TXDE (1 << 0) /* Tx Data empty */
62#define RXDF (1 << 1) /* Rx Data full */
63#define TXFAE (1 << 2) /* Tx FIFO almost empty */
64#define TXFE (1 << 3) /* Tx FIFO empty */
65#define RXFAF (1 << 4) /* Rx FIFO almost full */
66#define RXFF (1 << 5) /* Rx FIFO full */
67#define TXUDR (1 << 6) /* Tx underrun */
68#define RXOVER (1 << 7) /* Rx overrun */
69#define PER (1 << 8) /* Parity error */
70#define FER (1 << 9) /* Frame error */
71#define TCTS (1 << 10) /* Toggle of CTS */
72#define RXTOUT (1 << 11) /* Rx timeout */
73#define BKDONE (1 << 12) /* Break signal done */
74#define ERR (1 << 13) /* AHB error response */
75
76#define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
77#define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
78
79struct vt8500_port {
80 struct uart_port uart;
81 char name[16];
82 struct clk *clk;
83 unsigned int ier;
84};
85
86static inline void vt8500_write(struct uart_port *port, unsigned int val,
87 unsigned int off)
88{
89 writel(val, port->membase + off);
90}
91
92static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
93{
94 return readl(port->membase + off);
95}
96
97static void vt8500_stop_tx(struct uart_port *port)
98{
99 struct vt8500_port *vt8500_port = container_of(port,
100 struct vt8500_port,
101 uart);
102
103 vt8500_port->ier &= ~TX_FIFO_INTS;
104 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
105}
106
107static void vt8500_stop_rx(struct uart_port *port)
108{
109 struct vt8500_port *vt8500_port = container_of(port,
110 struct vt8500_port,
111 uart);
112
113 vt8500_port->ier &= ~RX_FIFO_INTS;
114 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
115}
116
117static void vt8500_enable_ms(struct uart_port *port)
118{
119 struct vt8500_port *vt8500_port = container_of(port,
120 struct vt8500_port,
121 uart);
122
123 vt8500_port->ier |= TCTS;
124 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
125}
126
127static void handle_rx(struct uart_port *port)
128{
129 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
130 if (!tty) {
131 /* Discard data: no tty available */
132 int count = (vt8500_read(port, VT8500_URFIDX) & 0x1f00) >> 8;
133 u16 ch;
134 while (count--)
135 ch = readw(port->membase + VT8500_RXFIFO);
136 return;
137 }
138
139 /*
140 * Handle overrun
141 */
142 if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
143 port->icount.overrun++;
144 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
145 }
146
147 /* and now the main RX loop */
148 while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
149 unsigned int c;
150 char flag = TTY_NORMAL;
151
152 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
153
154 /* Mask conditions we're ignorning. */
155 c &= ~port->read_status_mask;
156
157 if (c & FER) {
158 port->icount.frame++;
159 flag = TTY_FRAME;
160 } else if (c & PER) {
161 port->icount.parity++;
162 flag = TTY_PARITY;
163 }
164 port->icount.rx++;
165
166 if (!uart_handle_sysrq_char(port, c))
167 tty_insert_flip_char(tty, c, flag);
168 }
169
170 tty_flip_buffer_push(tty);
171 tty_kref_put(tty);
172}
173
174static void handle_tx(struct uart_port *port)
175{
176 struct circ_buf *xmit = &port->state->xmit;
177
178 if (port->x_char) {
179 writeb(port->x_char, port->membase + VT8500_TXFIFO);
180 port->icount.tx++;
181 port->x_char = 0;
182 }
183 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
184 vt8500_stop_tx(port);
185 return;
186 }
187
188 while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
189 if (uart_circ_empty(xmit))
190 break;
191
192 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
193
194 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
195 port->icount.tx++;
196 }
197
198 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
199 uart_write_wakeup(port);
200
201 if (uart_circ_empty(xmit))
202 vt8500_stop_tx(port);
203}
204
205static void vt8500_start_tx(struct uart_port *port)
206{
207 struct vt8500_port *vt8500_port = container_of(port,
208 struct vt8500_port,
209 uart);
210
211 vt8500_port->ier &= ~TX_FIFO_INTS;
212 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
213 handle_tx(port);
214 vt8500_port->ier |= TX_FIFO_INTS;
215 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
216}
217
218static void handle_delta_cts(struct uart_port *port)
219{
220 port->icount.cts++;
221 wake_up_interruptible(&port->state->port.delta_msr_wait);
222}
223
224static irqreturn_t vt8500_irq(int irq, void *dev_id)
225{
226 struct uart_port *port = dev_id;
227 unsigned long isr;
228
229 spin_lock(&port->lock);
230 isr = vt8500_read(port, VT8500_URISR);
231
232 /* Acknowledge active status bits */
233 vt8500_write(port, isr, VT8500_URISR);
234
235 if (isr & RX_FIFO_INTS)
236 handle_rx(port);
237 if (isr & TX_FIFO_INTS)
238 handle_tx(port);
239 if (isr & TCTS)
240 handle_delta_cts(port);
241
242 spin_unlock(&port->lock);
243
244 return IRQ_HANDLED;
245}
246
247static unsigned int vt8500_tx_empty(struct uart_port *port)
248{
249 return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
250 TIOCSER_TEMT : 0;
251}
252
253static unsigned int vt8500_get_mctrl(struct uart_port *port)
254{
255 unsigned int usr;
256
257 usr = vt8500_read(port, VT8500_URUSR);
258 if (usr & (1 << 4))
259 return TIOCM_CTS;
260 else
261 return 0;
262}
263
264static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
265{
266}
267
268static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
269{
270 if (break_ctl)
271 vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9),
272 VT8500_URLCR);
273}
274
275static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
276{
277 unsigned long div;
278 unsigned int loops = 1000;
279
280 div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
281
282 if (unlikely((baud < 900) || (baud > 921600)))
283 div |= 7;
284 else
285 div |= (921600 / baud) - 1;
286
287 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
288 cpu_relax();
289 vt8500_write(port, div, VT8500_URDIV);
290
291 return baud;
292}
293
294static int vt8500_startup(struct uart_port *port)
295{
296 struct vt8500_port *vt8500_port =
297 container_of(port, struct vt8500_port, uart);
298 int ret;
299
300 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
301 "vt8500_serial%d", port->line);
302
303 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
304 vt8500_port->name, port);
305 if (unlikely(ret))
306 return ret;
307
308 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
309
310 return 0;
311}
312
313static void vt8500_shutdown(struct uart_port *port)
314{
315 struct vt8500_port *vt8500_port =
316 container_of(port, struct vt8500_port, uart);
317
318 vt8500_port->ier = 0;
319
320 /* disable interrupts and FIFOs */
321 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
322 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
323 free_irq(port->irq, port);
324}
325
326static void vt8500_set_termios(struct uart_port *port,
327 struct ktermios *termios,
328 struct ktermios *old)
329{
330 struct vt8500_port *vt8500_port =
331 container_of(port, struct vt8500_port, uart);
332 unsigned long flags;
333 unsigned int baud, lcr;
334 unsigned int loops = 1000;
335
336 spin_lock_irqsave(&port->lock, flags);
337
338 /* calculate and set baud rate */
339 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
340 baud = vt8500_set_baud_rate(port, baud);
341 if (tty_termios_baud_rate(termios))
342 tty_termios_encode_baud_rate(termios, baud, baud);
343
344 /* calculate parity */
345 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
346 lcr &= ~((1 << 5) | (1 << 4));
347 if (termios->c_cflag & PARENB) {
348 lcr |= (1 << 4);
349 termios->c_cflag &= ~CMSPAR;
350 if (termios->c_cflag & PARODD)
351 lcr |= (1 << 5);
352 }
353
354 /* calculate bits per char */
355 lcr &= ~(1 << 2);
356 switch (termios->c_cflag & CSIZE) {
357 case CS7:
358 break;
359 case CS8:
360 default:
361 lcr |= (1 << 2);
362 termios->c_cflag &= ~CSIZE;
363 termios->c_cflag |= CS8;
364 break;
365 }
366
367 /* calculate stop bits */
368 lcr &= ~(1 << 3);
369 if (termios->c_cflag & CSTOPB)
370 lcr |= (1 << 3);
371
372 /* set parity, bits per char, and stop bit */
373 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
374
375 /* Configure status bits to ignore based on termio flags. */
376 port->read_status_mask = 0;
377 if (termios->c_iflag & IGNPAR)
378 port->read_status_mask = FER | PER;
379
380 uart_update_timeout(port, termios->c_cflag, baud);
381
382 /* Reset FIFOs */
383 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
384 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
385 && --loops)
386 cpu_relax();
387
388 /* Every possible FIFO-related interrupt */
389 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
390
391 /*
392 * CTS flow control
393 */
394 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
395 vt8500_port->ier |= TCTS;
396
397 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
398 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
399
400 spin_unlock_irqrestore(&port->lock, flags);
401}
402
403static const char *vt8500_type(struct uart_port *port)
404{
405 struct vt8500_port *vt8500_port =
406 container_of(port, struct vt8500_port, uart);
407 return vt8500_port->name;
408}
409
410static void vt8500_release_port(struct uart_port *port)
411{
412}
413
414static int vt8500_request_port(struct uart_port *port)
415{
416 return 0;
417}
418
419static void vt8500_config_port(struct uart_port *port, int flags)
420{
421 port->type = PORT_VT8500;
422}
423
424static int vt8500_verify_port(struct uart_port *port,
425 struct serial_struct *ser)
426{
427 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
428 return -EINVAL;
429 if (unlikely(port->irq != ser->irq))
430 return -EINVAL;
431 return 0;
432}
433
434static struct vt8500_port *vt8500_uart_ports[4];
435static struct uart_driver vt8500_uart_driver;
436
437#ifdef CONFIG_SERIAL_VT8500_CONSOLE
438
439static inline void wait_for_xmitr(struct uart_port *port)
440{
441 unsigned int status, tmout = 10000;
442
443 /* Wait up to 10ms for the character(s) to be sent. */
444 do {
445 status = vt8500_read(port, VT8500_URFIDX);
446
447 if (--tmout == 0)
448 break;
449 udelay(1);
450 } while (status & 0x10);
451}
452
453static void vt8500_console_putchar(struct uart_port *port, int c)
454{
455 wait_for_xmitr(port);
456 writeb(c, port->membase + VT8500_TXFIFO);
457}
458
459static void vt8500_console_write(struct console *co, const char *s,
460 unsigned int count)
461{
462 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
463 unsigned long ier;
464
465 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
466
467 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
468 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
469
470 uart_console_write(&vt8500_port->uart, s, count,
471 vt8500_console_putchar);
472
473 /*
474 * Finally, wait for transmitter to become empty
475 * and switch back to FIFO
476 */
477 wait_for_xmitr(&vt8500_port->uart);
478 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
479}
480
481static int __init vt8500_console_setup(struct console *co, char *options)
482{
483 struct vt8500_port *vt8500_port;
484 int baud = 9600;
485 int bits = 8;
486 int parity = 'n';
487 int flow = 'n';
488
489 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
490 return -ENXIO;
491
492 vt8500_port = vt8500_uart_ports[co->index];
493
494 if (!vt8500_port)
495 return -ENODEV;
496
497 if (options)
498 uart_parse_options(options, &baud, &parity, &bits, &flow);
499
500 return uart_set_options(&vt8500_port->uart,
501 co, baud, parity, bits, flow);
502}
503
504static struct console vt8500_console = {
505 .name = "ttyWMT",
506 .write = vt8500_console_write,
507 .device = uart_console_device,
508 .setup = vt8500_console_setup,
509 .flags = CON_PRINTBUFFER,
510 .index = -1,
511 .data = &vt8500_uart_driver,
512};
513
514#define VT8500_CONSOLE (&vt8500_console)
515
516#else
517#define VT8500_CONSOLE NULL
518#endif
519
520static struct uart_ops vt8500_uart_pops = {
521 .tx_empty = vt8500_tx_empty,
522 .set_mctrl = vt8500_set_mctrl,
523 .get_mctrl = vt8500_get_mctrl,
524 .stop_tx = vt8500_stop_tx,
525 .start_tx = vt8500_start_tx,
526 .stop_rx = vt8500_stop_rx,
527 .enable_ms = vt8500_enable_ms,
528 .break_ctl = vt8500_break_ctl,
529 .startup = vt8500_startup,
530 .shutdown = vt8500_shutdown,
531 .set_termios = vt8500_set_termios,
532 .type = vt8500_type,
533 .release_port = vt8500_release_port,
534 .request_port = vt8500_request_port,
535 .config_port = vt8500_config_port,
536 .verify_port = vt8500_verify_port,
537};
538
539static struct uart_driver vt8500_uart_driver = {
540 .owner = THIS_MODULE,
541 .driver_name = "vt8500_serial",
542 .dev_name = "ttyWMT",
543 .nr = 6,
544 .cons = VT8500_CONSOLE,
545};
546
547static int __init vt8500_serial_probe(struct platform_device *pdev)
548{
549 struct vt8500_port *vt8500_port;
550 struct resource *mmres, *irqres;
551 int ret;
552
553 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
554 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
555 if (!mmres || !irqres)
556 return -ENODEV;
557
558 vt8500_port = kzalloc(sizeof(struct vt8500_port), GFP_KERNEL);
559 if (!vt8500_port)
560 return -ENOMEM;
561
562 vt8500_port->uart.type = PORT_VT8500;
563 vt8500_port->uart.iotype = UPIO_MEM;
564 vt8500_port->uart.mapbase = mmres->start;
565 vt8500_port->uart.irq = irqres->start;
566 vt8500_port->uart.fifosize = 16;
567 vt8500_port->uart.ops = &vt8500_uart_pops;
568 vt8500_port->uart.line = pdev->id;
569 vt8500_port->uart.dev = &pdev->dev;
570 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
571 vt8500_port->uart.uartclk = 24000000;
572
573 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
574 "VT8500 UART%d", pdev->id);
575
576 vt8500_port->uart.membase = ioremap(mmres->start,
577 mmres->end - mmres->start + 1);
578 if (!vt8500_port->uart.membase) {
579 ret = -ENOMEM;
580 goto err;
581 }
582
583 vt8500_uart_ports[pdev->id] = vt8500_port;
584
585 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
586
587 platform_set_drvdata(pdev, vt8500_port);
588
589 return 0;
590
591err:
592 kfree(vt8500_port);
593 return ret;
594}
595
596static int __devexit vt8500_serial_remove(struct platform_device *pdev)
597{
598 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
599
600 platform_set_drvdata(pdev, NULL);
601 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
602 kfree(vt8500_port);
603
604 return 0;
605}
606
607static struct platform_driver vt8500_platform_driver = {
608 .probe = vt8500_serial_probe,
609 .remove = vt8500_serial_remove,
610 .driver = {
611 .name = "vt8500_serial",
612 .owner = THIS_MODULE,
613 },
614};
615
616static int __init vt8500_serial_init(void)
617{
618 int ret;
619
620 ret = uart_register_driver(&vt8500_uart_driver);
621 if (unlikely(ret))
622 return ret;
623
624 ret = platform_driver_register(&vt8500_platform_driver);
625
626 if (unlikely(ret))
627 uart_unregister_driver(&vt8500_uart_driver);
628
629 return ret;
630}
631
632static void __exit vt8500_serial_exit(void)
633{
634#ifdef CONFIG_SERIAL_VT8500_CONSOLE
635 unregister_console(&vt8500_console);
636#endif
637 platform_driver_unregister(&vt8500_platform_driver);
638 uart_unregister_driver(&vt8500_uart_driver);
639}
640
641module_init(vt8500_serial_init);
642module_exit(vt8500_serial_exit);
643
644MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
645MODULE_DESCRIPTION("Driver for vt8500 serial device");
646MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/xilinx_uartps.c b/drivers/tty/serial/xilinx_uartps.c
new file mode 100644
index 000000000000..19cc1e8149dd
--- /dev/null
+++ b/drivers/tty/serial/xilinx_uartps.c
@@ -0,0 +1,1113 @@
1/*
2 * Xilinx PS UART driver
3 *
4 * 2011 (c) Xilinx Inc.
5 *
6 * This program is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * either version 2 of the License, or (at your option) any
10 * later version.
11 *
12 */
13
14#include <linux/platform_device.h>
15#include <linux/serial_core.h>
16#include <linux/console.h>
17#include <linux/serial.h>
18#include <linux/irq.h>
19#include <linux/io.h>
20#include <linux/of.h>
21
22#define XUARTPS_TTY_NAME "ttyPS"
23#define XUARTPS_NAME "xuartps"
24#define XUARTPS_MAJOR 0 /* use dynamic node allocation */
25#define XUARTPS_MINOR 0 /* works best with devtmpfs */
26#define XUARTPS_NR_PORTS 2
27#define XUARTPS_FIFO_SIZE 16 /* FIFO size */
28#define XUARTPS_REGISTER_SPACE 0xFFF
29
30#define xuartps_readl(offset) ioread32(port->membase + offset)
31#define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
32
33/********************************Register Map********************************/
34/** UART
35 *
36 * Register offsets for the UART.
37 *
38 */
39#define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
40#define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
41#define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
42#define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
43#define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
44#define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
45#define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
46#define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
47#define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
48#define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
49#define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
50#define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
51#define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
52#define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
53#define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
54#define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
55 Width [15:0] */
56#define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
57 Width [7:0] */
58#define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
59
60/** Control Register
61 *
62 * The Control register (CR) controls the major functions of the device.
63 *
64 * Control Register Bit Definitions
65 */
66#define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
67#define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
68#define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
69#define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
70#define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
71#define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
72#define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
73#define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
74#define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
75
76/** Mode Register
77 *
78 * The mode register (MR) defines the mode of transfer as well as the data
79 * format. If this register is modified during transmission or reception,
80 * data validity cannot be guaranteed.
81 *
82 * Mode Register Bit Definitions
83 *
84 */
85#define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
86#define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
87#define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
88
89#define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
90#define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
91
92#define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
93#define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
94#define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
95#define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
96#define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
97
98#define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
99#define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
100#define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
101
102/** Interrupt Registers
103 *
104 * Interrupt control logic uses the interrupt enable register (IER) and the
105 * interrupt disable register (IDR) to set the value of the bits in the
106 * interrupt mask register (IMR). The IMR determines whether to pass an
107 * interrupt to the interrupt status register (ISR).
108 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
109 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
110 * Reading either IER or IDR returns 0x00.
111 *
112 * All four registers have the same bit definitions.
113 */
114#define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
115#define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
116#define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
117#define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
118#define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
119#define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
120#define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
121#define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
122#define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
123#define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
124#define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
125
126/** Channel Status Register
127 *
128 * The channel status register (CSR) is provided to enable the control logic
129 * to monitor the status of bits in the channel interrupt status register,
130 * even if these are masked out by the interrupt mask register.
131 */
132#define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
133#define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
134#define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
135#define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
136
137/**
138 * xuartps_isr - Interrupt handler
139 * @irq: Irq number
140 * @dev_id: Id of the port
141 *
142 * Returns IRQHANDLED
143 **/
144static irqreturn_t xuartps_isr(int irq, void *dev_id)
145{
146 struct uart_port *port = (struct uart_port *)dev_id;
147 struct tty_struct *tty;
148 unsigned long flags;
149 unsigned int isrstatus, numbytes;
150 unsigned int data;
151 char status = TTY_NORMAL;
152
153 /* Get the tty which could be NULL so don't assume it's valid */
154 tty = tty_port_tty_get(&port->state->port);
155
156 spin_lock_irqsave(&port->lock, flags);
157
158 /* Read the interrupt status register to determine which
159 * interrupt(s) is/are active.
160 */
161 isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
162
163 /* drop byte with parity error if IGNPAR specified */
164 if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
165 isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
166
167 isrstatus &= port->read_status_mask;
168 isrstatus &= ~port->ignore_status_mask;
169
170 if ((isrstatus & XUARTPS_IXR_TOUT) ||
171 (isrstatus & XUARTPS_IXR_RXTRIG)) {
172 /* Receive Timeout Interrupt */
173 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
174 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
175 data = xuartps_readl(XUARTPS_FIFO_OFFSET);
176 port->icount.rx++;
177
178 if (isrstatus & XUARTPS_IXR_PARITY) {
179 port->icount.parity++;
180 status = TTY_PARITY;
181 } else if (isrstatus & XUARTPS_IXR_FRAMING) {
182 port->icount.frame++;
183 status = TTY_FRAME;
184 } else if (isrstatus & XUARTPS_IXR_OVERRUN)
185 port->icount.overrun++;
186
187 if (tty)
188 uart_insert_char(port, isrstatus,
189 XUARTPS_IXR_OVERRUN, data,
190 status);
191 }
192 spin_unlock(&port->lock);
193 if (tty)
194 tty_flip_buffer_push(tty);
195 spin_lock(&port->lock);
196 }
197
198 /* Dispatch an appropriate handler */
199 if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
200 if (uart_circ_empty(&port->state->xmit)) {
201 xuartps_writel(XUARTPS_IXR_TXEMPTY,
202 XUARTPS_IDR_OFFSET);
203 } else {
204 numbytes = port->fifosize;
205 /* Break if no more data available in the UART buffer */
206 while (numbytes--) {
207 if (uart_circ_empty(&port->state->xmit))
208 break;
209 /* Get the data from the UART circular buffer
210 * and write it to the xuartps's TX_FIFO
211 * register.
212 */
213 xuartps_writel(
214 port->state->xmit.buf[port->state->xmit.
215 tail], XUARTPS_FIFO_OFFSET);
216
217 port->icount.tx++;
218
219 /* Adjust the tail of the UART buffer and wrap
220 * the buffer if it reaches limit.
221 */
222 port->state->xmit.tail =
223 (port->state->xmit.tail + 1) & \
224 (UART_XMIT_SIZE - 1);
225 }
226
227 if (uart_circ_chars_pending(
228 &port->state->xmit) < WAKEUP_CHARS)
229 uart_write_wakeup(port);
230 }
231 }
232
233 xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
234
235 /* be sure to release the lock and tty before leaving */
236 spin_unlock_irqrestore(&port->lock, flags);
237 tty_kref_put(tty);
238
239 return IRQ_HANDLED;
240}
241
242/**
243 * xuartps_set_baud_rate - Calculate and set the baud rate
244 * @port: Handle to the uart port structure
245 * @baud: Baud rate to set
246 *
247 * Returns baud rate, requested baud when possible, or actual baud when there
248 * was too much error
249 **/
250static unsigned int xuartps_set_baud_rate(struct uart_port *port,
251 unsigned int baud)
252{
253 unsigned int sel_clk;
254 unsigned int calc_baud = 0;
255 unsigned int brgr_val, brdiv_val;
256 unsigned int bauderror;
257
258 /* Formula to obtain baud rate is
259 * baud_tx/rx rate = sel_clk/CD * (BDIV + 1)
260 * input_clk = (Uart User Defined Clock or Apb Clock)
261 * depends on UCLKEN in MR Reg
262 * sel_clk = input_clk or input_clk/8;
263 * depends on CLKS in MR reg
264 * CD and BDIV depends on values in
265 * baud rate generate register
266 * baud rate clock divisor register
267 */
268 sel_clk = port->uartclk;
269 if (xuartps_readl(XUARTPS_MR_OFFSET) & XUARTPS_MR_CLKSEL)
270 sel_clk = sel_clk / 8;
271
272 /* Find the best values for baud generation */
273 for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
274
275 brgr_val = sel_clk / (baud * (brdiv_val + 1));
276 if (brgr_val < 2 || brgr_val > 65535)
277 continue;
278
279 calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
280
281 if (baud > calc_baud)
282 bauderror = baud - calc_baud;
283 else
284 bauderror = calc_baud - baud;
285
286 /* use the values when percent error is acceptable */
287 if (((bauderror * 100) / baud) < 3) {
288 calc_baud = baud;
289 break;
290 }
291 }
292
293 /* Set the values for the new baud rate */
294 xuartps_writel(brgr_val, XUARTPS_BAUDGEN_OFFSET);
295 xuartps_writel(brdiv_val, XUARTPS_BAUDDIV_OFFSET);
296
297 return calc_baud;
298}
299
300/*----------------------Uart Operations---------------------------*/
301
302/**
303 * xuartps_start_tx - Start transmitting bytes
304 * @port: Handle to the uart port structure
305 *
306 **/
307static void xuartps_start_tx(struct uart_port *port)
308{
309 unsigned int status, numbytes = port->fifosize;
310
311 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
312 return;
313
314 status = xuartps_readl(XUARTPS_CR_OFFSET);
315 /* Set the TX enable bit and clear the TX disable bit to enable the
316 * transmitter.
317 */
318 xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
319 XUARTPS_CR_OFFSET);
320
321 while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
322 & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
323
324 /* Break if no more data available in the UART buffer */
325 if (uart_circ_empty(&port->state->xmit))
326 break;
327
328 /* Get the data from the UART circular buffer and
329 * write it to the xuartps's TX_FIFO register.
330 */
331 xuartps_writel(
332 port->state->xmit.buf[port->state->xmit.tail],
333 XUARTPS_FIFO_OFFSET);
334 port->icount.tx++;
335
336 /* Adjust the tail of the UART buffer and wrap
337 * the buffer if it reaches limit.
338 */
339 port->state->xmit.tail = (port->state->xmit.tail + 1) &
340 (UART_XMIT_SIZE - 1);
341 }
342
343 /* Enable the TX Empty interrupt */
344 xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
345
346 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
347 uart_write_wakeup(port);
348}
349
350/**
351 * xuartps_stop_tx - Stop TX
352 * @port: Handle to the uart port structure
353 *
354 **/
355static void xuartps_stop_tx(struct uart_port *port)
356{
357 unsigned int regval;
358
359 regval = xuartps_readl(XUARTPS_CR_OFFSET);
360 regval |= XUARTPS_CR_TX_DIS;
361 /* Disable the transmitter */
362 xuartps_writel(regval, XUARTPS_CR_OFFSET);
363}
364
365/**
366 * xuartps_stop_rx - Stop RX
367 * @port: Handle to the uart port structure
368 *
369 **/
370static void xuartps_stop_rx(struct uart_port *port)
371{
372 unsigned int regval;
373
374 regval = xuartps_readl(XUARTPS_CR_OFFSET);
375 regval |= XUARTPS_CR_RX_DIS;
376 /* Disable the receiver */
377 xuartps_writel(regval, XUARTPS_CR_OFFSET);
378}
379
380/**
381 * xuartps_tx_empty - Check whether TX is empty
382 * @port: Handle to the uart port structure
383 *
384 * Returns TIOCSER_TEMT on success, 0 otherwise
385 **/
386static unsigned int xuartps_tx_empty(struct uart_port *port)
387{
388 unsigned int status;
389
390 status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
391 return status ? TIOCSER_TEMT : 0;
392}
393
394/**
395 * xuartps_break_ctl - Based on the input ctl we have to start or stop
396 * transmitting char breaks
397 * @port: Handle to the uart port structure
398 * @ctl: Value based on which start or stop decision is taken
399 *
400 **/
401static void xuartps_break_ctl(struct uart_port *port, int ctl)
402{
403 unsigned int status;
404 unsigned long flags;
405
406 spin_lock_irqsave(&port->lock, flags);
407
408 status = xuartps_readl(XUARTPS_CR_OFFSET);
409
410 if (ctl == -1)
411 xuartps_writel(XUARTPS_CR_STARTBRK | status,
412 XUARTPS_CR_OFFSET);
413 else {
414 if ((status & XUARTPS_CR_STOPBRK) == 0)
415 xuartps_writel(XUARTPS_CR_STOPBRK | status,
416 XUARTPS_CR_OFFSET);
417 }
418 spin_unlock_irqrestore(&port->lock, flags);
419}
420
421/**
422 * xuartps_set_termios - termios operations, handling data length, parity,
423 * stop bits, flow control, baud rate
424 * @port: Handle to the uart port structure
425 * @termios: Handle to the input termios structure
426 * @old: Values of the previously saved termios structure
427 *
428 **/
429static void xuartps_set_termios(struct uart_port *port,
430 struct ktermios *termios, struct ktermios *old)
431{
432 unsigned int cval = 0;
433 unsigned int baud;
434 unsigned long flags;
435 unsigned int ctrl_reg, mode_reg;
436
437 spin_lock_irqsave(&port->lock, flags);
438
439 /* Empty the receive FIFO 1st before making changes */
440 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
441 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
442 xuartps_readl(XUARTPS_FIFO_OFFSET);
443 }
444
445 /* Disable the TX and RX to set baud rate */
446 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
447 (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
448 XUARTPS_CR_OFFSET);
449
450 /* Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk */
451 baud = uart_get_baud_rate(port, termios, old, 0, 10000000);
452 baud = xuartps_set_baud_rate(port, baud);
453 if (tty_termios_baud_rate(termios))
454 tty_termios_encode_baud_rate(termios, baud, baud);
455
456 /*
457 * Update the per-port timeout.
458 */
459 uart_update_timeout(port, termios->c_cflag, baud);
460
461 /* Set TX/RX Reset */
462 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
463 (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
464 XUARTPS_CR_OFFSET);
465
466 ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
467
468 /* Clear the RX disable and TX disable bits and then set the TX enable
469 * bit and RX enable bit to enable the transmitter and receiver.
470 */
471 xuartps_writel(
472 (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
473 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
474 XUARTPS_CR_OFFSET);
475
476 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
477
478 port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
479 XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
480 port->ignore_status_mask = 0;
481
482 if (termios->c_iflag & INPCK)
483 port->read_status_mask |= XUARTPS_IXR_PARITY |
484 XUARTPS_IXR_FRAMING;
485
486 if (termios->c_iflag & IGNPAR)
487 port->ignore_status_mask |= XUARTPS_IXR_PARITY |
488 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
489
490 /* ignore all characters if CREAD is not set */
491 if ((termios->c_cflag & CREAD) == 0)
492 port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
493 XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
494 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
495
496 mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
497
498 /* Handling Data Size */
499 switch (termios->c_cflag & CSIZE) {
500 case CS6:
501 cval |= XUARTPS_MR_CHARLEN_6_BIT;
502 break;
503 case CS7:
504 cval |= XUARTPS_MR_CHARLEN_7_BIT;
505 break;
506 default:
507 case CS8:
508 cval |= XUARTPS_MR_CHARLEN_8_BIT;
509 termios->c_cflag &= ~CSIZE;
510 termios->c_cflag |= CS8;
511 break;
512 }
513
514 /* Handling Parity and Stop Bits length */
515 if (termios->c_cflag & CSTOPB)
516 cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
517 else
518 cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
519
520 if (termios->c_cflag & PARENB) {
521 /* Mark or Space parity */
522 if (termios->c_cflag & CMSPAR) {
523 if (termios->c_cflag & PARODD)
524 cval |= XUARTPS_MR_PARITY_MARK;
525 else
526 cval |= XUARTPS_MR_PARITY_SPACE;
527 } else if (termios->c_cflag & PARODD)
528 cval |= XUARTPS_MR_PARITY_ODD;
529 else
530 cval |= XUARTPS_MR_PARITY_EVEN;
531 } else
532 cval |= XUARTPS_MR_PARITY_NONE;
533 xuartps_writel(cval , XUARTPS_MR_OFFSET);
534
535 spin_unlock_irqrestore(&port->lock, flags);
536}
537
538/**
539 * xuartps_startup - Called when an application opens a xuartps port
540 * @port: Handle to the uart port structure
541 *
542 * Returns 0 on success, negative error otherwise
543 **/
544static int xuartps_startup(struct uart_port *port)
545{
546 unsigned int retval = 0, status = 0;
547
548 retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
549 (void *)port);
550 if (retval)
551 return retval;
552
553 /* Disable the TX and RX */
554 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
555 XUARTPS_CR_OFFSET);
556
557 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
558 * no break chars.
559 */
560 xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
561 XUARTPS_CR_OFFSET);
562
563 status = xuartps_readl(XUARTPS_CR_OFFSET);
564
565 /* Clear the RX disable and TX disable bits and then set the TX enable
566 * bit and RX enable bit to enable the transmitter and receiver.
567 */
568 xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
569 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
570 XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
571
572 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
573 * no parity.
574 */
575 xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
576 | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
577 XUARTPS_MR_OFFSET);
578
579 /* Set the RX FIFO Trigger level to 14 assuming FIFO size as 16 */
580 xuartps_writel(14, XUARTPS_RXWM_OFFSET);
581
582 /* Receive Timeout register is enabled with value of 10 */
583 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
584
585
586 /* Set the Interrupt Registers with desired interrupts */
587 xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
588 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
589 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
590 xuartps_writel(~(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
591 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
592 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT), XUARTPS_IDR_OFFSET);
593
594 return retval;
595}
596
597/**
598 * xuartps_shutdown - Called when an application closes a xuartps port
599 * @port: Handle to the uart port structure
600 *
601 **/
602static void xuartps_shutdown(struct uart_port *port)
603{
604 int status;
605
606 /* Disable interrupts */
607 status = xuartps_readl(XUARTPS_IMR_OFFSET);
608 xuartps_writel(status, XUARTPS_IDR_OFFSET);
609
610 /* Disable the TX and RX */
611 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
612 XUARTPS_CR_OFFSET);
613 free_irq(port->irq, port);
614}
615
616/**
617 * xuartps_type - Set UART type to xuartps port
618 * @port: Handle to the uart port structure
619 *
620 * Returns string on success, NULL otherwise
621 **/
622static const char *xuartps_type(struct uart_port *port)
623{
624 return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
625}
626
627/**
628 * xuartps_verify_port - Verify the port params
629 * @port: Handle to the uart port structure
630 * @ser: Handle to the structure whose members are compared
631 *
632 * Returns 0 if success otherwise -EINVAL
633 **/
634static int xuartps_verify_port(struct uart_port *port,
635 struct serial_struct *ser)
636{
637 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
638 return -EINVAL;
639 if (port->irq != ser->irq)
640 return -EINVAL;
641 if (ser->io_type != UPIO_MEM)
642 return -EINVAL;
643 if (port->iobase != ser->port)
644 return -EINVAL;
645 if (ser->hub6 != 0)
646 return -EINVAL;
647 return 0;
648}
649
650/**
651 * xuartps_request_port - Claim the memory region attached to xuartps port,
652 * called when the driver adds a xuartps port via
653 * uart_add_one_port()
654 * @port: Handle to the uart port structure
655 *
656 * Returns 0, -ENOMEM if request fails
657 **/
658static int xuartps_request_port(struct uart_port *port)
659{
660 if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
661 XUARTPS_NAME)) {
662 return -ENOMEM;
663 }
664
665 port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
666 if (!port->membase) {
667 dev_err(port->dev, "Unable to map registers\n");
668 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
669 return -ENOMEM;
670 }
671 return 0;
672}
673
674/**
675 * xuartps_release_port - Release the memory region attached to a xuartps
676 * port, called when the driver removes a xuartps
677 * port via uart_remove_one_port().
678 * @port: Handle to the uart port structure
679 *
680 **/
681static void xuartps_release_port(struct uart_port *port)
682{
683 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
684 iounmap(port->membase);
685 port->membase = NULL;
686}
687
688/**
689 * xuartps_config_port - Configure xuartps, called when the driver adds a
690 * xuartps port
691 * @port: Handle to the uart port structure
692 * @flags: If any
693 *
694 **/
695static void xuartps_config_port(struct uart_port *port, int flags)
696{
697 if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
698 port->type = PORT_XUARTPS;
699}
700
701/**
702 * xuartps_get_mctrl - Get the modem control state
703 *
704 * @port: Handle to the uart port structure
705 *
706 * Returns the modem control state
707 *
708 **/
709static unsigned int xuartps_get_mctrl(struct uart_port *port)
710{
711 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
712}
713
714static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
715{
716 /* N/A */
717}
718
719static void xuartps_enable_ms(struct uart_port *port)
720{
721 /* N/A */
722}
723
724/** The UART operations structure
725 */
726static struct uart_ops xuartps_ops = {
727 .set_mctrl = xuartps_set_mctrl,
728 .get_mctrl = xuartps_get_mctrl,
729 .enable_ms = xuartps_enable_ms,
730
731 .start_tx = xuartps_start_tx, /* Start transmitting */
732 .stop_tx = xuartps_stop_tx, /* Stop transmission */
733 .stop_rx = xuartps_stop_rx, /* Stop reception */
734 .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
735 .break_ctl = xuartps_break_ctl, /* Start/stop
736 * transmitting break
737 */
738 .set_termios = xuartps_set_termios, /* Set termios */
739 .startup = xuartps_startup, /* App opens xuartps */
740 .shutdown = xuartps_shutdown, /* App closes xuartps */
741 .type = xuartps_type, /* Set UART type */
742 .verify_port = xuartps_verify_port, /* Verification of port
743 * params
744 */
745 .request_port = xuartps_request_port, /* Claim resources
746 * associated with a
747 * xuartps port
748 */
749 .release_port = xuartps_release_port, /* Release resources
750 * associated with a
751 * xuartps port
752 */
753 .config_port = xuartps_config_port, /* Configure when driver
754 * adds a xuartps port
755 */
756};
757
758static struct uart_port xuartps_port[2];
759
760/**
761 * xuartps_get_port - Configure the port from the platform device resource
762 * info
763 *
764 * Returns a pointer to a uart_port or NULL for failure
765 **/
766static struct uart_port *xuartps_get_port(void)
767{
768 struct uart_port *port;
769 int id;
770
771 /* Find the next unused port */
772 for (id = 0; id < XUARTPS_NR_PORTS; id++)
773 if (xuartps_port[id].mapbase == 0)
774 break;
775
776 if (id >= XUARTPS_NR_PORTS)
777 return NULL;
778
779 port = &xuartps_port[id];
780
781 /* At this point, we've got an empty uart_port struct, initialize it */
782 spin_lock_init(&port->lock);
783 port->membase = NULL;
784 port->iobase = 1; /* mark port in use */
785 port->irq = 0;
786 port->type = PORT_UNKNOWN;
787 port->iotype = UPIO_MEM32;
788 port->flags = UPF_BOOT_AUTOCONF;
789 port->ops = &xuartps_ops;
790 port->fifosize = XUARTPS_FIFO_SIZE;
791 port->line = id;
792 port->dev = NULL;
793 return port;
794}
795
796/*-----------------------Console driver operations--------------------------*/
797
798#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
799/**
800 * xuartps_console_wait_tx - Wait for the TX to be full
801 * @port: Handle to the uart port structure
802 *
803 **/
804static void xuartps_console_wait_tx(struct uart_port *port)
805{
806 while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
807 != XUARTPS_SR_TXEMPTY)
808 barrier();
809}
810
811/**
812 * xuartps_console_putchar - write the character to the FIFO buffer
813 * @port: Handle to the uart port structure
814 * @ch: Character to be written
815 *
816 **/
817static void xuartps_console_putchar(struct uart_port *port, int ch)
818{
819 xuartps_console_wait_tx(port);
820 xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
821}
822
823/**
824 * xuartps_console_write - perform write operation
825 * @port: Handle to the uart port structure
826 * @s: Pointer to character array
827 * @count: No of characters
828 **/
829static void xuartps_console_write(struct console *co, const char *s,
830 unsigned int count)
831{
832 struct uart_port *port = &xuartps_port[co->index];
833 unsigned long flags;
834 unsigned int imr;
835 int locked = 1;
836
837 if (oops_in_progress)
838 locked = spin_trylock_irqsave(&port->lock, flags);
839 else
840 spin_lock_irqsave(&port->lock, flags);
841
842 /* save and disable interrupt */
843 imr = xuartps_readl(XUARTPS_IMR_OFFSET);
844 xuartps_writel(imr, XUARTPS_IDR_OFFSET);
845
846 uart_console_write(port, s, count, xuartps_console_putchar);
847 xuartps_console_wait_tx(port);
848
849 /* restore interrupt state, it seems like there may be a h/w bug
850 * in that the interrupt enable register should not need to be
851 * written based on the data sheet
852 */
853 xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
854 xuartps_writel(imr, XUARTPS_IER_OFFSET);
855
856 if (locked)
857 spin_unlock_irqrestore(&port->lock, flags);
858}
859
860/**
861 * xuartps_console_setup - Initialize the uart to default config
862 * @co: Console handle
863 * @options: Initial settings of uart
864 *
865 * Returns 0, -ENODEV if no device
866 **/
867static int __init xuartps_console_setup(struct console *co, char *options)
868{
869 struct uart_port *port = &xuartps_port[co->index];
870 int baud = 9600;
871 int bits = 8;
872 int parity = 'n';
873 int flow = 'n';
874
875 if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
876 return -EINVAL;
877
878 if (!port->mapbase) {
879 pr_debug("console on ttyPS%i not present\n", co->index);
880 return -ENODEV;
881 }
882
883 if (options)
884 uart_parse_options(options, &baud, &parity, &bits, &flow);
885
886 return uart_set_options(port, co, baud, parity, bits, flow);
887}
888
889static struct uart_driver xuartps_uart_driver;
890
891static struct console xuartps_console = {
892 .name = XUARTPS_TTY_NAME,
893 .write = xuartps_console_write,
894 .device = uart_console_device,
895 .setup = xuartps_console_setup,
896 .flags = CON_PRINTBUFFER,
897 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
898 .data = &xuartps_uart_driver,
899};
900
901/**
902 * xuartps_console_init - Initialization call
903 *
904 * Returns 0 on success, negative error otherwise
905 **/
906static int __init xuartps_console_init(void)
907{
908 register_console(&xuartps_console);
909 return 0;
910}
911
912console_initcall(xuartps_console_init);
913
914#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
915
916/** Structure Definitions
917 */
918static struct uart_driver xuartps_uart_driver = {
919 .owner = THIS_MODULE, /* Owner */
920 .driver_name = XUARTPS_NAME, /* Driver name */
921 .dev_name = XUARTPS_TTY_NAME, /* Node name */
922 .major = XUARTPS_MAJOR, /* Major number */
923 .minor = XUARTPS_MINOR, /* Minor number */
924 .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
925#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
926 .cons = &xuartps_console, /* Console */
927#endif
928};
929
930/* ---------------------------------------------------------------------
931 * Platform bus binding
932 */
933/**
934 * xuartps_probe - Platform driver probe
935 * @pdev: Pointer to the platform device structure
936 *
937 * Returns 0 on success, negative error otherwise
938 **/
939static int __devinit xuartps_probe(struct platform_device *pdev)
940{
941 int rc;
942 struct uart_port *port;
943 struct resource *res, *res2;
944 int clk = 0;
945
946#ifdef CONFIG_OF
947 const unsigned int *prop;
948
949 prop = of_get_property(pdev->dev.of_node, "clock", NULL);
950 if (prop)
951 clk = be32_to_cpup(prop);
952#else
953 clk = *((unsigned int *)(pdev->dev.platform_data));
954#endif
955 if (!clk) {
956 dev_err(&pdev->dev, "no clock specified\n");
957 return -ENODEV;
958 }
959
960 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
961 if (!res)
962 return -ENODEV;
963
964 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
965 if (!res2)
966 return -ENODEV;
967
968 /* Initialize the port structure */
969 port = xuartps_get_port();
970
971 if (!port) {
972 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
973 return -ENODEV;
974 } else {
975 /* Register the port.
976 * This function also registers this device with the tty layer
977 * and triggers invocation of the config_port() entry point.
978 */
979 port->mapbase = res->start;
980 port->irq = res2->start;
981 port->dev = &pdev->dev;
982 port->uartclk = clk;
983 dev_set_drvdata(&pdev->dev, port);
984 rc = uart_add_one_port(&xuartps_uart_driver, port);
985 if (rc) {
986 dev_err(&pdev->dev,
987 "uart_add_one_port() failed; err=%i\n", rc);
988 dev_set_drvdata(&pdev->dev, NULL);
989 return rc;
990 }
991 return 0;
992 }
993}
994
995/**
996 * xuartps_remove - called when the platform driver is unregistered
997 * @pdev: Pointer to the platform device structure
998 *
999 * Returns 0 on success, negative error otherwise
1000 **/
1001static int __devexit xuartps_remove(struct platform_device *pdev)
1002{
1003 struct uart_port *port = dev_get_drvdata(&pdev->dev);
1004 int rc = 0;
1005
1006 /* Remove the xuartps port from the serial core */
1007 if (port) {
1008 rc = uart_remove_one_port(&xuartps_uart_driver, port);
1009 dev_set_drvdata(&pdev->dev, NULL);
1010 port->mapbase = 0;
1011 }
1012 return rc;
1013}
1014
1015/**
1016 * xuartps_suspend - suspend event
1017 * @pdev: Pointer to the platform device structure
1018 * @state: State of the device
1019 *
1020 * Returns 0
1021 **/
1022static int xuartps_suspend(struct platform_device *pdev, pm_message_t state)
1023{
1024 /* Call the API provided in serial_core.c file which handles
1025 * the suspend.
1026 */
1027 uart_suspend_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1028 return 0;
1029}
1030
1031/**
1032 * xuartps_resume - Resume after a previous suspend
1033 * @pdev: Pointer to the platform device structure
1034 *
1035 * Returns 0
1036 **/
1037static int xuartps_resume(struct platform_device *pdev)
1038{
1039 uart_resume_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1040 return 0;
1041}
1042
1043/* Match table for of_platform binding */
1044
1045#ifdef CONFIG_OF
1046static struct of_device_id xuartps_of_match[] __devinitdata = {
1047 { .compatible = "xlnx,xuartps", },
1048 {}
1049};
1050MODULE_DEVICE_TABLE(of, xuartps_of_match);
1051#else
1052#define xuartps_of_match NULL
1053#endif
1054
1055static struct platform_driver xuartps_platform_driver = {
1056 .probe = xuartps_probe, /* Probe method */
1057 .remove = __exit_p(xuartps_remove), /* Detach method */
1058 .suspend = xuartps_suspend, /* Suspend */
1059 .resume = xuartps_resume, /* Resume after a suspend */
1060 .driver = {
1061 .owner = THIS_MODULE,
1062 .name = XUARTPS_NAME, /* Driver name */
1063 .of_match_table = xuartps_of_match,
1064 },
1065};
1066
1067/* ---------------------------------------------------------------------
1068 * Module Init and Exit
1069 */
1070/**
1071 * xuartps_init - Initial driver registration call
1072 *
1073 * Returns whether the registration was successful or not
1074 **/
1075static int __init xuartps_init(void)
1076{
1077 int retval = 0;
1078
1079 /* Register the xuartps driver with the serial core */
1080 retval = uart_register_driver(&xuartps_uart_driver);
1081 if (retval)
1082 return retval;
1083
1084 /* Register the platform driver */
1085 retval = platform_driver_register(&xuartps_platform_driver);
1086 if (retval)
1087 uart_unregister_driver(&xuartps_uart_driver);
1088
1089 return retval;
1090}
1091
1092/**
1093 * xuartps_exit - Driver unregistration call
1094 **/
1095static void __exit xuartps_exit(void)
1096{
1097 /* The order of unregistration is important. Unregister the
1098 * UART driver before the platform driver crashes the system.
1099 */
1100
1101 /* Unregister the platform driver */
1102 platform_driver_unregister(&xuartps_platform_driver);
1103
1104 /* Unregister the xuartps driver */
1105 uart_unregister_driver(&xuartps_uart_driver);
1106}
1107
1108module_init(xuartps_init);
1109module_exit(xuartps_exit);
1110
1111MODULE_DESCRIPTION("Driver for PS UART");
1112MODULE_AUTHOR("Xilinx Inc.");
1113MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/zs.c b/drivers/tty/serial/zs.c
new file mode 100644
index 000000000000..1a7fd3e70315
--- /dev/null
+++ b/drivers/tty/serial/zs.c
@@ -0,0 +1,1304 @@
1/*
2 * zs.c: Serial port driver for IOASIC DECstations.
3 *
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6 *
7 * DECstation changes
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
10 *
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 *
15 *
16 * Note: for IOASIC systems the wiring is as follows:
17 *
18 * mouse/keyboard:
19 * DIN-7 MJ-4 signal SCC
20 * 2 1 TxD <- A.TxD
21 * 3 4 RxD -> A.RxD
22 *
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal SCC
25 * 2 2 TxD <- B.TxD
26 * 3 5 RxD -> B.RxD
27 * 4 RTS <- ~A.RTS
28 * 5 CTS -> ~B.CTS
29 * 6 6 DSR -> ~A.SYNC
30 * 8 CD -> ~B.DCD
31 * 12 DSRS(DCE) -> ~A.CTS (*)
32 * 15 TxC -> B.TxC
33 * 17 RxC -> B.RxC
34 * 20 1 DTR <- ~A.DTR
35 * 22 RI -> ~A.DCD
36 * 23 DSRS(DTE) <- ~B.RTS
37 *
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
40 *
41 * As you can immediately notice the wiring of the RTS, DTR and DSR signals
42 * is a bit odd. This makes the handling of port B unnecessarily
43 * complicated and prevents the use of some automatic modes of operation.
44 */
45
46#if defined(CONFIG_SERIAL_ZS_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
47#define SUPPORT_SYSRQ
48#endif
49
50#include <linux/bug.h>
51#include <linux/console.h>
52#include <linux/delay.h>
53#include <linux/errno.h>
54#include <linux/init.h>
55#include <linux/interrupt.h>
56#include <linux/io.h>
57#include <linux/ioport.h>
58#include <linux/irqflags.h>
59#include <linux/kernel.h>
60#include <linux/major.h>
61#include <linux/serial.h>
62#include <linux/serial_core.h>
63#include <linux/spinlock.h>
64#include <linux/sysrq.h>
65#include <linux/tty.h>
66#include <linux/types.h>
67
68#include <asm/atomic.h>
69#include <asm/system.h>
70
71#include <asm/dec/interrupts.h>
72#include <asm/dec/ioasic_addrs.h>
73#include <asm/dec/system.h>
74
75#include "zs.h"
76
77
78MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
79MODULE_DESCRIPTION("DECstation Z85C30 serial driver");
80MODULE_LICENSE("GPL");
81
82
83static char zs_name[] __initdata = "DECstation Z85C30 serial driver version ";
84static char zs_version[] __initdata = "0.10";
85
86/*
87 * It would be nice to dynamically allocate everything that
88 * depends on ZS_NUM_SCCS, so we could support any number of
89 * Z85C30s, but for now...
90 */
91#define ZS_NUM_SCCS 2 /* Max # of ZS chips supported. */
92#define ZS_NUM_CHAN 2 /* 2 channels per chip. */
93#define ZS_CHAN_A 0 /* Index of the channel A. */
94#define ZS_CHAN_B 1 /* Index of the channel B. */
95#define ZS_CHAN_IO_SIZE 8 /* IOMEM space size. */
96#define ZS_CHAN_IO_STRIDE 4 /* Register alignment. */
97#define ZS_CHAN_IO_OFFSET 1 /* The SCC resides on the high byte
98 of the 16-bit IOBUS. */
99#define ZS_CLOCK 7372800 /* Z85C30 PCLK input clock rate. */
100
101#define to_zport(uport) container_of(uport, struct zs_port, port)
102
103struct zs_parms {
104 resource_size_t scc[ZS_NUM_SCCS];
105 int irq[ZS_NUM_SCCS];
106};
107
108static struct zs_scc zs_sccs[ZS_NUM_SCCS];
109
110static u8 zs_init_regs[ZS_NUM_REGS] __initdata = {
111 0, /* write 0 */
112 PAR_SPEC, /* write 1 */
113 0, /* write 2 */
114 0, /* write 3 */
115 X16CLK | SB1, /* write 4 */
116 0, /* write 5 */
117 0, 0, 0, /* write 6, 7, 8 */
118 MIE | DLC | NV, /* write 9 */
119 NRZ, /* write 10 */
120 TCBR | RCBR, /* write 11 */
121 0, 0, /* BRG time constant, write 12 + 13 */
122 BRSRC | BRENABL, /* write 14 */
123 0, /* write 15 */
124};
125
126/*
127 * Debugging.
128 */
129#undef ZS_DEBUG_REGS
130
131
132/*
133 * Reading and writing Z85C30 registers.
134 */
135static void recovery_delay(void)
136{
137 udelay(2);
138}
139
140static u8 read_zsreg(struct zs_port *zport, int reg)
141{
142 void __iomem *control = zport->port.membase + ZS_CHAN_IO_OFFSET;
143 u8 retval;
144
145 if (reg != 0) {
146 writeb(reg & 0xf, control);
147 fast_iob();
148 recovery_delay();
149 }
150 retval = readb(control);
151 recovery_delay();
152 return retval;
153}
154
155static void write_zsreg(struct zs_port *zport, int reg, u8 value)
156{
157 void __iomem *control = zport->port.membase + ZS_CHAN_IO_OFFSET;
158
159 if (reg != 0) {
160 writeb(reg & 0xf, control);
161 fast_iob(); recovery_delay();
162 }
163 writeb(value, control);
164 fast_iob();
165 recovery_delay();
166 return;
167}
168
169static u8 read_zsdata(struct zs_port *zport)
170{
171 void __iomem *data = zport->port.membase +
172 ZS_CHAN_IO_STRIDE + ZS_CHAN_IO_OFFSET;
173 u8 retval;
174
175 retval = readb(data);
176 recovery_delay();
177 return retval;
178}
179
180static void write_zsdata(struct zs_port *zport, u8 value)
181{
182 void __iomem *data = zport->port.membase +
183 ZS_CHAN_IO_STRIDE + ZS_CHAN_IO_OFFSET;
184
185 writeb(value, data);
186 fast_iob();
187 recovery_delay();
188 return;
189}
190
191#ifdef ZS_DEBUG_REGS
192void zs_dump(void)
193{
194 struct zs_port *zport;
195 int i, j;
196
197 for (i = 0; i < ZS_NUM_SCCS * ZS_NUM_CHAN; i++) {
198 zport = &zs_sccs[i / ZS_NUM_CHAN].zport[i % ZS_NUM_CHAN];
199
200 if (!zport->scc)
201 continue;
202
203 for (j = 0; j < 16; j++)
204 printk("W%-2d = 0x%02x\t", j, zport->regs[j]);
205 printk("\n");
206 for (j = 0; j < 16; j++)
207 printk("R%-2d = 0x%02x\t", j, read_zsreg(zport, j));
208 printk("\n\n");
209 }
210}
211#endif
212
213
214static void zs_spin_lock_cond_irq(spinlock_t *lock, int irq)
215{
216 if (irq)
217 spin_lock_irq(lock);
218 else
219 spin_lock(lock);
220}
221
222static void zs_spin_unlock_cond_irq(spinlock_t *lock, int irq)
223{
224 if (irq)
225 spin_unlock_irq(lock);
226 else
227 spin_unlock(lock);
228}
229
230static int zs_receive_drain(struct zs_port *zport)
231{
232 int loops = 10000;
233
234 while ((read_zsreg(zport, R0) & Rx_CH_AV) && --loops)
235 read_zsdata(zport);
236 return loops;
237}
238
239static int zs_transmit_drain(struct zs_port *zport, int irq)
240{
241 struct zs_scc *scc = zport->scc;
242 int loops = 10000;
243
244 while (!(read_zsreg(zport, R0) & Tx_BUF_EMP) && --loops) {
245 zs_spin_unlock_cond_irq(&scc->zlock, irq);
246 udelay(2);
247 zs_spin_lock_cond_irq(&scc->zlock, irq);
248 }
249 return loops;
250}
251
252static int zs_line_drain(struct zs_port *zport, int irq)
253{
254 struct zs_scc *scc = zport->scc;
255 int loops = 10000;
256
257 while (!(read_zsreg(zport, R1) & ALL_SNT) && --loops) {
258 zs_spin_unlock_cond_irq(&scc->zlock, irq);
259 udelay(2);
260 zs_spin_lock_cond_irq(&scc->zlock, irq);
261 }
262 return loops;
263}
264
265
266static void load_zsregs(struct zs_port *zport, u8 *regs, int irq)
267{
268 /* Let the current transmission finish. */
269 zs_line_drain(zport, irq);
270 /* Load 'em up. */
271 write_zsreg(zport, R3, regs[3] & ~RxENABLE);
272 write_zsreg(zport, R5, regs[5] & ~TxENAB);
273 write_zsreg(zport, R4, regs[4]);
274 write_zsreg(zport, R9, regs[9]);
275 write_zsreg(zport, R1, regs[1]);
276 write_zsreg(zport, R2, regs[2]);
277 write_zsreg(zport, R10, regs[10]);
278 write_zsreg(zport, R14, regs[14] & ~BRENABL);
279 write_zsreg(zport, R11, regs[11]);
280 write_zsreg(zport, R12, regs[12]);
281 write_zsreg(zport, R13, regs[13]);
282 write_zsreg(zport, R14, regs[14]);
283 write_zsreg(zport, R15, regs[15]);
284 if (regs[3] & RxENABLE)
285 write_zsreg(zport, R3, regs[3]);
286 if (regs[5] & TxENAB)
287 write_zsreg(zport, R5, regs[5]);
288 return;
289}
290
291
292/*
293 * Status handling routines.
294 */
295
296/*
297 * zs_tx_empty() -- get the transmitter empty status
298 *
299 * Purpose: Let user call ioctl() to get info when the UART physically
300 * is emptied. On bus types like RS485, the transmitter must
301 * release the bus after transmitting. This must be done when
302 * the transmit shift register is empty, not be done when the
303 * transmit holding register is empty. This functionality
304 * allows an RS485 driver to be written in user space.
305 */
306static unsigned int zs_tx_empty(struct uart_port *uport)
307{
308 struct zs_port *zport = to_zport(uport);
309 struct zs_scc *scc = zport->scc;
310 unsigned long flags;
311 u8 status;
312
313 spin_lock_irqsave(&scc->zlock, flags);
314 status = read_zsreg(zport, R1);
315 spin_unlock_irqrestore(&scc->zlock, flags);
316
317 return status & ALL_SNT ? TIOCSER_TEMT : 0;
318}
319
320static unsigned int zs_raw_get_ab_mctrl(struct zs_port *zport_a,
321 struct zs_port *zport_b)
322{
323 u8 status_a, status_b;
324 unsigned int mctrl;
325
326 status_a = read_zsreg(zport_a, R0);
327 status_b = read_zsreg(zport_b, R0);
328
329 mctrl = ((status_b & CTS) ? TIOCM_CTS : 0) |
330 ((status_b & DCD) ? TIOCM_CAR : 0) |
331 ((status_a & DCD) ? TIOCM_RNG : 0) |
332 ((status_a & SYNC_HUNT) ? TIOCM_DSR : 0);
333
334 return mctrl;
335}
336
337static unsigned int zs_raw_get_mctrl(struct zs_port *zport)
338{
339 struct zs_port *zport_a = &zport->scc->zport[ZS_CHAN_A];
340
341 return zport != zport_a ? zs_raw_get_ab_mctrl(zport_a, zport) : 0;
342}
343
344static unsigned int zs_raw_xor_mctrl(struct zs_port *zport)
345{
346 struct zs_port *zport_a = &zport->scc->zport[ZS_CHAN_A];
347 unsigned int mmask, mctrl, delta;
348 u8 mask_a, mask_b;
349
350 if (zport == zport_a)
351 return 0;
352
353 mask_a = zport_a->regs[15];
354 mask_b = zport->regs[15];
355
356 mmask = ((mask_b & CTSIE) ? TIOCM_CTS : 0) |
357 ((mask_b & DCDIE) ? TIOCM_CAR : 0) |
358 ((mask_a & DCDIE) ? TIOCM_RNG : 0) |
359 ((mask_a & SYNCIE) ? TIOCM_DSR : 0);
360
361 mctrl = zport->mctrl;
362 if (mmask) {
363 mctrl &= ~mmask;
364 mctrl |= zs_raw_get_ab_mctrl(zport_a, zport) & mmask;
365 }
366
367 delta = mctrl ^ zport->mctrl;
368 if (delta)
369 zport->mctrl = mctrl;
370
371 return delta;
372}
373
374static unsigned int zs_get_mctrl(struct uart_port *uport)
375{
376 struct zs_port *zport = to_zport(uport);
377 struct zs_scc *scc = zport->scc;
378 unsigned int mctrl;
379
380 spin_lock(&scc->zlock);
381 mctrl = zs_raw_get_mctrl(zport);
382 spin_unlock(&scc->zlock);
383
384 return mctrl;
385}
386
387static void zs_set_mctrl(struct uart_port *uport, unsigned int mctrl)
388{
389 struct zs_port *zport = to_zport(uport);
390 struct zs_scc *scc = zport->scc;
391 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
392 u8 oldloop, newloop;
393
394 spin_lock(&scc->zlock);
395 if (zport != zport_a) {
396 if (mctrl & TIOCM_DTR)
397 zport_a->regs[5] |= DTR;
398 else
399 zport_a->regs[5] &= ~DTR;
400 if (mctrl & TIOCM_RTS)
401 zport_a->regs[5] |= RTS;
402 else
403 zport_a->regs[5] &= ~RTS;
404 write_zsreg(zport_a, R5, zport_a->regs[5]);
405 }
406
407 /* Rarely modified, so don't poke at hardware unless necessary. */
408 oldloop = zport->regs[14];
409 newloop = oldloop;
410 if (mctrl & TIOCM_LOOP)
411 newloop |= LOOPBAK;
412 else
413 newloop &= ~LOOPBAK;
414 if (newloop != oldloop) {
415 zport->regs[14] = newloop;
416 write_zsreg(zport, R14, zport->regs[14]);
417 }
418 spin_unlock(&scc->zlock);
419}
420
421static void zs_raw_stop_tx(struct zs_port *zport)
422{
423 write_zsreg(zport, R0, RES_Tx_P);
424 zport->tx_stopped = 1;
425}
426
427static void zs_stop_tx(struct uart_port *uport)
428{
429 struct zs_port *zport = to_zport(uport);
430 struct zs_scc *scc = zport->scc;
431
432 spin_lock(&scc->zlock);
433 zs_raw_stop_tx(zport);
434 spin_unlock(&scc->zlock);
435}
436
437static void zs_raw_transmit_chars(struct zs_port *);
438
439static void zs_start_tx(struct uart_port *uport)
440{
441 struct zs_port *zport = to_zport(uport);
442 struct zs_scc *scc = zport->scc;
443
444 spin_lock(&scc->zlock);
445 if (zport->tx_stopped) {
446 zs_transmit_drain(zport, 0);
447 zport->tx_stopped = 0;
448 zs_raw_transmit_chars(zport);
449 }
450 spin_unlock(&scc->zlock);
451}
452
453static void zs_stop_rx(struct uart_port *uport)
454{
455 struct zs_port *zport = to_zport(uport);
456 struct zs_scc *scc = zport->scc;
457 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
458
459 spin_lock(&scc->zlock);
460 zport->regs[15] &= ~BRKIE;
461 zport->regs[1] &= ~(RxINT_MASK | TxINT_ENAB);
462 zport->regs[1] |= RxINT_DISAB;
463
464 if (zport != zport_a) {
465 /* A-side DCD tracks RI and SYNC tracks DSR. */
466 zport_a->regs[15] &= ~(DCDIE | SYNCIE);
467 write_zsreg(zport_a, R15, zport_a->regs[15]);
468 if (!(zport_a->regs[15] & BRKIE)) {
469 zport_a->regs[1] &= ~EXT_INT_ENAB;
470 write_zsreg(zport_a, R1, zport_a->regs[1]);
471 }
472
473 /* This-side DCD tracks DCD and CTS tracks CTS. */
474 zport->regs[15] &= ~(DCDIE | CTSIE);
475 zport->regs[1] &= ~EXT_INT_ENAB;
476 } else {
477 /* DCD tracks RI and SYNC tracks DSR for the B side. */
478 if (!(zport->regs[15] & (DCDIE | SYNCIE)))
479 zport->regs[1] &= ~EXT_INT_ENAB;
480 }
481
482 write_zsreg(zport, R15, zport->regs[15]);
483 write_zsreg(zport, R1, zport->regs[1]);
484 spin_unlock(&scc->zlock);
485}
486
487static void zs_enable_ms(struct uart_port *uport)
488{
489 struct zs_port *zport = to_zport(uport);
490 struct zs_scc *scc = zport->scc;
491 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
492
493 if (zport == zport_a)
494 return;
495
496 spin_lock(&scc->zlock);
497
498 /* Clear Ext interrupts if not being handled already. */
499 if (!(zport_a->regs[1] & EXT_INT_ENAB))
500 write_zsreg(zport_a, R0, RES_EXT_INT);
501
502 /* A-side DCD tracks RI and SYNC tracks DSR. */
503 zport_a->regs[1] |= EXT_INT_ENAB;
504 zport_a->regs[15] |= DCDIE | SYNCIE;
505
506 /* This-side DCD tracks DCD and CTS tracks CTS. */
507 zport->regs[15] |= DCDIE | CTSIE;
508
509 zs_raw_xor_mctrl(zport);
510
511 write_zsreg(zport_a, R1, zport_a->regs[1]);
512 write_zsreg(zport_a, R15, zport_a->regs[15]);
513 write_zsreg(zport, R15, zport->regs[15]);
514 spin_unlock(&scc->zlock);
515}
516
517static void zs_break_ctl(struct uart_port *uport, int break_state)
518{
519 struct zs_port *zport = to_zport(uport);
520 struct zs_scc *scc = zport->scc;
521 unsigned long flags;
522
523 spin_lock_irqsave(&scc->zlock, flags);
524 if (break_state == -1)
525 zport->regs[5] |= SND_BRK;
526 else
527 zport->regs[5] &= ~SND_BRK;
528 write_zsreg(zport, R5, zport->regs[5]);
529 spin_unlock_irqrestore(&scc->zlock, flags);
530}
531
532
533/*
534 * Interrupt handling routines.
535 */
536#define Rx_BRK 0x0100 /* BREAK event software flag. */
537#define Rx_SYS 0x0200 /* SysRq event software flag. */
538
539static void zs_receive_chars(struct zs_port *zport)
540{
541 struct uart_port *uport = &zport->port;
542 struct zs_scc *scc = zport->scc;
543 struct uart_icount *icount;
544 unsigned int avail, status, ch, flag;
545 int count;
546
547 for (count = 16; count; count--) {
548 spin_lock(&scc->zlock);
549 avail = read_zsreg(zport, R0) & Rx_CH_AV;
550 spin_unlock(&scc->zlock);
551 if (!avail)
552 break;
553
554 spin_lock(&scc->zlock);
555 status = read_zsreg(zport, R1) & (Rx_OVR | FRM_ERR | PAR_ERR);
556 ch = read_zsdata(zport);
557 spin_unlock(&scc->zlock);
558
559 flag = TTY_NORMAL;
560
561 icount = &uport->icount;
562 icount->rx++;
563
564 /* Handle the null char got when BREAK is removed. */
565 if (!ch)
566 status |= zport->tty_break;
567 if (unlikely(status &
568 (Rx_OVR | FRM_ERR | PAR_ERR | Rx_SYS | Rx_BRK))) {
569 zport->tty_break = 0;
570
571 /* Reset the error indication. */
572 if (status & (Rx_OVR | FRM_ERR | PAR_ERR)) {
573 spin_lock(&scc->zlock);
574 write_zsreg(zport, R0, ERR_RES);
575 spin_unlock(&scc->zlock);
576 }
577
578 if (status & (Rx_SYS | Rx_BRK)) {
579 icount->brk++;
580 /* SysRq discards the null char. */
581 if (status & Rx_SYS)
582 continue;
583 } else if (status & FRM_ERR)
584 icount->frame++;
585 else if (status & PAR_ERR)
586 icount->parity++;
587 if (status & Rx_OVR)
588 icount->overrun++;
589
590 status &= uport->read_status_mask;
591 if (status & Rx_BRK)
592 flag = TTY_BREAK;
593 else if (status & FRM_ERR)
594 flag = TTY_FRAME;
595 else if (status & PAR_ERR)
596 flag = TTY_PARITY;
597 }
598
599 if (uart_handle_sysrq_char(uport, ch))
600 continue;
601
602 uart_insert_char(uport, status, Rx_OVR, ch, flag);
603 }
604
605 tty_flip_buffer_push(uport->state->port.tty);
606}
607
608static void zs_raw_transmit_chars(struct zs_port *zport)
609{
610 struct circ_buf *xmit = &zport->port.state->xmit;
611
612 /* XON/XOFF chars. */
613 if (zport->port.x_char) {
614 write_zsdata(zport, zport->port.x_char);
615 zport->port.icount.tx++;
616 zport->port.x_char = 0;
617 return;
618 }
619
620 /* If nothing to do or stopped or hardware stopped. */
621 if (uart_circ_empty(xmit) || uart_tx_stopped(&zport->port)) {
622 zs_raw_stop_tx(zport);
623 return;
624 }
625
626 /* Send char. */
627 write_zsdata(zport, xmit->buf[xmit->tail]);
628 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
629 zport->port.icount.tx++;
630
631 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
632 uart_write_wakeup(&zport->port);
633
634 /* Are we are done? */
635 if (uart_circ_empty(xmit))
636 zs_raw_stop_tx(zport);
637}
638
639static void zs_transmit_chars(struct zs_port *zport)
640{
641 struct zs_scc *scc = zport->scc;
642
643 spin_lock(&scc->zlock);
644 zs_raw_transmit_chars(zport);
645 spin_unlock(&scc->zlock);
646}
647
648static void zs_status_handle(struct zs_port *zport, struct zs_port *zport_a)
649{
650 struct uart_port *uport = &zport->port;
651 struct zs_scc *scc = zport->scc;
652 unsigned int delta;
653 u8 status, brk;
654
655 spin_lock(&scc->zlock);
656
657 /* Get status from Read Register 0. */
658 status = read_zsreg(zport, R0);
659
660 if (zport->regs[15] & BRKIE) {
661 brk = status & BRK_ABRT;
662 if (brk && !zport->brk) {
663 spin_unlock(&scc->zlock);
664 if (uart_handle_break(uport))
665 zport->tty_break = Rx_SYS;
666 else
667 zport->tty_break = Rx_BRK;
668 spin_lock(&scc->zlock);
669 }
670 zport->brk = brk;
671 }
672
673 if (zport != zport_a) {
674 delta = zs_raw_xor_mctrl(zport);
675 spin_unlock(&scc->zlock);
676
677 if (delta & TIOCM_CTS)
678 uart_handle_cts_change(uport,
679 zport->mctrl & TIOCM_CTS);
680 if (delta & TIOCM_CAR)
681 uart_handle_dcd_change(uport,
682 zport->mctrl & TIOCM_CAR);
683 if (delta & TIOCM_RNG)
684 uport->icount.dsr++;
685 if (delta & TIOCM_DSR)
686 uport->icount.rng++;
687
688 if (delta)
689 wake_up_interruptible(&uport->state->port.delta_msr_wait);
690
691 spin_lock(&scc->zlock);
692 }
693
694 /* Clear the status condition... */
695 write_zsreg(zport, R0, RES_EXT_INT);
696
697 spin_unlock(&scc->zlock);
698}
699
700/*
701 * This is the Z85C30 driver's generic interrupt routine.
702 */
703static irqreturn_t zs_interrupt(int irq, void *dev_id)
704{
705 struct zs_scc *scc = dev_id;
706 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
707 struct zs_port *zport_b = &scc->zport[ZS_CHAN_B];
708 irqreturn_t status = IRQ_NONE;
709 u8 zs_intreg;
710 int count;
711
712 /*
713 * NOTE: The read register 3, which holds the irq status,
714 * does so for both channels on each chip. Although
715 * the status value itself must be read from the A
716 * channel and is only valid when read from channel A.
717 * Yes... broken hardware...
718 */
719 for (count = 16; count; count--) {
720 spin_lock(&scc->zlock);
721 zs_intreg = read_zsreg(zport_a, R3);
722 spin_unlock(&scc->zlock);
723 if (!zs_intreg)
724 break;
725
726 /*
727 * We do not like losing characters, so we prioritise
728 * interrupt sources a little bit differently than
729 * the SCC would, was it allowed to.
730 */
731 if (zs_intreg & CHBRxIP)
732 zs_receive_chars(zport_b);
733 if (zs_intreg & CHARxIP)
734 zs_receive_chars(zport_a);
735 if (zs_intreg & CHBEXT)
736 zs_status_handle(zport_b, zport_a);
737 if (zs_intreg & CHAEXT)
738 zs_status_handle(zport_a, zport_a);
739 if (zs_intreg & CHBTxIP)
740 zs_transmit_chars(zport_b);
741 if (zs_intreg & CHATxIP)
742 zs_transmit_chars(zport_a);
743
744 status = IRQ_HANDLED;
745 }
746
747 return status;
748}
749
750
751/*
752 * Finally, routines used to initialize the serial port.
753 */
754static int zs_startup(struct uart_port *uport)
755{
756 struct zs_port *zport = to_zport(uport);
757 struct zs_scc *scc = zport->scc;
758 unsigned long flags;
759 int irq_guard;
760 int ret;
761
762 irq_guard = atomic_add_return(1, &scc->irq_guard);
763 if (irq_guard == 1) {
764 ret = request_irq(zport->port.irq, zs_interrupt,
765 IRQF_SHARED, "scc", scc);
766 if (ret) {
767 atomic_add(-1, &scc->irq_guard);
768 printk(KERN_ERR "zs: can't get irq %d\n",
769 zport->port.irq);
770 return ret;
771 }
772 }
773
774 spin_lock_irqsave(&scc->zlock, flags);
775
776 /* Clear the receive FIFO. */
777 zs_receive_drain(zport);
778
779 /* Clear the interrupt registers. */
780 write_zsreg(zport, R0, ERR_RES);
781 write_zsreg(zport, R0, RES_Tx_P);
782 /* But Ext only if not being handled already. */
783 if (!(zport->regs[1] & EXT_INT_ENAB))
784 write_zsreg(zport, R0, RES_EXT_INT);
785
786 /* Finally, enable sequencing and interrupts. */
787 zport->regs[1] &= ~RxINT_MASK;
788 zport->regs[1] |= RxINT_ALL | TxINT_ENAB | EXT_INT_ENAB;
789 zport->regs[3] |= RxENABLE;
790 zport->regs[15] |= BRKIE;
791 write_zsreg(zport, R1, zport->regs[1]);
792 write_zsreg(zport, R3, zport->regs[3]);
793 write_zsreg(zport, R5, zport->regs[5]);
794 write_zsreg(zport, R15, zport->regs[15]);
795
796 /* Record the current state of RR0. */
797 zport->mctrl = zs_raw_get_mctrl(zport);
798 zport->brk = read_zsreg(zport, R0) & BRK_ABRT;
799
800 zport->tx_stopped = 1;
801
802 spin_unlock_irqrestore(&scc->zlock, flags);
803
804 return 0;
805}
806
807static void zs_shutdown(struct uart_port *uport)
808{
809 struct zs_port *zport = to_zport(uport);
810 struct zs_scc *scc = zport->scc;
811 unsigned long flags;
812 int irq_guard;
813
814 spin_lock_irqsave(&scc->zlock, flags);
815
816 zport->regs[3] &= ~RxENABLE;
817 write_zsreg(zport, R5, zport->regs[5]);
818 write_zsreg(zport, R3, zport->regs[3]);
819
820 spin_unlock_irqrestore(&scc->zlock, flags);
821
822 irq_guard = atomic_add_return(-1, &scc->irq_guard);
823 if (!irq_guard)
824 free_irq(zport->port.irq, scc);
825}
826
827
828static void zs_reset(struct zs_port *zport)
829{
830 struct zs_scc *scc = zport->scc;
831 int irq;
832 unsigned long flags;
833
834 spin_lock_irqsave(&scc->zlock, flags);
835 irq = !irqs_disabled_flags(flags);
836 if (!scc->initialised) {
837 /* Reset the pointer first, just in case... */
838 read_zsreg(zport, R0);
839 /* And let the current transmission finish. */
840 zs_line_drain(zport, irq);
841 write_zsreg(zport, R9, FHWRES);
842 udelay(10);
843 write_zsreg(zport, R9, 0);
844 scc->initialised = 1;
845 }
846 load_zsregs(zport, zport->regs, irq);
847 spin_unlock_irqrestore(&scc->zlock, flags);
848}
849
850static void zs_set_termios(struct uart_port *uport, struct ktermios *termios,
851 struct ktermios *old_termios)
852{
853 struct zs_port *zport = to_zport(uport);
854 struct zs_scc *scc = zport->scc;
855 struct zs_port *zport_a = &scc->zport[ZS_CHAN_A];
856 int irq;
857 unsigned int baud, brg;
858 unsigned long flags;
859
860 spin_lock_irqsave(&scc->zlock, flags);
861 irq = !irqs_disabled_flags(flags);
862
863 /* Byte size. */
864 zport->regs[3] &= ~RxNBITS_MASK;
865 zport->regs[5] &= ~TxNBITS_MASK;
866 switch (termios->c_cflag & CSIZE) {
867 case CS5:
868 zport->regs[3] |= Rx5;
869 zport->regs[5] |= Tx5;
870 break;
871 case CS6:
872 zport->regs[3] |= Rx6;
873 zport->regs[5] |= Tx6;
874 break;
875 case CS7:
876 zport->regs[3] |= Rx7;
877 zport->regs[5] |= Tx7;
878 break;
879 case CS8:
880 default:
881 zport->regs[3] |= Rx8;
882 zport->regs[5] |= Tx8;
883 break;
884 }
885
886 /* Parity and stop bits. */
887 zport->regs[4] &= ~(XCLK_MASK | SB_MASK | PAR_ENA | PAR_EVEN);
888 if (termios->c_cflag & CSTOPB)
889 zport->regs[4] |= SB2;
890 else
891 zport->regs[4] |= SB1;
892 if (termios->c_cflag & PARENB)
893 zport->regs[4] |= PAR_ENA;
894 if (!(termios->c_cflag & PARODD))
895 zport->regs[4] |= PAR_EVEN;
896 switch (zport->clk_mode) {
897 case 64:
898 zport->regs[4] |= X64CLK;
899 break;
900 case 32:
901 zport->regs[4] |= X32CLK;
902 break;
903 case 16:
904 zport->regs[4] |= X16CLK;
905 break;
906 case 1:
907 zport->regs[4] |= X1CLK;
908 break;
909 default:
910 BUG();
911 }
912
913 baud = uart_get_baud_rate(uport, termios, old_termios, 0,
914 uport->uartclk / zport->clk_mode / 4);
915
916 brg = ZS_BPS_TO_BRG(baud, uport->uartclk / zport->clk_mode);
917 zport->regs[12] = brg & 0xff;
918 zport->regs[13] = (brg >> 8) & 0xff;
919
920 uart_update_timeout(uport, termios->c_cflag, baud);
921
922 uport->read_status_mask = Rx_OVR;
923 if (termios->c_iflag & INPCK)
924 uport->read_status_mask |= FRM_ERR | PAR_ERR;
925 if (termios->c_iflag & (BRKINT | PARMRK))
926 uport->read_status_mask |= Rx_BRK;
927
928 uport->ignore_status_mask = 0;
929 if (termios->c_iflag & IGNPAR)
930 uport->ignore_status_mask |= FRM_ERR | PAR_ERR;
931 if (termios->c_iflag & IGNBRK) {
932 uport->ignore_status_mask |= Rx_BRK;
933 if (termios->c_iflag & IGNPAR)
934 uport->ignore_status_mask |= Rx_OVR;
935 }
936
937 if (termios->c_cflag & CREAD)
938 zport->regs[3] |= RxENABLE;
939 else
940 zport->regs[3] &= ~RxENABLE;
941
942 if (zport != zport_a) {
943 if (!(termios->c_cflag & CLOCAL)) {
944 zport->regs[15] |= DCDIE;
945 } else
946 zport->regs[15] &= ~DCDIE;
947 if (termios->c_cflag & CRTSCTS) {
948 zport->regs[15] |= CTSIE;
949 } else
950 zport->regs[15] &= ~CTSIE;
951 zs_raw_xor_mctrl(zport);
952 }
953
954 /* Load up the new values. */
955 load_zsregs(zport, zport->regs, irq);
956
957 spin_unlock_irqrestore(&scc->zlock, flags);
958}
959
960/*
961 * Hack alert!
962 * Required solely so that the initial PROM-based console
963 * works undisturbed in parallel with this one.
964 */
965static void zs_pm(struct uart_port *uport, unsigned int state,
966 unsigned int oldstate)
967{
968 struct zs_port *zport = to_zport(uport);
969
970 if (state < 3)
971 zport->regs[5] |= TxENAB;
972 else
973 zport->regs[5] &= ~TxENAB;
974 write_zsreg(zport, R5, zport->regs[5]);
975}
976
977
978static const char *zs_type(struct uart_port *uport)
979{
980 return "Z85C30 SCC";
981}
982
983static void zs_release_port(struct uart_port *uport)
984{
985 iounmap(uport->membase);
986 uport->membase = 0;
987 release_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE);
988}
989
990static int zs_map_port(struct uart_port *uport)
991{
992 if (!uport->membase)
993 uport->membase = ioremap_nocache(uport->mapbase,
994 ZS_CHAN_IO_SIZE);
995 if (!uport->membase) {
996 printk(KERN_ERR "zs: Cannot map MMIO\n");
997 return -ENOMEM;
998 }
999 return 0;
1000}
1001
1002static int zs_request_port(struct uart_port *uport)
1003{
1004 int ret;
1005
1006 if (!request_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE, "scc")) {
1007 printk(KERN_ERR "zs: Unable to reserve MMIO resource\n");
1008 return -EBUSY;
1009 }
1010 ret = zs_map_port(uport);
1011 if (ret) {
1012 release_mem_region(uport->mapbase, ZS_CHAN_IO_SIZE);
1013 return ret;
1014 }
1015 return 0;
1016}
1017
1018static void zs_config_port(struct uart_port *uport, int flags)
1019{
1020 struct zs_port *zport = to_zport(uport);
1021
1022 if (flags & UART_CONFIG_TYPE) {
1023 if (zs_request_port(uport))
1024 return;
1025
1026 uport->type = PORT_ZS;
1027
1028 zs_reset(zport);
1029 }
1030}
1031
1032static int zs_verify_port(struct uart_port *uport, struct serial_struct *ser)
1033{
1034 struct zs_port *zport = to_zport(uport);
1035 int ret = 0;
1036
1037 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ZS)
1038 ret = -EINVAL;
1039 if (ser->irq != uport->irq)
1040 ret = -EINVAL;
1041 if (ser->baud_base != uport->uartclk / zport->clk_mode / 4)
1042 ret = -EINVAL;
1043 return ret;
1044}
1045
1046
1047static struct uart_ops zs_ops = {
1048 .tx_empty = zs_tx_empty,
1049 .set_mctrl = zs_set_mctrl,
1050 .get_mctrl = zs_get_mctrl,
1051 .stop_tx = zs_stop_tx,
1052 .start_tx = zs_start_tx,
1053 .stop_rx = zs_stop_rx,
1054 .enable_ms = zs_enable_ms,
1055 .break_ctl = zs_break_ctl,
1056 .startup = zs_startup,
1057 .shutdown = zs_shutdown,
1058 .set_termios = zs_set_termios,
1059 .pm = zs_pm,
1060 .type = zs_type,
1061 .release_port = zs_release_port,
1062 .request_port = zs_request_port,
1063 .config_port = zs_config_port,
1064 .verify_port = zs_verify_port,
1065};
1066
1067/*
1068 * Initialize Z85C30 port structures.
1069 */
1070static int __init zs_probe_sccs(void)
1071{
1072 static int probed;
1073 struct zs_parms zs_parms;
1074 int chip, side, irq;
1075 int n_chips = 0;
1076 int i;
1077
1078 if (probed)
1079 return 0;
1080
1081 irq = dec_interrupt[DEC_IRQ_SCC0];
1082 if (irq >= 0) {
1083 zs_parms.scc[n_chips] = IOASIC_SCC0;
1084 zs_parms.irq[n_chips] = dec_interrupt[DEC_IRQ_SCC0];
1085 n_chips++;
1086 }
1087 irq = dec_interrupt[DEC_IRQ_SCC1];
1088 if (irq >= 0) {
1089 zs_parms.scc[n_chips] = IOASIC_SCC1;
1090 zs_parms.irq[n_chips] = dec_interrupt[DEC_IRQ_SCC1];
1091 n_chips++;
1092 }
1093 if (!n_chips)
1094 return -ENXIO;
1095
1096 probed = 1;
1097
1098 for (chip = 0; chip < n_chips; chip++) {
1099 spin_lock_init(&zs_sccs[chip].zlock);
1100 for (side = 0; side < ZS_NUM_CHAN; side++) {
1101 struct zs_port *zport = &zs_sccs[chip].zport[side];
1102 struct uart_port *uport = &zport->port;
1103
1104 zport->scc = &zs_sccs[chip];
1105 zport->clk_mode = 16;
1106
1107 uport->irq = zs_parms.irq[chip];
1108 uport->uartclk = ZS_CLOCK;
1109 uport->fifosize = 1;
1110 uport->iotype = UPIO_MEM;
1111 uport->flags = UPF_BOOT_AUTOCONF;
1112 uport->ops = &zs_ops;
1113 uport->line = chip * ZS_NUM_CHAN + side;
1114 uport->mapbase = dec_kn_slot_base +
1115 zs_parms.scc[chip] +
1116 (side ^ ZS_CHAN_B) * ZS_CHAN_IO_SIZE;
1117
1118 for (i = 0; i < ZS_NUM_REGS; i++)
1119 zport->regs[i] = zs_init_regs[i];
1120 }
1121 }
1122
1123 return 0;
1124}
1125
1126
1127#ifdef CONFIG_SERIAL_ZS_CONSOLE
1128static void zs_console_putchar(struct uart_port *uport, int ch)
1129{
1130 struct zs_port *zport = to_zport(uport);
1131 struct zs_scc *scc = zport->scc;
1132 int irq;
1133 unsigned long flags;
1134
1135 spin_lock_irqsave(&scc->zlock, flags);
1136 irq = !irqs_disabled_flags(flags);
1137 if (zs_transmit_drain(zport, irq))
1138 write_zsdata(zport, ch);
1139 spin_unlock_irqrestore(&scc->zlock, flags);
1140}
1141
1142/*
1143 * Print a string to the serial port trying not to disturb
1144 * any possible real use of the port...
1145 */
1146static void zs_console_write(struct console *co, const char *s,
1147 unsigned int count)
1148{
1149 int chip = co->index / ZS_NUM_CHAN, side = co->index % ZS_NUM_CHAN;
1150 struct zs_port *zport = &zs_sccs[chip].zport[side];
1151 struct zs_scc *scc = zport->scc;
1152 unsigned long flags;
1153 u8 txint, txenb;
1154 int irq;
1155
1156 /* Disable transmit interrupts and enable the transmitter. */
1157 spin_lock_irqsave(&scc->zlock, flags);
1158 txint = zport->regs[1];
1159 txenb = zport->regs[5];
1160 if (txint & TxINT_ENAB) {
1161 zport->regs[1] = txint & ~TxINT_ENAB;
1162 write_zsreg(zport, R1, zport->regs[1]);
1163 }
1164 if (!(txenb & TxENAB)) {
1165 zport->regs[5] = txenb | TxENAB;
1166 write_zsreg(zport, R5, zport->regs[5]);
1167 }
1168 spin_unlock_irqrestore(&scc->zlock, flags);
1169
1170 uart_console_write(&zport->port, s, count, zs_console_putchar);
1171
1172 /* Restore transmit interrupts and the transmitter enable. */
1173 spin_lock_irqsave(&scc->zlock, flags);
1174 irq = !irqs_disabled_flags(flags);
1175 zs_line_drain(zport, irq);
1176 if (!(txenb & TxENAB)) {
1177 zport->regs[5] &= ~TxENAB;
1178 write_zsreg(zport, R5, zport->regs[5]);
1179 }
1180 if (txint & TxINT_ENAB) {
1181 zport->regs[1] |= TxINT_ENAB;
1182 write_zsreg(zport, R1, zport->regs[1]);
1183 }
1184 spin_unlock_irqrestore(&scc->zlock, flags);
1185}
1186
1187/*
1188 * Setup serial console baud/bits/parity. We do two things here:
1189 * - construct a cflag setting for the first uart_open()
1190 * - initialise the serial port
1191 * Return non-zero if we didn't find a serial port.
1192 */
1193static int __init zs_console_setup(struct console *co, char *options)
1194{
1195 int chip = co->index / ZS_NUM_CHAN, side = co->index % ZS_NUM_CHAN;
1196 struct zs_port *zport = &zs_sccs[chip].zport[side];
1197 struct uart_port *uport = &zport->port;
1198 int baud = 9600;
1199 int bits = 8;
1200 int parity = 'n';
1201 int flow = 'n';
1202 int ret;
1203
1204 ret = zs_map_port(uport);
1205 if (ret)
1206 return ret;
1207
1208 zs_reset(zport);
1209 zs_pm(uport, 0, -1);
1210
1211 if (options)
1212 uart_parse_options(options, &baud, &parity, &bits, &flow);
1213 return uart_set_options(uport, co, baud, parity, bits, flow);
1214}
1215
1216static struct uart_driver zs_reg;
1217static struct console zs_console = {
1218 .name = "ttyS",
1219 .write = zs_console_write,
1220 .device = uart_console_device,
1221 .setup = zs_console_setup,
1222 .flags = CON_PRINTBUFFER,
1223 .index = -1,
1224 .data = &zs_reg,
1225};
1226
1227/*
1228 * Register console.
1229 */
1230static int __init zs_serial_console_init(void)
1231{
1232 int ret;
1233
1234 ret = zs_probe_sccs();
1235 if (ret)
1236 return ret;
1237 register_console(&zs_console);
1238
1239 return 0;
1240}
1241
1242console_initcall(zs_serial_console_init);
1243
1244#define SERIAL_ZS_CONSOLE &zs_console
1245#else
1246#define SERIAL_ZS_CONSOLE NULL
1247#endif /* CONFIG_SERIAL_ZS_CONSOLE */
1248
1249static struct uart_driver zs_reg = {
1250 .owner = THIS_MODULE,
1251 .driver_name = "serial",
1252 .dev_name = "ttyS",
1253 .major = TTY_MAJOR,
1254 .minor = 64,
1255 .nr = ZS_NUM_SCCS * ZS_NUM_CHAN,
1256 .cons = SERIAL_ZS_CONSOLE,
1257};
1258
1259/* zs_init inits the driver. */
1260static int __init zs_init(void)
1261{
1262 int i, ret;
1263
1264 pr_info("%s%s\n", zs_name, zs_version);
1265
1266 /* Find out how many Z85C30 SCCs we have. */
1267 ret = zs_probe_sccs();
1268 if (ret)
1269 return ret;
1270
1271 ret = uart_register_driver(&zs_reg);
1272 if (ret)
1273 return ret;
1274
1275 for (i = 0; i < ZS_NUM_SCCS * ZS_NUM_CHAN; i++) {
1276 struct zs_scc *scc = &zs_sccs[i / ZS_NUM_CHAN];
1277 struct zs_port *zport = &scc->zport[i % ZS_NUM_CHAN];
1278 struct uart_port *uport = &zport->port;
1279
1280 if (zport->scc)
1281 uart_add_one_port(&zs_reg, uport);
1282 }
1283
1284 return 0;
1285}
1286
1287static void __exit zs_exit(void)
1288{
1289 int i;
1290
1291 for (i = ZS_NUM_SCCS * ZS_NUM_CHAN - 1; i >= 0; i--) {
1292 struct zs_scc *scc = &zs_sccs[i / ZS_NUM_CHAN];
1293 struct zs_port *zport = &scc->zport[i % ZS_NUM_CHAN];
1294 struct uart_port *uport = &zport->port;
1295
1296 if (zport->scc)
1297 uart_remove_one_port(&zs_reg, uport);
1298 }
1299
1300 uart_unregister_driver(&zs_reg);
1301}
1302
1303module_init(zs_init);
1304module_exit(zs_exit);
diff --git a/drivers/tty/serial/zs.h b/drivers/tty/serial/zs.h
new file mode 100644
index 000000000000..aa921b57d827
--- /dev/null
+++ b/drivers/tty/serial/zs.h
@@ -0,0 +1,284 @@
1/*
2 * zs.h: Definitions for the DECstation Z85C30 serial driver.
3 *
4 * Adapted from drivers/sbus/char/sunserial.h by Paul Mackerras.
5 * Adapted from drivers/macintosh/macserial.h by Harald Koerfgen.
6 *
7 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
8 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
9 * Copyright (C) 2004, 2005, 2007 Maciej W. Rozycki
10 */
11#ifndef _SERIAL_ZS_H
12#define _SERIAL_ZS_H
13
14#ifdef __KERNEL__
15
16#define ZS_NUM_REGS 16
17
18/*
19 * This is our internal structure for each serial port's state.
20 */
21struct zs_port {
22 struct zs_scc *scc; /* Containing SCC. */
23 struct uart_port port; /* Underlying UART. */
24
25 int clk_mode; /* May be 1, 16, 32, or 64. */
26
27 unsigned int tty_break; /* Set on BREAK condition. */
28 int tx_stopped; /* Output is suspended. */
29
30 unsigned int mctrl; /* State of modem lines. */
31 u8 brk; /* BREAK state from RR0. */
32
33 u8 regs[ZS_NUM_REGS]; /* Channel write registers. */
34};
35
36/*
37 * Per-SCC state for locking and the interrupt handler.
38 */
39struct zs_scc {
40 struct zs_port zport[2];
41 spinlock_t zlock;
42 atomic_t irq_guard;
43 int initialised;
44};
45
46#endif /* __KERNEL__ */
47
48/*
49 * Conversion routines to/from brg time constants from/to bits per second.
50 */
51#define ZS_BRG_TO_BPS(brg, freq) ((freq) / 2 / ((brg) + 2))
52#define ZS_BPS_TO_BRG(bps, freq) ((((freq) + (bps)) / (2 * (bps))) - 2)
53
54/*
55 * The Zilog register set.
56 */
57
58/* Write Register 0 (Command) */
59#define R0 0 /* Register selects */
60#define R1 1
61#define R2 2
62#define R3 3
63#define R4 4
64#define R5 5
65#define R6 6
66#define R7 7
67#define R8 8
68#define R9 9
69#define R10 10
70#define R11 11
71#define R12 12
72#define R13 13
73#define R14 14
74#define R15 15
75
76#define NULLCODE 0 /* Null Code */
77#define POINT_HIGH 0x8 /* Select upper half of registers */
78#define RES_EXT_INT 0x10 /* Reset Ext. Status Interrupts */
79#define SEND_ABORT 0x18 /* HDLC Abort */
80#define RES_RxINT_FC 0x20 /* Reset RxINT on First Character */
81#define RES_Tx_P 0x28 /* Reset TxINT Pending */
82#define ERR_RES 0x30 /* Error Reset */
83#define RES_H_IUS 0x38 /* Reset highest IUS */
84
85#define RES_Rx_CRC 0x40 /* Reset Rx CRC Checker */
86#define RES_Tx_CRC 0x80 /* Reset Tx CRC Checker */
87#define RES_EOM_L 0xC0 /* Reset EOM latch */
88
89/* Write Register 1 (Tx/Rx/Ext Int Enable and WAIT/DMA Commands) */
90#define EXT_INT_ENAB 0x1 /* Ext Int Enable */
91#define TxINT_ENAB 0x2 /* Tx Int Enable */
92#define PAR_SPEC 0x4 /* Parity is special condition */
93
94#define RxINT_DISAB 0 /* Rx Int Disable */
95#define RxINT_FCERR 0x8 /* Rx Int on First Character Only or Error */
96#define RxINT_ALL 0x10 /* Int on all Rx Characters or error */
97#define RxINT_ERR 0x18 /* Int on error only */
98#define RxINT_MASK 0x18
99
100#define WT_RDY_RT 0x20 /* Wait/Ready on R/T */
101#define WT_FN_RDYFN 0x40 /* Wait/FN/Ready FN */
102#define WT_RDY_ENAB 0x80 /* Wait/Ready Enable */
103
104/* Write Register 2 (Interrupt Vector) */
105
106/* Write Register 3 (Receive Parameters and Control) */
107#define RxENABLE 0x1 /* Rx Enable */
108#define SYNC_L_INH 0x2 /* Sync Character Load Inhibit */
109#define ADD_SM 0x4 /* Address Search Mode (SDLC) */
110#define RxCRC_ENAB 0x8 /* Rx CRC Enable */
111#define ENT_HM 0x10 /* Enter Hunt Mode */
112#define AUTO_ENAB 0x20 /* Auto Enables */
113#define Rx5 0x0 /* Rx 5 Bits/Character */
114#define Rx7 0x40 /* Rx 7 Bits/Character */
115#define Rx6 0x80 /* Rx 6 Bits/Character */
116#define Rx8 0xc0 /* Rx 8 Bits/Character */
117#define RxNBITS_MASK 0xc0
118
119/* Write Register 4 (Transmit/Receive Miscellaneous Parameters and Modes) */
120#define PAR_ENA 0x1 /* Parity Enable */
121#define PAR_EVEN 0x2 /* Parity Even/Odd* */
122
123#define SYNC_ENAB 0 /* Sync Modes Enable */
124#define SB1 0x4 /* 1 stop bit/char */
125#define SB15 0x8 /* 1.5 stop bits/char */
126#define SB2 0xc /* 2 stop bits/char */
127#define SB_MASK 0xc
128
129#define MONSYNC 0 /* 8 Bit Sync character */
130#define BISYNC 0x10 /* 16 bit sync character */
131#define SDLC 0x20 /* SDLC Mode (01111110 Sync Flag) */
132#define EXTSYNC 0x30 /* External Sync Mode */
133
134#define X1CLK 0x0 /* x1 clock mode */
135#define X16CLK 0x40 /* x16 clock mode */
136#define X32CLK 0x80 /* x32 clock mode */
137#define X64CLK 0xc0 /* x64 clock mode */
138#define XCLK_MASK 0xc0
139
140/* Write Register 5 (Transmit Parameters and Controls) */
141#define TxCRC_ENAB 0x1 /* Tx CRC Enable */
142#define RTS 0x2 /* RTS */
143#define SDLC_CRC 0x4 /* SDLC/CRC-16 */
144#define TxENAB 0x8 /* Tx Enable */
145#define SND_BRK 0x10 /* Send Break */
146#define Tx5 0x0 /* Tx 5 bits (or less)/character */
147#define Tx7 0x20 /* Tx 7 bits/character */
148#define Tx6 0x40 /* Tx 6 bits/character */
149#define Tx8 0x60 /* Tx 8 bits/character */
150#define TxNBITS_MASK 0x60
151#define DTR 0x80 /* DTR */
152
153/* Write Register 6 (Sync bits 0-7/SDLC Address Field) */
154
155/* Write Register 7 (Sync bits 8-15/SDLC 01111110) */
156
157/* Write Register 8 (Transmit Buffer) */
158
159/* Write Register 9 (Master Interrupt Control) */
160#define VIS 1 /* Vector Includes Status */
161#define NV 2 /* No Vector */
162#define DLC 4 /* Disable Lower Chain */
163#define MIE 8 /* Master Interrupt Enable */
164#define STATHI 0x10 /* Status high */
165#define SOFTACK 0x20 /* Software Interrupt Acknowledge */
166#define NORESET 0 /* No reset on write to R9 */
167#define CHRB 0x40 /* Reset channel B */
168#define CHRA 0x80 /* Reset channel A */
169#define FHWRES 0xc0 /* Force hardware reset */
170
171/* Write Register 10 (Miscellaneous Transmitter/Receiver Control Bits) */
172#define BIT6 1 /* 6 bit/8bit sync */
173#define LOOPMODE 2 /* SDLC Loop mode */
174#define ABUNDER 4 /* Abort/flag on SDLC xmit underrun */
175#define MARKIDLE 8 /* Mark/flag on idle */
176#define GAOP 0x10 /* Go active on poll */
177#define NRZ 0 /* NRZ mode */
178#define NRZI 0x20 /* NRZI mode */
179#define FM1 0x40 /* FM1 (transition = 1) */
180#define FM0 0x60 /* FM0 (transition = 0) */
181#define CRCPS 0x80 /* CRC Preset I/O */
182
183/* Write Register 11 (Clock Mode Control) */
184#define TRxCXT 0 /* TRxC = Xtal output */
185#define TRxCTC 1 /* TRxC = Transmit clock */
186#define TRxCBR 2 /* TRxC = BR Generator Output */
187#define TRxCDP 3 /* TRxC = DPLL output */
188#define TRxCOI 4 /* TRxC O/I */
189#define TCRTxCP 0 /* Transmit clock = RTxC pin */
190#define TCTRxCP 8 /* Transmit clock = TRxC pin */
191#define TCBR 0x10 /* Transmit clock = BR Generator output */
192#define TCDPLL 0x18 /* Transmit clock = DPLL output */
193#define RCRTxCP 0 /* Receive clock = RTxC pin */
194#define RCTRxCP 0x20 /* Receive clock = TRxC pin */
195#define RCBR 0x40 /* Receive clock = BR Generator output */
196#define RCDPLL 0x60 /* Receive clock = DPLL output */
197#define RTxCX 0x80 /* RTxC Xtal/No Xtal */
198
199/* Write Register 12 (Lower Byte of Baud Rate Generator Time Constant) */
200
201/* Write Register 13 (Upper Byte of Baud Rate Generator Time Constant) */
202
203/* Write Register 14 (Miscellaneous Control Bits) */
204#define BRENABL 1 /* Baud rate generator enable */
205#define BRSRC 2 /* Baud rate generator source */
206#define DTRREQ 4 /* DTR/Request function */
207#define AUTOECHO 8 /* Auto Echo */
208#define LOOPBAK 0x10 /* Local loopback */
209#define SEARCH 0x20 /* Enter search mode */
210#define RMC 0x40 /* Reset missing clock */
211#define DISDPLL 0x60 /* Disable DPLL */
212#define SSBR 0x80 /* Set DPLL source = BR generator */
213#define SSRTxC 0xa0 /* Set DPLL source = RTxC */
214#define SFMM 0xc0 /* Set FM mode */
215#define SNRZI 0xe0 /* Set NRZI mode */
216
217/* Write Register 15 (External/Status Interrupt Control) */
218#define WR7P_EN 1 /* WR7 Prime SDLC Feature Enable */
219#define ZCIE 2 /* Zero count IE */
220#define DCDIE 8 /* DCD IE */
221#define SYNCIE 0x10 /* Sync/hunt IE */
222#define CTSIE 0x20 /* CTS IE */
223#define TxUIE 0x40 /* Tx Underrun/EOM IE */
224#define BRKIE 0x80 /* Break/Abort IE */
225
226
227/* Read Register 0 (Transmit/Receive Buffer Status and External Status) */
228#define Rx_CH_AV 0x1 /* Rx Character Available */
229#define ZCOUNT 0x2 /* Zero count */
230#define Tx_BUF_EMP 0x4 /* Tx Buffer empty */
231#define DCD 0x8 /* DCD */
232#define SYNC_HUNT 0x10 /* Sync/hunt */
233#define CTS 0x20 /* CTS */
234#define TxEOM 0x40 /* Tx underrun */
235#define BRK_ABRT 0x80 /* Break/Abort */
236
237/* Read Register 1 (Special Receive Condition Status) */
238#define ALL_SNT 0x1 /* All sent */
239/* Residue Data for 8 Rx bits/char programmed */
240#define RES3 0x8 /* 0/3 */
241#define RES4 0x4 /* 0/4 */
242#define RES5 0xc /* 0/5 */
243#define RES6 0x2 /* 0/6 */
244#define RES7 0xa /* 0/7 */
245#define RES8 0x6 /* 0/8 */
246#define RES18 0xe /* 1/8 */
247#define RES28 0x0 /* 2/8 */
248/* Special Rx Condition Interrupts */
249#define PAR_ERR 0x10 /* Parity Error */
250#define Rx_OVR 0x20 /* Rx Overrun Error */
251#define FRM_ERR 0x40 /* CRC/Framing Error */
252#define END_FR 0x80 /* End of Frame (SDLC) */
253
254/* Read Register 2 (Interrupt Vector (WR2) -- channel A). */
255
256/* Read Register 2 (Modified Interrupt Vector -- channel B). */
257
258/* Read Register 3 (Interrupt Pending Bits -- channel A only). */
259#define CHBEXT 0x1 /* Channel B Ext/Stat IP */
260#define CHBTxIP 0x2 /* Channel B Tx IP */
261#define CHBRxIP 0x4 /* Channel B Rx IP */
262#define CHAEXT 0x8 /* Channel A Ext/Stat IP */
263#define CHATxIP 0x10 /* Channel A Tx IP */
264#define CHARxIP 0x20 /* Channel A Rx IP */
265
266/* Read Register 6 (SDLC FIFO Status and Byte Count LSB) */
267
268/* Read Register 7 (SDLC FIFO Status and Byte Count MSB) */
269
270/* Read Register 8 (Receive Data) */
271
272/* Read Register 10 (Miscellaneous Status Bits) */
273#define ONLOOP 2 /* On loop */
274#define LOOPSEND 0x10 /* Loop sending */
275#define CLK2MIS 0x40 /* Two clocks missing */
276#define CLK1MIS 0x80 /* One clock missing */
277
278/* Read Register 12 (Lower Byte of Baud Rate Generator Constant (WR12)) */
279
280/* Read Register 13 (Upper Byte of Baud Rate Generator Constant (WR13) */
281
282/* Read Register 15 (External/Status Interrupt Control (WR15)) */
283
284#endif /* _SERIAL_ZS_H */
diff --git a/drivers/tty/synclink.c b/drivers/tty/synclink.c
new file mode 100644
index 000000000000..272e417a9b0d
--- /dev/null
+++ b/drivers/tty/synclink.c
@@ -0,0 +1,8117 @@
1/*
2 * $Id: synclink.c,v 4.38 2005/11/07 16:30:34 paulkf Exp $
3 *
4 * Device driver for Microgate SyncLink ISA and PCI
5 * high speed multiprotocol serial adapters.
6 *
7 * written by Paul Fulghum for Microgate Corporation
8 * paulkf@microgate.com
9 *
10 * Microgate and SyncLink are trademarks of Microgate Corporation
11 *
12 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
13 *
14 * Original release 01/11/99
15 *
16 * This code is released under the GNU General Public License (GPL)
17 *
18 * This driver is primarily intended for use in synchronous
19 * HDLC mode. Asynchronous mode is also provided.
20 *
21 * When operating in synchronous mode, each call to mgsl_write()
22 * contains exactly one complete HDLC frame. Calling mgsl_put_char
23 * will start assembling an HDLC frame that will not be sent until
24 * mgsl_flush_chars or mgsl_write is called.
25 *
26 * Synchronous receive data is reported as complete frames. To accomplish
27 * this, the TTY flip buffer is bypassed (too small to hold largest
28 * frame and may fragment frames) and the line discipline
29 * receive entry point is called directly.
30 *
31 * This driver has been tested with a slightly modified ppp.c driver
32 * for synchronous PPP.
33 *
34 * 2000/02/16
35 * Added interface for syncppp.c driver (an alternate synchronous PPP
36 * implementation that also supports Cisco HDLC). Each device instance
37 * registers as a tty device AND a network device (if dosyncppp option
38 * is set for the device). The functionality is determined by which
39 * device interface is opened.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
45 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54#if defined(__i386__)
55# define BREAKPOINT() asm(" int $3");
56#else
57# define BREAKPOINT() { }
58#endif
59
60#define MAX_ISA_DEVICES 10
61#define MAX_PCI_DEVICES 10
62#define MAX_TOTAL_DEVICES 20
63
64#include <linux/module.h>
65#include <linux/errno.h>
66#include <linux/signal.h>
67#include <linux/sched.h>
68#include <linux/timer.h>
69#include <linux/interrupt.h>
70#include <linux/pci.h>
71#include <linux/tty.h>
72#include <linux/tty_flip.h>
73#include <linux/serial.h>
74#include <linux/major.h>
75#include <linux/string.h>
76#include <linux/fcntl.h>
77#include <linux/ptrace.h>
78#include <linux/ioport.h>
79#include <linux/mm.h>
80#include <linux/seq_file.h>
81#include <linux/slab.h>
82#include <linux/delay.h>
83#include <linux/netdevice.h>
84#include <linux/vmalloc.h>
85#include <linux/init.h>
86#include <linux/ioctl.h>
87#include <linux/synclink.h>
88
89#include <asm/system.h>
90#include <asm/io.h>
91#include <asm/irq.h>
92#include <asm/dma.h>
93#include <linux/bitops.h>
94#include <asm/types.h>
95#include <linux/termios.h>
96#include <linux/workqueue.h>
97#include <linux/hdlc.h>
98#include <linux/dma-mapping.h>
99
100#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_MODULE))
101#define SYNCLINK_GENERIC_HDLC 1
102#else
103#define SYNCLINK_GENERIC_HDLC 0
104#endif
105
106#define GET_USER(error,value,addr) error = get_user(value,addr)
107#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
108#define PUT_USER(error,value,addr) error = put_user(value,addr)
109#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
110
111#include <asm/uaccess.h>
112
113#define RCLRVALUE 0xffff
114
115static MGSL_PARAMS default_params = {
116 MGSL_MODE_HDLC, /* unsigned long mode */
117 0, /* unsigned char loopback; */
118 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
119 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
120 0, /* unsigned long clock_speed; */
121 0xff, /* unsigned char addr_filter; */
122 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
123 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
124 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
125 9600, /* unsigned long data_rate; */
126 8, /* unsigned char data_bits; */
127 1, /* unsigned char stop_bits; */
128 ASYNC_PARITY_NONE /* unsigned char parity; */
129};
130
131#define SHARED_MEM_ADDRESS_SIZE 0x40000
132#define BUFFERLISTSIZE 4096
133#define DMABUFFERSIZE 4096
134#define MAXRXFRAMES 7
135
136typedef struct _DMABUFFERENTRY
137{
138 u32 phys_addr; /* 32-bit flat physical address of data buffer */
139 volatile u16 count; /* buffer size/data count */
140 volatile u16 status; /* Control/status field */
141 volatile u16 rcc; /* character count field */
142 u16 reserved; /* padding required by 16C32 */
143 u32 link; /* 32-bit flat link to next buffer entry */
144 char *virt_addr; /* virtual address of data buffer */
145 u32 phys_entry; /* physical address of this buffer entry */
146 dma_addr_t dma_addr;
147} DMABUFFERENTRY, *DMAPBUFFERENTRY;
148
149/* The queue of BH actions to be performed */
150
151#define BH_RECEIVE 1
152#define BH_TRANSMIT 2
153#define BH_STATUS 4
154
155#define IO_PIN_SHUTDOWN_LIMIT 100
156
157struct _input_signal_events {
158 int ri_up;
159 int ri_down;
160 int dsr_up;
161 int dsr_down;
162 int dcd_up;
163 int dcd_down;
164 int cts_up;
165 int cts_down;
166};
167
168/* transmit holding buffer definitions*/
169#define MAX_TX_HOLDING_BUFFERS 5
170struct tx_holding_buffer {
171 int buffer_size;
172 unsigned char * buffer;
173};
174
175
176/*
177 * Device instance data structure
178 */
179
180struct mgsl_struct {
181 int magic;
182 struct tty_port port;
183 int line;
184 int hw_version;
185
186 struct mgsl_icount icount;
187
188 int timeout;
189 int x_char; /* xon/xoff character */
190 u16 read_status_mask;
191 u16 ignore_status_mask;
192 unsigned char *xmit_buf;
193 int xmit_head;
194 int xmit_tail;
195 int xmit_cnt;
196
197 wait_queue_head_t status_event_wait_q;
198 wait_queue_head_t event_wait_q;
199 struct timer_list tx_timer; /* HDLC transmit timeout timer */
200 struct mgsl_struct *next_device; /* device list link */
201
202 spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
203 struct work_struct task; /* task structure for scheduling bh */
204
205 u32 EventMask; /* event trigger mask */
206 u32 RecordedEvents; /* pending events */
207
208 u32 max_frame_size; /* as set by device config */
209
210 u32 pending_bh;
211
212 bool bh_running; /* Protection from multiple */
213 int isr_overflow;
214 bool bh_requested;
215
216 int dcd_chkcount; /* check counts to prevent */
217 int cts_chkcount; /* too many IRQs if a signal */
218 int dsr_chkcount; /* is floating */
219 int ri_chkcount;
220
221 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
222 u32 buffer_list_phys;
223 dma_addr_t buffer_list_dma_addr;
224
225 unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
226 DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
227 unsigned int current_rx_buffer;
228
229 int num_tx_dma_buffers; /* number of tx dma frames required */
230 int tx_dma_buffers_used;
231 unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
232 DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
233 int start_tx_dma_buffer; /* tx dma buffer to start tx dma operation */
234 int current_tx_buffer; /* next tx dma buffer to be loaded */
235
236 unsigned char *intermediate_rxbuffer;
237
238 int num_tx_holding_buffers; /* number of tx holding buffer allocated */
239 int get_tx_holding_index; /* next tx holding buffer for adapter to load */
240 int put_tx_holding_index; /* next tx holding buffer to store user request */
241 int tx_holding_count; /* number of tx holding buffers waiting */
242 struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
243
244 bool rx_enabled;
245 bool rx_overflow;
246 bool rx_rcc_underrun;
247
248 bool tx_enabled;
249 bool tx_active;
250 u32 idle_mode;
251
252 u16 cmr_value;
253 u16 tcsr_value;
254
255 char device_name[25]; /* device instance name */
256
257 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
258 unsigned char bus; /* expansion bus number (zero based) */
259 unsigned char function; /* PCI device number */
260
261 unsigned int io_base; /* base I/O address of adapter */
262 unsigned int io_addr_size; /* size of the I/O address range */
263 bool io_addr_requested; /* true if I/O address requested */
264
265 unsigned int irq_level; /* interrupt level */
266 unsigned long irq_flags;
267 bool irq_requested; /* true if IRQ requested */
268
269 unsigned int dma_level; /* DMA channel */
270 bool dma_requested; /* true if dma channel requested */
271
272 u16 mbre_bit;
273 u16 loopback_bits;
274 u16 usc_idle_mode;
275
276 MGSL_PARAMS params; /* communications parameters */
277
278 unsigned char serial_signals; /* current serial signal states */
279
280 bool irq_occurred; /* for diagnostics use */
281 unsigned int init_error; /* Initialization startup error (DIAGS) */
282 int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
283
284 u32 last_mem_alloc;
285 unsigned char* memory_base; /* shared memory address (PCI only) */
286 u32 phys_memory_base;
287 bool shared_mem_requested;
288
289 unsigned char* lcr_base; /* local config registers (PCI only) */
290 u32 phys_lcr_base;
291 u32 lcr_offset;
292 bool lcr_mem_requested;
293
294 u32 misc_ctrl_value;
295 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
296 char char_buf[MAX_ASYNC_BUFFER_SIZE];
297 bool drop_rts_on_tx_done;
298
299 bool loopmode_insert_requested;
300 bool loopmode_send_done_requested;
301
302 struct _input_signal_events input_signal_events;
303
304 /* generic HDLC device parts */
305 int netcount;
306 spinlock_t netlock;
307
308#if SYNCLINK_GENERIC_HDLC
309 struct net_device *netdev;
310#endif
311};
312
313#define MGSL_MAGIC 0x5401
314
315/*
316 * The size of the serial xmit buffer is 1 page, or 4096 bytes
317 */
318#ifndef SERIAL_XMIT_SIZE
319#define SERIAL_XMIT_SIZE 4096
320#endif
321
322/*
323 * These macros define the offsets used in calculating the
324 * I/O address of the specified USC registers.
325 */
326
327
328#define DCPIN 2 /* Bit 1 of I/O address */
329#define SDPIN 4 /* Bit 2 of I/O address */
330
331#define DCAR 0 /* DMA command/address register */
332#define CCAR SDPIN /* channel command/address register */
333#define DATAREG DCPIN + SDPIN /* serial data register */
334#define MSBONLY 0x41
335#define LSBONLY 0x40
336
337/*
338 * These macros define the register address (ordinal number)
339 * used for writing address/value pairs to the USC.
340 */
341
342#define CMR 0x02 /* Channel mode Register */
343#define CCSR 0x04 /* Channel Command/status Register */
344#define CCR 0x06 /* Channel Control Register */
345#define PSR 0x08 /* Port status Register */
346#define PCR 0x0a /* Port Control Register */
347#define TMDR 0x0c /* Test mode Data Register */
348#define TMCR 0x0e /* Test mode Control Register */
349#define CMCR 0x10 /* Clock mode Control Register */
350#define HCR 0x12 /* Hardware Configuration Register */
351#define IVR 0x14 /* Interrupt Vector Register */
352#define IOCR 0x16 /* Input/Output Control Register */
353#define ICR 0x18 /* Interrupt Control Register */
354#define DCCR 0x1a /* Daisy Chain Control Register */
355#define MISR 0x1c /* Misc Interrupt status Register */
356#define SICR 0x1e /* status Interrupt Control Register */
357#define RDR 0x20 /* Receive Data Register */
358#define RMR 0x22 /* Receive mode Register */
359#define RCSR 0x24 /* Receive Command/status Register */
360#define RICR 0x26 /* Receive Interrupt Control Register */
361#define RSR 0x28 /* Receive Sync Register */
362#define RCLR 0x2a /* Receive count Limit Register */
363#define RCCR 0x2c /* Receive Character count Register */
364#define TC0R 0x2e /* Time Constant 0 Register */
365#define TDR 0x30 /* Transmit Data Register */
366#define TMR 0x32 /* Transmit mode Register */
367#define TCSR 0x34 /* Transmit Command/status Register */
368#define TICR 0x36 /* Transmit Interrupt Control Register */
369#define TSR 0x38 /* Transmit Sync Register */
370#define TCLR 0x3a /* Transmit count Limit Register */
371#define TCCR 0x3c /* Transmit Character count Register */
372#define TC1R 0x3e /* Time Constant 1 Register */
373
374
375/*
376 * MACRO DEFINITIONS FOR DMA REGISTERS
377 */
378
379#define DCR 0x06 /* DMA Control Register (shared) */
380#define DACR 0x08 /* DMA Array count Register (shared) */
381#define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
382#define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
383#define DICR 0x18 /* DMA Interrupt Control Register (shared) */
384#define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
385#define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
386
387#define TDMR 0x02 /* Transmit DMA mode Register */
388#define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
389#define TBCR 0x2a /* Transmit Byte count Register */
390#define TARL 0x2c /* Transmit Address Register (low) */
391#define TARU 0x2e /* Transmit Address Register (high) */
392#define NTBCR 0x3a /* Next Transmit Byte count Register */
393#define NTARL 0x3c /* Next Transmit Address Register (low) */
394#define NTARU 0x3e /* Next Transmit Address Register (high) */
395
396#define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
397#define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
398#define RBCR 0xaa /* Receive Byte count Register */
399#define RARL 0xac /* Receive Address Register (low) */
400#define RARU 0xae /* Receive Address Register (high) */
401#define NRBCR 0xba /* Next Receive Byte count Register */
402#define NRARL 0xbc /* Next Receive Address Register (low) */
403#define NRARU 0xbe /* Next Receive Address Register (high) */
404
405
406/*
407 * MACRO DEFINITIONS FOR MODEM STATUS BITS
408 */
409
410#define MODEMSTATUS_DTR 0x80
411#define MODEMSTATUS_DSR 0x40
412#define MODEMSTATUS_RTS 0x20
413#define MODEMSTATUS_CTS 0x10
414#define MODEMSTATUS_RI 0x04
415#define MODEMSTATUS_DCD 0x01
416
417
418/*
419 * Channel Command/Address Register (CCAR) Command Codes
420 */
421
422#define RTCmd_Null 0x0000
423#define RTCmd_ResetHighestIus 0x1000
424#define RTCmd_TriggerChannelLoadDma 0x2000
425#define RTCmd_TriggerRxDma 0x2800
426#define RTCmd_TriggerTxDma 0x3000
427#define RTCmd_TriggerRxAndTxDma 0x3800
428#define RTCmd_PurgeRxFifo 0x4800
429#define RTCmd_PurgeTxFifo 0x5000
430#define RTCmd_PurgeRxAndTxFifo 0x5800
431#define RTCmd_LoadRcc 0x6800
432#define RTCmd_LoadTcc 0x7000
433#define RTCmd_LoadRccAndTcc 0x7800
434#define RTCmd_LoadTC0 0x8800
435#define RTCmd_LoadTC1 0x9000
436#define RTCmd_LoadTC0AndTC1 0x9800
437#define RTCmd_SerialDataLSBFirst 0xa000
438#define RTCmd_SerialDataMSBFirst 0xa800
439#define RTCmd_SelectBigEndian 0xb000
440#define RTCmd_SelectLittleEndian 0xb800
441
442
443/*
444 * DMA Command/Address Register (DCAR) Command Codes
445 */
446
447#define DmaCmd_Null 0x0000
448#define DmaCmd_ResetTxChannel 0x1000
449#define DmaCmd_ResetRxChannel 0x1200
450#define DmaCmd_StartTxChannel 0x2000
451#define DmaCmd_StartRxChannel 0x2200
452#define DmaCmd_ContinueTxChannel 0x3000
453#define DmaCmd_ContinueRxChannel 0x3200
454#define DmaCmd_PauseTxChannel 0x4000
455#define DmaCmd_PauseRxChannel 0x4200
456#define DmaCmd_AbortTxChannel 0x5000
457#define DmaCmd_AbortRxChannel 0x5200
458#define DmaCmd_InitTxChannel 0x7000
459#define DmaCmd_InitRxChannel 0x7200
460#define DmaCmd_ResetHighestDmaIus 0x8000
461#define DmaCmd_ResetAllChannels 0x9000
462#define DmaCmd_StartAllChannels 0xa000
463#define DmaCmd_ContinueAllChannels 0xb000
464#define DmaCmd_PauseAllChannels 0xc000
465#define DmaCmd_AbortAllChannels 0xd000
466#define DmaCmd_InitAllChannels 0xf000
467
468#define TCmd_Null 0x0000
469#define TCmd_ClearTxCRC 0x2000
470#define TCmd_SelectTicrTtsaData 0x4000
471#define TCmd_SelectTicrTxFifostatus 0x5000
472#define TCmd_SelectTicrIntLevel 0x6000
473#define TCmd_SelectTicrdma_level 0x7000
474#define TCmd_SendFrame 0x8000
475#define TCmd_SendAbort 0x9000
476#define TCmd_EnableDleInsertion 0xc000
477#define TCmd_DisableDleInsertion 0xd000
478#define TCmd_ClearEofEom 0xe000
479#define TCmd_SetEofEom 0xf000
480
481#define RCmd_Null 0x0000
482#define RCmd_ClearRxCRC 0x2000
483#define RCmd_EnterHuntmode 0x3000
484#define RCmd_SelectRicrRtsaData 0x4000
485#define RCmd_SelectRicrRxFifostatus 0x5000
486#define RCmd_SelectRicrIntLevel 0x6000
487#define RCmd_SelectRicrdma_level 0x7000
488
489/*
490 * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
491 */
492
493#define RECEIVE_STATUS BIT5
494#define RECEIVE_DATA BIT4
495#define TRANSMIT_STATUS BIT3
496#define TRANSMIT_DATA BIT2
497#define IO_PIN BIT1
498#define MISC BIT0
499
500
501/*
502 * Receive status Bits in Receive Command/status Register RCSR
503 */
504
505#define RXSTATUS_SHORT_FRAME BIT8
506#define RXSTATUS_CODE_VIOLATION BIT8
507#define RXSTATUS_EXITED_HUNT BIT7
508#define RXSTATUS_IDLE_RECEIVED BIT6
509#define RXSTATUS_BREAK_RECEIVED BIT5
510#define RXSTATUS_ABORT_RECEIVED BIT5
511#define RXSTATUS_RXBOUND BIT4
512#define RXSTATUS_CRC_ERROR BIT3
513#define RXSTATUS_FRAMING_ERROR BIT3
514#define RXSTATUS_ABORT BIT2
515#define RXSTATUS_PARITY_ERROR BIT2
516#define RXSTATUS_OVERRUN BIT1
517#define RXSTATUS_DATA_AVAILABLE BIT0
518#define RXSTATUS_ALL 0x01f6
519#define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
520
521/*
522 * Values for setting transmit idle mode in
523 * Transmit Control/status Register (TCSR)
524 */
525#define IDLEMODE_FLAGS 0x0000
526#define IDLEMODE_ALT_ONE_ZERO 0x0100
527#define IDLEMODE_ZERO 0x0200
528#define IDLEMODE_ONE 0x0300
529#define IDLEMODE_ALT_MARK_SPACE 0x0500
530#define IDLEMODE_SPACE 0x0600
531#define IDLEMODE_MARK 0x0700
532#define IDLEMODE_MASK 0x0700
533
534/*
535 * IUSC revision identifiers
536 */
537#define IUSC_SL1660 0x4d44
538#define IUSC_PRE_SL1660 0x4553
539
540/*
541 * Transmit status Bits in Transmit Command/status Register (TCSR)
542 */
543
544#define TCSR_PRESERVE 0x0F00
545
546#define TCSR_UNDERWAIT BIT11
547#define TXSTATUS_PREAMBLE_SENT BIT7
548#define TXSTATUS_IDLE_SENT BIT6
549#define TXSTATUS_ABORT_SENT BIT5
550#define TXSTATUS_EOF_SENT BIT4
551#define TXSTATUS_EOM_SENT BIT4
552#define TXSTATUS_CRC_SENT BIT3
553#define TXSTATUS_ALL_SENT BIT2
554#define TXSTATUS_UNDERRUN BIT1
555#define TXSTATUS_FIFO_EMPTY BIT0
556#define TXSTATUS_ALL 0x00fa
557#define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
558
559
560#define MISCSTATUS_RXC_LATCHED BIT15
561#define MISCSTATUS_RXC BIT14
562#define MISCSTATUS_TXC_LATCHED BIT13
563#define MISCSTATUS_TXC BIT12
564#define MISCSTATUS_RI_LATCHED BIT11
565#define MISCSTATUS_RI BIT10
566#define MISCSTATUS_DSR_LATCHED BIT9
567#define MISCSTATUS_DSR BIT8
568#define MISCSTATUS_DCD_LATCHED BIT7
569#define MISCSTATUS_DCD BIT6
570#define MISCSTATUS_CTS_LATCHED BIT5
571#define MISCSTATUS_CTS BIT4
572#define MISCSTATUS_RCC_UNDERRUN BIT3
573#define MISCSTATUS_DPLL_NO_SYNC BIT2
574#define MISCSTATUS_BRG1_ZERO BIT1
575#define MISCSTATUS_BRG0_ZERO BIT0
576
577#define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
578#define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
579
580#define SICR_RXC_ACTIVE BIT15
581#define SICR_RXC_INACTIVE BIT14
582#define SICR_RXC (BIT15+BIT14)
583#define SICR_TXC_ACTIVE BIT13
584#define SICR_TXC_INACTIVE BIT12
585#define SICR_TXC (BIT13+BIT12)
586#define SICR_RI_ACTIVE BIT11
587#define SICR_RI_INACTIVE BIT10
588#define SICR_RI (BIT11+BIT10)
589#define SICR_DSR_ACTIVE BIT9
590#define SICR_DSR_INACTIVE BIT8
591#define SICR_DSR (BIT9+BIT8)
592#define SICR_DCD_ACTIVE BIT7
593#define SICR_DCD_INACTIVE BIT6
594#define SICR_DCD (BIT7+BIT6)
595#define SICR_CTS_ACTIVE BIT5
596#define SICR_CTS_INACTIVE BIT4
597#define SICR_CTS (BIT5+BIT4)
598#define SICR_RCC_UNDERFLOW BIT3
599#define SICR_DPLL_NO_SYNC BIT2
600#define SICR_BRG1_ZERO BIT1
601#define SICR_BRG0_ZERO BIT0
602
603void usc_DisableMasterIrqBit( struct mgsl_struct *info );
604void usc_EnableMasterIrqBit( struct mgsl_struct *info );
605void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
606void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
607void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
608
609#define usc_EnableInterrupts( a, b ) \
610 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
611
612#define usc_DisableInterrupts( a, b ) \
613 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
614
615#define usc_EnableMasterIrqBit(a) \
616 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
617
618#define usc_DisableMasterIrqBit(a) \
619 usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
620
621#define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
622
623/*
624 * Transmit status Bits in Transmit Control status Register (TCSR)
625 * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
626 */
627
628#define TXSTATUS_PREAMBLE_SENT BIT7
629#define TXSTATUS_IDLE_SENT BIT6
630#define TXSTATUS_ABORT_SENT BIT5
631#define TXSTATUS_EOF BIT4
632#define TXSTATUS_CRC_SENT BIT3
633#define TXSTATUS_ALL_SENT BIT2
634#define TXSTATUS_UNDERRUN BIT1
635#define TXSTATUS_FIFO_EMPTY BIT0
636
637#define DICR_MASTER BIT15
638#define DICR_TRANSMIT BIT0
639#define DICR_RECEIVE BIT1
640
641#define usc_EnableDmaInterrupts(a,b) \
642 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
643
644#define usc_DisableDmaInterrupts(a,b) \
645 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
646
647#define usc_EnableStatusIrqs(a,b) \
648 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
649
650#define usc_DisablestatusIrqs(a,b) \
651 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
652
653/* Transmit status Bits in Transmit Control status Register (TCSR) */
654/* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
655
656
657#define DISABLE_UNCONDITIONAL 0
658#define DISABLE_END_OF_FRAME 1
659#define ENABLE_UNCONDITIONAL 2
660#define ENABLE_AUTO_CTS 3
661#define ENABLE_AUTO_DCD 3
662#define usc_EnableTransmitter(a,b) \
663 usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
664#define usc_EnableReceiver(a,b) \
665 usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
666
667static u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
668static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
669static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
670
671static u16 usc_InReg( struct mgsl_struct *info, u16 Port );
672static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
673static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
674void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
675void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
676
677#define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
678#define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
679
680#define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
681
682static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
683static void usc_start_receiver( struct mgsl_struct *info );
684static void usc_stop_receiver( struct mgsl_struct *info );
685
686static void usc_start_transmitter( struct mgsl_struct *info );
687static void usc_stop_transmitter( struct mgsl_struct *info );
688static void usc_set_txidle( struct mgsl_struct *info );
689static void usc_load_txfifo( struct mgsl_struct *info );
690
691static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
692static void usc_enable_loopback( struct mgsl_struct *info, int enable );
693
694static void usc_get_serial_signals( struct mgsl_struct *info );
695static void usc_set_serial_signals( struct mgsl_struct *info );
696
697static void usc_reset( struct mgsl_struct *info );
698
699static void usc_set_sync_mode( struct mgsl_struct *info );
700static void usc_set_sdlc_mode( struct mgsl_struct *info );
701static void usc_set_async_mode( struct mgsl_struct *info );
702static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
703
704static void usc_loopback_frame( struct mgsl_struct *info );
705
706static void mgsl_tx_timeout(unsigned long context);
707
708
709static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
710static void usc_loopmode_insert_request( struct mgsl_struct * info );
711static int usc_loopmode_active( struct mgsl_struct * info);
712static void usc_loopmode_send_done( struct mgsl_struct * info );
713
714static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
715
716#if SYNCLINK_GENERIC_HDLC
717#define dev_to_port(D) (dev_to_hdlc(D)->priv)
718static void hdlcdev_tx_done(struct mgsl_struct *info);
719static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
720static int hdlcdev_init(struct mgsl_struct *info);
721static void hdlcdev_exit(struct mgsl_struct *info);
722#endif
723
724/*
725 * Defines a BUS descriptor value for the PCI adapter
726 * local bus address ranges.
727 */
728
729#define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
730(0x00400020 + \
731((WrHold) << 30) + \
732((WrDly) << 28) + \
733((RdDly) << 26) + \
734((Nwdd) << 20) + \
735((Nwad) << 15) + \
736((Nxda) << 13) + \
737((Nrdd) << 11) + \
738((Nrad) << 6) )
739
740static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
741
742/*
743 * Adapter diagnostic routines
744 */
745static bool mgsl_register_test( struct mgsl_struct *info );
746static bool mgsl_irq_test( struct mgsl_struct *info );
747static bool mgsl_dma_test( struct mgsl_struct *info );
748static bool mgsl_memory_test( struct mgsl_struct *info );
749static int mgsl_adapter_test( struct mgsl_struct *info );
750
751/*
752 * device and resource management routines
753 */
754static int mgsl_claim_resources(struct mgsl_struct *info);
755static void mgsl_release_resources(struct mgsl_struct *info);
756static void mgsl_add_device(struct mgsl_struct *info);
757static struct mgsl_struct* mgsl_allocate_device(void);
758
759/*
760 * DMA buffer manupulation functions.
761 */
762static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
763static bool mgsl_get_rx_frame( struct mgsl_struct *info );
764static bool mgsl_get_raw_rx_frame( struct mgsl_struct *info );
765static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
766static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
767static int num_free_tx_dma_buffers(struct mgsl_struct *info);
768static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
769static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
770
771/*
772 * DMA and Shared Memory buffer allocation and formatting
773 */
774static int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
775static void mgsl_free_dma_buffers(struct mgsl_struct *info);
776static int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
777static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
778static int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
779static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
780static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
781static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
782static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
783static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
784static bool load_next_tx_holding_buffer(struct mgsl_struct *info);
785static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
786
787/*
788 * Bottom half interrupt handlers
789 */
790static void mgsl_bh_handler(struct work_struct *work);
791static void mgsl_bh_receive(struct mgsl_struct *info);
792static void mgsl_bh_transmit(struct mgsl_struct *info);
793static void mgsl_bh_status(struct mgsl_struct *info);
794
795/*
796 * Interrupt handler routines and dispatch table.
797 */
798static void mgsl_isr_null( struct mgsl_struct *info );
799static void mgsl_isr_transmit_data( struct mgsl_struct *info );
800static void mgsl_isr_receive_data( struct mgsl_struct *info );
801static void mgsl_isr_receive_status( struct mgsl_struct *info );
802static void mgsl_isr_transmit_status( struct mgsl_struct *info );
803static void mgsl_isr_io_pin( struct mgsl_struct *info );
804static void mgsl_isr_misc( struct mgsl_struct *info );
805static void mgsl_isr_receive_dma( struct mgsl_struct *info );
806static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
807
808typedef void (*isr_dispatch_func)(struct mgsl_struct *);
809
810static isr_dispatch_func UscIsrTable[7] =
811{
812 mgsl_isr_null,
813 mgsl_isr_misc,
814 mgsl_isr_io_pin,
815 mgsl_isr_transmit_data,
816 mgsl_isr_transmit_status,
817 mgsl_isr_receive_data,
818 mgsl_isr_receive_status
819};
820
821/*
822 * ioctl call handlers
823 */
824static int tiocmget(struct tty_struct *tty);
825static int tiocmset(struct tty_struct *tty,
826 unsigned int set, unsigned int clear);
827static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
828 __user *user_icount);
829static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params);
830static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params);
831static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
832static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
833static int mgsl_txenable(struct mgsl_struct * info, int enable);
834static int mgsl_txabort(struct mgsl_struct * info);
835static int mgsl_rxenable(struct mgsl_struct * info, int enable);
836static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
837static int mgsl_loopmode_send_done( struct mgsl_struct * info );
838
839/* set non-zero on successful registration with PCI subsystem */
840static bool pci_registered;
841
842/*
843 * Global linked list of SyncLink devices
844 */
845static struct mgsl_struct *mgsl_device_list;
846static int mgsl_device_count;
847
848/*
849 * Set this param to non-zero to load eax with the
850 * .text section address and breakpoint on module load.
851 * This is useful for use with gdb and add-symbol-file command.
852 */
853static int break_on_load;
854
855/*
856 * Driver major number, defaults to zero to get auto
857 * assigned major number. May be forced as module parameter.
858 */
859static int ttymajor;
860
861/*
862 * Array of user specified options for ISA adapters.
863 */
864static int io[MAX_ISA_DEVICES];
865static int irq[MAX_ISA_DEVICES];
866static int dma[MAX_ISA_DEVICES];
867static int debug_level;
868static int maxframe[MAX_TOTAL_DEVICES];
869static int txdmabufs[MAX_TOTAL_DEVICES];
870static int txholdbufs[MAX_TOTAL_DEVICES];
871
872module_param(break_on_load, bool, 0);
873module_param(ttymajor, int, 0);
874module_param_array(io, int, NULL, 0);
875module_param_array(irq, int, NULL, 0);
876module_param_array(dma, int, NULL, 0);
877module_param(debug_level, int, 0);
878module_param_array(maxframe, int, NULL, 0);
879module_param_array(txdmabufs, int, NULL, 0);
880module_param_array(txholdbufs, int, NULL, 0);
881
882static char *driver_name = "SyncLink serial driver";
883static char *driver_version = "$Revision: 4.38 $";
884
885static int synclink_init_one (struct pci_dev *dev,
886 const struct pci_device_id *ent);
887static void synclink_remove_one (struct pci_dev *dev);
888
889static struct pci_device_id synclink_pci_tbl[] = {
890 { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
891 { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
892 { 0, }, /* terminate list */
893};
894MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
895
896MODULE_LICENSE("GPL");
897
898static struct pci_driver synclink_pci_driver = {
899 .name = "synclink",
900 .id_table = synclink_pci_tbl,
901 .probe = synclink_init_one,
902 .remove = __devexit_p(synclink_remove_one),
903};
904
905static struct tty_driver *serial_driver;
906
907/* number of characters left in xmit buffer before we ask for more */
908#define WAKEUP_CHARS 256
909
910
911static void mgsl_change_params(struct mgsl_struct *info);
912static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
913
914/*
915 * 1st function defined in .text section. Calling this function in
916 * init_module() followed by a breakpoint allows a remote debugger
917 * (gdb) to get the .text address for the add-symbol-file command.
918 * This allows remote debugging of dynamically loadable modules.
919 */
920static void* mgsl_get_text_ptr(void)
921{
922 return mgsl_get_text_ptr;
923}
924
925static inline int mgsl_paranoia_check(struct mgsl_struct *info,
926 char *name, const char *routine)
927{
928#ifdef MGSL_PARANOIA_CHECK
929 static const char *badmagic =
930 "Warning: bad magic number for mgsl struct (%s) in %s\n";
931 static const char *badinfo =
932 "Warning: null mgsl_struct for (%s) in %s\n";
933
934 if (!info) {
935 printk(badinfo, name, routine);
936 return 1;
937 }
938 if (info->magic != MGSL_MAGIC) {
939 printk(badmagic, name, routine);
940 return 1;
941 }
942#else
943 if (!info)
944 return 1;
945#endif
946 return 0;
947}
948
949/**
950 * line discipline callback wrappers
951 *
952 * The wrappers maintain line discipline references
953 * while calling into the line discipline.
954 *
955 * ldisc_receive_buf - pass receive data to line discipline
956 */
957
958static void ldisc_receive_buf(struct tty_struct *tty,
959 const __u8 *data, char *flags, int count)
960{
961 struct tty_ldisc *ld;
962 if (!tty)
963 return;
964 ld = tty_ldisc_ref(tty);
965 if (ld) {
966 if (ld->ops->receive_buf)
967 ld->ops->receive_buf(tty, data, flags, count);
968 tty_ldisc_deref(ld);
969 }
970}
971
972/* mgsl_stop() throttle (stop) transmitter
973 *
974 * Arguments: tty pointer to tty info structure
975 * Return Value: None
976 */
977static void mgsl_stop(struct tty_struct *tty)
978{
979 struct mgsl_struct *info = tty->driver_data;
980 unsigned long flags;
981
982 if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
983 return;
984
985 if ( debug_level >= DEBUG_LEVEL_INFO )
986 printk("mgsl_stop(%s)\n",info->device_name);
987
988 spin_lock_irqsave(&info->irq_spinlock,flags);
989 if (info->tx_enabled)
990 usc_stop_transmitter(info);
991 spin_unlock_irqrestore(&info->irq_spinlock,flags);
992
993} /* end of mgsl_stop() */
994
995/* mgsl_start() release (start) transmitter
996 *
997 * Arguments: tty pointer to tty info structure
998 * Return Value: None
999 */
1000static void mgsl_start(struct tty_struct *tty)
1001{
1002 struct mgsl_struct *info = tty->driver_data;
1003 unsigned long flags;
1004
1005 if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
1006 return;
1007
1008 if ( debug_level >= DEBUG_LEVEL_INFO )
1009 printk("mgsl_start(%s)\n",info->device_name);
1010
1011 spin_lock_irqsave(&info->irq_spinlock,flags);
1012 if (!info->tx_enabled)
1013 usc_start_transmitter(info);
1014 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1015
1016} /* end of mgsl_start() */
1017
1018/*
1019 * Bottom half work queue access functions
1020 */
1021
1022/* mgsl_bh_action() Return next bottom half action to perform.
1023 * Return Value: BH action code or 0 if nothing to do.
1024 */
1025static int mgsl_bh_action(struct mgsl_struct *info)
1026{
1027 unsigned long flags;
1028 int rc = 0;
1029
1030 spin_lock_irqsave(&info->irq_spinlock,flags);
1031
1032 if (info->pending_bh & BH_RECEIVE) {
1033 info->pending_bh &= ~BH_RECEIVE;
1034 rc = BH_RECEIVE;
1035 } else if (info->pending_bh & BH_TRANSMIT) {
1036 info->pending_bh &= ~BH_TRANSMIT;
1037 rc = BH_TRANSMIT;
1038 } else if (info->pending_bh & BH_STATUS) {
1039 info->pending_bh &= ~BH_STATUS;
1040 rc = BH_STATUS;
1041 }
1042
1043 if (!rc) {
1044 /* Mark BH routine as complete */
1045 info->bh_running = false;
1046 info->bh_requested = false;
1047 }
1048
1049 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1050
1051 return rc;
1052}
1053
1054/*
1055 * Perform bottom half processing of work items queued by ISR.
1056 */
1057static void mgsl_bh_handler(struct work_struct *work)
1058{
1059 struct mgsl_struct *info =
1060 container_of(work, struct mgsl_struct, task);
1061 int action;
1062
1063 if (!info)
1064 return;
1065
1066 if ( debug_level >= DEBUG_LEVEL_BH )
1067 printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
1068 __FILE__,__LINE__,info->device_name);
1069
1070 info->bh_running = true;
1071
1072 while((action = mgsl_bh_action(info)) != 0) {
1073
1074 /* Process work item */
1075 if ( debug_level >= DEBUG_LEVEL_BH )
1076 printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
1077 __FILE__,__LINE__,action);
1078
1079 switch (action) {
1080
1081 case BH_RECEIVE:
1082 mgsl_bh_receive(info);
1083 break;
1084 case BH_TRANSMIT:
1085 mgsl_bh_transmit(info);
1086 break;
1087 case BH_STATUS:
1088 mgsl_bh_status(info);
1089 break;
1090 default:
1091 /* unknown work item ID */
1092 printk("Unknown work item ID=%08X!\n", action);
1093 break;
1094 }
1095 }
1096
1097 if ( debug_level >= DEBUG_LEVEL_BH )
1098 printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
1099 __FILE__,__LINE__,info->device_name);
1100}
1101
1102static void mgsl_bh_receive(struct mgsl_struct *info)
1103{
1104 bool (*get_rx_frame)(struct mgsl_struct *info) =
1105 (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
1106
1107 if ( debug_level >= DEBUG_LEVEL_BH )
1108 printk( "%s(%d):mgsl_bh_receive(%s)\n",
1109 __FILE__,__LINE__,info->device_name);
1110
1111 do
1112 {
1113 if (info->rx_rcc_underrun) {
1114 unsigned long flags;
1115 spin_lock_irqsave(&info->irq_spinlock,flags);
1116 usc_start_receiver(info);
1117 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1118 return;
1119 }
1120 } while(get_rx_frame(info));
1121}
1122
1123static void mgsl_bh_transmit(struct mgsl_struct *info)
1124{
1125 struct tty_struct *tty = info->port.tty;
1126 unsigned long flags;
1127
1128 if ( debug_level >= DEBUG_LEVEL_BH )
1129 printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
1130 __FILE__,__LINE__,info->device_name);
1131
1132 if (tty)
1133 tty_wakeup(tty);
1134
1135 /* if transmitter idle and loopmode_send_done_requested
1136 * then start echoing RxD to TxD
1137 */
1138 spin_lock_irqsave(&info->irq_spinlock,flags);
1139 if ( !info->tx_active && info->loopmode_send_done_requested )
1140 usc_loopmode_send_done( info );
1141 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1142}
1143
1144static void mgsl_bh_status(struct mgsl_struct *info)
1145{
1146 if ( debug_level >= DEBUG_LEVEL_BH )
1147 printk( "%s(%d):mgsl_bh_status() entry on %s\n",
1148 __FILE__,__LINE__,info->device_name);
1149
1150 info->ri_chkcount = 0;
1151 info->dsr_chkcount = 0;
1152 info->dcd_chkcount = 0;
1153 info->cts_chkcount = 0;
1154}
1155
1156/* mgsl_isr_receive_status()
1157 *
1158 * Service a receive status interrupt. The type of status
1159 * interrupt is indicated by the state of the RCSR.
1160 * This is only used for HDLC mode.
1161 *
1162 * Arguments: info pointer to device instance data
1163 * Return Value: None
1164 */
1165static void mgsl_isr_receive_status( struct mgsl_struct *info )
1166{
1167 u16 status = usc_InReg( info, RCSR );
1168
1169 if ( debug_level >= DEBUG_LEVEL_ISR )
1170 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1171 __FILE__,__LINE__,status);
1172
1173 if ( (status & RXSTATUS_ABORT_RECEIVED) &&
1174 info->loopmode_insert_requested &&
1175 usc_loopmode_active(info) )
1176 {
1177 ++info->icount.rxabort;
1178 info->loopmode_insert_requested = false;
1179
1180 /* clear CMR:13 to start echoing RxD to TxD */
1181 info->cmr_value &= ~BIT13;
1182 usc_OutReg(info, CMR, info->cmr_value);
1183
1184 /* disable received abort irq (no longer required) */
1185 usc_OutReg(info, RICR,
1186 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1187 }
1188
1189 if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
1190 if (status & RXSTATUS_EXITED_HUNT)
1191 info->icount.exithunt++;
1192 if (status & RXSTATUS_IDLE_RECEIVED)
1193 info->icount.rxidle++;
1194 wake_up_interruptible(&info->event_wait_q);
1195 }
1196
1197 if (status & RXSTATUS_OVERRUN){
1198 info->icount.rxover++;
1199 usc_process_rxoverrun_sync( info );
1200 }
1201
1202 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
1203 usc_UnlatchRxstatusBits( info, status );
1204
1205} /* end of mgsl_isr_receive_status() */
1206
1207/* mgsl_isr_transmit_status()
1208 *
1209 * Service a transmit status interrupt
1210 * HDLC mode :end of transmit frame
1211 * Async mode:all data is sent
1212 * transmit status is indicated by bits in the TCSR.
1213 *
1214 * Arguments: info pointer to device instance data
1215 * Return Value: None
1216 */
1217static void mgsl_isr_transmit_status( struct mgsl_struct *info )
1218{
1219 u16 status = usc_InReg( info, TCSR );
1220
1221 if ( debug_level >= DEBUG_LEVEL_ISR )
1222 printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
1223 __FILE__,__LINE__,status);
1224
1225 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
1226 usc_UnlatchTxstatusBits( info, status );
1227
1228 if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
1229 {
1230 /* finished sending HDLC abort. This may leave */
1231 /* the TxFifo with data from the aborted frame */
1232 /* so purge the TxFifo. Also shutdown the DMA */
1233 /* channel in case there is data remaining in */
1234 /* the DMA buffer */
1235 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
1236 usc_RTCmd( info, RTCmd_PurgeTxFifo );
1237 }
1238
1239 if ( status & TXSTATUS_EOF_SENT )
1240 info->icount.txok++;
1241 else if ( status & TXSTATUS_UNDERRUN )
1242 info->icount.txunder++;
1243 else if ( status & TXSTATUS_ABORT_SENT )
1244 info->icount.txabort++;
1245 else
1246 info->icount.txunder++;
1247
1248 info->tx_active = false;
1249 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1250 del_timer(&info->tx_timer);
1251
1252 if ( info->drop_rts_on_tx_done ) {
1253 usc_get_serial_signals( info );
1254 if ( info->serial_signals & SerialSignal_RTS ) {
1255 info->serial_signals &= ~SerialSignal_RTS;
1256 usc_set_serial_signals( info );
1257 }
1258 info->drop_rts_on_tx_done = false;
1259 }
1260
1261#if SYNCLINK_GENERIC_HDLC
1262 if (info->netcount)
1263 hdlcdev_tx_done(info);
1264 else
1265#endif
1266 {
1267 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
1268 usc_stop_transmitter(info);
1269 return;
1270 }
1271 info->pending_bh |= BH_TRANSMIT;
1272 }
1273
1274} /* end of mgsl_isr_transmit_status() */
1275
1276/* mgsl_isr_io_pin()
1277 *
1278 * Service an Input/Output pin interrupt. The type of
1279 * interrupt is indicated by bits in the MISR
1280 *
1281 * Arguments: info pointer to device instance data
1282 * Return Value: None
1283 */
1284static void mgsl_isr_io_pin( struct mgsl_struct *info )
1285{
1286 struct mgsl_icount *icount;
1287 u16 status = usc_InReg( info, MISR );
1288
1289 if ( debug_level >= DEBUG_LEVEL_ISR )
1290 printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
1291 __FILE__,__LINE__,status);
1292
1293 usc_ClearIrqPendingBits( info, IO_PIN );
1294 usc_UnlatchIostatusBits( info, status );
1295
1296 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
1297 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
1298 icount = &info->icount;
1299 /* update input line counters */
1300 if (status & MISCSTATUS_RI_LATCHED) {
1301 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1302 usc_DisablestatusIrqs(info,SICR_RI);
1303 icount->rng++;
1304 if ( status & MISCSTATUS_RI )
1305 info->input_signal_events.ri_up++;
1306 else
1307 info->input_signal_events.ri_down++;
1308 }
1309 if (status & MISCSTATUS_DSR_LATCHED) {
1310 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1311 usc_DisablestatusIrqs(info,SICR_DSR);
1312 icount->dsr++;
1313 if ( status & MISCSTATUS_DSR )
1314 info->input_signal_events.dsr_up++;
1315 else
1316 info->input_signal_events.dsr_down++;
1317 }
1318 if (status & MISCSTATUS_DCD_LATCHED) {
1319 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1320 usc_DisablestatusIrqs(info,SICR_DCD);
1321 icount->dcd++;
1322 if (status & MISCSTATUS_DCD) {
1323 info->input_signal_events.dcd_up++;
1324 } else
1325 info->input_signal_events.dcd_down++;
1326#if SYNCLINK_GENERIC_HDLC
1327 if (info->netcount) {
1328 if (status & MISCSTATUS_DCD)
1329 netif_carrier_on(info->netdev);
1330 else
1331 netif_carrier_off(info->netdev);
1332 }
1333#endif
1334 }
1335 if (status & MISCSTATUS_CTS_LATCHED)
1336 {
1337 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1338 usc_DisablestatusIrqs(info,SICR_CTS);
1339 icount->cts++;
1340 if ( status & MISCSTATUS_CTS )
1341 info->input_signal_events.cts_up++;
1342 else
1343 info->input_signal_events.cts_down++;
1344 }
1345 wake_up_interruptible(&info->status_event_wait_q);
1346 wake_up_interruptible(&info->event_wait_q);
1347
1348 if ( (info->port.flags & ASYNC_CHECK_CD) &&
1349 (status & MISCSTATUS_DCD_LATCHED) ) {
1350 if ( debug_level >= DEBUG_LEVEL_ISR )
1351 printk("%s CD now %s...", info->device_name,
1352 (status & MISCSTATUS_DCD) ? "on" : "off");
1353 if (status & MISCSTATUS_DCD)
1354 wake_up_interruptible(&info->port.open_wait);
1355 else {
1356 if ( debug_level >= DEBUG_LEVEL_ISR )
1357 printk("doing serial hangup...");
1358 if (info->port.tty)
1359 tty_hangup(info->port.tty);
1360 }
1361 }
1362
1363 if ( (info->port.flags & ASYNC_CTS_FLOW) &&
1364 (status & MISCSTATUS_CTS_LATCHED) ) {
1365 if (info->port.tty->hw_stopped) {
1366 if (status & MISCSTATUS_CTS) {
1367 if ( debug_level >= DEBUG_LEVEL_ISR )
1368 printk("CTS tx start...");
1369 if (info->port.tty)
1370 info->port.tty->hw_stopped = 0;
1371 usc_start_transmitter(info);
1372 info->pending_bh |= BH_TRANSMIT;
1373 return;
1374 }
1375 } else {
1376 if (!(status & MISCSTATUS_CTS)) {
1377 if ( debug_level >= DEBUG_LEVEL_ISR )
1378 printk("CTS tx stop...");
1379 if (info->port.tty)
1380 info->port.tty->hw_stopped = 1;
1381 usc_stop_transmitter(info);
1382 }
1383 }
1384 }
1385 }
1386
1387 info->pending_bh |= BH_STATUS;
1388
1389 /* for diagnostics set IRQ flag */
1390 if ( status & MISCSTATUS_TXC_LATCHED ){
1391 usc_OutReg( info, SICR,
1392 (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
1393 usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
1394 info->irq_occurred = true;
1395 }
1396
1397} /* end of mgsl_isr_io_pin() */
1398
1399/* mgsl_isr_transmit_data()
1400 *
1401 * Service a transmit data interrupt (async mode only).
1402 *
1403 * Arguments: info pointer to device instance data
1404 * Return Value: None
1405 */
1406static void mgsl_isr_transmit_data( struct mgsl_struct *info )
1407{
1408 if ( debug_level >= DEBUG_LEVEL_ISR )
1409 printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
1410 __FILE__,__LINE__,info->xmit_cnt);
1411
1412 usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
1413
1414 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
1415 usc_stop_transmitter(info);
1416 return;
1417 }
1418
1419 if ( info->xmit_cnt )
1420 usc_load_txfifo( info );
1421 else
1422 info->tx_active = false;
1423
1424 if (info->xmit_cnt < WAKEUP_CHARS)
1425 info->pending_bh |= BH_TRANSMIT;
1426
1427} /* end of mgsl_isr_transmit_data() */
1428
1429/* mgsl_isr_receive_data()
1430 *
1431 * Service a receive data interrupt. This occurs
1432 * when operating in asynchronous interrupt transfer mode.
1433 * The receive data FIFO is flushed to the receive data buffers.
1434 *
1435 * Arguments: info pointer to device instance data
1436 * Return Value: None
1437 */
1438static void mgsl_isr_receive_data( struct mgsl_struct *info )
1439{
1440 int Fifocount;
1441 u16 status;
1442 int work = 0;
1443 unsigned char DataByte;
1444 struct tty_struct *tty = info->port.tty;
1445 struct mgsl_icount *icount = &info->icount;
1446
1447 if ( debug_level >= DEBUG_LEVEL_ISR )
1448 printk("%s(%d):mgsl_isr_receive_data\n",
1449 __FILE__,__LINE__);
1450
1451 usc_ClearIrqPendingBits( info, RECEIVE_DATA );
1452
1453 /* select FIFO status for RICR readback */
1454 usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
1455
1456 /* clear the Wordstatus bit so that status readback */
1457 /* only reflects the status of this byte */
1458 usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
1459
1460 /* flush the receive FIFO */
1461
1462 while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
1463 int flag;
1464
1465 /* read one byte from RxFIFO */
1466 outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
1467 info->io_base + CCAR );
1468 DataByte = inb( info->io_base + CCAR );
1469
1470 /* get the status of the received byte */
1471 status = usc_InReg(info, RCSR);
1472 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1473 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
1474 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1475
1476 icount->rx++;
1477
1478 flag = 0;
1479 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1480 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
1481 printk("rxerr=%04X\n",status);
1482 /* update error statistics */
1483 if ( status & RXSTATUS_BREAK_RECEIVED ) {
1484 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
1485 icount->brk++;
1486 } else if (status & RXSTATUS_PARITY_ERROR)
1487 icount->parity++;
1488 else if (status & RXSTATUS_FRAMING_ERROR)
1489 icount->frame++;
1490 else if (status & RXSTATUS_OVERRUN) {
1491 /* must issue purge fifo cmd before */
1492 /* 16C32 accepts more receive chars */
1493 usc_RTCmd(info,RTCmd_PurgeRxFifo);
1494 icount->overrun++;
1495 }
1496
1497 /* discard char if tty control flags say so */
1498 if (status & info->ignore_status_mask)
1499 continue;
1500
1501 status &= info->read_status_mask;
1502
1503 if (status & RXSTATUS_BREAK_RECEIVED) {
1504 flag = TTY_BREAK;
1505 if (info->port.flags & ASYNC_SAK)
1506 do_SAK(tty);
1507 } else if (status & RXSTATUS_PARITY_ERROR)
1508 flag = TTY_PARITY;
1509 else if (status & RXSTATUS_FRAMING_ERROR)
1510 flag = TTY_FRAME;
1511 } /* end of if (error) */
1512 tty_insert_flip_char(tty, DataByte, flag);
1513 if (status & RXSTATUS_OVERRUN) {
1514 /* Overrun is special, since it's
1515 * reported immediately, and doesn't
1516 * affect the current character
1517 */
1518 work += tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1519 }
1520 }
1521
1522 if ( debug_level >= DEBUG_LEVEL_ISR ) {
1523 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1524 __FILE__,__LINE__,icount->rx,icount->brk,
1525 icount->parity,icount->frame,icount->overrun);
1526 }
1527
1528 if(work)
1529 tty_flip_buffer_push(tty);
1530}
1531
1532/* mgsl_isr_misc()
1533 *
1534 * Service a miscellaneous interrupt source.
1535 *
1536 * Arguments: info pointer to device extension (instance data)
1537 * Return Value: None
1538 */
1539static void mgsl_isr_misc( struct mgsl_struct *info )
1540{
1541 u16 status = usc_InReg( info, MISR );
1542
1543 if ( debug_level >= DEBUG_LEVEL_ISR )
1544 printk("%s(%d):mgsl_isr_misc status=%04X\n",
1545 __FILE__,__LINE__,status);
1546
1547 if ((status & MISCSTATUS_RCC_UNDERRUN) &&
1548 (info->params.mode == MGSL_MODE_HDLC)) {
1549
1550 /* turn off receiver and rx DMA */
1551 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1552 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1553 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
1554 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
1555 usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS);
1556
1557 /* schedule BH handler to restart receiver */
1558 info->pending_bh |= BH_RECEIVE;
1559 info->rx_rcc_underrun = true;
1560 }
1561
1562 usc_ClearIrqPendingBits( info, MISC );
1563 usc_UnlatchMiscstatusBits( info, status );
1564
1565} /* end of mgsl_isr_misc() */
1566
1567/* mgsl_isr_null()
1568 *
1569 * Services undefined interrupt vectors from the
1570 * USC. (hence this function SHOULD never be called)
1571 *
1572 * Arguments: info pointer to device extension (instance data)
1573 * Return Value: None
1574 */
1575static void mgsl_isr_null( struct mgsl_struct *info )
1576{
1577
1578} /* end of mgsl_isr_null() */
1579
1580/* mgsl_isr_receive_dma()
1581 *
1582 * Service a receive DMA channel interrupt.
1583 * For this driver there are two sources of receive DMA interrupts
1584 * as identified in the Receive DMA mode Register (RDMR):
1585 *
1586 * BIT3 EOA/EOL End of List, all receive buffers in receive
1587 * buffer list have been filled (no more free buffers
1588 * available). The DMA controller has shut down.
1589 *
1590 * BIT2 EOB End of Buffer. This interrupt occurs when a receive
1591 * DMA buffer is terminated in response to completion
1592 * of a good frame or a frame with errors. The status
1593 * of the frame is stored in the buffer entry in the
1594 * list of receive buffer entries.
1595 *
1596 * Arguments: info pointer to device instance data
1597 * Return Value: None
1598 */
1599static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1600{
1601 u16 status;
1602
1603 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1604 usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
1605
1606 /* Read the receive DMA status to identify interrupt type. */
1607 /* This also clears the status bits. */
1608 status = usc_InDmaReg( info, RDMR );
1609
1610 if ( debug_level >= DEBUG_LEVEL_ISR )
1611 printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
1612 __FILE__,__LINE__,info->device_name,status);
1613
1614 info->pending_bh |= BH_RECEIVE;
1615
1616 if ( status & BIT3 ) {
1617 info->rx_overflow = true;
1618 info->icount.buf_overrun++;
1619 }
1620
1621} /* end of mgsl_isr_receive_dma() */
1622
1623/* mgsl_isr_transmit_dma()
1624 *
1625 * This function services a transmit DMA channel interrupt.
1626 *
1627 * For this driver there is one source of transmit DMA interrupts
1628 * as identified in the Transmit DMA Mode Register (TDMR):
1629 *
1630 * BIT2 EOB End of Buffer. This interrupt occurs when a
1631 * transmit DMA buffer has been emptied.
1632 *
1633 * The driver maintains enough transmit DMA buffers to hold at least
1634 * one max frame size transmit frame. When operating in a buffered
1635 * transmit mode, there may be enough transmit DMA buffers to hold at
1636 * least two or more max frame size frames. On an EOB condition,
1637 * determine if there are any queued transmit buffers and copy into
1638 * transmit DMA buffers if we have room.
1639 *
1640 * Arguments: info pointer to device instance data
1641 * Return Value: None
1642 */
1643static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1644{
1645 u16 status;
1646
1647 /* clear interrupt pending and IUS bit for Tx DMA IRQ */
1648 usc_OutDmaReg(info, CDIR, BIT8+BIT0 );
1649
1650 /* Read the transmit DMA status to identify interrupt type. */
1651 /* This also clears the status bits. */
1652
1653 status = usc_InDmaReg( info, TDMR );
1654
1655 if ( debug_level >= DEBUG_LEVEL_ISR )
1656 printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
1657 __FILE__,__LINE__,info->device_name,status);
1658
1659 if ( status & BIT2 ) {
1660 --info->tx_dma_buffers_used;
1661
1662 /* if there are transmit frames queued,
1663 * try to load the next one
1664 */
1665 if ( load_next_tx_holding_buffer(info) ) {
1666 /* if call returns non-zero value, we have
1667 * at least one free tx holding buffer
1668 */
1669 info->pending_bh |= BH_TRANSMIT;
1670 }
1671 }
1672
1673} /* end of mgsl_isr_transmit_dma() */
1674
1675/* mgsl_interrupt()
1676 *
1677 * Interrupt service routine entry point.
1678 *
1679 * Arguments:
1680 *
1681 * irq interrupt number that caused interrupt
1682 * dev_id device ID supplied during interrupt registration
1683 *
1684 * Return Value: None
1685 */
1686static irqreturn_t mgsl_interrupt(int dummy, void *dev_id)
1687{
1688 struct mgsl_struct *info = dev_id;
1689 u16 UscVector;
1690 u16 DmaVector;
1691
1692 if ( debug_level >= DEBUG_LEVEL_ISR )
1693 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)entry.\n",
1694 __FILE__, __LINE__, info->irq_level);
1695
1696 spin_lock(&info->irq_spinlock);
1697
1698 for(;;) {
1699 /* Read the interrupt vectors from hardware. */
1700 UscVector = usc_InReg(info, IVR) >> 9;
1701 DmaVector = usc_InDmaReg(info, DIVR);
1702
1703 if ( debug_level >= DEBUG_LEVEL_ISR )
1704 printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
1705 __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
1706
1707 if ( !UscVector && !DmaVector )
1708 break;
1709
1710 /* Dispatch interrupt vector */
1711 if ( UscVector )
1712 (*UscIsrTable[UscVector])(info);
1713 else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
1714 mgsl_isr_transmit_dma(info);
1715 else
1716 mgsl_isr_receive_dma(info);
1717
1718 if ( info->isr_overflow ) {
1719 printk(KERN_ERR "%s(%d):%s isr overflow irq=%d\n",
1720 __FILE__, __LINE__, info->device_name, info->irq_level);
1721 usc_DisableMasterIrqBit(info);
1722 usc_DisableDmaInterrupts(info,DICR_MASTER);
1723 break;
1724 }
1725 }
1726
1727 /* Request bottom half processing if there's something
1728 * for it to do and the bh is not already running
1729 */
1730
1731 if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
1732 if ( debug_level >= DEBUG_LEVEL_ISR )
1733 printk("%s(%d):%s queueing bh task.\n",
1734 __FILE__,__LINE__,info->device_name);
1735 schedule_work(&info->task);
1736 info->bh_requested = true;
1737 }
1738
1739 spin_unlock(&info->irq_spinlock);
1740
1741 if ( debug_level >= DEBUG_LEVEL_ISR )
1742 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)exit.\n",
1743 __FILE__, __LINE__, info->irq_level);
1744
1745 return IRQ_HANDLED;
1746} /* end of mgsl_interrupt() */
1747
1748/* startup()
1749 *
1750 * Initialize and start device.
1751 *
1752 * Arguments: info pointer to device instance data
1753 * Return Value: 0 if success, otherwise error code
1754 */
1755static int startup(struct mgsl_struct * info)
1756{
1757 int retval = 0;
1758
1759 if ( debug_level >= DEBUG_LEVEL_INFO )
1760 printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
1761
1762 if (info->port.flags & ASYNC_INITIALIZED)
1763 return 0;
1764
1765 if (!info->xmit_buf) {
1766 /* allocate a page of memory for a transmit buffer */
1767 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1768 if (!info->xmit_buf) {
1769 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1770 __FILE__,__LINE__,info->device_name);
1771 return -ENOMEM;
1772 }
1773 }
1774
1775 info->pending_bh = 0;
1776
1777 memset(&info->icount, 0, sizeof(info->icount));
1778
1779 setup_timer(&info->tx_timer, mgsl_tx_timeout, (unsigned long)info);
1780
1781 /* Allocate and claim adapter resources */
1782 retval = mgsl_claim_resources(info);
1783
1784 /* perform existence check and diagnostics */
1785 if ( !retval )
1786 retval = mgsl_adapter_test(info);
1787
1788 if ( retval ) {
1789 if (capable(CAP_SYS_ADMIN) && info->port.tty)
1790 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1791 mgsl_release_resources(info);
1792 return retval;
1793 }
1794
1795 /* program hardware for current parameters */
1796 mgsl_change_params(info);
1797
1798 if (info->port.tty)
1799 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1800
1801 info->port.flags |= ASYNC_INITIALIZED;
1802
1803 return 0;
1804
1805} /* end of startup() */
1806
1807/* shutdown()
1808 *
1809 * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
1810 *
1811 * Arguments: info pointer to device instance data
1812 * Return Value: None
1813 */
1814static void shutdown(struct mgsl_struct * info)
1815{
1816 unsigned long flags;
1817
1818 if (!(info->port.flags & ASYNC_INITIALIZED))
1819 return;
1820
1821 if (debug_level >= DEBUG_LEVEL_INFO)
1822 printk("%s(%d):mgsl_shutdown(%s)\n",
1823 __FILE__,__LINE__, info->device_name );
1824
1825 /* clear status wait queue because status changes */
1826 /* can't happen after shutting down the hardware */
1827 wake_up_interruptible(&info->status_event_wait_q);
1828 wake_up_interruptible(&info->event_wait_q);
1829
1830 del_timer_sync(&info->tx_timer);
1831
1832 if (info->xmit_buf) {
1833 free_page((unsigned long) info->xmit_buf);
1834 info->xmit_buf = NULL;
1835 }
1836
1837 spin_lock_irqsave(&info->irq_spinlock,flags);
1838 usc_DisableMasterIrqBit(info);
1839 usc_stop_receiver(info);
1840 usc_stop_transmitter(info);
1841 usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
1842 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
1843 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1844
1845 /* Disable DMAEN (Port 7, Bit 14) */
1846 /* This disconnects the DMA request signal from the ISA bus */
1847 /* on the ISA adapter. This has no effect for the PCI adapter */
1848 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
1849
1850 /* Disable INTEN (Port 6, Bit12) */
1851 /* This disconnects the IRQ request signal to the ISA bus */
1852 /* on the ISA adapter. This has no effect for the PCI adapter */
1853 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
1854
1855 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
1856 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1857 usc_set_serial_signals(info);
1858 }
1859
1860 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1861
1862 mgsl_release_resources(info);
1863
1864 if (info->port.tty)
1865 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1866
1867 info->port.flags &= ~ASYNC_INITIALIZED;
1868
1869} /* end of shutdown() */
1870
1871static void mgsl_program_hw(struct mgsl_struct *info)
1872{
1873 unsigned long flags;
1874
1875 spin_lock_irqsave(&info->irq_spinlock,flags);
1876
1877 usc_stop_receiver(info);
1878 usc_stop_transmitter(info);
1879 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1880
1881 if (info->params.mode == MGSL_MODE_HDLC ||
1882 info->params.mode == MGSL_MODE_RAW ||
1883 info->netcount)
1884 usc_set_sync_mode(info);
1885 else
1886 usc_set_async_mode(info);
1887
1888 usc_set_serial_signals(info);
1889
1890 info->dcd_chkcount = 0;
1891 info->cts_chkcount = 0;
1892 info->ri_chkcount = 0;
1893 info->dsr_chkcount = 0;
1894
1895 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
1896 usc_EnableInterrupts(info, IO_PIN);
1897 usc_get_serial_signals(info);
1898
1899 if (info->netcount || info->port.tty->termios->c_cflag & CREAD)
1900 usc_start_receiver(info);
1901
1902 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1903}
1904
1905/* Reconfigure adapter based on new parameters
1906 */
1907static void mgsl_change_params(struct mgsl_struct *info)
1908{
1909 unsigned cflag;
1910 int bits_per_char;
1911
1912 if (!info->port.tty || !info->port.tty->termios)
1913 return;
1914
1915 if (debug_level >= DEBUG_LEVEL_INFO)
1916 printk("%s(%d):mgsl_change_params(%s)\n",
1917 __FILE__,__LINE__, info->device_name );
1918
1919 cflag = info->port.tty->termios->c_cflag;
1920
1921 /* if B0 rate (hangup) specified then negate DTR and RTS */
1922 /* otherwise assert DTR and RTS */
1923 if (cflag & CBAUD)
1924 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1925 else
1926 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1927
1928 /* byte size and parity */
1929
1930 switch (cflag & CSIZE) {
1931 case CS5: info->params.data_bits = 5; break;
1932 case CS6: info->params.data_bits = 6; break;
1933 case CS7: info->params.data_bits = 7; break;
1934 case CS8: info->params.data_bits = 8; break;
1935 /* Never happens, but GCC is too dumb to figure it out */
1936 default: info->params.data_bits = 7; break;
1937 }
1938
1939 if (cflag & CSTOPB)
1940 info->params.stop_bits = 2;
1941 else
1942 info->params.stop_bits = 1;
1943
1944 info->params.parity = ASYNC_PARITY_NONE;
1945 if (cflag & PARENB) {
1946 if (cflag & PARODD)
1947 info->params.parity = ASYNC_PARITY_ODD;
1948 else
1949 info->params.parity = ASYNC_PARITY_EVEN;
1950#ifdef CMSPAR
1951 if (cflag & CMSPAR)
1952 info->params.parity = ASYNC_PARITY_SPACE;
1953#endif
1954 }
1955
1956 /* calculate number of jiffies to transmit a full
1957 * FIFO (32 bytes) at specified data rate
1958 */
1959 bits_per_char = info->params.data_bits +
1960 info->params.stop_bits + 1;
1961
1962 /* if port data rate is set to 460800 or less then
1963 * allow tty settings to override, otherwise keep the
1964 * current data rate.
1965 */
1966 if (info->params.data_rate <= 460800)
1967 info->params.data_rate = tty_get_baud_rate(info->port.tty);
1968
1969 if ( info->params.data_rate ) {
1970 info->timeout = (32*HZ*bits_per_char) /
1971 info->params.data_rate;
1972 }
1973 info->timeout += HZ/50; /* Add .02 seconds of slop */
1974
1975 if (cflag & CRTSCTS)
1976 info->port.flags |= ASYNC_CTS_FLOW;
1977 else
1978 info->port.flags &= ~ASYNC_CTS_FLOW;
1979
1980 if (cflag & CLOCAL)
1981 info->port.flags &= ~ASYNC_CHECK_CD;
1982 else
1983 info->port.flags |= ASYNC_CHECK_CD;
1984
1985 /* process tty input control flags */
1986
1987 info->read_status_mask = RXSTATUS_OVERRUN;
1988 if (I_INPCK(info->port.tty))
1989 info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
1990 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
1991 info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
1992
1993 if (I_IGNPAR(info->port.tty))
1994 info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
1995 if (I_IGNBRK(info->port.tty)) {
1996 info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
1997 /* If ignoring parity and break indicators, ignore
1998 * overruns too. (For real raw support).
1999 */
2000 if (I_IGNPAR(info->port.tty))
2001 info->ignore_status_mask |= RXSTATUS_OVERRUN;
2002 }
2003
2004 mgsl_program_hw(info);
2005
2006} /* end of mgsl_change_params() */
2007
2008/* mgsl_put_char()
2009 *
2010 * Add a character to the transmit buffer.
2011 *
2012 * Arguments: tty pointer to tty information structure
2013 * ch character to add to transmit buffer
2014 *
2015 * Return Value: None
2016 */
2017static int mgsl_put_char(struct tty_struct *tty, unsigned char ch)
2018{
2019 struct mgsl_struct *info = tty->driver_data;
2020 unsigned long flags;
2021 int ret = 0;
2022
2023 if (debug_level >= DEBUG_LEVEL_INFO) {
2024 printk(KERN_DEBUG "%s(%d):mgsl_put_char(%d) on %s\n",
2025 __FILE__, __LINE__, ch, info->device_name);
2026 }
2027
2028 if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
2029 return 0;
2030
2031 if (!info->xmit_buf)
2032 return 0;
2033
2034 spin_lock_irqsave(&info->irq_spinlock, flags);
2035
2036 if ((info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active) {
2037 if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
2038 info->xmit_buf[info->xmit_head++] = ch;
2039 info->xmit_head &= SERIAL_XMIT_SIZE-1;
2040 info->xmit_cnt++;
2041 ret = 1;
2042 }
2043 }
2044 spin_unlock_irqrestore(&info->irq_spinlock, flags);
2045 return ret;
2046
2047} /* end of mgsl_put_char() */
2048
2049/* mgsl_flush_chars()
2050 *
2051 * Enable transmitter so remaining characters in the
2052 * transmit buffer are sent.
2053 *
2054 * Arguments: tty pointer to tty information structure
2055 * Return Value: None
2056 */
2057static void mgsl_flush_chars(struct tty_struct *tty)
2058{
2059 struct mgsl_struct *info = tty->driver_data;
2060 unsigned long flags;
2061
2062 if ( debug_level >= DEBUG_LEVEL_INFO )
2063 printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
2064 __FILE__,__LINE__,info->device_name,info->xmit_cnt);
2065
2066 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
2067 return;
2068
2069 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2070 !info->xmit_buf)
2071 return;
2072
2073 if ( debug_level >= DEBUG_LEVEL_INFO )
2074 printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
2075 __FILE__,__LINE__,info->device_name );
2076
2077 spin_lock_irqsave(&info->irq_spinlock,flags);
2078
2079 if (!info->tx_active) {
2080 if ( (info->params.mode == MGSL_MODE_HDLC ||
2081 info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
2082 /* operating in synchronous (frame oriented) mode */
2083 /* copy data from circular xmit_buf to */
2084 /* transmit DMA buffer. */
2085 mgsl_load_tx_dma_buffer(info,
2086 info->xmit_buf,info->xmit_cnt);
2087 }
2088 usc_start_transmitter(info);
2089 }
2090
2091 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2092
2093} /* end of mgsl_flush_chars() */
2094
2095/* mgsl_write()
2096 *
2097 * Send a block of data
2098 *
2099 * Arguments:
2100 *
2101 * tty pointer to tty information structure
2102 * buf pointer to buffer containing send data
2103 * count size of send data in bytes
2104 *
2105 * Return Value: number of characters written
2106 */
2107static int mgsl_write(struct tty_struct * tty,
2108 const unsigned char *buf, int count)
2109{
2110 int c, ret = 0;
2111 struct mgsl_struct *info = tty->driver_data;
2112 unsigned long flags;
2113
2114 if ( debug_level >= DEBUG_LEVEL_INFO )
2115 printk( "%s(%d):mgsl_write(%s) count=%d\n",
2116 __FILE__,__LINE__,info->device_name,count);
2117
2118 if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
2119 goto cleanup;
2120
2121 if (!info->xmit_buf)
2122 goto cleanup;
2123
2124 if ( info->params.mode == MGSL_MODE_HDLC ||
2125 info->params.mode == MGSL_MODE_RAW ) {
2126 /* operating in synchronous (frame oriented) mode */
2127 /* operating in synchronous (frame oriented) mode */
2128 if (info->tx_active) {
2129
2130 if ( info->params.mode == MGSL_MODE_HDLC ) {
2131 ret = 0;
2132 goto cleanup;
2133 }
2134 /* transmitter is actively sending data -
2135 * if we have multiple transmit dma and
2136 * holding buffers, attempt to queue this
2137 * frame for transmission at a later time.
2138 */
2139 if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
2140 /* no tx holding buffers available */
2141 ret = 0;
2142 goto cleanup;
2143 }
2144
2145 /* queue transmit frame request */
2146 ret = count;
2147 save_tx_buffer_request(info,buf,count);
2148
2149 /* if we have sufficient tx dma buffers,
2150 * load the next buffered tx request
2151 */
2152 spin_lock_irqsave(&info->irq_spinlock,flags);
2153 load_next_tx_holding_buffer(info);
2154 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2155 goto cleanup;
2156 }
2157
2158 /* if operating in HDLC LoopMode and the adapter */
2159 /* has yet to be inserted into the loop, we can't */
2160 /* transmit */
2161
2162 if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
2163 !usc_loopmode_active(info) )
2164 {
2165 ret = 0;
2166 goto cleanup;
2167 }
2168
2169 if ( info->xmit_cnt ) {
2170 /* Send accumulated from send_char() calls */
2171 /* as frame and wait before accepting more data. */
2172 ret = 0;
2173
2174 /* copy data from circular xmit_buf to */
2175 /* transmit DMA buffer. */
2176 mgsl_load_tx_dma_buffer(info,
2177 info->xmit_buf,info->xmit_cnt);
2178 if ( debug_level >= DEBUG_LEVEL_INFO )
2179 printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
2180 __FILE__,__LINE__,info->device_name);
2181 } else {
2182 if ( debug_level >= DEBUG_LEVEL_INFO )
2183 printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
2184 __FILE__,__LINE__,info->device_name);
2185 ret = count;
2186 info->xmit_cnt = count;
2187 mgsl_load_tx_dma_buffer(info,buf,count);
2188 }
2189 } else {
2190 while (1) {
2191 spin_lock_irqsave(&info->irq_spinlock,flags);
2192 c = min_t(int, count,
2193 min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2194 SERIAL_XMIT_SIZE - info->xmit_head));
2195 if (c <= 0) {
2196 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2197 break;
2198 }
2199 memcpy(info->xmit_buf + info->xmit_head, buf, c);
2200 info->xmit_head = ((info->xmit_head + c) &
2201 (SERIAL_XMIT_SIZE-1));
2202 info->xmit_cnt += c;
2203 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2204 buf += c;
2205 count -= c;
2206 ret += c;
2207 }
2208 }
2209
2210 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2211 spin_lock_irqsave(&info->irq_spinlock,flags);
2212 if (!info->tx_active)
2213 usc_start_transmitter(info);
2214 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2215 }
2216cleanup:
2217 if ( debug_level >= DEBUG_LEVEL_INFO )
2218 printk( "%s(%d):mgsl_write(%s) returning=%d\n",
2219 __FILE__,__LINE__,info->device_name,ret);
2220
2221 return ret;
2222
2223} /* end of mgsl_write() */
2224
2225/* mgsl_write_room()
2226 *
2227 * Return the count of free bytes in transmit buffer
2228 *
2229 * Arguments: tty pointer to tty info structure
2230 * Return Value: None
2231 */
2232static int mgsl_write_room(struct tty_struct *tty)
2233{
2234 struct mgsl_struct *info = tty->driver_data;
2235 int ret;
2236
2237 if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
2238 return 0;
2239 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2240 if (ret < 0)
2241 ret = 0;
2242
2243 if (debug_level >= DEBUG_LEVEL_INFO)
2244 printk("%s(%d):mgsl_write_room(%s)=%d\n",
2245 __FILE__,__LINE__, info->device_name,ret );
2246
2247 if ( info->params.mode == MGSL_MODE_HDLC ||
2248 info->params.mode == MGSL_MODE_RAW ) {
2249 /* operating in synchronous (frame oriented) mode */
2250 if ( info->tx_active )
2251 return 0;
2252 else
2253 return HDLC_MAX_FRAME_SIZE;
2254 }
2255
2256 return ret;
2257
2258} /* end of mgsl_write_room() */
2259
2260/* mgsl_chars_in_buffer()
2261 *
2262 * Return the count of bytes in transmit buffer
2263 *
2264 * Arguments: tty pointer to tty info structure
2265 * Return Value: None
2266 */
2267static int mgsl_chars_in_buffer(struct tty_struct *tty)
2268{
2269 struct mgsl_struct *info = tty->driver_data;
2270
2271 if (debug_level >= DEBUG_LEVEL_INFO)
2272 printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
2273 __FILE__,__LINE__, info->device_name );
2274
2275 if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
2276 return 0;
2277
2278 if (debug_level >= DEBUG_LEVEL_INFO)
2279 printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
2280 __FILE__,__LINE__, info->device_name,info->xmit_cnt );
2281
2282 if ( info->params.mode == MGSL_MODE_HDLC ||
2283 info->params.mode == MGSL_MODE_RAW ) {
2284 /* operating in synchronous (frame oriented) mode */
2285 if ( info->tx_active )
2286 return info->max_frame_size;
2287 else
2288 return 0;
2289 }
2290
2291 return info->xmit_cnt;
2292} /* end of mgsl_chars_in_buffer() */
2293
2294/* mgsl_flush_buffer()
2295 *
2296 * Discard all data in the send buffer
2297 *
2298 * Arguments: tty pointer to tty info structure
2299 * Return Value: None
2300 */
2301static void mgsl_flush_buffer(struct tty_struct *tty)
2302{
2303 struct mgsl_struct *info = tty->driver_data;
2304 unsigned long flags;
2305
2306 if (debug_level >= DEBUG_LEVEL_INFO)
2307 printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
2308 __FILE__,__LINE__, info->device_name );
2309
2310 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
2311 return;
2312
2313 spin_lock_irqsave(&info->irq_spinlock,flags);
2314 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2315 del_timer(&info->tx_timer);
2316 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2317
2318 tty_wakeup(tty);
2319}
2320
2321/* mgsl_send_xchar()
2322 *
2323 * Send a high-priority XON/XOFF character
2324 *
2325 * Arguments: tty pointer to tty info structure
2326 * ch character to send
2327 * Return Value: None
2328 */
2329static void mgsl_send_xchar(struct tty_struct *tty, char ch)
2330{
2331 struct mgsl_struct *info = tty->driver_data;
2332 unsigned long flags;
2333
2334 if (debug_level >= DEBUG_LEVEL_INFO)
2335 printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
2336 __FILE__,__LINE__, info->device_name, ch );
2337
2338 if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
2339 return;
2340
2341 info->x_char = ch;
2342 if (ch) {
2343 /* Make sure transmit interrupts are on */
2344 spin_lock_irqsave(&info->irq_spinlock,flags);
2345 if (!info->tx_enabled)
2346 usc_start_transmitter(info);
2347 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2348 }
2349} /* end of mgsl_send_xchar() */
2350
2351/* mgsl_throttle()
2352 *
2353 * Signal remote device to throttle send data (our receive data)
2354 *
2355 * Arguments: tty pointer to tty info structure
2356 * Return Value: None
2357 */
2358static void mgsl_throttle(struct tty_struct * tty)
2359{
2360 struct mgsl_struct *info = tty->driver_data;
2361 unsigned long flags;
2362
2363 if (debug_level >= DEBUG_LEVEL_INFO)
2364 printk("%s(%d):mgsl_throttle(%s) entry\n",
2365 __FILE__,__LINE__, info->device_name );
2366
2367 if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
2368 return;
2369
2370 if (I_IXOFF(tty))
2371 mgsl_send_xchar(tty, STOP_CHAR(tty));
2372
2373 if (tty->termios->c_cflag & CRTSCTS) {
2374 spin_lock_irqsave(&info->irq_spinlock,flags);
2375 info->serial_signals &= ~SerialSignal_RTS;
2376 usc_set_serial_signals(info);
2377 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2378 }
2379} /* end of mgsl_throttle() */
2380
2381/* mgsl_unthrottle()
2382 *
2383 * Signal remote device to stop throttling send data (our receive data)
2384 *
2385 * Arguments: tty pointer to tty info structure
2386 * Return Value: None
2387 */
2388static void mgsl_unthrottle(struct tty_struct * tty)
2389{
2390 struct mgsl_struct *info = tty->driver_data;
2391 unsigned long flags;
2392
2393 if (debug_level >= DEBUG_LEVEL_INFO)
2394 printk("%s(%d):mgsl_unthrottle(%s) entry\n",
2395 __FILE__,__LINE__, info->device_name );
2396
2397 if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
2398 return;
2399
2400 if (I_IXOFF(tty)) {
2401 if (info->x_char)
2402 info->x_char = 0;
2403 else
2404 mgsl_send_xchar(tty, START_CHAR(tty));
2405 }
2406
2407 if (tty->termios->c_cflag & CRTSCTS) {
2408 spin_lock_irqsave(&info->irq_spinlock,flags);
2409 info->serial_signals |= SerialSignal_RTS;
2410 usc_set_serial_signals(info);
2411 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2412 }
2413
2414} /* end of mgsl_unthrottle() */
2415
2416/* mgsl_get_stats()
2417 *
2418 * get the current serial parameters information
2419 *
2420 * Arguments: info pointer to device instance data
2421 * user_icount pointer to buffer to hold returned stats
2422 *
2423 * Return Value: 0 if success, otherwise error code
2424 */
2425static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
2426{
2427 int err;
2428
2429 if (debug_level >= DEBUG_LEVEL_INFO)
2430 printk("%s(%d):mgsl_get_params(%s)\n",
2431 __FILE__,__LINE__, info->device_name);
2432
2433 if (!user_icount) {
2434 memset(&info->icount, 0, sizeof(info->icount));
2435 } else {
2436 mutex_lock(&info->port.mutex);
2437 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2438 mutex_unlock(&info->port.mutex);
2439 if (err)
2440 return -EFAULT;
2441 }
2442
2443 return 0;
2444
2445} /* end of mgsl_get_stats() */
2446
2447/* mgsl_get_params()
2448 *
2449 * get the current serial parameters information
2450 *
2451 * Arguments: info pointer to device instance data
2452 * user_params pointer to buffer to hold returned params
2453 *
2454 * Return Value: 0 if success, otherwise error code
2455 */
2456static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
2457{
2458 int err;
2459 if (debug_level >= DEBUG_LEVEL_INFO)
2460 printk("%s(%d):mgsl_get_params(%s)\n",
2461 __FILE__,__LINE__, info->device_name);
2462
2463 mutex_lock(&info->port.mutex);
2464 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2465 mutex_unlock(&info->port.mutex);
2466 if (err) {
2467 if ( debug_level >= DEBUG_LEVEL_INFO )
2468 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
2469 __FILE__,__LINE__,info->device_name);
2470 return -EFAULT;
2471 }
2472
2473 return 0;
2474
2475} /* end of mgsl_get_params() */
2476
2477/* mgsl_set_params()
2478 *
2479 * set the serial parameters
2480 *
2481 * Arguments:
2482 *
2483 * info pointer to device instance data
2484 * new_params user buffer containing new serial params
2485 *
2486 * Return Value: 0 if success, otherwise error code
2487 */
2488static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
2489{
2490 unsigned long flags;
2491 MGSL_PARAMS tmp_params;
2492 int err;
2493
2494 if (debug_level >= DEBUG_LEVEL_INFO)
2495 printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
2496 info->device_name );
2497 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2498 if (err) {
2499 if ( debug_level >= DEBUG_LEVEL_INFO )
2500 printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
2501 __FILE__,__LINE__,info->device_name);
2502 return -EFAULT;
2503 }
2504
2505 mutex_lock(&info->port.mutex);
2506 spin_lock_irqsave(&info->irq_spinlock,flags);
2507 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2508 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2509
2510 mgsl_change_params(info);
2511 mutex_unlock(&info->port.mutex);
2512
2513 return 0;
2514
2515} /* end of mgsl_set_params() */
2516
2517/* mgsl_get_txidle()
2518 *
2519 * get the current transmit idle mode
2520 *
2521 * Arguments: info pointer to device instance data
2522 * idle_mode pointer to buffer to hold returned idle mode
2523 *
2524 * Return Value: 0 if success, otherwise error code
2525 */
2526static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
2527{
2528 int err;
2529
2530 if (debug_level >= DEBUG_LEVEL_INFO)
2531 printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
2532 __FILE__,__LINE__, info->device_name, info->idle_mode);
2533
2534 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2535 if (err) {
2536 if ( debug_level >= DEBUG_LEVEL_INFO )
2537 printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
2538 __FILE__,__LINE__,info->device_name);
2539 return -EFAULT;
2540 }
2541
2542 return 0;
2543
2544} /* end of mgsl_get_txidle() */
2545
2546/* mgsl_set_txidle() service ioctl to set transmit idle mode
2547 *
2548 * Arguments: info pointer to device instance data
2549 * idle_mode new idle mode
2550 *
2551 * Return Value: 0 if success, otherwise error code
2552 */
2553static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
2554{
2555 unsigned long flags;
2556
2557 if (debug_level >= DEBUG_LEVEL_INFO)
2558 printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
2559 info->device_name, idle_mode );
2560
2561 spin_lock_irqsave(&info->irq_spinlock,flags);
2562 info->idle_mode = idle_mode;
2563 usc_set_txidle( info );
2564 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2565 return 0;
2566
2567} /* end of mgsl_set_txidle() */
2568
2569/* mgsl_txenable()
2570 *
2571 * enable or disable the transmitter
2572 *
2573 * Arguments:
2574 *
2575 * info pointer to device instance data
2576 * enable 1 = enable, 0 = disable
2577 *
2578 * Return Value: 0 if success, otherwise error code
2579 */
2580static int mgsl_txenable(struct mgsl_struct * info, int enable)
2581{
2582 unsigned long flags;
2583
2584 if (debug_level >= DEBUG_LEVEL_INFO)
2585 printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
2586 info->device_name, enable);
2587
2588 spin_lock_irqsave(&info->irq_spinlock,flags);
2589 if ( enable ) {
2590 if ( !info->tx_enabled ) {
2591
2592 usc_start_transmitter(info);
2593 /*--------------------------------------------------
2594 * if HDLC/SDLC Loop mode, attempt to insert the
2595 * station in the 'loop' by setting CMR:13. Upon
2596 * receipt of the next GoAhead (RxAbort) sequence,
2597 * the OnLoop indicator (CCSR:7) should go active
2598 * to indicate that we are on the loop
2599 *--------------------------------------------------*/
2600 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2601 usc_loopmode_insert_request( info );
2602 }
2603 } else {
2604 if ( info->tx_enabled )
2605 usc_stop_transmitter(info);
2606 }
2607 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2608 return 0;
2609
2610} /* end of mgsl_txenable() */
2611
2612/* mgsl_txabort() abort send HDLC frame
2613 *
2614 * Arguments: info pointer to device instance data
2615 * Return Value: 0 if success, otherwise error code
2616 */
2617static int mgsl_txabort(struct mgsl_struct * info)
2618{
2619 unsigned long flags;
2620
2621 if (debug_level >= DEBUG_LEVEL_INFO)
2622 printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
2623 info->device_name);
2624
2625 spin_lock_irqsave(&info->irq_spinlock,flags);
2626 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
2627 {
2628 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2629 usc_loopmode_cancel_transmit( info );
2630 else
2631 usc_TCmd(info,TCmd_SendAbort);
2632 }
2633 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2634 return 0;
2635
2636} /* end of mgsl_txabort() */
2637
2638/* mgsl_rxenable() enable or disable the receiver
2639 *
2640 * Arguments: info pointer to device instance data
2641 * enable 1 = enable, 0 = disable
2642 * Return Value: 0 if success, otherwise error code
2643 */
2644static int mgsl_rxenable(struct mgsl_struct * info, int enable)
2645{
2646 unsigned long flags;
2647
2648 if (debug_level >= DEBUG_LEVEL_INFO)
2649 printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
2650 info->device_name, enable);
2651
2652 spin_lock_irqsave(&info->irq_spinlock,flags);
2653 if ( enable ) {
2654 if ( !info->rx_enabled )
2655 usc_start_receiver(info);
2656 } else {
2657 if ( info->rx_enabled )
2658 usc_stop_receiver(info);
2659 }
2660 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2661 return 0;
2662
2663} /* end of mgsl_rxenable() */
2664
2665/* mgsl_wait_event() wait for specified event to occur
2666 *
2667 * Arguments: info pointer to device instance data
2668 * mask pointer to bitmask of events to wait for
2669 * Return Value: 0 if successful and bit mask updated with
2670 * of events triggerred,
2671 * otherwise error code
2672 */
2673static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2674{
2675 unsigned long flags;
2676 int s;
2677 int rc=0;
2678 struct mgsl_icount cprev, cnow;
2679 int events;
2680 int mask;
2681 struct _input_signal_events oldsigs, newsigs;
2682 DECLARE_WAITQUEUE(wait, current);
2683
2684 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2685 if (rc) {
2686 return -EFAULT;
2687 }
2688
2689 if (debug_level >= DEBUG_LEVEL_INFO)
2690 printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
2691 info->device_name, mask);
2692
2693 spin_lock_irqsave(&info->irq_spinlock,flags);
2694
2695 /* return immediately if state matches requested events */
2696 usc_get_serial_signals(info);
2697 s = info->serial_signals;
2698 events = mask &
2699 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2700 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2701 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2702 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2703 if (events) {
2704 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2705 goto exit;
2706 }
2707
2708 /* save current irq counts */
2709 cprev = info->icount;
2710 oldsigs = info->input_signal_events;
2711
2712 /* enable hunt and idle irqs if needed */
2713 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2714 u16 oldreg = usc_InReg(info,RICR);
2715 u16 newreg = oldreg +
2716 (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
2717 (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
2718 if (oldreg != newreg)
2719 usc_OutReg(info, RICR, newreg);
2720 }
2721
2722 set_current_state(TASK_INTERRUPTIBLE);
2723 add_wait_queue(&info->event_wait_q, &wait);
2724
2725 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2726
2727
2728 for(;;) {
2729 schedule();
2730 if (signal_pending(current)) {
2731 rc = -ERESTARTSYS;
2732 break;
2733 }
2734
2735 /* get current irq counts */
2736 spin_lock_irqsave(&info->irq_spinlock,flags);
2737 cnow = info->icount;
2738 newsigs = info->input_signal_events;
2739 set_current_state(TASK_INTERRUPTIBLE);
2740 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2741
2742 /* if no change, wait aborted for some reason */
2743 if (newsigs.dsr_up == oldsigs.dsr_up &&
2744 newsigs.dsr_down == oldsigs.dsr_down &&
2745 newsigs.dcd_up == oldsigs.dcd_up &&
2746 newsigs.dcd_down == oldsigs.dcd_down &&
2747 newsigs.cts_up == oldsigs.cts_up &&
2748 newsigs.cts_down == oldsigs.cts_down &&
2749 newsigs.ri_up == oldsigs.ri_up &&
2750 newsigs.ri_down == oldsigs.ri_down &&
2751 cnow.exithunt == cprev.exithunt &&
2752 cnow.rxidle == cprev.rxidle) {
2753 rc = -EIO;
2754 break;
2755 }
2756
2757 events = mask &
2758 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2759 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2760 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2761 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2762 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2763 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2764 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2765 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2766 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2767 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2768 if (events)
2769 break;
2770
2771 cprev = cnow;
2772 oldsigs = newsigs;
2773 }
2774
2775 remove_wait_queue(&info->event_wait_q, &wait);
2776 set_current_state(TASK_RUNNING);
2777
2778 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2779 spin_lock_irqsave(&info->irq_spinlock,flags);
2780 if (!waitqueue_active(&info->event_wait_q)) {
2781 /* disable enable exit hunt mode/idle rcvd IRQs */
2782 usc_OutReg(info, RICR, usc_InReg(info,RICR) &
2783 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
2784 }
2785 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2786 }
2787exit:
2788 if ( rc == 0 )
2789 PUT_USER(rc, events, mask_ptr);
2790
2791 return rc;
2792
2793} /* end of mgsl_wait_event() */
2794
2795static int modem_input_wait(struct mgsl_struct *info,int arg)
2796{
2797 unsigned long flags;
2798 int rc;
2799 struct mgsl_icount cprev, cnow;
2800 DECLARE_WAITQUEUE(wait, current);
2801
2802 /* save current irq counts */
2803 spin_lock_irqsave(&info->irq_spinlock,flags);
2804 cprev = info->icount;
2805 add_wait_queue(&info->status_event_wait_q, &wait);
2806 set_current_state(TASK_INTERRUPTIBLE);
2807 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2808
2809 for(;;) {
2810 schedule();
2811 if (signal_pending(current)) {
2812 rc = -ERESTARTSYS;
2813 break;
2814 }
2815
2816 /* get new irq counts */
2817 spin_lock_irqsave(&info->irq_spinlock,flags);
2818 cnow = info->icount;
2819 set_current_state(TASK_INTERRUPTIBLE);
2820 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2821
2822 /* if no change, wait aborted for some reason */
2823 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2824 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2825 rc = -EIO;
2826 break;
2827 }
2828
2829 /* check for change in caller specified modem input */
2830 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2831 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2832 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2833 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2834 rc = 0;
2835 break;
2836 }
2837
2838 cprev = cnow;
2839 }
2840 remove_wait_queue(&info->status_event_wait_q, &wait);
2841 set_current_state(TASK_RUNNING);
2842 return rc;
2843}
2844
2845/* return the state of the serial control and status signals
2846 */
2847static int tiocmget(struct tty_struct *tty)
2848{
2849 struct mgsl_struct *info = tty->driver_data;
2850 unsigned int result;
2851 unsigned long flags;
2852
2853 spin_lock_irqsave(&info->irq_spinlock,flags);
2854 usc_get_serial_signals(info);
2855 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2856
2857 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2858 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2859 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2860 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2861 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2862 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2863
2864 if (debug_level >= DEBUG_LEVEL_INFO)
2865 printk("%s(%d):%s tiocmget() value=%08X\n",
2866 __FILE__,__LINE__, info->device_name, result );
2867 return result;
2868}
2869
2870/* set modem control signals (DTR/RTS)
2871 */
2872static int tiocmset(struct tty_struct *tty,
2873 unsigned int set, unsigned int clear)
2874{
2875 struct mgsl_struct *info = tty->driver_data;
2876 unsigned long flags;
2877
2878 if (debug_level >= DEBUG_LEVEL_INFO)
2879 printk("%s(%d):%s tiocmset(%x,%x)\n",
2880 __FILE__,__LINE__,info->device_name, set, clear);
2881
2882 if (set & TIOCM_RTS)
2883 info->serial_signals |= SerialSignal_RTS;
2884 if (set & TIOCM_DTR)
2885 info->serial_signals |= SerialSignal_DTR;
2886 if (clear & TIOCM_RTS)
2887 info->serial_signals &= ~SerialSignal_RTS;
2888 if (clear & TIOCM_DTR)
2889 info->serial_signals &= ~SerialSignal_DTR;
2890
2891 spin_lock_irqsave(&info->irq_spinlock,flags);
2892 usc_set_serial_signals(info);
2893 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2894
2895 return 0;
2896}
2897
2898/* mgsl_break() Set or clear transmit break condition
2899 *
2900 * Arguments: tty pointer to tty instance data
2901 * break_state -1=set break condition, 0=clear
2902 * Return Value: error code
2903 */
2904static int mgsl_break(struct tty_struct *tty, int break_state)
2905{
2906 struct mgsl_struct * info = tty->driver_data;
2907 unsigned long flags;
2908
2909 if (debug_level >= DEBUG_LEVEL_INFO)
2910 printk("%s(%d):mgsl_break(%s,%d)\n",
2911 __FILE__,__LINE__, info->device_name, break_state);
2912
2913 if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
2914 return -EINVAL;
2915
2916 spin_lock_irqsave(&info->irq_spinlock,flags);
2917 if (break_state == -1)
2918 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
2919 else
2920 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
2921 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2922 return 0;
2923
2924} /* end of mgsl_break() */
2925
2926/*
2927 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2928 * Return: write counters to the user passed counter struct
2929 * NB: both 1->0 and 0->1 transitions are counted except for
2930 * RI where only 0->1 is counted.
2931 */
2932static int msgl_get_icount(struct tty_struct *tty,
2933 struct serial_icounter_struct *icount)
2934
2935{
2936 struct mgsl_struct * info = tty->driver_data;
2937 struct mgsl_icount cnow; /* kernel counter temps */
2938 unsigned long flags;
2939
2940 spin_lock_irqsave(&info->irq_spinlock,flags);
2941 cnow = info->icount;
2942 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2943
2944 icount->cts = cnow.cts;
2945 icount->dsr = cnow.dsr;
2946 icount->rng = cnow.rng;
2947 icount->dcd = cnow.dcd;
2948 icount->rx = cnow.rx;
2949 icount->tx = cnow.tx;
2950 icount->frame = cnow.frame;
2951 icount->overrun = cnow.overrun;
2952 icount->parity = cnow.parity;
2953 icount->brk = cnow.brk;
2954 icount->buf_overrun = cnow.buf_overrun;
2955 return 0;
2956}
2957
2958/* mgsl_ioctl() Service an IOCTL request
2959 *
2960 * Arguments:
2961 *
2962 * tty pointer to tty instance data
2963 * cmd IOCTL command code
2964 * arg command argument/context
2965 *
2966 * Return Value: 0 if success, otherwise error code
2967 */
2968static int mgsl_ioctl(struct tty_struct *tty,
2969 unsigned int cmd, unsigned long arg)
2970{
2971 struct mgsl_struct * info = tty->driver_data;
2972
2973 if (debug_level >= DEBUG_LEVEL_INFO)
2974 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2975 info->device_name, cmd );
2976
2977 if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
2978 return -ENODEV;
2979
2980 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2981 (cmd != TIOCMIWAIT)) {
2982 if (tty->flags & (1 << TTY_IO_ERROR))
2983 return -EIO;
2984 }
2985
2986 return mgsl_ioctl_common(info, cmd, arg);
2987}
2988
2989static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
2990{
2991 void __user *argp = (void __user *)arg;
2992
2993 switch (cmd) {
2994 case MGSL_IOCGPARAMS:
2995 return mgsl_get_params(info, argp);
2996 case MGSL_IOCSPARAMS:
2997 return mgsl_set_params(info, argp);
2998 case MGSL_IOCGTXIDLE:
2999 return mgsl_get_txidle(info, argp);
3000 case MGSL_IOCSTXIDLE:
3001 return mgsl_set_txidle(info,(int)arg);
3002 case MGSL_IOCTXENABLE:
3003 return mgsl_txenable(info,(int)arg);
3004 case MGSL_IOCRXENABLE:
3005 return mgsl_rxenable(info,(int)arg);
3006 case MGSL_IOCTXABORT:
3007 return mgsl_txabort(info);
3008 case MGSL_IOCGSTATS:
3009 return mgsl_get_stats(info, argp);
3010 case MGSL_IOCWAITEVENT:
3011 return mgsl_wait_event(info, argp);
3012 case MGSL_IOCLOOPTXDONE:
3013 return mgsl_loopmode_send_done(info);
3014 /* Wait for modem input (DCD,RI,DSR,CTS) change
3015 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
3016 */
3017 case TIOCMIWAIT:
3018 return modem_input_wait(info,(int)arg);
3019
3020 default:
3021 return -ENOIOCTLCMD;
3022 }
3023 return 0;
3024}
3025
3026/* mgsl_set_termios()
3027 *
3028 * Set new termios settings
3029 *
3030 * Arguments:
3031 *
3032 * tty pointer to tty structure
3033 * termios pointer to buffer to hold returned old termios
3034 *
3035 * Return Value: None
3036 */
3037static void mgsl_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3038{
3039 struct mgsl_struct *info = tty->driver_data;
3040 unsigned long flags;
3041
3042 if (debug_level >= DEBUG_LEVEL_INFO)
3043 printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
3044 tty->driver->name );
3045
3046 mgsl_change_params(info);
3047
3048 /* Handle transition to B0 status */
3049 if (old_termios->c_cflag & CBAUD &&
3050 !(tty->termios->c_cflag & CBAUD)) {
3051 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3052 spin_lock_irqsave(&info->irq_spinlock,flags);
3053 usc_set_serial_signals(info);
3054 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3055 }
3056
3057 /* Handle transition away from B0 status */
3058 if (!(old_termios->c_cflag & CBAUD) &&
3059 tty->termios->c_cflag & CBAUD) {
3060 info->serial_signals |= SerialSignal_DTR;
3061 if (!(tty->termios->c_cflag & CRTSCTS) ||
3062 !test_bit(TTY_THROTTLED, &tty->flags)) {
3063 info->serial_signals |= SerialSignal_RTS;
3064 }
3065 spin_lock_irqsave(&info->irq_spinlock,flags);
3066 usc_set_serial_signals(info);
3067 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3068 }
3069
3070 /* Handle turning off CRTSCTS */
3071 if (old_termios->c_cflag & CRTSCTS &&
3072 !(tty->termios->c_cflag & CRTSCTS)) {
3073 tty->hw_stopped = 0;
3074 mgsl_start(tty);
3075 }
3076
3077} /* end of mgsl_set_termios() */
3078
3079/* mgsl_close()
3080 *
3081 * Called when port is closed. Wait for remaining data to be
3082 * sent. Disable port and free resources.
3083 *
3084 * Arguments:
3085 *
3086 * tty pointer to open tty structure
3087 * filp pointer to open file object
3088 *
3089 * Return Value: None
3090 */
3091static void mgsl_close(struct tty_struct *tty, struct file * filp)
3092{
3093 struct mgsl_struct * info = tty->driver_data;
3094
3095 if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
3096 return;
3097
3098 if (debug_level >= DEBUG_LEVEL_INFO)
3099 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3100 __FILE__,__LINE__, info->device_name, info->port.count);
3101
3102 if (tty_port_close_start(&info->port, tty, filp) == 0)
3103 goto cleanup;
3104
3105 mutex_lock(&info->port.mutex);
3106 if (info->port.flags & ASYNC_INITIALIZED)
3107 mgsl_wait_until_sent(tty, info->timeout);
3108 mgsl_flush_buffer(tty);
3109 tty_ldisc_flush(tty);
3110 shutdown(info);
3111 mutex_unlock(&info->port.mutex);
3112
3113 tty_port_close_end(&info->port, tty);
3114 info->port.tty = NULL;
3115cleanup:
3116 if (debug_level >= DEBUG_LEVEL_INFO)
3117 printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
3118 tty->driver->name, info->port.count);
3119
3120} /* end of mgsl_close() */
3121
3122/* mgsl_wait_until_sent()
3123 *
3124 * Wait until the transmitter is empty.
3125 *
3126 * Arguments:
3127 *
3128 * tty pointer to tty info structure
3129 * timeout time to wait for send completion
3130 *
3131 * Return Value: None
3132 */
3133static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3134{
3135 struct mgsl_struct * info = tty->driver_data;
3136 unsigned long orig_jiffies, char_time;
3137
3138 if (!info )
3139 return;
3140
3141 if (debug_level >= DEBUG_LEVEL_INFO)
3142 printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
3143 __FILE__,__LINE__, info->device_name );
3144
3145 if (mgsl_paranoia_check(info, tty->name, "mgsl_wait_until_sent"))
3146 return;
3147
3148 if (!(info->port.flags & ASYNC_INITIALIZED))
3149 goto exit;
3150
3151 orig_jiffies = jiffies;
3152
3153 /* Set check interval to 1/5 of estimated time to
3154 * send a character, and make it at least 1. The check
3155 * interval should also be less than the timeout.
3156 * Note: use tight timings here to satisfy the NIST-PCTS.
3157 */
3158
3159 if ( info->params.data_rate ) {
3160 char_time = info->timeout/(32 * 5);
3161 if (!char_time)
3162 char_time++;
3163 } else
3164 char_time = 1;
3165
3166 if (timeout)
3167 char_time = min_t(unsigned long, char_time, timeout);
3168
3169 if ( info->params.mode == MGSL_MODE_HDLC ||
3170 info->params.mode == MGSL_MODE_RAW ) {
3171 while (info->tx_active) {
3172 msleep_interruptible(jiffies_to_msecs(char_time));
3173 if (signal_pending(current))
3174 break;
3175 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3176 break;
3177 }
3178 } else {
3179 while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
3180 info->tx_enabled) {
3181 msleep_interruptible(jiffies_to_msecs(char_time));
3182 if (signal_pending(current))
3183 break;
3184 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3185 break;
3186 }
3187 }
3188
3189exit:
3190 if (debug_level >= DEBUG_LEVEL_INFO)
3191 printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
3192 __FILE__,__LINE__, info->device_name );
3193
3194} /* end of mgsl_wait_until_sent() */
3195
3196/* mgsl_hangup()
3197 *
3198 * Called by tty_hangup() when a hangup is signaled.
3199 * This is the same as to closing all open files for the port.
3200 *
3201 * Arguments: tty pointer to associated tty object
3202 * Return Value: None
3203 */
3204static void mgsl_hangup(struct tty_struct *tty)
3205{
3206 struct mgsl_struct * info = tty->driver_data;
3207
3208 if (debug_level >= DEBUG_LEVEL_INFO)
3209 printk("%s(%d):mgsl_hangup(%s)\n",
3210 __FILE__,__LINE__, info->device_name );
3211
3212 if (mgsl_paranoia_check(info, tty->name, "mgsl_hangup"))
3213 return;
3214
3215 mgsl_flush_buffer(tty);
3216 shutdown(info);
3217
3218 info->port.count = 0;
3219 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
3220 info->port.tty = NULL;
3221
3222 wake_up_interruptible(&info->port.open_wait);
3223
3224} /* end of mgsl_hangup() */
3225
3226/*
3227 * carrier_raised()
3228 *
3229 * Return true if carrier is raised
3230 */
3231
3232static int carrier_raised(struct tty_port *port)
3233{
3234 unsigned long flags;
3235 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3236
3237 spin_lock_irqsave(&info->irq_spinlock, flags);
3238 usc_get_serial_signals(info);
3239 spin_unlock_irqrestore(&info->irq_spinlock, flags);
3240 return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
3241}
3242
3243static void dtr_rts(struct tty_port *port, int on)
3244{
3245 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3246 unsigned long flags;
3247
3248 spin_lock_irqsave(&info->irq_spinlock,flags);
3249 if (on)
3250 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3251 else
3252 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3253 usc_set_serial_signals(info);
3254 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3255}
3256
3257
3258/* block_til_ready()
3259 *
3260 * Block the current process until the specified port
3261 * is ready to be opened.
3262 *
3263 * Arguments:
3264 *
3265 * tty pointer to tty info structure
3266 * filp pointer to open file object
3267 * info pointer to device instance data
3268 *
3269 * Return Value: 0 if success, otherwise error code
3270 */
3271static int block_til_ready(struct tty_struct *tty, struct file * filp,
3272 struct mgsl_struct *info)
3273{
3274 DECLARE_WAITQUEUE(wait, current);
3275 int retval;
3276 bool do_clocal = false;
3277 bool extra_count = false;
3278 unsigned long flags;
3279 int dcd;
3280 struct tty_port *port = &info->port;
3281
3282 if (debug_level >= DEBUG_LEVEL_INFO)
3283 printk("%s(%d):block_til_ready on %s\n",
3284 __FILE__,__LINE__, tty->driver->name );
3285
3286 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3287 /* nonblock mode is set or port is not enabled */
3288 port->flags |= ASYNC_NORMAL_ACTIVE;
3289 return 0;
3290 }
3291
3292 if (tty->termios->c_cflag & CLOCAL)
3293 do_clocal = true;
3294
3295 /* Wait for carrier detect and the line to become
3296 * free (i.e., not in use by the callout). While we are in
3297 * this loop, port->count is dropped by one, so that
3298 * mgsl_close() knows when to free things. We restore it upon
3299 * exit, either normal or abnormal.
3300 */
3301
3302 retval = 0;
3303 add_wait_queue(&port->open_wait, &wait);
3304
3305 if (debug_level >= DEBUG_LEVEL_INFO)
3306 printk("%s(%d):block_til_ready before block on %s count=%d\n",
3307 __FILE__,__LINE__, tty->driver->name, port->count );
3308
3309 spin_lock_irqsave(&info->irq_spinlock, flags);
3310 if (!tty_hung_up_p(filp)) {
3311 extra_count = true;
3312 port->count--;
3313 }
3314 spin_unlock_irqrestore(&info->irq_spinlock, flags);
3315 port->blocked_open++;
3316
3317 while (1) {
3318 if (tty->termios->c_cflag & CBAUD)
3319 tty_port_raise_dtr_rts(port);
3320
3321 set_current_state(TASK_INTERRUPTIBLE);
3322
3323 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3324 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3325 -EAGAIN : -ERESTARTSYS;
3326 break;
3327 }
3328
3329 dcd = tty_port_carrier_raised(&info->port);
3330
3331 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || dcd))
3332 break;
3333
3334 if (signal_pending(current)) {
3335 retval = -ERESTARTSYS;
3336 break;
3337 }
3338
3339 if (debug_level >= DEBUG_LEVEL_INFO)
3340 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
3341 __FILE__,__LINE__, tty->driver->name, port->count );
3342
3343 tty_unlock();
3344 schedule();
3345 tty_lock();
3346 }
3347
3348 set_current_state(TASK_RUNNING);
3349 remove_wait_queue(&port->open_wait, &wait);
3350
3351 /* FIXME: Racy on hangup during close wait */
3352 if (extra_count)
3353 port->count++;
3354 port->blocked_open--;
3355
3356 if (debug_level >= DEBUG_LEVEL_INFO)
3357 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
3358 __FILE__,__LINE__, tty->driver->name, port->count );
3359
3360 if (!retval)
3361 port->flags |= ASYNC_NORMAL_ACTIVE;
3362
3363 return retval;
3364
3365} /* end of block_til_ready() */
3366
3367/* mgsl_open()
3368 *
3369 * Called when a port is opened. Init and enable port.
3370 * Perform serial-specific initialization for the tty structure.
3371 *
3372 * Arguments: tty pointer to tty info structure
3373 * filp associated file pointer
3374 *
3375 * Return Value: 0 if success, otherwise error code
3376 */
3377static int mgsl_open(struct tty_struct *tty, struct file * filp)
3378{
3379 struct mgsl_struct *info;
3380 int retval, line;
3381 unsigned long flags;
3382
3383 /* verify range of specified line number */
3384 line = tty->index;
3385 if ((line < 0) || (line >= mgsl_device_count)) {
3386 printk("%s(%d):mgsl_open with invalid line #%d.\n",
3387 __FILE__,__LINE__,line);
3388 return -ENODEV;
3389 }
3390
3391 /* find the info structure for the specified line */
3392 info = mgsl_device_list;
3393 while(info && info->line != line)
3394 info = info->next_device;
3395 if (mgsl_paranoia_check(info, tty->name, "mgsl_open"))
3396 return -ENODEV;
3397
3398 tty->driver_data = info;
3399 info->port.tty = tty;
3400
3401 if (debug_level >= DEBUG_LEVEL_INFO)
3402 printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
3403 __FILE__,__LINE__,tty->driver->name, info->port.count);
3404
3405 /* If port is closing, signal caller to try again */
3406 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
3407 if (info->port.flags & ASYNC_CLOSING)
3408 interruptible_sleep_on(&info->port.close_wait);
3409 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
3410 -EAGAIN : -ERESTARTSYS);
3411 goto cleanup;
3412 }
3413
3414 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3415
3416 spin_lock_irqsave(&info->netlock, flags);
3417 if (info->netcount) {
3418 retval = -EBUSY;
3419 spin_unlock_irqrestore(&info->netlock, flags);
3420 goto cleanup;
3421 }
3422 info->port.count++;
3423 spin_unlock_irqrestore(&info->netlock, flags);
3424
3425 if (info->port.count == 1) {
3426 /* 1st open on this device, init hardware */
3427 retval = startup(info);
3428 if (retval < 0)
3429 goto cleanup;
3430 }
3431
3432 retval = block_til_ready(tty, filp, info);
3433 if (retval) {
3434 if (debug_level >= DEBUG_LEVEL_INFO)
3435 printk("%s(%d):block_til_ready(%s) returned %d\n",
3436 __FILE__,__LINE__, info->device_name, retval);
3437 goto cleanup;
3438 }
3439
3440 if (debug_level >= DEBUG_LEVEL_INFO)
3441 printk("%s(%d):mgsl_open(%s) success\n",
3442 __FILE__,__LINE__, info->device_name);
3443 retval = 0;
3444
3445cleanup:
3446 if (retval) {
3447 if (tty->count == 1)
3448 info->port.tty = NULL; /* tty layer will release tty struct */
3449 if(info->port.count)
3450 info->port.count--;
3451 }
3452
3453 return retval;
3454
3455} /* end of mgsl_open() */
3456
3457/*
3458 * /proc fs routines....
3459 */
3460
3461static inline void line_info(struct seq_file *m, struct mgsl_struct *info)
3462{
3463 char stat_buf[30];
3464 unsigned long flags;
3465
3466 if (info->bus_type == MGSL_BUS_TYPE_PCI) {
3467 seq_printf(m, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
3468 info->device_name, info->io_base, info->irq_level,
3469 info->phys_memory_base, info->phys_lcr_base);
3470 } else {
3471 seq_printf(m, "%s:(E)ISA io:%04X irq:%d dma:%d",
3472 info->device_name, info->io_base,
3473 info->irq_level, info->dma_level);
3474 }
3475
3476 /* output current serial signal states */
3477 spin_lock_irqsave(&info->irq_spinlock,flags);
3478 usc_get_serial_signals(info);
3479 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3480
3481 stat_buf[0] = 0;
3482 stat_buf[1] = 0;
3483 if (info->serial_signals & SerialSignal_RTS)
3484 strcat(stat_buf, "|RTS");
3485 if (info->serial_signals & SerialSignal_CTS)
3486 strcat(stat_buf, "|CTS");
3487 if (info->serial_signals & SerialSignal_DTR)
3488 strcat(stat_buf, "|DTR");
3489 if (info->serial_signals & SerialSignal_DSR)
3490 strcat(stat_buf, "|DSR");
3491 if (info->serial_signals & SerialSignal_DCD)
3492 strcat(stat_buf, "|CD");
3493 if (info->serial_signals & SerialSignal_RI)
3494 strcat(stat_buf, "|RI");
3495
3496 if (info->params.mode == MGSL_MODE_HDLC ||
3497 info->params.mode == MGSL_MODE_RAW ) {
3498 seq_printf(m, " HDLC txok:%d rxok:%d",
3499 info->icount.txok, info->icount.rxok);
3500 if (info->icount.txunder)
3501 seq_printf(m, " txunder:%d", info->icount.txunder);
3502 if (info->icount.txabort)
3503 seq_printf(m, " txabort:%d", info->icount.txabort);
3504 if (info->icount.rxshort)
3505 seq_printf(m, " rxshort:%d", info->icount.rxshort);
3506 if (info->icount.rxlong)
3507 seq_printf(m, " rxlong:%d", info->icount.rxlong);
3508 if (info->icount.rxover)
3509 seq_printf(m, " rxover:%d", info->icount.rxover);
3510 if (info->icount.rxcrc)
3511 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
3512 } else {
3513 seq_printf(m, " ASYNC tx:%d rx:%d",
3514 info->icount.tx, info->icount.rx);
3515 if (info->icount.frame)
3516 seq_printf(m, " fe:%d", info->icount.frame);
3517 if (info->icount.parity)
3518 seq_printf(m, " pe:%d", info->icount.parity);
3519 if (info->icount.brk)
3520 seq_printf(m, " brk:%d", info->icount.brk);
3521 if (info->icount.overrun)
3522 seq_printf(m, " oe:%d", info->icount.overrun);
3523 }
3524
3525 /* Append serial signal status to end */
3526 seq_printf(m, " %s\n", stat_buf+1);
3527
3528 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
3529 info->tx_active,info->bh_requested,info->bh_running,
3530 info->pending_bh);
3531
3532 spin_lock_irqsave(&info->irq_spinlock,flags);
3533 {
3534 u16 Tcsr = usc_InReg( info, TCSR );
3535 u16 Tdmr = usc_InDmaReg( info, TDMR );
3536 u16 Ticr = usc_InReg( info, TICR );
3537 u16 Rscr = usc_InReg( info, RCSR );
3538 u16 Rdmr = usc_InDmaReg( info, RDMR );
3539 u16 Ricr = usc_InReg( info, RICR );
3540 u16 Icr = usc_InReg( info, ICR );
3541 u16 Dccr = usc_InReg( info, DCCR );
3542 u16 Tmr = usc_InReg( info, TMR );
3543 u16 Tccr = usc_InReg( info, TCCR );
3544 u16 Ccar = inw( info->io_base + CCAR );
3545 seq_printf(m, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
3546 "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
3547 Tcsr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
3548 }
3549 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3550}
3551
3552/* Called to print information about devices */
3553static int mgsl_proc_show(struct seq_file *m, void *v)
3554{
3555 struct mgsl_struct *info;
3556
3557 seq_printf(m, "synclink driver:%s\n", driver_version);
3558
3559 info = mgsl_device_list;
3560 while( info ) {
3561 line_info(m, info);
3562 info = info->next_device;
3563 }
3564 return 0;
3565}
3566
3567static int mgsl_proc_open(struct inode *inode, struct file *file)
3568{
3569 return single_open(file, mgsl_proc_show, NULL);
3570}
3571
3572static const struct file_operations mgsl_proc_fops = {
3573 .owner = THIS_MODULE,
3574 .open = mgsl_proc_open,
3575 .read = seq_read,
3576 .llseek = seq_lseek,
3577 .release = single_release,
3578};
3579
3580/* mgsl_allocate_dma_buffers()
3581 *
3582 * Allocate and format DMA buffers (ISA adapter)
3583 * or format shared memory buffers (PCI adapter).
3584 *
3585 * Arguments: info pointer to device instance data
3586 * Return Value: 0 if success, otherwise error
3587 */
3588static int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
3589{
3590 unsigned short BuffersPerFrame;
3591
3592 info->last_mem_alloc = 0;
3593
3594 /* Calculate the number of DMA buffers necessary to hold the */
3595 /* largest allowable frame size. Note: If the max frame size is */
3596 /* not an even multiple of the DMA buffer size then we need to */
3597 /* round the buffer count per frame up one. */
3598
3599 BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
3600 if ( info->max_frame_size % DMABUFFERSIZE )
3601 BuffersPerFrame++;
3602
3603 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3604 /*
3605 * The PCI adapter has 256KBytes of shared memory to use.
3606 * This is 64 PAGE_SIZE buffers.
3607 *
3608 * The first page is used for padding at this time so the
3609 * buffer list does not begin at offset 0 of the PCI
3610 * adapter's shared memory.
3611 *
3612 * The 2nd page is used for the buffer list. A 4K buffer
3613 * list can hold 128 DMA_BUFFER structures at 32 bytes
3614 * each.
3615 *
3616 * This leaves 62 4K pages.
3617 *
3618 * The next N pages are used for transmit frame(s). We
3619 * reserve enough 4K page blocks to hold the required
3620 * number of transmit dma buffers (num_tx_dma_buffers),
3621 * each of MaxFrameSize size.
3622 *
3623 * Of the remaining pages (62-N), determine how many can
3624 * be used to receive full MaxFrameSize inbound frames
3625 */
3626 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3627 info->rx_buffer_count = 62 - info->tx_buffer_count;
3628 } else {
3629 /* Calculate the number of PAGE_SIZE buffers needed for */
3630 /* receive and transmit DMA buffers. */
3631
3632
3633 /* Calculate the number of DMA buffers necessary to */
3634 /* hold 7 max size receive frames and one max size transmit frame. */
3635 /* The receive buffer count is bumped by one so we avoid an */
3636 /* End of List condition if all receive buffers are used when */
3637 /* using linked list DMA buffers. */
3638
3639 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3640 info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
3641
3642 /*
3643 * limit total TxBuffers & RxBuffers to 62 4K total
3644 * (ala PCI Allocation)
3645 */
3646
3647 if ( (info->tx_buffer_count + info->rx_buffer_count) > 62 )
3648 info->rx_buffer_count = 62 - info->tx_buffer_count;
3649
3650 }
3651
3652 if ( debug_level >= DEBUG_LEVEL_INFO )
3653 printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
3654 __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
3655
3656 if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
3657 mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
3658 mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0 ||
3659 mgsl_alloc_intermediate_rxbuffer_memory(info) < 0 ||
3660 mgsl_alloc_intermediate_txbuffer_memory(info) < 0 ) {
3661 printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
3662 return -ENOMEM;
3663 }
3664
3665 mgsl_reset_rx_dma_buffers( info );
3666 mgsl_reset_tx_dma_buffers( info );
3667
3668 return 0;
3669
3670} /* end of mgsl_allocate_dma_buffers() */
3671
3672/*
3673 * mgsl_alloc_buffer_list_memory()
3674 *
3675 * Allocate a common DMA buffer for use as the
3676 * receive and transmit buffer lists.
3677 *
3678 * A buffer list is a set of buffer entries where each entry contains
3679 * a pointer to an actual buffer and a pointer to the next buffer entry
3680 * (plus some other info about the buffer).
3681 *
3682 * The buffer entries for a list are built to form a circular list so
3683 * that when the entire list has been traversed you start back at the
3684 * beginning.
3685 *
3686 * This function allocates memory for just the buffer entries.
3687 * The links (pointer to next entry) are filled in with the physical
3688 * address of the next entry so the adapter can navigate the list
3689 * using bus master DMA. The pointers to the actual buffers are filled
3690 * out later when the actual buffers are allocated.
3691 *
3692 * Arguments: info pointer to device instance data
3693 * Return Value: 0 if success, otherwise error
3694 */
3695static int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
3696{
3697 unsigned int i;
3698
3699 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3700 /* PCI adapter uses shared memory. */
3701 info->buffer_list = info->memory_base + info->last_mem_alloc;
3702 info->buffer_list_phys = info->last_mem_alloc;
3703 info->last_mem_alloc += BUFFERLISTSIZE;
3704 } else {
3705 /* ISA adapter uses system memory. */
3706 /* The buffer lists are allocated as a common buffer that both */
3707 /* the processor and adapter can access. This allows the driver to */
3708 /* inspect portions of the buffer while other portions are being */
3709 /* updated by the adapter using Bus Master DMA. */
3710
3711 info->buffer_list = dma_alloc_coherent(NULL, BUFFERLISTSIZE, &info->buffer_list_dma_addr, GFP_KERNEL);
3712 if (info->buffer_list == NULL)
3713 return -ENOMEM;
3714 info->buffer_list_phys = (u32)(info->buffer_list_dma_addr);
3715 }
3716
3717 /* We got the memory for the buffer entry lists. */
3718 /* Initialize the memory block to all zeros. */
3719 memset( info->buffer_list, 0, BUFFERLISTSIZE );
3720
3721 /* Save virtual address pointers to the receive and */
3722 /* transmit buffer lists. (Receive 1st). These pointers will */
3723 /* be used by the processor to access the lists. */
3724 info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3725 info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3726 info->tx_buffer_list += info->rx_buffer_count;
3727
3728 /*
3729 * Build the links for the buffer entry lists such that
3730 * two circular lists are built. (Transmit and Receive).
3731 *
3732 * Note: the links are physical addresses
3733 * which are read by the adapter to determine the next
3734 * buffer entry to use.
3735 */
3736
3737 for ( i = 0; i < info->rx_buffer_count; i++ ) {
3738 /* calculate and store physical address of this buffer entry */
3739 info->rx_buffer_list[i].phys_entry =
3740 info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
3741
3742 /* calculate and store physical address of */
3743 /* next entry in cirular list of entries */
3744
3745 info->rx_buffer_list[i].link = info->buffer_list_phys;
3746
3747 if ( i < info->rx_buffer_count - 1 )
3748 info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3749 }
3750
3751 for ( i = 0; i < info->tx_buffer_count; i++ ) {
3752 /* calculate and store physical address of this buffer entry */
3753 info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
3754 ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
3755
3756 /* calculate and store physical address of */
3757 /* next entry in cirular list of entries */
3758
3759 info->tx_buffer_list[i].link = info->buffer_list_phys +
3760 info->rx_buffer_count * sizeof(DMABUFFERENTRY);
3761
3762 if ( i < info->tx_buffer_count - 1 )
3763 info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3764 }
3765
3766 return 0;
3767
3768} /* end of mgsl_alloc_buffer_list_memory() */
3769
3770/* Free DMA buffers allocated for use as the
3771 * receive and transmit buffer lists.
3772 * Warning:
3773 *
3774 * The data transfer buffers associated with the buffer list
3775 * MUST be freed before freeing the buffer list itself because
3776 * the buffer list contains the information necessary to free
3777 * the individual buffers!
3778 */
3779static void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
3780{
3781 if (info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI)
3782 dma_free_coherent(NULL, BUFFERLISTSIZE, info->buffer_list, info->buffer_list_dma_addr);
3783
3784 info->buffer_list = NULL;
3785 info->rx_buffer_list = NULL;
3786 info->tx_buffer_list = NULL;
3787
3788} /* end of mgsl_free_buffer_list_memory() */
3789
3790/*
3791 * mgsl_alloc_frame_memory()
3792 *
3793 * Allocate the frame DMA buffers used by the specified buffer list.
3794 * Each DMA buffer will be one memory page in size. This is necessary
3795 * because memory can fragment enough that it may be impossible
3796 * contiguous pages.
3797 *
3798 * Arguments:
3799 *
3800 * info pointer to device instance data
3801 * BufferList pointer to list of buffer entries
3802 * Buffercount count of buffer entries in buffer list
3803 *
3804 * Return Value: 0 if success, otherwise -ENOMEM
3805 */
3806static int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
3807{
3808 int i;
3809 u32 phys_addr;
3810
3811 /* Allocate page sized buffers for the receive buffer list */
3812
3813 for ( i = 0; i < Buffercount; i++ ) {
3814 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3815 /* PCI adapter uses shared memory buffers. */
3816 BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
3817 phys_addr = info->last_mem_alloc;
3818 info->last_mem_alloc += DMABUFFERSIZE;
3819 } else {
3820 /* ISA adapter uses system memory. */
3821 BufferList[i].virt_addr = dma_alloc_coherent(NULL, DMABUFFERSIZE, &BufferList[i].dma_addr, GFP_KERNEL);
3822 if (BufferList[i].virt_addr == NULL)
3823 return -ENOMEM;
3824 phys_addr = (u32)(BufferList[i].dma_addr);
3825 }
3826 BufferList[i].phys_addr = phys_addr;
3827 }
3828
3829 return 0;
3830
3831} /* end of mgsl_alloc_frame_memory() */
3832
3833/*
3834 * mgsl_free_frame_memory()
3835 *
3836 * Free the buffers associated with
3837 * each buffer entry of a buffer list.
3838 *
3839 * Arguments:
3840 *
3841 * info pointer to device instance data
3842 * BufferList pointer to list of buffer entries
3843 * Buffercount count of buffer entries in buffer list
3844 *
3845 * Return Value: None
3846 */
3847static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
3848{
3849 int i;
3850
3851 if ( BufferList ) {
3852 for ( i = 0 ; i < Buffercount ; i++ ) {
3853 if ( BufferList[i].virt_addr ) {
3854 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
3855 dma_free_coherent(NULL, DMABUFFERSIZE, BufferList[i].virt_addr, BufferList[i].dma_addr);
3856 BufferList[i].virt_addr = NULL;
3857 }
3858 }
3859 }
3860
3861} /* end of mgsl_free_frame_memory() */
3862
3863/* mgsl_free_dma_buffers()
3864 *
3865 * Free DMA buffers
3866 *
3867 * Arguments: info pointer to device instance data
3868 * Return Value: None
3869 */
3870static void mgsl_free_dma_buffers( struct mgsl_struct *info )
3871{
3872 mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
3873 mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
3874 mgsl_free_buffer_list_memory( info );
3875
3876} /* end of mgsl_free_dma_buffers() */
3877
3878
3879/*
3880 * mgsl_alloc_intermediate_rxbuffer_memory()
3881 *
3882 * Allocate a buffer large enough to hold max_frame_size. This buffer
3883 * is used to pass an assembled frame to the line discipline.
3884 *
3885 * Arguments:
3886 *
3887 * info pointer to device instance data
3888 *
3889 * Return Value: 0 if success, otherwise -ENOMEM
3890 */
3891static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3892{
3893 info->intermediate_rxbuffer = kmalloc(info->max_frame_size, GFP_KERNEL | GFP_DMA);
3894 if ( info->intermediate_rxbuffer == NULL )
3895 return -ENOMEM;
3896
3897 return 0;
3898
3899} /* end of mgsl_alloc_intermediate_rxbuffer_memory() */
3900
3901/*
3902 * mgsl_free_intermediate_rxbuffer_memory()
3903 *
3904 *
3905 * Arguments:
3906 *
3907 * info pointer to device instance data
3908 *
3909 * Return Value: None
3910 */
3911static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3912{
3913 kfree(info->intermediate_rxbuffer);
3914 info->intermediate_rxbuffer = NULL;
3915
3916} /* end of mgsl_free_intermediate_rxbuffer_memory() */
3917
3918/*
3919 * mgsl_alloc_intermediate_txbuffer_memory()
3920 *
3921 * Allocate intermdiate transmit buffer(s) large enough to hold max_frame_size.
3922 * This buffer is used to load transmit frames into the adapter's dma transfer
3923 * buffers when there is sufficient space.
3924 *
3925 * Arguments:
3926 *
3927 * info pointer to device instance data
3928 *
3929 * Return Value: 0 if success, otherwise -ENOMEM
3930 */
3931static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info)
3932{
3933 int i;
3934
3935 if ( debug_level >= DEBUG_LEVEL_INFO )
3936 printk("%s %s(%d) allocating %d tx holding buffers\n",
3937 info->device_name, __FILE__,__LINE__,info->num_tx_holding_buffers);
3938
3939 memset(info->tx_holding_buffers,0,sizeof(info->tx_holding_buffers));
3940
3941 for ( i=0; i<info->num_tx_holding_buffers; ++i) {
3942 info->tx_holding_buffers[i].buffer =
3943 kmalloc(info->max_frame_size, GFP_KERNEL);
3944 if (info->tx_holding_buffers[i].buffer == NULL) {
3945 for (--i; i >= 0; i--) {
3946 kfree(info->tx_holding_buffers[i].buffer);
3947 info->tx_holding_buffers[i].buffer = NULL;
3948 }
3949 return -ENOMEM;
3950 }
3951 }
3952
3953 return 0;
3954
3955} /* end of mgsl_alloc_intermediate_txbuffer_memory() */
3956
3957/*
3958 * mgsl_free_intermediate_txbuffer_memory()
3959 *
3960 *
3961 * Arguments:
3962 *
3963 * info pointer to device instance data
3964 *
3965 * Return Value: None
3966 */
3967static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info)
3968{
3969 int i;
3970
3971 for ( i=0; i<info->num_tx_holding_buffers; ++i ) {
3972 kfree(info->tx_holding_buffers[i].buffer);
3973 info->tx_holding_buffers[i].buffer = NULL;
3974 }
3975
3976 info->get_tx_holding_index = 0;
3977 info->put_tx_holding_index = 0;
3978 info->tx_holding_count = 0;
3979
3980} /* end of mgsl_free_intermediate_txbuffer_memory() */
3981
3982
3983/*
3984 * load_next_tx_holding_buffer()
3985 *
3986 * attempts to load the next buffered tx request into the
3987 * tx dma buffers
3988 *
3989 * Arguments:
3990 *
3991 * info pointer to device instance data
3992 *
3993 * Return Value: true if next buffered tx request loaded
3994 * into adapter's tx dma buffer,
3995 * false otherwise
3996 */
3997static bool load_next_tx_holding_buffer(struct mgsl_struct *info)
3998{
3999 bool ret = false;
4000
4001 if ( info->tx_holding_count ) {
4002 /* determine if we have enough tx dma buffers
4003 * to accommodate the next tx frame
4004 */
4005 struct tx_holding_buffer *ptx =
4006 &info->tx_holding_buffers[info->get_tx_holding_index];
4007 int num_free = num_free_tx_dma_buffers(info);
4008 int num_needed = ptx->buffer_size / DMABUFFERSIZE;
4009 if ( ptx->buffer_size % DMABUFFERSIZE )
4010 ++num_needed;
4011
4012 if (num_needed <= num_free) {
4013 info->xmit_cnt = ptx->buffer_size;
4014 mgsl_load_tx_dma_buffer(info,ptx->buffer,ptx->buffer_size);
4015
4016 --info->tx_holding_count;
4017 if ( ++info->get_tx_holding_index >= info->num_tx_holding_buffers)
4018 info->get_tx_holding_index=0;
4019
4020 /* restart transmit timer */
4021 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(5000));
4022
4023 ret = true;
4024 }
4025 }
4026
4027 return ret;
4028}
4029
4030/*
4031 * save_tx_buffer_request()
4032 *
4033 * attempt to store transmit frame request for later transmission
4034 *
4035 * Arguments:
4036 *
4037 * info pointer to device instance data
4038 * Buffer pointer to buffer containing frame to load
4039 * BufferSize size in bytes of frame in Buffer
4040 *
4041 * Return Value: 1 if able to store, 0 otherwise
4042 */
4043static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize)
4044{
4045 struct tx_holding_buffer *ptx;
4046
4047 if ( info->tx_holding_count >= info->num_tx_holding_buffers ) {
4048 return 0; /* all buffers in use */
4049 }
4050
4051 ptx = &info->tx_holding_buffers[info->put_tx_holding_index];
4052 ptx->buffer_size = BufferSize;
4053 memcpy( ptx->buffer, Buffer, BufferSize);
4054
4055 ++info->tx_holding_count;
4056 if ( ++info->put_tx_holding_index >= info->num_tx_holding_buffers)
4057 info->put_tx_holding_index=0;
4058
4059 return 1;
4060}
4061
4062static int mgsl_claim_resources(struct mgsl_struct *info)
4063{
4064 if (request_region(info->io_base,info->io_addr_size,"synclink") == NULL) {
4065 printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
4066 __FILE__,__LINE__,info->device_name, info->io_base);
4067 return -ENODEV;
4068 }
4069 info->io_addr_requested = true;
4070
4071 if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
4072 info->device_name, info ) < 0 ) {
4073 printk( "%s(%d):Can't request interrupt on device %s IRQ=%d\n",
4074 __FILE__,__LINE__,info->device_name, info->irq_level );
4075 goto errout;
4076 }
4077 info->irq_requested = true;
4078
4079 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4080 if (request_mem_region(info->phys_memory_base,0x40000,"synclink") == NULL) {
4081 printk( "%s(%d):mem addr conflict device %s Addr=%08X\n",
4082 __FILE__,__LINE__,info->device_name, info->phys_memory_base);
4083 goto errout;
4084 }
4085 info->shared_mem_requested = true;
4086 if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclink") == NULL) {
4087 printk( "%s(%d):lcr mem addr conflict device %s Addr=%08X\n",
4088 __FILE__,__LINE__,info->device_name, info->phys_lcr_base + info->lcr_offset);
4089 goto errout;
4090 }
4091 info->lcr_mem_requested = true;
4092
4093 info->memory_base = ioremap_nocache(info->phys_memory_base,
4094 0x40000);
4095 if (!info->memory_base) {
4096 printk( "%s(%d):Can't map shared memory on device %s MemAddr=%08X\n",
4097 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4098 goto errout;
4099 }
4100
4101 if ( !mgsl_memory_test(info) ) {
4102 printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
4103 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4104 goto errout;
4105 }
4106
4107 info->lcr_base = ioremap_nocache(info->phys_lcr_base,
4108 PAGE_SIZE);
4109 if (!info->lcr_base) {
4110 printk( "%s(%d):Can't map LCR memory on device %s MemAddr=%08X\n",
4111 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
4112 goto errout;
4113 }
4114 info->lcr_base += info->lcr_offset;
4115
4116 } else {
4117 /* claim DMA channel */
4118
4119 if (request_dma(info->dma_level,info->device_name) < 0){
4120 printk( "%s(%d):Can't request DMA channel on device %s DMA=%d\n",
4121 __FILE__,__LINE__,info->device_name, info->dma_level );
4122 mgsl_release_resources( info );
4123 return -ENODEV;
4124 }
4125 info->dma_requested = true;
4126
4127 /* ISA adapter uses bus master DMA */
4128 set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
4129 enable_dma(info->dma_level);
4130 }
4131
4132 if ( mgsl_allocate_dma_buffers(info) < 0 ) {
4133 printk( "%s(%d):Can't allocate DMA buffers on device %s DMA=%d\n",
4134 __FILE__,__LINE__,info->device_name, info->dma_level );
4135 goto errout;
4136 }
4137
4138 return 0;
4139errout:
4140 mgsl_release_resources(info);
4141 return -ENODEV;
4142
4143} /* end of mgsl_claim_resources() */
4144
4145static void mgsl_release_resources(struct mgsl_struct *info)
4146{
4147 if ( debug_level >= DEBUG_LEVEL_INFO )
4148 printk( "%s(%d):mgsl_release_resources(%s) entry\n",
4149 __FILE__,__LINE__,info->device_name );
4150
4151 if ( info->irq_requested ) {
4152 free_irq(info->irq_level, info);
4153 info->irq_requested = false;
4154 }
4155 if ( info->dma_requested ) {
4156 disable_dma(info->dma_level);
4157 free_dma(info->dma_level);
4158 info->dma_requested = false;
4159 }
4160 mgsl_free_dma_buffers(info);
4161 mgsl_free_intermediate_rxbuffer_memory(info);
4162 mgsl_free_intermediate_txbuffer_memory(info);
4163
4164 if ( info->io_addr_requested ) {
4165 release_region(info->io_base,info->io_addr_size);
4166 info->io_addr_requested = false;
4167 }
4168 if ( info->shared_mem_requested ) {
4169 release_mem_region(info->phys_memory_base,0x40000);
4170 info->shared_mem_requested = false;
4171 }
4172 if ( info->lcr_mem_requested ) {
4173 release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
4174 info->lcr_mem_requested = false;
4175 }
4176 if (info->memory_base){
4177 iounmap(info->memory_base);
4178 info->memory_base = NULL;
4179 }
4180 if (info->lcr_base){
4181 iounmap(info->lcr_base - info->lcr_offset);
4182 info->lcr_base = NULL;
4183 }
4184
4185 if ( debug_level >= DEBUG_LEVEL_INFO )
4186 printk( "%s(%d):mgsl_release_resources(%s) exit\n",
4187 __FILE__,__LINE__,info->device_name );
4188
4189} /* end of mgsl_release_resources() */
4190
4191/* mgsl_add_device()
4192 *
4193 * Add the specified device instance data structure to the
4194 * global linked list of devices and increment the device count.
4195 *
4196 * Arguments: info pointer to device instance data
4197 * Return Value: None
4198 */
4199static void mgsl_add_device( struct mgsl_struct *info )
4200{
4201 info->next_device = NULL;
4202 info->line = mgsl_device_count;
4203 sprintf(info->device_name,"ttySL%d",info->line);
4204
4205 if (info->line < MAX_TOTAL_DEVICES) {
4206 if (maxframe[info->line])
4207 info->max_frame_size = maxframe[info->line];
4208
4209 if (txdmabufs[info->line]) {
4210 info->num_tx_dma_buffers = txdmabufs[info->line];
4211 if (info->num_tx_dma_buffers < 1)
4212 info->num_tx_dma_buffers = 1;
4213 }
4214
4215 if (txholdbufs[info->line]) {
4216 info->num_tx_holding_buffers = txholdbufs[info->line];
4217 if (info->num_tx_holding_buffers < 1)
4218 info->num_tx_holding_buffers = 1;
4219 else if (info->num_tx_holding_buffers > MAX_TX_HOLDING_BUFFERS)
4220 info->num_tx_holding_buffers = MAX_TX_HOLDING_BUFFERS;
4221 }
4222 }
4223
4224 mgsl_device_count++;
4225
4226 if ( !mgsl_device_list )
4227 mgsl_device_list = info;
4228 else {
4229 struct mgsl_struct *current_dev = mgsl_device_list;
4230 while( current_dev->next_device )
4231 current_dev = current_dev->next_device;
4232 current_dev->next_device = info;
4233 }
4234
4235 if ( info->max_frame_size < 4096 )
4236 info->max_frame_size = 4096;
4237 else if ( info->max_frame_size > 65535 )
4238 info->max_frame_size = 65535;
4239
4240 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4241 printk( "SyncLink PCI v%d %s: IO=%04X IRQ=%d Mem=%08X,%08X MaxFrameSize=%u\n",
4242 info->hw_version + 1, info->device_name, info->io_base, info->irq_level,
4243 info->phys_memory_base, info->phys_lcr_base,
4244 info->max_frame_size );
4245 } else {
4246 printk( "SyncLink ISA %s: IO=%04X IRQ=%d DMA=%d MaxFrameSize=%u\n",
4247 info->device_name, info->io_base, info->irq_level, info->dma_level,
4248 info->max_frame_size );
4249 }
4250
4251#if SYNCLINK_GENERIC_HDLC
4252 hdlcdev_init(info);
4253#endif
4254
4255} /* end of mgsl_add_device() */
4256
4257static const struct tty_port_operations mgsl_port_ops = {
4258 .carrier_raised = carrier_raised,
4259 .dtr_rts = dtr_rts,
4260};
4261
4262
4263/* mgsl_allocate_device()
4264 *
4265 * Allocate and initialize a device instance structure
4266 *
4267 * Arguments: none
4268 * Return Value: pointer to mgsl_struct if success, otherwise NULL
4269 */
4270static struct mgsl_struct* mgsl_allocate_device(void)
4271{
4272 struct mgsl_struct *info;
4273
4274 info = kzalloc(sizeof(struct mgsl_struct),
4275 GFP_KERNEL);
4276
4277 if (!info) {
4278 printk("Error can't allocate device instance data\n");
4279 } else {
4280 tty_port_init(&info->port);
4281 info->port.ops = &mgsl_port_ops;
4282 info->magic = MGSL_MAGIC;
4283 INIT_WORK(&info->task, mgsl_bh_handler);
4284 info->max_frame_size = 4096;
4285 info->port.close_delay = 5*HZ/10;
4286 info->port.closing_wait = 30*HZ;
4287 init_waitqueue_head(&info->status_event_wait_q);
4288 init_waitqueue_head(&info->event_wait_q);
4289 spin_lock_init(&info->irq_spinlock);
4290 spin_lock_init(&info->netlock);
4291 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
4292 info->idle_mode = HDLC_TXIDLE_FLAGS;
4293 info->num_tx_dma_buffers = 1;
4294 info->num_tx_holding_buffers = 0;
4295 }
4296
4297 return info;
4298
4299} /* end of mgsl_allocate_device()*/
4300
4301static const struct tty_operations mgsl_ops = {
4302 .open = mgsl_open,
4303 .close = mgsl_close,
4304 .write = mgsl_write,
4305 .put_char = mgsl_put_char,
4306 .flush_chars = mgsl_flush_chars,
4307 .write_room = mgsl_write_room,
4308 .chars_in_buffer = mgsl_chars_in_buffer,
4309 .flush_buffer = mgsl_flush_buffer,
4310 .ioctl = mgsl_ioctl,
4311 .throttle = mgsl_throttle,
4312 .unthrottle = mgsl_unthrottle,
4313 .send_xchar = mgsl_send_xchar,
4314 .break_ctl = mgsl_break,
4315 .wait_until_sent = mgsl_wait_until_sent,
4316 .set_termios = mgsl_set_termios,
4317 .stop = mgsl_stop,
4318 .start = mgsl_start,
4319 .hangup = mgsl_hangup,
4320 .tiocmget = tiocmget,
4321 .tiocmset = tiocmset,
4322 .get_icount = msgl_get_icount,
4323 .proc_fops = &mgsl_proc_fops,
4324};
4325
4326/*
4327 * perform tty device initialization
4328 */
4329static int mgsl_init_tty(void)
4330{
4331 int rc;
4332
4333 serial_driver = alloc_tty_driver(128);
4334 if (!serial_driver)
4335 return -ENOMEM;
4336
4337 serial_driver->owner = THIS_MODULE;
4338 serial_driver->driver_name = "synclink";
4339 serial_driver->name = "ttySL";
4340 serial_driver->major = ttymajor;
4341 serial_driver->minor_start = 64;
4342 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4343 serial_driver->subtype = SERIAL_TYPE_NORMAL;
4344 serial_driver->init_termios = tty_std_termios;
4345 serial_driver->init_termios.c_cflag =
4346 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4347 serial_driver->init_termios.c_ispeed = 9600;
4348 serial_driver->init_termios.c_ospeed = 9600;
4349 serial_driver->flags = TTY_DRIVER_REAL_RAW;
4350 tty_set_operations(serial_driver, &mgsl_ops);
4351 if ((rc = tty_register_driver(serial_driver)) < 0) {
4352 printk("%s(%d):Couldn't register serial driver\n",
4353 __FILE__,__LINE__);
4354 put_tty_driver(serial_driver);
4355 serial_driver = NULL;
4356 return rc;
4357 }
4358
4359 printk("%s %s, tty major#%d\n",
4360 driver_name, driver_version,
4361 serial_driver->major);
4362 return 0;
4363}
4364
4365/* enumerate user specified ISA adapters
4366 */
4367static void mgsl_enum_isa_devices(void)
4368{
4369 struct mgsl_struct *info;
4370 int i;
4371
4372 /* Check for user specified ISA devices */
4373
4374 for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
4375 if ( debug_level >= DEBUG_LEVEL_INFO )
4376 printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
4377 io[i], irq[i], dma[i] );
4378
4379 info = mgsl_allocate_device();
4380 if ( !info ) {
4381 /* error allocating device instance data */
4382 if ( debug_level >= DEBUG_LEVEL_ERROR )
4383 printk( "can't allocate device instance data.\n");
4384 continue;
4385 }
4386
4387 /* Copy user configuration info to device instance data */
4388 info->io_base = (unsigned int)io[i];
4389 info->irq_level = (unsigned int)irq[i];
4390 info->irq_level = irq_canonicalize(info->irq_level);
4391 info->dma_level = (unsigned int)dma[i];
4392 info->bus_type = MGSL_BUS_TYPE_ISA;
4393 info->io_addr_size = 16;
4394 info->irq_flags = 0;
4395
4396 mgsl_add_device( info );
4397 }
4398}
4399
4400static void synclink_cleanup(void)
4401{
4402 int rc;
4403 struct mgsl_struct *info;
4404 struct mgsl_struct *tmp;
4405
4406 printk("Unloading %s: %s\n", driver_name, driver_version);
4407
4408 if (serial_driver) {
4409 if ((rc = tty_unregister_driver(serial_driver)))
4410 printk("%s(%d) failed to unregister tty driver err=%d\n",
4411 __FILE__,__LINE__,rc);
4412 put_tty_driver(serial_driver);
4413 }
4414
4415 info = mgsl_device_list;
4416 while(info) {
4417#if SYNCLINK_GENERIC_HDLC
4418 hdlcdev_exit(info);
4419#endif
4420 mgsl_release_resources(info);
4421 tmp = info;
4422 info = info->next_device;
4423 kfree(tmp);
4424 }
4425
4426 if (pci_registered)
4427 pci_unregister_driver(&synclink_pci_driver);
4428}
4429
4430static int __init synclink_init(void)
4431{
4432 int rc;
4433
4434 if (break_on_load) {
4435 mgsl_get_text_ptr();
4436 BREAKPOINT();
4437 }
4438
4439 printk("%s %s\n", driver_name, driver_version);
4440
4441 mgsl_enum_isa_devices();
4442 if ((rc = pci_register_driver(&synclink_pci_driver)) < 0)
4443 printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
4444 else
4445 pci_registered = true;
4446
4447 if ((rc = mgsl_init_tty()) < 0)
4448 goto error;
4449
4450 return 0;
4451
4452error:
4453 synclink_cleanup();
4454 return rc;
4455}
4456
4457static void __exit synclink_exit(void)
4458{
4459 synclink_cleanup();
4460}
4461
4462module_init(synclink_init);
4463module_exit(synclink_exit);
4464
4465/*
4466 * usc_RTCmd()
4467 *
4468 * Issue a USC Receive/Transmit command to the
4469 * Channel Command/Address Register (CCAR).
4470 *
4471 * Notes:
4472 *
4473 * The command is encoded in the most significant 5 bits <15..11>
4474 * of the CCAR value. Bits <10..7> of the CCAR must be preserved
4475 * and Bits <6..0> must be written as zeros.
4476 *
4477 * Arguments:
4478 *
4479 * info pointer to device information structure
4480 * Cmd command mask (use symbolic macros)
4481 *
4482 * Return Value:
4483 *
4484 * None
4485 */
4486static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
4487{
4488 /* output command to CCAR in bits <15..11> */
4489 /* preserve bits <10..7>, bits <6..0> must be zero */
4490
4491 outw( Cmd + info->loopback_bits, info->io_base + CCAR );
4492
4493 /* Read to flush write to CCAR */
4494 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4495 inw( info->io_base + CCAR );
4496
4497} /* end of usc_RTCmd() */
4498
4499/*
4500 * usc_DmaCmd()
4501 *
4502 * Issue a DMA command to the DMA Command/Address Register (DCAR).
4503 *
4504 * Arguments:
4505 *
4506 * info pointer to device information structure
4507 * Cmd DMA command mask (usc_DmaCmd_XX Macros)
4508 *
4509 * Return Value:
4510 *
4511 * None
4512 */
4513static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
4514{
4515 /* write command mask to DCAR */
4516 outw( Cmd + info->mbre_bit, info->io_base );
4517
4518 /* Read to flush write to DCAR */
4519 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4520 inw( info->io_base );
4521
4522} /* end of usc_DmaCmd() */
4523
4524/*
4525 * usc_OutDmaReg()
4526 *
4527 * Write a 16-bit value to a USC DMA register
4528 *
4529 * Arguments:
4530 *
4531 * info pointer to device info structure
4532 * RegAddr register address (number) for write
4533 * RegValue 16-bit value to write to register
4534 *
4535 * Return Value:
4536 *
4537 * None
4538 *
4539 */
4540static void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4541{
4542 /* Note: The DCAR is located at the adapter base address */
4543 /* Note: must preserve state of BIT8 in DCAR */
4544
4545 outw( RegAddr + info->mbre_bit, info->io_base );
4546 outw( RegValue, info->io_base );
4547
4548 /* Read to flush write to DCAR */
4549 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4550 inw( info->io_base );
4551
4552} /* end of usc_OutDmaReg() */
4553
4554/*
4555 * usc_InDmaReg()
4556 *
4557 * Read a 16-bit value from a DMA register
4558 *
4559 * Arguments:
4560 *
4561 * info pointer to device info structure
4562 * RegAddr register address (number) to read from
4563 *
4564 * Return Value:
4565 *
4566 * The 16-bit value read from register
4567 *
4568 */
4569static u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
4570{
4571 /* Note: The DCAR is located at the adapter base address */
4572 /* Note: must preserve state of BIT8 in DCAR */
4573
4574 outw( RegAddr + info->mbre_bit, info->io_base );
4575 return inw( info->io_base );
4576
4577} /* end of usc_InDmaReg() */
4578
4579/*
4580 *
4581 * usc_OutReg()
4582 *
4583 * Write a 16-bit value to a USC serial channel register
4584 *
4585 * Arguments:
4586 *
4587 * info pointer to device info structure
4588 * RegAddr register address (number) to write to
4589 * RegValue 16-bit value to write to register
4590 *
4591 * Return Value:
4592 *
4593 * None
4594 *
4595 */
4596static void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4597{
4598 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4599 outw( RegValue, info->io_base + CCAR );
4600
4601 /* Read to flush write to CCAR */
4602 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4603 inw( info->io_base + CCAR );
4604
4605} /* end of usc_OutReg() */
4606
4607/*
4608 * usc_InReg()
4609 *
4610 * Reads a 16-bit value from a USC serial channel register
4611 *
4612 * Arguments:
4613 *
4614 * info pointer to device extension
4615 * RegAddr register address (number) to read from
4616 *
4617 * Return Value:
4618 *
4619 * 16-bit value read from register
4620 */
4621static u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
4622{
4623 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4624 return inw( info->io_base + CCAR );
4625
4626} /* end of usc_InReg() */
4627
4628/* usc_set_sdlc_mode()
4629 *
4630 * Set up the adapter for SDLC DMA communications.
4631 *
4632 * Arguments: info pointer to device instance data
4633 * Return Value: NONE
4634 */
4635static void usc_set_sdlc_mode( struct mgsl_struct *info )
4636{
4637 u16 RegValue;
4638 bool PreSL1660;
4639
4640 /*
4641 * determine if the IUSC on the adapter is pre-SL1660. If
4642 * not, take advantage of the UnderWait feature of more
4643 * modern chips. If an underrun occurs and this bit is set,
4644 * the transmitter will idle the programmed idle pattern
4645 * until the driver has time to service the underrun. Otherwise,
4646 * the dma controller may get the cycles previously requested
4647 * and begin transmitting queued tx data.
4648 */
4649 usc_OutReg(info,TMCR,0x1f);
4650 RegValue=usc_InReg(info,TMDR);
4651 PreSL1660 = (RegValue == IUSC_PRE_SL1660);
4652
4653 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
4654 {
4655 /*
4656 ** Channel Mode Register (CMR)
4657 **
4658 ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
4659 ** <13> 0 0 = Transmit Disabled (initially)
4660 ** <12> 0 1 = Consecutive Idles share common 0
4661 ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
4662 ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
4663 ** <3..0> 0110 Receiver Mode = HDLC/SDLC
4664 **
4665 ** 1000 1110 0000 0110 = 0x8e06
4666 */
4667 RegValue = 0x8e06;
4668
4669 /*--------------------------------------------------
4670 * ignore user options for UnderRun Actions and
4671 * preambles
4672 *--------------------------------------------------*/
4673 }
4674 else
4675 {
4676 /* Channel mode Register (CMR)
4677 *
4678 * <15..14> 00 Tx Sub modes, Underrun Action
4679 * <13> 0 1 = Send Preamble before opening flag
4680 * <12> 0 1 = Consecutive Idles share common 0
4681 * <11..8> 0110 Transmitter mode = HDLC/SDLC
4682 * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
4683 * <3..0> 0110 Receiver mode = HDLC/SDLC
4684 *
4685 * 0000 0110 0000 0110 = 0x0606
4686 */
4687 if (info->params.mode == MGSL_MODE_RAW) {
4688 RegValue = 0x0001; /* Set Receive mode = external sync */
4689
4690 usc_OutReg( info, IOCR, /* Set IOCR DCD is RxSync Detect Input */
4691 (unsigned short)((usc_InReg(info, IOCR) & ~(BIT13|BIT12)) | BIT12));
4692
4693 /*
4694 * TxSubMode:
4695 * CMR <15> 0 Don't send CRC on Tx Underrun
4696 * CMR <14> x undefined
4697 * CMR <13> 0 Send preamble before openning sync
4698 * CMR <12> 0 Send 8-bit syncs, 1=send Syncs per TxLength
4699 *
4700 * TxMode:
4701 * CMR <11-8) 0100 MonoSync
4702 *
4703 * 0x00 0100 xxxx xxxx 04xx
4704 */
4705 RegValue |= 0x0400;
4706 }
4707 else {
4708
4709 RegValue = 0x0606;
4710
4711 if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
4712 RegValue |= BIT14;
4713 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4714 RegValue |= BIT15;
4715 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
4716 RegValue |= BIT15 + BIT14;
4717 }
4718
4719 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
4720 RegValue |= BIT13;
4721 }
4722
4723 if ( info->params.mode == MGSL_MODE_HDLC &&
4724 (info->params.flags & HDLC_FLAG_SHARE_ZERO) )
4725 RegValue |= BIT12;
4726
4727 if ( info->params.addr_filter != 0xff )
4728 {
4729 /* set up receive address filtering */
4730 usc_OutReg( info, RSR, info->params.addr_filter );
4731 RegValue |= BIT4;
4732 }
4733
4734 usc_OutReg( info, CMR, RegValue );
4735 info->cmr_value = RegValue;
4736
4737 /* Receiver mode Register (RMR)
4738 *
4739 * <15..13> 000 encoding
4740 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4741 * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
4742 * <9> 0 1 = Include Receive chars in CRC
4743 * <8> 1 1 = Use Abort/PE bit as abort indicator
4744 * <7..6> 00 Even parity
4745 * <5> 0 parity disabled
4746 * <4..2> 000 Receive Char Length = 8 bits
4747 * <1..0> 00 Disable Receiver
4748 *
4749 * 0000 0101 0000 0000 = 0x0500
4750 */
4751
4752 RegValue = 0x0500;
4753
4754 switch ( info->params.encoding ) {
4755 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4756 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4757 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
4758 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4759 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
4760 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
4761 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
4762 }
4763
4764 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4765 RegValue |= BIT9;
4766 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4767 RegValue |= ( BIT12 | BIT10 | BIT9 );
4768
4769 usc_OutReg( info, RMR, RegValue );
4770
4771 /* Set the Receive count Limit Register (RCLR) to 0xffff. */
4772 /* When an opening flag of an SDLC frame is recognized the */
4773 /* Receive Character count (RCC) is loaded with the value in */
4774 /* RCLR. The RCC is decremented for each received byte. The */
4775 /* value of RCC is stored after the closing flag of the frame */
4776 /* allowing the frame size to be computed. */
4777
4778 usc_OutReg( info, RCLR, RCLRVALUE );
4779
4780 usc_RCmd( info, RCmd_SelectRicrdma_level );
4781
4782 /* Receive Interrupt Control Register (RICR)
4783 *
4784 * <15..8> ? RxFIFO DMA Request Level
4785 * <7> 0 Exited Hunt IA (Interrupt Arm)
4786 * <6> 0 Idle Received IA
4787 * <5> 0 Break/Abort IA
4788 * <4> 0 Rx Bound IA
4789 * <3> 1 Queued status reflects oldest 2 bytes in FIFO
4790 * <2> 0 Abort/PE IA
4791 * <1> 1 Rx Overrun IA
4792 * <0> 0 Select TC0 value for readback
4793 *
4794 * 0000 0000 0000 1000 = 0x000a
4795 */
4796
4797 /* Carry over the Exit Hunt and Idle Received bits */
4798 /* in case they have been armed by usc_ArmEvents. */
4799
4800 RegValue = usc_InReg( info, RICR ) & 0xc0;
4801
4802 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4803 usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
4804 else
4805 usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
4806
4807 /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
4808
4809 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
4810 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
4811
4812 /* Transmit mode Register (TMR)
4813 *
4814 * <15..13> 000 encoding
4815 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4816 * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
4817 * <9> 0 1 = Tx CRC Enabled
4818 * <8> 0 1 = Append CRC to end of transmit frame
4819 * <7..6> 00 Transmit parity Even
4820 * <5> 0 Transmit parity Disabled
4821 * <4..2> 000 Tx Char Length = 8 bits
4822 * <1..0> 00 Disable Transmitter
4823 *
4824 * 0000 0100 0000 0000 = 0x0400
4825 */
4826
4827 RegValue = 0x0400;
4828
4829 switch ( info->params.encoding ) {
4830 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4831 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4832 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
4833 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4834 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
4835 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
4836 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
4837 }
4838
4839 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4840 RegValue |= BIT9 + BIT8;
4841 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4842 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
4843
4844 usc_OutReg( info, TMR, RegValue );
4845
4846 usc_set_txidle( info );
4847
4848
4849 usc_TCmd( info, TCmd_SelectTicrdma_level );
4850
4851 /* Transmit Interrupt Control Register (TICR)
4852 *
4853 * <15..8> ? Transmit FIFO DMA Level
4854 * <7> 0 Present IA (Interrupt Arm)
4855 * <6> 0 Idle Sent IA
4856 * <5> 1 Abort Sent IA
4857 * <4> 1 EOF/EOM Sent IA
4858 * <3> 0 CRC Sent IA
4859 * <2> 1 1 = Wait for SW Trigger to Start Frame
4860 * <1> 1 Tx Underrun IA
4861 * <0> 0 TC0 constant on read back
4862 *
4863 * 0000 0000 0011 0110 = 0x0036
4864 */
4865
4866 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4867 usc_OutReg( info, TICR, 0x0736 );
4868 else
4869 usc_OutReg( info, TICR, 0x1436 );
4870
4871 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
4872 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
4873
4874 /*
4875 ** Transmit Command/Status Register (TCSR)
4876 **
4877 ** <15..12> 0000 TCmd
4878 ** <11> 0/1 UnderWait
4879 ** <10..08> 000 TxIdle
4880 ** <7> x PreSent
4881 ** <6> x IdleSent
4882 ** <5> x AbortSent
4883 ** <4> x EOF/EOM Sent
4884 ** <3> x CRC Sent
4885 ** <2> x All Sent
4886 ** <1> x TxUnder
4887 ** <0> x TxEmpty
4888 **
4889 ** 0000 0000 0000 0000 = 0x0000
4890 */
4891 info->tcsr_value = 0;
4892
4893 if ( !PreSL1660 )
4894 info->tcsr_value |= TCSR_UNDERWAIT;
4895
4896 usc_OutReg( info, TCSR, info->tcsr_value );
4897
4898 /* Clock mode Control Register (CMCR)
4899 *
4900 * <15..14> 00 counter 1 Source = Disabled
4901 * <13..12> 00 counter 0 Source = Disabled
4902 * <11..10> 11 BRG1 Input is TxC Pin
4903 * <9..8> 11 BRG0 Input is TxC Pin
4904 * <7..6> 01 DPLL Input is BRG1 Output
4905 * <5..3> XXX TxCLK comes from Port 0
4906 * <2..0> XXX RxCLK comes from Port 1
4907 *
4908 * 0000 1111 0111 0111 = 0x0f77
4909 */
4910
4911 RegValue = 0x0f40;
4912
4913 if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
4914 RegValue |= 0x0003; /* RxCLK from DPLL */
4915 else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
4916 RegValue |= 0x0004; /* RxCLK from BRG0 */
4917 else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4918 RegValue |= 0x0006; /* RxCLK from TXC Input */
4919 else
4920 RegValue |= 0x0007; /* RxCLK from Port1 */
4921
4922 if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
4923 RegValue |= 0x0018; /* TxCLK from DPLL */
4924 else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
4925 RegValue |= 0x0020; /* TxCLK from BRG0 */
4926 else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4927 RegValue |= 0x0038; /* RxCLK from TXC Input */
4928 else
4929 RegValue |= 0x0030; /* TxCLK from Port0 */
4930
4931 usc_OutReg( info, CMCR, RegValue );
4932
4933
4934 /* Hardware Configuration Register (HCR)
4935 *
4936 * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
4937 * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
4938 * <12> 0 CVOK:0=report code violation in biphase
4939 * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
4940 * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
4941 * <7..6> 00 reserved
4942 * <5> 0 BRG1 mode:0=continuous,1=single cycle
4943 * <4> X BRG1 Enable
4944 * <3..2> 00 reserved
4945 * <1> 0 BRG0 mode:0=continuous,1=single cycle
4946 * <0> 0 BRG0 Enable
4947 */
4948
4949 RegValue = 0x0000;
4950
4951 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) {
4952 u32 XtalSpeed;
4953 u32 DpllDivisor;
4954 u16 Tc;
4955
4956 /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
4957 /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
4958
4959 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4960 XtalSpeed = 11059200;
4961 else
4962 XtalSpeed = 14745600;
4963
4964 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4965 DpllDivisor = 16;
4966 RegValue |= BIT10;
4967 }
4968 else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4969 DpllDivisor = 8;
4970 RegValue |= BIT11;
4971 }
4972 else
4973 DpllDivisor = 32;
4974
4975 /* Tc = (Xtal/Speed) - 1 */
4976 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
4977 /* then rounding up gives a more precise time constant. Instead */
4978 /* of rounding up and then subtracting 1 we just don't subtract */
4979 /* the one in this case. */
4980
4981 /*--------------------------------------------------
4982 * ejz: for DPLL mode, application should use the
4983 * same clock speed as the partner system, even
4984 * though clocking is derived from the input RxData.
4985 * In case the user uses a 0 for the clock speed,
4986 * default to 0xffffffff and don't try to divide by
4987 * zero
4988 *--------------------------------------------------*/
4989 if ( info->params.clock_speed )
4990 {
4991 Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
4992 if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
4993 / info->params.clock_speed) )
4994 Tc--;
4995 }
4996 else
4997 Tc = -1;
4998
4999
5000 /* Write 16-bit Time Constant for BRG1 */
5001 usc_OutReg( info, TC1R, Tc );
5002
5003 RegValue |= BIT4; /* enable BRG1 */
5004
5005 switch ( info->params.encoding ) {
5006 case HDLC_ENCODING_NRZ:
5007 case HDLC_ENCODING_NRZB:
5008 case HDLC_ENCODING_NRZI_MARK:
5009 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
5010 case HDLC_ENCODING_BIPHASE_MARK:
5011 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
5012 case HDLC_ENCODING_BIPHASE_LEVEL:
5013 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break;
5014 }
5015 }
5016
5017 usc_OutReg( info, HCR, RegValue );
5018
5019
5020 /* Channel Control/status Register (CCSR)
5021 *
5022 * <15> X RCC FIFO Overflow status (RO)
5023 * <14> X RCC FIFO Not Empty status (RO)
5024 * <13> 0 1 = Clear RCC FIFO (WO)
5025 * <12> X DPLL Sync (RW)
5026 * <11> X DPLL 2 Missed Clocks status (RO)
5027 * <10> X DPLL 1 Missed Clock status (RO)
5028 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
5029 * <7> X SDLC Loop On status (RO)
5030 * <6> X SDLC Loop Send status (RO)
5031 * <5> 1 Bypass counters for TxClk and RxClk (RW)
5032 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
5033 * <1..0> 00 reserved
5034 *
5035 * 0000 0000 0010 0000 = 0x0020
5036 */
5037
5038 usc_OutReg( info, CCSR, 0x1020 );
5039
5040
5041 if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
5042 usc_OutReg( info, SICR,
5043 (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
5044 }
5045
5046
5047 /* enable Master Interrupt Enable bit (MIE) */
5048 usc_EnableMasterIrqBit( info );
5049
5050 usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA +
5051 TRANSMIT_STATUS + TRANSMIT_DATA + MISC);
5052
5053 /* arm RCC underflow interrupt */
5054 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
5055 usc_EnableInterrupts(info, MISC);
5056
5057 info->mbre_bit = 0;
5058 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5059 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5060 info->mbre_bit = BIT8;
5061 outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
5062
5063 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
5064 /* Enable DMAEN (Port 7, Bit 14) */
5065 /* This connects the DMA request signal to the ISA bus */
5066 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14));
5067 }
5068
5069 /* DMA Control Register (DCR)
5070 *
5071 * <15..14> 10 Priority mode = Alternating Tx/Rx
5072 * 01 Rx has priority
5073 * 00 Tx has priority
5074 *
5075 * <13> 1 Enable Priority Preempt per DCR<15..14>
5076 * (WARNING DCR<11..10> must be 00 when this is 1)
5077 * 0 Choose activate channel per DCR<11..10>
5078 *
5079 * <12> 0 Little Endian for Array/List
5080 * <11..10> 00 Both Channels can use each bus grant
5081 * <9..6> 0000 reserved
5082 * <5> 0 7 CLK - Minimum Bus Re-request Interval
5083 * <4> 0 1 = drive D/C and S/D pins
5084 * <3> 1 1 = Add one wait state to all DMA cycles.
5085 * <2> 0 1 = Strobe /UAS on every transfer.
5086 * <1..0> 11 Addr incrementing only affects LS24 bits
5087 *
5088 * 0110 0000 0000 1011 = 0x600b
5089 */
5090
5091 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5092 /* PCI adapter does not need DMA wait state */
5093 usc_OutDmaReg( info, DCR, 0xa00b );
5094 }
5095 else
5096 usc_OutDmaReg( info, DCR, 0x800b );
5097
5098
5099 /* Receive DMA mode Register (RDMR)
5100 *
5101 * <15..14> 11 DMA mode = Linked List Buffer mode
5102 * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
5103 * <12> 1 Clear count of List Entry after fetching
5104 * <11..10> 00 Address mode = Increment
5105 * <9> 1 Terminate Buffer on RxBound
5106 * <8> 0 Bus Width = 16bits
5107 * <7..0> ? status Bits (write as 0s)
5108 *
5109 * 1111 0010 0000 0000 = 0xf200
5110 */
5111
5112 usc_OutDmaReg( info, RDMR, 0xf200 );
5113
5114
5115 /* Transmit DMA mode Register (TDMR)
5116 *
5117 * <15..14> 11 DMA mode = Linked List Buffer mode
5118 * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
5119 * <12> 1 Clear count of List Entry after fetching
5120 * <11..10> 00 Address mode = Increment
5121 * <9> 1 Terminate Buffer on end of frame
5122 * <8> 0 Bus Width = 16bits
5123 * <7..0> ? status Bits (Read Only so write as 0)
5124 *
5125 * 1111 0010 0000 0000 = 0xf200
5126 */
5127
5128 usc_OutDmaReg( info, TDMR, 0xf200 );
5129
5130
5131 /* DMA Interrupt Control Register (DICR)
5132 *
5133 * <15> 1 DMA Interrupt Enable
5134 * <14> 0 1 = Disable IEO from USC
5135 * <13> 0 1 = Don't provide vector during IntAck
5136 * <12> 1 1 = Include status in Vector
5137 * <10..2> 0 reserved, Must be 0s
5138 * <1> 0 1 = Rx DMA Interrupt Enabled
5139 * <0> 0 1 = Tx DMA Interrupt Enabled
5140 *
5141 * 1001 0000 0000 0000 = 0x9000
5142 */
5143
5144 usc_OutDmaReg( info, DICR, 0x9000 );
5145
5146 usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
5147 usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
5148 usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
5149
5150 /* Channel Control Register (CCR)
5151 *
5152 * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
5153 * <13> 0 Trigger Tx on SW Command Disabled
5154 * <12> 0 Flag Preamble Disabled
5155 * <11..10> 00 Preamble Length
5156 * <9..8> 00 Preamble Pattern
5157 * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
5158 * <5> 0 Trigger Rx on SW Command Disabled
5159 * <4..0> 0 reserved
5160 *
5161 * 1000 0000 1000 0000 = 0x8080
5162 */
5163
5164 RegValue = 0x8080;
5165
5166 switch ( info->params.preamble_length ) {
5167 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5168 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
5169 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break;
5170 }
5171
5172 switch ( info->params.preamble ) {
5173 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break;
5174 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5175 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
5176 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break;
5177 }
5178
5179 usc_OutReg( info, CCR, RegValue );
5180
5181
5182 /*
5183 * Burst/Dwell Control Register
5184 *
5185 * <15..8> 0x20 Maximum number of transfers per bus grant
5186 * <7..0> 0x00 Maximum number of clock cycles per bus grant
5187 */
5188
5189 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5190 /* don't limit bus occupancy on PCI adapter */
5191 usc_OutDmaReg( info, BDCR, 0x0000 );
5192 }
5193 else
5194 usc_OutDmaReg( info, BDCR, 0x2000 );
5195
5196 usc_stop_transmitter(info);
5197 usc_stop_receiver(info);
5198
5199} /* end of usc_set_sdlc_mode() */
5200
5201/* usc_enable_loopback()
5202 *
5203 * Set the 16C32 for internal loopback mode.
5204 * The TxCLK and RxCLK signals are generated from the BRG0 and
5205 * the TxD is looped back to the RxD internally.
5206 *
5207 * Arguments: info pointer to device instance data
5208 * enable 1 = enable loopback, 0 = disable
5209 * Return Value: None
5210 */
5211static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5212{
5213 if (enable) {
5214 /* blank external TXD output */
5215 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6));
5216
5217 /* Clock mode Control Register (CMCR)
5218 *
5219 * <15..14> 00 counter 1 Disabled
5220 * <13..12> 00 counter 0 Disabled
5221 * <11..10> 11 BRG1 Input is TxC Pin
5222 * <9..8> 11 BRG0 Input is TxC Pin
5223 * <7..6> 01 DPLL Input is BRG1 Output
5224 * <5..3> 100 TxCLK comes from BRG0
5225 * <2..0> 100 RxCLK comes from BRG0
5226 *
5227 * 0000 1111 0110 0100 = 0x0f64
5228 */
5229
5230 usc_OutReg( info, CMCR, 0x0f64 );
5231
5232 /* Write 16-bit Time Constant for BRG0 */
5233 /* use clock speed if available, otherwise use 8 for diagnostics */
5234 if (info->params.clock_speed) {
5235 if (info->bus_type == MGSL_BUS_TYPE_PCI)
5236 usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
5237 else
5238 usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
5239 } else
5240 usc_OutReg(info, TC0R, (u16)8);
5241
5242 /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
5243 mode = Continuous Set Bit 0 to enable BRG0. */
5244 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5245
5246 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5247 usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
5248
5249 /* set Internal Data loopback mode */
5250 info->loopback_bits = 0x300;
5251 outw( 0x0300, info->io_base + CCAR );
5252 } else {
5253 /* enable external TXD output */
5254 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6));
5255
5256 /* clear Internal Data loopback mode */
5257 info->loopback_bits = 0;
5258 outw( 0,info->io_base + CCAR );
5259 }
5260
5261} /* end of usc_enable_loopback() */
5262
5263/* usc_enable_aux_clock()
5264 *
5265 * Enabled the AUX clock output at the specified frequency.
5266 *
5267 * Arguments:
5268 *
5269 * info pointer to device extension
5270 * data_rate data rate of clock in bits per second
5271 * A data rate of 0 disables the AUX clock.
5272 *
5273 * Return Value: None
5274 */
5275static void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
5276{
5277 u32 XtalSpeed;
5278 u16 Tc;
5279
5280 if ( data_rate ) {
5281 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5282 XtalSpeed = 11059200;
5283 else
5284 XtalSpeed = 14745600;
5285
5286
5287 /* Tc = (Xtal/Speed) - 1 */
5288 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
5289 /* then rounding up gives a more precise time constant. Instead */
5290 /* of rounding up and then subtracting 1 we just don't subtract */
5291 /* the one in this case. */
5292
5293
5294 Tc = (u16)(XtalSpeed/data_rate);
5295 if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
5296 Tc--;
5297
5298 /* Write 16-bit Time Constant for BRG0 */
5299 usc_OutReg( info, TC0R, Tc );
5300
5301 /*
5302 * Hardware Configuration Register (HCR)
5303 * Clear Bit 1, BRG0 mode = Continuous
5304 * Set Bit 0 to enable BRG0.
5305 */
5306
5307 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5308
5309 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5310 usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
5311 } else {
5312 /* data rate == 0 so turn off BRG0 */
5313 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
5314 }
5315
5316} /* end of usc_enable_aux_clock() */
5317
5318/*
5319 *
5320 * usc_process_rxoverrun_sync()
5321 *
5322 * This function processes a receive overrun by resetting the
5323 * receive DMA buffers and issuing a Purge Rx FIFO command
5324 * to allow the receiver to continue receiving.
5325 *
5326 * Arguments:
5327 *
5328 * info pointer to device extension
5329 *
5330 * Return Value: None
5331 */
5332static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
5333{
5334 int start_index;
5335 int end_index;
5336 int frame_start_index;
5337 bool start_of_frame_found = false;
5338 bool end_of_frame_found = false;
5339 bool reprogram_dma = false;
5340
5341 DMABUFFERENTRY *buffer_list = info->rx_buffer_list;
5342 u32 phys_addr;
5343
5344 usc_DmaCmd( info, DmaCmd_PauseRxChannel );
5345 usc_RCmd( info, RCmd_EnterHuntmode );
5346 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5347
5348 /* CurrentRxBuffer points to the 1st buffer of the next */
5349 /* possibly available receive frame. */
5350
5351 frame_start_index = start_index = end_index = info->current_rx_buffer;
5352
5353 /* Search for an unfinished string of buffers. This means */
5354 /* that a receive frame started (at least one buffer with */
5355 /* count set to zero) but there is no terminiting buffer */
5356 /* (status set to non-zero). */
5357
5358 while( !buffer_list[end_index].count )
5359 {
5360 /* Count field has been reset to zero by 16C32. */
5361 /* This buffer is currently in use. */
5362
5363 if ( !start_of_frame_found )
5364 {
5365 start_of_frame_found = true;
5366 frame_start_index = end_index;
5367 end_of_frame_found = false;
5368 }
5369
5370 if ( buffer_list[end_index].status )
5371 {
5372 /* Status field has been set by 16C32. */
5373 /* This is the last buffer of a received frame. */
5374
5375 /* We want to leave the buffers for this frame intact. */
5376 /* Move on to next possible frame. */
5377
5378 start_of_frame_found = false;
5379 end_of_frame_found = true;
5380 }
5381
5382 /* advance to next buffer entry in linked list */
5383 end_index++;
5384 if ( end_index == info->rx_buffer_count )
5385 end_index = 0;
5386
5387 if ( start_index == end_index )
5388 {
5389 /* The entire list has been searched with all Counts == 0 and */
5390 /* all Status == 0. The receive buffers are */
5391 /* completely screwed, reset all receive buffers! */
5392 mgsl_reset_rx_dma_buffers( info );
5393 frame_start_index = 0;
5394 start_of_frame_found = false;
5395 reprogram_dma = true;
5396 break;
5397 }
5398 }
5399
5400 if ( start_of_frame_found && !end_of_frame_found )
5401 {
5402 /* There is an unfinished string of receive DMA buffers */
5403 /* as a result of the receiver overrun. */
5404
5405 /* Reset the buffers for the unfinished frame */
5406 /* and reprogram the receive DMA controller to start */
5407 /* at the 1st buffer of unfinished frame. */
5408
5409 start_index = frame_start_index;
5410
5411 do
5412 {
5413 *((unsigned long *)&(info->rx_buffer_list[start_index++].count)) = DMABUFFERSIZE;
5414
5415 /* Adjust index for wrap around. */
5416 if ( start_index == info->rx_buffer_count )
5417 start_index = 0;
5418
5419 } while( start_index != end_index );
5420
5421 reprogram_dma = true;
5422 }
5423
5424 if ( reprogram_dma )
5425 {
5426 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
5427 usc_ClearIrqPendingBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5428 usc_UnlatchRxstatusBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5429
5430 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5431
5432 /* This empties the receive FIFO and loads the RCC with RCLR */
5433 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5434
5435 /* program 16C32 with physical address of 1st DMA buffer entry */
5436 phys_addr = info->rx_buffer_list[frame_start_index].phys_entry;
5437 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5438 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5439
5440 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5441 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5442 usc_EnableInterrupts( info, RECEIVE_STATUS );
5443
5444 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5445 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5446
5447 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
5448 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5449 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5450 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5451 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5452 else
5453 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5454 }
5455 else
5456 {
5457 /* This empties the receive FIFO and loads the RCC with RCLR */
5458 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5459 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5460 }
5461
5462} /* end of usc_process_rxoverrun_sync() */
5463
5464/* usc_stop_receiver()
5465 *
5466 * Disable USC receiver
5467 *
5468 * Arguments: info pointer to device instance data
5469 * Return Value: None
5470 */
5471static void usc_stop_receiver( struct mgsl_struct *info )
5472{
5473 if (debug_level >= DEBUG_LEVEL_ISR)
5474 printk("%s(%d):usc_stop_receiver(%s)\n",
5475 __FILE__,__LINE__, info->device_name );
5476
5477 /* Disable receive DMA channel. */
5478 /* This also disables receive DMA channel interrupts */
5479 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5480
5481 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5482 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5483 usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS );
5484
5485 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5486
5487 /* This empties the receive FIFO and loads the RCC with RCLR */
5488 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5489 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5490
5491 info->rx_enabled = false;
5492 info->rx_overflow = false;
5493 info->rx_rcc_underrun = false;
5494
5495} /* end of stop_receiver() */
5496
5497/* usc_start_receiver()
5498 *
5499 * Enable the USC receiver
5500 *
5501 * Arguments: info pointer to device instance data
5502 * Return Value: None
5503 */
5504static void usc_start_receiver( struct mgsl_struct *info )
5505{
5506 u32 phys_addr;
5507
5508 if (debug_level >= DEBUG_LEVEL_ISR)
5509 printk("%s(%d):usc_start_receiver(%s)\n",
5510 __FILE__,__LINE__, info->device_name );
5511
5512 mgsl_reset_rx_dma_buffers( info );
5513 usc_stop_receiver( info );
5514
5515 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5516 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5517
5518 if ( info->params.mode == MGSL_MODE_HDLC ||
5519 info->params.mode == MGSL_MODE_RAW ) {
5520 /* DMA mode Transfers */
5521 /* Program the DMA controller. */
5522 /* Enable the DMA controller end of buffer interrupt. */
5523
5524 /* program 16C32 with physical address of 1st DMA buffer entry */
5525 phys_addr = info->rx_buffer_list[0].phys_entry;
5526 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5527 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5528
5529 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5530 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5531 usc_EnableInterrupts( info, RECEIVE_STATUS );
5532
5533 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5534 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5535
5536 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
5537 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5538 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5539 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5540 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5541 else
5542 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5543 } else {
5544 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
5545 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
5546 usc_EnableInterrupts(info, RECEIVE_DATA);
5547
5548 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5549 usc_RCmd( info, RCmd_EnterHuntmode );
5550
5551 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5552 }
5553
5554 usc_OutReg( info, CCSR, 0x1020 );
5555
5556 info->rx_enabled = true;
5557
5558} /* end of usc_start_receiver() */
5559
5560/* usc_start_transmitter()
5561 *
5562 * Enable the USC transmitter and send a transmit frame if
5563 * one is loaded in the DMA buffers.
5564 *
5565 * Arguments: info pointer to device instance data
5566 * Return Value: None
5567 */
5568static void usc_start_transmitter( struct mgsl_struct *info )
5569{
5570 u32 phys_addr;
5571 unsigned int FrameSize;
5572
5573 if (debug_level >= DEBUG_LEVEL_ISR)
5574 printk("%s(%d):usc_start_transmitter(%s)\n",
5575 __FILE__,__LINE__, info->device_name );
5576
5577 if ( info->xmit_cnt ) {
5578
5579 /* If auto RTS enabled and RTS is inactive, then assert */
5580 /* RTS and set a flag indicating that the driver should */
5581 /* negate RTS when the transmission completes. */
5582
5583 info->drop_rts_on_tx_done = false;
5584
5585 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
5586 usc_get_serial_signals( info );
5587 if ( !(info->serial_signals & SerialSignal_RTS) ) {
5588 info->serial_signals |= SerialSignal_RTS;
5589 usc_set_serial_signals( info );
5590 info->drop_rts_on_tx_done = true;
5591 }
5592 }
5593
5594
5595 if ( info->params.mode == MGSL_MODE_ASYNC ) {
5596 if ( !info->tx_active ) {
5597 usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
5598 usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
5599 usc_EnableInterrupts(info, TRANSMIT_DATA);
5600 usc_load_txfifo(info);
5601 }
5602 } else {
5603 /* Disable transmit DMA controller while programming. */
5604 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5605
5606 /* Transmit DMA buffer is loaded, so program USC */
5607 /* to send the frame contained in the buffers. */
5608
5609 FrameSize = info->tx_buffer_list[info->start_tx_dma_buffer].rcc;
5610
5611 /* if operating in Raw sync mode, reset the rcc component
5612 * of the tx dma buffer entry, otherwise, the serial controller
5613 * will send a closing sync char after this count.
5614 */
5615 if ( info->params.mode == MGSL_MODE_RAW )
5616 info->tx_buffer_list[info->start_tx_dma_buffer].rcc = 0;
5617
5618 /* Program the Transmit Character Length Register (TCLR) */
5619 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
5620 usc_OutReg( info, TCLR, (u16)FrameSize );
5621
5622 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5623
5624 /* Program the address of the 1st DMA Buffer Entry in linked list */
5625 phys_addr = info->tx_buffer_list[info->start_tx_dma_buffer].phys_entry;
5626 usc_OutDmaReg( info, NTARL, (u16)phys_addr );
5627 usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
5628
5629 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5630 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5631 usc_EnableInterrupts( info, TRANSMIT_STATUS );
5632
5633 if ( info->params.mode == MGSL_MODE_RAW &&
5634 info->num_tx_dma_buffers > 1 ) {
5635 /* When running external sync mode, attempt to 'stream' transmit */
5636 /* by filling tx dma buffers as they become available. To do this */
5637 /* we need to enable Tx DMA EOB Status interrupts : */
5638 /* */
5639 /* 1. Arm End of Buffer (EOB) Transmit DMA Interrupt (BIT2 of TDIAR) */
5640 /* 2. Enable Transmit DMA Interrupts (BIT0 of DICR) */
5641
5642 usc_OutDmaReg( info, TDIAR, BIT2|BIT3 );
5643 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT0) );
5644 }
5645
5646 /* Initialize Transmit DMA Channel */
5647 usc_DmaCmd( info, DmaCmd_InitTxChannel );
5648
5649 usc_TCmd( info, TCmd_SendFrame );
5650
5651 mod_timer(&info->tx_timer, jiffies +
5652 msecs_to_jiffies(5000));
5653 }
5654 info->tx_active = true;
5655 }
5656
5657 if ( !info->tx_enabled ) {
5658 info->tx_enabled = true;
5659 if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
5660 usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
5661 else
5662 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
5663 }
5664
5665} /* end of usc_start_transmitter() */
5666
5667/* usc_stop_transmitter()
5668 *
5669 * Stops the transmitter and DMA
5670 *
5671 * Arguments: info pointer to device isntance data
5672 * Return Value: None
5673 */
5674static void usc_stop_transmitter( struct mgsl_struct *info )
5675{
5676 if (debug_level >= DEBUG_LEVEL_ISR)
5677 printk("%s(%d):usc_stop_transmitter(%s)\n",
5678 __FILE__,__LINE__, info->device_name );
5679
5680 del_timer(&info->tx_timer);
5681
5682 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5683 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5684 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5685
5686 usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
5687 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5688 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5689
5690 info->tx_enabled = false;
5691 info->tx_active = false;
5692
5693} /* end of usc_stop_transmitter() */
5694
5695/* usc_load_txfifo()
5696 *
5697 * Fill the transmit FIFO until the FIFO is full or
5698 * there is no more data to load.
5699 *
5700 * Arguments: info pointer to device extension (instance data)
5701 * Return Value: None
5702 */
5703static void usc_load_txfifo( struct mgsl_struct *info )
5704{
5705 int Fifocount;
5706 u8 TwoBytes[2];
5707
5708 if ( !info->xmit_cnt && !info->x_char )
5709 return;
5710
5711 /* Select transmit FIFO status readback in TICR */
5712 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
5713
5714 /* load the Transmit FIFO until FIFOs full or all data sent */
5715
5716 while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
5717 /* there is more space in the transmit FIFO and */
5718 /* there is more data in transmit buffer */
5719
5720 if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
5721 /* write a 16-bit word from transmit buffer to 16C32 */
5722
5723 TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
5724 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5725 TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
5726 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5727
5728 outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
5729
5730 info->xmit_cnt -= 2;
5731 info->icount.tx += 2;
5732 } else {
5733 /* only 1 byte left to transmit or 1 FIFO slot left */
5734
5735 outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
5736 info->io_base + CCAR );
5737
5738 if (info->x_char) {
5739 /* transmit pending high priority char */
5740 outw( info->x_char,info->io_base + CCAR );
5741 info->x_char = 0;
5742 } else {
5743 outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
5744 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5745 info->xmit_cnt--;
5746 }
5747 info->icount.tx++;
5748 }
5749 }
5750
5751} /* end of usc_load_txfifo() */
5752
5753/* usc_reset()
5754 *
5755 * Reset the adapter to a known state and prepare it for further use.
5756 *
5757 * Arguments: info pointer to device instance data
5758 * Return Value: None
5759 */
5760static void usc_reset( struct mgsl_struct *info )
5761{
5762 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5763 int i;
5764 u32 readval;
5765
5766 /* Set BIT30 of Misc Control Register */
5767 /* (Local Control Register 0x50) to force reset of USC. */
5768
5769 volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5770 u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
5771
5772 info->misc_ctrl_value |= BIT30;
5773 *MiscCtrl = info->misc_ctrl_value;
5774
5775 /*
5776 * Force at least 170ns delay before clearing
5777 * reset bit. Each read from LCR takes at least
5778 * 30ns so 10 times for 300ns to be safe.
5779 */
5780 for(i=0;i<10;i++)
5781 readval = *MiscCtrl;
5782
5783 info->misc_ctrl_value &= ~BIT30;
5784 *MiscCtrl = info->misc_ctrl_value;
5785
5786 *LCR0BRDR = BUS_DESCRIPTOR(
5787 1, // Write Strobe Hold (0-3)
5788 2, // Write Strobe Delay (0-3)
5789 2, // Read Strobe Delay (0-3)
5790 0, // NWDD (Write data-data) (0-3)
5791 4, // NWAD (Write Addr-data) (0-31)
5792 0, // NXDA (Read/Write Data-Addr) (0-3)
5793 0, // NRDD (Read Data-Data) (0-3)
5794 5 // NRAD (Read Addr-Data) (0-31)
5795 );
5796 } else {
5797 /* do HW reset */
5798 outb( 0,info->io_base + 8 );
5799 }
5800
5801 info->mbre_bit = 0;
5802 info->loopback_bits = 0;
5803 info->usc_idle_mode = 0;
5804
5805 /*
5806 * Program the Bus Configuration Register (BCR)
5807 *
5808 * <15> 0 Don't use separate address
5809 * <14..6> 0 reserved
5810 * <5..4> 00 IAckmode = Default, don't care
5811 * <3> 1 Bus Request Totem Pole output
5812 * <2> 1 Use 16 Bit data bus
5813 * <1> 0 IRQ Totem Pole output
5814 * <0> 0 Don't Shift Right Addr
5815 *
5816 * 0000 0000 0000 1100 = 0x000c
5817 *
5818 * By writing to io_base + SDPIN the Wait/Ack pin is
5819 * programmed to work as a Wait pin.
5820 */
5821
5822 outw( 0x000c,info->io_base + SDPIN );
5823
5824
5825 outw( 0,info->io_base );
5826 outw( 0,info->io_base + CCAR );
5827
5828 /* select little endian byte ordering */
5829 usc_RTCmd( info, RTCmd_SelectLittleEndian );
5830
5831
5832 /* Port Control Register (PCR)
5833 *
5834 * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
5835 * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
5836 * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
5837 * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
5838 * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
5839 * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
5840 * <3..2> 01 Port 1 is Input (Dedicated RxC)
5841 * <1..0> 01 Port 0 is Input (Dedicated TxC)
5842 *
5843 * 1111 0000 1111 0101 = 0xf0f5
5844 */
5845
5846 usc_OutReg( info, PCR, 0xf0f5 );
5847
5848
5849 /*
5850 * Input/Output Control Register
5851 *
5852 * <15..14> 00 CTS is active low input
5853 * <13..12> 00 DCD is active low input
5854 * <11..10> 00 TxREQ pin is input (DSR)
5855 * <9..8> 00 RxREQ pin is input (RI)
5856 * <7..6> 00 TxD is output (Transmit Data)
5857 * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
5858 * <2..0> 100 RxC is Output (drive with BRG0)
5859 *
5860 * 0000 0000 0000 0100 = 0x0004
5861 */
5862
5863 usc_OutReg( info, IOCR, 0x0004 );
5864
5865} /* end of usc_reset() */
5866
5867/* usc_set_async_mode()
5868 *
5869 * Program adapter for asynchronous communications.
5870 *
5871 * Arguments: info pointer to device instance data
5872 * Return Value: None
5873 */
5874static void usc_set_async_mode( struct mgsl_struct *info )
5875{
5876 u16 RegValue;
5877
5878 /* disable interrupts while programming USC */
5879 usc_DisableMasterIrqBit( info );
5880
5881 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5882 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5883
5884 usc_loopback_frame( info );
5885
5886 /* Channel mode Register (CMR)
5887 *
5888 * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
5889 * <13..12> 00 00 = 16X Clock
5890 * <11..8> 0000 Transmitter mode = Asynchronous
5891 * <7..6> 00 reserved?
5892 * <5..4> 00 Rx Sub modes, 00 = 16X Clock
5893 * <3..0> 0000 Receiver mode = Asynchronous
5894 *
5895 * 0000 0000 0000 0000 = 0x0
5896 */
5897
5898 RegValue = 0;
5899 if ( info->params.stop_bits != 1 )
5900 RegValue |= BIT14;
5901 usc_OutReg( info, CMR, RegValue );
5902
5903
5904 /* Receiver mode Register (RMR)
5905 *
5906 * <15..13> 000 encoding = None
5907 * <12..08> 00000 reserved (Sync Only)
5908 * <7..6> 00 Even parity
5909 * <5> 0 parity disabled
5910 * <4..2> 000 Receive Char Length = 8 bits
5911 * <1..0> 00 Disable Receiver
5912 *
5913 * 0000 0000 0000 0000 = 0x0
5914 */
5915
5916 RegValue = 0;
5917
5918 if ( info->params.data_bits != 8 )
5919 RegValue |= BIT4+BIT3+BIT2;
5920
5921 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5922 RegValue |= BIT5;
5923 if ( info->params.parity != ASYNC_PARITY_ODD )
5924 RegValue |= BIT6;
5925 }
5926
5927 usc_OutReg( info, RMR, RegValue );
5928
5929
5930 /* Set IRQ trigger level */
5931
5932 usc_RCmd( info, RCmd_SelectRicrIntLevel );
5933
5934
5935 /* Receive Interrupt Control Register (RICR)
5936 *
5937 * <15..8> ? RxFIFO IRQ Request Level
5938 *
5939 * Note: For async mode the receive FIFO level must be set
5940 * to 0 to avoid the situation where the FIFO contains fewer bytes
5941 * than the trigger level and no more data is expected.
5942 *
5943 * <7> 0 Exited Hunt IA (Interrupt Arm)
5944 * <6> 0 Idle Received IA
5945 * <5> 0 Break/Abort IA
5946 * <4> 0 Rx Bound IA
5947 * <3> 0 Queued status reflects oldest byte in FIFO
5948 * <2> 0 Abort/PE IA
5949 * <1> 0 Rx Overrun IA
5950 * <0> 0 Select TC0 value for readback
5951 *
5952 * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
5953 */
5954
5955 usc_OutReg( info, RICR, 0x0000 );
5956
5957 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5958 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
5959
5960
5961 /* Transmit mode Register (TMR)
5962 *
5963 * <15..13> 000 encoding = None
5964 * <12..08> 00000 reserved (Sync Only)
5965 * <7..6> 00 Transmit parity Even
5966 * <5> 0 Transmit parity Disabled
5967 * <4..2> 000 Tx Char Length = 8 bits
5968 * <1..0> 00 Disable Transmitter
5969 *
5970 * 0000 0000 0000 0000 = 0x0
5971 */
5972
5973 RegValue = 0;
5974
5975 if ( info->params.data_bits != 8 )
5976 RegValue |= BIT4+BIT3+BIT2;
5977
5978 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5979 RegValue |= BIT5;
5980 if ( info->params.parity != ASYNC_PARITY_ODD )
5981 RegValue |= BIT6;
5982 }
5983
5984 usc_OutReg( info, TMR, RegValue );
5985
5986 usc_set_txidle( info );
5987
5988
5989 /* Set IRQ trigger level */
5990
5991 usc_TCmd( info, TCmd_SelectTicrIntLevel );
5992
5993
5994 /* Transmit Interrupt Control Register (TICR)
5995 *
5996 * <15..8> ? Transmit FIFO IRQ Level
5997 * <7> 0 Present IA (Interrupt Arm)
5998 * <6> 1 Idle Sent IA
5999 * <5> 0 Abort Sent IA
6000 * <4> 0 EOF/EOM Sent IA
6001 * <3> 0 CRC Sent IA
6002 * <2> 0 1 = Wait for SW Trigger to Start Frame
6003 * <1> 0 Tx Underrun IA
6004 * <0> 0 TC0 constant on read back
6005 *
6006 * 0000 0000 0100 0000 = 0x0040
6007 */
6008
6009 usc_OutReg( info, TICR, 0x1f40 );
6010
6011 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
6012 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
6013
6014 usc_enable_async_clock( info, info->params.data_rate );
6015
6016
6017 /* Channel Control/status Register (CCSR)
6018 *
6019 * <15> X RCC FIFO Overflow status (RO)
6020 * <14> X RCC FIFO Not Empty status (RO)
6021 * <13> 0 1 = Clear RCC FIFO (WO)
6022 * <12> X DPLL in Sync status (RO)
6023 * <11> X DPLL 2 Missed Clocks status (RO)
6024 * <10> X DPLL 1 Missed Clock status (RO)
6025 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
6026 * <7> X SDLC Loop On status (RO)
6027 * <6> X SDLC Loop Send status (RO)
6028 * <5> 1 Bypass counters for TxClk and RxClk (RW)
6029 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
6030 * <1..0> 00 reserved
6031 *
6032 * 0000 0000 0010 0000 = 0x0020
6033 */
6034
6035 usc_OutReg( info, CCSR, 0x0020 );
6036
6037 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6038 RECEIVE_DATA + RECEIVE_STATUS );
6039
6040 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6041 RECEIVE_DATA + RECEIVE_STATUS );
6042
6043 usc_EnableMasterIrqBit( info );
6044
6045 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6046 /* Enable INTEN (Port 6, Bit12) */
6047 /* This connects the IRQ request signal to the ISA bus */
6048 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6049 }
6050
6051 if (info->params.loopback) {
6052 info->loopback_bits = 0x300;
6053 outw(0x0300, info->io_base + CCAR);
6054 }
6055
6056} /* end of usc_set_async_mode() */
6057
6058/* usc_loopback_frame()
6059 *
6060 * Loop back a small (2 byte) dummy SDLC frame.
6061 * Interrupts and DMA are NOT used. The purpose of this is to
6062 * clear any 'stale' status info left over from running in async mode.
6063 *
6064 * The 16C32 shows the strange behaviour of marking the 1st
6065 * received SDLC frame with a CRC error even when there is no
6066 * CRC error. To get around this a small dummy from of 2 bytes
6067 * is looped back when switching from async to sync mode.
6068 *
6069 * Arguments: info pointer to device instance data
6070 * Return Value: None
6071 */
6072static void usc_loopback_frame( struct mgsl_struct *info )
6073{
6074 int i;
6075 unsigned long oldmode = info->params.mode;
6076
6077 info->params.mode = MGSL_MODE_HDLC;
6078
6079 usc_DisableMasterIrqBit( info );
6080
6081 usc_set_sdlc_mode( info );
6082 usc_enable_loopback( info, 1 );
6083
6084 /* Write 16-bit Time Constant for BRG0 */
6085 usc_OutReg( info, TC0R, 0 );
6086
6087 /* Channel Control Register (CCR)
6088 *
6089 * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
6090 * <13> 0 Trigger Tx on SW Command Disabled
6091 * <12> 0 Flag Preamble Disabled
6092 * <11..10> 00 Preamble Length = 8-Bits
6093 * <9..8> 01 Preamble Pattern = flags
6094 * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
6095 * <5> 0 Trigger Rx on SW Command Disabled
6096 * <4..0> 0 reserved
6097 *
6098 * 0000 0001 0000 0000 = 0x0100
6099 */
6100
6101 usc_OutReg( info, CCR, 0x0100 );
6102
6103 /* SETUP RECEIVER */
6104 usc_RTCmd( info, RTCmd_PurgeRxFifo );
6105 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
6106
6107 /* SETUP TRANSMITTER */
6108 /* Program the Transmit Character Length Register (TCLR) */
6109 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
6110 usc_OutReg( info, TCLR, 2 );
6111 usc_RTCmd( info, RTCmd_PurgeTxFifo );
6112
6113 /* unlatch Tx status bits, and start transmit channel. */
6114 usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
6115 outw(0,info->io_base + DATAREG);
6116
6117 /* ENABLE TRANSMITTER */
6118 usc_TCmd( info, TCmd_SendFrame );
6119 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
6120
6121 /* WAIT FOR RECEIVE COMPLETE */
6122 for (i=0 ; i<1000 ; i++)
6123 if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1))
6124 break;
6125
6126 /* clear Internal Data loopback mode */
6127 usc_enable_loopback(info, 0);
6128
6129 usc_EnableMasterIrqBit(info);
6130
6131 info->params.mode = oldmode;
6132
6133} /* end of usc_loopback_frame() */
6134
6135/* usc_set_sync_mode() Programs the USC for SDLC communications.
6136 *
6137 * Arguments: info pointer to adapter info structure
6138 * Return Value: None
6139 */
6140static void usc_set_sync_mode( struct mgsl_struct *info )
6141{
6142 usc_loopback_frame( info );
6143 usc_set_sdlc_mode( info );
6144
6145 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6146 /* Enable INTEN (Port 6, Bit12) */
6147 /* This connects the IRQ request signal to the ISA bus */
6148 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6149 }
6150
6151 usc_enable_aux_clock(info, info->params.clock_speed);
6152
6153 if (info->params.loopback)
6154 usc_enable_loopback(info,1);
6155
6156} /* end of mgsl_set_sync_mode() */
6157
6158/* usc_set_txidle() Set the HDLC idle mode for the transmitter.
6159 *
6160 * Arguments: info pointer to device instance data
6161 * Return Value: None
6162 */
6163static void usc_set_txidle( struct mgsl_struct *info )
6164{
6165 u16 usc_idle_mode = IDLEMODE_FLAGS;
6166
6167 /* Map API idle mode to USC register bits */
6168
6169 switch( info->idle_mode ){
6170 case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
6171 case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
6172 case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
6173 case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
6174 case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
6175 case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
6176 case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
6177 }
6178
6179 info->usc_idle_mode = usc_idle_mode;
6180 //usc_OutReg(info, TCSR, usc_idle_mode);
6181 info->tcsr_value &= ~IDLEMODE_MASK; /* clear idle mode bits */
6182 info->tcsr_value += usc_idle_mode;
6183 usc_OutReg(info, TCSR, info->tcsr_value);
6184
6185 /*
6186 * if SyncLink WAN adapter is running in external sync mode, the
6187 * transmitter has been set to Monosync in order to try to mimic
6188 * a true raw outbound bit stream. Monosync still sends an open/close
6189 * sync char at the start/end of a frame. Try to match those sync
6190 * patterns to the idle mode set here
6191 */
6192 if ( info->params.mode == MGSL_MODE_RAW ) {
6193 unsigned char syncpat = 0;
6194 switch( info->idle_mode ) {
6195 case HDLC_TXIDLE_FLAGS:
6196 syncpat = 0x7e;
6197 break;
6198 case HDLC_TXIDLE_ALT_ZEROS_ONES:
6199 syncpat = 0x55;
6200 break;
6201 case HDLC_TXIDLE_ZEROS:
6202 case HDLC_TXIDLE_SPACE:
6203 syncpat = 0x00;
6204 break;
6205 case HDLC_TXIDLE_ONES:
6206 case HDLC_TXIDLE_MARK:
6207 syncpat = 0xff;
6208 break;
6209 case HDLC_TXIDLE_ALT_MARK_SPACE:
6210 syncpat = 0xaa;
6211 break;
6212 }
6213
6214 usc_SetTransmitSyncChars(info,syncpat,syncpat);
6215 }
6216
6217} /* end of usc_set_txidle() */
6218
6219/* usc_get_serial_signals()
6220 *
6221 * Query the adapter for the state of the V24 status (input) signals.
6222 *
6223 * Arguments: info pointer to device instance data
6224 * Return Value: None
6225 */
6226static void usc_get_serial_signals( struct mgsl_struct *info )
6227{
6228 u16 status;
6229
6230 /* clear all serial signals except DTR and RTS */
6231 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
6232
6233 /* Read the Misc Interrupt status Register (MISR) to get */
6234 /* the V24 status signals. */
6235
6236 status = usc_InReg( info, MISR );
6237
6238 /* set serial signal bits to reflect MISR */
6239
6240 if ( status & MISCSTATUS_CTS )
6241 info->serial_signals |= SerialSignal_CTS;
6242
6243 if ( status & MISCSTATUS_DCD )
6244 info->serial_signals |= SerialSignal_DCD;
6245
6246 if ( status & MISCSTATUS_RI )
6247 info->serial_signals |= SerialSignal_RI;
6248
6249 if ( status & MISCSTATUS_DSR )
6250 info->serial_signals |= SerialSignal_DSR;
6251
6252} /* end of usc_get_serial_signals() */
6253
6254/* usc_set_serial_signals()
6255 *
6256 * Set the state of DTR and RTS based on contents of
6257 * serial_signals member of device extension.
6258 *
6259 * Arguments: info pointer to device instance data
6260 * Return Value: None
6261 */
6262static void usc_set_serial_signals( struct mgsl_struct *info )
6263{
6264 u16 Control;
6265 unsigned char V24Out = info->serial_signals;
6266
6267 /* get the current value of the Port Control Register (PCR) */
6268
6269 Control = usc_InReg( info, PCR );
6270
6271 if ( V24Out & SerialSignal_RTS )
6272 Control &= ~(BIT6);
6273 else
6274 Control |= BIT6;
6275
6276 if ( V24Out & SerialSignal_DTR )
6277 Control &= ~(BIT4);
6278 else
6279 Control |= BIT4;
6280
6281 usc_OutReg( info, PCR, Control );
6282
6283} /* end of usc_set_serial_signals() */
6284
6285/* usc_enable_async_clock()
6286 *
6287 * Enable the async clock at the specified frequency.
6288 *
6289 * Arguments: info pointer to device instance data
6290 * data_rate data rate of clock in bps
6291 * 0 disables the AUX clock.
6292 * Return Value: None
6293 */
6294static void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
6295{
6296 if ( data_rate ) {
6297 /*
6298 * Clock mode Control Register (CMCR)
6299 *
6300 * <15..14> 00 counter 1 Disabled
6301 * <13..12> 00 counter 0 Disabled
6302 * <11..10> 11 BRG1 Input is TxC Pin
6303 * <9..8> 11 BRG0 Input is TxC Pin
6304 * <7..6> 01 DPLL Input is BRG1 Output
6305 * <5..3> 100 TxCLK comes from BRG0
6306 * <2..0> 100 RxCLK comes from BRG0
6307 *
6308 * 0000 1111 0110 0100 = 0x0f64
6309 */
6310
6311 usc_OutReg( info, CMCR, 0x0f64 );
6312
6313
6314 /*
6315 * Write 16-bit Time Constant for BRG0
6316 * Time Constant = (ClkSpeed / data_rate) - 1
6317 * ClkSpeed = 921600 (ISA), 691200 (PCI)
6318 */
6319
6320 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6321 usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
6322 else
6323 usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
6324
6325
6326 /*
6327 * Hardware Configuration Register (HCR)
6328 * Clear Bit 1, BRG0 mode = Continuous
6329 * Set Bit 0 to enable BRG0.
6330 */
6331
6332 usc_OutReg( info, HCR,
6333 (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
6334
6335
6336 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
6337
6338 usc_OutReg( info, IOCR,
6339 (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
6340 } else {
6341 /* data rate == 0 so turn off BRG0 */
6342 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
6343 }
6344
6345} /* end of usc_enable_async_clock() */
6346
6347/*
6348 * Buffer Structures:
6349 *
6350 * Normal memory access uses virtual addresses that can make discontiguous
6351 * physical memory pages appear to be contiguous in the virtual address
6352 * space (the processors memory mapping handles the conversions).
6353 *
6354 * DMA transfers require physically contiguous memory. This is because
6355 * the DMA system controller and DMA bus masters deal with memory using
6356 * only physical addresses.
6357 *
6358 * This causes a problem under Windows NT when large DMA buffers are
6359 * needed. Fragmentation of the nonpaged pool prevents allocations of
6360 * physically contiguous buffers larger than the PAGE_SIZE.
6361 *
6362 * However the 16C32 supports Bus Master Scatter/Gather DMA which
6363 * allows DMA transfers to physically discontiguous buffers. Information
6364 * about each data transfer buffer is contained in a memory structure
6365 * called a 'buffer entry'. A list of buffer entries is maintained
6366 * to track and control the use of the data transfer buffers.
6367 *
6368 * To support this strategy we will allocate sufficient PAGE_SIZE
6369 * contiguous memory buffers to allow for the total required buffer
6370 * space.
6371 *
6372 * The 16C32 accesses the list of buffer entries using Bus Master
6373 * DMA. Control information is read from the buffer entries by the
6374 * 16C32 to control data transfers. status information is written to
6375 * the buffer entries by the 16C32 to indicate the status of completed
6376 * transfers.
6377 *
6378 * The CPU writes control information to the buffer entries to control
6379 * the 16C32 and reads status information from the buffer entries to
6380 * determine information about received and transmitted frames.
6381 *
6382 * Because the CPU and 16C32 (adapter) both need simultaneous access
6383 * to the buffer entries, the buffer entry memory is allocated with
6384 * HalAllocateCommonBuffer(). This restricts the size of the buffer
6385 * entry list to PAGE_SIZE.
6386 *
6387 * The actual data buffers on the other hand will only be accessed
6388 * by the CPU or the adapter but not by both simultaneously. This allows
6389 * Scatter/Gather packet based DMA procedures for using physically
6390 * discontiguous pages.
6391 */
6392
6393/*
6394 * mgsl_reset_tx_dma_buffers()
6395 *
6396 * Set the count for all transmit buffers to 0 to indicate the
6397 * buffer is available for use and set the current buffer to the
6398 * first buffer. This effectively makes all buffers free and
6399 * discards any data in buffers.
6400 *
6401 * Arguments: info pointer to device instance data
6402 * Return Value: None
6403 */
6404static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info )
6405{
6406 unsigned int i;
6407
6408 for ( i = 0; i < info->tx_buffer_count; i++ ) {
6409 *((unsigned long *)&(info->tx_buffer_list[i].count)) = 0;
6410 }
6411
6412 info->current_tx_buffer = 0;
6413 info->start_tx_dma_buffer = 0;
6414 info->tx_dma_buffers_used = 0;
6415
6416 info->get_tx_holding_index = 0;
6417 info->put_tx_holding_index = 0;
6418 info->tx_holding_count = 0;
6419
6420} /* end of mgsl_reset_tx_dma_buffers() */
6421
6422/*
6423 * num_free_tx_dma_buffers()
6424 *
6425 * returns the number of free tx dma buffers available
6426 *
6427 * Arguments: info pointer to device instance data
6428 * Return Value: number of free tx dma buffers
6429 */
6430static int num_free_tx_dma_buffers(struct mgsl_struct *info)
6431{
6432 return info->tx_buffer_count - info->tx_dma_buffers_used;
6433}
6434
6435/*
6436 * mgsl_reset_rx_dma_buffers()
6437 *
6438 * Set the count for all receive buffers to DMABUFFERSIZE
6439 * and set the current buffer to the first buffer. This effectively
6440 * makes all buffers free and discards any data in buffers.
6441 *
6442 * Arguments: info pointer to device instance data
6443 * Return Value: None
6444 */
6445static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
6446{
6447 unsigned int i;
6448
6449 for ( i = 0; i < info->rx_buffer_count; i++ ) {
6450 *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
6451// info->rx_buffer_list[i].count = DMABUFFERSIZE;
6452// info->rx_buffer_list[i].status = 0;
6453 }
6454
6455 info->current_rx_buffer = 0;
6456
6457} /* end of mgsl_reset_rx_dma_buffers() */
6458
6459/*
6460 * mgsl_free_rx_frame_buffers()
6461 *
6462 * Free the receive buffers used by a received SDLC
6463 * frame such that the buffers can be reused.
6464 *
6465 * Arguments:
6466 *
6467 * info pointer to device instance data
6468 * StartIndex index of 1st receive buffer of frame
6469 * EndIndex index of last receive buffer of frame
6470 *
6471 * Return Value: None
6472 */
6473static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
6474{
6475 bool Done = false;
6476 DMABUFFERENTRY *pBufEntry;
6477 unsigned int Index;
6478
6479 /* Starting with 1st buffer entry of the frame clear the status */
6480 /* field and set the count field to DMA Buffer Size. */
6481
6482 Index = StartIndex;
6483
6484 while( !Done ) {
6485 pBufEntry = &(info->rx_buffer_list[Index]);
6486
6487 if ( Index == EndIndex ) {
6488 /* This is the last buffer of the frame! */
6489 Done = true;
6490 }
6491
6492 /* reset current buffer for reuse */
6493// pBufEntry->status = 0;
6494// pBufEntry->count = DMABUFFERSIZE;
6495 *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
6496
6497 /* advance to next buffer entry in linked list */
6498 Index++;
6499 if ( Index == info->rx_buffer_count )
6500 Index = 0;
6501 }
6502
6503 /* set current buffer to next buffer after last buffer of frame */
6504 info->current_rx_buffer = Index;
6505
6506} /* end of free_rx_frame_buffers() */
6507
6508/* mgsl_get_rx_frame()
6509 *
6510 * This function attempts to return a received SDLC frame from the
6511 * receive DMA buffers. Only frames received without errors are returned.
6512 *
6513 * Arguments: info pointer to device extension
6514 * Return Value: true if frame returned, otherwise false
6515 */
6516static bool mgsl_get_rx_frame(struct mgsl_struct *info)
6517{
6518 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
6519 unsigned short status;
6520 DMABUFFERENTRY *pBufEntry;
6521 unsigned int framesize = 0;
6522 bool ReturnCode = false;
6523 unsigned long flags;
6524 struct tty_struct *tty = info->port.tty;
6525 bool return_frame = false;
6526
6527 /*
6528 * current_rx_buffer points to the 1st buffer of the next available
6529 * receive frame. To find the last buffer of the frame look for
6530 * a non-zero status field in the buffer entries. (The status
6531 * field is set by the 16C32 after completing a receive frame.
6532 */
6533
6534 StartIndex = EndIndex = info->current_rx_buffer;
6535
6536 while( !info->rx_buffer_list[EndIndex].status ) {
6537 /*
6538 * If the count field of the buffer entry is non-zero then
6539 * this buffer has not been used. (The 16C32 clears the count
6540 * field when it starts using the buffer.) If an unused buffer
6541 * is encountered then there are no frames available.
6542 */
6543
6544 if ( info->rx_buffer_list[EndIndex].count )
6545 goto Cleanup;
6546
6547 /* advance to next buffer entry in linked list */
6548 EndIndex++;
6549 if ( EndIndex == info->rx_buffer_count )
6550 EndIndex = 0;
6551
6552 /* if entire list searched then no frame available */
6553 if ( EndIndex == StartIndex ) {
6554 /* If this occurs then something bad happened,
6555 * all buffers have been 'used' but none mark
6556 * the end of a frame. Reset buffers and receiver.
6557 */
6558
6559 if ( info->rx_enabled ){
6560 spin_lock_irqsave(&info->irq_spinlock,flags);
6561 usc_start_receiver(info);
6562 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6563 }
6564 goto Cleanup;
6565 }
6566 }
6567
6568
6569 /* check status of receive frame */
6570
6571 status = info->rx_buffer_list[EndIndex].status;
6572
6573 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
6574 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
6575 if ( status & RXSTATUS_SHORT_FRAME )
6576 info->icount.rxshort++;
6577 else if ( status & RXSTATUS_ABORT )
6578 info->icount.rxabort++;
6579 else if ( status & RXSTATUS_OVERRUN )
6580 info->icount.rxover++;
6581 else {
6582 info->icount.rxcrc++;
6583 if ( info->params.crc_type & HDLC_CRC_RETURN_EX )
6584 return_frame = true;
6585 }
6586 framesize = 0;
6587#if SYNCLINK_GENERIC_HDLC
6588 {
6589 info->netdev->stats.rx_errors++;
6590 info->netdev->stats.rx_frame_errors++;
6591 }
6592#endif
6593 } else
6594 return_frame = true;
6595
6596 if ( return_frame ) {
6597 /* receive frame has no errors, get frame size.
6598 * The frame size is the starting value of the RCC (which was
6599 * set to 0xffff) minus the ending value of the RCC (decremented
6600 * once for each receive character) minus 2 for the 16-bit CRC.
6601 */
6602
6603 framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
6604
6605 /* adjust frame size for CRC if any */
6606 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
6607 framesize -= 2;
6608 else if ( info->params.crc_type == HDLC_CRC_32_CCITT )
6609 framesize -= 4;
6610 }
6611
6612 if ( debug_level >= DEBUG_LEVEL_BH )
6613 printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
6614 __FILE__,__LINE__,info->device_name,status,framesize);
6615
6616 if ( debug_level >= DEBUG_LEVEL_DATA )
6617 mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
6618 min_t(int, framesize, DMABUFFERSIZE),0);
6619
6620 if (framesize) {
6621 if ( ( (info->params.crc_type & HDLC_CRC_RETURN_EX) &&
6622 ((framesize+1) > info->max_frame_size) ) ||
6623 (framesize > info->max_frame_size) )
6624 info->icount.rxlong++;
6625 else {
6626 /* copy dma buffer(s) to contiguous intermediate buffer */
6627 int copy_count = framesize;
6628 int index = StartIndex;
6629 unsigned char *ptmp = info->intermediate_rxbuffer;
6630
6631 if ( !(status & RXSTATUS_CRC_ERROR))
6632 info->icount.rxok++;
6633
6634 while(copy_count) {
6635 int partial_count;
6636 if ( copy_count > DMABUFFERSIZE )
6637 partial_count = DMABUFFERSIZE;
6638 else
6639 partial_count = copy_count;
6640
6641 pBufEntry = &(info->rx_buffer_list[index]);
6642 memcpy( ptmp, pBufEntry->virt_addr, partial_count );
6643 ptmp += partial_count;
6644 copy_count -= partial_count;
6645
6646 if ( ++index == info->rx_buffer_count )
6647 index = 0;
6648 }
6649
6650 if ( info->params.crc_type & HDLC_CRC_RETURN_EX ) {
6651 ++framesize;
6652 *ptmp = (status & RXSTATUS_CRC_ERROR ?
6653 RX_CRC_ERROR :
6654 RX_OK);
6655
6656 if ( debug_level >= DEBUG_LEVEL_DATA )
6657 printk("%s(%d):mgsl_get_rx_frame(%s) rx frame status=%d\n",
6658 __FILE__,__LINE__,info->device_name,
6659 *ptmp);
6660 }
6661
6662#if SYNCLINK_GENERIC_HDLC
6663 if (info->netcount)
6664 hdlcdev_rx(info,info->intermediate_rxbuffer,framesize);
6665 else
6666#endif
6667 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6668 }
6669 }
6670 /* Free the buffers used by this frame. */
6671 mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
6672
6673 ReturnCode = true;
6674
6675Cleanup:
6676
6677 if ( info->rx_enabled && info->rx_overflow ) {
6678 /* The receiver needs to restarted because of
6679 * a receive overflow (buffer or FIFO). If the
6680 * receive buffers are now empty, then restart receiver.
6681 */
6682
6683 if ( !info->rx_buffer_list[EndIndex].status &&
6684 info->rx_buffer_list[EndIndex].count ) {
6685 spin_lock_irqsave(&info->irq_spinlock,flags);
6686 usc_start_receiver(info);
6687 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6688 }
6689 }
6690
6691 return ReturnCode;
6692
6693} /* end of mgsl_get_rx_frame() */
6694
6695/* mgsl_get_raw_rx_frame()
6696 *
6697 * This function attempts to return a received frame from the
6698 * receive DMA buffers when running in external loop mode. In this mode,
6699 * we will return at most one DMABUFFERSIZE frame to the application.
6700 * The USC receiver is triggering off of DCD going active to start a new
6701 * frame, and DCD going inactive to terminate the frame (similar to
6702 * processing a closing flag character).
6703 *
6704 * In this routine, we will return DMABUFFERSIZE "chunks" at a time.
6705 * If DCD goes inactive, the last Rx DMA Buffer will have a non-zero
6706 * status field and the RCC field will indicate the length of the
6707 * entire received frame. We take this RCC field and get the modulus
6708 * of RCC and DMABUFFERSIZE to determine if number of bytes in the
6709 * last Rx DMA buffer and return that last portion of the frame.
6710 *
6711 * Arguments: info pointer to device extension
6712 * Return Value: true if frame returned, otherwise false
6713 */
6714static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
6715{
6716 unsigned int CurrentIndex, NextIndex;
6717 unsigned short status;
6718 DMABUFFERENTRY *pBufEntry;
6719 unsigned int framesize = 0;
6720 bool ReturnCode = false;
6721 unsigned long flags;
6722 struct tty_struct *tty = info->port.tty;
6723
6724 /*
6725 * current_rx_buffer points to the 1st buffer of the next available
6726 * receive frame. The status field is set by the 16C32 after
6727 * completing a receive frame. If the status field of this buffer
6728 * is zero, either the USC is still filling this buffer or this
6729 * is one of a series of buffers making up a received frame.
6730 *
6731 * If the count field of this buffer is zero, the USC is either
6732 * using this buffer or has used this buffer. Look at the count
6733 * field of the next buffer. If that next buffer's count is
6734 * non-zero, the USC is still actively using the current buffer.
6735 * Otherwise, if the next buffer's count field is zero, the
6736 * current buffer is complete and the USC is using the next
6737 * buffer.
6738 */
6739 CurrentIndex = NextIndex = info->current_rx_buffer;
6740 ++NextIndex;
6741 if ( NextIndex == info->rx_buffer_count )
6742 NextIndex = 0;
6743
6744 if ( info->rx_buffer_list[CurrentIndex].status != 0 ||
6745 (info->rx_buffer_list[CurrentIndex].count == 0 &&
6746 info->rx_buffer_list[NextIndex].count == 0)) {
6747 /*
6748 * Either the status field of this dma buffer is non-zero
6749 * (indicating the last buffer of a receive frame) or the next
6750 * buffer is marked as in use -- implying this buffer is complete
6751 * and an intermediate buffer for this received frame.
6752 */
6753
6754 status = info->rx_buffer_list[CurrentIndex].status;
6755
6756 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
6757 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
6758 if ( status & RXSTATUS_SHORT_FRAME )
6759 info->icount.rxshort++;
6760 else if ( status & RXSTATUS_ABORT )
6761 info->icount.rxabort++;
6762 else if ( status & RXSTATUS_OVERRUN )
6763 info->icount.rxover++;
6764 else
6765 info->icount.rxcrc++;
6766 framesize = 0;
6767 } else {
6768 /*
6769 * A receive frame is available, get frame size and status.
6770 *
6771 * The frame size is the starting value of the RCC (which was
6772 * set to 0xffff) minus the ending value of the RCC (decremented
6773 * once for each receive character) minus 2 or 4 for the 16-bit
6774 * or 32-bit CRC.
6775 *
6776 * If the status field is zero, this is an intermediate buffer.
6777 * It's size is 4K.
6778 *
6779 * If the DMA Buffer Entry's Status field is non-zero, the
6780 * receive operation completed normally (ie: DCD dropped). The
6781 * RCC field is valid and holds the received frame size.
6782 * It is possible that the RCC field will be zero on a DMA buffer
6783 * entry with a non-zero status. This can occur if the total
6784 * frame size (number of bytes between the time DCD goes active
6785 * to the time DCD goes inactive) exceeds 65535 bytes. In this
6786 * case the 16C32 has underrun on the RCC count and appears to
6787 * stop updating this counter to let us know the actual received
6788 * frame size. If this happens (non-zero status and zero RCC),
6789 * simply return the entire RxDMA Buffer
6790 */
6791 if ( status ) {
6792 /*
6793 * In the event that the final RxDMA Buffer is
6794 * terminated with a non-zero status and the RCC
6795 * field is zero, we interpret this as the RCC
6796 * having underflowed (received frame > 65535 bytes).
6797 *
6798 * Signal the event to the user by passing back
6799 * a status of RxStatus_CrcError returning the full
6800 * buffer and let the app figure out what data is
6801 * actually valid
6802 */
6803 if ( info->rx_buffer_list[CurrentIndex].rcc )
6804 framesize = RCLRVALUE - info->rx_buffer_list[CurrentIndex].rcc;
6805 else
6806 framesize = DMABUFFERSIZE;
6807 }
6808 else
6809 framesize = DMABUFFERSIZE;
6810 }
6811
6812 if ( framesize > DMABUFFERSIZE ) {
6813 /*
6814 * if running in raw sync mode, ISR handler for
6815 * End Of Buffer events terminates all buffers at 4K.
6816 * If this frame size is said to be >4K, get the
6817 * actual number of bytes of the frame in this buffer.
6818 */
6819 framesize = framesize % DMABUFFERSIZE;
6820 }
6821
6822
6823 if ( debug_level >= DEBUG_LEVEL_BH )
6824 printk("%s(%d):mgsl_get_raw_rx_frame(%s) status=%04X size=%d\n",
6825 __FILE__,__LINE__,info->device_name,status,framesize);
6826
6827 if ( debug_level >= DEBUG_LEVEL_DATA )
6828 mgsl_trace_block(info,info->rx_buffer_list[CurrentIndex].virt_addr,
6829 min_t(int, framesize, DMABUFFERSIZE),0);
6830
6831 if (framesize) {
6832 /* copy dma buffer(s) to contiguous intermediate buffer */
6833 /* NOTE: we never copy more than DMABUFFERSIZE bytes */
6834
6835 pBufEntry = &(info->rx_buffer_list[CurrentIndex]);
6836 memcpy( info->intermediate_rxbuffer, pBufEntry->virt_addr, framesize);
6837 info->icount.rxok++;
6838
6839 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6840 }
6841
6842 /* Free the buffers used by this frame. */
6843 mgsl_free_rx_frame_buffers( info, CurrentIndex, CurrentIndex );
6844
6845 ReturnCode = true;
6846 }
6847
6848
6849 if ( info->rx_enabled && info->rx_overflow ) {
6850 /* The receiver needs to restarted because of
6851 * a receive overflow (buffer or FIFO). If the
6852 * receive buffers are now empty, then restart receiver.
6853 */
6854
6855 if ( !info->rx_buffer_list[CurrentIndex].status &&
6856 info->rx_buffer_list[CurrentIndex].count ) {
6857 spin_lock_irqsave(&info->irq_spinlock,flags);
6858 usc_start_receiver(info);
6859 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6860 }
6861 }
6862
6863 return ReturnCode;
6864
6865} /* end of mgsl_get_raw_rx_frame() */
6866
6867/* mgsl_load_tx_dma_buffer()
6868 *
6869 * Load the transmit DMA buffer with the specified data.
6870 *
6871 * Arguments:
6872 *
6873 * info pointer to device extension
6874 * Buffer pointer to buffer containing frame to load
6875 * BufferSize size in bytes of frame in Buffer
6876 *
6877 * Return Value: None
6878 */
6879static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
6880 const char *Buffer, unsigned int BufferSize)
6881{
6882 unsigned short Copycount;
6883 unsigned int i = 0;
6884 DMABUFFERENTRY *pBufEntry;
6885
6886 if ( debug_level >= DEBUG_LEVEL_DATA )
6887 mgsl_trace_block(info,Buffer, min_t(int, BufferSize, DMABUFFERSIZE), 1);
6888
6889 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
6890 /* set CMR:13 to start transmit when
6891 * next GoAhead (abort) is received
6892 */
6893 info->cmr_value |= BIT13;
6894 }
6895
6896 /* begin loading the frame in the next available tx dma
6897 * buffer, remember it's starting location for setting
6898 * up tx dma operation
6899 */
6900 i = info->current_tx_buffer;
6901 info->start_tx_dma_buffer = i;
6902
6903 /* Setup the status and RCC (Frame Size) fields of the 1st */
6904 /* buffer entry in the transmit DMA buffer list. */
6905
6906 info->tx_buffer_list[i].status = info->cmr_value & 0xf000;
6907 info->tx_buffer_list[i].rcc = BufferSize;
6908 info->tx_buffer_list[i].count = BufferSize;
6909
6910 /* Copy frame data from 1st source buffer to the DMA buffers. */
6911 /* The frame data may span multiple DMA buffers. */
6912
6913 while( BufferSize ){
6914 /* Get a pointer to next DMA buffer entry. */
6915 pBufEntry = &info->tx_buffer_list[i++];
6916
6917 if ( i == info->tx_buffer_count )
6918 i=0;
6919
6920 /* Calculate the number of bytes that can be copied from */
6921 /* the source buffer to this DMA buffer. */
6922 if ( BufferSize > DMABUFFERSIZE )
6923 Copycount = DMABUFFERSIZE;
6924 else
6925 Copycount = BufferSize;
6926
6927 /* Actually copy data from source buffer to DMA buffer. */
6928 /* Also set the data count for this individual DMA buffer. */
6929 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6930 mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
6931 else
6932 memcpy(pBufEntry->virt_addr, Buffer, Copycount);
6933
6934 pBufEntry->count = Copycount;
6935
6936 /* Advance source pointer and reduce remaining data count. */
6937 Buffer += Copycount;
6938 BufferSize -= Copycount;
6939
6940 ++info->tx_dma_buffers_used;
6941 }
6942
6943 /* remember next available tx dma buffer */
6944 info->current_tx_buffer = i;
6945
6946} /* end of mgsl_load_tx_dma_buffer() */
6947
6948/*
6949 * mgsl_register_test()
6950 *
6951 * Performs a register test of the 16C32.
6952 *
6953 * Arguments: info pointer to device instance data
6954 * Return Value: true if test passed, otherwise false
6955 */
6956static bool mgsl_register_test( struct mgsl_struct *info )
6957{
6958 static unsigned short BitPatterns[] =
6959 { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
6960 static unsigned int Patterncount = ARRAY_SIZE(BitPatterns);
6961 unsigned int i;
6962 bool rc = true;
6963 unsigned long flags;
6964
6965 spin_lock_irqsave(&info->irq_spinlock,flags);
6966 usc_reset(info);
6967
6968 /* Verify the reset state of some registers. */
6969
6970 if ( (usc_InReg( info, SICR ) != 0) ||
6971 (usc_InReg( info, IVR ) != 0) ||
6972 (usc_InDmaReg( info, DIVR ) != 0) ){
6973 rc = false;
6974 }
6975
6976 if ( rc ){
6977 /* Write bit patterns to various registers but do it out of */
6978 /* sync, then read back and verify values. */
6979
6980 for ( i = 0 ; i < Patterncount ; i++ ) {
6981 usc_OutReg( info, TC0R, BitPatterns[i] );
6982 usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
6983 usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
6984 usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
6985 usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
6986 usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
6987
6988 if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
6989 (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
6990 (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
6991 (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
6992 (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
6993 (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
6994 rc = false;
6995 break;
6996 }
6997 }
6998 }
6999
7000 usc_reset(info);
7001 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7002
7003 return rc;
7004
7005} /* end of mgsl_register_test() */
7006
7007/* mgsl_irq_test() Perform interrupt test of the 16C32.
7008 *
7009 * Arguments: info pointer to device instance data
7010 * Return Value: true if test passed, otherwise false
7011 */
7012static bool mgsl_irq_test( struct mgsl_struct *info )
7013{
7014 unsigned long EndTime;
7015 unsigned long flags;
7016
7017 spin_lock_irqsave(&info->irq_spinlock,flags);
7018 usc_reset(info);
7019
7020 /*
7021 * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
7022 * The ISR sets irq_occurred to true.
7023 */
7024
7025 info->irq_occurred = false;
7026
7027 /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
7028 /* Enable INTEN (Port 6, Bit12) */
7029 /* This connects the IRQ request signal to the ISA bus */
7030 /* on the ISA adapter. This has no effect for the PCI adapter */
7031 usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
7032
7033 usc_EnableMasterIrqBit(info);
7034 usc_EnableInterrupts(info, IO_PIN);
7035 usc_ClearIrqPendingBits(info, IO_PIN);
7036
7037 usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
7038 usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
7039
7040 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7041
7042 EndTime=100;
7043 while( EndTime-- && !info->irq_occurred ) {
7044 msleep_interruptible(10);
7045 }
7046
7047 spin_lock_irqsave(&info->irq_spinlock,flags);
7048 usc_reset(info);
7049 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7050
7051 return info->irq_occurred;
7052
7053} /* end of mgsl_irq_test() */
7054
7055/* mgsl_dma_test()
7056 *
7057 * Perform a DMA test of the 16C32. A small frame is
7058 * transmitted via DMA from a transmit buffer to a receive buffer
7059 * using single buffer DMA mode.
7060 *
7061 * Arguments: info pointer to device instance data
7062 * Return Value: true if test passed, otherwise false
7063 */
7064static bool mgsl_dma_test( struct mgsl_struct *info )
7065{
7066 unsigned short FifoLevel;
7067 unsigned long phys_addr;
7068 unsigned int FrameSize;
7069 unsigned int i;
7070 char *TmpPtr;
7071 bool rc = true;
7072 unsigned short status=0;
7073 unsigned long EndTime;
7074 unsigned long flags;
7075 MGSL_PARAMS tmp_params;
7076
7077 /* save current port options */
7078 memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
7079 /* load default port options */
7080 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
7081
7082#define TESTFRAMESIZE 40
7083
7084 spin_lock_irqsave(&info->irq_spinlock,flags);
7085
7086 /* setup 16C32 for SDLC DMA transfer mode */
7087
7088 usc_reset(info);
7089 usc_set_sdlc_mode(info);
7090 usc_enable_loopback(info,1);
7091
7092 /* Reprogram the RDMR so that the 16C32 does NOT clear the count
7093 * field of the buffer entry after fetching buffer address. This
7094 * way we can detect a DMA failure for a DMA read (which should be
7095 * non-destructive to system memory) before we try and write to
7096 * memory (where a failure could corrupt system memory).
7097 */
7098
7099 /* Receive DMA mode Register (RDMR)
7100 *
7101 * <15..14> 11 DMA mode = Linked List Buffer mode
7102 * <13> 1 RSBinA/L = store Rx status Block in List entry
7103 * <12> 0 1 = Clear count of List Entry after fetching
7104 * <11..10> 00 Address mode = Increment
7105 * <9> 1 Terminate Buffer on RxBound
7106 * <8> 0 Bus Width = 16bits
7107 * <7..0> ? status Bits (write as 0s)
7108 *
7109 * 1110 0010 0000 0000 = 0xe200
7110 */
7111
7112 usc_OutDmaReg( info, RDMR, 0xe200 );
7113
7114 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7115
7116
7117 /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
7118
7119 FrameSize = TESTFRAMESIZE;
7120
7121 /* setup 1st transmit buffer entry: */
7122 /* with frame size and transmit control word */
7123
7124 info->tx_buffer_list[0].count = FrameSize;
7125 info->tx_buffer_list[0].rcc = FrameSize;
7126 info->tx_buffer_list[0].status = 0x4000;
7127
7128 /* build a transmit frame in 1st transmit DMA buffer */
7129
7130 TmpPtr = info->tx_buffer_list[0].virt_addr;
7131 for (i = 0; i < FrameSize; i++ )
7132 *TmpPtr++ = i;
7133
7134 /* setup 1st receive buffer entry: */
7135 /* clear status, set max receive buffer size */
7136
7137 info->rx_buffer_list[0].status = 0;
7138 info->rx_buffer_list[0].count = FrameSize + 4;
7139
7140 /* zero out the 1st receive buffer */
7141
7142 memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
7143
7144 /* Set count field of next buffer entries to prevent */
7145 /* 16C32 from using buffers after the 1st one. */
7146
7147 info->tx_buffer_list[1].count = 0;
7148 info->rx_buffer_list[1].count = 0;
7149
7150
7151 /***************************/
7152 /* Program 16C32 receiver. */
7153 /***************************/
7154
7155 spin_lock_irqsave(&info->irq_spinlock,flags);
7156
7157 /* setup DMA transfers */
7158 usc_RTCmd( info, RTCmd_PurgeRxFifo );
7159
7160 /* program 16C32 receiver with physical address of 1st DMA buffer entry */
7161 phys_addr = info->rx_buffer_list[0].phys_entry;
7162 usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
7163 usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
7164
7165 /* Clear the Rx DMA status bits (read RDMR) and start channel */
7166 usc_InDmaReg( info, RDMR );
7167 usc_DmaCmd( info, DmaCmd_InitRxChannel );
7168
7169 /* Enable Receiver (RMR <1..0> = 10) */
7170 usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
7171
7172 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7173
7174
7175 /*************************************************************/
7176 /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
7177 /*************************************************************/
7178
7179 /* Wait 100ms for interrupt. */
7180 EndTime = jiffies + msecs_to_jiffies(100);
7181
7182 for(;;) {
7183 if (time_after(jiffies, EndTime)) {
7184 rc = false;
7185 break;
7186 }
7187
7188 spin_lock_irqsave(&info->irq_spinlock,flags);
7189 status = usc_InDmaReg( info, RDMR );
7190 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7191
7192 if ( !(status & BIT4) && (status & BIT5) ) {
7193 /* INITG (BIT 4) is inactive (no entry read in progress) AND */
7194 /* BUSY (BIT 5) is active (channel still active). */
7195 /* This means the buffer entry read has completed. */
7196 break;
7197 }
7198 }
7199
7200
7201 /******************************/
7202 /* Program 16C32 transmitter. */
7203 /******************************/
7204
7205 spin_lock_irqsave(&info->irq_spinlock,flags);
7206
7207 /* Program the Transmit Character Length Register (TCLR) */
7208 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
7209
7210 usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
7211 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7212
7213 /* Program the address of the 1st DMA Buffer Entry in linked list */
7214
7215 phys_addr = info->tx_buffer_list[0].phys_entry;
7216 usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
7217 usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
7218
7219 /* unlatch Tx status bits, and start transmit channel. */
7220
7221 usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0f00) | 0xfa) );
7222 usc_DmaCmd( info, DmaCmd_InitTxChannel );
7223
7224 /* wait for DMA controller to fill transmit FIFO */
7225
7226 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
7227
7228 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7229
7230
7231 /**********************************/
7232 /* WAIT FOR TRANSMIT FIFO TO FILL */
7233 /**********************************/
7234
7235 /* Wait 100ms */
7236 EndTime = jiffies + msecs_to_jiffies(100);
7237
7238 for(;;) {
7239 if (time_after(jiffies, EndTime)) {
7240 rc = false;
7241 break;
7242 }
7243
7244 spin_lock_irqsave(&info->irq_spinlock,flags);
7245 FifoLevel = usc_InReg(info, TICR) >> 8;
7246 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7247
7248 if ( FifoLevel < 16 )
7249 break;
7250 else
7251 if ( FrameSize < 32 ) {
7252 /* This frame is smaller than the entire transmit FIFO */
7253 /* so wait for the entire frame to be loaded. */
7254 if ( FifoLevel <= (32 - FrameSize) )
7255 break;
7256 }
7257 }
7258
7259
7260 if ( rc )
7261 {
7262 /* Enable 16C32 transmitter. */
7263
7264 spin_lock_irqsave(&info->irq_spinlock,flags);
7265
7266 /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
7267 usc_TCmd( info, TCmd_SendFrame );
7268 usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
7269
7270 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7271
7272
7273 /******************************/
7274 /* WAIT FOR TRANSMIT COMPLETE */
7275 /******************************/
7276
7277 /* Wait 100ms */
7278 EndTime = jiffies + msecs_to_jiffies(100);
7279
7280 /* While timer not expired wait for transmit complete */
7281
7282 spin_lock_irqsave(&info->irq_spinlock,flags);
7283 status = usc_InReg( info, TCSR );
7284 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7285
7286 while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) {
7287 if (time_after(jiffies, EndTime)) {
7288 rc = false;
7289 break;
7290 }
7291
7292 spin_lock_irqsave(&info->irq_spinlock,flags);
7293 status = usc_InReg( info, TCSR );
7294 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7295 }
7296 }
7297
7298
7299 if ( rc ){
7300 /* CHECK FOR TRANSMIT ERRORS */
7301 if ( status & (BIT5 + BIT1) )
7302 rc = false;
7303 }
7304
7305 if ( rc ) {
7306 /* WAIT FOR RECEIVE COMPLETE */
7307
7308 /* Wait 100ms */
7309 EndTime = jiffies + msecs_to_jiffies(100);
7310
7311 /* Wait for 16C32 to write receive status to buffer entry. */
7312 status=info->rx_buffer_list[0].status;
7313 while ( status == 0 ) {
7314 if (time_after(jiffies, EndTime)) {
7315 rc = false;
7316 break;
7317 }
7318 status=info->rx_buffer_list[0].status;
7319 }
7320 }
7321
7322
7323 if ( rc ) {
7324 /* CHECK FOR RECEIVE ERRORS */
7325 status = info->rx_buffer_list[0].status;
7326
7327 if ( status & (BIT8 + BIT3 + BIT1) ) {
7328 /* receive error has occurred */
7329 rc = false;
7330 } else {
7331 if ( memcmp( info->tx_buffer_list[0].virt_addr ,
7332 info->rx_buffer_list[0].virt_addr, FrameSize ) ){
7333 rc = false;
7334 }
7335 }
7336 }
7337
7338 spin_lock_irqsave(&info->irq_spinlock,flags);
7339 usc_reset( info );
7340 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7341
7342 /* restore current port options */
7343 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
7344
7345 return rc;
7346
7347} /* end of mgsl_dma_test() */
7348
7349/* mgsl_adapter_test()
7350 *
7351 * Perform the register, IRQ, and DMA tests for the 16C32.
7352 *
7353 * Arguments: info pointer to device instance data
7354 * Return Value: 0 if success, otherwise -ENODEV
7355 */
7356static int mgsl_adapter_test( struct mgsl_struct *info )
7357{
7358 if ( debug_level >= DEBUG_LEVEL_INFO )
7359 printk( "%s(%d):Testing device %s\n",
7360 __FILE__,__LINE__,info->device_name );
7361
7362 if ( !mgsl_register_test( info ) ) {
7363 info->init_error = DiagStatus_AddressFailure;
7364 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
7365 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
7366 return -ENODEV;
7367 }
7368
7369 if ( !mgsl_irq_test( info ) ) {
7370 info->init_error = DiagStatus_IrqFailure;
7371 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
7372 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
7373 return -ENODEV;
7374 }
7375
7376 if ( !mgsl_dma_test( info ) ) {
7377 info->init_error = DiagStatus_DmaFailure;
7378 printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
7379 __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
7380 return -ENODEV;
7381 }
7382
7383 if ( debug_level >= DEBUG_LEVEL_INFO )
7384 printk( "%s(%d):device %s passed diagnostics\n",
7385 __FILE__,__LINE__,info->device_name );
7386
7387 return 0;
7388
7389} /* end of mgsl_adapter_test() */
7390
7391/* mgsl_memory_test()
7392 *
7393 * Test the shared memory on a PCI adapter.
7394 *
7395 * Arguments: info pointer to device instance data
7396 * Return Value: true if test passed, otherwise false
7397 */
7398static bool mgsl_memory_test( struct mgsl_struct *info )
7399{
7400 static unsigned long BitPatterns[] =
7401 { 0x0, 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
7402 unsigned long Patterncount = ARRAY_SIZE(BitPatterns);
7403 unsigned long i;
7404 unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
7405 unsigned long * TestAddr;
7406
7407 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
7408 return true;
7409
7410 TestAddr = (unsigned long *)info->memory_base;
7411
7412 /* Test data lines with test pattern at one location. */
7413
7414 for ( i = 0 ; i < Patterncount ; i++ ) {
7415 *TestAddr = BitPatterns[i];
7416 if ( *TestAddr != BitPatterns[i] )
7417 return false;
7418 }
7419
7420 /* Test address lines with incrementing pattern over */
7421 /* entire address range. */
7422
7423 for ( i = 0 ; i < TestLimit ; i++ ) {
7424 *TestAddr = i * 4;
7425 TestAddr++;
7426 }
7427
7428 TestAddr = (unsigned long *)info->memory_base;
7429
7430 for ( i = 0 ; i < TestLimit ; i++ ) {
7431 if ( *TestAddr != i * 4 )
7432 return false;
7433 TestAddr++;
7434 }
7435
7436 memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
7437
7438 return true;
7439
7440} /* End Of mgsl_memory_test() */
7441
7442
7443/* mgsl_load_pci_memory()
7444 *
7445 * Load a large block of data into the PCI shared memory.
7446 * Use this instead of memcpy() or memmove() to move data
7447 * into the PCI shared memory.
7448 *
7449 * Notes:
7450 *
7451 * This function prevents the PCI9050 interface chip from hogging
7452 * the adapter local bus, which can starve the 16C32 by preventing
7453 * 16C32 bus master cycles.
7454 *
7455 * The PCI9050 documentation says that the 9050 will always release
7456 * control of the local bus after completing the current read
7457 * or write operation.
7458 *
7459 * It appears that as long as the PCI9050 write FIFO is full, the
7460 * PCI9050 treats all of the writes as a single burst transaction
7461 * and will not release the bus. This causes DMA latency problems
7462 * at high speeds when copying large data blocks to the shared
7463 * memory.
7464 *
7465 * This function in effect, breaks the a large shared memory write
7466 * into multiple transations by interleaving a shared memory read
7467 * which will flush the write FIFO and 'complete' the write
7468 * transation. This allows any pending DMA request to gain control
7469 * of the local bus in a timely fasion.
7470 *
7471 * Arguments:
7472 *
7473 * TargetPtr pointer to target address in PCI shared memory
7474 * SourcePtr pointer to source buffer for data
7475 * count count in bytes of data to copy
7476 *
7477 * Return Value: None
7478 */
7479static void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
7480 unsigned short count )
7481{
7482 /* 16 32-bit writes @ 60ns each = 960ns max latency on local bus */
7483#define PCI_LOAD_INTERVAL 64
7484
7485 unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
7486 unsigned short Index;
7487 unsigned long Dummy;
7488
7489 for ( Index = 0 ; Index < Intervalcount ; Index++ )
7490 {
7491 memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
7492 Dummy = *((volatile unsigned long *)TargetPtr);
7493 TargetPtr += PCI_LOAD_INTERVAL;
7494 SourcePtr += PCI_LOAD_INTERVAL;
7495 }
7496
7497 memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
7498
7499} /* End Of mgsl_load_pci_memory() */
7500
7501static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
7502{
7503 int i;
7504 int linecount;
7505 if (xmit)
7506 printk("%s tx data:\n",info->device_name);
7507 else
7508 printk("%s rx data:\n",info->device_name);
7509
7510 while(count) {
7511 if (count > 16)
7512 linecount = 16;
7513 else
7514 linecount = count;
7515
7516 for(i=0;i<linecount;i++)
7517 printk("%02X ",(unsigned char)data[i]);
7518 for(;i<17;i++)
7519 printk(" ");
7520 for(i=0;i<linecount;i++) {
7521 if (data[i]>=040 && data[i]<=0176)
7522 printk("%c",data[i]);
7523 else
7524 printk(".");
7525 }
7526 printk("\n");
7527
7528 data += linecount;
7529 count -= linecount;
7530 }
7531} /* end of mgsl_trace_block() */
7532
7533/* mgsl_tx_timeout()
7534 *
7535 * called when HDLC frame times out
7536 * update stats and do tx completion processing
7537 *
7538 * Arguments: context pointer to device instance data
7539 * Return Value: None
7540 */
7541static void mgsl_tx_timeout(unsigned long context)
7542{
7543 struct mgsl_struct *info = (struct mgsl_struct*)context;
7544 unsigned long flags;
7545
7546 if ( debug_level >= DEBUG_LEVEL_INFO )
7547 printk( "%s(%d):mgsl_tx_timeout(%s)\n",
7548 __FILE__,__LINE__,info->device_name);
7549 if(info->tx_active &&
7550 (info->params.mode == MGSL_MODE_HDLC ||
7551 info->params.mode == MGSL_MODE_RAW) ) {
7552 info->icount.txtimeout++;
7553 }
7554 spin_lock_irqsave(&info->irq_spinlock,flags);
7555 info->tx_active = false;
7556 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
7557
7558 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
7559 usc_loopmode_cancel_transmit( info );
7560
7561 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7562
7563#if SYNCLINK_GENERIC_HDLC
7564 if (info->netcount)
7565 hdlcdev_tx_done(info);
7566 else
7567#endif
7568 mgsl_bh_transmit(info);
7569
7570} /* end of mgsl_tx_timeout() */
7571
7572/* signal that there are no more frames to send, so that
7573 * line is 'released' by echoing RxD to TxD when current
7574 * transmission is complete (or immediately if no tx in progress).
7575 */
7576static int mgsl_loopmode_send_done( struct mgsl_struct * info )
7577{
7578 unsigned long flags;
7579
7580 spin_lock_irqsave(&info->irq_spinlock,flags);
7581 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
7582 if (info->tx_active)
7583 info->loopmode_send_done_requested = true;
7584 else
7585 usc_loopmode_send_done(info);
7586 }
7587 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7588
7589 return 0;
7590}
7591
7592/* release the line by echoing RxD to TxD
7593 * upon completion of a transmit frame
7594 */
7595static void usc_loopmode_send_done( struct mgsl_struct * info )
7596{
7597 info->loopmode_send_done_requested = false;
7598 /* clear CMR:13 to 0 to start echoing RxData to TxData */
7599 info->cmr_value &= ~BIT13;
7600 usc_OutReg(info, CMR, info->cmr_value);
7601}
7602
7603/* abort a transmit in progress while in HDLC LoopMode
7604 */
7605static void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
7606{
7607 /* reset tx dma channel and purge TxFifo */
7608 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7609 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
7610 usc_loopmode_send_done( info );
7611}
7612
7613/* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
7614 * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
7615 * we must clear CMR:13 to begin repeating TxData to RxData
7616 */
7617static void usc_loopmode_insert_request( struct mgsl_struct * info )
7618{
7619 info->loopmode_insert_requested = true;
7620
7621 /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
7622 * begin repeating TxData on RxData (complete insertion)
7623 */
7624 usc_OutReg( info, RICR,
7625 (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
7626
7627 /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
7628 info->cmr_value |= BIT13;
7629 usc_OutReg(info, CMR, info->cmr_value);
7630}
7631
7632/* return 1 if station is inserted into the loop, otherwise 0
7633 */
7634static int usc_loopmode_active( struct mgsl_struct * info)
7635{
7636 return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
7637}
7638
7639#if SYNCLINK_GENERIC_HDLC
7640
7641/**
7642 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
7643 * set encoding and frame check sequence (FCS) options
7644 *
7645 * dev pointer to network device structure
7646 * encoding serial encoding setting
7647 * parity FCS setting
7648 *
7649 * returns 0 if success, otherwise error code
7650 */
7651static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
7652 unsigned short parity)
7653{
7654 struct mgsl_struct *info = dev_to_port(dev);
7655 unsigned char new_encoding;
7656 unsigned short new_crctype;
7657
7658 /* return error if TTY interface open */
7659 if (info->port.count)
7660 return -EBUSY;
7661
7662 switch (encoding)
7663 {
7664 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
7665 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
7666 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
7667 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
7668 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
7669 default: return -EINVAL;
7670 }
7671
7672 switch (parity)
7673 {
7674 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
7675 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
7676 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
7677 default: return -EINVAL;
7678 }
7679
7680 info->params.encoding = new_encoding;
7681 info->params.crc_type = new_crctype;
7682
7683 /* if network interface up, reprogram hardware */
7684 if (info->netcount)
7685 mgsl_program_hw(info);
7686
7687 return 0;
7688}
7689
7690/**
7691 * called by generic HDLC layer to send frame
7692 *
7693 * skb socket buffer containing HDLC frame
7694 * dev pointer to network device structure
7695 */
7696static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
7697 struct net_device *dev)
7698{
7699 struct mgsl_struct *info = dev_to_port(dev);
7700 unsigned long flags;
7701
7702 if (debug_level >= DEBUG_LEVEL_INFO)
7703 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
7704
7705 /* stop sending until this frame completes */
7706 netif_stop_queue(dev);
7707
7708 /* copy data to device buffers */
7709 info->xmit_cnt = skb->len;
7710 mgsl_load_tx_dma_buffer(info, skb->data, skb->len);
7711
7712 /* update network statistics */
7713 dev->stats.tx_packets++;
7714 dev->stats.tx_bytes += skb->len;
7715
7716 /* done with socket buffer, so free it */
7717 dev_kfree_skb(skb);
7718
7719 /* save start time for transmit timeout detection */
7720 dev->trans_start = jiffies;
7721
7722 /* start hardware transmitter if necessary */
7723 spin_lock_irqsave(&info->irq_spinlock,flags);
7724 if (!info->tx_active)
7725 usc_start_transmitter(info);
7726 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7727
7728 return NETDEV_TX_OK;
7729}
7730
7731/**
7732 * called by network layer when interface enabled
7733 * claim resources and initialize hardware
7734 *
7735 * dev pointer to network device structure
7736 *
7737 * returns 0 if success, otherwise error code
7738 */
7739static int hdlcdev_open(struct net_device *dev)
7740{
7741 struct mgsl_struct *info = dev_to_port(dev);
7742 int rc;
7743 unsigned long flags;
7744
7745 if (debug_level >= DEBUG_LEVEL_INFO)
7746 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
7747
7748 /* generic HDLC layer open processing */
7749 if ((rc = hdlc_open(dev)))
7750 return rc;
7751
7752 /* arbitrate between network and tty opens */
7753 spin_lock_irqsave(&info->netlock, flags);
7754 if (info->port.count != 0 || info->netcount != 0) {
7755 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
7756 spin_unlock_irqrestore(&info->netlock, flags);
7757 return -EBUSY;
7758 }
7759 info->netcount=1;
7760 spin_unlock_irqrestore(&info->netlock, flags);
7761
7762 /* claim resources and init adapter */
7763 if ((rc = startup(info)) != 0) {
7764 spin_lock_irqsave(&info->netlock, flags);
7765 info->netcount=0;
7766 spin_unlock_irqrestore(&info->netlock, flags);
7767 return rc;
7768 }
7769
7770 /* assert DTR and RTS, apply hardware settings */
7771 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
7772 mgsl_program_hw(info);
7773
7774 /* enable network layer transmit */
7775 dev->trans_start = jiffies;
7776 netif_start_queue(dev);
7777
7778 /* inform generic HDLC layer of current DCD status */
7779 spin_lock_irqsave(&info->irq_spinlock, flags);
7780 usc_get_serial_signals(info);
7781 spin_unlock_irqrestore(&info->irq_spinlock, flags);
7782 if (info->serial_signals & SerialSignal_DCD)
7783 netif_carrier_on(dev);
7784 else
7785 netif_carrier_off(dev);
7786 return 0;
7787}
7788
7789/**
7790 * called by network layer when interface is disabled
7791 * shutdown hardware and release resources
7792 *
7793 * dev pointer to network device structure
7794 *
7795 * returns 0 if success, otherwise error code
7796 */
7797static int hdlcdev_close(struct net_device *dev)
7798{
7799 struct mgsl_struct *info = dev_to_port(dev);
7800 unsigned long flags;
7801
7802 if (debug_level >= DEBUG_LEVEL_INFO)
7803 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
7804
7805 netif_stop_queue(dev);
7806
7807 /* shutdown adapter and release resources */
7808 shutdown(info);
7809
7810 hdlc_close(dev);
7811
7812 spin_lock_irqsave(&info->netlock, flags);
7813 info->netcount=0;
7814 spin_unlock_irqrestore(&info->netlock, flags);
7815
7816 return 0;
7817}
7818
7819/**
7820 * called by network layer to process IOCTL call to network device
7821 *
7822 * dev pointer to network device structure
7823 * ifr pointer to network interface request structure
7824 * cmd IOCTL command code
7825 *
7826 * returns 0 if success, otherwise error code
7827 */
7828static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7829{
7830 const size_t size = sizeof(sync_serial_settings);
7831 sync_serial_settings new_line;
7832 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
7833 struct mgsl_struct *info = dev_to_port(dev);
7834 unsigned int flags;
7835
7836 if (debug_level >= DEBUG_LEVEL_INFO)
7837 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
7838
7839 /* return error if TTY interface open */
7840 if (info->port.count)
7841 return -EBUSY;
7842
7843 if (cmd != SIOCWANDEV)
7844 return hdlc_ioctl(dev, ifr, cmd);
7845
7846 switch(ifr->ifr_settings.type) {
7847 case IF_GET_IFACE: /* return current sync_serial_settings */
7848
7849 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
7850 if (ifr->ifr_settings.size < size) {
7851 ifr->ifr_settings.size = size; /* data size wanted */
7852 return -ENOBUFS;
7853 }
7854
7855 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7856 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7857 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7858 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7859
7860 switch (flags){
7861 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
7862 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
7863 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
7864 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
7865 default: new_line.clock_type = CLOCK_DEFAULT;
7866 }
7867
7868 new_line.clock_rate = info->params.clock_speed;
7869 new_line.loopback = info->params.loopback ? 1:0;
7870
7871 if (copy_to_user(line, &new_line, size))
7872 return -EFAULT;
7873 return 0;
7874
7875 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
7876
7877 if(!capable(CAP_NET_ADMIN))
7878 return -EPERM;
7879 if (copy_from_user(&new_line, line, size))
7880 return -EFAULT;
7881
7882 switch (new_line.clock_type)
7883 {
7884 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
7885 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
7886 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
7887 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
7888 case CLOCK_DEFAULT: flags = info->params.flags &
7889 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7890 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7891 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7892 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
7893 default: return -EINVAL;
7894 }
7895
7896 if (new_line.loopback != 0 && new_line.loopback != 1)
7897 return -EINVAL;
7898
7899 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7900 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7901 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7902 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7903 info->params.flags |= flags;
7904
7905 info->params.loopback = new_line.loopback;
7906
7907 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
7908 info->params.clock_speed = new_line.clock_rate;
7909 else
7910 info->params.clock_speed = 0;
7911
7912 /* if network interface up, reprogram hardware */
7913 if (info->netcount)
7914 mgsl_program_hw(info);
7915 return 0;
7916
7917 default:
7918 return hdlc_ioctl(dev, ifr, cmd);
7919 }
7920}
7921
7922/**
7923 * called by network layer when transmit timeout is detected
7924 *
7925 * dev pointer to network device structure
7926 */
7927static void hdlcdev_tx_timeout(struct net_device *dev)
7928{
7929 struct mgsl_struct *info = dev_to_port(dev);
7930 unsigned long flags;
7931
7932 if (debug_level >= DEBUG_LEVEL_INFO)
7933 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
7934
7935 dev->stats.tx_errors++;
7936 dev->stats.tx_aborted_errors++;
7937
7938 spin_lock_irqsave(&info->irq_spinlock,flags);
7939 usc_stop_transmitter(info);
7940 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7941
7942 netif_wake_queue(dev);
7943}
7944
7945/**
7946 * called by device driver when transmit completes
7947 * reenable network layer transmit if stopped
7948 *
7949 * info pointer to device instance information
7950 */
7951static void hdlcdev_tx_done(struct mgsl_struct *info)
7952{
7953 if (netif_queue_stopped(info->netdev))
7954 netif_wake_queue(info->netdev);
7955}
7956
7957/**
7958 * called by device driver when frame received
7959 * pass frame to network layer
7960 *
7961 * info pointer to device instance information
7962 * buf pointer to buffer contianing frame data
7963 * size count of data bytes in buf
7964 */
7965static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size)
7966{
7967 struct sk_buff *skb = dev_alloc_skb(size);
7968 struct net_device *dev = info->netdev;
7969
7970 if (debug_level >= DEBUG_LEVEL_INFO)
7971 printk("hdlcdev_rx(%s)\n", dev->name);
7972
7973 if (skb == NULL) {
7974 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
7975 dev->name);
7976 dev->stats.rx_dropped++;
7977 return;
7978 }
7979
7980 memcpy(skb_put(skb, size), buf, size);
7981
7982 skb->protocol = hdlc_type_trans(skb, dev);
7983
7984 dev->stats.rx_packets++;
7985 dev->stats.rx_bytes += size;
7986
7987 netif_rx(skb);
7988}
7989
7990static const struct net_device_ops hdlcdev_ops = {
7991 .ndo_open = hdlcdev_open,
7992 .ndo_stop = hdlcdev_close,
7993 .ndo_change_mtu = hdlc_change_mtu,
7994 .ndo_start_xmit = hdlc_start_xmit,
7995 .ndo_do_ioctl = hdlcdev_ioctl,
7996 .ndo_tx_timeout = hdlcdev_tx_timeout,
7997};
7998
7999/**
8000 * called by device driver when adding device instance
8001 * do generic HDLC initialization
8002 *
8003 * info pointer to device instance information
8004 *
8005 * returns 0 if success, otherwise error code
8006 */
8007static int hdlcdev_init(struct mgsl_struct *info)
8008{
8009 int rc;
8010 struct net_device *dev;
8011 hdlc_device *hdlc;
8012
8013 /* allocate and initialize network and HDLC layer objects */
8014
8015 if (!(dev = alloc_hdlcdev(info))) {
8016 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
8017 return -ENOMEM;
8018 }
8019
8020 /* for network layer reporting purposes only */
8021 dev->base_addr = info->io_base;
8022 dev->irq = info->irq_level;
8023 dev->dma = info->dma_level;
8024
8025 /* network layer callbacks and settings */
8026 dev->netdev_ops = &hdlcdev_ops;
8027 dev->watchdog_timeo = 10 * HZ;
8028 dev->tx_queue_len = 50;
8029
8030 /* generic HDLC layer callbacks and settings */
8031 hdlc = dev_to_hdlc(dev);
8032 hdlc->attach = hdlcdev_attach;
8033 hdlc->xmit = hdlcdev_xmit;
8034
8035 /* register objects with HDLC layer */
8036 if ((rc = register_hdlc_device(dev))) {
8037 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
8038 free_netdev(dev);
8039 return rc;
8040 }
8041
8042 info->netdev = dev;
8043 return 0;
8044}
8045
8046/**
8047 * called by device driver when removing device instance
8048 * do generic HDLC cleanup
8049 *
8050 * info pointer to device instance information
8051 */
8052static void hdlcdev_exit(struct mgsl_struct *info)
8053{
8054 unregister_hdlc_device(info->netdev);
8055 free_netdev(info->netdev);
8056 info->netdev = NULL;
8057}
8058
8059#endif /* CONFIG_HDLC */
8060
8061
8062static int __devinit synclink_init_one (struct pci_dev *dev,
8063 const struct pci_device_id *ent)
8064{
8065 struct mgsl_struct *info;
8066
8067 if (pci_enable_device(dev)) {
8068 printk("error enabling pci device %p\n", dev);
8069 return -EIO;
8070 }
8071
8072 if (!(info = mgsl_allocate_device())) {
8073 printk("can't allocate device instance data.\n");
8074 return -EIO;
8075 }
8076
8077 /* Copy user configuration info to device instance data */
8078
8079 info->io_base = pci_resource_start(dev, 2);
8080 info->irq_level = dev->irq;
8081 info->phys_memory_base = pci_resource_start(dev, 3);
8082
8083 /* Because veremap only works on page boundaries we must map
8084 * a larger area than is actually implemented for the LCR
8085 * memory range. We map a full page starting at the page boundary.
8086 */
8087 info->phys_lcr_base = pci_resource_start(dev, 0);
8088 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
8089 info->phys_lcr_base &= ~(PAGE_SIZE-1);
8090
8091 info->bus_type = MGSL_BUS_TYPE_PCI;
8092 info->io_addr_size = 8;
8093 info->irq_flags = IRQF_SHARED;
8094
8095 if (dev->device == 0x0210) {
8096 /* Version 1 PCI9030 based universal PCI adapter */
8097 info->misc_ctrl_value = 0x007c4080;
8098 info->hw_version = 1;
8099 } else {
8100 /* Version 0 PCI9050 based 5V PCI adapter
8101 * A PCI9050 bug prevents reading LCR registers if
8102 * LCR base address bit 7 is set. Maintain shadow
8103 * value so we can write to LCR misc control reg.
8104 */
8105 info->misc_ctrl_value = 0x087e4546;
8106 info->hw_version = 0;
8107 }
8108
8109 mgsl_add_device(info);
8110
8111 return 0;
8112}
8113
8114static void __devexit synclink_remove_one (struct pci_dev *dev)
8115{
8116}
8117
diff --git a/drivers/tty/synclink_gt.c b/drivers/tty/synclink_gt.c
new file mode 100644
index 000000000000..18b48cd3b41d
--- /dev/null
+++ b/drivers/tty/synclink_gt.c
@@ -0,0 +1,5161 @@
1/*
2 * Device driver for Microgate SyncLink GT serial adapters.
3 *
4 * written by Paul Fulghum for Microgate Corporation
5 * paulkf@microgate.com
6 *
7 * Microgate and SyncLink are trademarks of Microgate Corporation
8 *
9 * This code is released under the GNU General Public License (GPL)
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21 * OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24/*
25 * DEBUG OUTPUT DEFINITIONS
26 *
27 * uncomment lines below to enable specific types of debug output
28 *
29 * DBGINFO information - most verbose output
30 * DBGERR serious errors
31 * DBGBH bottom half service routine debugging
32 * DBGISR interrupt service routine debugging
33 * DBGDATA output receive and transmit data
34 * DBGTBUF output transmit DMA buffers and registers
35 * DBGRBUF output receive DMA buffers and registers
36 */
37
38#define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
39#define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
40#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
41#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
42#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
43/*#define DBGTBUF(info) dump_tbufs(info)*/
44/*#define DBGRBUF(info) dump_rbufs(info)*/
45
46
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/signal.h>
50#include <linux/sched.h>
51#include <linux/timer.h>
52#include <linux/interrupt.h>
53#include <linux/pci.h>
54#include <linux/tty.h>
55#include <linux/tty_flip.h>
56#include <linux/serial.h>
57#include <linux/major.h>
58#include <linux/string.h>
59#include <linux/fcntl.h>
60#include <linux/ptrace.h>
61#include <linux/ioport.h>
62#include <linux/mm.h>
63#include <linux/seq_file.h>
64#include <linux/slab.h>
65#include <linux/netdevice.h>
66#include <linux/vmalloc.h>
67#include <linux/init.h>
68#include <linux/delay.h>
69#include <linux/ioctl.h>
70#include <linux/termios.h>
71#include <linux/bitops.h>
72#include <linux/workqueue.h>
73#include <linux/hdlc.h>
74#include <linux/synclink.h>
75
76#include <asm/system.h>
77#include <asm/io.h>
78#include <asm/irq.h>
79#include <asm/dma.h>
80#include <asm/types.h>
81#include <asm/uaccess.h>
82
83#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
84#define SYNCLINK_GENERIC_HDLC 1
85#else
86#define SYNCLINK_GENERIC_HDLC 0
87#endif
88
89/*
90 * module identification
91 */
92static char *driver_name = "SyncLink GT";
93static char *tty_driver_name = "synclink_gt";
94static char *tty_dev_prefix = "ttySLG";
95MODULE_LICENSE("GPL");
96#define MGSL_MAGIC 0x5401
97#define MAX_DEVICES 32
98
99static struct pci_device_id pci_table[] = {
100 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
101 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
102 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104 {0,}, /* terminate list */
105};
106MODULE_DEVICE_TABLE(pci, pci_table);
107
108static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
109static void remove_one(struct pci_dev *dev);
110static struct pci_driver pci_driver = {
111 .name = "synclink_gt",
112 .id_table = pci_table,
113 .probe = init_one,
114 .remove = __devexit_p(remove_one),
115};
116
117static bool pci_registered;
118
119/*
120 * module configuration and status
121 */
122static struct slgt_info *slgt_device_list;
123static int slgt_device_count;
124
125static int ttymajor;
126static int debug_level;
127static int maxframe[MAX_DEVICES];
128
129module_param(ttymajor, int, 0);
130module_param(debug_level, int, 0);
131module_param_array(maxframe, int, NULL, 0);
132
133MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
134MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
135MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
136
137/*
138 * tty support and callbacks
139 */
140static struct tty_driver *serial_driver;
141
142static int open(struct tty_struct *tty, struct file * filp);
143static void close(struct tty_struct *tty, struct file * filp);
144static void hangup(struct tty_struct *tty);
145static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
146
147static int write(struct tty_struct *tty, const unsigned char *buf, int count);
148static int put_char(struct tty_struct *tty, unsigned char ch);
149static void send_xchar(struct tty_struct *tty, char ch);
150static void wait_until_sent(struct tty_struct *tty, int timeout);
151static int write_room(struct tty_struct *tty);
152static void flush_chars(struct tty_struct *tty);
153static void flush_buffer(struct tty_struct *tty);
154static void tx_hold(struct tty_struct *tty);
155static void tx_release(struct tty_struct *tty);
156
157static int ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
158static int chars_in_buffer(struct tty_struct *tty);
159static void throttle(struct tty_struct * tty);
160static void unthrottle(struct tty_struct * tty);
161static int set_break(struct tty_struct *tty, int break_state);
162
163/*
164 * generic HDLC support and callbacks
165 */
166#if SYNCLINK_GENERIC_HDLC
167#define dev_to_port(D) (dev_to_hdlc(D)->priv)
168static void hdlcdev_tx_done(struct slgt_info *info);
169static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
170static int hdlcdev_init(struct slgt_info *info);
171static void hdlcdev_exit(struct slgt_info *info);
172#endif
173
174
175/*
176 * device specific structures, macros and functions
177 */
178
179#define SLGT_MAX_PORTS 4
180#define SLGT_REG_SIZE 256
181
182/*
183 * conditional wait facility
184 */
185struct cond_wait {
186 struct cond_wait *next;
187 wait_queue_head_t q;
188 wait_queue_t wait;
189 unsigned int data;
190};
191static void init_cond_wait(struct cond_wait *w, unsigned int data);
192static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
193static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
194static void flush_cond_wait(struct cond_wait **head);
195
196/*
197 * DMA buffer descriptor and access macros
198 */
199struct slgt_desc
200{
201 __le16 count;
202 __le16 status;
203 __le32 pbuf; /* physical address of data buffer */
204 __le32 next; /* physical address of next descriptor */
205
206 /* driver book keeping */
207 char *buf; /* virtual address of data buffer */
208 unsigned int pdesc; /* physical address of this descriptor */
209 dma_addr_t buf_dma_addr;
210 unsigned short buf_count;
211};
212
213#define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
214#define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
215#define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
216#define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
217#define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
218#define desc_count(a) (le16_to_cpu((a).count))
219#define desc_status(a) (le16_to_cpu((a).status))
220#define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
221#define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
222#define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
223#define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
224#define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
225
226struct _input_signal_events {
227 int ri_up;
228 int ri_down;
229 int dsr_up;
230 int dsr_down;
231 int dcd_up;
232 int dcd_down;
233 int cts_up;
234 int cts_down;
235};
236
237/*
238 * device instance data structure
239 */
240struct slgt_info {
241 void *if_ptr; /* General purpose pointer (used by SPPP) */
242 struct tty_port port;
243
244 struct slgt_info *next_device; /* device list link */
245
246 int magic;
247
248 char device_name[25];
249 struct pci_dev *pdev;
250
251 int port_count; /* count of ports on adapter */
252 int adapter_num; /* adapter instance number */
253 int port_num; /* port instance number */
254
255 /* array of pointers to port contexts on this adapter */
256 struct slgt_info *port_array[SLGT_MAX_PORTS];
257
258 int line; /* tty line instance number */
259
260 struct mgsl_icount icount;
261
262 int timeout;
263 int x_char; /* xon/xoff character */
264 unsigned int read_status_mask;
265 unsigned int ignore_status_mask;
266
267 wait_queue_head_t status_event_wait_q;
268 wait_queue_head_t event_wait_q;
269 struct timer_list tx_timer;
270 struct timer_list rx_timer;
271
272 unsigned int gpio_present;
273 struct cond_wait *gpio_wait_q;
274
275 spinlock_t lock; /* spinlock for synchronizing with ISR */
276
277 struct work_struct task;
278 u32 pending_bh;
279 bool bh_requested;
280 bool bh_running;
281
282 int isr_overflow;
283 bool irq_requested; /* true if IRQ requested */
284 bool irq_occurred; /* for diagnostics use */
285
286 /* device configuration */
287
288 unsigned int bus_type;
289 unsigned int irq_level;
290 unsigned long irq_flags;
291
292 unsigned char __iomem * reg_addr; /* memory mapped registers address */
293 u32 phys_reg_addr;
294 bool reg_addr_requested;
295
296 MGSL_PARAMS params; /* communications parameters */
297 u32 idle_mode;
298 u32 max_frame_size; /* as set by device config */
299
300 unsigned int rbuf_fill_level;
301 unsigned int rx_pio;
302 unsigned int if_mode;
303 unsigned int base_clock;
304 unsigned int xsync;
305 unsigned int xctrl;
306
307 /* device status */
308
309 bool rx_enabled;
310 bool rx_restart;
311
312 bool tx_enabled;
313 bool tx_active;
314
315 unsigned char signals; /* serial signal states */
316 int init_error; /* initialization error */
317
318 unsigned char *tx_buf;
319 int tx_count;
320
321 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
322 char char_buf[MAX_ASYNC_BUFFER_SIZE];
323 bool drop_rts_on_tx_done;
324 struct _input_signal_events input_signal_events;
325
326 int dcd_chkcount; /* check counts to prevent */
327 int cts_chkcount; /* too many IRQs if a signal */
328 int dsr_chkcount; /* is floating */
329 int ri_chkcount;
330
331 char *bufs; /* virtual address of DMA buffer lists */
332 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
333
334 unsigned int rbuf_count;
335 struct slgt_desc *rbufs;
336 unsigned int rbuf_current;
337 unsigned int rbuf_index;
338 unsigned int rbuf_fill_index;
339 unsigned short rbuf_fill_count;
340
341 unsigned int tbuf_count;
342 struct slgt_desc *tbufs;
343 unsigned int tbuf_current;
344 unsigned int tbuf_start;
345
346 unsigned char *tmp_rbuf;
347 unsigned int tmp_rbuf_count;
348
349 /* SPPP/Cisco HDLC device parts */
350
351 int netcount;
352 spinlock_t netlock;
353#if SYNCLINK_GENERIC_HDLC
354 struct net_device *netdev;
355#endif
356
357};
358
359static MGSL_PARAMS default_params = {
360 .mode = MGSL_MODE_HDLC,
361 .loopback = 0,
362 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
363 .encoding = HDLC_ENCODING_NRZI_SPACE,
364 .clock_speed = 0,
365 .addr_filter = 0xff,
366 .crc_type = HDLC_CRC_16_CCITT,
367 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
368 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
369 .data_rate = 9600,
370 .data_bits = 8,
371 .stop_bits = 1,
372 .parity = ASYNC_PARITY_NONE
373};
374
375
376#define BH_RECEIVE 1
377#define BH_TRANSMIT 2
378#define BH_STATUS 4
379#define IO_PIN_SHUTDOWN_LIMIT 100
380
381#define DMABUFSIZE 256
382#define DESC_LIST_SIZE 4096
383
384#define MASK_PARITY BIT1
385#define MASK_FRAMING BIT0
386#define MASK_BREAK BIT14
387#define MASK_OVERRUN BIT4
388
389#define GSR 0x00 /* global status */
390#define JCR 0x04 /* JTAG control */
391#define IODR 0x08 /* GPIO direction */
392#define IOER 0x0c /* GPIO interrupt enable */
393#define IOVR 0x10 /* GPIO value */
394#define IOSR 0x14 /* GPIO interrupt status */
395#define TDR 0x80 /* tx data */
396#define RDR 0x80 /* rx data */
397#define TCR 0x82 /* tx control */
398#define TIR 0x84 /* tx idle */
399#define TPR 0x85 /* tx preamble */
400#define RCR 0x86 /* rx control */
401#define VCR 0x88 /* V.24 control */
402#define CCR 0x89 /* clock control */
403#define BDR 0x8a /* baud divisor */
404#define SCR 0x8c /* serial control */
405#define SSR 0x8e /* serial status */
406#define RDCSR 0x90 /* rx DMA control/status */
407#define TDCSR 0x94 /* tx DMA control/status */
408#define RDDAR 0x98 /* rx DMA descriptor address */
409#define TDDAR 0x9c /* tx DMA descriptor address */
410#define XSR 0x40 /* extended sync pattern */
411#define XCR 0x44 /* extended control */
412
413#define RXIDLE BIT14
414#define RXBREAK BIT14
415#define IRQ_TXDATA BIT13
416#define IRQ_TXIDLE BIT12
417#define IRQ_TXUNDER BIT11 /* HDLC */
418#define IRQ_RXDATA BIT10
419#define IRQ_RXIDLE BIT9 /* HDLC */
420#define IRQ_RXBREAK BIT9 /* async */
421#define IRQ_RXOVER BIT8
422#define IRQ_DSR BIT7
423#define IRQ_CTS BIT6
424#define IRQ_DCD BIT5
425#define IRQ_RI BIT4
426#define IRQ_ALL 0x3ff0
427#define IRQ_MASTER BIT0
428
429#define slgt_irq_on(info, mask) \
430 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
431#define slgt_irq_off(info, mask) \
432 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
433
434static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
435static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
436static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
437static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
438static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
439static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
440
441static void msc_set_vcr(struct slgt_info *info);
442
443static int startup(struct slgt_info *info);
444static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
445static void shutdown(struct slgt_info *info);
446static void program_hw(struct slgt_info *info);
447static void change_params(struct slgt_info *info);
448
449static int register_test(struct slgt_info *info);
450static int irq_test(struct slgt_info *info);
451static int loopback_test(struct slgt_info *info);
452static int adapter_test(struct slgt_info *info);
453
454static void reset_adapter(struct slgt_info *info);
455static void reset_port(struct slgt_info *info);
456static void async_mode(struct slgt_info *info);
457static void sync_mode(struct slgt_info *info);
458
459static void rx_stop(struct slgt_info *info);
460static void rx_start(struct slgt_info *info);
461static void reset_rbufs(struct slgt_info *info);
462static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
463static void rdma_reset(struct slgt_info *info);
464static bool rx_get_frame(struct slgt_info *info);
465static bool rx_get_buf(struct slgt_info *info);
466
467static void tx_start(struct slgt_info *info);
468static void tx_stop(struct slgt_info *info);
469static void tx_set_idle(struct slgt_info *info);
470static unsigned int free_tbuf_count(struct slgt_info *info);
471static unsigned int tbuf_bytes(struct slgt_info *info);
472static void reset_tbufs(struct slgt_info *info);
473static void tdma_reset(struct slgt_info *info);
474static bool tx_load(struct slgt_info *info, const char *buf, unsigned int count);
475
476static void get_signals(struct slgt_info *info);
477static void set_signals(struct slgt_info *info);
478static void enable_loopback(struct slgt_info *info);
479static void set_rate(struct slgt_info *info, u32 data_rate);
480
481static int bh_action(struct slgt_info *info);
482static void bh_handler(struct work_struct *work);
483static void bh_transmit(struct slgt_info *info);
484static void isr_serial(struct slgt_info *info);
485static void isr_rdma(struct slgt_info *info);
486static void isr_txeom(struct slgt_info *info, unsigned short status);
487static void isr_tdma(struct slgt_info *info);
488
489static int alloc_dma_bufs(struct slgt_info *info);
490static void free_dma_bufs(struct slgt_info *info);
491static int alloc_desc(struct slgt_info *info);
492static void free_desc(struct slgt_info *info);
493static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
494static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
495
496static int alloc_tmp_rbuf(struct slgt_info *info);
497static void free_tmp_rbuf(struct slgt_info *info);
498
499static void tx_timeout(unsigned long context);
500static void rx_timeout(unsigned long context);
501
502/*
503 * ioctl handlers
504 */
505static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
506static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
507static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
508static int get_txidle(struct slgt_info *info, int __user *idle_mode);
509static int set_txidle(struct slgt_info *info, int idle_mode);
510static int tx_enable(struct slgt_info *info, int enable);
511static int tx_abort(struct slgt_info *info);
512static int rx_enable(struct slgt_info *info, int enable);
513static int modem_input_wait(struct slgt_info *info,int arg);
514static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
515static int tiocmget(struct tty_struct *tty);
516static int tiocmset(struct tty_struct *tty,
517 unsigned int set, unsigned int clear);
518static int set_break(struct tty_struct *tty, int break_state);
519static int get_interface(struct slgt_info *info, int __user *if_mode);
520static int set_interface(struct slgt_info *info, int if_mode);
521static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
522static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
523static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
524static int get_xsync(struct slgt_info *info, int __user *if_mode);
525static int set_xsync(struct slgt_info *info, int if_mode);
526static int get_xctrl(struct slgt_info *info, int __user *if_mode);
527static int set_xctrl(struct slgt_info *info, int if_mode);
528
529/*
530 * driver functions
531 */
532static void add_device(struct slgt_info *info);
533static void device_init(int adapter_num, struct pci_dev *pdev);
534static int claim_resources(struct slgt_info *info);
535static void release_resources(struct slgt_info *info);
536
537/*
538 * DEBUG OUTPUT CODE
539 */
540#ifndef DBGINFO
541#define DBGINFO(fmt)
542#endif
543#ifndef DBGERR
544#define DBGERR(fmt)
545#endif
546#ifndef DBGBH
547#define DBGBH(fmt)
548#endif
549#ifndef DBGISR
550#define DBGISR(fmt)
551#endif
552
553#ifdef DBGDATA
554static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
555{
556 int i;
557 int linecount;
558 printk("%s %s data:\n",info->device_name, label);
559 while(count) {
560 linecount = (count > 16) ? 16 : count;
561 for(i=0; i < linecount; i++)
562 printk("%02X ",(unsigned char)data[i]);
563 for(;i<17;i++)
564 printk(" ");
565 for(i=0;i<linecount;i++) {
566 if (data[i]>=040 && data[i]<=0176)
567 printk("%c",data[i]);
568 else
569 printk(".");
570 }
571 printk("\n");
572 data += linecount;
573 count -= linecount;
574 }
575}
576#else
577#define DBGDATA(info, buf, size, label)
578#endif
579
580#ifdef DBGTBUF
581static void dump_tbufs(struct slgt_info *info)
582{
583 int i;
584 printk("tbuf_current=%d\n", info->tbuf_current);
585 for (i=0 ; i < info->tbuf_count ; i++) {
586 printk("%d: count=%04X status=%04X\n",
587 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
588 }
589}
590#else
591#define DBGTBUF(info)
592#endif
593
594#ifdef DBGRBUF
595static void dump_rbufs(struct slgt_info *info)
596{
597 int i;
598 printk("rbuf_current=%d\n", info->rbuf_current);
599 for (i=0 ; i < info->rbuf_count ; i++) {
600 printk("%d: count=%04X status=%04X\n",
601 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
602 }
603}
604#else
605#define DBGRBUF(info)
606#endif
607
608static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
609{
610#ifdef SANITY_CHECK
611 if (!info) {
612 printk("null struct slgt_info for (%s) in %s\n", devname, name);
613 return 1;
614 }
615 if (info->magic != MGSL_MAGIC) {
616 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
617 return 1;
618 }
619#else
620 if (!info)
621 return 1;
622#endif
623 return 0;
624}
625
626/**
627 * line discipline callback wrappers
628 *
629 * The wrappers maintain line discipline references
630 * while calling into the line discipline.
631 *
632 * ldisc_receive_buf - pass receive data to line discipline
633 */
634static void ldisc_receive_buf(struct tty_struct *tty,
635 const __u8 *data, char *flags, int count)
636{
637 struct tty_ldisc *ld;
638 if (!tty)
639 return;
640 ld = tty_ldisc_ref(tty);
641 if (ld) {
642 if (ld->ops->receive_buf)
643 ld->ops->receive_buf(tty, data, flags, count);
644 tty_ldisc_deref(ld);
645 }
646}
647
648/* tty callbacks */
649
650static int open(struct tty_struct *tty, struct file *filp)
651{
652 struct slgt_info *info;
653 int retval, line;
654 unsigned long flags;
655
656 line = tty->index;
657 if ((line < 0) || (line >= slgt_device_count)) {
658 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
659 return -ENODEV;
660 }
661
662 info = slgt_device_list;
663 while(info && info->line != line)
664 info = info->next_device;
665 if (sanity_check(info, tty->name, "open"))
666 return -ENODEV;
667 if (info->init_error) {
668 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
669 return -ENODEV;
670 }
671
672 tty->driver_data = info;
673 info->port.tty = tty;
674
675 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
676
677 /* If port is closing, signal caller to try again */
678 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
679 if (info->port.flags & ASYNC_CLOSING)
680 interruptible_sleep_on(&info->port.close_wait);
681 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
682 -EAGAIN : -ERESTARTSYS);
683 goto cleanup;
684 }
685
686 mutex_lock(&info->port.mutex);
687 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
688
689 spin_lock_irqsave(&info->netlock, flags);
690 if (info->netcount) {
691 retval = -EBUSY;
692 spin_unlock_irqrestore(&info->netlock, flags);
693 mutex_unlock(&info->port.mutex);
694 goto cleanup;
695 }
696 info->port.count++;
697 spin_unlock_irqrestore(&info->netlock, flags);
698
699 if (info->port.count == 1) {
700 /* 1st open on this device, init hardware */
701 retval = startup(info);
702 if (retval < 0) {
703 mutex_unlock(&info->port.mutex);
704 goto cleanup;
705 }
706 }
707 mutex_unlock(&info->port.mutex);
708 retval = block_til_ready(tty, filp, info);
709 if (retval) {
710 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
711 goto cleanup;
712 }
713
714 retval = 0;
715
716cleanup:
717 if (retval) {
718 if (tty->count == 1)
719 info->port.tty = NULL; /* tty layer will release tty struct */
720 if(info->port.count)
721 info->port.count--;
722 }
723
724 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
725 return retval;
726}
727
728static void close(struct tty_struct *tty, struct file *filp)
729{
730 struct slgt_info *info = tty->driver_data;
731
732 if (sanity_check(info, tty->name, "close"))
733 return;
734 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
735
736 if (tty_port_close_start(&info->port, tty, filp) == 0)
737 goto cleanup;
738
739 mutex_lock(&info->port.mutex);
740 if (info->port.flags & ASYNC_INITIALIZED)
741 wait_until_sent(tty, info->timeout);
742 flush_buffer(tty);
743 tty_ldisc_flush(tty);
744
745 shutdown(info);
746 mutex_unlock(&info->port.mutex);
747
748 tty_port_close_end(&info->port, tty);
749 info->port.tty = NULL;
750cleanup:
751 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
752}
753
754static void hangup(struct tty_struct *tty)
755{
756 struct slgt_info *info = tty->driver_data;
757 unsigned long flags;
758
759 if (sanity_check(info, tty->name, "hangup"))
760 return;
761 DBGINFO(("%s hangup\n", info->device_name));
762
763 flush_buffer(tty);
764
765 mutex_lock(&info->port.mutex);
766 shutdown(info);
767
768 spin_lock_irqsave(&info->port.lock, flags);
769 info->port.count = 0;
770 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
771 info->port.tty = NULL;
772 spin_unlock_irqrestore(&info->port.lock, flags);
773 mutex_unlock(&info->port.mutex);
774
775 wake_up_interruptible(&info->port.open_wait);
776}
777
778static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
779{
780 struct slgt_info *info = tty->driver_data;
781 unsigned long flags;
782
783 DBGINFO(("%s set_termios\n", tty->driver->name));
784
785 change_params(info);
786
787 /* Handle transition to B0 status */
788 if (old_termios->c_cflag & CBAUD &&
789 !(tty->termios->c_cflag & CBAUD)) {
790 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
791 spin_lock_irqsave(&info->lock,flags);
792 set_signals(info);
793 spin_unlock_irqrestore(&info->lock,flags);
794 }
795
796 /* Handle transition away from B0 status */
797 if (!(old_termios->c_cflag & CBAUD) &&
798 tty->termios->c_cflag & CBAUD) {
799 info->signals |= SerialSignal_DTR;
800 if (!(tty->termios->c_cflag & CRTSCTS) ||
801 !test_bit(TTY_THROTTLED, &tty->flags)) {
802 info->signals |= SerialSignal_RTS;
803 }
804 spin_lock_irqsave(&info->lock,flags);
805 set_signals(info);
806 spin_unlock_irqrestore(&info->lock,flags);
807 }
808
809 /* Handle turning off CRTSCTS */
810 if (old_termios->c_cflag & CRTSCTS &&
811 !(tty->termios->c_cflag & CRTSCTS)) {
812 tty->hw_stopped = 0;
813 tx_release(tty);
814 }
815}
816
817static void update_tx_timer(struct slgt_info *info)
818{
819 /*
820 * use worst case speed of 1200bps to calculate transmit timeout
821 * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
822 */
823 if (info->params.mode == MGSL_MODE_HDLC) {
824 int timeout = (tbuf_bytes(info) * 7) + 1000;
825 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
826 }
827}
828
829static int write(struct tty_struct *tty,
830 const unsigned char *buf, int count)
831{
832 int ret = 0;
833 struct slgt_info *info = tty->driver_data;
834 unsigned long flags;
835
836 if (sanity_check(info, tty->name, "write"))
837 return -EIO;
838
839 DBGINFO(("%s write count=%d\n", info->device_name, count));
840
841 if (!info->tx_buf || (count > info->max_frame_size))
842 return -EIO;
843
844 if (!count || tty->stopped || tty->hw_stopped)
845 return 0;
846
847 spin_lock_irqsave(&info->lock, flags);
848
849 if (info->tx_count) {
850 /* send accumulated data from send_char() */
851 if (!tx_load(info, info->tx_buf, info->tx_count))
852 goto cleanup;
853 info->tx_count = 0;
854 }
855
856 if (tx_load(info, buf, count))
857 ret = count;
858
859cleanup:
860 spin_unlock_irqrestore(&info->lock, flags);
861 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
862 return ret;
863}
864
865static int put_char(struct tty_struct *tty, unsigned char ch)
866{
867 struct slgt_info *info = tty->driver_data;
868 unsigned long flags;
869 int ret = 0;
870
871 if (sanity_check(info, tty->name, "put_char"))
872 return 0;
873 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
874 if (!info->tx_buf)
875 return 0;
876 spin_lock_irqsave(&info->lock,flags);
877 if (info->tx_count < info->max_frame_size) {
878 info->tx_buf[info->tx_count++] = ch;
879 ret = 1;
880 }
881 spin_unlock_irqrestore(&info->lock,flags);
882 return ret;
883}
884
885static void send_xchar(struct tty_struct *tty, char ch)
886{
887 struct slgt_info *info = tty->driver_data;
888 unsigned long flags;
889
890 if (sanity_check(info, tty->name, "send_xchar"))
891 return;
892 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
893 info->x_char = ch;
894 if (ch) {
895 spin_lock_irqsave(&info->lock,flags);
896 if (!info->tx_enabled)
897 tx_start(info);
898 spin_unlock_irqrestore(&info->lock,flags);
899 }
900}
901
902static void wait_until_sent(struct tty_struct *tty, int timeout)
903{
904 struct slgt_info *info = tty->driver_data;
905 unsigned long orig_jiffies, char_time;
906
907 if (!info )
908 return;
909 if (sanity_check(info, tty->name, "wait_until_sent"))
910 return;
911 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
912 if (!(info->port.flags & ASYNC_INITIALIZED))
913 goto exit;
914
915 orig_jiffies = jiffies;
916
917 /* Set check interval to 1/5 of estimated time to
918 * send a character, and make it at least 1. The check
919 * interval should also be less than the timeout.
920 * Note: use tight timings here to satisfy the NIST-PCTS.
921 */
922
923 if (info->params.data_rate) {
924 char_time = info->timeout/(32 * 5);
925 if (!char_time)
926 char_time++;
927 } else
928 char_time = 1;
929
930 if (timeout)
931 char_time = min_t(unsigned long, char_time, timeout);
932
933 while (info->tx_active) {
934 msleep_interruptible(jiffies_to_msecs(char_time));
935 if (signal_pending(current))
936 break;
937 if (timeout && time_after(jiffies, orig_jiffies + timeout))
938 break;
939 }
940exit:
941 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
942}
943
944static int write_room(struct tty_struct *tty)
945{
946 struct slgt_info *info = tty->driver_data;
947 int ret;
948
949 if (sanity_check(info, tty->name, "write_room"))
950 return 0;
951 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
952 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
953 return ret;
954}
955
956static void flush_chars(struct tty_struct *tty)
957{
958 struct slgt_info *info = tty->driver_data;
959 unsigned long flags;
960
961 if (sanity_check(info, tty->name, "flush_chars"))
962 return;
963 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
964
965 if (info->tx_count <= 0 || tty->stopped ||
966 tty->hw_stopped || !info->tx_buf)
967 return;
968
969 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
970
971 spin_lock_irqsave(&info->lock,flags);
972 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
973 info->tx_count = 0;
974 spin_unlock_irqrestore(&info->lock,flags);
975}
976
977static void flush_buffer(struct tty_struct *tty)
978{
979 struct slgt_info *info = tty->driver_data;
980 unsigned long flags;
981
982 if (sanity_check(info, tty->name, "flush_buffer"))
983 return;
984 DBGINFO(("%s flush_buffer\n", info->device_name));
985
986 spin_lock_irqsave(&info->lock, flags);
987 info->tx_count = 0;
988 spin_unlock_irqrestore(&info->lock, flags);
989
990 tty_wakeup(tty);
991}
992
993/*
994 * throttle (stop) transmitter
995 */
996static void tx_hold(struct tty_struct *tty)
997{
998 struct slgt_info *info = tty->driver_data;
999 unsigned long flags;
1000
1001 if (sanity_check(info, tty->name, "tx_hold"))
1002 return;
1003 DBGINFO(("%s tx_hold\n", info->device_name));
1004 spin_lock_irqsave(&info->lock,flags);
1005 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
1006 tx_stop(info);
1007 spin_unlock_irqrestore(&info->lock,flags);
1008}
1009
1010/*
1011 * release (start) transmitter
1012 */
1013static void tx_release(struct tty_struct *tty)
1014{
1015 struct slgt_info *info = tty->driver_data;
1016 unsigned long flags;
1017
1018 if (sanity_check(info, tty->name, "tx_release"))
1019 return;
1020 DBGINFO(("%s tx_release\n", info->device_name));
1021 spin_lock_irqsave(&info->lock, flags);
1022 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
1023 info->tx_count = 0;
1024 spin_unlock_irqrestore(&info->lock, flags);
1025}
1026
1027/*
1028 * Service an IOCTL request
1029 *
1030 * Arguments
1031 *
1032 * tty pointer to tty instance data
1033 * cmd IOCTL command code
1034 * arg command argument/context
1035 *
1036 * Return 0 if success, otherwise error code
1037 */
1038static int ioctl(struct tty_struct *tty,
1039 unsigned int cmd, unsigned long arg)
1040{
1041 struct slgt_info *info = tty->driver_data;
1042 void __user *argp = (void __user *)arg;
1043 int ret;
1044
1045 if (sanity_check(info, tty->name, "ioctl"))
1046 return -ENODEV;
1047 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1048
1049 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1050 (cmd != TIOCMIWAIT)) {
1051 if (tty->flags & (1 << TTY_IO_ERROR))
1052 return -EIO;
1053 }
1054
1055 switch (cmd) {
1056 case MGSL_IOCWAITEVENT:
1057 return wait_mgsl_event(info, argp);
1058 case TIOCMIWAIT:
1059 return modem_input_wait(info,(int)arg);
1060 case MGSL_IOCSGPIO:
1061 return set_gpio(info, argp);
1062 case MGSL_IOCGGPIO:
1063 return get_gpio(info, argp);
1064 case MGSL_IOCWAITGPIO:
1065 return wait_gpio(info, argp);
1066 case MGSL_IOCGXSYNC:
1067 return get_xsync(info, argp);
1068 case MGSL_IOCSXSYNC:
1069 return set_xsync(info, (int)arg);
1070 case MGSL_IOCGXCTRL:
1071 return get_xctrl(info, argp);
1072 case MGSL_IOCSXCTRL:
1073 return set_xctrl(info, (int)arg);
1074 }
1075 mutex_lock(&info->port.mutex);
1076 switch (cmd) {
1077 case MGSL_IOCGPARAMS:
1078 ret = get_params(info, argp);
1079 break;
1080 case MGSL_IOCSPARAMS:
1081 ret = set_params(info, argp);
1082 break;
1083 case MGSL_IOCGTXIDLE:
1084 ret = get_txidle(info, argp);
1085 break;
1086 case MGSL_IOCSTXIDLE:
1087 ret = set_txidle(info, (int)arg);
1088 break;
1089 case MGSL_IOCTXENABLE:
1090 ret = tx_enable(info, (int)arg);
1091 break;
1092 case MGSL_IOCRXENABLE:
1093 ret = rx_enable(info, (int)arg);
1094 break;
1095 case MGSL_IOCTXABORT:
1096 ret = tx_abort(info);
1097 break;
1098 case MGSL_IOCGSTATS:
1099 ret = get_stats(info, argp);
1100 break;
1101 case MGSL_IOCGIF:
1102 ret = get_interface(info, argp);
1103 break;
1104 case MGSL_IOCSIF:
1105 ret = set_interface(info,(int)arg);
1106 break;
1107 default:
1108 ret = -ENOIOCTLCMD;
1109 }
1110 mutex_unlock(&info->port.mutex);
1111 return ret;
1112}
1113
1114static int get_icount(struct tty_struct *tty,
1115 struct serial_icounter_struct *icount)
1116
1117{
1118 struct slgt_info *info = tty->driver_data;
1119 struct mgsl_icount cnow; /* kernel counter temps */
1120 unsigned long flags;
1121
1122 spin_lock_irqsave(&info->lock,flags);
1123 cnow = info->icount;
1124 spin_unlock_irqrestore(&info->lock,flags);
1125
1126 icount->cts = cnow.cts;
1127 icount->dsr = cnow.dsr;
1128 icount->rng = cnow.rng;
1129 icount->dcd = cnow.dcd;
1130 icount->rx = cnow.rx;
1131 icount->tx = cnow.tx;
1132 icount->frame = cnow.frame;
1133 icount->overrun = cnow.overrun;
1134 icount->parity = cnow.parity;
1135 icount->brk = cnow.brk;
1136 icount->buf_overrun = cnow.buf_overrun;
1137
1138 return 0;
1139}
1140
1141/*
1142 * support for 32 bit ioctl calls on 64 bit systems
1143 */
1144#ifdef CONFIG_COMPAT
1145static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1146{
1147 struct MGSL_PARAMS32 tmp_params;
1148
1149 DBGINFO(("%s get_params32\n", info->device_name));
1150 memset(&tmp_params, 0, sizeof(tmp_params));
1151 tmp_params.mode = (compat_ulong_t)info->params.mode;
1152 tmp_params.loopback = info->params.loopback;
1153 tmp_params.flags = info->params.flags;
1154 tmp_params.encoding = info->params.encoding;
1155 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1156 tmp_params.addr_filter = info->params.addr_filter;
1157 tmp_params.crc_type = info->params.crc_type;
1158 tmp_params.preamble_length = info->params.preamble_length;
1159 tmp_params.preamble = info->params.preamble;
1160 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1161 tmp_params.data_bits = info->params.data_bits;
1162 tmp_params.stop_bits = info->params.stop_bits;
1163 tmp_params.parity = info->params.parity;
1164 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1165 return -EFAULT;
1166 return 0;
1167}
1168
1169static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1170{
1171 struct MGSL_PARAMS32 tmp_params;
1172
1173 DBGINFO(("%s set_params32\n", info->device_name));
1174 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1175 return -EFAULT;
1176
1177 spin_lock(&info->lock);
1178 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
1179 info->base_clock = tmp_params.clock_speed;
1180 } else {
1181 info->params.mode = tmp_params.mode;
1182 info->params.loopback = tmp_params.loopback;
1183 info->params.flags = tmp_params.flags;
1184 info->params.encoding = tmp_params.encoding;
1185 info->params.clock_speed = tmp_params.clock_speed;
1186 info->params.addr_filter = tmp_params.addr_filter;
1187 info->params.crc_type = tmp_params.crc_type;
1188 info->params.preamble_length = tmp_params.preamble_length;
1189 info->params.preamble = tmp_params.preamble;
1190 info->params.data_rate = tmp_params.data_rate;
1191 info->params.data_bits = tmp_params.data_bits;
1192 info->params.stop_bits = tmp_params.stop_bits;
1193 info->params.parity = tmp_params.parity;
1194 }
1195 spin_unlock(&info->lock);
1196
1197 program_hw(info);
1198
1199 return 0;
1200}
1201
1202static long slgt_compat_ioctl(struct tty_struct *tty,
1203 unsigned int cmd, unsigned long arg)
1204{
1205 struct slgt_info *info = tty->driver_data;
1206 int rc = -ENOIOCTLCMD;
1207
1208 if (sanity_check(info, tty->name, "compat_ioctl"))
1209 return -ENODEV;
1210 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1211
1212 switch (cmd) {
1213
1214 case MGSL_IOCSPARAMS32:
1215 rc = set_params32(info, compat_ptr(arg));
1216 break;
1217
1218 case MGSL_IOCGPARAMS32:
1219 rc = get_params32(info, compat_ptr(arg));
1220 break;
1221
1222 case MGSL_IOCGPARAMS:
1223 case MGSL_IOCSPARAMS:
1224 case MGSL_IOCGTXIDLE:
1225 case MGSL_IOCGSTATS:
1226 case MGSL_IOCWAITEVENT:
1227 case MGSL_IOCGIF:
1228 case MGSL_IOCSGPIO:
1229 case MGSL_IOCGGPIO:
1230 case MGSL_IOCWAITGPIO:
1231 case MGSL_IOCGXSYNC:
1232 case MGSL_IOCGXCTRL:
1233 case MGSL_IOCSTXIDLE:
1234 case MGSL_IOCTXENABLE:
1235 case MGSL_IOCRXENABLE:
1236 case MGSL_IOCTXABORT:
1237 case TIOCMIWAIT:
1238 case MGSL_IOCSIF:
1239 case MGSL_IOCSXSYNC:
1240 case MGSL_IOCSXCTRL:
1241 rc = ioctl(tty, cmd, arg);
1242 break;
1243 }
1244
1245 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1246 return rc;
1247}
1248#else
1249#define slgt_compat_ioctl NULL
1250#endif /* ifdef CONFIG_COMPAT */
1251
1252/*
1253 * proc fs support
1254 */
1255static inline void line_info(struct seq_file *m, struct slgt_info *info)
1256{
1257 char stat_buf[30];
1258 unsigned long flags;
1259
1260 seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1261 info->device_name, info->phys_reg_addr,
1262 info->irq_level, info->max_frame_size);
1263
1264 /* output current serial signal states */
1265 spin_lock_irqsave(&info->lock,flags);
1266 get_signals(info);
1267 spin_unlock_irqrestore(&info->lock,flags);
1268
1269 stat_buf[0] = 0;
1270 stat_buf[1] = 0;
1271 if (info->signals & SerialSignal_RTS)
1272 strcat(stat_buf, "|RTS");
1273 if (info->signals & SerialSignal_CTS)
1274 strcat(stat_buf, "|CTS");
1275 if (info->signals & SerialSignal_DTR)
1276 strcat(stat_buf, "|DTR");
1277 if (info->signals & SerialSignal_DSR)
1278 strcat(stat_buf, "|DSR");
1279 if (info->signals & SerialSignal_DCD)
1280 strcat(stat_buf, "|CD");
1281 if (info->signals & SerialSignal_RI)
1282 strcat(stat_buf, "|RI");
1283
1284 if (info->params.mode != MGSL_MODE_ASYNC) {
1285 seq_printf(m, "\tHDLC txok:%d rxok:%d",
1286 info->icount.txok, info->icount.rxok);
1287 if (info->icount.txunder)
1288 seq_printf(m, " txunder:%d", info->icount.txunder);
1289 if (info->icount.txabort)
1290 seq_printf(m, " txabort:%d", info->icount.txabort);
1291 if (info->icount.rxshort)
1292 seq_printf(m, " rxshort:%d", info->icount.rxshort);
1293 if (info->icount.rxlong)
1294 seq_printf(m, " rxlong:%d", info->icount.rxlong);
1295 if (info->icount.rxover)
1296 seq_printf(m, " rxover:%d", info->icount.rxover);
1297 if (info->icount.rxcrc)
1298 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
1299 } else {
1300 seq_printf(m, "\tASYNC tx:%d rx:%d",
1301 info->icount.tx, info->icount.rx);
1302 if (info->icount.frame)
1303 seq_printf(m, " fe:%d", info->icount.frame);
1304 if (info->icount.parity)
1305 seq_printf(m, " pe:%d", info->icount.parity);
1306 if (info->icount.brk)
1307 seq_printf(m, " brk:%d", info->icount.brk);
1308 if (info->icount.overrun)
1309 seq_printf(m, " oe:%d", info->icount.overrun);
1310 }
1311
1312 /* Append serial signal status to end */
1313 seq_printf(m, " %s\n", stat_buf+1);
1314
1315 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1316 info->tx_active,info->bh_requested,info->bh_running,
1317 info->pending_bh);
1318}
1319
1320/* Called to print information about devices
1321 */
1322static int synclink_gt_proc_show(struct seq_file *m, void *v)
1323{
1324 struct slgt_info *info;
1325
1326 seq_puts(m, "synclink_gt driver\n");
1327
1328 info = slgt_device_list;
1329 while( info ) {
1330 line_info(m, info);
1331 info = info->next_device;
1332 }
1333 return 0;
1334}
1335
1336static int synclink_gt_proc_open(struct inode *inode, struct file *file)
1337{
1338 return single_open(file, synclink_gt_proc_show, NULL);
1339}
1340
1341static const struct file_operations synclink_gt_proc_fops = {
1342 .owner = THIS_MODULE,
1343 .open = synclink_gt_proc_open,
1344 .read = seq_read,
1345 .llseek = seq_lseek,
1346 .release = single_release,
1347};
1348
1349/*
1350 * return count of bytes in transmit buffer
1351 */
1352static int chars_in_buffer(struct tty_struct *tty)
1353{
1354 struct slgt_info *info = tty->driver_data;
1355 int count;
1356 if (sanity_check(info, tty->name, "chars_in_buffer"))
1357 return 0;
1358 count = tbuf_bytes(info);
1359 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
1360 return count;
1361}
1362
1363/*
1364 * signal remote device to throttle send data (our receive data)
1365 */
1366static void throttle(struct tty_struct * tty)
1367{
1368 struct slgt_info *info = tty->driver_data;
1369 unsigned long flags;
1370
1371 if (sanity_check(info, tty->name, "throttle"))
1372 return;
1373 DBGINFO(("%s throttle\n", info->device_name));
1374 if (I_IXOFF(tty))
1375 send_xchar(tty, STOP_CHAR(tty));
1376 if (tty->termios->c_cflag & CRTSCTS) {
1377 spin_lock_irqsave(&info->lock,flags);
1378 info->signals &= ~SerialSignal_RTS;
1379 set_signals(info);
1380 spin_unlock_irqrestore(&info->lock,flags);
1381 }
1382}
1383
1384/*
1385 * signal remote device to stop throttling send data (our receive data)
1386 */
1387static void unthrottle(struct tty_struct * tty)
1388{
1389 struct slgt_info *info = tty->driver_data;
1390 unsigned long flags;
1391
1392 if (sanity_check(info, tty->name, "unthrottle"))
1393 return;
1394 DBGINFO(("%s unthrottle\n", info->device_name));
1395 if (I_IXOFF(tty)) {
1396 if (info->x_char)
1397 info->x_char = 0;
1398 else
1399 send_xchar(tty, START_CHAR(tty));
1400 }
1401 if (tty->termios->c_cflag & CRTSCTS) {
1402 spin_lock_irqsave(&info->lock,flags);
1403 info->signals |= SerialSignal_RTS;
1404 set_signals(info);
1405 spin_unlock_irqrestore(&info->lock,flags);
1406 }
1407}
1408
1409/*
1410 * set or clear transmit break condition
1411 * break_state -1=set break condition, 0=clear
1412 */
1413static int set_break(struct tty_struct *tty, int break_state)
1414{
1415 struct slgt_info *info = tty->driver_data;
1416 unsigned short value;
1417 unsigned long flags;
1418
1419 if (sanity_check(info, tty->name, "set_break"))
1420 return -EINVAL;
1421 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1422
1423 spin_lock_irqsave(&info->lock,flags);
1424 value = rd_reg16(info, TCR);
1425 if (break_state == -1)
1426 value |= BIT6;
1427 else
1428 value &= ~BIT6;
1429 wr_reg16(info, TCR, value);
1430 spin_unlock_irqrestore(&info->lock,flags);
1431 return 0;
1432}
1433
1434#if SYNCLINK_GENERIC_HDLC
1435
1436/**
1437 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1438 * set encoding and frame check sequence (FCS) options
1439 *
1440 * dev pointer to network device structure
1441 * encoding serial encoding setting
1442 * parity FCS setting
1443 *
1444 * returns 0 if success, otherwise error code
1445 */
1446static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1447 unsigned short parity)
1448{
1449 struct slgt_info *info = dev_to_port(dev);
1450 unsigned char new_encoding;
1451 unsigned short new_crctype;
1452
1453 /* return error if TTY interface open */
1454 if (info->port.count)
1455 return -EBUSY;
1456
1457 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1458
1459 switch (encoding)
1460 {
1461 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1462 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1463 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1464 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1465 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1466 default: return -EINVAL;
1467 }
1468
1469 switch (parity)
1470 {
1471 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1472 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1473 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1474 default: return -EINVAL;
1475 }
1476
1477 info->params.encoding = new_encoding;
1478 info->params.crc_type = new_crctype;
1479
1480 /* if network interface up, reprogram hardware */
1481 if (info->netcount)
1482 program_hw(info);
1483
1484 return 0;
1485}
1486
1487/**
1488 * called by generic HDLC layer to send frame
1489 *
1490 * skb socket buffer containing HDLC frame
1491 * dev pointer to network device structure
1492 */
1493static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
1494 struct net_device *dev)
1495{
1496 struct slgt_info *info = dev_to_port(dev);
1497 unsigned long flags;
1498
1499 DBGINFO(("%s hdlc_xmit\n", dev->name));
1500
1501 if (!skb->len)
1502 return NETDEV_TX_OK;
1503
1504 /* stop sending until this frame completes */
1505 netif_stop_queue(dev);
1506
1507 /* update network statistics */
1508 dev->stats.tx_packets++;
1509 dev->stats.tx_bytes += skb->len;
1510
1511 /* save start time for transmit timeout detection */
1512 dev->trans_start = jiffies;
1513
1514 spin_lock_irqsave(&info->lock, flags);
1515 tx_load(info, skb->data, skb->len);
1516 spin_unlock_irqrestore(&info->lock, flags);
1517
1518 /* done with socket buffer, so free it */
1519 dev_kfree_skb(skb);
1520
1521 return NETDEV_TX_OK;
1522}
1523
1524/**
1525 * called by network layer when interface enabled
1526 * claim resources and initialize hardware
1527 *
1528 * dev pointer to network device structure
1529 *
1530 * returns 0 if success, otherwise error code
1531 */
1532static int hdlcdev_open(struct net_device *dev)
1533{
1534 struct slgt_info *info = dev_to_port(dev);
1535 int rc;
1536 unsigned long flags;
1537
1538 if (!try_module_get(THIS_MODULE))
1539 return -EBUSY;
1540
1541 DBGINFO(("%s hdlcdev_open\n", dev->name));
1542
1543 /* generic HDLC layer open processing */
1544 if ((rc = hdlc_open(dev)))
1545 return rc;
1546
1547 /* arbitrate between network and tty opens */
1548 spin_lock_irqsave(&info->netlock, flags);
1549 if (info->port.count != 0 || info->netcount != 0) {
1550 DBGINFO(("%s hdlc_open busy\n", dev->name));
1551 spin_unlock_irqrestore(&info->netlock, flags);
1552 return -EBUSY;
1553 }
1554 info->netcount=1;
1555 spin_unlock_irqrestore(&info->netlock, flags);
1556
1557 /* claim resources and init adapter */
1558 if ((rc = startup(info)) != 0) {
1559 spin_lock_irqsave(&info->netlock, flags);
1560 info->netcount=0;
1561 spin_unlock_irqrestore(&info->netlock, flags);
1562 return rc;
1563 }
1564
1565 /* assert DTR and RTS, apply hardware settings */
1566 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1567 program_hw(info);
1568
1569 /* enable network layer transmit */
1570 dev->trans_start = jiffies;
1571 netif_start_queue(dev);
1572
1573 /* inform generic HDLC layer of current DCD status */
1574 spin_lock_irqsave(&info->lock, flags);
1575 get_signals(info);
1576 spin_unlock_irqrestore(&info->lock, flags);
1577 if (info->signals & SerialSignal_DCD)
1578 netif_carrier_on(dev);
1579 else
1580 netif_carrier_off(dev);
1581 return 0;
1582}
1583
1584/**
1585 * called by network layer when interface is disabled
1586 * shutdown hardware and release resources
1587 *
1588 * dev pointer to network device structure
1589 *
1590 * returns 0 if success, otherwise error code
1591 */
1592static int hdlcdev_close(struct net_device *dev)
1593{
1594 struct slgt_info *info = dev_to_port(dev);
1595 unsigned long flags;
1596
1597 DBGINFO(("%s hdlcdev_close\n", dev->name));
1598
1599 netif_stop_queue(dev);
1600
1601 /* shutdown adapter and release resources */
1602 shutdown(info);
1603
1604 hdlc_close(dev);
1605
1606 spin_lock_irqsave(&info->netlock, flags);
1607 info->netcount=0;
1608 spin_unlock_irqrestore(&info->netlock, flags);
1609
1610 module_put(THIS_MODULE);
1611 return 0;
1612}
1613
1614/**
1615 * called by network layer to process IOCTL call to network device
1616 *
1617 * dev pointer to network device structure
1618 * ifr pointer to network interface request structure
1619 * cmd IOCTL command code
1620 *
1621 * returns 0 if success, otherwise error code
1622 */
1623static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1624{
1625 const size_t size = sizeof(sync_serial_settings);
1626 sync_serial_settings new_line;
1627 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1628 struct slgt_info *info = dev_to_port(dev);
1629 unsigned int flags;
1630
1631 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1632
1633 /* return error if TTY interface open */
1634 if (info->port.count)
1635 return -EBUSY;
1636
1637 if (cmd != SIOCWANDEV)
1638 return hdlc_ioctl(dev, ifr, cmd);
1639
1640 memset(&new_line, 0, sizeof(new_line));
1641
1642 switch(ifr->ifr_settings.type) {
1643 case IF_GET_IFACE: /* return current sync_serial_settings */
1644
1645 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1646 if (ifr->ifr_settings.size < size) {
1647 ifr->ifr_settings.size = size; /* data size wanted */
1648 return -ENOBUFS;
1649 }
1650
1651 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1652 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1653 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1654 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1655
1656 switch (flags){
1657 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1658 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1659 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1660 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1661 default: new_line.clock_type = CLOCK_DEFAULT;
1662 }
1663
1664 new_line.clock_rate = info->params.clock_speed;
1665 new_line.loopback = info->params.loopback ? 1:0;
1666
1667 if (copy_to_user(line, &new_line, size))
1668 return -EFAULT;
1669 return 0;
1670
1671 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1672
1673 if(!capable(CAP_NET_ADMIN))
1674 return -EPERM;
1675 if (copy_from_user(&new_line, line, size))
1676 return -EFAULT;
1677
1678 switch (new_line.clock_type)
1679 {
1680 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1681 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1682 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1683 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1684 case CLOCK_DEFAULT: flags = info->params.flags &
1685 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1686 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1687 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1688 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1689 default: return -EINVAL;
1690 }
1691
1692 if (new_line.loopback != 0 && new_line.loopback != 1)
1693 return -EINVAL;
1694
1695 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1696 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1697 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1698 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1699 info->params.flags |= flags;
1700
1701 info->params.loopback = new_line.loopback;
1702
1703 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1704 info->params.clock_speed = new_line.clock_rate;
1705 else
1706 info->params.clock_speed = 0;
1707
1708 /* if network interface up, reprogram hardware */
1709 if (info->netcount)
1710 program_hw(info);
1711 return 0;
1712
1713 default:
1714 return hdlc_ioctl(dev, ifr, cmd);
1715 }
1716}
1717
1718/**
1719 * called by network layer when transmit timeout is detected
1720 *
1721 * dev pointer to network device structure
1722 */
1723static void hdlcdev_tx_timeout(struct net_device *dev)
1724{
1725 struct slgt_info *info = dev_to_port(dev);
1726 unsigned long flags;
1727
1728 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1729
1730 dev->stats.tx_errors++;
1731 dev->stats.tx_aborted_errors++;
1732
1733 spin_lock_irqsave(&info->lock,flags);
1734 tx_stop(info);
1735 spin_unlock_irqrestore(&info->lock,flags);
1736
1737 netif_wake_queue(dev);
1738}
1739
1740/**
1741 * called by device driver when transmit completes
1742 * reenable network layer transmit if stopped
1743 *
1744 * info pointer to device instance information
1745 */
1746static void hdlcdev_tx_done(struct slgt_info *info)
1747{
1748 if (netif_queue_stopped(info->netdev))
1749 netif_wake_queue(info->netdev);
1750}
1751
1752/**
1753 * called by device driver when frame received
1754 * pass frame to network layer
1755 *
1756 * info pointer to device instance information
1757 * buf pointer to buffer contianing frame data
1758 * size count of data bytes in buf
1759 */
1760static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1761{
1762 struct sk_buff *skb = dev_alloc_skb(size);
1763 struct net_device *dev = info->netdev;
1764
1765 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1766
1767 if (skb == NULL) {
1768 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1769 dev->stats.rx_dropped++;
1770 return;
1771 }
1772
1773 memcpy(skb_put(skb, size), buf, size);
1774
1775 skb->protocol = hdlc_type_trans(skb, dev);
1776
1777 dev->stats.rx_packets++;
1778 dev->stats.rx_bytes += size;
1779
1780 netif_rx(skb);
1781}
1782
1783static const struct net_device_ops hdlcdev_ops = {
1784 .ndo_open = hdlcdev_open,
1785 .ndo_stop = hdlcdev_close,
1786 .ndo_change_mtu = hdlc_change_mtu,
1787 .ndo_start_xmit = hdlc_start_xmit,
1788 .ndo_do_ioctl = hdlcdev_ioctl,
1789 .ndo_tx_timeout = hdlcdev_tx_timeout,
1790};
1791
1792/**
1793 * called by device driver when adding device instance
1794 * do generic HDLC initialization
1795 *
1796 * info pointer to device instance information
1797 *
1798 * returns 0 if success, otherwise error code
1799 */
1800static int hdlcdev_init(struct slgt_info *info)
1801{
1802 int rc;
1803 struct net_device *dev;
1804 hdlc_device *hdlc;
1805
1806 /* allocate and initialize network and HDLC layer objects */
1807
1808 if (!(dev = alloc_hdlcdev(info))) {
1809 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1810 return -ENOMEM;
1811 }
1812
1813 /* for network layer reporting purposes only */
1814 dev->mem_start = info->phys_reg_addr;
1815 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1816 dev->irq = info->irq_level;
1817
1818 /* network layer callbacks and settings */
1819 dev->netdev_ops = &hdlcdev_ops;
1820 dev->watchdog_timeo = 10 * HZ;
1821 dev->tx_queue_len = 50;
1822
1823 /* generic HDLC layer callbacks and settings */
1824 hdlc = dev_to_hdlc(dev);
1825 hdlc->attach = hdlcdev_attach;
1826 hdlc->xmit = hdlcdev_xmit;
1827
1828 /* register objects with HDLC layer */
1829 if ((rc = register_hdlc_device(dev))) {
1830 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1831 free_netdev(dev);
1832 return rc;
1833 }
1834
1835 info->netdev = dev;
1836 return 0;
1837}
1838
1839/**
1840 * called by device driver when removing device instance
1841 * do generic HDLC cleanup
1842 *
1843 * info pointer to device instance information
1844 */
1845static void hdlcdev_exit(struct slgt_info *info)
1846{
1847 unregister_hdlc_device(info->netdev);
1848 free_netdev(info->netdev);
1849 info->netdev = NULL;
1850}
1851
1852#endif /* ifdef CONFIG_HDLC */
1853
1854/*
1855 * get async data from rx DMA buffers
1856 */
1857static void rx_async(struct slgt_info *info)
1858{
1859 struct tty_struct *tty = info->port.tty;
1860 struct mgsl_icount *icount = &info->icount;
1861 unsigned int start, end;
1862 unsigned char *p;
1863 unsigned char status;
1864 struct slgt_desc *bufs = info->rbufs;
1865 int i, count;
1866 int chars = 0;
1867 int stat;
1868 unsigned char ch;
1869
1870 start = end = info->rbuf_current;
1871
1872 while(desc_complete(bufs[end])) {
1873 count = desc_count(bufs[end]) - info->rbuf_index;
1874 p = bufs[end].buf + info->rbuf_index;
1875
1876 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1877 DBGDATA(info, p, count, "rx");
1878
1879 for(i=0 ; i < count; i+=2, p+=2) {
1880 ch = *p;
1881 icount->rx++;
1882
1883 stat = 0;
1884
1885 if ((status = *(p+1) & (BIT1 + BIT0))) {
1886 if (status & BIT1)
1887 icount->parity++;
1888 else if (status & BIT0)
1889 icount->frame++;
1890 /* discard char if tty control flags say so */
1891 if (status & info->ignore_status_mask)
1892 continue;
1893 if (status & BIT1)
1894 stat = TTY_PARITY;
1895 else if (status & BIT0)
1896 stat = TTY_FRAME;
1897 }
1898 if (tty) {
1899 tty_insert_flip_char(tty, ch, stat);
1900 chars++;
1901 }
1902 }
1903
1904 if (i < count) {
1905 /* receive buffer not completed */
1906 info->rbuf_index += i;
1907 mod_timer(&info->rx_timer, jiffies + 1);
1908 break;
1909 }
1910
1911 info->rbuf_index = 0;
1912 free_rbufs(info, end, end);
1913
1914 if (++end == info->rbuf_count)
1915 end = 0;
1916
1917 /* if entire list searched then no frame available */
1918 if (end == start)
1919 break;
1920 }
1921
1922 if (tty && chars)
1923 tty_flip_buffer_push(tty);
1924}
1925
1926/*
1927 * return next bottom half action to perform
1928 */
1929static int bh_action(struct slgt_info *info)
1930{
1931 unsigned long flags;
1932 int rc;
1933
1934 spin_lock_irqsave(&info->lock,flags);
1935
1936 if (info->pending_bh & BH_RECEIVE) {
1937 info->pending_bh &= ~BH_RECEIVE;
1938 rc = BH_RECEIVE;
1939 } else if (info->pending_bh & BH_TRANSMIT) {
1940 info->pending_bh &= ~BH_TRANSMIT;
1941 rc = BH_TRANSMIT;
1942 } else if (info->pending_bh & BH_STATUS) {
1943 info->pending_bh &= ~BH_STATUS;
1944 rc = BH_STATUS;
1945 } else {
1946 /* Mark BH routine as complete */
1947 info->bh_running = false;
1948 info->bh_requested = false;
1949 rc = 0;
1950 }
1951
1952 spin_unlock_irqrestore(&info->lock,flags);
1953
1954 return rc;
1955}
1956
1957/*
1958 * perform bottom half processing
1959 */
1960static void bh_handler(struct work_struct *work)
1961{
1962 struct slgt_info *info = container_of(work, struct slgt_info, task);
1963 int action;
1964
1965 if (!info)
1966 return;
1967 info->bh_running = true;
1968
1969 while((action = bh_action(info))) {
1970 switch (action) {
1971 case BH_RECEIVE:
1972 DBGBH(("%s bh receive\n", info->device_name));
1973 switch(info->params.mode) {
1974 case MGSL_MODE_ASYNC:
1975 rx_async(info);
1976 break;
1977 case MGSL_MODE_HDLC:
1978 while(rx_get_frame(info));
1979 break;
1980 case MGSL_MODE_RAW:
1981 case MGSL_MODE_MONOSYNC:
1982 case MGSL_MODE_BISYNC:
1983 case MGSL_MODE_XSYNC:
1984 while(rx_get_buf(info));
1985 break;
1986 }
1987 /* restart receiver if rx DMA buffers exhausted */
1988 if (info->rx_restart)
1989 rx_start(info);
1990 break;
1991 case BH_TRANSMIT:
1992 bh_transmit(info);
1993 break;
1994 case BH_STATUS:
1995 DBGBH(("%s bh status\n", info->device_name));
1996 info->ri_chkcount = 0;
1997 info->dsr_chkcount = 0;
1998 info->dcd_chkcount = 0;
1999 info->cts_chkcount = 0;
2000 break;
2001 default:
2002 DBGBH(("%s unknown action\n", info->device_name));
2003 break;
2004 }
2005 }
2006 DBGBH(("%s bh_handler exit\n", info->device_name));
2007}
2008
2009static void bh_transmit(struct slgt_info *info)
2010{
2011 struct tty_struct *tty = info->port.tty;
2012
2013 DBGBH(("%s bh_transmit\n", info->device_name));
2014 if (tty)
2015 tty_wakeup(tty);
2016}
2017
2018static void dsr_change(struct slgt_info *info, unsigned short status)
2019{
2020 if (status & BIT3) {
2021 info->signals |= SerialSignal_DSR;
2022 info->input_signal_events.dsr_up++;
2023 } else {
2024 info->signals &= ~SerialSignal_DSR;
2025 info->input_signal_events.dsr_down++;
2026 }
2027 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2028 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2029 slgt_irq_off(info, IRQ_DSR);
2030 return;
2031 }
2032 info->icount.dsr++;
2033 wake_up_interruptible(&info->status_event_wait_q);
2034 wake_up_interruptible(&info->event_wait_q);
2035 info->pending_bh |= BH_STATUS;
2036}
2037
2038static void cts_change(struct slgt_info *info, unsigned short status)
2039{
2040 if (status & BIT2) {
2041 info->signals |= SerialSignal_CTS;
2042 info->input_signal_events.cts_up++;
2043 } else {
2044 info->signals &= ~SerialSignal_CTS;
2045 info->input_signal_events.cts_down++;
2046 }
2047 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2048 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2049 slgt_irq_off(info, IRQ_CTS);
2050 return;
2051 }
2052 info->icount.cts++;
2053 wake_up_interruptible(&info->status_event_wait_q);
2054 wake_up_interruptible(&info->event_wait_q);
2055 info->pending_bh |= BH_STATUS;
2056
2057 if (info->port.flags & ASYNC_CTS_FLOW) {
2058 if (info->port.tty) {
2059 if (info->port.tty->hw_stopped) {
2060 if (info->signals & SerialSignal_CTS) {
2061 info->port.tty->hw_stopped = 0;
2062 info->pending_bh |= BH_TRANSMIT;
2063 return;
2064 }
2065 } else {
2066 if (!(info->signals & SerialSignal_CTS))
2067 info->port.tty->hw_stopped = 1;
2068 }
2069 }
2070 }
2071}
2072
2073static void dcd_change(struct slgt_info *info, unsigned short status)
2074{
2075 if (status & BIT1) {
2076 info->signals |= SerialSignal_DCD;
2077 info->input_signal_events.dcd_up++;
2078 } else {
2079 info->signals &= ~SerialSignal_DCD;
2080 info->input_signal_events.dcd_down++;
2081 }
2082 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2083 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2084 slgt_irq_off(info, IRQ_DCD);
2085 return;
2086 }
2087 info->icount.dcd++;
2088#if SYNCLINK_GENERIC_HDLC
2089 if (info->netcount) {
2090 if (info->signals & SerialSignal_DCD)
2091 netif_carrier_on(info->netdev);
2092 else
2093 netif_carrier_off(info->netdev);
2094 }
2095#endif
2096 wake_up_interruptible(&info->status_event_wait_q);
2097 wake_up_interruptible(&info->event_wait_q);
2098 info->pending_bh |= BH_STATUS;
2099
2100 if (info->port.flags & ASYNC_CHECK_CD) {
2101 if (info->signals & SerialSignal_DCD)
2102 wake_up_interruptible(&info->port.open_wait);
2103 else {
2104 if (info->port.tty)
2105 tty_hangup(info->port.tty);
2106 }
2107 }
2108}
2109
2110static void ri_change(struct slgt_info *info, unsigned short status)
2111{
2112 if (status & BIT0) {
2113 info->signals |= SerialSignal_RI;
2114 info->input_signal_events.ri_up++;
2115 } else {
2116 info->signals &= ~SerialSignal_RI;
2117 info->input_signal_events.ri_down++;
2118 }
2119 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2120 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2121 slgt_irq_off(info, IRQ_RI);
2122 return;
2123 }
2124 info->icount.rng++;
2125 wake_up_interruptible(&info->status_event_wait_q);
2126 wake_up_interruptible(&info->event_wait_q);
2127 info->pending_bh |= BH_STATUS;
2128}
2129
2130static void isr_rxdata(struct slgt_info *info)
2131{
2132 unsigned int count = info->rbuf_fill_count;
2133 unsigned int i = info->rbuf_fill_index;
2134 unsigned short reg;
2135
2136 while (rd_reg16(info, SSR) & IRQ_RXDATA) {
2137 reg = rd_reg16(info, RDR);
2138 DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
2139 if (desc_complete(info->rbufs[i])) {
2140 /* all buffers full */
2141 rx_stop(info);
2142 info->rx_restart = 1;
2143 continue;
2144 }
2145 info->rbufs[i].buf[count++] = (unsigned char)reg;
2146 /* async mode saves status byte to buffer for each data byte */
2147 if (info->params.mode == MGSL_MODE_ASYNC)
2148 info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
2149 if (count == info->rbuf_fill_level || (reg & BIT10)) {
2150 /* buffer full or end of frame */
2151 set_desc_count(info->rbufs[i], count);
2152 set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
2153 info->rbuf_fill_count = count = 0;
2154 if (++i == info->rbuf_count)
2155 i = 0;
2156 info->pending_bh |= BH_RECEIVE;
2157 }
2158 }
2159
2160 info->rbuf_fill_index = i;
2161 info->rbuf_fill_count = count;
2162}
2163
2164static void isr_serial(struct slgt_info *info)
2165{
2166 unsigned short status = rd_reg16(info, SSR);
2167
2168 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2169
2170 wr_reg16(info, SSR, status); /* clear pending */
2171
2172 info->irq_occurred = true;
2173
2174 if (info->params.mode == MGSL_MODE_ASYNC) {
2175 if (status & IRQ_TXIDLE) {
2176 if (info->tx_active)
2177 isr_txeom(info, status);
2178 }
2179 if (info->rx_pio && (status & IRQ_RXDATA))
2180 isr_rxdata(info);
2181 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2182 info->icount.brk++;
2183 /* process break detection if tty control allows */
2184 if (info->port.tty) {
2185 if (!(status & info->ignore_status_mask)) {
2186 if (info->read_status_mask & MASK_BREAK) {
2187 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2188 if (info->port.flags & ASYNC_SAK)
2189 do_SAK(info->port.tty);
2190 }
2191 }
2192 }
2193 }
2194 } else {
2195 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2196 isr_txeom(info, status);
2197 if (info->rx_pio && (status & IRQ_RXDATA))
2198 isr_rxdata(info);
2199 if (status & IRQ_RXIDLE) {
2200 if (status & RXIDLE)
2201 info->icount.rxidle++;
2202 else
2203 info->icount.exithunt++;
2204 wake_up_interruptible(&info->event_wait_q);
2205 }
2206
2207 if (status & IRQ_RXOVER)
2208 rx_start(info);
2209 }
2210
2211 if (status & IRQ_DSR)
2212 dsr_change(info, status);
2213 if (status & IRQ_CTS)
2214 cts_change(info, status);
2215 if (status & IRQ_DCD)
2216 dcd_change(info, status);
2217 if (status & IRQ_RI)
2218 ri_change(info, status);
2219}
2220
2221static void isr_rdma(struct slgt_info *info)
2222{
2223 unsigned int status = rd_reg32(info, RDCSR);
2224
2225 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2226
2227 /* RDCSR (rx DMA control/status)
2228 *
2229 * 31..07 reserved
2230 * 06 save status byte to DMA buffer
2231 * 05 error
2232 * 04 eol (end of list)
2233 * 03 eob (end of buffer)
2234 * 02 IRQ enable
2235 * 01 reset
2236 * 00 enable
2237 */
2238 wr_reg32(info, RDCSR, status); /* clear pending */
2239
2240 if (status & (BIT5 + BIT4)) {
2241 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2242 info->rx_restart = true;
2243 }
2244 info->pending_bh |= BH_RECEIVE;
2245}
2246
2247static void isr_tdma(struct slgt_info *info)
2248{
2249 unsigned int status = rd_reg32(info, TDCSR);
2250
2251 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2252
2253 /* TDCSR (tx DMA control/status)
2254 *
2255 * 31..06 reserved
2256 * 05 error
2257 * 04 eol (end of list)
2258 * 03 eob (end of buffer)
2259 * 02 IRQ enable
2260 * 01 reset
2261 * 00 enable
2262 */
2263 wr_reg32(info, TDCSR, status); /* clear pending */
2264
2265 if (status & (BIT5 + BIT4 + BIT3)) {
2266 // another transmit buffer has completed
2267 // run bottom half to get more send data from user
2268 info->pending_bh |= BH_TRANSMIT;
2269 }
2270}
2271
2272/*
2273 * return true if there are unsent tx DMA buffers, otherwise false
2274 *
2275 * if there are unsent buffers then info->tbuf_start
2276 * is set to index of first unsent buffer
2277 */
2278static bool unsent_tbufs(struct slgt_info *info)
2279{
2280 unsigned int i = info->tbuf_current;
2281 bool rc = false;
2282
2283 /*
2284 * search backwards from last loaded buffer (precedes tbuf_current)
2285 * for first unsent buffer (desc_count > 0)
2286 */
2287
2288 do {
2289 if (i)
2290 i--;
2291 else
2292 i = info->tbuf_count - 1;
2293 if (!desc_count(info->tbufs[i]))
2294 break;
2295 info->tbuf_start = i;
2296 rc = true;
2297 } while (i != info->tbuf_current);
2298
2299 return rc;
2300}
2301
2302static void isr_txeom(struct slgt_info *info, unsigned short status)
2303{
2304 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2305
2306 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2307 tdma_reset(info);
2308 if (status & IRQ_TXUNDER) {
2309 unsigned short val = rd_reg16(info, TCR);
2310 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2311 wr_reg16(info, TCR, val); /* clear reset bit */
2312 }
2313
2314 if (info->tx_active) {
2315 if (info->params.mode != MGSL_MODE_ASYNC) {
2316 if (status & IRQ_TXUNDER)
2317 info->icount.txunder++;
2318 else if (status & IRQ_TXIDLE)
2319 info->icount.txok++;
2320 }
2321
2322 if (unsent_tbufs(info)) {
2323 tx_start(info);
2324 update_tx_timer(info);
2325 return;
2326 }
2327 info->tx_active = false;
2328
2329 del_timer(&info->tx_timer);
2330
2331 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2332 info->signals &= ~SerialSignal_RTS;
2333 info->drop_rts_on_tx_done = false;
2334 set_signals(info);
2335 }
2336
2337#if SYNCLINK_GENERIC_HDLC
2338 if (info->netcount)
2339 hdlcdev_tx_done(info);
2340 else
2341#endif
2342 {
2343 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2344 tx_stop(info);
2345 return;
2346 }
2347 info->pending_bh |= BH_TRANSMIT;
2348 }
2349 }
2350}
2351
2352static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2353{
2354 struct cond_wait *w, *prev;
2355
2356 /* wake processes waiting for specific transitions */
2357 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2358 if (w->data & changed) {
2359 w->data = state;
2360 wake_up_interruptible(&w->q);
2361 if (prev != NULL)
2362 prev->next = w->next;
2363 else
2364 info->gpio_wait_q = w->next;
2365 } else
2366 prev = w;
2367 }
2368}
2369
2370/* interrupt service routine
2371 *
2372 * irq interrupt number
2373 * dev_id device ID supplied during interrupt registration
2374 */
2375static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
2376{
2377 struct slgt_info *info = dev_id;
2378 unsigned int gsr;
2379 unsigned int i;
2380
2381 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
2382
2383 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2384 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2385 info->irq_occurred = true;
2386 for(i=0; i < info->port_count ; i++) {
2387 if (info->port_array[i] == NULL)
2388 continue;
2389 spin_lock(&info->port_array[i]->lock);
2390 if (gsr & (BIT8 << i))
2391 isr_serial(info->port_array[i]);
2392 if (gsr & (BIT16 << (i*2)))
2393 isr_rdma(info->port_array[i]);
2394 if (gsr & (BIT17 << (i*2)))
2395 isr_tdma(info->port_array[i]);
2396 spin_unlock(&info->port_array[i]->lock);
2397 }
2398 }
2399
2400 if (info->gpio_present) {
2401 unsigned int state;
2402 unsigned int changed;
2403 spin_lock(&info->lock);
2404 while ((changed = rd_reg32(info, IOSR)) != 0) {
2405 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2406 /* read latched state of GPIO signals */
2407 state = rd_reg32(info, IOVR);
2408 /* clear pending GPIO interrupt bits */
2409 wr_reg32(info, IOSR, changed);
2410 for (i=0 ; i < info->port_count ; i++) {
2411 if (info->port_array[i] != NULL)
2412 isr_gpio(info->port_array[i], changed, state);
2413 }
2414 }
2415 spin_unlock(&info->lock);
2416 }
2417
2418 for(i=0; i < info->port_count ; i++) {
2419 struct slgt_info *port = info->port_array[i];
2420 if (port == NULL)
2421 continue;
2422 spin_lock(&port->lock);
2423 if ((port->port.count || port->netcount) &&
2424 port->pending_bh && !port->bh_running &&
2425 !port->bh_requested) {
2426 DBGISR(("%s bh queued\n", port->device_name));
2427 schedule_work(&port->task);
2428 port->bh_requested = true;
2429 }
2430 spin_unlock(&port->lock);
2431 }
2432
2433 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
2434 return IRQ_HANDLED;
2435}
2436
2437static int startup(struct slgt_info *info)
2438{
2439 DBGINFO(("%s startup\n", info->device_name));
2440
2441 if (info->port.flags & ASYNC_INITIALIZED)
2442 return 0;
2443
2444 if (!info->tx_buf) {
2445 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2446 if (!info->tx_buf) {
2447 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2448 return -ENOMEM;
2449 }
2450 }
2451
2452 info->pending_bh = 0;
2453
2454 memset(&info->icount, 0, sizeof(info->icount));
2455
2456 /* program hardware for current parameters */
2457 change_params(info);
2458
2459 if (info->port.tty)
2460 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2461
2462 info->port.flags |= ASYNC_INITIALIZED;
2463
2464 return 0;
2465}
2466
2467/*
2468 * called by close() and hangup() to shutdown hardware
2469 */
2470static void shutdown(struct slgt_info *info)
2471{
2472 unsigned long flags;
2473
2474 if (!(info->port.flags & ASYNC_INITIALIZED))
2475 return;
2476
2477 DBGINFO(("%s shutdown\n", info->device_name));
2478
2479 /* clear status wait queue because status changes */
2480 /* can't happen after shutting down the hardware */
2481 wake_up_interruptible(&info->status_event_wait_q);
2482 wake_up_interruptible(&info->event_wait_q);
2483
2484 del_timer_sync(&info->tx_timer);
2485 del_timer_sync(&info->rx_timer);
2486
2487 kfree(info->tx_buf);
2488 info->tx_buf = NULL;
2489
2490 spin_lock_irqsave(&info->lock,flags);
2491
2492 tx_stop(info);
2493 rx_stop(info);
2494
2495 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2496
2497 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2498 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2499 set_signals(info);
2500 }
2501
2502 flush_cond_wait(&info->gpio_wait_q);
2503
2504 spin_unlock_irqrestore(&info->lock,flags);
2505
2506 if (info->port.tty)
2507 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2508
2509 info->port.flags &= ~ASYNC_INITIALIZED;
2510}
2511
2512static void program_hw(struct slgt_info *info)
2513{
2514 unsigned long flags;
2515
2516 spin_lock_irqsave(&info->lock,flags);
2517
2518 rx_stop(info);
2519 tx_stop(info);
2520
2521 if (info->params.mode != MGSL_MODE_ASYNC ||
2522 info->netcount)
2523 sync_mode(info);
2524 else
2525 async_mode(info);
2526
2527 set_signals(info);
2528
2529 info->dcd_chkcount = 0;
2530 info->cts_chkcount = 0;
2531 info->ri_chkcount = 0;
2532 info->dsr_chkcount = 0;
2533
2534 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
2535 get_signals(info);
2536
2537 if (info->netcount ||
2538 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
2539 rx_start(info);
2540
2541 spin_unlock_irqrestore(&info->lock,flags);
2542}
2543
2544/*
2545 * reconfigure adapter based on new parameters
2546 */
2547static void change_params(struct slgt_info *info)
2548{
2549 unsigned cflag;
2550 int bits_per_char;
2551
2552 if (!info->port.tty || !info->port.tty->termios)
2553 return;
2554 DBGINFO(("%s change_params\n", info->device_name));
2555
2556 cflag = info->port.tty->termios->c_cflag;
2557
2558 /* if B0 rate (hangup) specified then negate DTR and RTS */
2559 /* otherwise assert DTR and RTS */
2560 if (cflag & CBAUD)
2561 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2562 else
2563 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2564
2565 /* byte size and parity */
2566
2567 switch (cflag & CSIZE) {
2568 case CS5: info->params.data_bits = 5; break;
2569 case CS6: info->params.data_bits = 6; break;
2570 case CS7: info->params.data_bits = 7; break;
2571 case CS8: info->params.data_bits = 8; break;
2572 default: info->params.data_bits = 7; break;
2573 }
2574
2575 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2576
2577 if (cflag & PARENB)
2578 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2579 else
2580 info->params.parity = ASYNC_PARITY_NONE;
2581
2582 /* calculate number of jiffies to transmit a full
2583 * FIFO (32 bytes) at specified data rate
2584 */
2585 bits_per_char = info->params.data_bits +
2586 info->params.stop_bits + 1;
2587
2588 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2589
2590 if (info->params.data_rate) {
2591 info->timeout = (32*HZ*bits_per_char) /
2592 info->params.data_rate;
2593 }
2594 info->timeout += HZ/50; /* Add .02 seconds of slop */
2595
2596 if (cflag & CRTSCTS)
2597 info->port.flags |= ASYNC_CTS_FLOW;
2598 else
2599 info->port.flags &= ~ASYNC_CTS_FLOW;
2600
2601 if (cflag & CLOCAL)
2602 info->port.flags &= ~ASYNC_CHECK_CD;
2603 else
2604 info->port.flags |= ASYNC_CHECK_CD;
2605
2606 /* process tty input control flags */
2607
2608 info->read_status_mask = IRQ_RXOVER;
2609 if (I_INPCK(info->port.tty))
2610 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2611 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2612 info->read_status_mask |= MASK_BREAK;
2613 if (I_IGNPAR(info->port.tty))
2614 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2615 if (I_IGNBRK(info->port.tty)) {
2616 info->ignore_status_mask |= MASK_BREAK;
2617 /* If ignoring parity and break indicators, ignore
2618 * overruns too. (For real raw support).
2619 */
2620 if (I_IGNPAR(info->port.tty))
2621 info->ignore_status_mask |= MASK_OVERRUN;
2622 }
2623
2624 program_hw(info);
2625}
2626
2627static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2628{
2629 DBGINFO(("%s get_stats\n", info->device_name));
2630 if (!user_icount) {
2631 memset(&info->icount, 0, sizeof(info->icount));
2632 } else {
2633 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2634 return -EFAULT;
2635 }
2636 return 0;
2637}
2638
2639static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2640{
2641 DBGINFO(("%s get_params\n", info->device_name));
2642 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2643 return -EFAULT;
2644 return 0;
2645}
2646
2647static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2648{
2649 unsigned long flags;
2650 MGSL_PARAMS tmp_params;
2651
2652 DBGINFO(("%s set_params\n", info->device_name));
2653 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2654 return -EFAULT;
2655
2656 spin_lock_irqsave(&info->lock, flags);
2657 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
2658 info->base_clock = tmp_params.clock_speed;
2659 else
2660 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2661 spin_unlock_irqrestore(&info->lock, flags);
2662
2663 program_hw(info);
2664
2665 return 0;
2666}
2667
2668static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2669{
2670 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2671 if (put_user(info->idle_mode, idle_mode))
2672 return -EFAULT;
2673 return 0;
2674}
2675
2676static int set_txidle(struct slgt_info *info, int idle_mode)
2677{
2678 unsigned long flags;
2679 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2680 spin_lock_irqsave(&info->lock,flags);
2681 info->idle_mode = idle_mode;
2682 if (info->params.mode != MGSL_MODE_ASYNC)
2683 tx_set_idle(info);
2684 spin_unlock_irqrestore(&info->lock,flags);
2685 return 0;
2686}
2687
2688static int tx_enable(struct slgt_info *info, int enable)
2689{
2690 unsigned long flags;
2691 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2692 spin_lock_irqsave(&info->lock,flags);
2693 if (enable) {
2694 if (!info->tx_enabled)
2695 tx_start(info);
2696 } else {
2697 if (info->tx_enabled)
2698 tx_stop(info);
2699 }
2700 spin_unlock_irqrestore(&info->lock,flags);
2701 return 0;
2702}
2703
2704/*
2705 * abort transmit HDLC frame
2706 */
2707static int tx_abort(struct slgt_info *info)
2708{
2709 unsigned long flags;
2710 DBGINFO(("%s tx_abort\n", info->device_name));
2711 spin_lock_irqsave(&info->lock,flags);
2712 tdma_reset(info);
2713 spin_unlock_irqrestore(&info->lock,flags);
2714 return 0;
2715}
2716
2717static int rx_enable(struct slgt_info *info, int enable)
2718{
2719 unsigned long flags;
2720 unsigned int rbuf_fill_level;
2721 DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
2722 spin_lock_irqsave(&info->lock,flags);
2723 /*
2724 * enable[31..16] = receive DMA buffer fill level
2725 * 0 = noop (leave fill level unchanged)
2726 * fill level must be multiple of 4 and <= buffer size
2727 */
2728 rbuf_fill_level = ((unsigned int)enable) >> 16;
2729 if (rbuf_fill_level) {
2730 if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
2731 spin_unlock_irqrestore(&info->lock, flags);
2732 return -EINVAL;
2733 }
2734 info->rbuf_fill_level = rbuf_fill_level;
2735 if (rbuf_fill_level < 128)
2736 info->rx_pio = 1; /* PIO mode */
2737 else
2738 info->rx_pio = 0; /* DMA mode */
2739 rx_stop(info); /* restart receiver to use new fill level */
2740 }
2741
2742 /*
2743 * enable[1..0] = receiver enable command
2744 * 0 = disable
2745 * 1 = enable
2746 * 2 = enable or force hunt mode if already enabled
2747 */
2748 enable &= 3;
2749 if (enable) {
2750 if (!info->rx_enabled)
2751 rx_start(info);
2752 else if (enable == 2) {
2753 /* force hunt mode (write 1 to RCR[3]) */
2754 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2755 }
2756 } else {
2757 if (info->rx_enabled)
2758 rx_stop(info);
2759 }
2760 spin_unlock_irqrestore(&info->lock,flags);
2761 return 0;
2762}
2763
2764/*
2765 * wait for specified event to occur
2766 */
2767static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2768{
2769 unsigned long flags;
2770 int s;
2771 int rc=0;
2772 struct mgsl_icount cprev, cnow;
2773 int events;
2774 int mask;
2775 struct _input_signal_events oldsigs, newsigs;
2776 DECLARE_WAITQUEUE(wait, current);
2777
2778 if (get_user(mask, mask_ptr))
2779 return -EFAULT;
2780
2781 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2782
2783 spin_lock_irqsave(&info->lock,flags);
2784
2785 /* return immediately if state matches requested events */
2786 get_signals(info);
2787 s = info->signals;
2788
2789 events = mask &
2790 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2791 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2792 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2793 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2794 if (events) {
2795 spin_unlock_irqrestore(&info->lock,flags);
2796 goto exit;
2797 }
2798
2799 /* save current irq counts */
2800 cprev = info->icount;
2801 oldsigs = info->input_signal_events;
2802
2803 /* enable hunt and idle irqs if needed */
2804 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2805 unsigned short val = rd_reg16(info, SCR);
2806 if (!(val & IRQ_RXIDLE))
2807 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2808 }
2809
2810 set_current_state(TASK_INTERRUPTIBLE);
2811 add_wait_queue(&info->event_wait_q, &wait);
2812
2813 spin_unlock_irqrestore(&info->lock,flags);
2814
2815 for(;;) {
2816 schedule();
2817 if (signal_pending(current)) {
2818 rc = -ERESTARTSYS;
2819 break;
2820 }
2821
2822 /* get current irq counts */
2823 spin_lock_irqsave(&info->lock,flags);
2824 cnow = info->icount;
2825 newsigs = info->input_signal_events;
2826 set_current_state(TASK_INTERRUPTIBLE);
2827 spin_unlock_irqrestore(&info->lock,flags);
2828
2829 /* if no change, wait aborted for some reason */
2830 if (newsigs.dsr_up == oldsigs.dsr_up &&
2831 newsigs.dsr_down == oldsigs.dsr_down &&
2832 newsigs.dcd_up == oldsigs.dcd_up &&
2833 newsigs.dcd_down == oldsigs.dcd_down &&
2834 newsigs.cts_up == oldsigs.cts_up &&
2835 newsigs.cts_down == oldsigs.cts_down &&
2836 newsigs.ri_up == oldsigs.ri_up &&
2837 newsigs.ri_down == oldsigs.ri_down &&
2838 cnow.exithunt == cprev.exithunt &&
2839 cnow.rxidle == cprev.rxidle) {
2840 rc = -EIO;
2841 break;
2842 }
2843
2844 events = mask &
2845 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2846 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2847 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2848 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2849 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2850 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2851 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2852 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2853 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2854 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2855 if (events)
2856 break;
2857
2858 cprev = cnow;
2859 oldsigs = newsigs;
2860 }
2861
2862 remove_wait_queue(&info->event_wait_q, &wait);
2863 set_current_state(TASK_RUNNING);
2864
2865
2866 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2867 spin_lock_irqsave(&info->lock,flags);
2868 if (!waitqueue_active(&info->event_wait_q)) {
2869 /* disable enable exit hunt mode/idle rcvd IRQs */
2870 wr_reg16(info, SCR,
2871 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2872 }
2873 spin_unlock_irqrestore(&info->lock,flags);
2874 }
2875exit:
2876 if (rc == 0)
2877 rc = put_user(events, mask_ptr);
2878 return rc;
2879}
2880
2881static int get_interface(struct slgt_info *info, int __user *if_mode)
2882{
2883 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2884 if (put_user(info->if_mode, if_mode))
2885 return -EFAULT;
2886 return 0;
2887}
2888
2889static int set_interface(struct slgt_info *info, int if_mode)
2890{
2891 unsigned long flags;
2892 unsigned short val;
2893
2894 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2895 spin_lock_irqsave(&info->lock,flags);
2896 info->if_mode = if_mode;
2897
2898 msc_set_vcr(info);
2899
2900 /* TCR (tx control) 07 1=RTS driver control */
2901 val = rd_reg16(info, TCR);
2902 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2903 val |= BIT7;
2904 else
2905 val &= ~BIT7;
2906 wr_reg16(info, TCR, val);
2907
2908 spin_unlock_irqrestore(&info->lock,flags);
2909 return 0;
2910}
2911
2912static int get_xsync(struct slgt_info *info, int __user *xsync)
2913{
2914 DBGINFO(("%s get_xsync=%x\n", info->device_name, info->xsync));
2915 if (put_user(info->xsync, xsync))
2916 return -EFAULT;
2917 return 0;
2918}
2919
2920/*
2921 * set extended sync pattern (1 to 4 bytes) for extended sync mode
2922 *
2923 * sync pattern is contained in least significant bytes of value
2924 * most significant byte of sync pattern is oldest (1st sent/detected)
2925 */
2926static int set_xsync(struct slgt_info *info, int xsync)
2927{
2928 unsigned long flags;
2929
2930 DBGINFO(("%s set_xsync=%x)\n", info->device_name, xsync));
2931 spin_lock_irqsave(&info->lock, flags);
2932 info->xsync = xsync;
2933 wr_reg32(info, XSR, xsync);
2934 spin_unlock_irqrestore(&info->lock, flags);
2935 return 0;
2936}
2937
2938static int get_xctrl(struct slgt_info *info, int __user *xctrl)
2939{
2940 DBGINFO(("%s get_xctrl=%x\n", info->device_name, info->xctrl));
2941 if (put_user(info->xctrl, xctrl))
2942 return -EFAULT;
2943 return 0;
2944}
2945
2946/*
2947 * set extended control options
2948 *
2949 * xctrl[31:19] reserved, must be zero
2950 * xctrl[18:17] extended sync pattern length in bytes
2951 * 00 = 1 byte in xsr[7:0]
2952 * 01 = 2 bytes in xsr[15:0]
2953 * 10 = 3 bytes in xsr[23:0]
2954 * 11 = 4 bytes in xsr[31:0]
2955 * xctrl[16] 1 = enable terminal count, 0=disabled
2956 * xctrl[15:0] receive terminal count for fixed length packets
2957 * value is count minus one (0 = 1 byte packet)
2958 * when terminal count is reached, receiver
2959 * automatically returns to hunt mode and receive
2960 * FIFO contents are flushed to DMA buffers with
2961 * end of frame (EOF) status
2962 */
2963static int set_xctrl(struct slgt_info *info, int xctrl)
2964{
2965 unsigned long flags;
2966
2967 DBGINFO(("%s set_xctrl=%x)\n", info->device_name, xctrl));
2968 spin_lock_irqsave(&info->lock, flags);
2969 info->xctrl = xctrl;
2970 wr_reg32(info, XCR, xctrl);
2971 spin_unlock_irqrestore(&info->lock, flags);
2972 return 0;
2973}
2974
2975/*
2976 * set general purpose IO pin state and direction
2977 *
2978 * user_gpio fields:
2979 * state each bit indicates a pin state
2980 * smask set bit indicates pin state to set
2981 * dir each bit indicates a pin direction (0=input, 1=output)
2982 * dmask set bit indicates pin direction to set
2983 */
2984static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2985{
2986 unsigned long flags;
2987 struct gpio_desc gpio;
2988 __u32 data;
2989
2990 if (!info->gpio_present)
2991 return -EINVAL;
2992 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2993 return -EFAULT;
2994 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2995 info->device_name, gpio.state, gpio.smask,
2996 gpio.dir, gpio.dmask));
2997
2998 spin_lock_irqsave(&info->port_array[0]->lock, flags);
2999 if (gpio.dmask) {
3000 data = rd_reg32(info, IODR);
3001 data |= gpio.dmask & gpio.dir;
3002 data &= ~(gpio.dmask & ~gpio.dir);
3003 wr_reg32(info, IODR, data);
3004 }
3005 if (gpio.smask) {
3006 data = rd_reg32(info, IOVR);
3007 data |= gpio.smask & gpio.state;
3008 data &= ~(gpio.smask & ~gpio.state);
3009 wr_reg32(info, IOVR, data);
3010 }
3011 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3012
3013 return 0;
3014}
3015
3016/*
3017 * get general purpose IO pin state and direction
3018 */
3019static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
3020{
3021 struct gpio_desc gpio;
3022 if (!info->gpio_present)
3023 return -EINVAL;
3024 gpio.state = rd_reg32(info, IOVR);
3025 gpio.smask = 0xffffffff;
3026 gpio.dir = rd_reg32(info, IODR);
3027 gpio.dmask = 0xffffffff;
3028 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3029 return -EFAULT;
3030 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
3031 info->device_name, gpio.state, gpio.dir));
3032 return 0;
3033}
3034
3035/*
3036 * conditional wait facility
3037 */
3038static void init_cond_wait(struct cond_wait *w, unsigned int data)
3039{
3040 init_waitqueue_head(&w->q);
3041 init_waitqueue_entry(&w->wait, current);
3042 w->data = data;
3043}
3044
3045static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
3046{
3047 set_current_state(TASK_INTERRUPTIBLE);
3048 add_wait_queue(&w->q, &w->wait);
3049 w->next = *head;
3050 *head = w;
3051}
3052
3053static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
3054{
3055 struct cond_wait *w, *prev;
3056 remove_wait_queue(&cw->q, &cw->wait);
3057 set_current_state(TASK_RUNNING);
3058 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
3059 if (w == cw) {
3060 if (prev != NULL)
3061 prev->next = w->next;
3062 else
3063 *head = w->next;
3064 break;
3065 }
3066 }
3067}
3068
3069static void flush_cond_wait(struct cond_wait **head)
3070{
3071 while (*head != NULL) {
3072 wake_up_interruptible(&(*head)->q);
3073 *head = (*head)->next;
3074 }
3075}
3076
3077/*
3078 * wait for general purpose I/O pin(s) to enter specified state
3079 *
3080 * user_gpio fields:
3081 * state - bit indicates target pin state
3082 * smask - set bit indicates watched pin
3083 *
3084 * The wait ends when at least one watched pin enters the specified
3085 * state. When 0 (no error) is returned, user_gpio->state is set to the
3086 * state of all GPIO pins when the wait ends.
3087 *
3088 * Note: Each pin may be a dedicated input, dedicated output, or
3089 * configurable input/output. The number and configuration of pins
3090 * varies with the specific adapter model. Only input pins (dedicated
3091 * or configured) can be monitored with this function.
3092 */
3093static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
3094{
3095 unsigned long flags;
3096 int rc = 0;
3097 struct gpio_desc gpio;
3098 struct cond_wait wait;
3099 u32 state;
3100
3101 if (!info->gpio_present)
3102 return -EINVAL;
3103 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
3104 return -EFAULT;
3105 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
3106 info->device_name, gpio.state, gpio.smask));
3107 /* ignore output pins identified by set IODR bit */
3108 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
3109 return -EINVAL;
3110 init_cond_wait(&wait, gpio.smask);
3111
3112 spin_lock_irqsave(&info->port_array[0]->lock, flags);
3113 /* enable interrupts for watched pins */
3114 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
3115 /* get current pin states */
3116 state = rd_reg32(info, IOVR);
3117
3118 if (gpio.smask & ~(state ^ gpio.state)) {
3119 /* already in target state */
3120 gpio.state = state;
3121 } else {
3122 /* wait for target state */
3123 add_cond_wait(&info->gpio_wait_q, &wait);
3124 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3125 schedule();
3126 if (signal_pending(current))
3127 rc = -ERESTARTSYS;
3128 else
3129 gpio.state = wait.data;
3130 spin_lock_irqsave(&info->port_array[0]->lock, flags);
3131 remove_cond_wait(&info->gpio_wait_q, &wait);
3132 }
3133
3134 /* disable all GPIO interrupts if no waiting processes */
3135 if (info->gpio_wait_q == NULL)
3136 wr_reg32(info, IOER, 0);
3137 spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
3138
3139 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3140 rc = -EFAULT;
3141 return rc;
3142}
3143
3144static int modem_input_wait(struct slgt_info *info,int arg)
3145{
3146 unsigned long flags;
3147 int rc;
3148 struct mgsl_icount cprev, cnow;
3149 DECLARE_WAITQUEUE(wait, current);
3150
3151 /* save current irq counts */
3152 spin_lock_irqsave(&info->lock,flags);
3153 cprev = info->icount;
3154 add_wait_queue(&info->status_event_wait_q, &wait);
3155 set_current_state(TASK_INTERRUPTIBLE);
3156 spin_unlock_irqrestore(&info->lock,flags);
3157
3158 for(;;) {
3159 schedule();
3160 if (signal_pending(current)) {
3161 rc = -ERESTARTSYS;
3162 break;
3163 }
3164
3165 /* get new irq counts */
3166 spin_lock_irqsave(&info->lock,flags);
3167 cnow = info->icount;
3168 set_current_state(TASK_INTERRUPTIBLE);
3169 spin_unlock_irqrestore(&info->lock,flags);
3170
3171 /* if no change, wait aborted for some reason */
3172 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3173 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3174 rc = -EIO;
3175 break;
3176 }
3177
3178 /* check for change in caller specified modem input */
3179 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3180 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3181 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3182 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3183 rc = 0;
3184 break;
3185 }
3186
3187 cprev = cnow;
3188 }
3189 remove_wait_queue(&info->status_event_wait_q, &wait);
3190 set_current_state(TASK_RUNNING);
3191 return rc;
3192}
3193
3194/*
3195 * return state of serial control and status signals
3196 */
3197static int tiocmget(struct tty_struct *tty)
3198{
3199 struct slgt_info *info = tty->driver_data;
3200 unsigned int result;
3201 unsigned long flags;
3202
3203 spin_lock_irqsave(&info->lock,flags);
3204 get_signals(info);
3205 spin_unlock_irqrestore(&info->lock,flags);
3206
3207 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3208 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3209 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3210 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3211 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3212 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3213
3214 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3215 return result;
3216}
3217
3218/*
3219 * set modem control signals (DTR/RTS)
3220 *
3221 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3222 * TIOCMSET = set/clear signal values
3223 * value bit mask for command
3224 */
3225static int tiocmset(struct tty_struct *tty,
3226 unsigned int set, unsigned int clear)
3227{
3228 struct slgt_info *info = tty->driver_data;
3229 unsigned long flags;
3230
3231 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3232
3233 if (set & TIOCM_RTS)
3234 info->signals |= SerialSignal_RTS;
3235 if (set & TIOCM_DTR)
3236 info->signals |= SerialSignal_DTR;
3237 if (clear & TIOCM_RTS)
3238 info->signals &= ~SerialSignal_RTS;
3239 if (clear & TIOCM_DTR)
3240 info->signals &= ~SerialSignal_DTR;
3241
3242 spin_lock_irqsave(&info->lock,flags);
3243 set_signals(info);
3244 spin_unlock_irqrestore(&info->lock,flags);
3245 return 0;
3246}
3247
3248static int carrier_raised(struct tty_port *port)
3249{
3250 unsigned long flags;
3251 struct slgt_info *info = container_of(port, struct slgt_info, port);
3252
3253 spin_lock_irqsave(&info->lock,flags);
3254 get_signals(info);
3255 spin_unlock_irqrestore(&info->lock,flags);
3256 return (info->signals & SerialSignal_DCD) ? 1 : 0;
3257}
3258
3259static void dtr_rts(struct tty_port *port, int on)
3260{
3261 unsigned long flags;
3262 struct slgt_info *info = container_of(port, struct slgt_info, port);
3263
3264 spin_lock_irqsave(&info->lock,flags);
3265 if (on)
3266 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3267 else
3268 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3269 set_signals(info);
3270 spin_unlock_irqrestore(&info->lock,flags);
3271}
3272
3273
3274/*
3275 * block current process until the device is ready to open
3276 */
3277static int block_til_ready(struct tty_struct *tty, struct file *filp,
3278 struct slgt_info *info)
3279{
3280 DECLARE_WAITQUEUE(wait, current);
3281 int retval;
3282 bool do_clocal = false;
3283 bool extra_count = false;
3284 unsigned long flags;
3285 int cd;
3286 struct tty_port *port = &info->port;
3287
3288 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3289
3290 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3291 /* nonblock mode is set or port is not enabled */
3292 port->flags |= ASYNC_NORMAL_ACTIVE;
3293 return 0;
3294 }
3295
3296 if (tty->termios->c_cflag & CLOCAL)
3297 do_clocal = true;
3298
3299 /* Wait for carrier detect and the line to become
3300 * free (i.e., not in use by the callout). While we are in
3301 * this loop, port->count is dropped by one, so that
3302 * close() knows when to free things. We restore it upon
3303 * exit, either normal or abnormal.
3304 */
3305
3306 retval = 0;
3307 add_wait_queue(&port->open_wait, &wait);
3308
3309 spin_lock_irqsave(&info->lock, flags);
3310 if (!tty_hung_up_p(filp)) {
3311 extra_count = true;
3312 port->count--;
3313 }
3314 spin_unlock_irqrestore(&info->lock, flags);
3315 port->blocked_open++;
3316
3317 while (1) {
3318 if ((tty->termios->c_cflag & CBAUD))
3319 tty_port_raise_dtr_rts(port);
3320
3321 set_current_state(TASK_INTERRUPTIBLE);
3322
3323 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3324 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3325 -EAGAIN : -ERESTARTSYS;
3326 break;
3327 }
3328
3329 cd = tty_port_carrier_raised(port);
3330
3331 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd ))
3332 break;
3333
3334 if (signal_pending(current)) {
3335 retval = -ERESTARTSYS;
3336 break;
3337 }
3338
3339 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3340 tty_unlock();
3341 schedule();
3342 tty_lock();
3343 }
3344
3345 set_current_state(TASK_RUNNING);
3346 remove_wait_queue(&port->open_wait, &wait);
3347
3348 if (extra_count)
3349 port->count++;
3350 port->blocked_open--;
3351
3352 if (!retval)
3353 port->flags |= ASYNC_NORMAL_ACTIVE;
3354
3355 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3356 return retval;
3357}
3358
3359static int alloc_tmp_rbuf(struct slgt_info *info)
3360{
3361 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3362 if (info->tmp_rbuf == NULL)
3363 return -ENOMEM;
3364 return 0;
3365}
3366
3367static void free_tmp_rbuf(struct slgt_info *info)
3368{
3369 kfree(info->tmp_rbuf);
3370 info->tmp_rbuf = NULL;
3371}
3372
3373/*
3374 * allocate DMA descriptor lists.
3375 */
3376static int alloc_desc(struct slgt_info *info)
3377{
3378 unsigned int i;
3379 unsigned int pbufs;
3380
3381 /* allocate memory to hold descriptor lists */
3382 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3383 if (info->bufs == NULL)
3384 return -ENOMEM;
3385
3386 memset(info->bufs, 0, DESC_LIST_SIZE);
3387
3388 info->rbufs = (struct slgt_desc*)info->bufs;
3389 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3390
3391 pbufs = (unsigned int)info->bufs_dma_addr;
3392
3393 /*
3394 * Build circular lists of descriptors
3395 */
3396
3397 for (i=0; i < info->rbuf_count; i++) {
3398 /* physical address of this descriptor */
3399 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3400
3401 /* physical address of next descriptor */
3402 if (i == info->rbuf_count - 1)
3403 info->rbufs[i].next = cpu_to_le32(pbufs);
3404 else
3405 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3406 set_desc_count(info->rbufs[i], DMABUFSIZE);
3407 }
3408
3409 for (i=0; i < info->tbuf_count; i++) {
3410 /* physical address of this descriptor */
3411 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3412
3413 /* physical address of next descriptor */
3414 if (i == info->tbuf_count - 1)
3415 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3416 else
3417 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3418 }
3419
3420 return 0;
3421}
3422
3423static void free_desc(struct slgt_info *info)
3424{
3425 if (info->bufs != NULL) {
3426 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3427 info->bufs = NULL;
3428 info->rbufs = NULL;
3429 info->tbufs = NULL;
3430 }
3431}
3432
3433static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3434{
3435 int i;
3436 for (i=0; i < count; i++) {
3437 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3438 return -ENOMEM;
3439 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3440 }
3441 return 0;
3442}
3443
3444static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3445{
3446 int i;
3447 for (i=0; i < count; i++) {
3448 if (bufs[i].buf == NULL)
3449 continue;
3450 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3451 bufs[i].buf = NULL;
3452 }
3453}
3454
3455static int alloc_dma_bufs(struct slgt_info *info)
3456{
3457 info->rbuf_count = 32;
3458 info->tbuf_count = 32;
3459
3460 if (alloc_desc(info) < 0 ||
3461 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3462 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3463 alloc_tmp_rbuf(info) < 0) {
3464 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3465 return -ENOMEM;
3466 }
3467 reset_rbufs(info);
3468 return 0;
3469}
3470
3471static void free_dma_bufs(struct slgt_info *info)
3472{
3473 if (info->bufs) {
3474 free_bufs(info, info->rbufs, info->rbuf_count);
3475 free_bufs(info, info->tbufs, info->tbuf_count);
3476 free_desc(info);
3477 }
3478 free_tmp_rbuf(info);
3479}
3480
3481static int claim_resources(struct slgt_info *info)
3482{
3483 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3484 DBGERR(("%s reg addr conflict, addr=%08X\n",
3485 info->device_name, info->phys_reg_addr));
3486 info->init_error = DiagStatus_AddressConflict;
3487 goto errout;
3488 }
3489 else
3490 info->reg_addr_requested = true;
3491
3492 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
3493 if (!info->reg_addr) {
3494 DBGERR(("%s can't map device registers, addr=%08X\n",
3495 info->device_name, info->phys_reg_addr));
3496 info->init_error = DiagStatus_CantAssignPciResources;
3497 goto errout;
3498 }
3499 return 0;
3500
3501errout:
3502 release_resources(info);
3503 return -ENODEV;
3504}
3505
3506static void release_resources(struct slgt_info *info)
3507{
3508 if (info->irq_requested) {
3509 free_irq(info->irq_level, info);
3510 info->irq_requested = false;
3511 }
3512
3513 if (info->reg_addr_requested) {
3514 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3515 info->reg_addr_requested = false;
3516 }
3517
3518 if (info->reg_addr) {
3519 iounmap(info->reg_addr);
3520 info->reg_addr = NULL;
3521 }
3522}
3523
3524/* Add the specified device instance data structure to the
3525 * global linked list of devices and increment the device count.
3526 */
3527static void add_device(struct slgt_info *info)
3528{
3529 char *devstr;
3530
3531 info->next_device = NULL;
3532 info->line = slgt_device_count;
3533 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3534
3535 if (info->line < MAX_DEVICES) {
3536 if (maxframe[info->line])
3537 info->max_frame_size = maxframe[info->line];
3538 }
3539
3540 slgt_device_count++;
3541
3542 if (!slgt_device_list)
3543 slgt_device_list = info;
3544 else {
3545 struct slgt_info *current_dev = slgt_device_list;
3546 while(current_dev->next_device)
3547 current_dev = current_dev->next_device;
3548 current_dev->next_device = info;
3549 }
3550
3551 if (info->max_frame_size < 4096)
3552 info->max_frame_size = 4096;
3553 else if (info->max_frame_size > 65535)
3554 info->max_frame_size = 65535;
3555
3556 switch(info->pdev->device) {
3557 case SYNCLINK_GT_DEVICE_ID:
3558 devstr = "GT";
3559 break;
3560 case SYNCLINK_GT2_DEVICE_ID:
3561 devstr = "GT2";
3562 break;
3563 case SYNCLINK_GT4_DEVICE_ID:
3564 devstr = "GT4";
3565 break;
3566 case SYNCLINK_AC_DEVICE_ID:
3567 devstr = "AC";
3568 info->params.mode = MGSL_MODE_ASYNC;
3569 break;
3570 default:
3571 devstr = "(unknown model)";
3572 }
3573 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3574 devstr, info->device_name, info->phys_reg_addr,
3575 info->irq_level, info->max_frame_size);
3576
3577#if SYNCLINK_GENERIC_HDLC
3578 hdlcdev_init(info);
3579#endif
3580}
3581
3582static const struct tty_port_operations slgt_port_ops = {
3583 .carrier_raised = carrier_raised,
3584 .dtr_rts = dtr_rts,
3585};
3586
3587/*
3588 * allocate device instance structure, return NULL on failure
3589 */
3590static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3591{
3592 struct slgt_info *info;
3593
3594 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
3595
3596 if (!info) {
3597 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3598 driver_name, adapter_num, port_num));
3599 } else {
3600 tty_port_init(&info->port);
3601 info->port.ops = &slgt_port_ops;
3602 info->magic = MGSL_MAGIC;
3603 INIT_WORK(&info->task, bh_handler);
3604 info->max_frame_size = 4096;
3605 info->base_clock = 14745600;
3606 info->rbuf_fill_level = DMABUFSIZE;
3607 info->port.close_delay = 5*HZ/10;
3608 info->port.closing_wait = 30*HZ;
3609 init_waitqueue_head(&info->status_event_wait_q);
3610 init_waitqueue_head(&info->event_wait_q);
3611 spin_lock_init(&info->netlock);
3612 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3613 info->idle_mode = HDLC_TXIDLE_FLAGS;
3614 info->adapter_num = adapter_num;
3615 info->port_num = port_num;
3616
3617 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3618 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
3619
3620 /* Copy configuration info to device instance data */
3621 info->pdev = pdev;
3622 info->irq_level = pdev->irq;
3623 info->phys_reg_addr = pci_resource_start(pdev,0);
3624
3625 info->bus_type = MGSL_BUS_TYPE_PCI;
3626 info->irq_flags = IRQF_SHARED;
3627
3628 info->init_error = -1; /* assume error, set to 0 on successful init */
3629 }
3630
3631 return info;
3632}
3633
3634static void device_init(int adapter_num, struct pci_dev *pdev)
3635{
3636 struct slgt_info *port_array[SLGT_MAX_PORTS];
3637 int i;
3638 int port_count = 1;
3639
3640 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3641 port_count = 2;
3642 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3643 port_count = 4;
3644
3645 /* allocate device instances for all ports */
3646 for (i=0; i < port_count; ++i) {
3647 port_array[i] = alloc_dev(adapter_num, i, pdev);
3648 if (port_array[i] == NULL) {
3649 for (--i; i >= 0; --i)
3650 kfree(port_array[i]);
3651 return;
3652 }
3653 }
3654
3655 /* give copy of port_array to all ports and add to device list */
3656 for (i=0; i < port_count; ++i) {
3657 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3658 add_device(port_array[i]);
3659 port_array[i]->port_count = port_count;
3660 spin_lock_init(&port_array[i]->lock);
3661 }
3662
3663 /* Allocate and claim adapter resources */
3664 if (!claim_resources(port_array[0])) {
3665
3666 alloc_dma_bufs(port_array[0]);
3667
3668 /* copy resource information from first port to others */
3669 for (i = 1; i < port_count; ++i) {
3670 port_array[i]->irq_level = port_array[0]->irq_level;
3671 port_array[i]->reg_addr = port_array[0]->reg_addr;
3672 alloc_dma_bufs(port_array[i]);
3673 }
3674
3675 if (request_irq(port_array[0]->irq_level,
3676 slgt_interrupt,
3677 port_array[0]->irq_flags,
3678 port_array[0]->device_name,
3679 port_array[0]) < 0) {
3680 DBGERR(("%s request_irq failed IRQ=%d\n",
3681 port_array[0]->device_name,
3682 port_array[0]->irq_level));
3683 } else {
3684 port_array[0]->irq_requested = true;
3685 adapter_test(port_array[0]);
3686 for (i=1 ; i < port_count ; i++) {
3687 port_array[i]->init_error = port_array[0]->init_error;
3688 port_array[i]->gpio_present = port_array[0]->gpio_present;
3689 }
3690 }
3691 }
3692
3693 for (i=0; i < port_count; ++i)
3694 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
3695}
3696
3697static int __devinit init_one(struct pci_dev *dev,
3698 const struct pci_device_id *ent)
3699{
3700 if (pci_enable_device(dev)) {
3701 printk("error enabling pci device %p\n", dev);
3702 return -EIO;
3703 }
3704 pci_set_master(dev);
3705 device_init(slgt_device_count, dev);
3706 return 0;
3707}
3708
3709static void __devexit remove_one(struct pci_dev *dev)
3710{
3711}
3712
3713static const struct tty_operations ops = {
3714 .open = open,
3715 .close = close,
3716 .write = write,
3717 .put_char = put_char,
3718 .flush_chars = flush_chars,
3719 .write_room = write_room,
3720 .chars_in_buffer = chars_in_buffer,
3721 .flush_buffer = flush_buffer,
3722 .ioctl = ioctl,
3723 .compat_ioctl = slgt_compat_ioctl,
3724 .throttle = throttle,
3725 .unthrottle = unthrottle,
3726 .send_xchar = send_xchar,
3727 .break_ctl = set_break,
3728 .wait_until_sent = wait_until_sent,
3729 .set_termios = set_termios,
3730 .stop = tx_hold,
3731 .start = tx_release,
3732 .hangup = hangup,
3733 .tiocmget = tiocmget,
3734 .tiocmset = tiocmset,
3735 .get_icount = get_icount,
3736 .proc_fops = &synclink_gt_proc_fops,
3737};
3738
3739static void slgt_cleanup(void)
3740{
3741 int rc;
3742 struct slgt_info *info;
3743 struct slgt_info *tmp;
3744
3745 printk(KERN_INFO "unload %s\n", driver_name);
3746
3747 if (serial_driver) {
3748 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3749 tty_unregister_device(serial_driver, info->line);
3750 if ((rc = tty_unregister_driver(serial_driver)))
3751 DBGERR(("tty_unregister_driver error=%d\n", rc));
3752 put_tty_driver(serial_driver);
3753 }
3754
3755 /* reset devices */
3756 info = slgt_device_list;
3757 while(info) {
3758 reset_port(info);
3759 info = info->next_device;
3760 }
3761
3762 /* release devices */
3763 info = slgt_device_list;
3764 while(info) {
3765#if SYNCLINK_GENERIC_HDLC
3766 hdlcdev_exit(info);
3767#endif
3768 free_dma_bufs(info);
3769 free_tmp_rbuf(info);
3770 if (info->port_num == 0)
3771 release_resources(info);
3772 tmp = info;
3773 info = info->next_device;
3774 kfree(tmp);
3775 }
3776
3777 if (pci_registered)
3778 pci_unregister_driver(&pci_driver);
3779}
3780
3781/*
3782 * Driver initialization entry point.
3783 */
3784static int __init slgt_init(void)
3785{
3786 int rc;
3787
3788 printk(KERN_INFO "%s\n", driver_name);
3789
3790 serial_driver = alloc_tty_driver(MAX_DEVICES);
3791 if (!serial_driver) {
3792 printk("%s can't allocate tty driver\n", driver_name);
3793 return -ENOMEM;
3794 }
3795
3796 /* Initialize the tty_driver structure */
3797
3798 serial_driver->owner = THIS_MODULE;
3799 serial_driver->driver_name = tty_driver_name;
3800 serial_driver->name = tty_dev_prefix;
3801 serial_driver->major = ttymajor;
3802 serial_driver->minor_start = 64;
3803 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3804 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3805 serial_driver->init_termios = tty_std_termios;
3806 serial_driver->init_termios.c_cflag =
3807 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3808 serial_driver->init_termios.c_ispeed = 9600;
3809 serial_driver->init_termios.c_ospeed = 9600;
3810 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3811 tty_set_operations(serial_driver, &ops);
3812 if ((rc = tty_register_driver(serial_driver)) < 0) {
3813 DBGERR(("%s can't register serial driver\n", driver_name));
3814 put_tty_driver(serial_driver);
3815 serial_driver = NULL;
3816 goto error;
3817 }
3818
3819 printk(KERN_INFO "%s, tty major#%d\n",
3820 driver_name, serial_driver->major);
3821
3822 slgt_device_count = 0;
3823 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3824 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3825 goto error;
3826 }
3827 pci_registered = true;
3828
3829 if (!slgt_device_list)
3830 printk("%s no devices found\n",driver_name);
3831
3832 return 0;
3833
3834error:
3835 slgt_cleanup();
3836 return rc;
3837}
3838
3839static void __exit slgt_exit(void)
3840{
3841 slgt_cleanup();
3842}
3843
3844module_init(slgt_init);
3845module_exit(slgt_exit);
3846
3847/*
3848 * register access routines
3849 */
3850
3851#define CALC_REGADDR() \
3852 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3853 if (addr >= 0x80) \
3854 reg_addr += (info->port_num) * 32; \
3855 else if (addr >= 0x40) \
3856 reg_addr += (info->port_num) * 16;
3857
3858static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3859{
3860 CALC_REGADDR();
3861 return readb((void __iomem *)reg_addr);
3862}
3863
3864static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3865{
3866 CALC_REGADDR();
3867 writeb(value, (void __iomem *)reg_addr);
3868}
3869
3870static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3871{
3872 CALC_REGADDR();
3873 return readw((void __iomem *)reg_addr);
3874}
3875
3876static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3877{
3878 CALC_REGADDR();
3879 writew(value, (void __iomem *)reg_addr);
3880}
3881
3882static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3883{
3884 CALC_REGADDR();
3885 return readl((void __iomem *)reg_addr);
3886}
3887
3888static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3889{
3890 CALC_REGADDR();
3891 writel(value, (void __iomem *)reg_addr);
3892}
3893
3894static void rdma_reset(struct slgt_info *info)
3895{
3896 unsigned int i;
3897
3898 /* set reset bit */
3899 wr_reg32(info, RDCSR, BIT1);
3900
3901 /* wait for enable bit cleared */
3902 for(i=0 ; i < 1000 ; i++)
3903 if (!(rd_reg32(info, RDCSR) & BIT0))
3904 break;
3905}
3906
3907static void tdma_reset(struct slgt_info *info)
3908{
3909 unsigned int i;
3910
3911 /* set reset bit */
3912 wr_reg32(info, TDCSR, BIT1);
3913
3914 /* wait for enable bit cleared */
3915 for(i=0 ; i < 1000 ; i++)
3916 if (!(rd_reg32(info, TDCSR) & BIT0))
3917 break;
3918}
3919
3920/*
3921 * enable internal loopback
3922 * TxCLK and RxCLK are generated from BRG
3923 * and TxD is looped back to RxD internally.
3924 */
3925static void enable_loopback(struct slgt_info *info)
3926{
3927 /* SCR (serial control) BIT2=looopback enable */
3928 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3929
3930 if (info->params.mode != MGSL_MODE_ASYNC) {
3931 /* CCR (clock control)
3932 * 07..05 tx clock source (010 = BRG)
3933 * 04..02 rx clock source (010 = BRG)
3934 * 01 auxclk enable (0 = disable)
3935 * 00 BRG enable (1 = enable)
3936 *
3937 * 0100 1001
3938 */
3939 wr_reg8(info, CCR, 0x49);
3940
3941 /* set speed if available, otherwise use default */
3942 if (info->params.clock_speed)
3943 set_rate(info, info->params.clock_speed);
3944 else
3945 set_rate(info, 3686400);
3946 }
3947}
3948
3949/*
3950 * set baud rate generator to specified rate
3951 */
3952static void set_rate(struct slgt_info *info, u32 rate)
3953{
3954 unsigned int div;
3955 unsigned int osc = info->base_clock;
3956
3957 /* div = osc/rate - 1
3958 *
3959 * Round div up if osc/rate is not integer to
3960 * force to next slowest rate.
3961 */
3962
3963 if (rate) {
3964 div = osc/rate;
3965 if (!(osc % rate) && div)
3966 div--;
3967 wr_reg16(info, BDR, (unsigned short)div);
3968 }
3969}
3970
3971static void rx_stop(struct slgt_info *info)
3972{
3973 unsigned short val;
3974
3975 /* disable and reset receiver */
3976 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3977 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3978 wr_reg16(info, RCR, val); /* clear reset bit */
3979
3980 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3981
3982 /* clear pending rx interrupts */
3983 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3984
3985 rdma_reset(info);
3986
3987 info->rx_enabled = false;
3988 info->rx_restart = false;
3989}
3990
3991static void rx_start(struct slgt_info *info)
3992{
3993 unsigned short val;
3994
3995 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3996
3997 /* clear pending rx overrun IRQ */
3998 wr_reg16(info, SSR, IRQ_RXOVER);
3999
4000 /* reset and disable receiver */
4001 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
4002 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
4003 wr_reg16(info, RCR, val); /* clear reset bit */
4004
4005 rdma_reset(info);
4006 reset_rbufs(info);
4007
4008 if (info->rx_pio) {
4009 /* rx request when rx FIFO not empty */
4010 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
4011 slgt_irq_on(info, IRQ_RXDATA);
4012 if (info->params.mode == MGSL_MODE_ASYNC) {
4013 /* enable saving of rx status */
4014 wr_reg32(info, RDCSR, BIT6);
4015 }
4016 } else {
4017 /* rx request when rx FIFO half full */
4018 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
4019 /* set 1st descriptor address */
4020 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
4021
4022 if (info->params.mode != MGSL_MODE_ASYNC) {
4023 /* enable rx DMA and DMA interrupt */
4024 wr_reg32(info, RDCSR, (BIT2 + BIT0));
4025 } else {
4026 /* enable saving of rx status, rx DMA and DMA interrupt */
4027 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
4028 }
4029 }
4030
4031 slgt_irq_on(info, IRQ_RXOVER);
4032
4033 /* enable receiver */
4034 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
4035
4036 info->rx_restart = false;
4037 info->rx_enabled = true;
4038}
4039
4040static void tx_start(struct slgt_info *info)
4041{
4042 if (!info->tx_enabled) {
4043 wr_reg16(info, TCR,
4044 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
4045 info->tx_enabled = true;
4046 }
4047
4048 if (desc_count(info->tbufs[info->tbuf_start])) {
4049 info->drop_rts_on_tx_done = false;
4050
4051 if (info->params.mode != MGSL_MODE_ASYNC) {
4052 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
4053 get_signals(info);
4054 if (!(info->signals & SerialSignal_RTS)) {
4055 info->signals |= SerialSignal_RTS;
4056 set_signals(info);
4057 info->drop_rts_on_tx_done = true;
4058 }
4059 }
4060
4061 slgt_irq_off(info, IRQ_TXDATA);
4062 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
4063 /* clear tx idle and underrun status bits */
4064 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
4065 } else {
4066 slgt_irq_off(info, IRQ_TXDATA);
4067 slgt_irq_on(info, IRQ_TXIDLE);
4068 /* clear tx idle status bit */
4069 wr_reg16(info, SSR, IRQ_TXIDLE);
4070 }
4071 /* set 1st descriptor address and start DMA */
4072 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
4073 wr_reg32(info, TDCSR, BIT2 + BIT0);
4074 info->tx_active = true;
4075 }
4076}
4077
4078static void tx_stop(struct slgt_info *info)
4079{
4080 unsigned short val;
4081
4082 del_timer(&info->tx_timer);
4083
4084 tdma_reset(info);
4085
4086 /* reset and disable transmitter */
4087 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
4088 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
4089
4090 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
4091
4092 /* clear tx idle and underrun status bit */
4093 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
4094
4095 reset_tbufs(info);
4096
4097 info->tx_enabled = false;
4098 info->tx_active = false;
4099}
4100
4101static void reset_port(struct slgt_info *info)
4102{
4103 if (!info->reg_addr)
4104 return;
4105
4106 tx_stop(info);
4107 rx_stop(info);
4108
4109 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4110 set_signals(info);
4111
4112 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4113}
4114
4115static void reset_adapter(struct slgt_info *info)
4116{
4117 int i;
4118 for (i=0; i < info->port_count; ++i) {
4119 if (info->port_array[i])
4120 reset_port(info->port_array[i]);
4121 }
4122}
4123
4124static void async_mode(struct slgt_info *info)
4125{
4126 unsigned short val;
4127
4128 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4129 tx_stop(info);
4130 rx_stop(info);
4131
4132 /* TCR (tx control)
4133 *
4134 * 15..13 mode, 010=async
4135 * 12..10 encoding, 000=NRZ
4136 * 09 parity enable
4137 * 08 1=odd parity, 0=even parity
4138 * 07 1=RTS driver control
4139 * 06 1=break enable
4140 * 05..04 character length
4141 * 00=5 bits
4142 * 01=6 bits
4143 * 10=7 bits
4144 * 11=8 bits
4145 * 03 0=1 stop bit, 1=2 stop bits
4146 * 02 reset
4147 * 01 enable
4148 * 00 auto-CTS enable
4149 */
4150 val = 0x4000;
4151
4152 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4153 val |= BIT7;
4154
4155 if (info->params.parity != ASYNC_PARITY_NONE) {
4156 val |= BIT9;
4157 if (info->params.parity == ASYNC_PARITY_ODD)
4158 val |= BIT8;
4159 }
4160
4161 switch (info->params.data_bits)
4162 {
4163 case 6: val |= BIT4; break;
4164 case 7: val |= BIT5; break;
4165 case 8: val |= BIT5 + BIT4; break;
4166 }
4167
4168 if (info->params.stop_bits != 1)
4169 val |= BIT3;
4170
4171 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4172 val |= BIT0;
4173
4174 wr_reg16(info, TCR, val);
4175
4176 /* RCR (rx control)
4177 *
4178 * 15..13 mode, 010=async
4179 * 12..10 encoding, 000=NRZ
4180 * 09 parity enable
4181 * 08 1=odd parity, 0=even parity
4182 * 07..06 reserved, must be 0
4183 * 05..04 character length
4184 * 00=5 bits
4185 * 01=6 bits
4186 * 10=7 bits
4187 * 11=8 bits
4188 * 03 reserved, must be zero
4189 * 02 reset
4190 * 01 enable
4191 * 00 auto-DCD enable
4192 */
4193 val = 0x4000;
4194
4195 if (info->params.parity != ASYNC_PARITY_NONE) {
4196 val |= BIT9;
4197 if (info->params.parity == ASYNC_PARITY_ODD)
4198 val |= BIT8;
4199 }
4200
4201 switch (info->params.data_bits)
4202 {
4203 case 6: val |= BIT4; break;
4204 case 7: val |= BIT5; break;
4205 case 8: val |= BIT5 + BIT4; break;
4206 }
4207
4208 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4209 val |= BIT0;
4210
4211 wr_reg16(info, RCR, val);
4212
4213 /* CCR (clock control)
4214 *
4215 * 07..05 011 = tx clock source is BRG/16
4216 * 04..02 010 = rx clock source is BRG
4217 * 01 0 = auxclk disabled
4218 * 00 1 = BRG enabled
4219 *
4220 * 0110 1001
4221 */
4222 wr_reg8(info, CCR, 0x69);
4223
4224 msc_set_vcr(info);
4225
4226 /* SCR (serial control)
4227 *
4228 * 15 1=tx req on FIFO half empty
4229 * 14 1=rx req on FIFO half full
4230 * 13 tx data IRQ enable
4231 * 12 tx idle IRQ enable
4232 * 11 rx break on IRQ enable
4233 * 10 rx data IRQ enable
4234 * 09 rx break off IRQ enable
4235 * 08 overrun IRQ enable
4236 * 07 DSR IRQ enable
4237 * 06 CTS IRQ enable
4238 * 05 DCD IRQ enable
4239 * 04 RI IRQ enable
4240 * 03 0=16x sampling, 1=8x sampling
4241 * 02 1=txd->rxd internal loopback enable
4242 * 01 reserved, must be zero
4243 * 00 1=master IRQ enable
4244 */
4245 val = BIT15 + BIT14 + BIT0;
4246 /* JCR[8] : 1 = x8 async mode feature available */
4247 if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
4248 ((info->base_clock < (info->params.data_rate * 16)) ||
4249 (info->base_clock % (info->params.data_rate * 16)))) {
4250 /* use 8x sampling */
4251 val |= BIT3;
4252 set_rate(info, info->params.data_rate * 8);
4253 } else {
4254 /* use 16x sampling */
4255 set_rate(info, info->params.data_rate * 16);
4256 }
4257 wr_reg16(info, SCR, val);
4258
4259 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4260
4261 if (info->params.loopback)
4262 enable_loopback(info);
4263}
4264
4265static void sync_mode(struct slgt_info *info)
4266{
4267 unsigned short val;
4268
4269 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4270 tx_stop(info);
4271 rx_stop(info);
4272
4273 /* TCR (tx control)
4274 *
4275 * 15..13 mode
4276 * 000=HDLC/SDLC
4277 * 001=raw bit synchronous
4278 * 010=asynchronous/isochronous
4279 * 011=monosync byte synchronous
4280 * 100=bisync byte synchronous
4281 * 101=xsync byte synchronous
4282 * 12..10 encoding
4283 * 09 CRC enable
4284 * 08 CRC32
4285 * 07 1=RTS driver control
4286 * 06 preamble enable
4287 * 05..04 preamble length
4288 * 03 share open/close flag
4289 * 02 reset
4290 * 01 enable
4291 * 00 auto-CTS enable
4292 */
4293 val = BIT2;
4294
4295 switch(info->params.mode) {
4296 case MGSL_MODE_XSYNC:
4297 val |= BIT15 + BIT13;
4298 break;
4299 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4300 case MGSL_MODE_BISYNC: val |= BIT15; break;
4301 case MGSL_MODE_RAW: val |= BIT13; break;
4302 }
4303 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4304 val |= BIT7;
4305
4306 switch(info->params.encoding)
4307 {
4308 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4309 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4310 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4311 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4312 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4313 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4314 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4315 }
4316
4317 switch (info->params.crc_type & HDLC_CRC_MASK)
4318 {
4319 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4320 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4321 }
4322
4323 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4324 val |= BIT6;
4325
4326 switch (info->params.preamble_length)
4327 {
4328 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4329 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4330 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4331 }
4332
4333 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4334 val |= BIT0;
4335
4336 wr_reg16(info, TCR, val);
4337
4338 /* TPR (transmit preamble) */
4339
4340 switch (info->params.preamble)
4341 {
4342 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4343 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4344 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4345 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4346 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4347 default: val = 0x7e; break;
4348 }
4349 wr_reg8(info, TPR, (unsigned char)val);
4350
4351 /* RCR (rx control)
4352 *
4353 * 15..13 mode
4354 * 000=HDLC/SDLC
4355 * 001=raw bit synchronous
4356 * 010=asynchronous/isochronous
4357 * 011=monosync byte synchronous
4358 * 100=bisync byte synchronous
4359 * 101=xsync byte synchronous
4360 * 12..10 encoding
4361 * 09 CRC enable
4362 * 08 CRC32
4363 * 07..03 reserved, must be 0
4364 * 02 reset
4365 * 01 enable
4366 * 00 auto-DCD enable
4367 */
4368 val = 0;
4369
4370 switch(info->params.mode) {
4371 case MGSL_MODE_XSYNC:
4372 val |= BIT15 + BIT13;
4373 break;
4374 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4375 case MGSL_MODE_BISYNC: val |= BIT15; break;
4376 case MGSL_MODE_RAW: val |= BIT13; break;
4377 }
4378
4379 switch(info->params.encoding)
4380 {
4381 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4382 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4383 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4384 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4385 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4386 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4387 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4388 }
4389
4390 switch (info->params.crc_type & HDLC_CRC_MASK)
4391 {
4392 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4393 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4394 }
4395
4396 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4397 val |= BIT0;
4398
4399 wr_reg16(info, RCR, val);
4400
4401 /* CCR (clock control)
4402 *
4403 * 07..05 tx clock source
4404 * 04..02 rx clock source
4405 * 01 auxclk enable
4406 * 00 BRG enable
4407 */
4408 val = 0;
4409
4410 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4411 {
4412 // when RxC source is DPLL, BRG generates 16X DPLL
4413 // reference clock, so take TxC from BRG/16 to get
4414 // transmit clock at actual data rate
4415 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4416 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4417 else
4418 val |= BIT6; /* 010, txclk = BRG */
4419 }
4420 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4421 val |= BIT7; /* 100, txclk = DPLL Input */
4422 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4423 val |= BIT5; /* 001, txclk = RXC Input */
4424
4425 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4426 val |= BIT3; /* 010, rxclk = BRG */
4427 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4428 val |= BIT4; /* 100, rxclk = DPLL */
4429 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4430 val |= BIT2; /* 001, rxclk = TXC Input */
4431
4432 if (info->params.clock_speed)
4433 val |= BIT1 + BIT0;
4434
4435 wr_reg8(info, CCR, (unsigned char)val);
4436
4437 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4438 {
4439 // program DPLL mode
4440 switch(info->params.encoding)
4441 {
4442 case HDLC_ENCODING_BIPHASE_MARK:
4443 case HDLC_ENCODING_BIPHASE_SPACE:
4444 val = BIT7; break;
4445 case HDLC_ENCODING_BIPHASE_LEVEL:
4446 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4447 val = BIT7 + BIT6; break;
4448 default: val = BIT6; // NRZ encodings
4449 }
4450 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4451
4452 // DPLL requires a 16X reference clock from BRG
4453 set_rate(info, info->params.clock_speed * 16);
4454 }
4455 else
4456 set_rate(info, info->params.clock_speed);
4457
4458 tx_set_idle(info);
4459
4460 msc_set_vcr(info);
4461
4462 /* SCR (serial control)
4463 *
4464 * 15 1=tx req on FIFO half empty
4465 * 14 1=rx req on FIFO half full
4466 * 13 tx data IRQ enable
4467 * 12 tx idle IRQ enable
4468 * 11 underrun IRQ enable
4469 * 10 rx data IRQ enable
4470 * 09 rx idle IRQ enable
4471 * 08 overrun IRQ enable
4472 * 07 DSR IRQ enable
4473 * 06 CTS IRQ enable
4474 * 05 DCD IRQ enable
4475 * 04 RI IRQ enable
4476 * 03 reserved, must be zero
4477 * 02 1=txd->rxd internal loopback enable
4478 * 01 reserved, must be zero
4479 * 00 1=master IRQ enable
4480 */
4481 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4482
4483 if (info->params.loopback)
4484 enable_loopback(info);
4485}
4486
4487/*
4488 * set transmit idle mode
4489 */
4490static void tx_set_idle(struct slgt_info *info)
4491{
4492 unsigned char val;
4493 unsigned short tcr;
4494
4495 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4496 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4497 */
4498 tcr = rd_reg16(info, TCR);
4499 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4500 /* disable preamble, set idle size to 16 bits */
4501 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4502 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4503 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4504 } else if (!(tcr & BIT6)) {
4505 /* preamble is disabled, set idle size to 8 bits */
4506 tcr &= ~(BIT5 + BIT4);
4507 }
4508 wr_reg16(info, TCR, tcr);
4509
4510 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4511 /* LSB of custom tx idle specified in tx idle register */
4512 val = (unsigned char)(info->idle_mode & 0xff);
4513 } else {
4514 /* standard 8 bit idle patterns */
4515 switch(info->idle_mode)
4516 {
4517 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4518 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4519 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4520 case HDLC_TXIDLE_ZEROS:
4521 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4522 default: val = 0xff;
4523 }
4524 }
4525
4526 wr_reg8(info, TIR, val);
4527}
4528
4529/*
4530 * get state of V24 status (input) signals
4531 */
4532static void get_signals(struct slgt_info *info)
4533{
4534 unsigned short status = rd_reg16(info, SSR);
4535
4536 /* clear all serial signals except DTR and RTS */
4537 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4538
4539 if (status & BIT3)
4540 info->signals |= SerialSignal_DSR;
4541 if (status & BIT2)
4542 info->signals |= SerialSignal_CTS;
4543 if (status & BIT1)
4544 info->signals |= SerialSignal_DCD;
4545 if (status & BIT0)
4546 info->signals |= SerialSignal_RI;
4547}
4548
4549/*
4550 * set V.24 Control Register based on current configuration
4551 */
4552static void msc_set_vcr(struct slgt_info *info)
4553{
4554 unsigned char val = 0;
4555
4556 /* VCR (V.24 control)
4557 *
4558 * 07..04 serial IF select
4559 * 03 DTR
4560 * 02 RTS
4561 * 01 LL
4562 * 00 RL
4563 */
4564
4565 switch(info->if_mode & MGSL_INTERFACE_MASK)
4566 {
4567 case MGSL_INTERFACE_RS232:
4568 val |= BIT5; /* 0010 */
4569 break;
4570 case MGSL_INTERFACE_V35:
4571 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4572 break;
4573 case MGSL_INTERFACE_RS422:
4574 val |= BIT6; /* 0100 */
4575 break;
4576 }
4577
4578 if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
4579 val |= BIT4;
4580 if (info->signals & SerialSignal_DTR)
4581 val |= BIT3;
4582 if (info->signals & SerialSignal_RTS)
4583 val |= BIT2;
4584 if (info->if_mode & MGSL_INTERFACE_LL)
4585 val |= BIT1;
4586 if (info->if_mode & MGSL_INTERFACE_RL)
4587 val |= BIT0;
4588 wr_reg8(info, VCR, val);
4589}
4590
4591/*
4592 * set state of V24 control (output) signals
4593 */
4594static void set_signals(struct slgt_info *info)
4595{
4596 unsigned char val = rd_reg8(info, VCR);
4597 if (info->signals & SerialSignal_DTR)
4598 val |= BIT3;
4599 else
4600 val &= ~BIT3;
4601 if (info->signals & SerialSignal_RTS)
4602 val |= BIT2;
4603 else
4604 val &= ~BIT2;
4605 wr_reg8(info, VCR, val);
4606}
4607
4608/*
4609 * free range of receive DMA buffers (i to last)
4610 */
4611static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4612{
4613 int done = 0;
4614
4615 while(!done) {
4616 /* reset current buffer for reuse */
4617 info->rbufs[i].status = 0;
4618 set_desc_count(info->rbufs[i], info->rbuf_fill_level);
4619 if (i == last)
4620 done = 1;
4621 if (++i == info->rbuf_count)
4622 i = 0;
4623 }
4624 info->rbuf_current = i;
4625}
4626
4627/*
4628 * mark all receive DMA buffers as free
4629 */
4630static void reset_rbufs(struct slgt_info *info)
4631{
4632 free_rbufs(info, 0, info->rbuf_count - 1);
4633 info->rbuf_fill_index = 0;
4634 info->rbuf_fill_count = 0;
4635}
4636
4637/*
4638 * pass receive HDLC frame to upper layer
4639 *
4640 * return true if frame available, otherwise false
4641 */
4642static bool rx_get_frame(struct slgt_info *info)
4643{
4644 unsigned int start, end;
4645 unsigned short status;
4646 unsigned int framesize = 0;
4647 unsigned long flags;
4648 struct tty_struct *tty = info->port.tty;
4649 unsigned char addr_field = 0xff;
4650 unsigned int crc_size = 0;
4651
4652 switch (info->params.crc_type & HDLC_CRC_MASK) {
4653 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4654 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4655 }
4656
4657check_again:
4658
4659 framesize = 0;
4660 addr_field = 0xff;
4661 start = end = info->rbuf_current;
4662
4663 for (;;) {
4664 if (!desc_complete(info->rbufs[end]))
4665 goto cleanup;
4666
4667 if (framesize == 0 && info->params.addr_filter != 0xff)
4668 addr_field = info->rbufs[end].buf[0];
4669
4670 framesize += desc_count(info->rbufs[end]);
4671
4672 if (desc_eof(info->rbufs[end]))
4673 break;
4674
4675 if (++end == info->rbuf_count)
4676 end = 0;
4677
4678 if (end == info->rbuf_current) {
4679 if (info->rx_enabled){
4680 spin_lock_irqsave(&info->lock,flags);
4681 rx_start(info);
4682 spin_unlock_irqrestore(&info->lock,flags);
4683 }
4684 goto cleanup;
4685 }
4686 }
4687
4688 /* status
4689 *
4690 * 15 buffer complete
4691 * 14..06 reserved
4692 * 05..04 residue
4693 * 02 eof (end of frame)
4694 * 01 CRC error
4695 * 00 abort
4696 */
4697 status = desc_status(info->rbufs[end]);
4698
4699 /* ignore CRC bit if not using CRC (bit is undefined) */
4700 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4701 status &= ~BIT1;
4702
4703 if (framesize == 0 ||
4704 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4705 free_rbufs(info, start, end);
4706 goto check_again;
4707 }
4708
4709 if (framesize < (2 + crc_size) || status & BIT0) {
4710 info->icount.rxshort++;
4711 framesize = 0;
4712 } else if (status & BIT1) {
4713 info->icount.rxcrc++;
4714 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4715 framesize = 0;
4716 }
4717
4718#if SYNCLINK_GENERIC_HDLC
4719 if (framesize == 0) {
4720 info->netdev->stats.rx_errors++;
4721 info->netdev->stats.rx_frame_errors++;
4722 }
4723#endif
4724
4725 DBGBH(("%s rx frame status=%04X size=%d\n",
4726 info->device_name, status, framesize));
4727 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
4728
4729 if (framesize) {
4730 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4731 framesize -= crc_size;
4732 crc_size = 0;
4733 }
4734
4735 if (framesize > info->max_frame_size + crc_size)
4736 info->icount.rxlong++;
4737 else {
4738 /* copy dma buffer(s) to contiguous temp buffer */
4739 int copy_count = framesize;
4740 int i = start;
4741 unsigned char *p = info->tmp_rbuf;
4742 info->tmp_rbuf_count = framesize;
4743
4744 info->icount.rxok++;
4745
4746 while(copy_count) {
4747 int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
4748 memcpy(p, info->rbufs[i].buf, partial_count);
4749 p += partial_count;
4750 copy_count -= partial_count;
4751 if (++i == info->rbuf_count)
4752 i = 0;
4753 }
4754
4755 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4756 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4757 framesize++;
4758 }
4759
4760#if SYNCLINK_GENERIC_HDLC
4761 if (info->netcount)
4762 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4763 else
4764#endif
4765 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4766 }
4767 }
4768 free_rbufs(info, start, end);
4769 return true;
4770
4771cleanup:
4772 return false;
4773}
4774
4775/*
4776 * pass receive buffer (RAW synchronous mode) to tty layer
4777 * return true if buffer available, otherwise false
4778 */
4779static bool rx_get_buf(struct slgt_info *info)
4780{
4781 unsigned int i = info->rbuf_current;
4782 unsigned int count;
4783
4784 if (!desc_complete(info->rbufs[i]))
4785 return false;
4786 count = desc_count(info->rbufs[i]);
4787 switch(info->params.mode) {
4788 case MGSL_MODE_MONOSYNC:
4789 case MGSL_MODE_BISYNC:
4790 case MGSL_MODE_XSYNC:
4791 /* ignore residue in byte synchronous modes */
4792 if (desc_residue(info->rbufs[i]))
4793 count--;
4794 break;
4795 }
4796 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4797 DBGINFO(("rx_get_buf size=%d\n", count));
4798 if (count)
4799 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
4800 info->flag_buf, count);
4801 free_rbufs(info, i, i);
4802 return true;
4803}
4804
4805static void reset_tbufs(struct slgt_info *info)
4806{
4807 unsigned int i;
4808 info->tbuf_current = 0;
4809 for (i=0 ; i < info->tbuf_count ; i++) {
4810 info->tbufs[i].status = 0;
4811 info->tbufs[i].count = 0;
4812 }
4813}
4814
4815/*
4816 * return number of free transmit DMA buffers
4817 */
4818static unsigned int free_tbuf_count(struct slgt_info *info)
4819{
4820 unsigned int count = 0;
4821 unsigned int i = info->tbuf_current;
4822
4823 do
4824 {
4825 if (desc_count(info->tbufs[i]))
4826 break; /* buffer in use */
4827 ++count;
4828 if (++i == info->tbuf_count)
4829 i=0;
4830 } while (i != info->tbuf_current);
4831
4832 /* if tx DMA active, last zero count buffer is in use */
4833 if (count && (rd_reg32(info, TDCSR) & BIT0))
4834 --count;
4835
4836 return count;
4837}
4838
4839/*
4840 * return number of bytes in unsent transmit DMA buffers
4841 * and the serial controller tx FIFO
4842 */
4843static unsigned int tbuf_bytes(struct slgt_info *info)
4844{
4845 unsigned int total_count = 0;
4846 unsigned int i = info->tbuf_current;
4847 unsigned int reg_value;
4848 unsigned int count;
4849 unsigned int active_buf_count = 0;
4850
4851 /*
4852 * Add descriptor counts for all tx DMA buffers.
4853 * If count is zero (cleared by DMA controller after read),
4854 * the buffer is complete or is actively being read from.
4855 *
4856 * Record buf_count of last buffer with zero count starting
4857 * from current ring position. buf_count is mirror
4858 * copy of count and is not cleared by serial controller.
4859 * If DMA controller is active, that buffer is actively
4860 * being read so add to total.
4861 */
4862 do {
4863 count = desc_count(info->tbufs[i]);
4864 if (count)
4865 total_count += count;
4866 else if (!total_count)
4867 active_buf_count = info->tbufs[i].buf_count;
4868 if (++i == info->tbuf_count)
4869 i = 0;
4870 } while (i != info->tbuf_current);
4871
4872 /* read tx DMA status register */
4873 reg_value = rd_reg32(info, TDCSR);
4874
4875 /* if tx DMA active, last zero count buffer is in use */
4876 if (reg_value & BIT0)
4877 total_count += active_buf_count;
4878
4879 /* add tx FIFO count = reg_value[15..8] */
4880 total_count += (reg_value >> 8) & 0xff;
4881
4882 /* if transmitter active add one byte for shift register */
4883 if (info->tx_active)
4884 total_count++;
4885
4886 return total_count;
4887}
4888
4889/*
4890 * load data into transmit DMA buffer ring and start transmitter if needed
4891 * return true if data accepted, otherwise false (buffers full)
4892 */
4893static bool tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4894{
4895 unsigned short count;
4896 unsigned int i;
4897 struct slgt_desc *d;
4898
4899 /* check required buffer space */
4900 if (DIV_ROUND_UP(size, DMABUFSIZE) > free_tbuf_count(info))
4901 return false;
4902
4903 DBGDATA(info, buf, size, "tx");
4904
4905 /*
4906 * copy data to one or more DMA buffers in circular ring
4907 * tbuf_start = first buffer for this data
4908 * tbuf_current = next free buffer
4909 *
4910 * Copy all data before making data visible to DMA controller by
4911 * setting descriptor count of the first buffer.
4912 * This prevents an active DMA controller from reading the first DMA
4913 * buffers of a frame and stopping before the final buffers are filled.
4914 */
4915
4916 info->tbuf_start = i = info->tbuf_current;
4917
4918 while (size) {
4919 d = &info->tbufs[i];
4920
4921 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4922 memcpy(d->buf, buf, count);
4923
4924 size -= count;
4925 buf += count;
4926
4927 /*
4928 * set EOF bit for last buffer of HDLC frame or
4929 * for every buffer in raw mode
4930 */
4931 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4932 info->params.mode == MGSL_MODE_RAW)
4933 set_desc_eof(*d, 1);
4934 else
4935 set_desc_eof(*d, 0);
4936
4937 /* set descriptor count for all but first buffer */
4938 if (i != info->tbuf_start)
4939 set_desc_count(*d, count);
4940 d->buf_count = count;
4941
4942 if (++i == info->tbuf_count)
4943 i = 0;
4944 }
4945
4946 info->tbuf_current = i;
4947
4948 /* set first buffer count to make new data visible to DMA controller */
4949 d = &info->tbufs[info->tbuf_start];
4950 set_desc_count(*d, d->buf_count);
4951
4952 /* start transmitter if needed and update transmit timeout */
4953 if (!info->tx_active)
4954 tx_start(info);
4955 update_tx_timer(info);
4956
4957 return true;
4958}
4959
4960static int register_test(struct slgt_info *info)
4961{
4962 static unsigned short patterns[] =
4963 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4964 static unsigned int count = ARRAY_SIZE(patterns);
4965 unsigned int i;
4966 int rc = 0;
4967
4968 for (i=0 ; i < count ; i++) {
4969 wr_reg16(info, TIR, patterns[i]);
4970 wr_reg16(info, BDR, patterns[(i+1)%count]);
4971 if ((rd_reg16(info, TIR) != patterns[i]) ||
4972 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4973 rc = -ENODEV;
4974 break;
4975 }
4976 }
4977 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4978 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4979 return rc;
4980}
4981
4982static int irq_test(struct slgt_info *info)
4983{
4984 unsigned long timeout;
4985 unsigned long flags;
4986 struct tty_struct *oldtty = info->port.tty;
4987 u32 speed = info->params.data_rate;
4988
4989 info->params.data_rate = 921600;
4990 info->port.tty = NULL;
4991
4992 spin_lock_irqsave(&info->lock, flags);
4993 async_mode(info);
4994 slgt_irq_on(info, IRQ_TXIDLE);
4995
4996 /* enable transmitter */
4997 wr_reg16(info, TCR,
4998 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4999
5000 /* write one byte and wait for tx idle */
5001 wr_reg16(info, TDR, 0);
5002
5003 /* assume failure */
5004 info->init_error = DiagStatus_IrqFailure;
5005 info->irq_occurred = false;
5006
5007 spin_unlock_irqrestore(&info->lock, flags);
5008
5009 timeout=100;
5010 while(timeout-- && !info->irq_occurred)
5011 msleep_interruptible(10);
5012
5013 spin_lock_irqsave(&info->lock,flags);
5014 reset_port(info);
5015 spin_unlock_irqrestore(&info->lock,flags);
5016
5017 info->params.data_rate = speed;
5018 info->port.tty = oldtty;
5019
5020 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
5021 return info->irq_occurred ? 0 : -ENODEV;
5022}
5023
5024static int loopback_test_rx(struct slgt_info *info)
5025{
5026 unsigned char *src, *dest;
5027 int count;
5028
5029 if (desc_complete(info->rbufs[0])) {
5030 count = desc_count(info->rbufs[0]);
5031 src = info->rbufs[0].buf;
5032 dest = info->tmp_rbuf;
5033
5034 for( ; count ; count-=2, src+=2) {
5035 /* src=data byte (src+1)=status byte */
5036 if (!(*(src+1) & (BIT9 + BIT8))) {
5037 *dest = *src;
5038 dest++;
5039 info->tmp_rbuf_count++;
5040 }
5041 }
5042 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
5043 return 1;
5044 }
5045 return 0;
5046}
5047
5048static int loopback_test(struct slgt_info *info)
5049{
5050#define TESTFRAMESIZE 20
5051
5052 unsigned long timeout;
5053 u16 count = TESTFRAMESIZE;
5054 unsigned char buf[TESTFRAMESIZE];
5055 int rc = -ENODEV;
5056 unsigned long flags;
5057
5058 struct tty_struct *oldtty = info->port.tty;
5059 MGSL_PARAMS params;
5060
5061 memcpy(&params, &info->params, sizeof(params));
5062
5063 info->params.mode = MGSL_MODE_ASYNC;
5064 info->params.data_rate = 921600;
5065 info->params.loopback = 1;
5066 info->port.tty = NULL;
5067
5068 /* build and send transmit frame */
5069 for (count = 0; count < TESTFRAMESIZE; ++count)
5070 buf[count] = (unsigned char)count;
5071
5072 info->tmp_rbuf_count = 0;
5073 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
5074
5075 /* program hardware for HDLC and enabled receiver */
5076 spin_lock_irqsave(&info->lock,flags);
5077 async_mode(info);
5078 rx_start(info);
5079 tx_load(info, buf, count);
5080 spin_unlock_irqrestore(&info->lock, flags);
5081
5082 /* wait for receive complete */
5083 for (timeout = 100; timeout; --timeout) {
5084 msleep_interruptible(10);
5085 if (loopback_test_rx(info)) {
5086 rc = 0;
5087 break;
5088 }
5089 }
5090
5091 /* verify received frame length and contents */
5092 if (!rc && (info->tmp_rbuf_count != count ||
5093 memcmp(buf, info->tmp_rbuf, count))) {
5094 rc = -ENODEV;
5095 }
5096
5097 spin_lock_irqsave(&info->lock,flags);
5098 reset_adapter(info);
5099 spin_unlock_irqrestore(&info->lock,flags);
5100
5101 memcpy(&info->params, &params, sizeof(info->params));
5102 info->port.tty = oldtty;
5103
5104 info->init_error = rc ? DiagStatus_DmaFailure : 0;
5105 return rc;
5106}
5107
5108static int adapter_test(struct slgt_info *info)
5109{
5110 DBGINFO(("testing %s\n", info->device_name));
5111 if (register_test(info) < 0) {
5112 printk("register test failure %s addr=%08X\n",
5113 info->device_name, info->phys_reg_addr);
5114 } else if (irq_test(info) < 0) {
5115 printk("IRQ test failure %s IRQ=%d\n",
5116 info->device_name, info->irq_level);
5117 } else if (loopback_test(info) < 0) {
5118 printk("loopback test failure %s\n", info->device_name);
5119 }
5120 return info->init_error;
5121}
5122
5123/*
5124 * transmit timeout handler
5125 */
5126static void tx_timeout(unsigned long context)
5127{
5128 struct slgt_info *info = (struct slgt_info*)context;
5129 unsigned long flags;
5130
5131 DBGINFO(("%s tx_timeout\n", info->device_name));
5132 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5133 info->icount.txtimeout++;
5134 }
5135 spin_lock_irqsave(&info->lock,flags);
5136 tx_stop(info);
5137 spin_unlock_irqrestore(&info->lock,flags);
5138
5139#if SYNCLINK_GENERIC_HDLC
5140 if (info->netcount)
5141 hdlcdev_tx_done(info);
5142 else
5143#endif
5144 bh_transmit(info);
5145}
5146
5147/*
5148 * receive buffer polling timer
5149 */
5150static void rx_timeout(unsigned long context)
5151{
5152 struct slgt_info *info = (struct slgt_info*)context;
5153 unsigned long flags;
5154
5155 DBGINFO(("%s rx_timeout\n", info->device_name));
5156 spin_lock_irqsave(&info->lock, flags);
5157 info->pending_bh |= BH_RECEIVE;
5158 spin_unlock_irqrestore(&info->lock, flags);
5159 bh_handler(&info->task);
5160}
5161
diff --git a/drivers/tty/synclinkmp.c b/drivers/tty/synclinkmp.c
new file mode 100644
index 000000000000..c77831c7675a
--- /dev/null
+++ b/drivers/tty/synclinkmp.c
@@ -0,0 +1,5600 @@
1/*
2 * $Id: synclinkmp.c,v 4.38 2005/07/15 13:29:44 paulkf Exp $
3 *
4 * Device driver for Microgate SyncLink Multiport
5 * high speed multiprotocol serial adapter.
6 *
7 * written by Paul Fulghum for Microgate Corporation
8 * paulkf@microgate.com
9 *
10 * Microgate and SyncLink are trademarks of Microgate Corporation
11 *
12 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
13 * This code is released under the GNU General Public License (GPL)
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
25 * OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
29#if defined(__i386__)
30# define BREAKPOINT() asm(" int $3");
31#else
32# define BREAKPOINT() { }
33#endif
34
35#define MAX_DEVICES 12
36
37#include <linux/module.h>
38#include <linux/errno.h>
39#include <linux/signal.h>
40#include <linux/sched.h>
41#include <linux/timer.h>
42#include <linux/interrupt.h>
43#include <linux/pci.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/serial.h>
47#include <linux/major.h>
48#include <linux/string.h>
49#include <linux/fcntl.h>
50#include <linux/ptrace.h>
51#include <linux/ioport.h>
52#include <linux/mm.h>
53#include <linux/seq_file.h>
54#include <linux/slab.h>
55#include <linux/netdevice.h>
56#include <linux/vmalloc.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/ioctl.h>
60
61#include <asm/system.h>
62#include <asm/io.h>
63#include <asm/irq.h>
64#include <asm/dma.h>
65#include <linux/bitops.h>
66#include <asm/types.h>
67#include <linux/termios.h>
68#include <linux/workqueue.h>
69#include <linux/hdlc.h>
70#include <linux/synclink.h>
71
72#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINKMP_MODULE))
73#define SYNCLINK_GENERIC_HDLC 1
74#else
75#define SYNCLINK_GENERIC_HDLC 0
76#endif
77
78#define GET_USER(error,value,addr) error = get_user(value,addr)
79#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
80#define PUT_USER(error,value,addr) error = put_user(value,addr)
81#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
82
83#include <asm/uaccess.h>
84
85static MGSL_PARAMS default_params = {
86 MGSL_MODE_HDLC, /* unsigned long mode */
87 0, /* unsigned char loopback; */
88 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
89 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
90 0, /* unsigned long clock_speed; */
91 0xff, /* unsigned char addr_filter; */
92 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
93 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
94 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
95 9600, /* unsigned long data_rate; */
96 8, /* unsigned char data_bits; */
97 1, /* unsigned char stop_bits; */
98 ASYNC_PARITY_NONE /* unsigned char parity; */
99};
100
101/* size in bytes of DMA data buffers */
102#define SCABUFSIZE 1024
103#define SCA_MEM_SIZE 0x40000
104#define SCA_BASE_SIZE 512
105#define SCA_REG_SIZE 16
106#define SCA_MAX_PORTS 4
107#define SCAMAXDESC 128
108
109#define BUFFERLISTSIZE 4096
110
111/* SCA-I style DMA buffer descriptor */
112typedef struct _SCADESC
113{
114 u16 next; /* lower l6 bits of next descriptor addr */
115 u16 buf_ptr; /* lower 16 bits of buffer addr */
116 u8 buf_base; /* upper 8 bits of buffer addr */
117 u8 pad1;
118 u16 length; /* length of buffer */
119 u8 status; /* status of buffer */
120 u8 pad2;
121} SCADESC, *PSCADESC;
122
123typedef struct _SCADESC_EX
124{
125 /* device driver bookkeeping section */
126 char *virt_addr; /* virtual address of data buffer */
127 u16 phys_entry; /* lower 16-bits of physical address of this descriptor */
128} SCADESC_EX, *PSCADESC_EX;
129
130/* The queue of BH actions to be performed */
131
132#define BH_RECEIVE 1
133#define BH_TRANSMIT 2
134#define BH_STATUS 4
135
136#define IO_PIN_SHUTDOWN_LIMIT 100
137
138struct _input_signal_events {
139 int ri_up;
140 int ri_down;
141 int dsr_up;
142 int dsr_down;
143 int dcd_up;
144 int dcd_down;
145 int cts_up;
146 int cts_down;
147};
148
149/*
150 * Device instance data structure
151 */
152typedef struct _synclinkmp_info {
153 void *if_ptr; /* General purpose pointer (used by SPPP) */
154 int magic;
155 struct tty_port port;
156 int line;
157 unsigned short close_delay;
158 unsigned short closing_wait; /* time to wait before closing */
159
160 struct mgsl_icount icount;
161
162 int timeout;
163 int x_char; /* xon/xoff character */
164 u16 read_status_mask1; /* break detection (SR1 indications) */
165 u16 read_status_mask2; /* parity/framing/overun (SR2 indications) */
166 unsigned char ignore_status_mask1; /* break detection (SR1 indications) */
167 unsigned char ignore_status_mask2; /* parity/framing/overun (SR2 indications) */
168 unsigned char *tx_buf;
169 int tx_put;
170 int tx_get;
171 int tx_count;
172
173 wait_queue_head_t status_event_wait_q;
174 wait_queue_head_t event_wait_q;
175 struct timer_list tx_timer; /* HDLC transmit timeout timer */
176 struct _synclinkmp_info *next_device; /* device list link */
177 struct timer_list status_timer; /* input signal status check timer */
178
179 spinlock_t lock; /* spinlock for synchronizing with ISR */
180 struct work_struct task; /* task structure for scheduling bh */
181
182 u32 max_frame_size; /* as set by device config */
183
184 u32 pending_bh;
185
186 bool bh_running; /* Protection from multiple */
187 int isr_overflow;
188 bool bh_requested;
189
190 int dcd_chkcount; /* check counts to prevent */
191 int cts_chkcount; /* too many IRQs if a signal */
192 int dsr_chkcount; /* is floating */
193 int ri_chkcount;
194
195 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
196 unsigned long buffer_list_phys;
197
198 unsigned int rx_buf_count; /* count of total allocated Rx buffers */
199 SCADESC *rx_buf_list; /* list of receive buffer entries */
200 SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
201 unsigned int current_rx_buf;
202
203 unsigned int tx_buf_count; /* count of total allocated Tx buffers */
204 SCADESC *tx_buf_list; /* list of transmit buffer entries */
205 SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
206 unsigned int last_tx_buf;
207
208 unsigned char *tmp_rx_buf;
209 unsigned int tmp_rx_buf_count;
210
211 bool rx_enabled;
212 bool rx_overflow;
213
214 bool tx_enabled;
215 bool tx_active;
216 u32 idle_mode;
217
218 unsigned char ie0_value;
219 unsigned char ie1_value;
220 unsigned char ie2_value;
221 unsigned char ctrlreg_value;
222 unsigned char old_signals;
223
224 char device_name[25]; /* device instance name */
225
226 int port_count;
227 int adapter_num;
228 int port_num;
229
230 struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
231
232 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
233
234 unsigned int irq_level; /* interrupt level */
235 unsigned long irq_flags;
236 bool irq_requested; /* true if IRQ requested */
237
238 MGSL_PARAMS params; /* communications parameters */
239
240 unsigned char serial_signals; /* current serial signal states */
241
242 bool irq_occurred; /* for diagnostics use */
243 unsigned int init_error; /* Initialization startup error */
244
245 u32 last_mem_alloc;
246 unsigned char* memory_base; /* shared memory address (PCI only) */
247 u32 phys_memory_base;
248 int shared_mem_requested;
249
250 unsigned char* sca_base; /* HD64570 SCA Memory address */
251 u32 phys_sca_base;
252 u32 sca_offset;
253 bool sca_base_requested;
254
255 unsigned char* lcr_base; /* local config registers (PCI only) */
256 u32 phys_lcr_base;
257 u32 lcr_offset;
258 int lcr_mem_requested;
259
260 unsigned char* statctrl_base; /* status/control register memory */
261 u32 phys_statctrl_base;
262 u32 statctrl_offset;
263 bool sca_statctrl_requested;
264
265 u32 misc_ctrl_value;
266 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
267 char char_buf[MAX_ASYNC_BUFFER_SIZE];
268 bool drop_rts_on_tx_done;
269
270 struct _input_signal_events input_signal_events;
271
272 /* SPPP/Cisco HDLC device parts */
273 int netcount;
274 spinlock_t netlock;
275
276#if SYNCLINK_GENERIC_HDLC
277 struct net_device *netdev;
278#endif
279
280} SLMP_INFO;
281
282#define MGSL_MAGIC 0x5401
283
284/*
285 * define serial signal status change macros
286 */
287#define MISCSTATUS_DCD_LATCHED (SerialSignal_DCD<<8) /* indicates change in DCD */
288#define MISCSTATUS_RI_LATCHED (SerialSignal_RI<<8) /* indicates change in RI */
289#define MISCSTATUS_CTS_LATCHED (SerialSignal_CTS<<8) /* indicates change in CTS */
290#define MISCSTATUS_DSR_LATCHED (SerialSignal_DSR<<8) /* change in DSR */
291
292/* Common Register macros */
293#define LPR 0x00
294#define PABR0 0x02
295#define PABR1 0x03
296#define WCRL 0x04
297#define WCRM 0x05
298#define WCRH 0x06
299#define DPCR 0x08
300#define DMER 0x09
301#define ISR0 0x10
302#define ISR1 0x11
303#define ISR2 0x12
304#define IER0 0x14
305#define IER1 0x15
306#define IER2 0x16
307#define ITCR 0x18
308#define INTVR 0x1a
309#define IMVR 0x1c
310
311/* MSCI Register macros */
312#define TRB 0x20
313#define TRBL 0x20
314#define TRBH 0x21
315#define SR0 0x22
316#define SR1 0x23
317#define SR2 0x24
318#define SR3 0x25
319#define FST 0x26
320#define IE0 0x28
321#define IE1 0x29
322#define IE2 0x2a
323#define FIE 0x2b
324#define CMD 0x2c
325#define MD0 0x2e
326#define MD1 0x2f
327#define MD2 0x30
328#define CTL 0x31
329#define SA0 0x32
330#define SA1 0x33
331#define IDL 0x34
332#define TMC 0x35
333#define RXS 0x36
334#define TXS 0x37
335#define TRC0 0x38
336#define TRC1 0x39
337#define RRC 0x3a
338#define CST0 0x3c
339#define CST1 0x3d
340
341/* Timer Register Macros */
342#define TCNT 0x60
343#define TCNTL 0x60
344#define TCNTH 0x61
345#define TCONR 0x62
346#define TCONRL 0x62
347#define TCONRH 0x63
348#define TMCS 0x64
349#define TEPR 0x65
350
351/* DMA Controller Register macros */
352#define DARL 0x80
353#define DARH 0x81
354#define DARB 0x82
355#define BAR 0x80
356#define BARL 0x80
357#define BARH 0x81
358#define BARB 0x82
359#define SAR 0x84
360#define SARL 0x84
361#define SARH 0x85
362#define SARB 0x86
363#define CPB 0x86
364#define CDA 0x88
365#define CDAL 0x88
366#define CDAH 0x89
367#define EDA 0x8a
368#define EDAL 0x8a
369#define EDAH 0x8b
370#define BFL 0x8c
371#define BFLL 0x8c
372#define BFLH 0x8d
373#define BCR 0x8e
374#define BCRL 0x8e
375#define BCRH 0x8f
376#define DSR 0x90
377#define DMR 0x91
378#define FCT 0x93
379#define DIR 0x94
380#define DCMD 0x95
381
382/* combine with timer or DMA register address */
383#define TIMER0 0x00
384#define TIMER1 0x08
385#define TIMER2 0x10
386#define TIMER3 0x18
387#define RXDMA 0x00
388#define TXDMA 0x20
389
390/* SCA Command Codes */
391#define NOOP 0x00
392#define TXRESET 0x01
393#define TXENABLE 0x02
394#define TXDISABLE 0x03
395#define TXCRCINIT 0x04
396#define TXCRCEXCL 0x05
397#define TXEOM 0x06
398#define TXABORT 0x07
399#define MPON 0x08
400#define TXBUFCLR 0x09
401#define RXRESET 0x11
402#define RXENABLE 0x12
403#define RXDISABLE 0x13
404#define RXCRCINIT 0x14
405#define RXREJECT 0x15
406#define SEARCHMP 0x16
407#define RXCRCEXCL 0x17
408#define RXCRCCALC 0x18
409#define CHRESET 0x21
410#define HUNT 0x31
411
412/* DMA command codes */
413#define SWABORT 0x01
414#define FEICLEAR 0x02
415
416/* IE0 */
417#define TXINTE BIT7
418#define RXINTE BIT6
419#define TXRDYE BIT1
420#define RXRDYE BIT0
421
422/* IE1 & SR1 */
423#define UDRN BIT7
424#define IDLE BIT6
425#define SYNCD BIT4
426#define FLGD BIT4
427#define CCTS BIT3
428#define CDCD BIT2
429#define BRKD BIT1
430#define ABTD BIT1
431#define GAPD BIT1
432#define BRKE BIT0
433#define IDLD BIT0
434
435/* IE2 & SR2 */
436#define EOM BIT7
437#define PMP BIT6
438#define SHRT BIT6
439#define PE BIT5
440#define ABT BIT5
441#define FRME BIT4
442#define RBIT BIT4
443#define OVRN BIT3
444#define CRCE BIT2
445
446
447/*
448 * Global linked list of SyncLink devices
449 */
450static SLMP_INFO *synclinkmp_device_list = NULL;
451static int synclinkmp_adapter_count = -1;
452static int synclinkmp_device_count = 0;
453
454/*
455 * Set this param to non-zero to load eax with the
456 * .text section address and breakpoint on module load.
457 * This is useful for use with gdb and add-symbol-file command.
458 */
459static int break_on_load = 0;
460
461/*
462 * Driver major number, defaults to zero to get auto
463 * assigned major number. May be forced as module parameter.
464 */
465static int ttymajor = 0;
466
467/*
468 * Array of user specified options for ISA adapters.
469 */
470static int debug_level = 0;
471static int maxframe[MAX_DEVICES] = {0,};
472
473module_param(break_on_load, bool, 0);
474module_param(ttymajor, int, 0);
475module_param(debug_level, int, 0);
476module_param_array(maxframe, int, NULL, 0);
477
478static char *driver_name = "SyncLink MultiPort driver";
479static char *driver_version = "$Revision: 4.38 $";
480
481static int synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
482static void synclinkmp_remove_one(struct pci_dev *dev);
483
484static struct pci_device_id synclinkmp_pci_tbl[] = {
485 { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
486 { 0, }, /* terminate list */
487};
488MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
489
490MODULE_LICENSE("GPL");
491
492static struct pci_driver synclinkmp_pci_driver = {
493 .name = "synclinkmp",
494 .id_table = synclinkmp_pci_tbl,
495 .probe = synclinkmp_init_one,
496 .remove = __devexit_p(synclinkmp_remove_one),
497};
498
499
500static struct tty_driver *serial_driver;
501
502/* number of characters left in xmit buffer before we ask for more */
503#define WAKEUP_CHARS 256
504
505
506/* tty callbacks */
507
508static int open(struct tty_struct *tty, struct file * filp);
509static void close(struct tty_struct *tty, struct file * filp);
510static void hangup(struct tty_struct *tty);
511static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
512
513static int write(struct tty_struct *tty, const unsigned char *buf, int count);
514static int put_char(struct tty_struct *tty, unsigned char ch);
515static void send_xchar(struct tty_struct *tty, char ch);
516static void wait_until_sent(struct tty_struct *tty, int timeout);
517static int write_room(struct tty_struct *tty);
518static void flush_chars(struct tty_struct *tty);
519static void flush_buffer(struct tty_struct *tty);
520static void tx_hold(struct tty_struct *tty);
521static void tx_release(struct tty_struct *tty);
522
523static int ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
524static int chars_in_buffer(struct tty_struct *tty);
525static void throttle(struct tty_struct * tty);
526static void unthrottle(struct tty_struct * tty);
527static int set_break(struct tty_struct *tty, int break_state);
528
529#if SYNCLINK_GENERIC_HDLC
530#define dev_to_port(D) (dev_to_hdlc(D)->priv)
531static void hdlcdev_tx_done(SLMP_INFO *info);
532static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size);
533static int hdlcdev_init(SLMP_INFO *info);
534static void hdlcdev_exit(SLMP_INFO *info);
535#endif
536
537/* ioctl handlers */
538
539static int get_stats(SLMP_INFO *info, struct mgsl_icount __user *user_icount);
540static int get_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
541static int set_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
542static int get_txidle(SLMP_INFO *info, int __user *idle_mode);
543static int set_txidle(SLMP_INFO *info, int idle_mode);
544static int tx_enable(SLMP_INFO *info, int enable);
545static int tx_abort(SLMP_INFO *info);
546static int rx_enable(SLMP_INFO *info, int enable);
547static int modem_input_wait(SLMP_INFO *info,int arg);
548static int wait_mgsl_event(SLMP_INFO *info, int __user *mask_ptr);
549static int tiocmget(struct tty_struct *tty);
550static int tiocmset(struct tty_struct *tty,
551 unsigned int set, unsigned int clear);
552static int set_break(struct tty_struct *tty, int break_state);
553
554static void add_device(SLMP_INFO *info);
555static void device_init(int adapter_num, struct pci_dev *pdev);
556static int claim_resources(SLMP_INFO *info);
557static void release_resources(SLMP_INFO *info);
558
559static int startup(SLMP_INFO *info);
560static int block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
561static int carrier_raised(struct tty_port *port);
562static void shutdown(SLMP_INFO *info);
563static void program_hw(SLMP_INFO *info);
564static void change_params(SLMP_INFO *info);
565
566static bool init_adapter(SLMP_INFO *info);
567static bool register_test(SLMP_INFO *info);
568static bool irq_test(SLMP_INFO *info);
569static bool loopback_test(SLMP_INFO *info);
570static int adapter_test(SLMP_INFO *info);
571static bool memory_test(SLMP_INFO *info);
572
573static void reset_adapter(SLMP_INFO *info);
574static void reset_port(SLMP_INFO *info);
575static void async_mode(SLMP_INFO *info);
576static void hdlc_mode(SLMP_INFO *info);
577
578static void rx_stop(SLMP_INFO *info);
579static void rx_start(SLMP_INFO *info);
580static void rx_reset_buffers(SLMP_INFO *info);
581static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
582static bool rx_get_frame(SLMP_INFO *info);
583
584static void tx_start(SLMP_INFO *info);
585static void tx_stop(SLMP_INFO *info);
586static void tx_load_fifo(SLMP_INFO *info);
587static void tx_set_idle(SLMP_INFO *info);
588static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
589
590static void get_signals(SLMP_INFO *info);
591static void set_signals(SLMP_INFO *info);
592static void enable_loopback(SLMP_INFO *info, int enable);
593static void set_rate(SLMP_INFO *info, u32 data_rate);
594
595static int bh_action(SLMP_INFO *info);
596static void bh_handler(struct work_struct *work);
597static void bh_receive(SLMP_INFO *info);
598static void bh_transmit(SLMP_INFO *info);
599static void bh_status(SLMP_INFO *info);
600static void isr_timer(SLMP_INFO *info);
601static void isr_rxint(SLMP_INFO *info);
602static void isr_rxrdy(SLMP_INFO *info);
603static void isr_txint(SLMP_INFO *info);
604static void isr_txrdy(SLMP_INFO *info);
605static void isr_rxdmaok(SLMP_INFO *info);
606static void isr_rxdmaerror(SLMP_INFO *info);
607static void isr_txdmaok(SLMP_INFO *info);
608static void isr_txdmaerror(SLMP_INFO *info);
609static void isr_io_pin(SLMP_INFO *info, u16 status);
610
611static int alloc_dma_bufs(SLMP_INFO *info);
612static void free_dma_bufs(SLMP_INFO *info);
613static int alloc_buf_list(SLMP_INFO *info);
614static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
615static int alloc_tmp_rx_buf(SLMP_INFO *info);
616static void free_tmp_rx_buf(SLMP_INFO *info);
617
618static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
619static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
620static void tx_timeout(unsigned long context);
621static void status_timeout(unsigned long context);
622
623static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
624static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
625static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
626static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
627static unsigned char read_status_reg(SLMP_INFO * info);
628static void write_control_reg(SLMP_INFO * info);
629
630
631static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
632static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
633static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
634
635static u32 misc_ctrl_value = 0x007e4040;
636static u32 lcr1_brdr_value = 0x00800028;
637
638static u32 read_ahead_count = 8;
639
640/* DPCR, DMA Priority Control
641 *
642 * 07..05 Not used, must be 0
643 * 04 BRC, bus release condition: 0=all transfers complete
644 * 1=release after 1 xfer on all channels
645 * 03 CCC, channel change condition: 0=every cycle
646 * 1=after each channel completes all xfers
647 * 02..00 PR<2..0>, priority 100=round robin
648 *
649 * 00000100 = 0x00
650 */
651static unsigned char dma_priority = 0x04;
652
653// Number of bytes that can be written to shared RAM
654// in a single write operation
655static u32 sca_pci_load_interval = 64;
656
657/*
658 * 1st function defined in .text section. Calling this function in
659 * init_module() followed by a breakpoint allows a remote debugger
660 * (gdb) to get the .text address for the add-symbol-file command.
661 * This allows remote debugging of dynamically loadable modules.
662 */
663static void* synclinkmp_get_text_ptr(void);
664static void* synclinkmp_get_text_ptr(void) {return synclinkmp_get_text_ptr;}
665
666static inline int sanity_check(SLMP_INFO *info,
667 char *name, const char *routine)
668{
669#ifdef SANITY_CHECK
670 static const char *badmagic =
671 "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
672 static const char *badinfo =
673 "Warning: null synclinkmp_struct for (%s) in %s\n";
674
675 if (!info) {
676 printk(badinfo, name, routine);
677 return 1;
678 }
679 if (info->magic != MGSL_MAGIC) {
680 printk(badmagic, name, routine);
681 return 1;
682 }
683#else
684 if (!info)
685 return 1;
686#endif
687 return 0;
688}
689
690/**
691 * line discipline callback wrappers
692 *
693 * The wrappers maintain line discipline references
694 * while calling into the line discipline.
695 *
696 * ldisc_receive_buf - pass receive data to line discipline
697 */
698
699static void ldisc_receive_buf(struct tty_struct *tty,
700 const __u8 *data, char *flags, int count)
701{
702 struct tty_ldisc *ld;
703 if (!tty)
704 return;
705 ld = tty_ldisc_ref(tty);
706 if (ld) {
707 if (ld->ops->receive_buf)
708 ld->ops->receive_buf(tty, data, flags, count);
709 tty_ldisc_deref(ld);
710 }
711}
712
713/* tty callbacks */
714
715/* Called when a port is opened. Init and enable port.
716 */
717static int open(struct tty_struct *tty, struct file *filp)
718{
719 SLMP_INFO *info;
720 int retval, line;
721 unsigned long flags;
722
723 line = tty->index;
724 if ((line < 0) || (line >= synclinkmp_device_count)) {
725 printk("%s(%d): open with invalid line #%d.\n",
726 __FILE__,__LINE__,line);
727 return -ENODEV;
728 }
729
730 info = synclinkmp_device_list;
731 while(info && info->line != line)
732 info = info->next_device;
733 if (sanity_check(info, tty->name, "open"))
734 return -ENODEV;
735 if ( info->init_error ) {
736 printk("%s(%d):%s device is not allocated, init error=%d\n",
737 __FILE__,__LINE__,info->device_name,info->init_error);
738 return -ENODEV;
739 }
740
741 tty->driver_data = info;
742 info->port.tty = tty;
743
744 if (debug_level >= DEBUG_LEVEL_INFO)
745 printk("%s(%d):%s open(), old ref count = %d\n",
746 __FILE__,__LINE__,tty->driver->name, info->port.count);
747
748 /* If port is closing, signal caller to try again */
749 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
750 if (info->port.flags & ASYNC_CLOSING)
751 interruptible_sleep_on(&info->port.close_wait);
752 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
753 -EAGAIN : -ERESTARTSYS);
754 goto cleanup;
755 }
756
757 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
758
759 spin_lock_irqsave(&info->netlock, flags);
760 if (info->netcount) {
761 retval = -EBUSY;
762 spin_unlock_irqrestore(&info->netlock, flags);
763 goto cleanup;
764 }
765 info->port.count++;
766 spin_unlock_irqrestore(&info->netlock, flags);
767
768 if (info->port.count == 1) {
769 /* 1st open on this device, init hardware */
770 retval = startup(info);
771 if (retval < 0)
772 goto cleanup;
773 }
774
775 retval = block_til_ready(tty, filp, info);
776 if (retval) {
777 if (debug_level >= DEBUG_LEVEL_INFO)
778 printk("%s(%d):%s block_til_ready() returned %d\n",
779 __FILE__,__LINE__, info->device_name, retval);
780 goto cleanup;
781 }
782
783 if (debug_level >= DEBUG_LEVEL_INFO)
784 printk("%s(%d):%s open() success\n",
785 __FILE__,__LINE__, info->device_name);
786 retval = 0;
787
788cleanup:
789 if (retval) {
790 if (tty->count == 1)
791 info->port.tty = NULL; /* tty layer will release tty struct */
792 if(info->port.count)
793 info->port.count--;
794 }
795
796 return retval;
797}
798
799/* Called when port is closed. Wait for remaining data to be
800 * sent. Disable port and free resources.
801 */
802static void close(struct tty_struct *tty, struct file *filp)
803{
804 SLMP_INFO * info = tty->driver_data;
805
806 if (sanity_check(info, tty->name, "close"))
807 return;
808
809 if (debug_level >= DEBUG_LEVEL_INFO)
810 printk("%s(%d):%s close() entry, count=%d\n",
811 __FILE__,__LINE__, info->device_name, info->port.count);
812
813 if (tty_port_close_start(&info->port, tty, filp) == 0)
814 goto cleanup;
815
816 mutex_lock(&info->port.mutex);
817 if (info->port.flags & ASYNC_INITIALIZED)
818 wait_until_sent(tty, info->timeout);
819
820 flush_buffer(tty);
821 tty_ldisc_flush(tty);
822 shutdown(info);
823 mutex_unlock(&info->port.mutex);
824
825 tty_port_close_end(&info->port, tty);
826 info->port.tty = NULL;
827cleanup:
828 if (debug_level >= DEBUG_LEVEL_INFO)
829 printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
830 tty->driver->name, info->port.count);
831}
832
833/* Called by tty_hangup() when a hangup is signaled.
834 * This is the same as closing all open descriptors for the port.
835 */
836static void hangup(struct tty_struct *tty)
837{
838 SLMP_INFO *info = tty->driver_data;
839 unsigned long flags;
840
841 if (debug_level >= DEBUG_LEVEL_INFO)
842 printk("%s(%d):%s hangup()\n",
843 __FILE__,__LINE__, info->device_name );
844
845 if (sanity_check(info, tty->name, "hangup"))
846 return;
847
848 mutex_lock(&info->port.mutex);
849 flush_buffer(tty);
850 shutdown(info);
851
852 spin_lock_irqsave(&info->port.lock, flags);
853 info->port.count = 0;
854 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
855 info->port.tty = NULL;
856 spin_unlock_irqrestore(&info->port.lock, flags);
857 mutex_unlock(&info->port.mutex);
858
859 wake_up_interruptible(&info->port.open_wait);
860}
861
862/* Set new termios settings
863 */
864static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
865{
866 SLMP_INFO *info = tty->driver_data;
867 unsigned long flags;
868
869 if (debug_level >= DEBUG_LEVEL_INFO)
870 printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
871 tty->driver->name );
872
873 change_params(info);
874
875 /* Handle transition to B0 status */
876 if (old_termios->c_cflag & CBAUD &&
877 !(tty->termios->c_cflag & CBAUD)) {
878 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
879 spin_lock_irqsave(&info->lock,flags);
880 set_signals(info);
881 spin_unlock_irqrestore(&info->lock,flags);
882 }
883
884 /* Handle transition away from B0 status */
885 if (!(old_termios->c_cflag & CBAUD) &&
886 tty->termios->c_cflag & CBAUD) {
887 info->serial_signals |= SerialSignal_DTR;
888 if (!(tty->termios->c_cflag & CRTSCTS) ||
889 !test_bit(TTY_THROTTLED, &tty->flags)) {
890 info->serial_signals |= SerialSignal_RTS;
891 }
892 spin_lock_irqsave(&info->lock,flags);
893 set_signals(info);
894 spin_unlock_irqrestore(&info->lock,flags);
895 }
896
897 /* Handle turning off CRTSCTS */
898 if (old_termios->c_cflag & CRTSCTS &&
899 !(tty->termios->c_cflag & CRTSCTS)) {
900 tty->hw_stopped = 0;
901 tx_release(tty);
902 }
903}
904
905/* Send a block of data
906 *
907 * Arguments:
908 *
909 * tty pointer to tty information structure
910 * buf pointer to buffer containing send data
911 * count size of send data in bytes
912 *
913 * Return Value: number of characters written
914 */
915static int write(struct tty_struct *tty,
916 const unsigned char *buf, int count)
917{
918 int c, ret = 0;
919 SLMP_INFO *info = tty->driver_data;
920 unsigned long flags;
921
922 if (debug_level >= DEBUG_LEVEL_INFO)
923 printk("%s(%d):%s write() count=%d\n",
924 __FILE__,__LINE__,info->device_name,count);
925
926 if (sanity_check(info, tty->name, "write"))
927 goto cleanup;
928
929 if (!info->tx_buf)
930 goto cleanup;
931
932 if (info->params.mode == MGSL_MODE_HDLC) {
933 if (count > info->max_frame_size) {
934 ret = -EIO;
935 goto cleanup;
936 }
937 if (info->tx_active)
938 goto cleanup;
939 if (info->tx_count) {
940 /* send accumulated data from send_char() calls */
941 /* as frame and wait before accepting more data. */
942 tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
943 goto start;
944 }
945 ret = info->tx_count = count;
946 tx_load_dma_buffer(info, buf, count);
947 goto start;
948 }
949
950 for (;;) {
951 c = min_t(int, count,
952 min(info->max_frame_size - info->tx_count - 1,
953 info->max_frame_size - info->tx_put));
954 if (c <= 0)
955 break;
956
957 memcpy(info->tx_buf + info->tx_put, buf, c);
958
959 spin_lock_irqsave(&info->lock,flags);
960 info->tx_put += c;
961 if (info->tx_put >= info->max_frame_size)
962 info->tx_put -= info->max_frame_size;
963 info->tx_count += c;
964 spin_unlock_irqrestore(&info->lock,flags);
965
966 buf += c;
967 count -= c;
968 ret += c;
969 }
970
971 if (info->params.mode == MGSL_MODE_HDLC) {
972 if (count) {
973 ret = info->tx_count = 0;
974 goto cleanup;
975 }
976 tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
977 }
978start:
979 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
980 spin_lock_irqsave(&info->lock,flags);
981 if (!info->tx_active)
982 tx_start(info);
983 spin_unlock_irqrestore(&info->lock,flags);
984 }
985
986cleanup:
987 if (debug_level >= DEBUG_LEVEL_INFO)
988 printk( "%s(%d):%s write() returning=%d\n",
989 __FILE__,__LINE__,info->device_name,ret);
990 return ret;
991}
992
993/* Add a character to the transmit buffer.
994 */
995static int put_char(struct tty_struct *tty, unsigned char ch)
996{
997 SLMP_INFO *info = tty->driver_data;
998 unsigned long flags;
999 int ret = 0;
1000
1001 if ( debug_level >= DEBUG_LEVEL_INFO ) {
1002 printk( "%s(%d):%s put_char(%d)\n",
1003 __FILE__,__LINE__,info->device_name,ch);
1004 }
1005
1006 if (sanity_check(info, tty->name, "put_char"))
1007 return 0;
1008
1009 if (!info->tx_buf)
1010 return 0;
1011
1012 spin_lock_irqsave(&info->lock,flags);
1013
1014 if ( (info->params.mode != MGSL_MODE_HDLC) ||
1015 !info->tx_active ) {
1016
1017 if (info->tx_count < info->max_frame_size - 1) {
1018 info->tx_buf[info->tx_put++] = ch;
1019 if (info->tx_put >= info->max_frame_size)
1020 info->tx_put -= info->max_frame_size;
1021 info->tx_count++;
1022 ret = 1;
1023 }
1024 }
1025
1026 spin_unlock_irqrestore(&info->lock,flags);
1027 return ret;
1028}
1029
1030/* Send a high-priority XON/XOFF character
1031 */
1032static void send_xchar(struct tty_struct *tty, char ch)
1033{
1034 SLMP_INFO *info = tty->driver_data;
1035 unsigned long flags;
1036
1037 if (debug_level >= DEBUG_LEVEL_INFO)
1038 printk("%s(%d):%s send_xchar(%d)\n",
1039 __FILE__,__LINE__, info->device_name, ch );
1040
1041 if (sanity_check(info, tty->name, "send_xchar"))
1042 return;
1043
1044 info->x_char = ch;
1045 if (ch) {
1046 /* Make sure transmit interrupts are on */
1047 spin_lock_irqsave(&info->lock,flags);
1048 if (!info->tx_enabled)
1049 tx_start(info);
1050 spin_unlock_irqrestore(&info->lock,flags);
1051 }
1052}
1053
1054/* Wait until the transmitter is empty.
1055 */
1056static void wait_until_sent(struct tty_struct *tty, int timeout)
1057{
1058 SLMP_INFO * info = tty->driver_data;
1059 unsigned long orig_jiffies, char_time;
1060
1061 if (!info )
1062 return;
1063
1064 if (debug_level >= DEBUG_LEVEL_INFO)
1065 printk("%s(%d):%s wait_until_sent() entry\n",
1066 __FILE__,__LINE__, info->device_name );
1067
1068 if (sanity_check(info, tty->name, "wait_until_sent"))
1069 return;
1070
1071 if (!test_bit(ASYNCB_INITIALIZED, &info->port.flags))
1072 goto exit;
1073
1074 orig_jiffies = jiffies;
1075
1076 /* Set check interval to 1/5 of estimated time to
1077 * send a character, and make it at least 1. The check
1078 * interval should also be less than the timeout.
1079 * Note: use tight timings here to satisfy the NIST-PCTS.
1080 */
1081
1082 if ( info->params.data_rate ) {
1083 char_time = info->timeout/(32 * 5);
1084 if (!char_time)
1085 char_time++;
1086 } else
1087 char_time = 1;
1088
1089 if (timeout)
1090 char_time = min_t(unsigned long, char_time, timeout);
1091
1092 if ( info->params.mode == MGSL_MODE_HDLC ) {
1093 while (info->tx_active) {
1094 msleep_interruptible(jiffies_to_msecs(char_time));
1095 if (signal_pending(current))
1096 break;
1097 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1098 break;
1099 }
1100 } else {
1101 /*
1102 * TODO: determine if there is something similar to USC16C32
1103 * TXSTATUS_ALL_SENT status
1104 */
1105 while ( info->tx_active && info->tx_enabled) {
1106 msleep_interruptible(jiffies_to_msecs(char_time));
1107 if (signal_pending(current))
1108 break;
1109 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1110 break;
1111 }
1112 }
1113
1114exit:
1115 if (debug_level >= DEBUG_LEVEL_INFO)
1116 printk("%s(%d):%s wait_until_sent() exit\n",
1117 __FILE__,__LINE__, info->device_name );
1118}
1119
1120/* Return the count of free bytes in transmit buffer
1121 */
1122static int write_room(struct tty_struct *tty)
1123{
1124 SLMP_INFO *info = tty->driver_data;
1125 int ret;
1126
1127 if (sanity_check(info, tty->name, "write_room"))
1128 return 0;
1129
1130 if (info->params.mode == MGSL_MODE_HDLC) {
1131 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
1132 } else {
1133 ret = info->max_frame_size - info->tx_count - 1;
1134 if (ret < 0)
1135 ret = 0;
1136 }
1137
1138 if (debug_level >= DEBUG_LEVEL_INFO)
1139 printk("%s(%d):%s write_room()=%d\n",
1140 __FILE__, __LINE__, info->device_name, ret);
1141
1142 return ret;
1143}
1144
1145/* enable transmitter and send remaining buffered characters
1146 */
1147static void flush_chars(struct tty_struct *tty)
1148{
1149 SLMP_INFO *info = tty->driver_data;
1150 unsigned long flags;
1151
1152 if ( debug_level >= DEBUG_LEVEL_INFO )
1153 printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
1154 __FILE__,__LINE__,info->device_name,info->tx_count);
1155
1156 if (sanity_check(info, tty->name, "flush_chars"))
1157 return;
1158
1159 if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
1160 !info->tx_buf)
1161 return;
1162
1163 if ( debug_level >= DEBUG_LEVEL_INFO )
1164 printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
1165 __FILE__,__LINE__,info->device_name );
1166
1167 spin_lock_irqsave(&info->lock,flags);
1168
1169 if (!info->tx_active) {
1170 if ( (info->params.mode == MGSL_MODE_HDLC) &&
1171 info->tx_count ) {
1172 /* operating in synchronous (frame oriented) mode */
1173 /* copy data from circular tx_buf to */
1174 /* transmit DMA buffer. */
1175 tx_load_dma_buffer(info,
1176 info->tx_buf,info->tx_count);
1177 }
1178 tx_start(info);
1179 }
1180
1181 spin_unlock_irqrestore(&info->lock,flags);
1182}
1183
1184/* Discard all data in the send buffer
1185 */
1186static void flush_buffer(struct tty_struct *tty)
1187{
1188 SLMP_INFO *info = tty->driver_data;
1189 unsigned long flags;
1190
1191 if (debug_level >= DEBUG_LEVEL_INFO)
1192 printk("%s(%d):%s flush_buffer() entry\n",
1193 __FILE__,__LINE__, info->device_name );
1194
1195 if (sanity_check(info, tty->name, "flush_buffer"))
1196 return;
1197
1198 spin_lock_irqsave(&info->lock,flags);
1199 info->tx_count = info->tx_put = info->tx_get = 0;
1200 del_timer(&info->tx_timer);
1201 spin_unlock_irqrestore(&info->lock,flags);
1202
1203 tty_wakeup(tty);
1204}
1205
1206/* throttle (stop) transmitter
1207 */
1208static void tx_hold(struct tty_struct *tty)
1209{
1210 SLMP_INFO *info = tty->driver_data;
1211 unsigned long flags;
1212
1213 if (sanity_check(info, tty->name, "tx_hold"))
1214 return;
1215
1216 if ( debug_level >= DEBUG_LEVEL_INFO )
1217 printk("%s(%d):%s tx_hold()\n",
1218 __FILE__,__LINE__,info->device_name);
1219
1220 spin_lock_irqsave(&info->lock,flags);
1221 if (info->tx_enabled)
1222 tx_stop(info);
1223 spin_unlock_irqrestore(&info->lock,flags);
1224}
1225
1226/* release (start) transmitter
1227 */
1228static void tx_release(struct tty_struct *tty)
1229{
1230 SLMP_INFO *info = tty->driver_data;
1231 unsigned long flags;
1232
1233 if (sanity_check(info, tty->name, "tx_release"))
1234 return;
1235
1236 if ( debug_level >= DEBUG_LEVEL_INFO )
1237 printk("%s(%d):%s tx_release()\n",
1238 __FILE__,__LINE__,info->device_name);
1239
1240 spin_lock_irqsave(&info->lock,flags);
1241 if (!info->tx_enabled)
1242 tx_start(info);
1243 spin_unlock_irqrestore(&info->lock,flags);
1244}
1245
1246/* Service an IOCTL request
1247 *
1248 * Arguments:
1249 *
1250 * tty pointer to tty instance data
1251 * cmd IOCTL command code
1252 * arg command argument/context
1253 *
1254 * Return Value: 0 if success, otherwise error code
1255 */
1256static int ioctl(struct tty_struct *tty,
1257 unsigned int cmd, unsigned long arg)
1258{
1259 SLMP_INFO *info = tty->driver_data;
1260 void __user *argp = (void __user *)arg;
1261
1262 if (debug_level >= DEBUG_LEVEL_INFO)
1263 printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
1264 info->device_name, cmd );
1265
1266 if (sanity_check(info, tty->name, "ioctl"))
1267 return -ENODEV;
1268
1269 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1270 (cmd != TIOCMIWAIT)) {
1271 if (tty->flags & (1 << TTY_IO_ERROR))
1272 return -EIO;
1273 }
1274
1275 switch (cmd) {
1276 case MGSL_IOCGPARAMS:
1277 return get_params(info, argp);
1278 case MGSL_IOCSPARAMS:
1279 return set_params(info, argp);
1280 case MGSL_IOCGTXIDLE:
1281 return get_txidle(info, argp);
1282 case MGSL_IOCSTXIDLE:
1283 return set_txidle(info, (int)arg);
1284 case MGSL_IOCTXENABLE:
1285 return tx_enable(info, (int)arg);
1286 case MGSL_IOCRXENABLE:
1287 return rx_enable(info, (int)arg);
1288 case MGSL_IOCTXABORT:
1289 return tx_abort(info);
1290 case MGSL_IOCGSTATS:
1291 return get_stats(info, argp);
1292 case MGSL_IOCWAITEVENT:
1293 return wait_mgsl_event(info, argp);
1294 case MGSL_IOCLOOPTXDONE:
1295 return 0; // TODO: Not supported, need to document
1296 /* Wait for modem input (DCD,RI,DSR,CTS) change
1297 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
1298 */
1299 case TIOCMIWAIT:
1300 return modem_input_wait(info,(int)arg);
1301
1302 /*
1303 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1304 * Return: write counters to the user passed counter struct
1305 * NB: both 1->0 and 0->1 transitions are counted except for
1306 * RI where only 0->1 is counted.
1307 */
1308 default:
1309 return -ENOIOCTLCMD;
1310 }
1311 return 0;
1312}
1313
1314static int get_icount(struct tty_struct *tty,
1315 struct serial_icounter_struct *icount)
1316{
1317 SLMP_INFO *info = tty->driver_data;
1318 struct mgsl_icount cnow; /* kernel counter temps */
1319 unsigned long flags;
1320
1321 spin_lock_irqsave(&info->lock,flags);
1322 cnow = info->icount;
1323 spin_unlock_irqrestore(&info->lock,flags);
1324
1325 icount->cts = cnow.cts;
1326 icount->dsr = cnow.dsr;
1327 icount->rng = cnow.rng;
1328 icount->dcd = cnow.dcd;
1329 icount->rx = cnow.rx;
1330 icount->tx = cnow.tx;
1331 icount->frame = cnow.frame;
1332 icount->overrun = cnow.overrun;
1333 icount->parity = cnow.parity;
1334 icount->brk = cnow.brk;
1335 icount->buf_overrun = cnow.buf_overrun;
1336
1337 return 0;
1338}
1339
1340/*
1341 * /proc fs routines....
1342 */
1343
1344static inline void line_info(struct seq_file *m, SLMP_INFO *info)
1345{
1346 char stat_buf[30];
1347 unsigned long flags;
1348
1349 seq_printf(m, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
1350 "\tIRQ=%d MaxFrameSize=%u\n",
1351 info->device_name,
1352 info->phys_sca_base,
1353 info->phys_memory_base,
1354 info->phys_statctrl_base,
1355 info->phys_lcr_base,
1356 info->irq_level,
1357 info->max_frame_size );
1358
1359 /* output current serial signal states */
1360 spin_lock_irqsave(&info->lock,flags);
1361 get_signals(info);
1362 spin_unlock_irqrestore(&info->lock,flags);
1363
1364 stat_buf[0] = 0;
1365 stat_buf[1] = 0;
1366 if (info->serial_signals & SerialSignal_RTS)
1367 strcat(stat_buf, "|RTS");
1368 if (info->serial_signals & SerialSignal_CTS)
1369 strcat(stat_buf, "|CTS");
1370 if (info->serial_signals & SerialSignal_DTR)
1371 strcat(stat_buf, "|DTR");
1372 if (info->serial_signals & SerialSignal_DSR)
1373 strcat(stat_buf, "|DSR");
1374 if (info->serial_signals & SerialSignal_DCD)
1375 strcat(stat_buf, "|CD");
1376 if (info->serial_signals & SerialSignal_RI)
1377 strcat(stat_buf, "|RI");
1378
1379 if (info->params.mode == MGSL_MODE_HDLC) {
1380 seq_printf(m, "\tHDLC txok:%d rxok:%d",
1381 info->icount.txok, info->icount.rxok);
1382 if (info->icount.txunder)
1383 seq_printf(m, " txunder:%d", info->icount.txunder);
1384 if (info->icount.txabort)
1385 seq_printf(m, " txabort:%d", info->icount.txabort);
1386 if (info->icount.rxshort)
1387 seq_printf(m, " rxshort:%d", info->icount.rxshort);
1388 if (info->icount.rxlong)
1389 seq_printf(m, " rxlong:%d", info->icount.rxlong);
1390 if (info->icount.rxover)
1391 seq_printf(m, " rxover:%d", info->icount.rxover);
1392 if (info->icount.rxcrc)
1393 seq_printf(m, " rxlong:%d", info->icount.rxcrc);
1394 } else {
1395 seq_printf(m, "\tASYNC tx:%d rx:%d",
1396 info->icount.tx, info->icount.rx);
1397 if (info->icount.frame)
1398 seq_printf(m, " fe:%d", info->icount.frame);
1399 if (info->icount.parity)
1400 seq_printf(m, " pe:%d", info->icount.parity);
1401 if (info->icount.brk)
1402 seq_printf(m, " brk:%d", info->icount.brk);
1403 if (info->icount.overrun)
1404 seq_printf(m, " oe:%d", info->icount.overrun);
1405 }
1406
1407 /* Append serial signal status to end */
1408 seq_printf(m, " %s\n", stat_buf+1);
1409
1410 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1411 info->tx_active,info->bh_requested,info->bh_running,
1412 info->pending_bh);
1413}
1414
1415/* Called to print information about devices
1416 */
1417static int synclinkmp_proc_show(struct seq_file *m, void *v)
1418{
1419 SLMP_INFO *info;
1420
1421 seq_printf(m, "synclinkmp driver:%s\n", driver_version);
1422
1423 info = synclinkmp_device_list;
1424 while( info ) {
1425 line_info(m, info);
1426 info = info->next_device;
1427 }
1428 return 0;
1429}
1430
1431static int synclinkmp_proc_open(struct inode *inode, struct file *file)
1432{
1433 return single_open(file, synclinkmp_proc_show, NULL);
1434}
1435
1436static const struct file_operations synclinkmp_proc_fops = {
1437 .owner = THIS_MODULE,
1438 .open = synclinkmp_proc_open,
1439 .read = seq_read,
1440 .llseek = seq_lseek,
1441 .release = single_release,
1442};
1443
1444/* Return the count of bytes in transmit buffer
1445 */
1446static int chars_in_buffer(struct tty_struct *tty)
1447{
1448 SLMP_INFO *info = tty->driver_data;
1449
1450 if (sanity_check(info, tty->name, "chars_in_buffer"))
1451 return 0;
1452
1453 if (debug_level >= DEBUG_LEVEL_INFO)
1454 printk("%s(%d):%s chars_in_buffer()=%d\n",
1455 __FILE__, __LINE__, info->device_name, info->tx_count);
1456
1457 return info->tx_count;
1458}
1459
1460/* Signal remote device to throttle send data (our receive data)
1461 */
1462static void throttle(struct tty_struct * tty)
1463{
1464 SLMP_INFO *info = tty->driver_data;
1465 unsigned long flags;
1466
1467 if (debug_level >= DEBUG_LEVEL_INFO)
1468 printk("%s(%d):%s throttle() entry\n",
1469 __FILE__,__LINE__, info->device_name );
1470
1471 if (sanity_check(info, tty->name, "throttle"))
1472 return;
1473
1474 if (I_IXOFF(tty))
1475 send_xchar(tty, STOP_CHAR(tty));
1476
1477 if (tty->termios->c_cflag & CRTSCTS) {
1478 spin_lock_irqsave(&info->lock,flags);
1479 info->serial_signals &= ~SerialSignal_RTS;
1480 set_signals(info);
1481 spin_unlock_irqrestore(&info->lock,flags);
1482 }
1483}
1484
1485/* Signal remote device to stop throttling send data (our receive data)
1486 */
1487static void unthrottle(struct tty_struct * tty)
1488{
1489 SLMP_INFO *info = tty->driver_data;
1490 unsigned long flags;
1491
1492 if (debug_level >= DEBUG_LEVEL_INFO)
1493 printk("%s(%d):%s unthrottle() entry\n",
1494 __FILE__,__LINE__, info->device_name );
1495
1496 if (sanity_check(info, tty->name, "unthrottle"))
1497 return;
1498
1499 if (I_IXOFF(tty)) {
1500 if (info->x_char)
1501 info->x_char = 0;
1502 else
1503 send_xchar(tty, START_CHAR(tty));
1504 }
1505
1506 if (tty->termios->c_cflag & CRTSCTS) {
1507 spin_lock_irqsave(&info->lock,flags);
1508 info->serial_signals |= SerialSignal_RTS;
1509 set_signals(info);
1510 spin_unlock_irqrestore(&info->lock,flags);
1511 }
1512}
1513
1514/* set or clear transmit break condition
1515 * break_state -1=set break condition, 0=clear
1516 */
1517static int set_break(struct tty_struct *tty, int break_state)
1518{
1519 unsigned char RegValue;
1520 SLMP_INFO * info = tty->driver_data;
1521 unsigned long flags;
1522
1523 if (debug_level >= DEBUG_LEVEL_INFO)
1524 printk("%s(%d):%s set_break(%d)\n",
1525 __FILE__,__LINE__, info->device_name, break_state);
1526
1527 if (sanity_check(info, tty->name, "set_break"))
1528 return -EINVAL;
1529
1530 spin_lock_irqsave(&info->lock,flags);
1531 RegValue = read_reg(info, CTL);
1532 if (break_state == -1)
1533 RegValue |= BIT3;
1534 else
1535 RegValue &= ~BIT3;
1536 write_reg(info, CTL, RegValue);
1537 spin_unlock_irqrestore(&info->lock,flags);
1538 return 0;
1539}
1540
1541#if SYNCLINK_GENERIC_HDLC
1542
1543/**
1544 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1545 * set encoding and frame check sequence (FCS) options
1546 *
1547 * dev pointer to network device structure
1548 * encoding serial encoding setting
1549 * parity FCS setting
1550 *
1551 * returns 0 if success, otherwise error code
1552 */
1553static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1554 unsigned short parity)
1555{
1556 SLMP_INFO *info = dev_to_port(dev);
1557 unsigned char new_encoding;
1558 unsigned short new_crctype;
1559
1560 /* return error if TTY interface open */
1561 if (info->port.count)
1562 return -EBUSY;
1563
1564 switch (encoding)
1565 {
1566 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1567 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1568 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1569 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1570 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1571 default: return -EINVAL;
1572 }
1573
1574 switch (parity)
1575 {
1576 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1577 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1578 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1579 default: return -EINVAL;
1580 }
1581
1582 info->params.encoding = new_encoding;
1583 info->params.crc_type = new_crctype;
1584
1585 /* if network interface up, reprogram hardware */
1586 if (info->netcount)
1587 program_hw(info);
1588
1589 return 0;
1590}
1591
1592/**
1593 * called by generic HDLC layer to send frame
1594 *
1595 * skb socket buffer containing HDLC frame
1596 * dev pointer to network device structure
1597 */
1598static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
1599 struct net_device *dev)
1600{
1601 SLMP_INFO *info = dev_to_port(dev);
1602 unsigned long flags;
1603
1604 if (debug_level >= DEBUG_LEVEL_INFO)
1605 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
1606
1607 /* stop sending until this frame completes */
1608 netif_stop_queue(dev);
1609
1610 /* copy data to device buffers */
1611 info->tx_count = skb->len;
1612 tx_load_dma_buffer(info, skb->data, skb->len);
1613
1614 /* update network statistics */
1615 dev->stats.tx_packets++;
1616 dev->stats.tx_bytes += skb->len;
1617
1618 /* done with socket buffer, so free it */
1619 dev_kfree_skb(skb);
1620
1621 /* save start time for transmit timeout detection */
1622 dev->trans_start = jiffies;
1623
1624 /* start hardware transmitter if necessary */
1625 spin_lock_irqsave(&info->lock,flags);
1626 if (!info->tx_active)
1627 tx_start(info);
1628 spin_unlock_irqrestore(&info->lock,flags);
1629
1630 return NETDEV_TX_OK;
1631}
1632
1633/**
1634 * called by network layer when interface enabled
1635 * claim resources and initialize hardware
1636 *
1637 * dev pointer to network device structure
1638 *
1639 * returns 0 if success, otherwise error code
1640 */
1641static int hdlcdev_open(struct net_device *dev)
1642{
1643 SLMP_INFO *info = dev_to_port(dev);
1644 int rc;
1645 unsigned long flags;
1646
1647 if (debug_level >= DEBUG_LEVEL_INFO)
1648 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
1649
1650 /* generic HDLC layer open processing */
1651 if ((rc = hdlc_open(dev)))
1652 return rc;
1653
1654 /* arbitrate between network and tty opens */
1655 spin_lock_irqsave(&info->netlock, flags);
1656 if (info->port.count != 0 || info->netcount != 0) {
1657 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
1658 spin_unlock_irqrestore(&info->netlock, flags);
1659 return -EBUSY;
1660 }
1661 info->netcount=1;
1662 spin_unlock_irqrestore(&info->netlock, flags);
1663
1664 /* claim resources and init adapter */
1665 if ((rc = startup(info)) != 0) {
1666 spin_lock_irqsave(&info->netlock, flags);
1667 info->netcount=0;
1668 spin_unlock_irqrestore(&info->netlock, flags);
1669 return rc;
1670 }
1671
1672 /* assert DTR and RTS, apply hardware settings */
1673 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1674 program_hw(info);
1675
1676 /* enable network layer transmit */
1677 dev->trans_start = jiffies;
1678 netif_start_queue(dev);
1679
1680 /* inform generic HDLC layer of current DCD status */
1681 spin_lock_irqsave(&info->lock, flags);
1682 get_signals(info);
1683 spin_unlock_irqrestore(&info->lock, flags);
1684 if (info->serial_signals & SerialSignal_DCD)
1685 netif_carrier_on(dev);
1686 else
1687 netif_carrier_off(dev);
1688 return 0;
1689}
1690
1691/**
1692 * called by network layer when interface is disabled
1693 * shutdown hardware and release resources
1694 *
1695 * dev pointer to network device structure
1696 *
1697 * returns 0 if success, otherwise error code
1698 */
1699static int hdlcdev_close(struct net_device *dev)
1700{
1701 SLMP_INFO *info = dev_to_port(dev);
1702 unsigned long flags;
1703
1704 if (debug_level >= DEBUG_LEVEL_INFO)
1705 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
1706
1707 netif_stop_queue(dev);
1708
1709 /* shutdown adapter and release resources */
1710 shutdown(info);
1711
1712 hdlc_close(dev);
1713
1714 spin_lock_irqsave(&info->netlock, flags);
1715 info->netcount=0;
1716 spin_unlock_irqrestore(&info->netlock, flags);
1717
1718 return 0;
1719}
1720
1721/**
1722 * called by network layer to process IOCTL call to network device
1723 *
1724 * dev pointer to network device structure
1725 * ifr pointer to network interface request structure
1726 * cmd IOCTL command code
1727 *
1728 * returns 0 if success, otherwise error code
1729 */
1730static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1731{
1732 const size_t size = sizeof(sync_serial_settings);
1733 sync_serial_settings new_line;
1734 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1735 SLMP_INFO *info = dev_to_port(dev);
1736 unsigned int flags;
1737
1738 if (debug_level >= DEBUG_LEVEL_INFO)
1739 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
1740
1741 /* return error if TTY interface open */
1742 if (info->port.count)
1743 return -EBUSY;
1744
1745 if (cmd != SIOCWANDEV)
1746 return hdlc_ioctl(dev, ifr, cmd);
1747
1748 switch(ifr->ifr_settings.type) {
1749 case IF_GET_IFACE: /* return current sync_serial_settings */
1750
1751 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1752 if (ifr->ifr_settings.size < size) {
1753 ifr->ifr_settings.size = size; /* data size wanted */
1754 return -ENOBUFS;
1755 }
1756
1757 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1758 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1759 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1760 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1761
1762 switch (flags){
1763 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1764 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1765 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1766 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1767 default: new_line.clock_type = CLOCK_DEFAULT;
1768 }
1769
1770 new_line.clock_rate = info->params.clock_speed;
1771 new_line.loopback = info->params.loopback ? 1:0;
1772
1773 if (copy_to_user(line, &new_line, size))
1774 return -EFAULT;
1775 return 0;
1776
1777 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1778
1779 if(!capable(CAP_NET_ADMIN))
1780 return -EPERM;
1781 if (copy_from_user(&new_line, line, size))
1782 return -EFAULT;
1783
1784 switch (new_line.clock_type)
1785 {
1786 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1787 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1788 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1789 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1790 case CLOCK_DEFAULT: flags = info->params.flags &
1791 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1792 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1793 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1794 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1795 default: return -EINVAL;
1796 }
1797
1798 if (new_line.loopback != 0 && new_line.loopback != 1)
1799 return -EINVAL;
1800
1801 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1802 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1803 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1804 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1805 info->params.flags |= flags;
1806
1807 info->params.loopback = new_line.loopback;
1808
1809 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1810 info->params.clock_speed = new_line.clock_rate;
1811 else
1812 info->params.clock_speed = 0;
1813
1814 /* if network interface up, reprogram hardware */
1815 if (info->netcount)
1816 program_hw(info);
1817 return 0;
1818
1819 default:
1820 return hdlc_ioctl(dev, ifr, cmd);
1821 }
1822}
1823
1824/**
1825 * called by network layer when transmit timeout is detected
1826 *
1827 * dev pointer to network device structure
1828 */
1829static void hdlcdev_tx_timeout(struct net_device *dev)
1830{
1831 SLMP_INFO *info = dev_to_port(dev);
1832 unsigned long flags;
1833
1834 if (debug_level >= DEBUG_LEVEL_INFO)
1835 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
1836
1837 dev->stats.tx_errors++;
1838 dev->stats.tx_aborted_errors++;
1839
1840 spin_lock_irqsave(&info->lock,flags);
1841 tx_stop(info);
1842 spin_unlock_irqrestore(&info->lock,flags);
1843
1844 netif_wake_queue(dev);
1845}
1846
1847/**
1848 * called by device driver when transmit completes
1849 * reenable network layer transmit if stopped
1850 *
1851 * info pointer to device instance information
1852 */
1853static void hdlcdev_tx_done(SLMP_INFO *info)
1854{
1855 if (netif_queue_stopped(info->netdev))
1856 netif_wake_queue(info->netdev);
1857}
1858
1859/**
1860 * called by device driver when frame received
1861 * pass frame to network layer
1862 *
1863 * info pointer to device instance information
1864 * buf pointer to buffer contianing frame data
1865 * size count of data bytes in buf
1866 */
1867static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size)
1868{
1869 struct sk_buff *skb = dev_alloc_skb(size);
1870 struct net_device *dev = info->netdev;
1871
1872 if (debug_level >= DEBUG_LEVEL_INFO)
1873 printk("hdlcdev_rx(%s)\n",dev->name);
1874
1875 if (skb == NULL) {
1876 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
1877 dev->name);
1878 dev->stats.rx_dropped++;
1879 return;
1880 }
1881
1882 memcpy(skb_put(skb, size), buf, size);
1883
1884 skb->protocol = hdlc_type_trans(skb, dev);
1885
1886 dev->stats.rx_packets++;
1887 dev->stats.rx_bytes += size;
1888
1889 netif_rx(skb);
1890}
1891
1892static const struct net_device_ops hdlcdev_ops = {
1893 .ndo_open = hdlcdev_open,
1894 .ndo_stop = hdlcdev_close,
1895 .ndo_change_mtu = hdlc_change_mtu,
1896 .ndo_start_xmit = hdlc_start_xmit,
1897 .ndo_do_ioctl = hdlcdev_ioctl,
1898 .ndo_tx_timeout = hdlcdev_tx_timeout,
1899};
1900
1901/**
1902 * called by device driver when adding device instance
1903 * do generic HDLC initialization
1904 *
1905 * info pointer to device instance information
1906 *
1907 * returns 0 if success, otherwise error code
1908 */
1909static int hdlcdev_init(SLMP_INFO *info)
1910{
1911 int rc;
1912 struct net_device *dev;
1913 hdlc_device *hdlc;
1914
1915 /* allocate and initialize network and HDLC layer objects */
1916
1917 if (!(dev = alloc_hdlcdev(info))) {
1918 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
1919 return -ENOMEM;
1920 }
1921
1922 /* for network layer reporting purposes only */
1923 dev->mem_start = info->phys_sca_base;
1924 dev->mem_end = info->phys_sca_base + SCA_BASE_SIZE - 1;
1925 dev->irq = info->irq_level;
1926
1927 /* network layer callbacks and settings */
1928 dev->netdev_ops = &hdlcdev_ops;
1929 dev->watchdog_timeo = 10 * HZ;
1930 dev->tx_queue_len = 50;
1931
1932 /* generic HDLC layer callbacks and settings */
1933 hdlc = dev_to_hdlc(dev);
1934 hdlc->attach = hdlcdev_attach;
1935 hdlc->xmit = hdlcdev_xmit;
1936
1937 /* register objects with HDLC layer */
1938 if ((rc = register_hdlc_device(dev))) {
1939 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1940 free_netdev(dev);
1941 return rc;
1942 }
1943
1944 info->netdev = dev;
1945 return 0;
1946}
1947
1948/**
1949 * called by device driver when removing device instance
1950 * do generic HDLC cleanup
1951 *
1952 * info pointer to device instance information
1953 */
1954static void hdlcdev_exit(SLMP_INFO *info)
1955{
1956 unregister_hdlc_device(info->netdev);
1957 free_netdev(info->netdev);
1958 info->netdev = NULL;
1959}
1960
1961#endif /* CONFIG_HDLC */
1962
1963
1964/* Return next bottom half action to perform.
1965 * Return Value: BH action code or 0 if nothing to do.
1966 */
1967static int bh_action(SLMP_INFO *info)
1968{
1969 unsigned long flags;
1970 int rc = 0;
1971
1972 spin_lock_irqsave(&info->lock,flags);
1973
1974 if (info->pending_bh & BH_RECEIVE) {
1975 info->pending_bh &= ~BH_RECEIVE;
1976 rc = BH_RECEIVE;
1977 } else if (info->pending_bh & BH_TRANSMIT) {
1978 info->pending_bh &= ~BH_TRANSMIT;
1979 rc = BH_TRANSMIT;
1980 } else if (info->pending_bh & BH_STATUS) {
1981 info->pending_bh &= ~BH_STATUS;
1982 rc = BH_STATUS;
1983 }
1984
1985 if (!rc) {
1986 /* Mark BH routine as complete */
1987 info->bh_running = false;
1988 info->bh_requested = false;
1989 }
1990
1991 spin_unlock_irqrestore(&info->lock,flags);
1992
1993 return rc;
1994}
1995
1996/* Perform bottom half processing of work items queued by ISR.
1997 */
1998static void bh_handler(struct work_struct *work)
1999{
2000 SLMP_INFO *info = container_of(work, SLMP_INFO, task);
2001 int action;
2002
2003 if (!info)
2004 return;
2005
2006 if ( debug_level >= DEBUG_LEVEL_BH )
2007 printk( "%s(%d):%s bh_handler() entry\n",
2008 __FILE__,__LINE__,info->device_name);
2009
2010 info->bh_running = true;
2011
2012 while((action = bh_action(info)) != 0) {
2013
2014 /* Process work item */
2015 if ( debug_level >= DEBUG_LEVEL_BH )
2016 printk( "%s(%d):%s bh_handler() work item action=%d\n",
2017 __FILE__,__LINE__,info->device_name, action);
2018
2019 switch (action) {
2020
2021 case BH_RECEIVE:
2022 bh_receive(info);
2023 break;
2024 case BH_TRANSMIT:
2025 bh_transmit(info);
2026 break;
2027 case BH_STATUS:
2028 bh_status(info);
2029 break;
2030 default:
2031 /* unknown work item ID */
2032 printk("%s(%d):%s Unknown work item ID=%08X!\n",
2033 __FILE__,__LINE__,info->device_name,action);
2034 break;
2035 }
2036 }
2037
2038 if ( debug_level >= DEBUG_LEVEL_BH )
2039 printk( "%s(%d):%s bh_handler() exit\n",
2040 __FILE__,__LINE__,info->device_name);
2041}
2042
2043static void bh_receive(SLMP_INFO *info)
2044{
2045 if ( debug_level >= DEBUG_LEVEL_BH )
2046 printk( "%s(%d):%s bh_receive()\n",
2047 __FILE__,__LINE__,info->device_name);
2048
2049 while( rx_get_frame(info) );
2050}
2051
2052static void bh_transmit(SLMP_INFO *info)
2053{
2054 struct tty_struct *tty = info->port.tty;
2055
2056 if ( debug_level >= DEBUG_LEVEL_BH )
2057 printk( "%s(%d):%s bh_transmit() entry\n",
2058 __FILE__,__LINE__,info->device_name);
2059
2060 if (tty)
2061 tty_wakeup(tty);
2062}
2063
2064static void bh_status(SLMP_INFO *info)
2065{
2066 if ( debug_level >= DEBUG_LEVEL_BH )
2067 printk( "%s(%d):%s bh_status() entry\n",
2068 __FILE__,__LINE__,info->device_name);
2069
2070 info->ri_chkcount = 0;
2071 info->dsr_chkcount = 0;
2072 info->dcd_chkcount = 0;
2073 info->cts_chkcount = 0;
2074}
2075
2076static void isr_timer(SLMP_INFO * info)
2077{
2078 unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
2079
2080 /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
2081 write_reg(info, IER2, 0);
2082
2083 /* TMCS, Timer Control/Status Register
2084 *
2085 * 07 CMF, Compare match flag (read only) 1=match
2086 * 06 ECMI, CMF Interrupt Enable: 0=disabled
2087 * 05 Reserved, must be 0
2088 * 04 TME, Timer Enable
2089 * 03..00 Reserved, must be 0
2090 *
2091 * 0000 0000
2092 */
2093 write_reg(info, (unsigned char)(timer + TMCS), 0);
2094
2095 info->irq_occurred = true;
2096
2097 if ( debug_level >= DEBUG_LEVEL_ISR )
2098 printk("%s(%d):%s isr_timer()\n",
2099 __FILE__,__LINE__,info->device_name);
2100}
2101
2102static void isr_rxint(SLMP_INFO * info)
2103{
2104 struct tty_struct *tty = info->port.tty;
2105 struct mgsl_icount *icount = &info->icount;
2106 unsigned char status = read_reg(info, SR1) & info->ie1_value & (FLGD + IDLD + CDCD + BRKD);
2107 unsigned char status2 = read_reg(info, SR2) & info->ie2_value & OVRN;
2108
2109 /* clear status bits */
2110 if (status)
2111 write_reg(info, SR1, status);
2112
2113 if (status2)
2114 write_reg(info, SR2, status2);
2115
2116 if ( debug_level >= DEBUG_LEVEL_ISR )
2117 printk("%s(%d):%s isr_rxint status=%02X %02x\n",
2118 __FILE__,__LINE__,info->device_name,status,status2);
2119
2120 if (info->params.mode == MGSL_MODE_ASYNC) {
2121 if (status & BRKD) {
2122 icount->brk++;
2123
2124 /* process break detection if tty control
2125 * is not set to ignore it
2126 */
2127 if ( tty ) {
2128 if (!(status & info->ignore_status_mask1)) {
2129 if (info->read_status_mask1 & BRKD) {
2130 tty_insert_flip_char(tty, 0, TTY_BREAK);
2131 if (info->port.flags & ASYNC_SAK)
2132 do_SAK(tty);
2133 }
2134 }
2135 }
2136 }
2137 }
2138 else {
2139 if (status & (FLGD|IDLD)) {
2140 if (status & FLGD)
2141 info->icount.exithunt++;
2142 else if (status & IDLD)
2143 info->icount.rxidle++;
2144 wake_up_interruptible(&info->event_wait_q);
2145 }
2146 }
2147
2148 if (status & CDCD) {
2149 /* simulate a common modem status change interrupt
2150 * for our handler
2151 */
2152 get_signals( info );
2153 isr_io_pin(info,
2154 MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
2155 }
2156}
2157
2158/*
2159 * handle async rx data interrupts
2160 */
2161static void isr_rxrdy(SLMP_INFO * info)
2162{
2163 u16 status;
2164 unsigned char DataByte;
2165 struct tty_struct *tty = info->port.tty;
2166 struct mgsl_icount *icount = &info->icount;
2167
2168 if ( debug_level >= DEBUG_LEVEL_ISR )
2169 printk("%s(%d):%s isr_rxrdy\n",
2170 __FILE__,__LINE__,info->device_name);
2171
2172 while((status = read_reg(info,CST0)) & BIT0)
2173 {
2174 int flag = 0;
2175 bool over = false;
2176 DataByte = read_reg(info,TRB);
2177
2178 icount->rx++;
2179
2180 if ( status & (PE + FRME + OVRN) ) {
2181 printk("%s(%d):%s rxerr=%04X\n",
2182 __FILE__,__LINE__,info->device_name,status);
2183
2184 /* update error statistics */
2185 if (status & PE)
2186 icount->parity++;
2187 else if (status & FRME)
2188 icount->frame++;
2189 else if (status & OVRN)
2190 icount->overrun++;
2191
2192 /* discard char if tty control flags say so */
2193 if (status & info->ignore_status_mask2)
2194 continue;
2195
2196 status &= info->read_status_mask2;
2197
2198 if ( tty ) {
2199 if (status & PE)
2200 flag = TTY_PARITY;
2201 else if (status & FRME)
2202 flag = TTY_FRAME;
2203 if (status & OVRN) {
2204 /* Overrun is special, since it's
2205 * reported immediately, and doesn't
2206 * affect the current character
2207 */
2208 over = true;
2209 }
2210 }
2211 } /* end of if (error) */
2212
2213 if ( tty ) {
2214 tty_insert_flip_char(tty, DataByte, flag);
2215 if (over)
2216 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2217 }
2218 }
2219
2220 if ( debug_level >= DEBUG_LEVEL_ISR ) {
2221 printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
2222 __FILE__,__LINE__,info->device_name,
2223 icount->rx,icount->brk,icount->parity,
2224 icount->frame,icount->overrun);
2225 }
2226
2227 if ( tty )
2228 tty_flip_buffer_push(tty);
2229}
2230
2231static void isr_txeom(SLMP_INFO * info, unsigned char status)
2232{
2233 if ( debug_level >= DEBUG_LEVEL_ISR )
2234 printk("%s(%d):%s isr_txeom status=%02x\n",
2235 __FILE__,__LINE__,info->device_name,status);
2236
2237 write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
2238 write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2239 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2240
2241 if (status & UDRN) {
2242 write_reg(info, CMD, TXRESET);
2243 write_reg(info, CMD, TXENABLE);
2244 } else
2245 write_reg(info, CMD, TXBUFCLR);
2246
2247 /* disable and clear tx interrupts */
2248 info->ie0_value &= ~TXRDYE;
2249 info->ie1_value &= ~(IDLE + UDRN);
2250 write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
2251 write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
2252
2253 if ( info->tx_active ) {
2254 if (info->params.mode != MGSL_MODE_ASYNC) {
2255 if (status & UDRN)
2256 info->icount.txunder++;
2257 else if (status & IDLE)
2258 info->icount.txok++;
2259 }
2260
2261 info->tx_active = false;
2262 info->tx_count = info->tx_put = info->tx_get = 0;
2263
2264 del_timer(&info->tx_timer);
2265
2266 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
2267 info->serial_signals &= ~SerialSignal_RTS;
2268 info->drop_rts_on_tx_done = false;
2269 set_signals(info);
2270 }
2271
2272#if SYNCLINK_GENERIC_HDLC
2273 if (info->netcount)
2274 hdlcdev_tx_done(info);
2275 else
2276#endif
2277 {
2278 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2279 tx_stop(info);
2280 return;
2281 }
2282 info->pending_bh |= BH_TRANSMIT;
2283 }
2284 }
2285}
2286
2287
2288/*
2289 * handle tx status interrupts
2290 */
2291static void isr_txint(SLMP_INFO * info)
2292{
2293 unsigned char status = read_reg(info, SR1) & info->ie1_value & (UDRN + IDLE + CCTS);
2294
2295 /* clear status bits */
2296 write_reg(info, SR1, status);
2297
2298 if ( debug_level >= DEBUG_LEVEL_ISR )
2299 printk("%s(%d):%s isr_txint status=%02x\n",
2300 __FILE__,__LINE__,info->device_name,status);
2301
2302 if (status & (UDRN + IDLE))
2303 isr_txeom(info, status);
2304
2305 if (status & CCTS) {
2306 /* simulate a common modem status change interrupt
2307 * for our handler
2308 */
2309 get_signals( info );
2310 isr_io_pin(info,
2311 MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
2312
2313 }
2314}
2315
2316/*
2317 * handle async tx data interrupts
2318 */
2319static void isr_txrdy(SLMP_INFO * info)
2320{
2321 if ( debug_level >= DEBUG_LEVEL_ISR )
2322 printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
2323 __FILE__,__LINE__,info->device_name,info->tx_count);
2324
2325 if (info->params.mode != MGSL_MODE_ASYNC) {
2326 /* disable TXRDY IRQ, enable IDLE IRQ */
2327 info->ie0_value &= ~TXRDYE;
2328 info->ie1_value |= IDLE;
2329 write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
2330 return;
2331 }
2332
2333 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2334 tx_stop(info);
2335 return;
2336 }
2337
2338 if ( info->tx_count )
2339 tx_load_fifo( info );
2340 else {
2341 info->tx_active = false;
2342 info->ie0_value &= ~TXRDYE;
2343 write_reg(info, IE0, info->ie0_value);
2344 }
2345
2346 if (info->tx_count < WAKEUP_CHARS)
2347 info->pending_bh |= BH_TRANSMIT;
2348}
2349
2350static void isr_rxdmaok(SLMP_INFO * info)
2351{
2352 /* BIT7 = EOT (end of transfer)
2353 * BIT6 = EOM (end of message/frame)
2354 */
2355 unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
2356
2357 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2358 write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2359
2360 if ( debug_level >= DEBUG_LEVEL_ISR )
2361 printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
2362 __FILE__,__LINE__,info->device_name,status);
2363
2364 info->pending_bh |= BH_RECEIVE;
2365}
2366
2367static void isr_rxdmaerror(SLMP_INFO * info)
2368{
2369 /* BIT5 = BOF (buffer overflow)
2370 * BIT4 = COF (counter overflow)
2371 */
2372 unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
2373
2374 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2375 write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2376
2377 if ( debug_level >= DEBUG_LEVEL_ISR )
2378 printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
2379 __FILE__,__LINE__,info->device_name,status);
2380
2381 info->rx_overflow = true;
2382 info->pending_bh |= BH_RECEIVE;
2383}
2384
2385static void isr_txdmaok(SLMP_INFO * info)
2386{
2387 unsigned char status_reg1 = read_reg(info, SR1);
2388
2389 write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
2390 write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2391 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2392
2393 if ( debug_level >= DEBUG_LEVEL_ISR )
2394 printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
2395 __FILE__,__LINE__,info->device_name,status_reg1);
2396
2397 /* program TXRDY as FIFO empty flag, enable TXRDY IRQ */
2398 write_reg16(info, TRC0, 0);
2399 info->ie0_value |= TXRDYE;
2400 write_reg(info, IE0, info->ie0_value);
2401}
2402
2403static void isr_txdmaerror(SLMP_INFO * info)
2404{
2405 /* BIT5 = BOF (buffer overflow)
2406 * BIT4 = COF (counter overflow)
2407 */
2408 unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
2409
2410 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2411 write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
2412
2413 if ( debug_level >= DEBUG_LEVEL_ISR )
2414 printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
2415 __FILE__,__LINE__,info->device_name,status);
2416}
2417
2418/* handle input serial signal changes
2419 */
2420static void isr_io_pin( SLMP_INFO *info, u16 status )
2421{
2422 struct mgsl_icount *icount;
2423
2424 if ( debug_level >= DEBUG_LEVEL_ISR )
2425 printk("%s(%d):isr_io_pin status=%04X\n",
2426 __FILE__,__LINE__,status);
2427
2428 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
2429 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
2430 icount = &info->icount;
2431 /* update input line counters */
2432 if (status & MISCSTATUS_RI_LATCHED) {
2433 icount->rng++;
2434 if ( status & SerialSignal_RI )
2435 info->input_signal_events.ri_up++;
2436 else
2437 info->input_signal_events.ri_down++;
2438 }
2439 if (status & MISCSTATUS_DSR_LATCHED) {
2440 icount->dsr++;
2441 if ( status & SerialSignal_DSR )
2442 info->input_signal_events.dsr_up++;
2443 else
2444 info->input_signal_events.dsr_down++;
2445 }
2446 if (status & MISCSTATUS_DCD_LATCHED) {
2447 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2448 info->ie1_value &= ~CDCD;
2449 write_reg(info, IE1, info->ie1_value);
2450 }
2451 icount->dcd++;
2452 if (status & SerialSignal_DCD) {
2453 info->input_signal_events.dcd_up++;
2454 } else
2455 info->input_signal_events.dcd_down++;
2456#if SYNCLINK_GENERIC_HDLC
2457 if (info->netcount) {
2458 if (status & SerialSignal_DCD)
2459 netif_carrier_on(info->netdev);
2460 else
2461 netif_carrier_off(info->netdev);
2462 }
2463#endif
2464 }
2465 if (status & MISCSTATUS_CTS_LATCHED)
2466 {
2467 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2468 info->ie1_value &= ~CCTS;
2469 write_reg(info, IE1, info->ie1_value);
2470 }
2471 icount->cts++;
2472 if ( status & SerialSignal_CTS )
2473 info->input_signal_events.cts_up++;
2474 else
2475 info->input_signal_events.cts_down++;
2476 }
2477 wake_up_interruptible(&info->status_event_wait_q);
2478 wake_up_interruptible(&info->event_wait_q);
2479
2480 if ( (info->port.flags & ASYNC_CHECK_CD) &&
2481 (status & MISCSTATUS_DCD_LATCHED) ) {
2482 if ( debug_level >= DEBUG_LEVEL_ISR )
2483 printk("%s CD now %s...", info->device_name,
2484 (status & SerialSignal_DCD) ? "on" : "off");
2485 if (status & SerialSignal_DCD)
2486 wake_up_interruptible(&info->port.open_wait);
2487 else {
2488 if ( debug_level >= DEBUG_LEVEL_ISR )
2489 printk("doing serial hangup...");
2490 if (info->port.tty)
2491 tty_hangup(info->port.tty);
2492 }
2493 }
2494
2495 if ( (info->port.flags & ASYNC_CTS_FLOW) &&
2496 (status & MISCSTATUS_CTS_LATCHED) ) {
2497 if ( info->port.tty ) {
2498 if (info->port.tty->hw_stopped) {
2499 if (status & SerialSignal_CTS) {
2500 if ( debug_level >= DEBUG_LEVEL_ISR )
2501 printk("CTS tx start...");
2502 info->port.tty->hw_stopped = 0;
2503 tx_start(info);
2504 info->pending_bh |= BH_TRANSMIT;
2505 return;
2506 }
2507 } else {
2508 if (!(status & SerialSignal_CTS)) {
2509 if ( debug_level >= DEBUG_LEVEL_ISR )
2510 printk("CTS tx stop...");
2511 info->port.tty->hw_stopped = 1;
2512 tx_stop(info);
2513 }
2514 }
2515 }
2516 }
2517 }
2518
2519 info->pending_bh |= BH_STATUS;
2520}
2521
2522/* Interrupt service routine entry point.
2523 *
2524 * Arguments:
2525 * irq interrupt number that caused interrupt
2526 * dev_id device ID supplied during interrupt registration
2527 * regs interrupted processor context
2528 */
2529static irqreturn_t synclinkmp_interrupt(int dummy, void *dev_id)
2530{
2531 SLMP_INFO *info = dev_id;
2532 unsigned char status, status0, status1=0;
2533 unsigned char dmastatus, dmastatus0, dmastatus1=0;
2534 unsigned char timerstatus0, timerstatus1=0;
2535 unsigned char shift;
2536 unsigned int i;
2537 unsigned short tmp;
2538
2539 if ( debug_level >= DEBUG_LEVEL_ISR )
2540 printk(KERN_DEBUG "%s(%d): synclinkmp_interrupt(%d)entry.\n",
2541 __FILE__, __LINE__, info->irq_level);
2542
2543 spin_lock(&info->lock);
2544
2545 for(;;) {
2546
2547 /* get status for SCA0 (ports 0-1) */
2548 tmp = read_reg16(info, ISR0); /* get ISR0 and ISR1 in one read */
2549 status0 = (unsigned char)tmp;
2550 dmastatus0 = (unsigned char)(tmp>>8);
2551 timerstatus0 = read_reg(info, ISR2);
2552
2553 if ( debug_level >= DEBUG_LEVEL_ISR )
2554 printk(KERN_DEBUG "%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
2555 __FILE__, __LINE__, info->device_name,
2556 status0, dmastatus0, timerstatus0);
2557
2558 if (info->port_count == 4) {
2559 /* get status for SCA1 (ports 2-3) */
2560 tmp = read_reg16(info->port_array[2], ISR0);
2561 status1 = (unsigned char)tmp;
2562 dmastatus1 = (unsigned char)(tmp>>8);
2563 timerstatus1 = read_reg(info->port_array[2], ISR2);
2564
2565 if ( debug_level >= DEBUG_LEVEL_ISR )
2566 printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
2567 __FILE__,__LINE__,info->device_name,
2568 status1,dmastatus1,timerstatus1);
2569 }
2570
2571 if (!status0 && !dmastatus0 && !timerstatus0 &&
2572 !status1 && !dmastatus1 && !timerstatus1)
2573 break;
2574
2575 for(i=0; i < info->port_count ; i++) {
2576 if (info->port_array[i] == NULL)
2577 continue;
2578 if (i < 2) {
2579 status = status0;
2580 dmastatus = dmastatus0;
2581 } else {
2582 status = status1;
2583 dmastatus = dmastatus1;
2584 }
2585
2586 shift = i & 1 ? 4 :0;
2587
2588 if (status & BIT0 << shift)
2589 isr_rxrdy(info->port_array[i]);
2590 if (status & BIT1 << shift)
2591 isr_txrdy(info->port_array[i]);
2592 if (status & BIT2 << shift)
2593 isr_rxint(info->port_array[i]);
2594 if (status & BIT3 << shift)
2595 isr_txint(info->port_array[i]);
2596
2597 if (dmastatus & BIT0 << shift)
2598 isr_rxdmaerror(info->port_array[i]);
2599 if (dmastatus & BIT1 << shift)
2600 isr_rxdmaok(info->port_array[i]);
2601 if (dmastatus & BIT2 << shift)
2602 isr_txdmaerror(info->port_array[i]);
2603 if (dmastatus & BIT3 << shift)
2604 isr_txdmaok(info->port_array[i]);
2605 }
2606
2607 if (timerstatus0 & (BIT5 | BIT4))
2608 isr_timer(info->port_array[0]);
2609 if (timerstatus0 & (BIT7 | BIT6))
2610 isr_timer(info->port_array[1]);
2611 if (timerstatus1 & (BIT5 | BIT4))
2612 isr_timer(info->port_array[2]);
2613 if (timerstatus1 & (BIT7 | BIT6))
2614 isr_timer(info->port_array[3]);
2615 }
2616
2617 for(i=0; i < info->port_count ; i++) {
2618 SLMP_INFO * port = info->port_array[i];
2619
2620 /* Request bottom half processing if there's something
2621 * for it to do and the bh is not already running.
2622 *
2623 * Note: startup adapter diags require interrupts.
2624 * do not request bottom half processing if the
2625 * device is not open in a normal mode.
2626 */
2627 if ( port && (port->port.count || port->netcount) &&
2628 port->pending_bh && !port->bh_running &&
2629 !port->bh_requested ) {
2630 if ( debug_level >= DEBUG_LEVEL_ISR )
2631 printk("%s(%d):%s queueing bh task.\n",
2632 __FILE__,__LINE__,port->device_name);
2633 schedule_work(&port->task);
2634 port->bh_requested = true;
2635 }
2636 }
2637
2638 spin_unlock(&info->lock);
2639
2640 if ( debug_level >= DEBUG_LEVEL_ISR )
2641 printk(KERN_DEBUG "%s(%d):synclinkmp_interrupt(%d)exit.\n",
2642 __FILE__, __LINE__, info->irq_level);
2643 return IRQ_HANDLED;
2644}
2645
2646/* Initialize and start device.
2647 */
2648static int startup(SLMP_INFO * info)
2649{
2650 if ( debug_level >= DEBUG_LEVEL_INFO )
2651 printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
2652
2653 if (info->port.flags & ASYNC_INITIALIZED)
2654 return 0;
2655
2656 if (!info->tx_buf) {
2657 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2658 if (!info->tx_buf) {
2659 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
2660 __FILE__,__LINE__,info->device_name);
2661 return -ENOMEM;
2662 }
2663 }
2664
2665 info->pending_bh = 0;
2666
2667 memset(&info->icount, 0, sizeof(info->icount));
2668
2669 /* program hardware for current parameters */
2670 reset_port(info);
2671
2672 change_params(info);
2673
2674 mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
2675
2676 if (info->port.tty)
2677 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2678
2679 info->port.flags |= ASYNC_INITIALIZED;
2680
2681 return 0;
2682}
2683
2684/* Called by close() and hangup() to shutdown hardware
2685 */
2686static void shutdown(SLMP_INFO * info)
2687{
2688 unsigned long flags;
2689
2690 if (!(info->port.flags & ASYNC_INITIALIZED))
2691 return;
2692
2693 if (debug_level >= DEBUG_LEVEL_INFO)
2694 printk("%s(%d):%s synclinkmp_shutdown()\n",
2695 __FILE__,__LINE__, info->device_name );
2696
2697 /* clear status wait queue because status changes */
2698 /* can't happen after shutting down the hardware */
2699 wake_up_interruptible(&info->status_event_wait_q);
2700 wake_up_interruptible(&info->event_wait_q);
2701
2702 del_timer(&info->tx_timer);
2703 del_timer(&info->status_timer);
2704
2705 kfree(info->tx_buf);
2706 info->tx_buf = NULL;
2707
2708 spin_lock_irqsave(&info->lock,flags);
2709
2710 reset_port(info);
2711
2712 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2713 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2714 set_signals(info);
2715 }
2716
2717 spin_unlock_irqrestore(&info->lock,flags);
2718
2719 if (info->port.tty)
2720 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2721
2722 info->port.flags &= ~ASYNC_INITIALIZED;
2723}
2724
2725static void program_hw(SLMP_INFO *info)
2726{
2727 unsigned long flags;
2728
2729 spin_lock_irqsave(&info->lock,flags);
2730
2731 rx_stop(info);
2732 tx_stop(info);
2733
2734 info->tx_count = info->tx_put = info->tx_get = 0;
2735
2736 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
2737 hdlc_mode(info);
2738 else
2739 async_mode(info);
2740
2741 set_signals(info);
2742
2743 info->dcd_chkcount = 0;
2744 info->cts_chkcount = 0;
2745 info->ri_chkcount = 0;
2746 info->dsr_chkcount = 0;
2747
2748 info->ie1_value |= (CDCD|CCTS);
2749 write_reg(info, IE1, info->ie1_value);
2750
2751 get_signals(info);
2752
2753 if (info->netcount || (info->port.tty && info->port.tty->termios->c_cflag & CREAD) )
2754 rx_start(info);
2755
2756 spin_unlock_irqrestore(&info->lock,flags);
2757}
2758
2759/* Reconfigure adapter based on new parameters
2760 */
2761static void change_params(SLMP_INFO *info)
2762{
2763 unsigned cflag;
2764 int bits_per_char;
2765
2766 if (!info->port.tty || !info->port.tty->termios)
2767 return;
2768
2769 if (debug_level >= DEBUG_LEVEL_INFO)
2770 printk("%s(%d):%s change_params()\n",
2771 __FILE__,__LINE__, info->device_name );
2772
2773 cflag = info->port.tty->termios->c_cflag;
2774
2775 /* if B0 rate (hangup) specified then negate DTR and RTS */
2776 /* otherwise assert DTR and RTS */
2777 if (cflag & CBAUD)
2778 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2779 else
2780 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2781
2782 /* byte size and parity */
2783
2784 switch (cflag & CSIZE) {
2785 case CS5: info->params.data_bits = 5; break;
2786 case CS6: info->params.data_bits = 6; break;
2787 case CS7: info->params.data_bits = 7; break;
2788 case CS8: info->params.data_bits = 8; break;
2789 /* Never happens, but GCC is too dumb to figure it out */
2790 default: info->params.data_bits = 7; break;
2791 }
2792
2793 if (cflag & CSTOPB)
2794 info->params.stop_bits = 2;
2795 else
2796 info->params.stop_bits = 1;
2797
2798 info->params.parity = ASYNC_PARITY_NONE;
2799 if (cflag & PARENB) {
2800 if (cflag & PARODD)
2801 info->params.parity = ASYNC_PARITY_ODD;
2802 else
2803 info->params.parity = ASYNC_PARITY_EVEN;
2804#ifdef CMSPAR
2805 if (cflag & CMSPAR)
2806 info->params.parity = ASYNC_PARITY_SPACE;
2807#endif
2808 }
2809
2810 /* calculate number of jiffies to transmit a full
2811 * FIFO (32 bytes) at specified data rate
2812 */
2813 bits_per_char = info->params.data_bits +
2814 info->params.stop_bits + 1;
2815
2816 /* if port data rate is set to 460800 or less then
2817 * allow tty settings to override, otherwise keep the
2818 * current data rate.
2819 */
2820 if (info->params.data_rate <= 460800) {
2821 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2822 }
2823
2824 if ( info->params.data_rate ) {
2825 info->timeout = (32*HZ*bits_per_char) /
2826 info->params.data_rate;
2827 }
2828 info->timeout += HZ/50; /* Add .02 seconds of slop */
2829
2830 if (cflag & CRTSCTS)
2831 info->port.flags |= ASYNC_CTS_FLOW;
2832 else
2833 info->port.flags &= ~ASYNC_CTS_FLOW;
2834
2835 if (cflag & CLOCAL)
2836 info->port.flags &= ~ASYNC_CHECK_CD;
2837 else
2838 info->port.flags |= ASYNC_CHECK_CD;
2839
2840 /* process tty input control flags */
2841
2842 info->read_status_mask2 = OVRN;
2843 if (I_INPCK(info->port.tty))
2844 info->read_status_mask2 |= PE | FRME;
2845 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2846 info->read_status_mask1 |= BRKD;
2847 if (I_IGNPAR(info->port.tty))
2848 info->ignore_status_mask2 |= PE | FRME;
2849 if (I_IGNBRK(info->port.tty)) {
2850 info->ignore_status_mask1 |= BRKD;
2851 /* If ignoring parity and break indicators, ignore
2852 * overruns too. (For real raw support).
2853 */
2854 if (I_IGNPAR(info->port.tty))
2855 info->ignore_status_mask2 |= OVRN;
2856 }
2857
2858 program_hw(info);
2859}
2860
2861static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
2862{
2863 int err;
2864
2865 if (debug_level >= DEBUG_LEVEL_INFO)
2866 printk("%s(%d):%s get_params()\n",
2867 __FILE__,__LINE__, info->device_name);
2868
2869 if (!user_icount) {
2870 memset(&info->icount, 0, sizeof(info->icount));
2871 } else {
2872 mutex_lock(&info->port.mutex);
2873 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2874 mutex_unlock(&info->port.mutex);
2875 if (err)
2876 return -EFAULT;
2877 }
2878
2879 return 0;
2880}
2881
2882static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
2883{
2884 int err;
2885 if (debug_level >= DEBUG_LEVEL_INFO)
2886 printk("%s(%d):%s get_params()\n",
2887 __FILE__,__LINE__, info->device_name);
2888
2889 mutex_lock(&info->port.mutex);
2890 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2891 mutex_unlock(&info->port.mutex);
2892 if (err) {
2893 if ( debug_level >= DEBUG_LEVEL_INFO )
2894 printk( "%s(%d):%s get_params() user buffer copy failed\n",
2895 __FILE__,__LINE__,info->device_name);
2896 return -EFAULT;
2897 }
2898
2899 return 0;
2900}
2901
2902static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
2903{
2904 unsigned long flags;
2905 MGSL_PARAMS tmp_params;
2906 int err;
2907
2908 if (debug_level >= DEBUG_LEVEL_INFO)
2909 printk("%s(%d):%s set_params\n",
2910 __FILE__,__LINE__,info->device_name );
2911 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2912 if (err) {
2913 if ( debug_level >= DEBUG_LEVEL_INFO )
2914 printk( "%s(%d):%s set_params() user buffer copy failed\n",
2915 __FILE__,__LINE__,info->device_name);
2916 return -EFAULT;
2917 }
2918
2919 mutex_lock(&info->port.mutex);
2920 spin_lock_irqsave(&info->lock,flags);
2921 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2922 spin_unlock_irqrestore(&info->lock,flags);
2923
2924 change_params(info);
2925 mutex_unlock(&info->port.mutex);
2926
2927 return 0;
2928}
2929
2930static int get_txidle(SLMP_INFO * info, int __user *idle_mode)
2931{
2932 int err;
2933
2934 if (debug_level >= DEBUG_LEVEL_INFO)
2935 printk("%s(%d):%s get_txidle()=%d\n",
2936 __FILE__,__LINE__, info->device_name, info->idle_mode);
2937
2938 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2939 if (err) {
2940 if ( debug_level >= DEBUG_LEVEL_INFO )
2941 printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
2942 __FILE__,__LINE__,info->device_name);
2943 return -EFAULT;
2944 }
2945
2946 return 0;
2947}
2948
2949static int set_txidle(SLMP_INFO * info, int idle_mode)
2950{
2951 unsigned long flags;
2952
2953 if (debug_level >= DEBUG_LEVEL_INFO)
2954 printk("%s(%d):%s set_txidle(%d)\n",
2955 __FILE__,__LINE__,info->device_name, idle_mode );
2956
2957 spin_lock_irqsave(&info->lock,flags);
2958 info->idle_mode = idle_mode;
2959 tx_set_idle( info );
2960 spin_unlock_irqrestore(&info->lock,flags);
2961 return 0;
2962}
2963
2964static int tx_enable(SLMP_INFO * info, int enable)
2965{
2966 unsigned long flags;
2967
2968 if (debug_level >= DEBUG_LEVEL_INFO)
2969 printk("%s(%d):%s tx_enable(%d)\n",
2970 __FILE__,__LINE__,info->device_name, enable);
2971
2972 spin_lock_irqsave(&info->lock,flags);
2973 if ( enable ) {
2974 if ( !info->tx_enabled ) {
2975 tx_start(info);
2976 }
2977 } else {
2978 if ( info->tx_enabled )
2979 tx_stop(info);
2980 }
2981 spin_unlock_irqrestore(&info->lock,flags);
2982 return 0;
2983}
2984
2985/* abort send HDLC frame
2986 */
2987static int tx_abort(SLMP_INFO * info)
2988{
2989 unsigned long flags;
2990
2991 if (debug_level >= DEBUG_LEVEL_INFO)
2992 printk("%s(%d):%s tx_abort()\n",
2993 __FILE__,__LINE__,info->device_name);
2994
2995 spin_lock_irqsave(&info->lock,flags);
2996 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
2997 info->ie1_value &= ~UDRN;
2998 info->ie1_value |= IDLE;
2999 write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
3000 write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
3001
3002 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
3003 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
3004
3005 write_reg(info, CMD, TXABORT);
3006 }
3007 spin_unlock_irqrestore(&info->lock,flags);
3008 return 0;
3009}
3010
3011static int rx_enable(SLMP_INFO * info, int enable)
3012{
3013 unsigned long flags;
3014
3015 if (debug_level >= DEBUG_LEVEL_INFO)
3016 printk("%s(%d):%s rx_enable(%d)\n",
3017 __FILE__,__LINE__,info->device_name,enable);
3018
3019 spin_lock_irqsave(&info->lock,flags);
3020 if ( enable ) {
3021 if ( !info->rx_enabled )
3022 rx_start(info);
3023 } else {
3024 if ( info->rx_enabled )
3025 rx_stop(info);
3026 }
3027 spin_unlock_irqrestore(&info->lock,flags);
3028 return 0;
3029}
3030
3031/* wait for specified event to occur
3032 */
3033static int wait_mgsl_event(SLMP_INFO * info, int __user *mask_ptr)
3034{
3035 unsigned long flags;
3036 int s;
3037 int rc=0;
3038 struct mgsl_icount cprev, cnow;
3039 int events;
3040 int mask;
3041 struct _input_signal_events oldsigs, newsigs;
3042 DECLARE_WAITQUEUE(wait, current);
3043
3044 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
3045 if (rc) {
3046 return -EFAULT;
3047 }
3048
3049 if (debug_level >= DEBUG_LEVEL_INFO)
3050 printk("%s(%d):%s wait_mgsl_event(%d)\n",
3051 __FILE__,__LINE__,info->device_name,mask);
3052
3053 spin_lock_irqsave(&info->lock,flags);
3054
3055 /* return immediately if state matches requested events */
3056 get_signals(info);
3057 s = info->serial_signals;
3058
3059 events = mask &
3060 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
3061 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
3062 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
3063 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
3064 if (events) {
3065 spin_unlock_irqrestore(&info->lock,flags);
3066 goto exit;
3067 }
3068
3069 /* save current irq counts */
3070 cprev = info->icount;
3071 oldsigs = info->input_signal_events;
3072
3073 /* enable hunt and idle irqs if needed */
3074 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
3075 unsigned char oldval = info->ie1_value;
3076 unsigned char newval = oldval +
3077 (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
3078 (mask & MgslEvent_IdleReceived ? IDLD:0);
3079 if ( oldval != newval ) {
3080 info->ie1_value = newval;
3081 write_reg(info, IE1, info->ie1_value);
3082 }
3083 }
3084
3085 set_current_state(TASK_INTERRUPTIBLE);
3086 add_wait_queue(&info->event_wait_q, &wait);
3087
3088 spin_unlock_irqrestore(&info->lock,flags);
3089
3090 for(;;) {
3091 schedule();
3092 if (signal_pending(current)) {
3093 rc = -ERESTARTSYS;
3094 break;
3095 }
3096
3097 /* get current irq counts */
3098 spin_lock_irqsave(&info->lock,flags);
3099 cnow = info->icount;
3100 newsigs = info->input_signal_events;
3101 set_current_state(TASK_INTERRUPTIBLE);
3102 spin_unlock_irqrestore(&info->lock,flags);
3103
3104 /* if no change, wait aborted for some reason */
3105 if (newsigs.dsr_up == oldsigs.dsr_up &&
3106 newsigs.dsr_down == oldsigs.dsr_down &&
3107 newsigs.dcd_up == oldsigs.dcd_up &&
3108 newsigs.dcd_down == oldsigs.dcd_down &&
3109 newsigs.cts_up == oldsigs.cts_up &&
3110 newsigs.cts_down == oldsigs.cts_down &&
3111 newsigs.ri_up == oldsigs.ri_up &&
3112 newsigs.ri_down == oldsigs.ri_down &&
3113 cnow.exithunt == cprev.exithunt &&
3114 cnow.rxidle == cprev.rxidle) {
3115 rc = -EIO;
3116 break;
3117 }
3118
3119 events = mask &
3120 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
3121 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
3122 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
3123 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
3124 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
3125 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
3126 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
3127 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
3128 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
3129 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
3130 if (events)
3131 break;
3132
3133 cprev = cnow;
3134 oldsigs = newsigs;
3135 }
3136
3137 remove_wait_queue(&info->event_wait_q, &wait);
3138 set_current_state(TASK_RUNNING);
3139
3140
3141 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
3142 spin_lock_irqsave(&info->lock,flags);
3143 if (!waitqueue_active(&info->event_wait_q)) {
3144 /* disable enable exit hunt mode/idle rcvd IRQs */
3145 info->ie1_value &= ~(FLGD|IDLD);
3146 write_reg(info, IE1, info->ie1_value);
3147 }
3148 spin_unlock_irqrestore(&info->lock,flags);
3149 }
3150exit:
3151 if ( rc == 0 )
3152 PUT_USER(rc, events, mask_ptr);
3153
3154 return rc;
3155}
3156
3157static int modem_input_wait(SLMP_INFO *info,int arg)
3158{
3159 unsigned long flags;
3160 int rc;
3161 struct mgsl_icount cprev, cnow;
3162 DECLARE_WAITQUEUE(wait, current);
3163
3164 /* save current irq counts */
3165 spin_lock_irqsave(&info->lock,flags);
3166 cprev = info->icount;
3167 add_wait_queue(&info->status_event_wait_q, &wait);
3168 set_current_state(TASK_INTERRUPTIBLE);
3169 spin_unlock_irqrestore(&info->lock,flags);
3170
3171 for(;;) {
3172 schedule();
3173 if (signal_pending(current)) {
3174 rc = -ERESTARTSYS;
3175 break;
3176 }
3177
3178 /* get new irq counts */
3179 spin_lock_irqsave(&info->lock,flags);
3180 cnow = info->icount;
3181 set_current_state(TASK_INTERRUPTIBLE);
3182 spin_unlock_irqrestore(&info->lock,flags);
3183
3184 /* if no change, wait aborted for some reason */
3185 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3186 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3187 rc = -EIO;
3188 break;
3189 }
3190
3191 /* check for change in caller specified modem input */
3192 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3193 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3194 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3195 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3196 rc = 0;
3197 break;
3198 }
3199
3200 cprev = cnow;
3201 }
3202 remove_wait_queue(&info->status_event_wait_q, &wait);
3203 set_current_state(TASK_RUNNING);
3204 return rc;
3205}
3206
3207/* return the state of the serial control and status signals
3208 */
3209static int tiocmget(struct tty_struct *tty)
3210{
3211 SLMP_INFO *info = tty->driver_data;
3212 unsigned int result;
3213 unsigned long flags;
3214
3215 spin_lock_irqsave(&info->lock,flags);
3216 get_signals(info);
3217 spin_unlock_irqrestore(&info->lock,flags);
3218
3219 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3220 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3221 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3222 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3223 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3224 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3225
3226 if (debug_level >= DEBUG_LEVEL_INFO)
3227 printk("%s(%d):%s tiocmget() value=%08X\n",
3228 __FILE__,__LINE__, info->device_name, result );
3229 return result;
3230}
3231
3232/* set modem control signals (DTR/RTS)
3233 */
3234static int tiocmset(struct tty_struct *tty,
3235 unsigned int set, unsigned int clear)
3236{
3237 SLMP_INFO *info = tty->driver_data;
3238 unsigned long flags;
3239
3240 if (debug_level >= DEBUG_LEVEL_INFO)
3241 printk("%s(%d):%s tiocmset(%x,%x)\n",
3242 __FILE__,__LINE__,info->device_name, set, clear);
3243
3244 if (set & TIOCM_RTS)
3245 info->serial_signals |= SerialSignal_RTS;
3246 if (set & TIOCM_DTR)
3247 info->serial_signals |= SerialSignal_DTR;
3248 if (clear & TIOCM_RTS)
3249 info->serial_signals &= ~SerialSignal_RTS;
3250 if (clear & TIOCM_DTR)
3251 info->serial_signals &= ~SerialSignal_DTR;
3252
3253 spin_lock_irqsave(&info->lock,flags);
3254 set_signals(info);
3255 spin_unlock_irqrestore(&info->lock,flags);
3256
3257 return 0;
3258}
3259
3260static int carrier_raised(struct tty_port *port)
3261{
3262 SLMP_INFO *info = container_of(port, SLMP_INFO, port);
3263 unsigned long flags;
3264
3265 spin_lock_irqsave(&info->lock,flags);
3266 get_signals(info);
3267 spin_unlock_irqrestore(&info->lock,flags);
3268
3269 return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
3270}
3271
3272static void dtr_rts(struct tty_port *port, int on)
3273{
3274 SLMP_INFO *info = container_of(port, SLMP_INFO, port);
3275 unsigned long flags;
3276
3277 spin_lock_irqsave(&info->lock,flags);
3278 if (on)
3279 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3280 else
3281 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3282 set_signals(info);
3283 spin_unlock_irqrestore(&info->lock,flags);
3284}
3285
3286/* Block the current process until the specified port is ready to open.
3287 */
3288static int block_til_ready(struct tty_struct *tty, struct file *filp,
3289 SLMP_INFO *info)
3290{
3291 DECLARE_WAITQUEUE(wait, current);
3292 int retval;
3293 bool do_clocal = false;
3294 bool extra_count = false;
3295 unsigned long flags;
3296 int cd;
3297 struct tty_port *port = &info->port;
3298
3299 if (debug_level >= DEBUG_LEVEL_INFO)
3300 printk("%s(%d):%s block_til_ready()\n",
3301 __FILE__,__LINE__, tty->driver->name );
3302
3303 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3304 /* nonblock mode is set or port is not enabled */
3305 /* just verify that callout device is not active */
3306 port->flags |= ASYNC_NORMAL_ACTIVE;
3307 return 0;
3308 }
3309
3310 if (tty->termios->c_cflag & CLOCAL)
3311 do_clocal = true;
3312
3313 /* Wait for carrier detect and the line to become
3314 * free (i.e., not in use by the callout). While we are in
3315 * this loop, port->count is dropped by one, so that
3316 * close() knows when to free things. We restore it upon
3317 * exit, either normal or abnormal.
3318 */
3319
3320 retval = 0;
3321 add_wait_queue(&port->open_wait, &wait);
3322
3323 if (debug_level >= DEBUG_LEVEL_INFO)
3324 printk("%s(%d):%s block_til_ready() before block, count=%d\n",
3325 __FILE__,__LINE__, tty->driver->name, port->count );
3326
3327 spin_lock_irqsave(&info->lock, flags);
3328 if (!tty_hung_up_p(filp)) {
3329 extra_count = true;
3330 port->count--;
3331 }
3332 spin_unlock_irqrestore(&info->lock, flags);
3333 port->blocked_open++;
3334
3335 while (1) {
3336 if (tty->termios->c_cflag & CBAUD)
3337 tty_port_raise_dtr_rts(port);
3338
3339 set_current_state(TASK_INTERRUPTIBLE);
3340
3341 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3342 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3343 -EAGAIN : -ERESTARTSYS;
3344 break;
3345 }
3346
3347 cd = tty_port_carrier_raised(port);
3348
3349 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd))
3350 break;
3351
3352 if (signal_pending(current)) {
3353 retval = -ERESTARTSYS;
3354 break;
3355 }
3356
3357 if (debug_level >= DEBUG_LEVEL_INFO)
3358 printk("%s(%d):%s block_til_ready() count=%d\n",
3359 __FILE__,__LINE__, tty->driver->name, port->count );
3360
3361 tty_unlock();
3362 schedule();
3363 tty_lock();
3364 }
3365
3366 set_current_state(TASK_RUNNING);
3367 remove_wait_queue(&port->open_wait, &wait);
3368
3369 if (extra_count)
3370 port->count++;
3371 port->blocked_open--;
3372
3373 if (debug_level >= DEBUG_LEVEL_INFO)
3374 printk("%s(%d):%s block_til_ready() after, count=%d\n",
3375 __FILE__,__LINE__, tty->driver->name, port->count );
3376
3377 if (!retval)
3378 port->flags |= ASYNC_NORMAL_ACTIVE;
3379
3380 return retval;
3381}
3382
3383static int alloc_dma_bufs(SLMP_INFO *info)
3384{
3385 unsigned short BuffersPerFrame;
3386 unsigned short BufferCount;
3387
3388 // Force allocation to start at 64K boundary for each port.
3389 // This is necessary because *all* buffer descriptors for a port
3390 // *must* be in the same 64K block. All descriptors on a port
3391 // share a common 'base' address (upper 8 bits of 24 bits) programmed
3392 // into the CBP register.
3393 info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
3394
3395 /* Calculate the number of DMA buffers necessary to hold the */
3396 /* largest allowable frame size. Note: If the max frame size is */
3397 /* not an even multiple of the DMA buffer size then we need to */
3398 /* round the buffer count per frame up one. */
3399
3400 BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
3401 if ( info->max_frame_size % SCABUFSIZE )
3402 BuffersPerFrame++;
3403
3404 /* calculate total number of data buffers (SCABUFSIZE) possible
3405 * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
3406 * for the descriptor list (BUFFERLISTSIZE).
3407 */
3408 BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
3409
3410 /* limit number of buffers to maximum amount of descriptors */
3411 if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
3412 BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
3413
3414 /* use enough buffers to transmit one max size frame */
3415 info->tx_buf_count = BuffersPerFrame + 1;
3416
3417 /* never use more than half the available buffers for transmit */
3418 if (info->tx_buf_count > (BufferCount/2))
3419 info->tx_buf_count = BufferCount/2;
3420
3421 if (info->tx_buf_count > SCAMAXDESC)
3422 info->tx_buf_count = SCAMAXDESC;
3423
3424 /* use remaining buffers for receive */
3425 info->rx_buf_count = BufferCount - info->tx_buf_count;
3426
3427 if (info->rx_buf_count > SCAMAXDESC)
3428 info->rx_buf_count = SCAMAXDESC;
3429
3430 if ( debug_level >= DEBUG_LEVEL_INFO )
3431 printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
3432 __FILE__,__LINE__, info->device_name,
3433 info->tx_buf_count,info->rx_buf_count);
3434
3435 if ( alloc_buf_list( info ) < 0 ||
3436 alloc_frame_bufs(info,
3437 info->rx_buf_list,
3438 info->rx_buf_list_ex,
3439 info->rx_buf_count) < 0 ||
3440 alloc_frame_bufs(info,
3441 info->tx_buf_list,
3442 info->tx_buf_list_ex,
3443 info->tx_buf_count) < 0 ||
3444 alloc_tmp_rx_buf(info) < 0 ) {
3445 printk("%s(%d):%s Can't allocate DMA buffer memory\n",
3446 __FILE__,__LINE__, info->device_name);
3447 return -ENOMEM;
3448 }
3449
3450 rx_reset_buffers( info );
3451
3452 return 0;
3453}
3454
3455/* Allocate DMA buffers for the transmit and receive descriptor lists.
3456 */
3457static int alloc_buf_list(SLMP_INFO *info)
3458{
3459 unsigned int i;
3460
3461 /* build list in adapter shared memory */
3462 info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
3463 info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
3464 info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
3465
3466 memset(info->buffer_list, 0, BUFFERLISTSIZE);
3467
3468 /* Save virtual address pointers to the receive and */
3469 /* transmit buffer lists. (Receive 1st). These pointers will */
3470 /* be used by the processor to access the lists. */
3471 info->rx_buf_list = (SCADESC *)info->buffer_list;
3472
3473 info->tx_buf_list = (SCADESC *)info->buffer_list;
3474 info->tx_buf_list += info->rx_buf_count;
3475
3476 /* Build links for circular buffer entry lists (tx and rx)
3477 *
3478 * Note: links are physical addresses read by the SCA device
3479 * to determine the next buffer entry to use.
3480 */
3481
3482 for ( i = 0; i < info->rx_buf_count; i++ ) {
3483 /* calculate and store physical address of this buffer entry */
3484 info->rx_buf_list_ex[i].phys_entry =
3485 info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
3486
3487 /* calculate and store physical address of */
3488 /* next entry in cirular list of entries */
3489 info->rx_buf_list[i].next = info->buffer_list_phys;
3490 if ( i < info->rx_buf_count - 1 )
3491 info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3492
3493 info->rx_buf_list[i].length = SCABUFSIZE;
3494 }
3495
3496 for ( i = 0; i < info->tx_buf_count; i++ ) {
3497 /* calculate and store physical address of this buffer entry */
3498 info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
3499 ((info->rx_buf_count + i) * sizeof(SCADESC));
3500
3501 /* calculate and store physical address of */
3502 /* next entry in cirular list of entries */
3503
3504 info->tx_buf_list[i].next = info->buffer_list_phys +
3505 info->rx_buf_count * sizeof(SCADESC);
3506
3507 if ( i < info->tx_buf_count - 1 )
3508 info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3509 }
3510
3511 return 0;
3512}
3513
3514/* Allocate the frame DMA buffers used by the specified buffer list.
3515 */
3516static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
3517{
3518 int i;
3519 unsigned long phys_addr;
3520
3521 for ( i = 0; i < count; i++ ) {
3522 buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
3523 phys_addr = info->port_array[0]->last_mem_alloc;
3524 info->port_array[0]->last_mem_alloc += SCABUFSIZE;
3525
3526 buf_list[i].buf_ptr = (unsigned short)phys_addr;
3527 buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
3528 }
3529
3530 return 0;
3531}
3532
3533static void free_dma_bufs(SLMP_INFO *info)
3534{
3535 info->buffer_list = NULL;
3536 info->rx_buf_list = NULL;
3537 info->tx_buf_list = NULL;
3538}
3539
3540/* allocate buffer large enough to hold max_frame_size.
3541 * This buffer is used to pass an assembled frame to the line discipline.
3542 */
3543static int alloc_tmp_rx_buf(SLMP_INFO *info)
3544{
3545 info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
3546 if (info->tmp_rx_buf == NULL)
3547 return -ENOMEM;
3548 return 0;
3549}
3550
3551static void free_tmp_rx_buf(SLMP_INFO *info)
3552{
3553 kfree(info->tmp_rx_buf);
3554 info->tmp_rx_buf = NULL;
3555}
3556
3557static int claim_resources(SLMP_INFO *info)
3558{
3559 if (request_mem_region(info->phys_memory_base,SCA_MEM_SIZE,"synclinkmp") == NULL) {
3560 printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
3561 __FILE__,__LINE__,info->device_name, info->phys_memory_base);
3562 info->init_error = DiagStatus_AddressConflict;
3563 goto errout;
3564 }
3565 else
3566 info->shared_mem_requested = true;
3567
3568 if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
3569 printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
3570 __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
3571 info->init_error = DiagStatus_AddressConflict;
3572 goto errout;
3573 }
3574 else
3575 info->lcr_mem_requested = true;
3576
3577 if (request_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE,"synclinkmp") == NULL) {
3578 printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
3579 __FILE__,__LINE__,info->device_name, info->phys_sca_base);
3580 info->init_error = DiagStatus_AddressConflict;
3581 goto errout;
3582 }
3583 else
3584 info->sca_base_requested = true;
3585
3586 if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE,"synclinkmp") == NULL) {
3587 printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
3588 __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
3589 info->init_error = DiagStatus_AddressConflict;
3590 goto errout;
3591 }
3592 else
3593 info->sca_statctrl_requested = true;
3594
3595 info->memory_base = ioremap_nocache(info->phys_memory_base,
3596 SCA_MEM_SIZE);
3597 if (!info->memory_base) {
3598 printk( "%s(%d):%s Can't map shared memory, MemAddr=%08X\n",
3599 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3600 info->init_error = DiagStatus_CantAssignPciResources;
3601 goto errout;
3602 }
3603
3604 info->lcr_base = ioremap_nocache(info->phys_lcr_base, PAGE_SIZE);
3605 if (!info->lcr_base) {
3606 printk( "%s(%d):%s Can't map LCR memory, MemAddr=%08X\n",
3607 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
3608 info->init_error = DiagStatus_CantAssignPciResources;
3609 goto errout;
3610 }
3611 info->lcr_base += info->lcr_offset;
3612
3613 info->sca_base = ioremap_nocache(info->phys_sca_base, PAGE_SIZE);
3614 if (!info->sca_base) {
3615 printk( "%s(%d):%s Can't map SCA memory, MemAddr=%08X\n",
3616 __FILE__,__LINE__,info->device_name, info->phys_sca_base );
3617 info->init_error = DiagStatus_CantAssignPciResources;
3618 goto errout;
3619 }
3620 info->sca_base += info->sca_offset;
3621
3622 info->statctrl_base = ioremap_nocache(info->phys_statctrl_base,
3623 PAGE_SIZE);
3624 if (!info->statctrl_base) {
3625 printk( "%s(%d):%s Can't map SCA Status/Control memory, MemAddr=%08X\n",
3626 __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
3627 info->init_error = DiagStatus_CantAssignPciResources;
3628 goto errout;
3629 }
3630 info->statctrl_base += info->statctrl_offset;
3631
3632 if ( !memory_test(info) ) {
3633 printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
3634 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3635 info->init_error = DiagStatus_MemoryError;
3636 goto errout;
3637 }
3638
3639 return 0;
3640
3641errout:
3642 release_resources( info );
3643 return -ENODEV;
3644}
3645
3646static void release_resources(SLMP_INFO *info)
3647{
3648 if ( debug_level >= DEBUG_LEVEL_INFO )
3649 printk( "%s(%d):%s release_resources() entry\n",
3650 __FILE__,__LINE__,info->device_name );
3651
3652 if ( info->irq_requested ) {
3653 free_irq(info->irq_level, info);
3654 info->irq_requested = false;
3655 }
3656
3657 if ( info->shared_mem_requested ) {
3658 release_mem_region(info->phys_memory_base,SCA_MEM_SIZE);
3659 info->shared_mem_requested = false;
3660 }
3661 if ( info->lcr_mem_requested ) {
3662 release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
3663 info->lcr_mem_requested = false;
3664 }
3665 if ( info->sca_base_requested ) {
3666 release_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE);
3667 info->sca_base_requested = false;
3668 }
3669 if ( info->sca_statctrl_requested ) {
3670 release_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE);
3671 info->sca_statctrl_requested = false;
3672 }
3673
3674 if (info->memory_base){
3675 iounmap(info->memory_base);
3676 info->memory_base = NULL;
3677 }
3678
3679 if (info->sca_base) {
3680 iounmap(info->sca_base - info->sca_offset);
3681 info->sca_base=NULL;
3682 }
3683
3684 if (info->statctrl_base) {
3685 iounmap(info->statctrl_base - info->statctrl_offset);
3686 info->statctrl_base=NULL;
3687 }
3688
3689 if (info->lcr_base){
3690 iounmap(info->lcr_base - info->lcr_offset);
3691 info->lcr_base = NULL;
3692 }
3693
3694 if ( debug_level >= DEBUG_LEVEL_INFO )
3695 printk( "%s(%d):%s release_resources() exit\n",
3696 __FILE__,__LINE__,info->device_name );
3697}
3698
3699/* Add the specified device instance data structure to the
3700 * global linked list of devices and increment the device count.
3701 */
3702static void add_device(SLMP_INFO *info)
3703{
3704 info->next_device = NULL;
3705 info->line = synclinkmp_device_count;
3706 sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
3707
3708 if (info->line < MAX_DEVICES) {
3709 if (maxframe[info->line])
3710 info->max_frame_size = maxframe[info->line];
3711 }
3712
3713 synclinkmp_device_count++;
3714
3715 if ( !synclinkmp_device_list )
3716 synclinkmp_device_list = info;
3717 else {
3718 SLMP_INFO *current_dev = synclinkmp_device_list;
3719 while( current_dev->next_device )
3720 current_dev = current_dev->next_device;
3721 current_dev->next_device = info;
3722 }
3723
3724 if ( info->max_frame_size < 4096 )
3725 info->max_frame_size = 4096;
3726 else if ( info->max_frame_size > 65535 )
3727 info->max_frame_size = 65535;
3728
3729 printk( "SyncLink MultiPort %s: "
3730 "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
3731 info->device_name,
3732 info->phys_sca_base,
3733 info->phys_memory_base,
3734 info->phys_statctrl_base,
3735 info->phys_lcr_base,
3736 info->irq_level,
3737 info->max_frame_size );
3738
3739#if SYNCLINK_GENERIC_HDLC
3740 hdlcdev_init(info);
3741#endif
3742}
3743
3744static const struct tty_port_operations port_ops = {
3745 .carrier_raised = carrier_raised,
3746 .dtr_rts = dtr_rts,
3747};
3748
3749/* Allocate and initialize a device instance structure
3750 *
3751 * Return Value: pointer to SLMP_INFO if success, otherwise NULL
3752 */
3753static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3754{
3755 SLMP_INFO *info;
3756
3757 info = kzalloc(sizeof(SLMP_INFO),
3758 GFP_KERNEL);
3759
3760 if (!info) {
3761 printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
3762 __FILE__,__LINE__, adapter_num, port_num);
3763 } else {
3764 tty_port_init(&info->port);
3765 info->port.ops = &port_ops;
3766 info->magic = MGSL_MAGIC;
3767 INIT_WORK(&info->task, bh_handler);
3768 info->max_frame_size = 4096;
3769 info->port.close_delay = 5*HZ/10;
3770 info->port.closing_wait = 30*HZ;
3771 init_waitqueue_head(&info->status_event_wait_q);
3772 init_waitqueue_head(&info->event_wait_q);
3773 spin_lock_init(&info->netlock);
3774 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3775 info->idle_mode = HDLC_TXIDLE_FLAGS;
3776 info->adapter_num = adapter_num;
3777 info->port_num = port_num;
3778
3779 /* Copy configuration info to device instance data */
3780 info->irq_level = pdev->irq;
3781 info->phys_lcr_base = pci_resource_start(pdev,0);
3782 info->phys_sca_base = pci_resource_start(pdev,2);
3783 info->phys_memory_base = pci_resource_start(pdev,3);
3784 info->phys_statctrl_base = pci_resource_start(pdev,4);
3785
3786 /* Because veremap only works on page boundaries we must map
3787 * a larger area than is actually implemented for the LCR
3788 * memory range. We map a full page starting at the page boundary.
3789 */
3790 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
3791 info->phys_lcr_base &= ~(PAGE_SIZE-1);
3792
3793 info->sca_offset = info->phys_sca_base & (PAGE_SIZE-1);
3794 info->phys_sca_base &= ~(PAGE_SIZE-1);
3795
3796 info->statctrl_offset = info->phys_statctrl_base & (PAGE_SIZE-1);
3797 info->phys_statctrl_base &= ~(PAGE_SIZE-1);
3798
3799 info->bus_type = MGSL_BUS_TYPE_PCI;
3800 info->irq_flags = IRQF_SHARED;
3801
3802 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3803 setup_timer(&info->status_timer, status_timeout,
3804 (unsigned long)info);
3805
3806 /* Store the PCI9050 misc control register value because a flaw
3807 * in the PCI9050 prevents LCR registers from being read if
3808 * BIOS assigns an LCR base address with bit 7 set.
3809 *
3810 * Only the misc control register is accessed for which only
3811 * write access is needed, so set an initial value and change
3812 * bits to the device instance data as we write the value
3813 * to the actual misc control register.
3814 */
3815 info->misc_ctrl_value = 0x087e4546;
3816
3817 /* initial port state is unknown - if startup errors
3818 * occur, init_error will be set to indicate the
3819 * problem. Once the port is fully initialized,
3820 * this value will be set to 0 to indicate the
3821 * port is available.
3822 */
3823 info->init_error = -1;
3824 }
3825
3826 return info;
3827}
3828
3829static void device_init(int adapter_num, struct pci_dev *pdev)
3830{
3831 SLMP_INFO *port_array[SCA_MAX_PORTS];
3832 int port;
3833
3834 /* allocate device instances for up to SCA_MAX_PORTS devices */
3835 for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3836 port_array[port] = alloc_dev(adapter_num,port,pdev);
3837 if( port_array[port] == NULL ) {
3838 for ( --port; port >= 0; --port )
3839 kfree(port_array[port]);
3840 return;
3841 }
3842 }
3843
3844 /* give copy of port_array to all ports and add to device list */
3845 for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3846 memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
3847 add_device( port_array[port] );
3848 spin_lock_init(&port_array[port]->lock);
3849 }
3850
3851 /* Allocate and claim adapter resources */
3852 if ( !claim_resources(port_array[0]) ) {
3853
3854 alloc_dma_bufs(port_array[0]);
3855
3856 /* copy resource information from first port to others */
3857 for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
3858 port_array[port]->lock = port_array[0]->lock;
3859 port_array[port]->irq_level = port_array[0]->irq_level;
3860 port_array[port]->memory_base = port_array[0]->memory_base;
3861 port_array[port]->sca_base = port_array[0]->sca_base;
3862 port_array[port]->statctrl_base = port_array[0]->statctrl_base;
3863 port_array[port]->lcr_base = port_array[0]->lcr_base;
3864 alloc_dma_bufs(port_array[port]);
3865 }
3866
3867 if ( request_irq(port_array[0]->irq_level,
3868 synclinkmp_interrupt,
3869 port_array[0]->irq_flags,
3870 port_array[0]->device_name,
3871 port_array[0]) < 0 ) {
3872 printk( "%s(%d):%s Can't request interrupt, IRQ=%d\n",
3873 __FILE__,__LINE__,
3874 port_array[0]->device_name,
3875 port_array[0]->irq_level );
3876 }
3877 else {
3878 port_array[0]->irq_requested = true;
3879 adapter_test(port_array[0]);
3880 }
3881 }
3882}
3883
3884static const struct tty_operations ops = {
3885 .open = open,
3886 .close = close,
3887 .write = write,
3888 .put_char = put_char,
3889 .flush_chars = flush_chars,
3890 .write_room = write_room,
3891 .chars_in_buffer = chars_in_buffer,
3892 .flush_buffer = flush_buffer,
3893 .ioctl = ioctl,
3894 .throttle = throttle,
3895 .unthrottle = unthrottle,
3896 .send_xchar = send_xchar,
3897 .break_ctl = set_break,
3898 .wait_until_sent = wait_until_sent,
3899 .set_termios = set_termios,
3900 .stop = tx_hold,
3901 .start = tx_release,
3902 .hangup = hangup,
3903 .tiocmget = tiocmget,
3904 .tiocmset = tiocmset,
3905 .get_icount = get_icount,
3906 .proc_fops = &synclinkmp_proc_fops,
3907};
3908
3909
3910static void synclinkmp_cleanup(void)
3911{
3912 int rc;
3913 SLMP_INFO *info;
3914 SLMP_INFO *tmp;
3915
3916 printk("Unloading %s %s\n", driver_name, driver_version);
3917
3918 if (serial_driver) {
3919 if ((rc = tty_unregister_driver(serial_driver)))
3920 printk("%s(%d) failed to unregister tty driver err=%d\n",
3921 __FILE__,__LINE__,rc);
3922 put_tty_driver(serial_driver);
3923 }
3924
3925 /* reset devices */
3926 info = synclinkmp_device_list;
3927 while(info) {
3928 reset_port(info);
3929 info = info->next_device;
3930 }
3931
3932 /* release devices */
3933 info = synclinkmp_device_list;
3934 while(info) {
3935#if SYNCLINK_GENERIC_HDLC
3936 hdlcdev_exit(info);
3937#endif
3938 free_dma_bufs(info);
3939 free_tmp_rx_buf(info);
3940 if ( info->port_num == 0 ) {
3941 if (info->sca_base)
3942 write_reg(info, LPR, 1); /* set low power mode */
3943 release_resources(info);
3944 }
3945 tmp = info;
3946 info = info->next_device;
3947 kfree(tmp);
3948 }
3949
3950 pci_unregister_driver(&synclinkmp_pci_driver);
3951}
3952
3953/* Driver initialization entry point.
3954 */
3955
3956static int __init synclinkmp_init(void)
3957{
3958 int rc;
3959
3960 if (break_on_load) {
3961 synclinkmp_get_text_ptr();
3962 BREAKPOINT();
3963 }
3964
3965 printk("%s %s\n", driver_name, driver_version);
3966
3967 if ((rc = pci_register_driver(&synclinkmp_pci_driver)) < 0) {
3968 printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
3969 return rc;
3970 }
3971
3972 serial_driver = alloc_tty_driver(128);
3973 if (!serial_driver) {
3974 rc = -ENOMEM;
3975 goto error;
3976 }
3977
3978 /* Initialize the tty_driver structure */
3979
3980 serial_driver->owner = THIS_MODULE;
3981 serial_driver->driver_name = "synclinkmp";
3982 serial_driver->name = "ttySLM";
3983 serial_driver->major = ttymajor;
3984 serial_driver->minor_start = 64;
3985 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3986 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3987 serial_driver->init_termios = tty_std_termios;
3988 serial_driver->init_termios.c_cflag =
3989 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3990 serial_driver->init_termios.c_ispeed = 9600;
3991 serial_driver->init_termios.c_ospeed = 9600;
3992 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3993 tty_set_operations(serial_driver, &ops);
3994 if ((rc = tty_register_driver(serial_driver)) < 0) {
3995 printk("%s(%d):Couldn't register serial driver\n",
3996 __FILE__,__LINE__);
3997 put_tty_driver(serial_driver);
3998 serial_driver = NULL;
3999 goto error;
4000 }
4001
4002 printk("%s %s, tty major#%d\n",
4003 driver_name, driver_version,
4004 serial_driver->major);
4005
4006 return 0;
4007
4008error:
4009 synclinkmp_cleanup();
4010 return rc;
4011}
4012
4013static void __exit synclinkmp_exit(void)
4014{
4015 synclinkmp_cleanup();
4016}
4017
4018module_init(synclinkmp_init);
4019module_exit(synclinkmp_exit);
4020
4021/* Set the port for internal loopback mode.
4022 * The TxCLK and RxCLK signals are generated from the BRG and
4023 * the TxD is looped back to the RxD internally.
4024 */
4025static void enable_loopback(SLMP_INFO *info, int enable)
4026{
4027 if (enable) {
4028 /* MD2 (Mode Register 2)
4029 * 01..00 CNCT<1..0> Channel Connection 11=Local Loopback
4030 */
4031 write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
4032
4033 /* degate external TxC clock source */
4034 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4035 write_control_reg(info);
4036
4037 /* RXS/TXS (Rx/Tx clock source)
4038 * 07 Reserved, must be 0
4039 * 06..04 Clock Source, 100=BRG
4040 * 03..00 Clock Divisor, 0000=1
4041 */
4042 write_reg(info, RXS, 0x40);
4043 write_reg(info, TXS, 0x40);
4044
4045 } else {
4046 /* MD2 (Mode Register 2)
4047 * 01..00 CNCT<1..0> Channel connection, 0=normal
4048 */
4049 write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
4050
4051 /* RXS/TXS (Rx/Tx clock source)
4052 * 07 Reserved, must be 0
4053 * 06..04 Clock Source, 000=RxC/TxC Pin
4054 * 03..00 Clock Divisor, 0000=1
4055 */
4056 write_reg(info, RXS, 0x00);
4057 write_reg(info, TXS, 0x00);
4058 }
4059
4060 /* set LinkSpeed if available, otherwise default to 2Mbps */
4061 if (info->params.clock_speed)
4062 set_rate(info, info->params.clock_speed);
4063 else
4064 set_rate(info, 3686400);
4065}
4066
4067/* Set the baud rate register to the desired speed
4068 *
4069 * data_rate data rate of clock in bits per second
4070 * A data rate of 0 disables the AUX clock.
4071 */
4072static void set_rate( SLMP_INFO *info, u32 data_rate )
4073{
4074 u32 TMCValue;
4075 unsigned char BRValue;
4076 u32 Divisor=0;
4077
4078 /* fBRG = fCLK/(TMC * 2^BR)
4079 */
4080 if (data_rate != 0) {
4081 Divisor = 14745600/data_rate;
4082 if (!Divisor)
4083 Divisor = 1;
4084
4085 TMCValue = Divisor;
4086
4087 BRValue = 0;
4088 if (TMCValue != 1 && TMCValue != 2) {
4089 /* BRValue of 0 provides 50/50 duty cycle *only* when
4090 * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
4091 * 50/50 duty cycle.
4092 */
4093 BRValue = 1;
4094 TMCValue >>= 1;
4095 }
4096
4097 /* while TMCValue is too big for TMC register, divide
4098 * by 2 and increment BR exponent.
4099 */
4100 for(; TMCValue > 256 && BRValue < 10; BRValue++)
4101 TMCValue >>= 1;
4102
4103 write_reg(info, TXS,
4104 (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
4105 write_reg(info, RXS,
4106 (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
4107 write_reg(info, TMC, (unsigned char)TMCValue);
4108 }
4109 else {
4110 write_reg(info, TXS,0);
4111 write_reg(info, RXS,0);
4112 write_reg(info, TMC, 0);
4113 }
4114}
4115
4116/* Disable receiver
4117 */
4118static void rx_stop(SLMP_INFO *info)
4119{
4120 if (debug_level >= DEBUG_LEVEL_ISR)
4121 printk("%s(%d):%s rx_stop()\n",
4122 __FILE__,__LINE__, info->device_name );
4123
4124 write_reg(info, CMD, RXRESET);
4125
4126 info->ie0_value &= ~RXRDYE;
4127 write_reg(info, IE0, info->ie0_value); /* disable Rx data interrupts */
4128
4129 write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
4130 write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4131 write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
4132
4133 info->rx_enabled = false;
4134 info->rx_overflow = false;
4135}
4136
4137/* enable the receiver
4138 */
4139static void rx_start(SLMP_INFO *info)
4140{
4141 int i;
4142
4143 if (debug_level >= DEBUG_LEVEL_ISR)
4144 printk("%s(%d):%s rx_start()\n",
4145 __FILE__,__LINE__, info->device_name );
4146
4147 write_reg(info, CMD, RXRESET);
4148
4149 if ( info->params.mode == MGSL_MODE_HDLC ) {
4150 /* HDLC, disabe IRQ on rxdata */
4151 info->ie0_value &= ~RXRDYE;
4152 write_reg(info, IE0, info->ie0_value);
4153
4154 /* Reset all Rx DMA buffers and program rx dma */
4155 write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
4156 write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4157
4158 for (i = 0; i < info->rx_buf_count; i++) {
4159 info->rx_buf_list[i].status = 0xff;
4160
4161 // throttle to 4 shared memory writes at a time to prevent
4162 // hogging local bus (keep latency time for DMA requests low).
4163 if (!(i % 4))
4164 read_status_reg(info);
4165 }
4166 info->current_rx_buf = 0;
4167
4168 /* set current/1st descriptor address */
4169 write_reg16(info, RXDMA + CDA,
4170 info->rx_buf_list_ex[0].phys_entry);
4171
4172 /* set new last rx descriptor address */
4173 write_reg16(info, RXDMA + EDA,
4174 info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
4175
4176 /* set buffer length (shared by all rx dma data buffers) */
4177 write_reg16(info, RXDMA + BFL, SCABUFSIZE);
4178
4179 write_reg(info, RXDMA + DIR, 0x60); /* enable Rx DMA interrupts (EOM/BOF) */
4180 write_reg(info, RXDMA + DSR, 0xf2); /* clear Rx DMA IRQs, enable Rx DMA */
4181 } else {
4182 /* async, enable IRQ on rxdata */
4183 info->ie0_value |= RXRDYE;
4184 write_reg(info, IE0, info->ie0_value);
4185 }
4186
4187 write_reg(info, CMD, RXENABLE);
4188
4189 info->rx_overflow = false;
4190 info->rx_enabled = true;
4191}
4192
4193/* Enable the transmitter and send a transmit frame if
4194 * one is loaded in the DMA buffers.
4195 */
4196static void tx_start(SLMP_INFO *info)
4197{
4198 if (debug_level >= DEBUG_LEVEL_ISR)
4199 printk("%s(%d):%s tx_start() tx_count=%d\n",
4200 __FILE__,__LINE__, info->device_name,info->tx_count );
4201
4202 if (!info->tx_enabled ) {
4203 write_reg(info, CMD, TXRESET);
4204 write_reg(info, CMD, TXENABLE);
4205 info->tx_enabled = true;
4206 }
4207
4208 if ( info->tx_count ) {
4209
4210 /* If auto RTS enabled and RTS is inactive, then assert */
4211 /* RTS and set a flag indicating that the driver should */
4212 /* negate RTS when the transmission completes. */
4213
4214 info->drop_rts_on_tx_done = false;
4215
4216 if (info->params.mode != MGSL_MODE_ASYNC) {
4217
4218 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
4219 get_signals( info );
4220 if ( !(info->serial_signals & SerialSignal_RTS) ) {
4221 info->serial_signals |= SerialSignal_RTS;
4222 set_signals( info );
4223 info->drop_rts_on_tx_done = true;
4224 }
4225 }
4226
4227 write_reg16(info, TRC0,
4228 (unsigned short)(((tx_negate_fifo_level-1)<<8) + tx_active_fifo_level));
4229
4230 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
4231 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4232
4233 /* set TX CDA (current descriptor address) */
4234 write_reg16(info, TXDMA + CDA,
4235 info->tx_buf_list_ex[0].phys_entry);
4236
4237 /* set TX EDA (last descriptor address) */
4238 write_reg16(info, TXDMA + EDA,
4239 info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
4240
4241 /* enable underrun IRQ */
4242 info->ie1_value &= ~IDLE;
4243 info->ie1_value |= UDRN;
4244 write_reg(info, IE1, info->ie1_value);
4245 write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
4246
4247 write_reg(info, TXDMA + DIR, 0x40); /* enable Tx DMA interrupts (EOM) */
4248 write_reg(info, TXDMA + DSR, 0xf2); /* clear Tx DMA IRQs, enable Tx DMA */
4249
4250 mod_timer(&info->tx_timer, jiffies +
4251 msecs_to_jiffies(5000));
4252 }
4253 else {
4254 tx_load_fifo(info);
4255 /* async, enable IRQ on txdata */
4256 info->ie0_value |= TXRDYE;
4257 write_reg(info, IE0, info->ie0_value);
4258 }
4259
4260 info->tx_active = true;
4261 }
4262}
4263
4264/* stop the transmitter and DMA
4265 */
4266static void tx_stop( SLMP_INFO *info )
4267{
4268 if (debug_level >= DEBUG_LEVEL_ISR)
4269 printk("%s(%d):%s tx_stop()\n",
4270 __FILE__,__LINE__, info->device_name );
4271
4272 del_timer(&info->tx_timer);
4273
4274 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
4275 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4276
4277 write_reg(info, CMD, TXRESET);
4278
4279 info->ie1_value &= ~(UDRN + IDLE);
4280 write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
4281 write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
4282
4283 info->ie0_value &= ~TXRDYE;
4284 write_reg(info, IE0, info->ie0_value); /* disable tx data interrupts */
4285
4286 info->tx_enabled = false;
4287 info->tx_active = false;
4288}
4289
4290/* Fill the transmit FIFO until the FIFO is full or
4291 * there is no more data to load.
4292 */
4293static void tx_load_fifo(SLMP_INFO *info)
4294{
4295 u8 TwoBytes[2];
4296
4297 /* do nothing is now tx data available and no XON/XOFF pending */
4298
4299 if ( !info->tx_count && !info->x_char )
4300 return;
4301
4302 /* load the Transmit FIFO until FIFOs full or all data sent */
4303
4304 while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
4305
4306 /* there is more space in the transmit FIFO and */
4307 /* there is more data in transmit buffer */
4308
4309 if ( (info->tx_count > 1) && !info->x_char ) {
4310 /* write 16-bits */
4311 TwoBytes[0] = info->tx_buf[info->tx_get++];
4312 if (info->tx_get >= info->max_frame_size)
4313 info->tx_get -= info->max_frame_size;
4314 TwoBytes[1] = info->tx_buf[info->tx_get++];
4315 if (info->tx_get >= info->max_frame_size)
4316 info->tx_get -= info->max_frame_size;
4317
4318 write_reg16(info, TRB, *((u16 *)TwoBytes));
4319
4320 info->tx_count -= 2;
4321 info->icount.tx += 2;
4322 } else {
4323 /* only 1 byte left to transmit or 1 FIFO slot left */
4324
4325 if (info->x_char) {
4326 /* transmit pending high priority char */
4327 write_reg(info, TRB, info->x_char);
4328 info->x_char = 0;
4329 } else {
4330 write_reg(info, TRB, info->tx_buf[info->tx_get++]);
4331 if (info->tx_get >= info->max_frame_size)
4332 info->tx_get -= info->max_frame_size;
4333 info->tx_count--;
4334 }
4335 info->icount.tx++;
4336 }
4337 }
4338}
4339
4340/* Reset a port to a known state
4341 */
4342static void reset_port(SLMP_INFO *info)
4343{
4344 if (info->sca_base) {
4345
4346 tx_stop(info);
4347 rx_stop(info);
4348
4349 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4350 set_signals(info);
4351
4352 /* disable all port interrupts */
4353 info->ie0_value = 0;
4354 info->ie1_value = 0;
4355 info->ie2_value = 0;
4356 write_reg(info, IE0, info->ie0_value);
4357 write_reg(info, IE1, info->ie1_value);
4358 write_reg(info, IE2, info->ie2_value);
4359
4360 write_reg(info, CMD, CHRESET);
4361 }
4362}
4363
4364/* Reset all the ports to a known state.
4365 */
4366static void reset_adapter(SLMP_INFO *info)
4367{
4368 int i;
4369
4370 for ( i=0; i < SCA_MAX_PORTS; ++i) {
4371 if (info->port_array[i])
4372 reset_port(info->port_array[i]);
4373 }
4374}
4375
4376/* Program port for asynchronous communications.
4377 */
4378static void async_mode(SLMP_INFO *info)
4379{
4380
4381 unsigned char RegValue;
4382
4383 tx_stop(info);
4384 rx_stop(info);
4385
4386 /* MD0, Mode Register 0
4387 *
4388 * 07..05 PRCTL<2..0>, Protocol Mode, 000=async
4389 * 04 AUTO, Auto-enable (RTS/CTS/DCD)
4390 * 03 Reserved, must be 0
4391 * 02 CRCCC, CRC Calculation, 0=disabled
4392 * 01..00 STOP<1..0> Stop bits (00=1,10=2)
4393 *
4394 * 0000 0000
4395 */
4396 RegValue = 0x00;
4397 if (info->params.stop_bits != 1)
4398 RegValue |= BIT1;
4399 write_reg(info, MD0, RegValue);
4400
4401 /* MD1, Mode Register 1
4402 *
4403 * 07..06 BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
4404 * 05..04 TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
4405 * 03..02 RXCHR<1..0>, rx char size
4406 * 01..00 PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
4407 *
4408 * 0100 0000
4409 */
4410 RegValue = 0x40;
4411 switch (info->params.data_bits) {
4412 case 7: RegValue |= BIT4 + BIT2; break;
4413 case 6: RegValue |= BIT5 + BIT3; break;
4414 case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
4415 }
4416 if (info->params.parity != ASYNC_PARITY_NONE) {
4417 RegValue |= BIT1;
4418 if (info->params.parity == ASYNC_PARITY_ODD)
4419 RegValue |= BIT0;
4420 }
4421 write_reg(info, MD1, RegValue);
4422
4423 /* MD2, Mode Register 2
4424 *
4425 * 07..02 Reserved, must be 0
4426 * 01..00 CNCT<1..0> Channel connection, 00=normal 11=local loopback
4427 *
4428 * 0000 0000
4429 */
4430 RegValue = 0x00;
4431 if (info->params.loopback)
4432 RegValue |= (BIT1 + BIT0);
4433 write_reg(info, MD2, RegValue);
4434
4435 /* RXS, Receive clock source
4436 *
4437 * 07 Reserved, must be 0
4438 * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4439 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4440 */
4441 RegValue=BIT6;
4442 write_reg(info, RXS, RegValue);
4443
4444 /* TXS, Transmit clock source
4445 *
4446 * 07 Reserved, must be 0
4447 * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4448 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4449 */
4450 RegValue=BIT6;
4451 write_reg(info, TXS, RegValue);
4452
4453 /* Control Register
4454 *
4455 * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4456 */
4457 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4458 write_control_reg(info);
4459
4460 tx_set_idle(info);
4461
4462 /* RRC Receive Ready Control 0
4463 *
4464 * 07..05 Reserved, must be 0
4465 * 04..00 RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
4466 */
4467 write_reg(info, RRC, 0x00);
4468
4469 /* TRC0 Transmit Ready Control 0
4470 *
4471 * 07..05 Reserved, must be 0
4472 * 04..00 TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
4473 */
4474 write_reg(info, TRC0, 0x10);
4475
4476 /* TRC1 Transmit Ready Control 1
4477 *
4478 * 07..05 Reserved, must be 0
4479 * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
4480 */
4481 write_reg(info, TRC1, 0x1e);
4482
4483 /* CTL, MSCI control register
4484 *
4485 * 07..06 Reserved, set to 0
4486 * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4487 * 04 IDLC, idle control, 0=mark 1=idle register
4488 * 03 BRK, break, 0=off 1 =on (async)
4489 * 02 SYNCLD, sync char load enable (BSC) 1=enabled
4490 * 01 GOP, go active on poll (LOOP mode) 1=enabled
4491 * 00 RTS, RTS output control, 0=active 1=inactive
4492 *
4493 * 0001 0001
4494 */
4495 RegValue = 0x10;
4496 if (!(info->serial_signals & SerialSignal_RTS))
4497 RegValue |= 0x01;
4498 write_reg(info, CTL, RegValue);
4499
4500 /* enable status interrupts */
4501 info->ie0_value |= TXINTE + RXINTE;
4502 write_reg(info, IE0, info->ie0_value);
4503
4504 /* enable break detect interrupt */
4505 info->ie1_value = BRKD;
4506 write_reg(info, IE1, info->ie1_value);
4507
4508 /* enable rx overrun interrupt */
4509 info->ie2_value = OVRN;
4510 write_reg(info, IE2, info->ie2_value);
4511
4512 set_rate( info, info->params.data_rate * 16 );
4513}
4514
4515/* Program the SCA for HDLC communications.
4516 */
4517static void hdlc_mode(SLMP_INFO *info)
4518{
4519 unsigned char RegValue;
4520 u32 DpllDivisor;
4521
4522 // Can't use DPLL because SCA outputs recovered clock on RxC when
4523 // DPLL mode selected. This causes output contention with RxC receiver.
4524 // Use of DPLL would require external hardware to disable RxC receiver
4525 // when DPLL mode selected.
4526 info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
4527
4528 /* disable DMA interrupts */
4529 write_reg(info, TXDMA + DIR, 0);
4530 write_reg(info, RXDMA + DIR, 0);
4531
4532 /* MD0, Mode Register 0
4533 *
4534 * 07..05 PRCTL<2..0>, Protocol Mode, 100=HDLC
4535 * 04 AUTO, Auto-enable (RTS/CTS/DCD)
4536 * 03 Reserved, must be 0
4537 * 02 CRCCC, CRC Calculation, 1=enabled
4538 * 01 CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
4539 * 00 CRC0, CRC initial value, 1 = all 1s
4540 *
4541 * 1000 0001
4542 */
4543 RegValue = 0x81;
4544 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4545 RegValue |= BIT4;
4546 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4547 RegValue |= BIT4;
4548 if (info->params.crc_type == HDLC_CRC_16_CCITT)
4549 RegValue |= BIT2 + BIT1;
4550 write_reg(info, MD0, RegValue);
4551
4552 /* MD1, Mode Register 1
4553 *
4554 * 07..06 ADDRS<1..0>, Address detect, 00=no addr check
4555 * 05..04 TXCHR<1..0>, tx char size, 00=8 bits
4556 * 03..02 RXCHR<1..0>, rx char size, 00=8 bits
4557 * 01..00 PMPM<1..0>, Parity mode, 00=no parity
4558 *
4559 * 0000 0000
4560 */
4561 RegValue = 0x00;
4562 write_reg(info, MD1, RegValue);
4563
4564 /* MD2, Mode Register 2
4565 *
4566 * 07 NRZFM, 0=NRZ, 1=FM
4567 * 06..05 CODE<1..0> Encoding, 00=NRZ
4568 * 04..03 DRATE<1..0> DPLL Divisor, 00=8
4569 * 02 Reserved, must be 0
4570 * 01..00 CNCT<1..0> Channel connection, 0=normal
4571 *
4572 * 0000 0000
4573 */
4574 RegValue = 0x00;
4575 switch(info->params.encoding) {
4576 case HDLC_ENCODING_NRZI: RegValue |= BIT5; break;
4577 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT7 + BIT5; break; /* aka FM1 */
4578 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
4579 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break; /* aka Manchester */
4580#if 0
4581 case HDLC_ENCODING_NRZB: /* not supported */
4582 case HDLC_ENCODING_NRZI_MARK: /* not supported */
4583 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: /* not supported */
4584#endif
4585 }
4586 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4587 DpllDivisor = 16;
4588 RegValue |= BIT3;
4589 } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4590 DpllDivisor = 8;
4591 } else {
4592 DpllDivisor = 32;
4593 RegValue |= BIT4;
4594 }
4595 write_reg(info, MD2, RegValue);
4596
4597
4598 /* RXS, Receive clock source
4599 *
4600 * 07 Reserved, must be 0
4601 * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4602 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4603 */
4604 RegValue=0;
4605 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4606 RegValue |= BIT6;
4607 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4608 RegValue |= BIT6 + BIT5;
4609 write_reg(info, RXS, RegValue);
4610
4611 /* TXS, Transmit clock source
4612 *
4613 * 07 Reserved, must be 0
4614 * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4615 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4616 */
4617 RegValue=0;
4618 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4619 RegValue |= BIT6;
4620 if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4621 RegValue |= BIT6 + BIT5;
4622 write_reg(info, TXS, RegValue);
4623
4624 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4625 set_rate(info, info->params.clock_speed * DpllDivisor);
4626 else
4627 set_rate(info, info->params.clock_speed);
4628
4629 /* GPDATA (General Purpose I/O Data Register)
4630 *
4631 * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4632 */
4633 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4634 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4635 else
4636 info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
4637 write_control_reg(info);
4638
4639 /* RRC Receive Ready Control 0
4640 *
4641 * 07..05 Reserved, must be 0
4642 * 04..00 RRC<4..0> Rx FIFO trigger active
4643 */
4644 write_reg(info, RRC, rx_active_fifo_level);
4645
4646 /* TRC0 Transmit Ready Control 0
4647 *
4648 * 07..05 Reserved, must be 0
4649 * 04..00 TRC<4..0> Tx FIFO trigger active
4650 */
4651 write_reg(info, TRC0, tx_active_fifo_level);
4652
4653 /* TRC1 Transmit Ready Control 1
4654 *
4655 * 07..05 Reserved, must be 0
4656 * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
4657 */
4658 write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
4659
4660 /* DMR, DMA Mode Register
4661 *
4662 * 07..05 Reserved, must be 0
4663 * 04 TMOD, Transfer Mode: 1=chained-block
4664 * 03 Reserved, must be 0
4665 * 02 NF, Number of Frames: 1=multi-frame
4666 * 01 CNTE, Frame End IRQ Counter enable: 0=disabled
4667 * 00 Reserved, must be 0
4668 *
4669 * 0001 0100
4670 */
4671 write_reg(info, TXDMA + DMR, 0x14);
4672 write_reg(info, RXDMA + DMR, 0x14);
4673
4674 /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4675 write_reg(info, RXDMA + CPB,
4676 (unsigned char)(info->buffer_list_phys >> 16));
4677
4678 /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4679 write_reg(info, TXDMA + CPB,
4680 (unsigned char)(info->buffer_list_phys >> 16));
4681
4682 /* enable status interrupts. other code enables/disables
4683 * the individual sources for these two interrupt classes.
4684 */
4685 info->ie0_value |= TXINTE + RXINTE;
4686 write_reg(info, IE0, info->ie0_value);
4687
4688 /* CTL, MSCI control register
4689 *
4690 * 07..06 Reserved, set to 0
4691 * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4692 * 04 IDLC, idle control, 0=mark 1=idle register
4693 * 03 BRK, break, 0=off 1 =on (async)
4694 * 02 SYNCLD, sync char load enable (BSC) 1=enabled
4695 * 01 GOP, go active on poll (LOOP mode) 1=enabled
4696 * 00 RTS, RTS output control, 0=active 1=inactive
4697 *
4698 * 0001 0001
4699 */
4700 RegValue = 0x10;
4701 if (!(info->serial_signals & SerialSignal_RTS))
4702 RegValue |= 0x01;
4703 write_reg(info, CTL, RegValue);
4704
4705 /* preamble not supported ! */
4706
4707 tx_set_idle(info);
4708 tx_stop(info);
4709 rx_stop(info);
4710
4711 set_rate(info, info->params.clock_speed);
4712
4713 if (info->params.loopback)
4714 enable_loopback(info,1);
4715}
4716
4717/* Set the transmit HDLC idle mode
4718 */
4719static void tx_set_idle(SLMP_INFO *info)
4720{
4721 unsigned char RegValue = 0xff;
4722
4723 /* Map API idle mode to SCA register bits */
4724 switch(info->idle_mode) {
4725 case HDLC_TXIDLE_FLAGS: RegValue = 0x7e; break;
4726 case HDLC_TXIDLE_ALT_ZEROS_ONES: RegValue = 0xaa; break;
4727 case HDLC_TXIDLE_ZEROS: RegValue = 0x00; break;
4728 case HDLC_TXIDLE_ONES: RegValue = 0xff; break;
4729 case HDLC_TXIDLE_ALT_MARK_SPACE: RegValue = 0xaa; break;
4730 case HDLC_TXIDLE_SPACE: RegValue = 0x00; break;
4731 case HDLC_TXIDLE_MARK: RegValue = 0xff; break;
4732 }
4733
4734 write_reg(info, IDL, RegValue);
4735}
4736
4737/* Query the adapter for the state of the V24 status (input) signals.
4738 */
4739static void get_signals(SLMP_INFO *info)
4740{
4741 u16 status = read_reg(info, SR3);
4742 u16 gpstatus = read_status_reg(info);
4743 u16 testbit;
4744
4745 /* clear all serial signals except DTR and RTS */
4746 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
4747
4748 /* set serial signal bits to reflect MISR */
4749
4750 if (!(status & BIT3))
4751 info->serial_signals |= SerialSignal_CTS;
4752
4753 if ( !(status & BIT2))
4754 info->serial_signals |= SerialSignal_DCD;
4755
4756 testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
4757 if (!(gpstatus & testbit))
4758 info->serial_signals |= SerialSignal_RI;
4759
4760 testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
4761 if (!(gpstatus & testbit))
4762 info->serial_signals |= SerialSignal_DSR;
4763}
4764
4765/* Set the state of DTR and RTS based on contents of
4766 * serial_signals member of device context.
4767 */
4768static void set_signals(SLMP_INFO *info)
4769{
4770 unsigned char RegValue;
4771 u16 EnableBit;
4772
4773 RegValue = read_reg(info, CTL);
4774 if (info->serial_signals & SerialSignal_RTS)
4775 RegValue &= ~BIT0;
4776 else
4777 RegValue |= BIT0;
4778 write_reg(info, CTL, RegValue);
4779
4780 // Port 0..3 DTR is ctrl reg <1,3,5,7>
4781 EnableBit = BIT1 << (info->port_num*2);
4782 if (info->serial_signals & SerialSignal_DTR)
4783 info->port_array[0]->ctrlreg_value &= ~EnableBit;
4784 else
4785 info->port_array[0]->ctrlreg_value |= EnableBit;
4786 write_control_reg(info);
4787}
4788
4789/*******************/
4790/* DMA Buffer Code */
4791/*******************/
4792
4793/* Set the count for all receive buffers to SCABUFSIZE
4794 * and set the current buffer to the first buffer. This effectively
4795 * makes all buffers free and discards any data in buffers.
4796 */
4797static void rx_reset_buffers(SLMP_INFO *info)
4798{
4799 rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
4800}
4801
4802/* Free the buffers used by a received frame
4803 *
4804 * info pointer to device instance data
4805 * first index of 1st receive buffer of frame
4806 * last index of last receive buffer of frame
4807 */
4808static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
4809{
4810 bool done = false;
4811
4812 while(!done) {
4813 /* reset current buffer for reuse */
4814 info->rx_buf_list[first].status = 0xff;
4815
4816 if (first == last) {
4817 done = true;
4818 /* set new last rx descriptor address */
4819 write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
4820 }
4821
4822 first++;
4823 if (first == info->rx_buf_count)
4824 first = 0;
4825 }
4826
4827 /* set current buffer to next buffer after last buffer of frame */
4828 info->current_rx_buf = first;
4829}
4830
4831/* Return a received frame from the receive DMA buffers.
4832 * Only frames received without errors are returned.
4833 *
4834 * Return Value: true if frame returned, otherwise false
4835 */
4836static bool rx_get_frame(SLMP_INFO *info)
4837{
4838 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
4839 unsigned short status;
4840 unsigned int framesize = 0;
4841 bool ReturnCode = false;
4842 unsigned long flags;
4843 struct tty_struct *tty = info->port.tty;
4844 unsigned char addr_field = 0xff;
4845 SCADESC *desc;
4846 SCADESC_EX *desc_ex;
4847
4848CheckAgain:
4849 /* assume no frame returned, set zero length */
4850 framesize = 0;
4851 addr_field = 0xff;
4852
4853 /*
4854 * current_rx_buf points to the 1st buffer of the next available
4855 * receive frame. To find the last buffer of the frame look for
4856 * a non-zero status field in the buffer entries. (The status
4857 * field is set by the 16C32 after completing a receive frame.
4858 */
4859 StartIndex = EndIndex = info->current_rx_buf;
4860
4861 for ( ;; ) {
4862 desc = &info->rx_buf_list[EndIndex];
4863 desc_ex = &info->rx_buf_list_ex[EndIndex];
4864
4865 if (desc->status == 0xff)
4866 goto Cleanup; /* current desc still in use, no frames available */
4867
4868 if (framesize == 0 && info->params.addr_filter != 0xff)
4869 addr_field = desc_ex->virt_addr[0];
4870
4871 framesize += desc->length;
4872
4873 /* Status != 0 means last buffer of frame */
4874 if (desc->status)
4875 break;
4876
4877 EndIndex++;
4878 if (EndIndex == info->rx_buf_count)
4879 EndIndex = 0;
4880
4881 if (EndIndex == info->current_rx_buf) {
4882 /* all buffers have been 'used' but none mark */
4883 /* the end of a frame. Reset buffers and receiver. */
4884 if ( info->rx_enabled ){
4885 spin_lock_irqsave(&info->lock,flags);
4886 rx_start(info);
4887 spin_unlock_irqrestore(&info->lock,flags);
4888 }
4889 goto Cleanup;
4890 }
4891
4892 }
4893
4894 /* check status of receive frame */
4895
4896 /* frame status is byte stored after frame data
4897 *
4898 * 7 EOM (end of msg), 1 = last buffer of frame
4899 * 6 Short Frame, 1 = short frame
4900 * 5 Abort, 1 = frame aborted
4901 * 4 Residue, 1 = last byte is partial
4902 * 3 Overrun, 1 = overrun occurred during frame reception
4903 * 2 CRC, 1 = CRC error detected
4904 *
4905 */
4906 status = desc->status;
4907
4908 /* ignore CRC bit if not using CRC (bit is undefined) */
4909 /* Note:CRC is not save to data buffer */
4910 if (info->params.crc_type == HDLC_CRC_NONE)
4911 status &= ~BIT2;
4912
4913 if (framesize == 0 ||
4914 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4915 /* discard 0 byte frames, this seems to occur sometime
4916 * when remote is idling flags.
4917 */
4918 rx_free_frame_buffers(info, StartIndex, EndIndex);
4919 goto CheckAgain;
4920 }
4921
4922 if (framesize < 2)
4923 status |= BIT6;
4924
4925 if (status & (BIT6+BIT5+BIT3+BIT2)) {
4926 /* received frame has errors,
4927 * update counts and mark frame size as 0
4928 */
4929 if (status & BIT6)
4930 info->icount.rxshort++;
4931 else if (status & BIT5)
4932 info->icount.rxabort++;
4933 else if (status & BIT3)
4934 info->icount.rxover++;
4935 else
4936 info->icount.rxcrc++;
4937
4938 framesize = 0;
4939#if SYNCLINK_GENERIC_HDLC
4940 {
4941 info->netdev->stats.rx_errors++;
4942 info->netdev->stats.rx_frame_errors++;
4943 }
4944#endif
4945 }
4946
4947 if ( debug_level >= DEBUG_LEVEL_BH )
4948 printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
4949 __FILE__,__LINE__,info->device_name,status,framesize);
4950
4951 if ( debug_level >= DEBUG_LEVEL_DATA )
4952 trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
4953 min_t(int, framesize,SCABUFSIZE),0);
4954
4955 if (framesize) {
4956 if (framesize > info->max_frame_size)
4957 info->icount.rxlong++;
4958 else {
4959 /* copy dma buffer(s) to contiguous intermediate buffer */
4960 int copy_count = framesize;
4961 int index = StartIndex;
4962 unsigned char *ptmp = info->tmp_rx_buf;
4963 info->tmp_rx_buf_count = framesize;
4964
4965 info->icount.rxok++;
4966
4967 while(copy_count) {
4968 int partial_count = min(copy_count,SCABUFSIZE);
4969 memcpy( ptmp,
4970 info->rx_buf_list_ex[index].virt_addr,
4971 partial_count );
4972 ptmp += partial_count;
4973 copy_count -= partial_count;
4974
4975 if ( ++index == info->rx_buf_count )
4976 index = 0;
4977 }
4978
4979#if SYNCLINK_GENERIC_HDLC
4980 if (info->netcount)
4981 hdlcdev_rx(info,info->tmp_rx_buf,framesize);
4982 else
4983#endif
4984 ldisc_receive_buf(tty,info->tmp_rx_buf,
4985 info->flag_buf, framesize);
4986 }
4987 }
4988 /* Free the buffers used by this frame. */
4989 rx_free_frame_buffers( info, StartIndex, EndIndex );
4990
4991 ReturnCode = true;
4992
4993Cleanup:
4994 if ( info->rx_enabled && info->rx_overflow ) {
4995 /* Receiver is enabled, but needs to restarted due to
4996 * rx buffer overflow. If buffers are empty, restart receiver.
4997 */
4998 if (info->rx_buf_list[EndIndex].status == 0xff) {
4999 spin_lock_irqsave(&info->lock,flags);
5000 rx_start(info);
5001 spin_unlock_irqrestore(&info->lock,flags);
5002 }
5003 }
5004
5005 return ReturnCode;
5006}
5007
5008/* load the transmit DMA buffer with data
5009 */
5010static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
5011{
5012 unsigned short copy_count;
5013 unsigned int i = 0;
5014 SCADESC *desc;
5015 SCADESC_EX *desc_ex;
5016
5017 if ( debug_level >= DEBUG_LEVEL_DATA )
5018 trace_block(info,buf, min_t(int, count,SCABUFSIZE), 1);
5019
5020 /* Copy source buffer to one or more DMA buffers, starting with
5021 * the first transmit dma buffer.
5022 */
5023 for(i=0;;)
5024 {
5025 copy_count = min_t(unsigned short,count,SCABUFSIZE);
5026
5027 desc = &info->tx_buf_list[i];
5028 desc_ex = &info->tx_buf_list_ex[i];
5029
5030 load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
5031
5032 desc->length = copy_count;
5033 desc->status = 0;
5034
5035 buf += copy_count;
5036 count -= copy_count;
5037
5038 if (!count)
5039 break;
5040
5041 i++;
5042 if (i >= info->tx_buf_count)
5043 i = 0;
5044 }
5045
5046 info->tx_buf_list[i].status = 0x81; /* set EOM and EOT status */
5047 info->last_tx_buf = ++i;
5048}
5049
5050static bool register_test(SLMP_INFO *info)
5051{
5052 static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
5053 static unsigned int count = ARRAY_SIZE(testval);
5054 unsigned int i;
5055 bool rc = true;
5056 unsigned long flags;
5057
5058 spin_lock_irqsave(&info->lock,flags);
5059 reset_port(info);
5060
5061 /* assume failure */
5062 info->init_error = DiagStatus_AddressFailure;
5063
5064 /* Write bit patterns to various registers but do it out of */
5065 /* sync, then read back and verify values. */
5066
5067 for (i = 0 ; i < count ; i++) {
5068 write_reg(info, TMC, testval[i]);
5069 write_reg(info, IDL, testval[(i+1)%count]);
5070 write_reg(info, SA0, testval[(i+2)%count]);
5071 write_reg(info, SA1, testval[(i+3)%count]);
5072
5073 if ( (read_reg(info, TMC) != testval[i]) ||
5074 (read_reg(info, IDL) != testval[(i+1)%count]) ||
5075 (read_reg(info, SA0) != testval[(i+2)%count]) ||
5076 (read_reg(info, SA1) != testval[(i+3)%count]) )
5077 {
5078 rc = false;
5079 break;
5080 }
5081 }
5082
5083 reset_port(info);
5084 spin_unlock_irqrestore(&info->lock,flags);
5085
5086 return rc;
5087}
5088
5089static bool irq_test(SLMP_INFO *info)
5090{
5091 unsigned long timeout;
5092 unsigned long flags;
5093
5094 unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
5095
5096 spin_lock_irqsave(&info->lock,flags);
5097 reset_port(info);
5098
5099 /* assume failure */
5100 info->init_error = DiagStatus_IrqFailure;
5101 info->irq_occurred = false;
5102
5103 /* setup timer0 on SCA0 to interrupt */
5104
5105 /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
5106 write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
5107
5108 write_reg(info, (unsigned char)(timer + TEPR), 0); /* timer expand prescale */
5109 write_reg16(info, (unsigned char)(timer + TCONR), 1); /* timer constant */
5110
5111
5112 /* TMCS, Timer Control/Status Register
5113 *
5114 * 07 CMF, Compare match flag (read only) 1=match
5115 * 06 ECMI, CMF Interrupt Enable: 1=enabled
5116 * 05 Reserved, must be 0
5117 * 04 TME, Timer Enable
5118 * 03..00 Reserved, must be 0
5119 *
5120 * 0101 0000
5121 */
5122 write_reg(info, (unsigned char)(timer + TMCS), 0x50);
5123
5124 spin_unlock_irqrestore(&info->lock,flags);
5125
5126 timeout=100;
5127 while( timeout-- && !info->irq_occurred ) {
5128 msleep_interruptible(10);
5129 }
5130
5131 spin_lock_irqsave(&info->lock,flags);
5132 reset_port(info);
5133 spin_unlock_irqrestore(&info->lock,flags);
5134
5135 return info->irq_occurred;
5136}
5137
5138/* initialize individual SCA device (2 ports)
5139 */
5140static bool sca_init(SLMP_INFO *info)
5141{
5142 /* set wait controller to single mem partition (low), no wait states */
5143 write_reg(info, PABR0, 0); /* wait controller addr boundary 0 */
5144 write_reg(info, PABR1, 0); /* wait controller addr boundary 1 */
5145 write_reg(info, WCRL, 0); /* wait controller low range */
5146 write_reg(info, WCRM, 0); /* wait controller mid range */
5147 write_reg(info, WCRH, 0); /* wait controller high range */
5148
5149 /* DPCR, DMA Priority Control
5150 *
5151 * 07..05 Not used, must be 0
5152 * 04 BRC, bus release condition: 0=all transfers complete
5153 * 03 CCC, channel change condition: 0=every cycle
5154 * 02..00 PR<2..0>, priority 100=round robin
5155 *
5156 * 00000100 = 0x04
5157 */
5158 write_reg(info, DPCR, dma_priority);
5159
5160 /* DMA Master Enable, BIT7: 1=enable all channels */
5161 write_reg(info, DMER, 0x80);
5162
5163 /* enable all interrupt classes */
5164 write_reg(info, IER0, 0xff); /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
5165 write_reg(info, IER1, 0xff); /* DMIB,DMIA (channels 0-3) */
5166 write_reg(info, IER2, 0xf0); /* TIRQ (timers 0-3) */
5167
5168 /* ITCR, interrupt control register
5169 * 07 IPC, interrupt priority, 0=MSCI->DMA
5170 * 06..05 IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
5171 * 04 VOS, Vector Output, 0=unmodified vector
5172 * 03..00 Reserved, must be 0
5173 */
5174 write_reg(info, ITCR, 0);
5175
5176 return true;
5177}
5178
5179/* initialize adapter hardware
5180 */
5181static bool init_adapter(SLMP_INFO *info)
5182{
5183 int i;
5184
5185 /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
5186 volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5187 u32 readval;
5188
5189 info->misc_ctrl_value |= BIT30;
5190 *MiscCtrl = info->misc_ctrl_value;
5191
5192 /*
5193 * Force at least 170ns delay before clearing
5194 * reset bit. Each read from LCR takes at least
5195 * 30ns so 10 times for 300ns to be safe.
5196 */
5197 for(i=0;i<10;i++)
5198 readval = *MiscCtrl;
5199
5200 info->misc_ctrl_value &= ~BIT30;
5201 *MiscCtrl = info->misc_ctrl_value;
5202
5203 /* init control reg (all DTRs off, all clksel=input) */
5204 info->ctrlreg_value = 0xaa;
5205 write_control_reg(info);
5206
5207 {
5208 volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
5209 lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
5210
5211 switch(read_ahead_count)
5212 {
5213 case 16:
5214 lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
5215 break;
5216 case 8:
5217 lcr1_brdr_value |= BIT5 + BIT4;
5218 break;
5219 case 4:
5220 lcr1_brdr_value |= BIT5 + BIT3;
5221 break;
5222 case 0:
5223 lcr1_brdr_value |= BIT5;
5224 break;
5225 }
5226
5227 *LCR1BRDR = lcr1_brdr_value;
5228 *MiscCtrl = misc_ctrl_value;
5229 }
5230
5231 sca_init(info->port_array[0]);
5232 sca_init(info->port_array[2]);
5233
5234 return true;
5235}
5236
5237/* Loopback an HDLC frame to test the hardware
5238 * interrupt and DMA functions.
5239 */
5240static bool loopback_test(SLMP_INFO *info)
5241{
5242#define TESTFRAMESIZE 20
5243
5244 unsigned long timeout;
5245 u16 count = TESTFRAMESIZE;
5246 unsigned char buf[TESTFRAMESIZE];
5247 bool rc = false;
5248 unsigned long flags;
5249
5250 struct tty_struct *oldtty = info->port.tty;
5251 u32 speed = info->params.clock_speed;
5252
5253 info->params.clock_speed = 3686400;
5254 info->port.tty = NULL;
5255
5256 /* assume failure */
5257 info->init_error = DiagStatus_DmaFailure;
5258
5259 /* build and send transmit frame */
5260 for (count = 0; count < TESTFRAMESIZE;++count)
5261 buf[count] = (unsigned char)count;
5262
5263 memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
5264
5265 /* program hardware for HDLC and enabled receiver */
5266 spin_lock_irqsave(&info->lock,flags);
5267 hdlc_mode(info);
5268 enable_loopback(info,1);
5269 rx_start(info);
5270 info->tx_count = count;
5271 tx_load_dma_buffer(info,buf,count);
5272 tx_start(info);
5273 spin_unlock_irqrestore(&info->lock,flags);
5274
5275 /* wait for receive complete */
5276 /* Set a timeout for waiting for interrupt. */
5277 for ( timeout = 100; timeout; --timeout ) {
5278 msleep_interruptible(10);
5279
5280 if (rx_get_frame(info)) {
5281 rc = true;
5282 break;
5283 }
5284 }
5285
5286 /* verify received frame length and contents */
5287 if (rc &&
5288 ( info->tmp_rx_buf_count != count ||
5289 memcmp(buf, info->tmp_rx_buf,count))) {
5290 rc = false;
5291 }
5292
5293 spin_lock_irqsave(&info->lock,flags);
5294 reset_adapter(info);
5295 spin_unlock_irqrestore(&info->lock,flags);
5296
5297 info->params.clock_speed = speed;
5298 info->port.tty = oldtty;
5299
5300 return rc;
5301}
5302
5303/* Perform diagnostics on hardware
5304 */
5305static int adapter_test( SLMP_INFO *info )
5306{
5307 unsigned long flags;
5308 if ( debug_level >= DEBUG_LEVEL_INFO )
5309 printk( "%s(%d):Testing device %s\n",
5310 __FILE__,__LINE__,info->device_name );
5311
5312 spin_lock_irqsave(&info->lock,flags);
5313 init_adapter(info);
5314 spin_unlock_irqrestore(&info->lock,flags);
5315
5316 info->port_array[0]->port_count = 0;
5317
5318 if ( register_test(info->port_array[0]) &&
5319 register_test(info->port_array[1])) {
5320
5321 info->port_array[0]->port_count = 2;
5322
5323 if ( register_test(info->port_array[2]) &&
5324 register_test(info->port_array[3]) )
5325 info->port_array[0]->port_count += 2;
5326 }
5327 else {
5328 printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
5329 __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
5330 return -ENODEV;
5331 }
5332
5333 if ( !irq_test(info->port_array[0]) ||
5334 !irq_test(info->port_array[1]) ||
5335 (info->port_count == 4 && !irq_test(info->port_array[2])) ||
5336 (info->port_count == 4 && !irq_test(info->port_array[3]))) {
5337 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
5338 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
5339 return -ENODEV;
5340 }
5341
5342 if (!loopback_test(info->port_array[0]) ||
5343 !loopback_test(info->port_array[1]) ||
5344 (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
5345 (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
5346 printk( "%s(%d):DMA test failure for device %s\n",
5347 __FILE__,__LINE__,info->device_name);
5348 return -ENODEV;
5349 }
5350
5351 if ( debug_level >= DEBUG_LEVEL_INFO )
5352 printk( "%s(%d):device %s passed diagnostics\n",
5353 __FILE__,__LINE__,info->device_name );
5354
5355 info->port_array[0]->init_error = 0;
5356 info->port_array[1]->init_error = 0;
5357 if ( info->port_count > 2 ) {
5358 info->port_array[2]->init_error = 0;
5359 info->port_array[3]->init_error = 0;
5360 }
5361
5362 return 0;
5363}
5364
5365/* Test the shared memory on a PCI adapter.
5366 */
5367static bool memory_test(SLMP_INFO *info)
5368{
5369 static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
5370 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
5371 unsigned long count = ARRAY_SIZE(testval);
5372 unsigned long i;
5373 unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
5374 unsigned long * addr = (unsigned long *)info->memory_base;
5375
5376 /* Test data lines with test pattern at one location. */
5377
5378 for ( i = 0 ; i < count ; i++ ) {
5379 *addr = testval[i];
5380 if ( *addr != testval[i] )
5381 return false;
5382 }
5383
5384 /* Test address lines with incrementing pattern over */
5385 /* entire address range. */
5386
5387 for ( i = 0 ; i < limit ; i++ ) {
5388 *addr = i * 4;
5389 addr++;
5390 }
5391
5392 addr = (unsigned long *)info->memory_base;
5393
5394 for ( i = 0 ; i < limit ; i++ ) {
5395 if ( *addr != i * 4 )
5396 return false;
5397 addr++;
5398 }
5399
5400 memset( info->memory_base, 0, SCA_MEM_SIZE );
5401 return true;
5402}
5403
5404/* Load data into PCI adapter shared memory.
5405 *
5406 * The PCI9050 releases control of the local bus
5407 * after completing the current read or write operation.
5408 *
5409 * While the PCI9050 write FIFO not empty, the
5410 * PCI9050 treats all of the writes as a single transaction
5411 * and does not release the bus. This causes DMA latency problems
5412 * at high speeds when copying large data blocks to the shared memory.
5413 *
5414 * This function breaks a write into multiple transations by
5415 * interleaving a read which flushes the write FIFO and 'completes'
5416 * the write transation. This allows any pending DMA request to gain control
5417 * of the local bus in a timely fasion.
5418 */
5419static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
5420{
5421 /* A load interval of 16 allows for 4 32-bit writes at */
5422 /* 136ns each for a maximum latency of 542ns on the local bus.*/
5423
5424 unsigned short interval = count / sca_pci_load_interval;
5425 unsigned short i;
5426
5427 for ( i = 0 ; i < interval ; i++ )
5428 {
5429 memcpy(dest, src, sca_pci_load_interval);
5430 read_status_reg(info);
5431 dest += sca_pci_load_interval;
5432 src += sca_pci_load_interval;
5433 }
5434
5435 memcpy(dest, src, count % sca_pci_load_interval);
5436}
5437
5438static void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
5439{
5440 int i;
5441 int linecount;
5442 if (xmit)
5443 printk("%s tx data:\n",info->device_name);
5444 else
5445 printk("%s rx data:\n",info->device_name);
5446
5447 while(count) {
5448 if (count > 16)
5449 linecount = 16;
5450 else
5451 linecount = count;
5452
5453 for(i=0;i<linecount;i++)
5454 printk("%02X ",(unsigned char)data[i]);
5455 for(;i<17;i++)
5456 printk(" ");
5457 for(i=0;i<linecount;i++) {
5458 if (data[i]>=040 && data[i]<=0176)
5459 printk("%c",data[i]);
5460 else
5461 printk(".");
5462 }
5463 printk("\n");
5464
5465 data += linecount;
5466 count -= linecount;
5467 }
5468} /* end of trace_block() */
5469
5470/* called when HDLC frame times out
5471 * update stats and do tx completion processing
5472 */
5473static void tx_timeout(unsigned long context)
5474{
5475 SLMP_INFO *info = (SLMP_INFO*)context;
5476 unsigned long flags;
5477
5478 if ( debug_level >= DEBUG_LEVEL_INFO )
5479 printk( "%s(%d):%s tx_timeout()\n",
5480 __FILE__,__LINE__,info->device_name);
5481 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5482 info->icount.txtimeout++;
5483 }
5484 spin_lock_irqsave(&info->lock,flags);
5485 info->tx_active = false;
5486 info->tx_count = info->tx_put = info->tx_get = 0;
5487
5488 spin_unlock_irqrestore(&info->lock,flags);
5489
5490#if SYNCLINK_GENERIC_HDLC
5491 if (info->netcount)
5492 hdlcdev_tx_done(info);
5493 else
5494#endif
5495 bh_transmit(info);
5496}
5497
5498/* called to periodically check the DSR/RI modem signal input status
5499 */
5500static void status_timeout(unsigned long context)
5501{
5502 u16 status = 0;
5503 SLMP_INFO *info = (SLMP_INFO*)context;
5504 unsigned long flags;
5505 unsigned char delta;
5506
5507
5508 spin_lock_irqsave(&info->lock,flags);
5509 get_signals(info);
5510 spin_unlock_irqrestore(&info->lock,flags);
5511
5512 /* check for DSR/RI state change */
5513
5514 delta = info->old_signals ^ info->serial_signals;
5515 info->old_signals = info->serial_signals;
5516
5517 if (delta & SerialSignal_DSR)
5518 status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
5519
5520 if (delta & SerialSignal_RI)
5521 status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
5522
5523 if (delta & SerialSignal_DCD)
5524 status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
5525
5526 if (delta & SerialSignal_CTS)
5527 status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
5528
5529 if (status)
5530 isr_io_pin(info,status);
5531
5532 mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
5533}
5534
5535
5536/* Register Access Routines -
5537 * All registers are memory mapped
5538 */
5539#define CALC_REGADDR() \
5540 unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
5541 if (info->port_num > 1) \
5542 RegAddr += 256; /* port 0-1 SCA0, 2-3 SCA1 */ \
5543 if ( info->port_num & 1) { \
5544 if (Addr > 0x7f) \
5545 RegAddr += 0x40; /* DMA access */ \
5546 else if (Addr > 0x1f && Addr < 0x60) \
5547 RegAddr += 0x20; /* MSCI access */ \
5548 }
5549
5550
5551static unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
5552{
5553 CALC_REGADDR();
5554 return *RegAddr;
5555}
5556static void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
5557{
5558 CALC_REGADDR();
5559 *RegAddr = Value;
5560}
5561
5562static u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
5563{
5564 CALC_REGADDR();
5565 return *((u16 *)RegAddr);
5566}
5567
5568static void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
5569{
5570 CALC_REGADDR();
5571 *((u16 *)RegAddr) = Value;
5572}
5573
5574static unsigned char read_status_reg(SLMP_INFO * info)
5575{
5576 unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5577 return *RegAddr;
5578}
5579
5580static void write_control_reg(SLMP_INFO * info)
5581{
5582 unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5583 *RegAddr = info->port_array[0]->ctrlreg_value;
5584}
5585
5586
5587static int __devinit synclinkmp_init_one (struct pci_dev *dev,
5588 const struct pci_device_id *ent)
5589{
5590 if (pci_enable_device(dev)) {
5591 printk("error enabling pci device %p\n", dev);
5592 return -EIO;
5593 }
5594 device_init( ++synclinkmp_adapter_count, dev );
5595 return 0;
5596}
5597
5598static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
5599{
5600}
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
new file mode 100644
index 000000000000..43db715f1502
--- /dev/null
+++ b/drivers/tty/sysrq.c
@@ -0,0 +1,905 @@
1/*
2 * Linux Magic System Request Key Hacks
3 *
4 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
6 *
7 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
8 * overhauled to use key registration
9 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
10 *
11 * Copyright (c) 2010 Dmitry Torokhov
12 * Input handler conversion
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/mount.h>
22#include <linux/kdev_t.h>
23#include <linux/major.h>
24#include <linux/reboot.h>
25#include <linux/sysrq.h>
26#include <linux/kbd_kern.h>
27#include <linux/proc_fs.h>
28#include <linux/nmi.h>
29#include <linux/quotaops.h>
30#include <linux/perf_event.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/suspend.h>
34#include <linux/writeback.h>
35#include <linux/buffer_head.h> /* for fsync_bdev() */
36#include <linux/swap.h>
37#include <linux/spinlock.h>
38#include <linux/vt_kern.h>
39#include <linux/workqueue.h>
40#include <linux/hrtimer.h>
41#include <linux/oom.h>
42#include <linux/slab.h>
43#include <linux/input.h>
44
45#include <asm/ptrace.h>
46#include <asm/irq_regs.h>
47
48/* Whether we react on sysrq keys or just ignore them */
49static int __read_mostly sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
50static bool __read_mostly sysrq_always_enabled;
51
52static bool sysrq_on(void)
53{
54 return sysrq_enabled || sysrq_always_enabled;
55}
56
57/*
58 * A value of 1 means 'all', other nonzero values are an op mask:
59 */
60static bool sysrq_on_mask(int mask)
61{
62 return sysrq_always_enabled ||
63 sysrq_enabled == 1 ||
64 (sysrq_enabled & mask);
65}
66
67static int __init sysrq_always_enabled_setup(char *str)
68{
69 sysrq_always_enabled = true;
70 pr_info("sysrq always enabled.\n");
71
72 return 1;
73}
74
75__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
76
77
78static void sysrq_handle_loglevel(int key)
79{
80 int i;
81
82 i = key - '0';
83 console_loglevel = 7;
84 printk("Loglevel set to %d\n", i);
85 console_loglevel = i;
86}
87static struct sysrq_key_op sysrq_loglevel_op = {
88 .handler = sysrq_handle_loglevel,
89 .help_msg = "loglevel(0-9)",
90 .action_msg = "Changing Loglevel",
91 .enable_mask = SYSRQ_ENABLE_LOG,
92};
93
94#ifdef CONFIG_VT
95static void sysrq_handle_SAK(int key)
96{
97 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
98 schedule_work(SAK_work);
99}
100static struct sysrq_key_op sysrq_SAK_op = {
101 .handler = sysrq_handle_SAK,
102 .help_msg = "saK",
103 .action_msg = "SAK",
104 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
105};
106#else
107#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
108#endif
109
110#ifdef CONFIG_VT
111static void sysrq_handle_unraw(int key)
112{
113 struct kbd_struct *kbd = &kbd_table[fg_console];
114
115 if (kbd)
116 kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
117}
118static struct sysrq_key_op sysrq_unraw_op = {
119 .handler = sysrq_handle_unraw,
120 .help_msg = "unRaw",
121 .action_msg = "Keyboard mode set to system default",
122 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
123};
124#else
125#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
126#endif /* CONFIG_VT */
127
128static void sysrq_handle_crash(int key)
129{
130 char *killer = NULL;
131
132 panic_on_oops = 1; /* force panic */
133 wmb();
134 *killer = 1;
135}
136static struct sysrq_key_op sysrq_crash_op = {
137 .handler = sysrq_handle_crash,
138 .help_msg = "Crash",
139 .action_msg = "Trigger a crash",
140 .enable_mask = SYSRQ_ENABLE_DUMP,
141};
142
143static void sysrq_handle_reboot(int key)
144{
145 lockdep_off();
146 local_irq_enable();
147 emergency_restart();
148}
149static struct sysrq_key_op sysrq_reboot_op = {
150 .handler = sysrq_handle_reboot,
151 .help_msg = "reBoot",
152 .action_msg = "Resetting",
153 .enable_mask = SYSRQ_ENABLE_BOOT,
154};
155
156static void sysrq_handle_sync(int key)
157{
158 emergency_sync();
159}
160static struct sysrq_key_op sysrq_sync_op = {
161 .handler = sysrq_handle_sync,
162 .help_msg = "Sync",
163 .action_msg = "Emergency Sync",
164 .enable_mask = SYSRQ_ENABLE_SYNC,
165};
166
167static void sysrq_handle_show_timers(int key)
168{
169 sysrq_timer_list_show();
170}
171
172static struct sysrq_key_op sysrq_show_timers_op = {
173 .handler = sysrq_handle_show_timers,
174 .help_msg = "show-all-timers(Q)",
175 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
176};
177
178static void sysrq_handle_mountro(int key)
179{
180 emergency_remount();
181}
182static struct sysrq_key_op sysrq_mountro_op = {
183 .handler = sysrq_handle_mountro,
184 .help_msg = "Unmount",
185 .action_msg = "Emergency Remount R/O",
186 .enable_mask = SYSRQ_ENABLE_REMOUNT,
187};
188
189#ifdef CONFIG_LOCKDEP
190static void sysrq_handle_showlocks(int key)
191{
192 debug_show_all_locks();
193}
194
195static struct sysrq_key_op sysrq_showlocks_op = {
196 .handler = sysrq_handle_showlocks,
197 .help_msg = "show-all-locks(D)",
198 .action_msg = "Show Locks Held",
199};
200#else
201#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
202#endif
203
204#ifdef CONFIG_SMP
205static DEFINE_SPINLOCK(show_lock);
206
207static void showacpu(void *dummy)
208{
209 unsigned long flags;
210
211 /* Idle CPUs have no interesting backtrace. */
212 if (idle_cpu(smp_processor_id()))
213 return;
214
215 spin_lock_irqsave(&show_lock, flags);
216 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
217 show_stack(NULL, NULL);
218 spin_unlock_irqrestore(&show_lock, flags);
219}
220
221static void sysrq_showregs_othercpus(struct work_struct *dummy)
222{
223 smp_call_function(showacpu, NULL, 0);
224}
225
226static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
227
228static void sysrq_handle_showallcpus(int key)
229{
230 /*
231 * Fall back to the workqueue based printing if the
232 * backtrace printing did not succeed or the
233 * architecture has no support for it:
234 */
235 if (!trigger_all_cpu_backtrace()) {
236 struct pt_regs *regs = get_irq_regs();
237
238 if (regs) {
239 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
240 show_regs(regs);
241 }
242 schedule_work(&sysrq_showallcpus);
243 }
244}
245
246static struct sysrq_key_op sysrq_showallcpus_op = {
247 .handler = sysrq_handle_showallcpus,
248 .help_msg = "show-backtrace-all-active-cpus(L)",
249 .action_msg = "Show backtrace of all active CPUs",
250 .enable_mask = SYSRQ_ENABLE_DUMP,
251};
252#endif
253
254static void sysrq_handle_showregs(int key)
255{
256 struct pt_regs *regs = get_irq_regs();
257 if (regs)
258 show_regs(regs);
259 perf_event_print_debug();
260}
261static struct sysrq_key_op sysrq_showregs_op = {
262 .handler = sysrq_handle_showregs,
263 .help_msg = "show-registers(P)",
264 .action_msg = "Show Regs",
265 .enable_mask = SYSRQ_ENABLE_DUMP,
266};
267
268static void sysrq_handle_showstate(int key)
269{
270 show_state();
271}
272static struct sysrq_key_op sysrq_showstate_op = {
273 .handler = sysrq_handle_showstate,
274 .help_msg = "show-task-states(T)",
275 .action_msg = "Show State",
276 .enable_mask = SYSRQ_ENABLE_DUMP,
277};
278
279static void sysrq_handle_showstate_blocked(int key)
280{
281 show_state_filter(TASK_UNINTERRUPTIBLE);
282}
283static struct sysrq_key_op sysrq_showstate_blocked_op = {
284 .handler = sysrq_handle_showstate_blocked,
285 .help_msg = "show-blocked-tasks(W)",
286 .action_msg = "Show Blocked State",
287 .enable_mask = SYSRQ_ENABLE_DUMP,
288};
289
290#ifdef CONFIG_TRACING
291#include <linux/ftrace.h>
292
293static void sysrq_ftrace_dump(int key)
294{
295 ftrace_dump(DUMP_ALL);
296}
297static struct sysrq_key_op sysrq_ftrace_dump_op = {
298 .handler = sysrq_ftrace_dump,
299 .help_msg = "dump-ftrace-buffer(Z)",
300 .action_msg = "Dump ftrace buffer",
301 .enable_mask = SYSRQ_ENABLE_DUMP,
302};
303#else
304#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
305#endif
306
307static void sysrq_handle_showmem(int key)
308{
309 show_mem(0);
310}
311static struct sysrq_key_op sysrq_showmem_op = {
312 .handler = sysrq_handle_showmem,
313 .help_msg = "show-memory-usage(M)",
314 .action_msg = "Show Memory",
315 .enable_mask = SYSRQ_ENABLE_DUMP,
316};
317
318/*
319 * Signal sysrq helper function. Sends a signal to all user processes.
320 */
321static void send_sig_all(int sig)
322{
323 struct task_struct *p;
324
325 for_each_process(p) {
326 if (p->mm && !is_global_init(p))
327 /* Not swapper, init nor kernel thread */
328 force_sig(sig, p);
329 }
330}
331
332static void sysrq_handle_term(int key)
333{
334 send_sig_all(SIGTERM);
335 console_loglevel = 8;
336}
337static struct sysrq_key_op sysrq_term_op = {
338 .handler = sysrq_handle_term,
339 .help_msg = "terminate-all-tasks(E)",
340 .action_msg = "Terminate All Tasks",
341 .enable_mask = SYSRQ_ENABLE_SIGNAL,
342};
343
344static void moom_callback(struct work_struct *ignored)
345{
346 out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL);
347}
348
349static DECLARE_WORK(moom_work, moom_callback);
350
351static void sysrq_handle_moom(int key)
352{
353 schedule_work(&moom_work);
354}
355static struct sysrq_key_op sysrq_moom_op = {
356 .handler = sysrq_handle_moom,
357 .help_msg = "memory-full-oom-kill(F)",
358 .action_msg = "Manual OOM execution",
359 .enable_mask = SYSRQ_ENABLE_SIGNAL,
360};
361
362#ifdef CONFIG_BLOCK
363static void sysrq_handle_thaw(int key)
364{
365 emergency_thaw_all();
366}
367static struct sysrq_key_op sysrq_thaw_op = {
368 .handler = sysrq_handle_thaw,
369 .help_msg = "thaw-filesystems(J)",
370 .action_msg = "Emergency Thaw of all frozen filesystems",
371 .enable_mask = SYSRQ_ENABLE_SIGNAL,
372};
373#endif
374
375static void sysrq_handle_kill(int key)
376{
377 send_sig_all(SIGKILL);
378 console_loglevel = 8;
379}
380static struct sysrq_key_op sysrq_kill_op = {
381 .handler = sysrq_handle_kill,
382 .help_msg = "kill-all-tasks(I)",
383 .action_msg = "Kill All Tasks",
384 .enable_mask = SYSRQ_ENABLE_SIGNAL,
385};
386
387static void sysrq_handle_unrt(int key)
388{
389 normalize_rt_tasks();
390}
391static struct sysrq_key_op sysrq_unrt_op = {
392 .handler = sysrq_handle_unrt,
393 .help_msg = "nice-all-RT-tasks(N)",
394 .action_msg = "Nice All RT Tasks",
395 .enable_mask = SYSRQ_ENABLE_RTNICE,
396};
397
398/* Key Operations table and lock */
399static DEFINE_SPINLOCK(sysrq_key_table_lock);
400
401static struct sysrq_key_op *sysrq_key_table[36] = {
402 &sysrq_loglevel_op, /* 0 */
403 &sysrq_loglevel_op, /* 1 */
404 &sysrq_loglevel_op, /* 2 */
405 &sysrq_loglevel_op, /* 3 */
406 &sysrq_loglevel_op, /* 4 */
407 &sysrq_loglevel_op, /* 5 */
408 &sysrq_loglevel_op, /* 6 */
409 &sysrq_loglevel_op, /* 7 */
410 &sysrq_loglevel_op, /* 8 */
411 &sysrq_loglevel_op, /* 9 */
412
413 /*
414 * a: Don't use for system provided sysrqs, it is handled specially on
415 * sparc and will never arrive.
416 */
417 NULL, /* a */
418 &sysrq_reboot_op, /* b */
419 &sysrq_crash_op, /* c & ibm_emac driver debug */
420 &sysrq_showlocks_op, /* d */
421 &sysrq_term_op, /* e */
422 &sysrq_moom_op, /* f */
423 /* g: May be registered for the kernel debugger */
424 NULL, /* g */
425 NULL, /* h - reserved for help */
426 &sysrq_kill_op, /* i */
427#ifdef CONFIG_BLOCK
428 &sysrq_thaw_op, /* j */
429#else
430 NULL, /* j */
431#endif
432 &sysrq_SAK_op, /* k */
433#ifdef CONFIG_SMP
434 &sysrq_showallcpus_op, /* l */
435#else
436 NULL, /* l */
437#endif
438 &sysrq_showmem_op, /* m */
439 &sysrq_unrt_op, /* n */
440 /* o: This will often be registered as 'Off' at init time */
441 NULL, /* o */
442 &sysrq_showregs_op, /* p */
443 &sysrq_show_timers_op, /* q */
444 &sysrq_unraw_op, /* r */
445 &sysrq_sync_op, /* s */
446 &sysrq_showstate_op, /* t */
447 &sysrq_mountro_op, /* u */
448 /* v: May be registered for frame buffer console restore */
449 NULL, /* v */
450 &sysrq_showstate_blocked_op, /* w */
451 /* x: May be registered on ppc/powerpc for xmon */
452 NULL, /* x */
453 /* y: May be registered on sparc64 for global register dump */
454 NULL, /* y */
455 &sysrq_ftrace_dump_op, /* z */
456};
457
458/* key2index calculation, -1 on invalid index */
459static int sysrq_key_table_key2index(int key)
460{
461 int retval;
462
463 if ((key >= '0') && (key <= '9'))
464 retval = key - '0';
465 else if ((key >= 'a') && (key <= 'z'))
466 retval = key + 10 - 'a';
467 else
468 retval = -1;
469 return retval;
470}
471
472/*
473 * get and put functions for the table, exposed to modules.
474 */
475struct sysrq_key_op *__sysrq_get_key_op(int key)
476{
477 struct sysrq_key_op *op_p = NULL;
478 int i;
479
480 i = sysrq_key_table_key2index(key);
481 if (i != -1)
482 op_p = sysrq_key_table[i];
483
484 return op_p;
485}
486
487static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
488{
489 int i = sysrq_key_table_key2index(key);
490
491 if (i != -1)
492 sysrq_key_table[i] = op_p;
493}
494
495void __handle_sysrq(int key, bool check_mask)
496{
497 struct sysrq_key_op *op_p;
498 int orig_log_level;
499 int i;
500 unsigned long flags;
501
502 spin_lock_irqsave(&sysrq_key_table_lock, flags);
503 /*
504 * Raise the apparent loglevel to maximum so that the sysrq header
505 * is shown to provide the user with positive feedback. We do not
506 * simply emit this at KERN_EMERG as that would change message
507 * routing in the consumers of /proc/kmsg.
508 */
509 orig_log_level = console_loglevel;
510 console_loglevel = 7;
511 printk(KERN_INFO "SysRq : ");
512
513 op_p = __sysrq_get_key_op(key);
514 if (op_p) {
515 /*
516 * Should we check for enabled operations (/proc/sysrq-trigger
517 * should not) and is the invoked operation enabled?
518 */
519 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
520 printk("%s\n", op_p->action_msg);
521 console_loglevel = orig_log_level;
522 op_p->handler(key);
523 } else {
524 printk("This sysrq operation is disabled.\n");
525 }
526 } else {
527 printk("HELP : ");
528 /* Only print the help msg once per handler */
529 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
530 if (sysrq_key_table[i]) {
531 int j;
532
533 for (j = 0; sysrq_key_table[i] !=
534 sysrq_key_table[j]; j++)
535 ;
536 if (j != i)
537 continue;
538 printk("%s ", sysrq_key_table[i]->help_msg);
539 }
540 }
541 printk("\n");
542 console_loglevel = orig_log_level;
543 }
544 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
545}
546
547void handle_sysrq(int key)
548{
549 if (sysrq_on())
550 __handle_sysrq(key, true);
551}
552EXPORT_SYMBOL(handle_sysrq);
553
554#ifdef CONFIG_INPUT
555
556/* Simple translation table for the SysRq keys */
557static const unsigned char sysrq_xlate[KEY_CNT] =
558 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
559 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
560 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
561 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
562 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
563 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
564 "\r\000/"; /* 0x60 - 0x6f */
565
566struct sysrq_state {
567 struct input_handle handle;
568 struct work_struct reinject_work;
569 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
570 unsigned int alt;
571 unsigned int alt_use;
572 bool active;
573 bool need_reinject;
574 bool reinjecting;
575};
576
577static void sysrq_reinject_alt_sysrq(struct work_struct *work)
578{
579 struct sysrq_state *sysrq =
580 container_of(work, struct sysrq_state, reinject_work);
581 struct input_handle *handle = &sysrq->handle;
582 unsigned int alt_code = sysrq->alt_use;
583
584 if (sysrq->need_reinject) {
585 /* we do not want the assignment to be reordered */
586 sysrq->reinjecting = true;
587 mb();
588
589 /* Simulate press and release of Alt + SysRq */
590 input_inject_event(handle, EV_KEY, alt_code, 1);
591 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
592 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
593
594 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
595 input_inject_event(handle, EV_KEY, alt_code, 0);
596 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
597
598 mb();
599 sysrq->reinjecting = false;
600 }
601}
602
603static bool sysrq_filter(struct input_handle *handle,
604 unsigned int type, unsigned int code, int value)
605{
606 struct sysrq_state *sysrq = handle->private;
607 bool was_active = sysrq->active;
608 bool suppress;
609
610 /*
611 * Do not filter anything if we are in the process of re-injecting
612 * Alt+SysRq combination.
613 */
614 if (sysrq->reinjecting)
615 return false;
616
617 switch (type) {
618
619 case EV_SYN:
620 suppress = false;
621 break;
622
623 case EV_KEY:
624 switch (code) {
625
626 case KEY_LEFTALT:
627 case KEY_RIGHTALT:
628 if (!value) {
629 /* One of ALTs is being released */
630 if (sysrq->active && code == sysrq->alt_use)
631 sysrq->active = false;
632
633 sysrq->alt = KEY_RESERVED;
634
635 } else if (value != 2) {
636 sysrq->alt = code;
637 sysrq->need_reinject = false;
638 }
639 break;
640
641 case KEY_SYSRQ:
642 if (value == 1 && sysrq->alt != KEY_RESERVED) {
643 sysrq->active = true;
644 sysrq->alt_use = sysrq->alt;
645 /*
646 * If nothing else will be pressed we'll need
647 * to re-inject Alt-SysRq keysroke.
648 */
649 sysrq->need_reinject = true;
650 }
651
652 /*
653 * Pretend that sysrq was never pressed at all. This
654 * is needed to properly handle KGDB which will try
655 * to release all keys after exiting debugger. If we
656 * do not clear key bit it KGDB will end up sending
657 * release events for Alt and SysRq, potentially
658 * triggering print screen function.
659 */
660 if (sysrq->active)
661 clear_bit(KEY_SYSRQ, handle->dev->key);
662
663 break;
664
665 default:
666 if (sysrq->active && value && value != 2) {
667 sysrq->need_reinject = false;
668 __handle_sysrq(sysrq_xlate[code], true);
669 }
670 break;
671 }
672
673 suppress = sysrq->active;
674
675 if (!sysrq->active) {
676 /*
677 * If we are not suppressing key presses keep track of
678 * keyboard state so we can release keys that have been
679 * pressed before entering SysRq mode.
680 */
681 if (value)
682 set_bit(code, sysrq->key_down);
683 else
684 clear_bit(code, sysrq->key_down);
685
686 if (was_active)
687 schedule_work(&sysrq->reinject_work);
688
689 } else if (value == 0 &&
690 test_and_clear_bit(code, sysrq->key_down)) {
691 /*
692 * Pass on release events for keys that was pressed before
693 * entering SysRq mode.
694 */
695 suppress = false;
696 }
697 break;
698
699 default:
700 suppress = sysrq->active;
701 break;
702 }
703
704 return suppress;
705}
706
707static int sysrq_connect(struct input_handler *handler,
708 struct input_dev *dev,
709 const struct input_device_id *id)
710{
711 struct sysrq_state *sysrq;
712 int error;
713
714 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
715 if (!sysrq)
716 return -ENOMEM;
717
718 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
719
720 sysrq->handle.dev = dev;
721 sysrq->handle.handler = handler;
722 sysrq->handle.name = "sysrq";
723 sysrq->handle.private = sysrq;
724
725 error = input_register_handle(&sysrq->handle);
726 if (error) {
727 pr_err("Failed to register input sysrq handler, error %d\n",
728 error);
729 goto err_free;
730 }
731
732 error = input_open_device(&sysrq->handle);
733 if (error) {
734 pr_err("Failed to open input device, error %d\n", error);
735 goto err_unregister;
736 }
737
738 return 0;
739
740 err_unregister:
741 input_unregister_handle(&sysrq->handle);
742 err_free:
743 kfree(sysrq);
744 return error;
745}
746
747static void sysrq_disconnect(struct input_handle *handle)
748{
749 struct sysrq_state *sysrq = handle->private;
750
751 input_close_device(handle);
752 cancel_work_sync(&sysrq->reinject_work);
753 input_unregister_handle(handle);
754 kfree(sysrq);
755}
756
757/*
758 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
759 * keyboards have SysRq key predefined and so user may add it to keymap
760 * later, but we expect all such keyboards to have left alt.
761 */
762static const struct input_device_id sysrq_ids[] = {
763 {
764 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
765 INPUT_DEVICE_ID_MATCH_KEYBIT,
766 .evbit = { BIT_MASK(EV_KEY) },
767 .keybit = { BIT_MASK(KEY_LEFTALT) },
768 },
769 { },
770};
771
772static struct input_handler sysrq_handler = {
773 .filter = sysrq_filter,
774 .connect = sysrq_connect,
775 .disconnect = sysrq_disconnect,
776 .name = "sysrq",
777 .id_table = sysrq_ids,
778};
779
780static bool sysrq_handler_registered;
781
782static inline void sysrq_register_handler(void)
783{
784 int error;
785
786 error = input_register_handler(&sysrq_handler);
787 if (error)
788 pr_err("Failed to register input handler, error %d", error);
789 else
790 sysrq_handler_registered = true;
791}
792
793static inline void sysrq_unregister_handler(void)
794{
795 if (sysrq_handler_registered) {
796 input_unregister_handler(&sysrq_handler);
797 sysrq_handler_registered = false;
798 }
799}
800
801#else
802
803static inline void sysrq_register_handler(void)
804{
805}
806
807static inline void sysrq_unregister_handler(void)
808{
809}
810
811#endif /* CONFIG_INPUT */
812
813int sysrq_toggle_support(int enable_mask)
814{
815 bool was_enabled = sysrq_on();
816
817 sysrq_enabled = enable_mask;
818
819 if (was_enabled != sysrq_on()) {
820 if (sysrq_on())
821 sysrq_register_handler();
822 else
823 sysrq_unregister_handler();
824 }
825
826 return 0;
827}
828
829static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
830 struct sysrq_key_op *remove_op_p)
831{
832 int retval;
833 unsigned long flags;
834
835 spin_lock_irqsave(&sysrq_key_table_lock, flags);
836 if (__sysrq_get_key_op(key) == remove_op_p) {
837 __sysrq_put_key_op(key, insert_op_p);
838 retval = 0;
839 } else {
840 retval = -1;
841 }
842 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
843 return retval;
844}
845
846int register_sysrq_key(int key, struct sysrq_key_op *op_p)
847{
848 return __sysrq_swap_key_ops(key, op_p, NULL);
849}
850EXPORT_SYMBOL(register_sysrq_key);
851
852int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
853{
854 return __sysrq_swap_key_ops(key, NULL, op_p);
855}
856EXPORT_SYMBOL(unregister_sysrq_key);
857
858#ifdef CONFIG_PROC_FS
859/*
860 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
861 */
862static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
863 size_t count, loff_t *ppos)
864{
865 if (count) {
866 char c;
867
868 if (get_user(c, buf))
869 return -EFAULT;
870 __handle_sysrq(c, false);
871 }
872
873 return count;
874}
875
876static const struct file_operations proc_sysrq_trigger_operations = {
877 .write = write_sysrq_trigger,
878 .llseek = noop_llseek,
879};
880
881static void sysrq_init_procfs(void)
882{
883 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
884 &proc_sysrq_trigger_operations))
885 pr_err("Failed to register proc interface\n");
886}
887
888#else
889
890static inline void sysrq_init_procfs(void)
891{
892}
893
894#endif /* CONFIG_PROC_FS */
895
896static int __init sysrq_init(void)
897{
898 sysrq_init_procfs();
899
900 if (sysrq_on())
901 sysrq_register_handler();
902
903 return 0;
904}
905module_init(sysrq_init);
diff --git a/drivers/tty/tty_audit.c b/drivers/tty/tty_audit.c
new file mode 100644
index 000000000000..7c5866920622
--- /dev/null
+++ b/drivers/tty/tty_audit.c
@@ -0,0 +1,360 @@
1/*
2 * Creating audit events from TTY input.
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted
5 * material is made available to anyone wishing to use, modify, copy, or
6 * redistribute it subject to the terms and conditions of the GNU General
7 * Public License v.2.
8 *
9 * Authors: Miloslav Trmac <mitr@redhat.com>
10 */
11
12#include <linux/audit.h>
13#include <linux/slab.h>
14#include <linux/tty.h>
15
16struct tty_audit_buf {
17 atomic_t count;
18 struct mutex mutex; /* Protects all data below */
19 int major, minor; /* The TTY which the data is from */
20 unsigned icanon:1;
21 size_t valid;
22 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
23};
24
25static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
26 int icanon)
27{
28 struct tty_audit_buf *buf;
29
30 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
31 if (!buf)
32 goto err;
33 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
34 if (!buf->data)
35 goto err_buf;
36 atomic_set(&buf->count, 1);
37 mutex_init(&buf->mutex);
38 buf->major = major;
39 buf->minor = minor;
40 buf->icanon = icanon;
41 buf->valid = 0;
42 return buf;
43
44err_buf:
45 kfree(buf);
46err:
47 return NULL;
48}
49
50static void tty_audit_buf_free(struct tty_audit_buf *buf)
51{
52 WARN_ON(buf->valid != 0);
53 kfree(buf->data);
54 kfree(buf);
55}
56
57static void tty_audit_buf_put(struct tty_audit_buf *buf)
58{
59 if (atomic_dec_and_test(&buf->count))
60 tty_audit_buf_free(buf);
61}
62
63static void tty_audit_log(const char *description, struct task_struct *tsk,
64 uid_t loginuid, unsigned sessionid, int major,
65 int minor, unsigned char *data, size_t size)
66{
67 struct audit_buffer *ab;
68
69 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
70 if (ab) {
71 char name[sizeof(tsk->comm)];
72 uid_t uid = task_uid(tsk);
73
74 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
75 "major=%d minor=%d comm=", description,
76 tsk->pid, uid, loginuid, sessionid,
77 major, minor);
78 get_task_comm(name, tsk);
79 audit_log_untrustedstring(ab, name);
80 audit_log_format(ab, " data=");
81 audit_log_n_hex(ab, data, size);
82 audit_log_end(ab);
83 }
84}
85
86/**
87 * tty_audit_buf_push - Push buffered data out
88 *
89 * Generate an audit message from the contents of @buf, which is owned by
90 * @tsk with @loginuid. @buf->mutex must be locked.
91 */
92static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
93 unsigned int sessionid,
94 struct tty_audit_buf *buf)
95{
96 if (buf->valid == 0)
97 return;
98 if (audit_enabled == 0) {
99 buf->valid = 0;
100 return;
101 }
102 tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
103 buf->data, buf->valid);
104 buf->valid = 0;
105}
106
107/**
108 * tty_audit_buf_push_current - Push buffered data out
109 *
110 * Generate an audit message from the contents of @buf, which is owned by
111 * the current task. @buf->mutex must be locked.
112 */
113static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
114{
115 uid_t auid = audit_get_loginuid(current);
116 unsigned int sessionid = audit_get_sessionid(current);
117 tty_audit_buf_push(current, auid, sessionid, buf);
118}
119
120/**
121 * tty_audit_exit - Handle a task exit
122 *
123 * Make sure all buffered data is written out and deallocate the buffer.
124 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
125 */
126void tty_audit_exit(void)
127{
128 struct tty_audit_buf *buf;
129
130 spin_lock_irq(&current->sighand->siglock);
131 buf = current->signal->tty_audit_buf;
132 current->signal->tty_audit_buf = NULL;
133 spin_unlock_irq(&current->sighand->siglock);
134 if (!buf)
135 return;
136
137 mutex_lock(&buf->mutex);
138 tty_audit_buf_push_current(buf);
139 mutex_unlock(&buf->mutex);
140
141 tty_audit_buf_put(buf);
142}
143
144/**
145 * tty_audit_fork - Copy TTY audit state for a new task
146 *
147 * Set up TTY audit state in @sig from current. @sig needs no locking.
148 */
149void tty_audit_fork(struct signal_struct *sig)
150{
151 spin_lock_irq(&current->sighand->siglock);
152 sig->audit_tty = current->signal->audit_tty;
153 spin_unlock_irq(&current->sighand->siglock);
154}
155
156/**
157 * tty_audit_tiocsti - Log TIOCSTI
158 */
159void tty_audit_tiocsti(struct tty_struct *tty, char ch)
160{
161 struct tty_audit_buf *buf;
162 int major, minor, should_audit;
163
164 spin_lock_irq(&current->sighand->siglock);
165 should_audit = current->signal->audit_tty;
166 buf = current->signal->tty_audit_buf;
167 if (buf)
168 atomic_inc(&buf->count);
169 spin_unlock_irq(&current->sighand->siglock);
170
171 major = tty->driver->major;
172 minor = tty->driver->minor_start + tty->index;
173 if (buf) {
174 mutex_lock(&buf->mutex);
175 if (buf->major == major && buf->minor == minor)
176 tty_audit_buf_push_current(buf);
177 mutex_unlock(&buf->mutex);
178 tty_audit_buf_put(buf);
179 }
180
181 if (should_audit && audit_enabled) {
182 uid_t auid;
183 unsigned int sessionid;
184
185 auid = audit_get_loginuid(current);
186 sessionid = audit_get_sessionid(current);
187 tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
188 minor, &ch, 1);
189 }
190}
191
192/**
193 * tty_audit_push_task - Flush task's pending audit data
194 * @tsk: task pointer
195 * @loginuid: sender login uid
196 * @sessionid: sender session id
197 *
198 * Called with a ref on @tsk held. Try to lock sighand and get a
199 * reference to the tty audit buffer if available.
200 * Flush the buffer or return an appropriate error code.
201 */
202int tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
203{
204 struct tty_audit_buf *buf = ERR_PTR(-EPERM);
205 unsigned long flags;
206
207 if (!lock_task_sighand(tsk, &flags))
208 return -ESRCH;
209
210 if (tsk->signal->audit_tty) {
211 buf = tsk->signal->tty_audit_buf;
212 if (buf)
213 atomic_inc(&buf->count);
214 }
215 unlock_task_sighand(tsk, &flags);
216
217 /*
218 * Return 0 when signal->audit_tty set
219 * but tsk->signal->tty_audit_buf == NULL.
220 */
221 if (!buf || IS_ERR(buf))
222 return PTR_ERR(buf);
223
224 mutex_lock(&buf->mutex);
225 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
226 mutex_unlock(&buf->mutex);
227
228 tty_audit_buf_put(buf);
229 return 0;
230}
231
232/**
233 * tty_audit_buf_get - Get an audit buffer.
234 *
235 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
236 * if TTY auditing is disabled or out of memory. Otherwise, return a new
237 * reference to the buffer.
238 */
239static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
240{
241 struct tty_audit_buf *buf, *buf2;
242
243 buf = NULL;
244 buf2 = NULL;
245 spin_lock_irq(&current->sighand->siglock);
246 if (likely(!current->signal->audit_tty))
247 goto out;
248 buf = current->signal->tty_audit_buf;
249 if (buf) {
250 atomic_inc(&buf->count);
251 goto out;
252 }
253 spin_unlock_irq(&current->sighand->siglock);
254
255 buf2 = tty_audit_buf_alloc(tty->driver->major,
256 tty->driver->minor_start + tty->index,
257 tty->icanon);
258 if (buf2 == NULL) {
259 audit_log_lost("out of memory in TTY auditing");
260 return NULL;
261 }
262
263 spin_lock_irq(&current->sighand->siglock);
264 if (!current->signal->audit_tty)
265 goto out;
266 buf = current->signal->tty_audit_buf;
267 if (!buf) {
268 current->signal->tty_audit_buf = buf2;
269 buf = buf2;
270 buf2 = NULL;
271 }
272 atomic_inc(&buf->count);
273 /* Fall through */
274 out:
275 spin_unlock_irq(&current->sighand->siglock);
276 if (buf2)
277 tty_audit_buf_free(buf2);
278 return buf;
279}
280
281/**
282 * tty_audit_add_data - Add data for TTY auditing.
283 *
284 * Audit @data of @size from @tty, if necessary.
285 */
286void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
287 size_t size)
288{
289 struct tty_audit_buf *buf;
290 int major, minor;
291
292 if (unlikely(size == 0))
293 return;
294
295 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
296 && tty->driver->subtype == PTY_TYPE_MASTER)
297 return;
298
299 buf = tty_audit_buf_get(tty);
300 if (!buf)
301 return;
302
303 mutex_lock(&buf->mutex);
304 major = tty->driver->major;
305 minor = tty->driver->minor_start + tty->index;
306 if (buf->major != major || buf->minor != minor
307 || buf->icanon != tty->icanon) {
308 tty_audit_buf_push_current(buf);
309 buf->major = major;
310 buf->minor = minor;
311 buf->icanon = tty->icanon;
312 }
313 do {
314 size_t run;
315
316 run = N_TTY_BUF_SIZE - buf->valid;
317 if (run > size)
318 run = size;
319 memcpy(buf->data + buf->valid, data, run);
320 buf->valid += run;
321 data += run;
322 size -= run;
323 if (buf->valid == N_TTY_BUF_SIZE)
324 tty_audit_buf_push_current(buf);
325 } while (size != 0);
326 mutex_unlock(&buf->mutex);
327 tty_audit_buf_put(buf);
328}
329
330/**
331 * tty_audit_push - Push buffered data out
332 *
333 * Make sure no audit data is pending for @tty on the current process.
334 */
335void tty_audit_push(struct tty_struct *tty)
336{
337 struct tty_audit_buf *buf;
338
339 spin_lock_irq(&current->sighand->siglock);
340 if (likely(!current->signal->audit_tty)) {
341 spin_unlock_irq(&current->sighand->siglock);
342 return;
343 }
344 buf = current->signal->tty_audit_buf;
345 if (buf)
346 atomic_inc(&buf->count);
347 spin_unlock_irq(&current->sighand->siglock);
348
349 if (buf) {
350 int major, minor;
351
352 major = tty->driver->major;
353 minor = tty->driver->minor_start + tty->index;
354 mutex_lock(&buf->mutex);
355 if (buf->major == major && buf->minor == minor)
356 tty_audit_buf_push_current(buf);
357 mutex_unlock(&buf->mutex);
358 tty_audit_buf_put(buf);
359 }
360}
diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
new file mode 100644
index 000000000000..6c9b7cd6778a
--- /dev/null
+++ b/drivers/tty/tty_buffer.c
@@ -0,0 +1,522 @@
1/*
2 * Tty buffer allocation management
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
19
20/**
21 * tty_buffer_free_all - free buffers used by a tty
22 * @tty: tty to free from
23 *
24 * Remove all the buffers pending on a tty whether queued with data
25 * or in the free ring. Must be called when the tty is no longer in use
26 *
27 * Locking: none
28 */
29
30void tty_buffer_free_all(struct tty_struct *tty)
31{
32 struct tty_buffer *thead;
33 while ((thead = tty->buf.head) != NULL) {
34 tty->buf.head = thead->next;
35 kfree(thead);
36 }
37 while ((thead = tty->buf.free) != NULL) {
38 tty->buf.free = thead->next;
39 kfree(thead);
40 }
41 tty->buf.tail = NULL;
42 tty->buf.memory_used = 0;
43}
44
45/**
46 * tty_buffer_alloc - allocate a tty buffer
47 * @tty: tty device
48 * @size: desired size (characters)
49 *
50 * Allocate a new tty buffer to hold the desired number of characters.
51 * Return NULL if out of memory or the allocation would exceed the
52 * per device queue
53 *
54 * Locking: Caller must hold tty->buf.lock
55 */
56
57static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
58{
59 struct tty_buffer *p;
60
61 if (tty->buf.memory_used + size > 65536)
62 return NULL;
63 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
64 if (p == NULL)
65 return NULL;
66 p->used = 0;
67 p->size = size;
68 p->next = NULL;
69 p->commit = 0;
70 p->read = 0;
71 p->char_buf_ptr = (char *)(p->data);
72 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
73 tty->buf.memory_used += size;
74 return p;
75}
76
77/**
78 * tty_buffer_free - free a tty buffer
79 * @tty: tty owning the buffer
80 * @b: the buffer to free
81 *
82 * Free a tty buffer, or add it to the free list according to our
83 * internal strategy
84 *
85 * Locking: Caller must hold tty->buf.lock
86 */
87
88static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
89{
90 /* Dumb strategy for now - should keep some stats */
91 tty->buf.memory_used -= b->size;
92 WARN_ON(tty->buf.memory_used < 0);
93
94 if (b->size >= 512)
95 kfree(b);
96 else {
97 b->next = tty->buf.free;
98 tty->buf.free = b;
99 }
100}
101
102/**
103 * __tty_buffer_flush - flush full tty buffers
104 * @tty: tty to flush
105 *
106 * flush all the buffers containing receive data. Caller must
107 * hold the buffer lock and must have ensured no parallel flush to
108 * ldisc is running.
109 *
110 * Locking: Caller must hold tty->buf.lock
111 */
112
113static void __tty_buffer_flush(struct tty_struct *tty)
114{
115 struct tty_buffer *thead;
116
117 while ((thead = tty->buf.head) != NULL) {
118 tty->buf.head = thead->next;
119 tty_buffer_free(tty, thead);
120 }
121 tty->buf.tail = NULL;
122}
123
124/**
125 * tty_buffer_flush - flush full tty buffers
126 * @tty: tty to flush
127 *
128 * flush all the buffers containing receive data. If the buffer is
129 * being processed by flush_to_ldisc then we defer the processing
130 * to that function
131 *
132 * Locking: none
133 */
134
135void tty_buffer_flush(struct tty_struct *tty)
136{
137 unsigned long flags;
138 spin_lock_irqsave(&tty->buf.lock, flags);
139
140 /* If the data is being pushed to the tty layer then we can't
141 process it here. Instead set a flag and the flush_to_ldisc
142 path will process the flush request before it exits */
143 if (test_bit(TTY_FLUSHING, &tty->flags)) {
144 set_bit(TTY_FLUSHPENDING, &tty->flags);
145 spin_unlock_irqrestore(&tty->buf.lock, flags);
146 wait_event(tty->read_wait,
147 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
148 return;
149 } else
150 __tty_buffer_flush(tty);
151 spin_unlock_irqrestore(&tty->buf.lock, flags);
152}
153
154/**
155 * tty_buffer_find - find a free tty buffer
156 * @tty: tty owning the buffer
157 * @size: characters wanted
158 *
159 * Locate an existing suitable tty buffer or if we are lacking one then
160 * allocate a new one. We round our buffers off in 256 character chunks
161 * to get better allocation behaviour.
162 *
163 * Locking: Caller must hold tty->buf.lock
164 */
165
166static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
167{
168 struct tty_buffer **tbh = &tty->buf.free;
169 while ((*tbh) != NULL) {
170 struct tty_buffer *t = *tbh;
171 if (t->size >= size) {
172 *tbh = t->next;
173 t->next = NULL;
174 t->used = 0;
175 t->commit = 0;
176 t->read = 0;
177 tty->buf.memory_used += t->size;
178 return t;
179 }
180 tbh = &((*tbh)->next);
181 }
182 /* Round the buffer size out */
183 size = (size + 0xFF) & ~0xFF;
184 return tty_buffer_alloc(tty, size);
185 /* Should possibly check if this fails for the largest buffer we
186 have queued and recycle that ? */
187}
188
189/**
190 * tty_buffer_request_room - grow tty buffer if needed
191 * @tty: tty structure
192 * @size: size desired
193 *
194 * Make at least size bytes of linear space available for the tty
195 * buffer. If we fail return the size we managed to find.
196 *
197 * Locking: Takes tty->buf.lock
198 */
199int tty_buffer_request_room(struct tty_struct *tty, size_t size)
200{
201 struct tty_buffer *b, *n;
202 int left;
203 unsigned long flags;
204
205 spin_lock_irqsave(&tty->buf.lock, flags);
206
207 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
208 remove this conditional if its worth it. This would be invisible
209 to the callers */
210 if ((b = tty->buf.tail) != NULL)
211 left = b->size - b->used;
212 else
213 left = 0;
214
215 if (left < size) {
216 /* This is the slow path - looking for new buffers to use */
217 if ((n = tty_buffer_find(tty, size)) != NULL) {
218 if (b != NULL) {
219 b->next = n;
220 b->commit = b->used;
221 } else
222 tty->buf.head = n;
223 tty->buf.tail = n;
224 } else
225 size = left;
226 }
227
228 spin_unlock_irqrestore(&tty->buf.lock, flags);
229 return size;
230}
231EXPORT_SYMBOL_GPL(tty_buffer_request_room);
232
233/**
234 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
235 * @tty: tty structure
236 * @chars: characters
237 * @flag: flag value for each character
238 * @size: size
239 *
240 * Queue a series of bytes to the tty buffering. All the characters
241 * passed are marked with the supplied flag. Returns the number added.
242 *
243 * Locking: Called functions may take tty->buf.lock
244 */
245
246int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
247 const unsigned char *chars, char flag, size_t size)
248{
249 int copied = 0;
250 do {
251 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
252 int space = tty_buffer_request_room(tty, goal);
253 struct tty_buffer *tb = tty->buf.tail;
254 /* If there is no space then tb may be NULL */
255 if (unlikely(space == 0))
256 break;
257 memcpy(tb->char_buf_ptr + tb->used, chars, space);
258 memset(tb->flag_buf_ptr + tb->used, flag, space);
259 tb->used += space;
260 copied += space;
261 chars += space;
262 /* There is a small chance that we need to split the data over
263 several buffers. If this is the case we must loop */
264 } while (unlikely(size > copied));
265 return copied;
266}
267EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
268
269/**
270 * tty_insert_flip_string_flags - Add characters to the tty buffer
271 * @tty: tty structure
272 * @chars: characters
273 * @flags: flag bytes
274 * @size: size
275 *
276 * Queue a series of bytes to the tty buffering. For each character
277 * the flags array indicates the status of the character. Returns the
278 * number added.
279 *
280 * Locking: Called functions may take tty->buf.lock
281 */
282
283int tty_insert_flip_string_flags(struct tty_struct *tty,
284 const unsigned char *chars, const char *flags, size_t size)
285{
286 int copied = 0;
287 do {
288 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
289 int space = tty_buffer_request_room(tty, goal);
290 struct tty_buffer *tb = tty->buf.tail;
291 /* If there is no space then tb may be NULL */
292 if (unlikely(space == 0))
293 break;
294 memcpy(tb->char_buf_ptr + tb->used, chars, space);
295 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
296 tb->used += space;
297 copied += space;
298 chars += space;
299 flags += space;
300 /* There is a small chance that we need to split the data over
301 several buffers. If this is the case we must loop */
302 } while (unlikely(size > copied));
303 return copied;
304}
305EXPORT_SYMBOL(tty_insert_flip_string_flags);
306
307/**
308 * tty_schedule_flip - push characters to ldisc
309 * @tty: tty to push from
310 *
311 * Takes any pending buffers and transfers their ownership to the
312 * ldisc side of the queue. It then schedules those characters for
313 * processing by the line discipline.
314 *
315 * Locking: Takes tty->buf.lock
316 */
317
318void tty_schedule_flip(struct tty_struct *tty)
319{
320 unsigned long flags;
321 spin_lock_irqsave(&tty->buf.lock, flags);
322 if (tty->buf.tail != NULL)
323 tty->buf.tail->commit = tty->buf.tail->used;
324 spin_unlock_irqrestore(&tty->buf.lock, flags);
325 schedule_work(&tty->buf.work);
326}
327EXPORT_SYMBOL(tty_schedule_flip);
328
329/**
330 * tty_prepare_flip_string - make room for characters
331 * @tty: tty
332 * @chars: return pointer for character write area
333 * @size: desired size
334 *
335 * Prepare a block of space in the buffer for data. Returns the length
336 * available and buffer pointer to the space which is now allocated and
337 * accounted for as ready for normal characters. This is used for drivers
338 * that need their own block copy routines into the buffer. There is no
339 * guarantee the buffer is a DMA target!
340 *
341 * Locking: May call functions taking tty->buf.lock
342 */
343
344int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
345 size_t size)
346{
347 int space = tty_buffer_request_room(tty, size);
348 if (likely(space)) {
349 struct tty_buffer *tb = tty->buf.tail;
350 *chars = tb->char_buf_ptr + tb->used;
351 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
352 tb->used += space;
353 }
354 return space;
355}
356EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
357
358/**
359 * tty_prepare_flip_string_flags - make room for characters
360 * @tty: tty
361 * @chars: return pointer for character write area
362 * @flags: return pointer for status flag write area
363 * @size: desired size
364 *
365 * Prepare a block of space in the buffer for data. Returns the length
366 * available and buffer pointer to the space which is now allocated and
367 * accounted for as ready for characters. This is used for drivers
368 * that need their own block copy routines into the buffer. There is no
369 * guarantee the buffer is a DMA target!
370 *
371 * Locking: May call functions taking tty->buf.lock
372 */
373
374int tty_prepare_flip_string_flags(struct tty_struct *tty,
375 unsigned char **chars, char **flags, size_t size)
376{
377 int space = tty_buffer_request_room(tty, size);
378 if (likely(space)) {
379 struct tty_buffer *tb = tty->buf.tail;
380 *chars = tb->char_buf_ptr + tb->used;
381 *flags = tb->flag_buf_ptr + tb->used;
382 tb->used += space;
383 }
384 return space;
385}
386EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
387
388
389
390/**
391 * flush_to_ldisc
392 * @work: tty structure passed from work queue.
393 *
394 * This routine is called out of the software interrupt to flush data
395 * from the buffer chain to the line discipline.
396 *
397 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
398 * while invoking the line discipline receive_buf method. The
399 * receive_buf method is single threaded for each tty instance.
400 */
401
402static void flush_to_ldisc(struct work_struct *work)
403{
404 struct tty_struct *tty =
405 container_of(work, struct tty_struct, buf.work);
406 unsigned long flags;
407 struct tty_ldisc *disc;
408
409 disc = tty_ldisc_ref(tty);
410 if (disc == NULL) /* !TTY_LDISC */
411 return;
412
413 spin_lock_irqsave(&tty->buf.lock, flags);
414
415 if (!test_and_set_bit(TTY_FLUSHING, &tty->flags)) {
416 struct tty_buffer *head;
417 while ((head = tty->buf.head) != NULL) {
418 int count;
419 char *char_buf;
420 unsigned char *flag_buf;
421
422 count = head->commit - head->read;
423 if (!count) {
424 if (head->next == NULL)
425 break;
426 tty->buf.head = head->next;
427 tty_buffer_free(tty, head);
428 continue;
429 }
430 /* Ldisc or user is trying to flush the buffers
431 we are feeding to the ldisc, stop feeding the
432 line discipline as we want to empty the queue */
433 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
434 break;
435 if (!tty->receive_room)
436 break;
437 if (count > tty->receive_room)
438 count = tty->receive_room;
439 char_buf = head->char_buf_ptr + head->read;
440 flag_buf = head->flag_buf_ptr + head->read;
441 head->read += count;
442 spin_unlock_irqrestore(&tty->buf.lock, flags);
443 disc->ops->receive_buf(tty, char_buf,
444 flag_buf, count);
445 spin_lock_irqsave(&tty->buf.lock, flags);
446 }
447 clear_bit(TTY_FLUSHING, &tty->flags);
448 }
449
450 /* We may have a deferred request to flush the input buffer,
451 if so pull the chain under the lock and empty the queue */
452 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
453 __tty_buffer_flush(tty);
454 clear_bit(TTY_FLUSHPENDING, &tty->flags);
455 wake_up(&tty->read_wait);
456 }
457 spin_unlock_irqrestore(&tty->buf.lock, flags);
458
459 tty_ldisc_deref(disc);
460}
461
462/**
463 * tty_flush_to_ldisc
464 * @tty: tty to push
465 *
466 * Push the terminal flip buffers to the line discipline.
467 *
468 * Must not be called from IRQ context.
469 */
470void tty_flush_to_ldisc(struct tty_struct *tty)
471{
472 flush_work(&tty->buf.work);
473}
474
475/**
476 * tty_flip_buffer_push - terminal
477 * @tty: tty to push
478 *
479 * Queue a push of the terminal flip buffers to the line discipline. This
480 * function must not be called from IRQ context if tty->low_latency is set.
481 *
482 * In the event of the queue being busy for flipping the work will be
483 * held off and retried later.
484 *
485 * Locking: tty buffer lock. Driver locks in low latency mode.
486 */
487
488void tty_flip_buffer_push(struct tty_struct *tty)
489{
490 unsigned long flags;
491 spin_lock_irqsave(&tty->buf.lock, flags);
492 if (tty->buf.tail != NULL)
493 tty->buf.tail->commit = tty->buf.tail->used;
494 spin_unlock_irqrestore(&tty->buf.lock, flags);
495
496 if (tty->low_latency)
497 flush_to_ldisc(&tty->buf.work);
498 else
499 schedule_work(&tty->buf.work);
500}
501EXPORT_SYMBOL(tty_flip_buffer_push);
502
503/**
504 * tty_buffer_init - prepare a tty buffer structure
505 * @tty: tty to initialise
506 *
507 * Set up the initial state of the buffer management for a tty device.
508 * Must be called before the other tty buffer functions are used.
509 *
510 * Locking: none
511 */
512
513void tty_buffer_init(struct tty_struct *tty)
514{
515 spin_lock_init(&tty->buf.lock);
516 tty->buf.head = NULL;
517 tty->buf.tail = NULL;
518 tty->buf.free = NULL;
519 tty->buf.memory_used = 0;
520 INIT_WORK(&tty->buf.work, flush_to_ldisc);
521}
522
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
new file mode 100644
index 000000000000..6556f7452ba6
--- /dev/null
+++ b/drivers/tty/tty_io.c
@@ -0,0 +1,3334 @@
1/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
7 * or rs-channels. It also implements echoing, cooked mode etc.
8 *
9 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
10 *
11 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
12 * tty_struct and tty_queue structures. Previously there was an array
13 * of 256 tty_struct's which was statically allocated, and the
14 * tty_queue structures were allocated at boot time. Both are now
15 * dynamically allocated only when the tty is open.
16 *
17 * Also restructured routines so that there is more of a separation
18 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
19 * the low-level tty routines (serial.c, pty.c, console.c). This
20 * makes for cleaner and more compact code. -TYT, 9/17/92
21 *
22 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
23 * which can be dynamically activated and de-activated by the line
24 * discipline handling modules (like SLIP).
25 *
26 * NOTE: pay no attention to the line discipline code (yet); its
27 * interface is still subject to change in this version...
28 * -- TYT, 1/31/92
29 *
30 * Added functionality to the OPOST tty handling. No delays, but all
31 * other bits should be there.
32 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
33 *
34 * Rewrote canonical mode and added more termios flags.
35 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
36 *
37 * Reorganized FASYNC support so mouse code can share it.
38 * -- ctm@ardi.com, 9Sep95
39 *
40 * New TIOCLINUX variants added.
41 * -- mj@k332.feld.cvut.cz, 19-Nov-95
42 *
43 * Restrict vt switching via ioctl()
44 * -- grif@cs.ucr.edu, 5-Dec-95
45 *
46 * Move console and virtual terminal code to more appropriate files,
47 * implement CONFIG_VT and generalize console device interface.
48 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
49 *
50 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
51 * -- Bill Hawes <whawes@star.net>, June 97
52 *
53 * Added devfs support.
54 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
55 *
56 * Added support for a Unix98-style ptmx device.
57 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
58 *
59 * Reduced memory usage for older ARM systems
60 * -- Russell King <rmk@arm.linux.org.uk>
61 *
62 * Move do_SAK() into process context. Less stack use in devfs functions.
63 * alloc_tty_struct() always uses kmalloc()
64 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
65 */
66
67#include <linux/types.h>
68#include <linux/major.h>
69#include <linux/errno.h>
70#include <linux/signal.h>
71#include <linux/fcntl.h>
72#include <linux/sched.h>
73#include <linux/interrupt.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/devpts_fs.h>
78#include <linux/file.h>
79#include <linux/fdtable.h>
80#include <linux/console.h>
81#include <linux/timer.h>
82#include <linux/ctype.h>
83#include <linux/kd.h>
84#include <linux/mm.h>
85#include <linux/string.h>
86#include <linux/slab.h>
87#include <linux/poll.h>
88#include <linux/proc_fs.h>
89#include <linux/init.h>
90#include <linux/module.h>
91#include <linux/device.h>
92#include <linux/wait.h>
93#include <linux/bitops.h>
94#include <linux/delay.h>
95#include <linux/seq_file.h>
96#include <linux/serial.h>
97
98#include <linux/uaccess.h>
99#include <asm/system.h>
100
101#include <linux/kbd_kern.h>
102#include <linux/vt_kern.h>
103#include <linux/selection.h>
104
105#include <linux/kmod.h>
106#include <linux/nsproxy.h>
107
108#undef TTY_DEBUG_HANGUP
109
110#define TTY_PARANOIA_CHECK 1
111#define CHECK_TTY_COUNT 1
112
113struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
119 .c_cc = INIT_C_CC,
120 .c_ispeed = 38400,
121 .c_ospeed = 38400
122};
123
124EXPORT_SYMBOL(tty_std_termios);
125
126/* This list gets poked at by procfs and various bits of boot up code. This
127 could do with some rationalisation such as pulling the tty proc function
128 into this file */
129
130LIST_HEAD(tty_drivers); /* linked list of tty drivers */
131
132/* Mutex to protect creating and releasing a tty. This is shared with
133 vt.c for deeply disgusting hack reasons */
134DEFINE_MUTEX(tty_mutex);
135EXPORT_SYMBOL(tty_mutex);
136
137/* Spinlock to protect the tty->tty_files list */
138DEFINE_SPINLOCK(tty_files_lock);
139
140static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
141static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
142ssize_t redirected_tty_write(struct file *, const char __user *,
143 size_t, loff_t *);
144static unsigned int tty_poll(struct file *, poll_table *);
145static int tty_open(struct inode *, struct file *);
146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
147#ifdef CONFIG_COMPAT
148static long tty_compat_ioctl(struct file *file, unsigned int cmd,
149 unsigned long arg);
150#else
151#define tty_compat_ioctl NULL
152#endif
153static int __tty_fasync(int fd, struct file *filp, int on);
154static int tty_fasync(int fd, struct file *filp, int on);
155static void release_tty(struct tty_struct *tty, int idx);
156static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
157static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
158
159/**
160 * alloc_tty_struct - allocate a tty object
161 *
162 * Return a new empty tty structure. The data fields have not
163 * been initialized in any way but has been zeroed
164 *
165 * Locking: none
166 */
167
168struct tty_struct *alloc_tty_struct(void)
169{
170 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
171}
172
173/**
174 * free_tty_struct - free a disused tty
175 * @tty: tty struct to free
176 *
177 * Free the write buffers, tty queue and tty memory itself.
178 *
179 * Locking: none. Must be called after tty is definitely unused
180 */
181
182void free_tty_struct(struct tty_struct *tty)
183{
184 if (tty->dev)
185 put_device(tty->dev);
186 kfree(tty->write_buf);
187 tty_buffer_free_all(tty);
188 kfree(tty);
189}
190
191static inline struct tty_struct *file_tty(struct file *file)
192{
193 return ((struct tty_file_private *)file->private_data)->tty;
194}
195
196/* Associate a new file with the tty structure */
197int tty_add_file(struct tty_struct *tty, struct file *file)
198{
199 struct tty_file_private *priv;
200
201 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
202 if (!priv)
203 return -ENOMEM;
204
205 priv->tty = tty;
206 priv->file = file;
207 file->private_data = priv;
208
209 spin_lock(&tty_files_lock);
210 list_add(&priv->list, &tty->tty_files);
211 spin_unlock(&tty_files_lock);
212
213 return 0;
214}
215
216/* Delete file from its tty */
217void tty_del_file(struct file *file)
218{
219 struct tty_file_private *priv = file->private_data;
220
221 spin_lock(&tty_files_lock);
222 list_del(&priv->list);
223 spin_unlock(&tty_files_lock);
224 file->private_data = NULL;
225 kfree(priv);
226}
227
228
229#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
230
231/**
232 * tty_name - return tty naming
233 * @tty: tty structure
234 * @buf: buffer for output
235 *
236 * Convert a tty structure into a name. The name reflects the kernel
237 * naming policy and if udev is in use may not reflect user space
238 *
239 * Locking: none
240 */
241
242char *tty_name(struct tty_struct *tty, char *buf)
243{
244 if (!tty) /* Hmm. NULL pointer. That's fun. */
245 strcpy(buf, "NULL tty");
246 else
247 strcpy(buf, tty->name);
248 return buf;
249}
250
251EXPORT_SYMBOL(tty_name);
252
253int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
254 const char *routine)
255{
256#ifdef TTY_PARANOIA_CHECK
257 if (!tty) {
258 printk(KERN_WARNING
259 "null TTY for (%d:%d) in %s\n",
260 imajor(inode), iminor(inode), routine);
261 return 1;
262 }
263 if (tty->magic != TTY_MAGIC) {
264 printk(KERN_WARNING
265 "bad magic number for tty struct (%d:%d) in %s\n",
266 imajor(inode), iminor(inode), routine);
267 return 1;
268 }
269#endif
270 return 0;
271}
272
273static int check_tty_count(struct tty_struct *tty, const char *routine)
274{
275#ifdef CHECK_TTY_COUNT
276 struct list_head *p;
277 int count = 0;
278
279 spin_lock(&tty_files_lock);
280 list_for_each(p, &tty->tty_files) {
281 count++;
282 }
283 spin_unlock(&tty_files_lock);
284 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
285 tty->driver->subtype == PTY_TYPE_SLAVE &&
286 tty->link && tty->link->count)
287 count++;
288 if (tty->count != count) {
289 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
290 "!= #fd's(%d) in %s\n",
291 tty->name, tty->count, count, routine);
292 return count;
293 }
294#endif
295 return 0;
296}
297
298/**
299 * get_tty_driver - find device of a tty
300 * @dev_t: device identifier
301 * @index: returns the index of the tty
302 *
303 * This routine returns a tty driver structure, given a device number
304 * and also passes back the index number.
305 *
306 * Locking: caller must hold tty_mutex
307 */
308
309static struct tty_driver *get_tty_driver(dev_t device, int *index)
310{
311 struct tty_driver *p;
312
313 list_for_each_entry(p, &tty_drivers, tty_drivers) {
314 dev_t base = MKDEV(p->major, p->minor_start);
315 if (device < base || device >= base + p->num)
316 continue;
317 *index = device - base;
318 return tty_driver_kref_get(p);
319 }
320 return NULL;
321}
322
323#ifdef CONFIG_CONSOLE_POLL
324
325/**
326 * tty_find_polling_driver - find device of a polled tty
327 * @name: name string to match
328 * @line: pointer to resulting tty line nr
329 *
330 * This routine returns a tty driver structure, given a name
331 * and the condition that the tty driver is capable of polled
332 * operation.
333 */
334struct tty_driver *tty_find_polling_driver(char *name, int *line)
335{
336 struct tty_driver *p, *res = NULL;
337 int tty_line = 0;
338 int len;
339 char *str, *stp;
340
341 for (str = name; *str; str++)
342 if ((*str >= '0' && *str <= '9') || *str == ',')
343 break;
344 if (!*str)
345 return NULL;
346
347 len = str - name;
348 tty_line = simple_strtoul(str, &str, 10);
349
350 mutex_lock(&tty_mutex);
351 /* Search through the tty devices to look for a match */
352 list_for_each_entry(p, &tty_drivers, tty_drivers) {
353 if (strncmp(name, p->name, len) != 0)
354 continue;
355 stp = str;
356 if (*stp == ',')
357 stp++;
358 if (*stp == '\0')
359 stp = NULL;
360
361 if (tty_line >= 0 && tty_line < p->num && p->ops &&
362 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
363 res = tty_driver_kref_get(p);
364 *line = tty_line;
365 break;
366 }
367 }
368 mutex_unlock(&tty_mutex);
369
370 return res;
371}
372EXPORT_SYMBOL_GPL(tty_find_polling_driver);
373#endif
374
375/**
376 * tty_check_change - check for POSIX terminal changes
377 * @tty: tty to check
378 *
379 * If we try to write to, or set the state of, a terminal and we're
380 * not in the foreground, send a SIGTTOU. If the signal is blocked or
381 * ignored, go ahead and perform the operation. (POSIX 7.2)
382 *
383 * Locking: ctrl_lock
384 */
385
386int tty_check_change(struct tty_struct *tty)
387{
388 unsigned long flags;
389 int ret = 0;
390
391 if (current->signal->tty != tty)
392 return 0;
393
394 spin_lock_irqsave(&tty->ctrl_lock, flags);
395
396 if (!tty->pgrp) {
397 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
398 goto out_unlock;
399 }
400 if (task_pgrp(current) == tty->pgrp)
401 goto out_unlock;
402 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
403 if (is_ignored(SIGTTOU))
404 goto out;
405 if (is_current_pgrp_orphaned()) {
406 ret = -EIO;
407 goto out;
408 }
409 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
410 set_thread_flag(TIF_SIGPENDING);
411 ret = -ERESTARTSYS;
412out:
413 return ret;
414out_unlock:
415 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
416 return ret;
417}
418
419EXPORT_SYMBOL(tty_check_change);
420
421static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
422 size_t count, loff_t *ppos)
423{
424 return 0;
425}
426
427static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
428 size_t count, loff_t *ppos)
429{
430 return -EIO;
431}
432
433/* No kernel lock held - none needed ;) */
434static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
435{
436 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
437}
438
439static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
440 unsigned long arg)
441{
442 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
443}
444
445static long hung_up_tty_compat_ioctl(struct file *file,
446 unsigned int cmd, unsigned long arg)
447{
448 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
449}
450
451static const struct file_operations tty_fops = {
452 .llseek = no_llseek,
453 .read = tty_read,
454 .write = tty_write,
455 .poll = tty_poll,
456 .unlocked_ioctl = tty_ioctl,
457 .compat_ioctl = tty_compat_ioctl,
458 .open = tty_open,
459 .release = tty_release,
460 .fasync = tty_fasync,
461};
462
463static const struct file_operations console_fops = {
464 .llseek = no_llseek,
465 .read = tty_read,
466 .write = redirected_tty_write,
467 .poll = tty_poll,
468 .unlocked_ioctl = tty_ioctl,
469 .compat_ioctl = tty_compat_ioctl,
470 .open = tty_open,
471 .release = tty_release,
472 .fasync = tty_fasync,
473};
474
475static const struct file_operations hung_up_tty_fops = {
476 .llseek = no_llseek,
477 .read = hung_up_tty_read,
478 .write = hung_up_tty_write,
479 .poll = hung_up_tty_poll,
480 .unlocked_ioctl = hung_up_tty_ioctl,
481 .compat_ioctl = hung_up_tty_compat_ioctl,
482 .release = tty_release,
483};
484
485static DEFINE_SPINLOCK(redirect_lock);
486static struct file *redirect;
487
488/**
489 * tty_wakeup - request more data
490 * @tty: terminal
491 *
492 * Internal and external helper for wakeups of tty. This function
493 * informs the line discipline if present that the driver is ready
494 * to receive more output data.
495 */
496
497void tty_wakeup(struct tty_struct *tty)
498{
499 struct tty_ldisc *ld;
500
501 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
502 ld = tty_ldisc_ref(tty);
503 if (ld) {
504 if (ld->ops->write_wakeup)
505 ld->ops->write_wakeup(tty);
506 tty_ldisc_deref(ld);
507 }
508 }
509 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
510}
511
512EXPORT_SYMBOL_GPL(tty_wakeup);
513
514/**
515 * __tty_hangup - actual handler for hangup events
516 * @work: tty device
517 *
518 * This can be called by the "eventd" kernel thread. That is process
519 * synchronous but doesn't hold any locks, so we need to make sure we
520 * have the appropriate locks for what we're doing.
521 *
522 * The hangup event clears any pending redirections onto the hung up
523 * device. It ensures future writes will error and it does the needed
524 * line discipline hangup and signal delivery. The tty object itself
525 * remains intact.
526 *
527 * Locking:
528 * BTM
529 * redirect lock for undoing redirection
530 * file list lock for manipulating list of ttys
531 * tty_ldisc_lock from called functions
532 * termios_mutex resetting termios data
533 * tasklist_lock to walk task list for hangup event
534 * ->siglock to protect ->signal/->sighand
535 */
536void __tty_hangup(struct tty_struct *tty)
537{
538 struct file *cons_filp = NULL;
539 struct file *filp, *f = NULL;
540 struct task_struct *p;
541 struct tty_file_private *priv;
542 int closecount = 0, n;
543 unsigned long flags;
544 int refs = 0;
545
546 if (!tty)
547 return;
548
549
550 spin_lock(&redirect_lock);
551 if (redirect && file_tty(redirect) == tty) {
552 f = redirect;
553 redirect = NULL;
554 }
555 spin_unlock(&redirect_lock);
556
557 tty_lock();
558
559 /* some functions below drop BTM, so we need this bit */
560 set_bit(TTY_HUPPING, &tty->flags);
561
562 /* inuse_filps is protected by the single tty lock,
563 this really needs to change if we want to flush the
564 workqueue with the lock held */
565 check_tty_count(tty, "tty_hangup");
566
567 spin_lock(&tty_files_lock);
568 /* This breaks for file handles being sent over AF_UNIX sockets ? */
569 list_for_each_entry(priv, &tty->tty_files, list) {
570 filp = priv->file;
571 if (filp->f_op->write == redirected_tty_write)
572 cons_filp = filp;
573 if (filp->f_op->write != tty_write)
574 continue;
575 closecount++;
576 __tty_fasync(-1, filp, 0); /* can't block */
577 filp->f_op = &hung_up_tty_fops;
578 }
579 spin_unlock(&tty_files_lock);
580
581 /*
582 * it drops BTM and thus races with reopen
583 * we protect the race by TTY_HUPPING
584 */
585 tty_ldisc_hangup(tty);
586
587 read_lock(&tasklist_lock);
588 if (tty->session) {
589 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
590 spin_lock_irq(&p->sighand->siglock);
591 if (p->signal->tty == tty) {
592 p->signal->tty = NULL;
593 /* We defer the dereferences outside fo
594 the tasklist lock */
595 refs++;
596 }
597 if (!p->signal->leader) {
598 spin_unlock_irq(&p->sighand->siglock);
599 continue;
600 }
601 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
602 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
603 put_pid(p->signal->tty_old_pgrp); /* A noop */
604 spin_lock_irqsave(&tty->ctrl_lock, flags);
605 if (tty->pgrp)
606 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
607 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
608 spin_unlock_irq(&p->sighand->siglock);
609 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
610 }
611 read_unlock(&tasklist_lock);
612
613 spin_lock_irqsave(&tty->ctrl_lock, flags);
614 clear_bit(TTY_THROTTLED, &tty->flags);
615 clear_bit(TTY_PUSH, &tty->flags);
616 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
617 put_pid(tty->session);
618 put_pid(tty->pgrp);
619 tty->session = NULL;
620 tty->pgrp = NULL;
621 tty->ctrl_status = 0;
622 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
623
624 /* Account for the p->signal references we killed */
625 while (refs--)
626 tty_kref_put(tty);
627
628 /*
629 * If one of the devices matches a console pointer, we
630 * cannot just call hangup() because that will cause
631 * tty->count and state->count to go out of sync.
632 * So we just call close() the right number of times.
633 */
634 if (cons_filp) {
635 if (tty->ops->close)
636 for (n = 0; n < closecount; n++)
637 tty->ops->close(tty, cons_filp);
638 } else if (tty->ops->hangup)
639 (tty->ops->hangup)(tty);
640 /*
641 * We don't want to have driver/ldisc interactions beyond
642 * the ones we did here. The driver layer expects no
643 * calls after ->hangup() from the ldisc side. However we
644 * can't yet guarantee all that.
645 */
646 set_bit(TTY_HUPPED, &tty->flags);
647 clear_bit(TTY_HUPPING, &tty->flags);
648 tty_ldisc_enable(tty);
649
650 tty_unlock();
651
652 if (f)
653 fput(f);
654}
655
656static void do_tty_hangup(struct work_struct *work)
657{
658 struct tty_struct *tty =
659 container_of(work, struct tty_struct, hangup_work);
660
661 __tty_hangup(tty);
662}
663
664/**
665 * tty_hangup - trigger a hangup event
666 * @tty: tty to hangup
667 *
668 * A carrier loss (virtual or otherwise) has occurred on this like
669 * schedule a hangup sequence to run after this event.
670 */
671
672void tty_hangup(struct tty_struct *tty)
673{
674#ifdef TTY_DEBUG_HANGUP
675 char buf[64];
676 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
677#endif
678 schedule_work(&tty->hangup_work);
679}
680
681EXPORT_SYMBOL(tty_hangup);
682
683/**
684 * tty_vhangup - process vhangup
685 * @tty: tty to hangup
686 *
687 * The user has asked via system call for the terminal to be hung up.
688 * We do this synchronously so that when the syscall returns the process
689 * is complete. That guarantee is necessary for security reasons.
690 */
691
692void tty_vhangup(struct tty_struct *tty)
693{
694#ifdef TTY_DEBUG_HANGUP
695 char buf[64];
696
697 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
698#endif
699 __tty_hangup(tty);
700}
701
702EXPORT_SYMBOL(tty_vhangup);
703
704
705/**
706 * tty_vhangup_self - process vhangup for own ctty
707 *
708 * Perform a vhangup on the current controlling tty
709 */
710
711void tty_vhangup_self(void)
712{
713 struct tty_struct *tty;
714
715 tty = get_current_tty();
716 if (tty) {
717 tty_vhangup(tty);
718 tty_kref_put(tty);
719 }
720}
721
722/**
723 * tty_hung_up_p - was tty hung up
724 * @filp: file pointer of tty
725 *
726 * Return true if the tty has been subject to a vhangup or a carrier
727 * loss
728 */
729
730int tty_hung_up_p(struct file *filp)
731{
732 return (filp->f_op == &hung_up_tty_fops);
733}
734
735EXPORT_SYMBOL(tty_hung_up_p);
736
737static void session_clear_tty(struct pid *session)
738{
739 struct task_struct *p;
740 do_each_pid_task(session, PIDTYPE_SID, p) {
741 proc_clear_tty(p);
742 } while_each_pid_task(session, PIDTYPE_SID, p);
743}
744
745/**
746 * disassociate_ctty - disconnect controlling tty
747 * @on_exit: true if exiting so need to "hang up" the session
748 *
749 * This function is typically called only by the session leader, when
750 * it wants to disassociate itself from its controlling tty.
751 *
752 * It performs the following functions:
753 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
754 * (2) Clears the tty from being controlling the session
755 * (3) Clears the controlling tty for all processes in the
756 * session group.
757 *
758 * The argument on_exit is set to 1 if called when a process is
759 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
760 *
761 * Locking:
762 * BTM is taken for hysterical raisins, and held when
763 * called from no_tty().
764 * tty_mutex is taken to protect tty
765 * ->siglock is taken to protect ->signal/->sighand
766 * tasklist_lock is taken to walk process list for sessions
767 * ->siglock is taken to protect ->signal/->sighand
768 */
769
770void disassociate_ctty(int on_exit)
771{
772 struct tty_struct *tty;
773 struct pid *tty_pgrp = NULL;
774
775 if (!current->signal->leader)
776 return;
777
778 tty = get_current_tty();
779 if (tty) {
780 tty_pgrp = get_pid(tty->pgrp);
781 if (on_exit) {
782 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
783 tty_vhangup(tty);
784 }
785 tty_kref_put(tty);
786 } else if (on_exit) {
787 struct pid *old_pgrp;
788 spin_lock_irq(&current->sighand->siglock);
789 old_pgrp = current->signal->tty_old_pgrp;
790 current->signal->tty_old_pgrp = NULL;
791 spin_unlock_irq(&current->sighand->siglock);
792 if (old_pgrp) {
793 kill_pgrp(old_pgrp, SIGHUP, on_exit);
794 kill_pgrp(old_pgrp, SIGCONT, on_exit);
795 put_pid(old_pgrp);
796 }
797 return;
798 }
799 if (tty_pgrp) {
800 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
801 if (!on_exit)
802 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
803 put_pid(tty_pgrp);
804 }
805
806 spin_lock_irq(&current->sighand->siglock);
807 put_pid(current->signal->tty_old_pgrp);
808 current->signal->tty_old_pgrp = NULL;
809 spin_unlock_irq(&current->sighand->siglock);
810
811 tty = get_current_tty();
812 if (tty) {
813 unsigned long flags;
814 spin_lock_irqsave(&tty->ctrl_lock, flags);
815 put_pid(tty->session);
816 put_pid(tty->pgrp);
817 tty->session = NULL;
818 tty->pgrp = NULL;
819 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
820 tty_kref_put(tty);
821 } else {
822#ifdef TTY_DEBUG_HANGUP
823 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
824 " = NULL", tty);
825#endif
826 }
827
828 /* Now clear signal->tty under the lock */
829 read_lock(&tasklist_lock);
830 session_clear_tty(task_session(current));
831 read_unlock(&tasklist_lock);
832}
833
834/**
835 *
836 * no_tty - Ensure the current process does not have a controlling tty
837 */
838void no_tty(void)
839{
840 struct task_struct *tsk = current;
841 tty_lock();
842 disassociate_ctty(0);
843 tty_unlock();
844 proc_clear_tty(tsk);
845}
846
847
848/**
849 * stop_tty - propagate flow control
850 * @tty: tty to stop
851 *
852 * Perform flow control to the driver. For PTY/TTY pairs we
853 * must also propagate the TIOCKPKT status. May be called
854 * on an already stopped device and will not re-call the driver
855 * method.
856 *
857 * This functionality is used by both the line disciplines for
858 * halting incoming flow and by the driver. It may therefore be
859 * called from any context, may be under the tty atomic_write_lock
860 * but not always.
861 *
862 * Locking:
863 * Uses the tty control lock internally
864 */
865
866void stop_tty(struct tty_struct *tty)
867{
868 unsigned long flags;
869 spin_lock_irqsave(&tty->ctrl_lock, flags);
870 if (tty->stopped) {
871 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
872 return;
873 }
874 tty->stopped = 1;
875 if (tty->link && tty->link->packet) {
876 tty->ctrl_status &= ~TIOCPKT_START;
877 tty->ctrl_status |= TIOCPKT_STOP;
878 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
879 }
880 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
881 if (tty->ops->stop)
882 (tty->ops->stop)(tty);
883}
884
885EXPORT_SYMBOL(stop_tty);
886
887/**
888 * start_tty - propagate flow control
889 * @tty: tty to start
890 *
891 * Start a tty that has been stopped if at all possible. Perform
892 * any necessary wakeups and propagate the TIOCPKT status. If this
893 * is the tty was previous stopped and is being started then the
894 * driver start method is invoked and the line discipline woken.
895 *
896 * Locking:
897 * ctrl_lock
898 */
899
900void start_tty(struct tty_struct *tty)
901{
902 unsigned long flags;
903 spin_lock_irqsave(&tty->ctrl_lock, flags);
904 if (!tty->stopped || tty->flow_stopped) {
905 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
906 return;
907 }
908 tty->stopped = 0;
909 if (tty->link && tty->link->packet) {
910 tty->ctrl_status &= ~TIOCPKT_STOP;
911 tty->ctrl_status |= TIOCPKT_START;
912 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
913 }
914 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
915 if (tty->ops->start)
916 (tty->ops->start)(tty);
917 /* If we have a running line discipline it may need kicking */
918 tty_wakeup(tty);
919}
920
921EXPORT_SYMBOL(start_tty);
922
923/**
924 * tty_read - read method for tty device files
925 * @file: pointer to tty file
926 * @buf: user buffer
927 * @count: size of user buffer
928 * @ppos: unused
929 *
930 * Perform the read system call function on this terminal device. Checks
931 * for hung up devices before calling the line discipline method.
932 *
933 * Locking:
934 * Locks the line discipline internally while needed. Multiple
935 * read calls may be outstanding in parallel.
936 */
937
938static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
939 loff_t *ppos)
940{
941 int i;
942 struct inode *inode = file->f_path.dentry->d_inode;
943 struct tty_struct *tty = file_tty(file);
944 struct tty_ldisc *ld;
945
946 if (tty_paranoia_check(tty, inode, "tty_read"))
947 return -EIO;
948 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
949 return -EIO;
950
951 /* We want to wait for the line discipline to sort out in this
952 situation */
953 ld = tty_ldisc_ref_wait(tty);
954 if (ld->ops->read)
955 i = (ld->ops->read)(tty, file, buf, count);
956 else
957 i = -EIO;
958 tty_ldisc_deref(ld);
959 if (i > 0)
960 inode->i_atime = current_fs_time(inode->i_sb);
961 return i;
962}
963
964void tty_write_unlock(struct tty_struct *tty)
965 __releases(&tty->atomic_write_lock)
966{
967 mutex_unlock(&tty->atomic_write_lock);
968 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
969}
970
971int tty_write_lock(struct tty_struct *tty, int ndelay)
972 __acquires(&tty->atomic_write_lock)
973{
974 if (!mutex_trylock(&tty->atomic_write_lock)) {
975 if (ndelay)
976 return -EAGAIN;
977 if (mutex_lock_interruptible(&tty->atomic_write_lock))
978 return -ERESTARTSYS;
979 }
980 return 0;
981}
982
983/*
984 * Split writes up in sane blocksizes to avoid
985 * denial-of-service type attacks
986 */
987static inline ssize_t do_tty_write(
988 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
989 struct tty_struct *tty,
990 struct file *file,
991 const char __user *buf,
992 size_t count)
993{
994 ssize_t ret, written = 0;
995 unsigned int chunk;
996
997 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
998 if (ret < 0)
999 return ret;
1000
1001 /*
1002 * We chunk up writes into a temporary buffer. This
1003 * simplifies low-level drivers immensely, since they
1004 * don't have locking issues and user mode accesses.
1005 *
1006 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1007 * big chunk-size..
1008 *
1009 * The default chunk-size is 2kB, because the NTTY
1010 * layer has problems with bigger chunks. It will
1011 * claim to be able to handle more characters than
1012 * it actually does.
1013 *
1014 * FIXME: This can probably go away now except that 64K chunks
1015 * are too likely to fail unless switched to vmalloc...
1016 */
1017 chunk = 2048;
1018 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1019 chunk = 65536;
1020 if (count < chunk)
1021 chunk = count;
1022
1023 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1024 if (tty->write_cnt < chunk) {
1025 unsigned char *buf_chunk;
1026
1027 if (chunk < 1024)
1028 chunk = 1024;
1029
1030 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1031 if (!buf_chunk) {
1032 ret = -ENOMEM;
1033 goto out;
1034 }
1035 kfree(tty->write_buf);
1036 tty->write_cnt = chunk;
1037 tty->write_buf = buf_chunk;
1038 }
1039
1040 /* Do the write .. */
1041 for (;;) {
1042 size_t size = count;
1043 if (size > chunk)
1044 size = chunk;
1045 ret = -EFAULT;
1046 if (copy_from_user(tty->write_buf, buf, size))
1047 break;
1048 ret = write(tty, file, tty->write_buf, size);
1049 if (ret <= 0)
1050 break;
1051 written += ret;
1052 buf += ret;
1053 count -= ret;
1054 if (!count)
1055 break;
1056 ret = -ERESTARTSYS;
1057 if (signal_pending(current))
1058 break;
1059 cond_resched();
1060 }
1061 if (written) {
1062 struct inode *inode = file->f_path.dentry->d_inode;
1063 inode->i_mtime = current_fs_time(inode->i_sb);
1064 ret = written;
1065 }
1066out:
1067 tty_write_unlock(tty);
1068 return ret;
1069}
1070
1071/**
1072 * tty_write_message - write a message to a certain tty, not just the console.
1073 * @tty: the destination tty_struct
1074 * @msg: the message to write
1075 *
1076 * This is used for messages that need to be redirected to a specific tty.
1077 * We don't put it into the syslog queue right now maybe in the future if
1078 * really needed.
1079 *
1080 * We must still hold the BTM and test the CLOSING flag for the moment.
1081 */
1082
1083void tty_write_message(struct tty_struct *tty, char *msg)
1084{
1085 if (tty) {
1086 mutex_lock(&tty->atomic_write_lock);
1087 tty_lock();
1088 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1089 tty_unlock();
1090 tty->ops->write(tty, msg, strlen(msg));
1091 } else
1092 tty_unlock();
1093 tty_write_unlock(tty);
1094 }
1095 return;
1096}
1097
1098
1099/**
1100 * tty_write - write method for tty device file
1101 * @file: tty file pointer
1102 * @buf: user data to write
1103 * @count: bytes to write
1104 * @ppos: unused
1105 *
1106 * Write data to a tty device via the line discipline.
1107 *
1108 * Locking:
1109 * Locks the line discipline as required
1110 * Writes to the tty driver are serialized by the atomic_write_lock
1111 * and are then processed in chunks to the device. The line discipline
1112 * write method will not be invoked in parallel for each device.
1113 */
1114
1115static ssize_t tty_write(struct file *file, const char __user *buf,
1116 size_t count, loff_t *ppos)
1117{
1118 struct inode *inode = file->f_path.dentry->d_inode;
1119 struct tty_struct *tty = file_tty(file);
1120 struct tty_ldisc *ld;
1121 ssize_t ret;
1122
1123 if (tty_paranoia_check(tty, inode, "tty_write"))
1124 return -EIO;
1125 if (!tty || !tty->ops->write ||
1126 (test_bit(TTY_IO_ERROR, &tty->flags)))
1127 return -EIO;
1128 /* Short term debug to catch buggy drivers */
1129 if (tty->ops->write_room == NULL)
1130 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1131 tty->driver->name);
1132 ld = tty_ldisc_ref_wait(tty);
1133 if (!ld->ops->write)
1134 ret = -EIO;
1135 else
1136 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1137 tty_ldisc_deref(ld);
1138 return ret;
1139}
1140
1141ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1142 size_t count, loff_t *ppos)
1143{
1144 struct file *p = NULL;
1145
1146 spin_lock(&redirect_lock);
1147 if (redirect) {
1148 get_file(redirect);
1149 p = redirect;
1150 }
1151 spin_unlock(&redirect_lock);
1152
1153 if (p) {
1154 ssize_t res;
1155 res = vfs_write(p, buf, count, &p->f_pos);
1156 fput(p);
1157 return res;
1158 }
1159 return tty_write(file, buf, count, ppos);
1160}
1161
1162static char ptychar[] = "pqrstuvwxyzabcde";
1163
1164/**
1165 * pty_line_name - generate name for a pty
1166 * @driver: the tty driver in use
1167 * @index: the minor number
1168 * @p: output buffer of at least 6 bytes
1169 *
1170 * Generate a name from a driver reference and write it to the output
1171 * buffer.
1172 *
1173 * Locking: None
1174 */
1175static void pty_line_name(struct tty_driver *driver, int index, char *p)
1176{
1177 int i = index + driver->name_base;
1178 /* ->name is initialized to "ttyp", but "tty" is expected */
1179 sprintf(p, "%s%c%x",
1180 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1181 ptychar[i >> 4 & 0xf], i & 0xf);
1182}
1183
1184/**
1185 * tty_line_name - generate name for a tty
1186 * @driver: the tty driver in use
1187 * @index: the minor number
1188 * @p: output buffer of at least 7 bytes
1189 *
1190 * Generate a name from a driver reference and write it to the output
1191 * buffer.
1192 *
1193 * Locking: None
1194 */
1195static void tty_line_name(struct tty_driver *driver, int index, char *p)
1196{
1197 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1198}
1199
1200/**
1201 * tty_driver_lookup_tty() - find an existing tty, if any
1202 * @driver: the driver for the tty
1203 * @idx: the minor number
1204 *
1205 * Return the tty, if found or ERR_PTR() otherwise.
1206 *
1207 * Locking: tty_mutex must be held. If tty is found, the mutex must
1208 * be held until the 'fast-open' is also done. Will change once we
1209 * have refcounting in the driver and per driver locking
1210 */
1211static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1212 struct inode *inode, int idx)
1213{
1214 struct tty_struct *tty;
1215
1216 if (driver->ops->lookup)
1217 return driver->ops->lookup(driver, inode, idx);
1218
1219 tty = driver->ttys[idx];
1220 return tty;
1221}
1222
1223/**
1224 * tty_init_termios - helper for termios setup
1225 * @tty: the tty to set up
1226 *
1227 * Initialise the termios structures for this tty. Thus runs under
1228 * the tty_mutex currently so we can be relaxed about ordering.
1229 */
1230
1231int tty_init_termios(struct tty_struct *tty)
1232{
1233 struct ktermios *tp;
1234 int idx = tty->index;
1235
1236 tp = tty->driver->termios[idx];
1237 if (tp == NULL) {
1238 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1239 if (tp == NULL)
1240 return -ENOMEM;
1241 memcpy(tp, &tty->driver->init_termios,
1242 sizeof(struct ktermios));
1243 tty->driver->termios[idx] = tp;
1244 }
1245 tty->termios = tp;
1246 tty->termios_locked = tp + 1;
1247
1248 /* Compatibility until drivers always set this */
1249 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1250 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1251 return 0;
1252}
1253EXPORT_SYMBOL_GPL(tty_init_termios);
1254
1255/**
1256 * tty_driver_install_tty() - install a tty entry in the driver
1257 * @driver: the driver for the tty
1258 * @tty: the tty
1259 *
1260 * Install a tty object into the driver tables. The tty->index field
1261 * will be set by the time this is called. This method is responsible
1262 * for ensuring any need additional structures are allocated and
1263 * configured.
1264 *
1265 * Locking: tty_mutex for now
1266 */
1267static int tty_driver_install_tty(struct tty_driver *driver,
1268 struct tty_struct *tty)
1269{
1270 int idx = tty->index;
1271 int ret;
1272
1273 if (driver->ops->install) {
1274 ret = driver->ops->install(driver, tty);
1275 return ret;
1276 }
1277
1278 if (tty_init_termios(tty) == 0) {
1279 tty_driver_kref_get(driver);
1280 tty->count++;
1281 driver->ttys[idx] = tty;
1282 return 0;
1283 }
1284 return -ENOMEM;
1285}
1286
1287/**
1288 * tty_driver_remove_tty() - remove a tty from the driver tables
1289 * @driver: the driver for the tty
1290 * @idx: the minor number
1291 *
1292 * Remvoe a tty object from the driver tables. The tty->index field
1293 * will be set by the time this is called.
1294 *
1295 * Locking: tty_mutex for now
1296 */
1297static void tty_driver_remove_tty(struct tty_driver *driver,
1298 struct tty_struct *tty)
1299{
1300 if (driver->ops->remove)
1301 driver->ops->remove(driver, tty);
1302 else
1303 driver->ttys[tty->index] = NULL;
1304}
1305
1306/*
1307 * tty_reopen() - fast re-open of an open tty
1308 * @tty - the tty to open
1309 *
1310 * Return 0 on success, -errno on error.
1311 *
1312 * Locking: tty_mutex must be held from the time the tty was found
1313 * till this open completes.
1314 */
1315static int tty_reopen(struct tty_struct *tty)
1316{
1317 struct tty_driver *driver = tty->driver;
1318
1319 if (test_bit(TTY_CLOSING, &tty->flags) ||
1320 test_bit(TTY_HUPPING, &tty->flags) ||
1321 test_bit(TTY_LDISC_CHANGING, &tty->flags))
1322 return -EIO;
1323
1324 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1325 driver->subtype == PTY_TYPE_MASTER) {
1326 /*
1327 * special case for PTY masters: only one open permitted,
1328 * and the slave side open count is incremented as well.
1329 */
1330 if (tty->count)
1331 return -EIO;
1332
1333 tty->link->count++;
1334 }
1335 tty->count++;
1336 tty->driver = driver; /* N.B. why do this every time?? */
1337
1338 mutex_lock(&tty->ldisc_mutex);
1339 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
1340 mutex_unlock(&tty->ldisc_mutex);
1341
1342 return 0;
1343}
1344
1345/**
1346 * tty_init_dev - initialise a tty device
1347 * @driver: tty driver we are opening a device on
1348 * @idx: device index
1349 * @ret_tty: returned tty structure
1350 * @first_ok: ok to open a new device (used by ptmx)
1351 *
1352 * Prepare a tty device. This may not be a "new" clean device but
1353 * could also be an active device. The pty drivers require special
1354 * handling because of this.
1355 *
1356 * Locking:
1357 * The function is called under the tty_mutex, which
1358 * protects us from the tty struct or driver itself going away.
1359 *
1360 * On exit the tty device has the line discipline attached and
1361 * a reference count of 1. If a pair was created for pty/tty use
1362 * and the other was a pty master then it too has a reference count of 1.
1363 *
1364 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1365 * failed open. The new code protects the open with a mutex, so it's
1366 * really quite straightforward. The mutex locking can probably be
1367 * relaxed for the (most common) case of reopening a tty.
1368 */
1369
1370struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1371 int first_ok)
1372{
1373 struct tty_struct *tty;
1374 int retval;
1375
1376 /* Check if pty master is being opened multiple times */
1377 if (driver->subtype == PTY_TYPE_MASTER &&
1378 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1379 return ERR_PTR(-EIO);
1380 }
1381
1382 /*
1383 * First time open is complex, especially for PTY devices.
1384 * This code guarantees that either everything succeeds and the
1385 * TTY is ready for operation, or else the table slots are vacated
1386 * and the allocated memory released. (Except that the termios
1387 * and locked termios may be retained.)
1388 */
1389
1390 if (!try_module_get(driver->owner))
1391 return ERR_PTR(-ENODEV);
1392
1393 tty = alloc_tty_struct();
1394 if (!tty) {
1395 retval = -ENOMEM;
1396 goto err_module_put;
1397 }
1398 initialize_tty_struct(tty, driver, idx);
1399
1400 retval = tty_driver_install_tty(driver, tty);
1401 if (retval < 0)
1402 goto err_deinit_tty;
1403
1404 /*
1405 * Structures all installed ... call the ldisc open routines.
1406 * If we fail here just call release_tty to clean up. No need
1407 * to decrement the use counts, as release_tty doesn't care.
1408 */
1409 retval = tty_ldisc_setup(tty, tty->link);
1410 if (retval)
1411 goto err_release_tty;
1412 return tty;
1413
1414err_deinit_tty:
1415 deinitialize_tty_struct(tty);
1416 free_tty_struct(tty);
1417err_module_put:
1418 module_put(driver->owner);
1419 return ERR_PTR(retval);
1420
1421 /* call the tty release_tty routine to clean out this slot */
1422err_release_tty:
1423 if (printk_ratelimit())
1424 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1425 "clearing slot %d\n", idx);
1426 release_tty(tty, idx);
1427 return ERR_PTR(retval);
1428}
1429
1430void tty_free_termios(struct tty_struct *tty)
1431{
1432 struct ktermios *tp;
1433 int idx = tty->index;
1434 /* Kill this flag and push into drivers for locking etc */
1435 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1436 /* FIXME: Locking on ->termios array */
1437 tp = tty->termios;
1438 tty->driver->termios[idx] = NULL;
1439 kfree(tp);
1440 }
1441}
1442EXPORT_SYMBOL(tty_free_termios);
1443
1444void tty_shutdown(struct tty_struct *tty)
1445{
1446 tty_driver_remove_tty(tty->driver, tty);
1447 tty_free_termios(tty);
1448}
1449EXPORT_SYMBOL(tty_shutdown);
1450
1451/**
1452 * release_one_tty - release tty structure memory
1453 * @kref: kref of tty we are obliterating
1454 *
1455 * Releases memory associated with a tty structure, and clears out the
1456 * driver table slots. This function is called when a device is no longer
1457 * in use. It also gets called when setup of a device fails.
1458 *
1459 * Locking:
1460 * tty_mutex - sometimes only
1461 * takes the file list lock internally when working on the list
1462 * of ttys that the driver keeps.
1463 *
1464 * This method gets called from a work queue so that the driver private
1465 * cleanup ops can sleep (needed for USB at least)
1466 */
1467static void release_one_tty(struct work_struct *work)
1468{
1469 struct tty_struct *tty =
1470 container_of(work, struct tty_struct, hangup_work);
1471 struct tty_driver *driver = tty->driver;
1472
1473 if (tty->ops->cleanup)
1474 tty->ops->cleanup(tty);
1475
1476 tty->magic = 0;
1477 tty_driver_kref_put(driver);
1478 module_put(driver->owner);
1479
1480 spin_lock(&tty_files_lock);
1481 list_del_init(&tty->tty_files);
1482 spin_unlock(&tty_files_lock);
1483
1484 put_pid(tty->pgrp);
1485 put_pid(tty->session);
1486 free_tty_struct(tty);
1487}
1488
1489static void queue_release_one_tty(struct kref *kref)
1490{
1491 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1492
1493 if (tty->ops->shutdown)
1494 tty->ops->shutdown(tty);
1495 else
1496 tty_shutdown(tty);
1497
1498 /* The hangup queue is now free so we can reuse it rather than
1499 waste a chunk of memory for each port */
1500 INIT_WORK(&tty->hangup_work, release_one_tty);
1501 schedule_work(&tty->hangup_work);
1502}
1503
1504/**
1505 * tty_kref_put - release a tty kref
1506 * @tty: tty device
1507 *
1508 * Release a reference to a tty device and if need be let the kref
1509 * layer destruct the object for us
1510 */
1511
1512void tty_kref_put(struct tty_struct *tty)
1513{
1514 if (tty)
1515 kref_put(&tty->kref, queue_release_one_tty);
1516}
1517EXPORT_SYMBOL(tty_kref_put);
1518
1519/**
1520 * release_tty - release tty structure memory
1521 *
1522 * Release both @tty and a possible linked partner (think pty pair),
1523 * and decrement the refcount of the backing module.
1524 *
1525 * Locking:
1526 * tty_mutex - sometimes only
1527 * takes the file list lock internally when working on the list
1528 * of ttys that the driver keeps.
1529 * FIXME: should we require tty_mutex is held here ??
1530 *
1531 */
1532static void release_tty(struct tty_struct *tty, int idx)
1533{
1534 /* This should always be true but check for the moment */
1535 WARN_ON(tty->index != idx);
1536
1537 if (tty->link)
1538 tty_kref_put(tty->link);
1539 tty_kref_put(tty);
1540}
1541
1542/**
1543 * tty_release - vfs callback for close
1544 * @inode: inode of tty
1545 * @filp: file pointer for handle to tty
1546 *
1547 * Called the last time each file handle is closed that references
1548 * this tty. There may however be several such references.
1549 *
1550 * Locking:
1551 * Takes bkl. See tty_release_dev
1552 *
1553 * Even releasing the tty structures is a tricky business.. We have
1554 * to be very careful that the structures are all released at the
1555 * same time, as interrupts might otherwise get the wrong pointers.
1556 *
1557 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1558 * lead to double frees or releasing memory still in use.
1559 */
1560
1561int tty_release(struct inode *inode, struct file *filp)
1562{
1563 struct tty_struct *tty = file_tty(filp);
1564 struct tty_struct *o_tty;
1565 int pty_master, tty_closing, o_tty_closing, do_sleep;
1566 int devpts;
1567 int idx;
1568 char buf[64];
1569
1570 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1571 return 0;
1572
1573 tty_lock();
1574 check_tty_count(tty, "tty_release_dev");
1575
1576 __tty_fasync(-1, filp, 0);
1577
1578 idx = tty->index;
1579 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1580 tty->driver->subtype == PTY_TYPE_MASTER);
1581 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1582 o_tty = tty->link;
1583
1584#ifdef TTY_PARANOIA_CHECK
1585 if (idx < 0 || idx >= tty->driver->num) {
1586 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1587 "free (%s)\n", tty->name);
1588 tty_unlock();
1589 return 0;
1590 }
1591 if (!devpts) {
1592 if (tty != tty->driver->ttys[idx]) {
1593 tty_unlock();
1594 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1595 "for (%s)\n", idx, tty->name);
1596 return 0;
1597 }
1598 if (tty->termios != tty->driver->termios[idx]) {
1599 tty_unlock();
1600 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1601 "for (%s)\n",
1602 idx, tty->name);
1603 return 0;
1604 }
1605 }
1606#endif
1607
1608#ifdef TTY_DEBUG_HANGUP
1609 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
1610 tty_name(tty, buf), tty->count);
1611#endif
1612
1613#ifdef TTY_PARANOIA_CHECK
1614 if (tty->driver->other &&
1615 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1616 if (o_tty != tty->driver->other->ttys[idx]) {
1617 tty_unlock();
1618 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1619 "not o_tty for (%s)\n",
1620 idx, tty->name);
1621 return 0 ;
1622 }
1623 if (o_tty->termios != tty->driver->other->termios[idx]) {
1624 tty_unlock();
1625 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1626 "not o_termios for (%s)\n",
1627 idx, tty->name);
1628 return 0;
1629 }
1630 if (o_tty->link != tty) {
1631 tty_unlock();
1632 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1633 return 0;
1634 }
1635 }
1636#endif
1637 if (tty->ops->close)
1638 tty->ops->close(tty, filp);
1639
1640 tty_unlock();
1641 /*
1642 * Sanity check: if tty->count is going to zero, there shouldn't be
1643 * any waiters on tty->read_wait or tty->write_wait. We test the
1644 * wait queues and kick everyone out _before_ actually starting to
1645 * close. This ensures that we won't block while releasing the tty
1646 * structure.
1647 *
1648 * The test for the o_tty closing is necessary, since the master and
1649 * slave sides may close in any order. If the slave side closes out
1650 * first, its count will be one, since the master side holds an open.
1651 * Thus this test wouldn't be triggered at the time the slave closes,
1652 * so we do it now.
1653 *
1654 * Note that it's possible for the tty to be opened again while we're
1655 * flushing out waiters. By recalculating the closing flags before
1656 * each iteration we avoid any problems.
1657 */
1658 while (1) {
1659 /* Guard against races with tty->count changes elsewhere and
1660 opens on /dev/tty */
1661
1662 mutex_lock(&tty_mutex);
1663 tty_lock();
1664 tty_closing = tty->count <= 1;
1665 o_tty_closing = o_tty &&
1666 (o_tty->count <= (pty_master ? 1 : 0));
1667 do_sleep = 0;
1668
1669 if (tty_closing) {
1670 if (waitqueue_active(&tty->read_wait)) {
1671 wake_up_poll(&tty->read_wait, POLLIN);
1672 do_sleep++;
1673 }
1674 if (waitqueue_active(&tty->write_wait)) {
1675 wake_up_poll(&tty->write_wait, POLLOUT);
1676 do_sleep++;
1677 }
1678 }
1679 if (o_tty_closing) {
1680 if (waitqueue_active(&o_tty->read_wait)) {
1681 wake_up_poll(&o_tty->read_wait, POLLIN);
1682 do_sleep++;
1683 }
1684 if (waitqueue_active(&o_tty->write_wait)) {
1685 wake_up_poll(&o_tty->write_wait, POLLOUT);
1686 do_sleep++;
1687 }
1688 }
1689 if (!do_sleep)
1690 break;
1691
1692 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1693 "active!\n", tty_name(tty, buf));
1694 tty_unlock();
1695 mutex_unlock(&tty_mutex);
1696 schedule();
1697 }
1698
1699 /*
1700 * The closing flags are now consistent with the open counts on
1701 * both sides, and we've completed the last operation that could
1702 * block, so it's safe to proceed with closing.
1703 */
1704 if (pty_master) {
1705 if (--o_tty->count < 0) {
1706 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
1707 "(%d) for %s\n",
1708 o_tty->count, tty_name(o_tty, buf));
1709 o_tty->count = 0;
1710 }
1711 }
1712 if (--tty->count < 0) {
1713 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
1714 tty->count, tty_name(tty, buf));
1715 tty->count = 0;
1716 }
1717
1718 /*
1719 * We've decremented tty->count, so we need to remove this file
1720 * descriptor off the tty->tty_files list; this serves two
1721 * purposes:
1722 * - check_tty_count sees the correct number of file descriptors
1723 * associated with this tty.
1724 * - do_tty_hangup no longer sees this file descriptor as
1725 * something that needs to be handled for hangups.
1726 */
1727 tty_del_file(filp);
1728
1729 /*
1730 * Perform some housekeeping before deciding whether to return.
1731 *
1732 * Set the TTY_CLOSING flag if this was the last open. In the
1733 * case of a pty we may have to wait around for the other side
1734 * to close, and TTY_CLOSING makes sure we can't be reopened.
1735 */
1736 if (tty_closing)
1737 set_bit(TTY_CLOSING, &tty->flags);
1738 if (o_tty_closing)
1739 set_bit(TTY_CLOSING, &o_tty->flags);
1740
1741 /*
1742 * If _either_ side is closing, make sure there aren't any
1743 * processes that still think tty or o_tty is their controlling
1744 * tty.
1745 */
1746 if (tty_closing || o_tty_closing) {
1747 read_lock(&tasklist_lock);
1748 session_clear_tty(tty->session);
1749 if (o_tty)
1750 session_clear_tty(o_tty->session);
1751 read_unlock(&tasklist_lock);
1752 }
1753
1754 mutex_unlock(&tty_mutex);
1755
1756 /* check whether both sides are closing ... */
1757 if (!tty_closing || (o_tty && !o_tty_closing)) {
1758 tty_unlock();
1759 return 0;
1760 }
1761
1762#ifdef TTY_DEBUG_HANGUP
1763 printk(KERN_DEBUG "freeing tty structure...");
1764#endif
1765 /*
1766 * Ask the line discipline code to release its structures
1767 */
1768 tty_ldisc_release(tty, o_tty);
1769 /*
1770 * The release_tty function takes care of the details of clearing
1771 * the slots and preserving the termios structure.
1772 */
1773 release_tty(tty, idx);
1774
1775 /* Make this pty number available for reallocation */
1776 if (devpts)
1777 devpts_kill_index(inode, idx);
1778 tty_unlock();
1779 return 0;
1780}
1781
1782/**
1783 * tty_open - open a tty device
1784 * @inode: inode of device file
1785 * @filp: file pointer to tty
1786 *
1787 * tty_open and tty_release keep up the tty count that contains the
1788 * number of opens done on a tty. We cannot use the inode-count, as
1789 * different inodes might point to the same tty.
1790 *
1791 * Open-counting is needed for pty masters, as well as for keeping
1792 * track of serial lines: DTR is dropped when the last close happens.
1793 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1794 *
1795 * The termios state of a pty is reset on first open so that
1796 * settings don't persist across reuse.
1797 *
1798 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
1799 * tty->count should protect the rest.
1800 * ->siglock protects ->signal/->sighand
1801 */
1802
1803static int tty_open(struct inode *inode, struct file *filp)
1804{
1805 struct tty_struct *tty = NULL;
1806 int noctty, retval;
1807 struct tty_driver *driver;
1808 int index;
1809 dev_t device = inode->i_rdev;
1810 unsigned saved_flags = filp->f_flags;
1811
1812 nonseekable_open(inode, filp);
1813
1814retry_open:
1815 noctty = filp->f_flags & O_NOCTTY;
1816 index = -1;
1817 retval = 0;
1818
1819 mutex_lock(&tty_mutex);
1820 tty_lock();
1821
1822 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1823 tty = get_current_tty();
1824 if (!tty) {
1825 tty_unlock();
1826 mutex_unlock(&tty_mutex);
1827 return -ENXIO;
1828 }
1829 driver = tty_driver_kref_get(tty->driver);
1830 index = tty->index;
1831 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1832 /* noctty = 1; */
1833 /* FIXME: Should we take a driver reference ? */
1834 tty_kref_put(tty);
1835 goto got_driver;
1836 }
1837#ifdef CONFIG_VT
1838 if (device == MKDEV(TTY_MAJOR, 0)) {
1839 extern struct tty_driver *console_driver;
1840 driver = tty_driver_kref_get(console_driver);
1841 index = fg_console;
1842 noctty = 1;
1843 goto got_driver;
1844 }
1845#endif
1846 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1847 struct tty_driver *console_driver = console_device(&index);
1848 if (console_driver) {
1849 driver = tty_driver_kref_get(console_driver);
1850 if (driver) {
1851 /* Don't let /dev/console block */
1852 filp->f_flags |= O_NONBLOCK;
1853 noctty = 1;
1854 goto got_driver;
1855 }
1856 }
1857 tty_unlock();
1858 mutex_unlock(&tty_mutex);
1859 return -ENODEV;
1860 }
1861
1862 driver = get_tty_driver(device, &index);
1863 if (!driver) {
1864 tty_unlock();
1865 mutex_unlock(&tty_mutex);
1866 return -ENODEV;
1867 }
1868got_driver:
1869 if (!tty) {
1870 /* check whether we're reopening an existing tty */
1871 tty = tty_driver_lookup_tty(driver, inode, index);
1872
1873 if (IS_ERR(tty)) {
1874 tty_unlock();
1875 mutex_unlock(&tty_mutex);
1876 return PTR_ERR(tty);
1877 }
1878 }
1879
1880 if (tty) {
1881 retval = tty_reopen(tty);
1882 if (retval)
1883 tty = ERR_PTR(retval);
1884 } else
1885 tty = tty_init_dev(driver, index, 0);
1886
1887 mutex_unlock(&tty_mutex);
1888 tty_driver_kref_put(driver);
1889 if (IS_ERR(tty)) {
1890 tty_unlock();
1891 return PTR_ERR(tty);
1892 }
1893
1894 retval = tty_add_file(tty, filp);
1895 if (retval) {
1896 tty_unlock();
1897 tty_release(inode, filp);
1898 return retval;
1899 }
1900
1901 check_tty_count(tty, "tty_open");
1902 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1903 tty->driver->subtype == PTY_TYPE_MASTER)
1904 noctty = 1;
1905#ifdef TTY_DEBUG_HANGUP
1906 printk(KERN_DEBUG "opening %s...", tty->name);
1907#endif
1908 if (tty->ops->open)
1909 retval = tty->ops->open(tty, filp);
1910 else
1911 retval = -ENODEV;
1912 filp->f_flags = saved_flags;
1913
1914 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1915 !capable(CAP_SYS_ADMIN))
1916 retval = -EBUSY;
1917
1918 if (retval) {
1919#ifdef TTY_DEBUG_HANGUP
1920 printk(KERN_DEBUG "error %d in opening %s...", retval,
1921 tty->name);
1922#endif
1923 tty_unlock(); /* need to call tty_release without BTM */
1924 tty_release(inode, filp);
1925 if (retval != -ERESTARTSYS)
1926 return retval;
1927
1928 if (signal_pending(current))
1929 return retval;
1930
1931 schedule();
1932 /*
1933 * Need to reset f_op in case a hangup happened.
1934 */
1935 tty_lock();
1936 if (filp->f_op == &hung_up_tty_fops)
1937 filp->f_op = &tty_fops;
1938 tty_unlock();
1939 goto retry_open;
1940 }
1941 tty_unlock();
1942
1943
1944 mutex_lock(&tty_mutex);
1945 tty_lock();
1946 spin_lock_irq(&current->sighand->siglock);
1947 if (!noctty &&
1948 current->signal->leader &&
1949 !current->signal->tty &&
1950 tty->session == NULL)
1951 __proc_set_tty(current, tty);
1952 spin_unlock_irq(&current->sighand->siglock);
1953 tty_unlock();
1954 mutex_unlock(&tty_mutex);
1955 return 0;
1956}
1957
1958
1959
1960/**
1961 * tty_poll - check tty status
1962 * @filp: file being polled
1963 * @wait: poll wait structures to update
1964 *
1965 * Call the line discipline polling method to obtain the poll
1966 * status of the device.
1967 *
1968 * Locking: locks called line discipline but ldisc poll method
1969 * may be re-entered freely by other callers.
1970 */
1971
1972static unsigned int tty_poll(struct file *filp, poll_table *wait)
1973{
1974 struct tty_struct *tty = file_tty(filp);
1975 struct tty_ldisc *ld;
1976 int ret = 0;
1977
1978 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1979 return 0;
1980
1981 ld = tty_ldisc_ref_wait(tty);
1982 if (ld->ops->poll)
1983 ret = (ld->ops->poll)(tty, filp, wait);
1984 tty_ldisc_deref(ld);
1985 return ret;
1986}
1987
1988static int __tty_fasync(int fd, struct file *filp, int on)
1989{
1990 struct tty_struct *tty = file_tty(filp);
1991 unsigned long flags;
1992 int retval = 0;
1993
1994 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
1995 goto out;
1996
1997 retval = fasync_helper(fd, filp, on, &tty->fasync);
1998 if (retval <= 0)
1999 goto out;
2000
2001 if (on) {
2002 enum pid_type type;
2003 struct pid *pid;
2004 if (!waitqueue_active(&tty->read_wait))
2005 tty->minimum_to_wake = 1;
2006 spin_lock_irqsave(&tty->ctrl_lock, flags);
2007 if (tty->pgrp) {
2008 pid = tty->pgrp;
2009 type = PIDTYPE_PGID;
2010 } else {
2011 pid = task_pid(current);
2012 type = PIDTYPE_PID;
2013 }
2014 get_pid(pid);
2015 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2016 retval = __f_setown(filp, pid, type, 0);
2017 put_pid(pid);
2018 if (retval)
2019 goto out;
2020 } else {
2021 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2022 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2023 }
2024 retval = 0;
2025out:
2026 return retval;
2027}
2028
2029static int tty_fasync(int fd, struct file *filp, int on)
2030{
2031 int retval;
2032 tty_lock();
2033 retval = __tty_fasync(fd, filp, on);
2034 tty_unlock();
2035 return retval;
2036}
2037
2038/**
2039 * tiocsti - fake input character
2040 * @tty: tty to fake input into
2041 * @p: pointer to character
2042 *
2043 * Fake input to a tty device. Does the necessary locking and
2044 * input management.
2045 *
2046 * FIXME: does not honour flow control ??
2047 *
2048 * Locking:
2049 * Called functions take tty_ldisc_lock
2050 * current->signal->tty check is safe without locks
2051 *
2052 * FIXME: may race normal receive processing
2053 */
2054
2055static int tiocsti(struct tty_struct *tty, char __user *p)
2056{
2057 char ch, mbz = 0;
2058 struct tty_ldisc *ld;
2059
2060 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2061 return -EPERM;
2062 if (get_user(ch, p))
2063 return -EFAULT;
2064 tty_audit_tiocsti(tty, ch);
2065 ld = tty_ldisc_ref_wait(tty);
2066 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2067 tty_ldisc_deref(ld);
2068 return 0;
2069}
2070
2071/**
2072 * tiocgwinsz - implement window query ioctl
2073 * @tty; tty
2074 * @arg: user buffer for result
2075 *
2076 * Copies the kernel idea of the window size into the user buffer.
2077 *
2078 * Locking: tty->termios_mutex is taken to ensure the winsize data
2079 * is consistent.
2080 */
2081
2082static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2083{
2084 int err;
2085
2086 mutex_lock(&tty->termios_mutex);
2087 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2088 mutex_unlock(&tty->termios_mutex);
2089
2090 return err ? -EFAULT: 0;
2091}
2092
2093/**
2094 * tty_do_resize - resize event
2095 * @tty: tty being resized
2096 * @rows: rows (character)
2097 * @cols: cols (character)
2098 *
2099 * Update the termios variables and send the necessary signals to
2100 * peform a terminal resize correctly
2101 */
2102
2103int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2104{
2105 struct pid *pgrp;
2106 unsigned long flags;
2107
2108 /* Lock the tty */
2109 mutex_lock(&tty->termios_mutex);
2110 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2111 goto done;
2112 /* Get the PID values and reference them so we can
2113 avoid holding the tty ctrl lock while sending signals */
2114 spin_lock_irqsave(&tty->ctrl_lock, flags);
2115 pgrp = get_pid(tty->pgrp);
2116 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2117
2118 if (pgrp)
2119 kill_pgrp(pgrp, SIGWINCH, 1);
2120 put_pid(pgrp);
2121
2122 tty->winsize = *ws;
2123done:
2124 mutex_unlock(&tty->termios_mutex);
2125 return 0;
2126}
2127
2128/**
2129 * tiocswinsz - implement window size set ioctl
2130 * @tty; tty side of tty
2131 * @arg: user buffer for result
2132 *
2133 * Copies the user idea of the window size to the kernel. Traditionally
2134 * this is just advisory information but for the Linux console it
2135 * actually has driver level meaning and triggers a VC resize.
2136 *
2137 * Locking:
2138 * Driver dependent. The default do_resize method takes the
2139 * tty termios mutex and ctrl_lock. The console takes its own lock
2140 * then calls into the default method.
2141 */
2142
2143static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2144{
2145 struct winsize tmp_ws;
2146 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2147 return -EFAULT;
2148
2149 if (tty->ops->resize)
2150 return tty->ops->resize(tty, &tmp_ws);
2151 else
2152 return tty_do_resize(tty, &tmp_ws);
2153}
2154
2155/**
2156 * tioccons - allow admin to move logical console
2157 * @file: the file to become console
2158 *
2159 * Allow the administrator to move the redirected console device
2160 *
2161 * Locking: uses redirect_lock to guard the redirect information
2162 */
2163
2164static int tioccons(struct file *file)
2165{
2166 if (!capable(CAP_SYS_ADMIN))
2167 return -EPERM;
2168 if (file->f_op->write == redirected_tty_write) {
2169 struct file *f;
2170 spin_lock(&redirect_lock);
2171 f = redirect;
2172 redirect = NULL;
2173 spin_unlock(&redirect_lock);
2174 if (f)
2175 fput(f);
2176 return 0;
2177 }
2178 spin_lock(&redirect_lock);
2179 if (redirect) {
2180 spin_unlock(&redirect_lock);
2181 return -EBUSY;
2182 }
2183 get_file(file);
2184 redirect = file;
2185 spin_unlock(&redirect_lock);
2186 return 0;
2187}
2188
2189/**
2190 * fionbio - non blocking ioctl
2191 * @file: file to set blocking value
2192 * @p: user parameter
2193 *
2194 * Historical tty interfaces had a blocking control ioctl before
2195 * the generic functionality existed. This piece of history is preserved
2196 * in the expected tty API of posix OS's.
2197 *
2198 * Locking: none, the open file handle ensures it won't go away.
2199 */
2200
2201static int fionbio(struct file *file, int __user *p)
2202{
2203 int nonblock;
2204
2205 if (get_user(nonblock, p))
2206 return -EFAULT;
2207
2208 spin_lock(&file->f_lock);
2209 if (nonblock)
2210 file->f_flags |= O_NONBLOCK;
2211 else
2212 file->f_flags &= ~O_NONBLOCK;
2213 spin_unlock(&file->f_lock);
2214 return 0;
2215}
2216
2217/**
2218 * tiocsctty - set controlling tty
2219 * @tty: tty structure
2220 * @arg: user argument
2221 *
2222 * This ioctl is used to manage job control. It permits a session
2223 * leader to set this tty as the controlling tty for the session.
2224 *
2225 * Locking:
2226 * Takes tty_mutex() to protect tty instance
2227 * Takes tasklist_lock internally to walk sessions
2228 * Takes ->siglock() when updating signal->tty
2229 */
2230
2231static int tiocsctty(struct tty_struct *tty, int arg)
2232{
2233 int ret = 0;
2234 if (current->signal->leader && (task_session(current) == tty->session))
2235 return ret;
2236
2237 mutex_lock(&tty_mutex);
2238 /*
2239 * The process must be a session leader and
2240 * not have a controlling tty already.
2241 */
2242 if (!current->signal->leader || current->signal->tty) {
2243 ret = -EPERM;
2244 goto unlock;
2245 }
2246
2247 if (tty->session) {
2248 /*
2249 * This tty is already the controlling
2250 * tty for another session group!
2251 */
2252 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2253 /*
2254 * Steal it away
2255 */
2256 read_lock(&tasklist_lock);
2257 session_clear_tty(tty->session);
2258 read_unlock(&tasklist_lock);
2259 } else {
2260 ret = -EPERM;
2261 goto unlock;
2262 }
2263 }
2264 proc_set_tty(current, tty);
2265unlock:
2266 mutex_unlock(&tty_mutex);
2267 return ret;
2268}
2269
2270/**
2271 * tty_get_pgrp - return a ref counted pgrp pid
2272 * @tty: tty to read
2273 *
2274 * Returns a refcounted instance of the pid struct for the process
2275 * group controlling the tty.
2276 */
2277
2278struct pid *tty_get_pgrp(struct tty_struct *tty)
2279{
2280 unsigned long flags;
2281 struct pid *pgrp;
2282
2283 spin_lock_irqsave(&tty->ctrl_lock, flags);
2284 pgrp = get_pid(tty->pgrp);
2285 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2286
2287 return pgrp;
2288}
2289EXPORT_SYMBOL_GPL(tty_get_pgrp);
2290
2291/**
2292 * tiocgpgrp - get process group
2293 * @tty: tty passed by user
2294 * @real_tty: tty side of the tty passed by the user if a pty else the tty
2295 * @p: returned pid
2296 *
2297 * Obtain the process group of the tty. If there is no process group
2298 * return an error.
2299 *
2300 * Locking: none. Reference to current->signal->tty is safe.
2301 */
2302
2303static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2304{
2305 struct pid *pid;
2306 int ret;
2307 /*
2308 * (tty == real_tty) is a cheap way of
2309 * testing if the tty is NOT a master pty.
2310 */
2311 if (tty == real_tty && current->signal->tty != real_tty)
2312 return -ENOTTY;
2313 pid = tty_get_pgrp(real_tty);
2314 ret = put_user(pid_vnr(pid), p);
2315 put_pid(pid);
2316 return ret;
2317}
2318
2319/**
2320 * tiocspgrp - attempt to set process group
2321 * @tty: tty passed by user
2322 * @real_tty: tty side device matching tty passed by user
2323 * @p: pid pointer
2324 *
2325 * Set the process group of the tty to the session passed. Only
2326 * permitted where the tty session is our session.
2327 *
2328 * Locking: RCU, ctrl lock
2329 */
2330
2331static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2332{
2333 struct pid *pgrp;
2334 pid_t pgrp_nr;
2335 int retval = tty_check_change(real_tty);
2336 unsigned long flags;
2337
2338 if (retval == -EIO)
2339 return -ENOTTY;
2340 if (retval)
2341 return retval;
2342 if (!current->signal->tty ||
2343 (current->signal->tty != real_tty) ||
2344 (real_tty->session != task_session(current)))
2345 return -ENOTTY;
2346 if (get_user(pgrp_nr, p))
2347 return -EFAULT;
2348 if (pgrp_nr < 0)
2349 return -EINVAL;
2350 rcu_read_lock();
2351 pgrp = find_vpid(pgrp_nr);
2352 retval = -ESRCH;
2353 if (!pgrp)
2354 goto out_unlock;
2355 retval = -EPERM;
2356 if (session_of_pgrp(pgrp) != task_session(current))
2357 goto out_unlock;
2358 retval = 0;
2359 spin_lock_irqsave(&tty->ctrl_lock, flags);
2360 put_pid(real_tty->pgrp);
2361 real_tty->pgrp = get_pid(pgrp);
2362 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2363out_unlock:
2364 rcu_read_unlock();
2365 return retval;
2366}
2367
2368/**
2369 * tiocgsid - get session id
2370 * @tty: tty passed by user
2371 * @real_tty: tty side of the tty passed by the user if a pty else the tty
2372 * @p: pointer to returned session id
2373 *
2374 * Obtain the session id of the tty. If there is no session
2375 * return an error.
2376 *
2377 * Locking: none. Reference to current->signal->tty is safe.
2378 */
2379
2380static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2381{
2382 /*
2383 * (tty == real_tty) is a cheap way of
2384 * testing if the tty is NOT a master pty.
2385 */
2386 if (tty == real_tty && current->signal->tty != real_tty)
2387 return -ENOTTY;
2388 if (!real_tty->session)
2389 return -ENOTTY;
2390 return put_user(pid_vnr(real_tty->session), p);
2391}
2392
2393/**
2394 * tiocsetd - set line discipline
2395 * @tty: tty device
2396 * @p: pointer to user data
2397 *
2398 * Set the line discipline according to user request.
2399 *
2400 * Locking: see tty_set_ldisc, this function is just a helper
2401 */
2402
2403static int tiocsetd(struct tty_struct *tty, int __user *p)
2404{
2405 int ldisc;
2406 int ret;
2407
2408 if (get_user(ldisc, p))
2409 return -EFAULT;
2410
2411 ret = tty_set_ldisc(tty, ldisc);
2412
2413 return ret;
2414}
2415
2416/**
2417 * send_break - performed time break
2418 * @tty: device to break on
2419 * @duration: timeout in mS
2420 *
2421 * Perform a timed break on hardware that lacks its own driver level
2422 * timed break functionality.
2423 *
2424 * Locking:
2425 * atomic_write_lock serializes
2426 *
2427 */
2428
2429static int send_break(struct tty_struct *tty, unsigned int duration)
2430{
2431 int retval;
2432
2433 if (tty->ops->break_ctl == NULL)
2434 return 0;
2435
2436 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2437 retval = tty->ops->break_ctl(tty, duration);
2438 else {
2439 /* Do the work ourselves */
2440 if (tty_write_lock(tty, 0) < 0)
2441 return -EINTR;
2442 retval = tty->ops->break_ctl(tty, -1);
2443 if (retval)
2444 goto out;
2445 if (!signal_pending(current))
2446 msleep_interruptible(duration);
2447 retval = tty->ops->break_ctl(tty, 0);
2448out:
2449 tty_write_unlock(tty);
2450 if (signal_pending(current))
2451 retval = -EINTR;
2452 }
2453 return retval;
2454}
2455
2456/**
2457 * tty_tiocmget - get modem status
2458 * @tty: tty device
2459 * @file: user file pointer
2460 * @p: pointer to result
2461 *
2462 * Obtain the modem status bits from the tty driver if the feature
2463 * is supported. Return -EINVAL if it is not available.
2464 *
2465 * Locking: none (up to the driver)
2466 */
2467
2468static int tty_tiocmget(struct tty_struct *tty, int __user *p)
2469{
2470 int retval = -EINVAL;
2471
2472 if (tty->ops->tiocmget) {
2473 retval = tty->ops->tiocmget(tty);
2474
2475 if (retval >= 0)
2476 retval = put_user(retval, p);
2477 }
2478 return retval;
2479}
2480
2481/**
2482 * tty_tiocmset - set modem status
2483 * @tty: tty device
2484 * @cmd: command - clear bits, set bits or set all
2485 * @p: pointer to desired bits
2486 *
2487 * Set the modem status bits from the tty driver if the feature
2488 * is supported. Return -EINVAL if it is not available.
2489 *
2490 * Locking: none (up to the driver)
2491 */
2492
2493static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
2494 unsigned __user *p)
2495{
2496 int retval;
2497 unsigned int set, clear, val;
2498
2499 if (tty->ops->tiocmset == NULL)
2500 return -EINVAL;
2501
2502 retval = get_user(val, p);
2503 if (retval)
2504 return retval;
2505 set = clear = 0;
2506 switch (cmd) {
2507 case TIOCMBIS:
2508 set = val;
2509 break;
2510 case TIOCMBIC:
2511 clear = val;
2512 break;
2513 case TIOCMSET:
2514 set = val;
2515 clear = ~val;
2516 break;
2517 }
2518 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2519 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2520 return tty->ops->tiocmset(tty, set, clear);
2521}
2522
2523static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2524{
2525 int retval = -EINVAL;
2526 struct serial_icounter_struct icount;
2527 memset(&icount, 0, sizeof(icount));
2528 if (tty->ops->get_icount)
2529 retval = tty->ops->get_icount(tty, &icount);
2530 if (retval != 0)
2531 return retval;
2532 if (copy_to_user(arg, &icount, sizeof(icount)))
2533 return -EFAULT;
2534 return 0;
2535}
2536
2537struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2538{
2539 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2540 tty->driver->subtype == PTY_TYPE_MASTER)
2541 tty = tty->link;
2542 return tty;
2543}
2544EXPORT_SYMBOL(tty_pair_get_tty);
2545
2546struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2547{
2548 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2549 tty->driver->subtype == PTY_TYPE_MASTER)
2550 return tty;
2551 return tty->link;
2552}
2553EXPORT_SYMBOL(tty_pair_get_pty);
2554
2555/*
2556 * Split this up, as gcc can choke on it otherwise..
2557 */
2558long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2559{
2560 struct tty_struct *tty = file_tty(file);
2561 struct tty_struct *real_tty;
2562 void __user *p = (void __user *)arg;
2563 int retval;
2564 struct tty_ldisc *ld;
2565 struct inode *inode = file->f_dentry->d_inode;
2566
2567 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2568 return -EINVAL;
2569
2570 real_tty = tty_pair_get_tty(tty);
2571
2572 /*
2573 * Factor out some common prep work
2574 */
2575 switch (cmd) {
2576 case TIOCSETD:
2577 case TIOCSBRK:
2578 case TIOCCBRK:
2579 case TCSBRK:
2580 case TCSBRKP:
2581 retval = tty_check_change(tty);
2582 if (retval)
2583 return retval;
2584 if (cmd != TIOCCBRK) {
2585 tty_wait_until_sent(tty, 0);
2586 if (signal_pending(current))
2587 return -EINTR;
2588 }
2589 break;
2590 }
2591
2592 /*
2593 * Now do the stuff.
2594 */
2595 switch (cmd) {
2596 case TIOCSTI:
2597 return tiocsti(tty, p);
2598 case TIOCGWINSZ:
2599 return tiocgwinsz(real_tty, p);
2600 case TIOCSWINSZ:
2601 return tiocswinsz(real_tty, p);
2602 case TIOCCONS:
2603 return real_tty != tty ? -EINVAL : tioccons(file);
2604 case FIONBIO:
2605 return fionbio(file, p);
2606 case TIOCEXCL:
2607 set_bit(TTY_EXCLUSIVE, &tty->flags);
2608 return 0;
2609 case TIOCNXCL:
2610 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2611 return 0;
2612 case TIOCNOTTY:
2613 if (current->signal->tty != tty)
2614 return -ENOTTY;
2615 no_tty();
2616 return 0;
2617 case TIOCSCTTY:
2618 return tiocsctty(tty, arg);
2619 case TIOCGPGRP:
2620 return tiocgpgrp(tty, real_tty, p);
2621 case TIOCSPGRP:
2622 return tiocspgrp(tty, real_tty, p);
2623 case TIOCGSID:
2624 return tiocgsid(tty, real_tty, p);
2625 case TIOCGETD:
2626 return put_user(tty->ldisc->ops->num, (int __user *)p);
2627 case TIOCSETD:
2628 return tiocsetd(tty, p);
2629 case TIOCVHANGUP:
2630 if (!capable(CAP_SYS_ADMIN))
2631 return -EPERM;
2632 tty_vhangup(tty);
2633 return 0;
2634 case TIOCGDEV:
2635 {
2636 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2637 return put_user(ret, (unsigned int __user *)p);
2638 }
2639 /*
2640 * Break handling
2641 */
2642 case TIOCSBRK: /* Turn break on, unconditionally */
2643 if (tty->ops->break_ctl)
2644 return tty->ops->break_ctl(tty, -1);
2645 return 0;
2646 case TIOCCBRK: /* Turn break off, unconditionally */
2647 if (tty->ops->break_ctl)
2648 return tty->ops->break_ctl(tty, 0);
2649 return 0;
2650 case TCSBRK: /* SVID version: non-zero arg --> no break */
2651 /* non-zero arg means wait for all output data
2652 * to be sent (performed above) but don't send break.
2653 * This is used by the tcdrain() termios function.
2654 */
2655 if (!arg)
2656 return send_break(tty, 250);
2657 return 0;
2658 case TCSBRKP: /* support for POSIX tcsendbreak() */
2659 return send_break(tty, arg ? arg*100 : 250);
2660
2661 case TIOCMGET:
2662 return tty_tiocmget(tty, p);
2663 case TIOCMSET:
2664 case TIOCMBIC:
2665 case TIOCMBIS:
2666 return tty_tiocmset(tty, cmd, p);
2667 case TIOCGICOUNT:
2668 retval = tty_tiocgicount(tty, p);
2669 /* For the moment allow fall through to the old method */
2670 if (retval != -EINVAL)
2671 return retval;
2672 break;
2673 case TCFLSH:
2674 switch (arg) {
2675 case TCIFLUSH:
2676 case TCIOFLUSH:
2677 /* flush tty buffer and allow ldisc to process ioctl */
2678 tty_buffer_flush(tty);
2679 break;
2680 }
2681 break;
2682 }
2683 if (tty->ops->ioctl) {
2684 retval = (tty->ops->ioctl)(tty, cmd, arg);
2685 if (retval != -ENOIOCTLCMD)
2686 return retval;
2687 }
2688 ld = tty_ldisc_ref_wait(tty);
2689 retval = -EINVAL;
2690 if (ld->ops->ioctl) {
2691 retval = ld->ops->ioctl(tty, file, cmd, arg);
2692 if (retval == -ENOIOCTLCMD)
2693 retval = -EINVAL;
2694 }
2695 tty_ldisc_deref(ld);
2696 return retval;
2697}
2698
2699#ifdef CONFIG_COMPAT
2700static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2701 unsigned long arg)
2702{
2703 struct inode *inode = file->f_dentry->d_inode;
2704 struct tty_struct *tty = file_tty(file);
2705 struct tty_ldisc *ld;
2706 int retval = -ENOIOCTLCMD;
2707
2708 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2709 return -EINVAL;
2710
2711 if (tty->ops->compat_ioctl) {
2712 retval = (tty->ops->compat_ioctl)(tty, cmd, arg);
2713 if (retval != -ENOIOCTLCMD)
2714 return retval;
2715 }
2716
2717 ld = tty_ldisc_ref_wait(tty);
2718 if (ld->ops->compat_ioctl)
2719 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2720 tty_ldisc_deref(ld);
2721
2722 return retval;
2723}
2724#endif
2725
2726/*
2727 * This implements the "Secure Attention Key" --- the idea is to
2728 * prevent trojan horses by killing all processes associated with this
2729 * tty when the user hits the "Secure Attention Key". Required for
2730 * super-paranoid applications --- see the Orange Book for more details.
2731 *
2732 * This code could be nicer; ideally it should send a HUP, wait a few
2733 * seconds, then send a INT, and then a KILL signal. But you then
2734 * have to coordinate with the init process, since all processes associated
2735 * with the current tty must be dead before the new getty is allowed
2736 * to spawn.
2737 *
2738 * Now, if it would be correct ;-/ The current code has a nasty hole -
2739 * it doesn't catch files in flight. We may send the descriptor to ourselves
2740 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2741 *
2742 * Nasty bug: do_SAK is being called in interrupt context. This can
2743 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2744 */
2745void __do_SAK(struct tty_struct *tty)
2746{
2747#ifdef TTY_SOFT_SAK
2748 tty_hangup(tty);
2749#else
2750 struct task_struct *g, *p;
2751 struct pid *session;
2752 int i;
2753 struct file *filp;
2754 struct fdtable *fdt;
2755
2756 if (!tty)
2757 return;
2758 session = tty->session;
2759
2760 tty_ldisc_flush(tty);
2761
2762 tty_driver_flush_buffer(tty);
2763
2764 read_lock(&tasklist_lock);
2765 /* Kill the entire session */
2766 do_each_pid_task(session, PIDTYPE_SID, p) {
2767 printk(KERN_NOTICE "SAK: killed process %d"
2768 " (%s): task_session(p)==tty->session\n",
2769 task_pid_nr(p), p->comm);
2770 send_sig(SIGKILL, p, 1);
2771 } while_each_pid_task(session, PIDTYPE_SID, p);
2772 /* Now kill any processes that happen to have the
2773 * tty open.
2774 */
2775 do_each_thread(g, p) {
2776 if (p->signal->tty == tty) {
2777 printk(KERN_NOTICE "SAK: killed process %d"
2778 " (%s): task_session(p)==tty->session\n",
2779 task_pid_nr(p), p->comm);
2780 send_sig(SIGKILL, p, 1);
2781 continue;
2782 }
2783 task_lock(p);
2784 if (p->files) {
2785 /*
2786 * We don't take a ref to the file, so we must
2787 * hold ->file_lock instead.
2788 */
2789 spin_lock(&p->files->file_lock);
2790 fdt = files_fdtable(p->files);
2791 for (i = 0; i < fdt->max_fds; i++) {
2792 filp = fcheck_files(p->files, i);
2793 if (!filp)
2794 continue;
2795 if (filp->f_op->read == tty_read &&
2796 file_tty(filp) == tty) {
2797 printk(KERN_NOTICE "SAK: killed process %d"
2798 " (%s): fd#%d opened to the tty\n",
2799 task_pid_nr(p), p->comm, i);
2800 force_sig(SIGKILL, p);
2801 break;
2802 }
2803 }
2804 spin_unlock(&p->files->file_lock);
2805 }
2806 task_unlock(p);
2807 } while_each_thread(g, p);
2808 read_unlock(&tasklist_lock);
2809#endif
2810}
2811
2812static void do_SAK_work(struct work_struct *work)
2813{
2814 struct tty_struct *tty =
2815 container_of(work, struct tty_struct, SAK_work);
2816 __do_SAK(tty);
2817}
2818
2819/*
2820 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2821 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2822 * the values which we write to it will be identical to the values which it
2823 * already has. --akpm
2824 */
2825void do_SAK(struct tty_struct *tty)
2826{
2827 if (!tty)
2828 return;
2829 schedule_work(&tty->SAK_work);
2830}
2831
2832EXPORT_SYMBOL(do_SAK);
2833
2834static int dev_match_devt(struct device *dev, void *data)
2835{
2836 dev_t *devt = data;
2837 return dev->devt == *devt;
2838}
2839
2840/* Must put_device() after it's unused! */
2841static struct device *tty_get_device(struct tty_struct *tty)
2842{
2843 dev_t devt = tty_devnum(tty);
2844 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
2845}
2846
2847
2848/**
2849 * initialize_tty_struct
2850 * @tty: tty to initialize
2851 *
2852 * This subroutine initializes a tty structure that has been newly
2853 * allocated.
2854 *
2855 * Locking: none - tty in question must not be exposed at this point
2856 */
2857
2858void initialize_tty_struct(struct tty_struct *tty,
2859 struct tty_driver *driver, int idx)
2860{
2861 memset(tty, 0, sizeof(struct tty_struct));
2862 kref_init(&tty->kref);
2863 tty->magic = TTY_MAGIC;
2864 tty_ldisc_init(tty);
2865 tty->session = NULL;
2866 tty->pgrp = NULL;
2867 tty->overrun_time = jiffies;
2868 tty->buf.head = tty->buf.tail = NULL;
2869 tty_buffer_init(tty);
2870 mutex_init(&tty->termios_mutex);
2871 mutex_init(&tty->ldisc_mutex);
2872 init_waitqueue_head(&tty->write_wait);
2873 init_waitqueue_head(&tty->read_wait);
2874 INIT_WORK(&tty->hangup_work, do_tty_hangup);
2875 mutex_init(&tty->atomic_read_lock);
2876 mutex_init(&tty->atomic_write_lock);
2877 mutex_init(&tty->output_lock);
2878 mutex_init(&tty->echo_lock);
2879 spin_lock_init(&tty->read_lock);
2880 spin_lock_init(&tty->ctrl_lock);
2881 INIT_LIST_HEAD(&tty->tty_files);
2882 INIT_WORK(&tty->SAK_work, do_SAK_work);
2883
2884 tty->driver = driver;
2885 tty->ops = driver->ops;
2886 tty->index = idx;
2887 tty_line_name(driver, idx, tty->name);
2888 tty->dev = tty_get_device(tty);
2889}
2890
2891/**
2892 * deinitialize_tty_struct
2893 * @tty: tty to deinitialize
2894 *
2895 * This subroutine deinitializes a tty structure that has been newly
2896 * allocated but tty_release cannot be called on that yet.
2897 *
2898 * Locking: none - tty in question must not be exposed at this point
2899 */
2900void deinitialize_tty_struct(struct tty_struct *tty)
2901{
2902 tty_ldisc_deinit(tty);
2903}
2904
2905/**
2906 * tty_put_char - write one character to a tty
2907 * @tty: tty
2908 * @ch: character
2909 *
2910 * Write one byte to the tty using the provided put_char method
2911 * if present. Returns the number of characters successfully output.
2912 *
2913 * Note: the specific put_char operation in the driver layer may go
2914 * away soon. Don't call it directly, use this method
2915 */
2916
2917int tty_put_char(struct tty_struct *tty, unsigned char ch)
2918{
2919 if (tty->ops->put_char)
2920 return tty->ops->put_char(tty, ch);
2921 return tty->ops->write(tty, &ch, 1);
2922}
2923EXPORT_SYMBOL_GPL(tty_put_char);
2924
2925struct class *tty_class;
2926
2927/**
2928 * tty_register_device - register a tty device
2929 * @driver: the tty driver that describes the tty device
2930 * @index: the index in the tty driver for this tty device
2931 * @device: a struct device that is associated with this tty device.
2932 * This field is optional, if there is no known struct device
2933 * for this tty device it can be set to NULL safely.
2934 *
2935 * Returns a pointer to the struct device for this tty device
2936 * (or ERR_PTR(-EFOO) on error).
2937 *
2938 * This call is required to be made to register an individual tty device
2939 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2940 * that bit is not set, this function should not be called by a tty
2941 * driver.
2942 *
2943 * Locking: ??
2944 */
2945
2946struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2947 struct device *device)
2948{
2949 char name[64];
2950 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2951
2952 if (index >= driver->num) {
2953 printk(KERN_ERR "Attempt to register invalid tty line number "
2954 " (%d).\n", index);
2955 return ERR_PTR(-EINVAL);
2956 }
2957
2958 if (driver->type == TTY_DRIVER_TYPE_PTY)
2959 pty_line_name(driver, index, name);
2960 else
2961 tty_line_name(driver, index, name);
2962
2963 return device_create(tty_class, device, dev, NULL, name);
2964}
2965EXPORT_SYMBOL(tty_register_device);
2966
2967/**
2968 * tty_unregister_device - unregister a tty device
2969 * @driver: the tty driver that describes the tty device
2970 * @index: the index in the tty driver for this tty device
2971 *
2972 * If a tty device is registered with a call to tty_register_device() then
2973 * this function must be called when the tty device is gone.
2974 *
2975 * Locking: ??
2976 */
2977
2978void tty_unregister_device(struct tty_driver *driver, unsigned index)
2979{
2980 device_destroy(tty_class,
2981 MKDEV(driver->major, driver->minor_start) + index);
2982}
2983EXPORT_SYMBOL(tty_unregister_device);
2984
2985struct tty_driver *alloc_tty_driver(int lines)
2986{
2987 struct tty_driver *driver;
2988
2989 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
2990 if (driver) {
2991 kref_init(&driver->kref);
2992 driver->magic = TTY_DRIVER_MAGIC;
2993 driver->num = lines;
2994 /* later we'll move allocation of tables here */
2995 }
2996 return driver;
2997}
2998EXPORT_SYMBOL(alloc_tty_driver);
2999
3000static void destruct_tty_driver(struct kref *kref)
3001{
3002 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3003 int i;
3004 struct ktermios *tp;
3005 void *p;
3006
3007 if (driver->flags & TTY_DRIVER_INSTALLED) {
3008 /*
3009 * Free the termios and termios_locked structures because
3010 * we don't want to get memory leaks when modular tty
3011 * drivers are removed from the kernel.
3012 */
3013 for (i = 0; i < driver->num; i++) {
3014 tp = driver->termios[i];
3015 if (tp) {
3016 driver->termios[i] = NULL;
3017 kfree(tp);
3018 }
3019 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3020 tty_unregister_device(driver, i);
3021 }
3022 p = driver->ttys;
3023 proc_tty_unregister_driver(driver);
3024 driver->ttys = NULL;
3025 driver->termios = NULL;
3026 kfree(p);
3027 cdev_del(&driver->cdev);
3028 }
3029 kfree(driver);
3030}
3031
3032void tty_driver_kref_put(struct tty_driver *driver)
3033{
3034 kref_put(&driver->kref, destruct_tty_driver);
3035}
3036EXPORT_SYMBOL(tty_driver_kref_put);
3037
3038void tty_set_operations(struct tty_driver *driver,
3039 const struct tty_operations *op)
3040{
3041 driver->ops = op;
3042};
3043EXPORT_SYMBOL(tty_set_operations);
3044
3045void put_tty_driver(struct tty_driver *d)
3046{
3047 tty_driver_kref_put(d);
3048}
3049EXPORT_SYMBOL(put_tty_driver);
3050
3051/*
3052 * Called by a tty driver to register itself.
3053 */
3054int tty_register_driver(struct tty_driver *driver)
3055{
3056 int error;
3057 int i;
3058 dev_t dev;
3059 void **p = NULL;
3060 struct device *d;
3061
3062 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
3063 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
3064 if (!p)
3065 return -ENOMEM;
3066 }
3067
3068 if (!driver->major) {
3069 error = alloc_chrdev_region(&dev, driver->minor_start,
3070 driver->num, driver->name);
3071 if (!error) {
3072 driver->major = MAJOR(dev);
3073 driver->minor_start = MINOR(dev);
3074 }
3075 } else {
3076 dev = MKDEV(driver->major, driver->minor_start);
3077 error = register_chrdev_region(dev, driver->num, driver->name);
3078 }
3079 if (error < 0) {
3080 kfree(p);
3081 return error;
3082 }
3083
3084 if (p) {
3085 driver->ttys = (struct tty_struct **)p;
3086 driver->termios = (struct ktermios **)(p + driver->num);
3087 } else {
3088 driver->ttys = NULL;
3089 driver->termios = NULL;
3090 }
3091
3092 cdev_init(&driver->cdev, &tty_fops);
3093 driver->cdev.owner = driver->owner;
3094 error = cdev_add(&driver->cdev, dev, driver->num);
3095 if (error) {
3096 unregister_chrdev_region(dev, driver->num);
3097 driver->ttys = NULL;
3098 driver->termios = NULL;
3099 kfree(p);
3100 return error;
3101 }
3102
3103 mutex_lock(&tty_mutex);
3104 list_add(&driver->tty_drivers, &tty_drivers);
3105 mutex_unlock(&tty_mutex);
3106
3107 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3108 for (i = 0; i < driver->num; i++) {
3109 d = tty_register_device(driver, i, NULL);
3110 if (IS_ERR(d)) {
3111 error = PTR_ERR(d);
3112 goto err;
3113 }
3114 }
3115 }
3116 proc_tty_register_driver(driver);
3117 driver->flags |= TTY_DRIVER_INSTALLED;
3118 return 0;
3119
3120err:
3121 for (i--; i >= 0; i--)
3122 tty_unregister_device(driver, i);
3123
3124 mutex_lock(&tty_mutex);
3125 list_del(&driver->tty_drivers);
3126 mutex_unlock(&tty_mutex);
3127
3128 unregister_chrdev_region(dev, driver->num);
3129 driver->ttys = NULL;
3130 driver->termios = NULL;
3131 kfree(p);
3132 return error;
3133}
3134
3135EXPORT_SYMBOL(tty_register_driver);
3136
3137/*
3138 * Called by a tty driver to unregister itself.
3139 */
3140int tty_unregister_driver(struct tty_driver *driver)
3141{
3142#if 0
3143 /* FIXME */
3144 if (driver->refcount)
3145 return -EBUSY;
3146#endif
3147 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3148 driver->num);
3149 mutex_lock(&tty_mutex);
3150 list_del(&driver->tty_drivers);
3151 mutex_unlock(&tty_mutex);
3152 return 0;
3153}
3154
3155EXPORT_SYMBOL(tty_unregister_driver);
3156
3157dev_t tty_devnum(struct tty_struct *tty)
3158{
3159 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3160}
3161EXPORT_SYMBOL(tty_devnum);
3162
3163void proc_clear_tty(struct task_struct *p)
3164{
3165 unsigned long flags;
3166 struct tty_struct *tty;
3167 spin_lock_irqsave(&p->sighand->siglock, flags);
3168 tty = p->signal->tty;
3169 p->signal->tty = NULL;
3170 spin_unlock_irqrestore(&p->sighand->siglock, flags);
3171 tty_kref_put(tty);
3172}
3173
3174/* Called under the sighand lock */
3175
3176static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3177{
3178 if (tty) {
3179 unsigned long flags;
3180 /* We should not have a session or pgrp to put here but.... */
3181 spin_lock_irqsave(&tty->ctrl_lock, flags);
3182 put_pid(tty->session);
3183 put_pid(tty->pgrp);
3184 tty->pgrp = get_pid(task_pgrp(tsk));
3185 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3186 tty->session = get_pid(task_session(tsk));
3187 if (tsk->signal->tty) {
3188 printk(KERN_DEBUG "tty not NULL!!\n");
3189 tty_kref_put(tsk->signal->tty);
3190 }
3191 }
3192 put_pid(tsk->signal->tty_old_pgrp);
3193 tsk->signal->tty = tty_kref_get(tty);
3194 tsk->signal->tty_old_pgrp = NULL;
3195}
3196
3197static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3198{
3199 spin_lock_irq(&tsk->sighand->siglock);
3200 __proc_set_tty(tsk, tty);
3201 spin_unlock_irq(&tsk->sighand->siglock);
3202}
3203
3204struct tty_struct *get_current_tty(void)
3205{
3206 struct tty_struct *tty;
3207 unsigned long flags;
3208
3209 spin_lock_irqsave(&current->sighand->siglock, flags);
3210 tty = tty_kref_get(current->signal->tty);
3211 spin_unlock_irqrestore(&current->sighand->siglock, flags);
3212 return tty;
3213}
3214EXPORT_SYMBOL_GPL(get_current_tty);
3215
3216void tty_default_fops(struct file_operations *fops)
3217{
3218 *fops = tty_fops;
3219}
3220
3221/*
3222 * Initialize the console device. This is called *early*, so
3223 * we can't necessarily depend on lots of kernel help here.
3224 * Just do some early initializations, and do the complex setup
3225 * later.
3226 */
3227void __init console_init(void)
3228{
3229 initcall_t *call;
3230
3231 /* Setup the default TTY line discipline. */
3232 tty_ldisc_begin();
3233
3234 /*
3235 * set up the console device so that later boot sequences can
3236 * inform about problems etc..
3237 */
3238 call = __con_initcall_start;
3239 while (call < __con_initcall_end) {
3240 (*call)();
3241 call++;
3242 }
3243}
3244
3245static char *tty_devnode(struct device *dev, mode_t *mode)
3246{
3247 if (!mode)
3248 return NULL;
3249 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3250 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3251 *mode = 0666;
3252 return NULL;
3253}
3254
3255static int __init tty_class_init(void)
3256{
3257 tty_class = class_create(THIS_MODULE, "tty");
3258 if (IS_ERR(tty_class))
3259 return PTR_ERR(tty_class);
3260 tty_class->devnode = tty_devnode;
3261 return 0;
3262}
3263
3264postcore_initcall(tty_class_init);
3265
3266/* 3/2004 jmc: why do these devices exist? */
3267static struct cdev tty_cdev, console_cdev;
3268
3269static ssize_t show_cons_active(struct device *dev,
3270 struct device_attribute *attr, char *buf)
3271{
3272 struct console *cs[16];
3273 int i = 0;
3274 struct console *c;
3275 ssize_t count = 0;
3276
3277 console_lock();
3278 for_each_console(c) {
3279 if (!c->device)
3280 continue;
3281 if (!c->write)
3282 continue;
3283 if ((c->flags & CON_ENABLED) == 0)
3284 continue;
3285 cs[i++] = c;
3286 if (i >= ARRAY_SIZE(cs))
3287 break;
3288 }
3289 while (i--)
3290 count += sprintf(buf + count, "%s%d%c",
3291 cs[i]->name, cs[i]->index, i ? ' ':'\n');
3292 console_unlock();
3293
3294 return count;
3295}
3296static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3297
3298static struct device *consdev;
3299
3300void console_sysfs_notify(void)
3301{
3302 if (consdev)
3303 sysfs_notify(&consdev->kobj, NULL, "active");
3304}
3305
3306/*
3307 * Ok, now we can initialize the rest of the tty devices and can count
3308 * on memory allocations, interrupts etc..
3309 */
3310int __init tty_init(void)
3311{
3312 cdev_init(&tty_cdev, &tty_fops);
3313 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3314 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3315 panic("Couldn't register /dev/tty driver\n");
3316 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3317
3318 cdev_init(&console_cdev, &console_fops);
3319 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3320 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3321 panic("Couldn't register /dev/console driver\n");
3322 consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3323 "console");
3324 if (IS_ERR(consdev))
3325 consdev = NULL;
3326 else
3327 WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
3328
3329#ifdef CONFIG_VT
3330 vty_init(&console_fops);
3331#endif
3332 return 0;
3333}
3334
diff --git a/drivers/tty/tty_ioctl.c b/drivers/tty/tty_ioctl.c
new file mode 100644
index 000000000000..53f2442c6099
--- /dev/null
+++ b/drivers/tty/tty_ioctl.c
@@ -0,0 +1,1181 @@
1/*
2 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
3 *
4 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
5 * which can be dynamically activated and de-activated by the line
6 * discipline handling modules (like SLIP).
7 */
8
9#include <linux/types.h>
10#include <linux/termios.h>
11#include <linux/errno.h>
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/major.h>
15#include <linux/tty.h>
16#include <linux/fcntl.h>
17#include <linux/string.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/bitops.h>
21#include <linux/mutex.h>
22
23#include <asm/io.h>
24#include <asm/uaccess.h>
25#include <asm/system.h>
26
27#undef TTY_DEBUG_WAIT_UNTIL_SENT
28
29#undef DEBUG
30
31/*
32 * Internal flag options for termios setting behavior
33 */
34#define TERMIOS_FLUSH 1
35#define TERMIOS_WAIT 2
36#define TERMIOS_TERMIO 4
37#define TERMIOS_OLD 8
38
39
40/**
41 * tty_chars_in_buffer - characters pending
42 * @tty: terminal
43 *
44 * Return the number of bytes of data in the device private
45 * output queue. If no private method is supplied there is assumed
46 * to be no queue on the device.
47 */
48
49int tty_chars_in_buffer(struct tty_struct *tty)
50{
51 if (tty->ops->chars_in_buffer)
52 return tty->ops->chars_in_buffer(tty);
53 else
54 return 0;
55}
56EXPORT_SYMBOL(tty_chars_in_buffer);
57
58/**
59 * tty_write_room - write queue space
60 * @tty: terminal
61 *
62 * Return the number of bytes that can be queued to this device
63 * at the present time. The result should be treated as a guarantee
64 * and the driver cannot offer a value it later shrinks by more than
65 * the number of bytes written. If no method is provided 2K is always
66 * returned and data may be lost as there will be no flow control.
67 */
68
69int tty_write_room(struct tty_struct *tty)
70{
71 if (tty->ops->write_room)
72 return tty->ops->write_room(tty);
73 return 2048;
74}
75EXPORT_SYMBOL(tty_write_room);
76
77/**
78 * tty_driver_flush_buffer - discard internal buffer
79 * @tty: terminal
80 *
81 * Discard the internal output buffer for this device. If no method
82 * is provided then either the buffer cannot be hardware flushed or
83 * there is no buffer driver side.
84 */
85void tty_driver_flush_buffer(struct tty_struct *tty)
86{
87 if (tty->ops->flush_buffer)
88 tty->ops->flush_buffer(tty);
89}
90EXPORT_SYMBOL(tty_driver_flush_buffer);
91
92/**
93 * tty_throttle - flow control
94 * @tty: terminal
95 *
96 * Indicate that a tty should stop transmitting data down the stack.
97 * Takes the termios mutex to protect against parallel throttle/unthrottle
98 * and also to ensure the driver can consistently reference its own
99 * termios data at this point when implementing software flow control.
100 */
101
102void tty_throttle(struct tty_struct *tty)
103{
104 mutex_lock(&tty->termios_mutex);
105 /* check TTY_THROTTLED first so it indicates our state */
106 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
107 tty->ops->throttle)
108 tty->ops->throttle(tty);
109 mutex_unlock(&tty->termios_mutex);
110}
111EXPORT_SYMBOL(tty_throttle);
112
113/**
114 * tty_unthrottle - flow control
115 * @tty: terminal
116 *
117 * Indicate that a tty may continue transmitting data down the stack.
118 * Takes the termios mutex to protect against parallel throttle/unthrottle
119 * and also to ensure the driver can consistently reference its own
120 * termios data at this point when implementing software flow control.
121 *
122 * Drivers should however remember that the stack can issue a throttle,
123 * then change flow control method, then unthrottle.
124 */
125
126void tty_unthrottle(struct tty_struct *tty)
127{
128 mutex_lock(&tty->termios_mutex);
129 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
130 tty->ops->unthrottle)
131 tty->ops->unthrottle(tty);
132 mutex_unlock(&tty->termios_mutex);
133}
134EXPORT_SYMBOL(tty_unthrottle);
135
136/**
137 * tty_wait_until_sent - wait for I/O to finish
138 * @tty: tty we are waiting for
139 * @timeout: how long we will wait
140 *
141 * Wait for characters pending in a tty driver to hit the wire, or
142 * for a timeout to occur (eg due to flow control)
143 *
144 * Locking: none
145 */
146
147void tty_wait_until_sent(struct tty_struct *tty, long timeout)
148{
149#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
150 char buf[64];
151
152 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
153#endif
154 if (!timeout)
155 timeout = MAX_SCHEDULE_TIMEOUT;
156 if (wait_event_interruptible_timeout(tty->write_wait,
157 !tty_chars_in_buffer(tty), timeout) >= 0) {
158 if (tty->ops->wait_until_sent)
159 tty->ops->wait_until_sent(tty, timeout);
160 }
161}
162EXPORT_SYMBOL(tty_wait_until_sent);
163
164
165/*
166 * Termios Helper Methods
167 */
168
169static void unset_locked_termios(struct ktermios *termios,
170 struct ktermios *old,
171 struct ktermios *locked)
172{
173 int i;
174
175#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
176
177 if (!locked) {
178 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
179 return;
180 }
181
182 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
183 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
184 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
185 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
186 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
187 for (i = 0; i < NCCS; i++)
188 termios->c_cc[i] = locked->c_cc[i] ?
189 old->c_cc[i] : termios->c_cc[i];
190 /* FIXME: What should we do for i/ospeed */
191}
192
193/*
194 * Routine which returns the baud rate of the tty
195 *
196 * Note that the baud_table needs to be kept in sync with the
197 * include/asm/termbits.h file.
198 */
199static const speed_t baud_table[] = {
200 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
201 9600, 19200, 38400, 57600, 115200, 230400, 460800,
202#ifdef __sparc__
203 76800, 153600, 307200, 614400, 921600
204#else
205 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
206 2500000, 3000000, 3500000, 4000000
207#endif
208};
209
210#ifndef __sparc__
211static const tcflag_t baud_bits[] = {
212 B0, B50, B75, B110, B134, B150, B200, B300, B600,
213 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
214 B57600, B115200, B230400, B460800, B500000, B576000,
215 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
216 B3000000, B3500000, B4000000
217};
218#else
219static const tcflag_t baud_bits[] = {
220 B0, B50, B75, B110, B134, B150, B200, B300, B600,
221 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
222 B57600, B115200, B230400, B460800, B76800, B153600,
223 B307200, B614400, B921600
224};
225#endif
226
227static int n_baud_table = ARRAY_SIZE(baud_table);
228
229/**
230 * tty_termios_baud_rate
231 * @termios: termios structure
232 *
233 * Convert termios baud rate data into a speed. This should be called
234 * with the termios lock held if this termios is a terminal termios
235 * structure. May change the termios data. Device drivers can call this
236 * function but should use ->c_[io]speed directly as they are updated.
237 *
238 * Locking: none
239 */
240
241speed_t tty_termios_baud_rate(struct ktermios *termios)
242{
243 unsigned int cbaud;
244
245 cbaud = termios->c_cflag & CBAUD;
246
247#ifdef BOTHER
248 /* Magic token for arbitrary speed via c_ispeed/c_ospeed */
249 if (cbaud == BOTHER)
250 return termios->c_ospeed;
251#endif
252 if (cbaud & CBAUDEX) {
253 cbaud &= ~CBAUDEX;
254
255 if (cbaud < 1 || cbaud + 15 > n_baud_table)
256 termios->c_cflag &= ~CBAUDEX;
257 else
258 cbaud += 15;
259 }
260 return baud_table[cbaud];
261}
262EXPORT_SYMBOL(tty_termios_baud_rate);
263
264/**
265 * tty_termios_input_baud_rate
266 * @termios: termios structure
267 *
268 * Convert termios baud rate data into a speed. This should be called
269 * with the termios lock held if this termios is a terminal termios
270 * structure. May change the termios data. Device drivers can call this
271 * function but should use ->c_[io]speed directly as they are updated.
272 *
273 * Locking: none
274 */
275
276speed_t tty_termios_input_baud_rate(struct ktermios *termios)
277{
278#ifdef IBSHIFT
279 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
280
281 if (cbaud == B0)
282 return tty_termios_baud_rate(termios);
283
284 /* Magic token for arbitrary speed via c_ispeed*/
285 if (cbaud == BOTHER)
286 return termios->c_ispeed;
287
288 if (cbaud & CBAUDEX) {
289 cbaud &= ~CBAUDEX;
290
291 if (cbaud < 1 || cbaud + 15 > n_baud_table)
292 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
293 else
294 cbaud += 15;
295 }
296 return baud_table[cbaud];
297#else
298 return tty_termios_baud_rate(termios);
299#endif
300}
301EXPORT_SYMBOL(tty_termios_input_baud_rate);
302
303/**
304 * tty_termios_encode_baud_rate
305 * @termios: ktermios structure holding user requested state
306 * @ispeed: input speed
307 * @ospeed: output speed
308 *
309 * Encode the speeds set into the passed termios structure. This is
310 * used as a library helper for drivers so that they can report back
311 * the actual speed selected when it differs from the speed requested
312 *
313 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
314 * we need to carefully set the bits when the user does not get the
315 * desired speed. We allow small margins and preserve as much of possible
316 * of the input intent to keep compatibility.
317 *
318 * Locking: Caller should hold termios lock. This is already held
319 * when calling this function from the driver termios handler.
320 *
321 * The ifdefs deal with platforms whose owners have yet to update them
322 * and will all go away once this is done.
323 */
324
325void tty_termios_encode_baud_rate(struct ktermios *termios,
326 speed_t ibaud, speed_t obaud)
327{
328 int i = 0;
329 int ifound = -1, ofound = -1;
330 int iclose = ibaud/50, oclose = obaud/50;
331 int ibinput = 0;
332
333 if (obaud == 0) /* CD dropped */
334 ibaud = 0; /* Clear ibaud to be sure */
335
336 termios->c_ispeed = ibaud;
337 termios->c_ospeed = obaud;
338
339#ifdef BOTHER
340 /* If the user asked for a precise weird speed give a precise weird
341 answer. If they asked for a Bfoo speed they many have problems
342 digesting non-exact replies so fuzz a bit */
343
344 if ((termios->c_cflag & CBAUD) == BOTHER)
345 oclose = 0;
346 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
347 iclose = 0;
348 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
349 ibinput = 1; /* An input speed was specified */
350#endif
351 termios->c_cflag &= ~CBAUD;
352
353 /*
354 * Our goal is to find a close match to the standard baud rate
355 * returned. Walk the baud rate table and if we get a very close
356 * match then report back the speed as a POSIX Bxxxx value by
357 * preference
358 */
359
360 do {
361 if (obaud - oclose <= baud_table[i] &&
362 obaud + oclose >= baud_table[i]) {
363 termios->c_cflag |= baud_bits[i];
364 ofound = i;
365 }
366 if (ibaud - iclose <= baud_table[i] &&
367 ibaud + iclose >= baud_table[i]) {
368 /* For the case input == output don't set IBAUD bits
369 if the user didn't do so */
370 if (ofound == i && !ibinput)
371 ifound = i;
372#ifdef IBSHIFT
373 else {
374 ifound = i;
375 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
376 }
377#endif
378 }
379 } while (++i < n_baud_table);
380
381 /*
382 * If we found no match then use BOTHER if provided or warn
383 * the user their platform maintainer needs to wake up if not.
384 */
385#ifdef BOTHER
386 if (ofound == -1)
387 termios->c_cflag |= BOTHER;
388 /* Set exact input bits only if the input and output differ or the
389 user already did */
390 if (ifound == -1 && (ibaud != obaud || ibinput))
391 termios->c_cflag |= (BOTHER << IBSHIFT);
392#else
393 if (ifound == -1 || ofound == -1) {
394 printk_once(KERN_WARNING "tty: Unable to return correct "
395 "speed data as your architecture needs updating.\n");
396 }
397#endif
398}
399EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
400
401/**
402 * tty_encode_baud_rate - set baud rate of the tty
403 * @ibaud: input baud rate
404 * @obad: output baud rate
405 *
406 * Update the current termios data for the tty with the new speed
407 * settings. The caller must hold the termios_mutex for the tty in
408 * question.
409 */
410
411void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
412{
413 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
414}
415EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
416
417/**
418 * tty_get_baud_rate - get tty bit rates
419 * @tty: tty to query
420 *
421 * Returns the baud rate as an integer for this terminal. The
422 * termios lock must be held by the caller and the terminal bit
423 * flags may be updated.
424 *
425 * Locking: none
426 */
427
428speed_t tty_get_baud_rate(struct tty_struct *tty)
429{
430 speed_t baud = tty_termios_baud_rate(tty->termios);
431
432 if (baud == 38400 && tty->alt_speed) {
433 if (!tty->warned) {
434 printk(KERN_WARNING "Use of setserial/setrocket to "
435 "set SPD_* flags is deprecated\n");
436 tty->warned = 1;
437 }
438 baud = tty->alt_speed;
439 }
440
441 return baud;
442}
443EXPORT_SYMBOL(tty_get_baud_rate);
444
445/**
446 * tty_termios_copy_hw - copy hardware settings
447 * @new: New termios
448 * @old: Old termios
449 *
450 * Propagate the hardware specific terminal setting bits from
451 * the old termios structure to the new one. This is used in cases
452 * where the hardware does not support reconfiguration or as a helper
453 * in some cases where only minimal reconfiguration is supported
454 */
455
456void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
457{
458 /* The bits a dumb device handles in software. Smart devices need
459 to always provide a set_termios method */
460 new->c_cflag &= HUPCL | CREAD | CLOCAL;
461 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
462 new->c_ispeed = old->c_ispeed;
463 new->c_ospeed = old->c_ospeed;
464}
465EXPORT_SYMBOL(tty_termios_copy_hw);
466
467/**
468 * tty_termios_hw_change - check for setting change
469 * @a: termios
470 * @b: termios to compare
471 *
472 * Check if any of the bits that affect a dumb device have changed
473 * between the two termios structures, or a speed change is needed.
474 */
475
476int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
477{
478 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
479 return 1;
480 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
481 return 1;
482 return 0;
483}
484EXPORT_SYMBOL(tty_termios_hw_change);
485
486/**
487 * tty_set_termios - update termios values
488 * @tty: tty to update
489 * @new_termios: desired new value
490 *
491 * Perform updates to the termios values set on this terminal. There
492 * is a bit of layering violation here with n_tty in terms of the
493 * internal knowledge of this function.
494 *
495 * Locking: termios_mutex
496 */
497
498int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
499{
500 struct ktermios old_termios;
501 struct tty_ldisc *ld;
502 unsigned long flags;
503
504 /*
505 * Perform the actual termios internal changes under lock.
506 */
507
508
509 /* FIXME: we need to decide on some locking/ordering semantics
510 for the set_termios notification eventually */
511 mutex_lock(&tty->termios_mutex);
512 old_termios = *tty->termios;
513 *tty->termios = *new_termios;
514 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
515
516 /* See if packet mode change of state. */
517 if (tty->link && tty->link->packet) {
518 int extproc = (old_termios.c_lflag & EXTPROC) |
519 (tty->termios->c_lflag & EXTPROC);
520 int old_flow = ((old_termios.c_iflag & IXON) &&
521 (old_termios.c_cc[VSTOP] == '\023') &&
522 (old_termios.c_cc[VSTART] == '\021'));
523 int new_flow = (I_IXON(tty) &&
524 STOP_CHAR(tty) == '\023' &&
525 START_CHAR(tty) == '\021');
526 if ((old_flow != new_flow) || extproc) {
527 spin_lock_irqsave(&tty->ctrl_lock, flags);
528 if (old_flow != new_flow) {
529 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
530 if (new_flow)
531 tty->ctrl_status |= TIOCPKT_DOSTOP;
532 else
533 tty->ctrl_status |= TIOCPKT_NOSTOP;
534 }
535 if (extproc)
536 tty->ctrl_status |= TIOCPKT_IOCTL;
537 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
538 wake_up_interruptible(&tty->link->read_wait);
539 }
540 }
541
542 if (tty->ops->set_termios)
543 (*tty->ops->set_termios)(tty, &old_termios);
544 else
545 tty_termios_copy_hw(tty->termios, &old_termios);
546
547 ld = tty_ldisc_ref(tty);
548 if (ld != NULL) {
549 if (ld->ops->set_termios)
550 (ld->ops->set_termios)(tty, &old_termios);
551 tty_ldisc_deref(ld);
552 }
553 mutex_unlock(&tty->termios_mutex);
554 return 0;
555}
556EXPORT_SYMBOL_GPL(tty_set_termios);
557
558/**
559 * set_termios - set termios values for a tty
560 * @tty: terminal device
561 * @arg: user data
562 * @opt: option information
563 *
564 * Helper function to prepare termios data and run necessary other
565 * functions before using tty_set_termios to do the actual changes.
566 *
567 * Locking:
568 * Called functions take ldisc and termios_mutex locks
569 */
570
571static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
572{
573 struct ktermios tmp_termios;
574 struct tty_ldisc *ld;
575 int retval = tty_check_change(tty);
576
577 if (retval)
578 return retval;
579
580 mutex_lock(&tty->termios_mutex);
581 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
582 mutex_unlock(&tty->termios_mutex);
583
584 if (opt & TERMIOS_TERMIO) {
585 if (user_termio_to_kernel_termios(&tmp_termios,
586 (struct termio __user *)arg))
587 return -EFAULT;
588#ifdef TCGETS2
589 } else if (opt & TERMIOS_OLD) {
590 if (user_termios_to_kernel_termios_1(&tmp_termios,
591 (struct termios __user *)arg))
592 return -EFAULT;
593 } else {
594 if (user_termios_to_kernel_termios(&tmp_termios,
595 (struct termios2 __user *)arg))
596 return -EFAULT;
597 }
598#else
599 } else if (user_termios_to_kernel_termios(&tmp_termios,
600 (struct termios __user *)arg))
601 return -EFAULT;
602#endif
603
604 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
605 * with the real speed so its unconditionally usable */
606 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
607 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
608
609 ld = tty_ldisc_ref(tty);
610
611 if (ld != NULL) {
612 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
613 ld->ops->flush_buffer(tty);
614 tty_ldisc_deref(ld);
615 }
616
617 if (opt & TERMIOS_WAIT) {
618 tty_wait_until_sent(tty, 0);
619 if (signal_pending(current))
620 return -EINTR;
621 }
622
623 tty_set_termios(tty, &tmp_termios);
624
625 /* FIXME: Arguably if tmp_termios == tty->termios AND the
626 actual requested termios was not tmp_termios then we may
627 want to return an error as no user requested change has
628 succeeded */
629 return 0;
630}
631
632static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
633{
634 mutex_lock(&tty->termios_mutex);
635 memcpy(kterm, tty->termios, sizeof(struct ktermios));
636 mutex_unlock(&tty->termios_mutex);
637}
638
639static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
640{
641 mutex_lock(&tty->termios_mutex);
642 memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
643 mutex_unlock(&tty->termios_mutex);
644}
645
646static int get_termio(struct tty_struct *tty, struct termio __user *termio)
647{
648 struct ktermios kterm;
649 copy_termios(tty, &kterm);
650 if (kernel_termios_to_user_termio(termio, &kterm))
651 return -EFAULT;
652 return 0;
653}
654
655
656#ifdef TCGETX
657
658/**
659 * set_termiox - set termiox fields if possible
660 * @tty: terminal
661 * @arg: termiox structure from user
662 * @opt: option flags for ioctl type
663 *
664 * Implement the device calling points for the SYS5 termiox ioctl
665 * interface in Linux
666 */
667
668static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
669{
670 struct termiox tnew;
671 struct tty_ldisc *ld;
672
673 if (tty->termiox == NULL)
674 return -EINVAL;
675 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
676 return -EFAULT;
677
678 ld = tty_ldisc_ref(tty);
679 if (ld != NULL) {
680 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
681 ld->ops->flush_buffer(tty);
682 tty_ldisc_deref(ld);
683 }
684 if (opt & TERMIOS_WAIT) {
685 tty_wait_until_sent(tty, 0);
686 if (signal_pending(current))
687 return -EINTR;
688 }
689
690 mutex_lock(&tty->termios_mutex);
691 if (tty->ops->set_termiox)
692 tty->ops->set_termiox(tty, &tnew);
693 mutex_unlock(&tty->termios_mutex);
694 return 0;
695}
696
697#endif
698
699
700#ifdef TIOCGETP
701/*
702 * These are deprecated, but there is limited support..
703 *
704 * The "sg_flags" translation is a joke..
705 */
706static int get_sgflags(struct tty_struct *tty)
707{
708 int flags = 0;
709
710 if (!(tty->termios->c_lflag & ICANON)) {
711 if (tty->termios->c_lflag & ISIG)
712 flags |= 0x02; /* cbreak */
713 else
714 flags |= 0x20; /* raw */
715 }
716 if (tty->termios->c_lflag & ECHO)
717 flags |= 0x08; /* echo */
718 if (tty->termios->c_oflag & OPOST)
719 if (tty->termios->c_oflag & ONLCR)
720 flags |= 0x10; /* crmod */
721 return flags;
722}
723
724static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
725{
726 struct sgttyb tmp;
727
728 mutex_lock(&tty->termios_mutex);
729 tmp.sg_ispeed = tty->termios->c_ispeed;
730 tmp.sg_ospeed = tty->termios->c_ospeed;
731 tmp.sg_erase = tty->termios->c_cc[VERASE];
732 tmp.sg_kill = tty->termios->c_cc[VKILL];
733 tmp.sg_flags = get_sgflags(tty);
734 mutex_unlock(&tty->termios_mutex);
735
736 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
737}
738
739static void set_sgflags(struct ktermios *termios, int flags)
740{
741 termios->c_iflag = ICRNL | IXON;
742 termios->c_oflag = 0;
743 termios->c_lflag = ISIG | ICANON;
744 if (flags & 0x02) { /* cbreak */
745 termios->c_iflag = 0;
746 termios->c_lflag &= ~ICANON;
747 }
748 if (flags & 0x08) { /* echo */
749 termios->c_lflag |= ECHO | ECHOE | ECHOK |
750 ECHOCTL | ECHOKE | IEXTEN;
751 }
752 if (flags & 0x10) { /* crmod */
753 termios->c_oflag |= OPOST | ONLCR;
754 }
755 if (flags & 0x20) { /* raw */
756 termios->c_iflag = 0;
757 termios->c_lflag &= ~(ISIG | ICANON);
758 }
759 if (!(termios->c_lflag & ICANON)) {
760 termios->c_cc[VMIN] = 1;
761 termios->c_cc[VTIME] = 0;
762 }
763}
764
765/**
766 * set_sgttyb - set legacy terminal values
767 * @tty: tty structure
768 * @sgttyb: pointer to old style terminal structure
769 *
770 * Updates a terminal from the legacy BSD style terminal information
771 * structure.
772 *
773 * Locking: termios_mutex
774 */
775
776static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
777{
778 int retval;
779 struct sgttyb tmp;
780 struct ktermios termios;
781
782 retval = tty_check_change(tty);
783 if (retval)
784 return retval;
785
786 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
787 return -EFAULT;
788
789 mutex_lock(&tty->termios_mutex);
790 termios = *tty->termios;
791 termios.c_cc[VERASE] = tmp.sg_erase;
792 termios.c_cc[VKILL] = tmp.sg_kill;
793 set_sgflags(&termios, tmp.sg_flags);
794 /* Try and encode into Bfoo format */
795#ifdef BOTHER
796 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
797 termios.c_ospeed);
798#endif
799 mutex_unlock(&tty->termios_mutex);
800 tty_set_termios(tty, &termios);
801 return 0;
802}
803#endif
804
805#ifdef TIOCGETC
806static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
807{
808 struct tchars tmp;
809
810 mutex_lock(&tty->termios_mutex);
811 tmp.t_intrc = tty->termios->c_cc[VINTR];
812 tmp.t_quitc = tty->termios->c_cc[VQUIT];
813 tmp.t_startc = tty->termios->c_cc[VSTART];
814 tmp.t_stopc = tty->termios->c_cc[VSTOP];
815 tmp.t_eofc = tty->termios->c_cc[VEOF];
816 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
817 mutex_unlock(&tty->termios_mutex);
818 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
819}
820
821static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
822{
823 struct tchars tmp;
824
825 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
826 return -EFAULT;
827 mutex_lock(&tty->termios_mutex);
828 tty->termios->c_cc[VINTR] = tmp.t_intrc;
829 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
830 tty->termios->c_cc[VSTART] = tmp.t_startc;
831 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
832 tty->termios->c_cc[VEOF] = tmp.t_eofc;
833 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
834 mutex_unlock(&tty->termios_mutex);
835 return 0;
836}
837#endif
838
839#ifdef TIOCGLTC
840static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
841{
842 struct ltchars tmp;
843
844 mutex_lock(&tty->termios_mutex);
845 tmp.t_suspc = tty->termios->c_cc[VSUSP];
846 /* what is dsuspc anyway? */
847 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
848 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
849 /* what is flushc anyway? */
850 tmp.t_flushc = tty->termios->c_cc[VEOL2];
851 tmp.t_werasc = tty->termios->c_cc[VWERASE];
852 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
853 mutex_unlock(&tty->termios_mutex);
854 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
855}
856
857static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
858{
859 struct ltchars tmp;
860
861 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
862 return -EFAULT;
863
864 mutex_lock(&tty->termios_mutex);
865 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
866 /* what is dsuspc anyway? */
867 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
868 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
869 /* what is flushc anyway? */
870 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
871 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
872 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
873 mutex_unlock(&tty->termios_mutex);
874 return 0;
875}
876#endif
877
878/**
879 * send_prio_char - send priority character
880 *
881 * Send a high priority character to the tty even if stopped
882 *
883 * Locking: none for xchar method, write ordering for write method.
884 */
885
886static int send_prio_char(struct tty_struct *tty, char ch)
887{
888 int was_stopped = tty->stopped;
889
890 if (tty->ops->send_xchar) {
891 tty->ops->send_xchar(tty, ch);
892 return 0;
893 }
894
895 if (tty_write_lock(tty, 0) < 0)
896 return -ERESTARTSYS;
897
898 if (was_stopped)
899 start_tty(tty);
900 tty->ops->write(tty, &ch, 1);
901 if (was_stopped)
902 stop_tty(tty);
903 tty_write_unlock(tty);
904 return 0;
905}
906
907/**
908 * tty_change_softcar - carrier change ioctl helper
909 * @tty: tty to update
910 * @arg: enable/disable CLOCAL
911 *
912 * Perform a change to the CLOCAL state and call into the driver
913 * layer to make it visible. All done with the termios mutex
914 */
915
916static int tty_change_softcar(struct tty_struct *tty, int arg)
917{
918 int ret = 0;
919 int bit = arg ? CLOCAL : 0;
920 struct ktermios old;
921
922 mutex_lock(&tty->termios_mutex);
923 old = *tty->termios;
924 tty->termios->c_cflag &= ~CLOCAL;
925 tty->termios->c_cflag |= bit;
926 if (tty->ops->set_termios)
927 tty->ops->set_termios(tty, &old);
928 if ((tty->termios->c_cflag & CLOCAL) != bit)
929 ret = -EINVAL;
930 mutex_unlock(&tty->termios_mutex);
931 return ret;
932}
933
934/**
935 * tty_mode_ioctl - mode related ioctls
936 * @tty: tty for the ioctl
937 * @file: file pointer for the tty
938 * @cmd: command
939 * @arg: ioctl argument
940 *
941 * Perform non line discipline specific mode control ioctls. This
942 * is designed to be called by line disciplines to ensure they provide
943 * consistent mode setting.
944 */
945
946int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
947 unsigned int cmd, unsigned long arg)
948{
949 struct tty_struct *real_tty;
950 void __user *p = (void __user *)arg;
951 int ret = 0;
952 struct ktermios kterm;
953
954 BUG_ON(file == NULL);
955
956 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
957 tty->driver->subtype == PTY_TYPE_MASTER)
958 real_tty = tty->link;
959 else
960 real_tty = tty;
961
962 switch (cmd) {
963#ifdef TIOCGETP
964 case TIOCGETP:
965 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
966 case TIOCSETP:
967 case TIOCSETN:
968 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
969#endif
970#ifdef TIOCGETC
971 case TIOCGETC:
972 return get_tchars(real_tty, p);
973 case TIOCSETC:
974 return set_tchars(real_tty, p);
975#endif
976#ifdef TIOCGLTC
977 case TIOCGLTC:
978 return get_ltchars(real_tty, p);
979 case TIOCSLTC:
980 return set_ltchars(real_tty, p);
981#endif
982 case TCSETSF:
983 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
984 case TCSETSW:
985 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
986 case TCSETS:
987 return set_termios(real_tty, p, TERMIOS_OLD);
988#ifndef TCGETS2
989 case TCGETS:
990 copy_termios(real_tty, &kterm);
991 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
992 ret = -EFAULT;
993 return ret;
994#else
995 case TCGETS:
996 copy_termios(real_tty, &kterm);
997 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
998 ret = -EFAULT;
999 return ret;
1000 case TCGETS2:
1001 copy_termios(real_tty, &kterm);
1002 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
1003 ret = -EFAULT;
1004 return ret;
1005 case TCSETSF2:
1006 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1007 case TCSETSW2:
1008 return set_termios(real_tty, p, TERMIOS_WAIT);
1009 case TCSETS2:
1010 return set_termios(real_tty, p, 0);
1011#endif
1012 case TCGETA:
1013 return get_termio(real_tty, p);
1014 case TCSETAF:
1015 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1016 case TCSETAW:
1017 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1018 case TCSETA:
1019 return set_termios(real_tty, p, TERMIOS_TERMIO);
1020#ifndef TCGETS2
1021 case TIOCGLCKTRMIOS:
1022 copy_termios_locked(real_tty, &kterm);
1023 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
1024 ret = -EFAULT;
1025 return ret;
1026 case TIOCSLCKTRMIOS:
1027 if (!capable(CAP_SYS_ADMIN))
1028 return -EPERM;
1029 copy_termios_locked(real_tty, &kterm);
1030 if (user_termios_to_kernel_termios(&kterm,
1031 (struct termios __user *) arg))
1032 return -EFAULT;
1033 mutex_lock(&real_tty->termios_mutex);
1034 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1035 mutex_unlock(&real_tty->termios_mutex);
1036 return 0;
1037#else
1038 case TIOCGLCKTRMIOS:
1039 copy_termios_locked(real_tty, &kterm);
1040 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
1041 ret = -EFAULT;
1042 return ret;
1043 case TIOCSLCKTRMIOS:
1044 if (!capable(CAP_SYS_ADMIN))
1045 return -EPERM;
1046 copy_termios_locked(real_tty, &kterm);
1047 if (user_termios_to_kernel_termios_1(&kterm,
1048 (struct termios __user *) arg))
1049 return -EFAULT;
1050 mutex_lock(&real_tty->termios_mutex);
1051 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1052 mutex_unlock(&real_tty->termios_mutex);
1053 return ret;
1054#endif
1055#ifdef TCGETX
1056 case TCGETX: {
1057 struct termiox ktermx;
1058 if (real_tty->termiox == NULL)
1059 return -EINVAL;
1060 mutex_lock(&real_tty->termios_mutex);
1061 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
1062 mutex_unlock(&real_tty->termios_mutex);
1063 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1064 ret = -EFAULT;
1065 return ret;
1066 }
1067 case TCSETX:
1068 return set_termiox(real_tty, p, 0);
1069 case TCSETXW:
1070 return set_termiox(real_tty, p, TERMIOS_WAIT);
1071 case TCSETXF:
1072 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1073#endif
1074 case TIOCGSOFTCAR:
1075 copy_termios(real_tty, &kterm);
1076 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
1077 (int __user *)arg);
1078 return ret;
1079 case TIOCSSOFTCAR:
1080 if (get_user(arg, (unsigned int __user *) arg))
1081 return -EFAULT;
1082 return tty_change_softcar(real_tty, arg);
1083 default:
1084 return -ENOIOCTLCMD;
1085 }
1086}
1087EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1088
1089int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1090{
1091 struct tty_ldisc *ld;
1092 int retval = tty_check_change(tty);
1093 if (retval)
1094 return retval;
1095
1096 ld = tty_ldisc_ref_wait(tty);
1097 switch (arg) {
1098 case TCIFLUSH:
1099 if (ld && ld->ops->flush_buffer)
1100 ld->ops->flush_buffer(tty);
1101 break;
1102 case TCIOFLUSH:
1103 if (ld && ld->ops->flush_buffer)
1104 ld->ops->flush_buffer(tty);
1105 /* fall through */
1106 case TCOFLUSH:
1107 tty_driver_flush_buffer(tty);
1108 break;
1109 default:
1110 tty_ldisc_deref(ld);
1111 return -EINVAL;
1112 }
1113 tty_ldisc_deref(ld);
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(tty_perform_flush);
1117
1118int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
1119 unsigned int cmd, unsigned long arg)
1120{
1121 unsigned long flags;
1122 int retval;
1123
1124 switch (cmd) {
1125 case TCXONC:
1126 retval = tty_check_change(tty);
1127 if (retval)
1128 return retval;
1129 switch (arg) {
1130 case TCOOFF:
1131 if (!tty->flow_stopped) {
1132 tty->flow_stopped = 1;
1133 stop_tty(tty);
1134 }
1135 break;
1136 case TCOON:
1137 if (tty->flow_stopped) {
1138 tty->flow_stopped = 0;
1139 start_tty(tty);
1140 }
1141 break;
1142 case TCIOFF:
1143 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1144 return send_prio_char(tty, STOP_CHAR(tty));
1145 break;
1146 case TCION:
1147 if (START_CHAR(tty) != __DISABLED_CHAR)
1148 return send_prio_char(tty, START_CHAR(tty));
1149 break;
1150 default:
1151 return -EINVAL;
1152 }
1153 return 0;
1154 case TCFLSH:
1155 return tty_perform_flush(tty, arg);
1156 case TIOCPKT:
1157 {
1158 int pktmode;
1159
1160 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1161 tty->driver->subtype != PTY_TYPE_MASTER)
1162 return -ENOTTY;
1163 if (get_user(pktmode, (int __user *) arg))
1164 return -EFAULT;
1165 spin_lock_irqsave(&tty->ctrl_lock, flags);
1166 if (pktmode) {
1167 if (!tty->packet) {
1168 tty->packet = 1;
1169 tty->link->ctrl_status = 0;
1170 }
1171 } else
1172 tty->packet = 0;
1173 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1174 return 0;
1175 }
1176 default:
1177 /* Try the mode commands */
1178 return tty_mode_ioctl(tty, file, cmd, arg);
1179 }
1180}
1181EXPORT_SYMBOL(n_tty_ioctl_helper);
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
new file mode 100644
index 000000000000..ef925d581713
--- /dev/null
+++ b/drivers/tty/tty_ldisc.c
@@ -0,0 +1,978 @@
1#include <linux/types.h>
2#include <linux/major.h>
3#include <linux/errno.h>
4#include <linux/signal.h>
5#include <linux/fcntl.h>
6#include <linux/sched.h>
7#include <linux/interrupt.h>
8#include <linux/tty.h>
9#include <linux/tty_driver.h>
10#include <linux/tty_flip.h>
11#include <linux/devpts_fs.h>
12#include <linux/file.h>
13#include <linux/console.h>
14#include <linux/timer.h>
15#include <linux/ctype.h>
16#include <linux/kd.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/slab.h>
20#include <linux/poll.h>
21#include <linux/proc_fs.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/wait.h>
26#include <linux/bitops.h>
27#include <linux/delay.h>
28#include <linux/seq_file.h>
29
30#include <linux/uaccess.h>
31#include <asm/system.h>
32
33#include <linux/kbd_kern.h>
34#include <linux/vt_kern.h>
35#include <linux/selection.h>
36
37#include <linux/kmod.h>
38#include <linux/nsproxy.h>
39
40/*
41 * This guards the refcounted line discipline lists. The lock
42 * must be taken with irqs off because there are hangup path
43 * callers who will do ldisc lookups and cannot sleep.
44 */
45
46static DEFINE_SPINLOCK(tty_ldisc_lock);
47static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
48static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
49/* Line disc dispatch table */
50static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
51
52static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
53{
54 if (ld)
55 atomic_inc(&ld->users);
56 return ld;
57}
58
59static void put_ldisc(struct tty_ldisc *ld)
60{
61 unsigned long flags;
62
63 if (WARN_ON_ONCE(!ld))
64 return;
65
66 /*
67 * If this is the last user, free the ldisc, and
68 * release the ldisc ops.
69 *
70 * We really want an "atomic_dec_and_lock_irqsave()",
71 * but we don't have it, so this does it by hand.
72 */
73 local_irq_save(flags);
74 if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
75 struct tty_ldisc_ops *ldo = ld->ops;
76
77 ldo->refcount--;
78 module_put(ldo->owner);
79 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
80
81 kfree(ld);
82 return;
83 }
84 local_irq_restore(flags);
85 wake_up(&tty_ldisc_idle);
86}
87
88/**
89 * tty_register_ldisc - install a line discipline
90 * @disc: ldisc number
91 * @new_ldisc: pointer to the ldisc object
92 *
93 * Installs a new line discipline into the kernel. The discipline
94 * is set up as unreferenced and then made available to the kernel
95 * from this point onwards.
96 *
97 * Locking:
98 * takes tty_ldisc_lock to guard against ldisc races
99 */
100
101int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
102{
103 unsigned long flags;
104 int ret = 0;
105
106 if (disc < N_TTY || disc >= NR_LDISCS)
107 return -EINVAL;
108
109 spin_lock_irqsave(&tty_ldisc_lock, flags);
110 tty_ldiscs[disc] = new_ldisc;
111 new_ldisc->num = disc;
112 new_ldisc->refcount = 0;
113 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
114
115 return ret;
116}
117EXPORT_SYMBOL(tty_register_ldisc);
118
119/**
120 * tty_unregister_ldisc - unload a line discipline
121 * @disc: ldisc number
122 * @new_ldisc: pointer to the ldisc object
123 *
124 * Remove a line discipline from the kernel providing it is not
125 * currently in use.
126 *
127 * Locking:
128 * takes tty_ldisc_lock to guard against ldisc races
129 */
130
131int tty_unregister_ldisc(int disc)
132{
133 unsigned long flags;
134 int ret = 0;
135
136 if (disc < N_TTY || disc >= NR_LDISCS)
137 return -EINVAL;
138
139 spin_lock_irqsave(&tty_ldisc_lock, flags);
140 if (tty_ldiscs[disc]->refcount)
141 ret = -EBUSY;
142 else
143 tty_ldiscs[disc] = NULL;
144 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
145
146 return ret;
147}
148EXPORT_SYMBOL(tty_unregister_ldisc);
149
150static struct tty_ldisc_ops *get_ldops(int disc)
151{
152 unsigned long flags;
153 struct tty_ldisc_ops *ldops, *ret;
154
155 spin_lock_irqsave(&tty_ldisc_lock, flags);
156 ret = ERR_PTR(-EINVAL);
157 ldops = tty_ldiscs[disc];
158 if (ldops) {
159 ret = ERR_PTR(-EAGAIN);
160 if (try_module_get(ldops->owner)) {
161 ldops->refcount++;
162 ret = ldops;
163 }
164 }
165 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
166 return ret;
167}
168
169static void put_ldops(struct tty_ldisc_ops *ldops)
170{
171 unsigned long flags;
172
173 spin_lock_irqsave(&tty_ldisc_lock, flags);
174 ldops->refcount--;
175 module_put(ldops->owner);
176 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
177}
178
179/**
180 * tty_ldisc_get - take a reference to an ldisc
181 * @disc: ldisc number
182 *
183 * Takes a reference to a line discipline. Deals with refcounts and
184 * module locking counts. Returns NULL if the discipline is not available.
185 * Returns a pointer to the discipline and bumps the ref count if it is
186 * available
187 *
188 * Locking:
189 * takes tty_ldisc_lock to guard against ldisc races
190 */
191
192static struct tty_ldisc *tty_ldisc_get(int disc)
193{
194 struct tty_ldisc *ld;
195 struct tty_ldisc_ops *ldops;
196
197 if (disc < N_TTY || disc >= NR_LDISCS)
198 return ERR_PTR(-EINVAL);
199
200 /*
201 * Get the ldisc ops - we may need to request them to be loaded
202 * dynamically and try again.
203 */
204 ldops = get_ldops(disc);
205 if (IS_ERR(ldops)) {
206 request_module("tty-ldisc-%d", disc);
207 ldops = get_ldops(disc);
208 if (IS_ERR(ldops))
209 return ERR_CAST(ldops);
210 }
211
212 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
213 if (ld == NULL) {
214 put_ldops(ldops);
215 return ERR_PTR(-ENOMEM);
216 }
217
218 ld->ops = ldops;
219 atomic_set(&ld->users, 1);
220 return ld;
221}
222
223static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
224{
225 return (*pos < NR_LDISCS) ? pos : NULL;
226}
227
228static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
229{
230 (*pos)++;
231 return (*pos < NR_LDISCS) ? pos : NULL;
232}
233
234static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
235{
236}
237
238static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
239{
240 int i = *(loff_t *)v;
241 struct tty_ldisc_ops *ldops;
242
243 ldops = get_ldops(i);
244 if (IS_ERR(ldops))
245 return 0;
246 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
247 put_ldops(ldops);
248 return 0;
249}
250
251static const struct seq_operations tty_ldiscs_seq_ops = {
252 .start = tty_ldiscs_seq_start,
253 .next = tty_ldiscs_seq_next,
254 .stop = tty_ldiscs_seq_stop,
255 .show = tty_ldiscs_seq_show,
256};
257
258static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
259{
260 return seq_open(file, &tty_ldiscs_seq_ops);
261}
262
263const struct file_operations tty_ldiscs_proc_fops = {
264 .owner = THIS_MODULE,
265 .open = proc_tty_ldiscs_open,
266 .read = seq_read,
267 .llseek = seq_lseek,
268 .release = seq_release,
269};
270
271/**
272 * tty_ldisc_assign - set ldisc on a tty
273 * @tty: tty to assign
274 * @ld: line discipline
275 *
276 * Install an instance of a line discipline into a tty structure. The
277 * ldisc must have a reference count above zero to ensure it remains.
278 * The tty instance refcount starts at zero.
279 *
280 * Locking:
281 * Caller must hold references
282 */
283
284static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
285{
286 tty->ldisc = ld;
287}
288
289/**
290 * tty_ldisc_try - internal helper
291 * @tty: the tty
292 *
293 * Make a single attempt to grab and bump the refcount on
294 * the tty ldisc. Return 0 on failure or 1 on success. This is
295 * used to implement both the waiting and non waiting versions
296 * of tty_ldisc_ref
297 *
298 * Locking: takes tty_ldisc_lock
299 */
300
301static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
302{
303 unsigned long flags;
304 struct tty_ldisc *ld;
305
306 spin_lock_irqsave(&tty_ldisc_lock, flags);
307 ld = NULL;
308 if (test_bit(TTY_LDISC, &tty->flags))
309 ld = get_ldisc(tty->ldisc);
310 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
311 return ld;
312}
313
314/**
315 * tty_ldisc_ref_wait - wait for the tty ldisc
316 * @tty: tty device
317 *
318 * Dereference the line discipline for the terminal and take a
319 * reference to it. If the line discipline is in flux then
320 * wait patiently until it changes.
321 *
322 * Note: Must not be called from an IRQ/timer context. The caller
323 * must also be careful not to hold other locks that will deadlock
324 * against a discipline change, such as an existing ldisc reference
325 * (which we check for)
326 *
327 * Locking: call functions take tty_ldisc_lock
328 */
329
330struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
331{
332 struct tty_ldisc *ld;
333
334 /* wait_event is a macro */
335 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
336 return ld;
337}
338EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
339
340/**
341 * tty_ldisc_ref - get the tty ldisc
342 * @tty: tty device
343 *
344 * Dereference the line discipline for the terminal and take a
345 * reference to it. If the line discipline is in flux then
346 * return NULL. Can be called from IRQ and timer functions.
347 *
348 * Locking: called functions take tty_ldisc_lock
349 */
350
351struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
352{
353 return tty_ldisc_try(tty);
354}
355EXPORT_SYMBOL_GPL(tty_ldisc_ref);
356
357/**
358 * tty_ldisc_deref - free a tty ldisc reference
359 * @ld: reference to free up
360 *
361 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
362 * be called in IRQ context.
363 *
364 * Locking: takes tty_ldisc_lock
365 */
366
367void tty_ldisc_deref(struct tty_ldisc *ld)
368{
369 put_ldisc(ld);
370}
371EXPORT_SYMBOL_GPL(tty_ldisc_deref);
372
373static inline void tty_ldisc_put(struct tty_ldisc *ld)
374{
375 put_ldisc(ld);
376}
377
378/**
379 * tty_ldisc_enable - allow ldisc use
380 * @tty: terminal to activate ldisc on
381 *
382 * Set the TTY_LDISC flag when the line discipline can be called
383 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
384 * changing flag to indicate any ldisc change is now over.
385 *
386 * Note: nobody should set the TTY_LDISC bit except via this function.
387 * Clearing directly is allowed.
388 */
389
390void tty_ldisc_enable(struct tty_struct *tty)
391{
392 set_bit(TTY_LDISC, &tty->flags);
393 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
394 wake_up(&tty_ldisc_wait);
395}
396
397/**
398 * tty_ldisc_flush - flush line discipline queue
399 * @tty: tty
400 *
401 * Flush the line discipline queue (if any) for this tty. If there
402 * is no line discipline active this is a no-op.
403 */
404
405void tty_ldisc_flush(struct tty_struct *tty)
406{
407 struct tty_ldisc *ld = tty_ldisc_ref(tty);
408 if (ld) {
409 if (ld->ops->flush_buffer)
410 ld->ops->flush_buffer(tty);
411 tty_ldisc_deref(ld);
412 }
413 tty_buffer_flush(tty);
414}
415EXPORT_SYMBOL_GPL(tty_ldisc_flush);
416
417/**
418 * tty_set_termios_ldisc - set ldisc field
419 * @tty: tty structure
420 * @num: line discipline number
421 *
422 * This is probably overkill for real world processors but
423 * they are not on hot paths so a little discipline won't do
424 * any harm.
425 *
426 * Locking: takes termios_mutex
427 */
428
429static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
430{
431 mutex_lock(&tty->termios_mutex);
432 tty->termios->c_line = num;
433 mutex_unlock(&tty->termios_mutex);
434}
435
436/**
437 * tty_ldisc_open - open a line discipline
438 * @tty: tty we are opening the ldisc on
439 * @ld: discipline to open
440 *
441 * A helper opening method. Also a convenient debugging and check
442 * point.
443 *
444 * Locking: always called with BTM already held.
445 */
446
447static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
448{
449 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
450 if (ld->ops->open) {
451 int ret;
452 /* BTM here locks versus a hangup event */
453 WARN_ON(!tty_locked());
454 ret = ld->ops->open(tty);
455 if (ret)
456 clear_bit(TTY_LDISC_OPEN, &tty->flags);
457 return ret;
458 }
459 return 0;
460}
461
462/**
463 * tty_ldisc_close - close a line discipline
464 * @tty: tty we are opening the ldisc on
465 * @ld: discipline to close
466 *
467 * A helper close method. Also a convenient debugging and check
468 * point.
469 */
470
471static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
472{
473 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
474 clear_bit(TTY_LDISC_OPEN, &tty->flags);
475 if (ld->ops->close)
476 ld->ops->close(tty);
477}
478
479/**
480 * tty_ldisc_restore - helper for tty ldisc change
481 * @tty: tty to recover
482 * @old: previous ldisc
483 *
484 * Restore the previous line discipline or N_TTY when a line discipline
485 * change fails due to an open error
486 */
487
488static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
489{
490 char buf[64];
491 struct tty_ldisc *new_ldisc;
492 int r;
493
494 /* There is an outstanding reference here so this is safe */
495 old = tty_ldisc_get(old->ops->num);
496 WARN_ON(IS_ERR(old));
497 tty_ldisc_assign(tty, old);
498 tty_set_termios_ldisc(tty, old->ops->num);
499 if (tty_ldisc_open(tty, old) < 0) {
500 tty_ldisc_put(old);
501 /* This driver is always present */
502 new_ldisc = tty_ldisc_get(N_TTY);
503 if (IS_ERR(new_ldisc))
504 panic("n_tty: get");
505 tty_ldisc_assign(tty, new_ldisc);
506 tty_set_termios_ldisc(tty, N_TTY);
507 r = tty_ldisc_open(tty, new_ldisc);
508 if (r < 0)
509 panic("Couldn't open N_TTY ldisc for "
510 "%s --- error %d.",
511 tty_name(tty, buf), r);
512 }
513}
514
515/**
516 * tty_ldisc_halt - shut down the line discipline
517 * @tty: tty device
518 *
519 * Shut down the line discipline and work queue for this tty device.
520 * The TTY_LDISC flag being cleared ensures no further references can
521 * be obtained while the delayed work queue halt ensures that no more
522 * data is fed to the ldisc.
523 *
524 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
525 * in order to make sure any currently executing ldisc work is also
526 * flushed.
527 */
528
529static int tty_ldisc_halt(struct tty_struct *tty)
530{
531 clear_bit(TTY_LDISC, &tty->flags);
532 return cancel_work_sync(&tty->buf.work);
533}
534
535/**
536 * tty_ldisc_flush_works - flush all works of a tty
537 * @tty: tty device to flush works for
538 *
539 * Sync flush all works belonging to @tty.
540 */
541static void tty_ldisc_flush_works(struct tty_struct *tty)
542{
543 flush_work_sync(&tty->hangup_work);
544 flush_work_sync(&tty->SAK_work);
545 flush_work_sync(&tty->buf.work);
546}
547
548/**
549 * tty_ldisc_wait_idle - wait for the ldisc to become idle
550 * @tty: tty to wait for
551 *
552 * Wait for the line discipline to become idle. The discipline must
553 * have been halted for this to guarantee it remains idle.
554 */
555static int tty_ldisc_wait_idle(struct tty_struct *tty)
556{
557 int ret;
558 ret = wait_event_timeout(tty_ldisc_idle,
559 atomic_read(&tty->ldisc->users) == 1, 5 * HZ);
560 if (ret < 0)
561 return ret;
562 return ret > 0 ? 0 : -EBUSY;
563}
564
565/**
566 * tty_set_ldisc - set line discipline
567 * @tty: the terminal to set
568 * @ldisc: the line discipline
569 *
570 * Set the discipline of a tty line. Must be called from a process
571 * context. The ldisc change logic has to protect itself against any
572 * overlapping ldisc change (including on the other end of pty pairs),
573 * the close of one side of a tty/pty pair, and eventually hangup.
574 *
575 * Locking: takes tty_ldisc_lock, termios_mutex
576 */
577
578int tty_set_ldisc(struct tty_struct *tty, int ldisc)
579{
580 int retval;
581 struct tty_ldisc *o_ldisc, *new_ldisc;
582 int work, o_work = 0;
583 struct tty_struct *o_tty;
584
585 new_ldisc = tty_ldisc_get(ldisc);
586 if (IS_ERR(new_ldisc))
587 return PTR_ERR(new_ldisc);
588
589 tty_lock();
590 /*
591 * We need to look at the tty locking here for pty/tty pairs
592 * when both sides try to change in parallel.
593 */
594
595 o_tty = tty->link; /* o_tty is the pty side or NULL */
596
597
598 /*
599 * Check the no-op case
600 */
601
602 if (tty->ldisc->ops->num == ldisc) {
603 tty_unlock();
604 tty_ldisc_put(new_ldisc);
605 return 0;
606 }
607
608 tty_unlock();
609 /*
610 * Problem: What do we do if this blocks ?
611 * We could deadlock here
612 */
613
614 tty_wait_until_sent(tty, 0);
615
616 tty_lock();
617 mutex_lock(&tty->ldisc_mutex);
618
619 /*
620 * We could be midstream of another ldisc change which has
621 * dropped the lock during processing. If so we need to wait.
622 */
623
624 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
625 mutex_unlock(&tty->ldisc_mutex);
626 tty_unlock();
627 wait_event(tty_ldisc_wait,
628 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
629 tty_lock();
630 mutex_lock(&tty->ldisc_mutex);
631 }
632
633 set_bit(TTY_LDISC_CHANGING, &tty->flags);
634
635 /*
636 * No more input please, we are switching. The new ldisc
637 * will update this value in the ldisc open function
638 */
639
640 tty->receive_room = 0;
641
642 o_ldisc = tty->ldisc;
643
644 tty_unlock();
645 /*
646 * Make sure we don't change while someone holds a
647 * reference to the line discipline. The TTY_LDISC bit
648 * prevents anyone taking a reference once it is clear.
649 * We need the lock to avoid racing reference takers.
650 *
651 * We must clear the TTY_LDISC bit here to avoid a livelock
652 * with a userspace app continually trying to use the tty in
653 * parallel to the change and re-referencing the tty.
654 */
655
656 work = tty_ldisc_halt(tty);
657 if (o_tty)
658 o_work = tty_ldisc_halt(o_tty);
659
660 /*
661 * Wait for ->hangup_work and ->buf.work handlers to terminate.
662 * We must drop the mutex here in case a hangup is also in process.
663 */
664
665 mutex_unlock(&tty->ldisc_mutex);
666
667 tty_ldisc_flush_works(tty);
668
669 retval = tty_ldisc_wait_idle(tty);
670
671 tty_lock();
672 mutex_lock(&tty->ldisc_mutex);
673
674 /* handle wait idle failure locked */
675 if (retval) {
676 tty_ldisc_put(new_ldisc);
677 goto enable;
678 }
679
680 if (test_bit(TTY_HUPPED, &tty->flags)) {
681 /* We were raced by the hangup method. It will have stomped
682 the ldisc data and closed the ldisc down */
683 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
684 mutex_unlock(&tty->ldisc_mutex);
685 tty_ldisc_put(new_ldisc);
686 tty_unlock();
687 return -EIO;
688 }
689
690 /* Shutdown the current discipline. */
691 tty_ldisc_close(tty, o_ldisc);
692
693 /* Now set up the new line discipline. */
694 tty_ldisc_assign(tty, new_ldisc);
695 tty_set_termios_ldisc(tty, ldisc);
696
697 retval = tty_ldisc_open(tty, new_ldisc);
698 if (retval < 0) {
699 /* Back to the old one or N_TTY if we can't */
700 tty_ldisc_put(new_ldisc);
701 tty_ldisc_restore(tty, o_ldisc);
702 }
703
704 /* At this point we hold a reference to the new ldisc and a
705 a reference to the old ldisc. If we ended up flipping back
706 to the existing ldisc we have two references to it */
707
708 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
709 tty->ops->set_ldisc(tty);
710
711 tty_ldisc_put(o_ldisc);
712
713enable:
714 /*
715 * Allow ldisc referencing to occur again
716 */
717
718 tty_ldisc_enable(tty);
719 if (o_tty)
720 tty_ldisc_enable(o_tty);
721
722 /* Restart the work queue in case no characters kick it off. Safe if
723 already running */
724 if (work)
725 schedule_work(&tty->buf.work);
726 if (o_work)
727 schedule_work(&o_tty->buf.work);
728 mutex_unlock(&tty->ldisc_mutex);
729 tty_unlock();
730 return retval;
731}
732
733/**
734 * tty_reset_termios - reset terminal state
735 * @tty: tty to reset
736 *
737 * Restore a terminal to the driver default state.
738 */
739
740static void tty_reset_termios(struct tty_struct *tty)
741{
742 mutex_lock(&tty->termios_mutex);
743 *tty->termios = tty->driver->init_termios;
744 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
745 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
746 mutex_unlock(&tty->termios_mutex);
747}
748
749
750/**
751 * tty_ldisc_reinit - reinitialise the tty ldisc
752 * @tty: tty to reinit
753 * @ldisc: line discipline to reinitialize
754 *
755 * Switch the tty to a line discipline and leave the ldisc
756 * state closed
757 */
758
759static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
760{
761 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
762
763 if (IS_ERR(ld))
764 return -1;
765
766 WARN_ON_ONCE(tty_ldisc_wait_idle(tty));
767
768 tty_ldisc_close(tty, tty->ldisc);
769 tty_ldisc_put(tty->ldisc);
770 tty->ldisc = NULL;
771 /*
772 * Switch the line discipline back
773 */
774 tty_ldisc_assign(tty, ld);
775 tty_set_termios_ldisc(tty, ldisc);
776
777 return 0;
778}
779
780/**
781 * tty_ldisc_hangup - hangup ldisc reset
782 * @tty: tty being hung up
783 *
784 * Some tty devices reset their termios when they receive a hangup
785 * event. In that situation we must also switch back to N_TTY properly
786 * before we reset the termios data.
787 *
788 * Locking: We can take the ldisc mutex as the rest of the code is
789 * careful to allow for this.
790 *
791 * In the pty pair case this occurs in the close() path of the
792 * tty itself so we must be careful about locking rules.
793 */
794
795void tty_ldisc_hangup(struct tty_struct *tty)
796{
797 struct tty_ldisc *ld;
798 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
799 int err = 0;
800
801 /*
802 * FIXME! What are the locking issues here? This may me overdoing
803 * things... This question is especially important now that we've
804 * removed the irqlock.
805 */
806 ld = tty_ldisc_ref(tty);
807 if (ld != NULL) {
808 /* We may have no line discipline at this point */
809 if (ld->ops->flush_buffer)
810 ld->ops->flush_buffer(tty);
811 tty_driver_flush_buffer(tty);
812 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
813 ld->ops->write_wakeup)
814 ld->ops->write_wakeup(tty);
815 if (ld->ops->hangup)
816 ld->ops->hangup(tty);
817 tty_ldisc_deref(ld);
818 }
819 /*
820 * FIXME: Once we trust the LDISC code better we can wait here for
821 * ldisc completion and fix the driver call race
822 */
823 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
824 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
825 /*
826 * Shutdown the current line discipline, and reset it to
827 * N_TTY if need be.
828 *
829 * Avoid racing set_ldisc or tty_ldisc_release
830 */
831 mutex_lock(&tty->ldisc_mutex);
832
833 /*
834 * this is like tty_ldisc_halt, but we need to give up
835 * the BTM before calling cancel_work_sync, which may
836 * need to wait for another function taking the BTM
837 */
838 clear_bit(TTY_LDISC, &tty->flags);
839 tty_unlock();
840 cancel_work_sync(&tty->buf.work);
841 mutex_unlock(&tty->ldisc_mutex);
842
843 tty_lock();
844 mutex_lock(&tty->ldisc_mutex);
845
846 /* At this point we have a closed ldisc and we want to
847 reopen it. We could defer this to the next open but
848 it means auditing a lot of other paths so this is
849 a FIXME */
850 if (tty->ldisc) { /* Not yet closed */
851 if (reset == 0) {
852
853 if (!tty_ldisc_reinit(tty, tty->termios->c_line))
854 err = tty_ldisc_open(tty, tty->ldisc);
855 else
856 err = 1;
857 }
858 /* If the re-open fails or we reset then go to N_TTY. The
859 N_TTY open cannot fail */
860 if (reset || err) {
861 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
862 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
863 }
864 tty_ldisc_enable(tty);
865 }
866 mutex_unlock(&tty->ldisc_mutex);
867 if (reset)
868 tty_reset_termios(tty);
869}
870
871/**
872 * tty_ldisc_setup - open line discipline
873 * @tty: tty being shut down
874 * @o_tty: pair tty for pty/tty pairs
875 *
876 * Called during the initial open of a tty/pty pair in order to set up the
877 * line disciplines and bind them to the tty. This has no locking issues
878 * as the device isn't yet active.
879 */
880
881int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
882{
883 struct tty_ldisc *ld = tty->ldisc;
884 int retval;
885
886 retval = tty_ldisc_open(tty, ld);
887 if (retval)
888 return retval;
889
890 if (o_tty) {
891 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
892 if (retval) {
893 tty_ldisc_close(tty, ld);
894 return retval;
895 }
896 tty_ldisc_enable(o_tty);
897 }
898 tty_ldisc_enable(tty);
899 return 0;
900}
901/**
902 * tty_ldisc_release - release line discipline
903 * @tty: tty being shut down
904 * @o_tty: pair tty for pty/tty pairs
905 *
906 * Called during the final close of a tty/pty pair in order to shut down
907 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
908 * ldisc has not been opened.
909 */
910
911void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
912{
913 /*
914 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
915 * kill any delayed work. As this is the final close it does not
916 * race with the set_ldisc code path.
917 */
918
919 tty_unlock();
920 tty_ldisc_halt(tty);
921 tty_ldisc_flush_works(tty);
922 tty_lock();
923
924 mutex_lock(&tty->ldisc_mutex);
925 /*
926 * Now kill off the ldisc
927 */
928 tty_ldisc_close(tty, tty->ldisc);
929 tty_ldisc_put(tty->ldisc);
930 /* Force an oops if we mess this up */
931 tty->ldisc = NULL;
932
933 /* Ensure the next open requests the N_TTY ldisc */
934 tty_set_termios_ldisc(tty, N_TTY);
935 mutex_unlock(&tty->ldisc_mutex);
936
937 /* This will need doing differently if we need to lock */
938 if (o_tty)
939 tty_ldisc_release(o_tty, NULL);
940
941 /* And the memory resources remaining (buffers, termios) will be
942 disposed of when the kref hits zero */
943}
944
945/**
946 * tty_ldisc_init - ldisc setup for new tty
947 * @tty: tty being allocated
948 *
949 * Set up the line discipline objects for a newly allocated tty. Note that
950 * the tty structure is not completely set up when this call is made.
951 */
952
953void tty_ldisc_init(struct tty_struct *tty)
954{
955 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
956 if (IS_ERR(ld))
957 panic("n_tty: init_tty");
958 tty_ldisc_assign(tty, ld);
959}
960
961/**
962 * tty_ldisc_init - ldisc cleanup for new tty
963 * @tty: tty that was allocated recently
964 *
965 * The tty structure must not becompletely set up (tty_ldisc_setup) when
966 * this call is made.
967 */
968void tty_ldisc_deinit(struct tty_struct *tty)
969{
970 put_ldisc(tty->ldisc);
971 tty_ldisc_assign(tty, NULL);
972}
973
974void tty_ldisc_begin(void)
975{
976 /* Setup the default TTY line discipline. */
977 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
978}
diff --git a/drivers/tty/tty_mutex.c b/drivers/tty/tty_mutex.c
new file mode 100644
index 000000000000..3b2bb7719442
--- /dev/null
+++ b/drivers/tty/tty_mutex.c
@@ -0,0 +1,44 @@
1#include <linux/tty.h>
2#include <linux/module.h>
3#include <linux/kallsyms.h>
4#include <linux/semaphore.h>
5#include <linux/sched.h>
6
7/*
8 * The 'big tty mutex'
9 *
10 * This mutex is taken and released by tty_lock() and tty_unlock(),
11 * replacing the older big kernel lock.
12 * It can no longer be taken recursively, and does not get
13 * released implicitly while sleeping.
14 *
15 * Don't use in new code.
16 */
17static DEFINE_MUTEX(big_tty_mutex);
18struct task_struct *__big_tty_mutex_owner;
19EXPORT_SYMBOL_GPL(__big_tty_mutex_owner);
20
21/*
22 * Getting the big tty mutex.
23 */
24void __lockfunc tty_lock(void)
25{
26 struct task_struct *task = current;
27
28 WARN_ON(__big_tty_mutex_owner == task);
29
30 mutex_lock(&big_tty_mutex);
31 __big_tty_mutex_owner = task;
32}
33EXPORT_SYMBOL(tty_lock);
34
35void __lockfunc tty_unlock(void)
36{
37 struct task_struct *task = current;
38
39 WARN_ON(__big_tty_mutex_owner != task);
40 __big_tty_mutex_owner = NULL;
41
42 mutex_unlock(&big_tty_mutex);
43}
44EXPORT_SYMBOL(tty_unlock);
diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c
new file mode 100644
index 000000000000..33d37d230f8f
--- /dev/null
+++ b/drivers/tty/tty_port.c
@@ -0,0 +1,446 @@
1/*
2 * Tty port functions
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/serial.h>
11#include <linux/timer.h>
12#include <linux/string.h>
13#include <linux/slab.h>
14#include <linux/sched.h>
15#include <linux/init.h>
16#include <linux/wait.h>
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/module.h>
20
21void tty_port_init(struct tty_port *port)
22{
23 memset(port, 0, sizeof(*port));
24 init_waitqueue_head(&port->open_wait);
25 init_waitqueue_head(&port->close_wait);
26 init_waitqueue_head(&port->delta_msr_wait);
27 mutex_init(&port->mutex);
28 mutex_init(&port->buf_mutex);
29 spin_lock_init(&port->lock);
30 port->close_delay = (50 * HZ) / 100;
31 port->closing_wait = (3000 * HZ) / 100;
32 kref_init(&port->kref);
33}
34EXPORT_SYMBOL(tty_port_init);
35
36int tty_port_alloc_xmit_buf(struct tty_port *port)
37{
38 /* We may sleep in get_zeroed_page() */
39 mutex_lock(&port->buf_mutex);
40 if (port->xmit_buf == NULL)
41 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
42 mutex_unlock(&port->buf_mutex);
43 if (port->xmit_buf == NULL)
44 return -ENOMEM;
45 return 0;
46}
47EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
48
49void tty_port_free_xmit_buf(struct tty_port *port)
50{
51 mutex_lock(&port->buf_mutex);
52 if (port->xmit_buf != NULL) {
53 free_page((unsigned long)port->xmit_buf);
54 port->xmit_buf = NULL;
55 }
56 mutex_unlock(&port->buf_mutex);
57}
58EXPORT_SYMBOL(tty_port_free_xmit_buf);
59
60static void tty_port_destructor(struct kref *kref)
61{
62 struct tty_port *port = container_of(kref, struct tty_port, kref);
63 if (port->xmit_buf)
64 free_page((unsigned long)port->xmit_buf);
65 if (port->ops->destruct)
66 port->ops->destruct(port);
67 else
68 kfree(port);
69}
70
71void tty_port_put(struct tty_port *port)
72{
73 if (port)
74 kref_put(&port->kref, tty_port_destructor);
75}
76EXPORT_SYMBOL(tty_port_put);
77
78/**
79 * tty_port_tty_get - get a tty reference
80 * @port: tty port
81 *
82 * Return a refcount protected tty instance or NULL if the port is not
83 * associated with a tty (eg due to close or hangup)
84 */
85
86struct tty_struct *tty_port_tty_get(struct tty_port *port)
87{
88 unsigned long flags;
89 struct tty_struct *tty;
90
91 spin_lock_irqsave(&port->lock, flags);
92 tty = tty_kref_get(port->tty);
93 spin_unlock_irqrestore(&port->lock, flags);
94 return tty;
95}
96EXPORT_SYMBOL(tty_port_tty_get);
97
98/**
99 * tty_port_tty_set - set the tty of a port
100 * @port: tty port
101 * @tty: the tty
102 *
103 * Associate the port and tty pair. Manages any internal refcounts.
104 * Pass NULL to deassociate a port
105 */
106
107void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
108{
109 unsigned long flags;
110
111 spin_lock_irqsave(&port->lock, flags);
112 if (port->tty)
113 tty_kref_put(port->tty);
114 port->tty = tty_kref_get(tty);
115 spin_unlock_irqrestore(&port->lock, flags);
116}
117EXPORT_SYMBOL(tty_port_tty_set);
118
119static void tty_port_shutdown(struct tty_port *port)
120{
121 mutex_lock(&port->mutex);
122 if (port->ops->shutdown && !port->console &&
123 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
124 port->ops->shutdown(port);
125 mutex_unlock(&port->mutex);
126}
127
128/**
129 * tty_port_hangup - hangup helper
130 * @port: tty port
131 *
132 * Perform port level tty hangup flag and count changes. Drop the tty
133 * reference.
134 */
135
136void tty_port_hangup(struct tty_port *port)
137{
138 unsigned long flags;
139
140 spin_lock_irqsave(&port->lock, flags);
141 port->count = 0;
142 port->flags &= ~ASYNC_NORMAL_ACTIVE;
143 if (port->tty) {
144 set_bit(TTY_IO_ERROR, &port->tty->flags);
145 tty_kref_put(port->tty);
146 }
147 port->tty = NULL;
148 spin_unlock_irqrestore(&port->lock, flags);
149 wake_up_interruptible(&port->open_wait);
150 wake_up_interruptible(&port->delta_msr_wait);
151 tty_port_shutdown(port);
152}
153EXPORT_SYMBOL(tty_port_hangup);
154
155/**
156 * tty_port_carrier_raised - carrier raised check
157 * @port: tty port
158 *
159 * Wrapper for the carrier detect logic. For the moment this is used
160 * to hide some internal details. This will eventually become entirely
161 * internal to the tty port.
162 */
163
164int tty_port_carrier_raised(struct tty_port *port)
165{
166 if (port->ops->carrier_raised == NULL)
167 return 1;
168 return port->ops->carrier_raised(port);
169}
170EXPORT_SYMBOL(tty_port_carrier_raised);
171
172/**
173 * tty_port_raise_dtr_rts - Raise DTR/RTS
174 * @port: tty port
175 *
176 * Wrapper for the DTR/RTS raise logic. For the moment this is used
177 * to hide some internal details. This will eventually become entirely
178 * internal to the tty port.
179 */
180
181void tty_port_raise_dtr_rts(struct tty_port *port)
182{
183 if (port->ops->dtr_rts)
184 port->ops->dtr_rts(port, 1);
185}
186EXPORT_SYMBOL(tty_port_raise_dtr_rts);
187
188/**
189 * tty_port_lower_dtr_rts - Lower DTR/RTS
190 * @port: tty port
191 *
192 * Wrapper for the DTR/RTS raise logic. For the moment this is used
193 * to hide some internal details. This will eventually become entirely
194 * internal to the tty port.
195 */
196
197void tty_port_lower_dtr_rts(struct tty_port *port)
198{
199 if (port->ops->dtr_rts)
200 port->ops->dtr_rts(port, 0);
201}
202EXPORT_SYMBOL(tty_port_lower_dtr_rts);
203
204/**
205 * tty_port_block_til_ready - Waiting logic for tty open
206 * @port: the tty port being opened
207 * @tty: the tty device being bound
208 * @filp: the file pointer of the opener
209 *
210 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
211 * Handles:
212 * - hangup (both before and during)
213 * - non blocking open
214 * - rts/dtr/dcd
215 * - signals
216 * - port flags and counts
217 *
218 * The passed tty_port must implement the carrier_raised method if it can
219 * do carrier detect and the dtr_rts method if it supports software
220 * management of these lines. Note that the dtr/rts raise is done each
221 * iteration as a hangup may have previously dropped them while we wait.
222 */
223
224int tty_port_block_til_ready(struct tty_port *port,
225 struct tty_struct *tty, struct file *filp)
226{
227 int do_clocal = 0, retval;
228 unsigned long flags;
229 DEFINE_WAIT(wait);
230 int cd;
231
232 /* block if port is in the process of being closed */
233 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
234 wait_event_interruptible_tty(port->close_wait,
235 !(port->flags & ASYNC_CLOSING));
236 if (port->flags & ASYNC_HUP_NOTIFY)
237 return -EAGAIN;
238 else
239 return -ERESTARTSYS;
240 }
241
242 /* if non-blocking mode is set we can pass directly to open unless
243 the port has just hung up or is in another error state */
244 if (tty->flags & (1 << TTY_IO_ERROR)) {
245 port->flags |= ASYNC_NORMAL_ACTIVE;
246 return 0;
247 }
248 if (filp->f_flags & O_NONBLOCK) {
249 /* Indicate we are open */
250 if (tty->termios->c_cflag & CBAUD)
251 tty_port_raise_dtr_rts(port);
252 port->flags |= ASYNC_NORMAL_ACTIVE;
253 return 0;
254 }
255
256 if (C_CLOCAL(tty))
257 do_clocal = 1;
258
259 /* Block waiting until we can proceed. We may need to wait for the
260 carrier, but we must also wait for any close that is in progress
261 before the next open may complete */
262
263 retval = 0;
264
265 /* The port lock protects the port counts */
266 spin_lock_irqsave(&port->lock, flags);
267 if (!tty_hung_up_p(filp))
268 port->count--;
269 port->blocked_open++;
270 spin_unlock_irqrestore(&port->lock, flags);
271
272 while (1) {
273 /* Indicate we are open */
274 if (tty->termios->c_cflag & CBAUD)
275 tty_port_raise_dtr_rts(port);
276
277 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
278 /* Check for a hangup or uninitialised port.
279 Return accordingly */
280 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
281 if (port->flags & ASYNC_HUP_NOTIFY)
282 retval = -EAGAIN;
283 else
284 retval = -ERESTARTSYS;
285 break;
286 }
287 /* Probe the carrier. For devices with no carrier detect this
288 will always return true */
289 cd = tty_port_carrier_raised(port);
290 if (!(port->flags & ASYNC_CLOSING) &&
291 (do_clocal || cd))
292 break;
293 if (signal_pending(current)) {
294 retval = -ERESTARTSYS;
295 break;
296 }
297 tty_unlock();
298 schedule();
299 tty_lock();
300 }
301 finish_wait(&port->open_wait, &wait);
302
303 /* Update counts. A parallel hangup will have set count to zero and
304 we must not mess that up further */
305 spin_lock_irqsave(&port->lock, flags);
306 if (!tty_hung_up_p(filp))
307 port->count++;
308 port->blocked_open--;
309 if (retval == 0)
310 port->flags |= ASYNC_NORMAL_ACTIVE;
311 spin_unlock_irqrestore(&port->lock, flags);
312 return retval;
313}
314EXPORT_SYMBOL(tty_port_block_til_ready);
315
316int tty_port_close_start(struct tty_port *port,
317 struct tty_struct *tty, struct file *filp)
318{
319 unsigned long flags;
320
321 spin_lock_irqsave(&port->lock, flags);
322 if (tty_hung_up_p(filp)) {
323 spin_unlock_irqrestore(&port->lock, flags);
324 return 0;
325 }
326
327 if (tty->count == 1 && port->count != 1) {
328 printk(KERN_WARNING
329 "tty_port_close_start: tty->count = 1 port count = %d.\n",
330 port->count);
331 port->count = 1;
332 }
333 if (--port->count < 0) {
334 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
335 port->count);
336 port->count = 0;
337 }
338
339 if (port->count) {
340 spin_unlock_irqrestore(&port->lock, flags);
341 if (port->ops->drop)
342 port->ops->drop(port);
343 return 0;
344 }
345 set_bit(ASYNCB_CLOSING, &port->flags);
346 tty->closing = 1;
347 spin_unlock_irqrestore(&port->lock, flags);
348 /* Don't block on a stalled port, just pull the chain */
349 if (tty->flow_stopped)
350 tty_driver_flush_buffer(tty);
351 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
352 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
353 tty_wait_until_sent(tty, port->closing_wait);
354 if (port->drain_delay) {
355 unsigned int bps = tty_get_baud_rate(tty);
356 long timeout;
357
358 if (bps > 1200)
359 timeout = max_t(long,
360 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
361 else
362 timeout = 2 * HZ;
363 schedule_timeout_interruptible(timeout);
364 }
365 /* Flush the ldisc buffering */
366 tty_ldisc_flush(tty);
367
368 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
369 hang up the line */
370 if (tty->termios->c_cflag & HUPCL)
371 tty_port_lower_dtr_rts(port);
372
373 /* Don't call port->drop for the last reference. Callers will want
374 to drop the last active reference in ->shutdown() or the tty
375 shutdown path */
376 return 1;
377}
378EXPORT_SYMBOL(tty_port_close_start);
379
380void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
381{
382 unsigned long flags;
383
384 spin_lock_irqsave(&port->lock, flags);
385 tty->closing = 0;
386
387 if (port->blocked_open) {
388 spin_unlock_irqrestore(&port->lock, flags);
389 if (port->close_delay) {
390 msleep_interruptible(
391 jiffies_to_msecs(port->close_delay));
392 }
393 spin_lock_irqsave(&port->lock, flags);
394 wake_up_interruptible(&port->open_wait);
395 }
396 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
397 wake_up_interruptible(&port->close_wait);
398 spin_unlock_irqrestore(&port->lock, flags);
399}
400EXPORT_SYMBOL(tty_port_close_end);
401
402void tty_port_close(struct tty_port *port, struct tty_struct *tty,
403 struct file *filp)
404{
405 if (tty_port_close_start(port, tty, filp) == 0)
406 return;
407 tty_port_shutdown(port);
408 set_bit(TTY_IO_ERROR, &tty->flags);
409 tty_port_close_end(port, tty);
410 tty_port_tty_set(port, NULL);
411}
412EXPORT_SYMBOL(tty_port_close);
413
414int tty_port_open(struct tty_port *port, struct tty_struct *tty,
415 struct file *filp)
416{
417 spin_lock_irq(&port->lock);
418 if (!tty_hung_up_p(filp))
419 ++port->count;
420 spin_unlock_irq(&port->lock);
421 tty_port_tty_set(port, tty);
422
423 /*
424 * Do the device-specific open only if the hardware isn't
425 * already initialized. Serialize open and shutdown using the
426 * port mutex.
427 */
428
429 mutex_lock(&port->mutex);
430
431 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
432 clear_bit(TTY_IO_ERROR, &tty->flags);
433 if (port->ops->activate) {
434 int retval = port->ops->activate(port, tty);
435 if (retval) {
436 mutex_unlock(&port->mutex);
437 return retval;
438 }
439 }
440 set_bit(ASYNCB_INITIALIZED, &port->flags);
441 }
442 mutex_unlock(&port->mutex);
443 return tty_port_block_til_ready(port, tty, filp);
444}
445
446EXPORT_SYMBOL(tty_port_open);
diff --git a/drivers/tty/vt/.gitignore b/drivers/tty/vt/.gitignore
new file mode 100644
index 000000000000..83683a2d8e6a
--- /dev/null
+++ b/drivers/tty/vt/.gitignore
@@ -0,0 +1,2 @@
1consolemap_deftbl.c
2defkeymap.c
diff --git a/drivers/tty/vt/Makefile b/drivers/tty/vt/Makefile
new file mode 100644
index 000000000000..14a51c9960df
--- /dev/null
+++ b/drivers/tty/vt/Makefile
@@ -0,0 +1,34 @@
1#
2# This file contains the font map for the default (hardware) font
3#
4FONTMAPFILE = cp437.uni
5
6obj-$(CONFIG_VT) += vt_ioctl.o vc_screen.o \
7 selection.o keyboard.o
8obj-$(CONFIG_CONSOLE_TRANSLATIONS) += consolemap.o consolemap_deftbl.o
9obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o
10
11# Files generated that shall be removed upon make clean
12clean-files := consolemap_deftbl.c defkeymap.c
13
14quiet_cmd_conmk = CONMK $@
15 cmd_conmk = scripts/conmakehash $< > $@
16
17$(obj)/consolemap_deftbl.c: $(src)/$(FONTMAPFILE)
18 $(call cmd,conmk)
19
20$(obj)/defkeymap.o: $(obj)/defkeymap.c
21
22# Uncomment if you're changing the keymap and have an appropriate
23# loadkeys version for the map. By default, we'll use the shipped
24# versions.
25# GENERATE_KEYMAP := 1
26
27ifdef GENERATE_KEYMAP
28
29$(obj)/defkeymap.c: $(obj)/%.c: $(src)/%.map
30 loadkeys --mktable $< > $@.tmp
31 sed -e 's/^static *//' $@.tmp > $@
32 rm $@.tmp
33
34endif
diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c
new file mode 100644
index 000000000000..45d3e80156d4
--- /dev/null
+++ b/drivers/tty/vt/consolemap.c
@@ -0,0 +1,745 @@
1/*
2 * consolemap.c
3 *
4 * Mapping from internal code (such as Latin-1 or Unicode or IBM PC code)
5 * to font positions.
6 *
7 * aeb, 950210
8 *
9 * Support for multiple unimaps by Jakub Jelinek <jj@ultra.linux.cz>, July 1998
10 *
11 * Fix bug in inverse translation. Stanislav Voronyi <stas@cnti.uanet.kharkov.ua>, Dec 1998
12 */
13
14#include <linux/module.h>
15#include <linux/kd.h>
16#include <linux/errno.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/tty.h>
21#include <asm/uaccess.h>
22#include <linux/consolemap.h>
23#include <linux/vt_kern.h>
24
25static unsigned short translations[][256] = {
26 /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */
27 {
28 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
29 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
30 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
31 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
32 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
33 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
34 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
35 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
36 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
37 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
38 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
39 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
40 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
41 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
42 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
43 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
44 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
45 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
46 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
47 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
48 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
49 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
50 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
51 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
52 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
53 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
54 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
55 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
56 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
57 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
58 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
59 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
60 },
61 /* VT100 graphics mapped to Unicode */
62 {
63 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
64 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
65 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
66 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
67 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
68 0x0028, 0x0029, 0x002a, 0x2192, 0x2190, 0x2191, 0x2193, 0x002f,
69 0x2588, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
70 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
71 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
72 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
73 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
74 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x00a0,
75 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
76 0x2591, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
77 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
78 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x007f,
79 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
80 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
81 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
82 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
83 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
84 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
85 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
86 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
87 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
88 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
89 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
90 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
91 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
92 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
93 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
94 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
95 },
96 /* IBM Codepage 437 mapped to Unicode */
97 {
98 0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
99 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
100 0x25b6, 0x25c0, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8,
101 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc,
102 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
103 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
104 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
105 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
106 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
107 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
108 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
109 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
110 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
111 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
112 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
113 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302,
114 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
115 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
116 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
117 0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192,
118 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
119 0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
120 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
121 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
122 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
123 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
124 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
125 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
126 0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03c4,
127 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229,
128 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248,
129 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
130 },
131 /* User mapping -- default to codes for direct font mapping */
132 {
133 0xf000, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf007,
134 0xf008, 0xf009, 0xf00a, 0xf00b, 0xf00c, 0xf00d, 0xf00e, 0xf00f,
135 0xf010, 0xf011, 0xf012, 0xf013, 0xf014, 0xf015, 0xf016, 0xf017,
136 0xf018, 0xf019, 0xf01a, 0xf01b, 0xf01c, 0xf01d, 0xf01e, 0xf01f,
137 0xf020, 0xf021, 0xf022, 0xf023, 0xf024, 0xf025, 0xf026, 0xf027,
138 0xf028, 0xf029, 0xf02a, 0xf02b, 0xf02c, 0xf02d, 0xf02e, 0xf02f,
139 0xf030, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036, 0xf037,
140 0xf038, 0xf039, 0xf03a, 0xf03b, 0xf03c, 0xf03d, 0xf03e, 0xf03f,
141 0xf040, 0xf041, 0xf042, 0xf043, 0xf044, 0xf045, 0xf046, 0xf047,
142 0xf048, 0xf049, 0xf04a, 0xf04b, 0xf04c, 0xf04d, 0xf04e, 0xf04f,
143 0xf050, 0xf051, 0xf052, 0xf053, 0xf054, 0xf055, 0xf056, 0xf057,
144 0xf058, 0xf059, 0xf05a, 0xf05b, 0xf05c, 0xf05d, 0xf05e, 0xf05f,
145 0xf060, 0xf061, 0xf062, 0xf063, 0xf064, 0xf065, 0xf066, 0xf067,
146 0xf068, 0xf069, 0xf06a, 0xf06b, 0xf06c, 0xf06d, 0xf06e, 0xf06f,
147 0xf070, 0xf071, 0xf072, 0xf073, 0xf074, 0xf075, 0xf076, 0xf077,
148 0xf078, 0xf079, 0xf07a, 0xf07b, 0xf07c, 0xf07d, 0xf07e, 0xf07f,
149 0xf080, 0xf081, 0xf082, 0xf083, 0xf084, 0xf085, 0xf086, 0xf087,
150 0xf088, 0xf089, 0xf08a, 0xf08b, 0xf08c, 0xf08d, 0xf08e, 0xf08f,
151 0xf090, 0xf091, 0xf092, 0xf093, 0xf094, 0xf095, 0xf096, 0xf097,
152 0xf098, 0xf099, 0xf09a, 0xf09b, 0xf09c, 0xf09d, 0xf09e, 0xf09f,
153 0xf0a0, 0xf0a1, 0xf0a2, 0xf0a3, 0xf0a4, 0xf0a5, 0xf0a6, 0xf0a7,
154 0xf0a8, 0xf0a9, 0xf0aa, 0xf0ab, 0xf0ac, 0xf0ad, 0xf0ae, 0xf0af,
155 0xf0b0, 0xf0b1, 0xf0b2, 0xf0b3, 0xf0b4, 0xf0b5, 0xf0b6, 0xf0b7,
156 0xf0b8, 0xf0b9, 0xf0ba, 0xf0bb, 0xf0bc, 0xf0bd, 0xf0be, 0xf0bf,
157 0xf0c0, 0xf0c1, 0xf0c2, 0xf0c3, 0xf0c4, 0xf0c5, 0xf0c6, 0xf0c7,
158 0xf0c8, 0xf0c9, 0xf0ca, 0xf0cb, 0xf0cc, 0xf0cd, 0xf0ce, 0xf0cf,
159 0xf0d0, 0xf0d1, 0xf0d2, 0xf0d3, 0xf0d4, 0xf0d5, 0xf0d6, 0xf0d7,
160 0xf0d8, 0xf0d9, 0xf0da, 0xf0db, 0xf0dc, 0xf0dd, 0xf0de, 0xf0df,
161 0xf0e0, 0xf0e1, 0xf0e2, 0xf0e3, 0xf0e4, 0xf0e5, 0xf0e6, 0xf0e7,
162 0xf0e8, 0xf0e9, 0xf0ea, 0xf0eb, 0xf0ec, 0xf0ed, 0xf0ee, 0xf0ef,
163 0xf0f0, 0xf0f1, 0xf0f2, 0xf0f3, 0xf0f4, 0xf0f5, 0xf0f6, 0xf0f7,
164 0xf0f8, 0xf0f9, 0xf0fa, 0xf0fb, 0xf0fc, 0xf0fd, 0xf0fe, 0xf0ff
165 }
166};
167
168/* The standard kernel character-to-font mappings are not invertible
169 -- this is just a best effort. */
170
171#define MAX_GLYPH 512 /* Max possible glyph value */
172
173static int inv_translate[MAX_NR_CONSOLES];
174
175struct uni_pagedir {
176 u16 **uni_pgdir[32];
177 unsigned long refcount;
178 unsigned long sum;
179 unsigned char *inverse_translations[4];
180 u16 *inverse_trans_unicode;
181 int readonly;
182};
183
184static struct uni_pagedir *dflt;
185
186static void set_inverse_transl(struct vc_data *conp, struct uni_pagedir *p, int i)
187{
188 int j, glyph;
189 unsigned short *t = translations[i];
190 unsigned char *q;
191
192 if (!p) return;
193 q = p->inverse_translations[i];
194
195 if (!q) {
196 q = p->inverse_translations[i] = (unsigned char *)
197 kmalloc(MAX_GLYPH, GFP_KERNEL);
198 if (!q) return;
199 }
200 memset(q, 0, MAX_GLYPH);
201
202 for (j = 0; j < E_TABSZ; j++) {
203 glyph = conv_uni_to_pc(conp, t[j]);
204 if (glyph >= 0 && glyph < MAX_GLYPH && q[glyph] < 32) {
205 /* prefer '-' above SHY etc. */
206 q[glyph] = j;
207 }
208 }
209}
210
211static void set_inverse_trans_unicode(struct vc_data *conp,
212 struct uni_pagedir *p)
213{
214 int i, j, k, glyph;
215 u16 **p1, *p2;
216 u16 *q;
217
218 if (!p) return;
219 q = p->inverse_trans_unicode;
220 if (!q) {
221 q = p->inverse_trans_unicode =
222 kmalloc(MAX_GLYPH * sizeof(u16), GFP_KERNEL);
223 if (!q)
224 return;
225 }
226 memset(q, 0, MAX_GLYPH * sizeof(u16));
227
228 for (i = 0; i < 32; i++) {
229 p1 = p->uni_pgdir[i];
230 if (!p1)
231 continue;
232 for (j = 0; j < 32; j++) {
233 p2 = p1[j];
234 if (!p2)
235 continue;
236 for (k = 0; k < 64; k++) {
237 glyph = p2[k];
238 if (glyph >= 0 && glyph < MAX_GLYPH
239 && q[glyph] < 32)
240 q[glyph] = (i << 11) + (j << 6) + k;
241 }
242 }
243 }
244}
245
246unsigned short *set_translate(int m, struct vc_data *vc)
247{
248 inv_translate[vc->vc_num] = m;
249 return translations[m];
250}
251
252/*
253 * Inverse translation is impossible for several reasons:
254 * 1. The font<->character maps are not 1-1.
255 * 2. The text may have been written while a different translation map
256 * was active.
257 * Still, it is now possible to a certain extent to cut and paste non-ASCII.
258 */
259u16 inverse_translate(struct vc_data *conp, int glyph, int use_unicode)
260{
261 struct uni_pagedir *p;
262 int m;
263 if (glyph < 0 || glyph >= MAX_GLYPH)
264 return 0;
265 else if (!(p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc))
266 return glyph;
267 else if (use_unicode) {
268 if (!p->inverse_trans_unicode)
269 return glyph;
270 else
271 return p->inverse_trans_unicode[glyph];
272 } else {
273 m = inv_translate[conp->vc_num];
274 if (!p->inverse_translations[m])
275 return glyph;
276 else
277 return p->inverse_translations[m][glyph];
278 }
279}
280EXPORT_SYMBOL_GPL(inverse_translate);
281
282static void update_user_maps(void)
283{
284 int i;
285 struct uni_pagedir *p, *q = NULL;
286
287 for (i = 0; i < MAX_NR_CONSOLES; i++) {
288 if (!vc_cons_allocated(i))
289 continue;
290 p = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc;
291 if (p && p != q) {
292 set_inverse_transl(vc_cons[i].d, p, USER_MAP);
293 set_inverse_trans_unicode(vc_cons[i].d, p);
294 q = p;
295 }
296 }
297}
298
299/*
300 * Load customizable translation table
301 * arg points to a 256 byte translation table.
302 *
303 * The "old" variants are for translation directly to font (using the
304 * 0xf000-0xf0ff "transparent" Unicodes) whereas the "new" variants set
305 * Unicodes explicitly.
306 */
307int con_set_trans_old(unsigned char __user * arg)
308{
309 int i;
310 unsigned short *p = translations[USER_MAP];
311
312 if (!access_ok(VERIFY_READ, arg, E_TABSZ))
313 return -EFAULT;
314
315 for (i=0; i<E_TABSZ ; i++) {
316 unsigned char uc;
317 __get_user(uc, arg+i);
318 p[i] = UNI_DIRECT_BASE | uc;
319 }
320
321 update_user_maps();
322 return 0;
323}
324
325int con_get_trans_old(unsigned char __user * arg)
326{
327 int i, ch;
328 unsigned short *p = translations[USER_MAP];
329
330 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ))
331 return -EFAULT;
332
333 for (i=0; i<E_TABSZ ; i++)
334 {
335 ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]);
336 __put_user((ch & ~0xff) ? 0 : ch, arg+i);
337 }
338 return 0;
339}
340
341int con_set_trans_new(ushort __user * arg)
342{
343 int i;
344 unsigned short *p = translations[USER_MAP];
345
346 if (!access_ok(VERIFY_READ, arg, E_TABSZ*sizeof(unsigned short)))
347 return -EFAULT;
348
349 for (i=0; i<E_TABSZ ; i++) {
350 unsigned short us;
351 __get_user(us, arg+i);
352 p[i] = us;
353 }
354
355 update_user_maps();
356 return 0;
357}
358
359int con_get_trans_new(ushort __user * arg)
360{
361 int i;
362 unsigned short *p = translations[USER_MAP];
363
364 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ*sizeof(unsigned short)))
365 return -EFAULT;
366
367 for (i=0; i<E_TABSZ ; i++)
368 __put_user(p[i], arg+i);
369
370 return 0;
371}
372
373/*
374 * Unicode -> current font conversion
375 *
376 * A font has at most 512 chars, usually 256.
377 * But one font position may represent several Unicode chars.
378 * A hashtable is somewhat of a pain to deal with, so use a
379 * "paged table" instead. Simulation has shown the memory cost of
380 * this 3-level paged table scheme to be comparable to a hash table.
381 */
382
383extern u8 dfont_unicount[]; /* Defined in console_defmap.c */
384extern u16 dfont_unitable[];
385
386static void con_release_unimap(struct uni_pagedir *p)
387{
388 u16 **p1;
389 int i, j;
390
391 if (p == dflt) dflt = NULL;
392 for (i = 0; i < 32; i++) {
393 if ((p1 = p->uni_pgdir[i]) != NULL) {
394 for (j = 0; j < 32; j++)
395 kfree(p1[j]);
396 kfree(p1);
397 }
398 p->uni_pgdir[i] = NULL;
399 }
400 for (i = 0; i < 4; i++) {
401 kfree(p->inverse_translations[i]);
402 p->inverse_translations[i] = NULL;
403 }
404 if (p->inverse_trans_unicode) {
405 kfree(p->inverse_trans_unicode);
406 p->inverse_trans_unicode = NULL;
407 }
408}
409
410void con_free_unimap(struct vc_data *vc)
411{
412 struct uni_pagedir *p;
413
414 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
415 if (!p)
416 return;
417 *vc->vc_uni_pagedir_loc = 0;
418 if (--p->refcount)
419 return;
420 con_release_unimap(p);
421 kfree(p);
422}
423
424static int con_unify_unimap(struct vc_data *conp, struct uni_pagedir *p)
425{
426 int i, j, k;
427 struct uni_pagedir *q;
428
429 for (i = 0; i < MAX_NR_CONSOLES; i++) {
430 if (!vc_cons_allocated(i))
431 continue;
432 q = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc;
433 if (!q || q == p || q->sum != p->sum)
434 continue;
435 for (j = 0; j < 32; j++) {
436 u16 **p1, **q1;
437 p1 = p->uni_pgdir[j]; q1 = q->uni_pgdir[j];
438 if (!p1 && !q1)
439 continue;
440 if (!p1 || !q1)
441 break;
442 for (k = 0; k < 32; k++) {
443 if (!p1[k] && !q1[k])
444 continue;
445 if (!p1[k] || !q1[k])
446 break;
447 if (memcmp(p1[k], q1[k], 64*sizeof(u16)))
448 break;
449 }
450 if (k < 32)
451 break;
452 }
453 if (j == 32) {
454 q->refcount++;
455 *conp->vc_uni_pagedir_loc = (unsigned long)q;
456 con_release_unimap(p);
457 kfree(p);
458 return 1;
459 }
460 }
461 return 0;
462}
463
464static int
465con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos)
466{
467 int i, n;
468 u16 **p1, *p2;
469
470 if (!(p1 = p->uni_pgdir[n = unicode >> 11])) {
471 p1 = p->uni_pgdir[n] = kmalloc(32*sizeof(u16 *), GFP_KERNEL);
472 if (!p1) return -ENOMEM;
473 for (i = 0; i < 32; i++)
474 p1[i] = NULL;
475 }
476
477 if (!(p2 = p1[n = (unicode >> 6) & 0x1f])) {
478 p2 = p1[n] = kmalloc(64*sizeof(u16), GFP_KERNEL);
479 if (!p2) return -ENOMEM;
480 memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */
481 }
482
483 p2[unicode & 0x3f] = fontpos;
484
485 p->sum += (fontpos << 20) + unicode;
486
487 return 0;
488}
489
490/* ui is a leftover from using a hashtable, but might be used again */
491int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui)
492{
493 struct uni_pagedir *p, *q;
494
495 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
496 if (p && p->readonly) return -EIO;
497 if (!p || --p->refcount) {
498 q = kzalloc(sizeof(*p), GFP_KERNEL);
499 if (!q) {
500 if (p) p->refcount++;
501 return -ENOMEM;
502 }
503 q->refcount=1;
504 *vc->vc_uni_pagedir_loc = (unsigned long)q;
505 } else {
506 if (p == dflt) dflt = NULL;
507 p->refcount++;
508 p->sum = 0;
509 con_release_unimap(p);
510 }
511 return 0;
512}
513
514int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
515{
516 int err = 0, err1, i;
517 struct uni_pagedir *p, *q;
518
519 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
520 if (p->readonly) return -EIO;
521
522 if (!ct) return 0;
523
524 if (p->refcount > 1) {
525 int j, k;
526 u16 **p1, *p2, l;
527
528 err1 = con_clear_unimap(vc, NULL);
529 if (err1) return err1;
530
531 q = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
532 for (i = 0, l = 0; i < 32; i++)
533 if ((p1 = p->uni_pgdir[i]))
534 for (j = 0; j < 32; j++)
535 if ((p2 = p1[j]))
536 for (k = 0; k < 64; k++, l++)
537 if (p2[k] != 0xffff) {
538 err1 = con_insert_unipair(q, l, p2[k]);
539 if (err1) {
540 p->refcount++;
541 *vc->vc_uni_pagedir_loc = (unsigned long)p;
542 con_release_unimap(q);
543 kfree(q);
544 return err1;
545 }
546 }
547 p = q;
548 } else if (p == dflt)
549 dflt = NULL;
550
551 while (ct--) {
552 unsigned short unicode, fontpos;
553 __get_user(unicode, &list->unicode);
554 __get_user(fontpos, &list->fontpos);
555 if ((err1 = con_insert_unipair(p, unicode,fontpos)) != 0)
556 err = err1;
557 list++;
558 }
559
560 if (con_unify_unimap(vc, p))
561 return err;
562
563 for (i = 0; i <= 3; i++)
564 set_inverse_transl(vc, p, i); /* Update all inverse translations */
565 set_inverse_trans_unicode(vc, p);
566
567 return err;
568}
569
570/* Loads the unimap for the hardware font, as defined in uni_hash.tbl.
571 The representation used was the most compact I could come up
572 with. This routine is executed at sys_setup time, and when the
573 PIO_FONTRESET ioctl is called. */
574
575int con_set_default_unimap(struct vc_data *vc)
576{
577 int i, j, err = 0, err1;
578 u16 *q;
579 struct uni_pagedir *p;
580
581 if (dflt) {
582 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
583 if (p == dflt)
584 return 0;
585 dflt->refcount++;
586 *vc->vc_uni_pagedir_loc = (unsigned long)dflt;
587 if (p && --p->refcount) {
588 con_release_unimap(p);
589 kfree(p);
590 }
591 return 0;
592 }
593
594 /* The default font is always 256 characters */
595
596 err = con_clear_unimap(vc, NULL);
597 if (err) return err;
598
599 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
600 q = dfont_unitable;
601
602 for (i = 0; i < 256; i++)
603 for (j = dfont_unicount[i]; j; j--) {
604 err1 = con_insert_unipair(p, *(q++), i);
605 if (err1)
606 err = err1;
607 }
608
609 if (con_unify_unimap(vc, p)) {
610 dflt = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
611 return err;
612 }
613
614 for (i = 0; i <= 3; i++)
615 set_inverse_transl(vc, p, i); /* Update all inverse translations */
616 set_inverse_trans_unicode(vc, p);
617 dflt = p;
618 return err;
619}
620EXPORT_SYMBOL(con_set_default_unimap);
621
622int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc)
623{
624 struct uni_pagedir *q;
625
626 if (!*src_vc->vc_uni_pagedir_loc)
627 return -EINVAL;
628 if (*dst_vc->vc_uni_pagedir_loc == *src_vc->vc_uni_pagedir_loc)
629 return 0;
630 con_free_unimap(dst_vc);
631 q = (struct uni_pagedir *)*src_vc->vc_uni_pagedir_loc;
632 q->refcount++;
633 *dst_vc->vc_uni_pagedir_loc = (long)q;
634 return 0;
635}
636
637int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct unipair __user *list)
638{
639 int i, j, k, ect;
640 u16 **p1, *p2;
641 struct uni_pagedir *p;
642
643 ect = 0;
644 if (*vc->vc_uni_pagedir_loc) {
645 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
646 for (i = 0; i < 32; i++)
647 if ((p1 = p->uni_pgdir[i]))
648 for (j = 0; j < 32; j++)
649 if ((p2 = *(p1++)))
650 for (k = 0; k < 64; k++) {
651 if (*p2 < MAX_GLYPH && ect++ < ct) {
652 __put_user((u_short)((i<<11)+(j<<6)+k),
653 &list->unicode);
654 __put_user((u_short) *p2,
655 &list->fontpos);
656 list++;
657 }
658 p2++;
659 }
660 }
661 __put_user(ect, uct);
662 return ((ect <= ct) ? 0 : -ENOMEM);
663}
664
665void con_protect_unimap(struct vc_data *vc, int rdonly)
666{
667 struct uni_pagedir *p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
668
669 if (p)
670 p->readonly = rdonly;
671}
672
673/*
674 * Always use USER_MAP. These functions are used by the keyboard,
675 * which shouldn't be affected by G0/G1 switching, etc.
676 * If the user map still contains default values, i.e. the
677 * direct-to-font mapping, then assume user is using Latin1.
678 */
679/* may be called during an interrupt */
680u32 conv_8bit_to_uni(unsigned char c)
681{
682 unsigned short uni = translations[USER_MAP][c];
683 return uni == (0xf000 | c) ? c : uni;
684}
685
686int conv_uni_to_8bit(u32 uni)
687{
688 int c;
689 for (c = 0; c < 0x100; c++)
690 if (translations[USER_MAP][c] == uni ||
691 (translations[USER_MAP][c] == (c | 0xf000) && uni == c))
692 return c;
693 return -1;
694}
695
696int
697conv_uni_to_pc(struct vc_data *conp, long ucs)
698{
699 int h;
700 u16 **p1, *p2;
701 struct uni_pagedir *p;
702
703 /* Only 16-bit codes supported at this time */
704 if (ucs > 0xffff)
705 return -4; /* Not found */
706 else if (ucs < 0x20)
707 return -1; /* Not a printable character */
708 else if (ucs == 0xfeff || (ucs >= 0x200b && ucs <= 0x200f))
709 return -2; /* Zero-width space */
710 /*
711 * UNI_DIRECT_BASE indicates the start of the region in the User Zone
712 * which always has a 1:1 mapping to the currently loaded font. The
713 * UNI_DIRECT_MASK indicates the bit span of the region.
714 */
715 else if ((ucs & ~UNI_DIRECT_MASK) == UNI_DIRECT_BASE)
716 return ucs & UNI_DIRECT_MASK;
717
718 if (!*conp->vc_uni_pagedir_loc)
719 return -3;
720
721 p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc;
722 if ((p1 = p->uni_pgdir[ucs >> 11]) &&
723 (p2 = p1[(ucs >> 6) & 0x1f]) &&
724 (h = p2[ucs & 0x3f]) < MAX_GLYPH)
725 return h;
726
727 return -4; /* not found */
728}
729
730/*
731 * This is called at sys_setup time, after memory and the console are
732 * initialized. It must be possible to call kmalloc(..., GFP_KERNEL)
733 * from this function, hence the call from sys_setup.
734 */
735void __init
736console_map_init(void)
737{
738 int i;
739
740 for (i = 0; i < MAX_NR_CONSOLES; i++)
741 if (vc_cons_allocated(i) && !*vc_cons[i].d->vc_uni_pagedir_loc)
742 con_set_default_unimap(vc_cons[i].d);
743}
744
745EXPORT_SYMBOL(con_copy_unimap);
diff --git a/drivers/tty/vt/cp437.uni b/drivers/tty/vt/cp437.uni
new file mode 100644
index 000000000000..bc6163484f62
--- /dev/null
+++ b/drivers/tty/vt/cp437.uni
@@ -0,0 +1,291 @@
1#
2# Unicode table for IBM Codepage 437. Note that there are many more
3# substitutions that could be conceived (for example, thick-line
4# graphs probably should be replaced with double-line ones, accented
5# Latin characters should replaced with their nonaccented versions,
6# and some upper case Greek characters could be replaced by Latin), however,
7# I have limited myself to the Unicodes used by the kernel ISO 8859-1,
8# DEC VT, and IBM CP 437 tables.
9#
10# --------------------------------
11#
12# Basic IBM dingbats, some of which will never have a purpose clear
13# to mankind
14#
150x00 U+0000
160x01 U+263a
170x02 U+263b
180x03 U+2665
190x04 U+2666 U+25c6
200x05 U+2663
210x06 U+2660
220x07 U+2022
230x08 U+25d8
240x09 U+25cb
250x0a U+25d9
260x0b U+2642
270x0c U+2640
280x0d U+266a
290x0e U+266b
300x0f U+263c U+00a4
310x10 U+25b6 U+25ba
320x11 U+25c0 U+25c4
330x12 U+2195
340x13 U+203c
350x14 U+00b6
360x15 U+00a7
370x16 U+25ac
380x17 U+21a8
390x18 U+2191
400x19 U+2193
410x1a U+2192
420x1b U+2190
430x1c U+221f
440x1d U+2194
450x1e U+25b2
460x1f U+25bc
47#
48# The ASCII range is identity-mapped, but some of the characters also
49# have to act as substitutes, especially the upper-case characters.
50#
510x20 U+0020
520x21 U+0021
530x22 U+0022 U+00a8
540x23 U+0023
550x24 U+0024
560x25 U+0025
570x26 U+0026
580x27 U+0027 U+00b4
590x28 U+0028
600x29 U+0029
610x2a U+002a
620x2b U+002b
630x2c U+002c U+00b8
640x2d U+002d U+00ad
650x2e U+002e
660x2f U+002f
670x30 U+0030
680x31 U+0031
690x32 U+0032
700x33 U+0033
710x34 U+0034
720x35 U+0035
730x36 U+0036
740x37 U+0037
750x38 U+0038
760x39 U+0039
770x3a U+003a
780x3b U+003b
790x3c U+003c
800x3d U+003d
810x3e U+003e
820x3f U+003f
830x40 U+0040
840x41 U+0041 U+00c0 U+00c1 U+00c2 U+00c3
850x42 U+0042
860x43 U+0043 U+00a9
870x44 U+0044 U+00d0
880x45 U+0045 U+00c8 U+00ca U+00cb
890x46 U+0046
900x47 U+0047
910x48 U+0048
920x49 U+0049 U+00cc U+00cd U+00ce U+00cf
930x4a U+004a
940x4b U+004b U+212a
950x4c U+004c
960x4d U+004d
970x4e U+004e
980x4f U+004f U+00d2 U+00d3 U+00d4 U+00d5
990x50 U+0050
1000x51 U+0051
1010x52 U+0052 U+00ae
1020x53 U+0053
1030x54 U+0054
1040x55 U+0055 U+00d9 U+00da U+00db
1050x56 U+0056
1060x57 U+0057
1070x58 U+0058
1080x59 U+0059 U+00dd
1090x5a U+005a
1100x5b U+005b
1110x5c U+005c
1120x5d U+005d
1130x5e U+005e
1140x5f U+005f U+23bd U+f804
1150x60 U+0060
1160x61 U+0061 U+00e3
1170x62 U+0062
1180x63 U+0063
1190x64 U+0064
1200x65 U+0065
1210x66 U+0066
1220x67 U+0067
1230x68 U+0068
1240x69 U+0069
1250x6a U+006a
1260x6b U+006b
1270x6c U+006c
1280x6d U+006d
1290x6e U+006e
1300x6f U+006f U+00f5
1310x70 U+0070
1320x71 U+0071
1330x72 U+0072
1340x73 U+0073
1350x74 U+0074
1360x75 U+0075
1370x76 U+0076
1380x77 U+0077
1390x78 U+0078 U+00d7
1400x79 U+0079 U+00fd
1410x7a U+007a
1420x7b U+007b
1430x7c U+007c U+00a6
1440x7d U+007d
1450x7e U+007e
146#
147# Okay, what on Earth is this one supposed to be used for?
148#
1490x7f U+2302
150#
151# Non-English characters, mostly lower case letters...
152#
1530x80 U+00c7
1540x81 U+00fc
1550x82 U+00e9
1560x83 U+00e2
1570x84 U+00e4
1580x85 U+00e0
1590x86 U+00e5
1600x87 U+00e7
1610x88 U+00ea
1620x89 U+00eb
1630x8a U+00e8
1640x8b U+00ef
1650x8c U+00ee
1660x8d U+00ec
1670x8e U+00c4
1680x8f U+00c5 U+212b
1690x90 U+00c9
1700x91 U+00e6
1710x92 U+00c6
1720x93 U+00f4
1730x94 U+00f6
1740x95 U+00f2
1750x96 U+00fb
1760x97 U+00f9
1770x98 U+00ff
1780x99 U+00d6
1790x9a U+00dc
1800x9b U+00a2
1810x9c U+00a3
1820x9d U+00a5
1830x9e U+20a7
1840x9f U+0192
1850xa0 U+00e1
1860xa1 U+00ed
1870xa2 U+00f3
1880xa3 U+00fa
1890xa4 U+00f1
1900xa5 U+00d1
1910xa6 U+00aa
1920xa7 U+00ba
1930xa8 U+00bf
1940xa9 U+2310
1950xaa U+00ac
1960xab U+00bd
1970xac U+00bc
1980xad U+00a1
1990xae U+00ab
2000xaf U+00bb
201#
202# Block graphics
203#
2040xb0 U+2591
2050xb1 U+2592
2060xb2 U+2593
2070xb3 U+2502
2080xb4 U+2524
2090xb5 U+2561
2100xb6 U+2562
2110xb7 U+2556
2120xb8 U+2555
2130xb9 U+2563
2140xba U+2551
2150xbb U+2557
2160xbc U+255d
2170xbd U+255c
2180xbe U+255b
2190xbf U+2510
2200xc0 U+2514
2210xc1 U+2534
2220xc2 U+252c
2230xc3 U+251c
2240xc4 U+2500
2250xc5 U+253c
2260xc6 U+255e
2270xc7 U+255f
2280xc8 U+255a
2290xc9 U+2554
2300xca U+2569
2310xcb U+2566
2320xcc U+2560
2330xcd U+2550
2340xce U+256c
2350xcf U+2567
2360xd0 U+2568
2370xd1 U+2564
2380xd2 U+2565
2390xd3 U+2559
2400xd4 U+2558
2410xd5 U+2552
2420xd6 U+2553
2430xd7 U+256b
2440xd8 U+256a
2450xd9 U+2518
2460xda U+250c
2470xdb U+2588
2480xdc U+2584
2490xdd U+258c
2500xde U+2590
2510xdf U+2580
252#
253# Greek letters and mathematical symbols
254#
2550xe0 U+03b1
2560xe1 U+03b2 U+00df
2570xe2 U+0393
2580xe3 U+03c0
2590xe4 U+03a3
2600xe5 U+03c3
2610xe6 U+00b5 U+03bc
2620xe7 U+03c4
2630xe8 U+03a6 U+00d8
2640xe9 U+0398
2650xea U+03a9 U+2126
2660xeb U+03b4 U+00f0
2670xec U+221e
2680xed U+03c6 U+00f8
2690xee U+03b5 U+2208
2700xef U+2229
2710xf0 U+2261
2720xf1 U+00b1
2730xf2 U+2265
2740xf3 U+2264
2750xf4 U+2320
2760xf5 U+2321
2770xf6 U+00f7
2780xf7 U+2248
2790xf8 U+00b0
2800xf9 U+2219
2810xfa U+00b7
2820xfb U+221a
2830xfc U+207f
2840xfd U+00b2
285#
286# Square bullet, non-spacing blank
287# Mapping U+fffd to the square bullet means it is the substitution
288# character
289#
2900xfe U+25a0 U+fffd
2910xff U+00a0
diff --git a/drivers/tty/vt/defkeymap.c_shipped b/drivers/tty/vt/defkeymap.c_shipped
new file mode 100644
index 000000000000..d2208dfe3f67
--- /dev/null
+++ b/drivers/tty/vt/defkeymap.c_shipped
@@ -0,0 +1,262 @@
1/* Do not edit this file! It was automatically generated by */
2/* loadkeys --mktable defkeymap.map > defkeymap.c */
3
4#include <linux/types.h>
5#include <linux/keyboard.h>
6#include <linux/kd.h>
7
8u_short plain_map[NR_KEYS] = {
9 0xf200, 0xf01b, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036,
10 0xf037, 0xf038, 0xf039, 0xf030, 0xf02d, 0xf03d, 0xf07f, 0xf009,
11 0xfb71, 0xfb77, 0xfb65, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69,
12 0xfb6f, 0xfb70, 0xf05b, 0xf05d, 0xf201, 0xf702, 0xfb61, 0xfb73,
13 0xfb64, 0xfb66, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf03b,
14 0xf027, 0xf060, 0xf700, 0xf05c, 0xfb7a, 0xfb78, 0xfb63, 0xfb76,
15 0xfb62, 0xfb6e, 0xfb6d, 0xf02c, 0xf02e, 0xf02f, 0xf700, 0xf30c,
16 0xf703, 0xf020, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
17 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf209, 0xf307,
18 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
19 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03c, 0xf10a,
20 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
21 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
22 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
23 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
24 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
25};
26
27u_short shift_map[NR_KEYS] = {
28 0xf200, 0xf01b, 0xf021, 0xf040, 0xf023, 0xf024, 0xf025, 0xf05e,
29 0xf026, 0xf02a, 0xf028, 0xf029, 0xf05f, 0xf02b, 0xf07f, 0xf009,
30 0xfb51, 0xfb57, 0xfb45, 0xfb52, 0xfb54, 0xfb59, 0xfb55, 0xfb49,
31 0xfb4f, 0xfb50, 0xf07b, 0xf07d, 0xf201, 0xf702, 0xfb41, 0xfb53,
32 0xfb44, 0xfb46, 0xfb47, 0xfb48, 0xfb4a, 0xfb4b, 0xfb4c, 0xf03a,
33 0xf022, 0xf07e, 0xf700, 0xf07c, 0xfb5a, 0xfb58, 0xfb43, 0xfb56,
34 0xfb42, 0xfb4e, 0xfb4d, 0xf03c, 0xf03e, 0xf03f, 0xf700, 0xf30c,
35 0xf703, 0xf020, 0xf207, 0xf10a, 0xf10b, 0xf10c, 0xf10d, 0xf10e,
36 0xf10f, 0xf110, 0xf111, 0xf112, 0xf113, 0xf213, 0xf203, 0xf307,
37 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
38 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03e, 0xf10a,
39 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
40 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
41 0xf20b, 0xf601, 0xf602, 0xf117, 0xf600, 0xf20a, 0xf115, 0xf116,
42 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
43 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
44};
45
46u_short altgr_map[NR_KEYS] = {
47 0xf200, 0xf200, 0xf200, 0xf040, 0xf200, 0xf024, 0xf200, 0xf200,
48 0xf07b, 0xf05b, 0xf05d, 0xf07d, 0xf05c, 0xf200, 0xf200, 0xf200,
49 0xfb71, 0xfb77, 0xf918, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69,
50 0xfb6f, 0xfb70, 0xf200, 0xf07e, 0xf201, 0xf702, 0xf914, 0xfb73,
51 0xf917, 0xf919, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf200,
52 0xf200, 0xf200, 0xf700, 0xf200, 0xfb7a, 0xfb78, 0xf916, 0xfb76,
53 0xf915, 0xfb6e, 0xfb6d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
54 0xf703, 0xf200, 0xf207, 0xf50c, 0xf50d, 0xf50e, 0xf50f, 0xf510,
55 0xf511, 0xf512, 0xf513, 0xf514, 0xf515, 0xf208, 0xf202, 0xf911,
56 0xf912, 0xf913, 0xf30b, 0xf90e, 0xf90f, 0xf910, 0xf30a, 0xf90b,
57 0xf90c, 0xf90d, 0xf90a, 0xf310, 0xf206, 0xf200, 0xf07c, 0xf516,
58 0xf517, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
59 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
60 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
61 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
62 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
63};
64
65u_short ctrl_map[NR_KEYS] = {
66 0xf200, 0xf200, 0xf200, 0xf000, 0xf01b, 0xf01c, 0xf01d, 0xf01e,
67 0xf01f, 0xf07f, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf008, 0xf200,
68 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
69 0xf00f, 0xf010, 0xf01b, 0xf01d, 0xf201, 0xf702, 0xf001, 0xf013,
70 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
71 0xf007, 0xf000, 0xf700, 0xf01c, 0xf01a, 0xf018, 0xf003, 0xf016,
72 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf20e, 0xf07f, 0xf700, 0xf30c,
73 0xf703, 0xf000, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
74 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf204, 0xf307,
75 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
76 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf10a,
77 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
78 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
79 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
80 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
81 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
82};
83
84u_short shift_ctrl_map[NR_KEYS] = {
85 0xf200, 0xf200, 0xf200, 0xf000, 0xf200, 0xf200, 0xf200, 0xf200,
86 0xf200, 0xf200, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf200, 0xf200,
87 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
88 0xf00f, 0xf010, 0xf200, 0xf200, 0xf201, 0xf702, 0xf001, 0xf013,
89 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
90 0xf200, 0xf200, 0xf700, 0xf200, 0xf01a, 0xf018, 0xf003, 0xf016,
91 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
92 0xf703, 0xf200, 0xf207, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
93 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf208, 0xf200, 0xf307,
94 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
95 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf200,
96 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
97 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
98 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
99 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
100 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
101};
102
103u_short alt_map[NR_KEYS] = {
104 0xf200, 0xf81b, 0xf831, 0xf832, 0xf833, 0xf834, 0xf835, 0xf836,
105 0xf837, 0xf838, 0xf839, 0xf830, 0xf82d, 0xf83d, 0xf87f, 0xf809,
106 0xf871, 0xf877, 0xf865, 0xf872, 0xf874, 0xf879, 0xf875, 0xf869,
107 0xf86f, 0xf870, 0xf85b, 0xf85d, 0xf80d, 0xf702, 0xf861, 0xf873,
108 0xf864, 0xf866, 0xf867, 0xf868, 0xf86a, 0xf86b, 0xf86c, 0xf83b,
109 0xf827, 0xf860, 0xf700, 0xf85c, 0xf87a, 0xf878, 0xf863, 0xf876,
110 0xf862, 0xf86e, 0xf86d, 0xf82c, 0xf82e, 0xf82f, 0xf700, 0xf30c,
111 0xf703, 0xf820, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504,
112 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf209, 0xf907,
113 0xf908, 0xf909, 0xf30b, 0xf904, 0xf905, 0xf906, 0xf30a, 0xf901,
114 0xf902, 0xf903, 0xf900, 0xf310, 0xf206, 0xf200, 0xf83c, 0xf50a,
115 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
116 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
117 0xf118, 0xf210, 0xf211, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
118 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
119 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
120};
121
122u_short ctrl_alt_map[NR_KEYS] = {
123 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
124 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
125 0xf811, 0xf817, 0xf805, 0xf812, 0xf814, 0xf819, 0xf815, 0xf809,
126 0xf80f, 0xf810, 0xf200, 0xf200, 0xf201, 0xf702, 0xf801, 0xf813,
127 0xf804, 0xf806, 0xf807, 0xf808, 0xf80a, 0xf80b, 0xf80c, 0xf200,
128 0xf200, 0xf200, 0xf700, 0xf200, 0xf81a, 0xf818, 0xf803, 0xf816,
129 0xf802, 0xf80e, 0xf80d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
130 0xf703, 0xf200, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504,
131 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf200, 0xf307,
132 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
133 0xf302, 0xf303, 0xf300, 0xf20c, 0xf206, 0xf200, 0xf200, 0xf50a,
134 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
135 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
136 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf20c,
137 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
138 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
139};
140
141ushort *key_maps[MAX_NR_KEYMAPS] = {
142 plain_map, shift_map, altgr_map, NULL,
143 ctrl_map, shift_ctrl_map, NULL, NULL,
144 alt_map, NULL, NULL, NULL,
145 ctrl_alt_map, NULL
146};
147
148unsigned int keymap_count = 7;
149
150/*
151 * Philosophy: most people do not define more strings, but they who do
152 * often want quite a lot of string space. So, we statically allocate
153 * the default and allocate dynamically in chunks of 512 bytes.
154 */
155
156char func_buf[] = {
157 '\033', '[', '[', 'A', 0,
158 '\033', '[', '[', 'B', 0,
159 '\033', '[', '[', 'C', 0,
160 '\033', '[', '[', 'D', 0,
161 '\033', '[', '[', 'E', 0,
162 '\033', '[', '1', '7', '~', 0,
163 '\033', '[', '1', '8', '~', 0,
164 '\033', '[', '1', '9', '~', 0,
165 '\033', '[', '2', '0', '~', 0,
166 '\033', '[', '2', '1', '~', 0,
167 '\033', '[', '2', '3', '~', 0,
168 '\033', '[', '2', '4', '~', 0,
169 '\033', '[', '2', '5', '~', 0,
170 '\033', '[', '2', '6', '~', 0,
171 '\033', '[', '2', '8', '~', 0,
172 '\033', '[', '2', '9', '~', 0,
173 '\033', '[', '3', '1', '~', 0,
174 '\033', '[', '3', '2', '~', 0,
175 '\033', '[', '3', '3', '~', 0,
176 '\033', '[', '3', '4', '~', 0,
177 '\033', '[', '1', '~', 0,
178 '\033', '[', '2', '~', 0,
179 '\033', '[', '3', '~', 0,
180 '\033', '[', '4', '~', 0,
181 '\033', '[', '5', '~', 0,
182 '\033', '[', '6', '~', 0,
183 '\033', '[', 'M', 0,
184 '\033', '[', 'P', 0,
185};
186
187char *funcbufptr = func_buf;
188int funcbufsize = sizeof(func_buf);
189int funcbufleft = 0; /* space left */
190
191char *func_table[MAX_NR_FUNC] = {
192 func_buf + 0,
193 func_buf + 5,
194 func_buf + 10,
195 func_buf + 15,
196 func_buf + 20,
197 func_buf + 25,
198 func_buf + 31,
199 func_buf + 37,
200 func_buf + 43,
201 func_buf + 49,
202 func_buf + 55,
203 func_buf + 61,
204 func_buf + 67,
205 func_buf + 73,
206 func_buf + 79,
207 func_buf + 85,
208 func_buf + 91,
209 func_buf + 97,
210 func_buf + 103,
211 func_buf + 109,
212 func_buf + 115,
213 func_buf + 120,
214 func_buf + 125,
215 func_buf + 130,
216 func_buf + 135,
217 func_buf + 140,
218 func_buf + 145,
219 NULL,
220 NULL,
221 func_buf + 149,
222 NULL,
223};
224
225struct kbdiacruc accent_table[MAX_DIACR] = {
226 {'`', 'A', 0300}, {'`', 'a', 0340},
227 {'\'', 'A', 0301}, {'\'', 'a', 0341},
228 {'^', 'A', 0302}, {'^', 'a', 0342},
229 {'~', 'A', 0303}, {'~', 'a', 0343},
230 {'"', 'A', 0304}, {'"', 'a', 0344},
231 {'O', 'A', 0305}, {'o', 'a', 0345},
232 {'0', 'A', 0305}, {'0', 'a', 0345},
233 {'A', 'A', 0305}, {'a', 'a', 0345},
234 {'A', 'E', 0306}, {'a', 'e', 0346},
235 {',', 'C', 0307}, {',', 'c', 0347},
236 {'`', 'E', 0310}, {'`', 'e', 0350},
237 {'\'', 'E', 0311}, {'\'', 'e', 0351},
238 {'^', 'E', 0312}, {'^', 'e', 0352},
239 {'"', 'E', 0313}, {'"', 'e', 0353},
240 {'`', 'I', 0314}, {'`', 'i', 0354},
241 {'\'', 'I', 0315}, {'\'', 'i', 0355},
242 {'^', 'I', 0316}, {'^', 'i', 0356},
243 {'"', 'I', 0317}, {'"', 'i', 0357},
244 {'-', 'D', 0320}, {'-', 'd', 0360},
245 {'~', 'N', 0321}, {'~', 'n', 0361},
246 {'`', 'O', 0322}, {'`', 'o', 0362},
247 {'\'', 'O', 0323}, {'\'', 'o', 0363},
248 {'^', 'O', 0324}, {'^', 'o', 0364},
249 {'~', 'O', 0325}, {'~', 'o', 0365},
250 {'"', 'O', 0326}, {'"', 'o', 0366},
251 {'/', 'O', 0330}, {'/', 'o', 0370},
252 {'`', 'U', 0331}, {'`', 'u', 0371},
253 {'\'', 'U', 0332}, {'\'', 'u', 0372},
254 {'^', 'U', 0333}, {'^', 'u', 0373},
255 {'"', 'U', 0334}, {'"', 'u', 0374},
256 {'\'', 'Y', 0335}, {'\'', 'y', 0375},
257 {'T', 'H', 0336}, {'t', 'h', 0376},
258 {'s', 's', 0337}, {'"', 'y', 0377},
259 {'s', 'z', 0337}, {'i', 'j', 0377},
260};
261
262unsigned int accent_table_size = 68;
diff --git a/drivers/tty/vt/defkeymap.map b/drivers/tty/vt/defkeymap.map
new file mode 100644
index 000000000000..50b30cace261
--- /dev/null
+++ b/drivers/tty/vt/defkeymap.map
@@ -0,0 +1,357 @@
1# Default kernel keymap. This uses 7 modifier combinations.
2keymaps 0-2,4-5,8,12
3# Change the above line into
4# keymaps 0-2,4-6,8,12
5# in case you want the entries
6# altgr control keycode 83 = Boot
7# altgr control keycode 111 = Boot
8# below.
9#
10# In fact AltGr is used very little, and one more keymap can
11# be saved by mapping AltGr to Alt (and adapting a few entries):
12# keycode 100 = Alt
13#
14keycode 1 = Escape Escape
15 alt keycode 1 = Meta_Escape
16keycode 2 = one exclam
17 alt keycode 2 = Meta_one
18keycode 3 = two at at
19 control keycode 3 = nul
20 shift control keycode 3 = nul
21 alt keycode 3 = Meta_two
22keycode 4 = three numbersign
23 control keycode 4 = Escape
24 alt keycode 4 = Meta_three
25keycode 5 = four dollar dollar
26 control keycode 5 = Control_backslash
27 alt keycode 5 = Meta_four
28keycode 6 = five percent
29 control keycode 6 = Control_bracketright
30 alt keycode 6 = Meta_five
31keycode 7 = six asciicircum
32 control keycode 7 = Control_asciicircum
33 alt keycode 7 = Meta_six
34keycode 8 = seven ampersand braceleft
35 control keycode 8 = Control_underscore
36 alt keycode 8 = Meta_seven
37keycode 9 = eight asterisk bracketleft
38 control keycode 9 = Delete
39 alt keycode 9 = Meta_eight
40keycode 10 = nine parenleft bracketright
41 alt keycode 10 = Meta_nine
42keycode 11 = zero parenright braceright
43 alt keycode 11 = Meta_zero
44keycode 12 = minus underscore backslash
45 control keycode 12 = Control_underscore
46 shift control keycode 12 = Control_underscore
47 alt keycode 12 = Meta_minus
48keycode 13 = equal plus
49 alt keycode 13 = Meta_equal
50keycode 14 = Delete Delete
51 control keycode 14 = BackSpace
52 alt keycode 14 = Meta_Delete
53keycode 15 = Tab Tab
54 alt keycode 15 = Meta_Tab
55keycode 16 = q
56keycode 17 = w
57keycode 18 = e
58 altgr keycode 18 = Hex_E
59keycode 19 = r
60keycode 20 = t
61keycode 21 = y
62keycode 22 = u
63keycode 23 = i
64keycode 24 = o
65keycode 25 = p
66keycode 26 = bracketleft braceleft
67 control keycode 26 = Escape
68 alt keycode 26 = Meta_bracketleft
69keycode 27 = bracketright braceright asciitilde
70 control keycode 27 = Control_bracketright
71 alt keycode 27 = Meta_bracketright
72keycode 28 = Return
73 alt keycode 28 = Meta_Control_m
74keycode 29 = Control
75keycode 30 = a
76 altgr keycode 30 = Hex_A
77keycode 31 = s
78keycode 32 = d
79 altgr keycode 32 = Hex_D
80keycode 33 = f
81 altgr keycode 33 = Hex_F
82keycode 34 = g
83keycode 35 = h
84keycode 36 = j
85keycode 37 = k
86keycode 38 = l
87keycode 39 = semicolon colon
88 alt keycode 39 = Meta_semicolon
89keycode 40 = apostrophe quotedbl
90 control keycode 40 = Control_g
91 alt keycode 40 = Meta_apostrophe
92keycode 41 = grave asciitilde
93 control keycode 41 = nul
94 alt keycode 41 = Meta_grave
95keycode 42 = Shift
96keycode 43 = backslash bar
97 control keycode 43 = Control_backslash
98 alt keycode 43 = Meta_backslash
99keycode 44 = z
100keycode 45 = x
101keycode 46 = c
102 altgr keycode 46 = Hex_C
103keycode 47 = v
104keycode 48 = b
105 altgr keycode 48 = Hex_B
106keycode 49 = n
107keycode 50 = m
108keycode 51 = comma less
109 alt keycode 51 = Meta_comma
110keycode 52 = period greater
111 control keycode 52 = Compose
112 alt keycode 52 = Meta_period
113keycode 53 = slash question
114 control keycode 53 = Delete
115 alt keycode 53 = Meta_slash
116keycode 54 = Shift
117keycode 55 = KP_Multiply
118keycode 56 = Alt
119keycode 57 = space space
120 control keycode 57 = nul
121 alt keycode 57 = Meta_space
122keycode 58 = Caps_Lock
123keycode 59 = F1 F11 Console_13
124 control keycode 59 = F1
125 alt keycode 59 = Console_1
126 control alt keycode 59 = Console_1
127keycode 60 = F2 F12 Console_14
128 control keycode 60 = F2
129 alt keycode 60 = Console_2
130 control alt keycode 60 = Console_2
131keycode 61 = F3 F13 Console_15
132 control keycode 61 = F3
133 alt keycode 61 = Console_3
134 control alt keycode 61 = Console_3
135keycode 62 = F4 F14 Console_16
136 control keycode 62 = F4
137 alt keycode 62 = Console_4
138 control alt keycode 62 = Console_4
139keycode 63 = F5 F15 Console_17
140 control keycode 63 = F5
141 alt keycode 63 = Console_5
142 control alt keycode 63 = Console_5
143keycode 64 = F6 F16 Console_18
144 control keycode 64 = F6
145 alt keycode 64 = Console_6
146 control alt keycode 64 = Console_6
147keycode 65 = F7 F17 Console_19
148 control keycode 65 = F7
149 alt keycode 65 = Console_7
150 control alt keycode 65 = Console_7
151keycode 66 = F8 F18 Console_20
152 control keycode 66 = F8
153 alt keycode 66 = Console_8
154 control alt keycode 66 = Console_8
155keycode 67 = F9 F19 Console_21
156 control keycode 67 = F9
157 alt keycode 67 = Console_9
158 control alt keycode 67 = Console_9
159keycode 68 = F10 F20 Console_22
160 control keycode 68 = F10
161 alt keycode 68 = Console_10
162 control alt keycode 68 = Console_10
163keycode 69 = Num_Lock
164 shift keycode 69 = Bare_Num_Lock
165keycode 70 = Scroll_Lock Show_Memory Show_Registers
166 control keycode 70 = Show_State
167 alt keycode 70 = Scroll_Lock
168keycode 71 = KP_7
169 alt keycode 71 = Ascii_7
170 altgr keycode 71 = Hex_7
171keycode 72 = KP_8
172 alt keycode 72 = Ascii_8
173 altgr keycode 72 = Hex_8
174keycode 73 = KP_9
175 alt keycode 73 = Ascii_9
176 altgr keycode 73 = Hex_9
177keycode 74 = KP_Subtract
178keycode 75 = KP_4
179 alt keycode 75 = Ascii_4
180 altgr keycode 75 = Hex_4
181keycode 76 = KP_5
182 alt keycode 76 = Ascii_5
183 altgr keycode 76 = Hex_5
184keycode 77 = KP_6
185 alt keycode 77 = Ascii_6
186 altgr keycode 77 = Hex_6
187keycode 78 = KP_Add
188keycode 79 = KP_1
189 alt keycode 79 = Ascii_1
190 altgr keycode 79 = Hex_1
191keycode 80 = KP_2
192 alt keycode 80 = Ascii_2
193 altgr keycode 80 = Hex_2
194keycode 81 = KP_3
195 alt keycode 81 = Ascii_3
196 altgr keycode 81 = Hex_3
197keycode 82 = KP_0
198 alt keycode 82 = Ascii_0
199 altgr keycode 82 = Hex_0
200keycode 83 = KP_Period
201# altgr control keycode 83 = Boot
202 control alt keycode 83 = Boot
203keycode 84 = Last_Console
204keycode 85 =
205keycode 86 = less greater bar
206 alt keycode 86 = Meta_less
207keycode 87 = F11 F11 Console_23
208 control keycode 87 = F11
209 alt keycode 87 = Console_11
210 control alt keycode 87 = Console_11
211keycode 88 = F12 F12 Console_24
212 control keycode 88 = F12
213 alt keycode 88 = Console_12
214 control alt keycode 88 = Console_12
215keycode 89 =
216keycode 90 =
217keycode 91 =
218keycode 92 =
219keycode 93 =
220keycode 94 =
221keycode 95 =
222keycode 96 = KP_Enter
223keycode 97 = Control
224keycode 98 = KP_Divide
225keycode 99 = Control_backslash
226 control keycode 99 = Control_backslash
227 alt keycode 99 = Control_backslash
228keycode 100 = AltGr
229keycode 101 = Break
230keycode 102 = Find
231keycode 103 = Up
232keycode 104 = Prior
233 shift keycode 104 = Scroll_Backward
234keycode 105 = Left
235 alt keycode 105 = Decr_Console
236keycode 106 = Right
237 alt keycode 106 = Incr_Console
238keycode 107 = Select
239keycode 108 = Down
240keycode 109 = Next
241 shift keycode 109 = Scroll_Forward
242keycode 110 = Insert
243keycode 111 = Remove
244# altgr control keycode 111 = Boot
245 control alt keycode 111 = Boot
246keycode 112 = Macro
247keycode 113 = F13
248keycode 114 = F14
249keycode 115 = Help
250keycode 116 = Do
251keycode 117 = F17
252keycode 118 = KP_MinPlus
253keycode 119 = Pause
254keycode 120 =
255keycode 121 =
256keycode 122 =
257keycode 123 =
258keycode 124 =
259keycode 125 =
260keycode 126 =
261keycode 127 =
262string F1 = "\033[[A"
263string F2 = "\033[[B"
264string F3 = "\033[[C"
265string F4 = "\033[[D"
266string F5 = "\033[[E"
267string F6 = "\033[17~"
268string F7 = "\033[18~"
269string F8 = "\033[19~"
270string F9 = "\033[20~"
271string F10 = "\033[21~"
272string F11 = "\033[23~"
273string F12 = "\033[24~"
274string F13 = "\033[25~"
275string F14 = "\033[26~"
276string F15 = "\033[28~"
277string F16 = "\033[29~"
278string F17 = "\033[31~"
279string F18 = "\033[32~"
280string F19 = "\033[33~"
281string F20 = "\033[34~"
282string Find = "\033[1~"
283string Insert = "\033[2~"
284string Remove = "\033[3~"
285string Select = "\033[4~"
286string Prior = "\033[5~"
287string Next = "\033[6~"
288string Macro = "\033[M"
289string Pause = "\033[P"
290compose '`' 'A' to 'À'
291compose '`' 'a' to 'à'
292compose '\'' 'A' to 'Á'
293compose '\'' 'a' to 'á'
294compose '^' 'A' to 'Â'
295compose '^' 'a' to 'â'
296compose '~' 'A' to 'Ã'
297compose '~' 'a' to 'ã'
298compose '"' 'A' to 'Ä'
299compose '"' 'a' to 'ä'
300compose 'O' 'A' to 'Å'
301compose 'o' 'a' to 'å'
302compose '0' 'A' to 'Å'
303compose '0' 'a' to 'å'
304compose 'A' 'A' to 'Å'
305compose 'a' 'a' to 'å'
306compose 'A' 'E' to 'Æ'
307compose 'a' 'e' to 'æ'
308compose ',' 'C' to 'Ç'
309compose ',' 'c' to 'ç'
310compose '`' 'E' to 'È'
311compose '`' 'e' to 'è'
312compose '\'' 'E' to 'É'
313compose '\'' 'e' to 'é'
314compose '^' 'E' to 'Ê'
315compose '^' 'e' to 'ê'
316compose '"' 'E' to 'Ë'
317compose '"' 'e' to 'ë'
318compose '`' 'I' to 'Ì'
319compose '`' 'i' to 'ì'
320compose '\'' 'I' to 'Í'
321compose '\'' 'i' to 'í'
322compose '^' 'I' to 'Î'
323compose '^' 'i' to 'î'
324compose '"' 'I' to 'Ï'
325compose '"' 'i' to 'ï'
326compose '-' 'D' to 'Ð'
327compose '-' 'd' to 'ð'
328compose '~' 'N' to 'Ñ'
329compose '~' 'n' to 'ñ'
330compose '`' 'O' to 'Ò'
331compose '`' 'o' to 'ò'
332compose '\'' 'O' to 'Ó'
333compose '\'' 'o' to 'ó'
334compose '^' 'O' to 'Ô'
335compose '^' 'o' to 'ô'
336compose '~' 'O' to 'Õ'
337compose '~' 'o' to 'õ'
338compose '"' 'O' to 'Ö'
339compose '"' 'o' to 'ö'
340compose '/' 'O' to 'Ø'
341compose '/' 'o' to 'ø'
342compose '`' 'U' to 'Ù'
343compose '`' 'u' to 'ù'
344compose '\'' 'U' to 'Ú'
345compose '\'' 'u' to 'ú'
346compose '^' 'U' to 'Û'
347compose '^' 'u' to 'û'
348compose '"' 'U' to 'Ü'
349compose '"' 'u' to 'ü'
350compose '\'' 'Y' to 'Ý'
351compose '\'' 'y' to 'ý'
352compose 'T' 'H' to 'Þ'
353compose 't' 'h' to 'þ'
354compose 's' 's' to 'ß'
355compose '"' 'y' to 'ÿ'
356compose 's' 'z' to 'ß'
357compose 'i' 'j' to 'ÿ'
diff --git a/drivers/tty/vt/keyboard.c b/drivers/tty/vt/keyboard.c
new file mode 100644
index 000000000000..3761ccf0f340
--- /dev/null
+++ b/drivers/tty/vt/keyboard.c
@@ -0,0 +1,1453 @@
1/*
2 * Written for linux by Johan Myreen as a translation from
3 * the assembly version by Linus (with diacriticals added)
4 *
5 * Some additional features added by Christoph Niemann (ChN), March 1993
6 *
7 * Loadable keymaps by Risto Kankkunen, May 1993
8 *
9 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
10 * Added decr/incr_console, dynamic keymaps, Unicode support,
11 * dynamic function/string keys, led setting, Sept 1994
12 * `Sticky' modifier keys, 951006.
13 *
14 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
15 *
16 * Modified to provide 'generic' keyboard support by Hamish Macdonald
17 * Merge with the m68k keyboard driver and split-off of the PC low-level
18 * parts by Geert Uytterhoeven, May 1997
19 *
20 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
21 * 30-07-98: Dead keys redone, aeb@cwi.nl.
22 * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
23 */
24
25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27#include <linux/consolemap.h>
28#include <linux/module.h>
29#include <linux/sched.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/mm.h>
33#include <linux/string.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/irq.h>
37
38#include <linux/kbd_kern.h>
39#include <linux/kbd_diacr.h>
40#include <linux/vt_kern.h>
41#include <linux/input.h>
42#include <linux/reboot.h>
43#include <linux/notifier.h>
44#include <linux/jiffies.h>
45
46extern void ctrl_alt_del(void);
47
48/*
49 * Exported functions/variables
50 */
51
52#define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
53
54/*
55 * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
56 * This seems a good reason to start with NumLock off. On HIL keyboards
57 * of PARISC machines however there is no NumLock key and everyone expects the keypad
58 * to be used for numbers.
59 */
60
61#if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
62#define KBD_DEFLEDS (1 << VC_NUMLOCK)
63#else
64#define KBD_DEFLEDS 0
65#endif
66
67#define KBD_DEFLOCK 0
68
69void compute_shiftstate(void);
70
71/*
72 * Handler Tables.
73 */
74
75#define K_HANDLERS\
76 k_self, k_fn, k_spec, k_pad,\
77 k_dead, k_cons, k_cur, k_shift,\
78 k_meta, k_ascii, k_lock, k_lowercase,\
79 k_slock, k_dead2, k_brl, k_ignore
80
81typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
82 char up_flag);
83static k_handler_fn K_HANDLERS;
84static k_handler_fn *k_handler[16] = { K_HANDLERS };
85
86#define FN_HANDLERS\
87 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
88 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
89 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
90 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
91 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
92
93typedef void (fn_handler_fn)(struct vc_data *vc);
94static fn_handler_fn FN_HANDLERS;
95static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
96
97/*
98 * Variables exported for vt_ioctl.c
99 */
100
101/* maximum values each key_handler can handle */
102const int max_vals[] = {
103 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
104 NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
105 255, NR_LOCK - 1, 255, NR_BRL - 1
106};
107
108const int NR_TYPES = ARRAY_SIZE(max_vals);
109
110struct kbd_struct kbd_table[MAX_NR_CONSOLES];
111EXPORT_SYMBOL_GPL(kbd_table);
112static struct kbd_struct *kbd = kbd_table;
113
114struct vt_spawn_console vt_spawn_con = {
115 .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
116 .pid = NULL,
117 .sig = 0,
118};
119
120/*
121 * Variables exported for vt.c
122 */
123
124int shift_state = 0;
125
126/*
127 * Internal Data.
128 */
129
130static struct input_handler kbd_handler;
131static DEFINE_SPINLOCK(kbd_event_lock);
132static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */
133static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
134static bool dead_key_next;
135static int npadch = -1; /* -1 or number assembled on pad */
136static unsigned int diacr;
137static char rep; /* flag telling character repeat */
138
139static unsigned char ledstate = 0xff; /* undefined */
140static unsigned char ledioctl;
141
142static struct ledptr {
143 unsigned int *addr;
144 unsigned int mask;
145 unsigned char valid:1;
146} ledptrs[3];
147
148/*
149 * Notifier list for console keyboard events
150 */
151static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
152
153int register_keyboard_notifier(struct notifier_block *nb)
154{
155 return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
156}
157EXPORT_SYMBOL_GPL(register_keyboard_notifier);
158
159int unregister_keyboard_notifier(struct notifier_block *nb)
160{
161 return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
162}
163EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
164
165/*
166 * Translation of scancodes to keycodes. We set them on only the first
167 * keyboard in the list that accepts the scancode and keycode.
168 * Explanation for not choosing the first attached keyboard anymore:
169 * USB keyboards for example have two event devices: one for all "normal"
170 * keys and one for extra function keys (like "volume up", "make coffee",
171 * etc.). So this means that scancodes for the extra function keys won't
172 * be valid for the first event device, but will be for the second.
173 */
174
175struct getset_keycode_data {
176 struct input_keymap_entry ke;
177 int error;
178};
179
180static int getkeycode_helper(struct input_handle *handle, void *data)
181{
182 struct getset_keycode_data *d = data;
183
184 d->error = input_get_keycode(handle->dev, &d->ke);
185
186 return d->error == 0; /* stop as soon as we successfully get one */
187}
188
189int getkeycode(unsigned int scancode)
190{
191 struct getset_keycode_data d = {
192 .ke = {
193 .flags = 0,
194 .len = sizeof(scancode),
195 .keycode = 0,
196 },
197 .error = -ENODEV,
198 };
199
200 memcpy(d.ke.scancode, &scancode, sizeof(scancode));
201
202 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
203
204 return d.error ?: d.ke.keycode;
205}
206
207static int setkeycode_helper(struct input_handle *handle, void *data)
208{
209 struct getset_keycode_data *d = data;
210
211 d->error = input_set_keycode(handle->dev, &d->ke);
212
213 return d->error == 0; /* stop as soon as we successfully set one */
214}
215
216int setkeycode(unsigned int scancode, unsigned int keycode)
217{
218 struct getset_keycode_data d = {
219 .ke = {
220 .flags = 0,
221 .len = sizeof(scancode),
222 .keycode = keycode,
223 },
224 .error = -ENODEV,
225 };
226
227 memcpy(d.ke.scancode, &scancode, sizeof(scancode));
228
229 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
230
231 return d.error;
232}
233
234/*
235 * Making beeps and bells. Note that we prefer beeps to bells, but when
236 * shutting the sound off we do both.
237 */
238
239static int kd_sound_helper(struct input_handle *handle, void *data)
240{
241 unsigned int *hz = data;
242 struct input_dev *dev = handle->dev;
243
244 if (test_bit(EV_SND, dev->evbit)) {
245 if (test_bit(SND_TONE, dev->sndbit)) {
246 input_inject_event(handle, EV_SND, SND_TONE, *hz);
247 if (*hz)
248 return 0;
249 }
250 if (test_bit(SND_BELL, dev->sndbit))
251 input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
252 }
253
254 return 0;
255}
256
257static void kd_nosound(unsigned long ignored)
258{
259 static unsigned int zero;
260
261 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
262}
263
264static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
265
266void kd_mksound(unsigned int hz, unsigned int ticks)
267{
268 del_timer_sync(&kd_mksound_timer);
269
270 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
271
272 if (hz && ticks)
273 mod_timer(&kd_mksound_timer, jiffies + ticks);
274}
275EXPORT_SYMBOL(kd_mksound);
276
277/*
278 * Setting the keyboard rate.
279 */
280
281static int kbd_rate_helper(struct input_handle *handle, void *data)
282{
283 struct input_dev *dev = handle->dev;
284 struct kbd_repeat *rep = data;
285
286 if (test_bit(EV_REP, dev->evbit)) {
287
288 if (rep[0].delay > 0)
289 input_inject_event(handle,
290 EV_REP, REP_DELAY, rep[0].delay);
291 if (rep[0].period > 0)
292 input_inject_event(handle,
293 EV_REP, REP_PERIOD, rep[0].period);
294
295 rep[1].delay = dev->rep[REP_DELAY];
296 rep[1].period = dev->rep[REP_PERIOD];
297 }
298
299 return 0;
300}
301
302int kbd_rate(struct kbd_repeat *rep)
303{
304 struct kbd_repeat data[2] = { *rep };
305
306 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
307 *rep = data[1]; /* Copy currently used settings */
308
309 return 0;
310}
311
312/*
313 * Helper Functions.
314 */
315static void put_queue(struct vc_data *vc, int ch)
316{
317 struct tty_struct *tty = vc->port.tty;
318
319 if (tty) {
320 tty_insert_flip_char(tty, ch, 0);
321 con_schedule_flip(tty);
322 }
323}
324
325static void puts_queue(struct vc_data *vc, char *cp)
326{
327 struct tty_struct *tty = vc->port.tty;
328
329 if (!tty)
330 return;
331
332 while (*cp) {
333 tty_insert_flip_char(tty, *cp, 0);
334 cp++;
335 }
336 con_schedule_flip(tty);
337}
338
339static void applkey(struct vc_data *vc, int key, char mode)
340{
341 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
342
343 buf[1] = (mode ? 'O' : '[');
344 buf[2] = key;
345 puts_queue(vc, buf);
346}
347
348/*
349 * Many other routines do put_queue, but I think either
350 * they produce ASCII, or they produce some user-assigned
351 * string, and in both cases we might assume that it is
352 * in utf-8 already.
353 */
354static void to_utf8(struct vc_data *vc, uint c)
355{
356 if (c < 0x80)
357 /* 0******* */
358 put_queue(vc, c);
359 else if (c < 0x800) {
360 /* 110***** 10****** */
361 put_queue(vc, 0xc0 | (c >> 6));
362 put_queue(vc, 0x80 | (c & 0x3f));
363 } else if (c < 0x10000) {
364 if (c >= 0xD800 && c < 0xE000)
365 return;
366 if (c == 0xFFFF)
367 return;
368 /* 1110**** 10****** 10****** */
369 put_queue(vc, 0xe0 | (c >> 12));
370 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
371 put_queue(vc, 0x80 | (c & 0x3f));
372 } else if (c < 0x110000) {
373 /* 11110*** 10****** 10****** 10****** */
374 put_queue(vc, 0xf0 | (c >> 18));
375 put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
376 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
377 put_queue(vc, 0x80 | (c & 0x3f));
378 }
379}
380
381/*
382 * Called after returning from RAW mode or when changing consoles - recompute
383 * shift_down[] and shift_state from key_down[] maybe called when keymap is
384 * undefined, so that shiftkey release is seen
385 */
386void compute_shiftstate(void)
387{
388 unsigned int i, j, k, sym, val;
389
390 shift_state = 0;
391 memset(shift_down, 0, sizeof(shift_down));
392
393 for (i = 0; i < ARRAY_SIZE(key_down); i++) {
394
395 if (!key_down[i])
396 continue;
397
398 k = i * BITS_PER_LONG;
399
400 for (j = 0; j < BITS_PER_LONG; j++, k++) {
401
402 if (!test_bit(k, key_down))
403 continue;
404
405 sym = U(key_maps[0][k]);
406 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
407 continue;
408
409 val = KVAL(sym);
410 if (val == KVAL(K_CAPSSHIFT))
411 val = KVAL(K_SHIFT);
412
413 shift_down[val]++;
414 shift_state |= (1 << val);
415 }
416 }
417}
418
419/*
420 * We have a combining character DIACR here, followed by the character CH.
421 * If the combination occurs in the table, return the corresponding value.
422 * Otherwise, if CH is a space or equals DIACR, return DIACR.
423 * Otherwise, conclude that DIACR was not combining after all,
424 * queue it and return CH.
425 */
426static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
427{
428 unsigned int d = diacr;
429 unsigned int i;
430
431 diacr = 0;
432
433 if ((d & ~0xff) == BRL_UC_ROW) {
434 if ((ch & ~0xff) == BRL_UC_ROW)
435 return d | ch;
436 } else {
437 for (i = 0; i < accent_table_size; i++)
438 if (accent_table[i].diacr == d && accent_table[i].base == ch)
439 return accent_table[i].result;
440 }
441
442 if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
443 return d;
444
445 if (kbd->kbdmode == VC_UNICODE)
446 to_utf8(vc, d);
447 else {
448 int c = conv_uni_to_8bit(d);
449 if (c != -1)
450 put_queue(vc, c);
451 }
452
453 return ch;
454}
455
456/*
457 * Special function handlers
458 */
459static void fn_enter(struct vc_data *vc)
460{
461 if (diacr) {
462 if (kbd->kbdmode == VC_UNICODE)
463 to_utf8(vc, diacr);
464 else {
465 int c = conv_uni_to_8bit(diacr);
466 if (c != -1)
467 put_queue(vc, c);
468 }
469 diacr = 0;
470 }
471
472 put_queue(vc, 13);
473 if (vc_kbd_mode(kbd, VC_CRLF))
474 put_queue(vc, 10);
475}
476
477static void fn_caps_toggle(struct vc_data *vc)
478{
479 if (rep)
480 return;
481
482 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
483}
484
485static void fn_caps_on(struct vc_data *vc)
486{
487 if (rep)
488 return;
489
490 set_vc_kbd_led(kbd, VC_CAPSLOCK);
491}
492
493static void fn_show_ptregs(struct vc_data *vc)
494{
495 struct pt_regs *regs = get_irq_regs();
496
497 if (regs)
498 show_regs(regs);
499}
500
501static void fn_hold(struct vc_data *vc)
502{
503 struct tty_struct *tty = vc->port.tty;
504
505 if (rep || !tty)
506 return;
507
508 /*
509 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
510 * these routines are also activated by ^S/^Q.
511 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
512 */
513 if (tty->stopped)
514 start_tty(tty);
515 else
516 stop_tty(tty);
517}
518
519static void fn_num(struct vc_data *vc)
520{
521 if (vc_kbd_mode(kbd, VC_APPLIC))
522 applkey(vc, 'P', 1);
523 else
524 fn_bare_num(vc);
525}
526
527/*
528 * Bind this to Shift-NumLock if you work in application keypad mode
529 * but want to be able to change the NumLock flag.
530 * Bind this to NumLock if you prefer that the NumLock key always
531 * changes the NumLock flag.
532 */
533static void fn_bare_num(struct vc_data *vc)
534{
535 if (!rep)
536 chg_vc_kbd_led(kbd, VC_NUMLOCK);
537}
538
539static void fn_lastcons(struct vc_data *vc)
540{
541 /* switch to the last used console, ChN */
542 set_console(last_console);
543}
544
545static void fn_dec_console(struct vc_data *vc)
546{
547 int i, cur = fg_console;
548
549 /* Currently switching? Queue this next switch relative to that. */
550 if (want_console != -1)
551 cur = want_console;
552
553 for (i = cur - 1; i != cur; i--) {
554 if (i == -1)
555 i = MAX_NR_CONSOLES - 1;
556 if (vc_cons_allocated(i))
557 break;
558 }
559 set_console(i);
560}
561
562static void fn_inc_console(struct vc_data *vc)
563{
564 int i, cur = fg_console;
565
566 /* Currently switching? Queue this next switch relative to that. */
567 if (want_console != -1)
568 cur = want_console;
569
570 for (i = cur+1; i != cur; i++) {
571 if (i == MAX_NR_CONSOLES)
572 i = 0;
573 if (vc_cons_allocated(i))
574 break;
575 }
576 set_console(i);
577}
578
579static void fn_send_intr(struct vc_data *vc)
580{
581 struct tty_struct *tty = vc->port.tty;
582
583 if (!tty)
584 return;
585 tty_insert_flip_char(tty, 0, TTY_BREAK);
586 con_schedule_flip(tty);
587}
588
589static void fn_scroll_forw(struct vc_data *vc)
590{
591 scrollfront(vc, 0);
592}
593
594static void fn_scroll_back(struct vc_data *vc)
595{
596 scrollback(vc, 0);
597}
598
599static void fn_show_mem(struct vc_data *vc)
600{
601 show_mem(0);
602}
603
604static void fn_show_state(struct vc_data *vc)
605{
606 show_state();
607}
608
609static void fn_boot_it(struct vc_data *vc)
610{
611 ctrl_alt_del();
612}
613
614static void fn_compose(struct vc_data *vc)
615{
616 dead_key_next = true;
617}
618
619static void fn_spawn_con(struct vc_data *vc)
620{
621 spin_lock(&vt_spawn_con.lock);
622 if (vt_spawn_con.pid)
623 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
624 put_pid(vt_spawn_con.pid);
625 vt_spawn_con.pid = NULL;
626 }
627 spin_unlock(&vt_spawn_con.lock);
628}
629
630static void fn_SAK(struct vc_data *vc)
631{
632 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
633 schedule_work(SAK_work);
634}
635
636static void fn_null(struct vc_data *vc)
637{
638 compute_shiftstate();
639}
640
641/*
642 * Special key handlers
643 */
644static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
645{
646}
647
648static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
649{
650 if (up_flag)
651 return;
652 if (value >= ARRAY_SIZE(fn_handler))
653 return;
654 if ((kbd->kbdmode == VC_RAW ||
655 kbd->kbdmode == VC_MEDIUMRAW ||
656 kbd->kbdmode == VC_OFF) &&
657 value != KVAL(K_SAK))
658 return; /* SAK is allowed even in raw mode */
659 fn_handler[value](vc);
660}
661
662static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
663{
664 pr_err("k_lowercase was called - impossible\n");
665}
666
667static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
668{
669 if (up_flag)
670 return; /* no action, if this is a key release */
671
672 if (diacr)
673 value = handle_diacr(vc, value);
674
675 if (dead_key_next) {
676 dead_key_next = false;
677 diacr = value;
678 return;
679 }
680 if (kbd->kbdmode == VC_UNICODE)
681 to_utf8(vc, value);
682 else {
683 int c = conv_uni_to_8bit(value);
684 if (c != -1)
685 put_queue(vc, c);
686 }
687}
688
689/*
690 * Handle dead key. Note that we now may have several
691 * dead keys modifying the same character. Very useful
692 * for Vietnamese.
693 */
694static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
695{
696 if (up_flag)
697 return;
698
699 diacr = (diacr ? handle_diacr(vc, value) : value);
700}
701
702static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
703{
704 k_unicode(vc, conv_8bit_to_uni(value), up_flag);
705}
706
707static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
708{
709 k_deadunicode(vc, value, up_flag);
710}
711
712/*
713 * Obsolete - for backwards compatibility only
714 */
715static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
716{
717 static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
718
719 k_deadunicode(vc, ret_diacr[value], up_flag);
720}
721
722static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
723{
724 if (up_flag)
725 return;
726
727 set_console(value);
728}
729
730static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
731{
732 if (up_flag)
733 return;
734
735 if ((unsigned)value < ARRAY_SIZE(func_table)) {
736 if (func_table[value])
737 puts_queue(vc, func_table[value]);
738 } else
739 pr_err("k_fn called with value=%d\n", value);
740}
741
742static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
743{
744 static const char cur_chars[] = "BDCA";
745
746 if (up_flag)
747 return;
748
749 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
750}
751
752static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
753{
754 static const char pad_chars[] = "0123456789+-*/\015,.?()#";
755 static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
756
757 if (up_flag)
758 return; /* no action, if this is a key release */
759
760 /* kludge... shift forces cursor/number keys */
761 if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
762 applkey(vc, app_map[value], 1);
763 return;
764 }
765
766 if (!vc_kbd_led(kbd, VC_NUMLOCK)) {
767
768 switch (value) {
769 case KVAL(K_PCOMMA):
770 case KVAL(K_PDOT):
771 k_fn(vc, KVAL(K_REMOVE), 0);
772 return;
773 case KVAL(K_P0):
774 k_fn(vc, KVAL(K_INSERT), 0);
775 return;
776 case KVAL(K_P1):
777 k_fn(vc, KVAL(K_SELECT), 0);
778 return;
779 case KVAL(K_P2):
780 k_cur(vc, KVAL(K_DOWN), 0);
781 return;
782 case KVAL(K_P3):
783 k_fn(vc, KVAL(K_PGDN), 0);
784 return;
785 case KVAL(K_P4):
786 k_cur(vc, KVAL(K_LEFT), 0);
787 return;
788 case KVAL(K_P6):
789 k_cur(vc, KVAL(K_RIGHT), 0);
790 return;
791 case KVAL(K_P7):
792 k_fn(vc, KVAL(K_FIND), 0);
793 return;
794 case KVAL(K_P8):
795 k_cur(vc, KVAL(K_UP), 0);
796 return;
797 case KVAL(K_P9):
798 k_fn(vc, KVAL(K_PGUP), 0);
799 return;
800 case KVAL(K_P5):
801 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
802 return;
803 }
804 }
805
806 put_queue(vc, pad_chars[value]);
807 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
808 put_queue(vc, 10);
809}
810
811static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
812{
813 int old_state = shift_state;
814
815 if (rep)
816 return;
817 /*
818 * Mimic typewriter:
819 * a CapsShift key acts like Shift but undoes CapsLock
820 */
821 if (value == KVAL(K_CAPSSHIFT)) {
822 value = KVAL(K_SHIFT);
823 if (!up_flag)
824 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
825 }
826
827 if (up_flag) {
828 /*
829 * handle the case that two shift or control
830 * keys are depressed simultaneously
831 */
832 if (shift_down[value])
833 shift_down[value]--;
834 } else
835 shift_down[value]++;
836
837 if (shift_down[value])
838 shift_state |= (1 << value);
839 else
840 shift_state &= ~(1 << value);
841
842 /* kludge */
843 if (up_flag && shift_state != old_state && npadch != -1) {
844 if (kbd->kbdmode == VC_UNICODE)
845 to_utf8(vc, npadch);
846 else
847 put_queue(vc, npadch & 0xff);
848 npadch = -1;
849 }
850}
851
852static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
853{
854 if (up_flag)
855 return;
856
857 if (vc_kbd_mode(kbd, VC_META)) {
858 put_queue(vc, '\033');
859 put_queue(vc, value);
860 } else
861 put_queue(vc, value | 0x80);
862}
863
864static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
865{
866 int base;
867
868 if (up_flag)
869 return;
870
871 if (value < 10) {
872 /* decimal input of code, while Alt depressed */
873 base = 10;
874 } else {
875 /* hexadecimal input of code, while AltGr depressed */
876 value -= 10;
877 base = 16;
878 }
879
880 if (npadch == -1)
881 npadch = value;
882 else
883 npadch = npadch * base + value;
884}
885
886static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
887{
888 if (up_flag || rep)
889 return;
890
891 chg_vc_kbd_lock(kbd, value);
892}
893
894static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
895{
896 k_shift(vc, value, up_flag);
897 if (up_flag || rep)
898 return;
899
900 chg_vc_kbd_slock(kbd, value);
901 /* try to make Alt, oops, AltGr and such work */
902 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
903 kbd->slockstate = 0;
904 chg_vc_kbd_slock(kbd, value);
905 }
906}
907
908/* by default, 300ms interval for combination release */
909static unsigned brl_timeout = 300;
910MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
911module_param(brl_timeout, uint, 0644);
912
913static unsigned brl_nbchords = 1;
914MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
915module_param(brl_nbchords, uint, 0644);
916
917static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
918{
919 static unsigned long chords;
920 static unsigned committed;
921
922 if (!brl_nbchords)
923 k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
924 else {
925 committed |= pattern;
926 chords++;
927 if (chords == brl_nbchords) {
928 k_unicode(vc, BRL_UC_ROW | committed, up_flag);
929 chords = 0;
930 committed = 0;
931 }
932 }
933}
934
935static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
936{
937 static unsigned pressed, committing;
938 static unsigned long releasestart;
939
940 if (kbd->kbdmode != VC_UNICODE) {
941 if (!up_flag)
942 pr_warning("keyboard mode must be unicode for braille patterns\n");
943 return;
944 }
945
946 if (!value) {
947 k_unicode(vc, BRL_UC_ROW, up_flag);
948 return;
949 }
950
951 if (value > 8)
952 return;
953
954 if (!up_flag) {
955 pressed |= 1 << (value - 1);
956 if (!brl_timeout)
957 committing = pressed;
958 } else if (brl_timeout) {
959 if (!committing ||
960 time_after(jiffies,
961 releasestart + msecs_to_jiffies(brl_timeout))) {
962 committing = pressed;
963 releasestart = jiffies;
964 }
965 pressed &= ~(1 << (value - 1));
966 if (!pressed && committing) {
967 k_brlcommit(vc, committing, 0);
968 committing = 0;
969 }
970 } else {
971 if (committing) {
972 k_brlcommit(vc, committing, 0);
973 committing = 0;
974 }
975 pressed &= ~(1 << (value - 1));
976 }
977}
978
979/*
980 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
981 * or (ii) whatever pattern of lights people want to show using KDSETLED,
982 * or (iii) specified bits of specified words in kernel memory.
983 */
984unsigned char getledstate(void)
985{
986 return ledstate;
987}
988
989void setledstate(struct kbd_struct *kbd, unsigned int led)
990{
991 if (!(led & ~7)) {
992 ledioctl = led;
993 kbd->ledmode = LED_SHOW_IOCTL;
994 } else
995 kbd->ledmode = LED_SHOW_FLAGS;
996
997 set_leds();
998}
999
1000static inline unsigned char getleds(void)
1001{
1002 struct kbd_struct *kbd = kbd_table + fg_console;
1003 unsigned char leds;
1004 int i;
1005
1006 if (kbd->ledmode == LED_SHOW_IOCTL)
1007 return ledioctl;
1008
1009 leds = kbd->ledflagstate;
1010
1011 if (kbd->ledmode == LED_SHOW_MEM) {
1012 for (i = 0; i < 3; i++)
1013 if (ledptrs[i].valid) {
1014 if (*ledptrs[i].addr & ledptrs[i].mask)
1015 leds |= (1 << i);
1016 else
1017 leds &= ~(1 << i);
1018 }
1019 }
1020 return leds;
1021}
1022
1023static int kbd_update_leds_helper(struct input_handle *handle, void *data)
1024{
1025 unsigned char leds = *(unsigned char *)data;
1026
1027 if (test_bit(EV_LED, handle->dev->evbit)) {
1028 input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
1029 input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02));
1030 input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04));
1031 input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
1032 }
1033
1034 return 0;
1035}
1036
1037/*
1038 * This is the tasklet that updates LED state on all keyboards
1039 * attached to the box. The reason we use tasklet is that we
1040 * need to handle the scenario when keyboard handler is not
1041 * registered yet but we already getting updates form VT to
1042 * update led state.
1043 */
1044static void kbd_bh(unsigned long dummy)
1045{
1046 unsigned char leds = getleds();
1047
1048 if (leds != ledstate) {
1049 input_handler_for_each_handle(&kbd_handler, &leds,
1050 kbd_update_leds_helper);
1051 ledstate = leds;
1052 }
1053}
1054
1055DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
1056
1057#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1058 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1059 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1060 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1061 defined(CONFIG_AVR32)
1062
1063#define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1064 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1065
1066static const unsigned short x86_keycodes[256] =
1067 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1068 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1069 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1070 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1071 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1072 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1073 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1074 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1075 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1076 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1077 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1078 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1079 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1080 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1081 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1082
1083#ifdef CONFIG_SPARC
1084static int sparc_l1_a_state;
1085extern void sun_do_break(void);
1086#endif
1087
1088static int emulate_raw(struct vc_data *vc, unsigned int keycode,
1089 unsigned char up_flag)
1090{
1091 int code;
1092
1093 switch (keycode) {
1094
1095 case KEY_PAUSE:
1096 put_queue(vc, 0xe1);
1097 put_queue(vc, 0x1d | up_flag);
1098 put_queue(vc, 0x45 | up_flag);
1099 break;
1100
1101 case KEY_HANGEUL:
1102 if (!up_flag)
1103 put_queue(vc, 0xf2);
1104 break;
1105
1106 case KEY_HANJA:
1107 if (!up_flag)
1108 put_queue(vc, 0xf1);
1109 break;
1110
1111 case KEY_SYSRQ:
1112 /*
1113 * Real AT keyboards (that's what we're trying
1114 * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
1115 * pressing PrtSc/SysRq alone, but simply 0x54
1116 * when pressing Alt+PrtSc/SysRq.
1117 */
1118 if (test_bit(KEY_LEFTALT, key_down) ||
1119 test_bit(KEY_RIGHTALT, key_down)) {
1120 put_queue(vc, 0x54 | up_flag);
1121 } else {
1122 put_queue(vc, 0xe0);
1123 put_queue(vc, 0x2a | up_flag);
1124 put_queue(vc, 0xe0);
1125 put_queue(vc, 0x37 | up_flag);
1126 }
1127 break;
1128
1129 default:
1130 if (keycode > 255)
1131 return -1;
1132
1133 code = x86_keycodes[keycode];
1134 if (!code)
1135 return -1;
1136
1137 if (code & 0x100)
1138 put_queue(vc, 0xe0);
1139 put_queue(vc, (code & 0x7f) | up_flag);
1140
1141 break;
1142 }
1143
1144 return 0;
1145}
1146
1147#else
1148
1149#define HW_RAW(dev) 0
1150
1151static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1152{
1153 if (keycode > 127)
1154 return -1;
1155
1156 put_queue(vc, keycode | up_flag);
1157 return 0;
1158}
1159#endif
1160
1161static void kbd_rawcode(unsigned char data)
1162{
1163 struct vc_data *vc = vc_cons[fg_console].d;
1164
1165 kbd = kbd_table + vc->vc_num;
1166 if (kbd->kbdmode == VC_RAW)
1167 put_queue(vc, data);
1168}
1169
1170static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1171{
1172 struct vc_data *vc = vc_cons[fg_console].d;
1173 unsigned short keysym, *key_map;
1174 unsigned char type;
1175 bool raw_mode;
1176 struct tty_struct *tty;
1177 int shift_final;
1178 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1179 int rc;
1180
1181 tty = vc->port.tty;
1182
1183 if (tty && (!tty->driver_data)) {
1184 /* No driver data? Strange. Okay we fix it then. */
1185 tty->driver_data = vc;
1186 }
1187
1188 kbd = kbd_table + vc->vc_num;
1189
1190#ifdef CONFIG_SPARC
1191 if (keycode == KEY_STOP)
1192 sparc_l1_a_state = down;
1193#endif
1194
1195 rep = (down == 2);
1196
1197 raw_mode = (kbd->kbdmode == VC_RAW);
1198 if (raw_mode && !hw_raw)
1199 if (emulate_raw(vc, keycode, !down << 7))
1200 if (keycode < BTN_MISC && printk_ratelimit())
1201 pr_warning("can't emulate rawmode for keycode %d\n",
1202 keycode);
1203
1204#ifdef CONFIG_SPARC
1205 if (keycode == KEY_A && sparc_l1_a_state) {
1206 sparc_l1_a_state = false;
1207 sun_do_break();
1208 }
1209#endif
1210
1211 if (kbd->kbdmode == VC_MEDIUMRAW) {
1212 /*
1213 * This is extended medium raw mode, with keys above 127
1214 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
1215 * the 'up' flag if needed. 0 is reserved, so this shouldn't
1216 * interfere with anything else. The two bytes after 0 will
1217 * always have the up flag set not to interfere with older
1218 * applications. This allows for 16384 different keycodes,
1219 * which should be enough.
1220 */
1221 if (keycode < 128) {
1222 put_queue(vc, keycode | (!down << 7));
1223 } else {
1224 put_queue(vc, !down << 7);
1225 put_queue(vc, (keycode >> 7) | 0x80);
1226 put_queue(vc, keycode | 0x80);
1227 }
1228 raw_mode = true;
1229 }
1230
1231 if (down)
1232 set_bit(keycode, key_down);
1233 else
1234 clear_bit(keycode, key_down);
1235
1236 if (rep &&
1237 (!vc_kbd_mode(kbd, VC_REPEAT) ||
1238 (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
1239 /*
1240 * Don't repeat a key if the input buffers are not empty and the
1241 * characters get aren't echoed locally. This makes key repeat
1242 * usable with slow applications and under heavy loads.
1243 */
1244 return;
1245 }
1246
1247 param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1248 param.ledstate = kbd->ledflagstate;
1249 key_map = key_maps[shift_final];
1250
1251 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1252 KBD_KEYCODE, &param);
1253 if (rc == NOTIFY_STOP || !key_map) {
1254 atomic_notifier_call_chain(&keyboard_notifier_list,
1255 KBD_UNBOUND_KEYCODE, &param);
1256 compute_shiftstate();
1257 kbd->slockstate = 0;
1258 return;
1259 }
1260
1261 if (keycode < NR_KEYS)
1262 keysym = key_map[keycode];
1263 else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
1264 keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
1265 else
1266 return;
1267
1268 type = KTYP(keysym);
1269
1270 if (type < 0xf0) {
1271 param.value = keysym;
1272 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1273 KBD_UNICODE, &param);
1274 if (rc != NOTIFY_STOP)
1275 if (down && !raw_mode)
1276 to_utf8(vc, keysym);
1277 return;
1278 }
1279
1280 type -= 0xf0;
1281
1282 if (type == KT_LETTER) {
1283 type = KT_LATIN;
1284 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1285 key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1286 if (key_map)
1287 keysym = key_map[keycode];
1288 }
1289 }
1290
1291 param.value = keysym;
1292 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1293 KBD_KEYSYM, &param);
1294 if (rc == NOTIFY_STOP)
1295 return;
1296
1297 if ((raw_mode || kbd->kbdmode == VC_OFF) && type != KT_SPEC && type != KT_SHIFT)
1298 return;
1299
1300 (*k_handler[type])(vc, keysym & 0xff, !down);
1301
1302 param.ledstate = kbd->ledflagstate;
1303 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
1304
1305 if (type != KT_SLOCK)
1306 kbd->slockstate = 0;
1307}
1308
1309static void kbd_event(struct input_handle *handle, unsigned int event_type,
1310 unsigned int event_code, int value)
1311{
1312 /* We are called with interrupts disabled, just take the lock */
1313 spin_lock(&kbd_event_lock);
1314
1315 if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
1316 kbd_rawcode(value);
1317 if (event_type == EV_KEY)
1318 kbd_keycode(event_code, value, HW_RAW(handle->dev));
1319
1320 spin_unlock(&kbd_event_lock);
1321
1322 tasklet_schedule(&keyboard_tasklet);
1323 do_poke_blanked_console = 1;
1324 schedule_console_callback();
1325}
1326
1327static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
1328{
1329 int i;
1330
1331 if (test_bit(EV_SND, dev->evbit))
1332 return true;
1333
1334 if (test_bit(EV_KEY, dev->evbit)) {
1335 for (i = KEY_RESERVED; i < BTN_MISC; i++)
1336 if (test_bit(i, dev->keybit))
1337 return true;
1338 for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++)
1339 if (test_bit(i, dev->keybit))
1340 return true;
1341 }
1342
1343 return false;
1344}
1345
1346/*
1347 * When a keyboard (or other input device) is found, the kbd_connect
1348 * function is called. The function then looks at the device, and if it
1349 * likes it, it can open it and get events from it. In this (kbd_connect)
1350 * function, we should decide which VT to bind that keyboard to initially.
1351 */
1352static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
1353 const struct input_device_id *id)
1354{
1355 struct input_handle *handle;
1356 int error;
1357
1358 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1359 if (!handle)
1360 return -ENOMEM;
1361
1362 handle->dev = dev;
1363 handle->handler = handler;
1364 handle->name = "kbd";
1365
1366 error = input_register_handle(handle);
1367 if (error)
1368 goto err_free_handle;
1369
1370 error = input_open_device(handle);
1371 if (error)
1372 goto err_unregister_handle;
1373
1374 return 0;
1375
1376 err_unregister_handle:
1377 input_unregister_handle(handle);
1378 err_free_handle:
1379 kfree(handle);
1380 return error;
1381}
1382
1383static void kbd_disconnect(struct input_handle *handle)
1384{
1385 input_close_device(handle);
1386 input_unregister_handle(handle);
1387 kfree(handle);
1388}
1389
1390/*
1391 * Start keyboard handler on the new keyboard by refreshing LED state to
1392 * match the rest of the system.
1393 */
1394static void kbd_start(struct input_handle *handle)
1395{
1396 tasklet_disable(&keyboard_tasklet);
1397
1398 if (ledstate != 0xff)
1399 kbd_update_leds_helper(handle, &ledstate);
1400
1401 tasklet_enable(&keyboard_tasklet);
1402}
1403
1404static const struct input_device_id kbd_ids[] = {
1405 {
1406 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1407 .evbit = { BIT_MASK(EV_KEY) },
1408 },
1409
1410 {
1411 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1412 .evbit = { BIT_MASK(EV_SND) },
1413 },
1414
1415 { }, /* Terminating entry */
1416};
1417
1418MODULE_DEVICE_TABLE(input, kbd_ids);
1419
1420static struct input_handler kbd_handler = {
1421 .event = kbd_event,
1422 .match = kbd_match,
1423 .connect = kbd_connect,
1424 .disconnect = kbd_disconnect,
1425 .start = kbd_start,
1426 .name = "kbd",
1427 .id_table = kbd_ids,
1428};
1429
1430int __init kbd_init(void)
1431{
1432 int i;
1433 int error;
1434
1435 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1436 kbd_table[i].ledflagstate = KBD_DEFLEDS;
1437 kbd_table[i].default_ledflagstate = KBD_DEFLEDS;
1438 kbd_table[i].ledmode = LED_SHOW_FLAGS;
1439 kbd_table[i].lockstate = KBD_DEFLOCK;
1440 kbd_table[i].slockstate = 0;
1441 kbd_table[i].modeflags = KBD_DEFMODE;
1442 kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1443 }
1444
1445 error = input_register_handler(&kbd_handler);
1446 if (error)
1447 return error;
1448
1449 tasklet_enable(&keyboard_tasklet);
1450 tasklet_schedule(&keyboard_tasklet);
1451
1452 return 0;
1453}
diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
new file mode 100644
index 000000000000..fb864e7fcd13
--- /dev/null
+++ b/drivers/tty/vt/selection.c
@@ -0,0 +1,345 @@
1/*
2 * This module exports the functions:
3 *
4 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
5 * 'void clear_selection(void)'
6 * 'int paste_selection(struct tty_struct *)'
7 * 'int sel_loadlut(char __user *)'
8 *
9 * Now that /dev/vcs exists, most of this can disappear again.
10 */
11
12#include <linux/module.h>
13#include <linux/tty.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/slab.h>
17#include <linux/types.h>
18
19#include <asm/uaccess.h>
20
21#include <linux/kbd_kern.h>
22#include <linux/vt_kern.h>
23#include <linux/consolemap.h>
24#include <linux/selection.h>
25#include <linux/tiocl.h>
26#include <linux/console.h>
27
28/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
29#define isspace(c) ((c) == ' ')
30
31extern void poke_blanked_console(void);
32
33/* Variables for selection control. */
34/* Use a dynamic buffer, instead of static (Dec 1994) */
35struct vc_data *sel_cons; /* must not be deallocated */
36static int use_unicode;
37static volatile int sel_start = -1; /* cleared by clear_selection */
38static int sel_end;
39static int sel_buffer_lth;
40static char *sel_buffer;
41
42/* clear_selection, highlight and highlight_pointer can be called
43 from interrupt (via scrollback/front) */
44
45/* set reverse video on characters s-e of console with selection. */
46static inline void highlight(const int s, const int e)
47{
48 invert_screen(sel_cons, s, e-s+2, 1);
49}
50
51/* use complementary color to show the pointer */
52static inline void highlight_pointer(const int where)
53{
54 complement_pos(sel_cons, where);
55}
56
57static u16
58sel_pos(int n)
59{
60 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
61 use_unicode);
62}
63
64/* remove the current selection highlight, if any,
65 from the console holding the selection. */
66void
67clear_selection(void) {
68 highlight_pointer(-1); /* hide the pointer */
69 if (sel_start != -1) {
70 highlight(sel_start, sel_end);
71 sel_start = -1;
72 }
73}
74
75/*
76 * User settable table: what characters are to be considered alphabetic?
77 * 256 bits
78 */
79static u32 inwordLut[8]={
80 0x00000000, /* control chars */
81 0x03FF0000, /* digits */
82 0x87FFFFFE, /* uppercase and '_' */
83 0x07FFFFFE, /* lowercase */
84 0x00000000,
85 0x00000000,
86 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
87 0xFF7FFFFF /* latin-1 accented letters, not division sign */
88};
89
90static inline int inword(const u16 c) {
91 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
92}
93
94/* set inwordLut contents. Invoked by ioctl(). */
95int sel_loadlut(char __user *p)
96{
97 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
98}
99
100/* does screen address p correspond to character at LH/RH edge of screen? */
101static inline int atedge(const int p, int size_row)
102{
103 return (!(p % size_row) || !((p + 2) % size_row));
104}
105
106/* constrain v such that v <= u */
107static inline unsigned short limit(const unsigned short v, const unsigned short u)
108{
109 return (v > u) ? u : v;
110}
111
112/* stores the char in UTF8 and returns the number of bytes used (1-3) */
113static int store_utf8(u16 c, char *p)
114{
115 if (c < 0x80) {
116 /* 0******* */
117 p[0] = c;
118 return 1;
119 } else if (c < 0x800) {
120 /* 110***** 10****** */
121 p[0] = 0xc0 | (c >> 6);
122 p[1] = 0x80 | (c & 0x3f);
123 return 2;
124 } else {
125 /* 1110**** 10****** 10****** */
126 p[0] = 0xe0 | (c >> 12);
127 p[1] = 0x80 | ((c >> 6) & 0x3f);
128 p[2] = 0x80 | (c & 0x3f);
129 return 3;
130 }
131}
132
133/* set the current selection. Invoked by ioctl() or by kernel code. */
134int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
135{
136 struct vc_data *vc = vc_cons[fg_console].d;
137 int sel_mode, new_sel_start, new_sel_end, spc;
138 char *bp, *obp;
139 int i, ps, pe, multiplier;
140 u16 c;
141 struct kbd_struct *kbd = kbd_table + fg_console;
142
143 poke_blanked_console();
144
145 { unsigned short xs, ys, xe, ye;
146
147 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
148 return -EFAULT;
149 __get_user(xs, &sel->xs);
150 __get_user(ys, &sel->ys);
151 __get_user(xe, &sel->xe);
152 __get_user(ye, &sel->ye);
153 __get_user(sel_mode, &sel->sel_mode);
154 xs--; ys--; xe--; ye--;
155 xs = limit(xs, vc->vc_cols - 1);
156 ys = limit(ys, vc->vc_rows - 1);
157 xe = limit(xe, vc->vc_cols - 1);
158 ye = limit(ye, vc->vc_rows - 1);
159 ps = ys * vc->vc_size_row + (xs << 1);
160 pe = ye * vc->vc_size_row + (xe << 1);
161
162 if (sel_mode == TIOCL_SELCLEAR) {
163 /* useful for screendump without selection highlights */
164 clear_selection();
165 return 0;
166 }
167
168 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
169 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
170 return 0;
171 }
172 }
173
174 if (ps > pe) /* make sel_start <= sel_end */
175 {
176 int tmp = ps;
177 ps = pe;
178 pe = tmp;
179 }
180
181 if (sel_cons != vc_cons[fg_console].d) {
182 clear_selection();
183 sel_cons = vc_cons[fg_console].d;
184 }
185 use_unicode = kbd && kbd->kbdmode == VC_UNICODE;
186
187 switch (sel_mode)
188 {
189 case TIOCL_SELCHAR: /* character-by-character selection */
190 new_sel_start = ps;
191 new_sel_end = pe;
192 break;
193 case TIOCL_SELWORD: /* word-by-word selection */
194 spc = isspace(sel_pos(ps));
195 for (new_sel_start = ps; ; ps -= 2)
196 {
197 if ((spc && !isspace(sel_pos(ps))) ||
198 (!spc && !inword(sel_pos(ps))))
199 break;
200 new_sel_start = ps;
201 if (!(ps % vc->vc_size_row))
202 break;
203 }
204 spc = isspace(sel_pos(pe));
205 for (new_sel_end = pe; ; pe += 2)
206 {
207 if ((spc && !isspace(sel_pos(pe))) ||
208 (!spc && !inword(sel_pos(pe))))
209 break;
210 new_sel_end = pe;
211 if (!((pe + 2) % vc->vc_size_row))
212 break;
213 }
214 break;
215 case TIOCL_SELLINE: /* line-by-line selection */
216 new_sel_start = ps - ps % vc->vc_size_row;
217 new_sel_end = pe + vc->vc_size_row
218 - pe % vc->vc_size_row - 2;
219 break;
220 case TIOCL_SELPOINTER:
221 highlight_pointer(pe);
222 return 0;
223 default:
224 return -EINVAL;
225 }
226
227 /* remove the pointer */
228 highlight_pointer(-1);
229
230 /* select to end of line if on trailing space */
231 if (new_sel_end > new_sel_start &&
232 !atedge(new_sel_end, vc->vc_size_row) &&
233 isspace(sel_pos(new_sel_end))) {
234 for (pe = new_sel_end + 2; ; pe += 2)
235 if (!isspace(sel_pos(pe)) ||
236 atedge(pe, vc->vc_size_row))
237 break;
238 if (isspace(sel_pos(pe)))
239 new_sel_end = pe;
240 }
241 if (sel_start == -1) /* no current selection */
242 highlight(new_sel_start, new_sel_end);
243 else if (new_sel_start == sel_start)
244 {
245 if (new_sel_end == sel_end) /* no action required */
246 return 0;
247 else if (new_sel_end > sel_end) /* extend to right */
248 highlight(sel_end + 2, new_sel_end);
249 else /* contract from right */
250 highlight(new_sel_end + 2, sel_end);
251 }
252 else if (new_sel_end == sel_end)
253 {
254 if (new_sel_start < sel_start) /* extend to left */
255 highlight(new_sel_start, sel_start - 2);
256 else /* contract from left */
257 highlight(sel_start, new_sel_start - 2);
258 }
259 else /* some other case; start selection from scratch */
260 {
261 clear_selection();
262 highlight(new_sel_start, new_sel_end);
263 }
264 sel_start = new_sel_start;
265 sel_end = new_sel_end;
266
267 /* Allocate a new buffer before freeing the old one ... */
268 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
269 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
270 if (!bp) {
271 printk(KERN_WARNING "selection: kmalloc() failed\n");
272 clear_selection();
273 return -ENOMEM;
274 }
275 kfree(sel_buffer);
276 sel_buffer = bp;
277
278 obp = bp;
279 for (i = sel_start; i <= sel_end; i += 2) {
280 c = sel_pos(i);
281 if (use_unicode)
282 bp += store_utf8(c, bp);
283 else
284 *bp++ = c;
285 if (!isspace(c))
286 obp = bp;
287 if (! ((i + 2) % vc->vc_size_row)) {
288 /* strip trailing blanks from line and add newline,
289 unless non-space at end of line. */
290 if (obp != bp) {
291 bp = obp;
292 *bp++ = '\r';
293 }
294 obp = bp;
295 }
296 }
297 sel_buffer_lth = bp - sel_buffer;
298 return 0;
299}
300
301/* Insert the contents of the selection buffer into the
302 * queue of the tty associated with the current console.
303 * Invoked by ioctl().
304 */
305int paste_selection(struct tty_struct *tty)
306{
307 struct vc_data *vc = tty->driver_data;
308 int pasted = 0;
309 unsigned int count;
310 struct tty_ldisc *ld;
311 DECLARE_WAITQUEUE(wait, current);
312
313 /* always called with BTM from vt_ioctl */
314 WARN_ON(!tty_locked());
315
316 console_lock();
317 poke_blanked_console();
318 console_unlock();
319
320 ld = tty_ldisc_ref(tty);
321 if (!ld) {
322 tty_unlock();
323 ld = tty_ldisc_ref_wait(tty);
324 tty_lock();
325 }
326
327 add_wait_queue(&vc->paste_wait, &wait);
328 while (sel_buffer && sel_buffer_lth > pasted) {
329 set_current_state(TASK_INTERRUPTIBLE);
330 if (test_bit(TTY_THROTTLED, &tty->flags)) {
331 schedule();
332 continue;
333 }
334 count = sel_buffer_lth - pasted;
335 count = min(count, tty->receive_room);
336 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
337 NULL, count);
338 pasted += count;
339 }
340 remove_wait_queue(&vc->paste_wait, &wait);
341 __set_current_state(TASK_RUNNING);
342
343 tty_ldisc_deref(ld);
344 return 0;
345}
diff --git a/drivers/tty/vt/vc_screen.c b/drivers/tty/vt/vc_screen.c
new file mode 100644
index 000000000000..66825c9f516a
--- /dev/null
+++ b/drivers/tty/vt/vc_screen.c
@@ -0,0 +1,665 @@
1/*
2 * Provide access to virtual console memory.
3 * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled)
4 * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
5 * [minor: N]
6 *
7 * /dev/vcsaN: idem, but including attributes, and prefixed with
8 * the 4 bytes lines,columns,x,y (as screendump used to give).
9 * Attribute/character pair is in native endianity.
10 * [minor: N+128]
11 *
12 * This replaces screendump and part of selection, so that the system
13 * administrator can control access using file system permissions.
14 *
15 * aeb@cwi.nl - efter Friedas begravelse - 950211
16 *
17 * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
18 * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
19 * - making it shorter - scr_readw are macros which expand in PRETTY long code
20 */
21
22#include <linux/kernel.h>
23#include <linux/major.h>
24#include <linux/errno.h>
25#include <linux/tty.h>
26#include <linux/interrupt.h>
27#include <linux/mm.h>
28#include <linux/init.h>
29#include <linux/vt_kern.h>
30#include <linux/selection.h>
31#include <linux/kbd_kern.h>
32#include <linux/console.h>
33#include <linux/device.h>
34#include <linux/sched.h>
35#include <linux/fs.h>
36#include <linux/poll.h>
37#include <linux/signal.h>
38#include <linux/slab.h>
39#include <linux/notifier.h>
40
41#include <asm/uaccess.h>
42#include <asm/byteorder.h>
43#include <asm/unaligned.h>
44
45#undef attr
46#undef org
47#undef addr
48#define HEADER_SIZE 4
49
50#define CON_BUF_SIZE (CONFIG_BASE_SMALL ? 256 : PAGE_SIZE)
51
52struct vcs_poll_data {
53 struct notifier_block notifier;
54 unsigned int cons_num;
55 bool seen_last_update;
56 wait_queue_head_t waitq;
57 struct fasync_struct *fasync;
58};
59
60static int
61vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param)
62{
63 struct vt_notifier_param *param = _param;
64 struct vc_data *vc = param->vc;
65 struct vcs_poll_data *poll =
66 container_of(nb, struct vcs_poll_data, notifier);
67 int currcons = poll->cons_num;
68
69 if (code != VT_UPDATE)
70 return NOTIFY_DONE;
71
72 if (currcons == 0)
73 currcons = fg_console;
74 else
75 currcons--;
76 if (currcons != vc->vc_num)
77 return NOTIFY_DONE;
78
79 poll->seen_last_update = false;
80 wake_up_interruptible(&poll->waitq);
81 kill_fasync(&poll->fasync, SIGIO, POLL_IN);
82 return NOTIFY_OK;
83}
84
85static void
86vcs_poll_data_free(struct vcs_poll_data *poll)
87{
88 unregister_vt_notifier(&poll->notifier);
89 kfree(poll);
90}
91
92static struct vcs_poll_data *
93vcs_poll_data_get(struct file *file)
94{
95 struct vcs_poll_data *poll = file->private_data;
96
97 if (poll)
98 return poll;
99
100 poll = kzalloc(sizeof(*poll), GFP_KERNEL);
101 if (!poll)
102 return NULL;
103 poll->cons_num = iminor(file->f_path.dentry->d_inode) & 127;
104 init_waitqueue_head(&poll->waitq);
105 poll->notifier.notifier_call = vcs_notifier;
106 if (register_vt_notifier(&poll->notifier) != 0) {
107 kfree(poll);
108 return NULL;
109 }
110
111 /*
112 * This code may be called either through ->poll() or ->fasync().
113 * If we have two threads using the same file descriptor, they could
114 * both enter this function, both notice that the structure hasn't
115 * been allocated yet and go ahead allocating it in parallel, but
116 * only one of them must survive and be shared otherwise we'd leak
117 * memory with a dangling notifier callback.
118 */
119 spin_lock(&file->f_lock);
120 if (!file->private_data) {
121 file->private_data = poll;
122 } else {
123 /* someone else raced ahead of us */
124 vcs_poll_data_free(poll);
125 poll = file->private_data;
126 }
127 spin_unlock(&file->f_lock);
128
129 return poll;
130}
131
132/*
133 * Returns VC for inode.
134 * Must be called with console_lock.
135 */
136static struct vc_data*
137vcs_vc(struct inode *inode, int *viewed)
138{
139 unsigned int currcons = iminor(inode) & 127;
140
141 WARN_CONSOLE_UNLOCKED();
142
143 if (currcons == 0) {
144 currcons = fg_console;
145 if (viewed)
146 *viewed = 1;
147 } else {
148 currcons--;
149 if (viewed)
150 *viewed = 0;
151 }
152 return vc_cons[currcons].d;
153}
154
155/*
156 * Returns size for VC carried by inode.
157 * Must be called with console_lock.
158 */
159static int
160vcs_size(struct inode *inode)
161{
162 int size;
163 int minor = iminor(inode);
164 struct vc_data *vc;
165
166 WARN_CONSOLE_UNLOCKED();
167
168 vc = vcs_vc(inode, NULL);
169 if (!vc)
170 return -ENXIO;
171
172 size = vc->vc_rows * vc->vc_cols;
173
174 if (minor & 128)
175 size = 2*size + HEADER_SIZE;
176 return size;
177}
178
179static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
180{
181 int size;
182
183 console_lock();
184 size = vcs_size(file->f_path.dentry->d_inode);
185 console_unlock();
186 if (size < 0)
187 return size;
188 switch (orig) {
189 default:
190 return -EINVAL;
191 case 2:
192 offset += size;
193 break;
194 case 1:
195 offset += file->f_pos;
196 case 0:
197 break;
198 }
199 if (offset < 0 || offset > size) {
200 return -EINVAL;
201 }
202 file->f_pos = offset;
203 return file->f_pos;
204}
205
206
207static ssize_t
208vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
209{
210 struct inode *inode = file->f_path.dentry->d_inode;
211 unsigned int currcons = iminor(inode);
212 struct vc_data *vc;
213 struct vcs_poll_data *poll;
214 long pos;
215 long attr, read;
216 int col, maxcol, viewed;
217 unsigned short *org = NULL;
218 ssize_t ret;
219 char *con_buf;
220
221 con_buf = (char *) __get_free_page(GFP_KERNEL);
222 if (!con_buf)
223 return -ENOMEM;
224
225 pos = *ppos;
226
227 /* Select the proper current console and verify
228 * sanity of the situation under the console lock.
229 */
230 console_lock();
231
232 attr = (currcons & 128);
233 ret = -ENXIO;
234 vc = vcs_vc(inode, &viewed);
235 if (!vc)
236 goto unlock_out;
237
238 ret = -EINVAL;
239 if (pos < 0)
240 goto unlock_out;
241 poll = file->private_data;
242 if (count && poll)
243 poll->seen_last_update = true;
244 read = 0;
245 ret = 0;
246 while (count) {
247 char *con_buf0, *con_buf_start;
248 long this_round, size;
249 ssize_t orig_count;
250 long p = pos;
251
252 /* Check whether we are above size each round,
253 * as copy_to_user at the end of this loop
254 * could sleep.
255 */
256 size = vcs_size(inode);
257 if (size < 0) {
258 if (read)
259 break;
260 ret = size;
261 goto unlock_out;
262 }
263 if (pos >= size)
264 break;
265 if (count > size - pos)
266 count = size - pos;
267
268 this_round = count;
269 if (this_round > CON_BUF_SIZE)
270 this_round = CON_BUF_SIZE;
271
272 /* Perform the whole read into the local con_buf.
273 * Then we can drop the console spinlock and safely
274 * attempt to move it to userspace.
275 */
276
277 con_buf_start = con_buf0 = con_buf;
278 orig_count = this_round;
279 maxcol = vc->vc_cols;
280 if (!attr) {
281 org = screen_pos(vc, p, viewed);
282 col = p % maxcol;
283 p += maxcol - col;
284 while (this_round-- > 0) {
285 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
286 if (++col == maxcol) {
287 org = screen_pos(vc, p, viewed);
288 col = 0;
289 p += maxcol;
290 }
291 }
292 } else {
293 if (p < HEADER_SIZE) {
294 size_t tmp_count;
295
296 con_buf0[0] = (char)vc->vc_rows;
297 con_buf0[1] = (char)vc->vc_cols;
298 getconsxy(vc, con_buf0 + 2);
299
300 con_buf_start += p;
301 this_round += p;
302 if (this_round > CON_BUF_SIZE) {
303 this_round = CON_BUF_SIZE;
304 orig_count = this_round - p;
305 }
306
307 tmp_count = HEADER_SIZE;
308 if (tmp_count > this_round)
309 tmp_count = this_round;
310
311 /* Advance state pointers and move on. */
312 this_round -= tmp_count;
313 p = HEADER_SIZE;
314 con_buf0 = con_buf + HEADER_SIZE;
315 /* If this_round >= 0, then p is even... */
316 } else if (p & 1) {
317 /* Skip first byte for output if start address is odd
318 * Update region sizes up/down depending on free
319 * space in buffer.
320 */
321 con_buf_start++;
322 if (this_round < CON_BUF_SIZE)
323 this_round++;
324 else
325 orig_count--;
326 }
327 if (this_round > 0) {
328 unsigned short *tmp_buf = (unsigned short *)con_buf0;
329
330 p -= HEADER_SIZE;
331 p /= 2;
332 col = p % maxcol;
333
334 org = screen_pos(vc, p, viewed);
335 p += maxcol - col;
336
337 /* Buffer has even length, so we can always copy
338 * character + attribute. We do not copy last byte
339 * to userspace if this_round is odd.
340 */
341 this_round = (this_round + 1) >> 1;
342
343 while (this_round) {
344 *tmp_buf++ = vcs_scr_readw(vc, org++);
345 this_round --;
346 if (++col == maxcol) {
347 org = screen_pos(vc, p, viewed);
348 col = 0;
349 p += maxcol;
350 }
351 }
352 }
353 }
354
355 /* Finally, release the console semaphore while we push
356 * all the data to userspace from our temporary buffer.
357 *
358 * AKPM: Even though it's a semaphore, we should drop it because
359 * the pagefault handling code may want to call printk().
360 */
361
362 console_unlock();
363 ret = copy_to_user(buf, con_buf_start, orig_count);
364 console_lock();
365
366 if (ret) {
367 read += (orig_count - ret);
368 ret = -EFAULT;
369 break;
370 }
371 buf += orig_count;
372 pos += orig_count;
373 read += orig_count;
374 count -= orig_count;
375 }
376 *ppos += read;
377 if (read)
378 ret = read;
379unlock_out:
380 console_unlock();
381 free_page((unsigned long) con_buf);
382 return ret;
383}
384
385static ssize_t
386vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
387{
388 struct inode *inode = file->f_path.dentry->d_inode;
389 unsigned int currcons = iminor(inode);
390 struct vc_data *vc;
391 long pos;
392 long attr, size, written;
393 char *con_buf0;
394 int col, maxcol, viewed;
395 u16 *org0 = NULL, *org = NULL;
396 size_t ret;
397 char *con_buf;
398
399 con_buf = (char *) __get_free_page(GFP_KERNEL);
400 if (!con_buf)
401 return -ENOMEM;
402
403 pos = *ppos;
404
405 /* Select the proper current console and verify
406 * sanity of the situation under the console lock.
407 */
408 console_lock();
409
410 attr = (currcons & 128);
411 ret = -ENXIO;
412 vc = vcs_vc(inode, &viewed);
413 if (!vc)
414 goto unlock_out;
415
416 size = vcs_size(inode);
417 ret = -EINVAL;
418 if (pos < 0 || pos > size)
419 goto unlock_out;
420 if (count > size - pos)
421 count = size - pos;
422 written = 0;
423 while (count) {
424 long this_round = count;
425 size_t orig_count;
426 long p;
427
428 if (this_round > CON_BUF_SIZE)
429 this_round = CON_BUF_SIZE;
430
431 /* Temporarily drop the console lock so that we can read
432 * in the write data from userspace safely.
433 */
434 console_unlock();
435 ret = copy_from_user(con_buf, buf, this_round);
436 console_lock();
437
438 if (ret) {
439 this_round -= ret;
440 if (!this_round) {
441 /* Abort loop if no data were copied. Otherwise
442 * fail with -EFAULT.
443 */
444 if (written)
445 break;
446 ret = -EFAULT;
447 goto unlock_out;
448 }
449 }
450
451 /* The vcs_size might have changed while we slept to grab
452 * the user buffer, so recheck.
453 * Return data written up to now on failure.
454 */
455 size = vcs_size(inode);
456 if (size < 0) {
457 if (written)
458 break;
459 ret = size;
460 goto unlock_out;
461 }
462 if (pos >= size)
463 break;
464 if (this_round > size - pos)
465 this_round = size - pos;
466
467 /* OK, now actually push the write to the console
468 * under the lock using the local kernel buffer.
469 */
470
471 con_buf0 = con_buf;
472 orig_count = this_round;
473 maxcol = vc->vc_cols;
474 p = pos;
475 if (!attr) {
476 org0 = org = screen_pos(vc, p, viewed);
477 col = p % maxcol;
478 p += maxcol - col;
479
480 while (this_round > 0) {
481 unsigned char c = *con_buf0++;
482
483 this_round--;
484 vcs_scr_writew(vc,
485 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
486 org++;
487 if (++col == maxcol) {
488 org = screen_pos(vc, p, viewed);
489 col = 0;
490 p += maxcol;
491 }
492 }
493 } else {
494 if (p < HEADER_SIZE) {
495 char header[HEADER_SIZE];
496
497 getconsxy(vc, header + 2);
498 while (p < HEADER_SIZE && this_round > 0) {
499 this_round--;
500 header[p++] = *con_buf0++;
501 }
502 if (!viewed)
503 putconsxy(vc, header + 2);
504 }
505 p -= HEADER_SIZE;
506 col = (p/2) % maxcol;
507 if (this_round > 0) {
508 org0 = org = screen_pos(vc, p/2, viewed);
509 if ((p & 1) && this_round > 0) {
510 char c;
511
512 this_round--;
513 c = *con_buf0++;
514#ifdef __BIG_ENDIAN
515 vcs_scr_writew(vc, c |
516 (vcs_scr_readw(vc, org) & 0xff00), org);
517#else
518 vcs_scr_writew(vc, (c << 8) |
519 (vcs_scr_readw(vc, org) & 0xff), org);
520#endif
521 org++;
522 p++;
523 if (++col == maxcol) {
524 org = screen_pos(vc, p/2, viewed);
525 col = 0;
526 }
527 }
528 p /= 2;
529 p += maxcol - col;
530 }
531 while (this_round > 1) {
532 unsigned short w;
533
534 w = get_unaligned(((unsigned short *)con_buf0));
535 vcs_scr_writew(vc, w, org++);
536 con_buf0 += 2;
537 this_round -= 2;
538 if (++col == maxcol) {
539 org = screen_pos(vc, p, viewed);
540 col = 0;
541 p += maxcol;
542 }
543 }
544 if (this_round > 0) {
545 unsigned char c;
546
547 c = *con_buf0++;
548#ifdef __BIG_ENDIAN
549 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
550#else
551 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
552#endif
553 }
554 }
555 count -= orig_count;
556 written += orig_count;
557 buf += orig_count;
558 pos += orig_count;
559 if (org0)
560 update_region(vc, (unsigned long)(org0), org - org0);
561 }
562 *ppos += written;
563 ret = written;
564 if (written)
565 vcs_scr_updated(vc);
566
567unlock_out:
568 console_unlock();
569 free_page((unsigned long) con_buf);
570 return ret;
571}
572
573static unsigned int
574vcs_poll(struct file *file, poll_table *wait)
575{
576 struct vcs_poll_data *poll = vcs_poll_data_get(file);
577 int ret = DEFAULT_POLLMASK|POLLERR|POLLPRI;
578
579 if (poll) {
580 poll_wait(file, &poll->waitq, wait);
581 if (poll->seen_last_update)
582 ret = DEFAULT_POLLMASK;
583 }
584 return ret;
585}
586
587static int
588vcs_fasync(int fd, struct file *file, int on)
589{
590 struct vcs_poll_data *poll = file->private_data;
591
592 if (!poll) {
593 /* don't allocate anything if all we want is disable fasync */
594 if (!on)
595 return 0;
596 poll = vcs_poll_data_get(file);
597 if (!poll)
598 return -ENOMEM;
599 }
600
601 return fasync_helper(fd, file, on, &poll->fasync);
602}
603
604static int
605vcs_open(struct inode *inode, struct file *filp)
606{
607 unsigned int currcons = iminor(inode) & 127;
608 int ret = 0;
609
610 tty_lock();
611 if(currcons && !vc_cons_allocated(currcons-1))
612 ret = -ENXIO;
613 tty_unlock();
614 return ret;
615}
616
617static int vcs_release(struct inode *inode, struct file *file)
618{
619 struct vcs_poll_data *poll = file->private_data;
620
621 if (poll)
622 vcs_poll_data_free(poll);
623 return 0;
624}
625
626static const struct file_operations vcs_fops = {
627 .llseek = vcs_lseek,
628 .read = vcs_read,
629 .write = vcs_write,
630 .poll = vcs_poll,
631 .fasync = vcs_fasync,
632 .open = vcs_open,
633 .release = vcs_release,
634};
635
636static struct class *vc_class;
637
638void vcs_make_sysfs(int index)
639{
640 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
641 "vcs%u", index + 1);
642 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
643 "vcsa%u", index + 1);
644}
645
646void vcs_remove_sysfs(int index)
647{
648 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
649 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
650}
651
652int __init vcs_init(void)
653{
654 unsigned int i;
655
656 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
657 panic("unable to get major %d for vcs device", VCS_MAJOR);
658 vc_class = class_create(THIS_MODULE, "vc");
659
660 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
661 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
662 for (i = 0; i < MIN_NR_CONSOLES; i++)
663 vcs_make_sysfs(i);
664 return 0;
665}
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
new file mode 100644
index 000000000000..b3915b7ad3e2
--- /dev/null
+++ b/drivers/tty/vt/vt.c
@@ -0,0 +1,4226 @@
1/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * Hopefully this will be a rather complete VT102 implementation.
7 *
8 * Beeping thanks to John T Kohl.
9 *
10 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
11 * Chars, and VT100 enhancements by Peter MacDonald.
12 *
13 * Copy and paste function by Andrew Haylett,
14 * some enhancements by Alessandro Rubini.
15 *
16 * Code to check for different video-cards mostly by Galen Hunt,
17 * <g-hunt@ee.utah.edu>
18 *
19 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
20 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
21 *
22 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
23 * Resizing of consoles, aeb, 940926
24 *
25 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
26 * <poe@daimi.aau.dk>
27 *
28 * User-defined bell sound, new setterm control sequences and printk
29 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
30 *
31 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
32 *
33 * Merge with the abstract console driver by Geert Uytterhoeven
34 * <geert@linux-m68k.org>, Jan 1997.
35 *
36 * Original m68k console driver modifications by
37 *
38 * - Arno Griffioen <arno@usn.nl>
39 * - David Carter <carter@cs.bris.ac.uk>
40 *
41 * The abstract console driver provides a generic interface for a text
42 * console. It supports VGA text mode, frame buffer based graphical consoles
43 * and special graphics processors that are only accessible through some
44 * registers (e.g. a TMS340x0 GSP).
45 *
46 * The interface to the hardware is specified using a special structure
47 * (struct consw) which contains function pointers to console operations
48 * (see <linux/console.h> for more information).
49 *
50 * Support for changeable cursor shape
51 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
52 *
53 * Ported to i386 and con_scrolldelta fixed
54 * by Emmanuel Marty <core@ggi-project.org>, April 1998
55 *
56 * Resurrected character buffers in videoram plus lots of other trickery
57 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
58 *
59 * Removed old-style timers, introduced console_timer, made timer
60 * deletion SMP-safe. 17Jun00, Andrew Morton
61 *
62 * Removed console_lock, enabled interrupts across all console operations
63 * 13 March 2001, Andrew Morton
64 *
65 * Fixed UTF-8 mode so alternate charset modes always work according
66 * to control sequences interpreted in do_con_trol function
67 * preserving backward VT100 semigraphics compatibility,
68 * malformed UTF sequences represented as sequences of replacement glyphs,
69 * original codes or '?' as a last resort if replacement glyph is undefined
70 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
71 */
72
73#include <linux/module.h>
74#include <linux/types.h>
75#include <linux/sched.h>
76#include <linux/tty.h>
77#include <linux/tty_flip.h>
78#include <linux/kernel.h>
79#include <linux/string.h>
80#include <linux/errno.h>
81#include <linux/kd.h>
82#include <linux/slab.h>
83#include <linux/major.h>
84#include <linux/mm.h>
85#include <linux/console.h>
86#include <linux/init.h>
87#include <linux/mutex.h>
88#include <linux/vt_kern.h>
89#include <linux/selection.h>
90#include <linux/tiocl.h>
91#include <linux/kbd_kern.h>
92#include <linux/consolemap.h>
93#include <linux/timer.h>
94#include <linux/interrupt.h>
95#include <linux/workqueue.h>
96#include <linux/pm.h>
97#include <linux/font.h>
98#include <linux/bitops.h>
99#include <linux/notifier.h>
100#include <linux/device.h>
101#include <linux/io.h>
102#include <asm/system.h>
103#include <linux/uaccess.h>
104#include <linux/kdb.h>
105#include <linux/ctype.h>
106
107#define MAX_NR_CON_DRIVER 16
108
109#define CON_DRIVER_FLAG_MODULE 1
110#define CON_DRIVER_FLAG_INIT 2
111#define CON_DRIVER_FLAG_ATTR 4
112
113struct con_driver {
114 const struct consw *con;
115 const char *desc;
116 struct device *dev;
117 int node;
118 int first;
119 int last;
120 int flag;
121};
122
123static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
124const struct consw *conswitchp;
125
126/* A bitmap for codes <32. A bit of 1 indicates that the code
127 * corresponding to that bit number invokes some special action
128 * (such as cursor movement) and should not be displayed as a
129 * glyph unless the disp_ctrl mode is explicitly enabled.
130 */
131#define CTRL_ACTION 0x0d00ff81
132#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
133
134/*
135 * Here is the default bell parameters: 750HZ, 1/8th of a second
136 */
137#define DEFAULT_BELL_PITCH 750
138#define DEFAULT_BELL_DURATION (HZ/8)
139
140struct vc vc_cons [MAX_NR_CONSOLES];
141
142#ifndef VT_SINGLE_DRIVER
143static const struct consw *con_driver_map[MAX_NR_CONSOLES];
144#endif
145
146static int con_open(struct tty_struct *, struct file *);
147static void vc_init(struct vc_data *vc, unsigned int rows,
148 unsigned int cols, int do_clear);
149static void gotoxy(struct vc_data *vc, int new_x, int new_y);
150static void save_cur(struct vc_data *vc);
151static void reset_terminal(struct vc_data *vc, int do_clear);
152static void con_flush_chars(struct tty_struct *tty);
153static int set_vesa_blanking(char __user *p);
154static void set_cursor(struct vc_data *vc);
155static void hide_cursor(struct vc_data *vc);
156static void console_callback(struct work_struct *ignored);
157static void blank_screen_t(unsigned long dummy);
158static void set_palette(struct vc_data *vc);
159
160static int printable; /* Is console ready for printing? */
161int default_utf8 = true;
162module_param(default_utf8, int, S_IRUGO | S_IWUSR);
163int global_cursor_default = -1;
164module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
165
166static int cur_default = CUR_DEFAULT;
167module_param(cur_default, int, S_IRUGO | S_IWUSR);
168
169/*
170 * ignore_poke: don't unblank the screen when things are typed. This is
171 * mainly for the privacy of braille terminal users.
172 */
173static int ignore_poke;
174
175int do_poke_blanked_console;
176int console_blanked;
177
178static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
179static int vesa_off_interval;
180static int blankinterval = 10*60;
181core_param(consoleblank, blankinterval, int, 0444);
182
183static DECLARE_WORK(console_work, console_callback);
184
185/*
186 * fg_console is the current virtual console,
187 * last_console is the last used one,
188 * want_console is the console we want to switch to,
189 * saved_* variants are for save/restore around kernel debugger enter/leave
190 */
191int fg_console;
192int last_console;
193int want_console = -1;
194static int saved_fg_console;
195static int saved_last_console;
196static int saved_want_console;
197static int saved_vc_mode;
198static int saved_console_blanked;
199
200/*
201 * For each existing display, we have a pointer to console currently visible
202 * on that display, allowing consoles other than fg_console to be refreshed
203 * appropriately. Unless the low-level driver supplies its own display_fg
204 * variable, we use this one for the "master display".
205 */
206static struct vc_data *master_display_fg;
207
208/*
209 * Unfortunately, we need to delay tty echo when we're currently writing to the
210 * console since the code is (and always was) not re-entrant, so we schedule
211 * all flip requests to process context with schedule-task() and run it from
212 * console_callback().
213 */
214
215/*
216 * For the same reason, we defer scrollback to the console callback.
217 */
218static int scrollback_delta;
219
220/*
221 * Hook so that the power management routines can (un)blank
222 * the console on our behalf.
223 */
224int (*console_blank_hook)(int);
225
226static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
227static int blank_state;
228static int blank_timer_expired;
229enum {
230 blank_off = 0,
231 blank_normal_wait,
232 blank_vesa_wait,
233};
234
235/*
236 * /sys/class/tty/tty0/
237 *
238 * the attribute 'active' contains the name of the current vc
239 * console and it supports poll() to detect vc switches
240 */
241static struct device *tty0dev;
242
243/*
244 * Notifier list for console events.
245 */
246static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
247
248int register_vt_notifier(struct notifier_block *nb)
249{
250 return atomic_notifier_chain_register(&vt_notifier_list, nb);
251}
252EXPORT_SYMBOL_GPL(register_vt_notifier);
253
254int unregister_vt_notifier(struct notifier_block *nb)
255{
256 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
257}
258EXPORT_SYMBOL_GPL(unregister_vt_notifier);
259
260static void notify_write(struct vc_data *vc, unsigned int unicode)
261{
262 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
263 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
264}
265
266static void notify_update(struct vc_data *vc)
267{
268 struct vt_notifier_param param = { .vc = vc };
269 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
270}
271/*
272 * Low-Level Functions
273 */
274
275#define IS_FG(vc) ((vc)->vc_num == fg_console)
276
277#ifdef VT_BUF_VRAM_ONLY
278#define DO_UPDATE(vc) 0
279#else
280#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
281#endif
282
283static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
284{
285 unsigned short *p;
286
287 if (!viewed)
288 p = (unsigned short *)(vc->vc_origin + offset);
289 else if (!vc->vc_sw->con_screen_pos)
290 p = (unsigned short *)(vc->vc_visible_origin + offset);
291 else
292 p = vc->vc_sw->con_screen_pos(vc, offset);
293 return p;
294}
295
296/* Called from the keyboard irq path.. */
297static inline void scrolldelta(int lines)
298{
299 /* FIXME */
300 /* scrolldelta needs some kind of consistency lock, but the BKL was
301 and still is not protecting versus the scheduled back end */
302 scrollback_delta += lines;
303 schedule_console_callback();
304}
305
306void schedule_console_callback(void)
307{
308 schedule_work(&console_work);
309}
310
311static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
312{
313 unsigned short *d, *s;
314
315 if (t+nr >= b)
316 nr = b - t - 1;
317 if (b > vc->vc_rows || t >= b || nr < 1)
318 return;
319 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
320 return;
321 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
322 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
323 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
324 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
325 vc->vc_size_row * nr);
326}
327
328static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
329{
330 unsigned short *s;
331 unsigned int step;
332
333 if (t+nr >= b)
334 nr = b - t - 1;
335 if (b > vc->vc_rows || t >= b || nr < 1)
336 return;
337 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
338 return;
339 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
340 step = vc->vc_cols * nr;
341 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
342 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
343}
344
345static void do_update_region(struct vc_data *vc, unsigned long start, int count)
346{
347#ifndef VT_BUF_VRAM_ONLY
348 unsigned int xx, yy, offset;
349 u16 *p;
350
351 p = (u16 *) start;
352 if (!vc->vc_sw->con_getxy) {
353 offset = (start - vc->vc_origin) / 2;
354 xx = offset % vc->vc_cols;
355 yy = offset / vc->vc_cols;
356 } else {
357 int nxx, nyy;
358 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
359 xx = nxx; yy = nyy;
360 }
361 for(;;) {
362 u16 attrib = scr_readw(p) & 0xff00;
363 int startx = xx;
364 u16 *q = p;
365 while (xx < vc->vc_cols && count) {
366 if (attrib != (scr_readw(p) & 0xff00)) {
367 if (p > q)
368 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
369 startx = xx;
370 q = p;
371 attrib = scr_readw(p) & 0xff00;
372 }
373 p++;
374 xx++;
375 count--;
376 }
377 if (p > q)
378 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
379 if (!count)
380 break;
381 xx = 0;
382 yy++;
383 if (vc->vc_sw->con_getxy) {
384 p = (u16 *)start;
385 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
386 }
387 }
388#endif
389}
390
391void update_region(struct vc_data *vc, unsigned long start, int count)
392{
393 WARN_CONSOLE_UNLOCKED();
394
395 if (DO_UPDATE(vc)) {
396 hide_cursor(vc);
397 do_update_region(vc, start, count);
398 set_cursor(vc);
399 }
400}
401
402/* Structure of attributes is hardware-dependent */
403
404static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
405 u8 _underline, u8 _reverse, u8 _italic)
406{
407 if (vc->vc_sw->con_build_attr)
408 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
409 _blink, _underline, _reverse, _italic);
410
411#ifndef VT_BUF_VRAM_ONLY
412/*
413 * ++roman: I completely changed the attribute format for monochrome
414 * mode (!can_do_color). The formerly used MDA (monochrome display
415 * adapter) format didn't allow the combination of certain effects.
416 * Now the attribute is just a bit vector:
417 * Bit 0..1: intensity (0..2)
418 * Bit 2 : underline
419 * Bit 3 : reverse
420 * Bit 7 : blink
421 */
422 {
423 u8 a = _color;
424 if (!vc->vc_can_do_color)
425 return _intensity |
426 (_italic ? 2 : 0) |
427 (_underline ? 4 : 0) |
428 (_reverse ? 8 : 0) |
429 (_blink ? 0x80 : 0);
430 if (_italic)
431 a = (a & 0xF0) | vc->vc_itcolor;
432 else if (_underline)
433 a = (a & 0xf0) | vc->vc_ulcolor;
434 else if (_intensity == 0)
435 a = (a & 0xf0) | vc->vc_ulcolor;
436 if (_reverse)
437 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
438 if (_blink)
439 a ^= 0x80;
440 if (_intensity == 2)
441 a ^= 0x08;
442 if (vc->vc_hi_font_mask == 0x100)
443 a <<= 1;
444 return a;
445 }
446#else
447 return 0;
448#endif
449}
450
451static void update_attr(struct vc_data *vc)
452{
453 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
454 vc->vc_blink, vc->vc_underline,
455 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
456 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
457}
458
459/* Note: inverting the screen twice should revert to the original state */
460void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
461{
462 unsigned short *p;
463
464 WARN_CONSOLE_UNLOCKED();
465
466 count /= 2;
467 p = screenpos(vc, offset, viewed);
468 if (vc->vc_sw->con_invert_region)
469 vc->vc_sw->con_invert_region(vc, p, count);
470#ifndef VT_BUF_VRAM_ONLY
471 else {
472 u16 *q = p;
473 int cnt = count;
474 u16 a;
475
476 if (!vc->vc_can_do_color) {
477 while (cnt--) {
478 a = scr_readw(q);
479 a ^= 0x0800;
480 scr_writew(a, q);
481 q++;
482 }
483 } else if (vc->vc_hi_font_mask == 0x100) {
484 while (cnt--) {
485 a = scr_readw(q);
486 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
487 scr_writew(a, q);
488 q++;
489 }
490 } else {
491 while (cnt--) {
492 a = scr_readw(q);
493 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
494 scr_writew(a, q);
495 q++;
496 }
497 }
498 }
499#endif
500 if (DO_UPDATE(vc))
501 do_update_region(vc, (unsigned long) p, count);
502}
503
504/* used by selection: complement pointer position */
505void complement_pos(struct vc_data *vc, int offset)
506{
507 static int old_offset = -1;
508 static unsigned short old;
509 static unsigned short oldx, oldy;
510
511 WARN_CONSOLE_UNLOCKED();
512
513 if (old_offset != -1 && old_offset >= 0 &&
514 old_offset < vc->vc_screenbuf_size) {
515 scr_writew(old, screenpos(vc, old_offset, 1));
516 if (DO_UPDATE(vc))
517 vc->vc_sw->con_putc(vc, old, oldy, oldx);
518 }
519
520 old_offset = offset;
521
522 if (offset != -1 && offset >= 0 &&
523 offset < vc->vc_screenbuf_size) {
524 unsigned short new;
525 unsigned short *p;
526 p = screenpos(vc, offset, 1);
527 old = scr_readw(p);
528 new = old ^ vc->vc_complement_mask;
529 scr_writew(new, p);
530 if (DO_UPDATE(vc)) {
531 oldx = (offset >> 1) % vc->vc_cols;
532 oldy = (offset >> 1) / vc->vc_cols;
533 vc->vc_sw->con_putc(vc, new, oldy, oldx);
534 }
535 }
536
537}
538
539static void insert_char(struct vc_data *vc, unsigned int nr)
540{
541 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
542
543 p = q + vc->vc_cols - nr - vc->vc_x;
544 while (--p >= q)
545 scr_writew(scr_readw(p), p + nr);
546 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
547 vc->vc_need_wrap = 0;
548 if (DO_UPDATE(vc)) {
549 unsigned short oldattr = vc->vc_attr;
550 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
551 vc->vc_cols - vc->vc_x - nr);
552 vc->vc_attr = vc->vc_video_erase_char >> 8;
553 while (nr--)
554 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
555 vc->vc_attr = oldattr;
556 }
557}
558
559static void delete_char(struct vc_data *vc, unsigned int nr)
560{
561 unsigned int i = vc->vc_x;
562 unsigned short *p = (unsigned short *)vc->vc_pos;
563
564 while (++i <= vc->vc_cols - nr) {
565 scr_writew(scr_readw(p+nr), p);
566 p++;
567 }
568 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
569 vc->vc_need_wrap = 0;
570 if (DO_UPDATE(vc)) {
571 unsigned short oldattr = vc->vc_attr;
572 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
573 vc->vc_cols - vc->vc_x - nr);
574 vc->vc_attr = vc->vc_video_erase_char >> 8;
575 while (nr--)
576 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
577 vc->vc_cols - 1 - nr);
578 vc->vc_attr = oldattr;
579 }
580}
581
582static int softcursor_original;
583
584static void add_softcursor(struct vc_data *vc)
585{
586 int i = scr_readw((u16 *) vc->vc_pos);
587 u32 type = vc->vc_cursor_type;
588
589 if (! (type & 0x10)) return;
590 if (softcursor_original != -1) return;
591 softcursor_original = i;
592 i |= ((type >> 8) & 0xff00 );
593 i ^= ((type) & 0xff00 );
594 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
595 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
596 scr_writew(i, (u16 *) vc->vc_pos);
597 if (DO_UPDATE(vc))
598 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
599}
600
601static void hide_softcursor(struct vc_data *vc)
602{
603 if (softcursor_original != -1) {
604 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
605 if (DO_UPDATE(vc))
606 vc->vc_sw->con_putc(vc, softcursor_original,
607 vc->vc_y, vc->vc_x);
608 softcursor_original = -1;
609 }
610}
611
612static void hide_cursor(struct vc_data *vc)
613{
614 if (vc == sel_cons)
615 clear_selection();
616 vc->vc_sw->con_cursor(vc, CM_ERASE);
617 hide_softcursor(vc);
618}
619
620static void set_cursor(struct vc_data *vc)
621{
622 if (!IS_FG(vc) || console_blanked ||
623 vc->vc_mode == KD_GRAPHICS)
624 return;
625 if (vc->vc_deccm) {
626 if (vc == sel_cons)
627 clear_selection();
628 add_softcursor(vc);
629 if ((vc->vc_cursor_type & 0x0f) != 1)
630 vc->vc_sw->con_cursor(vc, CM_DRAW);
631 } else
632 hide_cursor(vc);
633}
634
635static void set_origin(struct vc_data *vc)
636{
637 WARN_CONSOLE_UNLOCKED();
638
639 if (!CON_IS_VISIBLE(vc) ||
640 !vc->vc_sw->con_set_origin ||
641 !vc->vc_sw->con_set_origin(vc))
642 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
643 vc->vc_visible_origin = vc->vc_origin;
644 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
645 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
646}
647
648static inline void save_screen(struct vc_data *vc)
649{
650 WARN_CONSOLE_UNLOCKED();
651
652 if (vc->vc_sw->con_save_screen)
653 vc->vc_sw->con_save_screen(vc);
654}
655
656/*
657 * Redrawing of screen
658 */
659
660static void clear_buffer_attributes(struct vc_data *vc)
661{
662 unsigned short *p = (unsigned short *)vc->vc_origin;
663 int count = vc->vc_screenbuf_size / 2;
664 int mask = vc->vc_hi_font_mask | 0xff;
665
666 for (; count > 0; count--, p++) {
667 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
668 }
669}
670
671void redraw_screen(struct vc_data *vc, int is_switch)
672{
673 int redraw = 0;
674
675 WARN_CONSOLE_UNLOCKED();
676
677 if (!vc) {
678 /* strange ... */
679 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
680 return;
681 }
682
683 if (is_switch) {
684 struct vc_data *old_vc = vc_cons[fg_console].d;
685 if (old_vc == vc)
686 return;
687 if (!CON_IS_VISIBLE(vc))
688 redraw = 1;
689 *vc->vc_display_fg = vc;
690 fg_console = vc->vc_num;
691 hide_cursor(old_vc);
692 if (!CON_IS_VISIBLE(old_vc)) {
693 save_screen(old_vc);
694 set_origin(old_vc);
695 }
696 if (tty0dev)
697 sysfs_notify(&tty0dev->kobj, NULL, "active");
698 } else {
699 hide_cursor(vc);
700 redraw = 1;
701 }
702
703 if (redraw) {
704 int update;
705 int old_was_color = vc->vc_can_do_color;
706
707 set_origin(vc);
708 update = vc->vc_sw->con_switch(vc);
709 set_palette(vc);
710 /*
711 * If console changed from mono<->color, the best we can do
712 * is to clear the buffer attributes. As it currently stands,
713 * rebuilding new attributes from the old buffer is not doable
714 * without overly complex code.
715 */
716 if (old_was_color != vc->vc_can_do_color) {
717 update_attr(vc);
718 clear_buffer_attributes(vc);
719 }
720
721 /* Forcibly update if we're panicing */
722 if ((update && vc->vc_mode != KD_GRAPHICS) ||
723 vt_force_oops_output(vc))
724 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
725 }
726 set_cursor(vc);
727 if (is_switch) {
728 set_leds();
729 compute_shiftstate();
730 notify_update(vc);
731 }
732}
733
734/*
735 * Allocation, freeing and resizing of VTs.
736 */
737
738int vc_cons_allocated(unsigned int i)
739{
740 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
741}
742
743static void visual_init(struct vc_data *vc, int num, int init)
744{
745 /* ++Geert: vc->vc_sw->con_init determines console size */
746 if (vc->vc_sw)
747 module_put(vc->vc_sw->owner);
748 vc->vc_sw = conswitchp;
749#ifndef VT_SINGLE_DRIVER
750 if (con_driver_map[num])
751 vc->vc_sw = con_driver_map[num];
752#endif
753 __module_get(vc->vc_sw->owner);
754 vc->vc_num = num;
755 vc->vc_display_fg = &master_display_fg;
756 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
757 vc->vc_uni_pagedir = 0;
758 vc->vc_hi_font_mask = 0;
759 vc->vc_complement_mask = 0;
760 vc->vc_can_do_color = 0;
761 vc->vc_panic_force_write = false;
762 vc->vc_sw->con_init(vc, init);
763 if (!vc->vc_complement_mask)
764 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
765 vc->vc_s_complement_mask = vc->vc_complement_mask;
766 vc->vc_size_row = vc->vc_cols << 1;
767 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
768}
769
770int vc_allocate(unsigned int currcons) /* return 0 on success */
771{
772 WARN_CONSOLE_UNLOCKED();
773
774 if (currcons >= MAX_NR_CONSOLES)
775 return -ENXIO;
776 if (!vc_cons[currcons].d) {
777 struct vc_data *vc;
778 struct vt_notifier_param param;
779
780 /* prevent users from taking too much memory */
781 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
782 return -EPERM;
783
784 /* due to the granularity of kmalloc, we waste some memory here */
785 /* the alloc is done in two steps, to optimize the common situation
786 of a 25x80 console (structsize=216, screenbuf_size=4000) */
787 /* although the numbers above are not valid since long ago, the
788 point is still up-to-date and the comment still has its value
789 even if only as a historical artifact. --mj, July 1998 */
790 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
791 if (!vc)
792 return -ENOMEM;
793 vc_cons[currcons].d = vc;
794 tty_port_init(&vc->port);
795 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
796 visual_init(vc, currcons, 1);
797 if (!*vc->vc_uni_pagedir_loc)
798 con_set_default_unimap(vc);
799 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
800 if (!vc->vc_screenbuf) {
801 kfree(vc);
802 vc_cons[currcons].d = NULL;
803 return -ENOMEM;
804 }
805
806 /* If no drivers have overridden us and the user didn't pass a
807 boot option, default to displaying the cursor */
808 if (global_cursor_default == -1)
809 global_cursor_default = 1;
810
811 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
812 vcs_make_sysfs(currcons);
813 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
814 }
815 return 0;
816}
817
818static inline int resize_screen(struct vc_data *vc, int width, int height,
819 int user)
820{
821 /* Resizes the resolution of the display adapater */
822 int err = 0;
823
824 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
825 err = vc->vc_sw->con_resize(vc, width, height, user);
826
827 return err;
828}
829
830/*
831 * Change # of rows and columns (0 means unchanged/the size of fg_console)
832 * [this is to be used together with some user program
833 * like resize that changes the hardware videomode]
834 */
835#define VC_RESIZE_MAXCOL (32767)
836#define VC_RESIZE_MAXROW (32767)
837
838/**
839 * vc_do_resize - resizing method for the tty
840 * @tty: tty being resized
841 * @real_tty: real tty (different to tty if a pty/tty pair)
842 * @vc: virtual console private data
843 * @cols: columns
844 * @lines: lines
845 *
846 * Resize a virtual console, clipping according to the actual constraints.
847 * If the caller passes a tty structure then update the termios winsize
848 * information and perform any necessary signal handling.
849 *
850 * Caller must hold the console semaphore. Takes the termios mutex and
851 * ctrl_lock of the tty IFF a tty is passed.
852 */
853
854static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
855 unsigned int cols, unsigned int lines)
856{
857 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
858 unsigned long end;
859 unsigned int old_rows, old_row_size;
860 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
861 unsigned int user;
862 unsigned short *newscreen;
863
864 WARN_CONSOLE_UNLOCKED();
865
866 if (!vc)
867 return -ENXIO;
868
869 user = vc->vc_resize_user;
870 vc->vc_resize_user = 0;
871
872 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
873 return -EINVAL;
874
875 new_cols = (cols ? cols : vc->vc_cols);
876 new_rows = (lines ? lines : vc->vc_rows);
877 new_row_size = new_cols << 1;
878 new_screen_size = new_row_size * new_rows;
879
880 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
881 return 0;
882
883 newscreen = kmalloc(new_screen_size, GFP_USER);
884 if (!newscreen)
885 return -ENOMEM;
886
887 old_rows = vc->vc_rows;
888 old_row_size = vc->vc_size_row;
889
890 err = resize_screen(vc, new_cols, new_rows, user);
891 if (err) {
892 kfree(newscreen);
893 return err;
894 }
895
896 vc->vc_rows = new_rows;
897 vc->vc_cols = new_cols;
898 vc->vc_size_row = new_row_size;
899 vc->vc_screenbuf_size = new_screen_size;
900
901 rlth = min(old_row_size, new_row_size);
902 rrem = new_row_size - rlth;
903 old_origin = vc->vc_origin;
904 new_origin = (long) newscreen;
905 new_scr_end = new_origin + new_screen_size;
906
907 if (vc->vc_y > new_rows) {
908 if (old_rows - vc->vc_y < new_rows) {
909 /*
910 * Cursor near the bottom, copy contents from the
911 * bottom of buffer
912 */
913 old_origin += (old_rows - new_rows) * old_row_size;
914 } else {
915 /*
916 * Cursor is in no man's land, copy 1/2 screenful
917 * from the top and bottom of cursor position
918 */
919 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
920 }
921 }
922
923 end = old_origin + old_row_size * min(old_rows, new_rows);
924
925 update_attr(vc);
926
927 while (old_origin < end) {
928 scr_memcpyw((unsigned short *) new_origin,
929 (unsigned short *) old_origin, rlth);
930 if (rrem)
931 scr_memsetw((void *)(new_origin + rlth),
932 vc->vc_video_erase_char, rrem);
933 old_origin += old_row_size;
934 new_origin += new_row_size;
935 }
936 if (new_scr_end > new_origin)
937 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
938 new_scr_end - new_origin);
939 kfree(vc->vc_screenbuf);
940 vc->vc_screenbuf = newscreen;
941 vc->vc_screenbuf_size = new_screen_size;
942 set_origin(vc);
943
944 /* do part of a reset_terminal() */
945 vc->vc_top = 0;
946 vc->vc_bottom = vc->vc_rows;
947 gotoxy(vc, vc->vc_x, vc->vc_y);
948 save_cur(vc);
949
950 if (tty) {
951 /* Rewrite the requested winsize data with the actual
952 resulting sizes */
953 struct winsize ws;
954 memset(&ws, 0, sizeof(ws));
955 ws.ws_row = vc->vc_rows;
956 ws.ws_col = vc->vc_cols;
957 ws.ws_ypixel = vc->vc_scan_lines;
958 tty_do_resize(tty, &ws);
959 }
960
961 if (CON_IS_VISIBLE(vc))
962 update_screen(vc);
963 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
964 return err;
965}
966
967/**
968 * vc_resize - resize a VT
969 * @vc: virtual console
970 * @cols: columns
971 * @rows: rows
972 *
973 * Resize a virtual console as seen from the console end of things. We
974 * use the common vc_do_resize methods to update the structures. The
975 * caller must hold the console sem to protect console internals and
976 * vc->port.tty
977 */
978
979int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
980{
981 return vc_do_resize(vc->port.tty, vc, cols, rows);
982}
983
984/**
985 * vt_resize - resize a VT
986 * @tty: tty to resize
987 * @ws: winsize attributes
988 *
989 * Resize a virtual terminal. This is called by the tty layer as we
990 * register our own handler for resizing. The mutual helper does all
991 * the actual work.
992 *
993 * Takes the console sem and the called methods then take the tty
994 * termios_mutex and the tty ctrl_lock in that order.
995 */
996static int vt_resize(struct tty_struct *tty, struct winsize *ws)
997{
998 struct vc_data *vc = tty->driver_data;
999 int ret;
1000
1001 console_lock();
1002 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1003 console_unlock();
1004 return ret;
1005}
1006
1007void vc_deallocate(unsigned int currcons)
1008{
1009 WARN_CONSOLE_UNLOCKED();
1010
1011 if (vc_cons_allocated(currcons)) {
1012 struct vc_data *vc = vc_cons[currcons].d;
1013 struct vt_notifier_param param = { .vc = vc };
1014
1015 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1016 vcs_remove_sysfs(currcons);
1017 vc->vc_sw->con_deinit(vc);
1018 put_pid(vc->vt_pid);
1019 module_put(vc->vc_sw->owner);
1020 kfree(vc->vc_screenbuf);
1021 if (currcons >= MIN_NR_CONSOLES)
1022 kfree(vc);
1023 vc_cons[currcons].d = NULL;
1024 }
1025}
1026
1027/*
1028 * VT102 emulator
1029 */
1030
1031#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1032#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1033#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1034
1035#define decarm VC_REPEAT
1036#define decckm VC_CKMODE
1037#define kbdapplic VC_APPLIC
1038#define lnm VC_CRLF
1039
1040/*
1041 * this is what the terminal answers to a ESC-Z or csi0c query.
1042 */
1043#define VT100ID "\033[?1;2c"
1044#define VT102ID "\033[?6c"
1045
1046unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1047 8,12,10,14, 9,13,11,15 };
1048
1049/* the default colour table, for VGA+ colour systems */
1050int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1051 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1052int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1053 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1054int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1055 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1056
1057module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1058module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1059module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1060
1061/*
1062 * gotoxy() must verify all boundaries, because the arguments
1063 * might also be negative. If the given position is out of
1064 * bounds, the cursor is placed at the nearest margin.
1065 */
1066static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1067{
1068 int min_y, max_y;
1069
1070 if (new_x < 0)
1071 vc->vc_x = 0;
1072 else {
1073 if (new_x >= vc->vc_cols)
1074 vc->vc_x = vc->vc_cols - 1;
1075 else
1076 vc->vc_x = new_x;
1077 }
1078
1079 if (vc->vc_decom) {
1080 min_y = vc->vc_top;
1081 max_y = vc->vc_bottom;
1082 } else {
1083 min_y = 0;
1084 max_y = vc->vc_rows;
1085 }
1086 if (new_y < min_y)
1087 vc->vc_y = min_y;
1088 else if (new_y >= max_y)
1089 vc->vc_y = max_y - 1;
1090 else
1091 vc->vc_y = new_y;
1092 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1093 vc->vc_need_wrap = 0;
1094}
1095
1096/* for absolute user moves, when decom is set */
1097static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1098{
1099 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1100}
1101
1102void scrollback(struct vc_data *vc, int lines)
1103{
1104 if (!lines)
1105 lines = vc->vc_rows / 2;
1106 scrolldelta(-lines);
1107}
1108
1109void scrollfront(struct vc_data *vc, int lines)
1110{
1111 if (!lines)
1112 lines = vc->vc_rows / 2;
1113 scrolldelta(lines);
1114}
1115
1116static void lf(struct vc_data *vc)
1117{
1118 /* don't scroll if above bottom of scrolling region, or
1119 * if below scrolling region
1120 */
1121 if (vc->vc_y + 1 == vc->vc_bottom)
1122 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1123 else if (vc->vc_y < vc->vc_rows - 1) {
1124 vc->vc_y++;
1125 vc->vc_pos += vc->vc_size_row;
1126 }
1127 vc->vc_need_wrap = 0;
1128 notify_write(vc, '\n');
1129}
1130
1131static void ri(struct vc_data *vc)
1132{
1133 /* don't scroll if below top of scrolling region, or
1134 * if above scrolling region
1135 */
1136 if (vc->vc_y == vc->vc_top)
1137 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1138 else if (vc->vc_y > 0) {
1139 vc->vc_y--;
1140 vc->vc_pos -= vc->vc_size_row;
1141 }
1142 vc->vc_need_wrap = 0;
1143}
1144
1145static inline void cr(struct vc_data *vc)
1146{
1147 vc->vc_pos -= vc->vc_x << 1;
1148 vc->vc_need_wrap = vc->vc_x = 0;
1149 notify_write(vc, '\r');
1150}
1151
1152static inline void bs(struct vc_data *vc)
1153{
1154 if (vc->vc_x) {
1155 vc->vc_pos -= 2;
1156 vc->vc_x--;
1157 vc->vc_need_wrap = 0;
1158 notify_write(vc, '\b');
1159 }
1160}
1161
1162static inline void del(struct vc_data *vc)
1163{
1164 /* ignored */
1165}
1166
1167static void csi_J(struct vc_data *vc, int vpar)
1168{
1169 unsigned int count;
1170 unsigned short * start;
1171
1172 switch (vpar) {
1173 case 0: /* erase from cursor to end of display */
1174 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1175 start = (unsigned short *)vc->vc_pos;
1176 if (DO_UPDATE(vc)) {
1177 /* do in two stages */
1178 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1179 vc->vc_cols - vc->vc_x);
1180 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1181 vc->vc_rows - vc->vc_y - 1,
1182 vc->vc_cols);
1183 }
1184 break;
1185 case 1: /* erase from start to cursor */
1186 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1187 start = (unsigned short *)vc->vc_origin;
1188 if (DO_UPDATE(vc)) {
1189 /* do in two stages */
1190 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1191 vc->vc_cols);
1192 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1193 vc->vc_x + 1);
1194 }
1195 break;
1196 case 3: /* erase scroll-back buffer (and whole display) */
1197 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1198 vc->vc_screenbuf_size >> 1);
1199 set_origin(vc);
1200 if (CON_IS_VISIBLE(vc))
1201 update_screen(vc);
1202 /* fall through */
1203 case 2: /* erase whole display */
1204 count = vc->vc_cols * vc->vc_rows;
1205 start = (unsigned short *)vc->vc_origin;
1206 if (DO_UPDATE(vc))
1207 vc->vc_sw->con_clear(vc, 0, 0,
1208 vc->vc_rows,
1209 vc->vc_cols);
1210 break;
1211 default:
1212 return;
1213 }
1214 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1215 vc->vc_need_wrap = 0;
1216}
1217
1218static void csi_K(struct vc_data *vc, int vpar)
1219{
1220 unsigned int count;
1221 unsigned short * start;
1222
1223 switch (vpar) {
1224 case 0: /* erase from cursor to end of line */
1225 count = vc->vc_cols - vc->vc_x;
1226 start = (unsigned short *)vc->vc_pos;
1227 if (DO_UPDATE(vc))
1228 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1229 vc->vc_cols - vc->vc_x);
1230 break;
1231 case 1: /* erase from start of line to cursor */
1232 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1233 count = vc->vc_x + 1;
1234 if (DO_UPDATE(vc))
1235 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1236 vc->vc_x + 1);
1237 break;
1238 case 2: /* erase whole line */
1239 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1240 count = vc->vc_cols;
1241 if (DO_UPDATE(vc))
1242 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1243 vc->vc_cols);
1244 break;
1245 default:
1246 return;
1247 }
1248 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1249 vc->vc_need_wrap = 0;
1250}
1251
1252static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1253{ /* not vt100? */
1254 int count;
1255
1256 if (!vpar)
1257 vpar++;
1258 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1259
1260 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1261 if (DO_UPDATE(vc))
1262 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1263 vc->vc_need_wrap = 0;
1264}
1265
1266static void default_attr(struct vc_data *vc)
1267{
1268 vc->vc_intensity = 1;
1269 vc->vc_italic = 0;
1270 vc->vc_underline = 0;
1271 vc->vc_reverse = 0;
1272 vc->vc_blink = 0;
1273 vc->vc_color = vc->vc_def_color;
1274}
1275
1276/* console_lock is held */
1277static void csi_m(struct vc_data *vc)
1278{
1279 int i;
1280
1281 for (i = 0; i <= vc->vc_npar; i++)
1282 switch (vc->vc_par[i]) {
1283 case 0: /* all attributes off */
1284 default_attr(vc);
1285 break;
1286 case 1:
1287 vc->vc_intensity = 2;
1288 break;
1289 case 2:
1290 vc->vc_intensity = 0;
1291 break;
1292 case 3:
1293 vc->vc_italic = 1;
1294 break;
1295 case 4:
1296 vc->vc_underline = 1;
1297 break;
1298 case 5:
1299 vc->vc_blink = 1;
1300 break;
1301 case 7:
1302 vc->vc_reverse = 1;
1303 break;
1304 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1305 * Select primary font, don't display
1306 * control chars if defined, don't set
1307 * bit 8 on output.
1308 */
1309 vc->vc_translate = set_translate(vc->vc_charset == 0
1310 ? vc->vc_G0_charset
1311 : vc->vc_G1_charset, vc);
1312 vc->vc_disp_ctrl = 0;
1313 vc->vc_toggle_meta = 0;
1314 break;
1315 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1316 * Select first alternate font, lets
1317 * chars < 32 be displayed as ROM chars.
1318 */
1319 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1320 vc->vc_disp_ctrl = 1;
1321 vc->vc_toggle_meta = 0;
1322 break;
1323 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1324 * Select second alternate font, toggle
1325 * high bit before displaying as ROM char.
1326 */
1327 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1328 vc->vc_disp_ctrl = 1;
1329 vc->vc_toggle_meta = 1;
1330 break;
1331 case 21:
1332 case 22:
1333 vc->vc_intensity = 1;
1334 break;
1335 case 23:
1336 vc->vc_italic = 0;
1337 break;
1338 case 24:
1339 vc->vc_underline = 0;
1340 break;
1341 case 25:
1342 vc->vc_blink = 0;
1343 break;
1344 case 27:
1345 vc->vc_reverse = 0;
1346 break;
1347 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1348 * Enables underscore, white foreground
1349 * with white underscore (Linux - use
1350 * default foreground).
1351 */
1352 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1353 vc->vc_underline = 1;
1354 break;
1355 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1356 * Disable underline option.
1357 * Reset colour to default? It did this
1358 * before...
1359 */
1360 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1361 vc->vc_underline = 0;
1362 break;
1363 case 49:
1364 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1365 break;
1366 default:
1367 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1368 vc->vc_color = color_table[vc->vc_par[i] - 30]
1369 | (vc->vc_color & 0xf0);
1370 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1371 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1372 | (vc->vc_color & 0x0f);
1373 break;
1374 }
1375 update_attr(vc);
1376}
1377
1378static void respond_string(const char *p, struct tty_struct *tty)
1379{
1380 while (*p) {
1381 tty_insert_flip_char(tty, *p, 0);
1382 p++;
1383 }
1384 con_schedule_flip(tty);
1385}
1386
1387static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1388{
1389 char buf[40];
1390
1391 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1392 respond_string(buf, tty);
1393}
1394
1395static inline void status_report(struct tty_struct *tty)
1396{
1397 respond_string("\033[0n", tty); /* Terminal ok */
1398}
1399
1400static inline void respond_ID(struct tty_struct * tty)
1401{
1402 respond_string(VT102ID, tty);
1403}
1404
1405void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1406{
1407 char buf[8];
1408
1409 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1410 (char)('!' + mry));
1411 respond_string(buf, tty);
1412}
1413
1414/* invoked via ioctl(TIOCLINUX) and through set_selection */
1415int mouse_reporting(void)
1416{
1417 return vc_cons[fg_console].d->vc_report_mouse;
1418}
1419
1420/* console_lock is held */
1421static void set_mode(struct vc_data *vc, int on_off)
1422{
1423 int i;
1424
1425 for (i = 0; i <= vc->vc_npar; i++)
1426 if (vc->vc_ques) {
1427 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1428 case 1: /* Cursor keys send ^[Ox/^[[x */
1429 if (on_off)
1430 set_kbd(vc, decckm);
1431 else
1432 clr_kbd(vc, decckm);
1433 break;
1434 case 3: /* 80/132 mode switch unimplemented */
1435 vc->vc_deccolm = on_off;
1436#if 0
1437 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1438 /* this alone does not suffice; some user mode
1439 utility has to change the hardware regs */
1440#endif
1441 break;
1442 case 5: /* Inverted screen on/off */
1443 if (vc->vc_decscnm != on_off) {
1444 vc->vc_decscnm = on_off;
1445 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1446 update_attr(vc);
1447 }
1448 break;
1449 case 6: /* Origin relative/absolute */
1450 vc->vc_decom = on_off;
1451 gotoxay(vc, 0, 0);
1452 break;
1453 case 7: /* Autowrap on/off */
1454 vc->vc_decawm = on_off;
1455 break;
1456 case 8: /* Autorepeat on/off */
1457 if (on_off)
1458 set_kbd(vc, decarm);
1459 else
1460 clr_kbd(vc, decarm);
1461 break;
1462 case 9:
1463 vc->vc_report_mouse = on_off ? 1 : 0;
1464 break;
1465 case 25: /* Cursor on/off */
1466 vc->vc_deccm = on_off;
1467 break;
1468 case 1000:
1469 vc->vc_report_mouse = on_off ? 2 : 0;
1470 break;
1471 }
1472 } else {
1473 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1474 case 3: /* Monitor (display ctrls) */
1475 vc->vc_disp_ctrl = on_off;
1476 break;
1477 case 4: /* Insert Mode on/off */
1478 vc->vc_decim = on_off;
1479 break;
1480 case 20: /* Lf, Enter == CrLf/Lf */
1481 if (on_off)
1482 set_kbd(vc, lnm);
1483 else
1484 clr_kbd(vc, lnm);
1485 break;
1486 }
1487 }
1488}
1489
1490/* console_lock is held */
1491static void setterm_command(struct vc_data *vc)
1492{
1493 switch(vc->vc_par[0]) {
1494 case 1: /* set color for underline mode */
1495 if (vc->vc_can_do_color &&
1496 vc->vc_par[1] < 16) {
1497 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1498 if (vc->vc_underline)
1499 update_attr(vc);
1500 }
1501 break;
1502 case 2: /* set color for half intensity mode */
1503 if (vc->vc_can_do_color &&
1504 vc->vc_par[1] < 16) {
1505 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1506 if (vc->vc_intensity == 0)
1507 update_attr(vc);
1508 }
1509 break;
1510 case 8: /* store colors as defaults */
1511 vc->vc_def_color = vc->vc_attr;
1512 if (vc->vc_hi_font_mask == 0x100)
1513 vc->vc_def_color >>= 1;
1514 default_attr(vc);
1515 update_attr(vc);
1516 break;
1517 case 9: /* set blanking interval */
1518 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1519 poke_blanked_console();
1520 break;
1521 case 10: /* set bell frequency in Hz */
1522 if (vc->vc_npar >= 1)
1523 vc->vc_bell_pitch = vc->vc_par[1];
1524 else
1525 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1526 break;
1527 case 11: /* set bell duration in msec */
1528 if (vc->vc_npar >= 1)
1529 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1530 vc->vc_par[1] * HZ / 1000 : 0;
1531 else
1532 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1533 break;
1534 case 12: /* bring specified console to the front */
1535 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1536 set_console(vc->vc_par[1] - 1);
1537 break;
1538 case 13: /* unblank the screen */
1539 poke_blanked_console();
1540 break;
1541 case 14: /* set vesa powerdown interval */
1542 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1543 break;
1544 case 15: /* activate the previous console */
1545 set_console(last_console);
1546 break;
1547 }
1548}
1549
1550/* console_lock is held */
1551static void csi_at(struct vc_data *vc, unsigned int nr)
1552{
1553 if (nr > vc->vc_cols - vc->vc_x)
1554 nr = vc->vc_cols - vc->vc_x;
1555 else if (!nr)
1556 nr = 1;
1557 insert_char(vc, nr);
1558}
1559
1560/* console_lock is held */
1561static void csi_L(struct vc_data *vc, unsigned int nr)
1562{
1563 if (nr > vc->vc_rows - vc->vc_y)
1564 nr = vc->vc_rows - vc->vc_y;
1565 else if (!nr)
1566 nr = 1;
1567 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1568 vc->vc_need_wrap = 0;
1569}
1570
1571/* console_lock is held */
1572static void csi_P(struct vc_data *vc, unsigned int nr)
1573{
1574 if (nr > vc->vc_cols - vc->vc_x)
1575 nr = vc->vc_cols - vc->vc_x;
1576 else if (!nr)
1577 nr = 1;
1578 delete_char(vc, nr);
1579}
1580
1581/* console_lock is held */
1582static void csi_M(struct vc_data *vc, unsigned int nr)
1583{
1584 if (nr > vc->vc_rows - vc->vc_y)
1585 nr = vc->vc_rows - vc->vc_y;
1586 else if (!nr)
1587 nr=1;
1588 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1589 vc->vc_need_wrap = 0;
1590}
1591
1592/* console_lock is held (except via vc_init->reset_terminal */
1593static void save_cur(struct vc_data *vc)
1594{
1595 vc->vc_saved_x = vc->vc_x;
1596 vc->vc_saved_y = vc->vc_y;
1597 vc->vc_s_intensity = vc->vc_intensity;
1598 vc->vc_s_italic = vc->vc_italic;
1599 vc->vc_s_underline = vc->vc_underline;
1600 vc->vc_s_blink = vc->vc_blink;
1601 vc->vc_s_reverse = vc->vc_reverse;
1602 vc->vc_s_charset = vc->vc_charset;
1603 vc->vc_s_color = vc->vc_color;
1604 vc->vc_saved_G0 = vc->vc_G0_charset;
1605 vc->vc_saved_G1 = vc->vc_G1_charset;
1606}
1607
1608/* console_lock is held */
1609static void restore_cur(struct vc_data *vc)
1610{
1611 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1612 vc->vc_intensity = vc->vc_s_intensity;
1613 vc->vc_italic = vc->vc_s_italic;
1614 vc->vc_underline = vc->vc_s_underline;
1615 vc->vc_blink = vc->vc_s_blink;
1616 vc->vc_reverse = vc->vc_s_reverse;
1617 vc->vc_charset = vc->vc_s_charset;
1618 vc->vc_color = vc->vc_s_color;
1619 vc->vc_G0_charset = vc->vc_saved_G0;
1620 vc->vc_G1_charset = vc->vc_saved_G1;
1621 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1622 update_attr(vc);
1623 vc->vc_need_wrap = 0;
1624}
1625
1626enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1627 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1628 ESpalette };
1629
1630/* console_lock is held (except via vc_init()) */
1631static void reset_terminal(struct vc_data *vc, int do_clear)
1632{
1633 vc->vc_top = 0;
1634 vc->vc_bottom = vc->vc_rows;
1635 vc->vc_state = ESnormal;
1636 vc->vc_ques = 0;
1637 vc->vc_translate = set_translate(LAT1_MAP, vc);
1638 vc->vc_G0_charset = LAT1_MAP;
1639 vc->vc_G1_charset = GRAF_MAP;
1640 vc->vc_charset = 0;
1641 vc->vc_need_wrap = 0;
1642 vc->vc_report_mouse = 0;
1643 vc->vc_utf = default_utf8;
1644 vc->vc_utf_count = 0;
1645
1646 vc->vc_disp_ctrl = 0;
1647 vc->vc_toggle_meta = 0;
1648
1649 vc->vc_decscnm = 0;
1650 vc->vc_decom = 0;
1651 vc->vc_decawm = 1;
1652 vc->vc_deccm = global_cursor_default;
1653 vc->vc_decim = 0;
1654
1655 set_kbd(vc, decarm);
1656 clr_kbd(vc, decckm);
1657 clr_kbd(vc, kbdapplic);
1658 clr_kbd(vc, lnm);
1659 kbd_table[vc->vc_num].lockstate = 0;
1660 kbd_table[vc->vc_num].slockstate = 0;
1661 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1662 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1663 /* do not do set_leds here because this causes an endless tasklet loop
1664 when the keyboard hasn't been initialized yet */
1665
1666 vc->vc_cursor_type = cur_default;
1667 vc->vc_complement_mask = vc->vc_s_complement_mask;
1668
1669 default_attr(vc);
1670 update_attr(vc);
1671
1672 vc->vc_tab_stop[0] = 0x01010100;
1673 vc->vc_tab_stop[1] =
1674 vc->vc_tab_stop[2] =
1675 vc->vc_tab_stop[3] =
1676 vc->vc_tab_stop[4] =
1677 vc->vc_tab_stop[5] =
1678 vc->vc_tab_stop[6] =
1679 vc->vc_tab_stop[7] = 0x01010101;
1680
1681 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1682 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1683
1684 gotoxy(vc, 0, 0);
1685 save_cur(vc);
1686 if (do_clear)
1687 csi_J(vc, 2);
1688}
1689
1690/* console_lock is held */
1691static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1692{
1693 /*
1694 * Control characters can be used in the _middle_
1695 * of an escape sequence.
1696 */
1697 switch (c) {
1698 case 0:
1699 return;
1700 case 7:
1701 if (vc->vc_bell_duration)
1702 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1703 return;
1704 case 8:
1705 bs(vc);
1706 return;
1707 case 9:
1708 vc->vc_pos -= (vc->vc_x << 1);
1709 while (vc->vc_x < vc->vc_cols - 1) {
1710 vc->vc_x++;
1711 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1712 break;
1713 }
1714 vc->vc_pos += (vc->vc_x << 1);
1715 notify_write(vc, '\t');
1716 return;
1717 case 10: case 11: case 12:
1718 lf(vc);
1719 if (!is_kbd(vc, lnm))
1720 return;
1721 case 13:
1722 cr(vc);
1723 return;
1724 case 14:
1725 vc->vc_charset = 1;
1726 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1727 vc->vc_disp_ctrl = 1;
1728 return;
1729 case 15:
1730 vc->vc_charset = 0;
1731 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1732 vc->vc_disp_ctrl = 0;
1733 return;
1734 case 24: case 26:
1735 vc->vc_state = ESnormal;
1736 return;
1737 case 27:
1738 vc->vc_state = ESesc;
1739 return;
1740 case 127:
1741 del(vc);
1742 return;
1743 case 128+27:
1744 vc->vc_state = ESsquare;
1745 return;
1746 }
1747 switch(vc->vc_state) {
1748 case ESesc:
1749 vc->vc_state = ESnormal;
1750 switch (c) {
1751 case '[':
1752 vc->vc_state = ESsquare;
1753 return;
1754 case ']':
1755 vc->vc_state = ESnonstd;
1756 return;
1757 case '%':
1758 vc->vc_state = ESpercent;
1759 return;
1760 case 'E':
1761 cr(vc);
1762 lf(vc);
1763 return;
1764 case 'M':
1765 ri(vc);
1766 return;
1767 case 'D':
1768 lf(vc);
1769 return;
1770 case 'H':
1771 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1772 return;
1773 case 'Z':
1774 respond_ID(tty);
1775 return;
1776 case '7':
1777 save_cur(vc);
1778 return;
1779 case '8':
1780 restore_cur(vc);
1781 return;
1782 case '(':
1783 vc->vc_state = ESsetG0;
1784 return;
1785 case ')':
1786 vc->vc_state = ESsetG1;
1787 return;
1788 case '#':
1789 vc->vc_state = EShash;
1790 return;
1791 case 'c':
1792 reset_terminal(vc, 1);
1793 return;
1794 case '>': /* Numeric keypad */
1795 clr_kbd(vc, kbdapplic);
1796 return;
1797 case '=': /* Appl. keypad */
1798 set_kbd(vc, kbdapplic);
1799 return;
1800 }
1801 return;
1802 case ESnonstd:
1803 if (c=='P') { /* palette escape sequence */
1804 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1805 vc->vc_par[vc->vc_npar] = 0;
1806 vc->vc_npar = 0;
1807 vc->vc_state = ESpalette;
1808 return;
1809 } else if (c=='R') { /* reset palette */
1810 reset_palette(vc);
1811 vc->vc_state = ESnormal;
1812 } else
1813 vc->vc_state = ESnormal;
1814 return;
1815 case ESpalette:
1816 if (isxdigit(c)) {
1817 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1818 if (vc->vc_npar == 7) {
1819 int i = vc->vc_par[0] * 3, j = 1;
1820 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1821 vc->vc_palette[i++] += vc->vc_par[j++];
1822 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1823 vc->vc_palette[i++] += vc->vc_par[j++];
1824 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1825 vc->vc_palette[i] += vc->vc_par[j];
1826 set_palette(vc);
1827 vc->vc_state = ESnormal;
1828 }
1829 } else
1830 vc->vc_state = ESnormal;
1831 return;
1832 case ESsquare:
1833 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1834 vc->vc_par[vc->vc_npar] = 0;
1835 vc->vc_npar = 0;
1836 vc->vc_state = ESgetpars;
1837 if (c == '[') { /* Function key */
1838 vc->vc_state=ESfunckey;
1839 return;
1840 }
1841 vc->vc_ques = (c == '?');
1842 if (vc->vc_ques)
1843 return;
1844 case ESgetpars:
1845 if (c == ';' && vc->vc_npar < NPAR - 1) {
1846 vc->vc_npar++;
1847 return;
1848 } else if (c>='0' && c<='9') {
1849 vc->vc_par[vc->vc_npar] *= 10;
1850 vc->vc_par[vc->vc_npar] += c - '0';
1851 return;
1852 } else
1853 vc->vc_state = ESgotpars;
1854 case ESgotpars:
1855 vc->vc_state = ESnormal;
1856 switch(c) {
1857 case 'h':
1858 set_mode(vc, 1);
1859 return;
1860 case 'l':
1861 set_mode(vc, 0);
1862 return;
1863 case 'c':
1864 if (vc->vc_ques) {
1865 if (vc->vc_par[0])
1866 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1867 else
1868 vc->vc_cursor_type = cur_default;
1869 return;
1870 }
1871 break;
1872 case 'm':
1873 if (vc->vc_ques) {
1874 clear_selection();
1875 if (vc->vc_par[0])
1876 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1877 else
1878 vc->vc_complement_mask = vc->vc_s_complement_mask;
1879 return;
1880 }
1881 break;
1882 case 'n':
1883 if (!vc->vc_ques) {
1884 if (vc->vc_par[0] == 5)
1885 status_report(tty);
1886 else if (vc->vc_par[0] == 6)
1887 cursor_report(vc, tty);
1888 }
1889 return;
1890 }
1891 if (vc->vc_ques) {
1892 vc->vc_ques = 0;
1893 return;
1894 }
1895 switch(c) {
1896 case 'G': case '`':
1897 if (vc->vc_par[0])
1898 vc->vc_par[0]--;
1899 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1900 return;
1901 case 'A':
1902 if (!vc->vc_par[0])
1903 vc->vc_par[0]++;
1904 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1905 return;
1906 case 'B': case 'e':
1907 if (!vc->vc_par[0])
1908 vc->vc_par[0]++;
1909 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1910 return;
1911 case 'C': case 'a':
1912 if (!vc->vc_par[0])
1913 vc->vc_par[0]++;
1914 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1915 return;
1916 case 'D':
1917 if (!vc->vc_par[0])
1918 vc->vc_par[0]++;
1919 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1920 return;
1921 case 'E':
1922 if (!vc->vc_par[0])
1923 vc->vc_par[0]++;
1924 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1925 return;
1926 case 'F':
1927 if (!vc->vc_par[0])
1928 vc->vc_par[0]++;
1929 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1930 return;
1931 case 'd':
1932 if (vc->vc_par[0])
1933 vc->vc_par[0]--;
1934 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1935 return;
1936 case 'H': case 'f':
1937 if (vc->vc_par[0])
1938 vc->vc_par[0]--;
1939 if (vc->vc_par[1])
1940 vc->vc_par[1]--;
1941 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1942 return;
1943 case 'J':
1944 csi_J(vc, vc->vc_par[0]);
1945 return;
1946 case 'K':
1947 csi_K(vc, vc->vc_par[0]);
1948 return;
1949 case 'L':
1950 csi_L(vc, vc->vc_par[0]);
1951 return;
1952 case 'M':
1953 csi_M(vc, vc->vc_par[0]);
1954 return;
1955 case 'P':
1956 csi_P(vc, vc->vc_par[0]);
1957 return;
1958 case 'c':
1959 if (!vc->vc_par[0])
1960 respond_ID(tty);
1961 return;
1962 case 'g':
1963 if (!vc->vc_par[0])
1964 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1965 else if (vc->vc_par[0] == 3) {
1966 vc->vc_tab_stop[0] =
1967 vc->vc_tab_stop[1] =
1968 vc->vc_tab_stop[2] =
1969 vc->vc_tab_stop[3] =
1970 vc->vc_tab_stop[4] =
1971 vc->vc_tab_stop[5] =
1972 vc->vc_tab_stop[6] =
1973 vc->vc_tab_stop[7] = 0;
1974 }
1975 return;
1976 case 'm':
1977 csi_m(vc);
1978 return;
1979 case 'q': /* DECLL - but only 3 leds */
1980 /* map 0,1,2,3 to 0,1,2,4 */
1981 if (vc->vc_par[0] < 4)
1982 setledstate(kbd_table + vc->vc_num,
1983 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1984 return;
1985 case 'r':
1986 if (!vc->vc_par[0])
1987 vc->vc_par[0]++;
1988 if (!vc->vc_par[1])
1989 vc->vc_par[1] = vc->vc_rows;
1990 /* Minimum allowed region is 2 lines */
1991 if (vc->vc_par[0] < vc->vc_par[1] &&
1992 vc->vc_par[1] <= vc->vc_rows) {
1993 vc->vc_top = vc->vc_par[0] - 1;
1994 vc->vc_bottom = vc->vc_par[1];
1995 gotoxay(vc, 0, 0);
1996 }
1997 return;
1998 case 's':
1999 save_cur(vc);
2000 return;
2001 case 'u':
2002 restore_cur(vc);
2003 return;
2004 case 'X':
2005 csi_X(vc, vc->vc_par[0]);
2006 return;
2007 case '@':
2008 csi_at(vc, vc->vc_par[0]);
2009 return;
2010 case ']': /* setterm functions */
2011 setterm_command(vc);
2012 return;
2013 }
2014 return;
2015 case ESpercent:
2016 vc->vc_state = ESnormal;
2017 switch (c) {
2018 case '@': /* defined in ISO 2022 */
2019 vc->vc_utf = 0;
2020 return;
2021 case 'G': /* prelim official escape code */
2022 case '8': /* retained for compatibility */
2023 vc->vc_utf = 1;
2024 return;
2025 }
2026 return;
2027 case ESfunckey:
2028 vc->vc_state = ESnormal;
2029 return;
2030 case EShash:
2031 vc->vc_state = ESnormal;
2032 if (c == '8') {
2033 /* DEC screen alignment test. kludge :-) */
2034 vc->vc_video_erase_char =
2035 (vc->vc_video_erase_char & 0xff00) | 'E';
2036 csi_J(vc, 2);
2037 vc->vc_video_erase_char =
2038 (vc->vc_video_erase_char & 0xff00) | ' ';
2039 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2040 }
2041 return;
2042 case ESsetG0:
2043 if (c == '0')
2044 vc->vc_G0_charset = GRAF_MAP;
2045 else if (c == 'B')
2046 vc->vc_G0_charset = LAT1_MAP;
2047 else if (c == 'U')
2048 vc->vc_G0_charset = IBMPC_MAP;
2049 else if (c == 'K')
2050 vc->vc_G0_charset = USER_MAP;
2051 if (vc->vc_charset == 0)
2052 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2053 vc->vc_state = ESnormal;
2054 return;
2055 case ESsetG1:
2056 if (c == '0')
2057 vc->vc_G1_charset = GRAF_MAP;
2058 else if (c == 'B')
2059 vc->vc_G1_charset = LAT1_MAP;
2060 else if (c == 'U')
2061 vc->vc_G1_charset = IBMPC_MAP;
2062 else if (c == 'K')
2063 vc->vc_G1_charset = USER_MAP;
2064 if (vc->vc_charset == 1)
2065 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2066 vc->vc_state = ESnormal;
2067 return;
2068 default:
2069 vc->vc_state = ESnormal;
2070 }
2071}
2072
2073/* is_double_width() is based on the wcwidth() implementation by
2074 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2075 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2076 */
2077struct interval {
2078 uint32_t first;
2079 uint32_t last;
2080};
2081
2082static int bisearch(uint32_t ucs, const struct interval *table, int max)
2083{
2084 int min = 0;
2085 int mid;
2086
2087 if (ucs < table[0].first || ucs > table[max].last)
2088 return 0;
2089 while (max >= min) {
2090 mid = (min + max) / 2;
2091 if (ucs > table[mid].last)
2092 min = mid + 1;
2093 else if (ucs < table[mid].first)
2094 max = mid - 1;
2095 else
2096 return 1;
2097 }
2098 return 0;
2099}
2100
2101static int is_double_width(uint32_t ucs)
2102{
2103 static const struct interval double_width[] = {
2104 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2105 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2106 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2107 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2108 };
2109 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2110}
2111
2112/* acquires console_lock */
2113static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2114{
2115#ifdef VT_BUF_VRAM_ONLY
2116#define FLUSH do { } while(0);
2117#else
2118#define FLUSH if (draw_x >= 0) { \
2119 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2120 draw_x = -1; \
2121 }
2122#endif
2123
2124 int c, tc, ok, n = 0, draw_x = -1;
2125 unsigned int currcons;
2126 unsigned long draw_from = 0, draw_to = 0;
2127 struct vc_data *vc;
2128 unsigned char vc_attr;
2129 struct vt_notifier_param param;
2130 uint8_t rescan;
2131 uint8_t inverse;
2132 uint8_t width;
2133 u16 himask, charmask;
2134
2135 if (in_interrupt())
2136 return count;
2137
2138 might_sleep();
2139
2140 console_lock();
2141 vc = tty->driver_data;
2142 if (vc == NULL) {
2143 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2144 console_unlock();
2145 return 0;
2146 }
2147
2148 currcons = vc->vc_num;
2149 if (!vc_cons_allocated(currcons)) {
2150 /* could this happen? */
2151 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2152 console_unlock();
2153 return 0;
2154 }
2155
2156 himask = vc->vc_hi_font_mask;
2157 charmask = himask ? 0x1ff : 0xff;
2158
2159 /* undraw cursor first */
2160 if (IS_FG(vc))
2161 hide_cursor(vc);
2162
2163 param.vc = vc;
2164
2165 while (!tty->stopped && count) {
2166 int orig = *buf;
2167 c = orig;
2168 buf++;
2169 n++;
2170 count--;
2171 rescan = 0;
2172 inverse = 0;
2173 width = 1;
2174
2175 /* Do no translation at all in control states */
2176 if (vc->vc_state != ESnormal) {
2177 tc = c;
2178 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2179 /* Combine UTF-8 into Unicode in vc_utf_char.
2180 * vc_utf_count is the number of continuation bytes still
2181 * expected to arrive.
2182 * vc_npar is the number of continuation bytes arrived so
2183 * far
2184 */
2185rescan_last_byte:
2186 if ((c & 0xc0) == 0x80) {
2187 /* Continuation byte received */
2188 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2189 if (vc->vc_utf_count) {
2190 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2191 vc->vc_npar++;
2192 if (--vc->vc_utf_count) {
2193 /* Still need some bytes */
2194 continue;
2195 }
2196 /* Got a whole character */
2197 c = vc->vc_utf_char;
2198 /* Reject overlong sequences */
2199 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2200 c > utf8_length_changes[vc->vc_npar])
2201 c = 0xfffd;
2202 } else {
2203 /* Unexpected continuation byte */
2204 vc->vc_utf_count = 0;
2205 c = 0xfffd;
2206 }
2207 } else {
2208 /* Single ASCII byte or first byte of a sequence received */
2209 if (vc->vc_utf_count) {
2210 /* Continuation byte expected */
2211 rescan = 1;
2212 vc->vc_utf_count = 0;
2213 c = 0xfffd;
2214 } else if (c > 0x7f) {
2215 /* First byte of a multibyte sequence received */
2216 vc->vc_npar = 0;
2217 if ((c & 0xe0) == 0xc0) {
2218 vc->vc_utf_count = 1;
2219 vc->vc_utf_char = (c & 0x1f);
2220 } else if ((c & 0xf0) == 0xe0) {
2221 vc->vc_utf_count = 2;
2222 vc->vc_utf_char = (c & 0x0f);
2223 } else if ((c & 0xf8) == 0xf0) {
2224 vc->vc_utf_count = 3;
2225 vc->vc_utf_char = (c & 0x07);
2226 } else if ((c & 0xfc) == 0xf8) {
2227 vc->vc_utf_count = 4;
2228 vc->vc_utf_char = (c & 0x03);
2229 } else if ((c & 0xfe) == 0xfc) {
2230 vc->vc_utf_count = 5;
2231 vc->vc_utf_char = (c & 0x01);
2232 } else {
2233 /* 254 and 255 are invalid */
2234 c = 0xfffd;
2235 }
2236 if (vc->vc_utf_count) {
2237 /* Still need some bytes */
2238 continue;
2239 }
2240 }
2241 /* Nothing to do if an ASCII byte was received */
2242 }
2243 /* End of UTF-8 decoding. */
2244 /* c is the received character, or U+FFFD for invalid sequences. */
2245 /* Replace invalid Unicode code points with U+FFFD too */
2246 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2247 c = 0xfffd;
2248 tc = c;
2249 } else { /* no utf or alternate charset mode */
2250 tc = vc_translate(vc, c);
2251 }
2252
2253 param.c = tc;
2254 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2255 &param) == NOTIFY_STOP)
2256 continue;
2257
2258 /* If the original code was a control character we
2259 * only allow a glyph to be displayed if the code is
2260 * not normally used (such as for cursor movement) or
2261 * if the disp_ctrl mode has been explicitly enabled.
2262 * Certain characters (as given by the CTRL_ALWAYS
2263 * bitmap) are always displayed as control characters,
2264 * as the console would be pretty useless without
2265 * them; to display an arbitrary font position use the
2266 * direct-to-font zone in UTF-8 mode.
2267 */
2268 ok = tc && (c >= 32 ||
2269 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2270 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2271 && (c != 127 || vc->vc_disp_ctrl)
2272 && (c != 128+27);
2273
2274 if (vc->vc_state == ESnormal && ok) {
2275 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2276 if (is_double_width(c))
2277 width = 2;
2278 }
2279 /* Now try to find out how to display it */
2280 tc = conv_uni_to_pc(vc, tc);
2281 if (tc & ~charmask) {
2282 if (tc == -1 || tc == -2) {
2283 continue; /* nothing to display */
2284 }
2285 /* Glyph not found */
2286 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2287 /* In legacy mode use the glyph we get by a 1:1 mapping.
2288 This would make absolutely no sense with Unicode in mind,
2289 but do this for ASCII characters since a font may lack
2290 Unicode mapping info and we don't want to end up with
2291 having question marks only. */
2292 tc = c;
2293 } else {
2294 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2295 tc = conv_uni_to_pc(vc, 0xfffd);
2296 if (tc < 0) {
2297 inverse = 1;
2298 tc = conv_uni_to_pc(vc, '?');
2299 if (tc < 0) tc = '?';
2300 }
2301 }
2302 }
2303
2304 if (!inverse) {
2305 vc_attr = vc->vc_attr;
2306 } else {
2307 /* invert vc_attr */
2308 if (!vc->vc_can_do_color) {
2309 vc_attr = (vc->vc_attr) ^ 0x08;
2310 } else if (vc->vc_hi_font_mask == 0x100) {
2311 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2312 } else {
2313 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2314 }
2315 FLUSH
2316 }
2317
2318 while (1) {
2319 if (vc->vc_need_wrap || vc->vc_decim)
2320 FLUSH
2321 if (vc->vc_need_wrap) {
2322 cr(vc);
2323 lf(vc);
2324 }
2325 if (vc->vc_decim)
2326 insert_char(vc, 1);
2327 scr_writew(himask ?
2328 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2329 (vc_attr << 8) + tc,
2330 (u16 *) vc->vc_pos);
2331 if (DO_UPDATE(vc) && draw_x < 0) {
2332 draw_x = vc->vc_x;
2333 draw_from = vc->vc_pos;
2334 }
2335 if (vc->vc_x == vc->vc_cols - 1) {
2336 vc->vc_need_wrap = vc->vc_decawm;
2337 draw_to = vc->vc_pos + 2;
2338 } else {
2339 vc->vc_x++;
2340 draw_to = (vc->vc_pos += 2);
2341 }
2342
2343 if (!--width) break;
2344
2345 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2346 if (tc < 0) tc = ' ';
2347 }
2348 notify_write(vc, c);
2349
2350 if (inverse) {
2351 FLUSH
2352 }
2353
2354 if (rescan) {
2355 rescan = 0;
2356 inverse = 0;
2357 width = 1;
2358 c = orig;
2359 goto rescan_last_byte;
2360 }
2361 continue;
2362 }
2363 FLUSH
2364 do_con_trol(tty, vc, orig);
2365 }
2366 FLUSH
2367 console_conditional_schedule();
2368 console_unlock();
2369 notify_update(vc);
2370 return n;
2371#undef FLUSH
2372}
2373
2374/*
2375 * This is the console switching callback.
2376 *
2377 * Doing console switching in a process context allows
2378 * us to do the switches asynchronously (needed when we want
2379 * to switch due to a keyboard interrupt). Synchronization
2380 * with other console code and prevention of re-entrancy is
2381 * ensured with console_lock.
2382 */
2383static void console_callback(struct work_struct *ignored)
2384{
2385 console_lock();
2386
2387 if (want_console >= 0) {
2388 if (want_console != fg_console &&
2389 vc_cons_allocated(want_console)) {
2390 hide_cursor(vc_cons[fg_console].d);
2391 change_console(vc_cons[want_console].d);
2392 /* we only changed when the console had already
2393 been allocated - a new console is not created
2394 in an interrupt routine */
2395 }
2396 want_console = -1;
2397 }
2398 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2399 do_poke_blanked_console = 0;
2400 poke_blanked_console();
2401 }
2402 if (scrollback_delta) {
2403 struct vc_data *vc = vc_cons[fg_console].d;
2404 clear_selection();
2405 if (vc->vc_mode == KD_TEXT)
2406 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2407 scrollback_delta = 0;
2408 }
2409 if (blank_timer_expired) {
2410 do_blank_screen(0);
2411 blank_timer_expired = 0;
2412 }
2413 notify_update(vc_cons[fg_console].d);
2414
2415 console_unlock();
2416}
2417
2418int set_console(int nr)
2419{
2420 struct vc_data *vc = vc_cons[fg_console].d;
2421
2422 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2423 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2424
2425 /*
2426 * Console switch will fail in console_callback() or
2427 * change_console() so there is no point scheduling
2428 * the callback
2429 *
2430 * Existing set_console() users don't check the return
2431 * value so this shouldn't break anything
2432 */
2433 return -EINVAL;
2434 }
2435
2436 want_console = nr;
2437 schedule_console_callback();
2438
2439 return 0;
2440}
2441
2442struct tty_driver *console_driver;
2443
2444#ifdef CONFIG_VT_CONSOLE
2445
2446/**
2447 * vt_kmsg_redirect() - Sets/gets the kernel message console
2448 * @new: The new virtual terminal number or -1 if the console should stay
2449 * unchanged
2450 *
2451 * By default, the kernel messages are always printed on the current virtual
2452 * console. However, the user may modify that default with the
2453 * TIOCL_SETKMSGREDIRECT ioctl call.
2454 *
2455 * This function sets the kernel message console to be @new. It returns the old
2456 * virtual console number. The virtual terminal number 0 (both as parameter and
2457 * return value) means no redirection (i.e. always printed on the currently
2458 * active console).
2459 *
2460 * The parameter -1 means that only the current console is returned, but the
2461 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2462 * case to make the code more understandable.
2463 *
2464 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2465 * the parameter and always returns 0.
2466 */
2467int vt_kmsg_redirect(int new)
2468{
2469 static int kmsg_con;
2470
2471 if (new != -1)
2472 return xchg(&kmsg_con, new);
2473 else
2474 return kmsg_con;
2475}
2476
2477/*
2478 * Console on virtual terminal
2479 *
2480 * The console must be locked when we get here.
2481 */
2482
2483static void vt_console_print(struct console *co, const char *b, unsigned count)
2484{
2485 struct vc_data *vc = vc_cons[fg_console].d;
2486 unsigned char c;
2487 static DEFINE_SPINLOCK(printing_lock);
2488 const ushort *start;
2489 ushort cnt = 0;
2490 ushort myx;
2491 int kmsg_console;
2492
2493 /* console busy or not yet initialized */
2494 if (!printable)
2495 return;
2496 if (!spin_trylock(&printing_lock))
2497 return;
2498
2499 kmsg_console = vt_get_kmsg_redirect();
2500 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2501 vc = vc_cons[kmsg_console - 1].d;
2502
2503 /* read `x' only after setting currcons properly (otherwise
2504 the `x' macro will read the x of the foreground console). */
2505 myx = vc->vc_x;
2506
2507 if (!vc_cons_allocated(fg_console)) {
2508 /* impossible */
2509 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2510 goto quit;
2511 }
2512
2513 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2514 goto quit;
2515
2516 /* undraw cursor first */
2517 if (IS_FG(vc))
2518 hide_cursor(vc);
2519
2520 start = (ushort *)vc->vc_pos;
2521
2522 /* Contrived structure to try to emulate original need_wrap behaviour
2523 * Problems caused when we have need_wrap set on '\n' character */
2524 while (count--) {
2525 c = *b++;
2526 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2527 if (cnt > 0) {
2528 if (CON_IS_VISIBLE(vc))
2529 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2530 vc->vc_x += cnt;
2531 if (vc->vc_need_wrap)
2532 vc->vc_x--;
2533 cnt = 0;
2534 }
2535 if (c == 8) { /* backspace */
2536 bs(vc);
2537 start = (ushort *)vc->vc_pos;
2538 myx = vc->vc_x;
2539 continue;
2540 }
2541 if (c != 13)
2542 lf(vc);
2543 cr(vc);
2544 start = (ushort *)vc->vc_pos;
2545 myx = vc->vc_x;
2546 if (c == 10 || c == 13)
2547 continue;
2548 }
2549 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2550 notify_write(vc, c);
2551 cnt++;
2552 if (myx == vc->vc_cols - 1) {
2553 vc->vc_need_wrap = 1;
2554 continue;
2555 }
2556 vc->vc_pos += 2;
2557 myx++;
2558 }
2559 if (cnt > 0) {
2560 if (CON_IS_VISIBLE(vc))
2561 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2562 vc->vc_x += cnt;
2563 if (vc->vc_x == vc->vc_cols) {
2564 vc->vc_x--;
2565 vc->vc_need_wrap = 1;
2566 }
2567 }
2568 set_cursor(vc);
2569 notify_update(vc);
2570
2571quit:
2572 spin_unlock(&printing_lock);
2573}
2574
2575static struct tty_driver *vt_console_device(struct console *c, int *index)
2576{
2577 *index = c->index ? c->index-1 : fg_console;
2578 return console_driver;
2579}
2580
2581static struct console vt_console_driver = {
2582 .name = "tty",
2583 .write = vt_console_print,
2584 .device = vt_console_device,
2585 .unblank = unblank_screen,
2586 .flags = CON_PRINTBUFFER,
2587 .index = -1,
2588};
2589#endif
2590
2591/*
2592 * Handling of Linux-specific VC ioctls
2593 */
2594
2595/*
2596 * Generally a bit racy with respect to console_lock();.
2597 *
2598 * There are some functions which don't need it.
2599 *
2600 * There are some functions which can sleep for arbitrary periods
2601 * (paste_selection) but we don't need the lock there anyway.
2602 *
2603 * set_selection has locking, and definitely needs it
2604 */
2605
2606int tioclinux(struct tty_struct *tty, unsigned long arg)
2607{
2608 char type, data;
2609 char __user *p = (char __user *)arg;
2610 int lines;
2611 int ret;
2612
2613 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2614 return -EPERM;
2615 if (get_user(type, p))
2616 return -EFAULT;
2617 ret = 0;
2618
2619 switch (type)
2620 {
2621 case TIOCL_SETSEL:
2622 console_lock();
2623 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2624 console_unlock();
2625 break;
2626 case TIOCL_PASTESEL:
2627 ret = paste_selection(tty);
2628 break;
2629 case TIOCL_UNBLANKSCREEN:
2630 console_lock();
2631 unblank_screen();
2632 console_unlock();
2633 break;
2634 case TIOCL_SELLOADLUT:
2635 ret = sel_loadlut(p);
2636 break;
2637 case TIOCL_GETSHIFTSTATE:
2638
2639 /*
2640 * Make it possible to react to Shift+Mousebutton.
2641 * Note that 'shift_state' is an undocumented
2642 * kernel-internal variable; programs not closely
2643 * related to the kernel should not use this.
2644 */
2645 data = shift_state;
2646 ret = __put_user(data, p);
2647 break;
2648 case TIOCL_GETMOUSEREPORTING:
2649 data = mouse_reporting();
2650 ret = __put_user(data, p);
2651 break;
2652 case TIOCL_SETVESABLANK:
2653 ret = set_vesa_blanking(p);
2654 break;
2655 case TIOCL_GETKMSGREDIRECT:
2656 data = vt_get_kmsg_redirect();
2657 ret = __put_user(data, p);
2658 break;
2659 case TIOCL_SETKMSGREDIRECT:
2660 if (!capable(CAP_SYS_ADMIN)) {
2661 ret = -EPERM;
2662 } else {
2663 if (get_user(data, p+1))
2664 ret = -EFAULT;
2665 else
2666 vt_kmsg_redirect(data);
2667 }
2668 break;
2669 case TIOCL_GETFGCONSOLE:
2670 ret = fg_console;
2671 break;
2672 case TIOCL_SCROLLCONSOLE:
2673 if (get_user(lines, (s32 __user *)(p+4))) {
2674 ret = -EFAULT;
2675 } else {
2676 scrollfront(vc_cons[fg_console].d, lines);
2677 ret = 0;
2678 }
2679 break;
2680 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2681 console_lock();
2682 ignore_poke = 1;
2683 do_blank_screen(0);
2684 console_unlock();
2685 break;
2686 case TIOCL_BLANKEDSCREEN:
2687 ret = console_blanked;
2688 break;
2689 default:
2690 ret = -EINVAL;
2691 break;
2692 }
2693 return ret;
2694}
2695
2696/*
2697 * /dev/ttyN handling
2698 */
2699
2700static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2701{
2702 int retval;
2703
2704 retval = do_con_write(tty, buf, count);
2705 con_flush_chars(tty);
2706
2707 return retval;
2708}
2709
2710static int con_put_char(struct tty_struct *tty, unsigned char ch)
2711{
2712 if (in_interrupt())
2713 return 0; /* n_r3964 calls put_char() from interrupt context */
2714 return do_con_write(tty, &ch, 1);
2715}
2716
2717static int con_write_room(struct tty_struct *tty)
2718{
2719 if (tty->stopped)
2720 return 0;
2721 return 32768; /* No limit, really; we're not buffering */
2722}
2723
2724static int con_chars_in_buffer(struct tty_struct *tty)
2725{
2726 return 0; /* we're not buffering */
2727}
2728
2729/*
2730 * con_throttle and con_unthrottle are only used for
2731 * paste_selection(), which has to stuff in a large number of
2732 * characters...
2733 */
2734static void con_throttle(struct tty_struct *tty)
2735{
2736}
2737
2738static void con_unthrottle(struct tty_struct *tty)
2739{
2740 struct vc_data *vc = tty->driver_data;
2741
2742 wake_up_interruptible(&vc->paste_wait);
2743}
2744
2745/*
2746 * Turn the Scroll-Lock LED on when the tty is stopped
2747 */
2748static void con_stop(struct tty_struct *tty)
2749{
2750 int console_num;
2751 if (!tty)
2752 return;
2753 console_num = tty->index;
2754 if (!vc_cons_allocated(console_num))
2755 return;
2756 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2757 set_leds();
2758}
2759
2760/*
2761 * Turn the Scroll-Lock LED off when the console is started
2762 */
2763static void con_start(struct tty_struct *tty)
2764{
2765 int console_num;
2766 if (!tty)
2767 return;
2768 console_num = tty->index;
2769 if (!vc_cons_allocated(console_num))
2770 return;
2771 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2772 set_leds();
2773}
2774
2775static void con_flush_chars(struct tty_struct *tty)
2776{
2777 struct vc_data *vc;
2778
2779 if (in_interrupt()) /* from flush_to_ldisc */
2780 return;
2781
2782 /* if we race with con_close(), vt may be null */
2783 console_lock();
2784 vc = tty->driver_data;
2785 if (vc)
2786 set_cursor(vc);
2787 console_unlock();
2788}
2789
2790/*
2791 * Allocate the console screen memory.
2792 */
2793static int con_open(struct tty_struct *tty, struct file *filp)
2794{
2795 unsigned int currcons = tty->index;
2796 int ret = 0;
2797
2798 console_lock();
2799 if (tty->driver_data == NULL) {
2800 ret = vc_allocate(currcons);
2801 if (ret == 0) {
2802 struct vc_data *vc = vc_cons[currcons].d;
2803
2804 /* Still being freed */
2805 if (vc->port.tty) {
2806 console_unlock();
2807 return -ERESTARTSYS;
2808 }
2809 tty->driver_data = vc;
2810 vc->port.tty = tty;
2811
2812 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2813 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2814 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2815 }
2816 if (vc->vc_utf)
2817 tty->termios->c_iflag |= IUTF8;
2818 else
2819 tty->termios->c_iflag &= ~IUTF8;
2820 console_unlock();
2821 return ret;
2822 }
2823 }
2824 console_unlock();
2825 return ret;
2826}
2827
2828static void con_close(struct tty_struct *tty, struct file *filp)
2829{
2830 /* Nothing to do - we defer to shutdown */
2831}
2832
2833static void con_shutdown(struct tty_struct *tty)
2834{
2835 struct vc_data *vc = tty->driver_data;
2836 BUG_ON(vc == NULL);
2837 console_lock();
2838 vc->port.tty = NULL;
2839 console_unlock();
2840 tty_shutdown(tty);
2841}
2842
2843static int default_italic_color = 2; // green (ASCII)
2844static int default_underline_color = 3; // cyan (ASCII)
2845module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2846module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2847
2848static void vc_init(struct vc_data *vc, unsigned int rows,
2849 unsigned int cols, int do_clear)
2850{
2851 int j, k ;
2852
2853 vc->vc_cols = cols;
2854 vc->vc_rows = rows;
2855 vc->vc_size_row = cols << 1;
2856 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2857
2858 set_origin(vc);
2859 vc->vc_pos = vc->vc_origin;
2860 reset_vc(vc);
2861 for (j=k=0; j<16; j++) {
2862 vc->vc_palette[k++] = default_red[j] ;
2863 vc->vc_palette[k++] = default_grn[j] ;
2864 vc->vc_palette[k++] = default_blu[j] ;
2865 }
2866 vc->vc_def_color = 0x07; /* white */
2867 vc->vc_ulcolor = default_underline_color;
2868 vc->vc_itcolor = default_italic_color;
2869 vc->vc_halfcolor = 0x08; /* grey */
2870 init_waitqueue_head(&vc->paste_wait);
2871 reset_terminal(vc, do_clear);
2872}
2873
2874/*
2875 * This routine initializes console interrupts, and does nothing
2876 * else. If you want the screen to clear, call tty_write with
2877 * the appropriate escape-sequence.
2878 */
2879
2880static int __init con_init(void)
2881{
2882 const char *display_desc = NULL;
2883 struct vc_data *vc;
2884 unsigned int currcons = 0, i;
2885
2886 console_lock();
2887
2888 if (conswitchp)
2889 display_desc = conswitchp->con_startup();
2890 if (!display_desc) {
2891 fg_console = 0;
2892 console_unlock();
2893 return 0;
2894 }
2895
2896 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2897 struct con_driver *con_driver = &registered_con_driver[i];
2898
2899 if (con_driver->con == NULL) {
2900 con_driver->con = conswitchp;
2901 con_driver->desc = display_desc;
2902 con_driver->flag = CON_DRIVER_FLAG_INIT;
2903 con_driver->first = 0;
2904 con_driver->last = MAX_NR_CONSOLES - 1;
2905 break;
2906 }
2907 }
2908
2909 for (i = 0; i < MAX_NR_CONSOLES; i++)
2910 con_driver_map[i] = conswitchp;
2911
2912 if (blankinterval) {
2913 blank_state = blank_normal_wait;
2914 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
2915 }
2916
2917 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2918 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2919 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2920 tty_port_init(&vc->port);
2921 visual_init(vc, currcons, 1);
2922 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2923 vc_init(vc, vc->vc_rows, vc->vc_cols,
2924 currcons || !vc->vc_sw->con_save_screen);
2925 }
2926 currcons = fg_console = 0;
2927 master_display_fg = vc = vc_cons[currcons].d;
2928 set_origin(vc);
2929 save_screen(vc);
2930 gotoxy(vc, vc->vc_x, vc->vc_y);
2931 csi_J(vc, 0);
2932 update_screen(vc);
2933 pr_info("Console: %s %s %dx%d",
2934 vc->vc_can_do_color ? "colour" : "mono",
2935 display_desc, vc->vc_cols, vc->vc_rows);
2936 printable = 1;
2937 printk("\n");
2938
2939 console_unlock();
2940
2941#ifdef CONFIG_VT_CONSOLE
2942 register_console(&vt_console_driver);
2943#endif
2944 return 0;
2945}
2946console_initcall(con_init);
2947
2948static const struct tty_operations con_ops = {
2949 .open = con_open,
2950 .close = con_close,
2951 .write = con_write,
2952 .write_room = con_write_room,
2953 .put_char = con_put_char,
2954 .flush_chars = con_flush_chars,
2955 .chars_in_buffer = con_chars_in_buffer,
2956 .ioctl = vt_ioctl,
2957#ifdef CONFIG_COMPAT
2958 .compat_ioctl = vt_compat_ioctl,
2959#endif
2960 .stop = con_stop,
2961 .start = con_start,
2962 .throttle = con_throttle,
2963 .unthrottle = con_unthrottle,
2964 .resize = vt_resize,
2965 .shutdown = con_shutdown
2966};
2967
2968static struct cdev vc0_cdev;
2969
2970static ssize_t show_tty_active(struct device *dev,
2971 struct device_attribute *attr, char *buf)
2972{
2973 return sprintf(buf, "tty%d\n", fg_console + 1);
2974}
2975static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2976
2977int __init vty_init(const struct file_operations *console_fops)
2978{
2979 cdev_init(&vc0_cdev, console_fops);
2980 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2981 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2982 panic("Couldn't register /dev/tty0 driver\n");
2983 tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2984 if (IS_ERR(tty0dev))
2985 tty0dev = NULL;
2986 else
2987 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
2988
2989 vcs_init();
2990
2991 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2992 if (!console_driver)
2993 panic("Couldn't allocate console driver\n");
2994 console_driver->owner = THIS_MODULE;
2995 console_driver->name = "tty";
2996 console_driver->name_base = 1;
2997 console_driver->major = TTY_MAJOR;
2998 console_driver->minor_start = 1;
2999 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3000 console_driver->init_termios = tty_std_termios;
3001 if (default_utf8)
3002 console_driver->init_termios.c_iflag |= IUTF8;
3003 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3004 tty_set_operations(console_driver, &con_ops);
3005 if (tty_register_driver(console_driver))
3006 panic("Couldn't register console driver\n");
3007 kbd_init();
3008 console_map_init();
3009#ifdef CONFIG_MDA_CONSOLE
3010 mda_console_init();
3011#endif
3012 return 0;
3013}
3014
3015#ifndef VT_SINGLE_DRIVER
3016
3017static struct class *vtconsole_class;
3018
3019static int bind_con_driver(const struct consw *csw, int first, int last,
3020 int deflt)
3021{
3022 struct module *owner = csw->owner;
3023 const char *desc = NULL;
3024 struct con_driver *con_driver;
3025 int i, j = -1, k = -1, retval = -ENODEV;
3026
3027 if (!try_module_get(owner))
3028 return -ENODEV;
3029
3030 console_lock();
3031
3032 /* check if driver is registered */
3033 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3034 con_driver = &registered_con_driver[i];
3035
3036 if (con_driver->con == csw) {
3037 desc = con_driver->desc;
3038 retval = 0;
3039 break;
3040 }
3041 }
3042
3043 if (retval)
3044 goto err;
3045
3046 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3047 csw->con_startup();
3048 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3049 }
3050
3051 if (deflt) {
3052 if (conswitchp)
3053 module_put(conswitchp->owner);
3054
3055 __module_get(owner);
3056 conswitchp = csw;
3057 }
3058
3059 first = max(first, con_driver->first);
3060 last = min(last, con_driver->last);
3061
3062 for (i = first; i <= last; i++) {
3063 int old_was_color;
3064 struct vc_data *vc = vc_cons[i].d;
3065
3066 if (con_driver_map[i])
3067 module_put(con_driver_map[i]->owner);
3068 __module_get(owner);
3069 con_driver_map[i] = csw;
3070
3071 if (!vc || !vc->vc_sw)
3072 continue;
3073
3074 j = i;
3075
3076 if (CON_IS_VISIBLE(vc)) {
3077 k = i;
3078 save_screen(vc);
3079 }
3080
3081 old_was_color = vc->vc_can_do_color;
3082 vc->vc_sw->con_deinit(vc);
3083 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3084 visual_init(vc, i, 0);
3085 set_origin(vc);
3086 update_attr(vc);
3087
3088 /* If the console changed between mono <-> color, then
3089 * the attributes in the screenbuf will be wrong. The
3090 * following resets all attributes to something sane.
3091 */
3092 if (old_was_color != vc->vc_can_do_color)
3093 clear_buffer_attributes(vc);
3094 }
3095
3096 pr_info("Console: switching ");
3097 if (!deflt)
3098 printk("consoles %d-%d ", first+1, last+1);
3099 if (j >= 0) {
3100 struct vc_data *vc = vc_cons[j].d;
3101
3102 printk("to %s %s %dx%d\n",
3103 vc->vc_can_do_color ? "colour" : "mono",
3104 desc, vc->vc_cols, vc->vc_rows);
3105
3106 if (k >= 0) {
3107 vc = vc_cons[k].d;
3108 update_screen(vc);
3109 }
3110 } else
3111 printk("to %s\n", desc);
3112
3113 retval = 0;
3114err:
3115 console_unlock();
3116 module_put(owner);
3117 return retval;
3118};
3119
3120#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3121static int con_is_graphics(const struct consw *csw, int first, int last)
3122{
3123 int i, retval = 0;
3124
3125 for (i = first; i <= last; i++) {
3126 struct vc_data *vc = vc_cons[i].d;
3127
3128 if (vc && vc->vc_mode == KD_GRAPHICS) {
3129 retval = 1;
3130 break;
3131 }
3132 }
3133
3134 return retval;
3135}
3136
3137/**
3138 * unbind_con_driver - unbind a console driver
3139 * @csw: pointer to console driver to unregister
3140 * @first: first in range of consoles that @csw should be unbound from
3141 * @last: last in range of consoles that @csw should be unbound from
3142 * @deflt: should next bound console driver be default after @csw is unbound?
3143 *
3144 * To unbind a driver from all possible consoles, pass 0 as @first and
3145 * %MAX_NR_CONSOLES as @last.
3146 *
3147 * @deflt controls whether the console that ends up replacing @csw should be
3148 * the default console.
3149 *
3150 * RETURNS:
3151 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3152 * or 0 on success.
3153 */
3154int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3155{
3156 struct module *owner = csw->owner;
3157 const struct consw *defcsw = NULL;
3158 struct con_driver *con_driver = NULL, *con_back = NULL;
3159 int i, retval = -ENODEV;
3160
3161 if (!try_module_get(owner))
3162 return -ENODEV;
3163
3164 console_lock();
3165
3166 /* check if driver is registered and if it is unbindable */
3167 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3168 con_driver = &registered_con_driver[i];
3169
3170 if (con_driver->con == csw &&
3171 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3172 retval = 0;
3173 break;
3174 }
3175 }
3176
3177 if (retval) {
3178 console_unlock();
3179 goto err;
3180 }
3181
3182 retval = -ENODEV;
3183
3184 /* check if backup driver exists */
3185 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3186 con_back = &registered_con_driver[i];
3187
3188 if (con_back->con &&
3189 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
3190 defcsw = con_back->con;
3191 retval = 0;
3192 break;
3193 }
3194 }
3195
3196 if (retval) {
3197 console_unlock();
3198 goto err;
3199 }
3200
3201 if (!con_is_bound(csw)) {
3202 console_unlock();
3203 goto err;
3204 }
3205
3206 first = max(first, con_driver->first);
3207 last = min(last, con_driver->last);
3208
3209 for (i = first; i <= last; i++) {
3210 if (con_driver_map[i] == csw) {
3211 module_put(csw->owner);
3212 con_driver_map[i] = NULL;
3213 }
3214 }
3215
3216 if (!con_is_bound(defcsw)) {
3217 const struct consw *defconsw = conswitchp;
3218
3219 defcsw->con_startup();
3220 con_back->flag |= CON_DRIVER_FLAG_INIT;
3221 /*
3222 * vgacon may change the default driver to point
3223 * to dummycon, we restore it here...
3224 */
3225 conswitchp = defconsw;
3226 }
3227
3228 if (!con_is_bound(csw))
3229 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3230
3231 console_unlock();
3232 /* ignore return value, binding should not fail */
3233 bind_con_driver(defcsw, first, last, deflt);
3234err:
3235 module_put(owner);
3236 return retval;
3237
3238}
3239EXPORT_SYMBOL(unbind_con_driver);
3240
3241static int vt_bind(struct con_driver *con)
3242{
3243 const struct consw *defcsw = NULL, *csw = NULL;
3244 int i, more = 1, first = -1, last = -1, deflt = 0;
3245
3246 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3247 con_is_graphics(con->con, con->first, con->last))
3248 goto err;
3249
3250 csw = con->con;
3251
3252 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3253 struct con_driver *con = &registered_con_driver[i];
3254
3255 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3256 defcsw = con->con;
3257 break;
3258 }
3259 }
3260
3261 if (!defcsw)
3262 goto err;
3263
3264 while (more) {
3265 more = 0;
3266
3267 for (i = con->first; i <= con->last; i++) {
3268 if (con_driver_map[i] == defcsw) {
3269 if (first == -1)
3270 first = i;
3271 last = i;
3272 more = 1;
3273 } else if (first != -1)
3274 break;
3275 }
3276
3277 if (first == 0 && last == MAX_NR_CONSOLES -1)
3278 deflt = 1;
3279
3280 if (first != -1)
3281 bind_con_driver(csw, first, last, deflt);
3282
3283 first = -1;
3284 last = -1;
3285 deflt = 0;
3286 }
3287
3288err:
3289 return 0;
3290}
3291
3292static int vt_unbind(struct con_driver *con)
3293{
3294 const struct consw *csw = NULL;
3295 int i, more = 1, first = -1, last = -1, deflt = 0;
3296
3297 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3298 con_is_graphics(con->con, con->first, con->last))
3299 goto err;
3300
3301 csw = con->con;
3302
3303 while (more) {
3304 more = 0;
3305
3306 for (i = con->first; i <= con->last; i++) {
3307 if (con_driver_map[i] == csw) {
3308 if (first == -1)
3309 first = i;
3310 last = i;
3311 more = 1;
3312 } else if (first != -1)
3313 break;
3314 }
3315
3316 if (first == 0 && last == MAX_NR_CONSOLES -1)
3317 deflt = 1;
3318
3319 if (first != -1)
3320 unbind_con_driver(csw, first, last, deflt);
3321
3322 first = -1;
3323 last = -1;
3324 deflt = 0;
3325 }
3326
3327err:
3328 return 0;
3329}
3330#else
3331static inline int vt_bind(struct con_driver *con)
3332{
3333 return 0;
3334}
3335static inline int vt_unbind(struct con_driver *con)
3336{
3337 return 0;
3338}
3339#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3340
3341static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3342 const char *buf, size_t count)
3343{
3344 struct con_driver *con = dev_get_drvdata(dev);
3345 int bind = simple_strtoul(buf, NULL, 0);
3346
3347 if (bind)
3348 vt_bind(con);
3349 else
3350 vt_unbind(con);
3351
3352 return count;
3353}
3354
3355static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3356 char *buf)
3357{
3358 struct con_driver *con = dev_get_drvdata(dev);
3359 int bind = con_is_bound(con->con);
3360
3361 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3362}
3363
3364static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3365 char *buf)
3366{
3367 struct con_driver *con = dev_get_drvdata(dev);
3368
3369 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3370 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3371 con->desc);
3372
3373}
3374
3375static struct device_attribute device_attrs[] = {
3376 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3377 __ATTR(name, S_IRUGO, show_name, NULL),
3378};
3379
3380static int vtconsole_init_device(struct con_driver *con)
3381{
3382 int i;
3383 int error = 0;
3384
3385 con->flag |= CON_DRIVER_FLAG_ATTR;
3386 dev_set_drvdata(con->dev, con);
3387 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3388 error = device_create_file(con->dev, &device_attrs[i]);
3389 if (error)
3390 break;
3391 }
3392
3393 if (error) {
3394 while (--i >= 0)
3395 device_remove_file(con->dev, &device_attrs[i]);
3396 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3397 }
3398
3399 return error;
3400}
3401
3402static void vtconsole_deinit_device(struct con_driver *con)
3403{
3404 int i;
3405
3406 if (con->flag & CON_DRIVER_FLAG_ATTR) {
3407 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3408 device_remove_file(con->dev, &device_attrs[i]);
3409 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3410 }
3411}
3412
3413/**
3414 * con_is_bound - checks if driver is bound to the console
3415 * @csw: console driver
3416 *
3417 * RETURNS: zero if unbound, nonzero if bound
3418 *
3419 * Drivers can call this and if zero, they should release
3420 * all resources allocated on con_startup()
3421 */
3422int con_is_bound(const struct consw *csw)
3423{
3424 int i, bound = 0;
3425
3426 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3427 if (con_driver_map[i] == csw) {
3428 bound = 1;
3429 break;
3430 }
3431 }
3432
3433 return bound;
3434}
3435EXPORT_SYMBOL(con_is_bound);
3436
3437/**
3438 * con_debug_enter - prepare the console for the kernel debugger
3439 * @sw: console driver
3440 *
3441 * Called when the console is taken over by the kernel debugger, this
3442 * function needs to save the current console state, then put the console
3443 * into a state suitable for the kernel debugger.
3444 *
3445 * RETURNS:
3446 * Zero on success, nonzero if a failure occurred when trying to prepare
3447 * the console for the debugger.
3448 */
3449int con_debug_enter(struct vc_data *vc)
3450{
3451 int ret = 0;
3452
3453 saved_fg_console = fg_console;
3454 saved_last_console = last_console;
3455 saved_want_console = want_console;
3456 saved_vc_mode = vc->vc_mode;
3457 saved_console_blanked = console_blanked;
3458 vc->vc_mode = KD_TEXT;
3459 console_blanked = 0;
3460 if (vc->vc_sw->con_debug_enter)
3461 ret = vc->vc_sw->con_debug_enter(vc);
3462#ifdef CONFIG_KGDB_KDB
3463 /* Set the initial LINES variable if it is not already set */
3464 if (vc->vc_rows < 999) {
3465 int linecount;
3466 char lns[4];
3467 const char *setargs[3] = {
3468 "set",
3469 "LINES",
3470 lns,
3471 };
3472 if (kdbgetintenv(setargs[0], &linecount)) {
3473 snprintf(lns, 4, "%i", vc->vc_rows);
3474 kdb_set(2, setargs);
3475 }
3476 }
3477#endif /* CONFIG_KGDB_KDB */
3478 return ret;
3479}
3480EXPORT_SYMBOL_GPL(con_debug_enter);
3481
3482/**
3483 * con_debug_leave - restore console state
3484 * @sw: console driver
3485 *
3486 * Restore the console state to what it was before the kernel debugger
3487 * was invoked.
3488 *
3489 * RETURNS:
3490 * Zero on success, nonzero if a failure occurred when trying to restore
3491 * the console.
3492 */
3493int con_debug_leave(void)
3494{
3495 struct vc_data *vc;
3496 int ret = 0;
3497
3498 fg_console = saved_fg_console;
3499 last_console = saved_last_console;
3500 want_console = saved_want_console;
3501 console_blanked = saved_console_blanked;
3502 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3503
3504 vc = vc_cons[fg_console].d;
3505 if (vc->vc_sw->con_debug_leave)
3506 ret = vc->vc_sw->con_debug_leave(vc);
3507 return ret;
3508}
3509EXPORT_SYMBOL_GPL(con_debug_leave);
3510
3511/**
3512 * register_con_driver - register console driver to console layer
3513 * @csw: console driver
3514 * @first: the first console to take over, minimum value is 0
3515 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3516 *
3517 * DESCRIPTION: This function registers a console driver which can later
3518 * bind to a range of consoles specified by @first and @last. It will
3519 * also initialize the console driver by calling con_startup().
3520 */
3521int register_con_driver(const struct consw *csw, int first, int last)
3522{
3523 struct module *owner = csw->owner;
3524 struct con_driver *con_driver;
3525 const char *desc;
3526 int i, retval = 0;
3527
3528 if (!try_module_get(owner))
3529 return -ENODEV;
3530
3531 console_lock();
3532
3533 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3534 con_driver = &registered_con_driver[i];
3535
3536 /* already registered */
3537 if (con_driver->con == csw)
3538 retval = -EBUSY;
3539 }
3540
3541 if (retval)
3542 goto err;
3543
3544 desc = csw->con_startup();
3545
3546 if (!desc)
3547 goto err;
3548
3549 retval = -EINVAL;
3550
3551 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3552 con_driver = &registered_con_driver[i];
3553
3554 if (con_driver->con == NULL) {
3555 con_driver->con = csw;
3556 con_driver->desc = desc;
3557 con_driver->node = i;
3558 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3559 CON_DRIVER_FLAG_INIT;
3560 con_driver->first = first;
3561 con_driver->last = last;
3562 retval = 0;
3563 break;
3564 }
3565 }
3566
3567 if (retval)
3568 goto err;
3569
3570 con_driver->dev = device_create(vtconsole_class, NULL,
3571 MKDEV(0, con_driver->node),
3572 NULL, "vtcon%i",
3573 con_driver->node);
3574
3575 if (IS_ERR(con_driver->dev)) {
3576 printk(KERN_WARNING "Unable to create device for %s; "
3577 "errno = %ld\n", con_driver->desc,
3578 PTR_ERR(con_driver->dev));
3579 con_driver->dev = NULL;
3580 } else {
3581 vtconsole_init_device(con_driver);
3582 }
3583
3584err:
3585 console_unlock();
3586 module_put(owner);
3587 return retval;
3588}
3589EXPORT_SYMBOL(register_con_driver);
3590
3591/**
3592 * unregister_con_driver - unregister console driver from console layer
3593 * @csw: console driver
3594 *
3595 * DESCRIPTION: All drivers that registers to the console layer must
3596 * call this function upon exit, or if the console driver is in a state
3597 * where it won't be able to handle console services, such as the
3598 * framebuffer console without loaded framebuffer drivers.
3599 *
3600 * The driver must unbind first prior to unregistration.
3601 */
3602int unregister_con_driver(const struct consw *csw)
3603{
3604 int i, retval = -ENODEV;
3605
3606 console_lock();
3607
3608 /* cannot unregister a bound driver */
3609 if (con_is_bound(csw))
3610 goto err;
3611
3612 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3613 struct con_driver *con_driver = &registered_con_driver[i];
3614
3615 if (con_driver->con == csw &&
3616 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3617 vtconsole_deinit_device(con_driver);
3618 device_destroy(vtconsole_class,
3619 MKDEV(0, con_driver->node));
3620 con_driver->con = NULL;
3621 con_driver->desc = NULL;
3622 con_driver->dev = NULL;
3623 con_driver->node = 0;
3624 con_driver->flag = 0;
3625 con_driver->first = 0;
3626 con_driver->last = 0;
3627 retval = 0;
3628 break;
3629 }
3630 }
3631err:
3632 console_unlock();
3633 return retval;
3634}
3635EXPORT_SYMBOL(unregister_con_driver);
3636
3637/*
3638 * If we support more console drivers, this function is used
3639 * when a driver wants to take over some existing consoles
3640 * and become default driver for newly opened ones.
3641 *
3642 * take_over_console is basically a register followed by unbind
3643 */
3644int take_over_console(const struct consw *csw, int first, int last, int deflt)
3645{
3646 int err;
3647
3648 err = register_con_driver(csw, first, last);
3649 /* if we get an busy error we still want to bind the console driver
3650 * and return success, as we may have unbound the console driver
3651  * but not unregistered it.
3652 */
3653 if (err == -EBUSY)
3654 err = 0;
3655 if (!err)
3656 bind_con_driver(csw, first, last, deflt);
3657
3658 return err;
3659}
3660
3661/*
3662 * give_up_console is a wrapper to unregister_con_driver. It will only
3663 * work if driver is fully unbound.
3664 */
3665void give_up_console(const struct consw *csw)
3666{
3667 unregister_con_driver(csw);
3668}
3669
3670static int __init vtconsole_class_init(void)
3671{
3672 int i;
3673
3674 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3675 if (IS_ERR(vtconsole_class)) {
3676 printk(KERN_WARNING "Unable to create vt console class; "
3677 "errno = %ld\n", PTR_ERR(vtconsole_class));
3678 vtconsole_class = NULL;
3679 }
3680
3681 /* Add system drivers to sysfs */
3682 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3683 struct con_driver *con = &registered_con_driver[i];
3684
3685 if (con->con && !con->dev) {
3686 con->dev = device_create(vtconsole_class, NULL,
3687 MKDEV(0, con->node),
3688 NULL, "vtcon%i",
3689 con->node);
3690
3691 if (IS_ERR(con->dev)) {
3692 printk(KERN_WARNING "Unable to create "
3693 "device for %s; errno = %ld\n",
3694 con->desc, PTR_ERR(con->dev));
3695 con->dev = NULL;
3696 } else {
3697 vtconsole_init_device(con);
3698 }
3699 }
3700 }
3701
3702 return 0;
3703}
3704postcore_initcall(vtconsole_class_init);
3705
3706#endif
3707
3708/*
3709 * Screen blanking
3710 */
3711
3712static int set_vesa_blanking(char __user *p)
3713{
3714 unsigned int mode;
3715
3716 if (get_user(mode, p + 1))
3717 return -EFAULT;
3718
3719 vesa_blank_mode = (mode < 4) ? mode : 0;
3720 return 0;
3721}
3722
3723void do_blank_screen(int entering_gfx)
3724{
3725 struct vc_data *vc = vc_cons[fg_console].d;
3726 int i;
3727
3728 WARN_CONSOLE_UNLOCKED();
3729
3730 if (console_blanked) {
3731 if (blank_state == blank_vesa_wait) {
3732 blank_state = blank_off;
3733 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
3734 }
3735 return;
3736 }
3737
3738 /* entering graphics mode? */
3739 if (entering_gfx) {
3740 hide_cursor(vc);
3741 save_screen(vc);
3742 vc->vc_sw->con_blank(vc, -1, 1);
3743 console_blanked = fg_console + 1;
3744 blank_state = blank_off;
3745 set_origin(vc);
3746 return;
3747 }
3748
3749 if (blank_state != blank_normal_wait)
3750 return;
3751 blank_state = blank_off;
3752
3753 /* don't blank graphics */
3754 if (vc->vc_mode != KD_TEXT) {
3755 console_blanked = fg_console + 1;
3756 return;
3757 }
3758
3759 hide_cursor(vc);
3760 del_timer_sync(&console_timer);
3761 blank_timer_expired = 0;
3762
3763 save_screen(vc);
3764 /* In case we need to reset origin, blanking hook returns 1 */
3765 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
3766 console_blanked = fg_console + 1;
3767 if (i)
3768 set_origin(vc);
3769
3770 if (console_blank_hook && console_blank_hook(1))
3771 return;
3772
3773 if (vesa_off_interval && vesa_blank_mode) {
3774 blank_state = blank_vesa_wait;
3775 mod_timer(&console_timer, jiffies + vesa_off_interval);
3776 }
3777 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
3778}
3779EXPORT_SYMBOL(do_blank_screen);
3780
3781/*
3782 * Called by timer as well as from vt_console_driver
3783 */
3784void do_unblank_screen(int leaving_gfx)
3785{
3786 struct vc_data *vc;
3787
3788 /* This should now always be called from a "sane" (read: can schedule)
3789 * context for the sake of the low level drivers, except in the special
3790 * case of oops_in_progress
3791 */
3792 if (!oops_in_progress)
3793 might_sleep();
3794
3795 WARN_CONSOLE_UNLOCKED();
3796
3797 ignore_poke = 0;
3798 if (!console_blanked)
3799 return;
3800 if (!vc_cons_allocated(fg_console)) {
3801 /* impossible */
3802 pr_warning("unblank_screen: tty %d not allocated ??\n",
3803 fg_console+1);
3804 return;
3805 }
3806 vc = vc_cons[fg_console].d;
3807 /* Try to unblank in oops case too */
3808 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
3809 return; /* but leave console_blanked != 0 */
3810
3811 if (blankinterval) {
3812 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3813 blank_state = blank_normal_wait;
3814 }
3815
3816 console_blanked = 0;
3817 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
3818 /* Low-level driver cannot restore -> do it ourselves */
3819 update_screen(vc);
3820 if (console_blank_hook)
3821 console_blank_hook(0);
3822 set_palette(vc);
3823 set_cursor(vc);
3824 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
3825}
3826EXPORT_SYMBOL(do_unblank_screen);
3827
3828/*
3829 * This is called by the outside world to cause a forced unblank, mostly for
3830 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3831 * call it with 1 as an argument and so force a mode restore... that may kill
3832 * X or at least garbage the screen but would also make the Oops visible...
3833 */
3834void unblank_screen(void)
3835{
3836 do_unblank_screen(0);
3837}
3838
3839/*
3840 * We defer the timer blanking to work queue so it can take the console mutex
3841 * (console operations can still happen at irq time, but only from printk which
3842 * has the console mutex. Not perfect yet, but better than no locking
3843 */
3844static void blank_screen_t(unsigned long dummy)
3845{
3846 if (unlikely(!keventd_up())) {
3847 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3848 return;
3849 }
3850 blank_timer_expired = 1;
3851 schedule_work(&console_work);
3852}
3853
3854void poke_blanked_console(void)
3855{
3856 WARN_CONSOLE_UNLOCKED();
3857
3858 /* Add this so we quickly catch whoever might call us in a non
3859 * safe context. Nowadays, unblank_screen() isn't to be called in
3860 * atomic contexts and is allowed to schedule (with the special case
3861 * of oops_in_progress, but that isn't of any concern for this
3862 * function. --BenH.
3863 */
3864 might_sleep();
3865
3866 /* This isn't perfectly race free, but a race here would be mostly harmless,
3867 * at worse, we'll do a spurrious blank and it's unlikely
3868 */
3869 del_timer(&console_timer);
3870 blank_timer_expired = 0;
3871
3872 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3873 return;
3874 if (console_blanked)
3875 unblank_screen();
3876 else if (blankinterval) {
3877 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3878 blank_state = blank_normal_wait;
3879 }
3880}
3881
3882/*
3883 * Palettes
3884 */
3885
3886static void set_palette(struct vc_data *vc)
3887{
3888 WARN_CONSOLE_UNLOCKED();
3889
3890 if (vc->vc_mode != KD_GRAPHICS)
3891 vc->vc_sw->con_set_palette(vc, color_table);
3892}
3893
3894static int set_get_cmap(unsigned char __user *arg, int set)
3895{
3896 int i, j, k;
3897
3898 WARN_CONSOLE_UNLOCKED();
3899
3900 for (i = 0; i < 16; i++)
3901 if (set) {
3902 get_user(default_red[i], arg++);
3903 get_user(default_grn[i], arg++);
3904 get_user(default_blu[i], arg++);
3905 } else {
3906 put_user(default_red[i], arg++);
3907 put_user(default_grn[i], arg++);
3908 put_user(default_blu[i], arg++);
3909 }
3910 if (set) {
3911 for (i = 0; i < MAX_NR_CONSOLES; i++)
3912 if (vc_cons_allocated(i)) {
3913 for (j = k = 0; j < 16; j++) {
3914 vc_cons[i].d->vc_palette[k++] = default_red[j];
3915 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3916 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3917 }
3918 set_palette(vc_cons[i].d);
3919 }
3920 }
3921 return 0;
3922}
3923
3924/*
3925 * Load palette into the DAC registers. arg points to a colour
3926 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3927 */
3928
3929int con_set_cmap(unsigned char __user *arg)
3930{
3931 int rc;
3932
3933 console_lock();
3934 rc = set_get_cmap (arg,1);
3935 console_unlock();
3936
3937 return rc;
3938}
3939
3940int con_get_cmap(unsigned char __user *arg)
3941{
3942 int rc;
3943
3944 console_lock();
3945 rc = set_get_cmap (arg,0);
3946 console_unlock();
3947
3948 return rc;
3949}
3950
3951void reset_palette(struct vc_data *vc)
3952{
3953 int j, k;
3954 for (j=k=0; j<16; j++) {
3955 vc->vc_palette[k++] = default_red[j];
3956 vc->vc_palette[k++] = default_grn[j];
3957 vc->vc_palette[k++] = default_blu[j];
3958 }
3959 set_palette(vc);
3960}
3961
3962/*
3963 * Font switching
3964 *
3965 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3966 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3967 * depending on width) reserved for each character which is kinda wasty, but
3968 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3969 * is up to the actual low-level console-driver convert data into its favorite
3970 * format (maybe we should add a `fontoffset' field to the `display'
3971 * structure so we won't have to convert the fontdata all the time.
3972 * /Jes
3973 */
3974
3975#define max_font_size 65536
3976
3977static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3978{
3979 struct console_font font;
3980 int rc = -EINVAL;
3981 int c;
3982
3983 if (vc->vc_mode != KD_TEXT)
3984 return -EINVAL;
3985
3986 if (op->data) {
3987 font.data = kmalloc(max_font_size, GFP_KERNEL);
3988 if (!font.data)
3989 return -ENOMEM;
3990 } else
3991 font.data = NULL;
3992
3993 console_lock();
3994 if (vc->vc_sw->con_font_get)
3995 rc = vc->vc_sw->con_font_get(vc, &font);
3996 else
3997 rc = -ENOSYS;
3998 console_unlock();
3999
4000 if (rc)
4001 goto out;
4002
4003 c = (font.width+7)/8 * 32 * font.charcount;
4004
4005 if (op->data && font.charcount > op->charcount)
4006 rc = -ENOSPC;
4007 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4008 if (font.width > op->width || font.height > op->height)
4009 rc = -ENOSPC;
4010 } else {
4011 if (font.width != 8)
4012 rc = -EIO;
4013 else if ((op->height && font.height > op->height) ||
4014 font.height > 32)
4015 rc = -ENOSPC;
4016 }
4017 if (rc)
4018 goto out;
4019
4020 op->height = font.height;
4021 op->width = font.width;
4022 op->charcount = font.charcount;
4023
4024 if (op->data && copy_to_user(op->data, font.data, c))
4025 rc = -EFAULT;
4026
4027out:
4028 kfree(font.data);
4029 return rc;
4030}
4031
4032static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4033{
4034 struct console_font font;
4035 int rc = -EINVAL;
4036 int size;
4037
4038 if (vc->vc_mode != KD_TEXT)
4039 return -EINVAL;
4040 if (!op->data)
4041 return -EINVAL;
4042 if (op->charcount > 512)
4043 return -EINVAL;
4044 if (!op->height) { /* Need to guess font height [compat] */
4045 int h, i;
4046 u8 __user *charmap = op->data;
4047 u8 tmp;
4048
4049 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4050 so that we can get rid of this soon */
4051 if (!(op->flags & KD_FONT_FLAG_OLD))
4052 return -EINVAL;
4053 for (h = 32; h > 0; h--)
4054 for (i = 0; i < op->charcount; i++) {
4055 if (get_user(tmp, &charmap[32*i+h-1]))
4056 return -EFAULT;
4057 if (tmp)
4058 goto nonzero;
4059 }
4060 return -EINVAL;
4061 nonzero:
4062 op->height = h;
4063 }
4064 if (op->width <= 0 || op->width > 32 || op->height > 32)
4065 return -EINVAL;
4066 size = (op->width+7)/8 * 32 * op->charcount;
4067 if (size > max_font_size)
4068 return -ENOSPC;
4069 font.charcount = op->charcount;
4070 font.height = op->height;
4071 font.width = op->width;
4072 font.data = memdup_user(op->data, size);
4073 if (IS_ERR(font.data))
4074 return PTR_ERR(font.data);
4075 console_lock();
4076 if (vc->vc_sw->con_font_set)
4077 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4078 else
4079 rc = -ENOSYS;
4080 console_unlock();
4081 kfree(font.data);
4082 return rc;
4083}
4084
4085static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4086{
4087 struct console_font font = {.width = op->width, .height = op->height};
4088 char name[MAX_FONT_NAME];
4089 char *s = name;
4090 int rc;
4091
4092 if (vc->vc_mode != KD_TEXT)
4093 return -EINVAL;
4094
4095 if (!op->data)
4096 s = NULL;
4097 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4098 return -EFAULT;
4099 else
4100 name[MAX_FONT_NAME - 1] = 0;
4101
4102 console_lock();
4103 if (vc->vc_sw->con_font_default)
4104 rc = vc->vc_sw->con_font_default(vc, &font, s);
4105 else
4106 rc = -ENOSYS;
4107 console_unlock();
4108 if (!rc) {
4109 op->width = font.width;
4110 op->height = font.height;
4111 }
4112 return rc;
4113}
4114
4115static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4116{
4117 int con = op->height;
4118 int rc;
4119
4120 if (vc->vc_mode != KD_TEXT)
4121 return -EINVAL;
4122
4123 console_lock();
4124 if (!vc->vc_sw->con_font_copy)
4125 rc = -ENOSYS;
4126 else if (con < 0 || !vc_cons_allocated(con))
4127 rc = -ENOTTY;
4128 else if (con == vc->vc_num) /* nothing to do */
4129 rc = 0;
4130 else
4131 rc = vc->vc_sw->con_font_copy(vc, con);
4132 console_unlock();
4133 return rc;
4134}
4135
4136int con_font_op(struct vc_data *vc, struct console_font_op *op)
4137{
4138 switch (op->op) {
4139 case KD_FONT_OP_SET:
4140 return con_font_set(vc, op);
4141 case KD_FONT_OP_GET:
4142 return con_font_get(vc, op);
4143 case KD_FONT_OP_SET_DEFAULT:
4144 return con_font_default(vc, op);
4145 case KD_FONT_OP_COPY:
4146 return con_font_copy(vc, op);
4147 }
4148 return -ENOSYS;
4149}
4150
4151/*
4152 * Interface exported to selection and vcs.
4153 */
4154
4155/* used by selection */
4156u16 screen_glyph(struct vc_data *vc, int offset)
4157{
4158 u16 w = scr_readw(screenpos(vc, offset, 1));
4159 u16 c = w & 0xff;
4160
4161 if (w & vc->vc_hi_font_mask)
4162 c |= 0x100;
4163 return c;
4164}
4165EXPORT_SYMBOL_GPL(screen_glyph);
4166
4167/* used by vcs - note the word offset */
4168unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4169{
4170 return screenpos(vc, 2 * w_offset, viewed);
4171}
4172
4173void getconsxy(struct vc_data *vc, unsigned char *p)
4174{
4175 p[0] = vc->vc_x;
4176 p[1] = vc->vc_y;
4177}
4178
4179void putconsxy(struct vc_data *vc, unsigned char *p)
4180{
4181 hide_cursor(vc);
4182 gotoxy(vc, p[0], p[1]);
4183 set_cursor(vc);
4184}
4185
4186u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4187{
4188 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4189 return softcursor_original;
4190 return scr_readw(org);
4191}
4192
4193void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4194{
4195 scr_writew(val, org);
4196 if ((unsigned long)org == vc->vc_pos) {
4197 softcursor_original = -1;
4198 add_softcursor(vc);
4199 }
4200}
4201
4202void vcs_scr_updated(struct vc_data *vc)
4203{
4204 notify_update(vc);
4205}
4206
4207/*
4208 * Visible symbols for modules
4209 */
4210
4211EXPORT_SYMBOL(color_table);
4212EXPORT_SYMBOL(default_red);
4213EXPORT_SYMBOL(default_grn);
4214EXPORT_SYMBOL(default_blu);
4215EXPORT_SYMBOL(update_region);
4216EXPORT_SYMBOL(redraw_screen);
4217EXPORT_SYMBOL(vc_resize);
4218EXPORT_SYMBOL(fg_console);
4219EXPORT_SYMBOL(console_blank_hook);
4220EXPORT_SYMBOL(console_blanked);
4221EXPORT_SYMBOL(vc_cons);
4222EXPORT_SYMBOL(global_cursor_default);
4223#ifndef VT_SINGLE_DRIVER
4224EXPORT_SYMBOL(take_over_console);
4225EXPORT_SYMBOL(give_up_console);
4226#endif
diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
new file mode 100644
index 000000000000..5e096f43bcea
--- /dev/null
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -0,0 +1,1800 @@
1/*
2 * Copyright (C) 1992 obz under the linux copyright
3 *
4 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
5 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
6 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
7 * Some code moved for less code duplication - Andi Kleen - Mar 1997
8 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
9 */
10
11#include <linux/types.h>
12#include <linux/errno.h>
13#include <linux/sched.h>
14#include <linux/tty.h>
15#include <linux/timer.h>
16#include <linux/kernel.h>
17#include <linux/compat.h>
18#include <linux/module.h>
19#include <linux/kd.h>
20#include <linux/vt.h>
21#include <linux/string.h>
22#include <linux/slab.h>
23#include <linux/major.h>
24#include <linux/fs.h>
25#include <linux/console.h>
26#include <linux/consolemap.h>
27#include <linux/signal.h>
28#include <linux/timex.h>
29
30#include <asm/io.h>
31#include <asm/uaccess.h>
32
33#include <linux/kbd_kern.h>
34#include <linux/vt_kern.h>
35#include <linux/kbd_diacr.h>
36#include <linux/selection.h>
37
38char vt_dont_switch;
39extern struct tty_driver *console_driver;
40
41#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
42#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
43
44/*
45 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
46 * experimentation and study of X386 SYSV handling.
47 *
48 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
49 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
50 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
51 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
52 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
53 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
54 * to the current console is done by the main ioctl code.
55 */
56
57#ifdef CONFIG_X86
58#include <linux/syscalls.h>
59#endif
60
61static void complete_change_console(struct vc_data *vc);
62
63/*
64 * User space VT_EVENT handlers
65 */
66
67struct vt_event_wait {
68 struct list_head list;
69 struct vt_event event;
70 int done;
71};
72
73static LIST_HEAD(vt_events);
74static DEFINE_SPINLOCK(vt_event_lock);
75static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
76
77/**
78 * vt_event_post
79 * @event: the event that occurred
80 * @old: old console
81 * @new: new console
82 *
83 * Post an VT event to interested VT handlers
84 */
85
86void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
87{
88 struct list_head *pos, *head;
89 unsigned long flags;
90 int wake = 0;
91
92 spin_lock_irqsave(&vt_event_lock, flags);
93 head = &vt_events;
94
95 list_for_each(pos, head) {
96 struct vt_event_wait *ve = list_entry(pos,
97 struct vt_event_wait, list);
98 if (!(ve->event.event & event))
99 continue;
100 ve->event.event = event;
101 /* kernel view is consoles 0..n-1, user space view is
102 console 1..n with 0 meaning current, so we must bias */
103 ve->event.oldev = old + 1;
104 ve->event.newev = new + 1;
105 wake = 1;
106 ve->done = 1;
107 }
108 spin_unlock_irqrestore(&vt_event_lock, flags);
109 if (wake)
110 wake_up_interruptible(&vt_event_waitqueue);
111}
112
113/**
114 * vt_event_wait - wait for an event
115 * @vw: our event
116 *
117 * Waits for an event to occur which completes our vt_event_wait
118 * structure. On return the structure has wv->done set to 1 for success
119 * or 0 if some event such as a signal ended the wait.
120 */
121
122static void vt_event_wait(struct vt_event_wait *vw)
123{
124 unsigned long flags;
125 /* Prepare the event */
126 INIT_LIST_HEAD(&vw->list);
127 vw->done = 0;
128 /* Queue our event */
129 spin_lock_irqsave(&vt_event_lock, flags);
130 list_add(&vw->list, &vt_events);
131 spin_unlock_irqrestore(&vt_event_lock, flags);
132 /* Wait for it to pass */
133 wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
134 /* Dequeue it */
135 spin_lock_irqsave(&vt_event_lock, flags);
136 list_del(&vw->list);
137 spin_unlock_irqrestore(&vt_event_lock, flags);
138}
139
140/**
141 * vt_event_wait_ioctl - event ioctl handler
142 * @arg: argument to ioctl
143 *
144 * Implement the VT_WAITEVENT ioctl using the VT event interface
145 */
146
147static int vt_event_wait_ioctl(struct vt_event __user *event)
148{
149 struct vt_event_wait vw;
150
151 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
152 return -EFAULT;
153 /* Highest supported event for now */
154 if (vw.event.event & ~VT_MAX_EVENT)
155 return -EINVAL;
156
157 vt_event_wait(&vw);
158 /* If it occurred report it */
159 if (vw.done) {
160 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
161 return -EFAULT;
162 return 0;
163 }
164 return -EINTR;
165}
166
167/**
168 * vt_waitactive - active console wait
169 * @event: event code
170 * @n: new console
171 *
172 * Helper for event waits. Used to implement the legacy
173 * event waiting ioctls in terms of events
174 */
175
176int vt_waitactive(int n)
177{
178 struct vt_event_wait vw;
179 do {
180 if (n == fg_console + 1)
181 break;
182 vw.event.event = VT_EVENT_SWITCH;
183 vt_event_wait(&vw);
184 if (vw.done == 0)
185 return -EINTR;
186 } while (vw.event.newev != n);
187 return 0;
188}
189
190/*
191 * these are the valid i/o ports we're allowed to change. they map all the
192 * video ports
193 */
194#define GPFIRST 0x3b4
195#define GPLAST 0x3df
196#define GPNUM (GPLAST - GPFIRST + 1)
197
198#define i (tmp.kb_index)
199#define s (tmp.kb_table)
200#define v (tmp.kb_value)
201static inline int
202do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
203{
204 struct kbentry tmp;
205 ushort *key_map, val, ov;
206
207 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
208 return -EFAULT;
209
210 if (!capable(CAP_SYS_TTY_CONFIG))
211 perm = 0;
212
213 switch (cmd) {
214 case KDGKBENT:
215 key_map = key_maps[s];
216 if (key_map) {
217 val = U(key_map[i]);
218 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
219 val = K_HOLE;
220 } else
221 val = (i ? K_HOLE : K_NOSUCHMAP);
222 return put_user(val, &user_kbe->kb_value);
223 case KDSKBENT:
224 if (!perm)
225 return -EPERM;
226 if (!i && v == K_NOSUCHMAP) {
227 /* deallocate map */
228 key_map = key_maps[s];
229 if (s && key_map) {
230 key_maps[s] = NULL;
231 if (key_map[0] == U(K_ALLOCATED)) {
232 kfree(key_map);
233 keymap_count--;
234 }
235 }
236 break;
237 }
238
239 if (KTYP(v) < NR_TYPES) {
240 if (KVAL(v) > max_vals[KTYP(v)])
241 return -EINVAL;
242 } else
243 if (kbd->kbdmode != VC_UNICODE)
244 return -EINVAL;
245
246 /* ++Geert: non-PC keyboards may generate keycode zero */
247#if !defined(__mc68000__) && !defined(__powerpc__)
248 /* assignment to entry 0 only tests validity of args */
249 if (!i)
250 break;
251#endif
252
253 if (!(key_map = key_maps[s])) {
254 int j;
255
256 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
257 !capable(CAP_SYS_RESOURCE))
258 return -EPERM;
259
260 key_map = kmalloc(sizeof(plain_map),
261 GFP_KERNEL);
262 if (!key_map)
263 return -ENOMEM;
264 key_maps[s] = key_map;
265 key_map[0] = U(K_ALLOCATED);
266 for (j = 1; j < NR_KEYS; j++)
267 key_map[j] = U(K_HOLE);
268 keymap_count++;
269 }
270 ov = U(key_map[i]);
271 if (v == ov)
272 break; /* nothing to do */
273 /*
274 * Attention Key.
275 */
276 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
277 return -EPERM;
278 key_map[i] = U(v);
279 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
280 compute_shiftstate();
281 break;
282 }
283 return 0;
284}
285#undef i
286#undef s
287#undef v
288
289static inline int
290do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
291{
292 struct kbkeycode tmp;
293 int kc = 0;
294
295 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
296 return -EFAULT;
297 switch (cmd) {
298 case KDGETKEYCODE:
299 kc = getkeycode(tmp.scancode);
300 if (kc >= 0)
301 kc = put_user(kc, &user_kbkc->keycode);
302 break;
303 case KDSETKEYCODE:
304 if (!perm)
305 return -EPERM;
306 kc = setkeycode(tmp.scancode, tmp.keycode);
307 break;
308 }
309 return kc;
310}
311
312static inline int
313do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
314{
315 struct kbsentry *kbs;
316 char *p;
317 u_char *q;
318 u_char __user *up;
319 int sz;
320 int delta;
321 char *first_free, *fj, *fnw;
322 int i, j, k;
323 int ret;
324
325 if (!capable(CAP_SYS_TTY_CONFIG))
326 perm = 0;
327
328 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
329 if (!kbs) {
330 ret = -ENOMEM;
331 goto reterr;
332 }
333
334 /* we mostly copy too much here (512bytes), but who cares ;) */
335 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
336 ret = -EFAULT;
337 goto reterr;
338 }
339 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
340 i = kbs->kb_func;
341
342 switch (cmd) {
343 case KDGKBSENT:
344 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
345 a struct member */
346 up = user_kdgkb->kb_string;
347 p = func_table[i];
348 if(p)
349 for ( ; *p && sz; p++, sz--)
350 if (put_user(*p, up++)) {
351 ret = -EFAULT;
352 goto reterr;
353 }
354 if (put_user('\0', up)) {
355 ret = -EFAULT;
356 goto reterr;
357 }
358 kfree(kbs);
359 return ((p && *p) ? -EOVERFLOW : 0);
360 case KDSKBSENT:
361 if (!perm) {
362 ret = -EPERM;
363 goto reterr;
364 }
365
366 q = func_table[i];
367 first_free = funcbufptr + (funcbufsize - funcbufleft);
368 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
369 ;
370 if (j < MAX_NR_FUNC)
371 fj = func_table[j];
372 else
373 fj = first_free;
374
375 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
376 if (delta <= funcbufleft) { /* it fits in current buf */
377 if (j < MAX_NR_FUNC) {
378 memmove(fj + delta, fj, first_free - fj);
379 for (k = j; k < MAX_NR_FUNC; k++)
380 if (func_table[k])
381 func_table[k] += delta;
382 }
383 if (!q)
384 func_table[i] = fj;
385 funcbufleft -= delta;
386 } else { /* allocate a larger buffer */
387 sz = 256;
388 while (sz < funcbufsize - funcbufleft + delta)
389 sz <<= 1;
390 fnw = kmalloc(sz, GFP_KERNEL);
391 if(!fnw) {
392 ret = -ENOMEM;
393 goto reterr;
394 }
395
396 if (!q)
397 func_table[i] = fj;
398 if (fj > funcbufptr)
399 memmove(fnw, funcbufptr, fj - funcbufptr);
400 for (k = 0; k < j; k++)
401 if (func_table[k])
402 func_table[k] = fnw + (func_table[k] - funcbufptr);
403
404 if (first_free > fj) {
405 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
406 for (k = j; k < MAX_NR_FUNC; k++)
407 if (func_table[k])
408 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
409 }
410 if (funcbufptr != func_buf)
411 kfree(funcbufptr);
412 funcbufptr = fnw;
413 funcbufleft = funcbufleft - delta + sz - funcbufsize;
414 funcbufsize = sz;
415 }
416 strcpy(func_table[i], kbs->kb_string);
417 break;
418 }
419 ret = 0;
420reterr:
421 kfree(kbs);
422 return ret;
423}
424
425static inline int
426do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
427{
428 struct consolefontdesc cfdarg;
429 int i;
430
431 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
432 return -EFAULT;
433
434 switch (cmd) {
435 case PIO_FONTX:
436 if (!perm)
437 return -EPERM;
438 op->op = KD_FONT_OP_SET;
439 op->flags = KD_FONT_FLAG_OLD;
440 op->width = 8;
441 op->height = cfdarg.charheight;
442 op->charcount = cfdarg.charcount;
443 op->data = cfdarg.chardata;
444 return con_font_op(vc_cons[fg_console].d, op);
445 case GIO_FONTX: {
446 op->op = KD_FONT_OP_GET;
447 op->flags = KD_FONT_FLAG_OLD;
448 op->width = 8;
449 op->height = cfdarg.charheight;
450 op->charcount = cfdarg.charcount;
451 op->data = cfdarg.chardata;
452 i = con_font_op(vc_cons[fg_console].d, op);
453 if (i)
454 return i;
455 cfdarg.charheight = op->height;
456 cfdarg.charcount = op->charcount;
457 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
458 return -EFAULT;
459 return 0;
460 }
461 }
462 return -EINVAL;
463}
464
465static inline int
466do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
467{
468 struct unimapdesc tmp;
469
470 if (copy_from_user(&tmp, user_ud, sizeof tmp))
471 return -EFAULT;
472 if (tmp.entries)
473 if (!access_ok(VERIFY_WRITE, tmp.entries,
474 tmp.entry_ct*sizeof(struct unipair)))
475 return -EFAULT;
476 switch (cmd) {
477 case PIO_UNIMAP:
478 if (!perm)
479 return -EPERM;
480 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
481 case GIO_UNIMAP:
482 if (!perm && fg_console != vc->vc_num)
483 return -EPERM;
484 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
485 }
486 return 0;
487}
488
489
490
491/*
492 * We handle the console-specific ioctl's here. We allow the
493 * capability to modify any console, not just the fg_console.
494 */
495int vt_ioctl(struct tty_struct *tty,
496 unsigned int cmd, unsigned long arg)
497{
498 struct vc_data *vc = tty->driver_data;
499 struct console_font_op op; /* used in multiple places here */
500 struct kbd_struct * kbd;
501 unsigned int console;
502 unsigned char ucval;
503 unsigned int uival;
504 void __user *up = (void __user *)arg;
505 int i, perm;
506 int ret = 0;
507
508 console = vc->vc_num;
509
510 tty_lock();
511
512 if (!vc_cons_allocated(console)) { /* impossible? */
513 ret = -ENOIOCTLCMD;
514 goto out;
515 }
516
517
518 /*
519 * To have permissions to do most of the vt ioctls, we either have
520 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
521 */
522 perm = 0;
523 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
524 perm = 1;
525
526 kbd = kbd_table + console;
527 switch (cmd) {
528 case TIOCLINUX:
529 ret = tioclinux(tty, arg);
530 break;
531 case KIOCSOUND:
532 if (!perm)
533 goto eperm;
534 /*
535 * The use of PIT_TICK_RATE is historic, it used to be
536 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
537 * and 2.6.36, which was a minor but unfortunate ABI
538 * change.
539 */
540 if (arg)
541 arg = PIT_TICK_RATE / arg;
542 kd_mksound(arg, 0);
543 break;
544
545 case KDMKTONE:
546 if (!perm)
547 goto eperm;
548 {
549 unsigned int ticks, count;
550
551 /*
552 * Generate the tone for the appropriate number of ticks.
553 * If the time is zero, turn off sound ourselves.
554 */
555 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
556 count = ticks ? (arg & 0xffff) : 0;
557 if (count)
558 count = PIT_TICK_RATE / count;
559 kd_mksound(count, ticks);
560 break;
561 }
562
563 case KDGKBTYPE:
564 /*
565 * this is naive.
566 */
567 ucval = KB_101;
568 goto setchar;
569
570 /*
571 * These cannot be implemented on any machine that implements
572 * ioperm() in user level (such as Alpha PCs) or not at all.
573 *
574 * XXX: you should never use these, just call ioperm directly..
575 */
576#ifdef CONFIG_X86
577 case KDADDIO:
578 case KDDELIO:
579 /*
580 * KDADDIO and KDDELIO may be able to add ports beyond what
581 * we reject here, but to be safe...
582 */
583 if (arg < GPFIRST || arg > GPLAST) {
584 ret = -EINVAL;
585 break;
586 }
587 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
588 break;
589
590 case KDENABIO:
591 case KDDISABIO:
592 ret = sys_ioperm(GPFIRST, GPNUM,
593 (cmd == KDENABIO)) ? -ENXIO : 0;
594 break;
595#endif
596
597 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
598
599 case KDKBDREP:
600 {
601 struct kbd_repeat kbrep;
602
603 if (!capable(CAP_SYS_TTY_CONFIG))
604 goto eperm;
605
606 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
607 ret = -EFAULT;
608 break;
609 }
610 ret = kbd_rate(&kbrep);
611 if (ret)
612 break;
613 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
614 ret = -EFAULT;
615 break;
616 }
617
618 case KDSETMODE:
619 /*
620 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
621 * doesn't do a whole lot. i'm not sure if it should do any
622 * restoration of modes or what...
623 *
624 * XXX It should at least call into the driver, fbdev's definitely
625 * need to restore their engine state. --BenH
626 */
627 if (!perm)
628 goto eperm;
629 switch (arg) {
630 case KD_GRAPHICS:
631 break;
632 case KD_TEXT0:
633 case KD_TEXT1:
634 arg = KD_TEXT;
635 case KD_TEXT:
636 break;
637 default:
638 ret = -EINVAL;
639 goto out;
640 }
641 if (vc->vc_mode == (unsigned char) arg)
642 break;
643 vc->vc_mode = (unsigned char) arg;
644 if (console != fg_console)
645 break;
646 /*
647 * explicitly blank/unblank the screen if switching modes
648 */
649 console_lock();
650 if (arg == KD_TEXT)
651 do_unblank_screen(1);
652 else
653 do_blank_screen(1);
654 console_unlock();
655 break;
656
657 case KDGETMODE:
658 uival = vc->vc_mode;
659 goto setint;
660
661 case KDMAPDISP:
662 case KDUNMAPDISP:
663 /*
664 * these work like a combination of mmap and KDENABIO.
665 * this could be easily finished.
666 */
667 ret = -EINVAL;
668 break;
669
670 case KDSKBMODE:
671 if (!perm)
672 goto eperm;
673 switch(arg) {
674 case K_RAW:
675 kbd->kbdmode = VC_RAW;
676 break;
677 case K_MEDIUMRAW:
678 kbd->kbdmode = VC_MEDIUMRAW;
679 break;
680 case K_XLATE:
681 kbd->kbdmode = VC_XLATE;
682 compute_shiftstate();
683 break;
684 case K_UNICODE:
685 kbd->kbdmode = VC_UNICODE;
686 compute_shiftstate();
687 break;
688 case K_OFF:
689 kbd->kbdmode = VC_OFF;
690 break;
691 default:
692 ret = -EINVAL;
693 goto out;
694 }
695 tty_ldisc_flush(tty);
696 break;
697
698 case KDGKBMODE:
699 switch (kbd->kbdmode) {
700 case VC_RAW:
701 uival = K_RAW;
702 break;
703 case VC_MEDIUMRAW:
704 uival = K_MEDIUMRAW;
705 break;
706 case VC_UNICODE:
707 uival = K_UNICODE;
708 break;
709 case VC_OFF:
710 uival = K_OFF;
711 break;
712 default:
713 uival = K_XLATE;
714 break;
715 }
716 goto setint;
717
718 /* this could be folded into KDSKBMODE, but for compatibility
719 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
720 case KDSKBMETA:
721 switch(arg) {
722 case K_METABIT:
723 clr_vc_kbd_mode(kbd, VC_META);
724 break;
725 case K_ESCPREFIX:
726 set_vc_kbd_mode(kbd, VC_META);
727 break;
728 default:
729 ret = -EINVAL;
730 }
731 break;
732
733 case KDGKBMETA:
734 uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
735 setint:
736 ret = put_user(uival, (int __user *)arg);
737 break;
738
739 case KDGETKEYCODE:
740 case KDSETKEYCODE:
741 if(!capable(CAP_SYS_TTY_CONFIG))
742 perm = 0;
743 ret = do_kbkeycode_ioctl(cmd, up, perm);
744 break;
745
746 case KDGKBENT:
747 case KDSKBENT:
748 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
749 break;
750
751 case KDGKBSENT:
752 case KDSKBSENT:
753 ret = do_kdgkb_ioctl(cmd, up, perm);
754 break;
755
756 case KDGKBDIACR:
757 {
758 struct kbdiacrs __user *a = up;
759 struct kbdiacr diacr;
760 int i;
761
762 if (put_user(accent_table_size, &a->kb_cnt)) {
763 ret = -EFAULT;
764 break;
765 }
766 for (i = 0; i < accent_table_size; i++) {
767 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
768 diacr.base = conv_uni_to_8bit(accent_table[i].base);
769 diacr.result = conv_uni_to_8bit(accent_table[i].result);
770 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
771 ret = -EFAULT;
772 break;
773 }
774 }
775 break;
776 }
777 case KDGKBDIACRUC:
778 {
779 struct kbdiacrsuc __user *a = up;
780
781 if (put_user(accent_table_size, &a->kb_cnt))
782 ret = -EFAULT;
783 else if (copy_to_user(a->kbdiacruc, accent_table,
784 accent_table_size*sizeof(struct kbdiacruc)))
785 ret = -EFAULT;
786 break;
787 }
788
789 case KDSKBDIACR:
790 {
791 struct kbdiacrs __user *a = up;
792 struct kbdiacr diacr;
793 unsigned int ct;
794 int i;
795
796 if (!perm)
797 goto eperm;
798 if (get_user(ct,&a->kb_cnt)) {
799 ret = -EFAULT;
800 break;
801 }
802 if (ct >= MAX_DIACR) {
803 ret = -EINVAL;
804 break;
805 }
806 accent_table_size = ct;
807 for (i = 0; i < ct; i++) {
808 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
809 ret = -EFAULT;
810 break;
811 }
812 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
813 accent_table[i].base = conv_8bit_to_uni(diacr.base);
814 accent_table[i].result = conv_8bit_to_uni(diacr.result);
815 }
816 break;
817 }
818
819 case KDSKBDIACRUC:
820 {
821 struct kbdiacrsuc __user *a = up;
822 unsigned int ct;
823
824 if (!perm)
825 goto eperm;
826 if (get_user(ct,&a->kb_cnt)) {
827 ret = -EFAULT;
828 break;
829 }
830 if (ct >= MAX_DIACR) {
831 ret = -EINVAL;
832 break;
833 }
834 accent_table_size = ct;
835 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
836 ret = -EFAULT;
837 break;
838 }
839
840 /* the ioctls below read/set the flags usually shown in the leds */
841 /* don't use them - they will go away without warning */
842 case KDGKBLED:
843 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
844 goto setchar;
845
846 case KDSKBLED:
847 if (!perm)
848 goto eperm;
849 if (arg & ~0x77) {
850 ret = -EINVAL;
851 break;
852 }
853 kbd->ledflagstate = (arg & 7);
854 kbd->default_ledflagstate = ((arg >> 4) & 7);
855 set_leds();
856 break;
857
858 /* the ioctls below only set the lights, not the functions */
859 /* for those, see KDGKBLED and KDSKBLED above */
860 case KDGETLED:
861 ucval = getledstate();
862 setchar:
863 ret = put_user(ucval, (char __user *)arg);
864 break;
865
866 case KDSETLED:
867 if (!perm)
868 goto eperm;
869 setledstate(kbd, arg);
870 break;
871
872 /*
873 * A process can indicate its willingness to accept signals
874 * generated by pressing an appropriate key combination.
875 * Thus, one can have a daemon that e.g. spawns a new console
876 * upon a keypress and then changes to it.
877 * See also the kbrequest field of inittab(5).
878 */
879 case KDSIGACCEPT:
880 {
881 if (!perm || !capable(CAP_KILL))
882 goto eperm;
883 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
884 ret = -EINVAL;
885 else {
886 spin_lock_irq(&vt_spawn_con.lock);
887 put_pid(vt_spawn_con.pid);
888 vt_spawn_con.pid = get_pid(task_pid(current));
889 vt_spawn_con.sig = arg;
890 spin_unlock_irq(&vt_spawn_con.lock);
891 }
892 break;
893 }
894
895 case VT_SETMODE:
896 {
897 struct vt_mode tmp;
898
899 if (!perm)
900 goto eperm;
901 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
902 ret = -EFAULT;
903 goto out;
904 }
905 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
906 ret = -EINVAL;
907 goto out;
908 }
909 console_lock();
910 vc->vt_mode = tmp;
911 /* the frsig is ignored, so we set it to 0 */
912 vc->vt_mode.frsig = 0;
913 put_pid(vc->vt_pid);
914 vc->vt_pid = get_pid(task_pid(current));
915 /* no switch is required -- saw@shade.msu.ru */
916 vc->vt_newvt = -1;
917 console_unlock();
918 break;
919 }
920
921 case VT_GETMODE:
922 {
923 struct vt_mode tmp;
924 int rc;
925
926 console_lock();
927 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
928 console_unlock();
929
930 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
931 if (rc)
932 ret = -EFAULT;
933 break;
934 }
935
936 /*
937 * Returns global vt state. Note that VT 0 is always open, since
938 * it's an alias for the current VT, and people can't use it here.
939 * We cannot return state for more than 16 VTs, since v_state is short.
940 */
941 case VT_GETSTATE:
942 {
943 struct vt_stat __user *vtstat = up;
944 unsigned short state, mask;
945
946 if (put_user(fg_console + 1, &vtstat->v_active))
947 ret = -EFAULT;
948 else {
949 state = 1; /* /dev/tty0 is always open */
950 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
951 ++i, mask <<= 1)
952 if (VT_IS_IN_USE(i))
953 state |= mask;
954 ret = put_user(state, &vtstat->v_state);
955 }
956 break;
957 }
958
959 /*
960 * Returns the first available (non-opened) console.
961 */
962 case VT_OPENQRY:
963 for (i = 0; i < MAX_NR_CONSOLES; ++i)
964 if (! VT_IS_IN_USE(i))
965 break;
966 uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
967 goto setint;
968
969 /*
970 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
971 * with num >= 1 (switches to vt 0, our console, are not allowed, just
972 * to preserve sanity).
973 */
974 case VT_ACTIVATE:
975 if (!perm)
976 goto eperm;
977 if (arg == 0 || arg > MAX_NR_CONSOLES)
978 ret = -ENXIO;
979 else {
980 arg--;
981 console_lock();
982 ret = vc_allocate(arg);
983 console_unlock();
984 if (ret)
985 break;
986 set_console(arg);
987 }
988 break;
989
990 case VT_SETACTIVATE:
991 {
992 struct vt_setactivate vsa;
993
994 if (!perm)
995 goto eperm;
996
997 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
998 sizeof(struct vt_setactivate))) {
999 ret = -EFAULT;
1000 goto out;
1001 }
1002 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
1003 ret = -ENXIO;
1004 else {
1005 vsa.console--;
1006 console_lock();
1007 ret = vc_allocate(vsa.console);
1008 if (ret == 0) {
1009 struct vc_data *nvc;
1010 /* This is safe providing we don't drop the
1011 console sem between vc_allocate and
1012 finishing referencing nvc */
1013 nvc = vc_cons[vsa.console].d;
1014 nvc->vt_mode = vsa.mode;
1015 nvc->vt_mode.frsig = 0;
1016 put_pid(nvc->vt_pid);
1017 nvc->vt_pid = get_pid(task_pid(current));
1018 }
1019 console_unlock();
1020 if (ret)
1021 break;
1022 /* Commence switch and lock */
1023 set_console(vsa.console);
1024 }
1025 break;
1026 }
1027
1028 /*
1029 * wait until the specified VT has been activated
1030 */
1031 case VT_WAITACTIVE:
1032 if (!perm)
1033 goto eperm;
1034 if (arg == 0 || arg > MAX_NR_CONSOLES)
1035 ret = -ENXIO;
1036 else
1037 ret = vt_waitactive(arg);
1038 break;
1039
1040 /*
1041 * If a vt is under process control, the kernel will not switch to it
1042 * immediately, but postpone the operation until the process calls this
1043 * ioctl, allowing the switch to complete.
1044 *
1045 * According to the X sources this is the behavior:
1046 * 0: pending switch-from not OK
1047 * 1: pending switch-from OK
1048 * 2: completed switch-to OK
1049 */
1050 case VT_RELDISP:
1051 if (!perm)
1052 goto eperm;
1053
1054 if (vc->vt_mode.mode != VT_PROCESS) {
1055 ret = -EINVAL;
1056 break;
1057 }
1058 /*
1059 * Switching-from response
1060 */
1061 console_lock();
1062 if (vc->vt_newvt >= 0) {
1063 if (arg == 0)
1064 /*
1065 * Switch disallowed, so forget we were trying
1066 * to do it.
1067 */
1068 vc->vt_newvt = -1;
1069
1070 else {
1071 /*
1072 * The current vt has been released, so
1073 * complete the switch.
1074 */
1075 int newvt;
1076 newvt = vc->vt_newvt;
1077 vc->vt_newvt = -1;
1078 ret = vc_allocate(newvt);
1079 if (ret) {
1080 console_unlock();
1081 break;
1082 }
1083 /*
1084 * When we actually do the console switch,
1085 * make sure we are atomic with respect to
1086 * other console switches..
1087 */
1088 complete_change_console(vc_cons[newvt].d);
1089 }
1090 } else {
1091 /*
1092 * Switched-to response
1093 */
1094 /*
1095 * If it's just an ACK, ignore it
1096 */
1097 if (arg != VT_ACKACQ)
1098 ret = -EINVAL;
1099 }
1100 console_unlock();
1101 break;
1102
1103 /*
1104 * Disallocate memory associated to VT (but leave VT1)
1105 */
1106 case VT_DISALLOCATE:
1107 if (arg > MAX_NR_CONSOLES) {
1108 ret = -ENXIO;
1109 break;
1110 }
1111 if (arg == 0) {
1112 /* deallocate all unused consoles, but leave 0 */
1113 console_lock();
1114 for (i=1; i<MAX_NR_CONSOLES; i++)
1115 if (! VT_BUSY(i))
1116 vc_deallocate(i);
1117 console_unlock();
1118 } else {
1119 /* deallocate a single console, if possible */
1120 arg--;
1121 if (VT_BUSY(arg))
1122 ret = -EBUSY;
1123 else if (arg) { /* leave 0 */
1124 console_lock();
1125 vc_deallocate(arg);
1126 console_unlock();
1127 }
1128 }
1129 break;
1130
1131 case VT_RESIZE:
1132 {
1133 struct vt_sizes __user *vtsizes = up;
1134 struct vc_data *vc;
1135
1136 ushort ll,cc;
1137 if (!perm)
1138 goto eperm;
1139 if (get_user(ll, &vtsizes->v_rows) ||
1140 get_user(cc, &vtsizes->v_cols))
1141 ret = -EFAULT;
1142 else {
1143 console_lock();
1144 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1145 vc = vc_cons[i].d;
1146
1147 if (vc) {
1148 vc->vc_resize_user = 1;
1149 vc_resize(vc_cons[i].d, cc, ll);
1150 }
1151 }
1152 console_unlock();
1153 }
1154 break;
1155 }
1156
1157 case VT_RESIZEX:
1158 {
1159 struct vt_consize __user *vtconsize = up;
1160 ushort ll,cc,vlin,clin,vcol,ccol;
1161 if (!perm)
1162 goto eperm;
1163 if (!access_ok(VERIFY_READ, vtconsize,
1164 sizeof(struct vt_consize))) {
1165 ret = -EFAULT;
1166 break;
1167 }
1168 /* FIXME: Should check the copies properly */
1169 __get_user(ll, &vtconsize->v_rows);
1170 __get_user(cc, &vtconsize->v_cols);
1171 __get_user(vlin, &vtconsize->v_vlin);
1172 __get_user(clin, &vtconsize->v_clin);
1173 __get_user(vcol, &vtconsize->v_vcol);
1174 __get_user(ccol, &vtconsize->v_ccol);
1175 vlin = vlin ? vlin : vc->vc_scan_lines;
1176 if (clin) {
1177 if (ll) {
1178 if (ll != vlin/clin) {
1179 /* Parameters don't add up */
1180 ret = -EINVAL;
1181 break;
1182 }
1183 } else
1184 ll = vlin/clin;
1185 }
1186 if (vcol && ccol) {
1187 if (cc) {
1188 if (cc != vcol/ccol) {
1189 ret = -EINVAL;
1190 break;
1191 }
1192 } else
1193 cc = vcol/ccol;
1194 }
1195
1196 if (clin > 32) {
1197 ret = -EINVAL;
1198 break;
1199 }
1200
1201 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1202 if (!vc_cons[i].d)
1203 continue;
1204 console_lock();
1205 if (vlin)
1206 vc_cons[i].d->vc_scan_lines = vlin;
1207 if (clin)
1208 vc_cons[i].d->vc_font.height = clin;
1209 vc_cons[i].d->vc_resize_user = 1;
1210 vc_resize(vc_cons[i].d, cc, ll);
1211 console_unlock();
1212 }
1213 break;
1214 }
1215
1216 case PIO_FONT: {
1217 if (!perm)
1218 goto eperm;
1219 op.op = KD_FONT_OP_SET;
1220 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1221 op.width = 8;
1222 op.height = 0;
1223 op.charcount = 256;
1224 op.data = up;
1225 ret = con_font_op(vc_cons[fg_console].d, &op);
1226 break;
1227 }
1228
1229 case GIO_FONT: {
1230 op.op = KD_FONT_OP_GET;
1231 op.flags = KD_FONT_FLAG_OLD;
1232 op.width = 8;
1233 op.height = 32;
1234 op.charcount = 256;
1235 op.data = up;
1236 ret = con_font_op(vc_cons[fg_console].d, &op);
1237 break;
1238 }
1239
1240 case PIO_CMAP:
1241 if (!perm)
1242 ret = -EPERM;
1243 else
1244 ret = con_set_cmap(up);
1245 break;
1246
1247 case GIO_CMAP:
1248 ret = con_get_cmap(up);
1249 break;
1250
1251 case PIO_FONTX:
1252 case GIO_FONTX:
1253 ret = do_fontx_ioctl(cmd, up, perm, &op);
1254 break;
1255
1256 case PIO_FONTRESET:
1257 {
1258 if (!perm)
1259 goto eperm;
1260
1261#ifdef BROKEN_GRAPHICS_PROGRAMS
1262 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1263 font is not saved. */
1264 ret = -ENOSYS;
1265 break;
1266#else
1267 {
1268 op.op = KD_FONT_OP_SET_DEFAULT;
1269 op.data = NULL;
1270 ret = con_font_op(vc_cons[fg_console].d, &op);
1271 if (ret)
1272 break;
1273 con_set_default_unimap(vc_cons[fg_console].d);
1274 break;
1275 }
1276#endif
1277 }
1278
1279 case KDFONTOP: {
1280 if (copy_from_user(&op, up, sizeof(op))) {
1281 ret = -EFAULT;
1282 break;
1283 }
1284 if (!perm && op.op != KD_FONT_OP_GET)
1285 goto eperm;
1286 ret = con_font_op(vc, &op);
1287 if (ret)
1288 break;
1289 if (copy_to_user(up, &op, sizeof(op)))
1290 ret = -EFAULT;
1291 break;
1292 }
1293
1294 case PIO_SCRNMAP:
1295 if (!perm)
1296 ret = -EPERM;
1297 else
1298 ret = con_set_trans_old(up);
1299 break;
1300
1301 case GIO_SCRNMAP:
1302 ret = con_get_trans_old(up);
1303 break;
1304
1305 case PIO_UNISCRNMAP:
1306 if (!perm)
1307 ret = -EPERM;
1308 else
1309 ret = con_set_trans_new(up);
1310 break;
1311
1312 case GIO_UNISCRNMAP:
1313 ret = con_get_trans_new(up);
1314 break;
1315
1316 case PIO_UNIMAPCLR:
1317 { struct unimapinit ui;
1318 if (!perm)
1319 goto eperm;
1320 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1321 if (ret)
1322 ret = -EFAULT;
1323 else
1324 con_clear_unimap(vc, &ui);
1325 break;
1326 }
1327
1328 case PIO_UNIMAP:
1329 case GIO_UNIMAP:
1330 ret = do_unimap_ioctl(cmd, up, perm, vc);
1331 break;
1332
1333 case VT_LOCKSWITCH:
1334 if (!capable(CAP_SYS_TTY_CONFIG))
1335 goto eperm;
1336 vt_dont_switch = 1;
1337 break;
1338 case VT_UNLOCKSWITCH:
1339 if (!capable(CAP_SYS_TTY_CONFIG))
1340 goto eperm;
1341 vt_dont_switch = 0;
1342 break;
1343 case VT_GETHIFONTMASK:
1344 ret = put_user(vc->vc_hi_font_mask,
1345 (unsigned short __user *)arg);
1346 break;
1347 case VT_WAITEVENT:
1348 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1349 break;
1350 default:
1351 ret = -ENOIOCTLCMD;
1352 }
1353out:
1354 tty_unlock();
1355 return ret;
1356eperm:
1357 ret = -EPERM;
1358 goto out;
1359}
1360
1361void reset_vc(struct vc_data *vc)
1362{
1363 vc->vc_mode = KD_TEXT;
1364 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1365 vc->vt_mode.mode = VT_AUTO;
1366 vc->vt_mode.waitv = 0;
1367 vc->vt_mode.relsig = 0;
1368 vc->vt_mode.acqsig = 0;
1369 vc->vt_mode.frsig = 0;
1370 put_pid(vc->vt_pid);
1371 vc->vt_pid = NULL;
1372 vc->vt_newvt = -1;
1373 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1374 reset_palette(vc);
1375}
1376
1377void vc_SAK(struct work_struct *work)
1378{
1379 struct vc *vc_con =
1380 container_of(work, struct vc, SAK_work);
1381 struct vc_data *vc;
1382 struct tty_struct *tty;
1383
1384 console_lock();
1385 vc = vc_con->d;
1386 if (vc) {
1387 tty = vc->port.tty;
1388 /*
1389 * SAK should also work in all raw modes and reset
1390 * them properly.
1391 */
1392 if (tty)
1393 __do_SAK(tty);
1394 reset_vc(vc);
1395 }
1396 console_unlock();
1397}
1398
1399#ifdef CONFIG_COMPAT
1400
1401struct compat_consolefontdesc {
1402 unsigned short charcount; /* characters in font (256 or 512) */
1403 unsigned short charheight; /* scan lines per character (1-32) */
1404 compat_caddr_t chardata; /* font data in expanded form */
1405};
1406
1407static inline int
1408compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1409 int perm, struct console_font_op *op)
1410{
1411 struct compat_consolefontdesc cfdarg;
1412 int i;
1413
1414 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1415 return -EFAULT;
1416
1417 switch (cmd) {
1418 case PIO_FONTX:
1419 if (!perm)
1420 return -EPERM;
1421 op->op = KD_FONT_OP_SET;
1422 op->flags = KD_FONT_FLAG_OLD;
1423 op->width = 8;
1424 op->height = cfdarg.charheight;
1425 op->charcount = cfdarg.charcount;
1426 op->data = compat_ptr(cfdarg.chardata);
1427 return con_font_op(vc_cons[fg_console].d, op);
1428 case GIO_FONTX:
1429 op->op = KD_FONT_OP_GET;
1430 op->flags = KD_FONT_FLAG_OLD;
1431 op->width = 8;
1432 op->height = cfdarg.charheight;
1433 op->charcount = cfdarg.charcount;
1434 op->data = compat_ptr(cfdarg.chardata);
1435 i = con_font_op(vc_cons[fg_console].d, op);
1436 if (i)
1437 return i;
1438 cfdarg.charheight = op->height;
1439 cfdarg.charcount = op->charcount;
1440 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1441 return -EFAULT;
1442 return 0;
1443 }
1444 return -EINVAL;
1445}
1446
1447struct compat_console_font_op {
1448 compat_uint_t op; /* operation code KD_FONT_OP_* */
1449 compat_uint_t flags; /* KD_FONT_FLAG_* */
1450 compat_uint_t width, height; /* font size */
1451 compat_uint_t charcount;
1452 compat_caddr_t data; /* font data with height fixed to 32 */
1453};
1454
1455static inline int
1456compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1457 int perm, struct console_font_op *op, struct vc_data *vc)
1458{
1459 int i;
1460
1461 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1462 return -EFAULT;
1463 if (!perm && op->op != KD_FONT_OP_GET)
1464 return -EPERM;
1465 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1466 op->flags |= KD_FONT_FLAG_OLD;
1467 i = con_font_op(vc, op);
1468 if (i)
1469 return i;
1470 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1471 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1472 return -EFAULT;
1473 return 0;
1474}
1475
1476struct compat_unimapdesc {
1477 unsigned short entry_ct;
1478 compat_caddr_t entries;
1479};
1480
1481static inline int
1482compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1483 int perm, struct vc_data *vc)
1484{
1485 struct compat_unimapdesc tmp;
1486 struct unipair __user *tmp_entries;
1487
1488 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1489 return -EFAULT;
1490 tmp_entries = compat_ptr(tmp.entries);
1491 if (tmp_entries)
1492 if (!access_ok(VERIFY_WRITE, tmp_entries,
1493 tmp.entry_ct*sizeof(struct unipair)))
1494 return -EFAULT;
1495 switch (cmd) {
1496 case PIO_UNIMAP:
1497 if (!perm)
1498 return -EPERM;
1499 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1500 case GIO_UNIMAP:
1501 if (!perm && fg_console != vc->vc_num)
1502 return -EPERM;
1503 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1504 }
1505 return 0;
1506}
1507
1508long vt_compat_ioctl(struct tty_struct *tty,
1509 unsigned int cmd, unsigned long arg)
1510{
1511 struct vc_data *vc = tty->driver_data;
1512 struct console_font_op op; /* used in multiple places here */
1513 unsigned int console;
1514 void __user *up = (void __user *)arg;
1515 int perm;
1516 int ret = 0;
1517
1518 console = vc->vc_num;
1519
1520 tty_lock();
1521
1522 if (!vc_cons_allocated(console)) { /* impossible? */
1523 ret = -ENOIOCTLCMD;
1524 goto out;
1525 }
1526
1527 /*
1528 * To have permissions to do most of the vt ioctls, we either have
1529 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1530 */
1531 perm = 0;
1532 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1533 perm = 1;
1534
1535 switch (cmd) {
1536 /*
1537 * these need special handlers for incompatible data structures
1538 */
1539 case PIO_FONTX:
1540 case GIO_FONTX:
1541 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1542 break;
1543
1544 case KDFONTOP:
1545 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1546 break;
1547
1548 case PIO_UNIMAP:
1549 case GIO_UNIMAP:
1550 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1551 break;
1552
1553 /*
1554 * all these treat 'arg' as an integer
1555 */
1556 case KIOCSOUND:
1557 case KDMKTONE:
1558#ifdef CONFIG_X86
1559 case KDADDIO:
1560 case KDDELIO:
1561#endif
1562 case KDSETMODE:
1563 case KDMAPDISP:
1564 case KDUNMAPDISP:
1565 case KDSKBMODE:
1566 case KDSKBMETA:
1567 case KDSKBLED:
1568 case KDSETLED:
1569 case KDSIGACCEPT:
1570 case VT_ACTIVATE:
1571 case VT_WAITACTIVE:
1572 case VT_RELDISP:
1573 case VT_DISALLOCATE:
1574 case VT_RESIZE:
1575 case VT_RESIZEX:
1576 goto fallback;
1577
1578 /*
1579 * the rest has a compatible data structure behind arg,
1580 * but we have to convert it to a proper 64 bit pointer.
1581 */
1582 default:
1583 arg = (unsigned long)compat_ptr(arg);
1584 goto fallback;
1585 }
1586out:
1587 tty_unlock();
1588 return ret;
1589
1590fallback:
1591 tty_unlock();
1592 return vt_ioctl(tty, cmd, arg);
1593}
1594
1595
1596#endif /* CONFIG_COMPAT */
1597
1598
1599/*
1600 * Performs the back end of a vt switch. Called under the console
1601 * semaphore.
1602 */
1603static void complete_change_console(struct vc_data *vc)
1604{
1605 unsigned char old_vc_mode;
1606 int old = fg_console;
1607
1608 last_console = fg_console;
1609
1610 /*
1611 * If we're switching, we could be going from KD_GRAPHICS to
1612 * KD_TEXT mode or vice versa, which means we need to blank or
1613 * unblank the screen later.
1614 */
1615 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1616 switch_screen(vc);
1617
1618 /*
1619 * This can't appear below a successful kill_pid(). If it did,
1620 * then the *blank_screen operation could occur while X, having
1621 * received acqsig, is waking up on another processor. This
1622 * condition can lead to overlapping accesses to the VGA range
1623 * and the framebuffer (causing system lockups).
1624 *
1625 * To account for this we duplicate this code below only if the
1626 * controlling process is gone and we've called reset_vc.
1627 */
1628 if (old_vc_mode != vc->vc_mode) {
1629 if (vc->vc_mode == KD_TEXT)
1630 do_unblank_screen(1);
1631 else
1632 do_blank_screen(1);
1633 }
1634
1635 /*
1636 * If this new console is under process control, send it a signal
1637 * telling it that it has acquired. Also check if it has died and
1638 * clean up (similar to logic employed in change_console())
1639 */
1640 if (vc->vt_mode.mode == VT_PROCESS) {
1641 /*
1642 * Send the signal as privileged - kill_pid() will
1643 * tell us if the process has gone or something else
1644 * is awry
1645 */
1646 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1647 /*
1648 * The controlling process has died, so we revert back to
1649 * normal operation. In this case, we'll also change back
1650 * to KD_TEXT mode. I'm not sure if this is strictly correct
1651 * but it saves the agony when the X server dies and the screen
1652 * remains blanked due to KD_GRAPHICS! It would be nice to do
1653 * this outside of VT_PROCESS but there is no single process
1654 * to account for and tracking tty count may be undesirable.
1655 */
1656 reset_vc(vc);
1657
1658 if (old_vc_mode != vc->vc_mode) {
1659 if (vc->vc_mode == KD_TEXT)
1660 do_unblank_screen(1);
1661 else
1662 do_blank_screen(1);
1663 }
1664 }
1665 }
1666
1667 /*
1668 * Wake anyone waiting for their VT to activate
1669 */
1670 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1671 return;
1672}
1673
1674/*
1675 * Performs the front-end of a vt switch
1676 */
1677void change_console(struct vc_data *new_vc)
1678{
1679 struct vc_data *vc;
1680
1681 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1682 return;
1683
1684 /*
1685 * If this vt is in process mode, then we need to handshake with
1686 * that process before switching. Essentially, we store where that
1687 * vt wants to switch to and wait for it to tell us when it's done
1688 * (via VT_RELDISP ioctl).
1689 *
1690 * We also check to see if the controlling process still exists.
1691 * If it doesn't, we reset this vt to auto mode and continue.
1692 * This is a cheap way to track process control. The worst thing
1693 * that can happen is: we send a signal to a process, it dies, and
1694 * the switch gets "lost" waiting for a response; hopefully, the
1695 * user will try again, we'll detect the process is gone (unless
1696 * the user waits just the right amount of time :-) and revert the
1697 * vt to auto control.
1698 */
1699 vc = vc_cons[fg_console].d;
1700 if (vc->vt_mode.mode == VT_PROCESS) {
1701 /*
1702 * Send the signal as privileged - kill_pid() will
1703 * tell us if the process has gone or something else
1704 * is awry.
1705 *
1706 * We need to set vt_newvt *before* sending the signal or we
1707 * have a race.
1708 */
1709 vc->vt_newvt = new_vc->vc_num;
1710 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1711 /*
1712 * It worked. Mark the vt to switch to and
1713 * return. The process needs to send us a
1714 * VT_RELDISP ioctl to complete the switch.
1715 */
1716 return;
1717 }
1718
1719 /*
1720 * The controlling process has died, so we revert back to
1721 * normal operation. In this case, we'll also change back
1722 * to KD_TEXT mode. I'm not sure if this is strictly correct
1723 * but it saves the agony when the X server dies and the screen
1724 * remains blanked due to KD_GRAPHICS! It would be nice to do
1725 * this outside of VT_PROCESS but there is no single process
1726 * to account for and tracking tty count may be undesirable.
1727 */
1728 reset_vc(vc);
1729
1730 /*
1731 * Fall through to normal (VT_AUTO) handling of the switch...
1732 */
1733 }
1734
1735 /*
1736 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1737 */
1738 if (vc->vc_mode == KD_GRAPHICS)
1739 return;
1740
1741 complete_change_console(new_vc);
1742}
1743
1744/* Perform a kernel triggered VT switch for suspend/resume */
1745
1746static int disable_vt_switch;
1747
1748int vt_move_to_console(unsigned int vt, int alloc)
1749{
1750 int prev;
1751
1752 console_lock();
1753 /* Graphics mode - up to X */
1754 if (disable_vt_switch) {
1755 console_unlock();
1756 return 0;
1757 }
1758 prev = fg_console;
1759
1760 if (alloc && vc_allocate(vt)) {
1761 /* we can't have a free VC for now. Too bad,
1762 * we don't want to mess the screen for now. */
1763 console_unlock();
1764 return -ENOSPC;
1765 }
1766
1767 if (set_console(vt)) {
1768 /*
1769 * We're unable to switch to the SUSPEND_CONSOLE.
1770 * Let the calling function know so it can decide
1771 * what to do.
1772 */
1773 console_unlock();
1774 return -EIO;
1775 }
1776 console_unlock();
1777 tty_lock();
1778 if (vt_waitactive(vt + 1)) {
1779 pr_debug("Suspend: Can't switch VCs.");
1780 tty_unlock();
1781 return -EINTR;
1782 }
1783 tty_unlock();
1784 return prev;
1785}
1786
1787/*
1788 * Normally during a suspend, we allocate a new console and switch to it.
1789 * When we resume, we switch back to the original console. This switch
1790 * can be slow, so on systems where the framebuffer can handle restoration
1791 * of video registers anyways, there's little point in doing the console
1792 * switch. This function allows you to disable it by passing it '0'.
1793 */
1794void pm_set_vt_switch(int do_switch)
1795{
1796 console_lock();
1797 disable_vt_switch = !do_switch;
1798 console_unlock();
1799}
1800EXPORT_SYMBOL(pm_set_vt_switch);