bfin_5xx.c 36 KB

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