bfin_5xx.c 32 KB

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