efm32-uart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. {
  165. struct uart_port *port = &efm_port->port;
  166. while (efm32_uart_read32(efm_port, UARTn_STATUS) &
  167. UARTn_STATUS_RXDATAV) {
  168. u32 rxdata = efm32_uart_read32(efm_port, UARTn_RXDATAX);
  169. int flag = 0;
  170. /*
  171. * This is a reserved bit and I only saw it read as 0. But to be
  172. * sure not to be confused too much by new devices adhere to the
  173. * warning in the reference manual that reserverd bits might
  174. * read as 1 in the future.
  175. */
  176. rxdata &= ~SW_UARTn_RXDATAX_BERR;
  177. port->icount.rx++;
  178. if ((rxdata & UARTn_RXDATAX_FERR) &&
  179. !(rxdata & UARTn_RXDATAX_RXDATA__MASK)) {
  180. rxdata |= SW_UARTn_RXDATAX_BERR;
  181. port->icount.brk++;
  182. if (uart_handle_break(port))
  183. continue;
  184. } else if (rxdata & UARTn_RXDATAX_PERR)
  185. port->icount.parity++;
  186. else if (rxdata & UARTn_RXDATAX_FERR)
  187. port->icount.frame++;
  188. rxdata &= port->read_status_mask;
  189. if (rxdata & SW_UARTn_RXDATAX_BERR)
  190. flag = TTY_BREAK;
  191. else if (rxdata & UARTn_RXDATAX_PERR)
  192. flag = TTY_PARITY;
  193. else if (rxdata & UARTn_RXDATAX_FERR)
  194. flag = TTY_FRAME;
  195. else if (uart_handle_sysrq_char(port,
  196. rxdata & UARTn_RXDATAX_RXDATA__MASK))
  197. continue;
  198. if ((rxdata & port->ignore_status_mask) == 0)
  199. tty_insert_flip_char(&port->state->port,
  200. rxdata & UARTn_RXDATAX_RXDATA__MASK, flag);
  201. }
  202. }
  203. static irqreturn_t efm32_uart_rxirq(int irq, void *data)
  204. {
  205. struct efm32_uart_port *efm_port = data;
  206. u32 irqflag = efm32_uart_read32(efm_port, UARTn_IF);
  207. int handled = IRQ_NONE;
  208. struct uart_port *port = &efm_port->port;
  209. struct tty_port *tport = &port->state->port;
  210. struct tty_struct *tty;
  211. spin_lock(&port->lock);
  212. tty = tty_kref_get(tport->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);
  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. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  222. handled = IRQ_HANDLED;
  223. }
  224. if (tty) {
  225. tty_flip_buffer_push(tty);
  226. tty_kref_put(tty);
  227. }
  228. spin_unlock(&port->lock);
  229. return handled;
  230. }
  231. static irqreturn_t efm32_uart_txirq(int irq, void *data)
  232. {
  233. struct efm32_uart_port *efm_port = data;
  234. u32 irqflag = efm32_uart_read32(efm_port, UARTn_IF);
  235. /* TXBL doesn't need to be cleared */
  236. if (irqflag & UARTn_IF_TXC)
  237. efm32_uart_write32(efm_port, UARTn_IF_TXC, UARTn_IFC);
  238. if (irqflag & (UARTn_IF_TXC | UARTn_IF_TXBL)) {
  239. efm32_uart_tx_chars(efm_port);
  240. return IRQ_HANDLED;
  241. } else
  242. return IRQ_NONE;
  243. }
  244. static int efm32_uart_startup(struct uart_port *port)
  245. {
  246. struct efm32_uart_port *efm_port = to_efm_port(port);
  247. u32 location = 0;
  248. struct efm32_uart_pdata *pdata = dev_get_platdata(port->dev);
  249. int ret;
  250. if (pdata)
  251. location = UARTn_ROUTE_LOCATION(pdata->location);
  252. ret = clk_enable(efm_port->clk);
  253. if (ret) {
  254. efm_debug(efm_port, "failed to enable clk\n");
  255. goto err_clk_enable;
  256. }
  257. port->uartclk = clk_get_rate(efm_port->clk);
  258. /* Enable pins at configured location */
  259. efm32_uart_write32(efm_port, location | UARTn_ROUTE_RXPEN | UARTn_ROUTE_TXPEN,
  260. UARTn_ROUTE);
  261. ret = request_irq(port->irq, efm32_uart_rxirq, 0,
  262. DRIVER_NAME, efm_port);
  263. if (ret) {
  264. efm_debug(efm_port, "failed to register rxirq\n");
  265. goto err_request_irq_rx;
  266. }
  267. /* disable all irqs */
  268. efm32_uart_write32(efm_port, 0, UARTn_IEN);
  269. ret = request_irq(efm_port->txirq, efm32_uart_txirq, 0,
  270. DRIVER_NAME, efm_port);
  271. if (ret) {
  272. efm_debug(efm_port, "failed to register txirq\n");
  273. free_irq(port->irq, efm_port);
  274. err_request_irq_rx:
  275. clk_disable(efm_port->clk);
  276. } else {
  277. efm32_uart_write32(efm_port,
  278. UARTn_IF_RXDATAV | UARTn_IF_RXOF, UARTn_IEN);
  279. efm32_uart_write32(efm_port, UARTn_CMD_RXEN, UARTn_CMD);
  280. }
  281. err_clk_enable:
  282. return ret;
  283. }
  284. static void efm32_uart_shutdown(struct uart_port *port)
  285. {
  286. struct efm32_uart_port *efm_port = to_efm_port(port);
  287. efm32_uart_write32(efm_port, 0, UARTn_IEN);
  288. free_irq(port->irq, efm_port);
  289. clk_disable(efm_port->clk);
  290. }
  291. static void efm32_uart_set_termios(struct uart_port *port,
  292. struct ktermios *new, struct ktermios *old)
  293. {
  294. struct efm32_uart_port *efm_port = to_efm_port(port);
  295. unsigned long flags;
  296. unsigned baud;
  297. u32 clkdiv;
  298. u32 frame = 0;
  299. /* no modem control lines */
  300. new->c_cflag &= ~(CRTSCTS | CMSPAR);
  301. baud = uart_get_baud_rate(port, new, old,
  302. DIV_ROUND_CLOSEST(port->uartclk, 16 * 8192),
  303. DIV_ROUND_CLOSEST(port->uartclk, 16));
  304. switch (new->c_cflag & CSIZE) {
  305. case CS5:
  306. frame |= UARTn_FRAME_DATABITS(5);
  307. break;
  308. case CS6:
  309. frame |= UARTn_FRAME_DATABITS(6);
  310. break;
  311. case CS7:
  312. frame |= UARTn_FRAME_DATABITS(7);
  313. break;
  314. case CS8:
  315. frame |= UARTn_FRAME_DATABITS(8);
  316. break;
  317. }
  318. if (new->c_cflag & CSTOPB)
  319. /* the receiver only verifies the first stop bit */
  320. frame |= UARTn_FRAME_STOPBITS_TWO;
  321. else
  322. frame |= UARTn_FRAME_STOPBITS_ONE;
  323. if (new->c_cflag & PARENB) {
  324. if (new->c_cflag & PARODD)
  325. frame |= UARTn_FRAME_PARITY_ODD;
  326. else
  327. frame |= UARTn_FRAME_PARITY_EVEN;
  328. } else
  329. frame |= UARTn_FRAME_PARITY_NONE;
  330. /*
  331. * the 6 lowest bits of CLKDIV are dc, bit 6 has value 0.25.
  332. * port->uartclk <= 14e6, so 4 * port->uartclk doesn't overflow.
  333. */
  334. clkdiv = (DIV_ROUND_CLOSEST(4 * port->uartclk, 16 * baud) - 4) << 6;
  335. spin_lock_irqsave(&port->lock, flags);
  336. efm32_uart_write32(efm_port,
  337. UARTn_CMD_TXDIS | UARTn_CMD_RXDIS, UARTn_CMD);
  338. port->read_status_mask = UARTn_RXDATAX_RXDATA__MASK;
  339. if (new->c_iflag & INPCK)
  340. port->read_status_mask |=
  341. UARTn_RXDATAX_FERR | UARTn_RXDATAX_PERR;
  342. if (new->c_iflag & (BRKINT | PARMRK))
  343. port->read_status_mask |= SW_UARTn_RXDATAX_BERR;
  344. port->ignore_status_mask = 0;
  345. if (new->c_iflag & IGNPAR)
  346. port->ignore_status_mask |=
  347. UARTn_RXDATAX_FERR | UARTn_RXDATAX_PERR;
  348. if (new->c_iflag & IGNBRK)
  349. port->ignore_status_mask |= SW_UARTn_RXDATAX_BERR;
  350. uart_update_timeout(port, new->c_cflag, baud);
  351. efm32_uart_write32(efm_port, UARTn_CTRL_TXBIL, UARTn_CTRL);
  352. efm32_uart_write32(efm_port, frame, UARTn_FRAME);
  353. efm32_uart_write32(efm_port, clkdiv, UARTn_CLKDIV);
  354. efm32_uart_write32(efm_port, UARTn_CMD_TXEN | UARTn_CMD_RXEN,
  355. UARTn_CMD);
  356. spin_unlock_irqrestore(&port->lock, flags);
  357. }
  358. static const char *efm32_uart_type(struct uart_port *port)
  359. {
  360. return port->type == PORT_EFMUART ? "efm32-uart" : NULL;
  361. }
  362. static void efm32_uart_release_port(struct uart_port *port)
  363. {
  364. struct efm32_uart_port *efm_port = to_efm_port(port);
  365. clk_unprepare(efm_port->clk);
  366. clk_put(efm_port->clk);
  367. iounmap(port->membase);
  368. }
  369. static int efm32_uart_request_port(struct uart_port *port)
  370. {
  371. struct efm32_uart_port *efm_port = to_efm_port(port);
  372. int ret;
  373. port->membase = ioremap(port->mapbase, 60);
  374. if (!efm_port->port.membase) {
  375. ret = -ENOMEM;
  376. efm_debug(efm_port, "failed to remap\n");
  377. goto err_ioremap;
  378. }
  379. efm_port->clk = clk_get(port->dev, NULL);
  380. if (IS_ERR(efm_port->clk)) {
  381. ret = PTR_ERR(efm_port->clk);
  382. efm_debug(efm_port, "failed to get clock\n");
  383. goto err_clk_get;
  384. }
  385. ret = clk_prepare(efm_port->clk);
  386. if (ret) {
  387. clk_put(efm_port->clk);
  388. err_clk_get:
  389. iounmap(port->membase);
  390. err_ioremap:
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. static void efm32_uart_config_port(struct uart_port *port, int type)
  396. {
  397. if (type & UART_CONFIG_TYPE &&
  398. !efm32_uart_request_port(port))
  399. port->type = PORT_EFMUART;
  400. }
  401. static int efm32_uart_verify_port(struct uart_port *port,
  402. struct serial_struct *serinfo)
  403. {
  404. int ret = 0;
  405. if (serinfo->type != PORT_UNKNOWN && serinfo->type != PORT_EFMUART)
  406. ret = -EINVAL;
  407. return ret;
  408. }
  409. static struct uart_ops efm32_uart_pops = {
  410. .tx_empty = efm32_uart_tx_empty,
  411. .set_mctrl = efm32_uart_set_mctrl,
  412. .get_mctrl = efm32_uart_get_mctrl,
  413. .stop_tx = efm32_uart_stop_tx,
  414. .start_tx = efm32_uart_start_tx,
  415. .stop_rx = efm32_uart_stop_rx,
  416. .enable_ms = efm32_uart_enable_ms,
  417. .break_ctl = efm32_uart_break_ctl,
  418. .startup = efm32_uart_startup,
  419. .shutdown = efm32_uart_shutdown,
  420. .set_termios = efm32_uart_set_termios,
  421. .type = efm32_uart_type,
  422. .release_port = efm32_uart_release_port,
  423. .request_port = efm32_uart_request_port,
  424. .config_port = efm32_uart_config_port,
  425. .verify_port = efm32_uart_verify_port,
  426. };
  427. static struct efm32_uart_port *efm32_uart_ports[5];
  428. #ifdef CONFIG_SERIAL_EFM32_UART_CONSOLE
  429. static void efm32_uart_console_putchar(struct uart_port *port, int ch)
  430. {
  431. struct efm32_uart_port *efm_port = to_efm_port(port);
  432. unsigned int timeout = 0x400;
  433. u32 status;
  434. while (1) {
  435. status = efm32_uart_read32(efm_port, UARTn_STATUS);
  436. if (status & UARTn_STATUS_TXBL)
  437. break;
  438. if (!timeout--)
  439. return;
  440. }
  441. efm32_uart_write32(efm_port, ch, UARTn_TXDATA);
  442. }
  443. static void efm32_uart_console_write(struct console *co, const char *s,
  444. unsigned int count)
  445. {
  446. struct efm32_uart_port *efm_port = efm32_uart_ports[co->index];
  447. u32 status = efm32_uart_read32(efm_port, UARTn_STATUS);
  448. unsigned int timeout = 0x400;
  449. if (!(status & UARTn_STATUS_TXENS))
  450. efm32_uart_write32(efm_port, UARTn_CMD_TXEN, UARTn_CMD);
  451. uart_console_write(&efm_port->port, s, count,
  452. efm32_uart_console_putchar);
  453. /* Wait for the transmitter to become empty */
  454. while (1) {
  455. u32 status = efm32_uart_read32(efm_port, UARTn_STATUS);
  456. if (status & UARTn_STATUS_TXC)
  457. break;
  458. if (!timeout--)
  459. break;
  460. }
  461. if (!(status & UARTn_STATUS_TXENS))
  462. efm32_uart_write32(efm_port, UARTn_CMD_TXDIS, UARTn_CMD);
  463. }
  464. static void efm32_uart_console_get_options(struct efm32_uart_port *efm_port,
  465. int *baud, int *parity, int *bits)
  466. {
  467. u32 ctrl = efm32_uart_read32(efm_port, UARTn_CTRL);
  468. u32 route, clkdiv, frame;
  469. if (ctrl & UARTn_CTRL_SYNC)
  470. /* not operating in async mode */
  471. return;
  472. route = efm32_uart_read32(efm_port, UARTn_ROUTE);
  473. if (!(route & UARTn_ROUTE_TXPEN))
  474. /* tx pin not routed */
  475. return;
  476. clkdiv = efm32_uart_read32(efm_port, UARTn_CLKDIV);
  477. *baud = DIV_ROUND_CLOSEST(4 * efm_port->port.uartclk,
  478. 16 * (4 + (clkdiv >> 6)));
  479. frame = efm32_uart_read32(efm_port, UARTn_FRAME);
  480. if (frame & UARTn_FRAME_PARITY_ODD)
  481. *parity = 'o';
  482. else if (frame & UARTn_FRAME_PARITY_EVEN)
  483. *parity = 'e';
  484. else
  485. *parity = 'n';
  486. *bits = (frame & UARTn_FRAME_DATABITS__MASK) -
  487. UARTn_FRAME_DATABITS(4) + 4;
  488. efm_debug(efm_port, "get_opts: options=%d%c%d\n",
  489. *baud, *parity, *bits);
  490. }
  491. static int efm32_uart_console_setup(struct console *co, char *options)
  492. {
  493. struct efm32_uart_port *efm_port;
  494. int baud = 115200;
  495. int bits = 8;
  496. int parity = 'n';
  497. int flow = 'n';
  498. int ret;
  499. if (co->index < 0 || co->index >= ARRAY_SIZE(efm32_uart_ports)) {
  500. unsigned i;
  501. for (i = 0; i < ARRAY_SIZE(efm32_uart_ports); ++i) {
  502. if (efm32_uart_ports[i]) {
  503. pr_warn("efm32-console: fall back to console index %u (from %hhi)\n",
  504. i, co->index);
  505. co->index = i;
  506. break;
  507. }
  508. }
  509. }
  510. efm_port = efm32_uart_ports[co->index];
  511. if (!efm_port) {
  512. pr_warn("efm32-console: No port at %d\n", co->index);
  513. return -ENODEV;
  514. }
  515. ret = clk_prepare(efm_port->clk);
  516. if (ret) {
  517. dev_warn(efm_port->port.dev,
  518. "console: clk_prepare failed: %d\n", ret);
  519. return ret;
  520. }
  521. efm_port->port.uartclk = clk_get_rate(efm_port->clk);
  522. if (options)
  523. uart_parse_options(options, &baud, &parity, &bits, &flow);
  524. else
  525. efm32_uart_console_get_options(efm_port,
  526. &baud, &parity, &bits);
  527. return uart_set_options(&efm_port->port, co, baud, parity, bits, flow);
  528. }
  529. static struct uart_driver efm32_uart_reg;
  530. static struct console efm32_uart_console = {
  531. .name = DEV_NAME,
  532. .write = efm32_uart_console_write,
  533. .device = uart_console_device,
  534. .setup = efm32_uart_console_setup,
  535. .flags = CON_PRINTBUFFER,
  536. .index = -1,
  537. .data = &efm32_uart_reg,
  538. };
  539. #else
  540. #define efm32_uart_console (*(struct console *)NULL)
  541. #endif /* ifdef CONFIG_SERIAL_EFM32_UART_CONSOLE / else */
  542. static struct uart_driver efm32_uart_reg = {
  543. .owner = THIS_MODULE,
  544. .driver_name = DRIVER_NAME,
  545. .dev_name = DEV_NAME,
  546. .nr = ARRAY_SIZE(efm32_uart_ports),
  547. .cons = &efm32_uart_console,
  548. };
  549. static int efm32_uart_probe_dt(struct platform_device *pdev,
  550. struct efm32_uart_port *efm_port)
  551. {
  552. struct device_node *np = pdev->dev.of_node;
  553. int ret;
  554. if (!np)
  555. return 1;
  556. ret = of_alias_get_id(np, "serial");
  557. if (ret < 0) {
  558. dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
  559. return ret;
  560. } else {
  561. efm_port->port.line = ret;
  562. return 0;
  563. }
  564. }
  565. static int efm32_uart_probe(struct platform_device *pdev)
  566. {
  567. struct efm32_uart_port *efm_port;
  568. struct resource *res;
  569. int ret;
  570. efm_port = kzalloc(sizeof(*efm_port), GFP_KERNEL);
  571. if (!efm_port) {
  572. dev_dbg(&pdev->dev, "failed to allocate private data\n");
  573. return -ENOMEM;
  574. }
  575. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  576. if (!res) {
  577. ret = -ENODEV;
  578. dev_dbg(&pdev->dev, "failed to determine base address\n");
  579. goto err_get_base;
  580. }
  581. if (resource_size(res) < 60) {
  582. ret = -EINVAL;
  583. dev_dbg(&pdev->dev, "memory resource too small\n");
  584. goto err_too_small;
  585. }
  586. ret = platform_get_irq(pdev, 0);
  587. if (ret <= 0) {
  588. dev_dbg(&pdev->dev, "failed to get rx irq\n");
  589. goto err_get_rxirq;
  590. }
  591. efm_port->port.irq = ret;
  592. ret = platform_get_irq(pdev, 1);
  593. if (ret <= 0)
  594. ret = efm_port->port.irq + 1;
  595. efm_port->txirq = ret;
  596. efm_port->port.dev = &pdev->dev;
  597. efm_port->port.mapbase = res->start;
  598. efm_port->port.type = PORT_EFMUART;
  599. efm_port->port.iotype = UPIO_MEM32;
  600. efm_port->port.fifosize = 2;
  601. efm_port->port.ops = &efm32_uart_pops;
  602. efm_port->port.flags = UPF_BOOT_AUTOCONF;
  603. ret = efm32_uart_probe_dt(pdev, efm_port);
  604. if (ret > 0)
  605. /* not created by device tree */
  606. efm_port->port.line = pdev->id;
  607. if (efm_port->port.line >= 0 &&
  608. efm_port->port.line < ARRAY_SIZE(efm32_uart_ports))
  609. efm32_uart_ports[efm_port->port.line] = efm_port;
  610. ret = uart_add_one_port(&efm32_uart_reg, &efm_port->port);
  611. if (ret) {
  612. dev_dbg(&pdev->dev, "failed to add port: %d\n", ret);
  613. if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports))
  614. efm32_uart_ports[pdev->id] = NULL;
  615. err_get_rxirq:
  616. err_too_small:
  617. err_get_base:
  618. kfree(efm_port);
  619. } else {
  620. platform_set_drvdata(pdev, efm_port);
  621. dev_dbg(&pdev->dev, "\\o/\n");
  622. }
  623. return ret;
  624. }
  625. static int efm32_uart_remove(struct platform_device *pdev)
  626. {
  627. struct efm32_uart_port *efm_port = platform_get_drvdata(pdev);
  628. platform_set_drvdata(pdev, NULL);
  629. uart_remove_one_port(&efm32_uart_reg, &efm_port->port);
  630. if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(efm32_uart_ports))
  631. efm32_uart_ports[pdev->id] = NULL;
  632. kfree(efm_port);
  633. return 0;
  634. }
  635. static struct of_device_id efm32_uart_dt_ids[] = {
  636. {
  637. .compatible = "efm32,uart",
  638. }, {
  639. /* sentinel */
  640. }
  641. };
  642. MODULE_DEVICE_TABLE(of, efm32_uart_dt_ids);
  643. static struct platform_driver efm32_uart_driver = {
  644. .probe = efm32_uart_probe,
  645. .remove = efm32_uart_remove,
  646. .driver = {
  647. .name = DRIVER_NAME,
  648. .owner = THIS_MODULE,
  649. .of_match_table = efm32_uart_dt_ids,
  650. },
  651. };
  652. static int __init efm32_uart_init(void)
  653. {
  654. int ret;
  655. ret = uart_register_driver(&efm32_uart_reg);
  656. if (ret)
  657. return ret;
  658. ret = platform_driver_register(&efm32_uart_driver);
  659. if (ret)
  660. uart_unregister_driver(&efm32_uart_reg);
  661. pr_info("EFM32 UART/USART driver\n");
  662. return ret;
  663. }
  664. module_init(efm32_uart_init);
  665. static void __exit efm32_uart_exit(void)
  666. {
  667. platform_driver_unregister(&efm32_uart_driver);
  668. uart_unregister_driver(&efm32_uart_reg);
  669. }
  670. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  671. MODULE_DESCRIPTION("EFM32 UART/USART driver");
  672. MODULE_LICENSE("GPL v2");
  673. MODULE_ALIAS("platform:" DRIVER_NAME);