xilinx_uartps.c 46 KB

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