mpc52xx_uart.c 30 KB

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