sunsu.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  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/spinlock.h>
  20. #include <linux/errno.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/string.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/ioport.h>
  27. #include <linux/circ_buf.h>
  28. #include <linux/serial.h>
  29. #include <linux/sysrq.h>
  30. #include <linux/console.h>
  31. #include <linux/slab.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 <linux/of_device.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/prom.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.state->port.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.state->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.state->port.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 ktermios *termios,
  758. struct ktermios *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 = "sunsu",
  1000. .dev_name = "ttyS",
  1001. .major = TTY_MAJOR,
  1002. };
  1003. static int __devinit 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 %llx, irq %u\n",
  1021. to_of_device(up->port.dev)->node->full_name,
  1022. (up->su_type == SU_PORT_KBD) ? "Keyboard" : "Mouse",
  1023. (unsigned long long) up->port.mapbase,
  1024. up->port.irq);
  1025. #ifdef CONFIG_SERIO
  1026. serio = &up->serio;
  1027. serio->port_data = up;
  1028. serio->id.type = SERIO_RS232;
  1029. if (up->su_type == SU_PORT_KBD) {
  1030. serio->id.proto = SERIO_SUNKBD;
  1031. strlcpy(serio->name, "sukbd", sizeof(serio->name));
  1032. } else {
  1033. serio->id.proto = SERIO_SUN;
  1034. serio->id.extra = 1;
  1035. strlcpy(serio->name, "sums", sizeof(serio->name));
  1036. }
  1037. strlcpy(serio->phys,
  1038. (!(up->port.line & 1) ? "su/serio0" : "su/serio1"),
  1039. sizeof(serio->phys));
  1040. serio->write = sunsu_serio_write;
  1041. serio->open = sunsu_serio_open;
  1042. serio->close = sunsu_serio_close;
  1043. serio->dev.parent = up->port.dev;
  1044. serio_register_port(serio);
  1045. #endif
  1046. sunsu_change_speed(&up->port, up->cflag, 0, quot);
  1047. sunsu_startup(&up->port);
  1048. return 0;
  1049. }
  1050. /*
  1051. * ------------------------------------------------------------
  1052. * Serial console driver
  1053. * ------------------------------------------------------------
  1054. */
  1055. #ifdef CONFIG_SERIAL_SUNSU_CONSOLE
  1056. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  1057. /*
  1058. * Wait for transmitter & holding register to empty
  1059. */
  1060. static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
  1061. {
  1062. unsigned int status, tmout = 10000;
  1063. /* Wait up to 10ms for the character(s) to be sent. */
  1064. do {
  1065. status = serial_in(up, UART_LSR);
  1066. if (status & UART_LSR_BI)
  1067. up->lsr_break_flag = UART_LSR_BI;
  1068. if (--tmout == 0)
  1069. break;
  1070. udelay(1);
  1071. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  1072. /* Wait up to 1s for flow control if necessary */
  1073. if (up->port.flags & UPF_CONS_FLOW) {
  1074. tmout = 1000000;
  1075. while (--tmout &&
  1076. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  1077. udelay(1);
  1078. }
  1079. }
  1080. static void sunsu_console_putchar(struct uart_port *port, int ch)
  1081. {
  1082. struct uart_sunsu_port *up = (struct uart_sunsu_port *)port;
  1083. wait_for_xmitr(up);
  1084. serial_out(up, UART_TX, ch);
  1085. }
  1086. /*
  1087. * Print a string to the serial port trying not to disturb
  1088. * any possible real use of the port...
  1089. */
  1090. static void sunsu_console_write(struct console *co, const char *s,
  1091. unsigned int count)
  1092. {
  1093. struct uart_sunsu_port *up = &sunsu_ports[co->index];
  1094. unsigned long flags;
  1095. unsigned int ier;
  1096. int locked = 1;
  1097. local_irq_save(flags);
  1098. if (up->port.sysrq) {
  1099. locked = 0;
  1100. } else if (oops_in_progress) {
  1101. locked = spin_trylock(&up->port.lock);
  1102. } else
  1103. spin_lock(&up->port.lock);
  1104. /*
  1105. * First save the UER then disable the interrupts
  1106. */
  1107. ier = serial_in(up, UART_IER);
  1108. serial_out(up, UART_IER, 0);
  1109. uart_console_write(&up->port, s, count, sunsu_console_putchar);
  1110. /*
  1111. * Finally, wait for transmitter to become empty
  1112. * and restore the IER
  1113. */
  1114. wait_for_xmitr(up);
  1115. serial_out(up, UART_IER, ier);
  1116. if (locked)
  1117. spin_unlock(&up->port.lock);
  1118. local_irq_restore(flags);
  1119. }
  1120. /*
  1121. * Setup initial baud/bits/parity. We do two things here:
  1122. * - construct a cflag setting for the first su_open()
  1123. * - initialize the serial port
  1124. * Return non-zero if we didn't find a serial port.
  1125. */
  1126. static int __init sunsu_console_setup(struct console *co, char *options)
  1127. {
  1128. static struct ktermios dummy;
  1129. struct ktermios termios;
  1130. struct uart_port *port;
  1131. printk("Console: ttyS%d (SU)\n",
  1132. (sunsu_reg.minor - 64) + co->index);
  1133. /*
  1134. * Check whether an invalid uart number has been specified, and
  1135. * if so, search for the first available port that does have
  1136. * console support.
  1137. */
  1138. if (co->index >= UART_NR)
  1139. co->index = 0;
  1140. port = &sunsu_ports[co->index].port;
  1141. /*
  1142. * Temporary fix.
  1143. */
  1144. spin_lock_init(&port->lock);
  1145. /* Get firmware console settings. */
  1146. sunserial_console_termios(co, to_of_device(port->dev)->node);
  1147. memset(&termios, 0, sizeof(struct ktermios));
  1148. termios.c_cflag = co->cflag;
  1149. port->mctrl |= TIOCM_DTR;
  1150. port->ops->set_termios(port, &termios, &dummy);
  1151. return 0;
  1152. }
  1153. static struct console sunsu_console = {
  1154. .name = "ttyS",
  1155. .write = sunsu_console_write,
  1156. .device = uart_console_device,
  1157. .setup = sunsu_console_setup,
  1158. .flags = CON_PRINTBUFFER,
  1159. .index = -1,
  1160. .data = &sunsu_reg,
  1161. };
  1162. /*
  1163. * Register console.
  1164. */
  1165. static inline struct console *SUNSU_CONSOLE(void)
  1166. {
  1167. return &sunsu_console;
  1168. }
  1169. #else
  1170. #define SUNSU_CONSOLE() (NULL)
  1171. #define sunsu_serial_console_init() do { } while (0)
  1172. #endif
  1173. static enum su_type __devinit su_get_type(struct device_node *dp)
  1174. {
  1175. struct device_node *ap = of_find_node_by_path("/aliases");
  1176. if (ap) {
  1177. const char *keyb = of_get_property(ap, "keyboard", NULL);
  1178. const char *ms = of_get_property(ap, "mouse", NULL);
  1179. if (keyb) {
  1180. if (dp == of_find_node_by_path(keyb))
  1181. return SU_PORT_KBD;
  1182. }
  1183. if (ms) {
  1184. if (dp == of_find_node_by_path(ms))
  1185. return SU_PORT_MS;
  1186. }
  1187. }
  1188. return SU_PORT_PORT;
  1189. }
  1190. static int __devinit su_probe(struct of_device *op, const struct of_device_id *match)
  1191. {
  1192. static int inst;
  1193. struct device_node *dp = op->node;
  1194. struct uart_sunsu_port *up;
  1195. struct resource *rp;
  1196. enum su_type type;
  1197. bool ignore_line;
  1198. int err;
  1199. type = su_get_type(dp);
  1200. if (type == SU_PORT_PORT) {
  1201. if (inst >= UART_NR)
  1202. return -EINVAL;
  1203. up = &sunsu_ports[inst];
  1204. } else {
  1205. up = kzalloc(sizeof(*up), GFP_KERNEL);
  1206. if (!up)
  1207. return -ENOMEM;
  1208. }
  1209. up->port.line = inst;
  1210. spin_lock_init(&up->port.lock);
  1211. up->su_type = type;
  1212. rp = &op->resource[0];
  1213. up->port.mapbase = rp->start;
  1214. up->reg_size = (rp->end - rp->start) + 1;
  1215. up->port.membase = of_ioremap(rp, 0, up->reg_size, "su");
  1216. if (!up->port.membase) {
  1217. if (type != SU_PORT_PORT)
  1218. kfree(up);
  1219. return -ENOMEM;
  1220. }
  1221. up->port.irq = op->irqs[0];
  1222. up->port.dev = &op->dev;
  1223. up->port.type = PORT_UNKNOWN;
  1224. up->port.uartclk = (SU_BASE_BAUD * 16);
  1225. err = 0;
  1226. if (up->su_type == SU_PORT_KBD || up->su_type == SU_PORT_MS) {
  1227. err = sunsu_kbd_ms_init(up);
  1228. if (err) {
  1229. of_iounmap(&op->resource[0],
  1230. up->port.membase, up->reg_size);
  1231. kfree(up);
  1232. return err;
  1233. }
  1234. dev_set_drvdata(&op->dev, up);
  1235. return 0;
  1236. }
  1237. up->port.flags |= UPF_BOOT_AUTOCONF;
  1238. sunsu_autoconfig(up);
  1239. err = -ENODEV;
  1240. if (up->port.type == PORT_UNKNOWN)
  1241. goto out_unmap;
  1242. up->port.ops = &sunsu_pops;
  1243. ignore_line = false;
  1244. if (!strcmp(dp->name, "rsc-console") ||
  1245. !strcmp(dp->name, "lom-console"))
  1246. ignore_line = true;
  1247. sunserial_console_match(SUNSU_CONSOLE(), dp,
  1248. &sunsu_reg, up->port.line,
  1249. ignore_line);
  1250. err = uart_add_one_port(&sunsu_reg, &up->port);
  1251. if (err)
  1252. goto out_unmap;
  1253. dev_set_drvdata(&op->dev, up);
  1254. inst++;
  1255. return 0;
  1256. out_unmap:
  1257. of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
  1258. return err;
  1259. }
  1260. static int __devexit su_remove(struct of_device *op)
  1261. {
  1262. struct uart_sunsu_port *up = dev_get_drvdata(&op->dev);
  1263. if (up->su_type == SU_PORT_MS ||
  1264. up->su_type == SU_PORT_KBD) {
  1265. #ifdef CONFIG_SERIO
  1266. serio_unregister_port(&up->serio);
  1267. #endif
  1268. kfree(up);
  1269. } else if (up->port.type != PORT_UNKNOWN) {
  1270. uart_remove_one_port(&sunsu_reg, &up->port);
  1271. }
  1272. if (up->port.membase)
  1273. of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
  1274. dev_set_drvdata(&op->dev, NULL);
  1275. return 0;
  1276. }
  1277. static const struct of_device_id su_match[] = {
  1278. {
  1279. .name = "su",
  1280. },
  1281. {
  1282. .name = "su_pnp",
  1283. },
  1284. {
  1285. .name = "serial",
  1286. .compatible = "su",
  1287. },
  1288. {
  1289. .type = "serial",
  1290. .compatible = "su",
  1291. },
  1292. {},
  1293. };
  1294. MODULE_DEVICE_TABLE(of, su_match);
  1295. static struct of_platform_driver su_driver = {
  1296. .name = "su",
  1297. .match_table = su_match,
  1298. .probe = su_probe,
  1299. .remove = __devexit_p(su_remove),
  1300. };
  1301. static int __init sunsu_init(void)
  1302. {
  1303. struct device_node *dp;
  1304. int err;
  1305. int num_uart = 0;
  1306. for_each_node_by_name(dp, "su") {
  1307. if (su_get_type(dp) == SU_PORT_PORT)
  1308. num_uart++;
  1309. }
  1310. for_each_node_by_name(dp, "su_pnp") {
  1311. if (su_get_type(dp) == SU_PORT_PORT)
  1312. num_uart++;
  1313. }
  1314. for_each_node_by_name(dp, "serial") {
  1315. if (of_device_is_compatible(dp, "su")) {
  1316. if (su_get_type(dp) == SU_PORT_PORT)
  1317. num_uart++;
  1318. }
  1319. }
  1320. for_each_node_by_type(dp, "serial") {
  1321. if (of_device_is_compatible(dp, "su")) {
  1322. if (su_get_type(dp) == SU_PORT_PORT)
  1323. num_uart++;
  1324. }
  1325. }
  1326. if (num_uart) {
  1327. err = sunserial_register_minors(&sunsu_reg, num_uart);
  1328. if (err)
  1329. return err;
  1330. }
  1331. err = of_register_driver(&su_driver, &of_bus_type);
  1332. if (err && num_uart)
  1333. sunserial_unregister_minors(&sunsu_reg, num_uart);
  1334. return err;
  1335. }
  1336. static void __exit sunsu_exit(void)
  1337. {
  1338. if (sunsu_reg.nr)
  1339. sunserial_unregister_minors(&sunsu_reg, sunsu_reg.nr);
  1340. }
  1341. module_init(sunsu_init);
  1342. module_exit(sunsu_exit);
  1343. MODULE_AUTHOR("Eddie C. Dost, Peter Zaitcev, and David S. Miller");
  1344. MODULE_DESCRIPTION("Sun SU serial port driver");
  1345. MODULE_VERSION("2.0");
  1346. MODULE_LICENSE("GPL");