mpc52xx_uart.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
  3. *
  4. * FIXME According to the usermanual the status bits in the status register
  5. * are only updated when the peripherals access the FIFO and not when the
  6. * CPU access them. So since we use this bits to know when we stop writing
  7. * and reading, they may not be updated in-time and a race condition may
  8. * exists. But I haven't be able to prove this and I don't care. But if
  9. * any problem arises, it might worth checking. The TX/RX FIFO Stats
  10. * registers should be used in addition.
  11. * Update: Actually, they seem updated ... At least the bits we use.
  12. *
  13. *
  14. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  15. *
  16. * Some of the code has been inspired/copied from the 2.4 code written
  17. * by Dale Farnsworth <dfarnsworth@mvista.com>.
  18. *
  19. * Copyright (C) 2006 Secret Lab Technologies Ltd.
  20. * Grant Likely <grant.likely@secretlab.ca>
  21. * Copyright (C) 2004-2006 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. /* OF Platform device Usage :
  46. *
  47. * This driver is only used for PSCs configured in uart mode. The device
  48. * tree will have a node for each PSC in uart mode w/ device_type = "serial"
  49. * and "mpc52xx-psc-uart" in the compatible string
  50. *
  51. * By default, PSC devices are enumerated in the order they are found. However
  52. * a particular PSC number can be forces by adding 'device_no = <port#>'
  53. * to the device node.
  54. *
  55. * The driver init all necessary registers to place the PSC in uart mode without
  56. * DCD. However, the pin multiplexing aren't changed and should be set either
  57. * by the bootloader or in the platform init code.
  58. */
  59. #undef DEBUG
  60. #include <linux/device.h>
  61. #include <linux/module.h>
  62. #include <linux/tty.h>
  63. #include <linux/serial.h>
  64. #include <linux/sysrq.h>
  65. #include <linux/console.h>
  66. #include <asm/delay.h>
  67. #include <asm/io.h>
  68. #if defined(CONFIG_PPC_MERGE)
  69. #include <asm/of_platform.h>
  70. #else
  71. #include <linux/platform_device.h>
  72. #endif
  73. #include <asm/mpc52xx.h>
  74. #include <asm/mpc52xx_psc.h>
  75. #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  76. #define SUPPORT_SYSRQ
  77. #endif
  78. #include <linux/serial_core.h>
  79. /* We've been assigned a range on the "Low-density serial ports" major */
  80. #define SERIAL_PSC_MAJOR 204
  81. #define SERIAL_PSC_MINOR 148
  82. #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
  83. static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
  84. /* Rem: - We use the read_status_mask as a shadow of
  85. * psc->mpc52xx_psc_imr
  86. * - It's important that is array is all zero on start as we
  87. * use it to know if it's initialized or not ! If it's not sure
  88. * it's cleared, then a memset(...,0,...) should be added to
  89. * the console_init
  90. */
  91. #if defined(CONFIG_PPC_MERGE)
  92. /* lookup table for matching device nodes to index numbers */
  93. static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
  94. static void mpc52xx_uart_of_enumerate(void);
  95. #endif
  96. #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
  97. /* Forward declaration of the interruption handling routine */
  98. static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
  99. /* Simple macro to test if a port is console or not. This one is taken
  100. * for serial_core.c and maybe should be moved to serial_core.h ? */
  101. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  102. #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
  103. #else
  104. #define uart_console(port) (0)
  105. #endif
  106. #if defined(CONFIG_PPC_MERGE)
  107. static struct of_device_id mpc52xx_uart_of_match[] = {
  108. { .type = "serial", .compatible = "mpc5200-psc-uart", },
  109. {},
  110. };
  111. #endif
  112. /* ======================================================================== */
  113. /* UART operations */
  114. /* ======================================================================== */
  115. static unsigned int
  116. mpc52xx_uart_tx_empty(struct uart_port *port)
  117. {
  118. int status = in_be16(&PSC(port)->mpc52xx_psc_status);
  119. return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  120. }
  121. static void
  122. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  123. {
  124. /* Not implemented */
  125. }
  126. static unsigned int
  127. mpc52xx_uart_get_mctrl(struct uart_port *port)
  128. {
  129. /* Not implemented */
  130. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  131. }
  132. static void
  133. mpc52xx_uart_stop_tx(struct uart_port *port)
  134. {
  135. /* port->lock taken by caller */
  136. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  137. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  138. }
  139. static void
  140. mpc52xx_uart_start_tx(struct uart_port *port)
  141. {
  142. /* port->lock taken by caller */
  143. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  144. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  145. }
  146. static void
  147. mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
  148. {
  149. unsigned long flags;
  150. spin_lock_irqsave(&port->lock, flags);
  151. port->x_char = ch;
  152. if (ch) {
  153. /* Make sure tx interrupts are on */
  154. /* Truly necessary ??? They should be anyway */
  155. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  156. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  157. }
  158. spin_unlock_irqrestore(&port->lock, flags);
  159. }
  160. static void
  161. mpc52xx_uart_stop_rx(struct uart_port *port)
  162. {
  163. /* port->lock taken by caller */
  164. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  165. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  166. }
  167. static void
  168. mpc52xx_uart_enable_ms(struct uart_port *port)
  169. {
  170. /* Not implemented */
  171. }
  172. static void
  173. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  174. {
  175. unsigned long flags;
  176. spin_lock_irqsave(&port->lock, flags);
  177. if ( ctl == -1 )
  178. out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
  179. else
  180. out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
  181. spin_unlock_irqrestore(&port->lock, flags);
  182. }
  183. static int
  184. mpc52xx_uart_startup(struct uart_port *port)
  185. {
  186. struct mpc52xx_psc __iomem *psc = PSC(port);
  187. int ret;
  188. /* Request IRQ */
  189. ret = request_irq(port->irq, mpc52xx_uart_int,
  190. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
  191. if (ret)
  192. return ret;
  193. /* Reset/activate the port, clear and enable interrupts */
  194. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  195. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  196. out_be32(&psc->sicr,0); /* UART mode DCD ignored */
  197. out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
  198. out_8(&psc->rfcntl, 0x00);
  199. out_be16(&psc->rfalarm, 0x1ff);
  200. out_8(&psc->tfcntl, 0x07);
  201. out_be16(&psc->tfalarm, 0x80);
  202. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  203. out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
  204. out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
  205. out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
  206. return 0;
  207. }
  208. static void
  209. mpc52xx_uart_shutdown(struct uart_port *port)
  210. {
  211. struct mpc52xx_psc __iomem *psc = PSC(port);
  212. /* Shut down the port. Leave TX active if on a console port */
  213. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  214. if (!uart_console(port))
  215. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  216. port->read_status_mask = 0;
  217. out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
  218. /* Release interrupt */
  219. free_irq(port->irq, port);
  220. }
  221. static void
  222. mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
  223. struct ktermios *old)
  224. {
  225. struct mpc52xx_psc __iomem *psc = PSC(port);
  226. unsigned long flags;
  227. unsigned char mr1, mr2;
  228. unsigned short ctr;
  229. unsigned int j, baud, quot;
  230. /* Prepare what we're gonna write */
  231. mr1 = 0;
  232. switch (new->c_cflag & CSIZE) {
  233. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  234. break;
  235. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  236. break;
  237. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  238. break;
  239. case CS8:
  240. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  241. }
  242. if (new->c_cflag & PARENB) {
  243. mr1 |= (new->c_cflag & PARODD) ?
  244. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  245. } else
  246. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  247. mr2 = 0;
  248. if (new->c_cflag & CSTOPB)
  249. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  250. else
  251. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  252. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  253. MPC52xx_PSC_MODE_ONE_STOP;
  254. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  255. quot = uart_get_divisor(port, baud);
  256. ctr = quot & 0xffff;
  257. /* Get the lock */
  258. spin_lock_irqsave(&port->lock, flags);
  259. /* Update the per-port timeout */
  260. uart_update_timeout(port, new->c_cflag, baud);
  261. /* Do our best to flush TX & RX, so we don't loose anything */
  262. /* But we don't wait indefinitly ! */
  263. j = 5000000; /* Maximum wait */
  264. /* FIXME Can't receive chars since set_termios might be called at early
  265. * boot for the console, all stuff is not yet ready to receive at that
  266. * time and that just makes the kernel oops */
  267. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  268. while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
  269. --j)
  270. udelay(1);
  271. if (!j)
  272. printk( KERN_ERR "mpc52xx_uart.c: "
  273. "Unable to flush RX & TX fifos in-time in set_termios."
  274. "Some chars may have been lost.\n" );
  275. /* Reset the TX & RX */
  276. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  277. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  278. /* Send new mode settings */
  279. out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
  280. out_8(&psc->mode,mr1);
  281. out_8(&psc->mode,mr2);
  282. out_8(&psc->ctur,ctr >> 8);
  283. out_8(&psc->ctlr,ctr & 0xff);
  284. /* Reenable TX & RX */
  285. out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
  286. out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
  287. /* We're all set, release the lock */
  288. spin_unlock_irqrestore(&port->lock, flags);
  289. }
  290. static const char *
  291. mpc52xx_uart_type(struct uart_port *port)
  292. {
  293. return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
  294. }
  295. static void
  296. mpc52xx_uart_release_port(struct uart_port *port)
  297. {
  298. if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
  299. iounmap(port->membase);
  300. port->membase = NULL;
  301. }
  302. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  303. }
  304. static int
  305. mpc52xx_uart_request_port(struct uart_port *port)
  306. {
  307. int err;
  308. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  309. port->membase = ioremap(port->mapbase,
  310. sizeof(struct mpc52xx_psc));
  311. if (!port->membase)
  312. return -EINVAL;
  313. err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
  314. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  315. if (err && (port->flags & UPF_IOREMAP)) {
  316. iounmap(port->membase);
  317. port->membase = NULL;
  318. }
  319. return err;
  320. }
  321. static void
  322. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  323. {
  324. if ( (flags & UART_CONFIG_TYPE) &&
  325. (mpc52xx_uart_request_port(port) == 0) )
  326. port->type = PORT_MPC52xx;
  327. }
  328. static int
  329. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  330. {
  331. if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
  332. return -EINVAL;
  333. if ( (ser->irq != port->irq) ||
  334. (ser->io_type != SERIAL_IO_MEM) ||
  335. (ser->baud_base != port->uartclk) ||
  336. (ser->iomem_base != (void*)port->mapbase) ||
  337. (ser->hub6 != 0 ) )
  338. return -EINVAL;
  339. return 0;
  340. }
  341. static struct uart_ops mpc52xx_uart_ops = {
  342. .tx_empty = mpc52xx_uart_tx_empty,
  343. .set_mctrl = mpc52xx_uart_set_mctrl,
  344. .get_mctrl = mpc52xx_uart_get_mctrl,
  345. .stop_tx = mpc52xx_uart_stop_tx,
  346. .start_tx = mpc52xx_uart_start_tx,
  347. .send_xchar = mpc52xx_uart_send_xchar,
  348. .stop_rx = mpc52xx_uart_stop_rx,
  349. .enable_ms = mpc52xx_uart_enable_ms,
  350. .break_ctl = mpc52xx_uart_break_ctl,
  351. .startup = mpc52xx_uart_startup,
  352. .shutdown = mpc52xx_uart_shutdown,
  353. .set_termios = mpc52xx_uart_set_termios,
  354. /* .pm = mpc52xx_uart_pm, Not supported yet */
  355. /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
  356. .type = mpc52xx_uart_type,
  357. .release_port = mpc52xx_uart_release_port,
  358. .request_port = mpc52xx_uart_request_port,
  359. .config_port = mpc52xx_uart_config_port,
  360. .verify_port = mpc52xx_uart_verify_port
  361. };
  362. /* ======================================================================== */
  363. /* Interrupt handling */
  364. /* ======================================================================== */
  365. static inline int
  366. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  367. {
  368. struct tty_struct *tty = port->info->tty;
  369. unsigned char ch, flag;
  370. unsigned short status;
  371. /* While we can read, do so ! */
  372. while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
  373. MPC52xx_PSC_SR_RXRDY) {
  374. /* Get the char */
  375. ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  376. /* Handle sysreq char */
  377. #ifdef SUPPORT_SYSRQ
  378. if (uart_handle_sysrq_char(port, ch)) {
  379. port->sysrq = 0;
  380. continue;
  381. }
  382. #endif
  383. /* Store it */
  384. flag = TTY_NORMAL;
  385. port->icount.rx++;
  386. if ( status & (MPC52xx_PSC_SR_PE |
  387. MPC52xx_PSC_SR_FE |
  388. MPC52xx_PSC_SR_RB) ) {
  389. if (status & MPC52xx_PSC_SR_RB) {
  390. flag = TTY_BREAK;
  391. uart_handle_break(port);
  392. } else if (status & MPC52xx_PSC_SR_PE)
  393. flag = TTY_PARITY;
  394. else if (status & MPC52xx_PSC_SR_FE)
  395. flag = TTY_FRAME;
  396. /* Clear error condition */
  397. out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
  398. }
  399. tty_insert_flip_char(tty, ch, flag);
  400. if (status & MPC52xx_PSC_SR_OE) {
  401. /*
  402. * Overrun is special, since it's
  403. * reported immediately, and doesn't
  404. * affect the current character
  405. */
  406. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  407. }
  408. }
  409. tty_flip_buffer_push(tty);
  410. return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
  411. }
  412. static inline int
  413. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  414. {
  415. struct circ_buf *xmit = &port->info->xmit;
  416. /* Process out of band chars */
  417. if (port->x_char) {
  418. out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
  419. port->icount.tx++;
  420. port->x_char = 0;
  421. return 1;
  422. }
  423. /* Nothing to do ? */
  424. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  425. mpc52xx_uart_stop_tx(port);
  426. return 0;
  427. }
  428. /* Send chars */
  429. while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
  430. out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
  431. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  432. port->icount.tx++;
  433. if (uart_circ_empty(xmit))
  434. break;
  435. }
  436. /* Wake up */
  437. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  438. uart_write_wakeup(port);
  439. /* Maybe we're done after all */
  440. if (uart_circ_empty(xmit)) {
  441. mpc52xx_uart_stop_tx(port);
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. static irqreturn_t
  447. mpc52xx_uart_int(int irq, void *dev_id)
  448. {
  449. struct uart_port *port = dev_id;
  450. unsigned long pass = ISR_PASS_LIMIT;
  451. unsigned int keepgoing;
  452. unsigned short status;
  453. spin_lock(&port->lock);
  454. /* While we have stuff to do, we continue */
  455. do {
  456. /* If we don't find anything to do, we stop */
  457. keepgoing = 0;
  458. /* Read status */
  459. status = in_be16(&PSC(port)->mpc52xx_psc_isr);
  460. status &= port->read_status_mask;
  461. /* Do we need to receive chars ? */
  462. /* For this RX interrupts must be on and some chars waiting */
  463. if ( status & MPC52xx_PSC_IMR_RXRDY )
  464. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  465. /* Do we need to send chars ? */
  466. /* For this, TX must be ready and TX interrupt enabled */
  467. if ( status & MPC52xx_PSC_IMR_TXRDY )
  468. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  469. /* Limit number of iteration */
  470. if ( !(--pass) )
  471. keepgoing = 0;
  472. } while (keepgoing);
  473. spin_unlock(&port->lock);
  474. return IRQ_HANDLED;
  475. }
  476. /* ======================================================================== */
  477. /* Console ( if applicable ) */
  478. /* ======================================================================== */
  479. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  480. static void __init
  481. mpc52xx_console_get_options(struct uart_port *port,
  482. int *baud, int *parity, int *bits, int *flow)
  483. {
  484. struct mpc52xx_psc __iomem *psc = PSC(port);
  485. unsigned char mr1;
  486. pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
  487. /* Read the mode registers */
  488. out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
  489. mr1 = in_8(&psc->mode);
  490. /* CT{U,L}R are write-only ! */
  491. *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  492. #if !defined(CONFIG_PPC_MERGE)
  493. if (__res.bi_baudrate)
  494. *baud = __res.bi_baudrate;
  495. #endif
  496. /* Parse them */
  497. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  498. case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
  499. case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
  500. case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
  501. case MPC52xx_PSC_MODE_8_BITS:
  502. default: *bits = 8;
  503. }
  504. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  505. *parity = 'n';
  506. else
  507. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  508. }
  509. static void
  510. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  511. {
  512. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  513. struct mpc52xx_psc __iomem *psc = PSC(port);
  514. unsigned int i, j;
  515. /* Disable interrupts */
  516. out_be16(&psc->mpc52xx_psc_imr, 0);
  517. /* Wait the TX buffer to be empty */
  518. j = 5000000; /* Maximum wait */
  519. while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
  520. --j)
  521. udelay(1);
  522. /* Write all the chars */
  523. for (i = 0; i < count; i++, s++) {
  524. /* Line return handling */
  525. if (*s == '\n')
  526. out_8(&psc->mpc52xx_psc_buffer_8, '\r');
  527. /* Send the char */
  528. out_8(&psc->mpc52xx_psc_buffer_8, *s);
  529. /* Wait the TX buffer to be empty */
  530. j = 20000; /* Maximum wait */
  531. while (!(in_be16(&psc->mpc52xx_psc_status) &
  532. MPC52xx_PSC_SR_TXEMP) && --j)
  533. udelay(1);
  534. }
  535. /* Restore interrupt state */
  536. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  537. }
  538. #if !defined(CONFIG_PPC_MERGE)
  539. static int __init
  540. mpc52xx_console_setup(struct console *co, char *options)
  541. {
  542. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  543. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  544. int bits = 8;
  545. int parity = 'n';
  546. int flow = 'n';
  547. if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
  548. return -EINVAL;
  549. /* Basic port init. Needed since we use some uart_??? func before
  550. * real init for early access */
  551. spin_lock_init(&port->lock);
  552. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  553. port->ops = &mpc52xx_uart_ops;
  554. port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
  555. /* We ioremap ourself */
  556. port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
  557. if (port->membase == NULL)
  558. return -EINVAL;
  559. /* Setup the port parameters accoding to options */
  560. if (options)
  561. uart_parse_options(options, &baud, &parity, &bits, &flow);
  562. else
  563. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  564. return uart_set_options(port, co, baud, parity, bits, flow);
  565. }
  566. #else
  567. static int __init
  568. mpc52xx_console_setup(struct console *co, char *options)
  569. {
  570. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  571. struct device_node *np = mpc52xx_uart_nodes[co->index];
  572. unsigned int ipb_freq;
  573. struct resource res;
  574. int ret;
  575. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  576. int bits = 8;
  577. int parity = 'n';
  578. int flow = 'n';
  579. pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
  580. co, co->index, options);
  581. if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
  582. pr_debug("PSC%x out of range\n", co->index);
  583. return -EINVAL;
  584. }
  585. if (!np) {
  586. pr_debug("PSC%x not found in device tree\n", co->index);
  587. return -EINVAL;
  588. }
  589. pr_debug("Console on ttyPSC%x is %s\n",
  590. co->index, mpc52xx_uart_nodes[co->index]->full_name);
  591. /* Fetch register locations */
  592. if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
  593. pr_debug("Could not get resources for PSC%x\n", co->index);
  594. return ret;
  595. }
  596. /* Search for bus-frequency property in this node or a parent */
  597. if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
  598. pr_debug("Could not find IPB bus frequency!\n");
  599. return -EINVAL;
  600. }
  601. /* Basic port init. Needed since we use some uart_??? func before
  602. * real init for early access */
  603. spin_lock_init(&port->lock);
  604. port->uartclk = ipb_freq / 2;
  605. port->ops = &mpc52xx_uart_ops;
  606. port->mapbase = res.start;
  607. port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
  608. port->irq = irq_of_parse_and_map(np, 0);
  609. if (port->membase == NULL)
  610. return -EINVAL;
  611. pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
  612. (void*)port->mapbase, port->membase, port->irq, port->uartclk);
  613. /* Setup the port parameters accoding to options */
  614. if (options)
  615. uart_parse_options(options, &baud, &parity, &bits, &flow);
  616. else
  617. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  618. pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
  619. baud, bits, parity, flow);
  620. return uart_set_options(port, co, baud, parity, bits, flow);
  621. }
  622. #endif /* defined(CONFIG_PPC_MERGE) */
  623. static struct uart_driver mpc52xx_uart_driver;
  624. static struct console mpc52xx_console = {
  625. .name = "ttyPSC",
  626. .write = mpc52xx_console_write,
  627. .device = uart_console_device,
  628. .setup = mpc52xx_console_setup,
  629. .flags = CON_PRINTBUFFER,
  630. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
  631. .data = &mpc52xx_uart_driver,
  632. };
  633. static int __init
  634. mpc52xx_console_init(void)
  635. {
  636. #if defined(CONFIG_PPC_MERGE)
  637. mpc52xx_uart_of_enumerate();
  638. #endif
  639. register_console(&mpc52xx_console);
  640. return 0;
  641. }
  642. console_initcall(mpc52xx_console_init);
  643. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  644. #else
  645. #define MPC52xx_PSC_CONSOLE NULL
  646. #endif
  647. /* ======================================================================== */
  648. /* UART Driver */
  649. /* ======================================================================== */
  650. static struct uart_driver mpc52xx_uart_driver = {
  651. .owner = THIS_MODULE,
  652. .driver_name = "mpc52xx_psc_uart",
  653. .dev_name = "ttyPSC",
  654. .major = SERIAL_PSC_MAJOR,
  655. .minor = SERIAL_PSC_MINOR,
  656. .nr = MPC52xx_PSC_MAXNUM,
  657. .cons = MPC52xx_PSC_CONSOLE,
  658. };
  659. #if !defined(CONFIG_PPC_MERGE)
  660. /* ======================================================================== */
  661. /* Platform Driver */
  662. /* ======================================================================== */
  663. static int __devinit
  664. mpc52xx_uart_probe(struct platform_device *dev)
  665. {
  666. struct resource *res = dev->resource;
  667. struct uart_port *port = NULL;
  668. int i, idx, ret;
  669. /* Check validity & presence */
  670. idx = dev->id;
  671. if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
  672. return -EINVAL;
  673. if (!mpc52xx_match_psc_function(idx,"uart"))
  674. return -ENODEV;
  675. /* Init the port structure */
  676. port = &mpc52xx_uart_ports[idx];
  677. spin_lock_init(&port->lock);
  678. port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
  679. port->fifosize = 512;
  680. port->iotype = UPIO_MEM;
  681. port->flags = UPF_BOOT_AUTOCONF |
  682. ( uart_console(port) ? 0 : UPF_IOREMAP );
  683. port->line = idx;
  684. port->ops = &mpc52xx_uart_ops;
  685. port->dev = &dev->dev;
  686. /* Search for IRQ and mapbase */
  687. for (i=0 ; i<dev->num_resources ; i++, res++) {
  688. if (res->flags & IORESOURCE_MEM)
  689. port->mapbase = res->start;
  690. else if (res->flags & IORESOURCE_IRQ)
  691. port->irq = res->start;
  692. }
  693. if (!port->irq || !port->mapbase)
  694. return -EINVAL;
  695. /* Add the port to the uart sub-system */
  696. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  697. if (!ret)
  698. platform_set_drvdata(dev, (void*)port);
  699. return ret;
  700. }
  701. static int
  702. mpc52xx_uart_remove(struct platform_device *dev)
  703. {
  704. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  705. platform_set_drvdata(dev, NULL);
  706. if (port)
  707. uart_remove_one_port(&mpc52xx_uart_driver, port);
  708. return 0;
  709. }
  710. #ifdef CONFIG_PM
  711. static int
  712. mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
  713. {
  714. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  715. if (port)
  716. uart_suspend_port(&mpc52xx_uart_driver, port);
  717. return 0;
  718. }
  719. static int
  720. mpc52xx_uart_resume(struct platform_device *dev)
  721. {
  722. struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
  723. if (port)
  724. uart_resume_port(&mpc52xx_uart_driver, port);
  725. return 0;
  726. }
  727. #endif
  728. static struct platform_driver mpc52xx_uart_platform_driver = {
  729. .probe = mpc52xx_uart_probe,
  730. .remove = mpc52xx_uart_remove,
  731. #ifdef CONFIG_PM
  732. .suspend = mpc52xx_uart_suspend,
  733. .resume = mpc52xx_uart_resume,
  734. #endif
  735. .driver = {
  736. .name = "mpc52xx-psc",
  737. },
  738. };
  739. #endif /* !defined(CONFIG_PPC_MERGE) */
  740. #if defined(CONFIG_PPC_MERGE)
  741. /* ======================================================================== */
  742. /* OF Platform Driver */
  743. /* ======================================================================== */
  744. static int __devinit
  745. mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
  746. {
  747. int idx = -1;
  748. unsigned int ipb_freq;
  749. struct uart_port *port = NULL;
  750. struct resource res;
  751. int ret;
  752. dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
  753. /* Check validity & presence */
  754. for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
  755. if (mpc52xx_uart_nodes[idx] == op->node)
  756. break;
  757. if (idx >= MPC52xx_PSC_MAXNUM)
  758. return -EINVAL;
  759. pr_debug("Found %s assigned to ttyPSC%x\n",
  760. mpc52xx_uart_nodes[idx]->full_name, idx);
  761. /* Search for bus-frequency property in this node or a parent */
  762. if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
  763. dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
  764. return -EINVAL;
  765. }
  766. /* Init the port structure */
  767. port = &mpc52xx_uart_ports[idx];
  768. spin_lock_init(&port->lock);
  769. port->uartclk = ipb_freq / 2;
  770. port->fifosize = 512;
  771. port->iotype = UPIO_MEM;
  772. port->flags = UPF_BOOT_AUTOCONF |
  773. ( uart_console(port) ? 0 : UPF_IOREMAP );
  774. port->line = idx;
  775. port->ops = &mpc52xx_uart_ops;
  776. port->dev = &op->dev;
  777. /* Search for IRQ and mapbase */
  778. if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
  779. return ret;
  780. port->mapbase = res.start;
  781. port->irq = irq_of_parse_and_map(op->node, 0);
  782. dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
  783. (void*)port->mapbase, port->irq, port->uartclk);
  784. if ((port->irq==NO_IRQ) || !port->mapbase) {
  785. printk(KERN_ERR "Could not allocate resources for PSC\n");
  786. return -EINVAL;
  787. }
  788. /* Add the port to the uart sub-system */
  789. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  790. if (!ret)
  791. dev_set_drvdata(&op->dev, (void*)port);
  792. return ret;
  793. }
  794. static int
  795. mpc52xx_uart_of_remove(struct of_device *op)
  796. {
  797. struct uart_port *port = dev_get_drvdata(&op->dev);
  798. dev_set_drvdata(&op->dev, NULL);
  799. if (port) {
  800. uart_remove_one_port(&mpc52xx_uart_driver, port);
  801. irq_dispose_mapping(port->irq);
  802. }
  803. return 0;
  804. }
  805. #ifdef CONFIG_PM
  806. static int
  807. mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
  808. {
  809. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  810. if (port)
  811. uart_suspend_port(&mpc52xx_uart_driver, port);
  812. return 0;
  813. }
  814. static int
  815. mpc52xx_uart_of_resume(struct of_device *op)
  816. {
  817. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  818. if (port)
  819. uart_resume_port(&mpc52xx_uart_driver, port);
  820. return 0;
  821. }
  822. #endif
  823. static void
  824. mpc52xx_uart_of_assign(struct device_node *np, int idx)
  825. {
  826. int free_idx = -1;
  827. int i;
  828. /* Find the first free node */
  829. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  830. if (mpc52xx_uart_nodes[i] == NULL) {
  831. free_idx = i;
  832. break;
  833. }
  834. }
  835. if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
  836. idx = free_idx;
  837. if (idx < 0)
  838. return; /* No free slot; abort */
  839. /* If the slot is already occupied, then swap slots */
  840. if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
  841. mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
  842. mpc52xx_uart_nodes[idx] = np;
  843. }
  844. static void
  845. mpc52xx_uart_of_enumerate(void)
  846. {
  847. static int enum_done = 0;
  848. struct device_node *np;
  849. const unsigned int *devno;
  850. int i;
  851. if (enum_done)
  852. return;
  853. for_each_node_by_type(np, "serial") {
  854. if (!of_match_node(mpc52xx_uart_of_match, np))
  855. continue;
  856. /* Is a particular device number requested? */
  857. devno = of_get_property(np, "port-number", NULL);
  858. mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
  859. }
  860. enum_done = 1;
  861. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  862. if (mpc52xx_uart_nodes[i])
  863. pr_debug("%s assigned to ttyPSC%x\n",
  864. mpc52xx_uart_nodes[i]->full_name, i);
  865. }
  866. }
  867. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  868. static struct of_platform_driver mpc52xx_uart_of_driver = {
  869. .owner = THIS_MODULE,
  870. .name = "mpc52xx-psc-uart",
  871. .match_table = mpc52xx_uart_of_match,
  872. .probe = mpc52xx_uart_of_probe,
  873. .remove = mpc52xx_uart_of_remove,
  874. #ifdef CONFIG_PM
  875. .suspend = mpc52xx_uart_of_suspend,
  876. .resume = mpc52xx_uart_of_resume,
  877. #endif
  878. .driver = {
  879. .name = "mpc52xx-psc-uart",
  880. },
  881. };
  882. #endif /* defined(CONFIG_PPC_MERGE) */
  883. /* ======================================================================== */
  884. /* Module */
  885. /* ======================================================================== */
  886. static int __init
  887. mpc52xx_uart_init(void)
  888. {
  889. int ret;
  890. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  891. if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
  892. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  893. __FILE__, ret);
  894. return ret;
  895. }
  896. #if defined(CONFIG_PPC_MERGE)
  897. mpc52xx_uart_of_enumerate();
  898. ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
  899. if (ret) {
  900. printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
  901. __FILE__, ret);
  902. uart_unregister_driver(&mpc52xx_uart_driver);
  903. return ret;
  904. }
  905. #else
  906. ret = platform_driver_register(&mpc52xx_uart_platform_driver);
  907. if (ret) {
  908. printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
  909. __FILE__, ret);
  910. uart_unregister_driver(&mpc52xx_uart_driver);
  911. return ret;
  912. }
  913. #endif
  914. return 0;
  915. }
  916. static void __exit
  917. mpc52xx_uart_exit(void)
  918. {
  919. #if defined(CONFIG_PPC_MERGE)
  920. of_unregister_platform_driver(&mpc52xx_uart_of_driver);
  921. #else
  922. platform_driver_unregister(&mpc52xx_uart_platform_driver);
  923. #endif
  924. uart_unregister_driver(&mpc52xx_uart_driver);
  925. }
  926. module_init(mpc52xx_uart_init);
  927. module_exit(mpc52xx_uart_exit);
  928. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  929. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  930. MODULE_LICENSE("GPL");