dcc_v2.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/bitops.h>
  14. #include <linux/cdev.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/fs.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include <soc/qcom/memory_dump.h>
  24. #include <soc/qcom/scm.h>
  25. #include <dt-bindings/soc/qcom,dcc_v2.h>
  26. #define TIMEOUT_US (100)
  27. #define BM(lsb, msb) ((BIT(msb) - BIT(lsb)) + BIT(msb))
  28. #define BMVAL(val, lsb, msb) ((val & BM(lsb, msb)) >> lsb)
  29. #define BVAL(val, n) ((val & BIT(n)) >> n)
  30. #define dcc_writel(drvdata, val, off) \
  31. __raw_writel((val), drvdata->base + off)
  32. #define dcc_readl(drvdata, off) \
  33. __raw_readl(drvdata->base + off)
  34. #define dcc_sram_writel(drvdata, val, off) \
  35. __raw_writel((val), drvdata->ram_base + off)
  36. #define dcc_sram_readl(drvdata, off) \
  37. __raw_readl(drvdata->ram_base + off)
  38. #define HLOS_LIST_START 1
  39. /* DCC registers */
  40. #define DCC_HW_VERSION (0x00)
  41. #define DCC_HW_INFO (0x04)
  42. #define DCC_EXEC_CTRL (0x08)
  43. #define DCC_STATUS (0x0C)
  44. #define DCC_CFG (0x10)
  45. #define DCC_FDA_CURR (0x14)
  46. #define DCC_LLA_CURR (0x18)
  47. #define DCC_LL_LOCK(m) (0x1C + 0x80 * (m + HLOS_LIST_START))
  48. #define DCC_LL_CFG(m) (0x20 + 0x80 * (m + HLOS_LIST_START))
  49. #define DCC_LL_BASE(m) (0x24 + 0x80 * (m + HLOS_LIST_START))
  50. #define DCC_FD_BASE(m) (0x28 + 0x80 * (m + HLOS_LIST_START))
  51. #define DCC_LL_TIMEOUT(m) (0x2c + 0x80 * (m + HLOS_LIST_START))
  52. #define DCC_LL_INT_ENABLE(m) (0x30 + 0x80 * (m + HLOS_LIST_START))
  53. #define DCC_LL_INT_STATUS(m) (0x34 + 0x80 * (m + HLOS_LIST_START))
  54. #define DCC_FDA_CAPTURED(m) (0x38 + 0x80 * (m + HLOS_LIST_START))
  55. #define DCC_LLA_CAPTURED(m) (0x3C + 0x80 * (m + HLOS_LIST_START))
  56. #define DCC_LL_CRC_CAPTURED(m) (0x40 + 0x80 * (m + HLOS_LIST_START))
  57. #define DCC_LL_SW_TRIGGER(m) (0x44 + 0x80 * (m + HLOS_LIST_START))
  58. #define DCC_LL_BUS_ACCESS_STATUS(m) (0x48 + 0x80 * (m + HLOS_LIST_START))
  59. #define DCC_REG_DUMP_MAGIC_V2 (0x42445953)
  60. #define DCC_REG_DUMP_VER (1)
  61. #define MAX_DCC_OFFSET (0xFF * 4)
  62. #define MAX_DCC_LEN 0x7F
  63. #define MAX_LOOP_CNT 0xFF
  64. #define DCC_ADDR_DESCRIPTOR 0x00
  65. #define DCC_LOOP_DESCRIPTOR (BIT(30))
  66. #define DCC_RD_MOD_WR_DESCRIPTOR (BIT(31))
  67. #define DCC_LINK_DESCRIPTOR (BIT(31) | BIT(30))
  68. #define DCC_READ_IND 0x00
  69. #define DCC_WRITE_IND (BIT(28))
  70. #define DCC_AHB_IND 0x00
  71. #define DCC_APB_IND BIT(29)
  72. #define DCC_MAX_LINK_LIST 5
  73. #define DCC_INVALID_LINK_LIST 0xFF
  74. enum dcc_func_type {
  75. DCC_FUNC_TYPE_CAPTURE,
  76. DCC_FUNC_TYPE_CRC,
  77. };
  78. static const char * const str_dcc_func_type[] = {
  79. [DCC_FUNC_TYPE_CAPTURE] = "cap",
  80. [DCC_FUNC_TYPE_CRC] = "crc",
  81. };
  82. enum dcc_data_sink {
  83. DCC_DATA_SINK_SRAM,
  84. DCC_DATA_SINK_ATB
  85. };
  86. enum dcc_descriptor_type {
  87. DCC_ADDR_TYPE,
  88. DCC_LOOP_TYPE,
  89. DCC_READ_WRITE_TYPE,
  90. DCC_WRITE_TYPE
  91. };
  92. static const char * const str_dcc_data_sink[] = {
  93. [DCC_DATA_SINK_SRAM] = "sram",
  94. [DCC_DATA_SINK_ATB] = "atb",
  95. };
  96. struct rpm_trig_req {
  97. uint32_t enable;
  98. uint32_t reserved;
  99. };
  100. struct dcc_config_entry {
  101. uint32_t base;
  102. uint32_t offset;
  103. uint32_t len;
  104. uint32_t index;
  105. uint32_t loop_cnt;
  106. uint32_t write_val;
  107. uint32_t mask;
  108. bool apb_bus;
  109. enum dcc_descriptor_type desc_type;
  110. struct list_head list;
  111. };
  112. struct dcc_drvdata {
  113. void __iomem *base;
  114. uint32_t reg_size;
  115. struct device *dev;
  116. struct mutex mutex;
  117. void __iomem *ram_base;
  118. uint32_t ram_size;
  119. uint32_t ram_offset;
  120. enum dcc_data_sink data_sink;
  121. enum dcc_func_type func_type[DCC_MAX_LINK_LIST];
  122. uint32_t ram_cfg;
  123. uint32_t ram_start;
  124. bool enable[DCC_MAX_LINK_LIST];
  125. bool configured[DCC_MAX_LINK_LIST];
  126. bool interrupt_disable;
  127. char *sram_node;
  128. struct cdev sram_dev;
  129. struct class *sram_class;
  130. struct list_head cfg_head[DCC_MAX_LINK_LIST];
  131. uint32_t nr_config[DCC_MAX_LINK_LIST];
  132. uint8_t curr_list;
  133. uint8_t cti_trig;
  134. };
  135. static bool dcc_ready(struct dcc_drvdata *drvdata)
  136. {
  137. uint32_t val;
  138. /* poll until DCC ready */
  139. if (!readl_poll_timeout((drvdata->base + DCC_STATUS), val,
  140. (BMVAL(val, 0, 1) == 0), 1, TIMEOUT_US))
  141. return true;
  142. return false;
  143. }
  144. static int dcc_read_status(struct dcc_drvdata *drvdata)
  145. {
  146. int curr_list;
  147. uint32_t bus_status;
  148. for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) {
  149. if (!drvdata->enable[curr_list])
  150. continue;
  151. bus_status = dcc_readl(drvdata,
  152. DCC_LL_BUS_ACCESS_STATUS(curr_list));
  153. if (bus_status) {
  154. dev_err(drvdata->dev,
  155. "Read access error for list %d err: 0x%x",
  156. curr_list, bus_status);
  157. dcc_writel(drvdata, 0x3,
  158. DCC_LL_BUS_ACCESS_STATUS(curr_list));
  159. return -ENODATA;
  160. }
  161. }
  162. return 0;
  163. }
  164. static int dcc_sw_trigger(struct dcc_drvdata *drvdata)
  165. {
  166. int ret = 0;
  167. int curr_list;
  168. mutex_lock(&drvdata->mutex);
  169. if (!dcc_ready(drvdata)) {
  170. dev_err(drvdata->dev, "DCC is not ready\n");
  171. ret = -EBUSY;
  172. goto err;
  173. }
  174. for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) {
  175. if (!drvdata->enable[curr_list])
  176. continue;
  177. dcc_writel(drvdata, 1, DCC_LL_SW_TRIGGER(curr_list));
  178. }
  179. if (!dcc_ready(drvdata)) {
  180. dev_err(drvdata->dev,
  181. "DCC is busy after receiving sw tigger.\n");
  182. ret = -EBUSY;
  183. goto err;
  184. }
  185. ret = dcc_read_status(drvdata);
  186. err:
  187. mutex_unlock(&drvdata->mutex);
  188. return ret;
  189. }
  190. static int __dcc_ll_cfg(struct dcc_drvdata *drvdata, int curr_list)
  191. {
  192. int ret = 0;
  193. uint32_t sram_offset = drvdata->ram_cfg * 4;
  194. uint32_t prev_addr, addr;
  195. uint32_t prev_off = 0, off;
  196. uint32_t loop_off = 0;
  197. uint32_t link;
  198. uint32_t pos, total_len = 0, loop_len = 0;
  199. uint32_t loop, loop_cnt = 0;
  200. bool loop_start = false;
  201. struct dcc_config_entry *entry;
  202. prev_addr = 0;
  203. addr = 0;
  204. link = 0;
  205. list_for_each_entry(entry, &drvdata->cfg_head[curr_list], list) {
  206. switch (entry->desc_type) {
  207. case DCC_READ_WRITE_TYPE:
  208. {
  209. if (link) {
  210. /* write new offset = 1 to continue
  211. * processing the list
  212. */
  213. link |= ((0x1 << 8) & BM(8, 14));
  214. dcc_sram_writel(drvdata, link, sram_offset);
  215. sram_offset += 4;
  216. /* Reset link and prev_off */
  217. addr = 0x00;
  218. link = 0;
  219. prev_off = 0;
  220. prev_addr = addr;
  221. }
  222. addr = DCC_RD_MOD_WR_DESCRIPTOR;
  223. dcc_sram_writel(drvdata, addr, sram_offset);
  224. sram_offset += 4;
  225. dcc_sram_writel(drvdata, entry->mask, sram_offset);
  226. sram_offset += 4;
  227. dcc_sram_writel(drvdata, entry->write_val, sram_offset);
  228. sram_offset += 4;
  229. addr = 0;
  230. break;
  231. }
  232. case DCC_LOOP_TYPE:
  233. {
  234. /* Check if we need to write link of prev entry */
  235. if (link) {
  236. dcc_sram_writel(drvdata, link, sram_offset);
  237. sram_offset += 4;
  238. }
  239. if (loop_start) {
  240. loop = (sram_offset - loop_off) / 4;
  241. loop |= (loop_cnt << 13) & BM(13, 27);
  242. loop |= DCC_LOOP_DESCRIPTOR;
  243. total_len += (total_len - loop_len) * loop_cnt;
  244. dcc_sram_writel(drvdata, loop, sram_offset);
  245. sram_offset += 4;
  246. loop_start = false;
  247. loop_len = 0;
  248. loop_off = 0;
  249. } else {
  250. loop_start = true;
  251. loop_cnt = entry->loop_cnt - 1;
  252. loop_len = total_len;
  253. loop_off = sram_offset;
  254. }
  255. /* Reset link and prev_off */
  256. addr = 0x00;
  257. link = 0;
  258. prev_off = 0;
  259. prev_addr = addr;
  260. break;
  261. }
  262. case DCC_WRITE_TYPE:
  263. {
  264. if (link) {
  265. /* write new offset = 1 to continue
  266. * processing the list
  267. */
  268. link |= ((0x1 << 8) & BM(8, 14));
  269. dcc_sram_writel(drvdata, link, sram_offset);
  270. sram_offset += 4;
  271. /* Reset link and prev_off */
  272. addr = 0x00;
  273. prev_off = 0;
  274. prev_addr = addr;
  275. }
  276. off = entry->offset/4;
  277. /* write new offset-length pair to correct position */
  278. link |= ((off & BM(0, 7)) | BIT(15) |
  279. ((entry->len << 8) & BM(8, 14)));
  280. link |= DCC_LINK_DESCRIPTOR;
  281. /* Address type */
  282. addr = (entry->base >> 4) & BM(0, 27);
  283. if (entry->apb_bus)
  284. addr |= DCC_ADDR_DESCRIPTOR | DCC_WRITE_IND
  285. | DCC_APB_IND;
  286. else
  287. addr |= DCC_ADDR_DESCRIPTOR | DCC_WRITE_IND
  288. | DCC_AHB_IND;
  289. dcc_sram_writel(drvdata, addr, sram_offset);
  290. sram_offset += 4;
  291. dcc_sram_writel(drvdata, link, sram_offset);
  292. sram_offset += 4;
  293. dcc_sram_writel(drvdata, entry->write_val, sram_offset);
  294. sram_offset += 4;
  295. addr = 0x00;
  296. link = 0;
  297. break;
  298. }
  299. default:
  300. {
  301. /* Address type */
  302. addr = (entry->base >> 4) & BM(0, 27);
  303. if (entry->apb_bus)
  304. addr |= DCC_ADDR_DESCRIPTOR | DCC_READ_IND
  305. | DCC_APB_IND;
  306. else
  307. addr |= DCC_ADDR_DESCRIPTOR | DCC_READ_IND
  308. | DCC_AHB_IND;
  309. off = entry->offset/4;
  310. total_len += entry->len * 4;
  311. if (!prev_addr || prev_addr != addr || prev_off > off) {
  312. /* Check if we need to write prev link entry */
  313. if (link) {
  314. dcc_sram_writel(drvdata,
  315. link, sram_offset);
  316. sram_offset += 4;
  317. }
  318. dev_dbg(drvdata->dev,
  319. "DCC: sram address 0x%x\n",
  320. sram_offset);
  321. /* Write address */
  322. dcc_sram_writel(drvdata, addr, sram_offset);
  323. sram_offset += 4;
  324. /* Reset link and prev_off */
  325. link = 0;
  326. prev_off = 0;
  327. }
  328. if ((off - prev_off) > 0xFF ||
  329. entry->len > MAX_DCC_LEN) {
  330. dev_err(drvdata->dev,
  331. "DCC: Progamming error Base: 0x%x, offset 0x%x\n",
  332. entry->base, entry->offset);
  333. ret = -EINVAL;
  334. goto err;
  335. }
  336. if (link) {
  337. /*
  338. * link already has one offset-length so new
  339. * offset-length needs to be placed at
  340. * bits [29:15]
  341. */
  342. pos = 15;
  343. /* Clear bits [31:16] */
  344. link &= BM(0, 14);
  345. } else {
  346. /*
  347. * link is empty, so new offset-length needs
  348. * to be placed at bits [15:0]
  349. */
  350. pos = 0;
  351. link = 1 << 15;
  352. }
  353. /* write new offset-length pair to correct position */
  354. link |= (((off-prev_off) & BM(0, 7)) |
  355. ((entry->len << 8) & BM(8, 14))) << pos;
  356. link |= DCC_LINK_DESCRIPTOR;
  357. if (pos) {
  358. dcc_sram_writel(drvdata, link, sram_offset);
  359. sram_offset += 4;
  360. link = 0;
  361. }
  362. prev_off = off + entry->len - 1;
  363. prev_addr = addr;
  364. }
  365. }
  366. }
  367. if (link) {
  368. dcc_sram_writel(drvdata, link, sram_offset);
  369. sram_offset += 4;
  370. }
  371. if (loop_start) {
  372. dev_err(drvdata->dev,
  373. "DCC: Progamming error: Loop unterminated\n");
  374. ret = -EINVAL;
  375. goto err;
  376. }
  377. /* Handling special case of list ending with a rd_mod_wr */
  378. if (addr == DCC_RD_MOD_WR_DESCRIPTOR) {
  379. addr = (0xC105E) & BM(0, 27);
  380. addr |= DCC_ADDR_DESCRIPTOR;
  381. dcc_sram_writel(drvdata, addr, sram_offset);
  382. sram_offset += 4;
  383. }
  384. /* Setting zero to indicate end of the list */
  385. link = DCC_LINK_DESCRIPTOR;
  386. dcc_sram_writel(drvdata, link, sram_offset);
  387. sram_offset += 4;
  388. /* Update ram_cfg and check if the data will overstep */
  389. if (drvdata->data_sink == DCC_DATA_SINK_SRAM &&
  390. drvdata->func_type[curr_list] == DCC_FUNC_TYPE_CAPTURE) {
  391. drvdata->ram_cfg = (sram_offset + total_len) / 4;
  392. if (sram_offset + total_len > drvdata->ram_size) {
  393. sram_offset += total_len;
  394. goto overstep;
  395. }
  396. } else {
  397. drvdata->ram_cfg = sram_offset / 4;
  398. if (sram_offset > drvdata->ram_size)
  399. goto overstep;
  400. }
  401. drvdata->ram_start = sram_offset/4;
  402. return 0;
  403. overstep:
  404. ret = -EINVAL;
  405. memset_io(drvdata->ram_base, 0, drvdata->ram_size);
  406. dev_err(drvdata->dev, "DCC SRAM oversteps, 0x%x (0x%x)\n",
  407. sram_offset, drvdata->ram_size);
  408. err:
  409. return ret;
  410. }
  411. static void __dcc_first_crc(struct dcc_drvdata *drvdata)
  412. {
  413. int i;
  414. /*
  415. * Need to send 2 triggers to DCC. First trigger sets CRC error status
  416. * bit. So need second trigger to reset this bit.
  417. */
  418. for (i = 0; i < 2; i++) {
  419. if (!dcc_ready(drvdata))
  420. dev_err(drvdata->dev, "DCC is not ready\n");
  421. dcc_writel(drvdata, 1,
  422. DCC_LL_SW_TRIGGER(drvdata->curr_list));
  423. }
  424. /* Clear CRC error interrupt */
  425. dcc_writel(drvdata, BIT(1),
  426. DCC_LL_INT_STATUS(drvdata->curr_list));
  427. }
  428. static int dcc_valid_list(struct dcc_drvdata *drvdata, int curr_list)
  429. {
  430. uint32_t lock_reg;
  431. if (list_empty(&drvdata->cfg_head[curr_list]))
  432. return -EINVAL;
  433. if (drvdata->enable[curr_list]) {
  434. dev_err(drvdata->dev, "DCC is already enabled\n");
  435. return -EINVAL;
  436. }
  437. lock_reg = dcc_readl(drvdata, DCC_LL_LOCK(curr_list));
  438. if (lock_reg & 0x1) {
  439. dev_err(drvdata->dev, "DCC is already enabled\n");
  440. return -EINVAL;
  441. }
  442. dev_err(drvdata->dev, "DCC list passed %d\n", curr_list);
  443. return 0;
  444. }
  445. static int dcc_enable(struct dcc_drvdata *drvdata)
  446. {
  447. int ret = 0;
  448. int list;
  449. uint32_t ram_cfg_base;
  450. mutex_lock(&drvdata->mutex);
  451. memset_io(drvdata->ram_base, 0xDE, drvdata->ram_size);
  452. for (list = 0; list < DCC_MAX_LINK_LIST; list++) {
  453. if (dcc_valid_list(drvdata, list))
  454. continue;
  455. /* 1. Take ownership of the list */
  456. dcc_writel(drvdata, BIT(0), DCC_LL_LOCK(list));
  457. /* 2. Program linked-list in the SRAM */
  458. ram_cfg_base = drvdata->ram_cfg;
  459. ret = __dcc_ll_cfg(drvdata, list);
  460. if (ret) {
  461. dev_info(drvdata->dev, "DCC ram programming failed\n");
  462. goto err;
  463. }
  464. /* 3. program DCC_RAM_CFG reg */
  465. dcc_writel(drvdata, ram_cfg_base +
  466. drvdata->ram_offset/4, DCC_LL_BASE(list));
  467. dcc_writel(drvdata, drvdata->ram_start +
  468. drvdata->ram_offset/4, DCC_FD_BASE(list));
  469. dcc_writel(drvdata, 0xFFF, DCC_LL_TIMEOUT(list));
  470. /* 4. Configure trigger, data sink and function type */
  471. dcc_writel(drvdata, BIT(9) | ((drvdata->cti_trig << 8) |
  472. (drvdata->data_sink << 4) |
  473. (drvdata->func_type[list])), DCC_LL_CFG(list));
  474. /* 5. Clears interrupt status register */
  475. dcc_writel(drvdata, 0, DCC_LL_INT_ENABLE(list));
  476. dcc_writel(drvdata, (BIT(0) | BIT(1) | BIT(2)),
  477. DCC_LL_INT_STATUS(list));
  478. dev_info(drvdata->dev, "All values written to enable");
  479. /* Make sure all config is written in sram */
  480. mb();
  481. drvdata->enable[list] = 1;
  482. if (drvdata->func_type[list] == DCC_FUNC_TYPE_CRC) {
  483. __dcc_first_crc(drvdata);
  484. /* Enable CRC error interrupt */
  485. if (!drvdata->interrupt_disable)
  486. dcc_writel(drvdata, BIT(1),
  487. DCC_LL_INT_ENABLE(list));
  488. }
  489. }
  490. err:
  491. mutex_unlock(&drvdata->mutex);
  492. return ret;
  493. }
  494. static void dcc_disable(struct dcc_drvdata *drvdata)
  495. {
  496. int curr_list;
  497. mutex_lock(&drvdata->mutex);
  498. if (!dcc_ready(drvdata))
  499. dev_err(drvdata->dev, "DCC is not ready Disabling DCC...\n");
  500. for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) {
  501. if (!drvdata->enable[curr_list])
  502. continue;
  503. dcc_writel(drvdata, 0, DCC_LL_LOCK(curr_list));
  504. drvdata->enable[curr_list] = 0;
  505. }
  506. drvdata->ram_cfg = 0;
  507. drvdata->ram_start = 0;
  508. mutex_unlock(&drvdata->mutex);
  509. }
  510. static ssize_t dcc_curr_list(struct device *dev,
  511. struct device_attribute *attr,
  512. const char *buf, size_t size)
  513. {
  514. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  515. unsigned long val;
  516. uint32_t lock_reg;
  517. if (kstrtoul(buf, 16, &val))
  518. return -EINVAL;
  519. if (val >= DCC_MAX_LINK_LIST)
  520. return -EINVAL;
  521. mutex_lock(&drvdata->mutex);
  522. lock_reg = dcc_readl(drvdata, DCC_LL_LOCK(val));
  523. if (lock_reg & 0x1) {
  524. dev_err(drvdata->dev, "DCC linked list is already configured\n");
  525. mutex_unlock(&drvdata->mutex);
  526. return -EINVAL;
  527. }
  528. drvdata->curr_list = val;
  529. mutex_unlock(&drvdata->mutex);
  530. return size;
  531. }
  532. static DEVICE_ATTR(curr_list, 0200,
  533. NULL, dcc_curr_list);
  534. static ssize_t dcc_show_func_type(struct device *dev,
  535. struct device_attribute *attr, char *buf)
  536. {
  537. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  538. ssize_t len = 0;
  539. unsigned int i;
  540. for (i = 0; i < DCC_MAX_LINK_LIST; i++)
  541. len += scnprintf(buf + len, PAGE_SIZE - len, "%u :%s\n",
  542. i, str_dcc_func_type[drvdata->func_type[i]]);
  543. return len;
  544. }
  545. static ssize_t dcc_store_func_type(struct device *dev,
  546. struct device_attribute *attr,
  547. const char *buf, size_t size)
  548. {
  549. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  550. char str[10] = "";
  551. int ret;
  552. if (strlen(buf) >= 10)
  553. return -EINVAL;
  554. if (sscanf(buf, "%s", str) != 1)
  555. return -EINVAL;
  556. mutex_lock(&drvdata->mutex);
  557. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  558. dev_err(dev,
  559. "Select link list to program using curr_list\n");
  560. ret = -EINVAL;
  561. goto out;
  562. }
  563. if (drvdata->enable[drvdata->curr_list]) {
  564. ret = -EBUSY;
  565. goto out;
  566. }
  567. if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CAPTURE]))
  568. drvdata->func_type[drvdata->curr_list] =
  569. DCC_FUNC_TYPE_CAPTURE;
  570. else if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CRC]))
  571. drvdata->func_type[drvdata->curr_list] =
  572. DCC_FUNC_TYPE_CRC;
  573. else {
  574. ret = -EINVAL;
  575. goto out;
  576. }
  577. ret = size;
  578. out:
  579. mutex_unlock(&drvdata->mutex);
  580. return ret;
  581. }
  582. static DEVICE_ATTR(func_type, 0644,
  583. dcc_show_func_type, dcc_store_func_type);
  584. static ssize_t dcc_show_data_sink(struct device *dev,
  585. struct device_attribute *attr, char *buf)
  586. {
  587. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  588. return scnprintf(buf, PAGE_SIZE, "%s\n",
  589. str_dcc_data_sink[drvdata->data_sink]);
  590. }
  591. static ssize_t dcc_store_data_sink(struct device *dev,
  592. struct device_attribute *attr,
  593. const char *buf, size_t size)
  594. {
  595. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  596. char str[10] = "";
  597. int ret;
  598. if (strlen(buf) >= 10)
  599. return -EINVAL;
  600. if (sscanf(buf, "%s", str) != 1)
  601. return -EINVAL;
  602. mutex_lock(&drvdata->mutex);
  603. if (drvdata->enable[drvdata->curr_list]) {
  604. ret = -EBUSY;
  605. goto out;
  606. }
  607. if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_SRAM]))
  608. drvdata->data_sink = DCC_DATA_SINK_SRAM;
  609. else if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_ATB]))
  610. drvdata->data_sink = DCC_DATA_SINK_ATB;
  611. else {
  612. ret = -EINVAL;
  613. goto out;
  614. }
  615. ret = size;
  616. out:
  617. mutex_unlock(&drvdata->mutex);
  618. return ret;
  619. }
  620. static DEVICE_ATTR(data_sink, 0644,
  621. dcc_show_data_sink, dcc_store_data_sink);
  622. static ssize_t dcc_store_trigger(struct device *dev,
  623. struct device_attribute *attr,
  624. const char *buf, size_t size)
  625. {
  626. int ret = 0;
  627. unsigned long val;
  628. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  629. if (kstrtoul(buf, 16, &val))
  630. return -EINVAL;
  631. if (val != 1)
  632. return -EINVAL;
  633. ret = dcc_sw_trigger(drvdata);
  634. if (!ret)
  635. ret = size;
  636. return ret;
  637. }
  638. static DEVICE_ATTR(trigger, 0200, NULL, dcc_store_trigger);
  639. static ssize_t dcc_show_enable(struct device *dev,
  640. struct device_attribute *attr, char *buf)
  641. {
  642. int ret;
  643. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  644. mutex_lock(&drvdata->mutex);
  645. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  646. dev_err(dev, "Select link list to program using curr_list\n");
  647. ret = -EINVAL;
  648. goto err;
  649. }
  650. ret = scnprintf(buf, PAGE_SIZE, "%u\n",
  651. (unsigned int)drvdata->enable[drvdata->curr_list]);
  652. err:
  653. mutex_unlock(&drvdata->mutex);
  654. return ret;
  655. }
  656. static ssize_t dcc_store_enable(struct device *dev,
  657. struct device_attribute *attr,
  658. const char *buf, size_t size)
  659. {
  660. int ret = 0;
  661. unsigned long val;
  662. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  663. if (kstrtoul(buf, 16, &val))
  664. return -EINVAL;
  665. if (val)
  666. ret = dcc_enable(drvdata);
  667. else
  668. dcc_disable(drvdata);
  669. if (!ret)
  670. ret = size;
  671. return ret;
  672. }
  673. static DEVICE_ATTR(enable, 0644, dcc_show_enable,
  674. dcc_store_enable);
  675. static ssize_t dcc_show_config(struct device *dev,
  676. struct device_attribute *attr, char *buf)
  677. {
  678. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  679. struct dcc_config_entry *entry;
  680. char local_buf[64];
  681. int len = 0, count = 0;
  682. buf[0] = '\0';
  683. mutex_lock(&drvdata->mutex);
  684. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  685. dev_err(dev, "Select link list to program using curr_list\n");
  686. count = -EINVAL;
  687. goto err;
  688. }
  689. list_for_each_entry(entry,
  690. &drvdata->cfg_head[drvdata->curr_list], list) {
  691. switch (entry->desc_type) {
  692. case DCC_READ_WRITE_TYPE:
  693. len = snprintf(local_buf, 64,
  694. "Index: 0x%x, mask: 0x%x, val: 0x%x\n",
  695. entry->index, entry->mask,
  696. entry->write_val);
  697. break;
  698. case DCC_LOOP_TYPE:
  699. len = snprintf(local_buf, 64, "Index: 0x%x, Loop: %d\n",
  700. entry->index, entry->loop_cnt);
  701. break;
  702. case DCC_WRITE_TYPE:
  703. len = snprintf(local_buf, 64,
  704. "Write Index: 0x%x, Base: 0x%x, Offset: 0x%x, len: 0x%x APB: %d\n",
  705. entry->index, entry->base,
  706. entry->offset, entry->len,
  707. entry->apb_bus);
  708. break;
  709. default:
  710. len = snprintf(local_buf, 64,
  711. "Read Index: 0x%x, Base: 0x%x, Offset: 0x%x, len: 0x%x APB: %d\n",
  712. entry->index, entry->base,
  713. entry->offset, entry->len,
  714. entry->apb_bus);
  715. }
  716. if ((count + len) > PAGE_SIZE) {
  717. dev_err(dev, "DCC: Couldn't write complete config\n");
  718. break;
  719. }
  720. strlcat(buf, local_buf, PAGE_SIZE);
  721. count += len;
  722. }
  723. err:
  724. mutex_unlock(&drvdata->mutex);
  725. return count;
  726. }
  727. static int dcc_config_add(struct dcc_drvdata *drvdata, unsigned int addr,
  728. unsigned int len, int apb_bus)
  729. {
  730. int ret;
  731. struct dcc_config_entry *entry, *pentry;
  732. unsigned int base, offset;
  733. mutex_lock(&drvdata->mutex);
  734. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  735. dev_err(drvdata->dev, "Select link list to program using curr_list\n");
  736. ret = -EINVAL;
  737. goto err;
  738. }
  739. /* Check the len to avoid allocate huge memory */
  740. if (!len || len > (drvdata->ram_size / 8)) {
  741. dev_err(drvdata->dev, "DCC: Invalid length\n");
  742. ret = -EINVAL;
  743. goto err;
  744. }
  745. base = addr & BM(4, 31);
  746. if (!list_empty(&drvdata->cfg_head[drvdata->curr_list])) {
  747. pentry = list_last_entry(&drvdata->cfg_head[drvdata->curr_list],
  748. struct dcc_config_entry, list);
  749. if (addr >= (pentry->base + pentry->offset) &&
  750. addr <= (pentry->base + pentry->offset + MAX_DCC_OFFSET)) {
  751. /* Re-use base address from last entry */
  752. base = pentry->base;
  753. /*
  754. * Check if new address is contiguous to last entry's
  755. * addresses. If yes then we can re-use last entry and
  756. * just need to update its length.
  757. */
  758. if ((pentry->len * 4 + pentry->base + pentry->offset)
  759. == addr) {
  760. len += pentry->len;
  761. /*
  762. * Check if last entry can hold additional new
  763. * length. If yes then we don't need to create
  764. * a new entry else we need to add a new entry
  765. * with same base but updated offset.
  766. */
  767. if (len > MAX_DCC_LEN)
  768. pentry->len = MAX_DCC_LEN;
  769. else
  770. pentry->len = len;
  771. /*
  772. * Update start addr and len for remaining
  773. * addresses, which will be part of new
  774. * entry.
  775. */
  776. addr = pentry->base + pentry->offset +
  777. pentry->len * 4;
  778. len -= pentry->len;
  779. }
  780. }
  781. }
  782. offset = addr - base;
  783. while (len) {
  784. entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL);
  785. if (!entry) {
  786. ret = -ENOMEM;
  787. goto err;
  788. }
  789. entry->base = base;
  790. entry->offset = offset;
  791. entry->len = min_t(uint32_t, len, MAX_DCC_LEN);
  792. entry->index = drvdata->nr_config[drvdata->curr_list]++;
  793. entry->desc_type = DCC_ADDR_TYPE;
  794. entry->apb_bus = apb_bus;
  795. INIT_LIST_HEAD(&entry->list);
  796. list_add_tail(&entry->list,
  797. &drvdata->cfg_head[drvdata->curr_list]);
  798. len -= entry->len;
  799. offset += MAX_DCC_LEN * 4;
  800. }
  801. mutex_unlock(&drvdata->mutex);
  802. return 0;
  803. err:
  804. mutex_unlock(&drvdata->mutex);
  805. return ret;
  806. }
  807. static ssize_t dcc_store_config(struct device *dev,
  808. struct device_attribute *attr,
  809. const char *buf, size_t size)
  810. {
  811. int ret, len, apb_bus;
  812. unsigned int base;
  813. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  814. int nval;
  815. nval = sscanf(buf, "%x %i %d", &base, &len, &apb_bus);
  816. if (nval <= 0 || nval > 3)
  817. return -EINVAL;
  818. if (nval == 1) {
  819. len = 1;
  820. apb_bus = 0;
  821. } else if (nval == 2) {
  822. apb_bus = 0;
  823. } else {
  824. apb_bus = 1;
  825. }
  826. ret = dcc_config_add(drvdata, base, len, apb_bus);
  827. if (ret)
  828. return ret;
  829. return size;
  830. }
  831. static DEVICE_ATTR(config, 0644, dcc_show_config,
  832. dcc_store_config);
  833. static void dcc_config_reset(struct dcc_drvdata *drvdata)
  834. {
  835. struct dcc_config_entry *entry, *temp;
  836. int curr_list;
  837. mutex_lock(&drvdata->mutex);
  838. for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) {
  839. list_for_each_entry_safe(entry, temp,
  840. &drvdata->cfg_head[curr_list], list) {
  841. list_del(&entry->list);
  842. devm_kfree(drvdata->dev, entry);
  843. drvdata->nr_config[curr_list]--;
  844. }
  845. }
  846. drvdata->ram_start = 0;
  847. drvdata->ram_cfg = 0;
  848. mutex_unlock(&drvdata->mutex);
  849. }
  850. static ssize_t dcc_store_config_reset(struct device *dev,
  851. struct device_attribute *attr,
  852. const char *buf, size_t size)
  853. {
  854. unsigned long val;
  855. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  856. if (kstrtoul(buf, 16, &val))
  857. return -EINVAL;
  858. if (val)
  859. dcc_config_reset(drvdata);
  860. return size;
  861. }
  862. static DEVICE_ATTR(config_reset, 0200, NULL, dcc_store_config_reset);
  863. static ssize_t dcc_show_crc_error(struct device *dev,
  864. struct device_attribute *attr, char *buf)
  865. {
  866. int ret;
  867. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  868. mutex_lock(&drvdata->mutex);
  869. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  870. dev_err(dev, "Select link list to program using curr_list\n");
  871. ret = -EINVAL;
  872. goto err;
  873. }
  874. if (!drvdata->enable[drvdata->curr_list]) {
  875. ret = -EINVAL;
  876. goto err;
  877. }
  878. ret = scnprintf(buf, PAGE_SIZE, "%u\n",
  879. (unsigned int)BVAL(dcc_readl(
  880. drvdata, DCC_LL_INT_STATUS(drvdata->curr_list)), 1));
  881. err:
  882. mutex_unlock(&drvdata->mutex);
  883. return ret;
  884. }
  885. static DEVICE_ATTR(crc_error, 0444, dcc_show_crc_error, NULL);
  886. static ssize_t dcc_show_ready(struct device *dev,
  887. struct device_attribute *attr, char *buf)
  888. {
  889. int ret;
  890. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  891. mutex_lock(&drvdata->mutex);
  892. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  893. dev_err(dev, "Select link list to program using curr_list\n");
  894. ret = -EINVAL;
  895. goto err;
  896. }
  897. if (!drvdata->enable[drvdata->curr_list]) {
  898. ret = -EINVAL;
  899. goto err;
  900. }
  901. ret = scnprintf(buf, PAGE_SIZE, "%u\n",
  902. (unsigned int)BVAL(dcc_readl(drvdata, DCC_STATUS), 1));
  903. err:
  904. mutex_unlock(&drvdata->mutex);
  905. return ret;
  906. }
  907. static DEVICE_ATTR(ready, 0444, dcc_show_ready, NULL);
  908. static ssize_t dcc_show_interrupt_disable(struct device *dev,
  909. struct device_attribute *attr,
  910. char *buf)
  911. {
  912. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  913. return scnprintf(buf, PAGE_SIZE, "%u\n",
  914. (unsigned int)drvdata->interrupt_disable);
  915. }
  916. static ssize_t dcc_store_interrupt_disable(struct device *dev,
  917. struct device_attribute *attr,
  918. const char *buf, size_t size)
  919. {
  920. unsigned long val;
  921. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  922. if (kstrtoul(buf, 16, &val))
  923. return -EINVAL;
  924. mutex_lock(&drvdata->mutex);
  925. drvdata->interrupt_disable = (val ? 1:0);
  926. mutex_unlock(&drvdata->mutex);
  927. return size;
  928. }
  929. static DEVICE_ATTR(interrupt_disable, 0644,
  930. dcc_show_interrupt_disable, dcc_store_interrupt_disable);
  931. static int dcc_add_loop(struct dcc_drvdata *drvdata, unsigned long loop_cnt)
  932. {
  933. struct dcc_config_entry *entry;
  934. entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL);
  935. if (!entry)
  936. return -ENOMEM;
  937. entry->loop_cnt = min_t(uint32_t, loop_cnt, MAX_LOOP_CNT);
  938. entry->index = drvdata->nr_config[drvdata->curr_list]++;
  939. entry->desc_type = DCC_LOOP_TYPE;
  940. INIT_LIST_HEAD(&entry->list);
  941. list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]);
  942. return 0;
  943. }
  944. static ssize_t dcc_store_loop(struct device *dev,
  945. struct device_attribute *attr,
  946. const char *buf, size_t size)
  947. {
  948. int ret;
  949. unsigned long loop_cnt;
  950. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  951. mutex_lock(&drvdata->mutex);
  952. if (kstrtoul(buf, 16, &loop_cnt)) {
  953. ret = -EINVAL;
  954. goto err;
  955. }
  956. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  957. dev_err(dev, "Select link list to program using curr_list\n");
  958. ret = -EINVAL;
  959. goto err;
  960. }
  961. ret = dcc_add_loop(drvdata, loop_cnt);
  962. if (ret)
  963. goto err;
  964. mutex_unlock(&drvdata->mutex);
  965. return size;
  966. err:
  967. mutex_unlock(&drvdata->mutex);
  968. return ret;
  969. }
  970. static DEVICE_ATTR(loop, 0200, NULL, dcc_store_loop);
  971. static ssize_t dcc_rd_mod_wr(struct device *dev,
  972. struct device_attribute *attr,
  973. const char *buf, size_t size)
  974. {
  975. int ret = size;
  976. int nval;
  977. unsigned int mask, val;
  978. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  979. struct dcc_config_entry *entry;
  980. mutex_lock(&drvdata->mutex);
  981. nval = sscanf(buf, "%x %x", &mask, &val);
  982. if (nval <= 1 || nval > 2) {
  983. ret = -EINVAL;
  984. goto err;
  985. }
  986. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  987. dev_err(dev, "Select link list to program using curr_list\n");
  988. ret = -EINVAL;
  989. goto err;
  990. }
  991. if (list_empty(&drvdata->cfg_head[drvdata->curr_list])) {
  992. dev_err(drvdata->dev, "DCC: No read address programmed\n");
  993. ret = -EPERM;
  994. goto err;
  995. }
  996. entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL);
  997. if (!entry) {
  998. ret = -ENOMEM;
  999. goto err;
  1000. }
  1001. entry->desc_type = DCC_READ_WRITE_TYPE;
  1002. entry->mask = mask;
  1003. entry->write_val = val;
  1004. entry->index = drvdata->nr_config[drvdata->curr_list]++;
  1005. INIT_LIST_HEAD(&entry->list);
  1006. list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]);
  1007. err:
  1008. mutex_unlock(&drvdata->mutex);
  1009. return ret;
  1010. }
  1011. static DEVICE_ATTR(rd_mod_wr, 0200, NULL, dcc_rd_mod_wr);
  1012. static int dcc_add_write(struct dcc_drvdata *drvdata, unsigned int addr,
  1013. unsigned int write_val, int apb_bus)
  1014. {
  1015. struct dcc_config_entry *entry;
  1016. entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL);
  1017. if (!entry)
  1018. return -ENOMEM;
  1019. entry->desc_type = DCC_WRITE_TYPE;
  1020. entry->base = addr & BM(4, 31);
  1021. entry->offset = addr - entry->base;
  1022. entry->write_val = write_val;
  1023. entry->index = drvdata->nr_config[drvdata->curr_list]++;
  1024. entry->len = 1;
  1025. entry->apb_bus = apb_bus;
  1026. INIT_LIST_HEAD(&entry->list);
  1027. list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]);
  1028. return 0;
  1029. }
  1030. static ssize_t dcc_write(struct device *dev,
  1031. struct device_attribute *attr,
  1032. const char *buf, size_t size)
  1033. {
  1034. int ret;
  1035. int nval;
  1036. unsigned int addr, write_val;
  1037. int apb_bus = 0;
  1038. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  1039. mutex_lock(&drvdata->mutex);
  1040. nval = sscanf(buf, "%x %x %d", &addr, &write_val, &apb_bus);
  1041. if (nval <= 1 || nval > 3) {
  1042. ret = -EINVAL;
  1043. goto err;
  1044. }
  1045. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  1046. dev_err(dev, "Select link list to program using curr_list\n");
  1047. ret = -EINVAL;
  1048. goto err;
  1049. }
  1050. if (nval == 3 && apb_bus != 0)
  1051. apb_bus = 1;
  1052. ret = dcc_add_write(drvdata, addr, write_val, apb_bus);
  1053. if (ret)
  1054. goto err;
  1055. mutex_unlock(&drvdata->mutex);
  1056. return size;
  1057. err:
  1058. mutex_unlock(&drvdata->mutex);
  1059. return ret;
  1060. }
  1061. static DEVICE_ATTR(config_write, 0200, NULL, dcc_write);
  1062. static ssize_t dcc_show_cti_trig(struct device *dev,
  1063. struct device_attribute *attr, char *buf)
  1064. {
  1065. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  1066. return scnprintf(buf, PAGE_SIZE, "%d\n", drvdata->cti_trig);
  1067. }
  1068. static ssize_t dcc_store_cti_trig(struct device *dev,
  1069. struct device_attribute *attr,
  1070. const char *buf, size_t size)
  1071. {
  1072. unsigned long val;
  1073. int ret = 0;
  1074. struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
  1075. if (kstrtoul(buf, 16, &val))
  1076. return -EINVAL;
  1077. mutex_lock(&drvdata->mutex);
  1078. if (drvdata->curr_list >= DCC_MAX_LINK_LIST) {
  1079. dev_err(dev, "Select link list to program using curr_list\n");
  1080. ret = -EINVAL;
  1081. goto out;
  1082. }
  1083. if (drvdata->enable[drvdata->curr_list]) {
  1084. ret = -EBUSY;
  1085. goto out;
  1086. }
  1087. if (val)
  1088. drvdata->cti_trig = 1;
  1089. else
  1090. drvdata->cti_trig = 0;
  1091. out:
  1092. mutex_unlock(&drvdata->mutex);
  1093. return ret;
  1094. }
  1095. static DEVICE_ATTR(cti_trig, 0644,
  1096. dcc_show_cti_trig, dcc_store_cti_trig);
  1097. static const struct device_attribute *dcc_attrs[] = {
  1098. &dev_attr_func_type,
  1099. &dev_attr_data_sink,
  1100. &dev_attr_trigger,
  1101. &dev_attr_enable,
  1102. &dev_attr_config,
  1103. &dev_attr_config_reset,
  1104. &dev_attr_ready,
  1105. &dev_attr_crc_error,
  1106. &dev_attr_interrupt_disable,
  1107. &dev_attr_loop,
  1108. &dev_attr_rd_mod_wr,
  1109. &dev_attr_curr_list,
  1110. &dev_attr_config_write,
  1111. &dev_attr_cti_trig,
  1112. NULL,
  1113. };
  1114. static int dcc_create_files(struct device *dev,
  1115. const struct device_attribute **attrs)
  1116. {
  1117. int ret = 0, i;
  1118. for (i = 0; attrs[i] != NULL; i++) {
  1119. ret = device_create_file(dev, attrs[i]);
  1120. if (ret) {
  1121. dev_err(dev, "DCC: Couldn't create sysfs attribute: %s\n",
  1122. attrs[i]->attr.name);
  1123. break;
  1124. }
  1125. }
  1126. return ret;
  1127. }
  1128. static int dcc_sram_open(struct inode *inode, struct file *file)
  1129. {
  1130. struct dcc_drvdata *drvdata = container_of(inode->i_cdev,
  1131. struct dcc_drvdata,
  1132. sram_dev);
  1133. file->private_data = drvdata;
  1134. return 0;
  1135. }
  1136. static ssize_t dcc_sram_read(struct file *file, char __user *data,
  1137. size_t len, loff_t *ppos)
  1138. {
  1139. unsigned char *buf;
  1140. struct dcc_drvdata *drvdata = file->private_data;
  1141. /* EOF check */
  1142. if (drvdata->ram_size <= *ppos)
  1143. return 0;
  1144. if ((*ppos + len) > drvdata->ram_size)
  1145. len = (drvdata->ram_size - *ppos);
  1146. buf = kzalloc(len, GFP_KERNEL);
  1147. if (!buf)
  1148. return -ENOMEM;
  1149. memcpy_fromio(buf, (drvdata->ram_base + *ppos), len);
  1150. if (copy_to_user(data, buf, len)) {
  1151. dev_err(drvdata->dev,
  1152. "DCC: Couldn't copy all data to user\n");
  1153. kfree(buf);
  1154. return -EFAULT;
  1155. }
  1156. *ppos += len;
  1157. kfree(buf);
  1158. return len;
  1159. }
  1160. static const struct file_operations dcc_sram_fops = {
  1161. .owner = THIS_MODULE,
  1162. .open = dcc_sram_open,
  1163. .read = dcc_sram_read,
  1164. .llseek = no_llseek,
  1165. };
  1166. static int dcc_sram_dev_register(struct dcc_drvdata *drvdata)
  1167. {
  1168. int ret;
  1169. struct device *device;
  1170. dev_t dev;
  1171. ret = alloc_chrdev_region(&dev, 0, 1, drvdata->sram_node);
  1172. if (ret)
  1173. goto err_alloc;
  1174. cdev_init(&drvdata->sram_dev, &dcc_sram_fops);
  1175. drvdata->sram_dev.owner = THIS_MODULE;
  1176. ret = cdev_add(&drvdata->sram_dev, dev, 1);
  1177. if (ret)
  1178. goto err_cdev_add;
  1179. drvdata->sram_class = class_create(THIS_MODULE,
  1180. drvdata->sram_node);
  1181. if (IS_ERR(drvdata->sram_class)) {
  1182. ret = PTR_ERR(drvdata->sram_class);
  1183. goto err_class_create;
  1184. }
  1185. device = device_create(drvdata->sram_class, NULL,
  1186. drvdata->sram_dev.dev, drvdata,
  1187. drvdata->sram_node);
  1188. if (IS_ERR(device)) {
  1189. ret = PTR_ERR(device);
  1190. goto err_dev_create;
  1191. }
  1192. return 0;
  1193. err_dev_create:
  1194. class_destroy(drvdata->sram_class);
  1195. err_class_create:
  1196. cdev_del(&drvdata->sram_dev);
  1197. err_cdev_add:
  1198. unregister_chrdev_region(drvdata->sram_dev.dev, 1);
  1199. err_alloc:
  1200. return ret;
  1201. }
  1202. static void dcc_sram_dev_deregister(struct dcc_drvdata *drvdata)
  1203. {
  1204. device_destroy(drvdata->sram_class, drvdata->sram_dev.dev);
  1205. class_destroy(drvdata->sram_class);
  1206. cdev_del(&drvdata->sram_dev);
  1207. unregister_chrdev_region(drvdata->sram_dev.dev, 1);
  1208. }
  1209. static int dcc_sram_dev_init(struct dcc_drvdata *drvdata)
  1210. {
  1211. int ret = 0;
  1212. size_t node_size;
  1213. char *node_name = "dcc_sram";
  1214. struct device *dev = drvdata->dev;
  1215. node_size = strlen(node_name) + 1;
  1216. drvdata->sram_node = devm_kzalloc(dev, node_size, GFP_KERNEL);
  1217. if (!drvdata->sram_node)
  1218. return -ENOMEM;
  1219. strlcpy(drvdata->sram_node, node_name, node_size);
  1220. ret = dcc_sram_dev_register(drvdata);
  1221. if (ret)
  1222. dev_err(drvdata->dev, "DCC: sram node not registered.\n");
  1223. return ret;
  1224. }
  1225. static void dcc_sram_dev_exit(struct dcc_drvdata *drvdata)
  1226. {
  1227. dcc_sram_dev_deregister(drvdata);
  1228. }
  1229. static void dcc_configure_list(struct dcc_drvdata *drvdata,
  1230. struct device_node *np)
  1231. {
  1232. int ret, i;
  1233. const __be32 *prop;
  1234. uint32_t len, entry, val1, val2, apb_bus;
  1235. uint32_t curr_link_list;
  1236. ret = of_property_read_u32(np, "qcom,curr-link-list",
  1237. &curr_link_list);
  1238. if (ret)
  1239. return;
  1240. if (curr_link_list >= DCC_MAX_LINK_LIST) {
  1241. dev_err(drvdata->dev, "List configuration failed");
  1242. return;
  1243. }
  1244. drvdata->curr_list = curr_link_list;
  1245. prop = of_get_property(np, "qcom,link-list", &len);
  1246. if (prop) {
  1247. len /= sizeof(__be32);
  1248. i = 0;
  1249. while (i < len) {
  1250. entry = be32_to_cpu(prop[i++]);
  1251. val1 = be32_to_cpu(prop[i++]);
  1252. val2 = be32_to_cpu(prop[i++]);
  1253. apb_bus = be32_to_cpu(prop[i++]);
  1254. switch (entry) {
  1255. case DCC_READ:
  1256. ret = dcc_config_add(drvdata, val1,
  1257. val2, apb_bus);
  1258. break;
  1259. case DCC_WRITE:
  1260. ret = dcc_add_write(drvdata, val1,
  1261. val2, apb_bus);
  1262. break;
  1263. case DCC_LOOP:
  1264. ret = dcc_add_loop(drvdata, val1);
  1265. break;
  1266. default:
  1267. ret = -EINVAL;
  1268. }
  1269. if (ret) {
  1270. dev_err(drvdata->dev,
  1271. "DCC init time config failed err:%d\n",
  1272. ret);
  1273. break;
  1274. }
  1275. }
  1276. if (!ret)
  1277. dcc_enable(drvdata);
  1278. }
  1279. }
  1280. static int dcc_probe(struct platform_device *pdev)
  1281. {
  1282. int ret, i;
  1283. struct device *dev = &pdev->dev;
  1284. struct dcc_drvdata *drvdata;
  1285. struct resource *res;
  1286. const char *data_sink;
  1287. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  1288. if (!drvdata)
  1289. return -ENOMEM;
  1290. drvdata->dev = &pdev->dev;
  1291. platform_set_drvdata(pdev, drvdata);
  1292. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dcc-base");
  1293. if (!res)
  1294. return -EINVAL;
  1295. drvdata->reg_size = resource_size(res);
  1296. drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
  1297. if (!drvdata->base)
  1298. return -ENOMEM;
  1299. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1300. "dcc-ram-base");
  1301. if (!res)
  1302. return -EINVAL;
  1303. drvdata->ram_size = resource_size(res);
  1304. drvdata->ram_base = devm_ioremap(dev, res->start, resource_size(res));
  1305. if (!drvdata->ram_base)
  1306. return -ENOMEM;
  1307. ret = of_property_read_u32(pdev->dev.of_node, "dcc-ram-offset",
  1308. &drvdata->ram_offset);
  1309. if (ret)
  1310. return -EINVAL;
  1311. mutex_init(&drvdata->mutex);
  1312. for (i = 0; i < DCC_MAX_LINK_LIST; i++) {
  1313. INIT_LIST_HEAD(&drvdata->cfg_head[i]);
  1314. drvdata->nr_config[i] = 0;
  1315. }
  1316. memset_io(drvdata->ram_base, 0, drvdata->ram_size);
  1317. drvdata->data_sink = DCC_DATA_SINK_SRAM;
  1318. ret = of_property_read_string(pdev->dev.of_node, "qcom,data-sink",
  1319. &data_sink);
  1320. if (!ret) {
  1321. for (i = 0; i < ARRAY_SIZE(str_dcc_data_sink); i++)
  1322. if (!strcmp(data_sink, str_dcc_data_sink[i])) {
  1323. drvdata->data_sink = i;
  1324. break;
  1325. }
  1326. if (i == ARRAY_SIZE(str_dcc_data_sink)) {
  1327. dev_err(dev, "Unknown sink type for DCC Using '%s' as data sink\n",
  1328. str_dcc_data_sink[drvdata->data_sink]);
  1329. }
  1330. }
  1331. drvdata->curr_list = DCC_INVALID_LINK_LIST;
  1332. ret = dcc_sram_dev_init(drvdata);
  1333. if (ret)
  1334. goto err;
  1335. ret = dcc_create_files(dev, dcc_attrs);
  1336. if (ret)
  1337. goto err;
  1338. dcc_configure_list(drvdata, pdev->dev.of_node);
  1339. return 0;
  1340. err:
  1341. return ret;
  1342. }
  1343. static int dcc_remove(struct platform_device *pdev)
  1344. {
  1345. struct dcc_drvdata *drvdata = platform_get_drvdata(pdev);
  1346. dcc_sram_dev_exit(drvdata);
  1347. dcc_config_reset(drvdata);
  1348. return 0;
  1349. }
  1350. static const struct of_device_id msm_dcc_match[] = {
  1351. { .compatible = "qcom,dcc-v2"},
  1352. {}
  1353. };
  1354. static struct platform_driver dcc_driver = {
  1355. .probe = dcc_probe,
  1356. .remove = dcc_remove,
  1357. .driver = {
  1358. .name = "msm-dcc",
  1359. .owner = THIS_MODULE,
  1360. .of_match_table = msm_dcc_match,
  1361. },
  1362. };
  1363. static int __init dcc_init(void)
  1364. {
  1365. return platform_driver_register(&dcc_driver);
  1366. }
  1367. pure_initcall(dcc_init);
  1368. MODULE_LICENSE("GPL v2");
  1369. MODULE_DESCRIPTION("MSM data capture and compare engine");