sprd_serial.c 18 KB

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