PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/firmware/target/sh/archos/mascodec-archos.c

https://github.com/jdgordon/rockbox
C | 485 lines | 375 code | 73 blank | 37 comment | 43 complexity | 627a8ca7e26d0405e48d24d1b2c8a2e1 MD5 | raw file
  1. /***************************************************************************
  2. * __________ __ ___.
  3. * Open \______ \ ____ ____ | | _\_ |__ _______ ___
  4. * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
  5. * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
  6. * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
  7. * \/ \/ \/ \/ \/
  8. * $Id$
  9. *
  10. * Copyright (C) 2002 by Linus Nielsen Feltzing
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  18. * KIND, either express or implied.
  19. *
  20. ****************************************************************************/
  21. #include "stdbool.h"
  22. #include "config.h"
  23. #include "sh7034.h"
  24. #include "i2c.h"
  25. #include "debug.h"
  26. #include "mas35xx.h"
  27. #include "kernel.h"
  28. #include "system.h"
  29. #include "hwcompat.h"
  30. static int mas_devread(unsigned long *dest, int len);
  31. int mas_default_read(unsigned short *buf)
  32. {
  33. unsigned char *dest = (unsigned char *)buf;
  34. int ret = 0;
  35. i2c_begin();
  36. i2c_start();
  37. i2c_outb(MAS_DEV_WRITE);
  38. if (i2c_getack()) {
  39. i2c_outb(MAS_DATA_READ);
  40. if (i2c_getack()) {
  41. i2c_start();
  42. i2c_outb(MAS_DEV_READ);
  43. if (i2c_getack()) {
  44. dest[0] = i2c_inb(0);
  45. dest[1] = i2c_inb(1);
  46. }
  47. else
  48. ret = -3;
  49. }
  50. else
  51. ret = -2;
  52. }
  53. else
  54. ret = -1;
  55. i2c_stop();
  56. i2c_end();
  57. return ret;
  58. }
  59. int mas_run(unsigned short address)
  60. {
  61. int ret = 0;
  62. unsigned char buf[3];
  63. i2c_begin();
  64. buf[0] = MAS_DATA_WRITE;
  65. buf[1] = address >> 8;
  66. buf[2] = address & 0xff;
  67. /* send run command */
  68. if (i2c_write(MAS_DEV_WRITE,buf,3))
  69. {
  70. ret = -1;
  71. }
  72. i2c_end();
  73. return ret;
  74. }
  75. /* note: 'len' is number of 32-bit words, not number of bytes! */
  76. int mas_readmem(int bank, int addr, unsigned long* dest, int len)
  77. {
  78. int ret = 0;
  79. unsigned char buf[7];
  80. i2c_begin();
  81. buf[0] = MAS_DATA_WRITE;
  82. buf[1] = bank?MAS_CMD_READ_D1_MEM:MAS_CMD_READ_D0_MEM;
  83. buf[2] = 0x00;
  84. buf[3] = (len & 0xff00) >> 8;
  85. buf[4] = len & 0xff;
  86. buf[5] = (addr & 0xff00) >> 8;
  87. buf[6] = addr & 0xff;
  88. /* send read command */
  89. if (i2c_write(MAS_DEV_WRITE,buf,7))
  90. {
  91. ret = -1;
  92. }
  93. ret = mas_devread(dest, len);
  94. i2c_end();
  95. return ret;
  96. }
  97. /* note: 'len' is number of 32-bit words, not number of bytes! */
  98. int mas_writemem(int bank, int addr, const unsigned long* src, int len)
  99. {
  100. int ret = 0;
  101. int i, j;
  102. unsigned char buf[60];
  103. const unsigned char* ptr = (const unsigned char*)src;
  104. i2c_begin();
  105. i=0;
  106. buf[i++] = MAS_DATA_WRITE;
  107. buf[i++] = bank?MAS_CMD_WRITE_D1_MEM:MAS_CMD_WRITE_D0_MEM;
  108. buf[i++] = 0x00;
  109. buf[i++] = (len & 0xff00) >> 8;
  110. buf[i++] = len & 0xff;
  111. buf[i++] = (addr & 0xff00) >> 8;
  112. buf[i++] = addr & 0xff;
  113. j = 0;
  114. while(len--) {
  115. #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
  116. buf[i++] = 0;
  117. buf[i++] = ptr[j+1];
  118. buf[i++] = ptr[j+2];
  119. buf[i++] = ptr[j+3];
  120. #else
  121. buf[i++] = ptr[j+2];
  122. buf[i++] = ptr[j+3];
  123. buf[i++] = 0;
  124. buf[i++] = ptr[j+1];
  125. #endif
  126. j += 4;
  127. }
  128. /* send write command */
  129. if (i2c_write(MAS_DEV_WRITE,buf,i))
  130. {
  131. ret = -1;
  132. }
  133. i2c_end();
  134. return ret;
  135. }
  136. int mas_readreg(int reg)
  137. {
  138. int ret = 0;
  139. unsigned char buf[16];
  140. unsigned long value;
  141. i2c_begin();
  142. buf[0] = MAS_DATA_WRITE;
  143. buf[1] = MAS_CMD_READ_REG | (reg >> 4);
  144. buf[2] = (reg & 0x0f) << 4;
  145. /* send read command */
  146. if (i2c_write(MAS_DEV_WRITE,buf,3))
  147. {
  148. ret = -1;
  149. }
  150. else
  151. {
  152. if(mas_devread(&value, 1))
  153. {
  154. ret = -2;
  155. }
  156. else
  157. {
  158. ret = value;
  159. }
  160. }
  161. i2c_end();
  162. return ret;
  163. }
  164. int mas_writereg(int reg, unsigned int val)
  165. {
  166. int ret = 0;
  167. unsigned char buf[5];
  168. i2c_begin();
  169. buf[0] = MAS_DATA_WRITE;
  170. buf[1] = MAS_CMD_WRITE_REG | (reg >> 4);
  171. #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
  172. buf[2] = ((reg & 0x0f) << 4) | (val >> 16 & 0x0f);
  173. buf[3] = (val >> 8) & 0xff;
  174. buf[4] = val & 0xff;
  175. #else
  176. buf[2] = ((reg & 0x0f) << 4) | (val & 0x0f);
  177. buf[3] = (val >> 12) & 0xff;
  178. buf[4] = (val >> 4) & 0xff;
  179. #endif
  180. /* send write command */
  181. if (i2c_write(MAS_DEV_WRITE,buf,5))
  182. {
  183. ret = -1;
  184. }
  185. i2c_end();
  186. return ret;
  187. }
  188. /* note: 'len' is number of 32-bit words, not number of bytes! */
  189. static int mas_devread(unsigned long *dest, int len)
  190. {
  191. int ret = 0;
  192. unsigned char* ptr = (unsigned char*)dest;
  193. int i;
  194. /* handle read-back */
  195. /* Remember, the MAS values are only 20 bits, so we set
  196. the upper 12 bits to 0 */
  197. i2c_start();
  198. i2c_outb(MAS_DEV_WRITE);
  199. if (i2c_getack()) {
  200. i2c_outb(MAS_DATA_READ);
  201. if (i2c_getack()) {
  202. i2c_start();
  203. i2c_outb(MAS_DEV_READ);
  204. if (i2c_getack()) {
  205. for (i=0;len;i++) {
  206. len--;
  207. #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
  208. i2c_inb(0); /* Dummy read */
  209. ptr[i*4+0] = 0;
  210. ptr[i*4+1] = i2c_inb(0) & 0x0f;
  211. ptr[i*4+2] = i2c_inb(0);
  212. if(len)
  213. ptr[i*4+3] = i2c_inb(0);
  214. else
  215. ptr[i*4+3] = i2c_inb(1); /* NAK the last byte */
  216. #else
  217. ptr[i*4+2] = i2c_inb(0);
  218. ptr[i*4+3] = i2c_inb(0);
  219. ptr[i*4+0] = i2c_inb(0);
  220. if(len)
  221. ptr[i*4+1] = i2c_inb(0);
  222. else
  223. ptr[i*4+1] = i2c_inb(1); /* NAK the last byte */
  224. #endif
  225. }
  226. }
  227. else
  228. ret = -3;
  229. }
  230. else
  231. ret = -2;
  232. }
  233. else
  234. ret = -1;
  235. i2c_stop();
  236. return ret;
  237. }
  238. void mas_reset(void)
  239. {
  240. or_b(0x01, &PAIORH);
  241. #if CONFIG_CODEC == MAS3507D
  242. /* PB5 is "MAS enable". make it GPIO output and high */
  243. PBCR2 &= ~0x0c00;
  244. or_b(0x20, &PBIORL);
  245. or_b(0x20, &PBDRL);
  246. and_b(~0x01, &PADRH);
  247. sleep(HZ/100);
  248. or_b(0x01, &PADRH);
  249. sleep(HZ/5);
  250. #elif (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
  251. if (HW_MASK & ATA_ADDRESS_200)
  252. {
  253. and_b(~0x01, &PADRH);
  254. sleep(HZ/100);
  255. or_b(0x01, &PADRH);
  256. sleep(HZ/5);
  257. }
  258. else
  259. {
  260. /* Older recorder models don't invert the POR signal */
  261. or_b(0x01, &PADRH);
  262. sleep(HZ/100);
  263. and_b(~0x01, &PADRH);
  264. sleep(HZ/5);
  265. }
  266. #endif
  267. }
  268. #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
  269. int mas_direct_config_read(unsigned char reg)
  270. {
  271. int ret = 0;
  272. unsigned char tmp[2];
  273. i2c_begin();
  274. i2c_start();
  275. i2c_outb(MAS_DEV_WRITE);
  276. if (i2c_getack()) {
  277. i2c_outb(reg);
  278. if (i2c_getack()) {
  279. i2c_start();
  280. i2c_outb(MAS_DEV_READ);
  281. if (i2c_getack()) {
  282. tmp[0] = i2c_inb(0);
  283. tmp[1] = i2c_inb(1); /* NAK the last byte */
  284. ret = (tmp[0] << 8) | tmp[1];
  285. }
  286. else
  287. ret = -3;
  288. }
  289. else
  290. ret = -2;
  291. }
  292. else
  293. ret = -1;
  294. i2c_stop();
  295. i2c_end();
  296. return ret;
  297. }
  298. int mas_direct_config_write(unsigned char reg, unsigned int val)
  299. {
  300. int ret = 0;
  301. unsigned char buf[3];
  302. i2c_begin();
  303. buf[0] = reg;
  304. buf[1] = (val >> 8) & 0xff;
  305. buf[2] = val & 0xff;
  306. /* send write command */
  307. if (i2c_write(MAS_DEV_WRITE,buf,3))
  308. {
  309. ret = -1;
  310. }
  311. i2c_end();
  312. return ret;
  313. }
  314. int mas_codec_writereg(int reg, unsigned int val)
  315. {
  316. int ret = 0;
  317. unsigned char buf[5];
  318. i2c_begin();
  319. buf[0] = MAS_CODEC_WRITE;
  320. buf[1] = (reg >> 8) & 0xff;
  321. buf[2] = reg & 0xff;
  322. buf[3] = (val >> 8) & 0xff;
  323. buf[4] = val & 0xff;
  324. /* send write command */
  325. if (i2c_write(MAS_DEV_WRITE,buf,5))
  326. {
  327. ret = -1;
  328. }
  329. i2c_end();
  330. return ret;
  331. }
  332. int mas_codec_readreg(int reg)
  333. {
  334. int ret = 0;
  335. unsigned char buf[16];
  336. unsigned char tmp[2];
  337. i2c_begin();
  338. buf[0] = MAS_CODEC_WRITE;
  339. buf[1] = (reg >> 8) & 0xff;
  340. buf[2] = reg & 0xff;
  341. /* send read command */
  342. if (i2c_write(MAS_DEV_WRITE,buf,3))
  343. {
  344. ret = -1;
  345. }
  346. else
  347. {
  348. i2c_start();
  349. i2c_outb(MAS_DEV_WRITE);
  350. if (i2c_getack()) {
  351. i2c_outb(MAS_CODEC_READ);
  352. if (i2c_getack()) {
  353. i2c_start();
  354. i2c_outb(MAS_DEV_READ);
  355. if (i2c_getack()) {
  356. tmp[0] = i2c_inb(0);
  357. tmp[1] = i2c_inb(1); /* NAK the last byte */
  358. ret = (tmp[0] << 8) | tmp[1];
  359. }
  360. else
  361. ret = -4;
  362. }
  363. else
  364. ret = -3;
  365. }
  366. else
  367. ret = -2;
  368. i2c_stop();
  369. }
  370. i2c_end();
  371. return ret;
  372. }
  373. unsigned long mas_readver(void)
  374. {
  375. int ret = 0;
  376. unsigned char buf[16];
  377. unsigned long value;
  378. i2c_begin();
  379. buf[0] = MAS_DATA_WRITE;
  380. buf[1] = MAS_CMD_READ_IC_VER;
  381. buf[2] = 0;
  382. /* send read command */
  383. if (i2c_write(MAS_DEV_WRITE,buf,3))
  384. {
  385. ret = -1;
  386. }
  387. else
  388. {
  389. if(mas_devread(&value, 1))
  390. {
  391. ret = -2;
  392. }
  393. else
  394. {
  395. ret = value;
  396. }
  397. }
  398. i2c_end();
  399. return ret;
  400. }
  401. #endif
  402. #if CONFIG_TUNER & S1A0903X01
  403. static int pllfreq;
  404. void mas_store_pllfreq(int freq)
  405. {
  406. pllfreq = freq;
  407. }
  408. int mas_get_pllfreq(void)
  409. {
  410. return pllfreq;
  411. }
  412. #endif