pnx8xxx_uart.c 21 KB

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