ar933x_uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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_struct *tty;
  241. int max_count = 256;
  242. tty = tty_port_tty_get(&up->port.state->port);
  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. if (!tty) {
  253. /* discard the data if no tty available */
  254. continue;
  255. }
  256. up->port.icount.rx++;
  257. ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
  258. if (uart_handle_sysrq_char(&up->port, ch))
  259. continue;
  260. if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
  261. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  262. } while (max_count-- > 0);
  263. if (tty) {
  264. tty_flip_buffer_push(tty);
  265. tty_kref_put(tty);
  266. }
  267. }
  268. static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
  269. {
  270. struct circ_buf *xmit = &up->port.state->xmit;
  271. int count;
  272. if (uart_tx_stopped(&up->port))
  273. return;
  274. count = up->port.fifosize;
  275. do {
  276. unsigned int rdata;
  277. rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  278. if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
  279. break;
  280. if (up->port.x_char) {
  281. ar933x_uart_putc(up, up->port.x_char);
  282. up->port.icount.tx++;
  283. up->port.x_char = 0;
  284. continue;
  285. }
  286. if (uart_circ_empty(xmit))
  287. break;
  288. ar933x_uart_putc(up, xmit->buf[xmit->tail]);
  289. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  290. up->port.icount.tx++;
  291. } while (--count > 0);
  292. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  293. uart_write_wakeup(&up->port);
  294. if (!uart_circ_empty(xmit))
  295. ar933x_uart_start_tx_interrupt(up);
  296. }
  297. static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
  298. {
  299. struct ar933x_uart_port *up = dev_id;
  300. unsigned int status;
  301. status = ar933x_uart_read(up, AR933X_UART_CS_REG);
  302. if ((status & AR933X_UART_CS_HOST_INT) == 0)
  303. return IRQ_NONE;
  304. spin_lock(&up->port.lock);
  305. status = ar933x_uart_read(up, AR933X_UART_INT_REG);
  306. status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  307. if (status & AR933X_UART_INT_RX_VALID) {
  308. ar933x_uart_write(up, AR933X_UART_INT_REG,
  309. AR933X_UART_INT_RX_VALID);
  310. ar933x_uart_rx_chars(up);
  311. }
  312. if (status & AR933X_UART_INT_TX_EMPTY) {
  313. ar933x_uart_write(up, AR933X_UART_INT_REG,
  314. AR933X_UART_INT_TX_EMPTY);
  315. ar933x_uart_stop_tx_interrupt(up);
  316. ar933x_uart_tx_chars(up);
  317. }
  318. spin_unlock(&up->port.lock);
  319. return IRQ_HANDLED;
  320. }
  321. static int ar933x_uart_startup(struct uart_port *port)
  322. {
  323. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  324. unsigned long flags;
  325. int ret;
  326. ret = request_irq(up->port.irq, ar933x_uart_interrupt,
  327. up->port.irqflags, dev_name(up->port.dev), up);
  328. if (ret)
  329. return ret;
  330. spin_lock_irqsave(&up->port.lock, flags);
  331. /* Enable HOST interrupts */
  332. ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
  333. AR933X_UART_CS_HOST_INT_EN);
  334. /* Enable RX interrupts */
  335. up->ier = AR933X_UART_INT_RX_VALID;
  336. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  337. spin_unlock_irqrestore(&up->port.lock, flags);
  338. return 0;
  339. }
  340. static void ar933x_uart_shutdown(struct uart_port *port)
  341. {
  342. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  343. /* Disable all interrupts */
  344. up->ier = 0;
  345. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
  346. /* Disable break condition */
  347. ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
  348. AR933X_UART_CS_TX_BREAK);
  349. free_irq(up->port.irq, up);
  350. }
  351. static const char *ar933x_uart_type(struct uart_port *port)
  352. {
  353. return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
  354. }
  355. static void ar933x_uart_release_port(struct uart_port *port)
  356. {
  357. /* Nothing to release ... */
  358. }
  359. static int ar933x_uart_request_port(struct uart_port *port)
  360. {
  361. /* UARTs always present */
  362. return 0;
  363. }
  364. static void ar933x_uart_config_port(struct uart_port *port, int flags)
  365. {
  366. if (flags & UART_CONFIG_TYPE)
  367. port->type = PORT_AR933X;
  368. }
  369. static int ar933x_uart_verify_port(struct uart_port *port,
  370. struct serial_struct *ser)
  371. {
  372. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  373. if (ser->type != PORT_UNKNOWN &&
  374. ser->type != PORT_AR933X)
  375. return -EINVAL;
  376. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  377. return -EINVAL;
  378. if (ser->baud_base < up->min_baud ||
  379. ser->baud_base > up->max_baud)
  380. return -EINVAL;
  381. return 0;
  382. }
  383. static struct uart_ops ar933x_uart_ops = {
  384. .tx_empty = ar933x_uart_tx_empty,
  385. .set_mctrl = ar933x_uart_set_mctrl,
  386. .get_mctrl = ar933x_uart_get_mctrl,
  387. .stop_tx = ar933x_uart_stop_tx,
  388. .start_tx = ar933x_uart_start_tx,
  389. .stop_rx = ar933x_uart_stop_rx,
  390. .enable_ms = ar933x_uart_enable_ms,
  391. .break_ctl = ar933x_uart_break_ctl,
  392. .startup = ar933x_uart_startup,
  393. .shutdown = ar933x_uart_shutdown,
  394. .set_termios = ar933x_uart_set_termios,
  395. .type = ar933x_uart_type,
  396. .release_port = ar933x_uart_release_port,
  397. .request_port = ar933x_uart_request_port,
  398. .config_port = ar933x_uart_config_port,
  399. .verify_port = ar933x_uart_verify_port,
  400. };
  401. #ifdef CONFIG_SERIAL_AR933X_CONSOLE
  402. static struct ar933x_uart_port *
  403. ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
  404. static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
  405. {
  406. unsigned int status;
  407. unsigned int timeout = 60000;
  408. /* Wait up to 60ms for the character(s) to be sent. */
  409. do {
  410. status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
  411. if (--timeout == 0)
  412. break;
  413. udelay(1);
  414. } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
  415. }
  416. static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
  417. {
  418. struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
  419. ar933x_uart_wait_xmitr(up);
  420. ar933x_uart_putc(up, ch);
  421. }
  422. static void ar933x_uart_console_write(struct console *co, const char *s,
  423. unsigned int count)
  424. {
  425. struct ar933x_uart_port *up = ar933x_console_ports[co->index];
  426. unsigned long flags;
  427. unsigned int int_en;
  428. int locked = 1;
  429. local_irq_save(flags);
  430. if (up->port.sysrq)
  431. locked = 0;
  432. else if (oops_in_progress)
  433. locked = spin_trylock(&up->port.lock);
  434. else
  435. spin_lock(&up->port.lock);
  436. /*
  437. * First save the IER then disable the interrupts
  438. */
  439. int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
  440. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
  441. uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
  442. /*
  443. * Finally, wait for transmitter to become empty
  444. * and restore the IER
  445. */
  446. ar933x_uart_wait_xmitr(up);
  447. ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
  448. ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
  449. if (locked)
  450. spin_unlock(&up->port.lock);
  451. local_irq_restore(flags);
  452. }
  453. static int ar933x_uart_console_setup(struct console *co, char *options)
  454. {
  455. struct ar933x_uart_port *up;
  456. int baud = 115200;
  457. int bits = 8;
  458. int parity = 'n';
  459. int flow = 'n';
  460. if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
  461. return -EINVAL;
  462. up = ar933x_console_ports[co->index];
  463. if (!up)
  464. return -ENODEV;
  465. if (options)
  466. uart_parse_options(options, &baud, &parity, &bits, &flow);
  467. return uart_set_options(&up->port, co, baud, parity, bits, flow);
  468. }
  469. static struct console ar933x_uart_console = {
  470. .name = "ttyATH",
  471. .write = ar933x_uart_console_write,
  472. .device = uart_console_device,
  473. .setup = ar933x_uart_console_setup,
  474. .flags = CON_PRINTBUFFER,
  475. .index = -1,
  476. .data = &ar933x_uart_driver,
  477. };
  478. static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
  479. {
  480. ar933x_console_ports[up->port.line] = up;
  481. }
  482. #define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
  483. #else
  484. static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
  485. #define AR933X_SERIAL_CONSOLE NULL
  486. #endif /* CONFIG_SERIAL_AR933X_CONSOLE */
  487. static struct uart_driver ar933x_uart_driver = {
  488. .owner = THIS_MODULE,
  489. .driver_name = DRIVER_NAME,
  490. .dev_name = "ttyATH",
  491. .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
  492. .cons = AR933X_SERIAL_CONSOLE,
  493. };
  494. static int ar933x_uart_probe(struct platform_device *pdev)
  495. {
  496. struct ar933x_uart_platform_data *pdata;
  497. struct ar933x_uart_port *up;
  498. struct uart_port *port;
  499. struct resource *mem_res;
  500. struct resource *irq_res;
  501. unsigned int baud;
  502. int id;
  503. int ret;
  504. pdata = pdev->dev.platform_data;
  505. if (!pdata)
  506. return -EINVAL;
  507. id = pdev->id;
  508. if (id == -1)
  509. id = 0;
  510. if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
  511. return -EINVAL;
  512. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  513. if (!mem_res) {
  514. dev_err(&pdev->dev, "no MEM resource\n");
  515. return -EINVAL;
  516. }
  517. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  518. if (!irq_res) {
  519. dev_err(&pdev->dev, "no IRQ resource\n");
  520. return -EINVAL;
  521. }
  522. up = kzalloc(sizeof(struct ar933x_uart_port), GFP_KERNEL);
  523. if (!up)
  524. return -ENOMEM;
  525. port = &up->port;
  526. port->mapbase = mem_res->start;
  527. port->membase = ioremap(mem_res->start, AR933X_UART_REGS_SIZE);
  528. if (!port->membase) {
  529. ret = -ENOMEM;
  530. goto err_free_up;
  531. }
  532. port->line = id;
  533. port->irq = irq_res->start;
  534. port->dev = &pdev->dev;
  535. port->type = PORT_AR933X;
  536. port->iotype = UPIO_MEM32;
  537. port->uartclk = pdata->uartclk;
  538. port->regshift = 2;
  539. port->fifosize = AR933X_UART_FIFO_SIZE;
  540. port->ops = &ar933x_uart_ops;
  541. baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
  542. up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
  543. baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
  544. up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
  545. ar933x_uart_add_console_port(up);
  546. ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
  547. if (ret)
  548. goto err_unmap;
  549. platform_set_drvdata(pdev, up);
  550. return 0;
  551. err_unmap:
  552. iounmap(up->port.membase);
  553. err_free_up:
  554. kfree(up);
  555. return ret;
  556. }
  557. static int ar933x_uart_remove(struct platform_device *pdev)
  558. {
  559. struct ar933x_uart_port *up;
  560. up = platform_get_drvdata(pdev);
  561. platform_set_drvdata(pdev, NULL);
  562. if (up) {
  563. uart_remove_one_port(&ar933x_uart_driver, &up->port);
  564. iounmap(up->port.membase);
  565. kfree(up);
  566. }
  567. return 0;
  568. }
  569. static struct platform_driver ar933x_uart_platform_driver = {
  570. .probe = ar933x_uart_probe,
  571. .remove = ar933x_uart_remove,
  572. .driver = {
  573. .name = DRIVER_NAME,
  574. .owner = THIS_MODULE,
  575. },
  576. };
  577. static int __init ar933x_uart_init(void)
  578. {
  579. int ret;
  580. ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS;
  581. ret = uart_register_driver(&ar933x_uart_driver);
  582. if (ret)
  583. goto err_out;
  584. ret = platform_driver_register(&ar933x_uart_platform_driver);
  585. if (ret)
  586. goto err_unregister_uart_driver;
  587. return 0;
  588. err_unregister_uart_driver:
  589. uart_unregister_driver(&ar933x_uart_driver);
  590. err_out:
  591. return ret;
  592. }
  593. static void __exit ar933x_uart_exit(void)
  594. {
  595. platform_driver_unregister(&ar933x_uart_platform_driver);
  596. uart_unregister_driver(&ar933x_uart_driver);
  597. }
  598. module_init(ar933x_uart_init);
  599. module_exit(ar933x_uart_exit);
  600. MODULE_DESCRIPTION("Atheros AR933X UART driver");
  601. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  602. MODULE_LICENSE("GPL v2");
  603. MODULE_ALIAS("platform:" DRIVER_NAME);