mpc52xx_uart.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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) 2008 Freescale Semiconductor Inc.
  20. * John Rigby <jrigby@gmail.com>
  21. * Added support for MPC5121
  22. * Copyright (C) 2006 Secret Lab Technologies Ltd.
  23. * Grant Likely <grant.likely@secretlab.ca>
  24. * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
  25. * Copyright (C) 2003 MontaVista, Software, Inc.
  26. *
  27. * This file is licensed under the terms of the GNU General Public License
  28. * version 2. This program is licensed "as is" without any warranty of any
  29. * kind, whether express or implied.
  30. */
  31. /* Platform device Usage :
  32. *
  33. * Since PSCs can have multiple function, the correct driver for each one
  34. * is selected by calling mpc52xx_match_psc_function(...). The function
  35. * handled by this driver is "uart".
  36. *
  37. * The driver init all necessary registers to place the PSC in uart mode without
  38. * DCD. However, the pin multiplexing aren't changed and should be set either
  39. * by the bootloader or in the platform init code.
  40. *
  41. * The idx field must be equal to the PSC index (e.g. 0 for PSC1, 1 for PSC2,
  42. * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and
  43. * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly
  44. * fpr the console code : without this 1:1 mapping, at early boot time, when we
  45. * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
  46. * will be mapped to.
  47. */
  48. /* OF Platform device Usage :
  49. *
  50. * This driver is only used for PSCs configured in uart mode. The device
  51. * tree will have a node for each PSC with "mpc52xx-psc-uart" in the compatible
  52. * list.
  53. *
  54. * By default, PSC devices are enumerated in the order they are found. However
  55. * a particular PSC number can be forces by adding 'device_no = <port#>'
  56. * to the device node.
  57. *
  58. * The driver init all necessary registers to place the PSC in uart mode without
  59. * DCD. However, the pin multiplexing aren't changed and should be set either
  60. * by the bootloader or in the platform init code.
  61. */
  62. #undef DEBUG
  63. #include <linux/device.h>
  64. #include <linux/module.h>
  65. #include <linux/tty.h>
  66. #include <linux/serial.h>
  67. #include <linux/sysrq.h>
  68. #include <linux/console.h>
  69. #include <linux/delay.h>
  70. #include <linux/io.h>
  71. #include <linux/of.h>
  72. #include <linux/of_platform.h>
  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. /* lookup table for matching device nodes to index numbers */
  92. static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
  93. static void mpc52xx_uart_of_enumerate(void);
  94. #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
  95. /* Forward declaration of the interruption handling routine */
  96. static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
  97. /* Simple macro to test if a port is console or not. This one is taken
  98. * for serial_core.c and maybe should be moved to serial_core.h ? */
  99. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  100. #define uart_console(port) \
  101. ((port)->cons && (port)->cons->index == (port)->line)
  102. #else
  103. #define uart_console(port) (0)
  104. #endif
  105. /* ======================================================================== */
  106. /* PSC fifo operations for isolating differences between 52xx and 512x */
  107. /* ======================================================================== */
  108. struct psc_ops {
  109. void (*fifo_init)(struct uart_port *port);
  110. int (*raw_rx_rdy)(struct uart_port *port);
  111. int (*raw_tx_rdy)(struct uart_port *port);
  112. int (*rx_rdy)(struct uart_port *port);
  113. int (*tx_rdy)(struct uart_port *port);
  114. int (*tx_empty)(struct uart_port *port);
  115. void (*stop_rx)(struct uart_port *port);
  116. void (*start_tx)(struct uart_port *port);
  117. void (*stop_tx)(struct uart_port *port);
  118. void (*rx_clr_irq)(struct uart_port *port);
  119. void (*tx_clr_irq)(struct uart_port *port);
  120. void (*write_char)(struct uart_port *port, unsigned char c);
  121. unsigned char (*read_char)(struct uart_port *port);
  122. void (*cw_disable_ints)(struct uart_port *port);
  123. void (*cw_restore_ints)(struct uart_port *port);
  124. unsigned long (*getuartclk)(void *p);
  125. };
  126. #ifdef CONFIG_PPC_MPC52xx
  127. #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
  128. static void mpc52xx_psc_fifo_init(struct uart_port *port)
  129. {
  130. struct mpc52xx_psc __iomem *psc = PSC(port);
  131. struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
  132. /* /32 prescaler */
  133. out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00);
  134. out_8(&fifo->rfcntl, 0x00);
  135. out_be16(&fifo->rfalarm, 0x1ff);
  136. out_8(&fifo->tfcntl, 0x07);
  137. out_be16(&fifo->tfalarm, 0x80);
  138. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  139. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  140. }
  141. static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
  142. {
  143. return in_be16(&PSC(port)->mpc52xx_psc_status)
  144. & MPC52xx_PSC_SR_RXRDY;
  145. }
  146. static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
  147. {
  148. return in_be16(&PSC(port)->mpc52xx_psc_status)
  149. & MPC52xx_PSC_SR_TXRDY;
  150. }
  151. static int mpc52xx_psc_rx_rdy(struct uart_port *port)
  152. {
  153. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  154. & port->read_status_mask
  155. & MPC52xx_PSC_IMR_RXRDY;
  156. }
  157. static int mpc52xx_psc_tx_rdy(struct uart_port *port)
  158. {
  159. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  160. & port->read_status_mask
  161. & MPC52xx_PSC_IMR_TXRDY;
  162. }
  163. static int mpc52xx_psc_tx_empty(struct uart_port *port)
  164. {
  165. return in_be16(&PSC(port)->mpc52xx_psc_status)
  166. & MPC52xx_PSC_SR_TXEMP;
  167. }
  168. static void mpc52xx_psc_start_tx(struct uart_port *port)
  169. {
  170. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  171. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  172. }
  173. static void mpc52xx_psc_stop_tx(struct uart_port *port)
  174. {
  175. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  176. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  177. }
  178. static void mpc52xx_psc_stop_rx(struct uart_port *port)
  179. {
  180. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  181. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  182. }
  183. static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
  184. {
  185. }
  186. static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
  187. {
  188. }
  189. static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
  190. {
  191. out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
  192. }
  193. static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
  194. {
  195. return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  196. }
  197. static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
  198. {
  199. out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
  200. }
  201. static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
  202. {
  203. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  204. }
  205. /* Search for bus-frequency property in this node or a parent */
  206. static unsigned long mpc52xx_getuartclk(void *p)
  207. {
  208. /*
  209. * 5200 UARTs have a / 32 prescaler
  210. * but the generic serial code assumes 16
  211. * so return ipb freq / 2
  212. */
  213. return mpc5xxx_get_bus_frequency(p) / 2;
  214. }
  215. static struct psc_ops mpc52xx_psc_ops = {
  216. .fifo_init = mpc52xx_psc_fifo_init,
  217. .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
  218. .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
  219. .rx_rdy = mpc52xx_psc_rx_rdy,
  220. .tx_rdy = mpc52xx_psc_tx_rdy,
  221. .tx_empty = mpc52xx_psc_tx_empty,
  222. .stop_rx = mpc52xx_psc_stop_rx,
  223. .start_tx = mpc52xx_psc_start_tx,
  224. .stop_tx = mpc52xx_psc_stop_tx,
  225. .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
  226. .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
  227. .write_char = mpc52xx_psc_write_char,
  228. .read_char = mpc52xx_psc_read_char,
  229. .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
  230. .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
  231. .getuartclk = mpc52xx_getuartclk,
  232. };
  233. #endif /* CONFIG_MPC52xx */
  234. #ifdef CONFIG_PPC_MPC512x
  235. #define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
  236. static void mpc512x_psc_fifo_init(struct uart_port *port)
  237. {
  238. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  239. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  240. out_be32(&FIFO_512x(port)->txalarm, 1);
  241. out_be32(&FIFO_512x(port)->tximr, 0);
  242. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  243. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  244. out_be32(&FIFO_512x(port)->rxalarm, 1);
  245. out_be32(&FIFO_512x(port)->rximr, 0);
  246. out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  247. out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  248. }
  249. static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
  250. {
  251. return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  252. }
  253. static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
  254. {
  255. return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  256. }
  257. static int mpc512x_psc_rx_rdy(struct uart_port *port)
  258. {
  259. return in_be32(&FIFO_512x(port)->rxsr)
  260. & in_be32(&FIFO_512x(port)->rximr)
  261. & MPC512x_PSC_FIFO_ALARM;
  262. }
  263. static int mpc512x_psc_tx_rdy(struct uart_port *port)
  264. {
  265. return in_be32(&FIFO_512x(port)->txsr)
  266. & in_be32(&FIFO_512x(port)->tximr)
  267. & MPC512x_PSC_FIFO_ALARM;
  268. }
  269. static int mpc512x_psc_tx_empty(struct uart_port *port)
  270. {
  271. return in_be32(&FIFO_512x(port)->txsr)
  272. & MPC512x_PSC_FIFO_EMPTY;
  273. }
  274. static void mpc512x_psc_stop_rx(struct uart_port *port)
  275. {
  276. unsigned long rx_fifo_imr;
  277. rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
  278. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  279. out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
  280. }
  281. static void mpc512x_psc_start_tx(struct uart_port *port)
  282. {
  283. unsigned long tx_fifo_imr;
  284. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  285. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  286. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  287. }
  288. static void mpc512x_psc_stop_tx(struct uart_port *port)
  289. {
  290. unsigned long tx_fifo_imr;
  291. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  292. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  293. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  294. }
  295. static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
  296. {
  297. out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
  298. }
  299. static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
  300. {
  301. out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
  302. }
  303. static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
  304. {
  305. out_8(&FIFO_512x(port)->txdata_8, c);
  306. }
  307. static unsigned char mpc512x_psc_read_char(struct uart_port *port)
  308. {
  309. return in_8(&FIFO_512x(port)->rxdata_8);
  310. }
  311. static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
  312. {
  313. port->read_status_mask =
  314. in_be32(&FIFO_512x(port)->tximr) << 16 |
  315. in_be32(&FIFO_512x(port)->rximr);
  316. out_be32(&FIFO_512x(port)->tximr, 0);
  317. out_be32(&FIFO_512x(port)->rximr, 0);
  318. }
  319. static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
  320. {
  321. out_be32(&FIFO_512x(port)->tximr,
  322. (port->read_status_mask >> 16) & 0x7f);
  323. out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
  324. }
  325. static unsigned long mpc512x_getuartclk(void *p)
  326. {
  327. return mpc5xxx_get_bus_frequency(p);
  328. }
  329. static struct psc_ops mpc512x_psc_ops = {
  330. .fifo_init = mpc512x_psc_fifo_init,
  331. .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
  332. .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
  333. .rx_rdy = mpc512x_psc_rx_rdy,
  334. .tx_rdy = mpc512x_psc_tx_rdy,
  335. .tx_empty = mpc512x_psc_tx_empty,
  336. .stop_rx = mpc512x_psc_stop_rx,
  337. .start_tx = mpc512x_psc_start_tx,
  338. .stop_tx = mpc512x_psc_stop_tx,
  339. .rx_clr_irq = mpc512x_psc_rx_clr_irq,
  340. .tx_clr_irq = mpc512x_psc_tx_clr_irq,
  341. .write_char = mpc512x_psc_write_char,
  342. .read_char = mpc512x_psc_read_char,
  343. .cw_disable_ints = mpc512x_psc_cw_disable_ints,
  344. .cw_restore_ints = mpc512x_psc_cw_restore_ints,
  345. .getuartclk = mpc512x_getuartclk,
  346. };
  347. #endif
  348. static struct psc_ops *psc_ops;
  349. /* ======================================================================== */
  350. /* UART operations */
  351. /* ======================================================================== */
  352. static unsigned int
  353. mpc52xx_uart_tx_empty(struct uart_port *port)
  354. {
  355. return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
  356. }
  357. static void
  358. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  359. {
  360. if (mctrl & TIOCM_RTS)
  361. out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
  362. else
  363. out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
  364. }
  365. static unsigned int
  366. mpc52xx_uart_get_mctrl(struct uart_port *port)
  367. {
  368. unsigned int ret = TIOCM_DSR;
  369. u8 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
  370. if (!(status & MPC52xx_PSC_CTS))
  371. ret |= TIOCM_CTS;
  372. if (!(status & MPC52xx_PSC_DCD))
  373. ret |= TIOCM_CAR;
  374. return ret;
  375. }
  376. static void
  377. mpc52xx_uart_stop_tx(struct uart_port *port)
  378. {
  379. /* port->lock taken by caller */
  380. psc_ops->stop_tx(port);
  381. }
  382. static void
  383. mpc52xx_uart_start_tx(struct uart_port *port)
  384. {
  385. /* port->lock taken by caller */
  386. psc_ops->start_tx(port);
  387. }
  388. static void
  389. mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
  390. {
  391. unsigned long flags;
  392. spin_lock_irqsave(&port->lock, flags);
  393. port->x_char = ch;
  394. if (ch) {
  395. /* Make sure tx interrupts are on */
  396. /* Truly necessary ??? They should be anyway */
  397. psc_ops->start_tx(port);
  398. }
  399. spin_unlock_irqrestore(&port->lock, flags);
  400. }
  401. static void
  402. mpc52xx_uart_stop_rx(struct uart_port *port)
  403. {
  404. /* port->lock taken by caller */
  405. psc_ops->stop_rx(port);
  406. }
  407. static void
  408. mpc52xx_uart_enable_ms(struct uart_port *port)
  409. {
  410. struct mpc52xx_psc __iomem *psc = PSC(port);
  411. /* clear D_*-bits by reading them */
  412. in_8(&psc->mpc52xx_psc_ipcr);
  413. /* enable CTS and DCD as IPC interrupts */
  414. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  415. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  416. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  417. }
  418. static void
  419. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  420. {
  421. unsigned long flags;
  422. spin_lock_irqsave(&port->lock, flags);
  423. if (ctl == -1)
  424. out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
  425. else
  426. out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
  427. spin_unlock_irqrestore(&port->lock, flags);
  428. }
  429. static int
  430. mpc52xx_uart_startup(struct uart_port *port)
  431. {
  432. struct mpc52xx_psc __iomem *psc = PSC(port);
  433. int ret;
  434. /* Request IRQ */
  435. ret = request_irq(port->irq, mpc52xx_uart_int,
  436. IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
  437. "mpc52xx_psc_uart", port);
  438. if (ret)
  439. return ret;
  440. /* Reset/activate the port, clear and enable interrupts */
  441. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  442. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  443. out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
  444. psc_ops->fifo_init(port);
  445. out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
  446. out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
  447. return 0;
  448. }
  449. static void
  450. mpc52xx_uart_shutdown(struct uart_port *port)
  451. {
  452. struct mpc52xx_psc __iomem *psc = PSC(port);
  453. /* Shut down the port. Leave TX active if on a console port */
  454. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  455. if (!uart_console(port))
  456. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  457. port->read_status_mask = 0;
  458. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  459. /* Release interrupt */
  460. free_irq(port->irq, port);
  461. }
  462. static void
  463. mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
  464. struct ktermios *old)
  465. {
  466. struct mpc52xx_psc __iomem *psc = PSC(port);
  467. unsigned long flags;
  468. unsigned char mr1, mr2;
  469. unsigned short ctr;
  470. unsigned int j, baud, quot;
  471. /* Prepare what we're gonna write */
  472. mr1 = 0;
  473. switch (new->c_cflag & CSIZE) {
  474. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  475. break;
  476. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  477. break;
  478. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  479. break;
  480. case CS8:
  481. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  482. }
  483. if (new->c_cflag & PARENB) {
  484. mr1 |= (new->c_cflag & PARODD) ?
  485. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  486. } else
  487. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  488. mr2 = 0;
  489. if (new->c_cflag & CSTOPB)
  490. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  491. else
  492. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  493. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  494. MPC52xx_PSC_MODE_ONE_STOP;
  495. if (new->c_cflag & CRTSCTS) {
  496. mr1 |= MPC52xx_PSC_MODE_RXRTS;
  497. mr2 |= MPC52xx_PSC_MODE_TXCTS;
  498. }
  499. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
  500. quot = uart_get_divisor(port, baud);
  501. ctr = quot & 0xffff;
  502. /* Get the lock */
  503. spin_lock_irqsave(&port->lock, flags);
  504. /* Update the per-port timeout */
  505. uart_update_timeout(port, new->c_cflag, baud);
  506. /* Do our best to flush TX & RX, so we don't lose anything */
  507. /* But we don't wait indefinitely ! */
  508. j = 5000000; /* Maximum wait */
  509. /* FIXME Can't receive chars since set_termios might be called at early
  510. * boot for the console, all stuff is not yet ready to receive at that
  511. * time and that just makes the kernel oops */
  512. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  513. while (!mpc52xx_uart_tx_empty(port) && --j)
  514. udelay(1);
  515. if (!j)
  516. printk(KERN_ERR "mpc52xx_uart.c: "
  517. "Unable to flush RX & TX fifos in-time in set_termios."
  518. "Some chars may have been lost.\n");
  519. /* Reset the TX & RX */
  520. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  521. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  522. /* Send new mode settings */
  523. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  524. out_8(&psc->mode, mr1);
  525. out_8(&psc->mode, mr2);
  526. out_8(&psc->ctur, ctr >> 8);
  527. out_8(&psc->ctlr, ctr & 0xff);
  528. if (UART_ENABLE_MS(port, new->c_cflag))
  529. mpc52xx_uart_enable_ms(port);
  530. /* Reenable TX & RX */
  531. out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
  532. out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
  533. /* We're all set, release the lock */
  534. spin_unlock_irqrestore(&port->lock, flags);
  535. }
  536. static const char *
  537. mpc52xx_uart_type(struct uart_port *port)
  538. {
  539. return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
  540. }
  541. static void
  542. mpc52xx_uart_release_port(struct uart_port *port)
  543. {
  544. /* remapped by us ? */
  545. if (port->flags & UPF_IOREMAP) {
  546. iounmap(port->membase);
  547. port->membase = NULL;
  548. }
  549. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  550. }
  551. static int
  552. mpc52xx_uart_request_port(struct uart_port *port)
  553. {
  554. int err;
  555. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  556. port->membase = ioremap(port->mapbase,
  557. sizeof(struct mpc52xx_psc));
  558. if (!port->membase)
  559. return -EINVAL;
  560. err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
  561. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  562. if (err && (port->flags & UPF_IOREMAP)) {
  563. iounmap(port->membase);
  564. port->membase = NULL;
  565. }
  566. return err;
  567. }
  568. static void
  569. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  570. {
  571. if ((flags & UART_CONFIG_TYPE)
  572. && (mpc52xx_uart_request_port(port) == 0))
  573. port->type = PORT_MPC52xx;
  574. }
  575. static int
  576. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  577. {
  578. if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
  579. return -EINVAL;
  580. if ((ser->irq != port->irq) ||
  581. (ser->io_type != SERIAL_IO_MEM) ||
  582. (ser->baud_base != port->uartclk) ||
  583. (ser->iomem_base != (void *)port->mapbase) ||
  584. (ser->hub6 != 0))
  585. return -EINVAL;
  586. return 0;
  587. }
  588. static struct uart_ops mpc52xx_uart_ops = {
  589. .tx_empty = mpc52xx_uart_tx_empty,
  590. .set_mctrl = mpc52xx_uart_set_mctrl,
  591. .get_mctrl = mpc52xx_uart_get_mctrl,
  592. .stop_tx = mpc52xx_uart_stop_tx,
  593. .start_tx = mpc52xx_uart_start_tx,
  594. .send_xchar = mpc52xx_uart_send_xchar,
  595. .stop_rx = mpc52xx_uart_stop_rx,
  596. .enable_ms = mpc52xx_uart_enable_ms,
  597. .break_ctl = mpc52xx_uart_break_ctl,
  598. .startup = mpc52xx_uart_startup,
  599. .shutdown = mpc52xx_uart_shutdown,
  600. .set_termios = mpc52xx_uart_set_termios,
  601. /* .pm = mpc52xx_uart_pm, Not supported yet */
  602. /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
  603. .type = mpc52xx_uart_type,
  604. .release_port = mpc52xx_uart_release_port,
  605. .request_port = mpc52xx_uart_request_port,
  606. .config_port = mpc52xx_uart_config_port,
  607. .verify_port = mpc52xx_uart_verify_port
  608. };
  609. /* ======================================================================== */
  610. /* Interrupt handling */
  611. /* ======================================================================== */
  612. static inline int
  613. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  614. {
  615. struct tty_struct *tty = port->info->port.tty;
  616. unsigned char ch, flag;
  617. unsigned short status;
  618. /* While we can read, do so ! */
  619. while (psc_ops->raw_rx_rdy(port)) {
  620. /* Get the char */
  621. ch = psc_ops->read_char(port);
  622. /* Handle sysreq char */
  623. #ifdef SUPPORT_SYSRQ
  624. if (uart_handle_sysrq_char(port, ch)) {
  625. port->sysrq = 0;
  626. continue;
  627. }
  628. #endif
  629. /* Store it */
  630. flag = TTY_NORMAL;
  631. port->icount.rx++;
  632. status = in_be16(&PSC(port)->mpc52xx_psc_status);
  633. if (status & (MPC52xx_PSC_SR_PE |
  634. MPC52xx_PSC_SR_FE |
  635. MPC52xx_PSC_SR_RB)) {
  636. if (status & MPC52xx_PSC_SR_RB) {
  637. flag = TTY_BREAK;
  638. uart_handle_break(port);
  639. port->icount.brk++;
  640. } else if (status & MPC52xx_PSC_SR_PE) {
  641. flag = TTY_PARITY;
  642. port->icount.parity++;
  643. }
  644. else if (status & MPC52xx_PSC_SR_FE) {
  645. flag = TTY_FRAME;
  646. port->icount.frame++;
  647. }
  648. /* Clear error condition */
  649. out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
  650. }
  651. tty_insert_flip_char(tty, ch, flag);
  652. if (status & MPC52xx_PSC_SR_OE) {
  653. /*
  654. * Overrun is special, since it's
  655. * reported immediately, and doesn't
  656. * affect the current character
  657. */
  658. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  659. port->icount.overrun++;
  660. }
  661. }
  662. spin_unlock(&port->lock);
  663. tty_flip_buffer_push(tty);
  664. spin_lock(&port->lock);
  665. return psc_ops->raw_rx_rdy(port);
  666. }
  667. static inline int
  668. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  669. {
  670. struct circ_buf *xmit = &port->info->xmit;
  671. /* Process out of band chars */
  672. if (port->x_char) {
  673. psc_ops->write_char(port, port->x_char);
  674. port->icount.tx++;
  675. port->x_char = 0;
  676. return 1;
  677. }
  678. /* Nothing to do ? */
  679. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  680. mpc52xx_uart_stop_tx(port);
  681. return 0;
  682. }
  683. /* Send chars */
  684. while (psc_ops->raw_tx_rdy(port)) {
  685. psc_ops->write_char(port, xmit->buf[xmit->tail]);
  686. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  687. port->icount.tx++;
  688. if (uart_circ_empty(xmit))
  689. break;
  690. }
  691. /* Wake up */
  692. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  693. uart_write_wakeup(port);
  694. /* Maybe we're done after all */
  695. if (uart_circ_empty(xmit)) {
  696. mpc52xx_uart_stop_tx(port);
  697. return 0;
  698. }
  699. return 1;
  700. }
  701. static irqreturn_t
  702. mpc52xx_uart_int(int irq, void *dev_id)
  703. {
  704. struct uart_port *port = dev_id;
  705. unsigned long pass = ISR_PASS_LIMIT;
  706. unsigned int keepgoing;
  707. u8 status;
  708. spin_lock(&port->lock);
  709. /* While we have stuff to do, we continue */
  710. do {
  711. /* If we don't find anything to do, we stop */
  712. keepgoing = 0;
  713. psc_ops->rx_clr_irq(port);
  714. if (psc_ops->rx_rdy(port))
  715. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  716. psc_ops->tx_clr_irq(port);
  717. if (psc_ops->tx_rdy(port))
  718. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  719. status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
  720. if (status & MPC52xx_PSC_D_DCD)
  721. uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
  722. if (status & MPC52xx_PSC_D_CTS)
  723. uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
  724. /* Limit number of iteration */
  725. if (!(--pass))
  726. keepgoing = 0;
  727. } while (keepgoing);
  728. spin_unlock(&port->lock);
  729. return IRQ_HANDLED;
  730. }
  731. /* ======================================================================== */
  732. /* Console ( if applicable ) */
  733. /* ======================================================================== */
  734. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  735. static void __init
  736. mpc52xx_console_get_options(struct uart_port *port,
  737. int *baud, int *parity, int *bits, int *flow)
  738. {
  739. struct mpc52xx_psc __iomem *psc = PSC(port);
  740. unsigned char mr1;
  741. pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
  742. /* Read the mode registers */
  743. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  744. mr1 = in_8(&psc->mode);
  745. /* CT{U,L}R are write-only ! */
  746. *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  747. /* Parse them */
  748. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  749. case MPC52xx_PSC_MODE_5_BITS:
  750. *bits = 5;
  751. break;
  752. case MPC52xx_PSC_MODE_6_BITS:
  753. *bits = 6;
  754. break;
  755. case MPC52xx_PSC_MODE_7_BITS:
  756. *bits = 7;
  757. break;
  758. case MPC52xx_PSC_MODE_8_BITS:
  759. default:
  760. *bits = 8;
  761. }
  762. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  763. *parity = 'n';
  764. else
  765. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  766. }
  767. static void
  768. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  769. {
  770. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  771. unsigned int i, j;
  772. /* Disable interrupts */
  773. psc_ops->cw_disable_ints(port);
  774. /* Wait the TX buffer to be empty */
  775. j = 5000000; /* Maximum wait */
  776. while (!mpc52xx_uart_tx_empty(port) && --j)
  777. udelay(1);
  778. /* Write all the chars */
  779. for (i = 0; i < count; i++, s++) {
  780. /* Line return handling */
  781. if (*s == '\n')
  782. psc_ops->write_char(port, '\r');
  783. /* Send the char */
  784. psc_ops->write_char(port, *s);
  785. /* Wait the TX buffer to be empty */
  786. j = 20000; /* Maximum wait */
  787. while (!mpc52xx_uart_tx_empty(port) && --j)
  788. udelay(1);
  789. }
  790. /* Restore interrupt state */
  791. psc_ops->cw_restore_ints(port);
  792. }
  793. static int __init
  794. mpc52xx_console_setup(struct console *co, char *options)
  795. {
  796. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  797. struct device_node *np = mpc52xx_uart_nodes[co->index];
  798. unsigned int uartclk;
  799. struct resource res;
  800. int ret;
  801. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  802. int bits = 8;
  803. int parity = 'n';
  804. int flow = 'n';
  805. pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
  806. co, co->index, options);
  807. if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
  808. pr_debug("PSC%x out of range\n", co->index);
  809. return -EINVAL;
  810. }
  811. if (!np) {
  812. pr_debug("PSC%x not found in device tree\n", co->index);
  813. return -EINVAL;
  814. }
  815. pr_debug("Console on ttyPSC%x is %s\n",
  816. co->index, mpc52xx_uart_nodes[co->index]->full_name);
  817. /* Fetch register locations */
  818. ret = of_address_to_resource(np, 0, &res);
  819. if (ret) {
  820. pr_debug("Could not get resources for PSC%x\n", co->index);
  821. return ret;
  822. }
  823. uartclk = psc_ops->getuartclk(np);
  824. if (uartclk == 0) {
  825. pr_debug("Could not find uart clock frequency!\n");
  826. return -EINVAL;
  827. }
  828. /* Basic port init. Needed since we use some uart_??? func before
  829. * real init for early access */
  830. spin_lock_init(&port->lock);
  831. port->uartclk = uartclk;
  832. port->ops = &mpc52xx_uart_ops;
  833. port->mapbase = res.start;
  834. port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
  835. port->irq = irq_of_parse_and_map(np, 0);
  836. if (port->membase == NULL)
  837. return -EINVAL;
  838. pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
  839. (void *)port->mapbase, port->membase,
  840. port->irq, port->uartclk);
  841. /* Setup the port parameters accoding to options */
  842. if (options)
  843. uart_parse_options(options, &baud, &parity, &bits, &flow);
  844. else
  845. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  846. pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
  847. baud, bits, parity, flow);
  848. return uart_set_options(port, co, baud, parity, bits, flow);
  849. }
  850. static struct uart_driver mpc52xx_uart_driver;
  851. static struct console mpc52xx_console = {
  852. .name = "ttyPSC",
  853. .write = mpc52xx_console_write,
  854. .device = uart_console_device,
  855. .setup = mpc52xx_console_setup,
  856. .flags = CON_PRINTBUFFER,
  857. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
  858. .data = &mpc52xx_uart_driver,
  859. };
  860. static int __init
  861. mpc52xx_console_init(void)
  862. {
  863. mpc52xx_uart_of_enumerate();
  864. register_console(&mpc52xx_console);
  865. return 0;
  866. }
  867. console_initcall(mpc52xx_console_init);
  868. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  869. #else
  870. #define MPC52xx_PSC_CONSOLE NULL
  871. #endif
  872. /* ======================================================================== */
  873. /* UART Driver */
  874. /* ======================================================================== */
  875. static struct uart_driver mpc52xx_uart_driver = {
  876. .driver_name = "mpc52xx_psc_uart",
  877. .dev_name = "ttyPSC",
  878. .major = SERIAL_PSC_MAJOR,
  879. .minor = SERIAL_PSC_MINOR,
  880. .nr = MPC52xx_PSC_MAXNUM,
  881. .cons = MPC52xx_PSC_CONSOLE,
  882. };
  883. /* ======================================================================== */
  884. /* OF Platform Driver */
  885. /* ======================================================================== */
  886. static struct of_device_id mpc52xx_uart_of_match[] = {
  887. #ifdef CONFIG_PPC_MPC52xx
  888. { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  889. /* binding used by old lite5200 device trees: */
  890. { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  891. /* binding used by efika: */
  892. { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
  893. #endif
  894. #ifdef CONFIG_PPC_MPC512x
  895. { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
  896. #endif
  897. {},
  898. };
  899. static int __devinit
  900. mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
  901. {
  902. int idx = -1;
  903. unsigned int uartclk;
  904. struct uart_port *port = NULL;
  905. struct resource res;
  906. int ret;
  907. dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
  908. /* Check validity & presence */
  909. for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
  910. if (mpc52xx_uart_nodes[idx] == op->node)
  911. break;
  912. if (idx >= MPC52xx_PSC_MAXNUM)
  913. return -EINVAL;
  914. pr_debug("Found %s assigned to ttyPSC%x\n",
  915. mpc52xx_uart_nodes[idx]->full_name, idx);
  916. uartclk = psc_ops->getuartclk(op->node);
  917. if (uartclk == 0) {
  918. dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
  919. return -EINVAL;
  920. }
  921. /* Init the port structure */
  922. port = &mpc52xx_uart_ports[idx];
  923. spin_lock_init(&port->lock);
  924. port->uartclk = uartclk;
  925. port->fifosize = 512;
  926. port->iotype = UPIO_MEM;
  927. port->flags = UPF_BOOT_AUTOCONF |
  928. (uart_console(port) ? 0 : UPF_IOREMAP);
  929. port->line = idx;
  930. port->ops = &mpc52xx_uart_ops;
  931. port->dev = &op->dev;
  932. /* Search for IRQ and mapbase */
  933. ret = of_address_to_resource(op->node, 0, &res);
  934. if (ret)
  935. return ret;
  936. port->mapbase = res.start;
  937. if (!port->mapbase) {
  938. dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
  939. return -EINVAL;
  940. }
  941. port->irq = irq_of_parse_and_map(op->node, 0);
  942. if (port->irq == NO_IRQ) {
  943. dev_dbg(&op->dev, "Could not get irq\n");
  944. return -EINVAL;
  945. }
  946. dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
  947. (void *)port->mapbase, port->irq, port->uartclk);
  948. /* Add the port to the uart sub-system */
  949. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  950. if (ret) {
  951. irq_dispose_mapping(port->irq);
  952. return ret;
  953. }
  954. dev_set_drvdata(&op->dev, (void *)port);
  955. return 0;
  956. }
  957. static int
  958. mpc52xx_uart_of_remove(struct of_device *op)
  959. {
  960. struct uart_port *port = dev_get_drvdata(&op->dev);
  961. dev_set_drvdata(&op->dev, NULL);
  962. if (port) {
  963. uart_remove_one_port(&mpc52xx_uart_driver, port);
  964. irq_dispose_mapping(port->irq);
  965. }
  966. return 0;
  967. }
  968. #ifdef CONFIG_PM
  969. static int
  970. mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
  971. {
  972. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  973. if (port)
  974. uart_suspend_port(&mpc52xx_uart_driver, port);
  975. return 0;
  976. }
  977. static int
  978. mpc52xx_uart_of_resume(struct of_device *op)
  979. {
  980. struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
  981. if (port)
  982. uart_resume_port(&mpc52xx_uart_driver, port);
  983. return 0;
  984. }
  985. #endif
  986. static void
  987. mpc52xx_uart_of_assign(struct device_node *np)
  988. {
  989. int i;
  990. /* Find the first free PSC number */
  991. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  992. if (mpc52xx_uart_nodes[i] == NULL) {
  993. of_node_get(np);
  994. mpc52xx_uart_nodes[i] = np;
  995. return;
  996. }
  997. }
  998. }
  999. static void
  1000. mpc52xx_uart_of_enumerate(void)
  1001. {
  1002. static int enum_done;
  1003. struct device_node *np;
  1004. const struct of_device_id *match;
  1005. int i;
  1006. if (enum_done)
  1007. return;
  1008. /* Assign index to each PSC in device tree */
  1009. for_each_matching_node(np, mpc52xx_uart_of_match) {
  1010. match = of_match_node(mpc52xx_uart_of_match, np);
  1011. psc_ops = match->data;
  1012. mpc52xx_uart_of_assign(np);
  1013. }
  1014. enum_done = 1;
  1015. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1016. if (mpc52xx_uart_nodes[i])
  1017. pr_debug("%s assigned to ttyPSC%x\n",
  1018. mpc52xx_uart_nodes[i]->full_name, i);
  1019. }
  1020. }
  1021. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  1022. static struct of_platform_driver mpc52xx_uart_of_driver = {
  1023. .match_table = mpc52xx_uart_of_match,
  1024. .probe = mpc52xx_uart_of_probe,
  1025. .remove = mpc52xx_uart_of_remove,
  1026. #ifdef CONFIG_PM
  1027. .suspend = mpc52xx_uart_of_suspend,
  1028. .resume = mpc52xx_uart_of_resume,
  1029. #endif
  1030. .driver = {
  1031. .name = "mpc52xx-psc-uart",
  1032. },
  1033. };
  1034. /* ======================================================================== */
  1035. /* Module */
  1036. /* ======================================================================== */
  1037. static int __init
  1038. mpc52xx_uart_init(void)
  1039. {
  1040. int ret;
  1041. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  1042. ret = uart_register_driver(&mpc52xx_uart_driver);
  1043. if (ret) {
  1044. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  1045. __FILE__, ret);
  1046. return ret;
  1047. }
  1048. mpc52xx_uart_of_enumerate();
  1049. ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
  1050. if (ret) {
  1051. printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
  1052. __FILE__, ret);
  1053. uart_unregister_driver(&mpc52xx_uart_driver);
  1054. return ret;
  1055. }
  1056. return 0;
  1057. }
  1058. static void __exit
  1059. mpc52xx_uart_exit(void)
  1060. {
  1061. of_unregister_platform_driver(&mpc52xx_uart_of_driver);
  1062. uart_unregister_driver(&mpc52xx_uart_driver);
  1063. }
  1064. module_init(mpc52xx_uart_init);
  1065. module_exit(mpc52xx_uart_exit);
  1066. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  1067. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  1068. MODULE_LICENSE("GPL");