vr41xx_siu.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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_write(struct console *con, const char *s, unsigned count)
  661. {
  662. struct uart_port *port;
  663. uint8_t ier;
  664. unsigned i;
  665. port = &siu_uart_ports[con->index];
  666. ier = siu_read(port, UART_IER);
  667. siu_write(port, UART_IER, 0);
  668. for (i = 0; i < count && *s != '\0'; i++, s++) {
  669. wait_for_xmitr(port);
  670. siu_write(port, UART_TX, *s);
  671. if (*s == '\n') {
  672. wait_for_xmitr(port);
  673. siu_write(port, UART_TX, '\r');
  674. }
  675. }
  676. wait_for_xmitr(port);
  677. siu_write(port, UART_IER, ier);
  678. }
  679. static int siu_console_setup(struct console *con, char *options)
  680. {
  681. struct uart_port *port;
  682. int baud = 9600;
  683. int parity = 'n';
  684. int bits = 8;
  685. int flow = 'n';
  686. if (con->index >= SIU_PORTS_MAX)
  687. con->index = 0;
  688. port = &siu_uart_ports[con->index];
  689. if (port->membase == NULL) {
  690. if (port->mapbase == 0)
  691. return -ENODEV;
  692. port->membase = ioremap(port->mapbase, siu_port_size(port));
  693. }
  694. vr41xx_select_siu_interface(SIU_INTERFACE_RS232C);
  695. if (options != NULL)
  696. uart_parse_options(options, &baud, &parity, &bits, &flow);
  697. return uart_set_options(port, con, baud, parity, bits, flow);
  698. }
  699. static struct uart_driver siu_uart_driver;
  700. static struct console siu_console = {
  701. .name = "ttyVR",
  702. .write = siu_console_write,
  703. .device = uart_console_device,
  704. .setup = siu_console_setup,
  705. .flags = CON_PRINTBUFFER,
  706. .index = -1,
  707. .data = &siu_uart_driver,
  708. };
  709. static int __devinit siu_console_init(void)
  710. {
  711. struct uart_port *port;
  712. int num, i;
  713. num = siu_init_ports();
  714. if (num <= 0)
  715. return -ENODEV;
  716. for (i = 0; i < num; i++) {
  717. port = &siu_uart_ports[i];
  718. port->ops = &siu_uart_ops;
  719. }
  720. register_console(&siu_console);
  721. return 0;
  722. }
  723. console_initcall(siu_console_init);
  724. #define SERIAL_VR41XX_CONSOLE &siu_console
  725. #else
  726. #define SERIAL_VR41XX_CONSOLE NULL
  727. #endif
  728. static struct uart_driver siu_uart_driver = {
  729. .owner = THIS_MODULE,
  730. .driver_name = "SIU",
  731. .dev_name = "ttyVR",
  732. .devfs_name = "ttvr/",
  733. .major = SIU_MAJOR,
  734. .minor = SIU_MINOR_BASE,
  735. .cons = SERIAL_VR41XX_CONSOLE,
  736. };
  737. static int siu_probe(struct platform_device *dev)
  738. {
  739. struct uart_port *port;
  740. int num, i, retval;
  741. num = siu_init_ports();
  742. if (num <= 0)
  743. return -ENODEV;
  744. siu_uart_driver.nr = num;
  745. retval = uart_register_driver(&siu_uart_driver);
  746. if (retval)
  747. return retval;
  748. for (i = 0; i < num; i++) {
  749. port = &siu_uart_ports[i];
  750. port->ops = &siu_uart_ops;
  751. port->dev = &dev->dev;
  752. retval = uart_add_one_port(&siu_uart_driver, port);
  753. if (retval < 0) {
  754. port->dev = NULL;
  755. break;
  756. }
  757. }
  758. if (i == 0 && retval < 0) {
  759. uart_unregister_driver(&siu_uart_driver);
  760. return retval;
  761. }
  762. return 0;
  763. }
  764. static int siu_remove(struct platform_device *dev)
  765. {
  766. struct uart_port *port;
  767. int i;
  768. for (i = 0; i < siu_uart_driver.nr; i++) {
  769. port = &siu_uart_ports[i];
  770. if (port->dev == &dev->dev) {
  771. uart_remove_one_port(&siu_uart_driver, port);
  772. port->dev = NULL;
  773. }
  774. }
  775. uart_unregister_driver(&siu_uart_driver);
  776. return 0;
  777. }
  778. static int siu_suspend(struct platform_device *dev, pm_message_t state)
  779. {
  780. struct uart_port *port;
  781. int i;
  782. for (i = 0; i < siu_uart_driver.nr; i++) {
  783. port = &siu_uart_ports[i];
  784. if ((port->type == PORT_VR41XX_SIU ||
  785. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  786. uart_suspend_port(&siu_uart_driver, port);
  787. }
  788. return 0;
  789. }
  790. static int siu_resume(struct platform_device *dev)
  791. {
  792. struct uart_port *port;
  793. int i;
  794. for (i = 0; i < siu_uart_driver.nr; i++) {
  795. port = &siu_uart_ports[i];
  796. if ((port->type == PORT_VR41XX_SIU ||
  797. port->type == PORT_VR41XX_DSIU) && port->dev == &dev->dev)
  798. uart_resume_port(&siu_uart_driver, port);
  799. }
  800. return 0;
  801. }
  802. static struct platform_device *siu_platform_device;
  803. static struct platform_driver siu_device_driver = {
  804. .probe = siu_probe,
  805. .remove = siu_remove,
  806. .suspend = siu_suspend,
  807. .resume = siu_resume,
  808. .driver = {
  809. .name = "SIU",
  810. },
  811. };
  812. static int __devinit vr41xx_siu_init(void)
  813. {
  814. int retval;
  815. siu_platform_device = platform_device_register_simple("SIU", -1, NULL, 0);
  816. if (IS_ERR(siu_platform_device))
  817. return PTR_ERR(siu_platform_device);
  818. retval = platform_driver_register(&siu_device_driver);
  819. if (retval < 0)
  820. platform_device_unregister(siu_platform_device);
  821. return retval;
  822. }
  823. static void __devexit vr41xx_siu_exit(void)
  824. {
  825. platform_driver_unregister(&siu_device_driver);
  826. platform_device_unregister(siu_platform_device);
  827. }
  828. module_init(vr41xx_siu_init);
  829. module_exit(vr41xx_siu_exit);