st-asc.c 24 KB

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