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