i2c-ismt.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. * The full GNU General Public License is included in this distribution
  18. * in the file called LICENSE.GPL.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * * Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. /*
  49. * Supports the SMBus Message Transport (SMT) in the Intel Atom Processor
  50. * S12xx Product Family.
  51. *
  52. * Features supported by this driver:
  53. * Hardware PEC yes
  54. * Block buffer yes
  55. * Block process call transaction no
  56. * Slave mode no
  57. */
  58. #include <linux/module.h>
  59. #include <linux/pci.h>
  60. #include <linux/kernel.h>
  61. #include <linux/stddef.h>
  62. #include <linux/completion.h>
  63. #include <linux/dma-mapping.h>
  64. #include <linux/i2c.h>
  65. #include <linux/acpi.h>
  66. #include <linux/interrupt.h>
  67. #include <linux/io-64-nonatomic-lo-hi.h>
  68. /* PCI Address Constants */
  69. #define SMBBAR 0
  70. /* PCI DIDs for the Intel SMBus Message Transport (SMT) Devices */
  71. #define PCI_DEVICE_ID_INTEL_S1200_SMT0 0x0c59
  72. #define PCI_DEVICE_ID_INTEL_S1200_SMT1 0x0c5a
  73. #define PCI_DEVICE_ID_INTEL_DNV_SMT 0x19ac
  74. #define PCI_DEVICE_ID_INTEL_AVOTON_SMT 0x1f15
  75. #define ISMT_DESC_ENTRIES 2 /* number of descriptor entries */
  76. #define ISMT_MAX_RETRIES 3 /* number of SMBus retries to attempt */
  77. /* Hardware Descriptor Constants - Control Field */
  78. #define ISMT_DESC_CWRL 0x01 /* Command/Write Length */
  79. #define ISMT_DESC_BLK 0X04 /* Perform Block Transaction */
  80. #define ISMT_DESC_FAIR 0x08 /* Set fairness flag upon successful arbit. */
  81. #define ISMT_DESC_PEC 0x10 /* Packet Error Code */
  82. #define ISMT_DESC_I2C 0x20 /* I2C Enable */
  83. #define ISMT_DESC_INT 0x40 /* Interrupt */
  84. #define ISMT_DESC_SOE 0x80 /* Stop On Error */
  85. /* Hardware Descriptor Constants - Status Field */
  86. #define ISMT_DESC_SCS 0x01 /* Success */
  87. #define ISMT_DESC_DLTO 0x04 /* Data Low Time Out */
  88. #define ISMT_DESC_NAK 0x08 /* NAK Received */
  89. #define ISMT_DESC_CRC 0x10 /* CRC Error */
  90. #define ISMT_DESC_CLTO 0x20 /* Clock Low Time Out */
  91. #define ISMT_DESC_COL 0x40 /* Collisions */
  92. #define ISMT_DESC_LPR 0x80 /* Large Packet Received */
  93. /* Macros */
  94. #define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
  95. /* iSMT General Register address offsets (SMBBAR + <addr>) */
  96. #define ISMT_GR_GCTRL 0x000 /* General Control */
  97. #define ISMT_GR_SMTICL 0x008 /* SMT Interrupt Cause Location */
  98. #define ISMT_GR_ERRINTMSK 0x010 /* Error Interrupt Mask */
  99. #define ISMT_GR_ERRAERMSK 0x014 /* Error AER Mask */
  100. #define ISMT_GR_ERRSTS 0x018 /* Error Status */
  101. #define ISMT_GR_ERRINFO 0x01c /* Error Information */
  102. /* iSMT Master Registers */
  103. #define ISMT_MSTR_MDBA 0x100 /* Master Descriptor Base Address */
  104. #define ISMT_MSTR_MCTRL 0x108 /* Master Control */
  105. #define ISMT_MSTR_MSTS 0x10c /* Master Status */
  106. #define ISMT_MSTR_MDS 0x110 /* Master Descriptor Size */
  107. #define ISMT_MSTR_RPOLICY 0x114 /* Retry Policy */
  108. /* iSMT Miscellaneous Registers */
  109. #define ISMT_SPGT 0x300 /* SMBus PHY Global Timing */
  110. /* General Control Register (GCTRL) bit definitions */
  111. #define ISMT_GCTRL_TRST 0x04 /* Target Reset */
  112. #define ISMT_GCTRL_KILL 0x08 /* Kill */
  113. #define ISMT_GCTRL_SRST 0x40 /* Soft Reset */
  114. /* Master Control Register (MCTRL) bit definitions */
  115. #define ISMT_MCTRL_SS 0x01 /* Start/Stop */
  116. #define ISMT_MCTRL_MEIE 0x10 /* Master Error Interrupt Enable */
  117. #define ISMT_MCTRL_FMHP 0x00ff0000 /* Firmware Master Head Ptr (FMHP) */
  118. /* Master Status Register (MSTS) bit definitions */
  119. #define ISMT_MSTS_HMTP 0xff0000 /* HW Master Tail Pointer (HMTP) */
  120. #define ISMT_MSTS_MIS 0x20 /* Master Interrupt Status (MIS) */
  121. #define ISMT_MSTS_MEIS 0x10 /* Master Error Int Status (MEIS) */
  122. #define ISMT_MSTS_IP 0x01 /* In Progress */
  123. /* Master Descriptor Size (MDS) bit definitions */
  124. #define ISMT_MDS_MASK 0xff /* Master Descriptor Size mask (MDS) */
  125. /* SMBus PHY Global Timing Register (SPGT) bit definitions */
  126. #define ISMT_SPGT_SPD_MASK 0xc0000000 /* SMBus Speed mask */
  127. #define ISMT_SPGT_SPD_80K 0x00 /* 80 kHz */
  128. #define ISMT_SPGT_SPD_100K (0x1 << 30) /* 100 kHz */
  129. #define ISMT_SPGT_SPD_400K (0x2 << 30) /* 400 kHz */
  130. #define ISMT_SPGT_SPD_1M (0x3 << 30) /* 1 MHz */
  131. /* MSI Control Register (MSICTL) bit definitions */
  132. #define ISMT_MSICTL_MSIE 0x01 /* MSI Enable */
  133. /* iSMT Hardware Descriptor */
  134. struct ismt_desc {
  135. u8 tgtaddr_rw; /* target address & r/w bit */
  136. u8 wr_len_cmd; /* write length in bytes or a command */
  137. u8 rd_len; /* read length */
  138. u8 control; /* control bits */
  139. u8 status; /* status bits */
  140. u8 retry; /* collision retry and retry count */
  141. u8 rxbytes; /* received bytes */
  142. u8 txbytes; /* transmitted bytes */
  143. u32 dptr_low; /* lower 32 bit of the data pointer */
  144. u32 dptr_high; /* upper 32 bit of the data pointer */
  145. } __packed;
  146. struct ismt_priv {
  147. struct i2c_adapter adapter;
  148. void __iomem *smba; /* PCI BAR */
  149. struct pci_dev *pci_dev;
  150. struct ismt_desc *hw; /* descriptor virt base addr */
  151. dma_addr_t io_rng_dma; /* descriptor HW base addr */
  152. u8 head; /* ring buffer head pointer */
  153. struct completion cmp; /* interrupt completion */
  154. u8 buffer[I2C_SMBUS_BLOCK_MAX + 16]; /* temp R/W data buffer */
  155. };
  156. /**
  157. * ismt_ids - PCI device IDs supported by this driver
  158. */
  159. static const struct pci_device_id ismt_ids[] = {
  160. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
  161. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
  162. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMT) },
  163. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
  164. { 0, }
  165. };
  166. MODULE_DEVICE_TABLE(pci, ismt_ids);
  167. /* Bus speed control bits for slow debuggers - refer to the docs for usage */
  168. static unsigned int bus_speed;
  169. module_param(bus_speed, uint, S_IRUGO);
  170. MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
  171. /**
  172. * __ismt_desc_dump() - dump the contents of a specific descriptor
  173. */
  174. static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
  175. {
  176. dev_dbg(dev, "Descriptor struct: %p\n", desc);
  177. dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
  178. dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
  179. dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
  180. dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
  181. dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
  182. dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
  183. dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
  184. dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
  185. dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
  186. dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
  187. }
  188. /**
  189. * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
  190. * @priv: iSMT private data
  191. */
  192. static void ismt_desc_dump(struct ismt_priv *priv)
  193. {
  194. struct device *dev = &priv->pci_dev->dev;
  195. struct ismt_desc *desc = &priv->hw[priv->head];
  196. dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
  197. __ismt_desc_dump(dev, desc);
  198. }
  199. /**
  200. * ismt_gen_reg_dump() - dump the iSMT General Registers
  201. * @priv: iSMT private data
  202. */
  203. static void ismt_gen_reg_dump(struct ismt_priv *priv)
  204. {
  205. struct device *dev = &priv->pci_dev->dev;
  206. dev_dbg(dev, "Dump of the iSMT General Registers\n");
  207. dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
  208. priv->smba + ISMT_GR_GCTRL,
  209. readl(priv->smba + ISMT_GR_GCTRL));
  210. dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
  211. priv->smba + ISMT_GR_SMTICL,
  212. (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
  213. dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
  214. priv->smba + ISMT_GR_ERRINTMSK,
  215. readl(priv->smba + ISMT_GR_ERRINTMSK));
  216. dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
  217. priv->smba + ISMT_GR_ERRAERMSK,
  218. readl(priv->smba + ISMT_GR_ERRAERMSK));
  219. dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
  220. priv->smba + ISMT_GR_ERRSTS,
  221. readl(priv->smba + ISMT_GR_ERRSTS));
  222. dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
  223. priv->smba + ISMT_GR_ERRINFO,
  224. readl(priv->smba + ISMT_GR_ERRINFO));
  225. }
  226. /**
  227. * ismt_mstr_reg_dump() - dump the iSMT Master Registers
  228. * @priv: iSMT private data
  229. */
  230. static void ismt_mstr_reg_dump(struct ismt_priv *priv)
  231. {
  232. struct device *dev = &priv->pci_dev->dev;
  233. dev_dbg(dev, "Dump of the iSMT Master Registers\n");
  234. dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
  235. priv->smba + ISMT_MSTR_MDBA,
  236. (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
  237. dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
  238. priv->smba + ISMT_MSTR_MCTRL,
  239. readl(priv->smba + ISMT_MSTR_MCTRL));
  240. dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
  241. priv->smba + ISMT_MSTR_MSTS,
  242. readl(priv->smba + ISMT_MSTR_MSTS));
  243. dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
  244. priv->smba + ISMT_MSTR_MDS,
  245. readl(priv->smba + ISMT_MSTR_MDS));
  246. dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
  247. priv->smba + ISMT_MSTR_RPOLICY,
  248. readl(priv->smba + ISMT_MSTR_RPOLICY));
  249. dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
  250. priv->smba + ISMT_SPGT,
  251. readl(priv->smba + ISMT_SPGT));
  252. }
  253. /**
  254. * ismt_submit_desc() - add a descriptor to the ring
  255. * @priv: iSMT private data
  256. */
  257. static void ismt_submit_desc(struct ismt_priv *priv)
  258. {
  259. uint fmhp;
  260. uint val;
  261. ismt_desc_dump(priv);
  262. ismt_gen_reg_dump(priv);
  263. ismt_mstr_reg_dump(priv);
  264. /* Set the FMHP (Firmware Master Head Pointer)*/
  265. fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
  266. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  267. writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
  268. priv->smba + ISMT_MSTR_MCTRL);
  269. /* Set the start bit */
  270. val = readl(priv->smba + ISMT_MSTR_MCTRL);
  271. writel(val | ISMT_MCTRL_SS,
  272. priv->smba + ISMT_MSTR_MCTRL);
  273. }
  274. /**
  275. * ismt_process_desc() - handle the completion of the descriptor
  276. * @desc: the iSMT hardware descriptor
  277. * @data: data buffer from the upper layer
  278. * @priv: ismt_priv struct holding our dma buffer
  279. * @size: SMBus transaction type
  280. * @read_write: flag to indicate if this is a read or write
  281. */
  282. static int ismt_process_desc(const struct ismt_desc *desc,
  283. union i2c_smbus_data *data,
  284. struct ismt_priv *priv, int size,
  285. char read_write)
  286. {
  287. u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
  288. dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
  289. __ismt_desc_dump(&priv->pci_dev->dev, desc);
  290. ismt_gen_reg_dump(priv);
  291. ismt_mstr_reg_dump(priv);
  292. if (desc->status & ISMT_DESC_SCS) {
  293. if (read_write == I2C_SMBUS_WRITE &&
  294. size != I2C_SMBUS_PROC_CALL)
  295. return 0;
  296. switch (size) {
  297. case I2C_SMBUS_BYTE:
  298. case I2C_SMBUS_BYTE_DATA:
  299. data->byte = dma_buffer[0];
  300. break;
  301. case I2C_SMBUS_WORD_DATA:
  302. case I2C_SMBUS_PROC_CALL:
  303. data->word = dma_buffer[0] | (dma_buffer[1] << 8);
  304. break;
  305. case I2C_SMBUS_BLOCK_DATA:
  306. if (desc->rxbytes != dma_buffer[0] + 1)
  307. return -EMSGSIZE;
  308. memcpy(data->block, dma_buffer, desc->rxbytes);
  309. break;
  310. case I2C_SMBUS_I2C_BLOCK_DATA:
  311. memcpy(&data->block[1], dma_buffer, desc->rxbytes);
  312. data->block[0] = desc->rxbytes;
  313. break;
  314. }
  315. return 0;
  316. }
  317. if (likely(desc->status & ISMT_DESC_NAK))
  318. return -ENXIO;
  319. if (desc->status & ISMT_DESC_CRC)
  320. return -EBADMSG;
  321. if (desc->status & ISMT_DESC_COL)
  322. return -EAGAIN;
  323. if (desc->status & ISMT_DESC_LPR)
  324. return -EPROTO;
  325. if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
  326. return -ETIMEDOUT;
  327. return -EIO;
  328. }
  329. /**
  330. * ismt_access() - process an SMBus command
  331. * @adap: the i2c host adapter
  332. * @addr: address of the i2c/SMBus target
  333. * @flags: command options
  334. * @read_write: read from or write to device
  335. * @command: the i2c/SMBus command to issue
  336. * @size: SMBus transaction type
  337. * @data: read/write data buffer
  338. */
  339. static int ismt_access(struct i2c_adapter *adap, u16 addr,
  340. unsigned short flags, char read_write, u8 command,
  341. int size, union i2c_smbus_data *data)
  342. {
  343. int ret;
  344. unsigned long time_left;
  345. dma_addr_t dma_addr = 0; /* address of the data buffer */
  346. u8 dma_size = 0;
  347. enum dma_data_direction dma_direction = 0;
  348. struct ismt_desc *desc;
  349. struct ismt_priv *priv = i2c_get_adapdata(adap);
  350. struct device *dev = &priv->pci_dev->dev;
  351. u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
  352. desc = &priv->hw[priv->head];
  353. /* Initialize the DMA buffer */
  354. memset(priv->buffer, 0, sizeof(priv->buffer));
  355. /* Initialize the descriptor */
  356. memset(desc, 0, sizeof(struct ismt_desc));
  357. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
  358. /* Initialize common control bits */
  359. if (likely(pci_dev_msi_enabled(priv->pci_dev)))
  360. desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
  361. else
  362. desc->control = ISMT_DESC_FAIR;
  363. if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
  364. && (size != I2C_SMBUS_I2C_BLOCK_DATA))
  365. desc->control |= ISMT_DESC_PEC;
  366. switch (size) {
  367. case I2C_SMBUS_QUICK:
  368. dev_dbg(dev, "I2C_SMBUS_QUICK\n");
  369. break;
  370. case I2C_SMBUS_BYTE:
  371. if (read_write == I2C_SMBUS_WRITE) {
  372. /*
  373. * Send Byte
  374. * The command field contains the write data
  375. */
  376. dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
  377. desc->control |= ISMT_DESC_CWRL;
  378. desc->wr_len_cmd = command;
  379. } else {
  380. /* Receive Byte */
  381. dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
  382. dma_size = 1;
  383. dma_direction = DMA_FROM_DEVICE;
  384. desc->rd_len = 1;
  385. }
  386. break;
  387. case I2C_SMBUS_BYTE_DATA:
  388. if (read_write == I2C_SMBUS_WRITE) {
  389. /*
  390. * Write Byte
  391. * Command plus 1 data byte
  392. */
  393. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
  394. desc->wr_len_cmd = 2;
  395. dma_size = 2;
  396. dma_direction = DMA_TO_DEVICE;
  397. dma_buffer[0] = command;
  398. dma_buffer[1] = data->byte;
  399. } else {
  400. /* Read Byte */
  401. dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
  402. desc->control |= ISMT_DESC_CWRL;
  403. desc->wr_len_cmd = command;
  404. desc->rd_len = 1;
  405. dma_size = 1;
  406. dma_direction = DMA_FROM_DEVICE;
  407. }
  408. break;
  409. case I2C_SMBUS_WORD_DATA:
  410. if (read_write == I2C_SMBUS_WRITE) {
  411. /* Write Word */
  412. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
  413. desc->wr_len_cmd = 3;
  414. dma_size = 3;
  415. dma_direction = DMA_TO_DEVICE;
  416. dma_buffer[0] = command;
  417. dma_buffer[1] = data->word & 0xff;
  418. dma_buffer[2] = data->word >> 8;
  419. } else {
  420. /* Read Word */
  421. dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
  422. desc->wr_len_cmd = command;
  423. desc->control |= ISMT_DESC_CWRL;
  424. desc->rd_len = 2;
  425. dma_size = 2;
  426. dma_direction = DMA_FROM_DEVICE;
  427. }
  428. break;
  429. case I2C_SMBUS_PROC_CALL:
  430. dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
  431. desc->wr_len_cmd = 3;
  432. desc->rd_len = 2;
  433. dma_size = 3;
  434. dma_direction = DMA_BIDIRECTIONAL;
  435. dma_buffer[0] = command;
  436. dma_buffer[1] = data->word & 0xff;
  437. dma_buffer[2] = data->word >> 8;
  438. break;
  439. case I2C_SMBUS_BLOCK_DATA:
  440. if (read_write == I2C_SMBUS_WRITE) {
  441. /* Block Write */
  442. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
  443. dma_size = data->block[0] + 1;
  444. dma_direction = DMA_TO_DEVICE;
  445. desc->wr_len_cmd = dma_size;
  446. desc->control |= ISMT_DESC_BLK;
  447. dma_buffer[0] = command;
  448. memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
  449. } else {
  450. /* Block Read */
  451. dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
  452. dma_size = I2C_SMBUS_BLOCK_MAX;
  453. dma_direction = DMA_FROM_DEVICE;
  454. desc->rd_len = dma_size;
  455. desc->wr_len_cmd = command;
  456. desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
  457. }
  458. break;
  459. case I2C_SMBUS_I2C_BLOCK_DATA:
  460. /* Make sure the length is valid */
  461. if (data->block[0] < 1)
  462. data->block[0] = 1;
  463. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  464. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  465. if (read_write == I2C_SMBUS_WRITE) {
  466. /* i2c Block Write */
  467. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: WRITE\n");
  468. dma_size = data->block[0] + 1;
  469. dma_direction = DMA_TO_DEVICE;
  470. desc->wr_len_cmd = dma_size;
  471. desc->control |= ISMT_DESC_I2C;
  472. dma_buffer[0] = command;
  473. memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
  474. } else {
  475. /* i2c Block Read */
  476. dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
  477. dma_size = data->block[0];
  478. dma_direction = DMA_FROM_DEVICE;
  479. desc->rd_len = dma_size;
  480. desc->wr_len_cmd = command;
  481. desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
  482. /*
  483. * Per the "Table 15-15. I2C Commands",
  484. * in the External Design Specification (EDS),
  485. * (Document Number: 508084, Revision: 2.0),
  486. * the _rw bit must be 0
  487. */
  488. desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
  489. }
  490. break;
  491. default:
  492. dev_err(dev, "Unsupported transaction %d\n",
  493. size);
  494. return -EOPNOTSUPP;
  495. }
  496. /* map the data buffer */
  497. if (dma_size != 0) {
  498. dev_dbg(dev, " dev=%p\n", dev);
  499. dev_dbg(dev, " data=%p\n", data);
  500. dev_dbg(dev, " dma_buffer=%p\n", dma_buffer);
  501. dev_dbg(dev, " dma_size=%d\n", dma_size);
  502. dev_dbg(dev, " dma_direction=%d\n", dma_direction);
  503. dma_addr = dma_map_single(dev,
  504. dma_buffer,
  505. dma_size,
  506. dma_direction);
  507. if (dma_mapping_error(dev, dma_addr)) {
  508. dev_err(dev, "Error in mapping dma buffer %p\n",
  509. dma_buffer);
  510. return -EIO;
  511. }
  512. dev_dbg(dev, " dma_addr = %pad\n", &dma_addr);
  513. desc->dptr_low = lower_32_bits(dma_addr);
  514. desc->dptr_high = upper_32_bits(dma_addr);
  515. }
  516. reinit_completion(&priv->cmp);
  517. /* Add the descriptor */
  518. ismt_submit_desc(priv);
  519. /* Now we wait for interrupt completion, 1s */
  520. time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
  521. /* unmap the data buffer */
  522. if (dma_size != 0)
  523. dma_unmap_single(dev, dma_addr, dma_size, dma_direction);
  524. if (unlikely(!time_left)) {
  525. dev_err(dev, "completion wait timed out\n");
  526. ret = -ETIMEDOUT;
  527. goto out;
  528. }
  529. /* do any post processing of the descriptor here */
  530. ret = ismt_process_desc(desc, data, priv, size, read_write);
  531. out:
  532. /* Update the ring pointer */
  533. priv->head++;
  534. priv->head %= ISMT_DESC_ENTRIES;
  535. return ret;
  536. }
  537. /**
  538. * ismt_func() - report which i2c commands are supported by this adapter
  539. * @adap: the i2c host adapter
  540. */
  541. static u32 ismt_func(struct i2c_adapter *adap)
  542. {
  543. return I2C_FUNC_SMBUS_QUICK |
  544. I2C_FUNC_SMBUS_BYTE |
  545. I2C_FUNC_SMBUS_BYTE_DATA |
  546. I2C_FUNC_SMBUS_WORD_DATA |
  547. I2C_FUNC_SMBUS_PROC_CALL |
  548. I2C_FUNC_SMBUS_BLOCK_DATA |
  549. I2C_FUNC_SMBUS_I2C_BLOCK |
  550. I2C_FUNC_SMBUS_PEC;
  551. }
  552. /**
  553. * smbus_algorithm - the adapter algorithm and supported functionality
  554. * @smbus_xfer: the adapter algorithm
  555. * @functionality: functionality supported by the adapter
  556. */
  557. static const struct i2c_algorithm smbus_algorithm = {
  558. .smbus_xfer = ismt_access,
  559. .functionality = ismt_func,
  560. };
  561. /**
  562. * ismt_handle_isr() - interrupt handler bottom half
  563. * @priv: iSMT private data
  564. */
  565. static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
  566. {
  567. complete(&priv->cmp);
  568. return IRQ_HANDLED;
  569. }
  570. /**
  571. * ismt_do_interrupt() - IRQ interrupt handler
  572. * @vec: interrupt vector
  573. * @data: iSMT private data
  574. */
  575. static irqreturn_t ismt_do_interrupt(int vec, void *data)
  576. {
  577. u32 val;
  578. struct ismt_priv *priv = data;
  579. /*
  580. * check to see it's our interrupt, return IRQ_NONE if not ours
  581. * since we are sharing interrupt
  582. */
  583. val = readl(priv->smba + ISMT_MSTR_MSTS);
  584. if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
  585. return IRQ_NONE;
  586. else
  587. writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
  588. priv->smba + ISMT_MSTR_MSTS);
  589. return ismt_handle_isr(priv);
  590. }
  591. /**
  592. * ismt_do_msi_interrupt() - MSI interrupt handler
  593. * @vec: interrupt vector
  594. * @data: iSMT private data
  595. */
  596. static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
  597. {
  598. return ismt_handle_isr(data);
  599. }
  600. /**
  601. * ismt_hw_init() - initialize the iSMT hardware
  602. * @priv: iSMT private data
  603. */
  604. static void ismt_hw_init(struct ismt_priv *priv)
  605. {
  606. u32 val;
  607. struct device *dev = &priv->pci_dev->dev;
  608. /* initialize the Master Descriptor Base Address (MDBA) */
  609. writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
  610. /* initialize the Master Control Register (MCTRL) */
  611. writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
  612. /* initialize the Master Status Register (MSTS) */
  613. writel(0, priv->smba + ISMT_MSTR_MSTS);
  614. /* initialize the Master Descriptor Size (MDS) */
  615. val = readl(priv->smba + ISMT_MSTR_MDS);
  616. writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
  617. priv->smba + ISMT_MSTR_MDS);
  618. /*
  619. * Set the SMBus speed (could use this for slow HW debuggers)
  620. */
  621. val = readl(priv->smba + ISMT_SPGT);
  622. switch (bus_speed) {
  623. case 0:
  624. break;
  625. case 80:
  626. dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
  627. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
  628. priv->smba + ISMT_SPGT);
  629. break;
  630. case 100:
  631. dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
  632. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
  633. priv->smba + ISMT_SPGT);
  634. break;
  635. case 400:
  636. dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
  637. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
  638. priv->smba + ISMT_SPGT);
  639. break;
  640. case 1000:
  641. dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
  642. writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
  643. priv->smba + ISMT_SPGT);
  644. break;
  645. default:
  646. dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
  647. break;
  648. }
  649. val = readl(priv->smba + ISMT_SPGT);
  650. switch (val & ISMT_SPGT_SPD_MASK) {
  651. case ISMT_SPGT_SPD_80K:
  652. bus_speed = 80;
  653. break;
  654. case ISMT_SPGT_SPD_100K:
  655. bus_speed = 100;
  656. break;
  657. case ISMT_SPGT_SPD_400K:
  658. bus_speed = 400;
  659. break;
  660. case ISMT_SPGT_SPD_1M:
  661. bus_speed = 1000;
  662. break;
  663. }
  664. dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
  665. }
  666. /**
  667. * ismt_dev_init() - initialize the iSMT data structures
  668. * @priv: iSMT private data
  669. */
  670. static int ismt_dev_init(struct ismt_priv *priv)
  671. {
  672. /* allocate memory for the descriptor */
  673. priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
  674. (ISMT_DESC_ENTRIES
  675. * sizeof(struct ismt_desc)),
  676. &priv->io_rng_dma,
  677. GFP_KERNEL);
  678. if (!priv->hw)
  679. return -ENOMEM;
  680. memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
  681. priv->head = 0;
  682. init_completion(&priv->cmp);
  683. return 0;
  684. }
  685. /**
  686. * ismt_int_init() - initialize interrupts
  687. * @priv: iSMT private data
  688. */
  689. static int ismt_int_init(struct ismt_priv *priv)
  690. {
  691. int err;
  692. /* Try using MSI interrupts */
  693. err = pci_enable_msi(priv->pci_dev);
  694. if (err)
  695. goto intx;
  696. err = devm_request_irq(&priv->pci_dev->dev,
  697. priv->pci_dev->irq,
  698. ismt_do_msi_interrupt,
  699. 0,
  700. "ismt-msi",
  701. priv);
  702. if (err) {
  703. pci_disable_msi(priv->pci_dev);
  704. goto intx;
  705. }
  706. return 0;
  707. /* Try using legacy interrupts */
  708. intx:
  709. dev_warn(&priv->pci_dev->dev,
  710. "Unable to use MSI interrupts, falling back to legacy\n");
  711. err = devm_request_irq(&priv->pci_dev->dev,
  712. priv->pci_dev->irq,
  713. ismt_do_interrupt,
  714. IRQF_SHARED,
  715. "ismt-intx",
  716. priv);
  717. if (err) {
  718. dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
  719. return err;
  720. }
  721. return 0;
  722. }
  723. static struct pci_driver ismt_driver;
  724. /**
  725. * ismt_probe() - probe for iSMT devices
  726. * @pdev: PCI-Express device
  727. * @id: PCI-Express device ID
  728. */
  729. static int
  730. ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  731. {
  732. int err;
  733. struct ismt_priv *priv;
  734. unsigned long start, len;
  735. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  736. if (!priv)
  737. return -ENOMEM;
  738. pci_set_drvdata(pdev, priv);
  739. i2c_set_adapdata(&priv->adapter, priv);
  740. priv->adapter.owner = THIS_MODULE;
  741. priv->adapter.class = I2C_CLASS_HWMON;
  742. priv->adapter.algo = &smbus_algorithm;
  743. priv->adapter.dev.parent = &pdev->dev;
  744. ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev));
  745. priv->adapter.retries = ISMT_MAX_RETRIES;
  746. priv->pci_dev = pdev;
  747. err = pcim_enable_device(pdev);
  748. if (err) {
  749. dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
  750. err);
  751. return err;
  752. }
  753. /* enable bus mastering */
  754. pci_set_master(pdev);
  755. /* Determine the address of the SMBus area */
  756. start = pci_resource_start(pdev, SMBBAR);
  757. len = pci_resource_len(pdev, SMBBAR);
  758. if (!start || !len) {
  759. dev_err(&pdev->dev,
  760. "SMBus base address uninitialized, upgrade BIOS\n");
  761. return -ENODEV;
  762. }
  763. snprintf(priv->adapter.name, sizeof(priv->adapter.name),
  764. "SMBus iSMT adapter at %lx", start);
  765. dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
  766. dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
  767. err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
  768. if (err) {
  769. dev_err(&pdev->dev, "ACPI resource conflict!\n");
  770. return err;
  771. }
  772. err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
  773. if (err) {
  774. dev_err(&pdev->dev,
  775. "Failed to request SMBus region 0x%lx-0x%lx\n",
  776. start, start + len);
  777. return err;
  778. }
  779. priv->smba = pcim_iomap(pdev, SMBBAR, len);
  780. if (!priv->smba) {
  781. dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
  782. return -ENODEV;
  783. }
  784. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
  785. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
  786. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  787. (pci_set_consistent_dma_mask(pdev,
  788. DMA_BIT_MASK(32)) != 0)) {
  789. dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
  790. pdev);
  791. return -ENODEV;
  792. }
  793. }
  794. err = ismt_dev_init(priv);
  795. if (err)
  796. return err;
  797. ismt_hw_init(priv);
  798. err = ismt_int_init(priv);
  799. if (err)
  800. return err;
  801. err = i2c_add_adapter(&priv->adapter);
  802. if (err)
  803. return -ENODEV;
  804. return 0;
  805. }
  806. /**
  807. * ismt_remove() - release driver resources
  808. * @pdev: PCI-Express device
  809. */
  810. static void ismt_remove(struct pci_dev *pdev)
  811. {
  812. struct ismt_priv *priv = pci_get_drvdata(pdev);
  813. i2c_del_adapter(&priv->adapter);
  814. }
  815. static struct pci_driver ismt_driver = {
  816. .name = "ismt_smbus",
  817. .id_table = ismt_ids,
  818. .probe = ismt_probe,
  819. .remove = ismt_remove,
  820. };
  821. module_pci_driver(ismt_driver);
  822. MODULE_LICENSE("Dual BSD/GPL");
  823. MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
  824. MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");