mpc52xx_uart.c 30 KB

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