serial.c 29 KB

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