bfin_5xx.c 37 KB

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