efm32-uart.c 21 KB

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