ip22zilog.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Zilog serial chips found on SGI workstations and
  4. * servers. This driver could actually be made more generic.
  5. *
  6. * This is based on the drivers/serial/sunzilog.c code as of 2.6.0-test7 and the
  7. * old drivers/sgi/char/sgiserial.c code which itself is based of the original
  8. * drivers/sbus/char/zs.c code. A lot of code has been simply moved over
  9. * directly from there but much has been rewritten. Credits therefore go out
  10. * to David S. Miller, Eddie C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell
  11. * for their work there.
  12. *
  13. * Copyright (C) 2002 Ralf Baechle (ralf@linux-mips.org)
  14. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/delay.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/major.h>
  23. #include <linux/string.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/ioport.h>
  26. #include <linux/slab.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/init.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/sgialib.h>
  37. #include <asm/sgi/ioc.h>
  38. #include <asm/sgi/hpc3.h>
  39. #include <asm/sgi/ip22.h>
  40. #include <linux/serial_core.h>
  41. #include "ip22zilog.h"
  42. /*
  43. * On IP22 we need to delay after register accesses but we do not need to
  44. * flush writes.
  45. */
  46. #define ZSDELAY() udelay(5)
  47. #define ZSDELAY_LONG() udelay(20)
  48. #define ZS_WSYNC(channel) do { } while (0)
  49. #define NUM_CHANNELS 2
  50. #define CHANNEL_B 0
  51. #define CHANNEL_A 1
  52. #define ZS_CLOCK 3672000 /* Zilog input clock rate. */
  53. #define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
  54. /*
  55. * We wrap our port structure around the generic uart_port.
  56. */
  57. struct uart_ip22zilog_port {
  58. struct uart_port port;
  59. /* Current values of Zilog write registers. */
  60. unsigned char curregs[NUM_ZSREGS];
  61. unsigned int flags;
  62. #define IP22ZILOG_FLAG_IS_CONS 0x00000004
  63. #define IP22ZILOG_FLAG_IS_KGDB 0x00000008
  64. #define IP22ZILOG_FLAG_MODEM_STATUS 0x00000010
  65. #define IP22ZILOG_FLAG_REGS_HELD 0x00000040
  66. #define IP22ZILOG_FLAG_TX_STOPPED 0x00000080
  67. #define IP22ZILOG_FLAG_TX_ACTIVE 0x00000100
  68. #define IP22ZILOG_FLAG_RESET_DONE 0x00000200
  69. unsigned int tty_break;
  70. unsigned char parity_mask;
  71. unsigned char prev_status;
  72. };
  73. static struct uart_ip22zilog_port ip22zilog_port_table[NUM_CHANNELS];
  74. #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel *)((PORT)->membase))
  75. #define UART_ZILOG(PORT) ((struct uart_ip22zilog_port *)(PORT))
  76. #define IP22ZILOG_GET_CURR_REG(PORT, REGNUM) \
  77. (UART_ZILOG(PORT)->curregs[REGNUM])
  78. #define IP22ZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL) \
  79. ((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
  80. #define ZS_IS_CONS(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_CONS)
  81. #define ZS_IS_KGDB(UP) ((UP)->flags & IP22ZILOG_FLAG_IS_KGDB)
  82. #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & IP22ZILOG_FLAG_MODEM_STATUS)
  83. #define ZS_REGS_HELD(UP) ((UP)->flags & IP22ZILOG_FLAG_REGS_HELD)
  84. #define ZS_TX_STOPPED(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_STOPPED)
  85. #define ZS_TX_ACTIVE(UP) ((UP)->flags & IP22ZILOG_FLAG_TX_ACTIVE)
  86. /* Reading and writing Zilog8530 registers. The delays are to make this
  87. * driver work on the IP22 which needs a settling delay after each chip
  88. * register access, other machines handle this in hardware via auxiliary
  89. * flip-flops which implement the settle time we do in software.
  90. *
  91. * The port lock must be held and local IRQs must be disabled
  92. * when {read,write}_zsreg is invoked.
  93. */
  94. static unsigned char read_zsreg(struct zilog_channel *channel,
  95. unsigned char reg)
  96. {
  97. unsigned char retval;
  98. writeb(reg, &channel->control);
  99. ZSDELAY();
  100. retval = readb(&channel->control);
  101. ZSDELAY();
  102. return retval;
  103. }
  104. static void write_zsreg(struct zilog_channel *channel,
  105. unsigned char reg, unsigned char value)
  106. {
  107. writeb(reg, &channel->control);
  108. ZSDELAY();
  109. writeb(value, &channel->control);
  110. ZSDELAY();
  111. }
  112. static void ip22zilog_clear_fifo(struct zilog_channel *channel)
  113. {
  114. int i;
  115. for (i = 0; i < 32; i++) {
  116. unsigned char regval;
  117. regval = readb(&channel->control);
  118. ZSDELAY();
  119. if (regval & Rx_CH_AV)
  120. break;
  121. regval = read_zsreg(channel, R1);
  122. readb(&channel->data);
  123. ZSDELAY();
  124. if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  125. writeb(ERR_RES, &channel->control);
  126. ZSDELAY();
  127. ZS_WSYNC(channel);
  128. }
  129. }
  130. }
  131. /* This function must only be called when the TX is not busy. The UART
  132. * port lock must be held and local interrupts disabled.
  133. */
  134. static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
  135. {
  136. int i;
  137. /* Let pending transmits finish. */
  138. for (i = 0; i < 1000; i++) {
  139. unsigned char stat = read_zsreg(channel, R1);
  140. if (stat & ALL_SNT)
  141. break;
  142. udelay(100);
  143. }
  144. writeb(ERR_RES, &channel->control);
  145. ZSDELAY();
  146. ZS_WSYNC(channel);
  147. ip22zilog_clear_fifo(channel);
  148. /* Disable all interrupts. */
  149. write_zsreg(channel, R1,
  150. regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
  151. /* Set parity, sync config, stop bits, and clock divisor. */
  152. write_zsreg(channel, R4, regs[R4]);
  153. /* Set misc. TX/RX control bits. */
  154. write_zsreg(channel, R10, regs[R10]);
  155. /* Set TX/RX controls sans the enable bits. */
  156. write_zsreg(channel, R3, regs[R3] & ~RxENAB);
  157. write_zsreg(channel, R5, regs[R5] & ~TxENAB);
  158. /* Synchronous mode config. */
  159. write_zsreg(channel, R6, regs[R6]);
  160. write_zsreg(channel, R7, regs[R7]);
  161. /* Don't mess with the interrupt vector (R2, unused by us) and
  162. * master interrupt control (R9). We make sure this is setup
  163. * properly at probe time then never touch it again.
  164. */
  165. /* Disable baud generator. */
  166. write_zsreg(channel, R14, regs[R14] & ~BRENAB);
  167. /* Clock mode control. */
  168. write_zsreg(channel, R11, regs[R11]);
  169. /* Lower and upper byte of baud rate generator divisor. */
  170. write_zsreg(channel, R12, regs[R12]);
  171. write_zsreg(channel, R13, regs[R13]);
  172. /* Now rewrite R14, with BRENAB (if set). */
  173. write_zsreg(channel, R14, regs[R14]);
  174. /* External status interrupt control. */
  175. write_zsreg(channel, R15, regs[R15]);
  176. /* Reset external status interrupts. */
  177. write_zsreg(channel, R0, RES_EXT_INT);
  178. write_zsreg(channel, R0, RES_EXT_INT);
  179. /* Rewrite R3/R5, this time without enables masked. */
  180. write_zsreg(channel, R3, regs[R3]);
  181. write_zsreg(channel, R5, regs[R5]);
  182. /* Rewrite R1, this time without IRQ enabled masked. */
  183. write_zsreg(channel, R1, regs[R1]);
  184. }
  185. /* Reprogram the Zilog channel HW registers with the copies found in the
  186. * software state struct. If the transmitter is busy, we defer this update
  187. * until the next TX complete interrupt. Else, we do it right now.
  188. *
  189. * The UART port lock must be held and local interrupts disabled.
  190. */
  191. static void ip22zilog_maybe_update_regs(struct uart_ip22zilog_port *up,
  192. struct zilog_channel *channel)
  193. {
  194. if (!ZS_REGS_HELD(up)) {
  195. if (ZS_TX_ACTIVE(up)) {
  196. up->flags |= IP22ZILOG_FLAG_REGS_HELD;
  197. } else {
  198. __load_zsregs(channel, up->curregs);
  199. }
  200. }
  201. }
  202. #define Rx_BRK 0x0100 /* BREAK event software flag. */
  203. #define Rx_SYS 0x0200 /* SysRq event software flag. */
  204. static bool ip22zilog_receive_chars(struct uart_ip22zilog_port *up,
  205. struct zilog_channel *channel)
  206. {
  207. unsigned int r1;
  208. u8 ch, flag;
  209. bool push = up->port.state != NULL;
  210. for (;;) {
  211. ch = readb(&channel->control);
  212. ZSDELAY();
  213. if (!(ch & Rx_CH_AV))
  214. break;
  215. r1 = read_zsreg(channel, R1);
  216. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  217. writeb(ERR_RES, &channel->control);
  218. ZSDELAY();
  219. ZS_WSYNC(channel);
  220. }
  221. ch = readb(&channel->data);
  222. ZSDELAY();
  223. ch &= up->parity_mask;
  224. /* Handle the null char got when BREAK is removed. */
  225. if (!ch)
  226. r1 |= up->tty_break;
  227. /* A real serial line, record the character and status. */
  228. flag = TTY_NORMAL;
  229. up->port.icount.rx++;
  230. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | Rx_SYS | Rx_BRK)) {
  231. up->tty_break = 0;
  232. if (r1 & (Rx_SYS | Rx_BRK)) {
  233. up->port.icount.brk++;
  234. if (r1 & Rx_SYS)
  235. continue;
  236. r1 &= ~(PAR_ERR | CRC_ERR);
  237. }
  238. else if (r1 & PAR_ERR)
  239. up->port.icount.parity++;
  240. else if (r1 & CRC_ERR)
  241. up->port.icount.frame++;
  242. if (r1 & Rx_OVR)
  243. up->port.icount.overrun++;
  244. r1 &= up->port.read_status_mask;
  245. if (r1 & Rx_BRK)
  246. flag = TTY_BREAK;
  247. else if (r1 & PAR_ERR)
  248. flag = TTY_PARITY;
  249. else if (r1 & CRC_ERR)
  250. flag = TTY_FRAME;
  251. }
  252. if (uart_handle_sysrq_char(&up->port, ch))
  253. continue;
  254. if (push)
  255. uart_insert_char(&up->port, r1, Rx_OVR, ch, flag);
  256. }
  257. return push;
  258. }
  259. static void ip22zilog_status_handle(struct uart_ip22zilog_port *up,
  260. struct zilog_channel *channel)
  261. {
  262. unsigned char status;
  263. status = readb(&channel->control);
  264. ZSDELAY();
  265. writeb(RES_EXT_INT, &channel->control);
  266. ZSDELAY();
  267. ZS_WSYNC(channel);
  268. if (up->curregs[R15] & BRKIE) {
  269. if ((status & BRK_ABRT) && !(up->prev_status & BRK_ABRT)) {
  270. if (uart_handle_break(&up->port))
  271. up->tty_break = Rx_SYS;
  272. else
  273. up->tty_break = Rx_BRK;
  274. }
  275. }
  276. if (ZS_WANTS_MODEM_STATUS(up)) {
  277. if (status & SYNC)
  278. up->port.icount.dsr++;
  279. /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
  280. * But it does not tell us which bit has changed, we have to keep
  281. * track of this ourselves.
  282. */
  283. if ((status ^ up->prev_status) ^ DCD)
  284. uart_handle_dcd_change(&up->port,
  285. (status & DCD));
  286. if ((status ^ up->prev_status) ^ CTS)
  287. uart_handle_cts_change(&up->port,
  288. (status & CTS));
  289. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  290. }
  291. up->prev_status = status;
  292. }
  293. static void ip22zilog_transmit_chars(struct uart_ip22zilog_port *up,
  294. struct zilog_channel *channel)
  295. {
  296. struct tty_port *tport;
  297. unsigned char c;
  298. if (ZS_IS_CONS(up)) {
  299. unsigned char status = readb(&channel->control);
  300. ZSDELAY();
  301. /* TX still busy? Just wait for the next TX done interrupt.
  302. *
  303. * It can occur because of how we do serial console writes. It would
  304. * be nice to transmit console writes just like we normally would for
  305. * a TTY line. (ie. buffered and TX interrupt driven). That is not
  306. * easy because console writes cannot sleep. One solution might be
  307. * to poll on enough port->xmit space becoming free. -DaveM
  308. */
  309. if (!(status & Tx_BUF_EMP))
  310. return;
  311. }
  312. up->flags &= ~IP22ZILOG_FLAG_TX_ACTIVE;
  313. if (ZS_REGS_HELD(up)) {
  314. __load_zsregs(channel, up->curregs);
  315. up->flags &= ~IP22ZILOG_FLAG_REGS_HELD;
  316. }
  317. if (ZS_TX_STOPPED(up)) {
  318. up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
  319. goto ack_tx_int;
  320. }
  321. if (up->port.x_char) {
  322. up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
  323. writeb(up->port.x_char, &channel->data);
  324. ZSDELAY();
  325. ZS_WSYNC(channel);
  326. up->port.icount.tx++;
  327. up->port.x_char = 0;
  328. return;
  329. }
  330. if (up->port.state == NULL)
  331. goto ack_tx_int;
  332. tport = &up->port.state->port;
  333. if (uart_tx_stopped(&up->port))
  334. goto ack_tx_int;
  335. if (!uart_fifo_get(&up->port, &c))
  336. goto ack_tx_int;
  337. up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
  338. writeb(c, &channel->data);
  339. ZSDELAY();
  340. ZS_WSYNC(channel);
  341. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  342. uart_write_wakeup(&up->port);
  343. return;
  344. ack_tx_int:
  345. writeb(RES_Tx_P, &channel->control);
  346. ZSDELAY();
  347. ZS_WSYNC(channel);
  348. }
  349. static irqreturn_t ip22zilog_interrupt(int irq, void *dev_id)
  350. {
  351. struct uart_ip22zilog_port *up;
  352. struct zilog_channel *channel;
  353. unsigned char r3;
  354. bool push = false;
  355. up = &ip22zilog_port_table[CHANNEL_A];
  356. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  357. uart_port_lock(&up->port);
  358. r3 = read_zsreg(channel, R3);
  359. /* Channel A */
  360. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  361. writeb(RES_H_IUS, &channel->control);
  362. ZSDELAY();
  363. ZS_WSYNC(channel);
  364. if (r3 & CHARxIP)
  365. push = ip22zilog_receive_chars(up, channel);
  366. if (r3 & CHAEXT)
  367. ip22zilog_status_handle(up, channel);
  368. if (r3 & CHATxIP)
  369. ip22zilog_transmit_chars(up, channel);
  370. }
  371. uart_port_unlock(&up->port);
  372. if (push)
  373. tty_flip_buffer_push(&up->port.state->port);
  374. /* Channel B */
  375. up = &ip22zilog_port_table[CHANNEL_B];
  376. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  377. push = false;
  378. uart_port_lock(&up->port);
  379. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  380. writeb(RES_H_IUS, &channel->control);
  381. ZSDELAY();
  382. ZS_WSYNC(channel);
  383. if (r3 & CHBRxIP)
  384. push = ip22zilog_receive_chars(up, channel);
  385. if (r3 & CHBEXT)
  386. ip22zilog_status_handle(up, channel);
  387. if (r3 & CHBTxIP)
  388. ip22zilog_transmit_chars(up, channel);
  389. }
  390. uart_port_unlock(&up->port);
  391. if (push)
  392. tty_flip_buffer_push(&up->port.state->port);
  393. return IRQ_HANDLED;
  394. }
  395. /* A convenient way to quickly get R0 status. The caller must _not_ hold the
  396. * port lock, it is acquired here.
  397. */
  398. static __inline__ unsigned char ip22zilog_read_channel_status(struct uart_port *port)
  399. {
  400. struct zilog_channel *channel;
  401. unsigned char status;
  402. channel = ZILOG_CHANNEL_FROM_PORT(port);
  403. status = readb(&channel->control);
  404. ZSDELAY();
  405. return status;
  406. }
  407. /* The port lock is not held. */
  408. static unsigned int ip22zilog_tx_empty(struct uart_port *port)
  409. {
  410. unsigned long flags;
  411. unsigned char status;
  412. unsigned int ret;
  413. uart_port_lock_irqsave(port, &flags);
  414. status = ip22zilog_read_channel_status(port);
  415. uart_port_unlock_irqrestore(port, flags);
  416. if (status & Tx_BUF_EMP)
  417. ret = TIOCSER_TEMT;
  418. else
  419. ret = 0;
  420. return ret;
  421. }
  422. /* The port lock is held and interrupts are disabled. */
  423. static unsigned int ip22zilog_get_mctrl(struct uart_port *port)
  424. {
  425. unsigned char status;
  426. unsigned int ret;
  427. status = ip22zilog_read_channel_status(port);
  428. ret = 0;
  429. if (status & DCD)
  430. ret |= TIOCM_CAR;
  431. if (status & SYNC)
  432. ret |= TIOCM_DSR;
  433. if (status & CTS)
  434. ret |= TIOCM_CTS;
  435. return ret;
  436. }
  437. /* The port lock is held and interrupts are disabled. */
  438. static void ip22zilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
  439. {
  440. struct uart_ip22zilog_port *up =
  441. container_of(port, struct uart_ip22zilog_port, port);
  442. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  443. unsigned char set_bits, clear_bits;
  444. set_bits = clear_bits = 0;
  445. if (mctrl & TIOCM_RTS)
  446. set_bits |= RTS;
  447. else
  448. clear_bits |= RTS;
  449. if (mctrl & TIOCM_DTR)
  450. set_bits |= DTR;
  451. else
  452. clear_bits |= DTR;
  453. /* NOTE: Not subject to 'transmitter active' rule. */
  454. up->curregs[R5] |= set_bits;
  455. up->curregs[R5] &= ~clear_bits;
  456. write_zsreg(channel, R5, up->curregs[R5]);
  457. }
  458. /* The port lock is held and interrupts are disabled. */
  459. static void ip22zilog_stop_tx(struct uart_port *port)
  460. {
  461. struct uart_ip22zilog_port *up =
  462. container_of(port, struct uart_ip22zilog_port, port);
  463. up->flags |= IP22ZILOG_FLAG_TX_STOPPED;
  464. }
  465. /* The port lock is held and interrupts are disabled. */
  466. static void ip22zilog_start_tx(struct uart_port *port)
  467. {
  468. struct uart_ip22zilog_port *up =
  469. container_of(port, struct uart_ip22zilog_port, port);
  470. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  471. unsigned char status;
  472. up->flags |= IP22ZILOG_FLAG_TX_ACTIVE;
  473. up->flags &= ~IP22ZILOG_FLAG_TX_STOPPED;
  474. status = readb(&channel->control);
  475. ZSDELAY();
  476. /* TX busy? Just wait for the TX done interrupt. */
  477. if (!(status & Tx_BUF_EMP))
  478. return;
  479. /* Send the first character to jump-start the TX done
  480. * IRQ sending engine.
  481. */
  482. if (port->x_char) {
  483. writeb(port->x_char, &channel->data);
  484. ZSDELAY();
  485. ZS_WSYNC(channel);
  486. port->icount.tx++;
  487. port->x_char = 0;
  488. } else {
  489. struct tty_port *tport = &port->state->port;
  490. unsigned char c;
  491. if (!uart_fifo_get(port, &c))
  492. return;
  493. writeb(c, &channel->data);
  494. ZSDELAY();
  495. ZS_WSYNC(channel);
  496. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  497. uart_write_wakeup(&up->port);
  498. }
  499. }
  500. /* The port lock is held and interrupts are disabled. */
  501. static void ip22zilog_stop_rx(struct uart_port *port)
  502. {
  503. struct uart_ip22zilog_port *up = UART_ZILOG(port);
  504. struct zilog_channel *channel;
  505. if (ZS_IS_CONS(up))
  506. return;
  507. channel = ZILOG_CHANNEL_FROM_PORT(port);
  508. /* Disable all RX interrupts. */
  509. up->curregs[R1] &= ~RxINT_MASK;
  510. ip22zilog_maybe_update_regs(up, channel);
  511. }
  512. /* The port lock is held. */
  513. static void ip22zilog_enable_ms(struct uart_port *port)
  514. {
  515. struct uart_ip22zilog_port *up =
  516. container_of(port, struct uart_ip22zilog_port, port);
  517. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  518. unsigned char new_reg;
  519. new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  520. if (new_reg != up->curregs[R15]) {
  521. up->curregs[R15] = new_reg;
  522. /* NOTE: Not subject to 'transmitter active' rule. */
  523. write_zsreg(channel, R15, up->curregs[R15]);
  524. }
  525. }
  526. /* The port lock is not held. */
  527. static void ip22zilog_break_ctl(struct uart_port *port, int break_state)
  528. {
  529. struct uart_ip22zilog_port *up =
  530. container_of(port, struct uart_ip22zilog_port, port);
  531. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  532. unsigned char set_bits, clear_bits, new_reg;
  533. unsigned long flags;
  534. set_bits = clear_bits = 0;
  535. if (break_state)
  536. set_bits |= SND_BRK;
  537. else
  538. clear_bits |= SND_BRK;
  539. uart_port_lock_irqsave(port, &flags);
  540. new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
  541. if (new_reg != up->curregs[R5]) {
  542. up->curregs[R5] = new_reg;
  543. /* NOTE: Not subject to 'transmitter active' rule. */
  544. write_zsreg(channel, R5, up->curregs[R5]);
  545. }
  546. uart_port_unlock_irqrestore(port, flags);
  547. }
  548. static void __ip22zilog_reset(struct uart_ip22zilog_port *up)
  549. {
  550. struct zilog_channel *channel;
  551. int i;
  552. if (up->flags & IP22ZILOG_FLAG_RESET_DONE)
  553. return;
  554. /* Let pending transmits finish. */
  555. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  556. for (i = 0; i < 1000; i++) {
  557. unsigned char stat = read_zsreg(channel, R1);
  558. if (stat & ALL_SNT)
  559. break;
  560. udelay(100);
  561. }
  562. up = &ip22zilog_port_table[CHANNEL_A];
  563. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  564. write_zsreg(channel, R9, FHWRES);
  565. ZSDELAY_LONG();
  566. (void) read_zsreg(channel, R0);
  567. up->flags |= IP22ZILOG_FLAG_RESET_DONE;
  568. up = &ip22zilog_port_table[CHANNEL_B];
  569. up->flags |= IP22ZILOG_FLAG_RESET_DONE;
  570. }
  571. static void __ip22zilog_startup(struct uart_ip22zilog_port *up)
  572. {
  573. struct zilog_channel *channel;
  574. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  575. __ip22zilog_reset(up);
  576. __load_zsregs(channel, up->curregs);
  577. /* set master interrupt enable */
  578. write_zsreg(channel, R9, up->curregs[R9]);
  579. up->prev_status = readb(&channel->control);
  580. /* Enable receiver and transmitter. */
  581. up->curregs[R3] |= RxENAB;
  582. up->curregs[R5] |= TxENAB;
  583. up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  584. ip22zilog_maybe_update_regs(up, channel);
  585. }
  586. static int ip22zilog_startup(struct uart_port *port)
  587. {
  588. struct uart_ip22zilog_port *up = UART_ZILOG(port);
  589. unsigned long flags;
  590. if (ZS_IS_CONS(up))
  591. return 0;
  592. uart_port_lock_irqsave(port, &flags);
  593. __ip22zilog_startup(up);
  594. uart_port_unlock_irqrestore(port, flags);
  595. return 0;
  596. }
  597. /*
  598. * The test for ZS_IS_CONS is explained by the following e-mail:
  599. *****
  600. * From: Russell King <rmk@arm.linux.org.uk>
  601. * Date: Sun, 8 Dec 2002 10:18:38 +0000
  602. *
  603. * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
  604. * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
  605. * > and I noticed that something is not right with reference
  606. * > counting in this case. It seems that when the console
  607. * > is open by kernel initially, this is not accounted
  608. * > as an open, and uart_startup is not called.
  609. *
  610. * That is correct. We are unable to call uart_startup when the serial
  611. * console is initialised because it may need to allocate memory (as
  612. * request_irq does) and the memory allocators may not have been
  613. * initialised.
  614. *
  615. * 1. initialise the port into a state where it can send characters in the
  616. * console write method.
  617. *
  618. * 2. don't do the actual hardware shutdown in your shutdown() method (but
  619. * do the normal software shutdown - ie, free irqs etc)
  620. *****
  621. */
  622. static void ip22zilog_shutdown(struct uart_port *port)
  623. {
  624. struct uart_ip22zilog_port *up = UART_ZILOG(port);
  625. struct zilog_channel *channel;
  626. unsigned long flags;
  627. if (ZS_IS_CONS(up))
  628. return;
  629. uart_port_lock_irqsave(port, &flags);
  630. channel = ZILOG_CHANNEL_FROM_PORT(port);
  631. /* Disable receiver and transmitter. */
  632. up->curregs[R3] &= ~RxENAB;
  633. up->curregs[R5] &= ~TxENAB;
  634. /* Disable all interrupts and BRK assertion. */
  635. up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  636. up->curregs[R5] &= ~SND_BRK;
  637. ip22zilog_maybe_update_regs(up, channel);
  638. uart_port_unlock_irqrestore(port, flags);
  639. }
  640. /* Shared by TTY driver and serial console setup. The port lock is held
  641. * and local interrupts are disabled.
  642. */
  643. static void
  644. ip22zilog_convert_to_zs(struct uart_ip22zilog_port *up, unsigned int cflag,
  645. unsigned int iflag, int brg)
  646. {
  647. up->curregs[R10] = NRZ;
  648. up->curregs[R11] = TCBR | RCBR;
  649. /* Program BAUD and clock source. */
  650. up->curregs[R4] &= ~XCLK_MASK;
  651. up->curregs[R4] |= X16CLK;
  652. up->curregs[R12] = brg & 0xff;
  653. up->curregs[R13] = (brg >> 8) & 0xff;
  654. up->curregs[R14] = BRENAB;
  655. /* Character size, stop bits, and parity. */
  656. up->curregs[3] &= ~RxN_MASK;
  657. up->curregs[5] &= ~TxN_MASK;
  658. switch (cflag & CSIZE) {
  659. case CS5:
  660. up->curregs[3] |= Rx5;
  661. up->curregs[5] |= Tx5;
  662. up->parity_mask = 0x1f;
  663. break;
  664. case CS6:
  665. up->curregs[3] |= Rx6;
  666. up->curregs[5] |= Tx6;
  667. up->parity_mask = 0x3f;
  668. break;
  669. case CS7:
  670. up->curregs[3] |= Rx7;
  671. up->curregs[5] |= Tx7;
  672. up->parity_mask = 0x7f;
  673. break;
  674. case CS8:
  675. default:
  676. up->curregs[3] |= Rx8;
  677. up->curregs[5] |= Tx8;
  678. up->parity_mask = 0xff;
  679. break;
  680. }
  681. up->curregs[4] &= ~0x0c;
  682. if (cflag & CSTOPB)
  683. up->curregs[4] |= SB2;
  684. else
  685. up->curregs[4] |= SB1;
  686. if (cflag & PARENB)
  687. up->curregs[4] |= PAR_ENAB;
  688. else
  689. up->curregs[4] &= ~PAR_ENAB;
  690. if (!(cflag & PARODD))
  691. up->curregs[4] |= PAR_EVEN;
  692. else
  693. up->curregs[4] &= ~PAR_EVEN;
  694. up->port.read_status_mask = Rx_OVR;
  695. if (iflag & INPCK)
  696. up->port.read_status_mask |= CRC_ERR | PAR_ERR;
  697. if (iflag & (IGNBRK | BRKINT | PARMRK))
  698. up->port.read_status_mask |= BRK_ABRT;
  699. up->port.ignore_status_mask = 0;
  700. if (iflag & IGNPAR)
  701. up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  702. if (iflag & IGNBRK) {
  703. up->port.ignore_status_mask |= BRK_ABRT;
  704. if (iflag & IGNPAR)
  705. up->port.ignore_status_mask |= Rx_OVR;
  706. }
  707. if ((cflag & CREAD) == 0)
  708. up->port.ignore_status_mask = 0xff;
  709. }
  710. /* The port lock is not held. */
  711. static void
  712. ip22zilog_set_termios(struct uart_port *port, struct ktermios *termios,
  713. const struct ktermios *old)
  714. {
  715. struct uart_ip22zilog_port *up =
  716. container_of(port, struct uart_ip22zilog_port, port);
  717. unsigned long flags;
  718. int baud, brg;
  719. baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
  720. uart_port_lock_irqsave(&up->port, &flags);
  721. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  722. ip22zilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
  723. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  724. up->flags |= IP22ZILOG_FLAG_MODEM_STATUS;
  725. else
  726. up->flags &= ~IP22ZILOG_FLAG_MODEM_STATUS;
  727. ip22zilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
  728. uart_update_timeout(port, termios->c_cflag, baud);
  729. uart_port_unlock_irqrestore(&up->port, flags);
  730. }
  731. static const char *ip22zilog_type(struct uart_port *port)
  732. {
  733. return "IP22-Zilog";
  734. }
  735. /* We do not request/release mappings of the registers here, this
  736. * happens at early serial probe time.
  737. */
  738. static void ip22zilog_release_port(struct uart_port *port)
  739. {
  740. }
  741. static int ip22zilog_request_port(struct uart_port *port)
  742. {
  743. return 0;
  744. }
  745. /* These do not need to do anything interesting either. */
  746. static void ip22zilog_config_port(struct uart_port *port, int flags)
  747. {
  748. }
  749. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  750. static int ip22zilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  751. {
  752. return -EINVAL;
  753. }
  754. static const struct uart_ops ip22zilog_pops = {
  755. .tx_empty = ip22zilog_tx_empty,
  756. .set_mctrl = ip22zilog_set_mctrl,
  757. .get_mctrl = ip22zilog_get_mctrl,
  758. .stop_tx = ip22zilog_stop_tx,
  759. .start_tx = ip22zilog_start_tx,
  760. .stop_rx = ip22zilog_stop_rx,
  761. .enable_ms = ip22zilog_enable_ms,
  762. .break_ctl = ip22zilog_break_ctl,
  763. .startup = ip22zilog_startup,
  764. .shutdown = ip22zilog_shutdown,
  765. .set_termios = ip22zilog_set_termios,
  766. .type = ip22zilog_type,
  767. .release_port = ip22zilog_release_port,
  768. .request_port = ip22zilog_request_port,
  769. .config_port = ip22zilog_config_port,
  770. .verify_port = ip22zilog_verify_port,
  771. };
  772. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  773. #ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
  774. static void ip22zilog_put_char(struct uart_port *port, unsigned char ch)
  775. {
  776. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  777. int loops = ZS_PUT_CHAR_MAX_DELAY;
  778. /* This is a timed polling loop so do not switch the explicit
  779. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  780. */
  781. do {
  782. unsigned char val = readb(&channel->control);
  783. if (val & Tx_BUF_EMP) {
  784. ZSDELAY();
  785. break;
  786. }
  787. udelay(5);
  788. } while (--loops);
  789. writeb(ch, &channel->data);
  790. ZSDELAY();
  791. ZS_WSYNC(channel);
  792. }
  793. static void
  794. ip22zilog_console_write(struct console *con, const char *s, unsigned int count)
  795. {
  796. struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
  797. unsigned long flags;
  798. uart_port_lock_irqsave(&up->port, &flags);
  799. uart_console_write(&up->port, s, count, ip22zilog_put_char);
  800. udelay(2);
  801. uart_port_unlock_irqrestore(&up->port, flags);
  802. }
  803. static int __init ip22zilog_console_setup(struct console *con, char *options)
  804. {
  805. struct uart_ip22zilog_port *up = &ip22zilog_port_table[con->index];
  806. unsigned long flags;
  807. int baud = 9600, bits = 8;
  808. int parity = 'n';
  809. int flow = 'n';
  810. up->flags |= IP22ZILOG_FLAG_IS_CONS;
  811. printk(KERN_INFO "Console: ttyS%d (IP22-Zilog)\n", con->index);
  812. uart_port_lock_irqsave(&up->port, &flags);
  813. up->curregs[R15] |= BRKIE;
  814. __ip22zilog_startup(up);
  815. uart_port_unlock_irqrestore(&up->port, flags);
  816. if (options)
  817. uart_parse_options(options, &baud, &parity, &bits, &flow);
  818. return uart_set_options(&up->port, con, baud, parity, bits, flow);
  819. }
  820. static struct uart_driver ip22zilog_reg;
  821. static struct console ip22zilog_console = {
  822. .name = "ttyS",
  823. .write = ip22zilog_console_write,
  824. .device = uart_console_device,
  825. .setup = ip22zilog_console_setup,
  826. .flags = CON_PRINTBUFFER,
  827. .index = -1,
  828. .data = &ip22zilog_reg,
  829. };
  830. #endif /* CONFIG_SERIAL_IP22_ZILOG_CONSOLE */
  831. static struct uart_driver ip22zilog_reg = {
  832. .owner = THIS_MODULE,
  833. .driver_name = "serial",
  834. .dev_name = "ttyS",
  835. .major = TTY_MAJOR,
  836. .minor = 64,
  837. .nr = NUM_CHANNELS,
  838. #ifdef CONFIG_SERIAL_IP22_ZILOG_CONSOLE
  839. .cons = &ip22zilog_console,
  840. #endif
  841. };
  842. static void __init ip22zilog_prepare(struct uart_ip22zilog_port *up)
  843. {
  844. unsigned char sysrq_on = IS_ENABLED(CONFIG_SERIAL_IP22_ZILOG_CONSOLE);
  845. int brg;
  846. spin_lock_init(&up->port.lock);
  847. up->port.iotype = UPIO_MEM;
  848. up->port.uartclk = ZS_CLOCK;
  849. up->port.fifosize = 1;
  850. up->port.has_sysrq = sysrq_on;
  851. up->port.ops = &ip22zilog_pops;
  852. up->port.type = PORT_IP22ZILOG;
  853. /* Normal serial TTY. */
  854. up->parity_mask = 0xff;
  855. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  856. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  857. up->curregs[R3] = RxENAB | Rx8;
  858. up->curregs[R5] = TxENAB | Tx8;
  859. up->curregs[R9] = NV | MIE;
  860. up->curregs[R10] = NRZ;
  861. up->curregs[R11] = TCBR | RCBR;
  862. brg = BPS_TO_BRG(9600, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  863. up->curregs[R12] = (brg & 0xff);
  864. up->curregs[R13] = (brg >> 8) & 0xff;
  865. up->curregs[R14] = BRENAB;
  866. }
  867. static int ip22zilog_probe(struct platform_device *pdev)
  868. {
  869. struct uart_ip22zilog_port *up;
  870. char __iomem *membase;
  871. struct resource *res;
  872. int irq;
  873. int i;
  874. up = &ip22zilog_port_table[CHANNEL_B];
  875. if (up->port.dev)
  876. return -ENOSPC;
  877. irq = platform_get_irq(pdev, 0);
  878. if (irq < 0)
  879. return irq;
  880. membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  881. if (IS_ERR(membase))
  882. return PTR_ERR(membase);
  883. ip22zilog_prepare(up);
  884. up->port.mapbase = res->start + offsetof(struct zilog_layout, channelB);
  885. up->port.membase = membase + offsetof(struct zilog_layout, channelB);
  886. up->port.line = 0;
  887. up->port.dev = &pdev->dev;
  888. up->port.irq = irq;
  889. up = &ip22zilog_port_table[CHANNEL_A];
  890. ip22zilog_prepare(up);
  891. up->port.mapbase = res->start + offsetof(struct zilog_layout, channelA);
  892. up->port.membase = membase + offsetof(struct zilog_layout, channelA);
  893. up->port.line = 1;
  894. up->port.dev = &pdev->dev;
  895. up->port.irq = irq;
  896. if (request_irq(irq, ip22zilog_interrupt, 0,
  897. "IP22-Zilog", NULL)) {
  898. panic("IP22-Zilog: Unable to register zs interrupt handler.\n");
  899. }
  900. for (i = 0; i < NUM_CHANNELS; i++)
  901. uart_add_one_port(&ip22zilog_reg,
  902. &ip22zilog_port_table[i].port);
  903. return 0;
  904. }
  905. static void ip22zilog_remove(struct platform_device *pdev)
  906. {
  907. int i;
  908. for (i = 0; i < NUM_CHANNELS; i++) {
  909. uart_remove_one_port(&ip22zilog_reg,
  910. &ip22zilog_port_table[i].port);
  911. ip22zilog_port_table[i].port.dev = NULL;
  912. }
  913. }
  914. static struct platform_driver ip22zilog_driver = {
  915. .probe = ip22zilog_probe,
  916. .remove = ip22zilog_remove,
  917. .driver = {
  918. .name = "ip22zilog"
  919. }
  920. };
  921. static int __init ip22zilog_init(void)
  922. {
  923. int ret;
  924. ret = uart_register_driver(&ip22zilog_reg);
  925. if (ret)
  926. return ret;
  927. ret = platform_driver_register(&ip22zilog_driver);
  928. if (ret)
  929. uart_unregister_driver(&ip22zilog_reg);
  930. return ret;
  931. }
  932. static void __exit ip22zilog_exit(void)
  933. {
  934. uart_unregister_driver(&ip22zilog_reg);
  935. platform_driver_unregister(&ip22zilog_driver);
  936. }
  937. module_init(ip22zilog_init);
  938. module_exit(ip22zilog_exit);
  939. /* David wrote it but I'm to blame for the bugs ... */
  940. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  941. MODULE_DESCRIPTION("SGI Zilog serial port driver");
  942. MODULE_LICENSE("GPL");