mpc52xx_uart.c 49 KB

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