xilinx_uartps.c 31 KB

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