mpc52xx_uart.c 39 KB

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