st-asc.c 23 KB

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