serial.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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) || \
  237. defined(CONFIG_405EP) || defined(CONFIG_405EZ) || \
  238. defined(CONFIG_440)
  239. #if defined(CONFIG_440)
  240. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  241. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  242. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  243. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  244. #else
  245. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  246. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  247. #endif
  248. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  249. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
  250. #endif
  251. #if defined(CONFIG_440GP)
  252. #define CR0_MASK 0x3fff0000
  253. #define CR0_EXTCLK_ENA 0x00600000
  254. #define CR0_UDIV_POS 16
  255. #define UDIV_SUBTRACT 1
  256. #define UART0_SDR cntrl0
  257. #define MFREG(a, d) d = mfdcr(a)
  258. #define MTREG(a, d) mtdcr(a, d)
  259. #else /* #if defined(CONFIG_440GP) */
  260. /* all other 440 PPC's access clock divider via sdr register */
  261. #define CR0_MASK 0xdfffffff
  262. #define CR0_EXTCLK_ENA 0x00800000
  263. #define CR0_UDIV_POS 0
  264. #define UDIV_SUBTRACT 0
  265. #define UART0_SDR sdr_uart0
  266. #define UART1_SDR sdr_uart1
  267. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  268. defined(CONFIG_440GR) || defined(CONFIG_440GRx) || \
  269. defined(CONFIG_440SP) || defined(CONFIG_440SPe)
  270. #define UART2_SDR sdr_uart2
  271. #endif
  272. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  273. defined(CONFIG_440GR) || defined(CONFIG_440GRx)
  274. #define UART3_SDR sdr_uart3
  275. #endif
  276. #define MFREG(a, d) mfsdr(a, d)
  277. #define MTREG(a, d) mtsdr(a, d)
  278. #endif /* #if defined(CONFIG_440GP) */
  279. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  280. #define UART0_BASE 0xef600300
  281. #define UART1_BASE 0xef600400
  282. #define UCR0_MASK 0x0000007f
  283. #define UCR1_MASK 0x00007f00
  284. #define UCR0_UDIV_POS 0
  285. #define UCR1_UDIV_POS 8
  286. #define UDIV_MAX 127
  287. #else /* CONFIG_405GP || CONFIG_405CR */
  288. #define UART0_BASE 0xef600300
  289. #define UART1_BASE 0xef600400
  290. #define CR0_MASK 0x00001fff
  291. #define CR0_EXTCLK_ENA 0x000000c0
  292. #define CR0_UDIV_POS 1
  293. #define UDIV_MAX 32
  294. #endif
  295. /* using serial port 0 or 1 as U-Boot console ? */
  296. #if defined(CONFIG_UART1_CONSOLE)
  297. #define ACTING_UART0_BASE UART1_BASE
  298. #define ACTING_UART1_BASE UART0_BASE
  299. #else
  300. #define ACTING_UART0_BASE UART0_BASE
  301. #define ACTING_UART1_BASE UART1_BASE
  302. #endif
  303. #if defined(CONFIG_SERIAL_MULTI)
  304. #define UART_BASE dev_base
  305. #else
  306. #define UART_BASE ACTING_UART0_BASE
  307. #endif
  308. #if defined(CONFIG_405EP) && defined(CFG_EXT_SERIAL_CLOCK)
  309. #error "External serial clock not supported on AMCC PPC405EP!"
  310. #endif
  311. #define UART_RBR 0x00
  312. #define UART_THR 0x00
  313. #define UART_IER 0x01
  314. #define UART_IIR 0x02
  315. #define UART_FCR 0x02
  316. #define UART_LCR 0x03
  317. #define UART_MCR 0x04
  318. #define UART_LSR 0x05
  319. #define UART_MSR 0x06
  320. #define UART_SCR 0x07
  321. #define UART_DLL 0x00
  322. #define UART_DLM 0x01
  323. /*-----------------------------------------------------------------------------+
  324. | Line Status Register.
  325. +-----------------------------------------------------------------------------*/
  326. /*#define asyncLSRport1 ACTING_UART0_BASE+0x05 */
  327. #define asyncLSRDataReady1 0x01
  328. #define asyncLSROverrunError1 0x02
  329. #define asyncLSRParityError1 0x04
  330. #define asyncLSRFramingError1 0x08
  331. #define asyncLSRBreakInterrupt1 0x10
  332. #define asyncLSRTxHoldEmpty1 0x20
  333. #define asyncLSRTxShiftEmpty1 0x40
  334. #define asyncLSRRxFifoError1 0x80
  335. /*-----------------------------------------------------------------------------+
  336. | Miscellanies defines.
  337. +-----------------------------------------------------------------------------*/
  338. /*#define asyncTxBufferport1 ACTING_UART0_BASE+0x00 */
  339. /*#define asyncRxBufferport1 ACTING_UART0_BASE+0x00 */
  340. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  341. /*-----------------------------------------------------------------------------+
  342. | Fifo
  343. +-----------------------------------------------------------------------------*/
  344. typedef struct {
  345. char *rx_buffer;
  346. ulong rx_put;
  347. ulong rx_get;
  348. } serial_buffer_t;
  349. volatile static serial_buffer_t buf_info;
  350. #endif
  351. #if defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLOCK)
  352. static void serial_divs (int baudrate, unsigned long *pudiv,
  353. unsigned short *pbdiv)
  354. {
  355. sys_info_t sysinfo;
  356. unsigned long div; /* total divisor udiv * bdiv */
  357. unsigned long umin; /* minimum udiv */
  358. unsigned short diff; /* smallest diff */
  359. unsigned long udiv; /* best udiv */
  360. unsigned short idiff; /* current diff */
  361. unsigned short ibdiv; /* current bdiv */
  362. unsigned long i;
  363. unsigned long est; /* current estimate */
  364. get_sys_info(&sysinfo);
  365. udiv = 32; /* Assume lowest possible serial clk */
  366. div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
  367. umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
  368. diff = 32; /* highest possible */
  369. /* i is the test udiv value -- start with the largest
  370. * possible (32) to minimize serial clock and constrain
  371. * search to umin.
  372. */
  373. for (i = 32; i > umin; i--) {
  374. ibdiv = div / i;
  375. est = i * ibdiv;
  376. idiff = (est > div) ? (est-div) : (div-est);
  377. if (idiff == 0) {
  378. udiv = i;
  379. break; /* can't do better */
  380. } else if (idiff < diff) {
  381. udiv = i; /* best so far */
  382. diff = idiff; /* update lowest diff*/
  383. }
  384. }
  385. *pudiv = udiv;
  386. *pbdiv = div / udiv;
  387. }
  388. #elif defined(CONFIG_405EZ)
  389. static void serial_divs (int baudrate, unsigned long *pudiv,
  390. unsigned short *pbdiv)
  391. {
  392. sys_info_t sysinfo;
  393. unsigned long div; /* total divisor udiv * bdiv */
  394. unsigned long umin; /* minimum udiv */
  395. unsigned short diff; /* smallest diff */
  396. unsigned long udiv; /* best udiv */
  397. unsigned short idiff; /* current diff */
  398. unsigned short ibdiv; /* current bdiv */
  399. unsigned long i;
  400. unsigned long est; /* current estimate */
  401. unsigned long plloutb;
  402. unsigned long cpr_pllc;
  403. u32 reg;
  404. /* check the pll feedback source */
  405. mfcpr(cprpllc, cpr_pllc);
  406. get_sys_info(&sysinfo);
  407. plloutb = ((CONFIG_SYS_CLK_FREQ * ((cpr_pllc & PLLC_SRC_MASK) ?
  408. sysinfo.pllFwdDivB : sysinfo.pllFwdDiv) * sysinfo.pllFbkDiv) /
  409. sysinfo.pllFwdDivB);
  410. udiv = 256; /* Assume lowest possible serial clk */
  411. div = plloutb / (16 * baudrate); /* total divisor */
  412. umin = (plloutb / get_OPB_freq()) << 1; /* 2 x OPB divisor */
  413. diff = 256; /* highest possible */
  414. /* i is the test udiv value -- start with the largest
  415. * possible (256) to minimize serial clock and constrain
  416. * search to umin.
  417. */
  418. for (i = 256; i > umin; i--) {
  419. ibdiv = div / i;
  420. est = i * ibdiv;
  421. idiff = (est > div) ? (est-div) : (div-est);
  422. if (idiff == 0) {
  423. udiv = i;
  424. break; /* can't do better */
  425. } else if (idiff < diff) {
  426. udiv = i; /* best so far */
  427. diff = idiff; /* update lowest diff*/
  428. }
  429. }
  430. *pudiv = udiv;
  431. mfcpr(cprperd0, reg);
  432. reg &= ~0x0000ffff;
  433. reg |= ((udiv - 0) << 8) | (udiv - 0);
  434. mtcpr(cprperd0, reg);
  435. *pbdiv = div / udiv;
  436. }
  437. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK) */
  438. /*
  439. * Minimal serial functions needed to use one of the SMC ports
  440. * as serial console interface.
  441. */
  442. #if defined(CONFIG_440)
  443. #if defined(CONFIG_SERIAL_MULTI)
  444. int serial_init_dev (unsigned long dev_base)
  445. #else
  446. int serial_init(void)
  447. #endif
  448. {
  449. unsigned long reg;
  450. unsigned long udiv;
  451. unsigned short bdiv;
  452. volatile char val;
  453. #ifdef CFG_EXT_SERIAL_CLOCK
  454. unsigned long tmp;
  455. #endif
  456. MFREG(UART0_SDR, reg);
  457. reg &= ~CR0_MASK;
  458. #ifdef CFG_EXT_SERIAL_CLOCK
  459. reg |= CR0_EXTCLK_ENA;
  460. udiv = 1;
  461. tmp = gd->baudrate * 16;
  462. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  463. #else
  464. /* For 440, the cpu clock is on divider chain A, UART on divider
  465. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  466. * values that are subject to the 1/2 opb clock constraint
  467. */
  468. serial_divs (gd->baudrate, &udiv, &bdiv);
  469. #endif
  470. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  471. /*
  472. * Configure input clock to baudrate generator for all
  473. * available serial ports here
  474. */
  475. MTREG(UART0_SDR, reg);
  476. #if defined(UART1_SDR)
  477. MTREG(UART1_SDR, reg);
  478. #endif
  479. #if defined(UART2_SDR)
  480. MTREG(UART2_SDR, reg);
  481. #endif
  482. #if defined(UART3_SDR)
  483. MTREG(UART3_SDR, reg);
  484. #endif
  485. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  486. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  487. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  488. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  489. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  490. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  491. val = in8(UART_BASE + UART_LSR); /* clear line status */
  492. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  493. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  494. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  495. return (0);
  496. }
  497. #else /* !defined(CONFIG_440) */
  498. #if defined(CONFIG_SERIAL_MULTI)
  499. int serial_init_dev (unsigned long dev_base)
  500. #else
  501. int serial_init (void)
  502. #endif
  503. {
  504. unsigned long reg;
  505. unsigned long tmp;
  506. unsigned long clk;
  507. unsigned long udiv;
  508. unsigned short bdiv;
  509. volatile char val;
  510. #if defined(CONFIG_405EZ)
  511. serial_divs(gd->baudrate, &udiv, &bdiv);
  512. clk = tmp = reg = 0;
  513. #else
  514. #ifdef CONFIG_405EP
  515. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  516. clk = gd->cpu_clk;
  517. tmp = CFG_BASE_BAUD * 16;
  518. udiv = (clk + tmp / 2) / tmp;
  519. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  520. udiv = UDIV_MAX;
  521. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  522. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  523. mtdcr (cpc0_ucr, reg);
  524. #else /* CONFIG_405EP */
  525. reg = mfdcr(cntrl0) & ~CR0_MASK;
  526. #ifdef CFG_EXT_SERIAL_CLOCK
  527. clk = CFG_EXT_SERIAL_CLOCK;
  528. udiv = 1;
  529. reg |= CR0_EXTCLK_ENA;
  530. #else
  531. clk = gd->cpu_clk;
  532. #ifdef CFG_405_UART_ERRATA_59
  533. udiv = 31; /* Errata 59: stuck at 31 */
  534. #else
  535. tmp = CFG_BASE_BAUD * 16;
  536. udiv = (clk + tmp / 2) / tmp;
  537. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  538. udiv = UDIV_MAX;
  539. #endif
  540. #endif
  541. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  542. mtdcr (cntrl0, reg);
  543. #endif /* CONFIG_405EP */
  544. tmp = gd->baudrate * udiv * 16;
  545. bdiv = (clk + tmp / 2) / tmp;
  546. #endif /* CONFIG_405EZ */
  547. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  548. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  549. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  550. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  551. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  552. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  553. val = in8(UART_BASE + UART_LSR); /* clear line status */
  554. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  555. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  556. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  557. return (0);
  558. }
  559. #endif /* if defined(CONFIG_440) */
  560. #if defined(CONFIG_SERIAL_MULTI)
  561. void serial_setbrg_dev (unsigned long dev_base)
  562. #else
  563. void serial_setbrg (void)
  564. #endif
  565. {
  566. #if defined(CONFIG_SERIAL_MULTI)
  567. serial_init_dev(dev_base);
  568. #else
  569. serial_init();
  570. #endif
  571. }
  572. #if defined(CONFIG_SERIAL_MULTI)
  573. void serial_putc_dev (unsigned long dev_base, const char c)
  574. #else
  575. void serial_putc (const char c)
  576. #endif
  577. {
  578. int i;
  579. if (c == '\n')
  580. #if defined(CONFIG_SERIAL_MULTI)
  581. serial_putc_dev (dev_base, '\r');
  582. #else
  583. serial_putc ('\r');
  584. #endif
  585. /* check THRE bit, wait for transmiter available */
  586. for (i = 1; i < 3500; i++) {
  587. if ((in8 (UART_BASE + UART_LSR) & 0x20) == 0x20)
  588. break;
  589. udelay (100);
  590. }
  591. out8 (UART_BASE + UART_THR, c); /* put character out */
  592. }
  593. #if defined(CONFIG_SERIAL_MULTI)
  594. void serial_puts_dev (unsigned long dev_base, const char *s)
  595. #else
  596. void serial_puts (const char *s)
  597. #endif
  598. {
  599. while (*s) {
  600. #if defined(CONFIG_SERIAL_MULTI)
  601. serial_putc_dev (dev_base, *s++);
  602. #else
  603. serial_putc (*s++);
  604. #endif
  605. }
  606. }
  607. #if defined(CONFIG_SERIAL_MULTI)
  608. int serial_getc_dev (unsigned long dev_base)
  609. #else
  610. int serial_getc (void)
  611. #endif
  612. {
  613. unsigned char status = 0;
  614. while (1) {
  615. #if defined(CONFIG_HW_WATCHDOG)
  616. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  617. #endif /* CONFIG_HW_WATCHDOG */
  618. status = in8 (UART_BASE + UART_LSR);
  619. if ((status & asyncLSRDataReady1) != 0x0) {
  620. break;
  621. }
  622. if ((status & ( asyncLSRFramingError1 |
  623. asyncLSROverrunError1 |
  624. asyncLSRParityError1 |
  625. asyncLSRBreakInterrupt1 )) != 0) {
  626. out8 (UART_BASE + UART_LSR,
  627. asyncLSRFramingError1 |
  628. asyncLSROverrunError1 |
  629. asyncLSRParityError1 |
  630. asyncLSRBreakInterrupt1);
  631. }
  632. }
  633. return (0x000000ff & (int) in8 (UART_BASE));
  634. }
  635. #if defined(CONFIG_SERIAL_MULTI)
  636. int serial_tstc_dev (unsigned long dev_base)
  637. #else
  638. int serial_tstc (void)
  639. #endif
  640. {
  641. unsigned char status;
  642. status = in8 (UART_BASE + UART_LSR);
  643. if ((status & asyncLSRDataReady1) != 0x0) {
  644. return (1);
  645. }
  646. if ((status & ( asyncLSRFramingError1 |
  647. asyncLSROverrunError1 |
  648. asyncLSRParityError1 |
  649. asyncLSRBreakInterrupt1 )) != 0) {
  650. out8 (UART_BASE + UART_LSR,
  651. asyncLSRFramingError1 |
  652. asyncLSROverrunError1 |
  653. asyncLSRParityError1 |
  654. asyncLSRBreakInterrupt1);
  655. }
  656. return 0;
  657. }
  658. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  659. void serial_isr (void *arg)
  660. {
  661. int space;
  662. int c;
  663. const int rx_get = buf_info.rx_get;
  664. int rx_put = buf_info.rx_put;
  665. if (rx_get <= rx_put) {
  666. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  667. } else {
  668. space = rx_get - rx_put;
  669. }
  670. while (serial_tstc_dev (ACTING_UART0_BASE)) {
  671. c = serial_getc_dev (ACTING_UART0_BASE);
  672. if (space) {
  673. buf_info.rx_buffer[rx_put++] = c;
  674. space--;
  675. }
  676. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  677. rx_put = 0;
  678. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  679. /* Stop flow by setting RTS inactive */
  680. out8 (ACTING_UART0_BASE + UART_MCR,
  681. in8 (ACTING_UART0_BASE + UART_MCR) & (0xFF ^ 0x02));
  682. }
  683. }
  684. buf_info.rx_put = rx_put;
  685. }
  686. void serial_buffered_init (void)
  687. {
  688. serial_puts ("Switching to interrupt driven serial input mode.\n");
  689. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  690. buf_info.rx_put = 0;
  691. buf_info.rx_get = 0;
  692. if (in8 (ACTING_UART0_BASE + UART_MSR) & 0x10) {
  693. serial_puts ("Check CTS signal present on serial port: OK.\n");
  694. } else {
  695. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  696. }
  697. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  698. serial_isr /*interrupt_handler_t *handler */ ,
  699. (void *) &buf_info /*void *arg */ );
  700. /* Enable "RX Data Available" Interrupt on UART */
  701. /* out8(ACTING_UART0_BASE + UART_IER, in8(ACTING_UART0_BASE + UART_IER) |0x01); */
  702. out8 (ACTING_UART0_BASE + UART_IER, 0x01);
  703. /* Set DTR active */
  704. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x01);
  705. /* Start flow by setting RTS active */
  706. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  707. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  708. out8 (ACTING_UART0_BASE + UART_FCR, (1 << 6) | 1);
  709. }
  710. void serial_buffered_putc (const char c)
  711. {
  712. /* Wait for CTS */
  713. #if defined(CONFIG_HW_WATCHDOG)
  714. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10))
  715. WATCHDOG_RESET ();
  716. #else
  717. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10));
  718. #endif
  719. serial_putc (c);
  720. }
  721. void serial_buffered_puts (const char *s)
  722. {
  723. serial_puts (s);
  724. }
  725. int serial_buffered_getc (void)
  726. {
  727. int space;
  728. int c;
  729. int rx_get = buf_info.rx_get;
  730. int rx_put;
  731. #if defined(CONFIG_HW_WATCHDOG)
  732. while (rx_get == buf_info.rx_put)
  733. WATCHDOG_RESET ();
  734. #else
  735. while (rx_get == buf_info.rx_put);
  736. #endif
  737. c = buf_info.rx_buffer[rx_get++];
  738. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  739. rx_get = 0;
  740. buf_info.rx_get = rx_get;
  741. rx_put = buf_info.rx_put;
  742. if (rx_get <= rx_put) {
  743. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  744. } else {
  745. space = rx_get - rx_put;
  746. }
  747. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  748. /* Start flow by setting RTS active */
  749. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  750. }
  751. return c;
  752. }
  753. int serial_buffered_tstc (void)
  754. {
  755. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  756. }
  757. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  758. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  759. /*
  760. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  761. number 0 or number 1
  762. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  763. configuration has been already done
  764. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  765. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  766. */
  767. #if (CONFIG_KGDB_SER_INDEX & 2)
  768. void kgdb_serial_init (void)
  769. {
  770. volatile char val;
  771. unsigned short br_reg;
  772. get_clocks ();
  773. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  774. 5) / 10;
  775. /*
  776. * Init onboard 16550 UART
  777. */
  778. out8 (ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  779. out8 (ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  780. out8 (ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  781. out8 (ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  782. out8 (ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  783. out8 (ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  784. val = in8 (ACTING_UART1_BASE + UART_LSR); /* clear line status */
  785. val = in8 (ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  786. out8 (ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  787. out8 (ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  788. }
  789. void putDebugChar (const char c)
  790. {
  791. if (c == '\n')
  792. serial_putc ('\r');
  793. out8 (ACTING_UART1_BASE + UART_THR, c); /* put character out */
  794. /* check THRE bit, wait for transfer done */
  795. while ((in8 (ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  796. }
  797. void putDebugStr (const char *s)
  798. {
  799. while (*s) {
  800. serial_putc (*s++);
  801. }
  802. }
  803. int getDebugChar (void)
  804. {
  805. unsigned char status = 0;
  806. while (1) {
  807. status = in8 (ACTING_UART1_BASE + UART_LSR);
  808. if ((status & asyncLSRDataReady1) != 0x0) {
  809. break;
  810. }
  811. if ((status & ( asyncLSRFramingError1 |
  812. asyncLSROverrunError1 |
  813. asyncLSRParityError1 |
  814. asyncLSRBreakInterrupt1 )) != 0) {
  815. out8 (ACTING_UART1_BASE + UART_LSR,
  816. asyncLSRFramingError1 |
  817. asyncLSROverrunError1 |
  818. asyncLSRParityError1 |
  819. asyncLSRBreakInterrupt1);
  820. }
  821. }
  822. return (0x000000ff & (int) in8 (ACTING_UART1_BASE));
  823. }
  824. void kgdb_interruptible (int yes)
  825. {
  826. return;
  827. }
  828. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  829. void kgdb_serial_init (void)
  830. {
  831. serial_printf ("[on serial] ");
  832. }
  833. void putDebugChar (int c)
  834. {
  835. serial_putc (c);
  836. }
  837. void putDebugStr (const char *str)
  838. {
  839. serial_puts (str);
  840. }
  841. int getDebugChar (void)
  842. {
  843. return serial_getc ();
  844. }
  845. void kgdb_interruptible (int yes)
  846. {
  847. return;
  848. }
  849. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  850. #endif /* CFG_CMD_KGDB */
  851. #if defined(CONFIG_SERIAL_MULTI)
  852. int serial0_init(void)
  853. {
  854. return (serial_init_dev(UART0_BASE));
  855. }
  856. int serial1_init(void)
  857. {
  858. return (serial_init_dev(UART1_BASE));
  859. }
  860. void serial0_setbrg (void)
  861. {
  862. serial_setbrg_dev(UART0_BASE);
  863. }
  864. void serial1_setbrg (void)
  865. {
  866. serial_setbrg_dev(UART1_BASE);
  867. }
  868. void serial0_putc(const char c)
  869. {
  870. serial_putc_dev(UART0_BASE,c);
  871. }
  872. void serial1_putc(const char c)
  873. {
  874. serial_putc_dev(UART1_BASE, c);
  875. }
  876. void serial0_puts(const char *s)
  877. {
  878. serial_puts_dev(UART0_BASE, s);
  879. }
  880. void serial1_puts(const char *s)
  881. {
  882. serial_puts_dev(UART1_BASE, s);
  883. }
  884. int serial0_getc(void)
  885. {
  886. return(serial_getc_dev(UART0_BASE));
  887. }
  888. int serial1_getc(void)
  889. {
  890. return(serial_getc_dev(UART1_BASE));
  891. }
  892. int serial0_tstc(void)
  893. {
  894. return (serial_tstc_dev(UART0_BASE));
  895. }
  896. int serial1_tstc(void)
  897. {
  898. return (serial_tstc_dev(UART1_BASE));
  899. }
  900. struct serial_device serial0_device =
  901. {
  902. "serial0",
  903. "UART0",
  904. serial0_init,
  905. serial0_setbrg,
  906. serial0_getc,
  907. serial0_tstc,
  908. serial0_putc,
  909. serial0_puts,
  910. };
  911. struct serial_device serial1_device =
  912. {
  913. "serial1",
  914. "UART1",
  915. serial1_init,
  916. serial1_setbrg,
  917. serial1_getc,
  918. serial1_tstc,
  919. serial1_putc,
  920. serial1_puts,
  921. };
  922. #endif /* CONFIG_SERIAL_MULTI */
  923. #endif /* CONFIG_405GP || CONFIG_405CR */