mpc52xx_uart.c 39 KB

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