bfin_uart.c 40 KB

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