imx.c 23 KB

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