mpc52xx_uart.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
  4. *
  5. * FIXME According to the usermanual the status bits in the status register
  6. * are only updated when the peripherals access the FIFO and not when the
  7. * CPU access them. So since we use this bits to know when we stop writing
  8. * and reading, they may not be updated in-time and a race condition may
  9. * exists. But I haven't be able to prove this and I don't care. But if
  10. * any problem arises, it might worth checking. The TX/RX FIFO Stats
  11. * registers should be used in addition.
  12. * Update: Actually, they seem updated ... At least the bits we use.
  13. *
  14. *
  15. * Maintainer : Sylvain Munaut <tnt@246tNt.com>
  16. *
  17. * Some of the code has been inspired/copied from the 2.4 code written
  18. * by Dale Farnsworth <dfarnsworth@mvista.com>.
  19. *
  20. * Copyright (C) 2008 Freescale Semiconductor Inc.
  21. * John Rigby <jrigby@gmail.com>
  22. * Added support for MPC5121
  23. * Copyright (C) 2006 Secret Lab Technologies Ltd.
  24. * Grant Likely <grant.likely@secretlab.ca>
  25. * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
  26. * Copyright (C) 2003 MontaVista, Software, Inc.
  27. */
  28. #undef DEBUG
  29. #include <linux/device.h>
  30. #include <linux/module.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/serial.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/console.h>
  36. #include <linux/delay.h>
  37. #include <linux/io.h>
  38. #include <linux/of.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/clk.h>
  41. #include <asm/mpc52xx.h>
  42. #include <asm/mpc52xx_psc.h>
  43. #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  44. #define SUPPORT_SYSRQ
  45. #endif
  46. #include <linux/serial_core.h>
  47. /* We've been assigned a range on the "Low-density serial ports" major */
  48. #define SERIAL_PSC_MAJOR 204
  49. #define SERIAL_PSC_MINOR 148
  50. #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
  51. static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
  52. /* Rem: - We use the read_status_mask as a shadow of
  53. * psc->mpc52xx_psc_imr
  54. * - It's important that is array is all zero on start as we
  55. * use it to know if it's initialized or not ! If it's not sure
  56. * it's cleared, then a memset(...,0,...) should be added to
  57. * the console_init
  58. */
  59. /* lookup table for matching device nodes to index numbers */
  60. static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
  61. static void mpc52xx_uart_of_enumerate(void);
  62. #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
  63. /* Forward declaration of the interruption handling routine */
  64. static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
  65. static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
  66. /* ======================================================================== */
  67. /* PSC fifo operations for isolating differences between 52xx and 512x */
  68. /* ======================================================================== */
  69. struct psc_ops {
  70. void (*fifo_init)(struct uart_port *port);
  71. int (*raw_rx_rdy)(struct uart_port *port);
  72. int (*raw_tx_rdy)(struct uart_port *port);
  73. int (*rx_rdy)(struct uart_port *port);
  74. int (*tx_rdy)(struct uart_port *port);
  75. int (*tx_empty)(struct uart_port *port);
  76. void (*stop_rx)(struct uart_port *port);
  77. void (*start_tx)(struct uart_port *port);
  78. void (*stop_tx)(struct uart_port *port);
  79. void (*rx_clr_irq)(struct uart_port *port);
  80. void (*tx_clr_irq)(struct uart_port *port);
  81. void (*write_char)(struct uart_port *port, unsigned char c);
  82. unsigned char (*read_char)(struct uart_port *port);
  83. void (*cw_disable_ints)(struct uart_port *port);
  84. void (*cw_restore_ints)(struct uart_port *port);
  85. unsigned int (*set_baudrate)(struct uart_port *port,
  86. struct ktermios *new,
  87. struct ktermios *old);
  88. int (*clock_alloc)(struct uart_port *port);
  89. void (*clock_relse)(struct uart_port *port);
  90. int (*clock)(struct uart_port *port, int enable);
  91. int (*fifoc_init)(void);
  92. void (*fifoc_uninit)(void);
  93. void (*get_irq)(struct uart_port *, struct device_node *);
  94. irqreturn_t (*handle_irq)(struct uart_port *port);
  95. u16 (*get_status)(struct uart_port *port);
  96. u8 (*get_ipcr)(struct uart_port *port);
  97. void (*command)(struct uart_port *port, u8 cmd);
  98. void (*set_mode)(struct uart_port *port, u8 mr1, u8 mr2);
  99. void (*set_rts)(struct uart_port *port, int state);
  100. void (*enable_ms)(struct uart_port *port);
  101. void (*set_sicr)(struct uart_port *port, u32 val);
  102. void (*set_imr)(struct uart_port *port, u16 val);
  103. u8 (*get_mr1)(struct uart_port *port);
  104. };
  105. /* setting the prescaler and divisor reg is common for all chips */
  106. static inline void mpc52xx_set_divisor(struct mpc52xx_psc __iomem *psc,
  107. u16 prescaler, unsigned int divisor)
  108. {
  109. /* select prescaler */
  110. out_be16(&psc->mpc52xx_psc_clock_select, prescaler);
  111. out_8(&psc->ctur, divisor >> 8);
  112. out_8(&psc->ctlr, divisor & 0xff);
  113. }
  114. static u16 mpc52xx_psc_get_status(struct uart_port *port)
  115. {
  116. return in_be16(&PSC(port)->mpc52xx_psc_status);
  117. }
  118. static u8 mpc52xx_psc_get_ipcr(struct uart_port *port)
  119. {
  120. return in_8(&PSC(port)->mpc52xx_psc_ipcr);
  121. }
  122. static void mpc52xx_psc_command(struct uart_port *port, u8 cmd)
  123. {
  124. out_8(&PSC(port)->command, cmd);
  125. }
  126. static void mpc52xx_psc_set_mode(struct uart_port *port, u8 mr1, u8 mr2)
  127. {
  128. out_8(&PSC(port)->command, MPC52xx_PSC_SEL_MODE_REG_1);
  129. out_8(&PSC(port)->mode, mr1);
  130. out_8(&PSC(port)->mode, mr2);
  131. }
  132. static void mpc52xx_psc_set_rts(struct uart_port *port, int state)
  133. {
  134. if (state)
  135. out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
  136. else
  137. out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
  138. }
  139. static void mpc52xx_psc_enable_ms(struct uart_port *port)
  140. {
  141. struct mpc52xx_psc __iomem *psc = PSC(port);
  142. /* clear D_*-bits by reading them */
  143. in_8(&psc->mpc52xx_psc_ipcr);
  144. /* enable CTS and DCD as IPC interrupts */
  145. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  146. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  147. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  148. }
  149. static void mpc52xx_psc_set_sicr(struct uart_port *port, u32 val)
  150. {
  151. out_be32(&PSC(port)->sicr, val);
  152. }
  153. static void mpc52xx_psc_set_imr(struct uart_port *port, u16 val)
  154. {
  155. out_be16(&PSC(port)->mpc52xx_psc_imr, val);
  156. }
  157. static u8 mpc52xx_psc_get_mr1(struct uart_port *port)
  158. {
  159. out_8(&PSC(port)->command, MPC52xx_PSC_SEL_MODE_REG_1);
  160. return in_8(&PSC(port)->mode);
  161. }
  162. #ifdef CONFIG_PPC_MPC52xx
  163. #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
  164. static void mpc52xx_psc_fifo_init(struct uart_port *port)
  165. {
  166. struct mpc52xx_psc __iomem *psc = PSC(port);
  167. struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
  168. out_8(&fifo->rfcntl, 0x00);
  169. out_be16(&fifo->rfalarm, 0x1ff);
  170. out_8(&fifo->tfcntl, 0x07);
  171. out_be16(&fifo->tfalarm, 0x80);
  172. port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
  173. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  174. }
  175. static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
  176. {
  177. return in_be16(&PSC(port)->mpc52xx_psc_status)
  178. & MPC52xx_PSC_SR_RXRDY;
  179. }
  180. static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
  181. {
  182. return in_be16(&PSC(port)->mpc52xx_psc_status)
  183. & MPC52xx_PSC_SR_TXRDY;
  184. }
  185. static int mpc52xx_psc_rx_rdy(struct uart_port *port)
  186. {
  187. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  188. & port->read_status_mask
  189. & MPC52xx_PSC_IMR_RXRDY;
  190. }
  191. static int mpc52xx_psc_tx_rdy(struct uart_port *port)
  192. {
  193. return in_be16(&PSC(port)->mpc52xx_psc_isr)
  194. & port->read_status_mask
  195. & MPC52xx_PSC_IMR_TXRDY;
  196. }
  197. static int mpc52xx_psc_tx_empty(struct uart_port *port)
  198. {
  199. u16 sts = in_be16(&PSC(port)->mpc52xx_psc_status);
  200. return (sts & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
  201. }
  202. static void mpc52xx_psc_start_tx(struct uart_port *port)
  203. {
  204. port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
  205. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  206. }
  207. static void mpc52xx_psc_stop_tx(struct uart_port *port)
  208. {
  209. port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
  210. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  211. }
  212. static void mpc52xx_psc_stop_rx(struct uart_port *port)
  213. {
  214. port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
  215. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  216. }
  217. static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
  218. {
  219. }
  220. static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
  221. {
  222. }
  223. static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
  224. {
  225. out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
  226. }
  227. static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
  228. {
  229. return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
  230. }
  231. static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
  232. {
  233. out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
  234. }
  235. static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
  236. {
  237. out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
  238. }
  239. static unsigned int mpc5200_psc_set_baudrate(struct uart_port *port,
  240. struct ktermios *new,
  241. struct ktermios *old)
  242. {
  243. unsigned int baud;
  244. unsigned int divisor;
  245. /* The 5200 has a fixed /32 prescaler, uartclk contains the ipb freq */
  246. baud = uart_get_baud_rate(port, new, old,
  247. port->uartclk / (32 * 0xffff) + 1,
  248. port->uartclk / 32);
  249. divisor = (port->uartclk + 16 * baud) / (32 * baud);
  250. /* enable the /32 prescaler and set the divisor */
  251. mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
  252. return baud;
  253. }
  254. static unsigned int mpc5200b_psc_set_baudrate(struct uart_port *port,
  255. struct ktermios *new,
  256. struct ktermios *old)
  257. {
  258. unsigned int baud;
  259. unsigned int divisor;
  260. u16 prescaler;
  261. /* The 5200B has a selectable /4 or /32 prescaler, uartclk contains the
  262. * ipb freq */
  263. baud = uart_get_baud_rate(port, new, old,
  264. port->uartclk / (32 * 0xffff) + 1,
  265. port->uartclk / 4);
  266. divisor = (port->uartclk + 2 * baud) / (4 * baud);
  267. /* select the proper prescaler and set the divisor
  268. * prefer high prescaler for more tolerance on low baudrates */
  269. if (divisor > 0xffff || baud <= 115200) {
  270. divisor = (divisor + 4) / 8;
  271. prescaler = 0xdd00; /* /32 */
  272. } else
  273. prescaler = 0xff00; /* /4 */
  274. mpc52xx_set_divisor(PSC(port), prescaler, divisor);
  275. return baud;
  276. }
  277. static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
  278. {
  279. port->irqflags = 0;
  280. port->irq = irq_of_parse_and_map(np, 0);
  281. }
  282. /* 52xx specific interrupt handler. The caller holds the port lock */
  283. static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
  284. {
  285. return mpc5xxx_uart_process_int(port);
  286. }
  287. static const struct psc_ops mpc52xx_psc_ops = {
  288. .fifo_init = mpc52xx_psc_fifo_init,
  289. .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
  290. .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
  291. .rx_rdy = mpc52xx_psc_rx_rdy,
  292. .tx_rdy = mpc52xx_psc_tx_rdy,
  293. .tx_empty = mpc52xx_psc_tx_empty,
  294. .stop_rx = mpc52xx_psc_stop_rx,
  295. .start_tx = mpc52xx_psc_start_tx,
  296. .stop_tx = mpc52xx_psc_stop_tx,
  297. .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
  298. .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
  299. .write_char = mpc52xx_psc_write_char,
  300. .read_char = mpc52xx_psc_read_char,
  301. .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
  302. .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
  303. .set_baudrate = mpc5200_psc_set_baudrate,
  304. .get_irq = mpc52xx_psc_get_irq,
  305. .handle_irq = mpc52xx_psc_handle_irq,
  306. .get_status = mpc52xx_psc_get_status,
  307. .get_ipcr = mpc52xx_psc_get_ipcr,
  308. .command = mpc52xx_psc_command,
  309. .set_mode = mpc52xx_psc_set_mode,
  310. .set_rts = mpc52xx_psc_set_rts,
  311. .enable_ms = mpc52xx_psc_enable_ms,
  312. .set_sicr = mpc52xx_psc_set_sicr,
  313. .set_imr = mpc52xx_psc_set_imr,
  314. .get_mr1 = mpc52xx_psc_get_mr1,
  315. };
  316. static const struct psc_ops mpc5200b_psc_ops = {
  317. .fifo_init = mpc52xx_psc_fifo_init,
  318. .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
  319. .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
  320. .rx_rdy = mpc52xx_psc_rx_rdy,
  321. .tx_rdy = mpc52xx_psc_tx_rdy,
  322. .tx_empty = mpc52xx_psc_tx_empty,
  323. .stop_rx = mpc52xx_psc_stop_rx,
  324. .start_tx = mpc52xx_psc_start_tx,
  325. .stop_tx = mpc52xx_psc_stop_tx,
  326. .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
  327. .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
  328. .write_char = mpc52xx_psc_write_char,
  329. .read_char = mpc52xx_psc_read_char,
  330. .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
  331. .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
  332. .set_baudrate = mpc5200b_psc_set_baudrate,
  333. .get_irq = mpc52xx_psc_get_irq,
  334. .handle_irq = mpc52xx_psc_handle_irq,
  335. .get_status = mpc52xx_psc_get_status,
  336. .get_ipcr = mpc52xx_psc_get_ipcr,
  337. .command = mpc52xx_psc_command,
  338. .set_mode = mpc52xx_psc_set_mode,
  339. .set_rts = mpc52xx_psc_set_rts,
  340. .enable_ms = mpc52xx_psc_enable_ms,
  341. .set_sicr = mpc52xx_psc_set_sicr,
  342. .set_imr = mpc52xx_psc_set_imr,
  343. .get_mr1 = mpc52xx_psc_get_mr1,
  344. };
  345. #endif /* CONFIG_PPC_MPC52xx */
  346. #ifdef CONFIG_PPC_MPC512x
  347. #define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
  348. /* PSC FIFO Controller for mpc512x */
  349. struct psc_fifoc {
  350. u32 fifoc_cmd;
  351. u32 fifoc_int;
  352. u32 fifoc_dma;
  353. u32 fifoc_axe;
  354. u32 fifoc_debug;
  355. };
  356. static struct psc_fifoc __iomem *psc_fifoc;
  357. static unsigned int psc_fifoc_irq;
  358. static struct clk *psc_fifoc_clk;
  359. static void mpc512x_psc_fifo_init(struct uart_port *port)
  360. {
  361. /* /32 prescaler */
  362. out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
  363. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  364. out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  365. out_be32(&FIFO_512x(port)->txalarm, 1);
  366. out_be32(&FIFO_512x(port)->tximr, 0);
  367. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  368. out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  369. out_be32(&FIFO_512x(port)->rxalarm, 1);
  370. out_be32(&FIFO_512x(port)->rximr, 0);
  371. out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  372. out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  373. }
  374. static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
  375. {
  376. return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  377. }
  378. static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
  379. {
  380. return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  381. }
  382. static int mpc512x_psc_rx_rdy(struct uart_port *port)
  383. {
  384. return in_be32(&FIFO_512x(port)->rxsr)
  385. & in_be32(&FIFO_512x(port)->rximr)
  386. & MPC512x_PSC_FIFO_ALARM;
  387. }
  388. static int mpc512x_psc_tx_rdy(struct uart_port *port)
  389. {
  390. return in_be32(&FIFO_512x(port)->txsr)
  391. & in_be32(&FIFO_512x(port)->tximr)
  392. & MPC512x_PSC_FIFO_ALARM;
  393. }
  394. static int mpc512x_psc_tx_empty(struct uart_port *port)
  395. {
  396. return in_be32(&FIFO_512x(port)->txsr)
  397. & MPC512x_PSC_FIFO_EMPTY;
  398. }
  399. static void mpc512x_psc_stop_rx(struct uart_port *port)
  400. {
  401. unsigned long rx_fifo_imr;
  402. rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
  403. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  404. out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
  405. }
  406. static void mpc512x_psc_start_tx(struct uart_port *port)
  407. {
  408. unsigned long tx_fifo_imr;
  409. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  410. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  411. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  412. }
  413. static void mpc512x_psc_stop_tx(struct uart_port *port)
  414. {
  415. unsigned long tx_fifo_imr;
  416. tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
  417. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  418. out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
  419. }
  420. static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
  421. {
  422. out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
  423. }
  424. static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
  425. {
  426. out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
  427. }
  428. static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
  429. {
  430. out_8(&FIFO_512x(port)->txdata_8, c);
  431. }
  432. static unsigned char mpc512x_psc_read_char(struct uart_port *port)
  433. {
  434. return in_8(&FIFO_512x(port)->rxdata_8);
  435. }
  436. static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
  437. {
  438. port->read_status_mask =
  439. in_be32(&FIFO_512x(port)->tximr) << 16 |
  440. in_be32(&FIFO_512x(port)->rximr);
  441. out_be32(&FIFO_512x(port)->tximr, 0);
  442. out_be32(&FIFO_512x(port)->rximr, 0);
  443. }
  444. static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
  445. {
  446. out_be32(&FIFO_512x(port)->tximr,
  447. (port->read_status_mask >> 16) & 0x7f);
  448. out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
  449. }
  450. static unsigned int mpc512x_psc_set_baudrate(struct uart_port *port,
  451. struct ktermios *new,
  452. struct ktermios *old)
  453. {
  454. unsigned int baud;
  455. unsigned int divisor;
  456. /*
  457. * The "MPC5121e Microcontroller Reference Manual, Rev. 3" says on
  458. * pg. 30-10 that the chip supports a /32 and a /10 prescaler.
  459. * Furthermore, it states that "After reset, the prescaler by 10
  460. * for the UART mode is selected", but the reset register value is
  461. * 0x0000 which means a /32 prescaler. This is wrong.
  462. *
  463. * In reality using /32 prescaler doesn't work, as it is not supported!
  464. * Use /16 or /10 prescaler, see "MPC5121e Hardware Design Guide",
  465. * Chapter 4.1 PSC in UART Mode.
  466. * Calculate with a /16 prescaler here.
  467. */
  468. /* uartclk contains the ips freq */
  469. baud = uart_get_baud_rate(port, new, old,
  470. port->uartclk / (16 * 0xffff) + 1,
  471. port->uartclk / 16);
  472. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  473. /* enable the /16 prescaler and set the divisor */
  474. mpc52xx_set_divisor(PSC(port), 0xdd00, divisor);
  475. return baud;
  476. }
  477. /* Init PSC FIFO Controller */
  478. static int __init mpc512x_psc_fifoc_init(void)
  479. {
  480. int err;
  481. struct device_node *np;
  482. struct clk *clk;
  483. /* default error code, potentially overwritten by clock calls */
  484. err = -ENODEV;
  485. np = of_find_compatible_node(NULL, NULL,
  486. "fsl,mpc5121-psc-fifo");
  487. if (!np) {
  488. pr_err("%s: Can't find FIFOC node\n", __func__);
  489. goto out_err;
  490. }
  491. clk = of_clk_get(np, 0);
  492. if (IS_ERR(clk)) {
  493. /* backwards compat with device trees that lack clock specs */
  494. clk = clk_get_sys(np->name, "ipg");
  495. }
  496. if (IS_ERR(clk)) {
  497. pr_err("%s: Can't lookup FIFO clock\n", __func__);
  498. err = PTR_ERR(clk);
  499. goto out_ofnode_put;
  500. }
  501. if (clk_prepare_enable(clk)) {
  502. pr_err("%s: Can't enable FIFO clock\n", __func__);
  503. clk_put(clk);
  504. goto out_ofnode_put;
  505. }
  506. psc_fifoc_clk = clk;
  507. psc_fifoc = of_iomap(np, 0);
  508. if (!psc_fifoc) {
  509. pr_err("%s: Can't map FIFOC\n", __func__);
  510. goto out_clk_disable;
  511. }
  512. psc_fifoc_irq = irq_of_parse_and_map(np, 0);
  513. if (psc_fifoc_irq == 0) {
  514. pr_err("%s: Can't get FIFOC irq\n", __func__);
  515. goto out_unmap;
  516. }
  517. of_node_put(np);
  518. return 0;
  519. out_unmap:
  520. iounmap(psc_fifoc);
  521. out_clk_disable:
  522. clk_disable_unprepare(psc_fifoc_clk);
  523. clk_put(psc_fifoc_clk);
  524. out_ofnode_put:
  525. of_node_put(np);
  526. out_err:
  527. return err;
  528. }
  529. static void __exit mpc512x_psc_fifoc_uninit(void)
  530. {
  531. iounmap(psc_fifoc);
  532. /* disable the clock, errors are not fatal */
  533. if (psc_fifoc_clk) {
  534. clk_disable_unprepare(psc_fifoc_clk);
  535. clk_put(psc_fifoc_clk);
  536. psc_fifoc_clk = NULL;
  537. }
  538. }
  539. /* 512x specific interrupt handler. The caller holds the port lock */
  540. static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
  541. {
  542. unsigned long fifoc_int;
  543. int psc_num;
  544. /* Read pending PSC FIFOC interrupts */
  545. fifoc_int = in_be32(&psc_fifoc->fifoc_int);
  546. /* Check if it is an interrupt for this port */
  547. psc_num = (port->mapbase & 0xf00) >> 8;
  548. if (test_bit(psc_num, &fifoc_int) ||
  549. test_bit(psc_num + 16, &fifoc_int))
  550. return mpc5xxx_uart_process_int(port);
  551. return IRQ_NONE;
  552. }
  553. static struct clk *psc_mclk_clk[MPC52xx_PSC_MAXNUM];
  554. static struct clk *psc_ipg_clk[MPC52xx_PSC_MAXNUM];
  555. /* called from within the .request_port() callback (allocation) */
  556. static int mpc512x_psc_alloc_clock(struct uart_port *port)
  557. {
  558. int psc_num;
  559. struct clk *clk;
  560. int err;
  561. psc_num = (port->mapbase & 0xf00) >> 8;
  562. clk = devm_clk_get(port->dev, "mclk");
  563. if (IS_ERR(clk)) {
  564. dev_err(port->dev, "Failed to get MCLK!\n");
  565. err = PTR_ERR(clk);
  566. goto out_err;
  567. }
  568. err = clk_prepare_enable(clk);
  569. if (err) {
  570. dev_err(port->dev, "Failed to enable MCLK!\n");
  571. goto out_err;
  572. }
  573. psc_mclk_clk[psc_num] = clk;
  574. clk = devm_clk_get(port->dev, "ipg");
  575. if (IS_ERR(clk)) {
  576. dev_err(port->dev, "Failed to get IPG clock!\n");
  577. err = PTR_ERR(clk);
  578. goto out_err;
  579. }
  580. err = clk_prepare_enable(clk);
  581. if (err) {
  582. dev_err(port->dev, "Failed to enable IPG clock!\n");
  583. goto out_err;
  584. }
  585. psc_ipg_clk[psc_num] = clk;
  586. return 0;
  587. out_err:
  588. if (psc_mclk_clk[psc_num]) {
  589. clk_disable_unprepare(psc_mclk_clk[psc_num]);
  590. psc_mclk_clk[psc_num] = NULL;
  591. }
  592. if (psc_ipg_clk[psc_num]) {
  593. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  594. psc_ipg_clk[psc_num] = NULL;
  595. }
  596. return err;
  597. }
  598. /* called from within the .release_port() callback (release) */
  599. static void mpc512x_psc_relse_clock(struct uart_port *port)
  600. {
  601. int psc_num;
  602. struct clk *clk;
  603. psc_num = (port->mapbase & 0xf00) >> 8;
  604. clk = psc_mclk_clk[psc_num];
  605. if (clk) {
  606. clk_disable_unprepare(clk);
  607. psc_mclk_clk[psc_num] = NULL;
  608. }
  609. if (psc_ipg_clk[psc_num]) {
  610. clk_disable_unprepare(psc_ipg_clk[psc_num]);
  611. psc_ipg_clk[psc_num] = NULL;
  612. }
  613. }
  614. /* implementation of the .clock() callback (enable/disable) */
  615. static int mpc512x_psc_endis_clock(struct uart_port *port, int enable)
  616. {
  617. int psc_num;
  618. struct clk *psc_clk;
  619. int ret;
  620. if (uart_console(port))
  621. return 0;
  622. psc_num = (port->mapbase & 0xf00) >> 8;
  623. psc_clk = psc_mclk_clk[psc_num];
  624. if (!psc_clk) {
  625. dev_err(port->dev, "Failed to get PSC clock entry!\n");
  626. return -ENODEV;
  627. }
  628. dev_dbg(port->dev, "mclk %sable\n", enable ? "en" : "dis");
  629. if (enable) {
  630. ret = clk_enable(psc_clk);
  631. if (ret)
  632. dev_err(port->dev, "Failed to enable MCLK!\n");
  633. return ret;
  634. } else {
  635. clk_disable(psc_clk);
  636. return 0;
  637. }
  638. }
  639. static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
  640. {
  641. port->irqflags = IRQF_SHARED;
  642. port->irq = psc_fifoc_irq;
  643. }
  644. #endif
  645. #ifdef CONFIG_PPC_MPC512x
  646. #define PSC_5125(port) ((struct mpc5125_psc __iomem *)((port)->membase))
  647. #define FIFO_5125(port) ((struct mpc512x_psc_fifo __iomem *)(PSC_5125(port)+1))
  648. static void mpc5125_psc_fifo_init(struct uart_port *port)
  649. {
  650. /* /32 prescaler */
  651. out_8(&PSC_5125(port)->mpc52xx_psc_clock_select, 0xdd);
  652. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  653. out_be32(&FIFO_5125(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  654. out_be32(&FIFO_5125(port)->txalarm, 1);
  655. out_be32(&FIFO_5125(port)->tximr, 0);
  656. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
  657. out_be32(&FIFO_5125(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
  658. out_be32(&FIFO_5125(port)->rxalarm, 1);
  659. out_be32(&FIFO_5125(port)->rximr, 0);
  660. out_be32(&FIFO_5125(port)->tximr, MPC512x_PSC_FIFO_ALARM);
  661. out_be32(&FIFO_5125(port)->rximr, MPC512x_PSC_FIFO_ALARM);
  662. }
  663. static int mpc5125_psc_raw_rx_rdy(struct uart_port *port)
  664. {
  665. return !(in_be32(&FIFO_5125(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
  666. }
  667. static int mpc5125_psc_raw_tx_rdy(struct uart_port *port)
  668. {
  669. return !(in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_FULL);
  670. }
  671. static int mpc5125_psc_rx_rdy(struct uart_port *port)
  672. {
  673. return in_be32(&FIFO_5125(port)->rxsr) &
  674. in_be32(&FIFO_5125(port)->rximr) & MPC512x_PSC_FIFO_ALARM;
  675. }
  676. static int mpc5125_psc_tx_rdy(struct uart_port *port)
  677. {
  678. return in_be32(&FIFO_5125(port)->txsr) &
  679. in_be32(&FIFO_5125(port)->tximr) & MPC512x_PSC_FIFO_ALARM;
  680. }
  681. static int mpc5125_psc_tx_empty(struct uart_port *port)
  682. {
  683. return in_be32(&FIFO_5125(port)->txsr) & MPC512x_PSC_FIFO_EMPTY;
  684. }
  685. static void mpc5125_psc_stop_rx(struct uart_port *port)
  686. {
  687. unsigned long rx_fifo_imr;
  688. rx_fifo_imr = in_be32(&FIFO_5125(port)->rximr);
  689. rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  690. out_be32(&FIFO_5125(port)->rximr, rx_fifo_imr);
  691. }
  692. static void mpc5125_psc_start_tx(struct uart_port *port)
  693. {
  694. unsigned long tx_fifo_imr;
  695. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  696. tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
  697. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  698. }
  699. static void mpc5125_psc_stop_tx(struct uart_port *port)
  700. {
  701. unsigned long tx_fifo_imr;
  702. tx_fifo_imr = in_be32(&FIFO_5125(port)->tximr);
  703. tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
  704. out_be32(&FIFO_5125(port)->tximr, tx_fifo_imr);
  705. }
  706. static void mpc5125_psc_rx_clr_irq(struct uart_port *port)
  707. {
  708. out_be32(&FIFO_5125(port)->rxisr, in_be32(&FIFO_5125(port)->rxisr));
  709. }
  710. static void mpc5125_psc_tx_clr_irq(struct uart_port *port)
  711. {
  712. out_be32(&FIFO_5125(port)->txisr, in_be32(&FIFO_5125(port)->txisr));
  713. }
  714. static void mpc5125_psc_write_char(struct uart_port *port, unsigned char c)
  715. {
  716. out_8(&FIFO_5125(port)->txdata_8, c);
  717. }
  718. static unsigned char mpc5125_psc_read_char(struct uart_port *port)
  719. {
  720. return in_8(&FIFO_5125(port)->rxdata_8);
  721. }
  722. static void mpc5125_psc_cw_disable_ints(struct uart_port *port)
  723. {
  724. port->read_status_mask =
  725. in_be32(&FIFO_5125(port)->tximr) << 16 |
  726. in_be32(&FIFO_5125(port)->rximr);
  727. out_be32(&FIFO_5125(port)->tximr, 0);
  728. out_be32(&FIFO_5125(port)->rximr, 0);
  729. }
  730. static void mpc5125_psc_cw_restore_ints(struct uart_port *port)
  731. {
  732. out_be32(&FIFO_5125(port)->tximr,
  733. (port->read_status_mask >> 16) & 0x7f);
  734. out_be32(&FIFO_5125(port)->rximr, port->read_status_mask & 0x7f);
  735. }
  736. static inline void mpc5125_set_divisor(struct mpc5125_psc __iomem *psc,
  737. u8 prescaler, unsigned int divisor)
  738. {
  739. /* select prescaler */
  740. out_8(&psc->mpc52xx_psc_clock_select, prescaler);
  741. out_8(&psc->ctur, divisor >> 8);
  742. out_8(&psc->ctlr, divisor & 0xff);
  743. }
  744. static unsigned int mpc5125_psc_set_baudrate(struct uart_port *port,
  745. struct ktermios *new,
  746. struct ktermios *old)
  747. {
  748. unsigned int baud;
  749. unsigned int divisor;
  750. /*
  751. * Calculate with a /16 prescaler here.
  752. */
  753. /* uartclk contains the ips freq */
  754. baud = uart_get_baud_rate(port, new, old,
  755. port->uartclk / (16 * 0xffff) + 1,
  756. port->uartclk / 16);
  757. divisor = (port->uartclk + 8 * baud) / (16 * baud);
  758. /* enable the /16 prescaler and set the divisor */
  759. mpc5125_set_divisor(PSC_5125(port), 0xdd, divisor);
  760. return baud;
  761. }
  762. /*
  763. * MPC5125 have compatible PSC FIFO Controller.
  764. * Special init not needed.
  765. */
  766. static u16 mpc5125_psc_get_status(struct uart_port *port)
  767. {
  768. return in_be16(&PSC_5125(port)->mpc52xx_psc_status);
  769. }
  770. static u8 mpc5125_psc_get_ipcr(struct uart_port *port)
  771. {
  772. return in_8(&PSC_5125(port)->mpc52xx_psc_ipcr);
  773. }
  774. static void mpc5125_psc_command(struct uart_port *port, u8 cmd)
  775. {
  776. out_8(&PSC_5125(port)->command, cmd);
  777. }
  778. static void mpc5125_psc_set_mode(struct uart_port *port, u8 mr1, u8 mr2)
  779. {
  780. out_8(&PSC_5125(port)->mr1, mr1);
  781. out_8(&PSC_5125(port)->mr2, mr2);
  782. }
  783. static void mpc5125_psc_set_rts(struct uart_port *port, int state)
  784. {
  785. if (state & TIOCM_RTS)
  786. out_8(&PSC_5125(port)->op1, MPC52xx_PSC_OP_RTS);
  787. else
  788. out_8(&PSC_5125(port)->op0, MPC52xx_PSC_OP_RTS);
  789. }
  790. static void mpc5125_psc_enable_ms(struct uart_port *port)
  791. {
  792. struct mpc5125_psc __iomem *psc = PSC_5125(port);
  793. /* clear D_*-bits by reading them */
  794. in_8(&psc->mpc52xx_psc_ipcr);
  795. /* enable CTS and DCD as IPC interrupts */
  796. out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
  797. port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
  798. out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
  799. }
  800. static void mpc5125_psc_set_sicr(struct uart_port *port, u32 val)
  801. {
  802. out_be32(&PSC_5125(port)->sicr, val);
  803. }
  804. static void mpc5125_psc_set_imr(struct uart_port *port, u16 val)
  805. {
  806. out_be16(&PSC_5125(port)->mpc52xx_psc_imr, val);
  807. }
  808. static u8 mpc5125_psc_get_mr1(struct uart_port *port)
  809. {
  810. return in_8(&PSC_5125(port)->mr1);
  811. }
  812. static const struct psc_ops mpc5125_psc_ops = {
  813. .fifo_init = mpc5125_psc_fifo_init,
  814. .raw_rx_rdy = mpc5125_psc_raw_rx_rdy,
  815. .raw_tx_rdy = mpc5125_psc_raw_tx_rdy,
  816. .rx_rdy = mpc5125_psc_rx_rdy,
  817. .tx_rdy = mpc5125_psc_tx_rdy,
  818. .tx_empty = mpc5125_psc_tx_empty,
  819. .stop_rx = mpc5125_psc_stop_rx,
  820. .start_tx = mpc5125_psc_start_tx,
  821. .stop_tx = mpc5125_psc_stop_tx,
  822. .rx_clr_irq = mpc5125_psc_rx_clr_irq,
  823. .tx_clr_irq = mpc5125_psc_tx_clr_irq,
  824. .write_char = mpc5125_psc_write_char,
  825. .read_char = mpc5125_psc_read_char,
  826. .cw_disable_ints = mpc5125_psc_cw_disable_ints,
  827. .cw_restore_ints = mpc5125_psc_cw_restore_ints,
  828. .set_baudrate = mpc5125_psc_set_baudrate,
  829. .clock_alloc = mpc512x_psc_alloc_clock,
  830. .clock_relse = mpc512x_psc_relse_clock,
  831. .clock = mpc512x_psc_endis_clock,
  832. .fifoc_init = mpc512x_psc_fifoc_init,
  833. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  834. .get_irq = mpc512x_psc_get_irq,
  835. .handle_irq = mpc512x_psc_handle_irq,
  836. .get_status = mpc5125_psc_get_status,
  837. .get_ipcr = mpc5125_psc_get_ipcr,
  838. .command = mpc5125_psc_command,
  839. .set_mode = mpc5125_psc_set_mode,
  840. .set_rts = mpc5125_psc_set_rts,
  841. .enable_ms = mpc5125_psc_enable_ms,
  842. .set_sicr = mpc5125_psc_set_sicr,
  843. .set_imr = mpc5125_psc_set_imr,
  844. .get_mr1 = mpc5125_psc_get_mr1,
  845. };
  846. static const struct psc_ops mpc512x_psc_ops = {
  847. .fifo_init = mpc512x_psc_fifo_init,
  848. .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
  849. .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
  850. .rx_rdy = mpc512x_psc_rx_rdy,
  851. .tx_rdy = mpc512x_psc_tx_rdy,
  852. .tx_empty = mpc512x_psc_tx_empty,
  853. .stop_rx = mpc512x_psc_stop_rx,
  854. .start_tx = mpc512x_psc_start_tx,
  855. .stop_tx = mpc512x_psc_stop_tx,
  856. .rx_clr_irq = mpc512x_psc_rx_clr_irq,
  857. .tx_clr_irq = mpc512x_psc_tx_clr_irq,
  858. .write_char = mpc512x_psc_write_char,
  859. .read_char = mpc512x_psc_read_char,
  860. .cw_disable_ints = mpc512x_psc_cw_disable_ints,
  861. .cw_restore_ints = mpc512x_psc_cw_restore_ints,
  862. .set_baudrate = mpc512x_psc_set_baudrate,
  863. .clock_alloc = mpc512x_psc_alloc_clock,
  864. .clock_relse = mpc512x_psc_relse_clock,
  865. .clock = mpc512x_psc_endis_clock,
  866. .fifoc_init = mpc512x_psc_fifoc_init,
  867. .fifoc_uninit = mpc512x_psc_fifoc_uninit,
  868. .get_irq = mpc512x_psc_get_irq,
  869. .handle_irq = mpc512x_psc_handle_irq,
  870. .get_status = mpc52xx_psc_get_status,
  871. .get_ipcr = mpc52xx_psc_get_ipcr,
  872. .command = mpc52xx_psc_command,
  873. .set_mode = mpc52xx_psc_set_mode,
  874. .set_rts = mpc52xx_psc_set_rts,
  875. .enable_ms = mpc52xx_psc_enable_ms,
  876. .set_sicr = mpc52xx_psc_set_sicr,
  877. .set_imr = mpc52xx_psc_set_imr,
  878. .get_mr1 = mpc52xx_psc_get_mr1,
  879. };
  880. #endif /* CONFIG_PPC_MPC512x */
  881. static const struct psc_ops *psc_ops;
  882. /* ======================================================================== */
  883. /* UART operations */
  884. /* ======================================================================== */
  885. static unsigned int
  886. mpc52xx_uart_tx_empty(struct uart_port *port)
  887. {
  888. return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
  889. }
  890. static void
  891. mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  892. {
  893. psc_ops->set_rts(port, mctrl & TIOCM_RTS);
  894. }
  895. static unsigned int
  896. mpc52xx_uart_get_mctrl(struct uart_port *port)
  897. {
  898. unsigned int ret = TIOCM_DSR;
  899. u8 status = psc_ops->get_ipcr(port);
  900. if (!(status & MPC52xx_PSC_CTS))
  901. ret |= TIOCM_CTS;
  902. if (!(status & MPC52xx_PSC_DCD))
  903. ret |= TIOCM_CAR;
  904. return ret;
  905. }
  906. static void
  907. mpc52xx_uart_stop_tx(struct uart_port *port)
  908. {
  909. /* port->lock taken by caller */
  910. psc_ops->stop_tx(port);
  911. }
  912. static void
  913. mpc52xx_uart_start_tx(struct uart_port *port)
  914. {
  915. /* port->lock taken by caller */
  916. psc_ops->start_tx(port);
  917. }
  918. static void
  919. mpc52xx_uart_stop_rx(struct uart_port *port)
  920. {
  921. /* port->lock taken by caller */
  922. psc_ops->stop_rx(port);
  923. }
  924. static void
  925. mpc52xx_uart_enable_ms(struct uart_port *port)
  926. {
  927. psc_ops->enable_ms(port);
  928. }
  929. static void
  930. mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
  931. {
  932. unsigned long flags;
  933. spin_lock_irqsave(&port->lock, flags);
  934. if (ctl == -1)
  935. psc_ops->command(port, MPC52xx_PSC_START_BRK);
  936. else
  937. psc_ops->command(port, MPC52xx_PSC_STOP_BRK);
  938. spin_unlock_irqrestore(&port->lock, flags);
  939. }
  940. static int
  941. mpc52xx_uart_startup(struct uart_port *port)
  942. {
  943. int ret;
  944. if (psc_ops->clock) {
  945. ret = psc_ops->clock(port, 1);
  946. if (ret)
  947. return ret;
  948. }
  949. /* Request IRQ */
  950. ret = request_irq(port->irq, mpc52xx_uart_int,
  951. port->irqflags, "mpc52xx_psc_uart", port);
  952. if (ret)
  953. return ret;
  954. /* Reset/activate the port, clear and enable interrupts */
  955. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  956. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  957. /*
  958. * According to Freescale's support the RST_TX command can produce a
  959. * spike on the TX pin. So they recommend to delay "for one character".
  960. * One millisecond should be enough for everyone.
  961. */
  962. msleep(1);
  963. psc_ops->set_sicr(port, 0); /* UART mode DCD ignored */
  964. psc_ops->fifo_init(port);
  965. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  966. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  967. return 0;
  968. }
  969. static void
  970. mpc52xx_uart_shutdown(struct uart_port *port)
  971. {
  972. /* Shut down the port. Leave TX active if on a console port */
  973. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  974. if (!uart_console(port))
  975. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  976. port->read_status_mask = 0;
  977. psc_ops->set_imr(port, port->read_status_mask);
  978. if (psc_ops->clock)
  979. psc_ops->clock(port, 0);
  980. /* Disable interrupt */
  981. psc_ops->cw_disable_ints(port);
  982. /* Release interrupt */
  983. free_irq(port->irq, port);
  984. }
  985. static void
  986. mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
  987. struct ktermios *old)
  988. {
  989. unsigned long flags;
  990. unsigned char mr1, mr2;
  991. unsigned int j;
  992. unsigned int baud;
  993. /* Prepare what we're gonna write */
  994. mr1 = 0;
  995. switch (new->c_cflag & CSIZE) {
  996. case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
  997. break;
  998. case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
  999. break;
  1000. case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
  1001. break;
  1002. case CS8:
  1003. default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
  1004. }
  1005. if (new->c_cflag & PARENB) {
  1006. if (new->c_cflag & CMSPAR)
  1007. mr1 |= MPC52xx_PSC_MODE_PARFORCE;
  1008. /* With CMSPAR, PARODD also means high parity (same as termios) */
  1009. mr1 |= (new->c_cflag & PARODD) ?
  1010. MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
  1011. } else {
  1012. mr1 |= MPC52xx_PSC_MODE_PARNONE;
  1013. }
  1014. mr2 = 0;
  1015. if (new->c_cflag & CSTOPB)
  1016. mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
  1017. else
  1018. mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
  1019. MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
  1020. MPC52xx_PSC_MODE_ONE_STOP;
  1021. if (new->c_cflag & CRTSCTS) {
  1022. mr1 |= MPC52xx_PSC_MODE_RXRTS;
  1023. mr2 |= MPC52xx_PSC_MODE_TXCTS;
  1024. }
  1025. /* Get the lock */
  1026. spin_lock_irqsave(&port->lock, flags);
  1027. /* Do our best to flush TX & RX, so we don't lose anything */
  1028. /* But we don't wait indefinitely ! */
  1029. j = 5000000; /* Maximum wait */
  1030. /* FIXME Can't receive chars since set_termios might be called at early
  1031. * boot for the console, all stuff is not yet ready to receive at that
  1032. * time and that just makes the kernel oops */
  1033. /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
  1034. while (!mpc52xx_uart_tx_empty(port) && --j)
  1035. udelay(1);
  1036. if (!j)
  1037. printk(KERN_ERR "mpc52xx_uart.c: "
  1038. "Unable to flush RX & TX fifos in-time in set_termios."
  1039. "Some chars may have been lost.\n");
  1040. /* Reset the TX & RX */
  1041. psc_ops->command(port, MPC52xx_PSC_RST_RX);
  1042. psc_ops->command(port, MPC52xx_PSC_RST_TX);
  1043. /* Send new mode settings */
  1044. psc_ops->set_mode(port, mr1, mr2);
  1045. baud = psc_ops->set_baudrate(port, new, old);
  1046. /* Update the per-port timeout */
  1047. uart_update_timeout(port, new->c_cflag, baud);
  1048. if (UART_ENABLE_MS(port, new->c_cflag))
  1049. mpc52xx_uart_enable_ms(port);
  1050. /* Reenable TX & RX */
  1051. psc_ops->command(port, MPC52xx_PSC_TX_ENABLE);
  1052. psc_ops->command(port, MPC52xx_PSC_RX_ENABLE);
  1053. /* We're all set, release the lock */
  1054. spin_unlock_irqrestore(&port->lock, flags);
  1055. }
  1056. static const char *
  1057. mpc52xx_uart_type(struct uart_port *port)
  1058. {
  1059. /*
  1060. * We keep using PORT_MPC52xx for historic reasons although it applies
  1061. * for MPC512x, too, but print "MPC5xxx" to not irritate users
  1062. */
  1063. return port->type == PORT_MPC52xx ? "MPC5xxx PSC" : NULL;
  1064. }
  1065. static void
  1066. mpc52xx_uart_release_port(struct uart_port *port)
  1067. {
  1068. if (psc_ops->clock_relse)
  1069. psc_ops->clock_relse(port);
  1070. /* remapped by us ? */
  1071. if (port->flags & UPF_IOREMAP) {
  1072. iounmap(port->membase);
  1073. port->membase = NULL;
  1074. }
  1075. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1076. }
  1077. static int
  1078. mpc52xx_uart_request_port(struct uart_port *port)
  1079. {
  1080. int err;
  1081. if (port->flags & UPF_IOREMAP) /* Need to remap ? */
  1082. port->membase = ioremap(port->mapbase,
  1083. sizeof(struct mpc52xx_psc));
  1084. if (!port->membase)
  1085. return -EINVAL;
  1086. err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
  1087. "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
  1088. if (err)
  1089. goto out_membase;
  1090. if (psc_ops->clock_alloc) {
  1091. err = psc_ops->clock_alloc(port);
  1092. if (err)
  1093. goto out_mapregion;
  1094. }
  1095. return 0;
  1096. out_mapregion:
  1097. release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
  1098. out_membase:
  1099. if (port->flags & UPF_IOREMAP) {
  1100. iounmap(port->membase);
  1101. port->membase = NULL;
  1102. }
  1103. return err;
  1104. }
  1105. static void
  1106. mpc52xx_uart_config_port(struct uart_port *port, int flags)
  1107. {
  1108. if ((flags & UART_CONFIG_TYPE)
  1109. && (mpc52xx_uart_request_port(port) == 0))
  1110. port->type = PORT_MPC52xx;
  1111. }
  1112. static int
  1113. mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1114. {
  1115. if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
  1116. return -EINVAL;
  1117. if ((ser->irq != port->irq) ||
  1118. (ser->io_type != UPIO_MEM) ||
  1119. (ser->baud_base != port->uartclk) ||
  1120. (ser->iomem_base != (void *)port->mapbase) ||
  1121. (ser->hub6 != 0))
  1122. return -EINVAL;
  1123. return 0;
  1124. }
  1125. static const struct uart_ops mpc52xx_uart_ops = {
  1126. .tx_empty = mpc52xx_uart_tx_empty,
  1127. .set_mctrl = mpc52xx_uart_set_mctrl,
  1128. .get_mctrl = mpc52xx_uart_get_mctrl,
  1129. .stop_tx = mpc52xx_uart_stop_tx,
  1130. .start_tx = mpc52xx_uart_start_tx,
  1131. .stop_rx = mpc52xx_uart_stop_rx,
  1132. .enable_ms = mpc52xx_uart_enable_ms,
  1133. .break_ctl = mpc52xx_uart_break_ctl,
  1134. .startup = mpc52xx_uart_startup,
  1135. .shutdown = mpc52xx_uart_shutdown,
  1136. .set_termios = mpc52xx_uart_set_termios,
  1137. /* .pm = mpc52xx_uart_pm, Not supported yet */
  1138. .type = mpc52xx_uart_type,
  1139. .release_port = mpc52xx_uart_release_port,
  1140. .request_port = mpc52xx_uart_request_port,
  1141. .config_port = mpc52xx_uart_config_port,
  1142. .verify_port = mpc52xx_uart_verify_port
  1143. };
  1144. /* ======================================================================== */
  1145. /* Interrupt handling */
  1146. /* ======================================================================== */
  1147. static inline int
  1148. mpc52xx_uart_int_rx_chars(struct uart_port *port)
  1149. {
  1150. struct tty_port *tport = &port->state->port;
  1151. unsigned char ch, flag;
  1152. unsigned short status;
  1153. /* While we can read, do so ! */
  1154. while (psc_ops->raw_rx_rdy(port)) {
  1155. /* Get the char */
  1156. ch = psc_ops->read_char(port);
  1157. /* Handle sysreq char */
  1158. #ifdef SUPPORT_SYSRQ
  1159. if (uart_handle_sysrq_char(port, ch)) {
  1160. port->sysrq = 0;
  1161. continue;
  1162. }
  1163. #endif
  1164. /* Store it */
  1165. flag = TTY_NORMAL;
  1166. port->icount.rx++;
  1167. status = psc_ops->get_status(port);
  1168. if (status & (MPC52xx_PSC_SR_PE |
  1169. MPC52xx_PSC_SR_FE |
  1170. MPC52xx_PSC_SR_RB)) {
  1171. if (status & MPC52xx_PSC_SR_RB) {
  1172. flag = TTY_BREAK;
  1173. uart_handle_break(port);
  1174. port->icount.brk++;
  1175. } else if (status & MPC52xx_PSC_SR_PE) {
  1176. flag = TTY_PARITY;
  1177. port->icount.parity++;
  1178. }
  1179. else if (status & MPC52xx_PSC_SR_FE) {
  1180. flag = TTY_FRAME;
  1181. port->icount.frame++;
  1182. }
  1183. /* Clear error condition */
  1184. psc_ops->command(port, MPC52xx_PSC_RST_ERR_STAT);
  1185. }
  1186. tty_insert_flip_char(tport, ch, flag);
  1187. if (status & MPC52xx_PSC_SR_OE) {
  1188. /*
  1189. * Overrun is special, since it's
  1190. * reported immediately, and doesn't
  1191. * affect the current character
  1192. */
  1193. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  1194. port->icount.overrun++;
  1195. }
  1196. }
  1197. spin_unlock(&port->lock);
  1198. tty_flip_buffer_push(tport);
  1199. spin_lock(&port->lock);
  1200. return psc_ops->raw_rx_rdy(port);
  1201. }
  1202. static inline int
  1203. mpc52xx_uart_int_tx_chars(struct uart_port *port)
  1204. {
  1205. struct circ_buf *xmit = &port->state->xmit;
  1206. /* Process out of band chars */
  1207. if (port->x_char) {
  1208. psc_ops->write_char(port, port->x_char);
  1209. port->icount.tx++;
  1210. port->x_char = 0;
  1211. return 1;
  1212. }
  1213. /* Nothing to do ? */
  1214. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  1215. mpc52xx_uart_stop_tx(port);
  1216. return 0;
  1217. }
  1218. /* Send chars */
  1219. while (psc_ops->raw_tx_rdy(port)) {
  1220. psc_ops->write_char(port, xmit->buf[xmit->tail]);
  1221. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1222. port->icount.tx++;
  1223. if (uart_circ_empty(xmit))
  1224. break;
  1225. }
  1226. /* Wake up */
  1227. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1228. uart_write_wakeup(port);
  1229. /* Maybe we're done after all */
  1230. if (uart_circ_empty(xmit)) {
  1231. mpc52xx_uart_stop_tx(port);
  1232. return 0;
  1233. }
  1234. return 1;
  1235. }
  1236. static irqreturn_t
  1237. mpc5xxx_uart_process_int(struct uart_port *port)
  1238. {
  1239. unsigned long pass = ISR_PASS_LIMIT;
  1240. unsigned int keepgoing;
  1241. u8 status;
  1242. /* While we have stuff to do, we continue */
  1243. do {
  1244. /* If we don't find anything to do, we stop */
  1245. keepgoing = 0;
  1246. psc_ops->rx_clr_irq(port);
  1247. if (psc_ops->rx_rdy(port))
  1248. keepgoing |= mpc52xx_uart_int_rx_chars(port);
  1249. psc_ops->tx_clr_irq(port);
  1250. if (psc_ops->tx_rdy(port))
  1251. keepgoing |= mpc52xx_uart_int_tx_chars(port);
  1252. status = psc_ops->get_ipcr(port);
  1253. if (status & MPC52xx_PSC_D_DCD)
  1254. uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
  1255. if (status & MPC52xx_PSC_D_CTS)
  1256. uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
  1257. /* Limit number of iteration */
  1258. if (!(--pass))
  1259. keepgoing = 0;
  1260. } while (keepgoing);
  1261. return IRQ_HANDLED;
  1262. }
  1263. static irqreturn_t
  1264. mpc52xx_uart_int(int irq, void *dev_id)
  1265. {
  1266. struct uart_port *port = dev_id;
  1267. irqreturn_t ret;
  1268. spin_lock(&port->lock);
  1269. ret = psc_ops->handle_irq(port);
  1270. spin_unlock(&port->lock);
  1271. return ret;
  1272. }
  1273. /* ======================================================================== */
  1274. /* Console ( if applicable ) */
  1275. /* ======================================================================== */
  1276. #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
  1277. static void __init
  1278. mpc52xx_console_get_options(struct uart_port *port,
  1279. int *baud, int *parity, int *bits, int *flow)
  1280. {
  1281. unsigned char mr1;
  1282. pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
  1283. /* Read the mode registers */
  1284. mr1 = psc_ops->get_mr1(port);
  1285. /* CT{U,L}R are write-only ! */
  1286. *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1287. /* Parse them */
  1288. switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
  1289. case MPC52xx_PSC_MODE_5_BITS:
  1290. *bits = 5;
  1291. break;
  1292. case MPC52xx_PSC_MODE_6_BITS:
  1293. *bits = 6;
  1294. break;
  1295. case MPC52xx_PSC_MODE_7_BITS:
  1296. *bits = 7;
  1297. break;
  1298. case MPC52xx_PSC_MODE_8_BITS:
  1299. default:
  1300. *bits = 8;
  1301. }
  1302. if (mr1 & MPC52xx_PSC_MODE_PARNONE)
  1303. *parity = 'n';
  1304. else
  1305. *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
  1306. }
  1307. static void
  1308. mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
  1309. {
  1310. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1311. unsigned int i, j;
  1312. /* Disable interrupts */
  1313. psc_ops->cw_disable_ints(port);
  1314. /* Wait the TX buffer to be empty */
  1315. j = 5000000; /* Maximum wait */
  1316. while (!mpc52xx_uart_tx_empty(port) && --j)
  1317. udelay(1);
  1318. /* Write all the chars */
  1319. for (i = 0; i < count; i++, s++) {
  1320. /* Line return handling */
  1321. if (*s == '\n')
  1322. psc_ops->write_char(port, '\r');
  1323. /* Send the char */
  1324. psc_ops->write_char(port, *s);
  1325. /* Wait the TX buffer to be empty */
  1326. j = 20000; /* Maximum wait */
  1327. while (!mpc52xx_uart_tx_empty(port) && --j)
  1328. udelay(1);
  1329. }
  1330. /* Restore interrupt state */
  1331. psc_ops->cw_restore_ints(port);
  1332. }
  1333. static int __init
  1334. mpc52xx_console_setup(struct console *co, char *options)
  1335. {
  1336. struct uart_port *port = &mpc52xx_uart_ports[co->index];
  1337. struct device_node *np = mpc52xx_uart_nodes[co->index];
  1338. unsigned int uartclk;
  1339. struct resource res;
  1340. int ret;
  1341. int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
  1342. int bits = 8;
  1343. int parity = 'n';
  1344. int flow = 'n';
  1345. pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
  1346. co, co->index, options);
  1347. if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
  1348. pr_debug("PSC%x out of range\n", co->index);
  1349. return -EINVAL;
  1350. }
  1351. if (!np) {
  1352. pr_debug("PSC%x not found in device tree\n", co->index);
  1353. return -EINVAL;
  1354. }
  1355. pr_debug("Console on ttyPSC%x is %pOF\n",
  1356. co->index, mpc52xx_uart_nodes[co->index]);
  1357. /* Fetch register locations */
  1358. ret = of_address_to_resource(np, 0, &res);
  1359. if (ret) {
  1360. pr_debug("Could not get resources for PSC%x\n", co->index);
  1361. return ret;
  1362. }
  1363. uartclk = mpc5xxx_get_bus_frequency(np);
  1364. if (uartclk == 0) {
  1365. pr_debug("Could not find uart clock frequency!\n");
  1366. return -EINVAL;
  1367. }
  1368. /* Basic port init. Needed since we use some uart_??? func before
  1369. * real init for early access */
  1370. spin_lock_init(&port->lock);
  1371. port->uartclk = uartclk;
  1372. port->ops = &mpc52xx_uart_ops;
  1373. port->mapbase = res.start;
  1374. port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
  1375. port->irq = irq_of_parse_and_map(np, 0);
  1376. if (port->membase == NULL)
  1377. return -EINVAL;
  1378. pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
  1379. (void *)port->mapbase, port->membase,
  1380. port->irq, port->uartclk);
  1381. /* Setup the port parameters accoding to options */
  1382. if (options)
  1383. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1384. else
  1385. mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
  1386. pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
  1387. baud, bits, parity, flow);
  1388. return uart_set_options(port, co, baud, parity, bits, flow);
  1389. }
  1390. static struct uart_driver mpc52xx_uart_driver;
  1391. static struct console mpc52xx_console = {
  1392. .name = "ttyPSC",
  1393. .write = mpc52xx_console_write,
  1394. .device = uart_console_device,
  1395. .setup = mpc52xx_console_setup,
  1396. .flags = CON_PRINTBUFFER,
  1397. .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
  1398. .data = &mpc52xx_uart_driver,
  1399. };
  1400. static int __init
  1401. mpc52xx_console_init(void)
  1402. {
  1403. mpc52xx_uart_of_enumerate();
  1404. register_console(&mpc52xx_console);
  1405. return 0;
  1406. }
  1407. console_initcall(mpc52xx_console_init);
  1408. #define MPC52xx_PSC_CONSOLE &mpc52xx_console
  1409. #else
  1410. #define MPC52xx_PSC_CONSOLE NULL
  1411. #endif
  1412. /* ======================================================================== */
  1413. /* UART Driver */
  1414. /* ======================================================================== */
  1415. static struct uart_driver mpc52xx_uart_driver = {
  1416. .driver_name = "mpc52xx_psc_uart",
  1417. .dev_name = "ttyPSC",
  1418. .major = SERIAL_PSC_MAJOR,
  1419. .minor = SERIAL_PSC_MINOR,
  1420. .nr = MPC52xx_PSC_MAXNUM,
  1421. .cons = MPC52xx_PSC_CONSOLE,
  1422. };
  1423. /* ======================================================================== */
  1424. /* OF Platform Driver */
  1425. /* ======================================================================== */
  1426. static const struct of_device_id mpc52xx_uart_of_match[] = {
  1427. #ifdef CONFIG_PPC_MPC52xx
  1428. { .compatible = "fsl,mpc5200b-psc-uart", .data = &mpc5200b_psc_ops, },
  1429. { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1430. /* binding used by old lite5200 device trees: */
  1431. { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
  1432. /* binding used by efika: */
  1433. { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
  1434. #endif
  1435. #ifdef CONFIG_PPC_MPC512x
  1436. { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
  1437. { .compatible = "fsl,mpc5125-psc-uart", .data = &mpc5125_psc_ops, },
  1438. #endif
  1439. {},
  1440. };
  1441. static int mpc52xx_uart_of_probe(struct platform_device *op)
  1442. {
  1443. int idx = -1;
  1444. unsigned int uartclk;
  1445. struct uart_port *port = NULL;
  1446. struct resource res;
  1447. int ret;
  1448. /* Check validity & presence */
  1449. for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
  1450. if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
  1451. break;
  1452. if (idx >= MPC52xx_PSC_MAXNUM)
  1453. return -EINVAL;
  1454. pr_debug("Found %pOF assigned to ttyPSC%x\n",
  1455. mpc52xx_uart_nodes[idx], idx);
  1456. /* set the uart clock to the input clock of the psc, the different
  1457. * prescalers are taken into account in the set_baudrate() methods
  1458. * of the respective chip */
  1459. uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
  1460. if (uartclk == 0) {
  1461. dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
  1462. return -EINVAL;
  1463. }
  1464. /* Init the port structure */
  1465. port = &mpc52xx_uart_ports[idx];
  1466. spin_lock_init(&port->lock);
  1467. port->uartclk = uartclk;
  1468. port->fifosize = 512;
  1469. port->iotype = UPIO_MEM;
  1470. port->flags = UPF_BOOT_AUTOCONF |
  1471. (uart_console(port) ? 0 : UPF_IOREMAP);
  1472. port->line = idx;
  1473. port->ops = &mpc52xx_uart_ops;
  1474. port->dev = &op->dev;
  1475. /* Search for IRQ and mapbase */
  1476. ret = of_address_to_resource(op->dev.of_node, 0, &res);
  1477. if (ret)
  1478. return ret;
  1479. port->mapbase = res.start;
  1480. if (!port->mapbase) {
  1481. dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
  1482. return -EINVAL;
  1483. }
  1484. psc_ops->get_irq(port, op->dev.of_node);
  1485. if (port->irq == 0) {
  1486. dev_dbg(&op->dev, "Could not get irq\n");
  1487. return -EINVAL;
  1488. }
  1489. dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
  1490. (void *)port->mapbase, port->irq, port->uartclk);
  1491. /* Add the port to the uart sub-system */
  1492. ret = uart_add_one_port(&mpc52xx_uart_driver, port);
  1493. if (ret)
  1494. return ret;
  1495. platform_set_drvdata(op, (void *)port);
  1496. return 0;
  1497. }
  1498. static int
  1499. mpc52xx_uart_of_remove(struct platform_device *op)
  1500. {
  1501. struct uart_port *port = platform_get_drvdata(op);
  1502. if (port)
  1503. uart_remove_one_port(&mpc52xx_uart_driver, port);
  1504. return 0;
  1505. }
  1506. #ifdef CONFIG_PM
  1507. static int
  1508. mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
  1509. {
  1510. struct uart_port *port = platform_get_drvdata(op);
  1511. if (port)
  1512. uart_suspend_port(&mpc52xx_uart_driver, port);
  1513. return 0;
  1514. }
  1515. static int
  1516. mpc52xx_uart_of_resume(struct platform_device *op)
  1517. {
  1518. struct uart_port *port = platform_get_drvdata(op);
  1519. if (port)
  1520. uart_resume_port(&mpc52xx_uart_driver, port);
  1521. return 0;
  1522. }
  1523. #endif
  1524. static void
  1525. mpc52xx_uart_of_assign(struct device_node *np)
  1526. {
  1527. int i;
  1528. /* Find the first free PSC number */
  1529. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1530. if (mpc52xx_uart_nodes[i] == NULL) {
  1531. of_node_get(np);
  1532. mpc52xx_uart_nodes[i] = np;
  1533. return;
  1534. }
  1535. }
  1536. }
  1537. static void
  1538. mpc52xx_uart_of_enumerate(void)
  1539. {
  1540. static int enum_done;
  1541. struct device_node *np;
  1542. const struct of_device_id *match;
  1543. int i;
  1544. if (enum_done)
  1545. return;
  1546. /* Assign index to each PSC in device tree */
  1547. for_each_matching_node(np, mpc52xx_uart_of_match) {
  1548. match = of_match_node(mpc52xx_uart_of_match, np);
  1549. psc_ops = match->data;
  1550. mpc52xx_uart_of_assign(np);
  1551. }
  1552. enum_done = 1;
  1553. for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
  1554. if (mpc52xx_uart_nodes[i])
  1555. pr_debug("%pOF assigned to ttyPSC%x\n",
  1556. mpc52xx_uart_nodes[i], i);
  1557. }
  1558. }
  1559. MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
  1560. static struct platform_driver mpc52xx_uart_of_driver = {
  1561. .probe = mpc52xx_uart_of_probe,
  1562. .remove = mpc52xx_uart_of_remove,
  1563. #ifdef CONFIG_PM
  1564. .suspend = mpc52xx_uart_of_suspend,
  1565. .resume = mpc52xx_uart_of_resume,
  1566. #endif
  1567. .driver = {
  1568. .name = "mpc52xx-psc-uart",
  1569. .of_match_table = mpc52xx_uart_of_match,
  1570. },
  1571. };
  1572. /* ======================================================================== */
  1573. /* Module */
  1574. /* ======================================================================== */
  1575. static int __init
  1576. mpc52xx_uart_init(void)
  1577. {
  1578. int ret;
  1579. printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
  1580. ret = uart_register_driver(&mpc52xx_uart_driver);
  1581. if (ret) {
  1582. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  1583. __FILE__, ret);
  1584. return ret;
  1585. }
  1586. mpc52xx_uart_of_enumerate();
  1587. /*
  1588. * Map the PSC FIFO Controller and init if on MPC512x.
  1589. */
  1590. if (psc_ops && psc_ops->fifoc_init) {
  1591. ret = psc_ops->fifoc_init();
  1592. if (ret)
  1593. goto err_init;
  1594. }
  1595. ret = platform_driver_register(&mpc52xx_uart_of_driver);
  1596. if (ret) {
  1597. printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
  1598. __FILE__, ret);
  1599. goto err_reg;
  1600. }
  1601. return 0;
  1602. err_reg:
  1603. if (psc_ops && psc_ops->fifoc_uninit)
  1604. psc_ops->fifoc_uninit();
  1605. err_init:
  1606. uart_unregister_driver(&mpc52xx_uart_driver);
  1607. return ret;
  1608. }
  1609. static void __exit
  1610. mpc52xx_uart_exit(void)
  1611. {
  1612. if (psc_ops->fifoc_uninit)
  1613. psc_ops->fifoc_uninit();
  1614. platform_driver_unregister(&mpc52xx_uart_of_driver);
  1615. uart_unregister_driver(&mpc52xx_uart_driver);
  1616. }
  1617. module_init(mpc52xx_uart_init);
  1618. module_exit(mpc52xx_uart_exit);
  1619. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  1620. MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
  1621. MODULE_LICENSE("GPL");