ar933x_uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Atheros AR933X SoC built-in UART driver
  3. *
  4. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/ioport.h>
  14. #include <linux/init.h>
  15. #include <linux/console.h>
  16. #include <linux/sysrq.h>
  17. #include <linux/delay.h>
  18. #include <linux/platform_device.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. #define DRIVER_NAME "ar933x-uart"
  30. #define AR933X_UART_MAX_SCALE 0xff
  31. #define AR933X_UART_MAX_STEP 0xffff
  32. #define AR933X_UART_MIN_BAUD 300
  33. #define AR933X_UART_MAX_BAUD 3000000
  34. #define AR933X_DUMMY_STATUS_RD 0x01
  35. static struct uart_driver ar933x_uart_driver;
  36. struct ar933x_uart_port {
  37. struct uart_port port;
  38. unsigned int ier; /* shadow Interrupt Enable Register */
  39. unsigned int min_baud;
  40. unsigned int max_baud;
  41. struct clk *clk;
  42. };
  43. static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
  44. int offset)
  45. {
  46. return readl(up->port.membase + offset);
  47. }
  48. static inline void ar933x_uart_write(struct ar933x_uart_port *up,
  49. int offset, unsigned int value)
  50. {
  51. writel(value, up->port.membase + offset);
  52. }
  53. static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
  54. unsigned int offset,
  55. unsigned int mask,
  56. unsigned int val)
  57. {
  58. unsigned int t;
  59. t = ar933x_uart_read(up, offset);
  60. t &= ~mask;
  61. t |= val;
  62. ar933x_uart_write(up, offset, t);
  63. }
  64. static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
  65. unsigned int offset,
  66. unsigned int val)
  67. {
  68. ar933x_uart_rmw(up, offset, 0, val);
  69. }
  70. static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
  71. unsigned int offset,
  72. unsigned int val)
  73. {
  74. ar933x_uart_rmw(up, offset, val, 0);
  75. }
  76. static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
  77. {
  78. up->ier |= AR933X_UART_INT_TX_EMPTY;
  79. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  80. }
  81. static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
  82. {
  83. up->ier &= ~AR933X_UART_INT_TX_EMPTY;
  84. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  85. }
  86. static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
  87. {
  88. unsigned int rdata;
  89. rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
  90. rdata |= AR933X_UART_DATA_TX_CSR;
  91. ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
  92. }
  93. static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
  94. {
  95. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  96. unsigned long flags;
  97. unsigned int rdata;
  98. spin_lock_irqsave(&up->port.lock, flags);
  99. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  100. spin_unlock_irqrestore(&up->port.lock, flags);
  101. return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
  102. }
  103. static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
  104. {
  105. return TIOCM_CAR;
  106. }
  107. static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  108. {
  109. }
  110. static void ar933x_uart_start_tx(struct uart_port *port)
  111. {
  112. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  113. ar933x_uart_start_tx_interrupt(up);
  114. }
  115. static void ar933x_uart_stop_tx(struct uart_port *port)
  116. {
  117. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  118. ar933x_uart_stop_tx_interrupt(up);
  119. }
  120. static void ar933x_uart_stop_rx(struct uart_port *port)
  121. {
  122. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  123. up->ier &= ~AR933X_UART_INT_RX_VALID;
  124. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  125. }
  126. static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
  127. {
  128. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  129. unsigned long flags;
  130. spin_lock_irqsave(&up->port.lock, flags);
  131. if (break_state == -1)
  132. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  133. AR933X_UART_CS_TX_BREAK);
  134. else
  135. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  136. AR933X_UART_CS_TX_BREAK);
  137. spin_unlock_irqrestore(&up->port.lock, flags);
  138. }
  139. static void ar933x_uart_enable_ms(struct uart_port *port)
  140. {
  141. }
  142. /*
  143. * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
  144. */
  145. static unsigned long ar933x_uart_get_baud(unsigned int clk,
  146. unsigned int scale,
  147. unsigned int step)
  148. {
  149. u64 t;
  150. u32 div;
  151. div = (2 << 16) * (scale + 1);
  152. t = clk;
  153. t *= step;
  154. t += (div / 2);
  155. do_div(t, div);
  156. return t;
  157. }
  158. static void ar933x_uart_get_scale_step(unsigned int clk,
  159. unsigned int baud,
  160. unsigned int *scale,
  161. unsigned int *step)
  162. {
  163. unsigned int tscale;
  164. long min_diff;
  165. *scale = 0;
  166. *step = 0;
  167. min_diff = baud;
  168. for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) {
  169. u64 tstep;
  170. int diff;
  171. tstep = baud * (tscale + 1);
  172. tstep *= (2 << 16);
  173. do_div(tstep, clk);
  174. if (tstep > AR933X_UART_MAX_STEP)
  175. break;
  176. diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud);
  177. if (diff < min_diff) {
  178. min_diff = diff;
  179. *scale = tscale;
  180. *step = tstep;
  181. }
  182. }
  183. }
  184. static void ar933x_uart_set_termios(struct uart_port *port,
  185. struct ktermios *new,
  186. struct ktermios *old)
  187. {
  188. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  189. unsigned int cs;
  190. unsigned long flags;
  191. unsigned int baud, scale, step;
  192. /* Only CS8 is supported */
  193. new->c_cflag &= ~CSIZE;
  194. new->c_cflag |= CS8;
  195. /* Only one stop bit is supported */
  196. new->c_cflag &= ~CSTOPB;
  197. cs = 0;
  198. if (new->c_cflag & PARENB) {
  199. if (!(new->c_cflag & PARODD))
  200. cs |= AR933X_UART_CS_PARITY_EVEN;
  201. else
  202. cs |= AR933X_UART_CS_PARITY_ODD;
  203. } else {
  204. cs |= AR933X_UART_CS_PARITY_NONE;
  205. }
  206. /* Mark/space parity is not supported */
  207. new->c_cflag &= ~CMSPAR;
  208. baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud);
  209. ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step);
  210. /*
  211. * Ok, we're now changing the port state. Do it with
  212. * interrupts disabled.
  213. */
  214. spin_lock_irqsave(&up->port.lock, flags);
  215. /* disable the UART */
  216. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  217. AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S);
  218. /* Update the per-port timeout. */
  219. uart_update_timeout(port, new->c_cflag, baud);
  220. up->port.ignore_status_mask = 0;
  221. /* ignore all characters if CREAD is not set */
  222. if ((new->c_cflag & CREAD) == 0)
  223. up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
  224. ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
  225. scale << AR933X_UART_CLOCK_SCALE_S | step);
  226. /* setup configuration register */
  227. ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
  228. /* enable host interrupt */
  229. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  230. AR933X_UART_CS_HOST_INT_EN);
  231. /* reenable the UART */
  232. ar933x_uart_rmw(up, AR933X_UART_CS_REG,
  233. AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
  234. AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
  235. spin_unlock_irqrestore(&up->port.lock, flags);
  236. if (tty_termios_baud_rate(new))
  237. tty_termios_encode_baud_rate(new, baud, baud);
  238. }
  239. static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
  240. {
  241. struct tty_port *port = &up->port.state->port;
  242. int max_count = 256;
  243. do {
  244. unsigned int rdata;
  245. unsigned char ch;
  246. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  247. if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
  248. break;
  249. /* remove the character from the FIFO */
  250. ar933x_uart_write(up, AR933X_UART_DATA_REG,
  251. AR933X_UART_DATA_RX_CSR);
  252. up->port.icount.rx++;
  253. ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
  254. if (uart_handle_sysrq_char(&up->port, ch))
  255. continue;
  256. if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
  257. tty_insert_flip_char(port, ch, TTY_NORMAL);
  258. } while (max_count-- > 0);
  259. spin_unlock(&up->port.lock);
  260. tty_flip_buffer_push(port);
  261. spin_lock(&up->port.lock);
  262. }
  263. static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
  264. {
  265. struct circ_buf *xmit = &up->port.state->xmit;
  266. int count;
  267. if (uart_tx_stopped(&up->port))
  268. return;
  269. count = up->port.fifosize;
  270. do {
  271. unsigned int rdata;
  272. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  273. if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
  274. break;
  275. if (up->port.x_char) {
  276. ar933x_uart_putc(up, up->port.x_char);
  277. up->port.icount.tx++;
  278. up->port.x_char = 0;
  279. continue;
  280. }
  281. if (uart_circ_empty(xmit))
  282. break;
  283. ar933x_uart_putc(up, xmit->buf[xmit->tail]);
  284. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  285. up->port.icount.tx++;
  286. } while (--count > 0);
  287. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  288. uart_write_wakeup(&up->port);
  289. if (!uart_circ_empty(xmit))
  290. ar933x_uart_start_tx_interrupt(up);
  291. }
  292. static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
  293. {
  294. struct ar933x_uart_port *up = dev_id;
  295. unsigned int status;
  296. status = ar933x_uart_read(up, AR933X_UART_CS_REG);
  297. if ((status & AR933X_UART_CS_HOST_INT) == 0)
  298. return IRQ_NONE;
  299. spin_lock(&up->port.lock);
  300. status = ar933x_uart_read(up, AR933X_UART_INT_REG);
  301. status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  302. if (status & AR933X_UART_INT_RX_VALID) {
  303. ar933x_uart_write(up, AR933X_UART_INT_REG,
  304. AR933X_UART_INT_RX_VALID);
  305. ar933x_uart_rx_chars(up);
  306. }
  307. if (status & AR933X_UART_INT_TX_EMPTY) {
  308. ar933x_uart_write(up, AR933X_UART_INT_REG,
  309. AR933X_UART_INT_TX_EMPTY);
  310. ar933x_uart_stop_tx_interrupt(up);
  311. ar933x_uart_tx_chars(up);
  312. }
  313. spin_unlock(&up->port.lock);
  314. return IRQ_HANDLED;
  315. }
  316. static int ar933x_uart_startup(struct uart_port *port)
  317. {
  318. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  319. unsigned long flags;
  320. int ret;
  321. ret = request_irq(up->port.irq, ar933x_uart_interrupt,
  322. up->port.irqflags, dev_name(up->port.dev), up);
  323. if (ret)
  324. return ret;
  325. spin_lock_irqsave(&up->port.lock, flags);
  326. /* Enable HOST interrupts */
  327. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  328. AR933X_UART_CS_HOST_INT_EN);
  329. /* Enable RX interrupts */
  330. up->ier = AR933X_UART_INT_RX_VALID;
  331. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  332. spin_unlock_irqrestore(&up->port.lock, flags);
  333. return 0;
  334. }
  335. static void ar933x_uart_shutdown(struct uart_port *port)
  336. {
  337. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  338. /* Disable all interrupts */
  339. up->ier = 0;
  340. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  341. /* Disable break condition */
  342. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  343. AR933X_UART_CS_TX_BREAK);
  344. free_irq(up->port.irq, up);
  345. }
  346. static const char *ar933x_uart_type(struct uart_port *port)
  347. {
  348. return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
  349. }
  350. static void ar933x_uart_release_port(struct uart_port *port)
  351. {
  352. /* Nothing to release ... */
  353. }
  354. static int ar933x_uart_request_port(struct uart_port *port)
  355. {
  356. /* UARTs always present */
  357. return 0;
  358. }
  359. static void ar933x_uart_config_port(struct uart_port *port, int flags)
  360. {
  361. if (flags & UART_CONFIG_TYPE)
  362. port->type = PORT_AR933X;
  363. }
  364. static int ar933x_uart_verify_port(struct uart_port *port,
  365. struct serial_struct *ser)
  366. {
  367. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  368. if (ser->type != PORT_UNKNOWN &&
  369. ser->type != PORT_AR933X)
  370. return -EINVAL;
  371. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  372. return -EINVAL;
  373. if (ser->baud_base < up->min_baud ||
  374. ser->baud_base > up->max_baud)
  375. return -EINVAL;
  376. return 0;
  377. }
  378. static struct uart_ops ar933x_uart_ops = {
  379. .tx_empty = ar933x_uart_tx_empty,
  380. .set_mctrl = ar933x_uart_set_mctrl,
  381. .get_mctrl = ar933x_uart_get_mctrl,
  382. .stop_tx = ar933x_uart_stop_tx,
  383. .start_tx = ar933x_uart_start_tx,
  384. .stop_rx = ar933x_uart_stop_rx,
  385. .enable_ms = ar933x_uart_enable_ms,
  386. .break_ctl = ar933x_uart_break_ctl,
  387. .startup = ar933x_uart_startup,
  388. .shutdown = ar933x_uart_shutdown,
  389. .set_termios = ar933x_uart_set_termios,
  390. .type = ar933x_uart_type,
  391. .release_port = ar933x_uart_release_port,
  392. .request_port = ar933x_uart_request_port,
  393. .config_port = ar933x_uart_config_port,
  394. .verify_port = ar933x_uart_verify_port,
  395. };
  396. #ifdef CONFIG_SERIAL_AR933X_CONSOLE
  397. static struct ar933x_uart_port *
  398. ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
  399. static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
  400. {
  401. unsigned int status;
  402. unsigned int timeout = 60000;
  403. /* Wait up to 60ms for the character(s) to be sent. */
  404. do {
  405. status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  406. if (--timeout == 0)
  407. break;
  408. udelay(1);
  409. } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
  410. }
  411. static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
  412. {
  413. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  414. ar933x_uart_wait_xmitr(up);
  415. ar933x_uart_putc(up, ch);
  416. }
  417. static void ar933x_uart_console_write(struct console *co, const char *s,
  418. unsigned int count)
  419. {
  420. struct ar933x_uart_port *up = ar933x_console_ports[co->index];
  421. unsigned long flags;
  422. unsigned int int_en;
  423. int locked = 1;
  424. local_irq_save(flags);
  425. if (up->port.sysrq)
  426. locked = 0;
  427. else if (oops_in_progress)
  428. locked = spin_trylock(&up->port.lock);
  429. else
  430. spin_lock(&up->port.lock);
  431. /*
  432. * First save the IER then disable the interrupts
  433. */
  434. int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  435. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
  436. uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
  437. /*
  438. * Finally, wait for transmitter to become empty
  439. * and restore the IER
  440. */
  441. ar933x_uart_wait_xmitr(up);
  442. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
  443. ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
  444. if (locked)
  445. spin_unlock(&up->port.lock);
  446. local_irq_restore(flags);
  447. }
  448. static int ar933x_uart_console_setup(struct console *co, char *options)
  449. {
  450. struct ar933x_uart_port *up;
  451. int baud = 115200;
  452. int bits = 8;
  453. int parity = 'n';
  454. int flow = 'n';
  455. if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
  456. return -EINVAL;
  457. up = ar933x_console_ports[co->index];
  458. if (!up)
  459. return -ENODEV;
  460. if (options)
  461. uart_parse_options(options, &baud, &parity, &bits, &flow);
  462. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  463. }
  464. static struct console ar933x_uart_console = {
  465. .name = "ttyATH",
  466. .write = ar933x_uart_console_write,
  467. .device = uart_console_device,
  468. .setup = ar933x_uart_console_setup,
  469. .flags = CON_PRINTBUFFER,
  470. .index = -1,
  471. .data = &ar933x_uart_driver,
  472. };
  473. static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
  474. {
  475. ar933x_console_ports[up->port.line] = up;
  476. }
  477. #define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
  478. #else
  479. static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
  480. #define AR933X_SERIAL_CONSOLE NULL
  481. #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
  482. static struct uart_driver ar933x_uart_driver = {
  483. .owner = THIS_MODULE,
  484. .driver_name = DRIVER_NAME,
  485. .dev_name = "ttyATH",
  486. .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
  487. .cons = AR933X_SERIAL_CONSOLE,
  488. };
  489. static int ar933x_uart_probe(struct platform_device *pdev)
  490. {
  491. struct ar933x_uart_port *up;
  492. struct uart_port *port;
  493. struct resource *mem_res;
  494. struct resource *irq_res;
  495. unsigned int baud;
  496. int id;
  497. int ret;
  498. id = pdev->id;
  499. if (id == -1)
  500. id = 0;
  501. if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
  502. return -EINVAL;
  503. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  504. if (!irq_res) {
  505. dev_err(&pdev->dev, "no IRQ resource\n");
  506. return -EINVAL;
  507. }
  508. up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
  509. GFP_KERNEL);
  510. if (!up)
  511. return -ENOMEM;
  512. up->clk = devm_clk_get(&pdev->dev, "uart");
  513. if (IS_ERR(up->clk)) {
  514. dev_err(&pdev->dev, "unable to get UART clock\n");
  515. return PTR_ERR(up->clk);
  516. }
  517. port = &up->port;
  518. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  519. port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
  520. if (IS_ERR(port->membase))
  521. return PTR_ERR(port->membase);
  522. ret = clk_prepare_enable(up->clk);
  523. if (ret)
  524. return ret;
  525. port->uartclk = clk_get_rate(up->clk);
  526. if (!port->uartclk) {
  527. ret = -EINVAL;
  528. goto err_disable_clk;
  529. }
  530. port->mapbase = mem_res->start;
  531. port->line = id;
  532. port->irq = irq_res->start;
  533. port->dev = &pdev->dev;
  534. port->type = PORT_AR933X;
  535. port->iotype = UPIO_MEM32;
  536. port->regshift = 2;
  537. port->fifosize = AR933X_UART_FIFO_SIZE;
  538. port->ops = &ar933x_uart_ops;
  539. baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
  540. up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
  541. baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
  542. up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
  543. ar933x_uart_add_console_port(up);
  544. ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
  545. if (ret)
  546. goto err_disable_clk;
  547. platform_set_drvdata(pdev, up);
  548. return 0;
  549. err_disable_clk:
  550. clk_disable_unprepare(up->clk);
  551. return ret;
  552. }
  553. static int ar933x_uart_remove(struct platform_device *pdev)
  554. {
  555. struct ar933x_uart_port *up;
  556. up = platform_get_drvdata(pdev);
  557. if (up) {
  558. uart_remove_one_port(&ar933x_uart_driver, &up->port);
  559. clk_disable_unprepare(up->clk);
  560. }
  561. return 0;
  562. }
  563. static struct platform_driver ar933x_uart_platform_driver = {
  564. .probe = ar933x_uart_probe,
  565. .remove = ar933x_uart_remove,
  566. .driver = {
  567. .name = DRIVER_NAME,
  568. .owner = THIS_MODULE,
  569. },
  570. };
  571. static int __init ar933x_uart_init(void)
  572. {
  573. int ret;
  574. ret = uart_register_driver(&ar933x_uart_driver);
  575. if (ret)
  576. goto err_out;
  577. ret = platform_driver_register(&ar933x_uart_platform_driver);
  578. if (ret)
  579. goto err_unregister_uart_driver;
  580. return 0;
  581. err_unregister_uart_driver:
  582. uart_unregister_driver(&ar933x_uart_driver);
  583. err_out:
  584. return ret;
  585. }
  586. static void __exit ar933x_uart_exit(void)
  587. {
  588. platform_driver_unregister(&ar933x_uart_platform_driver);
  589. uart_unregister_driver(&ar933x_uart_driver);
  590. }
  591. module_init(ar933x_uart_init);
  592. module_exit(ar933x_uart_exit);
  593. MODULE_DESCRIPTION("Atheros AR933X UART driver");
  594. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  595. MODULE_LICENSE("GPL v2");
  596. MODULE_ALIAS("platform:" DRIVER_NAME);