sunsu.c 38 KB

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