msm_serial.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for msm7k serial device and console
  4. *
  5. * Copyright (C) 2007 Google, Inc.
  6. * Author: Robert Love <[email protected]>
  7. * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/atomic.h>
  11. #include <linux/dma/qcom_adm.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/module.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/wait.h>
  30. #define MSM_UART_MR1 0x0000
  31. #define MSM_UART_MR1_AUTO_RFR_LEVEL0 0x3F
  32. #define MSM_UART_MR1_AUTO_RFR_LEVEL1 0x3FF00
  33. #define MSM_UART_DM_MR1_AUTO_RFR_LEVEL1 0xFFFFFF00
  34. #define MSM_UART_MR1_RX_RDY_CTL BIT(7)
  35. #define MSM_UART_MR1_CTS_CTL BIT(6)
  36. #define MSM_UART_MR2 0x0004
  37. #define MSM_UART_MR2_ERROR_MODE BIT(6)
  38. #define MSM_UART_MR2_BITS_PER_CHAR 0x30
  39. #define MSM_UART_MR2_BITS_PER_CHAR_5 (0x0 << 4)
  40. #define MSM_UART_MR2_BITS_PER_CHAR_6 (0x1 << 4)
  41. #define MSM_UART_MR2_BITS_PER_CHAR_7 (0x2 << 4)
  42. #define MSM_UART_MR2_BITS_PER_CHAR_8 (0x3 << 4)
  43. #define MSM_UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2)
  44. #define MSM_UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2)
  45. #define MSM_UART_MR2_PARITY_MODE_NONE 0x0
  46. #define MSM_UART_MR2_PARITY_MODE_ODD 0x1
  47. #define MSM_UART_MR2_PARITY_MODE_EVEN 0x2
  48. #define MSM_UART_MR2_PARITY_MODE_SPACE 0x3
  49. #define MSM_UART_MR2_PARITY_MODE 0x3
  50. #define MSM_UART_CSR 0x0008
  51. #define MSM_UART_TF 0x000C
  52. #define UARTDM_TF 0x0070
  53. #define MSM_UART_CR 0x0010
  54. #define MSM_UART_CR_CMD_NULL (0 << 4)
  55. #define MSM_UART_CR_CMD_RESET_RX (1 << 4)
  56. #define MSM_UART_CR_CMD_RESET_TX (2 << 4)
  57. #define MSM_UART_CR_CMD_RESET_ERR (3 << 4)
  58. #define MSM_UART_CR_CMD_RESET_BREAK_INT (4 << 4)
  59. #define MSM_UART_CR_CMD_START_BREAK (5 << 4)
  60. #define MSM_UART_CR_CMD_STOP_BREAK (6 << 4)
  61. #define MSM_UART_CR_CMD_RESET_CTS (7 << 4)
  62. #define MSM_UART_CR_CMD_RESET_STALE_INT (8 << 4)
  63. #define MSM_UART_CR_CMD_PACKET_MODE (9 << 4)
  64. #define MSM_UART_CR_CMD_MODE_RESET (12 << 4)
  65. #define MSM_UART_CR_CMD_SET_RFR (13 << 4)
  66. #define MSM_UART_CR_CMD_RESET_RFR (14 << 4)
  67. #define MSM_UART_CR_CMD_PROTECTION_EN (16 << 4)
  68. #define MSM_UART_CR_CMD_STALE_EVENT_DISABLE (6 << 8)
  69. #define MSM_UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
  70. #define MSM_UART_CR_CMD_FORCE_STALE (4 << 8)
  71. #define MSM_UART_CR_CMD_RESET_TX_READY (3 << 8)
  72. #define MSM_UART_CR_TX_DISABLE BIT(3)
  73. #define MSM_UART_CR_TX_ENABLE BIT(2)
  74. #define MSM_UART_CR_RX_DISABLE BIT(1)
  75. #define MSM_UART_CR_RX_ENABLE BIT(0)
  76. #define MSM_UART_CR_CMD_RESET_RXBREAK_START ((1 << 11) | (2 << 4))
  77. #define MSM_UART_IMR 0x0014
  78. #define MSM_UART_IMR_TXLEV BIT(0)
  79. #define MSM_UART_IMR_RXSTALE BIT(3)
  80. #define MSM_UART_IMR_RXLEV BIT(4)
  81. #define MSM_UART_IMR_DELTA_CTS BIT(5)
  82. #define MSM_UART_IMR_CURRENT_CTS BIT(6)
  83. #define MSM_UART_IMR_RXBREAK_START BIT(10)
  84. #define MSM_UART_IPR_RXSTALE_LAST 0x20
  85. #define MSM_UART_IPR_STALE_LSB 0x1F
  86. #define MSM_UART_IPR_STALE_TIMEOUT_MSB 0x3FF80
  87. #define MSM_UART_DM_IPR_STALE_TIMEOUT_MSB 0xFFFFFF80
  88. #define MSM_UART_IPR 0x0018
  89. #define MSM_UART_TFWR 0x001C
  90. #define MSM_UART_RFWR 0x0020
  91. #define MSM_UART_HCR 0x0024
  92. #define MSM_UART_MREG 0x0028
  93. #define MSM_UART_NREG 0x002C
  94. #define MSM_UART_DREG 0x0030
  95. #define MSM_UART_MNDREG 0x0034
  96. #define MSM_UART_IRDA 0x0038
  97. #define MSM_UART_MISR_MODE 0x0040
  98. #define MSM_UART_MISR_RESET 0x0044
  99. #define MSM_UART_MISR_EXPORT 0x0048
  100. #define MSM_UART_MISR_VAL 0x004C
  101. #define MSM_UART_TEST_CTRL 0x0050
  102. #define MSM_UART_SR 0x0008
  103. #define MSM_UART_SR_HUNT_CHAR BIT(7)
  104. #define MSM_UART_SR_RX_BREAK BIT(6)
  105. #define MSM_UART_SR_PAR_FRAME_ERR BIT(5)
  106. #define MSM_UART_SR_OVERRUN BIT(4)
  107. #define MSM_UART_SR_TX_EMPTY BIT(3)
  108. #define MSM_UART_SR_TX_READY BIT(2)
  109. #define MSM_UART_SR_RX_FULL BIT(1)
  110. #define MSM_UART_SR_RX_READY BIT(0)
  111. #define MSM_UART_RF 0x000C
  112. #define UARTDM_RF 0x0070
  113. #define MSM_UART_MISR 0x0010
  114. #define MSM_UART_ISR 0x0014
  115. #define MSM_UART_ISR_TX_READY BIT(7)
  116. #define UARTDM_RXFS 0x50
  117. #define UARTDM_RXFS_BUF_SHIFT 0x7
  118. #define UARTDM_RXFS_BUF_MASK 0x7
  119. #define UARTDM_DMEN 0x3C
  120. #define UARTDM_DMEN_RX_SC_ENABLE BIT(5)
  121. #define UARTDM_DMEN_TX_SC_ENABLE BIT(4)
  122. #define UARTDM_DMEN_TX_BAM_ENABLE BIT(2) /* UARTDM_1P4 */
  123. #define UARTDM_DMEN_TX_DM_ENABLE BIT(0) /* < UARTDM_1P4 */
  124. #define UARTDM_DMEN_RX_BAM_ENABLE BIT(3) /* UARTDM_1P4 */
  125. #define UARTDM_DMEN_RX_DM_ENABLE BIT(1) /* < UARTDM_1P4 */
  126. #define UARTDM_DMRX 0x34
  127. #define UARTDM_NCF_TX 0x40
  128. #define UARTDM_RX_TOTAL_SNAP 0x38
  129. #define UARTDM_BURST_SIZE 16 /* in bytes */
  130. #define UARTDM_TX_AIGN(x) ((x) & ~0x3) /* valid for > 1p3 */
  131. #define UARTDM_TX_MAX 256 /* in bytes, valid for <= 1p3 */
  132. #define UARTDM_RX_SIZE (UART_XMIT_SIZE / 4)
  133. enum {
  134. UARTDM_1P1 = 1,
  135. UARTDM_1P2,
  136. UARTDM_1P3,
  137. UARTDM_1P4,
  138. };
  139. struct msm_dma {
  140. struct dma_chan *chan;
  141. enum dma_data_direction dir;
  142. dma_addr_t phys;
  143. unsigned char *virt;
  144. dma_cookie_t cookie;
  145. u32 enable_bit;
  146. unsigned int count;
  147. struct dma_async_tx_descriptor *desc;
  148. };
  149. struct msm_port {
  150. struct uart_port uart;
  151. char name[16];
  152. struct clk *clk;
  153. struct clk *pclk;
  154. unsigned int imr;
  155. int is_uartdm;
  156. unsigned int old_snap_state;
  157. bool break_detected;
  158. struct msm_dma tx_dma;
  159. struct msm_dma rx_dma;
  160. };
  161. static inline struct msm_port *to_msm_port(struct uart_port *up)
  162. {
  163. return container_of(up, struct msm_port, uart);
  164. }
  165. static
  166. void msm_write(struct uart_port *port, unsigned int val, unsigned int off)
  167. {
  168. writel_relaxed(val, port->membase + off);
  169. }
  170. static
  171. unsigned int msm_read(struct uart_port *port, unsigned int off)
  172. {
  173. return readl_relaxed(port->membase + off);
  174. }
  175. /*
  176. * Setup the MND registers to use the TCXO clock.
  177. */
  178. static void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
  179. {
  180. msm_write(port, 0x06, MSM_UART_MREG);
  181. msm_write(port, 0xF1, MSM_UART_NREG);
  182. msm_write(port, 0x0F, MSM_UART_DREG);
  183. msm_write(port, 0x1A, MSM_UART_MNDREG);
  184. port->uartclk = 1843200;
  185. }
  186. /*
  187. * Setup the MND registers to use the TCXO clock divided by 4.
  188. */
  189. static void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
  190. {
  191. msm_write(port, 0x18, MSM_UART_MREG);
  192. msm_write(port, 0xF6, MSM_UART_NREG);
  193. msm_write(port, 0x0F, MSM_UART_DREG);
  194. msm_write(port, 0x0A, MSM_UART_MNDREG);
  195. port->uartclk = 1843200;
  196. }
  197. static void msm_serial_set_mnd_regs(struct uart_port *port)
  198. {
  199. struct msm_port *msm_port = to_msm_port(port);
  200. /*
  201. * These registers don't exist so we change the clk input rate
  202. * on uartdm hardware instead
  203. */
  204. if (msm_port->is_uartdm)
  205. return;
  206. if (port->uartclk == 19200000)
  207. msm_serial_set_mnd_regs_tcxo(port);
  208. else if (port->uartclk == 4800000)
  209. msm_serial_set_mnd_regs_tcxoby4(port);
  210. }
  211. static void msm_handle_tx(struct uart_port *port);
  212. static void msm_start_rx_dma(struct msm_port *msm_port);
  213. static void msm_stop_dma(struct uart_port *port, struct msm_dma *dma)
  214. {
  215. struct device *dev = port->dev;
  216. unsigned int mapped;
  217. u32 val;
  218. mapped = dma->count;
  219. dma->count = 0;
  220. dmaengine_terminate_all(dma->chan);
  221. /*
  222. * DMA Stall happens if enqueue and flush command happens concurrently.
  223. * For example before changing the baud rate/protocol configuration and
  224. * sending flush command to ADM, disable the channel of UARTDM.
  225. * Note: should not reset the receiver here immediately as it is not
  226. * suggested to do disable/reset or reset/disable at the same time.
  227. */
  228. val = msm_read(port, UARTDM_DMEN);
  229. val &= ~dma->enable_bit;
  230. msm_write(port, val, UARTDM_DMEN);
  231. if (mapped)
  232. dma_unmap_single(dev, dma->phys, mapped, dma->dir);
  233. }
  234. static void msm_release_dma(struct msm_port *msm_port)
  235. {
  236. struct msm_dma *dma;
  237. dma = &msm_port->tx_dma;
  238. if (dma->chan) {
  239. msm_stop_dma(&msm_port->uart, dma);
  240. dma_release_channel(dma->chan);
  241. }
  242. memset(dma, 0, sizeof(*dma));
  243. dma = &msm_port->rx_dma;
  244. if (dma->chan) {
  245. msm_stop_dma(&msm_port->uart, dma);
  246. dma_release_channel(dma->chan);
  247. kfree(dma->virt);
  248. }
  249. memset(dma, 0, sizeof(*dma));
  250. }
  251. static void msm_request_tx_dma(struct msm_port *msm_port, resource_size_t base)
  252. {
  253. struct device *dev = msm_port->uart.dev;
  254. struct dma_slave_config conf;
  255. struct qcom_adm_peripheral_config periph_conf = {};
  256. struct msm_dma *dma;
  257. u32 crci = 0;
  258. int ret;
  259. dma = &msm_port->tx_dma;
  260. /* allocate DMA resources, if available */
  261. dma->chan = dma_request_chan(dev, "tx");
  262. if (IS_ERR(dma->chan))
  263. goto no_tx;
  264. of_property_read_u32(dev->of_node, "qcom,tx-crci", &crci);
  265. memset(&conf, 0, sizeof(conf));
  266. conf.direction = DMA_MEM_TO_DEV;
  267. conf.device_fc = true;
  268. conf.dst_addr = base + UARTDM_TF;
  269. conf.dst_maxburst = UARTDM_BURST_SIZE;
  270. if (crci) {
  271. conf.peripheral_config = &periph_conf;
  272. conf.peripheral_size = sizeof(periph_conf);
  273. periph_conf.crci = crci;
  274. }
  275. ret = dmaengine_slave_config(dma->chan, &conf);
  276. if (ret)
  277. goto rel_tx;
  278. dma->dir = DMA_TO_DEVICE;
  279. if (msm_port->is_uartdm < UARTDM_1P4)
  280. dma->enable_bit = UARTDM_DMEN_TX_DM_ENABLE;
  281. else
  282. dma->enable_bit = UARTDM_DMEN_TX_BAM_ENABLE;
  283. return;
  284. rel_tx:
  285. dma_release_channel(dma->chan);
  286. no_tx:
  287. memset(dma, 0, sizeof(*dma));
  288. }
  289. static void msm_request_rx_dma(struct msm_port *msm_port, resource_size_t base)
  290. {
  291. struct device *dev = msm_port->uart.dev;
  292. struct dma_slave_config conf;
  293. struct qcom_adm_peripheral_config periph_conf = {};
  294. struct msm_dma *dma;
  295. u32 crci = 0;
  296. int ret;
  297. dma = &msm_port->rx_dma;
  298. /* allocate DMA resources, if available */
  299. dma->chan = dma_request_chan(dev, "rx");
  300. if (IS_ERR(dma->chan))
  301. goto no_rx;
  302. of_property_read_u32(dev->of_node, "qcom,rx-crci", &crci);
  303. dma->virt = kzalloc(UARTDM_RX_SIZE, GFP_KERNEL);
  304. if (!dma->virt)
  305. goto rel_rx;
  306. memset(&conf, 0, sizeof(conf));
  307. conf.direction = DMA_DEV_TO_MEM;
  308. conf.device_fc = true;
  309. conf.src_addr = base + UARTDM_RF;
  310. conf.src_maxburst = UARTDM_BURST_SIZE;
  311. if (crci) {
  312. conf.peripheral_config = &periph_conf;
  313. conf.peripheral_size = sizeof(periph_conf);
  314. periph_conf.crci = crci;
  315. }
  316. ret = dmaengine_slave_config(dma->chan, &conf);
  317. if (ret)
  318. goto err;
  319. dma->dir = DMA_FROM_DEVICE;
  320. if (msm_port->is_uartdm < UARTDM_1P4)
  321. dma->enable_bit = UARTDM_DMEN_RX_DM_ENABLE;
  322. else
  323. dma->enable_bit = UARTDM_DMEN_RX_BAM_ENABLE;
  324. return;
  325. err:
  326. kfree(dma->virt);
  327. rel_rx:
  328. dma_release_channel(dma->chan);
  329. no_rx:
  330. memset(dma, 0, sizeof(*dma));
  331. }
  332. static inline void msm_wait_for_xmitr(struct uart_port *port)
  333. {
  334. unsigned int timeout = 500000;
  335. while (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_EMPTY)) {
  336. if (msm_read(port, MSM_UART_ISR) & MSM_UART_ISR_TX_READY)
  337. break;
  338. udelay(1);
  339. if (!timeout--)
  340. break;
  341. }
  342. msm_write(port, MSM_UART_CR_CMD_RESET_TX_READY, MSM_UART_CR);
  343. }
  344. static void msm_stop_tx(struct uart_port *port)
  345. {
  346. struct msm_port *msm_port = to_msm_port(port);
  347. msm_port->imr &= ~MSM_UART_IMR_TXLEV;
  348. msm_write(port, msm_port->imr, MSM_UART_IMR);
  349. }
  350. static void msm_start_tx(struct uart_port *port)
  351. {
  352. struct msm_port *msm_port = to_msm_port(port);
  353. struct msm_dma *dma = &msm_port->tx_dma;
  354. /* Already started in DMA mode */
  355. if (dma->count)
  356. return;
  357. msm_port->imr |= MSM_UART_IMR_TXLEV;
  358. msm_write(port, msm_port->imr, MSM_UART_IMR);
  359. }
  360. static void msm_reset_dm_count(struct uart_port *port, int count)
  361. {
  362. msm_wait_for_xmitr(port);
  363. msm_write(port, count, UARTDM_NCF_TX);
  364. msm_read(port, UARTDM_NCF_TX);
  365. }
  366. static void msm_complete_tx_dma(void *args)
  367. {
  368. struct msm_port *msm_port = args;
  369. struct uart_port *port = &msm_port->uart;
  370. struct circ_buf *xmit = &port->state->xmit;
  371. struct msm_dma *dma = &msm_port->tx_dma;
  372. struct dma_tx_state state;
  373. unsigned long flags;
  374. unsigned int count;
  375. u32 val;
  376. spin_lock_irqsave(&port->lock, flags);
  377. /* Already stopped */
  378. if (!dma->count)
  379. goto done;
  380. dmaengine_tx_status(dma->chan, dma->cookie, &state);
  381. dma_unmap_single(port->dev, dma->phys, dma->count, dma->dir);
  382. val = msm_read(port, UARTDM_DMEN);
  383. val &= ~dma->enable_bit;
  384. msm_write(port, val, UARTDM_DMEN);
  385. if (msm_port->is_uartdm > UARTDM_1P3) {
  386. msm_write(port, MSM_UART_CR_CMD_RESET_TX, MSM_UART_CR);
  387. msm_write(port, MSM_UART_CR_TX_ENABLE, MSM_UART_CR);
  388. }
  389. count = dma->count - state.residue;
  390. port->icount.tx += count;
  391. dma->count = 0;
  392. xmit->tail += count;
  393. xmit->tail &= UART_XMIT_SIZE - 1;
  394. /* Restore "Tx FIFO below watermark" interrupt */
  395. msm_port->imr |= MSM_UART_IMR_TXLEV;
  396. msm_write(port, msm_port->imr, MSM_UART_IMR);
  397. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  398. uart_write_wakeup(port);
  399. msm_handle_tx(port);
  400. done:
  401. spin_unlock_irqrestore(&port->lock, flags);
  402. }
  403. static int msm_handle_tx_dma(struct msm_port *msm_port, unsigned int count)
  404. {
  405. struct circ_buf *xmit = &msm_port->uart.state->xmit;
  406. struct uart_port *port = &msm_port->uart;
  407. struct msm_dma *dma = &msm_port->tx_dma;
  408. void *cpu_addr;
  409. int ret;
  410. u32 val;
  411. cpu_addr = &xmit->buf[xmit->tail];
  412. dma->phys = dma_map_single(port->dev, cpu_addr, count, dma->dir);
  413. ret = dma_mapping_error(port->dev, dma->phys);
  414. if (ret)
  415. return ret;
  416. dma->desc = dmaengine_prep_slave_single(dma->chan, dma->phys,
  417. count, DMA_MEM_TO_DEV,
  418. DMA_PREP_INTERRUPT |
  419. DMA_PREP_FENCE);
  420. if (!dma->desc) {
  421. ret = -EIO;
  422. goto unmap;
  423. }
  424. dma->desc->callback = msm_complete_tx_dma;
  425. dma->desc->callback_param = msm_port;
  426. dma->cookie = dmaengine_submit(dma->desc);
  427. ret = dma_submit_error(dma->cookie);
  428. if (ret)
  429. goto unmap;
  430. /*
  431. * Using DMA complete for Tx FIFO reload, no need for
  432. * "Tx FIFO below watermark" one, disable it
  433. */
  434. msm_port->imr &= ~MSM_UART_IMR_TXLEV;
  435. msm_write(port, msm_port->imr, MSM_UART_IMR);
  436. dma->count = count;
  437. val = msm_read(port, UARTDM_DMEN);
  438. val |= dma->enable_bit;
  439. if (msm_port->is_uartdm < UARTDM_1P4)
  440. msm_write(port, val, UARTDM_DMEN);
  441. msm_reset_dm_count(port, count);
  442. if (msm_port->is_uartdm > UARTDM_1P3)
  443. msm_write(port, val, UARTDM_DMEN);
  444. dma_async_issue_pending(dma->chan);
  445. return 0;
  446. unmap:
  447. dma_unmap_single(port->dev, dma->phys, count, dma->dir);
  448. return ret;
  449. }
  450. static void msm_complete_rx_dma(void *args)
  451. {
  452. struct msm_port *msm_port = args;
  453. struct uart_port *port = &msm_port->uart;
  454. struct tty_port *tport = &port->state->port;
  455. struct msm_dma *dma = &msm_port->rx_dma;
  456. int count = 0, i, sysrq;
  457. unsigned long flags;
  458. u32 val;
  459. spin_lock_irqsave(&port->lock, flags);
  460. /* Already stopped */
  461. if (!dma->count)
  462. goto done;
  463. val = msm_read(port, UARTDM_DMEN);
  464. val &= ~dma->enable_bit;
  465. msm_write(port, val, UARTDM_DMEN);
  466. if (msm_read(port, MSM_UART_SR) & MSM_UART_SR_OVERRUN) {
  467. port->icount.overrun++;
  468. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  469. msm_write(port, MSM_UART_CR_CMD_RESET_ERR, MSM_UART_CR);
  470. }
  471. count = msm_read(port, UARTDM_RX_TOTAL_SNAP);
  472. port->icount.rx += count;
  473. dma->count = 0;
  474. dma_unmap_single(port->dev, dma->phys, UARTDM_RX_SIZE, dma->dir);
  475. for (i = 0; i < count; i++) {
  476. char flag = TTY_NORMAL;
  477. if (msm_port->break_detected && dma->virt[i] == 0) {
  478. port->icount.brk++;
  479. flag = TTY_BREAK;
  480. msm_port->break_detected = false;
  481. if (uart_handle_break(port))
  482. continue;
  483. }
  484. if (!(port->read_status_mask & MSM_UART_SR_RX_BREAK))
  485. flag = TTY_NORMAL;
  486. spin_unlock_irqrestore(&port->lock, flags);
  487. sysrq = uart_handle_sysrq_char(port, dma->virt[i]);
  488. spin_lock_irqsave(&port->lock, flags);
  489. if (!sysrq)
  490. tty_insert_flip_char(tport, dma->virt[i], flag);
  491. }
  492. msm_start_rx_dma(msm_port);
  493. done:
  494. spin_unlock_irqrestore(&port->lock, flags);
  495. if (count)
  496. tty_flip_buffer_push(tport);
  497. }
  498. static void msm_start_rx_dma(struct msm_port *msm_port)
  499. {
  500. struct msm_dma *dma = &msm_port->rx_dma;
  501. struct uart_port *uart = &msm_port->uart;
  502. u32 val;
  503. int ret;
  504. if (IS_ENABLED(CONFIG_CONSOLE_POLL))
  505. return;
  506. if (!dma->chan)
  507. return;
  508. dma->phys = dma_map_single(uart->dev, dma->virt,
  509. UARTDM_RX_SIZE, dma->dir);
  510. ret = dma_mapping_error(uart->dev, dma->phys);
  511. if (ret)
  512. goto sw_mode;
  513. dma->desc = dmaengine_prep_slave_single(dma->chan, dma->phys,
  514. UARTDM_RX_SIZE, DMA_DEV_TO_MEM,
  515. DMA_PREP_INTERRUPT);
  516. if (!dma->desc)
  517. goto unmap;
  518. dma->desc->callback = msm_complete_rx_dma;
  519. dma->desc->callback_param = msm_port;
  520. dma->cookie = dmaengine_submit(dma->desc);
  521. ret = dma_submit_error(dma->cookie);
  522. if (ret)
  523. goto unmap;
  524. /*
  525. * Using DMA for FIFO off-load, no need for "Rx FIFO over
  526. * watermark" or "stale" interrupts, disable them
  527. */
  528. msm_port->imr &= ~(MSM_UART_IMR_RXLEV | MSM_UART_IMR_RXSTALE);
  529. /*
  530. * Well, when DMA is ADM3 engine(implied by <= UARTDM v1.3),
  531. * we need RXSTALE to flush input DMA fifo to memory
  532. */
  533. if (msm_port->is_uartdm < UARTDM_1P4)
  534. msm_port->imr |= MSM_UART_IMR_RXSTALE;
  535. msm_write(uart, msm_port->imr, MSM_UART_IMR);
  536. dma->count = UARTDM_RX_SIZE;
  537. dma_async_issue_pending(dma->chan);
  538. msm_write(uart, MSM_UART_CR_CMD_RESET_STALE_INT, MSM_UART_CR);
  539. msm_write(uart, MSM_UART_CR_CMD_STALE_EVENT_ENABLE, MSM_UART_CR);
  540. val = msm_read(uart, UARTDM_DMEN);
  541. val |= dma->enable_bit;
  542. if (msm_port->is_uartdm < UARTDM_1P4)
  543. msm_write(uart, val, UARTDM_DMEN);
  544. msm_write(uart, UARTDM_RX_SIZE, UARTDM_DMRX);
  545. if (msm_port->is_uartdm > UARTDM_1P3)
  546. msm_write(uart, val, UARTDM_DMEN);
  547. return;
  548. unmap:
  549. dma_unmap_single(uart->dev, dma->phys, UARTDM_RX_SIZE, dma->dir);
  550. sw_mode:
  551. /*
  552. * Switch from DMA to SW/FIFO mode. After clearing Rx BAM (UARTDM_DMEN),
  553. * receiver must be reset.
  554. */
  555. msm_write(uart, MSM_UART_CR_CMD_RESET_RX, MSM_UART_CR);
  556. msm_write(uart, MSM_UART_CR_RX_ENABLE, MSM_UART_CR);
  557. msm_write(uart, MSM_UART_CR_CMD_RESET_STALE_INT, MSM_UART_CR);
  558. msm_write(uart, 0xFFFFFF, UARTDM_DMRX);
  559. msm_write(uart, MSM_UART_CR_CMD_STALE_EVENT_ENABLE, MSM_UART_CR);
  560. /* Re-enable RX interrupts */
  561. msm_port->imr |= MSM_UART_IMR_RXLEV | MSM_UART_IMR_RXSTALE;
  562. msm_write(uart, msm_port->imr, MSM_UART_IMR);
  563. }
  564. static void msm_stop_rx(struct uart_port *port)
  565. {
  566. struct msm_port *msm_port = to_msm_port(port);
  567. struct msm_dma *dma = &msm_port->rx_dma;
  568. msm_port->imr &= ~(MSM_UART_IMR_RXLEV | MSM_UART_IMR_RXSTALE);
  569. msm_write(port, msm_port->imr, MSM_UART_IMR);
  570. if (dma->chan)
  571. msm_stop_dma(port, dma);
  572. }
  573. static void msm_enable_ms(struct uart_port *port)
  574. {
  575. struct msm_port *msm_port = to_msm_port(port);
  576. msm_port->imr |= MSM_UART_IMR_DELTA_CTS;
  577. msm_write(port, msm_port->imr, MSM_UART_IMR);
  578. }
  579. static void msm_handle_rx_dm(struct uart_port *port, unsigned int misr)
  580. __must_hold(&port->lock)
  581. {
  582. struct tty_port *tport = &port->state->port;
  583. unsigned int sr;
  584. int count = 0;
  585. struct msm_port *msm_port = to_msm_port(port);
  586. if ((msm_read(port, MSM_UART_SR) & MSM_UART_SR_OVERRUN)) {
  587. port->icount.overrun++;
  588. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  589. msm_write(port, MSM_UART_CR_CMD_RESET_ERR, MSM_UART_CR);
  590. }
  591. if (misr & MSM_UART_IMR_RXSTALE) {
  592. count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
  593. msm_port->old_snap_state;
  594. msm_port->old_snap_state = 0;
  595. } else {
  596. count = 4 * (msm_read(port, MSM_UART_RFWR));
  597. msm_port->old_snap_state += count;
  598. }
  599. /* TODO: Precise error reporting */
  600. port->icount.rx += count;
  601. while (count > 0) {
  602. unsigned char buf[4];
  603. int sysrq, r_count, i;
  604. sr = msm_read(port, MSM_UART_SR);
  605. if ((sr & MSM_UART_SR_RX_READY) == 0) {
  606. msm_port->old_snap_state -= count;
  607. break;
  608. }
  609. ioread32_rep(port->membase + UARTDM_RF, buf, 1);
  610. r_count = min_t(int, count, sizeof(buf));
  611. for (i = 0; i < r_count; i++) {
  612. char flag = TTY_NORMAL;
  613. if (msm_port->break_detected && buf[i] == 0) {
  614. port->icount.brk++;
  615. flag = TTY_BREAK;
  616. msm_port->break_detected = false;
  617. if (uart_handle_break(port))
  618. continue;
  619. }
  620. if (!(port->read_status_mask & MSM_UART_SR_RX_BREAK))
  621. flag = TTY_NORMAL;
  622. spin_unlock(&port->lock);
  623. sysrq = uart_handle_sysrq_char(port, buf[i]);
  624. spin_lock(&port->lock);
  625. if (!sysrq)
  626. tty_insert_flip_char(tport, buf[i], flag);
  627. }
  628. count -= r_count;
  629. }
  630. tty_flip_buffer_push(tport);
  631. if (misr & (MSM_UART_IMR_RXSTALE))
  632. msm_write(port, MSM_UART_CR_CMD_RESET_STALE_INT, MSM_UART_CR);
  633. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  634. msm_write(port, MSM_UART_CR_CMD_STALE_EVENT_ENABLE, MSM_UART_CR);
  635. /* Try to use DMA */
  636. msm_start_rx_dma(msm_port);
  637. }
  638. static void msm_handle_rx(struct uart_port *port)
  639. __must_hold(&port->lock)
  640. {
  641. struct tty_port *tport = &port->state->port;
  642. unsigned int sr;
  643. /*
  644. * Handle overrun. My understanding of the hardware is that overrun
  645. * is not tied to the RX buffer, so we handle the case out of band.
  646. */
  647. if ((msm_read(port, MSM_UART_SR) & MSM_UART_SR_OVERRUN)) {
  648. port->icount.overrun++;
  649. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  650. msm_write(port, MSM_UART_CR_CMD_RESET_ERR, MSM_UART_CR);
  651. }
  652. /* and now the main RX loop */
  653. while ((sr = msm_read(port, MSM_UART_SR)) & MSM_UART_SR_RX_READY) {
  654. unsigned int c;
  655. char flag = TTY_NORMAL;
  656. int sysrq;
  657. c = msm_read(port, MSM_UART_RF);
  658. if (sr & MSM_UART_SR_RX_BREAK) {
  659. port->icount.brk++;
  660. if (uart_handle_break(port))
  661. continue;
  662. } else if (sr & MSM_UART_SR_PAR_FRAME_ERR) {
  663. port->icount.frame++;
  664. } else {
  665. port->icount.rx++;
  666. }
  667. /* Mask conditions we're ignorning. */
  668. sr &= port->read_status_mask;
  669. if (sr & MSM_UART_SR_RX_BREAK)
  670. flag = TTY_BREAK;
  671. else if (sr & MSM_UART_SR_PAR_FRAME_ERR)
  672. flag = TTY_FRAME;
  673. spin_unlock(&port->lock);
  674. sysrq = uart_handle_sysrq_char(port, c);
  675. spin_lock(&port->lock);
  676. if (!sysrq)
  677. tty_insert_flip_char(tport, c, flag);
  678. }
  679. tty_flip_buffer_push(tport);
  680. }
  681. static void msm_handle_tx_pio(struct uart_port *port, unsigned int tx_count)
  682. {
  683. struct circ_buf *xmit = &port->state->xmit;
  684. struct msm_port *msm_port = to_msm_port(port);
  685. unsigned int num_chars;
  686. unsigned int tf_pointer = 0;
  687. void __iomem *tf;
  688. if (msm_port->is_uartdm)
  689. tf = port->membase + UARTDM_TF;
  690. else
  691. tf = port->membase + MSM_UART_TF;
  692. if (tx_count && msm_port->is_uartdm)
  693. msm_reset_dm_count(port, tx_count);
  694. while (tf_pointer < tx_count) {
  695. int i;
  696. char buf[4] = { 0 };
  697. if (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_READY))
  698. break;
  699. if (msm_port->is_uartdm)
  700. num_chars = min(tx_count - tf_pointer,
  701. (unsigned int)sizeof(buf));
  702. else
  703. num_chars = 1;
  704. for (i = 0; i < num_chars; i++) {
  705. buf[i] = xmit->buf[xmit->tail + i];
  706. port->icount.tx++;
  707. }
  708. iowrite32_rep(tf, buf, 1);
  709. xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
  710. tf_pointer += num_chars;
  711. }
  712. /* disable tx interrupts if nothing more to send */
  713. if (uart_circ_empty(xmit))
  714. msm_stop_tx(port);
  715. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  716. uart_write_wakeup(port);
  717. }
  718. static void msm_handle_tx(struct uart_port *port)
  719. {
  720. struct msm_port *msm_port = to_msm_port(port);
  721. struct circ_buf *xmit = &msm_port->uart.state->xmit;
  722. struct msm_dma *dma = &msm_port->tx_dma;
  723. unsigned int pio_count, dma_count, dma_min;
  724. char buf[4] = { 0 };
  725. void __iomem *tf;
  726. int err = 0;
  727. if (port->x_char) {
  728. if (msm_port->is_uartdm)
  729. tf = port->membase + UARTDM_TF;
  730. else
  731. tf = port->membase + MSM_UART_TF;
  732. buf[0] = port->x_char;
  733. if (msm_port->is_uartdm)
  734. msm_reset_dm_count(port, 1);
  735. iowrite32_rep(tf, buf, 1);
  736. port->icount.tx++;
  737. port->x_char = 0;
  738. return;
  739. }
  740. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  741. msm_stop_tx(port);
  742. return;
  743. }
  744. pio_count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  745. dma_count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  746. dma_min = 1; /* Always DMA */
  747. if (msm_port->is_uartdm > UARTDM_1P3) {
  748. dma_count = UARTDM_TX_AIGN(dma_count);
  749. dma_min = UARTDM_BURST_SIZE;
  750. } else {
  751. if (dma_count > UARTDM_TX_MAX)
  752. dma_count = UARTDM_TX_MAX;
  753. }
  754. if (pio_count > port->fifosize)
  755. pio_count = port->fifosize;
  756. if (!dma->chan || dma_count < dma_min)
  757. msm_handle_tx_pio(port, pio_count);
  758. else
  759. err = msm_handle_tx_dma(msm_port, dma_count);
  760. if (err) /* fall back to PIO mode */
  761. msm_handle_tx_pio(port, pio_count);
  762. }
  763. static void msm_handle_delta_cts(struct uart_port *port)
  764. {
  765. msm_write(port, MSM_UART_CR_CMD_RESET_CTS, MSM_UART_CR);
  766. port->icount.cts++;
  767. wake_up_interruptible(&port->state->port.delta_msr_wait);
  768. }
  769. static irqreturn_t msm_uart_irq(int irq, void *dev_id)
  770. {
  771. struct uart_port *port = dev_id;
  772. struct msm_port *msm_port = to_msm_port(port);
  773. struct msm_dma *dma = &msm_port->rx_dma;
  774. unsigned long flags;
  775. unsigned int misr;
  776. u32 val;
  777. spin_lock_irqsave(&port->lock, flags);
  778. misr = msm_read(port, MSM_UART_MISR);
  779. msm_write(port, 0, MSM_UART_IMR); /* disable interrupt */
  780. if (misr & MSM_UART_IMR_RXBREAK_START) {
  781. msm_port->break_detected = true;
  782. msm_write(port, MSM_UART_CR_CMD_RESET_RXBREAK_START, MSM_UART_CR);
  783. }
  784. if (misr & (MSM_UART_IMR_RXLEV | MSM_UART_IMR_RXSTALE)) {
  785. if (dma->count) {
  786. val = MSM_UART_CR_CMD_STALE_EVENT_DISABLE;
  787. msm_write(port, val, MSM_UART_CR);
  788. val = MSM_UART_CR_CMD_RESET_STALE_INT;
  789. msm_write(port, val, MSM_UART_CR);
  790. /*
  791. * Flush DMA input fifo to memory, this will also
  792. * trigger DMA RX completion
  793. */
  794. dmaengine_terminate_all(dma->chan);
  795. } else if (msm_port->is_uartdm) {
  796. msm_handle_rx_dm(port, misr);
  797. } else {
  798. msm_handle_rx(port);
  799. }
  800. }
  801. if (misr & MSM_UART_IMR_TXLEV)
  802. msm_handle_tx(port);
  803. if (misr & MSM_UART_IMR_DELTA_CTS)
  804. msm_handle_delta_cts(port);
  805. msm_write(port, msm_port->imr, MSM_UART_IMR); /* restore interrupt */
  806. spin_unlock_irqrestore(&port->lock, flags);
  807. return IRQ_HANDLED;
  808. }
  809. static unsigned int msm_tx_empty(struct uart_port *port)
  810. {
  811. return (msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
  812. }
  813. static unsigned int msm_get_mctrl(struct uart_port *port)
  814. {
  815. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
  816. }
  817. static void msm_reset(struct uart_port *port)
  818. {
  819. struct msm_port *msm_port = to_msm_port(port);
  820. unsigned int mr;
  821. /* reset everything */
  822. msm_write(port, MSM_UART_CR_CMD_RESET_RX, MSM_UART_CR);
  823. msm_write(port, MSM_UART_CR_CMD_RESET_TX, MSM_UART_CR);
  824. msm_write(port, MSM_UART_CR_CMD_RESET_ERR, MSM_UART_CR);
  825. msm_write(port, MSM_UART_CR_CMD_RESET_BREAK_INT, MSM_UART_CR);
  826. msm_write(port, MSM_UART_CR_CMD_RESET_CTS, MSM_UART_CR);
  827. msm_write(port, MSM_UART_CR_CMD_RESET_RFR, MSM_UART_CR);
  828. mr = msm_read(port, MSM_UART_MR1);
  829. mr &= ~MSM_UART_MR1_RX_RDY_CTL;
  830. msm_write(port, mr, MSM_UART_MR1);
  831. /* Disable DM modes */
  832. if (msm_port->is_uartdm)
  833. msm_write(port, 0, UARTDM_DMEN);
  834. }
  835. static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
  836. {
  837. unsigned int mr;
  838. mr = msm_read(port, MSM_UART_MR1);
  839. if (!(mctrl & TIOCM_RTS)) {
  840. mr &= ~MSM_UART_MR1_RX_RDY_CTL;
  841. msm_write(port, mr, MSM_UART_MR1);
  842. msm_write(port, MSM_UART_CR_CMD_RESET_RFR, MSM_UART_CR);
  843. } else {
  844. mr |= MSM_UART_MR1_RX_RDY_CTL;
  845. msm_write(port, mr, MSM_UART_MR1);
  846. }
  847. }
  848. static void msm_break_ctl(struct uart_port *port, int break_ctl)
  849. {
  850. if (break_ctl)
  851. msm_write(port, MSM_UART_CR_CMD_START_BREAK, MSM_UART_CR);
  852. else
  853. msm_write(port, MSM_UART_CR_CMD_STOP_BREAK, MSM_UART_CR);
  854. }
  855. struct msm_baud_map {
  856. u16 divisor;
  857. u8 code;
  858. u8 rxstale;
  859. };
  860. static const struct msm_baud_map *
  861. msm_find_best_baud(struct uart_port *port, unsigned int baud,
  862. unsigned long *rate)
  863. {
  864. struct msm_port *msm_port = to_msm_port(port);
  865. unsigned int divisor, result;
  866. unsigned long target, old, best_rate = 0, diff, best_diff = ULONG_MAX;
  867. const struct msm_baud_map *entry, *end, *best;
  868. static const struct msm_baud_map table[] = {
  869. { 1, 0xff, 31 },
  870. { 2, 0xee, 16 },
  871. { 3, 0xdd, 8 },
  872. { 4, 0xcc, 6 },
  873. { 6, 0xbb, 6 },
  874. { 8, 0xaa, 6 },
  875. { 12, 0x99, 6 },
  876. { 16, 0x88, 1 },
  877. { 24, 0x77, 1 },
  878. { 32, 0x66, 1 },
  879. { 48, 0x55, 1 },
  880. { 96, 0x44, 1 },
  881. { 192, 0x33, 1 },
  882. { 384, 0x22, 1 },
  883. { 768, 0x11, 1 },
  884. { 1536, 0x00, 1 },
  885. };
  886. best = table; /* Default to smallest divider */
  887. target = clk_round_rate(msm_port->clk, 16 * baud);
  888. divisor = DIV_ROUND_CLOSEST(target, 16 * baud);
  889. end = table + ARRAY_SIZE(table);
  890. entry = table;
  891. while (entry < end) {
  892. if (entry->divisor <= divisor) {
  893. result = target / entry->divisor / 16;
  894. diff = abs(result - baud);
  895. /* Keep track of best entry */
  896. if (diff < best_diff) {
  897. best_diff = diff;
  898. best = entry;
  899. best_rate = target;
  900. }
  901. if (result == baud)
  902. break;
  903. } else if (entry->divisor > divisor) {
  904. old = target;
  905. target = clk_round_rate(msm_port->clk, old + 1);
  906. /*
  907. * The rate didn't get any faster so we can't do
  908. * better at dividing it down
  909. */
  910. if (target == old)
  911. break;
  912. /* Start the divisor search over at this new rate */
  913. entry = table;
  914. divisor = DIV_ROUND_CLOSEST(target, 16 * baud);
  915. continue;
  916. }
  917. entry++;
  918. }
  919. *rate = best_rate;
  920. return best;
  921. }
  922. static int msm_set_baud_rate(struct uart_port *port, unsigned int baud,
  923. unsigned long *saved_flags)
  924. {
  925. unsigned int rxstale, watermark, mask;
  926. struct msm_port *msm_port = to_msm_port(port);
  927. const struct msm_baud_map *entry;
  928. unsigned long flags, rate;
  929. flags = *saved_flags;
  930. spin_unlock_irqrestore(&port->lock, flags);
  931. entry = msm_find_best_baud(port, baud, &rate);
  932. clk_set_rate(msm_port->clk, rate);
  933. baud = rate / 16 / entry->divisor;
  934. spin_lock_irqsave(&port->lock, flags);
  935. *saved_flags = flags;
  936. port->uartclk = rate;
  937. msm_write(port, entry->code, MSM_UART_CSR);
  938. /* RX stale watermark */
  939. rxstale = entry->rxstale;
  940. watermark = MSM_UART_IPR_STALE_LSB & rxstale;
  941. if (msm_port->is_uartdm) {
  942. mask = MSM_UART_DM_IPR_STALE_TIMEOUT_MSB;
  943. } else {
  944. watermark |= MSM_UART_IPR_RXSTALE_LAST;
  945. mask = MSM_UART_IPR_STALE_TIMEOUT_MSB;
  946. }
  947. watermark |= mask & (rxstale << 2);
  948. msm_write(port, watermark, MSM_UART_IPR);
  949. /* set RX watermark */
  950. watermark = (port->fifosize * 3) / 4;
  951. msm_write(port, watermark, MSM_UART_RFWR);
  952. /* set TX watermark */
  953. msm_write(port, 10, MSM_UART_TFWR);
  954. msm_write(port, MSM_UART_CR_CMD_PROTECTION_EN, MSM_UART_CR);
  955. msm_reset(port);
  956. /* Enable RX and TX */
  957. msm_write(port, MSM_UART_CR_TX_ENABLE | MSM_UART_CR_RX_ENABLE, MSM_UART_CR);
  958. /* turn on RX and CTS interrupts */
  959. msm_port->imr = MSM_UART_IMR_RXLEV | MSM_UART_IMR_RXSTALE |
  960. MSM_UART_IMR_CURRENT_CTS | MSM_UART_IMR_RXBREAK_START;
  961. msm_write(port, msm_port->imr, MSM_UART_IMR);
  962. if (msm_port->is_uartdm) {
  963. msm_write(port, MSM_UART_CR_CMD_RESET_STALE_INT, MSM_UART_CR);
  964. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  965. msm_write(port, MSM_UART_CR_CMD_STALE_EVENT_ENABLE, MSM_UART_CR);
  966. }
  967. return baud;
  968. }
  969. static void msm_init_clock(struct uart_port *port)
  970. {
  971. struct msm_port *msm_port = to_msm_port(port);
  972. clk_prepare_enable(msm_port->clk);
  973. clk_prepare_enable(msm_port->pclk);
  974. msm_serial_set_mnd_regs(port);
  975. }
  976. static int msm_startup(struct uart_port *port)
  977. {
  978. struct msm_port *msm_port = to_msm_port(port);
  979. unsigned int data, rfr_level, mask;
  980. int ret;
  981. snprintf(msm_port->name, sizeof(msm_port->name),
  982. "msm_serial%d", port->line);
  983. msm_init_clock(port);
  984. if (likely(port->fifosize > 12))
  985. rfr_level = port->fifosize - 12;
  986. else
  987. rfr_level = port->fifosize;
  988. /* set automatic RFR level */
  989. data = msm_read(port, MSM_UART_MR1);
  990. if (msm_port->is_uartdm)
  991. mask = MSM_UART_DM_MR1_AUTO_RFR_LEVEL1;
  992. else
  993. mask = MSM_UART_MR1_AUTO_RFR_LEVEL1;
  994. data &= ~mask;
  995. data &= ~MSM_UART_MR1_AUTO_RFR_LEVEL0;
  996. data |= mask & (rfr_level << 2);
  997. data |= MSM_UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
  998. msm_write(port, data, MSM_UART_MR1);
  999. if (msm_port->is_uartdm) {
  1000. msm_request_tx_dma(msm_port, msm_port->uart.mapbase);
  1001. msm_request_rx_dma(msm_port, msm_port->uart.mapbase);
  1002. }
  1003. ret = request_irq(port->irq, msm_uart_irq, IRQF_TRIGGER_HIGH,
  1004. msm_port->name, port);
  1005. if (unlikely(ret))
  1006. goto err_irq;
  1007. return 0;
  1008. err_irq:
  1009. if (msm_port->is_uartdm)
  1010. msm_release_dma(msm_port);
  1011. clk_disable_unprepare(msm_port->pclk);
  1012. clk_disable_unprepare(msm_port->clk);
  1013. return ret;
  1014. }
  1015. static void msm_shutdown(struct uart_port *port)
  1016. {
  1017. struct msm_port *msm_port = to_msm_port(port);
  1018. msm_port->imr = 0;
  1019. msm_write(port, 0, MSM_UART_IMR); /* disable interrupts */
  1020. if (msm_port->is_uartdm)
  1021. msm_release_dma(msm_port);
  1022. clk_disable_unprepare(msm_port->clk);
  1023. free_irq(port->irq, port);
  1024. }
  1025. static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
  1026. const struct ktermios *old)
  1027. {
  1028. struct msm_port *msm_port = to_msm_port(port);
  1029. struct msm_dma *dma = &msm_port->rx_dma;
  1030. unsigned long flags;
  1031. unsigned int baud, mr;
  1032. spin_lock_irqsave(&port->lock, flags);
  1033. if (dma->chan) /* Terminate if any */
  1034. msm_stop_dma(port, dma);
  1035. /* calculate and set baud rate */
  1036. baud = uart_get_baud_rate(port, termios, old, 300, 4000000);
  1037. baud = msm_set_baud_rate(port, baud, &flags);
  1038. if (tty_termios_baud_rate(termios))
  1039. tty_termios_encode_baud_rate(termios, baud, baud);
  1040. /* calculate parity */
  1041. mr = msm_read(port, MSM_UART_MR2);
  1042. mr &= ~MSM_UART_MR2_PARITY_MODE;
  1043. if (termios->c_cflag & PARENB) {
  1044. if (termios->c_cflag & PARODD)
  1045. mr |= MSM_UART_MR2_PARITY_MODE_ODD;
  1046. else if (termios->c_cflag & CMSPAR)
  1047. mr |= MSM_UART_MR2_PARITY_MODE_SPACE;
  1048. else
  1049. mr |= MSM_UART_MR2_PARITY_MODE_EVEN;
  1050. }
  1051. /* calculate bits per char */
  1052. mr &= ~MSM_UART_MR2_BITS_PER_CHAR;
  1053. switch (termios->c_cflag & CSIZE) {
  1054. case CS5:
  1055. mr |= MSM_UART_MR2_BITS_PER_CHAR_5;
  1056. break;
  1057. case CS6:
  1058. mr |= MSM_UART_MR2_BITS_PER_CHAR_6;
  1059. break;
  1060. case CS7:
  1061. mr |= MSM_UART_MR2_BITS_PER_CHAR_7;
  1062. break;
  1063. case CS8:
  1064. default:
  1065. mr |= MSM_UART_MR2_BITS_PER_CHAR_8;
  1066. break;
  1067. }
  1068. /* calculate stop bits */
  1069. mr &= ~(MSM_UART_MR2_STOP_BIT_LEN_ONE | MSM_UART_MR2_STOP_BIT_LEN_TWO);
  1070. if (termios->c_cflag & CSTOPB)
  1071. mr |= MSM_UART_MR2_STOP_BIT_LEN_TWO;
  1072. else
  1073. mr |= MSM_UART_MR2_STOP_BIT_LEN_ONE;
  1074. /* set parity, bits per char, and stop bit */
  1075. msm_write(port, mr, MSM_UART_MR2);
  1076. /* calculate and set hardware flow control */
  1077. mr = msm_read(port, MSM_UART_MR1);
  1078. mr &= ~(MSM_UART_MR1_CTS_CTL | MSM_UART_MR1_RX_RDY_CTL);
  1079. if (termios->c_cflag & CRTSCTS) {
  1080. mr |= MSM_UART_MR1_CTS_CTL;
  1081. mr |= MSM_UART_MR1_RX_RDY_CTL;
  1082. }
  1083. msm_write(port, mr, MSM_UART_MR1);
  1084. /* Configure status bits to ignore based on termio flags. */
  1085. port->read_status_mask = 0;
  1086. if (termios->c_iflag & INPCK)
  1087. port->read_status_mask |= MSM_UART_SR_PAR_FRAME_ERR;
  1088. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1089. port->read_status_mask |= MSM_UART_SR_RX_BREAK;
  1090. uart_update_timeout(port, termios->c_cflag, baud);
  1091. /* Try to use DMA */
  1092. msm_start_rx_dma(msm_port);
  1093. spin_unlock_irqrestore(&port->lock, flags);
  1094. }
  1095. static const char *msm_type(struct uart_port *port)
  1096. {
  1097. return "MSM";
  1098. }
  1099. static void msm_release_port(struct uart_port *port)
  1100. {
  1101. struct platform_device *pdev = to_platform_device(port->dev);
  1102. struct resource *uart_resource;
  1103. resource_size_t size;
  1104. uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1105. if (unlikely(!uart_resource))
  1106. return;
  1107. size = resource_size(uart_resource);
  1108. release_mem_region(port->mapbase, size);
  1109. iounmap(port->membase);
  1110. port->membase = NULL;
  1111. }
  1112. static int msm_request_port(struct uart_port *port)
  1113. {
  1114. struct platform_device *pdev = to_platform_device(port->dev);
  1115. struct resource *uart_resource;
  1116. resource_size_t size;
  1117. int ret;
  1118. uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1119. if (unlikely(!uart_resource))
  1120. return -ENXIO;
  1121. size = resource_size(uart_resource);
  1122. if (!request_mem_region(port->mapbase, size, "msm_serial"))
  1123. return -EBUSY;
  1124. port->membase = ioremap(port->mapbase, size);
  1125. if (!port->membase) {
  1126. ret = -EBUSY;
  1127. goto fail_release_port;
  1128. }
  1129. return 0;
  1130. fail_release_port:
  1131. release_mem_region(port->mapbase, size);
  1132. return ret;
  1133. }
  1134. static void msm_config_port(struct uart_port *port, int flags)
  1135. {
  1136. int ret;
  1137. if (flags & UART_CONFIG_TYPE) {
  1138. port->type = PORT_MSM;
  1139. ret = msm_request_port(port);
  1140. if (ret)
  1141. return;
  1142. }
  1143. }
  1144. static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
  1145. {
  1146. if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
  1147. return -EINVAL;
  1148. if (unlikely(port->irq != ser->irq))
  1149. return -EINVAL;
  1150. return 0;
  1151. }
  1152. static void msm_power(struct uart_port *port, unsigned int state,
  1153. unsigned int oldstate)
  1154. {
  1155. struct msm_port *msm_port = to_msm_port(port);
  1156. switch (state) {
  1157. case 0:
  1158. clk_prepare_enable(msm_port->clk);
  1159. clk_prepare_enable(msm_port->pclk);
  1160. break;
  1161. case 3:
  1162. clk_disable_unprepare(msm_port->clk);
  1163. clk_disable_unprepare(msm_port->pclk);
  1164. break;
  1165. default:
  1166. pr_err("msm_serial: Unknown PM state %d\n", state);
  1167. }
  1168. }
  1169. #ifdef CONFIG_CONSOLE_POLL
  1170. static int msm_poll_get_char_single(struct uart_port *port)
  1171. {
  1172. struct msm_port *msm_port = to_msm_port(port);
  1173. unsigned int rf_reg = msm_port->is_uartdm ? UARTDM_RF : MSM_UART_RF;
  1174. if (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_RX_READY))
  1175. return NO_POLL_CHAR;
  1176. return msm_read(port, rf_reg) & 0xff;
  1177. }
  1178. static int msm_poll_get_char_dm(struct uart_port *port)
  1179. {
  1180. int c;
  1181. static u32 slop;
  1182. static int count;
  1183. unsigned char *sp = (unsigned char *)&slop;
  1184. /* Check if a previous read had more than one char */
  1185. if (count) {
  1186. c = sp[sizeof(slop) - count];
  1187. count--;
  1188. /* Or if FIFO is empty */
  1189. } else if (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_RX_READY)) {
  1190. /*
  1191. * If RX packing buffer has less than a word, force stale to
  1192. * push contents into RX FIFO
  1193. */
  1194. count = msm_read(port, UARTDM_RXFS);
  1195. count = (count >> UARTDM_RXFS_BUF_SHIFT) & UARTDM_RXFS_BUF_MASK;
  1196. if (count) {
  1197. msm_write(port, MSM_UART_CR_CMD_FORCE_STALE, MSM_UART_CR);
  1198. slop = msm_read(port, UARTDM_RF);
  1199. c = sp[0];
  1200. count--;
  1201. msm_write(port, MSM_UART_CR_CMD_RESET_STALE_INT, MSM_UART_CR);
  1202. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  1203. msm_write(port, MSM_UART_CR_CMD_STALE_EVENT_ENABLE, MSM_UART_CR);
  1204. } else {
  1205. c = NO_POLL_CHAR;
  1206. }
  1207. /* FIFO has a word */
  1208. } else {
  1209. slop = msm_read(port, UARTDM_RF);
  1210. c = sp[0];
  1211. count = sizeof(slop) - 1;
  1212. }
  1213. return c;
  1214. }
  1215. static int msm_poll_get_char(struct uart_port *port)
  1216. {
  1217. u32 imr;
  1218. int c;
  1219. struct msm_port *msm_port = to_msm_port(port);
  1220. /* Disable all interrupts */
  1221. imr = msm_read(port, MSM_UART_IMR);
  1222. msm_write(port, 0, MSM_UART_IMR);
  1223. if (msm_port->is_uartdm)
  1224. c = msm_poll_get_char_dm(port);
  1225. else
  1226. c = msm_poll_get_char_single(port);
  1227. /* Enable interrupts */
  1228. msm_write(port, imr, MSM_UART_IMR);
  1229. return c;
  1230. }
  1231. static void msm_poll_put_char(struct uart_port *port, unsigned char c)
  1232. {
  1233. u32 imr;
  1234. struct msm_port *msm_port = to_msm_port(port);
  1235. /* Disable all interrupts */
  1236. imr = msm_read(port, MSM_UART_IMR);
  1237. msm_write(port, 0, MSM_UART_IMR);
  1238. if (msm_port->is_uartdm)
  1239. msm_reset_dm_count(port, 1);
  1240. /* Wait until FIFO is empty */
  1241. while (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_READY))
  1242. cpu_relax();
  1243. /* Write a character */
  1244. msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : MSM_UART_TF);
  1245. /* Wait until FIFO is empty */
  1246. while (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_READY))
  1247. cpu_relax();
  1248. /* Enable interrupts */
  1249. msm_write(port, imr, MSM_UART_IMR);
  1250. }
  1251. #endif
  1252. static const struct uart_ops msm_uart_pops = {
  1253. .tx_empty = msm_tx_empty,
  1254. .set_mctrl = msm_set_mctrl,
  1255. .get_mctrl = msm_get_mctrl,
  1256. .stop_tx = msm_stop_tx,
  1257. .start_tx = msm_start_tx,
  1258. .stop_rx = msm_stop_rx,
  1259. .enable_ms = msm_enable_ms,
  1260. .break_ctl = msm_break_ctl,
  1261. .startup = msm_startup,
  1262. .shutdown = msm_shutdown,
  1263. .set_termios = msm_set_termios,
  1264. .type = msm_type,
  1265. .release_port = msm_release_port,
  1266. .request_port = msm_request_port,
  1267. .config_port = msm_config_port,
  1268. .verify_port = msm_verify_port,
  1269. .pm = msm_power,
  1270. #ifdef CONFIG_CONSOLE_POLL
  1271. .poll_get_char = msm_poll_get_char,
  1272. .poll_put_char = msm_poll_put_char,
  1273. #endif
  1274. };
  1275. static struct msm_port msm_uart_ports[] = {
  1276. {
  1277. .uart = {
  1278. .iotype = UPIO_MEM,
  1279. .ops = &msm_uart_pops,
  1280. .flags = UPF_BOOT_AUTOCONF,
  1281. .fifosize = 64,
  1282. .line = 0,
  1283. },
  1284. },
  1285. {
  1286. .uart = {
  1287. .iotype = UPIO_MEM,
  1288. .ops = &msm_uart_pops,
  1289. .flags = UPF_BOOT_AUTOCONF,
  1290. .fifosize = 64,
  1291. .line = 1,
  1292. },
  1293. },
  1294. {
  1295. .uart = {
  1296. .iotype = UPIO_MEM,
  1297. .ops = &msm_uart_pops,
  1298. .flags = UPF_BOOT_AUTOCONF,
  1299. .fifosize = 64,
  1300. .line = 2,
  1301. },
  1302. },
  1303. };
  1304. #define MSM_UART_NR ARRAY_SIZE(msm_uart_ports)
  1305. static inline struct uart_port *msm_get_port_from_line(unsigned int line)
  1306. {
  1307. return &msm_uart_ports[line].uart;
  1308. }
  1309. #ifdef CONFIG_SERIAL_MSM_CONSOLE
  1310. static void __msm_console_write(struct uart_port *port, const char *s,
  1311. unsigned int count, bool is_uartdm)
  1312. {
  1313. unsigned long flags;
  1314. int i;
  1315. int num_newlines = 0;
  1316. bool replaced = false;
  1317. void __iomem *tf;
  1318. int locked = 1;
  1319. if (is_uartdm)
  1320. tf = port->membase + UARTDM_TF;
  1321. else
  1322. tf = port->membase + MSM_UART_TF;
  1323. /* Account for newlines that will get a carriage return added */
  1324. for (i = 0; i < count; i++)
  1325. if (s[i] == '\n')
  1326. num_newlines++;
  1327. count += num_newlines;
  1328. local_irq_save(flags);
  1329. if (port->sysrq)
  1330. locked = 0;
  1331. else if (oops_in_progress)
  1332. locked = spin_trylock(&port->lock);
  1333. else
  1334. spin_lock(&port->lock);
  1335. if (is_uartdm)
  1336. msm_reset_dm_count(port, count);
  1337. i = 0;
  1338. while (i < count) {
  1339. int j;
  1340. unsigned int num_chars;
  1341. char buf[4] = { 0 };
  1342. if (is_uartdm)
  1343. num_chars = min(count - i, (unsigned int)sizeof(buf));
  1344. else
  1345. num_chars = 1;
  1346. for (j = 0; j < num_chars; j++) {
  1347. char c = *s;
  1348. if (c == '\n' && !replaced) {
  1349. buf[j] = '\r';
  1350. j++;
  1351. replaced = true;
  1352. }
  1353. if (j < num_chars) {
  1354. buf[j] = c;
  1355. s++;
  1356. replaced = false;
  1357. }
  1358. }
  1359. while (!(msm_read(port, MSM_UART_SR) & MSM_UART_SR_TX_READY))
  1360. cpu_relax();
  1361. iowrite32_rep(tf, buf, 1);
  1362. i += num_chars;
  1363. }
  1364. if (locked)
  1365. spin_unlock(&port->lock);
  1366. local_irq_restore(flags);
  1367. }
  1368. static void msm_console_write(struct console *co, const char *s,
  1369. unsigned int count)
  1370. {
  1371. struct uart_port *port;
  1372. struct msm_port *msm_port;
  1373. BUG_ON(co->index < 0 || co->index >= MSM_UART_NR);
  1374. port = msm_get_port_from_line(co->index);
  1375. msm_port = to_msm_port(port);
  1376. __msm_console_write(port, s, count, msm_port->is_uartdm);
  1377. }
  1378. static int msm_console_setup(struct console *co, char *options)
  1379. {
  1380. struct uart_port *port;
  1381. int baud = 115200;
  1382. int bits = 8;
  1383. int parity = 'n';
  1384. int flow = 'n';
  1385. if (unlikely(co->index >= MSM_UART_NR || co->index < 0))
  1386. return -ENXIO;
  1387. port = msm_get_port_from_line(co->index);
  1388. if (unlikely(!port->membase))
  1389. return -ENXIO;
  1390. msm_init_clock(port);
  1391. if (options)
  1392. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1393. pr_info("msm_serial: console setup on port #%d\n", port->line);
  1394. return uart_set_options(port, co, baud, parity, bits, flow);
  1395. }
  1396. static void
  1397. msm_serial_early_write(struct console *con, const char *s, unsigned n)
  1398. {
  1399. struct earlycon_device *dev = con->data;
  1400. __msm_console_write(&dev->port, s, n, false);
  1401. }
  1402. static int __init
  1403. msm_serial_early_console_setup(struct earlycon_device *device, const char *opt)
  1404. {
  1405. if (!device->port.membase)
  1406. return -ENODEV;
  1407. device->con->write = msm_serial_early_write;
  1408. return 0;
  1409. }
  1410. OF_EARLYCON_DECLARE(msm_serial, "qcom,msm-uart",
  1411. msm_serial_early_console_setup);
  1412. static void
  1413. msm_serial_early_write_dm(struct console *con, const char *s, unsigned n)
  1414. {
  1415. struct earlycon_device *dev = con->data;
  1416. __msm_console_write(&dev->port, s, n, true);
  1417. }
  1418. static int __init
  1419. msm_serial_early_console_setup_dm(struct earlycon_device *device,
  1420. const char *opt)
  1421. {
  1422. if (!device->port.membase)
  1423. return -ENODEV;
  1424. device->con->write = msm_serial_early_write_dm;
  1425. return 0;
  1426. }
  1427. OF_EARLYCON_DECLARE(msm_serial_dm, "qcom,msm-uartdm",
  1428. msm_serial_early_console_setup_dm);
  1429. static struct uart_driver msm_uart_driver;
  1430. static struct console msm_console = {
  1431. .name = "ttyMSM",
  1432. .write = msm_console_write,
  1433. .device = uart_console_device,
  1434. .setup = msm_console_setup,
  1435. .flags = CON_PRINTBUFFER,
  1436. .index = -1,
  1437. .data = &msm_uart_driver,
  1438. };
  1439. #define MSM_CONSOLE (&msm_console)
  1440. #else
  1441. #define MSM_CONSOLE NULL
  1442. #endif
  1443. static struct uart_driver msm_uart_driver = {
  1444. .owner = THIS_MODULE,
  1445. .driver_name = "msm_serial",
  1446. .dev_name = "ttyMSM",
  1447. .nr = MSM_UART_NR,
  1448. .cons = MSM_CONSOLE,
  1449. };
  1450. static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
  1451. static const struct of_device_id msm_uartdm_table[] = {
  1452. { .compatible = "qcom,msm-uartdm-v1.1", .data = (void *)UARTDM_1P1 },
  1453. { .compatible = "qcom,msm-uartdm-v1.2", .data = (void *)UARTDM_1P2 },
  1454. { .compatible = "qcom,msm-uartdm-v1.3", .data = (void *)UARTDM_1P3 },
  1455. { .compatible = "qcom,msm-uartdm-v1.4", .data = (void *)UARTDM_1P4 },
  1456. { }
  1457. };
  1458. static int msm_serial_probe(struct platform_device *pdev)
  1459. {
  1460. struct msm_port *msm_port;
  1461. struct resource *resource;
  1462. struct uart_port *port;
  1463. const struct of_device_id *id;
  1464. int irq, line;
  1465. if (pdev->dev.of_node)
  1466. line = of_alias_get_id(pdev->dev.of_node, "serial");
  1467. else
  1468. line = pdev->id;
  1469. if (line < 0)
  1470. line = atomic_inc_return(&msm_uart_next_id) - 1;
  1471. if (unlikely(line < 0 || line >= MSM_UART_NR))
  1472. return -ENXIO;
  1473. dev_info(&pdev->dev, "msm_serial: detected port #%d\n", line);
  1474. port = msm_get_port_from_line(line);
  1475. port->dev = &pdev->dev;
  1476. msm_port = to_msm_port(port);
  1477. id = of_match_device(msm_uartdm_table, &pdev->dev);
  1478. if (id)
  1479. msm_port->is_uartdm = (unsigned long)id->data;
  1480. else
  1481. msm_port->is_uartdm = 0;
  1482. msm_port->clk = devm_clk_get(&pdev->dev, "core");
  1483. if (IS_ERR(msm_port->clk))
  1484. return PTR_ERR(msm_port->clk);
  1485. if (msm_port->is_uartdm) {
  1486. msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
  1487. if (IS_ERR(msm_port->pclk))
  1488. return PTR_ERR(msm_port->pclk);
  1489. }
  1490. port->uartclk = clk_get_rate(msm_port->clk);
  1491. dev_info(&pdev->dev, "uartclk = %d\n", port->uartclk);
  1492. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1493. if (unlikely(!resource))
  1494. return -ENXIO;
  1495. port->mapbase = resource->start;
  1496. irq = platform_get_irq(pdev, 0);
  1497. if (unlikely(irq < 0))
  1498. return -ENXIO;
  1499. port->irq = irq;
  1500. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MSM_CONSOLE);
  1501. platform_set_drvdata(pdev, port);
  1502. return uart_add_one_port(&msm_uart_driver, port);
  1503. }
  1504. static int msm_serial_remove(struct platform_device *pdev)
  1505. {
  1506. struct uart_port *port = platform_get_drvdata(pdev);
  1507. uart_remove_one_port(&msm_uart_driver, port);
  1508. return 0;
  1509. }
  1510. static const struct of_device_id msm_match_table[] = {
  1511. { .compatible = "qcom,msm-uart" },
  1512. { .compatible = "qcom,msm-uartdm" },
  1513. {}
  1514. };
  1515. MODULE_DEVICE_TABLE(of, msm_match_table);
  1516. static int __maybe_unused msm_serial_suspend(struct device *dev)
  1517. {
  1518. struct msm_port *port = dev_get_drvdata(dev);
  1519. uart_suspend_port(&msm_uart_driver, &port->uart);
  1520. return 0;
  1521. }
  1522. static int __maybe_unused msm_serial_resume(struct device *dev)
  1523. {
  1524. struct msm_port *port = dev_get_drvdata(dev);
  1525. uart_resume_port(&msm_uart_driver, &port->uart);
  1526. return 0;
  1527. }
  1528. static const struct dev_pm_ops msm_serial_dev_pm_ops = {
  1529. SET_SYSTEM_SLEEP_PM_OPS(msm_serial_suspend, msm_serial_resume)
  1530. };
  1531. static struct platform_driver msm_platform_driver = {
  1532. .remove = msm_serial_remove,
  1533. .probe = msm_serial_probe,
  1534. .driver = {
  1535. .name = "msm_serial",
  1536. .pm = &msm_serial_dev_pm_ops,
  1537. .of_match_table = msm_match_table,
  1538. },
  1539. };
  1540. static int __init msm_serial_init(void)
  1541. {
  1542. int ret;
  1543. ret = uart_register_driver(&msm_uart_driver);
  1544. if (unlikely(ret))
  1545. return ret;
  1546. ret = platform_driver_register(&msm_platform_driver);
  1547. if (unlikely(ret))
  1548. uart_unregister_driver(&msm_uart_driver);
  1549. pr_info("msm_serial: driver initialized\n");
  1550. return ret;
  1551. }
  1552. static void __exit msm_serial_exit(void)
  1553. {
  1554. platform_driver_unregister(&msm_platform_driver);
  1555. uart_unregister_driver(&msm_uart_driver);
  1556. }
  1557. module_init(msm_serial_init);
  1558. module_exit(msm_serial_exit);
  1559. MODULE_AUTHOR("Robert Love <[email protected]>");
  1560. MODULE_DESCRIPTION("Driver for msm7x serial device");
  1561. MODULE_LICENSE("GPL");