mpc52xx_uart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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);
  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. int err;
  276. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  277. port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
  278. if (!port->membase)
  279. return -EINVAL;
  280. err = request_mem_region(port->mapbase, MPC52xx_PSC_SIZE,
  281. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  282. if (err && (port->flags & UPF_IOREMAP)) {
  283. iounmap(port->membase);
  284. port->membase = NULL;
  285. }
  286. return err;
  287. }
  288. static void
  289. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  290. {
  291. if ( (flags & UART_CONFIG_TYPE) &&
  292. (mpc52xx_uart_request_port(port) == 0) )
  293. port->type = PORT_MPC52xx;
  294. }
  295. static int
  296. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  297. {
  298. if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
  299. return -EINVAL;
  300. if ( (ser->irq != port->irq) ||
  301. (ser->io_type != SERIAL_IO_MEM) ||
  302. (ser->baud_base != port->uartclk) ||
  303. (ser->iomem_base != (void*)port->mapbase) ||
  304. (ser->hub6 != 0 ) )
  305. return -EINVAL;
  306. return 0;
  307. }
  308. static struct uart_ops mpc52xx_uart_ops = {
  309. .tx_empty = mpc52xx_uart_tx_empty,
  310. .set_mctrl = mpc52xx_uart_set_mctrl,
  311. .get_mctrl = mpc52xx_uart_get_mctrl,
  312. .stop_tx = mpc52xx_uart_stop_tx,
  313. .start_tx = mpc52xx_uart_start_tx,
  314. .send_xchar = mpc52xx_uart_send_xchar,
  315. .stop_rx = mpc52xx_uart_stop_rx,
  316. .enable_ms = mpc52xx_uart_enable_ms,
  317. .break_ctl = mpc52xx_uart_break_ctl,
  318. .startup = mpc52xx_uart_startup,
  319. .shutdown = mpc52xx_uart_shutdown,
  320. .set_termios = mpc52xx_uart_set_termios,
  321. /* .pm = mpc52xx_uart_pm, Not supported yet */
  322. /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
  323. .type = mpc52xx_uart_type,
  324. .release_port = mpc52xx_uart_release_port,
  325. .request_port = mpc52xx_uart_request_port,
  326. .config_port = mpc52xx_uart_config_port,
  327. .verify_port = mpc52xx_uart_verify_port
  328. };
  329. /* ======================================================================== */
  330. /* Interrupt handling */
  331. /* ======================================================================== */
  332. static inline int
  333. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  334. {
  335. struct tty_struct *tty = port->info->tty;
  336. unsigned char ch, flag;
  337. unsigned short status;
  338. /* While we can read, do so ! */
  339. while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
  340. MPC52xx_PSC_SR_RXRDY) {
  341. /* Get the char */
  342. ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  343. /* Handle sysreq char */
  344. #ifdef SUPPORT_SYSRQ
  345. if (uart_handle_sysrq_char(port, ch)) {
  346. port->sysrq = 0;
  347. continue;
  348. }
  349. #endif
  350. /* Store it */
  351. flag = TTY_NORMAL;
  352. port->icount.rx++;
  353. if ( status & (MPC52xx_PSC_SR_PE |
  354. MPC52xx_PSC_SR_FE |
  355. MPC52xx_PSC_SR_RB) ) {
  356. if (status & MPC52xx_PSC_SR_RB) {
  357. flag = TTY_BREAK;
  358. uart_handle_break(port);
  359. } else if (status & MPC52xx_PSC_SR_PE)
  360. flag = TTY_PARITY;
  361. else if (status & MPC52xx_PSC_SR_FE)
  362. flag = TTY_FRAME;
  363. /* Clear error condition */
  364. out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
  365. }
  366. tty_insert_flip_char(tty, ch, flag);
  367. if (status & MPC52xx_PSC_SR_OE) {
  368. /*
  369. * Overrun is special, since it's
  370. * reported immediately, and doesn't
  371. * affect the current character
  372. */
  373. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  374. }
  375. }
  376. tty_flip_buffer_push(tty);
  377. return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
  378. }
  379. static inline int
  380. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  381. {
  382. struct circ_buf *xmit = &port->info->xmit;
  383. /* Process out of band chars */
  384. if (port->x_char) {
  385. out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
  386. port->icount.tx++;
  387. port->x_char = 0;
  388. return 1;
  389. }
  390. /* Nothing to do ? */
  391. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  392. mpc52xx_uart_stop_tx(port);
  393. return 0;
  394. }
  395. /* Send chars */
  396. while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
  397. out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
  398. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  399. port->icount.tx++;
  400. if (uart_circ_empty(xmit))
  401. break;
  402. }
  403. /* Wake up */
  404. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  405. uart_write_wakeup(port);
  406. /* Maybe we're done after all */
  407. if (uart_circ_empty(xmit)) {
  408. mpc52xx_uart_stop_tx(port);
  409. return 0;
  410. }
  411. return 1;
  412. }
  413. static irqreturn_t
  414. mpc52xx_uart_int(int irq, void *dev_id)
  415. {
  416. struct uart_port *port = dev_id;
  417. unsigned long pass = ISR_PASS_LIMIT;
  418. unsigned int keepgoing;
  419. unsigned short status;
  420. spin_lock(&port->lock);
  421. /* While we have stuff to do, we continue */
  422. do {
  423. /* If we don't find anything to do, we stop */
  424. keepgoing = 0;
  425. /* Read status */
  426. status = in_be16(&PSC(port)->mpc52xx_psc_isr);
  427. status &= port->read_status_mask;
  428. /* Do we need to receive chars ? */
  429. /* For this RX interrupts must be on and some chars waiting */
  430. if ( status & MPC52xx_PSC_IMR_RXRDY )
  431. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  432. /* Do we need to send chars ? */
  433. /* For this, TX must be ready and TX interrupt enabled */
  434. if ( status & MPC52xx_PSC_IMR_TXRDY )
  435. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  436. /* Limit number of iteration */
  437. if ( !(--pass) )
  438. keepgoing = 0;
  439. } while (keepgoing);
  440. spin_unlock(&port->lock);
  441. return IRQ_HANDLED;
  442. }
  443. /* ======================================================================== */
  444. /* Console ( if applicable ) */
  445. /* ======================================================================== */
  446. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  447. static void __init
  448. mpc52xx_console_get_options(struct uart_port *port,
  449. int *baud, int *parity, int *bits, int *flow)
  450. {
  451. struct mpc52xx_psc __iomem *psc = PSC(port);
  452. unsigned char mr1;
  453. /* Read the mode registers */
  454. out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
  455. mr1 = in_8(&psc->mode);
  456. /* CT{U,L}R are write-only ! */
  457. *baud = __res.bi_baudrate ?
  458. __res.bi_baudrate : CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  459. /* Parse them */
  460. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  461. case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
  462. case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
  463. case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
  464. case MPC52xx_PSC_MODE_8_BITS:
  465. default: *bits = 8;
  466. }
  467. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  468. *parity = 'n';
  469. else
  470. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  471. }
  472. static void
  473. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  474. {
  475. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  476. struct mpc52xx_psc __iomem *psc = PSC(port);
  477. unsigned int i, j;
  478. /* Disable interrupts */
  479. out_be16(&psc->mpc52xx_psc_imr, 0);
  480. /* Wait the TX buffer to be empty */
  481. j = 5000000; /* Maximum wait */
  482. while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
  483. --j)
  484. udelay(1);
  485. /* Write all the chars */
  486. for (i = 0; i < count; i++, s++) {
  487. /* Line return handling */
  488. if (*s == '\n')
  489. out_8(&psc->mpc52xx_psc_buffer_8, '\r');
  490. /* Send the char */
  491. out_8(&psc->mpc52xx_psc_buffer_8, *s);
  492. /* Wait the TX buffer to be empty */
  493. j = 20000; /* Maximum wait */
  494. while (!(in_be16(&psc->mpc52xx_psc_status) &
  495. MPC52xx_PSC_SR_TXEMP) && --j)
  496. udelay(1);
  497. }
  498. /* Restore interrupt state */
  499. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  500. }
  501. static int __init
  502. mpc52xx_console_setup(struct console *co, char *options)
  503. {
  504. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  505. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  506. int bits = 8;
  507. int parity = 'n';
  508. int flow = 'n';
  509. if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
  510. return -EINVAL;
  511. /* Basic port init. Needed since we use some uart_??? func before
  512. * real init for early access */
  513. spin_lock_init(&port->lock);
  514. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  515. port->ops = &mpc52xx_uart_ops;
  516. port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
  517. /* We ioremap ourself */
  518. port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
  519. if (port->membase == NULL)
  520. return -EINVAL;
  521. /* Setup the port parameters accoding to options */
  522. if (options)
  523. uart_parse_options(options, &baud, &parity, &bits, &flow);
  524. else
  525. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  526. return uart_set_options(port, co, baud, parity, bits, flow);
  527. }
  528. static struct uart_driver mpc52xx_uart_driver;
  529. static struct console mpc52xx_console = {
  530. .name = "ttyPSC",
  531. .write = mpc52xx_console_write,
  532. .device = uart_console_device,
  533. .setup = mpc52xx_console_setup,
  534. .flags = CON_PRINTBUFFER,
  535. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
  536. .data = &mpc52xx_uart_driver,
  537. };
  538. static int __init
  539. mpc52xx_console_init(void)
  540. {
  541. register_console(&mpc52xx_console);
  542. return 0;
  543. }
  544. console_initcall(mpc52xx_console_init);
  545. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  546. #else
  547. #define MPC52xx_PSC_CONSOLE NULL
  548. #endif
  549. /* ======================================================================== */
  550. /* UART Driver */
  551. /* ======================================================================== */
  552. static struct uart_driver mpc52xx_uart_driver = {
  553. .owner = THIS_MODULE,
  554. .driver_name = "mpc52xx_psc_uart",
  555. .dev_name = "ttyPSC",
  556. .major = SERIAL_PSC_MAJOR,
  557. .minor = SERIAL_PSC_MINOR,
  558. .nr = MPC52xx_PSC_MAXNUM,
  559. .cons = MPC52xx_PSC_CONSOLE,
  560. };
  561. /* ======================================================================== */
  562. /* Platform Driver */
  563. /* ======================================================================== */
  564. static int __devinit
  565. mpc52xx_uart_probe(struct platform_device *dev)
  566. {
  567. struct resource *res = dev->resource;
  568. struct uart_port *port = NULL;
  569. int i, idx, ret;
  570. /* Check validity & presence */
  571. idx = dev->id;
  572. if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
  573. return -EINVAL;
  574. if (!mpc52xx_match_psc_function(idx,"uart"))
  575. return -ENODEV;
  576. /* Init the port structure */
  577. port = &mpc52xx_uart_ports[idx];
  578. memset(port, 0x00, sizeof(struct uart_port));
  579. spin_lock_init(&port->lock);
  580. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  581. port->fifosize = 512;
  582. port->iotype = UPIO_MEM;
  583. port->flags = UPF_BOOT_AUTOCONF |
  584. ( uart_console(port) ? 0 : UPF_IOREMAP );
  585. port->line = idx;
  586. port->ops = &mpc52xx_uart_ops;
  587. /* Search for IRQ and mapbase */
  588. for (i=0 ; i<dev->num_resources ; i++, res++) {
  589. if (res->flags & IORESOURCE_MEM)
  590. port->mapbase = res->start;
  591. else if (res->flags & IORESOURCE_IRQ)
  592. port->irq = res->start;
  593. }
  594. if (!port->irq || !port->mapbase)
  595. return -EINVAL;
  596. /* Add the port to the uart sub-system */
  597. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  598. if (!ret)
  599. platform_set_drvdata(dev, (void*)port);
  600. return ret;
  601. }
  602. static int
  603. mpc52xx_uart_remove(struct platform_device *dev)
  604. {
  605. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  606. platform_set_drvdata(dev, NULL);
  607. if (port)
  608. uart_remove_one_port(&mpc52xx_uart_driver, port);
  609. return 0;
  610. }
  611. #ifdef CONFIG_PM
  612. static int
  613. mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
  614. {
  615. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  616. if (sport)
  617. uart_suspend_port(&mpc52xx_uart_driver, port);
  618. return 0;
  619. }
  620. static int
  621. mpc52xx_uart_resume(struct platform_device *dev)
  622. {
  623. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  624. if (port)
  625. uart_resume_port(&mpc52xx_uart_driver, port);
  626. return 0;
  627. }
  628. #endif
  629. static struct platform_driver mpc52xx_uart_platform_driver = {
  630. .probe = mpc52xx_uart_probe,
  631. .remove = mpc52xx_uart_remove,
  632. #ifdef CONFIG_PM
  633. .suspend = mpc52xx_uart_suspend,
  634. .resume = mpc52xx_uart_resume,
  635. #endif
  636. .driver = {
  637. .name = "mpc52xx-psc",
  638. },
  639. };
  640. /* ======================================================================== */
  641. /* Module */
  642. /* ======================================================================== */
  643. static int __init
  644. mpc52xx_uart_init(void)
  645. {
  646. int ret;
  647. printk(KERN_INFO "Serial: MPC52xx PSC driver\n");
  648. ret = uart_register_driver(&mpc52xx_uart_driver);
  649. if (ret == 0) {
  650. ret = platform_driver_register(&mpc52xx_uart_platform_driver);
  651. if (ret)
  652. uart_unregister_driver(&mpc52xx_uart_driver);
  653. }
  654. return ret;
  655. }
  656. static void __exit
  657. mpc52xx_uart_exit(void)
  658. {
  659. platform_driver_unregister(&mpc52xx_uart_platform_driver);
  660. uart_unregister_driver(&mpc52xx_uart_driver);
  661. }
  662. module_init(mpc52xx_uart_init);
  663. module_exit(mpc52xx_uart_exit);
  664. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  665. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  666. MODULE_LICENSE("GPL");