pnx8xxx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * UART driver for PNX8XXX SoCs
  3. *
  4. * Author: Per Hallsmark per.hallsmark@mvista.com
  5. * Ported to 2.6 kernel by EmbeddedAlley
  6. * Reworked by Vitaly Wool <vitalywool@gmail.com>
  7. *
  8. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  9. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  10. *
  11. * This file is licensed under the terms of the GNU General Public License
  12. * version 2. This program is licensed "as is" without any warranty of
  13. * any kind, whether express or implied.
  14. *
  15. */
  16. #if defined(CONFIG_SERIAL_PNX8XXX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  17. #define SUPPORT_SYSRQ
  18. #endif
  19. #include <linux/module.h>
  20. #include <linux/ioport.h>
  21. #include <linux/init.h>
  22. #include <linux/console.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/tty.h>
  27. #include <linux/tty_flip.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_pnx8xxx.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. /* We'll be using StrongARM sa1100 serial port major/minor */
  34. #define SERIAL_PNX8XXX_MAJOR 204
  35. #define MINOR_START 5
  36. #define NR_PORTS 2
  37. #define PNX8XXX_ISR_PASS_LIMIT 256
  38. /*
  39. * Convert from ignore_status_mask or read_status_mask to FIFO
  40. * and interrupt status bits
  41. */
  42. #define SM_TO_FIFO(x) ((x) >> 10)
  43. #define SM_TO_ISTAT(x) ((x) & 0x000001ff)
  44. #define FIFO_TO_SM(x) ((x) << 10)
  45. #define ISTAT_TO_SM(x) ((x) & 0x000001ff)
  46. /*
  47. * This is the size of our serial port register set.
  48. */
  49. #define UART_PORT_SIZE 0x1000
  50. /*
  51. * This determines how often we check the modem status signals
  52. * for any change. They generally aren't connected to an IRQ
  53. * so we have to poll them. We also check immediately before
  54. * filling the TX fifo incase CTS has been dropped.
  55. */
  56. #define MCTRL_TIMEOUT (250*HZ/1000)
  57. extern struct pnx8xxx_port pnx8xxx_ports[];
  58. static inline int serial_in(struct pnx8xxx_port *sport, int offset)
  59. {
  60. return (__raw_readl(sport->port.membase + offset));
  61. }
  62. static inline void serial_out(struct pnx8xxx_port *sport, int offset, int value)
  63. {
  64. __raw_writel(value, sport->port.membase + offset);
  65. }
  66. /*
  67. * Handle any change of modem status signal since we were last called.
  68. */
  69. static void pnx8xxx_mctrl_check(struct pnx8xxx_port *sport)
  70. {
  71. unsigned int status, changed;
  72. status = sport->port.ops->get_mctrl(&sport->port);
  73. changed = status ^ sport->old_status;
  74. if (changed == 0)
  75. return;
  76. sport->old_status = status;
  77. if (changed & TIOCM_RI)
  78. sport->port.icount.rng++;
  79. if (changed & TIOCM_DSR)
  80. sport->port.icount.dsr++;
  81. if (changed & TIOCM_CAR)
  82. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  83. if (changed & TIOCM_CTS)
  84. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  85. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  86. }
  87. /*
  88. * This is our per-port timeout handler, for checking the
  89. * modem status signals.
  90. */
  91. static void pnx8xxx_timeout(unsigned long data)
  92. {
  93. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)data;
  94. unsigned long flags;
  95. if (sport->port.state) {
  96. spin_lock_irqsave(&sport->port.lock, flags);
  97. pnx8xxx_mctrl_check(sport);
  98. spin_unlock_irqrestore(&sport->port.lock, flags);
  99. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  100. }
  101. }
  102. /*
  103. * interrupts disabled on entry
  104. */
  105. static void pnx8xxx_stop_tx(struct uart_port *port)
  106. {
  107. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  108. u32 ien;
  109. /* Disable TX intr */
  110. ien = serial_in(sport, PNX8XXX_IEN);
  111. serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLTX);
  112. /* Clear all pending TX intr */
  113. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
  114. }
  115. /*
  116. * interrupts may not be disabled on entry
  117. */
  118. static void pnx8xxx_start_tx(struct uart_port *port)
  119. {
  120. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  121. u32 ien;
  122. /* Clear all pending TX intr */
  123. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLTX);
  124. /* Enable TX intr */
  125. ien = serial_in(sport, PNX8XXX_IEN);
  126. serial_out(sport, PNX8XXX_IEN, ien | PNX8XXX_UART_INT_ALLTX);
  127. }
  128. /*
  129. * Interrupts enabled
  130. */
  131. static void pnx8xxx_stop_rx(struct uart_port *port)
  132. {
  133. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  134. u32 ien;
  135. /* Disable RX intr */
  136. ien = serial_in(sport, PNX8XXX_IEN);
  137. serial_out(sport, PNX8XXX_IEN, ien & ~PNX8XXX_UART_INT_ALLRX);
  138. /* Clear all pending RX intr */
  139. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX);
  140. }
  141. /*
  142. * Set the modem control timer to fire immediately.
  143. */
  144. static void pnx8xxx_enable_ms(struct uart_port *port)
  145. {
  146. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  147. mod_timer(&sport->timer, jiffies);
  148. }
  149. static void pnx8xxx_rx_chars(struct pnx8xxx_port *sport)
  150. {
  151. struct tty_struct *tty = sport->port.state->port.tty;
  152. unsigned int status, ch, flg;
  153. status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
  154. ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
  155. while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
  156. ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
  157. sport->port.icount.rx++;
  158. flg = TTY_NORMAL;
  159. /*
  160. * note that the error handling code is
  161. * out of the main execution path
  162. */
  163. if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
  164. PNX8XXX_UART_FIFO_RXPAR |
  165. PNX8XXX_UART_FIFO_RXBRK) |
  166. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
  167. if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
  168. status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  169. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
  170. sport->port.icount.brk++;
  171. if (uart_handle_break(&sport->port))
  172. goto ignore_char;
  173. } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
  174. sport->port.icount.parity++;
  175. else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
  176. sport->port.icount.frame++;
  177. if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
  178. sport->port.icount.overrun++;
  179. status &= sport->port.read_status_mask;
  180. if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
  181. flg = TTY_PARITY;
  182. else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
  183. flg = TTY_FRAME;
  184. #ifdef SUPPORT_SYSRQ
  185. sport->port.sysrq = 0;
  186. #endif
  187. }
  188. if (uart_handle_sysrq_char(&sport->port, ch))
  189. goto ignore_char;
  190. uart_insert_char(&sport->port, status,
  191. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
  192. ignore_char:
  193. serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
  194. PNX8XXX_UART_LCR_RX_NEXT);
  195. status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
  196. ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
  197. }
  198. tty_flip_buffer_push(tty);
  199. }
  200. static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
  201. {
  202. struct circ_buf *xmit = &sport->port.state->xmit;
  203. if (sport->port.x_char) {
  204. serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
  205. sport->port.icount.tx++;
  206. sport->port.x_char = 0;
  207. return;
  208. }
  209. /*
  210. * Check the modem control lines before
  211. * transmitting anything.
  212. */
  213. pnx8xxx_mctrl_check(sport);
  214. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  215. pnx8xxx_stop_tx(&sport->port);
  216. return;
  217. }
  218. /*
  219. * TX while bytes available
  220. */
  221. while (((serial_in(sport, PNX8XXX_FIFO) &
  222. PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
  223. serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
  224. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  225. sport->port.icount.tx++;
  226. if (uart_circ_empty(xmit))
  227. break;
  228. }
  229. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  230. uart_write_wakeup(&sport->port);
  231. if (uart_circ_empty(xmit))
  232. pnx8xxx_stop_tx(&sport->port);
  233. }
  234. static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
  235. {
  236. struct pnx8xxx_port *sport = dev_id;
  237. unsigned int status;
  238. spin_lock(&sport->port.lock);
  239. /* Get the interrupts */
  240. status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
  241. /* Byte or break signal received */
  242. if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
  243. pnx8xxx_rx_chars(sport);
  244. /* TX holding register empty - transmit a byte */
  245. if (status & PNX8XXX_UART_INT_TX)
  246. pnx8xxx_tx_chars(sport);
  247. /* Clear the ISTAT register */
  248. serial_out(sport, PNX8XXX_ICLR, status);
  249. spin_unlock(&sport->port.lock);
  250. return IRQ_HANDLED;
  251. }
  252. /*
  253. * Return TIOCSER_TEMT when transmitter is not busy.
  254. */
  255. static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
  256. {
  257. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  258. return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
  259. }
  260. static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
  261. {
  262. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  263. unsigned int mctrl = TIOCM_DSR;
  264. unsigned int msr;
  265. /* REVISIT */
  266. msr = serial_in(sport, PNX8XXX_MCR);
  267. mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
  268. mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
  269. return mctrl;
  270. }
  271. static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  272. {
  273. #if 0 /* FIXME */
  274. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  275. unsigned int msr;
  276. #endif
  277. }
  278. /*
  279. * Interrupts always disabled.
  280. */
  281. static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
  282. {
  283. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  284. unsigned long flags;
  285. unsigned int lcr;
  286. spin_lock_irqsave(&sport->port.lock, flags);
  287. lcr = serial_in(sport, PNX8XXX_LCR);
  288. if (break_state == -1)
  289. lcr |= PNX8XXX_UART_LCR_TXBREAK;
  290. else
  291. lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
  292. serial_out(sport, PNX8XXX_LCR, lcr);
  293. spin_unlock_irqrestore(&sport->port.lock, flags);
  294. }
  295. static int pnx8xxx_startup(struct uart_port *port)
  296. {
  297. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  298. int retval;
  299. /*
  300. * Allocate the IRQ
  301. */
  302. retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
  303. "pnx8xxx-uart", sport);
  304. if (retval)
  305. return retval;
  306. /*
  307. * Finally, clear and enable interrupts
  308. */
  309. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
  310. PNX8XXX_UART_INT_ALLTX);
  311. serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
  312. PNX8XXX_UART_INT_ALLRX |
  313. PNX8XXX_UART_INT_ALLTX);
  314. /*
  315. * Enable modem status interrupts
  316. */
  317. spin_lock_irq(&sport->port.lock);
  318. pnx8xxx_enable_ms(&sport->port);
  319. spin_unlock_irq(&sport->port.lock);
  320. return 0;
  321. }
  322. static void pnx8xxx_shutdown(struct uart_port *port)
  323. {
  324. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  325. int lcr;
  326. /*
  327. * Stop our timer.
  328. */
  329. del_timer_sync(&sport->timer);
  330. /*
  331. * Disable all interrupts
  332. */
  333. serial_out(sport, PNX8XXX_IEN, 0);
  334. /*
  335. * Reset the Tx and Rx FIFOS, disable the break condition
  336. */
  337. lcr = serial_in(sport, PNX8XXX_LCR);
  338. lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
  339. lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
  340. serial_out(sport, PNX8XXX_LCR, lcr);
  341. /*
  342. * Clear all interrupts
  343. */
  344. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
  345. PNX8XXX_UART_INT_ALLTX);
  346. /*
  347. * Free the interrupt
  348. */
  349. free_irq(sport->port.irq, sport);
  350. }
  351. static void
  352. pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
  353. struct ktermios *old)
  354. {
  355. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  356. unsigned long flags;
  357. unsigned int lcr_fcr, old_ien, baud, quot;
  358. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  359. /*
  360. * We only support CS7 and CS8.
  361. */
  362. while ((termios->c_cflag & CSIZE) != CS7 &&
  363. (termios->c_cflag & CSIZE) != CS8) {
  364. termios->c_cflag &= ~CSIZE;
  365. termios->c_cflag |= old_csize;
  366. old_csize = CS8;
  367. }
  368. if ((termios->c_cflag & CSIZE) == CS8)
  369. lcr_fcr = PNX8XXX_UART_LCR_8BIT;
  370. else
  371. lcr_fcr = 0;
  372. if (termios->c_cflag & CSTOPB)
  373. lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
  374. if (termios->c_cflag & PARENB) {
  375. lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
  376. if (!(termios->c_cflag & PARODD))
  377. lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
  378. }
  379. /*
  380. * Ask the core to calculate the divisor for us.
  381. */
  382. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  383. quot = uart_get_divisor(port, baud);
  384. spin_lock_irqsave(&sport->port.lock, flags);
  385. sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
  386. ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
  387. ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
  388. if (termios->c_iflag & INPCK)
  389. sport->port.read_status_mask |=
  390. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  391. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
  392. if (termios->c_iflag & (BRKINT | PARMRK))
  393. sport->port.read_status_mask |=
  394. ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
  395. /*
  396. * Characters to ignore
  397. */
  398. sport->port.ignore_status_mask = 0;
  399. if (termios->c_iflag & IGNPAR)
  400. sport->port.ignore_status_mask |=
  401. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  402. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
  403. if (termios->c_iflag & IGNBRK) {
  404. sport->port.ignore_status_mask |=
  405. ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
  406. /*
  407. * If we're ignoring parity and break indicators,
  408. * ignore overruns too (for real raw support).
  409. */
  410. if (termios->c_iflag & IGNPAR)
  411. sport->port.ignore_status_mask |=
  412. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
  413. }
  414. /*
  415. * ignore all characters if CREAD is not set
  416. */
  417. if ((termios->c_cflag & CREAD) == 0)
  418. sport->port.ignore_status_mask |=
  419. ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
  420. del_timer_sync(&sport->timer);
  421. /*
  422. * Update the per-port timeout.
  423. */
  424. uart_update_timeout(port, termios->c_cflag, baud);
  425. /*
  426. * disable interrupts and drain transmitter
  427. */
  428. old_ien = serial_in(sport, PNX8XXX_IEN);
  429. serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
  430. PNX8XXX_UART_INT_ALLRX));
  431. while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
  432. barrier();
  433. /* then, disable everything */
  434. serial_out(sport, PNX8XXX_IEN, 0);
  435. /* Reset the Rx and Tx FIFOs too */
  436. lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
  437. lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
  438. /* set the parity, stop bits and data size */
  439. serial_out(sport, PNX8XXX_LCR, lcr_fcr);
  440. /* set the baud rate */
  441. quot -= 1;
  442. serial_out(sport, PNX8XXX_BAUD, quot);
  443. serial_out(sport, PNX8XXX_ICLR, -1);
  444. serial_out(sport, PNX8XXX_IEN, old_ien);
  445. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  446. pnx8xxx_enable_ms(&sport->port);
  447. spin_unlock_irqrestore(&sport->port.lock, flags);
  448. }
  449. static const char *pnx8xxx_type(struct uart_port *port)
  450. {
  451. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  452. return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
  453. }
  454. /*
  455. * Release the memory region(s) being used by 'port'.
  456. */
  457. static void pnx8xxx_release_port(struct uart_port *port)
  458. {
  459. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  460. release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
  461. }
  462. /*
  463. * Request the memory region(s) being used by 'port'.
  464. */
  465. static int pnx8xxx_request_port(struct uart_port *port)
  466. {
  467. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  468. return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
  469. "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
  470. }
  471. /*
  472. * Configure/autoconfigure the port.
  473. */
  474. static void pnx8xxx_config_port(struct uart_port *port, int flags)
  475. {
  476. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  477. if (flags & UART_CONFIG_TYPE &&
  478. pnx8xxx_request_port(&sport->port) == 0)
  479. sport->port.type = PORT_PNX8XXX;
  480. }
  481. /*
  482. * Verify the new serial_struct (for TIOCSSERIAL).
  483. * The only change we allow are to the flags and type, and
  484. * even then only between PORT_PNX8XXX and PORT_UNKNOWN
  485. */
  486. static int
  487. pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
  488. {
  489. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  490. int ret = 0;
  491. if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
  492. ret = -EINVAL;
  493. if (sport->port.irq != ser->irq)
  494. ret = -EINVAL;
  495. if (ser->io_type != SERIAL_IO_MEM)
  496. ret = -EINVAL;
  497. if (sport->port.uartclk / 16 != ser->baud_base)
  498. ret = -EINVAL;
  499. if ((void *)sport->port.mapbase != ser->iomem_base)
  500. ret = -EINVAL;
  501. if (sport->port.iobase != ser->port)
  502. ret = -EINVAL;
  503. if (ser->hub6 != 0)
  504. ret = -EINVAL;
  505. return ret;
  506. }
  507. static struct uart_ops pnx8xxx_pops = {
  508. .tx_empty = pnx8xxx_tx_empty,
  509. .set_mctrl = pnx8xxx_set_mctrl,
  510. .get_mctrl = pnx8xxx_get_mctrl,
  511. .stop_tx = pnx8xxx_stop_tx,
  512. .start_tx = pnx8xxx_start_tx,
  513. .stop_rx = pnx8xxx_stop_rx,
  514. .enable_ms = pnx8xxx_enable_ms,
  515. .break_ctl = pnx8xxx_break_ctl,
  516. .startup = pnx8xxx_startup,
  517. .shutdown = pnx8xxx_shutdown,
  518. .set_termios = pnx8xxx_set_termios,
  519. .type = pnx8xxx_type,
  520. .release_port = pnx8xxx_release_port,
  521. .request_port = pnx8xxx_request_port,
  522. .config_port = pnx8xxx_config_port,
  523. .verify_port = pnx8xxx_verify_port,
  524. };
  525. /*
  526. * Setup the PNX8XXX serial ports.
  527. *
  528. * Note also that we support "console=ttySx" where "x" is either 0 or 1.
  529. */
  530. static void __init pnx8xxx_init_ports(void)
  531. {
  532. static int first = 1;
  533. int i;
  534. if (!first)
  535. return;
  536. first = 0;
  537. for (i = 0; i < NR_PORTS; i++) {
  538. init_timer(&pnx8xxx_ports[i].timer);
  539. pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
  540. pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
  541. pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
  542. }
  543. }
  544. #ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
  545. static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
  546. {
  547. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  548. int status;
  549. do {
  550. /* Wait for UART_TX register to empty */
  551. status = serial_in(sport, PNX8XXX_FIFO);
  552. } while (status & PNX8XXX_UART_FIFO_TXFIFO);
  553. serial_out(sport, PNX8XXX_FIFO, ch);
  554. }
  555. /*
  556. * Interrupts are disabled on entering
  557. */static void
  558. pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
  559. {
  560. struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
  561. unsigned int old_ien, status;
  562. /*
  563. * First, save IEN and then disable interrupts
  564. */
  565. old_ien = serial_in(sport, PNX8XXX_IEN);
  566. serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
  567. PNX8XXX_UART_INT_ALLRX));
  568. uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
  569. /*
  570. * Finally, wait for transmitter to become empty
  571. * and restore IEN
  572. */
  573. do {
  574. /* Wait for UART_TX register to empty */
  575. status = serial_in(sport, PNX8XXX_FIFO);
  576. } while (status & PNX8XXX_UART_FIFO_TXFIFO);
  577. /* Clear TX and EMPTY interrupt */
  578. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
  579. PNX8XXX_UART_INT_EMPTY);
  580. serial_out(sport, PNX8XXX_IEN, old_ien);
  581. }
  582. static int __init
  583. pnx8xxx_console_setup(struct console *co, char *options)
  584. {
  585. struct pnx8xxx_port *sport;
  586. int baud = 38400;
  587. int bits = 8;
  588. int parity = 'n';
  589. int flow = 'n';
  590. /*
  591. * Check whether an invalid uart number has been specified, and
  592. * if so, search for the first available port that does have
  593. * console support.
  594. */
  595. if (co->index == -1 || co->index >= NR_PORTS)
  596. co->index = 0;
  597. sport = &pnx8xxx_ports[co->index];
  598. if (options)
  599. uart_parse_options(options, &baud, &parity, &bits, &flow);
  600. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  601. }
  602. static struct uart_driver pnx8xxx_reg;
  603. static struct console pnx8xxx_console = {
  604. .name = "ttyS",
  605. .write = pnx8xxx_console_write,
  606. .device = uart_console_device,
  607. .setup = pnx8xxx_console_setup,
  608. .flags = CON_PRINTBUFFER,
  609. .index = -1,
  610. .data = &pnx8xxx_reg,
  611. };
  612. static int __init pnx8xxx_rs_console_init(void)
  613. {
  614. pnx8xxx_init_ports();
  615. register_console(&pnx8xxx_console);
  616. return 0;
  617. }
  618. console_initcall(pnx8xxx_rs_console_init);
  619. #define PNX8XXX_CONSOLE &pnx8xxx_console
  620. #else
  621. #define PNX8XXX_CONSOLE NULL
  622. #endif
  623. static struct uart_driver pnx8xxx_reg = {
  624. .owner = THIS_MODULE,
  625. .driver_name = "ttyS",
  626. .dev_name = "ttyS",
  627. .major = SERIAL_PNX8XXX_MAJOR,
  628. .minor = MINOR_START,
  629. .nr = NR_PORTS,
  630. .cons = PNX8XXX_CONSOLE,
  631. };
  632. static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
  633. {
  634. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  635. return uart_suspend_port(&pnx8xxx_reg, &sport->port);
  636. }
  637. static int pnx8xxx_serial_resume(struct platform_device *pdev)
  638. {
  639. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  640. return uart_resume_port(&pnx8xxx_reg, &sport->port);
  641. }
  642. static int pnx8xxx_serial_probe(struct platform_device *pdev)
  643. {
  644. struct resource *res = pdev->resource;
  645. int i;
  646. for (i = 0; i < pdev->num_resources; i++, res++) {
  647. if (!(res->flags & IORESOURCE_MEM))
  648. continue;
  649. for (i = 0; i < NR_PORTS; i++) {
  650. if (pnx8xxx_ports[i].port.mapbase != res->start)
  651. continue;
  652. pnx8xxx_ports[i].port.dev = &pdev->dev;
  653. uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
  654. platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
  655. break;
  656. }
  657. }
  658. return 0;
  659. }
  660. static int pnx8xxx_serial_remove(struct platform_device *pdev)
  661. {
  662. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  663. platform_set_drvdata(pdev, NULL);
  664. if (sport)
  665. uart_remove_one_port(&pnx8xxx_reg, &sport->port);
  666. return 0;
  667. }
  668. static struct platform_driver pnx8xxx_serial_driver = {
  669. .driver = {
  670. .name = "pnx8xxx-uart",
  671. .owner = THIS_MODULE,
  672. },
  673. .probe = pnx8xxx_serial_probe,
  674. .remove = pnx8xxx_serial_remove,
  675. .suspend = pnx8xxx_serial_suspend,
  676. .resume = pnx8xxx_serial_resume,
  677. };
  678. static int __init pnx8xxx_serial_init(void)
  679. {
  680. int ret;
  681. printk(KERN_INFO "Serial: PNX8XXX driver\n");
  682. pnx8xxx_init_ports();
  683. ret = uart_register_driver(&pnx8xxx_reg);
  684. if (ret == 0) {
  685. ret = platform_driver_register(&pnx8xxx_serial_driver);
  686. if (ret)
  687. uart_unregister_driver(&pnx8xxx_reg);
  688. }
  689. return ret;
  690. }
  691. static void __exit pnx8xxx_serial_exit(void)
  692. {
  693. platform_driver_unregister(&pnx8xxx_serial_driver);
  694. uart_unregister_driver(&pnx8xxx_reg);
  695. }
  696. module_init(pnx8xxx_serial_init);
  697. module_exit(pnx8xxx_serial_exit);
  698. MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
  699. MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
  700. MODULE_LICENSE("GPL");
  701. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
  702. MODULE_ALIAS("platform:pnx8xxx-uart");