msm_serial.c 44 KB

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