bfin_5xx.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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;
  347. unsigned long flags;
  348. spin_lock_irqsave(&uart->port.lock, flags);
  349. uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
  350. x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
  351. uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
  352. if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
  353. uart->rx_dma_nrows = 0;
  354. x_pos = DMA_RX_XCOUNT - x_pos;
  355. if (x_pos == DMA_RX_XCOUNT)
  356. x_pos = 0;
  357. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  358. if (pos != uart->rx_dma_buf.tail) {
  359. uart->rx_dma_buf.head = pos;
  360. bfin_serial_dma_rx_chars(uart);
  361. uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
  362. }
  363. spin_unlock_irqrestore(&uart->port.lock, flags);
  364. mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
  365. }
  366. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  367. {
  368. struct bfin_serial_port *uart = dev_id;
  369. struct circ_buf *xmit = &uart->port.info->xmit;
  370. spin_lock(&uart->port.lock);
  371. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  372. disable_dma(uart->tx_dma_channel);
  373. clear_dma_irqstat(uart->tx_dma_channel);
  374. UART_CLEAR_IER(uart, ETBEI);
  375. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  376. uart->port.icount.tx += uart->tx_count;
  377. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  378. uart_write_wakeup(&uart->port);
  379. bfin_serial_dma_tx_chars(uart);
  380. }
  381. spin_unlock(&uart->port.lock);
  382. return IRQ_HANDLED;
  383. }
  384. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  385. {
  386. struct bfin_serial_port *uart = dev_id;
  387. unsigned short irqstat;
  388. spin_lock(&uart->port.lock);
  389. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  390. clear_dma_irqstat(uart->rx_dma_channel);
  391. bfin_serial_dma_rx_chars(uart);
  392. spin_unlock(&uart->port.lock);
  393. return IRQ_HANDLED;
  394. }
  395. #endif
  396. /*
  397. * Return TIOCSER_TEMT when transmitter is not busy.
  398. */
  399. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  400. {
  401. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  402. unsigned short lsr;
  403. lsr = UART_GET_LSR(uart);
  404. if (lsr & TEMT)
  405. return TIOCSER_TEMT;
  406. else
  407. return 0;
  408. }
  409. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  410. {
  411. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  412. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  413. if (uart->cts_pin < 0)
  414. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  415. if (UART_GET_CTS(uart))
  416. return TIOCM_DSR | TIOCM_CAR;
  417. else
  418. #endif
  419. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  420. }
  421. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  422. {
  423. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  424. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  425. if (uart->rts_pin < 0)
  426. return;
  427. if (mctrl & TIOCM_RTS)
  428. UART_CLEAR_RTS(uart);
  429. else
  430. UART_SET_RTS(uart);
  431. #endif
  432. }
  433. /*
  434. * Handle any change of modem status signal since we were last called.
  435. */
  436. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  437. {
  438. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  439. unsigned int status;
  440. struct uart_info *info = uart->port.info;
  441. struct tty_struct *tty = info->port.tty;
  442. status = bfin_serial_get_mctrl(&uart->port);
  443. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  444. if (!(status & TIOCM_CTS)) {
  445. tty->hw_stopped = 1;
  446. uart->cts_timer.data = (unsigned long)(uart);
  447. uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
  448. uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
  449. add_timer(&(uart->cts_timer));
  450. } else {
  451. tty->hw_stopped = 0;
  452. }
  453. #endif
  454. }
  455. /*
  456. * Interrupts are always disabled.
  457. */
  458. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  459. {
  460. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  461. u16 lcr = UART_GET_LCR(uart);
  462. if (break_state)
  463. lcr |= SB;
  464. else
  465. lcr &= ~SB;
  466. UART_PUT_LCR(uart, lcr);
  467. SSYNC();
  468. }
  469. static int bfin_serial_startup(struct uart_port *port)
  470. {
  471. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  472. #ifdef CONFIG_SERIAL_BFIN_DMA
  473. dma_addr_t dma_handle;
  474. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  475. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  476. return -EBUSY;
  477. }
  478. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  479. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  480. free_dma(uart->rx_dma_channel);
  481. return -EBUSY;
  482. }
  483. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  484. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  485. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  486. uart->rx_dma_buf.head = 0;
  487. uart->rx_dma_buf.tail = 0;
  488. uart->rx_dma_nrows = 0;
  489. set_dma_config(uart->rx_dma_channel,
  490. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  491. INTR_ON_ROW, DIMENSION_2D,
  492. DATA_SIZE_8,
  493. DMA_SYNC_RESTART));
  494. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  495. set_dma_x_modify(uart->rx_dma_channel, 1);
  496. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  497. set_dma_y_modify(uart->rx_dma_channel, 1);
  498. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  499. enable_dma(uart->rx_dma_channel);
  500. uart->rx_dma_timer.data = (unsigned long)(uart);
  501. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  502. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  503. add_timer(&(uart->rx_dma_timer));
  504. #else
  505. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  506. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  507. if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
  508. kgdboc_break_enabled = 0;
  509. else {
  510. # endif
  511. if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  512. "BFIN_UART_RX", uart)) {
  513. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  514. return -EBUSY;
  515. }
  516. if (request_irq
  517. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  518. "BFIN_UART_TX", uart)) {
  519. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  520. free_irq(uart->port.irq, uart);
  521. return -EBUSY;
  522. }
  523. # ifdef CONFIG_BF54x
  524. {
  525. unsigned uart_dma_ch_rx, uart_dma_ch_tx;
  526. switch (uart->port.irq) {
  527. case IRQ_UART3_RX:
  528. uart_dma_ch_rx = CH_UART3_RX;
  529. uart_dma_ch_tx = CH_UART3_TX;
  530. break;
  531. case IRQ_UART2_RX:
  532. uart_dma_ch_rx = CH_UART2_RX;
  533. uart_dma_ch_tx = CH_UART2_TX;
  534. break;
  535. default:
  536. uart_dma_ch_rx = uart_dma_ch_tx = 0;
  537. break;
  538. };
  539. if (uart_dma_ch_rx &&
  540. request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
  541. printk(KERN_NOTICE"Fail to attach UART interrupt\n");
  542. free_irq(uart->port.irq, uart);
  543. free_irq(uart->port.irq + 1, uart);
  544. return -EBUSY;
  545. }
  546. if (uart_dma_ch_tx &&
  547. request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
  548. printk(KERN_NOTICE "Fail to attach UART interrupt\n");
  549. free_dma(uart_dma_ch_rx);
  550. free_irq(uart->port.irq, uart);
  551. free_irq(uart->port.irq + 1, uart);
  552. return -EBUSY;
  553. }
  554. }
  555. # endif
  556. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  557. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  558. }
  559. # endif
  560. #endif
  561. UART_SET_IER(uart, ERBFI);
  562. return 0;
  563. }
  564. static void bfin_serial_shutdown(struct uart_port *port)
  565. {
  566. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  567. #ifdef CONFIG_SERIAL_BFIN_DMA
  568. disable_dma(uart->tx_dma_channel);
  569. free_dma(uart->tx_dma_channel);
  570. disable_dma(uart->rx_dma_channel);
  571. free_dma(uart->rx_dma_channel);
  572. del_timer(&(uart->rx_dma_timer));
  573. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  574. #else
  575. #ifdef CONFIG_BF54x
  576. switch (uart->port.irq) {
  577. case IRQ_UART3_RX:
  578. free_dma(CH_UART3_RX);
  579. free_dma(CH_UART3_TX);
  580. break;
  581. case IRQ_UART2_RX:
  582. free_dma(CH_UART2_RX);
  583. free_dma(CH_UART2_TX);
  584. break;
  585. default:
  586. break;
  587. };
  588. #endif
  589. free_irq(uart->port.irq, uart);
  590. free_irq(uart->port.irq+1, uart);
  591. #endif
  592. }
  593. static void
  594. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  595. struct ktermios *old)
  596. {
  597. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  598. unsigned long flags;
  599. unsigned int baud, quot;
  600. unsigned short val, ier, lcr = 0;
  601. switch (termios->c_cflag & CSIZE) {
  602. case CS8:
  603. lcr = WLS(8);
  604. break;
  605. case CS7:
  606. lcr = WLS(7);
  607. break;
  608. case CS6:
  609. lcr = WLS(6);
  610. break;
  611. case CS5:
  612. lcr = WLS(5);
  613. break;
  614. default:
  615. printk(KERN_ERR "%s: word lengh not supported\n",
  616. __func__);
  617. }
  618. if (termios->c_cflag & CSTOPB)
  619. lcr |= STB;
  620. if (termios->c_cflag & PARENB)
  621. lcr |= PEN;
  622. if (!(termios->c_cflag & PARODD))
  623. lcr |= EPS;
  624. if (termios->c_cflag & CMSPAR)
  625. lcr |= STP;
  626. port->read_status_mask = OE;
  627. if (termios->c_iflag & INPCK)
  628. port->read_status_mask |= (FE | PE);
  629. if (termios->c_iflag & (BRKINT | PARMRK))
  630. port->read_status_mask |= BI;
  631. /*
  632. * Characters to ignore
  633. */
  634. port->ignore_status_mask = 0;
  635. if (termios->c_iflag & IGNPAR)
  636. port->ignore_status_mask |= FE | PE;
  637. if (termios->c_iflag & IGNBRK) {
  638. port->ignore_status_mask |= BI;
  639. /*
  640. * If we're ignoring parity and break indicators,
  641. * ignore overruns too (for real raw support).
  642. */
  643. if (termios->c_iflag & IGNPAR)
  644. port->ignore_status_mask |= OE;
  645. }
  646. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  647. quot = uart_get_divisor(port, baud);
  648. spin_lock_irqsave(&uart->port.lock, flags);
  649. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  650. /* Disable UART */
  651. ier = UART_GET_IER(uart);
  652. UART_DISABLE_INTS(uart);
  653. /* Set DLAB in LCR to Access DLL and DLH */
  654. UART_SET_DLAB(uart);
  655. UART_PUT_DLL(uart, quot & 0xFF);
  656. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  657. SSYNC();
  658. /* Clear DLAB in LCR to Access THR RBR IER */
  659. UART_CLEAR_DLAB(uart);
  660. UART_PUT_LCR(uart, lcr);
  661. /* Enable UART */
  662. UART_ENABLE_INTS(uart, ier);
  663. val = UART_GET_GCTL(uart);
  664. val |= UCEN;
  665. UART_PUT_GCTL(uart, val);
  666. /* Port speed changed, update the per-port timeout. */
  667. uart_update_timeout(port, termios->c_cflag, baud);
  668. spin_unlock_irqrestore(&uart->port.lock, flags);
  669. }
  670. static const char *bfin_serial_type(struct uart_port *port)
  671. {
  672. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  673. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  674. }
  675. /*
  676. * Release the memory region(s) being used by 'port'.
  677. */
  678. static void bfin_serial_release_port(struct uart_port *port)
  679. {
  680. }
  681. /*
  682. * Request the memory region(s) being used by 'port'.
  683. */
  684. static int bfin_serial_request_port(struct uart_port *port)
  685. {
  686. return 0;
  687. }
  688. /*
  689. * Configure/autoconfigure the port.
  690. */
  691. static void bfin_serial_config_port(struct uart_port *port, int flags)
  692. {
  693. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  694. if (flags & UART_CONFIG_TYPE &&
  695. bfin_serial_request_port(&uart->port) == 0)
  696. uart->port.type = PORT_BFIN;
  697. }
  698. /*
  699. * Verify the new serial_struct (for TIOCSSERIAL).
  700. * The only change we allow are to the flags and type, and
  701. * even then only between PORT_BFIN and PORT_UNKNOWN
  702. */
  703. static int
  704. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  705. {
  706. return 0;
  707. }
  708. /*
  709. * Enable the IrDA function if tty->ldisc.num is N_IRDA.
  710. * In other cases, disable IrDA function.
  711. */
  712. static void bfin_serial_set_ldisc(struct uart_port *port)
  713. {
  714. int line = port->line;
  715. unsigned short val;
  716. if (line >= port->info->port.tty->driver->num)
  717. return;
  718. switch (port->info->port.tty->termios->c_line) {
  719. case N_IRDA:
  720. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  721. val |= (IREN | RPOLC);
  722. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  723. break;
  724. default:
  725. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  726. val &= ~(IREN | RPOLC);
  727. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  728. }
  729. }
  730. #ifdef CONFIG_CONSOLE_POLL
  731. static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
  732. {
  733. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  734. while (!(UART_GET_LSR(uart) & THRE))
  735. cpu_relax();
  736. UART_CLEAR_DLAB(uart);
  737. UART_PUT_CHAR(uart, (unsigned char)chr);
  738. }
  739. static int bfin_serial_poll_get_char(struct uart_port *port)
  740. {
  741. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  742. unsigned char chr;
  743. while (!(UART_GET_LSR(uart) & DR))
  744. cpu_relax();
  745. UART_CLEAR_DLAB(uart);
  746. chr = UART_GET_CHAR(uart);
  747. return chr;
  748. }
  749. #endif
  750. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  751. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  752. static void bfin_kgdboc_port_shutdown(struct uart_port *port)
  753. {
  754. if (kgdboc_break_enabled) {
  755. kgdboc_break_enabled = 0;
  756. bfin_serial_shutdown(port);
  757. }
  758. }
  759. static int bfin_kgdboc_port_startup(struct uart_port *port)
  760. {
  761. kgdboc_port_line = port->line;
  762. kgdboc_break_enabled = !bfin_serial_startup(port);
  763. return 0;
  764. }
  765. #endif
  766. static void bfin_serial_reset_irda(struct uart_port *port)
  767. {
  768. int line = port->line;
  769. unsigned short val;
  770. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  771. val &= ~(IREN | RPOLC);
  772. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  773. SSYNC();
  774. val |= (IREN | RPOLC);
  775. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  776. SSYNC();
  777. }
  778. static struct uart_ops bfin_serial_pops = {
  779. .tx_empty = bfin_serial_tx_empty,
  780. .set_mctrl = bfin_serial_set_mctrl,
  781. .get_mctrl = bfin_serial_get_mctrl,
  782. .stop_tx = bfin_serial_stop_tx,
  783. .start_tx = bfin_serial_start_tx,
  784. .stop_rx = bfin_serial_stop_rx,
  785. .enable_ms = bfin_serial_enable_ms,
  786. .break_ctl = bfin_serial_break_ctl,
  787. .startup = bfin_serial_startup,
  788. .shutdown = bfin_serial_shutdown,
  789. .set_termios = bfin_serial_set_termios,
  790. .set_ldisc = bfin_serial_set_ldisc,
  791. .type = bfin_serial_type,
  792. .release_port = bfin_serial_release_port,
  793. .request_port = bfin_serial_request_port,
  794. .config_port = bfin_serial_config_port,
  795. .verify_port = bfin_serial_verify_port,
  796. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  797. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  798. .kgdboc_port_startup = bfin_kgdboc_port_startup,
  799. .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
  800. #endif
  801. #ifdef CONFIG_CONSOLE_POLL
  802. .poll_put_char = bfin_serial_poll_put_char,
  803. .poll_get_char = bfin_serial_poll_get_char,
  804. #endif
  805. };
  806. static void __init bfin_serial_init_ports(void)
  807. {
  808. static int first = 1;
  809. int i;
  810. if (!first)
  811. return;
  812. first = 0;
  813. for (i = 0; i < nr_active_ports; i++) {
  814. bfin_serial_ports[i].port.uartclk = get_sclk();
  815. bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
  816. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  817. bfin_serial_ports[i].port.line = i;
  818. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  819. bfin_serial_ports[i].port.membase =
  820. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  821. bfin_serial_ports[i].port.mapbase =
  822. bfin_serial_resource[i].uart_base_addr;
  823. bfin_serial_ports[i].port.irq =
  824. bfin_serial_resource[i].uart_irq;
  825. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  826. #ifdef CONFIG_SERIAL_BFIN_DMA
  827. bfin_serial_ports[i].tx_done = 1;
  828. bfin_serial_ports[i].tx_count = 0;
  829. bfin_serial_ports[i].tx_dma_channel =
  830. bfin_serial_resource[i].uart_tx_dma_channel;
  831. bfin_serial_ports[i].rx_dma_channel =
  832. bfin_serial_resource[i].uart_rx_dma_channel;
  833. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  834. #endif
  835. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  836. init_timer(&(bfin_serial_ports[i].cts_timer));
  837. bfin_serial_ports[i].cts_pin =
  838. bfin_serial_resource[i].uart_cts_pin;
  839. bfin_serial_ports[i].rts_pin =
  840. bfin_serial_resource[i].uart_rts_pin;
  841. #endif
  842. bfin_serial_hw_init(&bfin_serial_ports[i]);
  843. }
  844. }
  845. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  846. /*
  847. * If the port was already initialised (eg, by a boot loader),
  848. * try to determine the current setup.
  849. */
  850. static void __init
  851. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  852. int *parity, int *bits)
  853. {
  854. unsigned short status;
  855. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  856. if (status == (ERBFI | ETBEI)) {
  857. /* ok, the port was enabled */
  858. u16 lcr, dlh, dll;
  859. lcr = UART_GET_LCR(uart);
  860. *parity = 'n';
  861. if (lcr & PEN) {
  862. if (lcr & EPS)
  863. *parity = 'e';
  864. else
  865. *parity = 'o';
  866. }
  867. switch (lcr & 0x03) {
  868. case 0: *bits = 5; break;
  869. case 1: *bits = 6; break;
  870. case 2: *bits = 7; break;
  871. case 3: *bits = 8; break;
  872. }
  873. /* Set DLAB in LCR to Access DLL and DLH */
  874. UART_SET_DLAB(uart);
  875. dll = UART_GET_DLL(uart);
  876. dlh = UART_GET_DLH(uart);
  877. /* Clear DLAB in LCR to Access THR RBR IER */
  878. UART_CLEAR_DLAB(uart);
  879. *baud = get_sclk() / (16*(dll | dlh << 8));
  880. }
  881. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
  882. }
  883. static struct uart_driver bfin_serial_reg;
  884. static int __init
  885. bfin_serial_console_setup(struct console *co, char *options)
  886. {
  887. struct bfin_serial_port *uart;
  888. int baud = 57600;
  889. int bits = 8;
  890. int parity = 'n';
  891. # ifdef CONFIG_SERIAL_BFIN_CTSRTS
  892. int flow = 'r';
  893. # else
  894. int flow = 'n';
  895. # endif
  896. /*
  897. * Check whether an invalid uart number has been specified, and
  898. * if so, search for the first available port that does have
  899. * console support.
  900. */
  901. if (co->index == -1 || co->index >= nr_active_ports)
  902. co->index = 0;
  903. uart = &bfin_serial_ports[co->index];
  904. if (options)
  905. uart_parse_options(options, &baud, &parity, &bits, &flow);
  906. else
  907. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  908. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  909. }
  910. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  911. defined (CONFIG_EARLY_PRINTK) */
  912. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  913. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  914. {
  915. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  916. while (!(UART_GET_LSR(uart) & THRE))
  917. barrier();
  918. UART_PUT_CHAR(uart, ch);
  919. SSYNC();
  920. }
  921. /*
  922. * Interrupts are disabled on entering
  923. */
  924. static void
  925. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  926. {
  927. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  928. unsigned long flags;
  929. spin_lock_irqsave(&uart->port.lock, flags);
  930. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  931. spin_unlock_irqrestore(&uart->port.lock, flags);
  932. }
  933. static struct console bfin_serial_console = {
  934. .name = BFIN_SERIAL_NAME,
  935. .write = bfin_serial_console_write,
  936. .device = uart_console_device,
  937. .setup = bfin_serial_console_setup,
  938. .flags = CON_PRINTBUFFER,
  939. .index = -1,
  940. .data = &bfin_serial_reg,
  941. };
  942. static int __init bfin_serial_rs_console_init(void)
  943. {
  944. bfin_serial_init_ports();
  945. register_console(&bfin_serial_console);
  946. return 0;
  947. }
  948. console_initcall(bfin_serial_rs_console_init);
  949. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  950. #else
  951. #define BFIN_SERIAL_CONSOLE NULL
  952. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  953. #ifdef CONFIG_EARLY_PRINTK
  954. static __init void early_serial_putc(struct uart_port *port, int ch)
  955. {
  956. unsigned timeout = 0xffff;
  957. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  958. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  959. cpu_relax();
  960. UART_PUT_CHAR(uart, ch);
  961. }
  962. static __init void early_serial_write(struct console *con, const char *s,
  963. unsigned int n)
  964. {
  965. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  966. unsigned int i;
  967. for (i = 0; i < n; i++, s++) {
  968. if (*s == '\n')
  969. early_serial_putc(&uart->port, '\r');
  970. early_serial_putc(&uart->port, *s);
  971. }
  972. }
  973. static struct __initdata console bfin_early_serial_console = {
  974. .name = "early_BFuart",
  975. .write = early_serial_write,
  976. .device = uart_console_device,
  977. .flags = CON_PRINTBUFFER,
  978. .setup = bfin_serial_console_setup,
  979. .index = -1,
  980. .data = &bfin_serial_reg,
  981. };
  982. struct console __init *bfin_earlyserial_init(unsigned int port,
  983. unsigned int cflag)
  984. {
  985. struct bfin_serial_port *uart;
  986. struct ktermios t;
  987. if (port == -1 || port >= nr_active_ports)
  988. port = 0;
  989. bfin_serial_init_ports();
  990. bfin_early_serial_console.index = port;
  991. uart = &bfin_serial_ports[port];
  992. t.c_cflag = cflag;
  993. t.c_iflag = 0;
  994. t.c_oflag = 0;
  995. t.c_lflag = ICANON;
  996. t.c_line = port;
  997. bfin_serial_set_termios(&uart->port, &t, &t);
  998. return &bfin_early_serial_console;
  999. }
  1000. #endif /* CONFIG_EARLY_PRINTK */
  1001. static struct uart_driver bfin_serial_reg = {
  1002. .owner = THIS_MODULE,
  1003. .driver_name = "bfin-uart",
  1004. .dev_name = BFIN_SERIAL_NAME,
  1005. .major = BFIN_SERIAL_MAJOR,
  1006. .minor = BFIN_SERIAL_MINOR,
  1007. .nr = BFIN_UART_NR_PORTS,
  1008. .cons = BFIN_SERIAL_CONSOLE,
  1009. };
  1010. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1011. {
  1012. int i;
  1013. for (i = 0; i < nr_active_ports; i++) {
  1014. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1015. continue;
  1016. uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1017. }
  1018. return 0;
  1019. }
  1020. static int bfin_serial_resume(struct platform_device *dev)
  1021. {
  1022. int i;
  1023. for (i = 0; i < nr_active_ports; i++) {
  1024. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1025. continue;
  1026. uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1027. }
  1028. return 0;
  1029. }
  1030. static int bfin_serial_probe(struct platform_device *dev)
  1031. {
  1032. struct resource *res = dev->resource;
  1033. int i;
  1034. for (i = 0; i < dev->num_resources; i++, res++)
  1035. if (res->flags & IORESOURCE_MEM)
  1036. break;
  1037. if (i < dev->num_resources) {
  1038. for (i = 0; i < nr_active_ports; i++, res++) {
  1039. if (bfin_serial_ports[i].port.mapbase != res->start)
  1040. continue;
  1041. bfin_serial_ports[i].port.dev = &dev->dev;
  1042. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. static int bfin_serial_remove(struct platform_device *dev)
  1048. {
  1049. int i;
  1050. for (i = 0; i < nr_active_ports; i++) {
  1051. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1052. continue;
  1053. uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1054. bfin_serial_ports[i].port.dev = NULL;
  1055. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  1056. gpio_free(bfin_serial_ports[i].cts_pin);
  1057. gpio_free(bfin_serial_ports[i].rts_pin);
  1058. #endif
  1059. }
  1060. return 0;
  1061. }
  1062. static struct platform_driver bfin_serial_driver = {
  1063. .probe = bfin_serial_probe,
  1064. .remove = bfin_serial_remove,
  1065. .suspend = bfin_serial_suspend,
  1066. .resume = bfin_serial_resume,
  1067. .driver = {
  1068. .name = "bfin-uart",
  1069. .owner = THIS_MODULE,
  1070. },
  1071. };
  1072. static int __init bfin_serial_init(void)
  1073. {
  1074. int ret;
  1075. pr_info("Serial: Blackfin serial driver\n");
  1076. bfin_serial_init_ports();
  1077. ret = uart_register_driver(&bfin_serial_reg);
  1078. if (ret == 0) {
  1079. ret = platform_driver_register(&bfin_serial_driver);
  1080. if (ret) {
  1081. pr_debug("uart register failed\n");
  1082. uart_unregister_driver(&bfin_serial_reg);
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. static void __exit bfin_serial_exit(void)
  1088. {
  1089. platform_driver_unregister(&bfin_serial_driver);
  1090. uart_unregister_driver(&bfin_serial_reg);
  1091. }
  1092. module_init(bfin_serial_init);
  1093. module_exit(bfin_serial_exit);
  1094. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1095. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1096. MODULE_LICENSE("GPL");
  1097. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1098. MODULE_ALIAS("platform:bfin-uart");