serial-tegra.c 38 KB

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