pnx8xxx_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. unsigned int status, ch, flg;
  152. status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
  153. ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
  154. while (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFIFO)) {
  155. ch = serial_in(sport, PNX8XXX_FIFO) & 0xff;
  156. sport->port.icount.rx++;
  157. flg = TTY_NORMAL;
  158. /*
  159. * note that the error handling code is
  160. * out of the main execution path
  161. */
  162. if (status & (FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE |
  163. PNX8XXX_UART_FIFO_RXPAR |
  164. PNX8XXX_UART_FIFO_RXBRK) |
  165. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))) {
  166. if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXBRK)) {
  167. status &= ~(FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  168. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR));
  169. sport->port.icount.brk++;
  170. if (uart_handle_break(&sport->port))
  171. goto ignore_char;
  172. } else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
  173. sport->port.icount.parity++;
  174. else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
  175. sport->port.icount.frame++;
  176. if (status & ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN))
  177. sport->port.icount.overrun++;
  178. status &= sport->port.read_status_mask;
  179. if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR))
  180. flg = TTY_PARITY;
  181. else if (status & FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE))
  182. flg = TTY_FRAME;
  183. #ifdef SUPPORT_SYSRQ
  184. sport->port.sysrq = 0;
  185. #endif
  186. }
  187. if (uart_handle_sysrq_char(&sport->port, ch))
  188. goto ignore_char;
  189. uart_insert_char(&sport->port, status,
  190. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN), ch, flg);
  191. ignore_char:
  192. serial_out(sport, PNX8XXX_LCR, serial_in(sport, PNX8XXX_LCR) |
  193. PNX8XXX_UART_LCR_RX_NEXT);
  194. status = FIFO_TO_SM(serial_in(sport, PNX8XXX_FIFO)) |
  195. ISTAT_TO_SM(serial_in(sport, PNX8XXX_ISTAT));
  196. }
  197. tty_flip_buffer_push(&sport->port.state->port);
  198. }
  199. static void pnx8xxx_tx_chars(struct pnx8xxx_port *sport)
  200. {
  201. struct circ_buf *xmit = &sport->port.state->xmit;
  202. if (sport->port.x_char) {
  203. serial_out(sport, PNX8XXX_FIFO, sport->port.x_char);
  204. sport->port.icount.tx++;
  205. sport->port.x_char = 0;
  206. return;
  207. }
  208. /*
  209. * Check the modem control lines before
  210. * transmitting anything.
  211. */
  212. pnx8xxx_mctrl_check(sport);
  213. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  214. pnx8xxx_stop_tx(&sport->port);
  215. return;
  216. }
  217. /*
  218. * TX while bytes available
  219. */
  220. while (((serial_in(sport, PNX8XXX_FIFO) &
  221. PNX8XXX_UART_FIFO_TXFIFO) >> 16) < 16) {
  222. serial_out(sport, PNX8XXX_FIFO, xmit->buf[xmit->tail]);
  223. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  224. sport->port.icount.tx++;
  225. if (uart_circ_empty(xmit))
  226. break;
  227. }
  228. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  229. uart_write_wakeup(&sport->port);
  230. if (uart_circ_empty(xmit))
  231. pnx8xxx_stop_tx(&sport->port);
  232. }
  233. static irqreturn_t pnx8xxx_int(int irq, void *dev_id)
  234. {
  235. struct pnx8xxx_port *sport = dev_id;
  236. unsigned int status;
  237. spin_lock(&sport->port.lock);
  238. /* Get the interrupts */
  239. status = serial_in(sport, PNX8XXX_ISTAT) & serial_in(sport, PNX8XXX_IEN);
  240. /* Byte or break signal received */
  241. if (status & (PNX8XXX_UART_INT_RX | PNX8XXX_UART_INT_BREAK))
  242. pnx8xxx_rx_chars(sport);
  243. /* TX holding register empty - transmit a byte */
  244. if (status & PNX8XXX_UART_INT_TX)
  245. pnx8xxx_tx_chars(sport);
  246. /* Clear the ISTAT register */
  247. serial_out(sport, PNX8XXX_ICLR, status);
  248. spin_unlock(&sport->port.lock);
  249. return IRQ_HANDLED;
  250. }
  251. /*
  252. * Return TIOCSER_TEMT when transmitter is not busy.
  253. */
  254. static unsigned int pnx8xxx_tx_empty(struct uart_port *port)
  255. {
  256. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  257. return serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA ? 0 : TIOCSER_TEMT;
  258. }
  259. static unsigned int pnx8xxx_get_mctrl(struct uart_port *port)
  260. {
  261. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  262. unsigned int mctrl = TIOCM_DSR;
  263. unsigned int msr;
  264. /* REVISIT */
  265. msr = serial_in(sport, PNX8XXX_MCR);
  266. mctrl |= msr & PNX8XXX_UART_MCR_CTS ? TIOCM_CTS : 0;
  267. mctrl |= msr & PNX8XXX_UART_MCR_DCD ? TIOCM_CAR : 0;
  268. return mctrl;
  269. }
  270. static void pnx8xxx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  271. {
  272. #if 0 /* FIXME */
  273. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  274. unsigned int msr;
  275. #endif
  276. }
  277. /*
  278. * Interrupts always disabled.
  279. */
  280. static void pnx8xxx_break_ctl(struct uart_port *port, int break_state)
  281. {
  282. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  283. unsigned long flags;
  284. unsigned int lcr;
  285. spin_lock_irqsave(&sport->port.lock, flags);
  286. lcr = serial_in(sport, PNX8XXX_LCR);
  287. if (break_state == -1)
  288. lcr |= PNX8XXX_UART_LCR_TXBREAK;
  289. else
  290. lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
  291. serial_out(sport, PNX8XXX_LCR, lcr);
  292. spin_unlock_irqrestore(&sport->port.lock, flags);
  293. }
  294. static int pnx8xxx_startup(struct uart_port *port)
  295. {
  296. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  297. int retval;
  298. /*
  299. * Allocate the IRQ
  300. */
  301. retval = request_irq(sport->port.irq, pnx8xxx_int, 0,
  302. "pnx8xxx-uart", sport);
  303. if (retval)
  304. return retval;
  305. /*
  306. * Finally, clear and enable interrupts
  307. */
  308. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
  309. PNX8XXX_UART_INT_ALLTX);
  310. serial_out(sport, PNX8XXX_IEN, serial_in(sport, PNX8XXX_IEN) |
  311. PNX8XXX_UART_INT_ALLRX |
  312. PNX8XXX_UART_INT_ALLTX);
  313. /*
  314. * Enable modem status interrupts
  315. */
  316. spin_lock_irq(&sport->port.lock);
  317. pnx8xxx_enable_ms(&sport->port);
  318. spin_unlock_irq(&sport->port.lock);
  319. return 0;
  320. }
  321. static void pnx8xxx_shutdown(struct uart_port *port)
  322. {
  323. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  324. int lcr;
  325. /*
  326. * Stop our timer.
  327. */
  328. del_timer_sync(&sport->timer);
  329. /*
  330. * Disable all interrupts
  331. */
  332. serial_out(sport, PNX8XXX_IEN, 0);
  333. /*
  334. * Reset the Tx and Rx FIFOS, disable the break condition
  335. */
  336. lcr = serial_in(sport, PNX8XXX_LCR);
  337. lcr &= ~PNX8XXX_UART_LCR_TXBREAK;
  338. lcr |= PNX8XXX_UART_LCR_TX_RST | PNX8XXX_UART_LCR_RX_RST;
  339. serial_out(sport, PNX8XXX_LCR, lcr);
  340. /*
  341. * Clear all interrupts
  342. */
  343. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_ALLRX |
  344. PNX8XXX_UART_INT_ALLTX);
  345. /*
  346. * Free the interrupt
  347. */
  348. free_irq(sport->port.irq, sport);
  349. }
  350. static void
  351. pnx8xxx_set_termios(struct uart_port *port, struct ktermios *termios,
  352. struct ktermios *old)
  353. {
  354. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  355. unsigned long flags;
  356. unsigned int lcr_fcr, old_ien, baud, quot;
  357. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  358. /*
  359. * We only support CS7 and CS8.
  360. */
  361. while ((termios->c_cflag & CSIZE) != CS7 &&
  362. (termios->c_cflag & CSIZE) != CS8) {
  363. termios->c_cflag &= ~CSIZE;
  364. termios->c_cflag |= old_csize;
  365. old_csize = CS8;
  366. }
  367. if ((termios->c_cflag & CSIZE) == CS8)
  368. lcr_fcr = PNX8XXX_UART_LCR_8BIT;
  369. else
  370. lcr_fcr = 0;
  371. if (termios->c_cflag & CSTOPB)
  372. lcr_fcr |= PNX8XXX_UART_LCR_2STOPB;
  373. if (termios->c_cflag & PARENB) {
  374. lcr_fcr |= PNX8XXX_UART_LCR_PAREN;
  375. if (!(termios->c_cflag & PARODD))
  376. lcr_fcr |= PNX8XXX_UART_LCR_PAREVN;
  377. }
  378. /*
  379. * Ask the core to calculate the divisor for us.
  380. */
  381. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  382. quot = uart_get_divisor(port, baud);
  383. spin_lock_irqsave(&sport->port.lock, flags);
  384. sport->port.read_status_mask = ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN) |
  385. ISTAT_TO_SM(PNX8XXX_UART_INT_EMPTY) |
  386. ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
  387. if (termios->c_iflag & INPCK)
  388. sport->port.read_status_mask |=
  389. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  390. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
  391. if (termios->c_iflag & (BRKINT | PARMRK))
  392. sport->port.read_status_mask |=
  393. ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
  394. /*
  395. * Characters to ignore
  396. */
  397. sport->port.ignore_status_mask = 0;
  398. if (termios->c_iflag & IGNPAR)
  399. sport->port.ignore_status_mask |=
  400. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXFE) |
  401. FIFO_TO_SM(PNX8XXX_UART_FIFO_RXPAR);
  402. if (termios->c_iflag & IGNBRK) {
  403. sport->port.ignore_status_mask |=
  404. ISTAT_TO_SM(PNX8XXX_UART_INT_BREAK);
  405. /*
  406. * If we're ignoring parity and break indicators,
  407. * ignore overruns too (for real raw support).
  408. */
  409. if (termios->c_iflag & IGNPAR)
  410. sport->port.ignore_status_mask |=
  411. ISTAT_TO_SM(PNX8XXX_UART_INT_RXOVRN);
  412. }
  413. /*
  414. * ignore all characters if CREAD is not set
  415. */
  416. if ((termios->c_cflag & CREAD) == 0)
  417. sport->port.ignore_status_mask |=
  418. ISTAT_TO_SM(PNX8XXX_UART_INT_RX);
  419. del_timer_sync(&sport->timer);
  420. /*
  421. * Update the per-port timeout.
  422. */
  423. uart_update_timeout(port, termios->c_cflag, baud);
  424. /*
  425. * disable interrupts and drain transmitter
  426. */
  427. old_ien = serial_in(sport, PNX8XXX_IEN);
  428. serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
  429. PNX8XXX_UART_INT_ALLRX));
  430. while (serial_in(sport, PNX8XXX_FIFO) & PNX8XXX_UART_FIFO_TXFIFO_STA)
  431. barrier();
  432. /* then, disable everything */
  433. serial_out(sport, PNX8XXX_IEN, 0);
  434. /* Reset the Rx and Tx FIFOs too */
  435. lcr_fcr |= PNX8XXX_UART_LCR_TX_RST;
  436. lcr_fcr |= PNX8XXX_UART_LCR_RX_RST;
  437. /* set the parity, stop bits and data size */
  438. serial_out(sport, PNX8XXX_LCR, lcr_fcr);
  439. /* set the baud rate */
  440. quot -= 1;
  441. serial_out(sport, PNX8XXX_BAUD, quot);
  442. serial_out(sport, PNX8XXX_ICLR, -1);
  443. serial_out(sport, PNX8XXX_IEN, old_ien);
  444. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  445. pnx8xxx_enable_ms(&sport->port);
  446. spin_unlock_irqrestore(&sport->port.lock, flags);
  447. }
  448. static const char *pnx8xxx_type(struct uart_port *port)
  449. {
  450. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  451. return sport->port.type == PORT_PNX8XXX ? "PNX8XXX" : NULL;
  452. }
  453. /*
  454. * Release the memory region(s) being used by 'port'.
  455. */
  456. static void pnx8xxx_release_port(struct uart_port *port)
  457. {
  458. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  459. release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
  460. }
  461. /*
  462. * Request the memory region(s) being used by 'port'.
  463. */
  464. static int pnx8xxx_request_port(struct uart_port *port)
  465. {
  466. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  467. return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
  468. "pnx8xxx-uart") != NULL ? 0 : -EBUSY;
  469. }
  470. /*
  471. * Configure/autoconfigure the port.
  472. */
  473. static void pnx8xxx_config_port(struct uart_port *port, int flags)
  474. {
  475. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  476. if (flags & UART_CONFIG_TYPE &&
  477. pnx8xxx_request_port(&sport->port) == 0)
  478. sport->port.type = PORT_PNX8XXX;
  479. }
  480. /*
  481. * Verify the new serial_struct (for TIOCSSERIAL).
  482. * The only change we allow are to the flags and type, and
  483. * even then only between PORT_PNX8XXX and PORT_UNKNOWN
  484. */
  485. static int
  486. pnx8xxx_verify_port(struct uart_port *port, struct serial_struct *ser)
  487. {
  488. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  489. int ret = 0;
  490. if (ser->type != PORT_UNKNOWN && ser->type != PORT_PNX8XXX)
  491. ret = -EINVAL;
  492. if (sport->port.irq != ser->irq)
  493. ret = -EINVAL;
  494. if (ser->io_type != SERIAL_IO_MEM)
  495. ret = -EINVAL;
  496. if (sport->port.uartclk / 16 != ser->baud_base)
  497. ret = -EINVAL;
  498. if ((void *)sport->port.mapbase != ser->iomem_base)
  499. ret = -EINVAL;
  500. if (sport->port.iobase != ser->port)
  501. ret = -EINVAL;
  502. if (ser->hub6 != 0)
  503. ret = -EINVAL;
  504. return ret;
  505. }
  506. static struct uart_ops pnx8xxx_pops = {
  507. .tx_empty = pnx8xxx_tx_empty,
  508. .set_mctrl = pnx8xxx_set_mctrl,
  509. .get_mctrl = pnx8xxx_get_mctrl,
  510. .stop_tx = pnx8xxx_stop_tx,
  511. .start_tx = pnx8xxx_start_tx,
  512. .stop_rx = pnx8xxx_stop_rx,
  513. .enable_ms = pnx8xxx_enable_ms,
  514. .break_ctl = pnx8xxx_break_ctl,
  515. .startup = pnx8xxx_startup,
  516. .shutdown = pnx8xxx_shutdown,
  517. .set_termios = pnx8xxx_set_termios,
  518. .type = pnx8xxx_type,
  519. .release_port = pnx8xxx_release_port,
  520. .request_port = pnx8xxx_request_port,
  521. .config_port = pnx8xxx_config_port,
  522. .verify_port = pnx8xxx_verify_port,
  523. };
  524. /*
  525. * Setup the PNX8XXX serial ports.
  526. *
  527. * Note also that we support "console=ttySx" where "x" is either 0 or 1.
  528. */
  529. static void __init pnx8xxx_init_ports(void)
  530. {
  531. static int first = 1;
  532. int i;
  533. if (!first)
  534. return;
  535. first = 0;
  536. for (i = 0; i < NR_PORTS; i++) {
  537. init_timer(&pnx8xxx_ports[i].timer);
  538. pnx8xxx_ports[i].timer.function = pnx8xxx_timeout;
  539. pnx8xxx_ports[i].timer.data = (unsigned long)&pnx8xxx_ports[i];
  540. pnx8xxx_ports[i].port.ops = &pnx8xxx_pops;
  541. }
  542. }
  543. #ifdef CONFIG_SERIAL_PNX8XXX_CONSOLE
  544. static void pnx8xxx_console_putchar(struct uart_port *port, int ch)
  545. {
  546. struct pnx8xxx_port *sport = (struct pnx8xxx_port *)port;
  547. int status;
  548. do {
  549. /* Wait for UART_TX register to empty */
  550. status = serial_in(sport, PNX8XXX_FIFO);
  551. } while (status & PNX8XXX_UART_FIFO_TXFIFO);
  552. serial_out(sport, PNX8XXX_FIFO, ch);
  553. }
  554. /*
  555. * Interrupts are disabled on entering
  556. */static void
  557. pnx8xxx_console_write(struct console *co, const char *s, unsigned int count)
  558. {
  559. struct pnx8xxx_port *sport = &pnx8xxx_ports[co->index];
  560. unsigned int old_ien, status;
  561. /*
  562. * First, save IEN and then disable interrupts
  563. */
  564. old_ien = serial_in(sport, PNX8XXX_IEN);
  565. serial_out(sport, PNX8XXX_IEN, old_ien & ~(PNX8XXX_UART_INT_ALLTX |
  566. PNX8XXX_UART_INT_ALLRX));
  567. uart_console_write(&sport->port, s, count, pnx8xxx_console_putchar);
  568. /*
  569. * Finally, wait for transmitter to become empty
  570. * and restore IEN
  571. */
  572. do {
  573. /* Wait for UART_TX register to empty */
  574. status = serial_in(sport, PNX8XXX_FIFO);
  575. } while (status & PNX8XXX_UART_FIFO_TXFIFO);
  576. /* Clear TX and EMPTY interrupt */
  577. serial_out(sport, PNX8XXX_ICLR, PNX8XXX_UART_INT_TX |
  578. PNX8XXX_UART_INT_EMPTY);
  579. serial_out(sport, PNX8XXX_IEN, old_ien);
  580. }
  581. static int __init
  582. pnx8xxx_console_setup(struct console *co, char *options)
  583. {
  584. struct pnx8xxx_port *sport;
  585. int baud = 38400;
  586. int bits = 8;
  587. int parity = 'n';
  588. int flow = 'n';
  589. /*
  590. * Check whether an invalid uart number has been specified, and
  591. * if so, search for the first available port that does have
  592. * console support.
  593. */
  594. if (co->index == -1 || co->index >= NR_PORTS)
  595. co->index = 0;
  596. sport = &pnx8xxx_ports[co->index];
  597. if (options)
  598. uart_parse_options(options, &baud, &parity, &bits, &flow);
  599. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  600. }
  601. static struct uart_driver pnx8xxx_reg;
  602. static struct console pnx8xxx_console = {
  603. .name = "ttyS",
  604. .write = pnx8xxx_console_write,
  605. .device = uart_console_device,
  606. .setup = pnx8xxx_console_setup,
  607. .flags = CON_PRINTBUFFER,
  608. .index = -1,
  609. .data = &pnx8xxx_reg,
  610. };
  611. static int __init pnx8xxx_rs_console_init(void)
  612. {
  613. pnx8xxx_init_ports();
  614. register_console(&pnx8xxx_console);
  615. return 0;
  616. }
  617. console_initcall(pnx8xxx_rs_console_init);
  618. #define PNX8XXX_CONSOLE &pnx8xxx_console
  619. #else
  620. #define PNX8XXX_CONSOLE NULL
  621. #endif
  622. static struct uart_driver pnx8xxx_reg = {
  623. .owner = THIS_MODULE,
  624. .driver_name = "ttyS",
  625. .dev_name = "ttyS",
  626. .major = SERIAL_PNX8XXX_MAJOR,
  627. .minor = MINOR_START,
  628. .nr = NR_PORTS,
  629. .cons = PNX8XXX_CONSOLE,
  630. };
  631. static int pnx8xxx_serial_suspend(struct platform_device *pdev, pm_message_t state)
  632. {
  633. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  634. return uart_suspend_port(&pnx8xxx_reg, &sport->port);
  635. }
  636. static int pnx8xxx_serial_resume(struct platform_device *pdev)
  637. {
  638. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  639. return uart_resume_port(&pnx8xxx_reg, &sport->port);
  640. }
  641. static int pnx8xxx_serial_probe(struct platform_device *pdev)
  642. {
  643. struct resource *res = pdev->resource;
  644. int i;
  645. for (i = 0; i < pdev->num_resources; i++, res++) {
  646. if (!(res->flags & IORESOURCE_MEM))
  647. continue;
  648. for (i = 0; i < NR_PORTS; i++) {
  649. if (pnx8xxx_ports[i].port.mapbase != res->start)
  650. continue;
  651. pnx8xxx_ports[i].port.dev = &pdev->dev;
  652. uart_add_one_port(&pnx8xxx_reg, &pnx8xxx_ports[i].port);
  653. platform_set_drvdata(pdev, &pnx8xxx_ports[i]);
  654. break;
  655. }
  656. }
  657. return 0;
  658. }
  659. static int pnx8xxx_serial_remove(struct platform_device *pdev)
  660. {
  661. struct pnx8xxx_port *sport = platform_get_drvdata(pdev);
  662. platform_set_drvdata(pdev, NULL);
  663. if (sport)
  664. uart_remove_one_port(&pnx8xxx_reg, &sport->port);
  665. return 0;
  666. }
  667. static struct platform_driver pnx8xxx_serial_driver = {
  668. .driver = {
  669. .name = "pnx8xxx-uart",
  670. .owner = THIS_MODULE,
  671. },
  672. .probe = pnx8xxx_serial_probe,
  673. .remove = pnx8xxx_serial_remove,
  674. .suspend = pnx8xxx_serial_suspend,
  675. .resume = pnx8xxx_serial_resume,
  676. };
  677. static int __init pnx8xxx_serial_init(void)
  678. {
  679. int ret;
  680. printk(KERN_INFO "Serial: PNX8XXX driver\n");
  681. pnx8xxx_init_ports();
  682. ret = uart_register_driver(&pnx8xxx_reg);
  683. if (ret == 0) {
  684. ret = platform_driver_register(&pnx8xxx_serial_driver);
  685. if (ret)
  686. uart_unregister_driver(&pnx8xxx_reg);
  687. }
  688. return ret;
  689. }
  690. static void __exit pnx8xxx_serial_exit(void)
  691. {
  692. platform_driver_unregister(&pnx8xxx_serial_driver);
  693. uart_unregister_driver(&pnx8xxx_reg);
  694. }
  695. module_init(pnx8xxx_serial_init);
  696. module_exit(pnx8xxx_serial_exit);
  697. MODULE_AUTHOR("Embedded Alley Solutions, Inc.");
  698. MODULE_DESCRIPTION("PNX8XXX SoCs serial port driver");
  699. MODULE_LICENSE("GPL");
  700. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_PNX8XXX_MAJOR);
  701. MODULE_ALIAS("platform:pnx8xxx-uart");