xilinx_uartps.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. #ifdef CONFIG_COMMON_CLK
  366. /**
  367. * xuartps_clk_notitifer_cb - Clock notifier callback
  368. * @nb: Notifier block
  369. * @event: Notify event
  370. * @data: Notifier data
  371. * Returns NOTIFY_OK on success, NOTIFY_BAD on error.
  372. */
  373. static int xuartps_clk_notifier_cb(struct notifier_block *nb,
  374. unsigned long event, void *data)
  375. {
  376. u32 ctrl_reg;
  377. struct uart_port *port;
  378. int locked = 0;
  379. struct clk_notifier_data *ndata = data;
  380. unsigned long flags = 0;
  381. struct xuartps *xuartps = to_xuartps(nb);
  382. port = xuartps->port;
  383. if (port->suspended)
  384. return NOTIFY_OK;
  385. switch (event) {
  386. case PRE_RATE_CHANGE:
  387. {
  388. u32 bdiv;
  389. u32 cd;
  390. int div8;
  391. /*
  392. * Find out if current baud-rate can be achieved with new clock
  393. * frequency.
  394. */
  395. if (!xuartps_calc_baud_divs(ndata->new_rate, xuartps->baud,
  396. &bdiv, &cd, &div8))
  397. return NOTIFY_BAD;
  398. spin_lock_irqsave(&xuartps->port->lock, flags);
  399. /* Disable the TX and RX to set baud rate */
  400. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  401. (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
  402. XUARTPS_CR_OFFSET);
  403. spin_unlock_irqrestore(&xuartps->port->lock, flags);
  404. return NOTIFY_OK;
  405. }
  406. case POST_RATE_CHANGE:
  407. /*
  408. * Set clk dividers to generate correct baud with new clock
  409. * frequency.
  410. */
  411. spin_lock_irqsave(&xuartps->port->lock, flags);
  412. locked = 1;
  413. port->uartclk = ndata->new_rate;
  414. xuartps->baud = xuartps_set_baud_rate(xuartps->port,
  415. xuartps->baud);
  416. /* fall through */
  417. case ABORT_RATE_CHANGE:
  418. if (!locked)
  419. spin_lock_irqsave(&xuartps->port->lock, flags);
  420. /* Set TX/RX Reset */
  421. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  422. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  423. XUARTPS_CR_OFFSET);
  424. while (xuartps_readl(XUARTPS_CR_OFFSET) &
  425. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST))
  426. cpu_relax();
  427. /*
  428. * Clear the RX disable and TX disable bits and then set the TX
  429. * enable bit and RX enable bit to enable the transmitter and
  430. * receiver.
  431. */
  432. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  433. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  434. xuartps_writel(
  435. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS)) |
  436. (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  437. XUARTPS_CR_OFFSET);
  438. spin_unlock_irqrestore(&xuartps->port->lock, flags);
  439. return NOTIFY_OK;
  440. default:
  441. return NOTIFY_DONE;
  442. }
  443. }
  444. #endif
  445. /*----------------------Uart Operations---------------------------*/
  446. /**
  447. * xuartps_start_tx - Start transmitting bytes
  448. * @port: Handle to the uart port structure
  449. *
  450. **/
  451. static void xuartps_start_tx(struct uart_port *port)
  452. {
  453. unsigned int status, numbytes = port->fifosize;
  454. if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
  455. return;
  456. status = xuartps_readl(XUARTPS_CR_OFFSET);
  457. /* Set the TX enable bit and clear the TX disable bit to enable the
  458. * transmitter.
  459. */
  460. xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
  461. XUARTPS_CR_OFFSET);
  462. while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
  463. & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
  464. /* Break if no more data available in the UART buffer */
  465. if (uart_circ_empty(&port->state->xmit))
  466. break;
  467. /* Get the data from the UART circular buffer and
  468. * write it to the xuartps's TX_FIFO register.
  469. */
  470. xuartps_writel(
  471. port->state->xmit.buf[port->state->xmit.tail],
  472. XUARTPS_FIFO_OFFSET);
  473. port->icount.tx++;
  474. /* Adjust the tail of the UART buffer and wrap
  475. * the buffer if it reaches limit.
  476. */
  477. port->state->xmit.tail = (port->state->xmit.tail + 1) &
  478. (UART_XMIT_SIZE - 1);
  479. }
  480. xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_ISR_OFFSET);
  481. /* Enable the TX Empty interrupt */
  482. xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
  483. if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
  484. uart_write_wakeup(port);
  485. }
  486. /**
  487. * xuartps_stop_tx - Stop TX
  488. * @port: Handle to the uart port structure
  489. *
  490. **/
  491. static void xuartps_stop_tx(struct uart_port *port)
  492. {
  493. unsigned int regval;
  494. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  495. regval |= XUARTPS_CR_TX_DIS;
  496. /* Disable the transmitter */
  497. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  498. }
  499. /**
  500. * xuartps_stop_rx - Stop RX
  501. * @port: Handle to the uart port structure
  502. *
  503. **/
  504. static void xuartps_stop_rx(struct uart_port *port)
  505. {
  506. unsigned int regval;
  507. regval = xuartps_readl(XUARTPS_CR_OFFSET);
  508. regval |= XUARTPS_CR_RX_DIS;
  509. /* Disable the receiver */
  510. xuartps_writel(regval, XUARTPS_CR_OFFSET);
  511. }
  512. /**
  513. * xuartps_tx_empty - Check whether TX is empty
  514. * @port: Handle to the uart port structure
  515. *
  516. * Returns TIOCSER_TEMT on success, 0 otherwise
  517. **/
  518. static unsigned int xuartps_tx_empty(struct uart_port *port)
  519. {
  520. unsigned int status;
  521. status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
  522. return status ? TIOCSER_TEMT : 0;
  523. }
  524. /**
  525. * xuartps_break_ctl - Based on the input ctl we have to start or stop
  526. * transmitting char breaks
  527. * @port: Handle to the uart port structure
  528. * @ctl: Value based on which start or stop decision is taken
  529. *
  530. **/
  531. static void xuartps_break_ctl(struct uart_port *port, int ctl)
  532. {
  533. unsigned int status;
  534. unsigned long flags;
  535. spin_lock_irqsave(&port->lock, flags);
  536. status = xuartps_readl(XUARTPS_CR_OFFSET);
  537. if (ctl == -1)
  538. xuartps_writel(XUARTPS_CR_STARTBRK | status,
  539. XUARTPS_CR_OFFSET);
  540. else {
  541. if ((status & XUARTPS_CR_STOPBRK) == 0)
  542. xuartps_writel(XUARTPS_CR_STOPBRK | status,
  543. XUARTPS_CR_OFFSET);
  544. }
  545. spin_unlock_irqrestore(&port->lock, flags);
  546. }
  547. /**
  548. * xuartps_set_termios - termios operations, handling data length, parity,
  549. * stop bits, flow control, baud rate
  550. * @port: Handle to the uart port structure
  551. * @termios: Handle to the input termios structure
  552. * @old: Values of the previously saved termios structure
  553. *
  554. **/
  555. static void xuartps_set_termios(struct uart_port *port,
  556. struct ktermios *termios, struct ktermios *old)
  557. {
  558. unsigned int cval = 0;
  559. unsigned int baud, minbaud, maxbaud;
  560. unsigned long flags;
  561. unsigned int ctrl_reg, mode_reg;
  562. spin_lock_irqsave(&port->lock, flags);
  563. /* Empty the receive FIFO 1st before making changes */
  564. while ((xuartps_readl(XUARTPS_SR_OFFSET) &
  565. XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
  566. xuartps_readl(XUARTPS_FIFO_OFFSET);
  567. }
  568. /* Disable the TX and RX to set baud rate */
  569. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  570. (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
  571. XUARTPS_CR_OFFSET);
  572. /*
  573. * Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk
  574. * min and max baud should be calculated here based on port->uartclk.
  575. * this way we get a valid baud and can safely call set_baud()
  576. */
  577. minbaud = port->uartclk / ((XUARTPS_BDIV_MAX + 1) * XUARTPS_CD_MAX * 8);
  578. maxbaud = port->uartclk / (XUARTPS_BDIV_MIN + 1);
  579. baud = uart_get_baud_rate(port, termios, old, minbaud, maxbaud);
  580. baud = xuartps_set_baud_rate(port, baud);
  581. if (tty_termios_baud_rate(termios))
  582. tty_termios_encode_baud_rate(termios, baud, baud);
  583. /*
  584. * Update the per-port timeout.
  585. */
  586. uart_update_timeout(port, termios->c_cflag, baud);
  587. /* Set TX/RX Reset */
  588. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  589. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  590. XUARTPS_CR_OFFSET);
  591. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  592. /* Clear the RX disable and TX disable bits and then set the TX enable
  593. * bit and RX enable bit to enable the transmitter and receiver.
  594. */
  595. xuartps_writel(
  596. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  597. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  598. XUARTPS_CR_OFFSET);
  599. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  600. port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
  601. XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
  602. port->ignore_status_mask = 0;
  603. if (termios->c_iflag & INPCK)
  604. port->read_status_mask |= XUARTPS_IXR_PARITY |
  605. XUARTPS_IXR_FRAMING;
  606. if (termios->c_iflag & IGNPAR)
  607. port->ignore_status_mask |= XUARTPS_IXR_PARITY |
  608. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  609. /* ignore all characters if CREAD is not set */
  610. if ((termios->c_cflag & CREAD) == 0)
  611. port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
  612. XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
  613. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
  614. mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
  615. /* Handling Data Size */
  616. switch (termios->c_cflag & CSIZE) {
  617. case CS6:
  618. cval |= XUARTPS_MR_CHARLEN_6_BIT;
  619. break;
  620. case CS7:
  621. cval |= XUARTPS_MR_CHARLEN_7_BIT;
  622. break;
  623. default:
  624. case CS8:
  625. cval |= XUARTPS_MR_CHARLEN_8_BIT;
  626. termios->c_cflag &= ~CSIZE;
  627. termios->c_cflag |= CS8;
  628. break;
  629. }
  630. /* Handling Parity and Stop Bits length */
  631. if (termios->c_cflag & CSTOPB)
  632. cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
  633. else
  634. cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
  635. if (termios->c_cflag & PARENB) {
  636. /* Mark or Space parity */
  637. if (termios->c_cflag & CMSPAR) {
  638. if (termios->c_cflag & PARODD)
  639. cval |= XUARTPS_MR_PARITY_MARK;
  640. else
  641. cval |= XUARTPS_MR_PARITY_SPACE;
  642. } else {
  643. if (termios->c_cflag & PARODD)
  644. cval |= XUARTPS_MR_PARITY_ODD;
  645. else
  646. cval |= XUARTPS_MR_PARITY_EVEN;
  647. }
  648. } else {
  649. cval |= XUARTPS_MR_PARITY_NONE;
  650. }
  651. cval |= mode_reg & 1;
  652. xuartps_writel(cval, XUARTPS_MR_OFFSET);
  653. spin_unlock_irqrestore(&port->lock, flags);
  654. }
  655. /**
  656. * xuartps_startup - Called when an application opens a xuartps port
  657. * @port: Handle to the uart port structure
  658. *
  659. * Returns 0 on success, negative error otherwise
  660. **/
  661. static int xuartps_startup(struct uart_port *port)
  662. {
  663. unsigned int retval = 0, status = 0;
  664. retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
  665. (void *)port);
  666. if (retval)
  667. return retval;
  668. /* Disable the TX and RX */
  669. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  670. XUARTPS_CR_OFFSET);
  671. /* Set the Control Register with TX/RX Enable, TX/RX Reset,
  672. * no break chars.
  673. */
  674. xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
  675. XUARTPS_CR_OFFSET);
  676. status = xuartps_readl(XUARTPS_CR_OFFSET);
  677. /* Clear the RX disable and TX disable bits and then set the TX enable
  678. * bit and RX enable bit to enable the transmitter and receiver.
  679. */
  680. xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
  681. | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
  682. XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
  683. /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
  684. * no parity.
  685. */
  686. xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
  687. | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
  688. XUARTPS_MR_OFFSET);
  689. /*
  690. * Set the RX FIFO Trigger level to use most of the FIFO, but it
  691. * can be tuned with a module parameter
  692. */
  693. xuartps_writel(rx_trigger_level, XUARTPS_RXWM_OFFSET);
  694. /*
  695. * Receive Timeout register is enabled but it
  696. * can be tuned with a module parameter
  697. */
  698. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  699. /* Clear out any pending interrupts before enabling them */
  700. xuartps_writel(xuartps_readl(XUARTPS_ISR_OFFSET), XUARTPS_ISR_OFFSET);
  701. /* Set the Interrupt Registers with desired interrupts */
  702. xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
  703. XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
  704. XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
  705. return retval;
  706. }
  707. /**
  708. * xuartps_shutdown - Called when an application closes a xuartps port
  709. * @port: Handle to the uart port structure
  710. *
  711. **/
  712. static void xuartps_shutdown(struct uart_port *port)
  713. {
  714. int status;
  715. /* Disable interrupts */
  716. status = xuartps_readl(XUARTPS_IMR_OFFSET);
  717. xuartps_writel(status, XUARTPS_IDR_OFFSET);
  718. /* Disable the TX and RX */
  719. xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
  720. XUARTPS_CR_OFFSET);
  721. free_irq(port->irq, port);
  722. }
  723. /**
  724. * xuartps_type - Set UART type to xuartps port
  725. * @port: Handle to the uart port structure
  726. *
  727. * Returns string on success, NULL otherwise
  728. **/
  729. static const char *xuartps_type(struct uart_port *port)
  730. {
  731. return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
  732. }
  733. /**
  734. * xuartps_verify_port - Verify the port params
  735. * @port: Handle to the uart port structure
  736. * @ser: Handle to the structure whose members are compared
  737. *
  738. * Returns 0 if success otherwise -EINVAL
  739. **/
  740. static int xuartps_verify_port(struct uart_port *port,
  741. struct serial_struct *ser)
  742. {
  743. if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
  744. return -EINVAL;
  745. if (port->irq != ser->irq)
  746. return -EINVAL;
  747. if (ser->io_type != UPIO_MEM)
  748. return -EINVAL;
  749. if (port->iobase != ser->port)
  750. return -EINVAL;
  751. if (ser->hub6 != 0)
  752. return -EINVAL;
  753. return 0;
  754. }
  755. /**
  756. * xuartps_request_port - Claim the memory region attached to xuartps port,
  757. * called when the driver adds a xuartps port via
  758. * uart_add_one_port()
  759. * @port: Handle to the uart port structure
  760. *
  761. * Returns 0, -ENOMEM if request fails
  762. **/
  763. static int xuartps_request_port(struct uart_port *port)
  764. {
  765. if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
  766. XUARTPS_NAME)) {
  767. return -ENOMEM;
  768. }
  769. port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
  770. if (!port->membase) {
  771. dev_err(port->dev, "Unable to map registers\n");
  772. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  773. return -ENOMEM;
  774. }
  775. return 0;
  776. }
  777. /**
  778. * xuartps_release_port - Release the memory region attached to a xuartps
  779. * port, called when the driver removes a xuartps
  780. * port via uart_remove_one_port().
  781. * @port: Handle to the uart port structure
  782. *
  783. **/
  784. static void xuartps_release_port(struct uart_port *port)
  785. {
  786. release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
  787. iounmap(port->membase);
  788. port->membase = NULL;
  789. }
  790. /**
  791. * xuartps_config_port - Configure xuartps, called when the driver adds a
  792. * xuartps port
  793. * @port: Handle to the uart port structure
  794. * @flags: If any
  795. *
  796. **/
  797. static void xuartps_config_port(struct uart_port *port, int flags)
  798. {
  799. if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
  800. port->type = PORT_XUARTPS;
  801. }
  802. /**
  803. * xuartps_get_mctrl - Get the modem control state
  804. *
  805. * @port: Handle to the uart port structure
  806. *
  807. * Returns the modem control state
  808. *
  809. **/
  810. static unsigned int xuartps_get_mctrl(struct uart_port *port)
  811. {
  812. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  813. }
  814. static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
  815. {
  816. /* N/A */
  817. }
  818. static void xuartps_enable_ms(struct uart_port *port)
  819. {
  820. /* N/A */
  821. }
  822. #ifdef CONFIG_CONSOLE_POLL
  823. static int xuartps_poll_get_char(struct uart_port *port)
  824. {
  825. u32 imr;
  826. int c;
  827. /* Disable all interrupts */
  828. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  829. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  830. /* Check if FIFO is empty */
  831. if (xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_RXEMPTY)
  832. c = NO_POLL_CHAR;
  833. else /* Read a character */
  834. c = (unsigned char) xuartps_readl(XUARTPS_FIFO_OFFSET);
  835. /* Enable interrupts */
  836. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  837. return c;
  838. }
  839. static void xuartps_poll_put_char(struct uart_port *port, unsigned char c)
  840. {
  841. u32 imr;
  842. /* Disable all interrupts */
  843. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  844. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  845. /* Wait until FIFO is empty */
  846. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY))
  847. cpu_relax();
  848. /* Write a character */
  849. xuartps_writel(c, XUARTPS_FIFO_OFFSET);
  850. /* Wait until FIFO is empty */
  851. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY))
  852. cpu_relax();
  853. /* Enable interrupts */
  854. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  855. return;
  856. }
  857. #endif
  858. /** The UART operations structure
  859. */
  860. static struct uart_ops xuartps_ops = {
  861. .set_mctrl = xuartps_set_mctrl,
  862. .get_mctrl = xuartps_get_mctrl,
  863. .enable_ms = xuartps_enable_ms,
  864. .start_tx = xuartps_start_tx, /* Start transmitting */
  865. .stop_tx = xuartps_stop_tx, /* Stop transmission */
  866. .stop_rx = xuartps_stop_rx, /* Stop reception */
  867. .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
  868. .break_ctl = xuartps_break_ctl, /* Start/stop
  869. * transmitting break
  870. */
  871. .set_termios = xuartps_set_termios, /* Set termios */
  872. .startup = xuartps_startup, /* App opens xuartps */
  873. .shutdown = xuartps_shutdown, /* App closes xuartps */
  874. .type = xuartps_type, /* Set UART type */
  875. .verify_port = xuartps_verify_port, /* Verification of port
  876. * params
  877. */
  878. .request_port = xuartps_request_port, /* Claim resources
  879. * associated with a
  880. * xuartps port
  881. */
  882. .release_port = xuartps_release_port, /* Release resources
  883. * associated with a
  884. * xuartps port
  885. */
  886. .config_port = xuartps_config_port, /* Configure when driver
  887. * adds a xuartps port
  888. */
  889. #ifdef CONFIG_CONSOLE_POLL
  890. .poll_get_char = xuartps_poll_get_char,
  891. .poll_put_char = xuartps_poll_put_char,
  892. #endif
  893. };
  894. static struct uart_port xuartps_port[2];
  895. /**
  896. * xuartps_get_port - Configure the port from the platform device resource
  897. * info
  898. *
  899. * Returns a pointer to a uart_port or NULL for failure
  900. **/
  901. static struct uart_port *xuartps_get_port(void)
  902. {
  903. struct uart_port *port;
  904. int id;
  905. /* Find the next unused port */
  906. for (id = 0; id < XUARTPS_NR_PORTS; id++)
  907. if (xuartps_port[id].mapbase == 0)
  908. break;
  909. if (id >= XUARTPS_NR_PORTS)
  910. return NULL;
  911. port = &xuartps_port[id];
  912. /* At this point, we've got an empty uart_port struct, initialize it */
  913. spin_lock_init(&port->lock);
  914. port->membase = NULL;
  915. port->iobase = 1; /* mark port in use */
  916. port->irq = 0;
  917. port->type = PORT_UNKNOWN;
  918. port->iotype = UPIO_MEM32;
  919. port->flags = UPF_BOOT_AUTOCONF;
  920. port->ops = &xuartps_ops;
  921. port->fifosize = XUARTPS_FIFO_SIZE;
  922. port->line = id;
  923. port->dev = NULL;
  924. return port;
  925. }
  926. /*-----------------------Console driver operations--------------------------*/
  927. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  928. /**
  929. * xuartps_console_wait_tx - Wait for the TX to be full
  930. * @port: Handle to the uart port structure
  931. *
  932. **/
  933. static void xuartps_console_wait_tx(struct uart_port *port)
  934. {
  935. while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
  936. != XUARTPS_SR_TXEMPTY)
  937. barrier();
  938. }
  939. /**
  940. * xuartps_console_putchar - write the character to the FIFO buffer
  941. * @port: Handle to the uart port structure
  942. * @ch: Character to be written
  943. *
  944. **/
  945. static void xuartps_console_putchar(struct uart_port *port, int ch)
  946. {
  947. xuartps_console_wait_tx(port);
  948. xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
  949. }
  950. /**
  951. * xuartps_console_write - perform write operation
  952. * @port: Handle to the uart port structure
  953. * @s: Pointer to character array
  954. * @count: No of characters
  955. **/
  956. static void xuartps_console_write(struct console *co, const char *s,
  957. unsigned int count)
  958. {
  959. struct uart_port *port = &xuartps_port[co->index];
  960. unsigned long flags;
  961. unsigned int imr, ctrl;
  962. int locked = 1;
  963. if (oops_in_progress)
  964. locked = spin_trylock_irqsave(&port->lock, flags);
  965. else
  966. spin_lock_irqsave(&port->lock, flags);
  967. /* save and disable interrupt */
  968. imr = xuartps_readl(XUARTPS_IMR_OFFSET);
  969. xuartps_writel(imr, XUARTPS_IDR_OFFSET);
  970. /*
  971. * Make sure that the tx part is enabled. Set the TX enable bit and
  972. * clear the TX disable bit to enable the transmitter.
  973. */
  974. ctrl = xuartps_readl(XUARTPS_CR_OFFSET);
  975. xuartps_writel((ctrl & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
  976. XUARTPS_CR_OFFSET);
  977. uart_console_write(port, s, count, xuartps_console_putchar);
  978. xuartps_console_wait_tx(port);
  979. xuartps_writel(ctrl, XUARTPS_CR_OFFSET);
  980. /* restore interrupt state, it seems like there may be a h/w bug
  981. * in that the interrupt enable register should not need to be
  982. * written based on the data sheet
  983. */
  984. xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
  985. xuartps_writel(imr, XUARTPS_IER_OFFSET);
  986. if (locked)
  987. spin_unlock_irqrestore(&port->lock, flags);
  988. }
  989. /**
  990. * xuartps_console_setup - Initialize the uart to default config
  991. * @co: Console handle
  992. * @options: Initial settings of uart
  993. *
  994. * Returns 0, -ENODEV if no device
  995. **/
  996. static int __init xuartps_console_setup(struct console *co, char *options)
  997. {
  998. struct uart_port *port = &xuartps_port[co->index];
  999. int baud = 9600;
  1000. int bits = 8;
  1001. int parity = 'n';
  1002. int flow = 'n';
  1003. if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
  1004. return -EINVAL;
  1005. if (!port->mapbase) {
  1006. pr_debug("console on ttyPS%i not present\n", co->index);
  1007. return -ENODEV;
  1008. }
  1009. if (options)
  1010. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1011. return uart_set_options(port, co, baud, parity, bits, flow);
  1012. }
  1013. static struct uart_driver xuartps_uart_driver;
  1014. static struct console xuartps_console = {
  1015. .name = XUARTPS_TTY_NAME,
  1016. .write = xuartps_console_write,
  1017. .device = uart_console_device,
  1018. .setup = xuartps_console_setup,
  1019. .flags = CON_PRINTBUFFER,
  1020. .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
  1021. .data = &xuartps_uart_driver,
  1022. };
  1023. /**
  1024. * xuartps_console_init - Initialization call
  1025. *
  1026. * Returns 0 on success, negative error otherwise
  1027. **/
  1028. static int __init xuartps_console_init(void)
  1029. {
  1030. register_console(&xuartps_console);
  1031. return 0;
  1032. }
  1033. console_initcall(xuartps_console_init);
  1034. #endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
  1035. /** Structure Definitions
  1036. */
  1037. static struct uart_driver xuartps_uart_driver = {
  1038. .owner = THIS_MODULE, /* Owner */
  1039. .driver_name = XUARTPS_NAME, /* Driver name */
  1040. .dev_name = XUARTPS_TTY_NAME, /* Node name */
  1041. .major = XUARTPS_MAJOR, /* Major number */
  1042. .minor = XUARTPS_MINOR, /* Minor number */
  1043. .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
  1044. #ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
  1045. .cons = &xuartps_console, /* Console */
  1046. #endif
  1047. };
  1048. #ifdef CONFIG_PM_SLEEP
  1049. /**
  1050. * xuartps_suspend - suspend event
  1051. * @device: Pointer to the device structure
  1052. *
  1053. * Returns 0
  1054. */
  1055. static int xuartps_suspend(struct device *device)
  1056. {
  1057. struct uart_port *port = dev_get_drvdata(device);
  1058. struct tty_struct *tty;
  1059. struct device *tty_dev;
  1060. int may_wake = 0;
  1061. /* Get the tty which could be NULL so don't assume it's valid */
  1062. tty = tty_port_tty_get(&port->state->port);
  1063. if (tty) {
  1064. tty_dev = tty->dev;
  1065. may_wake = device_may_wakeup(tty_dev);
  1066. tty_kref_put(tty);
  1067. }
  1068. /*
  1069. * Call the API provided in serial_core.c file which handles
  1070. * the suspend.
  1071. */
  1072. uart_suspend_port(&xuartps_uart_driver, port);
  1073. if (console_suspend_enabled && !may_wake) {
  1074. struct xuartps *xuartps = port->private_data;
  1075. clk_disable(xuartps->refclk);
  1076. clk_disable(xuartps->aperclk);
  1077. } else {
  1078. unsigned long flags = 0;
  1079. spin_lock_irqsave(&port->lock, flags);
  1080. /* Empty the receive FIFO 1st before making changes */
  1081. while (!(xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_RXEMPTY))
  1082. xuartps_readl(XUARTPS_FIFO_OFFSET);
  1083. /* set RX trigger level to 1 */
  1084. xuartps_writel(1, XUARTPS_RXWM_OFFSET);
  1085. /* disable RX timeout interrups */
  1086. xuartps_writel(XUARTPS_IXR_TOUT, XUARTPS_IDR_OFFSET);
  1087. spin_unlock_irqrestore(&port->lock, flags);
  1088. }
  1089. return 0;
  1090. }
  1091. /**
  1092. * xuartps_resume - Resume after a previous suspend
  1093. * @device: Pointer to the device structure
  1094. *
  1095. * Returns 0
  1096. */
  1097. static int xuartps_resume(struct device *device)
  1098. {
  1099. struct uart_port *port = dev_get_drvdata(device);
  1100. unsigned long flags = 0;
  1101. u32 ctrl_reg;
  1102. struct tty_struct *tty;
  1103. struct device *tty_dev;
  1104. int may_wake = 0;
  1105. /* Get the tty which could be NULL so don't assume it's valid */
  1106. tty = tty_port_tty_get(&port->state->port);
  1107. if (tty) {
  1108. tty_dev = tty->dev;
  1109. may_wake = device_may_wakeup(tty_dev);
  1110. tty_kref_put(tty);
  1111. }
  1112. if (console_suspend_enabled && !may_wake) {
  1113. struct xuartps *xuartps = port->private_data;
  1114. clk_enable(xuartps->aperclk);
  1115. clk_enable(xuartps->refclk);
  1116. spin_lock_irqsave(&port->lock, flags);
  1117. /* Set TX/RX Reset */
  1118. xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
  1119. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
  1120. XUARTPS_CR_OFFSET);
  1121. while (xuartps_readl(XUARTPS_CR_OFFSET) &
  1122. (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST))
  1123. cpu_relax();
  1124. /* restore rx timeout value */
  1125. xuartps_writel(rx_timeout, XUARTPS_RXTOUT_OFFSET);
  1126. /* Enable Tx/Rx */
  1127. ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
  1128. xuartps_writel(
  1129. (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS)) |
  1130. (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
  1131. XUARTPS_CR_OFFSET);
  1132. spin_unlock_irqrestore(&port->lock, flags);
  1133. } else {
  1134. spin_lock_irqsave(&port->lock, flags);
  1135. /* restore original rx trigger level */
  1136. xuartps_writel(rx_trigger_level, XUARTPS_RXWM_OFFSET);
  1137. /* enable RX timeout interrupt */
  1138. xuartps_writel(XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
  1139. spin_unlock_irqrestore(&port->lock, flags);
  1140. }
  1141. return uart_resume_port(&xuartps_uart_driver, port);
  1142. }
  1143. #endif /* ! CONFIG_PM_SLEEP */
  1144. static SIMPLE_DEV_PM_OPS(xuartps_dev_pm_ops, xuartps_suspend, xuartps_resume);
  1145. /* ---------------------------------------------------------------------
  1146. * Platform bus binding
  1147. */
  1148. /**
  1149. * xuartps_probe - Platform driver probe
  1150. * @pdev: Pointer to the platform device structure
  1151. *
  1152. * Returns 0 on success, negative error otherwise
  1153. **/
  1154. static int xuartps_probe(struct platform_device *pdev)
  1155. {
  1156. int rc;
  1157. struct uart_port *port;
  1158. struct resource *res, *res2;
  1159. struct xuartps *xuartps_data;
  1160. xuartps_data = devm_kzalloc(&pdev->dev, sizeof(*xuartps_data),
  1161. GFP_KERNEL);
  1162. if (!xuartps_data)
  1163. return -ENOMEM;
  1164. xuartps_data->aperclk = devm_clk_get(&pdev->dev, "aper_clk");
  1165. if (IS_ERR(xuartps_data->aperclk)) {
  1166. dev_err(&pdev->dev, "aper_clk clock not found.\n");
  1167. return PTR_ERR(xuartps_data->aperclk);
  1168. }
  1169. xuartps_data->refclk = devm_clk_get(&pdev->dev, "ref_clk");
  1170. if (IS_ERR(xuartps_data->refclk)) {
  1171. dev_err(&pdev->dev, "ref_clk clock not found.\n");
  1172. return PTR_ERR(xuartps_data->refclk);
  1173. }
  1174. rc = clk_prepare_enable(xuartps_data->aperclk);
  1175. if (rc) {
  1176. dev_err(&pdev->dev, "Unable to enable APER clock.\n");
  1177. return rc;
  1178. }
  1179. rc = clk_prepare_enable(xuartps_data->refclk);
  1180. if (rc) {
  1181. dev_err(&pdev->dev, "Unable to enable device clock.\n");
  1182. goto err_out_clk_dis_aper;
  1183. }
  1184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1185. if (!res) {
  1186. rc = -ENODEV;
  1187. goto err_out_clk_disable;
  1188. }
  1189. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1190. if (!res2) {
  1191. rc = -ENODEV;
  1192. goto err_out_clk_disable;
  1193. }
  1194. #ifdef CONFIG_COMMON_CLK
  1195. xuartps_data->clk_rate_change_nb.notifier_call =
  1196. xuartps_clk_notifier_cb;
  1197. if (clk_notifier_register(xuartps_data->refclk,
  1198. &xuartps_data->clk_rate_change_nb))
  1199. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  1200. #endif
  1201. /* Initialize the port structure */
  1202. port = xuartps_get_port();
  1203. if (!port) {
  1204. dev_err(&pdev->dev, "Cannot get uart_port structure\n");
  1205. rc = -ENODEV;
  1206. goto err_out_notif_unreg;
  1207. } else {
  1208. /* Register the port.
  1209. * This function also registers this device with the tty layer
  1210. * and triggers invocation of the config_port() entry point.
  1211. */
  1212. port->mapbase = res->start;
  1213. port->irq = res2->start;
  1214. port->dev = &pdev->dev;
  1215. port->uartclk = clk_get_rate(xuartps_data->refclk);
  1216. port->private_data = xuartps_data;
  1217. xuartps_data->port = port;
  1218. platform_set_drvdata(pdev, port);
  1219. rc = uart_add_one_port(&xuartps_uart_driver, port);
  1220. if (rc) {
  1221. dev_err(&pdev->dev,
  1222. "uart_add_one_port() failed; err=%i\n", rc);
  1223. goto err_out_notif_unreg;
  1224. }
  1225. return 0;
  1226. }
  1227. err_out_notif_unreg:
  1228. #ifdef CONFIG_COMMON_CLK
  1229. clk_notifier_unregister(xuartps_data->refclk,
  1230. &xuartps_data->clk_rate_change_nb);
  1231. #endif
  1232. err_out_clk_disable:
  1233. clk_disable_unprepare(xuartps_data->refclk);
  1234. err_out_clk_dis_aper:
  1235. clk_disable_unprepare(xuartps_data->aperclk);
  1236. return rc;
  1237. }
  1238. /**
  1239. * xuartps_remove - called when the platform driver is unregistered
  1240. * @pdev: Pointer to the platform device structure
  1241. *
  1242. * Returns 0 on success, negative error otherwise
  1243. **/
  1244. static int xuartps_remove(struct platform_device *pdev)
  1245. {
  1246. struct uart_port *port = platform_get_drvdata(pdev);
  1247. struct xuartps *xuartps_data = port->private_data;
  1248. int rc;
  1249. /* Remove the xuartps port from the serial core */
  1250. #ifdef CONFIG_COMMON_CLK
  1251. clk_notifier_unregister(xuartps_data->refclk,
  1252. &xuartps_data->clk_rate_change_nb);
  1253. #endif
  1254. rc = uart_remove_one_port(&xuartps_uart_driver, port);
  1255. port->mapbase = 0;
  1256. clk_disable_unprepare(xuartps_data->refclk);
  1257. clk_disable_unprepare(xuartps_data->aperclk);
  1258. return rc;
  1259. }
  1260. /* Match table for of_platform binding */
  1261. static struct of_device_id xuartps_of_match[] = {
  1262. { .compatible = "xlnx,xuartps", },
  1263. {}
  1264. };
  1265. MODULE_DEVICE_TABLE(of, xuartps_of_match);
  1266. static struct platform_driver xuartps_platform_driver = {
  1267. .probe = xuartps_probe, /* Probe method */
  1268. .remove = xuartps_remove, /* Detach method */
  1269. .driver = {
  1270. .owner = THIS_MODULE,
  1271. .name = XUARTPS_NAME, /* Driver name */
  1272. .of_match_table = xuartps_of_match,
  1273. .pm = &xuartps_dev_pm_ops,
  1274. },
  1275. };
  1276. /* ---------------------------------------------------------------------
  1277. * Module Init and Exit
  1278. */
  1279. /**
  1280. * xuartps_init - Initial driver registration call
  1281. *
  1282. * Returns whether the registration was successful or not
  1283. **/
  1284. static int __init xuartps_init(void)
  1285. {
  1286. int retval = 0;
  1287. /* Register the xuartps driver with the serial core */
  1288. retval = uart_register_driver(&xuartps_uart_driver);
  1289. if (retval)
  1290. return retval;
  1291. /* Register the platform driver */
  1292. retval = platform_driver_register(&xuartps_platform_driver);
  1293. if (retval)
  1294. uart_unregister_driver(&xuartps_uart_driver);
  1295. return retval;
  1296. }
  1297. /**
  1298. * xuartps_exit - Driver unregistration call
  1299. **/
  1300. static void __exit xuartps_exit(void)
  1301. {
  1302. /* The order of unregistration is important. Unregister the
  1303. * UART driver before the platform driver crashes the system.
  1304. */
  1305. /* Unregister the platform driver */
  1306. platform_driver_unregister(&xuartps_platform_driver);
  1307. /* Unregister the xuartps driver */
  1308. uart_unregister_driver(&xuartps_uart_driver);
  1309. }
  1310. module_init(xuartps_init);
  1311. module_exit(xuartps_exit);
  1312. MODULE_DESCRIPTION("Driver for PS UART");
  1313. MODULE_AUTHOR("Xilinx Inc.");
  1314. MODULE_LICENSE("GPL");