xilinx_uartps.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * Cadence UART driver (found in Xilinx Zynq)
  3. *
  4. * 2011 - 2014 (C) Xilinx Inc.
  5. *
  6. * This program is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * either version 2 of the License, or (at your option) any
  10. * later version.
  11. *
  12. * This driver has originally been pushed by Xilinx using a Zynq-branding. This
  13. * still shows in the naming of this file, the kconfig symbols and some symbols
  14. * in the code.
  15. */
  16. #if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  17. #define SUPPORT_SYSRQ
  18. #endif
  19. #include <linux/platform_device.h>
  20. #include <linux/serial.h>
  21. #include <linux/console.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/clk.h>
  27. #include <linux/irq.h>
  28. #include <linux/io.h>
  29. #include <linux/of.h>
  30. #include <linux/module.h>
  31. #define CDNS_UART_TTY_NAME "ttyPS"
  32. #define CDNS_UART_NAME "xuartps"
  33. #define CDNS_UART_MAJOR 0 /* use dynamic node allocation */
  34. #define CDNS_UART_MINOR 0 /* works best with devtmpfs */
  35. #define CDNS_UART_NR_PORTS 2
  36. #define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
  37. #define CDNS_UART_REGISTER_SPACE 0x1000
  38. /* Rx Trigger level */
  39. static int rx_trigger_level = 56;
  40. module_param(rx_trigger_level, uint, S_IRUGO);
  41. MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
  42. /* Rx Timeout */
  43. static int rx_timeout = 10;
  44. module_param(rx_timeout, uint, S_IRUGO);
  45. MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
  46. /* Register offsets for the UART. */
  47. #define CDNS_UART_CR 0x00 /* Control Register */
  48. #define CDNS_UART_MR 0x04 /* Mode Register */
  49. #define CDNS_UART_IER 0x08 /* Interrupt Enable */
  50. #define CDNS_UART_IDR 0x0C /* Interrupt Disable */
  51. #define CDNS_UART_IMR 0x10 /* Interrupt Mask */
  52. #define CDNS_UART_ISR 0x14 /* Interrupt Status */
  53. #define CDNS_UART_BAUDGEN 0x18 /* Baud Rate Generator */
  54. #define CDNS_UART_RXTOUT 0x1C /* RX Timeout */
  55. #define CDNS_UART_RXWM 0x20 /* RX FIFO Trigger Level */
  56. #define CDNS_UART_MODEMCR 0x24 /* Modem Control */
  57. #define CDNS_UART_MODEMSR 0x28 /* Modem Status */
  58. #define CDNS_UART_SR 0x2C /* Channel Status */
  59. #define CDNS_UART_FIFO 0x30 /* FIFO */
  60. #define CDNS_UART_BAUDDIV 0x34 /* Baud Rate Divider */
  61. #define CDNS_UART_FLOWDEL 0x38 /* Flow Delay */
  62. #define CDNS_UART_IRRX_PWIDTH 0x3C /* IR Min Received Pulse Width */
  63. #define CDNS_UART_IRTX_PWIDTH 0x40 /* IR Transmitted pulse Width */
  64. #define CDNS_UART_TXWM 0x44 /* TX FIFO Trigger Level */
  65. /* Control Register Bit Definitions */
  66. #define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
  67. #define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
  68. #define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
  69. #define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
  70. #define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
  71. #define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
  72. #define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
  73. #define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
  74. #define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
  75. /*
  76. * Mode Register:
  77. * The mode register (MR) defines the mode of transfer as well as the data
  78. * format. If this register is modified during transmission or reception,
  79. * data validity cannot be guaranteed.
  80. */
  81. #define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
  82. #define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
  83. #define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
  84. #define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
  85. #define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
  86. #define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
  87. #define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
  88. #define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
  89. #define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
  90. #define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
  91. #define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
  92. #define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
  93. #define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
  94. /*
  95. * Interrupt Registers:
  96. * Interrupt control logic uses the interrupt enable register (IER) and the
  97. * interrupt disable register (IDR) to set the value of the bits in the
  98. * interrupt mask register (IMR). The IMR determines whether to pass an
  99. * interrupt to the interrupt status register (ISR).
  100. * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
  101. * interrupt. IMR and ISR are read only, and IER and IDR are write only.
  102. * Reading either IER or IDR returns 0x00.
  103. * All four registers have the same bit definitions.
  104. */
  105. #define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
  106. #define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
  107. #define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
  108. #define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
  109. #define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
  110. #define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
  111. #define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
  112. #define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
  113. #define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
  114. #define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
  115. #define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
  116. #define CDNS_UART_RX_IRQS (CDNS_UART_IXR_PARITY | CDNS_UART_IXR_FRAMING | \
  117. CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_RXTRIG | \
  118. CDNS_UART_IXR_TOUT)
  119. /* Goes in read_status_mask for break detection as the HW doesn't do it*/
  120. #define CDNS_UART_IXR_BRK 0x80000000
  121. /*
  122. * Modem Control register:
  123. * The read/write Modem Control register controls the interface with the modem
  124. * or data set, or a peripheral device emulating a modem.
  125. */
  126. #define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
  127. #define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
  128. #define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
  129. /*
  130. * Channel Status Register:
  131. * The channel status register (CSR) is provided to enable the control logic
  132. * to monitor the status of bits in the channel interrupt status register,
  133. * even if these are masked out by the interrupt mask register.
  134. */
  135. #define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  136. #define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
  137. #define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
  138. #define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
  139. /* baud dividers min/max values */
  140. #define CDNS_UART_BDIV_MIN 4
  141. #define CDNS_UART_BDIV_MAX 255
  142. #define CDNS_UART_CD_MAX 65535
  143. /**
  144. * struct cdns_uart - device data
  145. * @port: Pointer to the UART port
  146. * @uartclk: Reference clock
  147. * @pclk: APB clock
  148. * @baud: Current baud rate
  149. * @clk_rate_change_nb: Notifier block for clock changes
  150. */
  151. struct cdns_uart {
  152. struct uart_port *port;
  153. struct clk *uartclk;
  154. struct clk *pclk;
  155. unsigned int baud;
  156. struct notifier_block clk_rate_change_nb;
  157. };
  158. #define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
  159. clk_rate_change_nb);
  160. static void cdns_uart_handle_rx(struct uart_port *port, unsigned int isrstatus)
  161. {
  162. /*
  163. * There is no hardware break detection, so we interpret framing
  164. * error with all-zeros data as a break sequence. Most of the time,
  165. * there's another non-zero byte at the end of the sequence.
  166. */
  167. if (isrstatus & CDNS_UART_IXR_FRAMING) {
  168. while (!(readl(port->membase + CDNS_UART_SR) &
  169. CDNS_UART_SR_RXEMPTY)) {
  170. if (!readl(port->membase + CDNS_UART_FIFO)) {
  171. port->read_status_mask |= CDNS_UART_IXR_BRK;
  172. isrstatus &= ~CDNS_UART_IXR_FRAMING;
  173. }
  174. }
  175. writel(CDNS_UART_IXR_FRAMING, port->membase + CDNS_UART_ISR);
  176. }
  177. /* drop byte with parity error if IGNPAR specified */
  178. if (isrstatus & port->ignore_status_mask & CDNS_UART_IXR_PARITY)
  179. isrstatus &= ~(CDNS_UART_IXR_RXTRIG | CDNS_UART_IXR_TOUT);
  180. isrstatus &= port->read_status_mask;
  181. isrstatus &= ~port->ignore_status_mask;
  182. if (!(isrstatus & (CDNS_UART_IXR_TOUT | CDNS_UART_IXR_RXTRIG)))
  183. return;
  184. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)) {
  185. u32 data;
  186. char status = TTY_NORMAL;
  187. data = readl(port->membase + CDNS_UART_FIFO);
  188. /* Non-NULL byte after BREAK is garbage (99%) */
  189. if (data && (port->read_status_mask & CDNS_UART_IXR_BRK)) {
  190. port->read_status_mask &= ~CDNS_UART_IXR_BRK;
  191. port->icount.brk++;
  192. if (uart_handle_break(port))
  193. continue;
  194. }
  195. if (uart_handle_sysrq_char(port, data))
  196. continue;
  197. port->icount.rx++;
  198. if (isrstatus & CDNS_UART_IXR_PARITY) {
  199. port->icount.parity++;
  200. status = TTY_PARITY;
  201. } else if (isrstatus & CDNS_UART_IXR_FRAMING) {
  202. port->icount.frame++;
  203. status = TTY_FRAME;
  204. } else if (isrstatus & CDNS_UART_IXR_OVERRUN) {
  205. port->icount.overrun++;
  206. }
  207. uart_insert_char(port, isrstatus, CDNS_UART_IXR_OVERRUN,
  208. data, status);
  209. }
  210. tty_flip_buffer_push(&port->state->port);
  211. }
  212. static void cdns_uart_handle_tx(struct uart_port *port)
  213. {
  214. unsigned int numbytes;
  215. if (uart_circ_empty(&port->state->xmit)) {
  216. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
  217. return;
  218. }
  219. numbytes = port->fifosize;
  220. while (numbytes && !uart_circ_empty(&port->state->xmit) &&
  221. !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)) {
  222. /*
  223. * Get the data from the UART circular buffer
  224. * and write it to the cdns_uart's TX_FIFO
  225. * register.
  226. */
  227. writel(port->state->xmit.buf[port->state->xmit.tail],
  228. port->membase + CDNS_UART_FIFO);
  229. port->icount.tx++;
  230. /*
  231. * Adjust the tail of the UART buffer and wrap
  232. * the buffer if it reaches limit.
  233. */
  234. port->state->xmit.tail =
  235. (port->state->xmit.tail + 1) & (UART_XMIT_SIZE - 1);
  236. numbytes--;
  237. }
  238. if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
  239. uart_write_wakeup(port);
  240. }
  241. /**
  242. * cdns_uart_isr - Interrupt handler
  243. * @irq: Irq number
  244. * @dev_id: Id of the port
  245. *
  246. * Return: IRQHANDLED
  247. */
  248. static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
  249. {
  250. struct uart_port *port = (struct uart_port *)dev_id;
  251. unsigned long flags;
  252. unsigned int isrstatus;
  253. spin_lock_irqsave(&port->lock, flags);
  254. /* Read the interrupt status register to determine which
  255. * interrupt(s) is/are active.
  256. */
  257. isrstatus = readl(port->membase + CDNS_UART_ISR);
  258. if (isrstatus & CDNS_UART_RX_IRQS)
  259. cdns_uart_handle_rx(port, isrstatus);
  260. if ((isrstatus & CDNS_UART_IXR_TXEMPTY) == CDNS_UART_IXR_TXEMPTY)
  261. cdns_uart_handle_tx(port);
  262. writel(isrstatus, port->membase + CDNS_UART_ISR);
  263. /* be sure to release the lock and tty before leaving */
  264. spin_unlock_irqrestore(&port->lock, flags);
  265. return IRQ_HANDLED;
  266. }
  267. /**
  268. * cdns_uart_calc_baud_divs - Calculate baud rate divisors
  269. * @clk: UART module input clock
  270. * @baud: Desired baud rate
  271. * @rbdiv: BDIV value (return value)
  272. * @rcd: CD value (return value)
  273. * @div8: Value for clk_sel bit in mod (return value)
  274. * Return: baud rate, requested baud when possible, or actual baud when there
  275. * was too much error, zero if no valid divisors are found.
  276. *
  277. * Formula to obtain baud rate is
  278. * baud_tx/rx rate = clk/CD * (BDIV + 1)
  279. * input_clk = (Uart User Defined Clock or Apb Clock)
  280. * depends on UCLKEN in MR Reg
  281. * clk = input_clk or input_clk/8;
  282. * depends on CLKS in MR reg
  283. * CD and BDIV depends on values in
  284. * baud rate generate register
  285. * baud rate clock divisor register
  286. */
  287. static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
  288. unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
  289. {
  290. u32 cd, bdiv;
  291. unsigned int calc_baud;
  292. unsigned int bestbaud = 0;
  293. unsigned int bauderror;
  294. unsigned int besterror = ~0;
  295. if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
  296. *div8 = 1;
  297. clk /= 8;
  298. } else {
  299. *div8 = 0;
  300. }
  301. for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
  302. cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
  303. if (cd < 1 || cd > CDNS_UART_CD_MAX)
  304. continue;
  305. calc_baud = clk / (cd * (bdiv + 1));
  306. if (baud > calc_baud)
  307. bauderror = baud - calc_baud;
  308. else
  309. bauderror = calc_baud - baud;
  310. if (besterror > bauderror) {
  311. *rbdiv = bdiv;
  312. *rcd = cd;
  313. bestbaud = calc_baud;
  314. besterror = bauderror;
  315. }
  316. }
  317. /* use the values when percent error is acceptable */
  318. if (((besterror * 100) / baud) < 3)
  319. bestbaud = baud;
  320. return bestbaud;
  321. }
  322. /**
  323. * cdns_uart_set_baud_rate - Calculate and set the baud rate
  324. * @port: Handle to the uart port structure
  325. * @baud: Baud rate to set
  326. * Return: baud rate, requested baud when possible, or actual baud when there
  327. * was too much error, zero if no valid divisors are found.
  328. */
  329. static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
  330. unsigned int baud)
  331. {
  332. unsigned int calc_baud;
  333. u32 cd = 0, bdiv = 0;
  334. u32 mreg;
  335. int div8;
  336. struct cdns_uart *cdns_uart = port->private_data;
  337. calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
  338. &div8);
  339. /* Write new divisors to hardware */
  340. mreg = readl(port->membase + CDNS_UART_MR);
  341. if (div8)
  342. mreg |= CDNS_UART_MR_CLKSEL;
  343. else
  344. mreg &= ~CDNS_UART_MR_CLKSEL;
  345. writel(mreg, port->membase + CDNS_UART_MR);
  346. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  347. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  348. cdns_uart->baud = baud;
  349. return calc_baud;
  350. }
  351. #ifdef CONFIG_COMMON_CLK
  352. /**
  353. * cdns_uart_clk_notitifer_cb - Clock notifier callback
  354. * @nb: Notifier block
  355. * @event: Notify event
  356. * @data: Notifier data
  357. * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
  358. */
  359. static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
  360. unsigned long event, void *data)
  361. {
  362. u32 ctrl_reg;
  363. struct uart_port *port;
  364. int locked = 0;
  365. struct clk_notifier_data *ndata = data;
  366. unsigned long flags = 0;
  367. struct cdns_uart *cdns_uart = to_cdns_uart(nb);
  368. port = cdns_uart->port;
  369. if (port->suspended)
  370. return NOTIFY_OK;
  371. switch (event) {
  372. case PRE_RATE_CHANGE:
  373. {
  374. u32 bdiv, cd;
  375. int div8;
  376. /*
  377. * Find out if current baud-rate can be achieved with new clock
  378. * frequency.
  379. */
  380. if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
  381. &bdiv, &cd, &div8)) {
  382. dev_warn(port->dev, "clock rate change rejected\n");
  383. return NOTIFY_BAD;
  384. }
  385. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  386. /* Disable the TX and RX to set baud rate */
  387. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  388. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  389. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  390. spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
  391. return NOTIFY_OK;
  392. }
  393. case POST_RATE_CHANGE:
  394. /*
  395. * Set clk dividers to generate correct baud with new clock
  396. * frequency.
  397. */
  398. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  399. locked = 1;
  400. port->uartclk = ndata->new_rate;
  401. cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
  402. cdns_uart->baud);
  403. /* fall through */
  404. case ABORT_RATE_CHANGE:
  405. if (!locked)
  406. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  407. /* Set TX/RX Reset */
  408. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  409. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  410. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  411. while (readl(port->membase + CDNS_UART_CR) &
  412. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  413. cpu_relax();
  414. /*
  415. * Clear the RX disable and TX disable bits and then set the TX
  416. * enable bit and RX enable bit to enable the transmitter and
  417. * receiver.
  418. */
  419. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  420. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  421. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  422. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  423. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  424. spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
  425. return NOTIFY_OK;
  426. default:
  427. return NOTIFY_DONE;
  428. }
  429. }
  430. #endif
  431. /**
  432. * cdns_uart_start_tx - Start transmitting bytes
  433. * @port: Handle to the uart port structure
  434. */
  435. static void cdns_uart_start_tx(struct uart_port *port)
  436. {
  437. unsigned int status;
  438. if (uart_tx_stopped(port))
  439. return;
  440. /*
  441. * Set the TX enable bit and clear the TX disable bit to enable the
  442. * transmitter.
  443. */
  444. status = readl(port->membase + CDNS_UART_CR);
  445. status &= ~CDNS_UART_CR_TX_DIS;
  446. status |= CDNS_UART_CR_TX_EN;
  447. writel(status, port->membase + CDNS_UART_CR);
  448. if (uart_circ_empty(&port->state->xmit))
  449. return;
  450. cdns_uart_handle_tx(port);
  451. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
  452. /* Enable the TX Empty interrupt */
  453. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
  454. }
  455. /**
  456. * cdns_uart_stop_tx - Stop TX
  457. * @port: Handle to the uart port structure
  458. */
  459. static void cdns_uart_stop_tx(struct uart_port *port)
  460. {
  461. unsigned int regval;
  462. regval = readl(port->membase + CDNS_UART_CR);
  463. regval |= CDNS_UART_CR_TX_DIS;
  464. /* Disable the transmitter */
  465. writel(regval, port->membase + CDNS_UART_CR);
  466. }
  467. /**
  468. * cdns_uart_stop_rx - Stop RX
  469. * @port: Handle to the uart port structure
  470. */
  471. static void cdns_uart_stop_rx(struct uart_port *port)
  472. {
  473. unsigned int regval;
  474. /* Disable RX IRQs */
  475. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
  476. /* Disable the receiver */
  477. regval = readl(port->membase + CDNS_UART_CR);
  478. regval |= CDNS_UART_CR_RX_DIS;
  479. writel(regval, port->membase + CDNS_UART_CR);
  480. }
  481. /**
  482. * cdns_uart_tx_empty - Check whether TX is empty
  483. * @port: Handle to the uart port structure
  484. *
  485. * Return: TIOCSER_TEMT on success, 0 otherwise
  486. */
  487. static unsigned int cdns_uart_tx_empty(struct uart_port *port)
  488. {
  489. unsigned int status;
  490. status = readl(port->membase + CDNS_UART_SR) &
  491. CDNS_UART_SR_TXEMPTY;
  492. return status ? TIOCSER_TEMT : 0;
  493. }
  494. /**
  495. * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
  496. * transmitting char breaks
  497. * @port: Handle to the uart port structure
  498. * @ctl: Value based on which start or stop decision is taken
  499. */
  500. static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
  501. {
  502. unsigned int status;
  503. unsigned long flags;
  504. spin_lock_irqsave(&port->lock, flags);
  505. status = readl(port->membase + CDNS_UART_CR);
  506. if (ctl == -1)
  507. writel(CDNS_UART_CR_STARTBRK | status,
  508. port->membase + CDNS_UART_CR);
  509. else {
  510. if ((status & CDNS_UART_CR_STOPBRK) == 0)
  511. writel(CDNS_UART_CR_STOPBRK | status,
  512. port->membase + CDNS_UART_CR);
  513. }
  514. spin_unlock_irqrestore(&port->lock, flags);
  515. }
  516. /**
  517. * cdns_uart_set_termios - termios operations, handling data length, parity,
  518. * stop bits, flow control, baud rate
  519. * @port: Handle to the uart port structure
  520. * @termios: Handle to the input termios structure
  521. * @old: Values of the previously saved termios structure
  522. */
  523. static void cdns_uart_set_termios(struct uart_port *port,
  524. struct ktermios *termios, struct ktermios *old)
  525. {
  526. unsigned int cval = 0;
  527. unsigned int baud, minbaud, maxbaud;
  528. unsigned long flags;
  529. unsigned int ctrl_reg, mode_reg;
  530. spin_lock_irqsave(&port->lock, flags);
  531. /* Wait for the transmit FIFO to empty before making changes */
  532. if (!(readl(port->membase + CDNS_UART_CR) &
  533. CDNS_UART_CR_TX_DIS)) {
  534. while (!(readl(port->membase + CDNS_UART_SR) &
  535. CDNS_UART_SR_TXEMPTY)) {
  536. cpu_relax();
  537. }
  538. }
  539. /* Disable the TX and RX to set baud rate */
  540. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  541. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  542. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  543. /*
  544. * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
  545. * min and max baud should be calculated here based on port->uartclk.
  546. * this way we get a valid baud and can safely call set_baud()
  547. */
  548. minbaud = port->uartclk /
  549. ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
  550. maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
  551. baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
  552. baud = cdns_uart_set_baud_rate(port, baud);
  553. if (tty_termios_baud_rate(termios))
  554. tty_termios_encode_baud_rate(termios, baud, baud);
  555. /* Update the per-port timeout. */
  556. uart_update_timeout(port, termios->c_cflag, baud);
  557. /* Set TX/RX Reset */
  558. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  559. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  560. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  561. /*
  562. * Clear the RX disable and TX disable bits and then set the TX enable
  563. * bit and RX enable bit to enable the transmitter and receiver.
  564. */
  565. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  566. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  567. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  568. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  569. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  570. port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
  571. CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
  572. port->ignore_status_mask = 0;
  573. if (termios->c_iflag & INPCK)
  574. port->read_status_mask |= CDNS_UART_IXR_PARITY |
  575. CDNS_UART_IXR_FRAMING;
  576. if (termios->c_iflag & IGNPAR)
  577. port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
  578. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  579. /* ignore all characters if CREAD is not set */
  580. if ((termios->c_cflag & CREAD) == 0)
  581. port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
  582. CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
  583. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  584. mode_reg = readl(port->membase + CDNS_UART_MR);
  585. /* Handling Data Size */
  586. switch (termios->c_cflag & CSIZE) {
  587. case CS6:
  588. cval |= CDNS_UART_MR_CHARLEN_6_BIT;
  589. break;
  590. case CS7:
  591. cval |= CDNS_UART_MR_CHARLEN_7_BIT;
  592. break;
  593. default:
  594. case CS8:
  595. cval |= CDNS_UART_MR_CHARLEN_8_BIT;
  596. termios->c_cflag &= ~CSIZE;
  597. termios->c_cflag |= CS8;
  598. break;
  599. }
  600. /* Handling Parity and Stop Bits length */
  601. if (termios->c_cflag & CSTOPB)
  602. cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  603. else
  604. cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  605. if (termios->c_cflag & PARENB) {
  606. /* Mark or Space parity */
  607. if (termios->c_cflag & CMSPAR) {
  608. if (termios->c_cflag & PARODD)
  609. cval |= CDNS_UART_MR_PARITY_MARK;
  610. else
  611. cval |= CDNS_UART_MR_PARITY_SPACE;
  612. } else {
  613. if (termios->c_cflag & PARODD)
  614. cval |= CDNS_UART_MR_PARITY_ODD;
  615. else
  616. cval |= CDNS_UART_MR_PARITY_EVEN;
  617. }
  618. } else {
  619. cval |= CDNS_UART_MR_PARITY_NONE;
  620. }
  621. cval |= mode_reg & 1;
  622. writel(cval, port->membase + CDNS_UART_MR);
  623. spin_unlock_irqrestore(&port->lock, flags);
  624. }
  625. /**
  626. * cdns_uart_startup - Called when an application opens a cdns_uart port
  627. * @port: Handle to the uart port structure
  628. *
  629. * Return: 0 on success, negative errno otherwise
  630. */
  631. static int cdns_uart_startup(struct uart_port *port)
  632. {
  633. int ret;
  634. unsigned long flags;
  635. unsigned int status = 0;
  636. spin_lock_irqsave(&port->lock, flags);
  637. /* Disable the TX and RX */
  638. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  639. port->membase + CDNS_UART_CR);
  640. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  641. * no break chars.
  642. */
  643. writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
  644. port->membase + CDNS_UART_CR);
  645. /*
  646. * Clear the RX disable bit and then set the RX enable bit to enable
  647. * the receiver.
  648. */
  649. status = readl(port->membase + CDNS_UART_CR);
  650. status &= CDNS_UART_CR_RX_DIS;
  651. status |= CDNS_UART_CR_RX_EN;
  652. writel(status, port->membase + CDNS_UART_CR);
  653. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  654. * no parity.
  655. */
  656. writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
  657. | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
  658. port->membase + CDNS_UART_MR);
  659. /*
  660. * Set the RX FIFO Trigger level to use most of the FIFO, but it
  661. * can be tuned with a module parameter
  662. */
  663. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  664. /*
  665. * Receive Timeout register is enabled but it
  666. * can be tuned with a module parameter
  667. */
  668. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  669. /* Clear out any pending interrupts before enabling them */
  670. writel(readl(port->membase + CDNS_UART_ISR),
  671. port->membase + CDNS_UART_ISR);
  672. spin_unlock_irqrestore(&port->lock, flags);
  673. ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
  674. if (ret) {
  675. dev_err(port->dev, "request_irq '%d' failed with %d\n",
  676. port->irq, ret);
  677. return ret;
  678. }
  679. /* Set the Interrupt Registers with desired interrupts */
  680. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
  681. return 0;
  682. }
  683. /**
  684. * cdns_uart_shutdown - Called when an application closes a cdns_uart port
  685. * @port: Handle to the uart port structure
  686. */
  687. static void cdns_uart_shutdown(struct uart_port *port)
  688. {
  689. int status;
  690. unsigned long flags;
  691. spin_lock_irqsave(&port->lock, flags);
  692. /* Disable interrupts */
  693. status = readl(port->membase + CDNS_UART_IMR);
  694. writel(status, port->membase + CDNS_UART_IDR);
  695. writel(0xffffffff, port->membase + CDNS_UART_ISR);
  696. /* Disable the TX and RX */
  697. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  698. port->membase + CDNS_UART_CR);
  699. spin_unlock_irqrestore(&port->lock, flags);
  700. free_irq(port->irq, port);
  701. }
  702. /**
  703. * cdns_uart_type - Set UART type to cdns_uart port
  704. * @port: Handle to the uart port structure
  705. *
  706. * Return: string on success, NULL otherwise
  707. */
  708. static const char *cdns_uart_type(struct uart_port *port)
  709. {
  710. return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
  711. }
  712. /**
  713. * cdns_uart_verify_port - Verify the port params
  714. * @port: Handle to the uart port structure
  715. * @ser: Handle to the structure whose members are compared
  716. *
  717. * Return: 0 on success, negative errno otherwise.
  718. */
  719. static int cdns_uart_verify_port(struct uart_port *port,
  720. struct serial_struct *ser)
  721. {
  722. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  723. return -EINVAL;
  724. if (port->irq != ser->irq)
  725. return -EINVAL;
  726. if (ser->io_type != UPIO_MEM)
  727. return -EINVAL;
  728. if (port->iobase != ser->port)
  729. return -EINVAL;
  730. if (ser->hub6 != 0)
  731. return -EINVAL;
  732. return 0;
  733. }
  734. /**
  735. * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
  736. * called when the driver adds a cdns_uart port via
  737. * uart_add_one_port()
  738. * @port: Handle to the uart port structure
  739. *
  740. * Return: 0 on success, negative errno otherwise.
  741. */
  742. static int cdns_uart_request_port(struct uart_port *port)
  743. {
  744. if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
  745. CDNS_UART_NAME)) {
  746. return -ENOMEM;
  747. }
  748. port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
  749. if (!port->membase) {
  750. dev_err(port->dev, "Unable to map registers\n");
  751. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  752. return -ENOMEM;
  753. }
  754. return 0;
  755. }
  756. /**
  757. * cdns_uart_release_port - Release UART port
  758. * @port: Handle to the uart port structure
  759. *
  760. * Release the memory region attached to a cdns_uart port. Called when the
  761. * driver removes a cdns_uart port via uart_remove_one_port().
  762. */
  763. static void cdns_uart_release_port(struct uart_port *port)
  764. {
  765. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  766. iounmap(port->membase);
  767. port->membase = NULL;
  768. }
  769. /**
  770. * cdns_uart_config_port - Configure UART port
  771. * @port: Handle to the uart port structure
  772. * @flags: If any
  773. */
  774. static void cdns_uart_config_port(struct uart_port *port, int flags)
  775. {
  776. if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
  777. port->type = PORT_XUARTPS;
  778. }
  779. /**
  780. * cdns_uart_get_mctrl - Get the modem control state
  781. * @port: Handle to the uart port structure
  782. *
  783. * Return: the modem control state
  784. */
  785. static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
  786. {
  787. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  788. }
  789. static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  790. {
  791. u32 val;
  792. val = readl(port->membase + CDNS_UART_MODEMCR);
  793. val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
  794. if (mctrl & TIOCM_RTS)
  795. val |= CDNS_UART_MODEMCR_RTS;
  796. if (mctrl & TIOCM_DTR)
  797. val |= CDNS_UART_MODEMCR_DTR;
  798. writel(val, port->membase + CDNS_UART_MODEMCR);
  799. }
  800. #ifdef CONFIG_CONSOLE_POLL
  801. static int cdns_uart_poll_get_char(struct uart_port *port)
  802. {
  803. int c;
  804. unsigned long flags;
  805. spin_lock_irqsave(&port->lock, flags);
  806. /* Check if FIFO is empty */
  807. if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
  808. c = NO_POLL_CHAR;
  809. else /* Read a character */
  810. c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
  811. spin_unlock_irqrestore(&port->lock, flags);
  812. return c;
  813. }
  814. static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
  815. {
  816. unsigned long flags;
  817. spin_lock_irqsave(&port->lock, flags);
  818. /* Wait until FIFO is empty */
  819. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  820. cpu_relax();
  821. /* Write a character */
  822. writel(c, port->membase + CDNS_UART_FIFO);
  823. /* Wait until FIFO is empty */
  824. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  825. cpu_relax();
  826. spin_unlock_irqrestore(&port->lock, flags);
  827. return;
  828. }
  829. #endif
  830. static struct uart_ops cdns_uart_ops = {
  831. .set_mctrl = cdns_uart_set_mctrl,
  832. .get_mctrl = cdns_uart_get_mctrl,
  833. .start_tx = cdns_uart_start_tx,
  834. .stop_tx = cdns_uart_stop_tx,
  835. .stop_rx = cdns_uart_stop_rx,
  836. .tx_empty = cdns_uart_tx_empty,
  837. .break_ctl = cdns_uart_break_ctl,
  838. .set_termios = cdns_uart_set_termios,
  839. .startup = cdns_uart_startup,
  840. .shutdown = cdns_uart_shutdown,
  841. .type = cdns_uart_type,
  842. .verify_port = cdns_uart_verify_port,
  843. .request_port = cdns_uart_request_port,
  844. .release_port = cdns_uart_release_port,
  845. .config_port = cdns_uart_config_port,
  846. #ifdef CONFIG_CONSOLE_POLL
  847. .poll_get_char = cdns_uart_poll_get_char,
  848. .poll_put_char = cdns_uart_poll_put_char,
  849. #endif
  850. };
  851. static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
  852. /**
  853. * cdns_uart_get_port - Configure the port from platform device resource info
  854. * @id: Port id
  855. *
  856. * Return: a pointer to a uart_port or NULL for failure
  857. */
  858. static struct uart_port *cdns_uart_get_port(int id)
  859. {
  860. struct uart_port *port;
  861. /* Try the given port id if failed use default method */
  862. if (cdns_uart_port[id].mapbase != 0) {
  863. /* Find the next unused port */
  864. for (id = 0; id < CDNS_UART_NR_PORTS; id++)
  865. if (cdns_uart_port[id].mapbase == 0)
  866. break;
  867. }
  868. if (id >= CDNS_UART_NR_PORTS)
  869. return NULL;
  870. port = &cdns_uart_port[id];
  871. /* At this point, we've got an empty uart_port struct, initialize it */
  872. spin_lock_init(&port->lock);
  873. port->membase = NULL;
  874. port->irq = 0;
  875. port->type = PORT_UNKNOWN;
  876. port->iotype = UPIO_MEM32;
  877. port->flags = UPF_BOOT_AUTOCONF;
  878. port->ops = &cdns_uart_ops;
  879. port->fifosize = CDNS_UART_FIFO_SIZE;
  880. port->line = id;
  881. port->dev = NULL;
  882. return port;
  883. }
  884. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  885. /**
  886. * cdns_uart_console_wait_tx - Wait for the TX to be full
  887. * @port: Handle to the uart port structure
  888. */
  889. static void cdns_uart_console_wait_tx(struct uart_port *port)
  890. {
  891. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  892. barrier();
  893. }
  894. /**
  895. * cdns_uart_console_putchar - write the character to the FIFO buffer
  896. * @port: Handle to the uart port structure
  897. * @ch: Character to be written
  898. */
  899. static void cdns_uart_console_putchar(struct uart_port *port, int ch)
  900. {
  901. cdns_uart_console_wait_tx(port);
  902. writel(ch, port->membase + CDNS_UART_FIFO);
  903. }
  904. static void __init cdns_early_write(struct console *con, const char *s,
  905. unsigned n)
  906. {
  907. struct earlycon_device *dev = con->data;
  908. uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
  909. }
  910. static int __init cdns_early_console_setup(struct earlycon_device *device,
  911. const char *opt)
  912. {
  913. if (!device->port.membase)
  914. return -ENODEV;
  915. device->con->write = cdns_early_write;
  916. return 0;
  917. }
  918. OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
  919. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
  920. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
  921. /**
  922. * cdns_uart_console_write - perform write operation
  923. * @co: Console handle
  924. * @s: Pointer to character array
  925. * @count: No of characters
  926. */
  927. static void cdns_uart_console_write(struct console *co, const char *s,
  928. unsigned int count)
  929. {
  930. struct uart_port *port = &cdns_uart_port[co->index];
  931. unsigned long flags;
  932. unsigned int imr, ctrl;
  933. int locked = 1;
  934. if (port->sysrq)
  935. locked = 0;
  936. else if (oops_in_progress)
  937. locked = spin_trylock_irqsave(&port->lock, flags);
  938. else
  939. spin_lock_irqsave(&port->lock, flags);
  940. /* save and disable interrupt */
  941. imr = readl(port->membase + CDNS_UART_IMR);
  942. writel(imr, port->membase + CDNS_UART_IDR);
  943. /*
  944. * Make sure that the tx part is enabled. Set the TX enable bit and
  945. * clear the TX disable bit to enable the transmitter.
  946. */
  947. ctrl = readl(port->membase + CDNS_UART_CR);
  948. ctrl &= ~CDNS_UART_CR_TX_DIS;
  949. ctrl |= CDNS_UART_CR_TX_EN;
  950. writel(ctrl, port->membase + CDNS_UART_CR);
  951. uart_console_write(port, s, count, cdns_uart_console_putchar);
  952. cdns_uart_console_wait_tx(port);
  953. writel(ctrl, port->membase + CDNS_UART_CR);
  954. /* restore interrupt state */
  955. writel(imr, port->membase + CDNS_UART_IER);
  956. if (locked)
  957. spin_unlock_irqrestore(&port->lock, flags);
  958. }
  959. /**
  960. * cdns_uart_console_setup - Initialize the uart to default config
  961. * @co: Console handle
  962. * @options: Initial settings of uart
  963. *
  964. * Return: 0 on success, negative errno otherwise.
  965. */
  966. static int __init cdns_uart_console_setup(struct console *co, char *options)
  967. {
  968. struct uart_port *port = &cdns_uart_port[co->index];
  969. int baud = 9600;
  970. int bits = 8;
  971. int parity = 'n';
  972. int flow = 'n';
  973. if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
  974. return -EINVAL;
  975. if (!port->membase) {
  976. pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
  977. co->index);
  978. return -ENODEV;
  979. }
  980. if (options)
  981. uart_parse_options(options, &baud, &parity, &bits, &flow);
  982. return uart_set_options(port, co, baud, parity, bits, flow);
  983. }
  984. static struct uart_driver cdns_uart_uart_driver;
  985. static struct console cdns_uart_console = {
  986. .name = CDNS_UART_TTY_NAME,
  987. .write = cdns_uart_console_write,
  988. .device = uart_console_device,
  989. .setup = cdns_uart_console_setup,
  990. .flags = CON_PRINTBUFFER,
  991. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  992. .data = &cdns_uart_uart_driver,
  993. };
  994. /**
  995. * cdns_uart_console_init - Initialization call
  996. *
  997. * Return: 0 on success, negative errno otherwise
  998. */
  999. static int __init cdns_uart_console_init(void)
  1000. {
  1001. register_console(&cdns_uart_console);
  1002. return 0;
  1003. }
  1004. console_initcall(cdns_uart_console_init);
  1005. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1006. static struct uart_driver cdns_uart_uart_driver = {
  1007. .owner = THIS_MODULE,
  1008. .driver_name = CDNS_UART_NAME,
  1009. .dev_name = CDNS_UART_TTY_NAME,
  1010. .major = CDNS_UART_MAJOR,
  1011. .minor = CDNS_UART_MINOR,
  1012. .nr = CDNS_UART_NR_PORTS,
  1013. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1014. .cons = &cdns_uart_console,
  1015. #endif
  1016. };
  1017. #ifdef CONFIG_PM_SLEEP
  1018. /**
  1019. * cdns_uart_suspend - suspend event
  1020. * @device: Pointer to the device structure
  1021. *
  1022. * Return: 0
  1023. */
  1024. static int cdns_uart_suspend(struct device *device)
  1025. {
  1026. struct uart_port *port = dev_get_drvdata(device);
  1027. struct tty_struct *tty;
  1028. struct device *tty_dev;
  1029. int may_wake = 0;
  1030. /* Get the tty which could be NULL so don't assume it's valid */
  1031. tty = tty_port_tty_get(&port->state->port);
  1032. if (tty) {
  1033. tty_dev = tty->dev;
  1034. may_wake = device_may_wakeup(tty_dev);
  1035. tty_kref_put(tty);
  1036. }
  1037. /*
  1038. * Call the API provided in serial_core.c file which handles
  1039. * the suspend.
  1040. */
  1041. uart_suspend_port(&cdns_uart_uart_driver, port);
  1042. if (console_suspend_enabled && !may_wake) {
  1043. struct cdns_uart *cdns_uart = port->private_data;
  1044. clk_disable(cdns_uart->uartclk);
  1045. clk_disable(cdns_uart->pclk);
  1046. } else {
  1047. unsigned long flags = 0;
  1048. spin_lock_irqsave(&port->lock, flags);
  1049. /* Empty the receive FIFO 1st before making changes */
  1050. while (!(readl(port->membase + CDNS_UART_SR) &
  1051. CDNS_UART_SR_RXEMPTY))
  1052. readl(port->membase + CDNS_UART_FIFO);
  1053. /* set RX trigger level to 1 */
  1054. writel(1, port->membase + CDNS_UART_RXWM);
  1055. /* disable RX timeout interrups */
  1056. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
  1057. spin_unlock_irqrestore(&port->lock, flags);
  1058. }
  1059. return 0;
  1060. }
  1061. /**
  1062. * cdns_uart_resume - Resume after a previous suspend
  1063. * @device: Pointer to the device structure
  1064. *
  1065. * Return: 0
  1066. */
  1067. static int cdns_uart_resume(struct device *device)
  1068. {
  1069. struct uart_port *port = dev_get_drvdata(device);
  1070. unsigned long flags = 0;
  1071. u32 ctrl_reg;
  1072. struct tty_struct *tty;
  1073. struct device *tty_dev;
  1074. int may_wake = 0;
  1075. /* Get the tty which could be NULL so don't assume it's valid */
  1076. tty = tty_port_tty_get(&port->state->port);
  1077. if (tty) {
  1078. tty_dev = tty->dev;
  1079. may_wake = device_may_wakeup(tty_dev);
  1080. tty_kref_put(tty);
  1081. }
  1082. if (console_suspend_enabled && !may_wake) {
  1083. struct cdns_uart *cdns_uart = port->private_data;
  1084. clk_enable(cdns_uart->pclk);
  1085. clk_enable(cdns_uart->uartclk);
  1086. spin_lock_irqsave(&port->lock, flags);
  1087. /* Set TX/RX Reset */
  1088. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1089. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  1090. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1091. while (readl(port->membase + CDNS_UART_CR) &
  1092. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  1093. cpu_relax();
  1094. /* restore rx timeout value */
  1095. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  1096. /* Enable Tx/Rx */
  1097. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1098. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  1099. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  1100. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1101. spin_unlock_irqrestore(&port->lock, flags);
  1102. } else {
  1103. spin_lock_irqsave(&port->lock, flags);
  1104. /* restore original rx trigger level */
  1105. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  1106. /* enable RX timeout interrupt */
  1107. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
  1108. spin_unlock_irqrestore(&port->lock, flags);
  1109. }
  1110. return uart_resume_port(&cdns_uart_uart_driver, port);
  1111. }
  1112. #endif /* ! CONFIG_PM_SLEEP */
  1113. static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
  1114. cdns_uart_resume);
  1115. /**
  1116. * cdns_uart_probe - Platform driver probe
  1117. * @pdev: Pointer to the platform device structure
  1118. *
  1119. * Return: 0 on success, negative errno otherwise
  1120. */
  1121. static int cdns_uart_probe(struct platform_device *pdev)
  1122. {
  1123. int rc, id, irq;
  1124. struct uart_port *port;
  1125. struct resource *res;
  1126. struct cdns_uart *cdns_uart_data;
  1127. cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
  1128. GFP_KERNEL);
  1129. if (!cdns_uart_data)
  1130. return -ENOMEM;
  1131. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
  1132. if (IS_ERR(cdns_uart_data->pclk)) {
  1133. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
  1134. if (!IS_ERR(cdns_uart_data->pclk))
  1135. dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
  1136. }
  1137. if (IS_ERR(cdns_uart_data->pclk)) {
  1138. dev_err(&pdev->dev, "pclk clock not found.\n");
  1139. return PTR_ERR(cdns_uart_data->pclk);
  1140. }
  1141. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
  1142. if (IS_ERR(cdns_uart_data->uartclk)) {
  1143. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
  1144. if (!IS_ERR(cdns_uart_data->uartclk))
  1145. dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
  1146. }
  1147. if (IS_ERR(cdns_uart_data->uartclk)) {
  1148. dev_err(&pdev->dev, "uart_clk clock not found.\n");
  1149. return PTR_ERR(cdns_uart_data->uartclk);
  1150. }
  1151. rc = clk_prepare_enable(cdns_uart_data->pclk);
  1152. if (rc) {
  1153. dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
  1154. return rc;
  1155. }
  1156. rc = clk_prepare_enable(cdns_uart_data->uartclk);
  1157. if (rc) {
  1158. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1159. goto err_out_clk_dis_pclk;
  1160. }
  1161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1162. if (!res) {
  1163. rc = -ENODEV;
  1164. goto err_out_clk_disable;
  1165. }
  1166. irq = platform_get_irq(pdev, 0);
  1167. if (irq <= 0) {
  1168. rc = -ENXIO;
  1169. goto err_out_clk_disable;
  1170. }
  1171. #ifdef CONFIG_COMMON_CLK
  1172. cdns_uart_data->clk_rate_change_nb.notifier_call =
  1173. cdns_uart_clk_notifier_cb;
  1174. if (clk_notifier_register(cdns_uart_data->uartclk,
  1175. &cdns_uart_data->clk_rate_change_nb))
  1176. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1177. #endif
  1178. /* Look for a serialN alias */
  1179. id = of_alias_get_id(pdev->dev.of_node, "serial");
  1180. if (id < 0)
  1181. id = 0;
  1182. /* Initialize the port structure */
  1183. port = cdns_uart_get_port(id);
  1184. if (!port) {
  1185. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1186. rc = -ENODEV;
  1187. goto err_out_notif_unreg;
  1188. }
  1189. /*
  1190. * Register the port.
  1191. * This function also registers this device with the tty layer
  1192. * and triggers invocation of the config_port() entry point.
  1193. */
  1194. port->mapbase = res->start;
  1195. port->irq = irq;
  1196. port->dev = &pdev->dev;
  1197. port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
  1198. port->private_data = cdns_uart_data;
  1199. cdns_uart_data->port = port;
  1200. platform_set_drvdata(pdev, port);
  1201. rc = uart_add_one_port(&cdns_uart_uart_driver, port);
  1202. if (rc) {
  1203. dev_err(&pdev->dev,
  1204. "uart_add_one_port() failed; err=%i\n", rc);
  1205. goto err_out_notif_unreg;
  1206. }
  1207. return 0;
  1208. err_out_notif_unreg:
  1209. #ifdef CONFIG_COMMON_CLK
  1210. clk_notifier_unregister(cdns_uart_data->uartclk,
  1211. &cdns_uart_data->clk_rate_change_nb);
  1212. #endif
  1213. err_out_clk_disable:
  1214. clk_disable_unprepare(cdns_uart_data->uartclk);
  1215. err_out_clk_dis_pclk:
  1216. clk_disable_unprepare(cdns_uart_data->pclk);
  1217. return rc;
  1218. }
  1219. /**
  1220. * cdns_uart_remove - called when the platform driver is unregistered
  1221. * @pdev: Pointer to the platform device structure
  1222. *
  1223. * Return: 0 on success, negative errno otherwise
  1224. */
  1225. static int cdns_uart_remove(struct platform_device *pdev)
  1226. {
  1227. struct uart_port *port = platform_get_drvdata(pdev);
  1228. struct cdns_uart *cdns_uart_data = port->private_data;
  1229. int rc;
  1230. /* Remove the cdns_uart port from the serial core */
  1231. #ifdef CONFIG_COMMON_CLK
  1232. clk_notifier_unregister(cdns_uart_data->uartclk,
  1233. &cdns_uart_data->clk_rate_change_nb);
  1234. #endif
  1235. rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
  1236. port->mapbase = 0;
  1237. clk_disable_unprepare(cdns_uart_data->uartclk);
  1238. clk_disable_unprepare(cdns_uart_data->pclk);
  1239. return rc;
  1240. }
  1241. /* Match table for of_platform binding */
  1242. static const struct of_device_id cdns_uart_of_match[] = {
  1243. { .compatible = "xlnx,xuartps", },
  1244. { .compatible = "cdns,uart-r1p8", },
  1245. {}
  1246. };
  1247. MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
  1248. static struct platform_driver cdns_uart_platform_driver = {
  1249. .probe = cdns_uart_probe,
  1250. .remove = cdns_uart_remove,
  1251. .driver = {
  1252. .name = CDNS_UART_NAME,
  1253. .of_match_table = cdns_uart_of_match,
  1254. .pm = &cdns_uart_dev_pm_ops,
  1255. },
  1256. };
  1257. static int __init cdns_uart_init(void)
  1258. {
  1259. int retval = 0;
  1260. /* Register the cdns_uart driver with the serial core */
  1261. retval = uart_register_driver(&cdns_uart_uart_driver);
  1262. if (retval)
  1263. return retval;
  1264. /* Register the platform driver */
  1265. retval = platform_driver_register(&cdns_uart_platform_driver);
  1266. if (retval)
  1267. uart_unregister_driver(&cdns_uart_uart_driver);
  1268. return retval;
  1269. }
  1270. static void __exit cdns_uart_exit(void)
  1271. {
  1272. /* Unregister the platform driver */
  1273. platform_driver_unregister(&cdns_uart_platform_driver);
  1274. /* Unregister the cdns_uart driver */
  1275. uart_unregister_driver(&cdns_uart_uart_driver);
  1276. }
  1277. module_init(cdns_uart_init);
  1278. module_exit(cdns_uart_exit);
  1279. MODULE_DESCRIPTION("Driver for Cadence UART");
  1280. MODULE_AUTHOR("Xilinx Inc.");
  1281. MODULE_LICENSE("GPL");