s3c24x0_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * (C) Copyright 2002
  3. * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
  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. /* This code should work for both the S3C2400 and the S3C2410
  24. * as they seem to have the same I2C controller inside.
  25. * The different address mapping is handled by the s3c24xx.h files below.
  26. */
  27. #include <common.h>
  28. #ifdef CONFIG_DRIVER_S3C24X0_I2C
  29. #if defined(CONFIG_S3C2400)
  30. #include <s3c2400.h>
  31. #elif defined(CONFIG_S3C2410)
  32. #include <s3c2410.h>
  33. #endif
  34. #include <i2c.h>
  35. #ifdef CONFIG_HARD_I2C
  36. #define I2C_WRITE 0
  37. #define I2C_READ 1
  38. #define I2C_OK 0
  39. #define I2C_NOK 1
  40. #define I2C_NACK 2
  41. #define I2C_NOK_LA 3 /* Lost arbitration */
  42. #define I2C_NOK_TOUT 4 /* time out */
  43. #define I2CSTAT_BSY 0x20 /* Busy bit */
  44. #define I2CSTAT_NACK 0x01 /* Nack bit */
  45. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  46. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  47. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  48. #define I2C_START_STOP 0x20 /* START / STOP */
  49. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  50. #define I2C_TIMEOUT 1 /* 1 seconde */
  51. static int GetI2CSDA(void)
  52. {
  53. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  54. return (gpio->GPEDAT & 0x8000) >> 15;
  55. }
  56. #if 0
  57. static void SetI2CSDA(int x)
  58. {
  59. rGPEDAT = (rGPEDAT & ~0x8000) | (x&1) << 15;
  60. }
  61. #endif
  62. static void SetI2CSCL(int x)
  63. {
  64. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  65. gpio->GPEDAT = (gpio->GPEDAT & ~0x4000) | (x&1) << 14;
  66. }
  67. static int WaitForXfer(void)
  68. {
  69. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  70. int i, status;
  71. i = I2C_TIMEOUT * 1000;
  72. status = i2c->IICCON;
  73. while ((i > 0) && !(status & I2CCON_IRPND)) {
  74. udelay(1000);
  75. status = i2c->IICCON;
  76. i--;
  77. }
  78. return(status & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
  79. }
  80. static int IsACK(void)
  81. {
  82. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  83. return(!(i2c->IICSTAT & I2CSTAT_NACK));
  84. }
  85. static void ReadWriteByte(void)
  86. {
  87. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  88. i2c->IICCON &= ~I2CCON_IRPND;
  89. }
  90. void i2c_init (int speed, int slaveadd)
  91. {
  92. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  93. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  94. ulong freq, pres = 16, div;
  95. int i, status;
  96. /* wait for some time to give previous transfer a chance to finish */
  97. i = I2C_TIMEOUT * 1000;
  98. status = i2c->IICSTAT;
  99. while ((i > 0) && (status & I2CSTAT_BSY)) {
  100. udelay(1000);
  101. status = i2c->IICSTAT;
  102. i--;
  103. }
  104. if ((status & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  105. ulong old_gpecon = gpio->GPECON;
  106. /* bus still busy probably by (most) previously interrupted transfer */
  107. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  108. gpio->GPECON = (gpio->GPECON & ~0xF0000000) | 0x10000000;
  109. /* toggle I2CSCL until bus idle */
  110. SetI2CSCL(0); udelay(1000);
  111. i = 10;
  112. while ((i > 0) && (GetI2CSDA() != 1)) {
  113. SetI2CSCL(1); udelay(1000);
  114. SetI2CSCL(0); udelay(1000);
  115. i--;
  116. }
  117. SetI2CSCL(1); udelay(1000);
  118. /* restore pin functions */
  119. gpio->GPECON = old_gpecon;
  120. }
  121. /* calculate prescaler and divisor values */
  122. freq = get_PCLK();
  123. if ((freq / pres / (16+1)) > speed)
  124. /* set prescaler to 512 */
  125. pres = 512;
  126. div = 0;
  127. while ((freq / pres / (div+1)) > speed)
  128. div++;
  129. /* set prescaler, divisor according to freq, also set
  130. ACKGEN, IRQ */
  131. i2c->IICCON = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
  132. /* init to SLAVE REVEIVE and set slaveaddr */
  133. i2c->IICSTAT = 0;
  134. i2c->IICADD = slaveadd;
  135. /* program Master Transmit (and implicit STOP) */
  136. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  137. }
  138. /*
  139. cmd_type is 0 for write 1 for read.
  140. addr_len can take any value from 0-255, it is only limited
  141. by the char, we could make it larger if needed. If it is
  142. 0 we skip the address write cycle.
  143. */
  144. static
  145. int i2c_transfer(unsigned char cmd_type,
  146. unsigned char chip,
  147. unsigned char addr[],
  148. unsigned char addr_len,
  149. unsigned char data[],
  150. unsigned short data_len)
  151. {
  152. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  153. int i, status, result;
  154. if (data == 0 || data_len == 0) {
  155. /*Don't support data transfer of no length or to address 0*/
  156. printf( "i2c_transfer: bad call\n" );
  157. return I2C_NOK;
  158. }
  159. //CheckDelay();
  160. /* Check I2C bus idle */
  161. i = I2C_TIMEOUT * 1000;
  162. status = i2c->IICSTAT;
  163. while ((i > 0) && (status & I2CSTAT_BSY)) {
  164. udelay(1000);
  165. status = i2c->IICSTAT;
  166. i--;
  167. }
  168. if (status & I2CSTAT_BSY) {
  169. result = I2C_NOK_TOUT;
  170. return(result);
  171. }
  172. i2c->IICCON |= 0x80;
  173. result = I2C_OK;
  174. switch (cmd_type) {
  175. case I2C_WRITE:
  176. if (addr && addr_len) {
  177. i2c->IICDS = chip;
  178. /* send START */
  179. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  180. i = 0;
  181. while ((i < addr_len) && (result == I2C_OK)) {
  182. result = WaitForXfer();
  183. i2c->IICDS = addr[i];
  184. ReadWriteByte();
  185. i++;
  186. }
  187. i = 0;
  188. while ((i < data_len) && (result == I2C_OK)) {
  189. result = WaitForXfer();
  190. i2c->IICDS = data[i];
  191. ReadWriteByte();
  192. i++;
  193. }
  194. } else {
  195. i2c->IICDS = chip;
  196. /* send START */
  197. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  198. i = 0;
  199. while ((i < data_len) && (result = I2C_OK)) {
  200. result = WaitForXfer();
  201. i2c->IICDS = data[i];
  202. ReadWriteByte();
  203. i++;
  204. }
  205. }
  206. if (result == I2C_OK)
  207. result = WaitForXfer();
  208. /* send STOP */
  209. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  210. ReadWriteByte();
  211. break;
  212. case I2C_READ:
  213. if (addr && addr_len) {
  214. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  215. i2c->IICDS = chip;
  216. /* send START */
  217. i2c->IICSTAT |= I2C_START_STOP;
  218. result = WaitForXfer();
  219. if (IsACK()) {
  220. i = 0;
  221. while ((i < addr_len) && (result == I2C_OK)) {
  222. i2c->IICDS = addr[i];
  223. ReadWriteByte();
  224. result = WaitForXfer();
  225. i++;
  226. }
  227. i2c->IICDS = chip;
  228. /* resend START */
  229. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP;
  230. ReadWriteByte();
  231. result = WaitForXfer();
  232. i = 0;
  233. while ((i < data_len) && (result == I2C_OK)) {
  234. /* disable ACK for final READ */
  235. if (i == data_len - 1)
  236. i2c->IICCON &= ~0x80;
  237. ReadWriteByte();
  238. result = WaitForXfer();
  239. data[i] = i2c->IICDS;
  240. i++;
  241. }
  242. } else {
  243. result = I2C_NACK;
  244. }
  245. } else {
  246. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  247. i2c->IICDS = chip;
  248. /* send START */
  249. i2c->IICSTAT |= I2C_START_STOP;
  250. result = WaitForXfer();
  251. if (IsACK()) {
  252. i = 0;
  253. while ((i < data_len) && (result == I2C_OK)) {
  254. /* disable ACK for final READ */
  255. if (i == data_len - 1)
  256. i2c->IICCON &= ~0x80;
  257. ReadWriteByte();
  258. result = WaitForXfer();
  259. data[i] = i2c->IICDS;
  260. i++;
  261. }
  262. } else {
  263. result = I2C_NACK;
  264. }
  265. }
  266. /* send STOP */
  267. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  268. ReadWriteByte();
  269. break;
  270. default:
  271. printf( "i2c_transfer: bad call\n" );
  272. result = I2C_NOK;
  273. break;
  274. }
  275. return (result);
  276. }
  277. int i2c_probe (uchar chip)
  278. {
  279. uchar buf[1];
  280. buf[0] = 0;
  281. /*
  282. * What is needed is to send the chip address and verify that the
  283. * address was <ACK>ed (i.e. there was a chip at that address which
  284. * drove the data line low).
  285. */
  286. return(i2c_transfer (I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK);
  287. }
  288. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  289. {
  290. uchar xaddr[4];
  291. int ret;
  292. if ( alen > 4 ) {
  293. printf ("I2C read: addr len %d not supported\n", alen);
  294. return 1;
  295. }
  296. if ( alen > 0 ) {
  297. xaddr[0] = (addr >> 24) & 0xFF;
  298. xaddr[1] = (addr >> 16) & 0xFF;
  299. xaddr[2] = (addr >> 8) & 0xFF;
  300. xaddr[3] = addr & 0xFF;
  301. }
  302. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  303. /*
  304. * EEPROM chips that implement "address overflow" are ones
  305. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  306. * address and the extra bits end up in the "chip address"
  307. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  308. * four 256 byte chips.
  309. *
  310. * Note that we consider the length of the address field to
  311. * still be one byte because the extra address bits are
  312. * hidden in the chip address.
  313. */
  314. if( alen > 0 )
  315. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  316. #endif
  317. if( (ret = i2c_transfer(I2C_READ, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
  318. printf( "I2c read: failed %d\n", ret);
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  324. {
  325. uchar xaddr[4];
  326. if ( alen > 4 ) {
  327. printf ("I2C write: addr len %d not supported\n", alen);
  328. return 1;
  329. }
  330. if ( alen > 0 ) {
  331. xaddr[0] = (addr >> 24) & 0xFF;
  332. xaddr[1] = (addr >> 16) & 0xFF;
  333. xaddr[2] = (addr >> 8) & 0xFF;
  334. xaddr[3] = addr & 0xFF;
  335. }
  336. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  337. /*
  338. * EEPROM chips that implement "address overflow" are ones
  339. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  340. * address and the extra bits end up in the "chip address"
  341. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  342. * four 256 byte chips.
  343. *
  344. * Note that we consider the length of the address field to
  345. * still be one byte because the extra address bits are
  346. * hidden in the chip address.
  347. */
  348. if( alen > 0 )
  349. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  350. #endif
  351. return (i2c_transfer(I2C_WRITE, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
  352. }
  353. #endif /* CONFIG_HARD_I2C */
  354. #endif /* CONFIG_DRIVER_S3C24X0_I2C */