qcom_geni_serial.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
  3. /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
  4. #define __DISABLE_TRACE_MMIO__
  5. #include <linux/clk.h>
  6. #include <linux/console.h>
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pm_opp.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/pm_wakeirq.h>
  17. #include <linux/soc/qcom/geni-se.h>
  18. #include <linux/serial.h>
  19. #include <linux/serial_core.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <dt-bindings/interconnect/qcom,icc.h>
  24. /* UART specific GENI registers */
  25. #define SE_UART_LOOPBACK_CFG 0x22c
  26. #define SE_UART_IO_MACRO_CTRL 0x240
  27. #define SE_UART_TX_TRANS_CFG 0x25c
  28. #define SE_UART_TX_WORD_LEN 0x268
  29. #define SE_UART_TX_STOP_BIT_LEN 0x26c
  30. #define SE_UART_TX_TRANS_LEN 0x270
  31. #define SE_UART_RX_TRANS_CFG 0x280
  32. #define SE_UART_RX_WORD_LEN 0x28c
  33. #define SE_UART_RX_STALE_CNT 0x294
  34. #define SE_UART_TX_PARITY_CFG 0x2a4
  35. #define SE_UART_RX_PARITY_CFG 0x2a8
  36. #define SE_UART_MANUAL_RFR 0x2ac
  37. /* SE_UART_TRANS_CFG */
  38. #define UART_TX_PAR_EN BIT(0)
  39. #define UART_CTS_MASK BIT(1)
  40. /* SE_UART_TX_WORD_LEN */
  41. #define TX_WORD_LEN_MSK GENMASK(9, 0)
  42. /* SE_UART_TX_STOP_BIT_LEN */
  43. #define TX_STOP_BIT_LEN_MSK GENMASK(23, 0)
  44. #define TX_STOP_BIT_LEN_1 0
  45. #define TX_STOP_BIT_LEN_1_5 1
  46. #define TX_STOP_BIT_LEN_2 2
  47. /* SE_UART_TX_TRANS_LEN */
  48. #define TX_TRANS_LEN_MSK GENMASK(23, 0)
  49. /* SE_UART_RX_TRANS_CFG */
  50. #define UART_RX_INS_STATUS_BIT BIT(2)
  51. #define UART_RX_PAR_EN BIT(3)
  52. /* SE_UART_RX_WORD_LEN */
  53. #define RX_WORD_LEN_MASK GENMASK(9, 0)
  54. /* SE_UART_RX_STALE_CNT */
  55. #define RX_STALE_CNT GENMASK(23, 0)
  56. /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  57. #define PAR_CALC_EN BIT(0)
  58. #define PAR_MODE_MSK GENMASK(2, 1)
  59. #define PAR_MODE_SHFT 1
  60. #define PAR_EVEN 0x00
  61. #define PAR_ODD 0x01
  62. #define PAR_SPACE 0x10
  63. #define PAR_MARK 0x11
  64. /* SE_UART_MANUAL_RFR register fields */
  65. #define UART_MANUAL_RFR_EN BIT(31)
  66. #define UART_RFR_NOT_READY BIT(1)
  67. #define UART_RFR_READY BIT(0)
  68. /* UART M_CMD OP codes */
  69. #define UART_START_TX 0x1
  70. #define UART_START_BREAK 0x4
  71. #define UART_STOP_BREAK 0x5
  72. /* UART S_CMD OP codes */
  73. #define UART_START_READ 0x1
  74. #define UART_PARAM 0x1
  75. #define UART_OVERSAMPLING 32
  76. #define STALE_TIMEOUT 16
  77. #define DEFAULT_BITS_PER_CHAR 10
  78. #define GENI_UART_CONS_PORTS 1
  79. #define GENI_UART_PORTS 3
  80. #define DEF_FIFO_DEPTH_WORDS 16
  81. #define DEF_TX_WM 2
  82. #define DEF_FIFO_WIDTH_BITS 32
  83. #define UART_RX_WM 2
  84. /* SE_UART_LOOPBACK_CFG */
  85. #define RX_TX_SORTED BIT(0)
  86. #define CTS_RTS_SORTED BIT(1)
  87. #define RX_TX_CTS_RTS_SORTED (RX_TX_SORTED | CTS_RTS_SORTED)
  88. /* UART pin swap value */
  89. #define DEFAULT_IO_MACRO_IO0_IO1_MASK GENMASK(3, 0)
  90. #define IO_MACRO_IO0_SEL 0x3
  91. #define DEFAULT_IO_MACRO_IO2_IO3_MASK GENMASK(15, 4)
  92. #define IO_MACRO_IO2_IO3_SWAP 0x4640
  93. /* We always configure 4 bytes per FIFO word */
  94. #define BYTES_PER_FIFO_WORD 4
  95. struct qcom_geni_private_data {
  96. /* NOTE: earlycon port will have NULL here */
  97. struct uart_driver *drv;
  98. u32 poll_cached_bytes;
  99. unsigned int poll_cached_bytes_cnt;
  100. u32 write_cached_bytes;
  101. unsigned int write_cached_bytes_cnt;
  102. };
  103. struct qcom_geni_serial_port {
  104. struct uart_port uport;
  105. struct geni_se se;
  106. const char *name;
  107. u32 tx_fifo_depth;
  108. u32 tx_fifo_width;
  109. u32 rx_fifo_depth;
  110. bool setup;
  111. unsigned long clk_rate;
  112. int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
  113. unsigned int baud;
  114. void *rx_fifo;
  115. u32 loopback;
  116. bool brk;
  117. unsigned int tx_remaining;
  118. int wakeup_irq;
  119. bool rx_tx_swap;
  120. bool cts_rts_swap;
  121. struct qcom_geni_private_data private_data;
  122. };
  123. static const struct uart_ops qcom_geni_console_pops;
  124. static const struct uart_ops qcom_geni_uart_pops;
  125. static struct uart_driver qcom_geni_console_driver;
  126. static struct uart_driver qcom_geni_uart_driver;
  127. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
  128. static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
  129. static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
  130. static void qcom_geni_serial_stop_rx(struct uart_port *uport);
  131. static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
  132. #define to_dev_port(ptr, member) \
  133. container_of(ptr, struct qcom_geni_serial_port, member)
  134. static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
  135. [0] = {
  136. .uport = {
  137. .iotype = UPIO_MEM,
  138. .ops = &qcom_geni_uart_pops,
  139. .flags = UPF_BOOT_AUTOCONF,
  140. .line = 0,
  141. },
  142. },
  143. [1] = {
  144. .uport = {
  145. .iotype = UPIO_MEM,
  146. .ops = &qcom_geni_uart_pops,
  147. .flags = UPF_BOOT_AUTOCONF,
  148. .line = 1,
  149. },
  150. },
  151. [2] = {
  152. .uport = {
  153. .iotype = UPIO_MEM,
  154. .ops = &qcom_geni_uart_pops,
  155. .flags = UPF_BOOT_AUTOCONF,
  156. .line = 2,
  157. },
  158. },
  159. };
  160. static struct qcom_geni_serial_port qcom_geni_console_port = {
  161. .uport = {
  162. .iotype = UPIO_MEM,
  163. .ops = &qcom_geni_console_pops,
  164. .flags = UPF_BOOT_AUTOCONF,
  165. .line = 0,
  166. },
  167. };
  168. static int qcom_geni_serial_request_port(struct uart_port *uport)
  169. {
  170. struct platform_device *pdev = to_platform_device(uport->dev);
  171. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  172. uport->membase = devm_platform_ioremap_resource(pdev, 0);
  173. if (IS_ERR(uport->membase))
  174. return PTR_ERR(uport->membase);
  175. port->se.base = uport->membase;
  176. return 0;
  177. }
  178. static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
  179. {
  180. if (cfg_flags & UART_CONFIG_TYPE) {
  181. uport->type = PORT_MSM;
  182. qcom_geni_serial_request_port(uport);
  183. }
  184. }
  185. static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
  186. {
  187. unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
  188. u32 geni_ios;
  189. if (uart_console(uport)) {
  190. mctrl |= TIOCM_CTS;
  191. } else {
  192. geni_ios = readl(uport->membase + SE_GENI_IOS);
  193. if (!(geni_ios & IO2_DATA_IN))
  194. mctrl |= TIOCM_CTS;
  195. }
  196. return mctrl;
  197. }
  198. static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
  199. unsigned int mctrl)
  200. {
  201. u32 uart_manual_rfr = 0;
  202. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  203. if (uart_console(uport))
  204. return;
  205. if (mctrl & TIOCM_LOOP)
  206. port->loopback = RX_TX_CTS_RTS_SORTED;
  207. if (!(mctrl & TIOCM_RTS) && !uport->suspended)
  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. struct qcom_geni_private_data *private_data = uport->private_data;
  233. if (private_data->drv) {
  234. port = to_dev_port(uport, uport);
  235. baud = port->baud;
  236. if (!baud)
  237. baud = 115200;
  238. fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
  239. /*
  240. * Total polling iterations based on FIFO worth of bytes to be
  241. * sent at current baud. Add a little fluff to the wait.
  242. */
  243. timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
  244. }
  245. /*
  246. * Use custom implementation instead of readl_poll_atomic since ktimer
  247. * is not ready at the time of early console.
  248. */
  249. timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
  250. while (timeout_us) {
  251. reg = readl(uport->membase + offset);
  252. if ((bool)(reg & field) == set)
  253. return true;
  254. udelay(10);
  255. timeout_us -= 10;
  256. }
  257. return false;
  258. }
  259. static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
  260. {
  261. u32 m_cmd;
  262. writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
  263. m_cmd = UART_START_TX << M_OPCODE_SHFT;
  264. writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
  265. }
  266. static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
  267. {
  268. int done;
  269. u32 irq_clear = M_CMD_DONE_EN;
  270. done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  271. M_CMD_DONE_EN, true);
  272. if (!done) {
  273. writel(M_GENI_CMD_ABORT, uport->membase +
  274. SE_GENI_M_CMD_CTRL_REG);
  275. irq_clear |= M_CMD_ABORT_EN;
  276. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  277. M_CMD_ABORT_EN, true);
  278. }
  279. writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
  280. }
  281. static void qcom_geni_serial_abort_rx(struct uart_port *uport)
  282. {
  283. u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
  284. writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
  285. qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  286. S_GENI_CMD_ABORT, false);
  287. writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
  288. writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
  289. }
  290. #ifdef CONFIG_CONSOLE_POLL
  291. static int qcom_geni_serial_get_char(struct uart_port *uport)
  292. {
  293. struct qcom_geni_private_data *private_data = uport->private_data;
  294. u32 status;
  295. u32 word_cnt;
  296. int ret;
  297. if (!private_data->poll_cached_bytes_cnt) {
  298. status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
  299. writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  300. status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  301. writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  302. status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
  303. word_cnt = status & RX_FIFO_WC_MSK;
  304. if (!word_cnt)
  305. return NO_POLL_CHAR;
  306. if (word_cnt == 1 && (status & RX_LAST))
  307. /*
  308. * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
  309. * treated as if it was BYTES_PER_FIFO_WORD.
  310. */
  311. private_data->poll_cached_bytes_cnt =
  312. (status & RX_LAST_BYTE_VALID_MSK) >>
  313. RX_LAST_BYTE_VALID_SHFT;
  314. if (private_data->poll_cached_bytes_cnt == 0)
  315. private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
  316. private_data->poll_cached_bytes =
  317. readl(uport->membase + SE_GENI_RX_FIFOn);
  318. }
  319. private_data->poll_cached_bytes_cnt--;
  320. ret = private_data->poll_cached_bytes & 0xff;
  321. private_data->poll_cached_bytes >>= 8;
  322. return ret;
  323. }
  324. static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
  325. unsigned char c)
  326. {
  327. writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  328. qcom_geni_serial_setup_tx(uport, 1);
  329. WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  330. M_TX_FIFO_WATERMARK_EN, true));
  331. writel(c, uport->membase + SE_GENI_TX_FIFOn);
  332. writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  333. qcom_geni_serial_poll_tx_done(uport);
  334. }
  335. #endif
  336. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  337. static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
  338. {
  339. struct qcom_geni_private_data *private_data = uport->private_data;
  340. private_data->write_cached_bytes =
  341. (private_data->write_cached_bytes >> 8) | (ch << 24);
  342. private_data->write_cached_bytes_cnt++;
  343. if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
  344. writel(private_data->write_cached_bytes,
  345. uport->membase + SE_GENI_TX_FIFOn);
  346. private_data->write_cached_bytes_cnt = 0;
  347. }
  348. }
  349. static void
  350. __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
  351. unsigned int count)
  352. {
  353. struct qcom_geni_private_data *private_data = uport->private_data;
  354. int i;
  355. u32 bytes_to_send = count;
  356. for (i = 0; i < count; i++) {
  357. /*
  358. * uart_console_write() adds a carriage return for each newline.
  359. * Account for additional bytes to be written.
  360. */
  361. if (s[i] == '\n')
  362. bytes_to_send++;
  363. }
  364. writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  365. qcom_geni_serial_setup_tx(uport, bytes_to_send);
  366. for (i = 0; i < count; ) {
  367. size_t chars_to_write = 0;
  368. size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
  369. /*
  370. * If the WM bit never set, then the Tx state machine is not
  371. * in a valid state, so break, cancel/abort any existing
  372. * command. Unfortunately the current data being written is
  373. * lost.
  374. */
  375. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  376. M_TX_FIFO_WATERMARK_EN, true))
  377. break;
  378. chars_to_write = min_t(size_t, count - i, avail / 2);
  379. uart_console_write(uport, s + i, chars_to_write,
  380. qcom_geni_serial_wr_char);
  381. writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
  382. SE_GENI_M_IRQ_CLEAR);
  383. i += chars_to_write;
  384. }
  385. if (private_data->write_cached_bytes_cnt) {
  386. private_data->write_cached_bytes >>= BITS_PER_BYTE *
  387. (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
  388. writel(private_data->write_cached_bytes,
  389. uport->membase + SE_GENI_TX_FIFOn);
  390. private_data->write_cached_bytes_cnt = 0;
  391. }
  392. qcom_geni_serial_poll_tx_done(uport);
  393. }
  394. static void qcom_geni_serial_console_write(struct console *co, const char *s,
  395. unsigned int count)
  396. {
  397. struct uart_port *uport;
  398. struct qcom_geni_serial_port *port;
  399. bool locked = true;
  400. unsigned long flags;
  401. u32 geni_status;
  402. u32 irq_en;
  403. WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
  404. port = get_port_from_line(co->index, true);
  405. if (IS_ERR(port))
  406. return;
  407. uport = &port->uport;
  408. if (oops_in_progress)
  409. locked = spin_trylock_irqsave(&uport->lock, flags);
  410. else
  411. spin_lock_irqsave(&uport->lock, flags);
  412. geni_status = readl(uport->membase + SE_GENI_STATUS);
  413. /* Cancel the current write to log the fault */
  414. if (!locked) {
  415. geni_se_cancel_m_cmd(&port->se);
  416. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  417. M_CMD_CANCEL_EN, true)) {
  418. geni_se_abort_m_cmd(&port->se);
  419. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  420. M_CMD_ABORT_EN, true);
  421. writel(M_CMD_ABORT_EN, uport->membase +
  422. SE_GENI_M_IRQ_CLEAR);
  423. }
  424. writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  425. } else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
  426. /*
  427. * It seems we can't interrupt existing transfers if all data
  428. * has been sent, in which case we need to look for done first.
  429. */
  430. qcom_geni_serial_poll_tx_done(uport);
  431. if (!uart_circ_empty(&uport->state->xmit)) {
  432. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  433. writel(irq_en | M_TX_FIFO_WATERMARK_EN,
  434. uport->membase + SE_GENI_M_IRQ_EN);
  435. }
  436. }
  437. __qcom_geni_serial_console_write(uport, s, count);
  438. if (port->tx_remaining)
  439. qcom_geni_serial_setup_tx(uport, port->tx_remaining);
  440. if (locked)
  441. spin_unlock_irqrestore(&uport->lock, flags);
  442. }
  443. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  444. {
  445. u32 i;
  446. unsigned char buf[sizeof(u32)];
  447. struct tty_port *tport;
  448. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  449. tport = &uport->state->port;
  450. for (i = 0; i < bytes; ) {
  451. int c;
  452. int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
  453. ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
  454. i += chunk;
  455. if (drop)
  456. continue;
  457. for (c = 0; c < chunk; c++) {
  458. int sysrq;
  459. uport->icount.rx++;
  460. if (port->brk && buf[c] == 0) {
  461. port->brk = false;
  462. if (uart_handle_break(uport))
  463. continue;
  464. }
  465. sysrq = uart_prepare_sysrq_char(uport, buf[c]);
  466. if (!sysrq)
  467. tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
  468. }
  469. }
  470. if (!drop)
  471. tty_flip_buffer_push(tport);
  472. return 0;
  473. }
  474. #else
  475. static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  476. {
  477. return -EPERM;
  478. }
  479. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  480. static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
  481. {
  482. struct tty_port *tport;
  483. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  484. u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
  485. u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
  486. int ret;
  487. tport = &uport->state->port;
  488. ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
  489. if (drop)
  490. return 0;
  491. ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
  492. if (ret != bytes) {
  493. dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
  494. __func__, ret, bytes);
  495. WARN_ON_ONCE(1);
  496. }
  497. uport->icount.rx += ret;
  498. tty_flip_buffer_push(tport);
  499. return ret;
  500. }
  501. static void qcom_geni_serial_start_tx(struct uart_port *uport)
  502. {
  503. u32 irq_en;
  504. u32 status;
  505. status = readl(uport->membase + SE_GENI_STATUS);
  506. if (status & M_GENI_CMD_ACTIVE)
  507. return;
  508. if (!qcom_geni_serial_tx_empty(uport))
  509. return;
  510. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  511. irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
  512. writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  513. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  514. }
  515. static void qcom_geni_serial_stop_tx(struct uart_port *uport)
  516. {
  517. u32 irq_en;
  518. u32 status;
  519. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  520. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  521. irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
  522. writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
  523. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  524. status = readl(uport->membase + SE_GENI_STATUS);
  525. /* Possible stop tx is called multiple times. */
  526. if (!(status & M_GENI_CMD_ACTIVE))
  527. return;
  528. geni_se_cancel_m_cmd(&port->se);
  529. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  530. M_CMD_CANCEL_EN, true)) {
  531. geni_se_abort_m_cmd(&port->se);
  532. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  533. M_CMD_ABORT_EN, true);
  534. writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  535. }
  536. writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  537. }
  538. static void qcom_geni_serial_start_rx(struct uart_port *uport)
  539. {
  540. u32 irq_en;
  541. u32 status;
  542. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  543. status = readl(uport->membase + SE_GENI_STATUS);
  544. if (status & S_GENI_CMD_ACTIVE)
  545. qcom_geni_serial_stop_rx(uport);
  546. geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
  547. irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
  548. irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
  549. writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  550. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  551. irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  552. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  553. }
  554. static void qcom_geni_serial_stop_rx(struct uart_port *uport)
  555. {
  556. u32 irq_en;
  557. u32 status;
  558. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  559. u32 s_irq_status;
  560. irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
  561. irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
  562. writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  563. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  564. irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  565. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  566. status = readl(uport->membase + SE_GENI_STATUS);
  567. /* Possible stop rx is called multiple times. */
  568. if (!(status & S_GENI_CMD_ACTIVE))
  569. return;
  570. geni_se_cancel_s_cmd(&port->se);
  571. qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
  572. S_CMD_CANCEL_EN, true);
  573. /*
  574. * If timeout occurs secondary engine remains active
  575. * and Abort sequence is executed.
  576. */
  577. s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  578. /* Flush the Rx buffer */
  579. if (s_irq_status & S_RX_FIFO_LAST_EN)
  580. qcom_geni_serial_handle_rx(uport, true);
  581. writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  582. status = readl(uport->membase + SE_GENI_STATUS);
  583. if (status & S_GENI_CMD_ACTIVE)
  584. qcom_geni_serial_abort_rx(uport);
  585. }
  586. static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
  587. {
  588. u32 status;
  589. u32 word_cnt;
  590. u32 last_word_byte_cnt;
  591. u32 last_word_partial;
  592. u32 total_bytes;
  593. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  594. status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
  595. word_cnt = status & RX_FIFO_WC_MSK;
  596. last_word_partial = status & RX_LAST;
  597. last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
  598. RX_LAST_BYTE_VALID_SHFT;
  599. if (!word_cnt)
  600. return;
  601. total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
  602. if (last_word_partial && last_word_byte_cnt)
  603. total_bytes += last_word_byte_cnt;
  604. else
  605. total_bytes += BYTES_PER_FIFO_WORD;
  606. port->handle_rx(uport, total_bytes, drop);
  607. }
  608. static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
  609. bool active)
  610. {
  611. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  612. struct circ_buf *xmit = &uport->state->xmit;
  613. size_t avail;
  614. size_t remaining;
  615. size_t pending;
  616. int i;
  617. u32 status;
  618. u32 irq_en;
  619. unsigned int chunk;
  620. int tail;
  621. status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
  622. /* Complete the current tx command before taking newly added data */
  623. if (active)
  624. pending = port->tx_remaining;
  625. else
  626. pending = uart_circ_chars_pending(xmit);
  627. /* All data has been transmitted and acknowledged as received */
  628. if (!pending && !status && done) {
  629. qcom_geni_serial_stop_tx(uport);
  630. goto out_write_wakeup;
  631. }
  632. avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
  633. avail *= BYTES_PER_FIFO_WORD;
  634. tail = xmit->tail;
  635. chunk = min(avail, pending);
  636. if (!chunk)
  637. goto out_write_wakeup;
  638. if (!port->tx_remaining) {
  639. qcom_geni_serial_setup_tx(uport, pending);
  640. port->tx_remaining = pending;
  641. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  642. if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
  643. writel(irq_en | M_TX_FIFO_WATERMARK_EN,
  644. uport->membase + SE_GENI_M_IRQ_EN);
  645. }
  646. remaining = chunk;
  647. for (i = 0; i < chunk; ) {
  648. unsigned int tx_bytes;
  649. u8 buf[sizeof(u32)];
  650. int c;
  651. memset(buf, 0, sizeof(buf));
  652. tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
  653. for (c = 0; c < tx_bytes ; c++) {
  654. buf[c] = xmit->buf[tail++];
  655. tail &= UART_XMIT_SIZE - 1;
  656. }
  657. iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
  658. i += tx_bytes;
  659. uport->icount.tx += tx_bytes;
  660. remaining -= tx_bytes;
  661. port->tx_remaining -= tx_bytes;
  662. }
  663. xmit->tail = tail;
  664. /*
  665. * The tx fifo watermark is level triggered and latched. Though we had
  666. * cleared it in qcom_geni_serial_isr it will have already reasserted
  667. * so we must clear it again here after our writes.
  668. */
  669. writel(M_TX_FIFO_WATERMARK_EN,
  670. uport->membase + SE_GENI_M_IRQ_CLEAR);
  671. out_write_wakeup:
  672. if (!port->tx_remaining) {
  673. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  674. if (irq_en & M_TX_FIFO_WATERMARK_EN)
  675. writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
  676. uport->membase + SE_GENI_M_IRQ_EN);
  677. }
  678. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  679. uart_write_wakeup(uport);
  680. }
  681. static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
  682. {
  683. u32 m_irq_en;
  684. u32 m_irq_status;
  685. u32 s_irq_status;
  686. u32 geni_status;
  687. struct uart_port *uport = dev;
  688. bool drop_rx = false;
  689. struct tty_port *tport = &uport->state->port;
  690. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  691. if (uport->suspended)
  692. return IRQ_NONE;
  693. spin_lock(&uport->lock);
  694. m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
  695. s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  696. geni_status = readl(uport->membase + SE_GENI_STATUS);
  697. m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  698. writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  699. writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  700. if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
  701. goto out_unlock;
  702. if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
  703. uport->icount.overrun++;
  704. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  705. }
  706. if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
  707. qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
  708. geni_status & M_GENI_CMD_ACTIVE);
  709. if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
  710. if (s_irq_status & S_GP_IRQ_0_EN)
  711. uport->icount.parity++;
  712. drop_rx = true;
  713. } else if (s_irq_status & S_GP_IRQ_2_EN ||
  714. s_irq_status & S_GP_IRQ_3_EN) {
  715. uport->icount.brk++;
  716. port->brk = true;
  717. }
  718. if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
  719. s_irq_status & S_RX_FIFO_LAST_EN)
  720. qcom_geni_serial_handle_rx(uport, drop_rx);
  721. out_unlock:
  722. uart_unlock_and_check_sysrq(uport);
  723. return IRQ_HANDLED;
  724. }
  725. static int setup_fifos(struct qcom_geni_serial_port *port)
  726. {
  727. struct uart_port *uport;
  728. u32 old_rx_fifo_depth = port->rx_fifo_depth;
  729. uport = &port->uport;
  730. port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
  731. port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
  732. port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
  733. uport->fifosize =
  734. (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
  735. if (port->rx_fifo && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
  736. port->rx_fifo = devm_krealloc(uport->dev, port->rx_fifo,
  737. port->rx_fifo_depth * sizeof(u32),
  738. GFP_KERNEL);
  739. if (!port->rx_fifo)
  740. return -ENOMEM;
  741. }
  742. return 0;
  743. }
  744. static void qcom_geni_serial_shutdown(struct uart_port *uport)
  745. {
  746. disable_irq(uport->irq);
  747. }
  748. static int qcom_geni_serial_port_setup(struct uart_port *uport)
  749. {
  750. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  751. u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
  752. u32 proto;
  753. u32 pin_swap;
  754. int ret;
  755. proto = geni_se_read_proto(&port->se);
  756. if (proto != GENI_SE_UART) {
  757. dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
  758. return -ENXIO;
  759. }
  760. qcom_geni_serial_stop_rx(uport);
  761. ret = setup_fifos(port);
  762. if (ret)
  763. return ret;
  764. writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
  765. pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
  766. if (port->rx_tx_swap) {
  767. pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
  768. pin_swap |= IO_MACRO_IO2_IO3_SWAP;
  769. }
  770. if (port->cts_rts_swap) {
  771. pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
  772. pin_swap |= IO_MACRO_IO0_SEL;
  773. }
  774. /* Configure this register if RX-TX, CTS-RTS pins are swapped */
  775. if (port->rx_tx_swap || port->cts_rts_swap)
  776. writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
  777. /*
  778. * Make an unconditional cancel on the main sequencer to reset
  779. * it else we could end up in data loss scenarios.
  780. */
  781. if (uart_console(uport))
  782. qcom_geni_serial_poll_tx_done(uport);
  783. geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
  784. false, true, true);
  785. geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
  786. geni_se_select_mode(&port->se, GENI_SE_FIFO);
  787. qcom_geni_serial_start_rx(uport);
  788. port->setup = true;
  789. return 0;
  790. }
  791. static int qcom_geni_serial_startup(struct uart_port *uport)
  792. {
  793. int ret;
  794. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  795. if (!port->setup) {
  796. ret = qcom_geni_serial_port_setup(uport);
  797. if (ret)
  798. return ret;
  799. }
  800. enable_irq(uport->irq);
  801. return 0;
  802. }
  803. static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
  804. unsigned int *clk_div, unsigned int percent_tol)
  805. {
  806. unsigned long freq;
  807. unsigned long div, maxdiv;
  808. u64 mult;
  809. unsigned long offset, abs_tol, achieved;
  810. abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
  811. maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
  812. div = 1;
  813. while (div <= maxdiv) {
  814. mult = (u64)div * desired_clk;
  815. if (mult != (unsigned long)mult)
  816. break;
  817. offset = div * abs_tol;
  818. freq = clk_round_rate(clk, mult - offset);
  819. /* Can only get lower if we're done */
  820. if (freq < mult - offset)
  821. break;
  822. /*
  823. * Re-calculate div in case rounding skipped rates but we
  824. * ended up at a good one, then check for a match.
  825. */
  826. div = DIV_ROUND_CLOSEST(freq, desired_clk);
  827. achieved = DIV_ROUND_CLOSEST(freq, div);
  828. if (achieved <= desired_clk + abs_tol &&
  829. achieved >= desired_clk - abs_tol) {
  830. *clk_div = div;
  831. return freq;
  832. }
  833. div = DIV_ROUND_UP(freq, desired_clk);
  834. }
  835. return 0;
  836. }
  837. static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
  838. unsigned int sampling_rate, unsigned int *clk_div)
  839. {
  840. unsigned long ser_clk;
  841. unsigned long desired_clk;
  842. desired_clk = baud * sampling_rate;
  843. if (!desired_clk)
  844. return 0;
  845. /*
  846. * try to find a clock rate within 2% tolerance, then within 5%
  847. */
  848. ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
  849. if (!ser_clk)
  850. ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
  851. return ser_clk;
  852. }
  853. static void qcom_geni_serial_set_termios(struct uart_port *uport,
  854. struct ktermios *termios,
  855. const struct ktermios *old)
  856. {
  857. unsigned int baud;
  858. u32 bits_per_char;
  859. u32 tx_trans_cfg;
  860. u32 tx_parity_cfg;
  861. u32 rx_trans_cfg;
  862. u32 rx_parity_cfg;
  863. u32 stop_bit_len;
  864. unsigned int clk_div;
  865. u32 ser_clk_cfg;
  866. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  867. unsigned long clk_rate;
  868. u32 ver, sampling_rate;
  869. unsigned int avg_bw_core;
  870. qcom_geni_serial_stop_rx(uport);
  871. /* baud rate */
  872. baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
  873. port->baud = baud;
  874. sampling_rate = UART_OVERSAMPLING;
  875. /* Sampling rate is halved for IP versions >= 2.5 */
  876. ver = geni_se_get_qup_hw_version(&port->se);
  877. if (ver >= QUP_SE_VERSION_2_5)
  878. sampling_rate /= 2;
  879. clk_rate = get_clk_div_rate(port->se.clk, baud,
  880. sampling_rate, &clk_div);
  881. if (!clk_rate) {
  882. dev_err(port->se.dev,
  883. "Couldn't find suitable clock rate for %u\n",
  884. baud * sampling_rate);
  885. goto out_restart_rx;
  886. }
  887. dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
  888. baud * sampling_rate, clk_rate, clk_div);
  889. uport->uartclk = clk_rate;
  890. port->clk_rate = clk_rate;
  891. dev_pm_opp_set_rate(uport->dev, clk_rate);
  892. ser_clk_cfg = SER_CLK_EN;
  893. ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
  894. /*
  895. * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
  896. * only.
  897. */
  898. avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
  899. : GENI_DEFAULT_BW;
  900. port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
  901. port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
  902. geni_icc_set_bw(&port->se);
  903. /* parity */
  904. tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
  905. tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
  906. rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
  907. rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
  908. if (termios->c_cflag & PARENB) {
  909. tx_trans_cfg |= UART_TX_PAR_EN;
  910. rx_trans_cfg |= UART_RX_PAR_EN;
  911. tx_parity_cfg |= PAR_CALC_EN;
  912. rx_parity_cfg |= PAR_CALC_EN;
  913. if (termios->c_cflag & PARODD) {
  914. tx_parity_cfg |= PAR_ODD;
  915. rx_parity_cfg |= PAR_ODD;
  916. } else if (termios->c_cflag & CMSPAR) {
  917. tx_parity_cfg |= PAR_SPACE;
  918. rx_parity_cfg |= PAR_SPACE;
  919. } else {
  920. tx_parity_cfg |= PAR_EVEN;
  921. rx_parity_cfg |= PAR_EVEN;
  922. }
  923. } else {
  924. tx_trans_cfg &= ~UART_TX_PAR_EN;
  925. rx_trans_cfg &= ~UART_RX_PAR_EN;
  926. tx_parity_cfg &= ~PAR_CALC_EN;
  927. rx_parity_cfg &= ~PAR_CALC_EN;
  928. }
  929. /* bits per char */
  930. bits_per_char = tty_get_char_size(termios->c_cflag);
  931. /* stop bits */
  932. if (termios->c_cflag & CSTOPB)
  933. stop_bit_len = TX_STOP_BIT_LEN_2;
  934. else
  935. stop_bit_len = TX_STOP_BIT_LEN_1;
  936. /* flow control, clear the CTS_MASK bit if using flow control. */
  937. if (termios->c_cflag & CRTSCTS)
  938. tx_trans_cfg &= ~UART_CTS_MASK;
  939. else
  940. tx_trans_cfg |= UART_CTS_MASK;
  941. if (baud)
  942. uart_update_timeout(uport, termios->c_cflag, baud);
  943. if (!uart_console(uport))
  944. writel(port->loopback,
  945. uport->membase + SE_UART_LOOPBACK_CFG);
  946. writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  947. writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  948. writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  949. writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  950. writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  951. writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  952. writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  953. writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
  954. writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
  955. out_restart_rx:
  956. qcom_geni_serial_start_rx(uport);
  957. }
  958. static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
  959. {
  960. return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
  961. }
  962. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  963. static int qcom_geni_console_setup(struct console *co, char *options)
  964. {
  965. struct uart_port *uport;
  966. struct qcom_geni_serial_port *port;
  967. int baud = 115200;
  968. int bits = 8;
  969. int parity = 'n';
  970. int flow = 'n';
  971. int ret;
  972. if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
  973. return -ENXIO;
  974. port = get_port_from_line(co->index, true);
  975. if (IS_ERR(port)) {
  976. pr_err("Invalid line %d\n", co->index);
  977. return PTR_ERR(port);
  978. }
  979. uport = &port->uport;
  980. if (unlikely(!uport->membase))
  981. return -ENXIO;
  982. if (!port->setup) {
  983. ret = qcom_geni_serial_port_setup(uport);
  984. if (ret)
  985. return ret;
  986. }
  987. if (options)
  988. uart_parse_options(options, &baud, &parity, &bits, &flow);
  989. return uart_set_options(uport, co, baud, parity, bits, flow);
  990. }
  991. static void qcom_geni_serial_earlycon_write(struct console *con,
  992. const char *s, unsigned int n)
  993. {
  994. struct earlycon_device *dev = con->data;
  995. __qcom_geni_serial_console_write(&dev->port, s, n);
  996. }
  997. #ifdef CONFIG_CONSOLE_POLL
  998. static int qcom_geni_serial_earlycon_read(struct console *con,
  999. char *s, unsigned int n)
  1000. {
  1001. struct earlycon_device *dev = con->data;
  1002. struct uart_port *uport = &dev->port;
  1003. int num_read = 0;
  1004. int ch;
  1005. while (num_read < n) {
  1006. ch = qcom_geni_serial_get_char(uport);
  1007. if (ch == NO_POLL_CHAR)
  1008. break;
  1009. s[num_read++] = ch;
  1010. }
  1011. return num_read;
  1012. }
  1013. static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
  1014. struct console *con)
  1015. {
  1016. geni_se_setup_s_cmd(se, UART_START_READ, 0);
  1017. con->read = qcom_geni_serial_earlycon_read;
  1018. }
  1019. #else
  1020. static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
  1021. struct console *con) { }
  1022. #endif
  1023. static struct qcom_geni_private_data earlycon_private_data;
  1024. static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
  1025. const char *opt)
  1026. {
  1027. struct uart_port *uport = &dev->port;
  1028. u32 tx_trans_cfg;
  1029. u32 tx_parity_cfg = 0; /* Disable Tx Parity */
  1030. u32 rx_trans_cfg = 0;
  1031. u32 rx_parity_cfg = 0; /* Disable Rx Parity */
  1032. u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
  1033. u32 bits_per_char;
  1034. struct geni_se se;
  1035. if (!uport->membase)
  1036. return -EINVAL;
  1037. uport->private_data = &earlycon_private_data;
  1038. memset(&se, 0, sizeof(se));
  1039. se.base = uport->membase;
  1040. if (geni_se_read_proto(&se) != GENI_SE_UART)
  1041. return -ENXIO;
  1042. /*
  1043. * Ignore Flow control.
  1044. * n = 8.
  1045. */
  1046. tx_trans_cfg = UART_CTS_MASK;
  1047. bits_per_char = BITS_PER_BYTE;
  1048. /*
  1049. * Make an unconditional cancel on the main sequencer to reset
  1050. * it else we could end up in data loss scenarios.
  1051. */
  1052. qcom_geni_serial_poll_tx_done(uport);
  1053. qcom_geni_serial_abort_rx(uport);
  1054. geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
  1055. false, true, true);
  1056. geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
  1057. geni_se_select_mode(&se, GENI_SE_FIFO);
  1058. writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  1059. writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  1060. writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  1061. writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  1062. writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  1063. writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  1064. writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  1065. dev->con->write = qcom_geni_serial_earlycon_write;
  1066. dev->con->setup = NULL;
  1067. qcom_geni_serial_enable_early_read(&se, dev->con);
  1068. return 0;
  1069. }
  1070. OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
  1071. qcom_geni_serial_earlycon_setup);
  1072. static int __init console_register(struct uart_driver *drv)
  1073. {
  1074. return uart_register_driver(drv);
  1075. }
  1076. static void console_unregister(struct uart_driver *drv)
  1077. {
  1078. uart_unregister_driver(drv);
  1079. }
  1080. static struct console cons_ops = {
  1081. .name = "ttyMSM",
  1082. .write = qcom_geni_serial_console_write,
  1083. .device = uart_console_device,
  1084. .setup = qcom_geni_console_setup,
  1085. .flags = CON_PRINTBUFFER,
  1086. .index = -1,
  1087. .data = &qcom_geni_console_driver,
  1088. };
  1089. static struct uart_driver qcom_geni_console_driver = {
  1090. .owner = THIS_MODULE,
  1091. .driver_name = "qcom_geni_console",
  1092. .dev_name = "ttyMSM",
  1093. .nr = GENI_UART_CONS_PORTS,
  1094. .cons = &cons_ops,
  1095. };
  1096. #else
  1097. static int console_register(struct uart_driver *drv)
  1098. {
  1099. return 0;
  1100. }
  1101. static void console_unregister(struct uart_driver *drv)
  1102. {
  1103. }
  1104. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  1105. static struct uart_driver qcom_geni_uart_driver = {
  1106. .owner = THIS_MODULE,
  1107. .driver_name = "qcom_geni_uart",
  1108. .dev_name = "ttyHS",
  1109. .nr = GENI_UART_PORTS,
  1110. };
  1111. static void qcom_geni_serial_pm(struct uart_port *uport,
  1112. unsigned int new_state, unsigned int old_state)
  1113. {
  1114. struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
  1115. /* If we've never been called, treat it as off */
  1116. if (old_state == UART_PM_STATE_UNDEFINED)
  1117. old_state = UART_PM_STATE_OFF;
  1118. if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
  1119. geni_icc_enable(&port->se);
  1120. if (port->clk_rate)
  1121. dev_pm_opp_set_rate(uport->dev, port->clk_rate);
  1122. geni_se_resources_on(&port->se);
  1123. } else if (new_state == UART_PM_STATE_OFF &&
  1124. old_state == UART_PM_STATE_ON) {
  1125. geni_se_resources_off(&port->se);
  1126. dev_pm_opp_set_rate(uport->dev, 0);
  1127. geni_icc_disable(&port->se);
  1128. }
  1129. }
  1130. static const struct uart_ops qcom_geni_console_pops = {
  1131. .tx_empty = qcom_geni_serial_tx_empty,
  1132. .stop_tx = qcom_geni_serial_stop_tx,
  1133. .start_tx = qcom_geni_serial_start_tx,
  1134. .stop_rx = qcom_geni_serial_stop_rx,
  1135. .start_rx = qcom_geni_serial_start_rx,
  1136. .set_termios = qcom_geni_serial_set_termios,
  1137. .startup = qcom_geni_serial_startup,
  1138. .request_port = qcom_geni_serial_request_port,
  1139. .config_port = qcom_geni_serial_config_port,
  1140. .shutdown = qcom_geni_serial_shutdown,
  1141. .type = qcom_geni_serial_get_type,
  1142. .set_mctrl = qcom_geni_serial_set_mctrl,
  1143. .get_mctrl = qcom_geni_serial_get_mctrl,
  1144. #ifdef CONFIG_CONSOLE_POLL
  1145. .poll_get_char = qcom_geni_serial_get_char,
  1146. .poll_put_char = qcom_geni_serial_poll_put_char,
  1147. #endif
  1148. .pm = qcom_geni_serial_pm,
  1149. };
  1150. static const struct uart_ops qcom_geni_uart_pops = {
  1151. .tx_empty = qcom_geni_serial_tx_empty,
  1152. .stop_tx = qcom_geni_serial_stop_tx,
  1153. .start_tx = qcom_geni_serial_start_tx,
  1154. .stop_rx = qcom_geni_serial_stop_rx,
  1155. .set_termios = qcom_geni_serial_set_termios,
  1156. .startup = qcom_geni_serial_startup,
  1157. .request_port = qcom_geni_serial_request_port,
  1158. .config_port = qcom_geni_serial_config_port,
  1159. .shutdown = qcom_geni_serial_shutdown,
  1160. .type = qcom_geni_serial_get_type,
  1161. .set_mctrl = qcom_geni_serial_set_mctrl,
  1162. .get_mctrl = qcom_geni_serial_get_mctrl,
  1163. .pm = qcom_geni_serial_pm,
  1164. };
  1165. static int qcom_geni_serial_probe(struct platform_device *pdev)
  1166. {
  1167. int ret = 0;
  1168. int line;
  1169. struct qcom_geni_serial_port *port;
  1170. struct uart_port *uport;
  1171. struct resource *res;
  1172. int irq;
  1173. bool console = false;
  1174. struct uart_driver *drv;
  1175. if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
  1176. console = true;
  1177. if (console) {
  1178. drv = &qcom_geni_console_driver;
  1179. line = of_alias_get_id(pdev->dev.of_node, "serial");
  1180. } else {
  1181. drv = &qcom_geni_uart_driver;
  1182. line = of_alias_get_id(pdev->dev.of_node, "serial");
  1183. if (line == -ENODEV) /* compat with non-standard aliases */
  1184. line = of_alias_get_id(pdev->dev.of_node, "hsuart");
  1185. }
  1186. port = get_port_from_line(line, console);
  1187. if (IS_ERR(port)) {
  1188. dev_err(&pdev->dev, "Invalid line %d\n", line);
  1189. return PTR_ERR(port);
  1190. }
  1191. uport = &port->uport;
  1192. /* Don't allow 2 drivers to access the same port */
  1193. if (uport->private_data)
  1194. return -ENODEV;
  1195. uport->dev = &pdev->dev;
  1196. port->se.dev = &pdev->dev;
  1197. port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
  1198. port->se.clk = devm_clk_get(&pdev->dev, "se");
  1199. if (IS_ERR(port->se.clk)) {
  1200. ret = PTR_ERR(port->se.clk);
  1201. dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
  1202. return ret;
  1203. }
  1204. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1205. if (!res)
  1206. return -EINVAL;
  1207. uport->mapbase = res->start;
  1208. port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  1209. port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  1210. port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
  1211. if (!console) {
  1212. port->rx_fifo = devm_kcalloc(uport->dev,
  1213. port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
  1214. if (!port->rx_fifo)
  1215. return -ENOMEM;
  1216. }
  1217. ret = geni_icc_get(&port->se, NULL);
  1218. if (ret)
  1219. return ret;
  1220. port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
  1221. port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
  1222. /* Set BW for register access */
  1223. ret = geni_icc_set_bw(&port->se);
  1224. if (ret)
  1225. return ret;
  1226. port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
  1227. "qcom_geni_serial_%s%d",
  1228. uart_console(uport) ? "console" : "uart", uport->line);
  1229. if (!port->name)
  1230. return -ENOMEM;
  1231. irq = platform_get_irq(pdev, 0);
  1232. if (irq < 0)
  1233. return irq;
  1234. uport->irq = irq;
  1235. uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
  1236. if (!console)
  1237. port->wakeup_irq = platform_get_irq_optional(pdev, 1);
  1238. if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
  1239. port->rx_tx_swap = true;
  1240. if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
  1241. port->cts_rts_swap = true;
  1242. ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
  1243. if (ret)
  1244. return ret;
  1245. /* OPP table is optional */
  1246. ret = devm_pm_opp_of_add_table(&pdev->dev);
  1247. if (ret && ret != -ENODEV) {
  1248. dev_err(&pdev->dev, "invalid OPP table in device tree\n");
  1249. return ret;
  1250. }
  1251. port->private_data.drv = drv;
  1252. uport->private_data = &port->private_data;
  1253. platform_set_drvdata(pdev, port);
  1254. port->handle_rx = console ? handle_rx_console : handle_rx_uart;
  1255. irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
  1256. ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
  1257. IRQF_TRIGGER_HIGH, port->name, uport);
  1258. if (ret) {
  1259. dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
  1260. return ret;
  1261. }
  1262. ret = uart_add_one_port(drv, uport);
  1263. if (ret)
  1264. return ret;
  1265. if (port->wakeup_irq > 0) {
  1266. device_init_wakeup(&pdev->dev, true);
  1267. ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  1268. port->wakeup_irq);
  1269. if (ret) {
  1270. device_init_wakeup(&pdev->dev, false);
  1271. uart_remove_one_port(drv, uport);
  1272. return ret;
  1273. }
  1274. }
  1275. return 0;
  1276. }
  1277. static int qcom_geni_serial_remove(struct platform_device *pdev)
  1278. {
  1279. struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
  1280. struct uart_driver *drv = port->private_data.drv;
  1281. dev_pm_clear_wake_irq(&pdev->dev);
  1282. device_init_wakeup(&pdev->dev, false);
  1283. uart_remove_one_port(drv, &port->uport);
  1284. return 0;
  1285. }
  1286. static int qcom_geni_serial_sys_suspend(struct device *dev)
  1287. {
  1288. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1289. struct uart_port *uport = &port->uport;
  1290. struct qcom_geni_private_data *private_data = uport->private_data;
  1291. /*
  1292. * This is done so we can hit the lowest possible state in suspend
  1293. * even with no_console_suspend
  1294. */
  1295. if (uart_console(uport)) {
  1296. geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
  1297. geni_icc_set_bw(&port->se);
  1298. }
  1299. return uart_suspend_port(private_data->drv, uport);
  1300. }
  1301. static int qcom_geni_serial_sys_resume(struct device *dev)
  1302. {
  1303. int ret;
  1304. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1305. struct uart_port *uport = &port->uport;
  1306. struct qcom_geni_private_data *private_data = uport->private_data;
  1307. ret = uart_resume_port(private_data->drv, uport);
  1308. if (uart_console(uport)) {
  1309. geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
  1310. geni_icc_set_bw(&port->se);
  1311. }
  1312. return ret;
  1313. }
  1314. static int qcom_geni_serial_sys_hib_resume(struct device *dev)
  1315. {
  1316. int ret = 0;
  1317. struct uart_port *uport;
  1318. struct qcom_geni_private_data *private_data;
  1319. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1320. uport = &port->uport;
  1321. private_data = uport->private_data;
  1322. if (uart_console(uport)) {
  1323. geni_icc_set_tag(&port->se, 0x7);
  1324. geni_icc_set_bw(&port->se);
  1325. ret = uart_resume_port(private_data->drv, uport);
  1326. /*
  1327. * For hibernation usecase clients for
  1328. * console UART won't call port setup during restore,
  1329. * hence call port setup for console uart.
  1330. */
  1331. qcom_geni_serial_port_setup(uport);
  1332. } else {
  1333. /*
  1334. * Peripheral register settings are lost during hibernation.
  1335. * Update setup flag such that port setup happens again
  1336. * during next session. Clients of HS-UART will close and
  1337. * open the port during hibernation.
  1338. */
  1339. port->setup = false;
  1340. }
  1341. return ret;
  1342. }
  1343. static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
  1344. .suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
  1345. .resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
  1346. .freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
  1347. .poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
  1348. .restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
  1349. .thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
  1350. };
  1351. static const struct of_device_id qcom_geni_serial_match_table[] = {
  1352. { .compatible = "qcom,geni-debug-uart", },
  1353. { .compatible = "qcom,geni-uart", },
  1354. {}
  1355. };
  1356. MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
  1357. static struct platform_driver qcom_geni_serial_platform_driver = {
  1358. .remove = qcom_geni_serial_remove,
  1359. .probe = qcom_geni_serial_probe,
  1360. .driver = {
  1361. .name = "qcom_geni_serial",
  1362. .of_match_table = qcom_geni_serial_match_table,
  1363. .pm = &qcom_geni_serial_pm_ops,
  1364. },
  1365. };
  1366. static int __init qcom_geni_serial_init(void)
  1367. {
  1368. int ret;
  1369. ret = console_register(&qcom_geni_console_driver);
  1370. if (ret)
  1371. return ret;
  1372. ret = uart_register_driver(&qcom_geni_uart_driver);
  1373. if (ret) {
  1374. console_unregister(&qcom_geni_console_driver);
  1375. return ret;
  1376. }
  1377. ret = platform_driver_register(&qcom_geni_serial_platform_driver);
  1378. if (ret) {
  1379. console_unregister(&qcom_geni_console_driver);
  1380. uart_unregister_driver(&qcom_geni_uart_driver);
  1381. }
  1382. return ret;
  1383. }
  1384. module_init(qcom_geni_serial_init);
  1385. static void __exit qcom_geni_serial_exit(void)
  1386. {
  1387. platform_driver_unregister(&qcom_geni_serial_platform_driver);
  1388. console_unregister(&qcom_geni_console_driver);
  1389. uart_unregister_driver(&qcom_geni_uart_driver);
  1390. }
  1391. module_exit(qcom_geni_serial_exit);
  1392. MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
  1393. MODULE_LICENSE("GPL v2");