efm32-uart.c 21 KB

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