bfin_uart.c 39 KB

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