msm_serial.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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/hrtimer.h>
  21. #include <linux/module.h>
  22. #include <linux/io.h>
  23. #include <linux/ioport.h>
  24. #include <linux/irq.h>
  25. #include <linux/init.h>
  26. #include <linux/console.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/serial_core.h>
  30. #include <linux/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/delay.h>
  34. #include "msm_serial.h"
  35. struct msm_port {
  36. struct uart_port uart;
  37. char name[16];
  38. struct clk *clk;
  39. struct clk *pclk;
  40. unsigned int imr;
  41. unsigned int *gsbi_base;
  42. int is_uartdm;
  43. unsigned int old_snap_state;
  44. };
  45. static inline void wait_for_xmitr(struct uart_port *port, int bits)
  46. {
  47. if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY))
  48. while ((msm_read(port, UART_ISR) & bits) != bits)
  49. cpu_relax();
  50. }
  51. static void msm_stop_tx(struct uart_port *port)
  52. {
  53. struct msm_port *msm_port = UART_TO_MSM(port);
  54. msm_port->imr &= ~UART_IMR_TXLEV;
  55. msm_write(port, msm_port->imr, UART_IMR);
  56. }
  57. static void msm_start_tx(struct uart_port *port)
  58. {
  59. struct msm_port *msm_port = UART_TO_MSM(port);
  60. msm_port->imr |= UART_IMR_TXLEV;
  61. msm_write(port, msm_port->imr, UART_IMR);
  62. }
  63. static void msm_stop_rx(struct uart_port *port)
  64. {
  65. struct msm_port *msm_port = UART_TO_MSM(port);
  66. msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
  67. msm_write(port, msm_port->imr, UART_IMR);
  68. }
  69. static void msm_enable_ms(struct uart_port *port)
  70. {
  71. struct msm_port *msm_port = UART_TO_MSM(port);
  72. msm_port->imr |= UART_IMR_DELTA_CTS;
  73. msm_write(port, msm_port->imr, UART_IMR);
  74. }
  75. static void handle_rx_dm(struct uart_port *port, unsigned int misr)
  76. {
  77. struct tty_struct *tty = port->state->port.tty;
  78. unsigned int sr;
  79. int count = 0;
  80. struct msm_port *msm_port = UART_TO_MSM(port);
  81. if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
  82. port->icount.overrun++;
  83. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  84. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  85. }
  86. if (misr & UART_IMR_RXSTALE) {
  87. count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
  88. msm_port->old_snap_state;
  89. msm_port->old_snap_state = 0;
  90. } else {
  91. count = 4 * (msm_read(port, UART_RFWR));
  92. msm_port->old_snap_state += count;
  93. }
  94. /* TODO: Precise error reporting */
  95. port->icount.rx += count;
  96. while (count > 0) {
  97. unsigned int c;
  98. sr = msm_read(port, UART_SR);
  99. if ((sr & UART_SR_RX_READY) == 0) {
  100. msm_port->old_snap_state -= count;
  101. break;
  102. }
  103. c = msm_read(port, UARTDM_RF);
  104. if (sr & UART_SR_RX_BREAK) {
  105. port->icount.brk++;
  106. if (uart_handle_break(port))
  107. continue;
  108. } else if (sr & UART_SR_PAR_FRAME_ERR)
  109. port->icount.frame++;
  110. /* TODO: handle sysrq */
  111. tty_insert_flip_string(tty, (char *) &c,
  112. (count > 4) ? 4 : count);
  113. count -= 4;
  114. }
  115. tty_flip_buffer_push(tty);
  116. if (misr & (UART_IMR_RXSTALE))
  117. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  118. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  119. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  120. }
  121. static void handle_rx(struct uart_port *port)
  122. {
  123. struct tty_struct *tty = port->state->port.tty;
  124. unsigned int sr;
  125. /*
  126. * Handle overrun. My understanding of the hardware is that overrun
  127. * is not tied to the RX buffer, so we handle the case out of band.
  128. */
  129. if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
  130. port->icount.overrun++;
  131. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  132. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  133. }
  134. /* and now the main RX loop */
  135. while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
  136. unsigned int c;
  137. char flag = TTY_NORMAL;
  138. c = msm_read(port, UART_RF);
  139. if (sr & UART_SR_RX_BREAK) {
  140. port->icount.brk++;
  141. if (uart_handle_break(port))
  142. continue;
  143. } else if (sr & UART_SR_PAR_FRAME_ERR) {
  144. port->icount.frame++;
  145. } else {
  146. port->icount.rx++;
  147. }
  148. /* Mask conditions we're ignorning. */
  149. sr &= port->read_status_mask;
  150. if (sr & UART_SR_RX_BREAK) {
  151. flag = TTY_BREAK;
  152. } else if (sr & UART_SR_PAR_FRAME_ERR) {
  153. flag = TTY_FRAME;
  154. }
  155. if (!uart_handle_sysrq_char(port, c))
  156. tty_insert_flip_char(tty, c, flag);
  157. }
  158. tty_flip_buffer_push(tty);
  159. }
  160. static void reset_dm_count(struct uart_port *port)
  161. {
  162. wait_for_xmitr(port, UART_ISR_TX_READY);
  163. msm_write(port, 1, UARTDM_NCF_TX);
  164. }
  165. static void handle_tx(struct uart_port *port)
  166. {
  167. struct circ_buf *xmit = &port->state->xmit;
  168. struct msm_port *msm_port = UART_TO_MSM(port);
  169. int sent_tx;
  170. if (port->x_char) {
  171. if (msm_port->is_uartdm)
  172. reset_dm_count(port);
  173. msm_write(port, port->x_char,
  174. msm_port->is_uartdm ? UARTDM_TF : UART_TF);
  175. port->icount.tx++;
  176. port->x_char = 0;
  177. }
  178. if (msm_port->is_uartdm)
  179. reset_dm_count(port);
  180. while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
  181. if (uart_circ_empty(xmit)) {
  182. /* disable tx interrupts */
  183. msm_port->imr &= ~UART_IMR_TXLEV;
  184. msm_write(port, msm_port->imr, UART_IMR);
  185. break;
  186. }
  187. msm_write(port, xmit->buf[xmit->tail],
  188. msm_port->is_uartdm ? UARTDM_TF : UART_TF);
  189. if (msm_port->is_uartdm)
  190. reset_dm_count(port);
  191. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  192. port->icount.tx++;
  193. sent_tx = 1;
  194. }
  195. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  196. uart_write_wakeup(port);
  197. }
  198. static void handle_delta_cts(struct uart_port *port)
  199. {
  200. msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
  201. port->icount.cts++;
  202. wake_up_interruptible(&port->state->port.delta_msr_wait);
  203. }
  204. static irqreturn_t msm_irq(int irq, void *dev_id)
  205. {
  206. struct uart_port *port = dev_id;
  207. struct msm_port *msm_port = UART_TO_MSM(port);
  208. unsigned int misr;
  209. spin_lock(&port->lock);
  210. misr = msm_read(port, UART_MISR);
  211. msm_write(port, 0, UART_IMR); /* disable interrupt */
  212. if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
  213. if (msm_port->is_uartdm)
  214. handle_rx_dm(port, misr);
  215. else
  216. handle_rx(port);
  217. }
  218. if (misr & UART_IMR_TXLEV)
  219. handle_tx(port);
  220. if (misr & UART_IMR_DELTA_CTS)
  221. handle_delta_cts(port);
  222. msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
  223. spin_unlock(&port->lock);
  224. return IRQ_HANDLED;
  225. }
  226. static unsigned int msm_tx_empty(struct uart_port *port)
  227. {
  228. return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
  229. }
  230. static unsigned int msm_get_mctrl(struct uart_port *port)
  231. {
  232. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
  233. }
  234. static void msm_reset(struct uart_port *port)
  235. {
  236. /* reset everything */
  237. msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
  238. msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
  239. msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
  240. msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
  241. msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
  242. msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
  243. }
  244. void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
  245. {
  246. unsigned int mr;
  247. mr = msm_read(port, UART_MR1);
  248. if (!(mctrl & TIOCM_RTS)) {
  249. mr &= ~UART_MR1_RX_RDY_CTL;
  250. msm_write(port, mr, UART_MR1);
  251. msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
  252. } else {
  253. mr |= UART_MR1_RX_RDY_CTL;
  254. msm_write(port, mr, UART_MR1);
  255. }
  256. }
  257. static void msm_break_ctl(struct uart_port *port, int break_ctl)
  258. {
  259. if (break_ctl)
  260. msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
  261. else
  262. msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
  263. }
  264. static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
  265. {
  266. unsigned int baud_code, rxstale, watermark;
  267. struct msm_port *msm_port = UART_TO_MSM(port);
  268. switch (baud) {
  269. case 300:
  270. baud_code = UART_CSR_300;
  271. rxstale = 1;
  272. break;
  273. case 600:
  274. baud_code = UART_CSR_600;
  275. rxstale = 1;
  276. break;
  277. case 1200:
  278. baud_code = UART_CSR_1200;
  279. rxstale = 1;
  280. break;
  281. case 2400:
  282. baud_code = UART_CSR_2400;
  283. rxstale = 1;
  284. break;
  285. case 4800:
  286. baud_code = UART_CSR_4800;
  287. rxstale = 1;
  288. break;
  289. case 9600:
  290. baud_code = UART_CSR_9600;
  291. rxstale = 2;
  292. break;
  293. case 14400:
  294. baud_code = UART_CSR_14400;
  295. rxstale = 3;
  296. break;
  297. case 19200:
  298. baud_code = UART_CSR_19200;
  299. rxstale = 4;
  300. break;
  301. case 28800:
  302. baud_code = UART_CSR_28800;
  303. rxstale = 6;
  304. break;
  305. case 38400:
  306. baud_code = UART_CSR_38400;
  307. rxstale = 8;
  308. break;
  309. case 57600:
  310. baud_code = UART_CSR_57600;
  311. rxstale = 16;
  312. break;
  313. case 115200:
  314. default:
  315. baud_code = UART_CSR_115200;
  316. baud = 115200;
  317. rxstale = 31;
  318. break;
  319. }
  320. if (msm_port->is_uartdm)
  321. msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
  322. msm_write(port, baud_code, UART_CSR);
  323. /* RX stale watermark */
  324. watermark = UART_IPR_STALE_LSB & rxstale;
  325. watermark |= UART_IPR_RXSTALE_LAST;
  326. watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
  327. msm_write(port, watermark, UART_IPR);
  328. /* set RX watermark */
  329. watermark = (port->fifosize * 3) / 4;
  330. msm_write(port, watermark, UART_RFWR);
  331. /* set TX watermark */
  332. msm_write(port, 10, UART_TFWR);
  333. if (msm_port->is_uartdm) {
  334. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  335. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  336. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  337. }
  338. return baud;
  339. }
  340. static void msm_init_clock(struct uart_port *port)
  341. {
  342. struct msm_port *msm_port = UART_TO_MSM(port);
  343. clk_enable(msm_port->clk);
  344. if (!IS_ERR(msm_port->pclk))
  345. clk_enable(msm_port->pclk);
  346. msm_serial_set_mnd_regs(port);
  347. }
  348. static int msm_startup(struct uart_port *port)
  349. {
  350. struct msm_port *msm_port = UART_TO_MSM(port);
  351. unsigned int data, rfr_level;
  352. int ret;
  353. snprintf(msm_port->name, sizeof(msm_port->name),
  354. "msm_serial%d", port->line);
  355. ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
  356. msm_port->name, port);
  357. if (unlikely(ret))
  358. return ret;
  359. msm_init_clock(port);
  360. if (likely(port->fifosize > 12))
  361. rfr_level = port->fifosize - 12;
  362. else
  363. rfr_level = port->fifosize;
  364. /* set automatic RFR level */
  365. data = msm_read(port, UART_MR1);
  366. data &= ~UART_MR1_AUTO_RFR_LEVEL1;
  367. data &= ~UART_MR1_AUTO_RFR_LEVEL0;
  368. data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
  369. data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
  370. msm_write(port, data, UART_MR1);
  371. /* make sure that RXSTALE count is non-zero */
  372. data = msm_read(port, UART_IPR);
  373. if (unlikely(!data)) {
  374. data |= UART_IPR_RXSTALE_LAST;
  375. data |= UART_IPR_STALE_LSB;
  376. msm_write(port, data, UART_IPR);
  377. }
  378. data = 0;
  379. if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
  380. msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
  381. msm_reset(port);
  382. data = UART_CR_TX_ENABLE;
  383. }
  384. data |= UART_CR_RX_ENABLE;
  385. msm_write(port, data, UART_CR); /* enable TX & RX */
  386. /* Make sure IPR is not 0 to start with*/
  387. if (msm_port->is_uartdm)
  388. msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
  389. /* turn on RX and CTS interrupts */
  390. msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
  391. UART_IMR_CURRENT_CTS;
  392. if (msm_port->is_uartdm) {
  393. msm_write(port, 0xFFFFFF, UARTDM_DMRX);
  394. msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
  395. msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
  396. }
  397. msm_write(port, msm_port->imr, UART_IMR);
  398. return 0;
  399. }
  400. static void msm_shutdown(struct uart_port *port)
  401. {
  402. struct msm_port *msm_port = UART_TO_MSM(port);
  403. msm_port->imr = 0;
  404. msm_write(port, 0, UART_IMR); /* disable interrupts */
  405. clk_disable(msm_port->clk);
  406. free_irq(port->irq, port);
  407. }
  408. static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
  409. struct ktermios *old)
  410. {
  411. unsigned long flags;
  412. unsigned int baud, mr;
  413. spin_lock_irqsave(&port->lock, flags);
  414. /* calculate and set baud rate */
  415. baud = uart_get_baud_rate(port, termios, old, 300, 115200);
  416. baud = msm_set_baud_rate(port, baud);
  417. if (tty_termios_baud_rate(termios))
  418. tty_termios_encode_baud_rate(termios, baud, baud);
  419. /* calculate parity */
  420. mr = msm_read(port, UART_MR2);
  421. mr &= ~UART_MR2_PARITY_MODE;
  422. if (termios->c_cflag & PARENB) {
  423. if (termios->c_cflag & PARODD)
  424. mr |= UART_MR2_PARITY_MODE_ODD;
  425. else if (termios->c_cflag & CMSPAR)
  426. mr |= UART_MR2_PARITY_MODE_SPACE;
  427. else
  428. mr |= UART_MR2_PARITY_MODE_EVEN;
  429. }
  430. /* calculate bits per char */
  431. mr &= ~UART_MR2_BITS_PER_CHAR;
  432. switch (termios->c_cflag & CSIZE) {
  433. case CS5:
  434. mr |= UART_MR2_BITS_PER_CHAR_5;
  435. break;
  436. case CS6:
  437. mr |= UART_MR2_BITS_PER_CHAR_6;
  438. break;
  439. case CS7:
  440. mr |= UART_MR2_BITS_PER_CHAR_7;
  441. break;
  442. case CS8:
  443. default:
  444. mr |= UART_MR2_BITS_PER_CHAR_8;
  445. break;
  446. }
  447. /* calculate stop bits */
  448. mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
  449. if (termios->c_cflag & CSTOPB)
  450. mr |= UART_MR2_STOP_BIT_LEN_TWO;
  451. else
  452. mr |= UART_MR2_STOP_BIT_LEN_ONE;
  453. /* set parity, bits per char, and stop bit */
  454. msm_write(port, mr, UART_MR2);
  455. /* calculate and set hardware flow control */
  456. mr = msm_read(port, UART_MR1);
  457. mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
  458. if (termios->c_cflag & CRTSCTS) {
  459. mr |= UART_MR1_CTS_CTL;
  460. mr |= UART_MR1_RX_RDY_CTL;
  461. }
  462. msm_write(port, mr, UART_MR1);
  463. /* Configure status bits to ignore based on termio flags. */
  464. port->read_status_mask = 0;
  465. if (termios->c_iflag & INPCK)
  466. port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
  467. if (termios->c_iflag & (BRKINT | PARMRK))
  468. port->read_status_mask |= UART_SR_RX_BREAK;
  469. uart_update_timeout(port, termios->c_cflag, baud);
  470. spin_unlock_irqrestore(&port->lock, flags);
  471. }
  472. static const char *msm_type(struct uart_port *port)
  473. {
  474. return "MSM";
  475. }
  476. static void msm_release_port(struct uart_port *port)
  477. {
  478. struct platform_device *pdev = to_platform_device(port->dev);
  479. struct msm_port *msm_port = UART_TO_MSM(port);
  480. struct resource *uart_resource;
  481. struct resource *gsbi_resource;
  482. resource_size_t size;
  483. uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. if (unlikely(!uart_resource))
  485. return;
  486. size = resource_size(uart_resource);
  487. release_mem_region(port->mapbase, size);
  488. iounmap(port->membase);
  489. port->membase = NULL;
  490. if (msm_port->gsbi_base) {
  491. iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base +
  492. GSBI_CONTROL);
  493. gsbi_resource = platform_get_resource_byname(pdev,
  494. IORESOURCE_MEM,
  495. "gsbi_resource");
  496. if (unlikely(!gsbi_resource))
  497. return;
  498. size = resource_size(gsbi_resource);
  499. release_mem_region(gsbi_resource->start, size);
  500. iounmap(msm_port->gsbi_base);
  501. msm_port->gsbi_base = NULL;
  502. }
  503. }
  504. static int msm_request_port(struct uart_port *port)
  505. {
  506. struct msm_port *msm_port = UART_TO_MSM(port);
  507. struct platform_device *pdev = to_platform_device(port->dev);
  508. struct resource *uart_resource;
  509. struct resource *gsbi_resource;
  510. resource_size_t size;
  511. int ret;
  512. uart_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  513. "uart_resource");
  514. if (unlikely(!uart_resource))
  515. return -ENXIO;
  516. size = resource_size(uart_resource);
  517. if (!request_mem_region(port->mapbase, size, "msm_serial"))
  518. return -EBUSY;
  519. port->membase = ioremap(port->mapbase, size);
  520. if (!port->membase) {
  521. ret = -EBUSY;
  522. goto fail_release_port;
  523. }
  524. gsbi_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  525. "gsbi_resource");
  526. /* Is this a GSBI-based port? */
  527. if (gsbi_resource) {
  528. size = resource_size(gsbi_resource);
  529. if (!request_mem_region(gsbi_resource->start, size,
  530. "msm_serial")) {
  531. ret = -EBUSY;
  532. goto fail_release_port;
  533. }
  534. msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
  535. if (!msm_port->gsbi_base) {
  536. ret = -EBUSY;
  537. goto fail_release_gsbi;
  538. }
  539. }
  540. return 0;
  541. fail_release_gsbi:
  542. release_mem_region(gsbi_resource->start, size);
  543. fail_release_port:
  544. release_mem_region(port->mapbase, size);
  545. return ret;
  546. }
  547. static void msm_config_port(struct uart_port *port, int flags)
  548. {
  549. struct msm_port *msm_port = UART_TO_MSM(port);
  550. int ret;
  551. if (flags & UART_CONFIG_TYPE) {
  552. port->type = PORT_MSM;
  553. ret = msm_request_port(port);
  554. if (ret)
  555. return;
  556. }
  557. if (msm_port->is_uartdm)
  558. iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base +
  559. GSBI_CONTROL);
  560. }
  561. static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
  562. {
  563. if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
  564. return -EINVAL;
  565. if (unlikely(port->irq != ser->irq))
  566. return -EINVAL;
  567. return 0;
  568. }
  569. static void msm_power(struct uart_port *port, unsigned int state,
  570. unsigned int oldstate)
  571. {
  572. struct msm_port *msm_port = UART_TO_MSM(port);
  573. switch (state) {
  574. case 0:
  575. clk_enable(msm_port->clk);
  576. if (!IS_ERR(msm_port->pclk))
  577. clk_enable(msm_port->pclk);
  578. break;
  579. case 3:
  580. clk_disable(msm_port->clk);
  581. if (!IS_ERR(msm_port->pclk))
  582. clk_disable(msm_port->pclk);
  583. break;
  584. default:
  585. printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
  586. }
  587. }
  588. static struct uart_ops msm_uart_pops = {
  589. .tx_empty = msm_tx_empty,
  590. .set_mctrl = msm_set_mctrl,
  591. .get_mctrl = msm_get_mctrl,
  592. .stop_tx = msm_stop_tx,
  593. .start_tx = msm_start_tx,
  594. .stop_rx = msm_stop_rx,
  595. .enable_ms = msm_enable_ms,
  596. .break_ctl = msm_break_ctl,
  597. .startup = msm_startup,
  598. .shutdown = msm_shutdown,
  599. .set_termios = msm_set_termios,
  600. .type = msm_type,
  601. .release_port = msm_release_port,
  602. .request_port = msm_request_port,
  603. .config_port = msm_config_port,
  604. .verify_port = msm_verify_port,
  605. .pm = msm_power,
  606. };
  607. static struct msm_port msm_uart_ports[] = {
  608. {
  609. .uart = {
  610. .iotype = UPIO_MEM,
  611. .ops = &msm_uart_pops,
  612. .flags = UPF_BOOT_AUTOCONF,
  613. .fifosize = 64,
  614. .line = 0,
  615. },
  616. },
  617. {
  618. .uart = {
  619. .iotype = UPIO_MEM,
  620. .ops = &msm_uart_pops,
  621. .flags = UPF_BOOT_AUTOCONF,
  622. .fifosize = 64,
  623. .line = 1,
  624. },
  625. },
  626. {
  627. .uart = {
  628. .iotype = UPIO_MEM,
  629. .ops = &msm_uart_pops,
  630. .flags = UPF_BOOT_AUTOCONF,
  631. .fifosize = 64,
  632. .line = 2,
  633. },
  634. },
  635. };
  636. #define UART_NR ARRAY_SIZE(msm_uart_ports)
  637. static inline struct uart_port *get_port_from_line(unsigned int line)
  638. {
  639. return &msm_uart_ports[line].uart;
  640. }
  641. #ifdef CONFIG_SERIAL_MSM_CONSOLE
  642. static void msm_console_putchar(struct uart_port *port, int c)
  643. {
  644. struct msm_port *msm_port = UART_TO_MSM(port);
  645. if (msm_port->is_uartdm)
  646. reset_dm_count(port);
  647. while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
  648. ;
  649. msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
  650. }
  651. static void msm_console_write(struct console *co, const char *s,
  652. unsigned int count)
  653. {
  654. struct uart_port *port;
  655. struct msm_port *msm_port;
  656. BUG_ON(co->index < 0 || co->index >= UART_NR);
  657. port = get_port_from_line(co->index);
  658. msm_port = UART_TO_MSM(port);
  659. spin_lock(&port->lock);
  660. uart_console_write(port, s, count, msm_console_putchar);
  661. spin_unlock(&port->lock);
  662. }
  663. static int __init msm_console_setup(struct console *co, char *options)
  664. {
  665. struct uart_port *port;
  666. struct msm_port *msm_port;
  667. int baud, flow, bits, parity;
  668. if (unlikely(co->index >= UART_NR || co->index < 0))
  669. return -ENXIO;
  670. port = get_port_from_line(co->index);
  671. msm_port = UART_TO_MSM(port);
  672. if (unlikely(!port->membase))
  673. return -ENXIO;
  674. port->cons = co;
  675. msm_init_clock(port);
  676. if (options)
  677. uart_parse_options(options, &baud, &parity, &bits, &flow);
  678. bits = 8;
  679. parity = 'n';
  680. flow = 'n';
  681. msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
  682. UART_MR2); /* 8N1 */
  683. if (baud < 300 || baud > 115200)
  684. baud = 115200;
  685. msm_set_baud_rate(port, baud);
  686. msm_reset(port);
  687. if (msm_port->is_uartdm) {
  688. msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
  689. msm_write(port, UART_CR_TX_ENABLE, UART_CR);
  690. }
  691. printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
  692. return uart_set_options(port, co, baud, parity, bits, flow);
  693. }
  694. static struct uart_driver msm_uart_driver;
  695. static struct console msm_console = {
  696. .name = "ttyMSM",
  697. .write = msm_console_write,
  698. .device = uart_console_device,
  699. .setup = msm_console_setup,
  700. .flags = CON_PRINTBUFFER,
  701. .index = -1,
  702. .data = &msm_uart_driver,
  703. };
  704. #define MSM_CONSOLE (&msm_console)
  705. #else
  706. #define MSM_CONSOLE NULL
  707. #endif
  708. static struct uart_driver msm_uart_driver = {
  709. .owner = THIS_MODULE,
  710. .driver_name = "msm_serial",
  711. .dev_name = "ttyMSM",
  712. .nr = UART_NR,
  713. .cons = MSM_CONSOLE,
  714. };
  715. static int __init msm_serial_probe(struct platform_device *pdev)
  716. {
  717. struct msm_port *msm_port;
  718. struct resource *resource;
  719. struct uart_port *port;
  720. int irq;
  721. if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
  722. return -ENXIO;
  723. printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
  724. port = get_port_from_line(pdev->id);
  725. port->dev = &pdev->dev;
  726. msm_port = UART_TO_MSM(port);
  727. if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsbi_resource"))
  728. msm_port->is_uartdm = 1;
  729. else
  730. msm_port->is_uartdm = 0;
  731. if (msm_port->is_uartdm) {
  732. msm_port->clk = clk_get(&pdev->dev, "gsbi_uart_clk");
  733. msm_port->pclk = clk_get(&pdev->dev, "gsbi_pclk");
  734. } else {
  735. msm_port->clk = clk_get(&pdev->dev, "uart_clk");
  736. msm_port->pclk = ERR_PTR(-ENOENT);
  737. }
  738. if (unlikely(IS_ERR(msm_port->clk) || (IS_ERR(msm_port->pclk) &&
  739. msm_port->is_uartdm)))
  740. return PTR_ERR(msm_port->clk);
  741. if (msm_port->is_uartdm)
  742. clk_set_rate(msm_port->clk, 7372800);
  743. port->uartclk = clk_get_rate(msm_port->clk);
  744. printk(KERN_INFO "uartclk = %d\n", port->uartclk);
  745. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  746. "uart_resource");
  747. if (unlikely(!resource))
  748. return -ENXIO;
  749. port->mapbase = resource->start;
  750. irq = platform_get_irq(pdev, 0);
  751. if (unlikely(irq < 0))
  752. return -ENXIO;
  753. port->irq = irq;
  754. platform_set_drvdata(pdev, port);
  755. return uart_add_one_port(&msm_uart_driver, port);
  756. }
  757. static int __devexit msm_serial_remove(struct platform_device *pdev)
  758. {
  759. struct msm_port *msm_port = platform_get_drvdata(pdev);
  760. clk_put(msm_port->clk);
  761. return 0;
  762. }
  763. static struct platform_driver msm_platform_driver = {
  764. .remove = msm_serial_remove,
  765. .driver = {
  766. .name = "msm_serial",
  767. .owner = THIS_MODULE,
  768. },
  769. };
  770. static int __init msm_serial_init(void)
  771. {
  772. int ret;
  773. ret = uart_register_driver(&msm_uart_driver);
  774. if (unlikely(ret))
  775. return ret;
  776. ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
  777. if (unlikely(ret))
  778. uart_unregister_driver(&msm_uart_driver);
  779. printk(KERN_INFO "msm_serial: driver initialized\n");
  780. return ret;
  781. }
  782. static void __exit msm_serial_exit(void)
  783. {
  784. #ifdef CONFIG_SERIAL_MSM_CONSOLE
  785. unregister_console(&msm_console);
  786. #endif
  787. platform_driver_unregister(&msm_platform_driver);
  788. uart_unregister_driver(&msm_uart_driver);
  789. }
  790. module_init(msm_serial_init);
  791. module_exit(msm_serial_exit);
  792. MODULE_AUTHOR("Robert Love <rlove@google.com>");
  793. MODULE_DESCRIPTION("Driver for msm7x serial device");
  794. MODULE_LICENSE("GPL");