xilinx_uartps.c 46 KB

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