bfin_uart.c 40 KB

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