msm_serial_hs.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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 __devexit 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 uart_port *uport;
  777. struct msm_hs_port *msm_uport;
  778. msm_uport = container_of(cmd_ptr, struct msm_hs_port, rx.xfer);
  779. uport = &msm_uport->uport;
  780. spin_lock_irqsave(&uport->lock, flags);
  781. clk_enable(msm_uport->clk);
  782. tty = uport->state->port.tty;
  783. msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
  784. status = msm_hs_read(uport, UARTDM_SR_ADDR);
  785. /* overflow is not connect to data in a FIFO */
  786. if (unlikely((status & UARTDM_SR_OVERRUN_BMSK) &&
  787. (uport->read_status_mask & CREAD))) {
  788. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  789. uport->icount.buf_overrun++;
  790. error_f = 1;
  791. }
  792. if (!(uport->ignore_status_mask & INPCK))
  793. status = status & ~(UARTDM_SR_PAR_FRAME_BMSK);
  794. if (unlikely(status & UARTDM_SR_PAR_FRAME_BMSK)) {
  795. /* Can not tell difference between parity & frame error */
  796. uport->icount.parity++;
  797. error_f = 1;
  798. if (uport->ignore_status_mask & IGNPAR)
  799. tty_insert_flip_char(tty, 0, TTY_PARITY);
  800. }
  801. if (error_f)
  802. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
  803. if (msm_uport->clk_req_off_state == CLK_REQ_OFF_FLUSH_ISSUED)
  804. msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_FLUSHED;
  805. flush = msm_uport->rx.flush;
  806. if (flush == FLUSH_IGNORE)
  807. msm_hs_start_rx_locked(uport);
  808. if (flush == FLUSH_STOP)
  809. msm_uport->rx.flush = FLUSH_SHUTDOWN;
  810. if (flush >= FLUSH_DATA_INVALID)
  811. goto out;
  812. rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);
  813. if (0 != (uport->read_status_mask & CREAD)) {
  814. retval = tty_insert_flip_string(tty, msm_uport->rx.buffer,
  815. rx_count);
  816. BUG_ON(retval != rx_count);
  817. }
  818. msm_hs_start_rx_locked(uport);
  819. out:
  820. clk_disable(msm_uport->clk);
  821. spin_unlock_irqrestore(&uport->lock, flags);
  822. if (flush < FLUSH_DATA_INVALID)
  823. queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
  824. }
  825. static void msm_hs_tty_flip_buffer_work(struct work_struct *work)
  826. {
  827. struct msm_hs_port *msm_uport =
  828. container_of(work, struct msm_hs_port, rx.tty_work);
  829. struct tty_struct *tty = msm_uport->uport.state->port.tty;
  830. tty_flip_buffer_push(tty);
  831. }
  832. /*
  833. * Standard API, Current states of modem control inputs
  834. *
  835. * Since CTS can be handled entirely by HARDWARE we always
  836. * indicate clear to send and count on the TX FIFO to block when
  837. * it fills up.
  838. *
  839. * - TIOCM_DCD
  840. * - TIOCM_CTS
  841. * - TIOCM_DSR
  842. * - TIOCM_RI
  843. * (Unsupported) DCD and DSR will return them high. RI will return low.
  844. */
  845. static unsigned int msm_hs_get_mctrl_locked(struct uart_port *uport)
  846. {
  847. return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
  848. }
  849. /*
  850. * True enables UART auto RFR, which indicates we are ready for data if the RX
  851. * buffer is not full. False disables auto RFR, and deasserts RFR to indicate
  852. * we are not ready for data. Must be called with UART clock on.
  853. */
  854. static void set_rfr_locked(struct uart_port *uport, int auto_rfr)
  855. {
  856. unsigned int data;
  857. data = msm_hs_read(uport, UARTDM_MR1_ADDR);
  858. if (auto_rfr) {
  859. /* enable auto ready-for-receiving */
  860. data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
  861. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  862. } else {
  863. /* disable auto ready-for-receiving */
  864. data &= ~UARTDM_MR1_RX_RDY_CTL_BMSK;
  865. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  866. /* RFR is active low, set high */
  867. msm_hs_write(uport, UARTDM_CR_ADDR, RFR_HIGH);
  868. }
  869. }
  870. /*
  871. * Standard API, used to set or clear RFR
  872. */
  873. static void msm_hs_set_mctrl_locked(struct uart_port *uport,
  874. unsigned int mctrl)
  875. {
  876. unsigned int auto_rfr;
  877. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  878. clk_enable(msm_uport->clk);
  879. auto_rfr = TIOCM_RTS & mctrl ? 1 : 0;
  880. set_rfr_locked(uport, auto_rfr);
  881. clk_disable(msm_uport->clk);
  882. }
  883. /* Standard API, Enable modem status (CTS) interrupt */
  884. static void msm_hs_enable_ms_locked(struct uart_port *uport)
  885. {
  886. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  887. clk_enable(msm_uport->clk);
  888. /* Enable DELTA_CTS Interrupt */
  889. msm_uport->imr_reg |= UARTDM_ISR_DELTA_CTS_BMSK;
  890. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  891. clk_disable(msm_uport->clk);
  892. }
  893. /*
  894. * Standard API, Break Signal
  895. *
  896. * Control the transmission of a break signal. ctl eq 0 => break
  897. * signal terminate ctl ne 0 => start break signal
  898. */
  899. static void msm_hs_break_ctl(struct uart_port *uport, int ctl)
  900. {
  901. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  902. clk_enable(msm_uport->clk);
  903. msm_hs_write(uport, UARTDM_CR_ADDR, ctl ? START_BREAK : STOP_BREAK);
  904. clk_disable(msm_uport->clk);
  905. }
  906. static void msm_hs_config_port(struct uart_port *uport, int cfg_flags)
  907. {
  908. unsigned long flags;
  909. spin_lock_irqsave(&uport->lock, flags);
  910. if (cfg_flags & UART_CONFIG_TYPE) {
  911. uport->type = PORT_MSM;
  912. msm_hs_request_port(uport);
  913. }
  914. spin_unlock_irqrestore(&uport->lock, flags);
  915. }
  916. /* Handle CTS changes (Called from interrupt handler) */
  917. static void msm_hs_handle_delta_cts_locked(struct uart_port *uport)
  918. {
  919. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  920. clk_enable(msm_uport->clk);
  921. /* clear interrupt */
  922. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
  923. uport->icount.cts++;
  924. clk_disable(msm_uport->clk);
  925. /* clear the IOCTL TIOCMIWAIT if called */
  926. wake_up_interruptible(&uport->state->port.delta_msr_wait);
  927. }
  928. /* check if the TX path is flushed, and if so clock off
  929. * returns 0 did not clock off, need to retry (still sending final byte)
  930. * -1 did not clock off, do not retry
  931. * 1 if we clocked off
  932. */
  933. static int msm_hs_check_clock_off_locked(struct uart_port *uport)
  934. {
  935. unsigned long sr_status;
  936. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  937. struct circ_buf *tx_buf = &uport->state->xmit;
  938. /* Cancel if tx tty buffer is not empty, dma is in flight,
  939. * or tx fifo is not empty, or rx fifo is not empty */
  940. if (msm_uport->clk_state != MSM_HS_CLK_REQUEST_OFF ||
  941. !uart_circ_empty(tx_buf) || msm_uport->tx.dma_in_flight ||
  942. (msm_uport->imr_reg & UARTDM_ISR_TXLEV_BMSK) ||
  943. !(msm_uport->imr_reg & UARTDM_ISR_RXLEV_BMSK)) {
  944. return -1;
  945. }
  946. /* Make sure the uart is finished with the last byte */
  947. sr_status = msm_hs_read(uport, UARTDM_SR_ADDR);
  948. if (!(sr_status & UARTDM_SR_TXEMT_BMSK))
  949. return 0; /* retry */
  950. /* Make sure forced RXSTALE flush complete */
  951. switch (msm_uport->clk_req_off_state) {
  952. case CLK_REQ_OFF_START:
  953. msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_ISSUED;
  954. msm_hs_write(uport, UARTDM_CR_ADDR, FORCE_STALE_EVENT);
  955. return 0; /* RXSTALE flush not complete - retry */
  956. case CLK_REQ_OFF_RXSTALE_ISSUED:
  957. case CLK_REQ_OFF_FLUSH_ISSUED:
  958. return 0; /* RXSTALE flush not complete - retry */
  959. case CLK_REQ_OFF_RXSTALE_FLUSHED:
  960. break; /* continue */
  961. }
  962. if (msm_uport->rx.flush != FLUSH_SHUTDOWN) {
  963. if (msm_uport->rx.flush == FLUSH_NONE)
  964. msm_hs_stop_rx_locked(uport);
  965. return 0; /* come back later to really clock off */
  966. }
  967. /* we really want to clock off */
  968. clk_disable(msm_uport->clk);
  969. msm_uport->clk_state = MSM_HS_CLK_OFF;
  970. if (use_low_power_rx_wakeup(msm_uport)) {
  971. msm_uport->rx_wakeup.ignore = 1;
  972. enable_irq(msm_uport->rx_wakeup.irq);
  973. }
  974. return 1;
  975. }
  976. static enum hrtimer_restart msm_hs_clk_off_retry(struct hrtimer *timer)
  977. {
  978. unsigned long flags;
  979. int ret = HRTIMER_NORESTART;
  980. struct msm_hs_port *msm_uport = container_of(timer, struct msm_hs_port,
  981. clk_off_timer);
  982. struct uart_port *uport = &msm_uport->uport;
  983. spin_lock_irqsave(&uport->lock, flags);
  984. if (!msm_hs_check_clock_off_locked(uport)) {
  985. hrtimer_forward_now(timer, msm_uport->clk_off_delay);
  986. ret = HRTIMER_RESTART;
  987. }
  988. spin_unlock_irqrestore(&uport->lock, flags);
  989. return ret;
  990. }
  991. static irqreturn_t msm_hs_isr(int irq, void *dev)
  992. {
  993. unsigned long flags;
  994. unsigned long isr_status;
  995. struct msm_hs_port *msm_uport = dev;
  996. struct uart_port *uport = &msm_uport->uport;
  997. struct circ_buf *tx_buf = &uport->state->xmit;
  998. struct msm_hs_tx *tx = &msm_uport->tx;
  999. struct msm_hs_rx *rx = &msm_uport->rx;
  1000. spin_lock_irqsave(&uport->lock, flags);
  1001. isr_status = msm_hs_read(uport, UARTDM_MISR_ADDR);
  1002. /* Uart RX starting */
  1003. if (isr_status & UARTDM_ISR_RXLEV_BMSK) {
  1004. msm_uport->imr_reg &= ~UARTDM_ISR_RXLEV_BMSK;
  1005. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1006. }
  1007. /* Stale rx interrupt */
  1008. if (isr_status & UARTDM_ISR_RXSTALE_BMSK) {
  1009. msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
  1010. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
  1011. if (msm_uport->clk_req_off_state == CLK_REQ_OFF_RXSTALE_ISSUED)
  1012. msm_uport->clk_req_off_state =
  1013. CLK_REQ_OFF_FLUSH_ISSUED;
  1014. if (rx->flush == FLUSH_NONE) {
  1015. rx->flush = FLUSH_DATA_READY;
  1016. msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
  1017. }
  1018. }
  1019. /* tx ready interrupt */
  1020. if (isr_status & UARTDM_ISR_TX_READY_BMSK) {
  1021. /* Clear TX Ready */
  1022. msm_hs_write(uport, UARTDM_CR_ADDR, CLEAR_TX_READY);
  1023. if (msm_uport->clk_state == MSM_HS_CLK_REQUEST_OFF) {
  1024. msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
  1025. msm_hs_write(uport, UARTDM_IMR_ADDR,
  1026. msm_uport->imr_reg);
  1027. }
  1028. /* Complete DMA TX transactions and submit new transactions */
  1029. tx_buf->tail = (tx_buf->tail + tx->tx_count) & ~UART_XMIT_SIZE;
  1030. tx->dma_in_flight = 0;
  1031. uport->icount.tx += tx->tx_count;
  1032. if (tx->tx_ready_int_en)
  1033. msm_hs_submit_tx_locked(uport);
  1034. if (uart_circ_chars_pending(tx_buf) < WAKEUP_CHARS)
  1035. uart_write_wakeup(uport);
  1036. }
  1037. if (isr_status & UARTDM_ISR_TXLEV_BMSK) {
  1038. /* TX FIFO is empty */
  1039. msm_uport->imr_reg &= ~UARTDM_ISR_TXLEV_BMSK;
  1040. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1041. if (!msm_hs_check_clock_off_locked(uport))
  1042. hrtimer_start(&msm_uport->clk_off_timer,
  1043. msm_uport->clk_off_delay,
  1044. HRTIMER_MODE_REL);
  1045. }
  1046. /* Change in CTS interrupt */
  1047. if (isr_status & UARTDM_ISR_DELTA_CTS_BMSK)
  1048. msm_hs_handle_delta_cts_locked(uport);
  1049. spin_unlock_irqrestore(&uport->lock, flags);
  1050. return IRQ_HANDLED;
  1051. }
  1052. void msm_hs_request_clock_off_locked(struct uart_port *uport)
  1053. {
  1054. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1055. if (msm_uport->clk_state == MSM_HS_CLK_ON) {
  1056. msm_uport->clk_state = MSM_HS_CLK_REQUEST_OFF;
  1057. msm_uport->clk_req_off_state = CLK_REQ_OFF_START;
  1058. if (!use_low_power_rx_wakeup(msm_uport))
  1059. set_rfr_locked(uport, 0);
  1060. msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
  1061. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1062. }
  1063. }
  1064. /**
  1065. * msm_hs_request_clock_off - request to (i.e. asynchronously) turn off uart
  1066. * clock once pending TX is flushed and Rx DMA command is terminated.
  1067. * @uport: uart_port structure for the device instance.
  1068. *
  1069. * This functions puts the device into a partially active low power mode. It
  1070. * waits to complete all pending tx transactions, flushes ongoing Rx DMA
  1071. * command and terminates UART side Rx transaction, puts UART HW in non DMA
  1072. * mode and then clocks off the device. A client calls this when no UART
  1073. * data is expected. msm_request_clock_on() must be called before any further
  1074. * UART can be sent or received.
  1075. */
  1076. void msm_hs_request_clock_off(struct uart_port *uport)
  1077. {
  1078. unsigned long flags;
  1079. spin_lock_irqsave(&uport->lock, flags);
  1080. msm_hs_request_clock_off_locked(uport);
  1081. spin_unlock_irqrestore(&uport->lock, flags);
  1082. }
  1083. void msm_hs_request_clock_on_locked(struct uart_port *uport)
  1084. {
  1085. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1086. unsigned int data;
  1087. switch (msm_uport->clk_state) {
  1088. case MSM_HS_CLK_OFF:
  1089. clk_enable(msm_uport->clk);
  1090. disable_irq_nosync(msm_uport->rx_wakeup.irq);
  1091. /* fall-through */
  1092. case MSM_HS_CLK_REQUEST_OFF:
  1093. if (msm_uport->rx.flush == FLUSH_STOP ||
  1094. msm_uport->rx.flush == FLUSH_SHUTDOWN) {
  1095. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
  1096. data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
  1097. data |= UARTDM_RX_DM_EN_BMSK;
  1098. msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
  1099. }
  1100. hrtimer_try_to_cancel(&msm_uport->clk_off_timer);
  1101. if (msm_uport->rx.flush == FLUSH_SHUTDOWN)
  1102. msm_hs_start_rx_locked(uport);
  1103. if (!use_low_power_rx_wakeup(msm_uport))
  1104. set_rfr_locked(uport, 1);
  1105. if (msm_uport->rx.flush == FLUSH_STOP)
  1106. msm_uport->rx.flush = FLUSH_IGNORE;
  1107. msm_uport->clk_state = MSM_HS_CLK_ON;
  1108. break;
  1109. case MSM_HS_CLK_ON:
  1110. break;
  1111. case MSM_HS_CLK_PORT_OFF:
  1112. break;
  1113. }
  1114. }
  1115. /**
  1116. * msm_hs_request_clock_on - Switch the device from partially active low
  1117. * power mode to fully active (i.e. clock on) mode.
  1118. * @uport: uart_port structure for the device.
  1119. *
  1120. * This function switches on the input clock, puts UART HW into DMA mode
  1121. * and enqueues an Rx DMA command if the device was in partially active
  1122. * mode. It has no effect if called with the device in inactive state.
  1123. */
  1124. void msm_hs_request_clock_on(struct uart_port *uport)
  1125. {
  1126. unsigned long flags;
  1127. spin_lock_irqsave(&uport->lock, flags);
  1128. msm_hs_request_clock_on_locked(uport);
  1129. spin_unlock_irqrestore(&uport->lock, flags);
  1130. }
  1131. static irqreturn_t msm_hs_rx_wakeup_isr(int irq, void *dev)
  1132. {
  1133. unsigned int wakeup = 0;
  1134. unsigned long flags;
  1135. struct msm_hs_port *msm_uport = dev;
  1136. struct uart_port *uport = &msm_uport->uport;
  1137. struct tty_struct *tty = NULL;
  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 = uport->state->port.tty;
  1153. tty_insert_flip_char(tty,
  1154. msm_uport->rx_wakeup.rx_to_inject,
  1155. TTY_NORMAL);
  1156. queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
  1157. }
  1158. }
  1159. spin_unlock_irqrestore(&uport->lock, flags);
  1160. return IRQ_HANDLED;
  1161. }
  1162. static const char *msm_hs_type(struct uart_port *port)
  1163. {
  1164. return (port->type == PORT_MSM) ? "MSM_HS_UART" : NULL;
  1165. }
  1166. /* Called when port is opened */
  1167. static int msm_hs_startup(struct uart_port *uport)
  1168. {
  1169. int ret;
  1170. int rfr_level;
  1171. unsigned long flags;
  1172. unsigned int data;
  1173. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1174. struct circ_buf *tx_buf = &uport->state->xmit;
  1175. struct msm_hs_tx *tx = &msm_uport->tx;
  1176. struct msm_hs_rx *rx = &msm_uport->rx;
  1177. rfr_level = uport->fifosize;
  1178. if (rfr_level > 16)
  1179. rfr_level -= 16;
  1180. tx->dma_base = dma_map_single(uport->dev, tx_buf->buf, UART_XMIT_SIZE,
  1181. DMA_TO_DEVICE);
  1182. /* do not let tty layer execute RX in global workqueue, use a
  1183. * dedicated workqueue managed by this driver */
  1184. uport->state->port.tty->low_latency = 1;
  1185. /* turn on uart clk */
  1186. ret = msm_hs_init_clk_locked(uport);
  1187. if (unlikely(ret)) {
  1188. printk(KERN_ERR "Turning uartclk failed!\n");
  1189. goto err_msm_hs_init_clk;
  1190. }
  1191. /* Set auto RFR Level */
  1192. data = msm_hs_read(uport, UARTDM_MR1_ADDR);
  1193. data &= ~UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK;
  1194. data &= ~UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK;
  1195. data |= (UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK & (rfr_level << 2));
  1196. data |= (UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK & rfr_level);
  1197. msm_hs_write(uport, UARTDM_MR1_ADDR, data);
  1198. /* Make sure RXSTALE count is non-zero */
  1199. data = msm_hs_read(uport, UARTDM_IPR_ADDR);
  1200. if (!data) {
  1201. data |= 0x1f & UARTDM_IPR_STALE_LSB_BMSK;
  1202. msm_hs_write(uport, UARTDM_IPR_ADDR, data);
  1203. }
  1204. /* Enable Data Mover Mode */
  1205. data = UARTDM_TX_DM_EN_BMSK | UARTDM_RX_DM_EN_BMSK;
  1206. msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
  1207. /* Reset TX */
  1208. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
  1209. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
  1210. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
  1211. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_BREAK_INT);
  1212. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
  1213. msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
  1214. msm_hs_write(uport, UARTDM_CR_ADDR, RFR_LOW);
  1215. /* Turn on Uart Receiver */
  1216. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_EN_BMSK);
  1217. /* Turn on Uart Transmitter */
  1218. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_EN_BMSK);
  1219. /* Initialize the tx */
  1220. tx->tx_ready_int_en = 0;
  1221. tx->dma_in_flight = 0;
  1222. tx->xfer.complete_func = msm_hs_dmov_tx_callback;
  1223. tx->xfer.execute_func = NULL;
  1224. tx->command_ptr->cmd = CMD_LC |
  1225. CMD_DST_CRCI(msm_uport->dma_tx_crci) | CMD_MODE_BOX;
  1226. tx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
  1227. | (MSM_UARTDM_BURST_SIZE);
  1228. tx->command_ptr->row_offset = (MSM_UARTDM_BURST_SIZE << 16);
  1229. tx->command_ptr->dst_row_addr =
  1230. msm_uport->uport.mapbase + UARTDM_TF_ADDR;
  1231. /* Turn on Uart Receive */
  1232. rx->xfer.complete_func = msm_hs_dmov_rx_callback;
  1233. rx->xfer.execute_func = NULL;
  1234. rx->command_ptr->cmd = CMD_LC |
  1235. CMD_SRC_CRCI(msm_uport->dma_rx_crci) | CMD_MODE_BOX;
  1236. rx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
  1237. | (MSM_UARTDM_BURST_SIZE);
  1238. rx->command_ptr->row_offset = MSM_UARTDM_BURST_SIZE;
  1239. rx->command_ptr->src_row_addr = uport->mapbase + UARTDM_RF_ADDR;
  1240. msm_uport->imr_reg |= UARTDM_ISR_RXSTALE_BMSK;
  1241. /* Enable reading the current CTS, no harm even if CTS is ignored */
  1242. msm_uport->imr_reg |= UARTDM_ISR_CURRENT_CTS_BMSK;
  1243. msm_hs_write(uport, UARTDM_TFWR_ADDR, 0); /* TXLEV on empty TX fifo */
  1244. ret = request_irq(uport->irq, msm_hs_isr, IRQF_TRIGGER_HIGH,
  1245. "msm_hs_uart", msm_uport);
  1246. if (unlikely(ret)) {
  1247. printk(KERN_ERR "Request msm_hs_uart IRQ failed!\n");
  1248. goto err_request_irq;
  1249. }
  1250. if (use_low_power_rx_wakeup(msm_uport)) {
  1251. ret = request_irq(msm_uport->rx_wakeup.irq,
  1252. msm_hs_rx_wakeup_isr,
  1253. IRQF_TRIGGER_FALLING,
  1254. "msm_hs_rx_wakeup", msm_uport);
  1255. if (unlikely(ret)) {
  1256. printk(KERN_ERR "Request msm_hs_rx_wakeup IRQ failed!\n");
  1257. free_irq(uport->irq, msm_uport);
  1258. goto err_request_irq;
  1259. }
  1260. disable_irq(msm_uport->rx_wakeup.irq);
  1261. }
  1262. spin_lock_irqsave(&uport->lock, flags);
  1263. msm_hs_write(uport, UARTDM_RFWR_ADDR, 0);
  1264. msm_hs_start_rx_locked(uport);
  1265. spin_unlock_irqrestore(&uport->lock, flags);
  1266. ret = pm_runtime_set_active(uport->dev);
  1267. if (ret)
  1268. dev_err(uport->dev, "set active error:%d\n", ret);
  1269. pm_runtime_enable(uport->dev);
  1270. return 0;
  1271. err_request_irq:
  1272. err_msm_hs_init_clk:
  1273. dma_unmap_single(uport->dev, tx->dma_base,
  1274. UART_XMIT_SIZE, DMA_TO_DEVICE);
  1275. return ret;
  1276. }
  1277. /* Initialize tx and rx data structures */
  1278. static int __devinit uartdm_init_port(struct uart_port *uport)
  1279. {
  1280. int ret = 0;
  1281. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1282. struct msm_hs_tx *tx = &msm_uport->tx;
  1283. struct msm_hs_rx *rx = &msm_uport->rx;
  1284. /* Allocate the command pointer. Needs to be 64 bit aligned */
  1285. tx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
  1286. if (!tx->command_ptr)
  1287. return -ENOMEM;
  1288. tx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
  1289. if (!tx->command_ptr_ptr) {
  1290. ret = -ENOMEM;
  1291. goto err_tx_command_ptr_ptr;
  1292. }
  1293. tx->mapped_cmd_ptr = dma_map_single(uport->dev, tx->command_ptr,
  1294. sizeof(dmov_box), DMA_TO_DEVICE);
  1295. tx->mapped_cmd_ptr_ptr = dma_map_single(uport->dev,
  1296. tx->command_ptr_ptr,
  1297. sizeof(u32), DMA_TO_DEVICE);
  1298. tx->xfer.cmdptr = DMOV_CMD_ADDR(tx->mapped_cmd_ptr_ptr);
  1299. init_waitqueue_head(&rx->wait);
  1300. rx->pool = dma_pool_create("rx_buffer_pool", uport->dev,
  1301. UARTDM_RX_BUF_SIZE, 16, 0);
  1302. if (!rx->pool) {
  1303. pr_err("%s(): cannot allocate rx_buffer_pool", __func__);
  1304. ret = -ENOMEM;
  1305. goto err_dma_pool_create;
  1306. }
  1307. rx->buffer = dma_pool_alloc(rx->pool, GFP_KERNEL, &rx->rbuffer);
  1308. if (!rx->buffer) {
  1309. pr_err("%s(): cannot allocate rx->buffer", __func__);
  1310. ret = -ENOMEM;
  1311. goto err_dma_pool_alloc;
  1312. }
  1313. /* Allocate the command pointer. Needs to be 64 bit aligned */
  1314. rx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
  1315. if (!rx->command_ptr) {
  1316. pr_err("%s(): cannot allocate rx->command_ptr", __func__);
  1317. ret = -ENOMEM;
  1318. goto err_rx_command_ptr;
  1319. }
  1320. rx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
  1321. if (!rx->command_ptr_ptr) {
  1322. pr_err("%s(): cannot allocate rx->command_ptr_ptr", __func__);
  1323. ret = -ENOMEM;
  1324. goto err_rx_command_ptr_ptr;
  1325. }
  1326. rx->command_ptr->num_rows = ((UARTDM_RX_BUF_SIZE >> 4) << 16) |
  1327. (UARTDM_RX_BUF_SIZE >> 4);
  1328. rx->command_ptr->dst_row_addr = rx->rbuffer;
  1329. rx->mapped_cmd_ptr = dma_map_single(uport->dev, rx->command_ptr,
  1330. sizeof(dmov_box), DMA_TO_DEVICE);
  1331. *rx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(rx->mapped_cmd_ptr);
  1332. rx->cmdptr_dmaaddr = dma_map_single(uport->dev, rx->command_ptr_ptr,
  1333. sizeof(u32), DMA_TO_DEVICE);
  1334. rx->xfer.cmdptr = DMOV_CMD_ADDR(rx->cmdptr_dmaaddr);
  1335. INIT_WORK(&rx->tty_work, msm_hs_tty_flip_buffer_work);
  1336. return ret;
  1337. err_rx_command_ptr_ptr:
  1338. kfree(rx->command_ptr);
  1339. err_rx_command_ptr:
  1340. dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
  1341. msm_uport->rx.rbuffer);
  1342. err_dma_pool_alloc:
  1343. dma_pool_destroy(msm_uport->rx.pool);
  1344. err_dma_pool_create:
  1345. dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr_ptr,
  1346. sizeof(u32), DMA_TO_DEVICE);
  1347. dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr,
  1348. sizeof(dmov_box), DMA_TO_DEVICE);
  1349. kfree(msm_uport->tx.command_ptr_ptr);
  1350. err_tx_command_ptr_ptr:
  1351. kfree(msm_uport->tx.command_ptr);
  1352. return ret;
  1353. }
  1354. static int __devinit msm_hs_probe(struct platform_device *pdev)
  1355. {
  1356. int ret;
  1357. struct uart_port *uport;
  1358. struct msm_hs_port *msm_uport;
  1359. struct resource *resource;
  1360. const struct msm_serial_hs_platform_data *pdata =
  1361. pdev->dev.platform_data;
  1362. if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
  1363. printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
  1364. return -EINVAL;
  1365. }
  1366. msm_uport = &q_uart_port[pdev->id];
  1367. uport = &msm_uport->uport;
  1368. uport->dev = &pdev->dev;
  1369. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1370. if (unlikely(!resource))
  1371. return -ENXIO;
  1372. uport->mapbase = resource->start;
  1373. uport->irq = platform_get_irq(pdev, 0);
  1374. if (unlikely(uport->irq < 0))
  1375. return -ENXIO;
  1376. if (unlikely(irq_set_irq_wake(uport->irq, 1)))
  1377. return -ENXIO;
  1378. if (pdata == NULL || pdata->rx_wakeup_irq < 0)
  1379. msm_uport->rx_wakeup.irq = -1;
  1380. else {
  1381. msm_uport->rx_wakeup.irq = pdata->rx_wakeup_irq;
  1382. msm_uport->rx_wakeup.ignore = 1;
  1383. msm_uport->rx_wakeup.inject_rx = pdata->inject_rx_on_wakeup;
  1384. msm_uport->rx_wakeup.rx_to_inject = pdata->rx_to_inject;
  1385. if (unlikely(msm_uport->rx_wakeup.irq < 0))
  1386. return -ENXIO;
  1387. if (unlikely(irq_set_irq_wake(msm_uport->rx_wakeup.irq, 1)))
  1388. return -ENXIO;
  1389. }
  1390. if (pdata == NULL)
  1391. msm_uport->exit_lpm_cb = NULL;
  1392. else
  1393. msm_uport->exit_lpm_cb = pdata->exit_lpm_cb;
  1394. resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1395. "uartdm_channels");
  1396. if (unlikely(!resource))
  1397. return -ENXIO;
  1398. msm_uport->dma_tx_channel = resource->start;
  1399. msm_uport->dma_rx_channel = resource->end;
  1400. resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1401. "uartdm_crci");
  1402. if (unlikely(!resource))
  1403. return -ENXIO;
  1404. msm_uport->dma_tx_crci = resource->start;
  1405. msm_uport->dma_rx_crci = resource->end;
  1406. uport->iotype = UPIO_MEM;
  1407. uport->fifosize = UART_FIFOSIZE;
  1408. uport->ops = &msm_hs_ops;
  1409. uport->flags = UPF_BOOT_AUTOCONF;
  1410. uport->uartclk = UARTCLK;
  1411. msm_uport->imr_reg = 0x0;
  1412. msm_uport->clk = clk_get(&pdev->dev, "uartdm_clk");
  1413. if (IS_ERR(msm_uport->clk))
  1414. return PTR_ERR(msm_uport->clk);
  1415. ret = uartdm_init_port(uport);
  1416. if (unlikely(ret))
  1417. return ret;
  1418. msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
  1419. hrtimer_init(&msm_uport->clk_off_timer, CLOCK_MONOTONIC,
  1420. HRTIMER_MODE_REL);
  1421. msm_uport->clk_off_timer.function = msm_hs_clk_off_retry;
  1422. msm_uport->clk_off_delay = ktime_set(0, 1000000); /* 1ms */
  1423. uport->line = pdev->id;
  1424. return uart_add_one_port(&msm_hs_driver, uport);
  1425. }
  1426. static int __init msm_serial_hs_init(void)
  1427. {
  1428. int ret, i;
  1429. /* Init all UARTS as non-configured */
  1430. for (i = 0; i < UARTDM_NR; i++)
  1431. q_uart_port[i].uport.type = PORT_UNKNOWN;
  1432. msm_hs_workqueue = create_singlethread_workqueue("msm_serial_hs");
  1433. if (unlikely(!msm_hs_workqueue))
  1434. return -ENOMEM;
  1435. ret = uart_register_driver(&msm_hs_driver);
  1436. if (unlikely(ret)) {
  1437. printk(KERN_ERR "%s failed to load\n", __func__);
  1438. goto err_uart_register_driver;
  1439. }
  1440. ret = platform_driver_register(&msm_serial_hs_platform_driver);
  1441. if (ret) {
  1442. printk(KERN_ERR "%s failed to load\n", __func__);
  1443. goto err_platform_driver_register;
  1444. }
  1445. return ret;
  1446. err_platform_driver_register:
  1447. uart_unregister_driver(&msm_hs_driver);
  1448. err_uart_register_driver:
  1449. destroy_workqueue(msm_hs_workqueue);
  1450. return ret;
  1451. }
  1452. module_init(msm_serial_hs_init);
  1453. /*
  1454. * Called by the upper layer when port is closed.
  1455. * - Disables the port
  1456. * - Unhook the ISR
  1457. */
  1458. static void msm_hs_shutdown(struct uart_port *uport)
  1459. {
  1460. unsigned long flags;
  1461. struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
  1462. BUG_ON(msm_uport->rx.flush < FLUSH_STOP);
  1463. spin_lock_irqsave(&uport->lock, flags);
  1464. clk_enable(msm_uport->clk);
  1465. /* Disable the transmitter */
  1466. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_DISABLE_BMSK);
  1467. /* Disable the receiver */
  1468. msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_DISABLE_BMSK);
  1469. pm_runtime_disable(uport->dev);
  1470. pm_runtime_set_suspended(uport->dev);
  1471. /* Free the interrupt */
  1472. free_irq(uport->irq, msm_uport);
  1473. if (use_low_power_rx_wakeup(msm_uport))
  1474. free_irq(msm_uport->rx_wakeup.irq, msm_uport);
  1475. msm_uport->imr_reg = 0;
  1476. msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
  1477. wait_event(msm_uport->rx.wait, msm_uport->rx.flush == FLUSH_SHUTDOWN);
  1478. clk_disable(msm_uport->clk); /* to balance local clk_enable() */
  1479. if (msm_uport->clk_state != MSM_HS_CLK_OFF)
  1480. clk_disable(msm_uport->clk); /* to balance clk_state */
  1481. msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
  1482. dma_unmap_single(uport->dev, msm_uport->tx.dma_base,
  1483. UART_XMIT_SIZE, DMA_TO_DEVICE);
  1484. spin_unlock_irqrestore(&uport->lock, flags);
  1485. if (cancel_work_sync(&msm_uport->rx.tty_work))
  1486. msm_hs_tty_flip_buffer_work(&msm_uport->rx.tty_work);
  1487. }
  1488. static void __exit msm_serial_hs_exit(void)
  1489. {
  1490. flush_workqueue(msm_hs_workqueue);
  1491. destroy_workqueue(msm_hs_workqueue);
  1492. platform_driver_unregister(&msm_serial_hs_platform_driver);
  1493. uart_unregister_driver(&msm_hs_driver);
  1494. }
  1495. module_exit(msm_serial_hs_exit);
  1496. #ifdef CONFIG_PM_RUNTIME
  1497. static int msm_hs_runtime_idle(struct device *dev)
  1498. {
  1499. /*
  1500. * returning success from idle results in runtime suspend to be
  1501. * called
  1502. */
  1503. return 0;
  1504. }
  1505. static int msm_hs_runtime_resume(struct device *dev)
  1506. {
  1507. struct platform_device *pdev = container_of(dev, struct
  1508. platform_device, dev);
  1509. struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
  1510. msm_hs_request_clock_on(&msm_uport->uport);
  1511. return 0;
  1512. }
  1513. static int msm_hs_runtime_suspend(struct device *dev)
  1514. {
  1515. struct platform_device *pdev = container_of(dev, struct
  1516. platform_device, dev);
  1517. struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
  1518. msm_hs_request_clock_off(&msm_uport->uport);
  1519. return 0;
  1520. }
  1521. #else
  1522. #define msm_hs_runtime_idle NULL
  1523. #define msm_hs_runtime_resume NULL
  1524. #define msm_hs_runtime_suspend NULL
  1525. #endif
  1526. static const struct dev_pm_ops msm_hs_dev_pm_ops = {
  1527. .runtime_suspend = msm_hs_runtime_suspend,
  1528. .runtime_resume = msm_hs_runtime_resume,
  1529. .runtime_idle = msm_hs_runtime_idle,
  1530. };
  1531. static struct platform_driver msm_serial_hs_platform_driver = {
  1532. .probe = msm_hs_probe,
  1533. .remove = __devexit_p(msm_hs_remove),
  1534. .driver = {
  1535. .name = "msm_serial_hs",
  1536. .owner = THIS_MODULE,
  1537. .pm = &msm_hs_dev_pm_ops,
  1538. },
  1539. };
  1540. static struct uart_driver msm_hs_driver = {
  1541. .owner = THIS_MODULE,
  1542. .driver_name = "msm_serial_hs",
  1543. .dev_name = "ttyHS",
  1544. .nr = UARTDM_NR,
  1545. .cons = 0,
  1546. };
  1547. static struct uart_ops msm_hs_ops = {
  1548. .tx_empty = msm_hs_tx_empty,
  1549. .set_mctrl = msm_hs_set_mctrl_locked,
  1550. .get_mctrl = msm_hs_get_mctrl_locked,
  1551. .stop_tx = msm_hs_stop_tx_locked,
  1552. .start_tx = msm_hs_start_tx_locked,
  1553. .stop_rx = msm_hs_stop_rx_locked,
  1554. .enable_ms = msm_hs_enable_ms_locked,
  1555. .break_ctl = msm_hs_break_ctl,
  1556. .startup = msm_hs_startup,
  1557. .shutdown = msm_hs_shutdown,
  1558. .set_termios = msm_hs_set_termios,
  1559. .pm = msm_hs_pm,
  1560. .type = msm_hs_type,
  1561. .config_port = msm_hs_config_port,
  1562. .release_port = msm_hs_release_port,
  1563. .request_port = msm_hs_request_port,
  1564. };
  1565. MODULE_DESCRIPTION("High Speed UART Driver for the MSM chipset");
  1566. MODULE_VERSION("1.2");
  1567. MODULE_LICENSE("GPL v2");