vr41xx_siu.c 22 KB

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