vr41xx_siu.c 20 KB

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