serial.c 30 KB

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