sirfsoc_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Driver for CSR SiRFprimaII onboard UARTs.
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/ioport.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/init.h>
  12. #include <linux/sysrq.h>
  13. #include <linux/console.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/serial_core.h>
  17. #include <linux/serial.h>
  18. #include <linux/clk.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/mach/irq.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include "sirfsoc_uart.h"
  26. static unsigned int
  27. sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count);
  28. static unsigned int
  29. sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
  30. static struct uart_driver sirfsoc_uart_drv;
  31. static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
  32. {4000000, 2359296},
  33. {3500000, 1310721},
  34. {3000000, 1572865},
  35. {2500000, 1245186},
  36. {2000000, 1572866},
  37. {1500000, 1245188},
  38. {1152000, 1638404},
  39. {1000000, 1572869},
  40. {921600, 1114120},
  41. {576000, 1245196},
  42. {500000, 1245198},
  43. {460800, 1572876},
  44. {230400, 1310750},
  45. {115200, 1310781},
  46. {57600, 1310843},
  47. {38400, 1114328},
  48. {19200, 1114545},
  49. {9600, 1114979},
  50. };
  51. static struct sirfsoc_uart_port sirfsoc_uart_ports[SIRFSOC_UART_NR] = {
  52. [0] = {
  53. .port = {
  54. .iotype = UPIO_MEM,
  55. .flags = UPF_BOOT_AUTOCONF,
  56. .line = 0,
  57. },
  58. },
  59. [1] = {
  60. .port = {
  61. .iotype = UPIO_MEM,
  62. .flags = UPF_BOOT_AUTOCONF,
  63. .line = 1,
  64. },
  65. },
  66. [2] = {
  67. .port = {
  68. .iotype = UPIO_MEM,
  69. .flags = UPF_BOOT_AUTOCONF,
  70. .line = 2,
  71. },
  72. },
  73. [3] = {
  74. .port = {
  75. .iotype = UPIO_MEM,
  76. .flags = UPF_BOOT_AUTOCONF,
  77. .line = 3,
  78. },
  79. },
  80. [4] = {
  81. .port = {
  82. .iotype = UPIO_MEM,
  83. .flags = UPF_BOOT_AUTOCONF,
  84. .line = 4,
  85. },
  86. },
  87. };
  88. static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
  89. {
  90. return container_of(port, struct sirfsoc_uart_port, port);
  91. }
  92. static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
  93. {
  94. unsigned long reg;
  95. reg = rd_regl(port, SIRFUART_TX_FIFO_STATUS);
  96. if (reg & SIRFUART_FIFOEMPTY_MASK(port))
  97. return TIOCSER_TEMT;
  98. else
  99. return 0;
  100. }
  101. static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
  102. {
  103. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  104. if (!(sirfport->ms_enabled)) {
  105. goto cts_asserted;
  106. } else if (sirfport->hw_flow_ctrl) {
  107. if (!(rd_regl(port, SIRFUART_AFC_CTRL) &
  108. SIRFUART_CTS_IN_STATUS))
  109. goto cts_asserted;
  110. else
  111. goto cts_deasserted;
  112. }
  113. cts_deasserted:
  114. return TIOCM_CAR | TIOCM_DSR;
  115. cts_asserted:
  116. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  117. }
  118. static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  119. {
  120. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  121. unsigned int assert = mctrl & TIOCM_RTS;
  122. unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
  123. unsigned int current_val;
  124. if (sirfport->hw_flow_ctrl) {
  125. current_val = rd_regl(port, SIRFUART_AFC_CTRL) & ~0xFF;
  126. val |= current_val;
  127. wr_regl(port, SIRFUART_AFC_CTRL, val);
  128. }
  129. }
  130. static void sirfsoc_uart_stop_tx(struct uart_port *port)
  131. {
  132. unsigned int regv;
  133. regv = rd_regl(port, SIRFUART_INT_EN);
  134. wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_TX_INT_EN);
  135. }
  136. void sirfsoc_uart_start_tx(struct uart_port *port)
  137. {
  138. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  139. unsigned long regv;
  140. sirfsoc_uart_pio_tx_chars(sirfport, 1);
  141. wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_START);
  142. regv = rd_regl(port, SIRFUART_INT_EN);
  143. wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_TX_INT_EN);
  144. }
  145. static void sirfsoc_uart_stop_rx(struct uart_port *port)
  146. {
  147. unsigned long regv;
  148. wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
  149. regv = rd_regl(port, SIRFUART_INT_EN);
  150. wr_regl(port, SIRFUART_INT_EN, regv & ~SIRFUART_RX_IO_INT_EN);
  151. }
  152. static void sirfsoc_uart_disable_ms(struct uart_port *port)
  153. {
  154. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  155. unsigned long reg;
  156. sirfport->ms_enabled = 0;
  157. if (!sirfport->hw_flow_ctrl)
  158. return;
  159. reg = rd_regl(port, SIRFUART_AFC_CTRL);
  160. wr_regl(port, SIRFUART_AFC_CTRL, reg & ~0x3FF);
  161. reg = rd_regl(port, SIRFUART_INT_EN);
  162. wr_regl(port, SIRFUART_INT_EN, reg & ~SIRFUART_CTS_INT_EN);
  163. }
  164. static void sirfsoc_uart_enable_ms(struct uart_port *port)
  165. {
  166. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  167. unsigned long reg;
  168. unsigned long flg;
  169. if (!sirfport->hw_flow_ctrl)
  170. return;
  171. flg = SIRFUART_AFC_RX_EN | SIRFUART_AFC_TX_EN;
  172. reg = rd_regl(port, SIRFUART_AFC_CTRL);
  173. wr_regl(port, SIRFUART_AFC_CTRL, reg | flg);
  174. reg = rd_regl(port, SIRFUART_INT_EN);
  175. wr_regl(port, SIRFUART_INT_EN, reg | SIRFUART_CTS_INT_EN);
  176. uart_handle_cts_change(port,
  177. !(rd_regl(port, SIRFUART_AFC_CTRL) & SIRFUART_CTS_IN_STATUS));
  178. sirfport->ms_enabled = 1;
  179. }
  180. static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
  181. {
  182. unsigned long ulcon = rd_regl(port, SIRFUART_LINE_CTRL);
  183. if (break_state)
  184. ulcon |= SIRFUART_SET_BREAK;
  185. else
  186. ulcon &= ~SIRFUART_SET_BREAK;
  187. wr_regl(port, SIRFUART_LINE_CTRL, ulcon);
  188. }
  189. static unsigned int
  190. sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
  191. {
  192. unsigned int ch, rx_count = 0;
  193. struct tty_struct *tty;
  194. tty = tty_port_tty_get(&port->state->port);
  195. if (!tty)
  196. return -ENODEV;
  197. while (!(rd_regl(port, SIRFUART_RX_FIFO_STATUS) &
  198. SIRFUART_FIFOEMPTY_MASK(port))) {
  199. ch = rd_regl(port, SIRFUART_RX_FIFO_DATA) | SIRFUART_DUMMY_READ;
  200. if (unlikely(uart_handle_sysrq_char(port, ch)))
  201. continue;
  202. uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
  203. rx_count++;
  204. if (rx_count >= max_rx_count)
  205. break;
  206. }
  207. port->icount.rx += rx_count;
  208. tty_flip_buffer_push(tty);
  209. tty_kref_put(tty);
  210. return rx_count;
  211. }
  212. static unsigned int
  213. sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
  214. {
  215. struct uart_port *port = &sirfport->port;
  216. struct circ_buf *xmit = &port->state->xmit;
  217. unsigned int num_tx = 0;
  218. while (!uart_circ_empty(xmit) &&
  219. !(rd_regl(port, SIRFUART_TX_FIFO_STATUS) &
  220. SIRFUART_FIFOFULL_MASK(port)) &&
  221. count--) {
  222. wr_regl(port, SIRFUART_TX_FIFO_DATA, xmit->buf[xmit->tail]);
  223. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  224. port->icount.tx++;
  225. num_tx++;
  226. }
  227. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  228. uart_write_wakeup(port);
  229. return num_tx;
  230. }
  231. static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
  232. {
  233. unsigned long intr_status;
  234. unsigned long cts_status;
  235. unsigned long flag = TTY_NORMAL;
  236. struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
  237. struct uart_port *port = &sirfport->port;
  238. struct uart_state *state = port->state;
  239. struct circ_buf *xmit = &port->state->xmit;
  240. spin_lock(&port->lock);
  241. intr_status = rd_regl(port, SIRFUART_INT_STATUS);
  242. wr_regl(port, SIRFUART_INT_STATUS, intr_status);
  243. intr_status &= rd_regl(port, SIRFUART_INT_EN);
  244. if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT))) {
  245. if (intr_status & SIRFUART_RXD_BREAK) {
  246. if (uart_handle_break(port))
  247. goto recv_char;
  248. uart_insert_char(port, intr_status,
  249. SIRFUART_RX_OFLOW, 0, TTY_BREAK);
  250. spin_unlock(&port->lock);
  251. return IRQ_HANDLED;
  252. }
  253. if (intr_status & SIRFUART_RX_OFLOW)
  254. port->icount.overrun++;
  255. if (intr_status & SIRFUART_FRM_ERR) {
  256. port->icount.frame++;
  257. flag = TTY_FRAME;
  258. }
  259. if (intr_status & SIRFUART_PARITY_ERR)
  260. flag = TTY_PARITY;
  261. wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
  262. wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
  263. wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START);
  264. intr_status &= port->read_status_mask;
  265. uart_insert_char(port, intr_status,
  266. SIRFUART_RX_OFLOW_INT, 0, flag);
  267. }
  268. recv_char:
  269. if (intr_status & SIRFUART_CTS_INT_EN) {
  270. cts_status = !(rd_regl(port, SIRFUART_AFC_CTRL) &
  271. SIRFUART_CTS_IN_STATUS);
  272. if (cts_status != 0) {
  273. uart_handle_cts_change(port, 1);
  274. } else {
  275. uart_handle_cts_change(port, 0);
  276. wake_up_interruptible(&state->port.delta_msr_wait);
  277. }
  278. }
  279. if (intr_status & SIRFUART_RX_IO_INT_EN)
  280. sirfsoc_uart_pio_rx_chars(port, SIRFSOC_UART_IO_RX_MAX_CNT);
  281. if (intr_status & SIRFUART_TX_INT_EN) {
  282. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  283. spin_unlock(&port->lock);
  284. return IRQ_HANDLED;
  285. } else {
  286. sirfsoc_uart_pio_tx_chars(sirfport,
  287. SIRFSOC_UART_IO_TX_REASONABLE_CNT);
  288. if ((uart_circ_empty(xmit)) &&
  289. (rd_regl(port, SIRFUART_TX_FIFO_STATUS) &
  290. SIRFUART_FIFOEMPTY_MASK(port)))
  291. sirfsoc_uart_stop_tx(port);
  292. }
  293. }
  294. spin_unlock(&port->lock);
  295. return IRQ_HANDLED;
  296. }
  297. static void sirfsoc_uart_start_rx(struct uart_port *port)
  298. {
  299. unsigned long regv;
  300. regv = rd_regl(port, SIRFUART_INT_EN);
  301. wr_regl(port, SIRFUART_INT_EN, regv | SIRFUART_RX_IO_INT_EN);
  302. wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
  303. wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
  304. wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_START);
  305. }
  306. static unsigned int
  307. sirfsoc_calc_sample_div(unsigned long baud_rate,
  308. unsigned long ioclk_rate, unsigned long *setted_baud)
  309. {
  310. unsigned long min_delta = ~0UL;
  311. unsigned short sample_div;
  312. unsigned int regv = 0;
  313. unsigned long ioclk_div;
  314. unsigned long baud_tmp;
  315. int temp_delta;
  316. for (sample_div = SIRF_MIN_SAMPLE_DIV;
  317. sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
  318. ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
  319. if (ioclk_div > SIRF_IOCLK_DIV_MAX)
  320. continue;
  321. baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
  322. temp_delta = baud_tmp - baud_rate;
  323. temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
  324. if (temp_delta < min_delta) {
  325. regv = regv & (~SIRF_IOCLK_DIV_MASK);
  326. regv = regv | ioclk_div;
  327. regv = regv & (~SIRF_SAMPLE_DIV_MASK);
  328. regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
  329. min_delta = temp_delta;
  330. *setted_baud = baud_tmp;
  331. }
  332. }
  333. return regv;
  334. }
  335. static void sirfsoc_uart_set_termios(struct uart_port *port,
  336. struct ktermios *termios,
  337. struct ktermios *old)
  338. {
  339. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  340. unsigned long ioclk_rate;
  341. unsigned long config_reg = 0;
  342. unsigned long baud_rate;
  343. unsigned long setted_baud;
  344. unsigned long flags;
  345. unsigned long ic;
  346. unsigned int clk_div_reg = 0;
  347. unsigned long temp_reg_val;
  348. unsigned long rx_time_out;
  349. int threshold_div;
  350. int temp;
  351. ioclk_rate = 150000000;
  352. switch (termios->c_cflag & CSIZE) {
  353. default:
  354. case CS8:
  355. config_reg |= SIRFUART_DATA_BIT_LEN_8;
  356. break;
  357. case CS7:
  358. config_reg |= SIRFUART_DATA_BIT_LEN_7;
  359. break;
  360. case CS6:
  361. config_reg |= SIRFUART_DATA_BIT_LEN_6;
  362. break;
  363. case CS5:
  364. config_reg |= SIRFUART_DATA_BIT_LEN_5;
  365. break;
  366. }
  367. if (termios->c_cflag & CSTOPB)
  368. config_reg |= SIRFUART_STOP_BIT_LEN_2;
  369. baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
  370. spin_lock_irqsave(&port->lock, flags);
  371. port->read_status_mask = SIRFUART_RX_OFLOW_INT;
  372. port->ignore_status_mask = 0;
  373. /* read flags */
  374. if (termios->c_iflag & INPCK)
  375. port->read_status_mask |=
  376. SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT;
  377. if (termios->c_iflag & (BRKINT | PARMRK))
  378. port->read_status_mask |= SIRFUART_RXD_BREAK_INT;
  379. /* ignore flags */
  380. if (termios->c_iflag & IGNPAR)
  381. port->ignore_status_mask |=
  382. SIRFUART_FRM_ERR_INT | SIRFUART_PARITY_ERR_INT;
  383. if ((termios->c_cflag & CREAD) == 0)
  384. port->ignore_status_mask |= SIRFUART_DUMMY_READ;
  385. /* enable parity if PARENB is set*/
  386. if (termios->c_cflag & PARENB) {
  387. if (termios->c_cflag & CMSPAR) {
  388. if (termios->c_cflag & PARODD)
  389. config_reg |= SIRFUART_STICK_BIT_MARK;
  390. else
  391. config_reg |= SIRFUART_STICK_BIT_SPACE;
  392. } else if (termios->c_cflag & PARODD) {
  393. config_reg |= SIRFUART_STICK_BIT_ODD;
  394. } else {
  395. config_reg |= SIRFUART_STICK_BIT_EVEN;
  396. }
  397. }
  398. /* Hardware Flow Control Settings */
  399. if (UART_ENABLE_MS(port, termios->c_cflag)) {
  400. if (!sirfport->ms_enabled)
  401. sirfsoc_uart_enable_ms(port);
  402. } else {
  403. if (sirfport->ms_enabled)
  404. sirfsoc_uart_disable_ms(port);
  405. }
  406. /* common rate: fast calculation */
  407. for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
  408. if (baud_rate == baudrate_to_regv[ic].baud_rate)
  409. clk_div_reg = baudrate_to_regv[ic].reg_val;
  410. setted_baud = baud_rate;
  411. /* arbitary rate setting */
  412. if (unlikely(clk_div_reg == 0))
  413. clk_div_reg = sirfsoc_calc_sample_div(baud_rate, ioclk_rate,
  414. &setted_baud);
  415. wr_regl(port, SIRFUART_DIVISOR, clk_div_reg);
  416. if (tty_termios_baud_rate(termios))
  417. tty_termios_encode_baud_rate(termios, setted_baud, setted_baud);
  418. /* set receive timeout */
  419. rx_time_out = SIRFSOC_UART_RX_TIMEOUT(baud_rate, 20000);
  420. rx_time_out = (rx_time_out > 0xFFFF) ? 0xFFFF : rx_time_out;
  421. config_reg |= SIRFUART_RECV_TIMEOUT(rx_time_out);
  422. temp_reg_val = rd_regl(port, SIRFUART_TX_FIFO_OP);
  423. wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
  424. wr_regl(port, SIRFUART_TX_FIFO_OP,
  425. temp_reg_val & ~SIRFUART_TX_FIFO_START);
  426. wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, SIRFUART_TX_MODE_IO);
  427. wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, SIRFUART_RX_MODE_IO);
  428. wr_regl(port, SIRFUART_LINE_CTRL, config_reg);
  429. /* Reset Rx/Tx FIFO Threshold level for proper baudrate */
  430. if (baud_rate < 1000000)
  431. threshold_div = 1;
  432. else
  433. threshold_div = 2;
  434. temp = port->line == 1 ? 16 : 64;
  435. wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp / threshold_div);
  436. wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp / threshold_div);
  437. temp_reg_val |= SIRFUART_TX_FIFO_START;
  438. wr_regl(port, SIRFUART_TX_FIFO_OP, temp_reg_val);
  439. uart_update_timeout(port, termios->c_cflag, baud_rate);
  440. sirfsoc_uart_start_rx(port);
  441. wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_TX_EN | SIRFUART_RX_EN);
  442. spin_unlock_irqrestore(&port->lock, flags);
  443. }
  444. static void startup_uart_controller(struct uart_port *port)
  445. {
  446. unsigned long temp_regv;
  447. int temp;
  448. temp_regv = rd_regl(port, SIRFUART_TX_DMA_IO_CTRL);
  449. wr_regl(port, SIRFUART_TX_DMA_IO_CTRL, temp_regv | SIRFUART_TX_MODE_IO);
  450. temp_regv = rd_regl(port, SIRFUART_RX_DMA_IO_CTRL);
  451. wr_regl(port, SIRFUART_RX_DMA_IO_CTRL, temp_regv | SIRFUART_RX_MODE_IO);
  452. wr_regl(port, SIRFUART_TX_DMA_IO_LEN, 0);
  453. wr_regl(port, SIRFUART_RX_DMA_IO_LEN, 0);
  454. wr_regl(port, SIRFUART_TX_RX_EN, SIRFUART_RX_EN | SIRFUART_TX_EN);
  455. wr_regl(port, SIRFUART_TX_FIFO_OP, SIRFUART_TX_FIFO_RESET);
  456. wr_regl(port, SIRFUART_TX_FIFO_OP, 0);
  457. wr_regl(port, SIRFUART_RX_FIFO_OP, SIRFUART_RX_FIFO_RESET);
  458. wr_regl(port, SIRFUART_RX_FIFO_OP, 0);
  459. temp = port->line == 1 ? 16 : 64;
  460. wr_regl(port, SIRFUART_TX_FIFO_CTRL, temp);
  461. wr_regl(port, SIRFUART_RX_FIFO_CTRL, temp);
  462. }
  463. static int sirfsoc_uart_startup(struct uart_port *port)
  464. {
  465. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  466. unsigned int index = port->line;
  467. int ret;
  468. set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
  469. ret = request_irq(port->irq,
  470. sirfsoc_uart_isr,
  471. 0,
  472. SIRFUART_PORT_NAME,
  473. sirfport);
  474. if (ret != 0) {
  475. dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
  476. index, port->irq);
  477. goto irq_err;
  478. }
  479. startup_uart_controller(port);
  480. enable_irq(port->irq);
  481. irq_err:
  482. return ret;
  483. }
  484. static void sirfsoc_uart_shutdown(struct uart_port *port)
  485. {
  486. struct sirfsoc_uart_port *sirfport = to_sirfport(port);
  487. wr_regl(port, SIRFUART_INT_EN, 0);
  488. free_irq(port->irq, sirfport);
  489. if (sirfport->ms_enabled) {
  490. sirfsoc_uart_disable_ms(port);
  491. sirfport->ms_enabled = 0;
  492. }
  493. }
  494. static const char *sirfsoc_uart_type(struct uart_port *port)
  495. {
  496. return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
  497. }
  498. static int sirfsoc_uart_request_port(struct uart_port *port)
  499. {
  500. void *ret;
  501. ret = request_mem_region(port->mapbase,
  502. SIRFUART_MAP_SIZE, SIRFUART_PORT_NAME);
  503. return ret ? 0 : -EBUSY;
  504. }
  505. static void sirfsoc_uart_release_port(struct uart_port *port)
  506. {
  507. release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
  508. }
  509. static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
  510. {
  511. if (flags & UART_CONFIG_TYPE) {
  512. port->type = SIRFSOC_PORT_TYPE;
  513. sirfsoc_uart_request_port(port);
  514. }
  515. }
  516. static struct uart_ops sirfsoc_uart_ops = {
  517. .tx_empty = sirfsoc_uart_tx_empty,
  518. .get_mctrl = sirfsoc_uart_get_mctrl,
  519. .set_mctrl = sirfsoc_uart_set_mctrl,
  520. .stop_tx = sirfsoc_uart_stop_tx,
  521. .start_tx = sirfsoc_uart_start_tx,
  522. .stop_rx = sirfsoc_uart_stop_rx,
  523. .enable_ms = sirfsoc_uart_enable_ms,
  524. .break_ctl = sirfsoc_uart_break_ctl,
  525. .startup = sirfsoc_uart_startup,
  526. .shutdown = sirfsoc_uart_shutdown,
  527. .set_termios = sirfsoc_uart_set_termios,
  528. .type = sirfsoc_uart_type,
  529. .release_port = sirfsoc_uart_release_port,
  530. .request_port = sirfsoc_uart_request_port,
  531. .config_port = sirfsoc_uart_config_port,
  532. };
  533. #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
  534. static int __init sirfsoc_uart_console_setup(struct console *co, char *options)
  535. {
  536. unsigned int baud = 115200;
  537. unsigned int bits = 8;
  538. unsigned int parity = 'n';
  539. unsigned int flow = 'n';
  540. struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
  541. if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
  542. return -EINVAL;
  543. if (!port->mapbase)
  544. return -ENODEV;
  545. if (options)
  546. uart_parse_options(options, &baud, &parity, &bits, &flow);
  547. port->cons = co;
  548. return uart_set_options(port, co, baud, parity, bits, flow);
  549. }
  550. static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
  551. {
  552. while (rd_regl(port,
  553. SIRFUART_TX_FIFO_STATUS) & SIRFUART_FIFOFULL_MASK(port))
  554. cpu_relax();
  555. wr_regb(port, SIRFUART_TX_FIFO_DATA, ch);
  556. }
  557. static void sirfsoc_uart_console_write(struct console *co, const char *s,
  558. unsigned int count)
  559. {
  560. struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
  561. uart_console_write(port, s, count, sirfsoc_uart_console_putchar);
  562. }
  563. static struct console sirfsoc_uart_console = {
  564. .name = SIRFSOC_UART_NAME,
  565. .device = uart_console_device,
  566. .flags = CON_PRINTBUFFER,
  567. .index = -1,
  568. .write = sirfsoc_uart_console_write,
  569. .setup = sirfsoc_uart_console_setup,
  570. .data = &sirfsoc_uart_drv,
  571. };
  572. static int __init sirfsoc_uart_console_init(void)
  573. {
  574. register_console(&sirfsoc_uart_console);
  575. return 0;
  576. }
  577. console_initcall(sirfsoc_uart_console_init);
  578. #endif
  579. static struct uart_driver sirfsoc_uart_drv = {
  580. .owner = THIS_MODULE,
  581. .driver_name = SIRFUART_PORT_NAME,
  582. .nr = SIRFSOC_UART_NR,
  583. .dev_name = SIRFSOC_UART_NAME,
  584. .major = SIRFSOC_UART_MAJOR,
  585. .minor = SIRFSOC_UART_MINOR,
  586. #ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
  587. .cons = &sirfsoc_uart_console,
  588. #else
  589. .cons = NULL,
  590. #endif
  591. };
  592. int sirfsoc_uart_probe(struct platform_device *pdev)
  593. {
  594. struct sirfsoc_uart_port *sirfport;
  595. struct uart_port *port;
  596. struct resource *res;
  597. int ret;
  598. if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) {
  599. dev_err(&pdev->dev,
  600. "Unable to find cell-index in uart node.\n");
  601. ret = -EFAULT;
  602. goto err;
  603. }
  604. sirfport = &sirfsoc_uart_ports[pdev->id];
  605. port = &sirfport->port;
  606. port->dev = &pdev->dev;
  607. port->private_data = sirfport;
  608. if (of_find_property(pdev->dev.of_node, "hw_flow_ctrl", NULL))
  609. sirfport->hw_flow_ctrl = 1;
  610. if (of_property_read_u32(pdev->dev.of_node,
  611. "fifosize",
  612. &port->fifosize)) {
  613. dev_err(&pdev->dev,
  614. "Unable to find fifosize in uart node.\n");
  615. ret = -EFAULT;
  616. goto err;
  617. }
  618. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  619. if (res == NULL) {
  620. dev_err(&pdev->dev, "Insufficient resources.\n");
  621. ret = -EFAULT;
  622. goto err;
  623. }
  624. port->mapbase = res->start;
  625. port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  626. if (!port->membase) {
  627. dev_err(&pdev->dev, "Cannot remap resource.\n");
  628. ret = -ENOMEM;
  629. goto err;
  630. }
  631. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  632. if (res == NULL) {
  633. dev_err(&pdev->dev, "Insufficient resources.\n");
  634. ret = -EFAULT;
  635. goto err;
  636. }
  637. port->irq = res->start;
  638. if (sirfport->hw_flow_ctrl) {
  639. sirfport->p = pinctrl_get_select_default(&pdev->dev);
  640. ret = IS_ERR(sirfport->p);
  641. if (ret)
  642. goto err;
  643. }
  644. port->ops = &sirfsoc_uart_ops;
  645. spin_lock_init(&port->lock);
  646. platform_set_drvdata(pdev, sirfport);
  647. ret = uart_add_one_port(&sirfsoc_uart_drv, port);
  648. if (ret != 0) {
  649. dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
  650. goto port_err;
  651. }
  652. return 0;
  653. port_err:
  654. platform_set_drvdata(pdev, NULL);
  655. if (sirfport->hw_flow_ctrl)
  656. pinctrl_put(sirfport->p);
  657. err:
  658. return ret;
  659. }
  660. static int sirfsoc_uart_remove(struct platform_device *pdev)
  661. {
  662. struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
  663. struct uart_port *port = &sirfport->port;
  664. platform_set_drvdata(pdev, NULL);
  665. if (sirfport->hw_flow_ctrl)
  666. pinctrl_put(sirfport->p);
  667. uart_remove_one_port(&sirfsoc_uart_drv, port);
  668. return 0;
  669. }
  670. static int
  671. sirfsoc_uart_suspend(struct platform_device *pdev, pm_message_t state)
  672. {
  673. struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
  674. struct uart_port *port = &sirfport->port;
  675. uart_suspend_port(&sirfsoc_uart_drv, port);
  676. return 0;
  677. }
  678. static int sirfsoc_uart_resume(struct platform_device *pdev)
  679. {
  680. struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
  681. struct uart_port *port = &sirfport->port;
  682. uart_resume_port(&sirfsoc_uart_drv, port);
  683. return 0;
  684. }
  685. static struct of_device_id sirfsoc_uart_ids[] = {
  686. { .compatible = "sirf,prima2-uart", },
  687. { .compatible = "sirf,marco-uart", },
  688. {}
  689. };
  690. MODULE_DEVICE_TABLE(of, sirfsoc_serial_of_match);
  691. static struct platform_driver sirfsoc_uart_driver = {
  692. .probe = sirfsoc_uart_probe,
  693. .remove = sirfsoc_uart_remove,
  694. .suspend = sirfsoc_uart_suspend,
  695. .resume = sirfsoc_uart_resume,
  696. .driver = {
  697. .name = SIRFUART_PORT_NAME,
  698. .owner = THIS_MODULE,
  699. .of_match_table = sirfsoc_uart_ids,
  700. },
  701. };
  702. static int __init sirfsoc_uart_init(void)
  703. {
  704. int ret = 0;
  705. ret = uart_register_driver(&sirfsoc_uart_drv);
  706. if (ret)
  707. goto out;
  708. ret = platform_driver_register(&sirfsoc_uart_driver);
  709. if (ret)
  710. uart_unregister_driver(&sirfsoc_uart_drv);
  711. out:
  712. return ret;
  713. }
  714. module_init(sirfsoc_uart_init);
  715. static void __exit sirfsoc_uart_exit(void)
  716. {
  717. platform_driver_unregister(&sirfsoc_uart_driver);
  718. uart_unregister_driver(&sirfsoc_uart_drv);
  719. }
  720. module_exit(sirfsoc_uart_exit);
  721. MODULE_LICENSE("GPL v2");
  722. MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
  723. MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");