bfin_5xx.c 37 KB

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