bfin_5xx.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * Blackfin On-Chip Serial Driver
  3. *
  4. * Copyright 2006-2008 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  11. #define SUPPORT_SYSRQ
  12. #endif
  13. #include <linux/module.h>
  14. #include <linux/ioport.h>
  15. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/sysrq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/serial_core.h>
  22. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  23. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  24. #include <linux/kgdb.h>
  25. #include <asm/irq_regs.h>
  26. #endif
  27. #include <asm/gpio.h>
  28. #include <mach/bfin_serial_5xx.h>
  29. #ifdef CONFIG_SERIAL_BFIN_DMA
  30. #include <linux/dma-mapping.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <asm/cacheflush.h>
  34. #endif
  35. /* UART name and device definitions */
  36. #define BFIN_SERIAL_NAME "ttyBF"
  37. #define BFIN_SERIAL_MAJOR 204
  38. #define BFIN_SERIAL_MINOR 64
  39. static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
  40. static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
  41. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  42. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  43. # ifndef CONFIG_SERIAL_BFIN_PIO
  44. # error KGDB only support UART in PIO mode.
  45. # endif
  46. static int kgdboc_port_line;
  47. static int kgdboc_break_enabled;
  48. #endif
  49. /*
  50. * Setup for console. Argument comes from the menuconfig
  51. */
  52. #define DMA_RX_XCOUNT 512
  53. #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
  54. #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
  55. #define CTS_CHECK_JIFFIES (HZ / 50)
  56. #ifdef CONFIG_SERIAL_BFIN_DMA
  57. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
  58. #else
  59. static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
  60. #endif
  61. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
  62. static void bfin_serial_reset_irda(struct uart_port *port);
  63. /*
  64. * interrupts are disabled on entry
  65. */
  66. static void bfin_serial_stop_tx(struct uart_port *port)
  67. {
  68. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  69. #ifdef CONFIG_SERIAL_BFIN_DMA
  70. struct circ_buf *xmit = &uart->port.info->xmit;
  71. #endif
  72. while (!(UART_GET_LSR(uart) & TEMT))
  73. cpu_relax();
  74. #ifdef CONFIG_SERIAL_BFIN_DMA
  75. disable_dma(uart->tx_dma_channel);
  76. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  77. uart->port.icount.tx += uart->tx_count;
  78. uart->tx_count = 0;
  79. uart->tx_done = 1;
  80. #else
  81. #ifdef CONFIG_BF54x
  82. /* Clear TFI bit */
  83. UART_PUT_LSR(uart, TFI);
  84. #endif
  85. UART_CLEAR_IER(uart, ETBEI);
  86. #endif
  87. }
  88. /*
  89. * port is locked and interrupts are disabled
  90. */
  91. static void bfin_serial_start_tx(struct uart_port *port)
  92. {
  93. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  94. struct tty_struct *tty = uart->port.info->port.tty;
  95. /*
  96. * To avoid losting RX interrupt, we reset IR function
  97. * before sending data.
  98. */
  99. if (tty->termios->c_line == N_IRDA)
  100. bfin_serial_reset_irda(port);
  101. #ifdef CONFIG_SERIAL_BFIN_DMA
  102. if (uart->tx_done)
  103. bfin_serial_dma_tx_chars(uart);
  104. #else
  105. UART_SET_IER(uart, ETBEI);
  106. bfin_serial_tx_chars(uart);
  107. #endif
  108. }
  109. /*
  110. * Interrupts are enabled
  111. */
  112. static void bfin_serial_stop_rx(struct uart_port *port)
  113. {
  114. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  115. UART_CLEAR_IER(uart, ERBFI);
  116. }
  117. /*
  118. * Set the modem control timer to fire immediately.
  119. */
  120. static void bfin_serial_enable_ms(struct uart_port *port)
  121. {
  122. }
  123. #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
  124. # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
  125. # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
  126. #else
  127. # define UART_GET_ANOMALY_THRESHOLD(uart) 0
  128. # define UART_SET_ANOMALY_THRESHOLD(uart, v)
  129. #endif
  130. #ifdef CONFIG_SERIAL_BFIN_PIO
  131. static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
  132. {
  133. struct tty_struct *tty = NULL;
  134. unsigned int status, ch, flg;
  135. static struct timeval anomaly_start = { .tv_sec = 0 };
  136. status = UART_GET_LSR(uart);
  137. UART_CLEAR_LSR(uart);
  138. ch = UART_GET_CHAR(uart);
  139. uart->port.icount.rx++;
  140. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  141. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  142. if (kgdb_connected && kgdboc_port_line == uart->port.line)
  143. if (ch == 0x3) {/* Ctrl + C */
  144. kgdb_breakpoint();
  145. return;
  146. }
  147. if (!uart->port.info || !uart->port.info->tty)
  148. return;
  149. #endif
  150. tty = uart->port.info->tty;
  151. if (ANOMALY_05000363) {
  152. /* The BF533 (and BF561) family of processors have a nice anomaly
  153. * where they continuously generate characters for a "single" break.
  154. * We have to basically ignore this flood until the "next" valid
  155. * character comes across. Due to the nature of the flood, it is
  156. * not possible to reliably catch bytes that are sent too quickly
  157. * after this break. So application code talking to the Blackfin
  158. * which sends a break signal must allow at least 1.5 character
  159. * times after the end of the break for things to stabilize. This
  160. * timeout was picked as it must absolutely be larger than 1
  161. * character time +/- some percent. So 1.5 sounds good. All other
  162. * Blackfin families operate properly. Woo.
  163. */
  164. if (anomaly_start.tv_sec) {
  165. struct timeval curr;
  166. suseconds_t usecs;
  167. if ((~ch & (~ch + 1)) & 0xff)
  168. goto known_good_char;
  169. do_gettimeofday(&curr);
  170. if (curr.tv_sec - anomaly_start.tv_sec > 1)
  171. goto known_good_char;
  172. usecs = 0;
  173. if (curr.tv_sec != anomaly_start.tv_sec)
  174. usecs += USEC_PER_SEC;
  175. usecs += curr.tv_usec - anomaly_start.tv_usec;
  176. if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
  177. goto known_good_char;
  178. if (ch)
  179. anomaly_start.tv_sec = 0;
  180. else
  181. anomaly_start = curr;
  182. return;
  183. known_good_char:
  184. status &= ~BI;
  185. anomaly_start.tv_sec = 0;
  186. }
  187. }
  188. if (status & BI) {
  189. if (ANOMALY_05000363)
  190. if (bfin_revid() < 5)
  191. do_gettimeofday(&anomaly_start);
  192. uart->port.icount.brk++;
  193. if (uart_handle_break(&uart->port))
  194. goto ignore_char;
  195. status &= ~(PE | FE);
  196. }
  197. if (status & PE)
  198. uart->port.icount.parity++;
  199. if (status & OE)
  200. uart->port.icount.overrun++;
  201. if (status & FE)
  202. uart->port.icount.frame++;
  203. status &= uart->port.read_status_mask;
  204. if (status & BI)
  205. flg = TTY_BREAK;
  206. else if (status & PE)
  207. flg = TTY_PARITY;
  208. else if (status & FE)
  209. flg = TTY_FRAME;
  210. else
  211. flg = TTY_NORMAL;
  212. if (uart_handle_sysrq_char(&uart->port, ch))
  213. goto ignore_char;
  214. uart_insert_char(&uart->port, status, OE, ch, flg);
  215. ignore_char:
  216. tty_flip_buffer_push(tty);
  217. }
  218. static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
  219. {
  220. struct circ_buf *xmit = &uart->port.info->xmit;
  221. /*
  222. * Check the modem control lines before
  223. * transmitting anything.
  224. */
  225. bfin_serial_mctrl_check(uart);
  226. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  227. #ifdef CONFIG_BF54x
  228. /* Clear TFI bit */
  229. UART_PUT_LSR(uart, TFI);
  230. #endif
  231. UART_CLEAR_IER(uart, ETBEI);
  232. return;
  233. }
  234. if (uart->port.x_char) {
  235. UART_PUT_CHAR(uart, uart->port.x_char);
  236. uart->port.icount.tx++;
  237. uart->port.x_char = 0;
  238. }
  239. while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
  240. UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
  241. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  242. uart->port.icount.tx++;
  243. SSYNC();
  244. }
  245. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  246. uart_write_wakeup(&uart->port);
  247. }
  248. static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
  249. {
  250. struct bfin_serial_port *uart = dev_id;
  251. spin_lock(&uart->port.lock);
  252. while (UART_GET_LSR(uart) & DR)
  253. bfin_serial_rx_chars(uart);
  254. spin_unlock(&uart->port.lock);
  255. return IRQ_HANDLED;
  256. }
  257. static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
  258. {
  259. struct bfin_serial_port *uart = dev_id;
  260. spin_lock(&uart->port.lock);
  261. if (UART_GET_LSR(uart) & THRE)
  262. bfin_serial_tx_chars(uart);
  263. spin_unlock(&uart->port.lock);
  264. return IRQ_HANDLED;
  265. }
  266. #endif
  267. #ifdef CONFIG_SERIAL_BFIN_DMA
  268. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
  269. {
  270. struct circ_buf *xmit = &uart->port.info->xmit;
  271. uart->tx_done = 0;
  272. /*
  273. * Check the modem control lines before
  274. * transmitting anything.
  275. */
  276. bfin_serial_mctrl_check(uart);
  277. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  278. uart->tx_count = 0;
  279. uart->tx_done = 1;
  280. return;
  281. }
  282. if (uart->port.x_char) {
  283. UART_PUT_CHAR(uart, uart->port.x_char);
  284. uart->port.icount.tx++;
  285. uart->port.x_char = 0;
  286. }
  287. uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
  288. if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
  289. uart->tx_count = UART_XMIT_SIZE - xmit->tail;
  290. blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
  291. (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
  292. set_dma_config(uart->tx_dma_channel,
  293. set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
  294. INTR_ON_BUF,
  295. DIMENSION_LINEAR,
  296. DATA_SIZE_8,
  297. DMA_SYNC_RESTART));
  298. set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
  299. set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
  300. set_dma_x_modify(uart->tx_dma_channel, 1);
  301. enable_dma(uart->tx_dma_channel);
  302. UART_SET_IER(uart, ETBEI);
  303. }
  304. static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
  305. {
  306. struct tty_struct *tty = uart->port.info->port.tty;
  307. int i, flg, status;
  308. status = UART_GET_LSR(uart);
  309. UART_CLEAR_LSR(uart);
  310. uart->port.icount.rx +=
  311. CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
  312. UART_XMIT_SIZE);
  313. if (status & BI) {
  314. uart->port.icount.brk++;
  315. if (uart_handle_break(&uart->port))
  316. goto dma_ignore_char;
  317. status &= ~(PE | FE);
  318. }
  319. if (status & PE)
  320. uart->port.icount.parity++;
  321. if (status & OE)
  322. uart->port.icount.overrun++;
  323. if (status & FE)
  324. uart->port.icount.frame++;
  325. status &= uart->port.read_status_mask;
  326. if (status & BI)
  327. flg = TTY_BREAK;
  328. else if (status & PE)
  329. flg = TTY_PARITY;
  330. else if (status & FE)
  331. flg = TTY_FRAME;
  332. else
  333. flg = TTY_NORMAL;
  334. for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
  335. if (i >= UART_XMIT_SIZE)
  336. i = 0;
  337. if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
  338. uart_insert_char(&uart->port, status, OE,
  339. uart->rx_dma_buf.buf[i], flg);
  340. }
  341. dma_ignore_char:
  342. tty_flip_buffer_push(tty);
  343. }
  344. void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
  345. {
  346. int x_pos, pos, flags;
  347. spin_lock_irqsave(&uart->port.lock, flags);
  348. uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
  349. x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
  350. uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
  351. if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
  352. uart->rx_dma_nrows = 0;
  353. x_pos = DMA_RX_XCOUNT - x_pos;
  354. if (x_pos == DMA_RX_XCOUNT)
  355. x_pos = 0;
  356. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  357. if (pos != uart->rx_dma_buf.tail) {
  358. uart->rx_dma_buf.head = pos;
  359. bfin_serial_dma_rx_chars(uart);
  360. uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
  361. }
  362. spin_unlock_irqrestore(&uart->port.lock, flags);
  363. mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
  364. }
  365. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  366. {
  367. struct bfin_serial_port *uart = dev_id;
  368. struct circ_buf *xmit = &uart->port.info->xmit;
  369. spin_lock(&uart->port.lock);
  370. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  371. disable_dma(uart->tx_dma_channel);
  372. clear_dma_irqstat(uart->tx_dma_channel);
  373. UART_CLEAR_IER(uart, ETBEI);
  374. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  375. uart->port.icount.tx += uart->tx_count;
  376. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  377. uart_write_wakeup(&uart->port);
  378. bfin_serial_dma_tx_chars(uart);
  379. }
  380. spin_unlock(&uart->port.lock);
  381. return IRQ_HANDLED;
  382. }
  383. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  384. {
  385. struct bfin_serial_port *uart = dev_id;
  386. unsigned short irqstat;
  387. spin_lock(&uart->port.lock);
  388. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  389. clear_dma_irqstat(uart->rx_dma_channel);
  390. bfin_serial_dma_rx_chars(uart);
  391. spin_unlock(&uart->port.lock);
  392. return IRQ_HANDLED;
  393. }
  394. #endif
  395. /*
  396. * Return TIOCSER_TEMT when transmitter is not busy.
  397. */
  398. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  399. {
  400. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  401. unsigned short lsr;
  402. lsr = UART_GET_LSR(uart);
  403. if (lsr & TEMT)
  404. return TIOCSER_TEMT;
  405. else
  406. return 0;
  407. }
  408. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  409. {
  410. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  411. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  412. if (uart->cts_pin < 0)
  413. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  414. if (UART_GET_CTS(uart))
  415. return TIOCM_DSR | TIOCM_CAR;
  416. else
  417. #endif
  418. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  419. }
  420. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  421. {
  422. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  423. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  424. if (uart->rts_pin < 0)
  425. return;
  426. if (mctrl & TIOCM_RTS)
  427. UART_CLEAR_RTS(uart);
  428. else
  429. UART_SET_RTS(uart);
  430. #endif
  431. }
  432. /*
  433. * Handle any change of modem status signal since we were last called.
  434. */
  435. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  436. {
  437. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  438. unsigned int status;
  439. struct uart_info *info = uart->port.info;
  440. struct tty_struct *tty = info->port.tty;
  441. status = bfin_serial_get_mctrl(&uart->port);
  442. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  443. if (!(status & TIOCM_CTS)) {
  444. tty->hw_stopped = 1;
  445. uart->cts_timer.data = (unsigned long)(uart);
  446. uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
  447. uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
  448. add_timer(&(uart->cts_timer));
  449. } else {
  450. tty->hw_stopped = 0;
  451. }
  452. #endif
  453. }
  454. /*
  455. * Interrupts are always disabled.
  456. */
  457. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  458. {
  459. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  460. u16 lcr = UART_GET_LCR(uart);
  461. if (break_state)
  462. lcr |= SB;
  463. else
  464. lcr &= ~SB;
  465. UART_PUT_LCR(uart, lcr);
  466. SSYNC();
  467. }
  468. static int bfin_serial_startup(struct uart_port *port)
  469. {
  470. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  471. #ifdef CONFIG_SERIAL_BFIN_DMA
  472. dma_addr_t dma_handle;
  473. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  474. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  475. return -EBUSY;
  476. }
  477. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  478. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  479. free_dma(uart->rx_dma_channel);
  480. return -EBUSY;
  481. }
  482. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  483. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  484. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  485. uart->rx_dma_buf.head = 0;
  486. uart->rx_dma_buf.tail = 0;
  487. uart->rx_dma_nrows = 0;
  488. set_dma_config(uart->rx_dma_channel,
  489. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  490. INTR_ON_ROW, DIMENSION_2D,
  491. DATA_SIZE_8,
  492. DMA_SYNC_RESTART));
  493. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  494. set_dma_x_modify(uart->rx_dma_channel, 1);
  495. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  496. set_dma_y_modify(uart->rx_dma_channel, 1);
  497. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  498. enable_dma(uart->rx_dma_channel);
  499. uart->rx_dma_timer.data = (unsigned long)(uart);
  500. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  501. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  502. add_timer(&(uart->rx_dma_timer));
  503. #else
  504. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  505. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  506. if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
  507. kgdboc_break_enabled = 0;
  508. else {
  509. # endif
  510. if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  511. "BFIN_UART_RX", uart)) {
  512. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  513. return -EBUSY;
  514. }
  515. if (request_irq
  516. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  517. "BFIN_UART_TX", uart)) {
  518. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  519. free_irq(uart->port.irq, uart);
  520. return -EBUSY;
  521. }
  522. # ifdef CONFIG_BF54x
  523. {
  524. unsigned uart_dma_ch_rx, uart_dma_ch_tx;
  525. switch (uart->port.irq) {
  526. case IRQ_UART3_RX:
  527. uart_dma_ch_rx = CH_UART3_RX;
  528. uart_dma_ch_tx = CH_UART3_TX;
  529. break;
  530. case IRQ_UART2_RX:
  531. uart_dma_ch_rx = CH_UART2_RX;
  532. uart_dma_ch_tx = CH_UART2_TX;
  533. break;
  534. default:
  535. uart_dma_ch_rx = uart_dma_ch_tx = 0;
  536. break;
  537. };
  538. if (uart_dma_ch_rx &&
  539. request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
  540. printk(KERN_NOTICE"Fail to attach UART interrupt\n");
  541. free_irq(uart->port.irq, uart);
  542. free_irq(uart->port.irq + 1, uart);
  543. return -EBUSY;
  544. }
  545. if (uart_dma_ch_tx &&
  546. request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
  547. printk(KERN_NOTICE "Fail to attach UART interrupt\n");
  548. free_dma(uart_dma_ch_rx);
  549. free_irq(uart->port.irq, uart);
  550. free_irq(uart->port.irq + 1, uart);
  551. return -EBUSY;
  552. }
  553. }
  554. # endif
  555. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  556. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  557. }
  558. # endif
  559. #endif
  560. UART_SET_IER(uart, ERBFI);
  561. return 0;
  562. }
  563. static void bfin_serial_shutdown(struct uart_port *port)
  564. {
  565. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  566. #ifdef CONFIG_SERIAL_BFIN_DMA
  567. disable_dma(uart->tx_dma_channel);
  568. free_dma(uart->tx_dma_channel);
  569. disable_dma(uart->rx_dma_channel);
  570. free_dma(uart->rx_dma_channel);
  571. del_timer(&(uart->rx_dma_timer));
  572. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  573. #else
  574. #ifdef CONFIG_BF54x
  575. switch (uart->port.irq) {
  576. case IRQ_UART3_RX:
  577. free_dma(CH_UART3_RX);
  578. free_dma(CH_UART3_TX);
  579. break;
  580. case IRQ_UART2_RX:
  581. free_dma(CH_UART2_RX);
  582. free_dma(CH_UART2_TX);
  583. break;
  584. default:
  585. break;
  586. };
  587. #endif
  588. free_irq(uart->port.irq, uart);
  589. free_irq(uart->port.irq+1, uart);
  590. #endif
  591. }
  592. static void
  593. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  594. struct ktermios *old)
  595. {
  596. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  597. unsigned long flags;
  598. unsigned int baud, quot;
  599. unsigned short val, ier, lcr = 0;
  600. switch (termios->c_cflag & CSIZE) {
  601. case CS8:
  602. lcr = WLS(8);
  603. break;
  604. case CS7:
  605. lcr = WLS(7);
  606. break;
  607. case CS6:
  608. lcr = WLS(6);
  609. break;
  610. case CS5:
  611. lcr = WLS(5);
  612. break;
  613. default:
  614. printk(KERN_ERR "%s: word lengh not supported\n",
  615. __func__);
  616. }
  617. if (termios->c_cflag & CSTOPB)
  618. lcr |= STB;
  619. if (termios->c_cflag & PARENB)
  620. lcr |= PEN;
  621. if (!(termios->c_cflag & PARODD))
  622. lcr |= EPS;
  623. if (termios->c_cflag & CMSPAR)
  624. lcr |= STP;
  625. port->read_status_mask = OE;
  626. if (termios->c_iflag & INPCK)
  627. port->read_status_mask |= (FE | PE);
  628. if (termios->c_iflag & (BRKINT | PARMRK))
  629. port->read_status_mask |= BI;
  630. /*
  631. * Characters to ignore
  632. */
  633. port->ignore_status_mask = 0;
  634. if (termios->c_iflag & IGNPAR)
  635. port->ignore_status_mask |= FE | PE;
  636. if (termios->c_iflag & IGNBRK) {
  637. port->ignore_status_mask |= BI;
  638. /*
  639. * If we're ignoring parity and break indicators,
  640. * ignore overruns too (for real raw support).
  641. */
  642. if (termios->c_iflag & IGNPAR)
  643. port->ignore_status_mask |= OE;
  644. }
  645. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  646. quot = uart_get_divisor(port, baud);
  647. spin_lock_irqsave(&uart->port.lock, flags);
  648. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  649. /* Disable UART */
  650. ier = UART_GET_IER(uart);
  651. UART_DISABLE_INTS(uart);
  652. /* Set DLAB in LCR to Access DLL and DLH */
  653. UART_SET_DLAB(uart);
  654. UART_PUT_DLL(uart, quot & 0xFF);
  655. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  656. SSYNC();
  657. /* Clear DLAB in LCR to Access THR RBR IER */
  658. UART_CLEAR_DLAB(uart);
  659. UART_PUT_LCR(uart, lcr);
  660. /* Enable UART */
  661. UART_ENABLE_INTS(uart, ier);
  662. val = UART_GET_GCTL(uart);
  663. val |= UCEN;
  664. UART_PUT_GCTL(uart, val);
  665. /* Port speed changed, update the per-port timeout. */
  666. uart_update_timeout(port, termios->c_cflag, baud);
  667. spin_unlock_irqrestore(&uart->port.lock, flags);
  668. }
  669. static const char *bfin_serial_type(struct uart_port *port)
  670. {
  671. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  672. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  673. }
  674. /*
  675. * Release the memory region(s) being used by 'port'.
  676. */
  677. static void bfin_serial_release_port(struct uart_port *port)
  678. {
  679. }
  680. /*
  681. * Request the memory region(s) being used by 'port'.
  682. */
  683. static int bfin_serial_request_port(struct uart_port *port)
  684. {
  685. return 0;
  686. }
  687. /*
  688. * Configure/autoconfigure the port.
  689. */
  690. static void bfin_serial_config_port(struct uart_port *port, int flags)
  691. {
  692. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  693. if (flags & UART_CONFIG_TYPE &&
  694. bfin_serial_request_port(&uart->port) == 0)
  695. uart->port.type = PORT_BFIN;
  696. }
  697. /*
  698. * Verify the new serial_struct (for TIOCSSERIAL).
  699. * The only change we allow are to the flags and type, and
  700. * even then only between PORT_BFIN and PORT_UNKNOWN
  701. */
  702. static int
  703. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  704. {
  705. return 0;
  706. }
  707. /*
  708. * Enable the IrDA function if tty->ldisc.num is N_IRDA.
  709. * In other cases, disable IrDA function.
  710. */
  711. static void bfin_serial_set_ldisc(struct uart_port *port)
  712. {
  713. int line = port->line;
  714. unsigned short val;
  715. if (line >= port->info->port.tty->driver->num)
  716. return;
  717. switch (port->info->port.tty->termios->c_line) {
  718. case N_IRDA:
  719. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  720. val |= (IREN | RPOLC);
  721. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  722. break;
  723. default:
  724. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  725. val &= ~(IREN | RPOLC);
  726. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  727. }
  728. }
  729. #ifdef CONFIG_CONSOLE_POLL
  730. static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
  731. {
  732. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  733. while (!(UART_GET_LSR(uart) & THRE))
  734. cpu_relax();
  735. UART_CLEAR_DLAB(uart);
  736. UART_PUT_CHAR(uart, (unsigned char)chr);
  737. }
  738. static int bfin_serial_poll_get_char(struct uart_port *port)
  739. {
  740. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  741. unsigned char chr;
  742. while (!(UART_GET_LSR(uart) & DR))
  743. cpu_relax();
  744. UART_CLEAR_DLAB(uart);
  745. chr = UART_GET_CHAR(uart);
  746. return chr;
  747. }
  748. #endif
  749. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  750. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  751. static void bfin_kgdboc_port_shutdown(struct uart_port *port)
  752. {
  753. if (kgdboc_break_enabled) {
  754. kgdboc_break_enabled = 0;
  755. bfin_serial_shutdown(port);
  756. }
  757. }
  758. static int bfin_kgdboc_port_startup(struct uart_port *port)
  759. {
  760. kgdboc_port_line = port->line;
  761. kgdboc_break_enabled = !bfin_serial_startup(port);
  762. return 0;
  763. }
  764. #endif
  765. static void bfin_serial_reset_irda(struct uart_port *port)
  766. {
  767. int line = port->line;
  768. unsigned short val;
  769. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  770. val &= ~(IREN | RPOLC);
  771. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  772. SSYNC();
  773. val |= (IREN | RPOLC);
  774. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  775. SSYNC();
  776. }
  777. static struct uart_ops bfin_serial_pops = {
  778. .tx_empty = bfin_serial_tx_empty,
  779. .set_mctrl = bfin_serial_set_mctrl,
  780. .get_mctrl = bfin_serial_get_mctrl,
  781. .stop_tx = bfin_serial_stop_tx,
  782. .start_tx = bfin_serial_start_tx,
  783. .stop_rx = bfin_serial_stop_rx,
  784. .enable_ms = bfin_serial_enable_ms,
  785. .break_ctl = bfin_serial_break_ctl,
  786. .startup = bfin_serial_startup,
  787. .shutdown = bfin_serial_shutdown,
  788. .set_termios = bfin_serial_set_termios,
  789. .set_ldisc = bfin_serial_set_ldisc,
  790. .type = bfin_serial_type,
  791. .release_port = bfin_serial_release_port,
  792. .request_port = bfin_serial_request_port,
  793. .config_port = bfin_serial_config_port,
  794. .verify_port = bfin_serial_verify_port,
  795. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  796. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  797. .kgdboc_port_startup = bfin_kgdboc_port_startup,
  798. .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
  799. #endif
  800. #ifdef CONFIG_CONSOLE_POLL
  801. .poll_put_char = bfin_serial_poll_put_char,
  802. .poll_get_char = bfin_serial_poll_get_char,
  803. #endif
  804. };
  805. static void __init bfin_serial_init_ports(void)
  806. {
  807. static int first = 1;
  808. int i;
  809. if (!first)
  810. return;
  811. first = 0;
  812. for (i = 0; i < nr_active_ports; i++) {
  813. bfin_serial_ports[i].port.uartclk = get_sclk();
  814. bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
  815. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  816. bfin_serial_ports[i].port.line = i;
  817. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  818. bfin_serial_ports[i].port.membase =
  819. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  820. bfin_serial_ports[i].port.mapbase =
  821. bfin_serial_resource[i].uart_base_addr;
  822. bfin_serial_ports[i].port.irq =
  823. bfin_serial_resource[i].uart_irq;
  824. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  825. #ifdef CONFIG_SERIAL_BFIN_DMA
  826. bfin_serial_ports[i].tx_done = 1;
  827. bfin_serial_ports[i].tx_count = 0;
  828. bfin_serial_ports[i].tx_dma_channel =
  829. bfin_serial_resource[i].uart_tx_dma_channel;
  830. bfin_serial_ports[i].rx_dma_channel =
  831. bfin_serial_resource[i].uart_rx_dma_channel;
  832. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  833. #endif
  834. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  835. init_timer(&(bfin_serial_ports[i].cts_timer));
  836. bfin_serial_ports[i].cts_pin =
  837. bfin_serial_resource[i].uart_cts_pin;
  838. bfin_serial_ports[i].rts_pin =
  839. bfin_serial_resource[i].uart_rts_pin;
  840. #endif
  841. bfin_serial_hw_init(&bfin_serial_ports[i]);
  842. }
  843. }
  844. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  845. /*
  846. * If the port was already initialised (eg, by a boot loader),
  847. * try to determine the current setup.
  848. */
  849. static void __init
  850. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  851. int *parity, int *bits)
  852. {
  853. unsigned short status;
  854. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  855. if (status == (ERBFI | ETBEI)) {
  856. /* ok, the port was enabled */
  857. u16 lcr, dlh, dll;
  858. lcr = UART_GET_LCR(uart);
  859. *parity = 'n';
  860. if (lcr & PEN) {
  861. if (lcr & EPS)
  862. *parity = 'e';
  863. else
  864. *parity = 'o';
  865. }
  866. switch (lcr & 0x03) {
  867. case 0: *bits = 5; break;
  868. case 1: *bits = 6; break;
  869. case 2: *bits = 7; break;
  870. case 3: *bits = 8; break;
  871. }
  872. /* Set DLAB in LCR to Access DLL and DLH */
  873. UART_SET_DLAB(uart);
  874. dll = UART_GET_DLL(uart);
  875. dlh = UART_GET_DLH(uart);
  876. /* Clear DLAB in LCR to Access THR RBR IER */
  877. UART_CLEAR_DLAB(uart);
  878. *baud = get_sclk() / (16*(dll | dlh << 8));
  879. }
  880. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
  881. }
  882. static struct uart_driver bfin_serial_reg;
  883. static int __init
  884. bfin_serial_console_setup(struct console *co, char *options)
  885. {
  886. struct bfin_serial_port *uart;
  887. int baud = 57600;
  888. int bits = 8;
  889. int parity = 'n';
  890. # ifdef CONFIG_SERIAL_BFIN_CTSRTS
  891. int flow = 'r';
  892. # else
  893. int flow = 'n';
  894. # endif
  895. /*
  896. * Check whether an invalid uart number has been specified, and
  897. * if so, search for the first available port that does have
  898. * console support.
  899. */
  900. if (co->index == -1 || co->index >= nr_active_ports)
  901. co->index = 0;
  902. uart = &bfin_serial_ports[co->index];
  903. if (options)
  904. uart_parse_options(options, &baud, &parity, &bits, &flow);
  905. else
  906. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  907. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  908. }
  909. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  910. defined (CONFIG_EARLY_PRINTK) */
  911. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  912. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  913. {
  914. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  915. while (!(UART_GET_LSR(uart) & THRE))
  916. barrier();
  917. UART_PUT_CHAR(uart, ch);
  918. SSYNC();
  919. }
  920. /*
  921. * Interrupts are disabled on entering
  922. */
  923. static void
  924. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  925. {
  926. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  927. int flags = 0;
  928. spin_lock_irqsave(&uart->port.lock, flags);
  929. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  930. spin_unlock_irqrestore(&uart->port.lock, flags);
  931. }
  932. static struct console bfin_serial_console = {
  933. .name = BFIN_SERIAL_NAME,
  934. .write = bfin_serial_console_write,
  935. .device = uart_console_device,
  936. .setup = bfin_serial_console_setup,
  937. .flags = CON_PRINTBUFFER,
  938. .index = -1,
  939. .data = &bfin_serial_reg,
  940. };
  941. static int __init bfin_serial_rs_console_init(void)
  942. {
  943. bfin_serial_init_ports();
  944. register_console(&bfin_serial_console);
  945. return 0;
  946. }
  947. console_initcall(bfin_serial_rs_console_init);
  948. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  949. #else
  950. #define BFIN_SERIAL_CONSOLE NULL
  951. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  952. #ifdef CONFIG_EARLY_PRINTK
  953. static __init void early_serial_putc(struct uart_port *port, int ch)
  954. {
  955. unsigned timeout = 0xffff;
  956. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  957. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  958. cpu_relax();
  959. UART_PUT_CHAR(uart, ch);
  960. }
  961. static __init void early_serial_write(struct console *con, const char *s,
  962. unsigned int n)
  963. {
  964. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  965. unsigned int i;
  966. for (i = 0; i < n; i++, s++) {
  967. if (*s == '\n')
  968. early_serial_putc(&uart->port, '\r');
  969. early_serial_putc(&uart->port, *s);
  970. }
  971. }
  972. static struct __initdata console bfin_early_serial_console = {
  973. .name = "early_BFuart",
  974. .write = early_serial_write,
  975. .device = uart_console_device,
  976. .flags = CON_PRINTBUFFER,
  977. .setup = bfin_serial_console_setup,
  978. .index = -1,
  979. .data = &bfin_serial_reg,
  980. };
  981. struct console __init *bfin_earlyserial_init(unsigned int port,
  982. unsigned int cflag)
  983. {
  984. struct bfin_serial_port *uart;
  985. struct ktermios t;
  986. if (port == -1 || port >= nr_active_ports)
  987. port = 0;
  988. bfin_serial_init_ports();
  989. bfin_early_serial_console.index = port;
  990. uart = &bfin_serial_ports[port];
  991. t.c_cflag = cflag;
  992. t.c_iflag = 0;
  993. t.c_oflag = 0;
  994. t.c_lflag = ICANON;
  995. t.c_line = port;
  996. bfin_serial_set_termios(&uart->port, &t, &t);
  997. return &bfin_early_serial_console;
  998. }
  999. #endif /* CONFIG_EARLY_PRINTK */
  1000. static struct uart_driver bfin_serial_reg = {
  1001. .owner = THIS_MODULE,
  1002. .driver_name = "bfin-uart",
  1003. .dev_name = BFIN_SERIAL_NAME,
  1004. .major = BFIN_SERIAL_MAJOR,
  1005. .minor = BFIN_SERIAL_MINOR,
  1006. .nr = BFIN_UART_NR_PORTS,
  1007. .cons = BFIN_SERIAL_CONSOLE,
  1008. };
  1009. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1010. {
  1011. int i;
  1012. for (i = 0; i < nr_active_ports; i++) {
  1013. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1014. continue;
  1015. uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1016. }
  1017. return 0;
  1018. }
  1019. static int bfin_serial_resume(struct platform_device *dev)
  1020. {
  1021. int i;
  1022. for (i = 0; i < nr_active_ports; i++) {
  1023. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1024. continue;
  1025. uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1026. }
  1027. return 0;
  1028. }
  1029. static int bfin_serial_probe(struct platform_device *dev)
  1030. {
  1031. struct resource *res = dev->resource;
  1032. int i;
  1033. for (i = 0; i < dev->num_resources; i++, res++)
  1034. if (res->flags & IORESOURCE_MEM)
  1035. break;
  1036. if (i < dev->num_resources) {
  1037. for (i = 0; i < nr_active_ports; i++, res++) {
  1038. if (bfin_serial_ports[i].port.mapbase != res->start)
  1039. continue;
  1040. bfin_serial_ports[i].port.dev = &dev->dev;
  1041. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1042. }
  1043. }
  1044. return 0;
  1045. }
  1046. static int bfin_serial_remove(struct platform_device *dev)
  1047. {
  1048. int i;
  1049. for (i = 0; i < nr_active_ports; i++) {
  1050. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1051. continue;
  1052. uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1053. bfin_serial_ports[i].port.dev = NULL;
  1054. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  1055. gpio_free(bfin_serial_ports[i].cts_pin);
  1056. gpio_free(bfin_serial_ports[i].rts_pin);
  1057. #endif
  1058. }
  1059. return 0;
  1060. }
  1061. static struct platform_driver bfin_serial_driver = {
  1062. .probe = bfin_serial_probe,
  1063. .remove = bfin_serial_remove,
  1064. .suspend = bfin_serial_suspend,
  1065. .resume = bfin_serial_resume,
  1066. .driver = {
  1067. .name = "bfin-uart",
  1068. .owner = THIS_MODULE,
  1069. },
  1070. };
  1071. static int __init bfin_serial_init(void)
  1072. {
  1073. int ret;
  1074. pr_info("Serial: Blackfin serial driver\n");
  1075. bfin_serial_init_ports();
  1076. ret = uart_register_driver(&bfin_serial_reg);
  1077. if (ret == 0) {
  1078. ret = platform_driver_register(&bfin_serial_driver);
  1079. if (ret) {
  1080. pr_debug("uart register failed\n");
  1081. uart_unregister_driver(&bfin_serial_reg);
  1082. }
  1083. }
  1084. return ret;
  1085. }
  1086. static void __exit bfin_serial_exit(void)
  1087. {
  1088. platform_driver_unregister(&bfin_serial_driver);
  1089. uart_unregister_driver(&bfin_serial_reg);
  1090. }
  1091. module_init(bfin_serial_init);
  1092. module_exit(bfin_serial_exit);
  1093. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1094. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1095. MODULE_LICENSE("GPL");
  1096. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1097. MODULE_ALIAS("platform:bfin-uart");