mpc52xx_uart.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
  4. *
  5. * FIXME According to the usermanual the status bits in the status register
  6. * are only updated when the peripherals access the FIFO and not when the
  7. * CPU access them. So since we use this bits to know when we stop writing
  8. * and reading, they may not be updated in-time and a race condition may
  9. * exists. But I haven't be able to prove this and I don't care. But if
  10. * any problem arises, it might worth checking. The TX/RX FIFO Stats
  11. * registers should be used in addition.
  12. * Update: Actually, they seem updated ... At least the bits we use.
  13. *
  14. *
  15. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  16. *
  17. * Some of the code has been inspired/copied from the 2.4 code written
  18. * by Dale Farnsworth <dfarnsworth@mvista.com>.
  19. *
  20. * Copyright (C) 2008 Freescale Semiconductor Inc.
  21. * John Rigby <jrigby@gmail.com>
  22. * Added support for MPC5121
  23. * Copyright (C) 2006 Secret Lab Technologies Ltd.
  24. * Grant Likely <grant.likely@secretlab.ca>
  25. * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
  26. * Copyright (C) 2003 MontaVista, Software, Inc.
  27. */
  28. #undef DEBUG
  29. #include <linux/device.h>
  30. #include <linux/module.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/serial.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/console.h>
  36. #include <linux/delay.h>
  37. #include <linux/io.h>
  38. #include <linux/of.h>
  39. #include <linux/of_address.h>
  40. #include <linux/of_irq.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/clk.h>
  43. #include <asm/mpc52xx.h>
  44. #include <asm/mpc52xx_psc.h>
  45. #include <linux/serial_core.h>
  46. /* We've been assigned a range on the "Low-density serial ports" major */
  47. #define SERIAL_PSC_MAJOR 204
  48. #define SERIAL_PSC_MINOR 148
  49. #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
  50. static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
  51. /* Rem: - We use the read_status_mask as a shadow of
  52. * psc->mpc52xx_psc_imr
  53. * - It's important that is array is all zero on start as we
  54. * use it to know if it's initialized or not ! If it's not sure
  55. * it's cleared, then a memset(...,0,...) should be added to
  56. * the console_init
  57. */
  58. /* lookup table for matching device nodes to index numbers */
  59. static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
  60. static void mpc52xx_uart_of_enumerate(void);
  61. #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
  62. /* Forward declaration of the interruption handling routine */
  63. static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
  64. static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
  65. /* ======================================================================== */
  66. /* PSC fifo operations for isolating differences between 52xx and 512x */
  67. /* ======================================================================== */
  68. struct psc_ops {
  69. void (*fifo_init)(struct uart_port *port);
  70. unsigned int (*raw_rx_rdy)(struct uart_port *port);
  71. unsigned int (*raw_tx_rdy)(struct uart_port *port);
  72. unsigned int (*rx_rdy)(struct uart_port *port);
  73. unsigned int (*tx_rdy)(struct uart_port *port);
  74. unsigned int (*tx_empty)(struct uart_port *port);
  75. void (*stop_rx)(struct uart_port *port);
  76. void (*start_tx)(struct uart_port *port);
  77. void (*stop_tx)(struct uart_port *port);
  78. void (*rx_clr_irq)(struct uart_port *port);
  79. void (*tx_clr_irq)(struct uart_port *port);
  80. void (*write_char)(struct uart_port *port, unsigned char c);
  81. unsigned char (*read_char)(struct uart_port *port);
  82. void (*cw_disable_ints)(struct uart_port *port);
  83. void (*cw_restore_ints)(struct uart_port *port);
  84. unsigned int (*set_baudrate)(struct uart_port *port,
  85. struct ktermios *new,
  86. const struct ktermios *old);
  87. int (*clock_alloc)(struct uart_port *port);
  88. void (*clock_relse)(struct uart_port *port);
  89. int (*clock)(struct uart_port *port, int enable);
  90. int (*fifoc_init)(void);
  91. void (*fifoc_uninit)(void);
  92. void (*get_irq)(struct uart_port *, struct device_node *);
  93. irqreturn_t (*handle_irq)(struct uart_port *port);
  94. u16 (*get_status)(struct uart_port *port);
  95. u8 (*get_ipcr)(struct uart_port *port);
  96. void (*command)(struct uart_port *port, u8 cmd);
  97. void (*set_mode)(struct uart_port *port, u8 mr1, u8 mr2);
  98. void (*set_rts)(struct uart_port *port, int state);
  99. void (*enable_ms)(struct uart_port *port);
  100. void (*set_sicr)(struct uart_port *port, u32 val);
  101. void (*set_imr)(struct uart_port *port, u16 val);
  102. u8 (*get_mr1)(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. static u16 mpc52xx_psc_get_status(struct uart_port *port)
  114. {
  115. return in_be16(&PSC(port)->mpc52xx_psc_status);
  116. }
  117. static u8 mpc52xx_psc_get_ipcr(struct uart_port *port)
  118. {
  119. return in_8(&PSC(port)->mpc52xx_psc_ipcr);
  120. }
  121. static void mpc52xx_psc_command(struct uart_port *port, u8 cmd)
  122. {
  123. out_8(&PSC(port)->command, cmd);
  124. }
  125. static void mpc52xx_psc_set_mode(struct uart_port *port, u8 mr1, u8 mr2)
  126. {
  127. out_8(&PSC(port)->command, MPC52xx_PSC_SEL_MODE_REG_1);
  128. out_8(&PSC(port)->mode, mr1);
  129. out_8(&PSC(port)->mode, mr2);
  130. }
  131. static void mpc52xx_psc_set_rts(struct uart_port *port, int state)
  132. {
  133. if (state)
  134. out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
  135. else
  136. out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
  137. }
  138. static void mpc52xx_psc_enable_ms(struct uart_port *port)
  139. {
  140. struct mpc52xx_psc __iomem *psc = PSC(port);
  141. /* clear D_*-bits by reading them */
  142. in_8(&psc->mpc52xx_psc_ipcr);
  143. /* enable CTS and DCD as IPC interrupts */
  144. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  145. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  146. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  147. }
  148. static void mpc52xx_psc_set_sicr(struct uart_port *port, u32 val)
  149. {
  150. out_be32(&PSC(port)->sicr, val);
  151. }
  152. static void mpc52xx_psc_set_imr(struct uart_port *port, u16 val)
  153. {
  154. out_be16(&PSC(port)->mpc52xx_psc_imr, val);
  155. }
  156. static u8 mpc52xx_psc_get_mr1(struct uart_port *port)
  157. {
  158. out_8(&PSC(port)->command, MPC52xx_PSC_SEL_MODE_REG_1);
  159. return in_8(&PSC(port)->mode);
  160. }
  161. #ifdef CONFIG_PPC_MPC52xx
  162. #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
  163. static void mpc52xx_psc_fifo_init(struct uart_port *port)
  164. {
  165. struct mpc52xx_psc __iomem *psc = PSC(port);
  166. struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
  167. out_8(&fifo->rfcntl, 0x00);
  168. out_be16(&fifo->rfalarm, 0x1ff);
  169. out_8(&fifo->tfcntl, 0x07);
  170. out_be16(&fifo->tfalarm, 0x80);
  171. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  172. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  173. }
  174. static unsigned int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
  175. {
  176. return in_be16(&PSC(port)->mpc52xx_psc_status)
  177. & MPC52xx_PSC_SR_RXRDY;
  178. }
  179. static unsigned int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
  180. {
  181. return in_be16(&PSC(port)->mpc52xx_psc_status)
  182. & MPC52xx_PSC_SR_TXRDY;
  183. }
  184. static unsigned int mpc52xx_psc_rx_rdy(struct uart_port *port)
  185. {
  186. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  187. & port->read_status_mask
  188. & MPC52xx_PSC_IMR_RXRDY;
  189. }
  190. static unsigned int mpc52xx_psc_tx_rdy(struct uart_port *port)
  191. {
  192. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  193. & port->read_status_mask
  194. & MPC52xx_PSC_IMR_TXRDY;
  195. }
  196. static unsigned int mpc52xx_psc_tx_empty(struct uart_port *port)
  197. {
  198. u16 sts = in_be16(&PSC(port)->mpc52xx_psc_status);
  199. return (sts & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  200. }
  201. static void mpc52xx_psc_start_tx(struct uart_port *port)
  202. {
  203. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  204. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  205. }
  206. static void mpc52xx_psc_stop_tx(struct uart_port *port)
  207. {
  208. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  209. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  210. }
  211. static void mpc52xx_psc_stop_rx(struct uart_port *port)
  212. {
  213. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  214. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  215. }
  216. static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
  217. {
  218. }
  219. static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
  220. {
  221. }
  222. static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
  223. {
  224. out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
  225. }
  226. static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
  227. {
  228. return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  229. }
  230. static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
  231. {
  232. out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
  233. }
  234. static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
  235. {
  236. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  237. }
  238. static unsigned int mpc5200_psc_set_baudrate(struct uart_port *port,
  239. struct ktermios *new,
  240. const struct ktermios *old)
  241. {
  242. unsigned int baud;
  243. unsigned int divisor;
  244. /* The 5200 has a fixed /32 prescaler, uartclk contains the ipb freq */
  245. baud = uart_get_baud_rate(port, new, old,
  246. port->uartclk / (32 * 0xffff) + 1,
  247. port->uartclk / 32);
  248. divisor = (port->uartclk + 16 * baud) / (32 * baud);
  249. /* enable the /32 prescaler and set the divisor */
  250. mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
  251. return baud;
  252. }
  253. static unsigned int mpc5200b_psc_set_baudrate(struct uart_port *port,
  254. struct ktermios *new,
  255. const struct ktermios *old)
  256. {
  257. unsigned int baud;
  258. unsigned int divisor;
  259. u16 prescaler;
  260. /* The 5200B has a selectable /4 or /32 prescaler, uartclk contains the
  261. * ipb freq */
  262. baud = uart_get_baud_rate(port, new, old,
  263. port->uartclk / (32 * 0xffff) + 1,
  264. port->uartclk / 4);
  265. divisor = (port->uartclk + 2 * baud) / (4 * baud);
  266. /* select the proper prescaler and set the divisor
  267. * prefer high prescaler for more tolerance on low baudrates */
  268. if (divisor > 0xffff || baud <= 115200) {
  269. divisor = (divisor + 4) / 8;
  270. prescaler = 0xdd00; /* /32 */
  271. } else
  272. prescaler = 0xff00; /* /4 */
  273. mpc52xx_set_divisor(PSC(port), prescaler, divisor);
  274. return baud;
  275. }
  276. static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
  277. {
  278. port->irqflags = 0;
  279. port->irq = irq_of_parse_and_map(np, 0);
  280. }
  281. /* 52xx specific interrupt handler. The caller holds the port lock */
  282. static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
  283. {
  284. return mpc5xxx_uart_process_int(port);
  285. }
  286. static const struct psc_ops mpc52xx_psc_ops = {
  287. .fifo_init = mpc52xx_psc_fifo_init,
  288. .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
  289. .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
  290. .rx_rdy = mpc52xx_psc_rx_rdy,
  291. .tx_rdy = mpc52xx_psc_tx_rdy,
  292. .tx_empty = mpc52xx_psc_tx_empty,
  293. .stop_rx = mpc52xx_psc_stop_rx,
  294. .start_tx = mpc52xx_psc_start_tx,
  295. .stop_tx = mpc52xx_psc_stop_tx,
  296. .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
  297. .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
  298. .write_char = mpc52xx_psc_write_char,
  299. .read_char = mpc52xx_psc_read_char,
  300. .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
  301. .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
  302. .set_baudrate = mpc5200_psc_set_baudrate,
  303. .get_irq = mpc52xx_psc_get_irq,
  304. .handle_irq = mpc52xx_psc_handle_irq,
  305. .get_status = mpc52xx_psc_get_status,
  306. .get_ipcr = mpc52xx_psc_get_ipcr,
  307. .command = mpc52xx_psc_command,
  308. .set_mode = mpc52xx_psc_set_mode,
  309. .set_rts = mpc52xx_psc_set_rts,
  310. .enable_ms = mpc52xx_psc_enable_ms,
  311. .set_sicr = mpc52xx_psc_set_sicr,
  312. .set_imr = mpc52xx_psc_set_imr,
  313. .get_mr1 = mpc52xx_psc_get_mr1,
  314. };
  315. static const struct psc_ops mpc5200b_psc_ops = {
  316. .fifo_init = mpc52xx_psc_fifo_init,
  317. .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
  318. .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
  319. .rx_rdy = mpc52xx_psc_rx_rdy,
  320. .tx_rdy = mpc52xx_psc_tx_rdy,
  321. .tx_empty = mpc52xx_psc_tx_empty,
  322. .stop_rx = mpc52xx_psc_stop_rx,
  323. .start_tx = mpc52xx_psc_start_tx,
  324. .stop_tx = mpc52xx_psc_stop_tx,
  325. .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
  326. .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
  327. .write_char = mpc52xx_psc_write_char,
  328. .read_char = mpc52xx_psc_read_char,
  329. .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
  330. .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
  331. .set_baudrate = mpc5200b_psc_set_baudrate,
  332. .get_irq = mpc52xx_psc_get_irq,
  333. .handle_irq = mpc52xx_psc_handle_irq,
  334. .get_status = mpc52xx_psc_get_status,
  335. .get_ipcr = mpc52xx_psc_get_ipcr,
  336. .command = mpc52xx_psc_command,
  337. .set_mode = mpc52xx_psc_set_mode,
  338. .set_rts = mpc52xx_psc_set_rts,
  339. .enable_ms = mpc52xx_psc_enable_ms,
  340. .set_sicr = mpc52xx_psc_set_sicr,
  341. .set_imr = mpc52xx_psc_set_imr,
  342. .get_mr1 = mpc52xx_psc_get_mr1,
  343. };
  344. #endif /* CONFIG_PPC_MPC52xx */
  345. #ifdef CONFIG_PPC_MPC512x
  346. #define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
  347. /* PSC FIFO Controller for mpc512x */
  348. struct psc_fifoc {
  349. u32 fifoc_cmd;
  350. u32 fifoc_int;
  351. u32 fifoc_dma;
  352. u32 fifoc_axe;
  353. u32 fifoc_debug;
  354. };
  355. static struct psc_fifoc __iomem *psc_fifoc;
  356. static unsigned int psc_fifoc_irq;
  357. static struct clk *psc_fifoc_clk;
  358. static void mpc512x_psc_fifo_init(struct uart_port *port)
  359. {
  360. /* /32 prescaler */
  361. out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
  362. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  363. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  364. out_be32(&FIFO_512x(port)->txalarm, 1);
  365. out_be32(&FIFO_512x(port)->tximr, 0);
  366. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  367. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  368. out_be32(&FIFO_512x(port)->rxalarm, 1);
  369. out_be32(&FIFO_512x(port)->rximr, 0);
  370. out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  371. out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  372. }
  373. static unsigned int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
  374. {
  375. return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  376. }
  377. static unsigned int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
  378. {
  379. return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  380. }
  381. static unsigned int mpc512x_psc_rx_rdy(struct uart_port *port)
  382. {
  383. return in_be32(&FIFO_512x(port)->rxsr)
  384. & in_be32(&FIFO_512x(port)->rximr)
  385. & MPC512x_PSC_FIFO_ALARM;
  386. }
  387. static unsigned int mpc512x_psc_tx_rdy(struct uart_port *port)
  388. {
  389. return in_be32(&FIFO_512x(port)->txsr)
  390. & in_be32(&FIFO_512x(port)->tximr)
  391. & MPC512x_PSC_FIFO_ALARM;
  392. }
  393. static unsigned int mpc512x_psc_tx_empty(struct uart_port *port)
  394. {
  395. return in_be32(&FIFO_512x(port)->txsr)
  396. & MPC512x_PSC_FIFO_EMPTY;
  397. }
  398. static void mpc512x_psc_stop_rx(struct uart_port *port)
  399. {
  400. unsigned long rx_fifo_imr;
  401. rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
  402. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  403. out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
  404. }
  405. static void mpc512x_psc_start_tx(struct uart_port *port)
  406. {
  407. unsigned long tx_fifo_imr;
  408. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  409. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  410. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  411. }
  412. static void mpc512x_psc_stop_tx(struct uart_port *port)
  413. {
  414. unsigned long tx_fifo_imr;
  415. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  416. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  417. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  418. }
  419. static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
  420. {
  421. out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
  422. }
  423. static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
  424. {
  425. out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
  426. }
  427. static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
  428. {
  429. out_8(&FIFO_512x(port)->txdata_8, c);
  430. }
  431. static unsigned char mpc512x_psc_read_char(struct uart_port *port)
  432. {
  433. return in_8(&FIFO_512x(port)->rxdata_8);
  434. }
  435. static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
  436. {
  437. port->read_status_mask =
  438. in_be32(&FIFO_512x(port)->tximr) << 16 |
  439. in_be32(&FIFO_512x(port)->rximr);
  440. out_be32(&FIFO_512x(port)->tximr, 0);
  441. out_be32(&FIFO_512x(port)->rximr, 0);
  442. }
  443. static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
  444. {
  445. out_be32(&FIFO_512x(port)->tximr,
  446. (port->read_status_mask >> 16) & 0x7f);
  447. out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
  448. }
  449. static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
  450. struct ktermios *new,
  451. const struct ktermios *old)
  452. {
  453. unsigned int baud;
  454. unsigned int divisor;
  455. /*
  456. * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
  457. * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
  458. * Furthermore, it states that "After reset, the prescaler by 10
  459. * for the UART mode is selected", but the reset register value is
  460. * 0x0000 which means a /32 prescaler. This is wrong.
  461. *
  462. * In reality using /32 prescaler doesn't work, as it is not supported!
  463. * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
  464. * Chapter 4.1 PSC in UART Mode.
  465. * Calculate with a /16 prescaler here.
  466. */
  467. /* uartclk contains the ips freq */
  468. baud = uart_get_baud_rate(port, new, old,
  469. port->uartclk / (16 * 0xffff) + 1,
  470. port->uartclk / 16);
  471. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  472. /* enable the /16 prescaler and set the divisor */
  473. mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
  474. return baud;
  475. }
  476. /* Init PSC FIFO Controller */
  477. static int __init mpc512x_psc_fifoc_init(void)
  478. {
  479. int err;
  480. struct device_node *np;
  481. struct clk *clk;
  482. /* default error code, potentially overwritten by clock calls */
  483. err = -ENODEV;
  484. np = of_find_compatible_node(NULL, NULL,
  485. "fsl,mpc5121-psc-fifo");
  486. if (!np) {
  487. pr_err("%s: Can't find FIFOC node\n", __func__);
  488. goto out_err;
  489. }
  490. clk = of_clk_get(np, 0);
  491. if (IS_ERR(clk)) {
  492. /* backwards compat with device trees that lack clock specs */
  493. clk = clk_get_sys(np->name, "ipg");
  494. }
  495. if (IS_ERR(clk)) {
  496. pr_err("%s: Can't lookup FIFO clock\n", __func__);
  497. err = PTR_ERR(clk);
  498. goto out_ofnode_put;
  499. }
  500. if (clk_prepare_enable(clk)) {
  501. pr_err("%s: Can't enable FIFO clock\n", __func__);
  502. clk_put(clk);
  503. goto out_ofnode_put;
  504. }
  505. psc_fifoc_clk = clk;
  506. psc_fifoc = of_iomap(np, 0);
  507. if (!psc_fifoc) {
  508. pr_err("%s: Can't map FIFOC\n", __func__);
  509. goto out_clk_disable;
  510. }
  511. psc_fifoc_irq = irq_of_parse_and_map(np, 0);
  512. if (psc_fifoc_irq == 0) {
  513. pr_err("%s: Can't get FIFOC irq\n", __func__);
  514. goto out_unmap;
  515. }
  516. of_node_put(np);
  517. return 0;
  518. out_unmap:
  519. iounmap(psc_fifoc);
  520. out_clk_disable:
  521. clk_disable_unprepare(psc_fifoc_clk);
  522. clk_put(psc_fifoc_clk);
  523. out_ofnode_put:
  524. of_node_put(np);
  525. out_err:
  526. return err;
  527. }
  528. static void __exit mpc512x_psc_fifoc_uninit(void)
  529. {
  530. iounmap(psc_fifoc);
  531. /* disable the clock, errors are not fatal */
  532. if (psc_fifoc_clk) {
  533. clk_disable_unprepare(psc_fifoc_clk);
  534. clk_put(psc_fifoc_clk);
  535. psc_fifoc_clk = NULL;
  536. }
  537. }
  538. /* 512x specific interrupt handler. The caller holds the port lock */
  539. static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
  540. {
  541. unsigned long fifoc_int;
  542. int psc_num;
  543. /* Read pending PSC FIFOC interrupts */
  544. fifoc_int = in_be32(&psc_fifoc->fifoc_int);
  545. /* Check if it is an interrupt for this port */
  546. psc_num = (port->mapbase & 0xf00) >> 8;
  547. if (test_bit(psc_num, &fifoc_int) ||
  548. test_bit(psc_num + 16, &fifoc_int))
  549. return mpc5xxx_uart_process_int(port);
  550. return IRQ_NONE;
  551. }
  552. static struct clk *psc_mclk_clk[MPC52xx_PSC_MAXNUM];
  553. static struct clk *psc_ipg_clk[MPC52xx_PSC_MAXNUM];
  554. /* called from within the .request_port() callback (allocation) */
  555. static int mpc512x_psc_alloc_clock(struct uart_port *port)
  556. {
  557. int psc_num;
  558. struct clk *clk;
  559. int err;
  560. psc_num = (port->mapbase & 0xf00) >> 8;
  561. clk = devm_clk_get(port->dev, "mclk");
  562. if (IS_ERR(clk)) {
  563. dev_err(port->dev, "Failed to get MCLK!\n");
  564. err = PTR_ERR(clk);
  565. goto out_err;
  566. }
  567. err = clk_prepare_enable(clk);
  568. if (err) {
  569. dev_err(port->dev, "Failed to enable MCLK!\n");
  570. goto out_err;
  571. }
  572. psc_mclk_clk[psc_num] = clk;
  573. clk = devm_clk_get(port->dev, "ipg");
  574. if (IS_ERR(clk)) {
  575. dev_err(port->dev, "Failed to get IPG clock!\n");
  576. err = PTR_ERR(clk);
  577. goto out_err;
  578. }
  579. err = clk_prepare_enable(clk);
  580. if (err) {
  581. dev_err(port->dev, "Failed to enable IPG clock!\n");
  582. goto out_err;
  583. }
  584. psc_ipg_clk[psc_num] = clk;
  585. return 0;
  586. out_err:
  587. if (psc_mclk_clk[psc_num]) {
  588. clk_disable_unprepare(psc_mclk_clk[psc_num]);
  589. psc_mclk_clk[psc_num] = NULL;
  590. }
  591. if (psc_ipg_clk[psc_num]) {
  592. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  593. psc_ipg_clk[psc_num] = NULL;
  594. }
  595. return err;
  596. }
  597. /* called from within the .release_port() callback (release) */
  598. static void mpc512x_psc_relse_clock(struct uart_port *port)
  599. {
  600. int psc_num;
  601. struct clk *clk;
  602. psc_num = (port->mapbase & 0xf00) >> 8;
  603. clk = psc_mclk_clk[psc_num];
  604. if (clk) {
  605. clk_disable_unprepare(clk);
  606. psc_mclk_clk[psc_num] = NULL;
  607. }
  608. if (psc_ipg_clk[psc_num]) {
  609. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  610. psc_ipg_clk[psc_num] = NULL;
  611. }
  612. }
  613. /* implementation of the .clock() callback (enable/disable) */
  614. static int mpc512x_psc_endis_clock(struct uart_port *port, int enable)
  615. {
  616. int psc_num;
  617. struct clk *psc_clk;
  618. int ret;
  619. if (uart_console(port))
  620. return 0;
  621. psc_num = (port->mapbase & 0xf00) >> 8;
  622. psc_clk = psc_mclk_clk[psc_num];
  623. if (!psc_clk) {
  624. dev_err(port->dev, "Failed to get PSC clock entry!\n");
  625. return -ENODEV;
  626. }
  627. dev_dbg(port->dev, "mclk %sable\n", enable ? "en" : "dis");
  628. if (enable) {
  629. ret = clk_enable(psc_clk);
  630. if (ret)
  631. dev_err(port->dev, "Failed to enable MCLK!\n");
  632. return ret;
  633. } else {
  634. clk_disable(psc_clk);
  635. return 0;
  636. }
  637. }
  638. static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
  639. {
  640. port->irqflags = IRQF_SHARED;
  641. port->irq = psc_fifoc_irq;
  642. }
  643. #define PSC_5125(port) ((struct mpc5125_psc __iomem *)((port)->membase))
  644. #define FIFO_5125(port) ((struct mpc512x_psc_fifo __iomem *)(PSC_5125(port)+1))
  645. static void mpc5125_psc_fifo_init(struct uart_port *port)
  646. {
  647. /* /32 prescaler */
  648. out_8(&PSC_5125(port)->mpc52xx_psc_clock_select, 0xdd);
  649. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  650. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  651. out_be32(&FIFO_5125(port)->txalarm, 1);
  652. out_be32(&FIFO_5125(port)->tximr, 0);
  653. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  654. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  655. out_be32(&FIFO_5125(port)->rxalarm, 1);
  656. out_be32(&FIFO_5125(port)->rximr, 0);
  657. out_be32(&FIFO_5125(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  658. out_be32(&FIFO_5125(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  659. }
  660. static unsigned int mpc5125_psc_raw_rx_rdy(struct uart_port *port)
  661. {
  662. return !(in_be32(&FIFO_5125(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  663. }
  664. static unsigned int mpc5125_psc_raw_tx_rdy(struct uart_port *port)
  665. {
  666. return !(in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  667. }
  668. static unsigned int mpc5125_psc_rx_rdy(struct uart_port *port)
  669. {
  670. return in_be32(&FIFO_5125(port)->rxsr) &
  671. in_be32(&FIFO_5125(port)->rximr) & MPC512x_PSC_FIFO_ALARM;
  672. }
  673. static unsigned int mpc5125_psc_tx_rdy(struct uart_port *port)
  674. {
  675. return in_be32(&FIFO_5125(port)->txsr) &
  676. in_be32(&FIFO_5125(port)->tximr) & MPC512x_PSC_FIFO_ALARM;
  677. }
  678. static unsigned int mpc5125_psc_tx_empty(struct uart_port *port)
  679. {
  680. return in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_EMPTY;
  681. }
  682. static void mpc5125_psc_stop_rx(struct uart_port *port)
  683. {
  684. unsigned long rx_fifo_imr;
  685. rx_fifo_imr = in_be32(&FIFO_5125(port)->rximr);
  686. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  687. out_be32(&FIFO_5125(port)->rximr, rx_fifo_imr);
  688. }
  689. static void mpc5125_psc_start_tx(struct uart_port *port)
  690. {
  691. unsigned long tx_fifo_imr;
  692. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  693. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  694. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  695. }
  696. static void mpc5125_psc_stop_tx(struct uart_port *port)
  697. {
  698. unsigned long tx_fifo_imr;
  699. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  700. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  701. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  702. }
  703. static void mpc5125_psc_rx_clr_irq(struct uart_port *port)
  704. {
  705. out_be32(&FIFO_5125(port)->rxisr, in_be32(&FIFO_5125(port)->rxisr));
  706. }
  707. static void mpc5125_psc_tx_clr_irq(struct uart_port *port)
  708. {
  709. out_be32(&FIFO_5125(port)->txisr, in_be32(&FIFO_5125(port)->txisr));
  710. }
  711. static void mpc5125_psc_write_char(struct uart_port *port, unsigned char c)
  712. {
  713. out_8(&FIFO_5125(port)->txdata_8, c);
  714. }
  715. static unsigned char mpc5125_psc_read_char(struct uart_port *port)
  716. {
  717. return in_8(&FIFO_5125(port)->rxdata_8);
  718. }
  719. static void mpc5125_psc_cw_disable_ints(struct uart_port *port)
  720. {
  721. port->read_status_mask =
  722. in_be32(&FIFO_5125(port)->tximr) << 16 |
  723. in_be32(&FIFO_5125(port)->rximr);
  724. out_be32(&FIFO_5125(port)->tximr, 0);
  725. out_be32(&FIFO_5125(port)->rximr, 0);
  726. }
  727. static void mpc5125_psc_cw_restore_ints(struct uart_port *port)
  728. {
  729. out_be32(&FIFO_5125(port)->tximr,
  730. (port->read_status_mask >> 16) & 0x7f);
  731. out_be32(&FIFO_5125(port)->rximr, port->read_status_mask & 0x7f);
  732. }
  733. static inline void mpc5125_set_divisor(struct mpc5125_psc __iomem *psc,
  734. u8 prescaler, unsigned int divisor)
  735. {
  736. /* select prescaler */
  737. out_8(&psc->mpc52xx_psc_clock_select, prescaler);
  738. out_8(&psc->ctur, divisor >> 8);
  739. out_8(&psc->ctlr, divisor & 0xff);
  740. }
  741. static unsigned int mpc5125_psc_set_baudrate(struct uart_port *port,
  742. struct ktermios *new,
  743. const struct ktermios *old)
  744. {
  745. unsigned int baud;
  746. unsigned int divisor;
  747. /*
  748. * Calculate with a /16 prescaler here.
  749. */
  750. /* uartclk contains the ips freq */
  751. baud = uart_get_baud_rate(port, new, old,
  752. port->uartclk / (16 * 0xffff) + 1,
  753. port->uartclk / 16);
  754. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  755. /* enable the /16 prescaler and set the divisor */
  756. mpc5125_set_divisor(PSC_5125(port), 0xdd, divisor);
  757. return baud;
  758. }
  759. /*
  760. * MPC5125 have compatible PSC FIFO Controller.
  761. * Special init not needed.
  762. */
  763. static u16 mpc5125_psc_get_status(struct uart_port *port)
  764. {
  765. return in_be16(&PSC_5125(port)->mpc52xx_psc_status);
  766. }
  767. static u8 mpc5125_psc_get_ipcr(struct uart_port *port)
  768. {
  769. return in_8(&PSC_5125(port)->mpc52xx_psc_ipcr);
  770. }
  771. static void mpc5125_psc_command(struct uart_port *port, u8 cmd)
  772. {
  773. out_8(&PSC_5125(port)->command, cmd);
  774. }
  775. static void mpc5125_psc_set_mode(struct uart_port *port, u8 mr1, u8 mr2)
  776. {
  777. out_8(&PSC_5125(port)->mr1, mr1);
  778. out_8(&PSC_5125(port)->mr2, mr2);
  779. }
  780. static void mpc5125_psc_set_rts(struct uart_port *port, int state)
  781. {
  782. if (state & TIOCM_RTS)
  783. out_8(&PSC_5125(port)->op1, MPC52xx_PSC_OP_RTS);
  784. else
  785. out_8(&PSC_5125(port)->op0, MPC52xx_PSC_OP_RTS);
  786. }
  787. static void mpc5125_psc_enable_ms(struct uart_port *port)
  788. {
  789. struct mpc5125_psc __iomem *psc = PSC_5125(port);
  790. /* clear D_*-bits by reading them */
  791. in_8(&psc->mpc52xx_psc_ipcr);
  792. /* enable CTS and DCD as IPC interrupts */
  793. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  794. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  795. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  796. }
  797. static void mpc5125_psc_set_sicr(struct uart_port *port, u32 val)
  798. {
  799. out_be32(&PSC_5125(port)->sicr, val);
  800. }
  801. static void mpc5125_psc_set_imr(struct uart_port *port, u16 val)
  802. {
  803. out_be16(&PSC_5125(port)->mpc52xx_psc_imr, val);
  804. }
  805. static u8 mpc5125_psc_get_mr1(struct uart_port *port)
  806. {
  807. return in_8(&PSC_5125(port)->mr1);
  808. }
  809. static const struct psc_ops mpc5125_psc_ops = {
  810. .fifo_init = mpc5125_psc_fifo_init,
  811. .raw_rx_rdy = mpc5125_psc_raw_rx_rdy,
  812. .raw_tx_rdy = mpc5125_psc_raw_tx_rdy,
  813. .rx_rdy = mpc5125_psc_rx_rdy,
  814. .tx_rdy = mpc5125_psc_tx_rdy,
  815. .tx_empty = mpc5125_psc_tx_empty,
  816. .stop_rx = mpc5125_psc_stop_rx,
  817. .start_tx = mpc5125_psc_start_tx,
  818. .stop_tx = mpc5125_psc_stop_tx,
  819. .rx_clr_irq = mpc5125_psc_rx_clr_irq,
  820. .tx_clr_irq = mpc5125_psc_tx_clr_irq,
  821. .write_char = mpc5125_psc_write_char,
  822. .read_char = mpc5125_psc_read_char,
  823. .cw_disable_ints = mpc5125_psc_cw_disable_ints,
  824. .cw_restore_ints = mpc5125_psc_cw_restore_ints,
  825. .set_baudrate = mpc5125_psc_set_baudrate,
  826. .clock_alloc = mpc512x_psc_alloc_clock,
  827. .clock_relse = mpc512x_psc_relse_clock,
  828. .clock = mpc512x_psc_endis_clock,
  829. .fifoc_init = mpc512x_psc_fifoc_init,
  830. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  831. .get_irq = mpc512x_psc_get_irq,
  832. .handle_irq = mpc512x_psc_handle_irq,
  833. .get_status = mpc5125_psc_get_status,
  834. .get_ipcr = mpc5125_psc_get_ipcr,
  835. .command = mpc5125_psc_command,
  836. .set_mode = mpc5125_psc_set_mode,
  837. .set_rts = mpc5125_psc_set_rts,
  838. .enable_ms = mpc5125_psc_enable_ms,
  839. .set_sicr = mpc5125_psc_set_sicr,
  840. .set_imr = mpc5125_psc_set_imr,
  841. .get_mr1 = mpc5125_psc_get_mr1,
  842. };
  843. static const struct psc_ops mpc512x_psc_ops = {
  844. .fifo_init = mpc512x_psc_fifo_init,
  845. .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
  846. .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
  847. .rx_rdy = mpc512x_psc_rx_rdy,
  848. .tx_rdy = mpc512x_psc_tx_rdy,
  849. .tx_empty = mpc512x_psc_tx_empty,
  850. .stop_rx = mpc512x_psc_stop_rx,
  851. .start_tx = mpc512x_psc_start_tx,
  852. .stop_tx = mpc512x_psc_stop_tx,
  853. .rx_clr_irq = mpc512x_psc_rx_clr_irq,
  854. .tx_clr_irq = mpc512x_psc_tx_clr_irq,
  855. .write_char = mpc512x_psc_write_char,
  856. .read_char = mpc512x_psc_read_char,
  857. .cw_disable_ints = mpc512x_psc_cw_disable_ints,
  858. .cw_restore_ints = mpc512x_psc_cw_restore_ints,
  859. .set_baudrate = mpc512x_psc_set_baudrate,
  860. .clock_alloc = mpc512x_psc_alloc_clock,
  861. .clock_relse = mpc512x_psc_relse_clock,
  862. .clock = mpc512x_psc_endis_clock,
  863. .fifoc_init = mpc512x_psc_fifoc_init,
  864. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  865. .get_irq = mpc512x_psc_get_irq,
  866. .handle_irq = mpc512x_psc_handle_irq,
  867. .get_status = mpc52xx_psc_get_status,
  868. .get_ipcr = mpc52xx_psc_get_ipcr,
  869. .command = mpc52xx_psc_command,
  870. .set_mode = mpc52xx_psc_set_mode,
  871. .set_rts = mpc52xx_psc_set_rts,
  872. .enable_ms = mpc52xx_psc_enable_ms,
  873. .set_sicr = mpc52xx_psc_set_sicr,
  874. .set_imr = mpc52xx_psc_set_imr,
  875. .get_mr1 = mpc52xx_psc_get_mr1,
  876. };
  877. #endif /* CONFIG_PPC_MPC512x */
  878. static const struct psc_ops *psc_ops;
  879. /* ======================================================================== */
  880. /* UART operations */
  881. /* ======================================================================== */
  882. static unsigned int
  883. mpc52xx_uart_tx_empty(struct uart_port *port)
  884. {
  885. return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
  886. }
  887. static void
  888. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  889. {
  890. psc_ops->set_rts(port, mctrl & TIOCM_RTS);
  891. }
  892. static unsigned int
  893. mpc52xx_uart_get_mctrl(struct uart_port *port)
  894. {
  895. unsigned int ret = TIOCM_DSR;
  896. u8 status = psc_ops->get_ipcr(port);
  897. if (!(status & MPC52xx_PSC_CTS))
  898. ret |= TIOCM_CTS;
  899. if (!(status & MPC52xx_PSC_DCD))
  900. ret |= TIOCM_CAR;
  901. return ret;
  902. }
  903. static void
  904. mpc52xx_uart_stop_tx(struct uart_port *port)
  905. {
  906. /* port->lock taken by caller */
  907. psc_ops->stop_tx(port);
  908. }
  909. static void
  910. mpc52xx_uart_start_tx(struct uart_port *port)
  911. {
  912. /* port->lock taken by caller */
  913. psc_ops->start_tx(port);
  914. }
  915. static void
  916. mpc52xx_uart_stop_rx(struct uart_port *port)
  917. {
  918. /* port->lock taken by caller */
  919. psc_ops->stop_rx(port);
  920. }
  921. static void
  922. mpc52xx_uart_enable_ms(struct uart_port *port)
  923. {
  924. psc_ops->enable_ms(port);
  925. }
  926. static void
  927. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  928. {
  929. unsigned long flags;
  930. uart_port_lock_irqsave(port, &flags);
  931. if (ctl == -1)
  932. psc_ops->command(port, MPC52xx_PSC_START_BRK);
  933. else
  934. psc_ops->command(port, MPC52xx_PSC_STOP_BRK);
  935. uart_port_unlock_irqrestore(port, flags);
  936. }
  937. static int
  938. mpc52xx_uart_startup(struct uart_port *port)
  939. {
  940. int ret;
  941. if (psc_ops->clock) {
  942. ret = psc_ops->clock(port, 1);
  943. if (ret)
  944. return ret;
  945. }
  946. /* Request IRQ */
  947. ret = request_irq(port->irq, mpc52xx_uart_int,
  948. port->irqflags, "mpc52xx_psc_uart", port);
  949. if (ret)
  950. return ret;
  951. /* Reset/activate the port, clear and enable interrupts */
  952. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  953. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  954. /*
  955. * According to Freescale's support the RST_TX command can produce a
  956. * spike on the TX pin. So they recommend to delay "for one character".
  957. * One millisecond should be enough for everyone.
  958. */
  959. msleep(1);
  960. psc_ops->set_sicr(port, 0); /* UART mode DCD ignored */
  961. psc_ops->fifo_init(port);
  962. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  963. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  964. return 0;
  965. }
  966. static void
  967. mpc52xx_uart_shutdown(struct uart_port *port)
  968. {
  969. /* Shut down the port. Leave TX active if on a console port */
  970. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  971. if (!uart_console(port))
  972. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  973. port->read_status_mask = 0;
  974. psc_ops->set_imr(port, port->read_status_mask);
  975. if (psc_ops->clock)
  976. psc_ops->clock(port, 0);
  977. /* Disable interrupt */
  978. psc_ops->cw_disable_ints(port);
  979. /* Release interrupt */
  980. free_irq(port->irq, port);
  981. }
  982. static void
  983. mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
  984. const struct ktermios *old)
  985. {
  986. unsigned long flags;
  987. unsigned char mr1, mr2;
  988. unsigned int j;
  989. unsigned int baud;
  990. /* Prepare what we're gonna write */
  991. mr1 = 0;
  992. switch (new->c_cflag & CSIZE) {
  993. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  994. break;
  995. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  996. break;
  997. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  998. break;
  999. case CS8:
  1000. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  1001. }
  1002. if (new->c_cflag & PARENB) {
  1003. if (new->c_cflag & CMSPAR)
  1004. mr1 |= MPC52xx_PSC_MODE_PARFORCE;
  1005. /* With CMSPAR, PARODD also means high parity (same as termios) */
  1006. mr1 |= (new->c_cflag & PARODD) ?
  1007. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  1008. } else {
  1009. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  1010. }
  1011. mr2 = 0;
  1012. if (new->c_cflag & CSTOPB)
  1013. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  1014. else
  1015. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  1016. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  1017. MPC52xx_PSC_MODE_ONE_STOP;
  1018. if (new->c_cflag & CRTSCTS) {
  1019. mr1 |= MPC52xx_PSC_MODE_RXRTS;
  1020. mr2 |= MPC52xx_PSC_MODE_TXCTS;
  1021. }
  1022. /* Get the lock */
  1023. uart_port_lock_irqsave(port, &flags);
  1024. /* Do our best to flush TX & RX, so we don't lose anything */
  1025. /* But we don't wait indefinitely ! */
  1026. j = 5000000; /* Maximum wait */
  1027. /* FIXME Can't receive chars since set_termios might be called at early
  1028. * boot for the console, all stuff is not yet ready to receive at that
  1029. * time and that just makes the kernel oops */
  1030. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  1031. while (!mpc52xx_uart_tx_empty(port) && --j)
  1032. udelay(1);
  1033. if (!j)
  1034. printk(KERN_ERR "mpc52xx_uart.c: "
  1035. "Unable to flush RX & TX fifos in-time in set_termios."
  1036. "Some chars may have been lost.\n");
  1037. /* Reset the TX & RX */
  1038. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  1039. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  1040. /* Send new mode settings */
  1041. psc_ops->set_mode(port, mr1, mr2);
  1042. baud = psc_ops->set_baudrate(port, new, old);
  1043. /* Update the per-port timeout */
  1044. uart_update_timeout(port, new->c_cflag, baud);
  1045. if (UART_ENABLE_MS(port, new->c_cflag))
  1046. mpc52xx_uart_enable_ms(port);
  1047. /* Reenable TX & RX */
  1048. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  1049. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  1050. /* We're all set, release the lock */
  1051. uart_port_unlock_irqrestore(port, flags);
  1052. }
  1053. static const char *
  1054. mpc52xx_uart_type(struct uart_port *port)
  1055. {
  1056. /*
  1057. * We keep using PORT_MPC52xx for historic reasons although it applies
  1058. * for MPC512x, too, but print "MPC5xxx" to not irritate users
  1059. */
  1060. return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
  1061. }
  1062. static void
  1063. mpc52xx_uart_release_port(struct uart_port *port)
  1064. {
  1065. if (psc_ops->clock_relse)
  1066. psc_ops->clock_relse(port);
  1067. /* remapped by us ? */
  1068. if (port->flags & UPF_IOREMAP) {
  1069. iounmap(port->membase);
  1070. port->membase = NULL;
  1071. }
  1072. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1073. }
  1074. static int
  1075. mpc52xx_uart_request_port(struct uart_port *port)
  1076. {
  1077. int err;
  1078. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  1079. port->membase = ioremap(port->mapbase,
  1080. sizeof(struct mpc52xx_psc));
  1081. if (!port->membase)
  1082. return -EINVAL;
  1083. err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
  1084. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  1085. if (err)
  1086. goto out_membase;
  1087. if (psc_ops->clock_alloc) {
  1088. err = psc_ops->clock_alloc(port);
  1089. if (err)
  1090. goto out_mapregion;
  1091. }
  1092. return 0;
  1093. out_mapregion:
  1094. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1095. out_membase:
  1096. if (port->flags & UPF_IOREMAP) {
  1097. iounmap(port->membase);
  1098. port->membase = NULL;
  1099. }
  1100. return err;
  1101. }
  1102. static void
  1103. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  1104. {
  1105. if ((flags & UART_CONFIG_TYPE)
  1106. && (mpc52xx_uart_request_port(port) == 0))
  1107. port->type = PORT_MPC52xx;
  1108. }
  1109. static int
  1110. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1111. {
  1112. if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
  1113. return -EINVAL;
  1114. if ((ser->irq != port->irq) ||
  1115. (ser->io_type != UPIO_MEM) ||
  1116. (ser->baud_base != port->uartclk) ||
  1117. (ser->iomem_base != (void *)port->mapbase) ||
  1118. (ser->hub6 != 0))
  1119. return -EINVAL;
  1120. return 0;
  1121. }
  1122. static const struct uart_ops mpc52xx_uart_ops = {
  1123. .tx_empty = mpc52xx_uart_tx_empty,
  1124. .set_mctrl = mpc52xx_uart_set_mctrl,
  1125. .get_mctrl = mpc52xx_uart_get_mctrl,
  1126. .stop_tx = mpc52xx_uart_stop_tx,
  1127. .start_tx = mpc52xx_uart_start_tx,
  1128. .stop_rx = mpc52xx_uart_stop_rx,
  1129. .enable_ms = mpc52xx_uart_enable_ms,
  1130. .break_ctl = mpc52xx_uart_break_ctl,
  1131. .startup = mpc52xx_uart_startup,
  1132. .shutdown = mpc52xx_uart_shutdown,
  1133. .set_termios = mpc52xx_uart_set_termios,
  1134. .type = mpc52xx_uart_type,
  1135. .release_port = mpc52xx_uart_release_port,
  1136. .request_port = mpc52xx_uart_request_port,
  1137. .config_port = mpc52xx_uart_config_port,
  1138. .verify_port = mpc52xx_uart_verify_port
  1139. };
  1140. /* ======================================================================== */
  1141. /* Interrupt handling */
  1142. /* ======================================================================== */
  1143. static inline bool
  1144. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  1145. {
  1146. struct tty_port *tport = &port->state->port;
  1147. unsigned char ch, flag;
  1148. unsigned short status;
  1149. /* While we can read, do so ! */
  1150. while (psc_ops->raw_rx_rdy(port)) {
  1151. /* Get the char */
  1152. ch = psc_ops->read_char(port);
  1153. /* Handle sysreq char */
  1154. if (uart_handle_sysrq_char(port, ch))
  1155. continue;
  1156. /* Store it */
  1157. flag = TTY_NORMAL;
  1158. port->icount.rx++;
  1159. status = psc_ops->get_status(port);
  1160. if (status & (MPC52xx_PSC_SR_PE |
  1161. MPC52xx_PSC_SR_FE |
  1162. MPC52xx_PSC_SR_RB)) {
  1163. if (status & MPC52xx_PSC_SR_RB) {
  1164. flag = TTY_BREAK;
  1165. uart_handle_break(port);
  1166. port->icount.brk++;
  1167. } else if (status & MPC52xx_PSC_SR_PE) {
  1168. flag = TTY_PARITY;
  1169. port->icount.parity++;
  1170. }
  1171. else if (status & MPC52xx_PSC_SR_FE) {
  1172. flag = TTY_FRAME;
  1173. port->icount.frame++;
  1174. }
  1175. /* Clear error condition */
  1176. psc_ops->command(port, MPC52xx_PSC_RST_ERR_STAT);
  1177. }
  1178. tty_insert_flip_char(tport, ch, flag);
  1179. if (status & MPC52xx_PSC_SR_OE) {
  1180. /*
  1181. * Overrun is special, since it's
  1182. * reported immediately, and doesn't
  1183. * affect the current character
  1184. */
  1185. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  1186. port->icount.overrun++;
  1187. }
  1188. }
  1189. tty_flip_buffer_push(tport);
  1190. return psc_ops->raw_rx_rdy(port);
  1191. }
  1192. static inline bool
  1193. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  1194. {
  1195. u8 ch;
  1196. return uart_port_tx(port, ch,
  1197. psc_ops->raw_tx_rdy(port),
  1198. psc_ops->write_char(port, ch));
  1199. }
  1200. static irqreturn_t
  1201. mpc5xxx_uart_process_int(struct uart_port *port)
  1202. {
  1203. unsigned long pass = ISR_PASS_LIMIT;
  1204. bool keepgoing;
  1205. u8 status;
  1206. /* While we have stuff to do, we continue */
  1207. do {
  1208. /* If we don't find anything to do, we stop */
  1209. keepgoing = false;
  1210. psc_ops->rx_clr_irq(port);
  1211. if (psc_ops->rx_rdy(port))
  1212. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  1213. psc_ops->tx_clr_irq(port);
  1214. if (psc_ops->tx_rdy(port))
  1215. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  1216. status = psc_ops->get_ipcr(port);
  1217. if (status & MPC52xx_PSC_D_DCD)
  1218. uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
  1219. if (status & MPC52xx_PSC_D_CTS)
  1220. uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
  1221. /* Limit number of iteration */
  1222. if (!(--pass))
  1223. keepgoing = false;
  1224. } while (keepgoing);
  1225. return IRQ_HANDLED;
  1226. }
  1227. static irqreturn_t
  1228. mpc52xx_uart_int(int irq, void *dev_id)
  1229. {
  1230. struct uart_port *port = dev_id;
  1231. irqreturn_t ret;
  1232. uart_port_lock(port);
  1233. ret = psc_ops->handle_irq(port);
  1234. uart_port_unlock(port);
  1235. return ret;
  1236. }
  1237. /* ======================================================================== */
  1238. /* Console ( if applicable ) */
  1239. /* ======================================================================== */
  1240. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  1241. static void __init
  1242. mpc52xx_console_get_options(struct uart_port *port,
  1243. int *baud, int *parity, int *bits, int *flow)
  1244. {
  1245. unsigned char mr1;
  1246. pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
  1247. /* Read the mode registers */
  1248. mr1 = psc_ops->get_mr1(port);
  1249. /* CT{U,L}R are write-only ! */
  1250. *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1251. /* Parse them */
  1252. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  1253. case MPC52xx_PSC_MODE_5_BITS:
  1254. *bits = 5;
  1255. break;
  1256. case MPC52xx_PSC_MODE_6_BITS:
  1257. *bits = 6;
  1258. break;
  1259. case MPC52xx_PSC_MODE_7_BITS:
  1260. *bits = 7;
  1261. break;
  1262. case MPC52xx_PSC_MODE_8_BITS:
  1263. default:
  1264. *bits = 8;
  1265. }
  1266. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  1267. *parity = 'n';
  1268. else
  1269. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  1270. }
  1271. static void
  1272. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  1273. {
  1274. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1275. unsigned int i, j;
  1276. /* Disable interrupts */
  1277. psc_ops->cw_disable_ints(port);
  1278. /* Wait the TX buffer to be empty */
  1279. j = 5000000; /* Maximum wait */
  1280. while (!mpc52xx_uart_tx_empty(port) && --j)
  1281. udelay(1);
  1282. /* Write all the chars */
  1283. for (i = 0; i < count; i++, s++) {
  1284. /* Line return handling */
  1285. if (*s == '\n')
  1286. psc_ops->write_char(port, '\r');
  1287. /* Send the char */
  1288. psc_ops->write_char(port, *s);
  1289. /* Wait the TX buffer to be empty */
  1290. j = 20000; /* Maximum wait */
  1291. while (!mpc52xx_uart_tx_empty(port) && --j)
  1292. udelay(1);
  1293. }
  1294. /* Restore interrupt state */
  1295. psc_ops->cw_restore_ints(port);
  1296. }
  1297. static int __init
  1298. mpc52xx_console_setup(struct console *co, char *options)
  1299. {
  1300. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1301. struct device_node *np = mpc52xx_uart_nodes[co->index];
  1302. unsigned int uartclk;
  1303. struct resource res;
  1304. int ret;
  1305. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1306. int bits = 8;
  1307. int parity = 'n';
  1308. int flow = 'n';
  1309. pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
  1310. co, co->index, options);
  1311. if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
  1312. pr_debug("PSC%x out of range\n", co->index);
  1313. return -EINVAL;
  1314. }
  1315. if (!np) {
  1316. pr_debug("PSC%x not found in device tree\n", co->index);
  1317. return -EINVAL;
  1318. }
  1319. pr_debug("Console on ttyPSC%x is %pOF\n",
  1320. co->index, mpc52xx_uart_nodes[co->index]);
  1321. /* Fetch register locations */
  1322. ret = of_address_to_resource(np, 0, &res);
  1323. if (ret) {
  1324. pr_debug("Could not get resources for PSC%x\n", co->index);
  1325. return ret;
  1326. }
  1327. uartclk = mpc5xxx_fwnode_get_bus_frequency(of_fwnode_handle(np));
  1328. if (uartclk == 0) {
  1329. pr_debug("Could not find uart clock frequency!\n");
  1330. return -EINVAL;
  1331. }
  1332. /* Basic port init. Needed since we use some uart_??? func before
  1333. * real init for early access */
  1334. spin_lock_init(&port->lock);
  1335. port->uartclk = uartclk;
  1336. port->ops = &mpc52xx_uart_ops;
  1337. port->mapbase = res.start;
  1338. port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
  1339. port->irq = irq_of_parse_and_map(np, 0);
  1340. if (port->membase == NULL)
  1341. return -EINVAL;
  1342. pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
  1343. (void *)port->mapbase, port->membase,
  1344. port->irq, port->uartclk);
  1345. /* Setup the port parameters according to options */
  1346. if (options)
  1347. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1348. else
  1349. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  1350. pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
  1351. baud, bits, parity, flow);
  1352. return uart_set_options(port, co, baud, parity, bits, flow);
  1353. }
  1354. static struct uart_driver mpc52xx_uart_driver;
  1355. static struct console mpc52xx_console = {
  1356. .name = "ttyPSC",
  1357. .write = mpc52xx_console_write,
  1358. .device = uart_console_device,
  1359. .setup = mpc52xx_console_setup,
  1360. .flags = CON_PRINTBUFFER,
  1361. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
  1362. .data = &mpc52xx_uart_driver,
  1363. };
  1364. static int __init
  1365. mpc52xx_console_init(void)
  1366. {
  1367. mpc52xx_uart_of_enumerate();
  1368. register_console(&mpc52xx_console);
  1369. return 0;
  1370. }
  1371. console_initcall(mpc52xx_console_init);
  1372. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  1373. #else
  1374. #define MPC52xx_PSC_CONSOLE NULL
  1375. #endif
  1376. /* ======================================================================== */
  1377. /* UART Driver */
  1378. /* ======================================================================== */
  1379. static struct uart_driver mpc52xx_uart_driver = {
  1380. .driver_name = "mpc52xx_psc_uart",
  1381. .dev_name = "ttyPSC",
  1382. .major = SERIAL_PSC_MAJOR,
  1383. .minor = SERIAL_PSC_MINOR,
  1384. .nr = MPC52xx_PSC_MAXNUM,
  1385. .cons = MPC52xx_PSC_CONSOLE,
  1386. };
  1387. /* ======================================================================== */
  1388. /* OF Platform Driver */
  1389. /* ======================================================================== */
  1390. static const struct of_device_id mpc52xx_uart_of_match[] = {
  1391. #ifdef CONFIG_PPC_MPC52xx
  1392. { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
  1393. { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1394. /* binding used by old lite5200 device trees: */
  1395. { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1396. /* binding used by efika: */
  1397. { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
  1398. #endif
  1399. #ifdef CONFIG_PPC_MPC512x
  1400. { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
  1401. { .compatible = "fsl,mpc5125-psc-uart", .data = &mpc5125_psc_ops, },
  1402. #endif
  1403. {},
  1404. };
  1405. static int mpc52xx_uart_of_probe(struct platform_device *op)
  1406. {
  1407. int idx = -1;
  1408. unsigned int uartclk;
  1409. struct uart_port *port = NULL;
  1410. struct resource res;
  1411. int ret;
  1412. /* Check validity & presence */
  1413. for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
  1414. if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
  1415. break;
  1416. if (idx >= MPC52xx_PSC_MAXNUM)
  1417. return -EINVAL;
  1418. pr_debug("Found %pOF assigned to ttyPSC%x\n",
  1419. mpc52xx_uart_nodes[idx], idx);
  1420. /* set the uart clock to the input clock of the psc, the different
  1421. * prescalers are taken into account in the set_baudrate() methods
  1422. * of the respective chip */
  1423. uartclk = mpc5xxx_get_bus_frequency(&op->dev);
  1424. if (uartclk == 0) {
  1425. dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
  1426. return -EINVAL;
  1427. }
  1428. /* Init the port structure */
  1429. port = &mpc52xx_uart_ports[idx];
  1430. spin_lock_init(&port->lock);
  1431. port->uartclk = uartclk;
  1432. port->fifosize = 512;
  1433. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MPC52xx_CONSOLE);
  1434. port->iotype = UPIO_MEM;
  1435. port->flags = UPF_BOOT_AUTOCONF |
  1436. (uart_console(port) ? 0 : UPF_IOREMAP);
  1437. port->line = idx;
  1438. port->ops = &mpc52xx_uart_ops;
  1439. port->dev = &op->dev;
  1440. /* Search for IRQ and mapbase */
  1441. ret = of_address_to_resource(op->dev.of_node, 0, &res);
  1442. if (ret)
  1443. return ret;
  1444. port->mapbase = res.start;
  1445. if (!port->mapbase) {
  1446. dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
  1447. return -EINVAL;
  1448. }
  1449. psc_ops->get_irq(port, op->dev.of_node);
  1450. if (port->irq == 0) {
  1451. dev_dbg(&op->dev, "Could not get irq\n");
  1452. return -EINVAL;
  1453. }
  1454. dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
  1455. (void *)port->mapbase, port->irq, port->uartclk);
  1456. /* Add the port to the uart sub-system */
  1457. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  1458. if (ret)
  1459. return ret;
  1460. platform_set_drvdata(op, (void *)port);
  1461. return 0;
  1462. }
  1463. static void mpc52xx_uart_of_remove(struct platform_device *op)
  1464. {
  1465. struct uart_port *port = platform_get_drvdata(op);
  1466. if (port)
  1467. uart_remove_one_port(&mpc52xx_uart_driver, port);
  1468. }
  1469. #ifdef CONFIG_PM
  1470. static int
  1471. mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
  1472. {
  1473. struct uart_port *port = platform_get_drvdata(op);
  1474. if (port)
  1475. uart_suspend_port(&mpc52xx_uart_driver, port);
  1476. return 0;
  1477. }
  1478. static int
  1479. mpc52xx_uart_of_resume(struct platform_device *op)
  1480. {
  1481. struct uart_port *port = platform_get_drvdata(op);
  1482. if (port)
  1483. uart_resume_port(&mpc52xx_uart_driver, port);
  1484. return 0;
  1485. }
  1486. #endif
  1487. static void
  1488. mpc52xx_uart_of_assign(struct device_node *np)
  1489. {
  1490. int i;
  1491. /* Find the first free PSC number */
  1492. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1493. if (mpc52xx_uart_nodes[i] == NULL) {
  1494. of_node_get(np);
  1495. mpc52xx_uart_nodes[i] = np;
  1496. return;
  1497. }
  1498. }
  1499. }
  1500. static void
  1501. mpc52xx_uart_of_enumerate(void)
  1502. {
  1503. static int enum_done;
  1504. struct device_node *np;
  1505. const struct of_device_id *match;
  1506. int i;
  1507. if (enum_done)
  1508. return;
  1509. /* Assign index to each PSC in device tree */
  1510. for_each_matching_node(np, mpc52xx_uart_of_match) {
  1511. match = of_match_node(mpc52xx_uart_of_match, np);
  1512. psc_ops = match->data;
  1513. mpc52xx_uart_of_assign(np);
  1514. }
  1515. enum_done = 1;
  1516. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1517. if (mpc52xx_uart_nodes[i])
  1518. pr_debug("%pOF assigned to ttyPSC%x\n",
  1519. mpc52xx_uart_nodes[i], i);
  1520. }
  1521. }
  1522. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  1523. static struct platform_driver mpc52xx_uart_of_driver = {
  1524. .probe = mpc52xx_uart_of_probe,
  1525. .remove = mpc52xx_uart_of_remove,
  1526. #ifdef CONFIG_PM
  1527. .suspend = mpc52xx_uart_of_suspend,
  1528. .resume = mpc52xx_uart_of_resume,
  1529. #endif
  1530. .driver = {
  1531. .name = "mpc52xx-psc-uart",
  1532. .of_match_table = mpc52xx_uart_of_match,
  1533. },
  1534. };
  1535. /* ======================================================================== */
  1536. /* Module */
  1537. /* ======================================================================== */
  1538. static int __init
  1539. mpc52xx_uart_init(void)
  1540. {
  1541. int ret;
  1542. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  1543. ret = uart_register_driver(&mpc52xx_uart_driver);
  1544. if (ret) {
  1545. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  1546. __FILE__, ret);
  1547. return ret;
  1548. }
  1549. mpc52xx_uart_of_enumerate();
  1550. /*
  1551. * Map the PSC FIFO Controller and init if on MPC512x.
  1552. */
  1553. if (psc_ops && psc_ops->fifoc_init) {
  1554. ret = psc_ops->fifoc_init();
  1555. if (ret)
  1556. goto err_init;
  1557. }
  1558. ret = platform_driver_register(&mpc52xx_uart_of_driver);
  1559. if (ret) {
  1560. printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
  1561. __FILE__, ret);
  1562. goto err_reg;
  1563. }
  1564. return 0;
  1565. err_reg:
  1566. if (psc_ops && psc_ops->fifoc_uninit)
  1567. psc_ops->fifoc_uninit();
  1568. err_init:
  1569. uart_unregister_driver(&mpc52xx_uart_driver);
  1570. return ret;
  1571. }
  1572. static void __exit
  1573. mpc52xx_uart_exit(void)
  1574. {
  1575. if (psc_ops->fifoc_uninit)
  1576. psc_ops->fifoc_uninit();
  1577. platform_driver_unregister(&mpc52xx_uart_of_driver);
  1578. uart_unregister_driver(&mpc52xx_uart_driver);
  1579. }
  1580. module_init(mpc52xx_uart_init);
  1581. module_exit(mpc52xx_uart_exit);
  1582. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  1583. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  1584. MODULE_LICENSE("GPL");