scx200_acb.c 14 KB

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