serial-tegra.c 36 KB

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