xilinx_uartps.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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/gpio.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/delay.h>
  27. #include <linux/reset.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. * Modem Status register:
  147. * The read/write Modem Status register reports the interface with the modem
  148. * or data set, or a peripheral device emulating a modem.
  149. */
  150. #define CDNS_UART_MODEMSR_DCD BIT(7) /* Data Carrier Detect */
  151. #define CDNS_UART_MODEMSR_RI BIT(6) /* Ting Indicator */
  152. #define CDNS_UART_MODEMSR_DSR BIT(5) /* Data Set Ready */
  153. #define CDNS_UART_MODEMSR_CTS BIT(4) /* Clear To Send */
  154. /*
  155. * Channel Status Register:
  156. * The channel status register (CSR) is provided to enable the control logic
  157. * to monitor the status of bits in the channel interrupt status register,
  158. * even if these are masked out by the interrupt mask register.
  159. */
  160. #define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  161. #define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
  162. #define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
  163. #define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
  164. #define CDNS_UART_SR_TACTIVE 0x00000800 /* TX state machine active */
  165. /* baud dividers min/max values */
  166. #define CDNS_UART_BDIV_MIN 4
  167. #define CDNS_UART_BDIV_MAX 255
  168. #define CDNS_UART_CD_MAX 65535
  169. #define UART_AUTOSUSPEND_TIMEOUT 3000
  170. /**
  171. * struct cdns_uart - device data
  172. * @port: Pointer to the UART port
  173. * @uartclk: Reference clock
  174. * @pclk: APB clock
  175. * @cdns_uart_driver: Pointer to UART driver
  176. * @baud: Current baud rate
  177. * @clk_rate_change_nb: Notifier block for clock changes
  178. * @quirks: Flags for RXBS support.
  179. * @cts_override: Modem control state override
  180. * @gpiod_rts: Pointer to the gpio descriptor
  181. * @rs485_tx_started: RS485 tx state
  182. * @tx_timer: Timer for tx
  183. * @rstc: Pointer to the reset control
  184. */
  185. struct cdns_uart {
  186. struct uart_port *port;
  187. struct clk *uartclk;
  188. struct clk *pclk;
  189. struct uart_driver *cdns_uart_driver;
  190. unsigned int baud;
  191. struct notifier_block clk_rate_change_nb;
  192. u32 quirks;
  193. bool cts_override;
  194. struct gpio_desc *gpiod_rts;
  195. bool rs485_tx_started;
  196. struct hrtimer tx_timer;
  197. struct reset_control *rstc;
  198. };
  199. struct cdns_platform_data {
  200. u32 quirks;
  201. };
  202. static struct serial_rs485 cdns_rs485_supported = {
  203. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
  204. SER_RS485_RTS_AFTER_SEND,
  205. .delay_rts_before_send = 1,
  206. .delay_rts_after_send = 1,
  207. };
  208. #define to_cdns_uart(_nb) container_of(_nb, struct cdns_uart, \
  209. clk_rate_change_nb)
  210. /**
  211. * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
  212. * @dev_id: Id of the UART port
  213. * @isrstatus: The interrupt status register value as read
  214. * Return: None
  215. */
  216. static void cdns_uart_handle_rx(void *dev_id, unsigned int isrstatus)
  217. {
  218. struct uart_port *port = (struct uart_port *)dev_id;
  219. struct cdns_uart *cdns_uart = port->private_data;
  220. unsigned int data;
  221. unsigned int rxbs_status = 0;
  222. unsigned int status_mask;
  223. unsigned int framerrprocessed = 0;
  224. char status = TTY_NORMAL;
  225. bool is_rxbs_support;
  226. is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
  227. while ((readl(port->membase + CDNS_UART_SR) &
  228. CDNS_UART_SR_RXEMPTY) != CDNS_UART_SR_RXEMPTY) {
  229. if (is_rxbs_support)
  230. rxbs_status = readl(port->membase + CDNS_UART_RXBS);
  231. data = readl(port->membase + CDNS_UART_FIFO);
  232. port->icount.rx++;
  233. /*
  234. * There is no hardware break detection in Zynq, so we interpret
  235. * framing error with all-zeros data as a break sequence.
  236. * Most of the time, there's another non-zero byte at the
  237. * end of the sequence.
  238. */
  239. if (!is_rxbs_support && (isrstatus & CDNS_UART_IXR_FRAMING)) {
  240. if (!data) {
  241. port->read_status_mask |= CDNS_UART_IXR_BRK;
  242. framerrprocessed = 1;
  243. continue;
  244. }
  245. }
  246. if (is_rxbs_support && (rxbs_status & CDNS_UART_RXBS_BRK)) {
  247. port->icount.brk++;
  248. status = TTY_BREAK;
  249. if (uart_handle_break(port))
  250. continue;
  251. }
  252. isrstatus &= port->read_status_mask;
  253. isrstatus &= ~port->ignore_status_mask;
  254. status_mask = port->read_status_mask;
  255. status_mask &= ~port->ignore_status_mask;
  256. if (data &&
  257. (port->read_status_mask & CDNS_UART_IXR_BRK)) {
  258. port->read_status_mask &= ~CDNS_UART_IXR_BRK;
  259. port->icount.brk++;
  260. if (uart_handle_break(port))
  261. continue;
  262. }
  263. if (uart_prepare_sysrq_char(port, data))
  264. continue;
  265. if (is_rxbs_support) {
  266. if ((rxbs_status & CDNS_UART_RXBS_PARITY)
  267. && (status_mask & CDNS_UART_IXR_PARITY)) {
  268. port->icount.parity++;
  269. status = TTY_PARITY;
  270. }
  271. if ((rxbs_status & CDNS_UART_RXBS_FRAMING)
  272. && (status_mask & CDNS_UART_IXR_PARITY)) {
  273. port->icount.frame++;
  274. status = TTY_FRAME;
  275. }
  276. } else {
  277. if (isrstatus & CDNS_UART_IXR_PARITY) {
  278. port->icount.parity++;
  279. status = TTY_PARITY;
  280. }
  281. if ((isrstatus & CDNS_UART_IXR_FRAMING) &&
  282. !framerrprocessed) {
  283. port->icount.frame++;
  284. status = TTY_FRAME;
  285. }
  286. }
  287. if (isrstatus & CDNS_UART_IXR_OVERRUN) {
  288. port->icount.overrun++;
  289. tty_insert_flip_char(&port->state->port, 0,
  290. TTY_OVERRUN);
  291. }
  292. tty_insert_flip_char(&port->state->port, data, status);
  293. isrstatus = 0;
  294. }
  295. tty_flip_buffer_push(&port->state->port);
  296. }
  297. /**
  298. * cdns_rts_gpio_enable - Configure RTS/GPIO to high/low
  299. * @cdns_uart: Handle to the cdns_uart
  300. * @enable: Value to be set to RTS/GPIO
  301. */
  302. static void cdns_rts_gpio_enable(struct cdns_uart *cdns_uart, bool enable)
  303. {
  304. u32 val;
  305. if (cdns_uart->gpiod_rts) {
  306. gpiod_set_value(cdns_uart->gpiod_rts, enable);
  307. } else {
  308. val = readl(cdns_uart->port->membase + CDNS_UART_MODEMCR);
  309. if (enable)
  310. val |= CDNS_UART_MODEMCR_RTS;
  311. else
  312. val &= ~CDNS_UART_MODEMCR_RTS;
  313. writel(val, cdns_uart->port->membase + CDNS_UART_MODEMCR);
  314. }
  315. }
  316. /**
  317. * cdns_rs485_tx_setup - Tx setup specific to rs485
  318. * @cdns_uart: Handle to the cdns_uart
  319. */
  320. static void cdns_rs485_tx_setup(struct cdns_uart *cdns_uart)
  321. {
  322. bool enable;
  323. enable = cdns_uart->port->rs485.flags & SER_RS485_RTS_ON_SEND;
  324. cdns_rts_gpio_enable(cdns_uart, enable);
  325. cdns_uart->rs485_tx_started = true;
  326. }
  327. /**
  328. * cdns_rs485_rx_setup - Rx setup specific to rs485
  329. * @cdns_uart: Handle to the cdns_uart
  330. */
  331. static void cdns_rs485_rx_setup(struct cdns_uart *cdns_uart)
  332. {
  333. bool enable;
  334. enable = cdns_uart->port->rs485.flags & SER_RS485_RTS_AFTER_SEND;
  335. cdns_rts_gpio_enable(cdns_uart, enable);
  336. cdns_uart->rs485_tx_started = false;
  337. }
  338. /**
  339. * cdns_uart_tx_empty - Check whether TX is empty
  340. * @port: Handle to the uart port structure
  341. *
  342. * Return: TIOCSER_TEMT on success, 0 otherwise
  343. */
  344. static unsigned int cdns_uart_tx_empty(struct uart_port *port)
  345. {
  346. unsigned int status;
  347. status = readl(port->membase + CDNS_UART_SR);
  348. status &= (CDNS_UART_SR_TXEMPTY | CDNS_UART_SR_TACTIVE);
  349. return (status == CDNS_UART_SR_TXEMPTY) ? TIOCSER_TEMT : 0;
  350. }
  351. /**
  352. * cdns_rs485_rx_callback - Timer rx callback handler for rs485.
  353. * @t: Handle to the hrtimer structure
  354. */
  355. static enum hrtimer_restart cdns_rs485_rx_callback(struct hrtimer *t)
  356. {
  357. struct cdns_uart *cdns_uart = container_of(t, struct cdns_uart, tx_timer);
  358. /*
  359. * Default Rx should be setup, because Rx signaling path
  360. * need to enable to receive data.
  361. */
  362. cdns_rs485_rx_setup(cdns_uart);
  363. return HRTIMER_NORESTART;
  364. }
  365. /**
  366. * cdns_calc_after_tx_delay - calculate delay required for after tx.
  367. * @cdns_uart: Handle to the cdns_uart
  368. */
  369. static u64 cdns_calc_after_tx_delay(struct cdns_uart *cdns_uart)
  370. {
  371. /*
  372. * Frame time + stop bit time + rs485.delay_rts_after_send
  373. */
  374. return cdns_uart->port->frame_time
  375. + DIV_ROUND_UP(cdns_uart->port->frame_time, 7)
  376. + (u64)cdns_uart->port->rs485.delay_rts_after_send * NSEC_PER_MSEC;
  377. }
  378. /**
  379. * cdns_uart_handle_tx - Handle the bytes to be transmitted.
  380. * @dev_id: Id of the UART port
  381. * Return: None
  382. */
  383. static void cdns_uart_handle_tx(void *dev_id)
  384. {
  385. struct uart_port *port = (struct uart_port *)dev_id;
  386. struct cdns_uart *cdns_uart = port->private_data;
  387. struct tty_port *tport = &port->state->port;
  388. unsigned int numbytes;
  389. unsigned char ch;
  390. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  391. /* Disable the TX Empty interrupt */
  392. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR);
  393. return;
  394. }
  395. numbytes = port->fifosize;
  396. while (numbytes &&
  397. !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL) &&
  398. uart_fifo_get(port, &ch)) {
  399. writel(ch, port->membase + CDNS_UART_FIFO);
  400. numbytes--;
  401. }
  402. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  403. uart_write_wakeup(port);
  404. /* Enable the TX Empty interrupt */
  405. writel(CDNS_UART_IXR_TXEMPTY, cdns_uart->port->membase + CDNS_UART_IER);
  406. if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED &&
  407. (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port))) {
  408. cdns_uart->tx_timer.function = &cdns_rs485_rx_callback;
  409. hrtimer_start(&cdns_uart->tx_timer,
  410. ns_to_ktime(cdns_calc_after_tx_delay(cdns_uart)), HRTIMER_MODE_REL);
  411. }
  412. }
  413. /**
  414. * cdns_uart_isr - Interrupt handler
  415. * @irq: Irq number
  416. * @dev_id: Id of the port
  417. *
  418. * Return: IRQHANDLED
  419. */
  420. static irqreturn_t cdns_uart_isr(int irq, void *dev_id)
  421. {
  422. struct uart_port *port = (struct uart_port *)dev_id;
  423. unsigned int isrstatus;
  424. uart_port_lock(port);
  425. /* Read the interrupt status register to determine which
  426. * interrupt(s) is/are active and clear them.
  427. */
  428. isrstatus = readl(port->membase + CDNS_UART_ISR);
  429. writel(isrstatus, port->membase + CDNS_UART_ISR);
  430. if (isrstatus & CDNS_UART_IXR_TXEMPTY) {
  431. cdns_uart_handle_tx(dev_id);
  432. isrstatus &= ~CDNS_UART_IXR_TXEMPTY;
  433. }
  434. isrstatus &= port->read_status_mask;
  435. isrstatus &= ~port->ignore_status_mask;
  436. /*
  437. * Skip RX processing if RX is disabled as RXEMPTY will never be set
  438. * as read bytes will not be removed from the FIFO.
  439. */
  440. if (isrstatus & CDNS_UART_IXR_RXMASK &&
  441. !(readl(port->membase + CDNS_UART_CR) & CDNS_UART_CR_RX_DIS))
  442. cdns_uart_handle_rx(dev_id, isrstatus);
  443. uart_unlock_and_check_sysrq(port);
  444. return IRQ_HANDLED;
  445. }
  446. /**
  447. * cdns_uart_calc_baud_divs - Calculate baud rate divisors
  448. * @clk: UART module input clock
  449. * @baud: Desired baud rate
  450. * @rbdiv: BDIV value (return value)
  451. * @rcd: CD value (return value)
  452. * @div8: Value for clk_sel bit in mod (return value)
  453. * Return: baud rate, requested baud when possible, or actual baud when there
  454. * was too much error, zero if no valid divisors are found.
  455. *
  456. * Formula to obtain baud rate is
  457. * baud_tx/rx rate = clk/CD * (BDIV + 1)
  458. * input_clk = (Uart User Defined Clock or Apb Clock)
  459. * depends on UCLKEN in MR Reg
  460. * clk = input_clk or input_clk/8;
  461. * depends on CLKS in MR reg
  462. * CD and BDIV depends on values in
  463. * baud rate generate register
  464. * baud rate clock divisor register
  465. */
  466. static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
  467. unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
  468. {
  469. u32 cd, bdiv;
  470. unsigned int calc_baud;
  471. unsigned int bestbaud = 0;
  472. unsigned int bauderror;
  473. unsigned int besterror = ~0;
  474. if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
  475. *div8 = 1;
  476. clk /= 8;
  477. } else {
  478. *div8 = 0;
  479. }
  480. for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
  481. cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
  482. if (cd < 1 || cd > CDNS_UART_CD_MAX)
  483. continue;
  484. calc_baud = clk / (cd * (bdiv + 1));
  485. if (baud > calc_baud)
  486. bauderror = baud - calc_baud;
  487. else
  488. bauderror = calc_baud - baud;
  489. if (besterror > bauderror) {
  490. *rbdiv = bdiv;
  491. *rcd = cd;
  492. bestbaud = calc_baud;
  493. besterror = bauderror;
  494. }
  495. }
  496. /* use the values when percent error is acceptable */
  497. if (((besterror * 100) / baud) < 3)
  498. bestbaud = baud;
  499. return bestbaud;
  500. }
  501. /**
  502. * cdns_uart_set_baud_rate - Calculate and set the baud rate
  503. * @port: Handle to the uart port structure
  504. * @baud: Baud rate to set
  505. * Return: baud rate, requested baud when possible, or actual baud when there
  506. * was too much error, zero if no valid divisors are found.
  507. */
  508. static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
  509. unsigned int baud)
  510. {
  511. unsigned int calc_baud;
  512. u32 cd = 0, bdiv = 0;
  513. u32 mreg;
  514. int div8;
  515. struct cdns_uart *cdns_uart = port->private_data;
  516. calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
  517. &div8);
  518. /* Write new divisors to hardware */
  519. mreg = readl(port->membase + CDNS_UART_MR);
  520. if (div8)
  521. mreg |= CDNS_UART_MR_CLKSEL;
  522. else
  523. mreg &= ~CDNS_UART_MR_CLKSEL;
  524. writel(mreg, port->membase + CDNS_UART_MR);
  525. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  526. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  527. cdns_uart->baud = baud;
  528. return calc_baud;
  529. }
  530. #ifdef CONFIG_COMMON_CLK
  531. /**
  532. * cdns_uart_clk_notifier_cb - Clock notifier callback
  533. * @nb: Notifier block
  534. * @event: Notify event
  535. * @data: Notifier data
  536. * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
  537. */
  538. static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
  539. unsigned long event, void *data)
  540. {
  541. u32 ctrl_reg;
  542. struct uart_port *port;
  543. int locked = 0;
  544. struct clk_notifier_data *ndata = data;
  545. struct cdns_uart *cdns_uart = to_cdns_uart(nb);
  546. unsigned long flags;
  547. port = cdns_uart->port;
  548. if (port->suspended)
  549. return NOTIFY_OK;
  550. switch (event) {
  551. case PRE_RATE_CHANGE:
  552. {
  553. u32 bdiv, cd;
  554. int div8;
  555. /*
  556. * Find out if current baud-rate can be achieved with new clock
  557. * frequency.
  558. */
  559. if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
  560. &bdiv, &cd, &div8)) {
  561. dev_warn(port->dev, "clock rate change rejected\n");
  562. return NOTIFY_BAD;
  563. }
  564. uart_port_lock_irqsave(cdns_uart->port, &flags);
  565. /* Disable the TX and RX to set baud rate */
  566. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  567. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  568. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  569. uart_port_unlock_irqrestore(cdns_uart->port, flags);
  570. return NOTIFY_OK;
  571. }
  572. case POST_RATE_CHANGE:
  573. /*
  574. * Set clk dividers to generate correct baud with new clock
  575. * frequency.
  576. */
  577. uart_port_lock_irqsave(cdns_uart->port, &flags);
  578. locked = 1;
  579. port->uartclk = ndata->new_rate;
  580. cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
  581. cdns_uart->baud);
  582. fallthrough;
  583. case ABORT_RATE_CHANGE:
  584. if (!locked)
  585. uart_port_lock_irqsave(cdns_uart->port, &flags);
  586. /* Set TX/RX Reset */
  587. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  588. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  589. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  590. while (readl(port->membase + CDNS_UART_CR) &
  591. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  592. cpu_relax();
  593. /*
  594. * Clear the RX disable and TX disable bits and then set the TX
  595. * enable bit and RX enable bit to enable the transmitter and
  596. * receiver.
  597. */
  598. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  599. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  600. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  601. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  602. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  603. uart_port_unlock_irqrestore(cdns_uart->port, flags);
  604. return NOTIFY_OK;
  605. default:
  606. return NOTIFY_DONE;
  607. }
  608. }
  609. #endif
  610. /**
  611. * cdns_rs485_tx_callback - Timer tx callback handler for rs485.
  612. * @t: Handle to the hrtimer structure
  613. */
  614. static enum hrtimer_restart cdns_rs485_tx_callback(struct hrtimer *t)
  615. {
  616. struct cdns_uart *cdns_uart = container_of(t, struct cdns_uart, tx_timer);
  617. uart_port_lock(cdns_uart->port);
  618. cdns_uart_handle_tx(cdns_uart->port);
  619. uart_port_unlock(cdns_uart->port);
  620. return HRTIMER_NORESTART;
  621. }
  622. /**
  623. * cdns_uart_start_tx - Start transmitting bytes
  624. * @port: Handle to the uart port structure
  625. */
  626. static void cdns_uart_start_tx(struct uart_port *port)
  627. {
  628. unsigned int status;
  629. struct cdns_uart *cdns_uart = port->private_data;
  630. if (uart_tx_stopped(port))
  631. return;
  632. /*
  633. * Set the TX enable bit and clear the TX disable bit to enable the
  634. * transmitter.
  635. */
  636. status = readl(port->membase + CDNS_UART_CR);
  637. status &= ~CDNS_UART_CR_TX_DIS;
  638. status |= CDNS_UART_CR_TX_EN;
  639. writel(status, port->membase + CDNS_UART_CR);
  640. if (kfifo_is_empty(&port->state->port.xmit_fifo))
  641. return;
  642. /* Clear the TX Empty interrupt */
  643. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
  644. if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED) {
  645. if (!cdns_uart->rs485_tx_started) {
  646. cdns_uart->tx_timer.function = &cdns_rs485_tx_callback;
  647. cdns_rs485_tx_setup(cdns_uart);
  648. return hrtimer_start(&cdns_uart->tx_timer,
  649. ms_to_ktime(port->rs485.delay_rts_before_send),
  650. HRTIMER_MODE_REL);
  651. }
  652. }
  653. cdns_uart_handle_tx(port);
  654. }
  655. /**
  656. * cdns_uart_stop_tx - Stop TX
  657. * @port: Handle to the uart port structure
  658. */
  659. static void cdns_uart_stop_tx(struct uart_port *port)
  660. {
  661. unsigned int regval;
  662. struct cdns_uart *cdns_uart = port->private_data;
  663. if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED)
  664. cdns_rs485_rx_setup(cdns_uart);
  665. regval = readl(port->membase + CDNS_UART_CR);
  666. regval |= CDNS_UART_CR_TX_DIS;
  667. /* Disable the transmitter */
  668. writel(regval, port->membase + CDNS_UART_CR);
  669. }
  670. /**
  671. * cdns_uart_stop_rx - Stop RX
  672. * @port: Handle to the uart port structure
  673. */
  674. static void cdns_uart_stop_rx(struct uart_port *port)
  675. {
  676. unsigned int regval;
  677. /* Disable RX IRQs */
  678. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
  679. /* Disable the receiver */
  680. regval = readl(port->membase + CDNS_UART_CR);
  681. regval |= CDNS_UART_CR_RX_DIS;
  682. writel(regval, port->membase + CDNS_UART_CR);
  683. }
  684. /**
  685. * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
  686. * transmitting char breaks
  687. * @port: Handle to the uart port structure
  688. * @ctl: Value based on which start or stop decision is taken
  689. */
  690. static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
  691. {
  692. unsigned int status;
  693. unsigned long flags;
  694. uart_port_lock_irqsave(port, &flags);
  695. status = readl(port->membase + CDNS_UART_CR);
  696. if (ctl == -1)
  697. writel(CDNS_UART_CR_STARTBRK | (~CDNS_UART_CR_STOPBRK & status),
  698. port->membase + CDNS_UART_CR);
  699. else {
  700. if ((status & CDNS_UART_CR_STOPBRK) == 0)
  701. writel(CDNS_UART_CR_STOPBRK | status,
  702. port->membase + CDNS_UART_CR);
  703. }
  704. uart_port_unlock_irqrestore(port, flags);
  705. }
  706. /**
  707. * cdns_uart_set_termios - termios operations, handling data length, parity,
  708. * stop bits, flow control, baud rate
  709. * @port: Handle to the uart port structure
  710. * @termios: Handle to the input termios structure
  711. * @old: Values of the previously saved termios structure
  712. */
  713. static void cdns_uart_set_termios(struct uart_port *port,
  714. struct ktermios *termios,
  715. const struct ktermios *old)
  716. {
  717. u32 cval = 0;
  718. unsigned int baud, minbaud, maxbaud;
  719. unsigned long flags;
  720. unsigned int ctrl_reg, mode_reg;
  721. uart_port_lock_irqsave(port, &flags);
  722. /* Disable the TX and RX to set baud rate */
  723. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  724. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  725. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  726. /*
  727. * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
  728. * min and max baud should be calculated here based on port->uartclk.
  729. * this way we get a valid baud and can safely call set_baud()
  730. */
  731. minbaud = port->uartclk /
  732. ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
  733. maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
  734. baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
  735. baud = cdns_uart_set_baud_rate(port, baud);
  736. if (tty_termios_baud_rate(termios))
  737. tty_termios_encode_baud_rate(termios, baud, baud);
  738. /* Update the per-port timeout. */
  739. uart_update_timeout(port, termios->c_cflag, baud);
  740. /* Set TX/RX Reset */
  741. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  742. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  743. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  744. while (readl(port->membase + CDNS_UART_CR) &
  745. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  746. cpu_relax();
  747. /*
  748. * Clear the RX disable and TX disable bits and then set the TX enable
  749. * bit and RX enable bit to enable the transmitter and receiver.
  750. */
  751. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  752. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  753. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  754. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  755. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  756. port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
  757. CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
  758. port->ignore_status_mask = 0;
  759. if (termios->c_iflag & INPCK)
  760. port->read_status_mask |= CDNS_UART_IXR_PARITY |
  761. CDNS_UART_IXR_FRAMING;
  762. if (termios->c_iflag & IGNPAR)
  763. port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
  764. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  765. /* ignore all characters if CREAD is not set */
  766. if ((termios->c_cflag & CREAD) == 0)
  767. port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
  768. CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
  769. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  770. mode_reg = readl(port->membase + CDNS_UART_MR);
  771. /* Handling Data Size */
  772. switch (termios->c_cflag & CSIZE) {
  773. case CS6:
  774. cval |= CDNS_UART_MR_CHARLEN_6_BIT;
  775. break;
  776. case CS7:
  777. cval |= CDNS_UART_MR_CHARLEN_7_BIT;
  778. break;
  779. default:
  780. case CS8:
  781. cval |= CDNS_UART_MR_CHARLEN_8_BIT;
  782. termios->c_cflag &= ~CSIZE;
  783. termios->c_cflag |= CS8;
  784. break;
  785. }
  786. /* Handling Parity and Stop Bits length */
  787. if (termios->c_cflag & CSTOPB)
  788. cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  789. else
  790. cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  791. if (termios->c_cflag & PARENB) {
  792. /* Mark or Space parity */
  793. if (termios->c_cflag & CMSPAR) {
  794. if (termios->c_cflag & PARODD)
  795. cval |= CDNS_UART_MR_PARITY_MARK;
  796. else
  797. cval |= CDNS_UART_MR_PARITY_SPACE;
  798. } else {
  799. if (termios->c_cflag & PARODD)
  800. cval |= CDNS_UART_MR_PARITY_ODD;
  801. else
  802. cval |= CDNS_UART_MR_PARITY_EVEN;
  803. }
  804. } else {
  805. cval |= CDNS_UART_MR_PARITY_NONE;
  806. }
  807. cval |= mode_reg & 1;
  808. writel(cval, port->membase + CDNS_UART_MR);
  809. cval = readl(port->membase + CDNS_UART_MODEMCR);
  810. if (termios->c_cflag & CRTSCTS)
  811. cval |= CDNS_UART_MODEMCR_FCM;
  812. else
  813. cval &= ~CDNS_UART_MODEMCR_FCM;
  814. writel(cval, port->membase + CDNS_UART_MODEMCR);
  815. uart_port_unlock_irqrestore(port, flags);
  816. }
  817. /**
  818. * cdns_uart_startup - Called when an application opens a cdns_uart port
  819. * @port: Handle to the uart port structure
  820. *
  821. * Return: 0 on success, negative errno otherwise
  822. */
  823. static int cdns_uart_startup(struct uart_port *port)
  824. {
  825. struct cdns_uart *cdns_uart = port->private_data;
  826. bool is_brk_support;
  827. int ret;
  828. unsigned long flags;
  829. unsigned int status = 0;
  830. is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
  831. ret = reset_control_deassert(cdns_uart->rstc);
  832. if (ret)
  833. return ret;
  834. uart_port_lock_irqsave(port, &flags);
  835. /* Disable the TX and RX */
  836. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  837. port->membase + CDNS_UART_CR);
  838. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  839. * no break chars.
  840. */
  841. writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
  842. port->membase + CDNS_UART_CR);
  843. while (readl(port->membase + CDNS_UART_CR) &
  844. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  845. cpu_relax();
  846. if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED)
  847. cdns_rs485_rx_setup(cdns_uart);
  848. /*
  849. * Clear the RX disable bit and then set the RX enable bit to enable
  850. * the receiver.
  851. */
  852. status = readl(port->membase + CDNS_UART_CR);
  853. status &= ~CDNS_UART_CR_RX_DIS;
  854. status |= CDNS_UART_CR_RX_EN;
  855. writel(status, port->membase + CDNS_UART_CR);
  856. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  857. * no parity.
  858. */
  859. writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
  860. | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
  861. port->membase + CDNS_UART_MR);
  862. /*
  863. * Set the RX FIFO Trigger level to use most of the FIFO, but it
  864. * can be tuned with a module parameter
  865. */
  866. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  867. /*
  868. * Receive Timeout register is enabled but it
  869. * can be tuned with a module parameter
  870. */
  871. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  872. /* Clear out any pending interrupts before enabling them */
  873. writel(readl(port->membase + CDNS_UART_ISR),
  874. port->membase + CDNS_UART_ISR);
  875. uart_port_unlock_irqrestore(port, flags);
  876. ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
  877. if (ret) {
  878. dev_err(port->dev, "request_irq '%d' failed with %d\n",
  879. port->irq, ret);
  880. return ret;
  881. }
  882. /* Set the Interrupt Registers with desired interrupts */
  883. if (is_brk_support)
  884. writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
  885. port->membase + CDNS_UART_IER);
  886. else
  887. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
  888. return 0;
  889. }
  890. /**
  891. * cdns_uart_shutdown - Called when an application closes a cdns_uart port
  892. * @port: Handle to the uart port structure
  893. */
  894. static void cdns_uart_shutdown(struct uart_port *port)
  895. {
  896. int status;
  897. unsigned long flags;
  898. struct cdns_uart *cdns_uart = port->private_data;
  899. if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED)
  900. hrtimer_cancel(&cdns_uart->tx_timer);
  901. uart_port_lock_irqsave(port, &flags);
  902. /* Disable interrupts */
  903. status = readl(port->membase + CDNS_UART_IMR);
  904. writel(status, port->membase + CDNS_UART_IDR);
  905. writel(0xffffffff, port->membase + CDNS_UART_ISR);
  906. /* Disable the TX and RX */
  907. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  908. port->membase + CDNS_UART_CR);
  909. uart_port_unlock_irqrestore(port, flags);
  910. free_irq(port->irq, port);
  911. }
  912. /**
  913. * cdns_uart_type - Set UART type to cdns_uart port
  914. * @port: Handle to the uart port structure
  915. *
  916. * Return: string on success, NULL otherwise
  917. */
  918. static const char *cdns_uart_type(struct uart_port *port)
  919. {
  920. return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
  921. }
  922. /**
  923. * cdns_uart_verify_port - Verify the port params
  924. * @port: Handle to the uart port structure
  925. * @ser: Handle to the structure whose members are compared
  926. *
  927. * Return: 0 on success, negative errno otherwise.
  928. */
  929. static int cdns_uart_verify_port(struct uart_port *port,
  930. struct serial_struct *ser)
  931. {
  932. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  933. return -EINVAL;
  934. if (port->irq != ser->irq)
  935. return -EINVAL;
  936. if (ser->io_type != UPIO_MEM)
  937. return -EINVAL;
  938. if (port->iobase != ser->port)
  939. return -EINVAL;
  940. if (ser->hub6 != 0)
  941. return -EINVAL;
  942. return 0;
  943. }
  944. /**
  945. * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
  946. * called when the driver adds a cdns_uart port via
  947. * uart_add_one_port()
  948. * @port: Handle to the uart port structure
  949. *
  950. * Return: 0 on success, negative errno otherwise.
  951. */
  952. static int cdns_uart_request_port(struct uart_port *port)
  953. {
  954. if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
  955. CDNS_UART_NAME)) {
  956. return -ENOMEM;
  957. }
  958. port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
  959. if (!port->membase) {
  960. dev_err(port->dev, "Unable to map registers\n");
  961. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  962. return -ENOMEM;
  963. }
  964. return 0;
  965. }
  966. /**
  967. * cdns_uart_release_port - Release UART port
  968. * @port: Handle to the uart port structure
  969. *
  970. * Release the memory region attached to a cdns_uart port. Called when the
  971. * driver removes a cdns_uart port via uart_remove_one_port().
  972. */
  973. static void cdns_uart_release_port(struct uart_port *port)
  974. {
  975. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  976. iounmap(port->membase);
  977. port->membase = NULL;
  978. }
  979. /**
  980. * cdns_uart_config_port - Configure UART port
  981. * @port: Handle to the uart port structure
  982. * @flags: If any
  983. */
  984. static void cdns_uart_config_port(struct uart_port *port, int flags)
  985. {
  986. if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
  987. port->type = PORT_XUARTPS;
  988. }
  989. /**
  990. * cdns_uart_get_mctrl - Get the modem control state
  991. * @port: Handle to the uart port structure
  992. *
  993. * Return: the modem control state
  994. */
  995. static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
  996. {
  997. u32 val;
  998. unsigned int mctrl = 0;
  999. struct cdns_uart *cdns_uart_data = port->private_data;
  1000. if (cdns_uart_data->cts_override)
  1001. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  1002. val = readl(port->membase + CDNS_UART_MODEMSR);
  1003. if (val & CDNS_UART_MODEMSR_CTS)
  1004. mctrl |= TIOCM_CTS;
  1005. if (val & CDNS_UART_MODEMSR_DSR)
  1006. mctrl |= TIOCM_DSR;
  1007. if (val & CDNS_UART_MODEMSR_RI)
  1008. mctrl |= TIOCM_RNG;
  1009. if (val & CDNS_UART_MODEMSR_DCD)
  1010. mctrl |= TIOCM_CAR;
  1011. return mctrl;
  1012. }
  1013. static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1014. {
  1015. u32 val;
  1016. u32 mode_reg;
  1017. struct cdns_uart *cdns_uart_data = port->private_data;
  1018. if (cdns_uart_data->cts_override)
  1019. return;
  1020. val = readl(port->membase + CDNS_UART_MODEMCR);
  1021. mode_reg = readl(port->membase + CDNS_UART_MR);
  1022. val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
  1023. mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
  1024. if (mctrl & TIOCM_RTS)
  1025. val |= CDNS_UART_MODEMCR_RTS;
  1026. if (cdns_uart_data->gpiod_rts)
  1027. gpiod_set_value(cdns_uart_data->gpiod_rts, !(mctrl & TIOCM_RTS));
  1028. if (mctrl & TIOCM_DTR)
  1029. val |= CDNS_UART_MODEMCR_DTR;
  1030. if (mctrl & TIOCM_LOOP)
  1031. mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
  1032. else
  1033. mode_reg |= CDNS_UART_MR_CHMODE_NORM;
  1034. writel(val, port->membase + CDNS_UART_MODEMCR);
  1035. writel(mode_reg, port->membase + CDNS_UART_MR);
  1036. }
  1037. #ifdef CONFIG_CONSOLE_POLL
  1038. static int cdns_uart_poll_get_char(struct uart_port *port)
  1039. {
  1040. int c;
  1041. unsigned long flags;
  1042. uart_port_lock_irqsave(port, &flags);
  1043. /* Check if FIFO is empty */
  1044. if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
  1045. c = NO_POLL_CHAR;
  1046. else /* Read a character */
  1047. c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
  1048. uart_port_unlock_irqrestore(port, flags);
  1049. return c;
  1050. }
  1051. static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
  1052. {
  1053. unsigned long flags;
  1054. uart_port_lock_irqsave(port, &flags);
  1055. /* Wait until FIFO is empty */
  1056. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  1057. cpu_relax();
  1058. /* Write a character */
  1059. writel(c, port->membase + CDNS_UART_FIFO);
  1060. /* Wait until FIFO is empty */
  1061. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  1062. cpu_relax();
  1063. uart_port_unlock_irqrestore(port, flags);
  1064. }
  1065. #endif
  1066. static void cdns_uart_pm(struct uart_port *port, unsigned int state,
  1067. unsigned int oldstate)
  1068. {
  1069. switch (state) {
  1070. case UART_PM_STATE_OFF:
  1071. pm_runtime_mark_last_busy(port->dev);
  1072. pm_runtime_put_autosuspend(port->dev);
  1073. break;
  1074. default:
  1075. pm_runtime_get_sync(port->dev);
  1076. break;
  1077. }
  1078. }
  1079. static const struct uart_ops cdns_uart_ops = {
  1080. .set_mctrl = cdns_uart_set_mctrl,
  1081. .get_mctrl = cdns_uart_get_mctrl,
  1082. .start_tx = cdns_uart_start_tx,
  1083. .stop_tx = cdns_uart_stop_tx,
  1084. .stop_rx = cdns_uart_stop_rx,
  1085. .tx_empty = cdns_uart_tx_empty,
  1086. .break_ctl = cdns_uart_break_ctl,
  1087. .set_termios = cdns_uart_set_termios,
  1088. .startup = cdns_uart_startup,
  1089. .shutdown = cdns_uart_shutdown,
  1090. .pm = cdns_uart_pm,
  1091. .type = cdns_uart_type,
  1092. .verify_port = cdns_uart_verify_port,
  1093. .request_port = cdns_uart_request_port,
  1094. .release_port = cdns_uart_release_port,
  1095. .config_port = cdns_uart_config_port,
  1096. #ifdef CONFIG_CONSOLE_POLL
  1097. .poll_get_char = cdns_uart_poll_get_char,
  1098. .poll_put_char = cdns_uart_poll_put_char,
  1099. #endif
  1100. };
  1101. static struct uart_driver cdns_uart_uart_driver;
  1102. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1103. /**
  1104. * cdns_uart_console_putchar - write the character to the FIFO buffer
  1105. * @port: Handle to the uart port structure
  1106. * @ch: Character to be written
  1107. */
  1108. static void cdns_uart_console_putchar(struct uart_port *port, unsigned char ch)
  1109. {
  1110. unsigned int ctrl_reg;
  1111. unsigned long timeout;
  1112. timeout = jiffies + msecs_to_jiffies(1000);
  1113. while (1) {
  1114. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1115. if (!(ctrl_reg & CDNS_UART_CR_TX_DIS))
  1116. break;
  1117. if (time_after(jiffies, timeout)) {
  1118. dev_warn(port->dev,
  1119. "timeout waiting for Enable\n");
  1120. return;
  1121. }
  1122. cpu_relax();
  1123. }
  1124. timeout = jiffies + msecs_to_jiffies(1000);
  1125. while (1) {
  1126. ctrl_reg = readl(port->membase + CDNS_UART_SR);
  1127. if (!(ctrl_reg & CDNS_UART_SR_TXFULL))
  1128. break;
  1129. if (time_after(jiffies, timeout)) {
  1130. dev_warn(port->dev,
  1131. "timeout waiting for TX fifo\n");
  1132. return;
  1133. }
  1134. cpu_relax();
  1135. }
  1136. writel(ch, port->membase + CDNS_UART_FIFO);
  1137. }
  1138. static void cdns_early_write(struct console *con, const char *s,
  1139. unsigned int n)
  1140. {
  1141. struct earlycon_device *dev = con->data;
  1142. uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
  1143. }
  1144. static int __init cdns_early_console_setup(struct earlycon_device *device,
  1145. const char *opt)
  1146. {
  1147. struct uart_port *port = &device->port;
  1148. if (!port->membase)
  1149. return -ENODEV;
  1150. /* initialise control register */
  1151. writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
  1152. port->membase + CDNS_UART_CR);
  1153. /* only set baud if specified on command line - otherwise
  1154. * assume it has been initialized by a boot loader.
  1155. */
  1156. if (port->uartclk && device->baud) {
  1157. u32 cd = 0, bdiv = 0;
  1158. u32 mr;
  1159. int div8;
  1160. cdns_uart_calc_baud_divs(port->uartclk, device->baud,
  1161. &bdiv, &cd, &div8);
  1162. mr = CDNS_UART_MR_PARITY_NONE;
  1163. if (div8)
  1164. mr |= CDNS_UART_MR_CLKSEL;
  1165. writel(mr, port->membase + CDNS_UART_MR);
  1166. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  1167. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  1168. }
  1169. device->con->write = cdns_early_write;
  1170. return 0;
  1171. }
  1172. OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
  1173. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
  1174. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
  1175. OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
  1176. /* Static pointer to console port */
  1177. static struct uart_port *console_port;
  1178. /**
  1179. * cdns_uart_console_write - perform write operation
  1180. * @co: Console handle
  1181. * @s: Pointer to character array
  1182. * @count: No of characters
  1183. */
  1184. static void cdns_uart_console_write(struct console *co, const char *s,
  1185. unsigned int count)
  1186. {
  1187. struct uart_port *port = console_port;
  1188. unsigned long flags;
  1189. unsigned int imr, ctrl;
  1190. int locked = 1;
  1191. if (oops_in_progress)
  1192. locked = uart_port_trylock_irqsave(port, &flags);
  1193. else
  1194. uart_port_lock_irqsave(port, &flags);
  1195. /* save and disable interrupt */
  1196. imr = readl(port->membase + CDNS_UART_IMR);
  1197. writel(imr, port->membase + CDNS_UART_IDR);
  1198. /*
  1199. * Make sure that the tx part is enabled. Set the TX enable bit and
  1200. * clear the TX disable bit to enable the transmitter.
  1201. */
  1202. ctrl = readl(port->membase + CDNS_UART_CR);
  1203. ctrl &= ~CDNS_UART_CR_TX_DIS;
  1204. ctrl |= CDNS_UART_CR_TX_EN;
  1205. writel(ctrl, port->membase + CDNS_UART_CR);
  1206. uart_console_write(port, s, count, cdns_uart_console_putchar);
  1207. while (cdns_uart_tx_empty(port) != TIOCSER_TEMT)
  1208. cpu_relax();
  1209. /* restore interrupt state */
  1210. writel(imr, port->membase + CDNS_UART_IER);
  1211. if (locked)
  1212. uart_port_unlock_irqrestore(port, flags);
  1213. }
  1214. /**
  1215. * cdns_uart_console_setup - Initialize the uart to default config
  1216. * @co: Console handle
  1217. * @options: Initial settings of uart
  1218. *
  1219. * Return: 0 on success, negative errno otherwise.
  1220. */
  1221. static int cdns_uart_console_setup(struct console *co, char *options)
  1222. {
  1223. struct uart_port *port = console_port;
  1224. int baud = 9600;
  1225. int bits = 8;
  1226. int parity = 'n';
  1227. int flow = 'n';
  1228. unsigned long time_out;
  1229. if (!port->membase) {
  1230. pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
  1231. co->index);
  1232. return -ENODEV;
  1233. }
  1234. if (options)
  1235. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1236. /* Wait for tx_empty before setting up the console */
  1237. time_out = jiffies + usecs_to_jiffies(TX_TIMEOUT);
  1238. while (time_before(jiffies, time_out) &&
  1239. cdns_uart_tx_empty(port) != TIOCSER_TEMT)
  1240. cpu_relax();
  1241. return uart_set_options(port, co, baud, parity, bits, flow);
  1242. }
  1243. static struct console cdns_uart_console = {
  1244. .name = CDNS_UART_TTY_NAME,
  1245. .write = cdns_uart_console_write,
  1246. .device = uart_console_device,
  1247. .setup = cdns_uart_console_setup,
  1248. .flags = CON_PRINTBUFFER,
  1249. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  1250. .data = &cdns_uart_uart_driver,
  1251. };
  1252. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1253. #ifdef CONFIG_PM_SLEEP
  1254. /**
  1255. * cdns_uart_suspend - suspend event
  1256. * @device: Pointer to the device structure
  1257. *
  1258. * Return: 0
  1259. */
  1260. static int cdns_uart_suspend(struct device *device)
  1261. {
  1262. struct uart_port *port = dev_get_drvdata(device);
  1263. struct cdns_uart *cdns_uart = port->private_data;
  1264. int may_wake;
  1265. may_wake = device_may_wakeup(device);
  1266. if (console_suspend_enabled && uart_console(port) && may_wake) {
  1267. unsigned long flags;
  1268. uart_port_lock_irqsave(port, &flags);
  1269. /* Empty the receive FIFO 1st before making changes */
  1270. while (!(readl(port->membase + CDNS_UART_SR) &
  1271. CDNS_UART_SR_RXEMPTY))
  1272. readl(port->membase + CDNS_UART_FIFO);
  1273. /* set RX trigger level to 1 */
  1274. writel(1, port->membase + CDNS_UART_RXWM);
  1275. /* disable RX timeout interrups */
  1276. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
  1277. uart_port_unlock_irqrestore(port, flags);
  1278. }
  1279. /*
  1280. * Call the API provided in serial_core.c file which handles
  1281. * the suspend.
  1282. */
  1283. return uart_suspend_port(cdns_uart->cdns_uart_driver, port);
  1284. }
  1285. /**
  1286. * cdns_uart_resume - Resume after a previous suspend
  1287. * @device: Pointer to the device structure
  1288. *
  1289. * Return: 0
  1290. */
  1291. static int cdns_uart_resume(struct device *device)
  1292. {
  1293. struct uart_port *port = dev_get_drvdata(device);
  1294. struct cdns_uart *cdns_uart = port->private_data;
  1295. unsigned long flags;
  1296. u32 ctrl_reg;
  1297. int may_wake;
  1298. int ret;
  1299. may_wake = device_may_wakeup(device);
  1300. if (console_suspend_enabled && uart_console(port) && !may_wake) {
  1301. ret = clk_enable(cdns_uart->pclk);
  1302. if (ret)
  1303. return ret;
  1304. ret = clk_enable(cdns_uart->uartclk);
  1305. if (ret) {
  1306. clk_disable(cdns_uart->pclk);
  1307. return ret;
  1308. }
  1309. uart_port_lock_irqsave(port, &flags);
  1310. /* Set TX/RX Reset */
  1311. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1312. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  1313. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1314. while (readl(port->membase + CDNS_UART_CR) &
  1315. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  1316. cpu_relax();
  1317. /* restore rx timeout value */
  1318. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  1319. /* Enable Tx/Rx */
  1320. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1321. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  1322. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  1323. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1324. clk_disable(cdns_uart->uartclk);
  1325. clk_disable(cdns_uart->pclk);
  1326. uart_port_unlock_irqrestore(port, flags);
  1327. } else {
  1328. uart_port_lock_irqsave(port, &flags);
  1329. /* restore original rx trigger level */
  1330. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  1331. /* enable RX timeout interrupt */
  1332. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
  1333. uart_port_unlock_irqrestore(port, flags);
  1334. }
  1335. return uart_resume_port(cdns_uart->cdns_uart_driver, port);
  1336. }
  1337. #endif /* ! CONFIG_PM_SLEEP */
  1338. static int __maybe_unused cdns_runtime_suspend(struct device *dev)
  1339. {
  1340. struct uart_port *port = dev_get_drvdata(dev);
  1341. struct cdns_uart *cdns_uart = port->private_data;
  1342. clk_disable(cdns_uart->uartclk);
  1343. clk_disable(cdns_uart->pclk);
  1344. return 0;
  1345. };
  1346. static int __maybe_unused cdns_runtime_resume(struct device *dev)
  1347. {
  1348. struct uart_port *port = dev_get_drvdata(dev);
  1349. struct cdns_uart *cdns_uart = port->private_data;
  1350. int ret;
  1351. ret = clk_enable(cdns_uart->pclk);
  1352. if (ret)
  1353. return ret;
  1354. ret = clk_enable(cdns_uart->uartclk);
  1355. if (ret) {
  1356. clk_disable(cdns_uart->pclk);
  1357. return ret;
  1358. }
  1359. return 0;
  1360. };
  1361. static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
  1362. SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
  1363. SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
  1364. cdns_runtime_resume, NULL)
  1365. };
  1366. static const struct cdns_platform_data zynqmp_uart_def = {
  1367. .quirks = CDNS_UART_RXBS_SUPPORT, };
  1368. /* Match table for of_platform binding */
  1369. static const struct of_device_id cdns_uart_of_match[] = {
  1370. { .compatible = "xlnx,xuartps", },
  1371. { .compatible = "cdns,uart-r1p8", },
  1372. { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
  1373. { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
  1374. {}
  1375. };
  1376. MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
  1377. /* Temporary variable for storing number of instances */
  1378. static int instances;
  1379. /**
  1380. * cdns_rs485_config - Called when an application calls TIOCSRS485 ioctl.
  1381. * @port: Pointer to the uart_port structure
  1382. * @termios: Pointer to the ktermios structure
  1383. * @rs485: Pointer to the serial_rs485 structure
  1384. *
  1385. * Return: 0
  1386. */
  1387. static int cdns_rs485_config(struct uart_port *port, struct ktermios *termios,
  1388. struct serial_rs485 *rs485)
  1389. {
  1390. u32 val;
  1391. struct cdns_uart *cdns_uart = port->private_data;
  1392. if (rs485->flags & SER_RS485_ENABLED) {
  1393. dev_dbg(port->dev, "Setting UART to RS485\n");
  1394. /* Make sure auto RTS is disabled */
  1395. val = readl(port->membase + CDNS_UART_MODEMCR);
  1396. val &= ~CDNS_UART_MODEMCR_FCM;
  1397. writel(val, port->membase + CDNS_UART_MODEMCR);
  1398. /* Timer setup */
  1399. hrtimer_init(&cdns_uart->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1400. cdns_uart->tx_timer.function = &cdns_rs485_tx_callback;
  1401. /* Disable transmitter and make Rx setup*/
  1402. cdns_uart_stop_tx(port);
  1403. } else {
  1404. hrtimer_cancel(&cdns_uart->tx_timer);
  1405. }
  1406. return 0;
  1407. }
  1408. /**
  1409. * cdns_uart_probe - Platform driver probe
  1410. * @pdev: Pointer to the platform device structure
  1411. *
  1412. * Return: 0 on success, negative errno otherwise
  1413. */
  1414. static int cdns_uart_probe(struct platform_device *pdev)
  1415. {
  1416. int rc, id, irq;
  1417. struct uart_port *port;
  1418. struct resource *res;
  1419. struct cdns_uart *cdns_uart_data;
  1420. const struct of_device_id *match;
  1421. cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
  1422. GFP_KERNEL);
  1423. if (!cdns_uart_data)
  1424. return -ENOMEM;
  1425. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  1426. if (!port)
  1427. return -ENOMEM;
  1428. /* Look for a serialN alias */
  1429. id = of_alias_get_id(pdev->dev.of_node, "serial");
  1430. if (id < 0)
  1431. id = 0;
  1432. if (id >= CDNS_UART_NR_PORTS) {
  1433. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1434. return -ENODEV;
  1435. }
  1436. if (!cdns_uart_uart_driver.state) {
  1437. cdns_uart_uart_driver.owner = THIS_MODULE;
  1438. cdns_uart_uart_driver.driver_name = CDNS_UART_NAME;
  1439. cdns_uart_uart_driver.dev_name = CDNS_UART_TTY_NAME;
  1440. cdns_uart_uart_driver.major = CDNS_UART_MAJOR;
  1441. cdns_uart_uart_driver.minor = CDNS_UART_MINOR;
  1442. cdns_uart_uart_driver.nr = CDNS_UART_NR_PORTS;
  1443. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1444. cdns_uart_uart_driver.cons = &cdns_uart_console;
  1445. #endif
  1446. rc = uart_register_driver(&cdns_uart_uart_driver);
  1447. if (rc < 0) {
  1448. dev_err(&pdev->dev, "Failed to register driver\n");
  1449. return rc;
  1450. }
  1451. }
  1452. cdns_uart_data->cdns_uart_driver = &cdns_uart_uart_driver;
  1453. match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
  1454. if (match && match->data) {
  1455. const struct cdns_platform_data *data = match->data;
  1456. cdns_uart_data->quirks = data->quirks;
  1457. }
  1458. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
  1459. if (PTR_ERR(cdns_uart_data->pclk) == -EPROBE_DEFER) {
  1460. rc = PTR_ERR(cdns_uart_data->pclk);
  1461. goto err_out_unregister_driver;
  1462. }
  1463. if (IS_ERR(cdns_uart_data->pclk)) {
  1464. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
  1465. if (IS_ERR(cdns_uart_data->pclk)) {
  1466. rc = PTR_ERR(cdns_uart_data->pclk);
  1467. goto err_out_unregister_driver;
  1468. }
  1469. dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
  1470. }
  1471. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
  1472. if (PTR_ERR(cdns_uart_data->uartclk) == -EPROBE_DEFER) {
  1473. rc = PTR_ERR(cdns_uart_data->uartclk);
  1474. goto err_out_unregister_driver;
  1475. }
  1476. if (IS_ERR(cdns_uart_data->uartclk)) {
  1477. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
  1478. if (IS_ERR(cdns_uart_data->uartclk)) {
  1479. rc = PTR_ERR(cdns_uart_data->uartclk);
  1480. goto err_out_unregister_driver;
  1481. }
  1482. dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
  1483. }
  1484. cdns_uart_data->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
  1485. if (IS_ERR(cdns_uart_data->rstc)) {
  1486. rc = PTR_ERR(cdns_uart_data->rstc);
  1487. dev_err_probe(&pdev->dev, rc, "Cannot get UART reset\n");
  1488. goto err_out_unregister_driver;
  1489. }
  1490. rc = clk_prepare_enable(cdns_uart_data->pclk);
  1491. if (rc) {
  1492. dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
  1493. goto err_out_unregister_driver;
  1494. }
  1495. rc = clk_prepare_enable(cdns_uart_data->uartclk);
  1496. if (rc) {
  1497. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1498. goto err_out_clk_dis_pclk;
  1499. }
  1500. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1501. if (!res) {
  1502. rc = -ENODEV;
  1503. goto err_out_clk_disable;
  1504. }
  1505. irq = platform_get_irq(pdev, 0);
  1506. if (irq < 0) {
  1507. rc = irq;
  1508. goto err_out_clk_disable;
  1509. }
  1510. #ifdef CONFIG_COMMON_CLK
  1511. cdns_uart_data->clk_rate_change_nb.notifier_call =
  1512. cdns_uart_clk_notifier_cb;
  1513. if (clk_notifier_register(cdns_uart_data->uartclk,
  1514. &cdns_uart_data->clk_rate_change_nb))
  1515. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1516. #endif
  1517. /* At this point, we've got an empty uart_port struct, initialize it */
  1518. spin_lock_init(&port->lock);
  1519. port->type = PORT_UNKNOWN;
  1520. port->iotype = UPIO_MEM32;
  1521. port->flags = UPF_BOOT_AUTOCONF;
  1522. port->ops = &cdns_uart_ops;
  1523. port->fifosize = CDNS_UART_FIFO_SIZE;
  1524. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE);
  1525. port->line = id;
  1526. /*
  1527. * Register the port.
  1528. * This function also registers this device with the tty layer
  1529. * and triggers invocation of the config_port() entry point.
  1530. */
  1531. port->mapbase = res->start;
  1532. port->irq = irq;
  1533. port->dev = &pdev->dev;
  1534. port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
  1535. port->private_data = cdns_uart_data;
  1536. port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
  1537. CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
  1538. port->rs485_config = cdns_rs485_config;
  1539. port->rs485_supported = cdns_rs485_supported;
  1540. cdns_uart_data->port = port;
  1541. platform_set_drvdata(pdev, port);
  1542. rc = uart_get_rs485_mode(port);
  1543. if (rc)
  1544. goto err_out_clk_notifier;
  1545. cdns_uart_data->gpiod_rts = devm_gpiod_get_optional(&pdev->dev, "rts",
  1546. GPIOD_OUT_LOW);
  1547. if (IS_ERR(cdns_uart_data->gpiod_rts)) {
  1548. rc = PTR_ERR(cdns_uart_data->gpiod_rts);
  1549. dev_err(port->dev, "xuartps: devm_gpiod_get_optional failed\n");
  1550. goto err_out_clk_notifier;
  1551. }
  1552. pm_runtime_use_autosuspend(&pdev->dev);
  1553. pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
  1554. pm_runtime_set_active(&pdev->dev);
  1555. pm_runtime_enable(&pdev->dev);
  1556. device_init_wakeup(port->dev, true);
  1557. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1558. /*
  1559. * If console hasn't been found yet try to assign this port
  1560. * because it is required to be assigned for console setup function.
  1561. * If register_console() don't assign value, then console_port pointer
  1562. * is cleanup.
  1563. */
  1564. if (!console_port) {
  1565. cdns_uart_console.index = id;
  1566. console_port = port;
  1567. }
  1568. #endif
  1569. if (cdns_uart_data->port->rs485.flags & SER_RS485_ENABLED)
  1570. cdns_rs485_rx_setup(cdns_uart_data);
  1571. rc = uart_add_one_port(&cdns_uart_uart_driver, port);
  1572. if (rc) {
  1573. dev_err(&pdev->dev,
  1574. "uart_add_one_port() failed; err=%i\n", rc);
  1575. goto err_out_pm_disable;
  1576. }
  1577. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1578. /* This is not port which is used for console that's why clean it up */
  1579. if (console_port == port &&
  1580. !console_is_registered(cdns_uart_uart_driver.cons)) {
  1581. console_port = NULL;
  1582. cdns_uart_console.index = -1;
  1583. }
  1584. #endif
  1585. cdns_uart_data->cts_override = of_property_read_bool(pdev->dev.of_node,
  1586. "cts-override");
  1587. instances++;
  1588. return 0;
  1589. err_out_pm_disable:
  1590. pm_runtime_disable(&pdev->dev);
  1591. pm_runtime_set_suspended(&pdev->dev);
  1592. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1593. err_out_clk_notifier:
  1594. #ifdef CONFIG_COMMON_CLK
  1595. clk_notifier_unregister(cdns_uart_data->uartclk,
  1596. &cdns_uart_data->clk_rate_change_nb);
  1597. #endif
  1598. err_out_clk_disable:
  1599. clk_disable_unprepare(cdns_uart_data->uartclk);
  1600. err_out_clk_dis_pclk:
  1601. clk_disable_unprepare(cdns_uart_data->pclk);
  1602. err_out_unregister_driver:
  1603. if (!instances)
  1604. uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
  1605. return rc;
  1606. }
  1607. /**
  1608. * cdns_uart_remove - called when the platform driver is unregistered
  1609. * @pdev: Pointer to the platform device structure
  1610. */
  1611. static void cdns_uart_remove(struct platform_device *pdev)
  1612. {
  1613. struct uart_port *port = platform_get_drvdata(pdev);
  1614. struct cdns_uart *cdns_uart_data = port->private_data;
  1615. /* Remove the cdns_uart port from the serial core */
  1616. #ifdef CONFIG_COMMON_CLK
  1617. clk_notifier_unregister(cdns_uart_data->uartclk,
  1618. &cdns_uart_data->clk_rate_change_nb);
  1619. #endif
  1620. uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port);
  1621. port->mapbase = 0;
  1622. clk_disable_unprepare(cdns_uart_data->uartclk);
  1623. clk_disable_unprepare(cdns_uart_data->pclk);
  1624. pm_runtime_disable(&pdev->dev);
  1625. pm_runtime_set_suspended(&pdev->dev);
  1626. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1627. device_init_wakeup(&pdev->dev, false);
  1628. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1629. if (console_port == port)
  1630. console_port = NULL;
  1631. #endif
  1632. reset_control_assert(cdns_uart_data->rstc);
  1633. if (!--instances)
  1634. uart_unregister_driver(cdns_uart_data->cdns_uart_driver);
  1635. }
  1636. static struct platform_driver cdns_uart_platform_driver = {
  1637. .probe = cdns_uart_probe,
  1638. .remove_new = cdns_uart_remove,
  1639. .driver = {
  1640. .name = CDNS_UART_NAME,
  1641. .of_match_table = cdns_uart_of_match,
  1642. .pm = &cdns_uart_dev_pm_ops,
  1643. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_XILINX_PS_UART),
  1644. },
  1645. };
  1646. static int __init cdns_uart_init(void)
  1647. {
  1648. /* Register the platform driver */
  1649. return platform_driver_register(&cdns_uart_platform_driver);
  1650. }
  1651. static void __exit cdns_uart_exit(void)
  1652. {
  1653. /* Unregister the platform driver */
  1654. platform_driver_unregister(&cdns_uart_platform_driver);
  1655. }
  1656. arch_initcall(cdns_uart_init);
  1657. module_exit(cdns_uart_exit);
  1658. MODULE_DESCRIPTION("Driver for Cadence UART");
  1659. MODULE_AUTHOR("Xilinx Inc.");
  1660. MODULE_LICENSE("GPL");