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/reset.h>
  37. #include <linux/serial.h>
  38. #include <linux/serial_8250.h>
  39. #include <linux/serial_core.h>
  40. #include <linux/serial_reg.h>
  41. #include <linux/slab.h>
  42. #include <linux/string.h>
  43. #include <linux/termios.h>
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.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. struct reset_control *rst;
  95. unsigned int current_baud;
  96. /* Register shadow */
  97. unsigned long fcr_shadow;
  98. unsigned long mcr_shadow;
  99. unsigned long lcr_shadow;
  100. unsigned long ier_shadow;
  101. bool rts_active;
  102. int tx_in_progress;
  103. unsigned int tx_bytes;
  104. bool enable_modem_interrupt;
  105. bool rx_timeout;
  106. int rx_in_progress;
  107. int symb_bit;
  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. if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
  418. return;
  419. dmaengine_terminate_all(tup->tx_dma_chan);
  420. dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
  421. count = tup->tx_bytes_requested - state.residue;
  422. async_tx_ack(tup->tx_dma_desc);
  423. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  424. tup->tx_in_progress = 0;
  425. return;
  426. }
  427. static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
  428. {
  429. struct circ_buf *xmit = &tup->uport.state->xmit;
  430. tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
  431. tup->tx_in_progress = 0;
  432. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  433. uart_write_wakeup(&tup->uport);
  434. tegra_uart_start_next_tx(tup);
  435. return;
  436. }
  437. static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
  438. struct tty_port *tty)
  439. {
  440. do {
  441. char flag = TTY_NORMAL;
  442. unsigned long lsr = 0;
  443. unsigned char ch;
  444. lsr = tegra_uart_read(tup, UART_LSR);
  445. if (!(lsr & UART_LSR_DR))
  446. break;
  447. flag = tegra_uart_decode_rx_error(tup, lsr);
  448. ch = (unsigned char) tegra_uart_read(tup, UART_RX);
  449. tup->uport.icount.rx++;
  450. if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
  451. tty_insert_flip_char(tty, ch, flag);
  452. } while (1);
  453. return;
  454. }
  455. static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
  456. struct tty_port *tty, int count)
  457. {
  458. int copied;
  459. tup->uport.icount.rx += count;
  460. if (!tty) {
  461. dev_err(tup->uport.dev, "No tty port\n");
  462. return;
  463. }
  464. dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
  465. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  466. copied = tty_insert_flip_string(tty,
  467. ((unsigned char *)(tup->rx_dma_buf_virt)), count);
  468. if (copied != count) {
  469. WARN_ON(1);
  470. dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
  471. }
  472. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  473. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
  474. }
  475. static void tegra_uart_rx_dma_complete(void *args)
  476. {
  477. struct tegra_uart_port *tup = args;
  478. struct uart_port *u = &tup->uport;
  479. int count = tup->rx_bytes_requested;
  480. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  481. struct tty_port *port = &u->state->port;
  482. unsigned long flags;
  483. async_tx_ack(tup->rx_dma_desc);
  484. spin_lock_irqsave(&u->lock, flags);
  485. /* Deactivate flow control to stop sender */
  486. if (tup->rts_active)
  487. set_rts(tup, false);
  488. /* If we are here, DMA is stopped */
  489. if (count)
  490. tegra_uart_copy_rx_to_tty(tup, port, count);
  491. tegra_uart_handle_rx_pio(tup, port);
  492. if (tty) {
  493. spin_unlock_irqrestore(&u->lock, flags);
  494. tty_flip_buffer_push(port);
  495. spin_lock_irqsave(&u->lock, flags);
  496. tty_kref_put(tty);
  497. }
  498. tegra_uart_start_rx_dma(tup);
  499. /* Activate flow control to start transfer */
  500. if (tup->rts_active)
  501. set_rts(tup, true);
  502. spin_unlock_irqrestore(&u->lock, flags);
  503. }
  504. static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup,
  505. unsigned long *flags)
  506. {
  507. struct dma_tx_state state;
  508. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  509. struct tty_port *port = &tup->uport.state->port;
  510. struct uart_port *u = &tup->uport;
  511. int count;
  512. /* Deactivate flow control to stop sender */
  513. if (tup->rts_active)
  514. set_rts(tup, false);
  515. dmaengine_terminate_all(tup->rx_dma_chan);
  516. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  517. async_tx_ack(tup->rx_dma_desc);
  518. count = tup->rx_bytes_requested - state.residue;
  519. /* If we are here, DMA is stopped */
  520. if (count)
  521. tegra_uart_copy_rx_to_tty(tup, port, count);
  522. tegra_uart_handle_rx_pio(tup, port);
  523. if (tty) {
  524. spin_unlock_irqrestore(&u->lock, *flags);
  525. tty_flip_buffer_push(port);
  526. spin_lock_irqsave(&u->lock, *flags);
  527. tty_kref_put(tty);
  528. }
  529. tegra_uart_start_rx_dma(tup);
  530. if (tup->rts_active)
  531. set_rts(tup, true);
  532. }
  533. static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
  534. {
  535. unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
  536. tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
  537. tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
  538. DMA_PREP_INTERRUPT);
  539. if (!tup->rx_dma_desc) {
  540. dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
  541. return -EIO;
  542. }
  543. tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
  544. tup->rx_dma_desc->callback_param = tup;
  545. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  546. count, DMA_TO_DEVICE);
  547. tup->rx_bytes_requested = count;
  548. tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
  549. dma_async_issue_pending(tup->rx_dma_chan);
  550. return 0;
  551. }
  552. static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
  553. {
  554. struct tegra_uart_port *tup = to_tegra_uport(u);
  555. unsigned long msr;
  556. msr = tegra_uart_read(tup, UART_MSR);
  557. if (!(msr & UART_MSR_ANY_DELTA))
  558. return;
  559. if (msr & UART_MSR_TERI)
  560. tup->uport.icount.rng++;
  561. if (msr & UART_MSR_DDSR)
  562. tup->uport.icount.dsr++;
  563. /* We may only get DDCD when HW init and reset */
  564. if (msr & UART_MSR_DDCD)
  565. uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
  566. /* Will start/stop_tx accordingly */
  567. if (msr & UART_MSR_DCTS)
  568. uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
  569. return;
  570. }
  571. static irqreturn_t tegra_uart_isr(int irq, void *data)
  572. {
  573. struct tegra_uart_port *tup = data;
  574. struct uart_port *u = &tup->uport;
  575. unsigned long iir;
  576. unsigned long ier;
  577. bool is_rx_int = false;
  578. unsigned long flags;
  579. spin_lock_irqsave(&u->lock, flags);
  580. while (1) {
  581. iir = tegra_uart_read(tup, UART_IIR);
  582. if (iir & UART_IIR_NO_INT) {
  583. if (is_rx_int) {
  584. tegra_uart_handle_rx_dma(tup, &flags);
  585. if (tup->rx_in_progress) {
  586. ier = tup->ier_shadow;
  587. ier |= (UART_IER_RLSI | UART_IER_RTOIE |
  588. TEGRA_UART_IER_EORD);
  589. tup->ier_shadow = ier;
  590. tegra_uart_write(tup, ier, UART_IER);
  591. }
  592. }
  593. spin_unlock_irqrestore(&u->lock, flags);
  594. return IRQ_HANDLED;
  595. }
  596. switch ((iir >> 1) & 0x7) {
  597. case 0: /* Modem signal change interrupt */
  598. tegra_uart_handle_modem_signal_change(u);
  599. break;
  600. case 1: /* Transmit interrupt only triggered when using PIO */
  601. tup->ier_shadow &= ~UART_IER_THRI;
  602. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  603. tegra_uart_handle_tx_pio(tup);
  604. break;
  605. case 4: /* End of data */
  606. case 6: /* Rx timeout */
  607. case 2: /* Receive */
  608. if (!is_rx_int) {
  609. is_rx_int = true;
  610. /* Disable Rx interrupts */
  611. ier = tup->ier_shadow;
  612. ier |= UART_IER_RDI;
  613. tegra_uart_write(tup, ier, UART_IER);
  614. ier &= ~(UART_IER_RDI | UART_IER_RLSI |
  615. UART_IER_RTOIE | TEGRA_UART_IER_EORD);
  616. tup->ier_shadow = ier;
  617. tegra_uart_write(tup, ier, UART_IER);
  618. }
  619. break;
  620. case 3: /* Receive error */
  621. tegra_uart_decode_rx_error(tup,
  622. tegra_uart_read(tup, UART_LSR));
  623. break;
  624. case 5: /* break nothing to handle */
  625. case 7: /* break nothing to handle */
  626. break;
  627. }
  628. }
  629. }
  630. static void tegra_uart_stop_rx(struct uart_port *u)
  631. {
  632. struct tegra_uart_port *tup = to_tegra_uport(u);
  633. struct tty_struct *tty;
  634. struct tty_port *port = &u->state->port;
  635. struct dma_tx_state state;
  636. unsigned long ier;
  637. int count;
  638. if (tup->rts_active)
  639. set_rts(tup, false);
  640. if (!tup->rx_in_progress)
  641. return;
  642. tty = tty_port_tty_get(&tup->uport.state->port);
  643. tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
  644. ier = tup->ier_shadow;
  645. ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
  646. TEGRA_UART_IER_EORD);
  647. tup->ier_shadow = ier;
  648. tegra_uart_write(tup, ier, UART_IER);
  649. tup->rx_in_progress = 0;
  650. if (tup->rx_dma_chan) {
  651. dmaengine_terminate_all(tup->rx_dma_chan);
  652. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  653. async_tx_ack(tup->rx_dma_desc);
  654. count = tup->rx_bytes_requested - state.residue;
  655. tegra_uart_copy_rx_to_tty(tup, port, count);
  656. tegra_uart_handle_rx_pio(tup, port);
  657. } else {
  658. tegra_uart_handle_rx_pio(tup, port);
  659. }
  660. if (tty) {
  661. tty_flip_buffer_push(port);
  662. tty_kref_put(tty);
  663. }
  664. return;
  665. }
  666. static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
  667. {
  668. unsigned long flags;
  669. unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
  670. unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
  671. unsigned long wait_time;
  672. unsigned long lsr;
  673. unsigned long msr;
  674. unsigned long mcr;
  675. /* Disable interrupts */
  676. tegra_uart_write(tup, 0, UART_IER);
  677. lsr = tegra_uart_read(tup, UART_LSR);
  678. if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  679. msr = tegra_uart_read(tup, UART_MSR);
  680. mcr = tegra_uart_read(tup, UART_MCR);
  681. if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
  682. dev_err(tup->uport.dev,
  683. "Tx Fifo not empty, CTS disabled, waiting\n");
  684. /* Wait for Tx fifo to be empty */
  685. while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  686. wait_time = min(fifo_empty_time, 100lu);
  687. udelay(wait_time);
  688. fifo_empty_time -= wait_time;
  689. if (!fifo_empty_time) {
  690. msr = tegra_uart_read(tup, UART_MSR);
  691. mcr = tegra_uart_read(tup, UART_MCR);
  692. if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
  693. (msr & UART_MSR_CTS))
  694. dev_err(tup->uport.dev,
  695. "Slave not ready\n");
  696. break;
  697. }
  698. lsr = tegra_uart_read(tup, UART_LSR);
  699. }
  700. }
  701. spin_lock_irqsave(&tup->uport.lock, flags);
  702. /* Reset the Rx and Tx FIFOs */
  703. tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
  704. tup->current_baud = 0;
  705. spin_unlock_irqrestore(&tup->uport.lock, flags);
  706. clk_disable_unprepare(tup->uart_clk);
  707. }
  708. static int tegra_uart_hw_init(struct tegra_uart_port *tup)
  709. {
  710. int ret;
  711. tup->fcr_shadow = 0;
  712. tup->mcr_shadow = 0;
  713. tup->lcr_shadow = 0;
  714. tup->ier_shadow = 0;
  715. tup->current_baud = 0;
  716. clk_prepare_enable(tup->uart_clk);
  717. /* Reset the UART controller to clear all previous status.*/
  718. reset_control_assert(tup->rst);
  719. udelay(10);
  720. reset_control_deassert(tup->rst);
  721. tup->rx_in_progress = 0;
  722. tup->tx_in_progress = 0;
  723. /*
  724. * Set the trigger level
  725. *
  726. * For PIO mode:
  727. *
  728. * For receive, this will interrupt the CPU after that many number of
  729. * bytes are received, for the remaining bytes the receive timeout
  730. * interrupt is received. Rx high watermark is set to 4.
  731. *
  732. * For transmit, if the trasnmit interrupt is enabled, this will
  733. * interrupt the CPU when the number of entries in the FIFO reaches the
  734. * low watermark. Tx low watermark is set to 16 bytes.
  735. *
  736. * For DMA mode:
  737. *
  738. * Set the Tx trigger to 16. This should match the DMA burst size that
  739. * programmed in the DMA registers.
  740. */
  741. tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
  742. tup->fcr_shadow |= UART_FCR_R_TRIG_01;
  743. tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
  744. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  745. /*
  746. * Initialize the UART with default configuration
  747. * (115200, N, 8, 1) so that the receive DMA buffer may be
  748. * enqueued
  749. */
  750. tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
  751. tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
  752. tup->fcr_shadow |= UART_FCR_DMA_SELECT;
  753. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  754. ret = tegra_uart_start_rx_dma(tup);
  755. if (ret < 0) {
  756. dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
  757. return ret;
  758. }
  759. tup->rx_in_progress = 1;
  760. /*
  761. * Enable IE_RXS for the receive status interrupts like line errros.
  762. * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
  763. *
  764. * If using DMA mode, enable EORD instead of receive interrupt which
  765. * will interrupt after the UART is done with the receive instead of
  766. * the interrupt when the FIFO "threshold" is reached.
  767. *
  768. * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
  769. * the DATA is sitting in the FIFO and couldn't be transferred to the
  770. * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
  771. * triggered when there is a pause of the incomming data stream for 4
  772. * characters long.
  773. *
  774. * For pauses in the data which is not aligned to 4 bytes, we get
  775. * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
  776. * then the EORD.
  777. */
  778. tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
  779. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  780. return 0;
  781. }
  782. static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
  783. bool dma_to_memory)
  784. {
  785. struct dma_chan *dma_chan;
  786. unsigned char *dma_buf;
  787. dma_addr_t dma_phys;
  788. int ret;
  789. struct dma_slave_config dma_sconfig;
  790. dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
  791. dma_to_memory ? "rx" : "tx");
  792. if (IS_ERR(dma_chan)) {
  793. ret = PTR_ERR(dma_chan);
  794. dev_err(tup->uport.dev,
  795. "DMA channel alloc failed: %d\n", ret);
  796. return ret;
  797. }
  798. if (dma_to_memory) {
  799. dma_buf = dma_alloc_coherent(tup->uport.dev,
  800. TEGRA_UART_RX_DMA_BUFFER_SIZE,
  801. &dma_phys, GFP_KERNEL);
  802. if (!dma_buf) {
  803. dev_err(tup->uport.dev,
  804. "Not able to allocate the dma buffer\n");
  805. dma_release_channel(dma_chan);
  806. return -ENOMEM;
  807. }
  808. } else {
  809. dma_phys = dma_map_single(tup->uport.dev,
  810. tup->uport.state->xmit.buf, UART_XMIT_SIZE,
  811. DMA_TO_DEVICE);
  812. dma_buf = tup->uport.state->xmit.buf;
  813. }
  814. if (dma_to_memory) {
  815. dma_sconfig.src_addr = tup->uport.mapbase;
  816. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  817. dma_sconfig.src_maxburst = 4;
  818. } else {
  819. dma_sconfig.dst_addr = tup->uport.mapbase;
  820. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  821. dma_sconfig.dst_maxburst = 16;
  822. }
  823. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  824. if (ret < 0) {
  825. dev_err(tup->uport.dev,
  826. "Dma slave config failed, err = %d\n", ret);
  827. goto scrub;
  828. }
  829. if (dma_to_memory) {
  830. tup->rx_dma_chan = dma_chan;
  831. tup->rx_dma_buf_virt = dma_buf;
  832. tup->rx_dma_buf_phys = dma_phys;
  833. } else {
  834. tup->tx_dma_chan = dma_chan;
  835. tup->tx_dma_buf_virt = dma_buf;
  836. tup->tx_dma_buf_phys = dma_phys;
  837. }
  838. return 0;
  839. scrub:
  840. dma_release_channel(dma_chan);
  841. return ret;
  842. }
  843. static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
  844. bool dma_to_memory)
  845. {
  846. struct dma_chan *dma_chan;
  847. if (dma_to_memory) {
  848. dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
  849. tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
  850. dma_chan = tup->rx_dma_chan;
  851. tup->rx_dma_chan = NULL;
  852. tup->rx_dma_buf_phys = 0;
  853. tup->rx_dma_buf_virt = NULL;
  854. } else {
  855. dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
  856. UART_XMIT_SIZE, DMA_TO_DEVICE);
  857. dma_chan = tup->tx_dma_chan;
  858. tup->tx_dma_chan = NULL;
  859. tup->tx_dma_buf_phys = 0;
  860. tup->tx_dma_buf_virt = NULL;
  861. }
  862. dma_release_channel(dma_chan);
  863. }
  864. static int tegra_uart_startup(struct uart_port *u)
  865. {
  866. struct tegra_uart_port *tup = to_tegra_uport(u);
  867. int ret;
  868. ret = tegra_uart_dma_channel_allocate(tup, false);
  869. if (ret < 0) {
  870. dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
  871. return ret;
  872. }
  873. ret = tegra_uart_dma_channel_allocate(tup, true);
  874. if (ret < 0) {
  875. dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
  876. goto fail_rx_dma;
  877. }
  878. ret = tegra_uart_hw_init(tup);
  879. if (ret < 0) {
  880. dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
  881. goto fail_hw_init;
  882. }
  883. ret = request_irq(u->irq, tegra_uart_isr, 0,
  884. dev_name(u->dev), tup);
  885. if (ret < 0) {
  886. dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
  887. goto fail_hw_init;
  888. }
  889. return 0;
  890. fail_hw_init:
  891. tegra_uart_dma_channel_free(tup, true);
  892. fail_rx_dma:
  893. tegra_uart_dma_channel_free(tup, false);
  894. return ret;
  895. }
  896. /*
  897. * Flush any TX data submitted for DMA and PIO. Called when the
  898. * TX circular buffer is reset.
  899. */
  900. static void tegra_uart_flush_buffer(struct uart_port *u)
  901. {
  902. struct tegra_uart_port *tup = to_tegra_uport(u);
  903. tup->tx_bytes = 0;
  904. if (tup->tx_dma_chan)
  905. dmaengine_terminate_all(tup->tx_dma_chan);
  906. return;
  907. }
  908. static void tegra_uart_shutdown(struct uart_port *u)
  909. {
  910. struct tegra_uart_port *tup = to_tegra_uport(u);
  911. tegra_uart_hw_deinit(tup);
  912. tup->rx_in_progress = 0;
  913. tup->tx_in_progress = 0;
  914. tegra_uart_dma_channel_free(tup, true);
  915. tegra_uart_dma_channel_free(tup, false);
  916. free_irq(u->irq, tup);
  917. tegra_uart_flush_buffer(u);
  918. }
  919. static void tegra_uart_enable_ms(struct uart_port *u)
  920. {
  921. struct tegra_uart_port *tup = to_tegra_uport(u);
  922. if (tup->enable_modem_interrupt) {
  923. tup->ier_shadow |= UART_IER_MSI;
  924. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  925. }
  926. }
  927. static void tegra_uart_set_termios(struct uart_port *u,
  928. struct ktermios *termios, struct ktermios *oldtermios)
  929. {
  930. struct tegra_uart_port *tup = to_tegra_uport(u);
  931. unsigned int baud;
  932. unsigned long flags;
  933. unsigned int lcr;
  934. int symb_bit = 1;
  935. struct clk *parent_clk = clk_get_parent(tup->uart_clk);
  936. unsigned long parent_clk_rate = clk_get_rate(parent_clk);
  937. int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
  938. max_divider *= 16;
  939. spin_lock_irqsave(&u->lock, flags);
  940. /* Changing configuration, it is safe to stop any rx now */
  941. if (tup->rts_active)
  942. set_rts(tup, false);
  943. /* Clear all interrupts as configuration is going to be change */
  944. tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
  945. tegra_uart_read(tup, UART_IER);
  946. tegra_uart_write(tup, 0, UART_IER);
  947. tegra_uart_read(tup, UART_IER);
  948. /* Parity */
  949. lcr = tup->lcr_shadow;
  950. lcr &= ~UART_LCR_PARITY;
  951. /* CMSPAR isn't supported by this driver */
  952. termios->c_cflag &= ~CMSPAR;
  953. if ((termios->c_cflag & PARENB) == PARENB) {
  954. symb_bit++;
  955. if (termios->c_cflag & PARODD) {
  956. lcr |= UART_LCR_PARITY;
  957. lcr &= ~UART_LCR_EPAR;
  958. lcr &= ~UART_LCR_SPAR;
  959. } else {
  960. lcr |= UART_LCR_PARITY;
  961. lcr |= UART_LCR_EPAR;
  962. lcr &= ~UART_LCR_SPAR;
  963. }
  964. }
  965. lcr &= ~UART_LCR_WLEN8;
  966. switch (termios->c_cflag & CSIZE) {
  967. case CS5:
  968. lcr |= UART_LCR_WLEN5;
  969. symb_bit += 5;
  970. break;
  971. case CS6:
  972. lcr |= UART_LCR_WLEN6;
  973. symb_bit += 6;
  974. break;
  975. case CS7:
  976. lcr |= UART_LCR_WLEN7;
  977. symb_bit += 7;
  978. break;
  979. default:
  980. lcr |= UART_LCR_WLEN8;
  981. symb_bit += 8;
  982. break;
  983. }
  984. /* Stop bits */
  985. if (termios->c_cflag & CSTOPB) {
  986. lcr |= UART_LCR_STOP;
  987. symb_bit += 2;
  988. } else {
  989. lcr &= ~UART_LCR_STOP;
  990. symb_bit++;
  991. }
  992. tegra_uart_write(tup, lcr, UART_LCR);
  993. tup->lcr_shadow = lcr;
  994. tup->symb_bit = symb_bit;
  995. /* Baud rate. */
  996. baud = uart_get_baud_rate(u, termios, oldtermios,
  997. parent_clk_rate/max_divider,
  998. parent_clk_rate/16);
  999. spin_unlock_irqrestore(&u->lock, flags);
  1000. tegra_set_baudrate(tup, baud);
  1001. if (tty_termios_baud_rate(termios))
  1002. tty_termios_encode_baud_rate(termios, baud, baud);
  1003. spin_lock_irqsave(&u->lock, flags);
  1004. /* Flow control */
  1005. if (termios->c_cflag & CRTSCTS) {
  1006. tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
  1007. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1008. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1009. /* if top layer has asked to set rts active then do so here */
  1010. if (tup->rts_active)
  1011. set_rts(tup, true);
  1012. } else {
  1013. tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
  1014. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1015. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1016. }
  1017. /* update the port timeout based on new settings */
  1018. uart_update_timeout(u, termios->c_cflag, baud);
  1019. /* Make sure all write has completed */
  1020. tegra_uart_read(tup, UART_IER);
  1021. /* Reenable interrupt */
  1022. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  1023. tegra_uart_read(tup, UART_IER);
  1024. spin_unlock_irqrestore(&u->lock, flags);
  1025. return;
  1026. }
  1027. static const char *tegra_uart_type(struct uart_port *u)
  1028. {
  1029. return TEGRA_UART_TYPE;
  1030. }
  1031. static struct uart_ops tegra_uart_ops = {
  1032. .tx_empty = tegra_uart_tx_empty,
  1033. .set_mctrl = tegra_uart_set_mctrl,
  1034. .get_mctrl = tegra_uart_get_mctrl,
  1035. .stop_tx = tegra_uart_stop_tx,
  1036. .start_tx = tegra_uart_start_tx,
  1037. .stop_rx = tegra_uart_stop_rx,
  1038. .flush_buffer = tegra_uart_flush_buffer,
  1039. .enable_ms = tegra_uart_enable_ms,
  1040. .break_ctl = tegra_uart_break_ctl,
  1041. .startup = tegra_uart_startup,
  1042. .shutdown = tegra_uart_shutdown,
  1043. .set_termios = tegra_uart_set_termios,
  1044. .type = tegra_uart_type,
  1045. .request_port = tegra_uart_request_port,
  1046. .release_port = tegra_uart_release_port,
  1047. };
  1048. static struct uart_driver tegra_uart_driver = {
  1049. .owner = THIS_MODULE,
  1050. .driver_name = "tegra_hsuart",
  1051. .dev_name = "ttyTHS",
  1052. .cons = NULL,
  1053. .nr = TEGRA_UART_MAXIMUM,
  1054. };
  1055. static int tegra_uart_parse_dt(struct platform_device *pdev,
  1056. struct tegra_uart_port *tup)
  1057. {
  1058. struct device_node *np = pdev->dev.of_node;
  1059. int port;
  1060. port = of_alias_get_id(np, "serial");
  1061. if (port < 0) {
  1062. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
  1063. return port;
  1064. }
  1065. tup->uport.line = port;
  1066. tup->enable_modem_interrupt = of_property_read_bool(np,
  1067. "nvidia,enable-modem-interrupt");
  1068. return 0;
  1069. }
  1070. static struct tegra_uart_chip_data tegra20_uart_chip_data = {
  1071. .tx_fifo_full_status = false,
  1072. .allow_txfifo_reset_fifo_mode = true,
  1073. .support_clk_src_div = false,
  1074. };
  1075. static struct tegra_uart_chip_data tegra30_uart_chip_data = {
  1076. .tx_fifo_full_status = true,
  1077. .allow_txfifo_reset_fifo_mode = false,
  1078. .support_clk_src_div = true,
  1079. };
  1080. static const struct of_device_id tegra_uart_of_match[] = {
  1081. {
  1082. .compatible = "nvidia,tegra30-hsuart",
  1083. .data = &tegra30_uart_chip_data,
  1084. }, {
  1085. .compatible = "nvidia,tegra20-hsuart",
  1086. .data = &tegra20_uart_chip_data,
  1087. }, {
  1088. },
  1089. };
  1090. MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
  1091. static int tegra_uart_probe(struct platform_device *pdev)
  1092. {
  1093. struct tegra_uart_port *tup;
  1094. struct uart_port *u;
  1095. struct resource *resource;
  1096. int ret;
  1097. const struct tegra_uart_chip_data *cdata;
  1098. const struct of_device_id *match;
  1099. match = of_match_device(tegra_uart_of_match, &pdev->dev);
  1100. if (!match) {
  1101. dev_err(&pdev->dev, "Error: No device match found\n");
  1102. return -ENODEV;
  1103. }
  1104. cdata = match->data;
  1105. tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
  1106. if (!tup) {
  1107. dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
  1108. return -ENOMEM;
  1109. }
  1110. ret = tegra_uart_parse_dt(pdev, tup);
  1111. if (ret < 0)
  1112. return ret;
  1113. u = &tup->uport;
  1114. u->dev = &pdev->dev;
  1115. u->ops = &tegra_uart_ops;
  1116. u->type = PORT_TEGRA;
  1117. u->fifosize = 32;
  1118. tup->cdata = cdata;
  1119. platform_set_drvdata(pdev, tup);
  1120. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1121. if (!resource) {
  1122. dev_err(&pdev->dev, "No IO memory resource\n");
  1123. return -ENODEV;
  1124. }
  1125. u->mapbase = resource->start;
  1126. u->membase = devm_ioremap_resource(&pdev->dev, resource);
  1127. if (IS_ERR(u->membase))
  1128. return PTR_ERR(u->membase);
  1129. tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
  1130. if (IS_ERR(tup->uart_clk)) {
  1131. dev_err(&pdev->dev, "Couldn't get the clock\n");
  1132. return PTR_ERR(tup->uart_clk);
  1133. }
  1134. tup->rst = devm_reset_control_get(&pdev->dev, "serial");
  1135. if (IS_ERR(tup->rst)) {
  1136. dev_err(&pdev->dev, "Couldn't get the reset\n");
  1137. return PTR_ERR(tup->rst);
  1138. }
  1139. u->iotype = UPIO_MEM32;
  1140. u->irq = platform_get_irq(pdev, 0);
  1141. u->regshift = 2;
  1142. ret = uart_add_one_port(&tegra_uart_driver, u);
  1143. if (ret < 0) {
  1144. dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
  1145. return ret;
  1146. }
  1147. return ret;
  1148. }
  1149. static int tegra_uart_remove(struct platform_device *pdev)
  1150. {
  1151. struct tegra_uart_port *tup = platform_get_drvdata(pdev);
  1152. struct uart_port *u = &tup->uport;
  1153. uart_remove_one_port(&tegra_uart_driver, u);
  1154. return 0;
  1155. }
  1156. #ifdef CONFIG_PM_SLEEP
  1157. static int tegra_uart_suspend(struct device *dev)
  1158. {
  1159. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1160. struct uart_port *u = &tup->uport;
  1161. return uart_suspend_port(&tegra_uart_driver, u);
  1162. }
  1163. static int tegra_uart_resume(struct device *dev)
  1164. {
  1165. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1166. struct uart_port *u = &tup->uport;
  1167. return uart_resume_port(&tegra_uart_driver, u);
  1168. }
  1169. #endif
  1170. static const struct dev_pm_ops tegra_uart_pm_ops = {
  1171. SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
  1172. };
  1173. static struct platform_driver tegra_uart_platform_driver = {
  1174. .probe = tegra_uart_probe,
  1175. .remove = tegra_uart_remove,
  1176. .driver = {
  1177. .name = "serial-tegra",
  1178. .of_match_table = tegra_uart_of_match,
  1179. .pm = &tegra_uart_pm_ops,
  1180. },
  1181. };
  1182. static int __init tegra_uart_init(void)
  1183. {
  1184. int ret;
  1185. ret = uart_register_driver(&tegra_uart_driver);
  1186. if (ret < 0) {
  1187. pr_err("Could not register %s driver\n",
  1188. tegra_uart_driver.driver_name);
  1189. return ret;
  1190. }
  1191. ret = platform_driver_register(&tegra_uart_platform_driver);
  1192. if (ret < 0) {
  1193. pr_err("Uart platform driver register failed, e = %d\n", ret);
  1194. uart_unregister_driver(&tegra_uart_driver);
  1195. return ret;
  1196. }
  1197. return 0;
  1198. }
  1199. static void __exit tegra_uart_exit(void)
  1200. {
  1201. pr_info("Unloading tegra uart driver\n");
  1202. platform_driver_unregister(&tegra_uart_platform_driver);
  1203. uart_unregister_driver(&tegra_uart_driver);
  1204. }
  1205. module_init(tegra_uart_init);
  1206. module_exit(tegra_uart_exit);
  1207. MODULE_ALIAS("platform:serial-tegra");
  1208. MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
  1209. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1210. MODULE_LICENSE("GPL v2");