st-asc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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");