idt_89hpesx.c 43 KB

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