bfin_5xx.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /*
  2. * Blackfin On-Chip Serial Driver
  3. *
  4. * Copyright 2006-2007 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. #include <linux/module.h>
  14. #include <linux/ioport.h>
  15. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/sysrq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/serial_core.h>
  22. #ifdef CONFIG_KGDB_UART
  23. #include <linux/kgdb.h>
  24. #include <asm/irq_regs.h>
  25. #endif
  26. #include <asm/gpio.h>
  27. #include <asm/mach/bfin_serial_5xx.h>
  28. #ifdef CONFIG_SERIAL_BFIN_DMA
  29. #include <linux/dma-mapping.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/cacheflush.h>
  33. #endif
  34. /* UART name and device definitions */
  35. #define BFIN_SERIAL_NAME "ttyBF"
  36. #define BFIN_SERIAL_MAJOR 204
  37. #define BFIN_SERIAL_MINOR 64
  38. /*
  39. * Setup for console. Argument comes from the menuconfig
  40. */
  41. #define DMA_RX_XCOUNT 512
  42. #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
  43. #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
  44. #ifdef CONFIG_SERIAL_BFIN_DMA
  45. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
  46. #else
  47. static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
  48. #endif
  49. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
  50. /*
  51. * interrupts are disabled on entry
  52. */
  53. static void bfin_serial_stop_tx(struct uart_port *port)
  54. {
  55. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  56. struct circ_buf *xmit = &uart->port.info->xmit;
  57. #if !defined(CONFIG_BF54x) && !defined(CONFIG_SERIAL_BFIN_DMA)
  58. unsigned short ier;
  59. #endif
  60. while (!(UART_GET_LSR(uart) & TEMT))
  61. cpu_relax();
  62. #ifdef CONFIG_SERIAL_BFIN_DMA
  63. disable_dma(uart->tx_dma_channel);
  64. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  65. uart->port.icount.tx += uart->tx_count;
  66. uart->tx_count = 0;
  67. uart->tx_done = 1;
  68. #else
  69. #ifdef CONFIG_BF54x
  70. /* Clear TFI bit */
  71. UART_PUT_LSR(uart, TFI);
  72. UART_CLEAR_IER(uart, ETBEI);
  73. #else
  74. ier = UART_GET_IER(uart);
  75. ier &= ~ETBEI;
  76. UART_PUT_IER(uart, ier);
  77. #endif
  78. #endif
  79. }
  80. /*
  81. * port is locked and interrupts are disabled
  82. */
  83. static void bfin_serial_start_tx(struct uart_port *port)
  84. {
  85. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  86. #ifdef CONFIG_SERIAL_BFIN_DMA
  87. if (uart->tx_done)
  88. bfin_serial_dma_tx_chars(uart);
  89. #else
  90. #ifdef CONFIG_BF54x
  91. UART_SET_IER(uart, ETBEI);
  92. #else
  93. unsigned short ier;
  94. ier = UART_GET_IER(uart);
  95. ier |= ETBEI;
  96. UART_PUT_IER(uart, ier);
  97. #endif
  98. bfin_serial_tx_chars(uart);
  99. #endif
  100. }
  101. /*
  102. * Interrupts are enabled
  103. */
  104. static void bfin_serial_stop_rx(struct uart_port *port)
  105. {
  106. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  107. #ifdef CONFIG_KGDB_UART
  108. if (uart->port.line != CONFIG_KGDB_UART_PORT) {
  109. #endif
  110. #ifdef CONFIG_BF54x
  111. UART_CLEAR_IER(uart, ERBFI);
  112. #else
  113. unsigned short ier;
  114. ier = UART_GET_IER(uart);
  115. ier &= ~ERBFI;
  116. UART_PUT_IER(uart, ier);
  117. #endif
  118. #ifdef CONFIG_KGDB_UART
  119. }
  120. #endif
  121. }
  122. /*
  123. * Set the modem control timer to fire immediately.
  124. */
  125. static void bfin_serial_enable_ms(struct uart_port *port)
  126. {
  127. }
  128. #ifdef CONFIG_KGDB_UART
  129. static int kgdb_entry_state;
  130. void kgdb_put_debug_char(int chr)
  131. {
  132. struct bfin_serial_port *uart;
  133. if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
  134. uart = &bfin_serial_ports[0];
  135. else
  136. uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
  137. while (!(UART_GET_LSR(uart) & THRE)) {
  138. SSYNC();
  139. }
  140. #ifndef CONFIG_BF54x
  141. UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
  142. SSYNC();
  143. #endif
  144. UART_PUT_CHAR(uart, (unsigned char)chr);
  145. SSYNC();
  146. }
  147. int kgdb_get_debug_char(void)
  148. {
  149. struct bfin_serial_port *uart;
  150. unsigned char chr;
  151. if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
  152. uart = &bfin_serial_ports[0];
  153. else
  154. uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
  155. while(!(UART_GET_LSR(uart) & DR)) {
  156. SSYNC();
  157. }
  158. #ifndef CONFIG_BF54x
  159. UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
  160. SSYNC();
  161. #endif
  162. chr = UART_GET_CHAR(uart);
  163. SSYNC();
  164. return chr;
  165. }
  166. #endif
  167. #if ANOMALY_05000230 && defined(CONFIG_SERIAL_BFIN_PIO)
  168. # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
  169. # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
  170. #else
  171. # define UART_GET_ANOMALY_THRESHOLD(uart) 0
  172. # define UART_SET_ANOMALY_THRESHOLD(uart, v)
  173. #endif
  174. #ifdef CONFIG_SERIAL_BFIN_PIO
  175. static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
  176. {
  177. struct tty_struct *tty = uart->port.info->tty;
  178. unsigned int status, ch, flg;
  179. static struct timeval anomaly_start = { .tv_sec = 0 };
  180. #ifdef CONFIG_KGDB_UART
  181. struct pt_regs *regs = get_irq_regs();
  182. #endif
  183. status = UART_GET_LSR(uart);
  184. UART_CLEAR_LSR(uart);
  185. ch = UART_GET_CHAR(uart);
  186. uart->port.icount.rx++;
  187. #ifdef CONFIG_KGDB_UART
  188. if (uart->port.line == CONFIG_KGDB_UART_PORT) {
  189. if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
  190. kgdb_breakkey_pressed(regs);
  191. return;
  192. } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
  193. kgdb_entry_state = 1;
  194. } else if (kgdb_entry_state == 1 && ch == 'q') {
  195. kgdb_entry_state = 0;
  196. kgdb_breakkey_pressed(regs);
  197. return;
  198. } else if (ch == 0x3) {/* Ctrl + C */
  199. kgdb_entry_state = 0;
  200. kgdb_breakkey_pressed(regs);
  201. return;
  202. } else {
  203. kgdb_entry_state = 0;
  204. }
  205. }
  206. #endif
  207. if (ANOMALY_05000230) {
  208. /* The BF533 (and BF561) family of processors have a nice anomaly
  209. * where they continuously generate characters for a "single" break.
  210. * We have to basically ignore this flood until the "next" valid
  211. * character comes across. Due to the nature of the flood, it is
  212. * not possible to reliably catch bytes that are sent too quickly
  213. * after this break. So application code talking to the Blackfin
  214. * which sends a break signal must allow at least 1.5 character
  215. * times after the end of the break for things to stabilize. This
  216. * timeout was picked as it must absolutely be larger than 1
  217. * character time +/- some percent. So 1.5 sounds good. All other
  218. * Blackfin families operate properly. Woo.
  219. * Note: While Anomaly 05000230 does not directly address this,
  220. * the changes that went in for it also fixed this issue.
  221. * That anomaly was fixed in 0.5+ silicon. I like bunnies.
  222. */
  223. if (anomaly_start.tv_sec) {
  224. struct timeval curr;
  225. suseconds_t usecs;
  226. if ((~ch & (~ch + 1)) & 0xff)
  227. goto known_good_char;
  228. do_gettimeofday(&curr);
  229. if (curr.tv_sec - anomaly_start.tv_sec > 1)
  230. goto known_good_char;
  231. usecs = 0;
  232. if (curr.tv_sec != anomaly_start.tv_sec)
  233. usecs += USEC_PER_SEC;
  234. usecs += curr.tv_usec - anomaly_start.tv_usec;
  235. if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
  236. goto known_good_char;
  237. if (ch)
  238. anomaly_start.tv_sec = 0;
  239. else
  240. anomaly_start = curr;
  241. return;
  242. known_good_char:
  243. anomaly_start.tv_sec = 0;
  244. }
  245. }
  246. if (status & BI) {
  247. if (ANOMALY_05000230)
  248. if (bfin_revid() < 5)
  249. do_gettimeofday(&anomaly_start);
  250. uart->port.icount.brk++;
  251. if (uart_handle_break(&uart->port))
  252. goto ignore_char;
  253. status &= ~(PE | FE);
  254. }
  255. if (status & PE)
  256. uart->port.icount.parity++;
  257. if (status & OE)
  258. uart->port.icount.overrun++;
  259. if (status & FE)
  260. uart->port.icount.frame++;
  261. status &= uart->port.read_status_mask;
  262. if (status & BI)
  263. flg = TTY_BREAK;
  264. else if (status & PE)
  265. flg = TTY_PARITY;
  266. else if (status & FE)
  267. flg = TTY_FRAME;
  268. else
  269. flg = TTY_NORMAL;
  270. if (uart_handle_sysrq_char(&uart->port, ch))
  271. goto ignore_char;
  272. uart_insert_char(&uart->port, status, OE, ch, flg);
  273. ignore_char:
  274. tty_flip_buffer_push(tty);
  275. }
  276. static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
  277. {
  278. struct circ_buf *xmit = &uart->port.info->xmit;
  279. if (uart->port.x_char) {
  280. UART_PUT_CHAR(uart, uart->port.x_char);
  281. uart->port.icount.tx++;
  282. uart->port.x_char = 0;
  283. }
  284. /*
  285. * Check the modem control lines before
  286. * transmitting anything.
  287. */
  288. bfin_serial_mctrl_check(uart);
  289. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  290. bfin_serial_stop_tx(&uart->port);
  291. return;
  292. }
  293. while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
  294. UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
  295. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  296. uart->port.icount.tx++;
  297. SSYNC();
  298. }
  299. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  300. uart_write_wakeup(&uart->port);
  301. if (uart_circ_empty(xmit))
  302. bfin_serial_stop_tx(&uart->port);
  303. }
  304. static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
  305. {
  306. struct bfin_serial_port *uart = dev_id;
  307. spin_lock(&uart->port.lock);
  308. while (UART_GET_LSR(uart) & DR)
  309. bfin_serial_rx_chars(uart);
  310. spin_unlock(&uart->port.lock);
  311. return IRQ_HANDLED;
  312. }
  313. static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
  314. {
  315. struct bfin_serial_port *uart = dev_id;
  316. spin_lock(&uart->port.lock);
  317. if (UART_GET_LSR(uart) & THRE)
  318. bfin_serial_tx_chars(uart);
  319. spin_unlock(&uart->port.lock);
  320. return IRQ_HANDLED;
  321. }
  322. #endif
  323. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  324. static void bfin_serial_do_work(struct work_struct *work)
  325. {
  326. struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
  327. bfin_serial_mctrl_check(uart);
  328. }
  329. #endif
  330. #ifdef CONFIG_SERIAL_BFIN_DMA
  331. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
  332. {
  333. struct circ_buf *xmit = &uart->port.info->xmit;
  334. unsigned short ier;
  335. uart->tx_done = 0;
  336. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  337. uart->tx_count = 0;
  338. uart->tx_done = 1;
  339. return;
  340. }
  341. if (uart->port.x_char) {
  342. UART_PUT_CHAR(uart, uart->port.x_char);
  343. uart->port.icount.tx++;
  344. uart->port.x_char = 0;
  345. }
  346. /*
  347. * Check the modem control lines before
  348. * transmitting anything.
  349. */
  350. bfin_serial_mctrl_check(uart);
  351. uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
  352. if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
  353. uart->tx_count = UART_XMIT_SIZE - xmit->tail;
  354. blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
  355. (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
  356. set_dma_config(uart->tx_dma_channel,
  357. set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
  358. INTR_ON_BUF,
  359. DIMENSION_LINEAR,
  360. DATA_SIZE_8,
  361. DMA_SYNC_RESTART));
  362. set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
  363. set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
  364. set_dma_x_modify(uart->tx_dma_channel, 1);
  365. enable_dma(uart->tx_dma_channel);
  366. #ifdef CONFIG_BF54x
  367. UART_SET_IER(uart, ETBEI);
  368. #else
  369. ier = UART_GET_IER(uart);
  370. ier |= ETBEI;
  371. UART_PUT_IER(uart, ier);
  372. #endif
  373. }
  374. static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
  375. {
  376. struct tty_struct *tty = uart->port.info->tty;
  377. int i, flg, status;
  378. status = UART_GET_LSR(uart);
  379. UART_CLEAR_LSR(uart);
  380. uart->port.icount.rx +=
  381. CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
  382. UART_XMIT_SIZE);
  383. if (status & BI) {
  384. uart->port.icount.brk++;
  385. if (uart_handle_break(&uart->port))
  386. goto dma_ignore_char;
  387. status &= ~(PE | FE);
  388. }
  389. if (status & PE)
  390. uart->port.icount.parity++;
  391. if (status & OE)
  392. uart->port.icount.overrun++;
  393. if (status & FE)
  394. uart->port.icount.frame++;
  395. status &= uart->port.read_status_mask;
  396. if (status & BI)
  397. flg = TTY_BREAK;
  398. else if (status & PE)
  399. flg = TTY_PARITY;
  400. else if (status & FE)
  401. flg = TTY_FRAME;
  402. else
  403. flg = TTY_NORMAL;
  404. for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
  405. if (i >= UART_XMIT_SIZE)
  406. i = 0;
  407. if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
  408. uart_insert_char(&uart->port, status, OE,
  409. uart->rx_dma_buf.buf[i], flg);
  410. }
  411. dma_ignore_char:
  412. tty_flip_buffer_push(tty);
  413. }
  414. void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
  415. {
  416. int x_pos, pos;
  417. uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
  418. x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
  419. uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
  420. if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
  421. uart->rx_dma_nrows = 0;
  422. x_pos = DMA_RX_XCOUNT - x_pos;
  423. if (x_pos == DMA_RX_XCOUNT)
  424. x_pos = 0;
  425. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  426. if (pos != uart->rx_dma_buf.tail) {
  427. uart->rx_dma_buf.head = pos;
  428. bfin_serial_dma_rx_chars(uart);
  429. uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
  430. }
  431. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  432. add_timer(&(uart->rx_dma_timer));
  433. }
  434. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  435. {
  436. struct bfin_serial_port *uart = dev_id;
  437. struct circ_buf *xmit = &uart->port.info->xmit;
  438. unsigned short ier;
  439. spin_lock(&uart->port.lock);
  440. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  441. disable_dma(uart->tx_dma_channel);
  442. clear_dma_irqstat(uart->tx_dma_channel);
  443. #ifdef CONFIG_BF54x
  444. UART_CLEAR_IER(uart, ETBEI);
  445. #else
  446. ier = UART_GET_IER(uart);
  447. ier &= ~ETBEI;
  448. UART_PUT_IER(uart, ier);
  449. #endif
  450. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  451. uart->port.icount.tx += uart->tx_count;
  452. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  453. uart_write_wakeup(&uart->port);
  454. bfin_serial_dma_tx_chars(uart);
  455. }
  456. spin_unlock(&uart->port.lock);
  457. return IRQ_HANDLED;
  458. }
  459. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  460. {
  461. struct bfin_serial_port *uart = dev_id;
  462. unsigned short irqstat;
  463. spin_lock(&uart->port.lock);
  464. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  465. clear_dma_irqstat(uart->rx_dma_channel);
  466. spin_unlock(&uart->port.lock);
  467. del_timer(&(uart->rx_dma_timer));
  468. uart->rx_dma_timer.expires = jiffies;
  469. add_timer(&(uart->rx_dma_timer));
  470. return IRQ_HANDLED;
  471. }
  472. #endif
  473. /*
  474. * Return TIOCSER_TEMT when transmitter is not busy.
  475. */
  476. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  477. {
  478. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  479. unsigned short lsr;
  480. lsr = UART_GET_LSR(uart);
  481. if (lsr & TEMT)
  482. return TIOCSER_TEMT;
  483. else
  484. return 0;
  485. }
  486. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  487. {
  488. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  489. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  490. if (uart->cts_pin < 0)
  491. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  492. # ifdef BF54x
  493. if (UART_GET_MSR(uart) & CTS)
  494. # else
  495. if (gpio_get_value(uart->cts_pin))
  496. # endif
  497. return TIOCM_DSR | TIOCM_CAR;
  498. else
  499. #endif
  500. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  501. }
  502. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  503. {
  504. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  505. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  506. if (uart->rts_pin < 0)
  507. return;
  508. if (mctrl & TIOCM_RTS)
  509. # ifdef BF54x
  510. UART_PUT_MCR(uart, UART_GET_MCR(uart) & ~MRTS);
  511. # else
  512. gpio_set_value(uart->rts_pin, 0);
  513. # endif
  514. else
  515. # ifdef BF54x
  516. UART_PUT_MCR(uart, UART_GET_MCR(uart) | MRTS);
  517. # else
  518. gpio_set_value(uart->rts_pin, 1);
  519. # endif
  520. #endif
  521. }
  522. /*
  523. * Handle any change of modem status signal since we were last called.
  524. */
  525. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  526. {
  527. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  528. unsigned int status;
  529. struct uart_info *info = uart->port.info;
  530. struct tty_struct *tty = info->tty;
  531. status = bfin_serial_get_mctrl(&uart->port);
  532. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  533. if (!(status & TIOCM_CTS)) {
  534. tty->hw_stopped = 1;
  535. schedule_work(&uart->cts_workqueue);
  536. } else {
  537. tty->hw_stopped = 0;
  538. }
  539. #endif
  540. }
  541. /*
  542. * Interrupts are always disabled.
  543. */
  544. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  545. {
  546. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  547. u16 lcr = UART_GET_LCR(uart);
  548. if (break_state)
  549. lcr |= SB;
  550. else
  551. lcr &= ~SB;
  552. UART_PUT_LCR(uart, lcr);
  553. SSYNC();
  554. }
  555. static int bfin_serial_startup(struct uart_port *port)
  556. {
  557. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  558. #ifdef CONFIG_SERIAL_BFIN_DMA
  559. dma_addr_t dma_handle;
  560. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  561. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  562. return -EBUSY;
  563. }
  564. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  565. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  566. free_dma(uart->rx_dma_channel);
  567. return -EBUSY;
  568. }
  569. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  570. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  571. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  572. uart->rx_dma_buf.head = 0;
  573. uart->rx_dma_buf.tail = 0;
  574. uart->rx_dma_nrows = 0;
  575. set_dma_config(uart->rx_dma_channel,
  576. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  577. INTR_ON_ROW, DIMENSION_2D,
  578. DATA_SIZE_8,
  579. DMA_SYNC_RESTART));
  580. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  581. set_dma_x_modify(uart->rx_dma_channel, 1);
  582. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  583. set_dma_y_modify(uart->rx_dma_channel, 1);
  584. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  585. enable_dma(uart->rx_dma_channel);
  586. uart->rx_dma_timer.data = (unsigned long)(uart);
  587. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  588. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  589. add_timer(&(uart->rx_dma_timer));
  590. #else
  591. if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  592. "BFIN_UART_RX", uart)) {
  593. # ifdef CONFIG_KGDB_UART
  594. if (uart->port.line != CONFIG_KGDB_UART_PORT) {
  595. # endif
  596. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  597. return -EBUSY;
  598. # ifdef CONFIG_KGDB_UART
  599. }
  600. # endif
  601. }
  602. if (request_irq
  603. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  604. "BFIN_UART_TX", uart)) {
  605. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  606. free_irq(uart->port.irq, uart);
  607. return -EBUSY;
  608. }
  609. #endif
  610. #ifdef CONFIG_BF54x
  611. UART_SET_IER(uart, ERBFI);
  612. #else
  613. UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
  614. #endif
  615. return 0;
  616. }
  617. static void bfin_serial_shutdown(struct uart_port *port)
  618. {
  619. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  620. #ifdef CONFIG_SERIAL_BFIN_DMA
  621. disable_dma(uart->tx_dma_channel);
  622. free_dma(uart->tx_dma_channel);
  623. disable_dma(uart->rx_dma_channel);
  624. free_dma(uart->rx_dma_channel);
  625. del_timer(&(uart->rx_dma_timer));
  626. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  627. #else
  628. #ifdef CONFIG_KGDB_UART
  629. if (uart->port.line != CONFIG_KGDB_UART_PORT)
  630. #endif
  631. free_irq(uart->port.irq, uart);
  632. free_irq(uart->port.irq+1, uart);
  633. #endif
  634. }
  635. static void
  636. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  637. struct ktermios *old)
  638. {
  639. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  640. unsigned long flags;
  641. unsigned int baud, quot;
  642. unsigned short val, ier, lcr = 0;
  643. switch (termios->c_cflag & CSIZE) {
  644. case CS8:
  645. lcr = WLS(8);
  646. break;
  647. case CS7:
  648. lcr = WLS(7);
  649. break;
  650. case CS6:
  651. lcr = WLS(6);
  652. break;
  653. case CS5:
  654. lcr = WLS(5);
  655. break;
  656. default:
  657. printk(KERN_ERR "%s: word lengh not supported\n",
  658. __FUNCTION__);
  659. }
  660. if (termios->c_cflag & CSTOPB)
  661. lcr |= STB;
  662. if (termios->c_cflag & PARENB)
  663. lcr |= PEN;
  664. if (!(termios->c_cflag & PARODD))
  665. lcr |= EPS;
  666. if (termios->c_cflag & CMSPAR)
  667. lcr |= STP;
  668. port->read_status_mask = OE;
  669. if (termios->c_iflag & INPCK)
  670. port->read_status_mask |= (FE | PE);
  671. if (termios->c_iflag & (BRKINT | PARMRK))
  672. port->read_status_mask |= BI;
  673. /*
  674. * Characters to ignore
  675. */
  676. port->ignore_status_mask = 0;
  677. if (termios->c_iflag & IGNPAR)
  678. port->ignore_status_mask |= FE | PE;
  679. if (termios->c_iflag & IGNBRK) {
  680. port->ignore_status_mask |= BI;
  681. /*
  682. * If we're ignoring parity and break indicators,
  683. * ignore overruns too (for real raw support).
  684. */
  685. if (termios->c_iflag & IGNPAR)
  686. port->ignore_status_mask |= OE;
  687. }
  688. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  689. quot = uart_get_divisor(port, baud);
  690. spin_lock_irqsave(&uart->port.lock, flags);
  691. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  692. do {
  693. lsr = UART_GET_LSR(uart);
  694. } while (!(lsr & TEMT));
  695. /* Disable UART */
  696. ier = UART_GET_IER(uart);
  697. #ifdef CONFIG_BF54x
  698. UART_CLEAR_IER(uart, 0xF);
  699. #else
  700. UART_PUT_IER(uart, 0);
  701. #endif
  702. #ifndef CONFIG_BF54x
  703. /* Set DLAB in LCR to Access DLL and DLH */
  704. val = UART_GET_LCR(uart);
  705. val |= DLAB;
  706. UART_PUT_LCR(uart, val);
  707. SSYNC();
  708. #endif
  709. UART_PUT_DLL(uart, quot & 0xFF);
  710. SSYNC();
  711. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  712. SSYNC();
  713. #ifndef CONFIG_BF54x
  714. /* Clear DLAB in LCR to Access THR RBR IER */
  715. val = UART_GET_LCR(uart);
  716. val &= ~DLAB;
  717. UART_PUT_LCR(uart, val);
  718. SSYNC();
  719. #endif
  720. UART_PUT_LCR(uart, lcr);
  721. /* Enable UART */
  722. #ifdef CONFIG_BF54x
  723. UART_SET_IER(uart, ier);
  724. #else
  725. UART_PUT_IER(uart, ier);
  726. #endif
  727. val = UART_GET_GCTL(uart);
  728. val |= UCEN;
  729. UART_PUT_GCTL(uart, val);
  730. spin_unlock_irqrestore(&uart->port.lock, flags);
  731. }
  732. static const char *bfin_serial_type(struct uart_port *port)
  733. {
  734. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  735. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  736. }
  737. /*
  738. * Release the memory region(s) being used by 'port'.
  739. */
  740. static void bfin_serial_release_port(struct uart_port *port)
  741. {
  742. }
  743. /*
  744. * Request the memory region(s) being used by 'port'.
  745. */
  746. static int bfin_serial_request_port(struct uart_port *port)
  747. {
  748. return 0;
  749. }
  750. /*
  751. * Configure/autoconfigure the port.
  752. */
  753. static void bfin_serial_config_port(struct uart_port *port, int flags)
  754. {
  755. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  756. if (flags & UART_CONFIG_TYPE &&
  757. bfin_serial_request_port(&uart->port) == 0)
  758. uart->port.type = PORT_BFIN;
  759. }
  760. /*
  761. * Verify the new serial_struct (for TIOCSSERIAL).
  762. * The only change we allow are to the flags and type, and
  763. * even then only between PORT_BFIN and PORT_UNKNOWN
  764. */
  765. static int
  766. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  767. {
  768. return 0;
  769. }
  770. /*
  771. * Enable the IrDA function if tty->ldisc.num is N_IRDA.
  772. * In other cases, disable IrDA function.
  773. */
  774. static void bfin_set_ldisc(struct tty_struct *tty)
  775. {
  776. int line = tty->index;
  777. unsigned short val;
  778. if (line >= tty->driver->num)
  779. return;
  780. switch (tty->ldisc.num) {
  781. case N_IRDA:
  782. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  783. val |= (IREN | RPOLC);
  784. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  785. break;
  786. default:
  787. val = UART_GET_GCTL(&bfin_serial_ports[line]);
  788. val &= ~(IREN | RPOLC);
  789. UART_PUT_GCTL(&bfin_serial_ports[line], val);
  790. }
  791. }
  792. static struct uart_ops bfin_serial_pops = {
  793. .tx_empty = bfin_serial_tx_empty,
  794. .set_mctrl = bfin_serial_set_mctrl,
  795. .get_mctrl = bfin_serial_get_mctrl,
  796. .stop_tx = bfin_serial_stop_tx,
  797. .start_tx = bfin_serial_start_tx,
  798. .stop_rx = bfin_serial_stop_rx,
  799. .enable_ms = bfin_serial_enable_ms,
  800. .break_ctl = bfin_serial_break_ctl,
  801. .startup = bfin_serial_startup,
  802. .shutdown = bfin_serial_shutdown,
  803. .set_termios = bfin_serial_set_termios,
  804. .type = bfin_serial_type,
  805. .release_port = bfin_serial_release_port,
  806. .request_port = bfin_serial_request_port,
  807. .config_port = bfin_serial_config_port,
  808. .verify_port = bfin_serial_verify_port,
  809. };
  810. static void __init bfin_serial_init_ports(void)
  811. {
  812. static int first = 1;
  813. int i;
  814. if (!first)
  815. return;
  816. first = 0;
  817. for (i = 0; i < nr_ports; i++) {
  818. bfin_serial_ports[i].port.uartclk = get_sclk();
  819. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  820. bfin_serial_ports[i].port.line = i;
  821. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  822. bfin_serial_ports[i].port.membase =
  823. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  824. bfin_serial_ports[i].port.mapbase =
  825. bfin_serial_resource[i].uart_base_addr;
  826. bfin_serial_ports[i].port.irq =
  827. bfin_serial_resource[i].uart_irq;
  828. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  829. #ifdef CONFIG_SERIAL_BFIN_DMA
  830. bfin_serial_ports[i].tx_done = 1;
  831. bfin_serial_ports[i].tx_count = 0;
  832. bfin_serial_ports[i].tx_dma_channel =
  833. bfin_serial_resource[i].uart_tx_dma_channel;
  834. bfin_serial_ports[i].rx_dma_channel =
  835. bfin_serial_resource[i].uart_rx_dma_channel;
  836. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  837. #endif
  838. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  839. INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
  840. bfin_serial_ports[i].cts_pin =
  841. bfin_serial_resource[i].uart_cts_pin;
  842. bfin_serial_ports[i].rts_pin =
  843. bfin_serial_resource[i].uart_rts_pin;
  844. #endif
  845. bfin_serial_hw_init(&bfin_serial_ports[i]);
  846. }
  847. }
  848. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  849. /*
  850. * If the port was already initialised (eg, by a boot loader),
  851. * try to determine the current setup.
  852. */
  853. static void __init
  854. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  855. int *parity, int *bits)
  856. {
  857. unsigned short status;
  858. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  859. if (status == (ERBFI | ETBEI)) {
  860. /* ok, the port was enabled */
  861. unsigned short lcr, val;
  862. unsigned short dlh, dll;
  863. lcr = UART_GET_LCR(uart);
  864. *parity = 'n';
  865. if (lcr & PEN) {
  866. if (lcr & EPS)
  867. *parity = 'e';
  868. else
  869. *parity = 'o';
  870. }
  871. switch (lcr & 0x03) {
  872. case 0: *bits = 5; break;
  873. case 1: *bits = 6; break;
  874. case 2: *bits = 7; break;
  875. case 3: *bits = 8; break;
  876. }
  877. #ifndef CONFIG_BF54x
  878. /* Set DLAB in LCR to Access DLL and DLH */
  879. val = UART_GET_LCR(uart);
  880. val |= DLAB;
  881. UART_PUT_LCR(uart, val);
  882. #endif
  883. dll = UART_GET_DLL(uart);
  884. dlh = UART_GET_DLH(uart);
  885. #ifndef CONFIG_BF54x
  886. /* Clear DLAB in LCR to Access THR RBR IER */
  887. val = UART_GET_LCR(uart);
  888. val &= ~DLAB;
  889. UART_PUT_LCR(uart, val);
  890. #endif
  891. *baud = get_sclk() / (16*(dll | dlh << 8));
  892. }
  893. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
  894. }
  895. #endif
  896. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  897. static struct uart_driver bfin_serial_reg;
  898. static int __init
  899. bfin_serial_console_setup(struct console *co, char *options)
  900. {
  901. struct bfin_serial_port *uart;
  902. # ifdef CONFIG_SERIAL_BFIN_CONSOLE
  903. int baud = 57600;
  904. int bits = 8;
  905. int parity = 'n';
  906. # ifdef CONFIG_SERIAL_BFIN_CTSRTS
  907. int flow = 'r';
  908. # else
  909. int flow = 'n';
  910. # endif
  911. # endif
  912. /*
  913. * Check whether an invalid uart number has been specified, and
  914. * if so, search for the first available port that does have
  915. * console support.
  916. */
  917. if (co->index == -1 || co->index >= nr_ports)
  918. co->index = 0;
  919. uart = &bfin_serial_ports[co->index];
  920. # ifdef CONFIG_SERIAL_BFIN_CONSOLE
  921. if (options)
  922. uart_parse_options(options, &baud, &parity, &bits, &flow);
  923. else
  924. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  925. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  926. # else
  927. return 0;
  928. # endif
  929. }
  930. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  931. defined (CONFIG_EARLY_PRINTK) */
  932. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  933. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  934. {
  935. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  936. while (!(UART_GET_LSR(uart) & THRE))
  937. barrier();
  938. UART_PUT_CHAR(uart, ch);
  939. SSYNC();
  940. }
  941. /*
  942. * Interrupts are disabled on entering
  943. */
  944. static void
  945. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  946. {
  947. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  948. int flags = 0;
  949. spin_lock_irqsave(&uart->port.lock, flags);
  950. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  951. spin_unlock_irqrestore(&uart->port.lock, flags);
  952. }
  953. static struct console bfin_serial_console = {
  954. .name = BFIN_SERIAL_NAME,
  955. .write = bfin_serial_console_write,
  956. .device = uart_console_device,
  957. .setup = bfin_serial_console_setup,
  958. .flags = CON_PRINTBUFFER,
  959. .index = -1,
  960. .data = &bfin_serial_reg,
  961. };
  962. static int __init bfin_serial_rs_console_init(void)
  963. {
  964. bfin_serial_init_ports();
  965. register_console(&bfin_serial_console);
  966. #ifdef CONFIG_KGDB_UART
  967. kgdb_entry_state = 0;
  968. init_kgdb_uart();
  969. #endif
  970. return 0;
  971. }
  972. console_initcall(bfin_serial_rs_console_init);
  973. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  974. #else
  975. #define BFIN_SERIAL_CONSOLE NULL
  976. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  977. #ifdef CONFIG_EARLY_PRINTK
  978. static __init void early_serial_putc(struct uart_port *port, int ch)
  979. {
  980. unsigned timeout = 0xffff;
  981. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  982. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  983. cpu_relax();
  984. UART_PUT_CHAR(uart, ch);
  985. }
  986. static __init void early_serial_write(struct console *con, const char *s,
  987. unsigned int n)
  988. {
  989. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  990. unsigned int i;
  991. for (i = 0; i < n; i++, s++) {
  992. if (*s == '\n')
  993. early_serial_putc(&uart->port, '\r');
  994. early_serial_putc(&uart->port, *s);
  995. }
  996. }
  997. static struct __init console bfin_early_serial_console = {
  998. .name = "early_BFuart",
  999. .write = early_serial_write,
  1000. .device = uart_console_device,
  1001. .flags = CON_PRINTBUFFER,
  1002. .setup = bfin_serial_console_setup,
  1003. .index = -1,
  1004. .data = &bfin_serial_reg,
  1005. };
  1006. struct console __init *bfin_earlyserial_init(unsigned int port,
  1007. unsigned int cflag)
  1008. {
  1009. struct bfin_serial_port *uart;
  1010. struct ktermios t;
  1011. if (port == -1 || port >= nr_ports)
  1012. port = 0;
  1013. bfin_serial_init_ports();
  1014. bfin_early_serial_console.index = port;
  1015. uart = &bfin_serial_ports[port];
  1016. t.c_cflag = cflag;
  1017. t.c_iflag = 0;
  1018. t.c_oflag = 0;
  1019. t.c_lflag = ICANON;
  1020. t.c_line = port;
  1021. bfin_serial_set_termios(&uart->port, &t, &t);
  1022. return &bfin_early_serial_console;
  1023. }
  1024. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  1025. static struct uart_driver bfin_serial_reg = {
  1026. .owner = THIS_MODULE,
  1027. .driver_name = "bfin-uart",
  1028. .dev_name = BFIN_SERIAL_NAME,
  1029. .major = BFIN_SERIAL_MAJOR,
  1030. .minor = BFIN_SERIAL_MINOR,
  1031. .nr = NR_PORTS,
  1032. .cons = BFIN_SERIAL_CONSOLE,
  1033. };
  1034. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1035. {
  1036. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  1037. if (uart)
  1038. uart_suspend_port(&bfin_serial_reg, &uart->port);
  1039. return 0;
  1040. }
  1041. static int bfin_serial_resume(struct platform_device *dev)
  1042. {
  1043. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  1044. if (uart)
  1045. uart_resume_port(&bfin_serial_reg, &uart->port);
  1046. return 0;
  1047. }
  1048. static int bfin_serial_probe(struct platform_device *dev)
  1049. {
  1050. struct resource *res = dev->resource;
  1051. int i;
  1052. for (i = 0; i < dev->num_resources; i++, res++)
  1053. if (res->flags & IORESOURCE_MEM)
  1054. break;
  1055. if (i < dev->num_resources) {
  1056. for (i = 0; i < nr_ports; i++, res++) {
  1057. if (bfin_serial_ports[i].port.mapbase != res->start)
  1058. continue;
  1059. bfin_serial_ports[i].port.dev = &dev->dev;
  1060. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1061. platform_set_drvdata(dev, &bfin_serial_ports[i]);
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. static int bfin_serial_remove(struct platform_device *pdev)
  1067. {
  1068. struct bfin_serial_port *uart = platform_get_drvdata(pdev);
  1069. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  1070. gpio_free(uart->cts_pin);
  1071. gpio_free(uart->rts_pin);
  1072. #endif
  1073. platform_set_drvdata(pdev, NULL);
  1074. if (uart)
  1075. uart_remove_one_port(&bfin_serial_reg, &uart->port);
  1076. return 0;
  1077. }
  1078. static struct platform_driver bfin_serial_driver = {
  1079. .probe = bfin_serial_probe,
  1080. .remove = bfin_serial_remove,
  1081. .suspend = bfin_serial_suspend,
  1082. .resume = bfin_serial_resume,
  1083. .driver = {
  1084. .name = "bfin-uart",
  1085. .owner = THIS_MODULE,
  1086. },
  1087. };
  1088. static int __init bfin_serial_init(void)
  1089. {
  1090. int ret;
  1091. #ifdef CONFIG_KGDB_UART
  1092. struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
  1093. struct ktermios t;
  1094. #endif
  1095. pr_info("Serial: Blackfin serial driver\n");
  1096. bfin_serial_init_ports();
  1097. ret = uart_register_driver(&bfin_serial_reg);
  1098. if (ret == 0) {
  1099. bfin_serial_reg.tty_driver->set_ldisc = bfin_set_ldisc;
  1100. ret = platform_driver_register(&bfin_serial_driver);
  1101. if (ret) {
  1102. pr_debug("uart register failed\n");
  1103. uart_unregister_driver(&bfin_serial_reg);
  1104. }
  1105. }
  1106. #ifdef CONFIG_KGDB_UART
  1107. if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
  1108. request_irq(uart->port.irq, bfin_serial_rx_int,
  1109. IRQF_DISABLED, "BFIN_UART_RX", uart);
  1110. pr_info("Request irq for kgdb uart port\n");
  1111. #ifdef CONFIG_BF54x
  1112. UART_SET_IER(uart, ERBFI);
  1113. #else
  1114. UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
  1115. #endif
  1116. SSYNC();
  1117. t.c_cflag = CS8|B57600;
  1118. t.c_iflag = 0;
  1119. t.c_oflag = 0;
  1120. t.c_lflag = ICANON;
  1121. t.c_line = CONFIG_KGDB_UART_PORT;
  1122. bfin_serial_set_termios(&uart->port, &t, &t);
  1123. }
  1124. #endif
  1125. return ret;
  1126. }
  1127. static void __exit bfin_serial_exit(void)
  1128. {
  1129. platform_driver_unregister(&bfin_serial_driver);
  1130. uart_unregister_driver(&bfin_serial_reg);
  1131. }
  1132. module_init(bfin_serial_init);
  1133. module_exit(bfin_serial_exit);
  1134. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1135. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1136. MODULE_LICENSE("GPL");
  1137. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
  1138. MODULE_ALIAS("platform:bfin-uart");