bfin_5xx.c 37 KB

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