pnx8xxx_uart.c 21 KB

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