mpc52xx_uart.c 38 KB

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