xilinx_uartps.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Xilinx PS UART driver
  3. *
  4. * 2011 - 2013 (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. #if defined(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  14. #define SUPPORT_SYSRQ
  15. #endif
  16. #include <linux/platform_device.h>
  17. #include <linux/serial.h>
  18. #include <linux/console.h>
  19. #include <linux/serial_core.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/clk.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/of.h>
  27. #include <linux/module.h>
  28. #define XUARTPS_TTY_NAME "ttyPS"
  29. #define XUARTPS_NAME "xuartps"
  30. #define XUARTPS_MAJOR 0 /* use dynamic node allocation */
  31. #define XUARTPS_MINOR 0 /* works best with devtmpfs */
  32. #define XUARTPS_NR_PORTS 2
  33. #define XUARTPS_FIFO_SIZE 64 /* FIFO size */
  34. #define XUARTPS_REGISTER_SPACE 0xFFF
  35. #define xuartps_readl(offset) ioread32(port->membase + offset)
  36. #define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
  37. /* Rx Trigger level */
  38. static int rx_trigger_level = 56;
  39. module_param(rx_trigger_level, uint, S_IRUGO);
  40. MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
  41. /* Rx Timeout */
  42. static int rx_timeout = 10;
  43. module_param(rx_timeout, uint, S_IRUGO);
  44. MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
  45. /********************************Register Map********************************/
  46. /** UART
  47. *
  48. * Register offsets for the UART.
  49. *
  50. */
  51. #define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
  52. #define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
  53. #define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
  54. #define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
  55. #define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
  56. #define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
  57. #define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
  58. #define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
  59. #define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
  60. #define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
  61. #define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
  62. #define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
  63. #define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
  64. #define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
  65. #define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
  66. #define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
  67. Width [15:0] */
  68. #define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
  69. Width [7:0] */
  70. #define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
  71. /** Control Register
  72. *
  73. * The Control register (CR) controls the major functions of the device.
  74. *
  75. * Control Register Bit Definitions
  76. */
  77. #define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
  78. #define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
  79. #define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
  80. #define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
  81. #define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
  82. #define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
  83. #define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
  84. #define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
  85. #define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
  86. /** Mode Register
  87. *
  88. * The mode register (MR) defines the mode of transfer as well as the data
  89. * format. If this register is modified during transmission or reception,
  90. * data validity cannot be guaranteed.
  91. *
  92. * Mode Register Bit Definitions
  93. *
  94. */
  95. #define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
  96. #define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
  97. #define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
  98. #define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
  99. #define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
  100. #define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
  101. #define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
  102. #define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
  103. #define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
  104. #define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
  105. #define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
  106. #define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
  107. #define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
  108. /** Interrupt Registers
  109. *
  110. * Interrupt control logic uses the interrupt enable register (IER) and the
  111. * interrupt disable register (IDR) to set the value of the bits in the
  112. * interrupt mask register (IMR). The IMR determines whether to pass an
  113. * interrupt to the interrupt status register (ISR).
  114. * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
  115. * interrupt. IMR and ISR are read only, and IER and IDR are write only.
  116. * Reading either IER or IDR returns 0x00.
  117. *
  118. * All four registers have the same bit definitions.
  119. */
  120. #define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
  121. #define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
  122. #define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
  123. #define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
  124. #define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
  125. #define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
  126. #define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
  127. #define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
  128. #define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
  129. #define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
  130. #define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
  131. /* Goes in read_status_mask for break detection as the HW doesn't do it*/
  132. #define XUARTPS_IXR_BRK 0x80000000
  133. /** Channel Status Register
  134. *
  135. * The channel status register (CSR) is provided to enable the control logic
  136. * to monitor the status of bits in the channel interrupt status register,
  137. * even if these are masked out by the interrupt mask register.
  138. */
  139. #define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  140. #define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
  141. #define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
  142. #define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
  143. /* baud dividers min/max values */
  144. #define XUARTPS_BDIV_MIN 4
  145. #define XUARTPS_BDIV_MAX 255
  146. #define XUARTPS_CD_MAX 65535
  147. /**
  148. * struct xuartps - device data
  149. * @port Pointer to the UART port
  150. * @refclk Reference clock
  151. * @aperclk APB clock
  152. * @baud Current baud rate
  153. * @clk_rate_change_nb Notifier block for clock changes
  154. */
  155. struct xuartps {
  156. struct uart_port *port;
  157. struct clk *refclk;
  158. struct clk *aperclk;
  159. unsigned int baud;
  160. struct notifier_block clk_rate_change_nb;
  161. };
  162. #define to_xuartps(_nb) container_of(_nb, struct xuartps, clk_rate_change_nb);
  163. /**
  164. * xuartps_isr - Interrupt handler
  165. * @irq: Irq number
  166. * @dev_id: Id of the port
  167. *
  168. * Returns IRQHANDLED
  169. **/
  170. static irqreturn_t xuartps_isr(int irq, void *dev_id)
  171. {
  172. struct uart_port *port = (struct uart_port *)dev_id;
  173. unsigned long flags;
  174. unsigned int isrstatus, numbytes;
  175. unsigned int data;
  176. char status = TTY_NORMAL;
  177. spin_lock_irqsave(&port->lock, flags);
  178. /* Read the interrupt status register to determine which
  179. * interrupt(s) is/are active.
  180. */
  181. isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
  182. /*
  183. * There is no hardware break detection, so we interpret framing
  184. * error with all-zeros data as a break sequence. Most of the time,
  185. * there's another non-zero byte at the end of the sequence.
  186. */
  187. if (isrstatus & XUARTPS_IXR_FRAMING) {
  188. while (!(xuartps_readl(XUARTPS_SR_OFFSET) &
  189. XUARTPS_SR_RXEMPTY)) {
  190. if (!xuartps_readl(XUARTPS_FIFO_OFFSET)) {
  191. port->read_status_mask |= XUARTPS_IXR_BRK;
  192. isrstatus &= ~XUARTPS_IXR_FRAMING;
  193. }
  194. }
  195. xuartps_writel(XUARTPS_IXR_FRAMING, XUARTPS_ISR_OFFSET);
  196. }
  197. /* drop byte with parity error if IGNPAR specified */
  198. if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
  199. isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
  200. isrstatus &= port->read_status_mask;
  201. isrstatus &= ~port->ignore_status_mask;
  202. if ((isrstatus & XUARTPS_IXR_TOUT) ||
  203. (isrstatus & XUARTPS_IXR_RXTRIG)) {
  204. /* Receive Timeout Interrupt */
  205. while ((xuartps_readl(XUARTPS_SR_OFFSET) &
  206. XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
  207. data = xuartps_readl(XUARTPS_FIFO_OFFSET);
  208. /* Non-NULL byte after BREAK is garbage (99%) */
  209. if (data && (port->read_status_mask &
  210. XUARTPS_IXR_BRK)) {
  211. port->read_status_mask &= ~XUARTPS_IXR_BRK;
  212. port->icount.brk++;
  213. if (uart_handle_break(port))
  214. continue;
  215. }
  216. /*
  217. * uart_handle_sysrq_char() doesn't work if
  218. * spinlocked, for some reason
  219. */
  220. if (port->sysrq) {
  221. spin_unlock(&port->lock);
  222. if (uart_handle_sysrq_char(port,
  223. (unsigned char)data)) {
  224. spin_lock(&port->lock);
  225. continue;
  226. }
  227. spin_lock(&port->lock);
  228. }
  229. port->icount.rx++;
  230. if (isrstatus & XUARTPS_IXR_PARITY) {
  231. port->icount.parity++;
  232. status = TTY_PARITY;
  233. } else if (isrstatus & XUARTPS_IXR_FRAMING) {
  234. port->icount.frame++;
  235. status = TTY_FRAME;
  236. } else if (isrstatus & XUARTPS_IXR_OVERRUN)
  237. port->icount.overrun++;
  238. uart_insert_char(port, isrstatus, XUARTPS_IXR_OVERRUN,
  239. data, status);
  240. }
  241. spin_unlock(&port->lock);
  242. tty_flip_buffer_push(&port->state->port);
  243. spin_lock(&port->lock);
  244. }
  245. /* Dispatch an appropriate handler */
  246. if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
  247. if (uart_circ_empty(&port->state->xmit)) {
  248. xuartps_writel(XUARTPS_IXR_TXEMPTY,
  249. XUARTPS_IDR_OFFSET);
  250. } else {
  251. numbytes = port->fifosize;
  252. /* Break if no more data available in the UART buffer */
  253. while (numbytes--) {
  254. if (uart_circ_empty(&port->state->xmit))
  255. break;
  256. /* Get the data from the UART circular buffer
  257. * and write it to the xuartps's TX_FIFO
  258. * register.
  259. */
  260. xuartps_writel(
  261. port->state->xmit.buf[port->state->xmit.
  262. tail], XUARTPS_FIFO_OFFSET);
  263. port->icount.tx++;
  264. /* Adjust the tail of the UART buffer and wrap
  265. * the buffer if it reaches limit.
  266. */
  267. port->state->xmit.tail =
  268. (port->state->xmit.tail + 1) & \
  269. (UART_XMIT_SIZE - 1);
  270. }
  271. if (uart_circ_chars_pending(
  272. &port->state->xmit) < WAKEUP_CHARS)
  273. uart_write_wakeup(port);
  274. }
  275. }
  276. xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
  277. /* be sure to release the lock and tty before leaving */
  278. spin_unlock_irqrestore(&port->lock, flags);
  279. return IRQ_HANDLED;
  280. }
  281. /**
  282. * xuartps_calc_baud_divs - Calculate baud rate divisors
  283. * @clk: UART module input clock
  284. * @baud: Desired baud rate
  285. * @rbdiv: BDIV value (return value)
  286. * @rcd: CD value (return value)
  287. * @div8: Value for clk_sel bit in mod (return value)
  288. * Returns baud rate, requested baud when possible, or actual baud when there
  289. * was too much error, zero if no valid divisors are found.
  290. *
  291. * Formula to obtain baud rate is
  292. * baud_tx/rx rate = clk/CD * (BDIV + 1)
  293. * input_clk = (Uart User Defined Clock or Apb Clock)
  294. * depends on UCLKEN in MR Reg
  295. * clk = input_clk or input_clk/8;
  296. * depends on CLKS in MR reg
  297. * CD and BDIV depends on values in
  298. * baud rate generate register
  299. * baud rate clock divisor register
  300. */
  301. static unsigned int xuartps_calc_baud_divs(unsigned int clk, unsigned int baud,
  302. u32 *rbdiv, u32 *rcd, int *div8)
  303. {
  304. u32 cd, bdiv;
  305. unsigned int calc_baud;
  306. unsigned int bestbaud = 0;
  307. unsigned int bauderror;
  308. unsigned int besterror = ~0;
  309. if (baud < clk / ((XUARTPS_BDIV_MAX + 1) * XUARTPS_CD_MAX)) {
  310. *div8 = 1;
  311. clk /= 8;
  312. } else {
  313. *div8 = 0;
  314. }
  315. for (bdiv = XUARTPS_BDIV_MIN; bdiv <= XUARTPS_BDIV_MAX; bdiv++) {
  316. cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1));
  317. if (cd < 1 || cd > XUARTPS_CD_MAX)
  318. continue;
  319. calc_baud = clk / (cd * (bdiv + 1));
  320. if (baud > calc_baud)
  321. bauderror = baud - calc_baud;
  322. else
  323. bauderror = calc_baud - baud;
  324. if (besterror > bauderror) {
  325. *rbdiv = bdiv;
  326. *rcd = cd;
  327. bestbaud = calc_baud;
  328. besterror = bauderror;
  329. }
  330. }
  331. /* use the values when percent error is acceptable */
  332. if (((besterror * 100) / baud) < 3)
  333. bestbaud = baud;
  334. return bestbaud;
  335. }
  336. /**
  337. * xuartps_set_baud_rate - Calculate and set the baud rate
  338. * @port: Handle to the uart port structure
  339. * @baud: Baud rate to set
  340. * Returns baud rate, requested baud when possible, or actual baud when there
  341. * was too much error, zero if no valid divisors are found.
  342. */
  343. static unsigned int xuartps_set_baud_rate(struct uart_port *port,
  344. unsigned int baud)
  345. {
  346. unsigned int calc_baud;
  347. u32 cd = 0, bdiv = 0;
  348. u32 mreg;
  349. int div8;
  350. struct xuartps *xuartps = port->private_data;
  351. calc_baud = xuartps_calc_baud_divs(port->uartclk, baud, &bdiv, &cd,
  352. &div8);
  353. /* Write new divisors to hardware */
  354. mreg = xuartps_readl(XUARTPS_MR_OFFSET);
  355. if (div8)
  356. mreg |= XUARTPS_MR_CLKSEL;
  357. else
  358. mreg &= ~XUARTPS_MR_CLKSEL;
  359. xuartps_writel(mreg, XUARTPS_MR_OFFSET);
  360. xuartps_writel(cd, XUARTPS_BAUDGEN_OFFSET);
  361. xuartps_writel(bdiv, XUARTPS_BAUDDIV_OFFSET);
  362. xuartps->baud = baud;
  363. return calc_baud;
  364. }
  365. /**
  366. * xuartps_clk_notitifer_cb - Clock notifier callback
  367. * @nb: Notifier block
  368. * @event: Notify event
  369. * @data: Notifier data
  370. * Returns NOTIFY_OK on success, NOTIFY_BAD on error.
  371. */
  372. static int xuartps_clk_notifier_cb(struct notifier_block *nb,
  373. unsigned long event, void *data)
  374. {
  375. u32 ctrl_reg;
  376. struct uart_port *port;
  377. int locked = 0;
  378. struct clk_notifier_data *ndata = data;
  379. unsigned long flags = 0;
  380. struct xuartps *xuartps = to_xuartps(nb);
  381. port = xuartps->port;
  382. if (port->suspended)
  383. return NOTIFY_OK;
  384. switch (event) {
  385. case PRE_RATE_CHANGE:
  386. {
  387. u32 bdiv;
  388. u32 cd;
  389. int div8;
  390. /*
  391. * Find out if current baud-rate can be achieved with new clock
  392. * frequency.
  393. */
  394. if (!xuartps_calc_baud_divs(ndata->new_rate, xuartps->baud,
  395. &bdiv, &cd, &div8))
  396. return NOTIFY_BAD;
  397. spin_lock_irqsave(&xuartps->port->lock, flags);
  398. /* Disable the TX and RX to set baud rate */
  399. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  400. (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
  401. XUARTPS_CR_OFFSET);
  402. spin_unlock_irqrestore(&xuartps->port->lock, flags);
  403. return NOTIFY_OK;
  404. }
  405. case POST_RATE_CHANGE:
  406. /*
  407. * Set clk dividers to generate correct baud with new clock
  408. * frequency.
  409. */
  410. spin_lock_irqsave(&xuartps->port->lock, flags);
  411. locked = 1;
  412. port->uartclk = ndata->new_rate;
  413. xuartps->baud = xuartps_set_baud_rate(xuartps->port,
  414. xuartps->baud);
  415. /* fall through */
  416. case ABORT_RATE_CHANGE:
  417. if (!locked)
  418. spin_lock_irqsave(&xuartps->port->lock, flags);
  419. /* Set TX/RX Reset */
  420. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  421. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  422. XUARTPS_CR_OFFSET);
  423. while (xuartps_readl(XUARTPS_CR_OFFSET) &
  424. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST))
  425. cpu_relax();
  426. /*
  427. * Clear the RX disable and TX disable bits and then set the TX
  428. * enable bit and RX enable bit to enable the transmitter and
  429. * receiver.
  430. */
  431. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  432. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  433. xuartps_writel(
  434. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS)) |
  435. (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  436. XUARTPS_CR_OFFSET);
  437. spin_unlock_irqrestore(&xuartps->port->lock, flags);
  438. return NOTIFY_OK;
  439. default:
  440. return NOTIFY_DONE;
  441. }
  442. }
  443. /*----------------------Uart Operations---------------------------*/
  444. /**
  445. * xuartps_start_tx - Start transmitting bytes
  446. * @port: Handle to the uart port structure
  447. *
  448. **/
  449. static void xuartps_start_tx(struct uart_port *port)
  450. {
  451. unsigned int status, numbytes = port->fifosize;
  452. if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
  453. return;
  454. status = xuartps_readl(XUARTPS_CR_OFFSET);
  455. /* Set the TX enable bit and clear the TX disable bit to enable the
  456. * transmitter.
  457. */
  458. xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
  459. XUARTPS_CR_OFFSET);
  460. while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
  461. & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
  462. /* Break if no more data available in the UART buffer */
  463. if (uart_circ_empty(&port->state->xmit))
  464. break;
  465. /* Get the data from the UART circular buffer and
  466. * write it to the xuartps's TX_FIFO register.
  467. */
  468. xuartps_writel(
  469. port->state->xmit.buf[port->state->xmit.tail],
  470. XUARTPS_FIFO_OFFSET);
  471. port->icount.tx++;
  472. /* Adjust the tail of the UART buffer and wrap
  473. * the buffer if it reaches limit.
  474. */
  475. port->state->xmit.tail = (port->state->xmit.tail + 1) &
  476. (UART_XMIT_SIZE - 1);
  477. }
  478. xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_ISR_OFFSET);
  479. /* Enable the TX Empty interrupt */
  480. xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
  481. if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
  482. uart_write_wakeup(port);
  483. }
  484. /**
  485. * xuartps_stop_tx - Stop TX
  486. * @port: Handle to the uart port structure
  487. *
  488. **/
  489. static void xuartps_stop_tx(struct uart_port *port)
  490. {
  491. unsigned int regval;
  492. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  493. regval |= XUARTPS_CR_TX_DIS;
  494. /* Disable the transmitter */
  495. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  496. }
  497. /**
  498. * xuartps_stop_rx - Stop RX
  499. * @port: Handle to the uart port structure
  500. *
  501. **/
  502. static void xuartps_stop_rx(struct uart_port *port)
  503. {
  504. unsigned int regval;
  505. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  506. regval |= XUARTPS_CR_RX_DIS;
  507. /* Disable the receiver */
  508. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  509. }
  510. /**
  511. * xuartps_tx_empty - Check whether TX is empty
  512. * @port: Handle to the uart port structure
  513. *
  514. * Returns TIOCSER_TEMT on success, 0 otherwise
  515. **/
  516. static unsigned int xuartps_tx_empty(struct uart_port *port)
  517. {
  518. unsigned int status;
  519. status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
  520. return status ? TIOCSER_TEMT : 0;
  521. }
  522. /**
  523. * xuartps_break_ctl - Based on the input ctl we have to start or stop
  524. * transmitting char breaks
  525. * @port: Handle to the uart port structure
  526. * @ctl: Value based on which start or stop decision is taken
  527. *
  528. **/
  529. static void xuartps_break_ctl(struct uart_port *port, int ctl)
  530. {
  531. unsigned int status;
  532. unsigned long flags;
  533. spin_lock_irqsave(&port->lock, flags);
  534. status = xuartps_readl(XUARTPS_CR_OFFSET);
  535. if (ctl == -1)
  536. xuartps_writel(XUARTPS_CR_STARTBRK | status,
  537. XUARTPS_CR_OFFSET);
  538. else {
  539. if ((status & XUARTPS_CR_STOPBRK) == 0)
  540. xuartps_writel(XUARTPS_CR_STOPBRK | status,
  541. XUARTPS_CR_OFFSET);
  542. }
  543. spin_unlock_irqrestore(&port->lock, flags);
  544. }
  545. /**
  546. * xuartps_set_termios - termios operations, handling data length, parity,
  547. * stop bits, flow control, baud rate
  548. * @port: Handle to the uart port structure
  549. * @termios: Handle to the input termios structure
  550. * @old: Values of the previously saved termios structure
  551. *
  552. **/
  553. static void xuartps_set_termios(struct uart_port *port,
  554. struct ktermios *termios, struct ktermios *old)
  555. {
  556. unsigned int cval = 0;
  557. unsigned int baud, minbaud, maxbaud;
  558. unsigned long flags;
  559. unsigned int ctrl_reg, mode_reg;
  560. spin_lock_irqsave(&port->lock, flags);
  561. /* Empty the receive FIFO 1st before making changes */
  562. while ((xuartps_readl(XUARTPS_SR_OFFSET) &
  563. XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
  564. xuartps_readl(XUARTPS_FIFO_OFFSET);
  565. }
  566. /* Disable the TX and RX to set baud rate */
  567. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  568. (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
  569. XUARTPS_CR_OFFSET);
  570. /*
  571. * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
  572. * min and max baud should be calculated here based on port->uartclk.
  573. * this way we get a valid baud and can safely call set_baud()
  574. */
  575. minbaud = port->uartclk / ((XUARTPS_BDIV_MAX + 1) * XUARTPS_CD_MAX * 8);
  576. maxbaud = port->uartclk / (XUARTPS_BDIV_MIN + 1);
  577. baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
  578. baud = xuartps_set_baud_rate(port, baud);
  579. if (tty_termios_baud_rate(termios))
  580. tty_termios_encode_baud_rate(termios, baud, baud);
  581. /*
  582. * Update the per-port timeout.
  583. */
  584. uart_update_timeout(port, termios->c_cflag, baud);
  585. /* Set TX/RX Reset */
  586. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  587. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  588. XUARTPS_CR_OFFSET);
  589. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  590. /* Clear the RX disable and TX disable bits and then set the TX enable
  591. * bit and RX enable bit to enable the transmitter and receiver.
  592. */
  593. xuartps_writel(
  594. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  595. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  596. XUARTPS_CR_OFFSET);
  597. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  598. port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
  599. XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
  600. port->ignore_status_mask = 0;
  601. if (termios->c_iflag & INPCK)
  602. port->read_status_mask |= XUARTPS_IXR_PARITY |
  603. XUARTPS_IXR_FRAMING;
  604. if (termios->c_iflag & IGNPAR)
  605. port->ignore_status_mask |= XUARTPS_IXR_PARITY |
  606. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  607. /* ignore all characters if CREAD is not set */
  608. if ((termios->c_cflag & CREAD) == 0)
  609. port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
  610. XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
  611. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  612. mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
  613. /* Handling Data Size */
  614. switch (termios->c_cflag & CSIZE) {
  615. case CS6:
  616. cval |= XUARTPS_MR_CHARLEN_6_BIT;
  617. break;
  618. case CS7:
  619. cval |= XUARTPS_MR_CHARLEN_7_BIT;
  620. break;
  621. default:
  622. case CS8:
  623. cval |= XUARTPS_MR_CHARLEN_8_BIT;
  624. termios->c_cflag &= ~CSIZE;
  625. termios->c_cflag |= CS8;
  626. break;
  627. }
  628. /* Handling Parity and Stop Bits length */
  629. if (termios->c_cflag & CSTOPB)
  630. cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  631. else
  632. cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  633. if (termios->c_cflag & PARENB) {
  634. /* Mark or Space parity */
  635. if (termios->c_cflag & CMSPAR) {
  636. if (termios->c_cflag & PARODD)
  637. cval |= XUARTPS_MR_PARITY_MARK;
  638. else
  639. cval |= XUARTPS_MR_PARITY_SPACE;
  640. } else {
  641. if (termios->c_cflag & PARODD)
  642. cval |= XUARTPS_MR_PARITY_ODD;
  643. else
  644. cval |= XUARTPS_MR_PARITY_EVEN;
  645. }
  646. } else {
  647. cval |= XUARTPS_MR_PARITY_NONE;
  648. }
  649. cval |= mode_reg & 1;
  650. xuartps_writel(cval, XUARTPS_MR_OFFSET);
  651. spin_unlock_irqrestore(&port->lock, flags);
  652. }
  653. /**
  654. * xuartps_startup - Called when an application opens a xuartps port
  655. * @port: Handle to the uart port structure
  656. *
  657. * Returns 0 on success, negative error otherwise
  658. **/
  659. static int xuartps_startup(struct uart_port *port)
  660. {
  661. unsigned int retval = 0, status = 0;
  662. retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
  663. (void *)port);
  664. if (retval)
  665. return retval;
  666. /* Disable the TX and RX */
  667. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  668. XUARTPS_CR_OFFSET);
  669. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  670. * no break chars.
  671. */
  672. xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
  673. XUARTPS_CR_OFFSET);
  674. status = xuartps_readl(XUARTPS_CR_OFFSET);
  675. /* Clear the RX disable and TX disable bits and then set the TX enable
  676. * bit and RX enable bit to enable the transmitter and receiver.
  677. */
  678. xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  679. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
  680. XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
  681. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  682. * no parity.
  683. */
  684. xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
  685. | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
  686. XUARTPS_MR_OFFSET);
  687. /*
  688. * Set the RX FIFO Trigger level to use most of the FIFO, but it
  689. * can be tuned with a module parameter
  690. */
  691. xuartps_writel(rx_trigger_level, XUARTPS_RXWM_OFFSET);
  692. /*
  693. * Receive Timeout register is enabled but it
  694. * can be tuned with a module parameter
  695. */
  696. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  697. /* Clear out any pending interrupts before enabling them */
  698. xuartps_writel(xuartps_readl(XUARTPS_ISR_OFFSET), XUARTPS_ISR_OFFSET);
  699. /* Set the Interrupt Registers with desired interrupts */
  700. xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
  701. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
  702. XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
  703. return retval;
  704. }
  705. /**
  706. * xuartps_shutdown - Called when an application closes a xuartps port
  707. * @port: Handle to the uart port structure
  708. *
  709. **/
  710. static void xuartps_shutdown(struct uart_port *port)
  711. {
  712. int status;
  713. /* Disable interrupts */
  714. status = xuartps_readl(XUARTPS_IMR_OFFSET);
  715. xuartps_writel(status, XUARTPS_IDR_OFFSET);
  716. /* Disable the TX and RX */
  717. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  718. XUARTPS_CR_OFFSET);
  719. free_irq(port->irq, port);
  720. }
  721. /**
  722. * xuartps_type - Set UART type to xuartps port
  723. * @port: Handle to the uart port structure
  724. *
  725. * Returns string on success, NULL otherwise
  726. **/
  727. static const char *xuartps_type(struct uart_port *port)
  728. {
  729. return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
  730. }
  731. /**
  732. * xuartps_verify_port - Verify the port params
  733. * @port: Handle to the uart port structure
  734. * @ser: Handle to the structure whose members are compared
  735. *
  736. * Returns 0 if success otherwise -EINVAL
  737. **/
  738. static int xuartps_verify_port(struct uart_port *port,
  739. struct serial_struct *ser)
  740. {
  741. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  742. return -EINVAL;
  743. if (port->irq != ser->irq)
  744. return -EINVAL;
  745. if (ser->io_type != UPIO_MEM)
  746. return -EINVAL;
  747. if (port->iobase != ser->port)
  748. return -EINVAL;
  749. if (ser->hub6 != 0)
  750. return -EINVAL;
  751. return 0;
  752. }
  753. /**
  754. * xuartps_request_port - Claim the memory region attached to xuartps port,
  755. * called when the driver adds a xuartps port via
  756. * uart_add_one_port()
  757. * @port: Handle to the uart port structure
  758. *
  759. * Returns 0, -ENOMEM if request fails
  760. **/
  761. static int xuartps_request_port(struct uart_port *port)
  762. {
  763. if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
  764. XUARTPS_NAME)) {
  765. return -ENOMEM;
  766. }
  767. port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
  768. if (!port->membase) {
  769. dev_err(port->dev, "Unable to map registers\n");
  770. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  771. return -ENOMEM;
  772. }
  773. return 0;
  774. }
  775. /**
  776. * xuartps_release_port - Release the memory region attached to a xuartps
  777. * port, called when the driver removes a xuartps
  778. * port via uart_remove_one_port().
  779. * @port: Handle to the uart port structure
  780. *
  781. **/
  782. static void xuartps_release_port(struct uart_port *port)
  783. {
  784. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  785. iounmap(port->membase);
  786. port->membase = NULL;
  787. }
  788. /**
  789. * xuartps_config_port - Configure xuartps, called when the driver adds a
  790. * xuartps port
  791. * @port: Handle to the uart port structure
  792. * @flags: If any
  793. *
  794. **/
  795. static void xuartps_config_port(struct uart_port *port, int flags)
  796. {
  797. if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
  798. port->type = PORT_XUARTPS;
  799. }
  800. /**
  801. * xuartps_get_mctrl - Get the modem control state
  802. *
  803. * @port: Handle to the uart port structure
  804. *
  805. * Returns the modem control state
  806. *
  807. **/
  808. static unsigned int xuartps_get_mctrl(struct uart_port *port)
  809. {
  810. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  811. }
  812. static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
  813. {
  814. /* N/A */
  815. }
  816. static void xuartps_enable_ms(struct uart_port *port)
  817. {
  818. /* N/A */
  819. }
  820. #ifdef CONFIG_CONSOLE_POLL
  821. static int xuartps_poll_get_char(struct uart_port *port)
  822. {
  823. u32 imr;
  824. int c;
  825. /* Disable all interrupts */
  826. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  827. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  828. /* Check if FIFO is empty */
  829. if (xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_RXEMPTY)
  830. c = NO_POLL_CHAR;
  831. else /* Read a character */
  832. c = (unsigned char) xuartps_readl(XUARTPS_FIFO_OFFSET);
  833. /* Enable interrupts */
  834. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  835. return c;
  836. }
  837. static void xuartps_poll_put_char(struct uart_port *port, unsigned char c)
  838. {
  839. u32 imr;
  840. /* Disable all interrupts */
  841. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  842. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  843. /* Wait until FIFO is empty */
  844. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY))
  845. cpu_relax();
  846. /* Write a character */
  847. xuartps_writel(c, XUARTPS_FIFO_OFFSET);
  848. /* Wait until FIFO is empty */
  849. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY))
  850. cpu_relax();
  851. /* Enable interrupts */
  852. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  853. return;
  854. }
  855. #endif
  856. /** The UART operations structure
  857. */
  858. static struct uart_ops xuartps_ops = {
  859. .set_mctrl = xuartps_set_mctrl,
  860. .get_mctrl = xuartps_get_mctrl,
  861. .enable_ms = xuartps_enable_ms,
  862. .start_tx = xuartps_start_tx, /* Start transmitting */
  863. .stop_tx = xuartps_stop_tx, /* Stop transmission */
  864. .stop_rx = xuartps_stop_rx, /* Stop reception */
  865. .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
  866. .break_ctl = xuartps_break_ctl, /* Start/stop
  867. * transmitting break
  868. */
  869. .set_termios = xuartps_set_termios, /* Set termios */
  870. .startup = xuartps_startup, /* App opens xuartps */
  871. .shutdown = xuartps_shutdown, /* App closes xuartps */
  872. .type = xuartps_type, /* Set UART type */
  873. .verify_port = xuartps_verify_port, /* Verification of port
  874. * params
  875. */
  876. .request_port = xuartps_request_port, /* Claim resources
  877. * associated with a
  878. * xuartps port
  879. */
  880. .release_port = xuartps_release_port, /* Release resources
  881. * associated with a
  882. * xuartps port
  883. */
  884. .config_port = xuartps_config_port, /* Configure when driver
  885. * adds a xuartps port
  886. */
  887. #ifdef CONFIG_CONSOLE_POLL
  888. .poll_get_char = xuartps_poll_get_char,
  889. .poll_put_char = xuartps_poll_put_char,
  890. #endif
  891. };
  892. static struct uart_port xuartps_port[2];
  893. /**
  894. * xuartps_get_port - Configure the port from the platform device resource
  895. * info
  896. *
  897. * Returns a pointer to a uart_port or NULL for failure
  898. **/
  899. static struct uart_port *xuartps_get_port(void)
  900. {
  901. struct uart_port *port;
  902. int id;
  903. /* Find the next unused port */
  904. for (id = 0; id < XUARTPS_NR_PORTS; id++)
  905. if (xuartps_port[id].mapbase == 0)
  906. break;
  907. if (id >= XUARTPS_NR_PORTS)
  908. return NULL;
  909. port = &xuartps_port[id];
  910. /* At this point, we've got an empty uart_port struct, initialize it */
  911. spin_lock_init(&port->lock);
  912. port->membase = NULL;
  913. port->iobase = 1; /* mark port in use */
  914. port->irq = 0;
  915. port->type = PORT_UNKNOWN;
  916. port->iotype = UPIO_MEM32;
  917. port->flags = UPF_BOOT_AUTOCONF;
  918. port->ops = &xuartps_ops;
  919. port->fifosize = XUARTPS_FIFO_SIZE;
  920. port->line = id;
  921. port->dev = NULL;
  922. return port;
  923. }
  924. /*-----------------------Console driver operations--------------------------*/
  925. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  926. /**
  927. * xuartps_console_wait_tx - Wait for the TX to be full
  928. * @port: Handle to the uart port structure
  929. *
  930. **/
  931. static void xuartps_console_wait_tx(struct uart_port *port)
  932. {
  933. while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
  934. != XUARTPS_SR_TXEMPTY)
  935. barrier();
  936. }
  937. /**
  938. * xuartps_console_putchar - write the character to the FIFO buffer
  939. * @port: Handle to the uart port structure
  940. * @ch: Character to be written
  941. *
  942. **/
  943. static void xuartps_console_putchar(struct uart_port *port, int ch)
  944. {
  945. xuartps_console_wait_tx(port);
  946. xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
  947. }
  948. /**
  949. * xuartps_console_write - perform write operation
  950. * @port: Handle to the uart port structure
  951. * @s: Pointer to character array
  952. * @count: No of characters
  953. **/
  954. static void xuartps_console_write(struct console *co, const char *s,
  955. unsigned int count)
  956. {
  957. struct uart_port *port = &xuartps_port[co->index];
  958. unsigned long flags;
  959. unsigned int imr, ctrl;
  960. int locked = 1;
  961. if (oops_in_progress)
  962. locked = spin_trylock_irqsave(&port->lock, flags);
  963. else
  964. spin_lock_irqsave(&port->lock, flags);
  965. /* save and disable interrupt */
  966. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  967. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  968. /*
  969. * Make sure that the tx part is enabled. Set the TX enable bit and
  970. * clear the TX disable bit to enable the transmitter.
  971. */
  972. ctrl = xuartps_readl(XUARTPS_CR_OFFSET);
  973. xuartps_writel((ctrl & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
  974. XUARTPS_CR_OFFSET);
  975. uart_console_write(port, s, count, xuartps_console_putchar);
  976. xuartps_console_wait_tx(port);
  977. xuartps_writel(ctrl, XUARTPS_CR_OFFSET);
  978. /* restore interrupt state, it seems like there may be a h/w bug
  979. * in that the interrupt enable register should not need to be
  980. * written based on the data sheet
  981. */
  982. xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
  983. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  984. if (locked)
  985. spin_unlock_irqrestore(&port->lock, flags);
  986. }
  987. /**
  988. * xuartps_console_setup - Initialize the uart to default config
  989. * @co: Console handle
  990. * @options: Initial settings of uart
  991. *
  992. * Returns 0, -ENODEV if no device
  993. **/
  994. static int __init xuartps_console_setup(struct console *co, char *options)
  995. {
  996. struct uart_port *port = &xuartps_port[co->index];
  997. int baud = 9600;
  998. int bits = 8;
  999. int parity = 'n';
  1000. int flow = 'n';
  1001. if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
  1002. return -EINVAL;
  1003. if (!port->mapbase) {
  1004. pr_debug("console on ttyPS%i not present\n", co->index);
  1005. return -ENODEV;
  1006. }
  1007. if (options)
  1008. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1009. return uart_set_options(port, co, baud, parity, bits, flow);
  1010. }
  1011. static struct uart_driver xuartps_uart_driver;
  1012. static struct console xuartps_console = {
  1013. .name = XUARTPS_TTY_NAME,
  1014. .write = xuartps_console_write,
  1015. .device = uart_console_device,
  1016. .setup = xuartps_console_setup,
  1017. .flags = CON_PRINTBUFFER,
  1018. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  1019. .data = &xuartps_uart_driver,
  1020. };
  1021. /**
  1022. * xuartps_console_init - Initialization call
  1023. *
  1024. * Returns 0 on success, negative error otherwise
  1025. **/
  1026. static int __init xuartps_console_init(void)
  1027. {
  1028. register_console(&xuartps_console);
  1029. return 0;
  1030. }
  1031. console_initcall(xuartps_console_init);
  1032. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1033. #ifdef CONFIG_PM_SLEEP
  1034. /**
  1035. * xuartps_suspend - suspend event
  1036. * @device: Pointer to the device structure
  1037. *
  1038. * Returns 0
  1039. */
  1040. static int xuartps_suspend(struct device *device)
  1041. {
  1042. struct uart_port *port = dev_get_drvdata(device);
  1043. struct tty_struct *tty;
  1044. struct device *tty_dev;
  1045. int may_wake = 0;
  1046. /* Get the tty which could be NULL so don't assume it's valid */
  1047. tty = tty_port_tty_get(&port->state->port);
  1048. if (tty) {
  1049. tty_dev = tty->dev;
  1050. may_wake = device_may_wakeup(tty_dev);
  1051. tty_kref_put(tty);
  1052. }
  1053. /*
  1054. * Call the API provided in serial_core.c file which handles
  1055. * the suspend.
  1056. */
  1057. uart_suspend_port(&xuartps_uart_driver, port);
  1058. if (console_suspend_enabled && !may_wake) {
  1059. struct xuartps *xuartps = port->private_data;
  1060. clk_disable(xuartps->refclk);
  1061. clk_disable(xuartps->aperclk);
  1062. } else {
  1063. unsigned long flags = 0;
  1064. spin_lock_irqsave(&port->lock, flags);
  1065. /* Empty the receive FIFO 1st before making changes */
  1066. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_RXEMPTY))
  1067. xuartps_readl(XUARTPS_FIFO_OFFSET);
  1068. /* set RX trigger level to 1 */
  1069. xuartps_writel(1, XUARTPS_RXWM_OFFSET);
  1070. /* disable RX timeout interrups */
  1071. xuartps_writel(XUARTPS_IXR_TOUT, XUARTPS_IDR_OFFSET);
  1072. spin_unlock_irqrestore(&port->lock, flags);
  1073. }
  1074. return 0;
  1075. }
  1076. /**
  1077. * xuartps_resume - Resume after a previous suspend
  1078. * @device: Pointer to the device structure
  1079. *
  1080. * Returns 0
  1081. */
  1082. static int xuartps_resume(struct device *device)
  1083. {
  1084. struct uart_port *port = dev_get_drvdata(device);
  1085. unsigned long flags = 0;
  1086. u32 ctrl_reg;
  1087. struct tty_struct *tty;
  1088. struct device *tty_dev;
  1089. int may_wake = 0;
  1090. /* Get the tty which could be NULL so don't assume it's valid */
  1091. tty = tty_port_tty_get(&port->state->port);
  1092. if (tty) {
  1093. tty_dev = tty->dev;
  1094. may_wake = device_may_wakeup(tty_dev);
  1095. tty_kref_put(tty);
  1096. }
  1097. if (console_suspend_enabled && !may_wake) {
  1098. struct xuartps *xuartps = port->private_data;
  1099. clk_enable(xuartps->aperclk);
  1100. clk_enable(xuartps->refclk);
  1101. spin_lock_irqsave(&port->lock, flags);
  1102. /* Set TX/RX Reset */
  1103. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  1104. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  1105. XUARTPS_CR_OFFSET);
  1106. while (xuartps_readl(XUARTPS_CR_OFFSET) &
  1107. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST))
  1108. cpu_relax();
  1109. /* restore rx timeout value */
  1110. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  1111. /* Enable Tx/Rx */
  1112. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  1113. xuartps_writel(
  1114. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS)) |
  1115. (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  1116. XUARTPS_CR_OFFSET);
  1117. spin_unlock_irqrestore(&port->lock, flags);
  1118. } else {
  1119. spin_lock_irqsave(&port->lock, flags);
  1120. /* restore original rx trigger level */
  1121. xuartps_writel(rx_trigger_level, XUARTPS_RXWM_OFFSET);
  1122. /* enable RX timeout interrupt */
  1123. xuartps_writel(XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
  1124. spin_unlock_irqrestore(&port->lock, flags);
  1125. }
  1126. return uart_resume_port(&xuartps_uart_driver, port);
  1127. }
  1128. #endif /* ! CONFIG_PM_SLEEP */
  1129. static SIMPLE_DEV_PM_OPS(xuartps_dev_pm_ops, xuartps_suspend, xuartps_resume);
  1130. /** Structure Definitions
  1131. */
  1132. static struct uart_driver xuartps_uart_driver = {
  1133. .owner = THIS_MODULE, /* Owner */
  1134. .driver_name = XUARTPS_NAME, /* Driver name */
  1135. .dev_name = XUARTPS_TTY_NAME, /* Node name */
  1136. .major = XUARTPS_MAJOR, /* Major number */
  1137. .minor = XUARTPS_MINOR, /* Minor number */
  1138. .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
  1139. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1140. .cons = &xuartps_console, /* Console */
  1141. #endif
  1142. };
  1143. /* ---------------------------------------------------------------------
  1144. * Platform bus binding
  1145. */
  1146. /**
  1147. * xuartps_probe - Platform driver probe
  1148. * @pdev: Pointer to the platform device structure
  1149. *
  1150. * Returns 0 on success, negative error otherwise
  1151. **/
  1152. static int xuartps_probe(struct platform_device *pdev)
  1153. {
  1154. int rc;
  1155. struct uart_port *port;
  1156. struct resource *res, *res2;
  1157. struct xuartps *xuartps_data;
  1158. xuartps_data = devm_kzalloc(&pdev->dev, sizeof(*xuartps_data),
  1159. GFP_KERNEL);
  1160. if (!xuartps_data)
  1161. return -ENOMEM;
  1162. xuartps_data->aperclk = devm_clk_get(&pdev->dev, "aper_clk");
  1163. if (IS_ERR(xuartps_data->aperclk)) {
  1164. dev_err(&pdev->dev, "aper_clk clock not found.\n");
  1165. return PTR_ERR(xuartps_data->aperclk);
  1166. }
  1167. xuartps_data->refclk = devm_clk_get(&pdev->dev, "ref_clk");
  1168. if (IS_ERR(xuartps_data->refclk)) {
  1169. dev_err(&pdev->dev, "ref_clk clock not found.\n");
  1170. return PTR_ERR(xuartps_data->refclk);
  1171. }
  1172. rc = clk_prepare_enable(xuartps_data->aperclk);
  1173. if (rc) {
  1174. dev_err(&pdev->dev, "Unable to enable APER clock.\n");
  1175. return rc;
  1176. }
  1177. rc = clk_prepare_enable(xuartps_data->refclk);
  1178. if (rc) {
  1179. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1180. goto err_out_clk_dis_aper;
  1181. }
  1182. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1183. if (!res) {
  1184. rc = -ENODEV;
  1185. goto err_out_clk_disable;
  1186. }
  1187. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1188. if (!res2) {
  1189. rc = -ENODEV;
  1190. goto err_out_clk_disable;
  1191. }
  1192. xuartps_data->clk_rate_change_nb.notifier_call =
  1193. xuartps_clk_notifier_cb;
  1194. if (clk_notifier_register(xuartps_data->refclk,
  1195. &xuartps_data->clk_rate_change_nb))
  1196. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1197. /* Initialize the port structure */
  1198. port = xuartps_get_port();
  1199. if (!port) {
  1200. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1201. rc = -ENODEV;
  1202. goto err_out_notif_unreg;
  1203. } else {
  1204. /* Register the port.
  1205. * This function also registers this device with the tty layer
  1206. * and triggers invocation of the config_port() entry point.
  1207. */
  1208. port->mapbase = res->start;
  1209. port->irq = res2->start;
  1210. port->dev = &pdev->dev;
  1211. port->uartclk = clk_get_rate(xuartps_data->refclk);
  1212. port->private_data = xuartps_data;
  1213. xuartps_data->port = port;
  1214. platform_set_drvdata(pdev, port);
  1215. rc = uart_add_one_port(&xuartps_uart_driver, port);
  1216. if (rc) {
  1217. dev_err(&pdev->dev,
  1218. "uart_add_one_port() failed; err=%i\n", rc);
  1219. goto err_out_notif_unreg;
  1220. }
  1221. return 0;
  1222. }
  1223. err_out_notif_unreg:
  1224. clk_notifier_unregister(xuartps_data->refclk,
  1225. &xuartps_data->clk_rate_change_nb);
  1226. err_out_clk_disable:
  1227. clk_disable_unprepare(xuartps_data->refclk);
  1228. err_out_clk_dis_aper:
  1229. clk_disable_unprepare(xuartps_data->aperclk);
  1230. return rc;
  1231. }
  1232. /**
  1233. * xuartps_remove - called when the platform driver is unregistered
  1234. * @pdev: Pointer to the platform device structure
  1235. *
  1236. * Returns 0 on success, negative error otherwise
  1237. **/
  1238. static int xuartps_remove(struct platform_device *pdev)
  1239. {
  1240. struct uart_port *port = platform_get_drvdata(pdev);
  1241. struct xuartps *xuartps_data = port->private_data;
  1242. int rc;
  1243. /* Remove the xuartps port from the serial core */
  1244. clk_notifier_unregister(xuartps_data->refclk,
  1245. &xuartps_data->clk_rate_change_nb);
  1246. rc = uart_remove_one_port(&xuartps_uart_driver, port);
  1247. port->mapbase = 0;
  1248. clk_disable_unprepare(xuartps_data->refclk);
  1249. clk_disable_unprepare(xuartps_data->aperclk);
  1250. return rc;
  1251. }
  1252. /* Match table for of_platform binding */
  1253. static struct of_device_id xuartps_of_match[] = {
  1254. { .compatible = "xlnx,xuartps", },
  1255. {}
  1256. };
  1257. MODULE_DEVICE_TABLE(of, xuartps_of_match);
  1258. static struct platform_driver xuartps_platform_driver = {
  1259. .probe = xuartps_probe, /* Probe method */
  1260. .remove = xuartps_remove, /* Detach method */
  1261. .driver = {
  1262. .owner = THIS_MODULE,
  1263. .name = XUARTPS_NAME, /* Driver name */
  1264. .of_match_table = xuartps_of_match,
  1265. .pm = &xuartps_dev_pm_ops,
  1266. },
  1267. };
  1268. /* ---------------------------------------------------------------------
  1269. * Module Init and Exit
  1270. */
  1271. /**
  1272. * xuartps_init - Initial driver registration call
  1273. *
  1274. * Returns whether the registration was successful or not
  1275. **/
  1276. static int __init xuartps_init(void)
  1277. {
  1278. int retval = 0;
  1279. /* Register the xuartps driver with the serial core */
  1280. retval = uart_register_driver(&xuartps_uart_driver);
  1281. if (retval)
  1282. return retval;
  1283. /* Register the platform driver */
  1284. retval = platform_driver_register(&xuartps_platform_driver);
  1285. if (retval)
  1286. uart_unregister_driver(&xuartps_uart_driver);
  1287. return retval;
  1288. }
  1289. /**
  1290. * xuartps_exit - Driver unregistration call
  1291. **/
  1292. static void __exit xuartps_exit(void)
  1293. {
  1294. /* The order of unregistration is important. Unregister the
  1295. * UART driver before the platform driver crashes the system.
  1296. */
  1297. /* Unregister the platform driver */
  1298. platform_driver_unregister(&xuartps_platform_driver);
  1299. /* Unregister the xuartps driver */
  1300. uart_unregister_driver(&xuartps_uart_driver);
  1301. }
  1302. module_init(xuartps_init);
  1303. module_exit(xuartps_exit);
  1304. MODULE_DESCRIPTION("Driver for PS UART");
  1305. MODULE_AUTHOR("Xilinx Inc.");
  1306. MODULE_LICENSE("GPL");