scx200_acb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  3. National Semiconductor SCx200 ACCESS.bus support
  4. Based on i2c-keywest.c which is:
  5. Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  6. Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/i2c.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/pci.h>
  26. #include <linux/delay.h>
  27. #include <asm/io.h>
  28. #include <linux/scx200.h>
  29. #define NAME "scx200_acb"
  30. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  31. MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
  32. MODULE_LICENSE("GPL");
  33. #define MAX_DEVICES 4
  34. static int base[MAX_DEVICES] = { 0x820, 0x840 };
  35. module_param_array(base, int, NULL, 0);
  36. MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
  37. /* The hardware supports interrupt driven mode too, but I haven't
  38. implemented that. */
  39. #define POLLED_MODE 1
  40. #define POLL_TIMEOUT (HZ)
  41. enum scx200_acb_state {
  42. state_idle,
  43. state_address,
  44. state_command,
  45. state_repeat_start,
  46. state_quick,
  47. state_read,
  48. state_write,
  49. };
  50. static const char *scx200_acb_state_name[] = {
  51. "idle",
  52. "address",
  53. "command",
  54. "repeat_start",
  55. "quick",
  56. "read",
  57. "write",
  58. };
  59. /* Physical interface */
  60. struct scx200_acb_iface {
  61. struct scx200_acb_iface *next;
  62. struct i2c_adapter adapter;
  63. unsigned base;
  64. struct semaphore sem;
  65. /* State machine data */
  66. enum scx200_acb_state state;
  67. int result;
  68. u8 address_byte;
  69. u8 command;
  70. u8 *ptr;
  71. char needs_reset;
  72. unsigned len;
  73. };
  74. /* Register Definitions */
  75. #define ACBSDA (iface->base + 0)
  76. #define ACBST (iface->base + 1)
  77. #define ACBST_SDAST 0x40 /* SDA Status */
  78. #define ACBST_BER 0x20
  79. #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
  80. #define ACBST_STASTR 0x08 /* Stall After Start */
  81. #define ACBST_MASTER 0x02
  82. #define ACBCST (iface->base + 2)
  83. #define ACBCST_BB 0x02
  84. #define ACBCTL1 (iface->base + 3)
  85. #define ACBCTL1_STASTRE 0x80
  86. #define ACBCTL1_NMINTE 0x40
  87. #define ACBCTL1_ACK 0x10
  88. #define ACBCTL1_STOP 0x02
  89. #define ACBCTL1_START 0x01
  90. #define ACBADDR (iface->base + 4)
  91. #define ACBCTL2 (iface->base + 5)
  92. #define ACBCTL2_ENABLE 0x01
  93. /************************************************************************/
  94. static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
  95. {
  96. const char *errmsg;
  97. dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
  98. scx200_acb_state_name[iface->state], status);
  99. if (status & ACBST_BER) {
  100. errmsg = "bus error";
  101. goto error;
  102. }
  103. if (!(status & ACBST_MASTER)) {
  104. errmsg = "not master";
  105. goto error;
  106. }
  107. if (status & ACBST_NEGACK)
  108. goto negack;
  109. switch (iface->state) {
  110. case state_idle:
  111. dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
  112. break;
  113. case state_address:
  114. /* Do a pointer write first */
  115. outb(iface->address_byte & ~1, ACBSDA);
  116. iface->state = state_command;
  117. break;
  118. case state_command:
  119. outb(iface->command, ACBSDA);
  120. if (iface->address_byte & 1)
  121. iface->state = state_repeat_start;
  122. else
  123. iface->state = state_write;
  124. break;
  125. case state_repeat_start:
  126. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  127. /* fallthrough */
  128. case state_quick:
  129. if (iface->address_byte & 1) {
  130. if (iface->len == 1)
  131. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  132. else
  133. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  134. outb(iface->address_byte, ACBSDA);
  135. iface->state = state_read;
  136. } else {
  137. outb(iface->address_byte, ACBSDA);
  138. iface->state = state_write;
  139. }
  140. break;
  141. case state_read:
  142. /* Set ACK if receiving the last byte */
  143. if (iface->len == 1)
  144. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  145. else
  146. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  147. *iface->ptr++ = inb(ACBSDA);
  148. --iface->len;
  149. if (iface->len == 0) {
  150. iface->result = 0;
  151. iface->state = state_idle;
  152. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  153. }
  154. break;
  155. case state_write:
  156. if (iface->len == 0) {
  157. iface->result = 0;
  158. iface->state = state_idle;
  159. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  160. break;
  161. }
  162. outb(*iface->ptr++, ACBSDA);
  163. --iface->len;
  164. break;
  165. }
  166. return;
  167. negack:
  168. dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
  169. scx200_acb_state_name[iface->state]);
  170. iface->state = state_idle;
  171. iface->result = -ENXIO;
  172. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  173. outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
  174. return;
  175. error:
  176. dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
  177. scx200_acb_state_name[iface->state]);
  178. iface->state = state_idle;
  179. iface->result = -EIO;
  180. iface->needs_reset = 1;
  181. }
  182. static void scx200_acb_timeout(struct scx200_acb_iface *iface)
  183. {
  184. dev_err(&iface->adapter.dev, "timeout in state %s\n",
  185. scx200_acb_state_name[iface->state]);
  186. iface->state = state_idle;
  187. iface->result = -EIO;
  188. iface->needs_reset = 1;
  189. }
  190. #ifdef POLLED_MODE
  191. static void scx200_acb_poll(struct scx200_acb_iface *iface)
  192. {
  193. u8 status = 0;
  194. unsigned long timeout;
  195. timeout = jiffies + POLL_TIMEOUT;
  196. while (time_before(jiffies, timeout)) {
  197. status = inb(ACBST);
  198. if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
  199. scx200_acb_machine(iface, status);
  200. return;
  201. }
  202. msleep(10);
  203. }
  204. scx200_acb_timeout(iface);
  205. }
  206. #endif /* POLLED_MODE */
  207. static void scx200_acb_reset(struct scx200_acb_iface *iface)
  208. {
  209. /* Disable the ACCESS.bus device and Configure the SCL
  210. frequency: 16 clock cycles */
  211. outb(0x70, ACBCTL2);
  212. /* Polling mode */
  213. outb(0, ACBCTL1);
  214. /* Disable slave address */
  215. outb(0, ACBADDR);
  216. /* Enable the ACCESS.bus device */
  217. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  218. /* Free STALL after START */
  219. outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
  220. /* Send a STOP */
  221. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  222. /* Clear BER, NEGACK and STASTR bits */
  223. outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
  224. /* Clear BB bit */
  225. outb(inb(ACBCST) | ACBCST_BB, ACBCST);
  226. }
  227. static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
  228. u16 address, unsigned short flags,
  229. char rw, u8 command, int size,
  230. union i2c_smbus_data *data)
  231. {
  232. struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
  233. int len;
  234. u8 *buffer;
  235. u16 cur_word;
  236. int rc;
  237. switch (size) {
  238. case I2C_SMBUS_QUICK:
  239. len = 0;
  240. buffer = NULL;
  241. break;
  242. case I2C_SMBUS_BYTE:
  243. if (rw == I2C_SMBUS_READ) {
  244. len = 1;
  245. buffer = &data->byte;
  246. } else {
  247. len = 1;
  248. buffer = &command;
  249. }
  250. break;
  251. case I2C_SMBUS_BYTE_DATA:
  252. len = 1;
  253. buffer = &data->byte;
  254. break;
  255. case I2C_SMBUS_WORD_DATA:
  256. len = 2;
  257. cur_word = cpu_to_le16(data->word);
  258. buffer = (u8 *)&cur_word;
  259. break;
  260. case I2C_SMBUS_BLOCK_DATA:
  261. len = data->block[0];
  262. buffer = &data->block[1];
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. dev_dbg(&adapter->dev,
  268. "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
  269. size, address, command, len, rw);
  270. if (!len && rw == I2C_SMBUS_READ) {
  271. dev_dbg(&adapter->dev, "zero length read\n");
  272. return -EINVAL;
  273. }
  274. down(&iface->sem);
  275. iface->address_byte = address<<1;
  276. if (rw == I2C_SMBUS_READ)
  277. iface->address_byte |= 1;
  278. iface->command = command;
  279. iface->ptr = buffer;
  280. iface->len = len;
  281. iface->result = -EINVAL;
  282. iface->needs_reset = 0;
  283. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  284. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
  285. iface->state = state_quick;
  286. else
  287. iface->state = state_address;
  288. #ifdef POLLED_MODE
  289. while (iface->state != state_idle)
  290. scx200_acb_poll(iface);
  291. #else /* POLLED_MODE */
  292. #error Interrupt driven mode not implemented
  293. #endif /* POLLED_MODE */
  294. if (iface->needs_reset)
  295. scx200_acb_reset(iface);
  296. rc = iface->result;
  297. up(&iface->sem);
  298. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
  299. data->word = le16_to_cpu(cur_word);
  300. #ifdef DEBUG
  301. dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
  302. if (buffer) {
  303. int i;
  304. printk(" data:");
  305. for (i = 0; i < len; ++i)
  306. printk(" %02x", buffer[i]);
  307. }
  308. printk("\n");
  309. #endif
  310. return rc;
  311. }
  312. static u32 scx200_acb_func(struct i2c_adapter *adapter)
  313. {
  314. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  315. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  316. I2C_FUNC_SMBUS_BLOCK_DATA;
  317. }
  318. /* For now, we only handle combined mode (smbus) */
  319. static struct i2c_algorithm scx200_acb_algorithm = {
  320. .smbus_xfer = scx200_acb_smbus_xfer,
  321. .functionality = scx200_acb_func,
  322. };
  323. static struct scx200_acb_iface *scx200_acb_list;
  324. static int scx200_acb_probe(struct scx200_acb_iface *iface)
  325. {
  326. u8 val;
  327. /* Disable the ACCESS.bus device and Configure the SCL
  328. frequency: 16 clock cycles */
  329. outb(0x70, ACBCTL2);
  330. if (inb(ACBCTL2) != 0x70) {
  331. pr_debug(NAME ": ACBCTL2 readback failed\n");
  332. return -ENXIO;
  333. }
  334. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  335. val = inb(ACBCTL1);
  336. if (val) {
  337. pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
  338. val);
  339. return -ENXIO;
  340. }
  341. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  342. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  343. val = inb(ACBCTL1);
  344. if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
  345. pr_debug(NAME ": enabled, but NMINTE won't be set, "
  346. "ACBCTL1=0x%02x\n", val);
  347. return -ENXIO;
  348. }
  349. return 0;
  350. }
  351. static int __init scx200_acb_create(int base, int index)
  352. {
  353. struct scx200_acb_iface *iface;
  354. struct i2c_adapter *adapter;
  355. int rc = 0;
  356. char description[64];
  357. iface = kzalloc(sizeof(*iface), GFP_KERNEL);
  358. if (!iface) {
  359. printk(KERN_ERR NAME ": can't allocate memory\n");
  360. rc = -ENOMEM;
  361. goto errout;
  362. }
  363. adapter = &iface->adapter;
  364. i2c_set_adapdata(adapter, iface);
  365. snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
  366. adapter->owner = THIS_MODULE;
  367. adapter->id = I2C_HW_SMBUS_SCX200;
  368. adapter->algo = &scx200_acb_algorithm;
  369. adapter->class = I2C_CLASS_HWMON;
  370. init_MUTEX(&iface->sem);
  371. snprintf(description, sizeof(description),
  372. "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
  373. if (request_region(base, 8, description) == 0) {
  374. printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
  375. base, base + 8-1);
  376. rc = -EBUSY;
  377. goto errout;
  378. }
  379. iface->base = base;
  380. rc = scx200_acb_probe(iface);
  381. if (rc) {
  382. printk(KERN_WARNING NAME ": probe failed\n");
  383. goto errout;
  384. }
  385. scx200_acb_reset(iface);
  386. if (i2c_add_adapter(adapter) < 0) {
  387. printk(KERN_ERR NAME ": failed to register\n");
  388. rc = -ENODEV;
  389. goto errout;
  390. }
  391. lock_kernel();
  392. iface->next = scx200_acb_list;
  393. scx200_acb_list = iface;
  394. unlock_kernel();
  395. return 0;
  396. errout:
  397. if (iface) {
  398. if (iface->base)
  399. release_region(iface->base, 8);
  400. kfree(iface);
  401. }
  402. return rc;
  403. }
  404. static struct pci_device_id scx200[] = {
  405. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
  406. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
  407. { },
  408. };
  409. static int __init scx200_acb_init(void)
  410. {
  411. int i;
  412. int rc;
  413. pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
  414. /* Verify that this really is a SCx200 processor */
  415. if (pci_dev_present(scx200) == 0)
  416. return -ENODEV;
  417. rc = -ENXIO;
  418. for (i = 0; i < MAX_DEVICES; ++i) {
  419. if (base[i] > 0)
  420. rc = scx200_acb_create(base[i], i);
  421. }
  422. if (scx200_acb_list)
  423. return 0;
  424. return rc;
  425. }
  426. static void __exit scx200_acb_cleanup(void)
  427. {
  428. struct scx200_acb_iface *iface;
  429. lock_kernel();
  430. while ((iface = scx200_acb_list) != NULL) {
  431. scx200_acb_list = iface->next;
  432. unlock_kernel();
  433. i2c_del_adapter(&iface->adapter);
  434. release_region(iface->base, 8);
  435. kfree(iface);
  436. lock_kernel();
  437. }
  438. unlock_kernel();
  439. }
  440. module_init(scx200_acb_init);
  441. module_exit(scx200_acb_cleanup);