mpc52xx_uart.c 39 KB

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