bfin_5xx.c 36 KB

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