bfin_5xx.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. #ifdef CONFIG_SERIAL_BFIN_MODULE
  36. # undef CONFIG_EARLY_PRINTK
  37. #endif
  38. #ifdef CONFIG_SERIAL_BFIN_MODULE
  39. # undef CONFIG_EARLY_PRINTK
  40. #endif
  41. /* UART name and device definitions */
  42. #define BFIN_SERIAL_NAME "ttyBF"
  43. #define BFIN_SERIAL_MAJOR 204
  44. #define BFIN_SERIAL_MINOR 64
  45. static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
  46. static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
  47. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  48. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  49. # ifndef CONFIG_SERIAL_BFIN_PIO
  50. # error KGDB only support UART in PIO mode.
  51. # endif
  52. static int kgdboc_port_line;
  53. static int kgdboc_break_enabled;
  54. #endif
  55. /*
  56. * Setup for console. Argument comes from the menuconfig
  57. */
  58. #define DMA_RX_XCOUNT 512
  59. #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
  60. #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
  61. #ifdef CONFIG_SERIAL_BFIN_DMA
  62. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
  63. #else
  64. static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
  65. #endif
  66. static void bfin_serial_reset_irda(struct uart_port *port);
  67. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  68. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  69. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  70. {
  71. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  72. if (uart->cts_pin < 0)
  73. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  74. /* CTS PIN is negative assertive. */
  75. if (UART_GET_CTS(uart))
  76. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  77. else
  78. return TIOCM_DSR | TIOCM_CAR;
  79. }
  80. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  81. {
  82. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  83. if (uart->rts_pin < 0)
  84. return;
  85. /* RTS PIN is negative assertive. */
  86. if (mctrl & TIOCM_RTS)
  87. UART_ENABLE_RTS(uart);
  88. else
  89. UART_DISABLE_RTS(uart);
  90. }
  91. /*
  92. * Handle any change of modem status signal.
  93. */
  94. static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
  95. {
  96. struct bfin_serial_port *uart = dev_id;
  97. unsigned int status;
  98. status = bfin_serial_get_mctrl(&uart->port);
  99. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  100. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  101. uart->scts = 1;
  102. UART_CLEAR_SCTS(uart);
  103. UART_CLEAR_IER(uart, EDSSI);
  104. #endif
  105. return IRQ_HANDLED;
  106. }
  107. #else
  108. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  109. {
  110. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  111. }
  112. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  113. {
  114. }
  115. #endif
  116. /*
  117. * interrupts are disabled on entry
  118. */
  119. static void bfin_serial_stop_tx(struct uart_port *port)
  120. {
  121. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  122. #ifdef CONFIG_SERIAL_BFIN_DMA
  123. struct circ_buf *xmit = &uart->port.state->xmit;
  124. #endif
  125. while (!(UART_GET_LSR(uart) & TEMT))
  126. cpu_relax();
  127. #ifdef CONFIG_SERIAL_BFIN_DMA
  128. disable_dma(uart->tx_dma_channel);
  129. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  130. uart->port.icount.tx += uart->tx_count;
  131. uart->tx_count = 0;
  132. uart->tx_done = 1;
  133. #else
  134. #ifdef CONFIG_BF54x
  135. /* Clear TFI bit */
  136. UART_PUT_LSR(uart, TFI);
  137. #endif
  138. UART_CLEAR_IER(uart, ETBEI);
  139. #endif
  140. }
  141. /*
  142. * port is locked and interrupts are disabled
  143. */
  144. static void bfin_serial_start_tx(struct uart_port *port)
  145. {
  146. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  147. struct tty_struct *tty = uart->port.state->port.tty;
  148. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  149. if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
  150. uart->scts = 0;
  151. uart_handle_cts_change(&uart->port, uart->scts);
  152. }
  153. #endif
  154. /*
  155. * To avoid losting RX interrupt, we reset IR function
  156. * before sending data.
  157. */
  158. if (tty->termios->c_line == N_IRDA)
  159. bfin_serial_reset_irda(port);
  160. #ifdef CONFIG_SERIAL_BFIN_DMA
  161. if (uart->tx_done)
  162. bfin_serial_dma_tx_chars(uart);
  163. #else
  164. UART_SET_IER(uart, ETBEI);
  165. bfin_serial_tx_chars(uart);
  166. #endif
  167. }
  168. /*
  169. * Interrupts are enabled
  170. */
  171. static void bfin_serial_stop_rx(struct uart_port *port)
  172. {
  173. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  174. UART_CLEAR_IER(uart, ERBFI);
  175. }
  176. /*
  177. * Set the modem control timer to fire immediately.
  178. */
  179. static void bfin_serial_enable_ms(struct uart_port *port)
  180. {
  181. }
  182. #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
  183. # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
  184. # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
  185. #else
  186. # define UART_GET_ANOMALY_THRESHOLD(uart) 0
  187. # define UART_SET_ANOMALY_THRESHOLD(uart, v)
  188. #endif
  189. #ifdef CONFIG_SERIAL_BFIN_PIO
  190. static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
  191. {
  192. struct tty_struct *tty = NULL;
  193. unsigned int status, ch, flg;
  194. static struct timeval anomaly_start = { .tv_sec = 0 };
  195. status = UART_GET_LSR(uart);
  196. UART_CLEAR_LSR(uart);
  197. ch = UART_GET_CHAR(uart);
  198. uart->port.icount.rx++;
  199. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  200. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  201. if (kgdb_connected && kgdboc_port_line == uart->port.line)
  202. if (ch == 0x3) {/* Ctrl + C */
  203. kgdb_breakpoint();
  204. return;
  205. }
  206. if (!uart->port.state || !uart->port.state->port.tty)
  207. return;
  208. #endif
  209. tty = uart->port.state->port.tty;
  210. if (ANOMALY_05000363) {
  211. /* The BF533 (and BF561) family of processors have a nice anomaly
  212. * where they continuously generate characters for a "single" break.
  213. * We have to basically ignore this flood until the "next" valid
  214. * character comes across. Due to the nature of the flood, it is
  215. * not possible to reliably catch bytes that are sent too quickly
  216. * after this break. So application code talking to the Blackfin
  217. * which sends a break signal must allow at least 1.5 character
  218. * times after the end of the break for things to stabilize. This
  219. * timeout was picked as it must absolutely be larger than 1
  220. * character time +/- some percent. So 1.5 sounds good. All other
  221. * Blackfin families operate properly. Woo.
  222. */
  223. if (anomaly_start.tv_sec) {
  224. struct timeval curr;
  225. suseconds_t usecs;
  226. if ((~ch & (~ch + 1)) & 0xff)
  227. goto known_good_char;
  228. do_gettimeofday(&curr);
  229. if (curr.tv_sec - anomaly_start.tv_sec > 1)
  230. goto known_good_char;
  231. usecs = 0;
  232. if (curr.tv_sec != anomaly_start.tv_sec)
  233. usecs += USEC_PER_SEC;
  234. usecs += curr.tv_usec - anomaly_start.tv_usec;
  235. if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
  236. goto known_good_char;
  237. if (ch)
  238. anomaly_start.tv_sec = 0;
  239. else
  240. anomaly_start = curr;
  241. return;
  242. known_good_char:
  243. status &= ~BI;
  244. anomaly_start.tv_sec = 0;
  245. }
  246. }
  247. if (status & BI) {
  248. if (ANOMALY_05000363)
  249. if (bfin_revid() < 5)
  250. do_gettimeofday(&anomaly_start);
  251. uart->port.icount.brk++;
  252. if (uart_handle_break(&uart->port))
  253. goto ignore_char;
  254. status &= ~(PE | FE);
  255. }
  256. if (status & PE)
  257. uart->port.icount.parity++;
  258. if (status & OE)
  259. uart->port.icount.overrun++;
  260. if (status & FE)
  261. uart->port.icount.frame++;
  262. status &= uart->port.read_status_mask;
  263. if (status & BI)
  264. flg = TTY_BREAK;
  265. else if (status & PE)
  266. flg = TTY_PARITY;
  267. else if (status & FE)
  268. flg = TTY_FRAME;
  269. else
  270. flg = TTY_NORMAL;
  271. if (uart_handle_sysrq_char(&uart->port, ch))
  272. goto ignore_char;
  273. uart_insert_char(&uart->port, status, OE, ch, flg);
  274. ignore_char:
  275. tty_flip_buffer_push(tty);
  276. }
  277. static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
  278. {
  279. struct circ_buf *xmit = &uart->port.state->xmit;
  280. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  281. #ifdef CONFIG_BF54x
  282. /* Clear TFI bit */
  283. UART_PUT_LSR(uart, TFI);
  284. #endif
  285. /* Anomaly notes:
  286. * 05000215 - we always clear ETBEI within last UART TX
  287. * interrupt to end a string. It is always set
  288. * when start a new tx.
  289. */
  290. UART_CLEAR_IER(uart, ETBEI);
  291. return;
  292. }
  293. if (uart->port.x_char) {
  294. UART_PUT_CHAR(uart, uart->port.x_char);
  295. uart->port.icount.tx++;
  296. uart->port.x_char = 0;
  297. }
  298. while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
  299. UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
  300. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  301. uart->port.icount.tx++;
  302. SSYNC();
  303. }
  304. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  305. uart_write_wakeup(&uart->port);
  306. }
  307. static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
  308. {
  309. struct bfin_serial_port *uart = dev_id;
  310. spin_lock(&uart->port.lock);
  311. while (UART_GET_LSR(uart) & DR)
  312. bfin_serial_rx_chars(uart);
  313. spin_unlock(&uart->port.lock);
  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(uart->rx_dma_channel);
  411. spin_lock_bh(&uart->port.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->port.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->port.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->port.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->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  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->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  569. "BFIN_UART_TX", uart)) {
  570. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  571. free_irq(uart->port.irq, uart);
  572. return -EBUSY;
  573. }
  574. # ifdef CONFIG_BF54x
  575. {
  576. unsigned uart_dma_ch_rx, uart_dma_ch_tx;
  577. switch (uart->port.irq) {
  578. case IRQ_UART3_RX:
  579. uart_dma_ch_rx = CH_UART3_RX;
  580. uart_dma_ch_tx = CH_UART3_TX;
  581. break;
  582. case IRQ_UART2_RX:
  583. uart_dma_ch_rx = CH_UART2_RX;
  584. uart_dma_ch_tx = CH_UART2_TX;
  585. break;
  586. default:
  587. uart_dma_ch_rx = uart_dma_ch_tx = 0;
  588. break;
  589. };
  590. if (uart_dma_ch_rx &&
  591. request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
  592. printk(KERN_NOTICE"Fail to attach UART interrupt\n");
  593. free_irq(uart->port.irq, uart);
  594. free_irq(uart->port.irq + 1, uart);
  595. return -EBUSY;
  596. }
  597. if (uart_dma_ch_tx &&
  598. request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
  599. printk(KERN_NOTICE "Fail to attach UART interrupt\n");
  600. free_dma(uart_dma_ch_rx);
  601. free_irq(uart->port.irq, uart);
  602. free_irq(uart->port.irq + 1, uart);
  603. return -EBUSY;
  604. }
  605. }
  606. # endif
  607. # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  608. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  609. }
  610. # endif
  611. #endif
  612. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  613. if (uart->cts_pin >= 0) {
  614. if (request_irq(gpio_to_irq(uart->cts_pin),
  615. bfin_serial_mctrl_cts_int,
  616. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  617. IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
  618. uart->cts_pin = -1;
  619. pr_info("Unable to attach BlackFin UART CTS interrupt.\
  620. So, disable it.\n");
  621. }
  622. }
  623. if (uart->rts_pin >= 0) {
  624. gpio_request(uart->rts_pin, DRIVER_NAME);
  625. gpio_direction_output(uart->rts_pin, 0);
  626. }
  627. #endif
  628. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  629. if (request_irq(uart->status_irq,
  630. bfin_serial_mctrl_cts_int,
  631. IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
  632. pr_info("Unable to attach BlackFin UART Modem \
  633. Status interrupt.\n");
  634. }
  635. if (uart->cts_pin >= 0) {
  636. gpio_request(uart->cts_pin, DRIVER_NAME);
  637. gpio_direction_output(uart->cts_pin, 1);
  638. }
  639. if (uart->rts_pin >= 0) {
  640. gpio_request(uart->rts_pin, DRIVER_NAME);
  641. gpio_direction_output(uart->rts_pin, 0);
  642. }
  643. /* CTS RTS PINs are negative assertive. */
  644. UART_PUT_MCR(uart, ACTS);
  645. UART_SET_IER(uart, EDSSI);
  646. #endif
  647. UART_SET_IER(uart, ERBFI);
  648. return 0;
  649. }
  650. static void bfin_serial_shutdown(struct uart_port *port)
  651. {
  652. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  653. #ifdef CONFIG_SERIAL_BFIN_DMA
  654. disable_dma(uart->tx_dma_channel);
  655. free_dma(uart->tx_dma_channel);
  656. disable_dma(uart->rx_dma_channel);
  657. free_dma(uart->rx_dma_channel);
  658. del_timer(&(uart->rx_dma_timer));
  659. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  660. #else
  661. #ifdef CONFIG_BF54x
  662. switch (uart->port.irq) {
  663. case IRQ_UART3_RX:
  664. free_dma(CH_UART3_RX);
  665. free_dma(CH_UART3_TX);
  666. break;
  667. case IRQ_UART2_RX:
  668. free_dma(CH_UART2_RX);
  669. free_dma(CH_UART2_TX);
  670. break;
  671. default:
  672. break;
  673. };
  674. #endif
  675. free_irq(uart->port.irq, uart);
  676. free_irq(uart->port.irq+1, uart);
  677. #endif
  678. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  679. if (uart->cts_pin >= 0)
  680. free_irq(gpio_to_irq(uart->cts_pin), uart);
  681. if (uart->rts_pin >= 0)
  682. gpio_free(uart->rts_pin);
  683. #endif
  684. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  685. if (uart->cts_pin >= 0)
  686. gpio_free(uart->cts_pin);
  687. if (uart->rts_pin >= 0)
  688. gpio_free(uart->rts_pin);
  689. if (UART_GET_IER(uart) && EDSSI)
  690. free_irq(uart->status_irq, uart);
  691. #endif
  692. }
  693. static void
  694. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  695. struct ktermios *old)
  696. {
  697. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  698. unsigned long flags;
  699. unsigned int baud, quot;
  700. unsigned short val, ier, lcr = 0;
  701. switch (termios->c_cflag & CSIZE) {
  702. case CS8:
  703. lcr = WLS(8);
  704. break;
  705. case CS7:
  706. lcr = WLS(7);
  707. break;
  708. case CS6:
  709. lcr = WLS(6);
  710. break;
  711. case CS5:
  712. lcr = WLS(5);
  713. break;
  714. default:
  715. printk(KERN_ERR "%s: word lengh not supported\n",
  716. __func__);
  717. }
  718. /* Anomaly notes:
  719. * 05000231 - STOP bit is always set to 1 whatever the user is set.
  720. */
  721. if (termios->c_cflag & CSTOPB) {
  722. if (ANOMALY_05000231)
  723. printk(KERN_WARNING "STOP bits other than 1 is not "
  724. "supported in case of anomaly 05000231.\n");
  725. else
  726. lcr |= STB;
  727. }
  728. if (termios->c_cflag & PARENB)
  729. lcr |= PEN;
  730. if (!(termios->c_cflag & PARODD))
  731. lcr |= EPS;
  732. if (termios->c_cflag & CMSPAR)
  733. lcr |= STP;
  734. port->read_status_mask = OE;
  735. if (termios->c_iflag & INPCK)
  736. port->read_status_mask |= (FE | PE);
  737. if (termios->c_iflag & (BRKINT | PARMRK))
  738. port->read_status_mask |= BI;
  739. /*
  740. * Characters to ignore
  741. */
  742. port->ignore_status_mask = 0;
  743. if (termios->c_iflag & IGNPAR)
  744. port->ignore_status_mask |= FE | PE;
  745. if (termios->c_iflag & IGNBRK) {
  746. port->ignore_status_mask |= BI;
  747. /*
  748. * If we're ignoring parity and break indicators,
  749. * ignore overruns too (for real raw support).
  750. */
  751. if (termios->c_iflag & IGNPAR)
  752. port->ignore_status_mask |= OE;
  753. }
  754. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  755. quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
  756. spin_lock_irqsave(&uart->port.lock, flags);
  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)
  821. {
  822. int line = port->line;
  823. unsigned short val;
  824. if (line >= port->state->port.tty->driver->num)
  825. return;
  826. switch (port->state->port.tty->termios->c_line) {
  827. case N_IRDA:
  828. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  829. val |= (IREN | RPOLC);
  830. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  831. break;
  832. default:
  833. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  834. val &= ~(IREN | RPOLC);
  835. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  836. }
  837. }
  838. static void bfin_serial_reset_irda(struct uart_port *port)
  839. {
  840. int line = port->line;
  841. unsigned short val;
  842. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  843. val &= ~(IREN | RPOLC);
  844. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  845. SSYNC();
  846. val |= (IREN | RPOLC);
  847. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  848. SSYNC();
  849. }
  850. #ifdef CONFIG_CONSOLE_POLL
  851. /* Anomaly notes:
  852. * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
  853. * losing other bits of UART_LSR is not a problem here.
  854. */
  855. static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
  856. {
  857. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  858. while (!(UART_GET_LSR(uart) & THRE))
  859. cpu_relax();
  860. UART_CLEAR_DLAB(uart);
  861. UART_PUT_CHAR(uart, (unsigned char)chr);
  862. }
  863. static int bfin_serial_poll_get_char(struct uart_port *port)
  864. {
  865. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  866. unsigned char chr;
  867. while (!(UART_GET_LSR(uart) & DR))
  868. cpu_relax();
  869. UART_CLEAR_DLAB(uart);
  870. chr = UART_GET_CHAR(uart);
  871. return chr;
  872. }
  873. #endif
  874. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  875. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  876. static void bfin_kgdboc_port_shutdown(struct uart_port *port)
  877. {
  878. if (kgdboc_break_enabled) {
  879. kgdboc_break_enabled = 0;
  880. bfin_serial_shutdown(port);
  881. }
  882. }
  883. static int bfin_kgdboc_port_startup(struct uart_port *port)
  884. {
  885. kgdboc_port_line = port->line;
  886. kgdboc_break_enabled = !bfin_serial_startup(port);
  887. return 0;
  888. }
  889. #endif
  890. static struct uart_ops bfin_serial_pops = {
  891. .tx_empty = bfin_serial_tx_empty,
  892. .set_mctrl = bfin_serial_set_mctrl,
  893. .get_mctrl = bfin_serial_get_mctrl,
  894. .stop_tx = bfin_serial_stop_tx,
  895. .start_tx = bfin_serial_start_tx,
  896. .stop_rx = bfin_serial_stop_rx,
  897. .enable_ms = bfin_serial_enable_ms,
  898. .break_ctl = bfin_serial_break_ctl,
  899. .startup = bfin_serial_startup,
  900. .shutdown = bfin_serial_shutdown,
  901. .set_termios = bfin_serial_set_termios,
  902. .set_ldisc = bfin_serial_set_ldisc,
  903. .type = bfin_serial_type,
  904. .release_port = bfin_serial_release_port,
  905. .request_port = bfin_serial_request_port,
  906. .config_port = bfin_serial_config_port,
  907. .verify_port = bfin_serial_verify_port,
  908. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  909. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  910. .kgdboc_port_startup = bfin_kgdboc_port_startup,
  911. .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
  912. #endif
  913. #ifdef CONFIG_CONSOLE_POLL
  914. .poll_put_char = bfin_serial_poll_put_char,
  915. .poll_get_char = bfin_serial_poll_get_char,
  916. #endif
  917. };
  918. static void __init bfin_serial_hw_init(void)
  919. {
  920. #ifdef CONFIG_SERIAL_BFIN_UART0
  921. peripheral_request(P_UART0_TX, DRIVER_NAME);
  922. peripheral_request(P_UART0_RX, DRIVER_NAME);
  923. #endif
  924. #ifdef CONFIG_SERIAL_BFIN_UART1
  925. peripheral_request(P_UART1_TX, DRIVER_NAME);
  926. peripheral_request(P_UART1_RX, DRIVER_NAME);
  927. # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
  928. peripheral_request(P_UART1_RTS, DRIVER_NAME);
  929. peripheral_request(P_UART1_CTS, DRIVER_NAME);
  930. # endif
  931. #endif
  932. #ifdef CONFIG_SERIAL_BFIN_UART2
  933. peripheral_request(P_UART2_TX, DRIVER_NAME);
  934. peripheral_request(P_UART2_RX, DRIVER_NAME);
  935. #endif
  936. #ifdef CONFIG_SERIAL_BFIN_UART3
  937. peripheral_request(P_UART3_TX, DRIVER_NAME);
  938. peripheral_request(P_UART3_RX, DRIVER_NAME);
  939. # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
  940. peripheral_request(P_UART3_RTS, DRIVER_NAME);
  941. peripheral_request(P_UART3_CTS, DRIVER_NAME);
  942. # endif
  943. #endif
  944. }
  945. static void __init bfin_serial_init_ports(void)
  946. {
  947. static int first = 1;
  948. int i;
  949. if (!first)
  950. return;
  951. first = 0;
  952. bfin_serial_hw_init();
  953. for (i = 0; i < nr_active_ports; i++) {
  954. spin_lock_init(&bfin_serial_ports[i].port.lock);
  955. bfin_serial_ports[i].port.uartclk = get_sclk();
  956. bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
  957. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  958. bfin_serial_ports[i].port.line = i;
  959. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  960. bfin_serial_ports[i].port.membase =
  961. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  962. bfin_serial_ports[i].port.mapbase =
  963. bfin_serial_resource[i].uart_base_addr;
  964. bfin_serial_ports[i].port.irq =
  965. bfin_serial_resource[i].uart_irq;
  966. bfin_serial_ports[i].status_irq =
  967. bfin_serial_resource[i].uart_status_irq;
  968. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  969. #ifdef CONFIG_SERIAL_BFIN_DMA
  970. bfin_serial_ports[i].tx_done = 1;
  971. bfin_serial_ports[i].tx_count = 0;
  972. bfin_serial_ports[i].tx_dma_channel =
  973. bfin_serial_resource[i].uart_tx_dma_channel;
  974. bfin_serial_ports[i].rx_dma_channel =
  975. bfin_serial_resource[i].uart_rx_dma_channel;
  976. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  977. #endif
  978. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  979. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  980. bfin_serial_ports[i].cts_pin =
  981. bfin_serial_resource[i].uart_cts_pin;
  982. bfin_serial_ports[i].rts_pin =
  983. bfin_serial_resource[i].uart_rts_pin;
  984. #endif
  985. }
  986. }
  987. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  988. /*
  989. * If the port was already initialised (eg, by a boot loader),
  990. * try to determine the current setup.
  991. */
  992. static void __init
  993. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  994. int *parity, int *bits)
  995. {
  996. unsigned short status;
  997. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  998. if (status == (ERBFI | ETBEI)) {
  999. /* ok, the port was enabled */
  1000. u16 lcr, dlh, dll;
  1001. lcr = UART_GET_LCR(uart);
  1002. *parity = 'n';
  1003. if (lcr & PEN) {
  1004. if (lcr & EPS)
  1005. *parity = 'e';
  1006. else
  1007. *parity = 'o';
  1008. }
  1009. switch (lcr & 0x03) {
  1010. case 0: *bits = 5; break;
  1011. case 1: *bits = 6; break;
  1012. case 2: *bits = 7; break;
  1013. case 3: *bits = 8; break;
  1014. }
  1015. /* Set DLAB in LCR to Access DLL and DLH */
  1016. UART_SET_DLAB(uart);
  1017. dll = UART_GET_DLL(uart);
  1018. dlh = UART_GET_DLH(uart);
  1019. /* Clear DLAB in LCR to Access THR RBR IER */
  1020. UART_CLEAR_DLAB(uart);
  1021. *baud = get_sclk() / (16*(dll | dlh << 8));
  1022. }
  1023. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
  1024. }
  1025. static struct uart_driver bfin_serial_reg;
  1026. static int __init
  1027. bfin_serial_console_setup(struct console *co, char *options)
  1028. {
  1029. struct bfin_serial_port *uart;
  1030. int baud = 57600;
  1031. int bits = 8;
  1032. int parity = 'n';
  1033. # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  1034. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  1035. int flow = 'r';
  1036. # else
  1037. int flow = 'n';
  1038. # endif
  1039. /*
  1040. * Check whether an invalid uart number has been specified, and
  1041. * if so, search for the first available port that does have
  1042. * console support.
  1043. */
  1044. if (co->index == -1 || co->index >= nr_active_ports)
  1045. co->index = 0;
  1046. uart = &bfin_serial_ports[co->index];
  1047. if (options)
  1048. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1049. else
  1050. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  1051. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  1052. }
  1053. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  1054. defined (CONFIG_EARLY_PRINTK) */
  1055. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  1056. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  1057. {
  1058. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1059. while (!(UART_GET_LSR(uart) & THRE))
  1060. barrier();
  1061. UART_PUT_CHAR(uart, ch);
  1062. SSYNC();
  1063. }
  1064. /*
  1065. * Interrupts are disabled on entering
  1066. */
  1067. static void
  1068. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  1069. {
  1070. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&uart->port.lock, flags);
  1073. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  1074. spin_unlock_irqrestore(&uart->port.lock, flags);
  1075. }
  1076. static struct console bfin_serial_console = {
  1077. .name = BFIN_SERIAL_NAME,
  1078. .write = bfin_serial_console_write,
  1079. .device = uart_console_device,
  1080. .setup = bfin_serial_console_setup,
  1081. .flags = CON_PRINTBUFFER,
  1082. .index = -1,
  1083. .data = &bfin_serial_reg,
  1084. };
  1085. static int __init bfin_serial_rs_console_init(void)
  1086. {
  1087. bfin_serial_init_ports();
  1088. register_console(&bfin_serial_console);
  1089. return 0;
  1090. }
  1091. console_initcall(bfin_serial_rs_console_init);
  1092. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  1093. #else
  1094. #define BFIN_SERIAL_CONSOLE NULL
  1095. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  1096. #ifdef CONFIG_EARLY_PRINTK
  1097. static __init void early_serial_putc(struct uart_port *port, int ch)
  1098. {
  1099. unsigned timeout = 0xffff;
  1100. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1101. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  1102. cpu_relax();
  1103. UART_PUT_CHAR(uart, ch);
  1104. }
  1105. static __init void early_serial_write(struct console *con, const char *s,
  1106. unsigned int n)
  1107. {
  1108. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  1109. unsigned int i;
  1110. for (i = 0; i < n; i++, s++) {
  1111. if (*s == '\n')
  1112. early_serial_putc(&uart->port, '\r');
  1113. early_serial_putc(&uart->port, *s);
  1114. }
  1115. }
  1116. /*
  1117. * This should have a .setup or .early_setup in it, but then things get called
  1118. * without the command line options, and the baud rate gets messed up - so
  1119. * don't let the common infrastructure play with things. (see calls to setup
  1120. * & earlysetup in ./kernel/printk.c:register_console()
  1121. */
  1122. static struct __initdata console bfin_early_serial_console = {
  1123. .name = "early_BFuart",
  1124. .write = early_serial_write,
  1125. .device = uart_console_device,
  1126. .flags = CON_PRINTBUFFER,
  1127. .index = -1,
  1128. .data = &bfin_serial_reg,
  1129. };
  1130. struct console __init *bfin_earlyserial_init(unsigned int port,
  1131. unsigned int cflag)
  1132. {
  1133. struct bfin_serial_port *uart;
  1134. struct ktermios t;
  1135. if (port == -1 || port >= nr_active_ports)
  1136. port = 0;
  1137. bfin_serial_init_ports();
  1138. bfin_early_serial_console.index = port;
  1139. uart = &bfin_serial_ports[port];
  1140. t.c_cflag = cflag;
  1141. t.c_iflag = 0;
  1142. t.c_oflag = 0;
  1143. t.c_lflag = ICANON;
  1144. t.c_line = port;
  1145. bfin_serial_set_termios(&uart->port, &t, &t);
  1146. return &bfin_early_serial_console;
  1147. }
  1148. #endif /* CONFIG_EARLY_PRINTK */
  1149. static struct uart_driver bfin_serial_reg = {
  1150. .owner = THIS_MODULE,
  1151. .driver_name = "bfin-uart",
  1152. .dev_name = BFIN_SERIAL_NAME,
  1153. .major = BFIN_SERIAL_MAJOR,
  1154. .minor = BFIN_SERIAL_MINOR,
  1155. .nr = BFIN_UART_NR_PORTS,
  1156. .cons = BFIN_SERIAL_CONSOLE,
  1157. };
  1158. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1159. {
  1160. int i;
  1161. for (i = 0; i < nr_active_ports; i++) {
  1162. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1163. continue;
  1164. uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1165. }
  1166. return 0;
  1167. }
  1168. static int bfin_serial_resume(struct platform_device *dev)
  1169. {
  1170. int i;
  1171. for (i = 0; i < nr_active_ports; i++) {
  1172. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1173. continue;
  1174. uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1175. }
  1176. return 0;
  1177. }
  1178. static int bfin_serial_probe(struct platform_device *dev)
  1179. {
  1180. struct resource *res = dev->resource;
  1181. int i;
  1182. for (i = 0; i < dev->num_resources; i++, res++)
  1183. if (res->flags & IORESOURCE_MEM)
  1184. break;
  1185. if (i < dev->num_resources) {
  1186. for (i = 0; i < nr_active_ports; i++, res++) {
  1187. if (bfin_serial_ports[i].port.mapbase != res->start)
  1188. continue;
  1189. bfin_serial_ports[i].port.dev = &dev->dev;
  1190. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. static int bfin_serial_remove(struct platform_device *dev)
  1196. {
  1197. int i;
  1198. for (i = 0; i < nr_active_ports; i++) {
  1199. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1200. continue;
  1201. uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1202. bfin_serial_ports[i].port.dev = NULL;
  1203. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  1204. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  1205. gpio_free(bfin_serial_ports[i].cts_pin);
  1206. gpio_free(bfin_serial_ports[i].rts_pin);
  1207. #endif
  1208. }
  1209. return 0;
  1210. }
  1211. static struct platform_driver bfin_serial_driver = {
  1212. .probe = bfin_serial_probe,
  1213. .remove = bfin_serial_remove,
  1214. .suspend = bfin_serial_suspend,
  1215. .resume = bfin_serial_resume,
  1216. .driver = {
  1217. .name = "bfin-uart",
  1218. .owner = THIS_MODULE,
  1219. },
  1220. };
  1221. static int __init bfin_serial_init(void)
  1222. {
  1223. int ret;
  1224. pr_info("Serial: Blackfin serial driver\n");
  1225. bfin_serial_init_ports();
  1226. ret = uart_register_driver(&bfin_serial_reg);
  1227. if (ret == 0) {
  1228. ret = platform_driver_register(&bfin_serial_driver);
  1229. if (ret) {
  1230. pr_debug("uart register failed\n");
  1231. uart_unregister_driver(&bfin_serial_reg);
  1232. }
  1233. }
  1234. return ret;
  1235. }
  1236. static void __exit bfin_serial_exit(void)
  1237. {
  1238. platform_driver_unregister(&bfin_serial_driver);
  1239. uart_unregister_driver(&bfin_serial_reg);
  1240. }
  1241. module_init(bfin_serial_init);
  1242. module_exit(bfin_serial_exit);
  1243. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1244. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1245. MODULE_LICENSE("GPL");
  1246. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1247. MODULE_ALIAS("platform:bfin-uart");