qcom_geni_serial.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
  4. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  5. */
  6. /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
  7. #define __DISABLE_TRACE_MMIO__
  8. #include <linux/clk.h>
  9. #include <linux/console.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/pm_domain.h>
  16. #include <linux/pm_opp.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/pm_wakeirq.h>
  20. #include <linux/soc/qcom/geni-se.h>
  21. #include <linux/serial.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <dt-bindings/interconnect/qcom,icc.h>
  27. /* UART specific GENI registers */
  28. #define SE_UART_LOOPBACK_CFG 0x22c
  29. #define SE_UART_IO_MACRO_CTRL 0x240
  30. #define SE_UART_TX_TRANS_CFG 0x25c
  31. #define SE_UART_TX_WORD_LEN 0x268
  32. #define SE_UART_TX_STOP_BIT_LEN 0x26c
  33. #define SE_UART_TX_TRANS_LEN 0x270
  34. #define SE_UART_RX_TRANS_CFG 0x280
  35. #define SE_UART_RX_WORD_LEN 0x28c
  36. #define SE_UART_RX_STALE_CNT 0x294
  37. #define SE_UART_TX_PARITY_CFG 0x2a4
  38. #define SE_UART_RX_PARITY_CFG 0x2a8
  39. #define SE_UART_MANUAL_RFR 0x2ac
  40. /* SE_UART_TRANS_CFG */
  41. #define UART_TX_PAR_EN BIT(0)
  42. #define UART_CTS_MASK BIT(1)
  43. /* SE_UART_TX_STOP_BIT_LEN */
  44. #define TX_STOP_BIT_LEN_1 0
  45. #define TX_STOP_BIT_LEN_2 2
  46. /* SE_UART_RX_TRANS_CFG */
  47. #define UART_RX_PAR_EN BIT(3)
  48. /* SE_UART_RX_WORD_LEN */
  49. #define RX_WORD_LEN_MASK GENMASK(9, 0)
  50. /* SE_UART_RX_STALE_CNT */
  51. #define RX_STALE_CNT GENMASK(23, 0)
  52. /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  53. #define PAR_CALC_EN BIT(0)
  54. #define PAR_EVEN 0x00
  55. #define PAR_ODD 0x01
  56. #define PAR_SPACE 0x10
  57. /* SE_UART_MANUAL_RFR register fields */
  58. #define UART_MANUAL_RFR_EN BIT(31)
  59. #define UART_RFR_NOT_READY BIT(1)
  60. #define UART_RFR_READY BIT(0)
  61. /* UART M_CMD OP codes */
  62. #define UART_START_TX 0x1
  63. /* UART S_CMD OP codes */
  64. #define UART_START_READ 0x1
  65. #define UART_PARAM 0x1
  66. #define UART_PARAM_RFR_OPEN BIT(7)
  67. #define UART_OVERSAMPLING 32
  68. #define STALE_TIMEOUT 16
  69. #define DEFAULT_BITS_PER_CHAR 10
  70. #define GENI_UART_CONS_PORTS 1
  71. #define DEF_FIFO_DEPTH_WORDS 16
  72. #define DEF_TX_WM 2
  73. #define DEF_FIFO_WIDTH_BITS 32
  74. #define UART_RX_WM 2
  75. /* SE_UART_LOOPBACK_CFG */
  76. #define RX_TX_SORTED BIT(0)
  77. #define CTS_RTS_SORTED BIT(1)
  78. #define RX_TX_CTS_RTS_SORTED (RX_TX_SORTED | CTS_RTS_SORTED)
  79. /* UART pin swap value */
  80. #define DEFAULT_IO_MACRO_IO0_IO1_MASK GENMASK(3, 0)
  81. #define IO_MACRO_IO0_SEL 0x3
  82. #define DEFAULT_IO_MACRO_IO2_IO3_MASK GENMASK(15, 4)
  83. #define IO_MACRO_IO2_IO3_SWAP 0x4640
  84. /* We always configure 4 bytes per FIFO word */
  85. #define BYTES_PER_FIFO_WORD 4U
  86. #define DMA_RX_BUF_SIZE 2048
  87. static DEFINE_IDA(port_ida);
  88. #define DOMAIN_IDX_POWER 0
  89. #define DOMAIN_IDX_PERF 1
  90. struct qcom_geni_device_data {
  91. bool console;
  92. enum geni_se_xfer_mode mode;
  93. struct dev_pm_domain_attach_data pd_data;
  94. int (*resources_init)(struct uart_port *uport);
  95. int (*set_rate)(struct uart_port *uport, unsigned int baud);
  96. int (*power_state)(struct uart_port *uport, bool state);
  97. };
  98. struct qcom_geni_private_data {
  99. /* NOTE: earlycon port will have NULL here */
  100. struct uart_driver *drv;
  101. u32 poll_cached_bytes;
  102. unsigned int poll_cached_bytes_cnt;
  103. u32 write_cached_bytes;
  104. unsigned int write_cached_bytes_cnt;
  105. };
  106. struct qcom_geni_serial_port {
  107. struct uart_port uport;
  108. struct geni_se se;
  109. const char *name;
  110. u32 tx_fifo_depth;
  111. u32 tx_fifo_width;
  112. u32 rx_fifo_depth;
  113. dma_addr_t tx_dma_addr;
  114. dma_addr_t rx_dma_addr;
  115. bool setup;
  116. unsigned long poll_timeout_us;
  117. unsigned long clk_rate;
  118. void *rx_buf;
  119. u32 loopback;
  120. bool brk;
  121. unsigned int tx_remaining;
  122. unsigned int tx_queued;
  123. int wakeup_irq;
  124. bool rx_tx_swap;
  125. bool cts_rts_swap;
  126. struct qcom_geni_private_data private_data;
  127. const struct qcom_geni_device_data *dev_data;
  128. struct dev_pm_domain_list *pd_list;
  129. };
  130. static const struct uart_ops qcom_geni_console_pops;
  131. static const struct uart_ops qcom_geni_uart_pops;
  132. static struct uart_driver qcom_geni_console_driver;
  133. static struct uart_driver qcom_geni_uart_driver;
  134. static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
  135. static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
  136. static int qcom_geni_serial_port_setup(struct uart_port *uport);
  137. static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
  138. {
  139. return container_of(uport, struct qcom_geni_serial_port, uport);
  140. }
  141. static struct qcom_geni_serial_port qcom_geni_console_port = {
  142. .uport = {
  143. .iotype = UPIO_MEM,
  144. .ops = &qcom_geni_console_pops,
  145. .flags = UPF_BOOT_AUTOCONF,
  146. .line = 0,
  147. },
  148. };
  149. static const struct serial_rs485 qcom_geni_rs485_supported = {
  150. .flags = SER_RS485_ENABLED | SER_RS485_RTS_AFTER_SEND | SER_RS485_RTS_ON_SEND,
  151. };
  152. /**
  153. * qcom_geni_set_rs485_mode - Set RTS pin state for RS485 mode
  154. * @uport: UART port
  155. * @flag: RS485 flag to determine RTS polarity
  156. *
  157. * Enables manual RTS control for RS485. Sets RTS to READY or NOT_READY
  158. * based on the specified flag if RS485 mode is enabled.
  159. */
  160. static void qcom_geni_set_rs485_mode(struct uart_port *uport, u32 flag)
  161. {
  162. if (!(uport->rs485.flags & SER_RS485_ENABLED))
  163. return;
  164. u32 rfr = UART_MANUAL_RFR_EN;
  165. if (uport->rs485.flags & flag)
  166. rfr |= UART_RFR_NOT_READY;
  167. else
  168. rfr |= UART_RFR_READY;
  169. writel(rfr, uport->membase + SE_UART_MANUAL_RFR);
  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);
  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. struct qcom_geni_serial_port *port = to_dev_port(uport);
  206. if (uart_console(uport))
  207. return;
  208. if (mctrl & TIOCM_LOOP)
  209. port->loopback = RX_TX_CTS_RTS_SORTED;
  210. if (!(mctrl & TIOCM_RTS) && !uport->suspended)
  211. uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
  212. writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
  213. }
  214. static const char *qcom_geni_serial_get_type(struct uart_port *uport)
  215. {
  216. return "MSM";
  217. }
  218. static struct qcom_geni_serial_port *get_port_from_line(int line, bool console, struct device *dev)
  219. {
  220. struct qcom_geni_serial_port *port;
  221. int nr_ports = console ? GENI_UART_CONS_PORTS : CONFIG_SERIAL_QCOM_GENI_UART_PORTS;
  222. if (console) {
  223. if (line < 0 || line >= nr_ports)
  224. return ERR_PTR(-ENXIO);
  225. port = &qcom_geni_console_port;
  226. } else {
  227. int max_alias_num = of_alias_get_highest_id("serial");
  228. if (line < 0 || line >= nr_ports)
  229. line = ida_alloc_range(&port_ida, max_alias_num + 1,
  230. nr_ports - 1, GFP_KERNEL);
  231. else
  232. line = ida_alloc_range(&port_ida, line,
  233. nr_ports - 1, GFP_KERNEL);
  234. if (line < 0)
  235. return ERR_PTR(-ENXIO);
  236. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  237. if (!port)
  238. return ERR_PTR(-ENOMEM);
  239. port->uport.iotype = UPIO_MEM;
  240. port->uport.ops = &qcom_geni_uart_pops;
  241. port->uport.flags = UPF_BOOT_AUTOCONF;
  242. port->uport.line = line;
  243. }
  244. return port;
  245. }
  246. static bool qcom_geni_serial_main_active(struct uart_port *uport)
  247. {
  248. return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
  249. }
  250. static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
  251. {
  252. return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
  253. }
  254. static bool qcom_geni_serial_poll_bitfield(struct uart_port *uport,
  255. unsigned int offset, u32 field, u32 val)
  256. {
  257. u32 reg;
  258. struct qcom_geni_serial_port *port;
  259. unsigned long timeout_us = 20000;
  260. struct qcom_geni_private_data *private_data = uport->private_data;
  261. if (private_data->drv) {
  262. port = to_dev_port(uport);
  263. if (port->poll_timeout_us)
  264. timeout_us = port->poll_timeout_us;
  265. }
  266. /*
  267. * Use custom implementation instead of readl_poll_atomic since ktimer
  268. * is not ready at the time of early console.
  269. */
  270. timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
  271. while (timeout_us) {
  272. reg = readl(uport->membase + offset);
  273. if ((reg & field) == val)
  274. return true;
  275. udelay(10);
  276. timeout_us -= 10;
  277. }
  278. return false;
  279. }
  280. static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
  281. unsigned int offset, u32 field, bool set)
  282. {
  283. return qcom_geni_serial_poll_bitfield(uport, offset, field, set ? field : 0);
  284. }
  285. static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
  286. {
  287. u32 m_cmd;
  288. writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
  289. m_cmd = UART_START_TX << M_OPCODE_SHFT;
  290. writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
  291. }
  292. static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
  293. {
  294. int done;
  295. done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  296. M_CMD_DONE_EN, true);
  297. if (!done) {
  298. writel(M_GENI_CMD_ABORT, uport->membase +
  299. SE_GENI_M_CMD_CTRL_REG);
  300. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  301. M_CMD_ABORT_EN, true);
  302. writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  303. }
  304. }
  305. static void qcom_geni_serial_abort_rx(struct uart_port *uport)
  306. {
  307. u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
  308. writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
  309. qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  310. S_GENI_CMD_ABORT, false);
  311. writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
  312. writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
  313. }
  314. #ifdef CONFIG_CONSOLE_POLL
  315. static int qcom_geni_serial_get_char(struct uart_port *uport)
  316. {
  317. struct qcom_geni_private_data *private_data = uport->private_data;
  318. u32 status;
  319. u32 word_cnt;
  320. int ret;
  321. if (!private_data->poll_cached_bytes_cnt) {
  322. status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
  323. writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  324. status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  325. writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  326. status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
  327. word_cnt = status & RX_FIFO_WC_MSK;
  328. if (!word_cnt)
  329. return NO_POLL_CHAR;
  330. if (word_cnt == 1 && (status & RX_LAST))
  331. /*
  332. * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
  333. * treated as if it was BYTES_PER_FIFO_WORD.
  334. */
  335. private_data->poll_cached_bytes_cnt =
  336. (status & RX_LAST_BYTE_VALID_MSK) >>
  337. RX_LAST_BYTE_VALID_SHFT;
  338. if (private_data->poll_cached_bytes_cnt == 0)
  339. private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
  340. private_data->poll_cached_bytes =
  341. readl(uport->membase + SE_GENI_RX_FIFOn);
  342. }
  343. private_data->poll_cached_bytes_cnt--;
  344. ret = private_data->poll_cached_bytes & 0xff;
  345. private_data->poll_cached_bytes >>= 8;
  346. return ret;
  347. }
  348. static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
  349. unsigned char c)
  350. {
  351. if (qcom_geni_serial_main_active(uport)) {
  352. qcom_geni_serial_poll_tx_done(uport);
  353. __qcom_geni_serial_cancel_tx_cmd(uport);
  354. }
  355. writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  356. qcom_geni_serial_setup_tx(uport, 1);
  357. writel(c, uport->membase + SE_GENI_TX_FIFOn);
  358. qcom_geni_serial_poll_tx_done(uport);
  359. }
  360. static int qcom_geni_serial_poll_init(struct uart_port *uport)
  361. {
  362. struct qcom_geni_serial_port *port = to_dev_port(uport);
  363. int ret;
  364. if (!port->setup) {
  365. ret = qcom_geni_serial_port_setup(uport);
  366. if (ret)
  367. return ret;
  368. }
  369. if (!qcom_geni_serial_secondary_active(uport))
  370. geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
  371. return 0;
  372. }
  373. #endif
  374. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  375. static void qcom_geni_serial_drain_fifo(struct uart_port *uport)
  376. {
  377. struct qcom_geni_serial_port *port = to_dev_port(uport);
  378. qcom_geni_serial_poll_bitfield(uport, SE_GENI_M_GP_LENGTH, GP_LENGTH,
  379. port->tx_queued);
  380. }
  381. static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
  382. {
  383. struct qcom_geni_private_data *private_data = uport->private_data;
  384. private_data->write_cached_bytes =
  385. (private_data->write_cached_bytes >> 8) | (ch << 24);
  386. private_data->write_cached_bytes_cnt++;
  387. if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
  388. writel(private_data->write_cached_bytes,
  389. uport->membase + SE_GENI_TX_FIFOn);
  390. private_data->write_cached_bytes_cnt = 0;
  391. }
  392. }
  393. static void
  394. __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
  395. unsigned int count)
  396. {
  397. struct qcom_geni_private_data *private_data = uport->private_data;
  398. int i;
  399. u32 bytes_to_send = count;
  400. for (i = 0; i < count; i++) {
  401. /*
  402. * uart_console_write() adds a carriage return for each newline.
  403. * Account for additional bytes to be written.
  404. */
  405. if (s[i] == '\n')
  406. bytes_to_send++;
  407. }
  408. writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  409. writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  410. qcom_geni_serial_setup_tx(uport, bytes_to_send);
  411. for (i = 0; i < count; ) {
  412. size_t chars_to_write = 0;
  413. size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
  414. /*
  415. * If the WM bit never set, then the Tx state machine is not
  416. * in a valid state, so break, cancel/abort any existing
  417. * command. Unfortunately the current data being written is
  418. * lost.
  419. */
  420. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  421. M_TX_FIFO_WATERMARK_EN, true))
  422. break;
  423. chars_to_write = min_t(size_t, count - i, avail / 2);
  424. uart_console_write(uport, s + i, chars_to_write,
  425. qcom_geni_serial_wr_char);
  426. writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
  427. SE_GENI_M_IRQ_CLEAR);
  428. i += chars_to_write;
  429. }
  430. if (private_data->write_cached_bytes_cnt) {
  431. private_data->write_cached_bytes >>= BITS_PER_BYTE *
  432. (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
  433. writel(private_data->write_cached_bytes,
  434. uport->membase + SE_GENI_TX_FIFOn);
  435. private_data->write_cached_bytes_cnt = 0;
  436. }
  437. qcom_geni_serial_poll_tx_done(uport);
  438. }
  439. static void qcom_geni_serial_console_write(struct console *co, const char *s,
  440. unsigned int count)
  441. {
  442. struct uart_port *uport;
  443. struct qcom_geni_serial_port *port;
  444. u32 m_irq_en, s_irq_en;
  445. bool locked = true;
  446. unsigned long flags;
  447. WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
  448. port = get_port_from_line(co->index, true, NULL);
  449. if (IS_ERR(port))
  450. return;
  451. uport = &port->uport;
  452. if (oops_in_progress)
  453. locked = uart_port_trylock_irqsave(uport, &flags);
  454. else
  455. uart_port_lock_irqsave(uport, &flags);
  456. m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  457. s_irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
  458. writel(0, uport->membase + SE_GENI_M_IRQ_EN);
  459. writel(0, uport->membase + SE_GENI_S_IRQ_EN);
  460. if (qcom_geni_serial_main_active(uport)) {
  461. /* Wait for completion or drain FIFO */
  462. if (!locked || port->tx_remaining == 0)
  463. qcom_geni_serial_poll_tx_done(uport);
  464. else
  465. qcom_geni_serial_drain_fifo(uport);
  466. qcom_geni_serial_cancel_tx_cmd(uport);
  467. }
  468. __qcom_geni_serial_console_write(uport, s, count);
  469. writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  470. writel(s_irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  471. if (locked)
  472. uart_port_unlock_irqrestore(uport, flags);
  473. }
  474. static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  475. {
  476. u32 i;
  477. unsigned char buf[sizeof(u32)];
  478. struct tty_port *tport;
  479. struct qcom_geni_serial_port *port = to_dev_port(uport);
  480. tport = &uport->state->port;
  481. for (i = 0; i < bytes; ) {
  482. int c;
  483. int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
  484. ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
  485. i += chunk;
  486. if (drop)
  487. continue;
  488. for (c = 0; c < chunk; c++) {
  489. int sysrq;
  490. uport->icount.rx++;
  491. if (port->brk && buf[c] == 0) {
  492. port->brk = false;
  493. if (uart_handle_break(uport))
  494. continue;
  495. }
  496. sysrq = uart_prepare_sysrq_char(uport, buf[c]);
  497. if (!sysrq)
  498. tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
  499. }
  500. }
  501. if (!drop)
  502. tty_flip_buffer_push(tport);
  503. }
  504. #else
  505. static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
  506. {
  507. }
  508. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  509. static void handle_rx_uart(struct uart_port *uport, u32 bytes)
  510. {
  511. struct qcom_geni_serial_port *port = to_dev_port(uport);
  512. struct tty_port *tport = &uport->state->port;
  513. int ret;
  514. ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
  515. if (ret != bytes) {
  516. dev_err_ratelimited(uport->dev, "failed to push data (%d < %u)\n",
  517. ret, bytes);
  518. }
  519. uport->icount.rx += ret;
  520. tty_flip_buffer_push(tport);
  521. }
  522. static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
  523. {
  524. return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
  525. }
  526. static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
  527. {
  528. struct qcom_geni_serial_port *port = to_dev_port(uport);
  529. bool done;
  530. if (!qcom_geni_serial_main_active(uport))
  531. return;
  532. if (port->tx_dma_addr) {
  533. geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
  534. port->tx_remaining);
  535. port->tx_dma_addr = 0;
  536. port->tx_remaining = 0;
  537. }
  538. geni_se_cancel_m_cmd(&port->se);
  539. done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  540. M_CMD_CANCEL_EN, true);
  541. if (!done) {
  542. geni_se_abort_m_cmd(&port->se);
  543. done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  544. M_CMD_ABORT_EN, true);
  545. if (!done)
  546. dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
  547. writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  548. }
  549. writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  550. }
  551. static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
  552. {
  553. struct qcom_geni_serial_port *port = to_dev_port(uport);
  554. struct tty_port *tport = &uport->state->port;
  555. unsigned int xmit_size;
  556. u8 *tail;
  557. int ret;
  558. if (port->tx_dma_addr)
  559. return;
  560. if (kfifo_is_empty(&tport->xmit_fifo))
  561. return;
  562. xmit_size = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
  563. UART_XMIT_SIZE);
  564. qcom_geni_set_rs485_mode(uport, SER_RS485_RTS_ON_SEND);
  565. qcom_geni_serial_setup_tx(uport, xmit_size);
  566. ret = geni_se_tx_dma_prep(&port->se, tail, xmit_size,
  567. &port->tx_dma_addr);
  568. if (ret) {
  569. dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
  570. qcom_geni_serial_stop_tx_dma(uport);
  571. return;
  572. }
  573. port->tx_remaining = xmit_size;
  574. }
  575. static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
  576. {
  577. unsigned char c;
  578. u32 irq_en;
  579. /*
  580. * Start a new transfer in case the previous command was cancelled and
  581. * left data in the FIFO which may prevent the watermark interrupt
  582. * from triggering. Note that the stale data is discarded.
  583. */
  584. if (!qcom_geni_serial_main_active(uport) &&
  585. !qcom_geni_serial_tx_empty(uport)) {
  586. if (uart_fifo_out(uport, &c, 1) == 1) {
  587. writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  588. qcom_geni_serial_setup_tx(uport, 1);
  589. writel(c, uport->membase + SE_GENI_TX_FIFOn);
  590. }
  591. }
  592. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  593. irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
  594. writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
  595. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  596. }
  597. static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
  598. {
  599. u32 irq_en;
  600. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  601. irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
  602. writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
  603. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  604. }
  605. static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
  606. {
  607. struct qcom_geni_serial_port *port = to_dev_port(uport);
  608. geni_se_cancel_m_cmd(&port->se);
  609. if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  610. M_CMD_CANCEL_EN, true)) {
  611. geni_se_abort_m_cmd(&port->se);
  612. qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  613. M_CMD_ABORT_EN, true);
  614. writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  615. }
  616. writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
  617. }
  618. static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
  619. {
  620. struct qcom_geni_serial_port *port = to_dev_port(uport);
  621. if (!qcom_geni_serial_main_active(uport))
  622. return;
  623. __qcom_geni_serial_cancel_tx_cmd(uport);
  624. port->tx_remaining = 0;
  625. port->tx_queued = 0;
  626. }
  627. static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
  628. {
  629. u32 status;
  630. u32 word_cnt;
  631. u32 last_word_byte_cnt;
  632. u32 last_word_partial;
  633. u32 total_bytes;
  634. status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
  635. word_cnt = status & RX_FIFO_WC_MSK;
  636. last_word_partial = status & RX_LAST;
  637. last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
  638. RX_LAST_BYTE_VALID_SHFT;
  639. if (!word_cnt)
  640. return;
  641. total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
  642. if (last_word_partial && last_word_byte_cnt)
  643. total_bytes += last_word_byte_cnt;
  644. else
  645. total_bytes += BYTES_PER_FIFO_WORD;
  646. handle_rx_console(uport, total_bytes, drop);
  647. }
  648. static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
  649. {
  650. u32 irq_en;
  651. struct qcom_geni_serial_port *port = to_dev_port(uport);
  652. u32 s_irq_status;
  653. irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
  654. irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
  655. writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  656. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  657. irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  658. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  659. if (!qcom_geni_serial_secondary_active(uport))
  660. return;
  661. geni_se_cancel_s_cmd(&port->se);
  662. qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
  663. S_CMD_CANCEL_EN, true);
  664. /*
  665. * If timeout occurs secondary engine remains active
  666. * and Abort sequence is executed.
  667. */
  668. s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  669. /* Flush the Rx buffer */
  670. if (s_irq_status & S_RX_FIFO_LAST_EN)
  671. qcom_geni_serial_handle_rx_fifo(uport, true);
  672. writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  673. if (qcom_geni_serial_secondary_active(uport))
  674. qcom_geni_serial_abort_rx(uport);
  675. }
  676. static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
  677. {
  678. u32 irq_en;
  679. struct qcom_geni_serial_port *port = to_dev_port(uport);
  680. if (qcom_geni_serial_secondary_active(uport))
  681. qcom_geni_serial_stop_rx_fifo(uport);
  682. geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
  683. irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
  684. irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
  685. writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
  686. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  687. irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  688. writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
  689. }
  690. static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
  691. {
  692. struct qcom_geni_serial_port *port = to_dev_port(uport);
  693. bool done;
  694. if (!qcom_geni_serial_secondary_active(uport))
  695. return;
  696. geni_se_cancel_s_cmd(&port->se);
  697. done = qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
  698. RX_EOT, true);
  699. if (done) {
  700. writel(RX_EOT | RX_DMA_DONE,
  701. uport->membase + SE_DMA_RX_IRQ_CLR);
  702. } else {
  703. qcom_geni_serial_abort_rx(uport);
  704. writel(1, uport->membase + SE_DMA_RX_FSM_RST);
  705. qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
  706. RX_RESET_DONE, true);
  707. writel(RX_RESET_DONE | RX_DMA_DONE,
  708. uport->membase + SE_DMA_RX_IRQ_CLR);
  709. }
  710. if (port->rx_dma_addr) {
  711. geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
  712. DMA_RX_BUF_SIZE);
  713. port->rx_dma_addr = 0;
  714. }
  715. }
  716. static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
  717. {
  718. struct qcom_geni_serial_port *port = to_dev_port(uport);
  719. int ret;
  720. if (qcom_geni_serial_secondary_active(uport))
  721. qcom_geni_serial_stop_rx_dma(uport);
  722. geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
  723. ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
  724. DMA_RX_BUF_SIZE,
  725. &port->rx_dma_addr);
  726. if (ret) {
  727. dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
  728. qcom_geni_serial_stop_rx_dma(uport);
  729. }
  730. }
  731. static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
  732. {
  733. struct qcom_geni_serial_port *port = to_dev_port(uport);
  734. u32 rx_in;
  735. int ret;
  736. if (!qcom_geni_serial_secondary_active(uport))
  737. return;
  738. if (!port->rx_dma_addr)
  739. return;
  740. geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
  741. port->rx_dma_addr = 0;
  742. rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
  743. if (!rx_in) {
  744. dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
  745. return;
  746. }
  747. if (!drop)
  748. handle_rx_uart(uport, rx_in);
  749. ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
  750. DMA_RX_BUF_SIZE,
  751. &port->rx_dma_addr);
  752. if (ret) {
  753. dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
  754. qcom_geni_serial_stop_rx_dma(uport);
  755. }
  756. }
  757. static void qcom_geni_serial_start_rx(struct uart_port *uport)
  758. {
  759. uport->ops->start_rx(uport);
  760. }
  761. static void qcom_geni_serial_stop_rx(struct uart_port *uport)
  762. {
  763. uport->ops->stop_rx(uport);
  764. }
  765. static void qcom_geni_serial_stop_tx(struct uart_port *uport)
  766. {
  767. uport->ops->stop_tx(uport);
  768. }
  769. static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
  770. unsigned int chunk)
  771. {
  772. struct qcom_geni_serial_port *port = to_dev_port(uport);
  773. unsigned int tx_bytes, remaining = chunk;
  774. u8 buf[BYTES_PER_FIFO_WORD];
  775. while (remaining) {
  776. memset(buf, 0, sizeof(buf));
  777. tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
  778. uart_fifo_out(uport, buf, tx_bytes);
  779. iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
  780. remaining -= tx_bytes;
  781. port->tx_remaining -= tx_bytes;
  782. }
  783. }
  784. static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
  785. bool done, bool active)
  786. {
  787. struct qcom_geni_serial_port *port = to_dev_port(uport);
  788. struct tty_port *tport = &uport->state->port;
  789. size_t avail;
  790. size_t pending;
  791. u32 status;
  792. u32 irq_en;
  793. unsigned int chunk;
  794. status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
  795. /* Complete the current tx command before taking newly added data */
  796. if (active)
  797. pending = port->tx_remaining;
  798. else
  799. pending = kfifo_len(&tport->xmit_fifo);
  800. /* All data has been transmitted or command has been cancelled */
  801. if (!pending && done) {
  802. qcom_geni_serial_stop_tx_fifo(uport);
  803. goto out_write_wakeup;
  804. }
  805. if (active)
  806. avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
  807. else
  808. avail = port->tx_fifo_depth;
  809. avail *= BYTES_PER_FIFO_WORD;
  810. chunk = min(avail, pending);
  811. if (!chunk)
  812. goto out_write_wakeup;
  813. if (!active) {
  814. qcom_geni_serial_setup_tx(uport, pending);
  815. port->tx_remaining = pending;
  816. port->tx_queued = 0;
  817. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  818. if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
  819. writel(irq_en | M_TX_FIFO_WATERMARK_EN,
  820. uport->membase + SE_GENI_M_IRQ_EN);
  821. }
  822. qcom_geni_serial_send_chunk_fifo(uport, chunk);
  823. port->tx_queued += chunk;
  824. /*
  825. * The tx fifo watermark is level triggered and latched. Though we had
  826. * cleared it in qcom_geni_serial_isr it will have already reasserted
  827. * so we must clear it again here after our writes.
  828. */
  829. writel(M_TX_FIFO_WATERMARK_EN,
  830. uport->membase + SE_GENI_M_IRQ_CLEAR);
  831. out_write_wakeup:
  832. if (!port->tx_remaining) {
  833. irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  834. if (irq_en & M_TX_FIFO_WATERMARK_EN)
  835. writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
  836. uport->membase + SE_GENI_M_IRQ_EN);
  837. }
  838. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  839. uart_write_wakeup(uport);
  840. }
  841. static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
  842. {
  843. struct qcom_geni_serial_port *port = to_dev_port(uport);
  844. struct tty_port *tport = &uport->state->port;
  845. uart_xmit_advance(uport, port->tx_remaining);
  846. geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
  847. port->tx_dma_addr = 0;
  848. port->tx_remaining = 0;
  849. if (!kfifo_is_empty(&tport->xmit_fifo))
  850. qcom_geni_serial_start_tx_dma(uport);
  851. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  852. uart_write_wakeup(uport);
  853. }
  854. static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
  855. {
  856. u32 m_irq_en;
  857. u32 m_irq_status;
  858. u32 s_irq_status;
  859. u32 geni_status;
  860. u32 dma;
  861. u32 dma_tx_status;
  862. u32 dma_rx_status;
  863. struct uart_port *uport = dev;
  864. bool drop_rx = false;
  865. struct tty_port *tport = &uport->state->port;
  866. struct qcom_geni_serial_port *port = to_dev_port(uport);
  867. if (uport->suspended)
  868. return IRQ_NONE;
  869. uart_port_lock(uport);
  870. m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
  871. s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
  872. dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
  873. dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
  874. geni_status = readl(uport->membase + SE_GENI_STATUS);
  875. dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
  876. m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
  877. writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
  878. writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
  879. writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
  880. writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
  881. if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
  882. goto out_unlock;
  883. if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
  884. uport->icount.overrun++;
  885. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  886. }
  887. if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
  888. if (s_irq_status & S_GP_IRQ_0_EN)
  889. uport->icount.parity++;
  890. drop_rx = true;
  891. } else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
  892. uport->icount.brk++;
  893. port->brk = true;
  894. }
  895. if (dma) {
  896. if (dma_tx_status & TX_DMA_DONE) {
  897. qcom_geni_serial_handle_tx_dma(uport);
  898. qcom_geni_set_rs485_mode(uport, SER_RS485_RTS_AFTER_SEND);
  899. }
  900. if (dma_rx_status) {
  901. if (dma_rx_status & RX_RESET_DONE)
  902. goto out_unlock;
  903. if (dma_rx_status & RX_DMA_PARITY_ERR) {
  904. uport->icount.parity++;
  905. drop_rx = true;
  906. }
  907. if (dma_rx_status & RX_DMA_BREAK)
  908. uport->icount.brk++;
  909. if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
  910. qcom_geni_serial_handle_rx_dma(uport, drop_rx);
  911. }
  912. } else {
  913. if (m_irq_status & m_irq_en &
  914. (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
  915. qcom_geni_serial_handle_tx_fifo(uport,
  916. m_irq_status & M_CMD_DONE_EN,
  917. geni_status & M_GENI_CMD_ACTIVE);
  918. if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
  919. qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
  920. }
  921. out_unlock:
  922. uart_unlock_and_check_sysrq(uport);
  923. return IRQ_HANDLED;
  924. }
  925. static int setup_fifos(struct qcom_geni_serial_port *port)
  926. {
  927. struct uart_port *uport;
  928. u32 old_rx_fifo_depth = port->rx_fifo_depth;
  929. uport = &port->uport;
  930. port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
  931. port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
  932. port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
  933. uport->fifosize =
  934. (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
  935. if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
  936. /*
  937. * Use krealloc rather than krealloc_array because rx_buf is
  938. * accessed as 1 byte entries as well as 4 byte entries so it's
  939. * not necessarily an array.
  940. */
  941. port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
  942. port->rx_fifo_depth * sizeof(u32),
  943. GFP_KERNEL);
  944. if (!port->rx_buf)
  945. return -ENOMEM;
  946. }
  947. return 0;
  948. }
  949. static void qcom_geni_serial_shutdown(struct uart_port *uport)
  950. {
  951. disable_irq(uport->irq);
  952. uart_port_lock_irq(uport);
  953. qcom_geni_serial_stop_tx(uport);
  954. qcom_geni_serial_stop_rx(uport);
  955. qcom_geni_serial_cancel_tx_cmd(uport);
  956. uart_port_unlock_irq(uport);
  957. }
  958. static void qcom_geni_serial_flush_buffer(struct uart_port *uport)
  959. {
  960. qcom_geni_serial_cancel_tx_cmd(uport);
  961. }
  962. static int qcom_geni_serial_port_setup(struct uart_port *uport)
  963. {
  964. struct qcom_geni_serial_port *port = to_dev_port(uport);
  965. u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
  966. u32 proto;
  967. u32 pin_swap;
  968. int ret;
  969. proto = geni_se_read_proto(&port->se);
  970. if (proto == GENI_SE_INVALID_PROTO) {
  971. ret = geni_load_se_firmware(&port->se, GENI_SE_UART);
  972. if (ret) {
  973. dev_err(uport->dev, "UART firmware load failed ret: %d\n", ret);
  974. return ret;
  975. }
  976. } else if (proto != GENI_SE_UART) {
  977. dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
  978. return -ENXIO;
  979. }
  980. qcom_geni_serial_stop_rx(uport);
  981. ret = setup_fifos(port);
  982. if (ret)
  983. return ret;
  984. writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
  985. pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
  986. if (port->rx_tx_swap) {
  987. pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
  988. pin_swap |= IO_MACRO_IO2_IO3_SWAP;
  989. }
  990. if (port->cts_rts_swap) {
  991. pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
  992. pin_swap |= IO_MACRO_IO0_SEL;
  993. }
  994. /* Configure this register if RX-TX, CTS-RTS pins are swapped */
  995. if (port->rx_tx_swap || port->cts_rts_swap)
  996. writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
  997. /*
  998. * Make an unconditional cancel on the main sequencer to reset
  999. * it else we could end up in data loss scenarios.
  1000. */
  1001. if (uart_console(uport))
  1002. qcom_geni_serial_poll_tx_done(uport);
  1003. geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
  1004. false, true, true);
  1005. geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
  1006. geni_se_select_mode(&port->se, port->dev_data->mode);
  1007. port->setup = true;
  1008. return 0;
  1009. }
  1010. static int qcom_geni_serial_startup(struct uart_port *uport)
  1011. {
  1012. int ret;
  1013. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1014. if (!port->setup) {
  1015. ret = qcom_geni_serial_port_setup(uport);
  1016. if (ret)
  1017. return ret;
  1018. }
  1019. uart_port_lock_irq(uport);
  1020. qcom_geni_serial_start_rx(uport);
  1021. uart_port_unlock_irq(uport);
  1022. enable_irq(uport->irq);
  1023. return 0;
  1024. }
  1025. static int geni_serial_set_rate(struct uart_port *uport, unsigned int baud)
  1026. {
  1027. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1028. unsigned long clk_rate;
  1029. unsigned int avg_bw_core, clk_idx;
  1030. unsigned int clk_div;
  1031. u32 ver, sampling_rate;
  1032. u32 ser_clk_cfg;
  1033. int ret;
  1034. sampling_rate = UART_OVERSAMPLING;
  1035. /* Sampling rate is halved for IP versions >= 2.5 */
  1036. ver = geni_se_get_qup_hw_version(&port->se);
  1037. if (ver >= QUP_SE_VERSION_2_5)
  1038. sampling_rate /= 2;
  1039. ret = geni_se_clk_freq_match(&port->se, baud * sampling_rate, &clk_idx, &clk_rate, false);
  1040. if (ret) {
  1041. dev_err(port->se.dev, "Failed to find src clk for baud rate: %d ret: %d\n",
  1042. baud, ret);
  1043. return ret;
  1044. }
  1045. clk_div = DIV_ROUND_UP(clk_rate, baud * sampling_rate);
  1046. /* Check if calculated divider exceeds maximum allowed value */
  1047. if (clk_div > (CLK_DIV_MSK >> CLK_DIV_SHFT)) {
  1048. dev_err(port->se.dev, "Calculated clock divider %u exceeds maximum\n", clk_div);
  1049. return -EINVAL;
  1050. }
  1051. dev_dbg(port->se.dev, "desired_rate = %u, clk_rate = %lu, clk_div = %u\n, clk_idx = %u\n",
  1052. baud * sampling_rate, clk_rate, clk_div, clk_idx);
  1053. uport->uartclk = clk_rate;
  1054. port->clk_rate = clk_rate;
  1055. dev_pm_opp_set_rate(uport->dev, clk_rate);
  1056. ser_clk_cfg = SER_CLK_EN;
  1057. ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
  1058. /*
  1059. * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
  1060. * only.
  1061. */
  1062. avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
  1063. : GENI_DEFAULT_BW;
  1064. port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
  1065. port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
  1066. geni_icc_set_bw(&port->se);
  1067. writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
  1068. writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
  1069. /* Configure clock selection register with the selected clock index */
  1070. writel(clk_idx & CLK_SEL_MSK, uport->membase + SE_GENI_CLK_SEL);
  1071. return 0;
  1072. }
  1073. static int geni_serial_set_level(struct uart_port *uport, unsigned int baud)
  1074. {
  1075. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1076. struct device *perf_dev = port->pd_list->pd_devs[DOMAIN_IDX_PERF];
  1077. /*
  1078. * The performance protocol sets UART communication
  1079. * speeds by selecting different performance levels
  1080. * through the OPP framework.
  1081. *
  1082. * Supported perf levels for baudrates in firmware are below
  1083. * +---------------------+--------------------+
  1084. * | Perf level value | Baudrate values |
  1085. * +---------------------+--------------------+
  1086. * | 300 | 300 |
  1087. * | 1200 | 1200 |
  1088. * | 2400 | 2400 |
  1089. * | 4800 | 4800 |
  1090. * | 9600 | 9600 |
  1091. * | 19200 | 19200 |
  1092. * | 38400 | 38400 |
  1093. * | 57600 | 57600 |
  1094. * | 115200 | 115200 |
  1095. * | 230400 | 230400 |
  1096. * | 460800 | 460800 |
  1097. * | 921600 | 921600 |
  1098. * | 2000000 | 2000000 |
  1099. * | 3000000 | 3000000 |
  1100. * | 3200000 | 3200000 |
  1101. * | 4000000 | 4000000 |
  1102. * +---------------------+--------------------+
  1103. */
  1104. return dev_pm_opp_set_level(perf_dev, baud);
  1105. }
  1106. static void qcom_geni_serial_set_termios(struct uart_port *uport,
  1107. struct ktermios *termios,
  1108. const struct ktermios *old)
  1109. {
  1110. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1111. unsigned int baud;
  1112. unsigned long timeout;
  1113. u32 bits_per_char;
  1114. u32 tx_trans_cfg;
  1115. u32 tx_parity_cfg;
  1116. u32 rx_trans_cfg;
  1117. u32 rx_parity_cfg;
  1118. u32 stop_bit_len;
  1119. int ret = 0;
  1120. /* baud rate */
  1121. baud = uart_get_baud_rate(uport, termios, old, 300, 8000000);
  1122. ret = port->dev_data->set_rate(uport, baud);
  1123. if (ret)
  1124. return;
  1125. /* parity */
  1126. tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
  1127. tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
  1128. rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
  1129. rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
  1130. if (termios->c_cflag & PARENB) {
  1131. tx_trans_cfg |= UART_TX_PAR_EN;
  1132. rx_trans_cfg |= UART_RX_PAR_EN;
  1133. tx_parity_cfg |= PAR_CALC_EN;
  1134. rx_parity_cfg |= PAR_CALC_EN;
  1135. if (termios->c_cflag & PARODD) {
  1136. tx_parity_cfg |= PAR_ODD;
  1137. rx_parity_cfg |= PAR_ODD;
  1138. } else if (termios->c_cflag & CMSPAR) {
  1139. tx_parity_cfg |= PAR_SPACE;
  1140. rx_parity_cfg |= PAR_SPACE;
  1141. } else {
  1142. tx_parity_cfg |= PAR_EVEN;
  1143. rx_parity_cfg |= PAR_EVEN;
  1144. }
  1145. } else {
  1146. tx_trans_cfg &= ~UART_TX_PAR_EN;
  1147. rx_trans_cfg &= ~UART_RX_PAR_EN;
  1148. tx_parity_cfg &= ~PAR_CALC_EN;
  1149. rx_parity_cfg &= ~PAR_CALC_EN;
  1150. }
  1151. /* bits per char */
  1152. bits_per_char = tty_get_char_size(termios->c_cflag);
  1153. /* stop bits */
  1154. if (termios->c_cflag & CSTOPB)
  1155. stop_bit_len = TX_STOP_BIT_LEN_2;
  1156. else
  1157. stop_bit_len = TX_STOP_BIT_LEN_1;
  1158. /* flow control, clear the CTS_MASK bit if using flow control. */
  1159. if (termios->c_cflag & CRTSCTS)
  1160. tx_trans_cfg &= ~UART_CTS_MASK;
  1161. else
  1162. tx_trans_cfg |= UART_CTS_MASK;
  1163. if (baud) {
  1164. uart_update_timeout(uport, termios->c_cflag, baud);
  1165. /*
  1166. * Make sure that qcom_geni_serial_poll_bitfield() waits for
  1167. * the FIFO, two-word intermediate transfer register and shift
  1168. * register to clear.
  1169. *
  1170. * Note that uart_fifo_timeout() also adds a 20 ms margin.
  1171. */
  1172. timeout = jiffies_to_usecs(uart_fifo_timeout(uport));
  1173. timeout += 3 * timeout / port->tx_fifo_depth;
  1174. WRITE_ONCE(port->poll_timeout_us, timeout);
  1175. }
  1176. if (!uart_console(uport))
  1177. writel(port->loopback,
  1178. uport->membase + SE_UART_LOOPBACK_CFG);
  1179. writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  1180. writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  1181. writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  1182. writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  1183. writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  1184. writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  1185. writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  1186. }
  1187. #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
  1188. static int qcom_geni_console_setup(struct console *co, char *options)
  1189. {
  1190. struct uart_port *uport;
  1191. struct qcom_geni_serial_port *port;
  1192. int baud = 115200;
  1193. int bits = 8;
  1194. int parity = 'n';
  1195. int flow = 'n';
  1196. int ret;
  1197. if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
  1198. return -ENXIO;
  1199. port = get_port_from_line(co->index, true, NULL);
  1200. if (IS_ERR(port)) {
  1201. pr_err("Invalid line %d\n", co->index);
  1202. return PTR_ERR(port);
  1203. }
  1204. uport = &port->uport;
  1205. if (unlikely(!uport->membase))
  1206. return -ENXIO;
  1207. if (!port->setup) {
  1208. ret = qcom_geni_serial_port_setup(uport);
  1209. if (ret)
  1210. return ret;
  1211. }
  1212. if (options)
  1213. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1214. return uart_set_options(uport, co, baud, parity, bits, flow);
  1215. }
  1216. static void qcom_geni_serial_earlycon_write(struct console *con,
  1217. const char *s, unsigned int n)
  1218. {
  1219. struct earlycon_device *dev = con->data;
  1220. __qcom_geni_serial_console_write(&dev->port, s, n);
  1221. }
  1222. #ifdef CONFIG_CONSOLE_POLL
  1223. static int qcom_geni_serial_earlycon_read(struct console *con,
  1224. char *s, unsigned int n)
  1225. {
  1226. struct earlycon_device *dev = con->data;
  1227. struct uart_port *uport = &dev->port;
  1228. int num_read = 0;
  1229. int ch;
  1230. while (num_read < n) {
  1231. ch = qcom_geni_serial_get_char(uport);
  1232. if (ch == NO_POLL_CHAR)
  1233. break;
  1234. s[num_read++] = ch;
  1235. }
  1236. return num_read;
  1237. }
  1238. static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
  1239. struct console *con)
  1240. {
  1241. geni_se_setup_s_cmd(se, UART_START_READ, 0);
  1242. con->read = qcom_geni_serial_earlycon_read;
  1243. }
  1244. #else
  1245. static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
  1246. struct console *con) { }
  1247. #endif
  1248. static struct qcom_geni_private_data earlycon_private_data;
  1249. static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
  1250. const char *opt)
  1251. {
  1252. struct uart_port *uport = &dev->port;
  1253. u32 tx_trans_cfg;
  1254. u32 tx_parity_cfg = 0; /* Disable Tx Parity */
  1255. u32 rx_trans_cfg = 0;
  1256. u32 rx_parity_cfg = 0; /* Disable Rx Parity */
  1257. u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
  1258. u32 bits_per_char;
  1259. struct geni_se se;
  1260. if (!uport->membase)
  1261. return -EINVAL;
  1262. uport->private_data = &earlycon_private_data;
  1263. memset(&se, 0, sizeof(se));
  1264. se.base = uport->membase;
  1265. if (geni_se_read_proto(&se) != GENI_SE_UART)
  1266. return -ENXIO;
  1267. /*
  1268. * Ignore Flow control.
  1269. * n = 8.
  1270. */
  1271. tx_trans_cfg = UART_CTS_MASK;
  1272. bits_per_char = BITS_PER_BYTE;
  1273. /*
  1274. * Make an unconditional cancel on the main sequencer to reset
  1275. * it else we could end up in data loss scenarios.
  1276. */
  1277. qcom_geni_serial_poll_tx_done(uport);
  1278. qcom_geni_serial_abort_rx(uport);
  1279. geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
  1280. false, true, true);
  1281. geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
  1282. geni_se_select_mode(&se, GENI_SE_FIFO);
  1283. writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
  1284. writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
  1285. writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
  1286. writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
  1287. writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
  1288. writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
  1289. writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
  1290. dev->con->write = qcom_geni_serial_earlycon_write;
  1291. dev->con->setup = NULL;
  1292. qcom_geni_serial_enable_early_read(&se, dev->con);
  1293. return 0;
  1294. }
  1295. OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
  1296. qcom_geni_serial_earlycon_setup);
  1297. static int __init console_register(struct uart_driver *drv)
  1298. {
  1299. return uart_register_driver(drv);
  1300. }
  1301. static void console_unregister(struct uart_driver *drv)
  1302. {
  1303. uart_unregister_driver(drv);
  1304. }
  1305. static struct console cons_ops = {
  1306. .name = "ttyMSM",
  1307. .write = qcom_geni_serial_console_write,
  1308. .device = uart_console_device,
  1309. .setup = qcom_geni_console_setup,
  1310. .flags = CON_PRINTBUFFER,
  1311. .index = -1,
  1312. .data = &qcom_geni_console_driver,
  1313. };
  1314. static struct uart_driver qcom_geni_console_driver = {
  1315. .owner = THIS_MODULE,
  1316. .driver_name = "qcom_geni_console",
  1317. .dev_name = "ttyMSM",
  1318. .nr = GENI_UART_CONS_PORTS,
  1319. .cons = &cons_ops,
  1320. };
  1321. #else
  1322. static int console_register(struct uart_driver *drv)
  1323. {
  1324. return 0;
  1325. }
  1326. static void console_unregister(struct uart_driver *drv)
  1327. {
  1328. }
  1329. #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
  1330. static struct uart_driver qcom_geni_uart_driver = {
  1331. .owner = THIS_MODULE,
  1332. .driver_name = "qcom_geni_uart",
  1333. .dev_name = "ttyHS",
  1334. .nr = CONFIG_SERIAL_QCOM_GENI_UART_PORTS,
  1335. };
  1336. static int geni_serial_resources_on(struct uart_port *uport)
  1337. {
  1338. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1339. int ret;
  1340. ret = geni_icc_enable(&port->se);
  1341. if (ret)
  1342. return ret;
  1343. ret = geni_se_resources_on(&port->se);
  1344. if (ret) {
  1345. geni_icc_disable(&port->se);
  1346. return ret;
  1347. }
  1348. if (port->clk_rate)
  1349. dev_pm_opp_set_rate(uport->dev, port->clk_rate);
  1350. return 0;
  1351. }
  1352. static int geni_serial_resources_off(struct uart_port *uport)
  1353. {
  1354. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1355. int ret;
  1356. dev_pm_opp_set_rate(uport->dev, 0);
  1357. ret = geni_se_resources_off(&port->se);
  1358. if (ret)
  1359. return ret;
  1360. geni_icc_disable(&port->se);
  1361. return 0;
  1362. }
  1363. static int geni_serial_resource_state(struct uart_port *uport, bool power_on)
  1364. {
  1365. return power_on ? geni_serial_resources_on(uport) : geni_serial_resources_off(uport);
  1366. }
  1367. static int geni_serial_pwr_init(struct uart_port *uport)
  1368. {
  1369. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1370. int ret;
  1371. ret = dev_pm_domain_attach_list(port->se.dev,
  1372. &port->dev_data->pd_data, &port->pd_list);
  1373. if (ret <= 0)
  1374. return -EINVAL;
  1375. return 0;
  1376. }
  1377. static int geni_serial_resource_init(struct uart_port *uport)
  1378. {
  1379. struct qcom_geni_serial_port *port = to_dev_port(uport);
  1380. int ret;
  1381. port->se.clk = devm_clk_get(port->se.dev, "se");
  1382. if (IS_ERR(port->se.clk)) {
  1383. ret = PTR_ERR(port->se.clk);
  1384. dev_err(port->se.dev, "Err getting SE Core clk %d\n", ret);
  1385. return ret;
  1386. }
  1387. ret = geni_icc_get(&port->se, NULL);
  1388. if (ret)
  1389. return ret;
  1390. port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
  1391. port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
  1392. /* Set BW for register access */
  1393. ret = geni_icc_set_bw(&port->se);
  1394. if (ret)
  1395. return ret;
  1396. ret = devm_pm_opp_set_clkname(port->se.dev, "se");
  1397. if (ret)
  1398. return ret;
  1399. /* OPP table is optional */
  1400. ret = devm_pm_opp_of_add_table(port->se.dev);
  1401. if (ret && ret != -ENODEV) {
  1402. dev_err(port->se.dev, "invalid OPP table in device tree\n");
  1403. return ret;
  1404. }
  1405. return 0;
  1406. }
  1407. static void qcom_geni_serial_pm(struct uart_port *uport,
  1408. unsigned int new_state, unsigned int old_state)
  1409. {
  1410. /* If we've never been called, treat it as off */
  1411. if (old_state == UART_PM_STATE_UNDEFINED)
  1412. old_state = UART_PM_STATE_OFF;
  1413. if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
  1414. pm_runtime_resume_and_get(uport->dev);
  1415. else if (new_state == UART_PM_STATE_OFF &&
  1416. old_state == UART_PM_STATE_ON)
  1417. pm_runtime_put_sync(uport->dev);
  1418. }
  1419. /**
  1420. * qcom_geni_rs485_config - Configure RS485 settings for the UART port
  1421. * @uport: Pointer to the UART port structure
  1422. * @termios: Pointer to the termios structure
  1423. * @rs485: Pointer to the RS485 configuration structure
  1424. * This function configures the RTS (Request to Send) pin behavior for RS485 mode.
  1425. * When RS485 mode is enabled, the RTS pin is kept in default ACTIVE HIGH state.
  1426. * Return: Always returns 0.
  1427. */
  1428. static int qcom_geni_rs485_config(struct uart_port *uport,
  1429. struct ktermios *termios, struct serial_rs485 *rs485)
  1430. {
  1431. qcom_geni_set_rs485_mode(uport, SER_RS485_ENABLED);
  1432. return 0;
  1433. }
  1434. static const struct uart_ops qcom_geni_console_pops = {
  1435. .tx_empty = qcom_geni_serial_tx_empty,
  1436. .stop_tx = qcom_geni_serial_stop_tx_fifo,
  1437. .start_tx = qcom_geni_serial_start_tx_fifo,
  1438. .stop_rx = qcom_geni_serial_stop_rx_fifo,
  1439. .start_rx = qcom_geni_serial_start_rx_fifo,
  1440. .set_termios = qcom_geni_serial_set_termios,
  1441. .startup = qcom_geni_serial_startup,
  1442. .request_port = qcom_geni_serial_request_port,
  1443. .config_port = qcom_geni_serial_config_port,
  1444. .shutdown = qcom_geni_serial_shutdown,
  1445. .flush_buffer = qcom_geni_serial_flush_buffer,
  1446. .type = qcom_geni_serial_get_type,
  1447. .set_mctrl = qcom_geni_serial_set_mctrl,
  1448. .get_mctrl = qcom_geni_serial_get_mctrl,
  1449. #ifdef CONFIG_CONSOLE_POLL
  1450. .poll_get_char = qcom_geni_serial_get_char,
  1451. .poll_put_char = qcom_geni_serial_poll_put_char,
  1452. .poll_init = qcom_geni_serial_poll_init,
  1453. #endif
  1454. .pm = qcom_geni_serial_pm,
  1455. };
  1456. static const struct uart_ops qcom_geni_uart_pops = {
  1457. .tx_empty = qcom_geni_serial_tx_empty,
  1458. .stop_tx = qcom_geni_serial_stop_tx_dma,
  1459. .start_tx = qcom_geni_serial_start_tx_dma,
  1460. .start_rx = qcom_geni_serial_start_rx_dma,
  1461. .stop_rx = qcom_geni_serial_stop_rx_dma,
  1462. .set_termios = qcom_geni_serial_set_termios,
  1463. .startup = qcom_geni_serial_startup,
  1464. .request_port = qcom_geni_serial_request_port,
  1465. .config_port = qcom_geni_serial_config_port,
  1466. .shutdown = qcom_geni_serial_shutdown,
  1467. .type = qcom_geni_serial_get_type,
  1468. .set_mctrl = qcom_geni_serial_set_mctrl,
  1469. .get_mctrl = qcom_geni_serial_get_mctrl,
  1470. .pm = qcom_geni_serial_pm,
  1471. };
  1472. static int qcom_geni_serial_probe(struct platform_device *pdev)
  1473. {
  1474. int ret = 0;
  1475. int line;
  1476. struct qcom_geni_serial_port *port;
  1477. struct uart_port *uport;
  1478. struct resource *res;
  1479. int irq;
  1480. struct uart_driver *drv;
  1481. const struct qcom_geni_device_data *data;
  1482. data = of_device_get_match_data(&pdev->dev);
  1483. if (!data)
  1484. return -EINVAL;
  1485. if (data->console) {
  1486. drv = &qcom_geni_console_driver;
  1487. line = of_alias_get_id(pdev->dev.of_node, "serial");
  1488. } else {
  1489. drv = &qcom_geni_uart_driver;
  1490. line = of_alias_get_id(pdev->dev.of_node, "serial");
  1491. if (line == -ENODEV) /* compat with non-standard aliases */
  1492. line = of_alias_get_id(pdev->dev.of_node, "hsuart");
  1493. }
  1494. port = get_port_from_line(line, data->console, &pdev->dev);
  1495. if (IS_ERR(port)) {
  1496. dev_err(&pdev->dev, "Invalid line %d\n", line);
  1497. return PTR_ERR(port);
  1498. }
  1499. uport = &port->uport;
  1500. /* Don't allow 2 drivers to access the same port */
  1501. if (uport->private_data)
  1502. return -ENODEV;
  1503. uport->dev = &pdev->dev;
  1504. port->dev_data = data;
  1505. port->se.dev = &pdev->dev;
  1506. port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
  1507. ret = port->dev_data->resources_init(uport);
  1508. if (ret)
  1509. return ret;
  1510. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1511. if (!res) {
  1512. ret = -EINVAL;
  1513. goto error;
  1514. }
  1515. uport->mapbase = res->start;
  1516. uport->rs485_config = qcom_geni_rs485_config;
  1517. uport->rs485_supported = qcom_geni_rs485_supported;
  1518. port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  1519. port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  1520. port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
  1521. if (!data->console) {
  1522. port->rx_buf = devm_kzalloc(uport->dev,
  1523. DMA_RX_BUF_SIZE, GFP_KERNEL);
  1524. if (!port->rx_buf) {
  1525. ret = -ENOMEM;
  1526. goto error;
  1527. }
  1528. }
  1529. port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
  1530. "qcom_geni_serial_%s%d",
  1531. uart_console(uport) ? "console" : "uart", uport->line);
  1532. if (!port->name) {
  1533. ret = -ENOMEM;
  1534. goto error;
  1535. }
  1536. irq = platform_get_irq(pdev, 0);
  1537. if (irq < 0) {
  1538. ret = irq;
  1539. goto error;
  1540. }
  1541. uport->irq = irq;
  1542. uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
  1543. if (!data->console)
  1544. port->wakeup_irq = platform_get_irq_optional(pdev, 1);
  1545. if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
  1546. port->rx_tx_swap = true;
  1547. if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
  1548. port->cts_rts_swap = true;
  1549. port->private_data.drv = drv;
  1550. uport->private_data = &port->private_data;
  1551. platform_set_drvdata(pdev, port);
  1552. irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
  1553. ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
  1554. IRQF_TRIGGER_HIGH, port->name, uport);
  1555. if (ret) {
  1556. dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
  1557. goto error;
  1558. }
  1559. ret = uart_get_rs485_mode(uport);
  1560. if (ret)
  1561. goto error;
  1562. if (port->wakeup_irq > 0) {
  1563. device_init_wakeup(&pdev->dev, true);
  1564. ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  1565. port->wakeup_irq);
  1566. if (ret) {
  1567. device_init_wakeup(&pdev->dev, false);
  1568. ida_free(&port_ida, uport->line);
  1569. goto error;
  1570. }
  1571. }
  1572. devm_pm_runtime_enable(port->se.dev);
  1573. ret = uart_add_one_port(drv, uport);
  1574. if (ret)
  1575. goto error;
  1576. return 0;
  1577. error:
  1578. dev_pm_domain_detach_list(port->pd_list);
  1579. return ret;
  1580. }
  1581. static void qcom_geni_serial_remove(struct platform_device *pdev)
  1582. {
  1583. struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
  1584. struct uart_port *uport = &port->uport;
  1585. struct uart_driver *drv = port->private_data.drv;
  1586. dev_pm_clear_wake_irq(&pdev->dev);
  1587. device_init_wakeup(&pdev->dev, false);
  1588. ida_free(&port_ida, uport->line);
  1589. uart_remove_one_port(drv, &port->uport);
  1590. dev_pm_domain_detach_list(port->pd_list);
  1591. }
  1592. static int __maybe_unused qcom_geni_serial_runtime_suspend(struct device *dev)
  1593. {
  1594. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1595. struct uart_port *uport = &port->uport;
  1596. int ret = 0;
  1597. if (port->dev_data->power_state)
  1598. ret = port->dev_data->power_state(uport, false);
  1599. return ret;
  1600. }
  1601. static int __maybe_unused qcom_geni_serial_runtime_resume(struct device *dev)
  1602. {
  1603. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1604. struct uart_port *uport = &port->uport;
  1605. int ret = 0;
  1606. if (port->dev_data->power_state)
  1607. ret = port->dev_data->power_state(uport, true);
  1608. return ret;
  1609. }
  1610. static int qcom_geni_serial_suspend(struct device *dev)
  1611. {
  1612. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1613. struct uart_port *uport = &port->uport;
  1614. struct qcom_geni_private_data *private_data = uport->private_data;
  1615. /*
  1616. * This is done so we can hit the lowest possible state in suspend
  1617. * even with no_console_suspend
  1618. */
  1619. if (uart_console(uport)) {
  1620. geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
  1621. geni_icc_set_bw(&port->se);
  1622. }
  1623. return uart_suspend_port(private_data->drv, uport);
  1624. }
  1625. static int qcom_geni_serial_resume(struct device *dev)
  1626. {
  1627. int ret;
  1628. struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
  1629. struct uart_port *uport = &port->uport;
  1630. struct qcom_geni_private_data *private_data = uport->private_data;
  1631. ret = uart_resume_port(private_data->drv, uport);
  1632. if (uart_console(uport)) {
  1633. geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
  1634. geni_icc_set_bw(&port->se);
  1635. }
  1636. return ret;
  1637. }
  1638. static const struct qcom_geni_device_data qcom_geni_console_data = {
  1639. .console = true,
  1640. .mode = GENI_SE_FIFO,
  1641. .resources_init = geni_serial_resource_init,
  1642. .set_rate = geni_serial_set_rate,
  1643. .power_state = geni_serial_resource_state,
  1644. };
  1645. static const struct qcom_geni_device_data qcom_geni_uart_data = {
  1646. .console = false,
  1647. .mode = GENI_SE_DMA,
  1648. .resources_init = geni_serial_resource_init,
  1649. .set_rate = geni_serial_set_rate,
  1650. .power_state = geni_serial_resource_state,
  1651. };
  1652. static const struct qcom_geni_device_data sa8255p_qcom_geni_console_data = {
  1653. .console = true,
  1654. .mode = GENI_SE_FIFO,
  1655. .pd_data = {
  1656. .pd_flags = PD_FLAG_DEV_LINK_ON,
  1657. .pd_names = (const char*[]) { "power", "perf" },
  1658. .num_pd_names = 2,
  1659. },
  1660. .resources_init = geni_serial_pwr_init,
  1661. .set_rate = geni_serial_set_level,
  1662. };
  1663. static const struct qcom_geni_device_data sa8255p_qcom_geni_uart_data = {
  1664. .console = false,
  1665. .mode = GENI_SE_DMA,
  1666. .pd_data = {
  1667. .pd_flags = PD_FLAG_DEV_LINK_ON,
  1668. .pd_names = (const char*[]) { "power", "perf" },
  1669. .num_pd_names = 2,
  1670. },
  1671. .resources_init = geni_serial_pwr_init,
  1672. .set_rate = geni_serial_set_level,
  1673. };
  1674. static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
  1675. SET_RUNTIME_PM_OPS(qcom_geni_serial_runtime_suspend,
  1676. qcom_geni_serial_runtime_resume, NULL)
  1677. SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_suspend, qcom_geni_serial_resume)
  1678. };
  1679. static const struct of_device_id qcom_geni_serial_match_table[] = {
  1680. {
  1681. .compatible = "qcom,geni-debug-uart",
  1682. .data = &qcom_geni_console_data,
  1683. },
  1684. {
  1685. .compatible = "qcom,sa8255p-geni-debug-uart",
  1686. .data = &sa8255p_qcom_geni_console_data,
  1687. },
  1688. {
  1689. .compatible = "qcom,geni-uart",
  1690. .data = &qcom_geni_uart_data,
  1691. },
  1692. {
  1693. .compatible = "qcom,sa8255p-geni-uart",
  1694. .data = &sa8255p_qcom_geni_uart_data,
  1695. },
  1696. {}
  1697. };
  1698. MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
  1699. static struct platform_driver qcom_geni_serial_platform_driver = {
  1700. .remove = qcom_geni_serial_remove,
  1701. .probe = qcom_geni_serial_probe,
  1702. .driver = {
  1703. .name = "qcom_geni_serial",
  1704. .of_match_table = qcom_geni_serial_match_table,
  1705. .pm = &qcom_geni_serial_pm_ops,
  1706. },
  1707. };
  1708. static int __init qcom_geni_serial_init(void)
  1709. {
  1710. int ret;
  1711. ret = console_register(&qcom_geni_console_driver);
  1712. if (ret)
  1713. return ret;
  1714. ret = uart_register_driver(&qcom_geni_uart_driver);
  1715. if (ret) {
  1716. console_unregister(&qcom_geni_console_driver);
  1717. return ret;
  1718. }
  1719. ret = platform_driver_register(&qcom_geni_serial_platform_driver);
  1720. if (ret) {
  1721. console_unregister(&qcom_geni_console_driver);
  1722. uart_unregister_driver(&qcom_geni_uart_driver);
  1723. }
  1724. return ret;
  1725. }
  1726. module_init(qcom_geni_serial_init);
  1727. static void __exit qcom_geni_serial_exit(void)
  1728. {
  1729. platform_driver_unregister(&qcom_geni_serial_platform_driver);
  1730. console_unregister(&qcom_geni_console_driver);
  1731. uart_unregister_driver(&qcom_geni_uart_driver);
  1732. }
  1733. module_exit(qcom_geni_serial_exit);
  1734. MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
  1735. MODULE_LICENSE("GPL v2");