cpu.c 9.6 KB

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