serial-tegra.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. unsigned int tx_bytes_requested;
  119. unsigned 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. /**
  206. * tegra_uart_wait_cycle_time: Wait for N UART clock periods
  207. *
  208. * @tup: Tegra serial port data structure.
  209. * @cycles: Number of clock periods to wait.
  210. *
  211. * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
  212. * clock speed is 16X the current baud rate.
  213. */
  214. static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
  215. unsigned int cycles)
  216. {
  217. if (tup->current_baud)
  218. udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
  219. }
  220. /* Wait for a symbol-time. */
  221. static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
  222. unsigned int syms)
  223. {
  224. if (tup->current_baud)
  225. udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
  226. tup->current_baud));
  227. }
  228. static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
  229. {
  230. unsigned long fcr = tup->fcr_shadow;
  231. if (tup->cdata->allow_txfifo_reset_fifo_mode) {
  232. fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  233. tegra_uart_write(tup, fcr, UART_FCR);
  234. } else {
  235. fcr &= ~UART_FCR_ENABLE_FIFO;
  236. tegra_uart_write(tup, fcr, UART_FCR);
  237. udelay(60);
  238. fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  239. tegra_uart_write(tup, fcr, UART_FCR);
  240. fcr |= UART_FCR_ENABLE_FIFO;
  241. tegra_uart_write(tup, fcr, UART_FCR);
  242. }
  243. /* Dummy read to ensure the write is posted */
  244. tegra_uart_read(tup, UART_SCR);
  245. /*
  246. * For all tegra devices (up to t210), there is a hardware issue that
  247. * requires software to wait for 32 UART clock periods for the flush
  248. * to propagate, otherwise data could be lost.
  249. */
  250. tegra_uart_wait_cycle_time(tup, 32);
  251. }
  252. static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
  253. {
  254. unsigned long rate;
  255. unsigned int divisor;
  256. unsigned long lcr;
  257. int ret;
  258. if (tup->current_baud == baud)
  259. return 0;
  260. if (tup->cdata->support_clk_src_div) {
  261. rate = baud * 16;
  262. ret = clk_set_rate(tup->uart_clk, rate);
  263. if (ret < 0) {
  264. dev_err(tup->uport.dev,
  265. "clk_set_rate() failed for rate %lu\n", rate);
  266. return ret;
  267. }
  268. divisor = 1;
  269. } else {
  270. rate = clk_get_rate(tup->uart_clk);
  271. divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
  272. }
  273. lcr = tup->lcr_shadow;
  274. lcr |= UART_LCR_DLAB;
  275. tegra_uart_write(tup, lcr, UART_LCR);
  276. tegra_uart_write(tup, divisor & 0xFF, UART_TX);
  277. tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
  278. lcr &= ~UART_LCR_DLAB;
  279. tegra_uart_write(tup, lcr, UART_LCR);
  280. /* Dummy read to ensure the write is posted */
  281. tegra_uart_read(tup, UART_SCR);
  282. tup->current_baud = baud;
  283. /* wait two character intervals at new rate */
  284. tegra_uart_wait_sym_time(tup, 2);
  285. return 0;
  286. }
  287. static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
  288. unsigned long lsr)
  289. {
  290. char flag = TTY_NORMAL;
  291. if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
  292. if (lsr & UART_LSR_OE) {
  293. /* Overrrun error */
  294. flag = TTY_OVERRUN;
  295. tup->uport.icount.overrun++;
  296. dev_err(tup->uport.dev, "Got overrun errors\n");
  297. } else if (lsr & UART_LSR_PE) {
  298. /* Parity error */
  299. flag = TTY_PARITY;
  300. tup->uport.icount.parity++;
  301. dev_err(tup->uport.dev, "Got Parity errors\n");
  302. } else if (lsr & UART_LSR_FE) {
  303. flag = TTY_FRAME;
  304. tup->uport.icount.frame++;
  305. dev_err(tup->uport.dev, "Got frame errors\n");
  306. } else if (lsr & UART_LSR_BI) {
  307. dev_err(tup->uport.dev, "Got Break\n");
  308. tup->uport.icount.brk++;
  309. /* If FIFO read error without any data, reset Rx FIFO */
  310. if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
  311. tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
  312. }
  313. }
  314. return flag;
  315. }
  316. static int tegra_uart_request_port(struct uart_port *u)
  317. {
  318. return 0;
  319. }
  320. static void tegra_uart_release_port(struct uart_port *u)
  321. {
  322. /* Nothing to do here */
  323. }
  324. static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
  325. {
  326. struct circ_buf *xmit = &tup->uport.state->xmit;
  327. int i;
  328. for (i = 0; i < max_bytes; i++) {
  329. BUG_ON(uart_circ_empty(xmit));
  330. if (tup->cdata->tx_fifo_full_status) {
  331. unsigned long lsr = tegra_uart_read(tup, UART_LSR);
  332. if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
  333. break;
  334. }
  335. tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
  336. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  337. tup->uport.icount.tx++;
  338. }
  339. }
  340. static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
  341. unsigned int bytes)
  342. {
  343. if (bytes > TEGRA_UART_MIN_DMA)
  344. bytes = TEGRA_UART_MIN_DMA;
  345. tup->tx_in_progress = TEGRA_UART_TX_PIO;
  346. tup->tx_bytes = bytes;
  347. tup->ier_shadow |= UART_IER_THRI;
  348. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  349. }
  350. static void tegra_uart_tx_dma_complete(void *args)
  351. {
  352. struct tegra_uart_port *tup = args;
  353. struct circ_buf *xmit = &tup->uport.state->xmit;
  354. struct dma_tx_state state;
  355. unsigned long flags;
  356. unsigned int count;
  357. dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
  358. count = tup->tx_bytes_requested - state.residue;
  359. async_tx_ack(tup->tx_dma_desc);
  360. spin_lock_irqsave(&tup->uport.lock, flags);
  361. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  362. tup->tx_in_progress = 0;
  363. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  364. uart_write_wakeup(&tup->uport);
  365. tegra_uart_start_next_tx(tup);
  366. spin_unlock_irqrestore(&tup->uport.lock, flags);
  367. }
  368. static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
  369. unsigned long count)
  370. {
  371. struct circ_buf *xmit = &tup->uport.state->xmit;
  372. dma_addr_t tx_phys_addr;
  373. dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
  374. UART_XMIT_SIZE, DMA_TO_DEVICE);
  375. tup->tx_bytes = count & ~(0xF);
  376. tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
  377. tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
  378. tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
  379. DMA_PREP_INTERRUPT);
  380. if (!tup->tx_dma_desc) {
  381. dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
  382. return -EIO;
  383. }
  384. tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
  385. tup->tx_dma_desc->callback_param = tup;
  386. tup->tx_in_progress = TEGRA_UART_TX_DMA;
  387. tup->tx_bytes_requested = tup->tx_bytes;
  388. tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
  389. dma_async_issue_pending(tup->tx_dma_chan);
  390. return 0;
  391. }
  392. static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
  393. {
  394. unsigned long tail;
  395. unsigned long count;
  396. struct circ_buf *xmit = &tup->uport.state->xmit;
  397. tail = (unsigned long)&xmit->buf[xmit->tail];
  398. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  399. if (!count)
  400. return;
  401. if (count < TEGRA_UART_MIN_DMA)
  402. tegra_uart_start_pio_tx(tup, count);
  403. else if (BYTES_TO_ALIGN(tail) > 0)
  404. tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
  405. else
  406. tegra_uart_start_tx_dma(tup, count);
  407. }
  408. /* Called by serial core driver with u->lock taken. */
  409. static void tegra_uart_start_tx(struct uart_port *u)
  410. {
  411. struct tegra_uart_port *tup = to_tegra_uport(u);
  412. struct circ_buf *xmit = &u->state->xmit;
  413. if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
  414. tegra_uart_start_next_tx(tup);
  415. }
  416. static unsigned int tegra_uart_tx_empty(struct uart_port *u)
  417. {
  418. struct tegra_uart_port *tup = to_tegra_uport(u);
  419. unsigned int ret = 0;
  420. unsigned long flags;
  421. spin_lock_irqsave(&u->lock, flags);
  422. if (!tup->tx_in_progress) {
  423. unsigned long lsr = tegra_uart_read(tup, UART_LSR);
  424. if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
  425. ret = TIOCSER_TEMT;
  426. }
  427. spin_unlock_irqrestore(&u->lock, flags);
  428. return ret;
  429. }
  430. static void tegra_uart_stop_tx(struct uart_port *u)
  431. {
  432. struct tegra_uart_port *tup = to_tegra_uport(u);
  433. struct circ_buf *xmit = &tup->uport.state->xmit;
  434. struct dma_tx_state state;
  435. unsigned int count;
  436. if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
  437. return;
  438. dmaengine_terminate_all(tup->tx_dma_chan);
  439. dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
  440. count = tup->tx_bytes_requested - state.residue;
  441. async_tx_ack(tup->tx_dma_desc);
  442. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  443. tup->tx_in_progress = 0;
  444. return;
  445. }
  446. static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
  447. {
  448. struct circ_buf *xmit = &tup->uport.state->xmit;
  449. tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
  450. tup->tx_in_progress = 0;
  451. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  452. uart_write_wakeup(&tup->uport);
  453. tegra_uart_start_next_tx(tup);
  454. return;
  455. }
  456. static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
  457. struct tty_port *tty)
  458. {
  459. do {
  460. char flag = TTY_NORMAL;
  461. unsigned long lsr = 0;
  462. unsigned char ch;
  463. lsr = tegra_uart_read(tup, UART_LSR);
  464. if (!(lsr & UART_LSR_DR))
  465. break;
  466. flag = tegra_uart_decode_rx_error(tup, lsr);
  467. ch = (unsigned char) tegra_uart_read(tup, UART_RX);
  468. tup->uport.icount.rx++;
  469. if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
  470. tty_insert_flip_char(tty, ch, flag);
  471. } while (1);
  472. return;
  473. }
  474. static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
  475. struct tty_port *tty,
  476. unsigned int count)
  477. {
  478. int copied;
  479. /* If count is zero, then there is no data to be copied */
  480. if (!count)
  481. return;
  482. tup->uport.icount.rx += count;
  483. if (!tty) {
  484. dev_err(tup->uport.dev, "No tty port\n");
  485. return;
  486. }
  487. dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
  488. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  489. copied = tty_insert_flip_string(tty,
  490. ((unsigned char *)(tup->rx_dma_buf_virt)), count);
  491. if (copied != count) {
  492. WARN_ON(1);
  493. dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
  494. }
  495. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  496. TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
  497. }
  498. static void tegra_uart_rx_dma_complete(void *args)
  499. {
  500. struct tegra_uart_port *tup = args;
  501. struct uart_port *u = &tup->uport;
  502. unsigned int count = tup->rx_bytes_requested;
  503. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  504. struct tty_port *port = &u->state->port;
  505. unsigned long flags;
  506. struct dma_tx_state state;
  507. enum dma_status status;
  508. spin_lock_irqsave(&u->lock, flags);
  509. status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  510. if (status == DMA_IN_PROGRESS) {
  511. dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
  512. goto done;
  513. }
  514. async_tx_ack(tup->rx_dma_desc);
  515. /* Deactivate flow control to stop sender */
  516. if (tup->rts_active)
  517. set_rts(tup, false);
  518. /* If we are here, DMA is stopped */
  519. tegra_uart_copy_rx_to_tty(tup, port, count);
  520. tegra_uart_handle_rx_pio(tup, port);
  521. if (tty) {
  522. spin_unlock_irqrestore(&u->lock, flags);
  523. tty_flip_buffer_push(port);
  524. spin_lock_irqsave(&u->lock, flags);
  525. tty_kref_put(tty);
  526. }
  527. tegra_uart_start_rx_dma(tup);
  528. /* Activate flow control to start transfer */
  529. if (tup->rts_active)
  530. set_rts(tup, true);
  531. done:
  532. spin_unlock_irqrestore(&u->lock, flags);
  533. }
  534. static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup,
  535. unsigned long *flags)
  536. {
  537. struct dma_tx_state state;
  538. struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
  539. struct tty_port *port = &tup->uport.state->port;
  540. struct uart_port *u = &tup->uport;
  541. unsigned int count;
  542. /* Deactivate flow control to stop sender */
  543. if (tup->rts_active)
  544. set_rts(tup, false);
  545. dmaengine_terminate_all(tup->rx_dma_chan);
  546. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  547. async_tx_ack(tup->rx_dma_desc);
  548. count = tup->rx_bytes_requested - state.residue;
  549. /* If we are here, DMA is stopped */
  550. tegra_uart_copy_rx_to_tty(tup, port, count);
  551. tegra_uart_handle_rx_pio(tup, port);
  552. if (tty) {
  553. spin_unlock_irqrestore(&u->lock, *flags);
  554. tty_flip_buffer_push(port);
  555. spin_lock_irqsave(&u->lock, *flags);
  556. tty_kref_put(tty);
  557. }
  558. tegra_uart_start_rx_dma(tup);
  559. if (tup->rts_active)
  560. set_rts(tup, true);
  561. }
  562. static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
  563. {
  564. unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
  565. tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
  566. tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
  567. DMA_PREP_INTERRUPT);
  568. if (!tup->rx_dma_desc) {
  569. dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
  570. return -EIO;
  571. }
  572. tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
  573. tup->rx_dma_desc->callback_param = tup;
  574. dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
  575. count, DMA_TO_DEVICE);
  576. tup->rx_bytes_requested = count;
  577. tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
  578. dma_async_issue_pending(tup->rx_dma_chan);
  579. return 0;
  580. }
  581. static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
  582. {
  583. struct tegra_uart_port *tup = to_tegra_uport(u);
  584. unsigned long msr;
  585. msr = tegra_uart_read(tup, UART_MSR);
  586. if (!(msr & UART_MSR_ANY_DELTA))
  587. return;
  588. if (msr & UART_MSR_TERI)
  589. tup->uport.icount.rng++;
  590. if (msr & UART_MSR_DDSR)
  591. tup->uport.icount.dsr++;
  592. /* We may only get DDCD when HW init and reset */
  593. if (msr & UART_MSR_DDCD)
  594. uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
  595. /* Will start/stop_tx accordingly */
  596. if (msr & UART_MSR_DCTS)
  597. uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
  598. return;
  599. }
  600. static irqreturn_t tegra_uart_isr(int irq, void *data)
  601. {
  602. struct tegra_uart_port *tup = data;
  603. struct uart_port *u = &tup->uport;
  604. unsigned long iir;
  605. unsigned long ier;
  606. bool is_rx_int = false;
  607. unsigned long flags;
  608. spin_lock_irqsave(&u->lock, flags);
  609. while (1) {
  610. iir = tegra_uart_read(tup, UART_IIR);
  611. if (iir & UART_IIR_NO_INT) {
  612. if (is_rx_int) {
  613. tegra_uart_handle_rx_dma(tup, &flags);
  614. if (tup->rx_in_progress) {
  615. ier = tup->ier_shadow;
  616. ier |= (UART_IER_RLSI | UART_IER_RTOIE |
  617. TEGRA_UART_IER_EORD);
  618. tup->ier_shadow = ier;
  619. tegra_uart_write(tup, ier, UART_IER);
  620. }
  621. }
  622. spin_unlock_irqrestore(&u->lock, flags);
  623. return IRQ_HANDLED;
  624. }
  625. switch ((iir >> 1) & 0x7) {
  626. case 0: /* Modem signal change interrupt */
  627. tegra_uart_handle_modem_signal_change(u);
  628. break;
  629. case 1: /* Transmit interrupt only triggered when using PIO */
  630. tup->ier_shadow &= ~UART_IER_THRI;
  631. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  632. tegra_uart_handle_tx_pio(tup);
  633. break;
  634. case 4: /* End of data */
  635. case 6: /* Rx timeout */
  636. case 2: /* Receive */
  637. if (!is_rx_int) {
  638. is_rx_int = true;
  639. /* Disable Rx interrupts */
  640. ier = tup->ier_shadow;
  641. ier |= UART_IER_RDI;
  642. tegra_uart_write(tup, ier, UART_IER);
  643. ier &= ~(UART_IER_RDI | UART_IER_RLSI |
  644. UART_IER_RTOIE | TEGRA_UART_IER_EORD);
  645. tup->ier_shadow = ier;
  646. tegra_uart_write(tup, ier, UART_IER);
  647. }
  648. break;
  649. case 3: /* Receive error */
  650. tegra_uart_decode_rx_error(tup,
  651. tegra_uart_read(tup, UART_LSR));
  652. break;
  653. case 5: /* break nothing to handle */
  654. case 7: /* break nothing to handle */
  655. break;
  656. }
  657. }
  658. }
  659. static void tegra_uart_stop_rx(struct uart_port *u)
  660. {
  661. struct tegra_uart_port *tup = to_tegra_uport(u);
  662. struct tty_struct *tty;
  663. struct tty_port *port = &u->state->port;
  664. struct dma_tx_state state;
  665. unsigned long ier;
  666. int count;
  667. if (tup->rts_active)
  668. set_rts(tup, false);
  669. if (!tup->rx_in_progress)
  670. return;
  671. tty = tty_port_tty_get(&tup->uport.state->port);
  672. tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
  673. ier = tup->ier_shadow;
  674. ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
  675. TEGRA_UART_IER_EORD);
  676. tup->ier_shadow = ier;
  677. tegra_uart_write(tup, ier, UART_IER);
  678. tup->rx_in_progress = 0;
  679. if (tup->rx_dma_chan) {
  680. dmaengine_terminate_all(tup->rx_dma_chan);
  681. dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
  682. async_tx_ack(tup->rx_dma_desc);
  683. count = tup->rx_bytes_requested - state.residue;
  684. tegra_uart_copy_rx_to_tty(tup, port, count);
  685. tegra_uart_handle_rx_pio(tup, port);
  686. } else {
  687. tegra_uart_handle_rx_pio(tup, port);
  688. }
  689. if (tty) {
  690. tty_flip_buffer_push(port);
  691. tty_kref_put(tty);
  692. }
  693. return;
  694. }
  695. static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
  696. {
  697. unsigned long flags;
  698. unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
  699. unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
  700. unsigned long wait_time;
  701. unsigned long lsr;
  702. unsigned long msr;
  703. unsigned long mcr;
  704. /* Disable interrupts */
  705. tegra_uart_write(tup, 0, UART_IER);
  706. lsr = tegra_uart_read(tup, UART_LSR);
  707. if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  708. msr = tegra_uart_read(tup, UART_MSR);
  709. mcr = tegra_uart_read(tup, UART_MCR);
  710. if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
  711. dev_err(tup->uport.dev,
  712. "Tx Fifo not empty, CTS disabled, waiting\n");
  713. /* Wait for Tx fifo to be empty */
  714. while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
  715. wait_time = min(fifo_empty_time, 100lu);
  716. udelay(wait_time);
  717. fifo_empty_time -= wait_time;
  718. if (!fifo_empty_time) {
  719. msr = tegra_uart_read(tup, UART_MSR);
  720. mcr = tegra_uart_read(tup, UART_MCR);
  721. if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
  722. (msr & UART_MSR_CTS))
  723. dev_err(tup->uport.dev,
  724. "Slave not ready\n");
  725. break;
  726. }
  727. lsr = tegra_uart_read(tup, UART_LSR);
  728. }
  729. }
  730. spin_lock_irqsave(&tup->uport.lock, flags);
  731. /* Reset the Rx and Tx FIFOs */
  732. tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
  733. tup->current_baud = 0;
  734. spin_unlock_irqrestore(&tup->uport.lock, flags);
  735. clk_disable_unprepare(tup->uart_clk);
  736. }
  737. static int tegra_uart_hw_init(struct tegra_uart_port *tup)
  738. {
  739. int ret;
  740. tup->fcr_shadow = 0;
  741. tup->mcr_shadow = 0;
  742. tup->lcr_shadow = 0;
  743. tup->ier_shadow = 0;
  744. tup->current_baud = 0;
  745. clk_prepare_enable(tup->uart_clk);
  746. /* Reset the UART controller to clear all previous status.*/
  747. reset_control_assert(tup->rst);
  748. udelay(10);
  749. reset_control_deassert(tup->rst);
  750. tup->rx_in_progress = 0;
  751. tup->tx_in_progress = 0;
  752. /*
  753. * Set the trigger level
  754. *
  755. * For PIO mode:
  756. *
  757. * For receive, this will interrupt the CPU after that many number of
  758. * bytes are received, for the remaining bytes the receive timeout
  759. * interrupt is received. Rx high watermark is set to 4.
  760. *
  761. * For transmit, if the trasnmit interrupt is enabled, this will
  762. * interrupt the CPU when the number of entries in the FIFO reaches the
  763. * low watermark. Tx low watermark is set to 16 bytes.
  764. *
  765. * For DMA mode:
  766. *
  767. * Set the Tx trigger to 16. This should match the DMA burst size that
  768. * programmed in the DMA registers.
  769. */
  770. tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
  771. tup->fcr_shadow |= UART_FCR_R_TRIG_01;
  772. tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
  773. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  774. /* Dummy read to ensure the write is posted */
  775. tegra_uart_read(tup, UART_SCR);
  776. /*
  777. * For all tegra devices (up to t210), there is a hardware issue that
  778. * requires software to wait for 3 UART clock periods after enabling
  779. * the TX fifo, otherwise data could be lost.
  780. */
  781. tegra_uart_wait_cycle_time(tup, 3);
  782. /*
  783. * Initialize the UART with default configuration
  784. * (115200, N, 8, 1) so that the receive DMA buffer may be
  785. * enqueued
  786. */
  787. tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
  788. tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
  789. tup->fcr_shadow |= UART_FCR_DMA_SELECT;
  790. tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
  791. ret = tegra_uart_start_rx_dma(tup);
  792. if (ret < 0) {
  793. dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
  794. return ret;
  795. }
  796. tup->rx_in_progress = 1;
  797. /*
  798. * Enable IE_RXS for the receive status interrupts like line errros.
  799. * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
  800. *
  801. * If using DMA mode, enable EORD instead of receive interrupt which
  802. * will interrupt after the UART is done with the receive instead of
  803. * the interrupt when the FIFO "threshold" is reached.
  804. *
  805. * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
  806. * the DATA is sitting in the FIFO and couldn't be transferred to the
  807. * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
  808. * triggered when there is a pause of the incomming data stream for 4
  809. * characters long.
  810. *
  811. * For pauses in the data which is not aligned to 4 bytes, we get
  812. * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
  813. * then the EORD.
  814. */
  815. tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
  816. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  817. return 0;
  818. }
  819. static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
  820. bool dma_to_memory)
  821. {
  822. if (dma_to_memory) {
  823. dmaengine_terminate_all(tup->rx_dma_chan);
  824. dma_release_channel(tup->rx_dma_chan);
  825. dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
  826. tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
  827. tup->rx_dma_chan = NULL;
  828. tup->rx_dma_buf_phys = 0;
  829. tup->rx_dma_buf_virt = NULL;
  830. } else {
  831. dmaengine_terminate_all(tup->tx_dma_chan);
  832. dma_release_channel(tup->tx_dma_chan);
  833. dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
  834. UART_XMIT_SIZE, DMA_TO_DEVICE);
  835. tup->tx_dma_chan = NULL;
  836. tup->tx_dma_buf_phys = 0;
  837. tup->tx_dma_buf_virt = NULL;
  838. }
  839. }
  840. static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
  841. bool dma_to_memory)
  842. {
  843. struct dma_chan *dma_chan;
  844. unsigned char *dma_buf;
  845. dma_addr_t dma_phys;
  846. int ret;
  847. struct dma_slave_config dma_sconfig;
  848. dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
  849. dma_to_memory ? "rx" : "tx");
  850. if (IS_ERR(dma_chan)) {
  851. ret = PTR_ERR(dma_chan);
  852. dev_err(tup->uport.dev,
  853. "DMA channel alloc failed: %d\n", ret);
  854. return ret;
  855. }
  856. if (dma_to_memory) {
  857. dma_buf = dma_alloc_coherent(tup->uport.dev,
  858. TEGRA_UART_RX_DMA_BUFFER_SIZE,
  859. &dma_phys, GFP_KERNEL);
  860. if (!dma_buf) {
  861. dev_err(tup->uport.dev,
  862. "Not able to allocate the dma buffer\n");
  863. dma_release_channel(dma_chan);
  864. return -ENOMEM;
  865. }
  866. dma_sconfig.src_addr = tup->uport.mapbase;
  867. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  868. dma_sconfig.src_maxburst = 4;
  869. tup->rx_dma_chan = dma_chan;
  870. tup->rx_dma_buf_virt = dma_buf;
  871. tup->rx_dma_buf_phys = dma_phys;
  872. } else {
  873. dma_phys = dma_map_single(tup->uport.dev,
  874. tup->uport.state->xmit.buf, UART_XMIT_SIZE,
  875. DMA_TO_DEVICE);
  876. if (dma_mapping_error(tup->uport.dev, dma_phys)) {
  877. dev_err(tup->uport.dev, "dma_map_single tx failed\n");
  878. dma_release_channel(dma_chan);
  879. return -ENOMEM;
  880. }
  881. dma_buf = tup->uport.state->xmit.buf;
  882. dma_sconfig.dst_addr = tup->uport.mapbase;
  883. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  884. dma_sconfig.dst_maxburst = 16;
  885. tup->tx_dma_chan = dma_chan;
  886. tup->tx_dma_buf_virt = dma_buf;
  887. tup->tx_dma_buf_phys = dma_phys;
  888. }
  889. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  890. if (ret < 0) {
  891. dev_err(tup->uport.dev,
  892. "Dma slave config failed, err = %d\n", ret);
  893. tegra_uart_dma_channel_free(tup, dma_to_memory);
  894. return ret;
  895. }
  896. return 0;
  897. }
  898. static int tegra_uart_startup(struct uart_port *u)
  899. {
  900. struct tegra_uart_port *tup = to_tegra_uport(u);
  901. int ret;
  902. ret = tegra_uart_dma_channel_allocate(tup, false);
  903. if (ret < 0) {
  904. dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
  905. return ret;
  906. }
  907. ret = tegra_uart_dma_channel_allocate(tup, true);
  908. if (ret < 0) {
  909. dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
  910. goto fail_rx_dma;
  911. }
  912. ret = tegra_uart_hw_init(tup);
  913. if (ret < 0) {
  914. dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
  915. goto fail_hw_init;
  916. }
  917. ret = request_irq(u->irq, tegra_uart_isr, 0,
  918. dev_name(u->dev), tup);
  919. if (ret < 0) {
  920. dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
  921. goto fail_hw_init;
  922. }
  923. return 0;
  924. fail_hw_init:
  925. tegra_uart_dma_channel_free(tup, true);
  926. fail_rx_dma:
  927. tegra_uart_dma_channel_free(tup, false);
  928. return ret;
  929. }
  930. /*
  931. * Flush any TX data submitted for DMA and PIO. Called when the
  932. * TX circular buffer is reset.
  933. */
  934. static void tegra_uart_flush_buffer(struct uart_port *u)
  935. {
  936. struct tegra_uart_port *tup = to_tegra_uport(u);
  937. tup->tx_bytes = 0;
  938. if (tup->tx_dma_chan)
  939. dmaengine_terminate_all(tup->tx_dma_chan);
  940. return;
  941. }
  942. static void tegra_uart_shutdown(struct uart_port *u)
  943. {
  944. struct tegra_uart_port *tup = to_tegra_uport(u);
  945. tegra_uart_hw_deinit(tup);
  946. tup->rx_in_progress = 0;
  947. tup->tx_in_progress = 0;
  948. tegra_uart_dma_channel_free(tup, true);
  949. tegra_uart_dma_channel_free(tup, false);
  950. free_irq(u->irq, tup);
  951. }
  952. static void tegra_uart_enable_ms(struct uart_port *u)
  953. {
  954. struct tegra_uart_port *tup = to_tegra_uport(u);
  955. if (tup->enable_modem_interrupt) {
  956. tup->ier_shadow |= UART_IER_MSI;
  957. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  958. }
  959. }
  960. static void tegra_uart_set_termios(struct uart_port *u,
  961. struct ktermios *termios, struct ktermios *oldtermios)
  962. {
  963. struct tegra_uart_port *tup = to_tegra_uport(u);
  964. unsigned int baud;
  965. unsigned long flags;
  966. unsigned int lcr;
  967. int symb_bit = 1;
  968. struct clk *parent_clk = clk_get_parent(tup->uart_clk);
  969. unsigned long parent_clk_rate = clk_get_rate(parent_clk);
  970. int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
  971. max_divider *= 16;
  972. spin_lock_irqsave(&u->lock, flags);
  973. /* Changing configuration, it is safe to stop any rx now */
  974. if (tup->rts_active)
  975. set_rts(tup, false);
  976. /* Clear all interrupts as configuration is going to be change */
  977. tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
  978. tegra_uart_read(tup, UART_IER);
  979. tegra_uart_write(tup, 0, UART_IER);
  980. tegra_uart_read(tup, UART_IER);
  981. /* Parity */
  982. lcr = tup->lcr_shadow;
  983. lcr &= ~UART_LCR_PARITY;
  984. /* CMSPAR isn't supported by this driver */
  985. termios->c_cflag &= ~CMSPAR;
  986. if ((termios->c_cflag & PARENB) == PARENB) {
  987. symb_bit++;
  988. if (termios->c_cflag & PARODD) {
  989. lcr |= UART_LCR_PARITY;
  990. lcr &= ~UART_LCR_EPAR;
  991. lcr &= ~UART_LCR_SPAR;
  992. } else {
  993. lcr |= UART_LCR_PARITY;
  994. lcr |= UART_LCR_EPAR;
  995. lcr &= ~UART_LCR_SPAR;
  996. }
  997. }
  998. lcr &= ~UART_LCR_WLEN8;
  999. switch (termios->c_cflag & CSIZE) {
  1000. case CS5:
  1001. lcr |= UART_LCR_WLEN5;
  1002. symb_bit += 5;
  1003. break;
  1004. case CS6:
  1005. lcr |= UART_LCR_WLEN6;
  1006. symb_bit += 6;
  1007. break;
  1008. case CS7:
  1009. lcr |= UART_LCR_WLEN7;
  1010. symb_bit += 7;
  1011. break;
  1012. default:
  1013. lcr |= UART_LCR_WLEN8;
  1014. symb_bit += 8;
  1015. break;
  1016. }
  1017. /* Stop bits */
  1018. if (termios->c_cflag & CSTOPB) {
  1019. lcr |= UART_LCR_STOP;
  1020. symb_bit += 2;
  1021. } else {
  1022. lcr &= ~UART_LCR_STOP;
  1023. symb_bit++;
  1024. }
  1025. tegra_uart_write(tup, lcr, UART_LCR);
  1026. tup->lcr_shadow = lcr;
  1027. tup->symb_bit = symb_bit;
  1028. /* Baud rate. */
  1029. baud = uart_get_baud_rate(u, termios, oldtermios,
  1030. parent_clk_rate/max_divider,
  1031. parent_clk_rate/16);
  1032. spin_unlock_irqrestore(&u->lock, flags);
  1033. tegra_set_baudrate(tup, baud);
  1034. if (tty_termios_baud_rate(termios))
  1035. tty_termios_encode_baud_rate(termios, baud, baud);
  1036. spin_lock_irqsave(&u->lock, flags);
  1037. /* Flow control */
  1038. if (termios->c_cflag & CRTSCTS) {
  1039. tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
  1040. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1041. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1042. /* if top layer has asked to set rts active then do so here */
  1043. if (tup->rts_active)
  1044. set_rts(tup, true);
  1045. } else {
  1046. tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
  1047. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1048. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1049. }
  1050. /* update the port timeout based on new settings */
  1051. uart_update_timeout(u, termios->c_cflag, baud);
  1052. /* Make sure all write has completed */
  1053. tegra_uart_read(tup, UART_IER);
  1054. /* Reenable interrupt */
  1055. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  1056. tegra_uart_read(tup, UART_IER);
  1057. spin_unlock_irqrestore(&u->lock, flags);
  1058. return;
  1059. }
  1060. static const char *tegra_uart_type(struct uart_port *u)
  1061. {
  1062. return TEGRA_UART_TYPE;
  1063. }
  1064. static struct uart_ops tegra_uart_ops = {
  1065. .tx_empty = tegra_uart_tx_empty,
  1066. .set_mctrl = tegra_uart_set_mctrl,
  1067. .get_mctrl = tegra_uart_get_mctrl,
  1068. .stop_tx = tegra_uart_stop_tx,
  1069. .start_tx = tegra_uart_start_tx,
  1070. .stop_rx = tegra_uart_stop_rx,
  1071. .flush_buffer = tegra_uart_flush_buffer,
  1072. .enable_ms = tegra_uart_enable_ms,
  1073. .break_ctl = tegra_uart_break_ctl,
  1074. .startup = tegra_uart_startup,
  1075. .shutdown = tegra_uart_shutdown,
  1076. .set_termios = tegra_uart_set_termios,
  1077. .type = tegra_uart_type,
  1078. .request_port = tegra_uart_request_port,
  1079. .release_port = tegra_uart_release_port,
  1080. };
  1081. static struct uart_driver tegra_uart_driver = {
  1082. .owner = THIS_MODULE,
  1083. .driver_name = "tegra_hsuart",
  1084. .dev_name = "ttyTHS",
  1085. .cons = NULL,
  1086. .nr = TEGRA_UART_MAXIMUM,
  1087. };
  1088. static int tegra_uart_parse_dt(struct platform_device *pdev,
  1089. struct tegra_uart_port *tup)
  1090. {
  1091. struct device_node *np = pdev->dev.of_node;
  1092. int port;
  1093. port = of_alias_get_id(np, "serial");
  1094. if (port < 0) {
  1095. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
  1096. return port;
  1097. }
  1098. tup->uport.line = port;
  1099. tup->enable_modem_interrupt = of_property_read_bool(np,
  1100. "nvidia,enable-modem-interrupt");
  1101. return 0;
  1102. }
  1103. static struct tegra_uart_chip_data tegra20_uart_chip_data = {
  1104. .tx_fifo_full_status = false,
  1105. .allow_txfifo_reset_fifo_mode = true,
  1106. .support_clk_src_div = false,
  1107. };
  1108. static struct tegra_uart_chip_data tegra30_uart_chip_data = {
  1109. .tx_fifo_full_status = true,
  1110. .allow_txfifo_reset_fifo_mode = false,
  1111. .support_clk_src_div = true,
  1112. };
  1113. static const struct of_device_id tegra_uart_of_match[] = {
  1114. {
  1115. .compatible = "nvidia,tegra30-hsuart",
  1116. .data = &tegra30_uart_chip_data,
  1117. }, {
  1118. .compatible = "nvidia,tegra20-hsuart",
  1119. .data = &tegra20_uart_chip_data,
  1120. }, {
  1121. },
  1122. };
  1123. MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
  1124. static int tegra_uart_probe(struct platform_device *pdev)
  1125. {
  1126. struct tegra_uart_port *tup;
  1127. struct uart_port *u;
  1128. struct resource *resource;
  1129. int ret;
  1130. const struct tegra_uart_chip_data *cdata;
  1131. const struct of_device_id *match;
  1132. match = of_match_device(tegra_uart_of_match, &pdev->dev);
  1133. if (!match) {
  1134. dev_err(&pdev->dev, "Error: No device match found\n");
  1135. return -ENODEV;
  1136. }
  1137. cdata = match->data;
  1138. tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
  1139. if (!tup) {
  1140. dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
  1141. return -ENOMEM;
  1142. }
  1143. ret = tegra_uart_parse_dt(pdev, tup);
  1144. if (ret < 0)
  1145. return ret;
  1146. u = &tup->uport;
  1147. u->dev = &pdev->dev;
  1148. u->ops = &tegra_uart_ops;
  1149. u->type = PORT_TEGRA;
  1150. u->fifosize = 32;
  1151. tup->cdata = cdata;
  1152. platform_set_drvdata(pdev, tup);
  1153. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1154. if (!resource) {
  1155. dev_err(&pdev->dev, "No IO memory resource\n");
  1156. return -ENODEV;
  1157. }
  1158. u->mapbase = resource->start;
  1159. u->membase = devm_ioremap_resource(&pdev->dev, resource);
  1160. if (IS_ERR(u->membase))
  1161. return PTR_ERR(u->membase);
  1162. tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
  1163. if (IS_ERR(tup->uart_clk)) {
  1164. dev_err(&pdev->dev, "Couldn't get the clock\n");
  1165. return PTR_ERR(tup->uart_clk);
  1166. }
  1167. tup->rst = devm_reset_control_get(&pdev->dev, "serial");
  1168. if (IS_ERR(tup->rst)) {
  1169. dev_err(&pdev->dev, "Couldn't get the reset\n");
  1170. return PTR_ERR(tup->rst);
  1171. }
  1172. u->iotype = UPIO_MEM32;
  1173. u->irq = platform_get_irq(pdev, 0);
  1174. u->regshift = 2;
  1175. ret = uart_add_one_port(&tegra_uart_driver, u);
  1176. if (ret < 0) {
  1177. dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
  1178. return ret;
  1179. }
  1180. return ret;
  1181. }
  1182. static int tegra_uart_remove(struct platform_device *pdev)
  1183. {
  1184. struct tegra_uart_port *tup = platform_get_drvdata(pdev);
  1185. struct uart_port *u = &tup->uport;
  1186. uart_remove_one_port(&tegra_uart_driver, u);
  1187. return 0;
  1188. }
  1189. #ifdef CONFIG_PM_SLEEP
  1190. static int tegra_uart_suspend(struct device *dev)
  1191. {
  1192. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1193. struct uart_port *u = &tup->uport;
  1194. return uart_suspend_port(&tegra_uart_driver, u);
  1195. }
  1196. static int tegra_uart_resume(struct device *dev)
  1197. {
  1198. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1199. struct uart_port *u = &tup->uport;
  1200. return uart_resume_port(&tegra_uart_driver, u);
  1201. }
  1202. #endif
  1203. static const struct dev_pm_ops tegra_uart_pm_ops = {
  1204. SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
  1205. };
  1206. static struct platform_driver tegra_uart_platform_driver = {
  1207. .probe = tegra_uart_probe,
  1208. .remove = tegra_uart_remove,
  1209. .driver = {
  1210. .name = "serial-tegra",
  1211. .of_match_table = tegra_uart_of_match,
  1212. .pm = &tegra_uart_pm_ops,
  1213. },
  1214. };
  1215. static int __init tegra_uart_init(void)
  1216. {
  1217. int ret;
  1218. ret = uart_register_driver(&tegra_uart_driver);
  1219. if (ret < 0) {
  1220. pr_err("Could not register %s driver\n",
  1221. tegra_uart_driver.driver_name);
  1222. return ret;
  1223. }
  1224. ret = platform_driver_register(&tegra_uart_platform_driver);
  1225. if (ret < 0) {
  1226. pr_err("Uart platform driver register failed, e = %d\n", ret);
  1227. uart_unregister_driver(&tegra_uart_driver);
  1228. return ret;
  1229. }
  1230. return 0;
  1231. }
  1232. static void __exit tegra_uart_exit(void)
  1233. {
  1234. pr_info("Unloading tegra uart driver\n");
  1235. platform_driver_unregister(&tegra_uart_platform_driver);
  1236. uart_unregister_driver(&tegra_uart_driver);
  1237. }
  1238. module_init(tegra_uart_init);
  1239. module_exit(tegra_uart_exit);
  1240. MODULE_ALIAS("platform:serial-tegra");
  1241. MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
  1242. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1243. MODULE_LICENSE("GPL v2");