sunsu.c 42 KB

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