sprd_serial.c 18 KB

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