serial-tegra.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * serial_tegra.c
  3. *
  4. * High-speed serial driver for NVIDIA Tegra SoCs
  5. *
  6. * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
  7. *
  8. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/delay.h>
  25. #include <linux/dmaengine.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/dmapool.h>
  28. #include <linux/err.h>
  29. #include <linux/io.h>
  30. #include <linux/irq.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/serial.h>
  37. #include <linux/serial_8250.h>
  38. #include <linux/serial_core.h>
  39. #include <linux/serial_reg.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <linux/termios.h>
  43. #include <linux/tty.h>
  44. #include <linux/tty_flip.h>
  45. #include <linux/clk/tegra.h>
  46. #define TEGRA_UART_TYPE "TEGRA_UART"
  47. #define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
  48. #define BYTES_TO_ALIGN(x) ((unsigned long)(x) & 0x3)
  49. #define TEGRA_UART_RX_DMA_BUFFER_SIZE 4096
  50. #define TEGRA_UART_LSR_TXFIFO_FULL 0x100
  51. #define TEGRA_UART_IER_EORD 0x20
  52. #define TEGRA_UART_MCR_RTS_EN 0x40
  53. #define TEGRA_UART_MCR_CTS_EN 0x20
  54. #define TEGRA_UART_LSR_ANY (UART_LSR_OE | UART_LSR_BI | \
  55. UART_LSR_PE | UART_LSR_FE)
  56. #define TEGRA_UART_IRDA_CSR 0x08
  57. #define TEGRA_UART_SIR_ENABLED 0x80
  58. #define TEGRA_UART_TX_PIO 1
  59. #define TEGRA_UART_TX_DMA 2
  60. #define TEGRA_UART_MIN_DMA 16
  61. #define TEGRA_UART_FIFO_SIZE 32
  62. /*
  63. * Tx fifo trigger level setting in tegra uart is in
  64. * reverse way then conventional uart.
  65. */
  66. #define TEGRA_UART_TX_TRIG_16B 0x00
  67. #define TEGRA_UART_TX_TRIG_8B 0x10
  68. #define TEGRA_UART_TX_TRIG_4B 0x20
  69. #define TEGRA_UART_TX_TRIG_1B 0x30
  70. #define TEGRA_UART_MAXIMUM 5
  71. /* Default UART setting when started: 115200 no parity, stop, 8 data bits */
  72. #define TEGRA_UART_DEFAULT_BAUD 115200
  73. #define TEGRA_UART_DEFAULT_LSR UART_LCR_WLEN8
  74. /* Tx transfer mode */
  75. #define TEGRA_TX_PIO 1
  76. #define TEGRA_TX_DMA 2
  77. /**
  78. * tegra_uart_chip_data: SOC specific data.
  79. *
  80. * @tx_fifo_full_status: Status flag available for checking tx fifo full.
  81. * @allow_txfifo_reset_fifo_mode: allow_tx fifo reset with fifo mode or not.
  82. * Tegra30 does not allow this.
  83. * @support_clk_src_div: Clock source support the clock divider.
  84. */
  85. struct tegra_uart_chip_data {
  86. bool tx_fifo_full_status;
  87. bool allow_txfifo_reset_fifo_mode;
  88. bool support_clk_src_div;
  89. };
  90. struct tegra_uart_port {
  91. struct uart_port uport;
  92. const struct tegra_uart_chip_data *cdata;
  93. struct clk *uart_clk;
  94. unsigned int current_baud;
  95. /* Register shadow */
  96. unsigned long fcr_shadow;
  97. unsigned long mcr_shadow;
  98. unsigned long lcr_shadow;
  99. unsigned long ier_shadow;
  100. bool rts_active;
  101. int tx_in_progress;
  102. unsigned int tx_bytes;
  103. bool enable_modem_interrupt;
  104. bool rx_timeout;
  105. int rx_in_progress;
  106. int symb_bit;
  107. int dma_req_sel;
  108. struct dma_chan *rx_dma_chan;
  109. struct dma_chan *tx_dma_chan;
  110. dma_addr_t rx_dma_buf_phys;
  111. dma_addr_t tx_dma_buf_phys;
  112. unsigned char *rx_dma_buf_virt;
  113. unsigned char *tx_dma_buf_virt;
  114. struct dma_async_tx_descriptor *tx_dma_desc;
  115. struct dma_async_tx_descriptor *rx_dma_desc;
  116. dma_cookie_t tx_cookie;
  117. dma_cookie_t rx_cookie;
  118. int tx_bytes_requested;
  119. int rx_bytes_requested;
  120. };
  121. static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
  122. static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
  123. static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
  124. unsigned long reg)
  125. {
  126. return readl(tup->uport.membase + (reg << tup->uport.regshift));
  127. }
  128. static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
  129. unsigned long reg)
  130. {
  131. writel(val, tup->uport.membase + (reg << tup->uport.regshift));
  132. }
  133. static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
  134. {
  135. return container_of(u, struct tegra_uart_port, uport);
  136. }
  137. static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
  138. {
  139. struct tegra_uart_port *tup = to_tegra_uport(u);
  140. /*
  141. * RI - Ring detector is active
  142. * CD/DCD/CAR - Carrier detect is always active. For some reason
  143. * linux has different names for carrier detect.
  144. * DSR - Data Set ready is active as the hardware doesn't support it.
  145. * Don't know if the linux support this yet?
  146. * CTS - Clear to send. Always set to active, as the hardware handles
  147. * CTS automatically.
  148. */
  149. if (tup->enable_modem_interrupt)
  150. return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
  151. return TIOCM_CTS;
  152. }
  153. static void set_rts(struct tegra_uart_port *tup, bool active)
  154. {
  155. unsigned long mcr;
  156. mcr = tup->mcr_shadow;
  157. if (active)
  158. mcr |= TEGRA_UART_MCR_RTS_EN;
  159. else
  160. mcr &= ~TEGRA_UART_MCR_RTS_EN;
  161. if (mcr != tup->mcr_shadow) {
  162. tegra_uart_write(tup, mcr, UART_MCR);
  163. tup->mcr_shadow = mcr;
  164. }
  165. return;
  166. }
  167. static void set_dtr(struct tegra_uart_port *tup, bool active)
  168. {
  169. unsigned long mcr;
  170. mcr = tup->mcr_shadow;
  171. if (active)
  172. mcr |= UART_MCR_DTR;
  173. else
  174. mcr &= ~UART_MCR_DTR;
  175. if (mcr != tup->mcr_shadow) {
  176. tegra_uart_write(tup, mcr, UART_MCR);
  177. tup->mcr_shadow = mcr;
  178. }
  179. return;
  180. }
  181. static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
  182. {
  183. struct tegra_uart_port *tup = to_tegra_uport(u);
  184. unsigned long mcr;
  185. int dtr_enable;
  186. mcr = tup->mcr_shadow;
  187. tup->rts_active = !!(mctrl & TIOCM_RTS);
  188. set_rts(tup, tup->rts_active);
  189. dtr_enable = !!(mctrl & TIOCM_DTR);
  190. set_dtr(tup, dtr_enable);
  191. return;
  192. }
  193. static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
  194. {
  195. struct tegra_uart_port *tup = to_tegra_uport(u);
  196. unsigned long lcr;
  197. lcr = tup->lcr_shadow;
  198. if (break_ctl)
  199. lcr |= UART_LCR_SBC;
  200. else
  201. lcr &= ~UART_LCR_SBC;
  202. tegra_uart_write(tup, lcr, UART_LCR);
  203. tup->lcr_shadow = lcr;
  204. }
  205. /* Wait for a symbol-time. */
  206. static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
  207. unsigned int syms)
  208. {
  209. if (tup->current_baud)
  210. udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
  211. tup->current_baud));
  212. }
  213. static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
  214. {
  215. unsigned long fcr = tup->fcr_shadow;
  216. if (tup->cdata->allow_txfifo_reset_fifo_mode) {
  217. fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  218. tegra_uart_write(tup, fcr, UART_FCR);
  219. } else {
  220. fcr &= ~UART_FCR_ENABLE_FIFO;
  221. tegra_uart_write(tup, fcr, UART_FCR);
  222. udelay(60);
  223. fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  224. tegra_uart_write(tup, fcr, UART_FCR);
  225. fcr |= UART_FCR_ENABLE_FIFO;
  226. tegra_uart_write(tup, fcr, UART_FCR);
  227. }
  228. /* Dummy read to ensure the write is posted */
  229. tegra_uart_read(tup, UART_SCR);
  230. /* Wait for the flush to propagate. */
  231. tegra_uart_wait_sym_time(tup, 1);
  232. }
  233. static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
  234. {
  235. unsigned long rate;
  236. unsigned int divisor;
  237. unsigned long lcr;
  238. int ret;
  239. if (tup->current_baud == baud)
  240. return 0;
  241. if (tup->cdata->support_clk_src_div) {
  242. rate = baud * 16;
  243. ret = clk_set_rate(tup->uart_clk, rate);
  244. if (ret < 0) {
  245. dev_err(tup->uport.dev,
  246. "clk_set_rate() failed for rate %lu\n", rate);
  247. return ret;
  248. }
  249. divisor = 1;
  250. } else {
  251. rate = clk_get_rate(tup->uart_clk);
  252. divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
  253. }
  254. lcr = tup->lcr_shadow;
  255. lcr |= UART_LCR_DLAB;
  256. tegra_uart_write(tup, lcr, UART_LCR);
  257. tegra_uart_write(tup, divisor & 0xFF, UART_TX);
  258. tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
  259. lcr &= ~UART_LCR_DLAB;
  260. tegra_uart_write(tup, lcr, UART_LCR);
  261. /* Dummy read to ensure the write is posted */
  262. tegra_uart_read(tup, UART_SCR);
  263. tup->current_baud = baud;
  264. /* wait two character intervals at new rate */
  265. tegra_uart_wait_sym_time(tup, 2);
  266. return 0;
  267. }
  268. static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
  269. unsigned long lsr)
  270. {
  271. char flag = TTY_NORMAL;
  272. if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
  273. if (lsr & UART_LSR_OE) {
  274. /* Overrrun error */
  275. flag |= TTY_OVERRUN;
  276. tup->uport.icount.overrun++;
  277. dev_err(tup->uport.dev, "Got overrun errors\n");
  278. } else if (lsr & UART_LSR_PE) {
  279. /* Parity error */
  280. flag |= TTY_PARITY;
  281. tup->uport.icount.parity++;
  282. dev_err(tup->uport.dev, "Got Parity errors\n");
  283. } else if (lsr & UART_LSR_FE) {
  284. flag |= TTY_FRAME;
  285. tup->uport.icount.frame++;
  286. dev_err(tup->uport.dev, "Got frame errors\n");
  287. } else if (lsr & UART_LSR_BI) {
  288. dev_err(tup->uport.dev, "Got Break\n");
  289. tup->uport.icount.brk++;
  290. /* If FIFO read error without any data, reset Rx FIFO */
  291. if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
  292. tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
  293. }
  294. }
  295. return flag;
  296. }
  297. static int tegra_uart_request_port(struct uart_port *u)
  298. {
  299. return 0;
  300. }
  301. static void tegra_uart_release_port(struct uart_port *u)
  302. {
  303. /* Nothing to do here */
  304. }
  305. static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
  306. {
  307. struct circ_buf *xmit = &tup->uport.state->xmit;
  308. int i;
  309. for (i = 0; i < max_bytes; i++) {
  310. BUG_ON(uart_circ_empty(xmit));
  311. if (tup->cdata->tx_fifo_full_status) {
  312. unsigned long lsr = tegra_uart_read(tup, UART_LSR);
  313. if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
  314. break;
  315. }
  316. tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
  317. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  318. tup->uport.icount.tx++;
  319. }
  320. }
  321. static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
  322. unsigned int bytes)
  323. {
  324. if (bytes > TEGRA_UART_MIN_DMA)
  325. bytes = TEGRA_UART_MIN_DMA;
  326. tup->tx_in_progress = TEGRA_UART_TX_PIO;
  327. tup->tx_bytes = bytes;
  328. tup->ier_shadow |= UART_IER_THRI;
  329. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  330. }
  331. static void tegra_uart_tx_dma_complete(void *args)
  332. {
  333. struct tegra_uart_port *tup = args;
  334. struct circ_buf *xmit = &tup->uport.state->xmit;
  335. struct dma_tx_state state;
  336. unsigned long flags;
  337. int count;
  338. dmaengine_tx_status(tup->tx_dma_chan, tup->rx_cookie, &state);
  339. count = tup->tx_bytes_requested - state.residue;
  340. async_tx_ack(tup->tx_dma_desc);
  341. spin_lock_irqsave(&tup->uport.lock, flags);
  342. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  343. tup->tx_in_progress = 0;
  344. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  345. uart_write_wakeup(&tup->uport);
  346. tegra_uart_start_next_tx(tup);
  347. spin_unlock_irqrestore(&tup->uport.lock, flags);
  348. }
  349. static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
  350. unsigned long count)
  351. {
  352. struct circ_buf *xmit = &tup->uport.state->xmit;
  353. dma_addr_t tx_phys_addr;
  354. dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
  355. UART_XMIT_SIZE, DMA_TO_DEVICE);
  356. tup->tx_bytes = count & ~(0xF);
  357. tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
  358. tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
  359. tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
  360. DMA_PREP_INTERRUPT);
  361. if (!tup->tx_dma_desc) {
  362. dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
  363. return -EIO;
  364. }
  365. tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
  366. tup->tx_dma_desc->callback_param = tup;
  367. tup->tx_in_progress = TEGRA_UART_TX_DMA;
  368. tup->tx_bytes_requested = tup->tx_bytes;
  369. tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
  370. dma_async_issue_pending(tup->tx_dma_chan);
  371. return 0;
  372. }
  373. static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
  374. {
  375. unsigned long tail;
  376. unsigned long count;
  377. struct circ_buf *xmit = &tup->uport.state->xmit;
  378. tail = (unsigned long)&xmit->buf[xmit->tail];
  379. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  380. if (!count)
  381. return;
  382. if (count < TEGRA_UART_MIN_DMA)
  383. tegra_uart_start_pio_tx(tup, count);
  384. else if (BYTES_TO_ALIGN(tail) > 0)
  385. tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
  386. else
  387. tegra_uart_start_tx_dma(tup, count);
  388. }
  389. /* Called by serial core driver with u->lock taken. */
  390. static void tegra_uart_start_tx(struct uart_port *u)
  391. {
  392. struct tegra_uart_port *tup = to_tegra_uport(u);
  393. struct circ_buf *xmit = &u->state->xmit;
  394. if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
  395. tegra_uart_start_next_tx(tup);
  396. }
  397. static unsigned int tegra_uart_tx_empty(struct uart_port *u)
  398. {
  399. struct tegra_uart_port *tup = to_tegra_uport(u);
  400. unsigned int ret = 0;
  401. unsigned long flags;
  402. spin_lock_irqsave(&u->lock, flags);
  403. if (!tup->tx_in_progress) {
  404. unsigned long lsr = tegra_uart_read(tup, UART_LSR);
  405. if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
  406. ret = TIOCSER_TEMT;
  407. }
  408. spin_unlock_irqrestore(&u->lock, flags);
  409. return ret;
  410. }
  411. static void tegra_uart_stop_tx(struct uart_port *u)
  412. {
  413. struct tegra_uart_port *tup = to_tegra_uport(u);
  414. struct circ_buf *xmit = &tup->uport.state->xmit;
  415. struct dma_tx_state state;
  416. int count;
  417. dmaengine_terminate_all(tup->tx_dma_chan);
  418. dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
  419. count = tup->tx_bytes_requested - state.residue;
  420. async_tx_ack(tup->tx_dma_desc);
  421. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  422. tup->tx_in_progress = 0;
  423. return;
  424. }
  425. static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
  426. {
  427. struct circ_buf *xmit = &tup->uport.state->xmit;
  428. tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
  429. tup->tx_in_progress = 0;
  430. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  431. uart_write_wakeup(&tup->uport);
  432. tegra_uart_start_next_tx(tup);
  433. return;
  434. }
  435. static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
  436. struct tty_port *tty)
  437. {
  438. do {
  439. char flag = TTY_NORMAL;
  440. unsigned long lsr = 0;
  441. unsigned char ch;
  442. lsr = tegra_uart_read(tup, UART_LSR);
  443. if (!(lsr & UART_LSR_DR))
  444. break;
  445. flag = tegra_uart_decode_rx_error(tup, lsr);
  446. ch = (unsigned char) tegra_uart_read(tup, UART_RX);
  447. tup->uport.icount.rx++;
  448. if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
  449. tty_insert_flip_char(tty, ch, flag);
  450. } while (1);
  451. return;
  452. }
  453. static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
  454. struct tty_port *tty, int count)
  455. {
  456. int copied;
  457. tup->uport.icount.rx += count;
  458. if (!tty) {
  459. dev_err(tup->uport.dev, "No tty port\n");
  460. return;
  461. }
  462. dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
  463. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  464. copied = tty_insert_flip_string(tty,
  465. ((unsigned char *)(tup->rx_dma_buf_virt)), count);
  466. if (copied != count) {
  467. WARN_ON(1);
  468. dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
  469. }
  470. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  471. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
  472. }
  473. static void tegra_uart_rx_dma_complete(void *args)
  474. {
  475. struct tegra_uart_port *tup = args;
  476. struct uart_port *u = &tup->uport;
  477. int count = tup->rx_bytes_requested;
  478. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  479. struct tty_port *port = &u->state->port;
  480. unsigned long flags;
  481. async_tx_ack(tup->rx_dma_desc);
  482. spin_lock_irqsave(&u->lock, flags);
  483. /* Deactivate flow control to stop sender */
  484. if (tup->rts_active)
  485. set_rts(tup, false);
  486. /* If we are here, DMA is stopped */
  487. if (count)
  488. tegra_uart_copy_rx_to_tty(tup, port, count);
  489. tegra_uart_handle_rx_pio(tup, port);
  490. if (tty) {
  491. spin_unlock_irqrestore(&u->lock, flags);
  492. tty_flip_buffer_push(port);
  493. spin_lock_irqsave(&u->lock, flags);
  494. tty_kref_put(tty);
  495. }
  496. tegra_uart_start_rx_dma(tup);
  497. /* Activate flow control to start transfer */
  498. if (tup->rts_active)
  499. set_rts(tup, true);
  500. spin_unlock_irqrestore(&u->lock, flags);
  501. }
  502. static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup,
  503. unsigned long *flags)
  504. {
  505. struct dma_tx_state state;
  506. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  507. struct tty_port *port = &tup->uport.state->port;
  508. struct uart_port *u = &tup->uport;
  509. int count;
  510. /* Deactivate flow control to stop sender */
  511. if (tup->rts_active)
  512. set_rts(tup, false);
  513. dmaengine_terminate_all(tup->rx_dma_chan);
  514. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  515. count = tup->rx_bytes_requested - state.residue;
  516. /* If we are here, DMA is stopped */
  517. if (count)
  518. tegra_uart_copy_rx_to_tty(tup, port, count);
  519. tegra_uart_handle_rx_pio(tup, port);
  520. if (tty) {
  521. spin_unlock_irqrestore(&u->lock, *flags);
  522. tty_flip_buffer_push(port);
  523. spin_lock_irqsave(&u->lock, *flags);
  524. tty_kref_put(tty);
  525. }
  526. tegra_uart_start_rx_dma(tup);
  527. if (tup->rts_active)
  528. set_rts(tup, true);
  529. }
  530. static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
  531. {
  532. unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
  533. tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
  534. tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
  535. DMA_PREP_INTERRUPT);
  536. if (!tup->rx_dma_desc) {
  537. dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
  538. return -EIO;
  539. }
  540. tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
  541. tup->rx_dma_desc->callback_param = tup;
  542. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  543. count, DMA_TO_DEVICE);
  544. tup->rx_bytes_requested = count;
  545. tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
  546. dma_async_issue_pending(tup->rx_dma_chan);
  547. return 0;
  548. }
  549. static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
  550. {
  551. struct tegra_uart_port *tup = to_tegra_uport(u);
  552. unsigned long msr;
  553. msr = tegra_uart_read(tup, UART_MSR);
  554. if (!(msr & UART_MSR_ANY_DELTA))
  555. return;
  556. if (msr & UART_MSR_TERI)
  557. tup->uport.icount.rng++;
  558. if (msr & UART_MSR_DDSR)
  559. tup->uport.icount.dsr++;
  560. /* We may only get DDCD when HW init and reset */
  561. if (msr & UART_MSR_DDCD)
  562. uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
  563. /* Will start/stop_tx accordingly */
  564. if (msr & UART_MSR_DCTS)
  565. uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
  566. return;
  567. }
  568. static irqreturn_t tegra_uart_isr(int irq, void *data)
  569. {
  570. struct tegra_uart_port *tup = data;
  571. struct uart_port *u = &tup->uport;
  572. unsigned long iir;
  573. unsigned long ier;
  574. bool is_rx_int = false;
  575. unsigned long flags;
  576. spin_lock_irqsave(&u->lock, flags);
  577. while (1) {
  578. iir = tegra_uart_read(tup, UART_IIR);
  579. if (iir & UART_IIR_NO_INT) {
  580. if (is_rx_int) {
  581. tegra_uart_handle_rx_dma(tup, &flags);
  582. if (tup->rx_in_progress) {
  583. ier = tup->ier_shadow;
  584. ier |= (UART_IER_RLSI | UART_IER_RTOIE |
  585. TEGRA_UART_IER_EORD);
  586. tup->ier_shadow = ier;
  587. tegra_uart_write(tup, ier, UART_IER);
  588. }
  589. }
  590. spin_unlock_irqrestore(&u->lock, flags);
  591. return IRQ_HANDLED;
  592. }
  593. switch ((iir >> 1) & 0x7) {
  594. case 0: /* Modem signal change interrupt */
  595. tegra_uart_handle_modem_signal_change(u);
  596. break;
  597. case 1: /* Transmit interrupt only triggered when using PIO */
  598. tup->ier_shadow &= ~UART_IER_THRI;
  599. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  600. tegra_uart_handle_tx_pio(tup);
  601. break;
  602. case 4: /* End of data */
  603. case 6: /* Rx timeout */
  604. case 2: /* Receive */
  605. if (!is_rx_int) {
  606. is_rx_int = true;
  607. /* Disable Rx interrupts */
  608. ier = tup->ier_shadow;
  609. ier |= UART_IER_RDI;
  610. tegra_uart_write(tup, ier, UART_IER);
  611. ier &= ~(UART_IER_RDI | UART_IER_RLSI |
  612. UART_IER_RTOIE | TEGRA_UART_IER_EORD);
  613. tup->ier_shadow = ier;
  614. tegra_uart_write(tup, ier, UART_IER);
  615. }
  616. break;
  617. case 3: /* Receive error */
  618. tegra_uart_decode_rx_error(tup,
  619. tegra_uart_read(tup, UART_LSR));
  620. break;
  621. case 5: /* break nothing to handle */
  622. case 7: /* break nothing to handle */
  623. break;
  624. }
  625. }
  626. }
  627. static void tegra_uart_stop_rx(struct uart_port *u)
  628. {
  629. struct tegra_uart_port *tup = to_tegra_uport(u);
  630. struct tty_struct *tty;
  631. struct tty_port *port = &u->state->port;
  632. struct dma_tx_state state;
  633. unsigned long ier;
  634. int count;
  635. if (tup->rts_active)
  636. set_rts(tup, false);
  637. if (!tup->rx_in_progress)
  638. return;
  639. tty = tty_port_tty_get(&tup->uport.state->port);
  640. tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
  641. ier = tup->ier_shadow;
  642. ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
  643. TEGRA_UART_IER_EORD);
  644. tup->ier_shadow = ier;
  645. tegra_uart_write(tup, ier, UART_IER);
  646. tup->rx_in_progress = 0;
  647. if (tup->rx_dma_chan) {
  648. dmaengine_terminate_all(tup->rx_dma_chan);
  649. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  650. async_tx_ack(tup->rx_dma_desc);
  651. count = tup->rx_bytes_requested - state.residue;
  652. tegra_uart_copy_rx_to_tty(tup, port, count);
  653. tegra_uart_handle_rx_pio(tup, port);
  654. } else {
  655. tegra_uart_handle_rx_pio(tup, port);
  656. }
  657. if (tty) {
  658. tty_flip_buffer_push(port);
  659. tty_kref_put(tty);
  660. }
  661. return;
  662. }
  663. static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
  664. {
  665. unsigned long flags;
  666. unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
  667. unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
  668. unsigned long wait_time;
  669. unsigned long lsr;
  670. unsigned long msr;
  671. unsigned long mcr;
  672. /* Disable interrupts */
  673. tegra_uart_write(tup, 0, UART_IER);
  674. lsr = tegra_uart_read(tup, UART_LSR);
  675. if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  676. msr = tegra_uart_read(tup, UART_MSR);
  677. mcr = tegra_uart_read(tup, UART_MCR);
  678. if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
  679. dev_err(tup->uport.dev,
  680. "Tx Fifo not empty, CTS disabled, waiting\n");
  681. /* Wait for Tx fifo to be empty */
  682. while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  683. wait_time = min(fifo_empty_time, 100lu);
  684. udelay(wait_time);
  685. fifo_empty_time -= wait_time;
  686. if (!fifo_empty_time) {
  687. msr = tegra_uart_read(tup, UART_MSR);
  688. mcr = tegra_uart_read(tup, UART_MCR);
  689. if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
  690. (msr & UART_MSR_CTS))
  691. dev_err(tup->uport.dev,
  692. "Slave not ready\n");
  693. break;
  694. }
  695. lsr = tegra_uart_read(tup, UART_LSR);
  696. }
  697. }
  698. spin_lock_irqsave(&tup->uport.lock, flags);
  699. /* Reset the Rx and Tx FIFOs */
  700. tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
  701. tup->current_baud = 0;
  702. spin_unlock_irqrestore(&tup->uport.lock, flags);
  703. clk_disable_unprepare(tup->uart_clk);
  704. }
  705. static int tegra_uart_hw_init(struct tegra_uart_port *tup)
  706. {
  707. int ret;
  708. tup->fcr_shadow = 0;
  709. tup->mcr_shadow = 0;
  710. tup->lcr_shadow = 0;
  711. tup->ier_shadow = 0;
  712. tup->current_baud = 0;
  713. clk_prepare_enable(tup->uart_clk);
  714. /* Reset the UART controller to clear all previous status.*/
  715. tegra_periph_reset_assert(tup->uart_clk);
  716. udelay(10);
  717. tegra_periph_reset_deassert(tup->uart_clk);
  718. tup->rx_in_progress = 0;
  719. tup->tx_in_progress = 0;
  720. /*
  721. * Set the trigger level
  722. *
  723. * For PIO mode:
  724. *
  725. * For receive, this will interrupt the CPU after that many number of
  726. * bytes are received, for the remaining bytes the receive timeout
  727. * interrupt is received. Rx high watermark is set to 4.
  728. *
  729. * For transmit, if the trasnmit interrupt is enabled, this will
  730. * interrupt the CPU when the number of entries in the FIFO reaches the
  731. * low watermark. Tx low watermark is set to 16 bytes.
  732. *
  733. * For DMA mode:
  734. *
  735. * Set the Tx trigger to 16. This should match the DMA burst size that
  736. * programmed in the DMA registers.
  737. */
  738. tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
  739. tup->fcr_shadow |= UART_FCR_R_TRIG_01;
  740. tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
  741. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  742. /*
  743. * Initialize the UART with default configuration
  744. * (115200, N, 8, 1) so that the receive DMA buffer may be
  745. * enqueued
  746. */
  747. tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
  748. tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
  749. tup->fcr_shadow |= UART_FCR_DMA_SELECT;
  750. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  751. ret = tegra_uart_start_rx_dma(tup);
  752. if (ret < 0) {
  753. dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
  754. return ret;
  755. }
  756. tup->rx_in_progress = 1;
  757. /*
  758. * Enable IE_RXS for the receive status interrupts like line errros.
  759. * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
  760. *
  761. * If using DMA mode, enable EORD instead of receive interrupt which
  762. * will interrupt after the UART is done with the receive instead of
  763. * the interrupt when the FIFO "threshold" is reached.
  764. *
  765. * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
  766. * the DATA is sitting in the FIFO and couldn't be transferred to the
  767. * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
  768. * triggered when there is a pause of the incomming data stream for 4
  769. * characters long.
  770. *
  771. * For pauses in the data which is not aligned to 4 bytes, we get
  772. * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
  773. * then the EORD.
  774. */
  775. tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
  776. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  777. return 0;
  778. }
  779. static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
  780. bool dma_to_memory)
  781. {
  782. struct dma_chan *dma_chan;
  783. unsigned char *dma_buf;
  784. dma_addr_t dma_phys;
  785. int ret;
  786. struct dma_slave_config dma_sconfig;
  787. dma_cap_mask_t mask;
  788. dma_cap_zero(mask);
  789. dma_cap_set(DMA_SLAVE, mask);
  790. dma_chan = dma_request_channel(mask, NULL, NULL);
  791. if (!dma_chan) {
  792. dev_err(tup->uport.dev,
  793. "Dma channel is not available, will try later\n");
  794. return -EPROBE_DEFER;
  795. }
  796. if (dma_to_memory) {
  797. dma_buf = dma_alloc_coherent(tup->uport.dev,
  798. TEGRA_UART_RX_DMA_BUFFER_SIZE,
  799. &dma_phys, GFP_KERNEL);
  800. if (!dma_buf) {
  801. dev_err(tup->uport.dev,
  802. "Not able to allocate the dma buffer\n");
  803. dma_release_channel(dma_chan);
  804. return -ENOMEM;
  805. }
  806. } else {
  807. dma_phys = dma_map_single(tup->uport.dev,
  808. tup->uport.state->xmit.buf, UART_XMIT_SIZE,
  809. DMA_TO_DEVICE);
  810. dma_buf = tup->uport.state->xmit.buf;
  811. }
  812. dma_sconfig.slave_id = tup->dma_req_sel;
  813. if (dma_to_memory) {
  814. dma_sconfig.src_addr = tup->uport.mapbase;
  815. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  816. dma_sconfig.src_maxburst = 4;
  817. } else {
  818. dma_sconfig.dst_addr = tup->uport.mapbase;
  819. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  820. dma_sconfig.dst_maxburst = 16;
  821. }
  822. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  823. if (ret < 0) {
  824. dev_err(tup->uport.dev,
  825. "Dma slave config failed, err = %d\n", ret);
  826. goto scrub;
  827. }
  828. if (dma_to_memory) {
  829. tup->rx_dma_chan = dma_chan;
  830. tup->rx_dma_buf_virt = dma_buf;
  831. tup->rx_dma_buf_phys = dma_phys;
  832. } else {
  833. tup->tx_dma_chan = dma_chan;
  834. tup->tx_dma_buf_virt = dma_buf;
  835. tup->tx_dma_buf_phys = dma_phys;
  836. }
  837. return 0;
  838. scrub:
  839. dma_release_channel(dma_chan);
  840. return ret;
  841. }
  842. static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
  843. bool dma_to_memory)
  844. {
  845. struct dma_chan *dma_chan;
  846. if (dma_to_memory) {
  847. dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
  848. tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
  849. dma_chan = tup->rx_dma_chan;
  850. tup->rx_dma_chan = NULL;
  851. tup->rx_dma_buf_phys = 0;
  852. tup->rx_dma_buf_virt = NULL;
  853. } else {
  854. dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
  855. UART_XMIT_SIZE, DMA_TO_DEVICE);
  856. dma_chan = tup->tx_dma_chan;
  857. tup->tx_dma_chan = NULL;
  858. tup->tx_dma_buf_phys = 0;
  859. tup->tx_dma_buf_virt = NULL;
  860. }
  861. dma_release_channel(dma_chan);
  862. }
  863. static int tegra_uart_startup(struct uart_port *u)
  864. {
  865. struct tegra_uart_port *tup = to_tegra_uport(u);
  866. int ret;
  867. ret = tegra_uart_dma_channel_allocate(tup, false);
  868. if (ret < 0) {
  869. dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
  870. return ret;
  871. }
  872. ret = tegra_uart_dma_channel_allocate(tup, true);
  873. if (ret < 0) {
  874. dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
  875. goto fail_rx_dma;
  876. }
  877. ret = tegra_uart_hw_init(tup);
  878. if (ret < 0) {
  879. dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
  880. goto fail_hw_init;
  881. }
  882. ret = request_irq(u->irq, tegra_uart_isr, IRQF_DISABLED,
  883. dev_name(u->dev), tup);
  884. if (ret < 0) {
  885. dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
  886. goto fail_hw_init;
  887. }
  888. return 0;
  889. fail_hw_init:
  890. tegra_uart_dma_channel_free(tup, true);
  891. fail_rx_dma:
  892. tegra_uart_dma_channel_free(tup, false);
  893. return ret;
  894. }
  895. static void tegra_uart_shutdown(struct uart_port *u)
  896. {
  897. struct tegra_uart_port *tup = to_tegra_uport(u);
  898. tegra_uart_hw_deinit(tup);
  899. tup->rx_in_progress = 0;
  900. tup->tx_in_progress = 0;
  901. tegra_uart_dma_channel_free(tup, true);
  902. tegra_uart_dma_channel_free(tup, false);
  903. free_irq(u->irq, tup);
  904. }
  905. static void tegra_uart_enable_ms(struct uart_port *u)
  906. {
  907. struct tegra_uart_port *tup = to_tegra_uport(u);
  908. if (tup->enable_modem_interrupt) {
  909. tup->ier_shadow |= UART_IER_MSI;
  910. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  911. }
  912. }
  913. static void tegra_uart_set_termios(struct uart_port *u,
  914. struct ktermios *termios, struct ktermios *oldtermios)
  915. {
  916. struct tegra_uart_port *tup = to_tegra_uport(u);
  917. unsigned int baud;
  918. unsigned long flags;
  919. unsigned int lcr;
  920. int symb_bit = 1;
  921. struct clk *parent_clk = clk_get_parent(tup->uart_clk);
  922. unsigned long parent_clk_rate = clk_get_rate(parent_clk);
  923. int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
  924. max_divider *= 16;
  925. spin_lock_irqsave(&u->lock, flags);
  926. /* Changing configuration, it is safe to stop any rx now */
  927. if (tup->rts_active)
  928. set_rts(tup, false);
  929. /* Clear all interrupts as configuration is going to be change */
  930. tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
  931. tegra_uart_read(tup, UART_IER);
  932. tegra_uart_write(tup, 0, UART_IER);
  933. tegra_uart_read(tup, UART_IER);
  934. /* Parity */
  935. lcr = tup->lcr_shadow;
  936. lcr &= ~UART_LCR_PARITY;
  937. /* CMSPAR isn't supported by this driver */
  938. termios->c_cflag &= ~CMSPAR;
  939. if ((termios->c_cflag & PARENB) == PARENB) {
  940. symb_bit++;
  941. if (termios->c_cflag & PARODD) {
  942. lcr |= UART_LCR_PARITY;
  943. lcr &= ~UART_LCR_EPAR;
  944. lcr &= ~UART_LCR_SPAR;
  945. } else {
  946. lcr |= UART_LCR_PARITY;
  947. lcr |= UART_LCR_EPAR;
  948. lcr &= ~UART_LCR_SPAR;
  949. }
  950. }
  951. lcr &= ~UART_LCR_WLEN8;
  952. switch (termios->c_cflag & CSIZE) {
  953. case CS5:
  954. lcr |= UART_LCR_WLEN5;
  955. symb_bit += 5;
  956. break;
  957. case CS6:
  958. lcr |= UART_LCR_WLEN6;
  959. symb_bit += 6;
  960. break;
  961. case CS7:
  962. lcr |= UART_LCR_WLEN7;
  963. symb_bit += 7;
  964. break;
  965. default:
  966. lcr |= UART_LCR_WLEN8;
  967. symb_bit += 8;
  968. break;
  969. }
  970. /* Stop bits */
  971. if (termios->c_cflag & CSTOPB) {
  972. lcr |= UART_LCR_STOP;
  973. symb_bit += 2;
  974. } else {
  975. lcr &= ~UART_LCR_STOP;
  976. symb_bit++;
  977. }
  978. tegra_uart_write(tup, lcr, UART_LCR);
  979. tup->lcr_shadow = lcr;
  980. tup->symb_bit = symb_bit;
  981. /* Baud rate. */
  982. baud = uart_get_baud_rate(u, termios, oldtermios,
  983. parent_clk_rate/max_divider,
  984. parent_clk_rate/16);
  985. spin_unlock_irqrestore(&u->lock, flags);
  986. tegra_set_baudrate(tup, baud);
  987. if (tty_termios_baud_rate(termios))
  988. tty_termios_encode_baud_rate(termios, baud, baud);
  989. spin_lock_irqsave(&u->lock, flags);
  990. /* Flow control */
  991. if (termios->c_cflag & CRTSCTS) {
  992. tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
  993. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  994. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  995. /* if top layer has asked to set rts active then do so here */
  996. if (tup->rts_active)
  997. set_rts(tup, true);
  998. } else {
  999. tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
  1000. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1001. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1002. }
  1003. /* update the port timeout based on new settings */
  1004. uart_update_timeout(u, termios->c_cflag, baud);
  1005. /* Make sure all write has completed */
  1006. tegra_uart_read(tup, UART_IER);
  1007. /* Reenable interrupt */
  1008. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  1009. tegra_uart_read(tup, UART_IER);
  1010. spin_unlock_irqrestore(&u->lock, flags);
  1011. return;
  1012. }
  1013. /*
  1014. * Flush any TX data submitted for DMA and PIO. Called when the
  1015. * TX circular buffer is reset.
  1016. */
  1017. static void tegra_uart_flush_buffer(struct uart_port *u)
  1018. {
  1019. struct tegra_uart_port *tup = to_tegra_uport(u);
  1020. tup->tx_bytes = 0;
  1021. if (tup->tx_dma_chan)
  1022. dmaengine_terminate_all(tup->tx_dma_chan);
  1023. return;
  1024. }
  1025. static const char *tegra_uart_type(struct uart_port *u)
  1026. {
  1027. return TEGRA_UART_TYPE;
  1028. }
  1029. static struct uart_ops tegra_uart_ops = {
  1030. .tx_empty = tegra_uart_tx_empty,
  1031. .set_mctrl = tegra_uart_set_mctrl,
  1032. .get_mctrl = tegra_uart_get_mctrl,
  1033. .stop_tx = tegra_uart_stop_tx,
  1034. .start_tx = tegra_uart_start_tx,
  1035. .stop_rx = tegra_uart_stop_rx,
  1036. .flush_buffer = tegra_uart_flush_buffer,
  1037. .enable_ms = tegra_uart_enable_ms,
  1038. .break_ctl = tegra_uart_break_ctl,
  1039. .startup = tegra_uart_startup,
  1040. .shutdown = tegra_uart_shutdown,
  1041. .set_termios = tegra_uart_set_termios,
  1042. .type = tegra_uart_type,
  1043. .request_port = tegra_uart_request_port,
  1044. .release_port = tegra_uart_release_port,
  1045. };
  1046. static struct uart_driver tegra_uart_driver = {
  1047. .owner = THIS_MODULE,
  1048. .driver_name = "tegra_hsuart",
  1049. .dev_name = "ttyTHS",
  1050. .cons = NULL,
  1051. .nr = TEGRA_UART_MAXIMUM,
  1052. };
  1053. static int tegra_uart_parse_dt(struct platform_device *pdev,
  1054. struct tegra_uart_port *tup)
  1055. {
  1056. struct device_node *np = pdev->dev.of_node;
  1057. u32 of_dma[2];
  1058. int port;
  1059. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  1060. of_dma, 2) >= 0) {
  1061. tup->dma_req_sel = of_dma[1];
  1062. } else {
  1063. dev_err(&pdev->dev, "missing dma requestor in device tree\n");
  1064. return -EINVAL;
  1065. }
  1066. port = of_alias_get_id(np, "serial");
  1067. if (port < 0) {
  1068. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
  1069. return port;
  1070. }
  1071. tup->uport.line = port;
  1072. tup->enable_modem_interrupt = of_property_read_bool(np,
  1073. "nvidia,enable-modem-interrupt");
  1074. return 0;
  1075. }
  1076. static struct tegra_uart_chip_data tegra20_uart_chip_data = {
  1077. .tx_fifo_full_status = false,
  1078. .allow_txfifo_reset_fifo_mode = true,
  1079. .support_clk_src_div = false,
  1080. };
  1081. static struct tegra_uart_chip_data tegra30_uart_chip_data = {
  1082. .tx_fifo_full_status = true,
  1083. .allow_txfifo_reset_fifo_mode = false,
  1084. .support_clk_src_div = true,
  1085. };
  1086. static struct of_device_id tegra_uart_of_match[] = {
  1087. {
  1088. .compatible = "nvidia,tegra30-hsuart",
  1089. .data = &tegra30_uart_chip_data,
  1090. }, {
  1091. .compatible = "nvidia,tegra20-hsuart",
  1092. .data = &tegra20_uart_chip_data,
  1093. }, {
  1094. },
  1095. };
  1096. MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
  1097. static int tegra_uart_probe(struct platform_device *pdev)
  1098. {
  1099. struct tegra_uart_port *tup;
  1100. struct uart_port *u;
  1101. struct resource *resource;
  1102. int ret;
  1103. const struct tegra_uart_chip_data *cdata;
  1104. const struct of_device_id *match;
  1105. match = of_match_device(tegra_uart_of_match, &pdev->dev);
  1106. if (!match) {
  1107. dev_err(&pdev->dev, "Error: No device match found\n");
  1108. return -ENODEV;
  1109. }
  1110. cdata = match->data;
  1111. tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
  1112. if (!tup) {
  1113. dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
  1114. return -ENOMEM;
  1115. }
  1116. ret = tegra_uart_parse_dt(pdev, tup);
  1117. if (ret < 0)
  1118. return ret;
  1119. u = &tup->uport;
  1120. u->dev = &pdev->dev;
  1121. u->ops = &tegra_uart_ops;
  1122. u->type = PORT_TEGRA;
  1123. u->fifosize = 32;
  1124. tup->cdata = cdata;
  1125. platform_set_drvdata(pdev, tup);
  1126. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1127. if (!resource) {
  1128. dev_err(&pdev->dev, "No IO memory resource\n");
  1129. return -ENODEV;
  1130. }
  1131. u->mapbase = resource->start;
  1132. u->membase = devm_ioremap_resource(&pdev->dev, resource);
  1133. if (IS_ERR(u->membase))
  1134. return PTR_ERR(u->membase);
  1135. tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
  1136. if (IS_ERR(tup->uart_clk)) {
  1137. dev_err(&pdev->dev, "Couldn't get the clock\n");
  1138. return PTR_ERR(tup->uart_clk);
  1139. }
  1140. u->iotype = UPIO_MEM32;
  1141. u->irq = platform_get_irq(pdev, 0);
  1142. u->regshift = 2;
  1143. ret = uart_add_one_port(&tegra_uart_driver, u);
  1144. if (ret < 0) {
  1145. dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
  1146. return ret;
  1147. }
  1148. return ret;
  1149. }
  1150. static int tegra_uart_remove(struct platform_device *pdev)
  1151. {
  1152. struct tegra_uart_port *tup = platform_get_drvdata(pdev);
  1153. struct uart_port *u = &tup->uport;
  1154. uart_remove_one_port(&tegra_uart_driver, u);
  1155. return 0;
  1156. }
  1157. #ifdef CONFIG_PM_SLEEP
  1158. static int tegra_uart_suspend(struct device *dev)
  1159. {
  1160. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1161. struct uart_port *u = &tup->uport;
  1162. return uart_suspend_port(&tegra_uart_driver, u);
  1163. }
  1164. static int tegra_uart_resume(struct device *dev)
  1165. {
  1166. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1167. struct uart_port *u = &tup->uport;
  1168. return uart_resume_port(&tegra_uart_driver, u);
  1169. }
  1170. #endif
  1171. static const struct dev_pm_ops tegra_uart_pm_ops = {
  1172. SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
  1173. };
  1174. static struct platform_driver tegra_uart_platform_driver = {
  1175. .probe = tegra_uart_probe,
  1176. .remove = tegra_uart_remove,
  1177. .driver = {
  1178. .name = "serial-tegra",
  1179. .of_match_table = tegra_uart_of_match,
  1180. .pm = &tegra_uart_pm_ops,
  1181. },
  1182. };
  1183. static int __init tegra_uart_init(void)
  1184. {
  1185. int ret;
  1186. ret = uart_register_driver(&tegra_uart_driver);
  1187. if (ret < 0) {
  1188. pr_err("Could not register %s driver\n",
  1189. tegra_uart_driver.driver_name);
  1190. return ret;
  1191. }
  1192. ret = platform_driver_register(&tegra_uart_platform_driver);
  1193. if (ret < 0) {
  1194. pr_err("Uart platform driver register failed, e = %d\n", ret);
  1195. uart_unregister_driver(&tegra_uart_driver);
  1196. return ret;
  1197. }
  1198. return 0;
  1199. }
  1200. static void __exit tegra_uart_exit(void)
  1201. {
  1202. pr_info("Unloading tegra uart driver\n");
  1203. platform_driver_unregister(&tegra_uart_platform_driver);
  1204. uart_unregister_driver(&tegra_uart_driver);
  1205. }
  1206. module_init(tegra_uart_init);
  1207. module_exit(tegra_uart_exit);
  1208. MODULE_ALIAS("platform:serial-tegra");
  1209. MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
  1210. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1211. MODULE_LICENSE("GPL v2");