bfin_5xx.c 37 KB

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