xilinx_uartps.c 47 KB

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