ar933x_uart.c 18 KB

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