msm_serial_hs.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. /*
  2. * MSM 7k/8k High speed uart driver
  3. *
  4. * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
  5. * Copyright (c) 2008 Google Inc.
  6. * Modified: Nick Pelly <npelly@google.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  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.
  15. * See the GNU General Public License for more details.
  16. *
  17. * Has optional support for uart power management independent of linux
  18. * suspend/resume:
  19. *
  20. * RX wakeup.
  21. * UART wakeup can be triggered by RX activity (using a wakeup GPIO on the
  22. * UART RX pin). This should only be used if there is not a wakeup
  23. * GPIO on the UART CTS, and the first RX byte is known (for example, with the
  24. * Bluetooth Texas Instruments HCILL protocol), since the first RX byte will
  25. * always be lost. RTS will be asserted even while the UART is off in this mode
  26. * of operation. See msm_serial_hs_platform_data.rx_wakeup_irq.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_core.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/slab.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/irq.h>
  37. #include <linux/io.h>
  38. #include <linux/ioport.h>
  39. #include <linux/kernel.h>
  40. #include <linux/timer.h>
  41. #include <linux/clk.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/dmapool.h>
  46. #include <linux/wait.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/atomic.h>
  49. #include <asm/irq.h>
  50. #include <mach/hardware.h>
  51. #include <mach/dma.h>
  52. #include <linux/platform_data/msm_serial_hs.h>
  53. /* HSUART Registers */
  54. #define UARTDM_MR1_ADDR 0x0
  55. #define UARTDM_MR2_ADDR 0x4
  56. /* Data Mover result codes */
  57. #define RSLT_FIFO_CNTR_BMSK (0xE << 28)
  58. #define RSLT_VLD BIT(1)
  59. /* write only register */
  60. #define UARTDM_CSR_ADDR 0x8
  61. #define UARTDM_CSR_115200 0xFF
  62. #define UARTDM_CSR_57600 0xEE
  63. #define UARTDM_CSR_38400 0xDD
  64. #define UARTDM_CSR_28800 0xCC
  65. #define UARTDM_CSR_19200 0xBB
  66. #define UARTDM_CSR_14400 0xAA
  67. #define UARTDM_CSR_9600 0x99
  68. #define UARTDM_CSR_7200 0x88
  69. #define UARTDM_CSR_4800 0x77
  70. #define UARTDM_CSR_3600 0x66
  71. #define UARTDM_CSR_2400 0x55
  72. #define UARTDM_CSR_1200 0x44
  73. #define UARTDM_CSR_600 0x33
  74. #define UARTDM_CSR_300 0x22
  75. #define UARTDM_CSR_150 0x11
  76. #define UARTDM_CSR_75 0x00
  77. /* write only register */
  78. #define UARTDM_TF_ADDR 0x70
  79. #define UARTDM_TF2_ADDR 0x74
  80. #define UARTDM_TF3_ADDR 0x78
  81. #define UARTDM_TF4_ADDR 0x7C
  82. /* write only register */
  83. #define UARTDM_CR_ADDR 0x10
  84. #define UARTDM_IMR_ADDR 0x14
  85. #define UARTDM_IPR_ADDR 0x18
  86. #define UARTDM_TFWR_ADDR 0x1c
  87. #define UARTDM_RFWR_ADDR 0x20
  88. #define UARTDM_HCR_ADDR 0x24
  89. #define UARTDM_DMRX_ADDR 0x34
  90. #define UARTDM_IRDA_ADDR 0x38
  91. #define UARTDM_DMEN_ADDR 0x3c
  92. /* UART_DM_NO_CHARS_FOR_TX */
  93. #define UARTDM_NCF_TX_ADDR 0x40
  94. #define UARTDM_BADR_ADDR 0x44
  95. #define UARTDM_SIM_CFG_ADDR 0x80
  96. /* Read Only register */
  97. #define UARTDM_SR_ADDR 0x8
  98. /* Read Only register */
  99. #define UARTDM_RF_ADDR 0x70
  100. #define UARTDM_RF2_ADDR 0x74
  101. #define UARTDM_RF3_ADDR 0x78
  102. #define UARTDM_RF4_ADDR 0x7C
  103. /* Read Only register */
  104. #define UARTDM_MISR_ADDR 0x10
  105. /* Read Only register */
  106. #define UARTDM_ISR_ADDR 0x14
  107. #define UARTDM_RX_TOTAL_SNAP_ADDR 0x38
  108. #define UARTDM_RXFS_ADDR 0x50
  109. /* Register field Mask Mapping */
  110. #define UARTDM_SR_PAR_FRAME_BMSK BIT(5)
  111. #define UARTDM_SR_OVERRUN_BMSK BIT(4)
  112. #define UARTDM_SR_TXEMT_BMSK BIT(3)
  113. #define UARTDM_SR_TXRDY_BMSK BIT(2)
  114. #define UARTDM_SR_RXRDY_BMSK BIT(0)
  115. #define UARTDM_CR_TX_DISABLE_BMSK BIT(3)
  116. #define UARTDM_CR_RX_DISABLE_BMSK BIT(1)
  117. #define UARTDM_CR_TX_EN_BMSK BIT(2)
  118. #define UARTDM_CR_RX_EN_BMSK BIT(0)
  119. /* UARTDM_CR channel_comman bit value (register field is bits 8:4) */
  120. #define RESET_RX 0x10
  121. #define RESET_TX 0x20
  122. #define RESET_ERROR_STATUS 0x30
  123. #define RESET_BREAK_INT 0x40
  124. #define START_BREAK 0x50
  125. #define STOP_BREAK 0x60
  126. #define RESET_CTS 0x70
  127. #define RESET_STALE_INT 0x80
  128. #define RFR_LOW 0xD0
  129. #define RFR_HIGH 0xE0
  130. #define CR_PROTECTION_EN 0x100
  131. #define STALE_EVENT_ENABLE 0x500
  132. #define STALE_EVENT_DISABLE 0x600
  133. #define FORCE_STALE_EVENT 0x400
  134. #define CLEAR_TX_READY 0x300
  135. #define RESET_TX_ERROR 0x800
  136. #define RESET_TX_DONE 0x810
  137. #define UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK 0xffffff00
  138. #define UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK 0x3f
  139. #define UARTDM_MR1_CTS_CTL_BMSK 0x40
  140. #define UARTDM_MR1_RX_RDY_CTL_BMSK 0x80
  141. #define UARTDM_MR2_ERROR_MODE_BMSK 0x40
  142. #define UARTDM_MR2_BITS_PER_CHAR_BMSK 0x30
  143. /* bits per character configuration */
  144. #define FIVE_BPC (0 << 4)
  145. #define SIX_BPC (1 << 4)
  146. #define SEVEN_BPC (2 << 4)
  147. #define EIGHT_BPC (3 << 4)
  148. #define UARTDM_MR2_STOP_BIT_LEN_BMSK 0xc
  149. #define STOP_BIT_ONE (1 << 2)
  150. #define STOP_BIT_TWO (3 << 2)
  151. #define UARTDM_MR2_PARITY_MODE_BMSK 0x3
  152. /* Parity configuration */
  153. #define NO_PARITY 0x0
  154. #define EVEN_PARITY 0x1
  155. #define ODD_PARITY 0x2
  156. #define SPACE_PARITY 0x3
  157. #define UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK 0xffffff80
  158. #define UARTDM_IPR_STALE_LSB_BMSK 0x1f
  159. /* These can be used for both ISR and IMR register */
  160. #define UARTDM_ISR_TX_READY_BMSK BIT(7)
  161. #define UARTDM_ISR_CURRENT_CTS_BMSK BIT(6)
  162. #define UARTDM_ISR_DELTA_CTS_BMSK BIT(5)
  163. #define UARTDM_ISR_RXLEV_BMSK BIT(4)
  164. #define UARTDM_ISR_RXSTALE_BMSK BIT(3)
  165. #define UARTDM_ISR_RXBREAK_BMSK BIT(2)
  166. #define UARTDM_ISR_RXHUNT_BMSK BIT(1)
  167. #define UARTDM_ISR_TXLEV_BMSK BIT(0)
  168. /* Field definitions for UART_DM_DMEN*/
  169. #define UARTDM_TX_DM_EN_BMSK 0x1
  170. #define UARTDM_RX_DM_EN_BMSK 0x2
  171. #define UART_FIFOSIZE 64
  172. #define UARTCLK 7372800
  173. /* Rx DMA request states */
  174. enum flush_reason {
  175. FLUSH_NONE,
  176. FLUSH_DATA_READY,
  177. FLUSH_DATA_INVALID, /* values after this indicate invalid data */
  178. FLUSH_IGNORE = FLUSH_DATA_INVALID,
  179. FLUSH_STOP,
  180. FLUSH_SHUTDOWN,
  181. };
  182. /* UART clock states */
  183. enum msm_hs_clk_states_e {
  184. MSM_HS_CLK_PORT_OFF, /* port not in use */
  185. MSM_HS_CLK_OFF, /* clock disabled */
  186. MSM_HS_CLK_REQUEST_OFF, /* disable after TX and RX flushed */
  187. MSM_HS_CLK_ON, /* clock enabled */
  188. };
  189. /* Track the forced RXSTALE flush during clock off sequence.
  190. * These states are only valid during MSM_HS_CLK_REQUEST_OFF */
  191. enum msm_hs_clk_req_off_state_e {
  192. CLK_REQ_OFF_START,
  193. CLK_REQ_OFF_RXSTALE_ISSUED,
  194. CLK_REQ_OFF_FLUSH_ISSUED,
  195. CLK_REQ_OFF_RXSTALE_FLUSHED,
  196. };
  197. /**
  198. * struct msm_hs_tx
  199. * @tx_ready_int_en: ok to dma more tx?
  200. * @dma_in_flight: tx dma in progress
  201. * @xfer: top level DMA command pointer structure
  202. * @command_ptr: third level command struct pointer
  203. * @command_ptr_ptr: second level command list struct pointer
  204. * @mapped_cmd_ptr: DMA view of third level command struct
  205. * @mapped_cmd_ptr_ptr: DMA view of second level command list struct
  206. * @tx_count: number of bytes to transfer in DMA transfer
  207. * @dma_base: DMA view of UART xmit buffer
  208. *
  209. * This structure describes a single Tx DMA transaction. MSM DMA
  210. * commands have two levels of indirection. The top level command
  211. * ptr points to a list of command ptr which in turn points to a
  212. * single DMA 'command'. In our case each Tx transaction consists
  213. * of a single second level pointer pointing to a 'box type' command.
  214. */
  215. struct msm_hs_tx {
  216. unsigned int tx_ready_int_en;
  217. unsigned int dma_in_flight;
  218. struct msm_dmov_cmd xfer;
  219. dmov_box *command_ptr;
  220. u32 *command_ptr_ptr;
  221. dma_addr_t mapped_cmd_ptr;
  222. dma_addr_t mapped_cmd_ptr_ptr;
  223. int tx_count;
  224. dma_addr_t dma_base;
  225. };
  226. /**
  227. * struct msm_hs_rx
  228. * @flush: Rx DMA request state
  229. * @xfer: top level DMA command pointer structure
  230. * @cmdptr_dmaaddr: DMA view of second level command structure
  231. * @command_ptr: third level DMA command pointer structure
  232. * @command_ptr_ptr: second level DMA command list pointer
  233. * @mapped_cmd_ptr: DMA view of the third level command structure
  234. * @wait: wait for DMA completion before shutdown
  235. * @buffer: destination buffer for RX DMA
  236. * @rbuffer: DMA view of buffer
  237. * @pool: dma pool out of which coherent rx buffer is allocated
  238. * @tty_work: private work-queue for tty flip buffer push task
  239. *
  240. * This structure describes a single Rx DMA transaction. Rx DMA
  241. * transactions use box mode DMA commands.
  242. */
  243. struct msm_hs_rx {
  244. enum flush_reason flush;
  245. struct msm_dmov_cmd xfer;
  246. dma_addr_t cmdptr_dmaaddr;
  247. dmov_box *command_ptr;
  248. u32 *command_ptr_ptr;
  249. dma_addr_t mapped_cmd_ptr;
  250. wait_queue_head_t wait;
  251. dma_addr_t rbuffer;
  252. unsigned char *buffer;
  253. struct dma_pool *pool;
  254. struct work_struct tty_work;
  255. };
  256. /**
  257. * struct msm_hs_rx_wakeup
  258. * @irq: IRQ line to be configured as interrupt source on Rx activity
  259. * @ignore: boolean value. 1 = ignore the wakeup interrupt
  260. * @rx_to_inject: extra character to be inserted to Rx tty on wakeup
  261. * @inject_rx: 1 = insert rx_to_inject. 0 = do not insert extra character
  262. *
  263. * This is an optional structure required for UART Rx GPIO IRQ based
  264. * wakeup from low power state. UART wakeup can be triggered by RX activity
  265. * (using a wakeup GPIO on the UART RX pin). This should only be used if
  266. * there is not a wakeup GPIO on the UART CTS, and the first RX byte is
  267. * known (eg., with the Bluetooth Texas Instruments HCILL protocol),
  268. * since the first RX byte will always be lost. RTS will be asserted even
  269. * while the UART is clocked off in this mode of operation.
  270. */
  271. struct msm_hs_rx_wakeup {
  272. int irq; /* < 0 indicates low power wakeup disabled */
  273. unsigned char ignore;
  274. unsigned char inject_rx;
  275. char rx_to_inject;
  276. };
  277. /**
  278. * struct msm_hs_port
  279. * @uport: embedded uart port structure
  280. * @imr_reg: shadow value of UARTDM_IMR
  281. * @clk: uart input clock handle
  282. * @tx: Tx transaction related data structure
  283. * @rx: Rx transaction related data structure
  284. * @dma_tx_channel: Tx DMA command channel
  285. * @dma_rx_channel Rx DMA command channel
  286. * @dma_tx_crci: Tx channel rate control interface number
  287. * @dma_rx_crci: Rx channel rate control interface number
  288. * @clk_off_timer: Timer to poll DMA event completion before clock off
  289. * @clk_off_delay: clk_off_timer poll interval
  290. * @clk_state: overall clock state
  291. * @clk_req_off_state: post flush clock states
  292. * @rx_wakeup: optional rx_wakeup feature related data
  293. * @exit_lpm_cb: optional callback to exit low power mode
  294. *
  295. * Low level serial port structure.
  296. */
  297. struct msm_hs_port {
  298. struct uart_port uport;
  299. unsigned long imr_reg;
  300. struct clk *clk;
  301. struct msm_hs_tx tx;
  302. struct msm_hs_rx rx;
  303. int dma_tx_channel;
  304. int dma_rx_channel;
  305. int dma_tx_crci;
  306. int dma_rx_crci;
  307. struct hrtimer clk_off_timer;
  308. ktime_t clk_off_delay;
  309. enum msm_hs_clk_states_e clk_state;
  310. enum msm_hs_clk_req_off_state_e clk_req_off_state;
  311. struct msm_hs_rx_wakeup rx_wakeup;
  312. void (*exit_lpm_cb)(struct uart_port *);
  313. };
  314. #define MSM_UARTDM_BURST_SIZE 16 /* DM burst size (in bytes) */
  315. #define UARTDM_TX_BUF_SIZE UART_XMIT_SIZE
  316. #define UARTDM_RX_BUF_SIZE 512
  317. #define UARTDM_NR 2
  318. static struct msm_hs_port q_uart_port[UARTDM_NR];
  319. static struct platform_driver msm_serial_hs_platform_driver;
  320. static struct uart_driver msm_hs_driver;
  321. static struct uart_ops msm_hs_ops;
  322. static struct workqueue_struct *msm_hs_workqueue;
  323. #define UARTDM_TO_MSM(uart_port) \
  324. container_of((uart_port), struct msm_hs_port, uport)
  325. static unsigned int use_low_power_rx_wakeup(struct msm_hs_port
  326. *msm_uport)
  327. {
  328. return (msm_uport->rx_wakeup.irq >= 0);
  329. }
  330. static unsigned int msm_hs_read(struct uart_port *uport,
  331. unsigned int offset)
  332. {
  333. return ioread32(uport->membase + offset);
  334. }
  335. static void msm_hs_write(struct uart_port *uport, unsigned int offset,
  336. unsigned int value)
  337. {
  338. iowrite32(value, uport->membase + offset);
  339. }
  340. static void msm_hs_release_port(struct uart_port *port)
  341. {
  342. iounmap(port->membase);
  343. }
  344. static int msm_hs_request_port(struct uart_port *port)
  345. {
  346. port->membase = ioremap(port->mapbase, PAGE_SIZE);
  347. if (unlikely(!port->membase))
  348. return -ENOMEM;
  349. /* configure the CR Protection to Enable */
  350. msm_hs_write(port, UARTDM_CR_ADDR, CR_PROTECTION_EN);
  351. return 0;
  352. }
  353. static int msm_hs_remove(struct platform_device *pdev)
  354. {
  355. struct msm_hs_port *msm_uport;
  356. struct device *dev;
  357. if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
  358. printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
  359. return -EINVAL;
  360. }
  361. msm_uport = &q_uart_port[pdev->id];
  362. dev = msm_uport->uport.dev;
  363. dma_unmap_single(dev, msm_uport->rx.mapped_cmd_ptr, sizeof(dmov_box),
  364. DMA_TO_DEVICE);
  365. dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
  366. msm_uport->rx.rbuffer);
  367. dma_pool_destroy(msm_uport->rx.pool);
  368. dma_unmap_single(dev, msm_uport->rx.cmdptr_dmaaddr, sizeof(u32),
  369. DMA_TO_DEVICE);
  370. dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr_ptr, sizeof(u32),
  371. DMA_TO_DEVICE);
  372. dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr, sizeof(dmov_box),
  373. DMA_TO_DEVICE);
  374. uart_remove_one_port(&msm_hs_driver, &msm_uport->uport);
  375. clk_put(msm_uport->clk);
  376. /* Free the tx resources */
  377. kfree(msm_uport->tx.command_ptr);
  378. kfree(msm_uport->tx.command_ptr_ptr);
  379. /* Free the rx resources */
  380. kfree(msm_uport->rx.command_ptr);
  381. kfree(msm_uport->rx.command_ptr_ptr);
  382. iounmap(msm_uport->uport.membase);
  383. return 0;
  384. }
  385. static int msm_hs_init_clk_locked(struct uart_port *uport)
  386. {
  387. int ret;
  388. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  389. ret = clk_enable(msm_uport->clk);
  390. if (ret) {
  391. printk(KERN_ERR "Error could not turn on UART clk\n");
  392. return ret;
  393. }
  394. /* Set up the MREG/NREG/DREG/MNDREG */
  395. ret = clk_set_rate(msm_uport->clk, uport->uartclk);
  396. if (ret) {
  397. printk(KERN_WARNING "Error setting clock rate on UART\n");
  398. clk_disable(msm_uport->clk);
  399. return ret;
  400. }
  401. msm_uport->clk_state = MSM_HS_CLK_ON;
  402. return 0;
  403. }
  404. /* Enable and Disable clocks (Used for power management) */
  405. static void msm_hs_pm(struct uart_port *uport, unsigned int state,
  406. unsigned int oldstate)
  407. {
  408. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  409. if (use_low_power_rx_wakeup(msm_uport) ||
  410. msm_uport->exit_lpm_cb)
  411. return; /* ignore linux PM states,
  412. use msm_hs_request_clock API */
  413. switch (state) {
  414. case 0:
  415. clk_enable(msm_uport->clk);
  416. break;
  417. case 3:
  418. clk_disable(msm_uport->clk);
  419. break;
  420. default:
  421. dev_err(uport->dev, "msm_serial: Unknown PM state %d\n",
  422. state);
  423. }
  424. }
  425. /*
  426. * programs the UARTDM_CSR register with correct bit rates
  427. *
  428. * Interrupts should be disabled before we are called, as
  429. * we modify Set Baud rate
  430. * Set receive stale interrupt level, dependent on Bit Rate
  431. * Goal is to have around 8 ms before indicate stale.
  432. * roundup (((Bit Rate * .008) / 10) + 1
  433. */
  434. static void msm_hs_set_bps_locked(struct uart_port *uport,
  435. unsigned int bps)
  436. {
  437. unsigned long rxstale;
  438. unsigned long data;
  439. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  440. switch (bps) {
  441. case 300:
  442. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_75);
  443. rxstale = 1;
  444. break;
  445. case 600:
  446. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_150);
  447. rxstale = 1;
  448. break;
  449. case 1200:
  450. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_300);
  451. rxstale = 1;
  452. break;
  453. case 2400:
  454. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_600);
  455. rxstale = 1;
  456. break;
  457. case 4800:
  458. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_1200);
  459. rxstale = 1;
  460. break;
  461. case 9600:
  462. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
  463. rxstale = 2;
  464. break;
  465. case 14400:
  466. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_3600);
  467. rxstale = 3;
  468. break;
  469. case 19200:
  470. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_4800);
  471. rxstale = 4;
  472. break;
  473. case 28800:
  474. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_7200);
  475. rxstale = 6;
  476. break;
  477. case 38400:
  478. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_9600);
  479. rxstale = 8;
  480. break;
  481. case 57600:
  482. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_14400);
  483. rxstale = 16;
  484. break;
  485. case 76800:
  486. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_19200);
  487. rxstale = 16;
  488. break;
  489. case 115200:
  490. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_28800);
  491. rxstale = 31;
  492. break;
  493. case 230400:
  494. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_57600);
  495. rxstale = 31;
  496. break;
  497. case 460800:
  498. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
  499. rxstale = 31;
  500. break;
  501. case 4000000:
  502. case 3686400:
  503. case 3200000:
  504. case 3500000:
  505. case 3000000:
  506. case 2500000:
  507. case 1500000:
  508. case 1152000:
  509. case 1000000:
  510. case 921600:
  511. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
  512. rxstale = 31;
  513. break;
  514. default:
  515. msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
  516. /* default to 9600 */
  517. bps = 9600;
  518. rxstale = 2;
  519. break;
  520. }
  521. if (bps > 460800)
  522. uport->uartclk = bps * 16;
  523. else
  524. uport->uartclk = UARTCLK;
  525. if (clk_set_rate(msm_uport->clk, uport->uartclk)) {
  526. printk(KERN_WARNING "Error setting clock rate on UART\n");
  527. return;
  528. }
  529. data = rxstale & UARTDM_IPR_STALE_LSB_BMSK;
  530. data |= UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK & (rxstale << 2);
  531. msm_hs_write(uport, UARTDM_IPR_ADDR, data);
  532. }
  533. /*
  534. * termios : new ktermios
  535. * oldtermios: old ktermios previous setting
  536. *
  537. * Configure the serial port
  538. */
  539. static void msm_hs_set_termios(struct uart_port *uport,
  540. struct ktermios *termios,
  541. struct ktermios *oldtermios)
  542. {
  543. unsigned int bps;
  544. unsigned long data;
  545. unsigned long flags;
  546. unsigned int c_cflag = termios->c_cflag;
  547. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  548. spin_lock_irqsave(&uport->lock, flags);
  549. clk_enable(msm_uport->clk);
  550. /* 300 is the minimum baud support by the driver */
  551. bps = uart_get_baud_rate(uport, termios, oldtermios, 200, 4000000);
  552. /* Temporary remapping 200 BAUD to 3.2 mbps */
  553. if (bps == 200)
  554. bps = 3200000;
  555. msm_hs_set_bps_locked(uport, bps);
  556. data = msm_hs_read(uport, UARTDM_MR2_ADDR);
  557. data &= ~UARTDM_MR2_PARITY_MODE_BMSK;
  558. /* set parity */
  559. if (PARENB == (c_cflag & PARENB)) {
  560. if (PARODD == (c_cflag & PARODD))
  561. data |= ODD_PARITY;
  562. else if (CMSPAR == (c_cflag & CMSPAR))
  563. data |= SPACE_PARITY;
  564. else
  565. data |= EVEN_PARITY;
  566. }
  567. /* Set bits per char */
  568. data &= ~UARTDM_MR2_BITS_PER_CHAR_BMSK;
  569. switch (c_cflag & CSIZE) {
  570. case CS5:
  571. data |= FIVE_BPC;
  572. break;
  573. case CS6:
  574. data |= SIX_BPC;
  575. break;
  576. case CS7:
  577. data |= SEVEN_BPC;
  578. break;
  579. default:
  580. data |= EIGHT_BPC;
  581. break;
  582. }
  583. /* stop bits */
  584. if (c_cflag & CSTOPB) {
  585. data |= STOP_BIT_TWO;
  586. } else {
  587. /* otherwise 1 stop bit */
  588. data |= STOP_BIT_ONE;
  589. }
  590. data |= UARTDM_MR2_ERROR_MODE_BMSK;
  591. /* write parity/bits per char/stop bit configuration */
  592. msm_hs_write(uport, UARTDM_MR2_ADDR, data);
  593. /* Configure HW flow control */
  594. data = msm_hs_read(uport, UARTDM_MR1_ADDR);
  595. data &= ~(UARTDM_MR1_CTS_CTL_BMSK | UARTDM_MR1_RX_RDY_CTL_BMSK);
  596. if (c_cflag & CRTSCTS) {
  597. data |= UARTDM_MR1_CTS_CTL_BMSK;
  598. data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
  599. }
  600. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  601. uport->ignore_status_mask = termios->c_iflag & INPCK;
  602. uport->ignore_status_mask |= termios->c_iflag & IGNPAR;
  603. uport->read_status_mask = (termios->c_cflag & CREAD);
  604. msm_hs_write(uport, UARTDM_IMR_ADDR, 0);
  605. /* Set Transmit software time out */
  606. uart_update_timeout(uport, c_cflag, bps);
  607. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
  608. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
  609. if (msm_uport->rx.flush == FLUSH_NONE) {
  610. msm_uport->rx.flush = FLUSH_IGNORE;
  611. msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
  612. }
  613. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  614. clk_disable(msm_uport->clk);
  615. spin_unlock_irqrestore(&uport->lock, flags);
  616. }
  617. /*
  618. * Standard API, Transmitter
  619. * Any character in the transmit shift register is sent
  620. */
  621. static unsigned int msm_hs_tx_empty(struct uart_port *uport)
  622. {
  623. unsigned int data;
  624. unsigned int ret = 0;
  625. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  626. clk_enable(msm_uport->clk);
  627. data = msm_hs_read(uport, UARTDM_SR_ADDR);
  628. if (data & UARTDM_SR_TXEMT_BMSK)
  629. ret = TIOCSER_TEMT;
  630. clk_disable(msm_uport->clk);
  631. return ret;
  632. }
  633. /*
  634. * Standard API, Stop transmitter.
  635. * Any character in the transmit shift register is sent as
  636. * well as the current data mover transfer .
  637. */
  638. static void msm_hs_stop_tx_locked(struct uart_port *uport)
  639. {
  640. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  641. msm_uport->tx.tx_ready_int_en = 0;
  642. }
  643. /*
  644. * Standard API, Stop receiver as soon as possible.
  645. *
  646. * Function immediately terminates the operation of the
  647. * channel receiver and any incoming characters are lost. None
  648. * of the receiver status bits are affected by this command and
  649. * characters that are already in the receive FIFO there.
  650. */
  651. static void msm_hs_stop_rx_locked(struct uart_port *uport)
  652. {
  653. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  654. unsigned int data;
  655. clk_enable(msm_uport->clk);
  656. /* disable dlink */
  657. data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
  658. data &= ~UARTDM_RX_DM_EN_BMSK;
  659. msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
  660. /* Disable the receiver */
  661. if (msm_uport->rx.flush == FLUSH_NONE)
  662. msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
  663. if (msm_uport->rx.flush != FLUSH_SHUTDOWN)
  664. msm_uport->rx.flush = FLUSH_STOP;
  665. clk_disable(msm_uport->clk);
  666. }
  667. /* Transmit the next chunk of data */
  668. static void msm_hs_submit_tx_locked(struct uart_port *uport)
  669. {
  670. int left;
  671. int tx_count;
  672. dma_addr_t src_addr;
  673. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  674. struct msm_hs_tx *tx = &msm_uport->tx;
  675. struct circ_buf *tx_buf = &msm_uport->uport.state->xmit;
  676. if (uart_circ_empty(tx_buf) || uport->state->port.tty->stopped) {
  677. msm_hs_stop_tx_locked(uport);
  678. return;
  679. }
  680. tx->dma_in_flight = 1;
  681. tx_count = uart_circ_chars_pending(tx_buf);
  682. if (UARTDM_TX_BUF_SIZE < tx_count)
  683. tx_count = UARTDM_TX_BUF_SIZE;
  684. left = UART_XMIT_SIZE - tx_buf->tail;
  685. if (tx_count > left)
  686. tx_count = left;
  687. src_addr = tx->dma_base + tx_buf->tail;
  688. dma_sync_single_for_device(uport->dev, src_addr, tx_count,
  689. DMA_TO_DEVICE);
  690. tx->command_ptr->num_rows = (((tx_count + 15) >> 4) << 16) |
  691. ((tx_count + 15) >> 4);
  692. tx->command_ptr->src_row_addr = src_addr;
  693. dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr,
  694. sizeof(dmov_box), DMA_TO_DEVICE);
  695. *tx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(tx->mapped_cmd_ptr);
  696. dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr_ptr,
  697. sizeof(u32), DMA_TO_DEVICE);
  698. /* Save tx_count to use in Callback */
  699. tx->tx_count = tx_count;
  700. msm_hs_write(uport, UARTDM_NCF_TX_ADDR, tx_count);
  701. /* Disable the tx_ready interrupt */
  702. msm_uport->imr_reg &= ~UARTDM_ISR_TX_READY_BMSK;
  703. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  704. msm_dmov_enqueue_cmd(msm_uport->dma_tx_channel, &tx->xfer);
  705. }
  706. /* Start to receive the next chunk of data */
  707. static void msm_hs_start_rx_locked(struct uart_port *uport)
  708. {
  709. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  710. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
  711. msm_hs_write(uport, UARTDM_DMRX_ADDR, UARTDM_RX_BUF_SIZE);
  712. msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_ENABLE);
  713. msm_uport->imr_reg |= UARTDM_ISR_RXLEV_BMSK;
  714. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  715. msm_uport->rx.flush = FLUSH_NONE;
  716. msm_dmov_enqueue_cmd(msm_uport->dma_rx_channel, &msm_uport->rx.xfer);
  717. /* might have finished RX and be ready to clock off */
  718. hrtimer_start(&msm_uport->clk_off_timer, msm_uport->clk_off_delay,
  719. HRTIMER_MODE_REL);
  720. }
  721. /* Enable the transmitter Interrupt */
  722. static void msm_hs_start_tx_locked(struct uart_port *uport)
  723. {
  724. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  725. clk_enable(msm_uport->clk);
  726. if (msm_uport->exit_lpm_cb)
  727. msm_uport->exit_lpm_cb(uport);
  728. if (msm_uport->tx.tx_ready_int_en == 0) {
  729. msm_uport->tx.tx_ready_int_en = 1;
  730. msm_hs_submit_tx_locked(uport);
  731. }
  732. clk_disable(msm_uport->clk);
  733. }
  734. /*
  735. * This routine is called when we are done with a DMA transfer
  736. *
  737. * This routine is registered with Data mover when we set
  738. * up a Data Mover transfer. It is called from Data mover ISR
  739. * when the DMA transfer is done.
  740. */
  741. static void msm_hs_dmov_tx_callback(struct msm_dmov_cmd *cmd_ptr,
  742. unsigned int result,
  743. struct msm_dmov_errdata *err)
  744. {
  745. unsigned long flags;
  746. struct msm_hs_port *msm_uport;
  747. /* DMA did not finish properly */
  748. WARN_ON((((result & RSLT_FIFO_CNTR_BMSK) >> 28) == 1) &&
  749. !(result & RSLT_VLD));
  750. msm_uport = container_of(cmd_ptr, struct msm_hs_port, tx.xfer);
  751. spin_lock_irqsave(&msm_uport->uport.lock, flags);
  752. clk_enable(msm_uport->clk);
  753. msm_uport->imr_reg |= UARTDM_ISR_TX_READY_BMSK;
  754. msm_hs_write(&msm_uport->uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  755. clk_disable(msm_uport->clk);
  756. spin_unlock_irqrestore(&msm_uport->uport.lock, flags);
  757. }
  758. /*
  759. * This routine is called when we are done with a DMA transfer or the
  760. * a flush has been sent to the data mover driver.
  761. *
  762. * This routine is registered with Data mover when we set up a Data Mover
  763. * transfer. It is called from Data mover ISR when the DMA transfer is done.
  764. */
  765. static void msm_hs_dmov_rx_callback(struct msm_dmov_cmd *cmd_ptr,
  766. unsigned int result,
  767. struct msm_dmov_errdata *err)
  768. {
  769. int retval;
  770. int rx_count;
  771. unsigned long status;
  772. unsigned int error_f = 0;
  773. unsigned long flags;
  774. unsigned int flush;
  775. struct tty_struct *tty;
  776. struct tty_port *port;
  777. struct uart_port *uport;
  778. struct msm_hs_port *msm_uport;
  779. msm_uport = container_of(cmd_ptr, struct msm_hs_port, rx.xfer);
  780. uport = &msm_uport->uport;
  781. spin_lock_irqsave(&uport->lock, flags);
  782. clk_enable(msm_uport->clk);
  783. port = &uport->state->port;
  784. tty = port->tty;
  785. msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
  786. status = msm_hs_read(uport, UARTDM_SR_ADDR);
  787. /* overflow is not connect to data in a FIFO */
  788. if (unlikely((status & UARTDM_SR_OVERRUN_BMSK) &&
  789. (uport->read_status_mask & CREAD))) {
  790. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  791. uport->icount.buf_overrun++;
  792. error_f = 1;
  793. }
  794. if (!(uport->ignore_status_mask & INPCK))
  795. status = status & ~(UARTDM_SR_PAR_FRAME_BMSK);
  796. if (unlikely(status & UARTDM_SR_PAR_FRAME_BMSK)) {
  797. /* Can not tell difference between parity & frame error */
  798. uport->icount.parity++;
  799. error_f = 1;
  800. if (uport->ignore_status_mask & IGNPAR)
  801. tty_insert_flip_char(port, 0, TTY_PARITY);
  802. }
  803. if (error_f)
  804. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
  805. if (msm_uport->clk_req_off_state == CLK_REQ_OFF_FLUSH_ISSUED)
  806. msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_FLUSHED;
  807. flush = msm_uport->rx.flush;
  808. if (flush == FLUSH_IGNORE)
  809. msm_hs_start_rx_locked(uport);
  810. if (flush == FLUSH_STOP)
  811. msm_uport->rx.flush = FLUSH_SHUTDOWN;
  812. if (flush >= FLUSH_DATA_INVALID)
  813. goto out;
  814. rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);
  815. if (0 != (uport->read_status_mask & CREAD)) {
  816. retval = tty_insert_flip_string(port, msm_uport->rx.buffer,
  817. rx_count);
  818. BUG_ON(retval != rx_count);
  819. }
  820. msm_hs_start_rx_locked(uport);
  821. out:
  822. clk_disable(msm_uport->clk);
  823. spin_unlock_irqrestore(&uport->lock, flags);
  824. if (flush < FLUSH_DATA_INVALID)
  825. queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
  826. }
  827. static void msm_hs_tty_flip_buffer_work(struct work_struct *work)
  828. {
  829. struct msm_hs_port *msm_uport =
  830. container_of(work, struct msm_hs_port, rx.tty_work);
  831. tty_flip_buffer_push(&msm_uport->uport.state->port);
  832. }
  833. /*
  834. * Standard API, Current states of modem control inputs
  835. *
  836. * Since CTS can be handled entirely by HARDWARE we always
  837. * indicate clear to send and count on the TX FIFO to block when
  838. * it fills up.
  839. *
  840. * - TIOCM_DCD
  841. * - TIOCM_CTS
  842. * - TIOCM_DSR
  843. * - TIOCM_RI
  844. * (Unsupported) DCD and DSR will return them high. RI will return low.
  845. */
  846. static unsigned int msm_hs_get_mctrl_locked(struct uart_port *uport)
  847. {
  848. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  849. }
  850. /*
  851. * True enables UART auto RFR, which indicates we are ready for data if the RX
  852. * buffer is not full. False disables auto RFR, and deasserts RFR to indicate
  853. * we are not ready for data. Must be called with UART clock on.
  854. */
  855. static void set_rfr_locked(struct uart_port *uport, int auto_rfr)
  856. {
  857. unsigned int data;
  858. data = msm_hs_read(uport, UARTDM_MR1_ADDR);
  859. if (auto_rfr) {
  860. /* enable auto ready-for-receiving */
  861. data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
  862. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  863. } else {
  864. /* disable auto ready-for-receiving */
  865. data &= ~UARTDM_MR1_RX_RDY_CTL_BMSK;
  866. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  867. /* RFR is active low, set high */
  868. msm_hs_write(uport, UARTDM_CR_ADDR, RFR_HIGH);
  869. }
  870. }
  871. /*
  872. * Standard API, used to set or clear RFR
  873. */
  874. static void msm_hs_set_mctrl_locked(struct uart_port *uport,
  875. unsigned int mctrl)
  876. {
  877. unsigned int auto_rfr;
  878. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  879. clk_enable(msm_uport->clk);
  880. auto_rfr = TIOCM_RTS & mctrl ? 1 : 0;
  881. set_rfr_locked(uport, auto_rfr);
  882. clk_disable(msm_uport->clk);
  883. }
  884. /* Standard API, Enable modem status (CTS) interrupt */
  885. static void msm_hs_enable_ms_locked(struct uart_port *uport)
  886. {
  887. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  888. clk_enable(msm_uport->clk);
  889. /* Enable DELTA_CTS Interrupt */
  890. msm_uport->imr_reg |= UARTDM_ISR_DELTA_CTS_BMSK;
  891. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  892. clk_disable(msm_uport->clk);
  893. }
  894. /*
  895. * Standard API, Break Signal
  896. *
  897. * Control the transmission of a break signal. ctl eq 0 => break
  898. * signal terminate ctl ne 0 => start break signal
  899. */
  900. static void msm_hs_break_ctl(struct uart_port *uport, int ctl)
  901. {
  902. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  903. clk_enable(msm_uport->clk);
  904. msm_hs_write(uport, UARTDM_CR_ADDR, ctl ? START_BREAK : STOP_BREAK);
  905. clk_disable(msm_uport->clk);
  906. }
  907. static void msm_hs_config_port(struct uart_port *uport, int cfg_flags)
  908. {
  909. unsigned long flags;
  910. spin_lock_irqsave(&uport->lock, flags);
  911. if (cfg_flags & UART_CONFIG_TYPE) {
  912. uport->type = PORT_MSM;
  913. msm_hs_request_port(uport);
  914. }
  915. spin_unlock_irqrestore(&uport->lock, flags);
  916. }
  917. /* Handle CTS changes (Called from interrupt handler) */
  918. static void msm_hs_handle_delta_cts_locked(struct uart_port *uport)
  919. {
  920. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  921. clk_enable(msm_uport->clk);
  922. /* clear interrupt */
  923. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
  924. uport->icount.cts++;
  925. clk_disable(msm_uport->clk);
  926. /* clear the IOCTL TIOCMIWAIT if called */
  927. wake_up_interruptible(&uport->state->port.delta_msr_wait);
  928. }
  929. /* check if the TX path is flushed, and if so clock off
  930. * returns 0 did not clock off, need to retry (still sending final byte)
  931. * -1 did not clock off, do not retry
  932. * 1 if we clocked off
  933. */
  934. static int msm_hs_check_clock_off_locked(struct uart_port *uport)
  935. {
  936. unsigned long sr_status;
  937. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  938. struct circ_buf *tx_buf = &uport->state->xmit;
  939. /* Cancel if tx tty buffer is not empty, dma is in flight,
  940. * or tx fifo is not empty, or rx fifo is not empty */
  941. if (msm_uport->clk_state != MSM_HS_CLK_REQUEST_OFF ||
  942. !uart_circ_empty(tx_buf) || msm_uport->tx.dma_in_flight ||
  943. (msm_uport->imr_reg & UARTDM_ISR_TXLEV_BMSK) ||
  944. !(msm_uport->imr_reg & UARTDM_ISR_RXLEV_BMSK)) {
  945. return -1;
  946. }
  947. /* Make sure the uart is finished with the last byte */
  948. sr_status = msm_hs_read(uport, UARTDM_SR_ADDR);
  949. if (!(sr_status & UARTDM_SR_TXEMT_BMSK))
  950. return 0; /* retry */
  951. /* Make sure forced RXSTALE flush complete */
  952. switch (msm_uport->clk_req_off_state) {
  953. case CLK_REQ_OFF_START:
  954. msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_ISSUED;
  955. msm_hs_write(uport, UARTDM_CR_ADDR, FORCE_STALE_EVENT);
  956. return 0; /* RXSTALE flush not complete - retry */
  957. case CLK_REQ_OFF_RXSTALE_ISSUED:
  958. case CLK_REQ_OFF_FLUSH_ISSUED:
  959. return 0; /* RXSTALE flush not complete - retry */
  960. case CLK_REQ_OFF_RXSTALE_FLUSHED:
  961. break; /* continue */
  962. }
  963. if (msm_uport->rx.flush != FLUSH_SHUTDOWN) {
  964. if (msm_uport->rx.flush == FLUSH_NONE)
  965. msm_hs_stop_rx_locked(uport);
  966. return 0; /* come back later to really clock off */
  967. }
  968. /* we really want to clock off */
  969. clk_disable(msm_uport->clk);
  970. msm_uport->clk_state = MSM_HS_CLK_OFF;
  971. if (use_low_power_rx_wakeup(msm_uport)) {
  972. msm_uport->rx_wakeup.ignore = 1;
  973. enable_irq(msm_uport->rx_wakeup.irq);
  974. }
  975. return 1;
  976. }
  977. static enum hrtimer_restart msm_hs_clk_off_retry(struct hrtimer *timer)
  978. {
  979. unsigned long flags;
  980. int ret = HRTIMER_NORESTART;
  981. struct msm_hs_port *msm_uport = container_of(timer, struct msm_hs_port,
  982. clk_off_timer);
  983. struct uart_port *uport = &msm_uport->uport;
  984. spin_lock_irqsave(&uport->lock, flags);
  985. if (!msm_hs_check_clock_off_locked(uport)) {
  986. hrtimer_forward_now(timer, msm_uport->clk_off_delay);
  987. ret = HRTIMER_RESTART;
  988. }
  989. spin_unlock_irqrestore(&uport->lock, flags);
  990. return ret;
  991. }
  992. static irqreturn_t msm_hs_isr(int irq, void *dev)
  993. {
  994. unsigned long flags;
  995. unsigned long isr_status;
  996. struct msm_hs_port *msm_uport = dev;
  997. struct uart_port *uport = &msm_uport->uport;
  998. struct circ_buf *tx_buf = &uport->state->xmit;
  999. struct msm_hs_tx *tx = &msm_uport->tx;
  1000. struct msm_hs_rx *rx = &msm_uport->rx;
  1001. spin_lock_irqsave(&uport->lock, flags);
  1002. isr_status = msm_hs_read(uport, UARTDM_MISR_ADDR);
  1003. /* Uart RX starting */
  1004. if (isr_status & UARTDM_ISR_RXLEV_BMSK) {
  1005. msm_uport->imr_reg &= ~UARTDM_ISR_RXLEV_BMSK;
  1006. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1007. }
  1008. /* Stale rx interrupt */
  1009. if (isr_status & UARTDM_ISR_RXSTALE_BMSK) {
  1010. msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
  1011. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
  1012. if (msm_uport->clk_req_off_state == CLK_REQ_OFF_RXSTALE_ISSUED)
  1013. msm_uport->clk_req_off_state =
  1014. CLK_REQ_OFF_FLUSH_ISSUED;
  1015. if (rx->flush == FLUSH_NONE) {
  1016. rx->flush = FLUSH_DATA_READY;
  1017. msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
  1018. }
  1019. }
  1020. /* tx ready interrupt */
  1021. if (isr_status & UARTDM_ISR_TX_READY_BMSK) {
  1022. /* Clear TX Ready */
  1023. msm_hs_write(uport, UARTDM_CR_ADDR, CLEAR_TX_READY);
  1024. if (msm_uport->clk_state == MSM_HS_CLK_REQUEST_OFF) {
  1025. msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
  1026. msm_hs_write(uport, UARTDM_IMR_ADDR,
  1027. msm_uport->imr_reg);
  1028. }
  1029. /* Complete DMA TX transactions and submit new transactions */
  1030. tx_buf->tail = (tx_buf->tail + tx->tx_count) & ~UART_XMIT_SIZE;
  1031. tx->dma_in_flight = 0;
  1032. uport->icount.tx += tx->tx_count;
  1033. if (tx->tx_ready_int_en)
  1034. msm_hs_submit_tx_locked(uport);
  1035. if (uart_circ_chars_pending(tx_buf) < WAKEUP_CHARS)
  1036. uart_write_wakeup(uport);
  1037. }
  1038. if (isr_status & UARTDM_ISR_TXLEV_BMSK) {
  1039. /* TX FIFO is empty */
  1040. msm_uport->imr_reg &= ~UARTDM_ISR_TXLEV_BMSK;
  1041. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1042. if (!msm_hs_check_clock_off_locked(uport))
  1043. hrtimer_start(&msm_uport->clk_off_timer,
  1044. msm_uport->clk_off_delay,
  1045. HRTIMER_MODE_REL);
  1046. }
  1047. /* Change in CTS interrupt */
  1048. if (isr_status & UARTDM_ISR_DELTA_CTS_BMSK)
  1049. msm_hs_handle_delta_cts_locked(uport);
  1050. spin_unlock_irqrestore(&uport->lock, flags);
  1051. return IRQ_HANDLED;
  1052. }
  1053. void msm_hs_request_clock_off_locked(struct uart_port *uport)
  1054. {
  1055. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1056. if (msm_uport->clk_state == MSM_HS_CLK_ON) {
  1057. msm_uport->clk_state = MSM_HS_CLK_REQUEST_OFF;
  1058. msm_uport->clk_req_off_state = CLK_REQ_OFF_START;
  1059. if (!use_low_power_rx_wakeup(msm_uport))
  1060. set_rfr_locked(uport, 0);
  1061. msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
  1062. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1063. }
  1064. }
  1065. /**
  1066. * msm_hs_request_clock_off - request to (i.e. asynchronously) turn off uart
  1067. * clock once pending TX is flushed and Rx DMA command is terminated.
  1068. * @uport: uart_port structure for the device instance.
  1069. *
  1070. * This functions puts the device into a partially active low power mode. It
  1071. * waits to complete all pending tx transactions, flushes ongoing Rx DMA
  1072. * command and terminates UART side Rx transaction, puts UART HW in non DMA
  1073. * mode and then clocks off the device. A client calls this when no UART
  1074. * data is expected. msm_request_clock_on() must be called before any further
  1075. * UART can be sent or received.
  1076. */
  1077. void msm_hs_request_clock_off(struct uart_port *uport)
  1078. {
  1079. unsigned long flags;
  1080. spin_lock_irqsave(&uport->lock, flags);
  1081. msm_hs_request_clock_off_locked(uport);
  1082. spin_unlock_irqrestore(&uport->lock, flags);
  1083. }
  1084. void msm_hs_request_clock_on_locked(struct uart_port *uport)
  1085. {
  1086. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1087. unsigned int data;
  1088. switch (msm_uport->clk_state) {
  1089. case MSM_HS_CLK_OFF:
  1090. clk_enable(msm_uport->clk);
  1091. disable_irq_nosync(msm_uport->rx_wakeup.irq);
  1092. /* fall-through */
  1093. case MSM_HS_CLK_REQUEST_OFF:
  1094. if (msm_uport->rx.flush == FLUSH_STOP ||
  1095. msm_uport->rx.flush == FLUSH_SHUTDOWN) {
  1096. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
  1097. data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
  1098. data |= UARTDM_RX_DM_EN_BMSK;
  1099. msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
  1100. }
  1101. hrtimer_try_to_cancel(&msm_uport->clk_off_timer);
  1102. if (msm_uport->rx.flush == FLUSH_SHUTDOWN)
  1103. msm_hs_start_rx_locked(uport);
  1104. if (!use_low_power_rx_wakeup(msm_uport))
  1105. set_rfr_locked(uport, 1);
  1106. if (msm_uport->rx.flush == FLUSH_STOP)
  1107. msm_uport->rx.flush = FLUSH_IGNORE;
  1108. msm_uport->clk_state = MSM_HS_CLK_ON;
  1109. break;
  1110. case MSM_HS_CLK_ON:
  1111. break;
  1112. case MSM_HS_CLK_PORT_OFF:
  1113. break;
  1114. }
  1115. }
  1116. /**
  1117. * msm_hs_request_clock_on - Switch the device from partially active low
  1118. * power mode to fully active (i.e. clock on) mode.
  1119. * @uport: uart_port structure for the device.
  1120. *
  1121. * This function switches on the input clock, puts UART HW into DMA mode
  1122. * and enqueues an Rx DMA command if the device was in partially active
  1123. * mode. It has no effect if called with the device in inactive state.
  1124. */
  1125. void msm_hs_request_clock_on(struct uart_port *uport)
  1126. {
  1127. unsigned long flags;
  1128. spin_lock_irqsave(&uport->lock, flags);
  1129. msm_hs_request_clock_on_locked(uport);
  1130. spin_unlock_irqrestore(&uport->lock, flags);
  1131. }
  1132. static irqreturn_t msm_hs_rx_wakeup_isr(int irq, void *dev)
  1133. {
  1134. unsigned int wakeup = 0;
  1135. unsigned long flags;
  1136. struct msm_hs_port *msm_uport = dev;
  1137. struct uart_port *uport = &msm_uport->uport;
  1138. spin_lock_irqsave(&uport->lock, flags);
  1139. if (msm_uport->clk_state == MSM_HS_CLK_OFF) {
  1140. /* ignore the first irq - it is a pending irq that occurred
  1141. * before enable_irq() */
  1142. if (msm_uport->rx_wakeup.ignore)
  1143. msm_uport->rx_wakeup.ignore = 0;
  1144. else
  1145. wakeup = 1;
  1146. }
  1147. if (wakeup) {
  1148. /* the uart was clocked off during an rx, wake up and
  1149. * optionally inject char into tty rx */
  1150. msm_hs_request_clock_on_locked(uport);
  1151. if (msm_uport->rx_wakeup.inject_rx) {
  1152. tty_insert_flip_char(&uport->state->port,
  1153. msm_uport->rx_wakeup.rx_to_inject,
  1154. TTY_NORMAL);
  1155. queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
  1156. }
  1157. }
  1158. spin_unlock_irqrestore(&uport->lock, flags);
  1159. return IRQ_HANDLED;
  1160. }
  1161. static const char *msm_hs_type(struct uart_port *port)
  1162. {
  1163. return (port->type == PORT_MSM) ? "MSM_HS_UART" : NULL;
  1164. }
  1165. /* Called when port is opened */
  1166. static int msm_hs_startup(struct uart_port *uport)
  1167. {
  1168. int ret;
  1169. int rfr_level;
  1170. unsigned long flags;
  1171. unsigned int data;
  1172. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1173. struct circ_buf *tx_buf = &uport->state->xmit;
  1174. struct msm_hs_tx *tx = &msm_uport->tx;
  1175. struct msm_hs_rx *rx = &msm_uport->rx;
  1176. rfr_level = uport->fifosize;
  1177. if (rfr_level > 16)
  1178. rfr_level -= 16;
  1179. tx->dma_base = dma_map_single(uport->dev, tx_buf->buf, UART_XMIT_SIZE,
  1180. DMA_TO_DEVICE);
  1181. /* do not let tty layer execute RX in global workqueue, use a
  1182. * dedicated workqueue managed by this driver */
  1183. uport->state->port.low_latency = 1;
  1184. /* turn on uart clk */
  1185. ret = msm_hs_init_clk_locked(uport);
  1186. if (unlikely(ret)) {
  1187. printk(KERN_ERR "Turning uartclk failed!\n");
  1188. goto err_msm_hs_init_clk;
  1189. }
  1190. /* Set auto RFR Level */
  1191. data = msm_hs_read(uport, UARTDM_MR1_ADDR);
  1192. data &= ~UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK;
  1193. data &= ~UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK;
  1194. data |= (UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK & (rfr_level << 2));
  1195. data |= (UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK & rfr_level);
  1196. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  1197. /* Make sure RXSTALE count is non-zero */
  1198. data = msm_hs_read(uport, UARTDM_IPR_ADDR);
  1199. if (!data) {
  1200. data |= 0x1f & UARTDM_IPR_STALE_LSB_BMSK;
  1201. msm_hs_write(uport, UARTDM_IPR_ADDR, data);
  1202. }
  1203. /* Enable Data Mover Mode */
  1204. data = UARTDM_TX_DM_EN_BMSK | UARTDM_RX_DM_EN_BMSK;
  1205. msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
  1206. /* Reset TX */
  1207. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
  1208. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
  1209. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
  1210. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_BREAK_INT);
  1211. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
  1212. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
  1213. msm_hs_write(uport, UARTDM_CR_ADDR, RFR_LOW);
  1214. /* Turn on Uart Receiver */
  1215. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_EN_BMSK);
  1216. /* Turn on Uart Transmitter */
  1217. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_EN_BMSK);
  1218. /* Initialize the tx */
  1219. tx->tx_ready_int_en = 0;
  1220. tx->dma_in_flight = 0;
  1221. tx->xfer.complete_func = msm_hs_dmov_tx_callback;
  1222. tx->xfer.execute_func = NULL;
  1223. tx->command_ptr->cmd = CMD_LC |
  1224. CMD_DST_CRCI(msm_uport->dma_tx_crci) | CMD_MODE_BOX;
  1225. tx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
  1226. | (MSM_UARTDM_BURST_SIZE);
  1227. tx->command_ptr->row_offset = (MSM_UARTDM_BURST_SIZE << 16);
  1228. tx->command_ptr->dst_row_addr =
  1229. msm_uport->uport.mapbase + UARTDM_TF_ADDR;
  1230. /* Turn on Uart Receive */
  1231. rx->xfer.complete_func = msm_hs_dmov_rx_callback;
  1232. rx->xfer.execute_func = NULL;
  1233. rx->command_ptr->cmd = CMD_LC |
  1234. CMD_SRC_CRCI(msm_uport->dma_rx_crci) | CMD_MODE_BOX;
  1235. rx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
  1236. | (MSM_UARTDM_BURST_SIZE);
  1237. rx->command_ptr->row_offset = MSM_UARTDM_BURST_SIZE;
  1238. rx->command_ptr->src_row_addr = uport->mapbase + UARTDM_RF_ADDR;
  1239. msm_uport->imr_reg |= UARTDM_ISR_RXSTALE_BMSK;
  1240. /* Enable reading the current CTS, no harm even if CTS is ignored */
  1241. msm_uport->imr_reg |= UARTDM_ISR_CURRENT_CTS_BMSK;
  1242. msm_hs_write(uport, UARTDM_TFWR_ADDR, 0); /* TXLEV on empty TX fifo */
  1243. ret = request_irq(uport->irq, msm_hs_isr, IRQF_TRIGGER_HIGH,
  1244. "msm_hs_uart", msm_uport);
  1245. if (unlikely(ret)) {
  1246. printk(KERN_ERR "Request msm_hs_uart IRQ failed!\n");
  1247. goto err_request_irq;
  1248. }
  1249. if (use_low_power_rx_wakeup(msm_uport)) {
  1250. ret = request_irq(msm_uport->rx_wakeup.irq,
  1251. msm_hs_rx_wakeup_isr,
  1252. IRQF_TRIGGER_FALLING,
  1253. "msm_hs_rx_wakeup", msm_uport);
  1254. if (unlikely(ret)) {
  1255. printk(KERN_ERR "Request msm_hs_rx_wakeup IRQ failed!\n");
  1256. free_irq(uport->irq, msm_uport);
  1257. goto err_request_irq;
  1258. }
  1259. disable_irq(msm_uport->rx_wakeup.irq);
  1260. }
  1261. spin_lock_irqsave(&uport->lock, flags);
  1262. msm_hs_write(uport, UARTDM_RFWR_ADDR, 0);
  1263. msm_hs_start_rx_locked(uport);
  1264. spin_unlock_irqrestore(&uport->lock, flags);
  1265. ret = pm_runtime_set_active(uport->dev);
  1266. if (ret)
  1267. dev_err(uport->dev, "set active error:%d\n", ret);
  1268. pm_runtime_enable(uport->dev);
  1269. return 0;
  1270. err_request_irq:
  1271. err_msm_hs_init_clk:
  1272. dma_unmap_single(uport->dev, tx->dma_base,
  1273. UART_XMIT_SIZE, DMA_TO_DEVICE);
  1274. return ret;
  1275. }
  1276. /* Initialize tx and rx data structures */
  1277. static int uartdm_init_port(struct uart_port *uport)
  1278. {
  1279. int ret = 0;
  1280. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1281. struct msm_hs_tx *tx = &msm_uport->tx;
  1282. struct msm_hs_rx *rx = &msm_uport->rx;
  1283. /* Allocate the command pointer. Needs to be 64 bit aligned */
  1284. tx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
  1285. if (!tx->command_ptr)
  1286. return -ENOMEM;
  1287. tx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
  1288. if (!tx->command_ptr_ptr) {
  1289. ret = -ENOMEM;
  1290. goto err_tx_command_ptr_ptr;
  1291. }
  1292. tx->mapped_cmd_ptr = dma_map_single(uport->dev, tx->command_ptr,
  1293. sizeof(dmov_box), DMA_TO_DEVICE);
  1294. tx->mapped_cmd_ptr_ptr = dma_map_single(uport->dev,
  1295. tx->command_ptr_ptr,
  1296. sizeof(u32), DMA_TO_DEVICE);
  1297. tx->xfer.cmdptr = DMOV_CMD_ADDR(tx->mapped_cmd_ptr_ptr);
  1298. init_waitqueue_head(&rx->wait);
  1299. rx->pool = dma_pool_create("rx_buffer_pool", uport->dev,
  1300. UARTDM_RX_BUF_SIZE, 16, 0);
  1301. if (!rx->pool) {
  1302. pr_err("%s(): cannot allocate rx_buffer_pool", __func__);
  1303. ret = -ENOMEM;
  1304. goto err_dma_pool_create;
  1305. }
  1306. rx->buffer = dma_pool_alloc(rx->pool, GFP_KERNEL, &rx->rbuffer);
  1307. if (!rx->buffer) {
  1308. pr_err("%s(): cannot allocate rx->buffer", __func__);
  1309. ret = -ENOMEM;
  1310. goto err_dma_pool_alloc;
  1311. }
  1312. /* Allocate the command pointer. Needs to be 64 bit aligned */
  1313. rx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
  1314. if (!rx->command_ptr) {
  1315. pr_err("%s(): cannot allocate rx->command_ptr", __func__);
  1316. ret = -ENOMEM;
  1317. goto err_rx_command_ptr;
  1318. }
  1319. rx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
  1320. if (!rx->command_ptr_ptr) {
  1321. pr_err("%s(): cannot allocate rx->command_ptr_ptr", __func__);
  1322. ret = -ENOMEM;
  1323. goto err_rx_command_ptr_ptr;
  1324. }
  1325. rx->command_ptr->num_rows = ((UARTDM_RX_BUF_SIZE >> 4) << 16) |
  1326. (UARTDM_RX_BUF_SIZE >> 4);
  1327. rx->command_ptr->dst_row_addr = rx->rbuffer;
  1328. rx->mapped_cmd_ptr = dma_map_single(uport->dev, rx->command_ptr,
  1329. sizeof(dmov_box), DMA_TO_DEVICE);
  1330. *rx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(rx->mapped_cmd_ptr);
  1331. rx->cmdptr_dmaaddr = dma_map_single(uport->dev, rx->command_ptr_ptr,
  1332. sizeof(u32), DMA_TO_DEVICE);
  1333. rx->xfer.cmdptr = DMOV_CMD_ADDR(rx->cmdptr_dmaaddr);
  1334. INIT_WORK(&rx->tty_work, msm_hs_tty_flip_buffer_work);
  1335. return ret;
  1336. err_rx_command_ptr_ptr:
  1337. kfree(rx->command_ptr);
  1338. err_rx_command_ptr:
  1339. dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
  1340. msm_uport->rx.rbuffer);
  1341. err_dma_pool_alloc:
  1342. dma_pool_destroy(msm_uport->rx.pool);
  1343. err_dma_pool_create:
  1344. dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr_ptr,
  1345. sizeof(u32), DMA_TO_DEVICE);
  1346. dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr,
  1347. sizeof(dmov_box), DMA_TO_DEVICE);
  1348. kfree(msm_uport->tx.command_ptr_ptr);
  1349. err_tx_command_ptr_ptr:
  1350. kfree(msm_uport->tx.command_ptr);
  1351. return ret;
  1352. }
  1353. static int msm_hs_probe(struct platform_device *pdev)
  1354. {
  1355. int ret;
  1356. struct uart_port *uport;
  1357. struct msm_hs_port *msm_uport;
  1358. struct resource *resource;
  1359. const struct msm_serial_hs_platform_data *pdata =
  1360. pdev->dev.platform_data;
  1361. if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
  1362. printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
  1363. return -EINVAL;
  1364. }
  1365. msm_uport = &q_uart_port[pdev->id];
  1366. uport = &msm_uport->uport;
  1367. uport->dev = &pdev->dev;
  1368. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1369. if (unlikely(!resource))
  1370. return -ENXIO;
  1371. uport->mapbase = resource->start;
  1372. uport->irq = platform_get_irq(pdev, 0);
  1373. if (unlikely(uport->irq < 0))
  1374. return -ENXIO;
  1375. if (unlikely(irq_set_irq_wake(uport->irq, 1)))
  1376. return -ENXIO;
  1377. if (pdata == NULL || pdata->rx_wakeup_irq < 0)
  1378. msm_uport->rx_wakeup.irq = -1;
  1379. else {
  1380. msm_uport->rx_wakeup.irq = pdata->rx_wakeup_irq;
  1381. msm_uport->rx_wakeup.ignore = 1;
  1382. msm_uport->rx_wakeup.inject_rx = pdata->inject_rx_on_wakeup;
  1383. msm_uport->rx_wakeup.rx_to_inject = pdata->rx_to_inject;
  1384. if (unlikely(msm_uport->rx_wakeup.irq < 0))
  1385. return -ENXIO;
  1386. if (unlikely(irq_set_irq_wake(msm_uport->rx_wakeup.irq, 1)))
  1387. return -ENXIO;
  1388. }
  1389. if (pdata == NULL)
  1390. msm_uport->exit_lpm_cb = NULL;
  1391. else
  1392. msm_uport->exit_lpm_cb = pdata->exit_lpm_cb;
  1393. resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1394. "uartdm_channels");
  1395. if (unlikely(!resource))
  1396. return -ENXIO;
  1397. msm_uport->dma_tx_channel = resource->start;
  1398. msm_uport->dma_rx_channel = resource->end;
  1399. resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1400. "uartdm_crci");
  1401. if (unlikely(!resource))
  1402. return -ENXIO;
  1403. msm_uport->dma_tx_crci = resource->start;
  1404. msm_uport->dma_rx_crci = resource->end;
  1405. uport->iotype = UPIO_MEM;
  1406. uport->fifosize = UART_FIFOSIZE;
  1407. uport->ops = &msm_hs_ops;
  1408. uport->flags = UPF_BOOT_AUTOCONF;
  1409. uport->uartclk = UARTCLK;
  1410. msm_uport->imr_reg = 0x0;
  1411. msm_uport->clk = clk_get(&pdev->dev, "uartdm_clk");
  1412. if (IS_ERR(msm_uport->clk))
  1413. return PTR_ERR(msm_uport->clk);
  1414. ret = uartdm_init_port(uport);
  1415. if (unlikely(ret))
  1416. return ret;
  1417. msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
  1418. hrtimer_init(&msm_uport->clk_off_timer, CLOCK_MONOTONIC,
  1419. HRTIMER_MODE_REL);
  1420. msm_uport->clk_off_timer.function = msm_hs_clk_off_retry;
  1421. msm_uport->clk_off_delay = ktime_set(0, 1000000); /* 1ms */
  1422. uport->line = pdev->id;
  1423. return uart_add_one_port(&msm_hs_driver, uport);
  1424. }
  1425. static int __init msm_serial_hs_init(void)
  1426. {
  1427. int ret, i;
  1428. /* Init all UARTS as non-configured */
  1429. for (i = 0; i < UARTDM_NR; i++)
  1430. q_uart_port[i].uport.type = PORT_UNKNOWN;
  1431. msm_hs_workqueue = create_singlethread_workqueue("msm_serial_hs");
  1432. if (unlikely(!msm_hs_workqueue))
  1433. return -ENOMEM;
  1434. ret = uart_register_driver(&msm_hs_driver);
  1435. if (unlikely(ret)) {
  1436. printk(KERN_ERR "%s failed to load\n", __func__);
  1437. goto err_uart_register_driver;
  1438. }
  1439. ret = platform_driver_register(&msm_serial_hs_platform_driver);
  1440. if (ret) {
  1441. printk(KERN_ERR "%s failed to load\n", __func__);
  1442. goto err_platform_driver_register;
  1443. }
  1444. return ret;
  1445. err_platform_driver_register:
  1446. uart_unregister_driver(&msm_hs_driver);
  1447. err_uart_register_driver:
  1448. destroy_workqueue(msm_hs_workqueue);
  1449. return ret;
  1450. }
  1451. module_init(msm_serial_hs_init);
  1452. /*
  1453. * Called by the upper layer when port is closed.
  1454. * - Disables the port
  1455. * - Unhook the ISR
  1456. */
  1457. static void msm_hs_shutdown(struct uart_port *uport)
  1458. {
  1459. unsigned long flags;
  1460. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1461. BUG_ON(msm_uport->rx.flush < FLUSH_STOP);
  1462. spin_lock_irqsave(&uport->lock, flags);
  1463. clk_enable(msm_uport->clk);
  1464. /* Disable the transmitter */
  1465. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_DISABLE_BMSK);
  1466. /* Disable the receiver */
  1467. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_DISABLE_BMSK);
  1468. pm_runtime_disable(uport->dev);
  1469. pm_runtime_set_suspended(uport->dev);
  1470. /* Free the interrupt */
  1471. free_irq(uport->irq, msm_uport);
  1472. if (use_low_power_rx_wakeup(msm_uport))
  1473. free_irq(msm_uport->rx_wakeup.irq, msm_uport);
  1474. msm_uport->imr_reg = 0;
  1475. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1476. wait_event(msm_uport->rx.wait, msm_uport->rx.flush == FLUSH_SHUTDOWN);
  1477. clk_disable(msm_uport->clk); /* to balance local clk_enable() */
  1478. if (msm_uport->clk_state != MSM_HS_CLK_OFF)
  1479. clk_disable(msm_uport->clk); /* to balance clk_state */
  1480. msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
  1481. dma_unmap_single(uport->dev, msm_uport->tx.dma_base,
  1482. UART_XMIT_SIZE, DMA_TO_DEVICE);
  1483. spin_unlock_irqrestore(&uport->lock, flags);
  1484. if (cancel_work_sync(&msm_uport->rx.tty_work))
  1485. msm_hs_tty_flip_buffer_work(&msm_uport->rx.tty_work);
  1486. }
  1487. static void __exit msm_serial_hs_exit(void)
  1488. {
  1489. flush_workqueue(msm_hs_workqueue);
  1490. destroy_workqueue(msm_hs_workqueue);
  1491. platform_driver_unregister(&msm_serial_hs_platform_driver);
  1492. uart_unregister_driver(&msm_hs_driver);
  1493. }
  1494. module_exit(msm_serial_hs_exit);
  1495. #ifdef CONFIG_PM_RUNTIME
  1496. static int msm_hs_runtime_idle(struct device *dev)
  1497. {
  1498. /*
  1499. * returning success from idle results in runtime suspend to be
  1500. * called
  1501. */
  1502. return 0;
  1503. }
  1504. static int msm_hs_runtime_resume(struct device *dev)
  1505. {
  1506. struct platform_device *pdev = container_of(dev, struct
  1507. platform_device, dev);
  1508. struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
  1509. msm_hs_request_clock_on(&msm_uport->uport);
  1510. return 0;
  1511. }
  1512. static int msm_hs_runtime_suspend(struct device *dev)
  1513. {
  1514. struct platform_device *pdev = container_of(dev, struct
  1515. platform_device, dev);
  1516. struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
  1517. msm_hs_request_clock_off(&msm_uport->uport);
  1518. return 0;
  1519. }
  1520. #else
  1521. #define msm_hs_runtime_idle NULL
  1522. #define msm_hs_runtime_resume NULL
  1523. #define msm_hs_runtime_suspend NULL
  1524. #endif
  1525. static const struct dev_pm_ops msm_hs_dev_pm_ops = {
  1526. .runtime_suspend = msm_hs_runtime_suspend,
  1527. .runtime_resume = msm_hs_runtime_resume,
  1528. .runtime_idle = msm_hs_runtime_idle,
  1529. };
  1530. static struct platform_driver msm_serial_hs_platform_driver = {
  1531. .probe = msm_hs_probe,
  1532. .remove = msm_hs_remove,
  1533. .driver = {
  1534. .name = "msm_serial_hs",
  1535. .owner = THIS_MODULE,
  1536. .pm = &msm_hs_dev_pm_ops,
  1537. },
  1538. };
  1539. static struct uart_driver msm_hs_driver = {
  1540. .owner = THIS_MODULE,
  1541. .driver_name = "msm_serial_hs",
  1542. .dev_name = "ttyHS",
  1543. .nr = UARTDM_NR,
  1544. .cons = 0,
  1545. };
  1546. static struct uart_ops msm_hs_ops = {
  1547. .tx_empty = msm_hs_tx_empty,
  1548. .set_mctrl = msm_hs_set_mctrl_locked,
  1549. .get_mctrl = msm_hs_get_mctrl_locked,
  1550. .stop_tx = msm_hs_stop_tx_locked,
  1551. .start_tx = msm_hs_start_tx_locked,
  1552. .stop_rx = msm_hs_stop_rx_locked,
  1553. .enable_ms = msm_hs_enable_ms_locked,
  1554. .break_ctl = msm_hs_break_ctl,
  1555. .startup = msm_hs_startup,
  1556. .shutdown = msm_hs_shutdown,
  1557. .set_termios = msm_hs_set_termios,
  1558. .pm = msm_hs_pm,
  1559. .type = msm_hs_type,
  1560. .config_port = msm_hs_config_port,
  1561. .release_port = msm_hs_release_port,
  1562. .request_port = msm_hs_request_port,
  1563. };
  1564. MODULE_DESCRIPTION("High Speed UART Driver for the MSM chipset");
  1565. MODULE_VERSION("1.2");
  1566. MODULE_LICENSE("GPL v2");