efm32-uart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #if defined(CONFIG_SERIAL_EFM32_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  2. #define SUPPORT_SYSRQ
  3. #endif
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/io.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/console.h>
  9. #include <linux/sysrq.h>
  10. #include <linux/serial_core.h>
  11. #include <linux/tty_flip.h>
  12. #include <linux/slab.h>
  13. #include <linux/clk.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_data/efm32-uart.h>
  17. #define DRIVER_NAME "efm32-uart"
  18. #define DEV_NAME "ttyefm"
  19. #define UARTn_CTRL 0x00
  20. #define UARTn_CTRL_SYNC 0x0001
  21. #define UARTn_CTRL_TXBIL 0x1000
  22. #define UARTn_FRAME 0x04
  23. #define UARTn_FRAME_DATABITS__MASK 0x000f
  24. #define UARTn_FRAME_DATABITS(n) ((n) - 3)
  25. #define UARTn_FRAME_PARITY_NONE 0x0000
  26. #define UARTn_FRAME_PARITY_EVEN 0x0200
  27. #define UARTn_FRAME_PARITY_ODD 0x0300
  28. #define UARTn_FRAME_STOPBITS_HALF 0x0000
  29. #define UARTn_FRAME_STOPBITS_ONE 0x1000
  30. #define UARTn_FRAME_STOPBITS_TWO 0x3000
  31. #define UARTn_CMD 0x0c
  32. #define UARTn_CMD_RXEN 0x0001
  33. #define UARTn_CMD_RXDIS 0x0002
  34. #define UARTn_CMD_TXEN 0x0004
  35. #define UARTn_CMD_TXDIS 0x0008
  36. #define UARTn_STATUS 0x10
  37. #define UARTn_STATUS_TXENS 0x0002
  38. #define UARTn_STATUS_TXC 0x0020
  39. #define UARTn_STATUS_TXBL 0x0040
  40. #define UARTn_STATUS_RXDATAV 0x0080
  41. #define UARTn_CLKDIV 0x14
  42. #define UARTn_RXDATAX 0x18
  43. #define UARTn_RXDATAX_RXDATA__MASK 0x01ff
  44. #define UARTn_RXDATAX_PERR 0x4000
  45. #define UARTn_RXDATAX_FERR 0x8000
  46. /*
  47. * This is a software only flag used for ignore_status_mask and
  48. * read_status_mask! It's used for breaks that the hardware doesn't report
  49. * explicitly.
  50. */
  51. #define SW_UARTn_RXDATAX_BERR 0x2000
  52. #define UARTn_TXDATA 0x34
  53. #define UARTn_IF 0x40
  54. #define UARTn_IF_TXC 0x0001
  55. #define UARTn_IF_TXBL 0x0002
  56. #define UARTn_IF_RXDATAV 0x0004
  57. #define UARTn_IF_RXOF 0x0010
  58. #define UARTn_IFS 0x44
  59. #define UARTn_IFC 0x48
  60. #define UARTn_IEN 0x4c
  61. #define UARTn_ROUTE 0x54
  62. #define UARTn_ROUTE_LOCATION__MASK 0x0700
  63. #define UARTn_ROUTE_LOCATION(n) (((n) << 8) & UARTn_ROUTE_LOCATION__MASK)
  64. #define UARTn_ROUTE_RXPEN 0x0001
  65. #define UARTn_ROUTE_TXPEN 0x0002
  66. struct efm32_uart_port {
  67. struct uart_port port;
  68. unsigned int txirq;
  69. struct clk *clk;
  70. };
  71. #define to_efm_port(_port) container_of(_port, struct efm32_uart_port, port)
  72. #define efm_debug(efm_port, format, arg...) \
  73. dev_dbg(efm_port->port.dev, format, ##arg)
  74. static void efm32_uart_write32(struct efm32_uart_port *efm_port,
  75. u32 value, unsigned offset)
  76. {
  77. writel_relaxed(value, efm_port->port.membase + offset);
  78. }
  79. static u32 efm32_uart_read32(struct efm32_uart_port *efm_port,
  80. unsigned offset)
  81. {
  82. return readl_relaxed(efm_port->port.membase + offset);
  83. }
  84. static unsigned int efm32_uart_tx_empty(struct uart_port *port)
  85. {
  86. struct efm32_uart_port *efm_port = to_efm_port(port);
  87. u32 status = efm32_uart_read32(efm_port, UARTn_STATUS);
  88. if (status & UARTn_STATUS_TXC)
  89. return TIOCSER_TEMT;
  90. else
  91. return 0;
  92. }
  93. static void efm32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  94. {
  95. /* sorry, neither handshaking lines nor loop functionallity */
  96. }
  97. static unsigned int efm32_uart_get_mctrl(struct uart_port *port)
  98. {
  99. /* sorry, no handshaking lines available */
  100. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  101. }
  102. static void efm32_uart_stop_tx(struct uart_port *port)
  103. {
  104. struct efm32_uart_port *efm_port = to_efm_port(port);
  105. u32 ien = efm32_uart_read32(efm_port, UARTn_IEN);
  106. efm32_uart_write32(efm_port, UARTn_CMD_TXDIS, UARTn_CMD);
  107. ien &= ~(UARTn_IF_TXC | UARTn_IF_TXBL);
  108. efm32_uart_write32(efm_port, ien, UARTn_IEN);
  109. }
  110. static void efm32_uart_tx_chars(struct efm32_uart_port *efm_port)
  111. {
  112. struct uart_port *port = &efm_port->port;
  113. struct circ_buf *xmit = &port->state->xmit;
  114. while (efm32_uart_read32(efm_port, UARTn_STATUS) &
  115. UARTn_STATUS_TXBL) {
  116. if (port->x_char) {
  117. port->icount.tx++;
  118. efm32_uart_write32(efm_port, port->x_char,
  119. UARTn_TXDATA);
  120. port->x_char = 0;
  121. continue;
  122. }
  123. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  124. port->icount.tx++;
  125. efm32_uart_write32(efm_port, xmit->buf[xmit->tail],
  126. UARTn_TXDATA);
  127. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  128. } else
  129. break;
  130. }
  131. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  132. uart_write_wakeup(port);
  133. if (!port->x_char && uart_circ_empty(xmit) &&
  134. efm32_uart_read32(efm_port, UARTn_STATUS) &
  135. UARTn_STATUS_TXC)
  136. efm32_uart_stop_tx(port);
  137. }
  138. static void efm32_uart_start_tx(struct uart_port *port)
  139. {
  140. struct efm32_uart_port *efm_port = to_efm_port(port);
  141. u32 ien;
  142. efm32_uart_write32(efm_port,
  143. UARTn_IF_TXBL | UARTn_IF_TXC, UARTn_IFC);
  144. ien = efm32_uart_read32(efm_port, UARTn_IEN);
  145. efm32_uart_write32(efm_port,
  146. ien | UARTn_IF_TXBL | UARTn_IF_TXC, UARTn_IEN);
  147. efm32_uart_write32(efm_port, UARTn_CMD_TXEN, UARTn_CMD);
  148. efm32_uart_tx_chars(efm_port);
  149. }
  150. static void efm32_uart_stop_rx(struct uart_port *port)
  151. {
  152. struct efm32_uart_port *efm_port = to_efm_port(port);
  153. efm32_uart_write32(efm_port, UARTn_CMD_RXDIS, UARTn_CMD);
  154. }
  155. static void efm32_uart_enable_ms(struct uart_port *port)
  156. {
  157. /* no handshake lines, no modem status interrupts */
  158. }
  159. static void efm32_uart_break_ctl(struct uart_port *port, int ctl)
  160. {
  161. /* not possible without fiddling with gpios */
  162. }
  163. static void efm32_uart_rx_chars(struct efm32_uart_port *efm_port,
  164. struct tty_struct *tty)
  165. {
  166. struct uart_port *port = &efm_port->port;
  167. while (efm32_uart_read32(efm_port, UARTn_STATUS) &
  168. UARTn_STATUS_RXDATAV) {
  169. u32 rxdata = efm32_uart_read32(efm_port, UARTn_RXDATAX);
  170. int flag = 0;
  171. /*
  172. * This is a reserved bit and I only saw it read as 0. But to be
  173. * sure not to be confused too much by new devices adhere to the
  174. * warning in the reference manual that reserverd bits might
  175. * read as 1 in the future.
  176. */
  177. rxdata &= ~SW_UARTn_RXDATAX_BERR;
  178. port->icount.rx++;
  179. if ((rxdata & UARTn_RXDATAX_FERR) &&
  180. !(rxdata & UARTn_RXDATAX_RXDATA__MASK)) {
  181. rxdata |= SW_UARTn_RXDATAX_BERR;
  182. port->icount.brk++;
  183. if (uart_handle_break(port))
  184. continue;
  185. } else if (rxdata & UARTn_RXDATAX_PERR)
  186. port->icount.parity++;
  187. else if (rxdata & UARTn_RXDATAX_FERR)
  188. port->icount.frame++;
  189. rxdata &= port->read_status_mask;
  190. if (rxdata & SW_UARTn_RXDATAX_BERR)
  191. flag = TTY_BREAK;
  192. else if (rxdata & UARTn_RXDATAX_PERR)
  193. flag = TTY_PARITY;
  194. else if (rxdata & UARTn_RXDATAX_FERR)
  195. flag = TTY_FRAME;
  196. else if (uart_handle_sysrq_char(port,
  197. rxdata & UARTn_RXDATAX_RXDATA__MASK))
  198. continue;
  199. if (tty && (rxdata & port->ignore_status_mask) == 0)
  200. tty_insert_flip_char(tty,
  201. rxdata & UARTn_RXDATAX_RXDATA__MASK, flag);
  202. }
  203. }
  204. static irqreturn_t efm32_uart_rxirq(int irq, void *data)
  205. {
  206. struct efm32_uart_port *efm_port = data;
  207. u32 irqflag = efm32_uart_read32(efm_port, UARTn_IF);
  208. int handled = IRQ_NONE;
  209. struct uart_port *port = &efm_port->port;
  210. struct tty_struct *tty;
  211. spin_lock(&port->lock);
  212. tty = tty_kref_get(port->state->port.tty);
  213. if (irqflag & UARTn_IF_RXDATAV) {
  214. efm32_uart_write32(efm_port, UARTn_IF_RXDATAV, UARTn_IFC);
  215. efm32_uart_rx_chars(efm_port, tty);
  216. handled = IRQ_HANDLED;
  217. }
  218. if (irqflag & UARTn_IF_RXOF) {
  219. efm32_uart_write32(efm_port, UARTn_IF_RXOF, UARTn_IFC);
  220. port->icount.overrun++;
  221. if (tty)
  222. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  223. handled = IRQ_HANDLED;
  224. }
  225. if (tty) {
  226. tty_flip_buffer_push(tty);
  227. tty_kref_put(tty);
  228. }
  229. spin_unlock(&port->lock);
  230. return handled;
  231. }
  232. static irqreturn_t efm32_uart_txirq(int irq, void *data)
  233. {
  234. struct efm32_uart_port *efm_port = data;
  235. u32 irqflag = efm32_uart_read32(efm_port, UARTn_IF);
  236. /* TXBL doesn't need to be cleared */
  237. if (irqflag & UARTn_IF_TXC)
  238. efm32_uart_write32(efm_port, UARTn_IF_TXC, UARTn_IFC);
  239. if (irqflag & (UARTn_IF_TXC | UARTn_IF_TXBL)) {
  240. efm32_uart_tx_chars(efm_port);
  241. return IRQ_HANDLED;
  242. } else
  243. return IRQ_NONE;
  244. }
  245. static int efm32_uart_startup(struct uart_port *port)
  246. {
  247. struct efm32_uart_port *efm_port = to_efm_port(port);
  248. u32 location = 0;
  249. struct efm32_uart_pdata *pdata = dev_get_platdata(port->dev);
  250. int ret;
  251. if (pdata)
  252. location = UARTn_ROUTE_LOCATION(pdata->location);
  253. ret = clk_enable(efm_port->clk);
  254. if (ret) {
  255. efm_debug(efm_port, "failed to enable clk\n");
  256. goto err_clk_enable;
  257. }
  258. port->uartclk = clk_get_rate(efm_port->clk);
  259. /* Enable pins at configured location */
  260. efm32_uart_write32(efm_port, location | UARTn_ROUTE_RXPEN | UARTn_ROUTE_TXPEN,
  261. UARTn_ROUTE);
  262. ret = request_irq(port->irq, efm32_uart_rxirq, 0,
  263. DRIVER_NAME, efm_port);
  264. if (ret) {
  265. efm_debug(efm_port, "failed to register rxirq\n");
  266. goto err_request_irq_rx;
  267. }
  268. /* disable all irqs */
  269. efm32_uart_write32(efm_port, 0, UARTn_IEN);
  270. ret = request_irq(efm_port->txirq, efm32_uart_txirq, 0,
  271. DRIVER_NAME, efm_port);
  272. if (ret) {
  273. efm_debug(efm_port, "failed to register txirq\n");
  274. free_irq(port->irq, efm_port);
  275. err_request_irq_rx:
  276. clk_disable(efm_port->clk);
  277. } else {
  278. efm32_uart_write32(efm_port,
  279. UARTn_IF_RXDATAV | UARTn_IF_RXOF, UARTn_IEN);
  280. efm32_uart_write32(efm_port, UARTn_CMD_RXEN, UARTn_CMD);
  281. }
  282. err_clk_enable:
  283. return ret;
  284. }
  285. static void efm32_uart_shutdown(struct uart_port *port)
  286. {
  287. struct efm32_uart_port *efm_port = to_efm_port(port);
  288. efm32_uart_write32(efm_port, 0, UARTn_IEN);
  289. free_irq(port->irq, efm_port);
  290. clk_disable(efm_port->clk);
  291. }
  292. static void efm32_uart_set_termios(struct uart_port *port,
  293. struct ktermios *new, struct ktermios *old)
  294. {
  295. struct efm32_uart_port *efm_port = to_efm_port(port);
  296. unsigned long flags;
  297. unsigned baud;
  298. u32 clkdiv;
  299. u32 frame = 0;
  300. /* no modem control lines */
  301. new->c_cflag &= ~(CRTSCTS | CMSPAR);
  302. baud = uart_get_baud_rate(port, new, old,
  303. DIV_ROUND_CLOSEST(port->uartclk, 16 * 8192),
  304. DIV_ROUND_CLOSEST(port->uartclk, 16));
  305. switch (new->c_cflag & CSIZE) {
  306. case CS5:
  307. frame |= UARTn_FRAME_DATABITS(5);
  308. break;
  309. case CS6:
  310. frame |= UARTn_FRAME_DATABITS(6);
  311. break;
  312. case CS7:
  313. frame |= UARTn_FRAME_DATABITS(7);
  314. break;
  315. case CS8:
  316. frame |= UARTn_FRAME_DATABITS(8);
  317. break;
  318. }
  319. if (new->c_cflag & CSTOPB)
  320. /* the receiver only verifies the first stop bit */
  321. frame |= UARTn_FRAME_STOPBITS_TWO;
  322. else
  323. frame |= UARTn_FRAME_STOPBITS_ONE;
  324. if (new->c_cflag & PARENB) {
  325. if (new->c_cflag & PARODD)
  326. frame |= UARTn_FRAME_PARITY_ODD;
  327. else
  328. frame |= UARTn_FRAME_PARITY_EVEN;
  329. } else
  330. frame |= UARTn_FRAME_PARITY_NONE;
  331. /*
  332. * the 6 lowest bits of CLKDIV are dc, bit 6 has value 0.25.
  333. * port->uartclk <= 14e6, so 4 * port->uartclk doesn't overflow.
  334. */
  335. clkdiv = (DIV_ROUND_CLOSEST(4 * port->uartclk, 16 * baud) - 4) << 6;
  336. spin_lock_irqsave(&port->lock, flags);
  337. efm32_uart_write32(efm_port,
  338. UARTn_CMD_TXDIS | UARTn_CMD_RXDIS, UARTn_CMD);
  339. port->read_status_mask = UARTn_RXDATAX_RXDATA__MASK;
  340. if (new->c_iflag & INPCK)
  341. port->read_status_mask |=
  342. UARTn_RXDATAX_FERR | UARTn_RXDATAX_PERR;
  343. if (new->c_iflag & (BRKINT | PARMRK))
  344. port->read_status_mask |= SW_UARTn_RXDATAX_BERR;
  345. port->ignore_status_mask = 0;
  346. if (new->c_iflag & IGNPAR)
  347. port->ignore_status_mask |=
  348. UARTn_RXDATAX_FERR | UARTn_RXDATAX_PERR;
  349. if (new->c_iflag & IGNBRK)
  350. port->ignore_status_mask |= SW_UARTn_RXDATAX_BERR;
  351. uart_update_timeout(port, new->c_cflag, baud);
  352. efm32_uart_write32(efm_port, UARTn_CTRL_TXBIL, UARTn_CTRL);
  353. efm32_uart_write32(efm_port, frame, UARTn_FRAME);
  354. efm32_uart_write32(efm_port, clkdiv, UARTn_CLKDIV);
  355. efm32_uart_write32(efm_port, UARTn_CMD_TXEN | UARTn_CMD_RXEN,
  356. UARTn_CMD);
  357. spin_unlock_irqrestore(&port->lock, flags);
  358. }
  359. static const char *efm32_uart_type(struct uart_port *port)
  360. {
  361. return port->type == PORT_EFMUART ? "efm32-uart" : NULL;
  362. }
  363. static void efm32_uart_release_port(struct uart_port *port)
  364. {
  365. struct efm32_uart_port *efm_port = to_efm_port(port);
  366. clk_unprepare(efm_port->clk);
  367. clk_put(efm_port->clk);
  368. iounmap(port->membase);
  369. }
  370. static int efm32_uart_request_port(struct uart_port *port)
  371. {
  372. struct efm32_uart_port *efm_port = to_efm_port(port);
  373. int ret;
  374. port->membase = ioremap(port->mapbase, 60);
  375. if (!efm_port->port.membase) {
  376. ret = -ENOMEM;
  377. efm_debug(efm_port, "failed to remap\n");
  378. goto err_ioremap;
  379. }
  380. efm_port->clk = clk_get(port->dev, NULL);
  381. if (IS_ERR(efm_port->clk)) {
  382. ret = PTR_ERR(efm_port->clk);
  383. efm_debug(efm_port, "failed to get clock\n");
  384. goto err_clk_get;
  385. }
  386. ret = clk_prepare(efm_port->clk);
  387. if (ret) {
  388. clk_put(efm_port->clk);
  389. err_clk_get:
  390. iounmap(port->membase);
  391. err_ioremap:
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static void efm32_uart_config_port(struct uart_port *port, int type)
  397. {
  398. if (type & UART_CONFIG_TYPE &&
  399. !efm32_uart_request_port(port))
  400. port->type = PORT_EFMUART;
  401. }
  402. static int efm32_uart_verify_port(struct uart_port *port,
  403. struct serial_struct *serinfo)
  404. {
  405. int ret = 0;
  406. if (serinfo->type != PORT_UNKNOWN && serinfo->type != PORT_EFMUART)
  407. ret = -EINVAL;
  408. return ret;
  409. }
  410. static struct uart_ops efm32_uart_pops = {
  411. .tx_empty = efm32_uart_tx_empty,
  412. .set_mctrl = efm32_uart_set_mctrl,
  413. .get_mctrl = efm32_uart_get_mctrl,
  414. .stop_tx = efm32_uart_stop_tx,
  415. .start_tx = efm32_uart_start_tx,
  416. .stop_rx = efm32_uart_stop_rx,
  417. .enable_ms = efm32_uart_enable_ms,
  418. .break_ctl = efm32_uart_break_ctl,
  419. .startup = efm32_uart_startup,
  420. .shutdown = efm32_uart_shutdown,
  421. .set_termios = efm32_uart_set_termios,
  422. .type = efm32_uart_type,
  423. .release_port = efm32_uart_release_port,
  424. .request_port = efm32_uart_request_port,
  425. .config_port = efm32_uart_config_port,
  426. .verify_port = efm32_uart_verify_port,
  427. };
  428. static struct efm32_uart_port *efm32_uart_ports[5];
  429. #ifdef CONFIG_SERIAL_EFM32_UART_CONSOLE
  430. static void efm32_uart_console_putchar(struct uart_port *port, int ch)
  431. {
  432. struct efm32_uart_port *efm_port = to_efm_port(port);
  433. unsigned int timeout = 0x400;
  434. u32 status;
  435. while (1) {
  436. status = efm32_uart_read32(efm_port, UARTn_STATUS);
  437. if (status & UARTn_STATUS_TXBL)
  438. break;
  439. if (!timeout--)
  440. return;
  441. }
  442. efm32_uart_write32(efm_port, ch, UARTn_TXDATA);
  443. }
  444. static void efm32_uart_console_write(struct console *co, const char *s,
  445. unsigned int count)
  446. {
  447. struct efm32_uart_port *efm_port = efm32_uart_ports[co->index];
  448. u32 status = efm32_uart_read32(efm_port, UARTn_STATUS);
  449. unsigned int timeout = 0x400;
  450. if (!(status & UARTn_STATUS_TXENS))
  451. efm32_uart_write32(efm_port, UARTn_CMD_TXEN, UARTn_CMD);
  452. uart_console_write(&efm_port->port, s, count,
  453. efm32_uart_console_putchar);
  454. /* Wait for the transmitter to become empty */
  455. while (1) {
  456. u32 status = efm32_uart_read32(efm_port, UARTn_STATUS);
  457. if (status & UARTn_STATUS_TXC)
  458. break;
  459. if (!timeout--)
  460. break;
  461. }
  462. if (!(status & UARTn_STATUS_TXENS))
  463. efm32_uart_write32(efm_port, UARTn_CMD_TXDIS, UARTn_CMD);
  464. }
  465. static void efm32_uart_console_get_options(struct efm32_uart_port *efm_port,
  466. int *baud, int *parity, int *bits)
  467. {
  468. u32 ctrl = efm32_uart_read32(efm_port, UARTn_CTRL);
  469. u32 route, clkdiv, frame;
  470. if (ctrl & UARTn_CTRL_SYNC)
  471. /* not operating in async mode */
  472. return;
  473. route = efm32_uart_read32(efm_port, UARTn_ROUTE);
  474. if (!(route & UARTn_ROUTE_TXPEN))
  475. /* tx pin not routed */
  476. return;
  477. clkdiv = efm32_uart_read32(efm_port, UARTn_CLKDIV);
  478. *baud = DIV_ROUND_CLOSEST(4 * efm_port->port.uartclk,
  479. 16 * (4 + (clkdiv >> 6)));
  480. frame = efm32_uart_read32(efm_port, UARTn_FRAME);
  481. if (frame & UARTn_FRAME_PARITY_ODD)
  482. *parity = 'o';
  483. else if (frame & UARTn_FRAME_PARITY_EVEN)
  484. *parity = 'e';
  485. else
  486. *parity = 'n';
  487. *bits = (frame & UARTn_FRAME_DATABITS__MASK) -
  488. UARTn_FRAME_DATABITS(4) + 4;
  489. efm_debug(efm_port, "get_opts: options=%d%c%d\n",
  490. *baud, *parity, *bits);
  491. }
  492. static int efm32_uart_console_setup(struct console *co, char *options)
  493. {
  494. struct efm32_uart_port *efm_port;
  495. int baud = 115200;
  496. int bits = 8;
  497. int parity = 'n';
  498. int flow = 'n';
  499. int ret;
  500. if (co->index < 0 || co->index >= ARRAY_SIZE(efm32_uart_ports)) {
  501. unsigned i;
  502. for (i = 0; i < ARRAY_SIZE(efm32_uart_ports); ++i) {
  503. if (efm32_uart_ports[i]) {
  504. pr_warn("efm32-console: fall back to console index %u (from %hhi)\n",
  505. i, co->index);
  506. co->index = i;
  507. break;
  508. }
  509. }
  510. }
  511. efm_port = efm32_uart_ports[co->index];
  512. if (!efm_port) {
  513. pr_warn("efm32-console: No port at %d\n", co->index);
  514. return -ENODEV;
  515. }
  516. ret = clk_prepare(efm_port->clk);
  517. if (ret) {
  518. dev_warn(efm_port->port.dev,
  519. "console: clk_prepare failed: %d\n", ret);
  520. return ret;
  521. }
  522. efm_port->port.uartclk = clk_get_rate(efm_port->clk);
  523. if (options)
  524. uart_parse_options(options, &baud, &parity, &bits, &flow);
  525. else
  526. efm32_uart_console_get_options(efm_port,
  527. &baud, &parity, &bits);
  528. return uart_set_options(&efm_port->port, co, baud, parity, bits, flow);
  529. }
  530. static struct uart_driver efm32_uart_reg;
  531. static struct console efm32_uart_console = {
  532. .name = DEV_NAME,
  533. .write = efm32_uart_console_write,
  534. .device = uart_console_device,
  535. .setup = efm32_uart_console_setup,
  536. .flags = CON_PRINTBUFFER,
  537. .index = -1,
  538. .data = &efm32_uart_reg,
  539. };
  540. #else
  541. #define efm32_uart_console (*(struct console *)NULL)
  542. #endif /* ifdef CONFIG_SERIAL_EFM32_UART_CONSOLE / else */
  543. static struct uart_driver efm32_uart_reg = {
  544. .owner = THIS_MODULE,
  545. .driver_name = DRIVER_NAME,
  546. .dev_name = DEV_NAME,
  547. .nr = ARRAY_SIZE(efm32_uart_ports),
  548. .cons = &efm32_uart_console,
  549. };
  550. static int efm32_uart_probe_dt(struct platform_device *pdev,
  551. struct efm32_uart_port *efm_port)
  552. {
  553. struct device_node *np = pdev->dev.of_node;
  554. int ret;
  555. if (!np)
  556. return 1;
  557. ret = of_alias_get_id(np, "serial");
  558. if (ret < 0) {
  559. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  560. return ret;
  561. } else {
  562. efm_port->port.line = ret;
  563. return 0;
  564. }
  565. }
  566. static int __devinit efm32_uart_probe(struct platform_device *pdev)
  567. {
  568. struct efm32_uart_port *efm_port;
  569. struct resource *res;
  570. int ret;
  571. efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL);
  572. if (!efm_port) {
  573. dev_dbg(&pdev->dev, "failed to allocate private data\n");
  574. return -ENOMEM;
  575. }
  576. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  577. if (!res) {
  578. ret = -ENODEV;
  579. dev_dbg(&pdev->dev, "failed to determine base address\n");
  580. goto err_get_base;
  581. }
  582. if (resource_size(res) < 60) {
  583. ret = -EINVAL;
  584. dev_dbg(&pdev->dev, "memory resource too small\n");
  585. goto err_too_small;
  586. }
  587. ret = platform_get_irq(pdev, 0);
  588. if (ret <= 0) {
  589. dev_dbg(&pdev->dev, "failed to get rx irq\n");
  590. goto err_get_rxirq;
  591. }
  592. efm_port->port.irq = ret;
  593. ret = platform_get_irq(pdev, 1);
  594. if (ret <= 0)
  595. ret = efm_port->port.irq + 1;
  596. efm_port->txirq = ret;
  597. efm_port->port.dev = &pdev->dev;
  598. efm_port->port.mapbase = res->start;
  599. efm_port->port.type = PORT_EFMUART;
  600. efm_port->port.iotype = UPIO_MEM32;
  601. efm_port->port.fifosize = 2;
  602. efm_port->port.ops = &efm32_uart_pops;
  603. efm_port->port.flags = UPF_BOOT_AUTOCONF;
  604. ret = efm32_uart_probe_dt(pdev, efm_port);
  605. if (ret > 0)
  606. /* not created by device tree */
  607. efm_port->port.line = pdev->id;
  608. if (efm_port->port.line >= 0 &&
  609. efm_port->port.line < ARRAY_SIZE(efm32_uart_ports))
  610. efm32_uart_ports[efm_port->port.line] = efm_port;
  611. ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port);
  612. if (ret) {
  613. dev_dbg(&pdev->dev, "failed to add port: %d\n", ret);
  614. if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports))
  615. efm32_uart_ports[pdev->id] = NULL;
  616. err_get_rxirq:
  617. err_too_small:
  618. err_get_base:
  619. kfree(efm_port);
  620. } else {
  621. platform_set_drvdata(pdev, efm_port);
  622. dev_dbg(&pdev->dev, "\\o/\n");
  623. }
  624. return ret;
  625. }
  626. static int __devexit efm32_uart_remove(struct platform_device *pdev)
  627. {
  628. struct efm32_uart_port *efm_port = platform_get_drvdata(pdev);
  629. platform_set_drvdata(pdev, NULL);
  630. uart_remove_one_port(&efm32_uart_reg, &efm_port->port);
  631. if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports))
  632. efm32_uart_ports[pdev->id] = NULL;
  633. kfree(efm_port);
  634. return 0;
  635. }
  636. static struct of_device_id efm32_uart_dt_ids[] = {
  637. {
  638. .compatible = "efm32,uart",
  639. }, {
  640. /* sentinel */
  641. }
  642. };
  643. MODULE_DEVICE_TABLE(of, efm32_uart_dt_ids);
  644. static struct platform_driver efm32_uart_driver = {
  645. .probe = efm32_uart_probe,
  646. .remove = __devexit_p(efm32_uart_remove),
  647. .driver = {
  648. .name = DRIVER_NAME,
  649. .owner = THIS_MODULE,
  650. .of_match_table = efm32_uart_dt_ids,
  651. },
  652. };
  653. static int __init efm32_uart_init(void)
  654. {
  655. int ret;
  656. ret = uart_register_driver(&efm32_uart_reg);
  657. if (ret)
  658. return ret;
  659. ret = platform_driver_register(&efm32_uart_driver);
  660. if (ret)
  661. uart_unregister_driver(&efm32_uart_reg);
  662. pr_info("EFM32 UART/USART driver\n");
  663. return ret;
  664. }
  665. module_init(efm32_uart_init);
  666. static void __exit efm32_uart_exit(void)
  667. {
  668. platform_driver_unregister(&efm32_uart_driver);
  669. uart_unregister_driver(&efm32_uart_reg);
  670. }
  671. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  672. MODULE_DESCRIPTION("EFM32 UART/USART driver");
  673. MODULE_LICENSE("GPL v2");
  674. MODULE_ALIAS("platform:" DRIVER_NAME);