xilinx_uartps.c 42 KB

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