st-asc.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * st-asc.c: ST Asynchronous serial controller (ASC) driver
  3. *
  4. * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. */
  12. #if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  13. #define SUPPORT_SYSRQ
  14. #endif
  15. #include <linux/module.h>
  16. #include <linux/serial.h>
  17. #include <linux/console.h>
  18. #include <linux/sysrq.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/delay.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/serial_core.h>
  31. #include <linux/clk.h>
  32. #include <linux/gpio/consumer.h>
  33. #define DRIVER_NAME "st-asc"
  34. #define ASC_SERIAL_NAME "ttyAS"
  35. #define ASC_FIFO_SIZE 16
  36. #define ASC_MAX_PORTS 8
  37. /* Pinctrl states */
  38. #define DEFAULT 0
  39. #define NO_HW_FLOWCTRL 1
  40. struct asc_port {
  41. struct uart_port port;
  42. struct gpio_desc *rts;
  43. struct clk *clk;
  44. struct pinctrl *pinctrl;
  45. struct pinctrl_state *states[2];
  46. unsigned int hw_flow_control:1;
  47. unsigned int force_m1:1;
  48. };
  49. static struct asc_port asc_ports[ASC_MAX_PORTS];
  50. static struct uart_driver asc_uart_driver;
  51. /*---- UART Register definitions ------------------------------*/
  52. /* Register offsets */
  53. #define ASC_BAUDRATE 0x00
  54. #define ASC_TXBUF 0x04
  55. #define ASC_RXBUF 0x08
  56. #define ASC_CTL 0x0C
  57. #define ASC_INTEN 0x10
  58. #define ASC_STA 0x14
  59. #define ASC_GUARDTIME 0x18
  60. #define ASC_TIMEOUT 0x1C
  61. #define ASC_TXRESET 0x20
  62. #define ASC_RXRESET 0x24
  63. #define ASC_RETRIES 0x28
  64. /* ASC_RXBUF */
  65. #define ASC_RXBUF_PE 0x100
  66. #define ASC_RXBUF_FE 0x200
  67. /**
  68. * Some of status comes from higher bits of the character and some come from
  69. * the status register. Combining both of them in to single status using dummy
  70. * bits.
  71. */
  72. #define ASC_RXBUF_DUMMY_RX 0x10000
  73. #define ASC_RXBUF_DUMMY_BE 0x20000
  74. #define ASC_RXBUF_DUMMY_OE 0x40000
  75. /* ASC_CTL */
  76. #define ASC_CTL_MODE_MSK 0x0007
  77. #define ASC_CTL_MODE_8BIT 0x0001
  78. #define ASC_CTL_MODE_7BIT_PAR 0x0003
  79. #define ASC_CTL_MODE_9BIT 0x0004
  80. #define ASC_CTL_MODE_8BIT_WKUP 0x0005
  81. #define ASC_CTL_MODE_8BIT_PAR 0x0007
  82. #define ASC_CTL_STOP_MSK 0x0018
  83. #define ASC_CTL_STOP_HALFBIT 0x0000
  84. #define ASC_CTL_STOP_1BIT 0x0008
  85. #define ASC_CTL_STOP_1_HALFBIT 0x0010
  86. #define ASC_CTL_STOP_2BIT 0x0018
  87. #define ASC_CTL_PARITYODD 0x0020
  88. #define ASC_CTL_LOOPBACK 0x0040
  89. #define ASC_CTL_RUN 0x0080
  90. #define ASC_CTL_RXENABLE 0x0100
  91. #define ASC_CTL_SCENABLE 0x0200
  92. #define ASC_CTL_FIFOENABLE 0x0400
  93. #define ASC_CTL_CTSENABLE 0x0800
  94. #define ASC_CTL_BAUDMODE 0x1000
  95. /* ASC_GUARDTIME */
  96. #define ASC_GUARDTIME_MSK 0x00FF
  97. /* ASC_INTEN */
  98. #define ASC_INTEN_RBE 0x0001
  99. #define ASC_INTEN_TE 0x0002
  100. #define ASC_INTEN_THE 0x0004
  101. #define ASC_INTEN_PE 0x0008
  102. #define ASC_INTEN_FE 0x0010
  103. #define ASC_INTEN_OE 0x0020
  104. #define ASC_INTEN_TNE 0x0040
  105. #define ASC_INTEN_TOI 0x0080
  106. #define ASC_INTEN_RHF 0x0100
  107. /* ASC_RETRIES */
  108. #define ASC_RETRIES_MSK 0x00FF
  109. /* ASC_RXBUF */
  110. #define ASC_RXBUF_MSK 0x03FF
  111. /* ASC_STA */
  112. #define ASC_STA_RBF 0x0001
  113. #define ASC_STA_TE 0x0002
  114. #define ASC_STA_THE 0x0004
  115. #define ASC_STA_PE 0x0008
  116. #define ASC_STA_FE 0x0010
  117. #define ASC_STA_OE 0x0020
  118. #define ASC_STA_TNE 0x0040
  119. #define ASC_STA_TOI 0x0080
  120. #define ASC_STA_RHF 0x0100
  121. #define ASC_STA_TF 0x0200
  122. #define ASC_STA_NKD 0x0400
  123. /* ASC_TIMEOUT */
  124. #define ASC_TIMEOUT_MSK 0x00FF
  125. /* ASC_TXBUF */
  126. #define ASC_TXBUF_MSK 0x01FF
  127. /*---- Inline function definitions ---------------------------*/
  128. static inline struct asc_port *to_asc_port(struct uart_port *port)
  129. {
  130. return container_of(port, struct asc_port, port);
  131. }
  132. static inline u32 asc_in(struct uart_port *port, u32 offset)
  133. {
  134. #ifdef readl_relaxed
  135. return readl_relaxed(port->membase + offset);
  136. #else
  137. return readl(port->membase + offset);
  138. #endif
  139. }
  140. static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
  141. {
  142. #ifdef writel_relaxed
  143. writel_relaxed(value, port->membase + offset);
  144. #else
  145. writel(value, port->membase + offset);
  146. #endif
  147. }
  148. /*
  149. * Some simple utility functions to enable and disable interrupts.
  150. * Note that these need to be called with interrupts disabled.
  151. */
  152. static inline void asc_disable_tx_interrupts(struct uart_port *port)
  153. {
  154. u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
  155. asc_out(port, ASC_INTEN, intenable);
  156. (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
  157. }
  158. static inline void asc_enable_tx_interrupts(struct uart_port *port)
  159. {
  160. u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
  161. asc_out(port, ASC_INTEN, intenable);
  162. }
  163. static inline void asc_disable_rx_interrupts(struct uart_port *port)
  164. {
  165. u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
  166. asc_out(port, ASC_INTEN, intenable);
  167. (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
  168. }
  169. static inline void asc_enable_rx_interrupts(struct uart_port *port)
  170. {
  171. u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
  172. asc_out(port, ASC_INTEN, intenable);
  173. }
  174. static inline u32 asc_txfifo_is_empty(struct uart_port *port)
  175. {
  176. return asc_in(port, ASC_STA) & ASC_STA_TE;
  177. }
  178. static inline u32 asc_txfifo_is_half_empty(struct uart_port *port)
  179. {
  180. return asc_in(port, ASC_STA) & ASC_STA_THE;
  181. }
  182. static inline const char *asc_port_name(struct uart_port *port)
  183. {
  184. return to_platform_device(port->dev)->name;
  185. }
  186. /*----------------------------------------------------------------------*/
  187. /*
  188. * This section contains code to support the use of the ASC as a
  189. * generic serial port.
  190. */
  191. static inline unsigned asc_hw_txroom(struct uart_port *port)
  192. {
  193. u32 status = asc_in(port, ASC_STA);
  194. if (status & ASC_STA_THE)
  195. return port->fifosize / 2;
  196. else if (!(status & ASC_STA_TF))
  197. return 1;
  198. return 0;
  199. }
  200. /*
  201. * Start transmitting chars.
  202. * This is called from both interrupt and task level.
  203. * Either way interrupts are disabled.
  204. */
  205. static void asc_transmit_chars(struct uart_port *port)
  206. {
  207. struct circ_buf *xmit = &port->state->xmit;
  208. int txroom;
  209. unsigned char c;
  210. txroom = asc_hw_txroom(port);
  211. if ((txroom != 0) && port->x_char) {
  212. c = port->x_char;
  213. port->x_char = 0;
  214. asc_out(port, ASC_TXBUF, c);
  215. port->icount.tx++;
  216. txroom = asc_hw_txroom(port);
  217. }
  218. if (uart_tx_stopped(port)) {
  219. /*
  220. * We should try and stop the hardware here, but I
  221. * don't think the ASC has any way to do that.
  222. */
  223. asc_disable_tx_interrupts(port);
  224. return;
  225. }
  226. if (uart_circ_empty(xmit)) {
  227. asc_disable_tx_interrupts(port);
  228. return;
  229. }
  230. if (txroom == 0)
  231. return;
  232. do {
  233. c = xmit->buf[xmit->tail];
  234. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  235. asc_out(port, ASC_TXBUF, c);
  236. port->icount.tx++;
  237. txroom--;
  238. } while ((txroom > 0) && (!uart_circ_empty(xmit)));
  239. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  240. uart_write_wakeup(port);
  241. if (uart_circ_empty(xmit))
  242. asc_disable_tx_interrupts(port);
  243. }
  244. static void asc_receive_chars(struct uart_port *port)
  245. {
  246. struct tty_port *tport = &port->state->port;
  247. unsigned long status, mode;
  248. unsigned long c = 0;
  249. char flag;
  250. bool ignore_pe = false;
  251. /*
  252. * Datasheet states: If the MODE field selects an 8-bit frame then
  253. * this [parity error] bit is undefined. Software should ignore this
  254. * bit when reading 8-bit frames.
  255. */
  256. mode = asc_in(port, ASC_CTL) & ASC_CTL_MODE_MSK;
  257. if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
  258. ignore_pe = true;
  259. if (port->irq_wake)
  260. pm_wakeup_event(tport->tty->dev, 0);
  261. while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
  262. c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
  263. flag = TTY_NORMAL;
  264. port->icount.rx++;
  265. if (status & ASC_STA_OE || c & ASC_RXBUF_FE ||
  266. (c & ASC_RXBUF_PE && !ignore_pe)) {
  267. if (c & ASC_RXBUF_FE) {
  268. if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) {
  269. port->icount.brk++;
  270. if (uart_handle_break(port))
  271. continue;
  272. c |= ASC_RXBUF_DUMMY_BE;
  273. } else {
  274. port->icount.frame++;
  275. }
  276. } else if (c & ASC_RXBUF_PE) {
  277. port->icount.parity++;
  278. }
  279. /*
  280. * Reading any data from the RX FIFO clears the
  281. * overflow error condition.
  282. */
  283. if (status & ASC_STA_OE) {
  284. port->icount.overrun++;
  285. c |= ASC_RXBUF_DUMMY_OE;
  286. }
  287. c &= port->read_status_mask;
  288. if (c & ASC_RXBUF_DUMMY_BE)
  289. flag = TTY_BREAK;
  290. else if (c & ASC_RXBUF_PE)
  291. flag = TTY_PARITY;
  292. else if (c & ASC_RXBUF_FE)
  293. flag = TTY_FRAME;
  294. }
  295. if (uart_handle_sysrq_char(port, c & 0xff))
  296. continue;
  297. uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
  298. }
  299. /* Tell the rest of the system the news. New characters! */
  300. tty_flip_buffer_push(tport);
  301. }
  302. static irqreturn_t asc_interrupt(int irq, void *ptr)
  303. {
  304. struct uart_port *port = ptr;
  305. u32 status;
  306. spin_lock(&port->lock);
  307. status = asc_in(port, ASC_STA);
  308. if (status & ASC_STA_RBF) {
  309. /* Receive FIFO not empty */
  310. asc_receive_chars(port);
  311. }
  312. if ((status & ASC_STA_THE) &&
  313. (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
  314. /* Transmitter FIFO at least half empty */
  315. asc_transmit_chars(port);
  316. }
  317. spin_unlock(&port->lock);
  318. return IRQ_HANDLED;
  319. }
  320. /*----------------------------------------------------------------------*/
  321. /*
  322. * UART Functions
  323. */
  324. static unsigned int asc_tx_empty(struct uart_port *port)
  325. {
  326. return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
  327. }
  328. static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
  329. {
  330. struct asc_port *ascport = to_asc_port(port);
  331. /*
  332. * This routine is used for seting signals of: DTR, DCD, CTS and RTS.
  333. * We use ASC's hardware for CTS/RTS when hardware flow-control is
  334. * enabled, however if the RTS line is required for another purpose,
  335. * commonly controlled using HUP from userspace, then we need to toggle
  336. * it manually, using GPIO.
  337. *
  338. * Some boards also have DTR and DCD implemented using PIO pins, code to
  339. * do this should be hooked in here.
  340. */
  341. if (!ascport->rts)
  342. return;
  343. /* If HW flow-control is enabled, we can't fiddle with the RTS line */
  344. if (asc_in(port, ASC_CTL) & ASC_CTL_CTSENABLE)
  345. return;
  346. gpiod_set_value(ascport->rts, mctrl & TIOCM_RTS);
  347. }
  348. static unsigned int asc_get_mctrl(struct uart_port *port)
  349. {
  350. /*
  351. * This routine is used for geting signals of: DTR, DCD, DSR, RI,
  352. * and CTS/RTS
  353. */
  354. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  355. }
  356. /* There are probably characters waiting to be transmitted. */
  357. static void asc_start_tx(struct uart_port *port)
  358. {
  359. struct circ_buf *xmit = &port->state->xmit;
  360. if (!uart_circ_empty(xmit))
  361. asc_enable_tx_interrupts(port);
  362. }
  363. /* Transmit stop */
  364. static void asc_stop_tx(struct uart_port *port)
  365. {
  366. asc_disable_tx_interrupts(port);
  367. }
  368. /* Receive stop */
  369. static void asc_stop_rx(struct uart_port *port)
  370. {
  371. asc_disable_rx_interrupts(port);
  372. }
  373. /* Handle breaks - ignored by us */
  374. static void asc_break_ctl(struct uart_port *port, int break_state)
  375. {
  376. /* Nothing here yet .. */
  377. }
  378. /*
  379. * Enable port for reception.
  380. */
  381. static int asc_startup(struct uart_port *port)
  382. {
  383. if (request_irq(port->irq, asc_interrupt, 0,
  384. asc_port_name(port), port)) {
  385. dev_err(port->dev, "cannot allocate irq.\n");
  386. return -ENODEV;
  387. }
  388. asc_transmit_chars(port);
  389. asc_enable_rx_interrupts(port);
  390. return 0;
  391. }
  392. static void asc_shutdown(struct uart_port *port)
  393. {
  394. asc_disable_tx_interrupts(port);
  395. asc_disable_rx_interrupts(port);
  396. free_irq(port->irq, port);
  397. }
  398. static void asc_pm(struct uart_port *port, unsigned int state,
  399. unsigned int oldstate)
  400. {
  401. struct asc_port *ascport = to_asc_port(port);
  402. unsigned long flags = 0;
  403. u32 ctl;
  404. switch (state) {
  405. case UART_PM_STATE_ON:
  406. clk_prepare_enable(ascport->clk);
  407. break;
  408. case UART_PM_STATE_OFF:
  409. /*
  410. * Disable the ASC baud rate generator, which is as close as
  411. * we can come to turning it off. Note this is not called with
  412. * the port spinlock held.
  413. */
  414. spin_lock_irqsave(&port->lock, flags);
  415. ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
  416. asc_out(port, ASC_CTL, ctl);
  417. spin_unlock_irqrestore(&port->lock, flags);
  418. clk_disable_unprepare(ascport->clk);
  419. break;
  420. }
  421. }
  422. static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
  423. struct ktermios *old)
  424. {
  425. struct asc_port *ascport = to_asc_port(port);
  426. struct device_node *np = port->dev->of_node;
  427. struct gpio_desc *gpiod;
  428. unsigned int baud;
  429. u32 ctrl_val;
  430. tcflag_t cflag;
  431. unsigned long flags;
  432. /* Update termios to reflect hardware capabilities */
  433. termios->c_cflag &= ~(CMSPAR |
  434. (ascport->hw_flow_control ? 0 : CRTSCTS));
  435. port->uartclk = clk_get_rate(ascport->clk);
  436. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  437. cflag = termios->c_cflag;
  438. spin_lock_irqsave(&port->lock, flags);
  439. /* read control register */
  440. ctrl_val = asc_in(port, ASC_CTL);
  441. /* stop serial port and reset value */
  442. asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
  443. ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
  444. /* reset fifo rx & tx */
  445. asc_out(port, ASC_TXRESET, 1);
  446. asc_out(port, ASC_RXRESET, 1);
  447. /* set character length */
  448. if ((cflag & CSIZE) == CS7) {
  449. ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
  450. } else {
  451. ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
  452. ASC_CTL_MODE_8BIT;
  453. }
  454. /* set stop bit */
  455. ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
  456. /* odd parity */
  457. if (cflag & PARODD)
  458. ctrl_val |= ASC_CTL_PARITYODD;
  459. /* hardware flow control */
  460. if ((cflag & CRTSCTS)) {
  461. ctrl_val |= ASC_CTL_CTSENABLE;
  462. /* If flow-control selected, stop handling RTS manually */
  463. if (ascport->rts) {
  464. devm_gpiod_put(port->dev, ascport->rts);
  465. ascport->rts = NULL;
  466. pinctrl_select_state(ascport->pinctrl,
  467. ascport->states[DEFAULT]);
  468. }
  469. } else {
  470. /* If flow-control disabled, it's safe to handle RTS manually */
  471. if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
  472. pinctrl_select_state(ascport->pinctrl,
  473. ascport->states[NO_HW_FLOWCTRL]);
  474. gpiod = devm_fwnode_get_gpiod_from_child(port->dev,
  475. "rts",
  476. &np->fwnode,
  477. GPIOD_OUT_LOW,
  478. np->name);
  479. if (!IS_ERR(gpiod))
  480. ascport->rts = gpiod;
  481. }
  482. }
  483. if ((baud < 19200) && !ascport->force_m1) {
  484. asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
  485. } else {
  486. /*
  487. * MODE 1: recommended for high bit rates (above 19.2K)
  488. *
  489. * baudrate * 16 * 2^16
  490. * ASCBaudRate = ------------------------
  491. * inputclock
  492. *
  493. * To keep maths inside 64bits, we divide inputclock by 16.
  494. */
  495. u64 dividend = (u64)baud * (1 << 16);
  496. do_div(dividend, port->uartclk / 16);
  497. asc_out(port, ASC_BAUDRATE, dividend);
  498. ctrl_val |= ASC_CTL_BAUDMODE;
  499. }
  500. uart_update_timeout(port, cflag, baud);
  501. ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
  502. if (termios->c_iflag & INPCK)
  503. ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
  504. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  505. ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
  506. /*
  507. * Characters to ignore
  508. */
  509. ascport->port.ignore_status_mask = 0;
  510. if (termios->c_iflag & IGNPAR)
  511. ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
  512. if (termios->c_iflag & IGNBRK) {
  513. ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
  514. /*
  515. * If we're ignoring parity and break indicators,
  516. * ignore overruns too (for real raw support).
  517. */
  518. if (termios->c_iflag & IGNPAR)
  519. ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
  520. }
  521. /*
  522. * Ignore all characters if CREAD is not set.
  523. */
  524. if (!(termios->c_cflag & CREAD))
  525. ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
  526. /* Set the timeout */
  527. asc_out(port, ASC_TIMEOUT, 20);
  528. /* write final value and enable port */
  529. asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
  530. spin_unlock_irqrestore(&port->lock, flags);
  531. }
  532. static const char *asc_type(struct uart_port *port)
  533. {
  534. return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
  535. }
  536. static void asc_release_port(struct uart_port *port)
  537. {
  538. }
  539. static int asc_request_port(struct uart_port *port)
  540. {
  541. return 0;
  542. }
  543. /*
  544. * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
  545. * Set type field if successful
  546. */
  547. static void asc_config_port(struct uart_port *port, int flags)
  548. {
  549. if ((flags & UART_CONFIG_TYPE))
  550. port->type = PORT_ASC;
  551. }
  552. static int
  553. asc_verify_port(struct uart_port *port, struct serial_struct *ser)
  554. {
  555. /* No user changeable parameters */
  556. return -EINVAL;
  557. }
  558. #ifdef CONFIG_CONSOLE_POLL
  559. /*
  560. * Console polling routines for writing and reading from the uart while
  561. * in an interrupt or debug context (i.e. kgdb).
  562. */
  563. static int asc_get_poll_char(struct uart_port *port)
  564. {
  565. if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
  566. return NO_POLL_CHAR;
  567. return asc_in(port, ASC_RXBUF);
  568. }
  569. static void asc_put_poll_char(struct uart_port *port, unsigned char c)
  570. {
  571. while (!asc_txfifo_is_half_empty(port))
  572. cpu_relax();
  573. asc_out(port, ASC_TXBUF, c);
  574. }
  575. #endif /* CONFIG_CONSOLE_POLL */
  576. /*---------------------------------------------------------------------*/
  577. static const struct uart_ops asc_uart_ops = {
  578. .tx_empty = asc_tx_empty,
  579. .set_mctrl = asc_set_mctrl,
  580. .get_mctrl = asc_get_mctrl,
  581. .start_tx = asc_start_tx,
  582. .stop_tx = asc_stop_tx,
  583. .stop_rx = asc_stop_rx,
  584. .break_ctl = asc_break_ctl,
  585. .startup = asc_startup,
  586. .shutdown = asc_shutdown,
  587. .set_termios = asc_set_termios,
  588. .type = asc_type,
  589. .release_port = asc_release_port,
  590. .request_port = asc_request_port,
  591. .config_port = asc_config_port,
  592. .verify_port = asc_verify_port,
  593. .pm = asc_pm,
  594. #ifdef CONFIG_CONSOLE_POLL
  595. .poll_get_char = asc_get_poll_char,
  596. .poll_put_char = asc_put_poll_char,
  597. #endif /* CONFIG_CONSOLE_POLL */
  598. };
  599. static int asc_init_port(struct asc_port *ascport,
  600. struct platform_device *pdev)
  601. {
  602. struct uart_port *port = &ascport->port;
  603. struct resource *res;
  604. int ret;
  605. port->iotype = UPIO_MEM;
  606. port->flags = UPF_BOOT_AUTOCONF;
  607. port->ops = &asc_uart_ops;
  608. port->fifosize = ASC_FIFO_SIZE;
  609. port->dev = &pdev->dev;
  610. port->irq = platform_get_irq(pdev, 0);
  611. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  612. port->membase = devm_ioremap_resource(&pdev->dev, res);
  613. if (IS_ERR(port->membase))
  614. return PTR_ERR(port->membase);
  615. port->mapbase = res->start;
  616. spin_lock_init(&port->lock);
  617. ascport->clk = devm_clk_get(&pdev->dev, NULL);
  618. if (WARN_ON(IS_ERR(ascport->clk)))
  619. return -EINVAL;
  620. /* ensure that clk rate is correct by enabling the clk */
  621. clk_prepare_enable(ascport->clk);
  622. ascport->port.uartclk = clk_get_rate(ascport->clk);
  623. WARN_ON(ascport->port.uartclk == 0);
  624. clk_disable_unprepare(ascport->clk);
  625. ascport->pinctrl = devm_pinctrl_get(&pdev->dev);
  626. if (IS_ERR(ascport->pinctrl)) {
  627. ret = PTR_ERR(ascport->pinctrl);
  628. dev_err(&pdev->dev, "Failed to get Pinctrl: %d\n", ret);
  629. }
  630. ascport->states[DEFAULT] =
  631. pinctrl_lookup_state(ascport->pinctrl, "default");
  632. if (IS_ERR(ascport->states[DEFAULT])) {
  633. ret = PTR_ERR(ascport->states[DEFAULT]);
  634. dev_err(&pdev->dev,
  635. "Failed to look up Pinctrl state 'default': %d\n", ret);
  636. return ret;
  637. }
  638. /* "no-hw-flowctrl" state is optional */
  639. ascport->states[NO_HW_FLOWCTRL] =
  640. pinctrl_lookup_state(ascport->pinctrl, "no-hw-flowctrl");
  641. if (IS_ERR(ascport->states[NO_HW_FLOWCTRL]))
  642. ascport->states[NO_HW_FLOWCTRL] = NULL;
  643. return 0;
  644. }
  645. static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
  646. {
  647. struct device_node *np = pdev->dev.of_node;
  648. int id;
  649. if (!np)
  650. return NULL;
  651. id = of_alias_get_id(np, ASC_SERIAL_NAME);
  652. if (id < 0)
  653. id = 0;
  654. if (WARN_ON(id >= ASC_MAX_PORTS))
  655. return NULL;
  656. asc_ports[id].hw_flow_control = of_property_read_bool(np,
  657. "uart-has-rtscts");
  658. asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1");
  659. asc_ports[id].port.line = id;
  660. asc_ports[id].rts = NULL;
  661. return &asc_ports[id];
  662. }
  663. #ifdef CONFIG_OF
  664. static const struct of_device_id asc_match[] = {
  665. { .compatible = "st,asc", },
  666. {},
  667. };
  668. MODULE_DEVICE_TABLE(of, asc_match);
  669. #endif
  670. static int asc_serial_probe(struct platform_device *pdev)
  671. {
  672. int ret;
  673. struct asc_port *ascport;
  674. ascport = asc_of_get_asc_port(pdev);
  675. if (!ascport)
  676. return -ENODEV;
  677. ret = asc_init_port(ascport, pdev);
  678. if (ret)
  679. return ret;
  680. ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
  681. if (ret)
  682. return ret;
  683. platform_set_drvdata(pdev, &ascport->port);
  684. return 0;
  685. }
  686. static int asc_serial_remove(struct platform_device *pdev)
  687. {
  688. struct uart_port *port = platform_get_drvdata(pdev);
  689. return uart_remove_one_port(&asc_uart_driver, port);
  690. }
  691. #ifdef CONFIG_PM_SLEEP
  692. static int asc_serial_suspend(struct device *dev)
  693. {
  694. struct platform_device *pdev = to_platform_device(dev);
  695. struct uart_port *port = platform_get_drvdata(pdev);
  696. return uart_suspend_port(&asc_uart_driver, port);
  697. }
  698. static int asc_serial_resume(struct device *dev)
  699. {
  700. struct platform_device *pdev = to_platform_device(dev);
  701. struct uart_port *port = platform_get_drvdata(pdev);
  702. return uart_resume_port(&asc_uart_driver, port);
  703. }
  704. #endif /* CONFIG_PM_SLEEP */
  705. /*----------------------------------------------------------------------*/
  706. #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
  707. static void asc_console_putchar(struct uart_port *port, int ch)
  708. {
  709. unsigned int timeout = 1000000;
  710. /* Wait for upto 1 second in case flow control is stopping us. */
  711. while (--timeout && !asc_txfifo_is_half_empty(port))
  712. udelay(1);
  713. asc_out(port, ASC_TXBUF, ch);
  714. }
  715. /*
  716. * Print a string to the serial port trying not to disturb
  717. * any possible real use of the port...
  718. */
  719. static void asc_console_write(struct console *co, const char *s, unsigned count)
  720. {
  721. struct uart_port *port = &asc_ports[co->index].port;
  722. unsigned long flags;
  723. unsigned long timeout = 1000000;
  724. int locked = 1;
  725. u32 intenable;
  726. if (port->sysrq)
  727. locked = 0; /* asc_interrupt has already claimed the lock */
  728. else if (oops_in_progress)
  729. locked = spin_trylock_irqsave(&port->lock, flags);
  730. else
  731. spin_lock_irqsave(&port->lock, flags);
  732. /*
  733. * Disable interrupts so we don't get the IRQ line bouncing
  734. * up and down while interrupts are disabled.
  735. */
  736. intenable = asc_in(port, ASC_INTEN);
  737. asc_out(port, ASC_INTEN, 0);
  738. (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
  739. uart_console_write(port, s, count, asc_console_putchar);
  740. while (--timeout && !asc_txfifo_is_empty(port))
  741. udelay(1);
  742. asc_out(port, ASC_INTEN, intenable);
  743. if (locked)
  744. spin_unlock_irqrestore(&port->lock, flags);
  745. }
  746. static int asc_console_setup(struct console *co, char *options)
  747. {
  748. struct asc_port *ascport;
  749. int baud = 115200;
  750. int bits = 8;
  751. int parity = 'n';
  752. int flow = 'n';
  753. if (co->index >= ASC_MAX_PORTS)
  754. return -ENODEV;
  755. ascport = &asc_ports[co->index];
  756. /*
  757. * This driver does not support early console initialization
  758. * (use ARM early printk support instead), so we only expect
  759. * this to be called during the uart port registration when the
  760. * driver gets probed and the port should be mapped at that point.
  761. */
  762. if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
  763. return -ENXIO;
  764. if (options)
  765. uart_parse_options(options, &baud, &parity, &bits, &flow);
  766. return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
  767. }
  768. static struct console asc_console = {
  769. .name = ASC_SERIAL_NAME,
  770. .device = uart_console_device,
  771. .write = asc_console_write,
  772. .setup = asc_console_setup,
  773. .flags = CON_PRINTBUFFER,
  774. .index = -1,
  775. .data = &asc_uart_driver,
  776. };
  777. #define ASC_SERIAL_CONSOLE (&asc_console)
  778. #else
  779. #define ASC_SERIAL_CONSOLE NULL
  780. #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
  781. static struct uart_driver asc_uart_driver = {
  782. .owner = THIS_MODULE,
  783. .driver_name = DRIVER_NAME,
  784. .dev_name = ASC_SERIAL_NAME,
  785. .major = 0,
  786. .minor = 0,
  787. .nr = ASC_MAX_PORTS,
  788. .cons = ASC_SERIAL_CONSOLE,
  789. };
  790. static const struct dev_pm_ops asc_serial_pm_ops = {
  791. SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
  792. };
  793. static struct platform_driver asc_serial_driver = {
  794. .probe = asc_serial_probe,
  795. .remove = asc_serial_remove,
  796. .driver = {
  797. .name = DRIVER_NAME,
  798. .pm = &asc_serial_pm_ops,
  799. .of_match_table = of_match_ptr(asc_match),
  800. },
  801. };
  802. static int __init asc_init(void)
  803. {
  804. int ret;
  805. static const char banner[] __initconst =
  806. KERN_INFO "STMicroelectronics ASC driver initialized\n";
  807. printk(banner);
  808. ret = uart_register_driver(&asc_uart_driver);
  809. if (ret)
  810. return ret;
  811. ret = platform_driver_register(&asc_serial_driver);
  812. if (ret)
  813. uart_unregister_driver(&asc_uart_driver);
  814. return ret;
  815. }
  816. static void __exit asc_exit(void)
  817. {
  818. platform_driver_unregister(&asc_serial_driver);
  819. uart_unregister_driver(&asc_uart_driver);
  820. }
  821. module_init(asc_init);
  822. module_exit(asc_exit);
  823. MODULE_ALIAS("platform:" DRIVER_NAME);
  824. MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
  825. MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
  826. MODULE_LICENSE("GPL");