vr41xx_siu.c 21 KB

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