qcom_geni_serial.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
  3. #include <linux/clk.h>
  4. #include <linux/console.h>
  5. #include <linux/io.h>
  6. #include <linux/iopoll.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/qcom-geni-se.h>
  12. #include <linux/serial.h>
  13. #include <linux/serial_core.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. /* UART specific GENI registers */
  18. #define SE_UART_TX_TRANS_CFG 0x25c
  19. #define SE_UART_TX_WORD_LEN 0x268
  20. #define SE_UART_TX_STOP_BIT_LEN 0x26c
  21. #define SE_UART_TX_TRANS_LEN 0x270
  22. #define SE_UART_RX_TRANS_CFG 0x280
  23. #define SE_UART_RX_WORD_LEN 0x28c
  24. #define SE_UART_RX_STALE_CNT 0x294
  25. #define SE_UART_TX_PARITY_CFG 0x2a4
  26. #define SE_UART_RX_PARITY_CFG 0x2a8
  27. /* SE_UART_TRANS_CFG */
  28. #define UART_TX_PAR_EN BIT(0)
  29. #define UART_CTS_MASK BIT(1)
  30. /* SE_UART_TX_WORD_LEN */
  31. #define TX_WORD_LEN_MSK GENMASK(9, 0)
  32. /* SE_UART_TX_STOP_BIT_LEN */
  33. #define TX_STOP_BIT_LEN_MSK GENMASK(23, 0)
  34. #define TX_STOP_BIT_LEN_1 0
  35. #define TX_STOP_BIT_LEN_1_5 1
  36. #define TX_STOP_BIT_LEN_2 2
  37. /* SE_UART_TX_TRANS_LEN */
  38. #define TX_TRANS_LEN_MSK GENMASK(23, 0)
  39. /* SE_UART_RX_TRANS_CFG */
  40. #define UART_RX_INS_STATUS_BIT BIT(2)
  41. #define UART_RX_PAR_EN BIT(3)
  42. /* SE_UART_RX_WORD_LEN */
  43. #define RX_WORD_LEN_MASK GENMASK(9, 0)
  44. /* SE_UART_RX_STALE_CNT */
  45. #define RX_STALE_CNT GENMASK(23, 0)
  46. /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  47. #define PAR_CALC_EN BIT(0)
  48. #define PAR_MODE_MSK GENMASK(2, 1)
  49. #define PAR_MODE_SHFT 1
  50. #define PAR_EVEN 0x00
  51. #define PAR_ODD 0x01
  52. #define PAR_SPACE 0x10
  53. #define PAR_MARK 0x11
  54. /* UART M_CMD OP codes */
  55. #define UART_START_TX 0x1
  56. #define UART_START_BREAK 0x4
  57. #define UART_STOP_BREAK 0x5
  58. /* UART S_CMD OP codes */
  59. #define UART_START_READ 0x1
  60. #define UART_PARAM 0x1
  61. #define UART_OVERSAMPLING 32
  62. #define STALE_TIMEOUT 16
  63. #define DEFAULT_BITS_PER_CHAR 10
  64. #define GENI_UART_CONS_PORTS 1
  65. #define DEF_FIFO_DEPTH_WORDS 16
  66. #define DEF_TX_WM 2
  67. #define DEF_FIFO_WIDTH_BITS 32
  68. #define UART_CONSOLE_RX_WM 2
  69. #ifdef CONFIG_CONSOLE_POLL
  70. #define RX_BYTES_PW 1
  71. #else
  72. #define RX_BYTES_PW 4
  73. #endif
  74. struct qcom_geni_serial_port {
  75. struct uart_port uport;
  76. struct geni_se se;
  77. char name[20];
  78. u32 tx_fifo_depth;
  79. u32 tx_fifo_width;
  80. u32 rx_fifo_depth;
  81. u32 tx_wm;
  82. u32 rx_wm;
  83. u32 rx_rfr;
  84. enum geni_se_xfer_mode xfer_mode;
  85. bool setup;
  86. int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
  87. unsigned int baud;
  88. unsigned int tx_bytes_pw;
  89. unsigned int rx_bytes_pw;
  90. bool brk;
  91. };
  92. static const struct uart_ops qcom_geni_console_pops;
  93. static struct uart_driver qcom_geni_console_driver;
  94. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
  95. static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
  96. static void qcom_geni_serial_stop_rx(struct uart_port *uport);
  97. static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
  98. 32000000, 48000000, 64000000, 80000000,
  99. 96000000, 100000000};
  100. #define to_dev_port(ptr, member) \
  101. container_of(ptr, struct qcom_geni_serial_port, member)
  102. static struct qcom_geni_serial_port qcom_geni_console_port = {
  103. .uport = {
  104. .iotype = UPIO_MEM,
  105. .ops = &qcom_geni_console_pops,
  106. .flags = UPF_BOOT_AUTOCONF,
  107. .line = 0,
  108. },
  109. };
  110. static int qcom_geni_serial_request_port(struct uart_port *uport)
  111. {
  112. struct platform_device *pdev = to_platform_device(uport->dev);
  113. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  114. struct resource *res;
  115. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  116. uport->membase = devm_ioremap_resource(&pdev->dev, res);
  117. if (IS_ERR(uport->membase))
  118. return PTR_ERR(uport->membase);
  119. port->se.base = uport->membase;
  120. return 0;
  121. }
  122. static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
  123. {
  124. if (cfg_flags & UART_CONFIG_TYPE) {
  125. uport->type = PORT_MSM;
  126. qcom_geni_serial_request_port(uport);
  127. }
  128. }
  129. static unsigned int qcom_geni_cons_get_mctrl(struct uart_port *uport)
  130. {
  131. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  132. }
  133. static void qcom_geni_cons_set_mctrl(struct uart_port *uport,
  134. unsigned int mctrl)
  135. {
  136. }
  137. static const char *qcom_geni_serial_get_type(struct uart_port *uport)
  138. {
  139. return "MSM";
  140. }
  141. static struct qcom_geni_serial_port *get_port_from_line(int line)
  142. {
  143. if (line < 0 || line >= GENI_UART_CONS_PORTS)
  144. return ERR_PTR(-ENXIO);
  145. return &qcom_geni_console_port;
  146. }
  147. static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
  148. int offset, int field, bool set)
  149. {
  150. u32 reg;
  151. struct qcom_geni_serial_port *port;
  152. unsigned int baud;
  153. unsigned int fifo_bits;
  154. unsigned long timeout_us = 20000;
  155. /* Ensure polling is not re-ordered before the prior writes/reads */
  156. mb();
  157. if (uport->private_data) {
  158. port = to_dev_port(uport, uport);
  159. baud = port->baud;
  160. if (!baud)
  161. baud = 115200;
  162. fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
  163. /*
  164. * Total polling iterations based on FIFO worth of bytes to be
  165. * sent at current baud. Add a little fluff to the wait.
  166. */
  167. timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
  168. }
  169. /*
  170. * Use custom implementation instead of readl_poll_atomic since ktimer
  171. * is not ready at the time of early console.
  172. */
  173. timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
  174. while (timeout_us) {
  175. reg = readl_relaxed(uport->membase + offset);
  176. if ((bool)(reg & field) == set)
  177. return true;
  178. udelay(10);
  179. timeout_us -= 10;
  180. }
  181. return false;
  182. }
  183. static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
  184. {
  185. u32 m_cmd;
  186. writel_relaxed(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
  187. m_cmd = UART_START_TX << M_OPCODE_SHFT;
  188. writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
  189. }
  190. static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
  191. {
  192. int done;
  193. u32 irq_clear = M_CMD_DONE_EN;
  194. done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  195. M_CMD_DONE_EN, true);
  196. if (!done) {
  197. writel_relaxed(M_GENI_CMD_ABORT, uport->membase +
  198. SE_GENI_M_CMD_CTRL_REG);
  199. irq_clear |= M_CMD_ABORT_EN;
  200. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  201. M_CMD_ABORT_EN, true);
  202. }
  203. writel_relaxed(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
  204. }
  205. static void qcom_geni_serial_abort_rx(struct uart_port *uport)
  206. {
  207. u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
  208. writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
  209. qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  210. S_GENI_CMD_ABORT, false);
  211. writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
  212. writel_relaxed(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
  213. }
  214. #ifdef CONFIG_CONSOLE_POLL
  215. static int qcom_geni_serial_get_char(struct uart_port *uport)
  216. {
  217. u32 rx_fifo;
  218. u32 status;
  219. status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
  220. writel_relaxed(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  221. status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
  222. writel_relaxed(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  223. /*
  224. * Ensure the writes to clear interrupts is not re-ordered after
  225. * reading the data.
  226. */
  227. mb();
  228. status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS);
  229. if (!(status & RX_FIFO_WC_MSK))
  230. return NO_POLL_CHAR;
  231. rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn);
  232. return rx_fifo & 0xff;
  233. }
  234. static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
  235. unsigned char c)
  236. {
  237. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  238. writel_relaxed(port->tx_wm, uport->membase + SE_GENI_TX_WATERMARK_REG);
  239. qcom_geni_serial_setup_tx(uport, 1);
  240. WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  241. M_TX_FIFO_WATERMARK_EN, true));
  242. writel_relaxed(c, uport->membase + SE_GENI_TX_FIFOn);
  243. writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
  244. SE_GENI_M_IRQ_CLEAR);
  245. qcom_geni_serial_poll_tx_done(uport);
  246. }
  247. #endif
  248. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  249. static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
  250. {
  251. writel_relaxed(ch, uport->membase + SE_GENI_TX_FIFOn);
  252. }
  253. static void
  254. __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
  255. unsigned int count)
  256. {
  257. int i;
  258. u32 bytes_to_send = count;
  259. for (i = 0; i < count; i++) {
  260. /*
  261. * uart_console_write() adds a carriage return for each newline.
  262. * Account for additional bytes to be written.
  263. */
  264. if (s[i] == '\n')
  265. bytes_to_send++;
  266. }
  267. writel_relaxed(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  268. qcom_geni_serial_setup_tx(uport, bytes_to_send);
  269. for (i = 0; i < count; ) {
  270. size_t chars_to_write = 0;
  271. size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
  272. /*
  273. * If the WM bit never set, then the Tx state machine is not
  274. * in a valid state, so break, cancel/abort any existing
  275. * command. Unfortunately the current data being written is
  276. * lost.
  277. */
  278. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  279. M_TX_FIFO_WATERMARK_EN, true))
  280. break;
  281. chars_to_write = min_t(size_t, count - i, avail / 2);
  282. uart_console_write(uport, s + i, chars_to_write,
  283. qcom_geni_serial_wr_char);
  284. writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
  285. SE_GENI_M_IRQ_CLEAR);
  286. i += chars_to_write;
  287. }
  288. qcom_geni_serial_poll_tx_done(uport);
  289. }
  290. static void qcom_geni_serial_console_write(struct console *co, const char *s,
  291. unsigned int count)
  292. {
  293. struct uart_port *uport;
  294. struct qcom_geni_serial_port *port;
  295. bool locked = true;
  296. unsigned long flags;
  297. WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
  298. port = get_port_from_line(co->index);
  299. if (IS_ERR(port))
  300. return;
  301. uport = &port->uport;
  302. if (oops_in_progress)
  303. locked = spin_trylock_irqsave(&uport->lock, flags);
  304. else
  305. spin_lock_irqsave(&uport->lock, flags);
  306. /* Cancel the current write to log the fault */
  307. if (!locked) {
  308. geni_se_cancel_m_cmd(&port->se);
  309. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  310. M_CMD_CANCEL_EN, true)) {
  311. geni_se_abort_m_cmd(&port->se);
  312. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  313. M_CMD_ABORT_EN, true);
  314. writel_relaxed(M_CMD_ABORT_EN, uport->membase +
  315. SE_GENI_M_IRQ_CLEAR);
  316. }
  317. writel_relaxed(M_CMD_CANCEL_EN, uport->membase +
  318. SE_GENI_M_IRQ_CLEAR);
  319. }
  320. __qcom_geni_serial_console_write(uport, s, count);
  321. if (locked)
  322. spin_unlock_irqrestore(&uport->lock, flags);
  323. }
  324. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  325. {
  326. u32 i;
  327. unsigned char buf[sizeof(u32)];
  328. struct tty_port *tport;
  329. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  330. tport = &uport->state->port;
  331. for (i = 0; i < bytes; ) {
  332. int c;
  333. int chunk = min_t(int, bytes - i, port->rx_bytes_pw);
  334. ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
  335. i += chunk;
  336. if (drop)
  337. continue;
  338. for (c = 0; c < chunk; c++) {
  339. int sysrq;
  340. uport->icount.rx++;
  341. if (port->brk && buf[c] == 0) {
  342. port->brk = false;
  343. if (uart_handle_break(uport))
  344. continue;
  345. }
  346. sysrq = uart_handle_sysrq_char(uport, buf[c]);
  347. if (!sysrq)
  348. tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
  349. }
  350. }
  351. if (!drop)
  352. tty_flip_buffer_push(tport);
  353. return 0;
  354. }
  355. #else
  356. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  357. {
  358. return -EPERM;
  359. }
  360. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  361. static void qcom_geni_serial_start_tx(struct uart_port *uport)
  362. {
  363. u32 irq_en;
  364. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  365. u32 status;
  366. if (port->xfer_mode == GENI_SE_FIFO) {
  367. /*
  368. * readl ensures reading & writing of IRQ_EN register
  369. * is not re-ordered before checking the status of the
  370. * Serial Engine.
  371. */
  372. status = readl(uport->membase + SE_GENI_STATUS);
  373. if (status & M_GENI_CMD_ACTIVE)
  374. return;
  375. if (!qcom_geni_serial_tx_empty(uport))
  376. return;
  377. irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
  378. irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
  379. writel_relaxed(port->tx_wm, uport->membase +
  380. SE_GENI_TX_WATERMARK_REG);
  381. writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  382. }
  383. }
  384. static void qcom_geni_serial_stop_tx(struct uart_port *uport)
  385. {
  386. u32 irq_en;
  387. u32 status;
  388. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  389. irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
  390. irq_en &= ~M_CMD_DONE_EN;
  391. if (port->xfer_mode == GENI_SE_FIFO) {
  392. irq_en &= ~M_TX_FIFO_WATERMARK_EN;
  393. writel_relaxed(0, uport->membase +
  394. SE_GENI_TX_WATERMARK_REG);
  395. }
  396. writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  397. status = readl_relaxed(uport->membase + SE_GENI_STATUS);
  398. /* Possible stop tx is called multiple times. */
  399. if (!(status & M_GENI_CMD_ACTIVE))
  400. return;
  401. /*
  402. * Ensure cancel command write is not re-ordered before checking
  403. * the status of the Primary Sequencer.
  404. */
  405. mb();
  406. geni_se_cancel_m_cmd(&port->se);
  407. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  408. M_CMD_CANCEL_EN, true)) {
  409. geni_se_abort_m_cmd(&port->se);
  410. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  411. M_CMD_ABORT_EN, true);
  412. writel_relaxed(M_CMD_ABORT_EN, uport->membase +
  413. SE_GENI_M_IRQ_CLEAR);
  414. }
  415. writel_relaxed(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  416. }
  417. static void qcom_geni_serial_start_rx(struct uart_port *uport)
  418. {
  419. u32 irq_en;
  420. u32 status;
  421. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  422. status = readl_relaxed(uport->membase + SE_GENI_STATUS);
  423. if (status & S_GENI_CMD_ACTIVE)
  424. qcom_geni_serial_stop_rx(uport);
  425. /*
  426. * Ensure setup command write is not re-ordered before checking
  427. * the status of the Secondary Sequencer.
  428. */
  429. mb();
  430. geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
  431. if (port->xfer_mode == GENI_SE_FIFO) {
  432. irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
  433. irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
  434. writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  435. irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
  436. irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  437. writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  438. }
  439. }
  440. static void qcom_geni_serial_stop_rx(struct uart_port *uport)
  441. {
  442. u32 irq_en;
  443. u32 status;
  444. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  445. u32 irq_clear = S_CMD_DONE_EN;
  446. if (port->xfer_mode == GENI_SE_FIFO) {
  447. irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
  448. irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
  449. writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  450. irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
  451. irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  452. writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  453. }
  454. status = readl_relaxed(uport->membase + SE_GENI_STATUS);
  455. /* Possible stop rx is called multiple times. */
  456. if (!(status & S_GENI_CMD_ACTIVE))
  457. return;
  458. /*
  459. * Ensure cancel command write is not re-ordered before checking
  460. * the status of the Secondary Sequencer.
  461. */
  462. mb();
  463. geni_se_cancel_s_cmd(&port->se);
  464. qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  465. S_GENI_CMD_CANCEL, false);
  466. status = readl_relaxed(uport->membase + SE_GENI_STATUS);
  467. writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
  468. if (status & S_GENI_CMD_ACTIVE)
  469. qcom_geni_serial_abort_rx(uport);
  470. }
  471. static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
  472. {
  473. u32 status;
  474. u32 word_cnt;
  475. u32 last_word_byte_cnt;
  476. u32 last_word_partial;
  477. u32 total_bytes;
  478. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  479. status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS);
  480. word_cnt = status & RX_FIFO_WC_MSK;
  481. last_word_partial = status & RX_LAST;
  482. last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
  483. RX_LAST_BYTE_VALID_SHFT;
  484. if (!word_cnt)
  485. return;
  486. total_bytes = port->rx_bytes_pw * (word_cnt - 1);
  487. if (last_word_partial && last_word_byte_cnt)
  488. total_bytes += last_word_byte_cnt;
  489. else
  490. total_bytes += port->rx_bytes_pw;
  491. port->handle_rx(uport, total_bytes, drop);
  492. }
  493. static void qcom_geni_serial_handle_tx(struct uart_port *uport)
  494. {
  495. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  496. struct circ_buf *xmit = &uport->state->xmit;
  497. size_t avail;
  498. size_t remaining;
  499. int i;
  500. u32 status;
  501. unsigned int chunk;
  502. int tail;
  503. chunk = uart_circ_chars_pending(xmit);
  504. status = readl_relaxed(uport->membase + SE_GENI_TX_FIFO_STATUS);
  505. /* Both FIFO and framework buffer are drained */
  506. if (!chunk && !status) {
  507. qcom_geni_serial_stop_tx(uport);
  508. goto out_write_wakeup;
  509. }
  510. avail = (port->tx_fifo_depth - port->tx_wm) * port->tx_bytes_pw;
  511. tail = xmit->tail;
  512. chunk = min3((size_t)chunk, (size_t)(UART_XMIT_SIZE - tail), avail);
  513. if (!chunk)
  514. goto out_write_wakeup;
  515. qcom_geni_serial_setup_tx(uport, chunk);
  516. remaining = chunk;
  517. for (i = 0; i < chunk; ) {
  518. unsigned int tx_bytes;
  519. u8 buf[sizeof(u32)];
  520. int c;
  521. memset(buf, 0, ARRAY_SIZE(buf));
  522. tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw);
  523. for (c = 0; c < tx_bytes ; c++)
  524. buf[c] = xmit->buf[tail + c];
  525. iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
  526. i += tx_bytes;
  527. tail += tx_bytes;
  528. uport->icount.tx += tx_bytes;
  529. remaining -= tx_bytes;
  530. }
  531. xmit->tail = tail & (UART_XMIT_SIZE - 1);
  532. qcom_geni_serial_poll_tx_done(uport);
  533. out_write_wakeup:
  534. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  535. uart_write_wakeup(uport);
  536. }
  537. static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
  538. {
  539. unsigned int m_irq_status;
  540. unsigned int s_irq_status;
  541. struct uart_port *uport = dev;
  542. unsigned long flags;
  543. unsigned int m_irq_en;
  544. bool drop_rx = false;
  545. struct tty_port *tport = &uport->state->port;
  546. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  547. if (uport->suspended)
  548. return IRQ_NONE;
  549. spin_lock_irqsave(&uport->lock, flags);
  550. m_irq_status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
  551. s_irq_status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
  552. m_irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
  553. writel_relaxed(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  554. writel_relaxed(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  555. if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
  556. goto out_unlock;
  557. if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
  558. uport->icount.overrun++;
  559. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  560. }
  561. if (m_irq_status & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN) &&
  562. m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
  563. qcom_geni_serial_handle_tx(uport);
  564. if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
  565. if (s_irq_status & S_GP_IRQ_0_EN)
  566. uport->icount.parity++;
  567. drop_rx = true;
  568. } else if (s_irq_status & S_GP_IRQ_2_EN ||
  569. s_irq_status & S_GP_IRQ_3_EN) {
  570. uport->icount.brk++;
  571. port->brk = true;
  572. }
  573. if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
  574. s_irq_status & S_RX_FIFO_LAST_EN)
  575. qcom_geni_serial_handle_rx(uport, drop_rx);
  576. out_unlock:
  577. spin_unlock_irqrestore(&uport->lock, flags);
  578. return IRQ_HANDLED;
  579. }
  580. static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
  581. {
  582. struct uart_port *uport;
  583. uport = &port->uport;
  584. port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
  585. port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
  586. port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
  587. uport->fifosize =
  588. (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
  589. }
  590. static void set_rfr_wm(struct qcom_geni_serial_port *port)
  591. {
  592. /*
  593. * Set RFR (Flow off) to FIFO_DEPTH - 2.
  594. * RX WM level at 10% RX_FIFO_DEPTH.
  595. * TX WM level at 10% TX_FIFO_DEPTH.
  596. */
  597. port->rx_rfr = port->rx_fifo_depth - 2;
  598. port->rx_wm = UART_CONSOLE_RX_WM;
  599. port->tx_wm = DEF_TX_WM;
  600. }
  601. static void qcom_geni_serial_shutdown(struct uart_port *uport)
  602. {
  603. unsigned long flags;
  604. /* Stop the console before stopping the current tx */
  605. console_stop(uport->cons);
  606. free_irq(uport->irq, uport);
  607. spin_lock_irqsave(&uport->lock, flags);
  608. qcom_geni_serial_stop_tx(uport);
  609. qcom_geni_serial_stop_rx(uport);
  610. spin_unlock_irqrestore(&uport->lock, flags);
  611. }
  612. static int qcom_geni_serial_port_setup(struct uart_port *uport)
  613. {
  614. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  615. unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
  616. set_rfr_wm(port);
  617. writel_relaxed(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
  618. /*
  619. * Make an unconditional cancel on the main sequencer to reset
  620. * it else we could end up in data loss scenarios.
  621. */
  622. port->xfer_mode = GENI_SE_FIFO;
  623. qcom_geni_serial_poll_tx_done(uport);
  624. geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw,
  625. false, true, false);
  626. geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw,
  627. false, false, true);
  628. geni_se_init(&port->se, port->rx_wm, port->rx_rfr);
  629. geni_se_select_mode(&port->se, port->xfer_mode);
  630. port->setup = true;
  631. return 0;
  632. }
  633. static int qcom_geni_serial_startup(struct uart_port *uport)
  634. {
  635. int ret;
  636. u32 proto;
  637. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  638. scnprintf(port->name, sizeof(port->name),
  639. "qcom_serial_geni%d", uport->line);
  640. proto = geni_se_read_proto(&port->se);
  641. if (proto != GENI_SE_UART) {
  642. dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
  643. return -ENXIO;
  644. }
  645. get_tx_fifo_size(port);
  646. if (!port->setup) {
  647. ret = qcom_geni_serial_port_setup(uport);
  648. if (ret)
  649. return ret;
  650. }
  651. ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH,
  652. port->name, uport);
  653. if (ret)
  654. dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
  655. return ret;
  656. }
  657. static unsigned long get_clk_cfg(unsigned long clk_freq)
  658. {
  659. int i;
  660. for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
  661. if (!(root_freq[i] % clk_freq))
  662. return root_freq[i];
  663. }
  664. return 0;
  665. }
  666. static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div)
  667. {
  668. unsigned long ser_clk;
  669. unsigned long desired_clk;
  670. desired_clk = baud * UART_OVERSAMPLING;
  671. ser_clk = get_clk_cfg(desired_clk);
  672. if (!ser_clk) {
  673. pr_err("%s: Can't find matching DFS entry for baud %d\n",
  674. __func__, baud);
  675. return ser_clk;
  676. }
  677. *clk_div = ser_clk / desired_clk;
  678. return ser_clk;
  679. }
  680. static void qcom_geni_serial_set_termios(struct uart_port *uport,
  681. struct ktermios *termios, struct ktermios *old)
  682. {
  683. unsigned int baud;
  684. unsigned int bits_per_char;
  685. unsigned int tx_trans_cfg;
  686. unsigned int tx_parity_cfg;
  687. unsigned int rx_trans_cfg;
  688. unsigned int rx_parity_cfg;
  689. unsigned int stop_bit_len;
  690. unsigned int clk_div;
  691. unsigned long ser_clk_cfg;
  692. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  693. unsigned long clk_rate;
  694. qcom_geni_serial_stop_rx(uport);
  695. /* baud rate */
  696. baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
  697. port->baud = baud;
  698. clk_rate = get_clk_div_rate(baud, &clk_div);
  699. if (!clk_rate)
  700. goto out_restart_rx;
  701. uport->uartclk = clk_rate;
  702. clk_set_rate(port->se.clk, clk_rate);
  703. ser_clk_cfg = SER_CLK_EN;
  704. ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
  705. /* parity */
  706. tx_trans_cfg = readl_relaxed(uport->membase + SE_UART_TX_TRANS_CFG);
  707. tx_parity_cfg = readl_relaxed(uport->membase + SE_UART_TX_PARITY_CFG);
  708. rx_trans_cfg = readl_relaxed(uport->membase + SE_UART_RX_TRANS_CFG);
  709. rx_parity_cfg = readl_relaxed(uport->membase + SE_UART_RX_PARITY_CFG);
  710. if (termios->c_cflag & PARENB) {
  711. tx_trans_cfg |= UART_TX_PAR_EN;
  712. rx_trans_cfg |= UART_RX_PAR_EN;
  713. tx_parity_cfg |= PAR_CALC_EN;
  714. rx_parity_cfg |= PAR_CALC_EN;
  715. if (termios->c_cflag & PARODD) {
  716. tx_parity_cfg |= PAR_ODD;
  717. rx_parity_cfg |= PAR_ODD;
  718. } else if (termios->c_cflag & CMSPAR) {
  719. tx_parity_cfg |= PAR_SPACE;
  720. rx_parity_cfg |= PAR_SPACE;
  721. } else {
  722. tx_parity_cfg |= PAR_EVEN;
  723. rx_parity_cfg |= PAR_EVEN;
  724. }
  725. } else {
  726. tx_trans_cfg &= ~UART_TX_PAR_EN;
  727. rx_trans_cfg &= ~UART_RX_PAR_EN;
  728. tx_parity_cfg &= ~PAR_CALC_EN;
  729. rx_parity_cfg &= ~PAR_CALC_EN;
  730. }
  731. /* bits per char */
  732. switch (termios->c_cflag & CSIZE) {
  733. case CS5:
  734. bits_per_char = 5;
  735. break;
  736. case CS6:
  737. bits_per_char = 6;
  738. break;
  739. case CS7:
  740. bits_per_char = 7;
  741. break;
  742. case CS8:
  743. default:
  744. bits_per_char = 8;
  745. break;
  746. }
  747. /* stop bits */
  748. if (termios->c_cflag & CSTOPB)
  749. stop_bit_len = TX_STOP_BIT_LEN_2;
  750. else
  751. stop_bit_len = TX_STOP_BIT_LEN_1;
  752. /* flow control, clear the CTS_MASK bit if using flow control. */
  753. if (termios->c_cflag & CRTSCTS)
  754. tx_trans_cfg &= ~UART_CTS_MASK;
  755. else
  756. tx_trans_cfg |= UART_CTS_MASK;
  757. if (baud)
  758. uart_update_timeout(uport, termios->c_cflag, baud);
  759. writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  760. writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  761. writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  762. writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  763. writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  764. writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  765. writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  766. writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
  767. writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
  768. out_restart_rx:
  769. qcom_geni_serial_start_rx(uport);
  770. }
  771. static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
  772. {
  773. return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
  774. }
  775. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  776. static int __init qcom_geni_console_setup(struct console *co, char *options)
  777. {
  778. struct uart_port *uport;
  779. struct qcom_geni_serial_port *port;
  780. int baud;
  781. int bits = 8;
  782. int parity = 'n';
  783. int flow = 'n';
  784. if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
  785. return -ENXIO;
  786. port = get_port_from_line(co->index);
  787. if (IS_ERR(port)) {
  788. pr_err("Invalid line %d\n", co->index);
  789. return PTR_ERR(port);
  790. }
  791. uport = &port->uport;
  792. if (unlikely(!uport->membase))
  793. return -ENXIO;
  794. if (geni_se_resources_on(&port->se)) {
  795. dev_err(port->se.dev, "Error turning on resources\n");
  796. return -ENXIO;
  797. }
  798. if (unlikely(geni_se_read_proto(&port->se) != GENI_SE_UART)) {
  799. geni_se_resources_off(&port->se);
  800. return -ENXIO;
  801. }
  802. if (!port->setup) {
  803. port->tx_bytes_pw = 1;
  804. port->rx_bytes_pw = RX_BYTES_PW;
  805. qcom_geni_serial_stop_rx(uport);
  806. qcom_geni_serial_port_setup(uport);
  807. }
  808. if (options)
  809. uart_parse_options(options, &baud, &parity, &bits, &flow);
  810. return uart_set_options(uport, co, baud, parity, bits, flow);
  811. }
  812. static void qcom_geni_serial_earlycon_write(struct console *con,
  813. const char *s, unsigned int n)
  814. {
  815. struct earlycon_device *dev = con->data;
  816. __qcom_geni_serial_console_write(&dev->port, s, n);
  817. }
  818. static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
  819. const char *opt)
  820. {
  821. struct uart_port *uport = &dev->port;
  822. u32 tx_trans_cfg;
  823. u32 tx_parity_cfg = 0; /* Disable Tx Parity */
  824. u32 rx_trans_cfg = 0;
  825. u32 rx_parity_cfg = 0; /* Disable Rx Parity */
  826. u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
  827. u32 bits_per_char;
  828. struct geni_se se;
  829. if (!uport->membase)
  830. return -EINVAL;
  831. memset(&se, 0, sizeof(se));
  832. se.base = uport->membase;
  833. if (geni_se_read_proto(&se) != GENI_SE_UART)
  834. return -ENXIO;
  835. /*
  836. * Ignore Flow control.
  837. * n = 8.
  838. */
  839. tx_trans_cfg = UART_CTS_MASK;
  840. bits_per_char = BITS_PER_BYTE;
  841. /*
  842. * Make an unconditional cancel on the main sequencer to reset
  843. * it else we could end up in data loss scenarios.
  844. */
  845. qcom_geni_serial_poll_tx_done(uport);
  846. qcom_geni_serial_abort_rx(uport);
  847. geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false);
  848. geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
  849. geni_se_select_mode(&se, GENI_SE_FIFO);
  850. writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  851. writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  852. writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  853. writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  854. writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  855. writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  856. writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  857. dev->con->write = qcom_geni_serial_earlycon_write;
  858. dev->con->setup = NULL;
  859. return 0;
  860. }
  861. OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
  862. qcom_geni_serial_earlycon_setup);
  863. static int __init console_register(struct uart_driver *drv)
  864. {
  865. return uart_register_driver(drv);
  866. }
  867. static void console_unregister(struct uart_driver *drv)
  868. {
  869. uart_unregister_driver(drv);
  870. }
  871. static struct console cons_ops = {
  872. .name = "ttyMSM",
  873. .write = qcom_geni_serial_console_write,
  874. .device = uart_console_device,
  875. .setup = qcom_geni_console_setup,
  876. .flags = CON_PRINTBUFFER,
  877. .index = -1,
  878. .data = &qcom_geni_console_driver,
  879. };
  880. static struct uart_driver qcom_geni_console_driver = {
  881. .owner = THIS_MODULE,
  882. .driver_name = "qcom_geni_console",
  883. .dev_name = "ttyMSM",
  884. .nr = GENI_UART_CONS_PORTS,
  885. .cons = &cons_ops,
  886. };
  887. #else
  888. static int console_register(struct uart_driver *drv)
  889. {
  890. return 0;
  891. }
  892. static void console_unregister(struct uart_driver *drv)
  893. {
  894. }
  895. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  896. static void qcom_geni_serial_cons_pm(struct uart_port *uport,
  897. unsigned int new_state, unsigned int old_state)
  898. {
  899. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  900. if (unlikely(!uart_console(uport)))
  901. return;
  902. if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
  903. geni_se_resources_on(&port->se);
  904. else if (new_state == UART_PM_STATE_OFF &&
  905. old_state == UART_PM_STATE_ON)
  906. geni_se_resources_off(&port->se);
  907. }
  908. static const struct uart_ops qcom_geni_console_pops = {
  909. .tx_empty = qcom_geni_serial_tx_empty,
  910. .stop_tx = qcom_geni_serial_stop_tx,
  911. .start_tx = qcom_geni_serial_start_tx,
  912. .stop_rx = qcom_geni_serial_stop_rx,
  913. .set_termios = qcom_geni_serial_set_termios,
  914. .startup = qcom_geni_serial_startup,
  915. .request_port = qcom_geni_serial_request_port,
  916. .config_port = qcom_geni_serial_config_port,
  917. .shutdown = qcom_geni_serial_shutdown,
  918. .type = qcom_geni_serial_get_type,
  919. .set_mctrl = qcom_geni_cons_set_mctrl,
  920. .get_mctrl = qcom_geni_cons_get_mctrl,
  921. #ifdef CONFIG_CONSOLE_POLL
  922. .poll_get_char = qcom_geni_serial_get_char,
  923. .poll_put_char = qcom_geni_serial_poll_put_char,
  924. #endif
  925. .pm = qcom_geni_serial_cons_pm,
  926. };
  927. static int qcom_geni_serial_probe(struct platform_device *pdev)
  928. {
  929. int ret = 0;
  930. int line = -1;
  931. struct qcom_geni_serial_port *port;
  932. struct uart_port *uport;
  933. struct resource *res;
  934. int irq;
  935. if (pdev->dev.of_node)
  936. line = of_alias_get_id(pdev->dev.of_node, "serial");
  937. if (line < 0 || line >= GENI_UART_CONS_PORTS)
  938. return -ENXIO;
  939. port = get_port_from_line(line);
  940. if (IS_ERR(port)) {
  941. dev_err(&pdev->dev, "Invalid line %d\n", line);
  942. return PTR_ERR(port);
  943. }
  944. uport = &port->uport;
  945. /* Don't allow 2 drivers to access the same port */
  946. if (uport->private_data)
  947. return -ENODEV;
  948. uport->dev = &pdev->dev;
  949. port->se.dev = &pdev->dev;
  950. port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
  951. port->se.clk = devm_clk_get(&pdev->dev, "se");
  952. if (IS_ERR(port->se.clk)) {
  953. ret = PTR_ERR(port->se.clk);
  954. dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
  955. return ret;
  956. }
  957. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  958. if (!res)
  959. return -EINVAL;
  960. uport->mapbase = res->start;
  961. port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  962. port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  963. port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
  964. irq = platform_get_irq(pdev, 0);
  965. if (irq < 0) {
  966. dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq);
  967. return irq;
  968. }
  969. uport->irq = irq;
  970. uport->private_data = &qcom_geni_console_driver;
  971. platform_set_drvdata(pdev, port);
  972. port->handle_rx = handle_rx_console;
  973. return uart_add_one_port(&qcom_geni_console_driver, uport);
  974. }
  975. static int qcom_geni_serial_remove(struct platform_device *pdev)
  976. {
  977. struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
  978. struct uart_driver *drv = port->uport.private_data;
  979. uart_remove_one_port(drv, &port->uport);
  980. return 0;
  981. }
  982. static int __maybe_unused qcom_geni_serial_sys_suspend_noirq(struct device *dev)
  983. {
  984. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  985. struct uart_port *uport = &port->uport;
  986. uart_suspend_port(uport->private_data, uport);
  987. return 0;
  988. }
  989. static int __maybe_unused qcom_geni_serial_sys_resume_noirq(struct device *dev)
  990. {
  991. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  992. struct uart_port *uport = &port->uport;
  993. if (console_suspend_enabled && uport->suspended) {
  994. uart_resume_port(uport->private_data, uport);
  995. /*
  996. * uart_suspend_port() invokes port shutdown which in turn
  997. * frees the irq. uart_resume_port invokes port startup which
  998. * performs request_irq. The request_irq auto-enables the IRQ.
  999. * In addition, resume_noirq implicitly enables the IRQ and
  1000. * leads to an unbalanced IRQ enable warning. Disable the IRQ
  1001. * before returning so that the warning is suppressed.
  1002. */
  1003. disable_irq(uport->irq);
  1004. }
  1005. return 0;
  1006. }
  1007. static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
  1008. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend_noirq,
  1009. qcom_geni_serial_sys_resume_noirq)
  1010. };
  1011. static const struct of_device_id qcom_geni_serial_match_table[] = {
  1012. { .compatible = "qcom,geni-debug-uart", },
  1013. {}
  1014. };
  1015. MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
  1016. static struct platform_driver qcom_geni_serial_platform_driver = {
  1017. .remove = qcom_geni_serial_remove,
  1018. .probe = qcom_geni_serial_probe,
  1019. .driver = {
  1020. .name = "qcom_geni_serial",
  1021. .of_match_table = qcom_geni_serial_match_table,
  1022. .pm = &qcom_geni_serial_pm_ops,
  1023. },
  1024. };
  1025. static int __init qcom_geni_serial_init(void)
  1026. {
  1027. int ret;
  1028. ret = console_register(&qcom_geni_console_driver);
  1029. if (ret)
  1030. return ret;
  1031. ret = platform_driver_register(&qcom_geni_serial_platform_driver);
  1032. if (ret)
  1033. console_unregister(&qcom_geni_console_driver);
  1034. return ret;
  1035. }
  1036. module_init(qcom_geni_serial_init);
  1037. static void __exit qcom_geni_serial_exit(void)
  1038. {
  1039. platform_driver_unregister(&qcom_geni_serial_platform_driver);
  1040. console_unregister(&qcom_geni_console_driver);
  1041. }
  1042. module_exit(qcom_geni_serial_exit);
  1043. MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
  1044. MODULE_LICENSE("GPL v2");