xilinx_uartps.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Cadence UART driver (found in Xilinx Zynq)
  4. *
  5. * 2011 - 2014 (C) Xilinx Inc.
  6. *
  7. * This driver has originally been pushed by Xilinx using a Zynq-branding. This
  8. * still shows in the naming of this file, the kconfig symbols and some symbols
  9. * in the code.
  10. */
  11. #if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  12. #define SUPPORT_SYSRQ
  13. #endif
  14. #include <linux/platform_device.h>
  15. #include <linux/serial.h>
  16. #include <linux/console.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/clk.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/of.h>
  25. #include <linux/module.h>
  26. #include <linux/pm_runtime.h>
  27. #define CDNS_UART_TTY_NAME "ttyPS"
  28. #define CDNS_UART_NAME "xuartps"
  29. #define CDNS_UART_MAJOR 0 /* use dynamic node allocation */
  30. #define CDNS_UART_MINOR 0 /* works best with devtmpfs */
  31. #define CDNS_UART_NR_PORTS 2
  32. #define CDNS_UART_FIFO_SIZE 64 /* FIFO size */
  33. #define CDNS_UART_REGISTER_SPACE 0x1000
  34. /* Rx Trigger level */
  35. static int rx_trigger_level = 56;
  36. module_param(rx_trigger_level, uint, S_IRUGO);
  37. MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
  38. /* Rx Timeout */
  39. static int rx_timeout = 10;
  40. module_param(rx_timeout, uint, S_IRUGO);
  41. MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
  42. /* Register offsets for the UART. */
  43. #define CDNS_UART_CR 0x00 /* Control Register */
  44. #define CDNS_UART_MR 0x04 /* Mode Register */
  45. #define CDNS_UART_IER 0x08 /* Interrupt Enable */
  46. #define CDNS_UART_IDR 0x0C /* Interrupt Disable */
  47. #define CDNS_UART_IMR 0x10 /* Interrupt Mask */
  48. #define CDNS_UART_ISR 0x14 /* Interrupt Status */
  49. #define CDNS_UART_BAUDGEN 0x18 /* Baud Rate Generator */
  50. #define CDNS_UART_RXTOUT 0x1C /* RX Timeout */
  51. #define CDNS_UART_RXWM 0x20 /* RX FIFO Trigger Level */
  52. #define CDNS_UART_MODEMCR 0x24 /* Modem Control */
  53. #define CDNS_UART_MODEMSR 0x28 /* Modem Status */
  54. #define CDNS_UART_SR 0x2C /* Channel Status */
  55. #define CDNS_UART_FIFO 0x30 /* FIFO */
  56. #define CDNS_UART_BAUDDIV 0x34 /* Baud Rate Divider */
  57. #define CDNS_UART_FLOWDEL 0x38 /* Flow Delay */
  58. #define CDNS_UART_IRRX_PWIDTH 0x3C /* IR Min Received Pulse Width */
  59. #define CDNS_UART_IRTX_PWIDTH 0x40 /* IR Transmitted pulse Width */
  60. #define CDNS_UART_TXWM 0x44 /* TX FIFO Trigger Level */
  61. #define CDNS_UART_RXBS 0x48 /* RX FIFO byte status register */
  62. /* Control Register Bit Definitions */
  63. #define CDNS_UART_CR_STOPBRK 0x00000100 /* Stop TX break */
  64. #define CDNS_UART_CR_STARTBRK 0x00000080 /* Set TX break */
  65. #define CDNS_UART_CR_TX_DIS 0x00000020 /* TX disabled. */
  66. #define CDNS_UART_CR_TX_EN 0x00000010 /* TX enabled */
  67. #define CDNS_UART_CR_RX_DIS 0x00000008 /* RX disabled. */
  68. #define CDNS_UART_CR_RX_EN 0x00000004 /* RX enabled */
  69. #define CDNS_UART_CR_TXRST 0x00000002 /* TX logic reset */
  70. #define CDNS_UART_CR_RXRST 0x00000001 /* RX logic reset */
  71. #define CDNS_UART_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
  72. #define CDNS_UART_RXBS_PARITY 0x00000001 /* Parity error status */
  73. #define CDNS_UART_RXBS_FRAMING 0x00000002 /* Framing error status */
  74. #define CDNS_UART_RXBS_BRK 0x00000004 /* Overrun error status */
  75. /*
  76. * Mode Register:
  77. * The mode register (MR) defines the mode of transfer as well as the data
  78. * format. If this register is modified during transmission or reception,
  79. * data validity cannot be guaranteed.
  80. */
  81. #define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
  82. #define CDNS_UART_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
  83. #define CDNS_UART_MR_CHMODE_NORM 0x00000000 /* Normal mode */
  84. #define CDNS_UART_MR_CHMODE_MASK 0x00000300 /* Mask for mode bits */
  85. #define CDNS_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
  86. #define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
  87. #define CDNS_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
  88. #define CDNS_UART_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
  89. #define CDNS_UART_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
  90. #define CDNS_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
  91. #define CDNS_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
  92. #define CDNS_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
  93. #define CDNS_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
  94. #define CDNS_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
  95. /*
  96. * Interrupt Registers:
  97. * Interrupt control logic uses the interrupt enable register (IER) and the
  98. * interrupt disable register (IDR) to set the value of the bits in the
  99. * interrupt mask register (IMR). The IMR determines whether to pass an
  100. * interrupt to the interrupt status register (ISR).
  101. * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
  102. * interrupt. IMR and ISR are read only, and IER and IDR are write only.
  103. * Reading either IER or IDR returns 0x00.
  104. * All four registers have the same bit definitions.
  105. */
  106. #define CDNS_UART_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
  107. #define CDNS_UART_IXR_PARITY 0x00000080 /* Parity error interrupt */
  108. #define CDNS_UART_IXR_FRAMING 0x00000040 /* Framing error interrupt */
  109. #define CDNS_UART_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
  110. #define CDNS_UART_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
  111. #define CDNS_UART_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
  112. #define CDNS_UART_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
  113. #define CDNS_UART_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
  114. #define CDNS_UART_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
  115. #define CDNS_UART_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
  116. #define CDNS_UART_IXR_MASK 0x00001FFF /* Valid bit mask */
  117. /*
  118. * Do not enable parity error interrupt for the following
  119. * reason: When parity error interrupt is enabled, each Rx
  120. * parity error always results in 2 events. The first one
  121. * being parity error interrupt and the second one with a
  122. * proper Rx interrupt with the incoming data. Disabling
  123. * parity error interrupt ensures better handling of parity
  124. * error events. With this change, for a parity error case, we
  125. * get a Rx interrupt with parity error set in ISR register
  126. * and we still handle parity errors in the desired way.
  127. */
  128. #define CDNS_UART_RX_IRQS (CDNS_UART_IXR_FRAMING | \
  129. CDNS_UART_IXR_OVERRUN | \
  130. CDNS_UART_IXR_RXTRIG | \
  131. CDNS_UART_IXR_TOUT)
  132. /* Goes in read_status_mask for break detection as the HW doesn't do it*/
  133. #define CDNS_UART_IXR_BRK 0x00002000
  134. #define CDNS_UART_RXBS_SUPPORT BIT(1)
  135. /*
  136. * Modem Control register:
  137. * The read/write Modem Control register controls the interface with the modem
  138. * or data set, or a peripheral device emulating a modem.
  139. */
  140. #define CDNS_UART_MODEMCR_FCM 0x00000020 /* Automatic flow control mode */
  141. #define CDNS_UART_MODEMCR_RTS 0x00000002 /* Request to send output control */
  142. #define CDNS_UART_MODEMCR_DTR 0x00000001 /* Data Terminal Ready */
  143. /*
  144. * Channel Status Register:
  145. * The channel status register (CSR) is provided to enable the control logic
  146. * to monitor the status of bits in the channel interrupt status register,
  147. * even if these are masked out by the interrupt mask register.
  148. */
  149. #define CDNS_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  150. #define CDNS_UART_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
  151. #define CDNS_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
  152. #define CDNS_UART_SR_RXTRIG 0x00000001 /* Rx Trigger */
  153. #define CDNS_UART_SR_TACTIVE 0x00000800 /* TX state machine active */
  154. /* baud dividers min/max values */
  155. #define CDNS_UART_BDIV_MIN 4
  156. #define CDNS_UART_BDIV_MAX 255
  157. #define CDNS_UART_CD_MAX 65535
  158. #define UART_AUTOSUSPEND_TIMEOUT 3000
  159. /**
  160. * struct cdns_uart - device data
  161. * @port: Pointer to the UART port
  162. * @uartclk: Reference clock
  163. * @pclk: APB clock
  164. * @baud: Current baud rate
  165. * @clk_rate_change_nb: Notifier block for clock changes
  166. * @quirks: Flags for RXBS support.
  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. u32 mode_reg;
  873. val = readl(port->membase + CDNS_UART_MODEMCR);
  874. mode_reg = readl(port->membase + CDNS_UART_MR);
  875. val &= ~(CDNS_UART_MODEMCR_RTS | CDNS_UART_MODEMCR_DTR);
  876. mode_reg &= ~CDNS_UART_MR_CHMODE_MASK;
  877. if (mctrl & TIOCM_RTS)
  878. val |= CDNS_UART_MODEMCR_RTS;
  879. if (mctrl & TIOCM_DTR)
  880. val |= CDNS_UART_MODEMCR_DTR;
  881. if (mctrl & TIOCM_LOOP)
  882. mode_reg |= CDNS_UART_MR_CHMODE_L_LOOP;
  883. else
  884. mode_reg |= CDNS_UART_MR_CHMODE_NORM;
  885. writel(val, port->membase + CDNS_UART_MODEMCR);
  886. writel(mode_reg, port->membase + CDNS_UART_MR);
  887. }
  888. #ifdef CONFIG_CONSOLE_POLL
  889. static int cdns_uart_poll_get_char(struct uart_port *port)
  890. {
  891. int c;
  892. unsigned long flags;
  893. spin_lock_irqsave(&port->lock, flags);
  894. /* Check if FIFO is empty */
  895. if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY)
  896. c = NO_POLL_CHAR;
  897. else /* Read a character */
  898. c = (unsigned char) readl(port->membase + CDNS_UART_FIFO);
  899. spin_unlock_irqrestore(&port->lock, flags);
  900. return c;
  901. }
  902. static void cdns_uart_poll_put_char(struct uart_port *port, unsigned char c)
  903. {
  904. unsigned long flags;
  905. spin_lock_irqsave(&port->lock, flags);
  906. /* Wait until FIFO is empty */
  907. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  908. cpu_relax();
  909. /* Write a character */
  910. writel(c, port->membase + CDNS_UART_FIFO);
  911. /* Wait until FIFO is empty */
  912. while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY))
  913. cpu_relax();
  914. spin_unlock_irqrestore(&port->lock, flags);
  915. return;
  916. }
  917. #endif
  918. static void cdns_uart_pm(struct uart_port *port, unsigned int state,
  919. unsigned int oldstate)
  920. {
  921. switch (state) {
  922. case UART_PM_STATE_OFF:
  923. pm_runtime_mark_last_busy(port->dev);
  924. pm_runtime_put_autosuspend(port->dev);
  925. break;
  926. default:
  927. pm_runtime_get_sync(port->dev);
  928. break;
  929. }
  930. }
  931. static const struct uart_ops cdns_uart_ops = {
  932. .set_mctrl = cdns_uart_set_mctrl,
  933. .get_mctrl = cdns_uart_get_mctrl,
  934. .start_tx = cdns_uart_start_tx,
  935. .stop_tx = cdns_uart_stop_tx,
  936. .stop_rx = cdns_uart_stop_rx,
  937. .tx_empty = cdns_uart_tx_empty,
  938. .break_ctl = cdns_uart_break_ctl,
  939. .set_termios = cdns_uart_set_termios,
  940. .startup = cdns_uart_startup,
  941. .shutdown = cdns_uart_shutdown,
  942. .pm = cdns_uart_pm,
  943. .type = cdns_uart_type,
  944. .verify_port = cdns_uart_verify_port,
  945. .request_port = cdns_uart_request_port,
  946. .release_port = cdns_uart_release_port,
  947. .config_port = cdns_uart_config_port,
  948. #ifdef CONFIG_CONSOLE_POLL
  949. .poll_get_char = cdns_uart_poll_get_char,
  950. .poll_put_char = cdns_uart_poll_put_char,
  951. #endif
  952. };
  953. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  954. /**
  955. * cdns_uart_console_putchar - write the character to the FIFO buffer
  956. * @port: Handle to the uart port structure
  957. * @ch: Character to be written
  958. */
  959. static void cdns_uart_console_putchar(struct uart_port *port, int ch)
  960. {
  961. while (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL)
  962. cpu_relax();
  963. writel(ch, port->membase + CDNS_UART_FIFO);
  964. }
  965. static void cdns_early_write(struct console *con, const char *s,
  966. unsigned n)
  967. {
  968. struct earlycon_device *dev = con->data;
  969. uart_console_write(&dev->port, s, n, cdns_uart_console_putchar);
  970. }
  971. static int __init cdns_early_console_setup(struct earlycon_device *device,
  972. const char *opt)
  973. {
  974. struct uart_port *port = &device->port;
  975. if (!port->membase)
  976. return -ENODEV;
  977. /* initialise control register */
  978. writel(CDNS_UART_CR_TX_EN|CDNS_UART_CR_TXRST|CDNS_UART_CR_RXRST,
  979. port->membase + CDNS_UART_CR);
  980. /* only set baud if specified on command line - otherwise
  981. * assume it has been initialized by a boot loader.
  982. */
  983. if (port->uartclk && device->baud) {
  984. u32 cd = 0, bdiv = 0;
  985. u32 mr;
  986. int div8;
  987. cdns_uart_calc_baud_divs(port->uartclk, device->baud,
  988. &bdiv, &cd, &div8);
  989. mr = CDNS_UART_MR_PARITY_NONE;
  990. if (div8)
  991. mr |= CDNS_UART_MR_CLKSEL;
  992. writel(mr, port->membase + CDNS_UART_MR);
  993. writel(cd, port->membase + CDNS_UART_BAUDGEN);
  994. writel(bdiv, port->membase + CDNS_UART_BAUDDIV);
  995. }
  996. device->con->write = cdns_early_write;
  997. return 0;
  998. }
  999. OF_EARLYCON_DECLARE(cdns, "xlnx,xuartps", cdns_early_console_setup);
  1000. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
  1001. OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
  1002. OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
  1003. /* Static pointer to console port */
  1004. static struct uart_port *console_port;
  1005. /**
  1006. * cdns_uart_console_write - perform write operation
  1007. * @co: Console handle
  1008. * @s: Pointer to character array
  1009. * @count: No of characters
  1010. */
  1011. static void cdns_uart_console_write(struct console *co, const char *s,
  1012. unsigned int count)
  1013. {
  1014. struct uart_port *port = console_port;
  1015. unsigned long flags;
  1016. unsigned int imr, ctrl;
  1017. int locked = 1;
  1018. if (port->sysrq)
  1019. locked = 0;
  1020. else if (oops_in_progress)
  1021. locked = spin_trylock_irqsave(&port->lock, flags);
  1022. else
  1023. spin_lock_irqsave(&port->lock, flags);
  1024. /* save and disable interrupt */
  1025. imr = readl(port->membase + CDNS_UART_IMR);
  1026. writel(imr, port->membase + CDNS_UART_IDR);
  1027. /*
  1028. * Make sure that the tx part is enabled. Set the TX enable bit and
  1029. * clear the TX disable bit to enable the transmitter.
  1030. */
  1031. ctrl = readl(port->membase + CDNS_UART_CR);
  1032. ctrl &= ~CDNS_UART_CR_TX_DIS;
  1033. ctrl |= CDNS_UART_CR_TX_EN;
  1034. writel(ctrl, port->membase + CDNS_UART_CR);
  1035. uart_console_write(port, s, count, cdns_uart_console_putchar);
  1036. while ((readl(port->membase + CDNS_UART_SR) &
  1037. (CDNS_UART_SR_TXEMPTY | CDNS_UART_SR_TACTIVE)) !=
  1038. CDNS_UART_SR_TXEMPTY)
  1039. cpu_relax();
  1040. /* restore interrupt state */
  1041. writel(imr, port->membase + CDNS_UART_IER);
  1042. if (locked)
  1043. spin_unlock_irqrestore(&port->lock, flags);
  1044. }
  1045. /**
  1046. * cdns_uart_console_setup - Initialize the uart to default config
  1047. * @co: Console handle
  1048. * @options: Initial settings of uart
  1049. *
  1050. * Return: 0 on success, negative errno otherwise.
  1051. */
  1052. static int __init cdns_uart_console_setup(struct console *co, char *options)
  1053. {
  1054. struct uart_port *port = console_port;
  1055. int baud = 9600;
  1056. int bits = 8;
  1057. int parity = 'n';
  1058. int flow = 'n';
  1059. if (!port->membase) {
  1060. pr_debug("console on " CDNS_UART_TTY_NAME "%i not present\n",
  1061. co->index);
  1062. return -ENODEV;
  1063. }
  1064. if (options)
  1065. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1066. return uart_set_options(port, co, baud, parity, bits, flow);
  1067. }
  1068. static struct uart_driver cdns_uart_uart_driver;
  1069. static struct console cdns_uart_console = {
  1070. .name = CDNS_UART_TTY_NAME,
  1071. .write = cdns_uart_console_write,
  1072. .device = uart_console_device,
  1073. .setup = cdns_uart_console_setup,
  1074. .flags = CON_PRINTBUFFER,
  1075. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  1076. .data = &cdns_uart_uart_driver,
  1077. };
  1078. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1079. static struct uart_driver cdns_uart_uart_driver = {
  1080. .owner = THIS_MODULE,
  1081. .driver_name = CDNS_UART_NAME,
  1082. .dev_name = CDNS_UART_TTY_NAME,
  1083. .major = CDNS_UART_MAJOR,
  1084. .minor = CDNS_UART_MINOR,
  1085. .nr = CDNS_UART_NR_PORTS,
  1086. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1087. .cons = &cdns_uart_console,
  1088. #endif
  1089. };
  1090. #ifdef CONFIG_PM_SLEEP
  1091. /**
  1092. * cdns_uart_suspend - suspend event
  1093. * @device: Pointer to the device structure
  1094. *
  1095. * Return: 0
  1096. */
  1097. static int cdns_uart_suspend(struct device *device)
  1098. {
  1099. struct uart_port *port = dev_get_drvdata(device);
  1100. struct tty_struct *tty;
  1101. struct device *tty_dev;
  1102. int may_wake = 0;
  1103. /* Get the tty which could be NULL so don't assume it's valid */
  1104. tty = tty_port_tty_get(&port->state->port);
  1105. if (tty) {
  1106. tty_dev = tty->dev;
  1107. may_wake = device_may_wakeup(tty_dev);
  1108. tty_kref_put(tty);
  1109. }
  1110. /*
  1111. * Call the API provided in serial_core.c file which handles
  1112. * the suspend.
  1113. */
  1114. uart_suspend_port(&cdns_uart_uart_driver, port);
  1115. if (!(console_suspend_enabled && !may_wake)) {
  1116. unsigned long flags = 0;
  1117. spin_lock_irqsave(&port->lock, flags);
  1118. /* Empty the receive FIFO 1st before making changes */
  1119. while (!(readl(port->membase + CDNS_UART_SR) &
  1120. CDNS_UART_SR_RXEMPTY))
  1121. readl(port->membase + CDNS_UART_FIFO);
  1122. /* set RX trigger level to 1 */
  1123. writel(1, port->membase + CDNS_UART_RXWM);
  1124. /* disable RX timeout interrups */
  1125. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR);
  1126. spin_unlock_irqrestore(&port->lock, flags);
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * cdns_uart_resume - Resume after a previous suspend
  1132. * @device: Pointer to the device structure
  1133. *
  1134. * Return: 0
  1135. */
  1136. static int cdns_uart_resume(struct device *device)
  1137. {
  1138. struct uart_port *port = dev_get_drvdata(device);
  1139. unsigned long flags = 0;
  1140. u32 ctrl_reg;
  1141. struct tty_struct *tty;
  1142. struct device *tty_dev;
  1143. int may_wake = 0;
  1144. /* Get the tty which could be NULL so don't assume it's valid */
  1145. tty = tty_port_tty_get(&port->state->port);
  1146. if (tty) {
  1147. tty_dev = tty->dev;
  1148. may_wake = device_may_wakeup(tty_dev);
  1149. tty_kref_put(tty);
  1150. }
  1151. if (console_suspend_enabled && !may_wake) {
  1152. struct cdns_uart *cdns_uart = port->private_data;
  1153. clk_enable(cdns_uart->pclk);
  1154. clk_enable(cdns_uart->uartclk);
  1155. spin_lock_irqsave(&port->lock, flags);
  1156. /* Set TX/RX Reset */
  1157. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1158. ctrl_reg |= CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST;
  1159. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1160. while (readl(port->membase + CDNS_UART_CR) &
  1161. (CDNS_UART_CR_TXRST | CDNS_UART_CR_RXRST))
  1162. cpu_relax();
  1163. /* restore rx timeout value */
  1164. writel(rx_timeout, port->membase + CDNS_UART_RXTOUT);
  1165. /* Enable Tx/Rx */
  1166. ctrl_reg = readl(port->membase + CDNS_UART_CR);
  1167. ctrl_reg &= ~(CDNS_UART_CR_TX_DIS | CDNS_UART_CR_RX_DIS);
  1168. ctrl_reg |= CDNS_UART_CR_TX_EN | CDNS_UART_CR_RX_EN;
  1169. writel(ctrl_reg, port->membase + CDNS_UART_CR);
  1170. clk_disable(cdns_uart->uartclk);
  1171. clk_disable(cdns_uart->pclk);
  1172. spin_unlock_irqrestore(&port->lock, flags);
  1173. } else {
  1174. spin_lock_irqsave(&port->lock, flags);
  1175. /* restore original rx trigger level */
  1176. writel(rx_trigger_level, port->membase + CDNS_UART_RXWM);
  1177. /* enable RX timeout interrupt */
  1178. writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER);
  1179. spin_unlock_irqrestore(&port->lock, flags);
  1180. }
  1181. return uart_resume_port(&cdns_uart_uart_driver, port);
  1182. }
  1183. #endif /* ! CONFIG_PM_SLEEP */
  1184. static int __maybe_unused cdns_runtime_suspend(struct device *dev)
  1185. {
  1186. struct uart_port *port = dev_get_drvdata(dev);
  1187. struct cdns_uart *cdns_uart = port->private_data;
  1188. clk_disable(cdns_uart->uartclk);
  1189. clk_disable(cdns_uart->pclk);
  1190. return 0;
  1191. };
  1192. static int __maybe_unused cdns_runtime_resume(struct device *dev)
  1193. {
  1194. struct uart_port *port = dev_get_drvdata(dev);
  1195. struct cdns_uart *cdns_uart = port->private_data;
  1196. clk_enable(cdns_uart->pclk);
  1197. clk_enable(cdns_uart->uartclk);
  1198. return 0;
  1199. };
  1200. static const struct dev_pm_ops cdns_uart_dev_pm_ops = {
  1201. SET_SYSTEM_SLEEP_PM_OPS(cdns_uart_suspend, cdns_uart_resume)
  1202. SET_RUNTIME_PM_OPS(cdns_runtime_suspend,
  1203. cdns_runtime_resume, NULL)
  1204. };
  1205. static const struct cdns_platform_data zynqmp_uart_def = {
  1206. .quirks = CDNS_UART_RXBS_SUPPORT, };
  1207. /* Match table for of_platform binding */
  1208. static const struct of_device_id cdns_uart_of_match[] = {
  1209. { .compatible = "xlnx,xuartps", },
  1210. { .compatible = "cdns,uart-r1p8", },
  1211. { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
  1212. { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
  1213. {}
  1214. };
  1215. MODULE_DEVICE_TABLE(of, cdns_uart_of_match);
  1216. /**
  1217. * cdns_uart_probe - Platform driver probe
  1218. * @pdev: Pointer to the platform device structure
  1219. *
  1220. * Return: 0 on success, negative errno otherwise
  1221. */
  1222. static int cdns_uart_probe(struct platform_device *pdev)
  1223. {
  1224. int rc, id, irq;
  1225. struct uart_port *port;
  1226. struct resource *res;
  1227. struct cdns_uart *cdns_uart_data;
  1228. const struct of_device_id *match;
  1229. cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data),
  1230. GFP_KERNEL);
  1231. if (!cdns_uart_data)
  1232. return -ENOMEM;
  1233. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  1234. if (!port)
  1235. return -ENOMEM;
  1236. match = of_match_node(cdns_uart_of_match, pdev->dev.of_node);
  1237. if (match && match->data) {
  1238. const struct cdns_platform_data *data = match->data;
  1239. cdns_uart_data->quirks = data->quirks;
  1240. }
  1241. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk");
  1242. if (IS_ERR(cdns_uart_data->pclk)) {
  1243. cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk");
  1244. if (!IS_ERR(cdns_uart_data->pclk))
  1245. dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n");
  1246. }
  1247. if (IS_ERR(cdns_uart_data->pclk)) {
  1248. dev_err(&pdev->dev, "pclk clock not found.\n");
  1249. return PTR_ERR(cdns_uart_data->pclk);
  1250. }
  1251. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk");
  1252. if (IS_ERR(cdns_uart_data->uartclk)) {
  1253. cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk");
  1254. if (!IS_ERR(cdns_uart_data->uartclk))
  1255. dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n");
  1256. }
  1257. if (IS_ERR(cdns_uart_data->uartclk)) {
  1258. dev_err(&pdev->dev, "uart_clk clock not found.\n");
  1259. return PTR_ERR(cdns_uart_data->uartclk);
  1260. }
  1261. rc = clk_prepare_enable(cdns_uart_data->pclk);
  1262. if (rc) {
  1263. dev_err(&pdev->dev, "Unable to enable pclk clock.\n");
  1264. return rc;
  1265. }
  1266. rc = clk_prepare_enable(cdns_uart_data->uartclk);
  1267. if (rc) {
  1268. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1269. goto err_out_clk_dis_pclk;
  1270. }
  1271. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1272. if (!res) {
  1273. rc = -ENODEV;
  1274. goto err_out_clk_disable;
  1275. }
  1276. irq = platform_get_irq(pdev, 0);
  1277. if (irq <= 0) {
  1278. rc = -ENXIO;
  1279. goto err_out_clk_disable;
  1280. }
  1281. #ifdef CONFIG_COMMON_CLK
  1282. cdns_uart_data->clk_rate_change_nb.notifier_call =
  1283. cdns_uart_clk_notifier_cb;
  1284. if (clk_notifier_register(cdns_uart_data->uartclk,
  1285. &cdns_uart_data->clk_rate_change_nb))
  1286. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1287. #endif
  1288. /* Look for a serialN alias */
  1289. id = of_alias_get_id(pdev->dev.of_node, "serial");
  1290. if (id < 0)
  1291. id = 0;
  1292. if (id >= CDNS_UART_NR_PORTS) {
  1293. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1294. rc = -ENODEV;
  1295. goto err_out_notif_unreg;
  1296. }
  1297. /* At this point, we've got an empty uart_port struct, initialize it */
  1298. spin_lock_init(&port->lock);
  1299. port->membase = NULL;
  1300. port->irq = 0;
  1301. port->type = PORT_UNKNOWN;
  1302. port->iotype = UPIO_MEM32;
  1303. port->flags = UPF_BOOT_AUTOCONF;
  1304. port->ops = &cdns_uart_ops;
  1305. port->fifosize = CDNS_UART_FIFO_SIZE;
  1306. port->line = id;
  1307. port->dev = NULL;
  1308. /*
  1309. * Register the port.
  1310. * This function also registers this device with the tty layer
  1311. * and triggers invocation of the config_port() entry point.
  1312. */
  1313. port->mapbase = res->start;
  1314. port->irq = irq;
  1315. port->dev = &pdev->dev;
  1316. port->uartclk = clk_get_rate(cdns_uart_data->uartclk);
  1317. port->private_data = cdns_uart_data;
  1318. cdns_uart_data->port = port;
  1319. platform_set_drvdata(pdev, port);
  1320. pm_runtime_use_autosuspend(&pdev->dev);
  1321. pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
  1322. pm_runtime_set_active(&pdev->dev);
  1323. pm_runtime_enable(&pdev->dev);
  1324. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1325. /*
  1326. * If console hasn't been found yet try to assign this port
  1327. * because it is required to be assigned for console setup function.
  1328. * If register_console() don't assign value, then console_port pointer
  1329. * is cleanup.
  1330. */
  1331. if (cdns_uart_uart_driver.cons->index == -1)
  1332. console_port = port;
  1333. #endif
  1334. rc = uart_add_one_port(&cdns_uart_uart_driver, port);
  1335. if (rc) {
  1336. dev_err(&pdev->dev,
  1337. "uart_add_one_port() failed; err=%i\n", rc);
  1338. goto err_out_pm_disable;
  1339. }
  1340. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1341. /* This is not port which is used for console that's why clean it up */
  1342. if (cdns_uart_uart_driver.cons->index == -1)
  1343. console_port = NULL;
  1344. #endif
  1345. return 0;
  1346. err_out_pm_disable:
  1347. pm_runtime_disable(&pdev->dev);
  1348. pm_runtime_set_suspended(&pdev->dev);
  1349. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1350. err_out_notif_unreg:
  1351. #ifdef CONFIG_COMMON_CLK
  1352. clk_notifier_unregister(cdns_uart_data->uartclk,
  1353. &cdns_uart_data->clk_rate_change_nb);
  1354. #endif
  1355. err_out_clk_disable:
  1356. clk_disable_unprepare(cdns_uart_data->uartclk);
  1357. err_out_clk_dis_pclk:
  1358. clk_disable_unprepare(cdns_uart_data->pclk);
  1359. return rc;
  1360. }
  1361. /**
  1362. * cdns_uart_remove - called when the platform driver is unregistered
  1363. * @pdev: Pointer to the platform device structure
  1364. *
  1365. * Return: 0 on success, negative errno otherwise
  1366. */
  1367. static int cdns_uart_remove(struct platform_device *pdev)
  1368. {
  1369. struct uart_port *port = platform_get_drvdata(pdev);
  1370. struct cdns_uart *cdns_uart_data = port->private_data;
  1371. int rc;
  1372. /* Remove the cdns_uart port from the serial core */
  1373. #ifdef CONFIG_COMMON_CLK
  1374. clk_notifier_unregister(cdns_uart_data->uartclk,
  1375. &cdns_uart_data->clk_rate_change_nb);
  1376. #endif
  1377. rc = uart_remove_one_port(&cdns_uart_uart_driver, port);
  1378. port->mapbase = 0;
  1379. clk_disable_unprepare(cdns_uart_data->uartclk);
  1380. clk_disable_unprepare(cdns_uart_data->pclk);
  1381. pm_runtime_disable(&pdev->dev);
  1382. pm_runtime_set_suspended(&pdev->dev);
  1383. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1384. return rc;
  1385. }
  1386. static struct platform_driver cdns_uart_platform_driver = {
  1387. .probe = cdns_uart_probe,
  1388. .remove = cdns_uart_remove,
  1389. .driver = {
  1390. .name = CDNS_UART_NAME,
  1391. .of_match_table = cdns_uart_of_match,
  1392. .pm = &cdns_uart_dev_pm_ops,
  1393. },
  1394. };
  1395. static int __init cdns_uart_init(void)
  1396. {
  1397. int retval = 0;
  1398. /* Register the cdns_uart driver with the serial core */
  1399. retval = uart_register_driver(&cdns_uart_uart_driver);
  1400. if (retval)
  1401. return retval;
  1402. /* Register the platform driver */
  1403. retval = platform_driver_register(&cdns_uart_platform_driver);
  1404. if (retval)
  1405. uart_unregister_driver(&cdns_uart_uart_driver);
  1406. return retval;
  1407. }
  1408. static void __exit cdns_uart_exit(void)
  1409. {
  1410. /* Unregister the platform driver */
  1411. platform_driver_unregister(&cdns_uart_platform_driver);
  1412. /* Unregister the cdns_uart driver */
  1413. uart_unregister_driver(&cdns_uart_uart_driver);
  1414. }
  1415. arch_initcall(cdns_uart_init);
  1416. module_exit(cdns_uart_exit);
  1417. MODULE_DESCRIPTION("Driver for Cadence UART");
  1418. MODULE_AUTHOR("Xilinx Inc.");
  1419. MODULE_LICENSE("GPL");