bfin_uart.c 39 KB

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