scx200_acb.c 13 KB

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