bfin_uart.c 40 KB

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