serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * (C) Copyright 2000
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*------------------------------------------------------------------------------+ */
  27. /*
  28. * This source code is dual-licensed. You may use it under the terms of the
  29. * GNU General Public License version 2, or under the license below.
  30. *
  31. * This source code has been made available to you by IBM on an AS-IS
  32. * basis. Anyone receiving this source is licensed under IBM
  33. * copyrights to use it in any way he or she deems fit, including
  34. * copying it, modifying it, compiling it, and redistributing it either
  35. * with or without modifications. No license under IBM patents or
  36. * patent applications is to be implied by the copyright license.
  37. *
  38. * Any user of this software should understand that IBM cannot provide
  39. * technical support for this software and will not be responsible for
  40. * any consequences resulting from the use of this software.
  41. *
  42. * Any person who transfers this source code or any derivative work
  43. * must include the IBM copyright notice, this paragraph, and the
  44. * preceding two paragraphs in the transferred software.
  45. *
  46. * COPYRIGHT I B M CORPORATION 1995
  47. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  48. */
  49. /*------------------------------------------------------------------------------- */
  50. #include <common.h>
  51. #include <watchdog.h>
  52. #include <asm/io.h>
  53. #include <asm/ibmpc.h>
  54. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  55. #include <malloc.h>
  56. #endif
  57. DECLARE_GLOBAL_DATA_PTR;
  58. #define UART_RBR 0x00
  59. #define UART_THR 0x00
  60. #define UART_IER 0x01
  61. #define UART_IIR 0x02
  62. #define UART_FCR 0x02
  63. #define UART_LCR 0x03
  64. #define UART_MCR 0x04
  65. #define UART_LSR 0x05
  66. #define UART_MSR 0x06
  67. #define UART_SCR 0x07
  68. #define UART_DLL 0x00
  69. #define UART_DLM 0x01
  70. /*-----------------------------------------------------------------------------+
  71. | Line Status Register.
  72. +-----------------------------------------------------------------------------*/
  73. #define asyncLSRDataReady1 0x01
  74. #define asyncLSROverrunError1 0x02
  75. #define asyncLSRParityError1 0x04
  76. #define asyncLSRFramingError1 0x08
  77. #define asyncLSRBreakInterrupt1 0x10
  78. #define asyncLSRTxHoldEmpty1 0x20
  79. #define asyncLSRTxShiftEmpty1 0x40
  80. #define asyncLSRRxFifoError1 0x80
  81. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  82. /*-----------------------------------------------------------------------------+
  83. | Fifo
  84. +-----------------------------------------------------------------------------*/
  85. typedef struct {
  86. char *rx_buffer;
  87. ulong rx_put;
  88. ulong rx_get;
  89. int cts;
  90. } serial_buffer_t;
  91. volatile serial_buffer_t buf_info;
  92. static int serial_buffer_active=0;
  93. #endif
  94. static int serial_div(int baudrate)
  95. {
  96. switch (baudrate) {
  97. case 1200:
  98. return 96;
  99. case 9600:
  100. return 12;
  101. case 19200:
  102. return 6;
  103. case 38400:
  104. return 3;
  105. case 57600:
  106. return 2;
  107. case 115200:
  108. return 1;
  109. }
  110. return 12;
  111. }
  112. /*
  113. * Minimal serial functions needed to use one of the SMC ports
  114. * as serial console interface.
  115. */
  116. int serial_init(void)
  117. {
  118. volatile char val;
  119. int bdiv = serial_div(gd->baudrate);
  120. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  121. outb(bdiv, UART0_BASE + UART_DLL); /* set baudrate divisor */
  122. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  123. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  124. outb(0x01, UART0_BASE + UART_FCR); /* enable FIFO */
  125. outb(0x0b, UART0_BASE + UART_MCR); /* Set DTR and RTS active */
  126. val = inb(UART0_BASE + UART_LSR); /* clear line status */
  127. val = inb(UART0_BASE + UART_RBR); /* read receive buffer */
  128. outb(0x00, UART0_BASE + UART_SCR); /* set scratchpad */
  129. outb(0x00, UART0_BASE + UART_IER); /* set interrupt enable reg */
  130. return 0;
  131. }
  132. void serial_setbrg(void)
  133. {
  134. unsigned short bdiv;
  135. bdiv = serial_div(gd->baudrate);
  136. outb(0x80, UART0_BASE + UART_LCR); /* set DLAB bit */
  137. outb(bdiv&0xff, UART0_BASE + UART_DLL); /* set baudrate divisor */
  138. outb(bdiv >> 8, UART0_BASE + UART_DLM);/* set baudrate divisor */
  139. outb(0x03, UART0_BASE + UART_LCR); /* clear DLAB; set 8 bits, no parity */
  140. }
  141. void serial_putc(const char c)
  142. {
  143. int i;
  144. if (c == '\n')
  145. serial_putc ('\r');
  146. /* check THRE bit, wait for transmiter available */
  147. for (i = 1; i < 3500; i++) {
  148. if ((inb (UART0_BASE + UART_LSR) & 0x20) == 0x20) {
  149. break;
  150. }
  151. udelay(100);
  152. }
  153. outb(c, UART0_BASE + UART_THR); /* put character out */
  154. }
  155. void serial_puts(const char *s)
  156. {
  157. while (*s) {
  158. serial_putc(*s++);
  159. }
  160. }
  161. int serial_getc(void)
  162. {
  163. unsigned char status = 0;
  164. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  165. if (serial_buffer_active) {
  166. return serial_buffered_getc();
  167. }
  168. #endif
  169. while (1) {
  170. #if defined(CONFIG_HW_WATCHDOG)
  171. WATCHDOG_RESET(); /* Reset HW Watchdog, if needed */
  172. #endif /* CONFIG_HW_WATCHDOG */
  173. status = inb(UART0_BASE + UART_LSR);
  174. if ((status & asyncLSRDataReady1) != 0x0) {
  175. break;
  176. }
  177. if ((status & ( asyncLSRFramingError1 |
  178. asyncLSROverrunError1 |
  179. asyncLSRParityError1 |
  180. asyncLSRBreakInterrupt1 )) != 0) {
  181. outb(asyncLSRFramingError1 |
  182. asyncLSROverrunError1 |
  183. asyncLSRParityError1 |
  184. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  185. }
  186. }
  187. return (0x000000ff & (int) inb (UART0_BASE));
  188. }
  189. int serial_tstc(void)
  190. {
  191. unsigned char status;
  192. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  193. if (serial_buffer_active) {
  194. return serial_buffered_tstc();
  195. }
  196. #endif
  197. status = inb(UART0_BASE + UART_LSR);
  198. if ((status & asyncLSRDataReady1) != 0x0) {
  199. return (1);
  200. }
  201. if ((status & ( asyncLSRFramingError1 |
  202. asyncLSROverrunError1 |
  203. asyncLSRParityError1 |
  204. asyncLSRBreakInterrupt1 )) != 0) {
  205. outb(asyncLSRFramingError1 |
  206. asyncLSROverrunError1 |
  207. asyncLSRParityError1 |
  208. asyncLSRBreakInterrupt1, UART0_BASE + UART_LSR);
  209. }
  210. return 0;
  211. }
  212. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  213. void serial_isr(void *arg)
  214. {
  215. int space;
  216. int c;
  217. int rx_put = buf_info.rx_put;
  218. if (buf_info.rx_get <= rx_put) {
  219. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - buf_info.rx_get);
  220. } else {
  221. space = buf_info.rx_get - rx_put;
  222. }
  223. while (inb(UART0_BASE + UART_LSR) & 1) {
  224. c = inb(UART0_BASE);
  225. if (space) {
  226. buf_info.rx_buffer[rx_put++] = c;
  227. space--;
  228. if (rx_put == buf_info.rx_get) {
  229. buf_info.rx_get++;
  230. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO) {
  231. buf_info.rx_get = 0;
  232. }
  233. }
  234. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO) {
  235. rx_put = 0;
  236. if (0 == buf_info.rx_get) {
  237. buf_info.rx_get = 1;
  238. }
  239. }
  240. }
  241. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  242. /* Stop flow by setting RTS inactive */
  243. outb(inb(UART0_BASE + UART_MCR) & (0xFF ^ 0x02),
  244. UART0_BASE + UART_MCR);
  245. }
  246. }
  247. buf_info.rx_put = rx_put;
  248. }
  249. void serial_buffered_init(void)
  250. {
  251. serial_puts ("Switching to interrupt driven serial input mode.\n");
  252. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  253. buf_info.rx_put = 0;
  254. buf_info.rx_get = 0;
  255. if (inb (UART0_BASE + UART_MSR) & 0x10) {
  256. serial_puts ("Check CTS signal present on serial port: OK.\n");
  257. buf_info.cts = 1;
  258. } else {
  259. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  260. buf_info.cts = 0;
  261. }
  262. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  263. serial_isr /*interrupt_handler_t *handler */ ,
  264. (void *) &buf_info /*void *arg */ );
  265. /* Enable "RX Data Available" Interrupt on UART */
  266. /* outb(inb(UART0_BASE + UART_IER) |0x01, UART0_BASE + UART_IER); */
  267. outb(0x01, UART0_BASE + UART_IER);
  268. /* Set DTR and RTS active, enable interrupts */
  269. outb(inb (UART0_BASE + UART_MCR) | 0x0b, UART0_BASE + UART_MCR);
  270. /* Setup UART FIFO: RX trigger level: 1 byte, Enable FIFO */
  271. outb( /*(1 << 6) |*/ 1, UART0_BASE + UART_FCR);
  272. serial_buffer_active = 1;
  273. }
  274. void serial_buffered_putc (const char c)
  275. {
  276. int i;
  277. /* Wait for CTS */
  278. #if defined(CONFIG_HW_WATCHDOG)
  279. while (!(inb (UART0_BASE + UART_MSR) & 0x10))
  280. WATCHDOG_RESET ();
  281. #else
  282. if (buf_info.cts) {
  283. for (i=0;i<1000;i++) {
  284. if ((inb (UART0_BASE + UART_MSR) & 0x10)) {
  285. break;
  286. }
  287. }
  288. if (i!=1000) {
  289. buf_info.cts = 0;
  290. }
  291. } else {
  292. if ((inb (UART0_BASE + UART_MSR) & 0x10)) {
  293. buf_info.cts = 1;
  294. }
  295. }
  296. #endif
  297. serial_putc (c);
  298. }
  299. void serial_buffered_puts(const char *s)
  300. {
  301. serial_puts (s);
  302. }
  303. int serial_buffered_getc(void)
  304. {
  305. int space;
  306. int c;
  307. int rx_get = buf_info.rx_get;
  308. int rx_put;
  309. #if defined(CONFIG_HW_WATCHDOG)
  310. while (rx_get == buf_info.rx_put)
  311. WATCHDOG_RESET ();
  312. #else
  313. while (rx_get == buf_info.rx_put);
  314. #endif
  315. c = buf_info.rx_buffer[rx_get++];
  316. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO) {
  317. rx_get = 0;
  318. }
  319. buf_info.rx_get = rx_get;
  320. rx_put = buf_info.rx_put;
  321. if (rx_get <= rx_put) {
  322. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  323. } else {
  324. space = rx_get - rx_put;
  325. }
  326. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  327. /* Start flow by setting RTS active */
  328. outb(inb (UART0_BASE + UART_MCR) | 0x02, UART0_BASE + UART_MCR);
  329. }
  330. return c;
  331. }
  332. int serial_buffered_tstc(void)
  333. {
  334. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  335. }
  336. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  337. #if defined(CONFIG_CMD_KGDB)
  338. /*
  339. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  340. number 0 or number 1
  341. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  342. configuration has been already done
  343. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  344. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  345. */
  346. #if (CONFIG_KGDB_SER_INDEX & 2)
  347. void kgdb_serial_init(void)
  348. {
  349. volatile char val;
  350. bdiv = serial_div (CONFIG_KGDB_BAUDRATE);
  351. /*
  352. * Init onboard 16550 UART
  353. */
  354. outb(0x80, UART1_BASE + UART_LCR); /* set DLAB bit */
  355. outb((bdiv & 0xff), UART1_BASE + UART_DLL); /* set divisor for 9600 baud */
  356. outb((bdiv >> 8 ), UART1_BASE + UART_DLM); /* set divisor for 9600 baud */
  357. outb(0x03, UART1_BASE + UART_LCR); /* line control 8 bits no parity */
  358. outb(0x00, UART1_BASE + UART_FCR); /* disable FIFO */
  359. outb(0x00, UART1_BASE + UART_MCR); /* no modem control DTR RTS */
  360. val = inb(UART1_BASE + UART_LSR); /* clear line status */
  361. val = inb(UART1_BASE + UART_RBR); /* read receive buffer */
  362. outb(0x00, UART1_BASE + UART_SCR); /* set scratchpad */
  363. outb(0x00, UART1_BASE + UART_IER); /* set interrupt enable reg */
  364. }
  365. void putDebugChar(const char c)
  366. {
  367. if (c == '\n')
  368. serial_putc ('\r');
  369. outb(c, UART1_BASE + UART_THR); /* put character out */
  370. /* check THRE bit, wait for transfer done */
  371. while ((inb(UART1_BASE + UART_LSR) & 0x20) != 0x20);
  372. }
  373. void putDebugStr(const char *s)
  374. {
  375. while (*s) {
  376. serial_putc(*s++);
  377. }
  378. }
  379. int getDebugChar(void)
  380. {
  381. unsigned char status = 0;
  382. while (1) {
  383. status = inb(UART1_BASE + UART_LSR);
  384. if ((status & asyncLSRDataReady1) != 0x0) {
  385. break;
  386. }
  387. if ((status & ( asyncLSRFramingError1 |
  388. asyncLSROverrunError1 |
  389. asyncLSRParityError1 |
  390. asyncLSRBreakInterrupt1 )) != 0) {
  391. outb(asyncLSRFramingError1 |
  392. asyncLSROverrunError1 |
  393. asyncLSRParityError1 |
  394. asyncLSRBreakInterrupt1, UART1_BASE + UART_LSR);
  395. }
  396. }
  397. return (0x000000ff & (int) inb(UART1_BASE));
  398. }
  399. void kgdb_interruptible(int yes)
  400. {
  401. return;
  402. }
  403. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  404. void kgdb_serial_init(void)
  405. {
  406. serial_printf ("[on serial] ");
  407. }
  408. void putDebugChar(int c)
  409. {
  410. serial_putc (c);
  411. }
  412. void putDebugStr(const char *str)
  413. {
  414. serial_puts (str);
  415. }
  416. int getDebugChar(void)
  417. {
  418. return serial_getc ();
  419. }
  420. void kgdb_interruptible(int yes)
  421. {
  422. return;
  423. }
  424. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  425. #endif