serial.c 29 KB

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