xilinx_uartps.c 48 KB

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