serial-tegra.c 44 KB

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