st-asc.c 22 KB

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