st-asc.c 24 KB

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