sunsu.c 42 KB

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