xilinx_uartps.c 47 KB

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