serial.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. u32 reg;
  403. get_sys_info(&sysinfo);
  404. plloutb = ((CONFIG_SYS_CLK_FREQ * sysinfo.pllFwdDiv * sysinfo.pllFbkDiv)
  405. / sysinfo.pllFwdDivB);
  406. udiv = 256; /* Assume lowest possible serial clk */
  407. div = plloutb / (16 * baudrate); /* total divisor */
  408. umin = (plloutb / get_OPB_freq()) << 1; /* 2 x OPB divisor */
  409. diff = 256; /* highest possible */
  410. /* i is the test udiv value -- start with the largest
  411. * possible (256) to minimize serial clock and constrain
  412. * search to umin.
  413. */
  414. for (i = 256; i > umin; i--) {
  415. ibdiv = div / i;
  416. est = i * ibdiv;
  417. idiff = (est > div) ? (est-div) : (div-est);
  418. if (idiff == 0) {
  419. udiv = i;
  420. break; /* can't do better */
  421. } else if (idiff < diff) {
  422. udiv = i; /* best so far */
  423. diff = idiff; /* update lowest diff*/
  424. }
  425. }
  426. *pudiv = udiv;
  427. mfcpr(cprperd0, reg);
  428. reg &= ~0x0000ffff;
  429. reg |= ((udiv - 0) << 8) | (udiv - 0);
  430. mtcpr(cprperd0, reg);
  431. *pbdiv = div / udiv;
  432. }
  433. #endif /* defined(CONFIG_440) && !defined(CFG_EXT_SERIAL_CLK) */
  434. /*
  435. * Minimal serial functions needed to use one of the SMC ports
  436. * as serial console interface.
  437. */
  438. #if defined(CONFIG_440)
  439. #if defined(CONFIG_SERIAL_MULTI)
  440. int serial_init_dev (unsigned long dev_base)
  441. #else
  442. int serial_init(void)
  443. #endif
  444. {
  445. unsigned long reg;
  446. unsigned long udiv;
  447. unsigned short bdiv;
  448. volatile char val;
  449. #ifdef CFG_EXT_SERIAL_CLOCK
  450. unsigned long tmp;
  451. #endif
  452. MFREG(UART0_SDR, reg);
  453. reg &= ~CR0_MASK;
  454. #ifdef CFG_EXT_SERIAL_CLOCK
  455. reg |= CR0_EXTCLK_ENA;
  456. udiv = 1;
  457. tmp = gd->baudrate * 16;
  458. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  459. #else
  460. /* For 440, the cpu clock is on divider chain A, UART on divider
  461. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  462. * values that are subject to the 1/2 opb clock constraint
  463. */
  464. serial_divs (gd->baudrate, &udiv, &bdiv);
  465. #endif
  466. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  467. /*
  468. * Configure input clock to baudrate generator for all
  469. * available serial ports here
  470. */
  471. MTREG(UART0_SDR, reg);
  472. #if defined(UART1_SDR)
  473. MTREG(UART1_SDR, reg);
  474. #endif
  475. #if defined(UART2_SDR)
  476. MTREG(UART2_SDR, reg);
  477. #endif
  478. #if defined(UART3_SDR)
  479. MTREG(UART3_SDR, reg);
  480. #endif
  481. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  482. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  483. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  484. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  485. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  486. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  487. val = in8(UART_BASE + UART_LSR); /* clear line status */
  488. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  489. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  490. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  491. return (0);
  492. }
  493. #else /* !defined(CONFIG_440) */
  494. #if defined(CONFIG_SERIAL_MULTI)
  495. int serial_init_dev (unsigned long dev_base)
  496. #else
  497. int serial_init (void)
  498. #endif
  499. {
  500. unsigned long reg;
  501. unsigned long tmp;
  502. unsigned long clk;
  503. unsigned long udiv;
  504. unsigned short bdiv;
  505. volatile char val;
  506. #if defined(CONFIG_405EZ)
  507. serial_divs(gd->baudrate, &udiv, &bdiv);
  508. clk = tmp = reg = 0;
  509. #else
  510. #ifdef CONFIG_405EP
  511. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  512. clk = gd->cpu_clk;
  513. tmp = CFG_BASE_BAUD * 16;
  514. udiv = (clk + tmp / 2) / tmp;
  515. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  516. udiv = UDIV_MAX;
  517. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  518. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  519. mtdcr (cpc0_ucr, reg);
  520. #else /* CONFIG_405EP */
  521. reg = mfdcr(cntrl0) & ~CR0_MASK;
  522. #ifdef CFG_EXT_SERIAL_CLOCK
  523. clk = CFG_EXT_SERIAL_CLOCK;
  524. udiv = 1;
  525. reg |= CR0_EXTCLK_ENA;
  526. #else
  527. clk = gd->cpu_clk;
  528. #ifdef CFG_405_UART_ERRATA_59
  529. udiv = 31; /* Errata 59: stuck at 31 */
  530. #else
  531. tmp = CFG_BASE_BAUD * 16;
  532. udiv = (clk + tmp / 2) / tmp;
  533. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  534. udiv = UDIV_MAX;
  535. #endif
  536. #endif
  537. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  538. mtdcr (cntrl0, reg);
  539. #endif /* CONFIG_405EP */
  540. tmp = gd->baudrate * udiv * 16;
  541. bdiv = (clk + tmp / 2) / tmp;
  542. #endif /* CONFIG_405EZ */
  543. out8(UART_BASE + UART_LCR, 0x80); /* set DLAB bit */
  544. out8(UART_BASE + UART_DLL, bdiv); /* set baudrate divisor */
  545. out8(UART_BASE + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  546. out8(UART_BASE + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  547. out8(UART_BASE + UART_FCR, 0x00); /* disable FIFO */
  548. out8(UART_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  549. val = in8(UART_BASE + UART_LSR); /* clear line status */
  550. val = in8(UART_BASE + UART_RBR); /* read receive buffer */
  551. out8(UART_BASE + UART_SCR, 0x00); /* set scratchpad */
  552. out8(UART_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  553. return (0);
  554. }
  555. #endif /* if defined(CONFIG_440) */
  556. #if defined(CONFIG_SERIAL_MULTI)
  557. void serial_setbrg_dev (unsigned long dev_base)
  558. #else
  559. void serial_setbrg (void)
  560. #endif
  561. {
  562. #if defined(CONFIG_SERIAL_MULTI)
  563. serial_init_dev(dev_base);
  564. #else
  565. serial_init();
  566. #endif
  567. }
  568. #if defined(CONFIG_SERIAL_MULTI)
  569. void serial_putc_dev (unsigned long dev_base, const char c)
  570. #else
  571. void serial_putc (const char c)
  572. #endif
  573. {
  574. int i;
  575. if (c == '\n')
  576. #if defined(CONFIG_SERIAL_MULTI)
  577. serial_putc_dev (dev_base, '\r');
  578. #else
  579. serial_putc ('\r');
  580. #endif
  581. /* check THRE bit, wait for transmiter available */
  582. for (i = 1; i < 3500; i++) {
  583. if ((in8 (UART_BASE + UART_LSR) & 0x20) == 0x20)
  584. break;
  585. udelay (100);
  586. }
  587. out8 (UART_BASE + UART_THR, c); /* put character out */
  588. }
  589. #if defined(CONFIG_SERIAL_MULTI)
  590. void serial_puts_dev (unsigned long dev_base, const char *s)
  591. #else
  592. void serial_puts (const char *s)
  593. #endif
  594. {
  595. while (*s) {
  596. #if defined(CONFIG_SERIAL_MULTI)
  597. serial_putc_dev (dev_base, *s++);
  598. #else
  599. serial_putc (*s++);
  600. #endif
  601. }
  602. }
  603. #if defined(CONFIG_SERIAL_MULTI)
  604. int serial_getc_dev (unsigned long dev_base)
  605. #else
  606. int serial_getc (void)
  607. #endif
  608. {
  609. unsigned char status = 0;
  610. while (1) {
  611. #if defined(CONFIG_HW_WATCHDOG)
  612. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  613. #endif /* CONFIG_HW_WATCHDOG */
  614. status = in8 (UART_BASE + UART_LSR);
  615. if ((status & asyncLSRDataReady1) != 0x0) {
  616. break;
  617. }
  618. if ((status & ( asyncLSRFramingError1 |
  619. asyncLSROverrunError1 |
  620. asyncLSRParityError1 |
  621. asyncLSRBreakInterrupt1 )) != 0) {
  622. out8 (UART_BASE + UART_LSR,
  623. asyncLSRFramingError1 |
  624. asyncLSROverrunError1 |
  625. asyncLSRParityError1 |
  626. asyncLSRBreakInterrupt1);
  627. }
  628. }
  629. return (0x000000ff & (int) in8 (UART_BASE));
  630. }
  631. #if defined(CONFIG_SERIAL_MULTI)
  632. int serial_tstc_dev (unsigned long dev_base)
  633. #else
  634. int serial_tstc (void)
  635. #endif
  636. {
  637. unsigned char status;
  638. status = in8 (UART_BASE + UART_LSR);
  639. if ((status & asyncLSRDataReady1) != 0x0) {
  640. return (1);
  641. }
  642. if ((status & ( asyncLSRFramingError1 |
  643. asyncLSROverrunError1 |
  644. asyncLSRParityError1 |
  645. asyncLSRBreakInterrupt1 )) != 0) {
  646. out8 (UART_BASE + UART_LSR,
  647. asyncLSRFramingError1 |
  648. asyncLSROverrunError1 |
  649. asyncLSRParityError1 |
  650. asyncLSRBreakInterrupt1);
  651. }
  652. return 0;
  653. }
  654. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  655. void serial_isr (void *arg)
  656. {
  657. int space;
  658. int c;
  659. const int rx_get = buf_info.rx_get;
  660. int rx_put = buf_info.rx_put;
  661. if (rx_get <= rx_put) {
  662. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  663. } else {
  664. space = rx_get - rx_put;
  665. }
  666. while (serial_tstc_dev (ACTING_UART0_BASE)) {
  667. c = serial_getc_dev (ACTING_UART0_BASE);
  668. if (space) {
  669. buf_info.rx_buffer[rx_put++] = c;
  670. space--;
  671. }
  672. if (rx_put == CONFIG_SERIAL_SOFTWARE_FIFO)
  673. rx_put = 0;
  674. if (space < CONFIG_SERIAL_SOFTWARE_FIFO / 4) {
  675. /* Stop flow by setting RTS inactive */
  676. out8 (ACTING_UART0_BASE + UART_MCR,
  677. in8 (ACTING_UART0_BASE + UART_MCR) & (0xFF ^ 0x02));
  678. }
  679. }
  680. buf_info.rx_put = rx_put;
  681. }
  682. void serial_buffered_init (void)
  683. {
  684. serial_puts ("Switching to interrupt driven serial input mode.\n");
  685. buf_info.rx_buffer = malloc (CONFIG_SERIAL_SOFTWARE_FIFO);
  686. buf_info.rx_put = 0;
  687. buf_info.rx_get = 0;
  688. if (in8 (ACTING_UART0_BASE + UART_MSR) & 0x10) {
  689. serial_puts ("Check CTS signal present on serial port: OK.\n");
  690. } else {
  691. serial_puts ("WARNING: CTS signal not present on serial port.\n");
  692. }
  693. irq_install_handler ( VECNUM_U0 /*UART0 */ /*int vec */ ,
  694. serial_isr /*interrupt_handler_t *handler */ ,
  695. (void *) &buf_info /*void *arg */ );
  696. /* Enable "RX Data Available" Interrupt on UART */
  697. /* out8(ACTING_UART0_BASE + UART_IER, in8(ACTING_UART0_BASE + UART_IER) |0x01); */
  698. out8 (ACTING_UART0_BASE + UART_IER, 0x01);
  699. /* Set DTR active */
  700. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x01);
  701. /* Start flow by setting RTS active */
  702. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  703. /* Setup UART FIFO: RX trigger level: 4 byte, Enable FIFO */
  704. out8 (ACTING_UART0_BASE + UART_FCR, (1 << 6) | 1);
  705. }
  706. void serial_buffered_putc (const char c)
  707. {
  708. /* Wait for CTS */
  709. #if defined(CONFIG_HW_WATCHDOG)
  710. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10))
  711. WATCHDOG_RESET ();
  712. #else
  713. while (!(in8 (ACTING_UART0_BASE + UART_MSR) & 0x10));
  714. #endif
  715. serial_putc (c);
  716. }
  717. void serial_buffered_puts (const char *s)
  718. {
  719. serial_puts (s);
  720. }
  721. int serial_buffered_getc (void)
  722. {
  723. int space;
  724. int c;
  725. int rx_get = buf_info.rx_get;
  726. int rx_put;
  727. #if defined(CONFIG_HW_WATCHDOG)
  728. while (rx_get == buf_info.rx_put)
  729. WATCHDOG_RESET ();
  730. #else
  731. while (rx_get == buf_info.rx_put);
  732. #endif
  733. c = buf_info.rx_buffer[rx_get++];
  734. if (rx_get == CONFIG_SERIAL_SOFTWARE_FIFO)
  735. rx_get = 0;
  736. buf_info.rx_get = rx_get;
  737. rx_put = buf_info.rx_put;
  738. if (rx_get <= rx_put) {
  739. space = CONFIG_SERIAL_SOFTWARE_FIFO - (rx_put - rx_get);
  740. } else {
  741. space = rx_get - rx_put;
  742. }
  743. if (space > CONFIG_SERIAL_SOFTWARE_FIFO / 2) {
  744. /* Start flow by setting RTS active */
  745. out8 (ACTING_UART0_BASE + UART_MCR, in8 (ACTING_UART0_BASE + UART_MCR) | 0x02);
  746. }
  747. return c;
  748. }
  749. int serial_buffered_tstc (void)
  750. {
  751. return (buf_info.rx_get != buf_info.rx_put) ? 1 : 0;
  752. }
  753. #endif /* CONFIG_SERIAL_SOFTWARE_FIFO */
  754. #if defined(CONFIG_CMD_KGDB)
  755. /*
  756. AS HARNOIS : according to CONFIG_KGDB_SER_INDEX kgdb uses serial port
  757. number 0 or number 1
  758. - if CONFIG_KGDB_SER_INDEX = 1 => serial port number 0 :
  759. configuration has been already done
  760. - if CONFIG_KGDB_SER_INDEX = 2 => serial port number 1 :
  761. configure port 1 for serial I/O with rate = CONFIG_KGDB_BAUDRATE
  762. */
  763. #if (CONFIG_KGDB_SER_INDEX & 2)
  764. void kgdb_serial_init (void)
  765. {
  766. volatile char val;
  767. unsigned short br_reg;
  768. get_clocks ();
  769. br_reg = (((((gd->cpu_clk / 16) / 18) * 10) / CONFIG_KGDB_BAUDRATE) +
  770. 5) / 10;
  771. /*
  772. * Init onboard 16550 UART
  773. */
  774. out8 (ACTING_UART1_BASE + UART_LCR, 0x80); /* set DLAB bit */
  775. out8 (ACTING_UART1_BASE + UART_DLL, (br_reg & 0x00ff)); /* set divisor for 9600 baud */
  776. out8 (ACTING_UART1_BASE + UART_DLM, ((br_reg & 0xff00) >> 8)); /* set divisor for 9600 baud */
  777. out8 (ACTING_UART1_BASE + UART_LCR, 0x03); /* line control 8 bits no parity */
  778. out8 (ACTING_UART1_BASE + UART_FCR, 0x00); /* disable FIFO */
  779. out8 (ACTING_UART1_BASE + UART_MCR, 0x00); /* no modem control DTR RTS */
  780. val = in8 (ACTING_UART1_BASE + UART_LSR); /* clear line status */
  781. val = in8 (ACTING_UART1_BASE + UART_RBR); /* read receive buffer */
  782. out8 (ACTING_UART1_BASE + UART_SCR, 0x00); /* set scratchpad */
  783. out8 (ACTING_UART1_BASE + UART_IER, 0x00); /* set interrupt enable reg */
  784. }
  785. void putDebugChar (const char c)
  786. {
  787. if (c == '\n')
  788. serial_putc ('\r');
  789. out8 (ACTING_UART1_BASE + UART_THR, c); /* put character out */
  790. /* check THRE bit, wait for transfer done */
  791. while ((in8 (ACTING_UART1_BASE + UART_LSR) & 0x20) != 0x20);
  792. }
  793. void putDebugStr (const char *s)
  794. {
  795. while (*s) {
  796. serial_putc (*s++);
  797. }
  798. }
  799. int getDebugChar (void)
  800. {
  801. unsigned char status = 0;
  802. while (1) {
  803. status = in8 (ACTING_UART1_BASE + UART_LSR);
  804. if ((status & asyncLSRDataReady1) != 0x0) {
  805. break;
  806. }
  807. if ((status & ( asyncLSRFramingError1 |
  808. asyncLSROverrunError1 |
  809. asyncLSRParityError1 |
  810. asyncLSRBreakInterrupt1 )) != 0) {
  811. out8 (ACTING_UART1_BASE + UART_LSR,
  812. asyncLSRFramingError1 |
  813. asyncLSROverrunError1 |
  814. asyncLSRParityError1 |
  815. asyncLSRBreakInterrupt1);
  816. }
  817. }
  818. return (0x000000ff & (int) in8 (ACTING_UART1_BASE));
  819. }
  820. void kgdb_interruptible (int yes)
  821. {
  822. return;
  823. }
  824. #else /* ! (CONFIG_KGDB_SER_INDEX & 2) */
  825. void kgdb_serial_init (void)
  826. {
  827. serial_printf ("[on serial] ");
  828. }
  829. void putDebugChar (int c)
  830. {
  831. serial_putc (c);
  832. }
  833. void putDebugStr (const char *str)
  834. {
  835. serial_puts (str);
  836. }
  837. int getDebugChar (void)
  838. {
  839. return serial_getc ();
  840. }
  841. void kgdb_interruptible (int yes)
  842. {
  843. return;
  844. }
  845. #endif /* (CONFIG_KGDB_SER_INDEX & 2) */
  846. #endif
  847. #if defined(CONFIG_SERIAL_MULTI)
  848. int serial0_init(void)
  849. {
  850. return (serial_init_dev(UART0_BASE));
  851. }
  852. int serial1_init(void)
  853. {
  854. return (serial_init_dev(UART1_BASE));
  855. }
  856. void serial0_setbrg (void)
  857. {
  858. serial_setbrg_dev(UART0_BASE);
  859. }
  860. void serial1_setbrg (void)
  861. {
  862. serial_setbrg_dev(UART1_BASE);
  863. }
  864. void serial0_putc(const char c)
  865. {
  866. serial_putc_dev(UART0_BASE,c);
  867. }
  868. void serial1_putc(const char c)
  869. {
  870. serial_putc_dev(UART1_BASE, c);
  871. }
  872. void serial0_puts(const char *s)
  873. {
  874. serial_puts_dev(UART0_BASE, s);
  875. }
  876. void serial1_puts(const char *s)
  877. {
  878. serial_puts_dev(UART1_BASE, s);
  879. }
  880. int serial0_getc(void)
  881. {
  882. return(serial_getc_dev(UART0_BASE));
  883. }
  884. int serial1_getc(void)
  885. {
  886. return(serial_getc_dev(UART1_BASE));
  887. }
  888. int serial0_tstc(void)
  889. {
  890. return (serial_tstc_dev(UART0_BASE));
  891. }
  892. int serial1_tstc(void)
  893. {
  894. return (serial_tstc_dev(UART1_BASE));
  895. }
  896. struct serial_device serial0_device =
  897. {
  898. "serial0",
  899. "UART0",
  900. serial0_init,
  901. serial0_setbrg,
  902. serial0_getc,
  903. serial0_tstc,
  904. serial0_putc,
  905. serial0_puts,
  906. };
  907. struct serial_device serial1_device =
  908. {
  909. "serial1",
  910. "UART1",
  911. serial1_init,
  912. serial1_setbrg,
  913. serial1_getc,
  914. serial1_tstc,
  915. serial1_putc,
  916. serial1_puts,
  917. };
  918. #endif /* CONFIG_SERIAL_MULTI */
  919. #endif /* CONFIG_405GP || CONFIG_405CR */