bfin_5xx.c 33 KB

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