efm32-uart.c 20 KB

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