xilinx_uartps.c 55 KB

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