bfin_uart.c 39 KB

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