xilinx_uartps.c 43 KB

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