imx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * linux/drivers/serial/imx.c
  3. *
  4. * Driver for Motorola IMX serial ports
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * Author: Sascha Hauer <sascha@saschahauer.de>
  9. * Copyright (C) 2004 Pengutronix
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * [29-Mar-2005] Mike Lee
  26. * Added hardware handshake
  27. */
  28. #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  29. #define SUPPORT_SYSRQ
  30. #endif
  31. #include <linux/module.h>
  32. #include <linux/ioport.h>
  33. #include <linux/init.h>
  34. #include <linux/console.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/tty.h>
  38. #include <linux/tty_flip.h>
  39. #include <linux/serial_core.h>
  40. #include <linux/serial.h>
  41. #include <asm/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/hardware.h>
  44. #include <asm/arch/imx-uart.h>
  45. /* We've been assigned a range on the "Low-density serial ports" major */
  46. #define SERIAL_IMX_MAJOR 204
  47. #define MINOR_START 41
  48. #define NR_PORTS 2
  49. #define IMX_ISR_PASS_LIMIT 256
  50. /*
  51. * This is the size of our serial port register set.
  52. */
  53. #define UART_PORT_SIZE 0x100
  54. /*
  55. * This determines how often we check the modem status signals
  56. * for any change. They generally aren't connected to an IRQ
  57. * so we have to poll them. We also check immediately before
  58. * filling the TX fifo incase CTS has been dropped.
  59. */
  60. #define MCTRL_TIMEOUT (250*HZ/1000)
  61. #define DRIVER_NAME "IMX-uart"
  62. struct imx_port {
  63. struct uart_port port;
  64. struct timer_list timer;
  65. unsigned int old_status;
  66. int txirq,rxirq,rtsirq;
  67. int have_rtscts:1;
  68. };
  69. /*
  70. * Handle any change of modem status signal since we were last called.
  71. */
  72. static void imx_mctrl_check(struct imx_port *sport)
  73. {
  74. unsigned int status, changed;
  75. status = sport->port.ops->get_mctrl(&sport->port);
  76. changed = status ^ sport->old_status;
  77. if (changed == 0)
  78. return;
  79. sport->old_status = status;
  80. if (changed & TIOCM_RI)
  81. sport->port.icount.rng++;
  82. if (changed & TIOCM_DSR)
  83. sport->port.icount.dsr++;
  84. if (changed & TIOCM_CAR)
  85. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  86. if (changed & TIOCM_CTS)
  87. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  88. wake_up_interruptible(&sport->port.info->delta_msr_wait);
  89. }
  90. /*
  91. * This is our per-port timeout handler, for checking the
  92. * modem status signals.
  93. */
  94. static void imx_timeout(unsigned long data)
  95. {
  96. struct imx_port *sport = (struct imx_port *)data;
  97. unsigned long flags;
  98. if (sport->port.info) {
  99. spin_lock_irqsave(&sport->port.lock, flags);
  100. imx_mctrl_check(sport);
  101. spin_unlock_irqrestore(&sport->port.lock, flags);
  102. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  103. }
  104. }
  105. /*
  106. * interrupts disabled on entry
  107. */
  108. static void imx_stop_tx(struct uart_port *port)
  109. {
  110. struct imx_port *sport = (struct imx_port *)port;
  111. UCR1((u32)sport->port.membase) &= ~UCR1_TXMPTYEN;
  112. }
  113. /*
  114. * interrupts disabled on entry
  115. */
  116. static void imx_stop_rx(struct uart_port *port)
  117. {
  118. struct imx_port *sport = (struct imx_port *)port;
  119. UCR2((u32)sport->port.membase) &= ~UCR2_RXEN;
  120. }
  121. /*
  122. * Set the modem control timer to fire immediately.
  123. */
  124. static void imx_enable_ms(struct uart_port *port)
  125. {
  126. struct imx_port *sport = (struct imx_port *)port;
  127. mod_timer(&sport->timer, jiffies);
  128. }
  129. static inline void imx_transmit_buffer(struct imx_port *sport)
  130. {
  131. struct circ_buf *xmit = &sport->port.info->xmit;
  132. do {
  133. /* send xmit->buf[xmit->tail]
  134. * out the port here */
  135. URTX0((u32)sport->port.membase) = xmit->buf[xmit->tail];
  136. xmit->tail = (xmit->tail + 1) &
  137. (UART_XMIT_SIZE - 1);
  138. sport->port.icount.tx++;
  139. if (uart_circ_empty(xmit))
  140. break;
  141. } while (!(UTS((u32)sport->port.membase) & UTS_TXFULL));
  142. if (uart_circ_empty(xmit))
  143. imx_stop_tx(&sport->port);
  144. }
  145. /*
  146. * interrupts disabled on entry
  147. */
  148. static void imx_start_tx(struct uart_port *port)
  149. {
  150. struct imx_port *sport = (struct imx_port *)port;
  151. UCR1((u32)sport->port.membase) |= UCR1_TXMPTYEN;
  152. if(UTS((u32)sport->port.membase) & UTS_TXEMPTY)
  153. imx_transmit_buffer(sport);
  154. }
  155. static irqreturn_t imx_rtsint(int irq, void *dev_id)
  156. {
  157. struct imx_port *sport = (struct imx_port *)dev_id;
  158. unsigned int val = USR1((u32)sport->port.membase)&USR1_RTSS;
  159. unsigned long flags;
  160. spin_lock_irqsave(&sport->port.lock, flags);
  161. USR1((u32)sport->port.membase) = USR1_RTSD;
  162. uart_handle_cts_change(&sport->port, !!val);
  163. wake_up_interruptible(&sport->port.info->delta_msr_wait);
  164. spin_unlock_irqrestore(&sport->port.lock, flags);
  165. return IRQ_HANDLED;
  166. }
  167. static irqreturn_t imx_txint(int irq, void *dev_id)
  168. {
  169. struct imx_port *sport = (struct imx_port *)dev_id;
  170. struct circ_buf *xmit = &sport->port.info->xmit;
  171. unsigned long flags;
  172. spin_lock_irqsave(&sport->port.lock,flags);
  173. if (sport->port.x_char)
  174. {
  175. /* Send next char */
  176. URTX0((u32)sport->port.membase) = sport->port.x_char;
  177. goto out;
  178. }
  179. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  180. imx_stop_tx(&sport->port);
  181. goto out;
  182. }
  183. imx_transmit_buffer(sport);
  184. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  185. uart_write_wakeup(&sport->port);
  186. out:
  187. spin_unlock_irqrestore(&sport->port.lock,flags);
  188. return IRQ_HANDLED;
  189. }
  190. static irqreturn_t imx_rxint(int irq, void *dev_id)
  191. {
  192. struct imx_port *sport = dev_id;
  193. unsigned int rx,flg,ignored = 0;
  194. struct tty_struct *tty = sport->port.info->tty;
  195. unsigned long flags;
  196. rx = URXD0((u32)sport->port.membase);
  197. spin_lock_irqsave(&sport->port.lock,flags);
  198. do {
  199. flg = TTY_NORMAL;
  200. sport->port.icount.rx++;
  201. if( USR2((u32)sport->port.membase) & USR2_BRCD ) {
  202. USR2((u32)sport->port.membase) |= USR2_BRCD;
  203. if(uart_handle_break(&sport->port))
  204. goto ignore_char;
  205. }
  206. if (uart_handle_sysrq_char
  207. (&sport->port, (unsigned char)rx))
  208. goto ignore_char;
  209. if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
  210. goto handle_error;
  211. error_return:
  212. tty_insert_flip_char(tty, rx, flg);
  213. ignore_char:
  214. rx = URXD0((u32)sport->port.membase);
  215. } while(rx & URXD_CHARRDY);
  216. out:
  217. spin_unlock_irqrestore(&sport->port.lock,flags);
  218. tty_flip_buffer_push(tty);
  219. return IRQ_HANDLED;
  220. handle_error:
  221. if (rx & URXD_PRERR)
  222. sport->port.icount.parity++;
  223. else if (rx & URXD_FRMERR)
  224. sport->port.icount.frame++;
  225. if (rx & URXD_OVRRUN)
  226. sport->port.icount.overrun++;
  227. if (rx & sport->port.ignore_status_mask) {
  228. if (++ignored > 100)
  229. goto out;
  230. goto ignore_char;
  231. }
  232. rx &= sport->port.read_status_mask;
  233. if (rx & URXD_PRERR)
  234. flg = TTY_PARITY;
  235. else if (rx & URXD_FRMERR)
  236. flg = TTY_FRAME;
  237. if (rx & URXD_OVRRUN)
  238. flg = TTY_OVERRUN;
  239. #ifdef SUPPORT_SYSRQ
  240. sport->port.sysrq = 0;
  241. #endif
  242. goto error_return;
  243. }
  244. /*
  245. * Return TIOCSER_TEMT when transmitter is not busy.
  246. */
  247. static unsigned int imx_tx_empty(struct uart_port *port)
  248. {
  249. struct imx_port *sport = (struct imx_port *)port;
  250. return USR2((u32)sport->port.membase) & USR2_TXDC ? TIOCSER_TEMT : 0;
  251. }
  252. /*
  253. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  254. */
  255. static unsigned int imx_get_mctrl(struct uart_port *port)
  256. {
  257. struct imx_port *sport = (struct imx_port *)port;
  258. unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
  259. if (USR1((u32)sport->port.membase) & USR1_RTSS)
  260. tmp |= TIOCM_CTS;
  261. if (UCR2((u32)sport->port.membase) & UCR2_CTS)
  262. tmp |= TIOCM_RTS;
  263. return tmp;
  264. }
  265. static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  266. {
  267. struct imx_port *sport = (struct imx_port *)port;
  268. if (mctrl & TIOCM_RTS)
  269. UCR2((u32)sport->port.membase) |= UCR2_CTS;
  270. else
  271. UCR2((u32)sport->port.membase) &= ~UCR2_CTS;
  272. }
  273. /*
  274. * Interrupts always disabled.
  275. */
  276. static void imx_break_ctl(struct uart_port *port, int break_state)
  277. {
  278. struct imx_port *sport = (struct imx_port *)port;
  279. unsigned long flags;
  280. spin_lock_irqsave(&sport->port.lock, flags);
  281. if ( break_state != 0 )
  282. UCR1((u32)sport->port.membase) |= UCR1_SNDBRK;
  283. else
  284. UCR1((u32)sport->port.membase) &= ~UCR1_SNDBRK;
  285. spin_unlock_irqrestore(&sport->port.lock, flags);
  286. }
  287. #define TXTL 2 /* reset default */
  288. #define RXTL 1 /* reset default */
  289. static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
  290. {
  291. unsigned int val;
  292. unsigned int ufcr_rfdiv;
  293. /* set receiver / transmitter trigger level.
  294. * RFDIV is set such way to satisfy requested uartclk value
  295. */
  296. val = TXTL<<10 | RXTL;
  297. ufcr_rfdiv = (imx_get_perclk1() + sport->port.uartclk / 2) / sport->port.uartclk;
  298. if(!ufcr_rfdiv)
  299. ufcr_rfdiv = 1;
  300. if(ufcr_rfdiv >= 7)
  301. ufcr_rfdiv = 6;
  302. else
  303. ufcr_rfdiv = 6 - ufcr_rfdiv;
  304. val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
  305. UFCR((u32)sport->port.membase) = val;
  306. return 0;
  307. }
  308. static int imx_startup(struct uart_port *port)
  309. {
  310. struct imx_port *sport = (struct imx_port *)port;
  311. int retval;
  312. unsigned long flags;
  313. imx_setup_ufcr(sport, 0);
  314. /* disable the DREN bit (Data Ready interrupt enable) before
  315. * requesting IRQs
  316. */
  317. UCR4((u32)sport->port.membase) &= ~UCR4_DREN;
  318. /*
  319. * Allocate the IRQ
  320. */
  321. retval = request_irq(sport->rxirq, imx_rxint, 0,
  322. DRIVER_NAME, sport);
  323. if (retval) goto error_out1;
  324. retval = request_irq(sport->txirq, imx_txint, 0,
  325. DRIVER_NAME, sport);
  326. if (retval) goto error_out2;
  327. retval = request_irq(sport->rtsirq, imx_rtsint,
  328. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  329. DRIVER_NAME, sport);
  330. if (retval) goto error_out3;
  331. /*
  332. * Finally, clear and enable interrupts
  333. */
  334. USR1((u32)sport->port.membase) = USR1_RTSD;
  335. UCR1((u32)sport->port.membase) |=
  336. (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  337. UCR2((u32)sport->port.membase) |= (UCR2_RXEN | UCR2_TXEN);
  338. /*
  339. * Enable modem status interrupts
  340. */
  341. spin_lock_irqsave(&sport->port.lock,flags);
  342. imx_enable_ms(&sport->port);
  343. spin_unlock_irqrestore(&sport->port.lock,flags);
  344. return 0;
  345. error_out3:
  346. free_irq(sport->txirq, sport);
  347. error_out2:
  348. free_irq(sport->rxirq, sport);
  349. error_out1:
  350. return retval;
  351. }
  352. static void imx_shutdown(struct uart_port *port)
  353. {
  354. struct imx_port *sport = (struct imx_port *)port;
  355. /*
  356. * Stop our timer.
  357. */
  358. del_timer_sync(&sport->timer);
  359. /*
  360. * Free the interrupts
  361. */
  362. free_irq(sport->rtsirq, sport);
  363. free_irq(sport->txirq, sport);
  364. free_irq(sport->rxirq, sport);
  365. /*
  366. * Disable all interrupts, port and break condition.
  367. */
  368. UCR1((u32)sport->port.membase) &=
  369. ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
  370. }
  371. static void
  372. imx_set_termios(struct uart_port *port, struct termios *termios,
  373. struct termios *old)
  374. {
  375. struct imx_port *sport = (struct imx_port *)port;
  376. unsigned long flags;
  377. unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
  378. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  379. /*
  380. * If we don't support modem control lines, don't allow
  381. * these to be set.
  382. */
  383. if (0) {
  384. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  385. termios->c_cflag |= CLOCAL;
  386. }
  387. /*
  388. * We only support CS7 and CS8.
  389. */
  390. while ((termios->c_cflag & CSIZE) != CS7 &&
  391. (termios->c_cflag & CSIZE) != CS8) {
  392. termios->c_cflag &= ~CSIZE;
  393. termios->c_cflag |= old_csize;
  394. old_csize = CS8;
  395. }
  396. if ((termios->c_cflag & CSIZE) == CS8)
  397. ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
  398. else
  399. ucr2 = UCR2_SRST | UCR2_IRTS;
  400. if (termios->c_cflag & CRTSCTS) {
  401. if( sport->have_rtscts ) {
  402. ucr2 &= ~UCR2_IRTS;
  403. ucr2 |= UCR2_CTSC;
  404. } else {
  405. termios->c_cflag &= ~CRTSCTS;
  406. }
  407. }
  408. if (termios->c_cflag & CSTOPB)
  409. ucr2 |= UCR2_STPB;
  410. if (termios->c_cflag & PARENB) {
  411. ucr2 |= UCR2_PREN;
  412. if (termios->c_cflag & PARODD)
  413. ucr2 |= UCR2_PROE;
  414. }
  415. /*
  416. * Ask the core to calculate the divisor for us.
  417. */
  418. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  419. quot = uart_get_divisor(port, baud);
  420. spin_lock_irqsave(&sport->port.lock, flags);
  421. sport->port.read_status_mask = 0;
  422. if (termios->c_iflag & INPCK)
  423. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  424. if (termios->c_iflag & (BRKINT | PARMRK))
  425. sport->port.read_status_mask |= URXD_BRK;
  426. /*
  427. * Characters to ignore
  428. */
  429. sport->port.ignore_status_mask = 0;
  430. if (termios->c_iflag & IGNPAR)
  431. sport->port.ignore_status_mask |= URXD_PRERR;
  432. if (termios->c_iflag & IGNBRK) {
  433. sport->port.ignore_status_mask |= URXD_BRK;
  434. /*
  435. * If we're ignoring parity and break indicators,
  436. * ignore overruns too (for real raw support).
  437. */
  438. if (termios->c_iflag & IGNPAR)
  439. sport->port.ignore_status_mask |= URXD_OVRRUN;
  440. }
  441. del_timer_sync(&sport->timer);
  442. /*
  443. * Update the per-port timeout.
  444. */
  445. uart_update_timeout(port, termios->c_cflag, baud);
  446. /*
  447. * disable interrupts and drain transmitter
  448. */
  449. old_ucr1 = UCR1((u32)sport->port.membase);
  450. UCR1((u32)sport->port.membase) &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  451. while ( !(USR2((u32)sport->port.membase) & USR2_TXDC))
  452. barrier();
  453. /* then, disable everything */
  454. old_txrxen = UCR2((u32)sport->port.membase) & ( UCR2_TXEN | UCR2_RXEN );
  455. UCR2((u32)sport->port.membase) &= ~( UCR2_TXEN | UCR2_RXEN);
  456. /* set the parity, stop bits and data size */
  457. UCR2((u32)sport->port.membase) = ucr2;
  458. /* set the baud rate. We assume uartclk = 16 MHz
  459. *
  460. * baud * 16 UBIR - 1
  461. * --------- = --------
  462. * uartclk UBMR - 1
  463. */
  464. UBIR((u32)sport->port.membase) = (baud / 100) - 1;
  465. UBMR((u32)sport->port.membase) = 10000 - 1;
  466. UCR1((u32)sport->port.membase) = old_ucr1;
  467. UCR2((u32)sport->port.membase) |= old_txrxen;
  468. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  469. imx_enable_ms(&sport->port);
  470. spin_unlock_irqrestore(&sport->port.lock, flags);
  471. }
  472. static const char *imx_type(struct uart_port *port)
  473. {
  474. struct imx_port *sport = (struct imx_port *)port;
  475. return sport->port.type == PORT_IMX ? "IMX" : NULL;
  476. }
  477. /*
  478. * Release the memory region(s) being used by 'port'.
  479. */
  480. static void imx_release_port(struct uart_port *port)
  481. {
  482. struct imx_port *sport = (struct imx_port *)port;
  483. release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
  484. }
  485. /*
  486. * Request the memory region(s) being used by 'port'.
  487. */
  488. static int imx_request_port(struct uart_port *port)
  489. {
  490. struct imx_port *sport = (struct imx_port *)port;
  491. return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
  492. "imx-uart") != NULL ? 0 : -EBUSY;
  493. }
  494. /*
  495. * Configure/autoconfigure the port.
  496. */
  497. static void imx_config_port(struct uart_port *port, int flags)
  498. {
  499. struct imx_port *sport = (struct imx_port *)port;
  500. if (flags & UART_CONFIG_TYPE &&
  501. imx_request_port(&sport->port) == 0)
  502. sport->port.type = PORT_IMX;
  503. }
  504. /*
  505. * Verify the new serial_struct (for TIOCSSERIAL).
  506. * The only change we allow are to the flags and type, and
  507. * even then only between PORT_IMX and PORT_UNKNOWN
  508. */
  509. static int
  510. imx_verify_port(struct uart_port *port, struct serial_struct *ser)
  511. {
  512. struct imx_port *sport = (struct imx_port *)port;
  513. int ret = 0;
  514. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  515. ret = -EINVAL;
  516. if (sport->port.irq != ser->irq)
  517. ret = -EINVAL;
  518. if (ser->io_type != UPIO_MEM)
  519. ret = -EINVAL;
  520. if (sport->port.uartclk / 16 != ser->baud_base)
  521. ret = -EINVAL;
  522. if ((void *)sport->port.mapbase != ser->iomem_base)
  523. ret = -EINVAL;
  524. if (sport->port.iobase != ser->port)
  525. ret = -EINVAL;
  526. if (ser->hub6 != 0)
  527. ret = -EINVAL;
  528. return ret;
  529. }
  530. static struct uart_ops imx_pops = {
  531. .tx_empty = imx_tx_empty,
  532. .set_mctrl = imx_set_mctrl,
  533. .get_mctrl = imx_get_mctrl,
  534. .stop_tx = imx_stop_tx,
  535. .start_tx = imx_start_tx,
  536. .stop_rx = imx_stop_rx,
  537. .enable_ms = imx_enable_ms,
  538. .break_ctl = imx_break_ctl,
  539. .startup = imx_startup,
  540. .shutdown = imx_shutdown,
  541. .set_termios = imx_set_termios,
  542. .type = imx_type,
  543. .release_port = imx_release_port,
  544. .request_port = imx_request_port,
  545. .config_port = imx_config_port,
  546. .verify_port = imx_verify_port,
  547. };
  548. static struct imx_port imx_ports[] = {
  549. {
  550. .txirq = UART1_MINT_TX,
  551. .rxirq = UART1_MINT_RX,
  552. .rtsirq = UART1_MINT_RTS,
  553. .port = {
  554. .type = PORT_IMX,
  555. .iotype = UPIO_MEM,
  556. .membase = (void *)IMX_UART1_BASE,
  557. .mapbase = IMX_UART1_BASE, /* FIXME */
  558. .irq = UART1_MINT_RX,
  559. .uartclk = 16000000,
  560. .fifosize = 8,
  561. .flags = UPF_BOOT_AUTOCONF,
  562. .ops = &imx_pops,
  563. .line = 0,
  564. },
  565. }, {
  566. .txirq = UART2_MINT_TX,
  567. .rxirq = UART2_MINT_RX,
  568. .rtsirq = UART2_MINT_RTS,
  569. .port = {
  570. .type = PORT_IMX,
  571. .iotype = UPIO_MEM,
  572. .membase = (void *)IMX_UART2_BASE,
  573. .mapbase = IMX_UART2_BASE, /* FIXME */
  574. .irq = UART2_MINT_RX,
  575. .uartclk = 16000000,
  576. .fifosize = 8,
  577. .flags = UPF_BOOT_AUTOCONF,
  578. .ops = &imx_pops,
  579. .line = 1,
  580. },
  581. }
  582. };
  583. /*
  584. * Setup the IMX serial ports.
  585. * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
  586. * Which serial port this ends up being depends on the machine you're
  587. * running this kernel on. I'm not convinced that this is a good idea,
  588. * but that's the way it traditionally works.
  589. *
  590. */
  591. static void __init imx_init_ports(void)
  592. {
  593. static int first = 1;
  594. int i;
  595. if (!first)
  596. return;
  597. first = 0;
  598. for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
  599. init_timer(&imx_ports[i].timer);
  600. imx_ports[i].timer.function = imx_timeout;
  601. imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
  602. }
  603. }
  604. #ifdef CONFIG_SERIAL_IMX_CONSOLE
  605. static void imx_console_putchar(struct uart_port *port, int ch)
  606. {
  607. struct imx_port *sport = (struct imx_port *)port;
  608. while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
  609. barrier();
  610. URTX0((u32)sport->port.membase) = ch;
  611. }
  612. /*
  613. * Interrupts are disabled on entering
  614. */
  615. static void
  616. imx_console_write(struct console *co, const char *s, unsigned int count)
  617. {
  618. struct imx_port *sport = &imx_ports[co->index];
  619. unsigned int old_ucr1, old_ucr2;
  620. /*
  621. * First, save UCR1/2 and then disable interrupts
  622. */
  623. old_ucr1 = UCR1((u32)sport->port.membase);
  624. old_ucr2 = UCR2((u32)sport->port.membase);
  625. UCR1((u32)sport->port.membase) =
  626. (old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN)
  627. & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  628. UCR2((u32)sport->port.membase) = old_ucr2 | UCR2_TXEN;
  629. uart_console_write(&sport->port, s, count, imx_console_putchar);
  630. /*
  631. * Finally, wait for transmitter to become empty
  632. * and restore UCR1/2
  633. */
  634. while (!(USR2((u32)sport->port.membase) & USR2_TXDC));
  635. UCR1((u32)sport->port.membase) = old_ucr1;
  636. UCR2((u32)sport->port.membase) = old_ucr2;
  637. }
  638. /*
  639. * If the port was already initialised (eg, by a boot loader),
  640. * try to determine the current setup.
  641. */
  642. static void __init
  643. imx_console_get_options(struct imx_port *sport, int *baud,
  644. int *parity, int *bits)
  645. {
  646. if ( UCR1((u32)sport->port.membase) | UCR1_UARTEN ) {
  647. /* ok, the port was enabled */
  648. unsigned int ucr2, ubir,ubmr, uartclk;
  649. unsigned int baud_raw;
  650. unsigned int ucfr_rfdiv;
  651. ucr2 = UCR2((u32)sport->port.membase);
  652. *parity = 'n';
  653. if (ucr2 & UCR2_PREN) {
  654. if (ucr2 & UCR2_PROE)
  655. *parity = 'o';
  656. else
  657. *parity = 'e';
  658. }
  659. if (ucr2 & UCR2_WS)
  660. *bits = 8;
  661. else
  662. *bits = 7;
  663. ubir = UBIR((u32)sport->port.membase) & 0xffff;
  664. ubmr = UBMR((u32)sport->port.membase) & 0xffff;
  665. ucfr_rfdiv = (UFCR((u32)sport->port.membase) & UFCR_RFDIV) >> 7;
  666. if (ucfr_rfdiv == 6)
  667. ucfr_rfdiv = 7;
  668. else
  669. ucfr_rfdiv = 6 - ucfr_rfdiv;
  670. uartclk = imx_get_perclk1();
  671. uartclk /= ucfr_rfdiv;
  672. { /*
  673. * The next code provides exact computation of
  674. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  675. * without need of float support or long long division,
  676. * which would be required to prevent 32bit arithmetic overflow
  677. */
  678. unsigned int mul = ubir + 1;
  679. unsigned int div = 16 * (ubmr + 1);
  680. unsigned int rem = uartclk % div;
  681. baud_raw = (uartclk / div) * mul;
  682. baud_raw += (rem * mul + div / 2) / div;
  683. *baud = (baud_raw + 50) / 100 * 100;
  684. }
  685. if(*baud != baud_raw)
  686. printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
  687. baud_raw, *baud);
  688. }
  689. }
  690. static int __init
  691. imx_console_setup(struct console *co, char *options)
  692. {
  693. struct imx_port *sport;
  694. int baud = 9600;
  695. int bits = 8;
  696. int parity = 'n';
  697. int flow = 'n';
  698. /*
  699. * Check whether an invalid uart number has been specified, and
  700. * if so, search for the first available port that does have
  701. * console support.
  702. */
  703. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
  704. co->index = 0;
  705. sport = &imx_ports[co->index];
  706. if (options)
  707. uart_parse_options(options, &baud, &parity, &bits, &flow);
  708. else
  709. imx_console_get_options(sport, &baud, &parity, &bits);
  710. imx_setup_ufcr(sport, 0);
  711. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  712. }
  713. static struct uart_driver imx_reg;
  714. static struct console imx_console = {
  715. .name = "ttySMX",
  716. .write = imx_console_write,
  717. .device = uart_console_device,
  718. .setup = imx_console_setup,
  719. .flags = CON_PRINTBUFFER,
  720. .index = -1,
  721. .data = &imx_reg,
  722. };
  723. static int __init imx_rs_console_init(void)
  724. {
  725. imx_init_ports();
  726. register_console(&imx_console);
  727. return 0;
  728. }
  729. console_initcall(imx_rs_console_init);
  730. #define IMX_CONSOLE &imx_console
  731. #else
  732. #define IMX_CONSOLE NULL
  733. #endif
  734. static struct uart_driver imx_reg = {
  735. .owner = THIS_MODULE,
  736. .driver_name = DRIVER_NAME,
  737. .dev_name = "ttySMX",
  738. .major = SERIAL_IMX_MAJOR,
  739. .minor = MINOR_START,
  740. .nr = ARRAY_SIZE(imx_ports),
  741. .cons = IMX_CONSOLE,
  742. };
  743. static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
  744. {
  745. struct imx_port *sport = platform_get_drvdata(dev);
  746. if (sport)
  747. uart_suspend_port(&imx_reg, &sport->port);
  748. return 0;
  749. }
  750. static int serial_imx_resume(struct platform_device *dev)
  751. {
  752. struct imx_port *sport = platform_get_drvdata(dev);
  753. if (sport)
  754. uart_resume_port(&imx_reg, &sport->port);
  755. return 0;
  756. }
  757. static int serial_imx_probe(struct platform_device *dev)
  758. {
  759. struct imxuart_platform_data *pdata;
  760. imx_ports[dev->id].port.dev = &dev->dev;
  761. pdata = (struct imxuart_platform_data *)dev->dev.platform_data;
  762. if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
  763. imx_ports[dev->id].have_rtscts = 1;
  764. uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
  765. platform_set_drvdata(dev, &imx_ports[dev->id]);
  766. return 0;
  767. }
  768. static int serial_imx_remove(struct platform_device *dev)
  769. {
  770. struct imx_port *sport = platform_get_drvdata(dev);
  771. platform_set_drvdata(dev, NULL);
  772. if (sport)
  773. uart_remove_one_port(&imx_reg, &sport->port);
  774. return 0;
  775. }
  776. static struct platform_driver serial_imx_driver = {
  777. .probe = serial_imx_probe,
  778. .remove = serial_imx_remove,
  779. .suspend = serial_imx_suspend,
  780. .resume = serial_imx_resume,
  781. .driver = {
  782. .name = "imx-uart",
  783. },
  784. };
  785. static int __init imx_serial_init(void)
  786. {
  787. int ret;
  788. printk(KERN_INFO "Serial: IMX driver\n");
  789. imx_init_ports();
  790. ret = uart_register_driver(&imx_reg);
  791. if (ret)
  792. return ret;
  793. ret = platform_driver_register(&serial_imx_driver);
  794. if (ret != 0)
  795. uart_unregister_driver(&imx_reg);
  796. return 0;
  797. }
  798. static void __exit imx_serial_exit(void)
  799. {
  800. uart_unregister_driver(&imx_reg);
  801. platform_driver_unregister(&serial_imx_driver);
  802. }
  803. module_init(imx_serial_init);
  804. module_exit(imx_serial_exit);
  805. MODULE_AUTHOR("Sascha Hauer");
  806. MODULE_DESCRIPTION("IMX generic serial port driver");
  807. MODULE_LICENSE("GPL");