mpc52xx_uart.c 22 KB

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