serial.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * (C) Copyright 2000-2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*------------------------------------------------------------------------------+ */
  24. /*
  25. * This source code has been made available to you by IBM on an AS-IS
  26. * basis. Anyone receiving this source is licensed under IBM
  27. * copyrights to use it in any way he or she deems fit, including
  28. * copying it, modifying it, compiling it, and redistributing it either
  29. * with or without modifications. No license under IBM patents or
  30. * patent applications is to be implied by the copyright license.
  31. *
  32. * Any user of this software should understand that IBM cannot provide
  33. * technical support for this software and will not be responsible for
  34. * any consequences resulting from the use of this software.
  35. *
  36. * Any person who transfers this source code or any derivative work
  37. * must include the IBM copyright notice, this paragraph, and the
  38. * preceding two paragraphs in the transferred software.
  39. *
  40. * COPYRIGHT I B M CORPORATION 1995
  41. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  42. */
  43. /*------------------------------------------------------------------------------- */
  44. /*
  45. * Travis Sawyer 15 September 2004
  46. * Added CONFIG_SERIAL_MULTI support
  47. */
  48. #include <common.h>
  49. #include <commproc.h>
  50. #include <asm/processor.h>
  51. #include <watchdog.h>
  52. #include "vecnum.h"
  53. #ifdef CONFIG_SERIAL_MULTI
  54. #include <serial.h>
  55. #endif
  56. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  57. #include <malloc.h>
  58. #endif
  59. DECLARE_GLOBAL_DATA_PTR;
  60. /*****************************************************************************/
  61. #ifdef CONFIG_IOP480
  62. #define SPU_BASE 0x40000000
  63. #define spu_LineStat_rc 0x00 /* Line Status Register (Read/Clear) */
  64. #define spu_LineStat_w 0x04 /* Line Status Register (Set) */
  65. #define spu_Handshk_rc 0x08 /* Handshake Status Register (Read/Clear) */
  66. #define spu_Handshk_w 0x0c /* Handshake Status Register (Set) */
  67. #define spu_BRateDivh 0x10 /* Baud rate divisor high */
  68. #define spu_BRateDivl 0x14 /* Baud rate divisor low */
  69. #define spu_CtlReg 0x18 /* Control Register */
  70. #define spu_RxCmd 0x1c /* Rx Command Register */
  71. #define spu_TxCmd 0x20 /* Tx Command Register */
  72. #define spu_RxBuff 0x24 /* Rx data buffer */
  73. #define spu_TxBuff 0x24 /* Tx data buffer */
  74. /*-----------------------------------------------------------------------------+
  75. | Line Status Register.
  76. +-----------------------------------------------------------------------------*/
  77. #define asyncLSRport1 0x40000000
  78. #define asyncLSRport1set 0x40000004
  79. #define asyncLSRDataReady 0x80
  80. #define asyncLSRFramingError 0x40
  81. #define asyncLSROverrunError 0x20
  82. #define asyncLSRParityError 0x10
  83. #define asyncLSRBreakInterrupt 0x08
  84. #define asyncLSRTxHoldEmpty 0x04
  85. #define asyncLSRTxShiftEmpty 0x02
  86. /*-----------------------------------------------------------------------------+
  87. | Handshake Status Register.
  88. +-----------------------------------------------------------------------------*/
  89. #define asyncHSRport1 0x40000008
  90. #define asyncHSRport1set 0x4000000c
  91. #define asyncHSRDsr 0x80
  92. #define asyncLSRCts 0x40
  93. /*-----------------------------------------------------------------------------+
  94. | Control Register.
  95. +-----------------------------------------------------------------------------*/
  96. #define asyncCRport1 0x40000018
  97. #define asyncCRNormal 0x00
  98. #define asyncCRLoopback 0x40
  99. #define asyncCRAutoEcho 0x80
  100. #define asyncCRDtr 0x20
  101. #define asyncCRRts 0x10
  102. #define asyncCRWordLength7 0x00
  103. #define asyncCRWordLength8 0x08
  104. #define asyncCRParityDisable 0x00
  105. #define asyncCRParityEnable 0x04
  106. #define asyncCREvenParity 0x00
  107. #define asyncCROddParity 0x02
  108. #define asyncCRStopBitsOne 0x00
  109. #define asyncCRStopBitsTwo 0x01
  110. #define asyncCRDisableDtrRts 0x00
  111. /*-----------------------------------------------------------------------------+
  112. | Receiver Command Register.
  113. +-----------------------------------------------------------------------------*/
  114. #define asyncRCRport1 0x4000001c
  115. #define asyncRCRDisable 0x00
  116. #define asyncRCREnable 0x80
  117. #define asyncRCRIntDisable 0x00
  118. #define asyncRCRIntEnabled 0x20
  119. #define asyncRCRDMACh2 0x40
  120. #define asyncRCRDMACh3 0x60
  121. #define asyncRCRErrorInt 0x10
  122. #define asyncRCRPauseEnable 0x08
  123. /*-----------------------------------------------------------------------------+
  124. | Transmitter Command Register.
  125. +-----------------------------------------------------------------------------*/
  126. #define asyncTCRport1 0x40000020
  127. #define asyncTCRDisable 0x00
  128. #define asyncTCREnable 0x80
  129. #define asyncTCRIntDisable 0x00
  130. #define asyncTCRIntEnabled 0x20
  131. #define asyncTCRDMACh2 0x40
  132. #define asyncTCRDMACh3 0x60
  133. #define asyncTCRTxEmpty 0x10
  134. #define asyncTCRErrorInt 0x08
  135. #define asyncTCRStopPause 0x04
  136. #define asyncTCRBreakGen 0x02
  137. /*-----------------------------------------------------------------------------+
  138. | Miscellanies defines.
  139. +-----------------------------------------------------------------------------*/
  140. #define asyncTxBufferport1 0x40000024
  141. #define asyncRxBufferport1 0x40000024
  142. #define asyncDLABLsbport1 0x40000014
  143. #define asyncDLABMsbport1 0x40000010
  144. #define asyncXOFFchar 0x13
  145. #define asyncXONchar 0x11
  146. /*
  147. * Minimal serial functions needed to use one of the SMC ports
  148. * as serial console interface.
  149. */
  150. int serial_init (void)
  151. {
  152. volatile char val;
  153. unsigned short br_reg;
  154. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  155. /*
  156. * Init onboard UART
  157. */
  158. out8 (SPU_BASE + spu_LineStat_rc, 0x78); /* Clear all bits in Line Status Reg */
  159. out8 (SPU_BASE + spu_BRateDivl, (br_reg & 0x00ff)); /* Set baud rate divisor... */
  160. out8 (SPU_BASE + spu_BRateDivh, ((br_reg & 0xff00) >> 8)); /* ... */
  161. out8 (SPU_BASE + spu_CtlReg, 0x08); /* Set 8 bits, no parity and 1 stop bit */
  162. out8 (SPU_BASE + spu_RxCmd, 0xb0); /* Enable Rx */
  163. out8 (SPU_BASE + spu_TxCmd, 0x9c); /* Enable Tx */
  164. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  165. val = in8 (SPU_BASE + spu_RxBuff); /* Dummy read, to clear receiver */
  166. return (0);
  167. }
  168. void serial_setbrg (void)
  169. {
  170. unsigned short br_reg;
  171. br_reg = ((((CONFIG_CPUCLOCK * 1000000) / 16) / gd->baudrate) - 1);
  172. out8 (SPU_BASE + spu_BRateDivl, (br_reg & 0x00ff)); /* Set baud rate divisor... */
  173. out8 (SPU_BASE + spu_BRateDivh, ((br_reg & 0xff00) >> 8)); /* ... */
  174. }
  175. void serial_putc (const char c)
  176. {
  177. if (c == '\n')
  178. serial_putc ('\r');
  179. /* load status from handshake register */
  180. if (in8 (SPU_BASE + spu_Handshk_rc) != 00)
  181. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  182. out8 (SPU_BASE + spu_TxBuff, c); /* Put char */
  183. while ((in8 (SPU_BASE + spu_LineStat_rc) & 04) != 04) {
  184. if (in8 (SPU_BASE + spu_Handshk_rc) != 00)
  185. out8 (SPU_BASE + spu_Handshk_rc, 0xff); /* Clear Handshake */
  186. }
  187. }
  188. void serial_puts (const char *s)
  189. {
  190. while (*s) {
  191. serial_putc (*s++);
  192. }
  193. }
  194. int serial_getc ()
  195. {
  196. unsigned char status = 0;
  197. while (1) {
  198. status = in8 (asyncLSRport1);
  199. if ((status & asyncLSRDataReady) != 0x0) {
  200. break;
  201. }
  202. if ((status & ( asyncLSRFramingError |
  203. asyncLSROverrunError |
  204. asyncLSRParityError |
  205. asyncLSRBreakInterrupt )) != 0) {
  206. (void) out8 (asyncLSRport1,
  207. asyncLSRFramingError |
  208. asyncLSROverrunError |
  209. asyncLSRParityError |
  210. asyncLSRBreakInterrupt );
  211. }
  212. }
  213. return (0x000000ff & (int) in8 (asyncRxBufferport1));
  214. }
  215. int serial_tstc ()
  216. {
  217. unsigned char status;
  218. status = in8 (asyncLSRport1);
  219. if ((status & asyncLSRDataReady) != 0x0) {
  220. return (1);
  221. }
  222. if ((status & ( asyncLSRFramingError |
  223. asyncLSROverrunError |
  224. asyncLSRParityError |
  225. asyncLSRBreakInterrupt )) != 0) {
  226. (void) out8 (asyncLSRport1,
  227. asyncLSRFramingError |
  228. asyncLSROverrunError |
  229. asyncLSRParityError |
  230. asyncLSRBreakInterrupt);
  231. }
  232. return 0;
  233. }
  234. #endif /* CONFIG_IOP480 */
  235. /*****************************************************************************/
  236. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) || defined(CONFIG_405EP)
  237. #if defined(CONFIG_440)
  238. #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
  239. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  240. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  241. #else
  242. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  243. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  244. #endif
  245. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  246. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
  247. #endif
  248. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  249. #define CR0_MASK 0xdfffffff
  250. #define CR0_EXTCLK_ENA 0x00800000
  251. #define CR0_UDIV_POS 0
  252. #else
  253. #define CR0_MASK 0x3fff0000
  254. #define CR0_EXTCLK_ENA 0x00600000
  255. #define CR0_UDIV_POS 16
  256. #endif /* CONFIG_440GX */
  257. #elif defined(CONFIG_405EP)
  258. #define UART0_BASE 0xef600300
  259. #define UART1_BASE 0xef600400
  260. #define UCR0_MASK 0x0000007f
  261. #define UCR1_MASK 0x00007f00
  262. #define UCR0_UDIV_POS 0
  263. #define UCR1_UDIV_POS 8
  264. #define UDIV_MAX 127
  265. #else /* CONFIG_405GP || CONFIG_405CR */
  266. #define UART0_BASE 0xef600300
  267. #define UART1_BASE 0xef600400
  268. #define CR0_MASK 0x00001fff
  269. #define CR0_EXTCLK_ENA 0x000000c0
  270. #define CR0_UDIV_POS 1
  271. #define UDIV_MAX 32
  272. #endif
  273. /* using serial port 0 or 1 as U-Boot console ? */
  274. #if defined(CONFIG_UART1_CONSOLE)
  275. #define ACTING_UART0_BASE UART1_BASE
  276. #define ACTING_UART1_BASE UART0_BASE
  277. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
  278. defined(CONFIG_440GR) || defined(CONFIG_440SP) || \
  279. defined(CONFIG_440SPE)
  280. #define UART0_SDR sdr_uart1
  281. #define UART1_SDR sdr_uart0
  282. #endif /* CONFIG_440GX */
  283. #else
  284. #define ACTING_UART0_BASE UART0_BASE
  285. #define ACTING_UART1_BASE UART1_BASE
  286. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
  287. defined(CONFIG_440GR) || defined(CONFIG_440SP) || \
  288. defined(CONFIG_440SPE)
  289. #define UART0_SDR sdr_uart0
  290. #define UART1_SDR sdr_uart1
  291. #endif /* CONFIG_440GX */
  292. #endif
  293. #if defined(CONFIG_405EP) && defined(CFG_EXT_SERIAL_CLOCK)
  294. #error "External serial clock not supported on AMCC PPC405EP!"
  295. #endif
  296. #define UART_RBR 0x00
  297. #define UART_THR 0x00
  298. #define UART_IER 0x01
  299. #define UART_IIR 0x02
  300. #define UART_FCR 0x02
  301. #define UART_LCR 0x03
  302. #define UART_MCR 0x04
  303. #define UART_LSR 0x05
  304. #define UART_MSR 0x06
  305. #define UART_SCR 0x07
  306. #define UART_DLL 0x00
  307. #define UART_DLM 0x01
  308. /*-----------------------------------------------------------------------------+
  309. | Line Status Register.
  310. +-----------------------------------------------------------------------------*/
  311. /*#define asyncLSRport1 ACTING_UART0_BASE+0x05 */
  312. #define asyncLSRDataReady1 0x01
  313. #define asyncLSROverrunError1 0x02
  314. #define asyncLSRParityError1 0x04
  315. #define asyncLSRFramingError1 0x08
  316. #define asyncLSRBreakInterrupt1 0x10
  317. #define asyncLSRTxHoldEmpty1 0x20
  318. #define asyncLSRTxShiftEmpty1 0x40
  319. #define asyncLSRRxFifoError1 0x80
  320. /*-----------------------------------------------------------------------------+
  321. | Miscellanies defines.
  322. +-----------------------------------------------------------------------------*/
  323. /*#define asyncTxBufferport1 ACTING_UART0_BASE+0x00 */
  324. /*#define asyncRxBufferport1 ACTING_UART0_BASE+0x00 */
  325. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  326. /*-----------------------------------------------------------------------------+
  327. | Fifo
  328. +-----------------------------------------------------------------------------*/
  329. typedef struct {
  330. char *rx_buffer;
  331. ulong rx_put;
  332. ulong rx_get;
  333. } serial_buffer_t;
  334. volatile static serial_buffer_t buf_info;
  335. #endif
  336. #if defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLOCK)
  337. static void serial_divs (int baudrate, unsigned long *pudiv,
  338. unsigned short *pbdiv )
  339. {
  340. sys_info_t sysinfo;
  341. unsigned long div; /* total divisor udiv * bdiv */
  342. unsigned long umin; /* minimum udiv */
  343. unsigned short diff; /* smallest diff */
  344. unsigned long udiv; /* best udiv */
  345. unsigned short idiff; /* current diff */
  346. unsigned short ibdiv; /* current bdiv */
  347. unsigned long i;
  348. unsigned long est; /* current estimate */
  349. get_sys_info( &sysinfo );
  350. udiv = 32; /* Assume lowest possible serial clk */
  351. div = sysinfo.freqPLB/(16*baudrate); /* total divisor */
  352. umin = sysinfo.pllOpbDiv<<1; /* 2 x OPB divisor */
  353. diff = 32; /* highest possible */
  354. /* i is the test udiv value -- start with the largest
  355. * possible (32) to minimize serial clock and constrain
  356. * search to umin.
  357. */
  358. for( i = 32; i > umin; i-- ){
  359. ibdiv = div/i;
  360. est = i * ibdiv;
  361. idiff = (est > div) ? (est-div) : (div-est);
  362. if( idiff == 0 ){
  363. udiv = i;
  364. break; /* can't do better */
  365. }
  366. else if( idiff < diff ){
  367. udiv = i; /* best so far */
  368. diff = idiff; /* update lowest diff*/
  369. }
  370. }
  371. *pudiv = udiv;
  372. *pbdiv = div/udiv;
  373. }
  374. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK */
  375. /*
  376. * Minimal serial functions needed to use one of the SMC ports
  377. * as serial console interface.
  378. */
  379. #if defined(CONFIG_440)
  380. #if defined(CONFIG_SERIAL_MULTI)
  381. int serial_init_dev (unsigned long dev_base)
  382. #else
  383. int serial_init(void)
  384. #endif
  385. {
  386. unsigned long reg;
  387. unsigned long udiv;
  388. unsigned short bdiv;
  389. volatile char val;
  390. #ifdef CFG_EXT_SERIAL_CLOCK
  391. unsigned long tmp;
  392. #endif
  393. #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || \
  394. defined(CONFIG_440SPE)
  395. #if defined(CONFIG_SERIAL_MULTI)
  396. if (UART0_BASE == dev_base) {
  397. mfsdr(UART0_SDR,reg);
  398. reg &= ~CR0_MASK;
  399. } else {
  400. mfsdr(UART1_SDR,reg);
  401. reg &= ~CR0_MASK;
  402. }
  403. #else
  404. mfsdr(UART0_SDR,reg);
  405. reg &= ~CR0_MASK;
  406. #endif
  407. #else
  408. reg = mfdcr(cntrl0) & ~CR0_MASK;
  409. #endif /* CONFIG_440GX */
  410. #ifdef CFG_EXT_SERIAL_CLOCK
  411. reg |= CR0_EXTCLK_ENA;
  412. udiv = 1;
  413. tmp = gd->baudrate * 16;
  414. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  415. #else
  416. /* For 440, the cpu clock is on divider chain A, UART on divider
  417. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  418. * values that are subject to the 1/2 opb clock constraint
  419. */
  420. serial_divs (gd->baudrate, &udiv, &bdiv);
  421. #endif
  422. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
  423. defined(CONFIG_440GR) || defined(CONFIG_440SP) || \
  424. defined(CONFIG_440SPE)
  425. reg |= udiv << CR0_UDIV_POS; /* set the UART divisor */
  426. #if defined(CONFIG_SERIAL_MULTI)
  427. if (UART0_BASE == dev_base) {
  428. mtsdr (UART0_SDR,reg);
  429. } else {
  430. mtsdr (UART1_SDR,reg);
  431. }
  432. #else
  433. mtsdr (UART0_SDR,reg);
  434. #endif
  435. #else
  436. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  437. mtdcr (cntrl0, reg);
  438. #endif
  439. #if defined(CONFIG_SERIAL_MULTI)
  440. out8 (dev_base + UART_LCR, 0x80); /* set DLAB bit */
  441. out8 (dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  442. out8 (dev_base + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  443. out8 (dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  444. out8 (dev_base + UART_FCR, 0x00); /* disable FIFO */
  445. out8 (dev_base + UART_MCR, 0x00); /* no modem control DTR RTS */
  446. val = in8 (dev_base + UART_LSR); /* clear line status */
  447. val = in8 (dev_base + UART_RBR); /* read receive buffer */
  448. out8 (dev_base + UART_SCR, 0x00); /* set scratchpad */
  449. out8 (dev_base + UART_IER, 0x00); /* set interrupt enable reg */
  450. #else
  451. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  452. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  453. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  454. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  455. out8 (ACTING_UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  456. out8 (ACTING_UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  457. val = in8 (ACTING_UART0_BASE + UART_LSR); /* clear line status */
  458. val = in8 (ACTING_UART0_BASE + UART_RBR); /* read receive buffer */
  459. out8 (ACTING_UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  460. out8 (ACTING_UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  461. #endif
  462. return (0);
  463. }
  464. #else /* !defined(CONFIG_440) */
  465. #if defined(CONFIG_SERIAL_MULTI)
  466. int serial_init_dev (unsigned long dev_base)
  467. #else
  468. int serial_init (void)
  469. #endif
  470. {
  471. unsigned long reg;
  472. unsigned long tmp;
  473. unsigned long clk;
  474. unsigned long udiv;
  475. unsigned short bdiv;
  476. volatile char val;
  477. #ifdef CONFIG_405EP
  478. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  479. clk = gd->cpu_clk;
  480. tmp = CFG_BASE_BAUD * 16;
  481. udiv = (clk + tmp / 2) / tmp;
  482. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  483. udiv = UDIV_MAX;
  484. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  485. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  486. mtdcr (cpc0_ucr, reg);
  487. #else /* CONFIG_405EP */
  488. reg = mfdcr(cntrl0) & ~CR0_MASK;
  489. #ifdef CFG_EXT_SERIAL_CLOCK
  490. clk = CFG_EXT_SERIAL_CLOCK;
  491. udiv = 1;
  492. reg |= CR0_EXTCLK_ENA;
  493. #else
  494. clk = gd->cpu_clk;
  495. #ifdef CFG_405_UART_ERRATA_59
  496. udiv = 31; /* Errata 59: stuck at 31 */
  497. #else
  498. tmp = CFG_BASE_BAUD * 16;
  499. udiv = (clk + tmp / 2) / tmp;
  500. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  501. udiv = UDIV_MAX;
  502. #endif
  503. #endif
  504. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  505. mtdcr (cntrl0, reg);
  506. #endif /* CONFIG_405EP */
  507. tmp = gd->baudrate * udiv * 16;
  508. bdiv = (clk + tmp / 2) / tmp;
  509. #if defined(CONFIG_SERIAL_MULTI)
  510. out8 (dev_base + UART_LCR, 0x80); /* set DLAB bit */
  511. out8 (dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  512. out8 (dev_base + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  513. out8 (dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  514. out8 (dev_base + UART_FCR, 0x00); /* disable FIFO */
  515. out8 (dev_base + UART_MCR, 0x00); /* no modem control DTR RTS */
  516. val = in8 (dev_base + UART_LSR); /* clear line status */
  517. val = in8 (dev_base + UART_RBR); /* read receive buffer */
  518. out8 (dev_base + UART_SCR, 0x00); /* set scratchpad */
  519. out8 (dev_base + UART_IER, 0x00); /* set interrupt enable reg */
  520. #else
  521. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  522. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  523. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  524. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  525. out8 (ACTING_UART0_BASE + UART_FCR, 0x00); /* disable FIFO */
  526. out8 (ACTING_UART0_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  527. val = in8 (ACTING_UART0_BASE + UART_LSR); /* clear line status */
  528. val = in8 (ACTING_UART0_BASE + UART_RBR); /* read receive buffer */
  529. out8 (ACTING_UART0_BASE + UART_SCR, 0x00); /* set scratchpad */
  530. out8 (ACTING_UART0_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  531. #endif
  532. return (0);
  533. }
  534. #endif /* if defined(CONFIG_440) */
  535. #if defined(CONFIG_SERIAL_MULTI)
  536. void serial_setbrg_dev (unsigned long dev_base)
  537. #else
  538. void serial_setbrg (void)
  539. #endif
  540. {
  541. unsigned long tmp;
  542. unsigned long clk;
  543. unsigned long udiv;
  544. unsigned short bdiv;
  545. #ifdef CFG_EXT_SERIAL_CLOCK
  546. clk = CFG_EXT_SERIAL_CLOCK;
  547. #else
  548. clk = gd->cpu_clk;
  549. #endif
  550. #ifdef CONFIG_405EP
  551. udiv = ((mfdcr (cpc0_ucr) & UCR0_MASK) >> UCR0_UDIV_POS);
  552. #else
  553. udiv = ((mfdcr (cntrl0) & 0x3e) >> 1) + 1;
  554. #endif /* CONFIG_405EP */
  555. #if !defined(CFG_EXT_SERIAL_CLOCK) && \
  556. ( defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
  557. defined(CONFIG_440GR) || defined(CONFIG_440SP) || \
  558. defined(CONFIG_440SPE) )
  559. serial_divs (gd->baudrate, &udiv, &bdiv);
  560. tmp = udiv << CR0_UDIV_POS; /* set the UART divisor */
  561. #if defined(CONFIG_SERIAL_MULTI)
  562. if (UART0_BASE == dev_base) {
  563. mtsdr (UART0_SDR, tmp);
  564. } else {
  565. mtsdr (UART1_SDR, tmp);
  566. }
  567. #else
  568. mtsdr (UART0_SDR, tmp);
  569. #endif
  570. #else
  571. tmp = gd->baudrate * udiv * 16;
  572. bdiv = (clk + tmp / 2) / tmp;
  573. #endif /* !defined(CFG_EXT_SERIAL_CLOCK) && (...) */
  574. #if defined(CONFIG_SERIAL_MULTI)
  575. out8 (dev_base + UART_LCR, 0x80); /* set DLAB bit */
  576. out8 (dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  577. out8 (dev_base + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  578. out8 (dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  579. #else
  580. out8 (ACTING_UART0_BASE + UART_LCR, 0x80); /* set DLAB bit */
  581. out8 (ACTING_UART0_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  582. out8 (ACTING_UART0_BASE + UART_DLM, bdiv >> 8);/* set baudrate divisor */
  583. out8 (ACTING_UART0_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  584. #endif
  585. }
  586. #if defined(CONFIG_SERIAL_MULTI)
  587. void serial_putc_dev (unsigned long dev_base, const char c)
  588. #else
  589. void serial_putc (const char c)
  590. #endif
  591. {
  592. int i;
  593. if (c == '\n')
  594. #if defined(CONFIG_SERIAL_MULTI)
  595. serial_putc_dev (dev_base, '\r');
  596. #else
  597. serial_putc ('\r');
  598. #endif
  599. /* check THRE bit, wait for transmiter available */
  600. for (i = 1; i < 3500; i++) {
  601. #if defined(CONFIG_SERIAL_MULTI)
  602. if ((in8 (dev_base + UART_LSR) & 0x20) == 0x20)
  603. #else
  604. if ((in8 (ACTING_UART0_BASE + UART_LSR) & 0x20) == 0x20)
  605. #endif
  606. break;
  607. udelay (100);
  608. }
  609. #if defined(CONFIG_SERIAL_MULTI)
  610. out8 (dev_base + UART_THR, c); /* put character out */
  611. #else
  612. out8 (ACTING_UART0_BASE + UART_THR, c); /* put character out */
  613. #endif
  614. }
  615. #if defined(CONFIG_SERIAL_MULTI)
  616. void serial_puts_dev (unsigned long dev_base, const char *s)
  617. #else
  618. void serial_puts (const char *s)
  619. #endif
  620. {
  621. while (*s) {
  622. #if defined(CONFIG_SERIAL_MULTI)
  623. serial_putc_dev (dev_base, *s++);
  624. #else
  625. serial_putc (*s++);
  626. #endif
  627. }
  628. }
  629. #if defined(CONFIG_SERIAL_MULTI)
  630. int serial_getc_dev (unsigned long dev_base)
  631. #else
  632. int serial_getc (void)
  633. #endif
  634. {
  635. unsigned char status = 0;
  636. while (1) {
  637. #if defined(CONFIG_HW_WATCHDOG)
  638. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  639. #endif /* CONFIG_HW_WATCHDOG */
  640. #if defined(CONFIG_SERIAL_MULTI)
  641. status = in8 (dev_base + UART_LSR);
  642. #else
  643. status = in8 (ACTING_UART0_BASE + UART_LSR);
  644. #endif
  645. if ((status & asyncLSRDataReady1) != 0x0) {
  646. break;
  647. }
  648. if ((status & ( asyncLSRFramingError1 |
  649. asyncLSROverrunError1 |
  650. asyncLSRParityError1 |
  651. asyncLSRBreakInterrupt1 )) != 0) {
  652. #if defined(CONFIG_SERIAL_MULTI)
  653. out8 (dev_base + UART_LSR,
  654. #else
  655. out8 (ACTING_UART0_BASE + UART_LSR,
  656. #endif
  657. asyncLSRFramingError1 |
  658. asyncLSROverrunError1 |
  659. asyncLSRParityError1 |
  660. asyncLSRBreakInterrupt1);
  661. }
  662. }
  663. #if defined(CONFIG_SERIAL_MULTI)
  664. return (0x000000ff & (int) in8 (dev_base));
  665. #else
  666. return (0x000000ff & (int) in8 (ACTING_UART0_BASE));
  667. #endif
  668. }
  669. #if defined(CONFIG_SERIAL_MULTI)
  670. int serial_tstc_dev (unsigned long dev_base)
  671. #else
  672. int serial_tstc (void)
  673. #endif
  674. {
  675. unsigned char status;
  676. #if defined(CONFIG_SERIAL_MULTI)
  677. status = in8 (dev_base + UART_LSR);
  678. #else
  679. status = in8 (ACTING_UART0_BASE + UART_LSR);
  680. #endif
  681. if ((status & asyncLSRDataReady1) != 0x0) {
  682. return (1);
  683. }
  684. if ((status & ( asyncLSRFramingError1 |
  685. asyncLSROverrunError1 |
  686. asyncLSRParityError1 |
  687. asyncLSRBreakInterrupt1 )) != 0) {
  688. #if defined(CONFIG_SERIAL_MULTI)
  689. out8 (dev_base + UART_LSR,
  690. #else
  691. out8 (ACTING_UART0_BASE + UART_LSR,
  692. #endif
  693. asyncLSRFramingError1 |
  694. asyncLSROverrunError1 |
  695. asyncLSRParityError1 |
  696. asyncLSRBreakInterrupt1);
  697. }
  698. return 0;
  699. }
  700. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  701. void serial_isr (void *arg)
  702. {
  703. int space;
  704. int c;
  705. const int rx_get = buf_info.rx_get;
  706. int rx_put = buf_info.rx_put;
  707. if (rx_get <= rx_put) {
  708. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  709. } else {
  710. space = rx_get - rx_put;
  711. }
  712. while (serial_tstc_dev (ACTING_UART0_BASE)) {
  713. c = serial_getc_dev (ACTING_UART0_BASE);
  714. if (space) {
  715. buf_info.rx_buffer[rx_put++] = c;
  716. space--;
  717. }
  718. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  719. rx_put = 0;
  720. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  721. /* Stop flow by setting RTS inactive */
  722. out8 (ACTING_UART0_BASE + UART_MCR,
  723. in8 (ACTING_UART0_BASE + UART_MCR) & (0xFF ^ 0x02));
  724. }
  725. }
  726. buf_info.rx_put = rx_put;
  727. }
  728. void serial_buffered_init (void)
  729. {
  730. serial_puts ("Switching to interrupt driven serial input mode.\n");
  731. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  732. buf_info.rx_put = 0;
  733. buf_info.rx_get = 0;
  734. if (in8 (ACTING_UART0_BASE + UART_MSR) & 0x10) {
  735. serial_puts ("Check CTS signal present on serial port: OK.\n");
  736. } else {
  737. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  738. }
  739. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  740. serial_isr /*interrupt_handler_t *handler */ ,
  741. (void *) &buf_info /*void *arg */ );
  742. /* Enable "RX Data Available" Interrupt on UART */
  743. /* out8(ACTING_UART0_BASE + UART_IER, in8(ACTING_UART0_BASE + UART_IER) |0x01); */
  744. out8 (ACTING_UART0_BASE + UART_IER, 0x01);
  745. /* Set DTR active */
  746. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x01);
  747. /* Start flow by setting RTS active */
  748. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  749. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  750. out8 (ACTING_UART0_BASE + UART_FCR, (1 << 6) | 1);
  751. }
  752. void serial_buffered_putc (const char c)
  753. {
  754. /* Wait for CTS */
  755. #if defined(CONFIG_HW_WATCHDOG)
  756. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10))
  757. WATCHDOG_RESET ();
  758. #else
  759. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10));
  760. #endif
  761. serial_putc (c);
  762. }
  763. void serial_buffered_puts (const char *s)
  764. {
  765. serial_puts (s);
  766. }
  767. int serial_buffered_getc (void)
  768. {
  769. int space;
  770. int c;
  771. int rx_get = buf_info.rx_get;
  772. int rx_put;
  773. #if defined(CONFIG_HW_WATCHDOG)
  774. while (rx_get == buf_info.rx_put)
  775. WATCHDOG_RESET ();
  776. #else
  777. while (rx_get == buf_info.rx_put);
  778. #endif
  779. c = buf_info.rx_buffer[rx_get++];
  780. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  781. rx_get = 0;
  782. buf_info.rx_get = rx_get;
  783. rx_put = buf_info.rx_put;
  784. if (rx_get <= rx_put) {
  785. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  786. } else {
  787. space = rx_get - rx_put;
  788. }
  789. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  790. /* Start flow by setting RTS active */
  791. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  792. }
  793. return c;
  794. }
  795. int serial_buffered_tstc (void)
  796. {
  797. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  798. }
  799. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  800. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  801. /*
  802. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  803. number 0 or number 1
  804. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  805. configuration has been already done
  806. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  807. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  808. */
  809. #if (CONFIG_KGDB_SER_INDEX & 2)
  810. void kgdb_serial_init (void)
  811. {
  812. volatile char val;
  813. unsigned short br_reg;
  814. get_clocks ();
  815. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  816. 5) / 10;
  817. /*
  818. * Init onboard 16550 UART
  819. */
  820. out8 (ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  821. out8 (ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  822. out8 (ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  823. out8 (ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  824. out8 (ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  825. out8 (ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  826. val = in8 (ACTING_UART1_BASE + UART_LSR); /* clear line status */
  827. val = in8 (ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  828. out8 (ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  829. out8 (ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  830. }
  831. void putDebugChar (const char c)
  832. {
  833. if (c == '\n')
  834. serial_putc ('\r');
  835. out8 (ACTING_UART1_BASE + UART_THR, c); /* put character out */
  836. /* check THRE bit, wait for transfer done */
  837. while ((in8 (ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  838. }
  839. void putDebugStr (const char *s)
  840. {
  841. while (*s) {
  842. serial_putc (*s++);
  843. }
  844. }
  845. int getDebugChar (void)
  846. {
  847. unsigned char status = 0;
  848. while (1) {
  849. status = in8 (ACTING_UART1_BASE + UART_LSR);
  850. if ((status & asyncLSRDataReady1) != 0x0) {
  851. break;
  852. }
  853. if ((status & ( asyncLSRFramingError1 |
  854. asyncLSROverrunError1 |
  855. asyncLSRParityError1 |
  856. asyncLSRBreakInterrupt1 )) != 0) {
  857. out8 (ACTING_UART1_BASE + UART_LSR,
  858. asyncLSRFramingError1 |
  859. asyncLSROverrunError1 |
  860. asyncLSRParityError1 |
  861. asyncLSRBreakInterrupt1);
  862. }
  863. }
  864. return (0x000000ff & (int) in8 (ACTING_UART1_BASE));
  865. }
  866. void kgdb_interruptible (int yes)
  867. {
  868. return;
  869. }
  870. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  871. void kgdb_serial_init (void)
  872. {
  873. serial_printf ("[on serial] ");
  874. }
  875. void putDebugChar (int c)
  876. {
  877. serial_putc (c);
  878. }
  879. void putDebugStr (const char *str)
  880. {
  881. serial_puts (str);
  882. }
  883. int getDebugChar (void)
  884. {
  885. return serial_getc ();
  886. }
  887. void kgdb_interruptible (int yes)
  888. {
  889. return;
  890. }
  891. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  892. #endif /* CFG_CMD_KGDB */
  893. #if defined(CONFIG_SERIAL_MULTI)
  894. int serial0_init(void)
  895. {
  896. return (serial_init_dev(UART0_BASE));
  897. }
  898. int serial1_init(void)
  899. {
  900. return (serial_init_dev(UART1_BASE));
  901. }
  902. void serial0_setbrg (void)
  903. {
  904. serial_setbrg_dev(UART0_BASE);
  905. }
  906. void serial1_setbrg (void)
  907. {
  908. serial_setbrg_dev(UART1_BASE);
  909. }
  910. void serial0_putc(const char c)
  911. {
  912. serial_putc_dev(UART0_BASE,c);
  913. }
  914. void serial1_putc(const char c)
  915. {
  916. serial_putc_dev(UART1_BASE, c);
  917. }
  918. void serial0_puts(const char *s)
  919. {
  920. serial_puts_dev(UART0_BASE, s);
  921. }
  922. void serial1_puts(const char *s)
  923. {
  924. serial_puts_dev(UART1_BASE, s);
  925. }
  926. int serial0_getc(void)
  927. {
  928. return(serial_getc_dev(UART0_BASE));
  929. }
  930. int serial1_getc(void)
  931. {
  932. return(serial_getc_dev(UART1_BASE));
  933. }
  934. int serial0_tstc(void)
  935. {
  936. return (serial_tstc_dev(UART0_BASE));
  937. }
  938. int serial1_tstc(void)
  939. {
  940. return (serial_tstc_dev(UART1_BASE));
  941. }
  942. struct serial_device serial0_device =
  943. {
  944. "serial0",
  945. "UART0",
  946. serial0_init,
  947. serial0_setbrg,
  948. serial0_getc,
  949. serial0_tstc,
  950. serial0_putc,
  951. serial0_puts,
  952. };
  953. struct serial_device serial1_device =
  954. {
  955. "serial1",
  956. "UART1",
  957. serial1_init,
  958. serial1_setbrg,
  959. serial1_getc,
  960. serial1_tstc,
  961. serial1_putc,
  962. serial1_puts,
  963. };
  964. #endif /* CONFIG_SERIAL_MULTI */
  965. #endif /* CONFIG_405GP || CONFIG_405CR */