bfin_uart.c 39 KB

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