msm_serial_hs.c 52 KB

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