sunsu.c 41 KB

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