mpc52xx_uart.c 50 KB

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