vr41xx_siu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for NEC VR4100 series Serial Interface Unit.
  4. *
  5. * Copyright (C) 2004-2008 Yoichi Yuasa <yuasa@linux-mips.org>
  6. *
  7. * Based on drivers/serial/8250.c, by Russell King.
  8. */
  9. #if defined(CONFIG_SERIAL_VR41XX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  10. #define SUPPORT_SYSRQ
  11. #endif
  12. #include <linux/console.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/serial.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/serial_reg.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <asm/io.h>
  25. #include <asm/vr41xx/siu.h>
  26. #include <asm/vr41xx/vr41xx.h>
  27. #define SIU_BAUD_BASE 1152000
  28. #define SIU_MAJOR 204
  29. #define SIU_MINOR_BASE 82
  30. #define RX_MAX_COUNT 256
  31. #define TX_MAX_COUNT 15
  32. #define SIUIRSEL 0x08
  33. #define TMICMODE 0x20
  34. #define TMICTX 0x10
  35. #define IRMSEL 0x0c
  36. #define IRMSEL_HP 0x08
  37. #define IRMSEL_TEMIC 0x04
  38. #define IRMSEL_SHARP 0x00
  39. #define IRUSESEL 0x02
  40. #define SIRSEL 0x01
  41. static struct uart_port siu_uart_ports[SIU_PORTS_MAX] = {
  42. [0 ... SIU_PORTS_MAX-1] = {
  43. .lock = __SPIN_LOCK_UNLOCKED(siu_uart_ports->lock),
  44. .irq = 0,
  45. },
  46. };
  47. #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  48. static uint8_t lsr_break_flag[SIU_PORTS_MAX];
  49. #endif
  50. #define siu_read(port, offset) readb((port)->membase + (offset))
  51. #define siu_write(port, offset, value) writeb((value), (port)->membase + (offset))
  52. void vr41xx_select_siu_interface(siu_interface_t interface)
  53. {
  54. struct uart_port *port;
  55. unsigned long flags;
  56. uint8_t irsel;
  57. port = &siu_uart_ports[0];
  58. spin_lock_irqsave(&port->lock, flags);
  59. irsel = siu_read(port, SIUIRSEL);
  60. if (interface == SIU_INTERFACE_IRDA)
  61. irsel |= SIRSEL;
  62. else
  63. irsel &= ~SIRSEL;
  64. siu_write(port, SIUIRSEL, irsel);
  65. spin_unlock_irqrestore(&port->lock, flags);
  66. }
  67. EXPORT_SYMBOL_GPL(vr41xx_select_siu_interface);
  68. void vr41xx_use_irda(irda_use_t use)
  69. {
  70. struct uart_port *port;
  71. unsigned long flags;
  72. uint8_t irsel;
  73. port = &siu_uart_ports[0];
  74. spin_lock_irqsave(&port->lock, flags);
  75. irsel = siu_read(port, SIUIRSEL);
  76. if (use == FIR_USE_IRDA)
  77. irsel |= IRUSESEL;
  78. else
  79. irsel &= ~IRUSESEL;
  80. siu_write(port, SIUIRSEL, irsel);
  81. spin_unlock_irqrestore(&port->lock, flags);
  82. }
  83. EXPORT_SYMBOL_GPL(vr41xx_use_irda);
  84. void vr41xx_select_irda_module(irda_module_t module, irda_speed_t speed)
  85. {
  86. struct uart_port *port;
  87. unsigned long flags;
  88. uint8_t irsel;
  89. port = &siu_uart_ports[0];
  90. spin_lock_irqsave(&port->lock, flags);
  91. irsel = siu_read(port, SIUIRSEL);
  92. irsel &= ~(IRMSEL | TMICTX | TMICMODE);
  93. switch (module) {
  94. case SHARP_IRDA:
  95. irsel |= IRMSEL_SHARP;
  96. break;
  97. case TEMIC_IRDA:
  98. irsel |= IRMSEL_TEMIC | TMICMODE;
  99. if (speed == IRDA_TX_4MBPS)
  100. irsel |= TMICTX;
  101. break;
  102. case HP_IRDA:
  103. irsel |= IRMSEL_HP;
  104. break;
  105. default:
  106. break;
  107. }
  108. siu_write(port, SIUIRSEL, irsel);
  109. spin_unlock_irqrestore(&port->lock, flags);
  110. }
  111. EXPORT_SYMBOL_GPL(vr41xx_select_irda_module);
  112. static inline void siu_clear_fifo(struct uart_port *port)
  113. {
  114. siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  115. siu_write(port, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
  116. UART_FCR_CLEAR_XMIT);
  117. siu_write(port, UART_FCR, 0);
  118. }
  119. static inline unsigned long siu_port_size(struct uart_port *port)
  120. {
  121. switch (port->type) {
  122. case PORT_VR41XX_SIU:
  123. return 11UL;
  124. case PORT_VR41XX_DSIU:
  125. return 8UL;
  126. }
  127. return 0;
  128. }
  129. static inline unsigned int siu_check_type(struct uart_port *port)
  130. {
  131. if (port->line == 0)
  132. return PORT_VR41XX_SIU;
  133. if (port->line == 1 && port->irq)
  134. return PORT_VR41XX_DSIU;
  135. return PORT_UNKNOWN;
  136. }
  137. static inline const char *siu_type_name(struct uart_port *port)
  138. {
  139. switch (port->type) {
  140. case PORT_VR41XX_SIU:
  141. return "SIU";
  142. case PORT_VR41XX_DSIU:
  143. return "DSIU";
  144. }
  145. return NULL;
  146. }
  147. static unsigned int siu_tx_empty(struct uart_port *port)
  148. {
  149. uint8_t lsr;
  150. lsr = siu_read(port, UART_LSR);
  151. if (lsr & UART_LSR_TEMT)
  152. return TIOCSER_TEMT;
  153. return 0;
  154. }
  155. static void siu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  156. {
  157. uint8_t mcr = 0;
  158. if (mctrl & TIOCM_DTR)
  159. mcr |= UART_MCR_DTR;
  160. if (mctrl & TIOCM_RTS)
  161. mcr |= UART_MCR_RTS;
  162. if (mctrl & TIOCM_OUT1)
  163. mcr |= UART_MCR_OUT1;
  164. if (mctrl & TIOCM_OUT2)
  165. mcr |= UART_MCR_OUT2;
  166. if (mctrl & TIOCM_LOOP)
  167. mcr |= UART_MCR_LOOP;
  168. siu_write(port, UART_MCR, mcr);
  169. }
  170. static unsigned int siu_get_mctrl(struct uart_port *port)
  171. {
  172. uint8_t msr;
  173. unsigned int mctrl = 0;
  174. msr = siu_read(port, UART_MSR);
  175. if (msr & UART_MSR_DCD)
  176. mctrl |= TIOCM_CAR;
  177. if (msr & UART_MSR_RI)
  178. mctrl |= TIOCM_RNG;
  179. if (msr & UART_MSR_DSR)
  180. mctrl |= TIOCM_DSR;
  181. if (msr & UART_MSR_CTS)
  182. mctrl |= TIOCM_CTS;
  183. return mctrl;
  184. }
  185. static void siu_stop_tx(struct uart_port *port)
  186. {
  187. unsigned long flags;
  188. uint8_t ier;
  189. spin_lock_irqsave(&port->lock, flags);
  190. ier = siu_read(port, UART_IER);
  191. ier &= ~UART_IER_THRI;
  192. siu_write(port, UART_IER, ier);
  193. spin_unlock_irqrestore(&port->lock, flags);
  194. }
  195. static void siu_start_tx(struct uart_port *port)
  196. {
  197. unsigned long flags;
  198. uint8_t ier;
  199. spin_lock_irqsave(&port->lock, flags);
  200. ier = siu_read(port, UART_IER);
  201. ier |= UART_IER_THRI;
  202. siu_write(port, UART_IER, ier);
  203. spin_unlock_irqrestore(&port->lock, flags);
  204. }
  205. static void siu_stop_rx(struct uart_port *port)
  206. {
  207. unsigned long flags;
  208. uint8_t ier;
  209. spin_lock_irqsave(&port->lock, flags);
  210. ier = siu_read(port, UART_IER);
  211. ier &= ~UART_IER_RLSI;
  212. siu_write(port, UART_IER, ier);
  213. port->read_status_mask &= ~UART_LSR_DR;
  214. spin_unlock_irqrestore(&port->lock, flags);
  215. }
  216. static void siu_enable_ms(struct uart_port *port)
  217. {
  218. unsigned long flags;
  219. uint8_t ier;
  220. spin_lock_irqsave(&port->lock, flags);
  221. ier = siu_read(port, UART_IER);
  222. ier |= UART_IER_MSI;
  223. siu_write(port, UART_IER, ier);
  224. spin_unlock_irqrestore(&port->lock, flags);
  225. }
  226. static void siu_break_ctl(struct uart_port *port, int ctl)
  227. {
  228. unsigned long flags;
  229. uint8_t lcr;
  230. spin_lock_irqsave(&port->lock, flags);
  231. lcr = siu_read(port, UART_LCR);
  232. if (ctl == -1)
  233. lcr |= UART_LCR_SBC;
  234. else
  235. lcr &= ~UART_LCR_SBC;
  236. siu_write(port, UART_LCR, lcr);
  237. spin_unlock_irqrestore(&port->lock, flags);
  238. }
  239. static inline void receive_chars(struct uart_port *port, uint8_t *status)
  240. {
  241. uint8_t lsr, ch;
  242. char flag;
  243. int max_count = RX_MAX_COUNT;
  244. lsr = *status;
  245. do {
  246. ch = siu_read(port, UART_RX);
  247. port->icount.rx++;
  248. flag = TTY_NORMAL;
  249. #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  250. lsr |= lsr_break_flag[port->line];
  251. lsr_break_flag[port->line] = 0;
  252. #endif
  253. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
  254. UART_LSR_PE | UART_LSR_OE))) {
  255. if (lsr & UART_LSR_BI) {
  256. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  257. port->icount.brk++;
  258. if (uart_handle_break(port))
  259. goto ignore_char;
  260. }
  261. if (lsr & UART_LSR_FE)
  262. port->icount.frame++;
  263. if (lsr & UART_LSR_PE)
  264. port->icount.parity++;
  265. if (lsr & UART_LSR_OE)
  266. port->icount.overrun++;
  267. lsr &= port->read_status_mask;
  268. if (lsr & UART_LSR_BI)
  269. flag = TTY_BREAK;
  270. if (lsr & UART_LSR_FE)
  271. flag = TTY_FRAME;
  272. if (lsr & UART_LSR_PE)
  273. flag = TTY_PARITY;
  274. }
  275. if (uart_handle_sysrq_char(port, ch))
  276. goto ignore_char;
  277. uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
  278. ignore_char:
  279. lsr = siu_read(port, UART_LSR);
  280. } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
  281. tty_flip_buffer_push(&port->state->port);
  282. *status = lsr;
  283. }
  284. static inline void check_modem_status(struct uart_port *port)
  285. {
  286. uint8_t msr;
  287. msr = siu_read(port, UART_MSR);
  288. if ((msr & UART_MSR_ANY_DELTA) == 0)
  289. return;
  290. if (msr & UART_MSR_DDCD)
  291. uart_handle_dcd_change(port, msr & UART_MSR_DCD);
  292. if (msr & UART_MSR_TERI)
  293. port->icount.rng++;
  294. if (msr & UART_MSR_DDSR)
  295. port->icount.dsr++;
  296. if (msr & UART_MSR_DCTS)
  297. uart_handle_cts_change(port, msr & UART_MSR_CTS);
  298. wake_up_interruptible(&port->state->port.delta_msr_wait);
  299. }
  300. static inline void transmit_chars(struct uart_port *port)
  301. {
  302. struct circ_buf *xmit;
  303. int max_count = TX_MAX_COUNT;
  304. xmit = &port->state->xmit;
  305. if (port->x_char) {
  306. siu_write(port, UART_TX, port->x_char);
  307. port->icount.tx++;
  308. port->x_char = 0;
  309. return;
  310. }
  311. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  312. siu_stop_tx(port);
  313. return;
  314. }
  315. do {
  316. siu_write(port, UART_TX, xmit->buf[xmit->tail]);
  317. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  318. port->icount.tx++;
  319. if (uart_circ_empty(xmit))
  320. break;
  321. } while (max_count-- > 0);
  322. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  323. uart_write_wakeup(port);
  324. if (uart_circ_empty(xmit))
  325. siu_stop_tx(port);
  326. }
  327. static irqreturn_t siu_interrupt(int irq, void *dev_id)
  328. {
  329. struct uart_port *port;
  330. uint8_t iir, lsr;
  331. port = (struct uart_port *)dev_id;
  332. iir = siu_read(port, UART_IIR);
  333. if (iir & UART_IIR_NO_INT)
  334. return IRQ_NONE;
  335. lsr = siu_read(port, UART_LSR);
  336. if (lsr & UART_LSR_DR)
  337. receive_chars(port, &lsr);
  338. check_modem_status(port);
  339. if (lsr & UART_LSR_THRE)
  340. transmit_chars(port);
  341. return IRQ_HANDLED;
  342. }
  343. static int siu_startup(struct uart_port *port)
  344. {
  345. int retval;
  346. if (port->membase == NULL)
  347. return -ENODEV;
  348. siu_clear_fifo(port);
  349. (void)siu_read(port, UART_LSR);
  350. (void)siu_read(port, UART_RX);
  351. (void)siu_read(port, UART_IIR);
  352. (void)siu_read(port, UART_MSR);
  353. if (siu_read(port, UART_LSR) == 0xff)
  354. return -ENODEV;
  355. retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
  356. if (retval)
  357. return retval;
  358. if (port->type == PORT_VR41XX_DSIU)
  359. vr41xx_enable_dsiuint(DSIUINT_ALL);
  360. siu_write(port, UART_LCR, UART_LCR_WLEN8);
  361. spin_lock_irq(&port->lock);
  362. siu_set_mctrl(port, port->mctrl);
  363. spin_unlock_irq(&port->lock);
  364. siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
  365. (void)siu_read(port, UART_LSR);
  366. (void)siu_read(port, UART_RX);
  367. (void)siu_read(port, UART_IIR);
  368. (void)siu_read(port, UART_MSR);
  369. return 0;
  370. }
  371. static void siu_shutdown(struct uart_port *port)
  372. {
  373. unsigned long flags;
  374. uint8_t lcr;
  375. siu_write(port, UART_IER, 0);
  376. spin_lock_irqsave(&port->lock, flags);
  377. port->mctrl &= ~TIOCM_OUT2;
  378. siu_set_mctrl(port, port->mctrl);
  379. spin_unlock_irqrestore(&port->lock, flags);
  380. lcr = siu_read(port, UART_LCR);
  381. lcr &= ~UART_LCR_SBC;
  382. siu_write(port, UART_LCR, lcr);
  383. siu_clear_fifo(port);
  384. (void)siu_read(port, UART_RX);
  385. if (port->type == PORT_VR41XX_DSIU)
  386. vr41xx_disable_dsiuint(DSIUINT_ALL);
  387. free_irq(port->irq, port);
  388. }
  389. static void siu_set_termios(struct uart_port *port, struct ktermios *new,
  390. struct ktermios *old)
  391. {
  392. tcflag_t c_cflag, c_iflag;
  393. uint8_t lcr, fcr, ier;
  394. unsigned int baud, quot;
  395. unsigned long flags;
  396. c_cflag = new->c_cflag;
  397. switch (c_cflag & CSIZE) {
  398. case CS5:
  399. lcr = UART_LCR_WLEN5;
  400. break;
  401. case CS6:
  402. lcr = UART_LCR_WLEN6;
  403. break;
  404. case CS7:
  405. lcr = UART_LCR_WLEN7;
  406. break;
  407. default:
  408. lcr = UART_LCR_WLEN8;
  409. break;
  410. }
  411. if (c_cflag & CSTOPB)
  412. lcr |= UART_LCR_STOP;
  413. if (c_cflag & PARENB)
  414. lcr |= UART_LCR_PARITY;
  415. if ((c_cflag & PARODD) != PARODD)
  416. lcr |= UART_LCR_EPAR;
  417. if (c_cflag & CMSPAR)
  418. lcr |= UART_LCR_SPAR;
  419. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  420. quot = uart_get_divisor(port, baud);
  421. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  422. spin_lock_irqsave(&port->lock, flags);
  423. uart_update_timeout(port, c_cflag, baud);
  424. c_iflag = new->c_iflag;
  425. port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
  426. if (c_iflag & INPCK)
  427. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  428. if (c_iflag & (IGNBRK | BRKINT | PARMRK))
  429. port->read_status_mask |= UART_LSR_BI;
  430. port->ignore_status_mask = 0;
  431. if (c_iflag & IGNPAR)
  432. port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
  433. if (c_iflag & IGNBRK) {
  434. port->ignore_status_mask |= UART_LSR_BI;
  435. if (c_iflag & IGNPAR)
  436. port->ignore_status_mask |= UART_LSR_OE;
  437. }
  438. if ((c_cflag & CREAD) == 0)
  439. port->ignore_status_mask |= UART_LSR_DR;
  440. ier = siu_read(port, UART_IER);
  441. ier &= ~UART_IER_MSI;
  442. if (UART_ENABLE_MS(port, c_cflag))
  443. ier |= UART_IER_MSI;
  444. siu_write(port, UART_IER, ier);
  445. siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
  446. siu_write(port, UART_DLL, (uint8_t)quot);
  447. siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
  448. siu_write(port, UART_LCR, lcr);
  449. siu_write(port, UART_FCR, fcr);
  450. siu_set_mctrl(port, port->mctrl);
  451. spin_unlock_irqrestore(&port->lock, flags);
  452. }
  453. static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  454. {
  455. switch (state) {
  456. case 0:
  457. switch (port->type) {
  458. case PORT_VR41XX_SIU:
  459. vr41xx_supply_clock(SIU_CLOCK);
  460. break;
  461. case PORT_VR41XX_DSIU:
  462. vr41xx_supply_clock(DSIU_CLOCK);
  463. break;
  464. }
  465. break;
  466. case 3:
  467. switch (port->type) {
  468. case PORT_VR41XX_SIU:
  469. vr41xx_mask_clock(SIU_CLOCK);
  470. break;
  471. case PORT_VR41XX_DSIU:
  472. vr41xx_mask_clock(DSIU_CLOCK);
  473. break;
  474. }
  475. break;
  476. }
  477. }
  478. static const char *siu_type(struct uart_port *port)
  479. {
  480. return siu_type_name(port);
  481. }
  482. static void siu_release_port(struct uart_port *port)
  483. {
  484. unsigned long size;
  485. if (port->flags & UPF_IOREMAP) {
  486. iounmap(port->membase);
  487. port->membase = NULL;
  488. }
  489. size = siu_port_size(port);
  490. release_mem_region(port->mapbase, size);
  491. }
  492. static int siu_request_port(struct uart_port *port)
  493. {
  494. unsigned long size;
  495. struct resource *res;
  496. size = siu_port_size(port);
  497. res = request_mem_region(port->mapbase, size, siu_type_name(port));
  498. if (res == NULL)
  499. return -EBUSY;
  500. if (port->flags & UPF_IOREMAP) {
  501. port->membase = ioremap(port->mapbase, size);
  502. if (port->membase == NULL) {
  503. release_resource(res);
  504. return -ENOMEM;
  505. }
  506. }
  507. return 0;
  508. }
  509. static void siu_config_port(struct uart_port *port, int flags)
  510. {
  511. if (flags & UART_CONFIG_TYPE) {
  512. port->type = siu_check_type(port);
  513. (void)siu_request_port(port);
  514. }
  515. }
  516. static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
  517. {
  518. if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
  519. return -EINVAL;
  520. if (port->irq != serial->irq)
  521. return -EINVAL;
  522. if (port->iotype != serial->io_type)
  523. return -EINVAL;
  524. if (port->mapbase != (unsigned long)serial->iomem_base)
  525. return -EINVAL;
  526. return 0;
  527. }
  528. static const struct uart_ops siu_uart_ops = {
  529. .tx_empty = siu_tx_empty,
  530. .set_mctrl = siu_set_mctrl,
  531. .get_mctrl = siu_get_mctrl,
  532. .stop_tx = siu_stop_tx,
  533. .start_tx = siu_start_tx,
  534. .stop_rx = siu_stop_rx,
  535. .enable_ms = siu_enable_ms,
  536. .break_ctl = siu_break_ctl,
  537. .startup = siu_startup,
  538. .shutdown = siu_shutdown,
  539. .set_termios = siu_set_termios,
  540. .pm = siu_pm,
  541. .type = siu_type,
  542. .release_port = siu_release_port,
  543. .request_port = siu_request_port,
  544. .config_port = siu_config_port,
  545. .verify_port = siu_verify_port,
  546. };
  547. static int siu_init_ports(struct platform_device *pdev)
  548. {
  549. struct uart_port *port;
  550. struct resource *res;
  551. int *type = dev_get_platdata(&pdev->dev);
  552. int i;
  553. if (!type)
  554. return 0;
  555. port = siu_uart_ports;
  556. for (i = 0; i < SIU_PORTS_MAX; i++) {
  557. port->type = type[i];
  558. if (port->type == PORT_UNKNOWN)
  559. continue;
  560. port->irq = platform_get_irq(pdev, i);
  561. port->uartclk = SIU_BAUD_BASE * 16;
  562. port->fifosize = 16;
  563. port->regshift = 0;
  564. port->iotype = UPIO_MEM;
  565. port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  566. port->line = i;
  567. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  568. port->mapbase = res->start;
  569. port++;
  570. }
  571. return i;
  572. }
  573. #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  574. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  575. static void wait_for_xmitr(struct uart_port *port)
  576. {
  577. int timeout = 10000;
  578. uint8_t lsr, msr;
  579. do {
  580. lsr = siu_read(port, UART_LSR);
  581. if (lsr & UART_LSR_BI)
  582. lsr_break_flag[port->line] = UART_LSR_BI;
  583. if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
  584. break;
  585. } while (timeout-- > 0);
  586. if (port->flags & UPF_CONS_FLOW) {
  587. timeout = 1000000;
  588. do {
  589. msr = siu_read(port, UART_MSR);
  590. if ((msr & UART_MSR_CTS) != 0)
  591. break;
  592. } while (timeout-- > 0);
  593. }
  594. }
  595. static void siu_console_putchar(struct uart_port *port, int ch)
  596. {
  597. wait_for_xmitr(port);
  598. siu_write(port, UART_TX, ch);
  599. }
  600. static void siu_console_write(struct console *con, const char *s, unsigned count)
  601. {
  602. struct uart_port *port;
  603. uint8_t ier;
  604. port = &siu_uart_ports[con->index];
  605. ier = siu_read(port, UART_IER);
  606. siu_write(port, UART_IER, 0);
  607. uart_console_write(port, s, count, siu_console_putchar);
  608. wait_for_xmitr(port);
  609. siu_write(port, UART_IER, ier);
  610. }
  611. static int __init siu_console_setup(struct console *con, char *options)
  612. {
  613. struct uart_port *port;
  614. int baud = 9600;
  615. int parity = 'n';
  616. int bits = 8;
  617. int flow = 'n';
  618. if (con->index >= SIU_PORTS_MAX)
  619. con->index = 0;
  620. port = &siu_uart_ports[con->index];
  621. if (port->membase == NULL) {
  622. if (port->mapbase == 0)
  623. return -ENODEV;
  624. port->membase = ioremap(port->mapbase, siu_port_size(port));
  625. }
  626. if (port->type == PORT_VR41XX_SIU)
  627. vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
  628. if (options != NULL)
  629. uart_parse_options(options, &baud, &parity, &bits, &flow);
  630. return uart_set_options(port, con, baud, parity, bits, flow);
  631. }
  632. static struct uart_driver siu_uart_driver;
  633. static struct console siu_console = {
  634. .name = "ttyVR",
  635. .write = siu_console_write,
  636. .device = uart_console_device,
  637. .setup = siu_console_setup,
  638. .flags = CON_PRINTBUFFER,
  639. .index = -1,
  640. .data = &siu_uart_driver,
  641. };
  642. static int siu_console_init(void)
  643. {
  644. struct uart_port *port;
  645. int i;
  646. for (i = 0; i < SIU_PORTS_MAX; i++) {
  647. port = &siu_uart_ports[i];
  648. port->ops = &siu_uart_ops;
  649. }
  650. register_console(&siu_console);
  651. return 0;
  652. }
  653. console_initcall(siu_console_init);
  654. void __init vr41xx_siu_early_setup(struct uart_port *port)
  655. {
  656. if (port->type == PORT_UNKNOWN)
  657. return;
  658. siu_uart_ports[port->line].line = port->line;
  659. siu_uart_ports[port->line].type = port->type;
  660. siu_uart_ports[port->line].uartclk = SIU_BAUD_BASE * 16;
  661. siu_uart_ports[port->line].mapbase = port->mapbase;
  662. siu_uart_ports[port->line].ops = &siu_uart_ops;
  663. }
  664. #define SERIAL_VR41XX_CONSOLE &siu_console
  665. #else
  666. #define SERIAL_VR41XX_CONSOLE NULL
  667. #endif
  668. static struct uart_driver siu_uart_driver = {
  669. .owner = THIS_MODULE,
  670. .driver_name = "SIU",
  671. .dev_name = "ttyVR",
  672. .major = SIU_MAJOR,
  673. .minor = SIU_MINOR_BASE,
  674. .cons = SERIAL_VR41XX_CONSOLE,
  675. };
  676. static int siu_probe(struct platform_device *dev)
  677. {
  678. struct uart_port *port;
  679. int num, i, retval;
  680. num = siu_init_ports(dev);
  681. if (num <= 0)
  682. return -ENODEV;
  683. siu_uart_driver.nr = num;
  684. retval = uart_register_driver(&siu_uart_driver);
  685. if (retval)
  686. return retval;
  687. for (i = 0; i < num; i++) {
  688. port = &siu_uart_ports[i];
  689. port->ops = &siu_uart_ops;
  690. port->dev = &dev->dev;
  691. retval = uart_add_one_port(&siu_uart_driver, port);
  692. if (retval < 0) {
  693. port->dev = NULL;
  694. break;
  695. }
  696. }
  697. if (i == 0 && retval < 0) {
  698. uart_unregister_driver(&siu_uart_driver);
  699. return retval;
  700. }
  701. return 0;
  702. }
  703. static int siu_remove(struct platform_device *dev)
  704. {
  705. struct uart_port *port;
  706. int i;
  707. for (i = 0; i < siu_uart_driver.nr; i++) {
  708. port = &siu_uart_ports[i];
  709. if (port->dev == &dev->dev) {
  710. uart_remove_one_port(&siu_uart_driver, port);
  711. port->dev = NULL;
  712. }
  713. }
  714. uart_unregister_driver(&siu_uart_driver);
  715. return 0;
  716. }
  717. static int siu_suspend(struct platform_device *dev, pm_message_t state)
  718. {
  719. struct uart_port *port;
  720. int i;
  721. for (i = 0; i < siu_uart_driver.nr; i++) {
  722. port = &siu_uart_ports[i];
  723. if ((port->type == PORT_VR41XX_SIU ||
  724. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  725. uart_suspend_port(&siu_uart_driver, port);
  726. }
  727. return 0;
  728. }
  729. static int siu_resume(struct platform_device *dev)
  730. {
  731. struct uart_port *port;
  732. int i;
  733. for (i = 0; i < siu_uart_driver.nr; i++) {
  734. port = &siu_uart_ports[i];
  735. if ((port->type == PORT_VR41XX_SIU ||
  736. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  737. uart_resume_port(&siu_uart_driver, port);
  738. }
  739. return 0;
  740. }
  741. static struct platform_driver siu_device_driver = {
  742. .probe = siu_probe,
  743. .remove = siu_remove,
  744. .suspend = siu_suspend,
  745. .resume = siu_resume,
  746. .driver = {
  747. .name = "SIU",
  748. },
  749. };
  750. module_platform_driver(siu_device_driver);
  751. MODULE_LICENSE("GPL");
  752. MODULE_ALIAS("platform:SIU");