efm32-uart.c 20 KB

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