qcom_geni_serial.c 50 KB

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