ar933x_uart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Atheros AR933X SoC built-in UART driver
  4. *
  5. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/ioport.h>
  11. #include <linux/init.h>
  12. #include <linux/console.h>
  13. #include <linux/sysrq.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/serial.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/clk.h>
  27. #include <asm/div64.h>
  28. #include <asm/mach-ath79/ar933x_uart.h>
  29. #include "serial_mctrl_gpio.h"
  30. #define DRIVER_NAME "ar933x-uart"
  31. #define AR933X_UART_MAX_SCALE 0xff
  32. #define AR933X_UART_MAX_STEP 0xffff
  33. #define AR933X_UART_MIN_BAUD 300
  34. #define AR933X_UART_MAX_BAUD 3000000
  35. #define AR933X_DUMMY_STATUS_RD 0x01
  36. static struct uart_driver ar933x_uart_driver;
  37. struct ar933x_uart_port {
  38. struct uart_port port;
  39. unsigned int ier; /* shadow Interrupt Enable Register */
  40. unsigned int min_baud;
  41. unsigned int max_baud;
  42. struct clk *clk;
  43. struct mctrl_gpios *gpios;
  44. struct gpio_desc *rts_gpiod;
  45. };
  46. static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
  47. int offset)
  48. {
  49. return readl(up->port.membase + offset);
  50. }
  51. static inline void ar933x_uart_write(struct ar933x_uart_port *up,
  52. int offset, unsigned int value)
  53. {
  54. writel(value, up->port.membase + offset);
  55. }
  56. static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
  57. unsigned int offset,
  58. unsigned int mask,
  59. unsigned int val)
  60. {
  61. unsigned int t;
  62. t = ar933x_uart_read(up, offset);
  63. t &= ~mask;
  64. t |= val;
  65. ar933x_uart_write(up, offset, t);
  66. }
  67. static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
  68. unsigned int offset,
  69. unsigned int val)
  70. {
  71. ar933x_uart_rmw(up, offset, 0, val);
  72. }
  73. static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
  74. unsigned int offset,
  75. unsigned int val)
  76. {
  77. ar933x_uart_rmw(up, offset, val, 0);
  78. }
  79. static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
  80. {
  81. up->ier |= AR933X_UART_INT_TX_EMPTY;
  82. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  83. }
  84. static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
  85. {
  86. up->ier &= ~AR933X_UART_INT_TX_EMPTY;
  87. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  88. }
  89. static inline void ar933x_uart_start_rx_interrupt(struct ar933x_uart_port *up)
  90. {
  91. up->ier |= AR933X_UART_INT_RX_VALID;
  92. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  93. }
  94. static inline void ar933x_uart_stop_rx_interrupt(struct ar933x_uart_port *up)
  95. {
  96. up->ier &= ~AR933X_UART_INT_RX_VALID;
  97. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  98. }
  99. static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
  100. {
  101. unsigned int rdata;
  102. rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
  103. rdata |= AR933X_UART_DATA_TX_CSR;
  104. ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
  105. }
  106. static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
  107. {
  108. struct ar933x_uart_port *up =
  109. container_of(port, struct ar933x_uart_port, port);
  110. unsigned long flags;
  111. unsigned int rdata;
  112. uart_port_lock_irqsave(&up->port, &flags);
  113. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  114. uart_port_unlock_irqrestore(&up->port, flags);
  115. return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
  116. }
  117. static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
  118. {
  119. struct ar933x_uart_port *up =
  120. container_of(port, struct ar933x_uart_port, port);
  121. int ret = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  122. mctrl_gpio_get(up->gpios, &ret);
  123. return ret;
  124. }
  125. static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  126. {
  127. struct ar933x_uart_port *up =
  128. container_of(port, struct ar933x_uart_port, port);
  129. mctrl_gpio_set(up->gpios, mctrl);
  130. }
  131. static void ar933x_uart_start_tx(struct uart_port *port)
  132. {
  133. struct ar933x_uart_port *up =
  134. container_of(port, struct ar933x_uart_port, port);
  135. ar933x_uart_start_tx_interrupt(up);
  136. }
  137. static void ar933x_uart_wait_tx_complete(struct ar933x_uart_port *up)
  138. {
  139. unsigned int status;
  140. unsigned int timeout = 60000;
  141. /* Wait up to 60ms for the character(s) to be sent. */
  142. do {
  143. status = ar933x_uart_read(up, AR933X_UART_CS_REG);
  144. if (--timeout == 0)
  145. break;
  146. udelay(1);
  147. } while (status & AR933X_UART_CS_TX_BUSY);
  148. if (timeout == 0)
  149. dev_err(up->port.dev, "waiting for TX timed out\n");
  150. }
  151. static void ar933x_uart_rx_flush(struct ar933x_uart_port *up)
  152. {
  153. unsigned int status;
  154. /* clear RX_VALID interrupt */
  155. ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_RX_VALID);
  156. /* remove characters from the RX FIFO */
  157. do {
  158. ar933x_uart_write(up, AR933X_UART_DATA_REG, AR933X_UART_DATA_RX_CSR);
  159. status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  160. } while (status & AR933X_UART_DATA_RX_CSR);
  161. }
  162. static void ar933x_uart_stop_tx(struct uart_port *port)
  163. {
  164. struct ar933x_uart_port *up =
  165. container_of(port, struct ar933x_uart_port, port);
  166. ar933x_uart_stop_tx_interrupt(up);
  167. }
  168. static void ar933x_uart_stop_rx(struct uart_port *port)
  169. {
  170. struct ar933x_uart_port *up =
  171. container_of(port, struct ar933x_uart_port, port);
  172. ar933x_uart_stop_rx_interrupt(up);
  173. }
  174. static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
  175. {
  176. struct ar933x_uart_port *up =
  177. container_of(port, struct ar933x_uart_port, port);
  178. unsigned long flags;
  179. uart_port_lock_irqsave(&up->port, &flags);
  180. if (break_state == -1)
  181. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  182. AR933X_UART_CS_TX_BREAK);
  183. else
  184. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  185. AR933X_UART_CS_TX_BREAK);
  186. uart_port_unlock_irqrestore(&up->port, flags);
  187. }
  188. /*
  189. * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
  190. */
  191. static unsigned long ar933x_uart_get_baud(unsigned int clk,
  192. unsigned int scale,
  193. unsigned int step)
  194. {
  195. u64 t;
  196. u32 div;
  197. div = (2 << 16) * (scale + 1);
  198. t = clk;
  199. t *= step;
  200. t += (div / 2);
  201. do_div(t, div);
  202. return t;
  203. }
  204. static void ar933x_uart_get_scale_step(unsigned int clk,
  205. unsigned int baud,
  206. unsigned int *scale,
  207. unsigned int *step)
  208. {
  209. unsigned int tscale;
  210. long min_diff;
  211. *scale = 0;
  212. *step = 0;
  213. min_diff = baud;
  214. for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) {
  215. u64 tstep;
  216. int diff;
  217. tstep = baud * (tscale + 1);
  218. tstep *= (2 << 16);
  219. do_div(tstep, clk);
  220. if (tstep > AR933X_UART_MAX_STEP)
  221. break;
  222. diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud);
  223. if (diff < min_diff) {
  224. min_diff = diff;
  225. *scale = tscale;
  226. *step = tstep;
  227. }
  228. }
  229. }
  230. static void ar933x_uart_set_termios(struct uart_port *port,
  231. struct ktermios *new,
  232. const struct ktermios *old)
  233. {
  234. struct ar933x_uart_port *up =
  235. container_of(port, struct ar933x_uart_port, port);
  236. unsigned int cs;
  237. unsigned long flags;
  238. unsigned int baud, scale, step;
  239. /* Only CS8 is supported */
  240. new->c_cflag &= ~CSIZE;
  241. new->c_cflag |= CS8;
  242. /* Only one stop bit is supported */
  243. new->c_cflag &= ~CSTOPB;
  244. cs = 0;
  245. if (new->c_cflag & PARENB) {
  246. if (!(new->c_cflag & PARODD))
  247. cs |= AR933X_UART_CS_PARITY_EVEN;
  248. else
  249. cs |= AR933X_UART_CS_PARITY_ODD;
  250. } else {
  251. cs |= AR933X_UART_CS_PARITY_NONE;
  252. }
  253. /* Mark/space parity is not supported */
  254. new->c_cflag &= ~CMSPAR;
  255. baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud);
  256. ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step);
  257. /*
  258. * Ok, we're now changing the port state. Do it with
  259. * interrupts disabled.
  260. */
  261. uart_port_lock_irqsave(&up->port, &flags);
  262. /* disable the UART */
  263. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  264. AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S);
  265. /* Update the per-port timeout. */
  266. uart_update_timeout(port, new->c_cflag, baud);
  267. up->port.ignore_status_mask = 0;
  268. /* ignore all characters if CREAD is not set */
  269. if ((new->c_cflag & CREAD) == 0)
  270. up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
  271. ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
  272. scale << AR933X_UART_CLOCK_SCALE_S | step);
  273. /* setup configuration register */
  274. ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
  275. /* enable host interrupt */
  276. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  277. AR933X_UART_CS_HOST_INT_EN);
  278. /* enable RX and TX ready overide */
  279. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  280. AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE);
  281. /* reenable the UART */
  282. ar933x_uart_rmw(up, AR933X_UART_CS_REG,
  283. AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
  284. AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
  285. uart_port_unlock_irqrestore(&up->port, flags);
  286. if (tty_termios_baud_rate(new))
  287. tty_termios_encode_baud_rate(new, baud, baud);
  288. }
  289. static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
  290. {
  291. struct tty_port *port = &up->port.state->port;
  292. int max_count = 256;
  293. do {
  294. unsigned int rdata;
  295. unsigned char ch;
  296. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  297. if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
  298. break;
  299. /* remove the character from the FIFO */
  300. ar933x_uart_write(up, AR933X_UART_DATA_REG,
  301. AR933X_UART_DATA_RX_CSR);
  302. up->port.icount.rx++;
  303. ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
  304. if (uart_prepare_sysrq_char(&up->port, ch))
  305. continue;
  306. if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
  307. tty_insert_flip_char(port, ch, TTY_NORMAL);
  308. } while (max_count-- > 0);
  309. tty_flip_buffer_push(port);
  310. }
  311. static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
  312. {
  313. struct tty_port *tport = &up->port.state->port;
  314. struct serial_rs485 *rs485conf = &up->port.rs485;
  315. int count;
  316. bool half_duplex_send = false;
  317. if (uart_tx_stopped(&up->port))
  318. return;
  319. if ((rs485conf->flags & SER_RS485_ENABLED) &&
  320. (up->port.x_char || !kfifo_is_empty(&tport->xmit_fifo))) {
  321. ar933x_uart_stop_rx_interrupt(up);
  322. gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND));
  323. half_duplex_send = true;
  324. }
  325. count = up->port.fifosize;
  326. do {
  327. unsigned int rdata;
  328. unsigned char c;
  329. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  330. if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
  331. break;
  332. if (up->port.x_char) {
  333. ar933x_uart_putc(up, up->port.x_char);
  334. up->port.icount.tx++;
  335. up->port.x_char = 0;
  336. continue;
  337. }
  338. if (!uart_fifo_get(&up->port, &c))
  339. break;
  340. ar933x_uart_putc(up, c);
  341. } while (--count > 0);
  342. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  343. uart_write_wakeup(&up->port);
  344. if (!kfifo_is_empty(&tport->xmit_fifo)) {
  345. ar933x_uart_start_tx_interrupt(up);
  346. } else if (half_duplex_send) {
  347. ar933x_uart_wait_tx_complete(up);
  348. ar933x_uart_rx_flush(up);
  349. ar933x_uart_start_rx_interrupt(up);
  350. gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND));
  351. }
  352. }
  353. static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
  354. {
  355. struct ar933x_uart_port *up = dev_id;
  356. unsigned int status;
  357. status = ar933x_uart_read(up, AR933X_UART_CS_REG);
  358. if ((status & AR933X_UART_CS_HOST_INT) == 0)
  359. return IRQ_NONE;
  360. uart_port_lock(&up->port);
  361. status = ar933x_uart_read(up, AR933X_UART_INT_REG);
  362. status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  363. if (status & AR933X_UART_INT_RX_VALID) {
  364. ar933x_uart_write(up, AR933X_UART_INT_REG,
  365. AR933X_UART_INT_RX_VALID);
  366. ar933x_uart_rx_chars(up);
  367. }
  368. if (status & AR933X_UART_INT_TX_EMPTY) {
  369. ar933x_uart_write(up, AR933X_UART_INT_REG,
  370. AR933X_UART_INT_TX_EMPTY);
  371. ar933x_uart_stop_tx_interrupt(up);
  372. ar933x_uart_tx_chars(up);
  373. }
  374. uart_unlock_and_check_sysrq(&up->port);
  375. return IRQ_HANDLED;
  376. }
  377. static int ar933x_uart_startup(struct uart_port *port)
  378. {
  379. struct ar933x_uart_port *up =
  380. container_of(port, struct ar933x_uart_port, port);
  381. unsigned long flags;
  382. int ret;
  383. ret = request_irq(up->port.irq, ar933x_uart_interrupt,
  384. up->port.irqflags, dev_name(up->port.dev), up);
  385. if (ret)
  386. return ret;
  387. uart_port_lock_irqsave(&up->port, &flags);
  388. /* Enable HOST interrupts */
  389. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  390. AR933X_UART_CS_HOST_INT_EN);
  391. /* enable RX and TX ready overide */
  392. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  393. AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE);
  394. /* Enable RX interrupts */
  395. ar933x_uart_start_rx_interrupt(up);
  396. uart_port_unlock_irqrestore(&up->port, flags);
  397. return 0;
  398. }
  399. static void ar933x_uart_shutdown(struct uart_port *port)
  400. {
  401. struct ar933x_uart_port *up =
  402. container_of(port, struct ar933x_uart_port, port);
  403. /* Disable all interrupts */
  404. up->ier = 0;
  405. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  406. /* Disable break condition */
  407. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  408. AR933X_UART_CS_TX_BREAK);
  409. free_irq(up->port.irq, up);
  410. }
  411. static const char *ar933x_uart_type(struct uart_port *port)
  412. {
  413. return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
  414. }
  415. static void ar933x_uart_release_port(struct uart_port *port)
  416. {
  417. /* Nothing to release ... */
  418. }
  419. static int ar933x_uart_request_port(struct uart_port *port)
  420. {
  421. /* UARTs always present */
  422. return 0;
  423. }
  424. static void ar933x_uart_config_port(struct uart_port *port, int flags)
  425. {
  426. if (flags & UART_CONFIG_TYPE)
  427. port->type = PORT_AR933X;
  428. }
  429. static int ar933x_uart_verify_port(struct uart_port *port,
  430. struct serial_struct *ser)
  431. {
  432. struct ar933x_uart_port *up =
  433. container_of(port, struct ar933x_uart_port, port);
  434. if (ser->type != PORT_UNKNOWN &&
  435. ser->type != PORT_AR933X)
  436. return -EINVAL;
  437. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  438. return -EINVAL;
  439. if (ser->baud_base < up->min_baud ||
  440. ser->baud_base > up->max_baud)
  441. return -EINVAL;
  442. return 0;
  443. }
  444. #ifdef CONFIG_CONSOLE_POLL
  445. static int ar933x_poll_get_char(struct uart_port *port)
  446. {
  447. struct ar933x_uart_port *up =
  448. container_of(port, struct ar933x_uart_port, port);
  449. unsigned int rdata;
  450. unsigned char ch;
  451. u32 imr;
  452. /* Disable all interrupts */
  453. imr = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  454. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
  455. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  456. if ((rdata & AR933X_UART_DATA_RX_CSR) == 0) {
  457. /* Enable interrupts */
  458. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr);
  459. return NO_POLL_CHAR;
  460. }
  461. /* remove the character from the FIFO */
  462. ar933x_uart_write(up, AR933X_UART_DATA_REG,
  463. AR933X_UART_DATA_RX_CSR);
  464. ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
  465. /* Enable interrupts */
  466. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr);
  467. return ch;
  468. }
  469. static void ar933x_poll_put_char(struct uart_port *port, unsigned char c)
  470. {
  471. struct ar933x_uart_port *up =
  472. container_of(port, struct ar933x_uart_port, port);
  473. u32 imr;
  474. /* Disable all interrupts */
  475. imr = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  476. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
  477. /* Wait until FIFO is empty */
  478. while (!(ar933x_uart_read(up, AR933X_UART_DATA_REG) & AR933X_UART_DATA_TX_CSR))
  479. cpu_relax();
  480. /* Write a character */
  481. ar933x_uart_putc(up, c);
  482. /* Wait until FIFO is empty */
  483. while (!(ar933x_uart_read(up, AR933X_UART_DATA_REG) & AR933X_UART_DATA_TX_CSR))
  484. cpu_relax();
  485. /* Enable interrupts */
  486. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr);
  487. }
  488. #endif
  489. static const struct uart_ops ar933x_uart_ops = {
  490. .tx_empty = ar933x_uart_tx_empty,
  491. .set_mctrl = ar933x_uart_set_mctrl,
  492. .get_mctrl = ar933x_uart_get_mctrl,
  493. .stop_tx = ar933x_uart_stop_tx,
  494. .start_tx = ar933x_uart_start_tx,
  495. .stop_rx = ar933x_uart_stop_rx,
  496. .break_ctl = ar933x_uart_break_ctl,
  497. .startup = ar933x_uart_startup,
  498. .shutdown = ar933x_uart_shutdown,
  499. .set_termios = ar933x_uart_set_termios,
  500. .type = ar933x_uart_type,
  501. .release_port = ar933x_uart_release_port,
  502. .request_port = ar933x_uart_request_port,
  503. .config_port = ar933x_uart_config_port,
  504. .verify_port = ar933x_uart_verify_port,
  505. #ifdef CONFIG_CONSOLE_POLL
  506. .poll_get_char = ar933x_poll_get_char,
  507. .poll_put_char = ar933x_poll_put_char,
  508. #endif
  509. };
  510. static int ar933x_config_rs485(struct uart_port *port, struct ktermios *termios,
  511. struct serial_rs485 *rs485conf)
  512. {
  513. struct ar933x_uart_port *up =
  514. container_of(port, struct ar933x_uart_port, port);
  515. if (port->rs485.flags & SER_RS485_ENABLED)
  516. gpiod_set_value(up->rts_gpiod,
  517. !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND));
  518. return 0;
  519. }
  520. #ifdef CONFIG_SERIAL_AR933X_CONSOLE
  521. static struct ar933x_uart_port *
  522. ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
  523. static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
  524. {
  525. unsigned int status;
  526. unsigned int timeout = 60000;
  527. /* Wait up to 60ms for the character(s) to be sent. */
  528. do {
  529. status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  530. if (--timeout == 0)
  531. break;
  532. udelay(1);
  533. } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
  534. }
  535. static void ar933x_uart_console_putchar(struct uart_port *port, unsigned char ch)
  536. {
  537. struct ar933x_uart_port *up =
  538. container_of(port, struct ar933x_uart_port, port);
  539. ar933x_uart_wait_xmitr(up);
  540. ar933x_uart_putc(up, ch);
  541. }
  542. static void ar933x_uart_console_write(struct console *co, const char *s,
  543. unsigned int count)
  544. {
  545. struct ar933x_uart_port *up = ar933x_console_ports[co->index];
  546. unsigned long flags;
  547. unsigned int int_en;
  548. int locked = 1;
  549. if (oops_in_progress)
  550. locked = uart_port_trylock_irqsave(&up->port, &flags);
  551. else
  552. uart_port_lock_irqsave(&up->port, &flags);
  553. /*
  554. * First save the IER then disable the interrupts
  555. */
  556. int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  557. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
  558. uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
  559. /*
  560. * Finally, wait for transmitter to become empty
  561. * and restore the IER
  562. */
  563. ar933x_uart_wait_xmitr(up);
  564. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
  565. ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
  566. if (locked)
  567. uart_port_unlock_irqrestore(&up->port, flags);
  568. }
  569. static int ar933x_uart_console_setup(struct console *co, char *options)
  570. {
  571. struct ar933x_uart_port *up;
  572. int baud = 115200;
  573. int bits = 8;
  574. int parity = 'n';
  575. int flow = 'n';
  576. if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
  577. return -EINVAL;
  578. up = ar933x_console_ports[co->index];
  579. if (!up)
  580. return -ENODEV;
  581. if (options)
  582. uart_parse_options(options, &baud, &parity, &bits, &flow);
  583. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  584. }
  585. static struct console ar933x_uart_console = {
  586. .name = "ttyATH",
  587. .write = ar933x_uart_console_write,
  588. .device = uart_console_device,
  589. .setup = ar933x_uart_console_setup,
  590. .flags = CON_PRINTBUFFER,
  591. .index = -1,
  592. .data = &ar933x_uart_driver,
  593. };
  594. #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
  595. static struct uart_driver ar933x_uart_driver = {
  596. .owner = THIS_MODULE,
  597. .driver_name = DRIVER_NAME,
  598. .dev_name = "ttyATH",
  599. .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
  600. .cons = NULL, /* filled in runtime */
  601. };
  602. static const struct serial_rs485 ar933x_rs485_supported = {
  603. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
  604. };
  605. static int ar933x_uart_probe(struct platform_device *pdev)
  606. {
  607. struct ar933x_uart_port *up;
  608. struct uart_port *port;
  609. struct resource *mem_res;
  610. struct device_node *np;
  611. unsigned int baud;
  612. int id;
  613. int ret;
  614. int irq;
  615. np = pdev->dev.of_node;
  616. if (IS_ENABLED(CONFIG_OF) && np) {
  617. id = of_alias_get_id(np, "serial");
  618. if (id < 0) {
  619. dev_err(&pdev->dev, "unable to get alias id, err=%d\n",
  620. id);
  621. return id;
  622. }
  623. } else {
  624. id = pdev->id;
  625. if (id == -1)
  626. id = 0;
  627. }
  628. if (id >= CONFIG_SERIAL_AR933X_NR_UARTS)
  629. return -EINVAL;
  630. irq = platform_get_irq(pdev, 0);
  631. if (irq < 0)
  632. return irq;
  633. up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
  634. GFP_KERNEL);
  635. if (!up)
  636. return -ENOMEM;
  637. up->clk = devm_clk_get(&pdev->dev, "uart");
  638. if (IS_ERR(up->clk)) {
  639. dev_err(&pdev->dev, "unable to get UART clock\n");
  640. return PTR_ERR(up->clk);
  641. }
  642. port = &up->port;
  643. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &mem_res);
  644. if (IS_ERR(port->membase))
  645. return PTR_ERR(port->membase);
  646. ret = clk_prepare_enable(up->clk);
  647. if (ret)
  648. return ret;
  649. port->uartclk = clk_get_rate(up->clk);
  650. if (!port->uartclk) {
  651. ret = -EINVAL;
  652. goto err_disable_clk;
  653. }
  654. port->mapbase = mem_res->start;
  655. port->line = id;
  656. port->irq = irq;
  657. port->dev = &pdev->dev;
  658. port->type = PORT_AR933X;
  659. port->iotype = UPIO_MEM32;
  660. port->regshift = 2;
  661. port->fifosize = AR933X_UART_FIFO_SIZE;
  662. port->ops = &ar933x_uart_ops;
  663. port->rs485_config = ar933x_config_rs485;
  664. port->rs485_supported = ar933x_rs485_supported;
  665. baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
  666. up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
  667. baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
  668. up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
  669. ret = uart_get_rs485_mode(port);
  670. if (ret)
  671. goto err_disable_clk;
  672. up->gpios = mctrl_gpio_init(port, 0);
  673. if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS) {
  674. ret = PTR_ERR(up->gpios);
  675. goto err_disable_clk;
  676. }
  677. up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS);
  678. if (!up->rts_gpiod) {
  679. port->rs485_supported.flags &= ~SER_RS485_ENABLED;
  680. if (port->rs485.flags & SER_RS485_ENABLED) {
  681. dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n");
  682. port->rs485.flags &= ~SER_RS485_ENABLED;
  683. }
  684. }
  685. #ifdef CONFIG_SERIAL_AR933X_CONSOLE
  686. ar933x_console_ports[up->port.line] = up;
  687. #endif
  688. ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
  689. if (ret)
  690. goto err_disable_clk;
  691. platform_set_drvdata(pdev, up);
  692. return 0;
  693. err_disable_clk:
  694. clk_disable_unprepare(up->clk);
  695. return ret;
  696. }
  697. static void ar933x_uart_remove(struct platform_device *pdev)
  698. {
  699. struct ar933x_uart_port *up;
  700. up = platform_get_drvdata(pdev);
  701. if (up) {
  702. uart_remove_one_port(&ar933x_uart_driver, &up->port);
  703. clk_disable_unprepare(up->clk);
  704. }
  705. }
  706. #ifdef CONFIG_OF
  707. static const struct of_device_id ar933x_uart_of_ids[] = {
  708. { .compatible = "qca,ar9330-uart" },
  709. {},
  710. };
  711. MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids);
  712. #endif
  713. static struct platform_driver ar933x_uart_platform_driver = {
  714. .probe = ar933x_uart_probe,
  715. .remove = ar933x_uart_remove,
  716. .driver = {
  717. .name = DRIVER_NAME,
  718. .of_match_table = of_match_ptr(ar933x_uart_of_ids),
  719. },
  720. };
  721. static int __init ar933x_uart_init(void)
  722. {
  723. int ret;
  724. #ifdef CONFIG_SERIAL_AR933X_CONSOLE
  725. ar933x_uart_driver.cons = &ar933x_uart_console;
  726. #endif
  727. ret = uart_register_driver(&ar933x_uart_driver);
  728. if (ret)
  729. goto err_out;
  730. ret = platform_driver_register(&ar933x_uart_platform_driver);
  731. if (ret)
  732. goto err_unregister_uart_driver;
  733. return 0;
  734. err_unregister_uart_driver:
  735. uart_unregister_driver(&ar933x_uart_driver);
  736. err_out:
  737. return ret;
  738. }
  739. static void __exit ar933x_uart_exit(void)
  740. {
  741. platform_driver_unregister(&ar933x_uart_platform_driver);
  742. uart_unregister_driver(&ar933x_uart_driver);
  743. }
  744. module_init(ar933x_uart_init);
  745. module_exit(ar933x_uart_exit);
  746. MODULE_DESCRIPTION("Atheros AR933X UART driver");
  747. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  748. MODULE_LICENSE("GPL v2");
  749. MODULE_ALIAS("platform:" DRIVER_NAME);