mxs-auart.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * Freescale STMP37XX/STMP378X Application UART driver
  3. *
  4. * Author: dmitry pervushin <dimka@embeddedalley.com>
  5. *
  6. * Copyright 2008-2010 Freescale Semiconductor, Inc.
  7. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  8. *
  9. * The code contained herein is licensed under the GNU General Public
  10. * License. You may obtain a copy of the GNU General Public License
  11. * Version 2 or later at the following locations:
  12. *
  13. * http://www.opensource.org/licenses/gpl-license.html
  14. * http://www.gnu.org/copyleft/gpl.html
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/wait.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/serial.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/device.h>
  31. #include <linux/clk.h>
  32. #include <linux/delay.h>
  33. #include <linux/io.h>
  34. #include <linux/of_device.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/dmaengine.h>
  37. #include <asm/cacheflush.h>
  38. #define MXS_AUART_PORTS 5
  39. #define AUART_CTRL0 0x00000000
  40. #define AUART_CTRL0_SET 0x00000004
  41. #define AUART_CTRL0_CLR 0x00000008
  42. #define AUART_CTRL0_TOG 0x0000000c
  43. #define AUART_CTRL1 0x00000010
  44. #define AUART_CTRL1_SET 0x00000014
  45. #define AUART_CTRL1_CLR 0x00000018
  46. #define AUART_CTRL1_TOG 0x0000001c
  47. #define AUART_CTRL2 0x00000020
  48. #define AUART_CTRL2_SET 0x00000024
  49. #define AUART_CTRL2_CLR 0x00000028
  50. #define AUART_CTRL2_TOG 0x0000002c
  51. #define AUART_LINECTRL 0x00000030
  52. #define AUART_LINECTRL_SET 0x00000034
  53. #define AUART_LINECTRL_CLR 0x00000038
  54. #define AUART_LINECTRL_TOG 0x0000003c
  55. #define AUART_LINECTRL2 0x00000040
  56. #define AUART_LINECTRL2_SET 0x00000044
  57. #define AUART_LINECTRL2_CLR 0x00000048
  58. #define AUART_LINECTRL2_TOG 0x0000004c
  59. #define AUART_INTR 0x00000050
  60. #define AUART_INTR_SET 0x00000054
  61. #define AUART_INTR_CLR 0x00000058
  62. #define AUART_INTR_TOG 0x0000005c
  63. #define AUART_DATA 0x00000060
  64. #define AUART_STAT 0x00000070
  65. #define AUART_DEBUG 0x00000080
  66. #define AUART_VERSION 0x00000090
  67. #define AUART_AUTOBAUD 0x000000a0
  68. #define AUART_CTRL0_SFTRST (1 << 31)
  69. #define AUART_CTRL0_CLKGATE (1 << 30)
  70. #define AUART_CTRL0_RXTO_ENABLE (1 << 27)
  71. #define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
  72. #define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
  73. #define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
  74. #define AUART_CTRL2_DMAONERR (1 << 26)
  75. #define AUART_CTRL2_TXDMAE (1 << 25)
  76. #define AUART_CTRL2_RXDMAE (1 << 24)
  77. #define AUART_CTRL2_CTSEN (1 << 15)
  78. #define AUART_CTRL2_RTSEN (1 << 14)
  79. #define AUART_CTRL2_RTS (1 << 11)
  80. #define AUART_CTRL2_RXE (1 << 9)
  81. #define AUART_CTRL2_TXE (1 << 8)
  82. #define AUART_CTRL2_UARTEN (1 << 0)
  83. #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
  84. #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
  85. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  86. #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
  87. #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
  88. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  89. #define AUART_LINECTRL_WLEN_MASK 0x00000060
  90. #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
  91. #define AUART_LINECTRL_FEN (1 << 4)
  92. #define AUART_LINECTRL_STP2 (1 << 3)
  93. #define AUART_LINECTRL_EPS (1 << 2)
  94. #define AUART_LINECTRL_PEN (1 << 1)
  95. #define AUART_LINECTRL_BRK (1 << 0)
  96. #define AUART_INTR_RTIEN (1 << 22)
  97. #define AUART_INTR_TXIEN (1 << 21)
  98. #define AUART_INTR_RXIEN (1 << 20)
  99. #define AUART_INTR_CTSMIEN (1 << 17)
  100. #define AUART_INTR_RTIS (1 << 6)
  101. #define AUART_INTR_TXIS (1 << 5)
  102. #define AUART_INTR_RXIS (1 << 4)
  103. #define AUART_INTR_CTSMIS (1 << 1)
  104. #define AUART_STAT_BUSY (1 << 29)
  105. #define AUART_STAT_CTS (1 << 28)
  106. #define AUART_STAT_TXFE (1 << 27)
  107. #define AUART_STAT_TXFF (1 << 25)
  108. #define AUART_STAT_RXFE (1 << 24)
  109. #define AUART_STAT_OERR (1 << 19)
  110. #define AUART_STAT_BERR (1 << 18)
  111. #define AUART_STAT_PERR (1 << 17)
  112. #define AUART_STAT_FERR (1 << 16)
  113. #define AUART_STAT_RXCOUNT_MASK 0xffff
  114. static struct uart_driver auart_driver;
  115. enum mxs_auart_type {
  116. IMX23_AUART,
  117. IMX28_AUART,
  118. };
  119. struct mxs_auart_port {
  120. struct uart_port port;
  121. #define MXS_AUART_DMA_ENABLED 0x2
  122. #define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
  123. #define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
  124. #define MXS_AUART_RTSCTS 4 /* bit 4 */
  125. unsigned long flags;
  126. unsigned int ctrl;
  127. enum mxs_auart_type devtype;
  128. unsigned int irq;
  129. struct clk *clk;
  130. struct device *dev;
  131. /* for DMA */
  132. struct scatterlist tx_sgl;
  133. struct dma_chan *tx_dma_chan;
  134. void *tx_dma_buf;
  135. struct scatterlist rx_sgl;
  136. struct dma_chan *rx_dma_chan;
  137. void *rx_dma_buf;
  138. };
  139. static struct platform_device_id mxs_auart_devtype[] = {
  140. { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
  141. { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
  142. { /* sentinel */ }
  143. };
  144. MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
  145. static struct of_device_id mxs_auart_dt_ids[] = {
  146. {
  147. .compatible = "fsl,imx28-auart",
  148. .data = &mxs_auart_devtype[IMX28_AUART]
  149. }, {
  150. .compatible = "fsl,imx23-auart",
  151. .data = &mxs_auart_devtype[IMX23_AUART]
  152. }, { /* sentinel */ }
  153. };
  154. MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
  155. static inline int is_imx28_auart(struct mxs_auart_port *s)
  156. {
  157. return s->devtype == IMX28_AUART;
  158. }
  159. static inline bool auart_dma_enabled(struct mxs_auart_port *s)
  160. {
  161. return s->flags & MXS_AUART_DMA_ENABLED;
  162. }
  163. static void mxs_auart_stop_tx(struct uart_port *u);
  164. #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
  165. static void mxs_auart_tx_chars(struct mxs_auart_port *s);
  166. static void dma_tx_callback(void *param)
  167. {
  168. struct mxs_auart_port *s = param;
  169. struct circ_buf *xmit = &s->port.state->xmit;
  170. dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
  171. /* clear the bit used to serialize the DMA tx. */
  172. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  173. smp_mb__after_clear_bit();
  174. /* wake up the possible processes. */
  175. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  176. uart_write_wakeup(&s->port);
  177. mxs_auart_tx_chars(s);
  178. }
  179. static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
  180. {
  181. struct dma_async_tx_descriptor *desc;
  182. struct scatterlist *sgl = &s->tx_sgl;
  183. struct dma_chan *channel = s->tx_dma_chan;
  184. u32 pio;
  185. /* [1] : send PIO. Note, the first pio word is CTRL1. */
  186. pio = AUART_CTRL1_XFER_COUNT(size);
  187. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
  188. 1, DMA_TRANS_NONE, 0);
  189. if (!desc) {
  190. dev_err(s->dev, "step 1 error\n");
  191. return -EINVAL;
  192. }
  193. /* [2] : set DMA buffer. */
  194. sg_init_one(sgl, s->tx_dma_buf, size);
  195. dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
  196. desc = dmaengine_prep_slave_sg(channel, sgl,
  197. 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  198. if (!desc) {
  199. dev_err(s->dev, "step 2 error\n");
  200. return -EINVAL;
  201. }
  202. /* [3] : submit the DMA */
  203. desc->callback = dma_tx_callback;
  204. desc->callback_param = s;
  205. dmaengine_submit(desc);
  206. dma_async_issue_pending(channel);
  207. return 0;
  208. }
  209. static void mxs_auart_tx_chars(struct mxs_auart_port *s)
  210. {
  211. struct circ_buf *xmit = &s->port.state->xmit;
  212. if (auart_dma_enabled(s)) {
  213. u32 i = 0;
  214. int size;
  215. void *buffer = s->tx_dma_buf;
  216. if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
  217. return;
  218. while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  219. size = min_t(u32, UART_XMIT_SIZE - i,
  220. CIRC_CNT_TO_END(xmit->head,
  221. xmit->tail,
  222. UART_XMIT_SIZE));
  223. memcpy(buffer + i, xmit->buf + xmit->tail, size);
  224. xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
  225. i += size;
  226. if (i >= UART_XMIT_SIZE)
  227. break;
  228. }
  229. if (uart_tx_stopped(&s->port))
  230. mxs_auart_stop_tx(&s->port);
  231. if (i) {
  232. mxs_auart_dma_tx(s, i);
  233. } else {
  234. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  235. smp_mb__after_clear_bit();
  236. }
  237. return;
  238. }
  239. while (!(readl(s->port.membase + AUART_STAT) &
  240. AUART_STAT_TXFF)) {
  241. if (s->port.x_char) {
  242. s->port.icount.tx++;
  243. writel(s->port.x_char,
  244. s->port.membase + AUART_DATA);
  245. s->port.x_char = 0;
  246. continue;
  247. }
  248. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
  249. s->port.icount.tx++;
  250. writel(xmit->buf[xmit->tail],
  251. s->port.membase + AUART_DATA);
  252. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  253. } else
  254. break;
  255. }
  256. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  257. uart_write_wakeup(&s->port);
  258. if (uart_circ_empty(&(s->port.state->xmit)))
  259. writel(AUART_INTR_TXIEN,
  260. s->port.membase + AUART_INTR_CLR);
  261. else
  262. writel(AUART_INTR_TXIEN,
  263. s->port.membase + AUART_INTR_SET);
  264. if (uart_tx_stopped(&s->port))
  265. mxs_auart_stop_tx(&s->port);
  266. }
  267. static void mxs_auart_rx_char(struct mxs_auart_port *s)
  268. {
  269. int flag;
  270. u32 stat;
  271. u8 c;
  272. c = readl(s->port.membase + AUART_DATA);
  273. stat = readl(s->port.membase + AUART_STAT);
  274. flag = TTY_NORMAL;
  275. s->port.icount.rx++;
  276. if (stat & AUART_STAT_BERR) {
  277. s->port.icount.brk++;
  278. if (uart_handle_break(&s->port))
  279. goto out;
  280. } else if (stat & AUART_STAT_PERR) {
  281. s->port.icount.parity++;
  282. } else if (stat & AUART_STAT_FERR) {
  283. s->port.icount.frame++;
  284. }
  285. /*
  286. * Mask off conditions which should be ingored.
  287. */
  288. stat &= s->port.read_status_mask;
  289. if (stat & AUART_STAT_BERR) {
  290. flag = TTY_BREAK;
  291. } else if (stat & AUART_STAT_PERR)
  292. flag = TTY_PARITY;
  293. else if (stat & AUART_STAT_FERR)
  294. flag = TTY_FRAME;
  295. if (stat & AUART_STAT_OERR)
  296. s->port.icount.overrun++;
  297. if (uart_handle_sysrq_char(&s->port, c))
  298. goto out;
  299. uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
  300. out:
  301. writel(stat, s->port.membase + AUART_STAT);
  302. }
  303. static void mxs_auart_rx_chars(struct mxs_auart_port *s)
  304. {
  305. u32 stat = 0;
  306. for (;;) {
  307. stat = readl(s->port.membase + AUART_STAT);
  308. if (stat & AUART_STAT_RXFE)
  309. break;
  310. mxs_auart_rx_char(s);
  311. }
  312. writel(stat, s->port.membase + AUART_STAT);
  313. tty_flip_buffer_push(&s->port.state->port);
  314. }
  315. static int mxs_auart_request_port(struct uart_port *u)
  316. {
  317. return 0;
  318. }
  319. static int mxs_auart_verify_port(struct uart_port *u,
  320. struct serial_struct *ser)
  321. {
  322. if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
  323. return -EINVAL;
  324. return 0;
  325. }
  326. static void mxs_auart_config_port(struct uart_port *u, int flags)
  327. {
  328. }
  329. static const char *mxs_auart_type(struct uart_port *u)
  330. {
  331. struct mxs_auart_port *s = to_auart_port(u);
  332. return dev_name(s->dev);
  333. }
  334. static void mxs_auart_release_port(struct uart_port *u)
  335. {
  336. }
  337. static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
  338. {
  339. struct mxs_auart_port *s = to_auart_port(u);
  340. u32 ctrl = readl(u->membase + AUART_CTRL2);
  341. ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
  342. if (mctrl & TIOCM_RTS) {
  343. if (tty_port_cts_enabled(&u->state->port))
  344. ctrl |= AUART_CTRL2_RTSEN;
  345. else
  346. ctrl |= AUART_CTRL2_RTS;
  347. }
  348. s->ctrl = mctrl;
  349. writel(ctrl, u->membase + AUART_CTRL2);
  350. }
  351. static u32 mxs_auart_get_mctrl(struct uart_port *u)
  352. {
  353. struct mxs_auart_port *s = to_auart_port(u);
  354. u32 stat = readl(u->membase + AUART_STAT);
  355. int ctrl2 = readl(u->membase + AUART_CTRL2);
  356. u32 mctrl = s->ctrl;
  357. mctrl &= ~TIOCM_CTS;
  358. if (stat & AUART_STAT_CTS)
  359. mctrl |= TIOCM_CTS;
  360. if (ctrl2 & AUART_CTRL2_RTS)
  361. mctrl |= TIOCM_RTS;
  362. return mctrl;
  363. }
  364. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
  365. static void dma_rx_callback(void *arg)
  366. {
  367. struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
  368. struct tty_port *port = &s->port.state->port;
  369. int count;
  370. u32 stat;
  371. dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
  372. stat = readl(s->port.membase + AUART_STAT);
  373. stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
  374. AUART_STAT_PERR | AUART_STAT_FERR);
  375. count = stat & AUART_STAT_RXCOUNT_MASK;
  376. tty_insert_flip_string(port, s->rx_dma_buf, count);
  377. writel(stat, s->port.membase + AUART_STAT);
  378. tty_flip_buffer_push(port);
  379. /* start the next DMA for RX. */
  380. mxs_auart_dma_prep_rx(s);
  381. }
  382. static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
  383. {
  384. struct dma_async_tx_descriptor *desc;
  385. struct scatterlist *sgl = &s->rx_sgl;
  386. struct dma_chan *channel = s->rx_dma_chan;
  387. u32 pio[1];
  388. /* [1] : send PIO */
  389. pio[0] = AUART_CTRL0_RXTO_ENABLE
  390. | AUART_CTRL0_RXTIMEOUT(0x80)
  391. | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
  392. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  393. 1, DMA_TRANS_NONE, 0);
  394. if (!desc) {
  395. dev_err(s->dev, "step 1 error\n");
  396. return -EINVAL;
  397. }
  398. /* [2] : send DMA request */
  399. sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
  400. dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
  401. desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
  402. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  403. if (!desc) {
  404. dev_err(s->dev, "step 2 error\n");
  405. return -1;
  406. }
  407. /* [3] : submit the DMA, but do not issue it. */
  408. desc->callback = dma_rx_callback;
  409. desc->callback_param = s;
  410. dmaengine_submit(desc);
  411. dma_async_issue_pending(channel);
  412. return 0;
  413. }
  414. static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
  415. {
  416. if (s->tx_dma_chan) {
  417. dma_release_channel(s->tx_dma_chan);
  418. s->tx_dma_chan = NULL;
  419. }
  420. if (s->rx_dma_chan) {
  421. dma_release_channel(s->rx_dma_chan);
  422. s->rx_dma_chan = NULL;
  423. }
  424. kfree(s->tx_dma_buf);
  425. kfree(s->rx_dma_buf);
  426. s->tx_dma_buf = NULL;
  427. s->rx_dma_buf = NULL;
  428. }
  429. static void mxs_auart_dma_exit(struct mxs_auart_port *s)
  430. {
  431. writel(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
  432. s->port.membase + AUART_CTRL2_CLR);
  433. mxs_auart_dma_exit_channel(s);
  434. s->flags &= ~MXS_AUART_DMA_ENABLED;
  435. clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
  436. clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
  437. }
  438. static int mxs_auart_dma_init(struct mxs_auart_port *s)
  439. {
  440. if (auart_dma_enabled(s))
  441. return 0;
  442. /* init for RX */
  443. s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
  444. if (!s->rx_dma_chan)
  445. goto err_out;
  446. s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  447. if (!s->rx_dma_buf)
  448. goto err_out;
  449. /* init for TX */
  450. s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
  451. if (!s->tx_dma_chan)
  452. goto err_out;
  453. s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
  454. if (!s->tx_dma_buf)
  455. goto err_out;
  456. /* set the flags */
  457. s->flags |= MXS_AUART_DMA_ENABLED;
  458. dev_dbg(s->dev, "enabled the DMA support.");
  459. return 0;
  460. err_out:
  461. mxs_auart_dma_exit_channel(s);
  462. return -EINVAL;
  463. }
  464. static void mxs_auart_settermios(struct uart_port *u,
  465. struct ktermios *termios,
  466. struct ktermios *old)
  467. {
  468. struct mxs_auart_port *s = to_auart_port(u);
  469. u32 bm, ctrl, ctrl2, div;
  470. unsigned int cflag, baud;
  471. cflag = termios->c_cflag;
  472. ctrl = AUART_LINECTRL_FEN;
  473. ctrl2 = readl(u->membase + AUART_CTRL2);
  474. /* byte size */
  475. switch (cflag & CSIZE) {
  476. case CS5:
  477. bm = 0;
  478. break;
  479. case CS6:
  480. bm = 1;
  481. break;
  482. case CS7:
  483. bm = 2;
  484. break;
  485. case CS8:
  486. bm = 3;
  487. break;
  488. default:
  489. return;
  490. }
  491. ctrl |= AUART_LINECTRL_WLEN(bm);
  492. /* parity */
  493. if (cflag & PARENB) {
  494. ctrl |= AUART_LINECTRL_PEN;
  495. if ((cflag & PARODD) == 0)
  496. ctrl |= AUART_LINECTRL_EPS;
  497. }
  498. u->read_status_mask = 0;
  499. if (termios->c_iflag & INPCK)
  500. u->read_status_mask |= AUART_STAT_PERR;
  501. if (termios->c_iflag & (BRKINT | PARMRK))
  502. u->read_status_mask |= AUART_STAT_BERR;
  503. /*
  504. * Characters to ignore
  505. */
  506. u->ignore_status_mask = 0;
  507. if (termios->c_iflag & IGNPAR)
  508. u->ignore_status_mask |= AUART_STAT_PERR;
  509. if (termios->c_iflag & IGNBRK) {
  510. u->ignore_status_mask |= AUART_STAT_BERR;
  511. /*
  512. * If we're ignoring parity and break indicators,
  513. * ignore overruns too (for real raw support).
  514. */
  515. if (termios->c_iflag & IGNPAR)
  516. u->ignore_status_mask |= AUART_STAT_OERR;
  517. }
  518. /*
  519. * ignore all characters if CREAD is not set
  520. */
  521. if (cflag & CREAD)
  522. ctrl2 |= AUART_CTRL2_RXE;
  523. else
  524. ctrl2 &= ~AUART_CTRL2_RXE;
  525. /* figure out the stop bits requested */
  526. if (cflag & CSTOPB)
  527. ctrl |= AUART_LINECTRL_STP2;
  528. /* figure out the hardware flow control settings */
  529. if (cflag & CRTSCTS) {
  530. /*
  531. * The DMA has a bug(see errata:2836) in mx23.
  532. * So we can not implement the DMA for auart in mx23,
  533. * we can only implement the DMA support for auart
  534. * in mx28.
  535. */
  536. if (is_imx28_auart(s)
  537. && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
  538. if (!mxs_auart_dma_init(s))
  539. /* enable DMA tranfer */
  540. ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
  541. | AUART_CTRL2_DMAONERR;
  542. }
  543. ctrl2 |= AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN;
  544. } else {
  545. ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
  546. }
  547. /* set baud rate */
  548. baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
  549. div = u->uartclk * 32 / baud;
  550. ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
  551. ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
  552. writel(ctrl, u->membase + AUART_LINECTRL);
  553. writel(ctrl2, u->membase + AUART_CTRL2);
  554. uart_update_timeout(u, termios->c_cflag, baud);
  555. /* prepare for the DMA RX. */
  556. if (auart_dma_enabled(s) &&
  557. !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
  558. if (!mxs_auart_dma_prep_rx(s)) {
  559. /* Disable the normal RX interrupt. */
  560. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
  561. u->membase + AUART_INTR_CLR);
  562. } else {
  563. mxs_auart_dma_exit(s);
  564. dev_err(s->dev, "We can not start up the DMA.\n");
  565. }
  566. }
  567. }
  568. static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
  569. {
  570. u32 istat;
  571. struct mxs_auart_port *s = context;
  572. u32 stat = readl(s->port.membase + AUART_STAT);
  573. istat = readl(s->port.membase + AUART_INTR);
  574. /* ack irq */
  575. writel(istat & (AUART_INTR_RTIS
  576. | AUART_INTR_TXIS
  577. | AUART_INTR_RXIS
  578. | AUART_INTR_CTSMIS),
  579. s->port.membase + AUART_INTR_CLR);
  580. if (istat & AUART_INTR_CTSMIS) {
  581. uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
  582. writel(AUART_INTR_CTSMIS,
  583. s->port.membase + AUART_INTR_CLR);
  584. istat &= ~AUART_INTR_CTSMIS;
  585. }
  586. if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
  587. if (!auart_dma_enabled(s))
  588. mxs_auart_rx_chars(s);
  589. istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
  590. }
  591. if (istat & AUART_INTR_TXIS) {
  592. mxs_auart_tx_chars(s);
  593. istat &= ~AUART_INTR_TXIS;
  594. }
  595. return IRQ_HANDLED;
  596. }
  597. static void mxs_auart_reset(struct uart_port *u)
  598. {
  599. int i;
  600. unsigned int reg;
  601. writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
  602. for (i = 0; i < 10000; i++) {
  603. reg = readl(u->membase + AUART_CTRL0);
  604. if (!(reg & AUART_CTRL0_SFTRST))
  605. break;
  606. udelay(3);
  607. }
  608. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  609. }
  610. static int mxs_auart_startup(struct uart_port *u)
  611. {
  612. struct mxs_auart_port *s = to_auart_port(u);
  613. clk_prepare_enable(s->clk);
  614. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
  615. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
  616. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  617. u->membase + AUART_INTR);
  618. /*
  619. * Enable fifo so all four bytes of a DMA word are written to
  620. * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
  621. */
  622. writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
  623. return 0;
  624. }
  625. static void mxs_auart_shutdown(struct uart_port *u)
  626. {
  627. struct mxs_auart_port *s = to_auart_port(u);
  628. if (auart_dma_enabled(s))
  629. mxs_auart_dma_exit(s);
  630. writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
  631. writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
  632. u->membase + AUART_INTR_CLR);
  633. writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
  634. clk_disable_unprepare(s->clk);
  635. }
  636. static unsigned int mxs_auart_tx_empty(struct uart_port *u)
  637. {
  638. if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
  639. return TIOCSER_TEMT;
  640. else
  641. return 0;
  642. }
  643. static void mxs_auart_start_tx(struct uart_port *u)
  644. {
  645. struct mxs_auart_port *s = to_auart_port(u);
  646. /* enable transmitter */
  647. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
  648. mxs_auart_tx_chars(s);
  649. }
  650. static void mxs_auart_stop_tx(struct uart_port *u)
  651. {
  652. writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
  653. }
  654. static void mxs_auart_stop_rx(struct uart_port *u)
  655. {
  656. writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
  657. }
  658. static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
  659. {
  660. if (ctl)
  661. writel(AUART_LINECTRL_BRK,
  662. u->membase + AUART_LINECTRL_SET);
  663. else
  664. writel(AUART_LINECTRL_BRK,
  665. u->membase + AUART_LINECTRL_CLR);
  666. }
  667. static void mxs_auart_enable_ms(struct uart_port *port)
  668. {
  669. /* just empty */
  670. }
  671. static struct uart_ops mxs_auart_ops = {
  672. .tx_empty = mxs_auart_tx_empty,
  673. .start_tx = mxs_auart_start_tx,
  674. .stop_tx = mxs_auart_stop_tx,
  675. .stop_rx = mxs_auart_stop_rx,
  676. .enable_ms = mxs_auart_enable_ms,
  677. .break_ctl = mxs_auart_break_ctl,
  678. .set_mctrl = mxs_auart_set_mctrl,
  679. .get_mctrl = mxs_auart_get_mctrl,
  680. .startup = mxs_auart_startup,
  681. .shutdown = mxs_auart_shutdown,
  682. .set_termios = mxs_auart_settermios,
  683. .type = mxs_auart_type,
  684. .release_port = mxs_auart_release_port,
  685. .request_port = mxs_auart_request_port,
  686. .config_port = mxs_auart_config_port,
  687. .verify_port = mxs_auart_verify_port,
  688. };
  689. static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
  690. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  691. static void mxs_auart_console_putchar(struct uart_port *port, int ch)
  692. {
  693. unsigned int to = 1000;
  694. while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
  695. if (!to--)
  696. break;
  697. udelay(1);
  698. }
  699. writel(ch, port->membase + AUART_DATA);
  700. }
  701. static void
  702. auart_console_write(struct console *co, const char *str, unsigned int count)
  703. {
  704. struct mxs_auart_port *s;
  705. struct uart_port *port;
  706. unsigned int old_ctrl0, old_ctrl2;
  707. unsigned int to = 20000;
  708. if (co->index >= MXS_AUART_PORTS || co->index < 0)
  709. return;
  710. s = auart_port[co->index];
  711. port = &s->port;
  712. clk_enable(s->clk);
  713. /* First save the CR then disable the interrupts */
  714. old_ctrl2 = readl(port->membase + AUART_CTRL2);
  715. old_ctrl0 = readl(port->membase + AUART_CTRL0);
  716. writel(AUART_CTRL0_CLKGATE,
  717. port->membase + AUART_CTRL0_CLR);
  718. writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
  719. port->membase + AUART_CTRL2_SET);
  720. uart_console_write(port, str, count, mxs_auart_console_putchar);
  721. /* Finally, wait for transmitter to become empty ... */
  722. while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
  723. udelay(1);
  724. if (!to--)
  725. break;
  726. }
  727. /*
  728. * ... and restore the TCR if we waited long enough for the transmitter
  729. * to be idle. This might keep the transmitter enabled although it is
  730. * unused, but that is better than to disable it while it is still
  731. * transmitting.
  732. */
  733. if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
  734. writel(old_ctrl0, port->membase + AUART_CTRL0);
  735. writel(old_ctrl2, port->membase + AUART_CTRL2);
  736. }
  737. clk_disable(s->clk);
  738. }
  739. static void __init
  740. auart_console_get_options(struct uart_port *port, int *baud,
  741. int *parity, int *bits)
  742. {
  743. unsigned int lcr_h, quot;
  744. if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
  745. return;
  746. lcr_h = readl(port->membase + AUART_LINECTRL);
  747. *parity = 'n';
  748. if (lcr_h & AUART_LINECTRL_PEN) {
  749. if (lcr_h & AUART_LINECTRL_EPS)
  750. *parity = 'e';
  751. else
  752. *parity = 'o';
  753. }
  754. if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
  755. *bits = 7;
  756. else
  757. *bits = 8;
  758. quot = ((readl(port->membase + AUART_LINECTRL)
  759. & AUART_LINECTRL_BAUD_DIVINT_MASK))
  760. >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
  761. quot |= ((readl(port->membase + AUART_LINECTRL)
  762. & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
  763. >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
  764. if (quot == 0)
  765. quot = 1;
  766. *baud = (port->uartclk << 2) / quot;
  767. }
  768. static int __init
  769. auart_console_setup(struct console *co, char *options)
  770. {
  771. struct mxs_auart_port *s;
  772. int baud = 9600;
  773. int bits = 8;
  774. int parity = 'n';
  775. int flow = 'n';
  776. int ret;
  777. /*
  778. * Check whether an invalid uart number has been specified, and
  779. * if so, search for the first available port that does have
  780. * console support.
  781. */
  782. if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
  783. co->index = 0;
  784. s = auart_port[co->index];
  785. if (!s)
  786. return -ENODEV;
  787. clk_prepare_enable(s->clk);
  788. if (options)
  789. uart_parse_options(options, &baud, &parity, &bits, &flow);
  790. else
  791. auart_console_get_options(&s->port, &baud, &parity, &bits);
  792. ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
  793. clk_disable_unprepare(s->clk);
  794. return ret;
  795. }
  796. static struct console auart_console = {
  797. .name = "ttyAPP",
  798. .write = auart_console_write,
  799. .device = uart_console_device,
  800. .setup = auart_console_setup,
  801. .flags = CON_PRINTBUFFER,
  802. .index = -1,
  803. .data = &auart_driver,
  804. };
  805. #endif
  806. static struct uart_driver auart_driver = {
  807. .owner = THIS_MODULE,
  808. .driver_name = "ttyAPP",
  809. .dev_name = "ttyAPP",
  810. .major = 0,
  811. .minor = 0,
  812. .nr = MXS_AUART_PORTS,
  813. #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
  814. .cons = &auart_console,
  815. #endif
  816. };
  817. /*
  818. * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
  819. * could successfully get all information from dt or a negative errno.
  820. */
  821. static int serial_mxs_probe_dt(struct mxs_auart_port *s,
  822. struct platform_device *pdev)
  823. {
  824. struct device_node *np = pdev->dev.of_node;
  825. int ret;
  826. if (!np)
  827. /* no device tree device */
  828. return 1;
  829. ret = of_alias_get_id(np, "serial");
  830. if (ret < 0) {
  831. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  832. return ret;
  833. }
  834. s->port.line = ret;
  835. if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
  836. set_bit(MXS_AUART_RTSCTS, &s->flags);
  837. return 0;
  838. }
  839. static int mxs_auart_probe(struct platform_device *pdev)
  840. {
  841. const struct of_device_id *of_id =
  842. of_match_device(mxs_auart_dt_ids, &pdev->dev);
  843. struct mxs_auart_port *s;
  844. u32 version;
  845. int ret = 0;
  846. struct resource *r;
  847. s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
  848. if (!s) {
  849. ret = -ENOMEM;
  850. goto out;
  851. }
  852. ret = serial_mxs_probe_dt(s, pdev);
  853. if (ret > 0)
  854. s->port.line = pdev->id < 0 ? 0 : pdev->id;
  855. else if (ret < 0)
  856. goto out_free;
  857. if (of_id) {
  858. pdev->id_entry = of_id->data;
  859. s->devtype = pdev->id_entry->driver_data;
  860. }
  861. s->clk = clk_get(&pdev->dev, NULL);
  862. if (IS_ERR(s->clk)) {
  863. ret = PTR_ERR(s->clk);
  864. goto out_free;
  865. }
  866. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  867. if (!r) {
  868. ret = -ENXIO;
  869. goto out_free_clk;
  870. }
  871. s->port.mapbase = r->start;
  872. s->port.membase = ioremap(r->start, resource_size(r));
  873. s->port.ops = &mxs_auart_ops;
  874. s->port.iotype = UPIO_MEM;
  875. s->port.fifosize = 16;
  876. s->port.uartclk = clk_get_rate(s->clk);
  877. s->port.type = PORT_IMX;
  878. s->port.dev = s->dev = &pdev->dev;
  879. s->ctrl = 0;
  880. s->irq = platform_get_irq(pdev, 0);
  881. s->port.irq = s->irq;
  882. ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
  883. if (ret)
  884. goto out_free_clk;
  885. platform_set_drvdata(pdev, s);
  886. auart_port[s->port.line] = s;
  887. mxs_auart_reset(&s->port);
  888. ret = uart_add_one_port(&auart_driver, &s->port);
  889. if (ret)
  890. goto out_free_irq;
  891. version = readl(s->port.membase + AUART_VERSION);
  892. dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
  893. (version >> 24) & 0xff,
  894. (version >> 16) & 0xff, version & 0xffff);
  895. return 0;
  896. out_free_irq:
  897. auart_port[pdev->id] = NULL;
  898. free_irq(s->irq, s);
  899. out_free_clk:
  900. clk_put(s->clk);
  901. out_free:
  902. kfree(s);
  903. out:
  904. return ret;
  905. }
  906. static int mxs_auart_remove(struct platform_device *pdev)
  907. {
  908. struct mxs_auart_port *s = platform_get_drvdata(pdev);
  909. uart_remove_one_port(&auart_driver, &s->port);
  910. auart_port[pdev->id] = NULL;
  911. clk_put(s->clk);
  912. free_irq(s->irq, s);
  913. kfree(s);
  914. return 0;
  915. }
  916. static struct platform_driver mxs_auart_driver = {
  917. .probe = mxs_auart_probe,
  918. .remove = mxs_auart_remove,
  919. .driver = {
  920. .name = "mxs-auart",
  921. .owner = THIS_MODULE,
  922. .of_match_table = mxs_auart_dt_ids,
  923. },
  924. };
  925. static int __init mxs_auart_init(void)
  926. {
  927. int r;
  928. r = uart_register_driver(&auart_driver);
  929. if (r)
  930. goto out;
  931. r = platform_driver_register(&mxs_auart_driver);
  932. if (r)
  933. goto out_err;
  934. return 0;
  935. out_err:
  936. uart_unregister_driver(&auart_driver);
  937. out:
  938. return r;
  939. }
  940. static void __exit mxs_auart_exit(void)
  941. {
  942. platform_driver_unregister(&mxs_auart_driver);
  943. uart_unregister_driver(&auart_driver);
  944. }
  945. module_init(mxs_auart_init);
  946. module_exit(mxs_auart_exit);
  947. MODULE_LICENSE("GPL");
  948. MODULE_DESCRIPTION("Freescale MXS application uart driver");
  949. MODULE_ALIAS("platform:mxs-auart");