esp32_uart.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/bitfield.h>
  3. #include <linux/bits.h>
  4. #include <linux/clk.h>
  5. #include <linux/console.h>
  6. #include <linux/delay.h>
  7. #include <linux/io.h>
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. #include <linux/serial_core.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty_flip.h>
  16. #include <asm/serial.h>
  17. #define DRIVER_NAME "esp32-uart"
  18. #define DEV_NAME "ttyS"
  19. #define UART_NR 3
  20. #define ESP32_UART_TX_FIFO_SIZE 127
  21. #define ESP32_UART_RX_FIFO_SIZE 127
  22. #define UART_FIFO_REG 0x00
  23. #define UART_INT_RAW_REG 0x04
  24. #define UART_INT_ST_REG 0x08
  25. #define UART_INT_ENA_REG 0x0c
  26. #define UART_INT_CLR_REG 0x10
  27. #define UART_RXFIFO_FULL_INT BIT(0)
  28. #define UART_TXFIFO_EMPTY_INT BIT(1)
  29. #define UART_BRK_DET_INT BIT(7)
  30. #define UART_CLKDIV_REG 0x14
  31. #define ESP32_UART_CLKDIV GENMASK(19, 0)
  32. #define ESP32S3_UART_CLKDIV GENMASK(11, 0)
  33. #define UART_CLKDIV_SHIFT 0
  34. #define UART_CLKDIV_FRAG GENMASK(23, 20)
  35. #define UART_STATUS_REG 0x1c
  36. #define ESP32_UART_RXFIFO_CNT GENMASK(7, 0)
  37. #define ESP32S3_UART_RXFIFO_CNT GENMASK(9, 0)
  38. #define UART_RXFIFO_CNT_SHIFT 0
  39. #define UART_DSRN BIT(13)
  40. #define UART_CTSN BIT(14)
  41. #define ESP32_UART_TXFIFO_CNT GENMASK(23, 16)
  42. #define ESP32S3_UART_TXFIFO_CNT GENMASK(25, 16)
  43. #define UART_TXFIFO_CNT_SHIFT 16
  44. #define UART_CONF0_REG 0x20
  45. #define UART_PARITY BIT(0)
  46. #define UART_PARITY_EN BIT(1)
  47. #define UART_BIT_NUM GENMASK(3, 2)
  48. #define UART_BIT_NUM_5 0
  49. #define UART_BIT_NUM_6 1
  50. #define UART_BIT_NUM_7 2
  51. #define UART_BIT_NUM_8 3
  52. #define UART_STOP_BIT_NUM GENMASK(5, 4)
  53. #define UART_STOP_BIT_NUM_1 1
  54. #define UART_STOP_BIT_NUM_2 3
  55. #define UART_SW_RTS BIT(6)
  56. #define UART_SW_DTR BIT(7)
  57. #define UART_LOOPBACK BIT(14)
  58. #define UART_TX_FLOW_EN BIT(15)
  59. #define UART_RTS_INV BIT(23)
  60. #define UART_DTR_INV BIT(24)
  61. #define UART_CONF1_REG 0x24
  62. #define UART_RXFIFO_FULL_THRHD_SHIFT 0
  63. #define ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT 8
  64. #define ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT 10
  65. #define ESP32_UART_RX_FLOW_EN BIT(23)
  66. #define ESP32S3_UART_RX_FLOW_EN BIT(22)
  67. #define ESP32S3_UART_CLK_CONF_REG 0x78
  68. #define ESP32S3_UART_SCLK_DIV_B GENMASK(5, 0)
  69. #define ESP32S3_UART_SCLK_DIV_A GENMASK(11, 6)
  70. #define ESP32S3_UART_SCLK_DIV_NUM GENMASK(19, 12)
  71. #define ESP32S3_UART_SCLK_SEL GENMASK(21, 20)
  72. #define APB_CLK 1
  73. #define RC_FAST_CLK 2
  74. #define XTAL_CLK 3
  75. #define ESP32S3_UART_SCLK_EN BIT(22)
  76. #define ESP32S3_UART_RST_CORE BIT(23)
  77. #define ESP32S3_UART_TX_SCLK_EN BIT(24)
  78. #define ESP32S3_UART_RX_SCLK_EN BIT(25)
  79. #define ESP32S3_UART_TX_RST_CORE BIT(26)
  80. #define ESP32S3_UART_RX_RST_CORE BIT(27)
  81. #define ESP32S3_UART_CLK_CONF_DEFAULT \
  82. (ESP32S3_UART_RX_SCLK_EN | \
  83. ESP32S3_UART_TX_SCLK_EN | \
  84. ESP32S3_UART_SCLK_EN | \
  85. FIELD_PREP(ESP32S3_UART_SCLK_SEL, XTAL_CLK))
  86. struct esp32_port {
  87. struct uart_port port;
  88. struct clk *clk;
  89. };
  90. struct esp32_uart_variant {
  91. u32 clkdiv_mask;
  92. u32 rxfifo_cnt_mask;
  93. u32 txfifo_cnt_mask;
  94. u32 txfifo_empty_thrhd_shift;
  95. u32 rx_flow_en;
  96. const char *type;
  97. bool has_clkconf;
  98. };
  99. static const struct esp32_uart_variant esp32_variant = {
  100. .clkdiv_mask = ESP32_UART_CLKDIV,
  101. .rxfifo_cnt_mask = ESP32_UART_RXFIFO_CNT,
  102. .txfifo_cnt_mask = ESP32_UART_TXFIFO_CNT,
  103. .txfifo_empty_thrhd_shift = ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT,
  104. .rx_flow_en = ESP32_UART_RX_FLOW_EN,
  105. .type = "ESP32 UART",
  106. };
  107. static const struct esp32_uart_variant esp32s3_variant = {
  108. .clkdiv_mask = ESP32S3_UART_CLKDIV,
  109. .rxfifo_cnt_mask = ESP32S3_UART_RXFIFO_CNT,
  110. .txfifo_cnt_mask = ESP32S3_UART_TXFIFO_CNT,
  111. .txfifo_empty_thrhd_shift = ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT,
  112. .rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
  113. .type = "ESP32S3 UART",
  114. .has_clkconf = true,
  115. };
  116. static const struct of_device_id esp32_uart_dt_ids[] = {
  117. {
  118. .compatible = "esp,esp32-uart",
  119. .data = &esp32_variant,
  120. }, {
  121. .compatible = "esp,esp32s3-uart",
  122. .data = &esp32s3_variant,
  123. }, { /* sentinel */ }
  124. };
  125. MODULE_DEVICE_TABLE(of, esp32_uart_dt_ids);
  126. static struct esp32_port *esp32_uart_ports[UART_NR];
  127. static const struct esp32_uart_variant *port_variant(struct uart_port *port)
  128. {
  129. return port->private_data;
  130. }
  131. static void esp32_uart_write(struct uart_port *port, unsigned long reg, u32 v)
  132. {
  133. writel(v, port->membase + reg);
  134. }
  135. static u32 esp32_uart_read(struct uart_port *port, unsigned long reg)
  136. {
  137. return readl(port->membase + reg);
  138. }
  139. static u32 esp32_uart_tx_fifo_cnt(struct uart_port *port)
  140. {
  141. u32 status = esp32_uart_read(port, UART_STATUS_REG);
  142. return (status & port_variant(port)->txfifo_cnt_mask) >> UART_TXFIFO_CNT_SHIFT;
  143. }
  144. static u32 esp32_uart_rx_fifo_cnt(struct uart_port *port)
  145. {
  146. u32 status = esp32_uart_read(port, UART_STATUS_REG);
  147. return (status & port_variant(port)->rxfifo_cnt_mask) >> UART_RXFIFO_CNT_SHIFT;
  148. }
  149. /* return TIOCSER_TEMT when transmitter is not busy */
  150. static unsigned int esp32_uart_tx_empty(struct uart_port *port)
  151. {
  152. return esp32_uart_tx_fifo_cnt(port) ? 0 : TIOCSER_TEMT;
  153. }
  154. static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  155. {
  156. u32 conf0 = esp32_uart_read(port, UART_CONF0_REG);
  157. conf0 &= ~(UART_LOOPBACK |
  158. UART_SW_RTS | UART_RTS_INV |
  159. UART_SW_DTR | UART_DTR_INV);
  160. if (mctrl & TIOCM_RTS)
  161. conf0 |= UART_SW_RTS;
  162. if (mctrl & TIOCM_DTR)
  163. conf0 |= UART_SW_DTR;
  164. if (mctrl & TIOCM_LOOP)
  165. conf0 |= UART_LOOPBACK;
  166. esp32_uart_write(port, UART_CONF0_REG, conf0);
  167. }
  168. static unsigned int esp32_uart_get_mctrl(struct uart_port *port)
  169. {
  170. u32 status = esp32_uart_read(port, UART_STATUS_REG);
  171. unsigned int ret = TIOCM_CAR;
  172. if (status & UART_DSRN)
  173. ret |= TIOCM_DSR;
  174. if (status & UART_CTSN)
  175. ret |= TIOCM_CTS;
  176. return ret;
  177. }
  178. static void esp32_uart_stop_tx(struct uart_port *port)
  179. {
  180. u32 int_ena;
  181. int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
  182. int_ena &= ~UART_TXFIFO_EMPTY_INT;
  183. esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
  184. }
  185. static void esp32_uart_rxint(struct uart_port *port)
  186. {
  187. struct tty_port *tty_port = &port->state->port;
  188. u32 rx_fifo_cnt = esp32_uart_rx_fifo_cnt(port);
  189. unsigned long flags;
  190. u32 i;
  191. if (!rx_fifo_cnt)
  192. return;
  193. spin_lock_irqsave(&port->lock, flags);
  194. for (i = 0; i < rx_fifo_cnt; ++i) {
  195. u32 rx = esp32_uart_read(port, UART_FIFO_REG);
  196. if (!rx &&
  197. (esp32_uart_read(port, UART_INT_ST_REG) & UART_BRK_DET_INT)) {
  198. esp32_uart_write(port, UART_INT_CLR_REG, UART_BRK_DET_INT);
  199. ++port->icount.brk;
  200. uart_handle_break(port);
  201. } else {
  202. if (uart_handle_sysrq_char(port, (unsigned char)rx))
  203. continue;
  204. tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
  205. ++port->icount.rx;
  206. }
  207. }
  208. spin_unlock_irqrestore(&port->lock, flags);
  209. tty_flip_buffer_push(tty_port);
  210. }
  211. static void esp32_uart_put_char(struct uart_port *port, u8 c)
  212. {
  213. esp32_uart_write(port, UART_FIFO_REG, c);
  214. }
  215. static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
  216. {
  217. unsigned long timeout = jiffies + HZ;
  218. while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
  219. if (time_after(jiffies, timeout)) {
  220. dev_warn(port->dev, "timeout waiting for TX FIFO\n");
  221. return;
  222. }
  223. cpu_relax();
  224. }
  225. esp32_uart_put_char(port, c);
  226. }
  227. static void esp32_uart_transmit_buffer(struct uart_port *port)
  228. {
  229. u32 tx_fifo_used = esp32_uart_tx_fifo_cnt(port);
  230. unsigned int pending;
  231. u8 ch;
  232. if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
  233. return;
  234. pending = uart_port_tx_limited(port, ch,
  235. ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
  236. true, esp32_uart_put_char(port, ch),
  237. ({}));
  238. if (pending) {
  239. u32 int_ena;
  240. int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
  241. int_ena |= UART_TXFIFO_EMPTY_INT;
  242. esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
  243. }
  244. }
  245. static void esp32_uart_txint(struct uart_port *port)
  246. {
  247. esp32_uart_transmit_buffer(port);
  248. }
  249. static irqreturn_t esp32_uart_int(int irq, void *dev_id)
  250. {
  251. struct uart_port *port = dev_id;
  252. u32 status;
  253. status = esp32_uart_read(port, UART_INT_ST_REG);
  254. if (status & (UART_RXFIFO_FULL_INT | UART_BRK_DET_INT))
  255. esp32_uart_rxint(port);
  256. if (status & UART_TXFIFO_EMPTY_INT)
  257. esp32_uart_txint(port);
  258. esp32_uart_write(port, UART_INT_CLR_REG, status);
  259. return IRQ_RETVAL(status);
  260. }
  261. static void esp32_uart_start_tx(struct uart_port *port)
  262. {
  263. esp32_uart_transmit_buffer(port);
  264. }
  265. static void esp32_uart_stop_rx(struct uart_port *port)
  266. {
  267. u32 int_ena;
  268. int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
  269. int_ena &= ~UART_RXFIFO_FULL_INT;
  270. esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
  271. }
  272. static int esp32_uart_startup(struct uart_port *port)
  273. {
  274. int ret = 0;
  275. unsigned long flags;
  276. struct esp32_port *sport = container_of(port, struct esp32_port, port);
  277. ret = clk_prepare_enable(sport->clk);
  278. if (ret)
  279. return ret;
  280. ret = request_irq(port->irq, esp32_uart_int, 0, DRIVER_NAME, port);
  281. if (ret) {
  282. clk_disable_unprepare(sport->clk);
  283. return ret;
  284. }
  285. spin_lock_irqsave(&port->lock, flags);
  286. if (port_variant(port)->has_clkconf)
  287. esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
  288. ESP32S3_UART_CLK_CONF_DEFAULT);
  289. esp32_uart_write(port, UART_CONF1_REG,
  290. (1 << UART_RXFIFO_FULL_THRHD_SHIFT) |
  291. (1 << port_variant(port)->txfifo_empty_thrhd_shift));
  292. esp32_uart_write(port, UART_INT_CLR_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
  293. esp32_uart_write(port, UART_INT_ENA_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
  294. spin_unlock_irqrestore(&port->lock, flags);
  295. return ret;
  296. }
  297. static void esp32_uart_shutdown(struct uart_port *port)
  298. {
  299. struct esp32_port *sport = container_of(port, struct esp32_port, port);
  300. esp32_uart_write(port, UART_INT_ENA_REG, 0);
  301. free_irq(port->irq, port);
  302. clk_disable_unprepare(sport->clk);
  303. }
  304. static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
  305. {
  306. u32 sclk = port->uartclk;
  307. u32 div = sclk / baud;
  308. if (port_variant(port)->has_clkconf) {
  309. u32 sclk_div = div / port_variant(port)->clkdiv_mask;
  310. if (div > port_variant(port)->clkdiv_mask) {
  311. sclk /= (sclk_div + 1);
  312. div = sclk / baud;
  313. }
  314. esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
  315. FIELD_PREP(ESP32S3_UART_SCLK_DIV_NUM, sclk_div) |
  316. ESP32S3_UART_CLK_CONF_DEFAULT);
  317. }
  318. if (div <= port_variant(port)->clkdiv_mask) {
  319. u32 frag = (sclk * 16) / baud - div * 16;
  320. esp32_uart_write(port, UART_CLKDIV_REG,
  321. div | FIELD_PREP(UART_CLKDIV_FRAG, frag));
  322. return true;
  323. }
  324. return false;
  325. }
  326. static void esp32_uart_set_termios(struct uart_port *port,
  327. struct ktermios *termios,
  328. const struct ktermios *old)
  329. {
  330. unsigned long flags;
  331. u32 conf0, conf1;
  332. u32 baud;
  333. const u32 rx_flow_en = port_variant(port)->rx_flow_en;
  334. u32 max_div = port_variant(port)->clkdiv_mask;
  335. termios->c_cflag &= ~CMSPAR;
  336. if (port_variant(port)->has_clkconf)
  337. max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM);
  338. baud = uart_get_baud_rate(port, termios, old,
  339. port->uartclk / max_div,
  340. port->uartclk / 16);
  341. spin_lock_irqsave(&port->lock, flags);
  342. conf0 = esp32_uart_read(port, UART_CONF0_REG);
  343. conf0 &= ~(UART_PARITY_EN | UART_PARITY | UART_BIT_NUM | UART_STOP_BIT_NUM);
  344. conf1 = esp32_uart_read(port, UART_CONF1_REG);
  345. conf1 &= ~rx_flow_en;
  346. if (termios->c_cflag & PARENB) {
  347. conf0 |= UART_PARITY_EN;
  348. if (termios->c_cflag & PARODD)
  349. conf0 |= UART_PARITY;
  350. }
  351. switch (termios->c_cflag & CSIZE) {
  352. case CS5:
  353. conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_5);
  354. break;
  355. case CS6:
  356. conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_6);
  357. break;
  358. case CS7:
  359. conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_7);
  360. break;
  361. case CS8:
  362. conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_8);
  363. break;
  364. }
  365. if (termios->c_cflag & CSTOPB)
  366. conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_2);
  367. else
  368. conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
  369. if (termios->c_cflag & CRTSCTS)
  370. conf1 |= rx_flow_en;
  371. esp32_uart_write(port, UART_CONF0_REG, conf0);
  372. esp32_uart_write(port, UART_CONF1_REG, conf1);
  373. if (baud) {
  374. esp32_uart_set_baud(port, baud);
  375. uart_update_timeout(port, termios->c_cflag, baud);
  376. } else {
  377. if (esp32_uart_set_baud(port, 115200)) {
  378. baud = 115200;
  379. tty_termios_encode_baud_rate(termios, baud, baud);
  380. uart_update_timeout(port, termios->c_cflag, baud);
  381. } else {
  382. dev_warn(port->dev,
  383. "unable to set speed to %d baud or the default 115200\n",
  384. baud);
  385. }
  386. }
  387. spin_unlock_irqrestore(&port->lock, flags);
  388. }
  389. static const char *esp32_uart_type(struct uart_port *port)
  390. {
  391. return port_variant(port)->type;
  392. }
  393. /* configure/auto-configure the port */
  394. static void esp32_uart_config_port(struct uart_port *port, int flags)
  395. {
  396. if (flags & UART_CONFIG_TYPE)
  397. port->type = PORT_GENERIC;
  398. }
  399. #ifdef CONFIG_CONSOLE_POLL
  400. static int esp32_uart_poll_init(struct uart_port *port)
  401. {
  402. struct esp32_port *sport = container_of(port, struct esp32_port, port);
  403. return clk_prepare_enable(sport->clk);
  404. }
  405. static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
  406. {
  407. esp32_uart_put_char_sync(port, c);
  408. }
  409. static int esp32_uart_poll_get_char(struct uart_port *port)
  410. {
  411. if (esp32_uart_rx_fifo_cnt(port))
  412. return esp32_uart_read(port, UART_FIFO_REG);
  413. else
  414. return NO_POLL_CHAR;
  415. }
  416. #endif
  417. static const struct uart_ops esp32_uart_pops = {
  418. .tx_empty = esp32_uart_tx_empty,
  419. .set_mctrl = esp32_uart_set_mctrl,
  420. .get_mctrl = esp32_uart_get_mctrl,
  421. .stop_tx = esp32_uart_stop_tx,
  422. .start_tx = esp32_uart_start_tx,
  423. .stop_rx = esp32_uart_stop_rx,
  424. .startup = esp32_uart_startup,
  425. .shutdown = esp32_uart_shutdown,
  426. .set_termios = esp32_uart_set_termios,
  427. .type = esp32_uart_type,
  428. .config_port = esp32_uart_config_port,
  429. #ifdef CONFIG_CONSOLE_POLL
  430. .poll_init = esp32_uart_poll_init,
  431. .poll_put_char = esp32_uart_poll_put_char,
  432. .poll_get_char = esp32_uart_poll_get_char,
  433. #endif
  434. };
  435. static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
  436. {
  437. esp32_uart_put_char_sync(port, c);
  438. }
  439. static void esp32_uart_string_write(struct uart_port *port, const char *s,
  440. unsigned int count)
  441. {
  442. uart_console_write(port, s, count, esp32_uart_console_putchar);
  443. }
  444. static void
  445. esp32_uart_console_write(struct console *co, const char *s, unsigned int count)
  446. {
  447. struct esp32_port *sport = esp32_uart_ports[co->index];
  448. struct uart_port *port = &sport->port;
  449. unsigned long flags;
  450. bool locked = true;
  451. if (port->sysrq)
  452. locked = false;
  453. else if (oops_in_progress)
  454. locked = spin_trylock_irqsave(&port->lock, flags);
  455. else
  456. spin_lock_irqsave(&port->lock, flags);
  457. esp32_uart_string_write(port, s, count);
  458. if (locked)
  459. spin_unlock_irqrestore(&port->lock, flags);
  460. }
  461. static int __init esp32_uart_console_setup(struct console *co, char *options)
  462. {
  463. struct esp32_port *sport;
  464. int baud = 115200;
  465. int bits = 8;
  466. int parity = 'n';
  467. int flow = 'n';
  468. int ret;
  469. /*
  470. * check whether an invalid uart number has been specified, and
  471. * if so, search for the first available port that does have
  472. * console support.
  473. */
  474. if (co->index == -1 || co->index >= ARRAY_SIZE(esp32_uart_ports))
  475. co->index = 0;
  476. sport = esp32_uart_ports[co->index];
  477. if (!sport)
  478. return -ENODEV;
  479. ret = clk_prepare_enable(sport->clk);
  480. if (ret)
  481. return ret;
  482. if (options)
  483. uart_parse_options(options, &baud, &parity, &bits, &flow);
  484. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  485. }
  486. static int esp32_uart_console_exit(struct console *co)
  487. {
  488. struct esp32_port *sport = esp32_uart_ports[co->index];
  489. clk_disable_unprepare(sport->clk);
  490. return 0;
  491. }
  492. static struct uart_driver esp32_uart_reg;
  493. static struct console esp32_uart_console = {
  494. .name = DEV_NAME,
  495. .write = esp32_uart_console_write,
  496. .device = uart_console_device,
  497. .setup = esp32_uart_console_setup,
  498. .exit = esp32_uart_console_exit,
  499. .flags = CON_PRINTBUFFER,
  500. .index = -1,
  501. .data = &esp32_uart_reg,
  502. };
  503. static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
  504. {
  505. esp32_uart_put_char_sync(port, c);
  506. }
  507. static void esp32_uart_earlycon_write(struct console *con, const char *s,
  508. unsigned int n)
  509. {
  510. struct earlycon_device *dev = con->data;
  511. uart_console_write(&dev->port, s, n, esp32_uart_earlycon_putchar);
  512. }
  513. #ifdef CONFIG_CONSOLE_POLL
  514. static int esp32_uart_earlycon_read(struct console *con, char *s, unsigned int n)
  515. {
  516. struct earlycon_device *dev = con->data;
  517. unsigned int num_read = 0;
  518. while (num_read < n) {
  519. int c = esp32_uart_poll_get_char(&dev->port);
  520. if (c == NO_POLL_CHAR)
  521. break;
  522. s[num_read++] = c;
  523. }
  524. return num_read;
  525. }
  526. #endif
  527. static int __init esp32xx_uart_early_console_setup(struct earlycon_device *device,
  528. const char *options)
  529. {
  530. if (!device->port.membase)
  531. return -ENODEV;
  532. device->con->write = esp32_uart_earlycon_write;
  533. #ifdef CONFIG_CONSOLE_POLL
  534. device->con->read = esp32_uart_earlycon_read;
  535. #endif
  536. if (device->port.uartclk != BASE_BAUD * 16)
  537. esp32_uart_set_baud(&device->port, device->baud);
  538. return 0;
  539. }
  540. static int __init esp32_uart_early_console_setup(struct earlycon_device *device,
  541. const char *options)
  542. {
  543. device->port.private_data = (void *)&esp32_variant;
  544. return esp32xx_uart_early_console_setup(device, options);
  545. }
  546. OF_EARLYCON_DECLARE(esp32uart, "esp,esp32-uart",
  547. esp32_uart_early_console_setup);
  548. static int __init esp32s3_uart_early_console_setup(struct earlycon_device *device,
  549. const char *options)
  550. {
  551. device->port.private_data = (void *)&esp32s3_variant;
  552. return esp32xx_uart_early_console_setup(device, options);
  553. }
  554. OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
  555. esp32s3_uart_early_console_setup);
  556. static struct uart_driver esp32_uart_reg = {
  557. .owner = THIS_MODULE,
  558. .driver_name = DRIVER_NAME,
  559. .dev_name = DEV_NAME,
  560. .nr = ARRAY_SIZE(esp32_uart_ports),
  561. .cons = &esp32_uart_console,
  562. };
  563. static int esp32_uart_probe(struct platform_device *pdev)
  564. {
  565. struct device_node *np = pdev->dev.of_node;
  566. struct uart_port *port;
  567. struct esp32_port *sport;
  568. struct resource *res;
  569. int ret;
  570. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  571. if (!sport)
  572. return -ENOMEM;
  573. port = &sport->port;
  574. ret = of_alias_get_id(np, "serial");
  575. if (ret < 0) {
  576. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  577. return ret;
  578. }
  579. if (ret >= UART_NR) {
  580. dev_err(&pdev->dev, "driver limited to %d serial ports\n", UART_NR);
  581. return -ENOMEM;
  582. }
  583. port->line = ret;
  584. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  585. if (!res)
  586. return -ENODEV;
  587. port->mapbase = res->start;
  588. port->membase = devm_ioremap_resource(&pdev->dev, res);
  589. if (IS_ERR(port->membase))
  590. return PTR_ERR(port->membase);
  591. sport->clk = devm_clk_get(&pdev->dev, NULL);
  592. if (IS_ERR(sport->clk))
  593. return PTR_ERR(sport->clk);
  594. port->uartclk = clk_get_rate(sport->clk);
  595. port->dev = &pdev->dev;
  596. port->type = PORT_GENERIC;
  597. port->iotype = UPIO_MEM;
  598. port->irq = platform_get_irq(pdev, 0);
  599. port->ops = &esp32_uart_pops;
  600. port->flags = UPF_BOOT_AUTOCONF;
  601. port->has_sysrq = 1;
  602. port->fifosize = ESP32_UART_TX_FIFO_SIZE;
  603. port->private_data = (void *)device_get_match_data(&pdev->dev);
  604. esp32_uart_ports[port->line] = sport;
  605. platform_set_drvdata(pdev, port);
  606. return uart_add_one_port(&esp32_uart_reg, port);
  607. }
  608. static void esp32_uart_remove(struct platform_device *pdev)
  609. {
  610. struct uart_port *port = platform_get_drvdata(pdev);
  611. uart_remove_one_port(&esp32_uart_reg, port);
  612. }
  613. static struct platform_driver esp32_uart_driver = {
  614. .probe = esp32_uart_probe,
  615. .remove = esp32_uart_remove,
  616. .driver = {
  617. .name = DRIVER_NAME,
  618. .of_match_table = esp32_uart_dt_ids,
  619. },
  620. };
  621. static int __init esp32_uart_init(void)
  622. {
  623. int ret;
  624. ret = uart_register_driver(&esp32_uart_reg);
  625. if (ret)
  626. return ret;
  627. ret = platform_driver_register(&esp32_uart_driver);
  628. if (ret)
  629. uart_unregister_driver(&esp32_uart_reg);
  630. return ret;
  631. }
  632. static void __exit esp32_uart_exit(void)
  633. {
  634. platform_driver_unregister(&esp32_uart_driver);
  635. uart_unregister_driver(&esp32_uart_reg);
  636. }
  637. module_init(esp32_uart_init);
  638. module_exit(esp32_uart_exit);
  639. MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
  640. MODULE_DESCRIPTION("Espressif ESP32 UART support");
  641. MODULE_LICENSE("GPL");