mpc52xx_uart.c 36 KB

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