i2c-ali1535.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
  3. * Philip Edelbrock <phil@netroedge.com>,
  4. * Mark D. Studebaker <mdsxyz123@yahoo.com>,
  5. * Dan Eaton <dan.eaton@rocketlogix.com> and
  6. * Stephen Rousset <stephen.rousset@rocketlogix.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. /*
  19. This is the driver for the SMB Host controller on
  20. Acer Labs Inc. (ALI) M1535 South Bridge.
  21. The M1535 is a South bridge for portable systems.
  22. It is very similar to the M15x3 South bridges also produced
  23. by Acer Labs Inc. Some of the registers within the part
  24. have moved and some have been redefined slightly. Additionally,
  25. the sequencing of the SMBus transactions has been modified
  26. to be more consistent with the sequencing recommended by
  27. the manufacturer and observed through testing. These
  28. changes are reflected in this driver and can be identified
  29. by comparing this driver to the i2c-ali15x3 driver.
  30. For an overview of these chips see http://www.acerlabs.com
  31. The SMB controller is part of the 7101 device, which is an
  32. ACPI-compliant Power Management Unit (PMU).
  33. The whole 7101 device has to be enabled for the SMB to work.
  34. You can't just enable the SMB alone.
  35. The SMB and the ACPI have separate I/O spaces.
  36. We make sure that the SMB is enabled. We leave the ACPI alone.
  37. This driver controls the SMB Host only.
  38. This driver does not use interrupts.
  39. */
  40. /* Note: we assume there can only be one ALI1535, with one SMBus interface */
  41. #include <linux/module.h>
  42. #include <linux/pci.h>
  43. #include <linux/kernel.h>
  44. #include <linux/stddef.h>
  45. #include <linux/delay.h>
  46. #include <linux/ioport.h>
  47. #include <linux/i2c.h>
  48. #include <linux/acpi.h>
  49. #include <linux/io.h>
  50. /* ALI1535 SMBus address offsets */
  51. #define SMBHSTSTS (0 + ali1535_smba)
  52. #define SMBHSTTYP (1 + ali1535_smba)
  53. #define SMBHSTPORT (2 + ali1535_smba)
  54. #define SMBHSTCMD (7 + ali1535_smba)
  55. #define SMBHSTADD (3 + ali1535_smba)
  56. #define SMBHSTDAT0 (4 + ali1535_smba)
  57. #define SMBHSTDAT1 (5 + ali1535_smba)
  58. #define SMBBLKDAT (6 + ali1535_smba)
  59. /* PCI Address Constants */
  60. #define SMBCOM 0x004
  61. #define SMBREV 0x008
  62. #define SMBCFG 0x0D1
  63. #define SMBBA 0x0E2
  64. #define SMBHSTCFG 0x0F0
  65. #define SMBCLK 0x0F2
  66. /* Other settings */
  67. #define MAX_TIMEOUT 500 /* times 1/100 sec */
  68. #define ALI1535_SMB_IOSIZE 32
  69. #define ALI1535_SMB_DEFAULTBASE 0x8040
  70. /* ALI1535 address lock bits */
  71. #define ALI1535_LOCK 0x06 /* dwe */
  72. /* ALI1535 command constants */
  73. #define ALI1535_QUICK 0x00
  74. #define ALI1535_BYTE 0x10
  75. #define ALI1535_BYTE_DATA 0x20
  76. #define ALI1535_WORD_DATA 0x30
  77. #define ALI1535_BLOCK_DATA 0x40
  78. #define ALI1535_I2C_READ 0x60
  79. #define ALI1535_DEV10B_EN 0x80 /* Enable 10-bit addressing in */
  80. /* I2C read */
  81. #define ALI1535_T_OUT 0x08 /* Time-out Command (write) */
  82. #define ALI1535_A_HIGH_BIT9 0x08 /* Bit 9 of 10-bit address in */
  83. /* Alert-Response-Address */
  84. /* (read) */
  85. #define ALI1535_KILL 0x04 /* Kill Command (write) */
  86. #define ALI1535_A_HIGH_BIT8 0x04 /* Bit 8 of 10-bit address in */
  87. /* Alert-Response-Address */
  88. /* (read) */
  89. #define ALI1535_D_HI_MASK 0x03 /* Mask for isolating bits 9-8 */
  90. /* of 10-bit address in I2C */
  91. /* Read Command */
  92. /* ALI1535 status register bits */
  93. #define ALI1535_STS_IDLE 0x04
  94. #define ALI1535_STS_BUSY 0x08 /* host busy */
  95. #define ALI1535_STS_DONE 0x10 /* transaction complete */
  96. #define ALI1535_STS_DEV 0x20 /* device error */
  97. #define ALI1535_STS_BUSERR 0x40 /* bus error */
  98. #define ALI1535_STS_FAIL 0x80 /* failed bus transaction */
  99. #define ALI1535_STS_ERR 0xE0 /* all the bad error bits */
  100. #define ALI1535_BLOCK_CLR 0x04 /* reset block data index */
  101. /* ALI1535 device address register bits */
  102. #define ALI1535_RD_ADDR 0x01 /* Read/Write Bit in Device */
  103. /* Address field */
  104. /* -> Write = 0 */
  105. /* -> Read = 1 */
  106. #define ALI1535_SMBIO_EN 0x04 /* SMB I/O Space enable */
  107. static struct pci_driver ali1535_driver;
  108. static unsigned long ali1535_smba;
  109. static unsigned short ali1535_offset;
  110. /* Detect whether a ALI1535 can be found, and initialize it, where necessary.
  111. Note the differences between kernels with the old PCI BIOS interface and
  112. newer kernels with the real PCI interface. In compat.h some things are
  113. defined to make the transition easier. */
  114. static int ali1535_setup(struct pci_dev *dev)
  115. {
  116. int retval;
  117. unsigned char temp;
  118. /* Check the following things:
  119. - SMB I/O address is initialized
  120. - Device is enabled
  121. - We can use the addresses
  122. */
  123. retval = pci_enable_device(dev);
  124. if (retval) {
  125. dev_err(&dev->dev, "ALI1535_smb can't enable device\n");
  126. goto exit;
  127. }
  128. /* Determine the address of the SMBus area */
  129. pci_read_config_word(dev, SMBBA, &ali1535_offset);
  130. dev_dbg(&dev->dev, "ALI1535_smb is at offset 0x%04x\n", ali1535_offset);
  131. ali1535_offset &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
  132. if (ali1535_offset == 0) {
  133. dev_warn(&dev->dev,
  134. "ALI1535_smb region uninitialized - upgrade BIOS?\n");
  135. retval = -ENODEV;
  136. goto exit;
  137. }
  138. if (pci_resource_flags(dev, 0) & IORESOURCE_IO)
  139. ali1535_smba = pci_resource_start(dev, 0) + ali1535_offset;
  140. else
  141. ali1535_smba = ali1535_offset;
  142. retval = acpi_check_region(ali1535_smba, ALI1535_SMB_IOSIZE,
  143. ali1535_driver.name);
  144. if (retval)
  145. goto exit;
  146. if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,
  147. ali1535_driver.name)) {
  148. dev_err(&dev->dev, "ALI1535_smb region 0x%lx already in use!\n",
  149. ali1535_smba);
  150. retval = -EBUSY;
  151. goto exit;
  152. }
  153. /* check if whole device is enabled */
  154. pci_read_config_byte(dev, SMBCFG, &temp);
  155. if ((temp & ALI1535_SMBIO_EN) == 0) {
  156. dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
  157. retval = -ENODEV;
  158. goto exit_free;
  159. }
  160. /* Is SMB Host controller enabled? */
  161. pci_read_config_byte(dev, SMBHSTCFG, &temp);
  162. if ((temp & 1) == 0) {
  163. dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
  164. retval = -ENODEV;
  165. goto exit_free;
  166. }
  167. /* set SMB clock to 74KHz as recommended in data sheet */
  168. pci_write_config_byte(dev, SMBCLK, 0x20);
  169. /*
  170. The interrupt routing for SMB is set up in register 0x77 in the
  171. 1533 ISA Bridge device, NOT in the 7101 device.
  172. Don't bother with finding the 1533 device and reading the register.
  173. if ((....... & 0x0F) == 1)
  174. dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
  175. */
  176. pci_read_config_byte(dev, SMBREV, &temp);
  177. dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
  178. dev_dbg(&dev->dev, "ALI1535_smba = 0x%lx\n", ali1535_smba);
  179. return 0;
  180. exit_free:
  181. release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
  182. exit:
  183. return retval;
  184. }
  185. static int ali1535_transaction(struct i2c_adapter *adap)
  186. {
  187. int temp;
  188. int result = 0;
  189. int timeout = 0;
  190. dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
  191. "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  192. inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
  193. inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
  194. /* get status */
  195. temp = inb_p(SMBHSTSTS);
  196. /* Make sure the SMBus host is ready to start transmitting */
  197. /* Check the busy bit first */
  198. if (temp & ALI1535_STS_BUSY) {
  199. /* If the host controller is still busy, it may have timed out
  200. * in the previous transaction, resulting in a "SMBus Timeout"
  201. * printk. I've tried the following to reset a stuck busy bit.
  202. * 1. Reset the controller with an KILL command. (this
  203. * doesn't seem to clear the controller if an external
  204. * device is hung)
  205. * 2. Reset the controller and the other SMBus devices with a
  206. * T_OUT command. (this clears the host busy bit if an
  207. * external device is hung, but it comes back upon a new
  208. * access to a device)
  209. * 3. Disable and reenable the controller in SMBHSTCFG. Worst
  210. * case, nothing seems to work except power reset.
  211. */
  212. /* Try resetting entire SMB bus, including other devices - This
  213. * may not work either - it clears the BUSY bit but then the
  214. * BUSY bit may come back on when you try and use the chip
  215. * again. If that's the case you are stuck.
  216. */
  217. dev_info(&adap->dev,
  218. "Resetting entire SMB Bus to clear busy condition (%02x)\n",
  219. temp);
  220. outb_p(ALI1535_T_OUT, SMBHSTTYP);
  221. temp = inb_p(SMBHSTSTS);
  222. }
  223. /* now check the error bits and the busy bit */
  224. if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
  225. /* do a clear-on-write */
  226. outb_p(0xFF, SMBHSTSTS);
  227. temp = inb_p(SMBHSTSTS);
  228. if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
  229. /* This is probably going to be correctable only by a
  230. * power reset as one of the bits now appears to be
  231. * stuck */
  232. /* This may be a bus or device with electrical problems. */
  233. dev_err(&adap->dev,
  234. "SMBus reset failed! (0x%02x) - controller or "
  235. "device on bus is probably hung\n", temp);
  236. return -EBUSY;
  237. }
  238. } else {
  239. /* check and clear done bit */
  240. if (temp & ALI1535_STS_DONE)
  241. outb_p(temp, SMBHSTSTS);
  242. }
  243. /* start the transaction by writing anything to the start register */
  244. outb_p(0xFF, SMBHSTPORT);
  245. /* We will always wait for a fraction of a second! */
  246. timeout = 0;
  247. do {
  248. usleep_range(1000, 2000);
  249. temp = inb_p(SMBHSTSTS);
  250. } while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
  251. && (timeout++ < MAX_TIMEOUT));
  252. /* If the SMBus is still busy, we give up */
  253. if (timeout > MAX_TIMEOUT) {
  254. result = -ETIMEDOUT;
  255. dev_err(&adap->dev, "SMBus Timeout!\n");
  256. }
  257. if (temp & ALI1535_STS_FAIL) {
  258. result = -EIO;
  259. dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
  260. }
  261. /* Unfortunately the ALI SMB controller maps "no response" and "bus
  262. * collision" into a single bit. No response is the usual case so don't
  263. * do a printk. This means that bus collisions go unreported.
  264. */
  265. if (temp & ALI1535_STS_BUSERR) {
  266. result = -ENXIO;
  267. dev_dbg(&adap->dev,
  268. "Error: no response or bus collision ADD=%02x\n",
  269. inb_p(SMBHSTADD));
  270. }
  271. /* haven't ever seen this */
  272. if (temp & ALI1535_STS_DEV) {
  273. result = -EIO;
  274. dev_err(&adap->dev, "Error: device error\n");
  275. }
  276. /* check to see if the "command complete" indication is set */
  277. if (!(temp & ALI1535_STS_DONE)) {
  278. result = -ETIMEDOUT;
  279. dev_err(&adap->dev, "Error: command never completed\n");
  280. }
  281. dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
  282. "CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  283. inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
  284. inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
  285. /* take consequent actions for error conditions */
  286. if (!(temp & ALI1535_STS_DONE)) {
  287. /* issue "kill" to reset host controller */
  288. outb_p(ALI1535_KILL, SMBHSTTYP);
  289. outb_p(0xFF, SMBHSTSTS);
  290. } else if (temp & ALI1535_STS_ERR) {
  291. /* issue "timeout" to reset all devices on bus */
  292. outb_p(ALI1535_T_OUT, SMBHSTTYP);
  293. outb_p(0xFF, SMBHSTSTS);
  294. }
  295. return result;
  296. }
  297. /* Return negative errno on error. */
  298. static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
  299. unsigned short flags, char read_write, u8 command,
  300. int size, union i2c_smbus_data *data)
  301. {
  302. int i, len;
  303. int temp;
  304. int timeout;
  305. s32 result = 0;
  306. /* make sure SMBus is idle */
  307. temp = inb_p(SMBHSTSTS);
  308. for (timeout = 0;
  309. (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
  310. timeout++) {
  311. usleep_range(1000, 2000);
  312. temp = inb_p(SMBHSTSTS);
  313. }
  314. if (timeout >= MAX_TIMEOUT)
  315. dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
  316. /* clear status register (clear-on-write) */
  317. outb_p(0xFF, SMBHSTSTS);
  318. switch (size) {
  319. case I2C_SMBUS_QUICK:
  320. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  321. SMBHSTADD);
  322. size = ALI1535_QUICK;
  323. outb_p(size, SMBHSTTYP); /* output command */
  324. break;
  325. case I2C_SMBUS_BYTE:
  326. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  327. SMBHSTADD);
  328. size = ALI1535_BYTE;
  329. outb_p(size, SMBHSTTYP); /* output command */
  330. if (read_write == I2C_SMBUS_WRITE)
  331. outb_p(command, SMBHSTCMD);
  332. break;
  333. case I2C_SMBUS_BYTE_DATA:
  334. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  335. SMBHSTADD);
  336. size = ALI1535_BYTE_DATA;
  337. outb_p(size, SMBHSTTYP); /* output command */
  338. outb_p(command, SMBHSTCMD);
  339. if (read_write == I2C_SMBUS_WRITE)
  340. outb_p(data->byte, SMBHSTDAT0);
  341. break;
  342. case I2C_SMBUS_WORD_DATA:
  343. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  344. SMBHSTADD);
  345. size = ALI1535_WORD_DATA;
  346. outb_p(size, SMBHSTTYP); /* output command */
  347. outb_p(command, SMBHSTCMD);
  348. if (read_write == I2C_SMBUS_WRITE) {
  349. outb_p(data->word & 0xff, SMBHSTDAT0);
  350. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  351. }
  352. break;
  353. case I2C_SMBUS_BLOCK_DATA:
  354. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  355. SMBHSTADD);
  356. size = ALI1535_BLOCK_DATA;
  357. outb_p(size, SMBHSTTYP); /* output command */
  358. outb_p(command, SMBHSTCMD);
  359. if (read_write == I2C_SMBUS_WRITE) {
  360. len = data->block[0];
  361. if (len < 0) {
  362. len = 0;
  363. data->block[0] = len;
  364. }
  365. if (len > 32) {
  366. len = 32;
  367. data->block[0] = len;
  368. }
  369. outb_p(len, SMBHSTDAT0);
  370. /* Reset SMBBLKDAT */
  371. outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
  372. for (i = 1; i <= len; i++)
  373. outb_p(data->block[i], SMBBLKDAT);
  374. }
  375. break;
  376. default:
  377. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  378. result = -EOPNOTSUPP;
  379. goto EXIT;
  380. }
  381. result = ali1535_transaction(adap);
  382. if (result)
  383. goto EXIT;
  384. if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
  385. result = 0;
  386. goto EXIT;
  387. }
  388. switch (size) {
  389. case ALI1535_BYTE: /* Result put in SMBHSTDAT0 */
  390. data->byte = inb_p(SMBHSTDAT0);
  391. break;
  392. case ALI1535_BYTE_DATA:
  393. data->byte = inb_p(SMBHSTDAT0);
  394. break;
  395. case ALI1535_WORD_DATA:
  396. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  397. break;
  398. case ALI1535_BLOCK_DATA:
  399. len = inb_p(SMBHSTDAT0);
  400. if (len > 32)
  401. len = 32;
  402. data->block[0] = len;
  403. /* Reset SMBBLKDAT */
  404. outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
  405. for (i = 1; i <= data->block[0]; i++) {
  406. data->block[i] = inb_p(SMBBLKDAT);
  407. dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
  408. len, i, data->block[i]);
  409. }
  410. break;
  411. }
  412. EXIT:
  413. return result;
  414. }
  415. static u32 ali1535_func(struct i2c_adapter *adapter)
  416. {
  417. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  418. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  419. I2C_FUNC_SMBUS_BLOCK_DATA;
  420. }
  421. static const struct i2c_algorithm smbus_algorithm = {
  422. .smbus_xfer = ali1535_access,
  423. .functionality = ali1535_func,
  424. };
  425. static struct i2c_adapter ali1535_adapter = {
  426. .owner = THIS_MODULE,
  427. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  428. .algo = &smbus_algorithm,
  429. };
  430. static const struct pci_device_id ali1535_ids[] = {
  431. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
  432. { },
  433. };
  434. MODULE_DEVICE_TABLE(pci, ali1535_ids);
  435. static int ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
  436. {
  437. if (ali1535_setup(dev)) {
  438. dev_warn(&dev->dev,
  439. "ALI1535 not detected, module not inserted.\n");
  440. return -ENODEV;
  441. }
  442. /* set up the sysfs linkage to our parent device */
  443. ali1535_adapter.dev.parent = &dev->dev;
  444. snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),
  445. "SMBus ALI1535 adapter at %04x", ali1535_offset);
  446. return i2c_add_adapter(&ali1535_adapter);
  447. }
  448. static void ali1535_remove(struct pci_dev *dev)
  449. {
  450. i2c_del_adapter(&ali1535_adapter);
  451. release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
  452. }
  453. static struct pci_driver ali1535_driver = {
  454. .name = "ali1535_smbus",
  455. .id_table = ali1535_ids,
  456. .probe = ali1535_probe,
  457. .remove = ali1535_remove,
  458. };
  459. module_pci_driver(ali1535_driver);
  460. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  461. "Philip Edelbrock <phil@netroedge.com>, "
  462. "Mark D. Studebaker <mdsxyz123@yahoo.com> "
  463. "and Dan Eaton <dan.eaton@rocketlogix.com>");
  464. MODULE_DESCRIPTION("ALI1535 SMBus driver");
  465. MODULE_LICENSE("GPL");