i2c-sis630.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (c) 2002,2003 Alexander Malysh <[email protected]>
  4. */
  5. /*
  6. Status: beta
  7. Supports:
  8. SIS 630
  9. SIS 730
  10. SIS 964
  11. Notable differences between chips:
  12. +------------------------+--------------------+-------------------+
  13. | | SIS630/730 | SIS964 |
  14. +------------------------+--------------------+-------------------+
  15. | Clock | 14kHz/56kHz | 55.56kHz/27.78kHz |
  16. | SMBus registers offset | 0x80 | 0xE0 |
  17. | SMB_CNT | Bit 1 = Slave Busy | Bit 1 = Bus probe |
  18. | (not used yet) | Bit 3 is reserved | Bit 3 = Last byte |
  19. | SMB_PCOUNT | Offset + 0x06 | Offset + 0x14 |
  20. | SMB_COUNT | 4:0 bits | 5:0 bits |
  21. +------------------------+--------------------+-------------------+
  22. (Other differences don't affect the functions provided by the driver)
  23. Note: we assume there can only be one device, with one SMBus interface.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. #include <linux/pci.h>
  29. #include <linux/ioport.h>
  30. #include <linux/i2c.h>
  31. #include <linux/acpi.h>
  32. #include <linux/io.h>
  33. /* SIS964 id is defined here as we are the only file using it */
  34. #define PCI_DEVICE_ID_SI_964 0x0964
  35. /* SIS630/730/964 SMBus registers */
  36. #define SMB_STS 0x00 /* status */
  37. #define SMB_CNT 0x02 /* control */
  38. #define SMBHOST_CNT 0x03 /* host control */
  39. #define SMB_ADDR 0x04 /* address */
  40. #define SMB_CMD 0x05 /* command */
  41. #define SMB_COUNT 0x07 /* byte count */
  42. #define SMB_BYTE 0x08 /* ~0x8F data byte field */
  43. /* SMB_STS register */
  44. #define BYTE_DONE_STS 0x10 /* Byte Done Status / Block Array */
  45. #define SMBCOL_STS 0x04 /* Collision */
  46. #define SMBERR_STS 0x02 /* Device error */
  47. /* SMB_CNT register */
  48. #define MSTO_EN 0x40 /* Host Master Timeout Enable */
  49. #define SMBCLK_SEL 0x20 /* Host master clock selection */
  50. #define SMB_PROBE 0x02 /* Bus Probe/Slave busy */
  51. #define SMB_HOSTBUSY 0x01 /* Host Busy */
  52. /* SMBHOST_CNT register */
  53. #define SMB_KILL 0x20 /* Kill */
  54. #define SMB_START 0x10 /* Start */
  55. /* register count for request_region
  56. * As we don't use SMB_PCOUNT, 20 is ok for SiS630 and SiS964
  57. */
  58. #define SIS630_SMB_IOREGION 20
  59. /* PCI address constants */
  60. /* acpi base address register */
  61. #define SIS630_ACPI_BASE_REG 0x74
  62. /* bios control register */
  63. #define SIS630_BIOS_CTL_REG 0x40
  64. /* Other settings */
  65. #define MAX_TIMEOUT 500
  66. /* SIS630 constants */
  67. #define SIS630_QUICK 0x00
  68. #define SIS630_BYTE 0x01
  69. #define SIS630_BYTE_DATA 0x02
  70. #define SIS630_WORD_DATA 0x03
  71. #define SIS630_PCALL 0x04
  72. #define SIS630_BLOCK_DATA 0x05
  73. static struct pci_driver sis630_driver;
  74. /* insmod parameters */
  75. static bool high_clock;
  76. static bool force;
  77. module_param(high_clock, bool, 0);
  78. MODULE_PARM_DESC(high_clock,
  79. "Set Host Master Clock to 56KHz (default 14KHz) (SIS630/730 only).");
  80. module_param(force, bool, 0);
  81. MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
  82. /* SMBus base address */
  83. static unsigned short smbus_base;
  84. /* supported chips */
  85. static int supported[] = {
  86. PCI_DEVICE_ID_SI_630,
  87. PCI_DEVICE_ID_SI_730,
  88. PCI_DEVICE_ID_SI_760,
  89. 0 /* terminates the list */
  90. };
  91. static inline u8 sis630_read(u8 reg)
  92. {
  93. return inb(smbus_base + reg);
  94. }
  95. static inline void sis630_write(u8 reg, u8 data)
  96. {
  97. outb(data, smbus_base + reg);
  98. }
  99. static int sis630_transaction_start(struct i2c_adapter *adap, int size,
  100. u8 *oldclock)
  101. {
  102. int temp;
  103. /* Make sure the SMBus host is ready to start transmitting. */
  104. temp = sis630_read(SMB_CNT);
  105. if ((temp & (SMB_PROBE | SMB_HOSTBUSY)) != 0x00) {
  106. dev_dbg(&adap->dev, "SMBus busy (%02x). Resetting...\n", temp);
  107. /* kill smbus transaction */
  108. sis630_write(SMBHOST_CNT, SMB_KILL);
  109. temp = sis630_read(SMB_CNT);
  110. if (temp & (SMB_PROBE | SMB_HOSTBUSY)) {
  111. dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
  112. return -EBUSY;
  113. } else {
  114. dev_dbg(&adap->dev, "Successful!\n");
  115. }
  116. }
  117. /* save old clock, so we can prevent machine for hung */
  118. *oldclock = sis630_read(SMB_CNT);
  119. dev_dbg(&adap->dev, "saved clock 0x%02x\n", *oldclock);
  120. /* disable timeout interrupt,
  121. * set Host Master Clock to 56KHz if requested */
  122. if (high_clock)
  123. sis630_write(SMB_CNT, SMBCLK_SEL);
  124. else
  125. sis630_write(SMB_CNT, (*oldclock & ~MSTO_EN));
  126. /* clear all sticky bits */
  127. temp = sis630_read(SMB_STS);
  128. sis630_write(SMB_STS, temp & 0x1e);
  129. /* start the transaction by setting bit 4 and size */
  130. sis630_write(SMBHOST_CNT, SMB_START | (size & 0x07));
  131. return 0;
  132. }
  133. static int sis630_transaction_wait(struct i2c_adapter *adap, int size)
  134. {
  135. int temp, result = 0, timeout = 0;
  136. /* We will always wait for a fraction of a second! */
  137. do {
  138. msleep(1);
  139. temp = sis630_read(SMB_STS);
  140. /* check if block transmitted */
  141. if (size == SIS630_BLOCK_DATA && (temp & BYTE_DONE_STS))
  142. break;
  143. } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
  144. /* If the SMBus is still busy, we give up */
  145. if (timeout > MAX_TIMEOUT) {
  146. dev_dbg(&adap->dev, "SMBus Timeout!\n");
  147. result = -ETIMEDOUT;
  148. }
  149. if (temp & SMBERR_STS) {
  150. dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
  151. result = -ENXIO;
  152. }
  153. if (temp & SMBCOL_STS) {
  154. dev_err(&adap->dev, "Bus collision!\n");
  155. result = -EAGAIN;
  156. }
  157. return result;
  158. }
  159. static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock)
  160. {
  161. /* clear all status "sticky" bits */
  162. sis630_write(SMB_STS, 0xFF);
  163. dev_dbg(&adap->dev,
  164. "SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT));
  165. /*
  166. * restore old Host Master Clock if high_clock is set
  167. * and oldclock was not 56KHz
  168. */
  169. if (high_clock && !(oldclock & SMBCLK_SEL))
  170. sis630_write(SMB_CNT, sis630_read(SMB_CNT) & ~SMBCLK_SEL);
  171. dev_dbg(&adap->dev,
  172. "SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT));
  173. }
  174. static int sis630_transaction(struct i2c_adapter *adap, int size)
  175. {
  176. int result = 0;
  177. u8 oldclock = 0;
  178. result = sis630_transaction_start(adap, size, &oldclock);
  179. if (!result) {
  180. result = sis630_transaction_wait(adap, size);
  181. sis630_transaction_end(adap, oldclock);
  182. }
  183. return result;
  184. }
  185. static int sis630_block_data(struct i2c_adapter *adap,
  186. union i2c_smbus_data *data, int read_write)
  187. {
  188. int i, len = 0, rc = 0;
  189. u8 oldclock = 0;
  190. if (read_write == I2C_SMBUS_WRITE) {
  191. len = data->block[0];
  192. if (len < 0)
  193. len = 0;
  194. else if (len > 32)
  195. len = 32;
  196. sis630_write(SMB_COUNT, len);
  197. for (i = 1; i <= len; i++) {
  198. dev_dbg(&adap->dev,
  199. "set data 0x%02x\n", data->block[i]);
  200. /* set data */
  201. sis630_write(SMB_BYTE + (i - 1) % 8, data->block[i]);
  202. if (i == 8 || (len < 8 && i == len)) {
  203. dev_dbg(&adap->dev,
  204. "start trans len=%d i=%d\n", len, i);
  205. /* first transaction */
  206. rc = sis630_transaction_start(adap,
  207. SIS630_BLOCK_DATA, &oldclock);
  208. if (rc)
  209. return rc;
  210. } else if ((i - 1) % 8 == 7 || i == len) {
  211. dev_dbg(&adap->dev,
  212. "trans_wait len=%d i=%d\n", len, i);
  213. if (i > 8) {
  214. dev_dbg(&adap->dev,
  215. "clear smbary_sts"
  216. " len=%d i=%d\n", len, i);
  217. /*
  218. If this is not first transaction,
  219. we must clear sticky bit.
  220. clear SMBARY_STS
  221. */
  222. sis630_write(SMB_STS, BYTE_DONE_STS);
  223. }
  224. rc = sis630_transaction_wait(adap,
  225. SIS630_BLOCK_DATA);
  226. if (rc) {
  227. dev_dbg(&adap->dev,
  228. "trans_wait failed\n");
  229. break;
  230. }
  231. }
  232. }
  233. } else {
  234. /* read request */
  235. data->block[0] = len = 0;
  236. rc = sis630_transaction_start(adap,
  237. SIS630_BLOCK_DATA, &oldclock);
  238. if (rc)
  239. return rc;
  240. do {
  241. rc = sis630_transaction_wait(adap, SIS630_BLOCK_DATA);
  242. if (rc) {
  243. dev_dbg(&adap->dev, "trans_wait failed\n");
  244. break;
  245. }
  246. /* if this first transaction then read byte count */
  247. if (len == 0)
  248. data->block[0] = sis630_read(SMB_COUNT);
  249. /* just to be sure */
  250. if (data->block[0] > 32)
  251. data->block[0] = 32;
  252. dev_dbg(&adap->dev,
  253. "block data read len=0x%x\n", data->block[0]);
  254. for (i = 0; i < 8 && len < data->block[0]; i++, len++) {
  255. dev_dbg(&adap->dev,
  256. "read i=%d len=%d\n", i, len);
  257. data->block[len + 1] = sis630_read(SMB_BYTE +
  258. i);
  259. }
  260. dev_dbg(&adap->dev,
  261. "clear smbary_sts len=%d i=%d\n", len, i);
  262. /* clear SMBARY_STS */
  263. sis630_write(SMB_STS, BYTE_DONE_STS);
  264. } while (len < data->block[0]);
  265. }
  266. sis630_transaction_end(adap, oldclock);
  267. return rc;
  268. }
  269. /* Return negative errno on error. */
  270. static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
  271. unsigned short flags, char read_write,
  272. u8 command, int size, union i2c_smbus_data *data)
  273. {
  274. int status;
  275. switch (size) {
  276. case I2C_SMBUS_QUICK:
  277. sis630_write(SMB_ADDR,
  278. ((addr & 0x7f) << 1) | (read_write & 0x01));
  279. size = SIS630_QUICK;
  280. break;
  281. case I2C_SMBUS_BYTE:
  282. sis630_write(SMB_ADDR,
  283. ((addr & 0x7f) << 1) | (read_write & 0x01));
  284. if (read_write == I2C_SMBUS_WRITE)
  285. sis630_write(SMB_CMD, command);
  286. size = SIS630_BYTE;
  287. break;
  288. case I2C_SMBUS_BYTE_DATA:
  289. sis630_write(SMB_ADDR,
  290. ((addr & 0x7f) << 1) | (read_write & 0x01));
  291. sis630_write(SMB_CMD, command);
  292. if (read_write == I2C_SMBUS_WRITE)
  293. sis630_write(SMB_BYTE, data->byte);
  294. size = SIS630_BYTE_DATA;
  295. break;
  296. case I2C_SMBUS_PROC_CALL:
  297. case I2C_SMBUS_WORD_DATA:
  298. sis630_write(SMB_ADDR,
  299. ((addr & 0x7f) << 1) | (read_write & 0x01));
  300. sis630_write(SMB_CMD, command);
  301. if (read_write == I2C_SMBUS_WRITE) {
  302. sis630_write(SMB_BYTE, data->word & 0xff);
  303. sis630_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
  304. }
  305. size = (size == I2C_SMBUS_PROC_CALL ?
  306. SIS630_PCALL : SIS630_WORD_DATA);
  307. break;
  308. case I2C_SMBUS_BLOCK_DATA:
  309. sis630_write(SMB_ADDR,
  310. ((addr & 0x7f) << 1) | (read_write & 0x01));
  311. sis630_write(SMB_CMD, command);
  312. size = SIS630_BLOCK_DATA;
  313. return sis630_block_data(adap, data, read_write);
  314. default:
  315. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  316. return -EOPNOTSUPP;
  317. }
  318. status = sis630_transaction(adap, size);
  319. if (status)
  320. return status;
  321. if ((size != SIS630_PCALL) &&
  322. ((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) {
  323. return 0;
  324. }
  325. switch (size) {
  326. case SIS630_BYTE:
  327. case SIS630_BYTE_DATA:
  328. data->byte = sis630_read(SMB_BYTE);
  329. break;
  330. case SIS630_PCALL:
  331. case SIS630_WORD_DATA:
  332. data->word = sis630_read(SMB_BYTE) +
  333. (sis630_read(SMB_BYTE + 1) << 8);
  334. break;
  335. }
  336. return 0;
  337. }
  338. static u32 sis630_func(struct i2c_adapter *adapter)
  339. {
  340. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  341. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  342. I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
  343. }
  344. static int sis630_setup(struct pci_dev *sis630_dev)
  345. {
  346. unsigned char b;
  347. struct pci_dev *dummy = NULL;
  348. int retval, i;
  349. /* acpi base address */
  350. unsigned short acpi_base;
  351. /* check for supported SiS devices */
  352. for (i = 0; supported[i] > 0; i++) {
  353. dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy);
  354. if (dummy)
  355. break; /* found */
  356. }
  357. if (dummy) {
  358. pci_dev_put(dummy);
  359. } else if (force) {
  360. dev_err(&sis630_dev->dev,
  361. "WARNING: Can't detect SIS630 compatible device, but "
  362. "loading because of force option enabled\n");
  363. } else {
  364. return -ENODEV;
  365. }
  366. /*
  367. Enable ACPI first , so we can accsess reg 74-75
  368. in acpi io space and read acpi base addr
  369. */
  370. if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, &b)) {
  371. dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n");
  372. retval = -ENODEV;
  373. goto exit;
  374. }
  375. /* if ACPI already enabled , do nothing */
  376. if (!(b & 0x80) &&
  377. pci_write_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, b | 0x80)) {
  378. dev_err(&sis630_dev->dev, "Error: Can't enable ACPI\n");
  379. retval = -ENODEV;
  380. goto exit;
  381. }
  382. /* Determine the ACPI base address */
  383. if (pci_read_config_word(sis630_dev,
  384. SIS630_ACPI_BASE_REG, &acpi_base)) {
  385. dev_err(&sis630_dev->dev,
  386. "Error: Can't determine ACPI base address\n");
  387. retval = -ENODEV;
  388. goto exit;
  389. }
  390. dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04hx\n", acpi_base);
  391. if (supported[i] == PCI_DEVICE_ID_SI_760)
  392. smbus_base = acpi_base + 0xE0;
  393. else
  394. smbus_base = acpi_base + 0x80;
  395. dev_dbg(&sis630_dev->dev, "SMBus base at 0x%04hx\n", smbus_base);
  396. retval = acpi_check_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
  397. sis630_driver.name);
  398. if (retval)
  399. goto exit;
  400. /* Everything is happy, let's grab the memory and set things up. */
  401. if (!request_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
  402. sis630_driver.name)) {
  403. dev_err(&sis630_dev->dev,
  404. "I/O Region 0x%04x-0x%04x for SMBus already in use.\n",
  405. smbus_base + SMB_STS,
  406. smbus_base + SMB_STS + SIS630_SMB_IOREGION - 1);
  407. retval = -EBUSY;
  408. goto exit;
  409. }
  410. retval = 0;
  411. exit:
  412. if (retval)
  413. smbus_base = 0;
  414. return retval;
  415. }
  416. static const struct i2c_algorithm smbus_algorithm = {
  417. .smbus_xfer = sis630_access,
  418. .functionality = sis630_func,
  419. };
  420. static struct i2c_adapter sis630_adapter = {
  421. .owner = THIS_MODULE,
  422. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  423. .algo = &smbus_algorithm,
  424. .retries = 3
  425. };
  426. static const struct pci_device_id sis630_ids[] = {
  427. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  428. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) },
  429. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_964) },
  430. { 0, }
  431. };
  432. MODULE_DEVICE_TABLE(pci, sis630_ids);
  433. static int sis630_probe(struct pci_dev *dev, const struct pci_device_id *id)
  434. {
  435. if (sis630_setup(dev)) {
  436. dev_err(&dev->dev,
  437. "SIS630 compatible bus not detected, "
  438. "module not inserted.\n");
  439. return -ENODEV;
  440. }
  441. /* set up the sysfs linkage to our parent device */
  442. sis630_adapter.dev.parent = &dev->dev;
  443. snprintf(sis630_adapter.name, sizeof(sis630_adapter.name),
  444. "SMBus SIS630 adapter at %04x", smbus_base + SMB_STS);
  445. return i2c_add_adapter(&sis630_adapter);
  446. }
  447. static void sis630_remove(struct pci_dev *dev)
  448. {
  449. if (smbus_base) {
  450. i2c_del_adapter(&sis630_adapter);
  451. release_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION);
  452. smbus_base = 0;
  453. }
  454. }
  455. static struct pci_driver sis630_driver = {
  456. .name = "sis630_smbus",
  457. .id_table = sis630_ids,
  458. .probe = sis630_probe,
  459. .remove = sis630_remove,
  460. };
  461. module_pci_driver(sis630_driver);
  462. MODULE_LICENSE("GPL");
  463. MODULE_AUTHOR("Alexander Malysh <[email protected]>");
  464. MODULE_DESCRIPTION("SIS630 SMBus driver");