serial.c 31 KB

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