msm_geni_serial.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  1. /*
  2. * Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/bitmap.h>
  14. #include <linux/bitops.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/console.h>
  18. #include <linux/io.h>
  19. #include <linux/ipc_logging.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/qcom-geni-se.h>
  26. #include <linux/serial.h>
  27. #include <linux/serial_core.h>
  28. #include <linux/slab.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. /* UART specific GENI registers */
  32. #define SE_UART_LOOPBACK_CFG (0x22C)
  33. #define SE_UART_TX_TRANS_CFG (0x25C)
  34. #define SE_UART_TX_WORD_LEN (0x268)
  35. #define SE_UART_TX_STOP_BIT_LEN (0x26C)
  36. #define SE_UART_TX_TRANS_LEN (0x270)
  37. #define SE_UART_RX_TRANS_CFG (0x280)
  38. #define SE_UART_RX_WORD_LEN (0x28C)
  39. #define SE_UART_RX_STALE_CNT (0x294)
  40. #define SE_UART_TX_PARITY_CFG (0x2A4)
  41. #define SE_UART_RX_PARITY_CFG (0x2A8)
  42. #define SE_UART_MANUAL_RFR (0x2AC)
  43. /* SE_UART_LOOPBACK_CFG */
  44. #define NO_LOOPBACK (0)
  45. #define TX_RX_LOOPBACK (0x1)
  46. #define CTS_RFR_LOOPBACK (0x2)
  47. #define CTSRFR_TXRX_LOOPBACK (0x3)
  48. /* SE_UART_TRANS_CFG */
  49. #define UART_TX_PAR_EN (BIT(0))
  50. #define UART_CTS_MASK (BIT(1))
  51. /* SE_UART_TX_WORD_LEN */
  52. #define TX_WORD_LEN_MSK (GENMASK(9, 0))
  53. /* SE_UART_TX_STOP_BIT_LEN */
  54. #define TX_STOP_BIT_LEN_MSK (GENMASK(23, 0))
  55. #define TX_STOP_BIT_LEN_1 (0)
  56. #define TX_STOP_BIT_LEN_1_5 (1)
  57. #define TX_STOP_BIT_LEN_2 (2)
  58. /* SE_UART_TX_TRANS_LEN */
  59. #define TX_TRANS_LEN_MSK (GENMASK(23, 0))
  60. /* SE_UART_RX_TRANS_CFG */
  61. #define UART_RX_INS_STATUS_BIT (BIT(2))
  62. #define UART_RX_PAR_EN (BIT(3))
  63. /* SE_UART_RX_WORD_LEN */
  64. #define RX_WORD_LEN_MASK (GENMASK(9, 0))
  65. /* SE_UART_RX_STALE_CNT */
  66. #define RX_STALE_CNT (GENMASK(23, 0))
  67. /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
  68. #define PAR_CALC_EN (BIT(0))
  69. #define PAR_MODE_MSK (GENMASK(2, 1))
  70. #define PAR_MODE_SHFT (1)
  71. #define PAR_EVEN (0x00)
  72. #define PAR_ODD (0x01)
  73. #define PAR_SPACE (0x10)
  74. #define PAR_MARK (0x11)
  75. /* SE_UART_MANUAL_RFR register fields */
  76. #define UART_MANUAL_RFR_EN (BIT(31))
  77. #define UART_RFR_NOT_READY (BIT(1))
  78. #define UART_RFR_READY (BIT(0))
  79. /* UART M_CMD OP codes */
  80. #define UART_START_TX (0x1)
  81. #define UART_START_BREAK (0x4)
  82. #define UART_STOP_BREAK (0x5)
  83. /* UART S_CMD OP codes */
  84. #define UART_START_READ (0x1)
  85. #define UART_PARAM (0x1)
  86. #define UART_PARAM_RFR_OPEN (BIT(7))
  87. /* UART DMA Rx GP_IRQ_BITS */
  88. #define UART_DMA_RX_PARITY_ERR BIT(5)
  89. #define UART_DMA_RX_ERRS (GENMASK(5, 6))
  90. #define UART_DMA_RX_BREAK (GENMASK(7, 8))
  91. #define UART_OVERSAMPLING (32)
  92. #define STALE_TIMEOUT (16)
  93. #define DEFAULT_BITS_PER_CHAR (10)
  94. #define GENI_UART_NR_PORTS (15)
  95. #define GENI_UART_CONS_PORTS (2)
  96. #define DEF_FIFO_DEPTH_WORDS (16)
  97. #define DEF_TX_WM (2)
  98. #define DEF_FIFO_WIDTH_BITS (32)
  99. #define UART_CORE2X_VOTE (10000)
  100. #define UART_CONSOLE_CORE2X_VOTE (960)
  101. #define WAKEBYTE_TIMEOUT_MSEC (2000)
  102. #define WAIT_XFER_MAX_ITER (50)
  103. #define WAIT_XFER_MAX_TIMEOUT_US (10000)
  104. #define WAIT_XFER_MIN_TIMEOUT_US (9000)
  105. #define IPC_LOG_PWR_PAGES (6)
  106. #define IPC_LOG_MISC_PAGES (10)
  107. #define IPC_LOG_TX_RX_PAGES (8)
  108. #define DATA_BYTES_PER_LINE (32)
  109. #define IPC_LOG_MSG(ctx, x...) do { \
  110. if (ctx) \
  111. ipc_log_string(ctx, x); \
  112. } while (0)
  113. #define DMA_RX_BUF_SIZE (2048)
  114. #define UART_CONSOLE_RX_WM (2)
  115. struct msm_geni_serial_port {
  116. struct uart_port uport;
  117. char name[20];
  118. unsigned int tx_fifo_depth;
  119. unsigned int tx_fifo_width;
  120. unsigned int rx_fifo_depth;
  121. unsigned int tx_wm;
  122. unsigned int rx_wm;
  123. unsigned int rx_rfr;
  124. int xfer_mode;
  125. struct dentry *dbg;
  126. bool port_setup;
  127. unsigned int *rx_fifo;
  128. int (*handle_rx)(struct uart_port *uport,
  129. unsigned int rx_fifo_wc,
  130. unsigned int rx_last_byte_valid,
  131. unsigned int rx_last,
  132. bool drop_rx);
  133. struct device *wrapper_dev;
  134. struct se_geni_rsc serial_rsc;
  135. dma_addr_t tx_dma;
  136. unsigned int xmit_size;
  137. void *rx_buf;
  138. dma_addr_t rx_dma;
  139. int loopback;
  140. int wakeup_irq;
  141. unsigned char wakeup_byte;
  142. struct wakeup_source geni_wake;
  143. void *ipc_log_tx;
  144. void *ipc_log_rx;
  145. void *ipc_log_pwr;
  146. void *ipc_log_misc;
  147. unsigned int cur_baud;
  148. int ioctl_count;
  149. int edge_count;
  150. bool manual_flow;
  151. };
  152. static const struct uart_ops msm_geni_serial_pops;
  153. static struct uart_driver msm_geni_console_driver;
  154. static struct uart_driver msm_geni_serial_hs_driver;
  155. static int handle_rx_console(struct uart_port *uport,
  156. unsigned int rx_fifo_wc,
  157. unsigned int rx_last_byte_valid,
  158. unsigned int rx_last,
  159. bool drop_rx);
  160. static int handle_rx_hs(struct uart_port *uport,
  161. unsigned int rx_fifo_wc,
  162. unsigned int rx_last_byte_valid,
  163. unsigned int rx_last,
  164. bool drop_rx);
  165. static unsigned int msm_geni_serial_tx_empty(struct uart_port *port);
  166. static int msm_geni_serial_power_on(struct uart_port *uport);
  167. static void msm_geni_serial_power_off(struct uart_port *uport);
  168. static int msm_geni_serial_poll_bit(struct uart_port *uport,
  169. int offset, int bit_field, bool set);
  170. static void msm_geni_serial_stop_rx(struct uart_port *uport);
  171. static int msm_geni_serial_runtime_resume(struct device *dev);
  172. static int msm_geni_serial_runtime_suspend(struct device *dev);
  173. static atomic_t uart_line_id = ATOMIC_INIT(0);
  174. #define GET_DEV_PORT(uport) \
  175. container_of(uport, struct msm_geni_serial_port, uport)
  176. static struct msm_geni_serial_port msm_geni_console_port[GENI_UART_CONS_PORTS];
  177. static struct msm_geni_serial_port msm_geni_serial_ports[GENI_UART_NR_PORTS];
  178. static void msm_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
  179. {
  180. if (cfg_flags & UART_CONFIG_TYPE)
  181. uport->type = PORT_MSM;
  182. }
  183. static ssize_t msm_geni_serial_loopback_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct platform_device *pdev = to_platform_device(dev);
  187. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  188. return snprintf(buf, sizeof(int), "%d\n", port->loopback);
  189. }
  190. static ssize_t msm_geni_serial_loopback_store(struct device *dev,
  191. struct device_attribute *attr, const char *buf,
  192. size_t size)
  193. {
  194. struct platform_device *pdev = to_platform_device(dev);
  195. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  196. if (kstrtoint(buf, 0, &port->loopback)) {
  197. dev_err(dev, "Invalid input\n");
  198. return -EINVAL;
  199. }
  200. return size;
  201. }
  202. static DEVICE_ATTR(loopback, 0644, msm_geni_serial_loopback_show,
  203. msm_geni_serial_loopback_store);
  204. static void dump_ipc(void *ipc_ctx, char *prefix, char *string,
  205. u64 addr, int size)
  206. {
  207. char buf[DATA_BYTES_PER_LINE * 2];
  208. int len = 0;
  209. if (!ipc_ctx)
  210. return;
  211. len = min(size, DATA_BYTES_PER_LINE);
  212. hex_dump_to_buffer(string, len, DATA_BYTES_PER_LINE, 1, buf,
  213. sizeof(buf), false);
  214. ipc_log_string(ipc_ctx, "%s[0x%.10x:%d] : %s", prefix,
  215. (unsigned int)addr, size, buf);
  216. }
  217. static bool device_pending_suspend(struct uart_port *uport)
  218. {
  219. int usage_count = atomic_read(&uport->dev->power.usage_count);
  220. return (pm_runtime_status_suspended(uport->dev) || !usage_count);
  221. }
  222. static bool check_transfers_inflight(struct uart_port *uport)
  223. {
  224. bool xfer_on = false;
  225. bool tx_active = false;
  226. bool tx_fifo_status = false;
  227. bool m_cmd_active = false;
  228. bool rx_active = false;
  229. u32 rx_fifo_status = 0;
  230. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  231. u32 geni_status = geni_read_reg_nolog(uport->membase,
  232. SE_GENI_STATUS);
  233. struct circ_buf *xmit = &uport->state->xmit;
  234. /* Possible stop tx is called multiple times. */
  235. m_cmd_active = geni_status & M_GENI_CMD_ACTIVE;
  236. if (port->xfer_mode == SE_DMA) {
  237. tx_fifo_status = port->tx_dma ? 1 : 0;
  238. rx_fifo_status =
  239. geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN);
  240. } else {
  241. tx_fifo_status = geni_read_reg_nolog(uport->membase,
  242. SE_GENI_TX_FIFO_STATUS);
  243. rx_fifo_status = geni_read_reg_nolog(uport->membase,
  244. SE_GENI_RX_FIFO_STATUS);
  245. }
  246. tx_active = m_cmd_active || tx_fifo_status;
  247. rx_active = rx_fifo_status ? true : false;
  248. if (rx_active || tx_active || !uart_circ_empty(xmit))
  249. xfer_on = true;
  250. return xfer_on;
  251. }
  252. static void wait_for_transfers_inflight(struct uart_port *uport)
  253. {
  254. int iter = 0;
  255. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  256. while (iter < WAIT_XFER_MAX_ITER) {
  257. if (check_transfers_inflight(uport)) {
  258. usleep_range(WAIT_XFER_MIN_TIMEOUT_US,
  259. WAIT_XFER_MAX_TIMEOUT_US);
  260. iter++;
  261. } else {
  262. break;
  263. }
  264. }
  265. if (check_transfers_inflight(uport)) {
  266. u32 geni_status = geni_read_reg_nolog(uport->membase,
  267. SE_GENI_STATUS);
  268. u32 geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS);
  269. u32 rx_fifo_status = geni_read_reg_nolog(uport->membase,
  270. SE_GENI_RX_FIFO_STATUS);
  271. u32 rx_dma =
  272. geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN);
  273. IPC_LOG_MSG(port->ipc_log_misc,
  274. "%s IOS 0x%x geni status 0x%x rx: fifo 0x%x dma 0x%x\n",
  275. __func__, geni_ios, geni_status, rx_fifo_status, rx_dma);
  276. }
  277. }
  278. static int vote_clock_on(struct uart_port *uport)
  279. {
  280. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  281. int usage_count = atomic_read(&uport->dev->power.usage_count);
  282. int ret = 0;
  283. ret = msm_geni_serial_power_on(uport);
  284. if (ret) {
  285. dev_err(uport->dev, "Failed to vote clock on\n");
  286. return ret;
  287. }
  288. port->ioctl_count++;
  289. IPC_LOG_MSG(port->ipc_log_pwr, "%s%s ioctl %d usage_count %d\n",
  290. __func__, current->comm, port->ioctl_count, usage_count);
  291. return 0;
  292. }
  293. static int vote_clock_off(struct uart_port *uport)
  294. {
  295. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  296. int usage_count = atomic_read(&uport->dev->power.usage_count);
  297. if (!pm_runtime_enabled(uport->dev)) {
  298. dev_err(uport->dev, "RPM not available.Can't enable clocks\n");
  299. return -EPERM;
  300. }
  301. if (!port->ioctl_count) {
  302. dev_warn(uport->dev, "%s:Imbalanced vote off ioctl %d\n",
  303. __func__, port->ioctl_count);
  304. IPC_LOG_MSG(port->ipc_log_pwr,
  305. "%s:Imbalanced vote_off from userspace. %d",
  306. __func__, port->ioctl_count);
  307. return -EPERM;
  308. }
  309. wait_for_transfers_inflight(uport);
  310. port->ioctl_count--;
  311. msm_geni_serial_power_off(uport);
  312. IPC_LOG_MSG(port->ipc_log_pwr, "%s%s ioctl %d usage_count %d\n",
  313. __func__, current->comm, port->ioctl_count, usage_count);
  314. return 0;
  315. };
  316. static int msm_geni_serial_ioctl(struct uart_port *uport, unsigned int cmd,
  317. unsigned long arg)
  318. {
  319. int ret = -ENOIOCTLCMD;
  320. switch (cmd) {
  321. case TIOCPMGET: {
  322. ret = vote_clock_on(uport);
  323. break;
  324. }
  325. case TIOCPMPUT: {
  326. ret = vote_clock_off(uport);
  327. break;
  328. }
  329. case TIOCPMACT: {
  330. ret = !pm_runtime_status_suspended(uport->dev);
  331. break;
  332. }
  333. default:
  334. break;
  335. }
  336. return ret;
  337. }
  338. static void msm_geni_serial_break_ctl(struct uart_port *uport, int ctl)
  339. {
  340. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  341. if (!uart_console(uport) && device_pending_suspend(uport)) {
  342. IPC_LOG_MSG(port->ipc_log_misc,
  343. "%s.Device is suspended.\n", __func__);
  344. return;
  345. }
  346. if (ctl) {
  347. wait_for_transfers_inflight(uport);
  348. geni_setup_m_cmd(uport->membase, UART_START_BREAK, 0);
  349. } else {
  350. geni_setup_m_cmd(uport->membase, UART_STOP_BREAK, 0);
  351. }
  352. /* Ensure break start/stop command is setup before returning.*/
  353. mb();
  354. }
  355. static unsigned int msm_geni_cons_get_mctrl(struct uart_port *uport)
  356. {
  357. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  358. }
  359. static unsigned int msm_geni_serial_get_mctrl(struct uart_port *uport)
  360. {
  361. u32 geni_ios = 0;
  362. unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
  363. if (device_pending_suspend(uport))
  364. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  365. geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS);
  366. if (!(geni_ios & IO2_DATA_IN))
  367. mctrl |= TIOCM_CTS;
  368. return mctrl;
  369. }
  370. static void msm_geni_cons_set_mctrl(struct uart_port *uport,
  371. unsigned int mctrl)
  372. {
  373. }
  374. static void msm_geni_serial_set_mctrl(struct uart_port *uport,
  375. unsigned int mctrl)
  376. {
  377. u32 uart_manual_rfr = 0;
  378. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  379. if (device_pending_suspend(uport)) {
  380. IPC_LOG_MSG(port->ipc_log_misc,
  381. "%s.Device is suspended.\n", __func__);
  382. return;
  383. }
  384. if (!(mctrl & TIOCM_RTS)) {
  385. uart_manual_rfr |= (UART_MANUAL_RFR_EN | UART_RFR_NOT_READY);
  386. port->manual_flow = true;
  387. } else {
  388. port->manual_flow = false;
  389. }
  390. geni_write_reg_nolog(uart_manual_rfr, uport->membase,
  391. SE_UART_MANUAL_RFR);
  392. /* Write to flow control must complete before return to client*/
  393. mb();
  394. IPC_LOG_MSG(port->ipc_log_misc, "%s: Manual_rfr 0x%x\n",
  395. __func__, uart_manual_rfr);
  396. }
  397. static const char *msm_geni_serial_get_type(struct uart_port *uport)
  398. {
  399. return "MSM";
  400. }
  401. static struct msm_geni_serial_port *get_port_from_line(int line,
  402. bool is_console)
  403. {
  404. struct msm_geni_serial_port *port = NULL;
  405. if (is_console) {
  406. if ((line < 0) || (line >= GENI_UART_CONS_PORTS))
  407. port = ERR_PTR(-ENXIO);
  408. port = &msm_geni_console_port[line];
  409. } else {
  410. if ((line < 0) || (line >= GENI_UART_NR_PORTS))
  411. return ERR_PTR(-ENXIO);
  412. port = &msm_geni_serial_ports[line];
  413. }
  414. return port;
  415. }
  416. static int msm_geni_serial_power_on(struct uart_port *uport)
  417. {
  418. int ret = 0;
  419. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  420. if (!pm_runtime_enabled(uport->dev)) {
  421. if (pm_runtime_status_suspended(uport->dev)) {
  422. struct uart_state *state = uport->state;
  423. struct tty_port *tport = &state->port;
  424. int lock = mutex_trylock(&tport->mutex);
  425. IPC_LOG_MSG(port->ipc_log_pwr,
  426. "%s:Manual resume\n", __func__);
  427. pm_runtime_disable(uport->dev);
  428. ret = msm_geni_serial_runtime_resume(uport->dev);
  429. if (ret) {
  430. IPC_LOG_MSG(port->ipc_log_pwr,
  431. "%s:Manual RPM CB failed %d\n",
  432. __func__, ret);
  433. } else {
  434. pm_runtime_get_noresume(uport->dev);
  435. pm_runtime_set_active(uport->dev);
  436. enable_irq(uport->irq);
  437. }
  438. pm_runtime_enable(uport->dev);
  439. if (lock)
  440. mutex_unlock(&tport->mutex);
  441. }
  442. } else {
  443. ret = pm_runtime_get_sync(uport->dev);
  444. if (ret < 0) {
  445. IPC_LOG_MSG(port->ipc_log_pwr, "%s Err\n", __func__);
  446. WARN_ON_ONCE(1);
  447. pm_runtime_put_noidle(uport->dev);
  448. pm_runtime_set_suspended(uport->dev);
  449. return ret;
  450. }
  451. }
  452. return 0;
  453. }
  454. static void msm_geni_serial_power_off(struct uart_port *uport)
  455. {
  456. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  457. int usage_count = atomic_read(&uport->dev->power.usage_count);
  458. if (!usage_count) {
  459. IPC_LOG_MSG(port->ipc_log_pwr, "%s: Usage Count is already 0\n",
  460. __func__);
  461. return;
  462. }
  463. pm_runtime_mark_last_busy(uport->dev);
  464. pm_runtime_put_autosuspend(uport->dev);
  465. }
  466. static int msm_geni_serial_poll_bit(struct uart_port *uport,
  467. int offset, int bit_field, bool set)
  468. {
  469. int iter = 0;
  470. unsigned int reg;
  471. bool met = false;
  472. struct msm_geni_serial_port *port = NULL;
  473. bool cond = false;
  474. unsigned int baud = 115200;
  475. unsigned int fifo_bits = DEF_FIFO_DEPTH_WORDS * DEF_FIFO_WIDTH_BITS;
  476. unsigned long total_iter = 1000;
  477. if (uport->private_data && !uart_console(uport)) {
  478. port = GET_DEV_PORT(uport);
  479. baud = (port->cur_baud ? port->cur_baud : 115200);
  480. fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
  481. /*
  482. * Total polling iterations based on FIFO worth of bytes to be
  483. * sent at current baud .Add a little fluff to the wait.
  484. */
  485. total_iter = ((fifo_bits * USEC_PER_SEC) / baud) / 10;
  486. total_iter += 50;
  487. }
  488. while (iter < total_iter) {
  489. reg = geni_read_reg_nolog(uport->membase, offset);
  490. cond = reg & bit_field;
  491. if (cond == set) {
  492. met = true;
  493. break;
  494. }
  495. udelay(10);
  496. iter++;
  497. }
  498. return met;
  499. }
  500. static void msm_geni_serial_setup_tx(struct uart_port *uport,
  501. unsigned int xmit_size)
  502. {
  503. u32 m_cmd = 0;
  504. geni_write_reg_nolog(xmit_size, uport->membase, SE_UART_TX_TRANS_LEN);
  505. m_cmd |= (UART_START_TX << M_OPCODE_SHFT);
  506. geni_write_reg_nolog(m_cmd, uport->membase, SE_GENI_M_CMD0);
  507. /*
  508. * Writes to enable the primary sequencer should go through before
  509. * exiting this function.
  510. */
  511. mb();
  512. }
  513. static void msm_geni_serial_poll_cancel_tx(struct uart_port *uport)
  514. {
  515. int done = 0;
  516. unsigned int irq_clear = M_CMD_DONE_EN;
  517. done = msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  518. M_CMD_DONE_EN, true);
  519. if (!done) {
  520. geni_write_reg_nolog(M_GENI_CMD_ABORT, uport->membase,
  521. SE_GENI_M_CMD_CTRL_REG);
  522. irq_clear |= M_CMD_ABORT_EN;
  523. msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  524. M_CMD_ABORT_EN, true);
  525. }
  526. geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_M_IRQ_CLEAR);
  527. }
  528. static void msm_geni_serial_abort_rx(struct uart_port *uport)
  529. {
  530. unsigned int irq_clear = S_CMD_DONE_EN;
  531. geni_abort_s_cmd(uport->membase);
  532. /* Ensure this goes through before polling. */
  533. mb();
  534. irq_clear |= S_CMD_ABORT_EN;
  535. msm_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  536. S_GENI_CMD_ABORT, false);
  537. geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_S_IRQ_CLEAR);
  538. geni_write_reg(FORCE_DEFAULT, uport->membase, GENI_FORCE_DEFAULT_REG);
  539. }
  540. static void msm_geni_serial_complete_rx_eot(struct uart_port *uport)
  541. {
  542. int poll_done = 0, tries = 0;
  543. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  544. do {
  545. poll_done = msm_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
  546. RX_EOT, true);
  547. tries++;
  548. } while (!poll_done && tries < 5);
  549. if (!poll_done)
  550. IPC_LOG_MSG(port->ipc_log_misc,
  551. "%s: RX_EOT, GENI:0x%x, DMA_DEBUG:0x%x\n", __func__,
  552. geni_read_reg_nolog(uport->membase, SE_GENI_STATUS),
  553. geni_read_reg_nolog(uport->membase, SE_DMA_DEBUG_REG0));
  554. else
  555. geni_write_reg_nolog(RX_EOT, uport->membase, SE_DMA_RX_IRQ_CLR);
  556. }
  557. #ifdef CONFIG_CONSOLE_POLL
  558. static int msm_geni_serial_get_char(struct uart_port *uport)
  559. {
  560. unsigned int rx_fifo;
  561. unsigned int m_irq_status;
  562. unsigned int s_irq_status;
  563. if (!(msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  564. M_SEC_IRQ_EN, true)))
  565. return -ENXIO;
  566. m_irq_status = geni_read_reg_nolog(uport->membase,
  567. SE_GENI_M_IRQ_STATUS);
  568. s_irq_status = geni_read_reg_nolog(uport->membase,
  569. SE_GENI_S_IRQ_STATUS);
  570. geni_write_reg_nolog(m_irq_status, uport->membase,
  571. SE_GENI_M_IRQ_CLEAR);
  572. geni_write_reg_nolog(s_irq_status, uport->membase,
  573. SE_GENI_S_IRQ_CLEAR);
  574. if (!(msm_geni_serial_poll_bit(uport, SE_GENI_RX_FIFO_STATUS,
  575. RX_FIFO_WC_MSK, true)))
  576. return -ENXIO;
  577. /*
  578. * Read the Rx FIFO only after clearing the interrupt registers and
  579. * getting valid RX fifo status.
  580. */
  581. mb();
  582. rx_fifo = geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn);
  583. rx_fifo &= 0xFF;
  584. return rx_fifo;
  585. }
  586. static void msm_geni_serial_poll_put_char(struct uart_port *uport,
  587. unsigned char c)
  588. {
  589. int b = (int) c;
  590. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  591. geni_write_reg_nolog(port->tx_wm, uport->membase,
  592. SE_GENI_TX_WATERMARK_REG);
  593. msm_geni_serial_setup_tx(uport, 1);
  594. if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  595. M_TX_FIFO_WATERMARK_EN, true))
  596. WARN_ON(1);
  597. geni_write_reg_nolog(b, uport->membase, SE_GENI_TX_FIFOn);
  598. geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase,
  599. SE_GENI_M_IRQ_CLEAR);
  600. /*
  601. * Ensure FIFO write goes through before polling for status but.
  602. */
  603. mb();
  604. msm_geni_serial_poll_cancel_tx(uport);
  605. }
  606. #endif
  607. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  608. static void msm_geni_serial_wr_char(struct uart_port *uport, int ch)
  609. {
  610. geni_write_reg_nolog(ch, uport->membase, SE_GENI_TX_FIFOn);
  611. /*
  612. * Ensure FIFO write clear goes through before
  613. * next iteration.
  614. */
  615. mb();
  616. }
  617. static void
  618. __msm_geni_serial_console_write(struct uart_port *uport, const char *s,
  619. unsigned int count)
  620. {
  621. unsigned int new_line = 0;
  622. unsigned int i;
  623. unsigned int bytes_to_send = count;
  624. unsigned int fifo_depth = DEF_FIFO_DEPTH_WORDS;
  625. unsigned int tx_wm = DEF_TX_WM;
  626. for (i = 0; i < count; i++) {
  627. if (s[i] == '\n')
  628. new_line++;
  629. }
  630. bytes_to_send += new_line;
  631. geni_write_reg_nolog(tx_wm, uport->membase,
  632. SE_GENI_TX_WATERMARK_REG);
  633. msm_geni_serial_setup_tx(uport, bytes_to_send);
  634. i = 0;
  635. while (i < count) {
  636. u32 chars_to_write = 0;
  637. u32 avail_fifo_bytes = (fifo_depth - tx_wm);
  638. /*
  639. * If the WM bit never set, then the Tx state machine is not
  640. * in a valid state, so break, cancel/abort any existing
  641. * command. Unfortunately the current data being written is
  642. * lost.
  643. */
  644. if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  645. M_TX_FIFO_WATERMARK_EN, true))
  646. break;
  647. chars_to_write = min((count - i), avail_fifo_bytes);
  648. if ((chars_to_write << 1) > avail_fifo_bytes)
  649. chars_to_write = (avail_fifo_bytes >> 1);
  650. uart_console_write(uport, (s + i), chars_to_write,
  651. msm_geni_serial_wr_char);
  652. geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase,
  653. SE_GENI_M_IRQ_CLEAR);
  654. /* Ensure this goes through before polling for WM IRQ again.*/
  655. mb();
  656. i += chars_to_write;
  657. }
  658. msm_geni_serial_poll_cancel_tx(uport);
  659. }
  660. static void msm_geni_serial_console_write(struct console *co, const char *s,
  661. unsigned count)
  662. {
  663. struct uart_port *uport;
  664. struct msm_geni_serial_port *port;
  665. int locked = 1;
  666. unsigned long flags;
  667. WARN_ON(co->index < 0 || co->index >= GENI_UART_NR_PORTS);
  668. port = get_port_from_line(co->index, true);
  669. if (IS_ERR_OR_NULL(port))
  670. return;
  671. uport = &port->uport;
  672. if (oops_in_progress)
  673. locked = spin_trylock_irqsave(&uport->lock, flags);
  674. else
  675. spin_lock_irqsave(&uport->lock, flags);
  676. if (locked) {
  677. __msm_geni_serial_console_write(uport, s, count);
  678. spin_unlock_irqrestore(&uport->lock, flags);
  679. }
  680. }
  681. static int handle_rx_console(struct uart_port *uport,
  682. unsigned int rx_fifo_wc,
  683. unsigned int rx_last_byte_valid,
  684. unsigned int rx_last,
  685. bool drop_rx)
  686. {
  687. int i, c;
  688. unsigned char *rx_char;
  689. struct tty_port *tport;
  690. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  691. tport = &uport->state->port;
  692. for (i = 0; i < rx_fifo_wc; i++) {
  693. int bytes = 4;
  694. *(msm_port->rx_fifo) =
  695. geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn);
  696. if (drop_rx)
  697. continue;
  698. rx_char = (unsigned char *)msm_port->rx_fifo;
  699. if (i == (rx_fifo_wc - 1)) {
  700. if (rx_last && rx_last_byte_valid)
  701. bytes = rx_last_byte_valid;
  702. }
  703. for (c = 0; c < bytes; c++) {
  704. char flag = TTY_NORMAL;
  705. int sysrq;
  706. uport->icount.rx++;
  707. sysrq = uart_handle_sysrq_char(uport, rx_char[c]);
  708. if (!sysrq)
  709. tty_insert_flip_char(tport, rx_char[c], flag);
  710. }
  711. }
  712. if (!drop_rx)
  713. tty_flip_buffer_push(tport);
  714. return 0;
  715. }
  716. #else
  717. static int handle_rx_console(struct uart_port *uport,
  718. unsigned int rx_fifo_wc,
  719. unsigned int rx_last_byte_valid,
  720. unsigned int rx_last,
  721. bool drop_rx)
  722. {
  723. return -EPERM;
  724. }
  725. #endif /* (CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)) */
  726. static int msm_geni_serial_prep_dma_tx(struct uart_port *uport)
  727. {
  728. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  729. struct circ_buf *xmit = &uport->state->xmit;
  730. unsigned int xmit_size;
  731. int ret = 0;
  732. xmit_size = uart_circ_chars_pending(xmit);
  733. if (xmit_size < WAKEUP_CHARS)
  734. uart_write_wakeup(uport);
  735. if (xmit_size > (UART_XMIT_SIZE - xmit->tail))
  736. xmit_size = UART_XMIT_SIZE - xmit->tail;
  737. if (!xmit_size)
  738. return ret;
  739. dump_ipc(msm_port->ipc_log_tx, "DMA Tx",
  740. (char *)&xmit->buf[xmit->tail], 0, xmit_size);
  741. msm_geni_serial_setup_tx(uport, xmit_size);
  742. ret = geni_se_tx_dma_prep(msm_port->wrapper_dev, uport->membase,
  743. &xmit->buf[xmit->tail], xmit_size, &msm_port->tx_dma);
  744. if (!ret) {
  745. msm_port->xmit_size = xmit_size;
  746. } else {
  747. geni_write_reg_nolog(0, uport->membase,
  748. SE_UART_TX_TRANS_LEN);
  749. geni_cancel_m_cmd(uport->membase);
  750. if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  751. M_CMD_CANCEL_EN, true)) {
  752. geni_abort_m_cmd(uport->membase);
  753. msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  754. M_CMD_ABORT_EN, true);
  755. geni_write_reg_nolog(M_CMD_ABORT_EN, uport->membase,
  756. SE_GENI_M_IRQ_CLEAR);
  757. }
  758. geni_write_reg_nolog(M_CMD_CANCEL_EN, uport->membase,
  759. SE_GENI_M_IRQ_CLEAR);
  760. IPC_LOG_MSG(msm_port->ipc_log_tx, "%s: DMA map failure %d\n",
  761. __func__, ret);
  762. msm_port->tx_dma = (dma_addr_t)NULL;
  763. msm_port->xmit_size = 0;
  764. }
  765. return ret;
  766. }
  767. static void msm_geni_serial_start_tx(struct uart_port *uport)
  768. {
  769. unsigned int geni_m_irq_en;
  770. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  771. unsigned int geni_status;
  772. unsigned int geni_ios;
  773. if (!uart_console(uport) && !pm_runtime_active(uport->dev)) {
  774. IPC_LOG_MSG(msm_port->ipc_log_misc,
  775. "%s.Putting in async RPM vote\n", __func__);
  776. pm_runtime_get(uport->dev);
  777. goto exit_start_tx;
  778. }
  779. if (!uart_console(uport)) {
  780. IPC_LOG_MSG(msm_port->ipc_log_misc,
  781. "%s.Power on.\n", __func__);
  782. pm_runtime_get(uport->dev);
  783. }
  784. if (msm_port->xfer_mode == FIFO_MODE) {
  785. geni_status = geni_read_reg_nolog(uport->membase,
  786. SE_GENI_STATUS);
  787. if (geni_status & M_GENI_CMD_ACTIVE)
  788. goto check_flow_ctrl;
  789. if (!msm_geni_serial_tx_empty(uport))
  790. goto check_flow_ctrl;
  791. geni_m_irq_en = geni_read_reg_nolog(uport->membase,
  792. SE_GENI_M_IRQ_EN);
  793. geni_m_irq_en |= (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN);
  794. geni_write_reg_nolog(msm_port->tx_wm, uport->membase,
  795. SE_GENI_TX_WATERMARK_REG);
  796. geni_write_reg_nolog(geni_m_irq_en, uport->membase,
  797. SE_GENI_M_IRQ_EN);
  798. /* Geni command setup should complete before returning.*/
  799. mb();
  800. } else if (msm_port->xfer_mode == SE_DMA) {
  801. if (msm_port->tx_dma)
  802. goto check_flow_ctrl;
  803. msm_geni_serial_prep_dma_tx(uport);
  804. }
  805. return;
  806. check_flow_ctrl:
  807. geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS);
  808. if (!(geni_ios & IO2_DATA_IN))
  809. IPC_LOG_MSG(msm_port->ipc_log_misc, "%s: ios: 0x%08x\n",
  810. __func__, geni_ios);
  811. exit_start_tx:
  812. if (!uart_console(uport))
  813. msm_geni_serial_power_off(uport);
  814. }
  815. static void msm_geni_serial_tx_fsm_rst(struct uart_port *uport)
  816. {
  817. unsigned int tx_irq_en;
  818. int done = 0;
  819. int tries = 0;
  820. tx_irq_en = geni_read_reg_nolog(uport->membase, SE_DMA_TX_IRQ_EN);
  821. geni_write_reg_nolog(0, uport->membase, SE_DMA_TX_IRQ_EN_SET);
  822. geni_write_reg_nolog(1, uport->membase, SE_DMA_TX_FSM_RST);
  823. do {
  824. done = msm_geni_serial_poll_bit(uport, SE_DMA_TX_IRQ_STAT,
  825. TX_RESET_DONE, true);
  826. tries++;
  827. } while (!done && tries < 5);
  828. geni_write_reg_nolog(TX_DMA_DONE | TX_RESET_DONE, uport->membase,
  829. SE_DMA_TX_IRQ_CLR);
  830. geni_write_reg_nolog(tx_irq_en, uport->membase, SE_DMA_TX_IRQ_EN_SET);
  831. }
  832. static void stop_tx_sequencer(struct uart_port *uport)
  833. {
  834. unsigned int geni_m_irq_en;
  835. unsigned int geni_status;
  836. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  837. geni_m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN);
  838. geni_m_irq_en &= ~M_CMD_DONE_EN;
  839. if (port->xfer_mode == FIFO_MODE) {
  840. geni_m_irq_en &= ~M_TX_FIFO_WATERMARK_EN;
  841. geni_write_reg_nolog(0, uport->membase,
  842. SE_GENI_TX_WATERMARK_REG);
  843. } else if (port->xfer_mode == SE_DMA) {
  844. if (port->tx_dma) {
  845. msm_geni_serial_tx_fsm_rst(uport);
  846. geni_se_tx_dma_unprep(port->wrapper_dev, port->tx_dma,
  847. port->xmit_size);
  848. port->tx_dma = (dma_addr_t)NULL;
  849. }
  850. }
  851. port->xmit_size = 0;
  852. geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN);
  853. geni_status = geni_read_reg_nolog(uport->membase,
  854. SE_GENI_STATUS);
  855. /* Possible stop tx is called multiple times. */
  856. if (!(geni_status & M_GENI_CMD_ACTIVE))
  857. return;
  858. geni_cancel_m_cmd(uport->membase);
  859. if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  860. M_CMD_CANCEL_EN, true)) {
  861. geni_abort_m_cmd(uport->membase);
  862. msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
  863. M_CMD_ABORT_EN, true);
  864. geni_write_reg_nolog(M_CMD_ABORT_EN, uport->membase,
  865. SE_GENI_M_IRQ_CLEAR);
  866. }
  867. geni_write_reg_nolog(M_CMD_CANCEL_EN, uport, SE_GENI_M_IRQ_CLEAR);
  868. /*
  869. * If we end up having to cancel an on-going Tx for non-console usecase
  870. * then it means there was some unsent data in the Tx FIFO, consequently
  871. * it means that there is a vote imbalance as we put in a vote during
  872. * start_tx() that is removed only as part of a "done" ISR. To balance
  873. * this out, remove the vote put in during start_tx().
  874. */
  875. if (!uart_console(uport)) {
  876. IPC_LOG_MSG(port->ipc_log_misc, "%s:Removing vote\n", __func__);
  877. msm_geni_serial_power_off(uport);
  878. }
  879. IPC_LOG_MSG(port->ipc_log_misc, "%s:\n", __func__);
  880. }
  881. static void msm_geni_serial_stop_tx(struct uart_port *uport)
  882. {
  883. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  884. if (!uart_console(uport) && device_pending_suspend(uport)) {
  885. dev_err(uport->dev, "%s.Device is suspended.\n", __func__);
  886. IPC_LOG_MSG(port->ipc_log_misc,
  887. "%s.Device is suspended.\n", __func__);
  888. return;
  889. }
  890. stop_tx_sequencer(uport);
  891. }
  892. static void start_rx_sequencer(struct uart_port *uport)
  893. {
  894. unsigned int geni_s_irq_en;
  895. unsigned int geni_m_irq_en;
  896. unsigned int geni_status;
  897. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  898. int ret;
  899. u32 geni_se_param = UART_PARAM_RFR_OPEN;
  900. geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
  901. if (geni_status & S_GENI_CMD_ACTIVE)
  902. msm_geni_serial_stop_rx(uport);
  903. /* Start RX with the RFR_OPEN to keep RFR in always ready state */
  904. geni_setup_s_cmd(uport->membase, UART_START_READ, geni_se_param);
  905. if (port->xfer_mode == FIFO_MODE) {
  906. geni_s_irq_en = geni_read_reg_nolog(uport->membase,
  907. SE_GENI_S_IRQ_EN);
  908. geni_m_irq_en = geni_read_reg_nolog(uport->membase,
  909. SE_GENI_M_IRQ_EN);
  910. geni_s_irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
  911. geni_m_irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
  912. geni_write_reg_nolog(geni_s_irq_en, uport->membase,
  913. SE_GENI_S_IRQ_EN);
  914. geni_write_reg_nolog(geni_m_irq_en, uport->membase,
  915. SE_GENI_M_IRQ_EN);
  916. } else if (port->xfer_mode == SE_DMA) {
  917. ret = geni_se_rx_dma_prep(port->wrapper_dev, uport->membase,
  918. port->rx_buf, DMA_RX_BUF_SIZE, &port->rx_dma);
  919. if (ret) {
  920. dev_err(uport->dev, "%s: RX Prep dma failed %d\n",
  921. __func__, ret);
  922. msm_geni_serial_stop_rx(uport);
  923. goto exit_start_rx_sequencer;
  924. }
  925. }
  926. /*
  927. * Ensure the writes to the secondary sequencer and interrupt enables
  928. * go through.
  929. */
  930. mb();
  931. geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
  932. exit_start_rx_sequencer:
  933. IPC_LOG_MSG(port->ipc_log_misc, "%s 0x%x\n", __func__, geni_status);
  934. }
  935. static void msm_geni_serial_start_rx(struct uart_port *uport)
  936. {
  937. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  938. if (!uart_console(uport) && device_pending_suspend(uport)) {
  939. dev_err(uport->dev, "%s.Device is suspended.\n", __func__);
  940. IPC_LOG_MSG(port->ipc_log_misc,
  941. "%s.Device is suspended.\n", __func__);
  942. return;
  943. }
  944. start_rx_sequencer(&port->uport);
  945. }
  946. static void msm_geni_serial_rx_fsm_rst(struct uart_port *uport)
  947. {
  948. unsigned int rx_irq_en;
  949. int done = 0;
  950. int tries = 0;
  951. rx_irq_en = geni_read_reg_nolog(uport->membase, SE_DMA_RX_IRQ_EN);
  952. geni_write_reg_nolog(0, uport->membase, SE_DMA_RX_IRQ_EN_SET);
  953. geni_write_reg_nolog(1, uport->membase, SE_DMA_RX_FSM_RST);
  954. do {
  955. done = msm_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
  956. RX_RESET_DONE, true);
  957. tries++;
  958. } while (!done && tries < 5);
  959. geni_write_reg_nolog(RX_DMA_DONE | RX_RESET_DONE, uport->membase,
  960. SE_DMA_RX_IRQ_CLR);
  961. geni_write_reg_nolog(rx_irq_en, uport->membase, SE_DMA_RX_IRQ_EN_SET);
  962. }
  963. static void stop_rx_sequencer(struct uart_port *uport)
  964. {
  965. unsigned int geni_s_irq_en;
  966. unsigned int geni_m_irq_en;
  967. unsigned int geni_status;
  968. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  969. u32 irq_clear = S_CMD_CANCEL_EN;
  970. bool done;
  971. IPC_LOG_MSG(port->ipc_log_misc, "%s\n", __func__);
  972. if (port->xfer_mode == FIFO_MODE) {
  973. geni_s_irq_en = geni_read_reg_nolog(uport->membase,
  974. SE_GENI_S_IRQ_EN);
  975. geni_m_irq_en = geni_read_reg_nolog(uport->membase,
  976. SE_GENI_M_IRQ_EN);
  977. geni_s_irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
  978. geni_m_irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
  979. geni_write_reg_nolog(geni_s_irq_en, uport->membase,
  980. SE_GENI_S_IRQ_EN);
  981. geni_write_reg_nolog(geni_m_irq_en, uport->membase,
  982. SE_GENI_M_IRQ_EN);
  983. }
  984. geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
  985. /* Possible stop rx is called multiple times. */
  986. if (!(geni_status & S_GENI_CMD_ACTIVE))
  987. goto exit_rx_seq;
  988. geni_cancel_s_cmd(uport->membase);
  989. /*
  990. * Ensure that the cancel goes through before polling for the
  991. * cancel control bit.
  992. */
  993. mb();
  994. if (!uart_console(uport))
  995. msm_geni_serial_complete_rx_eot(uport);
  996. done = msm_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
  997. S_GENI_CMD_CANCEL, false);
  998. if (done) {
  999. geni_write_reg_nolog(irq_clear, uport->membase,
  1000. SE_GENI_S_IRQ_CLEAR);
  1001. goto exit_rx_seq;
  1002. } else {
  1003. IPC_LOG_MSG(port->ipc_log_misc, "%s Cancel fail 0x%x\n",
  1004. __func__, geni_status);
  1005. }
  1006. geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
  1007. if ((geni_status & S_GENI_CMD_ACTIVE)) {
  1008. IPC_LOG_MSG(port->ipc_log_misc, "%s:Abort Rx, GENI:0x%x\n",
  1009. __func__, geni_status);
  1010. msm_geni_serial_abort_rx(uport);
  1011. }
  1012. exit_rx_seq:
  1013. if (port->xfer_mode == SE_DMA && port->rx_dma) {
  1014. msm_geni_serial_rx_fsm_rst(uport);
  1015. geni_se_rx_dma_unprep(port->wrapper_dev, port->rx_dma,
  1016. DMA_RX_BUF_SIZE);
  1017. port->rx_dma = (dma_addr_t)NULL;
  1018. }
  1019. }
  1020. static void msm_geni_serial_stop_rx(struct uart_port *uport)
  1021. {
  1022. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  1023. if (!uart_console(uport) && device_pending_suspend(uport)) {
  1024. IPC_LOG_MSG(port->ipc_log_misc,
  1025. "%s.Device is suspended.\n", __func__);
  1026. return;
  1027. }
  1028. stop_rx_sequencer(uport);
  1029. }
  1030. static int handle_rx_hs(struct uart_port *uport,
  1031. unsigned int rx_fifo_wc,
  1032. unsigned int rx_last_byte_valid,
  1033. unsigned int rx_last,
  1034. bool drop_rx)
  1035. {
  1036. unsigned char *rx_char;
  1037. struct tty_port *tport;
  1038. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1039. int ret;
  1040. int rx_bytes = 0;
  1041. rx_bytes = (msm_port->tx_fifo_width * (rx_fifo_wc - 1)) >> 3;
  1042. rx_bytes += ((rx_last && rx_last_byte_valid) ?
  1043. rx_last_byte_valid : msm_port->tx_fifo_width >> 3);
  1044. tport = &uport->state->port;
  1045. ioread32_rep((uport->membase + SE_GENI_RX_FIFOn), msm_port->rx_fifo,
  1046. rx_fifo_wc);
  1047. if (drop_rx)
  1048. return 0;
  1049. rx_char = (unsigned char *)msm_port->rx_fifo;
  1050. ret = tty_insert_flip_string(tport, rx_char, rx_bytes);
  1051. if (ret != rx_bytes) {
  1052. dev_err(uport->dev, "%s: ret %d rx_bytes %d\n", __func__,
  1053. ret, rx_bytes);
  1054. WARN_ON(1);
  1055. }
  1056. uport->icount.rx += ret;
  1057. tty_flip_buffer_push(tport);
  1058. dump_ipc(msm_port->ipc_log_rx, "Rx", (char *)msm_port->rx_fifo, 0,
  1059. rx_bytes);
  1060. return ret;
  1061. }
  1062. static int msm_geni_serial_handle_rx(struct uart_port *uport, bool drop_rx)
  1063. {
  1064. int ret = 0;
  1065. unsigned int rx_fifo_status;
  1066. unsigned int rx_fifo_wc = 0;
  1067. unsigned int rx_last_byte_valid = 0;
  1068. unsigned int rx_last = 0;
  1069. struct tty_port *tport;
  1070. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  1071. tport = &uport->state->port;
  1072. rx_fifo_status = geni_read_reg_nolog(uport->membase,
  1073. SE_GENI_RX_FIFO_STATUS);
  1074. rx_fifo_wc = rx_fifo_status & RX_FIFO_WC_MSK;
  1075. rx_last_byte_valid = ((rx_fifo_status & RX_LAST_BYTE_VALID_MSK) >>
  1076. RX_LAST_BYTE_VALID_SHFT);
  1077. rx_last = rx_fifo_status & RX_LAST;
  1078. if (rx_fifo_wc)
  1079. port->handle_rx(uport, rx_fifo_wc, rx_last_byte_valid,
  1080. rx_last, drop_rx);
  1081. return ret;
  1082. }
  1083. static int msm_geni_serial_handle_tx(struct uart_port *uport)
  1084. {
  1085. int ret = 0;
  1086. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1087. struct circ_buf *xmit = &uport->state->xmit;
  1088. unsigned int avail_fifo_bytes = 0;
  1089. unsigned int bytes_remaining = 0;
  1090. int i = 0;
  1091. unsigned int tx_fifo_status;
  1092. unsigned int xmit_size;
  1093. unsigned int fifo_width_bytes =
  1094. (uart_console(uport) ? 1 : (msm_port->tx_fifo_width >> 3));
  1095. int temp_tail = 0;
  1096. xmit_size = uart_circ_chars_pending(xmit);
  1097. tx_fifo_status = geni_read_reg_nolog(uport->membase,
  1098. SE_GENI_TX_FIFO_STATUS);
  1099. /* Both FIFO and framework buffer are drained */
  1100. if (!xmit_size && !tx_fifo_status) {
  1101. msm_geni_serial_stop_tx(uport);
  1102. goto exit_handle_tx;
  1103. }
  1104. avail_fifo_bytes = (msm_port->tx_fifo_depth - msm_port->tx_wm) *
  1105. fifo_width_bytes;
  1106. temp_tail = xmit->tail & (UART_XMIT_SIZE - 1);
  1107. if (xmit_size > (UART_XMIT_SIZE - temp_tail))
  1108. xmit_size = (UART_XMIT_SIZE - temp_tail);
  1109. if (xmit_size > avail_fifo_bytes)
  1110. xmit_size = avail_fifo_bytes;
  1111. if (!xmit_size)
  1112. goto exit_handle_tx;
  1113. msm_geni_serial_setup_tx(uport, xmit_size);
  1114. bytes_remaining = xmit_size;
  1115. while (i < xmit_size) {
  1116. unsigned int tx_bytes;
  1117. unsigned int buf = 0;
  1118. int c;
  1119. tx_bytes = ((bytes_remaining < fifo_width_bytes) ?
  1120. bytes_remaining : fifo_width_bytes);
  1121. for (c = 0; c < tx_bytes ; c++)
  1122. buf |= (xmit->buf[temp_tail + c] << (c * 8));
  1123. geni_write_reg_nolog(buf, uport->membase, SE_GENI_TX_FIFOn);
  1124. i += tx_bytes;
  1125. bytes_remaining -= tx_bytes;
  1126. uport->icount.tx += tx_bytes;
  1127. temp_tail += tx_bytes;
  1128. /* Ensure FIFO write goes through */
  1129. wmb();
  1130. }
  1131. xmit->tail = temp_tail & (UART_XMIT_SIZE - 1);
  1132. if (uart_console(uport))
  1133. msm_geni_serial_poll_cancel_tx(uport);
  1134. exit_handle_tx:
  1135. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1136. uart_write_wakeup(uport);
  1137. return ret;
  1138. }
  1139. static int msm_geni_serial_handle_dma_rx(struct uart_port *uport, bool drop_rx)
  1140. {
  1141. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1142. unsigned int rx_bytes = 0;
  1143. struct tty_port *tport;
  1144. int ret;
  1145. unsigned int geni_status;
  1146. geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
  1147. /* Possible stop rx is called */
  1148. if (!(geni_status & S_GENI_CMD_ACTIVE))
  1149. return 0;
  1150. geni_se_rx_dma_unprep(msm_port->wrapper_dev, msm_port->rx_dma,
  1151. DMA_RX_BUF_SIZE);
  1152. msm_port->rx_dma = (dma_addr_t)NULL;
  1153. rx_bytes = geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN);
  1154. if (unlikely(!msm_port->rx_buf)) {
  1155. IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: NULL Rx_buf\n",
  1156. __func__);
  1157. return 0;
  1158. }
  1159. if (unlikely(!rx_bytes)) {
  1160. IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: Size %d\n",
  1161. __func__, rx_bytes);
  1162. goto exit_handle_dma_rx;
  1163. }
  1164. if (drop_rx)
  1165. goto exit_handle_dma_rx;
  1166. tport = &uport->state->port;
  1167. ret = tty_insert_flip_string(tport, (unsigned char *)(msm_port->rx_buf),
  1168. rx_bytes);
  1169. if (ret != rx_bytes) {
  1170. dev_err(uport->dev, "%s: ret %d rx_bytes %d\n", __func__,
  1171. ret, rx_bytes);
  1172. WARN_ON(1);
  1173. }
  1174. uport->icount.rx += ret;
  1175. tty_flip_buffer_push(tport);
  1176. dump_ipc(msm_port->ipc_log_rx, "DMA Rx", (char *)msm_port->rx_buf, 0,
  1177. rx_bytes);
  1178. exit_handle_dma_rx:
  1179. ret = geni_se_rx_dma_prep(msm_port->wrapper_dev, uport->membase,
  1180. msm_port->rx_buf, DMA_RX_BUF_SIZE, &msm_port->rx_dma);
  1181. if (ret)
  1182. IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: %d\n", __func__, ret);
  1183. return ret;
  1184. }
  1185. static int msm_geni_serial_handle_dma_tx(struct uart_port *uport)
  1186. {
  1187. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1188. struct circ_buf *xmit = &uport->state->xmit;
  1189. xmit->tail = (xmit->tail + msm_port->xmit_size) & (UART_XMIT_SIZE - 1);
  1190. geni_se_tx_dma_unprep(msm_port->wrapper_dev, msm_port->tx_dma,
  1191. msm_port->xmit_size);
  1192. uport->icount.tx += msm_port->xmit_size;
  1193. msm_port->tx_dma = (dma_addr_t)NULL;
  1194. msm_port->xmit_size = 0;
  1195. if (!uart_circ_empty(xmit))
  1196. msm_geni_serial_prep_dma_tx(uport);
  1197. else {
  1198. /*
  1199. * This will balance out the power vote put in during start_tx
  1200. * allowing the device to suspend.
  1201. */
  1202. if (!uart_console(uport)) {
  1203. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1204. "%s.Power Off.\n", __func__);
  1205. msm_geni_serial_power_off(uport);
  1206. }
  1207. uart_write_wakeup(uport);
  1208. }
  1209. return 0;
  1210. }
  1211. static irqreturn_t msm_geni_serial_isr(int isr, void *dev)
  1212. {
  1213. unsigned int m_irq_status;
  1214. unsigned int s_irq_status;
  1215. unsigned int dma;
  1216. unsigned int dma_tx_status;
  1217. unsigned int dma_rx_status;
  1218. struct uart_port *uport = dev;
  1219. unsigned long flags;
  1220. unsigned int m_irq_en;
  1221. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1222. struct tty_port *tport = &uport->state->port;
  1223. bool drop_rx = false;
  1224. spin_lock_irqsave(&uport->lock, flags);
  1225. if (uart_console(uport) && uport->suspended)
  1226. goto exit_geni_serial_isr;
  1227. if (!uart_console(uport) && pm_runtime_status_suspended(uport->dev)) {
  1228. dev_err(uport->dev, "%s.Device is suspended.\n", __func__);
  1229. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1230. "%s.Device is suspended.\n", __func__);
  1231. goto exit_geni_serial_isr;
  1232. }
  1233. m_irq_status = geni_read_reg_nolog(uport->membase,
  1234. SE_GENI_M_IRQ_STATUS);
  1235. s_irq_status = geni_read_reg_nolog(uport->membase,
  1236. SE_GENI_S_IRQ_STATUS);
  1237. m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN);
  1238. dma = geni_read_reg_nolog(uport->membase, SE_GENI_DMA_MODE_EN);
  1239. dma_tx_status = geni_read_reg_nolog(uport->membase, SE_DMA_TX_IRQ_STAT);
  1240. dma_rx_status = geni_read_reg_nolog(uport->membase, SE_DMA_RX_IRQ_STAT);
  1241. geni_write_reg_nolog(m_irq_status, uport->membase, SE_GENI_M_IRQ_CLEAR);
  1242. geni_write_reg_nolog(s_irq_status, uport->membase, SE_GENI_S_IRQ_CLEAR);
  1243. if ((m_irq_status & M_ILLEGAL_CMD_EN)) {
  1244. WARN_ON(1);
  1245. goto exit_geni_serial_isr;
  1246. }
  1247. if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
  1248. uport->icount.overrun++;
  1249. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  1250. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1251. "%s.sirq 0x%x buf_overrun:%d\n",
  1252. __func__, s_irq_status, uport->icount.buf_overrun);
  1253. }
  1254. if (!dma) {
  1255. if ((m_irq_status & m_irq_en) &
  1256. (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
  1257. msm_geni_serial_handle_tx(uport);
  1258. if ((s_irq_status & S_GP_IRQ_0_EN) ||
  1259. (s_irq_status & S_GP_IRQ_1_EN)) {
  1260. if (s_irq_status & S_GP_IRQ_0_EN)
  1261. uport->icount.parity++;
  1262. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1263. "%s.sirq 0x%x parity:%d\n",
  1264. __func__, s_irq_status, uport->icount.parity);
  1265. drop_rx = true;
  1266. } else if ((s_irq_status & S_GP_IRQ_2_EN) ||
  1267. (s_irq_status & S_GP_IRQ_3_EN)) {
  1268. uport->icount.brk++;
  1269. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1270. "%s.sirq 0x%x break:%d\n",
  1271. __func__, s_irq_status, uport->icount.brk);
  1272. }
  1273. if ((s_irq_status & S_RX_FIFO_WATERMARK_EN) ||
  1274. (s_irq_status & S_RX_FIFO_LAST_EN))
  1275. msm_geni_serial_handle_rx(uport, drop_rx);
  1276. } else {
  1277. if (dma_tx_status) {
  1278. geni_write_reg_nolog(dma_tx_status, uport->membase,
  1279. SE_DMA_TX_IRQ_CLR);
  1280. if (dma_tx_status & TX_DMA_DONE)
  1281. msm_geni_serial_handle_dma_tx(uport);
  1282. }
  1283. if (dma_rx_status) {
  1284. geni_write_reg_nolog(dma_rx_status, uport->membase,
  1285. SE_DMA_RX_IRQ_CLR);
  1286. if (dma_rx_status & RX_RESET_DONE) {
  1287. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1288. "%s.Reset done. 0x%x.\n",
  1289. __func__, dma_rx_status);
  1290. goto exit_geni_serial_isr;
  1291. }
  1292. if (dma_rx_status & UART_DMA_RX_ERRS) {
  1293. if (dma_rx_status & UART_DMA_RX_PARITY_ERR)
  1294. uport->icount.parity++;
  1295. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1296. "%s.Rx Errors. 0x%x parity:%d\n",
  1297. __func__, dma_rx_status,
  1298. uport->icount.parity);
  1299. drop_rx = true;
  1300. } else if (dma_rx_status & UART_DMA_RX_BREAK) {
  1301. uport->icount.brk++;
  1302. IPC_LOG_MSG(msm_port->ipc_log_misc,
  1303. "%s.Rx Errors. 0x%x break:%d\n",
  1304. __func__, dma_rx_status,
  1305. uport->icount.brk);
  1306. }
  1307. if (dma_rx_status & RX_DMA_DONE)
  1308. msm_geni_serial_handle_dma_rx(uport, drop_rx);
  1309. }
  1310. }
  1311. exit_geni_serial_isr:
  1312. spin_unlock_irqrestore(&uport->lock, flags);
  1313. return IRQ_HANDLED;
  1314. }
  1315. static irqreturn_t msm_geni_wakeup_isr(int isr, void *dev)
  1316. {
  1317. struct uart_port *uport = dev;
  1318. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  1319. struct tty_struct *tty;
  1320. unsigned long flags;
  1321. spin_lock_irqsave(&uport->lock, flags);
  1322. IPC_LOG_MSG(port->ipc_log_rx, "%s: Edge-Count %d\n", __func__,
  1323. port->edge_count);
  1324. if (port->wakeup_byte && (port->edge_count == 2)) {
  1325. tty = uport->state->port.tty;
  1326. tty_insert_flip_char(tty->port, port->wakeup_byte, TTY_NORMAL);
  1327. IPC_LOG_MSG(port->ipc_log_rx, "%s: Inject 0x%x\n",
  1328. __func__, port->wakeup_byte);
  1329. port->edge_count = 0;
  1330. tty_flip_buffer_push(tty->port);
  1331. __pm_wakeup_event(&port->geni_wake, WAKEBYTE_TIMEOUT_MSEC);
  1332. } else if (port->edge_count < 2) {
  1333. port->edge_count++;
  1334. }
  1335. spin_unlock_irqrestore(&uport->lock, flags);
  1336. return IRQ_HANDLED;
  1337. }
  1338. static int get_tx_fifo_size(struct msm_geni_serial_port *port)
  1339. {
  1340. struct uart_port *uport;
  1341. if (!port)
  1342. return -ENODEV;
  1343. uport = &port->uport;
  1344. port->tx_fifo_depth = get_tx_fifo_depth(uport->membase);
  1345. if (!port->tx_fifo_depth) {
  1346. dev_err(uport->dev, "%s:Invalid TX FIFO depth read\n",
  1347. __func__);
  1348. return -ENXIO;
  1349. }
  1350. port->tx_fifo_width = get_tx_fifo_width(uport->membase);
  1351. if (!port->tx_fifo_width) {
  1352. dev_err(uport->dev, "%s:Invalid TX FIFO width read\n",
  1353. __func__);
  1354. return -ENXIO;
  1355. }
  1356. port->rx_fifo_depth = get_rx_fifo_depth(uport->membase);
  1357. if (!port->rx_fifo_depth) {
  1358. dev_err(uport->dev, "%s:Invalid RX FIFO depth read\n",
  1359. __func__);
  1360. return -ENXIO;
  1361. }
  1362. uport->fifosize =
  1363. ((port->tx_fifo_depth * port->tx_fifo_width) >> 3);
  1364. return 0;
  1365. }
  1366. static void set_rfr_wm(struct msm_geni_serial_port *port)
  1367. {
  1368. /*
  1369. * Set RFR (Flow off) to FIFO_DEPTH - 2.
  1370. * RX WM level at 50% RX_FIFO_DEPTH.
  1371. * TX WM level at 10% TX_FIFO_DEPTH.
  1372. */
  1373. port->rx_rfr = port->rx_fifo_depth - 2;
  1374. if (!uart_console(&port->uport))
  1375. port->rx_wm = port->rx_fifo_depth >> 1;
  1376. else
  1377. port->rx_wm = UART_CONSOLE_RX_WM;
  1378. port->tx_wm = 2;
  1379. }
  1380. static void msm_geni_serial_shutdown(struct uart_port *uport)
  1381. {
  1382. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1383. unsigned long flags;
  1384. /* Stop the console before stopping the current tx */
  1385. if (uart_console(uport)) {
  1386. console_stop(uport->cons);
  1387. } else {
  1388. msm_geni_serial_power_on(uport);
  1389. wait_for_transfers_inflight(uport);
  1390. }
  1391. disable_irq(uport->irq);
  1392. free_irq(uport->irq, uport);
  1393. spin_lock_irqsave(&uport->lock, flags);
  1394. msm_geni_serial_stop_tx(uport);
  1395. msm_geni_serial_stop_rx(uport);
  1396. spin_unlock_irqrestore(&uport->lock, flags);
  1397. if (!uart_console(uport)) {
  1398. if (msm_port->ioctl_count) {
  1399. int i;
  1400. for (i = 0; i < msm_port->ioctl_count; i++) {
  1401. IPC_LOG_MSG(msm_port->ipc_log_pwr,
  1402. "%s IOCTL vote present. Forcing off\n",
  1403. __func__);
  1404. msm_geni_serial_power_off(uport);
  1405. }
  1406. msm_port->ioctl_count = 0;
  1407. }
  1408. msm_geni_serial_power_off(uport);
  1409. if (msm_port->wakeup_irq > 0) {
  1410. irq_set_irq_wake(msm_port->wakeup_irq, 0);
  1411. disable_irq(msm_port->wakeup_irq);
  1412. free_irq(msm_port->wakeup_irq, uport);
  1413. }
  1414. }
  1415. IPC_LOG_MSG(msm_port->ipc_log_misc, "%s\n", __func__);
  1416. }
  1417. static int msm_geni_serial_port_setup(struct uart_port *uport)
  1418. {
  1419. int ret = 0;
  1420. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1421. unsigned long cfg0, cfg1;
  1422. unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
  1423. set_rfr_wm(msm_port);
  1424. geni_write_reg_nolog(rxstale, uport->membase, SE_UART_RX_STALE_CNT);
  1425. if (!uart_console(uport)) {
  1426. /* For now only assume FIFO mode. */
  1427. msm_port->xfer_mode = SE_DMA;
  1428. se_get_packing_config(8, 4, false, &cfg0, &cfg1);
  1429. geni_write_reg_nolog(cfg0, uport->membase,
  1430. SE_GENI_TX_PACKING_CFG0);
  1431. geni_write_reg_nolog(cfg1, uport->membase,
  1432. SE_GENI_TX_PACKING_CFG1);
  1433. geni_write_reg_nolog(cfg0, uport->membase,
  1434. SE_GENI_RX_PACKING_CFG0);
  1435. geni_write_reg_nolog(cfg1, uport->membase,
  1436. SE_GENI_RX_PACKING_CFG1);
  1437. msm_port->handle_rx = handle_rx_hs;
  1438. msm_port->rx_fifo = devm_kzalloc(uport->dev,
  1439. sizeof(msm_port->rx_fifo_depth * sizeof(u32)),
  1440. GFP_KERNEL);
  1441. if (!msm_port->rx_fifo) {
  1442. ret = -ENOMEM;
  1443. goto exit_portsetup;
  1444. }
  1445. msm_port->rx_buf = devm_kzalloc(uport->dev, DMA_RX_BUF_SIZE,
  1446. GFP_KERNEL);
  1447. if (!msm_port->rx_buf) {
  1448. devm_kfree(uport->dev, msm_port->rx_fifo);
  1449. msm_port->rx_fifo = NULL;
  1450. ret = -ENOMEM;
  1451. goto exit_portsetup;
  1452. }
  1453. } else {
  1454. /*
  1455. * Make an unconditional cancel on the main sequencer to reset
  1456. * it else we could end up in data loss scenarios.
  1457. */
  1458. msm_port->xfer_mode = FIFO_MODE;
  1459. msm_geni_serial_poll_cancel_tx(uport);
  1460. se_get_packing_config(8, 1, false, &cfg0, &cfg1);
  1461. geni_write_reg_nolog(cfg0, uport->membase,
  1462. SE_GENI_TX_PACKING_CFG0);
  1463. geni_write_reg_nolog(cfg1, uport->membase,
  1464. SE_GENI_TX_PACKING_CFG1);
  1465. se_get_packing_config(8, 4, false, &cfg0, &cfg1);
  1466. geni_write_reg_nolog(cfg0, uport->membase,
  1467. SE_GENI_RX_PACKING_CFG0);
  1468. geni_write_reg_nolog(cfg1, uport->membase,
  1469. SE_GENI_RX_PACKING_CFG1);
  1470. }
  1471. ret = geni_se_init(uport->membase, msm_port->rx_wm, msm_port->rx_rfr);
  1472. if (ret) {
  1473. dev_err(uport->dev, "%s: Fail\n", __func__);
  1474. goto exit_portsetup;
  1475. }
  1476. ret = geni_se_select_mode(uport->membase, msm_port->xfer_mode);
  1477. if (ret)
  1478. goto exit_portsetup;
  1479. msm_port->port_setup = true;
  1480. /*
  1481. * Ensure Port setup related IO completes before returning to
  1482. * framework.
  1483. */
  1484. mb();
  1485. exit_portsetup:
  1486. return ret;
  1487. }
  1488. static int msm_geni_serial_startup(struct uart_port *uport)
  1489. {
  1490. int ret = 0;
  1491. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1492. scnprintf(msm_port->name, sizeof(msm_port->name), "msm_serial_geni%d",
  1493. uport->line);
  1494. if (likely(!uart_console(uport))) {
  1495. ret = msm_geni_serial_power_on(&msm_port->uport);
  1496. if (ret) {
  1497. dev_err(uport->dev, "%s:Failed to power on %d\n",
  1498. __func__, ret);
  1499. return ret;
  1500. }
  1501. }
  1502. if (unlikely(get_se_proto(uport->membase) != UART)) {
  1503. dev_err(uport->dev, "%s: Invalid FW %d loaded.\n",
  1504. __func__, get_se_proto(uport->membase));
  1505. ret = -ENXIO;
  1506. goto exit_startup;
  1507. }
  1508. IPC_LOG_MSG(msm_port->ipc_log_misc, "%s: FW Ver:0x%x%x\n",
  1509. __func__,
  1510. get_se_m_fw(uport->membase), get_se_s_fw(uport->membase));
  1511. get_tx_fifo_size(msm_port);
  1512. if (!msm_port->port_setup) {
  1513. if (msm_geni_serial_port_setup(uport))
  1514. goto exit_startup;
  1515. }
  1516. /*
  1517. * Ensure that all the port configuration writes complete
  1518. * before returning to the framework.
  1519. */
  1520. mb();
  1521. ret = request_irq(uport->irq, msm_geni_serial_isr, IRQF_TRIGGER_HIGH,
  1522. msm_port->name, uport);
  1523. if (unlikely(ret)) {
  1524. dev_err(uport->dev, "%s: Failed to get IRQ ret %d\n",
  1525. __func__, ret);
  1526. goto exit_startup;
  1527. }
  1528. if (msm_port->wakeup_irq > 0) {
  1529. ret = request_irq(msm_port->wakeup_irq, msm_geni_wakeup_isr,
  1530. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1531. "hs_uart_wakeup", uport);
  1532. if (unlikely(ret)) {
  1533. dev_err(uport->dev, "%s:Failed to get WakeIRQ ret%d\n",
  1534. __func__, ret);
  1535. goto exit_startup;
  1536. }
  1537. disable_irq(msm_port->wakeup_irq);
  1538. ret = irq_set_irq_wake(msm_port->wakeup_irq, 1);
  1539. if (unlikely(ret)) {
  1540. dev_err(uport->dev, "%s:Failed to set IRQ wake:%d\n",
  1541. __func__, ret);
  1542. goto exit_startup;
  1543. }
  1544. }
  1545. IPC_LOG_MSG(msm_port->ipc_log_misc, "%s\n", __func__);
  1546. exit_startup:
  1547. if (likely(!uart_console(uport)))
  1548. msm_geni_serial_power_off(&msm_port->uport);
  1549. return ret;
  1550. }
  1551. static int get_clk_cfg(unsigned long clk_freq, unsigned long *ser_clk)
  1552. {
  1553. unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
  1554. 32000000, 48000000, 64000000, 80000000, 96000000, 100000000,
  1555. 102400000, 112000000, 120000000, 128000000};
  1556. int i;
  1557. int match = -1;
  1558. for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
  1559. if (clk_freq > root_freq[i])
  1560. continue;
  1561. if (!(root_freq[i] % clk_freq)) {
  1562. match = i;
  1563. break;
  1564. }
  1565. }
  1566. if (match != -1)
  1567. *ser_clk = root_freq[match];
  1568. else
  1569. pr_err("clk_freq %ld\n", clk_freq);
  1570. return match;
  1571. }
  1572. static void geni_serial_write_term_regs(struct uart_port *uport, u32 loopback,
  1573. u32 tx_trans_cfg, u32 tx_parity_cfg, u32 rx_trans_cfg,
  1574. u32 rx_parity_cfg, u32 bits_per_char, u32 stop_bit_len,
  1575. u32 s_clk_cfg)
  1576. {
  1577. geni_write_reg_nolog(loopback, uport->membase, SE_UART_LOOPBACK_CFG);
  1578. geni_write_reg_nolog(tx_trans_cfg, uport->membase,
  1579. SE_UART_TX_TRANS_CFG);
  1580. geni_write_reg_nolog(tx_parity_cfg, uport->membase,
  1581. SE_UART_TX_PARITY_CFG);
  1582. geni_write_reg_nolog(rx_trans_cfg, uport->membase,
  1583. SE_UART_RX_TRANS_CFG);
  1584. geni_write_reg_nolog(rx_parity_cfg, uport->membase,
  1585. SE_UART_RX_PARITY_CFG);
  1586. geni_write_reg_nolog(bits_per_char, uport->membase,
  1587. SE_UART_TX_WORD_LEN);
  1588. geni_write_reg_nolog(bits_per_char, uport->membase,
  1589. SE_UART_RX_WORD_LEN);
  1590. geni_write_reg_nolog(stop_bit_len, uport->membase,
  1591. SE_UART_TX_STOP_BIT_LEN);
  1592. geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_M_CLK_CFG);
  1593. geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_S_CLK_CFG);
  1594. }
  1595. static int get_clk_div_rate(unsigned int baud, unsigned long *desired_clk_rate)
  1596. {
  1597. unsigned long ser_clk;
  1598. int dfs_index;
  1599. int clk_div = 0;
  1600. *desired_clk_rate = baud * UART_OVERSAMPLING;
  1601. dfs_index = get_clk_cfg(*desired_clk_rate, &ser_clk);
  1602. if (dfs_index < 0) {
  1603. pr_err("%s: Can't find matching DFS entry for baud %d\n",
  1604. __func__, baud);
  1605. clk_div = -EINVAL;
  1606. goto exit_get_clk_div_rate;
  1607. }
  1608. clk_div = ser_clk / *desired_clk_rate;
  1609. *desired_clk_rate = ser_clk;
  1610. exit_get_clk_div_rate:
  1611. return clk_div;
  1612. }
  1613. static void msm_geni_serial_set_termios(struct uart_port *uport,
  1614. struct ktermios *termios, struct ktermios *old)
  1615. {
  1616. unsigned int baud;
  1617. unsigned int bits_per_char = 0;
  1618. unsigned int tx_trans_cfg;
  1619. unsigned int tx_parity_cfg;
  1620. unsigned int rx_trans_cfg;
  1621. unsigned int rx_parity_cfg;
  1622. unsigned int stop_bit_len;
  1623. unsigned int clk_div;
  1624. unsigned long ser_clk_cfg = 0;
  1625. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  1626. unsigned long clk_rate;
  1627. unsigned long flags;
  1628. if (!uart_console(uport)) {
  1629. int ret = msm_geni_serial_power_on(uport);
  1630. if (ret) {
  1631. IPC_LOG_MSG(port->ipc_log_misc,
  1632. "%s: Failed to vote clock on:%d\n",
  1633. __func__, ret);
  1634. return;
  1635. }
  1636. }
  1637. /* Take a spinlock else stop_rx causes a race with an ISR due to Cancel
  1638. * and FSM_RESET. This also has a potential race with the dma_map/unmap
  1639. * operations of ISR.
  1640. */
  1641. spin_lock_irqsave(&uport->lock, flags);
  1642. msm_geni_serial_stop_rx(uport);
  1643. spin_unlock_irqrestore(&uport->lock, flags);
  1644. /* baud rate */
  1645. baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
  1646. port->cur_baud = baud;
  1647. clk_div = get_clk_div_rate(baud, &clk_rate);
  1648. if (clk_div <= 0)
  1649. goto exit_set_termios;
  1650. uport->uartclk = clk_rate;
  1651. clk_set_rate(port->serial_rsc.se_clk, clk_rate);
  1652. ser_clk_cfg |= SER_CLK_EN;
  1653. ser_clk_cfg |= (clk_div << CLK_DIV_SHFT);
  1654. /* parity */
  1655. tx_trans_cfg = geni_read_reg_nolog(uport->membase,
  1656. SE_UART_TX_TRANS_CFG);
  1657. tx_parity_cfg = geni_read_reg_nolog(uport->membase,
  1658. SE_UART_TX_PARITY_CFG);
  1659. rx_trans_cfg = geni_read_reg_nolog(uport->membase,
  1660. SE_UART_RX_TRANS_CFG);
  1661. rx_parity_cfg = geni_read_reg_nolog(uport->membase,
  1662. SE_UART_RX_PARITY_CFG);
  1663. if (termios->c_cflag & PARENB) {
  1664. tx_trans_cfg |= UART_TX_PAR_EN;
  1665. rx_trans_cfg |= UART_RX_PAR_EN;
  1666. tx_parity_cfg |= PAR_CALC_EN;
  1667. rx_parity_cfg |= PAR_CALC_EN;
  1668. if (termios->c_cflag & PARODD) {
  1669. tx_parity_cfg |= PAR_ODD;
  1670. rx_parity_cfg |= PAR_ODD;
  1671. } else if (termios->c_cflag & CMSPAR) {
  1672. tx_parity_cfg |= PAR_SPACE;
  1673. rx_parity_cfg |= PAR_SPACE;
  1674. } else {
  1675. tx_parity_cfg |= PAR_EVEN;
  1676. rx_parity_cfg |= PAR_EVEN;
  1677. }
  1678. } else {
  1679. tx_trans_cfg &= ~UART_TX_PAR_EN;
  1680. rx_trans_cfg &= ~UART_RX_PAR_EN;
  1681. tx_parity_cfg &= ~PAR_CALC_EN;
  1682. rx_parity_cfg &= ~PAR_CALC_EN;
  1683. }
  1684. /* bits per char */
  1685. switch (termios->c_cflag & CSIZE) {
  1686. case CS5:
  1687. bits_per_char = 5;
  1688. break;
  1689. case CS6:
  1690. bits_per_char = 6;
  1691. break;
  1692. case CS7:
  1693. bits_per_char = 7;
  1694. break;
  1695. case CS8:
  1696. default:
  1697. bits_per_char = 8;
  1698. break;
  1699. }
  1700. uport->status &= ~(UPSTAT_AUTOCTS);
  1701. /* stop bits */
  1702. if (termios->c_cflag & CSTOPB)
  1703. stop_bit_len = TX_STOP_BIT_LEN_2;
  1704. else
  1705. stop_bit_len = TX_STOP_BIT_LEN_1;
  1706. /* flow control, clear the CTS_MASK bit if using flow control. */
  1707. if (termios->c_cflag & CRTSCTS) {
  1708. tx_trans_cfg &= ~UART_CTS_MASK;
  1709. uport->status |= UPSTAT_AUTOCTS;
  1710. }
  1711. else
  1712. tx_trans_cfg |= UART_CTS_MASK;
  1713. /* status bits to ignore */
  1714. if (likely(baud))
  1715. uart_update_timeout(uport, termios->c_cflag, baud);
  1716. geni_serial_write_term_regs(uport, port->loopback, tx_trans_cfg,
  1717. tx_parity_cfg, rx_trans_cfg, rx_parity_cfg, bits_per_char,
  1718. stop_bit_len, ser_clk_cfg);
  1719. IPC_LOG_MSG(port->ipc_log_misc, "%s: baud %d\n", __func__, baud);
  1720. IPC_LOG_MSG(port->ipc_log_misc, "Tx: trans_cfg%d parity %d\n",
  1721. tx_trans_cfg, tx_parity_cfg);
  1722. IPC_LOG_MSG(port->ipc_log_misc, "Rx: trans_cfg%d parity %d",
  1723. rx_trans_cfg, rx_parity_cfg);
  1724. IPC_LOG_MSG(port->ipc_log_misc, "BitsChar%d stop bit%d\n",
  1725. bits_per_char, stop_bit_len);
  1726. exit_set_termios:
  1727. msm_geni_serial_start_rx(uport);
  1728. if (!uart_console(uport))
  1729. msm_geni_serial_power_off(uport);
  1730. return;
  1731. }
  1732. static unsigned int msm_geni_serial_tx_empty(struct uart_port *uport)
  1733. {
  1734. unsigned int tx_fifo_status;
  1735. unsigned int is_tx_empty = 1;
  1736. struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
  1737. if (!uart_console(uport) && device_pending_suspend(uport))
  1738. return 1;
  1739. if (port->xfer_mode == SE_DMA)
  1740. tx_fifo_status = port->tx_dma ? 1 : 0;
  1741. else
  1742. tx_fifo_status = geni_read_reg_nolog(uport->membase,
  1743. SE_GENI_TX_FIFO_STATUS);
  1744. if (tx_fifo_status)
  1745. is_tx_empty = 0;
  1746. return is_tx_empty;
  1747. }
  1748. static ssize_t msm_geni_serial_xfer_mode_show(struct device *dev,
  1749. struct device_attribute *attr, char *buf)
  1750. {
  1751. struct platform_device *pdev = to_platform_device(dev);
  1752. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  1753. ssize_t ret = 0;
  1754. if (port->xfer_mode == FIFO_MODE)
  1755. ret = snprintf(buf, sizeof("FIFO\n"), "FIFO\n");
  1756. else if (port->xfer_mode == SE_DMA)
  1757. ret = snprintf(buf, sizeof("SE_DMA\n"), "SE_DMA\n");
  1758. return ret;
  1759. }
  1760. static ssize_t msm_geni_serial_xfer_mode_store(struct device *dev,
  1761. struct device_attribute *attr, const char *buf,
  1762. size_t size)
  1763. {
  1764. struct platform_device *pdev = to_platform_device(dev);
  1765. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  1766. struct uart_port *uport = &port->uport;
  1767. int xfer_mode = port->xfer_mode;
  1768. unsigned long flags;
  1769. if (uart_console(uport))
  1770. return -EOPNOTSUPP;
  1771. if (strnstr(buf, "FIFO", strlen("FIFO"))) {
  1772. xfer_mode = FIFO_MODE;
  1773. } else if (strnstr(buf, "SE_DMA", strlen("SE_DMA"))) {
  1774. xfer_mode = SE_DMA;
  1775. } else {
  1776. dev_err(dev, "%s: Invalid input %s\n", __func__, buf);
  1777. return -EINVAL;
  1778. }
  1779. if (xfer_mode == port->xfer_mode)
  1780. return size;
  1781. msm_geni_serial_power_on(uport);
  1782. spin_lock_irqsave(&uport->lock, flags);
  1783. msm_geni_serial_stop_tx(uport);
  1784. msm_geni_serial_stop_rx(uport);
  1785. port->xfer_mode = xfer_mode;
  1786. geni_se_select_mode(uport->membase, port->xfer_mode);
  1787. spin_unlock_irqrestore(&uport->lock, flags);
  1788. msm_geni_serial_start_rx(uport);
  1789. msm_geni_serial_power_off(uport);
  1790. return size;
  1791. }
  1792. static DEVICE_ATTR(xfer_mode, 0644, msm_geni_serial_xfer_mode_show,
  1793. msm_geni_serial_xfer_mode_store);
  1794. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  1795. static int __init msm_geni_console_setup(struct console *co, char *options)
  1796. {
  1797. struct uart_port *uport;
  1798. struct msm_geni_serial_port *dev_port;
  1799. int baud = 115200;
  1800. int bits = 8;
  1801. int parity = 'n';
  1802. int flow = 'n';
  1803. int ret = 0;
  1804. if (unlikely(co->index >= GENI_UART_NR_PORTS || co->index < 0))
  1805. return -ENXIO;
  1806. dev_port = get_port_from_line(co->index, true);
  1807. if (IS_ERR_OR_NULL(dev_port)) {
  1808. ret = PTR_ERR(dev_port);
  1809. pr_err("Invalid line %d(%d)\n", co->index, ret);
  1810. return ret;
  1811. }
  1812. uport = &dev_port->uport;
  1813. if (unlikely(!uport->membase))
  1814. return -ENXIO;
  1815. if (se_geni_resources_on(&dev_port->serial_rsc))
  1816. WARN_ON(1);
  1817. if (unlikely(get_se_proto(uport->membase) != UART)) {
  1818. se_geni_resources_off(&dev_port->serial_rsc);
  1819. return -ENXIO;
  1820. }
  1821. if (!dev_port->port_setup) {
  1822. msm_geni_serial_stop_rx(uport);
  1823. msm_geni_serial_port_setup(uport);
  1824. }
  1825. if (options)
  1826. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1827. return uart_set_options(uport, co, baud, parity, bits, flow);
  1828. }
  1829. static void
  1830. msm_geni_serial_early_console_write(struct console *con, const char *s,
  1831. unsigned n)
  1832. {
  1833. struct earlycon_device *dev = con->data;
  1834. __msm_geni_serial_console_write(&dev->port, s, n);
  1835. }
  1836. static int __init
  1837. msm_geni_serial_earlycon_setup(struct earlycon_device *dev,
  1838. const char *opt)
  1839. {
  1840. struct uart_port *uport = &dev->port;
  1841. int ret = 0;
  1842. u32 tx_trans_cfg = 0;
  1843. u32 tx_parity_cfg = 0;
  1844. u32 rx_trans_cfg = 0;
  1845. u32 rx_parity_cfg = 0;
  1846. u32 stop_bit = 0;
  1847. u32 rx_stale = 0;
  1848. u32 bits_per_char = 0;
  1849. u32 s_clk_cfg = 0;
  1850. u32 baud = 115200;
  1851. u32 clk_div;
  1852. unsigned long clk_rate;
  1853. unsigned long cfg0, cfg1;
  1854. if (!uport->membase) {
  1855. ret = -ENOMEM;
  1856. goto exit_geni_serial_earlyconsetup;
  1857. }
  1858. if (get_se_proto(uport->membase) != UART) {
  1859. ret = -ENXIO;
  1860. goto exit_geni_serial_earlyconsetup;
  1861. }
  1862. /*
  1863. * Ignore Flow control.
  1864. * Disable Tx Parity.
  1865. * Don't check Parity during Rx.
  1866. * Disable Rx Parity.
  1867. * n = 8.
  1868. * Stop bit = 0.
  1869. * Stale timeout in bit-time (3 chars worth).
  1870. */
  1871. tx_trans_cfg |= UART_CTS_MASK;
  1872. tx_parity_cfg = 0;
  1873. rx_trans_cfg = 0;
  1874. rx_parity_cfg = 0;
  1875. bits_per_char = 0x8;
  1876. stop_bit = 0;
  1877. rx_stale = 0x18;
  1878. clk_div = get_clk_div_rate(baud, &clk_rate);
  1879. if (clk_div <= 0) {
  1880. ret = -EINVAL;
  1881. goto exit_geni_serial_earlyconsetup;
  1882. }
  1883. s_clk_cfg |= SER_CLK_EN;
  1884. s_clk_cfg |= (clk_div << CLK_DIV_SHFT);
  1885. /*
  1886. * Make an unconditional cancel on the main sequencer to reset
  1887. * it else we could end up in data loss scenarios.
  1888. */
  1889. msm_geni_serial_poll_cancel_tx(uport);
  1890. msm_geni_serial_abort_rx(uport);
  1891. se_get_packing_config(8, 1, false, &cfg0, &cfg1);
  1892. geni_se_init(uport->membase, (DEF_FIFO_DEPTH_WORDS >> 1),
  1893. (DEF_FIFO_DEPTH_WORDS - 2));
  1894. geni_se_select_mode(uport->membase, FIFO_MODE);
  1895. geni_write_reg_nolog(cfg0, uport->membase, SE_GENI_TX_PACKING_CFG0);
  1896. geni_write_reg_nolog(cfg1, uport->membase, SE_GENI_TX_PACKING_CFG1);
  1897. geni_write_reg_nolog(tx_trans_cfg, uport->membase,
  1898. SE_UART_TX_TRANS_CFG);
  1899. geni_write_reg_nolog(tx_parity_cfg, uport->membase,
  1900. SE_UART_TX_PARITY_CFG);
  1901. geni_write_reg_nolog(rx_trans_cfg, uport->membase,
  1902. SE_UART_RX_TRANS_CFG);
  1903. geni_write_reg_nolog(rx_parity_cfg, uport->membase,
  1904. SE_UART_RX_PARITY_CFG);
  1905. geni_write_reg_nolog(bits_per_char, uport->membase,
  1906. SE_UART_TX_WORD_LEN);
  1907. geni_write_reg_nolog(bits_per_char, uport->membase,
  1908. SE_UART_RX_WORD_LEN);
  1909. geni_write_reg_nolog(stop_bit, uport->membase, SE_UART_TX_STOP_BIT_LEN);
  1910. geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_M_CLK_CFG);
  1911. geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_S_CLK_CFG);
  1912. dev->con->write = msm_geni_serial_early_console_write;
  1913. dev->con->setup = NULL;
  1914. /*
  1915. * Ensure that the early console setup completes before
  1916. * returning.
  1917. */
  1918. mb();
  1919. exit_geni_serial_earlyconsetup:
  1920. return ret;
  1921. }
  1922. OF_EARLYCON_DECLARE(msm_geni_serial, "qcom,msm-geni-console",
  1923. msm_geni_serial_earlycon_setup);
  1924. static int console_register(struct uart_driver *drv)
  1925. {
  1926. return uart_register_driver(drv);
  1927. }
  1928. static void console_unregister(struct uart_driver *drv)
  1929. {
  1930. uart_unregister_driver(drv);
  1931. }
  1932. static struct console cons_ops = {
  1933. .name = "ttyMSM",
  1934. .write = msm_geni_serial_console_write,
  1935. .device = uart_console_device,
  1936. .setup = msm_geni_console_setup,
  1937. .flags = CON_PRINTBUFFER,
  1938. .index = -1,
  1939. .data = &msm_geni_console_driver,
  1940. };
  1941. static struct uart_driver msm_geni_console_driver = {
  1942. .owner = THIS_MODULE,
  1943. .driver_name = "msm_geni_console",
  1944. .dev_name = "ttyMSM",
  1945. .nr = GENI_UART_NR_PORTS,
  1946. .cons = &cons_ops,
  1947. };
  1948. #else
  1949. static int console_register(struct uart_driver *drv)
  1950. {
  1951. return 0;
  1952. }
  1953. static void console_unregister(struct uart_driver *drv)
  1954. {
  1955. }
  1956. #endif /* defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) */
  1957. static void msm_geni_serial_debug_init(struct uart_port *uport, bool console)
  1958. {
  1959. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  1960. msm_port->dbg = debugfs_create_dir(dev_name(uport->dev), NULL);
  1961. if (IS_ERR_OR_NULL(msm_port->dbg))
  1962. dev_err(uport->dev, "Failed to create dbg dir\n");
  1963. if (!console) {
  1964. char name[30];
  1965. memset(name, 0, sizeof(name));
  1966. if (!msm_port->ipc_log_rx) {
  1967. scnprintf(name, sizeof(name), "%s%s",
  1968. dev_name(uport->dev), "_rx");
  1969. msm_port->ipc_log_rx = ipc_log_context_create(
  1970. IPC_LOG_TX_RX_PAGES, name, 0);
  1971. if (!msm_port->ipc_log_rx)
  1972. dev_info(uport->dev, "Err in Rx IPC Log\n");
  1973. }
  1974. memset(name, 0, sizeof(name));
  1975. if (!msm_port->ipc_log_tx) {
  1976. scnprintf(name, sizeof(name), "%s%s",
  1977. dev_name(uport->dev), "_tx");
  1978. msm_port->ipc_log_tx = ipc_log_context_create(
  1979. IPC_LOG_TX_RX_PAGES, name, 0);
  1980. if (!msm_port->ipc_log_tx)
  1981. dev_info(uport->dev, "Err in Tx IPC Log\n");
  1982. }
  1983. memset(name, 0, sizeof(name));
  1984. if (!msm_port->ipc_log_pwr) {
  1985. scnprintf(name, sizeof(name), "%s%s",
  1986. dev_name(uport->dev), "_pwr");
  1987. msm_port->ipc_log_pwr = ipc_log_context_create(
  1988. IPC_LOG_PWR_PAGES, name, 0);
  1989. if (!msm_port->ipc_log_pwr)
  1990. dev_info(uport->dev, "Err in Pwr IPC Log\n");
  1991. }
  1992. memset(name, 0, sizeof(name));
  1993. if (!msm_port->ipc_log_misc) {
  1994. scnprintf(name, sizeof(name), "%s%s",
  1995. dev_name(uport->dev), "_misc");
  1996. msm_port->ipc_log_misc = ipc_log_context_create(
  1997. IPC_LOG_MISC_PAGES, name, 0);
  1998. if (!msm_port->ipc_log_misc)
  1999. dev_info(uport->dev, "Err in Misc IPC Log\n");
  2000. }
  2001. }
  2002. }
  2003. static void msm_geni_serial_cons_pm(struct uart_port *uport,
  2004. unsigned int new_state, unsigned int old_state)
  2005. {
  2006. struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
  2007. if (unlikely(!uart_console(uport)))
  2008. return;
  2009. if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
  2010. se_geni_resources_on(&msm_port->serial_rsc);
  2011. else if (new_state == UART_PM_STATE_OFF &&
  2012. old_state == UART_PM_STATE_ON)
  2013. se_geni_resources_off(&msm_port->serial_rsc);
  2014. }
  2015. static const struct uart_ops msm_geni_console_pops = {
  2016. .tx_empty = msm_geni_serial_tx_empty,
  2017. .stop_tx = msm_geni_serial_stop_tx,
  2018. .start_tx = msm_geni_serial_start_tx,
  2019. .stop_rx = msm_geni_serial_stop_rx,
  2020. .set_termios = msm_geni_serial_set_termios,
  2021. .startup = msm_geni_serial_startup,
  2022. .config_port = msm_geni_serial_config_port,
  2023. .shutdown = msm_geni_serial_shutdown,
  2024. .type = msm_geni_serial_get_type,
  2025. .set_mctrl = msm_geni_cons_set_mctrl,
  2026. .get_mctrl = msm_geni_cons_get_mctrl,
  2027. #ifdef CONFIG_CONSOLE_POLL
  2028. .poll_get_char = msm_geni_serial_get_char,
  2029. .poll_put_char = msm_geni_serial_poll_put_char,
  2030. #endif
  2031. .pm = msm_geni_serial_cons_pm,
  2032. };
  2033. static const struct uart_ops msm_geni_serial_pops = {
  2034. .tx_empty = msm_geni_serial_tx_empty,
  2035. .stop_tx = msm_geni_serial_stop_tx,
  2036. .start_tx = msm_geni_serial_start_tx,
  2037. .stop_rx = msm_geni_serial_stop_rx,
  2038. .set_termios = msm_geni_serial_set_termios,
  2039. .startup = msm_geni_serial_startup,
  2040. .config_port = msm_geni_serial_config_port,
  2041. .shutdown = msm_geni_serial_shutdown,
  2042. .type = msm_geni_serial_get_type,
  2043. .set_mctrl = msm_geni_serial_set_mctrl,
  2044. .get_mctrl = msm_geni_serial_get_mctrl,
  2045. .break_ctl = msm_geni_serial_break_ctl,
  2046. .flush_buffer = NULL,
  2047. .ioctl = msm_geni_serial_ioctl,
  2048. };
  2049. static const struct of_device_id msm_geni_device_tbl[] = {
  2050. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  2051. { .compatible = "qcom,msm-geni-console",
  2052. .data = (void *)&msm_geni_console_driver},
  2053. #endif
  2054. { .compatible = "qcom,msm-geni-serial-hs",
  2055. .data = (void *)&msm_geni_serial_hs_driver},
  2056. {},
  2057. };
  2058. static int msm_geni_serial_probe(struct platform_device *pdev)
  2059. {
  2060. int ret = 0;
  2061. int line;
  2062. struct msm_geni_serial_port *dev_port;
  2063. struct uart_port *uport;
  2064. struct resource *res;
  2065. struct uart_driver *drv;
  2066. const struct of_device_id *id;
  2067. bool is_console = false;
  2068. struct platform_device *wrapper_pdev;
  2069. struct device_node *wrapper_ph_node;
  2070. u32 wake_char = 0;
  2071. id = of_match_device(msm_geni_device_tbl, &pdev->dev);
  2072. if (id) {
  2073. dev_dbg(&pdev->dev, "%s: %s\n", __func__, id->compatible);
  2074. drv = (struct uart_driver *)id->data;
  2075. } else {
  2076. dev_err(&pdev->dev, "%s: No matching device found", __func__);
  2077. return -ENODEV;
  2078. }
  2079. if (pdev->dev.of_node) {
  2080. if (drv->cons)
  2081. line = of_alias_get_id(pdev->dev.of_node, "serial");
  2082. else
  2083. line = of_alias_get_id(pdev->dev.of_node, "hsuart");
  2084. } else {
  2085. line = pdev->id;
  2086. }
  2087. if (line < 0)
  2088. line = atomic_inc_return(&uart_line_id) - 1;
  2089. if ((line < 0) || (line >= GENI_UART_NR_PORTS))
  2090. return -ENXIO;
  2091. is_console = (drv->cons ? true : false);
  2092. dev_port = get_port_from_line(line, is_console);
  2093. if (IS_ERR_OR_NULL(dev_port)) {
  2094. ret = PTR_ERR(dev_port);
  2095. dev_err(&pdev->dev, "Invalid line %d(%d)\n",
  2096. line, ret);
  2097. goto exit_geni_serial_probe;
  2098. }
  2099. uport = &dev_port->uport;
  2100. /* Don't allow 2 drivers to access the same port */
  2101. if (uport->private_data) {
  2102. ret = -ENODEV;
  2103. goto exit_geni_serial_probe;
  2104. }
  2105. uport->dev = &pdev->dev;
  2106. wrapper_ph_node = of_parse_phandle(pdev->dev.of_node,
  2107. "qcom,wrapper-core", 0);
  2108. if (IS_ERR_OR_NULL(wrapper_ph_node)) {
  2109. ret = PTR_ERR(wrapper_ph_node);
  2110. goto exit_geni_serial_probe;
  2111. }
  2112. wrapper_pdev = of_find_device_by_node(wrapper_ph_node);
  2113. of_node_put(wrapper_ph_node);
  2114. if (IS_ERR_OR_NULL(wrapper_pdev)) {
  2115. ret = PTR_ERR(wrapper_pdev);
  2116. goto exit_geni_serial_probe;
  2117. }
  2118. dev_port->wrapper_dev = &wrapper_pdev->dev;
  2119. dev_port->serial_rsc.wrapper_dev = &wrapper_pdev->dev;
  2120. if (is_console)
  2121. ret = geni_se_resources_init(&dev_port->serial_rsc,
  2122. UART_CONSOLE_CORE2X_VOTE,
  2123. (DEFAULT_SE_CLK * DEFAULT_BUS_WIDTH));
  2124. else
  2125. ret = geni_se_resources_init(&dev_port->serial_rsc,
  2126. UART_CORE2X_VOTE,
  2127. (DEFAULT_SE_CLK * DEFAULT_BUS_WIDTH));
  2128. if (ret)
  2129. goto exit_geni_serial_probe;
  2130. dev_port->serial_rsc.ctrl_dev = &pdev->dev;
  2131. if (of_property_read_u32(pdev->dev.of_node, "qcom,wakeup-byte",
  2132. &wake_char)) {
  2133. dev_dbg(&pdev->dev, "No Wakeup byte specified\n");
  2134. } else {
  2135. dev_port->wakeup_byte = (u8)wake_char;
  2136. dev_info(&pdev->dev, "Wakeup byte 0x%x\n",
  2137. dev_port->wakeup_byte);
  2138. }
  2139. dev_port->serial_rsc.se_clk = devm_clk_get(&pdev->dev, "se-clk");
  2140. if (IS_ERR(dev_port->serial_rsc.se_clk)) {
  2141. ret = PTR_ERR(dev_port->serial_rsc.se_clk);
  2142. dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
  2143. goto exit_geni_serial_probe;
  2144. }
  2145. dev_port->serial_rsc.m_ahb_clk = devm_clk_get(&pdev->dev, "m-ahb");
  2146. if (IS_ERR(dev_port->serial_rsc.m_ahb_clk)) {
  2147. ret = PTR_ERR(dev_port->serial_rsc.m_ahb_clk);
  2148. dev_err(&pdev->dev, "Err getting M AHB clk %d\n", ret);
  2149. goto exit_geni_serial_probe;
  2150. }
  2151. dev_port->serial_rsc.s_ahb_clk = devm_clk_get(&pdev->dev, "s-ahb");
  2152. if (IS_ERR(dev_port->serial_rsc.s_ahb_clk)) {
  2153. ret = PTR_ERR(dev_port->serial_rsc.s_ahb_clk);
  2154. dev_err(&pdev->dev, "Err getting S AHB clk %d\n", ret);
  2155. goto exit_geni_serial_probe;
  2156. }
  2157. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "se_phys");
  2158. if (!res) {
  2159. ret = -ENXIO;
  2160. dev_err(&pdev->dev, "Err getting IO region\n");
  2161. goto exit_geni_serial_probe;
  2162. }
  2163. uport->mapbase = res->start;
  2164. uport->membase = devm_ioremap(&pdev->dev, res->start,
  2165. resource_size(res));
  2166. if (!uport->membase) {
  2167. ret = -ENOMEM;
  2168. dev_err(&pdev->dev, "Err IO mapping serial iomem");
  2169. goto exit_geni_serial_probe;
  2170. }
  2171. /* Optional to use the Rx pin as wakeup irq */
  2172. dev_port->wakeup_irq = platform_get_irq(pdev, 1);
  2173. if ((dev_port->wakeup_irq < 0 && !is_console))
  2174. dev_info(&pdev->dev, "No wakeup IRQ configured\n");
  2175. dev_port->serial_rsc.geni_pinctrl = devm_pinctrl_get(&pdev->dev);
  2176. if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_pinctrl)) {
  2177. dev_err(&pdev->dev, "No pinctrl config specified!\n");
  2178. ret = PTR_ERR(dev_port->serial_rsc.geni_pinctrl);
  2179. goto exit_geni_serial_probe;
  2180. }
  2181. dev_port->serial_rsc.geni_gpio_active =
  2182. pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl,
  2183. PINCTRL_DEFAULT);
  2184. if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_active)) {
  2185. dev_err(&pdev->dev, "No default config specified!\n");
  2186. ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_active);
  2187. goto exit_geni_serial_probe;
  2188. }
  2189. /*
  2190. * For clients who setup an Inband wakeup, leave the GPIO pins
  2191. * always connected to the core, else move the pins to their
  2192. * defined "sleep" state.
  2193. */
  2194. if (dev_port->wakeup_irq > 0) {
  2195. dev_port->serial_rsc.geni_gpio_sleep =
  2196. dev_port->serial_rsc.geni_gpio_active;
  2197. } else {
  2198. dev_port->serial_rsc.geni_gpio_sleep =
  2199. pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl,
  2200. PINCTRL_SLEEP);
  2201. if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_sleep)) {
  2202. dev_err(&pdev->dev, "No sleep config specified!\n");
  2203. ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_sleep);
  2204. goto exit_geni_serial_probe;
  2205. }
  2206. }
  2207. wakeup_source_init(&dev_port->geni_wake, dev_name(&pdev->dev));
  2208. dev_port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  2209. dev_port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
  2210. dev_port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
  2211. uport->fifosize =
  2212. ((dev_port->tx_fifo_depth * dev_port->tx_fifo_width) >> 3);
  2213. uport->irq = platform_get_irq(pdev, 0);
  2214. if (uport->irq < 0) {
  2215. ret = uport->irq;
  2216. dev_err(&pdev->dev, "Failed to get IRQ %d\n", ret);
  2217. goto exit_geni_serial_probe;
  2218. }
  2219. uport->private_data = (void *)drv;
  2220. platform_set_drvdata(pdev, dev_port);
  2221. if (is_console) {
  2222. dev_port->handle_rx = handle_rx_console;
  2223. dev_port->rx_fifo = devm_kzalloc(uport->dev, sizeof(u32),
  2224. GFP_KERNEL);
  2225. } else {
  2226. pm_runtime_set_suspended(&pdev->dev);
  2227. pm_runtime_set_autosuspend_delay(&pdev->dev, 150);
  2228. pm_runtime_use_autosuspend(&pdev->dev);
  2229. pm_runtime_enable(&pdev->dev);
  2230. }
  2231. dev_info(&pdev->dev, "Serial port%d added.FifoSize %d is_console%d\n",
  2232. line, uport->fifosize, is_console);
  2233. device_create_file(uport->dev, &dev_attr_loopback);
  2234. device_create_file(uport->dev, &dev_attr_xfer_mode);
  2235. msm_geni_serial_debug_init(uport, is_console);
  2236. dev_port->port_setup = false;
  2237. return uart_add_one_port(drv, uport);
  2238. exit_geni_serial_probe:
  2239. return ret;
  2240. }
  2241. static int msm_geni_serial_remove(struct platform_device *pdev)
  2242. {
  2243. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  2244. struct uart_driver *drv =
  2245. (struct uart_driver *)port->uport.private_data;
  2246. wakeup_source_trash(&port->geni_wake);
  2247. uart_remove_one_port(drv, &port->uport);
  2248. return 0;
  2249. }
  2250. #ifdef CONFIG_PM
  2251. static int msm_geni_serial_runtime_suspend(struct device *dev)
  2252. {
  2253. struct platform_device *pdev = to_platform_device(dev);
  2254. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  2255. int ret = 0;
  2256. u32 geni_status = geni_read_reg_nolog(port->uport.membase,
  2257. SE_GENI_STATUS);
  2258. wait_for_transfers_inflight(&port->uport);
  2259. /*
  2260. * Disable Interrupt
  2261. * Manual RFR On.
  2262. * Stop Rx.
  2263. * Resources off
  2264. */
  2265. disable_irq(port->uport.irq);
  2266. stop_rx_sequencer(&port->uport);
  2267. geni_status = geni_read_reg_nolog(port->uport.membase, SE_GENI_STATUS);
  2268. if ((geni_status & M_GENI_CMD_ACTIVE))
  2269. stop_tx_sequencer(&port->uport);
  2270. ret = se_geni_resources_off(&port->serial_rsc);
  2271. if (ret) {
  2272. dev_err(dev, "%s: Error ret %d\n", __func__, ret);
  2273. goto exit_runtime_suspend;
  2274. }
  2275. if (port->wakeup_irq > 0) {
  2276. port->edge_count = 0;
  2277. enable_irq(port->wakeup_irq);
  2278. }
  2279. IPC_LOG_MSG(port->ipc_log_pwr, "%s:\n", __func__);
  2280. __pm_relax(&port->geni_wake);
  2281. exit_runtime_suspend:
  2282. return ret;
  2283. }
  2284. static int msm_geni_serial_runtime_resume(struct device *dev)
  2285. {
  2286. struct platform_device *pdev = to_platform_device(dev);
  2287. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  2288. int ret = 0;
  2289. /*
  2290. * Do an unconditional relax followed by a stay awake in case the
  2291. * wake source is activated by the wakeup isr.
  2292. */
  2293. __pm_relax(&port->geni_wake);
  2294. __pm_stay_awake(&port->geni_wake);
  2295. if (port->wakeup_irq > 0)
  2296. disable_irq(port->wakeup_irq);
  2297. /*
  2298. * Resources On.
  2299. * Start Rx.
  2300. * Auto RFR.
  2301. * Enable IRQ.
  2302. */
  2303. ret = se_geni_resources_on(&port->serial_rsc);
  2304. if (ret) {
  2305. dev_err(dev, "%s: Error ret %d\n", __func__, ret);
  2306. __pm_relax(&port->geni_wake);
  2307. goto exit_runtime_resume;
  2308. }
  2309. start_rx_sequencer(&port->uport);
  2310. /* Ensure that the Rx is running before enabling interrupts */
  2311. mb();
  2312. if (pm_runtime_enabled(dev))
  2313. enable_irq(port->uport.irq);
  2314. IPC_LOG_MSG(port->ipc_log_pwr, "%s:\n", __func__);
  2315. exit_runtime_resume:
  2316. return ret;
  2317. }
  2318. static int msm_geni_serial_sys_suspend_noirq(struct device *dev)
  2319. {
  2320. struct platform_device *pdev = to_platform_device(dev);
  2321. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  2322. struct uart_port *uport = &port->uport;
  2323. if (uart_console(uport)) {
  2324. uart_suspend_port((struct uart_driver *)uport->private_data,
  2325. uport);
  2326. } else {
  2327. struct uart_state *state = uport->state;
  2328. struct tty_port *tty_port = &state->port;
  2329. mutex_lock(&tty_port->mutex);
  2330. if (!pm_runtime_status_suspended(dev)) {
  2331. dev_err(dev, "%s:Active userspace vote; ioctl_cnt %d\n",
  2332. __func__, port->ioctl_count);
  2333. IPC_LOG_MSG(port->ipc_log_pwr,
  2334. "%s:Active userspace vote; ioctl_cnt %d\n",
  2335. __func__, port->ioctl_count);
  2336. mutex_unlock(&tty_port->mutex);
  2337. return -EBUSY;
  2338. }
  2339. IPC_LOG_MSG(port->ipc_log_pwr, "%s\n", __func__);
  2340. mutex_unlock(&tty_port->mutex);
  2341. }
  2342. return 0;
  2343. }
  2344. static int msm_geni_serial_sys_resume_noirq(struct device *dev)
  2345. {
  2346. struct platform_device *pdev = to_platform_device(dev);
  2347. struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
  2348. struct uart_port *uport = &port->uport;
  2349. if (uart_console(uport) &&
  2350. console_suspend_enabled && uport->suspended) {
  2351. uart_resume_port((struct uart_driver *)uport->private_data,
  2352. uport);
  2353. disable_irq(uport->irq);
  2354. }
  2355. return 0;
  2356. }
  2357. #else
  2358. static int msm_geni_serial_runtime_suspend(struct device *dev)
  2359. {
  2360. return 0;
  2361. }
  2362. static int msm_geni_serial_runtime_resume(struct device *dev)
  2363. {
  2364. return 0;
  2365. }
  2366. static int msm_geni_serial_sys_suspend_noirq(struct device *dev)
  2367. {
  2368. return 0;
  2369. }
  2370. static int msm_geni_serial_sys_resume_noirq(struct device *dev)
  2371. {
  2372. return 0;
  2373. }
  2374. #endif
  2375. static const struct dev_pm_ops msm_geni_serial_pm_ops = {
  2376. .runtime_suspend = msm_geni_serial_runtime_suspend,
  2377. .runtime_resume = msm_geni_serial_runtime_resume,
  2378. .suspend_noirq = msm_geni_serial_sys_suspend_noirq,
  2379. .resume_noirq = msm_geni_serial_sys_resume_noirq,
  2380. };
  2381. static struct platform_driver msm_geni_serial_platform_driver = {
  2382. .remove = msm_geni_serial_remove,
  2383. .probe = msm_geni_serial_probe,
  2384. .driver = {
  2385. .name = "msm_geni_serial",
  2386. .of_match_table = msm_geni_device_tbl,
  2387. .pm = &msm_geni_serial_pm_ops,
  2388. },
  2389. };
  2390. static struct uart_driver msm_geni_serial_hs_driver = {
  2391. .owner = THIS_MODULE,
  2392. .driver_name = "msm_geni_serial_hs",
  2393. .dev_name = "ttyHS",
  2394. .nr = GENI_UART_NR_PORTS,
  2395. };
  2396. static int __init msm_geni_serial_init(void)
  2397. {
  2398. int ret = 0;
  2399. int i;
  2400. for (i = 0; i < GENI_UART_NR_PORTS; i++) {
  2401. msm_geni_serial_ports[i].uport.iotype = UPIO_MEM;
  2402. msm_geni_serial_ports[i].uport.ops = &msm_geni_serial_pops;
  2403. msm_geni_serial_ports[i].uport.flags = UPF_BOOT_AUTOCONF;
  2404. msm_geni_serial_ports[i].uport.line = i;
  2405. }
  2406. for (i = 0; i < GENI_UART_CONS_PORTS; i++) {
  2407. msm_geni_console_port[i].uport.iotype = UPIO_MEM;
  2408. msm_geni_console_port[i].uport.ops = &msm_geni_console_pops;
  2409. msm_geni_console_port[i].uport.flags = UPF_BOOT_AUTOCONF;
  2410. msm_geni_console_port[i].uport.line = i;
  2411. }
  2412. ret = console_register(&msm_geni_console_driver);
  2413. if (ret)
  2414. return ret;
  2415. ret = uart_register_driver(&msm_geni_serial_hs_driver);
  2416. if (ret) {
  2417. uart_unregister_driver(&msm_geni_console_driver);
  2418. return ret;
  2419. }
  2420. ret = platform_driver_register(&msm_geni_serial_platform_driver);
  2421. if (ret) {
  2422. console_unregister(&msm_geni_console_driver);
  2423. uart_unregister_driver(&msm_geni_serial_hs_driver);
  2424. return ret;
  2425. }
  2426. pr_info("%s: Driver initialized", __func__);
  2427. return ret;
  2428. }
  2429. module_init(msm_geni_serial_init);
  2430. static void __exit msm_geni_serial_exit(void)
  2431. {
  2432. platform_driver_unregister(&msm_geni_serial_platform_driver);
  2433. uart_unregister_driver(&msm_geni_serial_hs_driver);
  2434. console_unregister(&msm_geni_console_driver);
  2435. }
  2436. module_exit(msm_geni_serial_exit);
  2437. MODULE_DESCRIPTION("Serial driver for GENI based QTI serial cores");
  2438. MODULE_LICENSE("GPL v2");
  2439. MODULE_ALIAS("tty:msm_geni_geni_serial");