msm_serial.c 45 KB

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