vr41xx_siu.c 23 KB

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