i2c-ismt.c 27 KB

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