idt_89hpesx.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /*
  2. * This file is provided under a GPLv2 license. When using or
  3. * redistributing this file, you may do so under that license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, it can be found <http://www.gnu.org/licenses/>.
  20. *
  21. * The full GNU General Public License is included in this distribution in
  22. * the file called "COPYING".
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * IDT PCIe-switch NTB Linux driver
  37. *
  38. * Contact Information:
  39. * Serge Semin <fancer.lancer@gmail.com>, <Sergey.Semin@t-platforms.ru>
  40. */
  41. /*
  42. * NOTE of the IDT 89HPESx SMBus-slave interface driver
  43. * This driver primarily is developed to have an access to EEPROM device of
  44. * IDT PCIe-switches. IDT provides a simple SMBus interface to perform IO-
  45. * operations from/to EEPROM, which is located at private (so called Master)
  46. * SMBus of switches. Using that interface this the driver creates a simple
  47. * binary sysfs-file in the device directory:
  48. * /sys/bus/i2c/devices/<bus>-<devaddr>/eeprom
  49. * In case if read-only flag is specified in the dts-node of device desription,
  50. * User-space applications won't be able to write to the EEPROM sysfs-node.
  51. * Additionally IDT 89HPESx SMBus interface has an ability to write/read
  52. * data of device CSRs. This driver exposes debugf-file to perform simple IO
  53. * operations using that ability for just basic debug purpose. Particularly
  54. * next file is created in the specific debugfs-directory:
  55. * /sys/kernel/debug/idt_csr/
  56. * Format of the debugfs-node is:
  57. * $ cat /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
  58. * <CSR address>:<CSR value>
  59. * So reading the content of the file gives current CSR address and it value.
  60. * If User-space application wishes to change current CSR address,
  61. * it can just write a proper value to the sysfs-file:
  62. * $ echo "<CSR address>" > /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>
  63. * If it wants to change the CSR value as well, the format of the write
  64. * operation is:
  65. * $ echo "<CSR address>:<CSR value>" > \
  66. * /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
  67. * CSR address and value can be any of hexadecimal, decimal or octal format.
  68. */
  69. #include <linux/kernel.h>
  70. #include <linux/init.h>
  71. #include <linux/module.h>
  72. #include <linux/types.h>
  73. #include <linux/sizes.h>
  74. #include <linux/slab.h>
  75. #include <linux/mutex.h>
  76. #include <linux/sysfs.h>
  77. #include <linux/debugfs.h>
  78. #include <linux/mod_devicetable.h>
  79. #include <linux/property.h>
  80. #include <linux/i2c.h>
  81. #include <linux/pci_ids.h>
  82. #include <linux/delay.h>
  83. #define IDT_NAME "89hpesx"
  84. #define IDT_89HPESX_DESC "IDT 89HPESx SMBus-slave interface driver"
  85. #define IDT_89HPESX_VER "1.0"
  86. MODULE_DESCRIPTION(IDT_89HPESX_DESC);
  87. MODULE_VERSION(IDT_89HPESX_VER);
  88. MODULE_LICENSE("GPL v2");
  89. MODULE_AUTHOR("T-platforms");
  90. /*
  91. * csr_dbgdir - CSR read/write operations Debugfs directory
  92. */
  93. static struct dentry *csr_dbgdir;
  94. /*
  95. * struct idt_89hpesx_dev - IDT 89HPESx device data structure
  96. * @eesize: Size of EEPROM in bytes (calculated from "idt,eecompatible")
  97. * @eero: EEPROM Read-only flag
  98. * @eeaddr: EEPROM custom address
  99. *
  100. * @inieecmd: Initial cmd value for EEPROM read/write operations
  101. * @inicsrcmd: Initial cmd value for CSR read/write operations
  102. * @iniccode: Initialial command code value for IO-operations
  103. *
  104. * @csr: CSR address to perform read operation
  105. *
  106. * @smb_write: SMBus write method
  107. * @smb_read: SMBus read method
  108. * @smb_mtx: SMBus mutex
  109. *
  110. * @client: i2c client used to perform IO operations
  111. *
  112. * @ee_file: EEPROM read/write sysfs-file
  113. * @csr_file: CSR read/write debugfs-node
  114. */
  115. struct idt_smb_seq;
  116. struct idt_89hpesx_dev {
  117. u32 eesize;
  118. bool eero;
  119. u8 eeaddr;
  120. u8 inieecmd;
  121. u8 inicsrcmd;
  122. u8 iniccode;
  123. u16 csr;
  124. int (*smb_write)(struct idt_89hpesx_dev *, const struct idt_smb_seq *);
  125. int (*smb_read)(struct idt_89hpesx_dev *, struct idt_smb_seq *);
  126. struct mutex smb_mtx;
  127. struct i2c_client *client;
  128. struct bin_attribute *ee_file;
  129. struct dentry *csr_dir;
  130. struct dentry *csr_file;
  131. };
  132. /*
  133. * struct idt_smb_seq - sequence of data to be read/written from/to IDT 89HPESx
  134. * @ccode: SMBus command code
  135. * @bytecnt: Byte count of operation
  136. * @data: Data to by written
  137. */
  138. struct idt_smb_seq {
  139. u8 ccode;
  140. u8 bytecnt;
  141. u8 *data;
  142. };
  143. /*
  144. * struct idt_eeprom_seq - sequence of data to be read/written from/to EEPROM
  145. * @cmd: Transaction CMD
  146. * @eeaddr: EEPROM custom address
  147. * @memaddr: Internal memory address of EEPROM
  148. * @data: Data to be written at the memory address
  149. */
  150. struct idt_eeprom_seq {
  151. u8 cmd;
  152. u8 eeaddr;
  153. u16 memaddr;
  154. u8 data;
  155. } __packed;
  156. /*
  157. * struct idt_csr_seq - sequence of data to be read/written from/to CSR
  158. * @cmd: Transaction CMD
  159. * @csraddr: Internal IDT device CSR address
  160. * @data: Data to be read/written from/to the CSR address
  161. */
  162. struct idt_csr_seq {
  163. u8 cmd;
  164. u16 csraddr;
  165. u32 data;
  166. } __packed;
  167. /*
  168. * SMBus command code macros
  169. * @CCODE_END: Indicates the end of transaction
  170. * @CCODE_START: Indicates the start of transaction
  171. * @CCODE_CSR: CSR read/write transaction
  172. * @CCODE_EEPROM: EEPROM read/write transaction
  173. * @CCODE_BYTE: Supplied data has BYTE length
  174. * @CCODE_WORD: Supplied data has WORD length
  175. * @CCODE_BLOCK: Supplied data has variable length passed in bytecnt
  176. * byte right following CCODE byte
  177. */
  178. #define CCODE_END ((u8)0x01)
  179. #define CCODE_START ((u8)0x02)
  180. #define CCODE_CSR ((u8)0x00)
  181. #define CCODE_EEPROM ((u8)0x04)
  182. #define CCODE_BYTE ((u8)0x00)
  183. #define CCODE_WORD ((u8)0x20)
  184. #define CCODE_BLOCK ((u8)0x40)
  185. #define CCODE_PEC ((u8)0x80)
  186. /*
  187. * EEPROM command macros
  188. * @EEPROM_OP_WRITE: EEPROM write operation
  189. * @EEPROM_OP_READ: EEPROM read operation
  190. * @EEPROM_USA: Use specified address of EEPROM
  191. * @EEPROM_NAERR: EEPROM device is not ready to respond
  192. * @EEPROM_LAERR: EEPROM arbitration loss error
  193. * @EEPROM_MSS: EEPROM misplace start & stop bits error
  194. * @EEPROM_WR_CNT: Bytes count to perform write operation
  195. * @EEPROM_WRRD_CNT: Bytes count to write before reading
  196. * @EEPROM_RD_CNT: Bytes count to perform read operation
  197. * @EEPROM_DEF_SIZE: Fall back size of EEPROM
  198. * @EEPROM_DEF_ADDR: Defatul EEPROM address
  199. * @EEPROM_TOUT: Timeout before retry read operation if eeprom is busy
  200. */
  201. #define EEPROM_OP_WRITE ((u8)0x00)
  202. #define EEPROM_OP_READ ((u8)0x01)
  203. #define EEPROM_USA ((u8)0x02)
  204. #define EEPROM_NAERR ((u8)0x08)
  205. #define EEPROM_LAERR ((u8)0x10)
  206. #define EEPROM_MSS ((u8)0x20)
  207. #define EEPROM_WR_CNT ((u8)5)
  208. #define EEPROM_WRRD_CNT ((u8)4)
  209. #define EEPROM_RD_CNT ((u8)5)
  210. #define EEPROM_DEF_SIZE ((u16)4096)
  211. #define EEPROM_DEF_ADDR ((u8)0x50)
  212. #define EEPROM_TOUT (100)
  213. /*
  214. * CSR command macros
  215. * @CSR_DWE: Enable all four bytes of the operation
  216. * @CSR_OP_WRITE: CSR write operation
  217. * @CSR_OP_READ: CSR read operation
  218. * @CSR_RERR: Read operation error
  219. * @CSR_WERR: Write operation error
  220. * @CSR_WR_CNT: Bytes count to perform write operation
  221. * @CSR_WRRD_CNT: Bytes count to write before reading
  222. * @CSR_RD_CNT: Bytes count to perform read operation
  223. * @CSR_MAX: Maximum CSR address
  224. * @CSR_DEF: Default CSR address
  225. * @CSR_REAL_ADDR: CSR real unshifted address
  226. */
  227. #define CSR_DWE ((u8)0x0F)
  228. #define CSR_OP_WRITE ((u8)0x00)
  229. #define CSR_OP_READ ((u8)0x10)
  230. #define CSR_RERR ((u8)0x40)
  231. #define CSR_WERR ((u8)0x80)
  232. #define CSR_WR_CNT ((u8)7)
  233. #define CSR_WRRD_CNT ((u8)3)
  234. #define CSR_RD_CNT ((u8)7)
  235. #define CSR_MAX ((u32)0x3FFFF)
  236. #define CSR_DEF ((u16)0x0000)
  237. #define CSR_REAL_ADDR(val) ((unsigned int)val << 2)
  238. /*
  239. * IDT 89HPESx basic register
  240. * @IDT_VIDDID_CSR: PCIe VID and DID of IDT 89HPESx
  241. * @IDT_VID_MASK: Mask of VID
  242. */
  243. #define IDT_VIDDID_CSR ((u32)0x0000)
  244. #define IDT_VID_MASK ((u32)0xFFFF)
  245. /*
  246. * IDT 89HPESx can send NACK when new command is sent before previous one
  247. * fininshed execution. In this case driver retries operation
  248. * certain times.
  249. * @RETRY_CNT: Number of retries before giving up and fail
  250. * @idt_smb_safe: Generate a retry loop on corresponding SMBus method
  251. */
  252. #define RETRY_CNT (128)
  253. #define idt_smb_safe(ops, args...) ({ \
  254. int __retry = RETRY_CNT; \
  255. s32 __sts; \
  256. do { \
  257. __sts = i2c_smbus_ ## ops ## _data(args); \
  258. } while (__retry-- && __sts < 0); \
  259. __sts; \
  260. })
  261. /*===========================================================================
  262. * i2c bus level IO-operations
  263. *===========================================================================
  264. */
  265. /*
  266. * idt_smb_write_byte() - SMBus write method when I2C_SMBUS_BYTE_DATA operation
  267. * is only available
  268. * @pdev: Pointer to the driver data
  269. * @seq: Sequence of data to be written
  270. */
  271. static int idt_smb_write_byte(struct idt_89hpesx_dev *pdev,
  272. const struct idt_smb_seq *seq)
  273. {
  274. s32 sts;
  275. u8 ccode;
  276. int idx;
  277. /* Loop over the supplied data sending byte one-by-one */
  278. for (idx = 0; idx < seq->bytecnt; idx++) {
  279. /* Collect the command code byte */
  280. ccode = seq->ccode | CCODE_BYTE;
  281. if (idx == 0)
  282. ccode |= CCODE_START;
  283. if (idx == seq->bytecnt - 1)
  284. ccode |= CCODE_END;
  285. /* Send data to the device */
  286. sts = idt_smb_safe(write_byte, pdev->client, ccode,
  287. seq->data[idx]);
  288. if (sts != 0)
  289. return (int)sts;
  290. }
  291. return 0;
  292. }
  293. /*
  294. * idt_smb_read_byte() - SMBus read method when I2C_SMBUS_BYTE_DATA operation
  295. * is only available
  296. * @pdev: Pointer to the driver data
  297. * @seq: Buffer to read data to
  298. */
  299. static int idt_smb_read_byte(struct idt_89hpesx_dev *pdev,
  300. struct idt_smb_seq *seq)
  301. {
  302. s32 sts;
  303. u8 ccode;
  304. int idx;
  305. /* Loop over the supplied buffer receiving byte one-by-one */
  306. for (idx = 0; idx < seq->bytecnt; idx++) {
  307. /* Collect the command code byte */
  308. ccode = seq->ccode | CCODE_BYTE;
  309. if (idx == 0)
  310. ccode |= CCODE_START;
  311. if (idx == seq->bytecnt - 1)
  312. ccode |= CCODE_END;
  313. /* Read data from the device */
  314. sts = idt_smb_safe(read_byte, pdev->client, ccode);
  315. if (sts < 0)
  316. return (int)sts;
  317. seq->data[idx] = (u8)sts;
  318. }
  319. return 0;
  320. }
  321. /*
  322. * idt_smb_write_word() - SMBus write method when I2C_SMBUS_BYTE_DATA and
  323. * I2C_FUNC_SMBUS_WORD_DATA operations are available
  324. * @pdev: Pointer to the driver data
  325. * @seq: Sequence of data to be written
  326. */
  327. static int idt_smb_write_word(struct idt_89hpesx_dev *pdev,
  328. const struct idt_smb_seq *seq)
  329. {
  330. s32 sts;
  331. u8 ccode;
  332. int idx, evencnt;
  333. /* Calculate the even count of data to send */
  334. evencnt = seq->bytecnt - (seq->bytecnt % 2);
  335. /* Loop over the supplied data sending two bytes at a time */
  336. for (idx = 0; idx < evencnt; idx += 2) {
  337. /* Collect the command code byte */
  338. ccode = seq->ccode | CCODE_WORD;
  339. if (idx == 0)
  340. ccode |= CCODE_START;
  341. if (idx == evencnt - 2)
  342. ccode |= CCODE_END;
  343. /* Send word data to the device */
  344. sts = idt_smb_safe(write_word, pdev->client, ccode,
  345. *(u16 *)&seq->data[idx]);
  346. if (sts != 0)
  347. return (int)sts;
  348. }
  349. /* If there is odd number of bytes then send just one last byte */
  350. if (seq->bytecnt != evencnt) {
  351. /* Collect the command code byte */
  352. ccode = seq->ccode | CCODE_BYTE | CCODE_END;
  353. if (idx == 0)
  354. ccode |= CCODE_START;
  355. /* Send byte data to the device */
  356. sts = idt_smb_safe(write_byte, pdev->client, ccode,
  357. seq->data[idx]);
  358. if (sts != 0)
  359. return (int)sts;
  360. }
  361. return 0;
  362. }
  363. /*
  364. * idt_smb_read_word() - SMBus read method when I2C_SMBUS_BYTE_DATA and
  365. * I2C_FUNC_SMBUS_WORD_DATA operations are available
  366. * @pdev: Pointer to the driver data
  367. * @seq: Buffer to read data to
  368. */
  369. static int idt_smb_read_word(struct idt_89hpesx_dev *pdev,
  370. struct idt_smb_seq *seq)
  371. {
  372. s32 sts;
  373. u8 ccode;
  374. int idx, evencnt;
  375. /* Calculate the even count of data to send */
  376. evencnt = seq->bytecnt - (seq->bytecnt % 2);
  377. /* Loop over the supplied data reading two bytes at a time */
  378. for (idx = 0; idx < evencnt; idx += 2) {
  379. /* Collect the command code byte */
  380. ccode = seq->ccode | CCODE_WORD;
  381. if (idx == 0)
  382. ccode |= CCODE_START;
  383. if (idx == evencnt - 2)
  384. ccode |= CCODE_END;
  385. /* Read word data from the device */
  386. sts = idt_smb_safe(read_word, pdev->client, ccode);
  387. if (sts < 0)
  388. return (int)sts;
  389. *(u16 *)&seq->data[idx] = (u16)sts;
  390. }
  391. /* If there is odd number of bytes then receive just one last byte */
  392. if (seq->bytecnt != evencnt) {
  393. /* Collect the command code byte */
  394. ccode = seq->ccode | CCODE_BYTE | CCODE_END;
  395. if (idx == 0)
  396. ccode |= CCODE_START;
  397. /* Read last data byte from the device */
  398. sts = idt_smb_safe(read_byte, pdev->client, ccode);
  399. if (sts < 0)
  400. return (int)sts;
  401. seq->data[idx] = (u8)sts;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * idt_smb_write_block() - SMBus write method when I2C_SMBUS_BLOCK_DATA
  407. * operation is available
  408. * @pdev: Pointer to the driver data
  409. * @seq: Sequence of data to be written
  410. */
  411. static int idt_smb_write_block(struct idt_89hpesx_dev *pdev,
  412. const struct idt_smb_seq *seq)
  413. {
  414. u8 ccode;
  415. /* Return error if too much data passed to send */
  416. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  417. return -EINVAL;
  418. /* Collect the command code byte */
  419. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  420. /* Send block of data to the device */
  421. return idt_smb_safe(write_block, pdev->client, ccode, seq->bytecnt,
  422. seq->data);
  423. }
  424. /*
  425. * idt_smb_read_block() - SMBus read method when I2C_SMBUS_BLOCK_DATA
  426. * operation is available
  427. * @pdev: Pointer to the driver data
  428. * @seq: Buffer to read data to
  429. */
  430. static int idt_smb_read_block(struct idt_89hpesx_dev *pdev,
  431. struct idt_smb_seq *seq)
  432. {
  433. s32 sts;
  434. u8 ccode;
  435. /* Return error if too much data passed to send */
  436. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  437. return -EINVAL;
  438. /* Collect the command code byte */
  439. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  440. /* Read block of data from the device */
  441. sts = idt_smb_safe(read_block, pdev->client, ccode, seq->data);
  442. if (sts != seq->bytecnt)
  443. return (sts < 0 ? sts : -ENODATA);
  444. return 0;
  445. }
  446. /*
  447. * idt_smb_write_i2c_block() - SMBus write method when I2C_SMBUS_I2C_BLOCK_DATA
  448. * operation is available
  449. * @pdev: Pointer to the driver data
  450. * @seq: Sequence of data to be written
  451. *
  452. * NOTE It's usual SMBus write block operation, except the actual data length is
  453. * sent as first byte of data
  454. */
  455. static int idt_smb_write_i2c_block(struct idt_89hpesx_dev *pdev,
  456. const struct idt_smb_seq *seq)
  457. {
  458. u8 ccode, buf[I2C_SMBUS_BLOCK_MAX + 1];
  459. /* Return error if too much data passed to send */
  460. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  461. return -EINVAL;
  462. /* Collect the data to send. Length byte must be added prior the data */
  463. buf[0] = seq->bytecnt;
  464. memcpy(&buf[1], seq->data, seq->bytecnt);
  465. /* Collect the command code byte */
  466. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  467. /* Send length and block of data to the device */
  468. return idt_smb_safe(write_i2c_block, pdev->client, ccode,
  469. seq->bytecnt + 1, buf);
  470. }
  471. /*
  472. * idt_smb_read_i2c_block() - SMBus read method when I2C_SMBUS_I2C_BLOCK_DATA
  473. * operation is available
  474. * @pdev: Pointer to the driver data
  475. * @seq: Buffer to read data to
  476. *
  477. * NOTE It's usual SMBus read block operation, except the actual data length is
  478. * retrieved as first byte of data
  479. */
  480. static int idt_smb_read_i2c_block(struct idt_89hpesx_dev *pdev,
  481. struct idt_smb_seq *seq)
  482. {
  483. u8 ccode, buf[I2C_SMBUS_BLOCK_MAX + 1];
  484. s32 sts;
  485. /* Return error if too much data passed to send */
  486. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  487. return -EINVAL;
  488. /* Collect the command code byte */
  489. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  490. /* Read length and block of data from the device */
  491. sts = idt_smb_safe(read_i2c_block, pdev->client, ccode,
  492. seq->bytecnt + 1, buf);
  493. if (sts != seq->bytecnt + 1)
  494. return (sts < 0 ? sts : -ENODATA);
  495. if (buf[0] != seq->bytecnt)
  496. return -ENODATA;
  497. /* Copy retrieved data to the output data buffer */
  498. memcpy(seq->data, &buf[1], seq->bytecnt);
  499. return 0;
  500. }
  501. /*===========================================================================
  502. * EEPROM IO-operations
  503. *===========================================================================
  504. */
  505. /*
  506. * idt_eeprom_read_byte() - read just one byte from EEPROM
  507. * @pdev: Pointer to the driver data
  508. * @memaddr: Start EEPROM memory address
  509. * @data: Data to be written to EEPROM
  510. */
  511. static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
  512. u8 *data)
  513. {
  514. struct device *dev = &pdev->client->dev;
  515. struct idt_eeprom_seq eeseq;
  516. struct idt_smb_seq smbseq;
  517. int ret, retry;
  518. /* Initialize SMBus sequence fields */
  519. smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
  520. smbseq.data = (u8 *)&eeseq;
  521. /*
  522. * Sometimes EEPROM may respond with NACK if it's busy with previous
  523. * operation, so we need to perform a few attempts of read cycle
  524. */
  525. retry = RETRY_CNT;
  526. do {
  527. /* Send EEPROM memory address to read data from */
  528. smbseq.bytecnt = EEPROM_WRRD_CNT;
  529. eeseq.cmd = pdev->inieecmd | EEPROM_OP_READ;
  530. eeseq.eeaddr = pdev->eeaddr;
  531. eeseq.memaddr = cpu_to_le16(memaddr);
  532. ret = pdev->smb_write(pdev, &smbseq);
  533. if (ret != 0) {
  534. dev_err(dev, "Failed to init eeprom addr 0x%02hhx",
  535. memaddr);
  536. break;
  537. }
  538. /* Perform read operation */
  539. smbseq.bytecnt = EEPROM_RD_CNT;
  540. ret = pdev->smb_read(pdev, &smbseq);
  541. if (ret != 0) {
  542. dev_err(dev, "Failed to read eeprom data 0x%02hhx",
  543. memaddr);
  544. break;
  545. }
  546. /* Restart read operation if the device is busy */
  547. if (retry && (eeseq.cmd & EEPROM_NAERR)) {
  548. dev_dbg(dev, "EEPROM busy, retry reading after %d ms",
  549. EEPROM_TOUT);
  550. msleep(EEPROM_TOUT);
  551. continue;
  552. }
  553. /* Check whether IDT successfully read data from EEPROM */
  554. if (eeseq.cmd & (EEPROM_NAERR | EEPROM_LAERR | EEPROM_MSS)) {
  555. dev_err(dev,
  556. "Communication with eeprom failed, cmd 0x%hhx",
  557. eeseq.cmd);
  558. ret = -EREMOTEIO;
  559. break;
  560. }
  561. /* Save retrieved data and exit the loop */
  562. *data = eeseq.data;
  563. break;
  564. } while (retry--);
  565. /* Return the status of operation */
  566. return ret;
  567. }
  568. /*
  569. * idt_eeprom_write() - EEPROM write operation
  570. * @pdev: Pointer to the driver data
  571. * @memaddr: Start EEPROM memory address
  572. * @len: Length of data to be written
  573. * @data: Data to be written to EEPROM
  574. */
  575. static int idt_eeprom_write(struct idt_89hpesx_dev *pdev, u16 memaddr, u16 len,
  576. const u8 *data)
  577. {
  578. struct device *dev = &pdev->client->dev;
  579. struct idt_eeprom_seq eeseq;
  580. struct idt_smb_seq smbseq;
  581. int ret;
  582. u16 idx;
  583. /* Initialize SMBus sequence fields */
  584. smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
  585. smbseq.data = (u8 *)&eeseq;
  586. /* Send data byte-by-byte, checking if it is successfully written */
  587. for (idx = 0; idx < len; idx++, memaddr++) {
  588. /* Lock IDT SMBus device */
  589. mutex_lock(&pdev->smb_mtx);
  590. /* Perform write operation */
  591. smbseq.bytecnt = EEPROM_WR_CNT;
  592. eeseq.cmd = pdev->inieecmd | EEPROM_OP_WRITE;
  593. eeseq.eeaddr = pdev->eeaddr;
  594. eeseq.memaddr = cpu_to_le16(memaddr);
  595. eeseq.data = data[idx];
  596. ret = pdev->smb_write(pdev, &smbseq);
  597. if (ret != 0) {
  598. dev_err(dev,
  599. "Failed to write 0x%04hx:0x%02hhx to eeprom",
  600. memaddr, data[idx]);
  601. goto err_mutex_unlock;
  602. }
  603. /*
  604. * Check whether the data is successfully written by reading
  605. * from the same EEPROM memory address.
  606. */
  607. eeseq.data = ~data[idx];
  608. ret = idt_eeprom_read_byte(pdev, memaddr, &eeseq.data);
  609. if (ret != 0)
  610. goto err_mutex_unlock;
  611. /* Check whether the read byte is the same as written one */
  612. if (eeseq.data != data[idx]) {
  613. dev_err(dev, "Values don't match 0x%02hhx != 0x%02hhx",
  614. eeseq.data, data[idx]);
  615. ret = -EREMOTEIO;
  616. goto err_mutex_unlock;
  617. }
  618. /* Unlock IDT SMBus device */
  619. err_mutex_unlock:
  620. mutex_unlock(&pdev->smb_mtx);
  621. if (ret != 0)
  622. return ret;
  623. }
  624. return 0;
  625. }
  626. /*
  627. * idt_eeprom_read() - EEPROM read operation
  628. * @pdev: Pointer to the driver data
  629. * @memaddr: Start EEPROM memory address
  630. * @len: Length of data to read
  631. * @buf: Buffer to read data to
  632. */
  633. static int idt_eeprom_read(struct idt_89hpesx_dev *pdev, u16 memaddr, u16 len,
  634. u8 *buf)
  635. {
  636. int ret;
  637. u16 idx;
  638. /* Read data byte-by-byte, retrying if it wasn't successful */
  639. for (idx = 0; idx < len; idx++, memaddr++) {
  640. /* Lock IDT SMBus device */
  641. mutex_lock(&pdev->smb_mtx);
  642. /* Just read the byte to the buffer */
  643. ret = idt_eeprom_read_byte(pdev, memaddr, &buf[idx]);
  644. /* Unlock IDT SMBus device */
  645. mutex_unlock(&pdev->smb_mtx);
  646. /* Return error if read operation failed */
  647. if (ret != 0)
  648. return ret;
  649. }
  650. return 0;
  651. }
  652. /*===========================================================================
  653. * CSR IO-operations
  654. *===========================================================================
  655. */
  656. /*
  657. * idt_csr_write() - CSR write operation
  658. * @pdev: Pointer to the driver data
  659. * @csraddr: CSR address (with no two LS bits)
  660. * @data: Data to be written to CSR
  661. */
  662. static int idt_csr_write(struct idt_89hpesx_dev *pdev, u16 csraddr,
  663. const u32 data)
  664. {
  665. struct device *dev = &pdev->client->dev;
  666. struct idt_csr_seq csrseq;
  667. struct idt_smb_seq smbseq;
  668. int ret;
  669. /* Initialize SMBus sequence fields */
  670. smbseq.ccode = pdev->iniccode | CCODE_CSR;
  671. smbseq.data = (u8 *)&csrseq;
  672. /* Lock IDT SMBus device */
  673. mutex_lock(&pdev->smb_mtx);
  674. /* Perform write operation */
  675. smbseq.bytecnt = CSR_WR_CNT;
  676. csrseq.cmd = pdev->inicsrcmd | CSR_OP_WRITE;
  677. csrseq.csraddr = cpu_to_le16(csraddr);
  678. csrseq.data = cpu_to_le32(data);
  679. ret = pdev->smb_write(pdev, &smbseq);
  680. if (ret != 0) {
  681. dev_err(dev, "Failed to write 0x%04x: 0x%04x to csr",
  682. CSR_REAL_ADDR(csraddr), data);
  683. goto err_mutex_unlock;
  684. }
  685. /* Send CSR address to read data from */
  686. smbseq.bytecnt = CSR_WRRD_CNT;
  687. csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
  688. ret = pdev->smb_write(pdev, &smbseq);
  689. if (ret != 0) {
  690. dev_err(dev, "Failed to init csr address 0x%04x",
  691. CSR_REAL_ADDR(csraddr));
  692. goto err_mutex_unlock;
  693. }
  694. /* Perform read operation */
  695. smbseq.bytecnt = CSR_RD_CNT;
  696. ret = pdev->smb_read(pdev, &smbseq);
  697. if (ret != 0) {
  698. dev_err(dev, "Failed to read csr 0x%04x",
  699. CSR_REAL_ADDR(csraddr));
  700. goto err_mutex_unlock;
  701. }
  702. /* Check whether IDT successfully retrieved CSR data */
  703. if (csrseq.cmd & (CSR_RERR | CSR_WERR)) {
  704. dev_err(dev, "IDT failed to perform CSR r/w");
  705. ret = -EREMOTEIO;
  706. goto err_mutex_unlock;
  707. }
  708. /* Unlock IDT SMBus device */
  709. err_mutex_unlock:
  710. mutex_unlock(&pdev->smb_mtx);
  711. return ret;
  712. }
  713. /*
  714. * idt_csr_read() - CSR read operation
  715. * @pdev: Pointer to the driver data
  716. * @csraddr: CSR address (with no two LS bits)
  717. * @data: Data to be written to CSR
  718. */
  719. static int idt_csr_read(struct idt_89hpesx_dev *pdev, u16 csraddr, u32 *data)
  720. {
  721. struct device *dev = &pdev->client->dev;
  722. struct idt_csr_seq csrseq;
  723. struct idt_smb_seq smbseq;
  724. int ret;
  725. /* Initialize SMBus sequence fields */
  726. smbseq.ccode = pdev->iniccode | CCODE_CSR;
  727. smbseq.data = (u8 *)&csrseq;
  728. /* Lock IDT SMBus device */
  729. mutex_lock(&pdev->smb_mtx);
  730. /* Send CSR register address before reading it */
  731. smbseq.bytecnt = CSR_WRRD_CNT;
  732. csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
  733. csrseq.csraddr = cpu_to_le16(csraddr);
  734. ret = pdev->smb_write(pdev, &smbseq);
  735. if (ret != 0) {
  736. dev_err(dev, "Failed to init csr address 0x%04x",
  737. CSR_REAL_ADDR(csraddr));
  738. goto err_mutex_unlock;
  739. }
  740. /* Perform read operation */
  741. smbseq.bytecnt = CSR_RD_CNT;
  742. ret = pdev->smb_read(pdev, &smbseq);
  743. if (ret != 0) {
  744. dev_err(dev, "Failed to read csr 0x%04hx",
  745. CSR_REAL_ADDR(csraddr));
  746. goto err_mutex_unlock;
  747. }
  748. /* Check whether IDT successfully retrieved CSR data */
  749. if (csrseq.cmd & (CSR_RERR | CSR_WERR)) {
  750. dev_err(dev, "IDT failed to perform CSR r/w");
  751. ret = -EREMOTEIO;
  752. goto err_mutex_unlock;
  753. }
  754. /* Save data retrieved from IDT */
  755. *data = le32_to_cpu(csrseq.data);
  756. /* Unlock IDT SMBus device */
  757. err_mutex_unlock:
  758. mutex_unlock(&pdev->smb_mtx);
  759. return ret;
  760. }
  761. /*===========================================================================
  762. * Sysfs/debugfs-nodes IO-operations
  763. *===========================================================================
  764. */
  765. /*
  766. * eeprom_write() - EEPROM sysfs-node write callback
  767. * @filep: Pointer to the file system node
  768. * @kobj: Pointer to the kernel object related to the sysfs-node
  769. * @attr: Attributes of the file
  770. * @buf: Buffer to write data to
  771. * @off: Offset at which data should be written to
  772. * @count: Number of bytes to write
  773. */
  774. static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
  775. struct bin_attribute *attr,
  776. char *buf, loff_t off, size_t count)
  777. {
  778. struct idt_89hpesx_dev *pdev;
  779. int ret;
  780. /* Retrieve driver data */
  781. pdev = dev_get_drvdata(kobj_to_dev(kobj));
  782. /* Perform EEPROM write operation */
  783. ret = idt_eeprom_write(pdev, (u16)off, (u16)count, (u8 *)buf);
  784. return (ret != 0 ? ret : count);
  785. }
  786. /*
  787. * eeprom_read() - EEPROM sysfs-node read callback
  788. * @filep: Pointer to the file system node
  789. * @kobj: Pointer to the kernel object related to the sysfs-node
  790. * @attr: Attributes of the file
  791. * @buf: Buffer to write data to
  792. * @off: Offset at which data should be written to
  793. * @count: Number of bytes to write
  794. */
  795. static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
  796. struct bin_attribute *attr,
  797. char *buf, loff_t off, size_t count)
  798. {
  799. struct idt_89hpesx_dev *pdev;
  800. int ret;
  801. /* Retrieve driver data */
  802. pdev = dev_get_drvdata(kobj_to_dev(kobj));
  803. /* Perform EEPROM read operation */
  804. ret = idt_eeprom_read(pdev, (u16)off, (u16)count, (u8 *)buf);
  805. return (ret != 0 ? ret : count);
  806. }
  807. /*
  808. * idt_dbgfs_csr_write() - CSR debugfs-node write callback
  809. * @filep: Pointer to the file system file descriptor
  810. * @buf: Buffer to read data from
  811. * @count: Size of the buffer
  812. * @offp: Offset within the file
  813. *
  814. * It accepts either "0x<reg addr>:0x<value>" for saving register address
  815. * and writing value to specified DWORD register or "0x<reg addr>" for
  816. * just saving register address in order to perform next read operation.
  817. *
  818. * WARNING No spaces are allowed. Incoming string must be strictly formated as:
  819. * "<reg addr>:<value>". Register address must be aligned within 4 bytes
  820. * (one DWORD).
  821. */
  822. static ssize_t idt_dbgfs_csr_write(struct file *filep, const char __user *ubuf,
  823. size_t count, loff_t *offp)
  824. {
  825. struct idt_89hpesx_dev *pdev = filep->private_data;
  826. char *colon_ch, *csraddr_str, *csrval_str;
  827. int ret, csraddr_len;
  828. u32 csraddr, csrval;
  829. char *buf;
  830. /* Copy data from User-space */
  831. buf = kmalloc(count + 1, GFP_KERNEL);
  832. if (!buf)
  833. return -ENOMEM;
  834. ret = simple_write_to_buffer(buf, count, offp, ubuf, count);
  835. if (ret < 0)
  836. goto free_buf;
  837. buf[count] = 0;
  838. /* Find position of colon in the buffer */
  839. colon_ch = strnchr(buf, count, ':');
  840. /*
  841. * If there is colon passed then new CSR value should be parsed as
  842. * well, so allocate buffer for CSR address substring.
  843. * If no colon is found, then string must have just one number with
  844. * no new CSR value
  845. */
  846. if (colon_ch != NULL) {
  847. csraddr_len = colon_ch - buf;
  848. csraddr_str =
  849. kmalloc(csraddr_len + 1, GFP_KERNEL);
  850. if (csraddr_str == NULL) {
  851. ret = -ENOMEM;
  852. goto free_buf;
  853. }
  854. /* Copy the register address to the substring buffer */
  855. strncpy(csraddr_str, buf, csraddr_len);
  856. csraddr_str[csraddr_len] = '\0';
  857. /* Register value must follow the colon */
  858. csrval_str = colon_ch + 1;
  859. } else /* if (str_colon == NULL) */ {
  860. csraddr_str = (char *)buf; /* Just to shut warning up */
  861. csraddr_len = strnlen(csraddr_str, count);
  862. csrval_str = NULL;
  863. }
  864. /* Convert CSR address to u32 value */
  865. ret = kstrtou32(csraddr_str, 0, &csraddr);
  866. if (ret != 0)
  867. goto free_csraddr_str;
  868. /* Check whether passed register address is valid */
  869. if (csraddr > CSR_MAX || !IS_ALIGNED(csraddr, SZ_4)) {
  870. ret = -EINVAL;
  871. goto free_csraddr_str;
  872. }
  873. /* Shift register address to the right so to have u16 address */
  874. pdev->csr = (csraddr >> 2);
  875. /* Parse new CSR value and send it to IDT, if colon has been found */
  876. if (colon_ch != NULL) {
  877. ret = kstrtou32(csrval_str, 0, &csrval);
  878. if (ret != 0)
  879. goto free_csraddr_str;
  880. ret = idt_csr_write(pdev, pdev->csr, csrval);
  881. if (ret != 0)
  882. goto free_csraddr_str;
  883. }
  884. /* Free memory only if colon has been found */
  885. free_csraddr_str:
  886. if (colon_ch != NULL)
  887. kfree(csraddr_str);
  888. /* Free buffer allocated for data retrieved from User-space */
  889. free_buf:
  890. kfree(buf);
  891. return (ret != 0 ? ret : count);
  892. }
  893. /*
  894. * idt_dbgfs_csr_read() - CSR debugfs-node read callback
  895. * @filep: Pointer to the file system file descriptor
  896. * @buf: Buffer to write data to
  897. * @count: Size of the buffer
  898. * @offp: Offset within the file
  899. *
  900. * It just prints the pair "0x<reg addr>:0x<value>" to passed buffer.
  901. */
  902. #define CSRBUF_SIZE ((size_t)32)
  903. static ssize_t idt_dbgfs_csr_read(struct file *filep, char __user *ubuf,
  904. size_t count, loff_t *offp)
  905. {
  906. struct idt_89hpesx_dev *pdev = filep->private_data;
  907. u32 csraddr, csrval;
  908. char buf[CSRBUF_SIZE];
  909. int ret, size;
  910. /* Perform CSR read operation */
  911. ret = idt_csr_read(pdev, pdev->csr, &csrval);
  912. if (ret != 0)
  913. return ret;
  914. /* Shift register address to the left so to have real address */
  915. csraddr = ((u32)pdev->csr << 2);
  916. /* Print the "0x<reg addr>:0x<value>" to buffer */
  917. size = snprintf(buf, CSRBUF_SIZE, "0x%05x:0x%08x\n",
  918. (unsigned int)csraddr, (unsigned int)csrval);
  919. /* Copy data to User-space */
  920. return simple_read_from_buffer(ubuf, count, offp, buf, size);
  921. }
  922. /*
  923. * eeprom_attribute - EEPROM sysfs-node attributes
  924. *
  925. * NOTE Size will be changed in compliance with OF node. EEPROM attribute will
  926. * be read-only as well if the corresponding flag is specified in OF node.
  927. */
  928. static BIN_ATTR_RW(eeprom, EEPROM_DEF_SIZE);
  929. /*
  930. * csr_dbgfs_ops - CSR debugfs-node read/write operations
  931. */
  932. static const struct file_operations csr_dbgfs_ops = {
  933. .owner = THIS_MODULE,
  934. .open = simple_open,
  935. .write = idt_dbgfs_csr_write,
  936. .read = idt_dbgfs_csr_read
  937. };
  938. /*===========================================================================
  939. * Driver init/deinit methods
  940. *===========================================================================
  941. */
  942. /*
  943. * idt_set_defval() - disable EEPROM access by default
  944. * @pdev: Pointer to the driver data
  945. */
  946. static void idt_set_defval(struct idt_89hpesx_dev *pdev)
  947. {
  948. /* If OF info is missing then use next values */
  949. pdev->eesize = 0;
  950. pdev->eero = true;
  951. pdev->inieecmd = 0;
  952. pdev->eeaddr = 0;
  953. }
  954. static const struct i2c_device_id ee_ids[];
  955. /*
  956. * idt_ee_match_id() - check whether the node belongs to compatible EEPROMs
  957. */
  958. static const struct i2c_device_id *idt_ee_match_id(struct fwnode_handle *fwnode)
  959. {
  960. const struct i2c_device_id *id = ee_ids;
  961. const char *compatible, *p;
  962. char devname[I2C_NAME_SIZE];
  963. int ret;
  964. ret = fwnode_property_read_string(fwnode, "compatible", &compatible);
  965. if (ret)
  966. return NULL;
  967. p = strchr(compatible, ',');
  968. strlcpy(devname, p ? p + 1 : compatible, sizeof(devname));
  969. /* Search through the device name */
  970. while (id->name[0]) {
  971. if (strcmp(devname, id->name) == 0)
  972. return id;
  973. id++;
  974. }
  975. return NULL;
  976. }
  977. /*
  978. * idt_get_fw_data() - get IDT i2c-device parameters from device tree
  979. * @pdev: Pointer to the driver data
  980. */
  981. static void idt_get_fw_data(struct idt_89hpesx_dev *pdev)
  982. {
  983. struct device *dev = &pdev->client->dev;
  984. struct fwnode_handle *fwnode;
  985. const struct i2c_device_id *ee_id = NULL;
  986. u32 eeprom_addr;
  987. int ret;
  988. device_for_each_child_node(dev, fwnode) {
  989. ee_id = idt_ee_match_id(fwnode);
  990. if (!ee_id) {
  991. dev_warn(dev, "Skip unsupported EEPROM device");
  992. continue;
  993. } else
  994. break;
  995. }
  996. /* If there is no fwnode EEPROM device, then set zero size */
  997. if (!ee_id) {
  998. dev_warn(dev, "No fwnode, EEPROM access disabled");
  999. idt_set_defval(pdev);
  1000. return;
  1001. }
  1002. /* Retrieve EEPROM size */
  1003. pdev->eesize = (u32)ee_id->driver_data;
  1004. /* Get custom EEPROM address from 'reg' attribute */
  1005. ret = fwnode_property_read_u32(fwnode, "reg", &eeprom_addr);
  1006. if (ret || (eeprom_addr == 0)) {
  1007. dev_warn(dev, "No EEPROM reg found, use default address 0x%x",
  1008. EEPROM_DEF_ADDR);
  1009. pdev->inieecmd = 0;
  1010. pdev->eeaddr = EEPROM_DEF_ADDR << 1;
  1011. } else {
  1012. pdev->inieecmd = EEPROM_USA;
  1013. pdev->eeaddr = eeprom_addr << 1;
  1014. }
  1015. /* Check EEPROM 'read-only' flag */
  1016. if (fwnode_property_read_bool(fwnode, "read-only"))
  1017. pdev->eero = true;
  1018. else /* if (!fwnode_property_read_bool(node, "read-only")) */
  1019. pdev->eero = false;
  1020. dev_info(dev, "EEPROM of %d bytes found by 0x%x",
  1021. pdev->eesize, pdev->eeaddr);
  1022. }
  1023. /*
  1024. * idt_create_pdev() - create and init data structure of the driver
  1025. * @client: i2c client of IDT PCIe-switch device
  1026. */
  1027. static struct idt_89hpesx_dev *idt_create_pdev(struct i2c_client *client)
  1028. {
  1029. struct idt_89hpesx_dev *pdev;
  1030. /* Allocate memory for driver data */
  1031. pdev = devm_kmalloc(&client->dev, sizeof(struct idt_89hpesx_dev),
  1032. GFP_KERNEL);
  1033. if (pdev == NULL)
  1034. return ERR_PTR(-ENOMEM);
  1035. /* Initialize basic fields of the data */
  1036. pdev->client = client;
  1037. i2c_set_clientdata(client, pdev);
  1038. /* Read firmware nodes information */
  1039. idt_get_fw_data(pdev);
  1040. /* Initialize basic CSR CMD field - use full DWORD-sized r/w ops */
  1041. pdev->inicsrcmd = CSR_DWE;
  1042. pdev->csr = CSR_DEF;
  1043. /* Enable Packet Error Checking if it's supported by adapter */
  1044. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) {
  1045. pdev->iniccode = CCODE_PEC;
  1046. client->flags |= I2C_CLIENT_PEC;
  1047. } else /* PEC is unsupported */ {
  1048. pdev->iniccode = 0;
  1049. }
  1050. return pdev;
  1051. }
  1052. /*
  1053. * idt_free_pdev() - free data structure of the driver
  1054. * @pdev: Pointer to the driver data
  1055. */
  1056. static void idt_free_pdev(struct idt_89hpesx_dev *pdev)
  1057. {
  1058. /* Clear driver data from device private field */
  1059. i2c_set_clientdata(pdev->client, NULL);
  1060. }
  1061. /*
  1062. * idt_set_smbus_ops() - set supported SMBus operations
  1063. * @pdev: Pointer to the driver data
  1064. * Return status of smbus check operations
  1065. */
  1066. static int idt_set_smbus_ops(struct idt_89hpesx_dev *pdev)
  1067. {
  1068. struct i2c_adapter *adapter = pdev->client->adapter;
  1069. struct device *dev = &pdev->client->dev;
  1070. /* Check i2c adapter read functionality */
  1071. if (i2c_check_functionality(adapter,
  1072. I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
  1073. pdev->smb_read = idt_smb_read_block;
  1074. dev_dbg(dev, "SMBus block-read op chosen");
  1075. } else if (i2c_check_functionality(adapter,
  1076. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  1077. pdev->smb_read = idt_smb_read_i2c_block;
  1078. dev_dbg(dev, "SMBus i2c-block-read op chosen");
  1079. } else if (i2c_check_functionality(adapter,
  1080. I2C_FUNC_SMBUS_READ_WORD_DATA) &&
  1081. i2c_check_functionality(adapter,
  1082. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  1083. pdev->smb_read = idt_smb_read_word;
  1084. dev_warn(dev, "Use slow word/byte SMBus read ops");
  1085. } else if (i2c_check_functionality(adapter,
  1086. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  1087. pdev->smb_read = idt_smb_read_byte;
  1088. dev_warn(dev, "Use slow byte SMBus read op");
  1089. } else /* no supported smbus read operations */ {
  1090. dev_err(dev, "No supported SMBus read op");
  1091. return -EPFNOSUPPORT;
  1092. }
  1093. /* Check i2c adapter write functionality */
  1094. if (i2c_check_functionality(adapter,
  1095. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)) {
  1096. pdev->smb_write = idt_smb_write_block;
  1097. dev_dbg(dev, "SMBus block-write op chosen");
  1098. } else if (i2c_check_functionality(adapter,
  1099. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  1100. pdev->smb_write = idt_smb_write_i2c_block;
  1101. dev_dbg(dev, "SMBus i2c-block-write op chosen");
  1102. } else if (i2c_check_functionality(adapter,
  1103. I2C_FUNC_SMBUS_WRITE_WORD_DATA) &&
  1104. i2c_check_functionality(adapter,
  1105. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  1106. pdev->smb_write = idt_smb_write_word;
  1107. dev_warn(dev, "Use slow word/byte SMBus write op");
  1108. } else if (i2c_check_functionality(adapter,
  1109. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  1110. pdev->smb_write = idt_smb_write_byte;
  1111. dev_warn(dev, "Use slow byte SMBus write op");
  1112. } else /* no supported smbus write operations */ {
  1113. dev_err(dev, "No supported SMBus write op");
  1114. return -EPFNOSUPPORT;
  1115. }
  1116. /* Initialize IDT SMBus slave interface mutex */
  1117. mutex_init(&pdev->smb_mtx);
  1118. return 0;
  1119. }
  1120. /*
  1121. * idt_check_dev() - check whether it's really IDT 89HPESx device
  1122. * @pdev: Pointer to the driver data
  1123. * Return status of i2c adapter check operation
  1124. */
  1125. static int idt_check_dev(struct idt_89hpesx_dev *pdev)
  1126. {
  1127. struct device *dev = &pdev->client->dev;
  1128. u32 viddid;
  1129. int ret;
  1130. /* Read VID and DID directly from IDT memory space */
  1131. ret = idt_csr_read(pdev, IDT_VIDDID_CSR, &viddid);
  1132. if (ret != 0) {
  1133. dev_err(dev, "Failed to read VID/DID");
  1134. return ret;
  1135. }
  1136. /* Check whether it's IDT device */
  1137. if ((viddid & IDT_VID_MASK) != PCI_VENDOR_ID_IDT) {
  1138. dev_err(dev, "Got unsupported VID/DID: 0x%08x", viddid);
  1139. return -ENODEV;
  1140. }
  1141. dev_info(dev, "Found IDT 89HPES device VID:0x%04x, DID:0x%04x",
  1142. (viddid & IDT_VID_MASK), (viddid >> 16));
  1143. return 0;
  1144. }
  1145. /*
  1146. * idt_create_sysfs_files() - create sysfs attribute files
  1147. * @pdev: Pointer to the driver data
  1148. * Return status of operation
  1149. */
  1150. static int idt_create_sysfs_files(struct idt_89hpesx_dev *pdev)
  1151. {
  1152. struct device *dev = &pdev->client->dev;
  1153. int ret;
  1154. /* Don't do anything if EEPROM isn't accessible */
  1155. if (pdev->eesize == 0) {
  1156. dev_dbg(dev, "Skip creating sysfs-files");
  1157. return 0;
  1158. }
  1159. /* Allocate memory for attribute file */
  1160. pdev->ee_file = devm_kmalloc(dev, sizeof(*pdev->ee_file), GFP_KERNEL);
  1161. if (!pdev->ee_file)
  1162. return -ENOMEM;
  1163. /* Copy the declared EEPROM attr structure to change some of fields */
  1164. memcpy(pdev->ee_file, &bin_attr_eeprom, sizeof(*pdev->ee_file));
  1165. /* In case of read-only EEPROM get rid of write ability */
  1166. if (pdev->eero) {
  1167. pdev->ee_file->attr.mode &= ~0200;
  1168. pdev->ee_file->write = NULL;
  1169. }
  1170. /* Create EEPROM sysfs file */
  1171. pdev->ee_file->size = pdev->eesize;
  1172. ret = sysfs_create_bin_file(&dev->kobj, pdev->ee_file);
  1173. if (ret != 0) {
  1174. dev_err(dev, "Failed to create EEPROM sysfs-node");
  1175. return ret;
  1176. }
  1177. return 0;
  1178. }
  1179. /*
  1180. * idt_remove_sysfs_files() - remove sysfs attribute files
  1181. * @pdev: Pointer to the driver data
  1182. */
  1183. static void idt_remove_sysfs_files(struct idt_89hpesx_dev *pdev)
  1184. {
  1185. struct device *dev = &pdev->client->dev;
  1186. /* Don't do anything if EEPROM wasn't accessible */
  1187. if (pdev->eesize == 0)
  1188. return;
  1189. /* Remove EEPROM sysfs file */
  1190. sysfs_remove_bin_file(&dev->kobj, pdev->ee_file);
  1191. }
  1192. /*
  1193. * idt_create_dbgfs_files() - create debugfs files
  1194. * @pdev: Pointer to the driver data
  1195. */
  1196. #define CSRNAME_LEN ((size_t)32)
  1197. static void idt_create_dbgfs_files(struct idt_89hpesx_dev *pdev)
  1198. {
  1199. struct i2c_client *cli = pdev->client;
  1200. char fname[CSRNAME_LEN];
  1201. /* Create Debugfs directory for CSR file */
  1202. snprintf(fname, CSRNAME_LEN, "%d-%04hx", cli->adapter->nr, cli->addr);
  1203. pdev->csr_dir = debugfs_create_dir(fname, csr_dbgdir);
  1204. /* Create Debugfs file for CSR read/write operations */
  1205. pdev->csr_file = debugfs_create_file(cli->name, 0600,
  1206. pdev->csr_dir, pdev, &csr_dbgfs_ops);
  1207. }
  1208. /*
  1209. * idt_remove_dbgfs_files() - remove debugfs files
  1210. * @pdev: Pointer to the driver data
  1211. */
  1212. static void idt_remove_dbgfs_files(struct idt_89hpesx_dev *pdev)
  1213. {
  1214. /* Remove CSR directory and it sysfs-node */
  1215. debugfs_remove_recursive(pdev->csr_dir);
  1216. }
  1217. /*
  1218. * idt_probe() - IDT 89HPESx driver probe() callback method
  1219. */
  1220. static int idt_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1221. {
  1222. struct idt_89hpesx_dev *pdev;
  1223. int ret;
  1224. /* Create driver data */
  1225. pdev = idt_create_pdev(client);
  1226. if (IS_ERR(pdev))
  1227. return PTR_ERR(pdev);
  1228. /* Set SMBus operations */
  1229. ret = idt_set_smbus_ops(pdev);
  1230. if (ret != 0)
  1231. goto err_free_pdev;
  1232. /* Check whether it is truly IDT 89HPESx device */
  1233. ret = idt_check_dev(pdev);
  1234. if (ret != 0)
  1235. goto err_free_pdev;
  1236. /* Create sysfs files */
  1237. ret = idt_create_sysfs_files(pdev);
  1238. if (ret != 0)
  1239. goto err_free_pdev;
  1240. /* Create debugfs files */
  1241. idt_create_dbgfs_files(pdev);
  1242. return 0;
  1243. err_free_pdev:
  1244. idt_free_pdev(pdev);
  1245. return ret;
  1246. }
  1247. /*
  1248. * idt_remove() - IDT 89HPESx driver remove() callback method
  1249. */
  1250. static int idt_remove(struct i2c_client *client)
  1251. {
  1252. struct idt_89hpesx_dev *pdev = i2c_get_clientdata(client);
  1253. /* Remove debugfs files first */
  1254. idt_remove_dbgfs_files(pdev);
  1255. /* Remove sysfs files */
  1256. idt_remove_sysfs_files(pdev);
  1257. /* Discard driver data structure */
  1258. idt_free_pdev(pdev);
  1259. return 0;
  1260. }
  1261. /*
  1262. * ee_ids - array of supported EEPROMs
  1263. */
  1264. static const struct i2c_device_id ee_ids[] = {
  1265. { "24c32", 4096},
  1266. { "24c64", 8192},
  1267. { "24c128", 16384},
  1268. { "24c256", 32768},
  1269. { "24c512", 65536},
  1270. {}
  1271. };
  1272. MODULE_DEVICE_TABLE(i2c, ee_ids);
  1273. /*
  1274. * idt_ids - supported IDT 89HPESx devices
  1275. */
  1276. static const struct i2c_device_id idt_ids[] = {
  1277. { "89hpes8nt2", 0 },
  1278. { "89hpes12nt3", 0 },
  1279. { "89hpes24nt6ag2", 0 },
  1280. { "89hpes32nt8ag2", 0 },
  1281. { "89hpes32nt8bg2", 0 },
  1282. { "89hpes12nt12g2", 0 },
  1283. { "89hpes16nt16g2", 0 },
  1284. { "89hpes24nt24g2", 0 },
  1285. { "89hpes32nt24ag2", 0 },
  1286. { "89hpes32nt24bg2", 0 },
  1287. { "89hpes12n3", 0 },
  1288. { "89hpes12n3a", 0 },
  1289. { "89hpes24n3", 0 },
  1290. { "89hpes24n3a", 0 },
  1291. { "89hpes32h8", 0 },
  1292. { "89hpes32h8g2", 0 },
  1293. { "89hpes48h12", 0 },
  1294. { "89hpes48h12g2", 0 },
  1295. { "89hpes48h12ag2", 0 },
  1296. { "89hpes16h16", 0 },
  1297. { "89hpes22h16", 0 },
  1298. { "89hpes22h16g2", 0 },
  1299. { "89hpes34h16", 0 },
  1300. { "89hpes34h16g2", 0 },
  1301. { "89hpes64h16", 0 },
  1302. { "89hpes64h16g2", 0 },
  1303. { "89hpes64h16ag2", 0 },
  1304. /* { "89hpes3t3", 0 }, // No SMBus-slave iface */
  1305. { "89hpes12t3g2", 0 },
  1306. { "89hpes24t3g2", 0 },
  1307. /* { "89hpes4t4", 0 }, // No SMBus-slave iface */
  1308. { "89hpes16t4", 0 },
  1309. { "89hpes4t4g2", 0 },
  1310. { "89hpes10t4g2", 0 },
  1311. { "89hpes16t4g2", 0 },
  1312. { "89hpes16t4ag2", 0 },
  1313. { "89hpes5t5", 0 },
  1314. { "89hpes6t5", 0 },
  1315. { "89hpes8t5", 0 },
  1316. { "89hpes8t5a", 0 },
  1317. { "89hpes24t6", 0 },
  1318. { "89hpes6t6g2", 0 },
  1319. { "89hpes24t6g2", 0 },
  1320. { "89hpes16t7", 0 },
  1321. { "89hpes32t8", 0 },
  1322. { "89hpes32t8g2", 0 },
  1323. { "89hpes48t12", 0 },
  1324. { "89hpes48t12g2", 0 },
  1325. { /* END OF LIST */ }
  1326. };
  1327. MODULE_DEVICE_TABLE(i2c, idt_ids);
  1328. static const struct of_device_id idt_of_match[] = {
  1329. { .compatible = "idt,89hpes8nt2", },
  1330. { .compatible = "idt,89hpes12nt3", },
  1331. { .compatible = "idt,89hpes24nt6ag2", },
  1332. { .compatible = "idt,89hpes32nt8ag2", },
  1333. { .compatible = "idt,89hpes32nt8bg2", },
  1334. { .compatible = "idt,89hpes12nt12g2", },
  1335. { .compatible = "idt,89hpes16nt16g2", },
  1336. { .compatible = "idt,89hpes24nt24g2", },
  1337. { .compatible = "idt,89hpes32nt24ag2", },
  1338. { .compatible = "idt,89hpes32nt24bg2", },
  1339. { .compatible = "idt,89hpes12n3", },
  1340. { .compatible = "idt,89hpes12n3a", },
  1341. { .compatible = "idt,89hpes24n3", },
  1342. { .compatible = "idt,89hpes24n3a", },
  1343. { .compatible = "idt,89hpes32h8", },
  1344. { .compatible = "idt,89hpes32h8g2", },
  1345. { .compatible = "idt,89hpes48h12", },
  1346. { .compatible = "idt,89hpes48h12g2", },
  1347. { .compatible = "idt,89hpes48h12ag2", },
  1348. { .compatible = "idt,89hpes16h16", },
  1349. { .compatible = "idt,89hpes22h16", },
  1350. { .compatible = "idt,89hpes22h16g2", },
  1351. { .compatible = "idt,89hpes34h16", },
  1352. { .compatible = "idt,89hpes34h16g2", },
  1353. { .compatible = "idt,89hpes64h16", },
  1354. { .compatible = "idt,89hpes64h16g2", },
  1355. { .compatible = "idt,89hpes64h16ag2", },
  1356. { .compatible = "idt,89hpes12t3g2", },
  1357. { .compatible = "idt,89hpes24t3g2", },
  1358. { .compatible = "idt,89hpes16t4", },
  1359. { .compatible = "idt,89hpes4t4g2", },
  1360. { .compatible = "idt,89hpes10t4g2", },
  1361. { .compatible = "idt,89hpes16t4g2", },
  1362. { .compatible = "idt,89hpes16t4ag2", },
  1363. { .compatible = "idt,89hpes5t5", },
  1364. { .compatible = "idt,89hpes6t5", },
  1365. { .compatible = "idt,89hpes8t5", },
  1366. { .compatible = "idt,89hpes8t5a", },
  1367. { .compatible = "idt,89hpes24t6", },
  1368. { .compatible = "idt,89hpes6t6g2", },
  1369. { .compatible = "idt,89hpes24t6g2", },
  1370. { .compatible = "idt,89hpes16t7", },
  1371. { .compatible = "idt,89hpes32t8", },
  1372. { .compatible = "idt,89hpes32t8g2", },
  1373. { .compatible = "idt,89hpes48t12", },
  1374. { .compatible = "idt,89hpes48t12g2", },
  1375. { },
  1376. };
  1377. MODULE_DEVICE_TABLE(of, idt_of_match);
  1378. /*
  1379. * idt_driver - IDT 89HPESx driver structure
  1380. */
  1381. static struct i2c_driver idt_driver = {
  1382. .driver = {
  1383. .name = IDT_NAME,
  1384. .of_match_table = idt_of_match,
  1385. },
  1386. .probe = idt_probe,
  1387. .remove = idt_remove,
  1388. .id_table = idt_ids,
  1389. };
  1390. /*
  1391. * idt_init() - IDT 89HPESx driver init() callback method
  1392. */
  1393. static int __init idt_init(void)
  1394. {
  1395. /* Create Debugfs directory first */
  1396. if (debugfs_initialized())
  1397. csr_dbgdir = debugfs_create_dir("idt_csr", NULL);
  1398. /* Add new i2c-device driver */
  1399. return i2c_add_driver(&idt_driver);
  1400. }
  1401. module_init(idt_init);
  1402. /*
  1403. * idt_exit() - IDT 89HPESx driver exit() callback method
  1404. */
  1405. static void __exit idt_exit(void)
  1406. {
  1407. /* Discard debugfs directory and all files if any */
  1408. debugfs_remove_recursive(csr_dbgdir);
  1409. /* Unregister i2c-device driver */
  1410. i2c_del_driver(&idt_driver);
  1411. }
  1412. module_exit(idt_exit);