bfin_5xx.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. #ifdef CONFIG_SERIAL_BFIN_MODULE
  36. # undef CONFIG_EARLY_PRINTK
  37. #endif
  38. #ifdef CONFIG_SERIAL_BFIN_MODULE
  39. # undef CONFIG_EARLY_PRINTK
  40. #endif
  41. /* UART name and device definitions */
  42. #define BFIN_SERIAL_NAME "ttyBF"
  43. #define BFIN_SERIAL_MAJOR 204
  44. #define BFIN_SERIAL_MINOR 64
  45. static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
  46. static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
  47. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  48. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  49. # ifndef CONFIG_SERIAL_BFIN_PIO
  50. # error KGDB only support UART in PIO mode.
  51. # endif
  52. static int kgdboc_port_line;
  53. static int kgdboc_break_enabled;
  54. #endif
  55. /*
  56. * Setup for console. Argument comes from the menuconfig
  57. */
  58. #define DMA_RX_XCOUNT 512
  59. #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
  60. #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
  61. #ifdef CONFIG_SERIAL_BFIN_DMA
  62. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
  63. #else
  64. static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
  65. #endif
  66. static void bfin_serial_reset_irda(struct uart_port *port);
  67. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  68. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  69. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  70. {
  71. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  72. if (uart->cts_pin < 0)
  73. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  74. /* CTS PIN is negative assertive. */
  75. if (UART_GET_CTS(uart))
  76. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  77. else
  78. return TIOCM_DSR | TIOCM_CAR;
  79. }
  80. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  81. {
  82. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  83. if (uart->rts_pin < 0)
  84. return;
  85. /* RTS PIN is negative assertive. */
  86. if (mctrl & TIOCM_RTS)
  87. UART_ENABLE_RTS(uart);
  88. else
  89. UART_DISABLE_RTS(uart);
  90. }
  91. /*
  92. * Handle any change of modem status signal.
  93. */
  94. static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
  95. {
  96. struct bfin_serial_port *uart = dev_id;
  97. unsigned int status;
  98. status = bfin_serial_get_mctrl(&uart->port);
  99. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  100. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  101. uart->scts = 1;
  102. UART_CLEAR_SCTS(uart);
  103. UART_CLEAR_IER(uart, EDSSI);
  104. #endif
  105. return IRQ_HANDLED;
  106. }
  107. #else
  108. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  109. {
  110. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  111. }
  112. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  113. {
  114. }
  115. #endif
  116. /*
  117. * interrupts are disabled on entry
  118. */
  119. static void bfin_serial_stop_tx(struct uart_port *port)
  120. {
  121. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  122. #ifdef CONFIG_SERIAL_BFIN_DMA
  123. struct circ_buf *xmit = &uart->port.state->xmit;
  124. #endif
  125. while (!(UART_GET_LSR(uart) & TEMT))
  126. cpu_relax();
  127. #ifdef CONFIG_SERIAL_BFIN_DMA
  128. disable_dma(uart->tx_dma_channel);
  129. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  130. uart->port.icount.tx += uart->tx_count;
  131. uart->tx_count = 0;
  132. uart->tx_done = 1;
  133. #else
  134. #ifdef CONFIG_BF54x
  135. /* Clear TFI bit */
  136. UART_PUT_LSR(uart, TFI);
  137. #endif
  138. UART_CLEAR_IER(uart, ETBEI);
  139. #endif
  140. }
  141. /*
  142. * port is locked and interrupts are disabled
  143. */
  144. static void bfin_serial_start_tx(struct uart_port *port)
  145. {
  146. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  147. struct tty_struct *tty = uart->port.state->port.tty;
  148. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  149. if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
  150. uart->scts = 0;
  151. uart_handle_cts_change(&uart->port, uart->scts);
  152. }
  153. #endif
  154. /*
  155. * To avoid losting RX interrupt, we reset IR function
  156. * before sending data.
  157. */
  158. if (tty->termios->c_line == N_IRDA)
  159. bfin_serial_reset_irda(port);
  160. #ifdef CONFIG_SERIAL_BFIN_DMA
  161. if (uart->tx_done)
  162. bfin_serial_dma_tx_chars(uart);
  163. #else
  164. UART_SET_IER(uart, ETBEI);
  165. bfin_serial_tx_chars(uart);
  166. #endif
  167. }
  168. /*
  169. * Interrupts are enabled
  170. */
  171. static void bfin_serial_stop_rx(struct uart_port *port)
  172. {
  173. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  174. UART_CLEAR_IER(uart, ERBFI);
  175. }
  176. /*
  177. * Set the modem control timer to fire immediately.
  178. */
  179. static void bfin_serial_enable_ms(struct uart_port *port)
  180. {
  181. }
  182. #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
  183. # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
  184. # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
  185. #else
  186. # define UART_GET_ANOMALY_THRESHOLD(uart) 0
  187. # define UART_SET_ANOMALY_THRESHOLD(uart, v)
  188. #endif
  189. #ifdef CONFIG_SERIAL_BFIN_PIO
  190. static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
  191. {
  192. struct tty_struct *tty = NULL;
  193. unsigned int status, ch, flg;
  194. static struct timeval anomaly_start = { .tv_sec = 0 };
  195. status = UART_GET_LSR(uart);
  196. UART_CLEAR_LSR(uart);
  197. ch = UART_GET_CHAR(uart);
  198. uart->port.icount.rx++;
  199. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  200. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  201. if (kgdb_connected && kgdboc_port_line == uart->port.line
  202. && kgdboc_break_enabled)
  203. if (ch == 0x3) {/* Ctrl + C */
  204. kgdb_breakpoint();
  205. return;
  206. }
  207. if (!uart->port.state || !uart->port.state->port.tty)
  208. return;
  209. #endif
  210. tty = uart->port.state->port.tty;
  211. if (ANOMALY_05000363) {
  212. /* The BF533 (and BF561) family of processors have a nice anomaly
  213. * where they continuously generate characters for a "single" break.
  214. * We have to basically ignore this flood until the "next" valid
  215. * character comes across. Due to the nature of the flood, it is
  216. * not possible to reliably catch bytes that are sent too quickly
  217. * after this break. So application code talking to the Blackfin
  218. * which sends a break signal must allow at least 1.5 character
  219. * times after the end of the break for things to stabilize. This
  220. * timeout was picked as it must absolutely be larger than 1
  221. * character time +/- some percent. So 1.5 sounds good. All other
  222. * Blackfin families operate properly. Woo.
  223. */
  224. if (anomaly_start.tv_sec) {
  225. struct timeval curr;
  226. suseconds_t usecs;
  227. if ((~ch & (~ch + 1)) & 0xff)
  228. goto known_good_char;
  229. do_gettimeofday(&curr);
  230. if (curr.tv_sec - anomaly_start.tv_sec > 1)
  231. goto known_good_char;
  232. usecs = 0;
  233. if (curr.tv_sec != anomaly_start.tv_sec)
  234. usecs += USEC_PER_SEC;
  235. usecs += curr.tv_usec - anomaly_start.tv_usec;
  236. if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
  237. goto known_good_char;
  238. if (ch)
  239. anomaly_start.tv_sec = 0;
  240. else
  241. anomaly_start = curr;
  242. return;
  243. known_good_char:
  244. status &= ~BI;
  245. anomaly_start.tv_sec = 0;
  246. }
  247. }
  248. if (status & BI) {
  249. if (ANOMALY_05000363)
  250. if (bfin_revid() < 5)
  251. do_gettimeofday(&anomaly_start);
  252. uart->port.icount.brk++;
  253. if (uart_handle_break(&uart->port))
  254. goto ignore_char;
  255. status &= ~(PE | FE);
  256. }
  257. if (status & PE)
  258. uart->port.icount.parity++;
  259. if (status & OE)
  260. uart->port.icount.overrun++;
  261. if (status & FE)
  262. uart->port.icount.frame++;
  263. status &= uart->port.read_status_mask;
  264. if (status & BI)
  265. flg = TTY_BREAK;
  266. else if (status & PE)
  267. flg = TTY_PARITY;
  268. else if (status & FE)
  269. flg = TTY_FRAME;
  270. else
  271. flg = TTY_NORMAL;
  272. if (uart_handle_sysrq_char(&uart->port, ch))
  273. goto ignore_char;
  274. uart_insert_char(&uart->port, status, OE, ch, flg);
  275. ignore_char:
  276. tty_flip_buffer_push(tty);
  277. }
  278. static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
  279. {
  280. struct circ_buf *xmit = &uart->port.state->xmit;
  281. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  282. #ifdef CONFIG_BF54x
  283. /* Clear TFI bit */
  284. UART_PUT_LSR(uart, TFI);
  285. #endif
  286. /* Anomaly notes:
  287. * 05000215 - we always clear ETBEI within last UART TX
  288. * interrupt to end a string. It is always set
  289. * when start a new tx.
  290. */
  291. UART_CLEAR_IER(uart, ETBEI);
  292. return;
  293. }
  294. if (uart->port.x_char) {
  295. UART_PUT_CHAR(uart, uart->port.x_char);
  296. uart->port.icount.tx++;
  297. uart->port.x_char = 0;
  298. }
  299. while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
  300. UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
  301. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  302. uart->port.icount.tx++;
  303. SSYNC();
  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. unsigned uart_dma_ch_rx, uart_dma_ch_tx;
  580. switch (uart->port.irq) {
  581. case IRQ_UART3_RX:
  582. uart_dma_ch_rx = CH_UART3_RX;
  583. uart_dma_ch_tx = CH_UART3_TX;
  584. break;
  585. case IRQ_UART2_RX:
  586. uart_dma_ch_rx = CH_UART2_RX;
  587. uart_dma_ch_tx = CH_UART2_TX;
  588. break;
  589. default:
  590. uart_dma_ch_rx = uart_dma_ch_tx = 0;
  591. break;
  592. };
  593. if (uart_dma_ch_rx &&
  594. request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
  595. printk(KERN_NOTICE"Fail to attach UART interrupt\n");
  596. free_irq(uart->port.irq, uart);
  597. free_irq(uart->port.irq + 1, uart);
  598. return -EBUSY;
  599. }
  600. if (uart_dma_ch_tx &&
  601. request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
  602. printk(KERN_NOTICE "Fail to attach UART interrupt\n");
  603. free_dma(uart_dma_ch_rx);
  604. free_irq(uart->port.irq, uart);
  605. free_irq(uart->port.irq + 1, uart);
  606. return -EBUSY;
  607. }
  608. }
  609. # endif
  610. # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  611. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  612. }
  613. # endif
  614. #endif
  615. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  616. if (uart->cts_pin >= 0) {
  617. if (request_irq(gpio_to_irq(uart->cts_pin),
  618. bfin_serial_mctrl_cts_int,
  619. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  620. IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
  621. uart->cts_pin = -1;
  622. pr_info("Unable to attach BlackFin UART CTS interrupt.\
  623. So, disable it.\n");
  624. }
  625. }
  626. if (uart->rts_pin >= 0) {
  627. gpio_request(uart->rts_pin, DRIVER_NAME);
  628. gpio_direction_output(uart->rts_pin, 0);
  629. }
  630. #endif
  631. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  632. if (request_irq(uart->status_irq,
  633. bfin_serial_mctrl_cts_int,
  634. IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
  635. pr_info("Unable to attach BlackFin UART Modem \
  636. Status interrupt.\n");
  637. }
  638. /* CTS RTS PINs are negative assertive. */
  639. UART_PUT_MCR(uart, ACTS);
  640. UART_SET_IER(uart, EDSSI);
  641. #endif
  642. UART_SET_IER(uart, ERBFI);
  643. return 0;
  644. }
  645. static void bfin_serial_shutdown(struct uart_port *port)
  646. {
  647. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  648. #ifdef CONFIG_SERIAL_BFIN_DMA
  649. disable_dma(uart->tx_dma_channel);
  650. free_dma(uart->tx_dma_channel);
  651. disable_dma(uart->rx_dma_channel);
  652. free_dma(uart->rx_dma_channel);
  653. del_timer(&(uart->rx_dma_timer));
  654. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  655. #else
  656. #ifdef CONFIG_BF54x
  657. switch (uart->port.irq) {
  658. case IRQ_UART3_RX:
  659. free_dma(CH_UART3_RX);
  660. free_dma(CH_UART3_TX);
  661. break;
  662. case IRQ_UART2_RX:
  663. free_dma(CH_UART2_RX);
  664. free_dma(CH_UART2_TX);
  665. break;
  666. default:
  667. break;
  668. };
  669. #endif
  670. free_irq(uart->port.irq, uart);
  671. free_irq(uart->port.irq+1, uart);
  672. #endif
  673. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  674. if (uart->cts_pin >= 0)
  675. free_irq(gpio_to_irq(uart->cts_pin), uart);
  676. if (uart->rts_pin >= 0)
  677. gpio_free(uart->rts_pin);
  678. #endif
  679. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  680. if (UART_GET_IER(uart) && EDSSI)
  681. free_irq(uart->status_irq, uart);
  682. #endif
  683. }
  684. static void
  685. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  686. struct ktermios *old)
  687. {
  688. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  689. unsigned long flags;
  690. unsigned int baud, quot;
  691. unsigned short val, ier, lcr = 0;
  692. switch (termios->c_cflag & CSIZE) {
  693. case CS8:
  694. lcr = WLS(8);
  695. break;
  696. case CS7:
  697. lcr = WLS(7);
  698. break;
  699. case CS6:
  700. lcr = WLS(6);
  701. break;
  702. case CS5:
  703. lcr = WLS(5);
  704. break;
  705. default:
  706. printk(KERN_ERR "%s: word lengh not supported\n",
  707. __func__);
  708. }
  709. /* Anomaly notes:
  710. * 05000231 - STOP bit is always set to 1 whatever the user is set.
  711. */
  712. if (termios->c_cflag & CSTOPB) {
  713. if (ANOMALY_05000231)
  714. printk(KERN_WARNING "STOP bits other than 1 is not "
  715. "supported in case of anomaly 05000231.\n");
  716. else
  717. lcr |= STB;
  718. }
  719. if (termios->c_cflag & PARENB)
  720. lcr |= PEN;
  721. if (!(termios->c_cflag & PARODD))
  722. lcr |= EPS;
  723. if (termios->c_cflag & CMSPAR)
  724. lcr |= STP;
  725. port->read_status_mask = OE;
  726. if (termios->c_iflag & INPCK)
  727. port->read_status_mask |= (FE | PE);
  728. if (termios->c_iflag & (BRKINT | PARMRK))
  729. port->read_status_mask |= BI;
  730. /*
  731. * Characters to ignore
  732. */
  733. port->ignore_status_mask = 0;
  734. if (termios->c_iflag & IGNPAR)
  735. port->ignore_status_mask |= FE | PE;
  736. if (termios->c_iflag & IGNBRK) {
  737. port->ignore_status_mask |= BI;
  738. /*
  739. * If we're ignoring parity and break indicators,
  740. * ignore overruns too (for real raw support).
  741. */
  742. if (termios->c_iflag & IGNPAR)
  743. port->ignore_status_mask |= OE;
  744. }
  745. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  746. quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
  747. spin_lock_irqsave(&uart->port.lock, flags);
  748. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  749. /* Disable UART */
  750. ier = UART_GET_IER(uart);
  751. UART_DISABLE_INTS(uart);
  752. /* Set DLAB in LCR to Access DLL and DLH */
  753. UART_SET_DLAB(uart);
  754. UART_PUT_DLL(uart, quot & 0xFF);
  755. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  756. SSYNC();
  757. /* Clear DLAB in LCR to Access THR RBR IER */
  758. UART_CLEAR_DLAB(uart);
  759. UART_PUT_LCR(uart, lcr);
  760. /* Enable UART */
  761. UART_ENABLE_INTS(uart, ier);
  762. val = UART_GET_GCTL(uart);
  763. val |= UCEN;
  764. UART_PUT_GCTL(uart, val);
  765. /* Port speed changed, update the per-port timeout. */
  766. uart_update_timeout(port, termios->c_cflag, baud);
  767. spin_unlock_irqrestore(&uart->port.lock, flags);
  768. }
  769. static const char *bfin_serial_type(struct uart_port *port)
  770. {
  771. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  772. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  773. }
  774. /*
  775. * Release the memory region(s) being used by 'port'.
  776. */
  777. static void bfin_serial_release_port(struct uart_port *port)
  778. {
  779. }
  780. /*
  781. * Request the memory region(s) being used by 'port'.
  782. */
  783. static int bfin_serial_request_port(struct uart_port *port)
  784. {
  785. return 0;
  786. }
  787. /*
  788. * Configure/autoconfigure the port.
  789. */
  790. static void bfin_serial_config_port(struct uart_port *port, int flags)
  791. {
  792. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  793. if (flags & UART_CONFIG_TYPE &&
  794. bfin_serial_request_port(&uart->port) == 0)
  795. uart->port.type = PORT_BFIN;
  796. }
  797. /*
  798. * Verify the new serial_struct (for TIOCSSERIAL).
  799. * The only change we allow are to the flags and type, and
  800. * even then only between PORT_BFIN and PORT_UNKNOWN
  801. */
  802. static int
  803. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  804. {
  805. return 0;
  806. }
  807. /*
  808. * Enable the IrDA function if tty->ldisc.num is N_IRDA.
  809. * In other cases, disable IrDA function.
  810. */
  811. static void bfin_serial_set_ldisc(struct uart_port *port)
  812. {
  813. int line = port->line;
  814. unsigned short val;
  815. if (line >= port->state->port.tty->driver->num)
  816. return;
  817. switch (port->state->port.tty->termios->c_line) {
  818. case N_IRDA:
  819. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  820. val |= (IREN | RPOLC);
  821. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  822. break;
  823. default:
  824. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  825. val &= ~(IREN | RPOLC);
  826. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  827. }
  828. }
  829. static void bfin_serial_reset_irda(struct uart_port *port)
  830. {
  831. int line = port->line;
  832. unsigned short val;
  833. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  834. val &= ~(IREN | RPOLC);
  835. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  836. SSYNC();
  837. val |= (IREN | RPOLC);
  838. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  839. SSYNC();
  840. }
  841. #ifdef CONFIG_CONSOLE_POLL
  842. /* Anomaly notes:
  843. * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
  844. * losing other bits of UART_LSR is not a problem here.
  845. */
  846. static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
  847. {
  848. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  849. while (!(UART_GET_LSR(uart) & THRE))
  850. cpu_relax();
  851. UART_CLEAR_DLAB(uart);
  852. UART_PUT_CHAR(uart, (unsigned char)chr);
  853. }
  854. static int bfin_serial_poll_get_char(struct uart_port *port)
  855. {
  856. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  857. unsigned char chr;
  858. while (!(UART_GET_LSR(uart) & DR))
  859. cpu_relax();
  860. UART_CLEAR_DLAB(uart);
  861. chr = UART_GET_CHAR(uart);
  862. return chr;
  863. }
  864. #endif
  865. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  866. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  867. static void bfin_kgdboc_port_shutdown(struct uart_port *port)
  868. {
  869. if (kgdboc_break_enabled) {
  870. kgdboc_break_enabled = 0;
  871. bfin_serial_shutdown(port);
  872. }
  873. }
  874. static int bfin_kgdboc_port_startup(struct uart_port *port)
  875. {
  876. kgdboc_port_line = port->line;
  877. kgdboc_break_enabled = !bfin_serial_startup(port);
  878. return 0;
  879. }
  880. #endif
  881. static struct uart_ops bfin_serial_pops = {
  882. .tx_empty = bfin_serial_tx_empty,
  883. .set_mctrl = bfin_serial_set_mctrl,
  884. .get_mctrl = bfin_serial_get_mctrl,
  885. .stop_tx = bfin_serial_stop_tx,
  886. .start_tx = bfin_serial_start_tx,
  887. .stop_rx = bfin_serial_stop_rx,
  888. .enable_ms = bfin_serial_enable_ms,
  889. .break_ctl = bfin_serial_break_ctl,
  890. .startup = bfin_serial_startup,
  891. .shutdown = bfin_serial_shutdown,
  892. .set_termios = bfin_serial_set_termios,
  893. .set_ldisc = bfin_serial_set_ldisc,
  894. .type = bfin_serial_type,
  895. .release_port = bfin_serial_release_port,
  896. .request_port = bfin_serial_request_port,
  897. .config_port = bfin_serial_config_port,
  898. .verify_port = bfin_serial_verify_port,
  899. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  900. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  901. .kgdboc_port_startup = bfin_kgdboc_port_startup,
  902. .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
  903. #endif
  904. #ifdef CONFIG_CONSOLE_POLL
  905. .poll_put_char = bfin_serial_poll_put_char,
  906. .poll_get_char = bfin_serial_poll_get_char,
  907. #endif
  908. };
  909. static void __init bfin_serial_hw_init(void)
  910. {
  911. #ifdef CONFIG_SERIAL_BFIN_UART0
  912. peripheral_request(P_UART0_TX, DRIVER_NAME);
  913. peripheral_request(P_UART0_RX, DRIVER_NAME);
  914. #endif
  915. #ifdef CONFIG_SERIAL_BFIN_UART1
  916. peripheral_request(P_UART1_TX, DRIVER_NAME);
  917. peripheral_request(P_UART1_RX, DRIVER_NAME);
  918. # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
  919. peripheral_request(P_UART1_RTS, DRIVER_NAME);
  920. peripheral_request(P_UART1_CTS, DRIVER_NAME);
  921. # endif
  922. #endif
  923. #ifdef CONFIG_SERIAL_BFIN_UART2
  924. peripheral_request(P_UART2_TX, DRIVER_NAME);
  925. peripheral_request(P_UART2_RX, DRIVER_NAME);
  926. #endif
  927. #ifdef CONFIG_SERIAL_BFIN_UART3
  928. peripheral_request(P_UART3_TX, DRIVER_NAME);
  929. peripheral_request(P_UART3_RX, DRIVER_NAME);
  930. # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
  931. peripheral_request(P_UART3_RTS, DRIVER_NAME);
  932. peripheral_request(P_UART3_CTS, DRIVER_NAME);
  933. # endif
  934. #endif
  935. }
  936. static void __init bfin_serial_init_ports(void)
  937. {
  938. static int first = 1;
  939. int i;
  940. if (!first)
  941. return;
  942. first = 0;
  943. bfin_serial_hw_init();
  944. for (i = 0; i < nr_active_ports; i++) {
  945. spin_lock_init(&bfin_serial_ports[i].port.lock);
  946. bfin_serial_ports[i].port.uartclk = get_sclk();
  947. bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
  948. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  949. bfin_serial_ports[i].port.line = i;
  950. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  951. bfin_serial_ports[i].port.membase =
  952. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  953. bfin_serial_ports[i].port.mapbase =
  954. bfin_serial_resource[i].uart_base_addr;
  955. bfin_serial_ports[i].port.irq =
  956. bfin_serial_resource[i].uart_irq;
  957. bfin_serial_ports[i].status_irq =
  958. bfin_serial_resource[i].uart_status_irq;
  959. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  960. #ifdef CONFIG_SERIAL_BFIN_DMA
  961. bfin_serial_ports[i].tx_done = 1;
  962. bfin_serial_ports[i].tx_count = 0;
  963. bfin_serial_ports[i].tx_dma_channel =
  964. bfin_serial_resource[i].uart_tx_dma_channel;
  965. bfin_serial_ports[i].rx_dma_channel =
  966. bfin_serial_resource[i].uart_rx_dma_channel;
  967. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  968. #endif
  969. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  970. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  971. bfin_serial_ports[i].cts_pin =
  972. bfin_serial_resource[i].uart_cts_pin;
  973. bfin_serial_ports[i].rts_pin =
  974. bfin_serial_resource[i].uart_rts_pin;
  975. #endif
  976. }
  977. }
  978. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  979. /*
  980. * If the port was already initialised (eg, by a boot loader),
  981. * try to determine the current setup.
  982. */
  983. static void __init
  984. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  985. int *parity, int *bits)
  986. {
  987. unsigned short status;
  988. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  989. if (status == (ERBFI | ETBEI)) {
  990. /* ok, the port was enabled */
  991. u16 lcr, dlh, dll;
  992. lcr = UART_GET_LCR(uart);
  993. *parity = 'n';
  994. if (lcr & PEN) {
  995. if (lcr & EPS)
  996. *parity = 'e';
  997. else
  998. *parity = 'o';
  999. }
  1000. switch (lcr & 0x03) {
  1001. case 0: *bits = 5; break;
  1002. case 1: *bits = 6; break;
  1003. case 2: *bits = 7; break;
  1004. case 3: *bits = 8; break;
  1005. }
  1006. /* Set DLAB in LCR to Access DLL and DLH */
  1007. UART_SET_DLAB(uart);
  1008. dll = UART_GET_DLL(uart);
  1009. dlh = UART_GET_DLH(uart);
  1010. /* Clear DLAB in LCR to Access THR RBR IER */
  1011. UART_CLEAR_DLAB(uart);
  1012. *baud = get_sclk() / (16*(dll | dlh << 8));
  1013. }
  1014. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
  1015. }
  1016. static struct uart_driver bfin_serial_reg;
  1017. static int __init
  1018. bfin_serial_console_setup(struct console *co, char *options)
  1019. {
  1020. struct bfin_serial_port *uart;
  1021. int baud = 57600;
  1022. int bits = 8;
  1023. int parity = 'n';
  1024. # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  1025. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  1026. int flow = 'r';
  1027. # else
  1028. int flow = 'n';
  1029. # endif
  1030. /*
  1031. * Check whether an invalid uart number has been specified, and
  1032. * if so, search for the first available port that does have
  1033. * console support.
  1034. */
  1035. if (co->index == -1 || co->index >= nr_active_ports)
  1036. co->index = 0;
  1037. uart = &bfin_serial_ports[co->index];
  1038. if (options)
  1039. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1040. else
  1041. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  1042. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  1043. }
  1044. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  1045. defined (CONFIG_EARLY_PRINTK) */
  1046. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  1047. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  1048. {
  1049. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1050. while (!(UART_GET_LSR(uart) & THRE))
  1051. barrier();
  1052. UART_PUT_CHAR(uart, ch);
  1053. SSYNC();
  1054. }
  1055. /*
  1056. * Interrupts are disabled on entering
  1057. */
  1058. static void
  1059. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  1060. {
  1061. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  1062. unsigned long flags;
  1063. spin_lock_irqsave(&uart->port.lock, flags);
  1064. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  1065. spin_unlock_irqrestore(&uart->port.lock, flags);
  1066. }
  1067. static struct console bfin_serial_console = {
  1068. .name = BFIN_SERIAL_NAME,
  1069. .write = bfin_serial_console_write,
  1070. .device = uart_console_device,
  1071. .setup = bfin_serial_console_setup,
  1072. .flags = CON_PRINTBUFFER,
  1073. .index = -1,
  1074. .data = &bfin_serial_reg,
  1075. };
  1076. static int __init bfin_serial_rs_console_init(void)
  1077. {
  1078. bfin_serial_init_ports();
  1079. register_console(&bfin_serial_console);
  1080. return 0;
  1081. }
  1082. console_initcall(bfin_serial_rs_console_init);
  1083. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  1084. #else
  1085. #define BFIN_SERIAL_CONSOLE NULL
  1086. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  1087. #ifdef CONFIG_EARLY_PRINTK
  1088. static __init void early_serial_putc(struct uart_port *port, int ch)
  1089. {
  1090. unsigned timeout = 0xffff;
  1091. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1092. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  1093. cpu_relax();
  1094. UART_PUT_CHAR(uart, ch);
  1095. }
  1096. static __init void early_serial_write(struct console *con, const char *s,
  1097. unsigned int n)
  1098. {
  1099. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  1100. unsigned int i;
  1101. for (i = 0; i < n; i++, s++) {
  1102. if (*s == '\n')
  1103. early_serial_putc(&uart->port, '\r');
  1104. early_serial_putc(&uart->port, *s);
  1105. }
  1106. }
  1107. /*
  1108. * This should have a .setup or .early_setup in it, but then things get called
  1109. * without the command line options, and the baud rate gets messed up - so
  1110. * don't let the common infrastructure play with things. (see calls to setup
  1111. * & earlysetup in ./kernel/printk.c:register_console()
  1112. */
  1113. static struct __initdata console bfin_early_serial_console = {
  1114. .name = "early_BFuart",
  1115. .write = early_serial_write,
  1116. .device = uart_console_device,
  1117. .flags = CON_PRINTBUFFER,
  1118. .index = -1,
  1119. .data = &bfin_serial_reg,
  1120. };
  1121. struct console __init *bfin_earlyserial_init(unsigned int port,
  1122. unsigned int cflag)
  1123. {
  1124. struct bfin_serial_port *uart;
  1125. struct ktermios t;
  1126. if (port == -1 || port >= nr_active_ports)
  1127. port = 0;
  1128. bfin_serial_init_ports();
  1129. bfin_early_serial_console.index = port;
  1130. uart = &bfin_serial_ports[port];
  1131. t.c_cflag = cflag;
  1132. t.c_iflag = 0;
  1133. t.c_oflag = 0;
  1134. t.c_lflag = ICANON;
  1135. t.c_line = port;
  1136. bfin_serial_set_termios(&uart->port, &t, &t);
  1137. return &bfin_early_serial_console;
  1138. }
  1139. #endif /* CONFIG_EARLY_PRINTK */
  1140. static struct uart_driver bfin_serial_reg = {
  1141. .owner = THIS_MODULE,
  1142. .driver_name = "bfin-uart",
  1143. .dev_name = BFIN_SERIAL_NAME,
  1144. .major = BFIN_SERIAL_MAJOR,
  1145. .minor = BFIN_SERIAL_MINOR,
  1146. .nr = BFIN_UART_NR_PORTS,
  1147. .cons = BFIN_SERIAL_CONSOLE,
  1148. };
  1149. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1150. {
  1151. int i;
  1152. for (i = 0; i < nr_active_ports; i++) {
  1153. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1154. continue;
  1155. uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1156. }
  1157. return 0;
  1158. }
  1159. static int bfin_serial_resume(struct platform_device *dev)
  1160. {
  1161. int i;
  1162. for (i = 0; i < nr_active_ports; i++) {
  1163. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1164. continue;
  1165. uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1166. }
  1167. return 0;
  1168. }
  1169. static int bfin_serial_probe(struct platform_device *dev)
  1170. {
  1171. struct resource *res = dev->resource;
  1172. int i;
  1173. for (i = 0; i < dev->num_resources; i++, res++)
  1174. if (res->flags & IORESOURCE_MEM)
  1175. break;
  1176. if (i < dev->num_resources) {
  1177. for (i = 0; i < nr_active_ports; i++, res++) {
  1178. if (bfin_serial_ports[i].port.mapbase != res->start)
  1179. continue;
  1180. bfin_serial_ports[i].port.dev = &dev->dev;
  1181. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1182. }
  1183. }
  1184. return 0;
  1185. }
  1186. static int bfin_serial_remove(struct platform_device *dev)
  1187. {
  1188. int i;
  1189. for (i = 0; i < nr_active_ports; i++) {
  1190. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1191. continue;
  1192. uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1193. bfin_serial_ports[i].port.dev = NULL;
  1194. #if defined(CONFIG_SERIAL_BFIN_CTSRTS)
  1195. gpio_free(bfin_serial_ports[i].cts_pin);
  1196. gpio_free(bfin_serial_ports[i].rts_pin);
  1197. #endif
  1198. }
  1199. return 0;
  1200. }
  1201. static struct platform_driver bfin_serial_driver = {
  1202. .probe = bfin_serial_probe,
  1203. .remove = bfin_serial_remove,
  1204. .suspend = bfin_serial_suspend,
  1205. .resume = bfin_serial_resume,
  1206. .driver = {
  1207. .name = "bfin-uart",
  1208. .owner = THIS_MODULE,
  1209. },
  1210. };
  1211. static int __init bfin_serial_init(void)
  1212. {
  1213. int ret;
  1214. pr_info("Serial: Blackfin serial driver\n");
  1215. bfin_serial_init_ports();
  1216. ret = uart_register_driver(&bfin_serial_reg);
  1217. if (ret == 0) {
  1218. ret = platform_driver_register(&bfin_serial_driver);
  1219. if (ret) {
  1220. pr_debug("uart register failed\n");
  1221. uart_unregister_driver(&bfin_serial_reg);
  1222. }
  1223. }
  1224. return ret;
  1225. }
  1226. static void __exit bfin_serial_exit(void)
  1227. {
  1228. platform_driver_unregister(&bfin_serial_driver);
  1229. uart_unregister_driver(&bfin_serial_reg);
  1230. }
  1231. module_init(bfin_serial_init);
  1232. module_exit(bfin_serial_exit);
  1233. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1234. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1235. MODULE_LICENSE("GPL");
  1236. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1237. MODULE_ALIAS("platform:bfin-uart");