mpc52xx_uart.c 39 KB

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