i2c-sis630.c 15 KB

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