sunsu.c 41 KB

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