bfin_5xx.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. #include <asm/gpio.h>
  42. #include <asm/mach/bfin_serial_5xx.h>
  43. #ifdef CONFIG_SERIAL_BFIN_DMA
  44. #include <linux/dma-mapping.h>
  45. #include <asm/io.h>
  46. #include <asm/irq.h>
  47. #include <asm/cacheflush.h>
  48. #endif
  49. /* UART name and device definitions */
  50. #define BFIN_SERIAL_NAME "ttyBF"
  51. #define BFIN_SERIAL_MAJOR 204
  52. #define BFIN_SERIAL_MINOR 64
  53. /*
  54. * Setup for console. Argument comes from the menuconfig
  55. */
  56. #define DMA_RX_XCOUNT 512
  57. #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
  58. #define DMA_RX_FLUSH_JIFFIES 5
  59. #ifdef CONFIG_SERIAL_BFIN_DMA
  60. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
  61. #else
  62. static void bfin_serial_do_work(struct work_struct *work);
  63. static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
  64. static void local_put_char(struct bfin_serial_port *uart, char ch);
  65. #endif
  66. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
  67. /*
  68. * interrupts are disabled on entry
  69. */
  70. static void bfin_serial_stop_tx(struct uart_port *port)
  71. {
  72. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  73. #ifdef CONFIG_SERIAL_BFIN_DMA
  74. disable_dma(uart->tx_dma_channel);
  75. #else
  76. unsigned short ier;
  77. ier = UART_GET_IER(uart);
  78. ier &= ~ETBEI;
  79. UART_PUT_IER(uart, ier);
  80. #endif
  81. }
  82. /*
  83. * port is locked and interrupts are disabled
  84. */
  85. static void bfin_serial_start_tx(struct uart_port *port)
  86. {
  87. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  88. #ifdef CONFIG_SERIAL_BFIN_DMA
  89. bfin_serial_dma_tx_chars(uart);
  90. #else
  91. unsigned short ier;
  92. ier = UART_GET_IER(uart);
  93. ier |= ETBEI;
  94. UART_PUT_IER(uart, ier);
  95. bfin_serial_tx_chars(uart);
  96. #endif
  97. }
  98. /*
  99. * Interrupts are enabled
  100. */
  101. static void bfin_serial_stop_rx(struct uart_port *port)
  102. {
  103. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  104. unsigned short ier;
  105. ier = UART_GET_IER(uart);
  106. ier &= ~ERBFI;
  107. UART_PUT_IER(uart, ier);
  108. }
  109. /*
  110. * Set the modem control timer to fire immediately.
  111. */
  112. static void bfin_serial_enable_ms(struct uart_port *port)
  113. {
  114. }
  115. #ifdef CONFIG_SERIAL_BFIN_PIO
  116. static void local_put_char(struct bfin_serial_port *uart, char ch)
  117. {
  118. unsigned short status;
  119. int flags = 0;
  120. spin_lock_irqsave(&uart->port.lock, flags);
  121. do {
  122. status = UART_GET_LSR(uart);
  123. } while (!(status & THRE));
  124. UART_PUT_CHAR(uart, ch);
  125. SSYNC();
  126. spin_unlock_irqrestore(&uart->port.lock, flags);
  127. }
  128. static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
  129. {
  130. struct tty_struct *tty = uart->port.info->tty;
  131. unsigned int status, ch, flg;
  132. #ifdef BF533_FAMILY
  133. static int in_break = 0;
  134. #endif
  135. status = UART_GET_LSR(uart);
  136. ch = UART_GET_CHAR(uart);
  137. uart->port.icount.rx++;
  138. #ifdef BF533_FAMILY
  139. /* The BF533 family of processors have a nice misbehavior where
  140. * they continuously generate characters for a "single" break.
  141. * We have to basically ignore this flood until the "next" valid
  142. * character comes across. All other Blackfin families operate
  143. * properly though.
  144. */
  145. if (in_break) {
  146. if (ch != 0) {
  147. in_break = 0;
  148. ch = UART_GET_CHAR(uart);
  149. if (bfin_revid() < 5)
  150. return;
  151. } else
  152. return;
  153. }
  154. #endif
  155. if (status & BI) {
  156. #ifdef BF533_FAMILY
  157. in_break = 1;
  158. #endif
  159. uart->port.icount.brk++;
  160. if (uart_handle_break(&uart->port))
  161. goto ignore_char;
  162. }
  163. if (status & PE)
  164. uart->port.icount.parity++;
  165. if (status & OE)
  166. uart->port.icount.overrun++;
  167. if (status & FE)
  168. uart->port.icount.frame++;
  169. status &= uart->port.read_status_mask;
  170. if (status & BI)
  171. flg = TTY_BREAK;
  172. else if (status & PE)
  173. flg = TTY_PARITY;
  174. else if (status & FE)
  175. flg = TTY_FRAME;
  176. else
  177. flg = TTY_NORMAL;
  178. if (uart_handle_sysrq_char(&uart->port, ch))
  179. goto ignore_char;
  180. uart_insert_char(&uart->port, status, OE, ch, flg);
  181. ignore_char:
  182. tty_flip_buffer_push(tty);
  183. }
  184. static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
  185. {
  186. struct circ_buf *xmit = &uart->port.info->xmit;
  187. if (uart->port.x_char) {
  188. UART_PUT_CHAR(uart, uart->port.x_char);
  189. uart->port.icount.tx++;
  190. uart->port.x_char = 0;
  191. return;
  192. }
  193. /*
  194. * Check the modem control lines before
  195. * transmitting anything.
  196. */
  197. bfin_serial_mctrl_check(uart);
  198. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  199. bfin_serial_stop_tx(&uart->port);
  200. return;
  201. }
  202. local_put_char(uart, xmit->buf[xmit->tail]);
  203. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  204. uart->port.icount.tx++;
  205. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  206. uart_write_wakeup(&uart->port);
  207. if (uart_circ_empty(xmit))
  208. bfin_serial_stop_tx(&uart->port);
  209. }
  210. static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
  211. {
  212. struct bfin_serial_port *uart = dev_id;
  213. spin_lock(&uart->port.lock);
  214. while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_RX_READY)
  215. bfin_serial_rx_chars(uart);
  216. spin_unlock(&uart->port.lock);
  217. return IRQ_HANDLED;
  218. }
  219. static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
  220. {
  221. struct bfin_serial_port *uart = dev_id;
  222. spin_lock(&uart->port.lock);
  223. while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_TX_READY)
  224. bfin_serial_tx_chars(uart);
  225. spin_unlock(&uart->port.lock);
  226. return IRQ_HANDLED;
  227. }
  228. static void bfin_serial_do_work(struct work_struct *work)
  229. {
  230. struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
  231. bfin_serial_mctrl_check(uart);
  232. }
  233. #endif
  234. #ifdef CONFIG_SERIAL_BFIN_DMA
  235. static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
  236. {
  237. struct circ_buf *xmit = &uart->port.info->xmit;
  238. unsigned short ier;
  239. int flags = 0;
  240. if (!uart->tx_done)
  241. return;
  242. uart->tx_done = 0;
  243. if (uart->port.x_char) {
  244. UART_PUT_CHAR(uart, uart->port.x_char);
  245. uart->port.icount.tx++;
  246. uart->port.x_char = 0;
  247. uart->tx_done = 1;
  248. return;
  249. }
  250. /*
  251. * Check the modem control lines before
  252. * transmitting anything.
  253. */
  254. bfin_serial_mctrl_check(uart);
  255. if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
  256. bfin_serial_stop_tx(&uart->port);
  257. uart->tx_done = 1;
  258. return;
  259. }
  260. spin_lock_irqsave(&uart->port.lock, flags);
  261. uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
  262. if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
  263. uart->tx_count = UART_XMIT_SIZE - xmit->tail;
  264. blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
  265. (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
  266. set_dma_config(uart->tx_dma_channel,
  267. set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
  268. INTR_ON_BUF,
  269. DIMENSION_LINEAR,
  270. DATA_SIZE_8));
  271. set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
  272. set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
  273. set_dma_x_modify(uart->tx_dma_channel, 1);
  274. enable_dma(uart->tx_dma_channel);
  275. ier = UART_GET_IER(uart);
  276. ier |= ETBEI;
  277. UART_PUT_IER(uart, ier);
  278. spin_unlock_irqrestore(&uart->port.lock, flags);
  279. }
  280. static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
  281. {
  282. struct tty_struct *tty = uart->port.info->tty;
  283. int i, flg, status;
  284. status = UART_GET_LSR(uart);
  285. uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
  286. if (status & BI) {
  287. uart->port.icount.brk++;
  288. if (uart_handle_break(&uart->port))
  289. goto dma_ignore_char;
  290. }
  291. if (status & PE)
  292. uart->port.icount.parity++;
  293. if (status & OE)
  294. uart->port.icount.overrun++;
  295. if (status & FE)
  296. uart->port.icount.frame++;
  297. status &= uart->port.read_status_mask;
  298. if (status & BI)
  299. flg = TTY_BREAK;
  300. else if (status & PE)
  301. flg = TTY_PARITY;
  302. else if (status & FE)
  303. flg = TTY_FRAME;
  304. else
  305. flg = TTY_NORMAL;
  306. for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
  307. if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
  308. goto dma_ignore_char;
  309. uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
  310. }
  311. dma_ignore_char:
  312. tty_flip_buffer_push(tty);
  313. }
  314. void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
  315. {
  316. int x_pos, pos;
  317. int flags = 0;
  318. bfin_serial_dma_tx_chars(uart);
  319. spin_lock_irqsave(&uart->port.lock, flags);
  320. x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
  321. if (x_pos == DMA_RX_XCOUNT)
  322. x_pos = 0;
  323. pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
  324. if (pos>uart->rx_dma_buf.tail) {
  325. uart->rx_dma_buf.tail = pos;
  326. bfin_serial_dma_rx_chars(uart);
  327. uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
  328. }
  329. spin_unlock_irqrestore(&uart->port.lock, flags);
  330. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  331. add_timer(&(uart->rx_dma_timer));
  332. }
  333. static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
  334. {
  335. struct bfin_serial_port *uart = dev_id;
  336. struct circ_buf *xmit = &uart->port.info->xmit;
  337. unsigned short ier;
  338. spin_lock(&uart->port.lock);
  339. if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
  340. clear_dma_irqstat(uart->tx_dma_channel);
  341. disable_dma(uart->tx_dma_channel);
  342. ier = UART_GET_IER(uart);
  343. ier &= ~ETBEI;
  344. UART_PUT_IER(uart, ier);
  345. xmit->tail = (xmit->tail+uart->tx_count) &(UART_XMIT_SIZE -1);
  346. uart->port.icount.tx+=uart->tx_count;
  347. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  348. uart_write_wakeup(&uart->port);
  349. if (uart_circ_empty(xmit))
  350. bfin_serial_stop_tx(&uart->port);
  351. uart->tx_done = 1;
  352. }
  353. spin_unlock(&uart->port.lock);
  354. return IRQ_HANDLED;
  355. }
  356. static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
  357. {
  358. struct bfin_serial_port *uart = dev_id;
  359. unsigned short irqstat;
  360. uart->rx_dma_nrows++;
  361. if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
  362. uart->rx_dma_nrows = 0;
  363. uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
  364. bfin_serial_dma_rx_chars(uart);
  365. uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
  366. }
  367. spin_lock(&uart->port.lock);
  368. irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
  369. clear_dma_irqstat(uart->rx_dma_channel);
  370. spin_unlock(&uart->port.lock);
  371. return IRQ_HANDLED;
  372. }
  373. #endif
  374. /*
  375. * Return TIOCSER_TEMT when transmitter is not busy.
  376. */
  377. static unsigned int bfin_serial_tx_empty(struct uart_port *port)
  378. {
  379. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  380. unsigned short lsr;
  381. lsr = UART_GET_LSR(uart);
  382. if (lsr & TEMT)
  383. return TIOCSER_TEMT;
  384. else
  385. return 0;
  386. }
  387. static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
  388. {
  389. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  390. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  391. if (uart->cts_pin < 0)
  392. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  393. if (gpio_get_value(uart->cts_pin))
  394. return TIOCM_DSR | TIOCM_CAR;
  395. else
  396. #endif
  397. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  398. }
  399. static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  400. {
  401. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  402. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  403. if (uart->rts_pin < 0)
  404. return;
  405. if (mctrl & TIOCM_RTS)
  406. gpio_set_value(uart->rts_pin, 0);
  407. else
  408. gpio_set_value(uart->rts_pin, 1);
  409. #endif
  410. }
  411. /*
  412. * Handle any change of modem status signal since we were last called.
  413. */
  414. static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  415. {
  416. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  417. unsigned int status;
  418. # ifdef CONFIG_SERIAL_BFIN_DMA
  419. struct uart_info *info = uart->port.info;
  420. struct tty_struct *tty = info->tty;
  421. status = bfin_serial_get_mctrl(&uart->port);
  422. if (!(status & TIOCM_CTS)) {
  423. tty->hw_stopped = 1;
  424. } else {
  425. tty->hw_stopped = 0;
  426. }
  427. # else
  428. status = bfin_serial_get_mctrl(&uart->port);
  429. uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
  430. if (!(status & TIOCM_CTS))
  431. schedule_work(&uart->cts_workqueue);
  432. # endif
  433. #endif
  434. }
  435. /*
  436. * Interrupts are always disabled.
  437. */
  438. static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
  439. {
  440. }
  441. static int bfin_serial_startup(struct uart_port *port)
  442. {
  443. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  444. #ifdef CONFIG_SERIAL_BFIN_DMA
  445. dma_addr_t dma_handle;
  446. if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
  447. printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
  448. return -EBUSY;
  449. }
  450. if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
  451. printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
  452. free_dma(uart->rx_dma_channel);
  453. return -EBUSY;
  454. }
  455. set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
  456. set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
  457. uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
  458. uart->rx_dma_buf.head = 0;
  459. uart->rx_dma_buf.tail = 0;
  460. uart->rx_dma_nrows = 0;
  461. set_dma_config(uart->rx_dma_channel,
  462. set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
  463. INTR_ON_ROW, DIMENSION_2D,
  464. DATA_SIZE_8));
  465. set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
  466. set_dma_x_modify(uart->rx_dma_channel, 1);
  467. set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
  468. set_dma_y_modify(uart->rx_dma_channel, 1);
  469. set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
  470. enable_dma(uart->rx_dma_channel);
  471. uart->rx_dma_timer.data = (unsigned long)(uart);
  472. uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
  473. uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
  474. add_timer(&(uart->rx_dma_timer));
  475. #else
  476. if (request_irq
  477. (uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
  478. "BFIN_UART_RX", uart)) {
  479. printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
  480. return -EBUSY;
  481. }
  482. if (request_irq
  483. (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
  484. "BFIN_UART_TX", uart)) {
  485. printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
  486. free_irq(uart->port.irq, uart);
  487. return -EBUSY;
  488. }
  489. #endif
  490. UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
  491. return 0;
  492. }
  493. static void bfin_serial_shutdown(struct uart_port *port)
  494. {
  495. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  496. #ifdef CONFIG_SERIAL_BFIN_DMA
  497. disable_dma(uart->tx_dma_channel);
  498. free_dma(uart->tx_dma_channel);
  499. disable_dma(uart->rx_dma_channel);
  500. free_dma(uart->rx_dma_channel);
  501. del_timer(&(uart->rx_dma_timer));
  502. #else
  503. free_irq(uart->port.irq, uart);
  504. free_irq(uart->port.irq+1, uart);
  505. #endif
  506. }
  507. static void
  508. bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
  509. struct ktermios *old)
  510. {
  511. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  512. unsigned long flags;
  513. unsigned int baud, quot;
  514. unsigned short val, ier, lsr, lcr = 0;
  515. switch (termios->c_cflag & CSIZE) {
  516. case CS8:
  517. lcr = WLS(8);
  518. break;
  519. case CS7:
  520. lcr = WLS(7);
  521. break;
  522. case CS6:
  523. lcr = WLS(6);
  524. break;
  525. case CS5:
  526. lcr = WLS(5);
  527. break;
  528. default:
  529. printk(KERN_ERR "%s: word lengh not supported\n",
  530. __FUNCTION__);
  531. }
  532. if (termios->c_cflag & CSTOPB)
  533. lcr |= STB;
  534. if (termios->c_cflag & PARENB) {
  535. lcr |= PEN;
  536. if (!(termios->c_cflag & PARODD))
  537. lcr |= EPS;
  538. }
  539. port->read_status_mask = OE;
  540. if (termios->c_iflag & INPCK)
  541. port->read_status_mask |= (FE | PE);
  542. if (termios->c_iflag & (BRKINT | PARMRK))
  543. port->read_status_mask |= BI;
  544. /*
  545. * Characters to ignore
  546. */
  547. port->ignore_status_mask = 0;
  548. if (termios->c_iflag & IGNPAR)
  549. port->ignore_status_mask |= FE | PE;
  550. if (termios->c_iflag & IGNBRK) {
  551. port->ignore_status_mask |= BI;
  552. /*
  553. * If we're ignoring parity and break indicators,
  554. * ignore overruns too (for real raw support).
  555. */
  556. if (termios->c_iflag & IGNPAR)
  557. port->ignore_status_mask |= OE;
  558. }
  559. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  560. quot = uart_get_divisor(port, baud);
  561. spin_lock_irqsave(&uart->port.lock, flags);
  562. do {
  563. lsr = UART_GET_LSR(uart);
  564. } while (!(lsr & TEMT));
  565. /* Disable UART */
  566. ier = UART_GET_IER(uart);
  567. UART_PUT_IER(uart, 0);
  568. /* Set DLAB in LCR to Access DLL and DLH */
  569. val = UART_GET_LCR(uart);
  570. val |= DLAB;
  571. UART_PUT_LCR(uart, val);
  572. SSYNC();
  573. UART_PUT_DLL(uart, quot & 0xFF);
  574. SSYNC();
  575. UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
  576. SSYNC();
  577. /* Clear DLAB in LCR to Access THR RBR IER */
  578. val = UART_GET_LCR(uart);
  579. val &= ~DLAB;
  580. UART_PUT_LCR(uart, val);
  581. SSYNC();
  582. UART_PUT_LCR(uart, lcr);
  583. /* Enable UART */
  584. UART_PUT_IER(uart, ier);
  585. val = UART_GET_GCTL(uart);
  586. val |= UCEN;
  587. UART_PUT_GCTL(uart, val);
  588. spin_unlock_irqrestore(&uart->port.lock, flags);
  589. }
  590. static const char *bfin_serial_type(struct uart_port *port)
  591. {
  592. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  593. return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
  594. }
  595. /*
  596. * Release the memory region(s) being used by 'port'.
  597. */
  598. static void bfin_serial_release_port(struct uart_port *port)
  599. {
  600. }
  601. /*
  602. * Request the memory region(s) being used by 'port'.
  603. */
  604. static int bfin_serial_request_port(struct uart_port *port)
  605. {
  606. return 0;
  607. }
  608. /*
  609. * Configure/autoconfigure the port.
  610. */
  611. static void bfin_serial_config_port(struct uart_port *port, int flags)
  612. {
  613. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  614. if (flags & UART_CONFIG_TYPE &&
  615. bfin_serial_request_port(&uart->port) == 0)
  616. uart->port.type = PORT_BFIN;
  617. }
  618. /*
  619. * Verify the new serial_struct (for TIOCSSERIAL).
  620. * The only change we allow are to the flags and type, and
  621. * even then only between PORT_BFIN and PORT_UNKNOWN
  622. */
  623. static int
  624. bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  625. {
  626. return 0;
  627. }
  628. static struct uart_ops bfin_serial_pops = {
  629. .tx_empty = bfin_serial_tx_empty,
  630. .set_mctrl = bfin_serial_set_mctrl,
  631. .get_mctrl = bfin_serial_get_mctrl,
  632. .stop_tx = bfin_serial_stop_tx,
  633. .start_tx = bfin_serial_start_tx,
  634. .stop_rx = bfin_serial_stop_rx,
  635. .enable_ms = bfin_serial_enable_ms,
  636. .break_ctl = bfin_serial_break_ctl,
  637. .startup = bfin_serial_startup,
  638. .shutdown = bfin_serial_shutdown,
  639. .set_termios = bfin_serial_set_termios,
  640. .type = bfin_serial_type,
  641. .release_port = bfin_serial_release_port,
  642. .request_port = bfin_serial_request_port,
  643. .config_port = bfin_serial_config_port,
  644. .verify_port = bfin_serial_verify_port,
  645. };
  646. static void __init bfin_serial_init_ports(void)
  647. {
  648. static int first = 1;
  649. int i;
  650. if (!first)
  651. return;
  652. first = 0;
  653. for (i = 0; i < nr_ports; i++) {
  654. bfin_serial_ports[i].port.uartclk = get_sclk();
  655. bfin_serial_ports[i].port.ops = &bfin_serial_pops;
  656. bfin_serial_ports[i].port.line = i;
  657. bfin_serial_ports[i].port.iotype = UPIO_MEM;
  658. bfin_serial_ports[i].port.membase =
  659. (void __iomem *)bfin_serial_resource[i].uart_base_addr;
  660. bfin_serial_ports[i].port.mapbase =
  661. bfin_serial_resource[i].uart_base_addr;
  662. bfin_serial_ports[i].port.irq =
  663. bfin_serial_resource[i].uart_irq;
  664. bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
  665. #ifdef CONFIG_SERIAL_BFIN_DMA
  666. bfin_serial_ports[i].tx_done = 1;
  667. bfin_serial_ports[i].tx_count = 0;
  668. bfin_serial_ports[i].tx_dma_channel =
  669. bfin_serial_resource[i].uart_tx_dma_channel;
  670. bfin_serial_ports[i].rx_dma_channel =
  671. bfin_serial_resource[i].uart_rx_dma_channel;
  672. init_timer(&(bfin_serial_ports[i].rx_dma_timer));
  673. #else
  674. INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
  675. #endif
  676. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  677. bfin_serial_ports[i].cts_pin =
  678. bfin_serial_resource[i].uart_cts_pin;
  679. bfin_serial_ports[i].rts_pin =
  680. bfin_serial_resource[i].uart_rts_pin;
  681. #endif
  682. bfin_serial_hw_init(&bfin_serial_ports[i]);
  683. }
  684. }
  685. #ifdef CONFIG_SERIAL_BFIN_CONSOLE
  686. static void bfin_serial_console_putchar(struct uart_port *port, int ch)
  687. {
  688. struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
  689. while (!(UART_GET_LSR(uart)))
  690. barrier();
  691. UART_PUT_CHAR(uart, ch);
  692. SSYNC();
  693. }
  694. /*
  695. * Interrupts are disabled on entering
  696. */
  697. static void
  698. bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
  699. {
  700. struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
  701. int flags = 0;
  702. spin_lock_irqsave(&uart->port.lock, flags);
  703. uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
  704. spin_unlock_irqrestore(&uart->port.lock, flags);
  705. }
  706. /*
  707. * If the port was already initialised (eg, by a boot loader),
  708. * try to determine the current setup.
  709. */
  710. static void __init
  711. bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
  712. int *parity, int *bits)
  713. {
  714. unsigned short status;
  715. status = UART_GET_IER(uart) & (ERBFI | ETBEI);
  716. if (status == (ERBFI | ETBEI)) {
  717. /* ok, the port was enabled */
  718. unsigned short lcr, val;
  719. unsigned short dlh, dll;
  720. lcr = UART_GET_LCR(uart);
  721. *parity = 'n';
  722. if (lcr & PEN) {
  723. if (lcr & EPS)
  724. *parity = 'e';
  725. else
  726. *parity = 'o';
  727. }
  728. switch (lcr & 0x03) {
  729. case 0: *bits = 5; break;
  730. case 1: *bits = 6; break;
  731. case 2: *bits = 7; break;
  732. case 3: *bits = 8; break;
  733. }
  734. /* Set DLAB in LCR to Access DLL and DLH */
  735. val = UART_GET_LCR(uart);
  736. val |= DLAB;
  737. UART_PUT_LCR(uart, val);
  738. dll = UART_GET_DLL(uart);
  739. dlh = UART_GET_DLH(uart);
  740. /* Clear DLAB in LCR to Access THR RBR IER */
  741. val = UART_GET_LCR(uart);
  742. val &= ~DLAB;
  743. UART_PUT_LCR(uart, val);
  744. *baud = get_sclk() / (16*(dll | dlh << 8));
  745. }
  746. pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
  747. }
  748. static int __init
  749. bfin_serial_console_setup(struct console *co, char *options)
  750. {
  751. struct bfin_serial_port *uart;
  752. int baud = 57600;
  753. int bits = 8;
  754. int parity = 'n';
  755. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  756. int flow = 'r';
  757. #else
  758. int flow = 'n';
  759. #endif
  760. /*
  761. * Check whether an invalid uart number has been specified, and
  762. * if so, search for the first available port that does have
  763. * console support.
  764. */
  765. if (co->index == -1 || co->index >= nr_ports)
  766. co->index = 0;
  767. uart = &bfin_serial_ports[co->index];
  768. if (options)
  769. uart_parse_options(options, &baud, &parity, &bits, &flow);
  770. else
  771. bfin_serial_console_get_options(uart, &baud, &parity, &bits);
  772. return uart_set_options(&uart->port, co, baud, parity, bits, flow);
  773. }
  774. static struct uart_driver bfin_serial_reg;
  775. static struct console bfin_serial_console = {
  776. .name = BFIN_SERIAL_NAME,
  777. .write = bfin_serial_console_write,
  778. .device = uart_console_device,
  779. .setup = bfin_serial_console_setup,
  780. .flags = CON_PRINTBUFFER,
  781. .index = -1,
  782. .data = &bfin_serial_reg,
  783. };
  784. static int __init bfin_serial_rs_console_init(void)
  785. {
  786. bfin_serial_init_ports();
  787. register_console(&bfin_serial_console);
  788. return 0;
  789. }
  790. console_initcall(bfin_serial_rs_console_init);
  791. #define BFIN_SERIAL_CONSOLE &bfin_serial_console
  792. #else
  793. #define BFIN_SERIAL_CONSOLE NULL
  794. #endif
  795. static struct uart_driver bfin_serial_reg = {
  796. .owner = THIS_MODULE,
  797. .driver_name = "bfin-uart",
  798. .dev_name = BFIN_SERIAL_NAME,
  799. .major = BFIN_SERIAL_MAJOR,
  800. .minor = BFIN_SERIAL_MINOR,
  801. .nr = NR_PORTS,
  802. .cons = BFIN_SERIAL_CONSOLE,
  803. };
  804. static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
  805. {
  806. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  807. if (uart)
  808. uart_suspend_port(&bfin_serial_reg, &uart->port);
  809. return 0;
  810. }
  811. static int bfin_serial_resume(struct platform_device *dev)
  812. {
  813. struct bfin_serial_port *uart = platform_get_drvdata(dev);
  814. if (uart)
  815. uart_resume_port(&bfin_serial_reg, &uart->port);
  816. return 0;
  817. }
  818. static int bfin_serial_probe(struct platform_device *dev)
  819. {
  820. struct resource *res = dev->resource;
  821. int i;
  822. for (i = 0; i < dev->num_resources; i++, res++)
  823. if (res->flags & IORESOURCE_MEM)
  824. break;
  825. if (i < dev->num_resources) {
  826. for (i = 0; i < nr_ports; i++, res++) {
  827. if (bfin_serial_ports[i].port.mapbase != res->start)
  828. continue;
  829. bfin_serial_ports[i].port.dev = &dev->dev;
  830. uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
  831. platform_set_drvdata(dev, &bfin_serial_ports[i]);
  832. }
  833. }
  834. return 0;
  835. }
  836. static int bfin_serial_remove(struct platform_device *pdev)
  837. {
  838. struct bfin_serial_port *uart = platform_get_drvdata(pdev);
  839. #ifdef CONFIG_SERIAL_BFIN_CTSRTS
  840. gpio_free(uart->cts_pin);
  841. gpio_free(uart->rts_pin);
  842. #endif
  843. platform_set_drvdata(pdev, NULL);
  844. if (uart)
  845. uart_remove_one_port(&bfin_serial_reg, &uart->port);
  846. return 0;
  847. }
  848. static struct platform_driver bfin_serial_driver = {
  849. .probe = bfin_serial_probe,
  850. .remove = bfin_serial_remove,
  851. .suspend = bfin_serial_suspend,
  852. .resume = bfin_serial_resume,
  853. .driver = {
  854. .name = "bfin-uart",
  855. },
  856. };
  857. static int __init bfin_serial_init(void)
  858. {
  859. int ret;
  860. pr_info("Serial: Blackfin serial driver\n");
  861. bfin_serial_init_ports();
  862. ret = uart_register_driver(&bfin_serial_reg);
  863. if (ret == 0) {
  864. ret = platform_driver_register(&bfin_serial_driver);
  865. if (ret) {
  866. pr_debug("uart register failed\n");
  867. uart_unregister_driver(&bfin_serial_reg);
  868. }
  869. }
  870. return ret;
  871. }
  872. static void __exit bfin_serial_exit(void)
  873. {
  874. platform_driver_unregister(&bfin_serial_driver);
  875. uart_unregister_driver(&bfin_serial_reg);
  876. }
  877. module_init(bfin_serial_init);
  878. module_exit(bfin_serial_exit);
  879. MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
  880. MODULE_DESCRIPTION("Blackfin generic serial port driver");
  881. MODULE_LICENSE("GPL");
  882. MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);