mpc52xx_uart.c 50 KB

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