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