xilinx_uartps.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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_MASK 0x00001FFF /* Valid 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. if (isrstatus & CDNS_UART_IXR_MASK)
  330. cdns_uart_handle_rx(dev_id, isrstatus);
  331. spin_unlock(&port->lock);
  332. return IRQ_HANDLED;
  333. }
  334. /**
  335. * cdns_uart_calc_baud_divs - Calculate baud rate divisors
  336. * @clk: UART module input clock
  337. * @baud: Desired baud rate
  338. * @rbdiv: BDIV value (return value)
  339. * @rcd: CD value (return value)
  340. * @div8: Value for clk_sel bit in mod (return value)
  341. * Return: baud rate, requested baud when possible, or actual baud when there
  342. * was too much error, zero if no valid divisors are found.
  343. *
  344. * Formula to obtain baud rate is
  345. * baud_tx/rx rate = clk/CD * (BDIV + 1)
  346. * input_clk = (Uart User Defined Clock or Apb Clock)
  347. * depends on UCLKEN in MR Reg
  348. * clk = input_clk or input_clk/8;
  349. * depends on CLKS in MR reg
  350. * CD and BDIV depends on values in
  351. * baud rate generate register
  352. * baud rate clock divisor register
  353. */
  354. static unsigned int cdns_uart_calc_baud_divs(unsigned int clk,
  355. unsigned int baud, u32 *rbdiv, u32 *rcd, int *div8)
  356. {
  357. u32 cd, bdiv;
  358. unsigned int calc_baud;
  359. unsigned int bestbaud = 0;
  360. unsigned int bauderror;
  361. unsigned int besterror = ~0;
  362. if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) {
  363. *div8 = 1;
  364. clk /= 8;
  365. } else {
  366. *div8 = 0;
  367. }
  368. for (bdiv = CDNS_UART_BDIV_MIN; bdiv <= CDNS_UART_BDIV_MAX; bdiv++) {
  369. cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
  370. if (cd < 1 || cd > CDNS_UART_CD_MAX)
  371. continue;
  372. calc_baud = clk / (cd * (bdiv + 1));
  373. if (baud > calc_baud)
  374. bauderror = baud - calc_baud;
  375. else
  376. bauderror = calc_baud - baud;
  377. if (besterror > bauderror) {
  378. *rbdiv = bdiv;
  379. *rcd = cd;
  380. bestbaud = calc_baud;
  381. besterror = bauderror;
  382. }
  383. }
  384. /* use the values when percent error is acceptable */
  385. if (((besterror * 100) / baud) < 3)
  386. bestbaud = baud;
  387. return bestbaud;
  388. }
  389. /**
  390. * cdns_uart_set_baud_rate - Calculate and set the baud rate
  391. * @port: Handle to the uart port structure
  392. * @baud: Baud rate to set
  393. * Return: baud rate, requested baud when possible, or actual baud when there
  394. * was too much error, zero if no valid divisors are found.
  395. */
  396. static unsigned int cdns_uart_set_baud_rate(struct uart_port *port,
  397. unsigned int baud)
  398. {
  399. unsigned int calc_baud;
  400. u32 cd = 0, bdiv = 0;
  401. u32 mreg;
  402. int div8;
  403. struct cdns_uart *cdns_uart = port->private_data;
  404. calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
  405. &div8);
  406. /* Write new divisors to hardware */
  407. mreg = readl(port->membase + CDNS_UART_MR);
  408. if (div8)
  409. mreg |= CDNS_UART_MR_CLKSEL;
  410. else
  411. mreg &= ~CDNS_UART_MR_CLKSEL;
  412. writel(mreg, port->membase + CDNS_UART_MR);
  413. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  414. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  415. cdns_uart->baud = baud;
  416. return calc_baud;
  417. }
  418. #ifdef CONFIG_COMMON_CLK
  419. /**
  420. * cdns_uart_clk_notitifer_cb - Clock notifier callback
  421. * @nb: Notifier block
  422. * @event: Notify event
  423. * @data: Notifier data
  424. * Return: NOTIFY_OK or NOTIFY_DONE on success, NOTIFY_BAD on error.
  425. */
  426. static int cdns_uart_clk_notifier_cb(struct notifier_block *nb,
  427. unsigned long event, void *data)
  428. {
  429. u32 ctrl_reg;
  430. struct uart_port *port;
  431. int locked = 0;
  432. struct clk_notifier_data *ndata = data;
  433. unsigned long flags = 0;
  434. struct cdns_uart *cdns_uart = to_cdns_uart(nb);
  435. port = cdns_uart->port;
  436. if (port->suspended)
  437. return NOTIFY_OK;
  438. switch (event) {
  439. case PRE_RATE_CHANGE:
  440. {
  441. u32 bdiv, cd;
  442. int div8;
  443. /*
  444. * Find out if current baud-rate can be achieved with new clock
  445. * frequency.
  446. */
  447. if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud,
  448. &bdiv, &cd, &div8)) {
  449. dev_warn(port->dev, "clock rate change rejected\n");
  450. return NOTIFY_BAD;
  451. }
  452. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  453. /* Disable the TX and RX to set baud rate */
  454. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  455. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  456. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  457. spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
  458. return NOTIFY_OK;
  459. }
  460. case POST_RATE_CHANGE:
  461. /*
  462. * Set clk dividers to generate correct baud with new clock
  463. * frequency.
  464. */
  465. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  466. locked = 1;
  467. port->uartclk = ndata->new_rate;
  468. cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port,
  469. cdns_uart->baud);
  470. /* fall through */
  471. case ABORT_RATE_CHANGE:
  472. if (!locked)
  473. spin_lock_irqsave(&cdns_uart->port->lock, flags);
  474. /* Set TX/RX Reset */
  475. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  476. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  477. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  478. while (readl(port->membase + CDNS_UART_CR) &
  479. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  480. cpu_relax();
  481. /*
  482. * Clear the RX disable and TX disable bits and then set the TX
  483. * enable bit and RX enable bit to enable the transmitter and
  484. * receiver.
  485. */
  486. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  487. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  488. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  489. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  490. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  491. spin_unlock_irqrestore(&cdns_uart->port->lock, flags);
  492. return NOTIFY_OK;
  493. default:
  494. return NOTIFY_DONE;
  495. }
  496. }
  497. #endif
  498. /**
  499. * cdns_uart_start_tx - Start transmitting bytes
  500. * @port: Handle to the uart port structure
  501. */
  502. static void cdns_uart_start_tx(struct uart_port *port)
  503. {
  504. unsigned int status;
  505. if (uart_tx_stopped(port))
  506. return;
  507. /*
  508. * Set the TX enable bit and clear the TX disable bit to enable the
  509. * transmitter.
  510. */
  511. status = readl(port->membase + CDNS_UART_CR);
  512. status &= ~CDNS_UART_CR_TX_DIS;
  513. status |= CDNS_UART_CR_TX_EN;
  514. writel(status, port->membase + CDNS_UART_CR);
  515. if (uart_circ_empty(&port->state->xmit))
  516. return;
  517. cdns_uart_handle_tx(port);
  518. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR);
  519. /* Enable the TX Empty interrupt */
  520. writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IER);
  521. }
  522. /**
  523. * cdns_uart_stop_tx - Stop TX
  524. * @port: Handle to the uart port structure
  525. */
  526. static void cdns_uart_stop_tx(struct uart_port *port)
  527. {
  528. unsigned int regval;
  529. regval = readl(port->membase + CDNS_UART_CR);
  530. regval |= CDNS_UART_CR_TX_DIS;
  531. /* Disable the transmitter */
  532. writel(regval, port->membase + CDNS_UART_CR);
  533. }
  534. /**
  535. * cdns_uart_stop_rx - Stop RX
  536. * @port: Handle to the uart port structure
  537. */
  538. static void cdns_uart_stop_rx(struct uart_port *port)
  539. {
  540. unsigned int regval;
  541. /* Disable RX IRQs */
  542. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR);
  543. /* Disable the receiver */
  544. regval = readl(port->membase + CDNS_UART_CR);
  545. regval |= CDNS_UART_CR_RX_DIS;
  546. writel(regval, port->membase + CDNS_UART_CR);
  547. }
  548. /**
  549. * cdns_uart_tx_empty - Check whether TX is empty
  550. * @port: Handle to the uart port structure
  551. *
  552. * Return: TIOCSER_TEMT on success, 0 otherwise
  553. */
  554. static unsigned int cdns_uart_tx_empty(struct uart_port *port)
  555. {
  556. unsigned int status;
  557. status = readl(port->membase + CDNS_UART_SR) &
  558. CDNS_UART_SR_TXEMPTY;
  559. return status ? TIOCSER_TEMT : 0;
  560. }
  561. /**
  562. * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
  563. * transmitting char breaks
  564. * @port: Handle to the uart port structure
  565. * @ctl: Value based on which start or stop decision is taken
  566. */
  567. static void cdns_uart_break_ctl(struct uart_port *port, int ctl)
  568. {
  569. unsigned int status;
  570. unsigned long flags;
  571. spin_lock_irqsave(&port->lock, flags);
  572. status = readl(port->membase + CDNS_UART_CR);
  573. if (ctl == -1)
  574. writel(CDNS_UART_CR_STARTBRK | status,
  575. port->membase + CDNS_UART_CR);
  576. else {
  577. if ((status & CDNS_UART_CR_STOPBRK) == 0)
  578. writel(CDNS_UART_CR_STOPBRK | status,
  579. port->membase + CDNS_UART_CR);
  580. }
  581. spin_unlock_irqrestore(&port->lock, flags);
  582. }
  583. /**
  584. * cdns_uart_set_termios - termios operations, handling data length, parity,
  585. * stop bits, flow control, baud rate
  586. * @port: Handle to the uart port structure
  587. * @termios: Handle to the input termios structure
  588. * @old: Values of the previously saved termios structure
  589. */
  590. static void cdns_uart_set_termios(struct uart_port *port,
  591. struct ktermios *termios, struct ktermios *old)
  592. {
  593. unsigned int cval = 0;
  594. unsigned int baud, minbaud, maxbaud;
  595. unsigned long flags;
  596. unsigned int ctrl_reg, mode_reg;
  597. spin_lock_irqsave(&port->lock, flags);
  598. /* Wait for the transmit FIFO to empty before making changes */
  599. if (!(readl(port->membase + CDNS_UART_CR) &
  600. CDNS_UART_CR_TX_DIS)) {
  601. while (!(readl(port->membase + CDNS_UART_SR) &
  602. CDNS_UART_SR_TXEMPTY)) {
  603. cpu_relax();
  604. }
  605. }
  606. /* Disable the TX and RX to set baud rate */
  607. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  608. ctrl_reg |= CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS;
  609. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  610. /*
  611. * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
  612. * min and max baud should be calculated here based on port->uartclk.
  613. * this way we get a valid baud and can safely call set_baud()
  614. */
  615. minbaud = port->uartclk /
  616. ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8);
  617. maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1);
  618. baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
  619. baud = cdns_uart_set_baud_rate(port, baud);
  620. if (tty_termios_baud_rate(termios))
  621. tty_termios_encode_baud_rate(termios, baud, baud);
  622. /* Update the per-port timeout. */
  623. uart_update_timeout(port, termios->c_cflag, baud);
  624. /* Set TX/RX Reset */
  625. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  626. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  627. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  628. while (readl(port->membase + CDNS_UART_CR) &
  629. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  630. cpu_relax();
  631. /*
  632. * Clear the RX disable and TX disable bits and then set the TX enable
  633. * bit and RX enable bit to enable the transmitter and receiver.
  634. */
  635. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  636. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  637. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  638. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  639. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  640. port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG |
  641. CDNS_UART_IXR_OVERRUN | CDNS_UART_IXR_TOUT;
  642. port->ignore_status_mask = 0;
  643. if (termios->c_iflag & INPCK)
  644. port->read_status_mask |= CDNS_UART_IXR_PARITY |
  645. CDNS_UART_IXR_FRAMING;
  646. if (termios->c_iflag & IGNPAR)
  647. port->ignore_status_mask |= CDNS_UART_IXR_PARITY |
  648. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  649. /* ignore all characters if CREAD is not set */
  650. if ((termios->c_cflag & CREAD) == 0)
  651. port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG |
  652. CDNS_UART_IXR_TOUT | CDNS_UART_IXR_PARITY |
  653. CDNS_UART_IXR_FRAMING | CDNS_UART_IXR_OVERRUN;
  654. mode_reg = readl(port->membase + CDNS_UART_MR);
  655. /* Handling Data Size */
  656. switch (termios->c_cflag & CSIZE) {
  657. case CS6:
  658. cval |= CDNS_UART_MR_CHARLEN_6_BIT;
  659. break;
  660. case CS7:
  661. cval |= CDNS_UART_MR_CHARLEN_7_BIT;
  662. break;
  663. default:
  664. case CS8:
  665. cval |= CDNS_UART_MR_CHARLEN_8_BIT;
  666. termios->c_cflag &= ~CSIZE;
  667. termios->c_cflag |= CS8;
  668. break;
  669. }
  670. /* Handling Parity and Stop Bits length */
  671. if (termios->c_cflag & CSTOPB)
  672. cval |= CDNS_UART_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  673. else
  674. cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  675. if (termios->c_cflag & PARENB) {
  676. /* Mark or Space parity */
  677. if (termios->c_cflag & CMSPAR) {
  678. if (termios->c_cflag & PARODD)
  679. cval |= CDNS_UART_MR_PARITY_MARK;
  680. else
  681. cval |= CDNS_UART_MR_PARITY_SPACE;
  682. } else {
  683. if (termios->c_cflag & PARODD)
  684. cval |= CDNS_UART_MR_PARITY_ODD;
  685. else
  686. cval |= CDNS_UART_MR_PARITY_EVEN;
  687. }
  688. } else {
  689. cval |= CDNS_UART_MR_PARITY_NONE;
  690. }
  691. cval |= mode_reg & 1;
  692. writel(cval, port->membase + CDNS_UART_MR);
  693. spin_unlock_irqrestore(&port->lock, flags);
  694. }
  695. /**
  696. * cdns_uart_startup - Called when an application opens a cdns_uart port
  697. * @port: Handle to the uart port structure
  698. *
  699. * Return: 0 on success, negative errno otherwise
  700. */
  701. static int cdns_uart_startup(struct uart_port *port)
  702. {
  703. struct cdns_uart *cdns_uart = port->private_data;
  704. bool is_brk_support;
  705. int ret;
  706. unsigned long flags;
  707. unsigned int status = 0;
  708. is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT;
  709. spin_lock_irqsave(&port->lock, flags);
  710. /* Disable the TX and RX */
  711. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  712. port->membase + CDNS_UART_CR);
  713. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  714. * no break chars.
  715. */
  716. writel(CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST,
  717. port->membase + CDNS_UART_CR);
  718. while (readl(port->membase + CDNS_UART_CR) &
  719. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  720. cpu_relax();
  721. /*
  722. * Clear the RX disable bit and then set the RX enable bit to enable
  723. * the receiver.
  724. */
  725. status = readl(port->membase + CDNS_UART_CR);
  726. status &= CDNS_UART_CR_RX_DIS;
  727. status |= CDNS_UART_CR_RX_EN;
  728. writel(status, port->membase + CDNS_UART_CR);
  729. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  730. * no parity.
  731. */
  732. writel(CDNS_UART_MR_CHMODE_NORM | CDNS_UART_MR_STOPMODE_1_BIT
  733. | CDNS_UART_MR_PARITY_NONE | CDNS_UART_MR_CHARLEN_8_BIT,
  734. port->membase + CDNS_UART_MR);
  735. /*
  736. * Set the RX FIFO Trigger level to use most of the FIFO, but it
  737. * can be tuned with a module parameter
  738. */
  739. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  740. /*
  741. * Receive Timeout register is enabled but it
  742. * can be tuned with a module parameter
  743. */
  744. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  745. /* Clear out any pending interrupts before enabling them */
  746. writel(readl(port->membase + CDNS_UART_ISR),
  747. port->membase + CDNS_UART_ISR);
  748. spin_unlock_irqrestore(&port->lock, flags);
  749. ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port);
  750. if (ret) {
  751. dev_err(port->dev, "request_irq '%d' failed with %d\n",
  752. port->irq, ret);
  753. return ret;
  754. }
  755. /* Set the Interrupt Registers with desired interrupts */
  756. if (is_brk_support)
  757. writel(CDNS_UART_RX_IRQS | CDNS_UART_IXR_BRK,
  758. port->membase + CDNS_UART_IER);
  759. else
  760. writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER);
  761. return 0;
  762. }
  763. /**
  764. * cdns_uart_shutdown - Called when an application closes a cdns_uart port
  765. * @port: Handle to the uart port structure
  766. */
  767. static void cdns_uart_shutdown(struct uart_port *port)
  768. {
  769. int status;
  770. unsigned long flags;
  771. spin_lock_irqsave(&port->lock, flags);
  772. /* Disable interrupts */
  773. status = readl(port->membase + CDNS_UART_IMR);
  774. writel(status, port->membase + CDNS_UART_IDR);
  775. writel(0xffffffff, port->membase + CDNS_UART_ISR);
  776. /* Disable the TX and RX */
  777. writel(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS,
  778. port->membase + CDNS_UART_CR);
  779. spin_unlock_irqrestore(&port->lock, flags);
  780. free_irq(port->irq, port);
  781. }
  782. /**
  783. * cdns_uart_type - Set UART type to cdns_uart port
  784. * @port: Handle to the uart port structure
  785. *
  786. * Return: string on success, NULL otherwise
  787. */
  788. static const char *cdns_uart_type(struct uart_port *port)
  789. {
  790. return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL;
  791. }
  792. /**
  793. * cdns_uart_verify_port - Verify the port params
  794. * @port: Handle to the uart port structure
  795. * @ser: Handle to the structure whose members are compared
  796. *
  797. * Return: 0 on success, negative errno otherwise.
  798. */
  799. static int cdns_uart_verify_port(struct uart_port *port,
  800. struct serial_struct *ser)
  801. {
  802. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  803. return -EINVAL;
  804. if (port->irq != ser->irq)
  805. return -EINVAL;
  806. if (ser->io_type != UPIO_MEM)
  807. return -EINVAL;
  808. if (port->iobase != ser->port)
  809. return -EINVAL;
  810. if (ser->hub6 != 0)
  811. return -EINVAL;
  812. return 0;
  813. }
  814. /**
  815. * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
  816. * called when the driver adds a cdns_uart port via
  817. * uart_add_one_port()
  818. * @port: Handle to the uart port structure
  819. *
  820. * Return: 0 on success, negative errno otherwise.
  821. */
  822. static int cdns_uart_request_port(struct uart_port *port)
  823. {
  824. if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE,
  825. CDNS_UART_NAME)) {
  826. return -ENOMEM;
  827. }
  828. port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE);
  829. if (!port->membase) {
  830. dev_err(port->dev, "Unable to map registers\n");
  831. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  832. return -ENOMEM;
  833. }
  834. return 0;
  835. }
  836. /**
  837. * cdns_uart_release_port - Release UART port
  838. * @port: Handle to the uart port structure
  839. *
  840. * Release the memory region attached to a cdns_uart port. Called when the
  841. * driver removes a cdns_uart port via uart_remove_one_port().
  842. */
  843. static void cdns_uart_release_port(struct uart_port *port)
  844. {
  845. release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE);
  846. iounmap(port->membase);
  847. port->membase = NULL;
  848. }
  849. /**
  850. * cdns_uart_config_port - Configure UART port
  851. * @port: Handle to the uart port structure
  852. * @flags: If any
  853. */
  854. static void cdns_uart_config_port(struct uart_port *port, int flags)
  855. {
  856. if (flags & UART_CONFIG_TYPE && cdns_uart_request_port(port) == 0)
  857. port->type = PORT_XUARTPS;
  858. }
  859. /**
  860. * cdns_uart_get_mctrl - Get the modem control state
  861. * @port: Handle to the uart port structure
  862. *
  863. * Return: the modem control state
  864. */
  865. static unsigned int cdns_uart_get_mctrl(struct uart_port *port)
  866. {
  867. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  868. }
  869. static void cdns_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  870. {
  871. u32 val;
  872. val = readl(port->membase + CDNS_UART_MODEMCR);
  873. val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
  874. if (mctrl & TIOCM_RTS)
  875. val |= CDNS_UART_MODEMCR_RTS;
  876. if (mctrl & TIOCM_DTR)
  877. val |= CDNS_UART_MODEMCR_DTR;
  878. writel(val, port->membase + CDNS_UART_MODEMCR);
  879. }
  880. #ifdef CONFIG_CONSOLE_POLL
  881. static int cdns_uart_poll_get_char(struct uart_port *port)
  882. {
  883. int c;
  884. unsigned long flags;
  885. spin_lock_irqsave(&port->lock, flags);
  886. /* Check if FIFO is empty */
  887. if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
  888. c = NO_POLL_CHAR;
  889. else /* Read a character */
  890. c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
  891. spin_unlock_irqrestore(&port->lock, flags);
  892. return c;
  893. }
  894. static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
  895. {
  896. unsigned long flags;
  897. spin_lock_irqsave(&port->lock, flags);
  898. /* Wait until FIFO is empty */
  899. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  900. cpu_relax();
  901. /* Write a character */
  902. writel(c, port->membase + CDNS_UART_FIFO);
  903. /* Wait until FIFO is empty */
  904. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  905. cpu_relax();
  906. spin_unlock_irqrestore(&port->lock, flags);
  907. return;
  908. }
  909. #endif
  910. static void cdns_uart_pm(struct uart_port *port, unsigned int state,
  911. unsigned int oldstate)
  912. {
  913. struct cdns_uart *cdns_uart = port->private_data;
  914. switch (state) {
  915. case UART_PM_STATE_OFF:
  916. clk_disable(cdns_uart->uartclk);
  917. clk_disable(cdns_uart->pclk);
  918. break;
  919. default:
  920. clk_enable(cdns_uart->pclk);
  921. clk_enable(cdns_uart->uartclk);
  922. break;
  923. }
  924. }
  925. static const struct uart_ops cdns_uart_ops = {
  926. .set_mctrl = cdns_uart_set_mctrl,
  927. .get_mctrl = cdns_uart_get_mctrl,
  928. .start_tx = cdns_uart_start_tx,
  929. .stop_tx = cdns_uart_stop_tx,
  930. .stop_rx = cdns_uart_stop_rx,
  931. .tx_empty = cdns_uart_tx_empty,
  932. .break_ctl = cdns_uart_break_ctl,
  933. .set_termios = cdns_uart_set_termios,
  934. .startup = cdns_uart_startup,
  935. .shutdown = cdns_uart_shutdown,
  936. .pm = cdns_uart_pm,
  937. .type = cdns_uart_type,
  938. .verify_port = cdns_uart_verify_port,
  939. .request_port = cdns_uart_request_port,
  940. .release_port = cdns_uart_release_port,
  941. .config_port = cdns_uart_config_port,
  942. #ifdef CONFIG_CONSOLE_POLL
  943. .poll_get_char = cdns_uart_poll_get_char,
  944. .poll_put_char = cdns_uart_poll_put_char,
  945. #endif
  946. };
  947. static struct uart_port cdns_uart_port[CDNS_UART_NR_PORTS];
  948. /**
  949. * cdns_uart_get_port - Configure the port from platform device resource info
  950. * @id: Port id
  951. *
  952. * Return: a pointer to a uart_port or NULL for failure
  953. */
  954. static struct uart_port *cdns_uart_get_port(int id)
  955. {
  956. struct uart_port *port;
  957. /* Try the given port id if failed use default method */
  958. if (id < CDNS_UART_NR_PORTS && cdns_uart_port[id].mapbase != 0) {
  959. /* Find the next unused port */
  960. for (id = 0; id < CDNS_UART_NR_PORTS; id++)
  961. if (cdns_uart_port[id].mapbase == 0)
  962. break;
  963. }
  964. if (id >= CDNS_UART_NR_PORTS)
  965. return NULL;
  966. port = &cdns_uart_port[id];
  967. /* At this point, we've got an empty uart_port struct, initialize it */
  968. spin_lock_init(&port->lock);
  969. port->membase = NULL;
  970. port->irq = 0;
  971. port->type = PORT_UNKNOWN;
  972. port->iotype = UPIO_MEM32;
  973. port->flags = UPF_BOOT_AUTOCONF;
  974. port->ops = &cdns_uart_ops;
  975. port->fifosize = CDNS_UART_FIFO_SIZE;
  976. port->line = id;
  977. port->dev = NULL;
  978. return port;
  979. }
  980. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  981. /**
  982. * cdns_uart_console_wait_tx - Wait for the TX to be full
  983. * @port: Handle to the uart port structure
  984. */
  985. static void cdns_uart_console_wait_tx(struct uart_port *port)
  986. {
  987. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  988. barrier();
  989. }
  990. /**
  991. * cdns_uart_console_putchar - write the character to the FIFO buffer
  992. * @port: Handle to the uart port structure
  993. * @ch: Character to be written
  994. */
  995. static void cdns_uart_console_putchar(struct uart_port *port, int ch)
  996. {
  997. cdns_uart_console_wait_tx(port);
  998. writel(ch, port->membase + CDNS_UART_FIFO);
  999. }
  1000. static void __init cdns_early_write(struct console *con, const char *s,
  1001. unsigned n)
  1002. {
  1003. struct earlycon_device *dev = con->data;
  1004. uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
  1005. }
  1006. static int __init cdns_early_console_setup(struct earlycon_device *device,
  1007. const char *opt)
  1008. {
  1009. struct uart_port *port = &device->port;
  1010. if (!port->membase)
  1011. return -ENODEV;
  1012. /* initialise control register */
  1013. writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
  1014. port->membase + CDNS_UART_CR);
  1015. /* only set baud if specified on command line - otherwise
  1016. * assume it has been initialized by a boot loader.
  1017. */
  1018. if (device->baud) {
  1019. u32 cd = 0, bdiv = 0;
  1020. u32 mr;
  1021. int div8;
  1022. cdns_uart_calc_baud_divs(port->uartclk, device->baud,
  1023. &bdiv, &cd, &div8);
  1024. mr = CDNS_UART_MR_PARITY_NONE;
  1025. if (div8)
  1026. mr |= CDNS_UART_MR_CLKSEL;
  1027. writel(mr, port->membase + CDNS_UART_MR);
  1028. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  1029. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  1030. }
  1031. device->con->write = cdns_early_write;
  1032. return 0;
  1033. }
  1034. OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
  1035. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
  1036. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
  1037. OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
  1038. /**
  1039. * cdns_uart_console_write - perform write operation
  1040. * @co: Console handle
  1041. * @s: Pointer to character array
  1042. * @count: No of characters
  1043. */
  1044. static void cdns_uart_console_write(struct console *co, const char *s,
  1045. unsigned int count)
  1046. {
  1047. struct uart_port *port = &cdns_uart_port[co->index];
  1048. unsigned long flags;
  1049. unsigned int imr, ctrl;
  1050. int locked = 1;
  1051. if (port->sysrq)
  1052. locked = 0;
  1053. else if (oops_in_progress)
  1054. locked = spin_trylock_irqsave(&port->lock, flags);
  1055. else
  1056. spin_lock_irqsave(&port->lock, flags);
  1057. /* save and disable interrupt */
  1058. imr = readl(port->membase + CDNS_UART_IMR);
  1059. writel(imr, port->membase + CDNS_UART_IDR);
  1060. /*
  1061. * Make sure that the tx part is enabled. Set the TX enable bit and
  1062. * clear the TX disable bit to enable the transmitter.
  1063. */
  1064. ctrl = readl(port->membase + CDNS_UART_CR);
  1065. ctrl &= ~CDNS_UART_CR_TX_DIS;
  1066. ctrl |= CDNS_UART_CR_TX_EN;
  1067. writel(ctrl, port->membase + CDNS_UART_CR);
  1068. uart_console_write(port, s, count, cdns_uart_console_putchar);
  1069. cdns_uart_console_wait_tx(port);
  1070. writel(ctrl, port->membase + CDNS_UART_CR);
  1071. /* restore interrupt state */
  1072. writel(imr, port->membase + CDNS_UART_IER);
  1073. if (locked)
  1074. spin_unlock_irqrestore(&port->lock, flags);
  1075. }
  1076. /**
  1077. * cdns_uart_console_setup - Initialize the uart to default config
  1078. * @co: Console handle
  1079. * @options: Initial settings of uart
  1080. *
  1081. * Return: 0 on success, negative errno otherwise.
  1082. */
  1083. static int __init cdns_uart_console_setup(struct console *co, char *options)
  1084. {
  1085. struct uart_port *port = &cdns_uart_port[co->index];
  1086. int baud = 9600;
  1087. int bits = 8;
  1088. int parity = 'n';
  1089. int flow = 'n';
  1090. if (co->index < 0 || co->index >= CDNS_UART_NR_PORTS)
  1091. return -EINVAL;
  1092. if (!port->membase) {
  1093. pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
  1094. co->index);
  1095. return -ENODEV;
  1096. }
  1097. if (options)
  1098. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1099. return uart_set_options(port, co, baud, parity, bits, flow);
  1100. }
  1101. static struct uart_driver cdns_uart_uart_driver;
  1102. static struct console cdns_uart_console = {
  1103. .name = CDNS_UART_TTY_NAME,
  1104. .write = cdns_uart_console_write,
  1105. .device = uart_console_device,
  1106. .setup = cdns_uart_console_setup,
  1107. .flags = CON_PRINTBUFFER,
  1108. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  1109. .data = &cdns_uart_uart_driver,
  1110. };
  1111. /**
  1112. * cdns_uart_console_init - Initialization call
  1113. *
  1114. * Return: 0 on success, negative errno otherwise
  1115. */
  1116. static int __init cdns_uart_console_init(void)
  1117. {
  1118. register_console(&cdns_uart_console);
  1119. return 0;
  1120. }
  1121. console_initcall(cdns_uart_console_init);
  1122. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1123. static struct uart_driver cdns_uart_uart_driver = {
  1124. .owner = THIS_MODULE,
  1125. .driver_name = CDNS_UART_NAME,
  1126. .dev_name = CDNS_UART_TTY_NAME,
  1127. .major = CDNS_UART_MAJOR,
  1128. .minor = CDNS_UART_MINOR,
  1129. .nr = CDNS_UART_NR_PORTS,
  1130. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1131. .cons = &cdns_uart_console,
  1132. #endif
  1133. };
  1134. #ifdef CONFIG_PM_SLEEP
  1135. /**
  1136. * cdns_uart_suspend - suspend event
  1137. * @device: Pointer to the device structure
  1138. *
  1139. * Return: 0
  1140. */
  1141. static int cdns_uart_suspend(struct device *device)
  1142. {
  1143. struct uart_port *port = dev_get_drvdata(device);
  1144. struct tty_struct *tty;
  1145. struct device *tty_dev;
  1146. int may_wake = 0;
  1147. /* Get the tty which could be NULL so don't assume it's valid */
  1148. tty = tty_port_tty_get(&port->state->port);
  1149. if (tty) {
  1150. tty_dev = tty->dev;
  1151. may_wake = device_may_wakeup(tty_dev);
  1152. tty_kref_put(tty);
  1153. }
  1154. /*
  1155. * Call the API provided in serial_core.c file which handles
  1156. * the suspend.
  1157. */
  1158. uart_suspend_port(&cdns_uart_uart_driver, port);
  1159. if (console_suspend_enabled && !may_wake) {
  1160. struct cdns_uart *cdns_uart = port->private_data;
  1161. clk_disable(cdns_uart->uartclk);
  1162. clk_disable(cdns_uart->pclk);
  1163. } else {
  1164. unsigned long flags = 0;
  1165. spin_lock_irqsave(&port->lock, flags);
  1166. /* Empty the receive FIFO 1st before making changes */
  1167. while (!(readl(port->membase + CDNS_UART_SR) &
  1168. CDNS_UART_SR_RXEMPTY))
  1169. readl(port->membase + CDNS_UART_FIFO);
  1170. /* set RX trigger level to 1 */
  1171. writel(1, port->membase + CDNS_UART_RXWM);
  1172. /* disable RX timeout interrups */
  1173. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
  1174. spin_unlock_irqrestore(&port->lock, flags);
  1175. }
  1176. return 0;
  1177. }
  1178. /**
  1179. * cdns_uart_resume - Resume after a previous suspend
  1180. * @device: Pointer to the device structure
  1181. *
  1182. * Return: 0
  1183. */
  1184. static int cdns_uart_resume(struct device *device)
  1185. {
  1186. struct uart_port *port = dev_get_drvdata(device);
  1187. unsigned long flags = 0;
  1188. u32 ctrl_reg;
  1189. struct tty_struct *tty;
  1190. struct device *tty_dev;
  1191. int may_wake = 0;
  1192. /* Get the tty which could be NULL so don't assume it's valid */
  1193. tty = tty_port_tty_get(&port->state->port);
  1194. if (tty) {
  1195. tty_dev = tty->dev;
  1196. may_wake = device_may_wakeup(tty_dev);
  1197. tty_kref_put(tty);
  1198. }
  1199. if (console_suspend_enabled && !may_wake) {
  1200. struct cdns_uart *cdns_uart = port->private_data;
  1201. clk_enable(cdns_uart->pclk);
  1202. clk_enable(cdns_uart->uartclk);
  1203. spin_lock_irqsave(&port->lock, flags);
  1204. /* Set TX/RX Reset */
  1205. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1206. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  1207. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1208. while (readl(port->membase + CDNS_UART_CR) &
  1209. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  1210. cpu_relax();
  1211. /* restore rx timeout value */
  1212. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  1213. /* Enable Tx/Rx */
  1214. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1215. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  1216. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  1217. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1218. spin_unlock_irqrestore(&port->lock, flags);
  1219. } else {
  1220. spin_lock_irqsave(&port->lock, flags);
  1221. /* restore original rx trigger level */
  1222. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  1223. /* enable RX timeout interrupt */
  1224. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
  1225. spin_unlock_irqrestore(&port->lock, flags);
  1226. }
  1227. return uart_resume_port(&cdns_uart_uart_driver, port);
  1228. }
  1229. #endif /* ! CONFIG_PM_SLEEP */
  1230. static SIMPLE_DEV_PM_OPS(cdns_uart_dev_pm_ops, cdns_uart_suspend,
  1231. cdns_uart_resume);
  1232. static const struct cdns_platform_data zynqmp_uart_def = {
  1233. .quirks = CDNS_UART_RXBS_SUPPORT, };
  1234. /* Match table for of_platform binding */
  1235. static const struct of_device_id cdns_uart_of_match[] = {
  1236. { .compatible = "xlnx,xuartps", },
  1237. { .compatible = "cdns,uart-r1p8", },
  1238. { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
  1239. { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
  1240. {}
  1241. };
  1242. MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
  1243. /**
  1244. * cdns_uart_probe - Platform driver probe
  1245. * @pdev: Pointer to the platform device structure
  1246. *
  1247. * Return: 0 on success, negative errno otherwise
  1248. */
  1249. static int cdns_uart_probe(struct platform_device *pdev)
  1250. {
  1251. int rc, id, irq;
  1252. struct uart_port *port;
  1253. struct resource *res;
  1254. struct cdns_uart *cdns_uart_data;
  1255. const struct of_device_id *match;
  1256. cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
  1257. GFP_KERNEL);
  1258. if (!cdns_uart_data)
  1259. return -ENOMEM;
  1260. match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
  1261. if (match && match->data) {
  1262. const struct cdns_platform_data *data = match->data;
  1263. cdns_uart_data->quirks = data->quirks;
  1264. }
  1265. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
  1266. if (IS_ERR(cdns_uart_data->pclk)) {
  1267. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
  1268. if (!IS_ERR(cdns_uart_data->pclk))
  1269. dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
  1270. }
  1271. if (IS_ERR(cdns_uart_data->pclk)) {
  1272. dev_err(&pdev->dev, "pclk clock not found.\n");
  1273. return PTR_ERR(cdns_uart_data->pclk);
  1274. }
  1275. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
  1276. if (IS_ERR(cdns_uart_data->uartclk)) {
  1277. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
  1278. if (!IS_ERR(cdns_uart_data->uartclk))
  1279. dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
  1280. }
  1281. if (IS_ERR(cdns_uart_data->uartclk)) {
  1282. dev_err(&pdev->dev, "uart_clk clock not found.\n");
  1283. return PTR_ERR(cdns_uart_data->uartclk);
  1284. }
  1285. rc = clk_prepare(cdns_uart_data->pclk);
  1286. if (rc) {
  1287. dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
  1288. return rc;
  1289. }
  1290. rc = clk_prepare(cdns_uart_data->uartclk);
  1291. if (rc) {
  1292. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1293. goto err_out_clk_dis_pclk;
  1294. }
  1295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1296. if (!res) {
  1297. rc = -ENODEV;
  1298. goto err_out_clk_disable;
  1299. }
  1300. irq = platform_get_irq(pdev, 0);
  1301. if (irq <= 0) {
  1302. rc = -ENXIO;
  1303. goto err_out_clk_disable;
  1304. }
  1305. #ifdef CONFIG_COMMON_CLK
  1306. cdns_uart_data->clk_rate_change_nb.notifier_call =
  1307. cdns_uart_clk_notifier_cb;
  1308. if (clk_notifier_register(cdns_uart_data->uartclk,
  1309. &cdns_uart_data->clk_rate_change_nb))
  1310. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1311. #endif
  1312. /* Look for a serialN alias */
  1313. id = of_alias_get_id(pdev->dev.of_node, "serial");
  1314. if (id < 0)
  1315. id = 0;
  1316. /* Initialize the port structure */
  1317. port = cdns_uart_get_port(id);
  1318. if (!port) {
  1319. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1320. rc = -ENODEV;
  1321. goto err_out_notif_unreg;
  1322. }
  1323. /*
  1324. * Register the port.
  1325. * This function also registers this device with the tty layer
  1326. * and triggers invocation of the config_port() entry point.
  1327. */
  1328. port->mapbase = res->start;
  1329. port->irq = irq;
  1330. port->dev = &pdev->dev;
  1331. port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
  1332. port->private_data = cdns_uart_data;
  1333. cdns_uart_data->port = port;
  1334. platform_set_drvdata(pdev, port);
  1335. rc = uart_add_one_port(&cdns_uart_uart_driver, port);
  1336. if (rc) {
  1337. dev_err(&pdev->dev,
  1338. "uart_add_one_port() failed; err=%i\n", rc);
  1339. goto err_out_notif_unreg;
  1340. }
  1341. return 0;
  1342. err_out_notif_unreg:
  1343. #ifdef CONFIG_COMMON_CLK
  1344. clk_notifier_unregister(cdns_uart_data->uartclk,
  1345. &cdns_uart_data->clk_rate_change_nb);
  1346. #endif
  1347. err_out_clk_disable:
  1348. clk_unprepare(cdns_uart_data->uartclk);
  1349. err_out_clk_dis_pclk:
  1350. clk_unprepare(cdns_uart_data->pclk);
  1351. return rc;
  1352. }
  1353. /**
  1354. * cdns_uart_remove - called when the platform driver is unregistered
  1355. * @pdev: Pointer to the platform device structure
  1356. *
  1357. * Return: 0 on success, negative errno otherwise
  1358. */
  1359. static int cdns_uart_remove(struct platform_device *pdev)
  1360. {
  1361. struct uart_port *port = platform_get_drvdata(pdev);
  1362. struct cdns_uart *cdns_uart_data = port->private_data;
  1363. int rc;
  1364. /* Remove the cdns_uart port from the serial core */
  1365. #ifdef CONFIG_COMMON_CLK
  1366. clk_notifier_unregister(cdns_uart_data->uartclk,
  1367. &cdns_uart_data->clk_rate_change_nb);
  1368. #endif
  1369. rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
  1370. port->mapbase = 0;
  1371. clk_unprepare(cdns_uart_data->uartclk);
  1372. clk_unprepare(cdns_uart_data->pclk);
  1373. return rc;
  1374. }
  1375. static struct platform_driver cdns_uart_platform_driver = {
  1376. .probe = cdns_uart_probe,
  1377. .remove = cdns_uart_remove,
  1378. .driver = {
  1379. .name = CDNS_UART_NAME,
  1380. .of_match_table = cdns_uart_of_match,
  1381. .pm = &cdns_uart_dev_pm_ops,
  1382. },
  1383. };
  1384. static int __init cdns_uart_init(void)
  1385. {
  1386. int retval = 0;
  1387. /* Register the cdns_uart driver with the serial core */
  1388. retval = uart_register_driver(&cdns_uart_uart_driver);
  1389. if (retval)
  1390. return retval;
  1391. /* Register the platform driver */
  1392. retval = platform_driver_register(&cdns_uart_platform_driver);
  1393. if (retval)
  1394. uart_unregister_driver(&cdns_uart_uart_driver);
  1395. return retval;
  1396. }
  1397. static void __exit cdns_uart_exit(void)
  1398. {
  1399. /* Unregister the platform driver */
  1400. platform_driver_unregister(&cdns_uart_platform_driver);
  1401. /* Unregister the cdns_uart driver */
  1402. uart_unregister_driver(&cdns_uart_uart_driver);
  1403. }
  1404. module_init(cdns_uart_init);
  1405. module_exit(cdns_uart_exit);
  1406. MODULE_DESCRIPTION("Driver for Cadence UART");
  1407. MODULE_AUTHOR("Xilinx Inc.");
  1408. MODULE_LICENSE("GPL");