mpc52xx_uart.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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 = "mpc52xx-psc-uart", },
  109. { .type = "serial", .compatible = "mpc5200-psc", }, /* Efika only! */
  110. {},
  111. };
  112. #endif
  113. /* ======================================================================== */
  114. /* UART operations */
  115. /* ======================================================================== */
  116. static unsigned int
  117. mpc52xx_uart_tx_empty(struct uart_port *port)
  118. {
  119. int status = in_be16(&PSC(port)->mpc52xx_psc_status);
  120. return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  121. }
  122. static void
  123. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  124. {
  125. /* Not implemented */
  126. }
  127. static unsigned int
  128. mpc52xx_uart_get_mctrl(struct uart_port *port)
  129. {
  130. /* Not implemented */
  131. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  132. }
  133. static void
  134. mpc52xx_uart_stop_tx(struct uart_port *port)
  135. {
  136. /* port->lock taken by caller */
  137. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  138. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  139. }
  140. static void
  141. mpc52xx_uart_start_tx(struct uart_port *port)
  142. {
  143. /* port->lock taken by caller */
  144. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  145. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  146. }
  147. static void
  148. mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
  149. {
  150. unsigned long flags;
  151. spin_lock_irqsave(&port->lock, flags);
  152. port->x_char = ch;
  153. if (ch) {
  154. /* Make sure tx interrupts are on */
  155. /* Truly necessary ??? They should be anyway */
  156. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  157. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  158. }
  159. spin_unlock_irqrestore(&port->lock, flags);
  160. }
  161. static void
  162. mpc52xx_uart_stop_rx(struct uart_port *port)
  163. {
  164. /* port->lock taken by caller */
  165. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  166. out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
  167. }
  168. static void
  169. mpc52xx_uart_enable_ms(struct uart_port *port)
  170. {
  171. /* Not implemented */
  172. }
  173. static void
  174. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  175. {
  176. unsigned long flags;
  177. spin_lock_irqsave(&port->lock, flags);
  178. if ( ctl == -1 )
  179. out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
  180. else
  181. out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
  182. spin_unlock_irqrestore(&port->lock, flags);
  183. }
  184. static int
  185. mpc52xx_uart_startup(struct uart_port *port)
  186. {
  187. struct mpc52xx_psc __iomem *psc = PSC(port);
  188. int ret;
  189. /* Request IRQ */
  190. ret = request_irq(port->irq, mpc52xx_uart_int,
  191. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
  192. if (ret)
  193. return ret;
  194. /* Reset/activate the port, clear and enable interrupts */
  195. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  196. out_8(&psc->command,MPC52xx_PSC_RST_TX);
  197. out_be32(&psc->sicr,0); /* UART mode DCD ignored */
  198. out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
  199. out_8(&psc->rfcntl, 0x00);
  200. out_be16(&psc->rfalarm, 0x1ff);
  201. out_8(&psc->tfcntl, 0x07);
  202. out_be16(&psc->tfalarm, 0x80);
  203. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  204. out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
  205. out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
  206. out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
  207. return 0;
  208. }
  209. static void
  210. mpc52xx_uart_shutdown(struct uart_port *port)
  211. {
  212. struct mpc52xx_psc __iomem *psc = PSC(port);
  213. /* Shut down the port, interrupt and all */
  214. out_8(&psc->command,MPC52xx_PSC_RST_RX);
  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 %lx, mapped to %p, irq=%x, freq=%i\n",
  612. 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 %lx, irq=%x, freq=%i\n",
  783. 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. return 0;
  802. }
  803. #ifdef CONFIG_PM
  804. static int
  805. mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
  806. {
  807. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  808. if (port)
  809. uart_suspend_port(&mpc52xx_uart_driver, port);
  810. return 0;
  811. }
  812. static int
  813. mpc52xx_uart_of_resume(struct of_device *op)
  814. {
  815. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  816. if (port)
  817. uart_resume_port(&mpc52xx_uart_driver, port);
  818. return 0;
  819. }
  820. #endif
  821. static void
  822. mpc52xx_uart_of_assign(struct device_node *np, int idx)
  823. {
  824. int free_idx = -1;
  825. int i;
  826. /* Find the first free node */
  827. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  828. if (mpc52xx_uart_nodes[i] == NULL) {
  829. free_idx = i;
  830. break;
  831. }
  832. }
  833. if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
  834. idx = free_idx;
  835. if (idx < 0)
  836. return; /* No free slot; abort */
  837. /* If the slot is already occupied, then swap slots */
  838. if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
  839. mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
  840. mpc52xx_uart_nodes[i] = np;
  841. }
  842. static void
  843. mpc52xx_uart_of_enumerate(void)
  844. {
  845. static int enum_done = 0;
  846. struct device_node *np;
  847. const unsigned int *devno;
  848. int i;
  849. if (enum_done)
  850. return;
  851. for_each_node_by_type(np, "serial") {
  852. if (!of_match_node(mpc52xx_uart_of_match, np))
  853. continue;
  854. /* Is a particular device number requested? */
  855. devno = get_property(np, "device_no", NULL);
  856. mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
  857. }
  858. enum_done = 1;
  859. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  860. if (mpc52xx_uart_nodes[i])
  861. pr_debug("%s assigned to ttyPSC%x\n",
  862. mpc52xx_uart_nodes[i]->full_name, i);
  863. }
  864. }
  865. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  866. static struct of_platform_driver mpc52xx_uart_of_driver = {
  867. .owner = THIS_MODULE,
  868. .name = "mpc52xx-psc-uart",
  869. .match_table = mpc52xx_uart_of_match,
  870. .probe = mpc52xx_uart_of_probe,
  871. .remove = mpc52xx_uart_of_remove,
  872. #ifdef CONFIG_PM
  873. .suspend = mpc52xx_uart_of_suspend,
  874. .resume = mpc52xx_uart_of_resume,
  875. #endif
  876. .driver = {
  877. .name = "mpc52xx-psc-uart",
  878. },
  879. };
  880. #endif /* defined(CONFIG_PPC_MERGE) */
  881. /* ======================================================================== */
  882. /* Module */
  883. /* ======================================================================== */
  884. static int __init
  885. mpc52xx_uart_init(void)
  886. {
  887. int ret;
  888. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  889. if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
  890. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  891. __FILE__, ret);
  892. return ret;
  893. }
  894. #if defined(CONFIG_PPC_MERGE)
  895. mpc52xx_uart_of_enumerate();
  896. ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
  897. if (ret) {
  898. printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
  899. __FILE__, ret);
  900. uart_unregister_driver(&mpc52xx_uart_driver);
  901. return ret;
  902. }
  903. #else
  904. ret = platform_driver_register(&mpc52xx_uart_platform_driver);
  905. if (ret) {
  906. printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
  907. __FILE__, ret);
  908. uart_unregister_driver(&mpc52xx_uart_driver);
  909. return ret;
  910. }
  911. #endif
  912. return 0;
  913. }
  914. static void __exit
  915. mpc52xx_uart_exit(void)
  916. {
  917. #if defined(CONFIG_PPC_MERGE)
  918. of_unregister_platform_driver(&mpc52xx_uart_of_driver);
  919. #else
  920. platform_driver_unregister(&mpc52xx_uart_platform_driver);
  921. #endif
  922. uart_unregister_driver(&mpc52xx_uart_driver);
  923. }
  924. module_init(mpc52xx_uart_init);
  925. module_exit(mpc52xx_uart_exit);
  926. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  927. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  928. MODULE_LICENSE("GPL");