serial-tegra.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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 int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
  820. bool dma_to_memory)
  821. {
  822. struct dma_chan *dma_chan;
  823. unsigned char *dma_buf;
  824. dma_addr_t dma_phys;
  825. int ret;
  826. struct dma_slave_config dma_sconfig;
  827. dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
  828. dma_to_memory ? "rx" : "tx");
  829. if (IS_ERR(dma_chan)) {
  830. ret = PTR_ERR(dma_chan);
  831. dev_err(tup->uport.dev,
  832. "DMA channel alloc failed: %d\n", ret);
  833. return ret;
  834. }
  835. if (dma_to_memory) {
  836. dma_buf = dma_alloc_coherent(tup->uport.dev,
  837. TEGRA_UART_RX_DMA_BUFFER_SIZE,
  838. &dma_phys, GFP_KERNEL);
  839. if (!dma_buf) {
  840. dev_err(tup->uport.dev,
  841. "Not able to allocate the dma buffer\n");
  842. dma_release_channel(dma_chan);
  843. return -ENOMEM;
  844. }
  845. } else {
  846. dma_phys = dma_map_single(tup->uport.dev,
  847. tup->uport.state->xmit.buf, UART_XMIT_SIZE,
  848. DMA_TO_DEVICE);
  849. dma_buf = tup->uport.state->xmit.buf;
  850. }
  851. if (dma_to_memory) {
  852. dma_sconfig.src_addr = tup->uport.mapbase;
  853. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  854. dma_sconfig.src_maxburst = 4;
  855. } else {
  856. dma_sconfig.dst_addr = tup->uport.mapbase;
  857. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  858. dma_sconfig.dst_maxburst = 16;
  859. }
  860. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  861. if (ret < 0) {
  862. dev_err(tup->uport.dev,
  863. "Dma slave config failed, err = %d\n", ret);
  864. goto scrub;
  865. }
  866. if (dma_to_memory) {
  867. tup->rx_dma_chan = dma_chan;
  868. tup->rx_dma_buf_virt = dma_buf;
  869. tup->rx_dma_buf_phys = dma_phys;
  870. } else {
  871. tup->tx_dma_chan = dma_chan;
  872. tup->tx_dma_buf_virt = dma_buf;
  873. tup->tx_dma_buf_phys = dma_phys;
  874. }
  875. return 0;
  876. scrub:
  877. dma_release_channel(dma_chan);
  878. return ret;
  879. }
  880. static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
  881. bool dma_to_memory)
  882. {
  883. if (dma_to_memory) {
  884. dmaengine_terminate_all(tup->rx_dma_chan);
  885. dma_release_channel(tup->rx_dma_chan);
  886. dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
  887. tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
  888. tup->rx_dma_chan = NULL;
  889. tup->rx_dma_buf_phys = 0;
  890. tup->rx_dma_buf_virt = NULL;
  891. } else {
  892. dmaengine_terminate_all(tup->tx_dma_chan);
  893. dma_release_channel(tup->tx_dma_chan);
  894. dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
  895. UART_XMIT_SIZE, DMA_TO_DEVICE);
  896. tup->tx_dma_chan = NULL;
  897. tup->tx_dma_buf_phys = 0;
  898. tup->tx_dma_buf_virt = NULL;
  899. }
  900. }
  901. static int tegra_uart_startup(struct uart_port *u)
  902. {
  903. struct tegra_uart_port *tup = to_tegra_uport(u);
  904. int ret;
  905. ret = tegra_uart_dma_channel_allocate(tup, false);
  906. if (ret < 0) {
  907. dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
  908. return ret;
  909. }
  910. ret = tegra_uart_dma_channel_allocate(tup, true);
  911. if (ret < 0) {
  912. dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
  913. goto fail_rx_dma;
  914. }
  915. ret = tegra_uart_hw_init(tup);
  916. if (ret < 0) {
  917. dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
  918. goto fail_hw_init;
  919. }
  920. ret = request_irq(u->irq, tegra_uart_isr, 0,
  921. dev_name(u->dev), tup);
  922. if (ret < 0) {
  923. dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
  924. goto fail_hw_init;
  925. }
  926. return 0;
  927. fail_hw_init:
  928. tegra_uart_dma_channel_free(tup, true);
  929. fail_rx_dma:
  930. tegra_uart_dma_channel_free(tup, false);
  931. return ret;
  932. }
  933. /*
  934. * Flush any TX data submitted for DMA and PIO. Called when the
  935. * TX circular buffer is reset.
  936. */
  937. static void tegra_uart_flush_buffer(struct uart_port *u)
  938. {
  939. struct tegra_uart_port *tup = to_tegra_uport(u);
  940. tup->tx_bytes = 0;
  941. if (tup->tx_dma_chan)
  942. dmaengine_terminate_all(tup->tx_dma_chan);
  943. return;
  944. }
  945. static void tegra_uart_shutdown(struct uart_port *u)
  946. {
  947. struct tegra_uart_port *tup = to_tegra_uport(u);
  948. tegra_uart_hw_deinit(tup);
  949. tup->rx_in_progress = 0;
  950. tup->tx_in_progress = 0;
  951. tegra_uart_dma_channel_free(tup, true);
  952. tegra_uart_dma_channel_free(tup, false);
  953. free_irq(u->irq, tup);
  954. }
  955. static void tegra_uart_enable_ms(struct uart_port *u)
  956. {
  957. struct tegra_uart_port *tup = to_tegra_uport(u);
  958. if (tup->enable_modem_interrupt) {
  959. tup->ier_shadow |= UART_IER_MSI;
  960. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  961. }
  962. }
  963. static void tegra_uart_set_termios(struct uart_port *u,
  964. struct ktermios *termios, struct ktermios *oldtermios)
  965. {
  966. struct tegra_uart_port *tup = to_tegra_uport(u);
  967. unsigned int baud;
  968. unsigned long flags;
  969. unsigned int lcr;
  970. int symb_bit = 1;
  971. struct clk *parent_clk = clk_get_parent(tup->uart_clk);
  972. unsigned long parent_clk_rate = clk_get_rate(parent_clk);
  973. int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
  974. max_divider *= 16;
  975. spin_lock_irqsave(&u->lock, flags);
  976. /* Changing configuration, it is safe to stop any rx now */
  977. if (tup->rts_active)
  978. set_rts(tup, false);
  979. /* Clear all interrupts as configuration is going to be change */
  980. tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
  981. tegra_uart_read(tup, UART_IER);
  982. tegra_uart_write(tup, 0, UART_IER);
  983. tegra_uart_read(tup, UART_IER);
  984. /* Parity */
  985. lcr = tup->lcr_shadow;
  986. lcr &= ~UART_LCR_PARITY;
  987. /* CMSPAR isn't supported by this driver */
  988. termios->c_cflag &= ~CMSPAR;
  989. if ((termios->c_cflag & PARENB) == PARENB) {
  990. symb_bit++;
  991. if (termios->c_cflag & PARODD) {
  992. lcr |= UART_LCR_PARITY;
  993. lcr &= ~UART_LCR_EPAR;
  994. lcr &= ~UART_LCR_SPAR;
  995. } else {
  996. lcr |= UART_LCR_PARITY;
  997. lcr |= UART_LCR_EPAR;
  998. lcr &= ~UART_LCR_SPAR;
  999. }
  1000. }
  1001. lcr &= ~UART_LCR_WLEN8;
  1002. switch (termios->c_cflag & CSIZE) {
  1003. case CS5:
  1004. lcr |= UART_LCR_WLEN5;
  1005. symb_bit += 5;
  1006. break;
  1007. case CS6:
  1008. lcr |= UART_LCR_WLEN6;
  1009. symb_bit += 6;
  1010. break;
  1011. case CS7:
  1012. lcr |= UART_LCR_WLEN7;
  1013. symb_bit += 7;
  1014. break;
  1015. default:
  1016. lcr |= UART_LCR_WLEN8;
  1017. symb_bit += 8;
  1018. break;
  1019. }
  1020. /* Stop bits */
  1021. if (termios->c_cflag & CSTOPB) {
  1022. lcr |= UART_LCR_STOP;
  1023. symb_bit += 2;
  1024. } else {
  1025. lcr &= ~UART_LCR_STOP;
  1026. symb_bit++;
  1027. }
  1028. tegra_uart_write(tup, lcr, UART_LCR);
  1029. tup->lcr_shadow = lcr;
  1030. tup->symb_bit = symb_bit;
  1031. /* Baud rate. */
  1032. baud = uart_get_baud_rate(u, termios, oldtermios,
  1033. parent_clk_rate/max_divider,
  1034. parent_clk_rate/16);
  1035. spin_unlock_irqrestore(&u->lock, flags);
  1036. tegra_set_baudrate(tup, baud);
  1037. if (tty_termios_baud_rate(termios))
  1038. tty_termios_encode_baud_rate(termios, baud, baud);
  1039. spin_lock_irqsave(&u->lock, flags);
  1040. /* Flow control */
  1041. if (termios->c_cflag & CRTSCTS) {
  1042. tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
  1043. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1044. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1045. /* if top layer has asked to set rts active then do so here */
  1046. if (tup->rts_active)
  1047. set_rts(tup, true);
  1048. } else {
  1049. tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
  1050. tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
  1051. tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
  1052. }
  1053. /* update the port timeout based on new settings */
  1054. uart_update_timeout(u, termios->c_cflag, baud);
  1055. /* Make sure all write has completed */
  1056. tegra_uart_read(tup, UART_IER);
  1057. /* Reenable interrupt */
  1058. tegra_uart_write(tup, tup->ier_shadow, UART_IER);
  1059. tegra_uart_read(tup, UART_IER);
  1060. spin_unlock_irqrestore(&u->lock, flags);
  1061. return;
  1062. }
  1063. static const char *tegra_uart_type(struct uart_port *u)
  1064. {
  1065. return TEGRA_UART_TYPE;
  1066. }
  1067. static struct uart_ops tegra_uart_ops = {
  1068. .tx_empty = tegra_uart_tx_empty,
  1069. .set_mctrl = tegra_uart_set_mctrl,
  1070. .get_mctrl = tegra_uart_get_mctrl,
  1071. .stop_tx = tegra_uart_stop_tx,
  1072. .start_tx = tegra_uart_start_tx,
  1073. .stop_rx = tegra_uart_stop_rx,
  1074. .flush_buffer = tegra_uart_flush_buffer,
  1075. .enable_ms = tegra_uart_enable_ms,
  1076. .break_ctl = tegra_uart_break_ctl,
  1077. .startup = tegra_uart_startup,
  1078. .shutdown = tegra_uart_shutdown,
  1079. .set_termios = tegra_uart_set_termios,
  1080. .type = tegra_uart_type,
  1081. .request_port = tegra_uart_request_port,
  1082. .release_port = tegra_uart_release_port,
  1083. };
  1084. static struct uart_driver tegra_uart_driver = {
  1085. .owner = THIS_MODULE,
  1086. .driver_name = "tegra_hsuart",
  1087. .dev_name = "ttyTHS",
  1088. .cons = NULL,
  1089. .nr = TEGRA_UART_MAXIMUM,
  1090. };
  1091. static int tegra_uart_parse_dt(struct platform_device *pdev,
  1092. struct tegra_uart_port *tup)
  1093. {
  1094. struct device_node *np = pdev->dev.of_node;
  1095. int port;
  1096. port = of_alias_get_id(np, "serial");
  1097. if (port < 0) {
  1098. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
  1099. return port;
  1100. }
  1101. tup->uport.line = port;
  1102. tup->enable_modem_interrupt = of_property_read_bool(np,
  1103. "nvidia,enable-modem-interrupt");
  1104. return 0;
  1105. }
  1106. static struct tegra_uart_chip_data tegra20_uart_chip_data = {
  1107. .tx_fifo_full_status = false,
  1108. .allow_txfifo_reset_fifo_mode = true,
  1109. .support_clk_src_div = false,
  1110. };
  1111. static struct tegra_uart_chip_data tegra30_uart_chip_data = {
  1112. .tx_fifo_full_status = true,
  1113. .allow_txfifo_reset_fifo_mode = false,
  1114. .support_clk_src_div = true,
  1115. };
  1116. static const struct of_device_id tegra_uart_of_match[] = {
  1117. {
  1118. .compatible = "nvidia,tegra30-hsuart",
  1119. .data = &tegra30_uart_chip_data,
  1120. }, {
  1121. .compatible = "nvidia,tegra20-hsuart",
  1122. .data = &tegra20_uart_chip_data,
  1123. }, {
  1124. },
  1125. };
  1126. MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
  1127. static int tegra_uart_probe(struct platform_device *pdev)
  1128. {
  1129. struct tegra_uart_port *tup;
  1130. struct uart_port *u;
  1131. struct resource *resource;
  1132. int ret;
  1133. const struct tegra_uart_chip_data *cdata;
  1134. const struct of_device_id *match;
  1135. match = of_match_device(tegra_uart_of_match, &pdev->dev);
  1136. if (!match) {
  1137. dev_err(&pdev->dev, "Error: No device match found\n");
  1138. return -ENODEV;
  1139. }
  1140. cdata = match->data;
  1141. tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
  1142. if (!tup) {
  1143. dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
  1144. return -ENOMEM;
  1145. }
  1146. ret = tegra_uart_parse_dt(pdev, tup);
  1147. if (ret < 0)
  1148. return ret;
  1149. u = &tup->uport;
  1150. u->dev = &pdev->dev;
  1151. u->ops = &tegra_uart_ops;
  1152. u->type = PORT_TEGRA;
  1153. u->fifosize = 32;
  1154. tup->cdata = cdata;
  1155. platform_set_drvdata(pdev, tup);
  1156. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1157. if (!resource) {
  1158. dev_err(&pdev->dev, "No IO memory resource\n");
  1159. return -ENODEV;
  1160. }
  1161. u->mapbase = resource->start;
  1162. u->membase = devm_ioremap_resource(&pdev->dev, resource);
  1163. if (IS_ERR(u->membase))
  1164. return PTR_ERR(u->membase);
  1165. tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
  1166. if (IS_ERR(tup->uart_clk)) {
  1167. dev_err(&pdev->dev, "Couldn't get the clock\n");
  1168. return PTR_ERR(tup->uart_clk);
  1169. }
  1170. tup->rst = devm_reset_control_get(&pdev->dev, "serial");
  1171. if (IS_ERR(tup->rst)) {
  1172. dev_err(&pdev->dev, "Couldn't get the reset\n");
  1173. return PTR_ERR(tup->rst);
  1174. }
  1175. u->iotype = UPIO_MEM32;
  1176. u->irq = platform_get_irq(pdev, 0);
  1177. u->regshift = 2;
  1178. ret = uart_add_one_port(&tegra_uart_driver, u);
  1179. if (ret < 0) {
  1180. dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
  1181. return ret;
  1182. }
  1183. return ret;
  1184. }
  1185. static int tegra_uart_remove(struct platform_device *pdev)
  1186. {
  1187. struct tegra_uart_port *tup = platform_get_drvdata(pdev);
  1188. struct uart_port *u = &tup->uport;
  1189. uart_remove_one_port(&tegra_uart_driver, u);
  1190. return 0;
  1191. }
  1192. #ifdef CONFIG_PM_SLEEP
  1193. static int tegra_uart_suspend(struct device *dev)
  1194. {
  1195. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1196. struct uart_port *u = &tup->uport;
  1197. return uart_suspend_port(&tegra_uart_driver, u);
  1198. }
  1199. static int tegra_uart_resume(struct device *dev)
  1200. {
  1201. struct tegra_uart_port *tup = dev_get_drvdata(dev);
  1202. struct uart_port *u = &tup->uport;
  1203. return uart_resume_port(&tegra_uart_driver, u);
  1204. }
  1205. #endif
  1206. static const struct dev_pm_ops tegra_uart_pm_ops = {
  1207. SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
  1208. };
  1209. static struct platform_driver tegra_uart_platform_driver = {
  1210. .probe = tegra_uart_probe,
  1211. .remove = tegra_uart_remove,
  1212. .driver = {
  1213. .name = "serial-tegra",
  1214. .of_match_table = tegra_uart_of_match,
  1215. .pm = &tegra_uart_pm_ops,
  1216. },
  1217. };
  1218. static int __init tegra_uart_init(void)
  1219. {
  1220. int ret;
  1221. ret = uart_register_driver(&tegra_uart_driver);
  1222. if (ret < 0) {
  1223. pr_err("Could not register %s driver\n",
  1224. tegra_uart_driver.driver_name);
  1225. return ret;
  1226. }
  1227. ret = platform_driver_register(&tegra_uart_platform_driver);
  1228. if (ret < 0) {
  1229. pr_err("Uart platform driver register failed, e = %d\n", ret);
  1230. uart_unregister_driver(&tegra_uart_driver);
  1231. return ret;
  1232. }
  1233. return 0;
  1234. }
  1235. static void __exit tegra_uart_exit(void)
  1236. {
  1237. pr_info("Unloading tegra uart driver\n");
  1238. platform_driver_unregister(&tegra_uart_platform_driver);
  1239. uart_unregister_driver(&tegra_uart_driver);
  1240. }
  1241. module_init(tegra_uart_init);
  1242. module_exit(tegra_uart_exit);
  1243. MODULE_ALIAS("platform:serial-tegra");
  1244. MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
  1245. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1246. MODULE_LICENSE("GPL v2");