xilinx_uartps.c 31 KB

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