cpu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * (C) Copyright 2004
  3. * DAVE Srl
  4. * http://www.dave-tech.it
  5. * http://www.wawnet.biz
  6. * mailto:info@wawnet.biz
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * S3C44B0 CPU specific code
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <asm/hardware.h>
  32. static void s3c44b0_flush_cache(void)
  33. {
  34. volatile int i;
  35. /* flush cycle */
  36. for(i=0x10002000;i<0x10004800;i+=16)
  37. {
  38. *((int *)i)=0x0;
  39. }
  40. }
  41. int cpu_init (void)
  42. {
  43. icache_enable();
  44. return 0;
  45. }
  46. int cleanup_before_linux (void)
  47. {
  48. /*
  49. cache memory should be enabled before calling
  50. Linux to make the kernel uncompression faster
  51. */
  52. icache_enable();
  53. disable_interrupts ();
  54. return 0;
  55. }
  56. void reset_cpu (ulong addr)
  57. {
  58. /*
  59. reset the cpu using watchdog
  60. */
  61. /* Disable the watchdog.*/
  62. WTCON&=~(1<<5);
  63. /* set the timeout value to a short time... */
  64. WTCNT = 0x1;
  65. /* Enable the watchdog. */
  66. WTCON|=1;
  67. WTCON|=(1<<5);
  68. while(1) {
  69. /*NOP*/
  70. }
  71. }
  72. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  73. {
  74. disable_interrupts ();
  75. reset_cpu (0);
  76. /*NOTREACHED*/
  77. return (0);
  78. }
  79. void icache_enable (void)
  80. {
  81. ulong reg;
  82. s3c44b0_flush_cache();
  83. /*
  84. Init cache
  85. Non-cacheable area (everything outside RAM)
  86. 0x0000:0000 - 0x0C00:0000
  87. */
  88. NCACHBE0 = 0xC0000000;
  89. NCACHBE1 = 0x00000000;
  90. /*
  91. Enable chache
  92. */
  93. reg = SYSCFG;
  94. reg |= 0x00000006; /* 8kB */
  95. SYSCFG = reg;
  96. }
  97. void icache_disable (void)
  98. {
  99. ulong reg;
  100. reg = SYSCFG;
  101. reg &= ~0x00000006; /* 8kB */
  102. SYSCFG = reg;
  103. }
  104. int icache_status (void)
  105. {
  106. return 0;
  107. }
  108. void dcache_enable (void)
  109. {
  110. icache_enable();
  111. }
  112. void dcache_disable (void)
  113. {
  114. icache_disable();
  115. }
  116. int dcache_status (void)
  117. {
  118. return dcache_status();
  119. }
  120. /*
  121. RTC stuff
  122. */
  123. #include <rtc.h>
  124. #ifndef BCD2HEX
  125. #define BCD2HEX(n) ((n>>4)*10+(n&0x0f))
  126. #endif
  127. #ifndef HEX2BCD
  128. #define HEX2BCD(x) ((((x) / 10) << 4) + (x) % 10)
  129. #endif
  130. int rtc_get (struct rtc_time* tm)
  131. {
  132. RTCCON |= 1;
  133. tm->tm_year = BCD2HEX(BCDYEAR);
  134. tm->tm_mon = BCD2HEX(BCDMON);
  135. tm->tm_wday = BCD2HEX(BCDDATE);
  136. tm->tm_mday = BCD2HEX(BCDDAY);
  137. tm->tm_hour = BCD2HEX(BCDHOUR);
  138. tm->tm_min = BCD2HEX(BCDMIN);
  139. tm->tm_sec = BCD2HEX(BCDSEC);
  140. if (tm->tm_sec==0) {
  141. /* we have to re-read the rtc data because of the "one second deviation" problem */
  142. /* see RTC datasheet for more info about it */
  143. tm->tm_year = BCD2HEX(BCDYEAR);
  144. tm->tm_mon = BCD2HEX(BCDMON);
  145. tm->tm_mday = BCD2HEX(BCDDAY);
  146. tm->tm_wday = BCD2HEX(BCDDATE);
  147. tm->tm_hour = BCD2HEX(BCDHOUR);
  148. tm->tm_min = BCD2HEX(BCDMIN);
  149. tm->tm_sec = BCD2HEX(BCDSEC);
  150. }
  151. RTCCON &= ~1;
  152. if(tm->tm_year >= 70)
  153. tm->tm_year += 1900;
  154. else
  155. tm->tm_year += 2000;
  156. return 0;
  157. }
  158. int rtc_set (struct rtc_time* tm)
  159. {
  160. if(tm->tm_year < 2000)
  161. tm->tm_year -= 1900;
  162. else
  163. tm->tm_year -= 2000;
  164. RTCCON |= 1;
  165. BCDYEAR = HEX2BCD(tm->tm_year);
  166. BCDMON = HEX2BCD(tm->tm_mon);
  167. BCDDAY = HEX2BCD(tm->tm_mday);
  168. BCDDATE = HEX2BCD(tm->tm_wday);
  169. BCDHOUR = HEX2BCD(tm->tm_hour);
  170. BCDMIN = HEX2BCD(tm->tm_min);
  171. BCDSEC = HEX2BCD(tm->tm_sec);
  172. RTCCON &= 1;
  173. return 0;
  174. }
  175. void rtc_reset (void)
  176. {
  177. RTCCON |= 1;
  178. BCDYEAR = 0;
  179. BCDMON = 0;
  180. BCDDAY = 0;
  181. BCDDATE = 0;
  182. BCDHOUR = 0;
  183. BCDMIN = 0;
  184. BCDSEC = 0;
  185. RTCCON &= 1;
  186. }
  187. /*
  188. I2C stuff
  189. */
  190. /*
  191. * Initialization, must be called once on start up, may be called
  192. * repeatedly to change the speed and slave addresses.
  193. */
  194. void i2c_init(int speed, int slaveaddr)
  195. {
  196. /*
  197. setting up I2C support
  198. */
  199. unsigned int save_F,save_PF,rIICCON,rPCONA,rPDATA,rPCONF,rPUPF;
  200. save_F = PCONF;
  201. save_PF = PUPF;
  202. rPCONF = ((save_F & ~(0xF))| 0xa);
  203. rPUPF = (save_PF | 0x3);
  204. PCONF = rPCONF; /*PF0:IICSCL, PF1:IICSDA*/
  205. PUPF = rPUPF; /* Disable pull-up */
  206. /* Configuring pin for WC pin of EEprom */
  207. rPCONA = PCONA;
  208. rPCONA &= ~(1<<9);
  209. PCONA = rPCONA;
  210. rPDATA = PDATA;
  211. rPDATA &= ~(1<<9);
  212. PDATA = rPDATA;
  213. /*
  214. Enable ACK, IICCLK=MCLK/16, enable interrupt
  215. 75MHz/16/(12+1) = 390625 Hz
  216. */
  217. rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xC);
  218. IICCON = rIICCON;
  219. IICADD = slaveaddr;
  220. }
  221. /*
  222. * Probe the given I2C chip address. Returns 0 if a chip responded,
  223. * not 0 on failure.
  224. */
  225. int i2c_probe(uchar chip)
  226. {
  227. /*
  228. not implemented
  229. */
  230. printf("i2c_probe chip %d\n", (int) chip);
  231. return -1;
  232. }
  233. /*
  234. * Read/Write interface:
  235. * chip: I2C chip address, range 0..127
  236. * addr: Memory (register) address within the chip
  237. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  238. * memories, 0 for register type devices with only one
  239. * register)
  240. * buffer: Where to read/write the data
  241. * len: How many bytes to read/write
  242. *
  243. * Returns: 0 on success, not 0 on failure
  244. */
  245. #define S3C44B0X_rIIC_INTPEND (1<<4)
  246. #define S3C44B0X_rIIC_LAST_RECEIV_BIT (1<<0)
  247. #define S3C44B0X_rIIC_INTERRUPT_ENABLE (1<<5)
  248. #define S3C44B0_IIC_TIMEOUT 100
  249. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  250. {
  251. int k, j, temp;
  252. u32 rIICSTAT;
  253. /*
  254. send the device offset
  255. */
  256. rIICSTAT = 0xD0;
  257. IICSTAT = rIICSTAT;
  258. IICDS = chip; /* this is a write operation... */
  259. rIICSTAT |= (1<<5);
  260. IICSTAT = rIICSTAT;
  261. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  262. temp = IICCON;
  263. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  264. break;
  265. udelay(2000);
  266. }
  267. if (k==S3C44B0_IIC_TIMEOUT)
  268. return -1;
  269. /* wait and check ACK */
  270. temp = IICSTAT;
  271. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  272. return -1;
  273. IICDS = addr;
  274. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  275. /* wait and check ACK */
  276. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  277. temp = IICCON;
  278. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  279. break;
  280. udelay(2000);
  281. }
  282. if (k==S3C44B0_IIC_TIMEOUT)
  283. return -1;
  284. temp = IICSTAT;
  285. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  286. return -1;
  287. /*
  288. now we can start with the read operation...
  289. */
  290. IICDS = chip | 0x01; /* this is a read operation... */
  291. rIICSTAT = 0x90; /*master recv*/
  292. rIICSTAT |= (1<<5);
  293. IICSTAT = rIICSTAT;
  294. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  295. /* wait and check ACK */
  296. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  297. temp = IICCON;
  298. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  299. break;
  300. udelay(2000);
  301. }
  302. if (k==S3C44B0_IIC_TIMEOUT)
  303. return -1;
  304. temp = IICSTAT;
  305. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  306. return -1;
  307. for (j=0; j<len-1; j++) {
  308. /*clear pending bit to resume */
  309. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  310. IICCON = temp;
  311. /* wait and check ACK */
  312. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  313. temp = IICCON;
  314. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  315. break;
  316. udelay(2000);
  317. }
  318. if (k==S3C44B0_IIC_TIMEOUT)
  319. return -1;
  320. buffer[j] = IICDS; /*save readed data*/
  321. } /*end for(j)*/
  322. /*
  323. reading the last data
  324. unset ACK generation
  325. */
  326. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND | (1<<7));
  327. IICCON = temp;
  328. /* wait but NOT check ACK */
  329. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  330. temp = IICCON;
  331. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  332. break;
  333. udelay(2000);
  334. }
  335. if (k==S3C44B0_IIC_TIMEOUT)
  336. return -1;
  337. buffer[j] = IICDS; /*save readed data*/
  338. rIICSTAT = 0x90; /*master recv*/
  339. /* Write operation Terminate sending STOP */
  340. IICSTAT = rIICSTAT;
  341. /*Clear Int Pending Bit to RESUME*/
  342. temp = IICCON;
  343. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  344. IICCON = IICCON | (1<<7); /*restore ACK generation*/
  345. return 0;
  346. }
  347. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  348. {
  349. int j, k;
  350. u32 rIICSTAT, temp;
  351. /*
  352. send the device offset
  353. */
  354. rIICSTAT = 0xD0;
  355. IICSTAT = rIICSTAT;
  356. IICDS = chip; /* this is a write operation... */
  357. rIICSTAT |= (1<<5);
  358. IICSTAT = rIICSTAT;
  359. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  360. /* wait and check ACK */
  361. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  362. temp = IICCON;
  363. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  364. break;
  365. udelay(2000);
  366. }
  367. if (k==S3C44B0_IIC_TIMEOUT)
  368. return -1;
  369. temp = IICSTAT;
  370. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  371. return -1;
  372. IICDS = addr;
  373. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  374. /* wait and check ACK */
  375. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  376. temp = IICCON;
  377. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  378. break;
  379. udelay(2000);
  380. }
  381. if (k==S3C44B0_IIC_TIMEOUT)
  382. return -1;
  383. temp = IICSTAT;
  384. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  385. return -1;
  386. /*
  387. now we can start with the read write operation
  388. */
  389. for (j=0; j<len; j++) {
  390. IICDS = buffer[j]; /*prerare data to write*/
  391. /*clear pending bit to resume*/
  392. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  393. IICCON = temp;
  394. /* wait but NOT check ACK */
  395. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  396. temp = IICCON;
  397. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  398. break;
  399. udelay(2000);
  400. }
  401. if (k==S3C44B0_IIC_TIMEOUT)
  402. return -1;
  403. } /* end for(j) */
  404. /* sending stop to terminate */
  405. rIICSTAT = 0xD0; /*master send*/
  406. IICSTAT = rIICSTAT;
  407. /*Clear Int Pending Bit to RESUME*/
  408. temp = IICCON;
  409. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  410. return 0;
  411. }