sunsu.c 37 KB

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