msm_serial.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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/atomic.h>
  21. #include <linux/hrtimer.h>
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/irq.h>
  26. #include <linux/init.h>
  27. #include <linux/console.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial_core.h>
  31. #include <linux/serial.h>
  32. #include <linux/clk.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/delay.h>
  35. #include <linux/of.h>
  36. #include <linux/of_device.h>
  37. #include "msm_serial.h"
  38. enum {
  39. UARTDM_1P1 = 1,
  40. UARTDM_1P2,
  41. UARTDM_1P3,
  42. UARTDM_1P4,
  43. };
  44. struct msm_port {
  45. struct uart_port uart;
  46. char name[16];
  47. struct clk *clk;
  48. struct clk *pclk;
  49. unsigned int imr;
  50. int is_uartdm;
  51. unsigned int old_snap_state;
  52. };
  53. static inline void wait_for_xmitr(struct uart_port *port)
  54. {
  55. while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
  56. if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
  57. break;
  58. udelay(1);
  59. }
  60. msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
  61. }
  62. static void msm_stop_tx(struct uart_port *port)
  63. {
  64. struct msm_port *msm_port = UART_TO_MSM(port);
  65. msm_port->imr &= ~UART_IMR_TXLEV;
  66. msm_write(port, msm_port->imr, UART_IMR);
  67. }
  68. static void msm_start_tx(struct uart_port *port)
  69. {
  70. struct msm_port *msm_port = UART_TO_MSM(port);
  71. msm_port->imr |= UART_IMR_TXLEV;
  72. msm_write(port, msm_port->imr, UART_IMR);
  73. }
  74. static void msm_stop_rx(struct uart_port *port)
  75. {
  76. struct msm_port *msm_port = UART_TO_MSM(port);
  77. msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
  78. msm_write(port, msm_port->imr, UART_IMR);
  79. }
  80. static void msm_enable_ms(struct uart_port *port)
  81. {
  82. struct msm_port *msm_port = UART_TO_MSM(port);
  83. msm_port->imr |= UART_IMR_DELTA_CTS;
  84. msm_write(port, msm_port->imr, UART_IMR);
  85. }
  86. static void handle_rx_dm(struct uart_port *port, unsigned int misr)
  87. {
  88. struct tty_port *tport = &port->state->port;
  89. unsigned int sr;
  90. int count = 0;
  91. struct msm_port *msm_port = UART_TO_MSM(port);
  92. if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
  93. port->icount.overrun++;
  94. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  95. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  96. }
  97. if (misr & UART_IMR_RXSTALE) {
  98. count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
  99. msm_port->old_snap_state;
  100. msm_port->old_snap_state = 0;
  101. } else {
  102. count = 4 * (msm_read(port, UART_RFWR));
  103. msm_port->old_snap_state += count;
  104. }
  105. /* TODO: Precise error reporting */
  106. port->icount.rx += count;
  107. while (count > 0) {
  108. unsigned char buf[4];
  109. sr = msm_read(port, UART_SR);
  110. if ((sr & UART_SR_RX_READY) == 0) {
  111. msm_port->old_snap_state -= count;
  112. break;
  113. }
  114. ioread32_rep(port->membase + UARTDM_RF, buf, 1);
  115. if (sr & UART_SR_RX_BREAK) {
  116. port->icount.brk++;
  117. if (uart_handle_break(port))
  118. continue;
  119. } else if (sr & UART_SR_PAR_FRAME_ERR)
  120. port->icount.frame++;
  121. /* TODO: handle sysrq */
  122. tty_insert_flip_string(tport, buf, min(count, 4));
  123. count -= 4;
  124. }
  125. spin_unlock(&port->lock);
  126. tty_flip_buffer_push(tport);
  127. spin_lock(&port->lock);
  128. if (misr & (UART_IMR_RXSTALE))
  129. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  130. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  131. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  132. }
  133. static void handle_rx(struct uart_port *port)
  134. {
  135. struct tty_port *tport = &port->state->port;
  136. unsigned int sr;
  137. /*
  138. * Handle overrun. My understanding of the hardware is that overrun
  139. * is not tied to the RX buffer, so we handle the case out of band.
  140. */
  141. if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
  142. port->icount.overrun++;
  143. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  144. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  145. }
  146. /* and now the main RX loop */
  147. while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
  148. unsigned int c;
  149. char flag = TTY_NORMAL;
  150. c = msm_read(port, UART_RF);
  151. if (sr & UART_SR_RX_BREAK) {
  152. port->icount.brk++;
  153. if (uart_handle_break(port))
  154. continue;
  155. } else if (sr & UART_SR_PAR_FRAME_ERR) {
  156. port->icount.frame++;
  157. } else {
  158. port->icount.rx++;
  159. }
  160. /* Mask conditions we're ignorning. */
  161. sr &= port->read_status_mask;
  162. if (sr & UART_SR_RX_BREAK)
  163. flag = TTY_BREAK;
  164. else if (sr & UART_SR_PAR_FRAME_ERR)
  165. flag = TTY_FRAME;
  166. if (!uart_handle_sysrq_char(port, c))
  167. tty_insert_flip_char(tport, c, flag);
  168. }
  169. spin_unlock(&port->lock);
  170. tty_flip_buffer_push(tport);
  171. spin_lock(&port->lock);
  172. }
  173. static void reset_dm_count(struct uart_port *port, int count)
  174. {
  175. wait_for_xmitr(port);
  176. msm_write(port, count, UARTDM_NCF_TX);
  177. msm_read(port, UARTDM_NCF_TX);
  178. }
  179. static void handle_tx(struct uart_port *port)
  180. {
  181. struct circ_buf *xmit = &port->state->xmit;
  182. struct msm_port *msm_port = UART_TO_MSM(port);
  183. unsigned int tx_count, num_chars;
  184. unsigned int tf_pointer = 0;
  185. void __iomem *tf;
  186. if (msm_port->is_uartdm)
  187. tf = port->membase + UARTDM_TF;
  188. else
  189. tf = port->membase + UART_TF;
  190. tx_count = uart_circ_chars_pending(xmit);
  191. tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
  192. port->fifosize);
  193. if (port->x_char) {
  194. if (msm_port->is_uartdm)
  195. reset_dm_count(port, tx_count + 1);
  196. iowrite8_rep(tf, &port->x_char, 1);
  197. port->icount.tx++;
  198. port->x_char = 0;
  199. } else if (tx_count && msm_port->is_uartdm) {
  200. reset_dm_count(port, tx_count);
  201. }
  202. while (tf_pointer < tx_count) {
  203. int i;
  204. char buf[4] = { 0 };
  205. if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
  206. break;
  207. if (msm_port->is_uartdm)
  208. num_chars = min(tx_count - tf_pointer,
  209. (unsigned int)sizeof(buf));
  210. else
  211. num_chars = 1;
  212. for (i = 0; i < num_chars; i++) {
  213. buf[i] = xmit->buf[xmit->tail + i];
  214. port->icount.tx++;
  215. }
  216. iowrite32_rep(tf, buf, 1);
  217. xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
  218. tf_pointer += num_chars;
  219. }
  220. /* disable tx interrupts if nothing more to send */
  221. if (uart_circ_empty(xmit))
  222. msm_stop_tx(port);
  223. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  224. uart_write_wakeup(port);
  225. }
  226. static void handle_delta_cts(struct uart_port *port)
  227. {
  228. msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
  229. port->icount.cts++;
  230. wake_up_interruptible(&port->state->port.delta_msr_wait);
  231. }
  232. static irqreturn_t msm_irq(int irq, void *dev_id)
  233. {
  234. struct uart_port *port = dev_id;
  235. struct msm_port *msm_port = UART_TO_MSM(port);
  236. unsigned int misr;
  237. spin_lock(&port->lock);
  238. misr = msm_read(port, UART_MISR);
  239. msm_write(port, 0, UART_IMR); /* disable interrupt */
  240. if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
  241. if (msm_port->is_uartdm)
  242. handle_rx_dm(port, misr);
  243. else
  244. handle_rx(port);
  245. }
  246. if (misr & UART_IMR_TXLEV)
  247. handle_tx(port);
  248. if (misr & UART_IMR_DELTA_CTS)
  249. handle_delta_cts(port);
  250. msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
  251. spin_unlock(&port->lock);
  252. return IRQ_HANDLED;
  253. }
  254. static unsigned int msm_tx_empty(struct uart_port *port)
  255. {
  256. return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
  257. }
  258. static unsigned int msm_get_mctrl(struct uart_port *port)
  259. {
  260. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
  261. }
  262. static void msm_reset(struct uart_port *port)
  263. {
  264. struct msm_port *msm_port = UART_TO_MSM(port);
  265. /* reset everything */
  266. msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
  267. msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
  268. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  269. msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
  270. msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
  271. msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
  272. /* Disable DM modes */
  273. if (msm_port->is_uartdm)
  274. msm_write(port, 0, UARTDM_DMEN);
  275. }
  276. static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
  277. {
  278. unsigned int mr;
  279. mr = msm_read(port, UART_MR1);
  280. if (!(mctrl & TIOCM_RTS)) {
  281. mr &= ~UART_MR1_RX_RDY_CTL;
  282. msm_write(port, mr, UART_MR1);
  283. msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
  284. } else {
  285. mr |= UART_MR1_RX_RDY_CTL;
  286. msm_write(port, mr, UART_MR1);
  287. }
  288. }
  289. static void msm_break_ctl(struct uart_port *port, int break_ctl)
  290. {
  291. if (break_ctl)
  292. msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
  293. else
  294. msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
  295. }
  296. struct msm_baud_map {
  297. u16 divisor;
  298. u8 code;
  299. u8 rxstale;
  300. };
  301. static const struct msm_baud_map *
  302. msm_find_best_baud(struct uart_port *port, unsigned int baud)
  303. {
  304. unsigned int i, divisor;
  305. const struct msm_baud_map *entry;
  306. static const struct msm_baud_map table[] = {
  307. { 1536, 0x00, 1 },
  308. { 768, 0x11, 1 },
  309. { 384, 0x22, 1 },
  310. { 192, 0x33, 1 },
  311. { 96, 0x44, 1 },
  312. { 48, 0x55, 1 },
  313. { 32, 0x66, 1 },
  314. { 24, 0x77, 1 },
  315. { 16, 0x88, 1 },
  316. { 12, 0x99, 6 },
  317. { 8, 0xaa, 6 },
  318. { 6, 0xbb, 6 },
  319. { 4, 0xcc, 6 },
  320. { 3, 0xdd, 8 },
  321. { 2, 0xee, 16 },
  322. { 1, 0xff, 31 },
  323. };
  324. divisor = uart_get_divisor(port, baud);
  325. for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
  326. if (entry->divisor <= divisor)
  327. break;
  328. return entry; /* Default to smallest divider */
  329. }
  330. static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
  331. {
  332. unsigned int rxstale, watermark;
  333. struct msm_port *msm_port = UART_TO_MSM(port);
  334. const struct msm_baud_map *entry;
  335. entry = msm_find_best_baud(port, baud);
  336. if (msm_port->is_uartdm)
  337. msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
  338. msm_write(port, entry->code, UART_CSR);
  339. /* RX stale watermark */
  340. rxstale = entry->rxstale;
  341. watermark = UART_IPR_STALE_LSB & rxstale;
  342. watermark |= UART_IPR_RXSTALE_LAST;
  343. watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
  344. msm_write(port, watermark, UART_IPR);
  345. /* set RX watermark */
  346. watermark = (port->fifosize * 3) / 4;
  347. msm_write(port, watermark, UART_RFWR);
  348. /* set TX watermark */
  349. msm_write(port, 10, UART_TFWR);
  350. if (msm_port->is_uartdm) {
  351. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  352. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  353. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  354. }
  355. return baud;
  356. }
  357. static void msm_init_clock(struct uart_port *port)
  358. {
  359. struct msm_port *msm_port = UART_TO_MSM(port);
  360. clk_prepare_enable(msm_port->clk);
  361. clk_prepare_enable(msm_port->pclk);
  362. msm_serial_set_mnd_regs(port);
  363. }
  364. static int msm_startup(struct uart_port *port)
  365. {
  366. struct msm_port *msm_port = UART_TO_MSM(port);
  367. unsigned int data, rfr_level;
  368. int ret;
  369. snprintf(msm_port->name, sizeof(msm_port->name),
  370. "msm_serial%d", port->line);
  371. ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
  372. msm_port->name, port);
  373. if (unlikely(ret))
  374. return ret;
  375. msm_init_clock(port);
  376. if (likely(port->fifosize > 12))
  377. rfr_level = port->fifosize - 12;
  378. else
  379. rfr_level = port->fifosize;
  380. /* set automatic RFR level */
  381. data = msm_read(port, UART_MR1);
  382. data &= ~UART_MR1_AUTO_RFR_LEVEL1;
  383. data &= ~UART_MR1_AUTO_RFR_LEVEL0;
  384. data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
  385. data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
  386. msm_write(port, data, UART_MR1);
  387. /* make sure that RXSTALE count is non-zero */
  388. data = msm_read(port, UART_IPR);
  389. if (unlikely(!data)) {
  390. data |= UART_IPR_RXSTALE_LAST;
  391. data |= UART_IPR_STALE_LSB;
  392. msm_write(port, data, UART_IPR);
  393. }
  394. data = 0;
  395. if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
  396. msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
  397. msm_reset(port);
  398. data = UART_CR_TX_ENABLE;
  399. }
  400. data |= UART_CR_RX_ENABLE;
  401. msm_write(port, data, UART_CR); /* enable TX & RX */
  402. /* Make sure IPR is not 0 to start with*/
  403. if (msm_port->is_uartdm)
  404. msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
  405. /* turn on RX and CTS interrupts */
  406. msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
  407. UART_IMR_CURRENT_CTS;
  408. if (msm_port->is_uartdm) {
  409. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  410. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  411. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  412. }
  413. msm_write(port, msm_port->imr, UART_IMR);
  414. return 0;
  415. }
  416. static void msm_shutdown(struct uart_port *port)
  417. {
  418. struct msm_port *msm_port = UART_TO_MSM(port);
  419. msm_port->imr = 0;
  420. msm_write(port, 0, UART_IMR); /* disable interrupts */
  421. clk_disable_unprepare(msm_port->clk);
  422. free_irq(port->irq, port);
  423. }
  424. static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
  425. struct ktermios *old)
  426. {
  427. unsigned long flags;
  428. unsigned int baud, mr;
  429. spin_lock_irqsave(&port->lock, flags);
  430. /* calculate and set baud rate */
  431. baud = uart_get_baud_rate(port, termios, old, 300, 115200);
  432. baud = msm_set_baud_rate(port, baud);
  433. if (tty_termios_baud_rate(termios))
  434. tty_termios_encode_baud_rate(termios, baud, baud);
  435. /* calculate parity */
  436. mr = msm_read(port, UART_MR2);
  437. mr &= ~UART_MR2_PARITY_MODE;
  438. if (termios->c_cflag & PARENB) {
  439. if (termios->c_cflag & PARODD)
  440. mr |= UART_MR2_PARITY_MODE_ODD;
  441. else if (termios->c_cflag & CMSPAR)
  442. mr |= UART_MR2_PARITY_MODE_SPACE;
  443. else
  444. mr |= UART_MR2_PARITY_MODE_EVEN;
  445. }
  446. /* calculate bits per char */
  447. mr &= ~UART_MR2_BITS_PER_CHAR;
  448. switch (termios->c_cflag & CSIZE) {
  449. case CS5:
  450. mr |= UART_MR2_BITS_PER_CHAR_5;
  451. break;
  452. case CS6:
  453. mr |= UART_MR2_BITS_PER_CHAR_6;
  454. break;
  455. case CS7:
  456. mr |= UART_MR2_BITS_PER_CHAR_7;
  457. break;
  458. case CS8:
  459. default:
  460. mr |= UART_MR2_BITS_PER_CHAR_8;
  461. break;
  462. }
  463. /* calculate stop bits */
  464. mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
  465. if (termios->c_cflag & CSTOPB)
  466. mr |= UART_MR2_STOP_BIT_LEN_TWO;
  467. else
  468. mr |= UART_MR2_STOP_BIT_LEN_ONE;
  469. /* set parity, bits per char, and stop bit */
  470. msm_write(port, mr, UART_MR2);
  471. /* calculate and set hardware flow control */
  472. mr = msm_read(port, UART_MR1);
  473. mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
  474. if (termios->c_cflag & CRTSCTS) {
  475. mr |= UART_MR1_CTS_CTL;
  476. mr |= UART_MR1_RX_RDY_CTL;
  477. }
  478. msm_write(port, mr, UART_MR1);
  479. /* Configure status bits to ignore based on termio flags. */
  480. port->read_status_mask = 0;
  481. if (termios->c_iflag & INPCK)
  482. port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
  483. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  484. port->read_status_mask |= UART_SR_RX_BREAK;
  485. uart_update_timeout(port, termios->c_cflag, baud);
  486. spin_unlock_irqrestore(&port->lock, flags);
  487. }
  488. static const char *msm_type(struct uart_port *port)
  489. {
  490. return "MSM";
  491. }
  492. static void msm_release_port(struct uart_port *port)
  493. {
  494. struct platform_device *pdev = to_platform_device(port->dev);
  495. struct resource *uart_resource;
  496. resource_size_t size;
  497. uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  498. if (unlikely(!uart_resource))
  499. return;
  500. size = resource_size(uart_resource);
  501. release_mem_region(port->mapbase, size);
  502. iounmap(port->membase);
  503. port->membase = NULL;
  504. }
  505. static int msm_request_port(struct uart_port *port)
  506. {
  507. struct platform_device *pdev = to_platform_device(port->dev);
  508. struct resource *uart_resource;
  509. resource_size_t size;
  510. int ret;
  511. uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  512. if (unlikely(!uart_resource))
  513. return -ENXIO;
  514. size = resource_size(uart_resource);
  515. if (!request_mem_region(port->mapbase, size, "msm_serial"))
  516. return -EBUSY;
  517. port->membase = ioremap(port->mapbase, size);
  518. if (!port->membase) {
  519. ret = -EBUSY;
  520. goto fail_release_port;
  521. }
  522. return 0;
  523. fail_release_port:
  524. release_mem_region(port->mapbase, size);
  525. return ret;
  526. }
  527. static void msm_config_port(struct uart_port *port, int flags)
  528. {
  529. int ret;
  530. if (flags & UART_CONFIG_TYPE) {
  531. port->type = PORT_MSM;
  532. ret = msm_request_port(port);
  533. if (ret)
  534. return;
  535. }
  536. }
  537. static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
  538. {
  539. if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
  540. return -EINVAL;
  541. if (unlikely(port->irq != ser->irq))
  542. return -EINVAL;
  543. return 0;
  544. }
  545. static void msm_power(struct uart_port *port, unsigned int state,
  546. unsigned int oldstate)
  547. {
  548. struct msm_port *msm_port = UART_TO_MSM(port);
  549. switch (state) {
  550. case 0:
  551. clk_prepare_enable(msm_port->clk);
  552. clk_prepare_enable(msm_port->pclk);
  553. break;
  554. case 3:
  555. clk_disable_unprepare(msm_port->clk);
  556. clk_disable_unprepare(msm_port->pclk);
  557. break;
  558. default:
  559. pr_err("msm_serial: Unknown PM state %d\n", state);
  560. }
  561. }
  562. #ifdef CONFIG_CONSOLE_POLL
  563. static int msm_poll_get_char_single(struct uart_port *port)
  564. {
  565. struct msm_port *msm_port = UART_TO_MSM(port);
  566. unsigned int rf_reg = msm_port->is_uartdm ? UARTDM_RF : UART_RF;
  567. if (!(msm_read(port, UART_SR) & UART_SR_RX_READY))
  568. return NO_POLL_CHAR;
  569. return msm_read(port, rf_reg) & 0xff;
  570. }
  571. static int msm_poll_get_char_dm(struct uart_port *port)
  572. {
  573. int c;
  574. static u32 slop;
  575. static int count;
  576. unsigned char *sp = (unsigned char *)&slop;
  577. /* Check if a previous read had more than one char */
  578. if (count) {
  579. c = sp[sizeof(slop) - count];
  580. count--;
  581. /* Or if FIFO is empty */
  582. } else if (!(msm_read(port, UART_SR) & UART_SR_RX_READY)) {
  583. /*
  584. * If RX packing buffer has less than a word, force stale to
  585. * push contents into RX FIFO
  586. */
  587. count = msm_read(port, UARTDM_RXFS);
  588. count = (count >> UARTDM_RXFS_BUF_SHIFT) & UARTDM_RXFS_BUF_MASK;
  589. if (count) {
  590. msm_write(port, UART_CR_CMD_FORCE_STALE, UART_CR);
  591. slop = msm_read(port, UARTDM_RF);
  592. c = sp[0];
  593. count--;
  594. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  595. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  596. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE,
  597. UART_CR);
  598. } else {
  599. c = NO_POLL_CHAR;
  600. }
  601. /* FIFO has a word */
  602. } else {
  603. slop = msm_read(port, UARTDM_RF);
  604. c = sp[0];
  605. count = sizeof(slop) - 1;
  606. }
  607. return c;
  608. }
  609. static int msm_poll_get_char(struct uart_port *port)
  610. {
  611. u32 imr;
  612. int c;
  613. struct msm_port *msm_port = UART_TO_MSM(port);
  614. /* Disable all interrupts */
  615. imr = msm_read(port, UART_IMR);
  616. msm_write(port, 0, UART_IMR);
  617. if (msm_port->is_uartdm)
  618. c = msm_poll_get_char_dm(port);
  619. else
  620. c = msm_poll_get_char_single(port);
  621. /* Enable interrupts */
  622. msm_write(port, imr, UART_IMR);
  623. return c;
  624. }
  625. static void msm_poll_put_char(struct uart_port *port, unsigned char c)
  626. {
  627. u32 imr;
  628. struct msm_port *msm_port = UART_TO_MSM(port);
  629. /* Disable all interrupts */
  630. imr = msm_read(port, UART_IMR);
  631. msm_write(port, 0, UART_IMR);
  632. if (msm_port->is_uartdm)
  633. reset_dm_count(port, 1);
  634. /* Wait until FIFO is empty */
  635. while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
  636. cpu_relax();
  637. /* Write a character */
  638. msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
  639. /* Wait until FIFO is empty */
  640. while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
  641. cpu_relax();
  642. /* Enable interrupts */
  643. msm_write(port, imr, UART_IMR);
  644. }
  645. #endif
  646. static struct uart_ops msm_uart_pops = {
  647. .tx_empty = msm_tx_empty,
  648. .set_mctrl = msm_set_mctrl,
  649. .get_mctrl = msm_get_mctrl,
  650. .stop_tx = msm_stop_tx,
  651. .start_tx = msm_start_tx,
  652. .stop_rx = msm_stop_rx,
  653. .enable_ms = msm_enable_ms,
  654. .break_ctl = msm_break_ctl,
  655. .startup = msm_startup,
  656. .shutdown = msm_shutdown,
  657. .set_termios = msm_set_termios,
  658. .type = msm_type,
  659. .release_port = msm_release_port,
  660. .request_port = msm_request_port,
  661. .config_port = msm_config_port,
  662. .verify_port = msm_verify_port,
  663. .pm = msm_power,
  664. #ifdef CONFIG_CONSOLE_POLL
  665. .poll_get_char = msm_poll_get_char,
  666. .poll_put_char = msm_poll_put_char,
  667. #endif
  668. };
  669. static struct msm_port msm_uart_ports[] = {
  670. {
  671. .uart = {
  672. .iotype = UPIO_MEM,
  673. .ops = &msm_uart_pops,
  674. .flags = UPF_BOOT_AUTOCONF,
  675. .fifosize = 64,
  676. .line = 0,
  677. },
  678. },
  679. {
  680. .uart = {
  681. .iotype = UPIO_MEM,
  682. .ops = &msm_uart_pops,
  683. .flags = UPF_BOOT_AUTOCONF,
  684. .fifosize = 64,
  685. .line = 1,
  686. },
  687. },
  688. {
  689. .uart = {
  690. .iotype = UPIO_MEM,
  691. .ops = &msm_uart_pops,
  692. .flags = UPF_BOOT_AUTOCONF,
  693. .fifosize = 64,
  694. .line = 2,
  695. },
  696. },
  697. };
  698. #define UART_NR ARRAY_SIZE(msm_uart_ports)
  699. static inline struct uart_port *get_port_from_line(unsigned int line)
  700. {
  701. return &msm_uart_ports[line].uart;
  702. }
  703. #ifdef CONFIG_SERIAL_MSM_CONSOLE
  704. static void __msm_console_write(struct uart_port *port, const char *s,
  705. unsigned int count, bool is_uartdm)
  706. {
  707. int i;
  708. int num_newlines = 0;
  709. bool replaced = false;
  710. void __iomem *tf;
  711. if (is_uartdm)
  712. tf = port->membase + UARTDM_TF;
  713. else
  714. tf = port->membase + UART_TF;
  715. /* Account for newlines that will get a carriage return added */
  716. for (i = 0; i < count; i++)
  717. if (s[i] == '\n')
  718. num_newlines++;
  719. count += num_newlines;
  720. spin_lock(&port->lock);
  721. if (is_uartdm)
  722. reset_dm_count(port, count);
  723. i = 0;
  724. while (i < count) {
  725. int j;
  726. unsigned int num_chars;
  727. char buf[4] = { 0 };
  728. if (is_uartdm)
  729. num_chars = min(count - i, (unsigned int)sizeof(buf));
  730. else
  731. num_chars = 1;
  732. for (j = 0; j < num_chars; j++) {
  733. char c = *s;
  734. if (c == '\n' && !replaced) {
  735. buf[j] = '\r';
  736. j++;
  737. replaced = true;
  738. }
  739. if (j < num_chars) {
  740. buf[j] = c;
  741. s++;
  742. replaced = false;
  743. }
  744. }
  745. while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
  746. cpu_relax();
  747. iowrite32_rep(tf, buf, 1);
  748. i += num_chars;
  749. }
  750. spin_unlock(&port->lock);
  751. }
  752. static void msm_console_write(struct console *co, const char *s,
  753. unsigned int count)
  754. {
  755. struct uart_port *port;
  756. struct msm_port *msm_port;
  757. BUG_ON(co->index < 0 || co->index >= UART_NR);
  758. port = get_port_from_line(co->index);
  759. msm_port = UART_TO_MSM(port);
  760. __msm_console_write(port, s, count, msm_port->is_uartdm);
  761. }
  762. static int __init msm_console_setup(struct console *co, char *options)
  763. {
  764. struct uart_port *port;
  765. struct msm_port *msm_port;
  766. int baud = 0, flow, bits, parity;
  767. if (unlikely(co->index >= UART_NR || co->index < 0))
  768. return -ENXIO;
  769. port = get_port_from_line(co->index);
  770. msm_port = UART_TO_MSM(port);
  771. if (unlikely(!port->membase))
  772. return -ENXIO;
  773. msm_init_clock(port);
  774. if (options)
  775. uart_parse_options(options, &baud, &parity, &bits, &flow);
  776. bits = 8;
  777. parity = 'n';
  778. flow = 'n';
  779. msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
  780. UART_MR2); /* 8N1 */
  781. if (baud < 300 || baud > 115200)
  782. baud = 115200;
  783. msm_set_baud_rate(port, baud);
  784. msm_reset(port);
  785. if (msm_port->is_uartdm) {
  786. msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
  787. msm_write(port, UART_CR_TX_ENABLE, UART_CR);
  788. }
  789. pr_info("msm_serial: console setup on port #%d\n", port->line);
  790. return uart_set_options(port, co, baud, parity, bits, flow);
  791. }
  792. static void
  793. msm_serial_early_write(struct console *con, const char *s, unsigned n)
  794. {
  795. struct earlycon_device *dev = con->data;
  796. __msm_console_write(&dev->port, s, n, false);
  797. }
  798. static int __init
  799. msm_serial_early_console_setup(struct earlycon_device *device, const char *opt)
  800. {
  801. if (!device->port.membase)
  802. return -ENODEV;
  803. device->con->write = msm_serial_early_write;
  804. return 0;
  805. }
  806. EARLYCON_DECLARE(msm_serial, msm_serial_early_console_setup);
  807. OF_EARLYCON_DECLARE(msm_serial, "qcom,msm-uart",
  808. msm_serial_early_console_setup);
  809. static void
  810. msm_serial_early_write_dm(struct console *con, const char *s, unsigned n)
  811. {
  812. struct earlycon_device *dev = con->data;
  813. __msm_console_write(&dev->port, s, n, true);
  814. }
  815. static int __init
  816. msm_serial_early_console_setup_dm(struct earlycon_device *device,
  817. const char *opt)
  818. {
  819. if (!device->port.membase)
  820. return -ENODEV;
  821. device->con->write = msm_serial_early_write_dm;
  822. return 0;
  823. }
  824. EARLYCON_DECLARE(msm_serial_dm, msm_serial_early_console_setup_dm);
  825. OF_EARLYCON_DECLARE(msm_serial_dm, "qcom,msm-uartdm",
  826. msm_serial_early_console_setup_dm);
  827. static struct uart_driver msm_uart_driver;
  828. static struct console msm_console = {
  829. .name = "ttyMSM",
  830. .write = msm_console_write,
  831. .device = uart_console_device,
  832. .setup = msm_console_setup,
  833. .flags = CON_PRINTBUFFER,
  834. .index = -1,
  835. .data = &msm_uart_driver,
  836. };
  837. #define MSM_CONSOLE (&msm_console)
  838. #else
  839. #define MSM_CONSOLE NULL
  840. #endif
  841. static struct uart_driver msm_uart_driver = {
  842. .owner = THIS_MODULE,
  843. .driver_name = "msm_serial",
  844. .dev_name = "ttyMSM",
  845. .nr = UART_NR,
  846. .cons = MSM_CONSOLE,
  847. };
  848. static atomic_t msm_uart_next_id = ATOMIC_INIT(0);
  849. static const struct of_device_id msm_uartdm_table[] = {
  850. { .compatible = "qcom,msm-uartdm-v1.1", .data = (void *)UARTDM_1P1 },
  851. { .compatible = "qcom,msm-uartdm-v1.2", .data = (void *)UARTDM_1P2 },
  852. { .compatible = "qcom,msm-uartdm-v1.3", .data = (void *)UARTDM_1P3 },
  853. { .compatible = "qcom,msm-uartdm-v1.4", .data = (void *)UARTDM_1P4 },
  854. { }
  855. };
  856. static int msm_serial_probe(struct platform_device *pdev)
  857. {
  858. struct msm_port *msm_port;
  859. struct resource *resource;
  860. struct uart_port *port;
  861. const struct of_device_id *id;
  862. int irq;
  863. if (pdev->id == -1)
  864. pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;
  865. if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
  866. return -ENXIO;
  867. dev_info(&pdev->dev, "msm_serial: detected port #%d\n", pdev->id);
  868. port = get_port_from_line(pdev->id);
  869. port->dev = &pdev->dev;
  870. msm_port = UART_TO_MSM(port);
  871. id = of_match_device(msm_uartdm_table, &pdev->dev);
  872. if (id)
  873. msm_port->is_uartdm = (unsigned long)id->data;
  874. else
  875. msm_port->is_uartdm = 0;
  876. msm_port->clk = devm_clk_get(&pdev->dev, "core");
  877. if (IS_ERR(msm_port->clk))
  878. return PTR_ERR(msm_port->clk);
  879. if (msm_port->is_uartdm) {
  880. msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
  881. if (IS_ERR(msm_port->pclk))
  882. return PTR_ERR(msm_port->pclk);
  883. clk_set_rate(msm_port->clk, 1843200);
  884. }
  885. port->uartclk = clk_get_rate(msm_port->clk);
  886. dev_info(&pdev->dev, "uartclk = %d\n", port->uartclk);
  887. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  888. if (unlikely(!resource))
  889. return -ENXIO;
  890. port->mapbase = resource->start;
  891. irq = platform_get_irq(pdev, 0);
  892. if (unlikely(irq < 0))
  893. return -ENXIO;
  894. port->irq = irq;
  895. platform_set_drvdata(pdev, port);
  896. return uart_add_one_port(&msm_uart_driver, port);
  897. }
  898. static int msm_serial_remove(struct platform_device *pdev)
  899. {
  900. struct uart_port *port = platform_get_drvdata(pdev);
  901. uart_remove_one_port(&msm_uart_driver, port);
  902. return 0;
  903. }
  904. static const struct of_device_id msm_match_table[] = {
  905. { .compatible = "qcom,msm-uart" },
  906. { .compatible = "qcom,msm-uartdm" },
  907. {}
  908. };
  909. static struct platform_driver msm_platform_driver = {
  910. .remove = msm_serial_remove,
  911. .probe = msm_serial_probe,
  912. .driver = {
  913. .name = "msm_serial",
  914. .owner = THIS_MODULE,
  915. .of_match_table = msm_match_table,
  916. },
  917. };
  918. static int __init msm_serial_init(void)
  919. {
  920. int ret;
  921. ret = uart_register_driver(&msm_uart_driver);
  922. if (unlikely(ret))
  923. return ret;
  924. ret = platform_driver_register(&msm_platform_driver);
  925. if (unlikely(ret))
  926. uart_unregister_driver(&msm_uart_driver);
  927. pr_info("msm_serial: driver initialized\n");
  928. return ret;
  929. }
  930. static void __exit msm_serial_exit(void)
  931. {
  932. #ifdef CONFIG_SERIAL_MSM_CONSOLE
  933. unregister_console(&msm_console);
  934. #endif
  935. platform_driver_unregister(&msm_platform_driver);
  936. uart_unregister_driver(&msm_uart_driver);
  937. }
  938. module_init(msm_serial_init);
  939. module_exit(msm_serial_exit);
  940. MODULE_AUTHOR("Robert Love <rlove@google.com>");
  941. MODULE_DESCRIPTION("Driver for msm7x serial device");
  942. MODULE_LICENSE("GPL");