mpc52xx_uart.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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. u16 sts = in_be16(&PSC(port)->mpc52xx_psc_status);
  203. return (sts & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  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_PPC_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 struct clk *psc_fifoc_clk;
  362. static void mpc512x_psc_fifo_init(struct uart_port *port)
  363. {
  364. /* /32 prescaler */
  365. out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
  366. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  367. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  368. out_be32(&FIFO_512x(port)->txalarm, 1);
  369. out_be32(&FIFO_512x(port)->tximr, 0);
  370. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  371. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  372. out_be32(&FIFO_512x(port)->rxalarm, 1);
  373. out_be32(&FIFO_512x(port)->rximr, 0);
  374. out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  375. out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  376. }
  377. static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
  378. {
  379. return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  380. }
  381. static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
  382. {
  383. return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  384. }
  385. static int mpc512x_psc_rx_rdy(struct uart_port *port)
  386. {
  387. return in_be32(&FIFO_512x(port)->rxsr)
  388. & in_be32(&FIFO_512x(port)->rximr)
  389. & MPC512x_PSC_FIFO_ALARM;
  390. }
  391. static int mpc512x_psc_tx_rdy(struct uart_port *port)
  392. {
  393. return in_be32(&FIFO_512x(port)->txsr)
  394. & in_be32(&FIFO_512x(port)->tximr)
  395. & MPC512x_PSC_FIFO_ALARM;
  396. }
  397. static int mpc512x_psc_tx_empty(struct uart_port *port)
  398. {
  399. return in_be32(&FIFO_512x(port)->txsr)
  400. & MPC512x_PSC_FIFO_EMPTY;
  401. }
  402. static void mpc512x_psc_stop_rx(struct uart_port *port)
  403. {
  404. unsigned long rx_fifo_imr;
  405. rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
  406. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  407. out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
  408. }
  409. static void mpc512x_psc_start_tx(struct uart_port *port)
  410. {
  411. unsigned long tx_fifo_imr;
  412. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  413. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  414. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  415. }
  416. static void mpc512x_psc_stop_tx(struct uart_port *port)
  417. {
  418. unsigned long tx_fifo_imr;
  419. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  420. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  421. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  422. }
  423. static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
  424. {
  425. out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
  426. }
  427. static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
  428. {
  429. out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
  430. }
  431. static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
  432. {
  433. out_8(&FIFO_512x(port)->txdata_8, c);
  434. }
  435. static unsigned char mpc512x_psc_read_char(struct uart_port *port)
  436. {
  437. return in_8(&FIFO_512x(port)->rxdata_8);
  438. }
  439. static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
  440. {
  441. port->read_status_mask =
  442. in_be32(&FIFO_512x(port)->tximr) << 16 |
  443. in_be32(&FIFO_512x(port)->rximr);
  444. out_be32(&FIFO_512x(port)->tximr, 0);
  445. out_be32(&FIFO_512x(port)->rximr, 0);
  446. }
  447. static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
  448. {
  449. out_be32(&FIFO_512x(port)->tximr,
  450. (port->read_status_mask >> 16) & 0x7f);
  451. out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
  452. }
  453. static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
  454. struct ktermios *new,
  455. struct ktermios *old)
  456. {
  457. unsigned int baud;
  458. unsigned int divisor;
  459. /*
  460. * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
  461. * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
  462. * Furthermore, it states that "After reset, the prescaler by 10
  463. * for the UART mode is selected", but the reset register value is
  464. * 0x0000 which means a /32 prescaler. This is wrong.
  465. *
  466. * In reality using /32 prescaler doesn't work, as it is not supported!
  467. * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
  468. * Chapter 4.1 PSC in UART Mode.
  469. * Calculate with a /16 prescaler here.
  470. */
  471. /* uartclk contains the ips freq */
  472. baud = uart_get_baud_rate(port, new, old,
  473. port->uartclk / (16 * 0xffff) + 1,
  474. port->uartclk / 16);
  475. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  476. /* enable the /16 prescaler and set the divisor */
  477. mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
  478. return baud;
  479. }
  480. /* Init PSC FIFO Controller */
  481. static int __init mpc512x_psc_fifoc_init(void)
  482. {
  483. int err;
  484. struct device_node *np;
  485. struct clk *clk;
  486. /* default error code, potentially overwritten by clock calls */
  487. err = -ENODEV;
  488. np = of_find_compatible_node(NULL, NULL,
  489. "fsl,mpc5121-psc-fifo");
  490. if (!np) {
  491. pr_err("%s: Can't find FIFOC node\n", __func__);
  492. goto out_err;
  493. }
  494. clk = of_clk_get(np, 0);
  495. if (IS_ERR(clk)) {
  496. /* backwards compat with device trees that lack clock specs */
  497. clk = clk_get_sys(np->name, "ipg");
  498. }
  499. if (IS_ERR(clk)) {
  500. pr_err("%s: Can't lookup FIFO clock\n", __func__);
  501. err = PTR_ERR(clk);
  502. goto out_ofnode_put;
  503. }
  504. if (clk_prepare_enable(clk)) {
  505. pr_err("%s: Can't enable FIFO clock\n", __func__);
  506. clk_put(clk);
  507. goto out_ofnode_put;
  508. }
  509. psc_fifoc_clk = clk;
  510. psc_fifoc = of_iomap(np, 0);
  511. if (!psc_fifoc) {
  512. pr_err("%s: Can't map FIFOC\n", __func__);
  513. goto out_clk_disable;
  514. }
  515. psc_fifoc_irq = irq_of_parse_and_map(np, 0);
  516. if (psc_fifoc_irq == 0) {
  517. pr_err("%s: Can't get FIFOC irq\n", __func__);
  518. goto out_unmap;
  519. }
  520. of_node_put(np);
  521. return 0;
  522. out_unmap:
  523. iounmap(psc_fifoc);
  524. out_clk_disable:
  525. clk_disable_unprepare(psc_fifoc_clk);
  526. clk_put(psc_fifoc_clk);
  527. out_ofnode_put:
  528. of_node_put(np);
  529. out_err:
  530. return err;
  531. }
  532. static void __exit mpc512x_psc_fifoc_uninit(void)
  533. {
  534. iounmap(psc_fifoc);
  535. /* disable the clock, errors are not fatal */
  536. if (psc_fifoc_clk) {
  537. clk_disable_unprepare(psc_fifoc_clk);
  538. clk_put(psc_fifoc_clk);
  539. psc_fifoc_clk = NULL;
  540. }
  541. }
  542. /* 512x specific interrupt handler. The caller holds the port lock */
  543. static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
  544. {
  545. unsigned long fifoc_int;
  546. int psc_num;
  547. /* Read pending PSC FIFOC interrupts */
  548. fifoc_int = in_be32(&psc_fifoc->fifoc_int);
  549. /* Check if it is an interrupt for this port */
  550. psc_num = (port->mapbase & 0xf00) >> 8;
  551. if (test_bit(psc_num, &fifoc_int) ||
  552. test_bit(psc_num + 16, &fifoc_int))
  553. return mpc5xxx_uart_process_int(port);
  554. return IRQ_NONE;
  555. }
  556. static struct clk *psc_mclk_clk[MPC52xx_PSC_MAXNUM];
  557. static struct clk *psc_ipg_clk[MPC52xx_PSC_MAXNUM];
  558. /* called from within the .request_port() callback (allocation) */
  559. static int mpc512x_psc_alloc_clock(struct uart_port *port)
  560. {
  561. int psc_num;
  562. struct clk *clk;
  563. int err;
  564. psc_num = (port->mapbase & 0xf00) >> 8;
  565. clk = devm_clk_get(port->dev, "mclk");
  566. if (IS_ERR(clk)) {
  567. dev_err(port->dev, "Failed to get MCLK!\n");
  568. err = PTR_ERR(clk);
  569. goto out_err;
  570. }
  571. err = clk_prepare_enable(clk);
  572. if (err) {
  573. dev_err(port->dev, "Failed to enable MCLK!\n");
  574. goto out_err;
  575. }
  576. psc_mclk_clk[psc_num] = clk;
  577. clk = devm_clk_get(port->dev, "ipg");
  578. if (IS_ERR(clk)) {
  579. dev_err(port->dev, "Failed to get IPG clock!\n");
  580. err = PTR_ERR(clk);
  581. goto out_err;
  582. }
  583. err = clk_prepare_enable(clk);
  584. if (err) {
  585. dev_err(port->dev, "Failed to enable IPG clock!\n");
  586. goto out_err;
  587. }
  588. psc_ipg_clk[psc_num] = clk;
  589. return 0;
  590. out_err:
  591. if (psc_mclk_clk[psc_num]) {
  592. clk_disable_unprepare(psc_mclk_clk[psc_num]);
  593. psc_mclk_clk[psc_num] = NULL;
  594. }
  595. if (psc_ipg_clk[psc_num]) {
  596. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  597. psc_ipg_clk[psc_num] = NULL;
  598. }
  599. return err;
  600. }
  601. /* called from within the .release_port() callback (release) */
  602. static void mpc512x_psc_relse_clock(struct uart_port *port)
  603. {
  604. int psc_num;
  605. struct clk *clk;
  606. psc_num = (port->mapbase & 0xf00) >> 8;
  607. clk = psc_mclk_clk[psc_num];
  608. if (clk) {
  609. clk_disable_unprepare(clk);
  610. psc_mclk_clk[psc_num] = NULL;
  611. }
  612. if (psc_ipg_clk[psc_num]) {
  613. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  614. psc_ipg_clk[psc_num] = NULL;
  615. }
  616. }
  617. /* implementation of the .clock() callback (enable/disable) */
  618. static int mpc512x_psc_endis_clock(struct uart_port *port, int enable)
  619. {
  620. int psc_num;
  621. struct clk *psc_clk;
  622. int ret;
  623. if (uart_console(port))
  624. return 0;
  625. psc_num = (port->mapbase & 0xf00) >> 8;
  626. psc_clk = psc_mclk_clk[psc_num];
  627. if (!psc_clk) {
  628. dev_err(port->dev, "Failed to get PSC clock entry!\n");
  629. return -ENODEV;
  630. }
  631. dev_dbg(port->dev, "mclk %sable\n", enable ? "en" : "dis");
  632. if (enable) {
  633. ret = clk_enable(psc_clk);
  634. if (ret)
  635. dev_err(port->dev, "Failed to enable MCLK!\n");
  636. return ret;
  637. } else {
  638. clk_disable(psc_clk);
  639. return 0;
  640. }
  641. }
  642. static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
  643. {
  644. port->irqflags = IRQF_SHARED;
  645. port->irq = psc_fifoc_irq;
  646. }
  647. #endif
  648. #ifdef CONFIG_PPC_MPC512x
  649. #define PSC_5125(port) ((struct mpc5125_psc __iomem *)((port)->membase))
  650. #define FIFO_5125(port) ((struct mpc512x_psc_fifo __iomem *)(PSC_5125(port)+1))
  651. static void mpc5125_psc_fifo_init(struct uart_port *port)
  652. {
  653. /* /32 prescaler */
  654. out_8(&PSC_5125(port)->mpc52xx_psc_clock_select, 0xdd);
  655. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  656. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  657. out_be32(&FIFO_5125(port)->txalarm, 1);
  658. out_be32(&FIFO_5125(port)->tximr, 0);
  659. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  660. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  661. out_be32(&FIFO_5125(port)->rxalarm, 1);
  662. out_be32(&FIFO_5125(port)->rximr, 0);
  663. out_be32(&FIFO_5125(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  664. out_be32(&FIFO_5125(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  665. }
  666. static int mpc5125_psc_raw_rx_rdy(struct uart_port *port)
  667. {
  668. return !(in_be32(&FIFO_5125(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  669. }
  670. static int mpc5125_psc_raw_tx_rdy(struct uart_port *port)
  671. {
  672. return !(in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  673. }
  674. static int mpc5125_psc_rx_rdy(struct uart_port *port)
  675. {
  676. return in_be32(&FIFO_5125(port)->rxsr) &
  677. in_be32(&FIFO_5125(port)->rximr) & MPC512x_PSC_FIFO_ALARM;
  678. }
  679. static int mpc5125_psc_tx_rdy(struct uart_port *port)
  680. {
  681. return in_be32(&FIFO_5125(port)->txsr) &
  682. in_be32(&FIFO_5125(port)->tximr) & MPC512x_PSC_FIFO_ALARM;
  683. }
  684. static int mpc5125_psc_tx_empty(struct uart_port *port)
  685. {
  686. return in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_EMPTY;
  687. }
  688. static void mpc5125_psc_stop_rx(struct uart_port *port)
  689. {
  690. unsigned long rx_fifo_imr;
  691. rx_fifo_imr = in_be32(&FIFO_5125(port)->rximr);
  692. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  693. out_be32(&FIFO_5125(port)->rximr, rx_fifo_imr);
  694. }
  695. static void mpc5125_psc_start_tx(struct uart_port *port)
  696. {
  697. unsigned long tx_fifo_imr;
  698. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  699. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  700. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  701. }
  702. static void mpc5125_psc_stop_tx(struct uart_port *port)
  703. {
  704. unsigned long tx_fifo_imr;
  705. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  706. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  707. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  708. }
  709. static void mpc5125_psc_rx_clr_irq(struct uart_port *port)
  710. {
  711. out_be32(&FIFO_5125(port)->rxisr, in_be32(&FIFO_5125(port)->rxisr));
  712. }
  713. static void mpc5125_psc_tx_clr_irq(struct uart_port *port)
  714. {
  715. out_be32(&FIFO_5125(port)->txisr, in_be32(&FIFO_5125(port)->txisr));
  716. }
  717. static void mpc5125_psc_write_char(struct uart_port *port, unsigned char c)
  718. {
  719. out_8(&FIFO_5125(port)->txdata_8, c);
  720. }
  721. static unsigned char mpc5125_psc_read_char(struct uart_port *port)
  722. {
  723. return in_8(&FIFO_5125(port)->rxdata_8);
  724. }
  725. static void mpc5125_psc_cw_disable_ints(struct uart_port *port)
  726. {
  727. port->read_status_mask =
  728. in_be32(&FIFO_5125(port)->tximr) << 16 |
  729. in_be32(&FIFO_5125(port)->rximr);
  730. out_be32(&FIFO_5125(port)->tximr, 0);
  731. out_be32(&FIFO_5125(port)->rximr, 0);
  732. }
  733. static void mpc5125_psc_cw_restore_ints(struct uart_port *port)
  734. {
  735. out_be32(&FIFO_5125(port)->tximr,
  736. (port->read_status_mask >> 16) & 0x7f);
  737. out_be32(&FIFO_5125(port)->rximr, port->read_status_mask & 0x7f);
  738. }
  739. static inline void mpc5125_set_divisor(struct mpc5125_psc __iomem *psc,
  740. u8 prescaler, unsigned int divisor)
  741. {
  742. /* select prescaler */
  743. out_8(&psc->mpc52xx_psc_clock_select, prescaler);
  744. out_8(&psc->ctur, divisor >> 8);
  745. out_8(&psc->ctlr, divisor & 0xff);
  746. }
  747. static unsigned int mpc5125_psc_set_baudrate(struct uart_port *port,
  748. struct ktermios *new,
  749. struct ktermios *old)
  750. {
  751. unsigned int baud;
  752. unsigned int divisor;
  753. /*
  754. * Calculate with a /16 prescaler here.
  755. */
  756. /* uartclk contains the ips freq */
  757. baud = uart_get_baud_rate(port, new, old,
  758. port->uartclk / (16 * 0xffff) + 1,
  759. port->uartclk / 16);
  760. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  761. /* enable the /16 prescaler and set the divisor */
  762. mpc5125_set_divisor(PSC_5125(port), 0xdd, divisor);
  763. return baud;
  764. }
  765. /*
  766. * MPC5125 have compatible PSC FIFO Controller.
  767. * Special init not needed.
  768. */
  769. static u16 mpc5125_psc_get_status(struct uart_port *port)
  770. {
  771. return in_be16(&PSC_5125(port)->mpc52xx_psc_status);
  772. }
  773. static u8 mpc5125_psc_get_ipcr(struct uart_port *port)
  774. {
  775. return in_8(&PSC_5125(port)->mpc52xx_psc_ipcr);
  776. }
  777. static void mpc5125_psc_command(struct uart_port *port, u8 cmd)
  778. {
  779. out_8(&PSC_5125(port)->command, cmd);
  780. }
  781. static void mpc5125_psc_set_mode(struct uart_port *port, u8 mr1, u8 mr2)
  782. {
  783. out_8(&PSC_5125(port)->mr1, mr1);
  784. out_8(&PSC_5125(port)->mr2, mr2);
  785. }
  786. static void mpc5125_psc_set_rts(struct uart_port *port, int state)
  787. {
  788. if (state & TIOCM_RTS)
  789. out_8(&PSC_5125(port)->op1, MPC52xx_PSC_OP_RTS);
  790. else
  791. out_8(&PSC_5125(port)->op0, MPC52xx_PSC_OP_RTS);
  792. }
  793. static void mpc5125_psc_enable_ms(struct uart_port *port)
  794. {
  795. struct mpc5125_psc __iomem *psc = PSC_5125(port);
  796. /* clear D_*-bits by reading them */
  797. in_8(&psc->mpc52xx_psc_ipcr);
  798. /* enable CTS and DCD as IPC interrupts */
  799. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  800. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  801. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  802. }
  803. static void mpc5125_psc_set_sicr(struct uart_port *port, u32 val)
  804. {
  805. out_be32(&PSC_5125(port)->sicr, val);
  806. }
  807. static void mpc5125_psc_set_imr(struct uart_port *port, u16 val)
  808. {
  809. out_be16(&PSC_5125(port)->mpc52xx_psc_imr, val);
  810. }
  811. static u8 mpc5125_psc_get_mr1(struct uart_port *port)
  812. {
  813. return in_8(&PSC_5125(port)->mr1);
  814. }
  815. static struct psc_ops mpc5125_psc_ops = {
  816. .fifo_init = mpc5125_psc_fifo_init,
  817. .raw_rx_rdy = mpc5125_psc_raw_rx_rdy,
  818. .raw_tx_rdy = mpc5125_psc_raw_tx_rdy,
  819. .rx_rdy = mpc5125_psc_rx_rdy,
  820. .tx_rdy = mpc5125_psc_tx_rdy,
  821. .tx_empty = mpc5125_psc_tx_empty,
  822. .stop_rx = mpc5125_psc_stop_rx,
  823. .start_tx = mpc5125_psc_start_tx,
  824. .stop_tx = mpc5125_psc_stop_tx,
  825. .rx_clr_irq = mpc5125_psc_rx_clr_irq,
  826. .tx_clr_irq = mpc5125_psc_tx_clr_irq,
  827. .write_char = mpc5125_psc_write_char,
  828. .read_char = mpc5125_psc_read_char,
  829. .cw_disable_ints = mpc5125_psc_cw_disable_ints,
  830. .cw_restore_ints = mpc5125_psc_cw_restore_ints,
  831. .set_baudrate = mpc5125_psc_set_baudrate,
  832. .clock_alloc = mpc512x_psc_alloc_clock,
  833. .clock_relse = mpc512x_psc_relse_clock,
  834. .clock = mpc512x_psc_endis_clock,
  835. .fifoc_init = mpc512x_psc_fifoc_init,
  836. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  837. .get_irq = mpc512x_psc_get_irq,
  838. .handle_irq = mpc512x_psc_handle_irq,
  839. .get_status = mpc5125_psc_get_status,
  840. .get_ipcr = mpc5125_psc_get_ipcr,
  841. .command = mpc5125_psc_command,
  842. .set_mode = mpc5125_psc_set_mode,
  843. .set_rts = mpc5125_psc_set_rts,
  844. .enable_ms = mpc5125_psc_enable_ms,
  845. .set_sicr = mpc5125_psc_set_sicr,
  846. .set_imr = mpc5125_psc_set_imr,
  847. .get_mr1 = mpc5125_psc_get_mr1,
  848. };
  849. static struct psc_ops mpc512x_psc_ops = {
  850. .fifo_init = mpc512x_psc_fifo_init,
  851. .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
  852. .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
  853. .rx_rdy = mpc512x_psc_rx_rdy,
  854. .tx_rdy = mpc512x_psc_tx_rdy,
  855. .tx_empty = mpc512x_psc_tx_empty,
  856. .stop_rx = mpc512x_psc_stop_rx,
  857. .start_tx = mpc512x_psc_start_tx,
  858. .stop_tx = mpc512x_psc_stop_tx,
  859. .rx_clr_irq = mpc512x_psc_rx_clr_irq,
  860. .tx_clr_irq = mpc512x_psc_tx_clr_irq,
  861. .write_char = mpc512x_psc_write_char,
  862. .read_char = mpc512x_psc_read_char,
  863. .cw_disable_ints = mpc512x_psc_cw_disable_ints,
  864. .cw_restore_ints = mpc512x_psc_cw_restore_ints,
  865. .set_baudrate = mpc512x_psc_set_baudrate,
  866. .clock_alloc = mpc512x_psc_alloc_clock,
  867. .clock_relse = mpc512x_psc_relse_clock,
  868. .clock = mpc512x_psc_endis_clock,
  869. .fifoc_init = mpc512x_psc_fifoc_init,
  870. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  871. .get_irq = mpc512x_psc_get_irq,
  872. .handle_irq = mpc512x_psc_handle_irq,
  873. .get_status = mpc52xx_psc_get_status,
  874. .get_ipcr = mpc52xx_psc_get_ipcr,
  875. .command = mpc52xx_psc_command,
  876. .set_mode = mpc52xx_psc_set_mode,
  877. .set_rts = mpc52xx_psc_set_rts,
  878. .enable_ms = mpc52xx_psc_enable_ms,
  879. .set_sicr = mpc52xx_psc_set_sicr,
  880. .set_imr = mpc52xx_psc_set_imr,
  881. .get_mr1 = mpc52xx_psc_get_mr1,
  882. };
  883. #endif /* CONFIG_PPC_MPC512x */
  884. static const struct psc_ops *psc_ops;
  885. /* ======================================================================== */
  886. /* UART operations */
  887. /* ======================================================================== */
  888. static unsigned int
  889. mpc52xx_uart_tx_empty(struct uart_port *port)
  890. {
  891. return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
  892. }
  893. static void
  894. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  895. {
  896. psc_ops->set_rts(port, mctrl & TIOCM_RTS);
  897. }
  898. static unsigned int
  899. mpc52xx_uart_get_mctrl(struct uart_port *port)
  900. {
  901. unsigned int ret = TIOCM_DSR;
  902. u8 status = psc_ops->get_ipcr(port);
  903. if (!(status & MPC52xx_PSC_CTS))
  904. ret |= TIOCM_CTS;
  905. if (!(status & MPC52xx_PSC_DCD))
  906. ret |= TIOCM_CAR;
  907. return ret;
  908. }
  909. static void
  910. mpc52xx_uart_stop_tx(struct uart_port *port)
  911. {
  912. /* port->lock taken by caller */
  913. psc_ops->stop_tx(port);
  914. }
  915. static void
  916. mpc52xx_uart_start_tx(struct uart_port *port)
  917. {
  918. /* port->lock taken by caller */
  919. psc_ops->start_tx(port);
  920. }
  921. static void
  922. mpc52xx_uart_stop_rx(struct uart_port *port)
  923. {
  924. /* port->lock taken by caller */
  925. psc_ops->stop_rx(port);
  926. }
  927. static void
  928. mpc52xx_uart_enable_ms(struct uart_port *port)
  929. {
  930. psc_ops->enable_ms(port);
  931. }
  932. static void
  933. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  934. {
  935. unsigned long flags;
  936. spin_lock_irqsave(&port->lock, flags);
  937. if (ctl == -1)
  938. psc_ops->command(port, MPC52xx_PSC_START_BRK);
  939. else
  940. psc_ops->command(port, MPC52xx_PSC_STOP_BRK);
  941. spin_unlock_irqrestore(&port->lock, flags);
  942. }
  943. static int
  944. mpc52xx_uart_startup(struct uart_port *port)
  945. {
  946. int ret;
  947. if (psc_ops->clock) {
  948. ret = psc_ops->clock(port, 1);
  949. if (ret)
  950. return ret;
  951. }
  952. /* Request IRQ */
  953. ret = request_irq(port->irq, mpc52xx_uart_int,
  954. port->irqflags, "mpc52xx_psc_uart", port);
  955. if (ret)
  956. return ret;
  957. /* Reset/activate the port, clear and enable interrupts */
  958. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  959. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  960. /*
  961. * According to Freescale's support the RST_TX command can produce a
  962. * spike on the TX pin. So they recommend to delay "for one character".
  963. * One millisecond should be enough for everyone.
  964. */
  965. msleep(1);
  966. psc_ops->set_sicr(port, 0); /* UART mode DCD ignored */
  967. psc_ops->fifo_init(port);
  968. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  969. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  970. return 0;
  971. }
  972. static void
  973. mpc52xx_uart_shutdown(struct uart_port *port)
  974. {
  975. /* Shut down the port. Leave TX active if on a console port */
  976. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  977. if (!uart_console(port))
  978. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  979. port->read_status_mask = 0;
  980. psc_ops->set_imr(port, port->read_status_mask);
  981. if (psc_ops->clock)
  982. psc_ops->clock(port, 0);
  983. /* Disable interrupt */
  984. psc_ops->cw_disable_ints(port);
  985. /* Release interrupt */
  986. free_irq(port->irq, port);
  987. }
  988. static void
  989. mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
  990. struct ktermios *old)
  991. {
  992. unsigned long flags;
  993. unsigned char mr1, mr2;
  994. unsigned int j;
  995. unsigned int baud;
  996. /* Prepare what we're gonna write */
  997. mr1 = 0;
  998. switch (new->c_cflag & CSIZE) {
  999. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  1000. break;
  1001. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  1002. break;
  1003. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  1004. break;
  1005. case CS8:
  1006. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  1007. }
  1008. if (new->c_cflag & PARENB) {
  1009. if (new->c_cflag & CMSPAR)
  1010. mr1 |= MPC52xx_PSC_MODE_PARFORCE;
  1011. /* With CMSPAR, PARODD also means high parity (same as termios) */
  1012. mr1 |= (new->c_cflag & PARODD) ?
  1013. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  1014. } else {
  1015. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  1016. }
  1017. mr2 = 0;
  1018. if (new->c_cflag & CSTOPB)
  1019. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  1020. else
  1021. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  1022. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  1023. MPC52xx_PSC_MODE_ONE_STOP;
  1024. if (new->c_cflag & CRTSCTS) {
  1025. mr1 |= MPC52xx_PSC_MODE_RXRTS;
  1026. mr2 |= MPC52xx_PSC_MODE_TXCTS;
  1027. }
  1028. /* Get the lock */
  1029. spin_lock_irqsave(&port->lock, flags);
  1030. /* Do our best to flush TX & RX, so we don't lose anything */
  1031. /* But we don't wait indefinitely ! */
  1032. j = 5000000; /* Maximum wait */
  1033. /* FIXME Can't receive chars since set_termios might be called at early
  1034. * boot for the console, all stuff is not yet ready to receive at that
  1035. * time and that just makes the kernel oops */
  1036. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  1037. while (!mpc52xx_uart_tx_empty(port) && --j)
  1038. udelay(1);
  1039. if (!j)
  1040. printk(KERN_ERR "mpc52xx_uart.c: "
  1041. "Unable to flush RX & TX fifos in-time in set_termios."
  1042. "Some chars may have been lost.\n");
  1043. /* Reset the TX & RX */
  1044. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  1045. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  1046. /* Send new mode settings */
  1047. psc_ops->set_mode(port, mr1, mr2);
  1048. baud = psc_ops->set_baudrate(port, new, old);
  1049. /* Update the per-port timeout */
  1050. uart_update_timeout(port, new->c_cflag, baud);
  1051. if (UART_ENABLE_MS(port, new->c_cflag))
  1052. mpc52xx_uart_enable_ms(port);
  1053. /* Reenable TX & RX */
  1054. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  1055. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  1056. /* We're all set, release the lock */
  1057. spin_unlock_irqrestore(&port->lock, flags);
  1058. }
  1059. static const char *
  1060. mpc52xx_uart_type(struct uart_port *port)
  1061. {
  1062. /*
  1063. * We keep using PORT_MPC52xx for historic reasons although it applies
  1064. * for MPC512x, too, but print "MPC5xxx" to not irritate users
  1065. */
  1066. return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
  1067. }
  1068. static void
  1069. mpc52xx_uart_release_port(struct uart_port *port)
  1070. {
  1071. if (psc_ops->clock_relse)
  1072. psc_ops->clock_relse(port);
  1073. /* remapped by us ? */
  1074. if (port->flags & UPF_IOREMAP) {
  1075. iounmap(port->membase);
  1076. port->membase = NULL;
  1077. }
  1078. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1079. }
  1080. static int
  1081. mpc52xx_uart_request_port(struct uart_port *port)
  1082. {
  1083. int err;
  1084. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  1085. port->membase = ioremap(port->mapbase,
  1086. sizeof(struct mpc52xx_psc));
  1087. if (!port->membase)
  1088. return -EINVAL;
  1089. err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
  1090. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  1091. if (err)
  1092. goto out_membase;
  1093. if (psc_ops->clock_alloc) {
  1094. err = psc_ops->clock_alloc(port);
  1095. if (err)
  1096. goto out_mapregion;
  1097. }
  1098. return 0;
  1099. out_mapregion:
  1100. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1101. out_membase:
  1102. if (port->flags & UPF_IOREMAP) {
  1103. iounmap(port->membase);
  1104. port->membase = NULL;
  1105. }
  1106. return err;
  1107. }
  1108. static void
  1109. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  1110. {
  1111. if ((flags & UART_CONFIG_TYPE)
  1112. && (mpc52xx_uart_request_port(port) == 0))
  1113. port->type = PORT_MPC52xx;
  1114. }
  1115. static int
  1116. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1117. {
  1118. if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
  1119. return -EINVAL;
  1120. if ((ser->irq != port->irq) ||
  1121. (ser->io_type != UPIO_MEM) ||
  1122. (ser->baud_base != port->uartclk) ||
  1123. (ser->iomem_base != (void *)port->mapbase) ||
  1124. (ser->hub6 != 0))
  1125. return -EINVAL;
  1126. return 0;
  1127. }
  1128. static struct uart_ops mpc52xx_uart_ops = {
  1129. .tx_empty = mpc52xx_uart_tx_empty,
  1130. .set_mctrl = mpc52xx_uart_set_mctrl,
  1131. .get_mctrl = mpc52xx_uart_get_mctrl,
  1132. .stop_tx = mpc52xx_uart_stop_tx,
  1133. .start_tx = mpc52xx_uart_start_tx,
  1134. .stop_rx = mpc52xx_uart_stop_rx,
  1135. .enable_ms = mpc52xx_uart_enable_ms,
  1136. .break_ctl = mpc52xx_uart_break_ctl,
  1137. .startup = mpc52xx_uart_startup,
  1138. .shutdown = mpc52xx_uart_shutdown,
  1139. .set_termios = mpc52xx_uart_set_termios,
  1140. /* .pm = mpc52xx_uart_pm, Not supported yet */
  1141. .type = mpc52xx_uart_type,
  1142. .release_port = mpc52xx_uart_release_port,
  1143. .request_port = mpc52xx_uart_request_port,
  1144. .config_port = mpc52xx_uart_config_port,
  1145. .verify_port = mpc52xx_uart_verify_port
  1146. };
  1147. /* ======================================================================== */
  1148. /* Interrupt handling */
  1149. /* ======================================================================== */
  1150. static inline int
  1151. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  1152. {
  1153. struct tty_port *tport = &port->state->port;
  1154. unsigned char ch, flag;
  1155. unsigned short status;
  1156. /* While we can read, do so ! */
  1157. while (psc_ops->raw_rx_rdy(port)) {
  1158. /* Get the char */
  1159. ch = psc_ops->read_char(port);
  1160. /* Handle sysreq char */
  1161. #ifdef SUPPORT_SYSRQ
  1162. if (uart_handle_sysrq_char(port, ch)) {
  1163. port->sysrq = 0;
  1164. continue;
  1165. }
  1166. #endif
  1167. /* Store it */
  1168. flag = TTY_NORMAL;
  1169. port->icount.rx++;
  1170. status = psc_ops->get_status(port);
  1171. if (status & (MPC52xx_PSC_SR_PE |
  1172. MPC52xx_PSC_SR_FE |
  1173. MPC52xx_PSC_SR_RB)) {
  1174. if (status & MPC52xx_PSC_SR_RB) {
  1175. flag = TTY_BREAK;
  1176. uart_handle_break(port);
  1177. port->icount.brk++;
  1178. } else if (status & MPC52xx_PSC_SR_PE) {
  1179. flag = TTY_PARITY;
  1180. port->icount.parity++;
  1181. }
  1182. else if (status & MPC52xx_PSC_SR_FE) {
  1183. flag = TTY_FRAME;
  1184. port->icount.frame++;
  1185. }
  1186. /* Clear error condition */
  1187. psc_ops->command(port, MPC52xx_PSC_RST_ERR_STAT);
  1188. }
  1189. tty_insert_flip_char(tport, ch, flag);
  1190. if (status & MPC52xx_PSC_SR_OE) {
  1191. /*
  1192. * Overrun is special, since it's
  1193. * reported immediately, and doesn't
  1194. * affect the current character
  1195. */
  1196. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  1197. port->icount.overrun++;
  1198. }
  1199. }
  1200. spin_unlock(&port->lock);
  1201. tty_flip_buffer_push(tport);
  1202. spin_lock(&port->lock);
  1203. return psc_ops->raw_rx_rdy(port);
  1204. }
  1205. static inline int
  1206. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  1207. {
  1208. struct circ_buf *xmit = &port->state->xmit;
  1209. /* Process out of band chars */
  1210. if (port->x_char) {
  1211. psc_ops->write_char(port, port->x_char);
  1212. port->icount.tx++;
  1213. port->x_char = 0;
  1214. return 1;
  1215. }
  1216. /* Nothing to do ? */
  1217. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  1218. mpc52xx_uart_stop_tx(port);
  1219. return 0;
  1220. }
  1221. /* Send chars */
  1222. while (psc_ops->raw_tx_rdy(port)) {
  1223. psc_ops->write_char(port, xmit->buf[xmit->tail]);
  1224. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1225. port->icount.tx++;
  1226. if (uart_circ_empty(xmit))
  1227. break;
  1228. }
  1229. /* Wake up */
  1230. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1231. uart_write_wakeup(port);
  1232. /* Maybe we're done after all */
  1233. if (uart_circ_empty(xmit)) {
  1234. mpc52xx_uart_stop_tx(port);
  1235. return 0;
  1236. }
  1237. return 1;
  1238. }
  1239. static irqreturn_t
  1240. mpc5xxx_uart_process_int(struct uart_port *port)
  1241. {
  1242. unsigned long pass = ISR_PASS_LIMIT;
  1243. unsigned int keepgoing;
  1244. u8 status;
  1245. /* While we have stuff to do, we continue */
  1246. do {
  1247. /* If we don't find anything to do, we stop */
  1248. keepgoing = 0;
  1249. psc_ops->rx_clr_irq(port);
  1250. if (psc_ops->rx_rdy(port))
  1251. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  1252. psc_ops->tx_clr_irq(port);
  1253. if (psc_ops->tx_rdy(port))
  1254. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  1255. status = psc_ops->get_ipcr(port);
  1256. if (status & MPC52xx_PSC_D_DCD)
  1257. uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
  1258. if (status & MPC52xx_PSC_D_CTS)
  1259. uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
  1260. /* Limit number of iteration */
  1261. if (!(--pass))
  1262. keepgoing = 0;
  1263. } while (keepgoing);
  1264. return IRQ_HANDLED;
  1265. }
  1266. static irqreturn_t
  1267. mpc52xx_uart_int(int irq, void *dev_id)
  1268. {
  1269. struct uart_port *port = dev_id;
  1270. irqreturn_t ret;
  1271. spin_lock(&port->lock);
  1272. ret = psc_ops->handle_irq(port);
  1273. spin_unlock(&port->lock);
  1274. return ret;
  1275. }
  1276. /* ======================================================================== */
  1277. /* Console ( if applicable ) */
  1278. /* ======================================================================== */
  1279. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  1280. static void __init
  1281. mpc52xx_console_get_options(struct uart_port *port,
  1282. int *baud, int *parity, int *bits, int *flow)
  1283. {
  1284. unsigned char mr1;
  1285. pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
  1286. /* Read the mode registers */
  1287. mr1 = psc_ops->get_mr1(port);
  1288. /* CT{U,L}R are write-only ! */
  1289. *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1290. /* Parse them */
  1291. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  1292. case MPC52xx_PSC_MODE_5_BITS:
  1293. *bits = 5;
  1294. break;
  1295. case MPC52xx_PSC_MODE_6_BITS:
  1296. *bits = 6;
  1297. break;
  1298. case MPC52xx_PSC_MODE_7_BITS:
  1299. *bits = 7;
  1300. break;
  1301. case MPC52xx_PSC_MODE_8_BITS:
  1302. default:
  1303. *bits = 8;
  1304. }
  1305. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  1306. *parity = 'n';
  1307. else
  1308. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  1309. }
  1310. static void
  1311. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  1312. {
  1313. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1314. unsigned int i, j;
  1315. /* Disable interrupts */
  1316. psc_ops->cw_disable_ints(port);
  1317. /* Wait the TX buffer to be empty */
  1318. j = 5000000; /* Maximum wait */
  1319. while (!mpc52xx_uart_tx_empty(port) && --j)
  1320. udelay(1);
  1321. /* Write all the chars */
  1322. for (i = 0; i < count; i++, s++) {
  1323. /* Line return handling */
  1324. if (*s == '\n')
  1325. psc_ops->write_char(port, '\r');
  1326. /* Send the char */
  1327. psc_ops->write_char(port, *s);
  1328. /* Wait the TX buffer to be empty */
  1329. j = 20000; /* Maximum wait */
  1330. while (!mpc52xx_uart_tx_empty(port) && --j)
  1331. udelay(1);
  1332. }
  1333. /* Restore interrupt state */
  1334. psc_ops->cw_restore_ints(port);
  1335. }
  1336. static int __init
  1337. mpc52xx_console_setup(struct console *co, char *options)
  1338. {
  1339. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1340. struct device_node *np = mpc52xx_uart_nodes[co->index];
  1341. unsigned int uartclk;
  1342. struct resource res;
  1343. int ret;
  1344. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1345. int bits = 8;
  1346. int parity = 'n';
  1347. int flow = 'n';
  1348. pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
  1349. co, co->index, options);
  1350. if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
  1351. pr_debug("PSC%x out of range\n", co->index);
  1352. return -EINVAL;
  1353. }
  1354. if (!np) {
  1355. pr_debug("PSC%x not found in device tree\n", co->index);
  1356. return -EINVAL;
  1357. }
  1358. pr_debug("Console on ttyPSC%x is %s\n",
  1359. co->index, mpc52xx_uart_nodes[co->index]->full_name);
  1360. /* Fetch register locations */
  1361. ret = of_address_to_resource(np, 0, &res);
  1362. if (ret) {
  1363. pr_debug("Could not get resources for PSC%x\n", co->index);
  1364. return ret;
  1365. }
  1366. uartclk = mpc5xxx_get_bus_frequency(np);
  1367. if (uartclk == 0) {
  1368. pr_debug("Could not find uart clock frequency!\n");
  1369. return -EINVAL;
  1370. }
  1371. /* Basic port init. Needed since we use some uart_??? func before
  1372. * real init for early access */
  1373. spin_lock_init(&port->lock);
  1374. port->uartclk = uartclk;
  1375. port->ops = &mpc52xx_uart_ops;
  1376. port->mapbase = res.start;
  1377. port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
  1378. port->irq = irq_of_parse_and_map(np, 0);
  1379. if (port->membase == NULL)
  1380. return -EINVAL;
  1381. pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
  1382. (void *)port->mapbase, port->membase,
  1383. port->irq, port->uartclk);
  1384. /* Setup the port parameters accoding to options */
  1385. if (options)
  1386. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1387. else
  1388. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  1389. pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
  1390. baud, bits, parity, flow);
  1391. return uart_set_options(port, co, baud, parity, bits, flow);
  1392. }
  1393. static struct uart_driver mpc52xx_uart_driver;
  1394. static struct console mpc52xx_console = {
  1395. .name = "ttyPSC",
  1396. .write = mpc52xx_console_write,
  1397. .device = uart_console_device,
  1398. .setup = mpc52xx_console_setup,
  1399. .flags = CON_PRINTBUFFER,
  1400. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
  1401. .data = &mpc52xx_uart_driver,
  1402. };
  1403. static int __init
  1404. mpc52xx_console_init(void)
  1405. {
  1406. mpc52xx_uart_of_enumerate();
  1407. register_console(&mpc52xx_console);
  1408. return 0;
  1409. }
  1410. console_initcall(mpc52xx_console_init);
  1411. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  1412. #else
  1413. #define MPC52xx_PSC_CONSOLE NULL
  1414. #endif
  1415. /* ======================================================================== */
  1416. /* UART Driver */
  1417. /* ======================================================================== */
  1418. static struct uart_driver mpc52xx_uart_driver = {
  1419. .driver_name = "mpc52xx_psc_uart",
  1420. .dev_name = "ttyPSC",
  1421. .major = SERIAL_PSC_MAJOR,
  1422. .minor = SERIAL_PSC_MINOR,
  1423. .nr = MPC52xx_PSC_MAXNUM,
  1424. .cons = MPC52xx_PSC_CONSOLE,
  1425. };
  1426. /* ======================================================================== */
  1427. /* OF Platform Driver */
  1428. /* ======================================================================== */
  1429. static const struct of_device_id mpc52xx_uart_of_match[] = {
  1430. #ifdef CONFIG_PPC_MPC52xx
  1431. { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
  1432. { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1433. /* binding used by old lite5200 device trees: */
  1434. { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1435. /* binding used by efika: */
  1436. { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
  1437. #endif
  1438. #ifdef CONFIG_PPC_MPC512x
  1439. { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
  1440. { .compatible = "fsl,mpc5125-psc-uart", .data = &mpc5125_psc_ops, },
  1441. #endif
  1442. {},
  1443. };
  1444. static int mpc52xx_uart_of_probe(struct platform_device *op)
  1445. {
  1446. int idx = -1;
  1447. unsigned int uartclk;
  1448. struct uart_port *port = NULL;
  1449. struct resource res;
  1450. int ret;
  1451. /* Check validity & presence */
  1452. for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
  1453. if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
  1454. break;
  1455. if (idx >= MPC52xx_PSC_MAXNUM)
  1456. return -EINVAL;
  1457. pr_debug("Found %s assigned to ttyPSC%x\n",
  1458. mpc52xx_uart_nodes[idx]->full_name, idx);
  1459. /* set the uart clock to the input clock of the psc, the different
  1460. * prescalers are taken into account in the set_baudrate() methods
  1461. * of the respective chip */
  1462. uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
  1463. if (uartclk == 0) {
  1464. dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
  1465. return -EINVAL;
  1466. }
  1467. /* Init the port structure */
  1468. port = &mpc52xx_uart_ports[idx];
  1469. spin_lock_init(&port->lock);
  1470. port->uartclk = uartclk;
  1471. port->fifosize = 512;
  1472. port->iotype = UPIO_MEM;
  1473. port->flags = UPF_BOOT_AUTOCONF |
  1474. (uart_console(port) ? 0 : UPF_IOREMAP);
  1475. port->line = idx;
  1476. port->ops = &mpc52xx_uart_ops;
  1477. port->dev = &op->dev;
  1478. /* Search for IRQ and mapbase */
  1479. ret = of_address_to_resource(op->dev.of_node, 0, &res);
  1480. if (ret)
  1481. return ret;
  1482. port->mapbase = res.start;
  1483. if (!port->mapbase) {
  1484. dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
  1485. return -EINVAL;
  1486. }
  1487. psc_ops->get_irq(port, op->dev.of_node);
  1488. if (port->irq == 0) {
  1489. dev_dbg(&op->dev, "Could not get irq\n");
  1490. return -EINVAL;
  1491. }
  1492. dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
  1493. (void *)port->mapbase, port->irq, port->uartclk);
  1494. /* Add the port to the uart sub-system */
  1495. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  1496. if (ret)
  1497. return ret;
  1498. platform_set_drvdata(op, (void *)port);
  1499. return 0;
  1500. }
  1501. static int
  1502. mpc52xx_uart_of_remove(struct platform_device *op)
  1503. {
  1504. struct uart_port *port = platform_get_drvdata(op);
  1505. if (port)
  1506. uart_remove_one_port(&mpc52xx_uart_driver, port);
  1507. return 0;
  1508. }
  1509. #ifdef CONFIG_PM
  1510. static int
  1511. mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
  1512. {
  1513. struct uart_port *port = platform_get_drvdata(op);
  1514. if (port)
  1515. uart_suspend_port(&mpc52xx_uart_driver, port);
  1516. return 0;
  1517. }
  1518. static int
  1519. mpc52xx_uart_of_resume(struct platform_device *op)
  1520. {
  1521. struct uart_port *port = platform_get_drvdata(op);
  1522. if (port)
  1523. uart_resume_port(&mpc52xx_uart_driver, port);
  1524. return 0;
  1525. }
  1526. #endif
  1527. static void
  1528. mpc52xx_uart_of_assign(struct device_node *np)
  1529. {
  1530. int i;
  1531. /* Find the first free PSC number */
  1532. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1533. if (mpc52xx_uart_nodes[i] == NULL) {
  1534. of_node_get(np);
  1535. mpc52xx_uart_nodes[i] = np;
  1536. return;
  1537. }
  1538. }
  1539. }
  1540. static void
  1541. mpc52xx_uart_of_enumerate(void)
  1542. {
  1543. static int enum_done;
  1544. struct device_node *np;
  1545. const struct of_device_id *match;
  1546. int i;
  1547. if (enum_done)
  1548. return;
  1549. /* Assign index to each PSC in device tree */
  1550. for_each_matching_node(np, mpc52xx_uart_of_match) {
  1551. match = of_match_node(mpc52xx_uart_of_match, np);
  1552. psc_ops = match->data;
  1553. mpc52xx_uart_of_assign(np);
  1554. }
  1555. enum_done = 1;
  1556. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1557. if (mpc52xx_uart_nodes[i])
  1558. pr_debug("%s assigned to ttyPSC%x\n",
  1559. mpc52xx_uart_nodes[i]->full_name, i);
  1560. }
  1561. }
  1562. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  1563. static struct platform_driver mpc52xx_uart_of_driver = {
  1564. .probe = mpc52xx_uart_of_probe,
  1565. .remove = mpc52xx_uart_of_remove,
  1566. #ifdef CONFIG_PM
  1567. .suspend = mpc52xx_uart_of_suspend,
  1568. .resume = mpc52xx_uart_of_resume,
  1569. #endif
  1570. .driver = {
  1571. .name = "mpc52xx-psc-uart",
  1572. .of_match_table = mpc52xx_uart_of_match,
  1573. },
  1574. };
  1575. /* ======================================================================== */
  1576. /* Module */
  1577. /* ======================================================================== */
  1578. static int __init
  1579. mpc52xx_uart_init(void)
  1580. {
  1581. int ret;
  1582. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  1583. ret = uart_register_driver(&mpc52xx_uart_driver);
  1584. if (ret) {
  1585. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  1586. __FILE__, ret);
  1587. return ret;
  1588. }
  1589. mpc52xx_uart_of_enumerate();
  1590. /*
  1591. * Map the PSC FIFO Controller and init if on MPC512x.
  1592. */
  1593. if (psc_ops && psc_ops->fifoc_init) {
  1594. ret = psc_ops->fifoc_init();
  1595. if (ret)
  1596. goto err_init;
  1597. }
  1598. ret = platform_driver_register(&mpc52xx_uart_of_driver);
  1599. if (ret) {
  1600. printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
  1601. __FILE__, ret);
  1602. goto err_reg;
  1603. }
  1604. return 0;
  1605. err_reg:
  1606. if (psc_ops && psc_ops->fifoc_uninit)
  1607. psc_ops->fifoc_uninit();
  1608. err_init:
  1609. uart_unregister_driver(&mpc52xx_uart_driver);
  1610. return ret;
  1611. }
  1612. static void __exit
  1613. mpc52xx_uart_exit(void)
  1614. {
  1615. if (psc_ops->fifoc_uninit)
  1616. psc_ops->fifoc_uninit();
  1617. platform_driver_unregister(&mpc52xx_uart_of_driver);
  1618. uart_unregister_driver(&mpc52xx_uart_driver);
  1619. }
  1620. module_init(mpc52xx_uart_init);
  1621. module_exit(mpc52xx_uart_exit);
  1622. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  1623. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  1624. MODULE_LICENSE("GPL");