efm32-uart.c 21 KB

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