sunsu.c 38 KB

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