sprd_serial.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2012-2015 Spreadtrum Communications Inc.
  4. */
  5. #if defined(CONFIG_SERIAL_SPRD_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  6. #define SUPPORT_SYSRQ
  7. #endif
  8. #include <linux/clk.h>
  9. #include <linux/console.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/ioport.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/serial.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_flip.h>
  22. /* device name */
  23. #define UART_NR_MAX 8
  24. #define SPRD_TTY_NAME "ttyS"
  25. #define SPRD_FIFO_SIZE 128
  26. #define SPRD_DEF_RATE 26000000
  27. #define SPRD_BAUD_IO_LIMIT 3000000
  28. #define SPRD_TIMEOUT 256000
  29. /* the offset of serial registers and BITs for them */
  30. /* data registers */
  31. #define SPRD_TXD 0x0000
  32. #define SPRD_RXD 0x0004
  33. /* line status register and its BITs */
  34. #define SPRD_LSR 0x0008
  35. #define SPRD_LSR_OE BIT(4)
  36. #define SPRD_LSR_FE BIT(3)
  37. #define SPRD_LSR_PE BIT(2)
  38. #define SPRD_LSR_BI BIT(7)
  39. #define SPRD_LSR_TX_OVER BIT(15)
  40. /* data number in TX and RX fifo */
  41. #define SPRD_STS1 0x000C
  42. /* interrupt enable register and its BITs */
  43. #define SPRD_IEN 0x0010
  44. #define SPRD_IEN_RX_FULL BIT(0)
  45. #define SPRD_IEN_TX_EMPTY BIT(1)
  46. #define SPRD_IEN_BREAK_DETECT BIT(7)
  47. #define SPRD_IEN_TIMEOUT BIT(13)
  48. /* interrupt clear register */
  49. #define SPRD_ICLR 0x0014
  50. #define SPRD_ICLR_TIMEOUT BIT(13)
  51. /* line control register */
  52. #define SPRD_LCR 0x0018
  53. #define SPRD_LCR_STOP_1BIT 0x10
  54. #define SPRD_LCR_STOP_2BIT 0x30
  55. #define SPRD_LCR_DATA_LEN (BIT(2) | BIT(3))
  56. #define SPRD_LCR_DATA_LEN5 0x0
  57. #define SPRD_LCR_DATA_LEN6 0x4
  58. #define SPRD_LCR_DATA_LEN7 0x8
  59. #define SPRD_LCR_DATA_LEN8 0xc
  60. #define SPRD_LCR_PARITY (BIT(0) | BIT(1))
  61. #define SPRD_LCR_PARITY_EN 0x2
  62. #define SPRD_LCR_EVEN_PAR 0x0
  63. #define SPRD_LCR_ODD_PAR 0x1
  64. /* control register 1 */
  65. #define SPRD_CTL1 0x001C
  66. #define RX_HW_FLOW_CTL_THLD BIT(6)
  67. #define RX_HW_FLOW_CTL_EN BIT(7)
  68. #define TX_HW_FLOW_CTL_EN BIT(8)
  69. #define RX_TOUT_THLD_DEF 0x3E00
  70. #define RX_HFC_THLD_DEF 0x40
  71. /* fifo threshold register */
  72. #define SPRD_CTL2 0x0020
  73. #define THLD_TX_EMPTY 0x40
  74. #define THLD_RX_FULL 0x40
  75. /* config baud rate register */
  76. #define SPRD_CLKD0 0x0024
  77. #define SPRD_CLKD1 0x0028
  78. /* interrupt mask status register */
  79. #define SPRD_IMSR 0x002C
  80. #define SPRD_IMSR_RX_FIFO_FULL BIT(0)
  81. #define SPRD_IMSR_TX_FIFO_EMPTY BIT(1)
  82. #define SPRD_IMSR_BREAK_DETECT BIT(7)
  83. #define SPRD_IMSR_TIMEOUT BIT(13)
  84. struct reg_backup {
  85. u32 ien;
  86. u32 ctrl0;
  87. u32 ctrl1;
  88. u32 ctrl2;
  89. u32 clkd0;
  90. u32 clkd1;
  91. u32 dspwait;
  92. };
  93. struct sprd_uart_port {
  94. struct uart_port port;
  95. struct reg_backup reg_bak;
  96. char name[16];
  97. };
  98. static struct sprd_uart_port *sprd_port[UART_NR_MAX];
  99. static int sprd_ports_num;
  100. static inline unsigned int serial_in(struct uart_port *port, int offset)
  101. {
  102. return readl_relaxed(port->membase + offset);
  103. }
  104. static inline void serial_out(struct uart_port *port, int offset, int value)
  105. {
  106. writel_relaxed(value, port->membase + offset);
  107. }
  108. static unsigned int sprd_tx_empty(struct uart_port *port)
  109. {
  110. if (serial_in(port, SPRD_STS1) & 0xff00)
  111. return 0;
  112. else
  113. return TIOCSER_TEMT;
  114. }
  115. static unsigned int sprd_get_mctrl(struct uart_port *port)
  116. {
  117. return TIOCM_DSR | TIOCM_CTS;
  118. }
  119. static void sprd_set_mctrl(struct uart_port *port, unsigned int mctrl)
  120. {
  121. /* nothing to do */
  122. }
  123. static void sprd_stop_tx(struct uart_port *port)
  124. {
  125. unsigned int ien, iclr;
  126. iclr = serial_in(port, SPRD_ICLR);
  127. ien = serial_in(port, SPRD_IEN);
  128. iclr |= SPRD_IEN_TX_EMPTY;
  129. ien &= ~SPRD_IEN_TX_EMPTY;
  130. serial_out(port, SPRD_ICLR, iclr);
  131. serial_out(port, SPRD_IEN, ien);
  132. }
  133. static void sprd_start_tx(struct uart_port *port)
  134. {
  135. unsigned int ien;
  136. ien = serial_in(port, SPRD_IEN);
  137. if (!(ien & SPRD_IEN_TX_EMPTY)) {
  138. ien |= SPRD_IEN_TX_EMPTY;
  139. serial_out(port, SPRD_IEN, ien);
  140. }
  141. }
  142. static void sprd_stop_rx(struct uart_port *port)
  143. {
  144. unsigned int ien, iclr;
  145. iclr = serial_in(port, SPRD_ICLR);
  146. ien = serial_in(port, SPRD_IEN);
  147. ien &= ~(SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT);
  148. iclr |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT;
  149. serial_out(port, SPRD_IEN, ien);
  150. serial_out(port, SPRD_ICLR, iclr);
  151. }
  152. /* The Sprd serial does not support this function. */
  153. static void sprd_break_ctl(struct uart_port *port, int break_state)
  154. {
  155. /* nothing to do */
  156. }
  157. static int handle_lsr_errors(struct uart_port *port,
  158. unsigned int *flag,
  159. unsigned int *lsr)
  160. {
  161. int ret = 0;
  162. /* statistics */
  163. if (*lsr & SPRD_LSR_BI) {
  164. *lsr &= ~(SPRD_LSR_FE | SPRD_LSR_PE);
  165. port->icount.brk++;
  166. ret = uart_handle_break(port);
  167. if (ret)
  168. return ret;
  169. } else if (*lsr & SPRD_LSR_PE)
  170. port->icount.parity++;
  171. else if (*lsr & SPRD_LSR_FE)
  172. port->icount.frame++;
  173. if (*lsr & SPRD_LSR_OE)
  174. port->icount.overrun++;
  175. /* mask off conditions which should be ignored */
  176. *lsr &= port->read_status_mask;
  177. if (*lsr & SPRD_LSR_BI)
  178. *flag = TTY_BREAK;
  179. else if (*lsr & SPRD_LSR_PE)
  180. *flag = TTY_PARITY;
  181. else if (*lsr & SPRD_LSR_FE)
  182. *flag = TTY_FRAME;
  183. return ret;
  184. }
  185. static inline void sprd_rx(struct uart_port *port)
  186. {
  187. struct tty_port *tty = &port->state->port;
  188. unsigned int ch, flag, lsr, max_count = SPRD_TIMEOUT;
  189. while ((serial_in(port, SPRD_STS1) & 0x00ff) && max_count--) {
  190. lsr = serial_in(port, SPRD_LSR);
  191. ch = serial_in(port, SPRD_RXD);
  192. flag = TTY_NORMAL;
  193. port->icount.rx++;
  194. if (lsr & (SPRD_LSR_BI | SPRD_LSR_PE |
  195. SPRD_LSR_FE | SPRD_LSR_OE))
  196. if (handle_lsr_errors(port, &lsr, &flag))
  197. continue;
  198. if (uart_handle_sysrq_char(port, ch))
  199. continue;
  200. uart_insert_char(port, lsr, SPRD_LSR_OE, ch, flag);
  201. }
  202. tty_flip_buffer_push(tty);
  203. }
  204. static inline void sprd_tx(struct uart_port *port)
  205. {
  206. struct circ_buf *xmit = &port->state->xmit;
  207. int count;
  208. if (port->x_char) {
  209. serial_out(port, SPRD_TXD, port->x_char);
  210. port->icount.tx++;
  211. port->x_char = 0;
  212. return;
  213. }
  214. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  215. sprd_stop_tx(port);
  216. return;
  217. }
  218. count = THLD_TX_EMPTY;
  219. do {
  220. serial_out(port, SPRD_TXD, xmit->buf[xmit->tail]);
  221. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  222. port->icount.tx++;
  223. if (uart_circ_empty(xmit))
  224. break;
  225. } while (--count > 0);
  226. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  227. uart_write_wakeup(port);
  228. if (uart_circ_empty(xmit))
  229. sprd_stop_tx(port);
  230. }
  231. /* this handles the interrupt from one port */
  232. static irqreturn_t sprd_handle_irq(int irq, void *dev_id)
  233. {
  234. struct uart_port *port = dev_id;
  235. unsigned int ims;
  236. spin_lock(&port->lock);
  237. ims = serial_in(port, SPRD_IMSR);
  238. if (!ims) {
  239. spin_unlock(&port->lock);
  240. return IRQ_NONE;
  241. }
  242. if (ims & SPRD_IMSR_TIMEOUT)
  243. serial_out(port, SPRD_ICLR, SPRD_ICLR_TIMEOUT);
  244. if (ims & (SPRD_IMSR_RX_FIFO_FULL |
  245. SPRD_IMSR_BREAK_DETECT | SPRD_IMSR_TIMEOUT))
  246. sprd_rx(port);
  247. if (ims & SPRD_IMSR_TX_FIFO_EMPTY)
  248. sprd_tx(port);
  249. spin_unlock(&port->lock);
  250. return IRQ_HANDLED;
  251. }
  252. static int sprd_startup(struct uart_port *port)
  253. {
  254. int ret = 0;
  255. unsigned int ien, fc;
  256. unsigned int timeout;
  257. struct sprd_uart_port *sp;
  258. unsigned long flags;
  259. serial_out(port, SPRD_CTL2, ((THLD_TX_EMPTY << 8) | THLD_RX_FULL));
  260. /* clear rx fifo */
  261. timeout = SPRD_TIMEOUT;
  262. while (timeout-- && serial_in(port, SPRD_STS1) & 0x00ff)
  263. serial_in(port, SPRD_RXD);
  264. /* clear tx fifo */
  265. timeout = SPRD_TIMEOUT;
  266. while (timeout-- && serial_in(port, SPRD_STS1) & 0xff00)
  267. cpu_relax();
  268. /* clear interrupt */
  269. serial_out(port, SPRD_IEN, 0);
  270. serial_out(port, SPRD_ICLR, ~0);
  271. /* allocate irq */
  272. sp = container_of(port, struct sprd_uart_port, port);
  273. snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line);
  274. ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq,
  275. IRQF_SHARED, sp->name, port);
  276. if (ret) {
  277. dev_err(port->dev, "fail to request serial irq %d, ret=%d\n",
  278. port->irq, ret);
  279. return ret;
  280. }
  281. fc = serial_in(port, SPRD_CTL1);
  282. fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
  283. serial_out(port, SPRD_CTL1, fc);
  284. /* enable interrupt */
  285. spin_lock_irqsave(&port->lock, flags);
  286. ien = serial_in(port, SPRD_IEN);
  287. ien |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT | SPRD_IEN_TIMEOUT;
  288. serial_out(port, SPRD_IEN, ien);
  289. spin_unlock_irqrestore(&port->lock, flags);
  290. return 0;
  291. }
  292. static void sprd_shutdown(struct uart_port *port)
  293. {
  294. serial_out(port, SPRD_IEN, 0);
  295. serial_out(port, SPRD_ICLR, ~0);
  296. devm_free_irq(port->dev, port->irq, port);
  297. }
  298. static void sprd_set_termios(struct uart_port *port,
  299. struct ktermios *termios,
  300. struct ktermios *old)
  301. {
  302. unsigned int baud, quot;
  303. unsigned int lcr = 0, fc;
  304. unsigned long flags;
  305. /* ask the core to calculate the divisor for us */
  306. baud = uart_get_baud_rate(port, termios, old, 0, SPRD_BAUD_IO_LIMIT);
  307. quot = (unsigned int)((port->uartclk + baud / 2) / baud);
  308. /* set data length */
  309. switch (termios->c_cflag & CSIZE) {
  310. case CS5:
  311. lcr |= SPRD_LCR_DATA_LEN5;
  312. break;
  313. case CS6:
  314. lcr |= SPRD_LCR_DATA_LEN6;
  315. break;
  316. case CS7:
  317. lcr |= SPRD_LCR_DATA_LEN7;
  318. break;
  319. case CS8:
  320. default:
  321. lcr |= SPRD_LCR_DATA_LEN8;
  322. break;
  323. }
  324. /* calculate stop bits */
  325. lcr &= ~(SPRD_LCR_STOP_1BIT | SPRD_LCR_STOP_2BIT);
  326. if (termios->c_cflag & CSTOPB)
  327. lcr |= SPRD_LCR_STOP_2BIT;
  328. else
  329. lcr |= SPRD_LCR_STOP_1BIT;
  330. /* calculate parity */
  331. lcr &= ~SPRD_LCR_PARITY;
  332. termios->c_cflag &= ~CMSPAR; /* no support mark/space */
  333. if (termios->c_cflag & PARENB) {
  334. lcr |= SPRD_LCR_PARITY_EN;
  335. if (termios->c_cflag & PARODD)
  336. lcr |= SPRD_LCR_ODD_PAR;
  337. else
  338. lcr |= SPRD_LCR_EVEN_PAR;
  339. }
  340. spin_lock_irqsave(&port->lock, flags);
  341. /* update the per-port timeout */
  342. uart_update_timeout(port, termios->c_cflag, baud);
  343. port->read_status_mask = SPRD_LSR_OE;
  344. if (termios->c_iflag & INPCK)
  345. port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE;
  346. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  347. port->read_status_mask |= SPRD_LSR_BI;
  348. /* characters to ignore */
  349. port->ignore_status_mask = 0;
  350. if (termios->c_iflag & IGNPAR)
  351. port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE;
  352. if (termios->c_iflag & IGNBRK) {
  353. port->ignore_status_mask |= SPRD_LSR_BI;
  354. /*
  355. * If we're ignoring parity and break indicators,
  356. * ignore overruns too (for real raw support).
  357. */
  358. if (termios->c_iflag & IGNPAR)
  359. port->ignore_status_mask |= SPRD_LSR_OE;
  360. }
  361. /* flow control */
  362. fc = serial_in(port, SPRD_CTL1);
  363. fc &= ~(RX_HW_FLOW_CTL_THLD | RX_HW_FLOW_CTL_EN | TX_HW_FLOW_CTL_EN);
  364. if (termios->c_cflag & CRTSCTS) {
  365. fc |= RX_HW_FLOW_CTL_THLD;
  366. fc |= RX_HW_FLOW_CTL_EN;
  367. fc |= TX_HW_FLOW_CTL_EN;
  368. }
  369. /* clock divider bit0~bit15 */
  370. serial_out(port, SPRD_CLKD0, quot & 0xffff);
  371. /* clock divider bit16~bit20 */
  372. serial_out(port, SPRD_CLKD1, (quot & 0x1f0000) >> 16);
  373. serial_out(port, SPRD_LCR, lcr);
  374. fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF;
  375. serial_out(port, SPRD_CTL1, fc);
  376. spin_unlock_irqrestore(&port->lock, flags);
  377. /* Don't rewrite B0 */
  378. if (tty_termios_baud_rate(termios))
  379. tty_termios_encode_baud_rate(termios, baud, baud);
  380. }
  381. static const char *sprd_type(struct uart_port *port)
  382. {
  383. return "SPX";
  384. }
  385. static void sprd_release_port(struct uart_port *port)
  386. {
  387. /* nothing to do */
  388. }
  389. static int sprd_request_port(struct uart_port *port)
  390. {
  391. return 0;
  392. }
  393. static void sprd_config_port(struct uart_port *port, int flags)
  394. {
  395. if (flags & UART_CONFIG_TYPE)
  396. port->type = PORT_SPRD;
  397. }
  398. static int sprd_verify_port(struct uart_port *port,
  399. struct serial_struct *ser)
  400. {
  401. if (ser->type != PORT_SPRD)
  402. return -EINVAL;
  403. if (port->irq != ser->irq)
  404. return -EINVAL;
  405. if (port->iotype != ser->io_type)
  406. return -EINVAL;
  407. return 0;
  408. }
  409. static const struct uart_ops serial_sprd_ops = {
  410. .tx_empty = sprd_tx_empty,
  411. .get_mctrl = sprd_get_mctrl,
  412. .set_mctrl = sprd_set_mctrl,
  413. .stop_tx = sprd_stop_tx,
  414. .start_tx = sprd_start_tx,
  415. .stop_rx = sprd_stop_rx,
  416. .break_ctl = sprd_break_ctl,
  417. .startup = sprd_startup,
  418. .shutdown = sprd_shutdown,
  419. .set_termios = sprd_set_termios,
  420. .type = sprd_type,
  421. .release_port = sprd_release_port,
  422. .request_port = sprd_request_port,
  423. .config_port = sprd_config_port,
  424. .verify_port = sprd_verify_port,
  425. };
  426. #ifdef CONFIG_SERIAL_SPRD_CONSOLE
  427. static void wait_for_xmitr(struct uart_port *port)
  428. {
  429. unsigned int status, tmout = 10000;
  430. /* wait up to 10ms for the character(s) to be sent */
  431. do {
  432. status = serial_in(port, SPRD_STS1);
  433. if (--tmout == 0)
  434. break;
  435. udelay(1);
  436. } while (status & 0xff00);
  437. }
  438. static void sprd_console_putchar(struct uart_port *port, int ch)
  439. {
  440. wait_for_xmitr(port);
  441. serial_out(port, SPRD_TXD, ch);
  442. }
  443. static void sprd_console_write(struct console *co, const char *s,
  444. unsigned int count)
  445. {
  446. struct uart_port *port = &sprd_port[co->index]->port;
  447. int locked = 1;
  448. unsigned long flags;
  449. if (port->sysrq)
  450. locked = 0;
  451. else if (oops_in_progress)
  452. locked = spin_trylock_irqsave(&port->lock, flags);
  453. else
  454. spin_lock_irqsave(&port->lock, flags);
  455. uart_console_write(port, s, count, sprd_console_putchar);
  456. /* wait for transmitter to become empty */
  457. wait_for_xmitr(port);
  458. if (locked)
  459. spin_unlock_irqrestore(&port->lock, flags);
  460. }
  461. static int __init sprd_console_setup(struct console *co, char *options)
  462. {
  463. struct uart_port *port;
  464. int baud = 115200;
  465. int bits = 8;
  466. int parity = 'n';
  467. int flow = 'n';
  468. if (co->index >= UART_NR_MAX || co->index < 0)
  469. co->index = 0;
  470. port = &sprd_port[co->index]->port;
  471. if (port == NULL) {
  472. pr_info("serial port %d not yet initialized\n", co->index);
  473. return -ENODEV;
  474. }
  475. if (options)
  476. uart_parse_options(options, &baud, &parity, &bits, &flow);
  477. return uart_set_options(port, co, baud, parity, bits, flow);
  478. }
  479. static struct uart_driver sprd_uart_driver;
  480. static struct console sprd_console = {
  481. .name = SPRD_TTY_NAME,
  482. .write = sprd_console_write,
  483. .device = uart_console_device,
  484. .setup = sprd_console_setup,
  485. .flags = CON_PRINTBUFFER,
  486. .index = -1,
  487. .data = &sprd_uart_driver,
  488. };
  489. #define SPRD_CONSOLE (&sprd_console)
  490. /* Support for earlycon */
  491. static void sprd_putc(struct uart_port *port, int c)
  492. {
  493. unsigned int timeout = SPRD_TIMEOUT;
  494. while (timeout-- &&
  495. !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER))
  496. cpu_relax();
  497. writeb(c, port->membase + SPRD_TXD);
  498. }
  499. static void sprd_early_write(struct console *con, const char *s,
  500. unsigned n)
  501. {
  502. struct earlycon_device *dev = con->data;
  503. uart_console_write(&dev->port, s, n, sprd_putc);
  504. }
  505. static int __init sprd_early_console_setup(
  506. struct earlycon_device *device,
  507. const char *opt)
  508. {
  509. if (!device->port.membase)
  510. return -ENODEV;
  511. device->con->write = sprd_early_write;
  512. return 0;
  513. }
  514. OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart",
  515. sprd_early_console_setup);
  516. #else /* !CONFIG_SERIAL_SPRD_CONSOLE */
  517. #define SPRD_CONSOLE NULL
  518. #endif
  519. static struct uart_driver sprd_uart_driver = {
  520. .owner = THIS_MODULE,
  521. .driver_name = "sprd_serial",
  522. .dev_name = SPRD_TTY_NAME,
  523. .major = 0,
  524. .minor = 0,
  525. .nr = UART_NR_MAX,
  526. .cons = SPRD_CONSOLE,
  527. };
  528. static int sprd_probe_dt_alias(int index, struct device *dev)
  529. {
  530. struct device_node *np;
  531. int ret = index;
  532. if (!IS_ENABLED(CONFIG_OF))
  533. return ret;
  534. np = dev->of_node;
  535. if (!np)
  536. return ret;
  537. ret = of_alias_get_id(np, "serial");
  538. if (ret < 0)
  539. ret = index;
  540. else if (ret >= ARRAY_SIZE(sprd_port) || sprd_port[ret] != NULL) {
  541. dev_warn(dev, "requested serial port %d not available.\n", ret);
  542. ret = index;
  543. }
  544. return ret;
  545. }
  546. static int sprd_remove(struct platform_device *dev)
  547. {
  548. struct sprd_uart_port *sup = platform_get_drvdata(dev);
  549. if (sup) {
  550. uart_remove_one_port(&sprd_uart_driver, &sup->port);
  551. sprd_port[sup->port.line] = NULL;
  552. sprd_ports_num--;
  553. }
  554. if (!sprd_ports_num)
  555. uart_unregister_driver(&sprd_uart_driver);
  556. return 0;
  557. }
  558. static int sprd_probe(struct platform_device *pdev)
  559. {
  560. struct resource *res;
  561. struct uart_port *up;
  562. struct clk *clk;
  563. int irq;
  564. int index;
  565. int ret;
  566. for (index = 0; index < ARRAY_SIZE(sprd_port); index++)
  567. if (sprd_port[index] == NULL)
  568. break;
  569. if (index == ARRAY_SIZE(sprd_port))
  570. return -EBUSY;
  571. index = sprd_probe_dt_alias(index, &pdev->dev);
  572. sprd_port[index] = devm_kzalloc(&pdev->dev,
  573. sizeof(*sprd_port[index]), GFP_KERNEL);
  574. if (!sprd_port[index])
  575. return -ENOMEM;
  576. up = &sprd_port[index]->port;
  577. up->dev = &pdev->dev;
  578. up->line = index;
  579. up->type = PORT_SPRD;
  580. up->iotype = UPIO_MEM;
  581. up->uartclk = SPRD_DEF_RATE;
  582. up->fifosize = SPRD_FIFO_SIZE;
  583. up->ops = &serial_sprd_ops;
  584. up->flags = UPF_BOOT_AUTOCONF;
  585. clk = devm_clk_get(&pdev->dev, NULL);
  586. if (!IS_ERR_OR_NULL(clk))
  587. up->uartclk = clk_get_rate(clk);
  588. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. if (!res) {
  590. dev_err(&pdev->dev, "not provide mem resource\n");
  591. return -ENODEV;
  592. }
  593. up->mapbase = res->start;
  594. up->membase = devm_ioremap_resource(&pdev->dev, res);
  595. if (IS_ERR(up->membase))
  596. return PTR_ERR(up->membase);
  597. irq = platform_get_irq(pdev, 0);
  598. if (irq < 0) {
  599. dev_err(&pdev->dev, "not provide irq resource: %d\n", irq);
  600. return irq;
  601. }
  602. up->irq = irq;
  603. if (!sprd_ports_num) {
  604. ret = uart_register_driver(&sprd_uart_driver);
  605. if (ret < 0) {
  606. pr_err("Failed to register SPRD-UART driver\n");
  607. return ret;
  608. }
  609. }
  610. sprd_ports_num++;
  611. ret = uart_add_one_port(&sprd_uart_driver, up);
  612. if (ret) {
  613. sprd_port[index] = NULL;
  614. sprd_remove(pdev);
  615. }
  616. platform_set_drvdata(pdev, up);
  617. return ret;
  618. }
  619. #ifdef CONFIG_PM_SLEEP
  620. static int sprd_suspend(struct device *dev)
  621. {
  622. struct sprd_uart_port *sup = dev_get_drvdata(dev);
  623. uart_suspend_port(&sprd_uart_driver, &sup->port);
  624. return 0;
  625. }
  626. static int sprd_resume(struct device *dev)
  627. {
  628. struct sprd_uart_port *sup = dev_get_drvdata(dev);
  629. uart_resume_port(&sprd_uart_driver, &sup->port);
  630. return 0;
  631. }
  632. #endif
  633. static SIMPLE_DEV_PM_OPS(sprd_pm_ops, sprd_suspend, sprd_resume);
  634. static const struct of_device_id serial_ids[] = {
  635. {.compatible = "sprd,sc9836-uart",},
  636. {}
  637. };
  638. MODULE_DEVICE_TABLE(of, serial_ids);
  639. static struct platform_driver sprd_platform_driver = {
  640. .probe = sprd_probe,
  641. .remove = sprd_remove,
  642. .driver = {
  643. .name = "sprd_serial",
  644. .of_match_table = of_match_ptr(serial_ids),
  645. .pm = &sprd_pm_ops,
  646. },
  647. };
  648. module_platform_driver(sprd_platform_driver);
  649. MODULE_LICENSE("GPL v2");
  650. MODULE_DESCRIPTION("Spreadtrum SoC serial driver series");