mpc52xx_uart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * drivers/serial/mpc52xx_uart.c
  3. *
  4. * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
  5. *
  6. * FIXME According to the usermanual the status bits in the status register
  7. * are only updated when the peripherals access the FIFO and not when the
  8. * CPU access them. So since we use this bits to know when we stop writing
  9. * and reading, they may not be updated in-time and a race condition may
  10. * exists. But I haven't be able to prove this and I don't care. But if
  11. * any problem arises, it might worth checking. The TX/RX FIFO Stats
  12. * registers should be used in addition.
  13. * Update: Actually, they seem updated ... At least the bits we use.
  14. *
  15. *
  16. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  17. *
  18. * Some of the code has been inspired/copied from the 2.4 code written
  19. * by Dale Farnsworth <dfarnsworth@mvista.com>.
  20. *
  21. * Copyright (C) 2004-2005 Sylvain Munaut <tnt@246tNt.com>
  22. * Copyright (C) 2003 MontaVista, Software, Inc.
  23. *
  24. * This file is licensed under the terms of the GNU General Public License
  25. * version 2. This program is licensed "as is" without any warranty of any
  26. * kind, whether express or implied.
  27. */
  28. /* Platform device Usage :
  29. *
  30. * Since PSCs can have multiple function, the correct driver for each one
  31. * is selected by calling mpc52xx_match_psc_function(...). The function
  32. * handled by this driver is "uart".
  33. *
  34. * The driver init all necessary registers to place the PSC in uart mode without
  35. * DCD. However, the pin multiplexing aren't changed and should be set either
  36. * by the bootloader or in the platform init code.
  37. *
  38. * The idx field must be equal to the PSC index ( e.g. 0 for PSC1, 1 for PSC2,
  39. * and so on). So the PSC1 is mapped to /dev/ttyS0, PSC2 to /dev/ttyS1 and so
  40. * on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly for
  41. * the console code : without this 1:1 mapping, at early boot time, when we are
  42. * parsing the kernel args console=ttyS?, we wouldn't know wich PSC it will be
  43. * mapped to.
  44. */
  45. #include <linux/config.h>
  46. #include <linux/device.h>
  47. #include <linux/module.h>
  48. #include <linux/tty.h>
  49. #include <linux/serial.h>
  50. #include <linux/sysrq.h>
  51. #include <linux/console.h>
  52. #include <asm/delay.h>
  53. #include <asm/io.h>
  54. #include <asm/mpc52xx.h>
  55. #include <asm/mpc52xx_psc.h>
  56. #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  57. #define SUPPORT_SYSRQ
  58. #endif
  59. #include <linux/serial_core.h>
  60. #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
  61. static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
  62. /* Rem: - We use the read_status_mask as a shadow of
  63. * psc->mpc52xx_psc_imr
  64. * - It's important that is array is all zero on start as we
  65. * use it to know if it's initialized or not ! If it's not sure
  66. * it's cleared, then a memset(...,0,...) should be added to
  67. * the console_init
  68. */
  69. #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
  70. /* Forward declaration of the interruption handling routine */
  71. static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id,struct pt_regs *regs);
  72. /* Simple macro to test if a port is console or not. This one is taken
  73. * for serial_core.c and maybe should be moved to serial_core.h ? */
  74. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  75. #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
  76. #else
  77. #define uart_console(port) (0)
  78. #endif
  79. /* ======================================================================== */
  80. /* UART operations */
  81. /* ======================================================================== */
  82. static unsigned int
  83. mpc52xx_uart_tx_empty(struct uart_port *port)
  84. {
  85. int status = in_be16(&PSC(port)->mpc52xx_psc_status);
  86. return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  87. }
  88. static void
  89. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  90. {
  91. /* Not implemented */
  92. }
  93. static unsigned int
  94. mpc52xx_uart_get_mctrl(struct uart_port *port)
  95. {
  96. /* Not implemented */
  97. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  98. }
  99. static void
  100. mpc52xx_uart_stop_tx(struct uart_port *port)
  101. {
  102. /* port->lock taken by caller */
  103. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  104. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  105. }
  106. static void
  107. mpc52xx_uart_start_tx(struct uart_port *port)
  108. {
  109. /* port->lock taken by caller */
  110. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  111. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  112. }
  113. static void
  114. mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
  115. {
  116. unsigned long flags;
  117. spin_lock_irqsave(&port->lock, flags);
  118. port->x_char = ch;
  119. if (ch) {
  120. /* Make sure tx interrupts are on */
  121. /* Truly necessary ??? They should be anyway */
  122. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  123. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  124. }
  125. spin_unlock_irqrestore(&port->lock, flags);
  126. }
  127. static void
  128. mpc52xx_uart_stop_rx(struct uart_port *port)
  129. {
  130. /* port->lock taken by caller */
  131. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  132. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  133. }
  134. static void
  135. mpc52xx_uart_enable_ms(struct uart_port *port)
  136. {
  137. /* Not implemented */
  138. }
  139. static void
  140. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  141. {
  142. unsigned long flags;
  143. spin_lock_irqsave(&port->lock, flags);
  144. if ( ctl == -1 )
  145. out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
  146. else
  147. out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
  148. spin_unlock_irqrestore(&port->lock, flags);
  149. }
  150. static int
  151. mpc52xx_uart_startup(struct uart_port *port)
  152. {
  153. struct mpc52xx_psc __iomem *psc = PSC(port);
  154. int ret;
  155. /* Request IRQ */
  156. ret = request_irq(port->irq, mpc52xx_uart_int,
  157. SA_INTERRUPT | SA_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
  158. if (ret)
  159. return ret;
  160. /* Reset/activate the port, clear and enable interrupts */
  161. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  162. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  163. out_be32(&psc->sicr,0); /* UART mode DCD ignored */
  164. out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
  165. out_8(&psc->rfcntl, 0x00);
  166. out_be16(&psc->rfalarm, 0x1ff);
  167. out_8(&psc->tfcntl, 0x07);
  168. out_be16(&psc->tfalarm, 0x80);
  169. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  170. out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
  171. out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
  172. out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
  173. return 0;
  174. }
  175. static void
  176. mpc52xx_uart_shutdown(struct uart_port *port)
  177. {
  178. struct mpc52xx_psc __iomem *psc = PSC(port);
  179. /* Shut down the port, interrupt and all */
  180. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  181. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  182. port->read_status_mask = 0;
  183. out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
  184. /* Release interrupt */
  185. free_irq(port->irq, port);
  186. }
  187. static void
  188. mpc52xx_uart_set_termios(struct uart_port *port, struct termios *new,
  189. struct termios *old)
  190. {
  191. struct mpc52xx_psc __iomem *psc = PSC(port);
  192. unsigned long flags;
  193. unsigned char mr1, mr2;
  194. unsigned short ctr;
  195. unsigned int j, baud, quot;
  196. /* Prepare what we're gonna write */
  197. mr1 = 0;
  198. switch (new->c_cflag & CSIZE) {
  199. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  200. break;
  201. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  202. break;
  203. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  204. break;
  205. case CS8:
  206. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  207. }
  208. if (new->c_cflag & PARENB) {
  209. mr1 |= (new->c_cflag & PARODD) ?
  210. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  211. } else
  212. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  213. mr2 = 0;
  214. if (new->c_cflag & CSTOPB)
  215. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  216. else
  217. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  218. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  219. MPC52xx_PSC_MODE_ONE_STOP;
  220. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  221. quot = uart_get_divisor(port, baud);
  222. ctr = quot & 0xffff;
  223. /* Get the lock */
  224. spin_lock_irqsave(&port->lock, flags);
  225. /* Update the per-port timeout */
  226. uart_update_timeout(port, new->c_cflag, baud);
  227. /* Do our best to flush TX & RX, so we don't loose anything */
  228. /* But we don't wait indefinitly ! */
  229. j = 5000000; /* Maximum wait */
  230. /* FIXME Can't receive chars since set_termios might be called at early
  231. * boot for the console, all stuff is not yet ready to receive at that
  232. * time and that just makes the kernel oops */
  233. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  234. while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
  235. --j)
  236. udelay(1);
  237. if (!j)
  238. printk( KERN_ERR "mpc52xx_uart.c: "
  239. "Unable to flush RX & TX fifos in-time in set_termios."
  240. "Some chars may have been lost.\n" );
  241. /* Reset the TX & RX */
  242. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  243. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  244. /* Send new mode settings */
  245. out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
  246. out_8(&psc->mode,mr1);
  247. out_8(&psc->mode,mr2);
  248. out_8(&psc->ctur,ctr >> 8);
  249. out_8(&psc->ctlr,ctr & 0xff);
  250. /* Reenable TX & RX */
  251. out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
  252. out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
  253. /* We're all set, release the lock */
  254. spin_unlock_irqrestore(&port->lock, flags);
  255. }
  256. static const char *
  257. mpc52xx_uart_type(struct uart_port *port)
  258. {
  259. return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
  260. }
  261. static void
  262. mpc52xx_uart_release_port(struct uart_port *port)
  263. {
  264. if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
  265. iounmap(port->membase);
  266. port->membase = NULL;
  267. }
  268. release_mem_region(port->mapbase, MPC52xx_PSC_SIZE);
  269. }
  270. static int
  271. mpc52xx_uart_request_port(struct uart_port *port)
  272. {
  273. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  274. port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
  275. if (!port->membase)
  276. return -EINVAL;
  277. return request_mem_region(port->mapbase, MPC52xx_PSC_SIZE,
  278. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  279. }
  280. static void
  281. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  282. {
  283. if ( (flags & UART_CONFIG_TYPE) &&
  284. (mpc52xx_uart_request_port(port) == 0) )
  285. port->type = PORT_MPC52xx;
  286. }
  287. static int
  288. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  289. {
  290. if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
  291. return -EINVAL;
  292. if ( (ser->irq != port->irq) ||
  293. (ser->io_type != SERIAL_IO_MEM) ||
  294. (ser->baud_base != port->uartclk) ||
  295. (ser->iomem_base != (void*)port->mapbase) ||
  296. (ser->hub6 != 0 ) )
  297. return -EINVAL;
  298. return 0;
  299. }
  300. static struct uart_ops mpc52xx_uart_ops = {
  301. .tx_empty = mpc52xx_uart_tx_empty,
  302. .set_mctrl = mpc52xx_uart_set_mctrl,
  303. .get_mctrl = mpc52xx_uart_get_mctrl,
  304. .stop_tx = mpc52xx_uart_stop_tx,
  305. .start_tx = mpc52xx_uart_start_tx,
  306. .send_xchar = mpc52xx_uart_send_xchar,
  307. .stop_rx = mpc52xx_uart_stop_rx,
  308. .enable_ms = mpc52xx_uart_enable_ms,
  309. .break_ctl = mpc52xx_uart_break_ctl,
  310. .startup = mpc52xx_uart_startup,
  311. .shutdown = mpc52xx_uart_shutdown,
  312. .set_termios = mpc52xx_uart_set_termios,
  313. /* .pm = mpc52xx_uart_pm, Not supported yet */
  314. /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
  315. .type = mpc52xx_uart_type,
  316. .release_port = mpc52xx_uart_release_port,
  317. .request_port = mpc52xx_uart_request_port,
  318. .config_port = mpc52xx_uart_config_port,
  319. .verify_port = mpc52xx_uart_verify_port
  320. };
  321. /* ======================================================================== */
  322. /* Interrupt handling */
  323. /* ======================================================================== */
  324. static inline int
  325. mpc52xx_uart_int_rx_chars(struct uart_port *port, struct pt_regs *regs)
  326. {
  327. struct tty_struct *tty = port->info->tty;
  328. unsigned char ch;
  329. unsigned short status;
  330. /* While we can read, do so ! */
  331. while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
  332. MPC52xx_PSC_SR_RXRDY) {
  333. /* If we are full, just stop reading */
  334. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  335. break;
  336. /* Get the char */
  337. ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  338. /* Handle sysreq char */
  339. #ifdef SUPPORT_SYSRQ
  340. if (uart_handle_sysrq_char(port, ch, regs)) {
  341. port->sysrq = 0;
  342. continue;
  343. }
  344. #endif
  345. /* Store it */
  346. *tty->flip.char_buf_ptr = ch;
  347. *tty->flip.flag_buf_ptr = 0;
  348. port->icount.rx++;
  349. if ( status & (MPC52xx_PSC_SR_PE |
  350. MPC52xx_PSC_SR_FE |
  351. MPC52xx_PSC_SR_RB |
  352. MPC52xx_PSC_SR_OE) ) {
  353. if (status & MPC52xx_PSC_SR_RB) {
  354. *tty->flip.flag_buf_ptr = TTY_BREAK;
  355. uart_handle_break(port);
  356. } else if (status & MPC52xx_PSC_SR_PE)
  357. *tty->flip.flag_buf_ptr = TTY_PARITY;
  358. else if (status & MPC52xx_PSC_SR_FE)
  359. *tty->flip.flag_buf_ptr = TTY_FRAME;
  360. if (status & MPC52xx_PSC_SR_OE) {
  361. /*
  362. * Overrun is special, since it's
  363. * reported immediately, and doesn't
  364. * affect the current character
  365. */
  366. if (tty->flip.count < (TTY_FLIPBUF_SIZE-1)) {
  367. tty->flip.flag_buf_ptr++;
  368. tty->flip.char_buf_ptr++;
  369. tty->flip.count++;
  370. }
  371. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  372. }
  373. /* Clear error condition */
  374. out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
  375. }
  376. tty->flip.char_buf_ptr++;
  377. tty->flip.flag_buf_ptr++;
  378. tty->flip.count++;
  379. }
  380. tty_flip_buffer_push(tty);
  381. return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
  382. }
  383. static inline int
  384. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  385. {
  386. struct circ_buf *xmit = &port->info->xmit;
  387. /* Process out of band chars */
  388. if (port->x_char) {
  389. out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
  390. port->icount.tx++;
  391. port->x_char = 0;
  392. return 1;
  393. }
  394. /* Nothing to do ? */
  395. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  396. mpc52xx_uart_stop_tx(port);
  397. return 0;
  398. }
  399. /* Send chars */
  400. while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
  401. out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
  402. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  403. port->icount.tx++;
  404. if (uart_circ_empty(xmit))
  405. break;
  406. }
  407. /* Wake up */
  408. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  409. uart_write_wakeup(port);
  410. /* Maybe we're done after all */
  411. if (uart_circ_empty(xmit)) {
  412. mpc52xx_uart_stop_tx(port);
  413. return 0;
  414. }
  415. return 1;
  416. }
  417. static irqreturn_t
  418. mpc52xx_uart_int(int irq, void *dev_id, struct pt_regs *regs)
  419. {
  420. struct uart_port *port = (struct uart_port *) dev_id;
  421. unsigned long pass = ISR_PASS_LIMIT;
  422. unsigned int keepgoing;
  423. unsigned short status;
  424. if ( irq != port->irq ) {
  425. printk( KERN_WARNING
  426. "mpc52xx_uart_int : " \
  427. "Received wrong int %d. Waiting for %d\n",
  428. irq, port->irq);
  429. return IRQ_NONE;
  430. }
  431. spin_lock(&port->lock);
  432. /* While we have stuff to do, we continue */
  433. do {
  434. /* If we don't find anything to do, we stop */
  435. keepgoing = 0;
  436. /* Read status */
  437. status = in_be16(&PSC(port)->mpc52xx_psc_isr);
  438. status &= port->read_status_mask;
  439. /* Do we need to receive chars ? */
  440. /* For this RX interrupts must be on and some chars waiting */
  441. if ( status & MPC52xx_PSC_IMR_RXRDY )
  442. keepgoing |= mpc52xx_uart_int_rx_chars(port, regs);
  443. /* Do we need to send chars ? */
  444. /* For this, TX must be ready and TX interrupt enabled */
  445. if ( status & MPC52xx_PSC_IMR_TXRDY )
  446. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  447. /* Limit number of iteration */
  448. if ( !(--pass) )
  449. keepgoing = 0;
  450. } while (keepgoing);
  451. spin_unlock(&port->lock);
  452. return IRQ_HANDLED;
  453. }
  454. /* ======================================================================== */
  455. /* Console ( if applicable ) */
  456. /* ======================================================================== */
  457. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  458. static void __init
  459. mpc52xx_console_get_options(struct uart_port *port,
  460. int *baud, int *parity, int *bits, int *flow)
  461. {
  462. struct mpc52xx_psc __iomem *psc = PSC(port);
  463. unsigned char mr1;
  464. /* Read the mode registers */
  465. out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
  466. mr1 = in_8(&psc->mode);
  467. /* CT{U,L}R are write-only ! */
  468. *baud = __res.bi_baudrate ?
  469. __res.bi_baudrate : CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  470. /* Parse them */
  471. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  472. case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
  473. case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
  474. case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
  475. case MPC52xx_PSC_MODE_8_BITS:
  476. default: *bits = 8;
  477. }
  478. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  479. *parity = 'n';
  480. else
  481. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  482. }
  483. static void
  484. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  485. {
  486. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  487. struct mpc52xx_psc __iomem *psc = PSC(port);
  488. unsigned int i, j;
  489. /* Disable interrupts */
  490. out_be16(&psc->mpc52xx_psc_imr, 0);
  491. /* Wait the TX buffer to be empty */
  492. j = 5000000; /* Maximum wait */
  493. while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
  494. --j)
  495. udelay(1);
  496. /* Write all the chars */
  497. for ( i=0 ; i<count ; i++ ) {
  498. /* Send the char */
  499. out_8(&psc->mpc52xx_psc_buffer_8, *s);
  500. /* Line return handling */
  501. if ( *s++ == '\n' )
  502. out_8(&psc->mpc52xx_psc_buffer_8, '\r');
  503. /* Wait the TX buffer to be empty */
  504. j = 20000; /* Maximum wait */
  505. while (!(in_be16(&psc->mpc52xx_psc_status) &
  506. MPC52xx_PSC_SR_TXEMP) && --j)
  507. udelay(1);
  508. }
  509. /* Restore interrupt state */
  510. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  511. }
  512. static int __init
  513. mpc52xx_console_setup(struct console *co, char *options)
  514. {
  515. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  516. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  517. int bits = 8;
  518. int parity = 'n';
  519. int flow = 'n';
  520. if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
  521. return -EINVAL;
  522. /* Basic port init. Needed since we use some uart_??? func before
  523. * real init for early access */
  524. spin_lock_init(&port->lock);
  525. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  526. port->ops = &mpc52xx_uart_ops;
  527. port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
  528. /* We ioremap ourself */
  529. port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
  530. if (port->membase == NULL)
  531. return -EINVAL;
  532. /* Setup the port parameters accoding to options */
  533. if (options)
  534. uart_parse_options(options, &baud, &parity, &bits, &flow);
  535. else
  536. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  537. return uart_set_options(port, co, baud, parity, bits, flow);
  538. }
  539. extern struct uart_driver mpc52xx_uart_driver;
  540. static struct console mpc52xx_console = {
  541. .name = "ttyS",
  542. .write = mpc52xx_console_write,
  543. .device = uart_console_device,
  544. .setup = mpc52xx_console_setup,
  545. .flags = CON_PRINTBUFFER,
  546. .index = -1, /* Specified on the cmdline (e.g. console=ttyS0 ) */
  547. .data = &mpc52xx_uart_driver,
  548. };
  549. static int __init
  550. mpc52xx_console_init(void)
  551. {
  552. register_console(&mpc52xx_console);
  553. return 0;
  554. }
  555. console_initcall(mpc52xx_console_init);
  556. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  557. #else
  558. #define MPC52xx_PSC_CONSOLE NULL
  559. #endif
  560. /* ======================================================================== */
  561. /* UART Driver */
  562. /* ======================================================================== */
  563. static struct uart_driver mpc52xx_uart_driver = {
  564. .owner = THIS_MODULE,
  565. .driver_name = "mpc52xx_psc_uart",
  566. .dev_name = "ttyS",
  567. .devfs_name = "ttyS",
  568. .major = TTY_MAJOR,
  569. .minor = 64,
  570. .nr = MPC52xx_PSC_MAXNUM,
  571. .cons = MPC52xx_PSC_CONSOLE,
  572. };
  573. /* ======================================================================== */
  574. /* Platform Driver */
  575. /* ======================================================================== */
  576. static int __devinit
  577. mpc52xx_uart_probe(struct device *dev)
  578. {
  579. struct platform_device *pdev = to_platform_device(dev);
  580. struct resource *res = pdev->resource;
  581. struct uart_port *port = NULL;
  582. int i, idx, ret;
  583. /* Check validity & presence */
  584. idx = pdev->id;
  585. if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
  586. return -EINVAL;
  587. if (!mpc52xx_match_psc_function(idx,"uart"))
  588. return -ENODEV;
  589. /* Init the port structure */
  590. port = &mpc52xx_uart_ports[idx];
  591. memset(port, 0x00, sizeof(struct uart_port));
  592. spin_lock_init(&port->lock);
  593. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  594. port->fifosize = 255; /* Should be 512 ! But it can't be */
  595. /* stored in a unsigned char */
  596. port->iotype = UPIO_MEM;
  597. port->flags = UPF_BOOT_AUTOCONF |
  598. ( uart_console(port) ? 0 : UPF_IOREMAP );
  599. port->line = idx;
  600. port->ops = &mpc52xx_uart_ops;
  601. /* Search for IRQ and mapbase */
  602. for (i=0 ; i<pdev->num_resources ; i++, res++) {
  603. if (res->flags & IORESOURCE_MEM)
  604. port->mapbase = res->start;
  605. else if (res->flags & IORESOURCE_IRQ)
  606. port->irq = res->start;
  607. }
  608. if (!port->irq || !port->mapbase)
  609. return -EINVAL;
  610. /* Add the port to the uart sub-system */
  611. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  612. if (!ret)
  613. dev_set_drvdata(dev, (void*)port);
  614. return ret;
  615. }
  616. static int
  617. mpc52xx_uart_remove(struct device *dev)
  618. {
  619. struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
  620. dev_set_drvdata(dev, NULL);
  621. if (port)
  622. uart_remove_one_port(&mpc52xx_uart_driver, port);
  623. return 0;
  624. }
  625. #ifdef CONFIG_PM
  626. static int
  627. mpc52xx_uart_suspend(struct device *dev, u32 state, u32 level)
  628. {
  629. struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
  630. if (sport && level == SUSPEND_DISABLE)
  631. uart_suspend_port(&mpc52xx_uart_driver, port);
  632. return 0;
  633. }
  634. static int
  635. mpc52xx_uart_resume(struct device *dev, u32 level)
  636. {
  637. struct uart_port *port = (struct uart_port *) dev_get_drvdata(dev);
  638. if (port && level == RESUME_ENABLE)
  639. uart_resume_port(&mpc52xx_uart_driver, port);
  640. return 0;
  641. }
  642. #endif
  643. static struct device_driver mpc52xx_uart_platform_driver = {
  644. .name = "mpc52xx-psc",
  645. .bus = &platform_bus_type,
  646. .probe = mpc52xx_uart_probe,
  647. .remove = mpc52xx_uart_remove,
  648. #ifdef CONFIG_PM
  649. .suspend = mpc52xx_uart_suspend,
  650. .resume = mpc52xx_uart_resume,
  651. #endif
  652. };
  653. /* ======================================================================== */
  654. /* Module */
  655. /* ======================================================================== */
  656. static int __init
  657. mpc52xx_uart_init(void)
  658. {
  659. int ret;
  660. printk(KERN_INFO "Serial: MPC52xx PSC driver\n");
  661. ret = uart_register_driver(&mpc52xx_uart_driver);
  662. if (ret == 0) {
  663. ret = driver_register(&mpc52xx_uart_platform_driver);
  664. if (ret)
  665. uart_unregister_driver(&mpc52xx_uart_driver);
  666. }
  667. return ret;
  668. }
  669. static void __exit
  670. mpc52xx_uart_exit(void)
  671. {
  672. driver_unregister(&mpc52xx_uart_platform_driver);
  673. uart_unregister_driver(&mpc52xx_uart_driver);
  674. }
  675. module_init(mpc52xx_uart_init);
  676. module_exit(mpc52xx_uart_exit);
  677. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  678. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  679. MODULE_LICENSE("GPL");