xilinx_uartps.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Xilinx PS UART driver
  3. *
  4. * 2011 (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. */
  13. #include <linux/platform_device.h>
  14. #include <linux/serial.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/console.h>
  19. #include <linux/clk.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/module.h>
  24. #define XUARTPS_TTY_NAME "ttyPS"
  25. #define XUARTPS_NAME "xuartps"
  26. #define XUARTPS_MAJOR 0 /* use dynamic node allocation */
  27. #define XUARTPS_MINOR 0 /* works best with devtmpfs */
  28. #define XUARTPS_NR_PORTS 2
  29. #define XUARTPS_FIFO_SIZE 16 /* FIFO size */
  30. #define XUARTPS_REGISTER_SPACE 0xFFF
  31. #define xuartps_readl(offset) ioread32(port->membase + offset)
  32. #define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
  33. /********************************Register Map********************************/
  34. /** UART
  35. *
  36. * Register offsets for the UART.
  37. *
  38. */
  39. #define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
  40. #define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
  41. #define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
  42. #define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
  43. #define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
  44. #define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
  45. #define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
  46. #define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
  47. #define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
  48. #define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
  49. #define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
  50. #define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
  51. #define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
  52. #define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
  53. #define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
  54. #define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
  55. Width [15:0] */
  56. #define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
  57. Width [7:0] */
  58. #define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
  59. /** Control Register
  60. *
  61. * The Control register (CR) controls the major functions of the device.
  62. *
  63. * Control Register Bit Definitions
  64. */
  65. #define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
  66. #define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
  67. #define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
  68. #define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
  69. #define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
  70. #define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
  71. #define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
  72. #define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
  73. #define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
  74. /** Mode Register
  75. *
  76. * The mode register (MR) defines the mode of transfer as well as the data
  77. * format. If this register is modified during transmission or reception,
  78. * data validity cannot be guaranteed.
  79. *
  80. * Mode Register Bit Definitions
  81. *
  82. */
  83. #define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
  84. #define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
  85. #define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
  86. #define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
  87. #define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
  88. #define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
  89. #define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
  90. #define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
  91. #define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
  92. #define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
  93. #define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
  94. #define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
  95. #define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
  96. /** Interrupt Registers
  97. *
  98. * Interrupt control logic uses the interrupt enable register (IER) and the
  99. * interrupt disable register (IDR) to set the value of the bits in the
  100. * interrupt mask register (IMR). The IMR determines whether to pass an
  101. * interrupt to the interrupt status register (ISR).
  102. * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
  103. * interrupt. IMR and ISR are read only, and IER and IDR are write only.
  104. * Reading either IER or IDR returns 0x00.
  105. *
  106. * All four registers have the same bit definitions.
  107. */
  108. #define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
  109. #define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
  110. #define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
  111. #define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
  112. #define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
  113. #define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
  114. #define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
  115. #define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
  116. #define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
  117. #define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
  118. #define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
  119. /** Channel Status Register
  120. *
  121. * The channel status register (CSR) is provided to enable the control logic
  122. * to monitor the status of bits in the channel interrupt status register,
  123. * even if these are masked out by the interrupt mask register.
  124. */
  125. #define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  126. #define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
  127. #define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
  128. #define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
  129. /**
  130. * xuartps_isr - Interrupt handler
  131. * @irq: Irq number
  132. * @dev_id: Id of the port
  133. *
  134. * Returns IRQHANDLED
  135. **/
  136. static irqreturn_t xuartps_isr(int irq, void *dev_id)
  137. {
  138. struct uart_port *port = (struct uart_port *)dev_id;
  139. unsigned long flags;
  140. unsigned int isrstatus, numbytes;
  141. unsigned int data;
  142. char status = TTY_NORMAL;
  143. spin_lock_irqsave(&port->lock, flags);
  144. /* Read the interrupt status register to determine which
  145. * interrupt(s) is/are active.
  146. */
  147. isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
  148. /* drop byte with parity error if IGNPAR specified */
  149. if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
  150. isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
  151. isrstatus &= port->read_status_mask;
  152. isrstatus &= ~port->ignore_status_mask;
  153. if ((isrstatus & XUARTPS_IXR_TOUT) ||
  154. (isrstatus & XUARTPS_IXR_RXTRIG)) {
  155. /* Receive Timeout Interrupt */
  156. while ((xuartps_readl(XUARTPS_SR_OFFSET) &
  157. XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
  158. data = xuartps_readl(XUARTPS_FIFO_OFFSET);
  159. port->icount.rx++;
  160. if (isrstatus & XUARTPS_IXR_PARITY) {
  161. port->icount.parity++;
  162. status = TTY_PARITY;
  163. } else if (isrstatus & XUARTPS_IXR_FRAMING) {
  164. port->icount.frame++;
  165. status = TTY_FRAME;
  166. } else if (isrstatus & XUARTPS_IXR_OVERRUN)
  167. port->icount.overrun++;
  168. uart_insert_char(port, isrstatus, XUARTPS_IXR_OVERRUN,
  169. data, status);
  170. }
  171. spin_unlock(&port->lock);
  172. tty_flip_buffer_push(&port->state->port);
  173. spin_lock(&port->lock);
  174. }
  175. /* Dispatch an appropriate handler */
  176. if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
  177. if (uart_circ_empty(&port->state->xmit)) {
  178. xuartps_writel(XUARTPS_IXR_TXEMPTY,
  179. XUARTPS_IDR_OFFSET);
  180. } else {
  181. numbytes = port->fifosize;
  182. /* Break if no more data available in the UART buffer */
  183. while (numbytes--) {
  184. if (uart_circ_empty(&port->state->xmit))
  185. break;
  186. /* Get the data from the UART circular buffer
  187. * and write it to the xuartps's TX_FIFO
  188. * register.
  189. */
  190. xuartps_writel(
  191. port->state->xmit.buf[port->state->xmit.
  192. tail], XUARTPS_FIFO_OFFSET);
  193. port->icount.tx++;
  194. /* Adjust the tail of the UART buffer and wrap
  195. * the buffer if it reaches limit.
  196. */
  197. port->state->xmit.tail =
  198. (port->state->xmit.tail + 1) & \
  199. (UART_XMIT_SIZE - 1);
  200. }
  201. if (uart_circ_chars_pending(
  202. &port->state->xmit) < WAKEUP_CHARS)
  203. uart_write_wakeup(port);
  204. }
  205. }
  206. xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
  207. /* be sure to release the lock and tty before leaving */
  208. spin_unlock_irqrestore(&port->lock, flags);
  209. return IRQ_HANDLED;
  210. }
  211. /**
  212. * xuartps_set_baud_rate - Calculate and set the baud rate
  213. * @port: Handle to the uart port structure
  214. * @baud: Baud rate to set
  215. *
  216. * Returns baud rate, requested baud when possible, or actual baud when there
  217. * was too much error
  218. **/
  219. static unsigned int xuartps_set_baud_rate(struct uart_port *port,
  220. unsigned int baud)
  221. {
  222. unsigned int sel_clk;
  223. unsigned int calc_baud = 0;
  224. unsigned int brgr_val, brdiv_val;
  225. unsigned int bauderror;
  226. /* Formula to obtain baud rate is
  227. * baud_tx/rx rate = sel_clk/CD * (BDIV + 1)
  228. * input_clk = (Uart User Defined Clock or Apb Clock)
  229. * depends on UCLKEN in MR Reg
  230. * sel_clk = input_clk or input_clk/8;
  231. * depends on CLKS in MR reg
  232. * CD and BDIV depends on values in
  233. * baud rate generate register
  234. * baud rate clock divisor register
  235. */
  236. sel_clk = port->uartclk;
  237. if (xuartps_readl(XUARTPS_MR_OFFSET) & XUARTPS_MR_CLKSEL)
  238. sel_clk = sel_clk / 8;
  239. /* Find the best values for baud generation */
  240. for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
  241. brgr_val = sel_clk / (baud * (brdiv_val + 1));
  242. if (brgr_val < 2 || brgr_val > 65535)
  243. continue;
  244. calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
  245. if (baud > calc_baud)
  246. bauderror = baud - calc_baud;
  247. else
  248. bauderror = calc_baud - baud;
  249. /* use the values when percent error is acceptable */
  250. if (((bauderror * 100) / baud) < 3) {
  251. calc_baud = baud;
  252. break;
  253. }
  254. }
  255. /* Set the values for the new baud rate */
  256. xuartps_writel(brgr_val, XUARTPS_BAUDGEN_OFFSET);
  257. xuartps_writel(brdiv_val, XUARTPS_BAUDDIV_OFFSET);
  258. return calc_baud;
  259. }
  260. /*----------------------Uart Operations---------------------------*/
  261. /**
  262. * xuartps_start_tx - Start transmitting bytes
  263. * @port: Handle to the uart port structure
  264. *
  265. **/
  266. static void xuartps_start_tx(struct uart_port *port)
  267. {
  268. unsigned int status, numbytes = port->fifosize;
  269. if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
  270. return;
  271. status = xuartps_readl(XUARTPS_CR_OFFSET);
  272. /* Set the TX enable bit and clear the TX disable bit to enable the
  273. * transmitter.
  274. */
  275. xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
  276. XUARTPS_CR_OFFSET);
  277. while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
  278. & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
  279. /* Break if no more data available in the UART buffer */
  280. if (uart_circ_empty(&port->state->xmit))
  281. break;
  282. /* Get the data from the UART circular buffer and
  283. * write it to the xuartps's TX_FIFO register.
  284. */
  285. xuartps_writel(
  286. port->state->xmit.buf[port->state->xmit.tail],
  287. XUARTPS_FIFO_OFFSET);
  288. port->icount.tx++;
  289. /* Adjust the tail of the UART buffer and wrap
  290. * the buffer if it reaches limit.
  291. */
  292. port->state->xmit.tail = (port->state->xmit.tail + 1) &
  293. (UART_XMIT_SIZE - 1);
  294. }
  295. /* Enable the TX Empty interrupt */
  296. xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
  297. if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
  298. uart_write_wakeup(port);
  299. }
  300. /**
  301. * xuartps_stop_tx - Stop TX
  302. * @port: Handle to the uart port structure
  303. *
  304. **/
  305. static void xuartps_stop_tx(struct uart_port *port)
  306. {
  307. unsigned int regval;
  308. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  309. regval |= XUARTPS_CR_TX_DIS;
  310. /* Disable the transmitter */
  311. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  312. }
  313. /**
  314. * xuartps_stop_rx - Stop RX
  315. * @port: Handle to the uart port structure
  316. *
  317. **/
  318. static void xuartps_stop_rx(struct uart_port *port)
  319. {
  320. unsigned int regval;
  321. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  322. regval |= XUARTPS_CR_RX_DIS;
  323. /* Disable the receiver */
  324. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  325. }
  326. /**
  327. * xuartps_tx_empty - Check whether TX is empty
  328. * @port: Handle to the uart port structure
  329. *
  330. * Returns TIOCSER_TEMT on success, 0 otherwise
  331. **/
  332. static unsigned int xuartps_tx_empty(struct uart_port *port)
  333. {
  334. unsigned int status;
  335. status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
  336. return status ? TIOCSER_TEMT : 0;
  337. }
  338. /**
  339. * xuartps_break_ctl - Based on the input ctl we have to start or stop
  340. * transmitting char breaks
  341. * @port: Handle to the uart port structure
  342. * @ctl: Value based on which start or stop decision is taken
  343. *
  344. **/
  345. static void xuartps_break_ctl(struct uart_port *port, int ctl)
  346. {
  347. unsigned int status;
  348. unsigned long flags;
  349. spin_lock_irqsave(&port->lock, flags);
  350. status = xuartps_readl(XUARTPS_CR_OFFSET);
  351. if (ctl == -1)
  352. xuartps_writel(XUARTPS_CR_STARTBRK | status,
  353. XUARTPS_CR_OFFSET);
  354. else {
  355. if ((status & XUARTPS_CR_STOPBRK) == 0)
  356. xuartps_writel(XUARTPS_CR_STOPBRK | status,
  357. XUARTPS_CR_OFFSET);
  358. }
  359. spin_unlock_irqrestore(&port->lock, flags);
  360. }
  361. /**
  362. * xuartps_set_termios - termios operations, handling data length, parity,
  363. * stop bits, flow control, baud rate
  364. * @port: Handle to the uart port structure
  365. * @termios: Handle to the input termios structure
  366. * @old: Values of the previously saved termios structure
  367. *
  368. **/
  369. static void xuartps_set_termios(struct uart_port *port,
  370. struct ktermios *termios, struct ktermios *old)
  371. {
  372. unsigned int cval = 0;
  373. unsigned int baud;
  374. unsigned long flags;
  375. unsigned int ctrl_reg, mode_reg;
  376. spin_lock_irqsave(&port->lock, flags);
  377. /* Empty the receive FIFO 1st before making changes */
  378. while ((xuartps_readl(XUARTPS_SR_OFFSET) &
  379. XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
  380. xuartps_readl(XUARTPS_FIFO_OFFSET);
  381. }
  382. /* Disable the TX and RX to set baud rate */
  383. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  384. (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
  385. XUARTPS_CR_OFFSET);
  386. /* Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk */
  387. baud = uart_get_baud_rate(port, termios, old, 0, 10000000);
  388. baud = xuartps_set_baud_rate(port, baud);
  389. if (tty_termios_baud_rate(termios))
  390. tty_termios_encode_baud_rate(termios, baud, baud);
  391. /*
  392. * Update the per-port timeout.
  393. */
  394. uart_update_timeout(port, termios->c_cflag, baud);
  395. /* Set TX/RX Reset */
  396. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  397. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  398. XUARTPS_CR_OFFSET);
  399. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  400. /* Clear the RX disable and TX disable bits and then set the TX enable
  401. * bit and RX enable bit to enable the transmitter and receiver.
  402. */
  403. xuartps_writel(
  404. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  405. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  406. XUARTPS_CR_OFFSET);
  407. xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
  408. port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
  409. XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
  410. port->ignore_status_mask = 0;
  411. if (termios->c_iflag & INPCK)
  412. port->read_status_mask |= XUARTPS_IXR_PARITY |
  413. XUARTPS_IXR_FRAMING;
  414. if (termios->c_iflag & IGNPAR)
  415. port->ignore_status_mask |= XUARTPS_IXR_PARITY |
  416. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  417. /* ignore all characters if CREAD is not set */
  418. if ((termios->c_cflag & CREAD) == 0)
  419. port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
  420. XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
  421. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  422. mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
  423. /* Handling Data Size */
  424. switch (termios->c_cflag & CSIZE) {
  425. case CS6:
  426. cval |= XUARTPS_MR_CHARLEN_6_BIT;
  427. break;
  428. case CS7:
  429. cval |= XUARTPS_MR_CHARLEN_7_BIT;
  430. break;
  431. default:
  432. case CS8:
  433. cval |= XUARTPS_MR_CHARLEN_8_BIT;
  434. termios->c_cflag &= ~CSIZE;
  435. termios->c_cflag |= CS8;
  436. break;
  437. }
  438. /* Handling Parity and Stop Bits length */
  439. if (termios->c_cflag & CSTOPB)
  440. cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  441. else
  442. cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  443. if (termios->c_cflag & PARENB) {
  444. /* Mark or Space parity */
  445. if (termios->c_cflag & CMSPAR) {
  446. if (termios->c_cflag & PARODD)
  447. cval |= XUARTPS_MR_PARITY_MARK;
  448. else
  449. cval |= XUARTPS_MR_PARITY_SPACE;
  450. } else if (termios->c_cflag & PARODD)
  451. cval |= XUARTPS_MR_PARITY_ODD;
  452. else
  453. cval |= XUARTPS_MR_PARITY_EVEN;
  454. } else
  455. cval |= XUARTPS_MR_PARITY_NONE;
  456. xuartps_writel(cval , XUARTPS_MR_OFFSET);
  457. spin_unlock_irqrestore(&port->lock, flags);
  458. }
  459. /**
  460. * xuartps_startup - Called when an application opens a xuartps port
  461. * @port: Handle to the uart port structure
  462. *
  463. * Returns 0 on success, negative error otherwise
  464. **/
  465. static int xuartps_startup(struct uart_port *port)
  466. {
  467. unsigned int retval = 0, status = 0;
  468. retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
  469. (void *)port);
  470. if (retval)
  471. return retval;
  472. /* Disable the TX and RX */
  473. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  474. XUARTPS_CR_OFFSET);
  475. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  476. * no break chars.
  477. */
  478. xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
  479. XUARTPS_CR_OFFSET);
  480. status = xuartps_readl(XUARTPS_CR_OFFSET);
  481. /* Clear the RX disable and TX disable bits and then set the TX enable
  482. * bit and RX enable bit to enable the transmitter and receiver.
  483. */
  484. xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  485. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
  486. XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
  487. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  488. * no parity.
  489. */
  490. xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
  491. | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
  492. XUARTPS_MR_OFFSET);
  493. /* Set the RX FIFO Trigger level to 14 assuming FIFO size as 16 */
  494. xuartps_writel(14, XUARTPS_RXWM_OFFSET);
  495. /* Receive Timeout register is enabled with value of 10 */
  496. xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
  497. /* Clear out any pending interrupts before enabling them */
  498. xuartps_writel(xuartps_readl(XUARTPS_ISR_OFFSET), XUARTPS_ISR_OFFSET);
  499. /* Set the Interrupt Registers with desired interrupts */
  500. xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
  501. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
  502. XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
  503. xuartps_writel(~(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
  504. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
  505. XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT), XUARTPS_IDR_OFFSET);
  506. return retval;
  507. }
  508. /**
  509. * xuartps_shutdown - Called when an application closes a xuartps port
  510. * @port: Handle to the uart port structure
  511. *
  512. **/
  513. static void xuartps_shutdown(struct uart_port *port)
  514. {
  515. int status;
  516. /* Disable interrupts */
  517. status = xuartps_readl(XUARTPS_IMR_OFFSET);
  518. xuartps_writel(status, XUARTPS_IDR_OFFSET);
  519. /* Disable the TX and RX */
  520. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  521. XUARTPS_CR_OFFSET);
  522. free_irq(port->irq, port);
  523. }
  524. /**
  525. * xuartps_type - Set UART type to xuartps port
  526. * @port: Handle to the uart port structure
  527. *
  528. * Returns string on success, NULL otherwise
  529. **/
  530. static const char *xuartps_type(struct uart_port *port)
  531. {
  532. return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
  533. }
  534. /**
  535. * xuartps_verify_port - Verify the port params
  536. * @port: Handle to the uart port structure
  537. * @ser: Handle to the structure whose members are compared
  538. *
  539. * Returns 0 if success otherwise -EINVAL
  540. **/
  541. static int xuartps_verify_port(struct uart_port *port,
  542. struct serial_struct *ser)
  543. {
  544. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  545. return -EINVAL;
  546. if (port->irq != ser->irq)
  547. return -EINVAL;
  548. if (ser->io_type != UPIO_MEM)
  549. return -EINVAL;
  550. if (port->iobase != ser->port)
  551. return -EINVAL;
  552. if (ser->hub6 != 0)
  553. return -EINVAL;
  554. return 0;
  555. }
  556. /**
  557. * xuartps_request_port - Claim the memory region attached to xuartps port,
  558. * called when the driver adds a xuartps port via
  559. * uart_add_one_port()
  560. * @port: Handle to the uart port structure
  561. *
  562. * Returns 0, -ENOMEM if request fails
  563. **/
  564. static int xuartps_request_port(struct uart_port *port)
  565. {
  566. if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
  567. XUARTPS_NAME)) {
  568. return -ENOMEM;
  569. }
  570. port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
  571. if (!port->membase) {
  572. dev_err(port->dev, "Unable to map registers\n");
  573. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  574. return -ENOMEM;
  575. }
  576. return 0;
  577. }
  578. /**
  579. * xuartps_release_port - Release the memory region attached to a xuartps
  580. * port, called when the driver removes a xuartps
  581. * port via uart_remove_one_port().
  582. * @port: Handle to the uart port structure
  583. *
  584. **/
  585. static void xuartps_release_port(struct uart_port *port)
  586. {
  587. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  588. iounmap(port->membase);
  589. port->membase = NULL;
  590. }
  591. /**
  592. * xuartps_config_port - Configure xuartps, called when the driver adds a
  593. * xuartps port
  594. * @port: Handle to the uart port structure
  595. * @flags: If any
  596. *
  597. **/
  598. static void xuartps_config_port(struct uart_port *port, int flags)
  599. {
  600. if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
  601. port->type = PORT_XUARTPS;
  602. }
  603. /**
  604. * xuartps_get_mctrl - Get the modem control state
  605. *
  606. * @port: Handle to the uart port structure
  607. *
  608. * Returns the modem control state
  609. *
  610. **/
  611. static unsigned int xuartps_get_mctrl(struct uart_port *port)
  612. {
  613. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  614. }
  615. static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
  616. {
  617. /* N/A */
  618. }
  619. static void xuartps_enable_ms(struct uart_port *port)
  620. {
  621. /* N/A */
  622. }
  623. /** The UART operations structure
  624. */
  625. static struct uart_ops xuartps_ops = {
  626. .set_mctrl = xuartps_set_mctrl,
  627. .get_mctrl = xuartps_get_mctrl,
  628. .enable_ms = xuartps_enable_ms,
  629. .start_tx = xuartps_start_tx, /* Start transmitting */
  630. .stop_tx = xuartps_stop_tx, /* Stop transmission */
  631. .stop_rx = xuartps_stop_rx, /* Stop reception */
  632. .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
  633. .break_ctl = xuartps_break_ctl, /* Start/stop
  634. * transmitting break
  635. */
  636. .set_termios = xuartps_set_termios, /* Set termios */
  637. .startup = xuartps_startup, /* App opens xuartps */
  638. .shutdown = xuartps_shutdown, /* App closes xuartps */
  639. .type = xuartps_type, /* Set UART type */
  640. .verify_port = xuartps_verify_port, /* Verification of port
  641. * params
  642. */
  643. .request_port = xuartps_request_port, /* Claim resources
  644. * associated with a
  645. * xuartps port
  646. */
  647. .release_port = xuartps_release_port, /* Release resources
  648. * associated with a
  649. * xuartps port
  650. */
  651. .config_port = xuartps_config_port, /* Configure when driver
  652. * adds a xuartps port
  653. */
  654. };
  655. static struct uart_port xuartps_port[2];
  656. /**
  657. * xuartps_get_port - Configure the port from the platform device resource
  658. * info
  659. *
  660. * Returns a pointer to a uart_port or NULL for failure
  661. **/
  662. static struct uart_port *xuartps_get_port(void)
  663. {
  664. struct uart_port *port;
  665. int id;
  666. /* Find the next unused port */
  667. for (id = 0; id < XUARTPS_NR_PORTS; id++)
  668. if (xuartps_port[id].mapbase == 0)
  669. break;
  670. if (id >= XUARTPS_NR_PORTS)
  671. return NULL;
  672. port = &xuartps_port[id];
  673. /* At this point, we've got an empty uart_port struct, initialize it */
  674. spin_lock_init(&port->lock);
  675. port->membase = NULL;
  676. port->iobase = 1; /* mark port in use */
  677. port->irq = 0;
  678. port->type = PORT_UNKNOWN;
  679. port->iotype = UPIO_MEM32;
  680. port->flags = UPF_BOOT_AUTOCONF;
  681. port->ops = &xuartps_ops;
  682. port->fifosize = XUARTPS_FIFO_SIZE;
  683. port->line = id;
  684. port->dev = NULL;
  685. return port;
  686. }
  687. /*-----------------------Console driver operations--------------------------*/
  688. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  689. /**
  690. * xuartps_console_wait_tx - Wait for the TX to be full
  691. * @port: Handle to the uart port structure
  692. *
  693. **/
  694. static void xuartps_console_wait_tx(struct uart_port *port)
  695. {
  696. while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
  697. != XUARTPS_SR_TXEMPTY)
  698. barrier();
  699. }
  700. /**
  701. * xuartps_console_putchar - write the character to the FIFO buffer
  702. * @port: Handle to the uart port structure
  703. * @ch: Character to be written
  704. *
  705. **/
  706. static void xuartps_console_putchar(struct uart_port *port, int ch)
  707. {
  708. xuartps_console_wait_tx(port);
  709. xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
  710. }
  711. /**
  712. * xuartps_console_write - perform write operation
  713. * @port: Handle to the uart port structure
  714. * @s: Pointer to character array
  715. * @count: No of characters
  716. **/
  717. static void xuartps_console_write(struct console *co, const char *s,
  718. unsigned int count)
  719. {
  720. struct uart_port *port = &xuartps_port[co->index];
  721. unsigned long flags;
  722. unsigned int imr;
  723. int locked = 1;
  724. if (oops_in_progress)
  725. locked = spin_trylock_irqsave(&port->lock, flags);
  726. else
  727. spin_lock_irqsave(&port->lock, flags);
  728. /* save and disable interrupt */
  729. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  730. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  731. uart_console_write(port, s, count, xuartps_console_putchar);
  732. xuartps_console_wait_tx(port);
  733. /* restore interrupt state, it seems like there may be a h/w bug
  734. * in that the interrupt enable register should not need to be
  735. * written based on the data sheet
  736. */
  737. xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
  738. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  739. if (locked)
  740. spin_unlock_irqrestore(&port->lock, flags);
  741. }
  742. /**
  743. * xuartps_console_setup - Initialize the uart to default config
  744. * @co: Console handle
  745. * @options: Initial settings of uart
  746. *
  747. * Returns 0, -ENODEV if no device
  748. **/
  749. static int __init xuartps_console_setup(struct console *co, char *options)
  750. {
  751. struct uart_port *port = &xuartps_port[co->index];
  752. int baud = 9600;
  753. int bits = 8;
  754. int parity = 'n';
  755. int flow = 'n';
  756. if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
  757. return -EINVAL;
  758. if (!port->mapbase) {
  759. pr_debug("console on ttyPS%i not present\n", co->index);
  760. return -ENODEV;
  761. }
  762. if (options)
  763. uart_parse_options(options, &baud, &parity, &bits, &flow);
  764. return uart_set_options(port, co, baud, parity, bits, flow);
  765. }
  766. static struct uart_driver xuartps_uart_driver;
  767. static struct console xuartps_console = {
  768. .name = XUARTPS_TTY_NAME,
  769. .write = xuartps_console_write,
  770. .device = uart_console_device,
  771. .setup = xuartps_console_setup,
  772. .flags = CON_PRINTBUFFER,
  773. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  774. .data = &xuartps_uart_driver,
  775. };
  776. /**
  777. * xuartps_console_init - Initialization call
  778. *
  779. * Returns 0 on success, negative error otherwise
  780. **/
  781. static int __init xuartps_console_init(void)
  782. {
  783. register_console(&xuartps_console);
  784. return 0;
  785. }
  786. console_initcall(xuartps_console_init);
  787. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  788. /** Structure Definitions
  789. */
  790. static struct uart_driver xuartps_uart_driver = {
  791. .owner = THIS_MODULE, /* Owner */
  792. .driver_name = XUARTPS_NAME, /* Driver name */
  793. .dev_name = XUARTPS_TTY_NAME, /* Node name */
  794. .major = XUARTPS_MAJOR, /* Major number */
  795. .minor = XUARTPS_MINOR, /* Minor number */
  796. .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
  797. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  798. .cons = &xuartps_console, /* Console */
  799. #endif
  800. };
  801. /* ---------------------------------------------------------------------
  802. * Platform bus binding
  803. */
  804. /**
  805. * xuartps_probe - Platform driver probe
  806. * @pdev: Pointer to the platform device structure
  807. *
  808. * Returns 0 on success, negative error otherwise
  809. **/
  810. static int xuartps_probe(struct platform_device *pdev)
  811. {
  812. int rc;
  813. struct uart_port *port;
  814. struct resource *res, *res2;
  815. struct clk *clk;
  816. clk = of_clk_get(pdev->dev.of_node, 0);
  817. if (IS_ERR(clk)) {
  818. dev_err(&pdev->dev, "no clock specified\n");
  819. return PTR_ERR(clk);
  820. }
  821. rc = clk_prepare_enable(clk);
  822. if (rc) {
  823. dev_err(&pdev->dev, "could not enable clock\n");
  824. return -EBUSY;
  825. }
  826. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  827. if (!res)
  828. return -ENODEV;
  829. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  830. if (!res2)
  831. return -ENODEV;
  832. /* Initialize the port structure */
  833. port = xuartps_get_port();
  834. if (!port) {
  835. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  836. return -ENODEV;
  837. } else {
  838. /* Register the port.
  839. * This function also registers this device with the tty layer
  840. * and triggers invocation of the config_port() entry point.
  841. */
  842. port->mapbase = res->start;
  843. port->irq = res2->start;
  844. port->dev = &pdev->dev;
  845. port->uartclk = clk_get_rate(clk);
  846. port->private_data = clk;
  847. dev_set_drvdata(&pdev->dev, port);
  848. rc = uart_add_one_port(&xuartps_uart_driver, port);
  849. if (rc) {
  850. dev_err(&pdev->dev,
  851. "uart_add_one_port() failed; err=%i\n", rc);
  852. dev_set_drvdata(&pdev->dev, NULL);
  853. return rc;
  854. }
  855. return 0;
  856. }
  857. }
  858. /**
  859. * xuartps_remove - called when the platform driver is unregistered
  860. * @pdev: Pointer to the platform device structure
  861. *
  862. * Returns 0 on success, negative error otherwise
  863. **/
  864. static int xuartps_remove(struct platform_device *pdev)
  865. {
  866. struct uart_port *port = dev_get_drvdata(&pdev->dev);
  867. struct clk *clk = port->private_data;
  868. int rc;
  869. /* Remove the xuartps port from the serial core */
  870. rc = uart_remove_one_port(&xuartps_uart_driver, port);
  871. dev_set_drvdata(&pdev->dev, NULL);
  872. port->mapbase = 0;
  873. clk_disable_unprepare(clk);
  874. return rc;
  875. }
  876. /**
  877. * xuartps_suspend - suspend event
  878. * @pdev: Pointer to the platform device structure
  879. * @state: State of the device
  880. *
  881. * Returns 0
  882. **/
  883. static int xuartps_suspend(struct platform_device *pdev, pm_message_t state)
  884. {
  885. /* Call the API provided in serial_core.c file which handles
  886. * the suspend.
  887. */
  888. uart_suspend_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
  889. return 0;
  890. }
  891. /**
  892. * xuartps_resume - Resume after a previous suspend
  893. * @pdev: Pointer to the platform device structure
  894. *
  895. * Returns 0
  896. **/
  897. static int xuartps_resume(struct platform_device *pdev)
  898. {
  899. uart_resume_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
  900. return 0;
  901. }
  902. /* Match table for of_platform binding */
  903. static struct of_device_id xuartps_of_match[] = {
  904. { .compatible = "xlnx,xuartps", },
  905. {}
  906. };
  907. MODULE_DEVICE_TABLE(of, xuartps_of_match);
  908. static struct platform_driver xuartps_platform_driver = {
  909. .probe = xuartps_probe, /* Probe method */
  910. .remove = xuartps_remove, /* Detach method */
  911. .suspend = xuartps_suspend, /* Suspend */
  912. .resume = xuartps_resume, /* Resume after a suspend */
  913. .driver = {
  914. .owner = THIS_MODULE,
  915. .name = XUARTPS_NAME, /* Driver name */
  916. .of_match_table = xuartps_of_match,
  917. },
  918. };
  919. /* ---------------------------------------------------------------------
  920. * Module Init and Exit
  921. */
  922. /**
  923. * xuartps_init - Initial driver registration call
  924. *
  925. * Returns whether the registration was successful or not
  926. **/
  927. static int __init xuartps_init(void)
  928. {
  929. int retval = 0;
  930. /* Register the xuartps driver with the serial core */
  931. retval = uart_register_driver(&xuartps_uart_driver);
  932. if (retval)
  933. return retval;
  934. /* Register the platform driver */
  935. retval = platform_driver_register(&xuartps_platform_driver);
  936. if (retval)
  937. uart_unregister_driver(&xuartps_uart_driver);
  938. return retval;
  939. }
  940. /**
  941. * xuartps_exit - Driver unregistration call
  942. **/
  943. static void __exit xuartps_exit(void)
  944. {
  945. /* The order of unregistration is important. Unregister the
  946. * UART driver before the platform driver crashes the system.
  947. */
  948. /* Unregister the platform driver */
  949. platform_driver_unregister(&xuartps_platform_driver);
  950. /* Unregister the xuartps driver */
  951. uart_unregister_driver(&xuartps_uart_driver);
  952. }
  953. module_init(xuartps_init);
  954. module_exit(xuartps_exit);
  955. MODULE_DESCRIPTION("Driver for PS UART");
  956. MODULE_AUTHOR("Xilinx Inc.");
  957. MODULE_LICENSE("GPL");