0006-ithc.patch 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. From bccd64f261b835edd1abfc24c2f6ec170fb54728 Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Sun, 11 Dec 2022 12:03:38 +0100
  4. Subject: [PATCH] iommu: intel: Disable source id verification for ITHC
  5. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  6. Patchset: ithc
  7. ---
  8. drivers/iommu/intel/irq_remapping.c | 16 ++++++++++++++++
  9. 1 file changed, 16 insertions(+)
  10. diff --git a/drivers/iommu/intel/irq_remapping.c b/drivers/iommu/intel/irq_remapping.c
  11. index 466c1412dd45..565686813588 100644
  12. --- a/drivers/iommu/intel/irq_remapping.c
  13. +++ b/drivers/iommu/intel/irq_remapping.c
  14. @@ -389,6 +389,22 @@ static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  15. data.busmatch_count = 0;
  16. pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
  17. + /*
  18. + * The Intel Touch Host Controller is at 00:10.6, but for some reason
  19. + * the MSI interrupts have request id 01:05.0.
  20. + * Disable id verification to work around this.
  21. + * FIXME Find proper fix or turn this into a quirk.
  22. + */
  23. + if (dev->vendor == PCI_VENDOR_ID_INTEL && (dev->class >> 8) == PCI_CLASS_INPUT_PEN) {
  24. + switch(dev->device) {
  25. + case 0x98d0: case 0x98d1: // LKF
  26. + case 0xa0d0: case 0xa0d1: // TGL LP
  27. + case 0x43d0: case 0x43d1: // TGL H
  28. + set_irte_sid(irte, SVT_NO_VERIFY, SQ_ALL_16, 0);
  29. + return 0;
  30. + }
  31. + }
  32. +
  33. /*
  34. * DMA alias provides us with a PCI device and alias. The only case
  35. * where the it will return an alias on a different bus than the
  36. --
  37. 2.49.0
  38. From 4ad821daf98ea471e649a4c7e5be4c4ea140000b Mon Sep 17 00:00:00 2001
  39. From: quo <tuple@list.ru>
  40. Date: Sun, 11 Dec 2022 12:10:54 +0100
  41. Subject: [PATCH] hid: Add support for Intel Touch Host Controller
  42. Based on quo/ithc-linux@34539af4726d.
  43. Signed-off-by: Maximilian Stoll <luzmaximilian@gmail.com>
  44. Patchset: ithc
  45. ---
  46. drivers/hid/Kconfig | 2 +
  47. drivers/hid/Makefile | 1 +
  48. drivers/hid/ithc/Kbuild | 6 +
  49. drivers/hid/ithc/Kconfig | 12 +
  50. drivers/hid/ithc/ithc-debug.c | 149 ++++++++
  51. drivers/hid/ithc/ithc-debug.h | 7 +
  52. drivers/hid/ithc/ithc-dma.c | 312 ++++++++++++++++
  53. drivers/hid/ithc/ithc-dma.h | 47 +++
  54. drivers/hid/ithc/ithc-hid.c | 207 +++++++++++
  55. drivers/hid/ithc/ithc-hid.h | 32 ++
  56. drivers/hid/ithc/ithc-legacy.c | 254 +++++++++++++
  57. drivers/hid/ithc/ithc-legacy.h | 8 +
  58. drivers/hid/ithc/ithc-main.c | 431 ++++++++++++++++++++++
  59. drivers/hid/ithc/ithc-quickspi.c | 607 +++++++++++++++++++++++++++++++
  60. drivers/hid/ithc/ithc-quickspi.h | 39 ++
  61. drivers/hid/ithc/ithc-regs.c | 154 ++++++++
  62. drivers/hid/ithc/ithc-regs.h | 211 +++++++++++
  63. drivers/hid/ithc/ithc.h | 89 +++++
  64. 18 files changed, 2568 insertions(+)
  65. create mode 100644 drivers/hid/ithc/Kbuild
  66. create mode 100644 drivers/hid/ithc/Kconfig
  67. create mode 100644 drivers/hid/ithc/ithc-debug.c
  68. create mode 100644 drivers/hid/ithc/ithc-debug.h
  69. create mode 100644 drivers/hid/ithc/ithc-dma.c
  70. create mode 100644 drivers/hid/ithc/ithc-dma.h
  71. create mode 100644 drivers/hid/ithc/ithc-hid.c
  72. create mode 100644 drivers/hid/ithc/ithc-hid.h
  73. create mode 100644 drivers/hid/ithc/ithc-legacy.c
  74. create mode 100644 drivers/hid/ithc/ithc-legacy.h
  75. create mode 100644 drivers/hid/ithc/ithc-main.c
  76. create mode 100644 drivers/hid/ithc/ithc-quickspi.c
  77. create mode 100644 drivers/hid/ithc/ithc-quickspi.h
  78. create mode 100644 drivers/hid/ithc/ithc-regs.c
  79. create mode 100644 drivers/hid/ithc/ithc-regs.h
  80. create mode 100644 drivers/hid/ithc/ithc.h
  81. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  82. index 799aceeeea89..8b09289cbe8a 100644
  83. --- a/drivers/hid/Kconfig
  84. +++ b/drivers/hid/Kconfig
  85. @@ -1389,4 +1389,6 @@ source "drivers/hid/surface-hid/Kconfig"
  86. source "drivers/hid/ipts/Kconfig"
  87. +source "drivers/hid/ithc/Kconfig"
  88. +
  89. endif # HID_SUPPORT
  90. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  91. index b82016429b01..9ac9e13ad64c 100644
  92. --- a/drivers/hid/Makefile
  93. +++ b/drivers/hid/Makefile
  94. @@ -172,3 +172,4 @@ obj-$(CONFIG_AMD_SFH_HID) += amd-sfh-hid/
  95. obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  96. obj-$(CONFIG_HID_IPTS) += ipts/
  97. +obj-$(CONFIG_HID_ITHC) += ithc/
  98. diff --git a/drivers/hid/ithc/Kbuild b/drivers/hid/ithc/Kbuild
  99. new file mode 100644
  100. index 000000000000..4937ba131297
  101. --- /dev/null
  102. +++ b/drivers/hid/ithc/Kbuild
  103. @@ -0,0 +1,6 @@
  104. +obj-$(CONFIG_HID_ITHC) := ithc.o
  105. +
  106. +ithc-objs := ithc-main.o ithc-regs.o ithc-dma.o ithc-hid.o ithc-legacy.o ithc-quickspi.o ithc-debug.o
  107. +
  108. +ccflags-y := -std=gnu11 -Wno-declaration-after-statement
  109. +
  110. diff --git a/drivers/hid/ithc/Kconfig b/drivers/hid/ithc/Kconfig
  111. new file mode 100644
  112. index 000000000000..ede713023609
  113. --- /dev/null
  114. +++ b/drivers/hid/ithc/Kconfig
  115. @@ -0,0 +1,12 @@
  116. +config HID_ITHC
  117. + tristate "Intel Touch Host Controller"
  118. + depends on PCI
  119. + depends on HID
  120. + help
  121. + Say Y here if your system has a touchscreen using Intels
  122. + Touch Host Controller (ITHC / IPTS) technology.
  123. +
  124. + If unsure say N.
  125. +
  126. + To compile this driver as a module, choose M here: the
  127. + module will be called ithc.
  128. diff --git a/drivers/hid/ithc/ithc-debug.c b/drivers/hid/ithc/ithc-debug.c
  129. new file mode 100644
  130. index 000000000000..2d8c6afe9966
  131. --- /dev/null
  132. +++ b/drivers/hid/ithc/ithc-debug.c
  133. @@ -0,0 +1,149 @@
  134. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  135. +
  136. +#include "ithc.h"
  137. +
  138. +void ithc_log_regs(struct ithc *ithc)
  139. +{
  140. + if (!ithc->prev_regs)
  141. + return;
  142. + u32 __iomem *cur = (__iomem void *)ithc->regs;
  143. + u32 *prev = (void *)ithc->prev_regs;
  144. + for (int i = 1024; i < sizeof(*ithc->regs) / 4; i++) {
  145. + u32 x = readl(cur + i);
  146. + if (x != prev[i]) {
  147. + pci_info(ithc->pci, "reg %04x: %08x -> %08x\n", i * 4, prev[i], x);
  148. + prev[i] = x;
  149. + }
  150. + }
  151. +}
  152. +
  153. +static ssize_t ithc_debugfs_cmd_write(struct file *f, const char __user *buf, size_t len,
  154. + loff_t *offset)
  155. +{
  156. + // Debug commands consist of a single letter followed by a list of numbers (decimal or
  157. + // hexadecimal, space-separated).
  158. + struct ithc *ithc = file_inode(f)->i_private;
  159. + char cmd[256];
  160. + if (!ithc || !ithc->pci)
  161. + return -ENODEV;
  162. + if (!len)
  163. + return -EINVAL;
  164. + if (len >= sizeof(cmd))
  165. + return -EINVAL;
  166. + if (copy_from_user(cmd, buf, len))
  167. + return -EFAULT;
  168. + cmd[len] = 0;
  169. + if (cmd[len-1] == '\n')
  170. + cmd[len-1] = 0;
  171. + pci_info(ithc->pci, "debug command: %s\n", cmd);
  172. +
  173. + // Parse the list of arguments into a u32 array.
  174. + u32 n = 0;
  175. + const char *s = cmd + 1;
  176. + u32 a[32];
  177. + while (*s && *s != '\n') {
  178. + if (n >= ARRAY_SIZE(a))
  179. + return -EINVAL;
  180. + if (*s++ != ' ')
  181. + return -EINVAL;
  182. + char *e;
  183. + a[n++] = simple_strtoul(s, &e, 0);
  184. + if (e == s)
  185. + return -EINVAL;
  186. + s = e;
  187. + }
  188. + ithc_log_regs(ithc);
  189. +
  190. + // Execute the command.
  191. + switch (cmd[0]) {
  192. + case 'x': // reset
  193. + ithc_reset(ithc);
  194. + break;
  195. + case 'w': // write register: offset mask value
  196. + if (n != 3 || (a[0] & 3))
  197. + return -EINVAL;
  198. + pci_info(ithc->pci, "debug write 0x%04x = 0x%08x (mask 0x%08x)\n",
  199. + a[0], a[2], a[1]);
  200. + bitsl(((__iomem u32 *)ithc->regs) + a[0] / 4, a[1], a[2]);
  201. + break;
  202. + case 'r': // read register: offset
  203. + if (n != 1 || (a[0] & 3))
  204. + return -EINVAL;
  205. + pci_info(ithc->pci, "debug read 0x%04x = 0x%08x\n", a[0],
  206. + readl(((__iomem u32 *)ithc->regs) + a[0] / 4));
  207. + break;
  208. + case 's': // spi command: cmd offset len data...
  209. + // read config: s 4 0 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  210. + // set touch cfg: s 6 12 4 XX
  211. + if (n < 3 || a[2] > (n - 3) * 4)
  212. + return -EINVAL;
  213. + pci_info(ithc->pci, "debug spi command %u with %u bytes of data\n", a[0], a[2]);
  214. + if (!CHECK(ithc_spi_command, ithc, a[0], a[1], a[2], a + 3))
  215. + for (u32 i = 0; i < (a[2] + 3) / 4; i++)
  216. + pci_info(ithc->pci, "resp %u = 0x%08x\n", i, a[3+i]);
  217. + break;
  218. + case 'd': // dma command: cmd len data...
  219. + // get report descriptor: d 7 8 0 0
  220. + // enable multitouch: d 3 2 0x0105
  221. + if (n < 1)
  222. + return -EINVAL;
  223. + pci_info(ithc->pci, "debug dma command with %u bytes of data\n", n * 4);
  224. + struct ithc_data data = { .type = ITHC_DATA_RAW, .size = n * 4, .data = a };
  225. + if (ithc_dma_tx(ithc, &data))
  226. + pci_err(ithc->pci, "dma tx failed\n");
  227. + break;
  228. + default:
  229. + return -EINVAL;
  230. + }
  231. + ithc_log_regs(ithc);
  232. + return len;
  233. +}
  234. +
  235. +static struct dentry *dbg_dir;
  236. +
  237. +void __init ithc_debug_init_module(void)
  238. +{
  239. + struct dentry *d = debugfs_create_dir(DEVNAME, NULL);
  240. + if (IS_ERR(d))
  241. + pr_warn("failed to create debugfs dir (%li)\n", PTR_ERR(d));
  242. + else
  243. + dbg_dir = d;
  244. +}
  245. +
  246. +void __exit ithc_debug_exit_module(void)
  247. +{
  248. + debugfs_remove_recursive(dbg_dir);
  249. + dbg_dir = NULL;
  250. +}
  251. +
  252. +static const struct file_operations ithc_debugfops_cmd = {
  253. + .owner = THIS_MODULE,
  254. + .write = ithc_debugfs_cmd_write,
  255. +};
  256. +
  257. +static void ithc_debugfs_devres_release(struct device *dev, void *res)
  258. +{
  259. + struct dentry **dbgm = res;
  260. + debugfs_remove_recursive(*dbgm);
  261. +}
  262. +
  263. +int ithc_debug_init_device(struct ithc *ithc)
  264. +{
  265. + if (!dbg_dir)
  266. + return -ENOENT;
  267. + struct dentry **dbgm = devres_alloc(ithc_debugfs_devres_release, sizeof(*dbgm), GFP_KERNEL);
  268. + if (!dbgm)
  269. + return -ENOMEM;
  270. + devres_add(&ithc->pci->dev, dbgm);
  271. + struct dentry *dbg = debugfs_create_dir(pci_name(ithc->pci), dbg_dir);
  272. + if (IS_ERR(dbg))
  273. + return PTR_ERR(dbg);
  274. + *dbgm = dbg;
  275. +
  276. + struct dentry *cmd = debugfs_create_file("cmd", 0220, dbg, ithc, &ithc_debugfops_cmd);
  277. + if (IS_ERR(cmd))
  278. + return PTR_ERR(cmd);
  279. +
  280. + return 0;
  281. +}
  282. +
  283. diff --git a/drivers/hid/ithc/ithc-debug.h b/drivers/hid/ithc/ithc-debug.h
  284. new file mode 100644
  285. index 000000000000..38c53d916bdb
  286. --- /dev/null
  287. +++ b/drivers/hid/ithc/ithc-debug.h
  288. @@ -0,0 +1,7 @@
  289. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  290. +
  291. +void ithc_debug_init_module(void);
  292. +void ithc_debug_exit_module(void);
  293. +int ithc_debug_init_device(struct ithc *ithc);
  294. +void ithc_log_regs(struct ithc *ithc);
  295. +
  296. diff --git a/drivers/hid/ithc/ithc-dma.c b/drivers/hid/ithc/ithc-dma.c
  297. new file mode 100644
  298. index 000000000000..bf4eab33062b
  299. --- /dev/null
  300. +++ b/drivers/hid/ithc/ithc-dma.c
  301. @@ -0,0 +1,312 @@
  302. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  303. +
  304. +#include "ithc.h"
  305. +
  306. +// The THC uses tables of PRDs (physical region descriptors) to describe the TX and RX data buffers.
  307. +// Each PRD contains the DMA address and size of a block of DMA memory, and some status flags.
  308. +// This allows each data buffer to consist of multiple non-contiguous blocks of memory.
  309. +
  310. +static int ithc_dma_prd_alloc(struct ithc *ithc, struct ithc_dma_prd_buffer *p,
  311. + unsigned int num_buffers, unsigned int num_pages, enum dma_data_direction dir)
  312. +{
  313. + p->num_pages = num_pages;
  314. + p->dir = dir;
  315. + // We allocate enough space to have one PRD per data buffer page, however if the data
  316. + // buffer pages happen to be contiguous, we can describe the buffer using fewer PRDs, so
  317. + // some will remain unused (which is fine).
  318. + p->size = round_up(num_buffers * num_pages * sizeof(struct ithc_phys_region_desc), PAGE_SIZE);
  319. + p->addr = dmam_alloc_coherent(&ithc->pci->dev, p->size, &p->dma_addr, GFP_KERNEL);
  320. + if (!p->addr)
  321. + return -ENOMEM;
  322. + if (p->dma_addr & (PAGE_SIZE - 1))
  323. + return -EFAULT;
  324. + return 0;
  325. +}
  326. +
  327. +// Devres managed sg_table wrapper.
  328. +struct ithc_sg_table {
  329. + void *addr;
  330. + struct sg_table sgt;
  331. + enum dma_data_direction dir;
  332. +};
  333. +static void ithc_dma_sgtable_free(struct sg_table *sgt)
  334. +{
  335. + struct scatterlist *sg;
  336. + int i;
  337. + for_each_sgtable_sg(sgt, sg, i) {
  338. + struct page *p = sg_page(sg);
  339. + if (p)
  340. + __free_page(p);
  341. + }
  342. + sg_free_table(sgt);
  343. +}
  344. +static void ithc_dma_data_devres_release(struct device *dev, void *res)
  345. +{
  346. + struct ithc_sg_table *sgt = res;
  347. + if (sgt->addr)
  348. + vunmap(sgt->addr);
  349. + dma_unmap_sgtable(dev, &sgt->sgt, sgt->dir, 0);
  350. + ithc_dma_sgtable_free(&sgt->sgt);
  351. +}
  352. +
  353. +static int ithc_dma_data_alloc(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  354. + struct ithc_dma_data_buffer *b)
  355. +{
  356. + // We don't use dma_alloc_coherent() for data buffers, because they don't have to be
  357. + // coherent (they are unidirectional) or contiguous (we can use one PRD per page).
  358. + // We could use dma_alloc_noncontiguous(), however this still always allocates a single
  359. + // DMA mapped segment, which is more restrictive than what we need.
  360. + // Instead we use an sg_table of individually allocated pages.
  361. + struct page *pages[16];
  362. + if (prds->num_pages == 0 || prds->num_pages > ARRAY_SIZE(pages))
  363. + return -EINVAL;
  364. + b->active_idx = -1;
  365. + struct ithc_sg_table *sgt = devres_alloc(
  366. + ithc_dma_data_devres_release, sizeof(*sgt), GFP_KERNEL);
  367. + if (!sgt)
  368. + return -ENOMEM;
  369. + sgt->dir = prds->dir;
  370. +
  371. + if (!sg_alloc_table(&sgt->sgt, prds->num_pages, GFP_KERNEL)) {
  372. + struct scatterlist *sg;
  373. + int i;
  374. + bool ok = true;
  375. + for_each_sgtable_sg(&sgt->sgt, sg, i) {
  376. + // NOTE: don't need __GFP_DMA for PCI DMA
  377. + struct page *p = pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  378. + if (!p) {
  379. + ok = false;
  380. + break;
  381. + }
  382. + sg_set_page(sg, p, PAGE_SIZE, 0);
  383. + }
  384. + if (ok && !dma_map_sgtable(&ithc->pci->dev, &sgt->sgt, prds->dir, 0)) {
  385. + devres_add(&ithc->pci->dev, sgt);
  386. + b->sgt = &sgt->sgt;
  387. + b->addr = sgt->addr = vmap(pages, prds->num_pages, 0, PAGE_KERNEL);
  388. + if (!b->addr)
  389. + return -ENOMEM;
  390. + return 0;
  391. + }
  392. + ithc_dma_sgtable_free(&sgt->sgt);
  393. + }
  394. + devres_free(sgt);
  395. + return -ENOMEM;
  396. +}
  397. +
  398. +static int ithc_dma_data_buffer_put(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  399. + struct ithc_dma_data_buffer *b, unsigned int idx)
  400. +{
  401. + // Give a buffer to the THC.
  402. + struct ithc_phys_region_desc *prd = prds->addr;
  403. + prd += idx * prds->num_pages;
  404. + if (b->active_idx >= 0) {
  405. + pci_err(ithc->pci, "buffer already active\n");
  406. + return -EINVAL;
  407. + }
  408. + b->active_idx = idx;
  409. + if (prds->dir == DMA_TO_DEVICE) {
  410. + // TX buffer: Caller should have already filled the data buffer, so just fill
  411. + // the PRD and flush.
  412. + // (TODO: Support multi-page TX buffers. So far no device seems to use or need
  413. + // these though.)
  414. + if (b->data_size > PAGE_SIZE)
  415. + return -EINVAL;
  416. + prd->addr = sg_dma_address(b->sgt->sgl) >> 10;
  417. + prd->size = b->data_size | PRD_FLAG_END;
  418. + flush_kernel_vmap_range(b->addr, b->data_size);
  419. + } else if (prds->dir == DMA_FROM_DEVICE) {
  420. + // RX buffer: Reset PRDs.
  421. + struct scatterlist *sg;
  422. + int i;
  423. + for_each_sgtable_dma_sg(b->sgt, sg, i) {
  424. + prd->addr = sg_dma_address(sg) >> 10;
  425. + prd->size = sg_dma_len(sg);
  426. + prd++;
  427. + }
  428. + prd[-1].size |= PRD_FLAG_END;
  429. + }
  430. + dma_wmb(); // for the prds
  431. + dma_sync_sgtable_for_device(&ithc->pci->dev, b->sgt, prds->dir);
  432. + return 0;
  433. +}
  434. +
  435. +static int ithc_dma_data_buffer_get(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  436. + struct ithc_dma_data_buffer *b, unsigned int idx)
  437. +{
  438. + // Take a buffer from the THC.
  439. + struct ithc_phys_region_desc *prd = prds->addr;
  440. + prd += idx * prds->num_pages;
  441. + // This is purely a sanity check. We don't strictly need the idx parameter for this
  442. + // function, because it should always be the same as active_idx, unless we have a bug.
  443. + if (b->active_idx != idx) {
  444. + pci_err(ithc->pci, "wrong buffer index\n");
  445. + return -EINVAL;
  446. + }
  447. + b->active_idx = -1;
  448. + if (prds->dir == DMA_FROM_DEVICE) {
  449. + // RX buffer: Calculate actual received data size from PRDs.
  450. + dma_rmb(); // for the prds
  451. + b->data_size = 0;
  452. + struct scatterlist *sg;
  453. + int i;
  454. + for_each_sgtable_dma_sg(b->sgt, sg, i) {
  455. + unsigned int size = prd->size;
  456. + b->data_size += size & PRD_SIZE_MASK;
  457. + if (size & PRD_FLAG_END)
  458. + break;
  459. + if ((size & PRD_SIZE_MASK) != sg_dma_len(sg)) {
  460. + pci_err(ithc->pci, "truncated prd\n");
  461. + break;
  462. + }
  463. + prd++;
  464. + }
  465. + invalidate_kernel_vmap_range(b->addr, b->data_size);
  466. + }
  467. + dma_sync_sgtable_for_cpu(&ithc->pci->dev, b->sgt, prds->dir);
  468. + return 0;
  469. +}
  470. +
  471. +int ithc_dma_rx_init(struct ithc *ithc, u8 channel)
  472. +{
  473. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  474. + mutex_init(&rx->mutex);
  475. +
  476. + // Allocate buffers.
  477. + unsigned int num_pages = (ithc->max_rx_size + PAGE_SIZE - 1) / PAGE_SIZE;
  478. + pci_dbg(ithc->pci, "allocating rx buffers: num = %u, size = %u, pages = %u\n",
  479. + NUM_RX_BUF, ithc->max_rx_size, num_pages);
  480. + CHECK_RET(ithc_dma_prd_alloc, ithc, &rx->prds, NUM_RX_BUF, num_pages, DMA_FROM_DEVICE);
  481. + for (unsigned int i = 0; i < NUM_RX_BUF; i++)
  482. + CHECK_RET(ithc_dma_data_alloc, ithc, &rx->prds, &rx->bufs[i]);
  483. +
  484. + // Init registers.
  485. + writeb(DMA_RX_CONTROL2_RESET, &ithc->regs->dma_rx[channel].control2);
  486. + lo_hi_writeq(rx->prds.dma_addr, &ithc->regs->dma_rx[channel].addr);
  487. + writeb(NUM_RX_BUF - 1, &ithc->regs->dma_rx[channel].num_bufs);
  488. + writeb(num_pages - 1, &ithc->regs->dma_rx[channel].num_prds);
  489. + u8 head = readb(&ithc->regs->dma_rx[channel].head);
  490. + if (head) {
  491. + pci_err(ithc->pci, "head is nonzero (%u)\n", head);
  492. + return -EIO;
  493. + }
  494. +
  495. + // Init buffers.
  496. + for (unsigned int i = 0; i < NUM_RX_BUF; i++)
  497. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &rx->prds, &rx->bufs[i], i);
  498. +
  499. + writeb(head ^ DMA_RX_WRAP_FLAG, &ithc->regs->dma_rx[channel].tail);
  500. + return 0;
  501. +}
  502. +
  503. +void ithc_dma_rx_enable(struct ithc *ithc, u8 channel)
  504. +{
  505. + bitsb_set(&ithc->regs->dma_rx[channel].control,
  506. + DMA_RX_CONTROL_ENABLE | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_DATA);
  507. + CHECK(waitl, ithc, &ithc->regs->dma_rx[channel].status,
  508. + DMA_RX_STATUS_ENABLED, DMA_RX_STATUS_ENABLED);
  509. +}
  510. +
  511. +int ithc_dma_tx_init(struct ithc *ithc)
  512. +{
  513. + struct ithc_dma_tx *tx = &ithc->dma_tx;
  514. + mutex_init(&tx->mutex);
  515. +
  516. + // Allocate buffers.
  517. + unsigned int num_pages = (ithc->max_tx_size + PAGE_SIZE - 1) / PAGE_SIZE;
  518. + pci_dbg(ithc->pci, "allocating tx buffers: size = %u, pages = %u\n",
  519. + ithc->max_tx_size, num_pages);
  520. + CHECK_RET(ithc_dma_prd_alloc, ithc, &tx->prds, 1, num_pages, DMA_TO_DEVICE);
  521. + CHECK_RET(ithc_dma_data_alloc, ithc, &tx->prds, &tx->buf);
  522. +
  523. + // Init registers.
  524. + lo_hi_writeq(tx->prds.dma_addr, &ithc->regs->dma_tx.addr);
  525. + writeb(num_pages - 1, &ithc->regs->dma_tx.num_prds);
  526. +
  527. + // Init buffers.
  528. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  529. + return 0;
  530. +}
  531. +
  532. +static int ithc_dma_rx_unlocked(struct ithc *ithc, u8 channel)
  533. +{
  534. + // Process all filled RX buffers from the ringbuffer.
  535. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  536. + unsigned int n = rx->num_received;
  537. + u8 head_wrap = readb(&ithc->regs->dma_rx[channel].head);
  538. + while (1) {
  539. + u8 tail = n % NUM_RX_BUF;
  540. + u8 tail_wrap = tail | ((n / NUM_RX_BUF) & 1 ? 0 : DMA_RX_WRAP_FLAG);
  541. + writeb(tail_wrap, &ithc->regs->dma_rx[channel].tail);
  542. + // ringbuffer is full if tail_wrap == head_wrap
  543. + // ringbuffer is empty if tail_wrap == head_wrap ^ WRAP_FLAG
  544. + if (tail_wrap == (head_wrap ^ DMA_RX_WRAP_FLAG))
  545. + return 0;
  546. +
  547. + // take the buffer that the device just filled
  548. + struct ithc_dma_data_buffer *b = &rx->bufs[n % NUM_RX_BUF];
  549. + CHECK_RET(ithc_dma_data_buffer_get, ithc, &rx->prds, b, tail);
  550. + rx->num_received = ++n;
  551. +
  552. + // process data
  553. + struct ithc_data d;
  554. + if ((ithc->use_quickspi ? ithc_quickspi_decode_rx : ithc_legacy_decode_rx)
  555. + (ithc, b->addr, b->data_size, &d) < 0) {
  556. + pci_err(ithc->pci, "invalid dma rx data! channel %u, buffer %u, size %u: %*ph\n",
  557. + channel, tail, b->data_size, min((int)b->data_size, 64), b->addr);
  558. + print_hex_dump_debug(DEVNAME " data: ", DUMP_PREFIX_OFFSET, 32, 1,
  559. + b->addr, min(b->data_size, 0x400u), 0);
  560. + } else {
  561. + ithc_hid_process_data(ithc, &d);
  562. + }
  563. +
  564. + // give the buffer back to the device
  565. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &rx->prds, b, tail);
  566. + }
  567. +}
  568. +int ithc_dma_rx(struct ithc *ithc, u8 channel)
  569. +{
  570. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  571. + mutex_lock(&rx->mutex);
  572. + int ret = ithc_dma_rx_unlocked(ithc, channel);
  573. + mutex_unlock(&rx->mutex);
  574. + return ret;
  575. +}
  576. +
  577. +static int ithc_dma_tx_unlocked(struct ithc *ithc, const struct ithc_data *data)
  578. +{
  579. + // Send a single TX buffer to the THC.
  580. + pci_dbg(ithc->pci, "dma tx data type %u, size %u\n", data->type, data->size);
  581. + CHECK_RET(ithc_dma_data_buffer_get, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  582. +
  583. + // Fill the TX buffer with header and data.
  584. + ssize_t sz;
  585. + if (data->type == ITHC_DATA_RAW) {
  586. + sz = min(data->size, ithc->max_tx_size);
  587. + memcpy(ithc->dma_tx.buf.addr, data->data, sz);
  588. + } else {
  589. + sz = (ithc->use_quickspi ? ithc_quickspi_encode_tx : ithc_legacy_encode_tx)
  590. + (ithc, data, ithc->dma_tx.buf.addr, ithc->max_tx_size);
  591. + }
  592. + ithc->dma_tx.buf.data_size = sz < 0 ? 0 : sz;
  593. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  594. + if (sz < 0) {
  595. + pci_err(ithc->pci, "failed to encode tx data type %i, size %u, error %i\n",
  596. + data->type, data->size, (int)sz);
  597. + return -EINVAL;
  598. + }
  599. +
  600. + // Let the THC process the buffer.
  601. + bitsb_set(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND);
  602. + CHECK_RET(waitb, ithc, &ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND, 0);
  603. + writel(DMA_TX_STATUS_DONE, &ithc->regs->dma_tx.status);
  604. + return 0;
  605. +}
  606. +int ithc_dma_tx(struct ithc *ithc, const struct ithc_data *data)
  607. +{
  608. + mutex_lock(&ithc->dma_tx.mutex);
  609. + int ret = ithc_dma_tx_unlocked(ithc, data);
  610. + mutex_unlock(&ithc->dma_tx.mutex);
  611. + return ret;
  612. +}
  613. +
  614. diff --git a/drivers/hid/ithc/ithc-dma.h b/drivers/hid/ithc/ithc-dma.h
  615. new file mode 100644
  616. index 000000000000..1749a5819b3e
  617. --- /dev/null
  618. +++ b/drivers/hid/ithc/ithc-dma.h
  619. @@ -0,0 +1,47 @@
  620. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  621. +
  622. +#define PRD_SIZE_MASK 0xffffff
  623. +#define PRD_FLAG_END 0x1000000
  624. +#define PRD_FLAG_SUCCESS 0x2000000
  625. +#define PRD_FLAG_ERROR 0x4000000
  626. +
  627. +struct ithc_phys_region_desc {
  628. + u64 addr; // physical addr/1024
  629. + u32 size; // num bytes, PRD_FLAG_END marks last prd for data split over multiple prds
  630. + u32 unused;
  631. +};
  632. +
  633. +struct ithc_dma_prd_buffer {
  634. + void *addr;
  635. + dma_addr_t dma_addr;
  636. + u32 size;
  637. + u32 num_pages; // per data buffer
  638. + enum dma_data_direction dir;
  639. +};
  640. +
  641. +struct ithc_dma_data_buffer {
  642. + void *addr;
  643. + struct sg_table *sgt;
  644. + int active_idx;
  645. + u32 data_size;
  646. +};
  647. +
  648. +struct ithc_dma_tx {
  649. + struct mutex mutex;
  650. + struct ithc_dma_prd_buffer prds;
  651. + struct ithc_dma_data_buffer buf;
  652. +};
  653. +
  654. +struct ithc_dma_rx {
  655. + struct mutex mutex;
  656. + u32 num_received;
  657. + struct ithc_dma_prd_buffer prds;
  658. + struct ithc_dma_data_buffer bufs[NUM_RX_BUF];
  659. +};
  660. +
  661. +int ithc_dma_rx_init(struct ithc *ithc, u8 channel);
  662. +void ithc_dma_rx_enable(struct ithc *ithc, u8 channel);
  663. +int ithc_dma_tx_init(struct ithc *ithc);
  664. +int ithc_dma_rx(struct ithc *ithc, u8 channel);
  665. +int ithc_dma_tx(struct ithc *ithc, const struct ithc_data *data);
  666. +
  667. diff --git a/drivers/hid/ithc/ithc-hid.c b/drivers/hid/ithc/ithc-hid.c
  668. new file mode 100644
  669. index 000000000000..065646ab499e
  670. --- /dev/null
  671. +++ b/drivers/hid/ithc/ithc-hid.c
  672. @@ -0,0 +1,207 @@
  673. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  674. +
  675. +#include "ithc.h"
  676. +
  677. +static int ithc_hid_start(struct hid_device *hdev) { return 0; }
  678. +static void ithc_hid_stop(struct hid_device *hdev) { }
  679. +static int ithc_hid_open(struct hid_device *hdev) { return 0; }
  680. +static void ithc_hid_close(struct hid_device *hdev) { }
  681. +
  682. +static int ithc_hid_parse(struct hid_device *hdev)
  683. +{
  684. + struct ithc *ithc = hdev->driver_data;
  685. + const struct ithc_data get_report_desc = { .type = ITHC_DATA_REPORT_DESCRIPTOR };
  686. + WRITE_ONCE(ithc->hid.parse_done, false);
  687. + for (int retries = 0; ; retries++) {
  688. + ithc_log_regs(ithc);
  689. + CHECK_RET(ithc_dma_tx, ithc, &get_report_desc);
  690. + if (wait_event_timeout(ithc->hid.wait_parse, READ_ONCE(ithc->hid.parse_done),
  691. + msecs_to_jiffies(200))) {
  692. + ithc_log_regs(ithc);
  693. + return 0;
  694. + }
  695. + if (retries > 5) {
  696. + ithc_log_regs(ithc);
  697. + pci_err(ithc->pci, "failed to read report descriptor\n");
  698. + return -ETIMEDOUT;
  699. + }
  700. + pci_warn(ithc->pci, "failed to read report descriptor, retrying\n");
  701. + }
  702. +}
  703. +
  704. +static int ithc_hid_raw_request(struct hid_device *hdev, unsigned char reportnum, __u8 *buf,
  705. + size_t len, unsigned char rtype, int reqtype)
  706. +{
  707. + struct ithc *ithc = hdev->driver_data;
  708. + if (!buf || !len)
  709. + return -EINVAL;
  710. +
  711. + struct ithc_data d = { .size = len, .data = buf };
  712. + buf[0] = reportnum;
  713. +
  714. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT) {
  715. + d.type = ITHC_DATA_OUTPUT_REPORT;
  716. + CHECK_RET(ithc_dma_tx, ithc, &d);
  717. + return 0;
  718. + }
  719. +
  720. + if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT) {
  721. + d.type = ITHC_DATA_SET_FEATURE;
  722. + CHECK_RET(ithc_dma_tx, ithc, &d);
  723. + return 0;
  724. + }
  725. +
  726. + if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT) {
  727. + d.type = ITHC_DATA_GET_FEATURE;
  728. + d.data = &reportnum;
  729. + d.size = 1;
  730. +
  731. + // Prepare for response.
  732. + mutex_lock(&ithc->hid.get_feature_mutex);
  733. + ithc->hid.get_feature_buf = buf;
  734. + ithc->hid.get_feature_size = len;
  735. + mutex_unlock(&ithc->hid.get_feature_mutex);
  736. +
  737. + // Transmit 'get feature' request.
  738. + int r = CHECK(ithc_dma_tx, ithc, &d);
  739. + if (!r) {
  740. + r = wait_event_interruptible_timeout(ithc->hid.wait_get_feature,
  741. + !ithc->hid.get_feature_buf, msecs_to_jiffies(1000));
  742. + if (!r)
  743. + r = -ETIMEDOUT;
  744. + else if (r < 0)
  745. + r = -EINTR;
  746. + else
  747. + r = 0;
  748. + }
  749. +
  750. + // If everything went ok, the buffer has been filled with the response data.
  751. + // Return the response size.
  752. + mutex_lock(&ithc->hid.get_feature_mutex);
  753. + ithc->hid.get_feature_buf = NULL;
  754. + if (!r)
  755. + r = ithc->hid.get_feature_size;
  756. + mutex_unlock(&ithc->hid.get_feature_mutex);
  757. + return r;
  758. + }
  759. +
  760. + pci_err(ithc->pci, "unhandled hid request %i %i for report id %i\n",
  761. + rtype, reqtype, reportnum);
  762. + return -EINVAL;
  763. +}
  764. +
  765. +// FIXME hid_input_report()/hid_parse_report() currently don't take const buffers, so we have to
  766. +// cast away the const to avoid a compiler warning...
  767. +#define NOCONST(x) ((void *)x)
  768. +
  769. +void ithc_hid_process_data(struct ithc *ithc, struct ithc_data *d)
  770. +{
  771. + WARN_ON(!ithc->hid.dev);
  772. + if (!ithc->hid.dev)
  773. + return;
  774. +
  775. + switch (d->type) {
  776. +
  777. + case ITHC_DATA_IGNORE:
  778. + return;
  779. +
  780. + case ITHC_DATA_ERROR:
  781. + CHECK(ithc_reset, ithc);
  782. + return;
  783. +
  784. + case ITHC_DATA_REPORT_DESCRIPTOR:
  785. + // Response to the report descriptor request sent by ithc_hid_parse().
  786. + CHECK(hid_parse_report, ithc->hid.dev, NOCONST(d->data), d->size);
  787. + WRITE_ONCE(ithc->hid.parse_done, true);
  788. + wake_up(&ithc->hid.wait_parse);
  789. + return;
  790. +
  791. + case ITHC_DATA_INPUT_REPORT:
  792. + {
  793. + // Standard HID input report.
  794. + int r = hid_input_report(ithc->hid.dev, HID_INPUT_REPORT, NOCONST(d->data), d->size, 1);
  795. + if (r < 0) {
  796. + pci_warn(ithc->pci, "hid_input_report failed with %i (size %u, report ID 0x%02x)\n",
  797. + r, d->size, d->size ? *(u8 *)d->data : 0);
  798. + print_hex_dump_debug(DEVNAME " report: ", DUMP_PREFIX_OFFSET, 32, 1,
  799. + d->data, min(d->size, 0x400u), 0);
  800. + }
  801. + return;
  802. + }
  803. +
  804. + case ITHC_DATA_GET_FEATURE:
  805. + {
  806. + // Response to a 'get feature' request sent by ithc_hid_raw_request().
  807. + bool done = false;
  808. + mutex_lock(&ithc->hid.get_feature_mutex);
  809. + if (ithc->hid.get_feature_buf) {
  810. + if (d->size < ithc->hid.get_feature_size)
  811. + ithc->hid.get_feature_size = d->size;
  812. + memcpy(ithc->hid.get_feature_buf, d->data, ithc->hid.get_feature_size);
  813. + ithc->hid.get_feature_buf = NULL;
  814. + done = true;
  815. + }
  816. + mutex_unlock(&ithc->hid.get_feature_mutex);
  817. + if (done) {
  818. + wake_up(&ithc->hid.wait_get_feature);
  819. + } else {
  820. + // Received data without a matching request, or the request already
  821. + // timed out. (XXX What's the correct thing to do here?)
  822. + CHECK(hid_input_report, ithc->hid.dev, HID_FEATURE_REPORT,
  823. + NOCONST(d->data), d->size, 1);
  824. + }
  825. + return;
  826. + }
  827. +
  828. + default:
  829. + pci_err(ithc->pci, "unhandled data type %i\n", d->type);
  830. + return;
  831. + }
  832. +}
  833. +
  834. +static struct hid_ll_driver ithc_ll_driver = {
  835. + .start = ithc_hid_start,
  836. + .stop = ithc_hid_stop,
  837. + .open = ithc_hid_open,
  838. + .close = ithc_hid_close,
  839. + .parse = ithc_hid_parse,
  840. + .raw_request = ithc_hid_raw_request,
  841. +};
  842. +
  843. +static void ithc_hid_devres_release(struct device *dev, void *res)
  844. +{
  845. + struct hid_device **hidm = res;
  846. + if (*hidm)
  847. + hid_destroy_device(*hidm);
  848. +}
  849. +
  850. +int ithc_hid_init(struct ithc *ithc)
  851. +{
  852. + struct hid_device **hidm = devres_alloc(ithc_hid_devres_release, sizeof(*hidm), GFP_KERNEL);
  853. + if (!hidm)
  854. + return -ENOMEM;
  855. + devres_add(&ithc->pci->dev, hidm);
  856. + struct hid_device *hid = hid_allocate_device();
  857. + if (IS_ERR(hid))
  858. + return PTR_ERR(hid);
  859. + *hidm = hid;
  860. +
  861. + strscpy(hid->name, DEVFULLNAME, sizeof(hid->name));
  862. + strscpy(hid->phys, ithc->phys, sizeof(hid->phys));
  863. + hid->ll_driver = &ithc_ll_driver;
  864. + hid->bus = BUS_PCI;
  865. + hid->vendor = ithc->vendor_id;
  866. + hid->product = ithc->product_id;
  867. + hid->version = 0x100;
  868. + hid->dev.parent = &ithc->pci->dev;
  869. + hid->driver_data = ithc;
  870. +
  871. + ithc->hid.dev = hid;
  872. +
  873. + init_waitqueue_head(&ithc->hid.wait_parse);
  874. + init_waitqueue_head(&ithc->hid.wait_get_feature);
  875. + mutex_init(&ithc->hid.get_feature_mutex);
  876. +
  877. + return 0;
  878. +}
  879. +
  880. diff --git a/drivers/hid/ithc/ithc-hid.h b/drivers/hid/ithc/ithc-hid.h
  881. new file mode 100644
  882. index 000000000000..599eb912c8c8
  883. --- /dev/null
  884. +++ b/drivers/hid/ithc/ithc-hid.h
  885. @@ -0,0 +1,32 @@
  886. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  887. +
  888. +enum ithc_data_type {
  889. + ITHC_DATA_IGNORE,
  890. + ITHC_DATA_RAW,
  891. + ITHC_DATA_ERROR,
  892. + ITHC_DATA_REPORT_DESCRIPTOR,
  893. + ITHC_DATA_INPUT_REPORT,
  894. + ITHC_DATA_OUTPUT_REPORT,
  895. + ITHC_DATA_GET_FEATURE,
  896. + ITHC_DATA_SET_FEATURE,
  897. +};
  898. +
  899. +struct ithc_data {
  900. + enum ithc_data_type type;
  901. + u32 size;
  902. + const void *data;
  903. +};
  904. +
  905. +struct ithc_hid {
  906. + struct hid_device *dev;
  907. + bool parse_done;
  908. + wait_queue_head_t wait_parse;
  909. + wait_queue_head_t wait_get_feature;
  910. + struct mutex get_feature_mutex;
  911. + void *get_feature_buf;
  912. + size_t get_feature_size;
  913. +};
  914. +
  915. +int ithc_hid_init(struct ithc *ithc);
  916. +void ithc_hid_process_data(struct ithc *ithc, struct ithc_data *d);
  917. +
  918. diff --git a/drivers/hid/ithc/ithc-legacy.c b/drivers/hid/ithc/ithc-legacy.c
  919. new file mode 100644
  920. index 000000000000..8883987fb352
  921. --- /dev/null
  922. +++ b/drivers/hid/ithc/ithc-legacy.c
  923. @@ -0,0 +1,254 @@
  924. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  925. +
  926. +#include "ithc.h"
  927. +
  928. +#define DEVCFG_DMA_RX_SIZE(x) ((((x) & 0x3fff) + 1) << 6)
  929. +#define DEVCFG_DMA_TX_SIZE(x) (((((x) >> 14) & 0x3ff) + 1) << 6)
  930. +
  931. +#define DEVCFG_TOUCH_MASK 0x3f
  932. +#define DEVCFG_TOUCH_ENABLE BIT(0)
  933. +#define DEVCFG_TOUCH_PROP_DATA_ENABLE BIT(1)
  934. +#define DEVCFG_TOUCH_HID_REPORT_ENABLE BIT(2)
  935. +#define DEVCFG_TOUCH_POWER_STATE(x) (((x) & 7) << 3)
  936. +#define DEVCFG_TOUCH_UNKNOWN_6 BIT(6)
  937. +
  938. +#define DEVCFG_DEVICE_ID_TIC 0x43495424 // "$TIC"
  939. +
  940. +#define DEVCFG_SPI_CLKDIV(x) (((x) >> 1) & 7)
  941. +#define DEVCFG_SPI_CLKDIV_8 BIT(4)
  942. +#define DEVCFG_SPI_SUPPORTS_SINGLE BIT(5)
  943. +#define DEVCFG_SPI_SUPPORTS_DUAL BIT(6)
  944. +#define DEVCFG_SPI_SUPPORTS_QUAD BIT(7)
  945. +#define DEVCFG_SPI_MAX_TOUCH_POINTS(x) (((x) >> 8) & 0x3f)
  946. +#define DEVCFG_SPI_MIN_RESET_TIME(x) (((x) >> 16) & 0xf)
  947. +#define DEVCFG_SPI_NEEDS_HEARTBEAT BIT(20) // TODO implement heartbeat
  948. +#define DEVCFG_SPI_HEARTBEAT_INTERVAL(x) (((x) >> 21) & 7)
  949. +#define DEVCFG_SPI_UNKNOWN_25 BIT(25)
  950. +#define DEVCFG_SPI_UNKNOWN_26 BIT(26)
  951. +#define DEVCFG_SPI_UNKNOWN_27 BIT(27)
  952. +#define DEVCFG_SPI_DELAY(x) (((x) >> 28) & 7) // TODO use this
  953. +#define DEVCFG_SPI_USE_EXT_READ_CFG BIT(31) // TODO use this?
  954. +
  955. +struct ithc_device_config { // (Example values are from an SP7+.)
  956. + u32 irq_cause; // 00 = 0xe0000402 (0xe0000401 after DMA_RX_CODE_RESET)
  957. + u32 error; // 04 = 0x00000000
  958. + u32 dma_buf_sizes; // 08 = 0x000a00ff
  959. + u32 touch_cfg; // 0c = 0x0000001c
  960. + u32 touch_state; // 10 = 0x0000001c
  961. + u32 device_id; // 14 = 0x43495424 = "$TIC"
  962. + u32 spi_config; // 18 = 0xfda00a2e
  963. + u16 vendor_id; // 1c = 0x045e = Microsoft Corp.
  964. + u16 product_id; // 1e = 0x0c1a
  965. + u32 revision; // 20 = 0x00000001
  966. + u32 fw_version; // 24 = 0x05008a8b = 5.0.138.139 (this value looks more random on newer devices)
  967. + u32 command; // 28 = 0x00000000
  968. + u32 fw_mode; // 2c = 0x00000000 (for fw update?)
  969. + u32 _unknown_30; // 30 = 0x00000000
  970. + u8 eds_minor_ver; // 34 = 0x5e
  971. + u8 eds_major_ver; // 35 = 0x03
  972. + u8 interface_rev; // 36 = 0x04
  973. + u8 eu_kernel_ver; // 37 = 0x04
  974. + u32 _unknown_38; // 38 = 0x000001c0 (0x000001c1 after DMA_RX_CODE_RESET)
  975. + u32 _unknown_3c; // 3c = 0x00000002
  976. +};
  977. +static_assert(sizeof(struct ithc_device_config) == 64);
  978. +
  979. +#define RX_CODE_INPUT_REPORT 3
  980. +#define RX_CODE_FEATURE_REPORT 4
  981. +#define RX_CODE_REPORT_DESCRIPTOR 5
  982. +#define RX_CODE_RESET 7
  983. +
  984. +#define TX_CODE_SET_FEATURE 3
  985. +#define TX_CODE_GET_FEATURE 4
  986. +#define TX_CODE_OUTPUT_REPORT 5
  987. +#define TX_CODE_GET_REPORT_DESCRIPTOR 7
  988. +
  989. +static int ithc_set_device_enabled(struct ithc *ithc, bool enable)
  990. +{
  991. + u32 x = ithc->legacy_touch_cfg =
  992. + (ithc->legacy_touch_cfg & ~(u32)DEVCFG_TOUCH_MASK) |
  993. + DEVCFG_TOUCH_HID_REPORT_ENABLE |
  994. + (enable ? DEVCFG_TOUCH_ENABLE | DEVCFG_TOUCH_POWER_STATE(3) : 0);
  995. + return ithc_spi_command(ithc, SPI_CMD_CODE_WRITE,
  996. + offsetof(struct ithc_device_config, touch_cfg), sizeof(x), &x);
  997. +}
  998. +
  999. +int ithc_legacy_init(struct ithc *ithc)
  1000. +{
  1001. + // Since we don't yet know which SPI config the device wants, use default speed and mode
  1002. + // initially for reading config data.
  1003. + CHECK(ithc_set_spi_config, ithc, 2, true, SPI_MODE_SINGLE, SPI_MODE_SINGLE);
  1004. +
  1005. + // Setting the following bit seems to make reading the config more reliable.
  1006. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  1007. +
  1008. + // Setting this bit may be necessary on ADL devices.
  1009. + switch (ithc->pci->device) {
  1010. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1:
  1011. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2:
  1012. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1:
  1013. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2:
  1014. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1:
  1015. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2:
  1016. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_5);
  1017. + break;
  1018. + }
  1019. +
  1020. + // Take the touch device out of reset.
  1021. + bitsl(&ithc->regs->control_bits, CONTROL_QUIESCE, 0);
  1022. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, 0);
  1023. + for (int retries = 0; ; retries++) {
  1024. + ithc_log_regs(ithc);
  1025. + bitsl_set(&ithc->regs->control_bits, CONTROL_NRESET);
  1026. + if (!waitl(ithc, &ithc->regs->irq_cause, 0xf, 2))
  1027. + break;
  1028. + if (retries > 5) {
  1029. + pci_err(ithc->pci, "failed to reset device, irq_cause = 0x%08x\n",
  1030. + readl(&ithc->regs->irq_cause));
  1031. + return -ETIMEDOUT;
  1032. + }
  1033. + pci_warn(ithc->pci, "invalid irq_cause, retrying reset\n");
  1034. + bitsl(&ithc->regs->control_bits, CONTROL_NRESET, 0);
  1035. + if (msleep_interruptible(1000))
  1036. + return -EINTR;
  1037. + }
  1038. + ithc_log_regs(ithc);
  1039. +
  1040. + CHECK(waitl, ithc, &ithc->regs->dma_rx[0].status, DMA_RX_STATUS_READY, DMA_RX_STATUS_READY);
  1041. +
  1042. + // Read configuration data.
  1043. + u32 spi_cfg;
  1044. + for (int retries = 0; ; retries++) {
  1045. + ithc_log_regs(ithc);
  1046. + struct ithc_device_config config = { 0 };
  1047. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, 0, sizeof(config), &config);
  1048. + u32 *p = (void *)&config;
  1049. + pci_info(ithc->pci, "config: %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1050. + p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
  1051. + if (config.device_id == DEVCFG_DEVICE_ID_TIC) {
  1052. + spi_cfg = config.spi_config;
  1053. + ithc->vendor_id = config.vendor_id;
  1054. + ithc->product_id = config.product_id;
  1055. + ithc->product_rev = config.revision;
  1056. + ithc->max_rx_size = DEVCFG_DMA_RX_SIZE(config.dma_buf_sizes);
  1057. + ithc->max_tx_size = DEVCFG_DMA_TX_SIZE(config.dma_buf_sizes);
  1058. + ithc->legacy_touch_cfg = config.touch_cfg;
  1059. + ithc->have_config = true;
  1060. + break;
  1061. + }
  1062. + if (retries > 10) {
  1063. + pci_err(ithc->pci, "failed to read config, unknown device ID 0x%08x\n",
  1064. + config.device_id);
  1065. + return -EIO;
  1066. + }
  1067. + pci_warn(ithc->pci, "failed to read config, retrying\n");
  1068. + if (msleep_interruptible(100))
  1069. + return -EINTR;
  1070. + }
  1071. + ithc_log_regs(ithc);
  1072. +
  1073. + // Apply SPI config and enable touch device.
  1074. + CHECK_RET(ithc_set_spi_config, ithc,
  1075. + DEVCFG_SPI_CLKDIV(spi_cfg), (spi_cfg & DEVCFG_SPI_CLKDIV_8) != 0,
  1076. + spi_cfg & DEVCFG_SPI_SUPPORTS_QUAD ? SPI_MODE_QUAD :
  1077. + spi_cfg & DEVCFG_SPI_SUPPORTS_DUAL ? SPI_MODE_DUAL :
  1078. + SPI_MODE_SINGLE,
  1079. + SPI_MODE_SINGLE);
  1080. + CHECK_RET(ithc_set_device_enabled, ithc, true);
  1081. + ithc_log_regs(ithc);
  1082. + return 0;
  1083. +}
  1084. +
  1085. +void ithc_legacy_exit(struct ithc *ithc)
  1086. +{
  1087. + CHECK(ithc_set_device_enabled, ithc, false);
  1088. +}
  1089. +
  1090. +int ithc_legacy_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest)
  1091. +{
  1092. + const struct {
  1093. + u32 code;
  1094. + u32 data_size;
  1095. + u32 _unknown[14];
  1096. + } *hdr = src;
  1097. +
  1098. + if (len < sizeof(*hdr))
  1099. + return -ENODATA;
  1100. + // Note: RX data is not padded, even though TX data must be padded.
  1101. + if (len != sizeof(*hdr) + hdr->data_size)
  1102. + return -EMSGSIZE;
  1103. +
  1104. + dest->data = hdr + 1;
  1105. + dest->size = hdr->data_size;
  1106. +
  1107. + switch (hdr->code) {
  1108. + case RX_CODE_RESET:
  1109. + // The THC sends a reset request when we need to reinitialize the device.
  1110. + // This usually only happens if we send an invalid command or put the device
  1111. + // in a bad state.
  1112. + dest->type = ITHC_DATA_ERROR;
  1113. + return 0;
  1114. + case RX_CODE_REPORT_DESCRIPTOR:
  1115. + // The descriptor is preceded by 8 nul bytes.
  1116. + if (hdr->data_size < 8)
  1117. + return -ENODATA;
  1118. + dest->type = ITHC_DATA_REPORT_DESCRIPTOR;
  1119. + dest->data = (char *)(hdr + 1) + 8;
  1120. + dest->size = hdr->data_size - 8;
  1121. + return 0;
  1122. + case RX_CODE_INPUT_REPORT:
  1123. + dest->type = ITHC_DATA_INPUT_REPORT;
  1124. + return 0;
  1125. + case RX_CODE_FEATURE_REPORT:
  1126. + dest->type = ITHC_DATA_GET_FEATURE;
  1127. + return 0;
  1128. + default:
  1129. + return -EINVAL;
  1130. + }
  1131. +}
  1132. +
  1133. +ssize_t ithc_legacy_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  1134. + size_t maxlen)
  1135. +{
  1136. + struct {
  1137. + u32 code;
  1138. + u32 data_size;
  1139. + } *hdr = dest;
  1140. +
  1141. + size_t src_size = src->size;
  1142. + const void *src_data = src->data;
  1143. + const u64 get_report_desc_data = 0;
  1144. + u32 code;
  1145. +
  1146. + switch (src->type) {
  1147. + case ITHC_DATA_SET_FEATURE:
  1148. + code = TX_CODE_SET_FEATURE;
  1149. + break;
  1150. + case ITHC_DATA_GET_FEATURE:
  1151. + code = TX_CODE_GET_FEATURE;
  1152. + break;
  1153. + case ITHC_DATA_OUTPUT_REPORT:
  1154. + code = TX_CODE_OUTPUT_REPORT;
  1155. + break;
  1156. + case ITHC_DATA_REPORT_DESCRIPTOR:
  1157. + code = TX_CODE_GET_REPORT_DESCRIPTOR;
  1158. + src_size = sizeof(get_report_desc_data);
  1159. + src_data = &get_report_desc_data;
  1160. + break;
  1161. + default:
  1162. + return -EINVAL;
  1163. + }
  1164. +
  1165. + // Data must be padded to next 4-byte boundary.
  1166. + size_t padded = round_up(src_size, 4);
  1167. + if (sizeof(*hdr) + padded > maxlen)
  1168. + return -EOVERFLOW;
  1169. +
  1170. + // Fill the TX buffer with header and data.
  1171. + hdr->code = code;
  1172. + hdr->data_size = src_size;
  1173. + memcpy_and_pad(hdr + 1, padded, src_data, src_size, 0);
  1174. +
  1175. + return sizeof(*hdr) + padded;
  1176. +}
  1177. +
  1178. diff --git a/drivers/hid/ithc/ithc-legacy.h b/drivers/hid/ithc/ithc-legacy.h
  1179. new file mode 100644
  1180. index 000000000000..28d692462072
  1181. --- /dev/null
  1182. +++ b/drivers/hid/ithc/ithc-legacy.h
  1183. @@ -0,0 +1,8 @@
  1184. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  1185. +
  1186. +int ithc_legacy_init(struct ithc *ithc);
  1187. +void ithc_legacy_exit(struct ithc *ithc);
  1188. +int ithc_legacy_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest);
  1189. +ssize_t ithc_legacy_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  1190. + size_t maxlen);
  1191. +
  1192. diff --git a/drivers/hid/ithc/ithc-main.c b/drivers/hid/ithc/ithc-main.c
  1193. new file mode 100644
  1194. index 000000000000..ac56c253674b
  1195. --- /dev/null
  1196. +++ b/drivers/hid/ithc/ithc-main.c
  1197. @@ -0,0 +1,431 @@
  1198. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  1199. +
  1200. +#include "ithc.h"
  1201. +
  1202. +MODULE_DESCRIPTION("Intel Touch Host Controller driver");
  1203. +MODULE_LICENSE("Dual BSD/GPL");
  1204. +
  1205. +static const struct pci_device_id ithc_pci_tbl[] = {
  1206. + {
  1207. + .vendor = PCI_VENDOR_ID_INTEL,
  1208. + .device = PCI_ANY_ID,
  1209. + .subvendor = PCI_ANY_ID,
  1210. + .subdevice = PCI_ANY_ID,
  1211. + .class = PCI_CLASS_INPUT_PEN << 8,
  1212. + .class_mask = ~0,
  1213. + },
  1214. + {}
  1215. +};
  1216. +MODULE_DEVICE_TABLE(pci, ithc_pci_tbl);
  1217. +
  1218. +// Module parameters
  1219. +
  1220. +static bool ithc_use_polling = false;
  1221. +module_param_named(poll, ithc_use_polling, bool, 0);
  1222. +MODULE_PARM_DESC(poll, "Use polling instead of interrupts");
  1223. +
  1224. +// Since all known devices seem to use only channel 1, by default we disable channel 0.
  1225. +static bool ithc_use_rx0 = false;
  1226. +module_param_named(rx0, ithc_use_rx0, bool, 0);
  1227. +MODULE_PARM_DESC(rx0, "Use DMA RX channel 0");
  1228. +
  1229. +static bool ithc_use_rx1 = true;
  1230. +module_param_named(rx1, ithc_use_rx1, bool, 0);
  1231. +MODULE_PARM_DESC(rx1, "Use DMA RX channel 1");
  1232. +
  1233. +static int ithc_active_ltr_us = -1;
  1234. +module_param_named(activeltr, ithc_active_ltr_us, int, 0);
  1235. +MODULE_PARM_DESC(activeltr, "Active LTR value override (in microseconds)");
  1236. +
  1237. +static int ithc_idle_ltr_us = -1;
  1238. +module_param_named(idleltr, ithc_idle_ltr_us, int, 0);
  1239. +MODULE_PARM_DESC(idleltr, "Idle LTR value override (in microseconds)");
  1240. +
  1241. +static unsigned int ithc_idle_delay_ms = 1000;
  1242. +module_param_named(idledelay, ithc_idle_delay_ms, uint, 0);
  1243. +MODULE_PARM_DESC(idleltr, "Minimum idle time before applying idle LTR value (in milliseconds)");
  1244. +
  1245. +static bool ithc_log_regs_enabled = false;
  1246. +module_param_named(logregs, ithc_log_regs_enabled, bool, 0);
  1247. +MODULE_PARM_DESC(logregs, "Log changes in register values (for debugging)");
  1248. +
  1249. +// Interrupts/polling
  1250. +
  1251. +static void ithc_disable_interrupts(struct ithc *ithc)
  1252. +{
  1253. + writel(0, &ithc->regs->error_control);
  1254. + bitsb(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_IRQ, 0);
  1255. + bitsb(&ithc->regs->dma_rx[0].control, DMA_RX_CONTROL_IRQ_UNKNOWN_1 | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_READY | DMA_RX_CONTROL_IRQ_DATA, 0);
  1256. + bitsb(&ithc->regs->dma_rx[1].control, DMA_RX_CONTROL_IRQ_UNKNOWN_1 | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_READY | DMA_RX_CONTROL_IRQ_DATA, 0);
  1257. + bitsb(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_IRQ, 0);
  1258. +}
  1259. +
  1260. +static void ithc_clear_dma_rx_interrupts(struct ithc *ithc, unsigned int channel)
  1261. +{
  1262. + writel(DMA_RX_STATUS_ERROR | DMA_RX_STATUS_READY | DMA_RX_STATUS_HAVE_DATA,
  1263. + &ithc->regs->dma_rx[channel].status);
  1264. +}
  1265. +
  1266. +static void ithc_clear_interrupts(struct ithc *ithc)
  1267. +{
  1268. + writel(0xffffffff, &ithc->regs->error_flags);
  1269. + writel(ERROR_STATUS_DMA | ERROR_STATUS_SPI, &ithc->regs->error_status);
  1270. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  1271. + ithc_clear_dma_rx_interrupts(ithc, 0);
  1272. + ithc_clear_dma_rx_interrupts(ithc, 1);
  1273. + writel(DMA_TX_STATUS_DONE | DMA_TX_STATUS_ERROR | DMA_TX_STATUS_UNKNOWN_2,
  1274. + &ithc->regs->dma_tx.status);
  1275. +}
  1276. +
  1277. +static void ithc_idle_timer_callback(struct timer_list *t)
  1278. +{
  1279. + struct ithc *ithc = container_of(t, struct ithc, idle_timer);
  1280. + ithc_set_ltr_idle(ithc);
  1281. +}
  1282. +
  1283. +static void ithc_process(struct ithc *ithc)
  1284. +{
  1285. + ithc_log_regs(ithc);
  1286. +
  1287. + // The THC automatically transitions from LTR idle to active at the start of a DMA transfer.
  1288. + // It does not appear to automatically go back to idle, so we switch it back after a delay.
  1289. + mod_timer(&ithc->idle_timer, jiffies + msecs_to_jiffies(ithc_idle_delay_ms));
  1290. +
  1291. + bool rx0 = ithc_use_rx0 && (readl(&ithc->regs->dma_rx[0].status) & (DMA_RX_STATUS_ERROR | DMA_RX_STATUS_HAVE_DATA)) != 0;
  1292. + bool rx1 = ithc_use_rx1 && (readl(&ithc->regs->dma_rx[1].status) & (DMA_RX_STATUS_ERROR | DMA_RX_STATUS_HAVE_DATA)) != 0;
  1293. +
  1294. + // Read and clear error bits
  1295. + u32 err = readl(&ithc->regs->error_flags);
  1296. + if (err) {
  1297. + writel(err, &ithc->regs->error_flags);
  1298. + if (err & ~ERROR_FLAG_DMA_RX_TIMEOUT)
  1299. + pci_err(ithc->pci, "error flags: 0x%08x\n", err);
  1300. + if (err & ERROR_FLAG_DMA_RX_TIMEOUT)
  1301. + pci_err(ithc->pci, "DMA RX timeout/error (try decreasing activeltr/idleltr if this happens frequently)\n");
  1302. + }
  1303. +
  1304. + // Process DMA rx
  1305. + if (ithc_use_rx0) {
  1306. + ithc_clear_dma_rx_interrupts(ithc, 0);
  1307. + if (rx0)
  1308. + ithc_dma_rx(ithc, 0);
  1309. + }
  1310. + if (ithc_use_rx1) {
  1311. + ithc_clear_dma_rx_interrupts(ithc, 1);
  1312. + if (rx1)
  1313. + ithc_dma_rx(ithc, 1);
  1314. + }
  1315. +
  1316. + ithc_log_regs(ithc);
  1317. +}
  1318. +
  1319. +static irqreturn_t ithc_interrupt_thread(int irq, void *arg)
  1320. +{
  1321. + struct ithc *ithc = arg;
  1322. + pci_dbg(ithc->pci, "IRQ! err=%08x/%08x/%08x, cmd=%02x/%08x, rx0=%02x/%08x, rx1=%02x/%08x, tx=%02x/%08x\n",
  1323. + readl(&ithc->regs->error_control), readl(&ithc->regs->error_status), readl(&ithc->regs->error_flags),
  1324. + readb(&ithc->regs->spi_cmd.control), readl(&ithc->regs->spi_cmd.status),
  1325. + readb(&ithc->regs->dma_rx[0].control), readl(&ithc->regs->dma_rx[0].status),
  1326. + readb(&ithc->regs->dma_rx[1].control), readl(&ithc->regs->dma_rx[1].status),
  1327. + readb(&ithc->regs->dma_tx.control), readl(&ithc->regs->dma_tx.status));
  1328. + ithc_process(ithc);
  1329. + return IRQ_HANDLED;
  1330. +}
  1331. +
  1332. +static int ithc_poll_thread(void *arg)
  1333. +{
  1334. + struct ithc *ithc = arg;
  1335. + unsigned int sleep = 100;
  1336. + while (!kthread_should_stop()) {
  1337. + u32 n = ithc->dma_rx[1].num_received;
  1338. + ithc_process(ithc);
  1339. + // Decrease polling interval to 20ms if we received data, otherwise slowly
  1340. + // increase it up to 200ms.
  1341. + sleep = n != ithc->dma_rx[1].num_received ? 20
  1342. + : min(200u, sleep + (sleep >> 4) + 1);
  1343. + msleep_interruptible(sleep);
  1344. + }
  1345. + return 0;
  1346. +}
  1347. +
  1348. +// Device initialization and shutdown
  1349. +
  1350. +static void ithc_disable(struct ithc *ithc)
  1351. +{
  1352. + bitsl_set(&ithc->regs->control_bits, CONTROL_QUIESCE);
  1353. + CHECK(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, CONTROL_IS_QUIESCED);
  1354. + bitsl(&ithc->regs->control_bits, CONTROL_NRESET, 0);
  1355. + bitsb(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_SEND, 0);
  1356. + bitsb(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND, 0);
  1357. + bitsb(&ithc->regs->dma_rx[0].control, DMA_RX_CONTROL_ENABLE, 0);
  1358. + bitsb(&ithc->regs->dma_rx[1].control, DMA_RX_CONTROL_ENABLE, 0);
  1359. + ithc_disable_interrupts(ithc);
  1360. + ithc_clear_interrupts(ithc);
  1361. +}
  1362. +
  1363. +static int ithc_init_device(struct ithc *ithc)
  1364. +{
  1365. + // Read ACPI config for QuickSPI mode
  1366. + struct ithc_acpi_config cfg = { 0 };
  1367. + CHECK_RET(ithc_read_acpi_config, ithc, &cfg);
  1368. + if (!cfg.has_config)
  1369. + pci_info(ithc->pci, "no ACPI config, using legacy mode\n");
  1370. + else
  1371. + ithc_print_acpi_config(ithc, &cfg);
  1372. + ithc->use_quickspi = cfg.has_config;
  1373. +
  1374. + // Shut down device
  1375. + ithc_log_regs(ithc);
  1376. + bool was_enabled = (readl(&ithc->regs->control_bits) & CONTROL_NRESET) != 0;
  1377. + ithc_disable(ithc);
  1378. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_READY, CONTROL_READY);
  1379. + ithc_log_regs(ithc);
  1380. +
  1381. + // If the device was previously enabled, wait a bit to make sure it's fully shut down.
  1382. + if (was_enabled)
  1383. + if (msleep_interruptible(100))
  1384. + return -EINTR;
  1385. +
  1386. + // Set Latency Tolerance Reporting config. The device will automatically
  1387. + // apply these values depending on whether it is active or idle.
  1388. + // If active value is too high, DMA buffer data can become truncated.
  1389. + // By default, we set the active LTR value to 50us, and idle to 100ms.
  1390. + u64 active_ltr_ns = ithc_active_ltr_us >= 0 ? (u64)ithc_active_ltr_us * 1000
  1391. + : cfg.has_config && cfg.has_active_ltr ? (u64)cfg.active_ltr << 10
  1392. + : 50 * 1000;
  1393. + u64 idle_ltr_ns = ithc_idle_ltr_us >= 0 ? (u64)ithc_idle_ltr_us * 1000
  1394. + : cfg.has_config && cfg.has_idle_ltr ? (u64)cfg.idle_ltr << 10
  1395. + : 100 * 1000 * 1000;
  1396. + ithc_set_ltr_config(ithc, active_ltr_ns, idle_ltr_ns);
  1397. +
  1398. + if (ithc->use_quickspi)
  1399. + CHECK_RET(ithc_quickspi_init, ithc, &cfg);
  1400. + else
  1401. + CHECK_RET(ithc_legacy_init, ithc);
  1402. +
  1403. + return 0;
  1404. +}
  1405. +
  1406. +int ithc_reset(struct ithc *ithc)
  1407. +{
  1408. + // FIXME This should probably do devres_release_group()+ithc_start().
  1409. + // But because this is called during DMA processing, that would have to be done
  1410. + // asynchronously (schedule_work()?). And with extra locking?
  1411. + pci_err(ithc->pci, "reset\n");
  1412. + CHECK(ithc_init_device, ithc);
  1413. + if (ithc_use_rx0)
  1414. + ithc_dma_rx_enable(ithc, 0);
  1415. + if (ithc_use_rx1)
  1416. + ithc_dma_rx_enable(ithc, 1);
  1417. + ithc_log_regs(ithc);
  1418. + pci_dbg(ithc->pci, "reset completed\n");
  1419. + return 0;
  1420. +}
  1421. +
  1422. +static void ithc_stop(void *res)
  1423. +{
  1424. + struct ithc *ithc = res;
  1425. + pci_dbg(ithc->pci, "stopping\n");
  1426. + ithc_log_regs(ithc);
  1427. +
  1428. + if (ithc->poll_thread)
  1429. + CHECK(kthread_stop, ithc->poll_thread);
  1430. + if (ithc->irq >= 0)
  1431. + disable_irq(ithc->irq);
  1432. + if (ithc->use_quickspi)
  1433. + ithc_quickspi_exit(ithc);
  1434. + else
  1435. + ithc_legacy_exit(ithc);
  1436. + ithc_disable(ithc);
  1437. + del_timer_sync(&ithc->idle_timer);
  1438. +
  1439. + // Clear DMA config.
  1440. + for (unsigned int i = 0; i < 2; i++) {
  1441. + CHECK(waitl, ithc, &ithc->regs->dma_rx[i].status, DMA_RX_STATUS_ENABLED, 0);
  1442. + lo_hi_writeq(0, &ithc->regs->dma_rx[i].addr);
  1443. + writeb(0, &ithc->regs->dma_rx[i].num_bufs);
  1444. + writeb(0, &ithc->regs->dma_rx[i].num_prds);
  1445. + }
  1446. + lo_hi_writeq(0, &ithc->regs->dma_tx.addr);
  1447. + writeb(0, &ithc->regs->dma_tx.num_prds);
  1448. +
  1449. + ithc_log_regs(ithc);
  1450. + pci_dbg(ithc->pci, "stopped\n");
  1451. +}
  1452. +
  1453. +static void ithc_clear_drvdata(void *res)
  1454. +{
  1455. + struct pci_dev *pci = res;
  1456. + pci_set_drvdata(pci, NULL);
  1457. +}
  1458. +
  1459. +static int ithc_start(struct pci_dev *pci)
  1460. +{
  1461. + pci_dbg(pci, "starting\n");
  1462. + if (pci_get_drvdata(pci)) {
  1463. + pci_err(pci, "device already initialized\n");
  1464. + return -EINVAL;
  1465. + }
  1466. + if (!devres_open_group(&pci->dev, ithc_start, GFP_KERNEL))
  1467. + return -ENOMEM;
  1468. +
  1469. + // Allocate/init main driver struct.
  1470. + struct ithc *ithc = devm_kzalloc(&pci->dev, sizeof(*ithc), GFP_KERNEL);
  1471. + if (!ithc)
  1472. + return -ENOMEM;
  1473. + ithc->irq = -1;
  1474. + ithc->pci = pci;
  1475. + snprintf(ithc->phys, sizeof(ithc->phys), "pci-%s/" DEVNAME, pci_name(pci));
  1476. + pci_set_drvdata(pci, ithc);
  1477. + CHECK_RET(devm_add_action_or_reset, &pci->dev, ithc_clear_drvdata, pci);
  1478. + if (ithc_log_regs_enabled)
  1479. + ithc->prev_regs = devm_kzalloc(&pci->dev, sizeof(*ithc->prev_regs), GFP_KERNEL);
  1480. +
  1481. + // PCI initialization.
  1482. + CHECK_RET(pcim_enable_device, pci);
  1483. + pci_set_master(pci);
  1484. + CHECK_RET(pcim_iomap_regions, pci, BIT(0), DEVNAME " regs");
  1485. + CHECK_RET(dma_set_mask_and_coherent, &pci->dev, DMA_BIT_MASK(64));
  1486. + CHECK_RET(pci_set_power_state, pci, PCI_D0);
  1487. + ithc->regs = pcim_iomap_table(pci)[0];
  1488. +
  1489. + // Allocate IRQ.
  1490. + if (!ithc_use_polling) {
  1491. + CHECK_RET(pci_alloc_irq_vectors, pci, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
  1492. + ithc->irq = CHECK(pci_irq_vector, pci, 0);
  1493. + if (ithc->irq < 0)
  1494. + return ithc->irq;
  1495. + }
  1496. +
  1497. + // Initialize THC and touch device.
  1498. + CHECK_RET(ithc_init_device, ithc);
  1499. +
  1500. + // Initialize HID and DMA.
  1501. + CHECK_RET(ithc_hid_init, ithc);
  1502. + if (ithc_use_rx0)
  1503. + CHECK_RET(ithc_dma_rx_init, ithc, 0);
  1504. + if (ithc_use_rx1)
  1505. + CHECK_RET(ithc_dma_rx_init, ithc, 1);
  1506. + CHECK_RET(ithc_dma_tx_init, ithc);
  1507. +
  1508. + timer_setup(&ithc->idle_timer, ithc_idle_timer_callback, 0);
  1509. +
  1510. + // Add ithc_stop() callback AFTER setting up DMA buffers, so that polling/irqs/DMA are
  1511. + // disabled BEFORE the buffers are freed.
  1512. + CHECK_RET(devm_add_action_or_reset, &pci->dev, ithc_stop, ithc);
  1513. +
  1514. + // Start polling/IRQ.
  1515. + if (ithc_use_polling) {
  1516. + pci_info(pci, "using polling instead of irq\n");
  1517. + // Use a thread instead of simple timer because we want to be able to sleep.
  1518. + ithc->poll_thread = kthread_run(ithc_poll_thread, ithc, DEVNAME "poll");
  1519. + if (IS_ERR(ithc->poll_thread)) {
  1520. + int err = PTR_ERR(ithc->poll_thread);
  1521. + ithc->poll_thread = NULL;
  1522. + return err;
  1523. + }
  1524. + } else {
  1525. + CHECK_RET(devm_request_threaded_irq, &pci->dev, ithc->irq, NULL,
  1526. + ithc_interrupt_thread, IRQF_TRIGGER_HIGH | IRQF_ONESHOT, DEVNAME, ithc);
  1527. + }
  1528. +
  1529. + if (ithc_use_rx0)
  1530. + ithc_dma_rx_enable(ithc, 0);
  1531. + if (ithc_use_rx1)
  1532. + ithc_dma_rx_enable(ithc, 1);
  1533. +
  1534. + // hid_add_device() can only be called after irq/polling is started and DMA is enabled,
  1535. + // because it calls ithc_hid_parse() which reads the report descriptor via DMA.
  1536. + CHECK_RET(hid_add_device, ithc->hid.dev);
  1537. +
  1538. + CHECK(ithc_debug_init_device, ithc);
  1539. +
  1540. + ithc_set_ltr_idle(ithc);
  1541. +
  1542. + pci_dbg(pci, "started\n");
  1543. + return 0;
  1544. +}
  1545. +
  1546. +static int ithc_probe(struct pci_dev *pci, const struct pci_device_id *id)
  1547. +{
  1548. + pci_dbg(pci, "device probe\n");
  1549. + return ithc_start(pci);
  1550. +}
  1551. +
  1552. +static void ithc_remove(struct pci_dev *pci)
  1553. +{
  1554. + pci_dbg(pci, "device remove\n");
  1555. + // all cleanup is handled by devres
  1556. +}
  1557. +
  1558. +// For suspend/resume, we just deinitialize and reinitialize everything.
  1559. +// TODO It might be cleaner to keep the HID device around, however we would then have to signal
  1560. +// to userspace that the touch device has lost state and userspace needs to e.g. resend 'set
  1561. +// feature' requests. Hidraw does not seem to have a facility to do that.
  1562. +static int ithc_suspend(struct device *dev)
  1563. +{
  1564. + struct pci_dev *pci = to_pci_dev(dev);
  1565. + pci_dbg(pci, "pm suspend\n");
  1566. + devres_release_group(dev, ithc_start);
  1567. + return 0;
  1568. +}
  1569. +
  1570. +static int ithc_resume(struct device *dev)
  1571. +{
  1572. + struct pci_dev *pci = to_pci_dev(dev);
  1573. + pci_dbg(pci, "pm resume\n");
  1574. + return ithc_start(pci);
  1575. +}
  1576. +
  1577. +static int ithc_freeze(struct device *dev)
  1578. +{
  1579. + struct pci_dev *pci = to_pci_dev(dev);
  1580. + pci_dbg(pci, "pm freeze\n");
  1581. + devres_release_group(dev, ithc_start);
  1582. + return 0;
  1583. +}
  1584. +
  1585. +static int ithc_thaw(struct device *dev)
  1586. +{
  1587. + struct pci_dev *pci = to_pci_dev(dev);
  1588. + pci_dbg(pci, "pm thaw\n");
  1589. + return ithc_start(pci);
  1590. +}
  1591. +
  1592. +static int ithc_restore(struct device *dev)
  1593. +{
  1594. + struct pci_dev *pci = to_pci_dev(dev);
  1595. + pci_dbg(pci, "pm restore\n");
  1596. + return ithc_start(pci);
  1597. +}
  1598. +
  1599. +static struct pci_driver ithc_driver = {
  1600. + .name = DEVNAME,
  1601. + .id_table = ithc_pci_tbl,
  1602. + .probe = ithc_probe,
  1603. + .remove = ithc_remove,
  1604. + .driver.pm = &(const struct dev_pm_ops) {
  1605. + .suspend = ithc_suspend,
  1606. + .resume = ithc_resume,
  1607. + .freeze = ithc_freeze,
  1608. + .thaw = ithc_thaw,
  1609. + .restore = ithc_restore,
  1610. + },
  1611. + .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1612. +};
  1613. +
  1614. +static int __init ithc_init(void)
  1615. +{
  1616. + ithc_debug_init_module();
  1617. + return pci_register_driver(&ithc_driver);
  1618. +}
  1619. +
  1620. +static void __exit ithc_exit(void)
  1621. +{
  1622. + pci_unregister_driver(&ithc_driver);
  1623. + ithc_debug_exit_module();
  1624. +}
  1625. +
  1626. +module_init(ithc_init);
  1627. +module_exit(ithc_exit);
  1628. +
  1629. diff --git a/drivers/hid/ithc/ithc-quickspi.c b/drivers/hid/ithc/ithc-quickspi.c
  1630. new file mode 100644
  1631. index 000000000000..e2d1690b8cf8
  1632. --- /dev/null
  1633. +++ b/drivers/hid/ithc/ithc-quickspi.c
  1634. @@ -0,0 +1,607 @@
  1635. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  1636. +
  1637. +// Some public THC/QuickSPI documentation can be found in:
  1638. +// - Intel Firmware Support Package repo: https://github.com/intel/FSP
  1639. +// - HID over SPI (HIDSPI) spec: https://www.microsoft.com/en-us/download/details.aspx?id=103325
  1640. +
  1641. +#include "ithc.h"
  1642. +
  1643. +static const guid_t guid_hidspi =
  1644. + GUID_INIT(0x6e2ac436, 0x0fcf, 0x41af, 0xa2, 0x65, 0xb3, 0x2a, 0x22, 0x0d, 0xcf, 0xab);
  1645. +static const guid_t guid_thc_quickspi =
  1646. + GUID_INIT(0x300d35b7, 0xac20, 0x413e, 0x8e, 0x9c, 0x92, 0xe4, 0xda, 0xfd, 0x0a, 0xfe);
  1647. +static const guid_t guid_thc_ltr =
  1648. + GUID_INIT(0x84005682, 0x5b71, 0x41a4, 0x8d, 0x66, 0x81, 0x30, 0xf7, 0x87, 0xa1, 0x38);
  1649. +
  1650. +// TODO The HIDSPI spec says revision should be 3. Should we try both?
  1651. +#define DSM_REV 2
  1652. +
  1653. +struct hidspi_header {
  1654. + u8 type;
  1655. + u16 len;
  1656. + u8 id;
  1657. +} __packed;
  1658. +static_assert(sizeof(struct hidspi_header) == 4);
  1659. +
  1660. +#define HIDSPI_INPUT_TYPE_DATA 1
  1661. +#define HIDSPI_INPUT_TYPE_RESET_RESPONSE 3
  1662. +#define HIDSPI_INPUT_TYPE_COMMAND_RESPONSE 4
  1663. +#define HIDSPI_INPUT_TYPE_GET_FEATURE_RESPONSE 5
  1664. +#define HIDSPI_INPUT_TYPE_DEVICE_DESCRIPTOR 7
  1665. +#define HIDSPI_INPUT_TYPE_REPORT_DESCRIPTOR 8
  1666. +#define HIDSPI_INPUT_TYPE_SET_FEATURE_RESPONSE 9
  1667. +#define HIDSPI_INPUT_TYPE_OUTPUT_REPORT_RESPONSE 10
  1668. +#define HIDSPI_INPUT_TYPE_GET_INPUT_REPORT_RESPONSE 11
  1669. +
  1670. +#define HIDSPI_OUTPUT_TYPE_DEVICE_DESCRIPTOR_REQUEST 1
  1671. +#define HIDSPI_OUTPUT_TYPE_REPORT_DESCRIPTOR_REQUEST 2
  1672. +#define HIDSPI_OUTPUT_TYPE_SET_FEATURE 3
  1673. +#define HIDSPI_OUTPUT_TYPE_GET_FEATURE 4
  1674. +#define HIDSPI_OUTPUT_TYPE_OUTPUT_REPORT 5
  1675. +#define HIDSPI_OUTPUT_TYPE_INPUT_REPORT_REQUEST 6
  1676. +#define HIDSPI_OUTPUT_TYPE_COMMAND 7
  1677. +
  1678. +struct hidspi_device_descriptor {
  1679. + u16 wDeviceDescLength;
  1680. + u16 bcdVersion;
  1681. + u16 wReportDescLength;
  1682. + u16 wMaxInputLength;
  1683. + u16 wMaxOutputLength;
  1684. + u16 wMaxFragmentLength;
  1685. + u16 wVendorID;
  1686. + u16 wProductID;
  1687. + u16 wVersionID;
  1688. + u16 wFlags;
  1689. + u32 dwReserved;
  1690. +};
  1691. +static_assert(sizeof(struct hidspi_device_descriptor) == 24);
  1692. +
  1693. +static int read_acpi_u32(struct ithc *ithc, const guid_t *guid, u32 func, u32 *dest)
  1694. +{
  1695. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1696. + union acpi_object *o = acpi_evaluate_dsm(handle, guid, DSM_REV, func, NULL);
  1697. + if (!o)
  1698. + return 0;
  1699. + if (o->type != ACPI_TYPE_INTEGER) {
  1700. + pci_err(ithc->pci, "DSM %pUl %u returned type %i instead of integer\n",
  1701. + guid, func, o->type);
  1702. + ACPI_FREE(o);
  1703. + return -1;
  1704. + }
  1705. + pci_dbg(ithc->pci, "DSM %pUl %u = 0x%08x\n", guid, func, (u32)o->integer.value);
  1706. + *dest = (u32)o->integer.value;
  1707. + ACPI_FREE(o);
  1708. + return 1;
  1709. +}
  1710. +
  1711. +static int read_acpi_buf(struct ithc *ithc, const guid_t *guid, u32 func, size_t len, u8 *dest)
  1712. +{
  1713. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1714. + union acpi_object *o = acpi_evaluate_dsm(handle, guid, DSM_REV, func, NULL);
  1715. + if (!o)
  1716. + return 0;
  1717. + if (o->type != ACPI_TYPE_BUFFER) {
  1718. + pci_err(ithc->pci, "DSM %pUl %u returned type %i instead of buffer\n",
  1719. + guid, func, o->type);
  1720. + ACPI_FREE(o);
  1721. + return -1;
  1722. + }
  1723. + if (o->buffer.length != len) {
  1724. + pci_err(ithc->pci, "DSM %pUl %u returned len %u instead of %zu\n",
  1725. + guid, func, o->buffer.length, len);
  1726. + ACPI_FREE(o);
  1727. + return -1;
  1728. + }
  1729. + memcpy(dest, o->buffer.pointer, len);
  1730. + pci_dbg(ithc->pci, "DSM %pUl %u = 0x%02x\n", guid, func, dest[0]);
  1731. + ACPI_FREE(o);
  1732. + return 1;
  1733. +}
  1734. +
  1735. +int ithc_read_acpi_config(struct ithc *ithc, struct ithc_acpi_config *cfg)
  1736. +{
  1737. + int r;
  1738. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1739. +
  1740. + cfg->has_config = acpi_check_dsm(handle, &guid_hidspi, DSM_REV, BIT(0));
  1741. + if (!cfg->has_config)
  1742. + return 0;
  1743. +
  1744. + // HIDSPI settings
  1745. +
  1746. + r = read_acpi_u32(ithc, &guid_hidspi, 1, &cfg->input_report_header_address);
  1747. + if (r < 0)
  1748. + return r;
  1749. + cfg->has_input_report_header_address = r > 0;
  1750. + if (r > 0 && cfg->input_report_header_address > 0xffffff) {
  1751. + pci_err(ithc->pci, "Invalid input report header address 0x%x\n",
  1752. + cfg->input_report_header_address);
  1753. + return -1;
  1754. + }
  1755. +
  1756. + r = read_acpi_u32(ithc, &guid_hidspi, 2, &cfg->input_report_body_address);
  1757. + if (r < 0)
  1758. + return r;
  1759. + cfg->has_input_report_body_address = r > 0;
  1760. + if (r > 0 && cfg->input_report_body_address > 0xffffff) {
  1761. + pci_err(ithc->pci, "Invalid input report body address 0x%x\n",
  1762. + cfg->input_report_body_address);
  1763. + return -1;
  1764. + }
  1765. +
  1766. + r = read_acpi_u32(ithc, &guid_hidspi, 3, &cfg->output_report_body_address);
  1767. + if (r < 0)
  1768. + return r;
  1769. + cfg->has_output_report_body_address = r > 0;
  1770. + if (r > 0 && cfg->output_report_body_address > 0xffffff) {
  1771. + pci_err(ithc->pci, "Invalid output report body address 0x%x\n",
  1772. + cfg->output_report_body_address);
  1773. + return -1;
  1774. + }
  1775. +
  1776. + r = read_acpi_buf(ithc, &guid_hidspi, 4, sizeof(cfg->read_opcode), &cfg->read_opcode);
  1777. + if (r < 0)
  1778. + return r;
  1779. + cfg->has_read_opcode = r > 0;
  1780. +
  1781. + r = read_acpi_buf(ithc, &guid_hidspi, 5, sizeof(cfg->write_opcode), &cfg->write_opcode);
  1782. + if (r < 0)
  1783. + return r;
  1784. + cfg->has_write_opcode = r > 0;
  1785. +
  1786. + u32 flags;
  1787. + r = read_acpi_u32(ithc, &guid_hidspi, 6, &flags);
  1788. + if (r < 0)
  1789. + return r;
  1790. + cfg->has_read_mode = cfg->has_write_mode = r > 0;
  1791. + if (r > 0) {
  1792. + cfg->read_mode = (flags >> 14) & 3;
  1793. + cfg->write_mode = flags & BIT(13) ? cfg->read_mode : SPI_MODE_SINGLE;
  1794. + }
  1795. +
  1796. + // Quick SPI settings
  1797. +
  1798. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 1, &cfg->spi_frequency);
  1799. + if (r < 0)
  1800. + return r;
  1801. + cfg->has_spi_frequency = r > 0;
  1802. +
  1803. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 2, &cfg->limit_packet_size);
  1804. + if (r < 0)
  1805. + return r;
  1806. + cfg->has_limit_packet_size = r > 0;
  1807. +
  1808. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 3, &cfg->tx_delay);
  1809. + if (r < 0)
  1810. + return r;
  1811. + cfg->has_tx_delay = r > 0;
  1812. + if (r > 0)
  1813. + cfg->tx_delay &= 0xffff;
  1814. +
  1815. + // LTR settings
  1816. +
  1817. + r = read_acpi_u32(ithc, &guid_thc_ltr, 1, &cfg->active_ltr);
  1818. + if (r < 0)
  1819. + return r;
  1820. + cfg->has_active_ltr = r > 0;
  1821. + if (r > 0 && (!cfg->active_ltr || cfg->active_ltr > 0x3ff)) {
  1822. + if (cfg->active_ltr != 0xffffffff)
  1823. + pci_warn(ithc->pci, "Ignoring invalid active LTR value 0x%x\n",
  1824. + cfg->active_ltr);
  1825. + cfg->active_ltr = 500;
  1826. + }
  1827. +
  1828. + r = read_acpi_u32(ithc, &guid_thc_ltr, 2, &cfg->idle_ltr);
  1829. + if (r < 0)
  1830. + return r;
  1831. + cfg->has_idle_ltr = r > 0;
  1832. + if (r > 0 && (!cfg->idle_ltr || cfg->idle_ltr > 0x3ff)) {
  1833. + if (cfg->idle_ltr != 0xffffffff)
  1834. + pci_warn(ithc->pci, "Ignoring invalid idle LTR value 0x%x\n",
  1835. + cfg->idle_ltr);
  1836. + cfg->idle_ltr = 500;
  1837. + if (cfg->has_active_ltr && cfg->active_ltr > cfg->idle_ltr)
  1838. + cfg->idle_ltr = cfg->active_ltr;
  1839. + }
  1840. +
  1841. + return 0;
  1842. +}
  1843. +
  1844. +void ithc_print_acpi_config(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  1845. +{
  1846. + if (!cfg->has_config) {
  1847. + pci_info(ithc->pci, "No ACPI config");
  1848. + return;
  1849. + }
  1850. +
  1851. + char input_report_header_address[16] = "-";
  1852. + if (cfg->has_input_report_header_address)
  1853. + sprintf(input_report_header_address, "0x%x", cfg->input_report_header_address);
  1854. + char input_report_body_address[16] = "-";
  1855. + if (cfg->has_input_report_body_address)
  1856. + sprintf(input_report_body_address, "0x%x", cfg->input_report_body_address);
  1857. + char output_report_body_address[16] = "-";
  1858. + if (cfg->has_output_report_body_address)
  1859. + sprintf(output_report_body_address, "0x%x", cfg->output_report_body_address);
  1860. + char read_opcode[16] = "-";
  1861. + if (cfg->has_read_opcode)
  1862. + sprintf(read_opcode, "0x%02x", cfg->read_opcode);
  1863. + char write_opcode[16] = "-";
  1864. + if (cfg->has_write_opcode)
  1865. + sprintf(write_opcode, "0x%02x", cfg->write_opcode);
  1866. + char read_mode[16] = "-";
  1867. + if (cfg->has_read_mode)
  1868. + sprintf(read_mode, "%i", cfg->read_mode);
  1869. + char write_mode[16] = "-";
  1870. + if (cfg->has_write_mode)
  1871. + sprintf(write_mode, "%i", cfg->write_mode);
  1872. + char spi_frequency[16] = "-";
  1873. + if (cfg->has_spi_frequency)
  1874. + sprintf(spi_frequency, "%u", cfg->spi_frequency);
  1875. + char limit_packet_size[16] = "-";
  1876. + if (cfg->has_limit_packet_size)
  1877. + sprintf(limit_packet_size, "%u", cfg->limit_packet_size);
  1878. + char tx_delay[16] = "-";
  1879. + if (cfg->has_tx_delay)
  1880. + sprintf(tx_delay, "%u", cfg->tx_delay);
  1881. + char active_ltr[16] = "-";
  1882. + if (cfg->has_active_ltr)
  1883. + sprintf(active_ltr, "%u", cfg->active_ltr);
  1884. + char idle_ltr[16] = "-";
  1885. + if (cfg->has_idle_ltr)
  1886. + sprintf(idle_ltr, "%u", cfg->idle_ltr);
  1887. +
  1888. + pci_info(ithc->pci, "ACPI config: InputHeaderAddr=%s InputBodyAddr=%s OutputBodyAddr=%s ReadOpcode=%s WriteOpcode=%s ReadMode=%s WriteMode=%s Frequency=%s LimitPacketSize=%s TxDelay=%s ActiveLTR=%s IdleLTR=%s\n",
  1889. + input_report_header_address, input_report_body_address, output_report_body_address,
  1890. + read_opcode, write_opcode, read_mode, write_mode,
  1891. + spi_frequency, limit_packet_size, tx_delay, active_ltr, idle_ltr);
  1892. +}
  1893. +
  1894. +static void set_opcode(struct ithc *ithc, size_t i, u8 opcode)
  1895. +{
  1896. + writeb(opcode, &ithc->regs->opcode[i].header);
  1897. + writeb(opcode, &ithc->regs->opcode[i].single);
  1898. + writeb(opcode, &ithc->regs->opcode[i].dual);
  1899. + writeb(opcode, &ithc->regs->opcode[i].quad);
  1900. +}
  1901. +
  1902. +static int ithc_quickspi_init_regs(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  1903. +{
  1904. + pci_dbg(ithc->pci, "initializing QuickSPI registers\n");
  1905. +
  1906. + // SPI frequency and mode
  1907. + if (!cfg->has_spi_frequency || !cfg->spi_frequency) {
  1908. + pci_err(ithc->pci, "Missing SPI frequency in configuration\n");
  1909. + return -EINVAL;
  1910. + }
  1911. + unsigned int clkdiv = DIV_ROUND_UP(SPI_CLK_FREQ_BASE, cfg->spi_frequency);
  1912. + bool clkdiv8 = clkdiv > 7;
  1913. + if (clkdiv8)
  1914. + clkdiv = min(7u, DIV_ROUND_UP(clkdiv, 8u));
  1915. + if (!clkdiv)
  1916. + clkdiv = 1;
  1917. + CHECK_RET(ithc_set_spi_config, ithc, clkdiv, clkdiv8,
  1918. + cfg->has_read_mode ? cfg->read_mode : SPI_MODE_SINGLE,
  1919. + cfg->has_write_mode ? cfg->write_mode : SPI_MODE_SINGLE);
  1920. +
  1921. + // SPI addresses and opcodes
  1922. + if (cfg->has_input_report_header_address)
  1923. + writel(cfg->input_report_header_address, &ithc->regs->spi_header_addr);
  1924. + if (cfg->has_input_report_body_address) {
  1925. + writel(cfg->input_report_body_address, &ithc->regs->dma_rx[0].spi_addr);
  1926. + writel(cfg->input_report_body_address, &ithc->regs->dma_rx[1].spi_addr);
  1927. + }
  1928. + if (cfg->has_output_report_body_address)
  1929. + writel(cfg->output_report_body_address, &ithc->regs->dma_tx.spi_addr);
  1930. +
  1931. + switch (ithc->pci->device) {
  1932. + // LKF/TGL don't support QuickSPI.
  1933. + // For ADL, opcode layout is RX/TX/unused.
  1934. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1:
  1935. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2:
  1936. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1:
  1937. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2:
  1938. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1:
  1939. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2:
  1940. + if (cfg->has_read_opcode) {
  1941. + set_opcode(ithc, 0, cfg->read_opcode);
  1942. + }
  1943. + if (cfg->has_write_opcode) {
  1944. + set_opcode(ithc, 1, cfg->write_opcode);
  1945. + }
  1946. + break;
  1947. + // For MTL, opcode layout was changed to RX/RX/TX.
  1948. + // (RPL layout is unknown.)
  1949. + default:
  1950. + if (cfg->has_read_opcode) {
  1951. + set_opcode(ithc, 0, cfg->read_opcode);
  1952. + set_opcode(ithc, 1, cfg->read_opcode);
  1953. + }
  1954. + if (cfg->has_write_opcode) {
  1955. + set_opcode(ithc, 2, cfg->write_opcode);
  1956. + }
  1957. + break;
  1958. + }
  1959. +
  1960. + ithc_log_regs(ithc);
  1961. +
  1962. + // The rest...
  1963. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  1964. +
  1965. + bitsl(&ithc->regs->quickspi_config1,
  1966. + QUICKSPI_CONFIG1_UNKNOWN_0(0xff) | QUICKSPI_CONFIG1_UNKNOWN_5(0xff) |
  1967. + QUICKSPI_CONFIG1_UNKNOWN_10(0xff) | QUICKSPI_CONFIG1_UNKNOWN_16(0xffff),
  1968. + QUICKSPI_CONFIG1_UNKNOWN_0(4) | QUICKSPI_CONFIG1_UNKNOWN_5(4) |
  1969. + QUICKSPI_CONFIG1_UNKNOWN_10(22) | QUICKSPI_CONFIG1_UNKNOWN_16(2));
  1970. +
  1971. + bitsl(&ithc->regs->quickspi_config2,
  1972. + QUICKSPI_CONFIG2_UNKNOWN_0(0xff) | QUICKSPI_CONFIG2_UNKNOWN_5(0xff) |
  1973. + QUICKSPI_CONFIG2_UNKNOWN_12(0xff),
  1974. + QUICKSPI_CONFIG2_UNKNOWN_0(8) | QUICKSPI_CONFIG2_UNKNOWN_5(14) |
  1975. + QUICKSPI_CONFIG2_UNKNOWN_12(2));
  1976. +
  1977. + u32 pktsize = cfg->has_limit_packet_size && cfg->limit_packet_size == 1 ? 4 : 0x80;
  1978. + bitsl(&ithc->regs->spi_config,
  1979. + SPI_CONFIG_READ_PACKET_SIZE(0xfff) | SPI_CONFIG_WRITE_PACKET_SIZE(0xfff),
  1980. + SPI_CONFIG_READ_PACKET_SIZE(pktsize) | SPI_CONFIG_WRITE_PACKET_SIZE(pktsize));
  1981. +
  1982. + bitsl_set(&ithc->regs->quickspi_config2,
  1983. + QUICKSPI_CONFIG2_UNKNOWN_16 | QUICKSPI_CONFIG2_UNKNOWN_17);
  1984. + bitsl(&ithc->regs->quickspi_config2,
  1985. + QUICKSPI_CONFIG2_DISABLE_READ_ADDRESS_INCREMENT |
  1986. + QUICKSPI_CONFIG2_DISABLE_WRITE_ADDRESS_INCREMENT |
  1987. + QUICKSPI_CONFIG2_ENABLE_WRITE_STREAMING_MODE, 0);
  1988. +
  1989. + return 0;
  1990. +}
  1991. +
  1992. +static int wait_for_report(struct ithc *ithc)
  1993. +{
  1994. + CHECK_RET(waitl, ithc, &ithc->regs->dma_rx[0].status,
  1995. + DMA_RX_STATUS_READY, DMA_RX_STATUS_READY);
  1996. + writel(DMA_RX_STATUS_READY, &ithc->regs->dma_rx[0].status);
  1997. +
  1998. + u32 h = readl(&ithc->regs->input_header);
  1999. + ithc_log_regs(ithc);
  2000. + if (INPUT_HEADER_SYNC(h) != INPUT_HEADER_SYNC_VALUE
  2001. + || INPUT_HEADER_VERSION(h) != INPUT_HEADER_VERSION_VALUE) {
  2002. + pci_err(ithc->pci, "invalid input report frame header 0x%08x\n", h);
  2003. + return -ENODATA;
  2004. + }
  2005. + return INPUT_HEADER_REPORT_LENGTH(h) * 4;
  2006. +}
  2007. +
  2008. +static int ithc_quickspi_init_hidspi(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  2009. +{
  2010. + pci_dbg(ithc->pci, "initializing HIDSPI\n");
  2011. +
  2012. + // HIDSPI initialization sequence:
  2013. + // "1. The host shall invoke the ACPI reset method to clear the device state."
  2014. + acpi_status s = acpi_evaluate_object(ACPI_HANDLE(&ithc->pci->dev), "_RST", NULL, NULL);
  2015. + if (ACPI_FAILURE(s)) {
  2016. + pci_err(ithc->pci, "ACPI reset failed\n");
  2017. + return -EIO;
  2018. + }
  2019. +
  2020. + bitsl(&ithc->regs->control_bits, CONTROL_QUIESCE, 0);
  2021. +
  2022. + // "2. Within 1 second, the device shall signal an interrupt and make available to the host
  2023. + // an input report containing a device reset response."
  2024. + int size = wait_for_report(ithc);
  2025. + if (size < 0)
  2026. + return size;
  2027. + if (size < sizeof(struct hidspi_header)) {
  2028. + pci_err(ithc->pci, "SPI data size too small for reset response (%u)\n", size);
  2029. + return -EMSGSIZE;
  2030. + }
  2031. +
  2032. + // "3. The host shall read the reset response from the device at the Input Report addresses
  2033. + // specified in ACPI."
  2034. + u32 in_addr = cfg->has_input_report_body_address ? cfg->input_report_body_address : 0x1000;
  2035. + struct {
  2036. + struct hidspi_header header;
  2037. + union {
  2038. + struct hidspi_device_descriptor device_desc;
  2039. + u32 data[16];
  2040. + };
  2041. + } resp = { 0 };
  2042. + if (size > sizeof(resp)) {
  2043. + pci_err(ithc->pci, "SPI data size for reset response too big (%u)\n", size);
  2044. + return -EMSGSIZE;
  2045. + }
  2046. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, in_addr, size, &resp);
  2047. + if (resp.header.type != HIDSPI_INPUT_TYPE_RESET_RESPONSE) {
  2048. + pci_err(ithc->pci, "received type %i instead of reset response\n", resp.header.type);
  2049. + return -ENOMSG;
  2050. + }
  2051. +
  2052. + // "4. The host shall then write an Output Report to the device at the Output Report Address
  2053. + // specified in ACPI, requesting the Device Descriptor from the device."
  2054. + u32 out_addr = cfg->has_output_report_body_address ? cfg->output_report_body_address : 0x1000;
  2055. + struct hidspi_header req = { .type = HIDSPI_OUTPUT_TYPE_DEVICE_DESCRIPTOR_REQUEST };
  2056. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_WRITE, out_addr, sizeof(req), &req);
  2057. +
  2058. + // "5. Within 1 second, the device shall signal an interrupt and make available to the host
  2059. + // an input report containing the Device Descriptor."
  2060. + size = wait_for_report(ithc);
  2061. + if (size < 0)
  2062. + return size;
  2063. + if (size < sizeof(resp.header) + sizeof(resp.device_desc)) {
  2064. + pci_err(ithc->pci, "SPI data size too small for device descriptor (%u)\n", size);
  2065. + return -EMSGSIZE;
  2066. + }
  2067. +
  2068. + // "6. The host shall read the Device Descriptor from the Input Report addresses specified
  2069. + // in ACPI."
  2070. + if (size > sizeof(resp)) {
  2071. + pci_err(ithc->pci, "SPI data size for device descriptor too big (%u)\n", size);
  2072. + return -EMSGSIZE;
  2073. + }
  2074. + memset(&resp, 0, sizeof(resp));
  2075. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, in_addr, size, &resp);
  2076. + if (resp.header.type != HIDSPI_INPUT_TYPE_DEVICE_DESCRIPTOR) {
  2077. + pci_err(ithc->pci, "received type %i instead of device descriptor\n",
  2078. + resp.header.type);
  2079. + return -ENOMSG;
  2080. + }
  2081. + struct hidspi_device_descriptor *d = &resp.device_desc;
  2082. + if (resp.header.len < sizeof(*d)) {
  2083. + pci_err(ithc->pci, "response too small for device descriptor (%u)\n",
  2084. + resp.header.len);
  2085. + return -EMSGSIZE;
  2086. + }
  2087. + if (d->wDeviceDescLength != sizeof(*d)) {
  2088. + pci_err(ithc->pci, "invalid device descriptor length (%u)\n",
  2089. + d->wDeviceDescLength);
  2090. + return -EMSGSIZE;
  2091. + }
  2092. +
  2093. + pci_info(ithc->pci, "Device descriptor: bcdVersion=0x%04x wReportDescLength=%u wMaxInputLength=%u wMaxOutputLength=%u wMaxFragmentLength=%u wVendorID=0x%04x wProductID=0x%04x wVersionID=0x%04x wFlags=0x%04x dwReserved=0x%08x\n",
  2094. + d->bcdVersion, d->wReportDescLength,
  2095. + d->wMaxInputLength, d->wMaxOutputLength, d->wMaxFragmentLength,
  2096. + d->wVendorID, d->wProductID, d->wVersionID,
  2097. + d->wFlags, d->dwReserved);
  2098. +
  2099. + ithc->vendor_id = d->wVendorID;
  2100. + ithc->product_id = d->wProductID;
  2101. + ithc->product_rev = d->wVersionID;
  2102. + ithc->max_rx_size = max_t(u32, d->wMaxInputLength,
  2103. + d->wReportDescLength + sizeof(struct hidspi_header));
  2104. + ithc->max_tx_size = d->wMaxOutputLength;
  2105. + ithc->have_config = true;
  2106. +
  2107. + // "7. The device and host shall then enter their "Ready" states - where the device may
  2108. + // begin sending Input Reports, and the device shall be prepared for Output Reports from
  2109. + // the host."
  2110. +
  2111. + return 0;
  2112. +}
  2113. +
  2114. +int ithc_quickspi_init(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  2115. +{
  2116. + bitsl_set(&ithc->regs->control_bits, CONTROL_QUIESCE);
  2117. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, CONTROL_IS_QUIESCED);
  2118. +
  2119. + ithc_log_regs(ithc);
  2120. + CHECK_RET(ithc_quickspi_init_regs, ithc, cfg);
  2121. + ithc_log_regs(ithc);
  2122. + CHECK_RET(ithc_quickspi_init_hidspi, ithc, cfg);
  2123. + ithc_log_regs(ithc);
  2124. +
  2125. + // This value is set to 2 in ithc_quickspi_init_regs(). It needs to be set to 1 here,
  2126. + // otherwise DMA will not work. Maybe selects between DMA and PIO mode?
  2127. + bitsl(&ithc->regs->quickspi_config1,
  2128. + QUICKSPI_CONFIG1_UNKNOWN_16(0xffff), QUICKSPI_CONFIG1_UNKNOWN_16(1));
  2129. +
  2130. + // TODO Do we need to set any of the following bits here?
  2131. + //bitsb_set(&ithc->regs->dma_rx[1].control2, DMA_RX_CONTROL2_UNKNOWN_4);
  2132. + //bitsb_set(&ithc->regs->dma_rx[0].control2, DMA_RX_CONTROL2_UNKNOWN_5);
  2133. + //bitsb_set(&ithc->regs->dma_rx[1].control2, DMA_RX_CONTROL2_UNKNOWN_5);
  2134. + //bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_3);
  2135. + //bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  2136. +
  2137. + ithc_log_regs(ithc);
  2138. +
  2139. + return 0;
  2140. +}
  2141. +
  2142. +void ithc_quickspi_exit(struct ithc *ithc)
  2143. +{
  2144. + // TODO Should we send HIDSPI 'power off' command?
  2145. + //struct hidspi_header h = { .type = HIDSPI_OUTPUT_TYPE_COMMAND, .id = 3, };
  2146. + //struct ithc_data d = { .type = ITHC_DATA_RAW, .data = &h, .size = sizeof(h) };
  2147. + //CHECK(ithc_dma_tx, ithc, &d); // or ithc_spi_command()
  2148. +}
  2149. +
  2150. +int ithc_quickspi_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest)
  2151. +{
  2152. + const struct hidspi_header *hdr = src;
  2153. +
  2154. + if (len < sizeof(*hdr))
  2155. + return -ENODATA;
  2156. + // TODO Do we need to handle HIDSPI packet fragmentation?
  2157. + if (len < sizeof(*hdr) + hdr->len)
  2158. + return -EMSGSIZE;
  2159. + if (len > round_up(sizeof(*hdr) + hdr->len, 4))
  2160. + return -EMSGSIZE;
  2161. +
  2162. + switch (hdr->type) {
  2163. + case HIDSPI_INPUT_TYPE_RESET_RESPONSE:
  2164. + // TODO "When the device detects an error condition, it may interrupt and make
  2165. + // available to the host an Input Report containing an unsolicited Reset Response.
  2166. + // After receiving an unsolicited Reset Response, the host shall initiate the
  2167. + // request procedure from step (4) in the [HIDSPI initialization] process."
  2168. + dest->type = ITHC_DATA_ERROR;
  2169. + return 0;
  2170. + case HIDSPI_INPUT_TYPE_REPORT_DESCRIPTOR:
  2171. + dest->type = ITHC_DATA_REPORT_DESCRIPTOR;
  2172. + dest->data = hdr + 1;
  2173. + dest->size = hdr->len;
  2174. + return 0;
  2175. + case HIDSPI_INPUT_TYPE_DATA:
  2176. + case HIDSPI_INPUT_TYPE_GET_INPUT_REPORT_RESPONSE:
  2177. + dest->type = ITHC_DATA_INPUT_REPORT;
  2178. + dest->data = &hdr->id;
  2179. + dest->size = hdr->len + 1;
  2180. + return 0;
  2181. + case HIDSPI_INPUT_TYPE_GET_FEATURE_RESPONSE:
  2182. + dest->type = ITHC_DATA_GET_FEATURE;
  2183. + dest->data = &hdr->id;
  2184. + dest->size = hdr->len + 1;
  2185. + return 0;
  2186. + case HIDSPI_INPUT_TYPE_SET_FEATURE_RESPONSE:
  2187. + case HIDSPI_INPUT_TYPE_OUTPUT_REPORT_RESPONSE:
  2188. + dest->type = ITHC_DATA_IGNORE;
  2189. + return 0;
  2190. + default:
  2191. + return -EINVAL;
  2192. + }
  2193. +}
  2194. +
  2195. +ssize_t ithc_quickspi_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  2196. + size_t maxlen)
  2197. +{
  2198. + struct hidspi_header *hdr = dest;
  2199. +
  2200. + size_t src_size = src->size;
  2201. + const u8 *src_data = src->data;
  2202. + u8 type;
  2203. +
  2204. + switch (src->type) {
  2205. + case ITHC_DATA_SET_FEATURE:
  2206. + type = HIDSPI_OUTPUT_TYPE_SET_FEATURE;
  2207. + break;
  2208. + case ITHC_DATA_GET_FEATURE:
  2209. + type = HIDSPI_OUTPUT_TYPE_GET_FEATURE;
  2210. + break;
  2211. + case ITHC_DATA_OUTPUT_REPORT:
  2212. + type = HIDSPI_OUTPUT_TYPE_OUTPUT_REPORT;
  2213. + break;
  2214. + case ITHC_DATA_REPORT_DESCRIPTOR:
  2215. + type = HIDSPI_OUTPUT_TYPE_REPORT_DESCRIPTOR_REQUEST;
  2216. + src_size = 0;
  2217. + break;
  2218. + default:
  2219. + return -EINVAL;
  2220. + }
  2221. +
  2222. + u8 id = 0;
  2223. + if (src_size) {
  2224. + id = *src_data++;
  2225. + src_size--;
  2226. + }
  2227. +
  2228. + // Data must be padded to next 4-byte boundary.
  2229. + size_t padded = round_up(src_size, 4);
  2230. + if (sizeof(*hdr) + padded > maxlen)
  2231. + return -EOVERFLOW;
  2232. +
  2233. + // Fill the TX buffer with header and data.
  2234. + hdr->type = type;
  2235. + hdr->len = (u16)src_size;
  2236. + hdr->id = id;
  2237. + memcpy_and_pad(hdr + 1, padded, src_data, src_size, 0);
  2238. +
  2239. + return sizeof(*hdr) + padded;
  2240. +}
  2241. +
  2242. diff --git a/drivers/hid/ithc/ithc-quickspi.h b/drivers/hid/ithc/ithc-quickspi.h
  2243. new file mode 100644
  2244. index 000000000000..74d882f6b2f0
  2245. --- /dev/null
  2246. +++ b/drivers/hid/ithc/ithc-quickspi.h
  2247. @@ -0,0 +1,39 @@
  2248. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2249. +
  2250. +struct ithc_acpi_config {
  2251. + bool has_config: 1;
  2252. + bool has_input_report_header_address: 1;
  2253. + bool has_input_report_body_address: 1;
  2254. + bool has_output_report_body_address: 1;
  2255. + bool has_read_opcode: 1;
  2256. + bool has_write_opcode: 1;
  2257. + bool has_read_mode: 1;
  2258. + bool has_write_mode: 1;
  2259. + bool has_spi_frequency: 1;
  2260. + bool has_limit_packet_size: 1;
  2261. + bool has_tx_delay: 1;
  2262. + bool has_active_ltr: 1;
  2263. + bool has_idle_ltr: 1;
  2264. + u32 input_report_header_address;
  2265. + u32 input_report_body_address;
  2266. + u32 output_report_body_address;
  2267. + u8 read_opcode;
  2268. + u8 write_opcode;
  2269. + u8 read_mode;
  2270. + u8 write_mode;
  2271. + u32 spi_frequency;
  2272. + u32 limit_packet_size;
  2273. + u32 tx_delay; // us/10 // TODO use?
  2274. + u32 active_ltr; // ns/1024
  2275. + u32 idle_ltr; // ns/1024
  2276. +};
  2277. +
  2278. +int ithc_read_acpi_config(struct ithc *ithc, struct ithc_acpi_config *cfg);
  2279. +void ithc_print_acpi_config(struct ithc *ithc, const struct ithc_acpi_config *cfg);
  2280. +
  2281. +int ithc_quickspi_init(struct ithc *ithc, const struct ithc_acpi_config *cfg);
  2282. +void ithc_quickspi_exit(struct ithc *ithc);
  2283. +int ithc_quickspi_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest);
  2284. +ssize_t ithc_quickspi_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  2285. + size_t maxlen);
  2286. +
  2287. diff --git a/drivers/hid/ithc/ithc-regs.c b/drivers/hid/ithc/ithc-regs.c
  2288. new file mode 100644
  2289. index 000000000000..c0f13506af20
  2290. --- /dev/null
  2291. +++ b/drivers/hid/ithc/ithc-regs.c
  2292. @@ -0,0 +1,154 @@
  2293. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  2294. +
  2295. +#include "ithc.h"
  2296. +
  2297. +#define reg_num(r) (0x1fff & (u16)(__force u64)(r))
  2298. +
  2299. +void bitsl(__iomem u32 *reg, u32 mask, u32 val)
  2300. +{
  2301. + if (val & ~mask)
  2302. + pr_err("register 0x%x: invalid value 0x%x for bitmask 0x%x\n",
  2303. + reg_num(reg), val, mask);
  2304. + writel((readl(reg) & ~mask) | (val & mask), reg);
  2305. +}
  2306. +
  2307. +void bitsb(__iomem u8 *reg, u8 mask, u8 val)
  2308. +{
  2309. + if (val & ~mask)
  2310. + pr_err("register 0x%x: invalid value 0x%x for bitmask 0x%x\n",
  2311. + reg_num(reg), val, mask);
  2312. + writeb((readb(reg) & ~mask) | (val & mask), reg);
  2313. +}
  2314. +
  2315. +int waitl(struct ithc *ithc, __iomem u32 *reg, u32 mask, u32 val)
  2316. +{
  2317. + ithc_log_regs(ithc);
  2318. + pci_dbg(ithc->pci, "waiting for reg 0x%04x mask 0x%08x val 0x%08x\n",
  2319. + reg_num(reg), mask, val);
  2320. + u32 x;
  2321. + if (readl_poll_timeout(reg, x, (x & mask) == val, 200, 1000*1000)) {
  2322. + ithc_log_regs(ithc);
  2323. + pci_err(ithc->pci, "timed out waiting for reg 0x%04x mask 0x%08x val 0x%08x\n",
  2324. + reg_num(reg), mask, val);
  2325. + return -ETIMEDOUT;
  2326. + }
  2327. + ithc_log_regs(ithc);
  2328. + pci_dbg(ithc->pci, "done waiting\n");
  2329. + return 0;
  2330. +}
  2331. +
  2332. +int waitb(struct ithc *ithc, __iomem u8 *reg, u8 mask, u8 val)
  2333. +{
  2334. + ithc_log_regs(ithc);
  2335. + pci_dbg(ithc->pci, "waiting for reg 0x%04x mask 0x%02x val 0x%02x\n",
  2336. + reg_num(reg), mask, val);
  2337. + u8 x;
  2338. + if (readb_poll_timeout(reg, x, (x & mask) == val, 200, 1000*1000)) {
  2339. + ithc_log_regs(ithc);
  2340. + pci_err(ithc->pci, "timed out waiting for reg 0x%04x mask 0x%02x val 0x%02x\n",
  2341. + reg_num(reg), mask, val);
  2342. + return -ETIMEDOUT;
  2343. + }
  2344. + ithc_log_regs(ithc);
  2345. + pci_dbg(ithc->pci, "done waiting\n");
  2346. + return 0;
  2347. +}
  2348. +
  2349. +static void calc_ltr(u64 *ns, unsigned int *val, unsigned int *scale)
  2350. +{
  2351. + unsigned int s = 0;
  2352. + u64 v = *ns;
  2353. + while (v > 0x3ff) {
  2354. + s++;
  2355. + v >>= 5;
  2356. + }
  2357. + if (s > 5) {
  2358. + s = 5;
  2359. + v = 0x3ff;
  2360. + }
  2361. + *val = v;
  2362. + *scale = s;
  2363. + *ns = v << (5 * s);
  2364. +}
  2365. +
  2366. +void ithc_set_ltr_config(struct ithc *ithc, u64 active_ltr_ns, u64 idle_ltr_ns)
  2367. +{
  2368. + unsigned int active_val, active_scale, idle_val, idle_scale;
  2369. + calc_ltr(&active_ltr_ns, &active_val, &active_scale);
  2370. + calc_ltr(&idle_ltr_ns, &idle_val, &idle_scale);
  2371. + pci_dbg(ithc->pci, "setting active LTR value to %llu ns, idle LTR value to %llu ns\n",
  2372. + active_ltr_ns, idle_ltr_ns);
  2373. + writel(LTR_CONFIG_ENABLE_ACTIVE | LTR_CONFIG_ENABLE_IDLE | LTR_CONFIG_APPLY |
  2374. + LTR_CONFIG_ACTIVE_LTR_SCALE(active_scale) | LTR_CONFIG_ACTIVE_LTR_VALUE(active_val) |
  2375. + LTR_CONFIG_IDLE_LTR_SCALE(idle_scale) | LTR_CONFIG_IDLE_LTR_VALUE(idle_val),
  2376. + &ithc->regs->ltr_config);
  2377. +}
  2378. +
  2379. +void ithc_set_ltr_idle(struct ithc *ithc)
  2380. +{
  2381. + u32 ltr = readl(&ithc->regs->ltr_config);
  2382. + switch (ltr & (LTR_CONFIG_STATUS_ACTIVE | LTR_CONFIG_STATUS_IDLE)) {
  2383. + case LTR_CONFIG_STATUS_IDLE:
  2384. + break;
  2385. + case LTR_CONFIG_STATUS_ACTIVE:
  2386. + writel(ltr | LTR_CONFIG_TOGGLE | LTR_CONFIG_APPLY, &ithc->regs->ltr_config);
  2387. + break;
  2388. + default:
  2389. + pci_err(ithc->pci, "invalid LTR state 0x%08x\n", ltr);
  2390. + break;
  2391. + }
  2392. +}
  2393. +
  2394. +int ithc_set_spi_config(struct ithc *ithc, u8 clkdiv, bool clkdiv8, u8 read_mode, u8 write_mode)
  2395. +{
  2396. + if (clkdiv == 0 || clkdiv > 7 || read_mode > SPI_MODE_QUAD || write_mode > SPI_MODE_QUAD)
  2397. + return -EINVAL;
  2398. + static const char * const modes[] = { "single", "dual", "quad" };
  2399. + pci_dbg(ithc->pci, "setting SPI frequency to %i Hz, %s read, %s write\n",
  2400. + SPI_CLK_FREQ_BASE / (clkdiv * (clkdiv8 ? 8 : 1)),
  2401. + modes[read_mode], modes[write_mode]);
  2402. + bitsl(&ithc->regs->spi_config,
  2403. + SPI_CONFIG_READ_MODE(0xff) | SPI_CONFIG_READ_CLKDIV(0xff) |
  2404. + SPI_CONFIG_WRITE_MODE(0xff) | SPI_CONFIG_WRITE_CLKDIV(0xff) |
  2405. + SPI_CONFIG_CLKDIV_8,
  2406. + SPI_CONFIG_READ_MODE(read_mode) | SPI_CONFIG_READ_CLKDIV(clkdiv) |
  2407. + SPI_CONFIG_WRITE_MODE(write_mode) | SPI_CONFIG_WRITE_CLKDIV(clkdiv) |
  2408. + (clkdiv8 ? SPI_CONFIG_CLKDIV_8 : 0));
  2409. + return 0;
  2410. +}
  2411. +
  2412. +int ithc_spi_command(struct ithc *ithc, u8 command, u32 offset, u32 size, void *data)
  2413. +{
  2414. + pci_dbg(ithc->pci, "SPI command %u, size %u, offset 0x%x\n", command, size, offset);
  2415. + if (size > sizeof(ithc->regs->spi_cmd.data))
  2416. + return -EINVAL;
  2417. +
  2418. + // Wait if the device is still busy.
  2419. + CHECK_RET(waitl, ithc, &ithc->regs->spi_cmd.status, SPI_CMD_STATUS_BUSY, 0);
  2420. + // Clear result flags.
  2421. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  2422. +
  2423. + // Init SPI command data.
  2424. + writeb(command, &ithc->regs->spi_cmd.code);
  2425. + writew(size, &ithc->regs->spi_cmd.size);
  2426. + writel(offset, &ithc->regs->spi_cmd.offset);
  2427. + u32 *p = data, n = (size + 3) / 4;
  2428. + for (u32 i = 0; i < n; i++)
  2429. + writel(p[i], &ithc->regs->spi_cmd.data[i]);
  2430. +
  2431. + // Start transmission.
  2432. + bitsb_set(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_SEND);
  2433. + CHECK_RET(waitl, ithc, &ithc->regs->spi_cmd.status, SPI_CMD_STATUS_BUSY, 0);
  2434. +
  2435. + // Read response.
  2436. + if ((readl(&ithc->regs->spi_cmd.status) & (SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR)) != SPI_CMD_STATUS_DONE)
  2437. + return -EIO;
  2438. + if (readw(&ithc->regs->spi_cmd.size) != size)
  2439. + return -EMSGSIZE;
  2440. + for (u32 i = 0; i < n; i++)
  2441. + p[i] = readl(&ithc->regs->spi_cmd.data[i]);
  2442. +
  2443. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  2444. + return 0;
  2445. +}
  2446. +
  2447. diff --git a/drivers/hid/ithc/ithc-regs.h b/drivers/hid/ithc/ithc-regs.h
  2448. new file mode 100644
  2449. index 000000000000..4f541fe533fa
  2450. --- /dev/null
  2451. +++ b/drivers/hid/ithc/ithc-regs.h
  2452. @@ -0,0 +1,211 @@
  2453. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2454. +
  2455. +#define LTR_CONFIG_ENABLE_ACTIVE BIT(0)
  2456. +#define LTR_CONFIG_TOGGLE BIT(1)
  2457. +#define LTR_CONFIG_ENABLE_IDLE BIT(2)
  2458. +#define LTR_CONFIG_APPLY BIT(3)
  2459. +#define LTR_CONFIG_IDLE_LTR_SCALE(x) (((x) & 7) << 4)
  2460. +#define LTR_CONFIG_IDLE_LTR_VALUE(x) (((x) & 0x3ff) << 7)
  2461. +#define LTR_CONFIG_ACTIVE_LTR_SCALE(x) (((x) & 7) << 17)
  2462. +#define LTR_CONFIG_ACTIVE_LTR_VALUE(x) (((x) & 0x3ff) << 20)
  2463. +#define LTR_CONFIG_STATUS_ACTIVE BIT(30)
  2464. +#define LTR_CONFIG_STATUS_IDLE BIT(31)
  2465. +
  2466. +#define CONTROL_QUIESCE BIT(1)
  2467. +#define CONTROL_IS_QUIESCED BIT(2)
  2468. +#define CONTROL_NRESET BIT(3)
  2469. +#define CONTROL_UNKNOWN_24(x) (((x) & 3) << 24)
  2470. +#define CONTROL_READY BIT(29)
  2471. +
  2472. +#define SPI_CONFIG_READ_MODE(x) (((x) & 3) << 2)
  2473. +#define SPI_CONFIG_READ_CLKDIV(x) (((x) & 7) << 4)
  2474. +#define SPI_CONFIG_READ_PACKET_SIZE(x) (((x) & 0x1ff) << 7)
  2475. +#define SPI_CONFIG_WRITE_MODE(x) (((x) & 3) << 18)
  2476. +#define SPI_CONFIG_WRITE_CLKDIV(x) (((x) & 7) << 20)
  2477. +#define SPI_CONFIG_CLKDIV_8 BIT(23) // additionally divide clk by 8, for both read and write
  2478. +#define SPI_CONFIG_WRITE_PACKET_SIZE(x) (((x) & 0xff) << 24)
  2479. +
  2480. +#define SPI_CLK_FREQ_BASE 125000000
  2481. +#define SPI_MODE_SINGLE 0
  2482. +#define SPI_MODE_DUAL 1
  2483. +#define SPI_MODE_QUAD 2
  2484. +
  2485. +#define ERROR_CONTROL_UNKNOWN_0 BIT(0)
  2486. +#define ERROR_CONTROL_DISABLE_DMA BIT(1) // clears DMA_RX_CONTROL_ENABLE when a DMA error occurs
  2487. +#define ERROR_CONTROL_UNKNOWN_2 BIT(2)
  2488. +#define ERROR_CONTROL_UNKNOWN_3 BIT(3)
  2489. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_9 BIT(9)
  2490. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_10 BIT(10)
  2491. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_12 BIT(12)
  2492. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_13 BIT(13)
  2493. +#define ERROR_CONTROL_UNKNOWN_16(x) (((x) & 0xff) << 16) // spi error code irq?
  2494. +#define ERROR_CONTROL_SET_DMA_STATUS BIT(29) // sets DMA_RX_STATUS_ERROR when a DMA error occurs
  2495. +
  2496. +#define ERROR_STATUS_DMA BIT(28)
  2497. +#define ERROR_STATUS_SPI BIT(30)
  2498. +
  2499. +#define ERROR_FLAG_DMA_UNKNOWN_9 BIT(9)
  2500. +#define ERROR_FLAG_DMA_UNKNOWN_10 BIT(10)
  2501. +#define ERROR_FLAG_DMA_RX_TIMEOUT BIT(12) // set when we receive a truncated DMA message
  2502. +#define ERROR_FLAG_DMA_UNKNOWN_13 BIT(13)
  2503. +#define ERROR_FLAG_SPI_BUS_TURNAROUND BIT(16)
  2504. +#define ERROR_FLAG_SPI_RESPONSE_TIMEOUT BIT(17)
  2505. +#define ERROR_FLAG_SPI_INTRA_PACKET_TIMEOUT BIT(18)
  2506. +#define ERROR_FLAG_SPI_INVALID_RESPONSE BIT(19)
  2507. +#define ERROR_FLAG_SPI_HS_RX_TIMEOUT BIT(20)
  2508. +#define ERROR_FLAG_SPI_TOUCH_IC_INIT BIT(21)
  2509. +
  2510. +#define SPI_CMD_CONTROL_SEND BIT(0) // cleared by device when sending is complete
  2511. +#define SPI_CMD_CONTROL_IRQ BIT(1)
  2512. +
  2513. +#define SPI_CMD_CODE_READ 4
  2514. +#define SPI_CMD_CODE_WRITE 6
  2515. +
  2516. +#define SPI_CMD_STATUS_DONE BIT(0)
  2517. +#define SPI_CMD_STATUS_ERROR BIT(1)
  2518. +#define SPI_CMD_STATUS_BUSY BIT(3)
  2519. +
  2520. +#define DMA_TX_CONTROL_SEND BIT(0) // cleared by device when sending is complete
  2521. +#define DMA_TX_CONTROL_IRQ BIT(3)
  2522. +
  2523. +#define DMA_TX_STATUS_DONE BIT(0)
  2524. +#define DMA_TX_STATUS_ERROR BIT(1)
  2525. +#define DMA_TX_STATUS_UNKNOWN_2 BIT(2)
  2526. +#define DMA_TX_STATUS_UNKNOWN_3 BIT(3) // busy?
  2527. +
  2528. +#define INPUT_HEADER_VERSION(x) ((x) & 0xf)
  2529. +#define INPUT_HEADER_REPORT_LENGTH(x) (((x) >> 8) & 0x3fff)
  2530. +#define INPUT_HEADER_SYNC(x) ((x) >> 24)
  2531. +#define INPUT_HEADER_VERSION_VALUE 3
  2532. +#define INPUT_HEADER_SYNC_VALUE 0x5a
  2533. +
  2534. +#define QUICKSPI_CONFIG1_UNKNOWN_0(x) (((x) & 0x1f) << 0)
  2535. +#define QUICKSPI_CONFIG1_UNKNOWN_5(x) (((x) & 0x1f) << 5)
  2536. +#define QUICKSPI_CONFIG1_UNKNOWN_10(x) (((x) & 0x1f) << 10)
  2537. +#define QUICKSPI_CONFIG1_UNKNOWN_16(x) (((x) & 0xffff) << 16)
  2538. +
  2539. +#define QUICKSPI_CONFIG2_UNKNOWN_0(x) (((x) & 0x1f) << 0)
  2540. +#define QUICKSPI_CONFIG2_UNKNOWN_5(x) (((x) & 0x1f) << 5)
  2541. +#define QUICKSPI_CONFIG2_UNKNOWN_12(x) (((x) & 0xf) << 12)
  2542. +#define QUICKSPI_CONFIG2_UNKNOWN_16 BIT(16)
  2543. +#define QUICKSPI_CONFIG2_UNKNOWN_17 BIT(17)
  2544. +#define QUICKSPI_CONFIG2_DISABLE_READ_ADDRESS_INCREMENT BIT(24)
  2545. +#define QUICKSPI_CONFIG2_DISABLE_WRITE_ADDRESS_INCREMENT BIT(25)
  2546. +#define QUICKSPI_CONFIG2_ENABLE_WRITE_STREAMING_MODE BIT(27)
  2547. +#define QUICKSPI_CONFIG2_IRQ_POLARITY BIT(28)
  2548. +
  2549. +#define DMA_RX_CONTROL_ENABLE BIT(0)
  2550. +#define DMA_RX_CONTROL_IRQ_UNKNOWN_1 BIT(1) // rx1 only?
  2551. +#define DMA_RX_CONTROL_IRQ_ERROR BIT(3) // rx1 only?
  2552. +#define DMA_RX_CONTROL_IRQ_READY BIT(4) // rx0 only
  2553. +#define DMA_RX_CONTROL_IRQ_DATA BIT(5)
  2554. +
  2555. +#define DMA_RX_CONTROL2_UNKNOWN_4 BIT(4) // rx1 only?
  2556. +#define DMA_RX_CONTROL2_UNKNOWN_5 BIT(5) // rx0 only?
  2557. +#define DMA_RX_CONTROL2_RESET BIT(7) // resets ringbuffer indices
  2558. +
  2559. +#define DMA_RX_WRAP_FLAG BIT(7)
  2560. +
  2561. +#define DMA_RX_STATUS_ERROR BIT(3)
  2562. +#define DMA_RX_STATUS_READY BIT(4) // set in rx0 after using CONTROL_NRESET when it becomes possible to read config (can take >100ms)
  2563. +#define DMA_RX_STATUS_HAVE_DATA BIT(5)
  2564. +#define DMA_RX_STATUS_ENABLED BIT(8)
  2565. +
  2566. +#define INIT_UNKNOWN_GUC_2 BIT(2)
  2567. +#define INIT_UNKNOWN_3 BIT(3)
  2568. +#define INIT_UNKNOWN_GUC_4 BIT(4)
  2569. +#define INIT_UNKNOWN_5 BIT(5)
  2570. +#define INIT_UNKNOWN_31 BIT(31)
  2571. +
  2572. +// COUNTER_RESET can be written to counter registers to reset them to zero. However, in some cases this can mess up the THC.
  2573. +#define COUNTER_RESET BIT(31)
  2574. +
  2575. +struct ithc_registers {
  2576. + /* 0000 */ u32 _unknown_0000[5];
  2577. + /* 0014 */ u32 ltr_config;
  2578. + /* 0018 */ u32 _unknown_0018[1018];
  2579. + /* 1000 */ u32 _unknown_1000;
  2580. + /* 1004 */ u32 _unknown_1004;
  2581. + /* 1008 */ u32 control_bits;
  2582. + /* 100c */ u32 _unknown_100c;
  2583. + /* 1010 */ u32 spi_config;
  2584. + struct {
  2585. + /* 1014/1018/101c */ u8 header;
  2586. + /* 1015/1019/101d */ u8 quad;
  2587. + /* 1016/101a/101e */ u8 dual;
  2588. + /* 1017/101b/101f */ u8 single;
  2589. + } opcode[3];
  2590. + /* 1020 */ u32 error_control;
  2591. + /* 1024 */ u32 error_status; // write to clear
  2592. + /* 1028 */ u32 error_flags; // write to clear
  2593. + /* 102c */ u32 _unknown_102c[5];
  2594. + struct {
  2595. + /* 1040 */ u8 control;
  2596. + /* 1041 */ u8 code;
  2597. + /* 1042 */ u16 size;
  2598. + /* 1044 */ u32 status; // write to clear
  2599. + /* 1048 */ u32 offset;
  2600. + /* 104c */ u32 data[16];
  2601. + /* 108c */ u32 _unknown_108c;
  2602. + } spi_cmd;
  2603. + struct {
  2604. + /* 1090 */ u64 addr; // cannot be written with writeq(), must use lo_hi_writeq()
  2605. + /* 1098 */ u8 control;
  2606. + /* 1099 */ u8 _unknown_1099;
  2607. + /* 109a */ u8 _unknown_109a;
  2608. + /* 109b */ u8 num_prds;
  2609. + /* 109c */ u32 status; // write to clear
  2610. + /* 10a0 */ u32 _unknown_10a0[5];
  2611. + /* 10b4 */ u32 spi_addr;
  2612. + } dma_tx;
  2613. + /* 10b8 */ u32 spi_header_addr;
  2614. + union {
  2615. + /* 10bc */ u32 irq_cause; // in legacy THC mode
  2616. + /* 10bc */ u32 input_header; // in QuickSPI mode (see HIDSPI spec)
  2617. + };
  2618. + /* 10c0 */ u32 _unknown_10c0[8];
  2619. + /* 10e0 */ u32 _unknown_10e0_counters[3];
  2620. + /* 10ec */ u32 quickspi_config1;
  2621. + /* 10f0 */ u32 quickspi_config2;
  2622. + /* 10f4 */ u32 _unknown_10f4[3];
  2623. + struct {
  2624. + /* 1100/1200 */ u64 addr; // cannot be written with writeq(), must use lo_hi_writeq()
  2625. + /* 1108/1208 */ u8 num_bufs;
  2626. + /* 1109/1209 */ u8 num_prds;
  2627. + /* 110a/120a */ u16 _unknown_110a;
  2628. + /* 110c/120c */ u8 control;
  2629. + /* 110d/120d */ u8 head;
  2630. + /* 110e/120e */ u8 tail;
  2631. + /* 110f/120f */ u8 control2;
  2632. + /* 1110/1210 */ u32 status; // write to clear
  2633. + /* 1114/1214 */ u32 _unknown_1114;
  2634. + /* 1118/1218 */ u64 _unknown_1118_guc_addr;
  2635. + /* 1120/1220 */ u32 _unknown_1120_guc;
  2636. + /* 1124/1224 */ u32 _unknown_1124_guc;
  2637. + /* 1128/1228 */ u32 init_unknown;
  2638. + /* 112c/122c */ u32 _unknown_112c;
  2639. + /* 1130/1230 */ u64 _unknown_1130_guc_addr;
  2640. + /* 1138/1238 */ u32 _unknown_1138_guc;
  2641. + /* 113c/123c */ u32 _unknown_113c;
  2642. + /* 1140/1240 */ u32 _unknown_1140_guc;
  2643. + /* 1144/1244 */ u32 _unknown_1144[11];
  2644. + /* 1170/1270 */ u32 spi_addr;
  2645. + /* 1174/1274 */ u32 _unknown_1174[11];
  2646. + /* 11a0/12a0 */ u32 _unknown_11a0_counters[6];
  2647. + /* 11b8/12b8 */ u32 _unknown_11b8[18];
  2648. + } dma_rx[2];
  2649. +};
  2650. +static_assert(sizeof(struct ithc_registers) == 0x1300);
  2651. +
  2652. +void bitsl(__iomem u32 *reg, u32 mask, u32 val);
  2653. +void bitsb(__iomem u8 *reg, u8 mask, u8 val);
  2654. +#define bitsl_set(reg, x) bitsl(reg, x, x)
  2655. +#define bitsb_set(reg, x) bitsb(reg, x, x)
  2656. +int waitl(struct ithc *ithc, __iomem u32 *reg, u32 mask, u32 val);
  2657. +int waitb(struct ithc *ithc, __iomem u8 *reg, u8 mask, u8 val);
  2658. +
  2659. +void ithc_set_ltr_config(struct ithc *ithc, u64 active_ltr_ns, u64 idle_ltr_ns);
  2660. +void ithc_set_ltr_idle(struct ithc *ithc);
  2661. +int ithc_set_spi_config(struct ithc *ithc, u8 clkdiv, bool clkdiv8, u8 read_mode, u8 write_mode);
  2662. +int ithc_spi_command(struct ithc *ithc, u8 command, u32 offset, u32 size, void *data);
  2663. +
  2664. diff --git a/drivers/hid/ithc/ithc.h b/drivers/hid/ithc/ithc.h
  2665. new file mode 100644
  2666. index 000000000000..aec320d4e945
  2667. --- /dev/null
  2668. +++ b/drivers/hid/ithc/ithc.h
  2669. @@ -0,0 +1,89 @@
  2670. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2671. +
  2672. +#include <linux/acpi.h>
  2673. +#include <linux/debugfs.h>
  2674. +#include <linux/delay.h>
  2675. +#include <linux/dma-mapping.h>
  2676. +#include <linux/hid.h>
  2677. +#include <linux/highmem.h>
  2678. +#include <linux/input.h>
  2679. +#include <linux/io-64-nonatomic-lo-hi.h>
  2680. +#include <linux/iopoll.h>
  2681. +#include <linux/kthread.h>
  2682. +#include <linux/miscdevice.h>
  2683. +#include <linux/module.h>
  2684. +#include <linux/pci.h>
  2685. +#include <linux/poll.h>
  2686. +#include <linux/timer.h>
  2687. +#include <linux/vmalloc.h>
  2688. +
  2689. +#define DEVNAME "ithc"
  2690. +#define DEVFULLNAME "Intel Touch Host Controller"
  2691. +
  2692. +#undef pr_fmt
  2693. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2694. +
  2695. +#define CHECK(fn, ...) ({ int r = fn(__VA_ARGS__); if (r < 0) pci_err(ithc->pci, "%s: %s failed with %i\n", __func__, #fn, r); r; })
  2696. +#define CHECK_RET(...) do { int r = CHECK(__VA_ARGS__); if (r < 0) return r; } while (0)
  2697. +
  2698. +#define NUM_RX_BUF 16
  2699. +
  2700. +// PCI device IDs:
  2701. +// Lakefield
  2702. +#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT1 0x98d0
  2703. +#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT2 0x98d1
  2704. +// Tiger Lake
  2705. +#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT1 0xa0d0
  2706. +#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT2 0xa0d1
  2707. +#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT1 0x43d0
  2708. +#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT2 0x43d1
  2709. +// Alder Lake
  2710. +#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1 0x7ad8
  2711. +#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2 0x7ad9
  2712. +#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1 0x51d0
  2713. +#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2 0x51d1
  2714. +#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1 0x54d0
  2715. +#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2 0x54d1
  2716. +// Raptor Lake
  2717. +#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT1 0x7a58
  2718. +#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT2 0x7a59
  2719. +// Meteor Lake
  2720. +#define PCI_DEVICE_ID_INTEL_THC_MTL_S_PORT1 0x7f59
  2721. +#define PCI_DEVICE_ID_INTEL_THC_MTL_S_PORT2 0x7f5b
  2722. +#define PCI_DEVICE_ID_INTEL_THC_MTL_MP_PORT1 0x7e49
  2723. +#define PCI_DEVICE_ID_INTEL_THC_MTL_MP_PORT2 0x7e4b
  2724. +
  2725. +struct ithc;
  2726. +
  2727. +#include "ithc-regs.h"
  2728. +#include "ithc-hid.h"
  2729. +#include "ithc-dma.h"
  2730. +#include "ithc-legacy.h"
  2731. +#include "ithc-quickspi.h"
  2732. +#include "ithc-debug.h"
  2733. +
  2734. +struct ithc {
  2735. + char phys[32];
  2736. + struct pci_dev *pci;
  2737. + int irq;
  2738. + struct task_struct *poll_thread;
  2739. + struct timer_list idle_timer;
  2740. +
  2741. + struct ithc_registers __iomem *regs;
  2742. + struct ithc_registers *prev_regs; // for debugging
  2743. + struct ithc_dma_rx dma_rx[2];
  2744. + struct ithc_dma_tx dma_tx;
  2745. + struct ithc_hid hid;
  2746. +
  2747. + bool use_quickspi;
  2748. + bool have_config;
  2749. + u16 vendor_id;
  2750. + u16 product_id;
  2751. + u32 product_rev;
  2752. + u32 max_rx_size;
  2753. + u32 max_tx_size;
  2754. + u32 legacy_touch_cfg;
  2755. +};
  2756. +
  2757. +int ithc_reset(struct ithc *ithc);
  2758. +
  2759. --
  2760. 2.49.0