mpc52xx_uart.c 38 KB

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