vr41xx_siu.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/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. 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 platform_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->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 platform_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->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 platform_device *dev, pm_message_t state)
  783. {
  784. struct uart_port *port;
  785. int i;
  786. for (i = 0; i < siu_uart_driver.nr; i++) {
  787. port = &siu_uart_ports[i];
  788. if ((port->type == PORT_VR41XX_SIU ||
  789. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  790. uart_suspend_port(&siu_uart_driver, port);
  791. }
  792. return 0;
  793. }
  794. static int siu_resume(struct platform_device *dev)
  795. {
  796. struct uart_port *port;
  797. int i;
  798. for (i = 0; i < siu_uart_driver.nr; i++) {
  799. port = &siu_uart_ports[i];
  800. if ((port->type == PORT_VR41XX_SIU ||
  801. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  802. uart_resume_port(&siu_uart_driver, port);
  803. }
  804. return 0;
  805. }
  806. static struct platform_device *siu_platform_device;
  807. static struct platform_driver siu_device_driver = {
  808. .probe = siu_probe,
  809. .remove = siu_remove,
  810. .suspend = siu_suspend,
  811. .resume = siu_resume,
  812. .driver = {
  813. .name = "SIU",
  814. },
  815. };
  816. static int __devinit vr41xx_siu_init(void)
  817. {
  818. int retval;
  819. siu_platform_device = platform_device_register_simple("SIU", -1, NULL, 0);
  820. if (IS_ERR(siu_platform_device))
  821. return PTR_ERR(siu_platform_device);
  822. retval = platform_driver_register(&siu_device_driver);
  823. if (retval < 0)
  824. platform_device_unregister(siu_platform_device);
  825. return retval;
  826. }
  827. static void __devexit vr41xx_siu_exit(void)
  828. {
  829. platform_driver_unregister(&siu_device_driver);
  830. platform_device_unregister(siu_platform_device);
  831. }
  832. module_init(vr41xx_siu_init);
  833. module_exit(vr41xx_siu_exit);