scx200_acb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
  3. National Semiconductor SCx200 ACCESS.bus support
  4. Also supports the AMD CS5535 and AMD CS5536
  5. Based on i2c-keywest.c which is:
  6. Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  7. Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  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 the
  11. License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/i2c.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/delay.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <linux/scx200.h>
  30. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  31. MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
  32. MODULE_ALIAS("platform:cs5535-smb");
  33. MODULE_LICENSE("GPL");
  34. #define MAX_DEVICES 4
  35. static int base[MAX_DEVICES] = { 0x820, 0x840 };
  36. module_param_array(base, int, NULL, 0);
  37. MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
  38. #define POLL_TIMEOUT (HZ/5)
  39. enum scx200_acb_state {
  40. state_idle,
  41. state_address,
  42. state_command,
  43. state_repeat_start,
  44. state_quick,
  45. state_read,
  46. state_write,
  47. };
  48. static const char *scx200_acb_state_name[] = {
  49. "idle",
  50. "address",
  51. "command",
  52. "repeat_start",
  53. "quick",
  54. "read",
  55. "write",
  56. };
  57. /* Physical interface */
  58. struct scx200_acb_iface {
  59. struct scx200_acb_iface *next;
  60. struct i2c_adapter adapter;
  61. unsigned base;
  62. struct mutex mutex;
  63. /* State machine data */
  64. enum scx200_acb_state state;
  65. int result;
  66. u8 address_byte;
  67. u8 command;
  68. u8 *ptr;
  69. char needs_reset;
  70. unsigned len;
  71. };
  72. /* Register Definitions */
  73. #define ACBSDA (iface->base + 0)
  74. #define ACBST (iface->base + 1)
  75. #define ACBST_SDAST 0x40 /* SDA Status */
  76. #define ACBST_BER 0x20
  77. #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
  78. #define ACBST_STASTR 0x08 /* Stall After Start */
  79. #define ACBST_MASTER 0x02
  80. #define ACBCST (iface->base + 2)
  81. #define ACBCST_BB 0x02
  82. #define ACBCTL1 (iface->base + 3)
  83. #define ACBCTL1_STASTRE 0x80
  84. #define ACBCTL1_NMINTE 0x40
  85. #define ACBCTL1_ACK 0x10
  86. #define ACBCTL1_STOP 0x02
  87. #define ACBCTL1_START 0x01
  88. #define ACBADDR (iface->base + 4)
  89. #define ACBCTL2 (iface->base + 5)
  90. #define ACBCTL2_ENABLE 0x01
  91. /************************************************************************/
  92. static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
  93. {
  94. const char *errmsg;
  95. dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
  96. scx200_acb_state_name[iface->state], status);
  97. if (status & ACBST_BER) {
  98. errmsg = "bus error";
  99. goto error;
  100. }
  101. if (!(status & ACBST_MASTER)) {
  102. errmsg = "not master";
  103. goto error;
  104. }
  105. if (status & ACBST_NEGACK) {
  106. dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
  107. scx200_acb_state_name[iface->state]);
  108. iface->state = state_idle;
  109. iface->result = -ENXIO;
  110. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  111. outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
  112. /* Reset the status register */
  113. outb(0, ACBST);
  114. return;
  115. }
  116. switch (iface->state) {
  117. case state_idle:
  118. dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
  119. break;
  120. case state_address:
  121. /* Do a pointer write first */
  122. outb(iface->address_byte & ~1, ACBSDA);
  123. iface->state = state_command;
  124. break;
  125. case state_command:
  126. outb(iface->command, ACBSDA);
  127. if (iface->address_byte & 1)
  128. iface->state = state_repeat_start;
  129. else
  130. iface->state = state_write;
  131. break;
  132. case state_repeat_start:
  133. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  134. /* fallthrough */
  135. case state_quick:
  136. if (iface->address_byte & 1) {
  137. if (iface->len == 1)
  138. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  139. else
  140. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  141. outb(iface->address_byte, ACBSDA);
  142. iface->state = state_read;
  143. } else {
  144. outb(iface->address_byte, ACBSDA);
  145. iface->state = state_write;
  146. }
  147. break;
  148. case state_read:
  149. /* Set ACK if _next_ byte will be the last one */
  150. if (iface->len == 2)
  151. outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
  152. else
  153. outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
  154. if (iface->len == 1) {
  155. iface->result = 0;
  156. iface->state = state_idle;
  157. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  158. }
  159. *iface->ptr++ = inb(ACBSDA);
  160. --iface->len;
  161. break;
  162. case state_write:
  163. if (iface->len == 0) {
  164. iface->result = 0;
  165. iface->state = state_idle;
  166. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  167. break;
  168. }
  169. outb(*iface->ptr++, ACBSDA);
  170. --iface->len;
  171. break;
  172. }
  173. return;
  174. error:
  175. dev_err(&iface->adapter.dev,
  176. "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
  177. scx200_acb_state_name[iface->state], iface->address_byte,
  178. iface->len, status);
  179. iface->state = state_idle;
  180. iface->result = -EIO;
  181. iface->needs_reset = 1;
  182. }
  183. static void scx200_acb_poll(struct scx200_acb_iface *iface)
  184. {
  185. u8 status;
  186. unsigned long timeout;
  187. timeout = jiffies + POLL_TIMEOUT;
  188. while (1) {
  189. status = inb(ACBST);
  190. /* Reset the status register to avoid the hang */
  191. outb(0, ACBST);
  192. if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
  193. scx200_acb_machine(iface, status);
  194. return;
  195. }
  196. if (time_after(jiffies, timeout))
  197. break;
  198. cpu_relax();
  199. cond_resched();
  200. }
  201. dev_err(&iface->adapter.dev, "timeout in state %s\n",
  202. scx200_acb_state_name[iface->state]);
  203. iface->state = state_idle;
  204. iface->result = -EIO;
  205. iface->needs_reset = 1;
  206. }
  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. len = 1;
  244. buffer = rw ? &data->byte : &command;
  245. break;
  246. case I2C_SMBUS_BYTE_DATA:
  247. len = 1;
  248. buffer = &data->byte;
  249. break;
  250. case I2C_SMBUS_WORD_DATA:
  251. len = 2;
  252. cur_word = cpu_to_le16(data->word);
  253. buffer = (u8 *)&cur_word;
  254. break;
  255. case I2C_SMBUS_I2C_BLOCK_DATA:
  256. len = data->block[0];
  257. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  258. return -EINVAL;
  259. buffer = &data->block[1];
  260. break;
  261. default:
  262. return -EINVAL;
  263. }
  264. dev_dbg(&adapter->dev,
  265. "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
  266. size, address, command, len, rw);
  267. if (!len && rw == I2C_SMBUS_READ) {
  268. dev_dbg(&adapter->dev, "zero length read\n");
  269. return -EINVAL;
  270. }
  271. mutex_lock(&iface->mutex);
  272. iface->address_byte = (address << 1) | rw;
  273. iface->command = command;
  274. iface->ptr = buffer;
  275. iface->len = len;
  276. iface->result = -EINVAL;
  277. iface->needs_reset = 0;
  278. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  279. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
  280. iface->state = state_quick;
  281. else
  282. iface->state = state_address;
  283. while (iface->state != state_idle)
  284. scx200_acb_poll(iface);
  285. if (iface->needs_reset)
  286. scx200_acb_reset(iface);
  287. rc = iface->result;
  288. mutex_unlock(&iface->mutex);
  289. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
  290. data->word = le16_to_cpu(cur_word);
  291. #ifdef DEBUG
  292. dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
  293. if (buffer) {
  294. int i;
  295. printk(" data:");
  296. for (i = 0; i < len; ++i)
  297. printk(" %02x", buffer[i]);
  298. }
  299. printk("\n");
  300. #endif
  301. return rc;
  302. }
  303. static u32 scx200_acb_func(struct i2c_adapter *adapter)
  304. {
  305. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  306. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  307. I2C_FUNC_SMBUS_I2C_BLOCK;
  308. }
  309. /* For now, we only handle combined mode (smbus) */
  310. static const struct i2c_algorithm scx200_acb_algorithm = {
  311. .smbus_xfer = scx200_acb_smbus_xfer,
  312. .functionality = scx200_acb_func,
  313. };
  314. static struct scx200_acb_iface *scx200_acb_list;
  315. static DEFINE_MUTEX(scx200_acb_list_mutex);
  316. static int scx200_acb_probe(struct scx200_acb_iface *iface)
  317. {
  318. u8 val;
  319. /* Disable the ACCESS.bus device and Configure the SCL
  320. frequency: 16 clock cycles */
  321. outb(0x70, ACBCTL2);
  322. if (inb(ACBCTL2) != 0x70) {
  323. pr_debug("ACBCTL2 readback failed\n");
  324. return -ENXIO;
  325. }
  326. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  327. val = inb(ACBCTL1);
  328. if (val) {
  329. pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
  330. return -ENXIO;
  331. }
  332. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  333. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  334. val = inb(ACBCTL1);
  335. if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
  336. pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
  337. val);
  338. return -ENXIO;
  339. }
  340. return 0;
  341. }
  342. static struct scx200_acb_iface *scx200_create_iface(const char *text,
  343. struct device *dev, int index)
  344. {
  345. struct scx200_acb_iface *iface;
  346. struct i2c_adapter *adapter;
  347. iface = kzalloc(sizeof(*iface), GFP_KERNEL);
  348. if (!iface)
  349. return NULL;
  350. adapter = &iface->adapter;
  351. i2c_set_adapdata(adapter, iface);
  352. snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
  353. adapter->owner = THIS_MODULE;
  354. adapter->algo = &scx200_acb_algorithm;
  355. adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  356. adapter->dev.parent = dev;
  357. mutex_init(&iface->mutex);
  358. return iface;
  359. }
  360. static int scx200_acb_create(struct scx200_acb_iface *iface)
  361. {
  362. struct i2c_adapter *adapter;
  363. int rc;
  364. adapter = &iface->adapter;
  365. rc = scx200_acb_probe(iface);
  366. if (rc) {
  367. pr_warn("probe failed\n");
  368. return rc;
  369. }
  370. scx200_acb_reset(iface);
  371. if (i2c_add_adapter(adapter) < 0) {
  372. pr_err("failed to register\n");
  373. return -ENODEV;
  374. }
  375. if (!adapter->dev.parent) {
  376. /* If there's no dev, we're tracking (ISA) ifaces manually */
  377. mutex_lock(&scx200_acb_list_mutex);
  378. iface->next = scx200_acb_list;
  379. scx200_acb_list = iface;
  380. mutex_unlock(&scx200_acb_list_mutex);
  381. }
  382. return 0;
  383. }
  384. static struct scx200_acb_iface *scx200_create_dev(const char *text,
  385. unsigned long base, int index, struct device *dev)
  386. {
  387. struct scx200_acb_iface *iface;
  388. int rc;
  389. iface = scx200_create_iface(text, dev, index);
  390. if (iface == NULL)
  391. return NULL;
  392. if (!request_region(base, 8, iface->adapter.name)) {
  393. pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
  394. goto errout_free;
  395. }
  396. iface->base = base;
  397. rc = scx200_acb_create(iface);
  398. if (rc == 0)
  399. return iface;
  400. release_region(base, 8);
  401. errout_free:
  402. kfree(iface);
  403. return NULL;
  404. }
  405. static int scx200_probe(struct platform_device *pdev)
  406. {
  407. struct scx200_acb_iface *iface;
  408. struct resource *res;
  409. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  410. if (!res) {
  411. dev_err(&pdev->dev, "can't fetch device resource info\n");
  412. return -ENODEV;
  413. }
  414. iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
  415. if (!iface)
  416. return -EIO;
  417. dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
  418. iface->adapter.name);
  419. platform_set_drvdata(pdev, iface);
  420. return 0;
  421. }
  422. static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
  423. {
  424. i2c_del_adapter(&iface->adapter);
  425. release_region(iface->base, 8);
  426. kfree(iface);
  427. }
  428. static int scx200_remove(struct platform_device *pdev)
  429. {
  430. struct scx200_acb_iface *iface;
  431. iface = platform_get_drvdata(pdev);
  432. scx200_cleanup_iface(iface);
  433. return 0;
  434. }
  435. static struct platform_driver scx200_pci_driver = {
  436. .driver = {
  437. .name = "cs5535-smb",
  438. },
  439. .probe = scx200_probe,
  440. .remove = scx200_remove,
  441. };
  442. static const struct pci_device_id scx200_isa[] = {
  443. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
  444. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
  445. { 0, }
  446. };
  447. static __init void scx200_scan_isa(void)
  448. {
  449. int i;
  450. if (!pci_dev_present(scx200_isa))
  451. return;
  452. for (i = 0; i < MAX_DEVICES; ++i) {
  453. if (base[i] == 0)
  454. continue;
  455. /* XXX: should we care about failures? */
  456. scx200_create_dev("SCx200", base[i], i, NULL);
  457. }
  458. }
  459. static int __init scx200_acb_init(void)
  460. {
  461. pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
  462. /* First scan for ISA-based devices */
  463. scx200_scan_isa(); /* XXX: should we care about errors? */
  464. /* If at least one bus was created, init must succeed */
  465. if (scx200_acb_list)
  466. return 0;
  467. /* No ISA devices; register the platform driver for PCI-based devices */
  468. return platform_driver_register(&scx200_pci_driver);
  469. }
  470. static void __exit scx200_acb_cleanup(void)
  471. {
  472. struct scx200_acb_iface *iface;
  473. platform_driver_unregister(&scx200_pci_driver);
  474. mutex_lock(&scx200_acb_list_mutex);
  475. while ((iface = scx200_acb_list) != NULL) {
  476. scx200_acb_list = iface->next;
  477. mutex_unlock(&scx200_acb_list_mutex);
  478. scx200_cleanup_iface(iface);
  479. mutex_lock(&scx200_acb_list_mutex);
  480. }
  481. mutex_unlock(&scx200_acb_list_mutex);
  482. }
  483. module_init(scx200_acb_init);
  484. module_exit(scx200_acb_cleanup);