bfin_5xx.c 37 KB

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