ar933x_uart.c 17 KB

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