mxs-auart.c 28 KB

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