vr41xx_siu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Driver for NEC VR4100 series Serial Interface Unit.
  3. *
  4. * Copyright (C) 2004-2005 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. #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/platform_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. ch = siu_read(port, UART_RX);
  302. port->icount.rx++;
  303. flag = TTY_NORMAL;
  304. #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  305. lsr |= lsr_break_flag[port->line];
  306. lsr_break_flag[port->line] = 0;
  307. #endif
  308. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_FE |
  309. UART_LSR_PE | UART_LSR_OE))) {
  310. if (lsr & UART_LSR_BI) {
  311. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  312. port->icount.brk++;
  313. if (uart_handle_break(port))
  314. goto ignore_char;
  315. }
  316. if (lsr & UART_LSR_FE)
  317. port->icount.frame++;
  318. if (lsr & UART_LSR_PE)
  319. port->icount.parity++;
  320. if (lsr & UART_LSR_OE)
  321. port->icount.overrun++;
  322. lsr &= port->read_status_mask;
  323. if (lsr & UART_LSR_BI)
  324. flag = TTY_BREAK;
  325. if (lsr & UART_LSR_FE)
  326. flag = TTY_FRAME;
  327. if (lsr & UART_LSR_PE)
  328. flag = TTY_PARITY;
  329. }
  330. if (uart_handle_sysrq_char(port, ch, regs))
  331. goto ignore_char;
  332. uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
  333. ignore_char:
  334. lsr = siu_read(port, UART_LSR);
  335. } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
  336. tty_flip_buffer_push(tty);
  337. *status = lsr;
  338. }
  339. static inline void check_modem_status(struct uart_port *port)
  340. {
  341. uint8_t msr;
  342. msr = siu_read(port, UART_MSR);
  343. if ((msr & UART_MSR_ANY_DELTA) == 0)
  344. return;
  345. if (msr & UART_MSR_DDCD)
  346. uart_handle_dcd_change(port, msr & UART_MSR_DCD);
  347. if (msr & UART_MSR_TERI)
  348. port->icount.rng++;
  349. if (msr & UART_MSR_DDSR)
  350. port->icount.dsr++;
  351. if (msr & UART_MSR_DCTS)
  352. uart_handle_cts_change(port, msr & UART_MSR_CTS);
  353. wake_up_interruptible(&port->info->delta_msr_wait);
  354. }
  355. static inline void transmit_chars(struct uart_port *port)
  356. {
  357. struct circ_buf *xmit;
  358. int max_count = TX_MAX_COUNT;
  359. xmit = &port->info->xmit;
  360. if (port->x_char) {
  361. siu_write(port, UART_TX, port->x_char);
  362. port->icount.tx++;
  363. port->x_char = 0;
  364. return;
  365. }
  366. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  367. siu_stop_tx(port);
  368. return;
  369. }
  370. do {
  371. siu_write(port, UART_TX, xmit->buf[xmit->tail]);
  372. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  373. port->icount.tx++;
  374. if (uart_circ_empty(xmit))
  375. break;
  376. } while (max_count-- > 0);
  377. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  378. uart_write_wakeup(port);
  379. if (uart_circ_empty(xmit))
  380. siu_stop_tx(port);
  381. }
  382. static irqreturn_t siu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  383. {
  384. struct uart_port *port;
  385. uint8_t iir, lsr;
  386. port = (struct uart_port *)dev_id;
  387. iir = siu_read(port, UART_IIR);
  388. if (iir & UART_IIR_NO_INT)
  389. return IRQ_NONE;
  390. lsr = siu_read(port, UART_LSR);
  391. if (lsr & UART_LSR_DR)
  392. receive_chars(port, &lsr, regs);
  393. check_modem_status(port);
  394. if (lsr & UART_LSR_THRE)
  395. transmit_chars(port);
  396. return IRQ_HANDLED;
  397. }
  398. static int siu_startup(struct uart_port *port)
  399. {
  400. int retval;
  401. if (port->membase == NULL)
  402. return -ENODEV;
  403. siu_clear_fifo(port);
  404. (void)siu_read(port, UART_LSR);
  405. (void)siu_read(port, UART_RX);
  406. (void)siu_read(port, UART_IIR);
  407. (void)siu_read(port, UART_MSR);
  408. if (siu_read(port, UART_LSR) == 0xff)
  409. return -ENODEV;
  410. retval = request_irq(port->irq, siu_interrupt, 0, siu_type_name(port), port);
  411. if (retval)
  412. return retval;
  413. if (port->type == PORT_VR41XX_DSIU)
  414. vr41xx_enable_dsiuint(DSIUINT_ALL);
  415. siu_write(port, UART_LCR, UART_LCR_WLEN8);
  416. spin_lock_irq(&port->lock);
  417. siu_set_mctrl(port, port->mctrl);
  418. spin_unlock_irq(&port->lock);
  419. siu_write(port, UART_IER, UART_IER_RLSI | UART_IER_RDI);
  420. (void)siu_read(port, UART_LSR);
  421. (void)siu_read(port, UART_RX);
  422. (void)siu_read(port, UART_IIR);
  423. (void)siu_read(port, UART_MSR);
  424. return 0;
  425. }
  426. static void siu_shutdown(struct uart_port *port)
  427. {
  428. unsigned long flags;
  429. uint8_t lcr;
  430. siu_write(port, UART_IER, 0);
  431. spin_lock_irqsave(&port->lock, flags);
  432. port->mctrl &= ~TIOCM_OUT2;
  433. siu_set_mctrl(port, port->mctrl);
  434. spin_unlock_irqrestore(&port->lock, flags);
  435. lcr = siu_read(port, UART_LCR);
  436. lcr &= ~UART_LCR_SBC;
  437. siu_write(port, UART_LCR, lcr);
  438. siu_clear_fifo(port);
  439. (void)siu_read(port, UART_RX);
  440. if (port->type == PORT_VR41XX_DSIU)
  441. vr41xx_disable_dsiuint(DSIUINT_ALL);
  442. free_irq(port->irq, port);
  443. }
  444. static void siu_set_termios(struct uart_port *port, struct termios *new,
  445. struct termios *old)
  446. {
  447. tcflag_t c_cflag, c_iflag;
  448. uint8_t lcr, fcr, ier;
  449. unsigned int baud, quot;
  450. unsigned long flags;
  451. c_cflag = new->c_cflag;
  452. switch (c_cflag & CSIZE) {
  453. case CS5:
  454. lcr = UART_LCR_WLEN5;
  455. break;
  456. case CS6:
  457. lcr = UART_LCR_WLEN6;
  458. break;
  459. case CS7:
  460. lcr = UART_LCR_WLEN7;
  461. break;
  462. default:
  463. lcr = UART_LCR_WLEN8;
  464. break;
  465. }
  466. if (c_cflag & CSTOPB)
  467. lcr |= UART_LCR_STOP;
  468. if (c_cflag & PARENB)
  469. lcr |= UART_LCR_PARITY;
  470. if ((c_cflag & PARODD) != PARODD)
  471. lcr |= UART_LCR_EPAR;
  472. if (c_cflag & CMSPAR)
  473. lcr |= UART_LCR_SPAR;
  474. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  475. quot = uart_get_divisor(port, baud);
  476. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  477. spin_lock_irqsave(&port->lock, flags);
  478. uart_update_timeout(port, c_cflag, baud);
  479. c_iflag = new->c_iflag;
  480. port->read_status_mask = UART_LSR_THRE | UART_LSR_OE | UART_LSR_DR;
  481. if (c_iflag & INPCK)
  482. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  483. if (c_iflag & (BRKINT | PARMRK))
  484. port->read_status_mask |= UART_LSR_BI;
  485. port->ignore_status_mask = 0;
  486. if (c_iflag & IGNPAR)
  487. port->ignore_status_mask |= UART_LSR_FE | UART_LSR_PE;
  488. if (c_iflag & IGNBRK) {
  489. port->ignore_status_mask |= UART_LSR_BI;
  490. if (c_iflag & IGNPAR)
  491. port->ignore_status_mask |= UART_LSR_OE;
  492. }
  493. if ((c_cflag & CREAD) == 0)
  494. port->ignore_status_mask |= UART_LSR_DR;
  495. ier = siu_read(port, UART_IER);
  496. ier &= ~UART_IER_MSI;
  497. if (UART_ENABLE_MS(port, c_cflag))
  498. ier |= UART_IER_MSI;
  499. siu_write(port, UART_IER, ier);
  500. siu_write(port, UART_LCR, lcr | UART_LCR_DLAB);
  501. siu_write(port, UART_DLL, (uint8_t)quot);
  502. siu_write(port, UART_DLM, (uint8_t)(quot >> 8));
  503. siu_write(port, UART_LCR, lcr);
  504. siu_write(port, UART_FCR, fcr);
  505. siu_set_mctrl(port, port->mctrl);
  506. spin_unlock_irqrestore(&port->lock, flags);
  507. }
  508. static void siu_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  509. {
  510. switch (state) {
  511. case 0:
  512. switch (port->type) {
  513. case PORT_VR41XX_SIU:
  514. vr41xx_supply_clock(SIU_CLOCK);
  515. break;
  516. case PORT_VR41XX_DSIU:
  517. vr41xx_supply_clock(DSIU_CLOCK);
  518. break;
  519. }
  520. break;
  521. case 3:
  522. switch (port->type) {
  523. case PORT_VR41XX_SIU:
  524. vr41xx_mask_clock(SIU_CLOCK);
  525. break;
  526. case PORT_VR41XX_DSIU:
  527. vr41xx_mask_clock(DSIU_CLOCK);
  528. break;
  529. }
  530. break;
  531. }
  532. }
  533. static const char *siu_type(struct uart_port *port)
  534. {
  535. return siu_type_name(port);
  536. }
  537. static void siu_release_port(struct uart_port *port)
  538. {
  539. unsigned long size;
  540. if (port->flags & UPF_IOREMAP) {
  541. iounmap(port->membase);
  542. port->membase = NULL;
  543. }
  544. size = siu_port_size(port);
  545. release_mem_region(port->mapbase, size);
  546. }
  547. static int siu_request_port(struct uart_port *port)
  548. {
  549. unsigned long size;
  550. struct resource *res;
  551. size = siu_port_size(port);
  552. res = request_mem_region(port->mapbase, size, siu_type_name(port));
  553. if (res == NULL)
  554. return -EBUSY;
  555. if (port->flags & UPF_IOREMAP) {
  556. port->membase = ioremap(port->mapbase, size);
  557. if (port->membase == NULL) {
  558. release_resource(res);
  559. return -ENOMEM;
  560. }
  561. }
  562. return 0;
  563. }
  564. static void siu_config_port(struct uart_port *port, int flags)
  565. {
  566. if (flags & UART_CONFIG_TYPE) {
  567. port->type = siu_check_type(port);
  568. (void)siu_request_port(port);
  569. }
  570. }
  571. static int siu_verify_port(struct uart_port *port, struct serial_struct *serial)
  572. {
  573. if (port->type != PORT_VR41XX_SIU && port->type != PORT_VR41XX_DSIU)
  574. return -EINVAL;
  575. if (port->irq != serial->irq)
  576. return -EINVAL;
  577. if (port->iotype != serial->io_type)
  578. return -EINVAL;
  579. if (port->mapbase != (unsigned long)serial->iomem_base)
  580. return -EINVAL;
  581. return 0;
  582. }
  583. static struct uart_ops siu_uart_ops = {
  584. .tx_empty = siu_tx_empty,
  585. .set_mctrl = siu_set_mctrl,
  586. .get_mctrl = siu_get_mctrl,
  587. .stop_tx = siu_stop_tx,
  588. .start_tx = siu_start_tx,
  589. .stop_rx = siu_stop_rx,
  590. .enable_ms = siu_enable_ms,
  591. .break_ctl = siu_break_ctl,
  592. .startup = siu_startup,
  593. .shutdown = siu_shutdown,
  594. .set_termios = siu_set_termios,
  595. .pm = siu_pm,
  596. .type = siu_type,
  597. .release_port = siu_release_port,
  598. .request_port = siu_request_port,
  599. .config_port = siu_config_port,
  600. .verify_port = siu_verify_port,
  601. };
  602. static int siu_init_ports(void)
  603. {
  604. const struct siu_port *siu;
  605. struct uart_port *port;
  606. int i, num;
  607. switch (current_cpu_data.cputype) {
  608. case CPU_VR4111:
  609. case CPU_VR4121:
  610. siu = siu_type1_ports;
  611. break;
  612. case CPU_VR4122:
  613. case CPU_VR4131:
  614. case CPU_VR4133:
  615. siu = siu_type2_ports;
  616. break;
  617. default:
  618. return 0;
  619. }
  620. port = siu_uart_ports;
  621. num = siu_probe_ports();
  622. for (i = 0; i < num; i++) {
  623. spin_lock_init(&port->lock);
  624. port->irq = siu->irq;
  625. port->uartclk = SIU_BAUD_BASE * 16;
  626. port->fifosize = 16;
  627. port->regshift = 0;
  628. port->iotype = UPIO_MEM;
  629. port->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  630. port->type = siu->type;
  631. port->line = i;
  632. port->mapbase = siu->start;
  633. siu++;
  634. port++;
  635. }
  636. return num;
  637. }
  638. #ifdef CONFIG_SERIAL_VR41XX_CONSOLE
  639. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  640. static void wait_for_xmitr(struct uart_port *port)
  641. {
  642. int timeout = 10000;
  643. uint8_t lsr, msr;
  644. do {
  645. lsr = siu_read(port, UART_LSR);
  646. if (lsr & UART_LSR_BI)
  647. lsr_break_flag[port->line] = UART_LSR_BI;
  648. if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
  649. break;
  650. } while (timeout-- > 0);
  651. if (port->flags & UPF_CONS_FLOW) {
  652. timeout = 1000000;
  653. do {
  654. msr = siu_read(port, UART_MSR);
  655. if ((msr & UART_MSR_CTS) != 0)
  656. break;
  657. } while (timeout-- > 0);
  658. }
  659. }
  660. static void siu_console_putchar(struct uart_port *port, int ch)
  661. {
  662. wait_for_xmitr(port);
  663. siu_write(port, UART_TX, ch);
  664. }
  665. static void siu_console_write(struct console *con, const char *s, unsigned count)
  666. {
  667. struct uart_port *port;
  668. uint8_t ier;
  669. port = &siu_uart_ports[con->index];
  670. ier = siu_read(port, UART_IER);
  671. siu_write(port, UART_IER, 0);
  672. uart_console_write(port, s, count, siu_console_putchar);
  673. wait_for_xmitr(port);
  674. siu_write(port, UART_IER, ier);
  675. }
  676. static int siu_console_setup(struct console *con, char *options)
  677. {
  678. struct uart_port *port;
  679. int baud = 9600;
  680. int parity = 'n';
  681. int bits = 8;
  682. int flow = 'n';
  683. if (con->index >= SIU_PORTS_MAX)
  684. con->index = 0;
  685. port = &siu_uart_ports[con->index];
  686. if (port->membase == NULL) {
  687. if (port->mapbase == 0)
  688. return -ENODEV;
  689. port->membase = ioremap(port->mapbase, siu_port_size(port));
  690. }
  691. vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
  692. if (options != NULL)
  693. uart_parse_options(options, &baud, &parity, &bits, &flow);
  694. return uart_set_options(port, con, baud, parity, bits, flow);
  695. }
  696. static struct uart_driver siu_uart_driver;
  697. static struct console siu_console = {
  698. .name = "ttyVR",
  699. .write = siu_console_write,
  700. .device = uart_console_device,
  701. .setup = siu_console_setup,
  702. .flags = CON_PRINTBUFFER,
  703. .index = -1,
  704. .data = &siu_uart_driver,
  705. };
  706. static int __devinit siu_console_init(void)
  707. {
  708. struct uart_port *port;
  709. int num, i;
  710. num = siu_init_ports();
  711. if (num <= 0)
  712. return -ENODEV;
  713. for (i = 0; i < num; i++) {
  714. port = &siu_uart_ports[i];
  715. port->ops = &siu_uart_ops;
  716. }
  717. register_console(&siu_console);
  718. return 0;
  719. }
  720. console_initcall(siu_console_init);
  721. #define SERIAL_VR41XX_CONSOLE &siu_console
  722. #else
  723. #define SERIAL_VR41XX_CONSOLE NULL
  724. #endif
  725. static struct uart_driver siu_uart_driver = {
  726. .owner = THIS_MODULE,
  727. .driver_name = "SIU",
  728. .dev_name = "ttyVR",
  729. .devfs_name = "ttvr/",
  730. .major = SIU_MAJOR,
  731. .minor = SIU_MINOR_BASE,
  732. .cons = SERIAL_VR41XX_CONSOLE,
  733. };
  734. static int __devinit siu_probe(struct platform_device *dev)
  735. {
  736. struct uart_port *port;
  737. int num, i, retval;
  738. num = siu_init_ports();
  739. if (num <= 0)
  740. return -ENODEV;
  741. siu_uart_driver.nr = num;
  742. retval = uart_register_driver(&siu_uart_driver);
  743. if (retval)
  744. return retval;
  745. for (i = 0; i < num; i++) {
  746. port = &siu_uart_ports[i];
  747. port->ops = &siu_uart_ops;
  748. port->dev = &dev->dev;
  749. retval = uart_add_one_port(&siu_uart_driver, port);
  750. if (retval < 0) {
  751. port->dev = NULL;
  752. break;
  753. }
  754. }
  755. if (i == 0 && retval < 0) {
  756. uart_unregister_driver(&siu_uart_driver);
  757. return retval;
  758. }
  759. return 0;
  760. }
  761. static int __devexit siu_remove(struct platform_device *dev)
  762. {
  763. struct uart_port *port;
  764. int i;
  765. for (i = 0; i < siu_uart_driver.nr; i++) {
  766. port = &siu_uart_ports[i];
  767. if (port->dev == &dev->dev) {
  768. uart_remove_one_port(&siu_uart_driver, port);
  769. port->dev = NULL;
  770. }
  771. }
  772. uart_unregister_driver(&siu_uart_driver);
  773. return 0;
  774. }
  775. static int siu_suspend(struct platform_device *dev, pm_message_t state)
  776. {
  777. struct uart_port *port;
  778. int i;
  779. for (i = 0; i < siu_uart_driver.nr; i++) {
  780. port = &siu_uart_ports[i];
  781. if ((port->type == PORT_VR41XX_SIU ||
  782. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  783. uart_suspend_port(&siu_uart_driver, port);
  784. }
  785. return 0;
  786. }
  787. static int siu_resume(struct platform_device *dev)
  788. {
  789. struct uart_port *port;
  790. int i;
  791. for (i = 0; i < siu_uart_driver.nr; i++) {
  792. port = &siu_uart_ports[i];
  793. if ((port->type == PORT_VR41XX_SIU ||
  794. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  795. uart_resume_port(&siu_uart_driver, port);
  796. }
  797. return 0;
  798. }
  799. static struct platform_device *siu_platform_device;
  800. static struct platform_driver siu_device_driver = {
  801. .probe = siu_probe,
  802. .remove = __devexit_p(siu_remove),
  803. .suspend = siu_suspend,
  804. .resume = siu_resume,
  805. .driver = {
  806. .name = "SIU",
  807. .owner = THIS_MODULE,
  808. },
  809. };
  810. static int __init vr41xx_siu_init(void)
  811. {
  812. int retval;
  813. siu_platform_device = platform_device_alloc("SIU", -1);
  814. if (!siu_platform_device)
  815. return -ENOMEM;
  816. retval = platform_device_add(siu_platform_device);
  817. if (retval < 0) {
  818. platform_device_put(siu_platform_device);
  819. return retval;
  820. }
  821. retval = platform_driver_register(&siu_device_driver);
  822. if (retval < 0)
  823. platform_device_unregister(siu_platform_device);
  824. return retval;
  825. }
  826. static void __exit vr41xx_siu_exit(void)
  827. {
  828. platform_driver_unregister(&siu_device_driver);
  829. platform_device_unregister(siu_platform_device);
  830. }
  831. module_init(vr41xx_siu_init);
  832. module_exit(vr41xx_siu_exit);