xilinx_uartps.c 49 KB

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