coresight-cti.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /* Copyright (c) 2013-2017, 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/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/mutex.h>
  21. #include <linux/amba/bus.h>
  22. #include <linux/cpu_pm.h>
  23. #include <linux/topology.h>
  24. #include <linux/of.h>
  25. #include <linux/coresight.h>
  26. #include <linux/coresight-cti.h>
  27. #include "coresight-priv.h"
  28. #define cti_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off)
  29. #define cti_readl(drvdata, off) __raw_readl(drvdata->base + off)
  30. #define CTI_LOCK(drvdata) \
  31. do { \
  32. mb(); /* ensure configuration take effect before we lock it */ \
  33. cti_writel(drvdata, 0x0, CORESIGHT_LAR); \
  34. } while (0)
  35. #define CTI_UNLOCK(drvdata) \
  36. do { \
  37. cti_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \
  38. mb(); /* ensure unlock take effect before we configure */ \
  39. } while (0)
  40. #define CTICONTROL (0x000)
  41. #define CTIINTACK (0x010)
  42. #define CTIAPPSET (0x014)
  43. #define CTIAPPCLEAR (0x018)
  44. #define CTIAPPPULSE (0x01C)
  45. #define CTIINEN(n) (0x020 + (n * 4))
  46. #define CTIOUTEN(n) (0x0A0 + (n * 4))
  47. #define CTITRIGINSTATUS (0x130)
  48. #define CTITRIGOUTSTATUS (0x134)
  49. #define CTICHINSTATUS (0x138)
  50. #define CTICHOUTSTATUS (0x13C)
  51. #define CTIGATE (0x140)
  52. #define ASICCTL (0x144)
  53. #define ITCHINACK (0xEDC)
  54. #define ITTRIGINACK (0xEE0)
  55. #define ITCHOUT (0xEE4)
  56. #define ITTRIGOUT (0xEE8)
  57. #define ITCHOUTACK (0xEEC)
  58. #define ITTRIGOUTACK (0xEF0)
  59. #define ITCHIN (0xEF4)
  60. #define ITTRIGIN (0xEF8)
  61. #define CTI_MAX_TRIGGERS (32)
  62. #define CTI_MAX_CHANNELS (4)
  63. #define AFFINITY_LEVEL_L2 1
  64. #define to_cti_drvdata(c) container_of(c, struct cti_drvdata, cti)
  65. struct cti_state {
  66. unsigned int cticontrol;
  67. unsigned int ctiappset;
  68. unsigned int ctigate;
  69. unsigned int ctiinen[CTI_MAX_TRIGGERS];
  70. unsigned int ctiouten[CTI_MAX_TRIGGERS];
  71. };
  72. struct cti_pctrl {
  73. struct pinctrl *pctrl;
  74. int trig;
  75. };
  76. struct cti_drvdata {
  77. void __iomem *base;
  78. struct device *dev;
  79. struct coresight_device *csdev;
  80. struct clk *clk;
  81. spinlock_t spinlock;
  82. struct mutex mutex;
  83. struct coresight_cti cti;
  84. int refcnt;
  85. int cpu;
  86. bool cti_save;
  87. bool cti_hwclk;
  88. bool l2_off;
  89. struct cti_state *state;
  90. struct cti_pctrl *gpio_trigin;
  91. struct cti_pctrl *gpio_trigout;
  92. };
  93. static struct notifier_block cti_cpu_pm_notifier;
  94. static int registered;
  95. static LIST_HEAD(cti_list);
  96. static DEFINE_MUTEX(cti_lock);
  97. #ifdef CONFIG_CORESIGHT_CTI_SAVE_DISABLE
  98. static int cti_save_disable = 1;
  99. #else
  100. static int cti_save_disable;
  101. #endif
  102. static int cti_verify_trigger_bound(int trig)
  103. {
  104. if (trig < 0 || trig >= CTI_MAX_TRIGGERS)
  105. return -EINVAL;
  106. return 0;
  107. }
  108. static int cti_verify_channel_bound(int ch)
  109. {
  110. if (ch < 0 || ch >= CTI_MAX_CHANNELS)
  111. return -EINVAL;
  112. return 0;
  113. }
  114. static int cti_cpu_verify_access(struct cti_drvdata *drvdata)
  115. {
  116. if (drvdata->cti_save && drvdata->l2_off)
  117. return -EPERM;
  118. return 0;
  119. }
  120. void coresight_cti_ctx_save(void)
  121. {
  122. struct cti_drvdata *drvdata;
  123. struct coresight_cti *cti;
  124. int trig, cpuid, cpu;
  125. unsigned long flag;
  126. /*
  127. * Explicitly check and return to avoid latency associated with
  128. * traversing the linked list of all CTIs and checking for their
  129. * respective cti_save flag.
  130. */
  131. if (cti_save_disable)
  132. return;
  133. cpu = raw_smp_processor_id();
  134. list_for_each_entry(cti, &cti_list, link) {
  135. drvdata = to_cti_drvdata(cti);
  136. if (!drvdata->cti_save)
  137. continue;
  138. for_each_cpu(cpuid, topology_core_cpumask(cpu)) {
  139. if (drvdata->cpu == cpuid)
  140. goto out;
  141. }
  142. continue;
  143. out:
  144. spin_lock_irqsave(&drvdata->spinlock, flag);
  145. drvdata->l2_off = true;
  146. drvdata->state->cticontrol = cti_readl(drvdata, CTICONTROL);
  147. drvdata->state->ctiappset = cti_readl(drvdata, CTIAPPSET);
  148. drvdata->state->ctigate = cti_readl(drvdata, CTIGATE);
  149. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  150. drvdata->state->ctiinen[trig] =
  151. cti_readl(drvdata, CTIINEN(trig));
  152. drvdata->state->ctiouten[trig] =
  153. cti_readl(drvdata, CTIOUTEN(trig));
  154. }
  155. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  156. }
  157. }
  158. EXPORT_SYMBOL(coresight_cti_ctx_save);
  159. void coresight_cti_ctx_restore(void)
  160. {
  161. struct cti_drvdata *drvdata;
  162. struct coresight_cti *cti;
  163. int trig, cpuid, cpu;
  164. unsigned long flag;
  165. /*
  166. * Explicitly check and return to avoid latency associated with
  167. * traversing the linked list of all CTIs and checking for their
  168. * respective cti_save flag.
  169. */
  170. if (cti_save_disable)
  171. return;
  172. cpu = raw_smp_processor_id();
  173. list_for_each_entry(cti, &cti_list, link) {
  174. drvdata = to_cti_drvdata(cti);
  175. if (!drvdata->cti_save)
  176. continue;
  177. for_each_cpu(cpuid, topology_core_cpumask(cpu)) {
  178. if (drvdata->cpu == cpuid)
  179. goto out;
  180. }
  181. continue;
  182. out:
  183. spin_lock_irqsave(&drvdata->spinlock, flag);
  184. CTI_UNLOCK(drvdata);
  185. cti_writel(drvdata, drvdata->state->ctiappset, CTIAPPSET);
  186. cti_writel(drvdata, drvdata->state->ctigate, CTIGATE);
  187. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  188. cti_writel(drvdata, drvdata->state->ctiinen[trig],
  189. CTIINEN(trig));
  190. cti_writel(drvdata, drvdata->state->ctiouten[trig],
  191. CTIOUTEN(trig));
  192. }
  193. cti_writel(drvdata, drvdata->state->cticontrol, CTICONTROL);
  194. CTI_LOCK(drvdata);
  195. drvdata->l2_off = false;
  196. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  197. }
  198. }
  199. EXPORT_SYMBOL(coresight_cti_ctx_restore);
  200. static void cti_enable(struct cti_drvdata *drvdata)
  201. {
  202. CTI_UNLOCK(drvdata);
  203. cti_writel(drvdata, 0x1, CTICONTROL);
  204. CTI_LOCK(drvdata);
  205. }
  206. int cti_trigin_gpio_enable(struct cti_drvdata *drvdata)
  207. {
  208. int ret;
  209. struct pinctrl *pctrl;
  210. struct pinctrl_state *pctrl_state;
  211. if (drvdata->gpio_trigin->pctrl)
  212. return 0;
  213. pctrl = devm_pinctrl_get(drvdata->dev);
  214. if (IS_ERR(pctrl)) {
  215. dev_err(drvdata->dev, "pinctrl get failed\n");
  216. return PTR_ERR(pctrl);
  217. }
  218. pctrl_state = pinctrl_lookup_state(pctrl, "cti-trigin-pctrl");
  219. if (IS_ERR(pctrl_state)) {
  220. dev_err(drvdata->dev,
  221. "pinctrl get state failed\n");
  222. ret = PTR_ERR(pctrl_state);
  223. goto err;
  224. }
  225. ret = pinctrl_select_state(pctrl, pctrl_state);
  226. if (ret) {
  227. dev_err(drvdata->dev,
  228. "pinctrl enable state failed\n");
  229. goto err;
  230. }
  231. drvdata->gpio_trigin->pctrl = pctrl;
  232. return 0;
  233. err:
  234. devm_pinctrl_put(pctrl);
  235. return ret;
  236. }
  237. int cti_trigout_gpio_enable(struct cti_drvdata *drvdata)
  238. {
  239. int ret;
  240. struct pinctrl *pctrl;
  241. struct pinctrl_state *pctrl_state;
  242. if (drvdata->gpio_trigout->pctrl)
  243. return 0;
  244. pctrl = devm_pinctrl_get(drvdata->dev);
  245. if (IS_ERR(pctrl)) {
  246. dev_err(drvdata->dev, "pinctrl get failed\n");
  247. return PTR_ERR(pctrl);
  248. }
  249. pctrl_state = pinctrl_lookup_state(pctrl, "cti-trigout-pctrl");
  250. if (IS_ERR(pctrl_state)) {
  251. dev_err(drvdata->dev,
  252. "pinctrl get state failed\n");
  253. ret = PTR_ERR(pctrl_state);
  254. goto err;
  255. }
  256. ret = pinctrl_select_state(pctrl, pctrl_state);
  257. if (ret) {
  258. dev_err(drvdata->dev,
  259. "pinctrl enable state failed\n");
  260. goto err;
  261. }
  262. drvdata->gpio_trigout->pctrl = pctrl;
  263. return 0;
  264. err:
  265. devm_pinctrl_put(pctrl);
  266. return ret;
  267. }
  268. void cti_trigin_gpio_disable(struct cti_drvdata *drvdata)
  269. {
  270. if (!drvdata->gpio_trigin->pctrl)
  271. return;
  272. devm_pinctrl_put(drvdata->gpio_trigin->pctrl);
  273. drvdata->gpio_trigin->pctrl = NULL;
  274. }
  275. void cti_trigout_gpio_disable(struct cti_drvdata *drvdata)
  276. {
  277. if (!drvdata->gpio_trigout->pctrl)
  278. return;
  279. devm_pinctrl_put(drvdata->gpio_trigout->pctrl);
  280. drvdata->gpio_trigout->pctrl = NULL;
  281. }
  282. static void __cti_map_trigin(struct cti_drvdata *drvdata, int trig, int ch)
  283. {
  284. uint32_t ctien;
  285. if (drvdata->refcnt == 0)
  286. cti_enable(drvdata);
  287. CTI_UNLOCK(drvdata);
  288. ctien = cti_readl(drvdata, CTIINEN(trig));
  289. if (ctien & (0x1 << ch))
  290. goto out;
  291. cti_writel(drvdata, (ctien | 0x1 << ch), CTIINEN(trig));
  292. CTI_LOCK(drvdata);
  293. drvdata->refcnt++;
  294. return;
  295. out:
  296. CTI_LOCK(drvdata);
  297. }
  298. int coresight_cti_map_trigin(struct coresight_cti *cti, int trig, int ch)
  299. {
  300. struct cti_drvdata *drvdata;
  301. int ret;
  302. unsigned long flag;
  303. if (IS_ERR_OR_NULL(cti))
  304. return -EINVAL;
  305. ret = cti_verify_trigger_bound(trig);
  306. if (ret)
  307. return ret;
  308. ret = cti_verify_channel_bound(ch);
  309. if (ret)
  310. return ret;
  311. drvdata = to_cti_drvdata(cti);
  312. mutex_lock(&drvdata->mutex);
  313. if (drvdata->gpio_trigin->trig == trig) {
  314. ret = cti_trigin_gpio_enable(drvdata);
  315. if (ret)
  316. goto err0;
  317. }
  318. /*
  319. * refcnt can be used here since in all cases its value is modified only
  320. * within the mutex lock region in addition to within the spinlock.
  321. */
  322. if (drvdata->refcnt == 0) {
  323. ret = pm_runtime_get_sync(drvdata->dev);
  324. if (ret)
  325. goto err1;
  326. }
  327. spin_lock_irqsave(&drvdata->spinlock, flag);
  328. ret = cti_cpu_verify_access(drvdata);
  329. if (ret)
  330. goto err2;
  331. __cti_map_trigin(drvdata, trig, ch);
  332. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  333. mutex_unlock(&drvdata->mutex);
  334. return 0;
  335. err2:
  336. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  337. /*
  338. * We come here before refcnt is potentially modified in
  339. * __cti_map_trigin so it is safe to check it against 0 without
  340. * adjusting its value.
  341. */
  342. if (drvdata->refcnt == 0)
  343. pm_runtime_put(drvdata->dev);
  344. err1:
  345. cti_trigin_gpio_disable(drvdata);
  346. err0:
  347. mutex_unlock(&drvdata->mutex);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(coresight_cti_map_trigin);
  351. static void __cti_map_trigout(struct cti_drvdata *drvdata, int trig, int ch)
  352. {
  353. uint32_t ctien;
  354. if (drvdata->refcnt == 0)
  355. cti_enable(drvdata);
  356. CTI_UNLOCK(drvdata);
  357. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  358. if (ctien & (0x1 << ch))
  359. goto out;
  360. cti_writel(drvdata, (ctien | 0x1 << ch), CTIOUTEN(trig));
  361. CTI_LOCK(drvdata);
  362. drvdata->refcnt++;
  363. return;
  364. out:
  365. CTI_LOCK(drvdata);
  366. }
  367. int coresight_cti_map_trigout(struct coresight_cti *cti, int trig, int ch)
  368. {
  369. struct cti_drvdata *drvdata;
  370. int ret;
  371. unsigned long flag;
  372. if (IS_ERR_OR_NULL(cti))
  373. return -EINVAL;
  374. ret = cti_verify_trigger_bound(trig);
  375. if (ret)
  376. return ret;
  377. ret = cti_verify_channel_bound(ch);
  378. if (ret)
  379. return ret;
  380. drvdata = to_cti_drvdata(cti);
  381. mutex_lock(&drvdata->mutex);
  382. if (drvdata->gpio_trigout->trig == trig) {
  383. ret = cti_trigout_gpio_enable(drvdata);
  384. if (ret)
  385. goto err0;
  386. }
  387. /*
  388. * refcnt can be used here since in all cases its value is modified only
  389. * within the mutex lock region in addition to within the spinlock.
  390. */
  391. if (drvdata->refcnt == 0) {
  392. ret = pm_runtime_get_sync(drvdata->dev);
  393. if (ret)
  394. goto err1;
  395. }
  396. spin_lock_irqsave(&drvdata->spinlock, flag);
  397. ret = cti_cpu_verify_access(drvdata);
  398. if (ret)
  399. goto err2;
  400. __cti_map_trigout(drvdata, trig, ch);
  401. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  402. mutex_unlock(&drvdata->mutex);
  403. return 0;
  404. err2:
  405. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  406. /*
  407. * We come here before refcnt is potentially incremented in
  408. * __cti_map_trigout so it is safe to check it against 0.
  409. */
  410. if (drvdata->refcnt == 0)
  411. pm_runtime_put(drvdata->dev);
  412. err1:
  413. cti_trigout_gpio_disable(drvdata);
  414. err0:
  415. mutex_unlock(&drvdata->mutex);
  416. return ret;
  417. }
  418. EXPORT_SYMBOL(coresight_cti_map_trigout);
  419. static void cti_disable(struct cti_drvdata *drvdata)
  420. {
  421. CTI_UNLOCK(drvdata);
  422. /* Clear any pending triggers and ensure gate is enabled */
  423. cti_writel(drvdata, BM(0, (CTI_MAX_CHANNELS - 1)), CTIAPPCLEAR);
  424. cti_writel(drvdata, BM(0, (CTI_MAX_CHANNELS - 1)), CTIGATE);
  425. cti_writel(drvdata, 0x0, CTICONTROL);
  426. CTI_LOCK(drvdata);
  427. }
  428. static void __cti_unmap_trigin(struct cti_drvdata *drvdata, int trig, int ch)
  429. {
  430. uint32_t ctien;
  431. CTI_UNLOCK(drvdata);
  432. ctien = cti_readl(drvdata, CTIINEN(trig));
  433. if (!(ctien & (0x1 << ch)))
  434. goto out;
  435. cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIINEN(trig));
  436. CTI_LOCK(drvdata);
  437. drvdata->refcnt--;
  438. if (drvdata->refcnt == 0)
  439. cti_disable(drvdata);
  440. return;
  441. out:
  442. CTI_LOCK(drvdata);
  443. }
  444. void coresight_cti_unmap_trigin(struct coresight_cti *cti, int trig, int ch)
  445. {
  446. struct cti_drvdata *drvdata;
  447. unsigned long flag;
  448. if (IS_ERR_OR_NULL(cti))
  449. return;
  450. if (cti_verify_trigger_bound(trig))
  451. return;
  452. if (cti_verify_channel_bound(ch))
  453. return;
  454. drvdata = to_cti_drvdata(cti);
  455. mutex_lock(&drvdata->mutex);
  456. spin_lock_irqsave(&drvdata->spinlock, flag);
  457. if (cti_cpu_verify_access(drvdata))
  458. goto err;
  459. /*
  460. * This is required to avoid clk_disable_unprepare call from being made
  461. * when unmap is called without the corresponding map function call.
  462. */
  463. if (!drvdata->refcnt)
  464. goto err;
  465. __cti_unmap_trigin(drvdata, trig, ch);
  466. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  467. /*
  468. * refcnt can be used here since in all cases its value is modified only
  469. * within the mutex lock region in addition to within the spinlock.
  470. */
  471. if (drvdata->refcnt == 0)
  472. pm_runtime_put(drvdata->dev);
  473. if (drvdata->gpio_trigin->trig == trig)
  474. cti_trigin_gpio_disable(drvdata);
  475. mutex_unlock(&drvdata->mutex);
  476. return;
  477. err:
  478. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  479. mutex_unlock(&drvdata->mutex);
  480. }
  481. EXPORT_SYMBOL(coresight_cti_unmap_trigin);
  482. static void __cti_unmap_trigout(struct cti_drvdata *drvdata, int trig, int ch)
  483. {
  484. uint32_t ctien;
  485. CTI_UNLOCK(drvdata);
  486. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  487. if (!(ctien & (0x1 << ch)))
  488. goto out;
  489. cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIOUTEN(trig));
  490. CTI_LOCK(drvdata);
  491. drvdata->refcnt--;
  492. if (drvdata->refcnt == 0)
  493. cti_disable(drvdata);
  494. return;
  495. out:
  496. CTI_LOCK(drvdata);
  497. }
  498. void coresight_cti_unmap_trigout(struct coresight_cti *cti, int trig, int ch)
  499. {
  500. struct cti_drvdata *drvdata;
  501. unsigned long flag;
  502. if (IS_ERR_OR_NULL(cti))
  503. return;
  504. if (cti_verify_trigger_bound(trig))
  505. return;
  506. if (cti_verify_channel_bound(ch))
  507. return;
  508. drvdata = to_cti_drvdata(cti);
  509. mutex_lock(&drvdata->mutex);
  510. spin_lock_irqsave(&drvdata->spinlock, flag);
  511. if (cti_cpu_verify_access(drvdata))
  512. goto err;
  513. /*
  514. * This is required to avoid clk_disable_unprepare call from being made
  515. * when unmap is called without the corresponding map function call.
  516. */
  517. if (!drvdata->refcnt)
  518. goto err;
  519. __cti_unmap_trigout(drvdata, trig, ch);
  520. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  521. /*
  522. * refcnt can be used here since in all cases its value is modified only
  523. * within the mutex lock region in addition to within the spinlock.
  524. */
  525. if (drvdata->refcnt == 0)
  526. pm_runtime_put(drvdata->dev);
  527. if (drvdata->gpio_trigout->trig == trig)
  528. cti_trigout_gpio_disable(drvdata);
  529. mutex_unlock(&drvdata->mutex);
  530. return;
  531. err:
  532. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  533. mutex_unlock(&drvdata->mutex);
  534. }
  535. EXPORT_SYMBOL(coresight_cti_unmap_trigout);
  536. static void __cti_reset(struct cti_drvdata *drvdata)
  537. {
  538. int trig;
  539. if (!drvdata->refcnt)
  540. return;
  541. CTI_UNLOCK(drvdata);
  542. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  543. cti_writel(drvdata, 0, CTIINEN(trig));
  544. cti_writel(drvdata, 0, CTIOUTEN(trig));
  545. }
  546. CTI_LOCK(drvdata);
  547. cti_disable(drvdata);
  548. drvdata->refcnt = 0;
  549. }
  550. void coresight_cti_reset(struct coresight_cti *cti)
  551. {
  552. struct cti_drvdata *drvdata;
  553. unsigned long flag;
  554. int trig;
  555. int refcnt;
  556. if (IS_ERR_OR_NULL(cti))
  557. return;
  558. drvdata = to_cti_drvdata(cti);
  559. mutex_lock(&drvdata->mutex);
  560. refcnt = drvdata->refcnt;
  561. spin_lock_irqsave(&drvdata->spinlock, flag);
  562. if (cti_cpu_verify_access(drvdata))
  563. goto err;
  564. __cti_reset(drvdata);
  565. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  566. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  567. if (drvdata->gpio_trigin->trig == trig)
  568. cti_trigin_gpio_disable(drvdata);
  569. if (drvdata->gpio_trigout->trig == trig)
  570. cti_trigout_gpio_disable(drvdata);
  571. }
  572. if (refcnt)
  573. pm_runtime_put(drvdata->dev);
  574. mutex_unlock(&drvdata->mutex);
  575. return;
  576. err:
  577. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  578. mutex_unlock(&drvdata->mutex);
  579. }
  580. EXPORT_SYMBOL(coresight_cti_reset);
  581. static int __cti_set_trig(struct cti_drvdata *drvdata, int ch)
  582. {
  583. if (!drvdata->refcnt)
  584. return -EINVAL;
  585. CTI_UNLOCK(drvdata);
  586. cti_writel(drvdata, (1 << ch), CTIAPPSET);
  587. CTI_LOCK(drvdata);
  588. return 0;
  589. }
  590. int coresight_cti_set_trig(struct coresight_cti *cti, int ch)
  591. {
  592. struct cti_drvdata *drvdata;
  593. int ret;
  594. unsigned long flag;
  595. if (IS_ERR_OR_NULL(cti))
  596. return -EINVAL;
  597. ret = cti_verify_channel_bound(ch);
  598. if (ret)
  599. return ret;
  600. drvdata = to_cti_drvdata(cti);
  601. spin_lock_irqsave(&drvdata->spinlock, flag);
  602. ret = cti_cpu_verify_access(drvdata);
  603. if (ret)
  604. goto err;
  605. ret = __cti_set_trig(drvdata, ch);
  606. err:
  607. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  608. return ret;
  609. }
  610. EXPORT_SYMBOL(coresight_cti_set_trig);
  611. static void __cti_clear_trig(struct cti_drvdata *drvdata, int ch)
  612. {
  613. if (!drvdata->refcnt)
  614. return;
  615. CTI_UNLOCK(drvdata);
  616. cti_writel(drvdata, (1 << ch), CTIAPPCLEAR);
  617. CTI_LOCK(drvdata);
  618. }
  619. void coresight_cti_clear_trig(struct coresight_cti *cti, int ch)
  620. {
  621. struct cti_drvdata *drvdata;
  622. unsigned long flag;
  623. if (IS_ERR_OR_NULL(cti))
  624. return;
  625. if (cti_verify_channel_bound(ch))
  626. return;
  627. drvdata = to_cti_drvdata(cti);
  628. spin_lock_irqsave(&drvdata->spinlock, flag);
  629. if (cti_cpu_verify_access(drvdata))
  630. goto err;
  631. __cti_clear_trig(drvdata, ch);
  632. err:
  633. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  634. }
  635. EXPORT_SYMBOL(coresight_cti_clear_trig);
  636. static int __cti_pulse_trig(struct cti_drvdata *drvdata, int ch)
  637. {
  638. if (!drvdata->refcnt)
  639. return -EINVAL;
  640. CTI_UNLOCK(drvdata);
  641. cti_writel(drvdata, (1 << ch), CTIAPPPULSE);
  642. CTI_LOCK(drvdata);
  643. return 0;
  644. }
  645. int coresight_cti_pulse_trig(struct coresight_cti *cti, int ch)
  646. {
  647. struct cti_drvdata *drvdata;
  648. int ret;
  649. unsigned long flag;
  650. if (IS_ERR_OR_NULL(cti))
  651. return -EINVAL;
  652. ret = cti_verify_channel_bound(ch);
  653. if (ret)
  654. return ret;
  655. drvdata = to_cti_drvdata(cti);
  656. spin_lock_irqsave(&drvdata->spinlock, flag);
  657. ret = cti_cpu_verify_access(drvdata);
  658. if (ret)
  659. goto err;
  660. ret = __cti_pulse_trig(drvdata, ch);
  661. err:
  662. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(coresight_cti_pulse_trig);
  666. static int __cti_ack_trig(struct cti_drvdata *drvdata, int trig)
  667. {
  668. if (!drvdata->refcnt)
  669. return -EINVAL;
  670. CTI_UNLOCK(drvdata);
  671. cti_writel(drvdata, (0x1 << trig), CTIINTACK);
  672. CTI_LOCK(drvdata);
  673. return 0;
  674. }
  675. int coresight_cti_ack_trig(struct coresight_cti *cti, int trig)
  676. {
  677. struct cti_drvdata *drvdata;
  678. int ret;
  679. unsigned long flag;
  680. if (IS_ERR_OR_NULL(cti))
  681. return -EINVAL;
  682. ret = cti_verify_trigger_bound(trig);
  683. if (ret)
  684. return ret;
  685. drvdata = to_cti_drvdata(cti);
  686. spin_lock_irqsave(&drvdata->spinlock, flag);
  687. ret = cti_cpu_verify_access(drvdata);
  688. if (ret)
  689. goto err;
  690. ret = __cti_ack_trig(drvdata, trig);
  691. err:
  692. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL(coresight_cti_ack_trig);
  696. static int __cti_enable_gate(struct cti_drvdata *drvdata, int ch)
  697. {
  698. uint32_t ctigate;
  699. if (!drvdata->refcnt)
  700. return -EINVAL;
  701. CTI_UNLOCK(drvdata);
  702. ctigate = cti_readl(drvdata, CTIGATE);
  703. cti_writel(drvdata, (ctigate & ~(1 << ch)), CTIGATE);
  704. CTI_LOCK(drvdata);
  705. return 0;
  706. }
  707. int coresight_cti_enable_gate(struct coresight_cti *cti, int ch)
  708. {
  709. struct cti_drvdata *drvdata;
  710. int ret;
  711. unsigned long flag;
  712. if (IS_ERR_OR_NULL(cti))
  713. return -EINVAL;
  714. ret = cti_verify_channel_bound(ch);
  715. if (ret)
  716. return ret;
  717. drvdata = to_cti_drvdata(cti);
  718. spin_lock_irqsave(&drvdata->spinlock, flag);
  719. ret = cti_cpu_verify_access(drvdata);
  720. if (ret)
  721. goto err;
  722. ret = __cti_enable_gate(drvdata, ch);
  723. err:
  724. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  725. return ret;
  726. }
  727. EXPORT_SYMBOL(coresight_cti_enable_gate);
  728. static void __cti_disable_gate(struct cti_drvdata *drvdata, int ch)
  729. {
  730. uint32_t ctigate;
  731. if (!drvdata->refcnt)
  732. return;
  733. CTI_UNLOCK(drvdata);
  734. ctigate = cti_readl(drvdata, CTIGATE);
  735. cti_writel(drvdata, (ctigate | (1 << ch)), CTIGATE);
  736. CTI_LOCK(drvdata);
  737. }
  738. void coresight_cti_disable_gate(struct coresight_cti *cti, int ch)
  739. {
  740. struct cti_drvdata *drvdata;
  741. unsigned long flag;
  742. if (IS_ERR_OR_NULL(cti))
  743. return;
  744. if (cti_verify_channel_bound(ch))
  745. return;
  746. drvdata = to_cti_drvdata(cti);
  747. spin_lock_irqsave(&drvdata->spinlock, flag);
  748. if (cti_cpu_verify_access(drvdata))
  749. goto err;
  750. __cti_disable_gate(drvdata, ch);
  751. err:
  752. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  753. }
  754. EXPORT_SYMBOL(coresight_cti_disable_gate);
  755. struct coresight_cti *coresight_cti_get(const char *name)
  756. {
  757. struct coresight_cti *cti;
  758. mutex_lock(&cti_lock);
  759. list_for_each_entry(cti, &cti_list, link) {
  760. if (!strcmp(cti->name, name)) {
  761. mutex_unlock(&cti_lock);
  762. return cti;
  763. }
  764. }
  765. mutex_unlock(&cti_lock);
  766. return ERR_PTR(-EINVAL);
  767. }
  768. EXPORT_SYMBOL(coresight_cti_get);
  769. void coresight_cti_put(struct coresight_cti *cti)
  770. {
  771. }
  772. EXPORT_SYMBOL(coresight_cti_put);
  773. static ssize_t cti_show_trigin(struct device *dev,
  774. struct device_attribute *attr, char *buf)
  775. {
  776. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  777. unsigned long trig, ch, flag;
  778. uint32_t ctien;
  779. ssize_t size = 0;
  780. mutex_lock(&drvdata->mutex);
  781. /*
  782. * refcnt can be used here since in all cases its value is modified only
  783. * within the mutex lock region in addition to within the spinlock.
  784. */
  785. if (!drvdata->refcnt)
  786. goto err;
  787. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  788. spin_lock_irqsave(&drvdata->spinlock, flag);
  789. if (!cti_cpu_verify_access(drvdata))
  790. ctien = cti_readl(drvdata, CTIINEN(trig));
  791. else
  792. ctien = drvdata->state->ctiinen[trig];
  793. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  794. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  795. if (ctien & (1 << ch)) {
  796. /* Ensure we do not write more than PAGE_SIZE
  797. * bytes of data including \n character and null
  798. * terminator
  799. */
  800. size += scnprintf(&buf[size], PAGE_SIZE - size -
  801. 1, " %#lx %#lx,", trig, ch);
  802. if (size >= PAGE_SIZE - 2) {
  803. dev_err(dev, "show buffer full\n");
  804. goto err;
  805. }
  806. }
  807. }
  808. }
  809. err:
  810. size += scnprintf(&buf[size], 2, "\n");
  811. mutex_unlock(&drvdata->mutex);
  812. return size;
  813. }
  814. static DEVICE_ATTR(show_trigin, 0444, cti_show_trigin, NULL);
  815. static ssize_t cti_show_trigout(struct device *dev,
  816. struct device_attribute *attr, char *buf)
  817. {
  818. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  819. unsigned long trig, ch, flag;
  820. uint32_t ctien;
  821. ssize_t size = 0;
  822. mutex_lock(&drvdata->mutex);
  823. /*
  824. * refcnt can be used here since in all cases its value is modified only
  825. * within the mutex lock region in addition to within the spinlock.
  826. */
  827. if (!drvdata->refcnt)
  828. goto err;
  829. for (trig = 0; trig < CTI_MAX_TRIGGERS; trig++) {
  830. spin_lock_irqsave(&drvdata->spinlock, flag);
  831. if (!cti_cpu_verify_access(drvdata))
  832. ctien = cti_readl(drvdata, CTIOUTEN(trig));
  833. else
  834. ctien = drvdata->state->ctiouten[trig];
  835. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  836. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  837. if (ctien & (1 << ch)) {
  838. /* Ensure we do not write more than PAGE_SIZE
  839. * bytes of data including \n character and null
  840. * terminator
  841. */
  842. size += scnprintf(&buf[size], PAGE_SIZE - size -
  843. 1, " %#lx %#lx,", trig, ch);
  844. if (size >= PAGE_SIZE - 2) {
  845. dev_err(dev, "show buffer full\n");
  846. goto err;
  847. }
  848. }
  849. }
  850. }
  851. err:
  852. size += scnprintf(&buf[size], 2, "\n");
  853. mutex_unlock(&drvdata->mutex);
  854. return size;
  855. }
  856. static DEVICE_ATTR(show_trigout, 0444, cti_show_trigout, NULL);
  857. static ssize_t cti_store_map_trigin(struct device *dev,
  858. struct device_attribute *attr,
  859. const char *buf, size_t size)
  860. {
  861. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  862. unsigned long val1, val2;
  863. int ret;
  864. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  865. return -EINVAL;
  866. ret = coresight_cti_map_trigin(&drvdata->cti, val1, val2);
  867. if (ret)
  868. return ret;
  869. return size;
  870. }
  871. static DEVICE_ATTR(map_trigin, 0200, NULL, cti_store_map_trigin);
  872. static ssize_t cti_store_map_trigout(struct device *dev,
  873. struct device_attribute *attr,
  874. const char *buf, size_t size)
  875. {
  876. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  877. unsigned long val1, val2;
  878. int ret;
  879. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  880. return -EINVAL;
  881. ret = coresight_cti_map_trigout(&drvdata->cti, val1, val2);
  882. if (ret)
  883. return ret;
  884. return size;
  885. }
  886. static DEVICE_ATTR(map_trigout, 0200, NULL, cti_store_map_trigout);
  887. static ssize_t cti_store_unmap_trigin(struct device *dev,
  888. struct device_attribute *attr,
  889. const char *buf, size_t size)
  890. {
  891. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  892. unsigned long val1, val2;
  893. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  894. return -EINVAL;
  895. coresight_cti_unmap_trigin(&drvdata->cti, val1, val2);
  896. return size;
  897. }
  898. static DEVICE_ATTR(unmap_trigin, 0200, NULL, cti_store_unmap_trigin);
  899. static ssize_t cti_store_unmap_trigout(struct device *dev,
  900. struct device_attribute *attr,
  901. const char *buf, size_t size)
  902. {
  903. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  904. unsigned long val1, val2;
  905. if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
  906. return -EINVAL;
  907. coresight_cti_unmap_trigout(&drvdata->cti, val1, val2);
  908. return size;
  909. }
  910. static DEVICE_ATTR(unmap_trigout, 0200, NULL, cti_store_unmap_trigout);
  911. static ssize_t cti_store_reset(struct device *dev,
  912. struct device_attribute *attr,
  913. const char *buf, size_t size)
  914. {
  915. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  916. unsigned long val;
  917. if (kstrtoul(buf, 16, &val))
  918. return -EINVAL;
  919. if (!val)
  920. return -EINVAL;
  921. coresight_cti_reset(&drvdata->cti);
  922. return size;
  923. }
  924. static DEVICE_ATTR(reset, 0200, NULL, cti_store_reset);
  925. static ssize_t cti_show_trig(struct device *dev, struct device_attribute *attr,
  926. char *buf)
  927. {
  928. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  929. unsigned long ch, flag;
  930. uint32_t ctiset;
  931. ssize_t size = 0;
  932. mutex_lock(&drvdata->mutex);
  933. /*
  934. * refcnt can be used here since in all cases its value is modified only
  935. * within the mutex lock region in addition to within the spinlock.
  936. */
  937. if (!drvdata->refcnt)
  938. goto err;
  939. spin_lock_irqsave(&drvdata->spinlock, flag);
  940. if (!cti_cpu_verify_access(drvdata))
  941. ctiset = cti_readl(drvdata, CTIAPPSET);
  942. else
  943. ctiset = drvdata->state->ctiappset;
  944. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  945. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  946. if (ctiset & (1 << ch)) {
  947. /* Ensure we do not write more than PAGE_SIZE
  948. * bytes of data including \n character and null
  949. * terminator
  950. */
  951. size += scnprintf(&buf[size], PAGE_SIZE - size -
  952. 1, " %#lx,", ch);
  953. if (size >= PAGE_SIZE - 2) {
  954. dev_err(dev, "show buffer full\n");
  955. goto err;
  956. }
  957. }
  958. }
  959. err:
  960. size += scnprintf(&buf[size], 2, "\n");
  961. mutex_unlock(&drvdata->mutex);
  962. return size;
  963. }
  964. static DEVICE_ATTR(show_trig, 0444, cti_show_trig, NULL);
  965. static ssize_t cti_store_set_trig(struct device *dev,
  966. struct device_attribute *attr,
  967. const char *buf, size_t size)
  968. {
  969. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  970. unsigned long val;
  971. int ret;
  972. if (kstrtoul(buf, 16, &val))
  973. return -EINVAL;
  974. ret = coresight_cti_set_trig(&drvdata->cti, val);
  975. if (ret)
  976. return ret;
  977. return size;
  978. }
  979. static DEVICE_ATTR(set_trig, 0200, NULL, cti_store_set_trig);
  980. static ssize_t cti_store_clear_trig(struct device *dev,
  981. struct device_attribute *attr,
  982. const char *buf, size_t size)
  983. {
  984. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  985. unsigned long val;
  986. if (kstrtoul(buf, 16, &val))
  987. return -EINVAL;
  988. coresight_cti_clear_trig(&drvdata->cti, val);
  989. return size;
  990. }
  991. static DEVICE_ATTR(clear_trig, 0200, NULL, cti_store_clear_trig);
  992. static ssize_t cti_store_pulse_trig(struct device *dev,
  993. struct device_attribute *attr,
  994. const char *buf, size_t size)
  995. {
  996. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  997. unsigned long val;
  998. int ret;
  999. if (kstrtoul(buf, 16, &val))
  1000. return -EINVAL;
  1001. ret = coresight_cti_pulse_trig(&drvdata->cti, val);
  1002. if (ret)
  1003. return ret;
  1004. return size;
  1005. }
  1006. static DEVICE_ATTR(pulse_trig, 0200, NULL, cti_store_pulse_trig);
  1007. static ssize_t cti_store_ack_trig(struct device *dev,
  1008. struct device_attribute *attr,
  1009. const char *buf, size_t size)
  1010. {
  1011. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  1012. unsigned long val;
  1013. int ret;
  1014. if (kstrtoul(buf, 16, &val))
  1015. return -EINVAL;
  1016. ret = coresight_cti_ack_trig(&drvdata->cti, val);
  1017. if (ret)
  1018. return ret;
  1019. return size;
  1020. }
  1021. static DEVICE_ATTR(ack_trig, 0200, NULL, cti_store_ack_trig);
  1022. static ssize_t cti_show_gate(struct device *dev, struct device_attribute *attr,
  1023. char *buf)
  1024. {
  1025. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  1026. unsigned long ch, flag;
  1027. uint32_t ctigate;
  1028. ssize_t size = 0;
  1029. mutex_lock(&drvdata->mutex);
  1030. /*
  1031. * refcnt can be used here since in all cases its value is modified only
  1032. * within the mutex lock region in addition to within the spinlock.
  1033. */
  1034. if (!drvdata->refcnt)
  1035. goto err;
  1036. spin_lock_irqsave(&drvdata->spinlock, flag);
  1037. if (!cti_cpu_verify_access(drvdata))
  1038. ctigate = cti_readl(drvdata, CTIGATE);
  1039. else
  1040. ctigate = drvdata->state->ctigate;
  1041. spin_unlock_irqrestore(&drvdata->spinlock, flag);
  1042. for (ch = 0; ch < CTI_MAX_CHANNELS; ch++) {
  1043. if (ctigate & (1 << ch)) {
  1044. /* Ensure we do not write more than PAGE_SIZE
  1045. * bytes of data including \n character and null
  1046. * terminator
  1047. */
  1048. size += scnprintf(&buf[size], PAGE_SIZE - size -
  1049. 1, " %#lx,", ch);
  1050. if (size >= PAGE_SIZE - 2) {
  1051. dev_err(dev, "show buffer full\n");
  1052. goto err;
  1053. }
  1054. }
  1055. }
  1056. err:
  1057. size += scnprintf(&buf[size], 2, "\n");
  1058. mutex_unlock(&drvdata->mutex);
  1059. return size;
  1060. }
  1061. static DEVICE_ATTR(show_gate, 0444, cti_show_gate, NULL);
  1062. static ssize_t cti_store_enable_gate(struct device *dev,
  1063. struct device_attribute *attr,
  1064. const char *buf, size_t size)
  1065. {
  1066. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  1067. unsigned long val;
  1068. int ret;
  1069. if (kstrtoul(buf, 16, &val))
  1070. return -EINVAL;
  1071. ret = coresight_cti_enable_gate(&drvdata->cti, val);
  1072. if (ret)
  1073. return ret;
  1074. return size;
  1075. }
  1076. static DEVICE_ATTR(enable_gate, 0200, NULL, cti_store_enable_gate);
  1077. static ssize_t cti_store_disable_gate(struct device *dev,
  1078. struct device_attribute *attr,
  1079. const char *buf, size_t size)
  1080. {
  1081. struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
  1082. unsigned long val;
  1083. if (kstrtoul(buf, 16, &val))
  1084. return -EINVAL;
  1085. coresight_cti_disable_gate(&drvdata->cti, val);
  1086. return size;
  1087. }
  1088. static DEVICE_ATTR(disable_gate, 0200, NULL, cti_store_disable_gate);
  1089. static struct attribute *cti_attrs[] = {
  1090. &dev_attr_show_trigin.attr,
  1091. &dev_attr_show_trigout.attr,
  1092. &dev_attr_map_trigin.attr,
  1093. &dev_attr_map_trigout.attr,
  1094. &dev_attr_unmap_trigin.attr,
  1095. &dev_attr_unmap_trigout.attr,
  1096. &dev_attr_reset.attr,
  1097. &dev_attr_show_trig.attr,
  1098. &dev_attr_set_trig.attr,
  1099. &dev_attr_clear_trig.attr,
  1100. &dev_attr_pulse_trig.attr,
  1101. &dev_attr_ack_trig.attr,
  1102. &dev_attr_show_gate.attr,
  1103. &dev_attr_enable_gate.attr,
  1104. &dev_attr_disable_gate.attr,
  1105. NULL,
  1106. };
  1107. static struct attribute_group cti_attr_grp = {
  1108. .attrs = cti_attrs,
  1109. };
  1110. static const struct attribute_group *cti_attr_grps[] = {
  1111. &cti_attr_grp,
  1112. NULL,
  1113. };
  1114. static int cti_cpu_pm_callback(struct notifier_block *self,
  1115. unsigned long cmd, void *v)
  1116. {
  1117. unsigned long aff_level = (unsigned long) v;
  1118. switch (cmd) {
  1119. case CPU_CLUSTER_PM_ENTER:
  1120. if (aff_level == AFFINITY_LEVEL_L2)
  1121. coresight_cti_ctx_save();
  1122. break;
  1123. case CPU_CLUSTER_PM_ENTER_FAILED:
  1124. case CPU_CLUSTER_PM_EXIT:
  1125. if (aff_level == AFFINITY_LEVEL_L2)
  1126. coresight_cti_ctx_restore();
  1127. break;
  1128. }
  1129. return NOTIFY_OK;
  1130. }
  1131. static struct notifier_block cti_cpu_pm_notifier = {
  1132. .notifier_call = cti_cpu_pm_callback,
  1133. };
  1134. static int cti_probe(struct amba_device *adev, const struct amba_id *id)
  1135. {
  1136. int ret;
  1137. int trig;
  1138. struct device *dev = &adev->dev;
  1139. struct coresight_platform_data *pdata;
  1140. struct cti_drvdata *drvdata;
  1141. struct coresight_desc *desc;
  1142. struct device_node *cpu_node;
  1143. pdata = of_get_coresight_platform_data(dev, adev->dev.of_node);
  1144. if (IS_ERR(pdata))
  1145. return PTR_ERR(pdata);
  1146. adev->dev.platform_data = pdata;
  1147. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  1148. if (!drvdata)
  1149. return -ENOMEM;
  1150. /* Store the driver data pointer for use in exported functions */
  1151. drvdata->dev = &adev->dev;
  1152. dev_set_drvdata(dev, drvdata);
  1153. drvdata->base = devm_ioremap_resource(dev, &adev->res);
  1154. if (!drvdata->base)
  1155. return -ENOMEM;
  1156. spin_lock_init(&drvdata->spinlock);
  1157. mutex_init(&drvdata->mutex);
  1158. drvdata->gpio_trigin = devm_kzalloc(dev, sizeof(struct cti_pctrl),
  1159. GFP_KERNEL);
  1160. if (!drvdata->gpio_trigin)
  1161. return -ENOMEM;
  1162. drvdata->gpio_trigin->trig = -1;
  1163. ret = of_property_read_u32(adev->dev.of_node,
  1164. "qcom,cti-gpio-trigin", &trig);
  1165. if (!ret)
  1166. drvdata->gpio_trigin->trig = trig;
  1167. else if (ret != -EINVAL)
  1168. return ret;
  1169. drvdata->gpio_trigout = devm_kzalloc(dev, sizeof(struct cti_pctrl),
  1170. GFP_KERNEL);
  1171. if (!drvdata->gpio_trigout)
  1172. return -ENOMEM;
  1173. drvdata->gpio_trigout->trig = -1;
  1174. ret = of_property_read_u32(adev->dev.of_node,
  1175. "qcom,cti-gpio-trigout", &trig);
  1176. if (!ret)
  1177. drvdata->gpio_trigout->trig = trig;
  1178. else if (ret != -EINVAL)
  1179. return ret;
  1180. drvdata->cpu = -1;
  1181. cpu_node = of_parse_phandle(adev->dev.of_node, "cpu", 0);
  1182. if (cpu_node) {
  1183. drvdata->cpu = pdata ? pdata->cpu : -1;
  1184. if (drvdata->cpu == -1) {
  1185. dev_err(drvdata->dev, "CTI cpu node invalid\n");
  1186. return -EINVAL;
  1187. }
  1188. }
  1189. if (!cti_save_disable)
  1190. drvdata->cti_save = of_property_read_bool(adev->dev.of_node,
  1191. "qcom,cti-save");
  1192. if (drvdata->cti_save) {
  1193. drvdata->state = devm_kzalloc(dev, sizeof(struct cti_state),
  1194. GFP_KERNEL);
  1195. if (!drvdata->state)
  1196. return -ENOMEM;
  1197. drvdata->cti_hwclk = of_property_read_bool(adev->dev.of_node,
  1198. "qcom,cti-hwclk");
  1199. }
  1200. if (drvdata->cti_save && !drvdata->cti_hwclk) {
  1201. ret = pm_runtime_get_sync(drvdata->dev);
  1202. if (ret)
  1203. return ret;
  1204. }
  1205. mutex_lock(&cti_lock);
  1206. drvdata->cti.name = ((struct coresight_platform_data *)
  1207. (adev->dev.platform_data))->name;
  1208. list_add_tail(&drvdata->cti.link, &cti_list);
  1209. mutex_unlock(&cti_lock);
  1210. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  1211. if (!desc) {
  1212. ret = -ENOMEM;
  1213. goto err;
  1214. }
  1215. desc->type = CORESIGHT_DEV_TYPE_NONE;
  1216. desc->pdata = adev->dev.platform_data;
  1217. desc->dev = &adev->dev;
  1218. desc->groups = cti_attr_grps;
  1219. drvdata->csdev = coresight_register(desc);
  1220. if (IS_ERR(drvdata->csdev)) {
  1221. ret = PTR_ERR(drvdata->csdev);
  1222. goto err;
  1223. }
  1224. if (drvdata->cti_save) {
  1225. if (!registered)
  1226. cpu_pm_register_notifier(&cti_cpu_pm_notifier);
  1227. registered++;
  1228. }
  1229. pm_runtime_put(&adev->dev);
  1230. dev_dbg(dev, "CTI initialized\n");
  1231. return 0;
  1232. err:
  1233. if (drvdata->cti_save && !drvdata->cti_hwclk)
  1234. pm_runtime_put(&adev->dev);
  1235. return ret;
  1236. }
  1237. static struct amba_id cti_ids[] = {
  1238. {
  1239. .id = 0x0003b966,
  1240. .mask = 0x0003ffff,
  1241. .data = "CTI",
  1242. },
  1243. { 0, 0},
  1244. };
  1245. static struct amba_driver cti_driver = {
  1246. .drv = {
  1247. .name = "coresight-cti",
  1248. .owner = THIS_MODULE,
  1249. .suppress_bind_attrs = true,
  1250. },
  1251. .probe = cti_probe,
  1252. .id_table = cti_ids,
  1253. };
  1254. builtin_amba_driver(cti_driver);
  1255. MODULE_LICENSE("GPL v2");
  1256. MODULE_DESCRIPTION("CoreSight CTI driver");