scx200_acb.c 14 KB

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