sunsu.c 42 KB

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