xilinx_uartps.c 31 KB

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