scx200_acb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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/delay.h>
  27. #include <linux/mutex.h>
  28. #include <asm/io.h>
  29. #include <linux/scx200.h>
  30. #define NAME "scx200_acb"
  31. MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
  32. MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
  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. /* PCI device info */
  72. struct pci_dev *pdev;
  73. int bar;
  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, "%s in state %s\n", errmsg,
  179. scx200_acb_state_name[iface->state]);
  180. iface->state = state_idle;
  181. iface->result = -EIO;
  182. iface->needs_reset = 1;
  183. }
  184. static void scx200_acb_poll(struct scx200_acb_iface *iface)
  185. {
  186. u8 status;
  187. unsigned long timeout;
  188. timeout = jiffies + POLL_TIMEOUT;
  189. while (1) {
  190. status = inb(ACBST);
  191. /* Reset the status register to avoid the hang */
  192. outb(0, ACBST);
  193. if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
  194. scx200_acb_machine(iface, status);
  195. return;
  196. }
  197. if (time_after(jiffies, timeout))
  198. break;
  199. cpu_relax();
  200. cond_resched();
  201. }
  202. dev_err(&iface->adapter.dev, "timeout in state %s\n",
  203. scx200_acb_state_name[iface->state]);
  204. iface->state = state_idle;
  205. iface->result = -EIO;
  206. iface->needs_reset = 1;
  207. }
  208. static void scx200_acb_reset(struct scx200_acb_iface *iface)
  209. {
  210. /* Disable the ACCESS.bus device and Configure the SCL
  211. frequency: 16 clock cycles */
  212. outb(0x70, ACBCTL2);
  213. /* Polling mode */
  214. outb(0, ACBCTL1);
  215. /* Disable slave address */
  216. outb(0, ACBADDR);
  217. /* Enable the ACCESS.bus device */
  218. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  219. /* Free STALL after START */
  220. outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
  221. /* Send a STOP */
  222. outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
  223. /* Clear BER, NEGACK and STASTR bits */
  224. outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
  225. /* Clear BB bit */
  226. outb(inb(ACBCST) | ACBCST_BB, ACBCST);
  227. }
  228. static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
  229. u16 address, unsigned short flags,
  230. char rw, u8 command, int size,
  231. union i2c_smbus_data *data)
  232. {
  233. struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
  234. int len;
  235. u8 *buffer;
  236. u16 cur_word;
  237. int rc;
  238. switch (size) {
  239. case I2C_SMBUS_QUICK:
  240. len = 0;
  241. buffer = NULL;
  242. break;
  243. case I2C_SMBUS_BYTE:
  244. len = 1;
  245. buffer = rw ? &data->byte : &command;
  246. break;
  247. case I2C_SMBUS_BYTE_DATA:
  248. len = 1;
  249. buffer = &data->byte;
  250. break;
  251. case I2C_SMBUS_WORD_DATA:
  252. len = 2;
  253. cur_word = cpu_to_le16(data->word);
  254. buffer = (u8 *)&cur_word;
  255. break;
  256. case I2C_SMBUS_I2C_BLOCK_DATA:
  257. len = data->block[0];
  258. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  259. return -EINVAL;
  260. buffer = &data->block[1];
  261. break;
  262. default:
  263. return -EINVAL;
  264. }
  265. dev_dbg(&adapter->dev,
  266. "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
  267. size, address, command, len, rw);
  268. if (!len && rw == I2C_SMBUS_READ) {
  269. dev_dbg(&adapter->dev, "zero length read\n");
  270. return -EINVAL;
  271. }
  272. mutex_lock(&iface->mutex);
  273. iface->address_byte = (address << 1) | rw;
  274. iface->command = command;
  275. iface->ptr = buffer;
  276. iface->len = len;
  277. iface->result = -EINVAL;
  278. iface->needs_reset = 0;
  279. outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
  280. if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
  281. iface->state = state_quick;
  282. else
  283. iface->state = state_address;
  284. while (iface->state != state_idle)
  285. scx200_acb_poll(iface);
  286. if (iface->needs_reset)
  287. scx200_acb_reset(iface);
  288. rc = iface->result;
  289. mutex_unlock(&iface->mutex);
  290. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
  291. data->word = le16_to_cpu(cur_word);
  292. #ifdef DEBUG
  293. dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
  294. if (buffer) {
  295. int i;
  296. printk(" data:");
  297. for (i = 0; i < len; ++i)
  298. printk(" %02x", buffer[i]);
  299. }
  300. printk("\n");
  301. #endif
  302. return rc;
  303. }
  304. static u32 scx200_acb_func(struct i2c_adapter *adapter)
  305. {
  306. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  307. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  308. I2C_FUNC_SMBUS_I2C_BLOCK;
  309. }
  310. /* For now, we only handle combined mode (smbus) */
  311. static const struct i2c_algorithm scx200_acb_algorithm = {
  312. .smbus_xfer = scx200_acb_smbus_xfer,
  313. .functionality = scx200_acb_func,
  314. };
  315. static struct scx200_acb_iface *scx200_acb_list;
  316. static DEFINE_MUTEX(scx200_acb_list_mutex);
  317. static __init int scx200_acb_probe(struct scx200_acb_iface *iface)
  318. {
  319. u8 val;
  320. /* Disable the ACCESS.bus device and Configure the SCL
  321. frequency: 16 clock cycles */
  322. outb(0x70, ACBCTL2);
  323. if (inb(ACBCTL2) != 0x70) {
  324. pr_debug(NAME ": ACBCTL2 readback failed\n");
  325. return -ENXIO;
  326. }
  327. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  328. val = inb(ACBCTL1);
  329. if (val) {
  330. pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
  331. val);
  332. return -ENXIO;
  333. }
  334. outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
  335. outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
  336. val = inb(ACBCTL1);
  337. if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
  338. pr_debug(NAME ": enabled, but NMINTE won't be set, "
  339. "ACBCTL1=0x%02x\n", val);
  340. return -ENXIO;
  341. }
  342. return 0;
  343. }
  344. static __init struct scx200_acb_iface *scx200_create_iface(const char *text,
  345. struct device *dev, int index)
  346. {
  347. struct scx200_acb_iface *iface;
  348. struct i2c_adapter *adapter;
  349. iface = kzalloc(sizeof(*iface), GFP_KERNEL);
  350. if (!iface) {
  351. printk(KERN_ERR NAME ": can't allocate memory\n");
  352. return NULL;
  353. }
  354. adapter = &iface->adapter;
  355. i2c_set_adapdata(adapter, iface);
  356. snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
  357. adapter->owner = THIS_MODULE;
  358. adapter->id = I2C_HW_SMBUS_SCX200;
  359. adapter->algo = &scx200_acb_algorithm;
  360. adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  361. adapter->dev.parent = dev;
  362. mutex_init(&iface->mutex);
  363. return iface;
  364. }
  365. static int __init scx200_acb_create(struct scx200_acb_iface *iface)
  366. {
  367. struct i2c_adapter *adapter;
  368. int rc;
  369. adapter = &iface->adapter;
  370. rc = scx200_acb_probe(iface);
  371. if (rc) {
  372. printk(KERN_WARNING NAME ": probe failed\n");
  373. return rc;
  374. }
  375. scx200_acb_reset(iface);
  376. if (i2c_add_adapter(adapter) < 0) {
  377. printk(KERN_ERR NAME ": failed to register\n");
  378. return -ENODEV;
  379. }
  380. mutex_lock(&scx200_acb_list_mutex);
  381. iface->next = scx200_acb_list;
  382. scx200_acb_list = iface;
  383. mutex_unlock(&scx200_acb_list_mutex);
  384. return 0;
  385. }
  386. static __init int scx200_create_pci(const char *text, struct pci_dev *pdev,
  387. int bar)
  388. {
  389. struct scx200_acb_iface *iface;
  390. int rc;
  391. iface = scx200_create_iface(text, &pdev->dev, 0);
  392. if (iface == NULL)
  393. return -ENOMEM;
  394. iface->pdev = pdev;
  395. iface->bar = bar;
  396. rc = pci_enable_device_io(iface->pdev);
  397. if (rc)
  398. goto errout_free;
  399. rc = pci_request_region(iface->pdev, iface->bar, iface->adapter.name);
  400. if (rc) {
  401. printk(KERN_ERR NAME ": can't allocate PCI BAR %d\n",
  402. iface->bar);
  403. goto errout_free;
  404. }
  405. iface->base = pci_resource_start(iface->pdev, iface->bar);
  406. rc = scx200_acb_create(iface);
  407. if (rc == 0)
  408. return 0;
  409. pci_release_region(iface->pdev, iface->bar);
  410. pci_dev_put(iface->pdev);
  411. errout_free:
  412. kfree(iface);
  413. return rc;
  414. }
  415. static int __init scx200_create_isa(const char *text, unsigned long base,
  416. int index)
  417. {
  418. struct scx200_acb_iface *iface;
  419. int rc;
  420. iface = scx200_create_iface(text, NULL, index);
  421. if (iface == NULL)
  422. return -ENOMEM;
  423. if (!request_region(base, 8, iface->adapter.name)) {
  424. printk(KERN_ERR NAME ": can't allocate io 0x%lx-0x%lx\n",
  425. base, base + 8 - 1);
  426. rc = -EBUSY;
  427. goto errout_free;
  428. }
  429. iface->base = base;
  430. rc = scx200_acb_create(iface);
  431. if (rc == 0)
  432. return 0;
  433. release_region(base, 8);
  434. errout_free:
  435. kfree(iface);
  436. return rc;
  437. }
  438. /* Driver data is an index into the scx200_data array that indicates
  439. * the name and the BAR where the I/O address resource is located. ISA
  440. * devices are flagged with a bar value of -1 */
  441. static struct pci_device_id scx200_pci[] = {
  442. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE),
  443. .driver_data = 0 },
  444. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE),
  445. .driver_data = 0 },
  446. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA),
  447. .driver_data = 1 },
  448. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA),
  449. .driver_data = 2 }
  450. };
  451. static struct {
  452. const char *name;
  453. int bar;
  454. } scx200_data[] = {
  455. { "SCx200", -1 },
  456. { "CS5535", 0 },
  457. { "CS5536", 0 }
  458. };
  459. static __init int scx200_scan_pci(void)
  460. {
  461. int data, dev;
  462. int rc = -ENODEV;
  463. struct pci_dev *pdev;
  464. for(dev = 0; dev < ARRAY_SIZE(scx200_pci); dev++) {
  465. pdev = pci_get_device(scx200_pci[dev].vendor,
  466. scx200_pci[dev].device, NULL);
  467. if (pdev == NULL)
  468. continue;
  469. data = scx200_pci[dev].driver_data;
  470. /* if .bar is greater or equal to zero, this is a
  471. * PCI device - otherwise, we assume
  472. that the ports are ISA based
  473. */
  474. if (scx200_data[data].bar >= 0)
  475. rc = scx200_create_pci(scx200_data[data].name, pdev,
  476. scx200_data[data].bar);
  477. else {
  478. int i;
  479. pci_dev_put(pdev);
  480. for (i = 0; i < MAX_DEVICES; ++i) {
  481. if (base[i] == 0)
  482. continue;
  483. rc = scx200_create_isa(scx200_data[data].name,
  484. base[i],
  485. i);
  486. }
  487. }
  488. break;
  489. }
  490. return rc;
  491. }
  492. static int __init scx200_acb_init(void)
  493. {
  494. int rc;
  495. pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
  496. rc = scx200_scan_pci();
  497. /* If at least one bus was created, init must succeed */
  498. if (scx200_acb_list)
  499. return 0;
  500. return rc;
  501. }
  502. static void __exit scx200_acb_cleanup(void)
  503. {
  504. struct scx200_acb_iface *iface;
  505. mutex_lock(&scx200_acb_list_mutex);
  506. while ((iface = scx200_acb_list) != NULL) {
  507. scx200_acb_list = iface->next;
  508. mutex_unlock(&scx200_acb_list_mutex);
  509. i2c_del_adapter(&iface->adapter);
  510. if (iface->pdev) {
  511. pci_release_region(iface->pdev, iface->bar);
  512. pci_dev_put(iface->pdev);
  513. }
  514. else
  515. release_region(iface->base, 8);
  516. kfree(iface);
  517. mutex_lock(&scx200_acb_list_mutex);
  518. }
  519. mutex_unlock(&scx200_acb_list_mutex);
  520. }
  521. module_init(scx200_acb_init);
  522. module_exit(scx200_acb_cleanup);