bfin_5xx.c 40 KB

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