mpc52xx_uart.c 49 KB

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