bfin_5xx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. UART_CLEAR_IER(uart, ETBEI);
  280. return;
  281. }
  282. if (uart->port.x_char) {
  283. UART_PUT_CHAR(uart, uart->port.x_char);
  284. uart->port.icount.tx++;
  285. uart->port.x_char = 0;
  286. }
  287. while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
  288. UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
  289. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  290. uart->port.icount.tx++;
  291. SSYNC();
  292. }
  293. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  294. uart_write_wakeup(&uart->port);
  295. }
  296. static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
  297. {
  298. struct bfin_serial_port *uart = dev_id;
  299. spin_lock(&uart->port.lock);
  300. while (UART_GET_LSR(uart) & DR)
  301. bfin_serial_rx_chars(uart);
  302. spin_unlock(&uart->port.lock);
  303. return IRQ_HANDLED;
  304. }
  305. static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
  306. {
  307. struct bfin_serial_port *uart = dev_id;
  308. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  309. if (uart->scts && (!bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
  310. uart->scts = 0;
  311. uart_handle_cts_change(&uart->port, uart->scts);
  312. }
  313. #endif
  314. spin_lock(&uart->port.lock);
  315. if (UART_GET_LSR(uart) & THRE)
  316. bfin_serial_tx_chars(uart);
  317. spin_unlock(&uart->port.lock);
  318. return IRQ_HANDLED;
  319. }
  320. #endif
  321. #ifdef CONFIG_SERIAL_BFIN_DMA
  322. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
  323. {
  324. struct circ_buf *xmit = &uart->port.info->xmit;
  325. uart->tx_done = 0;
  326. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  327. uart->tx_count = 0;
  328. uart->tx_done = 1;
  329. return;
  330. }
  331. if (uart->port.x_char) {
  332. UART_PUT_CHAR(uart, uart->port.x_char);
  333. uart->port.icount.tx++;
  334. uart->port.x_char = 0;
  335. }
  336. uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
  337. if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
  338. uart->tx_count = UART_XMIT_SIZE - xmit->tail;
  339. blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
  340. (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
  341. set_dma_config(uart->tx_dma_channel,
  342. set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
  343. INTR_ON_BUF,
  344. DIMENSION_LINEAR,
  345. DATA_SIZE_8,
  346. DMA_SYNC_RESTART));
  347. set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
  348. set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
  349. set_dma_x_modify(uart->tx_dma_channel, 1);
  350. enable_dma(uart->tx_dma_channel);
  351. UART_SET_IER(uart, ETBEI);
  352. }
  353. static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
  354. {
  355. struct tty_struct *tty = uart->port.info->port.tty;
  356. int i, flg, status;
  357. status = UART_GET_LSR(uart);
  358. UART_CLEAR_LSR(uart);
  359. uart->port.icount.rx +=
  360. CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
  361. UART_XMIT_SIZE);
  362. if (status & BI) {
  363. uart->port.icount.brk++;
  364. if (uart_handle_break(&uart->port))
  365. goto dma_ignore_char;
  366. status &= ~(PE | FE);
  367. }
  368. if (status & PE)
  369. uart->port.icount.parity++;
  370. if (status & OE)
  371. uart->port.icount.overrun++;
  372. if (status & FE)
  373. uart->port.icount.frame++;
  374. status &= uart->port.read_status_mask;
  375. if (status & BI)
  376. flg = TTY_BREAK;
  377. else if (status & PE)
  378. flg = TTY_PARITY;
  379. else if (status & FE)
  380. flg = TTY_FRAME;
  381. else
  382. flg = TTY_NORMAL;
  383. for (i = uart->rx_dma_buf.tail; ; i++) {
  384. if (i >= UART_XMIT_SIZE)
  385. i = 0;
  386. if (i == uart->rx_dma_buf.head)
  387. break;
  388. if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
  389. uart_insert_char(&uart->port, status, OE,
  390. uart->rx_dma_buf.buf[i], flg);
  391. }
  392. dma_ignore_char:
  393. tty_flip_buffer_push(tty);
  394. }
  395. void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
  396. {
  397. int x_pos, pos;
  398. unsigned long flags;
  399. spin_lock_irqsave(&uart->port.lock, flags);
  400. uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
  401. x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
  402. uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
  403. if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
  404. uart->rx_dma_nrows = 0;
  405. x_pos = DMA_RX_XCOUNT - x_pos;
  406. if (x_pos == DMA_RX_XCOUNT)
  407. x_pos = 0;
  408. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  409. if (pos != uart->rx_dma_buf.tail) {
  410. uart->rx_dma_buf.head = pos;
  411. bfin_serial_dma_rx_chars(uart);
  412. uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
  413. }
  414. spin_unlock_irqrestore(&uart->port.lock, flags);
  415. mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
  416. }
  417. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  418. {
  419. struct bfin_serial_port *uart = dev_id;
  420. struct circ_buf *xmit = &uart->port.info->xmit;
  421. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  422. if (uart->scts && (!bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
  423. uart->scts = 0;
  424. uart_handle_cts_change(&uart->port, uart->scts);
  425. }
  426. #endif
  427. spin_lock(&uart->port.lock);
  428. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  429. disable_dma(uart->tx_dma_channel);
  430. clear_dma_irqstat(uart->tx_dma_channel);
  431. UART_CLEAR_IER(uart, ETBEI);
  432. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  433. uart->port.icount.tx += uart->tx_count;
  434. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  435. uart_write_wakeup(&uart->port);
  436. bfin_serial_dma_tx_chars(uart);
  437. }
  438. spin_unlock(&uart->port.lock);
  439. return IRQ_HANDLED;
  440. }
  441. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  442. {
  443. struct bfin_serial_port *uart = dev_id;
  444. unsigned short irqstat;
  445. spin_lock(&uart->port.lock);
  446. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  447. clear_dma_irqstat(uart->rx_dma_channel);
  448. bfin_serial_dma_rx_chars(uart);
  449. spin_unlock(&uart->port.lock);
  450. return IRQ_HANDLED;
  451. }
  452. #endif
  453. /*
  454. * Return TIOCSER_TEMT when transmitter is not busy.
  455. */
  456. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  457. {
  458. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  459. unsigned short lsr;
  460. lsr = UART_GET_LSR(uart);
  461. if (lsr & TEMT)
  462. return TIOCSER_TEMT;
  463. else
  464. return 0;
  465. }
  466. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  467. {
  468. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  469. u16 lcr = UART_GET_LCR(uart);
  470. if (break_state)
  471. lcr |= SB;
  472. else
  473. lcr &= ~SB;
  474. UART_PUT_LCR(uart, lcr);
  475. SSYNC();
  476. }
  477. static int bfin_serial_startup(struct uart_port *port)
  478. {
  479. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  480. #ifdef CONFIG_SERIAL_BFIN_DMA
  481. dma_addr_t dma_handle;
  482. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  483. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  484. return -EBUSY;
  485. }
  486. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  487. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  488. free_dma(uart->rx_dma_channel);
  489. return -EBUSY;
  490. }
  491. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  492. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  493. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  494. uart->rx_dma_buf.head = 0;
  495. uart->rx_dma_buf.tail = 0;
  496. uart->rx_dma_nrows = 0;
  497. set_dma_config(uart->rx_dma_channel,
  498. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  499. INTR_ON_ROW, DIMENSION_2D,
  500. DATA_SIZE_8,
  501. DMA_SYNC_RESTART));
  502. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  503. set_dma_x_modify(uart->rx_dma_channel, 1);
  504. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  505. set_dma_y_modify(uart->rx_dma_channel, 1);
  506. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  507. enable_dma(uart->rx_dma_channel);
  508. uart->rx_dma_timer.data = (unsigned long)(uart);
  509. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  510. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  511. add_timer(&(uart->rx_dma_timer));
  512. #else
  513. # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  514. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  515. if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
  516. kgdboc_break_enabled = 0;
  517. else {
  518. # endif
  519. if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  520. "BFIN_UART_RX", uart)) {
  521. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  522. return -EBUSY;
  523. }
  524. if (request_irq
  525. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  526. "BFIN_UART_TX", uart)) {
  527. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  528. free_irq(uart->port.irq, uart);
  529. return -EBUSY;
  530. }
  531. # ifdef CONFIG_BF54x
  532. {
  533. unsigned uart_dma_ch_rx, uart_dma_ch_tx;
  534. switch (uart->port.irq) {
  535. case IRQ_UART3_RX:
  536. uart_dma_ch_rx = CH_UART3_RX;
  537. uart_dma_ch_tx = CH_UART3_TX;
  538. break;
  539. case IRQ_UART2_RX:
  540. uart_dma_ch_rx = CH_UART2_RX;
  541. uart_dma_ch_tx = CH_UART2_TX;
  542. break;
  543. default:
  544. uart_dma_ch_rx = uart_dma_ch_tx = 0;
  545. break;
  546. };
  547. if (uart_dma_ch_rx &&
  548. request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
  549. printk(KERN_NOTICE"Fail to attach UART interrupt\n");
  550. free_irq(uart->port.irq, uart);
  551. free_irq(uart->port.irq + 1, uart);
  552. return -EBUSY;
  553. }
  554. if (uart_dma_ch_tx &&
  555. request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
  556. printk(KERN_NOTICE "Fail to attach UART interrupt\n");
  557. free_dma(uart_dma_ch_rx);
  558. free_irq(uart->port.irq, uart);
  559. free_irq(uart->port.irq + 1, uart);
  560. return -EBUSY;
  561. }
  562. }
  563. # endif
  564. # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  565. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  566. }
  567. # endif
  568. #endif
  569. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  570. if (uart->cts_pin >= 0) {
  571. if (request_irq(gpio_to_irq(uart->cts_pin),
  572. bfin_serial_mctrl_cts_int,
  573. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  574. IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
  575. uart->cts_pin = -1;
  576. pr_info("Unable to attach BlackFin UART CTS interrupt.\
  577. So, disable it.\n");
  578. }
  579. }
  580. if (uart->rts_pin >= 0) {
  581. gpio_request(uart->rts_pin, DRIVER_NAME);
  582. gpio_direction_output(uart->rts_pin, 0);
  583. }
  584. #endif
  585. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  586. if (request_irq(uart->status_irq,
  587. bfin_serial_mctrl_cts_int,
  588. IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
  589. pr_info("Unable to attach BlackFin UART Modem \
  590. Status interrupt.\n");
  591. }
  592. if (uart->cts_pin >= 0) {
  593. gpio_request(uart->cts_pin, DRIVER_NAME);
  594. gpio_direction_output(uart->cts_pin, 1);
  595. }
  596. if (uart->rts_pin >= 0) {
  597. gpio_request(uart->rts_pin, DRIVER_NAME);
  598. gpio_direction_output(uart->rts_pin, 0);
  599. }
  600. /* CTS RTS PINs are negative assertive. */
  601. UART_PUT_MCR(uart, ACTS);
  602. UART_SET_IER(uart, EDSSI);
  603. #endif
  604. UART_SET_IER(uart, ERBFI);
  605. return 0;
  606. }
  607. static void bfin_serial_shutdown(struct uart_port *port)
  608. {
  609. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  610. #ifdef CONFIG_SERIAL_BFIN_DMA
  611. disable_dma(uart->tx_dma_channel);
  612. free_dma(uart->tx_dma_channel);
  613. disable_dma(uart->rx_dma_channel);
  614. free_dma(uart->rx_dma_channel);
  615. del_timer(&(uart->rx_dma_timer));
  616. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  617. #else
  618. #ifdef CONFIG_BF54x
  619. switch (uart->port.irq) {
  620. case IRQ_UART3_RX:
  621. free_dma(CH_UART3_RX);
  622. free_dma(CH_UART3_TX);
  623. break;
  624. case IRQ_UART2_RX:
  625. free_dma(CH_UART2_RX);
  626. free_dma(CH_UART2_TX);
  627. break;
  628. default:
  629. break;
  630. };
  631. #endif
  632. free_irq(uart->port.irq, uart);
  633. free_irq(uart->port.irq+1, uart);
  634. #endif
  635. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  636. if (uart->cts_pin >= 0)
  637. free_irq(gpio_to_irq(uart->cts_pin), uart);
  638. if (uart->rts_pin >= 0)
  639. gpio_free(uart->rts_pin);
  640. #endif
  641. #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
  642. if (uart->cts_pin >= 0)
  643. gpio_free(uart->cts_pin);
  644. if (uart->rts_pin >= 0)
  645. gpio_free(uart->rts_pin);
  646. if (UART_GET_IER(uart) && EDSSI)
  647. free_irq(uart->status_irq, uart);
  648. #endif
  649. }
  650. static void
  651. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  652. struct ktermios *old)
  653. {
  654. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  655. unsigned long flags;
  656. unsigned int baud, quot;
  657. unsigned short val, ier, lcr = 0;
  658. switch (termios->c_cflag & CSIZE) {
  659. case CS8:
  660. lcr = WLS(8);
  661. break;
  662. case CS7:
  663. lcr = WLS(7);
  664. break;
  665. case CS6:
  666. lcr = WLS(6);
  667. break;
  668. case CS5:
  669. lcr = WLS(5);
  670. break;
  671. default:
  672. printk(KERN_ERR "%s: word lengh not supported\n",
  673. __func__);
  674. }
  675. if (termios->c_cflag & CSTOPB)
  676. lcr |= STB;
  677. if (termios->c_cflag & PARENB)
  678. lcr |= PEN;
  679. if (!(termios->c_cflag & PARODD))
  680. lcr |= EPS;
  681. if (termios->c_cflag & CMSPAR)
  682. lcr |= STP;
  683. port->read_status_mask = OE;
  684. if (termios->c_iflag & INPCK)
  685. port->read_status_mask |= (FE | PE);
  686. if (termios->c_iflag & (BRKINT | PARMRK))
  687. port->read_status_mask |= BI;
  688. /*
  689. * Characters to ignore
  690. */
  691. port->ignore_status_mask = 0;
  692. if (termios->c_iflag & IGNPAR)
  693. port->ignore_status_mask |= FE | PE;
  694. if (termios->c_iflag & IGNBRK) {
  695. port->ignore_status_mask |= BI;
  696. /*
  697. * If we're ignoring parity and break indicators,
  698. * ignore overruns too (for real raw support).
  699. */
  700. if (termios->c_iflag & IGNPAR)
  701. port->ignore_status_mask |= OE;
  702. }
  703. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  704. quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
  705. spin_lock_irqsave(&uart->port.lock, flags);
  706. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  707. /* Disable UART */
  708. ier = UART_GET_IER(uart);
  709. UART_DISABLE_INTS(uart);
  710. /* Set DLAB in LCR to Access DLL and DLH */
  711. UART_SET_DLAB(uart);
  712. UART_PUT_DLL(uart, quot & 0xFF);
  713. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  714. SSYNC();
  715. /* Clear DLAB in LCR to Access THR RBR IER */
  716. UART_CLEAR_DLAB(uart);
  717. UART_PUT_LCR(uart, lcr);
  718. /* Enable UART */
  719. UART_ENABLE_INTS(uart, ier);
  720. val = UART_GET_GCTL(uart);
  721. val |= UCEN;
  722. UART_PUT_GCTL(uart, val);
  723. /* Port speed changed, update the per-port timeout. */
  724. uart_update_timeout(port, termios->c_cflag, baud);
  725. spin_unlock_irqrestore(&uart->port.lock, flags);
  726. }
  727. static const char *bfin_serial_type(struct uart_port *port)
  728. {
  729. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  730. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  731. }
  732. /*
  733. * Release the memory region(s) being used by 'port'.
  734. */
  735. static void bfin_serial_release_port(struct uart_port *port)
  736. {
  737. }
  738. /*
  739. * Request the memory region(s) being used by 'port'.
  740. */
  741. static int bfin_serial_request_port(struct uart_port *port)
  742. {
  743. return 0;
  744. }
  745. /*
  746. * Configure/autoconfigure the port.
  747. */
  748. static void bfin_serial_config_port(struct uart_port *port, int flags)
  749. {
  750. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  751. if (flags & UART_CONFIG_TYPE &&
  752. bfin_serial_request_port(&uart->port) == 0)
  753. uart->port.type = PORT_BFIN;
  754. }
  755. /*
  756. * Verify the new serial_struct (for TIOCSSERIAL).
  757. * The only change we allow are to the flags and type, and
  758. * even then only between PORT_BFIN and PORT_UNKNOWN
  759. */
  760. static int
  761. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  762. {
  763. return 0;
  764. }
  765. /*
  766. * Enable the IrDA function if tty->ldisc.num is N_IRDA.
  767. * In other cases, disable IrDA function.
  768. */
  769. static void bfin_serial_set_ldisc(struct uart_port *port)
  770. {
  771. int line = port->line;
  772. unsigned short val;
  773. if (line >= port->info->port.tty->driver->num)
  774. return;
  775. switch (port->info->port.tty->termios->c_line) {
  776. case N_IRDA:
  777. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  778. val |= (IREN | RPOLC);
  779. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  780. break;
  781. default:
  782. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  783. val &= ~(IREN | RPOLC);
  784. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  785. }
  786. }
  787. static void bfin_serial_reset_irda(struct uart_port *port)
  788. {
  789. int line = port->line;
  790. unsigned short val;
  791. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  792. val &= ~(IREN | RPOLC);
  793. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  794. SSYNC();
  795. val |= (IREN | RPOLC);
  796. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  797. SSYNC();
  798. }
  799. #ifdef CONFIG_CONSOLE_POLL
  800. static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
  801. {
  802. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  803. while (!(UART_GET_LSR(uart) & THRE))
  804. cpu_relax();
  805. UART_CLEAR_DLAB(uart);
  806. UART_PUT_CHAR(uart, (unsigned char)chr);
  807. }
  808. static int bfin_serial_poll_get_char(struct uart_port *port)
  809. {
  810. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  811. unsigned char chr;
  812. while (!(UART_GET_LSR(uart) & DR))
  813. cpu_relax();
  814. UART_CLEAR_DLAB(uart);
  815. chr = UART_GET_CHAR(uart);
  816. return chr;
  817. }
  818. #endif
  819. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  820. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  821. static void bfin_kgdboc_port_shutdown(struct uart_port *port)
  822. {
  823. if (kgdboc_break_enabled) {
  824. kgdboc_break_enabled = 0;
  825. bfin_serial_shutdown(port);
  826. }
  827. }
  828. static int bfin_kgdboc_port_startup(struct uart_port *port)
  829. {
  830. kgdboc_port_line = port->line;
  831. kgdboc_break_enabled = !bfin_serial_startup(port);
  832. return 0;
  833. }
  834. #endif
  835. static struct uart_ops bfin_serial_pops = {
  836. .tx_empty = bfin_serial_tx_empty,
  837. .set_mctrl = bfin_serial_set_mctrl,
  838. .get_mctrl = bfin_serial_get_mctrl,
  839. .stop_tx = bfin_serial_stop_tx,
  840. .start_tx = bfin_serial_start_tx,
  841. .stop_rx = bfin_serial_stop_rx,
  842. .enable_ms = bfin_serial_enable_ms,
  843. .break_ctl = bfin_serial_break_ctl,
  844. .startup = bfin_serial_startup,
  845. .shutdown = bfin_serial_shutdown,
  846. .set_termios = bfin_serial_set_termios,
  847. .set_ldisc = bfin_serial_set_ldisc,
  848. .type = bfin_serial_type,
  849. .release_port = bfin_serial_release_port,
  850. .request_port = bfin_serial_request_port,
  851. .config_port = bfin_serial_config_port,
  852. .verify_port = bfin_serial_verify_port,
  853. #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
  854. defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
  855. .kgdboc_port_startup = bfin_kgdboc_port_startup,
  856. .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
  857. #endif
  858. #ifdef CONFIG_CONSOLE_POLL
  859. .poll_put_char = bfin_serial_poll_put_char,
  860. .poll_get_char = bfin_serial_poll_get_char,
  861. #endif
  862. };
  863. static void __init bfin_serial_hw_init(void)
  864. {
  865. #ifdef CONFIG_SERIAL_BFIN_UART0
  866. peripheral_request(P_UART0_TX, DRIVER_NAME);
  867. peripheral_request(P_UART0_RX, DRIVER_NAME);
  868. #endif
  869. #ifdef CONFIG_SERIAL_BFIN_UART1
  870. peripheral_request(P_UART1_TX, DRIVER_NAME);
  871. peripheral_request(P_UART1_RX, DRIVER_NAME);
  872. # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
  873. peripheral_request(P_UART1_RTS, DRIVER_NAME);
  874. peripheral_request(P_UART1_CTS, DRIVER_NAME);
  875. # endif
  876. #endif
  877. #ifdef CONFIG_SERIAL_BFIN_UART2
  878. peripheral_request(P_UART2_TX, DRIVER_NAME);
  879. peripheral_request(P_UART2_RX, DRIVER_NAME);
  880. #endif
  881. #ifdef CONFIG_SERIAL_BFIN_UART3
  882. peripheral_request(P_UART3_TX, DRIVER_NAME);
  883. peripheral_request(P_UART3_RX, DRIVER_NAME);
  884. # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
  885. peripheral_request(P_UART3_RTS, DRIVER_NAME);
  886. peripheral_request(P_UART3_CTS, DRIVER_NAME);
  887. # endif
  888. #endif
  889. }
  890. static void __init bfin_serial_init_ports(void)
  891. {
  892. static int first = 1;
  893. int i;
  894. if (!first)
  895. return;
  896. first = 0;
  897. bfin_serial_hw_init();
  898. for (i = 0; i < nr_active_ports; i++) {
  899. bfin_serial_ports[i].port.uartclk = get_sclk();
  900. bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
  901. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  902. bfin_serial_ports[i].port.line = i;
  903. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  904. bfin_serial_ports[i].port.membase =
  905. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  906. bfin_serial_ports[i].port.mapbase =
  907. bfin_serial_resource[i].uart_base_addr;
  908. bfin_serial_ports[i].port.irq =
  909. bfin_serial_resource[i].uart_irq;
  910. bfin_serial_ports[i].status_irq =
  911. bfin_serial_resource[i].uart_status_irq;
  912. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  913. #ifdef CONFIG_SERIAL_BFIN_DMA
  914. bfin_serial_ports[i].tx_done = 1;
  915. bfin_serial_ports[i].tx_count = 0;
  916. bfin_serial_ports[i].tx_dma_channel =
  917. bfin_serial_resource[i].uart_tx_dma_channel;
  918. bfin_serial_ports[i].rx_dma_channel =
  919. bfin_serial_resource[i].uart_rx_dma_channel;
  920. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  921. #endif
  922. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  923. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  924. bfin_serial_ports[i].cts_pin =
  925. bfin_serial_resource[i].uart_cts_pin;
  926. bfin_serial_ports[i].rts_pin =
  927. bfin_serial_resource[i].uart_rts_pin;
  928. #endif
  929. }
  930. }
  931. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  932. /*
  933. * If the port was already initialised (eg, by a boot loader),
  934. * try to determine the current setup.
  935. */
  936. static void __init
  937. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  938. int *parity, int *bits)
  939. {
  940. unsigned short status;
  941. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  942. if (status == (ERBFI | ETBEI)) {
  943. /* ok, the port was enabled */
  944. u16 lcr, dlh, dll;
  945. lcr = UART_GET_LCR(uart);
  946. *parity = 'n';
  947. if (lcr & PEN) {
  948. if (lcr & EPS)
  949. *parity = 'e';
  950. else
  951. *parity = 'o';
  952. }
  953. switch (lcr & 0x03) {
  954. case 0: *bits = 5; break;
  955. case 1: *bits = 6; break;
  956. case 2: *bits = 7; break;
  957. case 3: *bits = 8; break;
  958. }
  959. /* Set DLAB in LCR to Access DLL and DLH */
  960. UART_SET_DLAB(uart);
  961. dll = UART_GET_DLL(uart);
  962. dlh = UART_GET_DLH(uart);
  963. /* Clear DLAB in LCR to Access THR RBR IER */
  964. UART_CLEAR_DLAB(uart);
  965. *baud = get_sclk() / (16*(dll | dlh << 8));
  966. }
  967. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
  968. }
  969. static struct uart_driver bfin_serial_reg;
  970. static int __init
  971. bfin_serial_console_setup(struct console *co, char *options)
  972. {
  973. struct bfin_serial_port *uart;
  974. int baud = 57600;
  975. int bits = 8;
  976. int parity = 'n';
  977. # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  978. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  979. int flow = 'r';
  980. # else
  981. int flow = 'n';
  982. # endif
  983. /*
  984. * Check whether an invalid uart number has been specified, and
  985. * if so, search for the first available port that does have
  986. * console support.
  987. */
  988. if (co->index == -1 || co->index >= nr_active_ports)
  989. co->index = 0;
  990. uart = &bfin_serial_ports[co->index];
  991. if (options)
  992. uart_parse_options(options, &baud, &parity, &bits, &flow);
  993. else
  994. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  995. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  996. }
  997. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  998. defined (CONFIG_EARLY_PRINTK) */
  999. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  1000. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  1001. {
  1002. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1003. while (!(UART_GET_LSR(uart) & THRE))
  1004. barrier();
  1005. UART_PUT_CHAR(uart, ch);
  1006. SSYNC();
  1007. }
  1008. /*
  1009. * Interrupts are disabled on entering
  1010. */
  1011. static void
  1012. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  1013. {
  1014. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  1015. unsigned long flags;
  1016. spin_lock_irqsave(&uart->port.lock, flags);
  1017. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  1018. spin_unlock_irqrestore(&uart->port.lock, flags);
  1019. }
  1020. static struct console bfin_serial_console = {
  1021. .name = BFIN_SERIAL_NAME,
  1022. .write = bfin_serial_console_write,
  1023. .device = uart_console_device,
  1024. .setup = bfin_serial_console_setup,
  1025. .flags = CON_PRINTBUFFER,
  1026. .index = -1,
  1027. .data = &bfin_serial_reg,
  1028. };
  1029. static int __init bfin_serial_rs_console_init(void)
  1030. {
  1031. bfin_serial_init_ports();
  1032. register_console(&bfin_serial_console);
  1033. return 0;
  1034. }
  1035. console_initcall(bfin_serial_rs_console_init);
  1036. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  1037. #else
  1038. #define BFIN_SERIAL_CONSOLE NULL
  1039. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  1040. #ifdef CONFIG_EARLY_PRINTK
  1041. static __init void early_serial_putc(struct uart_port *port, int ch)
  1042. {
  1043. unsigned timeout = 0xffff;
  1044. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  1045. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  1046. cpu_relax();
  1047. UART_PUT_CHAR(uart, ch);
  1048. }
  1049. static __init void early_serial_write(struct console *con, const char *s,
  1050. unsigned int n)
  1051. {
  1052. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  1053. unsigned int i;
  1054. for (i = 0; i < n; i++, s++) {
  1055. if (*s == '\n')
  1056. early_serial_putc(&uart->port, '\r');
  1057. early_serial_putc(&uart->port, *s);
  1058. }
  1059. }
  1060. static struct __initdata console bfin_early_serial_console = {
  1061. .name = "early_BFuart",
  1062. .write = early_serial_write,
  1063. .device = uart_console_device,
  1064. .flags = CON_PRINTBUFFER,
  1065. .setup = bfin_serial_console_setup,
  1066. .index = -1,
  1067. .data = &bfin_serial_reg,
  1068. };
  1069. struct console __init *bfin_earlyserial_init(unsigned int port,
  1070. unsigned int cflag)
  1071. {
  1072. struct bfin_serial_port *uart;
  1073. struct ktermios t;
  1074. if (port == -1 || port >= nr_active_ports)
  1075. port = 0;
  1076. bfin_serial_init_ports();
  1077. bfin_early_serial_console.index = port;
  1078. uart = &bfin_serial_ports[port];
  1079. t.c_cflag = cflag;
  1080. t.c_iflag = 0;
  1081. t.c_oflag = 0;
  1082. t.c_lflag = ICANON;
  1083. t.c_line = port;
  1084. bfin_serial_set_termios(&uart->port, &t, &t);
  1085. return &bfin_early_serial_console;
  1086. }
  1087. #endif /* CONFIG_EARLY_PRINTK */
  1088. static struct uart_driver bfin_serial_reg = {
  1089. .owner = THIS_MODULE,
  1090. .driver_name = "bfin-uart",
  1091. .dev_name = BFIN_SERIAL_NAME,
  1092. .major = BFIN_SERIAL_MAJOR,
  1093. .minor = BFIN_SERIAL_MINOR,
  1094. .nr = BFIN_UART_NR_PORTS,
  1095. .cons = BFIN_SERIAL_CONSOLE,
  1096. };
  1097. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1098. {
  1099. int i;
  1100. for (i = 0; i < nr_active_ports; i++) {
  1101. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1102. continue;
  1103. uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1104. }
  1105. return 0;
  1106. }
  1107. static int bfin_serial_resume(struct platform_device *dev)
  1108. {
  1109. int i;
  1110. for (i = 0; i < nr_active_ports; i++) {
  1111. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1112. continue;
  1113. uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1114. }
  1115. return 0;
  1116. }
  1117. static int bfin_serial_probe(struct platform_device *dev)
  1118. {
  1119. struct resource *res = dev->resource;
  1120. int i;
  1121. for (i = 0; i < dev->num_resources; i++, res++)
  1122. if (res->flags & IORESOURCE_MEM)
  1123. break;
  1124. if (i < dev->num_resources) {
  1125. for (i = 0; i < nr_active_ports; i++, res++) {
  1126. if (bfin_serial_ports[i].port.mapbase != res->start)
  1127. continue;
  1128. bfin_serial_ports[i].port.dev = &dev->dev;
  1129. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. static int bfin_serial_remove(struct platform_device *dev)
  1135. {
  1136. int i;
  1137. for (i = 0; i < nr_active_ports; i++) {
  1138. if (bfin_serial_ports[i].port.dev != &dev->dev)
  1139. continue;
  1140. uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1141. bfin_serial_ports[i].port.dev = NULL;
  1142. #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
  1143. defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
  1144. gpio_free(bfin_serial_ports[i].cts_pin);
  1145. gpio_free(bfin_serial_ports[i].rts_pin);
  1146. #endif
  1147. }
  1148. return 0;
  1149. }
  1150. static struct platform_driver bfin_serial_driver = {
  1151. .probe = bfin_serial_probe,
  1152. .remove = bfin_serial_remove,
  1153. .suspend = bfin_serial_suspend,
  1154. .resume = bfin_serial_resume,
  1155. .driver = {
  1156. .name = "bfin-uart",
  1157. .owner = THIS_MODULE,
  1158. },
  1159. };
  1160. static int __init bfin_serial_init(void)
  1161. {
  1162. int ret;
  1163. pr_info("Serial: Blackfin serial driver\n");
  1164. bfin_serial_init_ports();
  1165. ret = uart_register_driver(&bfin_serial_reg);
  1166. if (ret == 0) {
  1167. ret = platform_driver_register(&bfin_serial_driver);
  1168. if (ret) {
  1169. pr_debug("uart register failed\n");
  1170. uart_unregister_driver(&bfin_serial_reg);
  1171. }
  1172. }
  1173. return ret;
  1174. }
  1175. static void __exit bfin_serial_exit(void)
  1176. {
  1177. platform_driver_unregister(&bfin_serial_driver);
  1178. uart_unregister_driver(&bfin_serial_reg);
  1179. }
  1180. module_init(bfin_serial_init);
  1181. module_exit(bfin_serial_exit);
  1182. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1183. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1184. MODULE_LICENSE("GPL");
  1185. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1186. MODULE_ALIAS("platform:bfin-uart");