integratorap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * (C) Copyright 2002
  7. * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
  8. *
  9. * (C) Copyright 2003
  10. * Texas Instruments, <www.ti.com>
  11. * Kshitij Gupta <Kshitij@ti.com>
  12. *
  13. * (C) Copyright 2004
  14. * ARM Ltd.
  15. * Philippe Robin, <philippe.robin@arm.com>
  16. *
  17. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of
  23. * the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  33. * MA 02111-1307 USA
  34. */
  35. #include <common.h>
  36. #ifdef CONFIG_PCI
  37. #include <pci.h>
  38. #endif
  39. DECLARE_GLOBAL_DATA_PTR;
  40. void flash__init (void);
  41. void ether__init (void);
  42. void peripheral_power_enable (void);
  43. #if defined(CONFIG_SHOW_BOOT_PROGRESS)
  44. void show_boot_progress(int progress)
  45. {
  46. printf("Boot reached stage %d\n", progress);
  47. }
  48. #endif
  49. #define COMP_MODE_ENABLE ((unsigned int)0x0000EAEF)
  50. static inline void delay (unsigned long loops)
  51. {
  52. __asm__ volatile ("1:\n"
  53. "subs %0, %1, #1\n"
  54. "bne 1b":"=r" (loops):"0" (loops));
  55. }
  56. /*
  57. * Miscellaneous platform dependent initialisations
  58. */
  59. int board_init (void)
  60. {
  61. /* arch number of Integrator Board */
  62. gd->bd->bi_arch_number = MACH_TYPE_INTEGRATOR;
  63. /* adress of boot parameters */
  64. gd->bd->bi_boot_params = 0x00000100;
  65. gd->flags = 0;
  66. #ifdef CONFIG_CM_REMAP
  67. extern void cm_remap(void);
  68. cm_remap(); /* remaps writeable memory to 0x00000000 */
  69. #endif
  70. icache_enable ();
  71. flash__init ();
  72. return 0;
  73. }
  74. int misc_init_r (void)
  75. {
  76. #ifdef CONFIG_PCI
  77. pci_init();
  78. #endif
  79. setenv("verify", "n");
  80. return (0);
  81. }
  82. /*
  83. * Initialize PCI Devices, report devices found.
  84. */
  85. #ifdef CONFIG_PCI
  86. #ifndef CONFIG_PCI_PNP
  87. static struct pci_config_table pci_integrator_config_table[] = {
  88. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x0f, PCI_ANY_ID,
  89. pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
  90. PCI_ENET0_MEMADDR,
  91. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }},
  92. { }
  93. };
  94. #endif
  95. /* V3 access routines */
  96. #define _V3Write16(o,v) (*(volatile unsigned short *)(PCI_V3_BASE + (unsigned int)(o)) = (unsigned short)(v))
  97. #define _V3Read16(o) (*(volatile unsigned short *)(PCI_V3_BASE + (unsigned int)(o)))
  98. #define _V3Write32(o,v) (*(volatile unsigned int *)(PCI_V3_BASE + (unsigned int)(o)) = (unsigned int)(v))
  99. #define _V3Read32(o) (*(volatile unsigned int *)(PCI_V3_BASE + (unsigned int)(o)))
  100. /* Compute address necessary to access PCI config space for the given */
  101. /* bus and device. */
  102. #define PCI_CONFIG_ADDRESS( __bus, __devfn, __offset ) ({ \
  103. unsigned int __address, __devicebit; \
  104. unsigned short __mapaddress; \
  105. unsigned int __dev = PCI_DEV (__devfn); /* FIXME to check!! (slot?) */ \
  106. \
  107. if (__bus == 0) { \
  108. /* local bus segment so need a type 0 config cycle */ \
  109. /* build the PCI configuration "address" with one-hot in A31-A11 */ \
  110. __address = PCI_CONFIG_BASE; \
  111. __address |= ((__devfn & 0x07) << 8); \
  112. __address |= __offset & 0xFF; \
  113. __mapaddress = 0x000A; /* 101=>config cycle, 0=>A1=A0=0 */ \
  114. __devicebit = (1 << (__dev + 11)); \
  115. \
  116. if ((__devicebit & 0xFF000000) != 0) { \
  117. /* high order bits are handled by the MAP register */ \
  118. __mapaddress |= (__devicebit >> 16); \
  119. } else { \
  120. /* low order bits handled directly in the address */ \
  121. __address |= __devicebit; \
  122. } \
  123. } else { /* bus !=0 */ \
  124. /* not the local bus segment so need a type 1 config cycle */ \
  125. /* A31-A24 are don't care (so clear to 0) */ \
  126. __mapaddress = 0x000B; /* 101=>config cycle, 1=>A1&A0 from PCI_CFG */ \
  127. __address = PCI_CONFIG_BASE; \
  128. __address |= ((__bus & 0xFF) << 16); /* bits 23..16 = bus number */ \
  129. __address |= ((__dev & 0x1F) << 11); /* bits 15..11 = device number */ \
  130. __address |= ((__devfn & 0x07) << 8); /* bits 10..8 = function number */ \
  131. __address |= __offset & 0xFF; /* bits 7..0 = register number */ \
  132. } \
  133. _V3Write16 (V3_LB_MAP1, __mapaddress); \
  134. __address; \
  135. })
  136. /* _V3OpenConfigWindow - open V3 configuration window */
  137. #define _V3OpenConfigWindow() { \
  138. /* Set up base0 to see all 512Mbytes of memory space (not */ \
  139. /* prefetchable), this frees up base1 for re-use by configuration*/ \
  140. /* memory */ \
  141. \
  142. _V3Write32 (V3_LB_BASE0, ((INTEGRATOR_PCI_BASE & 0xFFF00000) | \
  143. 0x90 | V3_LB_BASE_M_ENABLE)); \
  144. /* Set up base1 to point into configuration space, note that MAP1 */ \
  145. /* register is set up by pciMakeConfigAddress(). */ \
  146. \
  147. _V3Write32 (V3_LB_BASE1, ((CPU_PCI_CNFG_ADRS & 0xFFF00000) | \
  148. 0x40 | V3_LB_BASE_M_ENABLE)); \
  149. }
  150. /* _V3CloseConfigWindow - close V3 configuration window */
  151. #define _V3CloseConfigWindow() { \
  152. /* Reassign base1 for use by prefetchable PCI memory */ \
  153. _V3Write32 (V3_LB_BASE1, (((INTEGRATOR_PCI_BASE + 0x10000000) & 0xFFF00000) \
  154. | 0x84 | V3_LB_BASE_M_ENABLE)); \
  155. _V3Write16 (V3_LB_MAP1, \
  156. (((INTEGRATOR_PCI_BASE + 0x10000000) & 0xFFF00000) >> 16) | 0x0006); \
  157. \
  158. /* And shrink base0 back to a 256M window (NOTE: MAP0 already correct) */ \
  159. \
  160. _V3Write32 (V3_LB_BASE0, ((INTEGRATOR_PCI_BASE & 0xFFF00000) | \
  161. 0x80 | V3_LB_BASE_M_ENABLE)); \
  162. }
  163. static int pci_integrator_read_byte (struct pci_controller *hose, pci_dev_t dev,
  164. int offset, unsigned char *val)
  165. {
  166. _V3OpenConfigWindow ();
  167. *val = *(volatile unsigned char *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  168. PCI_FUNC (dev),
  169. offset);
  170. _V3CloseConfigWindow ();
  171. return 0;
  172. }
  173. static int pci_integrator_read__word (struct pci_controller *hose,
  174. pci_dev_t dev, int offset,
  175. unsigned short *val)
  176. {
  177. _V3OpenConfigWindow ();
  178. *val = *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  179. PCI_FUNC (dev),
  180. offset);
  181. _V3CloseConfigWindow ();
  182. return 0;
  183. }
  184. static int pci_integrator_read_dword (struct pci_controller *hose,
  185. pci_dev_t dev, int offset,
  186. unsigned int *val)
  187. {
  188. _V3OpenConfigWindow ();
  189. *val = *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  190. PCI_FUNC (dev),
  191. offset);
  192. *val |= (*(volatile unsigned int *)
  193. PCI_CONFIG_ADDRESS (PCI_BUS (dev), PCI_FUNC (dev),
  194. (offset + 2))) << 16;
  195. _V3CloseConfigWindow ();
  196. return 0;
  197. }
  198. static int pci_integrator_write_byte (struct pci_controller *hose,
  199. pci_dev_t dev, int offset,
  200. unsigned char val)
  201. {
  202. _V3OpenConfigWindow ();
  203. *(volatile unsigned char *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  204. PCI_FUNC (dev),
  205. offset) = val;
  206. _V3CloseConfigWindow ();
  207. return 0;
  208. }
  209. static int pci_integrator_write_word (struct pci_controller *hose,
  210. pci_dev_t dev, int offset,
  211. unsigned short val)
  212. {
  213. _V3OpenConfigWindow ();
  214. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  215. PCI_FUNC (dev),
  216. offset) = val;
  217. _V3CloseConfigWindow ();
  218. return 0;
  219. }
  220. static int pci_integrator_write_dword (struct pci_controller *hose,
  221. pci_dev_t dev, int offset,
  222. unsigned int val)
  223. {
  224. _V3OpenConfigWindow ();
  225. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  226. PCI_FUNC (dev),
  227. offset) = (val & 0xFFFF);
  228. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  229. PCI_FUNC (dev),
  230. (offset + 2)) = ((val >> 16) & 0xFFFF);
  231. _V3CloseConfigWindow ();
  232. return 0;
  233. }
  234. /******************************
  235. * PCI initialisation
  236. ******************************/
  237. struct pci_controller integrator_hose = {
  238. #ifndef CONFIG_PCI_PNP
  239. config_table: pci_integrator_config_table,
  240. #endif
  241. };
  242. void pci_init_board (void)
  243. {
  244. volatile int i, j;
  245. struct pci_controller *hose = &integrator_hose;
  246. /* setting this register will take the V3 out of reset */
  247. *(volatile unsigned int *) (INTEGRATOR_SC_PCIENABLE) = 1;
  248. /* wait a few usecs to settle the device and the PCI bus */
  249. for (i = 0; i < 100; i++)
  250. j = i + 1;
  251. /* Now write the Base I/O Address Word to V3_BASE + 0x6C */
  252. *(volatile unsigned short *) (V3_BASE + V3_LB_IO_BASE) =
  253. (unsigned short) (V3_BASE >> 16);
  254. do {
  255. *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA) = 0xAA;
  256. *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA + 4) =
  257. 0x55;
  258. } while (*(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA) != 0xAA
  259. || *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA +
  260. 4) != 0x55);
  261. /* Make sure that V3 register access is not locked, if it is, unlock it */
  262. if ((*(volatile unsigned short *) (V3_BASE + V3_SYSTEM) &
  263. V3_SYSTEM_M_LOCK)
  264. == V3_SYSTEM_M_LOCK)
  265. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) = 0xA05F;
  266. /* Ensure that the slave accesses from PCI are disabled while we */
  267. /* setup windows */
  268. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) &=
  269. ~(V3_COMMAND_M_MEM_EN | V3_COMMAND_M_IO_EN);
  270. /* Clear RST_OUT to 0; keep the PCI bus in reset until we've finished */
  271. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) &=
  272. ~V3_SYSTEM_M_RST_OUT;
  273. /* Make all accesses from PCI space retry until we're ready for them */
  274. *(volatile unsigned short *) (V3_BASE + V3_PCI_CFG) |=
  275. V3_PCI_CFG_M_RETRY_EN;
  276. /* Set up any V3 PCI Configuration Registers that we absolutely have to */
  277. /* LB_CFG controls Local Bus protocol. */
  278. /* Enable LocalBus byte strobes for READ accesses too. */
  279. /* set bit 7 BE_IMODE and bit 6 BE_OMODE */
  280. *(volatile unsigned short *) (V3_BASE + V3_LB_CFG) |= 0x0C0;
  281. /* PCI_CMD controls overall PCI operation. */
  282. /* Enable PCI bus master. */
  283. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) |= 0x04;
  284. /* PCI_MAP0 controls where the PCI to CPU memory window is on Local Bus */
  285. *(volatile unsigned int *) (V3_BASE + V3_PCI_MAP0) =
  286. (INTEGRATOR_BOOT_ROM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_512M |
  287. V3_PCI_MAP_M_REG_EN |
  288. V3_PCI_MAP_M_ENABLE);
  289. /* PCI_BASE0 is the PCI address of the start of the window */
  290. *(volatile unsigned int *) (V3_BASE + V3_PCI_BASE0) =
  291. INTEGRATOR_BOOT_ROM_BASE;
  292. /* PCI_MAP1 is LOCAL address of the start of the window */
  293. *(volatile unsigned int *) (V3_BASE + V3_PCI_MAP1) =
  294. (INTEGRATOR_HDR0_SDRAM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_1024M |
  295. V3_PCI_MAP_M_REG_EN |
  296. V3_PCI_MAP_M_ENABLE);
  297. /* PCI_BASE1 is the PCI address of the start of the window */
  298. *(volatile unsigned int *) (V3_BASE + V3_PCI_BASE1) =
  299. INTEGRATOR_HDR0_SDRAM_BASE;
  300. /* Set up the windows from local bus memory into PCI configuration, */
  301. /* I/O and Memory. */
  302. /* PCI I/O, LB_BASE2 and LB_MAP2 are used exclusively for this. */
  303. *(volatile unsigned short *) (V3_BASE + V3_LB_BASE2) =
  304. ((CPU_PCI_IO_ADRS >> 24) << 8) | V3_LB_BASE_M_ENABLE;
  305. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP2) = 0;
  306. /* PCI Configuration, use LB_BASE1/LB_MAP1. */
  307. /* PCI Memory use LB_BASE0/LB_MAP0 and LB_BASE1/LB_MAP1 */
  308. /* Map first 256Mbytes as non-prefetchable via BASE0/MAP0 */
  309. /* (INTEGRATOR_PCI_BASE == PCI_MEM_BASE) */
  310. *(volatile unsigned int *) (V3_BASE + V3_LB_BASE0) =
  311. INTEGRATOR_PCI_BASE | (0x80 | V3_LB_BASE_M_ENABLE);
  312. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP0) =
  313. ((INTEGRATOR_PCI_BASE >> 20) << 0x4) | 0x0006;
  314. /* Map second 256 Mbytes as prefetchable via BASE1/MAP1 */
  315. *(volatile unsigned int *) (V3_BASE + V3_LB_BASE1) =
  316. INTEGRATOR_PCI_BASE | (0x84 | V3_LB_BASE_M_ENABLE);
  317. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP1) =
  318. (((INTEGRATOR_PCI_BASE + 0x10000000) >> 20) << 4) | 0x0006;
  319. /* Allow accesses to PCI Configuration space */
  320. /* and set up A1, A0 for type 1 config cycles */
  321. *(volatile unsigned short *) (V3_BASE + V3_PCI_CFG) =
  322. ((*(volatile unsigned short *) (V3_BASE + V3_PCI_CFG)) &
  323. ~(V3_PCI_CFG_M_RETRY_EN | V3_PCI_CFG_M_AD_LOW1)) |
  324. V3_PCI_CFG_M_AD_LOW0;
  325. /* now we can allow in PCI MEMORY accesses */
  326. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) =
  327. (*(volatile unsigned short *) (V3_BASE + V3_PCI_CMD)) |
  328. V3_COMMAND_M_MEM_EN;
  329. /* Set RST_OUT to take the PCI bus is out of reset, PCI devices can */
  330. /* initialise and lock the V3 system register so that no one else */
  331. /* can play with it */
  332. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) =
  333. (*(volatile unsigned short *) (V3_BASE + V3_SYSTEM)) |
  334. V3_SYSTEM_M_RST_OUT;
  335. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) =
  336. (*(volatile unsigned short *) (V3_BASE + V3_SYSTEM)) |
  337. V3_SYSTEM_M_LOCK;
  338. /*
  339. * Register the hose
  340. */
  341. hose->first_busno = 0;
  342. hose->last_busno = 0xff;
  343. /* System memory space */
  344. pci_set_region (hose->regions + 0,
  345. 0x00000000, 0x40000000, 0x01000000,
  346. PCI_REGION_MEM | PCI_REGION_MEMORY);
  347. /* PCI Memory - config space */
  348. pci_set_region (hose->regions + 1,
  349. 0x00000000, 0x62000000, 0x01000000, PCI_REGION_MEM);
  350. /* PCI V3 regs */
  351. pci_set_region (hose->regions + 2,
  352. 0x00000000, 0x61000000, 0x00080000, PCI_REGION_MEM);
  353. /* PCI I/O space */
  354. pci_set_region (hose->regions + 3,
  355. 0x00000000, 0x60000000, 0x00010000, PCI_REGION_IO);
  356. pci_set_ops (hose,
  357. pci_integrator_read_byte,
  358. pci_integrator_read__word,
  359. pci_integrator_read_dword,
  360. pci_integrator_write_byte,
  361. pci_integrator_write_word, pci_integrator_write_dword);
  362. hose->region_count = 4;
  363. pci_register_hose (hose);
  364. pciauto_config_init (hose);
  365. pciauto_config_device (hose, 0);
  366. hose->last_busno = pci_hose_scan (hose);
  367. }
  368. #endif
  369. /******************************
  370. Routine:
  371. Description:
  372. ******************************/
  373. void flash__init (void)
  374. {
  375. }
  376. /*************************************************************
  377. Routine:ether__init
  378. Description: take the Ethernet controller out of reset and wait
  379. for the EEPROM load to complete.
  380. *************************************************************/
  381. void ether__init (void)
  382. {
  383. }
  384. /******************************
  385. Routine:
  386. Description:
  387. ******************************/
  388. int dram_init (void)
  389. {
  390. gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
  391. gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
  392. #ifdef CONFIG_CM_SPD_DETECT
  393. {
  394. extern void dram_query(void);
  395. unsigned long cm_reg_sdram;
  396. unsigned long sdram_shift;
  397. dram_query(); /* Assembler accesses to CM registers */
  398. /* Queries the SPD values */
  399. /* Obtain the SDRAM size from the CM SDRAM register */
  400. cm_reg_sdram = *(volatile ulong *)(CM_BASE + OS_SDRAM);
  401. /* Register SDRAM size
  402. *
  403. * 0xXXXXXXbbb000bb 16 MB
  404. * 0xXXXXXXbbb001bb 32 MB
  405. * 0xXXXXXXbbb010bb 64 MB
  406. * 0xXXXXXXbbb011bb 128 MB
  407. * 0xXXXXXXbbb100bb 256 MB
  408. *
  409. */
  410. sdram_shift = ((cm_reg_sdram & 0x0000001C)/4)%4;
  411. gd->bd->bi_dram[0].size = 0x01000000 << sdram_shift;
  412. }
  413. #endif /* CM_SPD_DETECT */
  414. return 0;
  415. }
  416. /* The Integrator/AP timer1 is clocked at 24MHz
  417. * can be divided by 16 or 256
  418. * and is a 16-bit counter
  419. */
  420. /* U-Boot expects a 32 bit timer running at CFG_HZ*/
  421. static ulong timestamp; /* U-Boot ticks since startup */
  422. static ulong total_count = 0; /* Total timer count */
  423. static ulong lastdec; /* Timer reading at last call */
  424. static ulong div_clock = 256; /* Divisor applied to the timer clock */
  425. static ulong div_timer = 1; /* Divisor to convert timer reading
  426. * change to U-Boot ticks
  427. */
  428. /* CFG_HZ = CFG_HZ_CLOCK/(div_clock * div_timer) */
  429. #define TIMER_LOAD_VAL 0x0000FFFFL
  430. #define READ_TIMER ((*(volatile ulong *)(CFG_TIMERBASE+4)) & 0x0000FFFFL)
  431. /* all function return values in U-Boot ticks i.e. (1/CFG_HZ) sec
  432. * - unless otherwise stated
  433. */
  434. /* starts a counter
  435. * - the Integrator/AP timer issues an interrupt
  436. * each time it reaches zero
  437. */
  438. int interrupt_init (void)
  439. {
  440. /* Load timer with initial value */
  441. *(volatile ulong *)(CFG_TIMERBASE + 0) = TIMER_LOAD_VAL;
  442. /* Set timer to be
  443. * enabled 1
  444. * free-running 0
  445. * XX 00
  446. * divider 256 10
  447. * XX 00
  448. */
  449. *(volatile ulong *)(CFG_TIMERBASE + 8) = 0x00000088;
  450. total_count = 0;
  451. /* init the timestamp and lastdec value */
  452. reset_timer_masked();
  453. div_timer = CFG_HZ_CLOCK / CFG_HZ;
  454. div_timer /= div_clock;
  455. return (0);
  456. }
  457. /*
  458. * timer without interrupts
  459. */
  460. void reset_timer (void)
  461. {
  462. reset_timer_masked ();
  463. }
  464. ulong get_timer (ulong base_ticks)
  465. {
  466. return get_timer_masked () - base_ticks;
  467. }
  468. void set_timer (ulong ticks)
  469. {
  470. timestamp = ticks;
  471. total_count = ticks * div_timer;
  472. reset_timer_masked();
  473. }
  474. /* delay x useconds */
  475. void udelay (unsigned long usec)
  476. {
  477. ulong tmo, tmp;
  478. /* Convert to U-Boot ticks */
  479. tmo = usec * CFG_HZ;
  480. tmo /= (1000000L);
  481. tmp = get_timer_masked(); /* get current timestamp */
  482. tmo += tmp; /* wake up timestamp */
  483. while (get_timer_masked () < tmo) { /* loop till event */
  484. /*NOP*/;
  485. }
  486. }
  487. void reset_timer_masked (void)
  488. {
  489. /* reset time */
  490. lastdec = READ_TIMER; /* capture current decrementer value */
  491. timestamp = 0; /* start "advancing" time stamp from 0 */
  492. }
  493. /* converts the timer reading to U-Boot ticks */
  494. /* the timestamp is the number of ticks since reset */
  495. /* This routine does not detect wraps unless called regularly
  496. ASSUMES a call at least every 16 seconds to detect every reload */
  497. ulong get_timer_masked (void)
  498. {
  499. ulong now = READ_TIMER; /* current count */
  500. if (now > lastdec) {
  501. /* Must have wrapped */
  502. total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
  503. } else {
  504. total_count += lastdec - now;
  505. }
  506. lastdec = now;
  507. timestamp = total_count/div_timer;
  508. return timestamp;
  509. }
  510. /* waits specified delay value and resets timestamp */
  511. void udelay_masked (unsigned long usec)
  512. {
  513. udelay(usec);
  514. }
  515. /*
  516. * This function is derived from PowerPC code (read timebase as long long).
  517. * On ARM it just returns the timer value.
  518. */
  519. unsigned long long get_ticks(void)
  520. {
  521. return get_timer(0);
  522. }
  523. /*
  524. * Return the timebase clock frequency
  525. * i.e. how often the timer decrements
  526. */
  527. ulong get_tbclk (void)
  528. {
  529. return CFG_HZ_CLOCK/div_clock;
  530. }