i2c-au1550.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
  3. * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
  4. *
  5. * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * The documentation describes this as an SMBus controller, but it doesn't
  8. * understand any of the SMBus protocol in hardware. It's really an I2C
  9. * controller that could emulate most of the SMBus in software.
  10. *
  11. * This is just a skeleton adapter to use with the Au1550 PSC
  12. * algorithm. It was developed for the Pb1550, but will work with
  13. * any Au1550 board that has a similar PSC configuration.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version 2
  18. * of the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  28. */
  29. #include <linux/config.h>
  30. #include <linux/delay.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/errno.h>
  35. #include <linux/i2c.h>
  36. #include <asm/mach-au1x00/au1000.h>
  37. #include <asm/mach-pb1x00/pb1550.h>
  38. #include <asm/mach-au1x00/au1xxx_psc.h>
  39. #include "i2c-au1550.h"
  40. static int
  41. wait_xfer_done(struct i2c_au1550_data *adap)
  42. {
  43. u32 stat;
  44. int i;
  45. volatile psc_smb_t *sp;
  46. sp = (volatile psc_smb_t *)(adap->psc_base);
  47. /* Wait for Tx FIFO Underflow.
  48. */
  49. for (i = 0; i < adap->xfer_timeout; i++) {
  50. stat = sp->psc_smbevnt;
  51. au_sync();
  52. if ((stat & PSC_SMBEVNT_TU) != 0) {
  53. /* Clear it. */
  54. sp->psc_smbevnt = PSC_SMBEVNT_TU;
  55. au_sync();
  56. return 0;
  57. }
  58. udelay(1);
  59. }
  60. return -ETIMEDOUT;
  61. }
  62. static int
  63. wait_ack(struct i2c_au1550_data *adap)
  64. {
  65. u32 stat;
  66. volatile psc_smb_t *sp;
  67. if (wait_xfer_done(adap))
  68. return -ETIMEDOUT;
  69. sp = (volatile psc_smb_t *)(adap->psc_base);
  70. stat = sp->psc_smbevnt;
  71. au_sync();
  72. if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
  73. return -ETIMEDOUT;
  74. return 0;
  75. }
  76. static int
  77. wait_master_done(struct i2c_au1550_data *adap)
  78. {
  79. u32 stat;
  80. int i;
  81. volatile psc_smb_t *sp;
  82. sp = (volatile psc_smb_t *)(adap->psc_base);
  83. /* Wait for Master Done.
  84. */
  85. for (i = 0; i < adap->xfer_timeout; i++) {
  86. stat = sp->psc_smbevnt;
  87. au_sync();
  88. if ((stat & PSC_SMBEVNT_MD) != 0)
  89. return 0;
  90. udelay(1);
  91. }
  92. return -ETIMEDOUT;
  93. }
  94. static int
  95. do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd)
  96. {
  97. volatile psc_smb_t *sp;
  98. u32 stat;
  99. sp = (volatile psc_smb_t *)(adap->psc_base);
  100. /* Reset the FIFOs, clear events.
  101. */
  102. sp->psc_smbpcr = PSC_SMBPCR_DC;
  103. sp->psc_smbevnt = PSC_SMBEVNT_ALLCLR;
  104. au_sync();
  105. do {
  106. stat = sp->psc_smbpcr;
  107. au_sync();
  108. } while ((stat & PSC_SMBPCR_DC) != 0);
  109. /* Write out the i2c chip address and specify operation
  110. */
  111. addr <<= 1;
  112. if (rd)
  113. addr |= 1;
  114. /* Put byte into fifo, start up master.
  115. */
  116. sp->psc_smbtxrx = addr;
  117. au_sync();
  118. sp->psc_smbpcr = PSC_SMBPCR_MS;
  119. au_sync();
  120. if (wait_ack(adap))
  121. return -EIO;
  122. return 0;
  123. }
  124. static u32
  125. wait_for_rx_byte(struct i2c_au1550_data *adap, u32 *ret_data)
  126. {
  127. int j;
  128. u32 data, stat;
  129. volatile psc_smb_t *sp;
  130. if (wait_xfer_done(adap))
  131. return -EIO;
  132. sp = (volatile psc_smb_t *)(adap->psc_base);
  133. j = adap->xfer_timeout * 100;
  134. do {
  135. j--;
  136. if (j <= 0)
  137. return -EIO;
  138. stat = sp->psc_smbstat;
  139. au_sync();
  140. if ((stat & PSC_SMBSTAT_RE) == 0)
  141. j = 0;
  142. else
  143. udelay(1);
  144. } while (j > 0);
  145. data = sp->psc_smbtxrx;
  146. au_sync();
  147. *ret_data = data;
  148. return 0;
  149. }
  150. static int
  151. i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
  152. unsigned int len)
  153. {
  154. int i;
  155. u32 data;
  156. volatile psc_smb_t *sp;
  157. if (len == 0)
  158. return 0;
  159. /* A read is performed by stuffing the transmit fifo with
  160. * zero bytes for timing, waiting for bytes to appear in the
  161. * receive fifo, then reading the bytes.
  162. */
  163. sp = (volatile psc_smb_t *)(adap->psc_base);
  164. i = 0;
  165. while (i < (len-1)) {
  166. sp->psc_smbtxrx = 0;
  167. au_sync();
  168. if (wait_for_rx_byte(adap, &data))
  169. return -EIO;
  170. buf[i] = data;
  171. i++;
  172. }
  173. /* The last byte has to indicate transfer done.
  174. */
  175. sp->psc_smbtxrx = PSC_SMBTXRX_STP;
  176. au_sync();
  177. if (wait_master_done(adap))
  178. return -EIO;
  179. data = sp->psc_smbtxrx;
  180. au_sync();
  181. buf[i] = data;
  182. return 0;
  183. }
  184. static int
  185. i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
  186. unsigned int len)
  187. {
  188. int i;
  189. u32 data;
  190. volatile psc_smb_t *sp;
  191. if (len == 0)
  192. return 0;
  193. sp = (volatile psc_smb_t *)(adap->psc_base);
  194. i = 0;
  195. while (i < (len-1)) {
  196. data = buf[i];
  197. sp->psc_smbtxrx = data;
  198. au_sync();
  199. if (wait_ack(adap))
  200. return -EIO;
  201. i++;
  202. }
  203. /* The last byte has to indicate transfer done.
  204. */
  205. data = buf[i];
  206. data |= PSC_SMBTXRX_STP;
  207. sp->psc_smbtxrx = data;
  208. au_sync();
  209. if (wait_master_done(adap))
  210. return -EIO;
  211. return 0;
  212. }
  213. static int
  214. au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
  215. {
  216. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  217. struct i2c_msg *p;
  218. int i, err = 0;
  219. for (i = 0; !err && i < num; i++) {
  220. p = &msgs[i];
  221. err = do_address(adap, p->addr, p->flags & I2C_M_RD);
  222. if (err || !p->len)
  223. continue;
  224. if (p->flags & I2C_M_RD)
  225. err = i2c_read(adap, p->buf, p->len);
  226. else
  227. err = i2c_write(adap, p->buf, p->len);
  228. }
  229. /* Return the number of messages processed, or the error code.
  230. */
  231. if (err == 0)
  232. err = num;
  233. return err;
  234. }
  235. static u32
  236. au1550_func(struct i2c_adapter *adap)
  237. {
  238. return I2C_FUNC_I2C;
  239. }
  240. static struct i2c_algorithm au1550_algo = {
  241. .name = "Au1550 algorithm",
  242. .id = I2C_ALGO_AU1550,
  243. .master_xfer = au1550_xfer,
  244. .functionality = au1550_func,
  245. };
  246. /*
  247. * registering functions to load algorithms at runtime
  248. * Prior to calling us, the 50MHz clock frequency and routing
  249. * must have been set up for the PSC indicated by the adapter.
  250. */
  251. int
  252. i2c_au1550_add_bus(struct i2c_adapter *i2c_adap)
  253. {
  254. struct i2c_au1550_data *adap = i2c_adap->algo_data;
  255. volatile psc_smb_t *sp;
  256. u32 stat;
  257. i2c_adap->algo = &au1550_algo;
  258. /* Now, set up the PSC for SMBus PIO mode.
  259. */
  260. sp = (volatile psc_smb_t *)(adap->psc_base);
  261. sp->psc_ctrl = PSC_CTRL_DISABLE;
  262. au_sync();
  263. sp->psc_sel = PSC_SEL_PS_SMBUSMODE;
  264. sp->psc_smbcfg = 0;
  265. au_sync();
  266. sp->psc_ctrl = PSC_CTRL_ENABLE;
  267. au_sync();
  268. do {
  269. stat = sp->psc_smbstat;
  270. au_sync();
  271. } while ((stat & PSC_SMBSTAT_SR) == 0);
  272. sp->psc_smbcfg = (PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 |
  273. PSC_SMBCFG_DD_DISABLE);
  274. /* Divide by 8 to get a 6.25 MHz clock. The later protocol
  275. * timings are based on this clock.
  276. */
  277. sp->psc_smbcfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
  278. sp->psc_smbmsk = PSC_SMBMSK_ALLMASK;
  279. au_sync();
  280. /* Set the protocol timer values. See Table 71 in the
  281. * Au1550 Data Book for standard timing values.
  282. */
  283. sp->psc_smbtmr = PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
  284. PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
  285. PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
  286. PSC_SMBTMR_SET_CH(15);
  287. au_sync();
  288. sp->psc_smbcfg |= PSC_SMBCFG_DE_ENABLE;
  289. do {
  290. stat = sp->psc_smbstat;
  291. au_sync();
  292. } while ((stat & PSC_SMBSTAT_DR) == 0);
  293. return i2c_add_adapter(i2c_adap);
  294. }
  295. int
  296. i2c_au1550_del_bus(struct i2c_adapter *adap)
  297. {
  298. return i2c_del_adapter(adap);
  299. }
  300. static int
  301. pb1550_reg(struct i2c_client *client)
  302. {
  303. return 0;
  304. }
  305. static int
  306. pb1550_unreg(struct i2c_client *client)
  307. {
  308. return 0;
  309. }
  310. static struct i2c_au1550_data pb1550_i2c_info = {
  311. SMBUS_PSC_BASE, 200, 200
  312. };
  313. static struct i2c_adapter pb1550_board_adapter = {
  314. name: "pb1550 adapter",
  315. id: I2C_HW_AU1550_PSC,
  316. algo: NULL,
  317. algo_data: &pb1550_i2c_info,
  318. client_register: pb1550_reg,
  319. client_unregister: pb1550_unreg,
  320. };
  321. /* BIG hack to support the control interface on the Wolfson WM8731
  322. * audio codec on the Pb1550 board. We get an address and two data
  323. * bytes to write, create an i2c message, and send it across the
  324. * i2c transfer function. We do this here because we have access to
  325. * the i2c adapter structure.
  326. */
  327. static struct i2c_msg wm_i2c_msg; /* We don't want this stuff on the stack */
  328. static u8 i2cbuf[2];
  329. int
  330. pb1550_wm_codec_write(u8 addr, u8 reg, u8 val)
  331. {
  332. wm_i2c_msg.addr = addr;
  333. wm_i2c_msg.flags = 0;
  334. wm_i2c_msg.buf = i2cbuf;
  335. wm_i2c_msg.len = 2;
  336. i2cbuf[0] = reg;
  337. i2cbuf[1] = val;
  338. return pb1550_board_adapter.algo->master_xfer(&pb1550_board_adapter, &wm_i2c_msg, 1);
  339. }
  340. static int __init
  341. i2c_au1550_init(void)
  342. {
  343. printk(KERN_INFO "Au1550 I2C: ");
  344. /* This is where we would set up a 50MHz clock source
  345. * and routing. On the Pb1550, the SMBus is PSC2, which
  346. * uses a shared clock with USB. This has been already
  347. * configured by Yamon as a 48MHz clock, close enough
  348. * for our work.
  349. */
  350. if (i2c_au1550_add_bus(&pb1550_board_adapter) < 0) {
  351. printk("failed to initialize.\n");
  352. return -ENODEV;
  353. }
  354. printk("initialized.\n");
  355. return 0;
  356. }
  357. static void __exit
  358. i2c_au1550_exit(void)
  359. {
  360. i2c_au1550_del_bus(&pb1550_board_adapter);
  361. }
  362. MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
  363. MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
  364. MODULE_LICENSE("GPL");
  365. module_init (i2c_au1550_init);
  366. module_exit (i2c_au1550_exit);