sunsu.c 37 KB

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