sunsu.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /* $Id: su.c,v 1.55 2002/01/08 16:00:16 davem Exp $
  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. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/errno.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/major.h>
  26. #include <linux/string.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/ioport.h>
  29. #include <linux/circ_buf.h>
  30. #include <linux/serial.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/console.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 <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/prom.h>
  42. #include <asm/of_device.h>
  43. #if defined(CONFIG_SERIAL_SUNSU_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  44. #define SUPPORT_SYSRQ
  45. #endif
  46. #include <linux/serial_core.h>
  47. #include "suncore.h"
  48. /* We are on a NS PC87303 clocked with 24.0 MHz, which results
  49. * in a UART clock of 1.8462 MHz.
  50. */
  51. #define SU_BASE_BAUD (1846200 / 16)
  52. enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
  53. static char *su_typev[] = { "su(???)", "su(mouse)", "su(kbd)", "su(serial)" };
  54. /*
  55. * Here we define the default xmit fifo size used for each type of UART.
  56. */
  57. static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
  58. { "unknown", 1, 0 },
  59. { "8250", 1, 0 },
  60. { "16450", 1, 0 },
  61. { "16550", 1, 0 },
  62. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  63. { "Cirrus", 1, 0 },
  64. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  65. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
  66. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO },
  67. { "Startech", 1, 0 },
  68. { "16C950/954", 128, UART_CLEAR_FIFO | UART_USE_FIFO },
  69. { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
  70. { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
  71. { "RSA", 2048, UART_CLEAR_FIFO | UART_USE_FIFO }
  72. };
  73. struct uart_sunsu_port {
  74. struct uart_port port;
  75. unsigned char acr;
  76. unsigned char ier;
  77. unsigned short rev;
  78. unsigned char lcr;
  79. unsigned int lsr_break_flag;
  80. unsigned int cflag;
  81. /* Probing information. */
  82. enum su_type su_type;
  83. unsigned int type_probed; /* XXX Stupid */
  84. unsigned long reg_size;
  85. #ifdef CONFIG_SERIO
  86. struct serio serio;
  87. int serio_open;
  88. #endif
  89. };
  90. static unsigned int serial_in(struct uart_sunsu_port *up, int offset)
  91. {
  92. offset <<= up->port.regshift;
  93. switch (up->port.iotype) {
  94. case UPIO_HUB6:
  95. outb(up->port.hub6 - 1 + offset, up->port.iobase);
  96. return inb(up->port.iobase + 1);
  97. case UPIO_MEM:
  98. return readb(up->port.membase + offset);
  99. default:
  100. return inb(up->port.iobase + offset);
  101. }
  102. }
  103. static void serial_out(struct uart_sunsu_port *up, int offset, int value)
  104. {
  105. #ifndef CONFIG_SPARC64
  106. /*
  107. * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
  108. * connected with a gate then go to SlavIO. When IRQ4 goes tristated
  109. * gate outputs a logical one. Since we use level triggered interrupts
  110. * we have lockup and watchdog reset. We cannot mask IRQ because
  111. * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
  112. * This problem is similar to what Alpha people suffer, see serial.c.
  113. */
  114. if (offset == UART_MCR)
  115. value |= UART_MCR_OUT2;
  116. #endif
  117. offset <<= up->port.regshift;
  118. switch (up->port.iotype) {
  119. case UPIO_HUB6:
  120. outb(up->port.hub6 - 1 + offset, up->port.iobase);
  121. outb(value, up->port.iobase + 1);
  122. break;
  123. case UPIO_MEM:
  124. writeb(value, up->port.membase + offset);
  125. break;
  126. default:
  127. outb(value, up->port.iobase + offset);
  128. }
  129. }
  130. /*
  131. * We used to support using pause I/O for certain machines. We
  132. * haven't supported this for a while, but just in case it's badly
  133. * needed for certain old 386 machines, I've left these #define's
  134. * in....
  135. */
  136. #define serial_inp(up, offset) serial_in(up, offset)
  137. #define serial_outp(up, offset, value) serial_out(up, offset, value)
  138. /*
  139. * For the 16C950
  140. */
  141. static void serial_icr_write(struct uart_sunsu_port *up, int offset, int value)
  142. {
  143. serial_out(up, UART_SCR, offset);
  144. serial_out(up, UART_ICR, value);
  145. }
  146. #if 0 /* Unused currently */
  147. static unsigned int serial_icr_read(struct uart_sunsu_port *up, int offset)
  148. {
  149. unsigned int value;
  150. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  151. serial_out(up, UART_SCR, offset);
  152. value = serial_in(up, UART_ICR);
  153. serial_icr_write(up, UART_ACR, up->acr);
  154. return value;
  155. }
  156. #endif
  157. #ifdef CONFIG_SERIAL_8250_RSA
  158. /*
  159. * Attempts to turn on the RSA FIFO. Returns zero on failure.
  160. * We set the port uart clock rate if we succeed.
  161. */
  162. static int __enable_rsa(struct uart_sunsu_port *up)
  163. {
  164. unsigned char mode;
  165. int result;
  166. mode = serial_inp(up, UART_RSA_MSR);
  167. result = mode & UART_RSA_MSR_FIFO;
  168. if (!result) {
  169. serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
  170. mode = serial_inp(up, UART_RSA_MSR);
  171. result = mode & UART_RSA_MSR_FIFO;
  172. }
  173. if (result)
  174. up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
  175. return result;
  176. }
  177. static void enable_rsa(struct uart_sunsu_port *up)
  178. {
  179. if (up->port.type == PORT_RSA) {
  180. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
  181. spin_lock_irq(&up->port.lock);
  182. __enable_rsa(up);
  183. spin_unlock_irq(&up->port.lock);
  184. }
  185. if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
  186. serial_outp(up, UART_RSA_FRR, 0);
  187. }
  188. }
  189. /*
  190. * Attempts to turn off the RSA FIFO. Returns zero on failure.
  191. * It is unknown why interrupts were disabled in here. However,
  192. * the caller is expected to preserve this behaviour by grabbing
  193. * the spinlock before calling this function.
  194. */
  195. static void disable_rsa(struct uart_sunsu_port *up)
  196. {
  197. unsigned char mode;
  198. int result;
  199. if (up->port.type == PORT_RSA &&
  200. up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
  201. spin_lock_irq(&up->port.lock);
  202. mode = serial_inp(up, UART_RSA_MSR);
  203. result = !(mode & UART_RSA_MSR_FIFO);
  204. if (!result) {
  205. serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
  206. mode = serial_inp(up, UART_RSA_MSR);
  207. result = !(mode & UART_RSA_MSR_FIFO);
  208. }
  209. if (result)
  210. up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
  211. spin_unlock_irq(&up->port.lock);
  212. }
  213. }
  214. #endif /* CONFIG_SERIAL_8250_RSA */
  215. static inline void __stop_tx(struct uart_sunsu_port *p)
  216. {
  217. if (p->ier & UART_IER_THRI) {
  218. p->ier &= ~UART_IER_THRI;
  219. serial_out(p, UART_IER, p->ier);
  220. }
  221. }
  222. static void sunsu_stop_tx(struct uart_port *port)
  223. {
  224. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  225. __stop_tx(up);
  226. /*
  227. * We really want to stop the transmitter from sending.
  228. */
  229. if (up->port.type == PORT_16C950) {
  230. up->acr |= UART_ACR_TXDIS;
  231. serial_icr_write(up, UART_ACR, up->acr);
  232. }
  233. }
  234. static void sunsu_start_tx(struct uart_port *port)
  235. {
  236. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  237. if (!(up->ier & UART_IER_THRI)) {
  238. up->ier |= UART_IER_THRI;
  239. serial_out(up, UART_IER, up->ier);
  240. }
  241. /*
  242. * Re-enable the transmitter if we disabled it.
  243. */
  244. if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
  245. up->acr &= ~UART_ACR_TXDIS;
  246. serial_icr_write(up, UART_ACR, up->acr);
  247. }
  248. }
  249. static void sunsu_stop_rx(struct uart_port *port)
  250. {
  251. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  252. up->ier &= ~UART_IER_RLSI;
  253. up->port.read_status_mask &= ~UART_LSR_DR;
  254. serial_out(up, UART_IER, up->ier);
  255. }
  256. static void sunsu_enable_ms(struct uart_port *port)
  257. {
  258. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  259. unsigned long flags;
  260. spin_lock_irqsave(&up->port.lock, flags);
  261. up->ier |= UART_IER_MSI;
  262. serial_out(up, UART_IER, up->ier);
  263. spin_unlock_irqrestore(&up->port.lock, flags);
  264. }
  265. static struct tty_struct *
  266. receive_chars(struct uart_sunsu_port *up, unsigned char *status, struct pt_regs *regs)
  267. {
  268. struct tty_struct *tty = up->port.info->tty;
  269. unsigned char ch, flag;
  270. int max_count = 256;
  271. int saw_console_brk = 0;
  272. do {
  273. ch = serial_inp(up, UART_RX);
  274. flag = TTY_NORMAL;
  275. up->port.icount.rx++;
  276. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  277. UART_LSR_FE | UART_LSR_OE))) {
  278. /*
  279. * For statistics only
  280. */
  281. if (*status & UART_LSR_BI) {
  282. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  283. up->port.icount.brk++;
  284. if (up->port.cons != NULL &&
  285. up->port.line == up->port.cons->index)
  286. saw_console_brk = 1;
  287. /*
  288. * We do the SysRQ and SAK checking
  289. * here because otherwise the break
  290. * may get masked by ignore_status_mask
  291. * or read_status_mask.
  292. */
  293. if (uart_handle_break(&up->port))
  294. goto ignore_char;
  295. } else if (*status & UART_LSR_PE)
  296. up->port.icount.parity++;
  297. else if (*status & UART_LSR_FE)
  298. up->port.icount.frame++;
  299. if (*status & UART_LSR_OE)
  300. up->port.icount.overrun++;
  301. /*
  302. * Mask off conditions which should be ingored.
  303. */
  304. *status &= up->port.read_status_mask;
  305. if (up->port.cons != NULL &&
  306. up->port.line == up->port.cons->index) {
  307. /* Recover the break flag from console xmit */
  308. *status |= up->lsr_break_flag;
  309. up->lsr_break_flag = 0;
  310. }
  311. if (*status & UART_LSR_BI) {
  312. flag = TTY_BREAK;
  313. } else if (*status & UART_LSR_PE)
  314. flag = TTY_PARITY;
  315. else if (*status & UART_LSR_FE)
  316. flag = TTY_FRAME;
  317. }
  318. if (uart_handle_sysrq_char(&up->port, ch, regs))
  319. goto ignore_char;
  320. if ((*status & up->port.ignore_status_mask) == 0)
  321. tty_insert_flip_char(tty, ch, flag);
  322. if (*status & UART_LSR_OE)
  323. /*
  324. * Overrun is special, since it's reported
  325. * immediately, and doesn't affect the current
  326. * character.
  327. */
  328. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  329. ignore_char:
  330. *status = serial_inp(up, UART_LSR);
  331. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  332. if (saw_console_brk)
  333. sun_do_break();
  334. return tty;
  335. }
  336. static void transmit_chars(struct uart_sunsu_port *up)
  337. {
  338. struct circ_buf *xmit = &up->port.info->xmit;
  339. int count;
  340. if (up->port.x_char) {
  341. serial_outp(up, UART_TX, up->port.x_char);
  342. up->port.icount.tx++;
  343. up->port.x_char = 0;
  344. return;
  345. }
  346. if (uart_tx_stopped(&up->port)) {
  347. sunsu_stop_tx(&up->port);
  348. return;
  349. }
  350. if (uart_circ_empty(xmit)) {
  351. __stop_tx(up);
  352. return;
  353. }
  354. count = up->port.fifosize;
  355. do {
  356. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  357. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  358. up->port.icount.tx++;
  359. if (uart_circ_empty(xmit))
  360. break;
  361. } while (--count > 0);
  362. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  363. uart_write_wakeup(&up->port);
  364. if (uart_circ_empty(xmit))
  365. __stop_tx(up);
  366. }
  367. static void check_modem_status(struct uart_sunsu_port *up)
  368. {
  369. int status;
  370. status = serial_in(up, UART_MSR);
  371. if ((status & UART_MSR_ANY_DELTA) == 0)
  372. return;
  373. if (status & UART_MSR_TERI)
  374. up->port.icount.rng++;
  375. if (status & UART_MSR_DDSR)
  376. up->port.icount.dsr++;
  377. if (status & UART_MSR_DDCD)
  378. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  379. if (status & UART_MSR_DCTS)
  380. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  381. wake_up_interruptible(&up->port.info->delta_msr_wait);
  382. }
  383. static irqreturn_t sunsu_serial_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  384. {
  385. struct uart_sunsu_port *up = dev_id;
  386. unsigned long flags;
  387. unsigned char status;
  388. spin_lock_irqsave(&up->port.lock, flags);
  389. do {
  390. struct tty_struct *tty;
  391. status = serial_inp(up, UART_LSR);
  392. tty = NULL;
  393. if (status & UART_LSR_DR)
  394. tty = receive_chars(up, &status, regs);
  395. check_modem_status(up);
  396. if (status & UART_LSR_THRE)
  397. transmit_chars(up);
  398. spin_unlock_irqrestore(&up->port.lock, flags);
  399. if (tty)
  400. tty_flip_buffer_push(tty);
  401. spin_lock_irqsave(&up->port.lock, flags);
  402. } while (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT));
  403. spin_unlock_irqrestore(&up->port.lock, flags);
  404. return IRQ_HANDLED;
  405. }
  406. /* Separate interrupt handling path for keyboard/mouse ports. */
  407. static void
  408. sunsu_change_speed(struct uart_port *port, unsigned int cflag,
  409. unsigned int iflag, unsigned int quot);
  410. static void sunsu_change_mouse_baud(struct uart_sunsu_port *up)
  411. {
  412. unsigned int cur_cflag = up->cflag;
  413. int quot, new_baud;
  414. up->cflag &= ~CBAUD;
  415. up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
  416. quot = up->port.uartclk / (16 * new_baud);
  417. sunsu_change_speed(&up->port, up->cflag, 0, quot);
  418. }
  419. static void receive_kbd_ms_chars(struct uart_sunsu_port *up, struct pt_regs *regs, int is_break)
  420. {
  421. do {
  422. unsigned char ch = serial_inp(up, UART_RX);
  423. /* Stop-A is handled by drivers/char/keyboard.c now. */
  424. if (up->su_type == SU_PORT_KBD) {
  425. #ifdef CONFIG_SERIO
  426. serio_interrupt(&up->serio, ch, 0, regs);
  427. #endif
  428. } else if (up->su_type == SU_PORT_MS) {
  429. int ret = suncore_mouse_baud_detection(ch, is_break);
  430. switch (ret) {
  431. case 2:
  432. sunsu_change_mouse_baud(up);
  433. /* fallthru */
  434. case 1:
  435. break;
  436. case 0:
  437. #ifdef CONFIG_SERIO
  438. serio_interrupt(&up->serio, ch, 0, regs);
  439. #endif
  440. break;
  441. };
  442. }
  443. } while (serial_in(up, UART_LSR) & UART_LSR_DR);
  444. }
  445. static irqreturn_t sunsu_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  446. {
  447. struct uart_sunsu_port *up = dev_id;
  448. if (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT)) {
  449. unsigned char status = serial_inp(up, UART_LSR);
  450. if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
  451. receive_kbd_ms_chars(up, regs,
  452. (status & UART_LSR_BI) != 0);
  453. }
  454. return IRQ_HANDLED;
  455. }
  456. static unsigned int sunsu_tx_empty(struct uart_port *port)
  457. {
  458. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  459. unsigned long flags;
  460. unsigned int ret;
  461. spin_lock_irqsave(&up->port.lock, flags);
  462. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  463. spin_unlock_irqrestore(&up->port.lock, flags);
  464. return ret;
  465. }
  466. static unsigned int sunsu_get_mctrl(struct uart_port *port)
  467. {
  468. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  469. unsigned char status;
  470. unsigned int ret;
  471. status = serial_in(up, UART_MSR);
  472. ret = 0;
  473. if (status & UART_MSR_DCD)
  474. ret |= TIOCM_CAR;
  475. if (status & UART_MSR_RI)
  476. ret |= TIOCM_RNG;
  477. if (status & UART_MSR_DSR)
  478. ret |= TIOCM_DSR;
  479. if (status & UART_MSR_CTS)
  480. ret |= TIOCM_CTS;
  481. return ret;
  482. }
  483. static void sunsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  484. {
  485. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  486. unsigned char mcr = 0;
  487. if (mctrl & TIOCM_RTS)
  488. mcr |= UART_MCR_RTS;
  489. if (mctrl & TIOCM_DTR)
  490. mcr |= UART_MCR_DTR;
  491. if (mctrl & TIOCM_OUT1)
  492. mcr |= UART_MCR_OUT1;
  493. if (mctrl & TIOCM_OUT2)
  494. mcr |= UART_MCR_OUT2;
  495. if (mctrl & TIOCM_LOOP)
  496. mcr |= UART_MCR_LOOP;
  497. serial_out(up, UART_MCR, mcr);
  498. }
  499. static void sunsu_break_ctl(struct uart_port *port, int break_state)
  500. {
  501. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  502. unsigned long flags;
  503. spin_lock_irqsave(&up->port.lock, flags);
  504. if (break_state == -1)
  505. up->lcr |= UART_LCR_SBC;
  506. else
  507. up->lcr &= ~UART_LCR_SBC;
  508. serial_out(up, UART_LCR, up->lcr);
  509. spin_unlock_irqrestore(&up->port.lock, flags);
  510. }
  511. static int sunsu_startup(struct uart_port *port)
  512. {
  513. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  514. unsigned long flags;
  515. int retval;
  516. if (up->port.type == PORT_16C950) {
  517. /* Wake up and initialize UART */
  518. up->acr = 0;
  519. serial_outp(up, UART_LCR, 0xBF);
  520. serial_outp(up, UART_EFR, UART_EFR_ECB);
  521. serial_outp(up, UART_IER, 0);
  522. serial_outp(up, UART_LCR, 0);
  523. serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
  524. serial_outp(up, UART_LCR, 0xBF);
  525. serial_outp(up, UART_EFR, UART_EFR_ECB);
  526. serial_outp(up, UART_LCR, 0);
  527. }
  528. #ifdef CONFIG_SERIAL_8250_RSA
  529. /*
  530. * If this is an RSA port, see if we can kick it up to the
  531. * higher speed clock.
  532. */
  533. enable_rsa(up);
  534. #endif
  535. /*
  536. * Clear the FIFO buffers and disable them.
  537. * (they will be reenabled in set_termios())
  538. */
  539. if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
  540. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  541. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  542. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  543. serial_outp(up, UART_FCR, 0);
  544. }
  545. /*
  546. * Clear the interrupt registers.
  547. */
  548. (void) serial_inp(up, UART_LSR);
  549. (void) serial_inp(up, UART_RX);
  550. (void) serial_inp(up, UART_IIR);
  551. (void) serial_inp(up, UART_MSR);
  552. /*
  553. * At this point, there's no way the LSR could still be 0xff;
  554. * if it is, then bail out, because there's likely no UART
  555. * here.
  556. */
  557. if (!(up->port.flags & UPF_BUGGY_UART) &&
  558. (serial_inp(up, UART_LSR) == 0xff)) {
  559. printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
  560. return -ENODEV;
  561. }
  562. if (up->su_type != SU_PORT_PORT) {
  563. retval = request_irq(up->port.irq, sunsu_kbd_ms_interrupt,
  564. SA_SHIRQ, su_typev[up->su_type], up);
  565. } else {
  566. retval = request_irq(up->port.irq, sunsu_serial_interrupt,
  567. SA_SHIRQ, su_typev[up->su_type], up);
  568. }
  569. if (retval) {
  570. printk("su: Cannot register IRQ %d\n", up->port.irq);
  571. return retval;
  572. }
  573. /*
  574. * Now, initialize the UART
  575. */
  576. serial_outp(up, UART_LCR, UART_LCR_WLEN8);
  577. spin_lock_irqsave(&up->port.lock, flags);
  578. up->port.mctrl |= TIOCM_OUT2;
  579. sunsu_set_mctrl(&up->port, up->port.mctrl);
  580. spin_unlock_irqrestore(&up->port.lock, flags);
  581. /*
  582. * Finally, enable interrupts. Note: Modem status interrupts
  583. * are set via set_termios(), which will be occurring imminently
  584. * anyway, so we don't enable them here.
  585. */
  586. up->ier = UART_IER_RLSI | UART_IER_RDI;
  587. serial_outp(up, UART_IER, up->ier);
  588. if (up->port.flags & UPF_FOURPORT) {
  589. unsigned int icp;
  590. /*
  591. * Enable interrupts on the AST Fourport board
  592. */
  593. icp = (up->port.iobase & 0xfe0) | 0x01f;
  594. outb_p(0x80, icp);
  595. (void) inb_p(icp);
  596. }
  597. /*
  598. * And clear the interrupt registers again for luck.
  599. */
  600. (void) serial_inp(up, UART_LSR);
  601. (void) serial_inp(up, UART_RX);
  602. (void) serial_inp(up, UART_IIR);
  603. (void) serial_inp(up, UART_MSR);
  604. return 0;
  605. }
  606. static void sunsu_shutdown(struct uart_port *port)
  607. {
  608. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  609. unsigned long flags;
  610. /*
  611. * Disable interrupts from this port
  612. */
  613. up->ier = 0;
  614. serial_outp(up, UART_IER, 0);
  615. spin_lock_irqsave(&up->port.lock, flags);
  616. if (up->port.flags & UPF_FOURPORT) {
  617. /* reset interrupts on the AST Fourport board */
  618. inb((up->port.iobase & 0xfe0) | 0x1f);
  619. up->port.mctrl |= TIOCM_OUT1;
  620. } else
  621. up->port.mctrl &= ~TIOCM_OUT2;
  622. sunsu_set_mctrl(&up->port, up->port.mctrl);
  623. spin_unlock_irqrestore(&up->port.lock, flags);
  624. /*
  625. * Disable break condition and FIFOs
  626. */
  627. serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
  628. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  629. UART_FCR_CLEAR_RCVR |
  630. UART_FCR_CLEAR_XMIT);
  631. serial_outp(up, UART_FCR, 0);
  632. #ifdef CONFIG_SERIAL_8250_RSA
  633. /*
  634. * Reset the RSA board back to 115kbps compat mode.
  635. */
  636. disable_rsa(up);
  637. #endif
  638. /*
  639. * Read data port to reset things.
  640. */
  641. (void) serial_in(up, UART_RX);
  642. free_irq(up->port.irq, up);
  643. }
  644. static void
  645. sunsu_change_speed(struct uart_port *port, unsigned int cflag,
  646. unsigned int iflag, unsigned int quot)
  647. {
  648. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  649. unsigned char cval, fcr = 0;
  650. unsigned long flags;
  651. switch (cflag & CSIZE) {
  652. case CS5:
  653. cval = 0x00;
  654. break;
  655. case CS6:
  656. cval = 0x01;
  657. break;
  658. case CS7:
  659. cval = 0x02;
  660. break;
  661. default:
  662. case CS8:
  663. cval = 0x03;
  664. break;
  665. }
  666. if (cflag & CSTOPB)
  667. cval |= 0x04;
  668. if (cflag & PARENB)
  669. cval |= UART_LCR_PARITY;
  670. if (!(cflag & PARODD))
  671. cval |= UART_LCR_EPAR;
  672. #ifdef CMSPAR
  673. if (cflag & CMSPAR)
  674. cval |= UART_LCR_SPAR;
  675. #endif
  676. /*
  677. * Work around a bug in the Oxford Semiconductor 952 rev B
  678. * chip which causes it to seriously miscalculate baud rates
  679. * when DLL is 0.
  680. */
  681. if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
  682. up->rev == 0x5201)
  683. quot ++;
  684. if (uart_config[up->port.type].flags & UART_USE_FIFO) {
  685. if ((up->port.uartclk / quot) < (2400 * 16))
  686. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  687. #ifdef CONFIG_SERIAL_8250_RSA
  688. else if (up->port.type == PORT_RSA)
  689. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
  690. #endif
  691. else
  692. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
  693. }
  694. if (up->port.type == PORT_16750)
  695. fcr |= UART_FCR7_64BYTE;
  696. /*
  697. * Ok, we're now changing the port state. Do it with
  698. * interrupts disabled.
  699. */
  700. spin_lock_irqsave(&up->port.lock, flags);
  701. /*
  702. * Update the per-port timeout.
  703. */
  704. uart_update_timeout(port, cflag, (port->uartclk / (16 * quot)));
  705. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  706. if (iflag & INPCK)
  707. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  708. if (iflag & (BRKINT | PARMRK))
  709. up->port.read_status_mask |= UART_LSR_BI;
  710. /*
  711. * Characteres to ignore
  712. */
  713. up->port.ignore_status_mask = 0;
  714. if (iflag & IGNPAR)
  715. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  716. if (iflag & IGNBRK) {
  717. up->port.ignore_status_mask |= UART_LSR_BI;
  718. /*
  719. * If we're ignoring parity and break indicators,
  720. * ignore overruns too (for real raw support).
  721. */
  722. if (iflag & IGNPAR)
  723. up->port.ignore_status_mask |= UART_LSR_OE;
  724. }
  725. /*
  726. * ignore all characters if CREAD is not set
  727. */
  728. if ((cflag & CREAD) == 0)
  729. up->port.ignore_status_mask |= UART_LSR_DR;
  730. /*
  731. * CTS flow control flag and modem status interrupts
  732. */
  733. up->ier &= ~UART_IER_MSI;
  734. if (UART_ENABLE_MS(&up->port, cflag))
  735. up->ier |= UART_IER_MSI;
  736. serial_out(up, UART_IER, up->ier);
  737. if (uart_config[up->port.type].flags & UART_STARTECH) {
  738. serial_outp(up, UART_LCR, 0xBF);
  739. serial_outp(up, UART_EFR, cflag & CRTSCTS ? UART_EFR_CTS :0);
  740. }
  741. serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
  742. serial_outp(up, UART_DLL, quot & 0xff); /* LS of divisor */
  743. serial_outp(up, UART_DLM, quot >> 8); /* MS of divisor */
  744. if (up->port.type == PORT_16750)
  745. serial_outp(up, UART_FCR, fcr); /* set fcr */
  746. serial_outp(up, UART_LCR, cval); /* reset DLAB */
  747. up->lcr = cval; /* Save LCR */
  748. if (up->port.type != PORT_16750) {
  749. if (fcr & UART_FCR_ENABLE_FIFO) {
  750. /* emulated UARTs (Lucent Venus 167x) need two steps */
  751. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  752. }
  753. serial_outp(up, UART_FCR, fcr); /* set fcr */
  754. }
  755. up->cflag = cflag;
  756. spin_unlock_irqrestore(&up->port.lock, flags);
  757. }
  758. static void
  759. sunsu_set_termios(struct uart_port *port, struct termios *termios,
  760. struct termios *old)
  761. {
  762. unsigned int baud, quot;
  763. /*
  764. * Ask the core to calculate the divisor for us.
  765. */
  766. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  767. quot = uart_get_divisor(port, baud);
  768. sunsu_change_speed(port, termios->c_cflag, termios->c_iflag, quot);
  769. }
  770. static void sunsu_release_port(struct uart_port *port)
  771. {
  772. }
  773. static int sunsu_request_port(struct uart_port *port)
  774. {
  775. return 0;
  776. }
  777. static void sunsu_config_port(struct uart_port *port, int flags)
  778. {
  779. struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
  780. if (flags & UART_CONFIG_TYPE) {
  781. /*
  782. * We are supposed to call autoconfig here, but this requires
  783. * splitting all the OBP probing crap from the UART probing.
  784. * We'll do it when we kill sunsu.c altogether.
  785. */
  786. port->type = up->type_probed; /* XXX */
  787. }
  788. }
  789. static int
  790. sunsu_verify_port(struct uart_port *port, struct serial_struct *ser)
  791. {
  792. return -EINVAL;
  793. }
  794. static const char *
  795. sunsu_type(struct uart_port *port)
  796. {
  797. int type = port->type;
  798. if (type >= ARRAY_SIZE(uart_config))
  799. type = 0;
  800. return uart_config[type].name;
  801. }
  802. static struct uart_ops sunsu_pops = {
  803. .tx_empty = sunsu_tx_empty,
  804. .set_mctrl = sunsu_set_mctrl,
  805. .get_mctrl = sunsu_get_mctrl,
  806. .stop_tx = sunsu_stop_tx,
  807. .start_tx = sunsu_start_tx,
  808. .stop_rx = sunsu_stop_rx,
  809. .enable_ms = sunsu_enable_ms,
  810. .break_ctl = sunsu_break_ctl,
  811. .startup = sunsu_startup,
  812. .shutdown = sunsu_shutdown,
  813. .set_termios = sunsu_set_termios,
  814. .type = sunsu_type,
  815. .release_port = sunsu_release_port,
  816. .request_port = sunsu_request_port,
  817. .config_port = sunsu_config_port,
  818. .verify_port = sunsu_verify_port,
  819. };
  820. #define UART_NR 4
  821. static struct uart_sunsu_port sunsu_ports[UART_NR];
  822. #ifdef CONFIG_SERIO
  823. static DEFINE_SPINLOCK(sunsu_serio_lock);
  824. static int sunsu_serio_write(struct serio *serio, unsigned char ch)
  825. {
  826. struct uart_sunsu_port *up = serio->port_data;
  827. unsigned long flags;
  828. int lsr;
  829. spin_lock_irqsave(&sunsu_serio_lock, flags);
  830. do {
  831. lsr = serial_in(up, UART_LSR);
  832. } while (!(lsr & UART_LSR_THRE));
  833. /* Send the character out. */
  834. serial_out(up, UART_TX, ch);
  835. spin_unlock_irqrestore(&sunsu_serio_lock, flags);
  836. return 0;
  837. }
  838. static int sunsu_serio_open(struct serio *serio)
  839. {
  840. struct uart_sunsu_port *up = serio->port_data;
  841. unsigned long flags;
  842. int ret;
  843. spin_lock_irqsave(&sunsu_serio_lock, flags);
  844. if (!up->serio_open) {
  845. up->serio_open = 1;
  846. ret = 0;
  847. } else
  848. ret = -EBUSY;
  849. spin_unlock_irqrestore(&sunsu_serio_lock, flags);
  850. return ret;
  851. }
  852. static void sunsu_serio_close(struct serio *serio)
  853. {
  854. struct uart_sunsu_port *up = serio->port_data;
  855. unsigned long flags;
  856. spin_lock_irqsave(&sunsu_serio_lock, flags);
  857. up->serio_open = 0;
  858. spin_unlock_irqrestore(&sunsu_serio_lock, flags);
  859. }
  860. #endif /* CONFIG_SERIO */
  861. static void sunsu_autoconfig(struct uart_sunsu_port *up)
  862. {
  863. unsigned char status1, status2, scratch, scratch2, scratch3;
  864. unsigned char save_lcr, save_mcr;
  865. unsigned long flags;
  866. if (up->su_type == SU_PORT_NONE)
  867. return;
  868. up->type_probed = PORT_UNKNOWN;
  869. up->port.iotype = UPIO_MEM;
  870. spin_lock_irqsave(&up->port.lock, flags);
  871. if (!(up->port.flags & UPF_BUGGY_UART)) {
  872. /*
  873. * Do a simple existence test first; if we fail this, there's
  874. * no point trying anything else.
  875. *
  876. * 0x80 is used as a nonsense port to prevent against false
  877. * positives due to ISA bus float. The assumption is that
  878. * 0x80 is a non-existent port; which should be safe since
  879. * include/asm/io.h also makes this assumption.
  880. */
  881. scratch = serial_inp(up, UART_IER);
  882. serial_outp(up, UART_IER, 0);
  883. #ifdef __i386__
  884. outb(0xff, 0x080);
  885. #endif
  886. scratch2 = serial_inp(up, UART_IER);
  887. serial_outp(up, UART_IER, 0x0f);
  888. #ifdef __i386__
  889. outb(0, 0x080);
  890. #endif
  891. scratch3 = serial_inp(up, UART_IER);
  892. serial_outp(up, UART_IER, scratch);
  893. if (scratch2 != 0 || scratch3 != 0x0F)
  894. goto out; /* We failed; there's nothing here */
  895. }
  896. save_mcr = serial_in(up, UART_MCR);
  897. save_lcr = serial_in(up, UART_LCR);
  898. /*
  899. * Check to see if a UART is really there. Certain broken
  900. * internal modems based on the Rockwell chipset fail this
  901. * test, because they apparently don't implement the loopback
  902. * test mode. So this test is skipped on the COM 1 through
  903. * COM 4 ports. This *should* be safe, since no board
  904. * manufacturer would be stupid enough to design a board
  905. * that conflicts with COM 1-4 --- we hope!
  906. */
  907. if (!(up->port.flags & UPF_SKIP_TEST)) {
  908. serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
  909. status1 = serial_inp(up, UART_MSR) & 0xF0;
  910. serial_outp(up, UART_MCR, save_mcr);
  911. if (status1 != 0x90)
  912. goto out; /* We failed loopback test */
  913. }
  914. serial_outp(up, UART_LCR, 0xBF); /* set up for StarTech test */
  915. serial_outp(up, UART_EFR, 0); /* EFR is the same as FCR */
  916. serial_outp(up, UART_LCR, 0);
  917. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  918. scratch = serial_in(up, UART_IIR) >> 6;
  919. switch (scratch) {
  920. case 0:
  921. up->port.type = PORT_16450;
  922. break;
  923. case 1:
  924. up->port.type = PORT_UNKNOWN;
  925. break;
  926. case 2:
  927. up->port.type = PORT_16550;
  928. break;
  929. case 3:
  930. up->port.type = PORT_16550A;
  931. break;
  932. }
  933. if (up->port.type == PORT_16550A) {
  934. /* Check for Startech UART's */
  935. serial_outp(up, UART_LCR, UART_LCR_DLAB);
  936. if (serial_in(up, UART_EFR) == 0) {
  937. up->port.type = PORT_16650;
  938. } else {
  939. serial_outp(up, UART_LCR, 0xBF);
  940. if (serial_in(up, UART_EFR) == 0)
  941. up->port.type = PORT_16650V2;
  942. }
  943. }
  944. if (up->port.type == PORT_16550A) {
  945. /* Check for TI 16750 */
  946. serial_outp(up, UART_LCR, save_lcr | UART_LCR_DLAB);
  947. serial_outp(up, UART_FCR,
  948. UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  949. scratch = serial_in(up, UART_IIR) >> 5;
  950. if (scratch == 7) {
  951. /*
  952. * If this is a 16750, and not a cheap UART
  953. * clone, then it should only go into 64 byte
  954. * mode if the UART_FCR7_64BYTE bit was set
  955. * while UART_LCR_DLAB was latched.
  956. */
  957. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  958. serial_outp(up, UART_LCR, 0);
  959. serial_outp(up, UART_FCR,
  960. UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  961. scratch = serial_in(up, UART_IIR) >> 5;
  962. if (scratch == 6)
  963. up->port.type = PORT_16750;
  964. }
  965. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  966. }
  967. serial_outp(up, UART_LCR, save_lcr);
  968. if (up->port.type == PORT_16450) {
  969. scratch = serial_in(up, UART_SCR);
  970. serial_outp(up, UART_SCR, 0xa5);
  971. status1 = serial_in(up, UART_SCR);
  972. serial_outp(up, UART_SCR, 0x5a);
  973. status2 = serial_in(up, UART_SCR);
  974. serial_outp(up, UART_SCR, scratch);
  975. if ((status1 != 0xa5) || (status2 != 0x5a))
  976. up->port.type = PORT_8250;
  977. }
  978. up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
  979. if (up->port.type == PORT_UNKNOWN)
  980. goto out;
  981. up->type_probed = up->port.type; /* XXX */
  982. /*
  983. * Reset the UART.
  984. */
  985. #ifdef CONFIG_SERIAL_8250_RSA
  986. if (up->port.type == PORT_RSA)
  987. serial_outp(up, UART_RSA_FRR, 0);
  988. #endif
  989. serial_outp(up, UART_MCR, save_mcr);
  990. serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
  991. UART_FCR_CLEAR_RCVR |
  992. UART_FCR_CLEAR_XMIT));
  993. serial_outp(up, UART_FCR, 0);
  994. (void)serial_in(up, UART_RX);
  995. serial_outp(up, UART_IER, 0);
  996. out:
  997. spin_unlock_irqrestore(&up->port.lock, flags);
  998. }
  999. static struct uart_driver sunsu_reg = {
  1000. .owner = THIS_MODULE,
  1001. .driver_name = "serial",
  1002. .dev_name = "ttyS",
  1003. .major = TTY_MAJOR,
  1004. };
  1005. static int __init sunsu_kbd_ms_init(struct uart_sunsu_port *up)
  1006. {
  1007. int quot, baud;
  1008. #ifdef CONFIG_SERIO
  1009. struct serio *serio;
  1010. #endif
  1011. if (up->su_type == SU_PORT_KBD) {
  1012. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1013. baud = 1200;
  1014. } else {
  1015. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1016. baud = 4800;
  1017. }
  1018. quot = up->port.uartclk / (16 * baud);
  1019. sunsu_autoconfig(up);
  1020. if (up->port.type == PORT_UNKNOWN)
  1021. return -ENODEV;
  1022. #ifdef CONFIG_SERIO
  1023. serio = &up->serio;
  1024. serio->port_data = up;
  1025. serio->id.type = SERIO_RS232;
  1026. if (up->su_type == SU_PORT_KBD) {
  1027. serio->id.proto = SERIO_SUNKBD;
  1028. strlcpy(serio->name, "sukbd", sizeof(serio->name));
  1029. } else {
  1030. serio->id.proto = SERIO_SUN;
  1031. serio->id.extra = 1;
  1032. strlcpy(serio->name, "sums", sizeof(serio->name));
  1033. }
  1034. strlcpy(serio->phys,
  1035. (!(up->port.line & 1) ? "su/serio0" : "su/serio1"),
  1036. sizeof(serio->phys));
  1037. serio->write = sunsu_serio_write;
  1038. serio->open = sunsu_serio_open;
  1039. serio->close = sunsu_serio_close;
  1040. serio->dev.parent = up->port.dev;
  1041. serio_register_port(serio);
  1042. #endif
  1043. sunsu_change_speed(&up->port, up->cflag, 0, quot);
  1044. sunsu_startup(&up->port);
  1045. return 0;
  1046. }
  1047. /*
  1048. * ------------------------------------------------------------
  1049. * Serial console driver
  1050. * ------------------------------------------------------------
  1051. */
  1052. #ifdef CONFIG_SERIAL_SUNSU_CONSOLE
  1053. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  1054. /*
  1055. * Wait for transmitter & holding register to empty
  1056. */
  1057. static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
  1058. {
  1059. unsigned int status, tmout = 10000;
  1060. /* Wait up to 10ms for the character(s) to be sent. */
  1061. do {
  1062. status = serial_in(up, UART_LSR);
  1063. if (status & UART_LSR_BI)
  1064. up->lsr_break_flag = UART_LSR_BI;
  1065. if (--tmout == 0)
  1066. break;
  1067. udelay(1);
  1068. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  1069. /* Wait up to 1s for flow control if necessary */
  1070. if (up->port.flags & UPF_CONS_FLOW) {
  1071. tmout = 1000000;
  1072. while (--tmout &&
  1073. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  1074. udelay(1);
  1075. }
  1076. }
  1077. static void sunsu_console_putchar(struct uart_port *port, int ch)
  1078. {
  1079. struct uart_sunsu_port *up = (struct uart_sunsu_port *)port;
  1080. wait_for_xmitr(up);
  1081. serial_out(up, UART_TX, ch);
  1082. }
  1083. /*
  1084. * Print a string to the serial port trying not to disturb
  1085. * any possible real use of the port...
  1086. */
  1087. static void sunsu_console_write(struct console *co, const char *s,
  1088. unsigned int count)
  1089. {
  1090. struct uart_sunsu_port *up = &sunsu_ports[co->index];
  1091. unsigned int ier;
  1092. /*
  1093. * First save the UER then disable the interrupts
  1094. */
  1095. ier = serial_in(up, UART_IER);
  1096. serial_out(up, UART_IER, 0);
  1097. uart_console_write(&up->port, s, count, sunsu_console_putchar);
  1098. /*
  1099. * Finally, wait for transmitter to become empty
  1100. * and restore the IER
  1101. */
  1102. wait_for_xmitr(up);
  1103. serial_out(up, UART_IER, ier);
  1104. }
  1105. /*
  1106. * Setup initial baud/bits/parity. We do two things here:
  1107. * - construct a cflag setting for the first su_open()
  1108. * - initialize the serial port
  1109. * Return non-zero if we didn't find a serial port.
  1110. */
  1111. static int sunsu_console_setup(struct console *co, char *options)
  1112. {
  1113. struct uart_port *port;
  1114. int baud = 9600;
  1115. int bits = 8;
  1116. int parity = 'n';
  1117. int flow = 'n';
  1118. printk("Console: ttyS%d (SU)\n",
  1119. (sunsu_reg.minor - 64) + co->index);
  1120. /*
  1121. * Check whether an invalid uart number has been specified, and
  1122. * if so, search for the first available port that does have
  1123. * console support.
  1124. */
  1125. if (co->index >= UART_NR)
  1126. co->index = 0;
  1127. port = &sunsu_ports[co->index].port;
  1128. /*
  1129. * Temporary fix.
  1130. */
  1131. spin_lock_init(&port->lock);
  1132. if (options)
  1133. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1134. return uart_set_options(port, co, baud, parity, bits, flow);
  1135. }
  1136. static struct console sunsu_cons = {
  1137. .name = "ttyS",
  1138. .write = sunsu_console_write,
  1139. .device = uart_console_device,
  1140. .setup = sunsu_console_setup,
  1141. .flags = CON_PRINTBUFFER,
  1142. .index = -1,
  1143. .data = &sunsu_reg,
  1144. };
  1145. /*
  1146. * Register console.
  1147. */
  1148. static inline struct console *SUNSU_CONSOLE(int num_uart)
  1149. {
  1150. int i;
  1151. if (con_is_present())
  1152. return NULL;
  1153. for (i = 0; i < num_uart; i++) {
  1154. int this_minor = sunsu_reg.minor + i;
  1155. if ((this_minor - 64) == (serial_console - 1))
  1156. break;
  1157. }
  1158. if (i == num_uart)
  1159. return NULL;
  1160. sunsu_cons.index = i;
  1161. return &sunsu_cons;
  1162. }
  1163. #else
  1164. #define SUNSU_CONSOLE(num_uart) (NULL)
  1165. #define sunsu_serial_console_init() do { } while (0)
  1166. #endif
  1167. static enum su_type __devinit su_get_type(struct device_node *dp)
  1168. {
  1169. struct device_node *ap = of_find_node_by_path("/aliases");
  1170. if (ap) {
  1171. char *keyb = of_get_property(ap, "keyboard", NULL);
  1172. char *ms = of_get_property(ap, "mouse", NULL);
  1173. if (keyb) {
  1174. if (dp == of_find_node_by_path(keyb))
  1175. return SU_PORT_KBD;
  1176. }
  1177. if (ms) {
  1178. if (dp == of_find_node_by_path(ms))
  1179. return SU_PORT_MS;
  1180. }
  1181. }
  1182. return SU_PORT_PORT;
  1183. }
  1184. static int __devinit su_probe(struct of_device *op, const struct of_device_id *match)
  1185. {
  1186. static int inst;
  1187. struct device_node *dp = op->node;
  1188. struct uart_sunsu_port *up;
  1189. struct resource *rp;
  1190. int err;
  1191. if (inst >= UART_NR)
  1192. return -EINVAL;
  1193. up = &sunsu_ports[inst];
  1194. up->port.line = inst;
  1195. spin_lock_init(&up->port.lock);
  1196. up->su_type = su_get_type(dp);
  1197. rp = &op->resource[0];
  1198. up->port.mapbase = op->resource[0].start;
  1199. up->reg_size = (rp->end - rp->start) + 1;
  1200. up->port.membase = of_ioremap(rp, 0, up->reg_size, "su");
  1201. if (!up->port.membase)
  1202. return -ENOMEM;
  1203. up->port.irq = op->irqs[0];
  1204. up->port.dev = &op->dev;
  1205. up->port.type = PORT_UNKNOWN;
  1206. up->port.uartclk = (SU_BASE_BAUD * 16);
  1207. err = 0;
  1208. if (up->su_type == SU_PORT_KBD || up->su_type == SU_PORT_MS) {
  1209. err = sunsu_kbd_ms_init(up);
  1210. if (err)
  1211. goto out_unmap;
  1212. return 0;
  1213. }
  1214. up->port.flags |= UPF_BOOT_AUTOCONF;
  1215. sunsu_autoconfig(up);
  1216. err = -ENODEV;
  1217. if (up->port.type == PORT_UNKNOWN)
  1218. goto out_unmap;
  1219. up->port.ops = &sunsu_pops;
  1220. err = uart_add_one_port(&sunsu_reg, &up->port);
  1221. if (err)
  1222. goto out_unmap;
  1223. dev_set_drvdata(&op->dev, up);
  1224. inst++;
  1225. return 0;
  1226. out_unmap:
  1227. of_iounmap(up->port.membase, up->reg_size);
  1228. return err;
  1229. }
  1230. static int __devexit su_remove(struct of_device *dev)
  1231. {
  1232. struct uart_sunsu_port *up = dev_get_drvdata(&dev->dev);;
  1233. if (up->su_type == SU_PORT_MS ||
  1234. up->su_type == SU_PORT_KBD) {
  1235. #ifdef CONFIG_SERIO
  1236. serio_unregister_port(&up->serio);
  1237. #endif
  1238. } else if (up->port.type != PORT_UNKNOWN)
  1239. uart_remove_one_port(&sunsu_reg, &up->port);
  1240. return 0;
  1241. }
  1242. static struct of_device_id su_match[] = {
  1243. {
  1244. .name = "su",
  1245. },
  1246. {
  1247. .name = "su_pnp",
  1248. },
  1249. {
  1250. .name = "serial",
  1251. .compatible = "su",
  1252. },
  1253. {},
  1254. };
  1255. MODULE_DEVICE_TABLE(of, su_match);
  1256. static struct of_platform_driver su_driver = {
  1257. .name = "su",
  1258. .match_table = su_match,
  1259. .probe = su_probe,
  1260. .remove = __devexit_p(su_remove),
  1261. };
  1262. static int num_uart;
  1263. static int __init sunsu_init(void)
  1264. {
  1265. struct device_node *dp;
  1266. int err;
  1267. num_uart = 0;
  1268. for_each_node_by_name(dp, "su") {
  1269. if (su_get_type(dp) == SU_PORT_PORT)
  1270. num_uart++;
  1271. }
  1272. for_each_node_by_name(dp, "su_pnp") {
  1273. if (su_get_type(dp) == SU_PORT_PORT)
  1274. num_uart++;
  1275. }
  1276. for_each_node_by_name(dp, "serial") {
  1277. if (of_device_is_compatible(dp, "su")) {
  1278. if (su_get_type(dp) == SU_PORT_PORT)
  1279. num_uart++;
  1280. }
  1281. }
  1282. if (num_uart) {
  1283. sunsu_reg.minor = sunserial_current_minor;
  1284. sunsu_reg.nr = num_uart;
  1285. err = uart_register_driver(&sunsu_reg);
  1286. if (err)
  1287. return err;
  1288. sunsu_reg.tty_driver->name_base = sunsu_reg.minor - 64;
  1289. sunserial_current_minor += num_uart;
  1290. sunsu_reg.cons = SUNSU_CONSOLE(num_uart);
  1291. }
  1292. err = of_register_driver(&su_driver, &of_bus_type);
  1293. if (err && num_uart)
  1294. uart_unregister_driver(&sunsu_reg);
  1295. return err;
  1296. }
  1297. static void __exit sunsu_exit(void)
  1298. {
  1299. if (num_uart)
  1300. uart_unregister_driver(&sunsu_reg);
  1301. }
  1302. module_init(sunsu_init);
  1303. module_exit(sunsu_exit);
  1304. MODULE_AUTHOR("Eddie C. Dost, Peter Zaitcev, and David S. Miller");
  1305. MODULE_DESCRIPTION("Sun SU serial port driver");
  1306. MODULE_VERSION("2.0");
  1307. MODULE_LICENSE("GPL");