msm_serial_hs.c 52 KB

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