mpc52xx_uart.c 48 KB

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