st-asc.c 24 KB

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