qcom_geni_serial.c 37 KB

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