msm_serial.c 26 KB

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