xilinx_uartps.c 32 KB

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