mpc52xx_uart.c 39 KB

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