pmac_low_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * arch/ppc/platforms/pmac_low_i2c.c
  3. *
  4. * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * This file contains some low-level i2c access routines that
  12. * need to be used by various bits of the PowerMac platform code
  13. * at times where the real asynchronous & interrupt driven driver
  14. * cannot be used. The API borrows some semantics from the darwin
  15. * driver in order to ease the implementation of the platform
  16. * properties parser
  17. */
  18. #undef DEBUG
  19. #include <linux/config.h>
  20. #include <linux/types.h>
  21. #include <linux/sched.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/adb.h>
  25. #include <linux/pmu.h>
  26. #include <asm/keylargo.h>
  27. #include <asm/uninorth.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/machdep.h>
  31. #include <asm/pmac_low_i2c.h>
  32. #define MAX_LOW_I2C_HOST 4
  33. #ifdef DEBUG
  34. #define DBG(x...) do {\
  35. printk(KERN_DEBUG "KW:" x); \
  36. } while(0)
  37. #else
  38. #define DBG(x...)
  39. #endif
  40. struct low_i2c_host;
  41. typedef int (*low_i2c_func_t)(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len);
  42. struct low_i2c_host
  43. {
  44. struct device_node *np; /* OF device node */
  45. struct semaphore mutex; /* Access mutex for use by i2c-keywest */
  46. low_i2c_func_t func; /* Access function */
  47. unsigned int is_open : 1; /* Poor man's access control */
  48. int mode; /* Current mode */
  49. int channel; /* Current channel */
  50. int num_channels; /* Number of channels */
  51. void __iomem *base; /* For keywest-i2c, base address */
  52. int bsteps; /* And register stepping */
  53. int speed; /* And speed */
  54. };
  55. static struct low_i2c_host low_i2c_hosts[MAX_LOW_I2C_HOST];
  56. /* No locking is necessary on allocation, we are running way before
  57. * anything can race with us
  58. */
  59. static struct low_i2c_host *find_low_i2c_host(struct device_node *np)
  60. {
  61. int i;
  62. for (i = 0; i < MAX_LOW_I2C_HOST; i++)
  63. if (low_i2c_hosts[i].np == np)
  64. return &low_i2c_hosts[i];
  65. return NULL;
  66. }
  67. /*
  68. *
  69. * i2c-keywest implementation (UniNorth, U2, U3, Keylargo's)
  70. *
  71. */
  72. /*
  73. * Keywest i2c definitions borrowed from drivers/i2c/i2c-keywest.h,
  74. * should be moved somewhere in include/asm-ppc/
  75. */
  76. /* Register indices */
  77. typedef enum {
  78. reg_mode = 0,
  79. reg_control,
  80. reg_status,
  81. reg_isr,
  82. reg_ier,
  83. reg_addr,
  84. reg_subaddr,
  85. reg_data
  86. } reg_t;
  87. /* Mode register */
  88. #define KW_I2C_MODE_100KHZ 0x00
  89. #define KW_I2C_MODE_50KHZ 0x01
  90. #define KW_I2C_MODE_25KHZ 0x02
  91. #define KW_I2C_MODE_DUMB 0x00
  92. #define KW_I2C_MODE_STANDARD 0x04
  93. #define KW_I2C_MODE_STANDARDSUB 0x08
  94. #define KW_I2C_MODE_COMBINED 0x0C
  95. #define KW_I2C_MODE_MODE_MASK 0x0C
  96. #define KW_I2C_MODE_CHAN_MASK 0xF0
  97. /* Control register */
  98. #define KW_I2C_CTL_AAK 0x01
  99. #define KW_I2C_CTL_XADDR 0x02
  100. #define KW_I2C_CTL_STOP 0x04
  101. #define KW_I2C_CTL_START 0x08
  102. /* Status register */
  103. #define KW_I2C_STAT_BUSY 0x01
  104. #define KW_I2C_STAT_LAST_AAK 0x02
  105. #define KW_I2C_STAT_LAST_RW 0x04
  106. #define KW_I2C_STAT_SDA 0x08
  107. #define KW_I2C_STAT_SCL 0x10
  108. /* IER & ISR registers */
  109. #define KW_I2C_IRQ_DATA 0x01
  110. #define KW_I2C_IRQ_ADDR 0x02
  111. #define KW_I2C_IRQ_STOP 0x04
  112. #define KW_I2C_IRQ_START 0x08
  113. #define KW_I2C_IRQ_MASK 0x0F
  114. /* State machine states */
  115. enum {
  116. state_idle,
  117. state_addr,
  118. state_read,
  119. state_write,
  120. state_stop,
  121. state_dead
  122. };
  123. #define WRONG_STATE(name) do {\
  124. printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
  125. name, __kw_state_names[state], isr); \
  126. } while(0)
  127. static const char *__kw_state_names[] = {
  128. "state_idle",
  129. "state_addr",
  130. "state_read",
  131. "state_write",
  132. "state_stop",
  133. "state_dead"
  134. };
  135. static inline u8 __kw_read_reg(struct low_i2c_host *host, reg_t reg)
  136. {
  137. return readb(host->base + (((unsigned int)reg) << host->bsteps));
  138. }
  139. static inline void __kw_write_reg(struct low_i2c_host *host, reg_t reg, u8 val)
  140. {
  141. writeb(val, host->base + (((unsigned)reg) << host->bsteps));
  142. (void)__kw_read_reg(host, reg_subaddr);
  143. }
  144. #define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)
  145. #define kw_read_reg(reg) __kw_read_reg(host, reg)
  146. /* Don't schedule, the g5 fan controller is too
  147. * timing sensitive
  148. */
  149. static u8 kw_wait_interrupt(struct low_i2c_host* host)
  150. {
  151. int i, j;
  152. u8 isr;
  153. for (i = 0; i < 100000; i++) {
  154. isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  155. if (isr != 0)
  156. return isr;
  157. /* This code is used with the timebase frozen, we cannot rely
  158. * on udelay ! For now, just use a bogus loop
  159. */
  160. for (j = 1; j < 10000; j++)
  161. mb();
  162. }
  163. return isr;
  164. }
  165. static int kw_handle_interrupt(struct low_i2c_host *host, int state, int rw, int *rc, u8 **data, int *len, u8 isr)
  166. {
  167. u8 ack;
  168. DBG("kw_handle_interrupt(%s, isr: %x)\n", __kw_state_names[state], isr);
  169. if (isr == 0) {
  170. if (state != state_stop) {
  171. DBG("KW: Timeout !\n");
  172. *rc = -EIO;
  173. goto stop;
  174. }
  175. if (state == state_stop) {
  176. ack = kw_read_reg(reg_status);
  177. if (!(ack & KW_I2C_STAT_BUSY)) {
  178. state = state_idle;
  179. kw_write_reg(reg_ier, 0x00);
  180. }
  181. }
  182. return state;
  183. }
  184. if (isr & KW_I2C_IRQ_ADDR) {
  185. ack = kw_read_reg(reg_status);
  186. if (state != state_addr) {
  187. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  188. WRONG_STATE("KW_I2C_IRQ_ADDR");
  189. *rc = -EIO;
  190. goto stop;
  191. }
  192. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  193. *rc = -ENODEV;
  194. DBG("KW: NAK on address\n");
  195. return state_stop;
  196. } else {
  197. if (rw) {
  198. state = state_read;
  199. if (*len > 1)
  200. kw_write_reg(reg_control, KW_I2C_CTL_AAK);
  201. } else {
  202. state = state_write;
  203. kw_write_reg(reg_data, **data);
  204. (*data)++; (*len)--;
  205. }
  206. }
  207. kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  208. }
  209. if (isr & KW_I2C_IRQ_DATA) {
  210. if (state == state_read) {
  211. **data = kw_read_reg(reg_data);
  212. (*data)++; (*len)--;
  213. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  214. if ((*len) == 0)
  215. state = state_stop;
  216. else if ((*len) == 1)
  217. kw_write_reg(reg_control, 0);
  218. } else if (state == state_write) {
  219. ack = kw_read_reg(reg_status);
  220. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  221. DBG("KW: nack on data write\n");
  222. *rc = -EIO;
  223. goto stop;
  224. } else if (*len) {
  225. kw_write_reg(reg_data, **data);
  226. (*data)++; (*len)--;
  227. } else {
  228. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  229. state = state_stop;
  230. *rc = 0;
  231. }
  232. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  233. } else {
  234. kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);
  235. WRONG_STATE("KW_I2C_IRQ_DATA");
  236. if (state != state_stop) {
  237. *rc = -EIO;
  238. goto stop;
  239. }
  240. }
  241. }
  242. if (isr & KW_I2C_IRQ_STOP) {
  243. kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);
  244. if (state != state_stop) {
  245. WRONG_STATE("KW_I2C_IRQ_STOP");
  246. *rc = -EIO;
  247. }
  248. return state_idle;
  249. }
  250. if (isr & KW_I2C_IRQ_START)
  251. kw_write_reg(reg_isr, KW_I2C_IRQ_START);
  252. return state;
  253. stop:
  254. kw_write_reg(reg_control, KW_I2C_CTL_STOP);
  255. return state_stop;
  256. }
  257. static int keywest_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 subaddr, u8 *data, int len)
  258. {
  259. u8 mode_reg = host->speed;
  260. int state = state_addr;
  261. int rc = 0;
  262. /* Setup mode & subaddress if any */
  263. switch(host->mode) {
  264. case pmac_low_i2c_mode_dumb:
  265. printk(KERN_ERR "low_i2c: Dumb mode not supported !\n");
  266. return -EINVAL;
  267. case pmac_low_i2c_mode_std:
  268. mode_reg |= KW_I2C_MODE_STANDARD;
  269. break;
  270. case pmac_low_i2c_mode_stdsub:
  271. mode_reg |= KW_I2C_MODE_STANDARDSUB;
  272. break;
  273. case pmac_low_i2c_mode_combined:
  274. mode_reg |= KW_I2C_MODE_COMBINED;
  275. break;
  276. }
  277. /* Setup channel & clear pending irqs */
  278. kw_write_reg(reg_isr, kw_read_reg(reg_isr));
  279. kw_write_reg(reg_mode, mode_reg | (host->channel << 4));
  280. kw_write_reg(reg_status, 0);
  281. /* Set up address and r/w bit */
  282. kw_write_reg(reg_addr, addr);
  283. /* Set up the sub address */
  284. if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  285. || (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  286. kw_write_reg(reg_subaddr, subaddr);
  287. /* Start sending address & disable interrupt*/
  288. kw_write_reg(reg_ier, 0 /*KW_I2C_IRQ_MASK*/);
  289. kw_write_reg(reg_control, KW_I2C_CTL_XADDR);
  290. /* State machine, to turn into an interrupt handler */
  291. while(state != state_idle) {
  292. u8 isr = kw_wait_interrupt(host);
  293. state = kw_handle_interrupt(host, state, addr & 1, &rc, &data, &len, isr);
  294. }
  295. return rc;
  296. }
  297. static void keywest_low_i2c_add(struct device_node *np)
  298. {
  299. struct low_i2c_host *host = find_low_i2c_host(NULL);
  300. u32 *psteps, *prate, steps, aoffset = 0;
  301. struct device_node *parent;
  302. if (host == NULL) {
  303. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  304. np->full_name);
  305. return;
  306. }
  307. memset(host, 0, sizeof(*host));
  308. init_MUTEX(&host->mutex);
  309. host->np = of_node_get(np);
  310. psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
  311. steps = psteps ? (*psteps) : 0x10;
  312. for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)
  313. steps >>= 1;
  314. parent = of_get_parent(np);
  315. host->num_channels = 1;
  316. if (parent && parent->name[0] == 'u') {
  317. host->num_channels = 2;
  318. aoffset = 3;
  319. }
  320. /* Select interface rate */
  321. host->speed = KW_I2C_MODE_100KHZ;
  322. prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
  323. if (prate) switch(*prate) {
  324. case 100:
  325. host->speed = KW_I2C_MODE_100KHZ;
  326. break;
  327. case 50:
  328. host->speed = KW_I2C_MODE_50KHZ;
  329. break;
  330. case 25:
  331. host->speed = KW_I2C_MODE_25KHZ;
  332. break;
  333. }
  334. host->mode = pmac_low_i2c_mode_std;
  335. host->base = ioremap(np->addrs[0].address + aoffset,
  336. np->addrs[0].size);
  337. host->func = keywest_low_i2c_func;
  338. }
  339. /*
  340. *
  341. * PMU implementation
  342. *
  343. */
  344. #ifdef CONFIG_ADB_PMU
  345. static int pmu_low_i2c_func(struct low_i2c_host *host, u8 addr, u8 sub, u8 *data, int len)
  346. {
  347. // TODO
  348. return -ENODEV;
  349. }
  350. static void pmu_low_i2c_add(struct device_node *np)
  351. {
  352. struct low_i2c_host *host = find_low_i2c_host(NULL);
  353. if (host == NULL) {
  354. printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",
  355. np->full_name);
  356. return;
  357. }
  358. memset(host, 0, sizeof(*host));
  359. init_MUTEX(&host->mutex);
  360. host->np = of_node_get(np);
  361. host->num_channels = 3;
  362. host->mode = pmac_low_i2c_mode_std;
  363. host->func = pmu_low_i2c_func;
  364. }
  365. #endif /* CONFIG_ADB_PMU */
  366. void __init pmac_init_low_i2c(void)
  367. {
  368. struct device_node *np;
  369. /* Probe keywest-i2c busses */
  370. np = of_find_compatible_node(NULL, "i2c", "keywest-i2c");
  371. while(np) {
  372. keywest_low_i2c_add(np);
  373. np = of_find_compatible_node(np, "i2c", "keywest-i2c");
  374. }
  375. #ifdef CONFIG_ADB_PMU
  376. /* Probe PMU busses */
  377. np = of_find_node_by_name(NULL, "via-pmu");
  378. if (np)
  379. pmu_low_i2c_add(np);
  380. #endif /* CONFIG_ADB_PMU */
  381. /* TODO: Add CUDA support as well */
  382. }
  383. int pmac_low_i2c_lock(struct device_node *np)
  384. {
  385. struct low_i2c_host *host = find_low_i2c_host(np);
  386. if (!host)
  387. return -ENODEV;
  388. down(&host->mutex);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(pmac_low_i2c_lock);
  392. int pmac_low_i2c_unlock(struct device_node *np)
  393. {
  394. struct low_i2c_host *host = find_low_i2c_host(np);
  395. if (!host)
  396. return -ENODEV;
  397. up(&host->mutex);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(pmac_low_i2c_unlock);
  401. int pmac_low_i2c_open(struct device_node *np, int channel)
  402. {
  403. struct low_i2c_host *host = find_low_i2c_host(np);
  404. if (!host)
  405. return -ENODEV;
  406. if (channel >= host->num_channels)
  407. return -EINVAL;
  408. down(&host->mutex);
  409. host->is_open = 1;
  410. host->channel = channel;
  411. return 0;
  412. }
  413. EXPORT_SYMBOL(pmac_low_i2c_open);
  414. int pmac_low_i2c_close(struct device_node *np)
  415. {
  416. struct low_i2c_host *host = find_low_i2c_host(np);
  417. if (!host)
  418. return -ENODEV;
  419. host->is_open = 0;
  420. up(&host->mutex);
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(pmac_low_i2c_close);
  424. int pmac_low_i2c_setmode(struct device_node *np, int mode)
  425. {
  426. struct low_i2c_host *host = find_low_i2c_host(np);
  427. if (!host)
  428. return -ENODEV;
  429. WARN_ON(!host->is_open);
  430. host->mode = mode;
  431. return 0;
  432. }
  433. EXPORT_SYMBOL(pmac_low_i2c_setmode);
  434. int pmac_low_i2c_xfer(struct device_node *np, u8 addrdir, u8 subaddr, u8 *data, int len)
  435. {
  436. struct low_i2c_host *host = find_low_i2c_host(np);
  437. if (!host)
  438. return -ENODEV;
  439. WARN_ON(!host->is_open);
  440. return host->func(host, addrdir, subaddr, data, len);
  441. }
  442. EXPORT_SYMBOL(pmac_low_i2c_xfer);