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