bfin_5xx.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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 5
  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. int flags = 0;
  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. /*
  348. * Check the modem control lines before
  349. * transmitting anything.
  350. */
  351. bfin_serial_mctrl_check(uart);
  352. spin_lock_irqsave(&uart->port.lock, flags);
  353. uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
  354. if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
  355. uart->tx_count = UART_XMIT_SIZE - xmit->tail;
  356. blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
  357. (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
  358. set_dma_config(uart->tx_dma_channel,
  359. set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
  360. INTR_ON_BUF,
  361. DIMENSION_LINEAR,
  362. DATA_SIZE_8,
  363. DMA_SYNC_RESTART));
  364. set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
  365. set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
  366. set_dma_x_modify(uart->tx_dma_channel, 1);
  367. enable_dma(uart->tx_dma_channel);
  368. #ifdef CONFIG_BF54x
  369. UART_SET_IER(uart, ETBEI);
  370. #else
  371. ier = UART_GET_IER(uart);
  372. ier |= ETBEI;
  373. UART_PUT_IER(uart, ier);
  374. #endif
  375. spin_unlock_irqrestore(&uart->port.lock, flags);
  376. }
  377. static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
  378. {
  379. struct tty_struct *tty = uart->port.info->tty;
  380. int i, flg, status;
  381. status = UART_GET_LSR(uart);
  382. UART_CLEAR_LSR(uart);
  383. uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
  384. if (status & BI) {
  385. uart->port.icount.brk++;
  386. if (uart_handle_break(&uart->port))
  387. goto dma_ignore_char;
  388. status &= ~(PE | FE);
  389. }
  390. if (status & PE)
  391. uart->port.icount.parity++;
  392. if (status & OE)
  393. uart->port.icount.overrun++;
  394. if (status & FE)
  395. uart->port.icount.frame++;
  396. status &= uart->port.read_status_mask;
  397. if (status & BI)
  398. flg = TTY_BREAK;
  399. else if (status & PE)
  400. flg = TTY_PARITY;
  401. else if (status & FE)
  402. flg = TTY_FRAME;
  403. else
  404. flg = TTY_NORMAL;
  405. for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
  406. if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
  407. goto dma_ignore_char;
  408. uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
  409. }
  410. dma_ignore_char:
  411. tty_flip_buffer_push(tty);
  412. }
  413. void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
  414. {
  415. int x_pos, pos;
  416. int flags = 0;
  417. spin_lock_irqsave(&uart->port.lock, flags);
  418. x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
  419. if (x_pos == DMA_RX_XCOUNT)
  420. x_pos = 0;
  421. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  422. if (pos>uart->rx_dma_buf.tail) {
  423. uart->rx_dma_buf.tail = pos;
  424. bfin_serial_dma_rx_chars(uart);
  425. uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
  426. }
  427. spin_unlock_irqrestore(&uart->port.lock, flags);
  428. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  429. add_timer(&(uart->rx_dma_timer));
  430. }
  431. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  432. {
  433. struct bfin_serial_port *uart = dev_id;
  434. struct circ_buf *xmit = &uart->port.info->xmit;
  435. unsigned short ier;
  436. spin_lock(&uart->port.lock);
  437. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  438. disable_dma(uart->tx_dma_channel);
  439. clear_dma_irqstat(uart->tx_dma_channel);
  440. #ifdef CONFIG_BF54x
  441. UART_CLEAR_IER(uart, ETBEI);
  442. #else
  443. ier = UART_GET_IER(uart);
  444. ier &= ~ETBEI;
  445. UART_PUT_IER(uart, ier);
  446. #endif
  447. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  448. uart_write_wakeup(&uart->port);
  449. xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
  450. uart->port.icount.tx += uart->tx_count;
  451. bfin_serial_dma_tx_chars(uart);
  452. }
  453. spin_unlock(&uart->port.lock);
  454. return IRQ_HANDLED;
  455. }
  456. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  457. {
  458. struct bfin_serial_port *uart = dev_id;
  459. unsigned short irqstat;
  460. uart->rx_dma_nrows++;
  461. uart->rx_dma_buf.tail = DMA_RX_XCOUNT * uart->rx_dma_nrows;
  462. bfin_serial_dma_rx_chars(uart);
  463. if (uart->rx_dma_nrows >= DMA_RX_YCOUNT) {
  464. uart->rx_dma_nrows = 0;
  465. uart->rx_dma_buf.tail = 0;
  466. }
  467. uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
  468. spin_lock(&uart->port.lock);
  469. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  470. clear_dma_irqstat(uart->rx_dma_channel);
  471. spin_unlock(&uart->port.lock);
  472. return IRQ_HANDLED;
  473. }
  474. #endif
  475. /*
  476. * Return TIOCSER_TEMT when transmitter is not busy.
  477. */
  478. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  479. {
  480. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  481. unsigned short lsr;
  482. lsr = UART_GET_LSR(uart);
  483. if (lsr & TEMT)
  484. return TIOCSER_TEMT;
  485. else
  486. return 0;
  487. }
  488. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  489. {
  490. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  491. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  492. if (uart->cts_pin < 0)
  493. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  494. # ifdef BF54x
  495. if (UART_GET_MSR(uart) & CTS)
  496. # else
  497. if (gpio_get_value(uart->cts_pin))
  498. # endif
  499. return TIOCM_DSR | TIOCM_CAR;
  500. else
  501. #endif
  502. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  503. }
  504. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  505. {
  506. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  507. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  508. if (uart->rts_pin < 0)
  509. return;
  510. if (mctrl & TIOCM_RTS)
  511. # ifdef BF54x
  512. UART_PUT_MCR(uart, UART_GET_MCR(uart) & ~MRTS);
  513. # else
  514. gpio_set_value(uart->rts_pin, 0);
  515. # endif
  516. else
  517. # ifdef BF54x
  518. UART_PUT_MCR(uart, UART_GET_MCR(uart) | MRTS);
  519. # else
  520. gpio_set_value(uart->rts_pin, 1);
  521. # endif
  522. #endif
  523. }
  524. /*
  525. * Handle any change of modem status signal since we were last called.
  526. */
  527. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  528. {
  529. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  530. unsigned int status;
  531. struct uart_info *info = uart->port.info;
  532. struct tty_struct *tty = info->tty;
  533. status = bfin_serial_get_mctrl(&uart->port);
  534. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  535. if (!(status & TIOCM_CTS)) {
  536. tty->hw_stopped = 1;
  537. schedule_work(&uart->cts_workqueue);
  538. } else {
  539. tty->hw_stopped = 0;
  540. }
  541. #endif
  542. }
  543. /*
  544. * Interrupts are always disabled.
  545. */
  546. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  547. {
  548. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  549. u16 lcr = UART_GET_LCR(uart);
  550. if (break_state)
  551. lcr |= SB;
  552. else
  553. lcr &= ~SB;
  554. UART_PUT_LCR(uart, lcr);
  555. SSYNC();
  556. }
  557. static int bfin_serial_startup(struct uart_port *port)
  558. {
  559. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  560. #ifdef CONFIG_SERIAL_BFIN_DMA
  561. dma_addr_t dma_handle;
  562. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  563. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  564. return -EBUSY;
  565. }
  566. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  567. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  568. free_dma(uart->rx_dma_channel);
  569. return -EBUSY;
  570. }
  571. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  572. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  573. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  574. uart->rx_dma_buf.head = 0;
  575. uart->rx_dma_buf.tail = 0;
  576. uart->rx_dma_nrows = 0;
  577. set_dma_config(uart->rx_dma_channel,
  578. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  579. INTR_ON_ROW, DIMENSION_2D,
  580. DATA_SIZE_8,
  581. DMA_SYNC_RESTART));
  582. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  583. set_dma_x_modify(uart->rx_dma_channel, 1);
  584. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  585. set_dma_y_modify(uart->rx_dma_channel, 1);
  586. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  587. enable_dma(uart->rx_dma_channel);
  588. uart->rx_dma_timer.data = (unsigned long)(uart);
  589. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  590. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  591. add_timer(&(uart->rx_dma_timer));
  592. #else
  593. if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  594. "BFIN_UART_RX", uart)) {
  595. # ifdef CONFIG_KGDB_UART
  596. if (uart->port.line != CONFIG_KGDB_UART_PORT) {
  597. # endif
  598. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  599. return -EBUSY;
  600. # ifdef CONFIG_KGDB_UART
  601. }
  602. # endif
  603. }
  604. if (request_irq
  605. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  606. "BFIN_UART_TX", uart)) {
  607. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  608. free_irq(uart->port.irq, uart);
  609. return -EBUSY;
  610. }
  611. #endif
  612. #ifdef CONFIG_BF54x
  613. UART_SET_IER(uart, ERBFI);
  614. #else
  615. UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
  616. #endif
  617. return 0;
  618. }
  619. static void bfin_serial_shutdown(struct uart_port *port)
  620. {
  621. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  622. #ifdef CONFIG_SERIAL_BFIN_DMA
  623. disable_dma(uart->tx_dma_channel);
  624. free_dma(uart->tx_dma_channel);
  625. disable_dma(uart->rx_dma_channel);
  626. free_dma(uart->rx_dma_channel);
  627. del_timer(&(uart->rx_dma_timer));
  628. dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
  629. #else
  630. #ifdef CONFIG_KGDB_UART
  631. if (uart->port.line != CONFIG_KGDB_UART_PORT)
  632. #endif
  633. free_irq(uart->port.irq, uart);
  634. free_irq(uart->port.irq+1, uart);
  635. #endif
  636. }
  637. static void
  638. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  639. struct ktermios *old)
  640. {
  641. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  642. unsigned long flags;
  643. unsigned int baud, quot;
  644. unsigned short val, ier, lsr, lcr = 0;
  645. switch (termios->c_cflag & CSIZE) {
  646. case CS8:
  647. lcr = WLS(8);
  648. break;
  649. case CS7:
  650. lcr = WLS(7);
  651. break;
  652. case CS6:
  653. lcr = WLS(6);
  654. break;
  655. case CS5:
  656. lcr = WLS(5);
  657. break;
  658. default:
  659. printk(KERN_ERR "%s: word lengh not supported\n",
  660. __FUNCTION__);
  661. }
  662. if (termios->c_cflag & CSTOPB)
  663. lcr |= STB;
  664. if (termios->c_cflag & PARENB)
  665. lcr |= PEN;
  666. if (!(termios->c_cflag & PARODD))
  667. lcr |= EPS;
  668. if (termios->c_cflag & CMSPAR)
  669. lcr |= STP;
  670. port->read_status_mask = OE;
  671. if (termios->c_iflag & INPCK)
  672. port->read_status_mask |= (FE | PE);
  673. if (termios->c_iflag & (BRKINT | PARMRK))
  674. port->read_status_mask |= BI;
  675. /*
  676. * Characters to ignore
  677. */
  678. port->ignore_status_mask = 0;
  679. if (termios->c_iflag & IGNPAR)
  680. port->ignore_status_mask |= FE | PE;
  681. if (termios->c_iflag & IGNBRK) {
  682. port->ignore_status_mask |= BI;
  683. /*
  684. * If we're ignoring parity and break indicators,
  685. * ignore overruns too (for real raw support).
  686. */
  687. if (termios->c_iflag & IGNPAR)
  688. port->ignore_status_mask |= OE;
  689. }
  690. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  691. quot = uart_get_divisor(port, baud);
  692. spin_lock_irqsave(&uart->port.lock, flags);
  693. UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
  694. do {
  695. lsr = UART_GET_LSR(uart);
  696. } while (!(lsr & TEMT));
  697. /* Disable UART */
  698. ier = UART_GET_IER(uart);
  699. #ifdef CONFIG_BF54x
  700. UART_CLEAR_IER(uart, 0xF);
  701. #else
  702. UART_PUT_IER(uart, 0);
  703. #endif
  704. #ifndef CONFIG_BF54x
  705. /* Set DLAB in LCR to Access DLL and DLH */
  706. val = UART_GET_LCR(uart);
  707. val |= DLAB;
  708. UART_PUT_LCR(uart, val);
  709. SSYNC();
  710. #endif
  711. UART_PUT_DLL(uart, quot & 0xFF);
  712. SSYNC();
  713. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  714. SSYNC();
  715. #ifndef CONFIG_BF54x
  716. /* Clear DLAB in LCR to Access THR RBR IER */
  717. val = UART_GET_LCR(uart);
  718. val &= ~DLAB;
  719. UART_PUT_LCR(uart, val);
  720. SSYNC();
  721. #endif
  722. UART_PUT_LCR(uart, lcr);
  723. /* Enable UART */
  724. #ifdef CONFIG_BF54x
  725. UART_SET_IER(uart, ier);
  726. #else
  727. UART_PUT_IER(uart, ier);
  728. #endif
  729. val = UART_GET_GCTL(uart);
  730. val |= UCEN;
  731. UART_PUT_GCTL(uart, val);
  732. spin_unlock_irqrestore(&uart->port.lock, flags);
  733. }
  734. static const char *bfin_serial_type(struct uart_port *port)
  735. {
  736. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  737. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  738. }
  739. /*
  740. * Release the memory region(s) being used by 'port'.
  741. */
  742. static void bfin_serial_release_port(struct uart_port *port)
  743. {
  744. }
  745. /*
  746. * Request the memory region(s) being used by 'port'.
  747. */
  748. static int bfin_serial_request_port(struct uart_port *port)
  749. {
  750. return 0;
  751. }
  752. /*
  753. * Configure/autoconfigure the port.
  754. */
  755. static void bfin_serial_config_port(struct uart_port *port, int flags)
  756. {
  757. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  758. if (flags & UART_CONFIG_TYPE &&
  759. bfin_serial_request_port(&uart->port) == 0)
  760. uart->port.type = PORT_BFIN;
  761. }
  762. /*
  763. * Verify the new serial_struct (for TIOCSSERIAL).
  764. * The only change we allow are to the flags and type, and
  765. * even then only between PORT_BFIN and PORT_UNKNOWN
  766. */
  767. static int
  768. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  769. {
  770. return 0;
  771. }
  772. static struct uart_ops bfin_serial_pops = {
  773. .tx_empty = bfin_serial_tx_empty,
  774. .set_mctrl = bfin_serial_set_mctrl,
  775. .get_mctrl = bfin_serial_get_mctrl,
  776. .stop_tx = bfin_serial_stop_tx,
  777. .start_tx = bfin_serial_start_tx,
  778. .stop_rx = bfin_serial_stop_rx,
  779. .enable_ms = bfin_serial_enable_ms,
  780. .break_ctl = bfin_serial_break_ctl,
  781. .startup = bfin_serial_startup,
  782. .shutdown = bfin_serial_shutdown,
  783. .set_termios = bfin_serial_set_termios,
  784. .type = bfin_serial_type,
  785. .release_port = bfin_serial_release_port,
  786. .request_port = bfin_serial_request_port,
  787. .config_port = bfin_serial_config_port,
  788. .verify_port = bfin_serial_verify_port,
  789. };
  790. static void __init bfin_serial_init_ports(void)
  791. {
  792. static int first = 1;
  793. int i;
  794. if (!first)
  795. return;
  796. first = 0;
  797. for (i = 0; i < nr_ports; i++) {
  798. bfin_serial_ports[i].port.uartclk = get_sclk();
  799. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  800. bfin_serial_ports[i].port.line = i;
  801. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  802. bfin_serial_ports[i].port.membase =
  803. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  804. bfin_serial_ports[i].port.mapbase =
  805. bfin_serial_resource[i].uart_base_addr;
  806. bfin_serial_ports[i].port.irq =
  807. bfin_serial_resource[i].uart_irq;
  808. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  809. #ifdef CONFIG_SERIAL_BFIN_DMA
  810. bfin_serial_ports[i].tx_done = 1;
  811. bfin_serial_ports[i].tx_count = 0;
  812. bfin_serial_ports[i].tx_dma_channel =
  813. bfin_serial_resource[i].uart_tx_dma_channel;
  814. bfin_serial_ports[i].rx_dma_channel =
  815. bfin_serial_resource[i].uart_rx_dma_channel;
  816. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  817. #endif
  818. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  819. INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
  820. bfin_serial_ports[i].cts_pin =
  821. bfin_serial_resource[i].uart_cts_pin;
  822. bfin_serial_ports[i].rts_pin =
  823. bfin_serial_resource[i].uart_rts_pin;
  824. #endif
  825. bfin_serial_hw_init(&bfin_serial_ports[i]);
  826. }
  827. }
  828. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  829. /*
  830. * If the port was already initialised (eg, by a boot loader),
  831. * try to determine the current setup.
  832. */
  833. static void __init
  834. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  835. int *parity, int *bits)
  836. {
  837. unsigned short status;
  838. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  839. if (status == (ERBFI | ETBEI)) {
  840. /* ok, the port was enabled */
  841. unsigned short lcr, val;
  842. unsigned short dlh, dll;
  843. lcr = UART_GET_LCR(uart);
  844. *parity = 'n';
  845. if (lcr & PEN) {
  846. if (lcr & EPS)
  847. *parity = 'e';
  848. else
  849. *parity = 'o';
  850. }
  851. switch (lcr & 0x03) {
  852. case 0: *bits = 5; break;
  853. case 1: *bits = 6; break;
  854. case 2: *bits = 7; break;
  855. case 3: *bits = 8; break;
  856. }
  857. #ifndef CONFIG_BF54x
  858. /* Set DLAB in LCR to Access DLL and DLH */
  859. val = UART_GET_LCR(uart);
  860. val |= DLAB;
  861. UART_PUT_LCR(uart, val);
  862. #endif
  863. dll = UART_GET_DLL(uart);
  864. dlh = UART_GET_DLH(uart);
  865. #ifndef CONFIG_BF54x
  866. /* Clear DLAB in LCR to Access THR RBR IER */
  867. val = UART_GET_LCR(uart);
  868. val &= ~DLAB;
  869. UART_PUT_LCR(uart, val);
  870. #endif
  871. *baud = get_sclk() / (16*(dll | dlh << 8));
  872. }
  873. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
  874. }
  875. #endif
  876. #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
  877. static struct uart_driver bfin_serial_reg;
  878. static int __init
  879. bfin_serial_console_setup(struct console *co, char *options)
  880. {
  881. struct bfin_serial_port *uart;
  882. # ifdef CONFIG_SERIAL_BFIN_CONSOLE
  883. int baud = 57600;
  884. int bits = 8;
  885. int parity = 'n';
  886. # ifdef CONFIG_SERIAL_BFIN_CTSRTS
  887. int flow = 'r';
  888. # else
  889. int flow = 'n';
  890. # endif
  891. # endif
  892. /*
  893. * Check whether an invalid uart number has been specified, and
  894. * if so, search for the first available port that does have
  895. * console support.
  896. */
  897. if (co->index == -1 || co->index >= nr_ports)
  898. co->index = 0;
  899. uart = &bfin_serial_ports[co->index];
  900. # ifdef CONFIG_SERIAL_BFIN_CONSOLE
  901. if (options)
  902. uart_parse_options(options, &baud, &parity, &bits, &flow);
  903. else
  904. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  905. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  906. # else
  907. return 0;
  908. # endif
  909. }
  910. #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
  911. defined (CONFIG_EARLY_PRINTK) */
  912. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  913. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  914. {
  915. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  916. while (!(UART_GET_LSR(uart) & THRE))
  917. barrier();
  918. UART_PUT_CHAR(uart, ch);
  919. SSYNC();
  920. }
  921. /*
  922. * Interrupts are disabled on entering
  923. */
  924. static void
  925. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  926. {
  927. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  928. int flags = 0;
  929. spin_lock_irqsave(&uart->port.lock, flags);
  930. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  931. spin_unlock_irqrestore(&uart->port.lock, flags);
  932. }
  933. static struct console bfin_serial_console = {
  934. .name = BFIN_SERIAL_NAME,
  935. .write = bfin_serial_console_write,
  936. .device = uart_console_device,
  937. .setup = bfin_serial_console_setup,
  938. .flags = CON_PRINTBUFFER,
  939. .index = -1,
  940. .data = &bfin_serial_reg,
  941. };
  942. static int __init bfin_serial_rs_console_init(void)
  943. {
  944. bfin_serial_init_ports();
  945. register_console(&bfin_serial_console);
  946. #ifdef CONFIG_KGDB_UART
  947. kgdb_entry_state = 0;
  948. init_kgdb_uart();
  949. #endif
  950. return 0;
  951. }
  952. console_initcall(bfin_serial_rs_console_init);
  953. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  954. #else
  955. #define BFIN_SERIAL_CONSOLE NULL
  956. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  957. #ifdef CONFIG_EARLY_PRINTK
  958. static __init void early_serial_putc(struct uart_port *port, int ch)
  959. {
  960. unsigned timeout = 0xffff;
  961. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  962. while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
  963. cpu_relax();
  964. UART_PUT_CHAR(uart, ch);
  965. }
  966. static __init void early_serial_write(struct console *con, const char *s,
  967. unsigned int n)
  968. {
  969. struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
  970. unsigned int i;
  971. for (i = 0; i < n; i++, s++) {
  972. if (*s == '\n')
  973. early_serial_putc(&uart->port, '\r');
  974. early_serial_putc(&uart->port, *s);
  975. }
  976. }
  977. static struct __init console bfin_early_serial_console = {
  978. .name = "early_BFuart",
  979. .write = early_serial_write,
  980. .device = uart_console_device,
  981. .flags = CON_PRINTBUFFER,
  982. .setup = bfin_serial_console_setup,
  983. .index = -1,
  984. .data = &bfin_serial_reg,
  985. };
  986. struct console __init *bfin_earlyserial_init(unsigned int port,
  987. unsigned int cflag)
  988. {
  989. struct bfin_serial_port *uart;
  990. struct ktermios t;
  991. if (port == -1 || port >= nr_ports)
  992. port = 0;
  993. bfin_serial_init_ports();
  994. bfin_early_serial_console.index = port;
  995. uart = &bfin_serial_ports[port];
  996. t.c_cflag = cflag;
  997. t.c_iflag = 0;
  998. t.c_oflag = 0;
  999. t.c_lflag = ICANON;
  1000. t.c_line = port;
  1001. bfin_serial_set_termios(&uart->port, &t, &t);
  1002. return &bfin_early_serial_console;
  1003. }
  1004. #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
  1005. static struct uart_driver bfin_serial_reg = {
  1006. .owner = THIS_MODULE,
  1007. .driver_name = "bfin-uart",
  1008. .dev_name = BFIN_SERIAL_NAME,
  1009. .major = BFIN_SERIAL_MAJOR,
  1010. .minor = BFIN_SERIAL_MINOR,
  1011. .nr = NR_PORTS,
  1012. .cons = BFIN_SERIAL_CONSOLE,
  1013. };
  1014. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  1015. {
  1016. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  1017. if (uart)
  1018. uart_suspend_port(&bfin_serial_reg, &uart->port);
  1019. return 0;
  1020. }
  1021. static int bfin_serial_resume(struct platform_device *dev)
  1022. {
  1023. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  1024. if (uart)
  1025. uart_resume_port(&bfin_serial_reg, &uart->port);
  1026. return 0;
  1027. }
  1028. static int bfin_serial_probe(struct platform_device *dev)
  1029. {
  1030. struct resource *res = dev->resource;
  1031. int i;
  1032. for (i = 0; i < dev->num_resources; i++, res++)
  1033. if (res->flags & IORESOURCE_MEM)
  1034. break;
  1035. if (i < dev->num_resources) {
  1036. for (i = 0; i < nr_ports; i++, res++) {
  1037. if (bfin_serial_ports[i].port.mapbase != res->start)
  1038. continue;
  1039. bfin_serial_ports[i].port.dev = &dev->dev;
  1040. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  1041. platform_set_drvdata(dev, &bfin_serial_ports[i]);
  1042. }
  1043. }
  1044. return 0;
  1045. }
  1046. static int bfin_serial_remove(struct platform_device *pdev)
  1047. {
  1048. struct bfin_serial_port *uart = platform_get_drvdata(pdev);
  1049. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  1050. gpio_free(uart->cts_pin);
  1051. gpio_free(uart->rts_pin);
  1052. #endif
  1053. platform_set_drvdata(pdev, NULL);
  1054. if (uart)
  1055. uart_remove_one_port(&bfin_serial_reg, &uart->port);
  1056. return 0;
  1057. }
  1058. static struct platform_driver bfin_serial_driver = {
  1059. .probe = bfin_serial_probe,
  1060. .remove = bfin_serial_remove,
  1061. .suspend = bfin_serial_suspend,
  1062. .resume = bfin_serial_resume,
  1063. .driver = {
  1064. .name = "bfin-uart",
  1065. },
  1066. };
  1067. static int __init bfin_serial_init(void)
  1068. {
  1069. int ret;
  1070. #ifdef CONFIG_KGDB_UART
  1071. struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
  1072. struct ktermios t;
  1073. #endif
  1074. pr_info("Serial: Blackfin serial driver\n");
  1075. bfin_serial_init_ports();
  1076. ret = uart_register_driver(&bfin_serial_reg);
  1077. if (ret == 0) {
  1078. ret = platform_driver_register(&bfin_serial_driver);
  1079. if (ret) {
  1080. pr_debug("uart register failed\n");
  1081. uart_unregister_driver(&bfin_serial_reg);
  1082. }
  1083. }
  1084. #ifdef CONFIG_KGDB_UART
  1085. if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
  1086. request_irq(uart->port.irq, bfin_serial_rx_int,
  1087. IRQF_DISABLED, "BFIN_UART_RX", uart);
  1088. pr_info("Request irq for kgdb uart port\n");
  1089. #ifdef CONFIG_BF54x
  1090. UART_SET_IER(uart, ERBFI);
  1091. #else
  1092. UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
  1093. #endif
  1094. SSYNC();
  1095. t.c_cflag = CS8|B57600;
  1096. t.c_iflag = 0;
  1097. t.c_oflag = 0;
  1098. t.c_lflag = ICANON;
  1099. t.c_line = CONFIG_KGDB_UART_PORT;
  1100. bfin_serial_set_termios(&uart->port, &t, &t);
  1101. }
  1102. #endif
  1103. return ret;
  1104. }
  1105. static void __exit bfin_serial_exit(void)
  1106. {
  1107. platform_driver_unregister(&bfin_serial_driver);
  1108. uart_unregister_driver(&bfin_serial_reg);
  1109. }
  1110. module_init(bfin_serial_init);
  1111. module_exit(bfin_serial_exit);
  1112. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  1113. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  1114. MODULE_LICENSE("GPL");
  1115. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);