cpu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. void 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. }
  157. void rtc_set (struct rtc_time* tm)
  158. {
  159. if(tm->tm_year < 2000)
  160. tm->tm_year -= 1900;
  161. else
  162. tm->tm_year -= 2000;
  163. RTCCON |= 1;
  164. BCDYEAR = HEX2BCD(tm->tm_year);
  165. BCDMON = HEX2BCD(tm->tm_mon);
  166. BCDDAY = HEX2BCD(tm->tm_mday);
  167. BCDDATE = HEX2BCD(tm->tm_wday);
  168. BCDHOUR = HEX2BCD(tm->tm_hour);
  169. BCDMIN = HEX2BCD(tm->tm_min);
  170. BCDSEC = HEX2BCD(tm->tm_sec);
  171. RTCCON &= 1;
  172. }
  173. void rtc_reset (void)
  174. {
  175. RTCCON |= 1;
  176. BCDYEAR = 0;
  177. BCDMON = 0;
  178. BCDDAY = 0;
  179. BCDDATE = 0;
  180. BCDHOUR = 0;
  181. BCDMIN = 0;
  182. BCDSEC = 0;
  183. RTCCON &= 1;
  184. }
  185. /*
  186. I2C stuff
  187. */
  188. /*
  189. * Initialization, must be called once on start up, may be called
  190. * repeatedly to change the speed and slave addresses.
  191. */
  192. void i2c_init(int speed, int slaveaddr)
  193. {
  194. /*
  195. setting up I2C support
  196. */
  197. unsigned int save_F,save_PF,rIICCON,rPCONA,rPDATA,rPCONF,rPUPF;
  198. save_F = PCONF;
  199. save_PF = PUPF;
  200. rPCONF = ((save_F & ~(0xF))| 0xa);
  201. rPUPF = (save_PF | 0x3);
  202. PCONF = rPCONF; /*PF0:IICSCL, PF1:IICSDA*/
  203. PUPF = rPUPF; /* Disable pull-up */
  204. /* Configuring pin for WC pin of EEprom */
  205. rPCONA = PCONA;
  206. rPCONA &= ~(1<<9);
  207. PCONA = rPCONA;
  208. rPDATA = PDATA;
  209. rPDATA &= ~(1<<9);
  210. PDATA = rPDATA;
  211. /*
  212. Enable ACK, IICCLK=MCLK/16, enable interrupt
  213. 75Mhz/16/(12+1) = 390625 Hz
  214. */
  215. rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xC);
  216. IICCON = rIICCON;
  217. IICADD = slaveaddr;
  218. }
  219. /*
  220. * Probe the given I2C chip address. Returns 0 if a chip responded,
  221. * not 0 on failure.
  222. */
  223. int i2c_probe(uchar chip)
  224. {
  225. /*
  226. not implemented
  227. */
  228. printf("i2c_probe chip %d\n", (int) chip);
  229. return -1;
  230. }
  231. /*
  232. * Read/Write interface:
  233. * chip: I2C chip address, range 0..127
  234. * addr: Memory (register) address within the chip
  235. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  236. * memories, 0 for register type devices with only one
  237. * register)
  238. * buffer: Where to read/write the data
  239. * len: How many bytes to read/write
  240. *
  241. * Returns: 0 on success, not 0 on failure
  242. */
  243. #define S3C44B0X_rIIC_INTPEND (1<<4)
  244. #define S3C44B0X_rIIC_LAST_RECEIV_BIT (1<<0)
  245. #define S3C44B0X_rIIC_INTERRUPT_ENABLE (1<<5)
  246. #define S3C44B0_IIC_TIMEOUT 100
  247. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  248. {
  249. int k, j, temp;
  250. u32 rIICSTAT;
  251. /*
  252. send the device offset
  253. */
  254. rIICSTAT = 0xD0;
  255. IICSTAT = rIICSTAT;
  256. IICDS = chip; /* this is a write operation... */
  257. rIICSTAT |= (1<<5);
  258. IICSTAT = rIICSTAT;
  259. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  260. temp = IICCON;
  261. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  262. break;
  263. udelay(2000);
  264. }
  265. if (k==S3C44B0_IIC_TIMEOUT)
  266. return -1;
  267. /* wait and check ACK */
  268. temp = IICSTAT;
  269. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  270. return -1;
  271. IICDS = addr;
  272. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  273. /* wait and check ACK */
  274. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  275. temp = IICCON;
  276. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  277. break;
  278. udelay(2000);
  279. }
  280. if (k==S3C44B0_IIC_TIMEOUT)
  281. return -1;
  282. temp = IICSTAT;
  283. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  284. return -1;
  285. /*
  286. now we can start with the read operation...
  287. */
  288. IICDS = chip | 0x01; /* this is a read operation... */
  289. rIICSTAT = 0x90; /*master recv*/
  290. rIICSTAT |= (1<<5);
  291. IICSTAT = rIICSTAT;
  292. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  293. /* wait and check ACK */
  294. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  295. temp = IICCON;
  296. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  297. break;
  298. udelay(2000);
  299. }
  300. if (k==S3C44B0_IIC_TIMEOUT)
  301. return -1;
  302. temp = IICSTAT;
  303. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  304. return -1;
  305. for (j=0; j<len-1; j++) {
  306. /*clear pending bit to resume */
  307. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  308. IICCON = temp;
  309. /* wait and check ACK */
  310. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  311. temp = IICCON;
  312. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  313. break;
  314. udelay(2000);
  315. }
  316. if (k==S3C44B0_IIC_TIMEOUT)
  317. return -1;
  318. buffer[j] = IICDS; /*save readed data*/
  319. } /*end for(j)*/
  320. /*
  321. reading the last data
  322. unset ACK generation
  323. */
  324. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND | (1<<7));
  325. IICCON = temp;
  326. /* wait but NOT check ACK */
  327. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  328. temp = IICCON;
  329. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  330. break;
  331. udelay(2000);
  332. }
  333. if (k==S3C44B0_IIC_TIMEOUT)
  334. return -1;
  335. buffer[j] = IICDS; /*save readed data*/
  336. rIICSTAT = 0x90; /*master recv*/
  337. /* Write operation Terminate sending STOP */
  338. IICSTAT = rIICSTAT;
  339. /*Clear Int Pending Bit to RESUME*/
  340. temp = IICCON;
  341. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  342. IICCON = IICCON | (1<<7); /*restore ACK generation*/
  343. return 0;
  344. }
  345. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  346. {
  347. int j, k;
  348. u32 rIICSTAT, temp;
  349. /*
  350. send the device offset
  351. */
  352. rIICSTAT = 0xD0;
  353. IICSTAT = rIICSTAT;
  354. IICDS = chip; /* this is a write operation... */
  355. rIICSTAT |= (1<<5);
  356. IICSTAT = rIICSTAT;
  357. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  358. /* wait and check ACK */
  359. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  360. temp = IICCON;
  361. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  362. break;
  363. udelay(2000);
  364. }
  365. if (k==S3C44B0_IIC_TIMEOUT)
  366. return -1;
  367. temp = IICSTAT;
  368. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  369. return -1;
  370. IICDS = addr;
  371. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  372. /* wait and check ACK */
  373. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  374. temp = IICCON;
  375. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  376. break;
  377. udelay(2000);
  378. }
  379. if (k==S3C44B0_IIC_TIMEOUT)
  380. return -1;
  381. temp = IICSTAT;
  382. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  383. return -1;
  384. /*
  385. now we can start with the read write operation
  386. */
  387. for (j=0; j<len; j++) {
  388. IICDS = buffer[j]; /*prerare data to write*/
  389. /*clear pending bit to resume*/
  390. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  391. IICCON = temp;
  392. /* wait but NOT check ACK */
  393. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  394. temp = IICCON;
  395. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  396. break;
  397. udelay(2000);
  398. }
  399. if (k==S3C44B0_IIC_TIMEOUT)
  400. return -1;
  401. } /* end for(j) */
  402. /* sending stop to terminate */
  403. rIICSTAT = 0xD0; /*master send*/
  404. IICSTAT = rIICSTAT;
  405. /*Clear Int Pending Bit to RESUME*/
  406. temp = IICCON;
  407. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  408. return 0;
  409. }